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.

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