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.

483 lines
13KB

  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 "avi.h"
  21. #include "intfloat_readwrite.h"
  22. const CodecTag codec_aiff_tags[] = {
  23. { CODEC_ID_PCM_S16BE, MKTAG('N','O','N','E') },
  24. { CODEC_ID_PCM_S8, MKTAG('N','O','N','E') },
  25. { CODEC_ID_PCM_S24BE, MKTAG('N','O','N','E') },
  26. { CODEC_ID_PCM_S32BE, MKTAG('N','O','N','E') },
  27. { CODEC_ID_PCM_ALAW, MKTAG('a','l','a','w') },
  28. { CODEC_ID_PCM_ALAW, MKTAG('A','L','A','W') },
  29. { CODEC_ID_PCM_MULAW, MKTAG('u','l','a','w') },
  30. { CODEC_ID_PCM_MULAW, MKTAG('U','L','A','W') },
  31. { CODEC_ID_MACE3, MKTAG('M','A','C','3') },
  32. { CODEC_ID_MACE6, MKTAG('M','A','C','6') },
  33. { CODEC_ID_GSM, MKTAG('G','S','M',' ') },
  34. { CODEC_ID_ADPCM_G726, MKTAG('G','7','2','6') },
  35. { 0, 0 },
  36. };
  37. #define AIFF 0
  38. #define AIFF_C_VERSION1 0xA2805140
  39. static int aiff_codec_get_id (int bps)
  40. {
  41. if (bps <= 8)
  42. return CODEC_ID_PCM_S8;
  43. if (bps <= 16)
  44. return CODEC_ID_PCM_S16BE;
  45. if (bps <= 24)
  46. return CODEC_ID_PCM_S24BE;
  47. if (bps <= 32)
  48. return CODEC_ID_PCM_S32BE;
  49. /* bigger than 32 isn't allowed */
  50. return 0;
  51. }
  52. /* returns the size of the found tag */
  53. static int get_tag(ByteIOContext *pb, uint32_t * tag)
  54. {
  55. int size;
  56. if (url_feof(pb))
  57. return AVERROR_IO;
  58. *tag = get_le32(pb);
  59. size = get_be32(pb);
  60. if (size < 0)
  61. size = 0x7fffffff;
  62. return size;
  63. }
  64. /* Metadata string read */
  65. static void get_meta(ByteIOContext *pb, char * str, int strsize, int size)
  66. {
  67. int res;
  68. if (size > strsize-1)
  69. res = get_buffer(pb, (uint8_t*)str, strsize-1);
  70. else
  71. res = get_buffer(pb, (uint8_t*)str, size);
  72. if (res < 0)
  73. return;
  74. str[res] = 0;
  75. if (size & 1)
  76. size++;
  77. size -= res;
  78. if (size);
  79. url_fskip(pb, size);
  80. }
  81. /* Returns the number of bits per second */
  82. static int fix_bps(int codec_id)
  83. {
  84. switch (codec_id) {
  85. case CODEC_ID_PCM_S8:
  86. return 8;
  87. case CODEC_ID_PCM_S16BE:
  88. return 16;
  89. case CODEC_ID_PCM_S24BE:
  90. return 24;
  91. case CODEC_ID_PCM_S32BE:
  92. return 32;
  93. }
  94. return -1;
  95. }
  96. /* Returns the number of sound data frames or negative on error */
  97. unsigned int get_aiff_header(ByteIOContext *pb, AVCodecContext *codec,
  98. int size, unsigned version)
  99. {
  100. AVExtFloat ext;
  101. double sample_rate;
  102. unsigned int num_frames;
  103. if (size & 1)
  104. size++;
  105. codec->codec_type = CODEC_TYPE_AUDIO;
  106. codec->channels = get_be16(pb);
  107. num_frames = get_be32(pb);
  108. codec->bits_per_sample = get_be16(pb);
  109. get_buffer(pb, (uint8_t*)&ext, sizeof(ext));/* Sample rate is in */
  110. sample_rate = av_ext2dbl(ext); /* 80 bits BE IEEE extended float */
  111. codec->sample_rate = sample_rate;
  112. size -= 18;
  113. /* Got an AIFF-C? */
  114. if (version == AIFF_C_VERSION1) {
  115. codec->codec_tag = get_le32(pb);
  116. codec->codec_id = codec_get_id (codec_aiff_tags, codec->codec_tag);
  117. if (codec->codec_id == CODEC_ID_PCM_S16BE) {
  118. codec->codec_id = aiff_codec_get_id (codec->bits_per_sample);
  119. codec->bits_per_sample = fix_bps(codec->codec_id);
  120. }
  121. size -= 4;
  122. } else {
  123. /* Need the codec type */
  124. codec->codec_id = aiff_codec_get_id (codec->bits_per_sample);
  125. codec->bits_per_sample = fix_bps(codec->codec_id);
  126. }
  127. if (!codec->codec_id)
  128. return AVERROR_INVALIDDATA;
  129. /* Block align needs to be computed in all cases, as the definition
  130. * is specific to applications -> here we use the WAVE format definition */
  131. codec->block_align = (codec->bits_per_sample * codec->channels) >> 3;
  132. codec->bit_rate = codec->sample_rate * codec->block_align;
  133. /* Chunk is over */
  134. if (size)
  135. url_fseek(pb, size, SEEK_CUR);
  136. return num_frames;
  137. }
  138. #ifdef CONFIG_MUXERS
  139. typedef struct {
  140. offset_t form;
  141. offset_t frames;
  142. offset_t ssnd;
  143. } AIFFOutputContext;
  144. static int aiff_write_header(AVFormatContext *s)
  145. {
  146. AIFFOutputContext *aiff = s->priv_data;
  147. ByteIOContext *pb = &s->pb;
  148. AVCodecContext *enc = s->streams[0]->codec;
  149. AVExtFloat sample_rate;
  150. int coder_len;
  151. /* First verify if format is ok */
  152. enc->codec_tag = codec_get_tag(codec_aiff_tags, enc->codec_id);
  153. if (!enc->codec_tag) {
  154. av_free(aiff);
  155. return -1;
  156. }
  157. coder_len = strlen(enc->codec->name);
  158. /* FORM AIFF header */
  159. put_tag(pb, "FORM");
  160. aiff->form = url_ftell(pb);
  161. put_be32(pb, 0); /* file length */
  162. put_tag(pb, "AIFC");
  163. /* Version chunk */
  164. put_tag(pb, "FVER");
  165. put_be32(pb, 4);
  166. put_be32(pb, 0xA2805140);
  167. /* Common chunk */
  168. put_tag(pb, "COMM");
  169. if (coder_len & 1) /* Common chunk is of var size */
  170. put_be32(pb, 23+coder_len);
  171. else
  172. put_be32(pb, 24+coder_len);
  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 = (enc->block_align<<3) / enc->channels;
  178. put_be16(pb, enc->bits_per_sample); /* Sample size */
  179. sample_rate = av_dbl2ext((double)enc->sample_rate);
  180. put_buffer(pb, (uint8_t*)&sample_rate, sizeof(sample_rate));
  181. put_le32(pb, enc->codec_tag);
  182. if (coder_len & 1) {
  183. put_byte(pb, coder_len);
  184. put_buffer(pb, (uint8_t*)enc->codec->name, coder_len);
  185. } else {
  186. put_byte(pb, coder_len+1);
  187. put_buffer(pb, (uint8_t*)enc->codec->name, coder_len);
  188. put_byte(pb, 0);
  189. }
  190. /* Sound data chunk */
  191. put_tag(pb, "SSND");
  192. aiff->ssnd = url_ftell(pb); /* Sound chunk size */
  193. put_be32(pb, 0); /* Sound samples data size */
  194. put_be32(pb, 0); /* Data offset */
  195. put_be32(pb, 0); /* Block-size (block align) */
  196. av_set_pts_info(s->streams[0], 64, 1, s->streams[0]->codec->sample_rate);
  197. /* Data is starting here */
  198. put_flush_packet(pb);
  199. return 0;
  200. }
  201. static int aiff_write_packet(AVFormatContext *s, AVPacket *pkt)
  202. {
  203. ByteIOContext *pb = &s->pb;
  204. put_buffer(pb, pkt->data, pkt->size);
  205. return 0;
  206. }
  207. static int aiff_write_trailer(AVFormatContext *s)
  208. {
  209. ByteIOContext *pb = &s->pb;
  210. AIFFOutputContext *aiff = s->priv_data;
  211. AVCodecContext *enc = s->streams[0]->codec;
  212. /* Chunks sizes must be even */
  213. offset_t file_size, end_size;
  214. end_size = file_size = url_ftell(pb);
  215. if (file_size & 1) {
  216. put_byte(pb, 0);
  217. end_size++;
  218. }
  219. if (!url_is_streamed(&s->pb)) {
  220. /* File length */
  221. url_fseek(pb, aiff->form, SEEK_SET);
  222. put_be32(pb, (uint32_t)(file_size - aiff->form - 4));
  223. /* Number of sample frames */
  224. url_fseek(pb, aiff->frames, SEEK_SET);
  225. put_be32(pb, ((uint32_t)(file_size-aiff->ssnd-12))/enc->block_align);
  226. /* Sound Data chunk size */
  227. url_fseek(pb, aiff->ssnd, SEEK_SET);
  228. put_be32(pb, (uint32_t)(file_size - aiff->ssnd - 4));
  229. /* return to the end */
  230. url_fseek(pb, end_size, SEEK_SET);
  231. put_flush_packet(pb);
  232. }
  233. return 0;
  234. }
  235. #endif //CONFIG_MUXERS
  236. static int aiff_probe(AVProbeData *p)
  237. {
  238. /* check file header */
  239. if (p->buf_size < 16)
  240. return 0;
  241. if (p->buf[0] == 'F' && p->buf[1] == 'O' &&
  242. p->buf[2] == 'R' && p->buf[3] == 'M' &&
  243. p->buf[8] == 'A' && p->buf[9] == 'I' &&
  244. p->buf[10] == 'F' && (p->buf[11] == 'F' || p->buf[11] == 'C'))
  245. return AVPROBE_SCORE_MAX;
  246. else
  247. return 0;
  248. }
  249. /* aiff input */
  250. static int aiff_read_header(AVFormatContext *s,
  251. AVFormatParameters *ap)
  252. {
  253. int size, filesize, offset;
  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_NOMEM;
  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. break;
  285. case MKTAG('F', 'V', 'E', 'R'): /* Version chunk */
  286. version = get_be32(pb);
  287. break;
  288. case MKTAG('N', 'A', 'M', 'E'): /* Sample name chunk */
  289. get_meta (pb, s->title, sizeof(s->title), size);
  290. break;
  291. case MKTAG('A', 'U', 'T', 'H'): /* Author chunk */
  292. get_meta (pb, s->author, sizeof(s->author), size);
  293. break;
  294. case MKTAG('(', 'c', ')', ' '): /* Copyright chunk */
  295. get_meta (pb, s->copyright, sizeof(s->copyright), size);
  296. break;
  297. case MKTAG('A', 'N', 'N', 'O'): /* Annotation chunk */
  298. get_meta (pb, s->comment, sizeof(s->comment), size);
  299. break;
  300. case MKTAG('S', 'S', 'N', 'D'): /* Sampled sound chunk */
  301. get_be32(pb); /* Block align... don't care */
  302. offset = get_be32(pb); /* Offset of sound data */
  303. goto got_sound;
  304. default: /* Jump */
  305. if (size & 1) /* Always even aligned */
  306. size++;
  307. url_fskip (pb, size);
  308. }
  309. }
  310. /* End of loop and didn't get sound */
  311. return AVERROR_INVALIDDATA;
  312. got_sound:
  313. /* Now positioned, get the sound data start and end */
  314. if (st->nb_frames)
  315. s->file_size = st->nb_frames * st->codec->block_align;
  316. av_set_pts_info(st, 64, 1, st->codec->sample_rate);
  317. st->start_time = 0;
  318. st->duration = st->nb_frames;
  319. /* Position the stream at the first block */
  320. url_fskip(pb, offset);
  321. return 0;
  322. }
  323. #define MAX_SIZE 4096
  324. static int aiff_read_packet(AVFormatContext *s,
  325. AVPacket *pkt)
  326. {
  327. offset_t pos;
  328. int res, size;
  329. /* End of stream may be reached */
  330. if (url_feof(&s->pb))
  331. return AVERROR_IO;
  332. /* Need to know if reached the end sound data */
  333. size = MAX_SIZE;
  334. if (s->file_size) {
  335. pos = url_ftell (&s->pb) - s->file_size;
  336. if (pos >= s->file_size)
  337. size = 0;
  338. else if (pos + MAX_SIZE >= s->file_size)
  339. size = s->file_size - pos;
  340. }
  341. /* Now for that packet */
  342. res = av_get_packet (&s->pb, pkt, MAX_SIZE);
  343. if (res < 0)
  344. return res;
  345. /* Only one stream in an AIFF file */
  346. pkt->stream_index = 0;
  347. /* Finaly fix the read to a block */
  348. if (size <= res)
  349. pkt->size = size - (size % s->streams[0]->codec->block_align);
  350. else
  351. pkt->size = res - (res % s->streams[0]->codec->block_align);
  352. return 0;
  353. }
  354. static int aiff_read_close(AVFormatContext *s)
  355. {
  356. return 0;
  357. }
  358. static int aiff_read_seek(AVFormatContext *s,
  359. int stream_index, int64_t timestamp, int flags)
  360. {
  361. return pcm_read_seek(s, stream_index, timestamp, flags);
  362. }
  363. static AVInputFormat aiff_iformat = {
  364. "aiff",
  365. "Audio IFF",
  366. 0,
  367. aiff_probe,
  368. aiff_read_header,
  369. aiff_read_packet,
  370. aiff_read_close,
  371. aiff_read_seek,
  372. };
  373. #ifdef CONFIG_MUXERS
  374. static AVOutputFormat aiff_oformat = {
  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. };
  386. #endif //CONFIG_MUXERS
  387. int ff_aiff_init(void)
  388. {
  389. av_register_input_format(&aiff_iformat);
  390. #ifdef CONFIG_MUXERS
  391. av_register_output_format(&aiff_oformat);
  392. #endif //CONFIG_MUXERS
  393. return 0;
  394. }