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.

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