X-Git-Url: https://git.sesse.net/?a=blobdiff_plain;f=libavformat%2Felectronicarts.c;h=b3855b69a585d36c6095ee146da2d8222ee3abed;hb=e02de9df4b218bd6e1e927b67fd4075741545688;hp=22e2e804351ff145b1ab625428f435ce3ee4bea4;hpb=288f2ffb57ae9e9eee2748aca26da3aeb3ca6f6c;p=ffmpeg diff --git a/libavformat/electronicarts.c b/libavformat/electronicarts.c index 22e2e804351..b3855b69a58 100644 --- a/libavformat/electronicarts.c +++ b/libavformat/electronicarts.c @@ -25,6 +25,8 @@ * by Robin Kay (komadori at gekkou.co.uk) */ +#include + #include "libavutil/intreadwrite.h" #include "avformat.h" #include "internal.h" @@ -44,21 +46,21 @@ #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; @@ -99,65 +101,65 @@ 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); @@ -165,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; } } @@ -245,7 +247,7 @@ static int process_audio_header_elements(AVFormatContext *s) return 1; } -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; @@ -280,11 +282,9 @@ static int process_audio_header_eacs(AVFormatContext *s) "unsupported stream type; audio compression_type=%i\n", compression_type); } - - return 1; } -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; @@ -293,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; @@ -306,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; @@ -319,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; @@ -333,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; @@ -365,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: @@ -381,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: @@ -394,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: @@ -404,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; } @@ -479,7 +473,7 @@ static int ea_read_header(AVFormatContext *s) } 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; @@ -532,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; @@ -557,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; @@ -567,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; @@ -611,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; @@ -621,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;