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.

433 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. 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. aiff->data_end = avio_tell(pb) + size;
  258. offset = avio_rb32(pb); /* Offset of sound data */
  259. avio_rb32(pb); /* BlockSize... don't care */
  260. offset += avio_tell(pb); /* Compute absolute data offset */
  261. if (st->codecpar->block_align && !(pb->seekable & AVIO_SEEKABLE_NORMAL)) /* Assume COMM already parsed */
  262. goto got_sound;
  263. if (!(pb->seekable & AVIO_SEEKABLE_NORMAL)) {
  264. av_log(s, AV_LOG_ERROR, "file is not seekable\n");
  265. return -1;
  266. }
  267. avio_skip(pb, size - 8);
  268. break;
  269. case MKTAG('w', 'a', 'v', 'e'):
  270. if ((uint64_t)size > (1<<30))
  271. return -1;
  272. if ((ret = ff_get_extradata(s, st->codecpar, pb, size)) < 0)
  273. return ret;
  274. if ( (st->codecpar->codec_id == AV_CODEC_ID_QDMC || st->codecpar->codec_id == AV_CODEC_ID_QDM2)
  275. && size>=12*4 && !st->codecpar->block_align) {
  276. st->codecpar->block_align = AV_RB32(st->codecpar->extradata+11*4);
  277. aiff->block_duration = AV_RB32(st->codecpar->extradata+9*4);
  278. } else if (st->codecpar->codec_id == AV_CODEC_ID_QCELP) {
  279. char rate = 0;
  280. if (size >= 25)
  281. rate = st->codecpar->extradata[24];
  282. switch (rate) {
  283. case 'H': // RATE_HALF
  284. st->codecpar->block_align = 17;
  285. break;
  286. case 'F': // RATE_FULL
  287. default:
  288. st->codecpar->block_align = 35;
  289. }
  290. aiff->block_duration = 160;
  291. st->codecpar->bit_rate = (int64_t)st->codecpar->sample_rate * (st->codecpar->block_align << 3) /
  292. aiff->block_duration;
  293. }
  294. break;
  295. case MKTAG('C','H','A','N'):
  296. if ((ret = ff_mov_read_chan(s, pb, st, size)) < 0)
  297. return ret;
  298. break;
  299. case MKTAG('A','P','C','M'): /* XA ADPCM compressed sound chunk */
  300. st->codecpar->codec_id = AV_CODEC_ID_ADPCM_XA;
  301. aiff->data_end = avio_tell(pb) + size;
  302. offset = avio_tell(pb) + 8;
  303. /* This field is unknown and its data seems to be irrelevant */
  304. avio_rb32(pb);
  305. st->codecpar->block_align = avio_rb32(pb);
  306. goto got_sound;
  307. break;
  308. case 0:
  309. if (offset > 0 && st->codecpar->block_align) // COMM && SSND
  310. goto got_sound;
  311. default: /* Jump */
  312. avio_skip(pb, size);
  313. }
  314. /* Skip required padding byte for odd-sized chunks. */
  315. if (size & 1) {
  316. filesize--;
  317. avio_skip(pb, 1);
  318. }
  319. }
  320. ret = ff_replaygain_export(st, s->metadata);
  321. if (ret < 0)
  322. return ret;
  323. got_sound:
  324. if (!st->codecpar->block_align && st->codecpar->codec_id == AV_CODEC_ID_QCELP) {
  325. av_log(s, AV_LOG_WARNING, "qcelp without wave chunk, assuming full rate\n");
  326. st->codecpar->block_align = 35;
  327. } else if (!st->codecpar->block_align) {
  328. av_log(s, AV_LOG_ERROR, "could not find COMM tag or invalid block_align value\n");
  329. return -1;
  330. }
  331. /* Now positioned, get the sound data start and end */
  332. avpriv_set_pts_info(st, 64, 1, st->codecpar->sample_rate);
  333. st->start_time = 0;
  334. st->duration = st->nb_frames * aiff->block_duration;
  335. /* Position the stream at the first block */
  336. avio_seek(pb, offset, SEEK_SET);
  337. return 0;
  338. }
  339. #define MAX_SIZE 4096
  340. static int aiff_read_packet(AVFormatContext *s,
  341. AVPacket *pkt)
  342. {
  343. AVStream *st = s->streams[0];
  344. AIFFInputContext *aiff = s->priv_data;
  345. int64_t max_size;
  346. int res, size;
  347. /* calculate size of remaining data */
  348. max_size = aiff->data_end - avio_tell(s->pb);
  349. if (max_size <= 0)
  350. return AVERROR_EOF;
  351. if (!st->codecpar->block_align) {
  352. av_log(s, AV_LOG_ERROR, "block_align not set\n");
  353. return AVERROR_INVALIDDATA;
  354. }
  355. /* Now for that packet */
  356. switch (st->codecpar->codec_id) {
  357. case AV_CODEC_ID_ADPCM_IMA_QT:
  358. case AV_CODEC_ID_GSM:
  359. case AV_CODEC_ID_QDM2:
  360. case AV_CODEC_ID_QCELP:
  361. size = st->codecpar->block_align;
  362. break;
  363. default:
  364. size = st->codecpar->block_align ? (MAX_SIZE / st->codecpar->block_align) * st->codecpar->block_align : MAX_SIZE;
  365. }
  366. size = FFMIN(max_size, size);
  367. res = av_get_packet(s->pb, pkt, size);
  368. if (res < 0)
  369. return res;
  370. if (size >= st->codecpar->block_align)
  371. pkt->flags &= ~AV_PKT_FLAG_CORRUPT;
  372. /* Only one stream in an AIFF file */
  373. pkt->stream_index = 0;
  374. pkt->duration = (res / st->codecpar->block_align) * aiff->block_duration;
  375. return 0;
  376. }
  377. AVInputFormat ff_aiff_demuxer = {
  378. .name = "aiff",
  379. .long_name = NULL_IF_CONFIG_SMALL("Audio IFF"),
  380. .priv_data_size = sizeof(AIFFInputContext),
  381. .read_probe = aiff_probe,
  382. .read_header = aiff_read_header,
  383. .read_packet = aiff_read_packet,
  384. .read_seek = ff_pcm_read_seek,
  385. .codec_tag = (const AVCodecTag* const []){ ff_codec_aiff_tags, 0 },
  386. };