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.

201 lines
5.3KB

  1. /*
  2. * IFF (.iff) file demuxer
  3. * Copyright (c) 2008 Jaikrishnan Menon <realityman@gmx.net>
  4. *
  5. * This file is part of FFmpeg.
  6. *
  7. * FFmpeg is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU Lesser General Public
  9. * License as published by the Free Software Foundation; either
  10. * version 2.1 of the License, or (at your option) any later version.
  11. *
  12. * FFmpeg is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. * Lesser General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Lesser General Public
  18. * License along with FFmpeg; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  20. */
  21. /**
  22. * @file iff.c
  23. * IFF file demuxer
  24. * by Jaikrishnan Menon
  25. * for more information on the .iff file format, visit:
  26. * http://wiki.multimedia.cx/index.php?title=IFF
  27. */
  28. #include "avformat.h"
  29. #define ID_8SVX MKTAG('8','S','V','X')
  30. #define ID_VHDR MKTAG('V','H','D','R')
  31. #define ID_ATAK MKTAG('A','T','A','K')
  32. #define ID_RLSE MKTAG('R','L','S','E')
  33. #define ID_CHAN MKTAG('C','H','A','N')
  34. #define ID_FORM MKTAG('F','O','R','M')
  35. #define ID_ANNO MKTAG('A','N','N','O')
  36. #define ID_AUTH MKTAG('A','U','T','H')
  37. #define ID_CHRS MKTAG('C','H','R','S')
  38. #define ID_COPYRIGHT MKTAG('(','c',')',' ')
  39. #define ID_CSET MKTAG('C','S','E','T')
  40. #define ID_FVER MKTAG('F','V','E','R')
  41. #define ID_NAME MKTAG('N','A','M','E')
  42. #define ID_TEXT MKTAG('T','E','X','T')
  43. #define ID_BODY MKTAG('B','O','D','Y')
  44. #define LEFT 2
  45. #define RIGHT 4
  46. #define STEREO 6
  47. #define PACKET_SIZE 1024
  48. typedef enum {COMP_NONE, COMP_FIB, COMP_EXP} svx8_compression_type;
  49. typedef struct {
  50. uint32_t body_size;
  51. uint32_t sent_bytes;
  52. uint32_t audio_frame_count;
  53. } IffDemuxContext;
  54. static void interleave_stereo(const uint8_t *src, uint8_t *dest, int size)
  55. {
  56. uint8_t *end = dest + size;
  57. size = size>>1;
  58. while(dest < end) {
  59. *dest++ = *src;
  60. *dest++ = *(src+size);
  61. src++;
  62. }
  63. }
  64. static int iff_probe(AVProbeData *p)
  65. {
  66. const uint8_t *d = p->buf;
  67. if ( AV_RL32(d) == ID_FORM &&
  68. AV_RL32(d+8) == ID_8SVX)
  69. return AVPROBE_SCORE_MAX;
  70. return 0;
  71. }
  72. static int iff_read_header(AVFormatContext *s,
  73. AVFormatParameters *ap)
  74. {
  75. IffDemuxContext *iff = s->priv_data;
  76. ByteIOContext *pb = s->pb;
  77. AVStream *st;
  78. uint32_t chunk_id, data_size;
  79. int padding, done = 0;
  80. st = av_new_stream(s, 0);
  81. if (!st)
  82. return AVERROR(ENOMEM);
  83. st->codec->channels = 1;
  84. url_fskip(pb, 12);
  85. while(!done && !url_feof(pb)) {
  86. chunk_id = get_le32(pb);
  87. data_size = get_be32(pb);
  88. padding = data_size & 1;
  89. switch(chunk_id) {
  90. case ID_VHDR:
  91. url_fskip(pb, 12);
  92. st->codec->sample_rate = get_be16(pb);
  93. url_fskip(pb, 1);
  94. st->codec->codec_tag = get_byte(pb);
  95. url_fskip(pb, 4);
  96. break;
  97. case ID_BODY:
  98. iff->body_size = data_size;
  99. done = 1;
  100. break;
  101. case ID_CHAN:
  102. st->codec->channels = (get_be32(pb) < 6) ? 1 : 2;
  103. break;
  104. default:
  105. url_fseek(pb, data_size + padding, SEEK_CUR);
  106. break;
  107. }
  108. }
  109. if(!st->codec->sample_rate)
  110. return AVERROR_INVALIDDATA;
  111. av_set_pts_info(st, 32, 1, st->codec->sample_rate);
  112. st->codec->codec_type = CODEC_TYPE_AUDIO;
  113. switch(st->codec->codec_tag) {
  114. case COMP_NONE:
  115. st->codec->codec_id = CODEC_ID_PCM_S8;
  116. break;
  117. case COMP_FIB:
  118. st->codec->codec_id = CODEC_ID_8SVX_FIB;
  119. break;
  120. case COMP_EXP:
  121. st->codec->codec_id = CODEC_ID_8SVX_EXP;
  122. break;
  123. default:
  124. av_log(s, AV_LOG_ERROR, "iff: unknown compression method\n");
  125. return -1;
  126. }
  127. st->codec->bits_per_coded_sample = 8;
  128. st->codec->bit_rate = st->codec->channels * st->codec->sample_rate * st->codec->bits_per_coded_sample;
  129. st->codec->block_align = st->codec->channels * st->codec->bits_per_coded_sample;
  130. return 0;
  131. }
  132. static int iff_read_packet(AVFormatContext *s,
  133. AVPacket *pkt)
  134. {
  135. IffDemuxContext *iff = s->priv_data;
  136. ByteIOContext *pb = s->pb;
  137. int ret;
  138. if(iff->sent_bytes > iff->body_size)
  139. return AVERROR(EIO);
  140. if(s->streams[0]->codec->channels == 2) {
  141. uint8_t sample_buffer[PACKET_SIZE];
  142. ret = get_buffer(pb, sample_buffer, PACKET_SIZE);
  143. if(av_new_packet(pkt, PACKET_SIZE) < 0) {
  144. av_log(s, AV_LOG_ERROR, "iff: cannot allocate packet \n");
  145. return AVERROR(ENOMEM);
  146. }
  147. interleave_stereo(sample_buffer, pkt->data, PACKET_SIZE);
  148. }
  149. else {
  150. ret = av_get_packet(pb, pkt, PACKET_SIZE);
  151. }
  152. if(iff->sent_bytes == 0)
  153. pkt->flags |= PKT_FLAG_KEY;
  154. iff->sent_bytes += PACKET_SIZE;
  155. pkt->stream_index = 0;
  156. pkt->pts = iff->audio_frame_count;
  157. iff->audio_frame_count += ret / s->streams[0]->codec->channels;
  158. return ret;
  159. }
  160. AVInputFormat iff_demuxer = {
  161. "IFF",
  162. NULL_IF_CONFIG_SMALL("IFF format"),
  163. sizeof(IffDemuxContext),
  164. iff_probe,
  165. iff_read_header,
  166. iff_read_packet,
  167. };