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.

348 lines
11KB

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