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.

901 lines
32KB

  1. /*
  2. * Copyright (c) 2008 Jaikrishnan Menon <realityman@gmx.net>
  3. * Copyright (c) 2010 Peter Ross <pross@xvid.org>
  4. * Copyright (c) 2010 Sebastian Vater <cdgs.basty@googlemail.com>
  5. *
  6. * This file is part of FFmpeg.
  7. *
  8. * FFmpeg is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU Lesser General Public
  10. * License as published by the Free Software Foundation; either
  11. * version 2.1 of the License, or (at your option) any later version.
  12. *
  13. * FFmpeg is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  16. * Lesser General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU Lesser General Public
  19. * License along with FFmpeg; if not, write to the Free Software
  20. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  21. */
  22. /**
  23. * @file
  24. * IFF file demuxer
  25. * by Jaikrishnan Menon
  26. * for more information on the .iff file format, visit:
  27. * http://wiki.multimedia.cx/index.php?title=IFF
  28. */
  29. #include <inttypes.h>
  30. #include "libavutil/avassert.h"
  31. #include "libavutil/channel_layout.h"
  32. #include "libavutil/intreadwrite.h"
  33. #include "libavutil/dict.h"
  34. #include "libavcodec/bytestream.h"
  35. #include "avformat.h"
  36. #include "id3v2.h"
  37. #include "internal.h"
  38. #define ID_8SVX MKTAG('8','S','V','X')
  39. #define ID_16SV MKTAG('1','6','S','V')
  40. #define ID_MAUD MKTAG('M','A','U','D')
  41. #define ID_MHDR MKTAG('M','H','D','R')
  42. #define ID_MDAT MKTAG('M','D','A','T')
  43. #define ID_VHDR MKTAG('V','H','D','R')
  44. #define ID_ATAK MKTAG('A','T','A','K')
  45. #define ID_RLSE MKTAG('R','L','S','E')
  46. #define ID_CHAN MKTAG('C','H','A','N')
  47. #define ID_PBM MKTAG('P','B','M',' ')
  48. #define ID_ILBM MKTAG('I','L','B','M')
  49. #define ID_BMHD MKTAG('B','M','H','D')
  50. #define ID_DGBL MKTAG('D','G','B','L')
  51. #define ID_CAMG MKTAG('C','A','M','G')
  52. #define ID_CMAP MKTAG('C','M','A','P')
  53. #define ID_ACBM MKTAG('A','C','B','M')
  54. #define ID_DEEP MKTAG('D','E','E','P')
  55. #define ID_RGB8 MKTAG('R','G','B','8')
  56. #define ID_RGBN MKTAG('R','G','B','N')
  57. #define ID_DSD MKTAG('D','S','D',' ')
  58. #define ID_DST MKTAG('D','S','T',' ')
  59. #define ID_DSTC MKTAG('D','S','T','C')
  60. #define ID_DSTF MKTAG('D','S','T','F')
  61. #define ID_FRTE MKTAG('F','R','T','E')
  62. #define ID_ANIM MKTAG('A','N','I','M')
  63. #define ID_ANHD MKTAG('A','N','H','D')
  64. #define ID_DLTA MKTAG('D','L','T','A')
  65. #define ID_DPAN MKTAG('D','P','A','N')
  66. #define ID_FORM MKTAG('F','O','R','M')
  67. #define ID_FRM8 MKTAG('F','R','M','8')
  68. #define ID_ANNO MKTAG('A','N','N','O')
  69. #define ID_AUTH MKTAG('A','U','T','H')
  70. #define ID_CHRS MKTAG('C','H','R','S')
  71. #define ID_COPYRIGHT MKTAG('(','c',')',' ')
  72. #define ID_CSET MKTAG('C','S','E','T')
  73. #define ID_FVER MKTAG('F','V','E','R')
  74. #define ID_NAME MKTAG('N','A','M','E')
  75. #define ID_TEXT MKTAG('T','E','X','T')
  76. #define ID_ABIT MKTAG('A','B','I','T')
  77. #define ID_BODY MKTAG('B','O','D','Y')
  78. #define ID_DBOD MKTAG('D','B','O','D')
  79. #define ID_DPEL MKTAG('D','P','E','L')
  80. #define ID_DLOC MKTAG('D','L','O','C')
  81. #define ID_TVDC MKTAG('T','V','D','C')
  82. #define LEFT 2
  83. #define RIGHT 4
  84. #define STEREO 6
  85. /**
  86. * This number of bytes if added at the beginning of each AVPacket
  87. * which contain additional information about video properties
  88. * which has to be shared between demuxer and decoder.
  89. * This number may change between frames, e.g. the demuxer might
  90. * set it to smallest possible size of 2 to indicate that there's
  91. * no extradata changing in this frame.
  92. */
  93. #define IFF_EXTRA_VIDEO_SIZE 41
  94. typedef enum {
  95. COMP_NONE,
  96. COMP_FIB,
  97. COMP_EXP
  98. } svx8_compression_type;
  99. typedef struct IffDemuxContext {
  100. int is_64bit; ///< chunk size is 64-bit
  101. int64_t body_pos;
  102. int64_t body_end;
  103. uint32_t body_size;
  104. svx8_compression_type svx8_compression;
  105. unsigned maud_bits;
  106. unsigned maud_compression;
  107. unsigned bitmap_compression; ///< delta compression method used
  108. unsigned bpp; ///< bits per plane to decode (differs from bits_per_coded_sample if HAM)
  109. unsigned ham; ///< 0 if non-HAM or number of hold bits (6 for bpp > 6, 4 otherwise)
  110. unsigned flags; ///< 1 for EHB, 0 is no extra half darkening
  111. unsigned transparency; ///< transparency color index in palette
  112. unsigned masking; ///< masking method used
  113. uint8_t tvdc[32]; ///< TVDC lookup table
  114. int64_t pts;
  115. } IffDemuxContext;
  116. /* Metadata string read */
  117. static int get_metadata(AVFormatContext *s,
  118. const char *const tag,
  119. const unsigned data_size)
  120. {
  121. uint8_t *buf = ((data_size + 1) == 0) ? NULL : av_malloc(data_size + 1);
  122. if (!buf)
  123. return AVERROR(ENOMEM);
  124. if (avio_read(s->pb, buf, data_size) != data_size) {
  125. av_free(buf);
  126. return AVERROR(EIO);
  127. }
  128. buf[data_size] = 0;
  129. av_dict_set(&s->metadata, tag, buf, AV_DICT_DONT_STRDUP_VAL);
  130. return 0;
  131. }
  132. static int iff_probe(const AVProbeData *p)
  133. {
  134. const uint8_t *d = p->buf;
  135. if ( (AV_RL32(d) == ID_FORM &&
  136. (AV_RL32(d+8) == ID_8SVX ||
  137. AV_RL32(d+8) == ID_16SV ||
  138. AV_RL32(d+8) == ID_MAUD ||
  139. AV_RL32(d+8) == ID_PBM ||
  140. AV_RL32(d+8) == ID_ACBM ||
  141. AV_RL32(d+8) == ID_DEEP ||
  142. AV_RL32(d+8) == ID_ILBM ||
  143. AV_RL32(d+8) == ID_RGB8 ||
  144. AV_RL32(d+8) == ID_ANIM ||
  145. AV_RL32(d+8) == ID_RGBN)) ||
  146. (AV_RL32(d) == ID_FRM8 && AV_RL32(d+12) == ID_DSD))
  147. return AVPROBE_SCORE_MAX;
  148. return 0;
  149. }
  150. static const AVCodecTag dsd_codec_tags[] = {
  151. { AV_CODEC_ID_DSD_MSBF, ID_DSD },
  152. { AV_CODEC_ID_DST, ID_DST },
  153. { AV_CODEC_ID_NONE, 0 },
  154. };
  155. #define DSD_SLFT MKTAG('S','L','F','T')
  156. #define DSD_SRGT MKTAG('S','R','G','T')
  157. #define DSD_MLFT MKTAG('M','L','F','T')
  158. #define DSD_MRGT MKTAG('M','R','G','T')
  159. #define DSD_C MKTAG('C',' ',' ',' ')
  160. #define DSD_LS MKTAG('L','S',' ',' ')
  161. #define DSD_RS MKTAG('R','S',' ',' ')
  162. #define DSD_LFE MKTAG('L','F','E',' ')
  163. static const uint32_t dsd_stereo[] = { DSD_SLFT, DSD_SRGT };
  164. static const uint32_t dsd_5point0[] = { DSD_MLFT, DSD_MRGT, DSD_C, DSD_LS, DSD_RS };
  165. static const uint32_t dsd_5point1[] = { DSD_MLFT, DSD_MRGT, DSD_C, DSD_LFE, DSD_LS, DSD_RS };
  166. typedef struct {
  167. uint64_t layout;
  168. const uint32_t * dsd_layout;
  169. } DSDLayoutDesc;
  170. static const DSDLayoutDesc dsd_channel_layout[] = {
  171. { AV_CH_LAYOUT_STEREO, dsd_stereo },
  172. { AV_CH_LAYOUT_5POINT0, dsd_5point0 },
  173. { AV_CH_LAYOUT_5POINT1, dsd_5point1 },
  174. };
  175. static const uint64_t dsd_loudspeaker_config[] = {
  176. AV_CH_LAYOUT_STEREO,
  177. 0, 0,
  178. AV_CH_LAYOUT_5POINT0, AV_CH_LAYOUT_5POINT1,
  179. };
  180. static const char * dsd_source_comment[] = {
  181. "dsd_source_comment",
  182. "analogue_source_comment",
  183. "pcm_source_comment",
  184. };
  185. static const char * dsd_history_comment[] = {
  186. "general_remark",
  187. "operator_name",
  188. "creating_machine",
  189. "timezone",
  190. "file_revision"
  191. };
  192. static int parse_dsd_diin(AVFormatContext *s, AVStream *st, uint64_t eof)
  193. {
  194. AVIOContext *pb = s->pb;
  195. while (avio_tell(pb) + 12 <= eof && !avio_feof(pb)) {
  196. uint32_t tag = avio_rl32(pb);
  197. uint64_t size = avio_rb64(pb);
  198. uint64_t orig_pos = avio_tell(pb);
  199. const char * metadata_tag = NULL;
  200. switch(tag) {
  201. case MKTAG('D','I','A','R'): metadata_tag = "artist"; break;
  202. case MKTAG('D','I','T','I'): metadata_tag = "title"; break;
  203. }
  204. if (metadata_tag && size > 4) {
  205. unsigned int tag_size = avio_rb32(pb);
  206. int ret = get_metadata(s, metadata_tag, FFMIN(tag_size, size - 4));
  207. if (ret < 0) {
  208. av_log(s, AV_LOG_ERROR, "cannot allocate metadata tag %s!\n", metadata_tag);
  209. return ret;
  210. }
  211. }
  212. avio_skip(pb, size - (avio_tell(pb) - orig_pos) + (size & 1));
  213. }
  214. return 0;
  215. }
  216. static int parse_dsd_prop(AVFormatContext *s, AVStream *st, uint64_t eof)
  217. {
  218. AVIOContext *pb = s->pb;
  219. char abss[24];
  220. int hour, min, sec, i, ret, config;
  221. int dsd_layout[6];
  222. ID3v2ExtraMeta *id3v2_extra_meta;
  223. while (avio_tell(pb) + 12 <= eof && !avio_feof(pb)) {
  224. uint32_t tag = avio_rl32(pb);
  225. uint64_t size = avio_rb64(pb);
  226. uint64_t orig_pos = avio_tell(pb);
  227. switch(tag) {
  228. case MKTAG('A','B','S','S'):
  229. if (size < 8)
  230. return AVERROR_INVALIDDATA;
  231. hour = avio_rb16(pb);
  232. min = avio_r8(pb);
  233. sec = avio_r8(pb);
  234. snprintf(abss, sizeof(abss), "%02dh:%02dm:%02ds:%d", hour, min, sec, avio_rb32(pb));
  235. av_dict_set(&st->metadata, "absolute_start_time", abss, 0);
  236. break;
  237. case MKTAG('C','H','N','L'):
  238. if (size < 2)
  239. return AVERROR_INVALIDDATA;
  240. st->codecpar->channels = avio_rb16(pb);
  241. if (size < 2 + st->codecpar->channels * 4)
  242. return AVERROR_INVALIDDATA;
  243. st->codecpar->channel_layout = 0;
  244. if (st->codecpar->channels > FF_ARRAY_ELEMS(dsd_layout)) {
  245. avpriv_request_sample(s, "channel layout");
  246. break;
  247. }
  248. for (i = 0; i < st->codecpar->channels; i++)
  249. dsd_layout[i] = avio_rl32(pb);
  250. for (i = 0; i < FF_ARRAY_ELEMS(dsd_channel_layout); i++) {
  251. const DSDLayoutDesc * d = &dsd_channel_layout[i];
  252. if (av_get_channel_layout_nb_channels(d->layout) == st->codecpar->channels &&
  253. !memcmp(d->dsd_layout, dsd_layout, st->codecpar->channels * sizeof(uint32_t))) {
  254. st->codecpar->channel_layout = d->layout;
  255. break;
  256. }
  257. }
  258. break;
  259. case MKTAG('C','M','P','R'):
  260. if (size < 4)
  261. return AVERROR_INVALIDDATA;
  262. st->codecpar->codec_tag = tag = avio_rl32(pb);
  263. st->codecpar->codec_id = ff_codec_get_id(dsd_codec_tags, tag);
  264. if (!st->codecpar->codec_id) {
  265. av_log(s, AV_LOG_ERROR, "'%s' compression is not supported\n",
  266. av_fourcc2str(tag));
  267. return AVERROR_PATCHWELCOME;
  268. }
  269. break;
  270. case MKTAG('F','S',' ',' '):
  271. if (size < 4)
  272. return AVERROR_INVALIDDATA;
  273. st->codecpar->sample_rate = avio_rb32(pb) / 8;
  274. break;
  275. case MKTAG('I','D','3',' '):
  276. id3v2_extra_meta = NULL;
  277. ff_id3v2_read(s, ID3v2_DEFAULT_MAGIC, &id3v2_extra_meta, size);
  278. if (id3v2_extra_meta) {
  279. if ((ret = ff_id3v2_parse_apic(s, id3v2_extra_meta)) < 0 ||
  280. (ret = ff_id3v2_parse_chapters(s, id3v2_extra_meta)) < 0) {
  281. ff_id3v2_free_extra_meta(&id3v2_extra_meta);
  282. return ret;
  283. }
  284. ff_id3v2_free_extra_meta(&id3v2_extra_meta);
  285. }
  286. if (size < avio_tell(pb) - orig_pos) {
  287. av_log(s, AV_LOG_ERROR, "id3 exceeds chunk size\n");
  288. return AVERROR_INVALIDDATA;
  289. }
  290. break;
  291. case MKTAG('L','S','C','O'):
  292. if (size < 2)
  293. return AVERROR_INVALIDDATA;
  294. config = avio_rb16(pb);
  295. if (config != 0xFFFF) {
  296. if (config < FF_ARRAY_ELEMS(dsd_loudspeaker_config))
  297. st->codecpar->channel_layout = dsd_loudspeaker_config[config];
  298. if (!st->codecpar->channel_layout)
  299. avpriv_request_sample(s, "loudspeaker configuration %d", config);
  300. }
  301. break;
  302. }
  303. avio_skip(pb, size - (avio_tell(pb) - orig_pos) + (size & 1));
  304. }
  305. return 0;
  306. }
  307. static int read_dst_frame(AVFormatContext *s, AVPacket *pkt)
  308. {
  309. IffDemuxContext *iff = s->priv_data;
  310. AVIOContext *pb = s->pb;
  311. uint32_t chunk_id;
  312. uint64_t chunk_pos, data_pos, data_size;
  313. int ret = AVERROR_EOF;
  314. while (!avio_feof(pb)) {
  315. chunk_pos = avio_tell(pb);
  316. if (chunk_pos >= iff->body_end)
  317. return AVERROR_EOF;
  318. chunk_id = avio_rl32(pb);
  319. data_size = iff->is_64bit ? avio_rb64(pb) : avio_rb32(pb);
  320. data_pos = avio_tell(pb);
  321. if (data_size < 1)
  322. return AVERROR_INVALIDDATA;
  323. switch (chunk_id) {
  324. case ID_DSTF:
  325. if (!pkt) {
  326. iff->body_pos = avio_tell(pb) - (iff->is_64bit ? 12 : 8);
  327. iff->body_size = iff->body_end - iff->body_pos;
  328. return 0;
  329. }
  330. ret = av_get_packet(pb, pkt, data_size);
  331. if (ret < 0)
  332. return ret;
  333. if (data_size & 1)
  334. avio_skip(pb, 1);
  335. pkt->flags |= AV_PKT_FLAG_KEY;
  336. pkt->stream_index = 0;
  337. pkt->duration = 588 * s->streams[0]->codecpar->sample_rate / 44100;
  338. pkt->pos = chunk_pos;
  339. chunk_pos = avio_tell(pb);
  340. if (chunk_pos >= iff->body_end)
  341. return 0;
  342. avio_seek(pb, chunk_pos, SEEK_SET);
  343. return 0;
  344. case ID_FRTE:
  345. if (data_size < 4)
  346. return AVERROR_INVALIDDATA;
  347. s->streams[0]->duration = avio_rb32(pb) * 588LL * s->streams[0]->codecpar->sample_rate / 44100;
  348. break;
  349. }
  350. avio_skip(pb, data_size - (avio_tell(pb) - data_pos) + (data_size & 1));
  351. }
  352. return ret;
  353. }
  354. static const uint8_t deep_rgb24[] = {0, 0, 0, 3, 0, 1, 0, 8, 0, 2, 0, 8, 0, 3, 0, 8};
  355. static const uint8_t deep_rgba[] = {0, 0, 0, 4, 0, 1, 0, 8, 0, 2, 0, 8, 0, 3, 0, 8};
  356. static const uint8_t deep_bgra[] = {0, 0, 0, 4, 0, 3, 0, 8, 0, 2, 0, 8, 0, 1, 0, 8};
  357. static const uint8_t deep_argb[] = {0, 0, 0, 4, 0,17, 0, 8, 0, 1, 0, 8, 0, 2, 0, 8};
  358. static const uint8_t deep_abgr[] = {0, 0, 0, 4, 0,17, 0, 8, 0, 3, 0, 8, 0, 2, 0, 8};
  359. static int iff_read_header(AVFormatContext *s)
  360. {
  361. IffDemuxContext *iff = s->priv_data;
  362. AVIOContext *pb = s->pb;
  363. AVStream *st;
  364. uint8_t *buf;
  365. uint32_t chunk_id;
  366. uint64_t data_size;
  367. uint32_t screenmode = 0, num, den;
  368. unsigned transparency = 0;
  369. unsigned masking = 0; // no mask
  370. uint8_t fmt[16];
  371. int fmt_size;
  372. st = avformat_new_stream(s, NULL);
  373. if (!st)
  374. return AVERROR(ENOMEM);
  375. st->codecpar->channels = 1;
  376. st->codecpar->channel_layout = AV_CH_LAYOUT_MONO;
  377. iff->is_64bit = avio_rl32(pb) == ID_FRM8;
  378. avio_skip(pb, iff->is_64bit ? 8 : 4);
  379. // codec_tag used by ByteRun1 decoder to distinguish progressive (PBM) and interlaced (ILBM) content
  380. st->codecpar->codec_tag = avio_rl32(pb);
  381. if (st->codecpar->codec_tag == ID_ANIM) {
  382. avio_skip(pb, 12);
  383. }
  384. iff->bitmap_compression = -1;
  385. iff->svx8_compression = -1;
  386. iff->maud_bits = -1;
  387. iff->maud_compression = -1;
  388. while(!avio_feof(pb)) {
  389. uint64_t orig_pos;
  390. int res;
  391. const char *metadata_tag = NULL;
  392. int version, nb_comments, i;
  393. chunk_id = avio_rl32(pb);
  394. data_size = iff->is_64bit ? avio_rb64(pb) : avio_rb32(pb);
  395. orig_pos = avio_tell(pb);
  396. if (data_size >= INT64_MAX)
  397. return AVERROR_INVALIDDATA;
  398. switch(chunk_id) {
  399. case ID_VHDR:
  400. st->codecpar->codec_type = AVMEDIA_TYPE_AUDIO;
  401. if (data_size < 14)
  402. return AVERROR_INVALIDDATA;
  403. avio_skip(pb, 12);
  404. st->codecpar->sample_rate = avio_rb16(pb);
  405. if (data_size >= 16) {
  406. avio_skip(pb, 1);
  407. iff->svx8_compression = avio_r8(pb);
  408. }
  409. break;
  410. case ID_MHDR:
  411. st->codecpar->codec_type = AVMEDIA_TYPE_AUDIO;
  412. if (data_size < 32)
  413. return AVERROR_INVALIDDATA;
  414. avio_skip(pb, 4);
  415. iff->maud_bits = avio_rb16(pb);
  416. avio_skip(pb, 2);
  417. num = avio_rb32(pb);
  418. den = avio_rb16(pb);
  419. if (!den)
  420. return AVERROR_INVALIDDATA;
  421. avio_skip(pb, 2);
  422. st->codecpar->sample_rate = num / den;
  423. st->codecpar->channels = avio_rb16(pb);
  424. iff->maud_compression = avio_rb16(pb);
  425. if (st->codecpar->channels == 1)
  426. st->codecpar->channel_layout = AV_CH_LAYOUT_MONO;
  427. else if (st->codecpar->channels == 2)
  428. st->codecpar->channel_layout = AV_CH_LAYOUT_STEREO;
  429. break;
  430. case ID_ABIT:
  431. case ID_BODY:
  432. case ID_DBOD:
  433. case ID_DSD:
  434. case ID_DST:
  435. case ID_MDAT:
  436. iff->body_pos = avio_tell(pb);
  437. iff->body_end = iff->body_pos + data_size;
  438. iff->body_size = data_size;
  439. if (chunk_id == ID_DST) {
  440. int ret = read_dst_frame(s, NULL);
  441. if (ret < 0)
  442. return ret;
  443. }
  444. break;
  445. case ID_CHAN:
  446. if (data_size < 4)
  447. return AVERROR_INVALIDDATA;
  448. if (avio_rb32(pb) < 6) {
  449. st->codecpar->channels = 1;
  450. st->codecpar->channel_layout = AV_CH_LAYOUT_MONO;
  451. } else {
  452. st->codecpar->channels = 2;
  453. st->codecpar->channel_layout = AV_CH_LAYOUT_STEREO;
  454. }
  455. break;
  456. case ID_CAMG:
  457. if (data_size < 4)
  458. return AVERROR_INVALIDDATA;
  459. screenmode = avio_rb32(pb);
  460. break;
  461. case ID_CMAP:
  462. if (data_size < 3 || data_size > 768 || data_size % 3) {
  463. av_log(s, AV_LOG_ERROR, "Invalid CMAP chunk size %"PRIu64"\n",
  464. data_size);
  465. return AVERROR_INVALIDDATA;
  466. }
  467. res = ff_alloc_extradata(st->codecpar,
  468. data_size + IFF_EXTRA_VIDEO_SIZE);
  469. if (res < 0)
  470. return res;
  471. if (avio_read(pb, st->codecpar->extradata + IFF_EXTRA_VIDEO_SIZE, data_size) < 0) {
  472. av_freep(&st->codecpar->extradata);
  473. st->codecpar->extradata_size = 0;
  474. return AVERROR(EIO);
  475. }
  476. break;
  477. case ID_BMHD:
  478. st->codecpar->codec_type = AVMEDIA_TYPE_VIDEO;
  479. if (data_size <= 8)
  480. return AVERROR_INVALIDDATA;
  481. st->codecpar->width = avio_rb16(pb);
  482. st->codecpar->height = avio_rb16(pb);
  483. avio_skip(pb, 4); // x, y offset
  484. st->codecpar->bits_per_coded_sample = avio_r8(pb);
  485. if (data_size >= 10)
  486. masking = avio_r8(pb);
  487. if (data_size >= 11)
  488. iff->bitmap_compression = avio_r8(pb);
  489. if (data_size >= 14) {
  490. avio_skip(pb, 1); // padding
  491. transparency = avio_rb16(pb);
  492. }
  493. if (data_size >= 16) {
  494. st->sample_aspect_ratio.num = avio_r8(pb);
  495. st->sample_aspect_ratio.den = avio_r8(pb);
  496. }
  497. break;
  498. case ID_ANHD:
  499. break;
  500. case ID_DPAN:
  501. avio_skip(pb, 2);
  502. st->duration = avio_rb16(pb);
  503. break;
  504. case ID_DPEL:
  505. if (data_size < 4 || (data_size & 3))
  506. return AVERROR_INVALIDDATA;
  507. if ((fmt_size = avio_read(pb, fmt, sizeof(fmt))) < 0)
  508. return fmt_size;
  509. if (fmt_size == sizeof(deep_rgb24) && !memcmp(fmt, deep_rgb24, sizeof(deep_rgb24)))
  510. st->codecpar->format = AV_PIX_FMT_RGB24;
  511. else if (fmt_size == sizeof(deep_rgba) && !memcmp(fmt, deep_rgba, sizeof(deep_rgba)))
  512. st->codecpar->format = AV_PIX_FMT_RGBA;
  513. else if (fmt_size == sizeof(deep_bgra) && !memcmp(fmt, deep_bgra, sizeof(deep_bgra)))
  514. st->codecpar->format = AV_PIX_FMT_BGRA;
  515. else if (fmt_size == sizeof(deep_argb) && !memcmp(fmt, deep_argb, sizeof(deep_argb)))
  516. st->codecpar->format = AV_PIX_FMT_ARGB;
  517. else if (fmt_size == sizeof(deep_abgr) && !memcmp(fmt, deep_abgr, sizeof(deep_abgr)))
  518. st->codecpar->format = AV_PIX_FMT_ABGR;
  519. else {
  520. avpriv_request_sample(s, "color format %.16s", fmt);
  521. return AVERROR_PATCHWELCOME;
  522. }
  523. break;
  524. case ID_DGBL:
  525. st->codecpar->codec_type = AVMEDIA_TYPE_VIDEO;
  526. if (data_size < 8)
  527. return AVERROR_INVALIDDATA;
  528. st->codecpar->width = avio_rb16(pb);
  529. st->codecpar->height = avio_rb16(pb);
  530. iff->bitmap_compression = avio_rb16(pb);
  531. st->sample_aspect_ratio.num = avio_r8(pb);
  532. st->sample_aspect_ratio.den = avio_r8(pb);
  533. st->codecpar->bits_per_coded_sample = 24;
  534. break;
  535. case ID_DLOC:
  536. if (data_size < 4)
  537. return AVERROR_INVALIDDATA;
  538. st->codecpar->width = avio_rb16(pb);
  539. st->codecpar->height = avio_rb16(pb);
  540. break;
  541. case ID_TVDC:
  542. if (data_size < sizeof(iff->tvdc))
  543. return AVERROR_INVALIDDATA;
  544. res = avio_read(pb, iff->tvdc, sizeof(iff->tvdc));
  545. if (res < 0)
  546. return res;
  547. break;
  548. case ID_ANNO:
  549. case ID_TEXT: metadata_tag = "comment"; break;
  550. case ID_AUTH: metadata_tag = "artist"; break;
  551. case ID_COPYRIGHT: metadata_tag = "copyright"; break;
  552. case ID_NAME: metadata_tag = "title"; break;
  553. /* DSD tags */
  554. case MKTAG('F','V','E','R'):
  555. if (data_size < 4)
  556. return AVERROR_INVALIDDATA;
  557. version = avio_rb32(pb);
  558. av_log(s, AV_LOG_DEBUG, "DSIFF v%d.%d.%d.%d\n",version >> 24, (version >> 16) & 0xFF, (version >> 8) & 0xFF, version & 0xFF);
  559. st->codecpar->codec_type = AVMEDIA_TYPE_AUDIO;
  560. break;
  561. case MKTAG('D','I','I','N'):
  562. res = parse_dsd_diin(s, st, orig_pos + data_size);
  563. if (res < 0)
  564. return res;
  565. break;
  566. case MKTAG('P','R','O','P'):
  567. if (data_size < 4)
  568. return AVERROR_INVALIDDATA;
  569. if (avio_rl32(pb) != MKTAG('S','N','D',' ')) {
  570. avpriv_request_sample(s, "unknown property type");
  571. break;
  572. }
  573. res = parse_dsd_prop(s, st, orig_pos + data_size);
  574. if (res < 0)
  575. return res;
  576. break;
  577. case MKTAG('C','O','M','T'):
  578. if (data_size < 2)
  579. return AVERROR_INVALIDDATA;
  580. nb_comments = avio_rb16(pb);
  581. for (i = 0; i < nb_comments; i++) {
  582. int year, mon, day, hour, min, type, ref;
  583. char tmp[24];
  584. const char *tag;
  585. int metadata_size;
  586. year = avio_rb16(pb);
  587. mon = avio_r8(pb);
  588. day = avio_r8(pb);
  589. hour = avio_r8(pb);
  590. min = avio_r8(pb);
  591. snprintf(tmp, sizeof(tmp), "%04d-%02d-%02d %02d:%02d", year, mon, day, hour, min);
  592. av_dict_set(&st->metadata, "comment_time", tmp, 0);
  593. type = avio_rb16(pb);
  594. ref = avio_rb16(pb);
  595. switch (type) {
  596. case 1:
  597. if (!i)
  598. tag = "channel_comment";
  599. else {
  600. snprintf(tmp, sizeof(tmp), "channel%d_comment", ref);
  601. tag = tmp;
  602. }
  603. break;
  604. case 2:
  605. tag = ref < FF_ARRAY_ELEMS(dsd_source_comment) ? dsd_source_comment[ref] : "source_comment";
  606. break;
  607. case 3:
  608. tag = ref < FF_ARRAY_ELEMS(dsd_history_comment) ? dsd_history_comment[ref] : "file_history";
  609. break;
  610. default:
  611. tag = "comment";
  612. }
  613. metadata_size = avio_rb32(pb);
  614. if ((res = get_metadata(s, tag, metadata_size)) < 0) {
  615. av_log(s, AV_LOG_ERROR, "cannot allocate metadata tag %s!\n", tag);
  616. return res;
  617. }
  618. if (metadata_size & 1)
  619. avio_skip(pb, 1);
  620. }
  621. break;
  622. }
  623. if (metadata_tag) {
  624. if ((res = get_metadata(s, metadata_tag, data_size)) < 0) {
  625. av_log(s, AV_LOG_ERROR, "cannot allocate metadata tag %s!\n", metadata_tag);
  626. return res;
  627. }
  628. }
  629. avio_skip(pb, data_size - (avio_tell(pb) - orig_pos) + (data_size & 1));
  630. }
  631. if (st->codecpar->codec_tag == ID_ANIM)
  632. avio_seek(pb, 12, SEEK_SET);
  633. else
  634. avio_seek(pb, iff->body_pos, SEEK_SET);
  635. switch(st->codecpar->codec_type) {
  636. case AVMEDIA_TYPE_AUDIO:
  637. avpriv_set_pts_info(st, 32, 1, st->codecpar->sample_rate);
  638. if (st->codecpar->codec_tag == ID_16SV)
  639. st->codecpar->codec_id = AV_CODEC_ID_PCM_S16BE_PLANAR;
  640. else if (st->codecpar->codec_tag == ID_MAUD) {
  641. if (iff->maud_bits == 8 && !iff->maud_compression) {
  642. st->codecpar->codec_id = AV_CODEC_ID_PCM_U8;
  643. } else if (iff->maud_bits == 16 && !iff->maud_compression) {
  644. st->codecpar->codec_id = AV_CODEC_ID_PCM_S16BE;
  645. } else if (iff->maud_bits == 8 && iff->maud_compression == 2) {
  646. st->codecpar->codec_id = AV_CODEC_ID_PCM_ALAW;
  647. } else if (iff->maud_bits == 8 && iff->maud_compression == 3) {
  648. st->codecpar->codec_id = AV_CODEC_ID_PCM_MULAW;
  649. } else {
  650. avpriv_request_sample(s, "compression %d and bit depth %d", iff->maud_compression, iff->maud_bits);
  651. return AVERROR_PATCHWELCOME;
  652. }
  653. } else if (st->codecpar->codec_tag != ID_DSD &&
  654. st->codecpar->codec_tag != ID_DST) {
  655. switch (iff->svx8_compression) {
  656. case COMP_NONE:
  657. st->codecpar->codec_id = AV_CODEC_ID_PCM_S8_PLANAR;
  658. break;
  659. case COMP_FIB:
  660. st->codecpar->codec_id = AV_CODEC_ID_8SVX_FIB;
  661. break;
  662. case COMP_EXP:
  663. st->codecpar->codec_id = AV_CODEC_ID_8SVX_EXP;
  664. break;
  665. default:
  666. av_log(s, AV_LOG_ERROR,
  667. "Unknown SVX8 compression method '%d'\n", iff->svx8_compression);
  668. return -1;
  669. }
  670. }
  671. st->codecpar->bits_per_coded_sample = av_get_bits_per_sample(st->codecpar->codec_id);
  672. st->codecpar->bit_rate = (int64_t)st->codecpar->channels * st->codecpar->sample_rate * st->codecpar->bits_per_coded_sample;
  673. st->codecpar->block_align = st->codecpar->channels * st->codecpar->bits_per_coded_sample;
  674. if (st->codecpar->codec_tag == ID_DSD && st->codecpar->block_align <= 0)
  675. return AVERROR_INVALIDDATA;
  676. break;
  677. case AVMEDIA_TYPE_VIDEO:
  678. iff->bpp = st->codecpar->bits_per_coded_sample;
  679. if (st->codecpar->codec_tag == ID_ANIM)
  680. avpriv_set_pts_info(st, 32, 1, 60);
  681. if ((screenmode & 0x800 /* Hold And Modify */) && iff->bpp <= 8) {
  682. iff->ham = iff->bpp > 6 ? 6 : 4;
  683. st->codecpar->bits_per_coded_sample = 24;
  684. }
  685. iff->flags = (screenmode & 0x80 /* Extra HalfBrite */) && iff->bpp <= 8;
  686. iff->masking = masking;
  687. iff->transparency = transparency;
  688. if (!st->codecpar->extradata) {
  689. int ret = ff_alloc_extradata(st->codecpar, IFF_EXTRA_VIDEO_SIZE);
  690. if (ret < 0)
  691. return ret;
  692. }
  693. av_assert0(st->codecpar->extradata_size >= IFF_EXTRA_VIDEO_SIZE);
  694. buf = st->codecpar->extradata;
  695. bytestream_put_be16(&buf, IFF_EXTRA_VIDEO_SIZE);
  696. bytestream_put_byte(&buf, iff->bitmap_compression);
  697. bytestream_put_byte(&buf, iff->bpp);
  698. bytestream_put_byte(&buf, iff->ham);
  699. bytestream_put_byte(&buf, iff->flags);
  700. bytestream_put_be16(&buf, iff->transparency);
  701. bytestream_put_byte(&buf, iff->masking);
  702. bytestream_put_buffer(&buf, iff->tvdc, sizeof(iff->tvdc));
  703. st->codecpar->codec_id = AV_CODEC_ID_IFF_ILBM;
  704. break;
  705. default:
  706. return -1;
  707. }
  708. return 0;
  709. }
  710. static unsigned get_anim_duration(uint8_t *buf, int size)
  711. {
  712. GetByteContext gb;
  713. bytestream2_init(&gb, buf, size);
  714. bytestream2_skip(&gb, 4);
  715. while (bytestream2_get_bytes_left(&gb) > 8) {
  716. unsigned chunk = bytestream2_get_le32(&gb);
  717. unsigned size = bytestream2_get_be32(&gb);
  718. if (chunk == ID_ANHD) {
  719. if (size < 40)
  720. break;
  721. bytestream2_skip(&gb, 14);
  722. return bytestream2_get_be32(&gb);
  723. } else {
  724. bytestream2_skip(&gb, size + size & 1);
  725. }
  726. }
  727. return 10;
  728. }
  729. static int iff_read_packet(AVFormatContext *s,
  730. AVPacket *pkt)
  731. {
  732. IffDemuxContext *iff = s->priv_data;
  733. AVIOContext *pb = s->pb;
  734. AVStream *st = s->streams[0];
  735. int ret;
  736. int64_t pos = avio_tell(pb);
  737. if (avio_feof(pb))
  738. return AVERROR_EOF;
  739. if (st->codecpar->codec_tag != ID_ANIM && pos >= iff->body_end)
  740. return AVERROR_EOF;
  741. if (st->codecpar->codec_type == AVMEDIA_TYPE_AUDIO) {
  742. if (st->codecpar->codec_tag == ID_DSD || st->codecpar->codec_tag == ID_MAUD) {
  743. ret = av_get_packet(pb, pkt, FFMIN(iff->body_end - pos, 1024 * st->codecpar->block_align));
  744. } else if (st->codecpar->codec_tag == ID_DST) {
  745. return read_dst_frame(s, pkt);
  746. } else {
  747. if (iff->body_size > INT_MAX)
  748. return AVERROR_INVALIDDATA;
  749. ret = av_get_packet(pb, pkt, iff->body_size);
  750. }
  751. } else if (st->codecpar->codec_type == AVMEDIA_TYPE_VIDEO &&
  752. st->codecpar->codec_tag == ID_ANIM) {
  753. uint64_t data_size, orig_pos;
  754. uint32_t chunk_id, chunk_id2;
  755. while (!avio_feof(pb)) {
  756. if (avio_feof(pb))
  757. return AVERROR_EOF;
  758. orig_pos = avio_tell(pb);
  759. chunk_id = avio_rl32(pb);
  760. data_size = avio_rb32(pb);
  761. chunk_id2 = avio_rl32(pb);
  762. if (chunk_id == ID_FORM &&
  763. chunk_id2 == ID_ILBM) {
  764. avio_skip(pb, -4);
  765. break;
  766. } else if (chunk_id == ID_FORM &&
  767. chunk_id2 == ID_ANIM) {
  768. continue;
  769. } else {
  770. avio_skip(pb, data_size);
  771. }
  772. }
  773. ret = av_get_packet(pb, pkt, data_size);
  774. pkt->pos = orig_pos;
  775. pkt->duration = get_anim_duration(pkt->data, pkt->size);
  776. if (pos == 12)
  777. pkt->flags |= AV_PKT_FLAG_KEY;
  778. } else if (st->codecpar->codec_type == AVMEDIA_TYPE_VIDEO &&
  779. st->codecpar->codec_tag != ID_ANIM) {
  780. ret = av_get_packet(pb, pkt, iff->body_size);
  781. pkt->pos = pos;
  782. if (pos == iff->body_pos)
  783. pkt->flags |= AV_PKT_FLAG_KEY;
  784. } else {
  785. av_assert0(0);
  786. }
  787. if (ret < 0)
  788. return ret;
  789. pkt->stream_index = 0;
  790. return ret;
  791. }
  792. AVInputFormat ff_iff_demuxer = {
  793. .name = "iff",
  794. .long_name = NULL_IF_CONFIG_SMALL("IFF (Interchange File Format)"),
  795. .priv_data_size = sizeof(IffDemuxContext),
  796. .read_probe = iff_probe,
  797. .read_header = iff_read_header,
  798. .read_packet = iff_read_packet,
  799. .flags = AVFMT_GENERIC_INDEX | AVFMT_NO_BYTE_SEEK,
  800. };