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/dict.h"
  23. #include "avformat.h"
  24. #include "internal.h"
  25. #include "pcm.h"
  26. #include "aiff.h"
  27. #include "id3v2.h"
  28. #include "mov_chan.h"
  29. #include "replaygain.h"
  30. #define AIFF 0
  31. #define AIFF_C_VERSION1 0xA2805140
  32. typedef struct AIFFInputContext {
  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 (avio_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 -= res;
  72. str[res] = 0;
  73. av_dict_set(&s->metadata, key, str, AV_DICT_DONT_STRDUP_VAL);
  74. }
  75. avio_skip(s->pb, size);
  76. }
  77. /* Returns the number of sound data frames or negative on error */
  78. static int get_aiff_header(AVFormatContext *s, int size,
  79. unsigned version)
  80. {
  81. AVIOContext *pb = s->pb;
  82. AVCodecParameters *par = s->streams[0]->codecpar;
  83. AIFFInputContext *aiff = s->priv_data;
  84. int exp;
  85. uint64_t val;
  86. int sample_rate;
  87. unsigned int num_frames;
  88. if (size & 1)
  89. size++;
  90. par->codec_type = AVMEDIA_TYPE_AUDIO;
  91. par->channels = avio_rb16(pb);
  92. num_frames = avio_rb32(pb);
  93. par->bits_per_coded_sample = avio_rb16(pb);
  94. exp = avio_rb16(pb) - 16383 - 63;
  95. val = avio_rb64(pb);
  96. if (exp <-63 || exp >63) {
  97. av_log(s, AV_LOG_ERROR, "exp %d is out of range\n", exp);
  98. return AVERROR_INVALIDDATA;
  99. }
  100. if (exp >= 0)
  101. sample_rate = val << exp;
  102. else
  103. sample_rate = (val + (1ULL<<(-exp-1))) >> -exp;
  104. par->sample_rate = sample_rate;
  105. if (size < 18)
  106. return AVERROR_INVALIDDATA;
  107. size -= 18;
  108. /* get codec id for AIFF-C */
  109. if (size < 4) {
  110. version = AIFF;
  111. } else if (version == AIFF_C_VERSION1) {
  112. par->codec_tag = avio_rl32(pb);
  113. par->codec_id = ff_codec_get_id(ff_codec_aiff_tags, par->codec_tag);
  114. if (par->codec_id == AV_CODEC_ID_NONE)
  115. avpriv_request_sample(s, "unknown or unsupported codec tag: %s",
  116. av_fourcc2str(par->codec_tag));
  117. size -= 4;
  118. }
  119. if (version != AIFF_C_VERSION1 || par->codec_id == AV_CODEC_ID_PCM_S16BE) {
  120. par->codec_id = aiff_codec_get_id(par->bits_per_coded_sample);
  121. par->bits_per_coded_sample = av_get_bits_per_sample(par->codec_id);
  122. aiff->block_duration = 1;
  123. } else {
  124. switch (par->codec_id) {
  125. case AV_CODEC_ID_PCM_F32BE:
  126. case AV_CODEC_ID_PCM_F64BE:
  127. case AV_CODEC_ID_PCM_S16LE:
  128. case AV_CODEC_ID_PCM_ALAW:
  129. case AV_CODEC_ID_PCM_MULAW:
  130. aiff->block_duration = 1;
  131. break;
  132. case AV_CODEC_ID_ADPCM_IMA_QT:
  133. par->block_align = 34 * par->channels;
  134. break;
  135. case AV_CODEC_ID_MACE3:
  136. par->block_align = 2 * par->channels;
  137. break;
  138. case AV_CODEC_ID_ADPCM_G726LE:
  139. par->bits_per_coded_sample = 5;
  140. case AV_CODEC_ID_ADPCM_IMA_WS:
  141. case AV_CODEC_ID_ADPCM_G722:
  142. case AV_CODEC_ID_MACE6:
  143. case AV_CODEC_ID_SDX2_DPCM:
  144. par->block_align = 1 * par->channels;
  145. break;
  146. case AV_CODEC_ID_GSM:
  147. par->block_align = 33;
  148. break;
  149. default:
  150. aiff->block_duration = 1;
  151. break;
  152. }
  153. if (par->block_align > 0)
  154. aiff->block_duration = av_get_audio_frame_duration2(par,
  155. par->block_align);
  156. }
  157. /* Block align needs to be computed in all cases, as the definition
  158. * is specific to applications -> here we use the WAVE format definition */
  159. if (!par->block_align)
  160. par->block_align = (av_get_bits_per_sample(par->codec_id) * par->channels) >> 3;
  161. if (aiff->block_duration) {
  162. par->bit_rate = (int64_t)par->sample_rate * (par->block_align << 3) /
  163. aiff->block_duration;
  164. }
  165. /* Chunk is over */
  166. if (size)
  167. avio_skip(pb, size);
  168. return num_frames;
  169. }
  170. static int aiff_probe(const AVProbeData *p)
  171. {
  172. /* check file header */
  173. if (p->buf[0] == 'F' && p->buf[1] == 'O' &&
  174. p->buf[2] == 'R' && p->buf[3] == 'M' &&
  175. p->buf[8] == 'A' && p->buf[9] == 'I' &&
  176. p->buf[10] == 'F' && (p->buf[11] == 'F' || p->buf[11] == 'C'))
  177. return AVPROBE_SCORE_MAX;
  178. else
  179. return 0;
  180. }
  181. /* aiff input */
  182. static int aiff_read_header(AVFormatContext *s)
  183. {
  184. int ret, size, filesize;
  185. int64_t offset = 0, position;
  186. uint32_t tag;
  187. unsigned version = AIFF_C_VERSION1;
  188. AVIOContext *pb = s->pb;
  189. AVStream * st;
  190. AIFFInputContext *aiff = s->priv_data;
  191. ID3v2ExtraMeta *id3v2_extra_meta = NULL;
  192. /* check FORM header */
  193. filesize = get_tag(pb, &tag);
  194. if (filesize < 0 || tag != MKTAG('F', 'O', 'R', 'M'))
  195. return AVERROR_INVALIDDATA;
  196. /* AIFF data type */
  197. tag = avio_rl32(pb);
  198. if (tag == MKTAG('A', 'I', 'F', 'F')) /* Got an AIFF file */
  199. version = AIFF;
  200. else if (tag != MKTAG('A', 'I', 'F', 'C')) /* An AIFF-C file then */
  201. return AVERROR_INVALIDDATA;
  202. filesize -= 4;
  203. st = avformat_new_stream(s, NULL);
  204. if (!st)
  205. return AVERROR(ENOMEM);
  206. while (filesize > 0) {
  207. /* parse different chunks */
  208. size = get_tag(pb, &tag);
  209. if (size == AVERROR_EOF && offset > 0 && st->codecpar->block_align) {
  210. av_log(s, AV_LOG_WARNING, "header parser hit EOF\n");
  211. goto got_sound;
  212. }
  213. if (size < 0)
  214. return size;
  215. if (size >= 0x7fffffff - 8)
  216. filesize = 0;
  217. else
  218. filesize -= size + 8;
  219. switch (tag) {
  220. case MKTAG('C', 'O', 'M', 'M'): /* Common chunk */
  221. /* Then for the complete header info */
  222. st->nb_frames = get_aiff_header(s, size, version);
  223. if (st->nb_frames < 0)
  224. return st->nb_frames;
  225. if (offset > 0) // COMM is after SSND
  226. goto got_sound;
  227. break;
  228. case MKTAG('I', 'D', '3', ' '):
  229. position = avio_tell(pb);
  230. ff_id3v2_read(s, ID3v2_DEFAULT_MAGIC, &id3v2_extra_meta, size);
  231. if (id3v2_extra_meta)
  232. if ((ret = ff_id3v2_parse_apic(s, id3v2_extra_meta)) < 0 ||
  233. (ret = ff_id3v2_parse_chapters(s, id3v2_extra_meta)) < 0) {
  234. ff_id3v2_free_extra_meta(&id3v2_extra_meta);
  235. return ret;
  236. }
  237. ff_id3v2_free_extra_meta(&id3v2_extra_meta);
  238. if (position + size > avio_tell(pb))
  239. avio_skip(pb, position + size - avio_tell(pb));
  240. break;
  241. case MKTAG('F', 'V', 'E', 'R'): /* Version chunk */
  242. version = avio_rb32(pb);
  243. break;
  244. case MKTAG('N', 'A', 'M', 'E'): /* Sample name chunk */
  245. get_meta(s, "title" , size);
  246. break;
  247. case MKTAG('A', 'U', 'T', 'H'): /* Author chunk */
  248. get_meta(s, "author" , size);
  249. break;
  250. case MKTAG('(', 'c', ')', ' '): /* Copyright chunk */
  251. get_meta(s, "copyright", size);
  252. break;
  253. case MKTAG('A', 'N', 'N', 'O'): /* Annotation chunk */
  254. get_meta(s, "comment" , size);
  255. break;
  256. case MKTAG('S', 'S', 'N', 'D'): /* Sampled sound chunk */
  257. if (size < 8)
  258. return AVERROR_INVALIDDATA;
  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 = ff_aiff_codec_tags_list,
  390. };