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.

465 lines
13KB

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