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.

338 lines
10KB

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