[FFmpeg-devel] [PATCH 03/12] fftools/ffmpeg: replace OutputStream.file_index by a pointer

Anton Khirnov anton at khirnov.net
Wed Dec 13 21:29:58 EET 2023


Reduces the need to use the output_files global array.
---
 fftools/ffmpeg.c          | 12 ++++++------
 fftools/ffmpeg.h          |  4 +++-
 fftools/ffmpeg_enc.c      | 21 +++++++++++----------
 fftools/ffmpeg_filter.c   | 22 +++++++++++-----------
 fftools/ffmpeg_mux.c      | 11 +++++------
 fftools/ffmpeg_mux_init.c | 12 ++++++------
 6 files changed, 42 insertions(+), 40 deletions(-)

diff --git a/fftools/ffmpeg.c b/fftools/ffmpeg.c
index 3aab302c27..8077248254 100644
--- a/fftools/ffmpeg.c
+++ b/fftools/ffmpeg.c
@@ -374,7 +374,7 @@ static void ffmpeg_cleanup(int ret)
 
 OutputStream *ost_iter(OutputStream *prev)
 {
-    int of_idx  = prev ? prev->file_index : 0;
+    int of_idx  = prev ? prev->file->index : 0;
     int ost_idx = prev ? prev->index + 1  : 0;
 
     for (; of_idx < nb_output_files; of_idx++) {
@@ -515,7 +515,7 @@ static void print_report(int is_last_report, int64_t timer_start, int64_t cur_ti
         if (vid && ost->type == AVMEDIA_TYPE_VIDEO) {
             av_bprintf(&buf, "q=%2.1f ", q);
             av_bprintf(&buf_script, "stream_%d_%d_q=%.1f\n",
-                       ost->file_index, ost->index, q);
+                       ost->file->index, ost->index, q);
         }
         if (!vid && ost->type == AVMEDIA_TYPE_VIDEO && ost->filter) {
             float fps;
@@ -527,7 +527,7 @@ static void print_report(int is_last_report, int64_t timer_start, int64_t cur_ti
             av_bprintf(&buf_script, "frame=%"PRId64"\n", frame_number);
             av_bprintf(&buf_script, "fps=%.2f\n", fps);
             av_bprintf(&buf_script, "stream_%d_%d_q=%.1f\n",
-                       ost->file_index, ost->index, q);
+                       ost->file->index, ost->index, q);
             if (is_last_report)
                 av_bprintf(&buf, "L");
 
@@ -765,7 +765,7 @@ static void print_stream_maps(void)
         if (ost->attachment_filename) {
             /* an attached file */
             av_log(NULL, AV_LOG_INFO, "  File %s -> Stream #%d:%d\n",
-                   ost->attachment_filename, ost->file_index, ost->index);
+                   ost->attachment_filename, ost->file->index, ost->index);
             continue;
         }
 
@@ -775,7 +775,7 @@ static void print_stream_maps(void)
             if (nb_filtergraphs > 1)
                 av_log(NULL, AV_LOG_INFO, " (graph %d)", ost->filter->graph->index);
 
-            av_log(NULL, AV_LOG_INFO, " -> Stream #%d:%d (%s)\n", ost->file_index,
+            av_log(NULL, AV_LOG_INFO, " -> Stream #%d:%d (%s)\n", ost->file->index,
                    ost->index, ost->enc_ctx->codec->name);
             continue;
         }
@@ -783,7 +783,7 @@ static void print_stream_maps(void)
         av_log(NULL, AV_LOG_INFO, "  Stream #%d:%d -> #%d:%d",
                ost->ist->file->index,
                ost->ist->index,
-               ost->file_index,
+               ost->file->index,
                ost->index);
         if (ost->enc_ctx) {
             const AVCodec *in_codec    = ost->ist->dec;
diff --git a/fftools/ffmpeg.h b/fftools/ffmpeg.h
index d7b6e689fc..63e72e4fbc 100644
--- a/fftools/ffmpeg.h
+++ b/fftools/ffmpeg.h
@@ -514,7 +514,9 @@ typedef struct OutputStream {
 
     enum AVMediaType type;
 
-    int file_index;          /* file index */
+    /* parent muxer */
+    struct OutputFile *file;
+
     int index;               /* stream index in the output file */
 
     /**
diff --git a/fftools/ffmpeg_enc.c b/fftools/ffmpeg_enc.c
index 320df2dee7..9b013af2fe 100644
--- a/fftools/ffmpeg_enc.c
+++ b/fftools/ffmpeg_enc.c
@@ -172,7 +172,7 @@ int enc_open(void *opaque, const AVFrame *frame)
     AVCodecContext *enc_ctx = ost->enc_ctx;
     AVCodecContext *dec_ctx = NULL;
     const AVCodec      *enc = enc_ctx->codec;
-    OutputFile      *of = output_files[ost->file_index];
+    OutputFile          *of = ost->file;
     FrameData *fd;
     int frame_samples = 0;
     int ret;
@@ -188,7 +188,7 @@ int enc_open(void *opaque, const AVFrame *frame)
         fd = (FrameData*)frame->opaque_ref->data;
     }
 
-    ret = set_encoder_id(output_files[ost->file_index], ost);
+    ret = set_encoder_id(of, ost);
     if (ret < 0)
         return ret;
 
@@ -374,7 +374,7 @@ int enc_open(void *opaque, const AVFrame *frame)
 
 static int check_recording_time(OutputStream *ost, int64_t ts, AVRational tb)
 {
-    OutputFile *of = output_files[ost->file_index];
+    OutputFile *of = ost->file;
 
     if (of->recording_time != INT64_MAX &&
         av_compare_ts(ts, tb, of->recording_time, AV_TIME_BASE_Q) >= 0) {
@@ -413,8 +413,8 @@ static int do_subtitle_out(OutputFile *of, OutputStream *ost, const AVSubtitle *
 
     /* shift timestamp to honor -ss and make check_recording_time() work with -t */
     pts = sub->pts;
-    if (output_files[ost->file_index]->start_time != AV_NOPTS_VALUE)
-        pts -= output_files[ost->file_index]->start_time;
+    if (of->start_time != AV_NOPTS_VALUE)
+        pts -= of->start_time;
     for (i = 0; i < nb; i++) {
         AVSubtitle local_sub = *sub;
 
@@ -495,7 +495,7 @@ void enc_stats_write(OutputStream *ost, EncStats *es,
 
         switch (c->type) {
         case ENC_STATS_LITERAL:         avio_write (io, c->str,     c->str_len);                    continue;
-        case ENC_STATS_FILE_IDX:        avio_printf(io, "%d",       ost->file_index);               continue;
+        case ENC_STATS_FILE_IDX:        avio_printf(io, "%d",       ost->file->index);              continue;
         case ENC_STATS_STREAM_IDX:      avio_printf(io, "%d",       ost->index);                    continue;
         case ENC_STATS_TIMEBASE:        avio_printf(io, "%d/%d",    tb.num, tb.den);                continue;
         case ENC_STATS_TIMEBASE_IN:     avio_printf(io, "%d/%d",    tbi.num, tbi.den);              continue;
@@ -583,7 +583,8 @@ static int update_video_stats(OutputStream *ost, const AVPacket *pkt, int write_
         fprintf(vstats_file, "frame= %5"PRId64" q= %2.1f ", frame_number,
                 quality / (float)FF_QP2LAMBDA);
     } else  {
-        fprintf(vstats_file, "out= %2d st= %2d frame= %5"PRId64" q= %2.1f ", ost->file_index, ost->index, frame_number,
+        fprintf(vstats_file, "out= %2d st= %2d frame= %5"PRId64" q= %2.1f ",
+                ost->file->index, ost->index, frame_number,
                 quality / (float)FF_QP2LAMBDA);
     }
 
@@ -648,7 +649,7 @@ static int encode_frame(OutputFile *of, OutputStream *ost, AVFrame *frame,
 
         ret = avcodec_receive_packet(enc, pkt);
         update_benchmark("%s_%s %d.%d", action, type_desc,
-                         ost->file_index, ost->index);
+                         of->index, ost->index);
 
         pkt->time_base = enc->time_base;
 
@@ -787,7 +788,7 @@ static int do_video_out(OutputFile *of, OutputStream *ost,
 
 static int frame_encode(OutputStream *ost, AVFrame *frame, AVPacket *pkt)
 {
-    OutputFile *of = output_files[ost->file_index];
+    OutputFile *of = ost->file;
     enum AVMediaType type = ost->type;
 
     if (type == AVMEDIA_TYPE_SUBTITLE) {
@@ -810,7 +811,7 @@ static int frame_encode(OutputStream *ost, AVFrame *frame, AVPacket *pkt)
 static void enc_thread_set_name(const OutputStream *ost)
 {
     char name[16];
-    snprintf(name, sizeof(name), "enc%d:%d:%s", ost->file_index, ost->index,
+    snprintf(name, sizeof(name), "enc%d:%d:%s", ost->file->index, ost->index,
              ost->enc_ctx->codec->name);
     ff_thread_setname(name);
 }
diff --git a/fftools/ffmpeg_filter.c b/fftools/ffmpeg_filter.c
index 40608d9e44..4ecaa06be9 100644
--- a/fftools/ffmpeg_filter.c
+++ b/fftools/ffmpeg_filter.c
@@ -720,7 +720,7 @@ static int set_channel_layout(OutputFilterPriv *f, OutputStream *ost)
 int ofilter_bind_ost(OutputFilter *ofilter, OutputStream *ost,
                      unsigned sched_idx_enc)
 {
-    const OutputFile  *of = output_files[ost->file_index];
+    const OutputFile  *of = ost->file;
     OutputFilterPriv *ofp = ofp_from_ofilter(ofilter);
     FilterGraph  *fg = ofilter->graph;
     FilterGraphPriv *fgp = fgp_from_fg(fg);
@@ -1040,7 +1040,7 @@ int init_simple_filtergraph(InputStream *ist, OutputStream *ost,
 
     snprintf(fgp->log_name, sizeof(fgp->log_name), "%cf#%d:%d",
              av_get_media_type_string(ost->type)[0],
-             ost->file_index, ost->index);
+             ost->file->index, ost->index);
 
     if (fg->nb_inputs != 1 || fg->nb_outputs != 1) {
         av_log(fg, AV_LOG_ERROR, "Simple filtergraph '%s' was expected "
@@ -1219,7 +1219,7 @@ static int configure_output_video_filter(FilterGraph *fg, AVFilterGraph *graph,
 {
     OutputFilterPriv *ofp = ofp_from_ofilter(ofilter);
     OutputStream *ost = ofilter->ost;
-    OutputFile    *of = output_files[ost->file_index];
+    OutputFile    *of = ost->file;
     AVFilterContext *last_filter = out->filter_ctx;
     AVBPrint bprint;
     int pad_idx = out->pad_idx;
@@ -1227,7 +1227,7 @@ static int configure_output_video_filter(FilterGraph *fg, AVFilterGraph *graph,
     const char *pix_fmts;
     char name[255];
 
-    snprintf(name, sizeof(name), "out_%d_%d", ost->file_index, ost->index);
+    snprintf(name, sizeof(name), "out_%d_%d", ost->file->index, ost->index);
     ret = avfilter_graph_create_filter(&ofp->filter,
                                        avfilter_get_by_name("buffersink"),
                                        name, NULL, NULL, graph);
@@ -1248,7 +1248,7 @@ static int configure_output_video_filter(FilterGraph *fg, AVFilterGraph *graph,
         }
 
         snprintf(name, sizeof(name), "scaler_out_%d_%d",
-                 ost->file_index, ost->index);
+                 ost->file->index, ost->index);
         if ((ret = avfilter_graph_create_filter(&filter, avfilter_get_by_name("scale"),
                                                 name, args, NULL, graph)) < 0)
             return ret;
@@ -1281,7 +1281,7 @@ static int configure_output_video_filter(FilterGraph *fg, AVFilterGraph *graph,
     }
 
     snprintf(name, sizeof(name), "trim_out_%d_%d",
-             ost->file_index, ost->index);
+             ost->file->index, ost->index);
     ret = insert_trim(of->start_time, of->recording_time,
                       &last_filter, &pad_idx, name);
     if (ret < 0)
@@ -1299,14 +1299,14 @@ static int configure_output_audio_filter(FilterGraph *fg, AVFilterGraph *graph,
 {
     OutputFilterPriv *ofp = ofp_from_ofilter(ofilter);
     OutputStream *ost = ofilter->ost;
-    OutputFile    *of = output_files[ost->file_index];
+    OutputFile    *of = ost->file;
     AVFilterContext *last_filter = out->filter_ctx;
     int pad_idx = out->pad_idx;
     AVBPrint args;
     char name[255];
     int ret;
 
-    snprintf(name, sizeof(name), "out_%d_%d", ost->file_index, ost->index);
+    snprintf(name, sizeof(name), "out_%d_%d", ost->file->index, ost->index);
     ret = avfilter_graph_create_filter(&ofp->filter,
                                        avfilter_get_by_name("abuffersink"),
                                        name, NULL, NULL, graph);
@@ -1361,7 +1361,7 @@ static int configure_output_audio_filter(FilterGraph *fg, AVFilterGraph *graph,
         AVFilterContext *format;
 
         snprintf(name, sizeof(name), "format_out_%d_%d",
-                 ost->file_index, ost->index);
+                 ost->file->index, ost->index);
         ret = avfilter_graph_create_filter(&format,
                                            avfilter_get_by_name("aformat"),
                                            name, args.str, NULL, graph);
@@ -1389,7 +1389,7 @@ static int configure_output_audio_filter(FilterGraph *fg, AVFilterGraph *graph,
     }
 
     snprintf(name, sizeof(name), "trim for output stream %d:%d",
-             ost->file_index, ost->index);
+             ost->file->index, ost->index);
     ret = insert_trim(of->start_time, of->recording_time,
                       &last_filter, &pad_idx, name);
     if (ret < 0)
@@ -2675,7 +2675,7 @@ static void fg_thread_set_name(const FilterGraph *fg)
         OutputStream *ost = fg->outputs[0]->ost;
         snprintf(name, sizeof(name), "%cf#%d:%d",
                  av_get_media_type_string(ost->type)[0],
-                 ost->file_index, ost->index);
+                 ost->file->index, ost->index);
     } else {
         snprintf(name, sizeof(name), "fc%d", fg->index);
     }
diff --git a/fftools/ffmpeg_mux.c b/fftools/ffmpeg_mux.c
index 067dc65d4e..fdedc550e1 100644
--- a/fftools/ffmpeg_mux.c
+++ b/fftools/ffmpeg_mux.c
@@ -102,7 +102,7 @@ static int write_packet(Muxer *mux, OutputStream *ost, AVPacket *pkt)
             pkt->dts > pkt->pts) {
             av_log(s, AV_LOG_WARNING, "Invalid DTS: %"PRId64" PTS: %"PRId64" in output stream %d:%d, replacing by guess\n",
                    pkt->dts, pkt->pts,
-                   ost->file_index, ost->st->index);
+                   mux->of.index, ost->st->index);
             pkt->pts =
             pkt->dts = pkt->pts + pkt->dts + ms->last_mux_dts + 1
                      - FFMIN3(pkt->pts, pkt->dts, ms->last_mux_dts + 1)
@@ -118,7 +118,7 @@ static int write_packet(Muxer *mux, OutputStream *ost, AVPacket *pkt)
                     loglevel = AV_LOG_ERROR;
                 av_log(s, loglevel, "Non-monotonic DTS in output stream "
                        "%d:%d; previous: %"PRId64", current: %"PRId64"; ",
-                       ost->file_index, ost->st->index, ms->last_mux_dts, pkt->dts);
+                       mux->of.index, ost->st->index, ms->last_mux_dts, pkt->dts);
                 if (exit_on_error) {
                     ret = AVERROR(EINVAL);
                     goto fail;
@@ -203,7 +203,7 @@ static int sync_queue_process(Muxer *mux, OutputStream *ost, AVPacket *pkt, int
     return 0;
 }
 
-static int of_streamcopy(OutputStream *ost, AVPacket *pkt);
+static int of_streamcopy(OutputFile *of, OutputStream *ost, AVPacket *pkt);
 
 /* apply the output bitstream filters */
 static int mux_packet_filter(Muxer *mux, MuxThreadContext *mt,
@@ -214,7 +214,7 @@ static int mux_packet_filter(Muxer *mux, MuxThreadContext *mt,
     int ret = 0;
 
     if (pkt && !ost->enc) {
-        ret = of_streamcopy(ost, pkt);
+        ret = of_streamcopy(&mux->of, ost, pkt);
         if (ret == AVERROR(EAGAIN))
             return 0;
         else if (ret == AVERROR_EOF) {
@@ -377,9 +377,8 @@ finish:
     return (void*)(intptr_t)ret;
 }
 
-static int of_streamcopy(OutputStream *ost, AVPacket *pkt)
+static int of_streamcopy(OutputFile *of, OutputStream *ost, AVPacket *pkt)
 {
-    OutputFile *of = output_files[ost->file_index];
     MuxStream  *ms = ms_from_ost(ost);
     DemuxPktData *pd = pkt->opaque_ref ? (DemuxPktData*)pkt->opaque_ref->data : NULL;
     int64_t      dts = pd ? pd->dts_est : AV_NOPTS_VALUE;
diff --git a/fftools/ffmpeg_mux_init.c b/fftools/ffmpeg_mux_init.c
index 64c173e006..455876d456 100644
--- a/fftools/ffmpeg_mux_init.c
+++ b/fftools/ffmpeg_mux_init.c
@@ -431,7 +431,7 @@ static MuxStream *mux_stream_alloc(Muxer *mux, enum AVMediaType type)
     if (!ms)
         return NULL;
 
-    ms->ost.file_index = mux->of.index;
+    ms->ost.file       = &mux->of;
     ms->ost.index      = mux->of.nb_streams - 1;
     ms->ost.type       = type;
 
@@ -748,7 +748,7 @@ static int new_stream_video(Muxer *mux, const OptionsContext *o,
             FILE *f;
 
             /* compute this stream's global index */
-            for (int i = 0; i <= ost->file_index; i++)
+            for (int i = 0; i <= ost->file->index; i++)
                 ost_idx += output_files[i]->nb_streams;
 
             snprintf(logfilename, sizeof(logfilename), "%s-%d.log",
@@ -793,7 +793,7 @@ static int new_stream_video(Muxer *mux, const OptionsContext *o,
         ost->vsync_method = video_sync_method;
         MATCH_PER_STREAM_OPT(fps_mode, str, fps_mode, oc, st);
         if (fps_mode) {
-            ret = parse_and_set_vsync(fps_mode, &ost->vsync_method, ost->file_index, ost->index, 0);
+            ret = parse_and_set_vsync(fps_mode, &ost->vsync_method, ost->file->index, ost->index, 0);
             if (ret < 0)
                 return ret;
         }
@@ -893,7 +893,7 @@ static int new_stream_audio(Muxer *mux, const OptionsContext *o,
         /* check for channel mapping for this audio stream */
         for (int n = 0; n < o->nb_audio_channel_maps; n++) {
             AudioChannelMap *map = &o->audio_channel_maps[n];
-            if ((map->ofile_idx   == -1 || ost->file_index == map->ofile_idx) &&
+            if ((map->ofile_idx   == -1 || ost->file->index == map->ofile_idx) &&
                 (map->ostream_idx == -1 || ost->st->index  == map->ostream_idx)) {
                 InputStream *ist;
 
@@ -901,7 +901,7 @@ static int new_stream_audio(Muxer *mux, const OptionsContext *o,
                     ist = NULL;
                 } else if (!ost->ist) {
                     av_log(ost, AV_LOG_FATAL, "Cannot determine input stream for channel mapping %d.%d\n",
-                           ost->file_index, ost->st->index);
+                           ost->file->index, ost->st->index);
                     continue;
                 } else {
                     ist = ost->ist;
@@ -1481,7 +1481,7 @@ static int ost_add(Muxer *mux, const OptionsContext *o, enum AVMediaType type,
                 return ret;
         } else {
             ret = sch_connect(mux->sch, SCH_DSTREAM(ost->ist->file->index, sched_idx),
-                                        SCH_MSTREAM(ost->file_index, ms->sch_idx));
+                                        SCH_MSTREAM(ost->file->index, ms->sch_idx));
             if (ret < 0)
                 return ret;
         }
-- 
2.42.0



More information about the ffmpeg-devel mailing list