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.

390 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 AIFFInputContext {
  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 (avio_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 (size < 4) {
  103. version = AIFF;
  104. } else if (version == AIFF_C_VERSION1) {
  105. codec->codec_tag = avio_rl32(pb);
  106. codec->codec_id = ff_codec_get_id(ff_codec_aiff_tags, codec->codec_tag);
  107. size -= 4;
  108. }
  109. if (version != AIFF_C_VERSION1 || codec->codec_id == AV_CODEC_ID_PCM_S16BE) {
  110. codec->codec_id = aiff_codec_get_id(codec->bits_per_coded_sample);
  111. codec->bits_per_coded_sample = av_get_bits_per_sample(codec->codec_id);
  112. aiff->block_duration = 1;
  113. } else {
  114. switch (codec->codec_id) {
  115. case AV_CODEC_ID_PCM_F32BE:
  116. case AV_CODEC_ID_PCM_F64BE:
  117. case AV_CODEC_ID_PCM_S16LE:
  118. case AV_CODEC_ID_PCM_ALAW:
  119. case AV_CODEC_ID_PCM_MULAW:
  120. aiff->block_duration = 1;
  121. break;
  122. case AV_CODEC_ID_ADPCM_IMA_QT:
  123. codec->block_align = 34*codec->channels;
  124. break;
  125. case AV_CODEC_ID_MACE3:
  126. codec->block_align = 2*codec->channels;
  127. break;
  128. case AV_CODEC_ID_ADPCM_G726LE:
  129. codec->bits_per_coded_sample = 5;
  130. case AV_CODEC_ID_ADPCM_G722:
  131. case AV_CODEC_ID_MACE6:
  132. codec->block_align = 1*codec->channels;
  133. break;
  134. case AV_CODEC_ID_GSM:
  135. codec->block_align = 33;
  136. break;
  137. default:
  138. aiff->block_duration = 1;
  139. break;
  140. }
  141. if (codec->block_align > 0)
  142. aiff->block_duration = av_get_audio_frame_duration(codec,
  143. codec->block_align);
  144. }
  145. /* Block align needs to be computed in all cases, as the definition
  146. * is specific to applications -> here we use the WAVE format definition */
  147. if (!codec->block_align)
  148. codec->block_align = (av_get_bits_per_sample(codec->codec_id) * codec->channels) >> 3;
  149. if (aiff->block_duration) {
  150. codec->bit_rate = codec->sample_rate * (codec->block_align << 3) /
  151. aiff->block_duration;
  152. }
  153. /* Chunk is over */
  154. if (size)
  155. avio_skip(pb, size);
  156. return num_frames;
  157. }
  158. static int aiff_probe(AVProbeData *p)
  159. {
  160. /* check file header */
  161. if (p->buf[0] == 'F' && p->buf[1] == 'O' &&
  162. p->buf[2] == 'R' && p->buf[3] == 'M' &&
  163. p->buf[8] == 'A' && p->buf[9] == 'I' &&
  164. p->buf[10] == 'F' && (p->buf[11] == 'F' || p->buf[11] == 'C'))
  165. return AVPROBE_SCORE_MAX;
  166. else
  167. return 0;
  168. }
  169. /* aiff input */
  170. static int aiff_read_header(AVFormatContext *s)
  171. {
  172. int ret, size, filesize;
  173. int64_t offset = 0, position;
  174. uint32_t tag;
  175. unsigned version = AIFF_C_VERSION1;
  176. AVIOContext *pb = s->pb;
  177. AVStream * st;
  178. AIFFInputContext *aiff = s->priv_data;
  179. ID3v2ExtraMeta *id3v2_extra_meta = NULL;
  180. /* check FORM header */
  181. filesize = get_tag(pb, &tag);
  182. if (filesize < 0 || tag != MKTAG('F', 'O', 'R', 'M'))
  183. return AVERROR_INVALIDDATA;
  184. /* AIFF data type */
  185. tag = avio_rl32(pb);
  186. if (tag == MKTAG('A', 'I', 'F', 'F')) /* Got an AIFF file */
  187. version = AIFF;
  188. else if (tag != MKTAG('A', 'I', 'F', 'C')) /* An AIFF-C file then */
  189. return AVERROR_INVALIDDATA;
  190. filesize -= 4;
  191. st = avformat_new_stream(s, NULL);
  192. if (!st)
  193. return AVERROR(ENOMEM);
  194. while (filesize > 0) {
  195. /* parse different chunks */
  196. size = get_tag(pb, &tag);
  197. if (size == AVERROR_EOF && offset > 0 && st->codec->block_align) {
  198. av_log(s, AV_LOG_WARNING, "header parser hit EOF\n");
  199. goto got_sound;
  200. }
  201. if (size < 0)
  202. return size;
  203. filesize -= size + 8;
  204. switch (tag) {
  205. case MKTAG('C', 'O', 'M', 'M'): /* Common chunk */
  206. /* Then for the complete header info */
  207. st->nb_frames = get_aiff_header(s, size, version);
  208. if (st->nb_frames < 0)
  209. return st->nb_frames;
  210. if (offset > 0) // COMM is after SSND
  211. goto got_sound;
  212. break;
  213. case MKTAG('I', 'D', '3', ' '):
  214. position = avio_tell(pb);
  215. ff_id3v2_read(s, ID3v2_DEFAULT_MAGIC, &id3v2_extra_meta, size);
  216. if (id3v2_extra_meta)
  217. if ((ret = ff_id3v2_parse_apic(s, &id3v2_extra_meta)) < 0) {
  218. ff_id3v2_free_extra_meta(&id3v2_extra_meta);
  219. return ret;
  220. }
  221. ff_id3v2_free_extra_meta(&id3v2_extra_meta);
  222. if (position + size > avio_tell(pb))
  223. avio_skip(pb, position + size - avio_tell(pb));
  224. break;
  225. case MKTAG('F', 'V', 'E', 'R'): /* Version chunk */
  226. version = avio_rb32(pb);
  227. break;
  228. case MKTAG('N', 'A', 'M', 'E'): /* Sample name chunk */
  229. get_meta(s, "title" , size);
  230. break;
  231. case MKTAG('A', 'U', 'T', 'H'): /* Author chunk */
  232. get_meta(s, "author" , size);
  233. break;
  234. case MKTAG('(', 'c', ')', ' '): /* Copyright chunk */
  235. get_meta(s, "copyright", size);
  236. break;
  237. case MKTAG('A', 'N', 'N', 'O'): /* Annotation chunk */
  238. get_meta(s, "comment" , size);
  239. break;
  240. case MKTAG('S', 'S', 'N', 'D'): /* Sampled sound chunk */
  241. aiff->data_end = avio_tell(pb) + size;
  242. offset = avio_rb32(pb); /* Offset of sound data */
  243. avio_rb32(pb); /* BlockSize... don't care */
  244. offset += avio_tell(pb); /* Compute absolute data offset */
  245. if (st->codec->block_align && !pb->seekable) /* Assume COMM already parsed */
  246. goto got_sound;
  247. if (!pb->seekable) {
  248. av_log(s, AV_LOG_ERROR, "file is not seekable\n");
  249. return -1;
  250. }
  251. avio_skip(pb, size - 8);
  252. break;
  253. case MKTAG('w', 'a', 'v', 'e'):
  254. if ((uint64_t)size > (1<<30))
  255. return -1;
  256. if (ff_get_extradata(st->codec, pb, size) < 0)
  257. return AVERROR(ENOMEM);
  258. if (st->codec->codec_id == AV_CODEC_ID_QDM2 && size>=12*4 && !st->codec->block_align) {
  259. st->codec->block_align = AV_RB32(st->codec->extradata+11*4);
  260. aiff->block_duration = AV_RB32(st->codec->extradata+9*4);
  261. } else if (st->codec->codec_id == AV_CODEC_ID_QCELP) {
  262. char rate = 0;
  263. if (size >= 25)
  264. rate = st->codec->extradata[24];
  265. switch (rate) {
  266. case 'H': // RATE_HALF
  267. st->codec->block_align = 17;
  268. break;
  269. case 'F': // RATE_FULL
  270. default:
  271. st->codec->block_align = 35;
  272. }
  273. aiff->block_duration = 160;
  274. st->codec->bit_rate = st->codec->sample_rate * (st->codec->block_align << 3) /
  275. aiff->block_duration;
  276. }
  277. break;
  278. case MKTAG('C','H','A','N'):
  279. if(ff_mov_read_chan(s, pb, st, size) < 0)
  280. return AVERROR_INVALIDDATA;
  281. break;
  282. case 0:
  283. if (offset > 0 && st->codec->block_align) // COMM && SSND
  284. goto got_sound;
  285. default: /* Jump */
  286. if (size & 1) /* Always even aligned */
  287. size++;
  288. avio_skip(pb, size);
  289. }
  290. }
  291. got_sound:
  292. if (!st->codec->block_align) {
  293. av_log(s, AV_LOG_ERROR, "could not find COMM tag or invalid block_align value\n");
  294. return -1;
  295. }
  296. /* Now positioned, get the sound data start and end */
  297. avpriv_set_pts_info(st, 64, 1, st->codec->sample_rate);
  298. st->start_time = 0;
  299. st->duration = st->nb_frames * aiff->block_duration;
  300. /* Position the stream at the first block */
  301. avio_seek(pb, offset, SEEK_SET);
  302. return 0;
  303. }
  304. #define MAX_SIZE 4096
  305. static int aiff_read_packet(AVFormatContext *s,
  306. AVPacket *pkt)
  307. {
  308. AVStream *st = s->streams[0];
  309. AIFFInputContext *aiff = s->priv_data;
  310. int64_t max_size;
  311. int res, size;
  312. /* calculate size of remaining data */
  313. max_size = aiff->data_end - avio_tell(s->pb);
  314. if (max_size <= 0)
  315. return AVERROR_EOF;
  316. /* Now for that packet */
  317. switch (st->codec->codec_id) {
  318. case AV_CODEC_ID_ADPCM_IMA_QT:
  319. case AV_CODEC_ID_GSM:
  320. case AV_CODEC_ID_QDM2:
  321. case AV_CODEC_ID_QCELP:
  322. size = st->codec->block_align;
  323. break;
  324. default:
  325. size = (MAX_SIZE / st->codec->block_align) * st->codec->block_align;
  326. }
  327. size = FFMIN(max_size, size);
  328. res = av_get_packet(s->pb, pkt, size);
  329. if (res < 0)
  330. return res;
  331. if (size >= st->codec->block_align)
  332. pkt->flags &= ~AV_PKT_FLAG_CORRUPT;
  333. /* Only one stream in an AIFF file */
  334. pkt->stream_index = 0;
  335. pkt->duration = (res / st->codec->block_align) * aiff->block_duration;
  336. return 0;
  337. }
  338. AVInputFormat ff_aiff_demuxer = {
  339. .name = "aiff",
  340. .long_name = NULL_IF_CONFIG_SMALL("Audio IFF"),
  341. .priv_data_size = sizeof(AIFFInputContext),
  342. .read_probe = aiff_probe,
  343. .read_header = aiff_read_header,
  344. .read_packet = aiff_read_packet,
  345. .read_seek = ff_pcm_read_seek,
  346. .codec_tag = (const AVCodecTag* const []){ ff_codec_aiff_tags, 0 },
  347. };