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.

478 lines
14KB

  1. /*
  2. * AIFF/AIFF-C muxer and 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/intfloat_readwrite.h"
  22. #include "avformat.h"
  23. #include "raw.h"
  24. #include "riff.h"
  25. static const AVCodecTag codec_aiff_tags[] = {
  26. { CODEC_ID_PCM_S16BE, MKTAG('N','O','N','E') },
  27. { CODEC_ID_PCM_S8, MKTAG('N','O','N','E') },
  28. { CODEC_ID_PCM_S24BE, MKTAG('N','O','N','E') },
  29. { CODEC_ID_PCM_S32BE, MKTAG('N','O','N','E') },
  30. { CODEC_ID_PCM_F32BE, MKTAG('f','l','3','2') },
  31. { CODEC_ID_PCM_F64BE, MKTAG('f','l','6','4') },
  32. { CODEC_ID_PCM_ALAW, MKTAG('a','l','a','w') },
  33. { CODEC_ID_PCM_MULAW, MKTAG('u','l','a','w') },
  34. { CODEC_ID_MACE3, MKTAG('M','A','C','3') },
  35. { CODEC_ID_MACE6, MKTAG('M','A','C','6') },
  36. { CODEC_ID_GSM, MKTAG('G','S','M',' ') },
  37. { CODEC_ID_ADPCM_G726, MKTAG('G','7','2','6') },
  38. { CODEC_ID_PCM_S16LE, MKTAG('s','o','w','t') },
  39. { CODEC_ID_ADPCM_IMA_QT, MKTAG('i','m','a','4') },
  40. { CODEC_ID_QDM2, MKTAG('Q','D','M','2') },
  41. { 0, 0 },
  42. };
  43. #define AIFF 0
  44. #define AIFF_C_VERSION1 0xA2805140
  45. typedef struct {
  46. int64_t data_end;
  47. } AIFFInputContext;
  48. static enum CodecID aiff_codec_get_id(int bps)
  49. {
  50. if (bps <= 8)
  51. return CODEC_ID_PCM_S8;
  52. if (bps <= 16)
  53. return CODEC_ID_PCM_S16BE;
  54. if (bps <= 24)
  55. return CODEC_ID_PCM_S24BE;
  56. if (bps <= 32)
  57. return CODEC_ID_PCM_S32BE;
  58. /* bigger than 32 isn't allowed */
  59. return CODEC_ID_NONE;
  60. }
  61. /* returns the size of the found tag */
  62. static int get_tag(ByteIOContext *pb, uint32_t * tag)
  63. {
  64. int size;
  65. if (url_feof(pb))
  66. return AVERROR(EIO);
  67. *tag = get_le32(pb);
  68. size = get_be32(pb);
  69. if (size < 0)
  70. size = 0x7fffffff;
  71. return size;
  72. }
  73. /* Metadata string read */
  74. static void get_meta(AVFormatContext *s, const char *key, int size)
  75. {
  76. uint8_t str[1024];
  77. int res = get_buffer(s->pb, str, FFMIN(sizeof(str)-1, size));
  78. if (res < 0)
  79. return;
  80. str[res] = 0;
  81. if (size & 1)
  82. size++;
  83. size -= res;
  84. if (size)
  85. url_fskip(s->pb, size);
  86. av_metadata_set(&s->metadata, key, str);
  87. }
  88. /* Returns the number of sound data frames or negative on error */
  89. static unsigned int get_aiff_header(ByteIOContext *pb, AVCodecContext *codec,
  90. int size, unsigned version)
  91. {
  92. AVExtFloat ext;
  93. double sample_rate;
  94. unsigned int num_frames;
  95. if (size & 1)
  96. size++;
  97. codec->codec_type = CODEC_TYPE_AUDIO;
  98. codec->channels = get_be16(pb);
  99. num_frames = get_be32(pb);
  100. codec->bits_per_coded_sample = get_be16(pb);
  101. get_buffer(pb, (uint8_t*)&ext, sizeof(ext));/* Sample rate is in */
  102. sample_rate = av_ext2dbl(ext); /* 80 bits BE IEEE extended float */
  103. codec->sample_rate = sample_rate;
  104. size -= 18;
  105. /* Got an AIFF-C? */
  106. if (version == AIFF_C_VERSION1) {
  107. codec->codec_tag = get_le32(pb);
  108. codec->codec_id = ff_codec_get_id(codec_aiff_tags, codec->codec_tag);
  109. switch (codec->codec_id) {
  110. case CODEC_ID_PCM_S16BE:
  111. codec->codec_id = aiff_codec_get_id(codec->bits_per_coded_sample);
  112. codec->bits_per_coded_sample = av_get_bits_per_sample(codec->codec_id);
  113. break;
  114. case CODEC_ID_ADPCM_IMA_QT:
  115. codec->block_align = 34*codec->channels;
  116. codec->frame_size = 64;
  117. break;
  118. case CODEC_ID_MACE3:
  119. codec->block_align = 2*codec->channels;
  120. codec->frame_size = 6;
  121. break;
  122. case CODEC_ID_MACE6:
  123. codec->block_align = 1*codec->channels;
  124. codec->frame_size = 6;
  125. break;
  126. case CODEC_ID_GSM:
  127. codec->block_align = 33;
  128. codec->frame_size = 160;
  129. break;
  130. default:
  131. break;
  132. }
  133. size -= 4;
  134. } else {
  135. /* Need the codec type */
  136. codec->codec_id = aiff_codec_get_id(codec->bits_per_coded_sample);
  137. codec->bits_per_coded_sample = av_get_bits_per_sample(codec->codec_id);
  138. }
  139. /* Block align needs to be computed in all cases, as the definition
  140. * is specific to applications -> here we use the WAVE format definition */
  141. if (!codec->block_align)
  142. codec->block_align = (codec->bits_per_coded_sample * codec->channels) >> 3;
  143. codec->bit_rate = (codec->frame_size ? codec->sample_rate/codec->frame_size :
  144. codec->sample_rate) * (codec->block_align << 3);
  145. /* Chunk is over */
  146. if (size)
  147. url_fseek(pb, size, SEEK_CUR);
  148. return num_frames;
  149. }
  150. #if CONFIG_AIFF_MUXER
  151. typedef struct {
  152. int64_t form;
  153. int64_t frames;
  154. int64_t ssnd;
  155. } AIFFOutputContext;
  156. static int aiff_write_header(AVFormatContext *s)
  157. {
  158. AIFFOutputContext *aiff = s->priv_data;
  159. ByteIOContext *pb = s->pb;
  160. AVCodecContext *enc = s->streams[0]->codec;
  161. AVExtFloat sample_rate;
  162. int aifc = 0;
  163. /* First verify if format is ok */
  164. if (!enc->codec_tag)
  165. return -1;
  166. if (enc->codec_tag != MKTAG('N','O','N','E'))
  167. aifc = 1;
  168. /* FORM AIFF header */
  169. put_tag(pb, "FORM");
  170. aiff->form = url_ftell(pb);
  171. put_be32(pb, 0); /* file length */
  172. put_tag(pb, aifc ? "AIFC" : "AIFF");
  173. if (aifc) { // compressed audio
  174. enc->bits_per_coded_sample = 16;
  175. if (!enc->block_align) {
  176. av_log(s, AV_LOG_ERROR, "block align not set\n");
  177. return -1;
  178. }
  179. /* Version chunk */
  180. put_tag(pb, "FVER");
  181. put_be32(pb, 4);
  182. put_be32(pb, 0xA2805140);
  183. }
  184. /* Common chunk */
  185. put_tag(pb, "COMM");
  186. put_be32(pb, aifc ? 24 : 18); /* size */
  187. put_be16(pb, enc->channels); /* Number of channels */
  188. aiff->frames = url_ftell(pb);
  189. put_be32(pb, 0); /* Number of frames */
  190. if (!enc->bits_per_coded_sample)
  191. enc->bits_per_coded_sample = av_get_bits_per_sample(enc->codec_id);
  192. if (!enc->bits_per_coded_sample) {
  193. av_log(s, AV_LOG_ERROR, "could not compute bits per sample\n");
  194. return -1;
  195. }
  196. if (!enc->block_align)
  197. enc->block_align = (enc->bits_per_coded_sample * enc->channels) >> 3;
  198. put_be16(pb, enc->bits_per_coded_sample); /* Sample size */
  199. sample_rate = av_dbl2ext((double)enc->sample_rate);
  200. put_buffer(pb, (uint8_t*)&sample_rate, sizeof(sample_rate));
  201. if (aifc) {
  202. put_le32(pb, enc->codec_tag);
  203. put_be16(pb, 0);
  204. }
  205. /* Sound data chunk */
  206. put_tag(pb, "SSND");
  207. aiff->ssnd = url_ftell(pb); /* Sound chunk size */
  208. put_be32(pb, 0); /* Sound samples data size */
  209. put_be32(pb, 0); /* Data offset */
  210. put_be32(pb, 0); /* Block-size (block align) */
  211. av_set_pts_info(s->streams[0], 64, 1, s->streams[0]->codec->sample_rate);
  212. /* Data is starting here */
  213. put_flush_packet(pb);
  214. return 0;
  215. }
  216. static int aiff_write_packet(AVFormatContext *s, AVPacket *pkt)
  217. {
  218. ByteIOContext *pb = s->pb;
  219. put_buffer(pb, pkt->data, pkt->size);
  220. return 0;
  221. }
  222. static int aiff_write_trailer(AVFormatContext *s)
  223. {
  224. ByteIOContext *pb = s->pb;
  225. AIFFOutputContext *aiff = s->priv_data;
  226. AVCodecContext *enc = s->streams[0]->codec;
  227. /* Chunks sizes must be even */
  228. int64_t file_size, end_size;
  229. end_size = file_size = url_ftell(pb);
  230. if (file_size & 1) {
  231. put_byte(pb, 0);
  232. end_size++;
  233. }
  234. if (!url_is_streamed(s->pb)) {
  235. /* File length */
  236. url_fseek(pb, aiff->form, SEEK_SET);
  237. put_be32(pb, file_size - aiff->form - 4);
  238. /* Number of sample frames */
  239. url_fseek(pb, aiff->frames, SEEK_SET);
  240. put_be32(pb, (file_size-aiff->ssnd-12)/enc->block_align);
  241. /* Sound Data chunk size */
  242. url_fseek(pb, aiff->ssnd, SEEK_SET);
  243. put_be32(pb, file_size - aiff->ssnd - 4);
  244. /* return to the end */
  245. url_fseek(pb, end_size, SEEK_SET);
  246. put_flush_packet(pb);
  247. }
  248. return 0;
  249. }
  250. #endif /* CONFIG_AIFF_MUXER */
  251. static int aiff_probe(AVProbeData *p)
  252. {
  253. /* check file header */
  254. if (p->buf[0] == 'F' && p->buf[1] == 'O' &&
  255. p->buf[2] == 'R' && p->buf[3] == 'M' &&
  256. p->buf[8] == 'A' && p->buf[9] == 'I' &&
  257. p->buf[10] == 'F' && (p->buf[11] == 'F' || p->buf[11] == 'C'))
  258. return AVPROBE_SCORE_MAX;
  259. else
  260. return 0;
  261. }
  262. /* aiff input */
  263. static int aiff_read_header(AVFormatContext *s,
  264. AVFormatParameters *ap)
  265. {
  266. int size, filesize;
  267. int64_t offset = 0;
  268. uint32_t tag;
  269. unsigned version = AIFF_C_VERSION1;
  270. ByteIOContext *pb = s->pb;
  271. AVStream * st;
  272. AIFFInputContext *aiff = s->priv_data;
  273. /* check FORM header */
  274. filesize = get_tag(pb, &tag);
  275. if (filesize < 0 || tag != MKTAG('F', 'O', 'R', 'M'))
  276. return AVERROR_INVALIDDATA;
  277. /* AIFF data type */
  278. tag = get_le32(pb);
  279. if (tag == MKTAG('A', 'I', 'F', 'F')) /* Got an AIFF file */
  280. version = AIFF;
  281. else if (tag != MKTAG('A', 'I', 'F', 'C')) /* An AIFF-C file then */
  282. return AVERROR_INVALIDDATA;
  283. filesize -= 4;
  284. st = av_new_stream(s, 0);
  285. if (!st)
  286. return AVERROR(ENOMEM);
  287. while (filesize > 0) {
  288. /* parse different chunks */
  289. size = get_tag(pb, &tag);
  290. if (size < 0)
  291. return size;
  292. filesize -= size + 8;
  293. switch (tag) {
  294. case MKTAG('C', 'O', 'M', 'M'): /* Common chunk */
  295. /* Then for the complete header info */
  296. st->nb_frames = get_aiff_header(pb, st->codec, size, version);
  297. if (st->nb_frames < 0)
  298. return st->nb_frames;
  299. if (offset > 0) // COMM is after SSND
  300. goto got_sound;
  301. break;
  302. case MKTAG('F', 'V', 'E', 'R'): /* Version chunk */
  303. version = get_be32(pb);
  304. break;
  305. case MKTAG('N', 'A', 'M', 'E'): /* Sample name chunk */
  306. get_meta(s, "title" , size);
  307. break;
  308. case MKTAG('A', 'U', 'T', 'H'): /* Author chunk */
  309. get_meta(s, "author" , size);
  310. break;
  311. case MKTAG('(', 'c', ')', ' '): /* Copyright chunk */
  312. get_meta(s, "copyright", size);
  313. break;
  314. case MKTAG('A', 'N', 'N', 'O'): /* Annotation chunk */
  315. get_meta(s, "comment" , size);
  316. break;
  317. case MKTAG('S', 'S', 'N', 'D'): /* Sampled sound chunk */
  318. aiff->data_end = url_ftell(pb) + size;
  319. offset = get_be32(pb); /* Offset of sound data */
  320. get_be32(pb); /* BlockSize... don't care */
  321. offset += url_ftell(pb); /* Compute absolute data offset */
  322. if (st->codec->block_align) /* Assume COMM already parsed */
  323. goto got_sound;
  324. if (url_is_streamed(pb)) {
  325. av_log(s, AV_LOG_ERROR, "file is not seekable\n");
  326. return -1;
  327. }
  328. url_fskip(pb, size - 8);
  329. break;
  330. case MKTAG('w', 'a', 'v', 'e'):
  331. if ((uint64_t)size > (1<<30))
  332. return -1;
  333. st->codec->extradata = av_mallocz(size + FF_INPUT_BUFFER_PADDING_SIZE);
  334. if (!st->codec->extradata)
  335. return AVERROR(ENOMEM);
  336. st->codec->extradata_size = size;
  337. get_buffer(pb, st->codec->extradata, size);
  338. break;
  339. default: /* Jump */
  340. if (size & 1) /* Always even aligned */
  341. size++;
  342. url_fskip (pb, size);
  343. }
  344. }
  345. if (!st->codec->block_align) {
  346. av_log(s, AV_LOG_ERROR, "could not find COMM tag\n");
  347. return -1;
  348. }
  349. got_sound:
  350. /* Now positioned, get the sound data start and end */
  351. if (st->nb_frames)
  352. s->file_size = st->nb_frames * st->codec->block_align;
  353. av_set_pts_info(st, 64, 1, st->codec->sample_rate);
  354. st->start_time = 0;
  355. st->duration = st->codec->frame_size ?
  356. st->nb_frames * st->codec->frame_size : st->nb_frames;
  357. /* Position the stream at the first block */
  358. url_fseek(pb, offset, SEEK_SET);
  359. return 0;
  360. }
  361. #define MAX_SIZE 4096
  362. static int aiff_read_packet(AVFormatContext *s,
  363. AVPacket *pkt)
  364. {
  365. AVStream *st = s->streams[0];
  366. AIFFInputContext *aiff = s->priv_data;
  367. int64_t max_size;
  368. int res;
  369. /* calculate size of remaining data */
  370. max_size = aiff->data_end - url_ftell(s->pb);
  371. if (max_size <= 0)
  372. return AVERROR_EOF;
  373. /* Now for that packet */
  374. max_size = FFMIN(max_size, (MAX_SIZE / st->codec->block_align) * st->codec->block_align);
  375. res = av_get_packet(s->pb, pkt, max_size);
  376. if (res < 0)
  377. return res;
  378. /* Only one stream in an AIFF file */
  379. pkt->stream_index = 0;
  380. return 0;
  381. }
  382. #if CONFIG_AIFF_DEMUXER
  383. AVInputFormat aiff_demuxer = {
  384. "aiff",
  385. NULL_IF_CONFIG_SMALL("Audio IFF"),
  386. sizeof(AIFFInputContext),
  387. aiff_probe,
  388. aiff_read_header,
  389. aiff_read_packet,
  390. NULL,
  391. pcm_read_seek,
  392. .codec_tag= (const AVCodecTag* const []){codec_aiff_tags, 0},
  393. };
  394. #endif
  395. #if CONFIG_AIFF_MUXER
  396. AVOutputFormat aiff_muxer = {
  397. "aiff",
  398. NULL_IF_CONFIG_SMALL("Audio IFF"),
  399. "audio/aiff",
  400. "aif,aiff,afc,aifc",
  401. sizeof(AIFFOutputContext),
  402. CODEC_ID_PCM_S16BE,
  403. CODEC_ID_NONE,
  404. aiff_write_header,
  405. aiff_write_packet,
  406. aiff_write_trailer,
  407. .codec_tag= (const AVCodecTag* const []){codec_aiff_tags, 0},
  408. };
  409. #endif