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.

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