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.

310 lines
9.3KB

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