]> git.sesse.net Git - ffmpeg/blobdiff - libavformat/dv.c
Add forgotten avio-direct reference file.
[ffmpeg] / libavformat / dv.c
index d99dee37824be6ea7cad387cdbf7f7b2323b3573..81a0e47adedc61f2da215b28d1c79efb5967a8bc 100644 (file)
@@ -34,7 +34,9 @@
 #include "libavcodec/dvdata.h"
 #include "libavutil/intreadwrite.h"
 #include "libavutil/mathematics.h"
+#include "libavutil/timecode.h"
 #include "dv.h"
+#include "libavutil/avassert.h"
 
 struct DVDemuxContext {
     const DVprofile*  sys;    /* Current DV profile. E.g.: 525/60, 625/50 */
@@ -97,6 +99,10 @@ static const uint8_t* dv_extract_pack(uint8_t* frame, enum dv_pack_type t)
     return frame[offs] == t ? &frame[offs] : NULL;
 }
 
+static const int dv_audio_frequency[3] = {
+    48000, 44100, 32000,
+};
+
 /*
  * There's a couple of assumptions being made here:
  * 1. By default we silence erroneous (0x8000/16bit 0x800/12bit) audio samples.
@@ -124,21 +130,29 @@ static int dv_extract_audio(uint8_t* frame, uint8_t* ppcm[4],
     if (quant > 1)
         return -1; /* unsupported quantization */
 
+    if (freq >= FF_ARRAY_ELEMS(dv_audio_frequency))
+        return AVERROR_INVALIDDATA;
+
     size = (sys->audio_min_samples[freq] + smpls) * 4; /* 2ch, 2bytes */
     half_ch = sys->difseg_size / 2;
 
     /* We work with 720p frames split in half, thus even frames have
      * channels 0,1 and odd 2,3. */
     ipcm = (sys->height == 720 && !(frame[1] & 0x0C)) ? 2 : 0;
-    pcm  = ppcm[ipcm++];
 
     /* for each DIF channel */
     for (chan = 0; chan < sys->n_difchan; chan++) {
+        av_assert0(ipcm<4);
+        pcm = ppcm[ipcm++];
+        if (!pcm)
+            break;
+
         /* for each DIF segment */
         for (i = 0; i < sys->difseg_size; i++) {
             frame += 6 * 80; /* skip DIF segment header */
             if (quant == 1 && i == half_ch) {
                 /* next stereo channel (12bit mode only) */
+                av_assert0(ipcm<4);
                 pcm = ppcm[ipcm++];
                 if (!pcm)
                     break;
@@ -181,11 +195,6 @@ static int dv_extract_audio(uint8_t* frame, uint8_t* ppcm[4],
                 frame += 16 * 80; /* 15 Video DIFs + 1 Audio DIF */
             }
         }
-
-        /* next stereo channel (50Mbps and 100Mbps only) */
-        pcm = ppcm[ipcm++];
-        if (!pcm)
-            break;
     }
 
     return size;
@@ -207,6 +216,18 @@ static int dv_extract_audio_info(DVDemuxContext* c, uint8_t* frame)
     stype = (as_pack[3] & 0x1f);      /* 0 - 2CH, 2 - 4CH, 3 - 8CH */
     quant =  as_pack[4] & 0x07;       /* 0 - 16bit linear, 1 - 12bit nonlinear */
 
+    if (freq >= FF_ARRAY_ELEMS(dv_audio_frequency)) {
+        av_log(c->fctx, AV_LOG_ERROR,
+               "Unrecognized audio sample rate index (%d)\n", freq);
+        return 0;
+    }
+
+    if (stype > 3) {
+        av_log(c->fctx, AV_LOG_ERROR, "stype %d is invalid\n", stype);
+        c->ach = 0;
+        return 0;
+    }
+
     /* note: ach counts PAIRS of channels (i.e. stereo channels) */
     ach = ((int[4]){  1,  0,  2,  4})[stype];
     if (ach == 1 && quant && freq == 2)
@@ -268,42 +289,19 @@ static int dv_extract_video_info(DVDemuxContext *c, uint8_t* frame)
     return size;
 }
 
-static int bcd2int(uint8_t bcd)
+static int dv_extract_timecode(DVDemuxContext* c, uint8_t* frame, char *tc)
 {
-   int low  = bcd & 0xf;
-   int high = bcd >> 4;
-   if (low > 9 || high > 9)
-       return -1;
-   return low + 10*high;
-}
-
-static int dv_extract_timecode(DVDemuxContext* c, uint8_t* frame, char tc[32])
-{
-    int hh, mm, ss, ff, drop_frame;
     const uint8_t *tc_pack;
 
-    tc_pack = dv_extract_pack(frame, dv_timecode);
-    if (!tc_pack)
-        return 0;
-
-    ff = bcd2int(tc_pack[1] & 0x3f);
-    ss = bcd2int(tc_pack[2] & 0x7f);
-    mm = bcd2int(tc_pack[3] & 0x7f);
-    hh = bcd2int(tc_pack[4] & 0x3f);
-    drop_frame = tc_pack[1] >> 6 & 0x1;
-
-    if (ff < 0 || ss < 0 || mm < 0 || hh < 0)
-        return -1;
-
     // For PAL systems, drop frame bit is replaced by an arbitrary
     // bit so its value should not be considered. Drop frame timecode
     // is only relevant for NTSC systems.
-    if(c->sys->ltc_divisor == 25 || c->sys->ltc_divisor == 50) {
-        drop_frame = 0;
-    }
+    int prevent_df = c->sys->ltc_divisor == 25 || c->sys->ltc_divisor == 50;
 
-    snprintf(tc, 32, "%02d:%02d:%02d%c%02d",
-             hh, mm, ss, drop_frame ? ';' : ':', ff);
+    tc_pack = dv_extract_pack(frame, dv_timecode);
+    if (!tc_pack)
+        return 0;
+    av_timecode_make_smpte_tc_string(tc, AV_RB32(tc_pack + 1), prevent_df);
     return 1;
 }
 
@@ -325,13 +323,7 @@ DVDemuxContext* avpriv_dv_init_demux(AVFormatContext *s)
         return NULL;
     }
 
-    c->sys  = NULL;
-    c->fctx = s;
-    memset(c->ast, 0, sizeof(c->ast));
-    c->ach    = 0;
-    c->frames = 0;
-    c->abytes = 0;
-
+    c->fctx                   = s;
     c->vst->codec->codec_type = AVMEDIA_TYPE_VIDEO;
     c->vst->codec->codec_id   = CODEC_ID_DVVIDEO;
     c->vst->codec->bit_rate   = 25000000;
@@ -378,7 +370,8 @@ int avpriv_dv_produce_packet(DVDemuxContext *c, AVPacket *pkt,
        c->audio_pkt[i].pts  = c->abytes * 30000*8 / c->ast[i]->codec->bit_rate;
        ppcm[i] = c->audio_buf[i];
     }
-    dv_extract_audio(buf, ppcm, c->sys);
+    if (c->ach)
+        dv_extract_audio(buf, ppcm, c->sys);
 
     /* We work with 720p frames split in half, thus even frames have
      * channels 0,1 and odd 2,3. */
@@ -425,7 +418,7 @@ static int64_t dv_frame_offset(AVFormatContext *s, DVDemuxContext *c,
     return offset + s->data_offset;
 }
 
-void dv_offset_reset(DVDemuxContext *c, int64_t frame_offset)
+void ff_dv_offset_reset(DVDemuxContext *c, int64_t frame_offset)
 {
     c->frames= frame_offset;
     if (c->ach)
@@ -446,7 +439,7 @@ typedef struct RawDVContext {
 
 static int dv_read_timecode(AVFormatContext *s) {
     int ret;
-    char timecode[32];
+    char timecode[AV_TIMECODE_STR_SIZE];
     int64_t pos = avio_tell(s->pb);
 
     // Read 3 DIF blocks: Header block and 2 Subcode blocks.
@@ -476,8 +469,7 @@ finish:
     return ret;
 }
 
-static int dv_read_header(AVFormatContext *s,
-                          AVFormatParameters *ap)
+static int dv_read_header(AVFormatContext *s)
 {
     unsigned state, marker_pos = 0;
     RawDVContext *c = s->priv_data;
@@ -503,7 +495,7 @@ static int dv_read_header(AVFormatContext *s,
     }
     AV_WB32(c->buf, state);
 
-    if (avio_read(s->pb, c->buf + 4, DV_PROFILE_BYTES - 4) <= 0 ||
+    if (avio_read(s->pb, c->buf + 4, DV_PROFILE_BYTES - 4) != DV_PROFILE_BYTES - 4 ||
         avio_seek(s->pb, -DV_PROFILE_BYTES, SEEK_CUR) < 0)
         return AVERROR(EIO);
 
@@ -554,7 +546,7 @@ static int dv_read_seek(AVFormatContext *s, int stream_index,
     if (avio_seek(s->pb, offset, SEEK_SET) < 0)
         return -1;
 
-    dv_offset_reset(c, offset / c->sys->frame_size);
+    ff_dv_offset_reset(c, offset / c->sys->frame_size);
     return 0;
 }