[FFmpeg-devel] [PATCH 1/2] avformat/dv: allow returning damaged audio

Michael Niedermayer michael at niedermayer.cc
Sat Aug 1 01:20:18 EEST 2020


Fixes: Ticket8762
Signed-off-by: Michael Niedermayer <michael at niedermayer.cc>
---
 libavformat/dv.c | 49 +++++++++++++++++++++++++++++++++++++++++-------
 1 file changed, 42 insertions(+), 7 deletions(-)

diff --git a/libavformat/dv.c b/libavformat/dv.c
index e99422d4b5..7ebe1815b3 100644
--- a/libavformat/dv.c
+++ b/libavformat/dv.c
@@ -34,12 +34,18 @@
 #include "libavcodec/dv_profile.h"
 #include "libavcodec/dv.h"
 #include "libavutil/channel_layout.h"
+#include "libavutil/opt.h"
 #include "libavutil/intreadwrite.h"
 #include "libavutil/mathematics.h"
 #include "libavutil/timecode.h"
 #include "dv.h"
 #include "libavutil/avassert.h"
 
+enum AudioConceal {
+    AUDIO_CONCEAL_PASS = 1,
+    AUDIO_CONCEAL_DROP = 0,
+};
+
 struct DVDemuxContext {
     const AVDVProfile*  sys;    /* Current DV profile. E.g.: 525/60, 625/50 */
     AVFormatContext*  fctx;
@@ -50,6 +56,8 @@ struct DVDemuxContext {
     int               ach;
     int               frames;
     uint64_t          abytes;
+    uint8_t           as_pack[5];
+    int               dvaudio_concealment;
 };
 
 static inline uint16_t dv_audio_12to16(uint16_t sample)
@@ -72,7 +80,7 @@ static inline uint16_t dv_audio_12to16(uint16_t sample)
     return result;
 }
 
-static const uint8_t *dv_extract_pack(const uint8_t *frame, enum dv_pack_type t)
+static const uint8_t *dv_extract_pack(DVDemuxContext *d, const uint8_t *frame, enum dv_pack_type t)
 {
     int offs;
     int c;
@@ -101,6 +109,13 @@ static const uint8_t *dv_extract_pack(const uint8_t *frame, enum dv_pack_type t)
             break;
     }
 
+    if (t == dv_audio_source || t == dv_audio_control) {
+        if (frame[offs] == t) {
+            memcpy(d->as_pack, &frame[offs], sizeof(d->as_pack));
+        } else if (d->as_pack[0] && d->dvaudio_concealment == AUDIO_CONCEAL_PASS)
+            return d->as_pack;
+    }
+
     return frame[offs] == t ? &frame[offs] : NULL;
 }
 
@@ -116,7 +131,7 @@ static const int dv_audio_frequency[3] = {
  * 3. Audio is always returned as 16-bit linear samples: 12-bit nonlinear samples
  *    are converted into 16-bit linear ones.
  */
-static int dv_extract_audio(const uint8_t *frame, uint8_t **ppcm,
+static int dv_extract_audio(DVDemuxContext *c, const uint8_t *frame, uint8_t **ppcm,
                             const AVDVProfile *sys)
 {
     int size, chan, i, j, d, of, smpls, freq, quant, half_ch;
@@ -124,7 +139,7 @@ static int dv_extract_audio(const uint8_t *frame, uint8_t **ppcm,
     const uint8_t *as_pack;
     uint8_t *pcm, ipcm;
 
-    as_pack = dv_extract_pack(frame, dv_audio_source);
+    as_pack = dv_extract_pack(c, frame, dv_audio_source);
     if (!as_pack)    /* No audio ? */
         return 0;
 
@@ -224,7 +239,7 @@ static int dv_extract_audio_info(DVDemuxContext *c, const uint8_t *frame)
     const uint8_t *as_pack;
     int freq, stype, smpls, quant, i, ach;
 
-    as_pack = dv_extract_pack(frame, dv_audio_source);
+    as_pack = dv_extract_pack(c, frame, dv_audio_source);
     if (!as_pack || !c->sys) {    /* No audio ? */
         c->ach = 0;
         return 0;
@@ -292,7 +307,7 @@ static int dv_extract_video_info(DVDemuxContext *c, const uint8_t *frame)
     c->vst->avg_frame_rate = av_inv_q(c->vst->time_base);
 
     /* finding out SAR is a little bit messy */
-    vsc_pack = dv_extract_pack(frame, dv_video_control);
+    vsc_pack = dv_extract_pack(c, frame, dv_video_control);
     apt      = frame[4] & 0x07;
     is16_9   = (vsc_pack && ((vsc_pack[2] & 0x07) == 0x02 ||
                              (!apt && (vsc_pack[2] & 0x07) == 0x07)));
@@ -312,7 +327,7 @@ static int dv_extract_timecode(DVDemuxContext* c, const uint8_t* frame, char *tc
     // is only relevant for NTSC systems.
     int prevent_df = c->sys->ltc_divisor == 25 || c->sys->ltc_divisor == 50;
 
-    tc_pack = dv_extract_pack(frame, dv_timecode);
+    tc_pack = dv_extract_pack(c, frame, dv_timecode);
     if (!tc_pack)
         return 0;
     av_timecode_make_smpte_tc_string(tc, AV_RB32(tc_pack + 1), prevent_df);
@@ -384,7 +399,7 @@ int avpriv_dv_produce_packet(DVDemuxContext *c, AVPacket *pkt,
         ppcm[i] = c->audio_buf[i];
     }
     if (c->ach)
-        dv_extract_audio(buf, ppcm, c->sys);
+        dv_extract_audio(c, buf, ppcm, c->sys);
 
     /* We work with 720p frames split in half, thus even frames have
      * channels 0,1 and odd 2,3. */
@@ -452,8 +467,10 @@ void ff_dv_offset_reset(DVDemuxContext *c, int64_t frame_offset)
  ************************************************************/
 
 typedef struct RawDVContext {
+    const AVClass  *class;
     DVDemuxContext *dv_demux;
     uint8_t         buf[DV_MAX_FRAME_SIZE];
+    int             dvaudio_concealment;
 } RawDVContext;
 
 static int dv_read_timecode(AVFormatContext *s) {
@@ -500,6 +517,7 @@ static int dv_read_header(AVFormatContext *s)
     c->dv_demux = avpriv_dv_init_demux(s);
     if (!c->dv_demux)
         return AVERROR(ENOMEM);
+    c->dv_demux->dvaudio_concealment = c->dvaudio_concealment;
 
     state = avio_rb32(s->pb);
     while ((state & 0xffffff7f) != 0x1f07003f) {
@@ -639,6 +657,22 @@ static int dv_probe(const AVProbeData *p)
     return 0;
 }
 
+#define OFFSET(x) offsetof(RawDVContext, x)
+#define DEC AV_OPT_FLAG_DECODING_PARAM
+static const AVOption dv_options[] = {
+    { "dvaudio_concealment", "", OFFSET(dvaudio_concealment), AV_OPT_TYPE_INT  , {.i64 = AUDIO_CONCEAL_DROP}, 0, INT_MAX, DEC, "dvaudio_concealment"},
+    { "drop",                "", 0                          , AV_OPT_TYPE_CONST, {.i64 = AUDIO_CONCEAL_DROP}, 0, INT_MAX, DEC, "dvaudio_concealment"},
+    { "pass",                "", 0                          , AV_OPT_TYPE_CONST, {.i64 = AUDIO_CONCEAL_PASS}, 0, INT_MAX, DEC, "dvaudio_concealment"},
+    { NULL },
+};
+
+static const AVClass dv_demuxer_class = {
+    .class_name = "DV demuxer",
+    .item_name  = av_default_item_name,
+    .option     = dv_options,
+    .version    = LIBAVUTIL_VERSION_INT,
+};
+
 AVInputFormat ff_dv_demuxer = {
     .name           = "dv",
     .long_name      = NULL_IF_CONFIG_SMALL("DV (Digital Video)"),
@@ -649,4 +683,5 @@ AVInputFormat ff_dv_demuxer = {
     .read_close     = dv_read_close,
     .read_seek      = dv_read_seek,
     .extensions     = "dv,dif",
+    .priv_class     = &dv_demuxer_class,
 };
-- 
2.17.1



More information about the ffmpeg-devel mailing list