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.

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