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.

330 lines
10.0KB

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