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.

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