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.

353 lines
12KB

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