avformat/dashenc: Set VP9 codec string with profile, level and bitdepth
[ffmpeg.git] / libavformat / takdec.c
index 182eb9a..6fda35c 100644 (file)
  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  */
 
+#include "libavutil/crc.h"
+
+#define BITSTREAM_READER_LE
 #include "libavcodec/tak.h"
+
+#include "apetag.h"
 #include "avformat.h"
+#include "avio_internal.h"
 #include "internal.h"
 #include "rawdec.h"
-#include "apetag.h"
 
 typedef struct TAKDemuxContext {
-    int      mlast_frame;
-    int64_t  left;
+    int     mlast_frame;
+    int64_t data_end;
 } TAKDemuxContext;
 
 static int tak_probe(AVProbeData *p)
 {
     if (!memcmp(p->buf, "tBaK", 4))
-        return AVPROBE_SCORE_MAX / 2;
+        return AVPROBE_SCORE_EXTENSION;
     return 0;
 }
 
+static unsigned long tak_check_crc(unsigned long checksum, const uint8_t *buf,
+                                   unsigned int len)
+{
+    return av_crc(av_crc_get_table(AV_CRC_24_IEEE), checksum, buf, len);
+}
+
 static int tak_read_header(AVFormatContext *s)
 {
     TAKDemuxContext *tc = s->priv_data;
-    AVIOContext *pb = s->pb;
+    AVIOContext *pb     = s->pb;
     GetBitContext gb;
     AVStream *st;
     uint8_t *buffer = NULL;
@@ -50,9 +61,9 @@ static int tak_read_header(AVFormatContext *s)
     if (!st)
         return AVERROR(ENOMEM);
 
-    st->codec->codec_type = AVMEDIA_TYPE_AUDIO;
-    st->codec->codec_id   = AV_CODEC_ID_TAK;
-    st->need_parsing      = AVSTREAM_PARSE_FULL_RAW;
+    st->codecpar->codec_type = AVMEDIA_TYPE_AUDIO;
+    st->codecpar->codec_id   = AV_CODEC_ID_TAK;
+    st->need_parsing         = AVSTREAM_PARSE_FULL_RAW;
 
     tc->mlast_frame = 0;
     if (avio_rl32(pb) != MKTAG('t', 'B', 'a', 'K')) {
@@ -60,7 +71,7 @@ static int tak_read_header(AVFormatContext *s)
         return 0;
     }
 
-    while (!url_feof(pb)) {
+    while (!avio_feof(pb)) {
         enum TAKMetaDataType type;
         int size;
 
@@ -71,16 +82,27 @@ static int tak_read_header(AVFormatContext *s)
         case TAK_METADATA_STREAMINFO:
         case TAK_METADATA_LAST_FRAME:
         case TAK_METADATA_ENCODER:
-            buffer = av_malloc(size + FF_INPUT_BUFFER_PADDING_SIZE);
+            if (size <= 3)
+                return AVERROR_INVALIDDATA;
+
+            buffer = av_malloc(size - 3 + AV_INPUT_BUFFER_PADDING_SIZE);
             if (!buffer)
                 return AVERROR(ENOMEM);
+            memset(buffer + size - 3, 0, AV_INPUT_BUFFER_PADDING_SIZE);
 
-            if (avio_read(pb, buffer, size) != size) {
+            ffio_init_checksum(pb, tak_check_crc, 0xCE04B7U);
+            if (avio_read(pb, buffer, size - 3) != size - 3) {
                 av_freep(&buffer);
                 return AVERROR(EIO);
             }
+            if (ffio_get_checksum(s->pb) != avio_rb24(pb)) {
+                av_log(s, AV_LOG_ERROR, "%d metadata block CRC error.\n", type);
+                if (s->error_recognition & AV_EF_EXPLODE) {
+                    av_freep(&buffer);
+                    return AVERROR_INVALIDDATA;
+                }
+            }
 
-            init_get_bits(&gb, buffer, size * 8);
             break;
         case TAK_METADATA_MD5: {
             uint8_t md5[16];
@@ -88,23 +110,31 @@ static int tak_read_header(AVFormatContext *s)
 
             if (size != 19)
                 return AVERROR_INVALIDDATA;
+            ffio_init_checksum(pb, tak_check_crc, 0xCE04B7U);
             avio_read(pb, md5, 16);
-            avio_skip(pb, 3);
+            if (ffio_get_checksum(s->pb) != avio_rb24(pb)) {
+                av_log(s, AV_LOG_ERROR, "MD5 metadata block CRC error.\n");
+                if (s->error_recognition & AV_EF_EXPLODE)
+                    return AVERROR_INVALIDDATA;
+            }
+
             av_log(s, AV_LOG_VERBOSE, "MD5=");
             for (i = 0; i < 16; i++)
                 av_log(s, AV_LOG_VERBOSE, "%02x", md5[i]);
             av_log(s, AV_LOG_VERBOSE, "\n");
             break;
-            }
-        case TAK_METADATA_END:
-            if (pb->seekable) {
-                int64_t curpos = avio_tell(pb);
+        }
+        case TAK_METADATA_END: {
+            int64_t curpos = avio_tell(pb);
 
+            if (pb->seekable & AVIO_SEEKABLE_NORMAL) {
                 ff_ape_parse_tag(s);
                 avio_seek(pb, curpos, SEEK_SET);
             }
+
+            tc->data_end += curpos;
             return 0;
-            break;
+        }
         default:
             ret = avio_skip(pb, size);
             if (ret < 0)
@@ -114,27 +144,31 @@ static int tak_read_header(AVFormatContext *s)
         if (type == TAK_METADATA_STREAMINFO) {
             TAKStreamInfo ti;
 
-            avpriv_tak_parse_streaminfo(&gb, &ti);
+            ret = avpriv_tak_parse_streaminfo(&ti, buffer, size -3);
+            if (ret < 0)
+                return AVERROR_INVALIDDATA;
             if (ti.samples > 0)
                 st->duration = ti.samples;
-            st->codec->bits_per_coded_sample = ti.bps;
+            st->codecpar->bits_per_coded_sample = ti.bps;
             if (ti.ch_layout)
-                st->codec->channel_layout = ti.ch_layout;
-            st->codec->sample_rate = ti.sample_rate;
-            st->codec->channels    = ti.channels;
-            st->start_time         = 0;
-            avpriv_set_pts_info(st, 64, 1, st->codec->sample_rate);
-            st->codec->extradata      = buffer;
-            st->codec->extradata_size = size;
-            buffer = NULL;
+                st->codecpar->channel_layout = ti.ch_layout;
+            st->codecpar->sample_rate           = ti.sample_rate;
+            st->codecpar->channels              = ti.channels;
+            st->start_time                   = 0;
+            avpriv_set_pts_info(st, 64, 1, st->codecpar->sample_rate);
+            st->codecpar->extradata             = buffer;
+            st->codecpar->extradata_size        = size - 3;
+            buffer                           = NULL;
         } else if (type == TAK_METADATA_LAST_FRAME) {
             if (size != 11)
                 return AVERROR_INVALIDDATA;
+            init_get_bits8(&gb, buffer, size - 3);
             tc->mlast_frame = 1;
-            tc->left = get_bits_longlong(&gb, TAK_LAST_FRAME_POS_BITS) +
-                       get_bits(&gb, TAK_LAST_FRAME_SIZE_BITS);
+            tc->data_end    = get_bits64(&gb, TAK_LAST_FRAME_POS_BITS) +
+                              get_bits(&gb, TAK_LAST_FRAME_SIZE_BITS);
             av_freep(&buffer);
         } else if (type == TAK_METADATA_ENCODER) {
+            init_get_bits8(&gb, buffer, size - 3);
             av_log(s, AV_LOG_VERBOSE, "encoder version: %0X\n",
                    get_bits_long(&gb, TAK_ENCODER_VERSION_BITS));
             av_freep(&buffer);
@@ -151,10 +185,11 @@ static int raw_read_packet(AVFormatContext *s, AVPacket *pkt)
 
     if (tc->mlast_frame) {
         AVIOContext *pb = s->pb;
-        int64_t size;
+        int64_t size, left;
 
-        size = FFMIN(tc->left, 1024);
-        if (!size)
+        left = tc->data_end - avio_tell(pb);
+        size = FFMIN(left, 1024);
+        if (size <= 0)
             return AVERROR_EOF;
 
         ret = av_get_packet(pb, pkt, size);
@@ -162,7 +197,6 @@ static int raw_read_packet(AVFormatContext *s, AVPacket *pkt)
             return ret;
 
         pkt->stream_index = 0;
-        tc->left -= ret;
     } else {
         ret = ff_raw_read_partial_packet(s, pkt);
     }