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.

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