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.

337 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. /* Got an 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. switch (codec->codec_id) {
  103. case CODEC_ID_PCM_S16BE:
  104. codec->codec_id = aiff_codec_get_id(codec->bits_per_coded_sample);
  105. codec->bits_per_coded_sample = av_get_bits_per_sample(codec->codec_id);
  106. aiff->block_duration = 1;
  107. break;
  108. case CODEC_ID_ADPCM_IMA_QT:
  109. codec->block_align = 34*codec->channels;
  110. aiff->block_duration = 64;
  111. break;
  112. case CODEC_ID_MACE3:
  113. codec->block_align = 2*codec->channels;
  114. aiff->block_duration = 6;
  115. break;
  116. case CODEC_ID_MACE6:
  117. codec->block_align = 1*codec->channels;
  118. aiff->block_duration = 6;
  119. break;
  120. case CODEC_ID_GSM:
  121. codec->block_align = 33;
  122. aiff->block_duration = 160;
  123. break;
  124. case CODEC_ID_QCELP:
  125. codec->block_align = 35;
  126. aiff->block_duration = 160;
  127. break;
  128. default:
  129. break;
  130. }
  131. size -= 4;
  132. } else {
  133. /* Need the codec type */
  134. codec->codec_id = aiff_codec_get_id(codec->bits_per_coded_sample);
  135. codec->bits_per_coded_sample = av_get_bits_per_sample(codec->codec_id);
  136. aiff->block_duration = 1;
  137. }
  138. /* Block align needs to be computed in all cases, as the definition
  139. * is specific to applications -> here we use the WAVE format definition */
  140. if (!codec->block_align)
  141. codec->block_align = (codec->bits_per_coded_sample * codec->channels) >> 3;
  142. codec->bit_rate = codec->sample_rate * (codec->block_align << 3) /
  143. aiff->block_duration;
  144. /* Chunk is over */
  145. if (size)
  146. avio_skip(pb, size);
  147. return num_frames;
  148. }
  149. static int aiff_probe(AVProbeData *p)
  150. {
  151. /* check file header */
  152. if (p->buf[0] == 'F' && p->buf[1] == 'O' &&
  153. p->buf[2] == 'R' && p->buf[3] == 'M' &&
  154. p->buf[8] == 'A' && p->buf[9] == 'I' &&
  155. p->buf[10] == 'F' && (p->buf[11] == 'F' || p->buf[11] == 'C'))
  156. return AVPROBE_SCORE_MAX;
  157. else
  158. return 0;
  159. }
  160. /* aiff input */
  161. static int aiff_read_header(AVFormatContext *s)
  162. {
  163. int size, filesize;
  164. int64_t offset = 0;
  165. uint32_t tag;
  166. unsigned version = AIFF_C_VERSION1;
  167. AVIOContext *pb = s->pb;
  168. AVStream * st;
  169. AIFFInputContext *aiff = s->priv_data;
  170. /* check FORM header */
  171. filesize = get_tag(pb, &tag);
  172. if (filesize < 0 || tag != MKTAG('F', 'O', 'R', 'M'))
  173. return AVERROR_INVALIDDATA;
  174. /* AIFF data type */
  175. tag = avio_rl32(pb);
  176. if (tag == MKTAG('A', 'I', 'F', 'F')) /* Got an AIFF file */
  177. version = AIFF;
  178. else if (tag != MKTAG('A', 'I', 'F', 'C')) /* An AIFF-C file then */
  179. return AVERROR_INVALIDDATA;
  180. filesize -= 4;
  181. st = avformat_new_stream(s, NULL);
  182. if (!st)
  183. return AVERROR(ENOMEM);
  184. while (filesize > 0) {
  185. /* parse different chunks */
  186. size = get_tag(pb, &tag);
  187. if (size < 0)
  188. return size;
  189. filesize -= size + 8;
  190. switch (tag) {
  191. case MKTAG('C', 'O', 'M', 'M'): /* Common chunk */
  192. /* Then for the complete header info */
  193. st->nb_frames = get_aiff_header(s, size, version);
  194. if (st->nb_frames < 0)
  195. return st->nb_frames;
  196. if (offset > 0) // COMM is after SSND
  197. goto got_sound;
  198. break;
  199. case MKTAG('F', 'V', 'E', 'R'): /* Version chunk */
  200. version = avio_rb32(pb);
  201. break;
  202. case MKTAG('N', 'A', 'M', 'E'): /* Sample name chunk */
  203. get_meta(s, "title" , size);
  204. break;
  205. case MKTAG('A', 'U', 'T', 'H'): /* Author chunk */
  206. get_meta(s, "author" , size);
  207. break;
  208. case MKTAG('(', 'c', ')', ' '): /* Copyright chunk */
  209. get_meta(s, "copyright", size);
  210. break;
  211. case MKTAG('A', 'N', 'N', 'O'): /* Annotation chunk */
  212. get_meta(s, "comment" , size);
  213. break;
  214. case MKTAG('S', 'S', 'N', 'D'): /* Sampled sound chunk */
  215. aiff->data_end = avio_tell(pb) + size;
  216. offset = avio_rb32(pb); /* Offset of sound data */
  217. avio_rb32(pb); /* BlockSize... don't care */
  218. offset += avio_tell(pb); /* Compute absolute data offset */
  219. if (st->codec->block_align) /* Assume COMM already parsed */
  220. goto got_sound;
  221. if (!pb->seekable) {
  222. av_log(s, AV_LOG_ERROR, "file is not seekable\n");
  223. return -1;
  224. }
  225. avio_skip(pb, size - 8);
  226. break;
  227. case MKTAG('w', 'a', 'v', 'e'):
  228. if ((uint64_t)size > (1<<30))
  229. return -1;
  230. st->codec->extradata = av_mallocz(size + FF_INPUT_BUFFER_PADDING_SIZE);
  231. if (!st->codec->extradata)
  232. return AVERROR(ENOMEM);
  233. st->codec->extradata_size = size;
  234. avio_read(pb, st->codec->extradata, size);
  235. break;
  236. case MKTAG('C','H','A','N'):
  237. if (size < 12)
  238. return AVERROR_INVALIDDATA;
  239. ff_mov_read_chan(s, size, st->codec);
  240. break;
  241. default: /* Jump */
  242. if (size & 1) /* Always even aligned */
  243. size++;
  244. avio_skip(pb, size);
  245. }
  246. }
  247. got_sound:
  248. if (!st->codec->block_align) {
  249. av_log(s, AV_LOG_ERROR, "could not find COMM tag or invalid block_align value\n");
  250. return -1;
  251. }
  252. /* Now positioned, get the sound data start and end */
  253. avpriv_set_pts_info(st, 64, 1, st->codec->sample_rate);
  254. st->start_time = 0;
  255. st->duration = st->nb_frames * aiff->block_duration;
  256. /* Position the stream at the first block */
  257. avio_seek(pb, offset, SEEK_SET);
  258. return 0;
  259. }
  260. #define MAX_SIZE 4096
  261. static int aiff_read_packet(AVFormatContext *s,
  262. AVPacket *pkt)
  263. {
  264. AVStream *st = s->streams[0];
  265. AIFFInputContext *aiff = s->priv_data;
  266. int64_t max_size;
  267. int res, size;
  268. /* calculate size of remaining data */
  269. max_size = aiff->data_end - avio_tell(s->pb);
  270. if (max_size <= 0)
  271. return AVERROR_EOF;
  272. /* Now for that packet */
  273. if (st->codec->block_align >= 33) // GSM, QCLP, IMA4
  274. size = st->codec->block_align;
  275. else
  276. size = (MAX_SIZE / st->codec->block_align) * st->codec->block_align;
  277. size = FFMIN(max_size, size);
  278. res = av_get_packet(s->pb, pkt, size);
  279. if (res < 0)
  280. return res;
  281. /* Only one stream in an AIFF file */
  282. pkt->stream_index = 0;
  283. pkt->duration = (res / st->codec->block_align) * aiff->block_duration;
  284. return 0;
  285. }
  286. AVInputFormat ff_aiff_demuxer = {
  287. .name = "aiff",
  288. .long_name = NULL_IF_CONFIG_SMALL("Audio IFF"),
  289. .priv_data_size = sizeof(AIFFInputContext),
  290. .read_probe = aiff_probe,
  291. .read_header = aiff_read_header,
  292. .read_packet = aiff_read_packet,
  293. .read_seek = ff_pcm_read_seek,
  294. .codec_tag= (const AVCodecTag* const []){ff_codec_aiff_tags, 0},
  295. };