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.

316 lines
9.4KB

  1. /*
  2. * AIFF/AIFF-C demuxer
  3. * Copyright (c) 2006 Patrick Guimond
  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. #include "libavutil/intfloat_readwrite.h"
  22. #include "avformat.h"
  23. #include "raw.h"
  24. #include "aiff.h"
  25. #define AIFF 0
  26. #define AIFF_C_VERSION1 0xA2805140
  27. typedef struct {
  28. int64_t data_end;
  29. } AIFFInputContext;
  30. static enum CodecID aiff_codec_get_id(int bps)
  31. {
  32. if (bps <= 8)
  33. return CODEC_ID_PCM_S8;
  34. if (bps <= 16)
  35. return CODEC_ID_PCM_S16BE;
  36. if (bps <= 24)
  37. return CODEC_ID_PCM_S24BE;
  38. if (bps <= 32)
  39. return CODEC_ID_PCM_S32BE;
  40. /* bigger than 32 isn't allowed */
  41. return CODEC_ID_NONE;
  42. }
  43. /* returns the size of the found tag */
  44. static int get_tag(ByteIOContext *pb, uint32_t * tag)
  45. {
  46. int size;
  47. if (url_feof(pb))
  48. return AVERROR(EIO);
  49. *tag = get_le32(pb);
  50. size = get_be32(pb);
  51. if (size < 0)
  52. size = 0x7fffffff;
  53. return size;
  54. }
  55. /* Metadata string read */
  56. static void get_meta(AVFormatContext *s, const char *key, int size)
  57. {
  58. uint8_t str[1024];
  59. int res = get_buffer(s->pb, str, FFMIN(sizeof(str)-1, size));
  60. if (res < 0)
  61. return;
  62. str[res] = 0;
  63. if (size & 1)
  64. size++;
  65. size -= res;
  66. if (size)
  67. url_fskip(s->pb, size);
  68. av_metadata_set(&s->metadata, key, str);
  69. }
  70. /* Returns the number of sound data frames or negative on error */
  71. static unsigned int get_aiff_header(ByteIOContext *pb, AVCodecContext *codec,
  72. int size, unsigned version)
  73. {
  74. AVExtFloat ext;
  75. double sample_rate;
  76. unsigned int num_frames;
  77. if (size & 1)
  78. size++;
  79. codec->codec_type = CODEC_TYPE_AUDIO;
  80. codec->channels = get_be16(pb);
  81. num_frames = get_be32(pb);
  82. codec->bits_per_coded_sample = get_be16(pb);
  83. get_buffer(pb, (uint8_t*)&ext, sizeof(ext));/* Sample rate is in */
  84. sample_rate = av_ext2dbl(ext); /* 80 bits BE IEEE extended float */
  85. codec->sample_rate = sample_rate;
  86. size -= 18;
  87. /* Got an AIFF-C? */
  88. if (version == AIFF_C_VERSION1) {
  89. codec->codec_tag = get_le32(pb);
  90. codec->codec_id = ff_codec_get_id(ff_codec_aiff_tags, codec->codec_tag);
  91. switch (codec->codec_id) {
  92. case CODEC_ID_PCM_S16BE:
  93. codec->codec_id = aiff_codec_get_id(codec->bits_per_coded_sample);
  94. codec->bits_per_coded_sample = av_get_bits_per_sample(codec->codec_id);
  95. break;
  96. case CODEC_ID_ADPCM_IMA_QT:
  97. codec->block_align = 34*codec->channels;
  98. codec->frame_size = 64;
  99. break;
  100. case CODEC_ID_MACE3:
  101. codec->block_align = 2*codec->channels;
  102. codec->frame_size = 6;
  103. break;
  104. case CODEC_ID_MACE6:
  105. codec->block_align = 1*codec->channels;
  106. codec->frame_size = 6;
  107. break;
  108. case CODEC_ID_GSM:
  109. codec->block_align = 33;
  110. codec->frame_size = 160;
  111. break;
  112. default:
  113. break;
  114. }
  115. size -= 4;
  116. } else {
  117. /* Need the codec type */
  118. codec->codec_id = aiff_codec_get_id(codec->bits_per_coded_sample);
  119. codec->bits_per_coded_sample = av_get_bits_per_sample(codec->codec_id);
  120. }
  121. /* Block align needs to be computed in all cases, as the definition
  122. * is specific to applications -> here we use the WAVE format definition */
  123. if (!codec->block_align)
  124. codec->block_align = (codec->bits_per_coded_sample * codec->channels) >> 3;
  125. codec->bit_rate = (codec->frame_size ? codec->sample_rate/codec->frame_size :
  126. codec->sample_rate) * (codec->block_align << 3);
  127. /* Chunk is over */
  128. if (size)
  129. url_fseek(pb, size, SEEK_CUR);
  130. return num_frames;
  131. }
  132. static int aiff_probe(AVProbeData *p)
  133. {
  134. /* check file header */
  135. if (p->buf[0] == 'F' && p->buf[1] == 'O' &&
  136. p->buf[2] == 'R' && p->buf[3] == 'M' &&
  137. p->buf[8] == 'A' && p->buf[9] == 'I' &&
  138. p->buf[10] == 'F' && (p->buf[11] == 'F' || p->buf[11] == 'C'))
  139. return AVPROBE_SCORE_MAX;
  140. else
  141. return 0;
  142. }
  143. /* aiff input */
  144. static int aiff_read_header(AVFormatContext *s,
  145. AVFormatParameters *ap)
  146. {
  147. int size, filesize;
  148. int64_t offset = 0;
  149. uint32_t tag;
  150. unsigned version = AIFF_C_VERSION1;
  151. ByteIOContext *pb = s->pb;
  152. AVStream * st;
  153. AIFFInputContext *aiff = s->priv_data;
  154. /* check FORM header */
  155. filesize = get_tag(pb, &tag);
  156. if (filesize < 0 || tag != MKTAG('F', 'O', 'R', 'M'))
  157. return AVERROR_INVALIDDATA;
  158. /* AIFF data type */
  159. tag = get_le32(pb);
  160. if (tag == MKTAG('A', 'I', 'F', 'F')) /* Got an AIFF file */
  161. version = AIFF;
  162. else if (tag != MKTAG('A', 'I', 'F', 'C')) /* An AIFF-C file then */
  163. return AVERROR_INVALIDDATA;
  164. filesize -= 4;
  165. st = av_new_stream(s, 0);
  166. if (!st)
  167. return AVERROR(ENOMEM);
  168. while (filesize > 0) {
  169. /* parse different chunks */
  170. size = get_tag(pb, &tag);
  171. if (size < 0)
  172. return size;
  173. filesize -= size + 8;
  174. switch (tag) {
  175. case MKTAG('C', 'O', 'M', 'M'): /* Common chunk */
  176. /* Then for the complete header info */
  177. st->nb_frames = get_aiff_header(pb, st->codec, size, version);
  178. if (st->nb_frames < 0)
  179. return st->nb_frames;
  180. if (offset > 0) // COMM is after SSND
  181. goto got_sound;
  182. break;
  183. case MKTAG('F', 'V', 'E', 'R'): /* Version chunk */
  184. version = get_be32(pb);
  185. break;
  186. case MKTAG('N', 'A', 'M', 'E'): /* Sample name chunk */
  187. get_meta(s, "title" , size);
  188. break;
  189. case MKTAG('A', 'U', 'T', 'H'): /* Author chunk */
  190. get_meta(s, "author" , size);
  191. break;
  192. case MKTAG('(', 'c', ')', ' '): /* Copyright chunk */
  193. get_meta(s, "copyright", size);
  194. break;
  195. case MKTAG('A', 'N', 'N', 'O'): /* Annotation chunk */
  196. get_meta(s, "comment" , size);
  197. break;
  198. case MKTAG('S', 'S', 'N', 'D'): /* Sampled sound chunk */
  199. aiff->data_end = url_ftell(pb) + size;
  200. offset = get_be32(pb); /* Offset of sound data */
  201. get_be32(pb); /* BlockSize... don't care */
  202. offset += url_ftell(pb); /* Compute absolute data offset */
  203. if (st->codec->block_align) /* Assume COMM already parsed */
  204. goto got_sound;
  205. if (url_is_streamed(pb)) {
  206. av_log(s, AV_LOG_ERROR, "file is not seekable\n");
  207. return -1;
  208. }
  209. url_fskip(pb, size - 8);
  210. break;
  211. case MKTAG('w', 'a', 'v', 'e'):
  212. if ((uint64_t)size > (1<<30))
  213. return -1;
  214. st->codec->extradata = av_mallocz(size + FF_INPUT_BUFFER_PADDING_SIZE);
  215. if (!st->codec->extradata)
  216. return AVERROR(ENOMEM);
  217. st->codec->extradata_size = size;
  218. get_buffer(pb, st->codec->extradata, size);
  219. break;
  220. default: /* Jump */
  221. if (size & 1) /* Always even aligned */
  222. size++;
  223. url_fskip (pb, size);
  224. }
  225. }
  226. if (!st->codec->block_align) {
  227. av_log(s, AV_LOG_ERROR, "could not find COMM tag\n");
  228. return -1;
  229. }
  230. got_sound:
  231. /* Now positioned, get the sound data start and end */
  232. if (st->nb_frames)
  233. s->file_size = st->nb_frames * st->codec->block_align;
  234. av_set_pts_info(st, 64, 1, st->codec->sample_rate);
  235. st->start_time = 0;
  236. st->duration = st->codec->frame_size ?
  237. st->nb_frames * st->codec->frame_size : st->nb_frames;
  238. /* Position the stream at the first block */
  239. url_fseek(pb, offset, SEEK_SET);
  240. return 0;
  241. }
  242. #define MAX_SIZE 4096
  243. static int aiff_read_packet(AVFormatContext *s,
  244. AVPacket *pkt)
  245. {
  246. AVStream *st = s->streams[0];
  247. AIFFInputContext *aiff = s->priv_data;
  248. int64_t max_size;
  249. int res;
  250. /* calculate size of remaining data */
  251. max_size = aiff->data_end - url_ftell(s->pb);
  252. if (max_size <= 0)
  253. return AVERROR_EOF;
  254. /* Now for that packet */
  255. max_size = FFMIN(max_size, (MAX_SIZE / st->codec->block_align) * st->codec->block_align);
  256. res = av_get_packet(s->pb, pkt, max_size);
  257. if (res < 0)
  258. return res;
  259. /* Only one stream in an AIFF file */
  260. pkt->stream_index = 0;
  261. return 0;
  262. }
  263. AVInputFormat aiff_demuxer = {
  264. "aiff",
  265. NULL_IF_CONFIG_SMALL("Audio IFF"),
  266. sizeof(AIFFInputContext),
  267. aiff_probe,
  268. aiff_read_header,
  269. aiff_read_packet,
  270. NULL,
  271. pcm_read_seek,
  272. .codec_tag= (const AVCodecTag* const []){ff_codec_aiff_tags, 0},
  273. };