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.

389 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. unsigned 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. static void interleave_stereo(const uint8_t *src, uint8_t *dest, int size)
  89. {
  90. uint8_t *end = dest + size;
  91. size = size>>1;
  92. while(dest < end) {
  93. *dest++ = *src;
  94. *dest++ = *(src+size);
  95. src++;
  96. }
  97. }
  98. /* Metadata string read */
  99. static int get_metadata(AVFormatContext *s,
  100. const char *const tag,
  101. const unsigned data_size)
  102. {
  103. uint8_t *buf = ((data_size + 1) == 0) ? NULL : av_malloc(data_size + 1);
  104. if (!buf)
  105. return AVERROR(ENOMEM);
  106. if (avio_read(s->pb, buf, data_size) < 0) {
  107. av_free(buf);
  108. return AVERROR(EIO);
  109. }
  110. buf[data_size] = 0;
  111. av_metadata_set2(&s->metadata, tag, buf, AV_METADATA_DONT_STRDUP_VAL);
  112. return 0;
  113. }
  114. static int iff_probe(AVProbeData *p)
  115. {
  116. const uint8_t *d = p->buf;
  117. if ( AV_RL32(d) == ID_FORM &&
  118. (AV_RL32(d+8) == ID_8SVX || AV_RL32(d+8) == ID_PBM || AV_RL32(d+8) == ID_ILBM) )
  119. return AVPROBE_SCORE_MAX;
  120. return 0;
  121. }
  122. static int iff_read_header(AVFormatContext *s,
  123. AVFormatParameters *ap)
  124. {
  125. IffDemuxContext *iff = s->priv_data;
  126. AVIOContext *pb = s->pb;
  127. AVStream *st;
  128. uint8_t *buf;
  129. uint32_t chunk_id, data_size;
  130. int compression = -1;
  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. 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. 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. 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_ANNO:
  204. case ID_TEXT:
  205. metadata_tag = "comment";
  206. break;
  207. case ID_AUTH:
  208. metadata_tag = "artist";
  209. break;
  210. case ID_COPYRIGHT:
  211. metadata_tag = "copyright";
  212. break;
  213. case ID_NAME:
  214. metadata_tag = "title";
  215. break;
  216. }
  217. if (metadata_tag) {
  218. if ((res = get_metadata(s, metadata_tag, data_size)) < 0) {
  219. av_log(s, AV_LOG_ERROR, "cannot allocate metadata tag %s!", metadata_tag);
  220. return res;
  221. }
  222. }
  223. avio_skip(pb, data_size - (avio_tell(pb) - orig_pos) + (data_size & 1));
  224. }
  225. avio_seek(pb, iff->body_pos, SEEK_SET);
  226. switch(st->codec->codec_type) {
  227. case AVMEDIA_TYPE_AUDIO:
  228. av_set_pts_info(st, 32, 1, st->codec->sample_rate);
  229. switch(compression) {
  230. case COMP_NONE:
  231. st->codec->codec_id = CODEC_ID_PCM_S8;
  232. break;
  233. case COMP_FIB:
  234. st->codec->codec_id = CODEC_ID_8SVX_FIB;
  235. break;
  236. case COMP_EXP:
  237. st->codec->codec_id = CODEC_ID_8SVX_EXP;
  238. break;
  239. default:
  240. av_log(s, AV_LOG_ERROR, "unknown compression method\n");
  241. return -1;
  242. }
  243. st->codec->bits_per_coded_sample = 8;
  244. st->codec->bit_rate = st->codec->channels * st->codec->sample_rate * st->codec->bits_per_coded_sample;
  245. st->codec->block_align = st->codec->channels * st->codec->bits_per_coded_sample;
  246. break;
  247. case AVMEDIA_TYPE_VIDEO:
  248. iff->compression = compression;
  249. iff->bpp = st->codec->bits_per_coded_sample;
  250. if ((screenmode & 0x800 /* Hold And Modify */) && iff->bpp <= 8) {
  251. iff->ham = iff->bpp > 6 ? 6 : 4;
  252. st->codec->bits_per_coded_sample = 24;
  253. }
  254. iff->flags = (screenmode & 0x80 /* Extra HalfBrite */) && iff->bpp <= 8;
  255. iff->masking = masking;
  256. iff->transparency = transparency;
  257. if (!st->codec->extradata) {
  258. st->codec->extradata_size = IFF_EXTRA_VIDEO_SIZE;
  259. st->codec->extradata = av_malloc(IFF_EXTRA_VIDEO_SIZE + FF_INPUT_BUFFER_PADDING_SIZE);
  260. if (!st->codec->extradata)
  261. return AVERROR(ENOMEM);
  262. }
  263. buf = st->codec->extradata;
  264. bytestream_put_be16(&buf, IFF_EXTRA_VIDEO_SIZE);
  265. bytestream_put_byte(&buf, iff->compression);
  266. bytestream_put_byte(&buf, iff->bpp);
  267. bytestream_put_byte(&buf, iff->ham);
  268. bytestream_put_byte(&buf, iff->flags);
  269. bytestream_put_be16(&buf, iff->transparency);
  270. bytestream_put_byte(&buf, iff->masking);
  271. switch (compression) {
  272. case BITMAP_RAW:
  273. st->codec->codec_id = CODEC_ID_IFF_ILBM;
  274. break;
  275. case BITMAP_BYTERUN1:
  276. st->codec->codec_id = CODEC_ID_IFF_BYTERUN1;
  277. break;
  278. default:
  279. av_log(s, AV_LOG_ERROR, "unknown compression method\n");
  280. return AVERROR_INVALIDDATA;
  281. }
  282. break;
  283. default:
  284. return -1;
  285. }
  286. return 0;
  287. }
  288. static int iff_read_packet(AVFormatContext *s,
  289. AVPacket *pkt)
  290. {
  291. IffDemuxContext *iff = s->priv_data;
  292. AVIOContext *pb = s->pb;
  293. AVStream *st = s->streams[0];
  294. int ret;
  295. if(iff->sent_bytes >= iff->body_size)
  296. return AVERROR(EIO);
  297. if(st->codec->channels == 2) {
  298. uint8_t sample_buffer[PACKET_SIZE];
  299. ret = avio_read(pb, sample_buffer, PACKET_SIZE);
  300. if(av_new_packet(pkt, PACKET_SIZE) < 0) {
  301. av_log(s, AV_LOG_ERROR, "cannot allocate packet\n");
  302. return AVERROR(ENOMEM);
  303. }
  304. interleave_stereo(sample_buffer, pkt->data, PACKET_SIZE);
  305. } else if (st->codec->codec_type == AVMEDIA_TYPE_VIDEO) {
  306. uint8_t *buf;
  307. if (av_new_packet(pkt, iff->body_size + 2) < 0) {
  308. return AVERROR(ENOMEM);
  309. }
  310. buf = pkt->data;
  311. bytestream_put_be16(&buf, 2);
  312. ret = avio_read(pb, buf, iff->body_size);
  313. } else {
  314. ret = av_get_packet(pb, pkt, PACKET_SIZE);
  315. }
  316. if(iff->sent_bytes == 0)
  317. pkt->flags |= AV_PKT_FLAG_KEY;
  318. if(st->codec->codec_type == AVMEDIA_TYPE_AUDIO) {
  319. iff->sent_bytes += PACKET_SIZE;
  320. } else {
  321. iff->sent_bytes = iff->body_size;
  322. }
  323. pkt->stream_index = 0;
  324. if(st->codec->codec_type == AVMEDIA_TYPE_AUDIO) {
  325. pkt->pts = iff->audio_frame_count;
  326. iff->audio_frame_count += ret / st->codec->channels;
  327. }
  328. return ret;
  329. }
  330. AVInputFormat ff_iff_demuxer = {
  331. "IFF",
  332. NULL_IF_CONFIG_SMALL("IFF format"),
  333. sizeof(IffDemuxContext),
  334. iff_probe,
  335. iff_read_header,
  336. iff_read_packet,
  337. };