iff.c 12 KB

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