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.

458 lines
12KB

  1. /*
  2. * AIFF/AIFF-C encoder and decoder
  3. * Copyright (c) 2006 Patrick Guimond
  4. *
  5. * This library is free software; you can redistribute it and/or
  6. * modify it under the terms of the GNU Lesser General Public
  7. * License as published by the Free Software Foundation; either
  8. * version 2 of the License, or (at your option) any later version.
  9. *
  10. * This library is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  13. * Lesser General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU Lesser General Public
  16. * License along with this library; if not, write to the Free Software
  17. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  18. */
  19. #include "avformat.h"
  20. #include "allformats.h"
  21. #include "riff.h"
  22. #include "intfloat_readwrite.h"
  23. const CodecTag codec_aiff_tags[] = {
  24. { CODEC_ID_PCM_S16BE, MKTAG('N','O','N','E') },
  25. { CODEC_ID_PCM_S8, MKTAG('N','O','N','E') },
  26. { CODEC_ID_PCM_S24BE, MKTAG('N','O','N','E') },
  27. { CODEC_ID_PCM_S32BE, MKTAG('N','O','N','E') },
  28. { CODEC_ID_PCM_ALAW, MKTAG('a','l','a','w') },
  29. { CODEC_ID_PCM_ALAW, MKTAG('A','L','A','W') },
  30. { CODEC_ID_PCM_MULAW, MKTAG('u','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. { 0, 0 },
  37. };
  38. #define AIFF 0
  39. #define AIFF_C_VERSION1 0xA2805140
  40. static int aiff_codec_get_id (int bps)
  41. {
  42. if (bps <= 8)
  43. return CODEC_ID_PCM_S8;
  44. if (bps <= 16)
  45. return CODEC_ID_PCM_S16BE;
  46. if (bps <= 24)
  47. return CODEC_ID_PCM_S24BE;
  48. if (bps <= 32)
  49. return CODEC_ID_PCM_S32BE;
  50. /* bigger than 32 isn't allowed */
  51. return 0;
  52. }
  53. /* returns the size of the found tag */
  54. static int get_tag(ByteIOContext *pb, uint32_t * tag)
  55. {
  56. int size;
  57. if (url_feof(pb))
  58. return AVERROR_IO;
  59. *tag = get_le32(pb);
  60. size = get_be32(pb);
  61. if (size < 0)
  62. size = 0x7fffffff;
  63. return size;
  64. }
  65. /* Metadata string read */
  66. static void get_meta(ByteIOContext *pb, char * str, int strsize, int size)
  67. {
  68. int res;
  69. if (size > strsize-1)
  70. res = get_buffer(pb, (uint8_t*)str, strsize-1);
  71. else
  72. res = get_buffer(pb, (uint8_t*)str, size);
  73. if (res < 0)
  74. return;
  75. str[res] = 0;
  76. if (size & 1)
  77. size++;
  78. size -= res;
  79. if (size);
  80. url_fskip(pb, size);
  81. }
  82. /* Returns the number of bits per second */
  83. static int fix_bps(int codec_id)
  84. {
  85. switch (codec_id) {
  86. case CODEC_ID_PCM_S8:
  87. return 8;
  88. case CODEC_ID_PCM_S16BE:
  89. return 16;
  90. case CODEC_ID_PCM_S24BE:
  91. return 24;
  92. case CODEC_ID_PCM_S32BE:
  93. return 32;
  94. }
  95. return -1;
  96. }
  97. /* Returns the number of sound data frames or negative on error */
  98. static unsigned int get_aiff_header(ByteIOContext *pb, AVCodecContext *codec,
  99. int size, unsigned version)
  100. {
  101. AVExtFloat ext;
  102. double sample_rate;
  103. unsigned int num_frames;
  104. if (size & 1)
  105. size++;
  106. codec->codec_type = CODEC_TYPE_AUDIO;
  107. codec->channels = get_be16(pb);
  108. num_frames = get_be32(pb);
  109. codec->bits_per_sample = get_be16(pb);
  110. get_buffer(pb, (uint8_t*)&ext, sizeof(ext));/* Sample rate is in */
  111. sample_rate = av_ext2dbl(ext); /* 80 bits BE IEEE extended float */
  112. codec->sample_rate = sample_rate;
  113. size -= 18;
  114. /* Got an AIFF-C? */
  115. if (version == AIFF_C_VERSION1) {
  116. codec->codec_tag = get_le32(pb);
  117. codec->codec_id = codec_get_id (codec_aiff_tags, codec->codec_tag);
  118. if (codec->codec_id == CODEC_ID_PCM_S16BE) {
  119. codec->codec_id = aiff_codec_get_id (codec->bits_per_sample);
  120. codec->bits_per_sample = fix_bps(codec->codec_id);
  121. }
  122. size -= 4;
  123. } else {
  124. /* Need the codec type */
  125. codec->codec_id = aiff_codec_get_id (codec->bits_per_sample);
  126. codec->bits_per_sample = fix_bps(codec->codec_id);
  127. }
  128. if (!codec->codec_id)
  129. return AVERROR_INVALIDDATA;
  130. /* Block align needs to be computed in all cases, as the definition
  131. * is specific to applications -> here we use the WAVE format definition */
  132. codec->block_align = (codec->bits_per_sample * codec->channels) >> 3;
  133. codec->bit_rate = codec->sample_rate * codec->block_align;
  134. /* Chunk is over */
  135. if (size)
  136. url_fseek(pb, size, SEEK_CUR);
  137. return num_frames;
  138. }
  139. #ifdef CONFIG_MUXERS
  140. typedef struct {
  141. offset_t form;
  142. offset_t frames;
  143. offset_t ssnd;
  144. } AIFFOutputContext;
  145. static int aiff_write_header(AVFormatContext *s)
  146. {
  147. AIFFOutputContext *aiff = s->priv_data;
  148. ByteIOContext *pb = &s->pb;
  149. AVCodecContext *enc = s->streams[0]->codec;
  150. AVExtFloat sample_rate;
  151. int coder_len;
  152. /* First verify if format is ok */
  153. enc->codec_tag = codec_get_tag(codec_aiff_tags, enc->codec_id);
  154. if (!enc->codec_tag) {
  155. av_free(aiff);
  156. return -1;
  157. }
  158. coder_len = strlen(enc->codec->name);
  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. if (coder_len & 1) /* Common chunk is of var size */
  171. put_be32(pb, 23+coder_len);
  172. else
  173. put_be32(pb, 24+coder_len);
  174. put_be16(pb, enc->channels); /* Number of channels */
  175. aiff->frames = url_ftell(pb);
  176. put_be32(pb, 0); /* Number of frames */
  177. if (!enc->bits_per_sample)
  178. enc->bits_per_sample = (enc->block_align<<3) / enc->channels;
  179. put_be16(pb, enc->bits_per_sample); /* Sample size */
  180. sample_rate = av_dbl2ext((double)enc->sample_rate);
  181. put_buffer(pb, (uint8_t*)&sample_rate, sizeof(sample_rate));
  182. put_le32(pb, enc->codec_tag);
  183. if (coder_len & 1) {
  184. put_byte(pb, coder_len);
  185. put_buffer(pb, (const uint8_t*)enc->codec->name, coder_len);
  186. } else {
  187. put_byte(pb, coder_len+1);
  188. put_buffer(pb, (const uint8_t*)enc->codec->name, coder_len);
  189. put_byte(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_size < 16)
  241. return 0;
  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, offset;
  255. uint32_t tag;
  256. unsigned version = AIFF_C_VERSION1;
  257. ByteIOContext *pb = &s->pb;
  258. AVStream * st = s->streams[0];
  259. /* check FORM header */
  260. filesize = get_tag(pb, &tag);
  261. if (filesize < 0 || tag != MKTAG('F', 'O', 'R', 'M'))
  262. return AVERROR_INVALIDDATA;
  263. /* AIFF data type */
  264. tag = get_le32(pb);
  265. if (tag == MKTAG('A', 'I', 'F', 'F')) /* Got an AIFF file */
  266. version = AIFF;
  267. else if (tag != MKTAG('A', 'I', 'F', 'C')) /* An AIFF-C file then */
  268. return AVERROR_INVALIDDATA;
  269. filesize -= 4;
  270. st = av_new_stream(s, 0);
  271. if (!st)
  272. return AVERROR_NOMEM;
  273. while (filesize > 0) {
  274. /* parse different chunks */
  275. size = get_tag(pb, &tag);
  276. if (size < 0)
  277. return size;
  278. filesize -= size + 8;
  279. switch (tag) {
  280. case MKTAG('C', 'O', 'M', 'M'): /* Common chunk */
  281. /* Then for the complete header info */
  282. st->nb_frames = get_aiff_header (pb, st->codec, size, version);
  283. if (st->nb_frames < 0)
  284. return st->nb_frames;
  285. break;
  286. case MKTAG('F', 'V', 'E', 'R'): /* Version chunk */
  287. version = get_be32(pb);
  288. break;
  289. case MKTAG('N', 'A', 'M', 'E'): /* Sample name chunk */
  290. get_meta (pb, s->title, sizeof(s->title), size);
  291. break;
  292. case MKTAG('A', 'U', 'T', 'H'): /* Author chunk */
  293. get_meta (pb, s->author, sizeof(s->author), size);
  294. break;
  295. case MKTAG('(', 'c', ')', ' '): /* Copyright chunk */
  296. get_meta (pb, s->copyright, sizeof(s->copyright), size);
  297. break;
  298. case MKTAG('A', 'N', 'N', 'O'): /* Annotation chunk */
  299. get_meta (pb, s->comment, sizeof(s->comment), size);
  300. break;
  301. case MKTAG('S', 'S', 'N', 'D'): /* Sampled sound chunk */
  302. get_be32(pb); /* Block align... don't care */
  303. offset = get_be32(pb); /* Offset of sound data */
  304. goto got_sound;
  305. default: /* Jump */
  306. if (size & 1) /* Always even aligned */
  307. size++;
  308. url_fskip (pb, size);
  309. }
  310. }
  311. /* End of loop and didn't get sound */
  312. return AVERROR_INVALIDDATA;
  313. got_sound:
  314. /* Now positioned, get the sound data start and end */
  315. if (st->nb_frames)
  316. s->file_size = st->nb_frames * st->codec->block_align;
  317. av_set_pts_info(st, 64, 1, st->codec->sample_rate);
  318. st->start_time = 0;
  319. st->duration = st->nb_frames;
  320. /* Position the stream at the first block */
  321. url_fskip(pb, offset);
  322. return 0;
  323. }
  324. #define MAX_SIZE 4096
  325. static int aiff_read_packet(AVFormatContext *s,
  326. AVPacket *pkt)
  327. {
  328. AVStream *st = s->streams[0];
  329. int res;
  330. /* End of stream may be reached */
  331. if (url_feof(&s->pb))
  332. return AVERROR_IO;
  333. /* Now for that packet */
  334. res = av_get_packet(&s->pb, pkt, (MAX_SIZE / st->codec->block_align) * st->codec->block_align);
  335. if (res < 0)
  336. return res;
  337. /* Only one stream in an AIFF file */
  338. pkt->stream_index = 0;
  339. return 0;
  340. }
  341. static int aiff_read_close(AVFormatContext *s)
  342. {
  343. return 0;
  344. }
  345. static int aiff_read_seek(AVFormatContext *s,
  346. int stream_index, int64_t timestamp, int flags)
  347. {
  348. return pcm_read_seek(s, stream_index, timestamp, flags);
  349. }
  350. #ifdef CONFIG_AIFF_DEMUXER
  351. AVInputFormat aiff_demuxer = {
  352. "aiff",
  353. "Audio IFF",
  354. 0,
  355. aiff_probe,
  356. aiff_read_header,
  357. aiff_read_packet,
  358. aiff_read_close,
  359. aiff_read_seek,
  360. };
  361. #endif
  362. #ifdef CONFIG_AIFF_MUXER
  363. AVOutputFormat aiff_muxer = {
  364. "aiff",
  365. "Audio IFF",
  366. "audio/aiff",
  367. "aif,aiff,afc,aifc",
  368. sizeof(AIFFOutputContext),
  369. CODEC_ID_PCM_S16BE,
  370. CODEC_ID_NONE,
  371. aiff_write_header,
  372. aiff_write_packet,
  373. aiff_write_trailer,
  374. };
  375. #endif