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.

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