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.

423 lines
15KB

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