]> git.sesse.net Git - ffmpeg/blobdiff - libavformat/electronicarts.c
set bytes per sample in the context
[ffmpeg] / libavformat / electronicarts.c
index e2100547fc160c41437b786d8b81721d07dfcfe2..02099545f404058a2f8383b2c5af694afa54787d 100644 (file)
 #define MV0K_TAG MKTAG('M', 'V', '0', 'K')
 #define MV0F_TAG MKTAG('M', 'V', '0', 'F')
 
-#define EA_SAMPLE_RATE 22050
-#define EA_BITS_PER_SAMPLE 16
-#define EA_PREAMBLE_SIZE 8
-
 typedef struct EaDemuxContext {
     int big_endian;
 
@@ -52,6 +48,8 @@ typedef struct EaDemuxContext {
 
     int64_t audio_pts;
 
+    int bytes;
+    int sample_rate;
     int num_channels;
     int num_samples;
 } EaDemuxContext;
@@ -82,8 +80,10 @@ static int process_audio_header_elements(AVFormatContext *s)
     int inHeader = 1;
     EaDemuxContext *ea = s->priv_data;
     ByteIOContext *pb = &s->pb;
-    int compression_type = -1;
+    int compression_type = -1, revision = -1;
 
+    ea->bytes = 2;
+    ea->sample_rate = -1;
     ea->num_channels = 1;
 
     while (inHeader) {
@@ -100,6 +100,10 @@ static int process_audio_header_elements(AVFormatContext *s)
                 subbyte = get_byte(pb);
 
                 switch (subbyte) {
+                case 0x80:
+                    revision = read_arbitary(pb);
+                    av_log (s, AV_LOG_INFO, "revision (element 0x80) set to 0x%08x\n", revision);
+                    break;
                 case 0x82:
                     ea->num_channels = read_arbitary(pb);
                     av_log (s, AV_LOG_INFO, "num_channels (element 0x82) set to 0x%08x\n", ea->num_channels);
@@ -108,6 +112,10 @@ static int process_audio_header_elements(AVFormatContext *s)
                     compression_type = read_arbitary(pb);
                     av_log (s, AV_LOG_INFO, "compression_type (element 0x83) set to 0x%08x\n", compression_type);
                     break;
+                case 0x84:
+                    ea->sample_rate = read_arbitary(pb);
+                    av_log (s, AV_LOG_INFO, "sample_rate (element 0x84) set to %i\n", ea->sample_rate);
+                    break;
                 case 0x85:
                     ea->num_samples = read_arbitary(pb);
                     av_log (s, AV_LOG_INFO, "num_samples (element 0x85) set to 0x%08x\n", ea->num_samples);
@@ -139,12 +147,16 @@ static int process_audio_header_elements(AVFormatContext *s)
     }
 
     switch (compression_type) {
+    case  0: ea->audio_codec = CODEC_ID_PCM_S16LE; break;
     case  7: ea->audio_codec = CODEC_ID_ADPCM_EA; break;
     default:
         av_log(s, AV_LOG_ERROR, "unsupported stream type; compression_type=%i\n", compression_type);
         return 0;
     }
 
+    if (ea->sample_rate == -1)
+        ea->sample_rate = revision==3 ? 48000 : 22050;
+
     return 1;
 }
 
@@ -249,13 +261,13 @@ static int ea_read_header(AVFormatContext *s,
     st = av_new_stream(s, 0);
     if (!st)
         return AVERROR(ENOMEM);
-    av_set_pts_info(st, 33, 1, EA_SAMPLE_RATE);
+    av_set_pts_info(st, 33, 1, ea->sample_rate);
     st->codec->codec_type = CODEC_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_sample = EA_BITS_PER_SAMPLE;
+    st->codec->sample_rate = ea->sample_rate;
+    st->codec->bits_per_sample = ea->bytes * 8;
     st->codec->bit_rate = st->codec->channels * st->codec->sample_rate *
         st->codec->bits_per_sample / 4;
     st->codec->block_align = st->codec->channels * st->codec->bits_per_sample;
@@ -273,16 +285,12 @@ static int ea_read_packet(AVFormatContext *s,
     ByteIOContext *pb = &s->pb;
     int ret = 0;
     int packet_read = 0;
-    unsigned char preamble[EA_PREAMBLE_SIZE];
     unsigned int chunk_type, chunk_size;
     int key = 0;
 
     while (!packet_read) {
-
-        if (get_buffer(pb, preamble, EA_PREAMBLE_SIZE) != EA_PREAMBLE_SIZE)
-            return AVERROR(EIO);
-        chunk_type = AV_RL32(&preamble[0]);
-        chunk_size = AV_RL32(&preamble[4]) - EA_PREAMBLE_SIZE;
+        chunk_type = get_le32(pb);
+        chunk_size = (ea->big_endian ? get_be32(pb) : get_le32(pb)) - 8;
 
         switch (chunk_type) {
         /* audio data */
@@ -294,7 +302,7 @@ static int ea_read_packet(AVFormatContext *s,
                     pkt->stream_index = ea->audio_stream_index;
                     pkt->pts = 90000;
                     pkt->pts *= ea->audio_frame_counter;
-                    pkt->pts /= EA_SAMPLE_RATE;
+                    pkt->pts /= ea->sample_rate;
 
                     /* 2 samples/byte, 1 or 2 samples per frame depending
                      * on stereo; chunk also has 12-byte header */