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.

333 lines
9.9KB

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