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.

290 lines
8.8KB

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