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.

343 lines
10KB

  1. /*
  2. * AIFF/AIFF-C 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 "libavutil/mathematics.h"
  22. #include "libavutil/dict.h"
  23. #include "avformat.h"
  24. #include "internal.h"
  25. #include "pcm.h"
  26. #include "aiff.h"
  27. #include "isom.h"
  28. #define AIFF 0
  29. #define AIFF_C_VERSION1 0xA2805140
  30. typedef struct {
  31. int64_t data_end;
  32. int block_duration;
  33. } AIFFInputContext;
  34. static enum CodecID aiff_codec_get_id(int bps)
  35. {
  36. if (bps <= 8)
  37. return CODEC_ID_PCM_S8;
  38. if (bps <= 16)
  39. return CODEC_ID_PCM_S16BE;
  40. if (bps <= 24)
  41. return CODEC_ID_PCM_S24BE;
  42. if (bps <= 32)
  43. return CODEC_ID_PCM_S32BE;
  44. /* bigger than 32 isn't allowed */
  45. return CODEC_ID_NONE;
  46. }
  47. /* returns the size of the found tag */
  48. static int get_tag(AVIOContext *pb, uint32_t * tag)
  49. {
  50. int size;
  51. if (url_feof(pb))
  52. return AVERROR(EIO);
  53. *tag = avio_rl32(pb);
  54. size = avio_rb32(pb);
  55. if (size < 0)
  56. size = 0x7fffffff;
  57. return size;
  58. }
  59. /* Metadata string read */
  60. static void get_meta(AVFormatContext *s, const char *key, int size)
  61. {
  62. uint8_t *str = av_malloc(size+1);
  63. if (str) {
  64. int res = avio_read(s->pb, str, size);
  65. if (res < 0){
  66. av_free(str);
  67. return;
  68. }
  69. size += (size&1)-res;
  70. str[res] = 0;
  71. av_dict_set(&s->metadata, key, str, AV_DICT_DONT_STRDUP_VAL);
  72. }else
  73. size+= size&1;
  74. avio_skip(s->pb, size);
  75. }
  76. /* Returns the number of sound data frames or negative on error */
  77. static unsigned int get_aiff_header(AVFormatContext *s, int size,
  78. unsigned version)
  79. {
  80. AVIOContext *pb = s->pb;
  81. AVCodecContext *codec = s->streams[0]->codec;
  82. AIFFInputContext *aiff = s->priv_data;
  83. int exp;
  84. uint64_t val;
  85. double sample_rate;
  86. unsigned int num_frames;
  87. if (size & 1)
  88. size++;
  89. codec->codec_type = AVMEDIA_TYPE_AUDIO;
  90. codec->channels = avio_rb16(pb);
  91. num_frames = avio_rb32(pb);
  92. codec->bits_per_coded_sample = avio_rb16(pb);
  93. exp = avio_rb16(pb);
  94. val = avio_rb64(pb);
  95. sample_rate = ldexp(val, exp - 16383 - 63);
  96. codec->sample_rate = sample_rate;
  97. size -= 18;
  98. /* get codec id for AIFF-C */
  99. if (version == AIFF_C_VERSION1) {
  100. codec->codec_tag = avio_rl32(pb);
  101. codec->codec_id = ff_codec_get_id(ff_codec_aiff_tags, codec->codec_tag);
  102. size -= 4;
  103. }
  104. if (version != AIFF_C_VERSION1 || codec->codec_id == CODEC_ID_PCM_S16BE) {
  105. codec->codec_id = aiff_codec_get_id(codec->bits_per_coded_sample);
  106. codec->bits_per_coded_sample = av_get_bits_per_sample(codec->codec_id);
  107. aiff->block_duration = 1;
  108. } else {
  109. switch (codec->codec_id) {
  110. case CODEC_ID_PCM_F32BE:
  111. case CODEC_ID_PCM_F64BE:
  112. case CODEC_ID_PCM_S16LE:
  113. case CODEC_ID_PCM_ALAW:
  114. case CODEC_ID_PCM_MULAW:
  115. aiff->block_duration = 1;
  116. break;
  117. case CODEC_ID_ADPCM_IMA_QT:
  118. codec->block_align = 34*codec->channels;
  119. break;
  120. case CODEC_ID_MACE3:
  121. codec->block_align = 2*codec->channels;
  122. break;
  123. case CODEC_ID_MACE6:
  124. codec->block_align = 1*codec->channels;
  125. break;
  126. case CODEC_ID_GSM:
  127. codec->block_align = 33;
  128. break;
  129. case CODEC_ID_QCELP:
  130. codec->block_align = 35;
  131. break;
  132. default:
  133. aiff->block_duration = 1;
  134. break;
  135. }
  136. if (codec->block_align > 0)
  137. aiff->block_duration = av_get_audio_frame_duration(codec,
  138. codec->block_align);
  139. }
  140. /* Block align needs to be computed in all cases, as the definition
  141. * is specific to applications -> here we use the WAVE format definition */
  142. if (!codec->block_align)
  143. codec->block_align = (codec->bits_per_coded_sample * codec->channels) >> 3;
  144. if (aiff->block_duration) {
  145. codec->bit_rate = codec->sample_rate * (codec->block_align << 3) /
  146. aiff->block_duration;
  147. }
  148. /* Chunk is over */
  149. if (size)
  150. avio_skip(pb, size);
  151. return num_frames;
  152. }
  153. static int aiff_probe(AVProbeData *p)
  154. {
  155. /* check file header */
  156. if (p->buf[0] == 'F' && p->buf[1] == 'O' &&
  157. p->buf[2] == 'R' && p->buf[3] == 'M' &&
  158. p->buf[8] == 'A' && p->buf[9] == 'I' &&
  159. p->buf[10] == 'F' && (p->buf[11] == 'F' || p->buf[11] == 'C'))
  160. return AVPROBE_SCORE_MAX;
  161. else
  162. return 0;
  163. }
  164. /* aiff input */
  165. static int aiff_read_header(AVFormatContext *s)
  166. {
  167. int size, filesize;
  168. int64_t offset = 0;
  169. uint32_t tag;
  170. unsigned version = AIFF_C_VERSION1;
  171. AVIOContext *pb = s->pb;
  172. AVStream * st;
  173. AIFFInputContext *aiff = s->priv_data;
  174. /* check FORM header */
  175. filesize = get_tag(pb, &tag);
  176. if (filesize < 0 || tag != MKTAG('F', 'O', 'R', 'M'))
  177. return AVERROR_INVALIDDATA;
  178. /* AIFF data type */
  179. tag = avio_rl32(pb);
  180. if (tag == MKTAG('A', 'I', 'F', 'F')) /* Got an AIFF file */
  181. version = AIFF;
  182. else if (tag != MKTAG('A', 'I', 'F', 'C')) /* An AIFF-C file then */
  183. return AVERROR_INVALIDDATA;
  184. filesize -= 4;
  185. st = avformat_new_stream(s, NULL);
  186. if (!st)
  187. return AVERROR(ENOMEM);
  188. while (filesize > 0) {
  189. /* parse different chunks */
  190. size = get_tag(pb, &tag);
  191. if (size < 0)
  192. return size;
  193. filesize -= size + 8;
  194. switch (tag) {
  195. case MKTAG('C', 'O', 'M', 'M'): /* Common chunk */
  196. /* Then for the complete header info */
  197. st->nb_frames = get_aiff_header(s, size, version);
  198. if (st->nb_frames < 0)
  199. return st->nb_frames;
  200. if (offset > 0) // COMM is after SSND
  201. goto got_sound;
  202. break;
  203. case MKTAG('F', 'V', 'E', 'R'): /* Version chunk */
  204. version = avio_rb32(pb);
  205. break;
  206. case MKTAG('N', 'A', 'M', 'E'): /* Sample name chunk */
  207. get_meta(s, "title" , size);
  208. break;
  209. case MKTAG('A', 'U', 'T', 'H'): /* Author chunk */
  210. get_meta(s, "author" , size);
  211. break;
  212. case MKTAG('(', 'c', ')', ' '): /* Copyright chunk */
  213. get_meta(s, "copyright", size);
  214. break;
  215. case MKTAG('A', 'N', 'N', 'O'): /* Annotation chunk */
  216. get_meta(s, "comment" , size);
  217. break;
  218. case MKTAG('S', 'S', 'N', 'D'): /* Sampled sound chunk */
  219. aiff->data_end = avio_tell(pb) + size;
  220. offset = avio_rb32(pb); /* Offset of sound data */
  221. avio_rb32(pb); /* BlockSize... don't care */
  222. offset += avio_tell(pb); /* Compute absolute data offset */
  223. if (st->codec->block_align) /* Assume COMM already parsed */
  224. goto got_sound;
  225. if (!pb->seekable) {
  226. av_log(s, AV_LOG_ERROR, "file is not seekable\n");
  227. return -1;
  228. }
  229. avio_skip(pb, size - 8);
  230. break;
  231. case MKTAG('w', 'a', 'v', 'e'):
  232. if ((uint64_t)size > (1<<30))
  233. return -1;
  234. st->codec->extradata = av_mallocz(size + FF_INPUT_BUFFER_PADDING_SIZE);
  235. if (!st->codec->extradata)
  236. return AVERROR(ENOMEM);
  237. st->codec->extradata_size = size;
  238. avio_read(pb, st->codec->extradata, size);
  239. break;
  240. case MKTAG('C','H','A','N'):
  241. if (size < 12)
  242. return AVERROR_INVALIDDATA;
  243. ff_mov_read_chan(s, size, st->codec);
  244. break;
  245. default: /* Jump */
  246. if (size & 1) /* Always even aligned */
  247. size++;
  248. avio_skip(pb, size);
  249. }
  250. }
  251. got_sound:
  252. if (!st->codec->block_align) {
  253. av_log(s, AV_LOG_ERROR, "could not find COMM tag or invalid block_align value\n");
  254. return -1;
  255. }
  256. /* Now positioned, get the sound data start and end */
  257. avpriv_set_pts_info(st, 64, 1, st->codec->sample_rate);
  258. st->start_time = 0;
  259. st->duration = st->nb_frames * aiff->block_duration;
  260. /* Position the stream at the first block */
  261. avio_seek(pb, offset, SEEK_SET);
  262. return 0;
  263. }
  264. #define MAX_SIZE 4096
  265. static int aiff_read_packet(AVFormatContext *s,
  266. AVPacket *pkt)
  267. {
  268. AVStream *st = s->streams[0];
  269. AIFFInputContext *aiff = s->priv_data;
  270. int64_t max_size;
  271. int res, size;
  272. /* calculate size of remaining data */
  273. max_size = aiff->data_end - avio_tell(s->pb);
  274. if (max_size <= 0)
  275. return AVERROR_EOF;
  276. /* Now for that packet */
  277. if (st->codec->block_align >= 33) // GSM, QCLP, IMA4
  278. size = st->codec->block_align;
  279. else
  280. size = (MAX_SIZE / st->codec->block_align) * st->codec->block_align;
  281. size = FFMIN(max_size, size);
  282. res = av_get_packet(s->pb, pkt, size);
  283. if (res < 0)
  284. return res;
  285. if (size >= st->codec->block_align)
  286. pkt->flags &= ~AV_PKT_FLAG_CORRUPT;
  287. /* Only one stream in an AIFF file */
  288. pkt->stream_index = 0;
  289. pkt->duration = (res / st->codec->block_align) * aiff->block_duration;
  290. return 0;
  291. }
  292. AVInputFormat ff_aiff_demuxer = {
  293. .name = "aiff",
  294. .long_name = NULL_IF_CONFIG_SMALL("Audio IFF"),
  295. .priv_data_size = sizeof(AIFFInputContext),
  296. .read_probe = aiff_probe,
  297. .read_header = aiff_read_header,
  298. .read_packet = aiff_read_packet,
  299. .read_seek = ff_pcm_read_seek,
  300. .codec_tag = (const AVCodecTag* const []){ ff_codec_aiff_tags, 0 },
  301. };