aiffdec.c 10 KB

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