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.

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