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.

437 lines
14KB

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