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.

380 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: metadata_tag = "comment"; break;
  206. case ID_AUTH: metadata_tag = "artist"; break;
  207. case ID_COPYRIGHT: metadata_tag = "copyright"; break;
  208. case ID_NAME: metadata_tag = "title"; break;
  209. }
  210. if (metadata_tag) {
  211. if ((res = get_metadata(s, metadata_tag, data_size)) < 0) {
  212. av_log(s, AV_LOG_ERROR, "cannot allocate metadata tag %s!", metadata_tag);
  213. return res;
  214. }
  215. }
  216. avio_skip(pb, data_size - (avio_tell(pb) - orig_pos) + (data_size & 1));
  217. }
  218. avio_seek(pb, iff->body_pos, SEEK_SET);
  219. switch(st->codec->codec_type) {
  220. case AVMEDIA_TYPE_AUDIO:
  221. av_set_pts_info(st, 32, 1, st->codec->sample_rate);
  222. switch (iff->svx8_compression) {
  223. case COMP_NONE:
  224. st->codec->codec_id = CODEC_ID_PCM_S8;
  225. break;
  226. case COMP_FIB:
  227. st->codec->codec_id = CODEC_ID_8SVX_FIB;
  228. break;
  229. case COMP_EXP:
  230. st->codec->codec_id = CODEC_ID_8SVX_EXP;
  231. break;
  232. default:
  233. av_log(s, AV_LOG_ERROR,
  234. "Unknown SVX8 compression method '%d'\n", iff->svx8_compression);
  235. return -1;
  236. }
  237. st->codec->bits_per_coded_sample = iff->svx8_compression == COMP_NONE ? 8 : 4;
  238. st->codec->bit_rate = st->codec->channels * st->codec->sample_rate * st->codec->bits_per_coded_sample;
  239. st->codec->block_align = st->codec->channels * st->codec->bits_per_coded_sample;
  240. break;
  241. case AVMEDIA_TYPE_VIDEO:
  242. iff->bpp = st->codec->bits_per_coded_sample;
  243. if ((screenmode & 0x800 /* Hold And Modify */) && iff->bpp <= 8) {
  244. iff->ham = iff->bpp > 6 ? 6 : 4;
  245. st->codec->bits_per_coded_sample = 24;
  246. }
  247. iff->flags = (screenmode & 0x80 /* Extra HalfBrite */) && iff->bpp <= 8;
  248. iff->masking = masking;
  249. iff->transparency = transparency;
  250. if (!st->codec->extradata) {
  251. st->codec->extradata_size = IFF_EXTRA_VIDEO_SIZE;
  252. st->codec->extradata = av_malloc(IFF_EXTRA_VIDEO_SIZE + FF_INPUT_BUFFER_PADDING_SIZE);
  253. if (!st->codec->extradata)
  254. return AVERROR(ENOMEM);
  255. }
  256. buf = st->codec->extradata;
  257. bytestream_put_be16(&buf, IFF_EXTRA_VIDEO_SIZE);
  258. bytestream_put_byte(&buf, iff->bitmap_compression);
  259. bytestream_put_byte(&buf, iff->bpp);
  260. bytestream_put_byte(&buf, iff->ham);
  261. bytestream_put_byte(&buf, iff->flags);
  262. bytestream_put_be16(&buf, iff->transparency);
  263. bytestream_put_byte(&buf, iff->masking);
  264. switch (iff->bitmap_compression) {
  265. case BITMAP_RAW:
  266. st->codec->codec_id = CODEC_ID_IFF_ILBM;
  267. break;
  268. case BITMAP_BYTERUN1:
  269. st->codec->codec_id = CODEC_ID_IFF_BYTERUN1;
  270. break;
  271. default:
  272. av_log(s, AV_LOG_ERROR,
  273. "Unknown bitmap compression method '%d'\n", iff->bitmap_compression);
  274. return AVERROR_INVALIDDATA;
  275. }
  276. break;
  277. default:
  278. return -1;
  279. }
  280. return 0;
  281. }
  282. static int iff_read_packet(AVFormatContext *s,
  283. AVPacket *pkt)
  284. {
  285. IffDemuxContext *iff = s->priv_data;
  286. AVIOContext *pb = s->pb;
  287. AVStream *st = s->streams[0];
  288. int ret;
  289. if(iff->sent_bytes >= iff->body_size)
  290. return AVERROR(EIO);
  291. if(st->codec->channels == 2) {
  292. uint8_t sample_buffer[PACKET_SIZE];
  293. ret = avio_read(pb, sample_buffer, PACKET_SIZE);
  294. if(av_new_packet(pkt, PACKET_SIZE) < 0) {
  295. av_log(s, AV_LOG_ERROR, "cannot allocate packet\n");
  296. return AVERROR(ENOMEM);
  297. }
  298. interleave_stereo(sample_buffer, pkt->data, PACKET_SIZE);
  299. } else if (st->codec->codec_type == AVMEDIA_TYPE_VIDEO) {
  300. uint8_t *buf;
  301. if (av_new_packet(pkt, iff->body_size + 2) < 0) {
  302. return AVERROR(ENOMEM);
  303. }
  304. buf = pkt->data;
  305. bytestream_put_be16(&buf, 2);
  306. ret = avio_read(pb, buf, iff->body_size);
  307. } else {
  308. ret = av_get_packet(pb, pkt, PACKET_SIZE);
  309. }
  310. if(iff->sent_bytes == 0)
  311. pkt->flags |= AV_PKT_FLAG_KEY;
  312. if(st->codec->codec_type == AVMEDIA_TYPE_AUDIO) {
  313. iff->sent_bytes += PACKET_SIZE;
  314. } else {
  315. iff->sent_bytes = iff->body_size;
  316. }
  317. pkt->stream_index = 0;
  318. if(st->codec->codec_type == AVMEDIA_TYPE_AUDIO) {
  319. pkt->pts = iff->audio_frame_count;
  320. iff->audio_frame_count += ret / st->codec->channels;
  321. }
  322. return ret;
  323. }
  324. AVInputFormat ff_iff_demuxer = {
  325. "IFF",
  326. NULL_IF_CONFIG_SMALL("IFF format"),
  327. sizeof(IffDemuxContext),
  328. iff_probe,
  329. iff_read_header,
  330. iff_read_packet,
  331. };