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.

393 lines
13KB

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