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.

323 lines
9.7KB

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