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.

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