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.

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