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.

332 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. /* Got an 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. switch (codec->codec_id) {
  100. case CODEC_ID_PCM_S16BE:
  101. codec->codec_id = aiff_codec_get_id(codec->bits_per_coded_sample);
  102. codec->bits_per_coded_sample = av_get_bits_per_sample(codec->codec_id);
  103. aiff->block_duration = 1;
  104. break;
  105. case CODEC_ID_ADPCM_IMA_QT:
  106. codec->block_align = 34*codec->channels;
  107. aiff->block_duration = 64;
  108. break;
  109. case CODEC_ID_MACE3:
  110. codec->block_align = 2*codec->channels;
  111. aiff->block_duration = 6;
  112. break;
  113. case CODEC_ID_MACE6:
  114. codec->block_align = 1*codec->channels;
  115. aiff->block_duration = 6;
  116. break;
  117. case CODEC_ID_GSM:
  118. codec->block_align = 33;
  119. aiff->block_duration = 160;
  120. break;
  121. case CODEC_ID_QCELP:
  122. codec->block_align = 35;
  123. aiff->block_duration = 160;
  124. break;
  125. default:
  126. break;
  127. }
  128. size -= 4;
  129. } else {
  130. /* Need the codec type */
  131. codec->codec_id = aiff_codec_get_id(codec->bits_per_coded_sample);
  132. codec->bits_per_coded_sample = av_get_bits_per_sample(codec->codec_id);
  133. aiff->block_duration = 1;
  134. }
  135. /* Block align needs to be computed in all cases, as the definition
  136. * is specific to applications -> here we use the WAVE format definition */
  137. if (!codec->block_align)
  138. codec->block_align = (codec->bits_per_coded_sample * codec->channels) >> 3;
  139. if (aiff->block_duration) {
  140. codec->bit_rate = codec->sample_rate * (codec->block_align << 3) /
  141. aiff->block_duration;
  142. }
  143. /* Chunk is over */
  144. if (size)
  145. avio_skip(pb, size);
  146. return num_frames;
  147. }
  148. static int aiff_probe(AVProbeData *p)
  149. {
  150. /* check file header */
  151. if (p->buf[0] == 'F' && p->buf[1] == 'O' &&
  152. p->buf[2] == 'R' && p->buf[3] == 'M' &&
  153. p->buf[8] == 'A' && p->buf[9] == 'I' &&
  154. p->buf[10] == 'F' && (p->buf[11] == 'F' || p->buf[11] == 'C'))
  155. return AVPROBE_SCORE_MAX;
  156. else
  157. return 0;
  158. }
  159. /* aiff input */
  160. static int aiff_read_header(AVFormatContext *s)
  161. {
  162. int size, filesize;
  163. int64_t offset = 0;
  164. uint32_t tag;
  165. unsigned version = AIFF_C_VERSION1;
  166. AVIOContext *pb = s->pb;
  167. AVStream * st;
  168. AIFFInputContext *aiff = s->priv_data;
  169. /* check FORM header */
  170. filesize = get_tag(pb, &tag);
  171. if (filesize < 0 || tag != MKTAG('F', 'O', 'R', 'M'))
  172. return AVERROR_INVALIDDATA;
  173. /* AIFF data type */
  174. tag = avio_rl32(pb);
  175. if (tag == MKTAG('A', 'I', 'F', 'F')) /* Got an AIFF file */
  176. version = AIFF;
  177. else if (tag != MKTAG('A', 'I', 'F', 'C')) /* An AIFF-C file then */
  178. return AVERROR_INVALIDDATA;
  179. filesize -= 4;
  180. st = avformat_new_stream(s, NULL);
  181. if (!st)
  182. return AVERROR(ENOMEM);
  183. while (filesize > 0) {
  184. /* parse different chunks */
  185. size = get_tag(pb, &tag);
  186. if (size < 0)
  187. return size;
  188. filesize -= size + 8;
  189. switch (tag) {
  190. case MKTAG('C', 'O', 'M', 'M'): /* Common chunk */
  191. /* Then for the complete header info */
  192. st->nb_frames = get_aiff_header(s, size, version);
  193. if (st->nb_frames < 0)
  194. return st->nb_frames;
  195. if (offset > 0) // COMM is after SSND
  196. goto got_sound;
  197. break;
  198. case MKTAG('F', 'V', 'E', 'R'): /* Version chunk */
  199. version = avio_rb32(pb);
  200. break;
  201. case MKTAG('N', 'A', 'M', 'E'): /* Sample name chunk */
  202. get_meta(s, "title" , size);
  203. break;
  204. case MKTAG('A', 'U', 'T', 'H'): /* Author chunk */
  205. get_meta(s, "author" , size);
  206. break;
  207. case MKTAG('(', 'c', ')', ' '): /* Copyright chunk */
  208. get_meta(s, "copyright", size);
  209. break;
  210. case MKTAG('A', 'N', 'N', 'O'): /* Annotation chunk */
  211. get_meta(s, "comment" , size);
  212. break;
  213. case MKTAG('S', 'S', 'N', 'D'): /* Sampled sound chunk */
  214. aiff->data_end = avio_tell(pb) + size;
  215. offset = avio_rb32(pb); /* Offset of sound data */
  216. avio_rb32(pb); /* BlockSize... don't care */
  217. offset += avio_tell(pb); /* Compute absolute data offset */
  218. if (st->codec->block_align) /* Assume COMM already parsed */
  219. goto got_sound;
  220. if (!pb->seekable) {
  221. av_log(s, AV_LOG_ERROR, "file is not seekable\n");
  222. return -1;
  223. }
  224. avio_skip(pb, size - 8);
  225. break;
  226. case MKTAG('w', 'a', 'v', 'e'):
  227. if ((uint64_t)size > (1<<30))
  228. return -1;
  229. st->codec->extradata = av_mallocz(size + FF_INPUT_BUFFER_PADDING_SIZE);
  230. if (!st->codec->extradata)
  231. return AVERROR(ENOMEM);
  232. st->codec->extradata_size = size;
  233. avio_read(pb, st->codec->extradata, size);
  234. break;
  235. default: /* Jump */
  236. if (size & 1) /* Always even aligned */
  237. size++;
  238. avio_skip(pb, size);
  239. }
  240. }
  241. got_sound:
  242. if (!st->codec->block_align) {
  243. av_log(s, AV_LOG_ERROR, "could not find COMM tag or invalid block_align value\n");
  244. return -1;
  245. }
  246. /* Now positioned, get the sound data start and end */
  247. avpriv_set_pts_info(st, 64, 1, st->codec->sample_rate);
  248. st->start_time = 0;
  249. st->duration = st->nb_frames * aiff->block_duration;
  250. /* Position the stream at the first block */
  251. avio_seek(pb, offset, SEEK_SET);
  252. return 0;
  253. }
  254. #define MAX_SIZE 4096
  255. static int aiff_read_packet(AVFormatContext *s,
  256. AVPacket *pkt)
  257. {
  258. AVStream *st = s->streams[0];
  259. AIFFInputContext *aiff = s->priv_data;
  260. int64_t max_size;
  261. int res, size;
  262. /* calculate size of remaining data */
  263. max_size = aiff->data_end - avio_tell(s->pb);
  264. if (max_size <= 0)
  265. return AVERROR_EOF;
  266. /* Now for that packet */
  267. if (st->codec->block_align >= 33) // GSM, QCLP, IMA4
  268. size = st->codec->block_align;
  269. else
  270. size = (MAX_SIZE / st->codec->block_align) * st->codec->block_align;
  271. size = FFMIN(max_size, size);
  272. res = av_get_packet(s->pb, pkt, size);
  273. if (res < 0)
  274. return res;
  275. /* Only one stream in an AIFF file */
  276. pkt->stream_index = 0;
  277. pkt->duration = (res / st->codec->block_align) * aiff->block_duration;
  278. return 0;
  279. }
  280. AVInputFormat ff_aiff_demuxer = {
  281. .name = "aiff",
  282. .long_name = NULL_IF_CONFIG_SMALL("Audio IFF"),
  283. .priv_data_size = sizeof(AIFFInputContext),
  284. .read_probe = aiff_probe,
  285. .read_header = aiff_read_header,
  286. .read_packet = aiff_read_packet,
  287. .read_seek = ff_pcm_read_seek,
  288. .codec_tag= (const AVCodecTag* const []){ff_codec_aiff_tags, 0},
  289. };