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.

445 lines
12KB

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