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.

447 lines
12KB

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