]> git.sesse.net Git - ffmpeg/blobdiff - libavformat/electronicarts.c
mov: Wrap stsc index and count compare in a separate function
[ffmpeg] / libavformat / electronicarts.c
index c2f4dba8ab06ce522eb5c19ad13816fc387e6fc7..2df27e4a00e7281cd2a94577d783ff336b4e91db 100644 (file)
@@ -1,5 +1,5 @@
 /* Electronic Arts Multimedia File Demuxer
- * Copyright (c) 2004  The ffmpeg Project
+ * Copyright (c) 2004  The FFmpeg project
  * Copyright (c) 2006-2008 Peter Ross
  *
  * This file is part of Libav.
@@ -25,6 +25,8 @@
  * by Robin Kay (komadori at gekkou.co.uk)
  */
 
+#include <inttypes.h>
+
 #include "libavutil/intreadwrite.h"
 #include "avformat.h"
 #include "internal.h"
 #define GSTR_TAG MKTAG('G', 'S', 'T', 'R')
 #define SCDl_TAG MKTAG('S', 'C', 'D', 'l')
 #define SCEl_TAG MKTAG('S', 'C', 'E', 'l')
-#define kVGT_TAG MKTAG('k', 'V', 'G', 'T')  /* TGV i-frame */
-#define fVGT_TAG MKTAG('f', 'V', 'G', 'T')  /* TGV p-frame */
+#define kVGT_TAG MKTAG('k', 'V', 'G', 'T')  /* TGV I-frame */
+#define fVGT_TAG MKTAG('f', 'V', 'G', 'T')  /* TGV P-frame */
 #define mTCD_TAG MKTAG('m', 'T', 'C', 'D')  /* MDEC */
-#define MADk_TAG MKTAG('M', 'A', 'D', 'k')  /* MAD i-frame */
-#define MADm_TAG MKTAG('M', 'A', 'D', 'm')  /* MAD p-frame */
+#define MADk_TAG MKTAG('M', 'A', 'D', 'k')  /* MAD I-frame */
+#define MADm_TAG MKTAG('M', 'A', 'D', 'm')  /* MAD P-frame */
 #define MADe_TAG MKTAG('M', 'A', 'D', 'e')  /* MAD lqp-frame */
-#define MPCh_TAG MKTAG('M', 'P', 'C', 'h')  /* MPEG2 */
-#define TGQs_TAG MKTAG('T', 'G', 'Q', 's')  /* TGQ i-frame (appears in .TGQ files) */
-#define pQGT_TAG MKTAG('p', 'Q', 'G', 'T')  /* TGQ i-frame (appears in .UV files) */
-#define pIQT_TAG MKTAG('p', 'I', 'Q', 'T')  /* TQI/UV2 i-frame (.UV2/.WVE) */
+#define MPCh_TAG MKTAG('M', 'P', 'C', 'h')  /* MPEG-2 */
+#define TGQs_TAG MKTAG('T', 'G', 'Q', 's')  /* TGQ I-frame (appears in .TGQ files) */
+#define pQGT_TAG MKTAG('p', 'Q', 'G', 'T')  /* TGQ I-frame (appears in .UV files) */
+#define pIQT_TAG MKTAG('p', 'I', 'Q', 'T')  /* TQI/UV2 I-frame (.UV2/.WVE) */
 #define MVhd_TAG MKTAG('M', 'V', 'h', 'd')
 #define MV0K_TAG MKTAG('M', 'V', '0', 'K')
 #define MV0F_TAG MKTAG('M', 'V', '0', 'F')
 #define MVIh_TAG MKTAG('M', 'V', 'I', 'h')  /* CMV header */
-#define MVIf_TAG MKTAG('M', 'V', 'I', 'f')  /* CMV i-frame */
+#define MVIf_TAG MKTAG('M', 'V', 'I', 'f')  /* CMV I-frame */
 
 typedef struct EaDemuxContext {
     int big_endian;
@@ -77,7 +79,7 @@ typedef struct EaDemuxContext {
     int num_samples;
 } EaDemuxContext;
 
-static uint32_t read_arbitary(AVIOContext *pb)
+static uint32_t read_arbitrary(AVIOContext *pb)
 {
     uint8_t size, byte;
     int i;
@@ -95,73 +97,69 @@ static uint32_t read_arbitary(AVIOContext *pb)
     return word;
 }
 
-/*
- * Process PT/GSTR sound header
- * return 1 if success, 0 if invalid format, otherwise AVERROR_xxx
- */
 static int process_audio_header_elements(AVFormatContext *s)
 {
     EaDemuxContext *ea = s->priv_data;
     AVIOContext    *pb = s->pb;
-    int inHeader = 1;
+    int in_header = 1;
     int compression_type = -1, revision = -1, revision2 = -1;
 
     ea->bytes        = 2;
     ea->sample_rate  = -1;
     ea->num_channels = 1;
 
-    while (!pb->eof_reached && inHeader) {
-        int inSubheader;
+    while (!pb->eof_reached && in_header) {
+        int in_subheader;
         uint8_t byte;
         byte = avio_r8(pb);
 
         switch (byte) {
         case 0xFD:
             av_log(s, AV_LOG_DEBUG, "entered audio subheader\n");
-            inSubheader = 1;
-            while (!pb->eof_reached && inSubheader) {
+            in_subheader = 1;
+            while (!pb->eof_reached && in_subheader) {
                 uint8_t subbyte;
                 subbyte = avio_r8(pb);
 
                 switch (subbyte) {
                 case 0x80:
-                    revision = read_arbitary(pb);
+                    revision = read_arbitrary(pb);
                     av_log(s, AV_LOG_DEBUG,
                            "revision (element 0x80) set to 0x%08x\n", revision);
                     break;
                 case 0x82:
-                    ea->num_channels = read_arbitary(pb);
+                    ea->num_channels = read_arbitrary(pb);
                     av_log(s, AV_LOG_DEBUG,
                            "num_channels (element 0x82) set to 0x%08x\n",
                            ea->num_channels);
                     break;
                 case 0x83:
-                    compression_type = read_arbitary(pb);
+                    compression_type = read_arbitrary(pb);
                     av_log(s, AV_LOG_DEBUG,
                            "compression_type (element 0x83) set to 0x%08x\n",
                            compression_type);
                     break;
                 case 0x84:
-                    ea->sample_rate = read_arbitary(pb);
+                    ea->sample_rate = read_arbitrary(pb);
                     av_log(s, AV_LOG_DEBUG,
                            "sample_rate (element 0x84) set to %i\n",
                            ea->sample_rate);
                     break;
                 case 0x85:
-                    ea->num_samples = read_arbitary(pb);
+                    ea->num_samples = read_arbitrary(pb);
                     av_log(s, AV_LOG_DEBUG,
                            "num_samples (element 0x85) set to 0x%08x\n",
                            ea->num_samples);
                     break;
                 case 0x8A:
                     av_log(s, AV_LOG_DEBUG,
-                           "element 0x%02x set to 0x%08x\n",
-                           subbyte, read_arbitary(pb));
+                           "element 0x%02x set to 0x%08"PRIx32"\n",
+                           subbyte, read_arbitrary(pb));
                     av_log(s, AV_LOG_DEBUG, "exited audio subheader\n");
-                    inSubheader = 0;
+                    in_subheader = 0;
                     break;
                 case 0xA0:
-                    revision2 = read_arbitary(pb);
+                    revision2 = read_arbitrary(pb);
                     av_log(s, AV_LOG_DEBUG,
                            "revision2 (element 0xA0) set to 0x%08x\n",
                            revision2);
@@ -169,25 +167,25 @@ static int process_audio_header_elements(AVFormatContext *s)
                 case 0xFF:
                     av_log(s, AV_LOG_DEBUG,
                            "end of header block reached (within audio subheader)\n");
-                    inSubheader = 0;
-                    inHeader    = 0;
+                    in_subheader = 0;
+                    in_header    = 0;
                     break;
                 default:
                     av_log(s, AV_LOG_DEBUG,
-                           "element 0x%02x set to 0x%08x\n",
-                           subbyte, read_arbitary(pb));
+                           "element 0x%02x set to 0x%08"PRIx32"\n",
+                           subbyte, read_arbitrary(pb));
                     break;
                 }
             }
             break;
         case 0xFF:
             av_log(s, AV_LOG_DEBUG, "end of header block reached\n");
-            inHeader = 0;
+            in_header = 0;
             break;
         default:
             av_log(s, AV_LOG_DEBUG,
-                   "header element 0x%02x set to 0x%08x\n",
-                   byte, read_arbitary(pb));
+                   "header element 0x%02x set to 0x%08"PRIx32"\n",
+                   byte, read_arbitrary(pb));
             break;
         }
     }
@@ -249,11 +247,7 @@ static int process_audio_header_elements(AVFormatContext *s)
     return 1;
 }
 
-/*
- * Process EACS sound header
- * return 1 if success, 0 if invalid format, otherwise AVERROR_xxx
- */
-static int process_audio_header_eacs(AVFormatContext *s)
+static void process_audio_header_eacs(AVFormatContext *s)
 {
     EaDemuxContext *ea = s->priv_data;
     AVIOContext *pb    = s->pb;
@@ -288,15 +282,9 @@ static int process_audio_header_eacs(AVFormatContext *s)
                "unsupported stream type; audio compression_type=%i\n",
                compression_type);
     }
-
-    return 1;
 }
 
-/*
- * Process SEAD sound header
- * return 1 if success, 0 if invalid format, otherwise AVERROR_xxx
- */
-static int process_audio_header_sead(AVFormatContext *s)
+static void process_audio_header_sead(AVFormatContext *s)
 {
     EaDemuxContext *ea = s->priv_data;
     AVIOContext *pb    = s->pb;
@@ -305,11 +293,9 @@ static int process_audio_header_sead(AVFormatContext *s)
     ea->bytes        = avio_rl32(pb);  /* 1=8-bit, 2=16-bit */
     ea->num_channels = avio_rl32(pb);
     ea->audio_codec  = AV_CODEC_ID_ADPCM_IMA_EA_SEAD;
-
-    return 1;
 }
 
-static int process_video_header_mdec(AVFormatContext *s)
+static void process_video_header_mdec(AVFormatContext *s)
 {
     EaDemuxContext *ea = s->priv_data;
     AVIOContext *pb    = s->pb;
@@ -318,11 +304,9 @@ static int process_video_header_mdec(AVFormatContext *s)
     ea->height      = avio_rl16(pb);
     ea->time_base   = (AVRational) { 1, 15 };
     ea->video_codec = AV_CODEC_ID_MDEC;
-
-    return 1;
 }
 
-static int process_video_header_vp6(AVFormatContext *s)
+static void process_video_header_vp6(AVFormatContext *s)
 {
     EaDemuxContext *ea = s->priv_data;
     AVIOContext *pb    = s->pb;
@@ -331,11 +315,9 @@ static int process_video_header_vp6(AVFormatContext *s)
     ea->time_base.den = avio_rl32(pb);
     ea->time_base.num = avio_rl32(pb);
     ea->video_codec   = AV_CODEC_ID_VP6;
-
-    return 1;
 }
 
-static int process_video_header_cmv(AVFormatContext *s)
+static void process_video_header_cmv(AVFormatContext *s)
 {
     EaDemuxContext *ea = s->priv_data;
     int fps;
@@ -345,14 +327,10 @@ static int process_video_header_cmv(AVFormatContext *s)
     if (fps)
         ea->time_base = (AVRational) { 1, fps };
     ea->video_codec = AV_CODEC_ID_CMV;
-
-    return 0;
 }
 
-/*
- * Process EA file header
- * Returns 1 if the EA file is valid and successfully opened, 0 otherwise
- */
+/* Process EA file header.
+ * Return 1 if the EA file is valid and successfully opened, 0 otherwise. */
 static int process_ea_header(AVFormatContext *s)
 {
     uint32_t blockid, size = 0;
@@ -377,7 +355,7 @@ static int process_ea_header(AVFormatContext *s)
                 av_log(s, AV_LOG_ERROR, "unknown 1SNh headerid\n");
                 return 0;
             }
-            err = process_audio_header_eacs(s);
+            process_audio_header_eacs(s);
             break;
 
         case SCHl_TAG:
@@ -393,11 +371,11 @@ static int process_ea_header(AVFormatContext *s)
             break;
 
         case SEAD_TAG:
-            err = process_audio_header_sead(s);
+            process_audio_header_sead(s);
             break;
 
         case MVIh_TAG:
-            err = process_video_header_cmv(s);
+            process_video_header_cmv(s);
             break;
 
         case kVGT_TAG:
@@ -406,7 +384,7 @@ static int process_ea_header(AVFormatContext *s)
             break;
 
         case mTCD_TAG:
-            err = process_video_header_mdec(s);
+            process_video_header_mdec(s);
             break;
 
         case MPCh_TAG:
@@ -416,18 +394,22 @@ static int process_ea_header(AVFormatContext *s)
         case pQGT_TAG:
         case TGQs_TAG:
             ea->video_codec = AV_CODEC_ID_TGQ;
+            ea->time_base   = (AVRational) { 1, 15 };
             break;
 
         case pIQT_TAG:
             ea->video_codec = AV_CODEC_ID_TQI;
+            ea->time_base   = (AVRational) { 1, 15 };
             break;
 
         case MADk_TAG:
             ea->video_codec = AV_CODEC_ID_MAD;
+            avio_skip(pb, 6);
+            ea->time_base = (AVRational) { avio_rl16(pb), 1000 };
             break;
 
         case MVhd_TAG:
-            err = process_video_header_vp6(s);
+            process_video_header_vp6(s);
             break;
         }
 
@@ -480,18 +462,18 @@ static int ea_read_header(AVFormatContext *s)
         if (!st)
             return AVERROR(ENOMEM);
         ea->video_stream_index = st->index;
-        st->codec->codec_type  = AVMEDIA_TYPE_VIDEO;
-        st->codec->codec_id    = ea->video_codec;
-        st->codec->codec_tag   = 0; /* no fourcc */
-        st->codec->width       = ea->width;
-        st->codec->height      = ea->height;
+        st->codecpar->codec_type  = AVMEDIA_TYPE_VIDEO;
+        st->codecpar->codec_id    = ea->video_codec;
+        st->codecpar->codec_tag   = 0; /* no fourcc */
+        st->codecpar->width       = ea->width;
+        st->codecpar->height      = ea->height;
         avpriv_set_pts_info(st, 33, ea->time_base.num, ea->time_base.den);
         st->avg_frame_rate     = (AVRational) { ea->time_base.den,
                                                 ea->time_base.num };
     }
 
     if (ea->audio_codec) {
-        if (ea->num_channels <= 0) {
+        if (ea->num_channels <= 0 || ea->num_channels > 2) {
             av_log(s, AV_LOG_WARNING,
                    "Unsupported number of channels: %d\n", ea->num_channels);
             ea->audio_codec = 0;
@@ -515,17 +497,17 @@ static int ea_read_header(AVFormatContext *s)
         if (!st)
             return AVERROR(ENOMEM);
         avpriv_set_pts_info(st, 33, 1, ea->sample_rate);
-        st->codec->codec_type            = AVMEDIA_TYPE_AUDIO;
-        st->codec->codec_id              = ea->audio_codec;
-        st->codec->codec_tag             = 0;   /* no tag */
-        st->codec->channels              = ea->num_channels;
-        st->codec->sample_rate           = ea->sample_rate;
-        st->codec->bits_per_coded_sample = ea->bytes * 8;
-        st->codec->bit_rate              = st->codec->channels *
-                                           st->codec->sample_rate *
-                                           st->codec->bits_per_coded_sample / 4;
-        st->codec->block_align           = st->codec->channels *
-                                           st->codec->bits_per_coded_sample;
+        st->codecpar->codec_type            = AVMEDIA_TYPE_AUDIO;
+        st->codecpar->codec_id              = ea->audio_codec;
+        st->codecpar->codec_tag             = 0;   /* no tag */
+        st->codecpar->channels              = ea->num_channels;
+        st->codecpar->sample_rate           = ea->sample_rate;
+        st->codecpar->bits_per_coded_sample = ea->bytes * 8;
+        st->codecpar->bit_rate              = st->codecpar->channels *
+                                              st->codecpar->sample_rate *
+                                              st->codecpar->bits_per_coded_sample / 4;
+        st->codecpar->block_align           = st->codecpar->channels *
+                                              st->codecpar->bits_per_coded_sample;
         ea->audio_stream_index           = st->index;
         st->start_time                   = 0;
     }
@@ -544,7 +526,7 @@ static int ea_read_packet(AVFormatContext *s, AVPacket *pkt)
     while (!packet_read) {
         chunk_type = avio_rl32(pb);
         chunk_size = ea->big_endian ? avio_rb32(pb) : avio_rl32(pb);
-        if (chunk_size <= 8)
+        if (chunk_size < 8)
             return AVERROR_INVALIDDATA;
         chunk_size -= 8;
 
@@ -569,6 +551,9 @@ static int ea_read_packet(AVFormatContext *s, AVPacket *pkt)
                 avio_skip(pb, 8);
                 chunk_size -= 12;
             }
+            if (!chunk_size)
+                continue;
+
             ret = av_get_packet(pb, pkt, chunk_size);
             if (ret < 0)
                 return ret;
@@ -579,10 +564,16 @@ static int ea_read_packet(AVFormatContext *s, AVPacket *pkt)
             case AV_CODEC_ID_ADPCM_EA_R1:
             case AV_CODEC_ID_ADPCM_EA_R2:
             case AV_CODEC_ID_ADPCM_IMA_EA_EACS:
-                pkt->duration = AV_RL32(pkt->data);
-                break;
             case AV_CODEC_ID_ADPCM_EA_R3:
-                pkt->duration = AV_RB32(pkt->data);
+                if (pkt->size < 4) {
+                    av_log(s, AV_LOG_ERROR, "Packet is too short\n");
+                    av_packet_unref(pkt);
+                    return AVERROR_INVALIDDATA;
+                }
+                if (ea->audio_codec == AV_CODEC_ID_ADPCM_EA_R3)
+                    pkt->duration = AV_RB32(pkt->data);
+                else
+                    pkt->duration = AV_RL32(pkt->data);
                 break;
             case AV_CODEC_ID_ADPCM_IMA_EA_SEAD:
                 pkt->duration = ret * 2 / ea->num_channels;
@@ -623,7 +614,10 @@ static int ea_read_packet(AVFormatContext *s, AVPacket *pkt)
             goto get_video_packet;
 
         case mTCD_TAG:
-            avio_skip(pb, 8);               // skip ea dct header
+            if (chunk_size < 8)
+                return AVERROR_INVALIDDATA;
+
+            avio_skip(pb, 8);               // skip ea DCT header
             chunk_size -= 8;
             goto get_video_packet;
 
@@ -633,6 +627,9 @@ static int ea_read_packet(AVFormatContext *s, AVPacket *pkt)
             key = AV_PKT_FLAG_KEY;
         case MV0F_TAG:
 get_video_packet:
+            if (!chunk_size)
+                continue;
+
             ret = av_get_packet(pb, pkt, chunk_size);
             if (ret < 0)
                 return ret;