[FFmpeg-cvslog] fftools/ffmpeg: store stream media type in OutputStream

Anton Khirnov git at videolan.org
Mon Apr 17 13:07:30 EEST 2023


ffmpeg | branch: master | Anton Khirnov <anton at khirnov.net> | Tue Apr 11 10:15:55 2023 +0200| [a064aed4c37836468f4156e6b3aaf150a626fa50] | committer: Anton Khirnov

fftools/ffmpeg: store stream media type in OutputStream

Reduces access to a deeply nested muxer property
OutputStream.st->codecpar->codec_type for this fundamental and immutable
stream property.

Besides making the code shorter, this will allow making the AVStream
(OutputStream.st) private to the muxer in the future.

> http://git.videolan.org/gitweb.cgi/ffmpeg.git/?a=commit;h=a064aed4c37836468f4156e6b3aaf150a626fa50
---

 fftools/ffmpeg.c          |  4 ++--
 fftools/ffmpeg.h          |  2 ++
 fftools/ffmpeg_mux.c      | 11 +++++------
 fftools/ffmpeg_mux_init.c | 26 ++++++++++++--------------
 4 files changed, 21 insertions(+), 22 deletions(-)

diff --git a/fftools/ffmpeg.c b/fftools/ffmpeg.c
index 14861fec4e..d143244e7c 100644
--- a/fftools/ffmpeg.c
+++ b/fftools/ffmpeg.c
@@ -748,12 +748,12 @@ static void print_report(int is_last_report, int64_t timer_start, int64_t cur_ti
         const AVCodecContext * const enc = ost->enc_ctx;
         const float q = enc ? ost->quality / (float) FF_QP2LAMBDA : -1;
 
-        if (vid && ost->st->codecpar->codec_type == AVMEDIA_TYPE_VIDEO) {
+        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);
         }
-        if (!vid && ost->st->codecpar->codec_type == AVMEDIA_TYPE_VIDEO) {
+        if (!vid && ost->type == AVMEDIA_TYPE_VIDEO) {
             float fps;
             uint64_t frame_number = atomic_load(&ost->packets_written);
 
diff --git a/fftools/ffmpeg.h b/fftools/ffmpeg.h
index 5833c13812..f08f5db49e 100644
--- a/fftools/ffmpeg.h
+++ b/fftools/ffmpeg.h
@@ -578,6 +578,8 @@ typedef struct Encoder Encoder;
 typedef struct OutputStream {
     const AVClass *class;
 
+    enum AVMediaType type;
+
     int file_index;          /* file index */
     int index;               /* stream index in the output file */
 
diff --git a/fftools/ffmpeg_mux.c b/fftools/ffmpeg_mux.c
index b316925115..eb64d8c3ff 100644
--- a/fftools/ffmpeg_mux.c
+++ b/fftools/ffmpeg_mux.c
@@ -62,7 +62,6 @@ static int write_packet(Muxer *mux, OutputStream *ost, AVPacket *pkt)
 {
     MuxStream *ms = ms_from_ost(ost);
     AVFormatContext *s = mux->fc;
-    AVStream *st = ost->st;
     int64_t fs;
     uint64_t frame_num;
     int ret;
@@ -74,10 +73,10 @@ static int write_packet(Muxer *mux, OutputStream *ost, AVPacket *pkt)
         goto fail;
     }
 
-    if (st->codecpar->codec_type == AVMEDIA_TYPE_VIDEO && ost->vsync_method == VSYNC_DROP)
+    if (ost->type == AVMEDIA_TYPE_VIDEO && ost->vsync_method == VSYNC_DROP)
         pkt->pts = pkt->dts = AV_NOPTS_VALUE;
 
-    if (st->codecpar->codec_type == AVMEDIA_TYPE_VIDEO) {
+    if (ost->type == AVMEDIA_TYPE_VIDEO) {
         if (ost->frame_rate.num && ost->is_cfr) {
             if (pkt->duration > 0)
                 av_log(ost, AV_LOG_WARNING, "Overriding packet duration by frame rate, this should not happen\n");
@@ -101,12 +100,12 @@ static int write_packet(Muxer *mux, OutputStream *ost, AVPacket *pkt)
                      - FFMIN3(pkt->pts, pkt->dts, ms->last_mux_dts + 1)
                      - FFMAX3(pkt->pts, pkt->dts, ms->last_mux_dts + 1);
         }
-        if ((st->codecpar->codec_type == AVMEDIA_TYPE_AUDIO || st->codecpar->codec_type == AVMEDIA_TYPE_VIDEO || st->codecpar->codec_type == AVMEDIA_TYPE_SUBTITLE) &&
+        if ((ost->type == AVMEDIA_TYPE_AUDIO || ost->type == AVMEDIA_TYPE_VIDEO || ost->type == AVMEDIA_TYPE_SUBTITLE) &&
             pkt->dts != AV_NOPTS_VALUE &&
             ms->last_mux_dts != AV_NOPTS_VALUE) {
             int64_t max = ms->last_mux_dts + !(s->oformat->flags & AVFMT_TS_NONSTRICT);
             if (pkt->dts < max) {
-                int loglevel = max - pkt->dts > 2 || st->codecpar->codec_type == AVMEDIA_TYPE_VIDEO ? AV_LOG_WARNING : AV_LOG_DEBUG;
+                int loglevel = max - pkt->dts > 2 || ost->type == AVMEDIA_TYPE_VIDEO ? AV_LOG_WARNING : AV_LOG_DEBUG;
                 if (exit_on_error)
                     loglevel = AV_LOG_ERROR;
                 av_log(s, loglevel, "Non-monotonous DTS in output stream "
@@ -136,7 +135,7 @@ static int write_packet(Muxer *mux, OutputStream *ost, AVPacket *pkt)
     if (debug_ts) {
         av_log(ost, AV_LOG_INFO, "muxer <- type:%s "
                 "pkt_pts:%s pkt_pts_time:%s pkt_dts:%s pkt_dts_time:%s duration:%s duration_time:%s size:%d\n",
-                av_get_media_type_string(st->codecpar->codec_type),
+                av_get_media_type_string(ost->type),
                 av_ts2str(pkt->pts), av_ts2timestr(pkt->pts, &ost->st->time_base),
                 av_ts2str(pkt->dts), av_ts2timestr(pkt->dts, &ost->st->time_base),
                 av_ts2str(pkt->duration), av_ts2timestr(pkt->duration, &ost->st->time_base),
diff --git a/fftools/ffmpeg_mux_init.c b/fftools/ffmpeg_mux_init.c
index 3d14b8268d..b53f5d278d 100644
--- a/fftools/ffmpeg_mux_init.c
+++ b/fftools/ffmpeg_mux_init.c
@@ -108,7 +108,7 @@ static int check_opt_bitexact(void *ctx, const AVDictionary *opts,
 static int choose_encoder(const OptionsContext *o, AVFormatContext *s,
                           OutputStream *ost, const AVCodec **enc)
 {
-    enum AVMediaType type = ost->st->codecpar->codec_type;
+    enum AVMediaType type = ost->type;
     char *codec_name = NULL;
 
     *enc = NULL;
@@ -117,7 +117,7 @@ static int choose_encoder(const OptionsContext *o, AVFormatContext *s,
         MATCH_PER_STREAM_OPT(codec_names, str, codec_name, s, ost->st);
         if (!codec_name) {
             ost->st->codecpar->codec_id = av_guess_codec(s->oformat, NULL, s->url,
-                                                         NULL, ost->st->codecpar->codec_type);
+                                                         NULL, ost->type);
             *enc = avcodec_find_encoder(ost->st->codecpar->codec_id);
             if (!*enc) {
                 av_log(ost, AV_LOG_FATAL, "Automatic encoder selection failed "
@@ -127,7 +127,7 @@ static int choose_encoder(const OptionsContext *o, AVFormatContext *s,
                 return AVERROR_ENCODER_NOT_FOUND;
             }
         } else if (strcmp(codec_name, "copy")) {
-            *enc = find_codec_or_die(ost, codec_name, ost->st->codecpar->codec_type, 1);
+            *enc = find_codec_or_die(ost, codec_name, ost->type, 1);
             ost->st->codecpar->codec_id = (*enc)->id;
         }
     }
@@ -410,6 +410,7 @@ static MuxStream *mux_stream_alloc(Muxer *mux, enum AVMediaType type)
 
     ms->ost.file_index = mux->of.index;
     ms->ost.index      = mux->of.nb_streams - 1;
+    ms->ost.type       = type;
 
     ms->ost.class = &output_stream_class;
 
@@ -422,8 +423,6 @@ static MuxStream *mux_stream_alloc(Muxer *mux, enum AVMediaType type)
 static char *get_ost_filters(const OptionsContext *o, AVFormatContext *oc,
                              OutputStream *ost)
 {
-    AVStream *st = ost->st;
-
     if (ost->filters_script && ost->filters) {
         av_log(ost, AV_LOG_ERROR, "Both -filter and -filter_script set\n");
         exit_program(1);
@@ -434,8 +433,7 @@ static char *get_ost_filters(const OptionsContext *o, AVFormatContext *oc,
     else if (ost->filters)
         return av_strdup(ost->filters);
 
-    return av_strdup(st->codecpar->codec_type == AVMEDIA_TYPE_VIDEO ?
-                     "null" : "anull");
+    return av_strdup(ost->type == AVMEDIA_TYPE_VIDEO ? "null" : "anull");
 }
 
 static void check_streamcopy_filters(const OptionsContext *o, AVFormatContext *oc,
@@ -1597,7 +1595,7 @@ static int setup_sync_queues(Muxer *mux, AVFormatContext *oc, int64_t buf_size_u
     for (int i = 0; i < oc->nb_streams; i++) {
         OutputStream *ost = of->streams[i];
         MuxStream     *ms = ms_from_ost(ost);
-        enum AVMediaType type = ost->st->codecpar->codec_type;
+        enum AVMediaType type = ost->type;
 
         ost->sq_idx_encode = -1;
         ost->sq_idx_mux    = -1;
@@ -1631,7 +1629,7 @@ static int setup_sync_queues(Muxer *mux, AVFormatContext *oc, int64_t buf_size_u
         for (int i = 0; i < oc->nb_streams; i++) {
             OutputStream *ost = of->streams[i];
             MuxStream     *ms = ms_from_ost(ost);
-            enum AVMediaType type = ost->st->codecpar->codec_type;
+            enum AVMediaType type = ost->type;
 
             if (!IS_AV_ENC(ost, type))
                 continue;
@@ -1660,7 +1658,7 @@ static int setup_sync_queues(Muxer *mux, AVFormatContext *oc, int64_t buf_size_u
         for (int i = 0; i < oc->nb_streams; i++) {
             OutputStream *ost = of->streams[i];
             MuxStream     *ms = ms_from_ost(ost);
-            enum AVMediaType type = ost->st->codecpar->codec_type;
+            enum AVMediaType type = ost->type;
 
             if (!IS_INTERLEAVED(type))
                 continue;
@@ -2116,7 +2114,7 @@ static int set_dispositions(Muxer *mux, const OptionsContext *o)
     for (int i = 0; i < ctx->nb_streams; i++) {
         OutputStream *ost = of->streams[i];
 
-        nb_streams[ost->st->codecpar->codec_type]++;
+        nb_streams[ost->type]++;
 
         MATCH_PER_STREAM_OPT(disposition, str, dispositions[i], ctx, ost->st);
 
@@ -2126,7 +2124,7 @@ static int set_dispositions(Muxer *mux, const OptionsContext *o)
             ost->st->disposition = ost->ist->st->disposition;
 
             if (ost->st->disposition & AV_DISPOSITION_DEFAULT)
-                have_default[ost->st->codecpar->codec_type] = 1;
+                have_default[ost->type] = 1;
         }
     }
 
@@ -2149,7 +2147,7 @@ static int set_dispositions(Muxer *mux, const OptionsContext *o)
         // "Suitable" means the first of that type, skipping attached pictures.
         for (int i = 0; i < ctx->nb_streams; i++) {
             OutputStream *ost = of->streams[i];
-            enum AVMediaType type = ost->st->codecpar->codec_type;
+            enum AVMediaType type = ost->type;
 
             if (nb_streams[type] < 2 || have_default[type] ||
                 ost->st->disposition & AV_DISPOSITION_ATTACHED_PIC)
@@ -2242,7 +2240,7 @@ static int process_forced_keyframes(Muxer *mux, const OptionsContext *o)
 
         MATCH_PER_STREAM_OPT(forced_key_frames, str, forced_keyframes, mux->fc, ost->st);
 
-        if (!(ost->st->codecpar->codec_type == AVMEDIA_TYPE_VIDEO &&
+        if (!(ost->type == AVMEDIA_TYPE_VIDEO &&
               ost->enc_ctx && forced_keyframes))
             continue;
 



More information about the ffmpeg-cvslog mailing list