iff.c 14 KB

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