/libavformat/iff.c

http://github.com/FFmpeg/FFmpeg · C · 897 lines · 759 code · 99 blank · 39 comment · 215 complexity · c75f3f493faf2b747e10af6698a2a91d MD5 · raw file

  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. switch(chunk_id) {
  397. case ID_VHDR:
  398. st->codecpar->codec_type = AVMEDIA_TYPE_AUDIO;
  399. if (data_size < 14)
  400. return AVERROR_INVALIDDATA;
  401. avio_skip(pb, 12);
  402. st->codecpar->sample_rate = avio_rb16(pb);
  403. if (data_size >= 16) {
  404. avio_skip(pb, 1);
  405. iff->svx8_compression = avio_r8(pb);
  406. }
  407. break;
  408. case ID_MHDR:
  409. st->codecpar->codec_type = AVMEDIA_TYPE_AUDIO;
  410. if (data_size < 32)
  411. return AVERROR_INVALIDDATA;
  412. avio_skip(pb, 4);
  413. iff->maud_bits = avio_rb16(pb);
  414. avio_skip(pb, 2);
  415. num = avio_rb32(pb);
  416. den = avio_rb16(pb);
  417. if (!den)
  418. return AVERROR_INVALIDDATA;
  419. avio_skip(pb, 2);
  420. st->codecpar->sample_rate = num / den;
  421. st->codecpar->channels = avio_rb16(pb);
  422. iff->maud_compression = avio_rb16(pb);
  423. if (st->codecpar->channels == 1)
  424. st->codecpar->channel_layout = AV_CH_LAYOUT_MONO;
  425. else if (st->codecpar->channels == 2)
  426. st->codecpar->channel_layout = AV_CH_LAYOUT_STEREO;
  427. break;
  428. case ID_ABIT:
  429. case ID_BODY:
  430. case ID_DBOD:
  431. case ID_DSD:
  432. case ID_DST:
  433. case ID_MDAT:
  434. iff->body_pos = avio_tell(pb);
  435. iff->body_end = iff->body_pos + data_size;
  436. iff->body_size = data_size;
  437. if (chunk_id == ID_DST) {
  438. int ret = read_dst_frame(s, NULL);
  439. if (ret < 0)
  440. return ret;
  441. }
  442. break;
  443. case ID_CHAN:
  444. if (data_size < 4)
  445. return AVERROR_INVALIDDATA;
  446. if (avio_rb32(pb) < 6) {
  447. st->codecpar->channels = 1;
  448. st->codecpar->channel_layout = AV_CH_LAYOUT_MONO;
  449. } else {
  450. st->codecpar->channels = 2;
  451. st->codecpar->channel_layout = AV_CH_LAYOUT_STEREO;
  452. }
  453. break;
  454. case ID_CAMG:
  455. if (data_size < 4)
  456. return AVERROR_INVALIDDATA;
  457. screenmode = avio_rb32(pb);
  458. break;
  459. case ID_CMAP:
  460. if (data_size < 3 || data_size > 768 || data_size % 3) {
  461. av_log(s, AV_LOG_ERROR, "Invalid CMAP chunk size %"PRIu64"\n",
  462. data_size);
  463. return AVERROR_INVALIDDATA;
  464. }
  465. res = ff_alloc_extradata(st->codecpar,
  466. data_size + IFF_EXTRA_VIDEO_SIZE);
  467. if (res < 0)
  468. return res;
  469. if (avio_read(pb, st->codecpar->extradata + IFF_EXTRA_VIDEO_SIZE, data_size) < 0) {
  470. av_freep(&st->codecpar->extradata);
  471. st->codecpar->extradata_size = 0;
  472. return AVERROR(EIO);
  473. }
  474. break;
  475. case ID_BMHD:
  476. st->codecpar->codec_type = AVMEDIA_TYPE_VIDEO;
  477. if (data_size <= 8)
  478. return AVERROR_INVALIDDATA;
  479. st->codecpar->width = avio_rb16(pb);
  480. st->codecpar->height = avio_rb16(pb);
  481. avio_skip(pb, 4); // x, y offset
  482. st->codecpar->bits_per_coded_sample = avio_r8(pb);
  483. if (data_size >= 10)
  484. masking = avio_r8(pb);
  485. if (data_size >= 11)
  486. iff->bitmap_compression = avio_r8(pb);
  487. if (data_size >= 14) {
  488. avio_skip(pb, 1); // padding
  489. transparency = avio_rb16(pb);
  490. }
  491. if (data_size >= 16) {
  492. st->sample_aspect_ratio.num = avio_r8(pb);
  493. st->sample_aspect_ratio.den = avio_r8(pb);
  494. }
  495. break;
  496. case ID_ANHD:
  497. break;
  498. case ID_DPAN:
  499. avio_skip(pb, 2);
  500. st->duration = avio_rb16(pb);
  501. break;
  502. case ID_DPEL:
  503. if (data_size < 4 || (data_size & 3))
  504. return AVERROR_INVALIDDATA;
  505. if ((fmt_size = avio_read(pb, fmt, sizeof(fmt))) < 0)
  506. return fmt_size;
  507. if (fmt_size == sizeof(deep_rgb24) && !memcmp(fmt, deep_rgb24, sizeof(deep_rgb24)))
  508. st->codecpar->format = AV_PIX_FMT_RGB24;
  509. else if (fmt_size == sizeof(deep_rgba) && !memcmp(fmt, deep_rgba, sizeof(deep_rgba)))
  510. st->codecpar->format = AV_PIX_FMT_RGBA;
  511. else if (fmt_size == sizeof(deep_bgra) && !memcmp(fmt, deep_bgra, sizeof(deep_bgra)))
  512. st->codecpar->format = AV_PIX_FMT_BGRA;
  513. else if (fmt_size == sizeof(deep_argb) && !memcmp(fmt, deep_argb, sizeof(deep_argb)))
  514. st->codecpar->format = AV_PIX_FMT_ARGB;
  515. else if (fmt_size == sizeof(deep_abgr) && !memcmp(fmt, deep_abgr, sizeof(deep_abgr)))
  516. st->codecpar->format = AV_PIX_FMT_ABGR;
  517. else {
  518. avpriv_request_sample(s, "color format %.16s", fmt);
  519. return AVERROR_PATCHWELCOME;
  520. }
  521. break;
  522. case ID_DGBL:
  523. st->codecpar->codec_type = AVMEDIA_TYPE_VIDEO;
  524. if (data_size < 8)
  525. return AVERROR_INVALIDDATA;
  526. st->codecpar->width = avio_rb16(pb);
  527. st->codecpar->height = avio_rb16(pb);
  528. iff->bitmap_compression = avio_rb16(pb);
  529. st->sample_aspect_ratio.num = avio_r8(pb);
  530. st->sample_aspect_ratio.den = avio_r8(pb);
  531. st->codecpar->bits_per_coded_sample = 24;
  532. break;
  533. case ID_DLOC:
  534. if (data_size < 4)
  535. return AVERROR_INVALIDDATA;
  536. st->codecpar->width = avio_rb16(pb);
  537. st->codecpar->height = avio_rb16(pb);
  538. break;
  539. case ID_TVDC:
  540. if (data_size < sizeof(iff->tvdc))
  541. return AVERROR_INVALIDDATA;
  542. res = avio_read(pb, iff->tvdc, sizeof(iff->tvdc));
  543. if (res < 0)
  544. return res;
  545. break;
  546. case ID_ANNO:
  547. case ID_TEXT: metadata_tag = "comment"; break;
  548. case ID_AUTH: metadata_tag = "artist"; break;
  549. case ID_COPYRIGHT: metadata_tag = "copyright"; break;
  550. case ID_NAME: metadata_tag = "title"; break;
  551. /* DSD tags */
  552. case MKTAG('F','V','E','R'):
  553. if (data_size < 4)
  554. return AVERROR_INVALIDDATA;
  555. version = avio_rb32(pb);
  556. av_log(s, AV_LOG_DEBUG, "DSIFF v%d.%d.%d.%d\n",version >> 24, (version >> 16) & 0xFF, (version >> 8) & 0xFF, version & 0xFF);
  557. st->codecpar->codec_type = AVMEDIA_TYPE_AUDIO;
  558. break;
  559. case MKTAG('D','I','I','N'):
  560. res = parse_dsd_diin(s, st, orig_pos + data_size);
  561. if (res < 0)
  562. return res;
  563. break;
  564. case MKTAG('P','R','O','P'):
  565. if (data_size < 4)
  566. return AVERROR_INVALIDDATA;
  567. if (avio_rl32(pb) != MKTAG('S','N','D',' ')) {
  568. avpriv_request_sample(s, "unknown property type");
  569. break;
  570. }
  571. res = parse_dsd_prop(s, st, orig_pos + data_size);
  572. if (res < 0)
  573. return res;
  574. break;
  575. case MKTAG('C','O','M','T'):
  576. if (data_size < 2)
  577. return AVERROR_INVALIDDATA;
  578. nb_comments = avio_rb16(pb);
  579. for (i = 0; i < nb_comments; i++) {
  580. int year, mon, day, hour, min, type, ref;
  581. char tmp[24];
  582. const char *tag;
  583. int metadata_size;
  584. year = avio_rb16(pb);
  585. mon = avio_r8(pb);
  586. day = avio_r8(pb);
  587. hour = avio_r8(pb);
  588. min = avio_r8(pb);
  589. snprintf(tmp, sizeof(tmp), "%04d-%02d-%02d %02d:%02d", year, mon, day, hour, min);
  590. av_dict_set(&st->metadata, "comment_time", tmp, 0);
  591. type = avio_rb16(pb);
  592. ref = avio_rb16(pb);
  593. switch (type) {
  594. case 1:
  595. if (!i)
  596. tag = "channel_comment";
  597. else {
  598. snprintf(tmp, sizeof(tmp), "channel%d_comment", ref);
  599. tag = tmp;
  600. }
  601. break;
  602. case 2:
  603. tag = ref < FF_ARRAY_ELEMS(dsd_source_comment) ? dsd_source_comment[ref] : "source_comment";
  604. break;
  605. case 3:
  606. tag = ref < FF_ARRAY_ELEMS(dsd_history_comment) ? dsd_history_comment[ref] : "file_history";
  607. break;
  608. default:
  609. tag = "comment";
  610. }
  611. metadata_size = avio_rb32(pb);
  612. if ((res = get_metadata(s, tag, metadata_size)) < 0) {
  613. av_log(s, AV_LOG_ERROR, "cannot allocate metadata tag %s!\n", tag);
  614. return res;
  615. }
  616. if (metadata_size & 1)
  617. avio_skip(pb, 1);
  618. }
  619. break;
  620. }
  621. if (metadata_tag) {
  622. if ((res = get_metadata(s, metadata_tag, data_size)) < 0) {
  623. av_log(s, AV_LOG_ERROR, "cannot allocate metadata tag %s!\n", metadata_tag);
  624. return res;
  625. }
  626. }
  627. avio_skip(pb, data_size - (avio_tell(pb) - orig_pos) + (data_size & 1));
  628. }
  629. if (st->codecpar->codec_tag == ID_ANIM)
  630. avio_seek(pb, 12, SEEK_SET);
  631. else
  632. avio_seek(pb, iff->body_pos, SEEK_SET);
  633. switch(st->codecpar->codec_type) {
  634. case AVMEDIA_TYPE_AUDIO:
  635. avpriv_set_pts_info(st, 32, 1, st->codecpar->sample_rate);
  636. if (st->codecpar->codec_tag == ID_16SV)
  637. st->codecpar->codec_id = AV_CODEC_ID_PCM_S16BE_PLANAR;
  638. else if (st->codecpar->codec_tag == ID_MAUD) {
  639. if (iff->maud_bits == 8 && !iff->maud_compression) {
  640. st->codecpar->codec_id = AV_CODEC_ID_PCM_U8;
  641. } else if (iff->maud_bits == 16 && !iff->maud_compression) {
  642. st->codecpar->codec_id = AV_CODEC_ID_PCM_S16BE;
  643. } else if (iff->maud_bits == 8 && iff->maud_compression == 2) {
  644. st->codecpar->codec_id = AV_CODEC_ID_PCM_ALAW;
  645. } else if (iff->maud_bits == 8 && iff->maud_compression == 3) {
  646. st->codecpar->codec_id = AV_CODEC_ID_PCM_MULAW;
  647. } else {
  648. avpriv_request_sample(s, "compression %d and bit depth %d", iff->maud_compression, iff->maud_bits);
  649. return AVERROR_PATCHWELCOME;
  650. }
  651. } else if (st->codecpar->codec_tag != ID_DSD &&
  652. st->codecpar->codec_tag != ID_DST) {
  653. switch (iff->svx8_compression) {
  654. case COMP_NONE:
  655. st->codecpar->codec_id = AV_CODEC_ID_PCM_S8_PLANAR;
  656. break;
  657. case COMP_FIB:
  658. st->codecpar->codec_id = AV_CODEC_ID_8SVX_FIB;
  659. break;
  660. case COMP_EXP:
  661. st->codecpar->codec_id = AV_CODEC_ID_8SVX_EXP;
  662. break;
  663. default:
  664. av_log(s, AV_LOG_ERROR,
  665. "Unknown SVX8 compression method '%d'\n", iff->svx8_compression);
  666. return -1;
  667. }
  668. }
  669. st->codecpar->bits_per_coded_sample = av_get_bits_per_sample(st->codecpar->codec_id);
  670. st->codecpar->bit_rate = (int64_t)st->codecpar->channels * st->codecpar->sample_rate * st->codecpar->bits_per_coded_sample;
  671. st->codecpar->block_align = st->codecpar->channels * st->codecpar->bits_per_coded_sample;
  672. if (st->codecpar->codec_tag == ID_DSD && st->codecpar->block_align <= 0)
  673. return AVERROR_INVALIDDATA;
  674. break;
  675. case AVMEDIA_TYPE_VIDEO:
  676. iff->bpp = st->codecpar->bits_per_coded_sample;
  677. if (st->codecpar->codec_tag == ID_ANIM)
  678. avpriv_set_pts_info(st, 32, 1, 60);
  679. if ((screenmode & 0x800 /* Hold And Modify */) && iff->bpp <= 8) {
  680. iff->ham = iff->bpp > 6 ? 6 : 4;
  681. st->codecpar->bits_per_coded_sample = 24;
  682. }
  683. iff->flags = (screenmode & 0x80 /* Extra HalfBrite */) && iff->bpp <= 8;
  684. iff->masking = masking;
  685. iff->transparency = transparency;
  686. if (!st->codecpar->extradata) {
  687. int ret = ff_alloc_extradata(st->codecpar, IFF_EXTRA_VIDEO_SIZE);
  688. if (ret < 0)
  689. return ret;
  690. }
  691. av_assert0(st->codecpar->extradata_size >= IFF_EXTRA_VIDEO_SIZE);
  692. buf = st->codecpar->extradata;
  693. bytestream_put_be16(&buf, IFF_EXTRA_VIDEO_SIZE);
  694. bytestream_put_byte(&buf, iff->bitmap_compression);
  695. bytestream_put_byte(&buf, iff->bpp);
  696. bytestream_put_byte(&buf, iff->ham);
  697. bytestream_put_byte(&buf, iff->flags);
  698. bytestream_put_be16(&buf, iff->transparency);
  699. bytestream_put_byte(&buf, iff->masking);
  700. bytestream_put_buffer(&buf, iff->tvdc, sizeof(iff->tvdc));
  701. st->codecpar->codec_id = AV_CODEC_ID_IFF_ILBM;
  702. break;
  703. default:
  704. return -1;
  705. }
  706. return 0;
  707. }
  708. static unsigned get_anim_duration(uint8_t *buf, int size)
  709. {
  710. GetByteContext gb;
  711. bytestream2_init(&gb, buf, size);
  712. bytestream2_skip(&gb, 4);
  713. while (bytestream2_get_bytes_left(&gb) > 8) {
  714. unsigned chunk = bytestream2_get_le32(&gb);
  715. unsigned size = bytestream2_get_be32(&gb);
  716. if (chunk == ID_ANHD) {
  717. if (size < 40)
  718. break;
  719. bytestream2_skip(&gb, 14);
  720. return bytestream2_get_be32(&gb);
  721. } else {
  722. bytestream2_skip(&gb, size + size & 1);
  723. }
  724. }
  725. return 10;
  726. }
  727. static int iff_read_packet(AVFormatContext *s,
  728. AVPacket *pkt)
  729. {
  730. IffDemuxContext *iff = s->priv_data;
  731. AVIOContext *pb = s->pb;
  732. AVStream *st = s->streams[0];
  733. int ret;
  734. int64_t pos = avio_tell(pb);
  735. if (avio_feof(pb))
  736. return AVERROR_EOF;
  737. if (st->codecpar->codec_tag != ID_ANIM && pos >= iff->body_end)
  738. return AVERROR_EOF;
  739. if (st->codecpar->codec_type == AVMEDIA_TYPE_AUDIO) {
  740. if (st->codecpar->codec_tag == ID_DSD || st->codecpar->codec_tag == ID_MAUD) {
  741. ret = av_get_packet(pb, pkt, FFMIN(iff->body_end - pos, 1024 * st->codecpar->block_align));
  742. } else if (st->codecpar->codec_tag == ID_DST) {
  743. return read_dst_frame(s, pkt);
  744. } else {
  745. if (iff->body_size > INT_MAX)
  746. return AVERROR_INVALIDDATA;
  747. ret = av_get_packet(pb, pkt, iff->body_size);
  748. }
  749. } else if (st->codecpar->codec_type == AVMEDIA_TYPE_VIDEO &&
  750. st->codecpar->codec_tag == ID_ANIM) {
  751. uint64_t data_size, orig_pos;
  752. uint32_t chunk_id, chunk_id2;
  753. while (!avio_feof(pb)) {
  754. if (avio_feof(pb))
  755. return AVERROR_EOF;
  756. orig_pos = avio_tell(pb);
  757. chunk_id = avio_rl32(pb);
  758. data_size = avio_rb32(pb);
  759. chunk_id2 = avio_rl32(pb);
  760. if (chunk_id == ID_FORM &&
  761. chunk_id2 == ID_ILBM) {
  762. avio_skip(pb, -4);
  763. break;
  764. } else if (chunk_id == ID_FORM &&
  765. chunk_id2 == ID_ANIM) {
  766. continue;
  767. } else {
  768. avio_skip(pb, data_size);
  769. }
  770. }
  771. ret = av_get_packet(pb, pkt, data_size);
  772. pkt->pos = orig_pos;
  773. pkt->duration = get_anim_duration(pkt->data, pkt->size);
  774. if (pos == 12)
  775. pkt->flags |= AV_PKT_FLAG_KEY;
  776. } else if (st->codecpar->codec_type == AVMEDIA_TYPE_VIDEO &&
  777. st->codecpar->codec_tag != ID_ANIM) {
  778. ret = av_get_packet(pb, pkt, iff->body_size);
  779. pkt->pos = pos;
  780. if (pos == iff->body_pos)
  781. pkt->flags |= AV_PKT_FLAG_KEY;
  782. } else {
  783. av_assert0(0);
  784. }
  785. if (ret < 0)
  786. return ret;
  787. pkt->stream_index = 0;
  788. return ret;
  789. }
  790. AVInputFormat ff_iff_demuxer = {
  791. .name = "iff",
  792. .long_name = NULL_IF_CONFIG_SMALL("IFF (Interchange File Format)"),
  793. .priv_data_size = sizeof(IffDemuxContext),
  794. .read_probe = iff_probe,
  795. .read_header = iff_read_header,
  796. .read_packet = iff_read_packet,
  797. .flags = AVFMT_GENERIC_INDEX | AVFMT_NO_BYTE_SEEK,
  798. };