You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

754 lines
23KB

  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. * @file
  23. * Electronic Arts Multimedia file demuxer (WVE/UV2/etc.)
  24. * by Robin Kay (komadori at gekkou.co.uk)
  25. */
  26. #include <inttypes.h>
  27. #include "libavutil/intreadwrite.h"
  28. #include "avformat.h"
  29. #include "internal.h"
  30. #define SCHl_TAG MKTAG('S', 'C', 'H', 'l')
  31. #define SEAD_TAG MKTAG('S', 'E', 'A', 'D') /* Sxxx header */
  32. #define SNDC_TAG MKTAG('S', 'N', 'D', 'C') /* Sxxx data */
  33. #define SEND_TAG MKTAG('S', 'E', 'N', 'D') /* Sxxx end */
  34. #define SHEN_TAG MKTAG('S', 'H', 'E', 'N') /* SxEN header */
  35. #define SDEN_TAG MKTAG('S', 'D', 'E', 'N') /* SxEN data */
  36. #define SEEN_TAG MKTAG('S', 'E', 'E', 'N') /* SxEN end */
  37. #define ISNh_TAG MKTAG('1', 'S', 'N', 'h') /* 1SNx header */
  38. #define EACS_TAG MKTAG('E', 'A', 'C', 'S')
  39. #define ISNd_TAG MKTAG('1', 'S', 'N', 'd') /* 1SNx data */
  40. #define ISNe_TAG MKTAG('1', 'S', 'N', 'e') /* 1SNx end */
  41. #define PT00_TAG MKTAG('P', 'T', 0x0, 0x0)
  42. #define GSTR_TAG MKTAG('G', 'S', 'T', 'R')
  43. #define SCDl_TAG MKTAG('S', 'C', 'D', 'l')
  44. #define SCEl_TAG MKTAG('S', 'C', 'E', 'l')
  45. #define kVGT_TAG MKTAG('k', 'V', 'G', 'T') /* TGV I-frame */
  46. #define fVGT_TAG MKTAG('f', 'V', 'G', 'T') /* TGV P-frame */
  47. #define mTCD_TAG MKTAG('m', 'T', 'C', 'D') /* MDEC */
  48. #define MADk_TAG MKTAG('M', 'A', 'D', 'k') /* MAD I-frame */
  49. #define MADm_TAG MKTAG('M', 'A', 'D', 'm') /* MAD P-frame */
  50. #define MADe_TAG MKTAG('M', 'A', 'D', 'e') /* MAD lqp-frame */
  51. #define MPCh_TAG MKTAG('M', 'P', 'C', 'h') /* MPEG-2 */
  52. #define TGQs_TAG MKTAG('T', 'G', 'Q', 's') /* TGQ I-frame (appears in .TGQ files) */
  53. #define pQGT_TAG MKTAG('p', 'Q', 'G', 'T') /* TGQ I-frame (appears in .UV files) */
  54. #define pIQT_TAG MKTAG('p', 'I', 'Q', 'T') /* TQI/UV2 I-frame (.UV2/.WVE) */
  55. #define MVhd_TAG MKTAG('M', 'V', 'h', 'd')
  56. #define MV0K_TAG MKTAG('M', 'V', '0', 'K')
  57. #define MV0F_TAG MKTAG('M', 'V', '0', 'F')
  58. #define AVhd_TAG MKTAG('A', 'V', 'h', 'd')
  59. #define AV0K_TAG MKTAG('A', 'V', '0', 'K')
  60. #define AV0F_TAG MKTAG('A', 'V', '0', 'F')
  61. #define MVIh_TAG MKTAG('M', 'V', 'I', 'h') /* CMV header */
  62. #define MVIf_TAG MKTAG('M', 'V', 'I', 'f') /* CMV I-frame */
  63. #define AVP6_TAG MKTAG('A', 'V', 'P', '6')
  64. typedef struct VideoProperties {
  65. enum AVCodecID codec;
  66. AVRational time_base;
  67. int width, height;
  68. int nb_frames;
  69. int stream_index;
  70. } VideoProperties;
  71. typedef struct EaDemuxContext {
  72. int big_endian;
  73. VideoProperties video, alpha;
  74. enum AVCodecID audio_codec;
  75. int audio_stream_index;
  76. int bytes;
  77. int sample_rate;
  78. int num_channels;
  79. int num_samples;
  80. int platform;
  81. } EaDemuxContext;
  82. static uint32_t read_arbitrary(AVIOContext *pb)
  83. {
  84. uint8_t size, byte;
  85. int i;
  86. uint32_t word;
  87. size = avio_r8(pb);
  88. word = 0;
  89. for (i = 0; i < size; i++) {
  90. byte = avio_r8(pb);
  91. word <<= 8;
  92. word |= byte;
  93. }
  94. return word;
  95. }
  96. static int process_audio_header_elements(AVFormatContext *s)
  97. {
  98. EaDemuxContext *ea = s->priv_data;
  99. AVIOContext *pb = s->pb;
  100. int in_header = 1;
  101. int compression_type = -1, revision = -1, revision2 = -1;
  102. ea->bytes = 2;
  103. ea->sample_rate = -1;
  104. ea->num_channels = 1;
  105. while (!avio_feof(pb) && in_header) {
  106. int in_subheader;
  107. uint8_t byte;
  108. byte = avio_r8(pb);
  109. switch (byte) {
  110. case 0xFD:
  111. av_log(s, AV_LOG_DEBUG, "entered audio subheader\n");
  112. in_subheader = 1;
  113. while (!avio_feof(pb) && in_subheader) {
  114. uint8_t subbyte;
  115. subbyte = avio_r8(pb);
  116. switch (subbyte) {
  117. case 0x80:
  118. revision = read_arbitrary(pb);
  119. av_log(s, AV_LOG_DEBUG,
  120. "revision (element 0x80) set to 0x%08x\n", revision);
  121. break;
  122. case 0x82:
  123. ea->num_channels = read_arbitrary(pb);
  124. av_log(s, AV_LOG_DEBUG,
  125. "num_channels (element 0x82) set to 0x%08x\n",
  126. ea->num_channels);
  127. break;
  128. case 0x83:
  129. compression_type = read_arbitrary(pb);
  130. av_log(s, AV_LOG_DEBUG,
  131. "compression_type (element 0x83) set to 0x%08x\n",
  132. compression_type);
  133. break;
  134. case 0x84:
  135. ea->sample_rate = read_arbitrary(pb);
  136. av_log(s, AV_LOG_DEBUG,
  137. "sample_rate (element 0x84) set to %i\n",
  138. ea->sample_rate);
  139. break;
  140. case 0x85:
  141. ea->num_samples = read_arbitrary(pb);
  142. av_log(s, AV_LOG_DEBUG,
  143. "num_samples (element 0x85) set to 0x%08x\n",
  144. ea->num_samples);
  145. break;
  146. case 0x8A:
  147. av_log(s, AV_LOG_DEBUG,
  148. "element 0x%02x set to 0x%08"PRIx32"\n",
  149. subbyte, read_arbitrary(pb));
  150. av_log(s, AV_LOG_DEBUG, "exited audio subheader\n");
  151. in_subheader = 0;
  152. break;
  153. case 0xA0:
  154. revision2 = read_arbitrary(pb);
  155. av_log(s, AV_LOG_DEBUG,
  156. "revision2 (element 0xA0) set to 0x%08x\n",
  157. revision2);
  158. break;
  159. case 0xFF:
  160. av_log(s, AV_LOG_DEBUG,
  161. "end of header block reached (within audio subheader)\n");
  162. in_subheader = 0;
  163. in_header = 0;
  164. break;
  165. default:
  166. av_log(s, AV_LOG_DEBUG,
  167. "element 0x%02x set to 0x%08"PRIx32"\n",
  168. subbyte, read_arbitrary(pb));
  169. break;
  170. }
  171. }
  172. break;
  173. case 0xFF:
  174. av_log(s, AV_LOG_DEBUG, "end of header block reached\n");
  175. in_header = 0;
  176. break;
  177. default:
  178. av_log(s, AV_LOG_DEBUG,
  179. "header element 0x%02x set to 0x%08"PRIx32"\n",
  180. byte, read_arbitrary(pb));
  181. break;
  182. }
  183. }
  184. switch (compression_type) {
  185. case 0:
  186. ea->audio_codec = AV_CODEC_ID_PCM_S16LE;
  187. break;
  188. case 7:
  189. ea->audio_codec = AV_CODEC_ID_ADPCM_EA;
  190. break;
  191. case -1:
  192. switch (revision) {
  193. case 1:
  194. ea->audio_codec = AV_CODEC_ID_ADPCM_EA_R1;
  195. break;
  196. case 2:
  197. ea->audio_codec = AV_CODEC_ID_ADPCM_EA_R2;
  198. break;
  199. case 3:
  200. ea->audio_codec = AV_CODEC_ID_ADPCM_EA_R3;
  201. break;
  202. case -1:
  203. break;
  204. default:
  205. avpriv_request_sample(s, "stream type; revision=%i", revision);
  206. return 0;
  207. }
  208. switch (revision2) {
  209. case 8:
  210. ea->audio_codec = AV_CODEC_ID_PCM_S16LE_PLANAR;
  211. break;
  212. case 10:
  213. switch (revision) {
  214. case -1:
  215. case 2: ea->audio_codec = AV_CODEC_ID_ADPCM_EA_R1; break;
  216. case 3: ea->audio_codec = AV_CODEC_ID_ADPCM_EA_R2; break;
  217. default:
  218. avpriv_request_sample(s, "stream type; revision=%i, revision2=%i", revision, revision2);
  219. return 0;
  220. }
  221. break;
  222. case 15:
  223. case 16:
  224. ea->audio_codec = AV_CODEC_ID_MP3;
  225. break;
  226. case -1:
  227. break;
  228. default:
  229. ea->audio_codec = AV_CODEC_ID_NONE;
  230. avpriv_request_sample(s, "stream type; revision2=%i", revision2);
  231. return 0;
  232. }
  233. break;
  234. default:
  235. avpriv_request_sample(s,
  236. "stream type; compression_type=%i",
  237. compression_type);
  238. return 0;
  239. }
  240. if (ea->audio_codec == AV_CODEC_ID_NONE && ea->platform == 0x01)
  241. ea->audio_codec = AV_CODEC_ID_ADPCM_PSX;
  242. if (ea->sample_rate == -1)
  243. ea->sample_rate = revision == 3 ? 48000 : 22050;
  244. return 1;
  245. }
  246. static void process_audio_header_eacs(AVFormatContext *s)
  247. {
  248. EaDemuxContext *ea = s->priv_data;
  249. AVIOContext *pb = s->pb;
  250. int compression_type;
  251. ea->sample_rate = ea->big_endian ? avio_rb32(pb) : avio_rl32(pb);
  252. ea->bytes = avio_r8(pb); /* 1=8-bit, 2=16-bit */
  253. ea->num_channels = avio_r8(pb);
  254. compression_type = avio_r8(pb);
  255. avio_skip(pb, 13);
  256. switch (compression_type) {
  257. case 0:
  258. switch (ea->bytes) {
  259. case 1:
  260. ea->audio_codec = AV_CODEC_ID_PCM_S8;
  261. break;
  262. case 2:
  263. ea->audio_codec = AV_CODEC_ID_PCM_S16LE;
  264. break;
  265. }
  266. break;
  267. case 1:
  268. ea->audio_codec = AV_CODEC_ID_PCM_MULAW;
  269. ea->bytes = 1;
  270. break;
  271. case 2:
  272. ea->audio_codec = AV_CODEC_ID_ADPCM_IMA_EA_EACS;
  273. break;
  274. default:
  275. avpriv_request_sample(s,
  276. "stream type; audio compression_type=%i",
  277. compression_type);
  278. }
  279. }
  280. static void process_audio_header_sead(AVFormatContext *s)
  281. {
  282. EaDemuxContext *ea = s->priv_data;
  283. AVIOContext *pb = s->pb;
  284. ea->sample_rate = avio_rl32(pb);
  285. ea->bytes = avio_rl32(pb); /* 1=8-bit, 2=16-bit */
  286. ea->num_channels = avio_rl32(pb);
  287. ea->audio_codec = AV_CODEC_ID_ADPCM_IMA_EA_SEAD;
  288. }
  289. static void process_video_header_mdec(AVFormatContext *s, VideoProperties *video)
  290. {
  291. AVIOContext *pb = s->pb;
  292. avio_skip(pb, 4);
  293. video->width = avio_rl16(pb);
  294. video->height = avio_rl16(pb);
  295. video->time_base = (AVRational) { 1, 15 };
  296. video->codec = AV_CODEC_ID_MDEC;
  297. }
  298. static int process_video_header_vp6(AVFormatContext *s, VideoProperties *video)
  299. {
  300. AVIOContext *pb = s->pb;
  301. avio_skip(pb, 8);
  302. video->nb_frames = avio_rl32(pb);
  303. avio_skip(pb, 4);
  304. video->time_base.den = avio_rl32(pb);
  305. video->time_base.num = avio_rl32(pb);
  306. if (video->time_base.den <= 0 || video->time_base.num <= 0) {
  307. av_log(s, AV_LOG_ERROR, "Timebase is invalid\n");
  308. return AVERROR_INVALIDDATA;
  309. }
  310. video->codec = AV_CODEC_ID_VP6;
  311. return 1;
  312. }
  313. static void process_video_header_cmv(AVFormatContext *s, VideoProperties *video)
  314. {
  315. int fps;
  316. avio_skip(s->pb, 10);
  317. fps = avio_rl16(s->pb);
  318. if (fps)
  319. video->time_base = (AVRational) { 1, fps };
  320. video->codec = AV_CODEC_ID_CMV;
  321. }
  322. /* Process EA file header.
  323. * Return 1 if the EA file is valid and successfully opened, 0 otherwise. */
  324. static int process_ea_header(AVFormatContext *s)
  325. {
  326. uint32_t blockid, size = 0;
  327. EaDemuxContext *ea = s->priv_data;
  328. AVIOContext *pb = s->pb;
  329. int i;
  330. for (i = 0; i < 5 && (!ea->audio_codec || !ea->video.codec); i++) {
  331. uint64_t startpos = avio_tell(pb);
  332. int err = 0;
  333. blockid = avio_rl32(pb);
  334. size = avio_rl32(pb);
  335. if (i == 0)
  336. ea->big_endian = size > av_bswap32(size);
  337. if (ea->big_endian)
  338. size = av_bswap32(size);
  339. if (size < 8) {
  340. av_log(s, AV_LOG_ERROR, "chunk size too small\n");
  341. return AVERROR_INVALIDDATA;
  342. }
  343. switch (blockid) {
  344. case ISNh_TAG:
  345. if (avio_rl32(pb) != EACS_TAG) {
  346. avpriv_request_sample(s, "unknown 1SNh headerid");
  347. return 0;
  348. }
  349. process_audio_header_eacs(s);
  350. break;
  351. case SCHl_TAG:
  352. case SHEN_TAG:
  353. blockid = avio_rl32(pb);
  354. if (blockid == GSTR_TAG) {
  355. avio_skip(pb, 4);
  356. } else if ((blockid & 0xFF) != (PT00_TAG & 0xFF)) {
  357. blockid = avio_rl32(pb);
  358. }
  359. ea->platform = (blockid >> 16) & 0xFF;
  360. err = process_audio_header_elements(s);
  361. break;
  362. case SEAD_TAG:
  363. process_audio_header_sead(s);
  364. break;
  365. case MVIh_TAG:
  366. process_video_header_cmv(s, &ea->video);
  367. break;
  368. case kVGT_TAG:
  369. ea->video.codec = AV_CODEC_ID_TGV;
  370. break;
  371. case mTCD_TAG:
  372. process_video_header_mdec(s, &ea->video);
  373. break;
  374. case MPCh_TAG:
  375. ea->video.codec = AV_CODEC_ID_MPEG2VIDEO;
  376. break;
  377. case pQGT_TAG:
  378. case TGQs_TAG:
  379. ea->video.codec = AV_CODEC_ID_TGQ;
  380. ea->video.time_base = (AVRational) { 1, 15 };
  381. break;
  382. case pIQT_TAG:
  383. ea->video.codec = AV_CODEC_ID_TQI;
  384. ea->video.time_base = (AVRational) { 1, 15 };
  385. break;
  386. case MADk_TAG:
  387. ea->video.codec = AV_CODEC_ID_MAD;
  388. avio_skip(pb, 6);
  389. ea->video.time_base = (AVRational) { avio_rl16(pb), 1000 };
  390. break;
  391. case MVhd_TAG:
  392. err = process_video_header_vp6(s, &ea->video);
  393. break;
  394. case AVhd_TAG:
  395. err = process_video_header_vp6(s, &ea->alpha);
  396. break;
  397. }
  398. if (err < 0) {
  399. av_log(s, AV_LOG_ERROR, "error parsing header: %i\n", err);
  400. return err;
  401. }
  402. avio_seek(pb, startpos + size, SEEK_SET);
  403. }
  404. avio_seek(pb, 0, SEEK_SET);
  405. return 1;
  406. }
  407. static int ea_probe(const AVProbeData *p)
  408. {
  409. unsigned big_endian, size;
  410. switch (AV_RL32(&p->buf[0])) {
  411. case ISNh_TAG:
  412. case SCHl_TAG:
  413. case SEAD_TAG:
  414. case SHEN_TAG:
  415. case kVGT_TAG:
  416. case MADk_TAG:
  417. case MPCh_TAG:
  418. case MVhd_TAG:
  419. case MVIh_TAG:
  420. case AVP6_TAG:
  421. break;
  422. default:
  423. return 0;
  424. }
  425. size = AV_RL32(&p->buf[4]);
  426. big_endian = size > 0x000FFFFF;
  427. if (big_endian)
  428. size = av_bswap32(size);
  429. if (size > 0xfffff || size < 8)
  430. return 0;
  431. return AVPROBE_SCORE_MAX;
  432. }
  433. static int init_video_stream(AVFormatContext *s, VideoProperties *video)
  434. {
  435. AVStream *st;
  436. if (!video->codec)
  437. return 0;
  438. /* initialize the video decoder stream */
  439. st = avformat_new_stream(s, NULL);
  440. if (!st)
  441. return AVERROR(ENOMEM);
  442. video->stream_index = st->index;
  443. st->codecpar->codec_type = AVMEDIA_TYPE_VIDEO;
  444. st->codecpar->codec_id = video->codec;
  445. // parsing is necessary to make FFmpeg generate correct timestamps
  446. if (st->codecpar->codec_id == AV_CODEC_ID_MPEG2VIDEO)
  447. st->need_parsing = AVSTREAM_PARSE_HEADERS;
  448. st->codecpar->codec_tag = 0; /* no fourcc */
  449. st->codecpar->width = video->width;
  450. st->codecpar->height = video->height;
  451. st->duration = st->nb_frames = video->nb_frames;
  452. if (video->time_base.num)
  453. avpriv_set_pts_info(st, 64, video->time_base.num, video->time_base.den);
  454. st->r_frame_rate =
  455. st->avg_frame_rate = av_inv_q(video->time_base);
  456. return 0;
  457. }
  458. static int ea_read_header(AVFormatContext *s)
  459. {
  460. EaDemuxContext *ea = s->priv_data;
  461. AVStream *st;
  462. if (process_ea_header(s)<=0)
  463. return AVERROR(EIO);
  464. if (init_video_stream(s, &ea->video) || init_video_stream(s, &ea->alpha))
  465. return AVERROR(ENOMEM);
  466. if (ea->audio_codec) {
  467. if (ea->num_channels <= 0 || ea->num_channels > 2) {
  468. av_log(s, AV_LOG_WARNING,
  469. "Unsupported number of channels: %d\n", ea->num_channels);
  470. goto no_audio;
  471. }
  472. if (ea->sample_rate <= 0) {
  473. av_log(s, AV_LOG_ERROR,
  474. "Unsupported sample rate: %d\n", ea->sample_rate);
  475. goto no_audio;
  476. }
  477. if (ea->bytes <= 0 || ea->bytes > 2) {
  478. av_log(s, AV_LOG_ERROR,
  479. "Invalid number of bytes per sample: %d\n", ea->bytes);
  480. goto no_audio;
  481. }
  482. /* initialize the audio decoder stream */
  483. st = avformat_new_stream(s, NULL);
  484. if (!st)
  485. return AVERROR(ENOMEM);
  486. avpriv_set_pts_info(st, 33, 1, ea->sample_rate);
  487. st->codecpar->codec_type = AVMEDIA_TYPE_AUDIO;
  488. st->codecpar->codec_id = ea->audio_codec;
  489. st->codecpar->codec_tag = 0; /* no tag */
  490. st->codecpar->channels = ea->num_channels;
  491. st->codecpar->sample_rate = ea->sample_rate;
  492. st->codecpar->bits_per_coded_sample = ea->bytes * 8;
  493. st->codecpar->bit_rate = (int64_t)st->codecpar->channels *
  494. st->codecpar->sample_rate *
  495. st->codecpar->bits_per_coded_sample / 4;
  496. st->codecpar->block_align = st->codecpar->channels *
  497. st->codecpar->bits_per_coded_sample;
  498. ea->audio_stream_index = st->index;
  499. st->start_time = 0;
  500. return 1;
  501. }
  502. no_audio:
  503. ea->audio_codec = AV_CODEC_ID_NONE;
  504. if (!ea->video.codec)
  505. return AVERROR_INVALIDDATA;
  506. return 1;
  507. }
  508. static int ea_read_packet(AVFormatContext *s, AVPacket *pkt)
  509. {
  510. EaDemuxContext *ea = s->priv_data;
  511. AVIOContext *pb = s->pb;
  512. int partial_packet = 0;
  513. int hit_end = 0;
  514. unsigned int chunk_type, chunk_size;
  515. int ret = 0, packet_read = 0, key = 0;
  516. int av_uninit(num_samples);
  517. while ((!packet_read && !hit_end) || partial_packet) {
  518. chunk_type = avio_rl32(pb);
  519. chunk_size = ea->big_endian ? avio_rb32(pb) : avio_rl32(pb);
  520. if (chunk_size < 8)
  521. return AVERROR_INVALIDDATA;
  522. chunk_size -= 8;
  523. switch (chunk_type) {
  524. /* audio data */
  525. case ISNh_TAG:
  526. /* header chunk also contains data; skip over the header portion */
  527. if (chunk_size < 32)
  528. return AVERROR_INVALIDDATA;
  529. avio_skip(pb, 32);
  530. chunk_size -= 32;
  531. case ISNd_TAG:
  532. case SCDl_TAG:
  533. case SNDC_TAG:
  534. case SDEN_TAG:
  535. if (!ea->audio_codec) {
  536. avio_skip(pb, chunk_size);
  537. break;
  538. } else if (ea->audio_codec == AV_CODEC_ID_PCM_S16LE_PLANAR ||
  539. ea->audio_codec == AV_CODEC_ID_MP3) {
  540. num_samples = avio_rl32(pb);
  541. avio_skip(pb, 8);
  542. chunk_size -= 12;
  543. } else if (ea->audio_codec == AV_CODEC_ID_ADPCM_PSX) {
  544. avio_skip(pb, 8);
  545. chunk_size -= 8;
  546. }
  547. if (partial_packet) {
  548. avpriv_request_sample(s, "video header followed by audio packet");
  549. av_packet_unref(pkt);
  550. partial_packet = 0;
  551. }
  552. if (!chunk_size)
  553. continue;
  554. ret = av_get_packet(pb, pkt, chunk_size);
  555. if (ret < 0)
  556. return ret;
  557. pkt->stream_index = ea->audio_stream_index;
  558. switch (ea->audio_codec) {
  559. case AV_CODEC_ID_ADPCM_EA:
  560. case AV_CODEC_ID_ADPCM_EA_R1:
  561. case AV_CODEC_ID_ADPCM_EA_R2:
  562. case AV_CODEC_ID_ADPCM_IMA_EA_EACS:
  563. case AV_CODEC_ID_ADPCM_EA_R3:
  564. if (pkt->size < 4) {
  565. av_log(s, AV_LOG_ERROR, "Packet is too short\n");
  566. return AVERROR_INVALIDDATA;
  567. }
  568. if (ea->audio_codec == AV_CODEC_ID_ADPCM_EA_R3)
  569. pkt->duration = AV_RB32(pkt->data);
  570. else
  571. pkt->duration = AV_RL32(pkt->data);
  572. break;
  573. case AV_CODEC_ID_ADPCM_IMA_EA_SEAD:
  574. pkt->duration = ret * 2 / ea->num_channels;
  575. break;
  576. case AV_CODEC_ID_PCM_S16LE_PLANAR:
  577. case AV_CODEC_ID_MP3:
  578. pkt->duration = num_samples;
  579. break;
  580. case AV_CODEC_ID_ADPCM_PSX:
  581. pkt->duration = chunk_size / (16 * ea->num_channels) * 28;
  582. break;
  583. default:
  584. pkt->duration = chunk_size / (ea->bytes * ea->num_channels);
  585. }
  586. packet_read = 1;
  587. break;
  588. /* ending tag */
  589. case 0:
  590. case ISNe_TAG:
  591. case SCEl_TAG:
  592. case SEND_TAG:
  593. case SEEN_TAG:
  594. while (!avio_feof(pb)) {
  595. int tag = avio_rl32(pb);
  596. if (tag == ISNh_TAG ||
  597. tag == SCHl_TAG ||
  598. tag == SEAD_TAG ||
  599. tag == SHEN_TAG) {
  600. avio_skip(pb, -4);
  601. break;
  602. }
  603. }
  604. if (avio_feof(pb))
  605. ret = AVERROR_EOF;
  606. hit_end = 1;
  607. break;
  608. case MVIh_TAG:
  609. case kVGT_TAG:
  610. case pQGT_TAG:
  611. case TGQs_TAG:
  612. case MADk_TAG:
  613. key = AV_PKT_FLAG_KEY;
  614. case MVIf_TAG:
  615. case fVGT_TAG:
  616. case MADm_TAG:
  617. case MADe_TAG:
  618. avio_seek(pb, -8, SEEK_CUR); // include chunk preamble
  619. chunk_size += 8;
  620. goto get_video_packet;
  621. case mTCD_TAG:
  622. if (chunk_size < 8)
  623. return AVERROR_INVALIDDATA;
  624. avio_skip(pb, 8); // skip ea DCT header
  625. chunk_size -= 8;
  626. goto get_video_packet;
  627. case MV0K_TAG:
  628. case AV0K_TAG:
  629. case MPCh_TAG:
  630. case pIQT_TAG:
  631. key = AV_PKT_FLAG_KEY;
  632. case MV0F_TAG:
  633. case AV0F_TAG:
  634. get_video_packet:
  635. if (!chunk_size)
  636. continue;
  637. if (partial_packet) {
  638. ret = av_append_packet(pb, pkt, chunk_size);
  639. } else
  640. ret = av_get_packet(pb, pkt, chunk_size);
  641. if (ret < 0) {
  642. packet_read = 1;
  643. break;
  644. }
  645. partial_packet = chunk_type == MVIh_TAG;
  646. if (chunk_type == AV0K_TAG || chunk_type == AV0F_TAG)
  647. pkt->stream_index = ea->alpha.stream_index;
  648. else
  649. pkt->stream_index = ea->video.stream_index;
  650. pkt->flags |= key;
  651. packet_read = 1;
  652. break;
  653. default:
  654. avio_skip(pb, chunk_size);
  655. break;
  656. }
  657. }
  658. if (ret >= 0 && hit_end && !packet_read)
  659. return AVERROR(EAGAIN);
  660. return ret;
  661. }
  662. AVInputFormat ff_ea_demuxer = {
  663. .name = "ea",
  664. .long_name = NULL_IF_CONFIG_SMALL("Electronic Arts Multimedia"),
  665. .priv_data_size = sizeof(EaDemuxContext),
  666. .read_probe = ea_probe,
  667. .read_header = ea_read_header,
  668. .read_packet = ea_read_packet,
  669. };