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.

909 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. if (size >= INT64_MAX)
  201. return AVERROR_INVALIDDATA;
  202. switch(tag) {
  203. case MKTAG('D','I','A','R'): metadata_tag = "artist"; break;
  204. case MKTAG('D','I','T','I'): metadata_tag = "title"; break;
  205. }
  206. if (metadata_tag && size > 4) {
  207. unsigned int tag_size = avio_rb32(pb);
  208. int ret = get_metadata(s, metadata_tag, FFMIN(tag_size, size - 4));
  209. if (ret < 0) {
  210. av_log(s, AV_LOG_ERROR, "cannot allocate metadata tag %s!\n", metadata_tag);
  211. return ret;
  212. }
  213. }
  214. avio_skip(pb, size - (avio_tell(pb) - orig_pos) + (size & 1));
  215. }
  216. return 0;
  217. }
  218. static int parse_dsd_prop(AVFormatContext *s, AVStream *st, uint64_t eof)
  219. {
  220. AVIOContext *pb = s->pb;
  221. char abss[24];
  222. int hour, min, sec, i, ret, config;
  223. int dsd_layout[6];
  224. ID3v2ExtraMeta *id3v2_extra_meta;
  225. while (avio_tell(pb) + 12 <= eof && !avio_feof(pb)) {
  226. uint32_t tag = avio_rl32(pb);
  227. uint64_t size = avio_rb64(pb);
  228. uint64_t orig_pos = avio_tell(pb);
  229. if (size >= INT64_MAX)
  230. return AVERROR_INVALIDDATA;
  231. switch(tag) {
  232. case MKTAG('A','B','S','S'):
  233. if (size < 8)
  234. return AVERROR_INVALIDDATA;
  235. hour = avio_rb16(pb);
  236. min = avio_r8(pb);
  237. sec = avio_r8(pb);
  238. snprintf(abss, sizeof(abss), "%02dh:%02dm:%02ds:%d", hour, min, sec, avio_rb32(pb));
  239. av_dict_set(&st->metadata, "absolute_start_time", abss, 0);
  240. break;
  241. case MKTAG('C','H','N','L'):
  242. if (size < 2)
  243. return AVERROR_INVALIDDATA;
  244. st->codecpar->channels = avio_rb16(pb);
  245. if (size < 2 + st->codecpar->channels * 4)
  246. return AVERROR_INVALIDDATA;
  247. st->codecpar->channel_layout = 0;
  248. if (st->codecpar->channels > FF_ARRAY_ELEMS(dsd_layout)) {
  249. avpriv_request_sample(s, "channel layout");
  250. break;
  251. }
  252. for (i = 0; i < st->codecpar->channels; i++)
  253. dsd_layout[i] = avio_rl32(pb);
  254. for (i = 0; i < FF_ARRAY_ELEMS(dsd_channel_layout); i++) {
  255. const DSDLayoutDesc * d = &dsd_channel_layout[i];
  256. if (av_get_channel_layout_nb_channels(d->layout) == st->codecpar->channels &&
  257. !memcmp(d->dsd_layout, dsd_layout, st->codecpar->channels * sizeof(uint32_t))) {
  258. st->codecpar->channel_layout = d->layout;
  259. break;
  260. }
  261. }
  262. break;
  263. case MKTAG('C','M','P','R'):
  264. if (size < 4)
  265. return AVERROR_INVALIDDATA;
  266. st->codecpar->codec_tag = tag = avio_rl32(pb);
  267. st->codecpar->codec_id = ff_codec_get_id(dsd_codec_tags, tag);
  268. if (!st->codecpar->codec_id) {
  269. av_log(s, AV_LOG_ERROR, "'%s' compression is not supported\n",
  270. av_fourcc2str(tag));
  271. return AVERROR_PATCHWELCOME;
  272. }
  273. break;
  274. case MKTAG('F','S',' ',' '):
  275. if (size < 4)
  276. return AVERROR_INVALIDDATA;
  277. st->codecpar->sample_rate = avio_rb32(pb) / 8;
  278. break;
  279. case MKTAG('I','D','3',' '):
  280. id3v2_extra_meta = NULL;
  281. ff_id3v2_read(s, ID3v2_DEFAULT_MAGIC, &id3v2_extra_meta, size);
  282. if (id3v2_extra_meta) {
  283. if ((ret = ff_id3v2_parse_apic(s, id3v2_extra_meta)) < 0 ||
  284. (ret = ff_id3v2_parse_chapters(s, id3v2_extra_meta)) < 0) {
  285. ff_id3v2_free_extra_meta(&id3v2_extra_meta);
  286. return ret;
  287. }
  288. ff_id3v2_free_extra_meta(&id3v2_extra_meta);
  289. }
  290. if (size < avio_tell(pb) - orig_pos) {
  291. av_log(s, AV_LOG_ERROR, "id3 exceeds chunk size\n");
  292. return AVERROR_INVALIDDATA;
  293. }
  294. break;
  295. case MKTAG('L','S','C','O'):
  296. if (size < 2)
  297. return AVERROR_INVALIDDATA;
  298. config = avio_rb16(pb);
  299. if (config != 0xFFFF) {
  300. if (config < FF_ARRAY_ELEMS(dsd_loudspeaker_config))
  301. st->codecpar->channel_layout = dsd_loudspeaker_config[config];
  302. if (!st->codecpar->channel_layout)
  303. avpriv_request_sample(s, "loudspeaker configuration %d", config);
  304. }
  305. break;
  306. }
  307. avio_skip(pb, size - (avio_tell(pb) - orig_pos) + (size & 1));
  308. }
  309. return 0;
  310. }
  311. static int read_dst_frame(AVFormatContext *s, AVPacket *pkt)
  312. {
  313. IffDemuxContext *iff = s->priv_data;
  314. AVIOContext *pb = s->pb;
  315. uint32_t chunk_id;
  316. uint64_t chunk_pos, data_pos, data_size;
  317. int ret = AVERROR_EOF;
  318. while (!avio_feof(pb)) {
  319. chunk_pos = avio_tell(pb);
  320. if (chunk_pos >= iff->body_end)
  321. return AVERROR_EOF;
  322. chunk_id = avio_rl32(pb);
  323. data_size = iff->is_64bit ? avio_rb64(pb) : avio_rb32(pb);
  324. data_pos = avio_tell(pb);
  325. if (data_size < 1)
  326. return AVERROR_INVALIDDATA;
  327. switch (chunk_id) {
  328. case ID_DSTF:
  329. if (!pkt) {
  330. iff->body_pos = avio_tell(pb) - (iff->is_64bit ? 12 : 8);
  331. iff->body_size = iff->body_end - iff->body_pos;
  332. return 0;
  333. }
  334. ret = av_get_packet(pb, pkt, data_size);
  335. if (ret < 0)
  336. return ret;
  337. if (data_size & 1)
  338. avio_skip(pb, 1);
  339. pkt->flags |= AV_PKT_FLAG_KEY;
  340. pkt->stream_index = 0;
  341. pkt->duration = 588 * s->streams[0]->codecpar->sample_rate / 44100;
  342. pkt->pos = chunk_pos;
  343. chunk_pos = avio_tell(pb);
  344. if (chunk_pos >= iff->body_end)
  345. return 0;
  346. avio_seek(pb, chunk_pos, SEEK_SET);
  347. return 0;
  348. case ID_FRTE:
  349. if (data_size < 4)
  350. return AVERROR_INVALIDDATA;
  351. s->streams[0]->duration = avio_rb32(pb) * 588LL * s->streams[0]->codecpar->sample_rate / 44100;
  352. break;
  353. }
  354. avio_skip(pb, data_size - (avio_tell(pb) - data_pos) + (data_size & 1));
  355. }
  356. return ret;
  357. }
  358. static const uint8_t deep_rgb24[] = {0, 0, 0, 3, 0, 1, 0, 8, 0, 2, 0, 8, 0, 3, 0, 8};
  359. static const uint8_t deep_rgba[] = {0, 0, 0, 4, 0, 1, 0, 8, 0, 2, 0, 8, 0, 3, 0, 8};
  360. static const uint8_t deep_bgra[] = {0, 0, 0, 4, 0, 3, 0, 8, 0, 2, 0, 8, 0, 1, 0, 8};
  361. static const uint8_t deep_argb[] = {0, 0, 0, 4, 0,17, 0, 8, 0, 1, 0, 8, 0, 2, 0, 8};
  362. static const uint8_t deep_abgr[] = {0, 0, 0, 4, 0,17, 0, 8, 0, 3, 0, 8, 0, 2, 0, 8};
  363. static int iff_read_header(AVFormatContext *s)
  364. {
  365. IffDemuxContext *iff = s->priv_data;
  366. AVIOContext *pb = s->pb;
  367. AVStream *st;
  368. uint8_t *buf;
  369. uint32_t chunk_id;
  370. uint64_t data_size;
  371. uint32_t screenmode = 0, num, den;
  372. unsigned transparency = 0;
  373. unsigned masking = 0; // no mask
  374. uint8_t fmt[16];
  375. int fmt_size;
  376. st = avformat_new_stream(s, NULL);
  377. if (!st)
  378. return AVERROR(ENOMEM);
  379. st->codecpar->channels = 1;
  380. st->codecpar->channel_layout = AV_CH_LAYOUT_MONO;
  381. iff->is_64bit = avio_rl32(pb) == ID_FRM8;
  382. avio_skip(pb, iff->is_64bit ? 8 : 4);
  383. // codec_tag used by ByteRun1 decoder to distinguish progressive (PBM) and interlaced (ILBM) content
  384. st->codecpar->codec_tag = avio_rl32(pb);
  385. if (st->codecpar->codec_tag == ID_ANIM) {
  386. avio_skip(pb, 12);
  387. }
  388. iff->bitmap_compression = -1;
  389. iff->svx8_compression = -1;
  390. iff->maud_bits = -1;
  391. iff->maud_compression = -1;
  392. while(!avio_feof(pb)) {
  393. uint64_t orig_pos;
  394. int res;
  395. const char *metadata_tag = NULL;
  396. int version, nb_comments, i;
  397. chunk_id = avio_rl32(pb);
  398. data_size = iff->is_64bit ? avio_rb64(pb) : avio_rb32(pb);
  399. orig_pos = avio_tell(pb);
  400. if (data_size >= INT64_MAX)
  401. return AVERROR_INVALIDDATA;
  402. switch(chunk_id) {
  403. case ID_VHDR:
  404. st->codecpar->codec_type = AVMEDIA_TYPE_AUDIO;
  405. if (data_size < 14)
  406. return AVERROR_INVALIDDATA;
  407. avio_skip(pb, 12);
  408. st->codecpar->sample_rate = avio_rb16(pb);
  409. if (data_size >= 16) {
  410. avio_skip(pb, 1);
  411. iff->svx8_compression = avio_r8(pb);
  412. }
  413. break;
  414. case ID_MHDR:
  415. st->codecpar->codec_type = AVMEDIA_TYPE_AUDIO;
  416. if (data_size < 32)
  417. return AVERROR_INVALIDDATA;
  418. avio_skip(pb, 4);
  419. iff->maud_bits = avio_rb16(pb);
  420. avio_skip(pb, 2);
  421. num = avio_rb32(pb);
  422. den = avio_rb16(pb);
  423. if (!den)
  424. return AVERROR_INVALIDDATA;
  425. avio_skip(pb, 2);
  426. st->codecpar->sample_rate = num / den;
  427. st->codecpar->channels = avio_rb16(pb);
  428. iff->maud_compression = avio_rb16(pb);
  429. if (st->codecpar->channels == 1)
  430. st->codecpar->channel_layout = AV_CH_LAYOUT_MONO;
  431. else if (st->codecpar->channels == 2)
  432. st->codecpar->channel_layout = AV_CH_LAYOUT_STEREO;
  433. break;
  434. case ID_ABIT:
  435. case ID_BODY:
  436. case ID_DBOD:
  437. case ID_DSD:
  438. case ID_DST:
  439. case ID_MDAT:
  440. iff->body_pos = avio_tell(pb);
  441. iff->body_end = iff->body_pos + data_size;
  442. iff->body_size = data_size;
  443. if (chunk_id == ID_DST) {
  444. int ret = read_dst_frame(s, NULL);
  445. if (ret < 0)
  446. return ret;
  447. }
  448. break;
  449. case ID_CHAN:
  450. if (data_size < 4)
  451. return AVERROR_INVALIDDATA;
  452. if (avio_rb32(pb) < 6) {
  453. st->codecpar->channels = 1;
  454. st->codecpar->channel_layout = AV_CH_LAYOUT_MONO;
  455. } else {
  456. st->codecpar->channels = 2;
  457. st->codecpar->channel_layout = AV_CH_LAYOUT_STEREO;
  458. }
  459. break;
  460. case ID_CAMG:
  461. if (data_size < 4)
  462. return AVERROR_INVALIDDATA;
  463. screenmode = avio_rb32(pb);
  464. break;
  465. case ID_CMAP:
  466. if (data_size < 3 || data_size > 768 || data_size % 3) {
  467. av_log(s, AV_LOG_ERROR, "Invalid CMAP chunk size %"PRIu64"\n",
  468. data_size);
  469. return AVERROR_INVALIDDATA;
  470. }
  471. res = ff_alloc_extradata(st->codecpar,
  472. data_size + IFF_EXTRA_VIDEO_SIZE);
  473. if (res < 0)
  474. return res;
  475. if (avio_read(pb, st->codecpar->extradata + IFF_EXTRA_VIDEO_SIZE, data_size) < 0) {
  476. av_freep(&st->codecpar->extradata);
  477. st->codecpar->extradata_size = 0;
  478. return AVERROR(EIO);
  479. }
  480. break;
  481. case ID_BMHD:
  482. st->codecpar->codec_type = AVMEDIA_TYPE_VIDEO;
  483. if (data_size <= 8)
  484. return AVERROR_INVALIDDATA;
  485. st->codecpar->width = avio_rb16(pb);
  486. st->codecpar->height = avio_rb16(pb);
  487. avio_skip(pb, 4); // x, y offset
  488. st->codecpar->bits_per_coded_sample = avio_r8(pb);
  489. if (data_size >= 10)
  490. masking = avio_r8(pb);
  491. if (data_size >= 11)
  492. iff->bitmap_compression = avio_r8(pb);
  493. if (data_size >= 14) {
  494. avio_skip(pb, 1); // padding
  495. transparency = avio_rb16(pb);
  496. }
  497. if (data_size >= 16) {
  498. st->sample_aspect_ratio.num = avio_r8(pb);
  499. st->sample_aspect_ratio.den = avio_r8(pb);
  500. }
  501. break;
  502. case ID_ANHD:
  503. break;
  504. case ID_DPAN:
  505. avio_skip(pb, 2);
  506. st->duration = avio_rb16(pb);
  507. break;
  508. case ID_DPEL:
  509. if (data_size < 4 || (data_size & 3))
  510. return AVERROR_INVALIDDATA;
  511. if ((fmt_size = avio_read(pb, fmt, sizeof(fmt))) < 0)
  512. return fmt_size;
  513. if (fmt_size == sizeof(deep_rgb24) && !memcmp(fmt, deep_rgb24, sizeof(deep_rgb24)))
  514. st->codecpar->format = AV_PIX_FMT_RGB24;
  515. else if (fmt_size == sizeof(deep_rgba) && !memcmp(fmt, deep_rgba, sizeof(deep_rgba)))
  516. st->codecpar->format = AV_PIX_FMT_RGBA;
  517. else if (fmt_size == sizeof(deep_bgra) && !memcmp(fmt, deep_bgra, sizeof(deep_bgra)))
  518. st->codecpar->format = AV_PIX_FMT_BGRA;
  519. else if (fmt_size == sizeof(deep_argb) && !memcmp(fmt, deep_argb, sizeof(deep_argb)))
  520. st->codecpar->format = AV_PIX_FMT_ARGB;
  521. else if (fmt_size == sizeof(deep_abgr) && !memcmp(fmt, deep_abgr, sizeof(deep_abgr)))
  522. st->codecpar->format = AV_PIX_FMT_ABGR;
  523. else {
  524. avpriv_request_sample(s, "color format %.16s", fmt);
  525. return AVERROR_PATCHWELCOME;
  526. }
  527. break;
  528. case ID_DGBL:
  529. st->codecpar->codec_type = AVMEDIA_TYPE_VIDEO;
  530. if (data_size < 8)
  531. return AVERROR_INVALIDDATA;
  532. st->codecpar->width = avio_rb16(pb);
  533. st->codecpar->height = avio_rb16(pb);
  534. iff->bitmap_compression = avio_rb16(pb);
  535. st->sample_aspect_ratio.num = avio_r8(pb);
  536. st->sample_aspect_ratio.den = avio_r8(pb);
  537. st->codecpar->bits_per_coded_sample = 24;
  538. break;
  539. case ID_DLOC:
  540. if (data_size < 4)
  541. return AVERROR_INVALIDDATA;
  542. st->codecpar->width = avio_rb16(pb);
  543. st->codecpar->height = avio_rb16(pb);
  544. break;
  545. case ID_TVDC:
  546. if (data_size < sizeof(iff->tvdc))
  547. return AVERROR_INVALIDDATA;
  548. res = avio_read(pb, iff->tvdc, sizeof(iff->tvdc));
  549. if (res < 0)
  550. return res;
  551. break;
  552. case ID_ANNO:
  553. case ID_TEXT: metadata_tag = "comment"; break;
  554. case ID_AUTH: metadata_tag = "artist"; break;
  555. case ID_COPYRIGHT: metadata_tag = "copyright"; break;
  556. case ID_NAME: metadata_tag = "title"; break;
  557. /* DSD tags */
  558. case MKTAG('F','V','E','R'):
  559. if (data_size < 4)
  560. return AVERROR_INVALIDDATA;
  561. version = avio_rb32(pb);
  562. av_log(s, AV_LOG_DEBUG, "DSIFF v%d.%d.%d.%d\n",version >> 24, (version >> 16) & 0xFF, (version >> 8) & 0xFF, version & 0xFF);
  563. st->codecpar->codec_type = AVMEDIA_TYPE_AUDIO;
  564. break;
  565. case MKTAG('D','I','I','N'):
  566. res = parse_dsd_diin(s, st, orig_pos + data_size);
  567. if (res < 0)
  568. return res;
  569. break;
  570. case MKTAG('P','R','O','P'):
  571. if (data_size < 4)
  572. return AVERROR_INVALIDDATA;
  573. if (avio_rl32(pb) != MKTAG('S','N','D',' ')) {
  574. avpriv_request_sample(s, "unknown property type");
  575. break;
  576. }
  577. res = parse_dsd_prop(s, st, orig_pos + data_size);
  578. if (res < 0)
  579. return res;
  580. break;
  581. case MKTAG('C','O','M','T'):
  582. if (data_size < 2)
  583. return AVERROR_INVALIDDATA;
  584. nb_comments = avio_rb16(pb);
  585. for (i = 0; i < nb_comments; i++) {
  586. int year, mon, day, hour, min, type, ref;
  587. char tmp[24];
  588. const char *tag;
  589. int metadata_size;
  590. year = avio_rb16(pb);
  591. mon = avio_r8(pb);
  592. day = avio_r8(pb);
  593. hour = avio_r8(pb);
  594. min = avio_r8(pb);
  595. snprintf(tmp, sizeof(tmp), "%04d-%02d-%02d %02d:%02d", year, mon, day, hour, min);
  596. av_dict_set(&st->metadata, "comment_time", tmp, 0);
  597. type = avio_rb16(pb);
  598. ref = avio_rb16(pb);
  599. switch (type) {
  600. case 1:
  601. if (!i)
  602. tag = "channel_comment";
  603. else {
  604. snprintf(tmp, sizeof(tmp), "channel%d_comment", ref);
  605. tag = tmp;
  606. }
  607. break;
  608. case 2:
  609. tag = ref < FF_ARRAY_ELEMS(dsd_source_comment) ? dsd_source_comment[ref] : "source_comment";
  610. break;
  611. case 3:
  612. tag = ref < FF_ARRAY_ELEMS(dsd_history_comment) ? dsd_history_comment[ref] : "file_history";
  613. break;
  614. default:
  615. tag = "comment";
  616. }
  617. metadata_size = avio_rb32(pb);
  618. if ((res = get_metadata(s, tag, metadata_size)) < 0) {
  619. av_log(s, AV_LOG_ERROR, "cannot allocate metadata tag %s!\n", tag);
  620. return res;
  621. }
  622. if (metadata_size & 1)
  623. avio_skip(pb, 1);
  624. }
  625. break;
  626. }
  627. if (metadata_tag) {
  628. if ((res = get_metadata(s, metadata_tag, data_size)) < 0) {
  629. av_log(s, AV_LOG_ERROR, "cannot allocate metadata tag %s!\n", metadata_tag);
  630. return res;
  631. }
  632. }
  633. avio_skip(pb, data_size - (avio_tell(pb) - orig_pos) + (data_size & 1));
  634. }
  635. if (st->codecpar->codec_tag == ID_ANIM)
  636. avio_seek(pb, 12, SEEK_SET);
  637. else
  638. avio_seek(pb, iff->body_pos, SEEK_SET);
  639. switch(st->codecpar->codec_type) {
  640. case AVMEDIA_TYPE_AUDIO:
  641. avpriv_set_pts_info(st, 32, 1, st->codecpar->sample_rate);
  642. if (st->codecpar->codec_tag == ID_16SV)
  643. st->codecpar->codec_id = AV_CODEC_ID_PCM_S16BE_PLANAR;
  644. else if (st->codecpar->codec_tag == ID_MAUD) {
  645. if (iff->maud_bits == 8 && !iff->maud_compression) {
  646. st->codecpar->codec_id = AV_CODEC_ID_PCM_U8;
  647. } else if (iff->maud_bits == 16 && !iff->maud_compression) {
  648. st->codecpar->codec_id = AV_CODEC_ID_PCM_S16BE;
  649. } else if (iff->maud_bits == 8 && iff->maud_compression == 2) {
  650. st->codecpar->codec_id = AV_CODEC_ID_PCM_ALAW;
  651. } else if (iff->maud_bits == 8 && iff->maud_compression == 3) {
  652. st->codecpar->codec_id = AV_CODEC_ID_PCM_MULAW;
  653. } else {
  654. avpriv_request_sample(s, "compression %d and bit depth %d", iff->maud_compression, iff->maud_bits);
  655. return AVERROR_PATCHWELCOME;
  656. }
  657. } else if (st->codecpar->codec_tag != ID_DSD &&
  658. st->codecpar->codec_tag != ID_DST) {
  659. switch (iff->svx8_compression) {
  660. case COMP_NONE:
  661. st->codecpar->codec_id = AV_CODEC_ID_PCM_S8_PLANAR;
  662. break;
  663. case COMP_FIB:
  664. st->codecpar->codec_id = AV_CODEC_ID_8SVX_FIB;
  665. break;
  666. case COMP_EXP:
  667. st->codecpar->codec_id = AV_CODEC_ID_8SVX_EXP;
  668. break;
  669. default:
  670. av_log(s, AV_LOG_ERROR,
  671. "Unknown SVX8 compression method '%d'\n", iff->svx8_compression);
  672. return -1;
  673. }
  674. }
  675. st->codecpar->bits_per_coded_sample = av_get_bits_per_sample(st->codecpar->codec_id);
  676. st->codecpar->bit_rate = (int64_t)st->codecpar->channels * st->codecpar->sample_rate * st->codecpar->bits_per_coded_sample;
  677. st->codecpar->block_align = st->codecpar->channels * st->codecpar->bits_per_coded_sample;
  678. if (st->codecpar->codec_tag == ID_DSD && st->codecpar->block_align <= 0)
  679. return AVERROR_INVALIDDATA;
  680. break;
  681. case AVMEDIA_TYPE_VIDEO:
  682. iff->bpp = st->codecpar->bits_per_coded_sample;
  683. if (st->codecpar->codec_tag == ID_ANIM)
  684. avpriv_set_pts_info(st, 32, 1, 60);
  685. if ((screenmode & 0x800 /* Hold And Modify */) && iff->bpp <= 8) {
  686. iff->ham = iff->bpp > 6 ? 6 : 4;
  687. st->codecpar->bits_per_coded_sample = 24;
  688. }
  689. iff->flags = (screenmode & 0x80 /* Extra HalfBrite */) && iff->bpp <= 8;
  690. iff->masking = masking;
  691. iff->transparency = transparency;
  692. if (!st->codecpar->extradata) {
  693. int ret = ff_alloc_extradata(st->codecpar, IFF_EXTRA_VIDEO_SIZE);
  694. if (ret < 0)
  695. return ret;
  696. }
  697. av_assert0(st->codecpar->extradata_size >= IFF_EXTRA_VIDEO_SIZE);
  698. buf = st->codecpar->extradata;
  699. bytestream_put_be16(&buf, IFF_EXTRA_VIDEO_SIZE);
  700. bytestream_put_byte(&buf, iff->bitmap_compression);
  701. bytestream_put_byte(&buf, iff->bpp);
  702. bytestream_put_byte(&buf, iff->ham);
  703. bytestream_put_byte(&buf, iff->flags);
  704. bytestream_put_be16(&buf, iff->transparency);
  705. bytestream_put_byte(&buf, iff->masking);
  706. bytestream_put_buffer(&buf, iff->tvdc, sizeof(iff->tvdc));
  707. st->codecpar->codec_id = AV_CODEC_ID_IFF_ILBM;
  708. break;
  709. default:
  710. return -1;
  711. }
  712. return 0;
  713. }
  714. static unsigned get_anim_duration(uint8_t *buf, int size)
  715. {
  716. GetByteContext gb;
  717. bytestream2_init(&gb, buf, size);
  718. bytestream2_skip(&gb, 4);
  719. while (bytestream2_get_bytes_left(&gb) > 8) {
  720. unsigned chunk = bytestream2_get_le32(&gb);
  721. unsigned size = bytestream2_get_be32(&gb);
  722. if (chunk == ID_ANHD) {
  723. if (size < 40)
  724. break;
  725. bytestream2_skip(&gb, 14);
  726. return bytestream2_get_be32(&gb);
  727. } else {
  728. bytestream2_skip(&gb, size + size & 1);
  729. }
  730. }
  731. return 10;
  732. }
  733. static int iff_read_packet(AVFormatContext *s,
  734. AVPacket *pkt)
  735. {
  736. IffDemuxContext *iff = s->priv_data;
  737. AVIOContext *pb = s->pb;
  738. AVStream *st = s->streams[0];
  739. int ret;
  740. int64_t pos = avio_tell(pb);
  741. if (avio_feof(pb))
  742. return AVERROR_EOF;
  743. if (st->codecpar->codec_tag != ID_ANIM && pos >= iff->body_end)
  744. return AVERROR_EOF;
  745. if (st->codecpar->codec_type == AVMEDIA_TYPE_AUDIO) {
  746. if (st->codecpar->codec_tag == ID_DSD || st->codecpar->codec_tag == ID_MAUD) {
  747. ret = av_get_packet(pb, pkt, FFMIN(iff->body_end - pos, 1024 * st->codecpar->block_align));
  748. } else if (st->codecpar->codec_tag == ID_DST) {
  749. return read_dst_frame(s, pkt);
  750. } else {
  751. if (iff->body_size > INT_MAX || !iff->body_size)
  752. return AVERROR_INVALIDDATA;
  753. ret = av_get_packet(pb, pkt, iff->body_size);
  754. }
  755. } else if (st->codecpar->codec_type == AVMEDIA_TYPE_VIDEO &&
  756. st->codecpar->codec_tag == ID_ANIM) {
  757. uint64_t data_size, orig_pos;
  758. uint32_t chunk_id, chunk_id2;
  759. while (!avio_feof(pb)) {
  760. if (avio_feof(pb))
  761. return AVERROR_EOF;
  762. orig_pos = avio_tell(pb);
  763. chunk_id = avio_rl32(pb);
  764. data_size = avio_rb32(pb);
  765. chunk_id2 = avio_rl32(pb);
  766. if (chunk_id == ID_FORM &&
  767. chunk_id2 == ID_ILBM) {
  768. avio_skip(pb, -4);
  769. break;
  770. } else if (chunk_id == ID_FORM &&
  771. chunk_id2 == ID_ANIM) {
  772. continue;
  773. } else {
  774. avio_skip(pb, data_size);
  775. }
  776. }
  777. ret = av_get_packet(pb, pkt, data_size);
  778. pkt->pos = orig_pos;
  779. pkt->duration = get_anim_duration(pkt->data, pkt->size);
  780. if (pos == 12)
  781. pkt->flags |= AV_PKT_FLAG_KEY;
  782. } else if (st->codecpar->codec_type == AVMEDIA_TYPE_VIDEO &&
  783. st->codecpar->codec_tag != ID_ANIM) {
  784. if (iff->body_size > INT_MAX || !iff->body_size)
  785. return AVERROR_INVALIDDATA;
  786. ret = av_get_packet(pb, pkt, iff->body_size);
  787. pkt->pos = pos;
  788. if (pos == iff->body_pos)
  789. pkt->flags |= AV_PKT_FLAG_KEY;
  790. } else {
  791. av_assert0(0);
  792. }
  793. if (ret < 0)
  794. return ret;
  795. pkt->stream_index = 0;
  796. return ret;
  797. }
  798. AVInputFormat ff_iff_demuxer = {
  799. .name = "iff",
  800. .long_name = NULL_IF_CONFIG_SMALL("IFF (Interchange File Format)"),
  801. .priv_data_size = sizeof(IffDemuxContext),
  802. .read_probe = iff_probe,
  803. .read_header = iff_read_header,
  804. .read_packet = iff_read_packet,
  805. .flags = AVFMT_GENERIC_INDEX | AVFMT_NO_BYTE_SEEK,
  806. };