aiffdec.c 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331
  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/mathematics.h"
  22. #include "libavutil/dict.h"
  23. #include "avformat.h"
  24. #include "internal.h"
  25. #include "pcm.h"
  26. #include "aiff.h"
  27. #include "isom.h"
  28. #define AIFF 0
  29. #define AIFF_C_VERSION1 0xA2805140
  30. typedef struct {
  31. int64_t data_end;
  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 (url_feof(pb))
  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. if (str) {
  63. int res = avio_read(s->pb, str, size);
  64. if (res < 0){
  65. av_free(str);
  66. return;
  67. }
  68. size += (size&1)-res;
  69. str[res] = 0;
  70. av_dict_set(&s->metadata, key, str, AV_METADATA_DONT_STRDUP_VAL);
  71. }else
  72. size+= size&1;
  73. avio_skip(s->pb, size);
  74. }
  75. /* Returns the number of sound data frames or negative on error */
  76. static unsigned int get_aiff_header(AVIOContext *pb, AVCodecContext *codec,
  77. int size, unsigned version)
  78. {
  79. int exp;
  80. uint64_t val;
  81. double sample_rate;
  82. unsigned int num_frames;
  83. if (size & 1)
  84. size++;
  85. codec->codec_type = AVMEDIA_TYPE_AUDIO;
  86. codec->channels = avio_rb16(pb);
  87. num_frames = avio_rb32(pb);
  88. codec->bits_per_coded_sample = avio_rb16(pb);
  89. exp = avio_rb16(pb);
  90. val = avio_rb64(pb);
  91. sample_rate = ldexp(val, exp - 16383 - 63);
  92. codec->sample_rate = sample_rate;
  93. size -= 18;
  94. /* Got an AIFF-C? */
  95. if (version == AIFF_C_VERSION1) {
  96. codec->codec_tag = avio_rl32(pb);
  97. codec->codec_id = ff_codec_get_id(ff_codec_aiff_tags, codec->codec_tag);
  98. switch (codec->codec_id) {
  99. case CODEC_ID_PCM_S16BE:
  100. codec->codec_id = aiff_codec_get_id(codec->bits_per_coded_sample);
  101. codec->bits_per_coded_sample = av_get_bits_per_sample(codec->codec_id);
  102. break;
  103. case CODEC_ID_ADPCM_IMA_QT:
  104. codec->block_align = 34*codec->channels;
  105. codec->frame_size = 64;
  106. break;
  107. case CODEC_ID_MACE3:
  108. codec->block_align = 2*codec->channels;
  109. codec->frame_size = 6;
  110. break;
  111. case CODEC_ID_MACE6:
  112. codec->block_align = 1*codec->channels;
  113. codec->frame_size = 6;
  114. break;
  115. case CODEC_ID_GSM:
  116. codec->block_align = 33;
  117. codec->frame_size = 160;
  118. break;
  119. case CODEC_ID_QCELP:
  120. codec->block_align = 35;
  121. codec->frame_size= 160;
  122. break;
  123. default:
  124. break;
  125. }
  126. size -= 4;
  127. } else {
  128. /* Need the codec type */
  129. codec->codec_id = aiff_codec_get_id(codec->bits_per_coded_sample);
  130. codec->bits_per_coded_sample = av_get_bits_per_sample(codec->codec_id);
  131. }
  132. /* Block align needs to be computed in all cases, as the definition
  133. * is specific to applications -> here we use the WAVE format definition */
  134. if (!codec->block_align)
  135. codec->block_align = (codec->bits_per_coded_sample * codec->channels) >> 3;
  136. codec->bit_rate = (codec->frame_size ? codec->sample_rate/codec->frame_size :
  137. codec->sample_rate) * (codec->block_align << 3);
  138. /* Chunk is over */
  139. if (size)
  140. avio_skip(pb, size);
  141. return num_frames;
  142. }
  143. static int aiff_probe(AVProbeData *p)
  144. {
  145. /* check file header */
  146. if (p->buf[0] == 'F' && p->buf[1] == 'O' &&
  147. p->buf[2] == 'R' && p->buf[3] == 'M' &&
  148. p->buf[8] == 'A' && p->buf[9] == 'I' &&
  149. p->buf[10] == 'F' && (p->buf[11] == 'F' || p->buf[11] == 'C'))
  150. return AVPROBE_SCORE_MAX;
  151. else
  152. return 0;
  153. }
  154. /* aiff input */
  155. static int aiff_read_header(AVFormatContext *s,
  156. AVFormatParameters *ap)
  157. {
  158. int size, filesize;
  159. int64_t offset = 0;
  160. uint32_t tag;
  161. unsigned version = AIFF_C_VERSION1;
  162. AVIOContext *pb = s->pb;
  163. AVStream * st;
  164. AIFFInputContext *aiff = s->priv_data;
  165. /* check FORM header */
  166. filesize = get_tag(pb, &tag);
  167. if (filesize < 0 || tag != MKTAG('F', 'O', 'R', 'M'))
  168. return AVERROR_INVALIDDATA;
  169. /* AIFF data type */
  170. tag = avio_rl32(pb);
  171. if (tag == MKTAG('A', 'I', 'F', 'F')) /* Got an AIFF file */
  172. version = AIFF;
  173. else if (tag != MKTAG('A', 'I', 'F', 'C')) /* An AIFF-C file then */
  174. return AVERROR_INVALIDDATA;
  175. filesize -= 4;
  176. st = avformat_new_stream(s, NULL);
  177. if (!st)
  178. return AVERROR(ENOMEM);
  179. while (filesize > 0) {
  180. /* parse different chunks */
  181. size = get_tag(pb, &tag);
  182. if (size < 0)
  183. return size;
  184. filesize -= size + 8;
  185. switch (tag) {
  186. case MKTAG('C', 'O', 'M', 'M'): /* Common chunk */
  187. /* Then for the complete header info */
  188. st->nb_frames = get_aiff_header(pb, st->codec, size, version);
  189. if (st->nb_frames < 0)
  190. return st->nb_frames;
  191. if (offset > 0) // COMM is after SSND
  192. goto got_sound;
  193. break;
  194. case MKTAG('F', 'V', 'E', 'R'): /* Version chunk */
  195. version = avio_rb32(pb);
  196. break;
  197. case MKTAG('N', 'A', 'M', 'E'): /* Sample name chunk */
  198. get_meta(s, "title" , size);
  199. break;
  200. case MKTAG('A', 'U', 'T', 'H'): /* Author chunk */
  201. get_meta(s, "author" , size);
  202. break;
  203. case MKTAG('(', 'c', ')', ' '): /* Copyright chunk */
  204. get_meta(s, "copyright", size);
  205. break;
  206. case MKTAG('A', 'N', 'N', 'O'): /* Annotation chunk */
  207. get_meta(s, "comment" , size);
  208. break;
  209. case MKTAG('S', 'S', 'N', 'D'): /* Sampled sound chunk */
  210. aiff->data_end = avio_tell(pb) + size;
  211. offset = avio_rb32(pb); /* Offset of sound data */
  212. avio_rb32(pb); /* BlockSize... don't care */
  213. offset += avio_tell(pb); /* Compute absolute data offset */
  214. if (st->codec->block_align) /* Assume COMM already parsed */
  215. goto got_sound;
  216. if (!pb->seekable) {
  217. av_log(s, AV_LOG_ERROR, "file is not seekable\n");
  218. return -1;
  219. }
  220. avio_skip(pb, size - 8);
  221. break;
  222. case MKTAG('w', 'a', 'v', 'e'):
  223. if ((uint64_t)size > (1<<30))
  224. return -1;
  225. st->codec->extradata = av_mallocz(size + FF_INPUT_BUFFER_PADDING_SIZE);
  226. if (!st->codec->extradata)
  227. return AVERROR(ENOMEM);
  228. st->codec->extradata_size = size;
  229. avio_read(pb, st->codec->extradata, size);
  230. break;
  231. case MKTAG('C','H','A','N'):
  232. if (size < 12)
  233. return AVERROR_INVALIDDATA;
  234. ff_mov_read_chan(s, size, st->codec);
  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->codec->frame_size ?
  251. st->nb_frames * st->codec->frame_size : st->nb_frames;
  252. /* Position the stream at the first block */
  253. avio_seek(pb, offset, SEEK_SET);
  254. return 0;
  255. }
  256. #define MAX_SIZE 4096
  257. static int aiff_read_packet(AVFormatContext *s,
  258. AVPacket *pkt)
  259. {
  260. AVStream *st = s->streams[0];
  261. AIFFInputContext *aiff = s->priv_data;
  262. int64_t max_size;
  263. int res, size;
  264. /* calculate size of remaining data */
  265. max_size = aiff->data_end - avio_tell(s->pb);
  266. if (max_size <= 0)
  267. return AVERROR_EOF;
  268. /* Now for that packet */
  269. if (st->codec->block_align >= 33) // GSM, QCLP, IMA4
  270. size = st->codec->block_align;
  271. else
  272. size = (MAX_SIZE / st->codec->block_align) * st->codec->block_align;
  273. size = FFMIN(max_size, size);
  274. res = av_get_packet(s->pb, pkt, size);
  275. if (res < 0)
  276. return res;
  277. /* Only one stream in an AIFF file */
  278. pkt->stream_index = 0;
  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 = pcm_read_seek,
  289. .codec_tag= (const AVCodecTag* const []){ff_codec_aiff_tags, 0},
  290. };