iff.c 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324
  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 Libav.
  8. *
  9. * Libav 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. * Libav 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 Libav; 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 "libavutil/intreadwrite.h"
  31. #include "avformat.h"
  32. #define ID_8SVX MKTAG('8','S','V','X')
  33. #define ID_VHDR MKTAG('V','H','D','R')
  34. #define ID_ATAK MKTAG('A','T','A','K')
  35. #define ID_RLSE MKTAG('R','L','S','E')
  36. #define ID_CHAN MKTAG('C','H','A','N')
  37. #define ID_PBM MKTAG('P','B','M',' ')
  38. #define ID_ILBM MKTAG('I','L','B','M')
  39. #define ID_BMHD MKTAG('B','M','H','D')
  40. #define ID_CMAP MKTAG('C','M','A','P')
  41. #define ID_FORM MKTAG('F','O','R','M')
  42. #define ID_ANNO MKTAG('A','N','N','O')
  43. #define ID_AUTH MKTAG('A','U','T','H')
  44. #define ID_CHRS MKTAG('C','H','R','S')
  45. #define ID_COPYRIGHT MKTAG('(','c',')',' ')
  46. #define ID_CSET MKTAG('C','S','E','T')
  47. #define ID_FVER MKTAG('F','V','E','R')
  48. #define ID_NAME MKTAG('N','A','M','E')
  49. #define ID_TEXT MKTAG('T','E','X','T')
  50. #define ID_BODY MKTAG('B','O','D','Y')
  51. #define ID_ANNO MKTAG('A','N','N','O')
  52. #define LEFT 2
  53. #define RIGHT 4
  54. #define STEREO 6
  55. #define PACKET_SIZE 1024
  56. typedef enum {
  57. COMP_NONE,
  58. COMP_FIB,
  59. COMP_EXP
  60. } svx8_compression_type;
  61. typedef enum {
  62. BITMAP_RAW,
  63. BITMAP_BYTERUN1
  64. } bitmap_compression_type;
  65. typedef struct {
  66. uint64_t body_pos;
  67. uint32_t body_size;
  68. uint32_t sent_bytes;
  69. uint32_t audio_frame_count;
  70. } IffDemuxContext;
  71. static void interleave_stereo(const uint8_t *src, uint8_t *dest, int size)
  72. {
  73. uint8_t *end = dest + size;
  74. size = size>>1;
  75. while(dest < end) {
  76. *dest++ = *src;
  77. *dest++ = *(src+size);
  78. src++;
  79. }
  80. }
  81. /* Metadata string read */
  82. static int get_metadata(AVFormatContext *s,
  83. const char *const tag,
  84. const unsigned data_size)
  85. {
  86. uint8_t *buf = ((data_size + 1) == 0) ? NULL : av_malloc(data_size + 1);
  87. if (!buf)
  88. return AVERROR(ENOMEM);
  89. if (avio_read(s->pb, buf, data_size) < 0) {
  90. av_free(buf);
  91. return AVERROR(EIO);
  92. }
  93. buf[data_size] = 0;
  94. av_metadata_set2(&s->metadata, tag, buf, AV_METADATA_DONT_STRDUP_VAL);
  95. return 0;
  96. }
  97. static int iff_probe(AVProbeData *p)
  98. {
  99. const uint8_t *d = p->buf;
  100. if ( AV_RL32(d) == ID_FORM &&
  101. (AV_RL32(d+8) == ID_8SVX || AV_RL32(d+8) == ID_PBM || AV_RL32(d+8) == ID_ILBM) )
  102. return AVPROBE_SCORE_MAX;
  103. return 0;
  104. }
  105. static int iff_read_header(AVFormatContext *s,
  106. AVFormatParameters *ap)
  107. {
  108. IffDemuxContext *iff = s->priv_data;
  109. AVIOContext *pb = s->pb;
  110. AVStream *st;
  111. uint32_t chunk_id, data_size;
  112. int compression = -1;
  113. st = av_new_stream(s, 0);
  114. if (!st)
  115. return AVERROR(ENOMEM);
  116. st->codec->channels = 1;
  117. avio_skip(pb, 8);
  118. // codec_tag used by ByteRun1 decoder to distinguish progressive (PBM) and interlaced (ILBM) content
  119. st->codec->codec_tag = avio_rl32(pb);
  120. while(!pb->eof_reached) {
  121. uint64_t orig_pos;
  122. int res;
  123. const char *metadata_tag = NULL;
  124. chunk_id = avio_rl32(pb);
  125. data_size = avio_rb32(pb);
  126. orig_pos = avio_tell(pb);
  127. switch(chunk_id) {
  128. case ID_VHDR:
  129. st->codec->codec_type = AVMEDIA_TYPE_AUDIO;
  130. if (data_size < 14)
  131. return AVERROR_INVALIDDATA;
  132. avio_skip(pb, 12);
  133. st->codec->sample_rate = avio_rb16(pb);
  134. if (data_size >= 16) {
  135. avio_skip(pb, 1);
  136. compression = avio_r8(pb);
  137. }
  138. break;
  139. case ID_BODY:
  140. iff->body_pos = avio_tell(pb);
  141. iff->body_size = data_size;
  142. break;
  143. case ID_CHAN:
  144. if (data_size < 4)
  145. return AVERROR_INVALIDDATA;
  146. st->codec->channels = (avio_rb32(pb) < 6) ? 1 : 2;
  147. break;
  148. case ID_CMAP:
  149. st->codec->extradata_size = data_size;
  150. st->codec->extradata = av_malloc(data_size);
  151. if (!st->codec->extradata)
  152. return AVERROR(ENOMEM);
  153. if (avio_read(pb, st->codec->extradata, data_size) < 0)
  154. return AVERROR(EIO);
  155. break;
  156. case ID_BMHD:
  157. st->codec->codec_type = AVMEDIA_TYPE_VIDEO;
  158. if (data_size <= 8)
  159. return AVERROR_INVALIDDATA;
  160. st->codec->width = avio_rb16(pb);
  161. st->codec->height = avio_rb16(pb);
  162. avio_skip(pb, 4); // x, y offset
  163. st->codec->bits_per_coded_sample = avio_r8(pb);
  164. if (data_size >= 11) {
  165. avio_skip(pb, 1); // masking
  166. compression = avio_r8(pb);
  167. }
  168. if (data_size >= 16) {
  169. avio_skip(pb, 3); // paddding, transparent
  170. st->sample_aspect_ratio.num = avio_r8(pb);
  171. st->sample_aspect_ratio.den = avio_r8(pb);
  172. }
  173. break;
  174. case ID_ANNO:
  175. case ID_TEXT:
  176. metadata_tag = "comment";
  177. break;
  178. case ID_AUTH:
  179. metadata_tag = "artist";
  180. break;
  181. case ID_COPYRIGHT:
  182. metadata_tag = "copyright";
  183. break;
  184. case ID_NAME:
  185. metadata_tag = "title";
  186. break;
  187. }
  188. if (metadata_tag) {
  189. if ((res = get_metadata(s, metadata_tag, data_size)) < 0) {
  190. av_log(s, AV_LOG_ERROR, "cannot allocate metadata tag %s!", metadata_tag);
  191. return res;
  192. }
  193. }
  194. avio_skip(pb, data_size - (avio_tell(pb) - orig_pos) + (data_size & 1));
  195. }
  196. avio_seek(pb, iff->body_pos, SEEK_SET);
  197. switch(st->codec->codec_type) {
  198. case AVMEDIA_TYPE_AUDIO:
  199. av_set_pts_info(st, 32, 1, st->codec->sample_rate);
  200. switch(compression) {
  201. case COMP_NONE:
  202. st->codec->codec_id = CODEC_ID_PCM_S8;
  203. break;
  204. case COMP_FIB:
  205. st->codec->codec_id = CODEC_ID_8SVX_FIB;
  206. break;
  207. case COMP_EXP:
  208. st->codec->codec_id = CODEC_ID_8SVX_EXP;
  209. break;
  210. default:
  211. av_log(s, AV_LOG_ERROR, "unknown compression method\n");
  212. return -1;
  213. }
  214. st->codec->bits_per_coded_sample = 8;
  215. st->codec->bit_rate = st->codec->channels * st->codec->sample_rate * st->codec->bits_per_coded_sample;
  216. st->codec->block_align = st->codec->channels * st->codec->bits_per_coded_sample;
  217. break;
  218. case AVMEDIA_TYPE_VIDEO:
  219. switch (compression) {
  220. case BITMAP_RAW:
  221. st->codec->codec_id = CODEC_ID_IFF_ILBM;
  222. break;
  223. case BITMAP_BYTERUN1:
  224. st->codec->codec_id = CODEC_ID_IFF_BYTERUN1;
  225. break;
  226. default:
  227. av_log(s, AV_LOG_ERROR, "unknown compression method\n");
  228. return AVERROR_INVALIDDATA;
  229. }
  230. break;
  231. default:
  232. return -1;
  233. }
  234. return 0;
  235. }
  236. static int iff_read_packet(AVFormatContext *s,
  237. AVPacket *pkt)
  238. {
  239. IffDemuxContext *iff = s->priv_data;
  240. AVIOContext *pb = s->pb;
  241. AVStream *st = s->streams[0];
  242. int ret;
  243. if(iff->sent_bytes >= iff->body_size)
  244. return AVERROR(EIO);
  245. if(st->codec->channels == 2) {
  246. uint8_t sample_buffer[PACKET_SIZE];
  247. ret = avio_read(pb, sample_buffer, PACKET_SIZE);
  248. if(av_new_packet(pkt, PACKET_SIZE) < 0) {
  249. av_log(s, AV_LOG_ERROR, "cannot allocate packet\n");
  250. return AVERROR(ENOMEM);
  251. }
  252. interleave_stereo(sample_buffer, pkt->data, PACKET_SIZE);
  253. } else if (st->codec->codec_type == AVMEDIA_TYPE_VIDEO) {
  254. ret = av_get_packet(pb, pkt, iff->body_size);
  255. } else {
  256. ret = av_get_packet(pb, pkt, PACKET_SIZE);
  257. }
  258. if(iff->sent_bytes == 0)
  259. pkt->flags |= AV_PKT_FLAG_KEY;
  260. if(st->codec->codec_type == AVMEDIA_TYPE_AUDIO) {
  261. iff->sent_bytes += PACKET_SIZE;
  262. } else {
  263. iff->sent_bytes = iff->body_size;
  264. }
  265. pkt->stream_index = 0;
  266. if(st->codec->codec_type == AVMEDIA_TYPE_AUDIO) {
  267. pkt->pts = iff->audio_frame_count;
  268. iff->audio_frame_count += ret / st->codec->channels;
  269. }
  270. return ret;
  271. }
  272. AVInputFormat ff_iff_demuxer = {
  273. "IFF",
  274. NULL_IF_CONFIG_SMALL("IFF format"),
  275. sizeof(IffDemuxContext),
  276. iff_probe,
  277. iff_read_header,
  278. iff_read_packet,
  279. };