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.

391 lines
12KB

  1. /*
  2. * IFF (.iff) file demuxer
  3. * Copyright (c) 2008 Jaikrishnan Menon <realityman@gmx.net>
  4. * Copyright (c) 2010 Peter Ross <pross@xvid.org>
  5. * Copyright (c) 2010 Sebastian Vater <cdgs.basty@googlemail.com>
  6. *
  7. * This file is part of FFmpeg.
  8. *
  9. * FFmpeg is free software; you can redistribute it and/or
  10. * modify it under the terms of the GNU Lesser General Public
  11. * License as published by the Free Software Foundation; either
  12. * version 2.1 of the License, or (at your option) any later version.
  13. *
  14. * FFmpeg is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  17. * Lesser General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU Lesser General Public
  20. * License along with FFmpeg; if not, write to the Free Software
  21. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  22. */
  23. /**
  24. * @file
  25. * IFF file demuxer
  26. * by Jaikrishnan Menon
  27. * for more information on the .iff file format, visit:
  28. * http://wiki.multimedia.cx/index.php?title=IFF
  29. */
  30. #include "libavcodec/bytestream.h"
  31. #include "libavutil/intreadwrite.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. #define PACKET_SIZE 1024
  58. /**
  59. * This number of bytes if added at the beginning of each AVPacket
  60. * which contain additional information about video properties
  61. * which has to be shared between demuxer and decoder.
  62. * This number may change between frames, e.g. the demuxer might
  63. * set it to smallest possible size of 2 to indicate that there's
  64. * no extradata changing in this frame.
  65. */
  66. #define IFF_EXTRA_VIDEO_SIZE 9
  67. typedef enum {
  68. COMP_NONE,
  69. COMP_FIB,
  70. COMP_EXP
  71. } svx8_compression_type;
  72. typedef enum {
  73. BITMAP_RAW,
  74. BITMAP_BYTERUN1
  75. } bitmap_compression_type;
  76. typedef struct {
  77. uint64_t body_pos;
  78. uint32_t body_size;
  79. uint32_t sent_bytes;
  80. uint32_t audio_frame_count;
  81. svx8_compression_type svx8_compression;
  82. bitmap_compression_type bitmap_compression; ///< delta compression method used
  83. unsigned bpp; ///< bits per plane to decode (differs from bits_per_coded_sample if HAM)
  84. unsigned ham; ///< 0 if non-HAM or number of hold bits (6 for bpp > 6, 4 otherwise)
  85. unsigned flags; ///< 1 for EHB, 0 is no extra half darkening
  86. unsigned transparency; ///< transparency color index in palette
  87. unsigned masking; ///< masking method used
  88. } IffDemuxContext;
  89. static void interleave_stereo(const uint8_t *src, uint8_t *dest, int size)
  90. {
  91. uint8_t *end = dest + size;
  92. size = size>>1;
  93. while(dest < end) {
  94. *dest++ = *src;
  95. *dest++ = *(src+size);
  96. src++;
  97. }
  98. }
  99. /* Metadata string read */
  100. static int get_metadata(AVFormatContext *s,
  101. const char *const tag,
  102. const unsigned data_size)
  103. {
  104. uint8_t *buf = ((data_size + 1) == 0) ? NULL : av_malloc(data_size + 1);
  105. if (!buf)
  106. return AVERROR(ENOMEM);
  107. if (avio_read(s->pb, buf, data_size) < 0) {
  108. av_free(buf);
  109. return AVERROR(EIO);
  110. }
  111. buf[data_size] = 0;
  112. av_metadata_set2(&s->metadata, tag, buf, AV_METADATA_DONT_STRDUP_VAL);
  113. return 0;
  114. }
  115. static int iff_probe(AVProbeData *p)
  116. {
  117. const uint8_t *d = p->buf;
  118. if ( AV_RL32(d) == ID_FORM &&
  119. (AV_RL32(d+8) == ID_8SVX || AV_RL32(d+8) == ID_PBM || AV_RL32(d+8) == ID_ILBM) )
  120. return AVPROBE_SCORE_MAX;
  121. return 0;
  122. }
  123. static int iff_read_header(AVFormatContext *s,
  124. AVFormatParameters *ap)
  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. st = av_new_stream(s, 0);
  135. if (!st)
  136. return AVERROR(ENOMEM);
  137. st->codec->channels = 1;
  138. avio_skip(pb, 8);
  139. // codec_tag used by ByteRun1 decoder to distinguish progressive (PBM) and interlaced (ILBM) content
  140. st->codec->codec_tag = avio_rl32(pb);
  141. while(!url_feof(pb)) {
  142. uint64_t orig_pos;
  143. int res;
  144. const char *metadata_tag = NULL;
  145. chunk_id = avio_rl32(pb);
  146. data_size = avio_rb32(pb);
  147. orig_pos = avio_tell(pb);
  148. switch(chunk_id) {
  149. case ID_VHDR:
  150. st->codec->codec_type = AVMEDIA_TYPE_AUDIO;
  151. if (data_size < 14)
  152. return AVERROR_INVALIDDATA;
  153. avio_skip(pb, 12);
  154. st->codec->sample_rate = avio_rb16(pb);
  155. if (data_size >= 16) {
  156. avio_skip(pb, 1);
  157. iff->svx8_compression = avio_r8(pb);
  158. }
  159. break;
  160. case ID_BODY:
  161. iff->body_pos = avio_tell(pb);
  162. iff->body_size = data_size;
  163. break;
  164. case ID_CHAN:
  165. if (data_size < 4)
  166. return AVERROR_INVALIDDATA;
  167. st->codec->channels = (avio_rb32(pb) < 6) ? 1 : 2;
  168. break;
  169. case ID_CAMG:
  170. if (data_size < 4)
  171. return AVERROR_INVALIDDATA;
  172. screenmode = avio_rb32(pb);
  173. break;
  174. case ID_CMAP:
  175. st->codec->extradata_size = data_size + IFF_EXTRA_VIDEO_SIZE;
  176. st->codec->extradata = av_malloc(data_size + IFF_EXTRA_VIDEO_SIZE + FF_INPUT_BUFFER_PADDING_SIZE);
  177. if (!st->codec->extradata)
  178. return AVERROR(ENOMEM);
  179. if (avio_read(pb, st->codec->extradata + IFF_EXTRA_VIDEO_SIZE, data_size) < 0)
  180. return AVERROR(EIO);
  181. break;
  182. case ID_BMHD:
  183. iff->bitmap_compression = -1;
  184. st->codec->codec_type = AVMEDIA_TYPE_VIDEO;
  185. if (data_size <= 8)
  186. return AVERROR_INVALIDDATA;
  187. st->codec->width = avio_rb16(pb);
  188. st->codec->height = avio_rb16(pb);
  189. avio_skip(pb, 4); // x, y offset
  190. st->codec->bits_per_coded_sample = avio_r8(pb);
  191. if (data_size >= 10)
  192. masking = avio_r8(pb);
  193. if (data_size >= 11)
  194. iff->bitmap_compression = avio_r8(pb);
  195. if (data_size >= 14) {
  196. avio_skip(pb, 1); // padding
  197. transparency = avio_rb16(pb);
  198. }
  199. if (data_size >= 16) {
  200. st->sample_aspect_ratio.num = avio_r8(pb);
  201. st->sample_aspect_ratio.den = avio_r8(pb);
  202. }
  203. break;
  204. case ID_ANNO:
  205. case ID_TEXT:
  206. metadata_tag = "comment";
  207. break;
  208. case ID_AUTH:
  209. metadata_tag = "artist";
  210. break;
  211. case ID_COPYRIGHT:
  212. metadata_tag = "copyright";
  213. break;
  214. case ID_NAME:
  215. metadata_tag = "title";
  216. break;
  217. }
  218. if (metadata_tag) {
  219. if ((res = get_metadata(s, metadata_tag, data_size)) < 0) {
  220. av_log(s, AV_LOG_ERROR, "cannot allocate metadata tag %s!", metadata_tag);
  221. return res;
  222. }
  223. }
  224. avio_skip(pb, data_size - (avio_tell(pb) - orig_pos) + (data_size & 1));
  225. }
  226. avio_seek(pb, iff->body_pos, SEEK_SET);
  227. switch(st->codec->codec_type) {
  228. case AVMEDIA_TYPE_AUDIO:
  229. av_set_pts_info(st, 32, 1, st->codec->sample_rate);
  230. switch (iff->svx8_compression) {
  231. case COMP_NONE:
  232. st->codec->codec_id = CODEC_ID_PCM_S8;
  233. break;
  234. case COMP_FIB:
  235. st->codec->codec_id = CODEC_ID_8SVX_FIB;
  236. break;
  237. case COMP_EXP:
  238. st->codec->codec_id = CODEC_ID_8SVX_EXP;
  239. break;
  240. default:
  241. av_log(s, AV_LOG_ERROR,
  242. "Unknown SVX8 compression method '%d'\n", iff->svx8_compression);
  243. return -1;
  244. }
  245. st->codec->bits_per_coded_sample = iff->svx8_compression == COMP_NONE ? 8 : 4;
  246. st->codec->bit_rate = st->codec->channels * st->codec->sample_rate * st->codec->bits_per_coded_sample;
  247. st->codec->block_align = st->codec->channels * st->codec->bits_per_coded_sample;
  248. break;
  249. case AVMEDIA_TYPE_VIDEO:
  250. iff->bpp = st->codec->bits_per_coded_sample;
  251. if ((screenmode & 0x800 /* Hold And Modify */) && iff->bpp <= 8) {
  252. iff->ham = iff->bpp > 6 ? 6 : 4;
  253. st->codec->bits_per_coded_sample = 24;
  254. }
  255. iff->flags = (screenmode & 0x80 /* Extra HalfBrite */) && iff->bpp <= 8;
  256. iff->masking = masking;
  257. iff->transparency = transparency;
  258. if (!st->codec->extradata) {
  259. st->codec->extradata_size = IFF_EXTRA_VIDEO_SIZE;
  260. st->codec->extradata = av_malloc(IFF_EXTRA_VIDEO_SIZE + FF_INPUT_BUFFER_PADDING_SIZE);
  261. if (!st->codec->extradata)
  262. return AVERROR(ENOMEM);
  263. }
  264. buf = st->codec->extradata;
  265. bytestream_put_be16(&buf, IFF_EXTRA_VIDEO_SIZE);
  266. bytestream_put_byte(&buf, iff->bitmap_compression);
  267. bytestream_put_byte(&buf, iff->bpp);
  268. bytestream_put_byte(&buf, iff->ham);
  269. bytestream_put_byte(&buf, iff->flags);
  270. bytestream_put_be16(&buf, iff->transparency);
  271. bytestream_put_byte(&buf, iff->masking);
  272. switch (iff->bitmap_compression) {
  273. case BITMAP_RAW:
  274. st->codec->codec_id = CODEC_ID_IFF_ILBM;
  275. break;
  276. case BITMAP_BYTERUN1:
  277. st->codec->codec_id = CODEC_ID_IFF_BYTERUN1;
  278. break;
  279. default:
  280. av_log(s, AV_LOG_ERROR,
  281. "Unknown bitmap compression method '%d'\n", iff->bitmap_compression);
  282. return AVERROR_INVALIDDATA;
  283. }
  284. break;
  285. default:
  286. return -1;
  287. }
  288. return 0;
  289. }
  290. static int iff_read_packet(AVFormatContext *s,
  291. AVPacket *pkt)
  292. {
  293. IffDemuxContext *iff = s->priv_data;
  294. AVIOContext *pb = s->pb;
  295. AVStream *st = s->streams[0];
  296. int ret;
  297. if(iff->sent_bytes >= iff->body_size)
  298. return AVERROR(EIO);
  299. if(st->codec->channels == 2) {
  300. uint8_t sample_buffer[PACKET_SIZE];
  301. ret = avio_read(pb, sample_buffer, PACKET_SIZE);
  302. if(av_new_packet(pkt, PACKET_SIZE) < 0) {
  303. av_log(s, AV_LOG_ERROR, "cannot allocate packet\n");
  304. return AVERROR(ENOMEM);
  305. }
  306. interleave_stereo(sample_buffer, pkt->data, PACKET_SIZE);
  307. } else if (st->codec->codec_type == AVMEDIA_TYPE_VIDEO) {
  308. uint8_t *buf;
  309. if (av_new_packet(pkt, iff->body_size + 2) < 0) {
  310. return AVERROR(ENOMEM);
  311. }
  312. buf = pkt->data;
  313. bytestream_put_be16(&buf, 2);
  314. ret = avio_read(pb, buf, iff->body_size);
  315. } else {
  316. ret = av_get_packet(pb, pkt, PACKET_SIZE);
  317. }
  318. if(iff->sent_bytes == 0)
  319. pkt->flags |= AV_PKT_FLAG_KEY;
  320. if(st->codec->codec_type == AVMEDIA_TYPE_AUDIO) {
  321. iff->sent_bytes += PACKET_SIZE;
  322. } else {
  323. iff->sent_bytes = iff->body_size;
  324. }
  325. pkt->stream_index = 0;
  326. if(st->codec->codec_type == AVMEDIA_TYPE_AUDIO) {
  327. pkt->pts = iff->audio_frame_count;
  328. iff->audio_frame_count += ret / st->codec->channels;
  329. }
  330. return ret;
  331. }
  332. AVInputFormat ff_iff_demuxer = {
  333. "IFF",
  334. NULL_IF_CONFIG_SMALL("IFF format"),
  335. sizeof(IffDemuxContext),
  336. iff_probe,
  337. iff_read_header,
  338. iff_read_packet,
  339. };