]> git.sesse.net Git - ffmpeg/blobdiff - libavformat/matroskadec.c
avformat/matroskadec: fix the type of the TrackLanguage
[ffmpeg] / libavformat / matroskadec.c
index c224c3447a2902667d3616e2ef68ec7520cf09f2..383869bced8d08062e12949b3e86c1a1f32d1ee1 100644 (file)
@@ -554,7 +554,7 @@ static EbmlSyntax matroska_track[] = {
     { MATROSKA_ID_CODECID,               EBML_STR,   0, offsetof(MatroskaTrack, codec_id) },
     { MATROSKA_ID_CODECPRIVATE,          EBML_BIN,   0, offsetof(MatroskaTrack, codec_priv) },
     { MATROSKA_ID_CODECDELAY,            EBML_UINT,  0, offsetof(MatroskaTrack, codec_delay) },
-    { MATROSKA_ID_TRACKLANGUAGE,         EBML_UTF8,  0, offsetof(MatroskaTrack, language),     { .s = "eng" } },
+    { MATROSKA_ID_TRACKLANGUAGE,         EBML_STR,   0, offsetof(MatroskaTrack, language),     { .s = "eng" } },
     { MATROSKA_ID_TRACKDEFAULTDURATION,  EBML_UINT,  0, offsetof(MatroskaTrack, default_duration) },
     { MATROSKA_ID_TRACKTIMECODESCALE,    EBML_FLOAT, 0, offsetof(MatroskaTrack, time_scale),   { .f = 1.0 } },
     { MATROSKA_ID_TRACKFLAGDEFAULT,      EBML_UINT,  0, offsetof(MatroskaTrack, flag_default), { .u = 1 } },
@@ -707,7 +707,7 @@ static EbmlSyntax matroska_segments[] = {
 };
 
 static EbmlSyntax matroska_blockmore[] = {
-    { MATROSKA_ID_BLOCKADDID,      EBML_UINT, 0, offsetof(MatroskaBlock,additional_id) },
+    { MATROSKA_ID_BLOCKADDID,      EBML_UINT, 0, offsetof(MatroskaBlock,additional_id), { .u = 1 } },
     { MATROSKA_ID_BLOCKADDITIONAL, EBML_BIN,  0, offsetof(MatroskaBlock,additional) },
     CHILD_OF(matroska_blockadditions)
 };
@@ -1024,28 +1024,17 @@ static int ebml_read_master(MatroskaDemuxContext *matroska,
 }
 
 /*
- * Read signed/unsigned "EBML" numbers.
+ * Read a signed "EBML number"
  * Return: number of bytes processed, < 0 on error
  */
-static int matroska_ebmlnum_uint(MatroskaDemuxContext *matroska,
-                                 uint8_t *data, uint32_t size, uint64_t *num)
-{
-    AVIOContext pb;
-    ffio_init_context(&pb, data, size, 0, NULL, NULL, NULL, NULL);
-    return ebml_read_num(matroska, &pb, FFMIN(size, 8), num, 1);
-}
-
-/*
- * Same as above, but signed.
- */
 static int matroska_ebmlnum_sint(MatroskaDemuxContext *matroska,
-                                 uint8_t *data, uint32_t size, int64_t *num)
+                                 AVIOContext *pb, int64_t *num)
 {
     uint64_t unum;
     int res;
 
     /* read as unsigned number first */
-    if ((res = matroska_ebmlnum_uint(matroska, data, size, &unum)) < 0)
+    if ((res = ebml_read_num(matroska, pb, 8, &unum, 1)) < 0)
         return res;
 
     /* make signed (weird way) */
@@ -1610,6 +1599,7 @@ static int matroska_decode_buffer(uint8_t **buf, int *buf_size,
 #if CONFIG_LZO
     case MATROSKA_TRACK_ENCODING_COMP_LZO:
         do {
+            int insize = isize;
             olen       = pkt_size *= 3;
             newpktdata = av_realloc(pkt_data, pkt_size + AV_LZO_OUTPUT_PADDING
                                                        + AV_INPUT_BUFFER_PADDING_SIZE);
@@ -1618,7 +1608,7 @@ static int matroska_decode_buffer(uint8_t **buf, int *buf_size,
                 goto failed;
             }
             pkt_data = newpktdata;
-            result   = av_lzo1x_decode(pkt_data, &olen, data, &isize);
+            result   = av_lzo1x_decode(pkt_data, &olen, data, &insize);
         } while (result == AV_LZO_OUTPUT_FULL && pkt_size < 10000000);
         if (result) {
             result = AVERROR_INVALIDDATA;
@@ -2413,8 +2403,8 @@ static int matroska_parse_tracks(AVFormatContext *s)
 
         if (key_id_base64) {
             /* export encryption key id as base64 metadata tag */
-            av_dict_set(&st->metadata, "enc_key_id", key_id_base64, 0);
-            av_freep(&key_id_base64);
+            av_dict_set(&st->metadata, "enc_key_id", key_id_base64,
+                        AV_DICT_DONT_STRDUP_VAL);
         }
 
         if (!strcmp(track->codec_id, "V_MS/VFW/FOURCC") &&
@@ -2545,34 +2535,33 @@ static int matroska_parse_tracks(AVFormatContext *s)
             memcpy(&extradata[12], track->codec_priv.data,
                    track->codec_priv.size);
         } else if (codec_id == AV_CODEC_ID_TTA) {
-            extradata_size = 30;
-            extradata      = av_mallocz(extradata_size + AV_INPUT_BUFFER_PADDING_SIZE);
-            if (!extradata)
-                return AVERROR(ENOMEM);
-            ffio_init_context(&b, extradata, extradata_size, 1,
-                              NULL, NULL, NULL, NULL);
-            avio_write(&b, "TTA1", 4);
-            avio_wl16(&b, 1);
+            uint8_t *ptr;
             if (track->audio.channels > UINT16_MAX ||
                 track->audio.bitdepth > UINT16_MAX) {
                 av_log(matroska->ctx, AV_LOG_WARNING,
                        "Too large audio channel number %"PRIu64
                        " or bitdepth %"PRIu64". Skipping track.\n",
                        track->audio.channels, track->audio.bitdepth);
-                av_freep(&extradata);
                 if (matroska->ctx->error_recognition & AV_EF_EXPLODE)
                     return AVERROR_INVALIDDATA;
                 else
                     continue;
             }
-            avio_wl16(&b, track->audio.channels);
-            avio_wl16(&b, track->audio.bitdepth);
             if (track->audio.out_samplerate < 0 || track->audio.out_samplerate > INT_MAX)
                 return AVERROR_INVALIDDATA;
-            avio_wl32(&b, track->audio.out_samplerate);
-            avio_wl32(&b, av_rescale((matroska->duration * matroska->time_scale),
-                                     track->audio.out_samplerate,
-                                     AV_TIME_BASE * 1000));
+            extradata_size = 22;
+            extradata      = av_mallocz(extradata_size + AV_INPUT_BUFFER_PADDING_SIZE);
+            if (!extradata)
+                return AVERROR(ENOMEM);
+            ptr = extradata;
+            bytestream_put_be32(&ptr, AV_RB32("TTA1"));
+            bytestream_put_le16(&ptr, 1);
+            bytestream_put_le16(&ptr, track->audio.channels);
+            bytestream_put_le16(&ptr, track->audio.bitdepth);
+            bytestream_put_le32(&ptr, track->audio.out_samplerate);
+            bytestream_put_le32(&ptr, av_rescale(matroska->duration * matroska->time_scale,
+                                                 track->audio.out_samplerate,
+                                                 AV_TIME_BASE * 1000));
         } else if (codec_id == AV_CODEC_ID_RV10 ||
                    codec_id == AV_CODEC_ID_RV20 ||
                    codec_id == AV_CODEC_ID_RV30 ||
@@ -2990,20 +2979,15 @@ static void matroska_clear_queue(MatroskaDemuxContext *matroska)
 }
 
 static int matroska_parse_laces(MatroskaDemuxContext *matroska, uint8_t **buf,
-                                int *buf_size, int type,
-                                uint32_t **lace_buf, int *laces)
+                                int size, int type, AVIOContext *pb,
+                                uint32_t lace_size[256], int *laces)
 {
-    int res = 0, n, size = *buf_size;
+    int n;
     uint8_t *data = *buf;
-    uint32_t *lace_size;
 
     if (!type) {
         *laces    = 1;
-        *lace_buf = av_malloc(sizeof(**lace_buf));
-        if (!*lace_buf)
-            return AVERROR(ENOMEM);
-
-        *lace_buf[0] = size;
+        lace_size[0] = size;
         return 0;
     }
 
@@ -3011,22 +2995,18 @@ static int matroska_parse_laces(MatroskaDemuxContext *matroska, uint8_t **buf,
     *laces    = *data + 1;
     data     += 1;
     size     -= 1;
-    lace_size = av_malloc_array(*laces, sizeof(*lace_size));
-    if (!lace_size)
-        return AVERROR(ENOMEM);
 
     switch (type) {
     case 0x1: /* Xiph lacing */
     {
         uint8_t temp;
         uint32_t total = 0;
-        for (n = 0; res == 0 && n < *laces - 1; n++) {
+        for (n = 0; n < *laces - 1; n++) {
             lace_size[n] = 0;
 
             while (1) {
                 if (size <= total) {
-                    res = AVERROR_INVALIDDATA;
-                    break;
+                    return AVERROR_INVALIDDATA;
                 }
                 temp          = *data;
                 total        += temp;
@@ -3038,8 +3018,7 @@ static int matroska_parse_laces(MatroskaDemuxContext *matroska, uint8_t **buf,
             }
         }
         if (size <= total) {
-            res = AVERROR_INVALIDDATA;
-            break;
+            return AVERROR_INVALIDDATA;
         }
 
         lace_size[n] = size - total;
@@ -3048,8 +3027,7 @@ static int matroska_parse_laces(MatroskaDemuxContext *matroska, uint8_t **buf,
 
     case 0x2: /* fixed-size lacing */
         if (size % (*laces)) {
-            res = AVERROR_INVALIDDATA;
-            break;
+            return AVERROR_INVALIDDATA;
         }
         for (n = 0; n < *laces; n++)
             lace_size[n] = size / *laces;
@@ -3059,34 +3037,35 @@ static int matroska_parse_laces(MatroskaDemuxContext *matroska, uint8_t **buf,
     {
         uint64_t num;
         uint64_t total;
-        n = matroska_ebmlnum_uint(matroska, data, size, &num);
-        if (n < 0 || num > INT_MAX) {
-            av_log(matroska->ctx, AV_LOG_INFO,
-                   "EBML block data error\n");
-            res = n<0 ? n : AVERROR_INVALIDDATA;
-            break;
-        }
-        data += n;
-        size -= n;
+        int offset;
+
+        avio_skip(pb, 4);
+
+        n = ebml_read_num(matroska, pb, 8, &num, 1);
+        if (n < 0)
+            return n;
+        if (num > INT_MAX)
+            return AVERROR_INVALIDDATA;
+
         total = lace_size[0] = num;
-        for (n = 1; res == 0 && n < *laces - 1; n++) {
+        offset = n;
+        for (n = 1; n < *laces - 1; n++) {
             int64_t snum;
             int r;
-            r = matroska_ebmlnum_sint(matroska, data, size, &snum);
-            if (r < 0 || lace_size[n - 1] + snum > (uint64_t)INT_MAX) {
-                av_log(matroska->ctx, AV_LOG_INFO,
-                       "EBML block data error\n");
-                res = r<0 ? r : AVERROR_INVALIDDATA;
-                break;
-            }
-            data        += r;
-            size        -= r;
+            r = matroska_ebmlnum_sint(matroska, pb, &snum);
+            if (r < 0)
+                return r;
+            if (lace_size[n - 1] + snum > (uint64_t)INT_MAX)
+                return AVERROR_INVALIDDATA;
+
             lace_size[n] = lace_size[n - 1] + snum;
             total       += lace_size[n];
+            offset      += r;
         }
+        data += offset;
+        size -= offset;
         if (size <= total) {
-            res = AVERROR_INVALIDDATA;
-            break;
+            return AVERROR_INVALIDDATA;
         }
         lace_size[*laces - 1] = size - total;
         break;
@@ -3094,10 +3073,8 @@ static int matroska_parse_laces(MatroskaDemuxContext *matroska, uint8_t **buf,
     }
 
     *buf      = data;
-    *lace_buf = lace_size;
-    *buf_size = size;
 
-    return res;
+    return 0;
 }
 
 static int matroska_parse_rm_audio(MatroskaDemuxContext *matroska,
@@ -3263,11 +3240,8 @@ fail:
 static int matroska_parse_prores(MatroskaTrack *track, uint8_t *src,
                                  uint8_t **pdst, int *size)
 {
-    uint8_t *dst = src;
-    int dstlen = *size;
-
-    if (AV_RB32(&src[4]) != MKBETAG('i', 'c', 'p', 'f')) {
-        dstlen += 8;
+    uint8_t *dst;
+    int dstlen = *size + 8;
 
         dst = av_malloc(dstlen + AV_INPUT_BUFFER_PADDING_SIZE);
         if (!dst)
@@ -3277,7 +3251,6 @@ static int matroska_parse_prores(MatroskaTrack *track, uint8_t *src,
         AV_WB32(dst + 4, MKBETAG('i', 'c', 'p', 'f'));
         memcpy(dst + 8, src, dstlen - 8);
         memset(dst + dstlen, 0, AV_INPUT_BUFFER_PADDING_SIZE);
-    }
 
     *pdst = dst;
     *size = dstlen;
@@ -3432,7 +3405,8 @@ static int matroska_parse_frame(MatroskaDemuxContext *matroska,
         pkt_data = wv_data;
     }
 
-    if (st->codecpar->codec_id == AV_CODEC_ID_PRORES) {
+    if (st->codecpar->codec_id == AV_CODEC_ID_PRORES &&
+        AV_RB32(pkt_data + 4)  != MKBETAG('i', 'c', 'p', 'f')) {
         uint8_t *pr_data;
         res = matroska_parse_prores(track, pkt_data, &pr_data, &pkt_size);
         if (res < 0) {
@@ -3529,17 +3503,19 @@ static int matroska_parse_block(MatroskaDemuxContext *matroska, AVBufferRef *buf
 {
     uint64_t timecode = AV_NOPTS_VALUE;
     MatroskaTrack *track;
+    AVIOContext pb;
     int res = 0;
     AVStream *st;
     int16_t block_time;
-    uint32_t *lace_size = NULL;
+    uint32_t lace_size[256];
     int n, flags, laces = 0;
     uint64_t num;
     int trust_default_duration = 1;
 
-    if ((n = matroska_ebmlnum_uint(matroska, data, size, &num)) < 0) {
+    ffio_init_context(&pb, data, size, 0, NULL, NULL, NULL, NULL);
+
+    if ((n = ebml_read_num(matroska, &pb, 8, &num, 1)) < 0)
         return n;
-    }
     data += n;
     size -= n;
 
@@ -3590,11 +3566,12 @@ static int matroska_parse_block(MatroskaDemuxContext *matroska, AVBufferRef *buf
         }
     }
 
-    res = matroska_parse_laces(matroska, &data, &size, (flags & 0x06) >> 1,
-                               &lace_size, &laces);
-
-    if (res)
-        goto end;
+    res = matroska_parse_laces(matroska, &data, size, (flags & 0x06) >> 1,
+                               &pb, lace_size, &laces);
+    if (res < 0) {
+        av_log(matroska->ctx, AV_LOG_ERROR, "Error parsing frame sizes.\n");
+        return res;
+    }
 
     if (track->audio.samplerate == 8000) {
         // If this is needed for more codecs, then add them here
@@ -3614,11 +3591,6 @@ static int matroska_parse_block(MatroskaDemuxContext *matroska, AVBufferRef *buf
     for (n = 0; n < laces; n++) {
         int64_t lace_duration = block_duration*(n+1) / laces - block_duration*n / laces;
 
-        if (lace_size[n] > size) {
-            av_log(matroska->ctx, AV_LOG_ERROR, "Invalid packet size\n");
-            break;
-        }
-
         if ((st->codecpar->codec_id == AV_CODEC_ID_RA_288 ||
              st->codecpar->codec_id == AV_CODEC_ID_COOK   ||
              st->codecpar->codec_id == AV_CODEC_ID_SIPR   ||
@@ -3628,7 +3600,7 @@ static int matroska_parse_block(MatroskaDemuxContext *matroska, AVBufferRef *buf
                                           lace_size[n],
                                           timecode, pos);
             if (res)
-                goto end;
+                return res;
 
         } else if (st->codecpar->codec_id == AV_CODEC_ID_WEBVTT) {
             res = matroska_parse_webvtt(matroska, track, st,
@@ -3636,7 +3608,7 @@ static int matroska_parse_block(MatroskaDemuxContext *matroska, AVBufferRef *buf
                                         timecode, lace_duration,
                                         pos);
             if (res)
-                goto end;
+                return res;
         } else {
             res = matroska_parse_frame(matroska, track, st, buf, data, lace_size[n],
                                        timecode, lace_duration, pos,
@@ -3644,18 +3616,15 @@ static int matroska_parse_block(MatroskaDemuxContext *matroska, AVBufferRef *buf
                                        additional, additional_id, additional_size,
                                        discard_padding);
             if (res)
-                goto end;
+                return res;
         }
 
         if (timecode != AV_NOPTS_VALUE)
             timecode = lace_duration ? timecode + lace_duration : AV_NOPTS_VALUE;
         data += lace_size[n];
-        size -= lace_size[n];
     }
 
-end:
-    av_free(lace_size);
-    return res;
+    return 0;
 }
 
 static int matroska_parse_cluster(MatroskaDemuxContext *matroska)
@@ -4142,8 +4111,8 @@ static int webm_dash_manifest_cues(AVFormatContext *s, int64_t init_range)
         }
         end += ret;
     }
-    av_dict_set(&s->streams[0]->metadata, CUE_TIMESTAMPS, buf, 0);
-    av_free(buf);
+    av_dict_set(&s->streams[0]->metadata, CUE_TIMESTAMPS,
+                buf, AV_DICT_DONT_STRDUP_VAL);
 
     return 0;
 }
@@ -4168,8 +4137,8 @@ static int webm_dash_manifest_read_header(AVFormatContext *s)
     if (!matroska->is_live) {
         buf = av_asprintf("%g", matroska->duration);
         if (!buf) return AVERROR(ENOMEM);
-        av_dict_set(&s->streams[0]->metadata, DURATION, buf, 0);
-        av_free(buf);
+        av_dict_set(&s->streams[0]->metadata, DURATION,
+                    buf, AV_DICT_DONT_STRDUP_VAL);
 
         // initialization range
         // 5 is the offset of Cluster ID.