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.

479 lines
17KB

  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. uint64_t body_pos;
  89. uint32_t body_size;
  90. uint32_t sent_bytes;
  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) < 0) {
  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_size = data_size;
  208. break;
  209. case ID_CHAN:
  210. if (data_size < 4)
  211. return AVERROR_INVALIDDATA;
  212. if (avio_rb32(pb) < 6) {
  213. st->codec->channels = 1;
  214. st->codec->channel_layout = AV_CH_LAYOUT_MONO;
  215. } else {
  216. st->codec->channels = 2;
  217. st->codec->channel_layout = AV_CH_LAYOUT_STEREO;
  218. }
  219. break;
  220. case ID_CAMG:
  221. if (data_size < 4)
  222. return AVERROR_INVALIDDATA;
  223. screenmode = avio_rb32(pb);
  224. break;
  225. case ID_CMAP:
  226. st->codec->extradata_size = data_size + IFF_EXTRA_VIDEO_SIZE;
  227. st->codec->extradata = av_malloc(data_size + IFF_EXTRA_VIDEO_SIZE + FF_INPUT_BUFFER_PADDING_SIZE);
  228. if (!st->codec->extradata)
  229. return AVERROR(ENOMEM);
  230. if (avio_read(pb, st->codec->extradata + IFF_EXTRA_VIDEO_SIZE, data_size) < 0)
  231. return AVERROR(EIO);
  232. break;
  233. case ID_BMHD:
  234. st->codec->codec_type = AVMEDIA_TYPE_VIDEO;
  235. if (data_size <= 8)
  236. return AVERROR_INVALIDDATA;
  237. st->codec->width = avio_rb16(pb);
  238. st->codec->height = avio_rb16(pb);
  239. avio_skip(pb, 4); // x, y offset
  240. st->codec->bits_per_coded_sample = avio_r8(pb);
  241. if (data_size >= 10)
  242. masking = avio_r8(pb);
  243. if (data_size >= 11)
  244. iff->bitmap_compression = avio_r8(pb);
  245. if (data_size >= 14) {
  246. avio_skip(pb, 1); // padding
  247. transparency = avio_rb16(pb);
  248. }
  249. if (data_size >= 16) {
  250. st->sample_aspect_ratio.num = avio_r8(pb);
  251. st->sample_aspect_ratio.den = avio_r8(pb);
  252. }
  253. break;
  254. case ID_DPEL:
  255. if (data_size < 4 || (data_size & 3))
  256. return AVERROR_INVALIDDATA;
  257. if ((fmt_size = avio_read(pb, fmt, sizeof(fmt))) < 0)
  258. return fmt_size;
  259. if (fmt_size == sizeof(deep_rgb24) && !memcmp(fmt, deep_rgb24, sizeof(deep_rgb24)))
  260. st->codec->pix_fmt = AV_PIX_FMT_RGB24;
  261. else if (fmt_size == sizeof(deep_rgba) && !memcmp(fmt, deep_rgba, sizeof(deep_rgba)))
  262. st->codec->pix_fmt = AV_PIX_FMT_RGBA;
  263. else if (fmt_size == sizeof(deep_bgra) && !memcmp(fmt, deep_bgra, sizeof(deep_bgra)))
  264. st->codec->pix_fmt = AV_PIX_FMT_BGRA;
  265. else if (fmt_size == sizeof(deep_argb) && !memcmp(fmt, deep_argb, sizeof(deep_argb)))
  266. st->codec->pix_fmt = AV_PIX_FMT_ARGB;
  267. else if (fmt_size == sizeof(deep_abgr) && !memcmp(fmt, deep_abgr, sizeof(deep_abgr)))
  268. st->codec->pix_fmt = AV_PIX_FMT_ABGR;
  269. else {
  270. av_log_ask_for_sample(s, "unsupported color format\n");
  271. return AVERROR_PATCHWELCOME;
  272. }
  273. break;
  274. case ID_DGBL:
  275. st->codec->codec_type = AVMEDIA_TYPE_VIDEO;
  276. if (data_size < 8)
  277. return AVERROR_INVALIDDATA;
  278. st->codec->width = avio_rb16(pb);
  279. st->codec->height = avio_rb16(pb);
  280. iff->bitmap_compression = avio_rb16(pb);
  281. st->sample_aspect_ratio.num = avio_r8(pb);
  282. st->sample_aspect_ratio.den = avio_r8(pb);
  283. st->codec->bits_per_coded_sample = 24;
  284. break;
  285. case ID_DLOC:
  286. if (data_size < 4)
  287. return AVERROR_INVALIDDATA;
  288. st->codec->width = avio_rb16(pb);
  289. st->codec->height = avio_rb16(pb);
  290. break;
  291. case ID_TVDC:
  292. if (data_size < sizeof(iff->tvdc))
  293. return AVERROR_INVALIDDATA;
  294. res = avio_read(pb, iff->tvdc, sizeof(iff->tvdc));
  295. if (res < 0)
  296. return res;
  297. break;
  298. case ID_ANNO:
  299. case ID_TEXT: metadata_tag = "comment"; break;
  300. case ID_AUTH: metadata_tag = "artist"; break;
  301. case ID_COPYRIGHT: metadata_tag = "copyright"; break;
  302. case ID_NAME: metadata_tag = "title"; break;
  303. }
  304. if (metadata_tag) {
  305. if ((res = get_metadata(s, metadata_tag, data_size)) < 0) {
  306. av_log(s, AV_LOG_ERROR, "cannot allocate metadata tag %s!\n", metadata_tag);
  307. return res;
  308. }
  309. }
  310. avio_skip(pb, data_size - (avio_tell(pb) - orig_pos) + (data_size & 1));
  311. }
  312. avio_seek(pb, iff->body_pos, SEEK_SET);
  313. switch(st->codec->codec_type) {
  314. case AVMEDIA_TYPE_AUDIO:
  315. avpriv_set_pts_info(st, 32, 1, st->codec->sample_rate);
  316. if (st->codec->codec_tag == ID_16SV)
  317. st->codec->codec_id = AV_CODEC_ID_PCM_S16BE_PLANAR;
  318. else if (st->codec->codec_tag == ID_MAUD) {
  319. if (iff->maud_bits == 8 && !iff->maud_compression) {
  320. st->codec->codec_id = AV_CODEC_ID_PCM_U8;
  321. } else if (iff->maud_bits == 16 && !iff->maud_compression) {
  322. st->codec->codec_id = AV_CODEC_ID_PCM_S16BE;
  323. } else if (iff->maud_bits == 8 && iff->maud_compression == 2) {
  324. st->codec->codec_id = AV_CODEC_ID_PCM_ALAW;
  325. } else if (iff->maud_bits == 8 && iff->maud_compression == 3) {
  326. st->codec->codec_id = AV_CODEC_ID_PCM_MULAW;
  327. } else {
  328. av_log_ask_for_sample(s, "unsupported compression %d and bit depth %d\n", iff->maud_compression, iff->maud_bits);
  329. return AVERROR_PATCHWELCOME;
  330. }
  331. st->codec->bits_per_coded_sample =
  332. av_get_bits_per_sample(st->codec->codec_id);
  333. st->codec->block_align =
  334. st->codec->bits_per_coded_sample * st->codec->channels / 8;
  335. } else {
  336. switch (iff->svx8_compression) {
  337. case COMP_NONE:
  338. st->codec->codec_id = AV_CODEC_ID_PCM_S8_PLANAR;
  339. break;
  340. case COMP_FIB:
  341. st->codec->codec_id = AV_CODEC_ID_8SVX_FIB;
  342. break;
  343. case COMP_EXP:
  344. st->codec->codec_id = AV_CODEC_ID_8SVX_EXP;
  345. break;
  346. default:
  347. av_log(s, AV_LOG_ERROR,
  348. "Unknown SVX8 compression method '%d'\n", iff->svx8_compression);
  349. return -1;
  350. }
  351. }
  352. st->codec->bits_per_coded_sample = av_get_bits_per_sample(st->codec->codec_id);
  353. st->codec->bit_rate = st->codec->channels * st->codec->sample_rate * st->codec->bits_per_coded_sample;
  354. st->codec->block_align = st->codec->channels * st->codec->bits_per_coded_sample;
  355. break;
  356. case AVMEDIA_TYPE_VIDEO:
  357. iff->bpp = st->codec->bits_per_coded_sample;
  358. if ((screenmode & 0x800 /* Hold And Modify */) && iff->bpp <= 8) {
  359. iff->ham = iff->bpp > 6 ? 6 : 4;
  360. st->codec->bits_per_coded_sample = 24;
  361. }
  362. iff->flags = (screenmode & 0x80 /* Extra HalfBrite */) && iff->bpp <= 8;
  363. iff->masking = masking;
  364. iff->transparency = transparency;
  365. if (!st->codec->extradata) {
  366. st->codec->extradata_size = IFF_EXTRA_VIDEO_SIZE;
  367. st->codec->extradata = av_malloc(IFF_EXTRA_VIDEO_SIZE + FF_INPUT_BUFFER_PADDING_SIZE);
  368. if (!st->codec->extradata)
  369. return AVERROR(ENOMEM);
  370. }
  371. buf = st->codec->extradata;
  372. bytestream_put_be16(&buf, IFF_EXTRA_VIDEO_SIZE);
  373. bytestream_put_byte(&buf, iff->bitmap_compression);
  374. bytestream_put_byte(&buf, iff->bpp);
  375. bytestream_put_byte(&buf, iff->ham);
  376. bytestream_put_byte(&buf, iff->flags);
  377. bytestream_put_be16(&buf, iff->transparency);
  378. bytestream_put_byte(&buf, iff->masking);
  379. bytestream_put_buffer(&buf, iff->tvdc, sizeof(iff->tvdc));
  380. st->codec->codec_id = AV_CODEC_ID_IFF_ILBM;
  381. break;
  382. default:
  383. return -1;
  384. }
  385. return 0;
  386. }
  387. static int iff_read_packet(AVFormatContext *s,
  388. AVPacket *pkt)
  389. {
  390. IffDemuxContext *iff = s->priv_data;
  391. AVIOContext *pb = s->pb;
  392. AVStream *st = s->streams[0];
  393. int ret;
  394. if(iff->sent_bytes >= iff->body_size)
  395. return AVERROR_EOF;
  396. if (st->codec->codec_type == AVMEDIA_TYPE_AUDIO) {
  397. if (st->codec->codec_tag == ID_MAUD) {
  398. ret = av_get_packet(pb, pkt,
  399. FFMIN(iff->body_size - iff->sent_bytes, 1024 * st->codec->block_align));
  400. } else {
  401. ret = av_get_packet(pb, pkt, iff->body_size);
  402. }
  403. } else if (st->codec->codec_type == AVMEDIA_TYPE_VIDEO) {
  404. uint8_t *buf;
  405. if (av_new_packet(pkt, iff->body_size + 2) < 0) {
  406. return AVERROR(ENOMEM);
  407. }
  408. buf = pkt->data;
  409. bytestream_put_be16(&buf, 2);
  410. ret = avio_read(pb, buf, iff->body_size);
  411. } else {
  412. av_assert0(0);
  413. }
  414. if(iff->sent_bytes == 0)
  415. pkt->flags |= AV_PKT_FLAG_KEY;
  416. if (ret < 0)
  417. return ret;
  418. iff->sent_bytes += ret;
  419. pkt->stream_index = 0;
  420. return ret;
  421. }
  422. AVInputFormat ff_iff_demuxer = {
  423. .name = "iff",
  424. .long_name = NULL_IF_CONFIG_SMALL("IFF (Interchange File Format)"),
  425. .priv_data_size = sizeof(IffDemuxContext),
  426. .read_probe = iff_probe,
  427. .read_header = iff_read_header,
  428. .read_packet = iff_read_packet,
  429. };