FFmpeg
 All Data Structures 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  av_log_ask_for_sample(s, "unsupported stream type; revision=%i\n", 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  av_log_ask_for_sample(s, "unsupported stream type; revision=%i, revision2=%i\n", 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  av_log_ask_for_sample(s, "unsupported stream type; revision2=%i\n", revision2);
207  return 0;
208  }
209  break;
210  default:
211  av_log_ask_for_sample(s, "unsupported stream type; compression_type=%i\n", 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  av_log_ask_for_sample(s, "unsupported stream type; audio compression_type=%i\n", 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);
293 
294  return 1;
295 }
296 
298 {
299  EaDemuxContext *ea = s->priv_data;
300  int fps;
301 
302  avio_skip(s->pb, 10);
303  fps = avio_rl16(s->pb);
304  if (fps)
305  ea->time_base = (AVRational){1, fps};
307 
308  return 0;
309 }
310 
311 /*
312  * Process EA file header
313  * Returns 1 if the EA file is valid and successfully opened, 0 otherwise
314  */
316  uint32_t blockid, size = 0;
317  EaDemuxContext *ea = s->priv_data;
318  AVIOContext *pb = s->pb;
319  int i;
320 
321  for (i=0; i<5 && (!ea->audio_codec || !ea->video_codec); i++) {
322  unsigned int startpos = avio_tell(pb);
323  int err = 0;
324 
325  blockid = avio_rl32(pb);
326  size = avio_rl32(pb);
327  if (i == 0)
328  ea->big_endian = size > 0x000FFFFF;
329  if (ea->big_endian)
330  size = av_bswap32(size);
331 
332  switch (blockid) {
333  case ISNh_TAG:
334  if (avio_rl32(pb) != EACS_TAG) {
335  av_log_ask_for_sample(s, "unknown 1SNh headerid\n");
336  return 0;
337  }
338  err = process_audio_header_eacs(s);
339  break;
340 
341  case SCHl_TAG :
342  case SHEN_TAG :
343  blockid = avio_rl32(pb);
344  if (blockid == GSTR_TAG) {
345  avio_skip(pb, 4);
346  } else if ((blockid & 0xFFFF)!=PT00_TAG) {
347  av_log_ask_for_sample(s, "unknown SCHl headerid\n");
348  return 0;
349  }
351  break;
352 
353  case SEAD_TAG:
354  err = process_audio_header_sead(s);
355  break;
356 
357  case MVIh_TAG :
358  err = process_video_header_cmv(s);
359  break;
360 
361  case kVGT_TAG:
363  break;
364 
365  case mTCD_TAG :
366  err = process_video_header_mdec(s);
367  break;
368 
369  case MPCh_TAG:
371  break;
372 
373  case pQGT_TAG:
374  case TGQs_TAG:
376  break;
377 
378  case pIQT_TAG:
380  break;
381 
382  case MADk_TAG :
384  break;
385 
386  case MVhd_TAG :
387  err = process_video_header_vp6(s);
388  break;
389  }
390 
391  if (err < 0) {
392  av_log(s, AV_LOG_ERROR, "error parsing header: %i\n", err);
393  return err;
394  }
395 
396  avio_seek(pb, startpos + size, SEEK_SET);
397  }
398 
399  avio_seek(pb, 0, SEEK_SET);
400 
401  return 1;
402 }
403 
404 
405 static int ea_probe(AVProbeData *p)
406 {
407  switch (AV_RL32(&p->buf[0])) {
408  case ISNh_TAG:
409  case SCHl_TAG:
410  case SEAD_TAG:
411  case SHEN_TAG:
412  case kVGT_TAG:
413  case MADk_TAG:
414  case MPCh_TAG:
415  case MVhd_TAG:
416  case MVIh_TAG:
417  break;
418  default:
419  return 0;
420  }
421  if (AV_RL32(&p->buf[4]) > 0xfffff && AV_RB32(&p->buf[4]) > 0xfffff)
422  return 0;
423  return AVPROBE_SCORE_MAX;
424 }
425 
427 {
428  EaDemuxContext *ea = s->priv_data;
429  AVStream *st;
430 
431  if (process_ea_header(s)<=0)
432  return AVERROR(EIO);
433 
434  if (ea->video_codec) {
435  /* initialize the video decoder stream */
436  st = avformat_new_stream(s, NULL);
437  if (!st)
438  return AVERROR(ENOMEM);
439  ea->video_stream_index = st->index;
441  st->codec->codec_id = ea->video_codec;
442  // parsing is necessary to make FFmpeg generate correct timestamps
445  st->codec->codec_tag = 0; /* no fourcc */
446  st->codec->width = ea->width;
447  st->codec->height = ea->height;
448  st->duration = st->nb_frames = ea->nb_frames;
449  if (ea->time_base.num)
450  avpriv_set_pts_info(st, 64, ea->time_base.num, ea->time_base.den);
451  st->r_frame_rate =
452  st->avg_frame_rate = av_inv_q(ea->time_base);
453  }
454 
455  if (ea->audio_codec) {
456  if (ea->num_channels <= 0) {
457  av_log(s, AV_LOG_WARNING, "Unsupported number of channels: %d\n", ea->num_channels);
458  ea->audio_codec = 0;
459  return 1;
460  }
461  if (ea->sample_rate <= 0) {
462  av_log(s, AV_LOG_ERROR, "Unsupported sample rate: %d\n", ea->sample_rate);
463  ea->audio_codec = 0;
464  return 1;
465  }
466  if (ea->bytes <= 0) {
467  av_log(s, AV_LOG_ERROR, "Invalid number of bytes per sample: %d\n", ea->bytes);
469  return 1;
470  }
471 
472  /* initialize the audio decoder stream */
473  st = avformat_new_stream(s, NULL);
474  if (!st)
475  return AVERROR(ENOMEM);
476  avpriv_set_pts_info(st, 33, 1, ea->sample_rate);
478  st->codec->codec_id = ea->audio_codec;
479  st->codec->codec_tag = 0; /* no tag */
480  st->codec->channels = ea->num_channels;
481  st->codec->sample_rate = ea->sample_rate;
482  st->codec->bits_per_coded_sample = ea->bytes * 8;
483  st->codec->bit_rate = st->codec->channels * st->codec->sample_rate *
484  st->codec->bits_per_coded_sample / 4;
486  ea->audio_stream_index = st->index;
487  st->start_time = 0;
488  }
489 
490  return 1;
491 }
492 
494  AVPacket *pkt)
495 {
496  EaDemuxContext *ea = s->priv_data;
497  AVIOContext *pb = s->pb;
498  int ret = 0;
499  int packet_read = 0;
500  int partial_packet = 0;
501  unsigned int chunk_type, chunk_size;
502  int key = 0;
503  int av_uninit(num_samples);
504 
505  while (!packet_read || partial_packet) {
506  chunk_type = avio_rl32(pb);
507  chunk_size = ea->big_endian ? avio_rb32(pb) : avio_rl32(pb);
508  if (chunk_size <= 8)
509  return AVERROR_INVALIDDATA;
510  chunk_size -= 8;
511 
512  switch (chunk_type) {
513  /* audio data */
514  case ISNh_TAG:
515  /* header chunk also contains data; skip over the header portion*/
516  if (chunk_size < 32)
517  return AVERROR_INVALIDDATA;
518  avio_skip(pb, 32);
519  chunk_size -= 32;
520  case ISNd_TAG:
521  case SCDl_TAG:
522  case SNDC_TAG:
523  case SDEN_TAG:
524  if (!ea->audio_codec) {
525  avio_skip(pb, chunk_size);
526  break;
527  } else if (ea->audio_codec == AV_CODEC_ID_PCM_S16LE_PLANAR ||
528  ea->audio_codec == AV_CODEC_ID_MP3) {
529  num_samples = avio_rl32(pb);
530  avio_skip(pb, 8);
531  chunk_size -= 12;
532  }
533  if (partial_packet) {
534  av_log_ask_for_sample(s, "video header followed by audio packet not supported.\n");
535  av_free_packet(pkt);
536  partial_packet = 0;
537  }
538  ret = av_get_packet(pb, pkt, chunk_size);
539  if (ret < 0)
540  return ret;
541  pkt->stream_index = ea->audio_stream_index;
542 
543  switch (ea->audio_codec) {
548  if (pkt->size >= 4)
549  pkt->duration = AV_RL32(pkt->data);
550  break;
552  if (pkt->size >= 4)
553  pkt->duration = AV_RB32(pkt->data);
554  break;
556  pkt->duration = ret * 2 / ea->num_channels;
557  break;
559  case AV_CODEC_ID_MP3:
560  pkt->duration = num_samples;
561  break;
562  default:
563  pkt->duration = chunk_size / (ea->bytes * ea->num_channels);
564  }
565 
566  packet_read = 1;
567  break;
568 
569  /* ending tag */
570  case 0:
571  case ISNe_TAG:
572  case SCEl_TAG:
573  case SEND_TAG:
574  case SEEN_TAG:
575  ret = AVERROR(EIO);
576  packet_read = 1;
577  break;
578 
579  case MVIh_TAG:
580  case kVGT_TAG:
581  case pQGT_TAG:
582  case TGQs_TAG:
583  case MADk_TAG:
584  key = AV_PKT_FLAG_KEY;
585  case MVIf_TAG:
586  case fVGT_TAG:
587  case MADm_TAG:
588  case MADe_TAG:
589  avio_seek(pb, -8, SEEK_CUR); // include chunk preamble
590  chunk_size += 8;
591  goto get_video_packet;
592 
593  case mTCD_TAG:
594  avio_skip(pb, 8); // skip ea dct header
595  chunk_size -= 8;
596  goto get_video_packet;
597 
598  case MV0K_TAG:
599  case MPCh_TAG:
600  case pIQT_TAG:
601  key = AV_PKT_FLAG_KEY;
602  case MV0F_TAG:
603 get_video_packet:
604  if (partial_packet) {
605  ret = av_append_packet(pb, pkt, chunk_size);
606  } else
607  ret = av_get_packet(pb, pkt, chunk_size);
608  if (ret < 0) {
609  packet_read = 1;
610  break;
611  }
612  partial_packet = chunk_type == MVIh_TAG;
613  pkt->stream_index = ea->video_stream_index;
614  pkt->flags |= key;
615  packet_read = 1;
616  break;
617 
618  default:
619  avio_skip(pb, chunk_size);
620  break;
621  }
622  }
623 
624  if (ret < 0 && partial_packet)
625  av_free_packet(pkt);
626  return ret;
627 }
628 
630  .name = "ea",
631  .long_name = NULL_IF_CONFIG_SMALL("Electronic Arts Multimedia"),
632  .priv_data_size = sizeof(EaDemuxContext),
633  .read_probe = ea_probe,
636 };