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.

279 lines
8.4KB

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