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.

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