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.

404 lines
13KB

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