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.

382 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 < 0)
  198. return size;
  199. filesize -= size + 8;
  200. switch (tag) {
  201. case MKTAG('C', 'O', 'M', 'M'): /* Common chunk */
  202. /* Then for the complete header info */
  203. st->nb_frames = get_aiff_header(s, size, version);
  204. if (st->nb_frames < 0)
  205. return st->nb_frames;
  206. if (offset > 0) // COMM is after SSND
  207. goto got_sound;
  208. break;
  209. case MKTAG('I', 'D', '3', ' '):
  210. position = avio_tell(pb);
  211. ff_id3v2_read(s, ID3v2_DEFAULT_MAGIC, &id3v2_extra_meta, size);
  212. if (id3v2_extra_meta)
  213. if ((ret = ff_id3v2_parse_apic(s, &id3v2_extra_meta)) < 0) {
  214. ff_id3v2_free_extra_meta(&id3v2_extra_meta);
  215. return ret;
  216. }
  217. ff_id3v2_free_extra_meta(&id3v2_extra_meta);
  218. if (position + size > avio_tell(pb))
  219. avio_skip(pb, position + size - avio_tell(pb));
  220. break;
  221. case MKTAG('F', 'V', 'E', 'R'): /* Version chunk */
  222. version = avio_rb32(pb);
  223. break;
  224. case MKTAG('N', 'A', 'M', 'E'): /* Sample name chunk */
  225. get_meta(s, "title" , size);
  226. break;
  227. case MKTAG('A', 'U', 'T', 'H'): /* Author chunk */
  228. get_meta(s, "author" , size);
  229. break;
  230. case MKTAG('(', 'c', ')', ' '): /* Copyright chunk */
  231. get_meta(s, "copyright", size);
  232. break;
  233. case MKTAG('A', 'N', 'N', 'O'): /* Annotation chunk */
  234. get_meta(s, "comment" , size);
  235. break;
  236. case MKTAG('S', 'S', 'N', 'D'): /* Sampled sound chunk */
  237. aiff->data_end = avio_tell(pb) + size;
  238. offset = avio_rb32(pb); /* Offset of sound data */
  239. avio_rb32(pb); /* BlockSize... don't care */
  240. offset += avio_tell(pb); /* Compute absolute data offset */
  241. if (st->codec->block_align && !pb->seekable) /* Assume COMM already parsed */
  242. goto got_sound;
  243. if (!pb->seekable) {
  244. av_log(s, AV_LOG_ERROR, "file is not seekable\n");
  245. return -1;
  246. }
  247. avio_skip(pb, size - 8);
  248. break;
  249. case MKTAG('w', 'a', 'v', 'e'):
  250. if ((uint64_t)size > (1<<30))
  251. return -1;
  252. if (ff_get_extradata(st->codec, pb, size) < 0)
  253. return AVERROR(ENOMEM);
  254. if (st->codec->codec_id == AV_CODEC_ID_QDM2 && size>=12*4 && !st->codec->block_align) {
  255. st->codec->block_align = AV_RB32(st->codec->extradata+11*4);
  256. aiff->block_duration = AV_RB32(st->codec->extradata+9*4);
  257. } else if (st->codec->codec_id == AV_CODEC_ID_QCELP) {
  258. char rate = 0;
  259. if (size >= 25)
  260. rate = st->codec->extradata[24];
  261. switch (rate) {
  262. case 'H': // RATE_HALF
  263. st->codec->block_align = 17;
  264. break;
  265. case 'F': // RATE_FULL
  266. default:
  267. st->codec->block_align = 35;
  268. }
  269. aiff->block_duration = 160;
  270. st->codec->bit_rate = st->codec->sample_rate * (st->codec->block_align << 3) /
  271. aiff->block_duration;
  272. }
  273. break;
  274. case MKTAG('C','H','A','N'):
  275. if(ff_mov_read_chan(s, pb, st, size) < 0)
  276. return AVERROR_INVALIDDATA;
  277. break;
  278. default: /* Jump */
  279. if (size & 1) /* Always even aligned */
  280. size++;
  281. avio_skip(pb, size);
  282. }
  283. }
  284. got_sound:
  285. if (!st->codec->block_align) {
  286. av_log(s, AV_LOG_ERROR, "could not find COMM tag or invalid block_align value\n");
  287. return -1;
  288. }
  289. /* Now positioned, get the sound data start and end */
  290. avpriv_set_pts_info(st, 64, 1, st->codec->sample_rate);
  291. st->start_time = 0;
  292. st->duration = st->nb_frames * aiff->block_duration;
  293. /* Position the stream at the first block */
  294. avio_seek(pb, offset, SEEK_SET);
  295. return 0;
  296. }
  297. #define MAX_SIZE 4096
  298. static int aiff_read_packet(AVFormatContext *s,
  299. AVPacket *pkt)
  300. {
  301. AVStream *st = s->streams[0];
  302. AIFFInputContext *aiff = s->priv_data;
  303. int64_t max_size;
  304. int res, size;
  305. /* calculate size of remaining data */
  306. max_size = aiff->data_end - avio_tell(s->pb);
  307. if (max_size <= 0)
  308. return AVERROR_EOF;
  309. /* Now for that packet */
  310. switch (st->codec->codec_id) {
  311. case AV_CODEC_ID_ADPCM_IMA_QT:
  312. case AV_CODEC_ID_GSM:
  313. case AV_CODEC_ID_QDM2:
  314. case AV_CODEC_ID_QCELP:
  315. size = st->codec->block_align;
  316. break;
  317. default:
  318. size = (MAX_SIZE / st->codec->block_align) * st->codec->block_align;
  319. }
  320. size = FFMIN(max_size, size);
  321. res = av_get_packet(s->pb, pkt, size);
  322. if (res < 0)
  323. return res;
  324. if (size >= st->codec->block_align)
  325. pkt->flags &= ~AV_PKT_FLAG_CORRUPT;
  326. /* Only one stream in an AIFF file */
  327. pkt->stream_index = 0;
  328. pkt->duration = (res / st->codec->block_align) * aiff->block_duration;
  329. return 0;
  330. }
  331. AVInputFormat ff_aiff_demuxer = {
  332. .name = "aiff",
  333. .long_name = NULL_IF_CONFIG_SMALL("Audio IFF"),
  334. .priv_data_size = sizeof(AIFFInputContext),
  335. .read_probe = aiff_probe,
  336. .read_header = aiff_read_header,
  337. .read_packet = aiff_read_packet,
  338. .read_seek = ff_pcm_read_seek,
  339. .codec_tag = (const AVCodecTag* const []){ ff_codec_aiff_tags, 0 },
  340. };