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.

490 lines
18KB

  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 "libavutil/avassert.h"
  30. #include "libavutil/channel_layout.h"
  31. #include "libavutil/intreadwrite.h"
  32. #include "libavutil/dict.h"
  33. #include "libavcodec/bytestream.h"
  34. #include "avformat.h"
  35. #include "internal.h"
  36. #define ID_8SVX MKTAG('8','S','V','X')
  37. #define ID_16SV MKTAG('1','6','S','V')
  38. #define ID_MAUD MKTAG('M','A','U','D')
  39. #define ID_MHDR MKTAG('M','H','D','R')
  40. #define ID_MDAT MKTAG('M','D','A','T')
  41. #define ID_VHDR MKTAG('V','H','D','R')
  42. #define ID_ATAK MKTAG('A','T','A','K')
  43. #define ID_RLSE MKTAG('R','L','S','E')
  44. #define ID_CHAN MKTAG('C','H','A','N')
  45. #define ID_PBM MKTAG('P','B','M',' ')
  46. #define ID_ILBM MKTAG('I','L','B','M')
  47. #define ID_BMHD MKTAG('B','M','H','D')
  48. #define ID_DGBL MKTAG('D','G','B','L')
  49. #define ID_CAMG MKTAG('C','A','M','G')
  50. #define ID_CMAP MKTAG('C','M','A','P')
  51. #define ID_ACBM MKTAG('A','C','B','M')
  52. #define ID_DEEP MKTAG('D','E','E','P')
  53. #define ID_RGB8 MKTAG('R','G','B','8')
  54. #define ID_RGBN MKTAG('R','G','B','N')
  55. #define ID_FORM MKTAG('F','O','R','M')
  56. #define ID_ANNO MKTAG('A','N','N','O')
  57. #define ID_AUTH MKTAG('A','U','T','H')
  58. #define ID_CHRS MKTAG('C','H','R','S')
  59. #define ID_COPYRIGHT MKTAG('(','c',')',' ')
  60. #define ID_CSET MKTAG('C','S','E','T')
  61. #define ID_FVER MKTAG('F','V','E','R')
  62. #define ID_NAME MKTAG('N','A','M','E')
  63. #define ID_TEXT MKTAG('T','E','X','T')
  64. #define ID_ABIT MKTAG('A','B','I','T')
  65. #define ID_BODY MKTAG('B','O','D','Y')
  66. #define ID_DBOD MKTAG('D','B','O','D')
  67. #define ID_DPEL MKTAG('D','P','E','L')
  68. #define ID_DLOC MKTAG('D','L','O','C')
  69. #define ID_TVDC MKTAG('T','V','D','C')
  70. #define LEFT 2
  71. #define RIGHT 4
  72. #define STEREO 6
  73. /**
  74. * This number of bytes if added at the beginning of each AVPacket
  75. * which contain additional information about video properties
  76. * which has to be shared between demuxer and decoder.
  77. * This number may change between frames, e.g. the demuxer might
  78. * set it to smallest possible size of 2 to indicate that there's
  79. * no extradata changing in this frame.
  80. */
  81. #define IFF_EXTRA_VIDEO_SIZE 41
  82. typedef enum {
  83. COMP_NONE,
  84. COMP_FIB,
  85. COMP_EXP
  86. } svx8_compression_type;
  87. typedef struct {
  88. int64_t body_pos;
  89. int64_t body_end;
  90. uint32_t body_size;
  91. svx8_compression_type svx8_compression;
  92. unsigned maud_bits;
  93. unsigned maud_compression;
  94. unsigned bitmap_compression; ///< delta compression method used
  95. unsigned bpp; ///< bits per plane to decode (differs from bits_per_coded_sample if HAM)
  96. unsigned ham; ///< 0 if non-HAM or number of hold bits (6 for bpp > 6, 4 otherwise)
  97. unsigned flags; ///< 1 for EHB, 0 is no extra half darkening
  98. unsigned transparency; ///< transparency color index in palette
  99. unsigned masking; ///< masking method used
  100. uint8_t tvdc[32]; ///< TVDC lookup table
  101. } IffDemuxContext;
  102. /* Metadata string read */
  103. static int get_metadata(AVFormatContext *s,
  104. const char *const tag,
  105. const unsigned data_size)
  106. {
  107. uint8_t *buf = ((data_size + 1) == 0) ? NULL : av_malloc(data_size + 1);
  108. if (!buf)
  109. return AVERROR(ENOMEM);
  110. if (avio_read(s->pb, buf, data_size) != data_size) {
  111. av_free(buf);
  112. return AVERROR(EIO);
  113. }
  114. buf[data_size] = 0;
  115. av_dict_set(&s->metadata, tag, buf, AV_DICT_DONT_STRDUP_VAL);
  116. return 0;
  117. }
  118. static int iff_probe(AVProbeData *p)
  119. {
  120. const uint8_t *d = p->buf;
  121. if ( AV_RL32(d) == ID_FORM &&
  122. (AV_RL32(d+8) == ID_8SVX ||
  123. AV_RL32(d+8) == ID_16SV ||
  124. AV_RL32(d+8) == ID_MAUD ||
  125. AV_RL32(d+8) == ID_PBM ||
  126. AV_RL32(d+8) == ID_ACBM ||
  127. AV_RL32(d+8) == ID_DEEP ||
  128. AV_RL32(d+8) == ID_ILBM ||
  129. AV_RL32(d+8) == ID_RGB8 ||
  130. AV_RL32(d+8) == ID_RGBN) )
  131. return AVPROBE_SCORE_MAX;
  132. return 0;
  133. }
  134. static const uint8_t deep_rgb24[] = {0, 0, 0, 3, 0, 1, 0, 8, 0, 2, 0, 8, 0, 3, 0, 8};
  135. static const uint8_t deep_rgba[] = {0, 0, 0, 4, 0, 1, 0, 8, 0, 2, 0, 8, 0, 3, 0, 8};
  136. static const uint8_t deep_bgra[] = {0, 0, 0, 4, 0, 3, 0, 8, 0, 2, 0, 8, 0, 1, 0, 8};
  137. static const uint8_t deep_argb[] = {0, 0, 0, 4, 0,17, 0, 8, 0, 1, 0, 8, 0, 2, 0, 8};
  138. static const uint8_t deep_abgr[] = {0, 0, 0, 4, 0,17, 0, 8, 0, 3, 0, 8, 0, 2, 0, 8};
  139. static int iff_read_header(AVFormatContext *s)
  140. {
  141. IffDemuxContext *iff = s->priv_data;
  142. AVIOContext *pb = s->pb;
  143. AVStream *st;
  144. uint8_t *buf;
  145. uint32_t chunk_id, data_size;
  146. uint32_t screenmode = 0, num, den;
  147. unsigned transparency = 0;
  148. unsigned masking = 0; // no mask
  149. uint8_t fmt[16];
  150. int fmt_size;
  151. st = avformat_new_stream(s, NULL);
  152. if (!st)
  153. return AVERROR(ENOMEM);
  154. st->codec->channels = 1;
  155. st->codec->channel_layout = AV_CH_LAYOUT_MONO;
  156. avio_skip(pb, 8);
  157. // codec_tag used by ByteRun1 decoder to distinguish progressive (PBM) and interlaced (ILBM) content
  158. st->codec->codec_tag = avio_rl32(pb);
  159. iff->bitmap_compression = -1;
  160. iff->svx8_compression = -1;
  161. iff->maud_bits = -1;
  162. iff->maud_compression = -1;
  163. while(!url_feof(pb)) {
  164. uint64_t orig_pos;
  165. int res;
  166. const char *metadata_tag = NULL;
  167. chunk_id = avio_rl32(pb);
  168. data_size = avio_rb32(pb);
  169. orig_pos = avio_tell(pb);
  170. switch(chunk_id) {
  171. case ID_VHDR:
  172. st->codec->codec_type = AVMEDIA_TYPE_AUDIO;
  173. if (data_size < 14)
  174. return AVERROR_INVALIDDATA;
  175. avio_skip(pb, 12);
  176. st->codec->sample_rate = avio_rb16(pb);
  177. if (data_size >= 16) {
  178. avio_skip(pb, 1);
  179. iff->svx8_compression = avio_r8(pb);
  180. }
  181. break;
  182. case ID_MHDR:
  183. st->codec->codec_type = AVMEDIA_TYPE_AUDIO;
  184. if (data_size < 32)
  185. return AVERROR_INVALIDDATA;
  186. avio_skip(pb, 4);
  187. iff->maud_bits = avio_rb16(pb);
  188. avio_skip(pb, 2);
  189. num = avio_rb32(pb);
  190. den = avio_rb16(pb);
  191. if (!den)
  192. return AVERROR_INVALIDDATA;
  193. avio_skip(pb, 2);
  194. st->codec->sample_rate = num / den;
  195. st->codec->channels = avio_rb16(pb);
  196. iff->maud_compression = avio_rb16(pb);
  197. if (st->codec->channels == 1)
  198. st->codec->channel_layout = AV_CH_LAYOUT_MONO;
  199. else if (st->codec->channels == 2)
  200. st->codec->channel_layout = AV_CH_LAYOUT_STEREO;
  201. break;
  202. case ID_ABIT:
  203. case ID_BODY:
  204. case ID_DBOD:
  205. case ID_MDAT:
  206. iff->body_pos = avio_tell(pb);
  207. iff->body_end = iff->body_pos + data_size;
  208. iff->body_size = data_size;
  209. break;
  210. case ID_CHAN:
  211. if (data_size < 4)
  212. return AVERROR_INVALIDDATA;
  213. if (avio_rb32(pb) < 6) {
  214. st->codec->channels = 1;
  215. st->codec->channel_layout = AV_CH_LAYOUT_MONO;
  216. } else {
  217. st->codec->channels = 2;
  218. st->codec->channel_layout = AV_CH_LAYOUT_STEREO;
  219. }
  220. break;
  221. case ID_CAMG:
  222. if (data_size < 4)
  223. return AVERROR_INVALIDDATA;
  224. screenmode = avio_rb32(pb);
  225. break;
  226. case ID_CMAP:
  227. if (data_size < 3 || data_size > 768 || data_size % 3) {
  228. av_log(s, AV_LOG_ERROR, "Invalid CMAP chunk size %d\n",
  229. data_size);
  230. return AVERROR_INVALIDDATA;
  231. }
  232. st->codec->extradata_size = data_size + IFF_EXTRA_VIDEO_SIZE;
  233. st->codec->extradata = av_malloc(data_size + IFF_EXTRA_VIDEO_SIZE + FF_INPUT_BUFFER_PADDING_SIZE);
  234. if (!st->codec->extradata)
  235. return AVERROR(ENOMEM);
  236. if (avio_read(pb, st->codec->extradata + IFF_EXTRA_VIDEO_SIZE, data_size) < 0)
  237. return AVERROR(EIO);
  238. break;
  239. case ID_BMHD:
  240. st->codec->codec_type = AVMEDIA_TYPE_VIDEO;
  241. if (data_size <= 8)
  242. return AVERROR_INVALIDDATA;
  243. st->codec->width = avio_rb16(pb);
  244. st->codec->height = avio_rb16(pb);
  245. avio_skip(pb, 4); // x, y offset
  246. st->codec->bits_per_coded_sample = avio_r8(pb);
  247. if (data_size >= 10)
  248. masking = avio_r8(pb);
  249. if (data_size >= 11)
  250. iff->bitmap_compression = avio_r8(pb);
  251. if (data_size >= 14) {
  252. avio_skip(pb, 1); // padding
  253. transparency = avio_rb16(pb);
  254. }
  255. if (data_size >= 16) {
  256. st->sample_aspect_ratio.num = avio_r8(pb);
  257. st->sample_aspect_ratio.den = avio_r8(pb);
  258. }
  259. break;
  260. case ID_DPEL:
  261. if (data_size < 4 || (data_size & 3))
  262. return AVERROR_INVALIDDATA;
  263. if ((fmt_size = avio_read(pb, fmt, sizeof(fmt))) < 0)
  264. return fmt_size;
  265. if (fmt_size == sizeof(deep_rgb24) && !memcmp(fmt, deep_rgb24, sizeof(deep_rgb24)))
  266. st->codec->pix_fmt = AV_PIX_FMT_RGB24;
  267. else if (fmt_size == sizeof(deep_rgba) && !memcmp(fmt, deep_rgba, sizeof(deep_rgba)))
  268. st->codec->pix_fmt = AV_PIX_FMT_RGBA;
  269. else if (fmt_size == sizeof(deep_bgra) && !memcmp(fmt, deep_bgra, sizeof(deep_bgra)))
  270. st->codec->pix_fmt = AV_PIX_FMT_BGRA;
  271. else if (fmt_size == sizeof(deep_argb) && !memcmp(fmt, deep_argb, sizeof(deep_argb)))
  272. st->codec->pix_fmt = AV_PIX_FMT_ARGB;
  273. else if (fmt_size == sizeof(deep_abgr) && !memcmp(fmt, deep_abgr, sizeof(deep_abgr)))
  274. st->codec->pix_fmt = AV_PIX_FMT_ABGR;
  275. else {
  276. avpriv_request_sample(s, "color format %.16s", fmt);
  277. return AVERROR_PATCHWELCOME;
  278. }
  279. break;
  280. case ID_DGBL:
  281. st->codec->codec_type = AVMEDIA_TYPE_VIDEO;
  282. if (data_size < 8)
  283. return AVERROR_INVALIDDATA;
  284. st->codec->width = avio_rb16(pb);
  285. st->codec->height = avio_rb16(pb);
  286. iff->bitmap_compression = avio_rb16(pb);
  287. st->sample_aspect_ratio.num = avio_r8(pb);
  288. st->sample_aspect_ratio.den = avio_r8(pb);
  289. st->codec->bits_per_coded_sample = 24;
  290. break;
  291. case ID_DLOC:
  292. if (data_size < 4)
  293. return AVERROR_INVALIDDATA;
  294. st->codec->width = avio_rb16(pb);
  295. st->codec->height = avio_rb16(pb);
  296. break;
  297. case ID_TVDC:
  298. if (data_size < sizeof(iff->tvdc))
  299. return AVERROR_INVALIDDATA;
  300. res = avio_read(pb, iff->tvdc, sizeof(iff->tvdc));
  301. if (res < 0)
  302. return res;
  303. break;
  304. case ID_ANNO:
  305. case ID_TEXT: metadata_tag = "comment"; break;
  306. case ID_AUTH: metadata_tag = "artist"; break;
  307. case ID_COPYRIGHT: metadata_tag = "copyright"; break;
  308. case ID_NAME: metadata_tag = "title"; break;
  309. }
  310. if (metadata_tag) {
  311. if ((res = get_metadata(s, metadata_tag, data_size)) < 0) {
  312. av_log(s, AV_LOG_ERROR, "cannot allocate metadata tag %s!\n", metadata_tag);
  313. return res;
  314. }
  315. }
  316. avio_skip(pb, data_size - (avio_tell(pb) - orig_pos) + (data_size & 1));
  317. }
  318. avio_seek(pb, iff->body_pos, SEEK_SET);
  319. switch(st->codec->codec_type) {
  320. case AVMEDIA_TYPE_AUDIO:
  321. avpriv_set_pts_info(st, 32, 1, st->codec->sample_rate);
  322. if (st->codec->codec_tag == ID_16SV)
  323. st->codec->codec_id = AV_CODEC_ID_PCM_S16BE_PLANAR;
  324. else if (st->codec->codec_tag == ID_MAUD) {
  325. if (iff->maud_bits == 8 && !iff->maud_compression) {
  326. st->codec->codec_id = AV_CODEC_ID_PCM_U8;
  327. } else if (iff->maud_bits == 16 && !iff->maud_compression) {
  328. st->codec->codec_id = AV_CODEC_ID_PCM_S16BE;
  329. } else if (iff->maud_bits == 8 && iff->maud_compression == 2) {
  330. st->codec->codec_id = AV_CODEC_ID_PCM_ALAW;
  331. } else if (iff->maud_bits == 8 && iff->maud_compression == 3) {
  332. st->codec->codec_id = AV_CODEC_ID_PCM_MULAW;
  333. } else {
  334. avpriv_request_sample(s, "compression %d and bit depth %d", iff->maud_compression, iff->maud_bits);
  335. return AVERROR_PATCHWELCOME;
  336. }
  337. st->codec->bits_per_coded_sample =
  338. av_get_bits_per_sample(st->codec->codec_id);
  339. st->codec->block_align =
  340. st->codec->bits_per_coded_sample * st->codec->channels / 8;
  341. } else {
  342. switch (iff->svx8_compression) {
  343. case COMP_NONE:
  344. st->codec->codec_id = AV_CODEC_ID_PCM_S8_PLANAR;
  345. break;
  346. case COMP_FIB:
  347. st->codec->codec_id = AV_CODEC_ID_8SVX_FIB;
  348. break;
  349. case COMP_EXP:
  350. st->codec->codec_id = AV_CODEC_ID_8SVX_EXP;
  351. break;
  352. default:
  353. av_log(s, AV_LOG_ERROR,
  354. "Unknown SVX8 compression method '%d'\n", iff->svx8_compression);
  355. return -1;
  356. }
  357. }
  358. st->codec->bits_per_coded_sample = av_get_bits_per_sample(st->codec->codec_id);
  359. st->codec->bit_rate = st->codec->channels * st->codec->sample_rate * st->codec->bits_per_coded_sample;
  360. st->codec->block_align = st->codec->channels * st->codec->bits_per_coded_sample;
  361. break;
  362. case AVMEDIA_TYPE_VIDEO:
  363. iff->bpp = st->codec->bits_per_coded_sample;
  364. if ((screenmode & 0x800 /* Hold And Modify */) && iff->bpp <= 8) {
  365. iff->ham = iff->bpp > 6 ? 6 : 4;
  366. st->codec->bits_per_coded_sample = 24;
  367. }
  368. iff->flags = (screenmode & 0x80 /* Extra HalfBrite */) && iff->bpp <= 8;
  369. iff->masking = masking;
  370. iff->transparency = transparency;
  371. if (!st->codec->extradata) {
  372. st->codec->extradata_size = IFF_EXTRA_VIDEO_SIZE;
  373. st->codec->extradata = av_malloc(IFF_EXTRA_VIDEO_SIZE + FF_INPUT_BUFFER_PADDING_SIZE);
  374. if (!st->codec->extradata)
  375. return AVERROR(ENOMEM);
  376. }
  377. av_assert0(st->codec->extradata_size >= IFF_EXTRA_VIDEO_SIZE);
  378. buf = st->codec->extradata;
  379. bytestream_put_be16(&buf, IFF_EXTRA_VIDEO_SIZE);
  380. bytestream_put_byte(&buf, iff->bitmap_compression);
  381. bytestream_put_byte(&buf, iff->bpp);
  382. bytestream_put_byte(&buf, iff->ham);
  383. bytestream_put_byte(&buf, iff->flags);
  384. bytestream_put_be16(&buf, iff->transparency);
  385. bytestream_put_byte(&buf, iff->masking);
  386. bytestream_put_buffer(&buf, iff->tvdc, sizeof(iff->tvdc));
  387. st->codec->codec_id = AV_CODEC_ID_IFF_ILBM;
  388. break;
  389. default:
  390. return -1;
  391. }
  392. return 0;
  393. }
  394. static int iff_read_packet(AVFormatContext *s,
  395. AVPacket *pkt)
  396. {
  397. IffDemuxContext *iff = s->priv_data;
  398. AVIOContext *pb = s->pb;
  399. AVStream *st = s->streams[0];
  400. int ret;
  401. int64_t pos = avio_tell(pb);
  402. if (pos >= iff->body_end)
  403. return AVERROR_EOF;
  404. if (st->codec->codec_type == AVMEDIA_TYPE_AUDIO) {
  405. if (st->codec->codec_tag == ID_MAUD) {
  406. ret = av_get_packet(pb, pkt, FFMIN(iff->body_end - pos, 1024 * st->codec->block_align));
  407. } else {
  408. ret = av_get_packet(pb, pkt, iff->body_size);
  409. }
  410. } else if (st->codec->codec_type == AVMEDIA_TYPE_VIDEO) {
  411. uint8_t *buf;
  412. if (av_new_packet(pkt, iff->body_size + 2) < 0) {
  413. return AVERROR(ENOMEM);
  414. }
  415. buf = pkt->data;
  416. bytestream_put_be16(&buf, 2);
  417. ret = avio_read(pb, buf, iff->body_size);
  418. if (ret<0) {
  419. av_free_packet(pkt);
  420. } else if (ret < iff->body_size)
  421. av_shrink_packet(pkt, ret + 2);
  422. } else {
  423. av_assert0(0);
  424. }
  425. if (pos == iff->body_pos)
  426. pkt->flags |= AV_PKT_FLAG_KEY;
  427. if (ret < 0)
  428. return ret;
  429. pkt->stream_index = 0;
  430. return ret;
  431. }
  432. AVInputFormat ff_iff_demuxer = {
  433. .name = "iff",
  434. .long_name = NULL_IF_CONFIG_SMALL("IFF (Interchange File Format)"),
  435. .priv_data_size = sizeof(IffDemuxContext),
  436. .read_probe = iff_probe,
  437. .read_header = iff_read_header,
  438. .read_packet = iff_read_packet,
  439. .flags = AVFMT_GENERIC_INDEX | AVFMT_NO_BYTE_SEEK,
  440. };