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.

375 lines
12KB

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