libavformat/electronicarts.c
ad81a9fe
 /* Electronic Arts Multimedia File Demuxer
6dfa70f2
  * Copyright (c) 2004 The FFmpeg Project
b2f181c2
  * Copyright (c) 2006-2008 Peter Ross
ad81a9fe
  *
b78e7197
  * This file is part of FFmpeg.
  *
  * FFmpeg is free software; you can redistribute it and/or
ad81a9fe
  * modify it under the terms of the GNU Lesser General Public
  * License as published by the Free Software Foundation; either
b78e7197
  * version 2.1 of the License, or (at your option) any later version.
ad81a9fe
  *
b78e7197
  * FFmpeg is distributed in the hope that it will be useful,
ad81a9fe
  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  * Lesser General Public License for more details.
  *
  * You should have received a copy of the GNU Lesser General Public
b78e7197
  * License along with FFmpeg; if not, write to the Free Software
5509bffa
  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
ad81a9fe
  */
 
 /**
ba87f080
  * @file
ad81a9fe
  * Electronic Arts Multimedia file demuxer (WVE/UV2/etc.)
  * by Robin Kay (komadori at gekkou.co.uk)
  */
 
d92024f1
 #include <inttypes.h>
 
6a5d31ac
 #include "libavutil/intreadwrite.h"
ad81a9fe
 #include "avformat.h"
c3f9ebf7
 #include "internal.h"
ad81a9fe
 
 #define SCHl_TAG MKTAG('S', 'C', 'H', 'l')
8747fce9
 #define SEAD_TAG MKTAG('S', 'E', 'A', 'D')  /* Sxxx header */
 #define SNDC_TAG MKTAG('S', 'N', 'D', 'C')  /* Sxxx data */
 #define SEND_TAG MKTAG('S', 'E', 'N', 'D')  /* Sxxx end */
 #define SHEN_TAG MKTAG('S', 'H', 'E', 'N')  /* SxEN header */
 #define SDEN_TAG MKTAG('S', 'D', 'E', 'N')  /* SxEN data */
 #define SEEN_TAG MKTAG('S', 'E', 'E', 'N')  /* SxEN end */
 #define ISNh_TAG MKTAG('1', 'S', 'N', 'h')  /* 1SNx header */
98168220
 #define EACS_TAG MKTAG('E', 'A', 'C', 'S')
8747fce9
 #define ISNd_TAG MKTAG('1', 'S', 'N', 'd')  /* 1SNx data */
 #define ISNe_TAG MKTAG('1', 'S', 'N', 'e')  /* 1SNx end */
ad81a9fe
 #define PT00_TAG MKTAG('P', 'T', 0x0, 0x0)
d3e412d0
 #define GSTR_TAG MKTAG('G', 'S', 'T', 'R')
ad81a9fe
 #define SCDl_TAG MKTAG('S', 'C', 'D', 'l')
 #define SCEl_TAG MKTAG('S', 'C', 'E', 'l')
a90cff13
 #define kVGT_TAG MKTAG('k', 'V', 'G', 'T')  /* TGV I-frame */
 #define fVGT_TAG MKTAG('f', 'V', 'G', 'T')  /* TGV P-frame */
8747fce9
 #define mTCD_TAG MKTAG('m', 'T', 'C', 'D')  /* MDEC */
a90cff13
 #define MADk_TAG MKTAG('M', 'A', 'D', 'k')  /* MAD I-frame */
 #define MADm_TAG MKTAG('M', 'A', 'D', 'm')  /* MAD P-frame */
8747fce9
 #define MADe_TAG MKTAG('M', 'A', 'D', 'e')  /* MAD lqp-frame */
a90cff13
 #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) */
d3e412d0
 #define MVhd_TAG MKTAG('M', 'V', 'h', 'd')
 #define MV0K_TAG MKTAG('M', 'V', '0', 'K')
 #define MV0F_TAG MKTAG('M', 'V', '0', 'F')
803bdc54
 #define AVhd_TAG MKTAG('A', 'V', 'h', 'd')
 #define AV0K_TAG MKTAG('A', 'V', '0', 'K')
 #define AV0F_TAG MKTAG('A', 'V', '0', 'F')
8747fce9
 #define MVIh_TAG MKTAG('M', 'V', 'I', 'h')  /* CMV header */
a90cff13
 #define MVIf_TAG MKTAG('M', 'V', 'I', 'f')  /* CMV I-frame */
b368428f
 #define AVP6_TAG MKTAG('A', 'V', 'P', '6')
ad81a9fe
 
a2517fca
 typedef struct VideoProperties {
     enum AVCodecID codec;
d3e412d0
     AVRational time_base;
d9d9a8c0
     int width, height;
015fd02b
     int nb_frames;
a2517fca
     int stream_index;
 } VideoProperties;
 
 typedef struct EaDemuxContext {
     int big_endian;
 
803bdc54
     VideoProperties video, alpha;
ad81a9fe
 
36ef5369
     enum AVCodecID audio_codec;
ad81a9fe
     int audio_stream_index;
 
ffbd0bcd
     int bytes;
6c867e04
     int sample_rate;
ad81a9fe
     int num_channels;
     int num_samples;
0d7c0274
 
     int platform;
ad81a9fe
 } EaDemuxContext;
 
4908c8ef
 static uint32_t read_arbitrary(AVIOContext *pb)
8747fce9
 {
ad81a9fe
     uint8_t size, byte;
     int i;
     uint32_t word;
 
e63a3628
     size = avio_r8(pb);
ad81a9fe
 
     word = 0;
     for (i = 0; i < size; i++) {
8747fce9
         byte   = avio_r8(pb);
ad81a9fe
         word <<= 8;
8747fce9
         word  |= byte;
ad81a9fe
     }
 
     return word;
 }
 
215eb102
 static int process_audio_header_elements(AVFormatContext *s)
 {
e4141433
     EaDemuxContext *ea = s->priv_data;
8747fce9
     AVIOContext    *pb = s->pb;
4908c8ef
     int in_header = 1;
725d86bf
     int compression_type = -1, revision = -1, revision2 = -1;
ad81a9fe
 
8747fce9
     ea->bytes        = 2;
     ea->sample_rate  = -1;
9853ce80
     ea->num_channels = 1;
 
d34ec64a
     while (!avio_feof(pb) && in_header) {
4908c8ef
         int in_subheader;
ad81a9fe
         uint8_t byte;
e63a3628
         byte = avio_r8(pb);
ad81a9fe
 
         switch (byte) {
         case 0xFD:
8747fce9
             av_log(s, AV_LOG_DEBUG, "entered audio subheader\n");
4908c8ef
             in_subheader = 1;
d34ec64a
             while (!avio_feof(pb) && in_subheader) {
ad81a9fe
                 uint8_t subbyte;
e63a3628
                 subbyte = avio_r8(pb);
ad81a9fe
 
                 switch (subbyte) {
6c867e04
                 case 0x80:
4908c8ef
                     revision = read_arbitrary(pb);
8747fce9
                     av_log(s, AV_LOG_DEBUG,
                            "revision (element 0x80) set to 0x%08x\n", revision);
6c867e04
                     break;
ad81a9fe
                 case 0x82:
4908c8ef
                     ea->num_channels = read_arbitrary(pb);
8747fce9
                     av_log(s, AV_LOG_DEBUG,
                            "num_channels (element 0x82) set to 0x%08x\n",
                            ea->num_channels);
ad81a9fe
                     break;
                 case 0x83:
4908c8ef
                     compression_type = read_arbitrary(pb);
8747fce9
                     av_log(s, AV_LOG_DEBUG,
                            "compression_type (element 0x83) set to 0x%08x\n",
                            compression_type);
ad81a9fe
                     break;
6c867e04
                 case 0x84:
4908c8ef
                     ea->sample_rate = read_arbitrary(pb);
8747fce9
                     av_log(s, AV_LOG_DEBUG,
                            "sample_rate (element 0x84) set to %i\n",
                            ea->sample_rate);
6c867e04
                     break;
ad81a9fe
                 case 0x85:
4908c8ef
                     ea->num_samples = read_arbitrary(pb);
8747fce9
                     av_log(s, AV_LOG_DEBUG,
                            "num_samples (element 0x85) set to 0x%08x\n",
                            ea->num_samples);
ad81a9fe
                     break;
                 case 0x8A:
8747fce9
                     av_log(s, AV_LOG_DEBUG,
d92024f1
                            "element 0x%02x set to 0x%08"PRIx32"\n",
4908c8ef
                            subbyte, read_arbitrary(pb));
8747fce9
                     av_log(s, AV_LOG_DEBUG, "exited audio subheader\n");
4908c8ef
                     in_subheader = 0;
ad81a9fe
                     break;
725d86bf
                 case 0xA0:
4908c8ef
                     revision2 = read_arbitrary(pb);
8747fce9
                     av_log(s, AV_LOG_DEBUG,
                            "revision2 (element 0xA0) set to 0x%08x\n",
                            revision2);
725d86bf
                     break;
d3e412d0
                 case 0xFF:
8747fce9
                     av_log(s, AV_LOG_DEBUG,
                            "end of header block reached (within audio subheader)\n");
4908c8ef
                     in_subheader = 0;
                     in_header    = 0;
d3e412d0
                     break;
ad81a9fe
                 default:
8747fce9
                     av_log(s, AV_LOG_DEBUG,
d92024f1
                            "element 0x%02x set to 0x%08"PRIx32"\n",
4908c8ef
                            subbyte, read_arbitrary(pb));
ad81a9fe
                     break;
                 }
             }
             break;
         case 0xFF:
8747fce9
             av_log(s, AV_LOG_DEBUG, "end of header block reached\n");
4908c8ef
             in_header = 0;
ad81a9fe
             break;
         default:
8747fce9
             av_log(s, AV_LOG_DEBUG,
d92024f1
                    "header element 0x%02x set to 0x%08"PRIx32"\n",
4908c8ef
                    byte, read_arbitrary(pb));
ad81a9fe
             break;
         }
     }
 
93fa8b2b
     switch (compression_type) {
8747fce9
     case  0:
         ea->audio_codec = AV_CODEC_ID_PCM_S16LE;
         break;
     case  7:
         ea->audio_codec = AV_CODEC_ID_ADPCM_EA;
         break;
e7583962
     case -1:
         switch (revision) {
8747fce9
         case  1:
             ea->audio_codec = AV_CODEC_ID_ADPCM_EA_R1;
             break;
         case  2:
             ea->audio_codec = AV_CODEC_ID_ADPCM_EA_R2;
             break;
         case  3:
             ea->audio_codec = AV_CODEC_ID_ADPCM_EA_R3;
             break;
         case -1:
             break;
e7583962
         default:
a9b42487
             avpriv_request_sample(s, "stream type; revision=%i", revision);
e7583962
             return 0;
         }
725d86bf
         switch (revision2) {
8747fce9
         case  8:
             ea->audio_codec = AV_CODEC_ID_PCM_S16LE_PLANAR;
             break;
6f78ba8b
         case 10:
             switch (revision) {
             case -1:
             case  2: ea->audio_codec = AV_CODEC_ID_ADPCM_EA_R1; break;
             case  3: ea->audio_codec = AV_CODEC_ID_ADPCM_EA_R2; break;
             default:
a9b42487
                 avpriv_request_sample(s, "stream type; revision=%i, revision2=%i", revision, revision2);
6f78ba8b
                 return 0;
             }
             break;
cdfc61de
         case 15:
8747fce9
         case 16:
             ea->audio_codec = AV_CODEC_ID_MP3;
             break;
         case -1:
             break;
725d86bf
         default:
36ef5369
             ea->audio_codec = AV_CODEC_ID_NONE;
a9b42487
             avpriv_request_sample(s, "stream type; revision2=%i", revision2);
725d86bf
             return 0;
         }
e7583962
         break;
93fa8b2b
     default:
a87cf368
         avpriv_request_sample(s,
                               "stream type; compression_type=%i",
                               compression_type);
93fa8b2b
         return 0;
     }
af704ee4
 
0d7c0274
     if (ea->audio_codec == AV_CODEC_ID_NONE && ea->platform == 0x01)
         ea->audio_codec = AV_CODEC_ID_ADPCM_PSX;
6c867e04
     if (ea->sample_rate == -1)
8747fce9
         ea->sample_rate = revision == 3 ? 48000 : 22050;
6c867e04
 
215eb102
     return 1;
 }
 
163a7297
 static void process_audio_header_eacs(AVFormatContext *s)
98168220
 {
     EaDemuxContext *ea = s->priv_data;
8747fce9
     AVIOContext *pb    = s->pb;
98168220
     int compression_type;
 
e63a3628
     ea->sample_rate  = ea->big_endian ? avio_rb32(pb) : avio_rl32(pb);
     ea->bytes        = avio_r8(pb);   /* 1=8-bit, 2=16-bit */
     ea->num_channels = avio_r8(pb);
     compression_type = avio_r8(pb);
45a8a02a
     avio_skip(pb, 13);
98168220
 
     switch (compression_type) {
     case 0:
         switch (ea->bytes) {
8747fce9
         case 1:
             ea->audio_codec = AV_CODEC_ID_PCM_S8;
             break;
         case 2:
             ea->audio_codec = AV_CODEC_ID_PCM_S16LE;
             break;
98168220
         }
         break;
8747fce9
     case 1:
         ea->audio_codec = AV_CODEC_ID_PCM_MULAW;
         ea->bytes       = 1;
         break;
     case 2:
         ea->audio_codec = AV_CODEC_ID_ADPCM_IMA_EA_EACS;
         break;
98168220
     default:
a87cf368
         avpriv_request_sample(s,
                               "stream type; audio compression_type=%i",
                               compression_type);
98168220
     }
 }
 
163a7297
 static void process_audio_header_sead(AVFormatContext *s)
7bb65d89
 {
     EaDemuxContext *ea = s->priv_data;
8747fce9
     AVIOContext *pb    = s->pb;
7bb65d89
 
e63a3628
     ea->sample_rate  = avio_rl32(pb);
     ea->bytes        = avio_rl32(pb);  /* 1=8-bit, 2=16-bit */
     ea->num_channels = avio_rl32(pb);
36ef5369
     ea->audio_codec  = AV_CODEC_ID_ADPCM_IMA_EA_SEAD;
7bb65d89
 }
 
a2517fca
 static void process_video_header_mdec(AVFormatContext *s, VideoProperties *video)
d9d9a8c0
 {
8747fce9
     AVIOContext *pb    = s->pb;
45a8a02a
     avio_skip(pb, 4);
a2517fca
     video->width       = avio_rl16(pb);
     video->height      = avio_rl16(pb);
     video->time_base   = (AVRational) { 1, 15 };
     video->codec = AV_CODEC_ID_MDEC;
d9d9a8c0
 }
 
a2517fca
 static int process_video_header_vp6(AVFormatContext *s, VideoProperties *video)
f4097430
 {
a2517fca
     AVIOContext *pb = s->pb;
f4097430
 
015fd02b
     avio_skip(pb, 8);
a2517fca
     video->nb_frames = avio_rl32(pb);
015fd02b
     avio_skip(pb, 4);
a2517fca
     video->time_base.den = avio_rl32(pb);
     video->time_base.num = avio_rl32(pb);
     if (video->time_base.den <= 0 || video->time_base.num <= 0) {
1831274f
         av_log(s, AV_LOG_ERROR, "Timebase is invalid\n");
         return AVERROR_INVALIDDATA;
     }
a2517fca
     video->codec   = AV_CODEC_ID_VP6;
f4097430
 
     return 1;
 }
 
a2517fca
 static void process_video_header_cmv(AVFormatContext *s, VideoProperties *video)
e39400c3
 {
     int fps;
 
     avio_skip(s->pb, 10);
     fps = avio_rl16(s->pb);
     if (fps)
a2517fca
         video->time_base = (AVRational) { 1, fps };
     video->codec = AV_CODEC_ID_CMV;
e39400c3
 }
 
a90cff13
 /* Process EA file header.
  * Return 1 if the EA file is valid and successfully opened, 0 otherwise. */
8747fce9
 static int process_ea_header(AVFormatContext *s)
 {
215eb102
     uint32_t blockid, size = 0;
     EaDemuxContext *ea = s->priv_data;
8747fce9
     AVIOContext *pb    = s->pb;
f8cab062
     int i;
 
a2517fca
     for (i = 0; i < 5 && (!ea->audio_codec || !ea->video.codec); i++) {
587e3c34
         uint64_t startpos     = avio_tell(pb);
8747fce9
         int err               = 0;
215eb102
 
e63a3628
         blockid = avio_rl32(pb);
8747fce9
         size    = avio_rl32(pb);
97e5dcc0
         if (i == 0)
55328d5f
             ea->big_endian = size > av_bswap32(size);
97e5dcc0
         if (ea->big_endian)
8fc0162a
             size = av_bswap32(size);
f8cab062
 
20769d93
         if (size < 8) {
             av_log(s, AV_LOG_ERROR, "chunk size too small\n");
             return AVERROR_INVALIDDATA;
         }
 
f8cab062
         switch (blockid) {
8747fce9
         case ISNh_TAG:
             if (avio_rl32(pb) != EACS_TAG) {
a87cf368
                 avpriv_request_sample(s, "unknown 1SNh headerid");
8747fce9
                 return 0;
             }
163a7297
             process_audio_header_eacs(s);
8747fce9
             break;
98168220
 
8747fce9
         case SCHl_TAG:
         case SHEN_TAG:
             blockid = avio_rl32(pb);
             if (blockid == GSTR_TAG) {
                 avio_skip(pb, 4);
0d7c0274
             } else if ((blockid & 0xFF) != (PT00_TAG & 0xFF)) {
                 blockid = avio_rl32(pb);
8747fce9
             }
0d7c0274
             ea->platform = (blockid >> 16) & 0xFF;
8747fce9
             err = process_audio_header_elements(s);
             break;
f8cab062
 
8747fce9
         case SEAD_TAG:
163a7297
             process_audio_header_sead(s);
8747fce9
             break;
7bb65d89
 
8747fce9
         case MVIh_TAG:
a2517fca
             process_video_header_cmv(s, &ea->video);
8747fce9
             break;
b2f181c2
 
8747fce9
         case kVGT_TAG:
a2517fca
             ea->video.codec = AV_CODEC_ID_TGV;
8747fce9
             break;
bbf020e9
 
8747fce9
         case mTCD_TAG:
a2517fca
             process_video_header_mdec(s, &ea->video);
8747fce9
             break;
d9d9a8c0
 
8747fce9
         case MPCh_TAG:
a2517fca
             ea->video.codec = AV_CODEC_ID_MPEG2VIDEO;
8747fce9
             break;
d3302b70
 
8747fce9
         case pQGT_TAG:
         case TGQs_TAG:
a2517fca
             ea->video.codec = AV_CODEC_ID_TGQ;
             ea->video.time_base   = (AVRational) { 1, 15 };
8747fce9
             break;
d2299316
 
8747fce9
         case pIQT_TAG:
a2517fca
             ea->video.codec = AV_CODEC_ID_TQI;
             ea->video.time_base   = (AVRational) { 1, 15 };
8747fce9
             break;
468d298d
 
8747fce9
         case MADk_TAG:
a2517fca
             ea->video.codec = AV_CODEC_ID_MAD;
7b6aae23
             avio_skip(pb, 6);
a2517fca
             ea->video.time_base = (AVRational) { avio_rl16(pb), 1000 };
8747fce9
             break;
eacfe858
 
8747fce9
         case MVhd_TAG:
a2517fca
             err = process_video_header_vp6(s, &ea->video);
8747fce9
             break;
803bdc54
 
         case AVhd_TAG:
             err = process_video_header_vp6(s, &ea->alpha);
             break;
f8cab062
         }
 
2c82386d
         if (err < 0) {
             av_log(s, AV_LOG_ERROR, "error parsing header: %i\n", err);
             return err;
         }
 
f59d8ff8
         avio_seek(pb, startpos + size, SEEK_SET);
f8cab062
     }
215eb102
 
f59d8ff8
     avio_seek(pb, 0, SEEK_SET);
ad81a9fe
 
     return 1;
 }
 
 static int ea_probe(AVProbeData *p)
 {
f9b89b61
     unsigned big_endian, size;
 
39fe9d79
     switch (AV_RL32(&p->buf[0])) {
97e6ee20
     case ISNh_TAG:
39fe9d79
     case SCHl_TAG:
7bb65d89
     case SEAD_TAG:
ebc22cc2
     case SHEN_TAG:
968fc6b9
     case kVGT_TAG:
     case MADk_TAG:
     case MPCh_TAG:
39fe9d79
     case MVhd_TAG:
968fc6b9
     case MVIh_TAG:
b368428f
     case AVP6_TAG:
892d7e78
         break;
     default:
         return 0;
39fe9d79
     }
f9b89b61
     size = AV_RL32(&p->buf[4]);
     big_endian = size > 0x000FFFFF;
     if (big_endian)
         size = av_bswap32(size);
     if (size > 0xfffff || size < 8)
892d7e78
         return 0;
8747fce9
 
892d7e78
     return AVPROBE_SCORE_MAX;
ad81a9fe
 }
 
a2517fca
 static int init_video_stream(AVFormatContext *s, VideoProperties *video)
 {
     AVStream *st;
 
     if (!video->codec)
         return 0;
 
     /* initialize the video decoder stream */
     st = avformat_new_stream(s, NULL);
     if (!st)
         return AVERROR(ENOMEM);
     video->stream_index = st->index;
     st->codec->codec_type  = AVMEDIA_TYPE_VIDEO;
     st->codec->codec_id    = video->codec;
     // parsing is necessary to make FFmpeg generate correct timestamps
     if (st->codec->codec_id == AV_CODEC_ID_MPEG2VIDEO)
         st->need_parsing = AVSTREAM_PARSE_HEADERS;
     st->codec->codec_tag   = 0; /* no fourcc */
     st->codec->width       = video->width;
     st->codec->height      = video->height;
     st->duration           = st->nb_frames = video->nb_frames;
     if (video->time_base.num)
         avpriv_set_pts_info(st, 64, video->time_base.num, video->time_base.den);
     st->r_frame_rate       =
     st->avg_frame_rate     = av_inv_q(video->time_base);
     return 0;
 }
 
6e9651d1
 static int ea_read_header(AVFormatContext *s)
ad81a9fe
 {
e4141433
     EaDemuxContext *ea = s->priv_data;
ad81a9fe
     AVStream *st;
 
f37174bc
     if (process_ea_header(s)<=0)
6f3e0b21
         return AVERROR(EIO);
ad81a9fe
 
803bdc54
     if (init_video_stream(s, &ea->video) || init_video_stream(s, &ea->alpha))
a2517fca
         return AVERROR(ENOMEM);
ad81a9fe
 
c611463f
     if (ea->audio_codec) {
a9221e39
         if (ea->num_channels <= 0 || ea->num_channels > 2) {
8747fce9
             av_log(s, AV_LOG_WARNING,
                    "Unsupported number of channels: %d\n", ea->num_channels);
42396c2e
             ea->audio_codec = 0;
             return 1;
         }
         if (ea->sample_rate <= 0) {
8747fce9
             av_log(s, AV_LOG_ERROR,
                    "Unsupported sample rate: %d\n", ea->sample_rate);
42396c2e
             ea->audio_codec = 0;
             return 1;
         }
73b16198
         if (ea->bytes <= 0) {
8747fce9
             av_log(s, AV_LOG_ERROR,
                    "Invalid number of bytes per sample: %d\n", ea->bytes);
36ef5369
             ea->audio_codec = AV_CODEC_ID_NONE;
73b16198
             return 1;
         }
42396c2e
 
a2c9473d
         /* initialize the audio decoder stream */
3b3bbdd3
         st = avformat_new_stream(s, NULL);
a2c9473d
         if (!st)
             return AVERROR(ENOMEM);
c3f9ebf7
         avpriv_set_pts_info(st, 33, 1, ea->sample_rate);
8747fce9
         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;
dd1c8f3e
         st->codec->bits_per_coded_sample = ea->bytes * 8;
8747fce9
         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;
         ea->audio_stream_index           = st->index;
         st->start_time                   = 0;
c611463f
     }
ad81a9fe
 
     return 1;
 }
 
8747fce9
 static int ea_read_packet(AVFormatContext *s, AVPacket *pkt)
ad81a9fe
 {
     EaDemuxContext *ea = s->priv_data;
8747fce9
     AVIOContext *pb    = s->pb;
a0eb8de5
     int partial_packet = 0;
ad81a9fe
     unsigned int chunk_type, chunk_size;
8747fce9
     int ret = 0, packet_read = 0, key = 0;
b57ac115
     int av_uninit(num_samples);
ad81a9fe
 
a0eb8de5
     while (!packet_read || partial_packet) {
e63a3628
         chunk_type = avio_rl32(pb);
273e6af4
         chunk_size = ea->big_endian ? avio_rb32(pb) : avio_rl32(pb);
4d6c5152
         if (chunk_size < 8)
273e6af4
             return AVERROR_INVALIDDATA;
         chunk_size -= 8;
ad81a9fe
 
         switch (chunk_type) {
         /* audio data */
94e8c227
         case ISNh_TAG:
8747fce9
             /* header chunk also contains data; skip over the header portion */
273e6af4
             if (chunk_size < 32)
                 return AVERROR_INVALIDDATA;
45a8a02a
             avio_skip(pb, 32);
98168220
             chunk_size -= 32;
94e8c227
         case ISNd_TAG:
ad81a9fe
         case SCDl_TAG:
7bb65d89
         case SNDC_TAG:
ebc22cc2
         case SDEN_TAG:
018d0ff9
             if (!ea->audio_codec) {
45a8a02a
                 avio_skip(pb, chunk_size);
018d0ff9
                 break;
36ef5369
             } else if (ea->audio_codec == AV_CODEC_ID_PCM_S16LE_PLANAR ||
                        ea->audio_codec == AV_CODEC_ID_MP3) {
e63a3628
                 num_samples = avio_rl32(pb);
45a8a02a
                 avio_skip(pb, 8);
725d86bf
                 chunk_size -= 12;
0d7c0274
             } else if (ea->audio_codec == AV_CODEC_ID_ADPCM_PSX) {
                 avio_skip(pb, 8);
                 chunk_size -= 8;
018d0ff9
             }
350dd853
 
a0eb8de5
             if (partial_packet) {
a9b42487
                 avpriv_request_sample(s, "video header followed by audio packet");
c2f861ca
                 av_packet_unref(pkt);
a0eb8de5
                 partial_packet = 0;
             }
350dd853
 
4d6c5152
             if (!chunk_size)
                 continue;
 
2692067a
             ret = av_get_packet(pb, pkt, chunk_size);
f772b7fa
             if (ret < 0)
                 return ret;
46e97dc3
             pkt->stream_index = ea->audio_stream_index;
 
             switch (ea->audio_codec) {
36ef5369
             case AV_CODEC_ID_ADPCM_EA:
             case AV_CODEC_ID_ADPCM_EA_R1:
             case AV_CODEC_ID_ADPCM_EA_R2:
             case AV_CODEC_ID_ADPCM_IMA_EA_EACS:
             case AV_CODEC_ID_ADPCM_EA_R3:
f7e61695
                 if (pkt->size < 4) {
                     av_log(s, AV_LOG_ERROR, "Packet is too short\n");
ce70f28a
                     av_packet_unref(pkt);
f7e61695
                     return AVERROR_INVALIDDATA;
                 }
                 if (ea->audio_codec == AV_CODEC_ID_ADPCM_EA_R3)
fb1ea777
                     pkt->duration = AV_RB32(pkt->data);
f7e61695
                 else
                     pkt->duration = AV_RL32(pkt->data);
ea289186
                 break;
36ef5369
             case AV_CODEC_ID_ADPCM_IMA_EA_SEAD:
ea289186
                 pkt->duration = ret * 2 / ea->num_channels;
46e97dc3
                 break;
36ef5369
             case AV_CODEC_ID_PCM_S16LE_PLANAR:
             case AV_CODEC_ID_MP3:
ea289186
                 pkt->duration = num_samples;
46e97dc3
                 break;
0d7c0274
             case AV_CODEC_ID_ADPCM_PSX:
                 pkt->duration = chunk_size / (16 * ea->num_channels) * 28;
                 break;
46e97dc3
             default:
ea289186
                 pkt->duration = chunk_size / (ea->bytes * ea->num_channels);
46e97dc3
             }
ad81a9fe
 
             packet_read = 1;
             break;
 
         /* ending tag */
44626387
         case 0:
94e8c227
         case ISNe_TAG:
ad81a9fe
         case SCEl_TAG:
7bb65d89
         case SEND_TAG:
ebc22cc2
         case SEEN_TAG:
6debfce6
             while (!avio_feof(pb)) {
                 int tag = avio_rl32(pb);
 
                 if (tag == ISNh_TAG ||
                     tag == SCHl_TAG ||
                     tag == SEAD_TAG ||
                     tag == SHEN_TAG) {
                     avio_skip(pb, -4);
                     break;
                 }
             }
             if (avio_feof(pb))
                 ret = AVERROR_EOF;
ad81a9fe
             packet_read = 1;
             break;
 
b2f181c2
         case MVIh_TAG:
bbf020e9
         case kVGT_TAG:
d2299316
         case pQGT_TAG:
         case TGQs_TAG:
eacfe858
         case MADk_TAG:
cc947f04
             key = AV_PKT_FLAG_KEY;
b2f181c2
         case MVIf_TAG:
bbf020e9
         case fVGT_TAG:
eacfe858
         case MADm_TAG:
         case MADe_TAG:
8747fce9
             avio_seek(pb, -8, SEEK_CUR);    // include chunk preamble
b2f181c2
             chunk_size += 8;
             goto get_video_packet;
 
d9d9a8c0
         case mTCD_TAG:
4d6c5152
             if (chunk_size < 8)
                 return AVERROR_INVALIDDATA;
 
a90cff13
             avio_skip(pb, 8);               // skip ea DCT header
d9d9a8c0
             chunk_size -= 8;
             goto get_video_packet;
 
d3e412d0
         case MV0K_TAG:
803bdc54
         case AV0K_TAG:
d3302b70
         case MPCh_TAG:
468d298d
         case pIQT_TAG:
cc947f04
             key = AV_PKT_FLAG_KEY;
d3e412d0
         case MV0F_TAG:
803bdc54
         case AV0F_TAG:
b2f181c2
 get_video_packet:
4d6c5152
             if (!chunk_size)
                 continue;
 
a0eb8de5
             if (partial_packet) {
                 ret = av_append_packet(pb, pkt, chunk_size);
             } else
                 ret = av_get_packet(pb, pkt, chunk_size);
             if (ret < 0) {
                 packet_read = 1;
                 break;
             }
             partial_packet = chunk_type == MVIh_TAG;
803bdc54
             if (chunk_type == AV0K_TAG || chunk_type == AV0F_TAG)
                 pkt->stream_index = ea->alpha.stream_index;
             else
                 pkt->stream_index = ea->video.stream_index;
8747fce9
             pkt->flags       |= key;
             packet_read       = 1;
d3e412d0
             break;
 
ad81a9fe
         default:
45a8a02a
             avio_skip(pb, chunk_size);
ad81a9fe
             break;
         }
     }
 
a0eb8de5
     if (ret < 0 && partial_packet)
c2f861ca
         av_packet_unref(pkt);
ad81a9fe
     return ret;
 }
 
66355be3
 AVInputFormat ff_ea_demuxer = {
dfc2c4d9
     .name           = "ea",
6774247a
     .long_name      = NULL_IF_CONFIG_SMALL("Electronic Arts Multimedia"),
dfc2c4d9
     .priv_data_size = sizeof(EaDemuxContext),
     .read_probe     = ea_probe,
     .read_header    = ea_read_header,
     .read_packet    = ea_read_packet,
ad81a9fe
 };