FFmpeg
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
electronicarts.c
Go to the documentation of this file.
1 /* Electronic Arts Multimedia File Demuxer
2  * Copyright (c) 2004 The ffmpeg Project
3  * Copyright (c) 2006-2008 Peter Ross
4  *
5  * This file is part of FFmpeg.
6  *
7  * FFmpeg is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Lesser General Public
9  * License as published by the Free Software Foundation; either
10  * version 2.1 of the License, or (at your option) any later version.
11  *
12  * FFmpeg is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15  * Lesser General Public License for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public
18  * License along with FFmpeg; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20  */
21 
22 /**
23  * @file
24  * Electronic Arts Multimedia file demuxer (WVE/UV2/etc.)
25  * by Robin Kay (komadori at gekkou.co.uk)
26  */
27 
28 #include "libavutil/intreadwrite.h"
29 #include "avformat.h"
30 #include "internal.h"
31 
32 #define SCHl_TAG MKTAG('S', 'C', 'H', 'l')
33 #define SEAD_TAG MKTAG('S', 'E', 'A', 'D') /* Sxxx header */
34 #define SNDC_TAG MKTAG('S', 'N', 'D', 'C') /* Sxxx data */
35 #define SEND_TAG MKTAG('S', 'E', 'N', 'D') /* Sxxx end */
36 #define SHEN_TAG MKTAG('S', 'H', 'E', 'N') /* SxEN header */
37 #define SDEN_TAG MKTAG('S', 'D', 'E', 'N') /* SxEN data */
38 #define SEEN_TAG MKTAG('S', 'E', 'E', 'N') /* SxEN end */
39 #define ISNh_TAG MKTAG('1', 'S', 'N', 'h') /* 1SNx header */
40 #define EACS_TAG MKTAG('E', 'A', 'C', 'S')
41 #define ISNd_TAG MKTAG('1', 'S', 'N', 'd') /* 1SNx data */
42 #define ISNe_TAG MKTAG('1', 'S', 'N', 'e') /* 1SNx end */
43 #define PT00_TAG MKTAG('P', 'T', 0x0, 0x0)
44 #define GSTR_TAG MKTAG('G', 'S', 'T', 'R')
45 #define SCDl_TAG MKTAG('S', 'C', 'D', 'l')
46 #define SCEl_TAG MKTAG('S', 'C', 'E', 'l')
47 #define kVGT_TAG MKTAG('k', 'V', 'G', 'T') /* TGV i-frame */
48 #define fVGT_TAG MKTAG('f', 'V', 'G', 'T') /* TGV p-frame */
49 #define mTCD_TAG MKTAG('m', 'T', 'C', 'D') /* MDEC */
50 #define MADk_TAG MKTAG('M', 'A', 'D', 'k') /* MAD i-frame */
51 #define MADm_TAG MKTAG('M', 'A', 'D', 'm') /* MAD p-frame */
52 #define MADe_TAG MKTAG('M', 'A', 'D', 'e') /* MAD lqp-frame */
53 #define MPCh_TAG MKTAG('M', 'P', 'C', 'h') /* MPEG2 */
54 #define TGQs_TAG MKTAG('T', 'G', 'Q', 's') /* TGQ i-frame (appears in .TGQ files) */
55 #define pQGT_TAG MKTAG('p', 'Q', 'G', 'T') /* TGQ i-frame (appears in .UV files) */
56 #define pIQT_TAG MKTAG('p', 'I', 'Q', 'T') /* TQI/UV2 i-frame (.UV2/.WVE) */
57 #define MVhd_TAG MKTAG('M', 'V', 'h', 'd')
58 #define MV0K_TAG MKTAG('M', 'V', '0', 'K')
59 #define MV0F_TAG MKTAG('M', 'V', '0', 'F')
60 #define MVIh_TAG MKTAG('M', 'V', 'I', 'h') /* CMV header */
61 #define MVIf_TAG MKTAG('M', 'V', 'I', 'f') /* CMV i-frame */
62 
63 typedef struct EaDemuxContext {
65 
68  int width, height;
69  int nb_frames;
71 
74 
75  int bytes;
80 
81 static uint32_t read_arbitary(AVIOContext *pb) {
82  uint8_t size, byte;
83  int i;
84  uint32_t word;
85 
86  size = avio_r8(pb);
87 
88  word = 0;
89  for (i = 0; i < size; i++) {
90  byte = avio_r8(pb);
91  word <<= 8;
92  word |= byte;
93  }
94 
95  return word;
96 }
97 
98 /*
99  * Process PT/GSTR sound header
100  * return 1 if success, 0 if invalid format, otherwise AVERROR_xxx
101  */
103 {
104  int inHeader = 1;
105  EaDemuxContext *ea = s->priv_data;
106  AVIOContext *pb = s->pb;
107  int compression_type = -1, revision = -1, revision2 = -1;
108 
109  ea->bytes = 2;
110  ea->sample_rate = -1;
111  ea->num_channels = 1;
112 
113  while (!url_feof(pb) && inHeader) {
114  int inSubheader;
115  uint8_t byte;
116  byte = avio_r8(pb);
117 
118  switch (byte) {
119  case 0xFD:
120  av_log (s, AV_LOG_DEBUG, "entered audio subheader\n");
121  inSubheader = 1;
122  while (!url_feof(pb) && inSubheader) {
123  uint8_t subbyte;
124  subbyte = avio_r8(pb);
125 
126  switch (subbyte) {
127  case 0x80:
128  revision = read_arbitary(pb);
129  av_log (s, AV_LOG_DEBUG, "revision (element 0x80) set to 0x%08x\n", revision);
130  break;
131  case 0x82:
132  ea->num_channels = read_arbitary(pb);
133  av_log (s, AV_LOG_DEBUG, "num_channels (element 0x82) set to 0x%08x\n", ea->num_channels);
134  break;
135  case 0x83:
136  compression_type = read_arbitary(pb);
137  av_log (s, AV_LOG_DEBUG, "compression_type (element 0x83) set to 0x%08x\n", compression_type);
138  break;
139  case 0x84:
140  ea->sample_rate = read_arbitary(pb);
141  av_log (s, AV_LOG_DEBUG, "sample_rate (element 0x84) set to %i\n", ea->sample_rate);
142  break;
143  case 0x85:
144  ea->num_samples = read_arbitary(pb);
145  av_log (s, AV_LOG_DEBUG, "num_samples (element 0x85) set to 0x%08x\n", ea->num_samples);
146  break;
147  case 0x8A:
148  av_log (s, AV_LOG_DEBUG, "element 0x%02x set to 0x%08x\n", subbyte, read_arbitary(pb));
149  av_log (s, AV_LOG_DEBUG, "exited audio subheader\n");
150  inSubheader = 0;
151  break;
152  case 0xA0:
153  revision2 = read_arbitary(pb);
154  av_log (s, AV_LOG_DEBUG, "revision2 (element 0xA0) set to 0x%08x\n", revision2);
155  break;
156  case 0xFF:
157  av_log (s, AV_LOG_DEBUG, "end of header block reached (within audio subheader)\n");
158  inSubheader = 0;
159  inHeader = 0;
160  break;
161  default:
162  av_log (s, AV_LOG_DEBUG, "element 0x%02x set to 0x%08x\n", subbyte, read_arbitary(pb));
163  break;
164  }
165  }
166  break;
167  case 0xFF:
168  av_log (s, AV_LOG_DEBUG, "end of header block reached\n");
169  inHeader = 0;
170  break;
171  default:
172  av_log (s, AV_LOG_DEBUG, "header element 0x%02x set to 0x%08x\n", byte, read_arbitary(pb));
173  break;
174  }
175  }
176 
177  switch (compression_type) {
178  case 0: ea->audio_codec = AV_CODEC_ID_PCM_S16LE; break;
179  case 7: ea->audio_codec = AV_CODEC_ID_ADPCM_EA; break;
180  case -1:
181  switch (revision) {
182  case 1: ea->audio_codec = AV_CODEC_ID_ADPCM_EA_R1; break;
183  case 2: ea->audio_codec = AV_CODEC_ID_ADPCM_EA_R2; break;
184  case 3: ea->audio_codec = AV_CODEC_ID_ADPCM_EA_R3; break;
185  case -1: break;
186  default:
187  avpriv_request_sample(s, "stream type; revision=%i", revision);
188  return 0;
189  }
190  switch (revision2) {
191  case 8: ea->audio_codec = AV_CODEC_ID_PCM_S16LE_PLANAR; break;
192  case 10:
193  switch (revision) {
194  case -1:
195  case 2: ea->audio_codec = AV_CODEC_ID_ADPCM_EA_R1; break;
196  case 3: ea->audio_codec = AV_CODEC_ID_ADPCM_EA_R2; break;
197  default:
198  avpriv_request_sample(s, "stream type; revision=%i, revision2=%i", revision, revision2);
199  return 0;
200  }
201  break;
202  case 16: ea->audio_codec = AV_CODEC_ID_MP3; break;
203  case -1: break;
204  default:
206  avpriv_request_sample(s, "stream type; revision2=%i", revision2);
207  return 0;
208  }
209  break;
210  default:
211  avpriv_request_sample(s, "stream type; compression_type=%i", compression_type);
212  return 0;
213  }
214 
215  if (ea->sample_rate == -1)
216  ea->sample_rate = revision==3 ? 48000 : 22050;
217 
218  return 1;
219 }
220 
221 /*
222  * Process EACS sound header
223  * return 1 if success, 0 if invalid format, otherwise AVERROR_xxx
224  */
226 {
227  EaDemuxContext *ea = s->priv_data;
228  AVIOContext *pb = s->pb;
229  int compression_type;
230 
231  ea->sample_rate = ea->big_endian ? avio_rb32(pb) : avio_rl32(pb);
232  ea->bytes = avio_r8(pb); /* 1=8-bit, 2=16-bit */
233  ea->num_channels = avio_r8(pb);
234  compression_type = avio_r8(pb);
235  avio_skip(pb, 13);
236 
237  switch (compression_type) {
238  case 0:
239  switch (ea->bytes) {
240  case 1: ea->audio_codec = AV_CODEC_ID_PCM_S8; break;
241  case 2: ea->audio_codec = AV_CODEC_ID_PCM_S16LE; break;
242  }
243  break;
244  case 1: ea->audio_codec = AV_CODEC_ID_PCM_MULAW; ea->bytes = 1; break;
245  case 2: ea->audio_codec = AV_CODEC_ID_ADPCM_IMA_EA_EACS; break;
246  default:
247  avpriv_request_sample(s, "stream type; audio compression_type=%i", compression_type);
248  }
249 
250  return 1;
251 }
252 
253 /*
254  * Process SEAD sound header
255  * return 1 if success, 0 if invalid format, otherwise AVERROR_xxx
256  */
258 {
259  EaDemuxContext *ea = s->priv_data;
260  AVIOContext *pb = s->pb;
261 
262  ea->sample_rate = avio_rl32(pb);
263  ea->bytes = avio_rl32(pb); /* 1=8-bit, 2=16-bit */
264  ea->num_channels = avio_rl32(pb);
266 
267  return 1;
268 }
269 
271 {
272  EaDemuxContext *ea = s->priv_data;
273  AVIOContext *pb = s->pb;
274  avio_skip(pb, 4);
275  ea->width = avio_rl16(pb);
276  ea->height = avio_rl16(pb);
277  ea->time_base = (AVRational){1,15};
279  return 1;
280 }
281 
283 {
284  EaDemuxContext *ea = s->priv_data;
285  AVIOContext *pb = s->pb;
286 
287  avio_skip(pb, 8);
288  ea->nb_frames = avio_rl32(pb);
289  avio_skip(pb, 4);
290  ea->time_base.den = avio_rl32(pb);
291  ea->time_base.num = avio_rl32(pb);
292  if (ea->time_base.den <= 0 || ea->time_base.num <= 0) {
293  av_log(s, AV_LOG_ERROR, "Timebase is invalid\n");
294  return AVERROR_INVALIDDATA;
295  }
297 
298  return 1;
299 }
300 
302 {
303  EaDemuxContext *ea = s->priv_data;
304  int fps;
305 
306  avio_skip(s->pb, 10);
307  fps = avio_rl16(s->pb);
308  if (fps)
309  ea->time_base = (AVRational){1, fps};
311 
312  return 0;
313 }
314 
315 /*
316  * Process EA file header
317  * Returns 1 if the EA file is valid and successfully opened, 0 otherwise
318  */
320  uint32_t blockid, size = 0;
321  EaDemuxContext *ea = s->priv_data;
322  AVIOContext *pb = s->pb;
323  int i;
324 
325  for (i=0; i<5 && (!ea->audio_codec || !ea->video_codec); i++) {
326  unsigned int startpos = avio_tell(pb);
327  int err = 0;
328 
329  blockid = avio_rl32(pb);
330  size = avio_rl32(pb);
331  if (i == 0)
332  ea->big_endian = size > 0x000FFFFF;
333  if (ea->big_endian)
334  size = av_bswap32(size);
335 
336  switch (blockid) {
337  case ISNh_TAG:
338  if (avio_rl32(pb) != EACS_TAG) {
339  avpriv_request_sample(s, "unknown 1SNh headerid");
340  return 0;
341  }
342  err = process_audio_header_eacs(s);
343  break;
344 
345  case SCHl_TAG :
346  case SHEN_TAG :
347  blockid = avio_rl32(pb);
348  if (blockid == GSTR_TAG) {
349  avio_skip(pb, 4);
350  } else if ((blockid & 0xFFFF)!=PT00_TAG) {
351  avpriv_request_sample(s, "unknown SCHl headerid");
352  return 0;
353  }
355  break;
356 
357  case SEAD_TAG:
358  err = process_audio_header_sead(s);
359  break;
360 
361  case MVIh_TAG :
362  err = process_video_header_cmv(s);
363  break;
364 
365  case kVGT_TAG:
367  break;
368 
369  case mTCD_TAG :
370  err = process_video_header_mdec(s);
371  break;
372 
373  case MPCh_TAG:
375  break;
376 
377  case pQGT_TAG:
378  case TGQs_TAG:
380  break;
381 
382  case pIQT_TAG:
384  break;
385 
386  case MADk_TAG :
388  break;
389 
390  case MVhd_TAG :
391  err = process_video_header_vp6(s);
392  break;
393  }
394 
395  if (err < 0) {
396  av_log(s, AV_LOG_ERROR, "error parsing header: %i\n", err);
397  return err;
398  }
399 
400  avio_seek(pb, startpos + size, SEEK_SET);
401  }
402 
403  avio_seek(pb, 0, SEEK_SET);
404 
405  return 1;
406 }
407 
408 
409 static int ea_probe(AVProbeData *p)
410 {
411  switch (AV_RL32(&p->buf[0])) {
412  case ISNh_TAG:
413  case SCHl_TAG:
414  case SEAD_TAG:
415  case SHEN_TAG:
416  case kVGT_TAG:
417  case MADk_TAG:
418  case MPCh_TAG:
419  case MVhd_TAG:
420  case MVIh_TAG:
421  break;
422  default:
423  return 0;
424  }
425  if (AV_RL32(&p->buf[4]) > 0xfffff && AV_RB32(&p->buf[4]) > 0xfffff)
426  return 0;
427  return AVPROBE_SCORE_MAX;
428 }
429 
431 {
432  EaDemuxContext *ea = s->priv_data;
433  AVStream *st;
434 
435  if (process_ea_header(s)<=0)
436  return AVERROR(EIO);
437 
438  if (ea->video_codec) {
439  /* initialize the video decoder stream */
440  st = avformat_new_stream(s, NULL);
441  if (!st)
442  return AVERROR(ENOMEM);
443  ea->video_stream_index = st->index;
445  st->codec->codec_id = ea->video_codec;
446  // parsing is necessary to make FFmpeg generate correct timestamps
449  st->codec->codec_tag = 0; /* no fourcc */
450  st->codec->width = ea->width;
451  st->codec->height = ea->height;
452  st->duration = st->nb_frames = ea->nb_frames;
453  if (ea->time_base.num)
454  avpriv_set_pts_info(st, 64, ea->time_base.num, ea->time_base.den);
455  st->r_frame_rate =
456  st->avg_frame_rate = av_inv_q(ea->time_base);
457  }
458 
459  if (ea->audio_codec) {
460  if (ea->num_channels <= 0) {
461  av_log(s, AV_LOG_WARNING, "Unsupported number of channels: %d\n", ea->num_channels);
462  ea->audio_codec = 0;
463  return 1;
464  }
465  if (ea->sample_rate <= 0) {
466  av_log(s, AV_LOG_ERROR, "Unsupported sample rate: %d\n", ea->sample_rate);
467  ea->audio_codec = 0;
468  return 1;
469  }
470  if (ea->bytes <= 0) {
471  av_log(s, AV_LOG_ERROR, "Invalid number of bytes per sample: %d\n", ea->bytes);
473  return 1;
474  }
475 
476  /* initialize the audio decoder stream */
477  st = avformat_new_stream(s, NULL);
478  if (!st)
479  return AVERROR(ENOMEM);
480  avpriv_set_pts_info(st, 33, 1, ea->sample_rate);
482  st->codec->codec_id = ea->audio_codec;
483  st->codec->codec_tag = 0; /* no tag */
484  st->codec->channels = ea->num_channels;
485  st->codec->sample_rate = ea->sample_rate;
486  st->codec->bits_per_coded_sample = ea->bytes * 8;
487  st->codec->bit_rate = st->codec->channels * st->codec->sample_rate *
488  st->codec->bits_per_coded_sample / 4;
490  ea->audio_stream_index = st->index;
491  st->start_time = 0;
492  }
493 
494  return 1;
495 }
496 
498  AVPacket *pkt)
499 {
500  EaDemuxContext *ea = s->priv_data;
501  AVIOContext *pb = s->pb;
502  int ret = 0;
503  int packet_read = 0;
504  int partial_packet = 0;
505  unsigned int chunk_type, chunk_size;
506  int key = 0;
507  int av_uninit(num_samples);
508 
509  while (!packet_read || partial_packet) {
510  chunk_type = avio_rl32(pb);
511  chunk_size = ea->big_endian ? avio_rb32(pb) : avio_rl32(pb);
512  if (chunk_size <= 8)
513  return AVERROR_INVALIDDATA;
514  chunk_size -= 8;
515 
516  switch (chunk_type) {
517  /* audio data */
518  case ISNh_TAG:
519  /* header chunk also contains data; skip over the header portion*/
520  if (chunk_size < 32)
521  return AVERROR_INVALIDDATA;
522  avio_skip(pb, 32);
523  chunk_size -= 32;
524  case ISNd_TAG:
525  case SCDl_TAG:
526  case SNDC_TAG:
527  case SDEN_TAG:
528  if (!ea->audio_codec) {
529  avio_skip(pb, chunk_size);
530  break;
531  } else if (ea->audio_codec == AV_CODEC_ID_PCM_S16LE_PLANAR ||
532  ea->audio_codec == AV_CODEC_ID_MP3) {
533  num_samples = avio_rl32(pb);
534  avio_skip(pb, 8);
535  chunk_size -= 12;
536  }
537  if (partial_packet) {
538  avpriv_request_sample(s, "video header followed by audio packet");
539  av_free_packet(pkt);
540  partial_packet = 0;
541  }
542  ret = av_get_packet(pb, pkt, chunk_size);
543  if (ret < 0)
544  return ret;
545  pkt->stream_index = ea->audio_stream_index;
546 
547  switch (ea->audio_codec) {
552  if (pkt->size >= 4)
553  pkt->duration = AV_RL32(pkt->data);
554  break;
556  if (pkt->size >= 4)
557  pkt->duration = AV_RB32(pkt->data);
558  break;
560  pkt->duration = ret * 2 / ea->num_channels;
561  break;
563  case AV_CODEC_ID_MP3:
564  pkt->duration = num_samples;
565  break;
566  default:
567  pkt->duration = chunk_size / (ea->bytes * ea->num_channels);
568  }
569 
570  packet_read = 1;
571  break;
572 
573  /* ending tag */
574  case 0:
575  case ISNe_TAG:
576  case SCEl_TAG:
577  case SEND_TAG:
578  case SEEN_TAG:
579  ret = AVERROR(EIO);
580  packet_read = 1;
581  break;
582 
583  case MVIh_TAG:
584  case kVGT_TAG:
585  case pQGT_TAG:
586  case TGQs_TAG:
587  case MADk_TAG:
588  key = AV_PKT_FLAG_KEY;
589  case MVIf_TAG:
590  case fVGT_TAG:
591  case MADm_TAG:
592  case MADe_TAG:
593  avio_seek(pb, -8, SEEK_CUR); // include chunk preamble
594  chunk_size += 8;
595  goto get_video_packet;
596 
597  case mTCD_TAG:
598  avio_skip(pb, 8); // skip ea dct header
599  chunk_size -= 8;
600  goto get_video_packet;
601 
602  case MV0K_TAG:
603  case MPCh_TAG:
604  case pIQT_TAG:
605  key = AV_PKT_FLAG_KEY;
606  case MV0F_TAG:
607 get_video_packet:
608  if (partial_packet) {
609  ret = av_append_packet(pb, pkt, chunk_size);
610  } else
611  ret = av_get_packet(pb, pkt, chunk_size);
612  if (ret < 0) {
613  packet_read = 1;
614  break;
615  }
616  partial_packet = chunk_type == MVIh_TAG;
617  pkt->stream_index = ea->video_stream_index;
618  pkt->flags |= key;
619  packet_read = 1;
620  break;
621 
622  default:
623  avio_skip(pb, chunk_size);
624  break;
625  }
626  }
627 
628  if (ret < 0 && partial_packet)
629  av_free_packet(pkt);
630  return ret;
631 }
632 
634  .name = "ea",
635  .long_name = NULL_IF_CONFIG_SMALL("Electronic Arts Multimedia"),
636  .priv_data_size = sizeof(EaDemuxContext),
637  .read_probe = ea_probe,
640 };