History

fftools/ffmpeg_mux: move OutputStream.enc_ctx to Encoder

The encoding AVCodecContext is a part of the encoder, and so should live there.

Anton Khirnov committed 2 years ago

Commit 64f3feb1 parent 434377a5 files · +62 −54Browse files

Unified · Split

5 files
  1. fftools/ffmpeg.c +3 −3
  2. fftools/ffmpeg.h +2 −1
  3. fftools/ffmpeg_enc.c +24 −9
  4. fftools/ffmpeg_mux.c +2 −6
  5. fftools/ffmpeg_mux_init.c +31 −35
fftools/ffmpeg.c+3−3
@@ -728,7 +728,7 @@ static void print_stream_maps(void) · 727 unmodified lines
728728 av_log(NULL, AV_LOG_INFO, " (graph %d)", ost->filter->graph->index);
729729
730730 av_log(NULL, AV_LOG_INFO, " -> Stream #%d:%d (%s)\n", ost->file->index,
731− ost->index, ost->enc_ctx->codec->name);
731+ ost->index, ost->enc->enc_ctx->codec->name);
732732 continue;
733733 }
734734
@@ -737,9 +737,9 @@ static void print_stream_maps(void) · 2 unmodified lines
737737 ost->ist->index,
738738 ost->file->index,
739739 ost->index);
740− if (ost->enc_ctx) {
740+ if (ost->enc) {
741741 const AVCodec *in_codec = ost->ist->dec;
742− const AVCodec *out_codec = ost->enc_ctx->codec;
742+ const AVCodec *out_codec = ost->enc->enc_ctx->codec;
743743 const char *decoder_name = "?";
744744 const char *in_codec_name = "?";
745745 const char *encoder_name = "?";
fftools/ffmpeg.h+2−1
@@ -572,6 +572,8 @@ typedef struct KeyframeForceCtx { · 571 unmodified lines
572572 typedef struct Encoder {
573573 const AVClass *class;
574574
575+ AVCodecContext *enc_ctx;
576+
575577 // number of frames/samples sent to the encoder
576578 uint64_t frames_encoded;
577579 uint64_t samples_encoded;
@@ -602,7 +604,6 @@ typedef struct OutputStream { · 24 unmodified lines
602604 AVStream *st; /* stream in the output file */
603605
604606 Encoder *enc;
605− AVCodecContext *enc_ctx;
606607
607608 /* video only */
608609 #if FFMPEG_OPT_TOP
fftools/ffmpeg_enc.c+24−9
@@ -75,6 +75,10 @@ void enc_free(Encoder **penc) · 74 unmodified lines
7575 if (!enc)
7676 return;
7777
78+ if (enc->enc_ctx)
79+ av_freep(&enc->enc_ctx->stats_in);
80+ avcodec_free_context(&enc->enc_ctx);
81+
7882 av_freep(penc);
7983 }
8084
@@ -96,6 +100,7 @@ int enc_alloc(Encoder **penc, const AVCodec *codec, · 15 unmodified lines
96100 Scheduler *sch, unsigned sch_idx, void *log_parent)
97101 {
98102 EncoderPriv *ep;
103+ int ret = 0;
99104
100105 *penc = NULL;
101106
@@ -111,9 +116,18 @@ int enc_alloc(Encoder **penc, const AVCodec *codec, · 9 unmodified lines
111116
112117 snprintf(ep->log_name, sizeof(ep->log_name), "enc:%s", codec->name);
113118
119+ ep->e.enc_ctx = avcodec_alloc_context3(codec);
120+ if (!ep->e.enc_ctx) {
121+ ret = AVERROR(ENOMEM);
122+ goto fail;
123+ }
124+
114125 *penc = &ep->e;
115126
116127 return 0;
128+fail:
129+ enc_free((Encoder**)&ep);
130+ return ret;
117131 }
118132
119133 static int hw_device_setup_for_encode(Encoder *e, AVCodecContext *enc_ctx,
@@ -173,7 +187,7 @@ int enc_open(void *opaque, const AVFrame *frame) · 53 unmodified lines
173187 InputStream *ist = ost->ist;
174188 Encoder *e = ost->enc;
175189 EncoderPriv *ep = ep_from_enc(e);
176− AVCodecContext *enc_ctx = ost->enc_ctx;
190+ AVCodecContext *enc_ctx = e->enc_ctx;
177191 Decoder *dec = NULL;
178192 const AVCodec *enc = enc_ctx->codec;
179193 OutputFile *of = ost->file;
@@ -372,7 +386,7 @@ static int do_subtitle_out(OutputFile *of, OutputStream *ost, const AVSubtitle * · 192 unmodified lines
372386 if ((of->start_time != AV_NOPTS_VALUE && sub->pts < of->start_time))
373387 return 0;
374388
375− enc = ost->enc_ctx;
389+ enc = e->enc_ctx;
376390
377391 /* Note: DVB subtitle need one packet to draw them and one other
378392 packet to clear them */
@@ -524,10 +538,11 @@ static inline double psnr(double d) · 145 unmodified lines
524538
525539 static int update_video_stats(OutputStream *ost, const AVPacket *pkt, int write_vstats)
526540 {
527− EncoderPriv *ep = ep_from_enc(ost->enc);
541+ Encoder *e = ost->enc;
542+ EncoderPriv *ep = ep_from_enc(e);
528543 const uint8_t *sd = av_packet_get_side_data(pkt, AV_PKT_DATA_QUALITY_STATS,
529544 NULL);
530− AVCodecContext *enc = ost->enc_ctx;
545+ AVCodecContext *enc = e->enc_ctx;
531546 enum AVPictureType pict_type;
532547 int64_t frame_number;
533548 double ti1, bitrate, avg_bitrate;
@@ -591,7 +606,7 @@ static int encode_frame(OutputFile *of, OutputStream *ost, AVFrame *frame, · 57 unmodified lines
591606 {
592607 Encoder *e = ost->enc;
593608 EncoderPriv *ep = ep_from_enc(e);
594− AVCodecContext *enc = ost->enc_ctx;
609+ AVCodecContext *enc = e->enc_ctx;
595610 const char *type_desc = av_get_media_type_string(enc->codec_type);
596611 const char *action = frame ? "encode" : "flush";
597612 int ret;
@@ -775,7 +790,7 @@ static int frame_encode(OutputStream *ost, AVFrame *frame, AVPacket *pkt) · 177 unmodified lines
775790 return AVERROR_EOF;
776791
777792 if (type == AVMEDIA_TYPE_VIDEO) {
778− frame->quality = ost->enc_ctx->global_quality;
793+ frame->quality = e->enc_ctx->global_quality;
779794 frame->pict_type = forced_kf_apply(e, &ost->kf, frame);
780795
781796 #if FFMPEG_OPT_TOP
@@ -785,8 +800,8 @@ static int frame_encode(OutputStream *ost, AVFrame *frame, AVPacket *pkt) · 3 unmodified lines
785800 }
786801 #endif
787802 } else {
788− if (!(ost->enc_ctx->codec->capabilities & AV_CODEC_CAP_PARAM_CHANGE) &&
789− ost->enc_ctx->ch_layout.nb_channels != frame->ch_layout.nb_channels) {
803+ if (!(e->enc_ctx->codec->capabilities & AV_CODEC_CAP_PARAM_CHANGE) &&
804+ e->enc_ctx->ch_layout.nb_channels != frame->ch_layout.nb_channels) {
790805 av_log(e, AV_LOG_ERROR,
791806 "Audio channel count changed and encoder does not support parameter changes\n");
792807 return 0;
@@ -801,7 +816,7 @@ static void enc_thread_set_name(const OutputStream *ost) · 8 unmodified lines
801816 {
802817 char name[16];
803818 snprintf(name, sizeof(name), "enc%d:%d:%s", ost->file->index, ost->index,
804− ost->enc_ctx->codec->name);
819+ ost->enc->enc_ctx->codec->name);
805820 ff_thread_setname(name);
806821 }
807822
fftools/ffmpeg_mux.c+2−6
@@ -661,8 +661,8 @@ static int check_written(OutputFile *of) · 660 unmodified lines
661661
662662 total_packets_written += packets_written;
663663
664− if (ost->enc_ctx &&
665− (ost->enc_ctx->flags & (AV_CODEC_FLAG_PASS1 | AV_CODEC_FLAG_PASS2))
664+ if (ost->enc &&
665+ (ost->enc->enc_ctx->flags & (AV_CODEC_FLAG_PASS1 | AV_CODEC_FLAG_PASS2))
666666 != AV_CODEC_FLAG_PASS1)
667667 pass1_used = 0;
668668
@@ -837,10 +837,6 @@ static void ost_free(OutputStream **post) · 168 unmodified lines
837837
838838 av_freep(&ost->attachment_filename);
839839
840− if (ost->enc_ctx)
841− av_freep(&ost->enc_ctx->stats_in);
842− avcodec_free_context(&ost->enc_ctx);
843−
844840 enc_stats_uninit(&ost->enc_stats_pre);
845841 enc_stats_uninit(&ost->enc_stats_post);
846842 enc_stats_uninit(&ms->stats);
fftools/ffmpeg_mux_init.c+31−35
@@ -555,7 +555,7 @@ static enum AVPixelFormat pix_fmt_parse(OutputStream *ost, const char *name) · 554 unmodified lines
555555 return AV_PIX_FMT_NONE;
556556 }
557557
558− ret = avcodec_get_supported_config(ost->enc_ctx, NULL, AV_CODEC_CONFIG_PIX_FORMAT,
558+ ret = avcodec_get_supported_config(ost->enc->enc_ctx, NULL, AV_CODEC_CONFIG_PIX_FORMAT,
559559 0, (const void **) &fmts, NULL);
560560 if (ret < 0)
561561 return AV_PIX_FMT_NONE;
@@ -587,7 +587,7 @@ static enum AVPixelFormat pix_fmt_parse(OutputStream *ost, const char *name) · 25 unmodified lines
587587 }
588588
589589 if (fmts && !fmt_in_list(fmts, fmt))
590− fmt = choose_pixel_fmt(ost->enc_ctx, fmt);
590+ fmt = choose_pixel_fmt(ost->enc->enc_ctx, fmt);
591591
592592 return fmt;
593593 }
@@ -632,8 +632,8 @@ static int new_stream_video(Muxer *mux, const OptionsContext *o, · 38 unmodified lines
632632 ost->frame_aspect_ratio = q;
633633 }
634634
635− if (ost->enc_ctx) {
636− AVCodecContext *video_enc = ost->enc_ctx;
635+ if (ost->enc) {
636+ AVCodecContext *video_enc = ost->enc->enc_ctx;
637637 const char *p = NULL, *fps_mode = NULL;
638638 const char *frame_size = NULL;
639639 const char *frame_pix_fmt = NULL;
@@ -746,10 +746,10 @@ static int new_stream_video(Muxer *mux, const OptionsContext *o, · 106 unmodified lines
746746 ost->logfile_prefix ? ost->logfile_prefix :
747747 DEFAULT_PASS_LOGFILENAME_PREFIX,
748748 ost_idx);
749− if (!strcmp(ost->enc_ctx->codec->name, "libx264") || !strcmp(ost->enc_ctx->codec->name, "libvvenc")) {
750− if (av_opt_is_set_to_default_by_name(ost->enc_ctx, "stats",
749+ if (!strcmp(video_enc->codec->name, "libx264") || !strcmp(video_enc->codec->name, "libvvenc")) {
750+ if (av_opt_is_set_to_default_by_name(video_enc, "stats",
751751 AV_OPT_SEARCH_CHILDREN) > 0)
752− av_opt_set(ost->enc_ctx, "stats", logfilename,
752+ av_opt_set(video_enc, "stats", logfilename,
753753 AV_OPT_SEARCH_CHILDREN);
754754 } else {
755755 if (video_enc->flags & AV_CODEC_FLAG_PASS2) {
@@ -842,8 +842,8 @@ static int new_stream_audio(Muxer *mux, const OptionsContext *o, · 86 unmodified lines
842842 AVFormatContext *oc = mux->fc;
843843 AVStream *st = ost->st;
844844
845− if (ost->enc_ctx) {
846− AVCodecContext *audio_enc = ost->enc_ctx;
845+ if (ost->enc) {
846+ AVCodecContext *audio_enc = ost->enc->enc_ctx;
847847 int channels = 0;
848848 const char *layout = NULL;
849849 const char *sample_fmt = NULL;
@@ -881,8 +881,8 @@ static int new_stream_subtitle(Muxer *mux, const OptionsContext *o, · 31 unmodified lines
881881
882882 st = ost->st;
883883
884− if (ost->enc_ctx) {
885− AVCodecContext *subtitle_enc = ost->enc_ctx;
884+ if (ost->enc) {
885+ AVCodecContext *subtitle_enc = ost->enc->enc_ctx;
886886
887887 AVCodecDescriptor const *input_descriptor =
888888 avcodec_descriptor_get(ost->ist->par->codec_id);
@@ -923,7 +923,7 @@ ost_bind_filter(const Muxer *mux, MuxStream *ms, OutputFilter *ofilter, · 34 unmodified lines
923923 const ViewSpecifier *vs)
924924 {
925925 OutputStream *ost = &ms->ost;
926− AVCodecContext *enc_ctx = ost->enc_ctx;
926+ AVCodecContext *enc_ctx = ost->enc->enc_ctx;
927927 char name[16];
928928 int ret;
929929
@@ -1245,10 +1245,6 @@ static int ost_add(Muxer *mux, const OptionsContext *o, enum AVMediaType type, · 315 unmodified lines
12451245 }
12461246
12471247 if (enc) {
1248− ost->enc_ctx = avcodec_alloc_context3(enc);
1249− if (!ost->enc_ctx)
1250− return AVERROR(ENOMEM);
1251−
12521248 ret = sch_add_enc(mux->sch, encoder_thread, ost,
12531249 ost->type == AVMEDIA_TYPE_SUBTITLE ? NULL : enc_open);
12541250 if (ret < 0)
@@ -1290,21 +1286,21 @@ static int ost_add(Muxer *mux, const OptionsContext *o, enum AVMediaType type, · 35 unmodified lines
12901286 if (!ms->pkt)
12911287 return AVERROR(ENOMEM);
12921288
1293− if (ost->enc_ctx) {
1289+ if (ost->enc) {
12941290 AVIOContext *s = NULL;
12951291 char *buf = NULL, *arg = NULL;
12961292 const char *enc_stats_pre = NULL, *enc_stats_post = NULL, *mux_stats = NULL;
12971293 const char *enc_time_base = NULL, *preset = NULL;
12981294
1299− ret = filter_codec_opts(o->g->codec_opts, ost->enc_ctx->codec_id,
1300− oc, st, ost->enc_ctx->codec, &encoder_opts,
1295+ ret = filter_codec_opts(o->g->codec_opts, enc->id,
1296+ oc, st, enc, &encoder_opts,
13011297 &mux->enc_opts_used);
13021298 if (ret < 0)
13031299 goto fail;
13041300
13051301 opt_match_per_stream_str(ost, &o->presets, oc, st, &preset);
13061302 opt_match_per_stream_int(ost, &o->autoscale, oc, st, &autoscale);
1307− if (preset && (!(ret = get_preset_file_2(preset, ost->enc_ctx->codec->name, &s)))) {
1303+ if (preset && (!(ret = get_preset_file_2(preset, enc->name, &s)))) {
13081304 AVBPrint bprint;
13091305 av_bprint_init(&bprint, 0, AV_BPRINT_SIZE_UNLIMITED);
13101306 do {
@@ -1404,7 +1400,7 @@ static int ost_add(Muxer *mux, const OptionsContext *o, enum AVMediaType type, · 93 unmodified lines
14041400
14051401 threads_manual = !!av_dict_get(encoder_opts, "threads", NULL, 0);
14061402
1407− ret = av_opt_set_dict2(ost->enc_ctx, &encoder_opts, AV_OPT_SEARCH_CHILDREN);
1403+ ret = av_opt_set_dict2(ost->enc->enc_ctx, &encoder_opts, AV_OPT_SEARCH_CHILDREN);
14081404 if (ret < 0) {
14091405 av_log(ost, AV_LOG_ERROR, "Error applying encoder options: %s\n",
14101406 av_err2str(ret));
@@ -1417,7 +1413,7 @@ static int ost_add(Muxer *mux, const OptionsContext *o, enum AVMediaType type, · 6 unmodified lines
14171413
14181414 // default to automatic thread count
14191415 if (!threads_manual)
1420− ost->enc_ctx->thread_count = 0;
1416+ ost->enc->enc_ctx->thread_count = 0;
14211417 } else {
14221418 ret = filter_codec_opts(o->g->codec_opts, AV_CODEC_ID_NONE, oc, st,
14231419 NULL, &encoder_opts,
@@ -1429,8 +1425,8 @@ static int ost_add(Muxer *mux, const OptionsContext *o, enum AVMediaType type, · 5 unmodified lines
14291425
14301426 if (o->bitexact) {
14311427 ost->bitexact = 1;
1432− } else if (ost->enc_ctx) {
1433− ost->bitexact = !!(ost->enc_ctx->flags & AV_CODEC_FLAG_BITEXACT);
1428+ } else if (ost->enc) {
1429+ ost->bitexact = !!(ost->enc->enc_ctx->flags & AV_CODEC_FLAG_BITEXACT);
14341430 }
14351431
14361432 if (enc) {
@@ -1482,14 +1478,14 @@ static int ost_add(Muxer *mux, const OptionsContext *o, enum AVMediaType type, · 45 unmodified lines
14821478 }
14831479 ost->st->codecpar->codec_tag = tag;
14841480 ms->par_in->codec_tag = tag;
1485− if (ost->enc_ctx)
1486− ost->enc_ctx->codec_tag = tag;
1481+ if (ost->enc)
1482+ ost->enc->enc_ctx->codec_tag = tag;
14871483 }
14881484
14891485 opt_match_per_stream_dbl(ost, &o->qscale, oc, st, &qscale);
1490− if (ost->enc_ctx && qscale >= 0) {
1491− ost->enc_ctx->flags |= AV_CODEC_FLAG_QSCALE;
1492− ost->enc_ctx->global_quality = FF_QP2LAMBDA * qscale;
1486+ if (ost->enc && qscale >= 0) {
1487+ ost->enc->enc_ctx->flags |= AV_CODEC_FLAG_QSCALE;
1488+ ost->enc->enc_ctx->global_quality = FF_QP2LAMBDA * qscale;
14931489 }
14941490
14951491 if (ms->sch_idx >= 0) {
@@ -1511,8 +1507,8 @@ static int ost_add(Muxer *mux, const OptionsContext *o, enum AVMediaType type, · 15 unmodified lines
15111507 opt_match_per_stream_int(ost, &o->fix_sub_duration_heartbeat,
15121508 oc, st, &ost->fix_sub_duration_heartbeat);
15131509
1514− if (oc->oformat->flags & AVFMT_GLOBALHEADER && ost->enc_ctx)
1515− ost->enc_ctx->flags |= AV_CODEC_FLAG_GLOBAL_HEADER;
1510+ if (oc->oformat->flags & AVFMT_GLOBALHEADER && ost->enc)
1511+ ost->enc->enc_ctx->flags |= AV_CODEC_FLAG_GLOBAL_HEADER;
15161512
15171513 opt_match_per_stream_int(ost, &o->copy_initial_nonkeyframes,
15181514 oc, st, &ms->copy_initial_nonkeyframes);
@@ -2047,7 +2043,7 @@ static int setup_sync_queues(Muxer *mux, AVFormatContext *oc, · 528 unmodified lines
20472043 int limit_frames = 0, limit_frames_av_enc = 0;
20482044
20492045 #define IS_AV_ENC(ost, type) \
2050− (ost->enc_ctx && (type == AVMEDIA_TYPE_VIDEO || type == AVMEDIA_TYPE_AUDIO))
2046+ (ost->enc && (type == AVMEDIA_TYPE_VIDEO || type == AVMEDIA_TYPE_AUDIO))
20512047 #define IS_INTERLEAVED(type) (type != AVMEDIA_TYPE_ATTACHMENT)
20522048
20532049 for (int i = 0; i < oc->nb_streams; i++) {
@@ -2059,8 +2055,8 @@ static int setup_sync_queues(Muxer *mux, AVFormatContext *oc, · 5 unmodified lines
20592055
20602056 nb_interleaved += IS_INTERLEAVED(type);
20612057 nb_av_enc += IS_AV_ENC(ost, type);
2062− nb_audio_fs += (ost->enc_ctx && type == AVMEDIA_TYPE_AUDIO &&
2063− !(ost->enc_ctx->codec->capabilities & AV_CODEC_CAP_VARIABLE_FRAME_SIZE));
2058+ nb_audio_fs += (ost->enc && type == AVMEDIA_TYPE_AUDIO &&
2059+ !(ost->enc->enc_ctx->codec->capabilities & AV_CODEC_CAP_VARIABLE_FRAME_SIZE));
20642060
20652061 limit_frames |= ms->max_frames < INT64_MAX;
20662062 limit_frames_av_enc |= (ms->max_frames < INT64_MAX) && IS_AV_ENC(ost, type);
@@ -3193,7 +3189,7 @@ static int process_forced_keyframes(Muxer *mux, const OptionsContext *o) · 1126 unmodified lines
31933189 mux->fc, ost->st, &forced_keyframes);
31943190
31953191 if (!(ost->type == AVMEDIA_TYPE_VIDEO &&
3196− ost->enc_ctx && forced_keyframes))
3192+ ost->enc && forced_keyframes))
31973193 continue;
31983194
31993195 if (!strncmp(forced_keyframes, "expr:", 5)) {

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t