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.

332 lines
9.9KB

  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 "avformat.h"
  23. #include "pcm.h"
  24. #include "aiff.h"
  25. #include "isom.h"
  26. #define AIFF 0
  27. #define AIFF_C_VERSION1 0xA2805140
  28. typedef struct {
  29. int64_t data_end;
  30. } AIFFInputContext;
  31. static enum CodecID aiff_codec_get_id(int bps)
  32. {
  33. if (bps <= 8)
  34. return CODEC_ID_PCM_S8;
  35. if (bps <= 16)
  36. return CODEC_ID_PCM_S16BE;
  37. if (bps <= 24)
  38. return CODEC_ID_PCM_S24BE;
  39. if (bps <= 32)
  40. return CODEC_ID_PCM_S32BE;
  41. /* bigger than 32 isn't allowed */
  42. return CODEC_ID_NONE;
  43. }
  44. /* returns the size of the found tag */
  45. static int get_tag(AVIOContext *pb, uint32_t * tag)
  46. {
  47. int size;
  48. if (url_feof(pb))
  49. return AVERROR(EIO);
  50. *tag = avio_rl32(pb);
  51. size = avio_rb32(pb);
  52. if (size < 0)
  53. size = 0x7fffffff;
  54. return size;
  55. }
  56. /* Metadata string read */
  57. static void get_meta(AVFormatContext *s, const char *key, int size)
  58. {
  59. uint8_t *str = av_malloc(size+1);
  60. if (str) {
  61. int res = avio_read(s->pb, str, size);
  62. if (res < 0){
  63. av_free(str);
  64. return;
  65. }
  66. size += (size&1)-res;
  67. str[res] = 0;
  68. av_metadata_set2(&s->metadata, key, str, AV_METADATA_DONT_STRDUP_VAL);
  69. }else
  70. size+= size&1;
  71. avio_skip(s->pb, size);
  72. }
  73. /* Returns the number of sound data frames or negative on error */
  74. static unsigned int get_aiff_header(AVIOContext *pb, AVCodecContext *codec,
  75. int size, unsigned version)
  76. {
  77. AVExtFloat ext;
  78. double sample_rate;
  79. unsigned int num_frames;
  80. if (size & 1)
  81. size++;
  82. codec->codec_type = AVMEDIA_TYPE_AUDIO;
  83. codec->channels = avio_rb16(pb);
  84. num_frames = avio_rb32(pb);
  85. codec->bits_per_coded_sample = avio_rb16(pb);
  86. avio_read(pb, (uint8_t*)&ext, sizeof(ext));/* Sample rate is in */
  87. sample_rate = av_ext2dbl(ext); /* 80 bits BE IEEE extended float */
  88. codec->sample_rate = sample_rate;
  89. size -= 18;
  90. /* Got an AIFF-C? */
  91. if (version == AIFF_C_VERSION1) {
  92. codec->codec_tag = avio_rl32(pb);
  93. codec->codec_id = ff_codec_get_id(ff_codec_aiff_tags, codec->codec_tag);
  94. switch (codec->codec_id) {
  95. case CODEC_ID_PCM_S16BE:
  96. codec->codec_id = aiff_codec_get_id(codec->bits_per_coded_sample);
  97. codec->bits_per_coded_sample = av_get_bits_per_sample(codec->codec_id);
  98. break;
  99. case CODEC_ID_ADPCM_IMA_QT:
  100. codec->block_align = 34*codec->channels;
  101. codec->frame_size = 64;
  102. break;
  103. case CODEC_ID_MACE3:
  104. codec->block_align = 2*codec->channels;
  105. codec->frame_size = 6;
  106. break;
  107. case CODEC_ID_MACE6:
  108. codec->block_align = 1*codec->channels;
  109. codec->frame_size = 6;
  110. break;
  111. case CODEC_ID_GSM:
  112. codec->block_align = 33;
  113. codec->frame_size = 160;
  114. break;
  115. case CODEC_ID_QCELP:
  116. codec->block_align = 35;
  117. codec->frame_size= 160;
  118. break;
  119. default:
  120. break;
  121. }
  122. size -= 4;
  123. } else {
  124. /* Need the codec type */
  125. codec->codec_id = aiff_codec_get_id(codec->bits_per_coded_sample);
  126. codec->bits_per_coded_sample = av_get_bits_per_sample(codec->codec_id);
  127. }
  128. /* Block align needs to be computed in all cases, as the definition
  129. * is specific to applications -> here we use the WAVE format definition */
  130. if (!codec->block_align)
  131. codec->block_align = (codec->bits_per_coded_sample * codec->channels) >> 3;
  132. codec->bit_rate = (codec->frame_size ? codec->sample_rate/codec->frame_size :
  133. codec->sample_rate) * (codec->block_align << 3);
  134. /* Chunk is over */
  135. if (size)
  136. avio_skip(pb, size);
  137. return num_frames;
  138. }
  139. static int aiff_probe(AVProbeData *p)
  140. {
  141. /* check file header */
  142. if (p->buf[0] == 'F' && p->buf[1] == 'O' &&
  143. p->buf[2] == 'R' && p->buf[3] == 'M' &&
  144. p->buf[8] == 'A' && p->buf[9] == 'I' &&
  145. p->buf[10] == 'F' && (p->buf[11] == 'F' || p->buf[11] == 'C'))
  146. return AVPROBE_SCORE_MAX;
  147. else
  148. return 0;
  149. }
  150. /* aiff input */
  151. static int aiff_read_header(AVFormatContext *s,
  152. AVFormatParameters *ap)
  153. {
  154. int size, filesize;
  155. int64_t offset = 0;
  156. uint32_t tag;
  157. unsigned version = AIFF_C_VERSION1;
  158. AVIOContext *pb = s->pb;
  159. AVStream * st;
  160. AIFFInputContext *aiff = s->priv_data;
  161. /* check FORM header */
  162. filesize = get_tag(pb, &tag);
  163. if (filesize < 0 || tag != MKTAG('F', 'O', 'R', 'M'))
  164. return AVERROR_INVALIDDATA;
  165. /* AIFF data type */
  166. tag = avio_rl32(pb);
  167. if (tag == MKTAG('A', 'I', 'F', 'F')) /* Got an AIFF file */
  168. version = AIFF;
  169. else if (tag != MKTAG('A', 'I', 'F', 'C')) /* An AIFF-C file then */
  170. return AVERROR_INVALIDDATA;
  171. filesize -= 4;
  172. st = av_new_stream(s, 0);
  173. if (!st)
  174. return AVERROR(ENOMEM);
  175. while (filesize > 0) {
  176. /* parse different chunks */
  177. size = get_tag(pb, &tag);
  178. if (size < 0)
  179. return size;
  180. filesize -= size + 8;
  181. switch (tag) {
  182. case MKTAG('C', 'O', 'M', 'M'): /* Common chunk */
  183. /* Then for the complete header info */
  184. st->nb_frames = get_aiff_header(pb, st->codec, size, version);
  185. if (st->nb_frames < 0)
  186. return st->nb_frames;
  187. if (offset > 0) // COMM is after SSND
  188. goto got_sound;
  189. break;
  190. case MKTAG('F', 'V', 'E', 'R'): /* Version chunk */
  191. version = avio_rb32(pb);
  192. break;
  193. case MKTAG('N', 'A', 'M', 'E'): /* Sample name chunk */
  194. get_meta(s, "title" , size);
  195. break;
  196. case MKTAG('A', 'U', 'T', 'H'): /* Author chunk */
  197. get_meta(s, "author" , size);
  198. break;
  199. case MKTAG('(', 'c', ')', ' '): /* Copyright chunk */
  200. get_meta(s, "copyright", size);
  201. break;
  202. case MKTAG('A', 'N', 'N', 'O'): /* Annotation chunk */
  203. get_meta(s, "comment" , size);
  204. break;
  205. case MKTAG('S', 'S', 'N', 'D'): /* Sampled sound chunk */
  206. aiff->data_end = avio_tell(pb) + size;
  207. offset = avio_rb32(pb); /* Offset of sound data */
  208. avio_rb32(pb); /* BlockSize... don't care */
  209. offset += avio_tell(pb); /* Compute absolute data offset */
  210. if (st->codec->block_align) /* Assume COMM already parsed */
  211. goto got_sound;
  212. if (!pb->seekable) {
  213. av_log(s, AV_LOG_ERROR, "file is not seekable\n");
  214. return -1;
  215. }
  216. avio_skip(pb, size - 8);
  217. break;
  218. case MKTAG('w', 'a', 'v', 'e'):
  219. if ((uint64_t)size > (1<<30))
  220. return -1;
  221. st->codec->extradata = av_mallocz(size + FF_INPUT_BUFFER_PADDING_SIZE);
  222. if (!st->codec->extradata)
  223. return AVERROR(ENOMEM);
  224. st->codec->extradata_size = size;
  225. avio_read(pb, st->codec->extradata, size);
  226. break;
  227. case MKTAG('C','H','A','N'):
  228. if (size < 12)
  229. return AVERROR_INVALIDDATA;
  230. ff_mov_read_chan(s, size, st->codec);
  231. break;
  232. default: /* Jump */
  233. if (size & 1) /* Always even aligned */
  234. size++;
  235. avio_skip(pb, size);
  236. }
  237. }
  238. if (!st->codec->block_align) {
  239. av_log(s, AV_LOG_ERROR, "could not find COMM tag\n");
  240. return -1;
  241. }
  242. got_sound:
  243. /* Now positioned, get the sound data start and end */
  244. if (st->nb_frames)
  245. s->file_size = st->nb_frames * st->codec->block_align;
  246. av_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. "aiff",
  281. NULL_IF_CONFIG_SMALL("Audio IFF"),
  282. sizeof(AIFFInputContext),
  283. aiff_probe,
  284. aiff_read_header,
  285. aiff_read_packet,
  286. NULL,
  287. pcm_read_seek,
  288. .codec_tag= (const AVCodecTag* const []){ff_codec_aiff_tags, 0},
  289. };