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.

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