[FFmpeg-cvslog] avformat: implement retiming directly in mxfenc and gxfenc

Marton Balint git at videolan.org
Fri May 8 00:26:40 EEST 2020


ffmpeg | branch: master | Marton Balint <cus at passwd.hu> | Tue Apr 28 00:39:40 2020 +0200| [8360fd26105f58fbec9fb8edeaa5755160c9994f] | committer: Marton Balint

avformat: implement retiming directly in mxfenc and gxfenc

Generic retime functionality is replaced by a few lines of code directly in the
muxers which used it, which seems a lot easier to understand and this way the
retiming is not dependant of the input durations.

Also remove retimeinterleave, since it is not used by anything anymore.

Signed-off-by: Marton Balint <cus at passwd.hu>

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

 libavformat/Makefile           |  4 ++--
 libavformat/gxfenc.c           | 21 +++++++++++------
 libavformat/mxfenc.c           | 14 +++++++-----
 libavformat/retimeinterleave.c | 51 ------------------------------------------
 libavformat/retimeinterleave.h | 51 ------------------------------------------
 5 files changed, 25 insertions(+), 116 deletions(-)

diff --git a/libavformat/Makefile b/libavformat/Makefile
index ec01c6c65c..5fa24cef16 100644
--- a/libavformat/Makefile
+++ b/libavformat/Makefile
@@ -205,7 +205,7 @@ OBJS-$(CONFIG_GIF_DEMUXER)               += gifdec.o
 OBJS-$(CONFIG_GSM_DEMUXER)               += gsmdec.o
 OBJS-$(CONFIG_GSM_MUXER)                 += rawenc.o
 OBJS-$(CONFIG_GXF_DEMUXER)               += gxf.o
-OBJS-$(CONFIG_GXF_MUXER)                 += gxfenc.o retimeinterleave.o
+OBJS-$(CONFIG_GXF_MUXER)                 += gxfenc.o
 OBJS-$(CONFIG_G722_DEMUXER)              += g722.o rawdec.o
 OBJS-$(CONFIG_G722_MUXER)                += rawenc.o
 OBJS-$(CONFIG_G723_1_DEMUXER)            += g723_1.o
@@ -347,7 +347,7 @@ OBJS-$(CONFIG_MUSX_DEMUXER)              += musx.o
 OBJS-$(CONFIG_MV_DEMUXER)                += mvdec.o
 OBJS-$(CONFIG_MVI_DEMUXER)               += mvi.o
 OBJS-$(CONFIG_MXF_DEMUXER)               += mxfdec.o mxf.o
-OBJS-$(CONFIG_MXF_MUXER)                 += mxfenc.o mxf.o retimeinterleave.o avc.o
+OBJS-$(CONFIG_MXF_MUXER)                 += mxfenc.o mxf.o avc.o
 OBJS-$(CONFIG_MXG_DEMUXER)               += mxg.o
 OBJS-$(CONFIG_NC_DEMUXER)                += ncdec.o
 OBJS-$(CONFIG_NISTSPHERE_DEMUXER)        += nistspheredec.o pcm.o
diff --git a/libavformat/gxfenc.c b/libavformat/gxfenc.c
index 60468c36ce..6d4df894f6 100644
--- a/libavformat/gxfenc.c
+++ b/libavformat/gxfenc.c
@@ -27,7 +27,6 @@
 #include "avformat.h"
 #include "internal.h"
 #include "gxf.h"
-#include "retimeinterleave.h"
 
 #define GXF_SAMPLES_PER_FRAME 32768
 #define GXF_AUDIO_PACKET_SIZE 65536
@@ -45,7 +44,7 @@ typedef struct GXFTimecode{
 } GXFTimecode;
 
 typedef struct GXFStreamContext {
-    RetimeInterleaveContext aic;
+    int64_t pkt_cnt;
     uint32_t track_type;
     uint32_t sample_size;
     uint32_t sample_rate;
@@ -815,7 +814,6 @@ static int gxf_write_header(AVFormatContext *s)
                 return -1;
             }
         }
-        ff_retime_interleave_init(&sc->aic, st->time_base);
         /* FIXME first 10 audio tracks are 0 to 9 next 22 are A to V */
         sc->media_info = media_info<<8 | ('0'+tracks[media_info]++);
         sc->order = s->nb_streams - st->index;
@@ -1012,10 +1010,19 @@ static int gxf_compare_field_nb(AVFormatContext *s, const AVPacket *next,
 
 static int gxf_interleave_packet(AVFormatContext *s, AVPacket *out, AVPacket *pkt, int flush)
 {
-    if (pkt && s->streams[pkt->stream_index]->codecpar->codec_type == AVMEDIA_TYPE_VIDEO)
-        pkt->duration = 2; // enforce 2 fields
-    return ff_retime_interleave(s, out, pkt, flush,
-                                ff_interleave_packet_per_dts, gxf_compare_field_nb);
+    int ret;
+    if (pkt) {
+        AVStream *st = s->streams[pkt->stream_index];
+        GXFStreamContext *sc = st->priv_data;
+        if (st->codecpar->codec_type == AVMEDIA_TYPE_VIDEO)
+            pkt->pts = pkt->dts = sc->pkt_cnt * 2; // enforce 2 fields
+        else
+            pkt->pts = pkt->dts = sc->pkt_cnt * GXF_SAMPLES_PER_FRAME;
+        sc->pkt_cnt++;
+        if ((ret = ff_interleave_add_packet(s, pkt, gxf_compare_field_nb)) < 0)
+            return ret;
+    }
+    return ff_interleave_packet_per_dts(s, out, NULL, flush);
 }
 
 AVOutputFormat ff_gxf_muxer = {
diff --git a/libavformat/mxfenc.c b/libavformat/mxfenc.c
index 63a2799b08..c3b6809e98 100644
--- a/libavformat/mxfenc.c
+++ b/libavformat/mxfenc.c
@@ -52,7 +52,6 @@
 #include "libavcodec/h264_ps.h"
 #include "libavcodec/golomb.h"
 #include "libavcodec/internal.h"
-#include "retimeinterleave.h"
 #include "avformat.h"
 #include "avio_internal.h"
 #include "internal.h"
@@ -79,7 +78,7 @@ typedef struct MXFIndexEntry {
 } MXFIndexEntry;
 
 typedef struct MXFStreamContext {
-    RetimeInterleaveContext aic;
+    int64_t pkt_cnt;         ///< pkt counter for muxed packets
     UID track_essence_element_key;
     int index;               ///< index in mxf_essence_container_uls table
     const UID *codec_ul;
@@ -2598,7 +2597,6 @@ static int mxf_write_header(AVFormatContext *s)
                 return -1;
             }
         }
-        ff_retime_interleave_init(&sc->aic, av_inv_q(mxf->tc.rate));
 
         if (sc->index == -1) {
             sc->index = mxf_get_essence_container_ul_index(st->codecpar->codec_id);
@@ -3087,8 +3085,14 @@ static int mxf_compare_timestamps(AVFormatContext *s, const AVPacket *next,
 
 static int mxf_interleave(AVFormatContext *s, AVPacket *out, AVPacket *pkt, int flush)
 {
-    return ff_retime_interleave(s, out, pkt, flush,
-                                mxf_interleave_get_packet, mxf_compare_timestamps);
+    int ret;
+    if (pkt) {
+        MXFStreamContext *sc = s->streams[pkt->stream_index]->priv_data;
+        pkt->pts = pkt->dts = sc->pkt_cnt++;
+        if ((ret = ff_interleave_add_packet(s, pkt, mxf_compare_timestamps)) < 0)
+            return ret;
+    }
+    return mxf_interleave_get_packet(s, out, NULL, flush);
 }
 
 #define MXF_COMMON_OPTIONS \
diff --git a/libavformat/retimeinterleave.c b/libavformat/retimeinterleave.c
deleted file mode 100644
index 9f874e3626..0000000000
--- a/libavformat/retimeinterleave.c
+++ /dev/null
@@ -1,51 +0,0 @@
-/*
- * Retime Interleaving functions
- *
- * Copyright (c) 2009 Baptiste Coudurier <baptiste dot coudurier at gmail dot com>
- *
- * This file is part of FFmpeg.
- *
- * FFmpeg is free software; you can redistribute it and/or
- * modify it under the terms of the GNU Lesser General Public
- * License as published by the Free Software Foundation; either
- * version 2.1 of the License, or (at your option) any later version.
- *
- * FFmpeg is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
- * Lesser General Public License for more details.
- *
- * You should have received a copy of the GNU Lesser General Public
- * License along with FFmpeg; if not, write to the Free Software
- * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
- */
-
-#include "libavutil/mathematics.h"
-#include "avformat.h"
-#include "retimeinterleave.h"
-#include "internal.h"
-
-void ff_retime_interleave_init(RetimeInterleaveContext *aic, AVRational time_base)
-{
-    aic->time_base = time_base;
-}
-
-int ff_retime_interleave(AVFormatContext *s, AVPacket *out, AVPacket *pkt, int flush,
-                        int (*get_packet)(AVFormatContext *, AVPacket *, AVPacket *, int),
-                        int (*compare_ts)(AVFormatContext *, const AVPacket *, const AVPacket *))
-{
-    int ret;
-
-    if (pkt) {
-        AVStream *st = s->streams[pkt->stream_index];
-        RetimeInterleaveContext *aic = st->priv_data;
-        pkt->duration = av_rescale_q(pkt->duration, st->time_base, aic->time_base);
-        // rewrite pts and dts to be decoded time line position
-        pkt->pts = pkt->dts = aic->dts;
-        aic->dts += pkt->duration;
-        if ((ret = ff_interleave_add_packet(s, pkt, compare_ts)) < 0)
-            return ret;
-    }
-
-    return get_packet(s, out, NULL, flush);
-}
diff --git a/libavformat/retimeinterleave.h b/libavformat/retimeinterleave.h
deleted file mode 100644
index de0a7442b0..0000000000
--- a/libavformat/retimeinterleave.h
+++ /dev/null
@@ -1,51 +0,0 @@
-/*
- * audio interleaving prototypes and declarations
- *
- * Copyright (c) 2009 Baptiste Coudurier <baptiste dot coudurier at gmail dot com>
- *
- * This file is part of FFmpeg.
- *
- * FFmpeg is free software; you can redistribute it and/or
- * modify it under the terms of the GNU Lesser General Public
- * License as published by the Free Software Foundation; either
- * version 2.1 of the License, or (at your option) any later version.
- *
- * FFmpeg is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
- * Lesser General Public License for more details.
- *
- * You should have received a copy of the GNU Lesser General Public
- * License along with FFmpeg; if not, write to the Free Software
- * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
- */
-
-#ifndef AVFORMAT_RETIMEINTERLEAVE_H
-#define AVFORMAT_RETIMEINTERLEAVE_H
-
-#include "avformat.h"
-
-typedef struct RetimeInterleaveContext {
-    uint64_t dts;                 ///< current dts
-    AVRational time_base;         ///< time base of output packets
-} RetimeInterleaveContext;
-
-/**
- * Init the retime interleave context
- */
-void ff_retime_interleave_init(RetimeInterleaveContext *aic, AVRational time_base);
-
-/**
- * Retime packets per RetimeInterleaveContext->time_base and interleave them
- * correctly.
- * The first element of AVStream->priv_data must be RetimeInterleaveContext
- * when using this function.
- *
- * @param get_packet function will output a packet when streams are correctly interleaved.
- * @param compare_ts function will compare AVPackets and decide interleaving order.
- */
-int ff_retime_interleave(AVFormatContext *s, AVPacket *out, AVPacket *pkt, int flush,
-                        int (*get_packet)(AVFormatContext *, AVPacket *, AVPacket *, int),
-                        int (*compare_ts)(AVFormatContext *, const AVPacket *, const AVPacket *));
-
-#endif /* AVFORMAT_AUDIOINTERLEAVE_H */



More information about the ffmpeg-cvslog mailing list