aiffdec.c 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324
  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 "pcm.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(AVIOContext *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 = av_malloc(size+1);
  59. int res;
  60. if (!str) {
  61. url_fskip(s->pb, size);
  62. return;
  63. }
  64. res = get_buffer(s->pb, str, size);
  65. if (res < 0)
  66. return;
  67. str[res] = 0;
  68. av_metadata_set2(&s->metadata, key, str, AV_METADATA_DONT_STRDUP_VAL);
  69. }
  70. /* Returns the number of sound data frames or negative on error */
  71. static unsigned int get_aiff_header(AVIOContext *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 = AVMEDIA_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. case CODEC_ID_QCELP:
  113. codec->block_align = 35;
  114. codec->frame_size= 160;
  115. break;
  116. default:
  117. break;
  118. }
  119. size -= 4;
  120. } else {
  121. /* Need the codec type */
  122. codec->codec_id = aiff_codec_get_id(codec->bits_per_coded_sample);
  123. codec->bits_per_coded_sample = av_get_bits_per_sample(codec->codec_id);
  124. }
  125. /* Block align needs to be computed in all cases, as the definition
  126. * is specific to applications -> here we use the WAVE format definition */
  127. if (!codec->block_align)
  128. codec->block_align = (codec->bits_per_coded_sample * codec->channels) >> 3;
  129. codec->bit_rate = (codec->frame_size ? codec->sample_rate/codec->frame_size :
  130. codec->sample_rate) * (codec->block_align << 3);
  131. /* Chunk is over */
  132. if (size)
  133. url_fseek(pb, size, SEEK_CUR);
  134. return num_frames;
  135. }
  136. static int aiff_probe(AVProbeData *p)
  137. {
  138. /* check file header */
  139. if (p->buf[0] == 'F' && p->buf[1] == 'O' &&
  140. p->buf[2] == 'R' && p->buf[3] == 'M' &&
  141. p->buf[8] == 'A' && p->buf[9] == 'I' &&
  142. p->buf[10] == 'F' && (p->buf[11] == 'F' || p->buf[11] == 'C'))
  143. return AVPROBE_SCORE_MAX;
  144. else
  145. return 0;
  146. }
  147. /* aiff input */
  148. static int aiff_read_header(AVFormatContext *s,
  149. AVFormatParameters *ap)
  150. {
  151. int size, filesize;
  152. int64_t offset = 0;
  153. uint32_t tag;
  154. unsigned version = AIFF_C_VERSION1;
  155. AVIOContext *pb = s->pb;
  156. AVStream * st;
  157. AIFFInputContext *aiff = s->priv_data;
  158. /* check FORM header */
  159. filesize = get_tag(pb, &tag);
  160. if (filesize < 0 || tag != MKTAG('F', 'O', 'R', 'M'))
  161. return AVERROR_INVALIDDATA;
  162. /* AIFF data type */
  163. tag = get_le32(pb);
  164. if (tag == MKTAG('A', 'I', 'F', 'F')) /* Got an AIFF file */
  165. version = AIFF;
  166. else if (tag != MKTAG('A', 'I', 'F', 'C')) /* An AIFF-C file then */
  167. return AVERROR_INVALIDDATA;
  168. filesize -= 4;
  169. st = av_new_stream(s, 0);
  170. if (!st)
  171. return AVERROR(ENOMEM);
  172. while (filesize > 0) {
  173. /* parse different chunks */
  174. size = get_tag(pb, &tag);
  175. if (size < 0)
  176. return size;
  177. filesize -= size + 8;
  178. switch (tag) {
  179. case MKTAG('C', 'O', 'M', 'M'): /* Common chunk */
  180. /* Then for the complete header info */
  181. st->nb_frames = get_aiff_header(pb, st->codec, size, version);
  182. if (st->nb_frames < 0)
  183. return st->nb_frames;
  184. if (offset > 0) // COMM is after SSND
  185. goto got_sound;
  186. break;
  187. case MKTAG('F', 'V', 'E', 'R'): /* Version chunk */
  188. version = get_be32(pb);
  189. break;
  190. case MKTAG('N', 'A', 'M', 'E'): /* Sample name chunk */
  191. get_meta(s, "title" , size);
  192. break;
  193. case MKTAG('A', 'U', 'T', 'H'): /* Author chunk */
  194. get_meta(s, "author" , size);
  195. break;
  196. case MKTAG('(', 'c', ')', ' '): /* Copyright chunk */
  197. get_meta(s, "copyright", size);
  198. break;
  199. case MKTAG('A', 'N', 'N', 'O'): /* Annotation chunk */
  200. get_meta(s, "comment" , size);
  201. break;
  202. case MKTAG('S', 'S', 'N', 'D'): /* Sampled sound chunk */
  203. aiff->data_end = url_ftell(pb) + size;
  204. offset = get_be32(pb); /* Offset of sound data */
  205. get_be32(pb); /* BlockSize... don't care */
  206. offset += url_ftell(pb); /* Compute absolute data offset */
  207. if (st->codec->block_align) /* Assume COMM already parsed */
  208. goto got_sound;
  209. if (url_is_streamed(pb)) {
  210. av_log(s, AV_LOG_ERROR, "file is not seekable\n");
  211. return -1;
  212. }
  213. url_fskip(pb, size - 8);
  214. break;
  215. case MKTAG('w', 'a', 'v', 'e'):
  216. if ((uint64_t)size > (1<<30))
  217. return -1;
  218. st->codec->extradata = av_mallocz(size + FF_INPUT_BUFFER_PADDING_SIZE);
  219. if (!st->codec->extradata)
  220. return AVERROR(ENOMEM);
  221. st->codec->extradata_size = size;
  222. get_buffer(pb, st->codec->extradata, size);
  223. break;
  224. default: /* Jump */
  225. if (size & 1) /* Always even aligned */
  226. size++;
  227. url_fskip (pb, size);
  228. }
  229. }
  230. if (!st->codec->block_align) {
  231. av_log(s, AV_LOG_ERROR, "could not find COMM tag\n");
  232. return -1;
  233. }
  234. got_sound:
  235. /* Now positioned, get the sound data start and end */
  236. if (st->nb_frames)
  237. s->file_size = st->nb_frames * st->codec->block_align;
  238. av_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. url_fseek(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 - url_ftell(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. "aiff",
  273. NULL_IF_CONFIG_SMALL("Audio IFF"),
  274. sizeof(AIFFInputContext),
  275. aiff_probe,
  276. aiff_read_header,
  277. aiff_read_packet,
  278. NULL,
  279. pcm_read_seek,
  280. .codec_tag= (const AVCodecTag* const []){ff_codec_aiff_tags, 0},
  281. };