• Main Page
  • Related Pages
  • Modules
  • Data Structures
  • Files
  • Examples
  • File List
  • Globals

libavformat/aiffdec.c

Go to the documentation of this file.
00001 /*
00002  * AIFF/AIFF-C demuxer
00003  * Copyright (c) 2006  Patrick Guimond
00004  *
00005  * This file is part of FFmpeg.
00006  *
00007  * FFmpeg is free software; you can redistribute it and/or
00008  * modify it under the terms of the GNU Lesser General Public
00009  * License as published by the Free Software Foundation; either
00010  * version 2.1 of the License, or (at your option) any later version.
00011  *
00012  * FFmpeg is distributed in the hope that it will be useful,
00013  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00014  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00015  * Lesser General Public License for more details.
00016  *
00017  * You should have received a copy of the GNU Lesser General Public
00018  * License along with FFmpeg; if not, write to the Free Software
00019  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
00020  */
00021 
00022 #include "libavutil/intfloat_readwrite.h"
00023 #include "libavutil/dict.h"
00024 #include "avformat.h"
00025 #include "pcm.h"
00026 #include "aiff.h"
00027 #include "isom.h"
00028 
00029 #define AIFF                    0
00030 #define AIFF_C_VERSION1         0xA2805140
00031 
00032 typedef struct {
00033     int64_t data_end;
00034 } AIFFInputContext;
00035 
00036 static enum CodecID aiff_codec_get_id(int bps)
00037 {
00038     if (bps <= 8)
00039         return CODEC_ID_PCM_S8;
00040     if (bps <= 16)
00041         return CODEC_ID_PCM_S16BE;
00042     if (bps <= 24)
00043         return CODEC_ID_PCM_S24BE;
00044     if (bps <= 32)
00045         return CODEC_ID_PCM_S32BE;
00046 
00047     /* bigger than 32 isn't allowed  */
00048     return CODEC_ID_NONE;
00049 }
00050 
00051 /* returns the size of the found tag */
00052 static int get_tag(AVIOContext *pb, uint32_t * tag)
00053 {
00054     int size;
00055 
00056     if (url_feof(pb))
00057         return AVERROR(EIO);
00058 
00059     *tag = avio_rl32(pb);
00060     size = avio_rb32(pb);
00061 
00062     if (size < 0)
00063         size = 0x7fffffff;
00064 
00065     return size;
00066 }
00067 
00068 /* Metadata string read */
00069 static void get_meta(AVFormatContext *s, const char *key, int size)
00070 {
00071     uint8_t *str = av_malloc(size+1);
00072 
00073     if (str) {
00074         int res = avio_read(s->pb, str, size);
00075         if (res < 0){
00076             av_free(str);
00077             return;
00078         }
00079         size += (size&1)-res;
00080         str[res] = 0;
00081         av_dict_set(&s->metadata, key, str, AV_METADATA_DONT_STRDUP_VAL);
00082     }else
00083         size+= size&1;
00084 
00085     avio_skip(s->pb, size);
00086 }
00087 
00088 /* Returns the number of sound data frames or negative on error */
00089 static unsigned int get_aiff_header(AVIOContext *pb, AVCodecContext *codec,
00090                              int size, unsigned version)
00091 {
00092     AVExtFloat ext;
00093     double sample_rate;
00094     unsigned int num_frames;
00095 
00096     if (size & 1)
00097         size++;
00098     codec->codec_type = AVMEDIA_TYPE_AUDIO;
00099     codec->channels = avio_rb16(pb);
00100     num_frames = avio_rb32(pb);
00101     codec->bits_per_coded_sample = avio_rb16(pb);
00102 
00103     avio_read(pb, (uint8_t*)&ext, sizeof(ext));/* Sample rate is in */
00104     sample_rate = av_ext2dbl(ext);          /* 80 bits BE IEEE extended float */
00105     codec->sample_rate = sample_rate;
00106     size -= 18;
00107 
00108     /* Got an AIFF-C? */
00109     if (version == AIFF_C_VERSION1) {
00110         codec->codec_tag = avio_rl32(pb);
00111         codec->codec_id  = ff_codec_get_id(ff_codec_aiff_tags, codec->codec_tag);
00112 
00113         switch (codec->codec_id) {
00114         case CODEC_ID_PCM_S16BE:
00115             codec->codec_id = aiff_codec_get_id(codec->bits_per_coded_sample);
00116             codec->bits_per_coded_sample = av_get_bits_per_sample(codec->codec_id);
00117             break;
00118         case CODEC_ID_ADPCM_IMA_QT:
00119             codec->block_align = 34*codec->channels;
00120             codec->frame_size = 64;
00121             break;
00122         case CODEC_ID_MACE3:
00123             codec->block_align = 2*codec->channels;
00124             codec->frame_size = 6;
00125             break;
00126         case CODEC_ID_MACE6:
00127             codec->block_align = 1*codec->channels;
00128             codec->frame_size = 6;
00129             break;
00130         case CODEC_ID_GSM:
00131             codec->block_align = 33;
00132             codec->frame_size = 160;
00133             break;
00134         case CODEC_ID_QCELP:
00135             codec->block_align = 35;
00136             codec->frame_size= 160;
00137             break;
00138         default:
00139             break;
00140         }
00141         size -= 4;
00142     } else {
00143         /* Need the codec type */
00144         codec->codec_id = aiff_codec_get_id(codec->bits_per_coded_sample);
00145         codec->bits_per_coded_sample = av_get_bits_per_sample(codec->codec_id);
00146     }
00147 
00148     /* Block align needs to be computed in all cases, as the definition
00149      * is specific to applications -> here we use the WAVE format definition */
00150     if (!codec->block_align)
00151         codec->block_align = (codec->bits_per_coded_sample * codec->channels) >> 3;
00152 
00153     codec->bit_rate = (codec->frame_size ? codec->sample_rate/codec->frame_size :
00154                        codec->sample_rate) * (codec->block_align << 3);
00155 
00156     /* Chunk is over */
00157     if (size)
00158         avio_skip(pb, size);
00159 
00160     return num_frames;
00161 }
00162 
00163 static int aiff_probe(AVProbeData *p)
00164 {
00165     /* check file header */
00166     if (p->buf[0] == 'F' && p->buf[1] == 'O' &&
00167         p->buf[2] == 'R' && p->buf[3] == 'M' &&
00168         p->buf[8] == 'A' && p->buf[9] == 'I' &&
00169         p->buf[10] == 'F' && (p->buf[11] == 'F' || p->buf[11] == 'C'))
00170         return AVPROBE_SCORE_MAX;
00171     else
00172         return 0;
00173 }
00174 
00175 /* aiff input */
00176 static int aiff_read_header(AVFormatContext *s,
00177                             AVFormatParameters *ap)
00178 {
00179     int size, filesize;
00180     int64_t offset = 0;
00181     uint32_t tag;
00182     unsigned version = AIFF_C_VERSION1;
00183     AVIOContext *pb = s->pb;
00184     AVStream * st;
00185     AIFFInputContext *aiff = s->priv_data;
00186 
00187     /* check FORM header */
00188     filesize = get_tag(pb, &tag);
00189     if (filesize < 0 || tag != MKTAG('F', 'O', 'R', 'M'))
00190         return AVERROR_INVALIDDATA;
00191 
00192     /* AIFF data type */
00193     tag = avio_rl32(pb);
00194     if (tag == MKTAG('A', 'I', 'F', 'F'))       /* Got an AIFF file */
00195         version = AIFF;
00196     else if (tag != MKTAG('A', 'I', 'F', 'C'))  /* An AIFF-C file then */
00197         return AVERROR_INVALIDDATA;
00198 
00199     filesize -= 4;
00200 
00201     st = av_new_stream(s, 0);
00202     if (!st)
00203         return AVERROR(ENOMEM);
00204 
00205     while (filesize > 0) {
00206         /* parse different chunks */
00207         size = get_tag(pb, &tag);
00208         if (size < 0)
00209             return size;
00210 
00211         filesize -= size + 8;
00212 
00213         switch (tag) {
00214         case MKTAG('C', 'O', 'M', 'M'):     /* Common chunk */
00215             /* Then for the complete header info */
00216             st->nb_frames = get_aiff_header(pb, st->codec, size, version);
00217             if (st->nb_frames < 0)
00218                 return st->nb_frames;
00219             if (offset > 0) // COMM is after SSND
00220                 goto got_sound;
00221             break;
00222         case MKTAG('F', 'V', 'E', 'R'):     /* Version chunk */
00223             version = avio_rb32(pb);
00224             break;
00225         case MKTAG('N', 'A', 'M', 'E'):     /* Sample name chunk */
00226             get_meta(s, "title"    , size);
00227             break;
00228         case MKTAG('A', 'U', 'T', 'H'):     /* Author chunk */
00229             get_meta(s, "author"   , size);
00230             break;
00231         case MKTAG('(', 'c', ')', ' '):     /* Copyright chunk */
00232             get_meta(s, "copyright", size);
00233             break;
00234         case MKTAG('A', 'N', 'N', 'O'):     /* Annotation chunk */
00235             get_meta(s, "comment"  , size);
00236             break;
00237         case MKTAG('S', 'S', 'N', 'D'):     /* Sampled sound chunk */
00238             aiff->data_end = avio_tell(pb) + size;
00239             offset = avio_rb32(pb);      /* Offset of sound data */
00240             avio_rb32(pb);               /* BlockSize... don't care */
00241             offset += avio_tell(pb);    /* Compute absolute data offset */
00242             if (st->codec->block_align)    /* Assume COMM already parsed */
00243                 goto got_sound;
00244             if (!pb->seekable) {
00245                 av_log(s, AV_LOG_ERROR, "file is not seekable\n");
00246                 return -1;
00247             }
00248             avio_skip(pb, size - 8);
00249             break;
00250         case MKTAG('w', 'a', 'v', 'e'):
00251             if ((uint64_t)size > (1<<30))
00252                 return -1;
00253             st->codec->extradata = av_mallocz(size + FF_INPUT_BUFFER_PADDING_SIZE);
00254             if (!st->codec->extradata)
00255                 return AVERROR(ENOMEM);
00256             st->codec->extradata_size = size;
00257             avio_read(pb, st->codec->extradata, size);
00258             break;
00259         case MKTAG('C','H','A','N'):
00260             if (size < 12)
00261                 return AVERROR_INVALIDDATA;
00262             ff_mov_read_chan(s, size, st->codec);
00263             break;
00264         default: /* Jump */
00265             if (size & 1)   /* Always even aligned */
00266                 size++;
00267             avio_skip(pb, size);
00268         }
00269     }
00270 
00271     if (!st->codec->block_align) {
00272         av_log(s, AV_LOG_ERROR, "could not find COMM tag\n");
00273         return -1;
00274     }
00275 
00276 got_sound:
00277     /* Now positioned, get the sound data start and end */
00278     if (st->nb_frames)
00279         s->file_size = st->nb_frames * st->codec->block_align;
00280 
00281     av_set_pts_info(st, 64, 1, st->codec->sample_rate);
00282     st->start_time = 0;
00283     st->duration = st->codec->frame_size ?
00284         st->nb_frames * st->codec->frame_size : st->nb_frames;
00285 
00286     /* Position the stream at the first block */
00287     avio_seek(pb, offset, SEEK_SET);
00288 
00289     return 0;
00290 }
00291 
00292 #define MAX_SIZE 4096
00293 
00294 static int aiff_read_packet(AVFormatContext *s,
00295                             AVPacket *pkt)
00296 {
00297     AVStream *st = s->streams[0];
00298     AIFFInputContext *aiff = s->priv_data;
00299     int64_t max_size;
00300     int res, size;
00301 
00302     /* calculate size of remaining data */
00303     max_size = aiff->data_end - avio_tell(s->pb);
00304     if (max_size <= 0)
00305         return AVERROR_EOF;
00306 
00307     /* Now for that packet */
00308     if (st->codec->block_align >= 33) // GSM, QCLP, IMA4
00309         size = st->codec->block_align;
00310     else
00311         size = (MAX_SIZE / st->codec->block_align) * st->codec->block_align;
00312     size = FFMIN(max_size, size);
00313     res = av_get_packet(s->pb, pkt, size);
00314     if (res < 0)
00315         return res;
00316 
00317     /* Only one stream in an AIFF file */
00318     pkt->stream_index = 0;
00319     return 0;
00320 }
00321 
00322 AVInputFormat ff_aiff_demuxer = {
00323     "aiff",
00324     NULL_IF_CONFIG_SMALL("Audio IFF"),
00325     sizeof(AIFFInputContext),
00326     aiff_probe,
00327     aiff_read_header,
00328     aiff_read_packet,
00329     NULL,
00330     pcm_read_seek,
00331     .codec_tag= (const AVCodecTag* const []){ff_codec_aiff_tags, 0},
00332 };

Generated on Fri Feb 22 2013 07:24:31 for FFmpeg by  doxygen 1.7.1