aiffdec.c 11 KB

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