aiff.c 14 KB

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