flac.c

Go to the documentation of this file.
00001 /*
00002  * FLAC (Free Lossless Audio Codec) decoder
00003  * Copyright (c) 2003 Alex Beregszaszi
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 /**
00023  * @file flac.c
00024  * FLAC (Free Lossless Audio Codec) decoder
00025  * @author Alex Beregszaszi
00026  *
00027  * For more information on the FLAC format, visit:
00028  *  http://flac.sourceforge.net/
00029  *
00030  * This decoder can be used in 1 of 2 ways: Either raw FLAC data can be fed
00031  * through, starting from the initial 'fLaC' signature; or by passing the
00032  * 34-byte streaminfo structure through avctx->extradata[_size] followed
00033  * by data starting with the 0xFFF8 marker.
00034  */
00035 
00036 #include <limits.h>
00037 
00038 #define ALT_BITSTREAM_READER
00039 #include "libavutil/crc.h"
00040 #include "avcodec.h"
00041 #include "bitstream.h"
00042 #include "golomb.h"
00043 #include "flac.h"
00044 
00045 #undef NDEBUG
00046 #include <assert.h>
00047 
00048 #define MAX_CHANNELS 8
00049 #define MAX_BLOCKSIZE 65535
00050 #define FLAC_STREAMINFO_SIZE 34
00051 
00052 enum decorrelation_type {
00053     INDEPENDENT,
00054     LEFT_SIDE,
00055     RIGHT_SIDE,
00056     MID_SIDE,
00057 };
00058 
00059 typedef struct FLACContext {
00060     FLACSTREAMINFO
00061 
00062     AVCodecContext *avctx;
00063     GetBitContext gb;
00064 
00065     int blocksize/*, last_blocksize*/;
00066     int curr_bps;
00067     enum decorrelation_type decorrelation;
00068 
00069     int32_t *decoded[MAX_CHANNELS];
00070     uint8_t *bitstream;
00071     unsigned int bitstream_size;
00072     unsigned int bitstream_index;
00073     unsigned int allocated_bitstream_size;
00074 } FLACContext;
00075 
00076 #define METADATA_TYPE_STREAMINFO 0
00077 
00078 static const int sample_rate_table[] =
00079 { 0, 0, 0, 0,
00080   8000, 16000, 22050, 24000, 32000, 44100, 48000, 96000,
00081   0, 0, 0, 0 };
00082 
00083 static const int sample_size_table[] =
00084 { 0, 8, 12, 0, 16, 20, 24, 0 };
00085 
00086 static const int blocksize_table[] = {
00087      0,    192, 576<<0, 576<<1, 576<<2, 576<<3,      0,      0,
00088 256<<0, 256<<1, 256<<2, 256<<3, 256<<4, 256<<5, 256<<6, 256<<7
00089 };
00090 
00091 static int64_t get_utf8(GetBitContext *gb){
00092     int64_t val;
00093     GET_UTF8(val, get_bits(gb, 8), return -1;)
00094     return val;
00095 }
00096 
00097 static void allocate_buffers(FLACContext *s);
00098 static int metadata_parse(FLACContext *s);
00099 
00100 static av_cold int flac_decode_init(AVCodecContext * avctx)
00101 {
00102     FLACContext *s = avctx->priv_data;
00103     s->avctx = avctx;
00104 
00105     if (avctx->extradata_size > 4) {
00106         /* initialize based on the demuxer-supplied streamdata header */
00107         if (avctx->extradata_size == FLAC_STREAMINFO_SIZE) {
00108             ff_flac_parse_streaminfo(avctx, (FLACStreaminfo *)s, avctx->extradata);
00109             allocate_buffers(s);
00110         } else {
00111             init_get_bits(&s->gb, avctx->extradata, avctx->extradata_size*8);
00112             metadata_parse(s);
00113         }
00114     }
00115 
00116     return 0;
00117 }
00118 
00119 static void dump_headers(AVCodecContext *avctx, FLACStreaminfo *s)
00120 {
00121     av_log(avctx, AV_LOG_DEBUG, "  Blocksize: %d .. %d\n", s->min_blocksize, s->max_blocksize);
00122     av_log(avctx, AV_LOG_DEBUG, "  Max Framesize: %d\n", s->max_framesize);
00123     av_log(avctx, AV_LOG_DEBUG, "  Samplerate: %d\n", s->samplerate);
00124     av_log(avctx, AV_LOG_DEBUG, "  Channels: %d\n", s->channels);
00125     av_log(avctx, AV_LOG_DEBUG, "  Bits: %d\n", s->bps);
00126 }
00127 
00128 static void allocate_buffers(FLACContext *s){
00129     int i;
00130 
00131     assert(s->max_blocksize);
00132 
00133     if(s->max_framesize == 0 && s->max_blocksize){
00134         s->max_framesize= (s->channels * s->bps * s->max_blocksize + 7)/ 8; //FIXME header overhead
00135     }
00136 
00137     for (i = 0; i < s->channels; i++)
00138     {
00139         s->decoded[i] = av_realloc(s->decoded[i], sizeof(int32_t)*s->max_blocksize);
00140     }
00141 
00142     if(s->allocated_bitstream_size < s->max_framesize)
00143         s->bitstream= av_fast_realloc(s->bitstream, &s->allocated_bitstream_size, s->max_framesize);
00144 }
00145 
00146 void ff_flac_parse_streaminfo(AVCodecContext *avctx, struct FLACStreaminfo *s,
00147                               const uint8_t *buffer)
00148 {
00149     GetBitContext gb;
00150     init_get_bits(&gb, buffer, FLAC_STREAMINFO_SIZE*8);
00151 
00152     /* mandatory streaminfo */
00153     s->min_blocksize = get_bits(&gb, 16);
00154     s->max_blocksize = get_bits(&gb, 16);
00155 
00156     skip_bits(&gb, 24); /* skip min frame size */
00157     s->max_framesize = get_bits_long(&gb, 24);
00158 
00159     s->samplerate = get_bits_long(&gb, 20);
00160     s->channels = get_bits(&gb, 3) + 1;
00161     s->bps = get_bits(&gb, 5) + 1;
00162 
00163     avctx->channels = s->channels;
00164     avctx->sample_rate = s->samplerate;
00165 
00166     skip_bits(&gb, 36); /* total num of samples */
00167 
00168     skip_bits(&gb, 64); /* md5 sum */
00169     skip_bits(&gb, 64); /* md5 sum */
00170 
00171     dump_headers(avctx, s);
00172 }
00173 
00174 /**
00175  * Parse a list of metadata blocks. This list of blocks must begin with
00176  * the fLaC marker.
00177  * @param s the flac decoding context containing the gb bit reader used to
00178  *          parse metadata
00179  * @return 1 if some metadata was read, 0 if no fLaC marker was found
00180  */
00181 static int metadata_parse(FLACContext *s)
00182 {
00183     int i, metadata_last, metadata_type, metadata_size, streaminfo_updated=0;
00184     int initial_pos= get_bits_count(&s->gb);
00185 
00186     if (show_bits_long(&s->gb, 32) == MKBETAG('f','L','a','C')) {
00187         skip_bits(&s->gb, 32);
00188 
00189         av_log(s->avctx, AV_LOG_DEBUG, "STREAM HEADER\n");
00190         do {
00191             metadata_last = get_bits1(&s->gb);
00192             metadata_type = get_bits(&s->gb, 7);
00193             metadata_size = get_bits_long(&s->gb, 24);
00194 
00195             if(get_bits_count(&s->gb) + 8*metadata_size > s->gb.size_in_bits){
00196                 skip_bits_long(&s->gb, initial_pos - get_bits_count(&s->gb));
00197                 break;
00198             }
00199 
00200             av_log(s->avctx, AV_LOG_DEBUG,
00201                    " metadata block: flag = %d, type = %d, size = %d\n",
00202                    metadata_last, metadata_type, metadata_size);
00203             if (metadata_size) {
00204                 switch (metadata_type) {
00205                 case METADATA_TYPE_STREAMINFO:
00206                     ff_flac_parse_streaminfo(s->avctx, (FLACStreaminfo *)s, s->gb.buffer+get_bits_count(&s->gb)/8);
00207                     streaminfo_updated = 1;
00208 
00209                 default:
00210                     for (i=0; i<metadata_size; i++)
00211                         skip_bits(&s->gb, 8);
00212                 }
00213             }
00214         } while (!metadata_last);
00215 
00216         if (streaminfo_updated)
00217             allocate_buffers(s);
00218         return 1;
00219     }
00220     return 0;
00221 }
00222 
00223 static int decode_residuals(FLACContext *s, int channel, int pred_order)
00224 {
00225     int i, tmp, partition, method_type, rice_order;
00226     int sample = 0, samples;
00227 
00228     method_type = get_bits(&s->gb, 2);
00229     if (method_type > 1){
00230         av_log(s->avctx, AV_LOG_DEBUG, "illegal residual coding method %d\n", method_type);
00231         return -1;
00232     }
00233 
00234     rice_order = get_bits(&s->gb, 4);
00235 
00236     samples= s->blocksize >> rice_order;
00237     if (pred_order > samples) {
00238         av_log(s->avctx, AV_LOG_ERROR, "invalid predictor order: %i > %i\n", pred_order, samples);
00239         return -1;
00240     }
00241 
00242     sample=
00243     i= pred_order;
00244     for (partition = 0; partition < (1 << rice_order); partition++)
00245     {
00246         tmp = get_bits(&s->gb, method_type == 0 ? 4 : 5);
00247         if (tmp == (method_type == 0 ? 15 : 31))
00248         {
00249             av_log(s->avctx, AV_LOG_DEBUG, "fixed len partition\n");
00250             tmp = get_bits(&s->gb, 5);
00251             for (; i < samples; i++, sample++)
00252                 s->decoded[channel][sample] = get_sbits(&s->gb, tmp);
00253         }
00254         else
00255         {
00256 //            av_log(s->avctx, AV_LOG_DEBUG, "rice coded partition k=%d\n", tmp);
00257             for (; i < samples; i++, sample++){
00258                 s->decoded[channel][sample] = get_sr_golomb_flac(&s->gb, tmp, INT_MAX, 0);
00259             }
00260         }
00261         i= 0;
00262     }
00263 
00264 //    av_log(s->avctx, AV_LOG_DEBUG, "partitions: %d, samples: %d\n", 1 << rice_order, sample);
00265 
00266     return 0;
00267 }
00268 
00269 static int decode_subframe_fixed(FLACContext *s, int channel, int pred_order)
00270 {
00271     const int blocksize = s->blocksize;
00272     int32_t *decoded = s->decoded[channel];
00273     int a, b, c, d, i;
00274 
00275 //    av_log(s->avctx, AV_LOG_DEBUG, "  SUBFRAME FIXED\n");
00276 
00277     /* warm up samples */
00278 //    av_log(s->avctx, AV_LOG_DEBUG, "   warm up samples: %d\n", pred_order);
00279 
00280     for (i = 0; i < pred_order; i++)
00281     {
00282         decoded[i] = get_sbits(&s->gb, s->curr_bps);
00283 //        av_log(s->avctx, AV_LOG_DEBUG, "    %d: %d\n", i, s->decoded[channel][i]);
00284     }
00285 
00286     if (decode_residuals(s, channel, pred_order) < 0)
00287         return -1;
00288 
00289     if(pred_order > 0)
00290         a = decoded[pred_order-1];
00291     if(pred_order > 1)
00292         b = a - decoded[pred_order-2];
00293     if(pred_order > 2)
00294         c = b - decoded[pred_order-2] + decoded[pred_order-3];
00295     if(pred_order > 3)
00296         d = c - decoded[pred_order-2] + 2*decoded[pred_order-3] - decoded[pred_order-4];
00297 
00298     switch(pred_order)
00299     {
00300         case 0:
00301             break;
00302         case 1:
00303             for (i = pred_order; i < blocksize; i++)
00304                 decoded[i] = a += decoded[i];
00305             break;
00306         case 2:
00307             for (i = pred_order; i < blocksize; i++)
00308                 decoded[i] = a += b += decoded[i];
00309             break;
00310         case 3:
00311             for (i = pred_order; i < blocksize; i++)
00312                 decoded[i] = a += b += c += decoded[i];
00313             break;
00314         case 4:
00315             for (i = pred_order; i < blocksize; i++)
00316                 decoded[i] = a += b += c += d += decoded[i];
00317             break;
00318         default:
00319             av_log(s->avctx, AV_LOG_ERROR, "illegal pred order %d\n", pred_order);
00320             return -1;
00321     }
00322 
00323     return 0;
00324 }
00325 
00326 static int decode_subframe_lpc(FLACContext *s, int channel, int pred_order)
00327 {
00328     int i, j;
00329     int coeff_prec, qlevel;
00330     int coeffs[pred_order];
00331     int32_t *decoded = s->decoded[channel];
00332 
00333 //    av_log(s->avctx, AV_LOG_DEBUG, "  SUBFRAME LPC\n");
00334 
00335     /* warm up samples */
00336 //    av_log(s->avctx, AV_LOG_DEBUG, "   warm up samples: %d\n", pred_order);
00337 
00338     for (i = 0; i < pred_order; i++)
00339     {
00340         decoded[i] = get_sbits(&s->gb, s->curr_bps);
00341 //        av_log(s->avctx, AV_LOG_DEBUG, "    %d: %d\n", i, decoded[i]);
00342     }
00343 
00344     coeff_prec = get_bits(&s->gb, 4) + 1;
00345     if (coeff_prec == 16)
00346     {
00347         av_log(s->avctx, AV_LOG_DEBUG, "invalid coeff precision\n");
00348         return -1;
00349     }
00350 //    av_log(s->avctx, AV_LOG_DEBUG, "   qlp coeff prec: %d\n", coeff_prec);
00351     qlevel = get_sbits(&s->gb, 5);
00352 //    av_log(s->avctx, AV_LOG_DEBUG, "   quant level: %d\n", qlevel);
00353     if(qlevel < 0){
00354         av_log(s->avctx, AV_LOG_DEBUG, "qlevel %d not supported, maybe buggy stream\n", qlevel);
00355         return -1;
00356     }
00357 
00358     for (i = 0; i < pred_order; i++)
00359     {
00360         coeffs[i] = get_sbits(&s->gb, coeff_prec);
00361 //        av_log(s->avctx, AV_LOG_DEBUG, "    %d: %d\n", i, coeffs[i]);
00362     }
00363 
00364     if (decode_residuals(s, channel, pred_order) < 0)
00365         return -1;
00366 
00367     if (s->bps > 16) {
00368         int64_t sum;
00369         for (i = pred_order; i < s->blocksize; i++)
00370         {
00371             sum = 0;
00372             for (j = 0; j < pred_order; j++)
00373                 sum += (int64_t)coeffs[j] * decoded[i-j-1];
00374             decoded[i] += sum >> qlevel;
00375         }
00376     } else {
00377         for (i = pred_order; i < s->blocksize-1; i += 2)
00378         {
00379             int c;
00380             int d = decoded[i-pred_order];
00381             int s0 = 0, s1 = 0;
00382             for (j = pred_order-1; j > 0; j--)
00383             {
00384                 c = coeffs[j];
00385                 s0 += c*d;
00386                 d = decoded[i-j];
00387                 s1 += c*d;
00388             }
00389             c = coeffs[0];
00390             s0 += c*d;
00391             d = decoded[i] += s0 >> qlevel;
00392             s1 += c*d;
00393             decoded[i+1] += s1 >> qlevel;
00394         }
00395         if (i < s->blocksize)
00396         {
00397             int sum = 0;
00398             for (j = 0; j < pred_order; j++)
00399                 sum += coeffs[j] * decoded[i-j-1];
00400             decoded[i] += sum >> qlevel;
00401         }
00402     }
00403 
00404     return 0;
00405 }
00406 
00407 static inline int decode_subframe(FLACContext *s, int channel)
00408 {
00409     int type, wasted = 0;
00410     int i, tmp;
00411 
00412     s->curr_bps = s->bps;
00413     if(channel == 0){
00414         if(s->decorrelation == RIGHT_SIDE)
00415             s->curr_bps++;
00416     }else{
00417         if(s->decorrelation == LEFT_SIDE || s->decorrelation == MID_SIDE)
00418             s->curr_bps++;
00419     }
00420 
00421     if (get_bits1(&s->gb))
00422     {
00423         av_log(s->avctx, AV_LOG_ERROR, "invalid subframe padding\n");
00424         return -1;
00425     }
00426     type = get_bits(&s->gb, 6);
00427 //    wasted = get_bits1(&s->gb);
00428 
00429 //    if (wasted)
00430 //    {
00431 //        while (!get_bits1(&s->gb))
00432 //            wasted++;
00433 //        if (wasted)
00434 //            wasted++;
00435 //        s->curr_bps -= wasted;
00436 //    }
00437 #if 0
00438     wasted= 16 - av_log2(show_bits(&s->gb, 17));
00439     skip_bits(&s->gb, wasted+1);
00440     s->curr_bps -= wasted;
00441 #else
00442     if (get_bits1(&s->gb))
00443     {
00444         wasted = 1;
00445         while (!get_bits1(&s->gb))
00446             wasted++;
00447         s->curr_bps -= wasted;
00448         av_log(s->avctx, AV_LOG_DEBUG, "%d wasted bits\n", wasted);
00449     }
00450 #endif
00451 //FIXME use av_log2 for types
00452     if (type == 0)
00453     {
00454         av_log(s->avctx, AV_LOG_DEBUG, "coding type: constant\n");
00455         tmp = get_sbits(&s->gb, s->curr_bps);
00456         for (i = 0; i < s->blocksize; i++)
00457             s->decoded[channel][i] = tmp;
00458     }
00459     else if (type == 1)
00460     {
00461         av_log(s->avctx, AV_LOG_DEBUG, "coding type: verbatim\n");
00462         for (i = 0; i < s->blocksize; i++)
00463             s->decoded[channel][i] = get_sbits(&s->gb, s->curr_bps);
00464     }
00465     else if ((type >= 8) && (type <= 12))
00466     {
00467 //        av_log(s->avctx, AV_LOG_DEBUG, "coding type: fixed\n");
00468         if (decode_subframe_fixed(s, channel, type & ~0x8) < 0)
00469             return -1;
00470     }
00471     else if (type >= 32)
00472     {
00473 //        av_log(s->avctx, AV_LOG_DEBUG, "coding type: lpc\n");
00474         if (decode_subframe_lpc(s, channel, (type & ~0x20)+1) < 0)
00475             return -1;
00476     }
00477     else
00478     {
00479         av_log(s->avctx, AV_LOG_ERROR, "invalid coding type\n");
00480         return -1;
00481     }
00482 
00483     if (wasted)
00484     {
00485         int i;
00486         for (i = 0; i < s->blocksize; i++)
00487             s->decoded[channel][i] <<= wasted;
00488     }
00489 
00490     return 0;
00491 }
00492 
00493 static int decode_frame(FLACContext *s, int alloc_data_size)
00494 {
00495     int blocksize_code, sample_rate_code, sample_size_code, assignment, i, crc8;
00496     int decorrelation, bps, blocksize, samplerate;
00497 
00498     blocksize_code = get_bits(&s->gb, 4);
00499 
00500     sample_rate_code = get_bits(&s->gb, 4);
00501 
00502     assignment = get_bits(&s->gb, 4); /* channel assignment */
00503     if (assignment < 8 && s->channels == assignment+1)
00504         decorrelation = INDEPENDENT;
00505     else if (assignment >=8 && assignment < 11 && s->channels == 2)
00506         decorrelation = LEFT_SIDE + assignment - 8;
00507     else
00508     {
00509         av_log(s->avctx, AV_LOG_ERROR, "unsupported channel assignment %d (channels=%d)\n", assignment, s->channels);
00510         return -1;
00511     }
00512 
00513     sample_size_code = get_bits(&s->gb, 3);
00514     if(sample_size_code == 0)
00515         bps= s->bps;
00516     else if((sample_size_code != 3) && (sample_size_code != 7))
00517         bps = sample_size_table[sample_size_code];
00518     else
00519     {
00520         av_log(s->avctx, AV_LOG_ERROR, "invalid sample size code (%d)\n", sample_size_code);
00521         return -1;
00522     }
00523 
00524     if (get_bits1(&s->gb))
00525     {
00526         av_log(s->avctx, AV_LOG_ERROR, "broken stream, invalid padding\n");
00527         return -1;
00528     }
00529 
00530     if(get_utf8(&s->gb) < 0){
00531         av_log(s->avctx, AV_LOG_ERROR, "utf8 fscked\n");
00532         return -1;
00533     }
00534 #if 0
00535     if (/*((blocksize_code == 6) || (blocksize_code == 7)) &&*/
00536         (s->min_blocksize != s->max_blocksize)){
00537     }else{
00538     }
00539 #endif
00540 
00541     if (blocksize_code == 0)
00542         blocksize = s->min_blocksize;
00543     else if (blocksize_code == 6)
00544         blocksize = get_bits(&s->gb, 8)+1;
00545     else if (blocksize_code == 7)
00546         blocksize = get_bits(&s->gb, 16)+1;
00547     else
00548         blocksize = blocksize_table[blocksize_code];
00549 
00550     if(blocksize > s->max_blocksize){
00551         av_log(s->avctx, AV_LOG_ERROR, "blocksize %d > %d\n", blocksize, s->max_blocksize);
00552         return -1;
00553     }
00554 
00555     if(blocksize * s->channels * sizeof(int16_t) > alloc_data_size)
00556         return -1;
00557 
00558     if (sample_rate_code == 0){
00559         samplerate= s->samplerate;
00560     }else if ((sample_rate_code > 3) && (sample_rate_code < 12))
00561         samplerate = sample_rate_table[sample_rate_code];
00562     else if (sample_rate_code == 12)
00563         samplerate = get_bits(&s->gb, 8) * 1000;
00564     else if (sample_rate_code == 13)
00565         samplerate = get_bits(&s->gb, 16);
00566     else if (sample_rate_code == 14)
00567         samplerate = get_bits(&s->gb, 16) * 10;
00568     else{
00569         av_log(s->avctx, AV_LOG_ERROR, "illegal sample rate code %d\n", sample_rate_code);
00570         return -1;
00571     }
00572 
00573     skip_bits(&s->gb, 8);
00574     crc8 = av_crc(av_crc_get_table(AV_CRC_8_ATM), 0,
00575                   s->gb.buffer, get_bits_count(&s->gb)/8);
00576     if(crc8){
00577         av_log(s->avctx, AV_LOG_ERROR, "header crc mismatch crc=%2X\n", crc8);
00578         return -1;
00579     }
00580 
00581     s->blocksize    = blocksize;
00582     s->samplerate   = samplerate;
00583     s->bps          = bps;
00584     s->decorrelation= decorrelation;
00585 
00586 //    dump_headers(s->avctx, (FLACStreaminfo *)s);
00587 
00588     /* subframes */
00589     for (i = 0; i < s->channels; i++)
00590     {
00591 //        av_log(s->avctx, AV_LOG_DEBUG, "decoded: %x residual: %x\n", s->decoded[i], s->residual[i]);
00592         if (decode_subframe(s, i) < 0)
00593             return -1;
00594     }
00595 
00596     align_get_bits(&s->gb);
00597 
00598     /* frame footer */
00599     skip_bits(&s->gb, 16); /* data crc */
00600 
00601     return 0;
00602 }
00603 
00604 static int flac_decode_frame(AVCodecContext *avctx,
00605                             void *data, int *data_size,
00606                             const uint8_t *buf, int buf_size)
00607 {
00608     FLACContext *s = avctx->priv_data;
00609     int tmp = 0, i, j = 0, input_buf_size = 0;
00610     int16_t *samples = data;
00611     int alloc_data_size= *data_size;
00612 
00613     *data_size=0;
00614 
00615     if(s->max_framesize == 0){
00616         s->max_framesize= 65536; // should hopefully be enough for the first header
00617         s->bitstream= av_fast_realloc(s->bitstream, &s->allocated_bitstream_size, s->max_framesize);
00618     }
00619 
00620     if(1 && s->max_framesize){//FIXME truncated
00621             if(s->bitstream_size < 4 || AV_RL32(s->bitstream) != MKTAG('f','L','a','C'))
00622                 buf_size= FFMIN(buf_size, s->max_framesize - FFMIN(s->bitstream_size, s->max_framesize));
00623             input_buf_size= buf_size;
00624 
00625             if(s->bitstream_size + buf_size < buf_size || s->bitstream_index + s->bitstream_size + buf_size < s->bitstream_index)
00626                 return -1;
00627 
00628             if(s->allocated_bitstream_size < s->bitstream_size + buf_size)
00629                 s->bitstream= av_fast_realloc(s->bitstream, &s->allocated_bitstream_size, s->bitstream_size + buf_size);
00630 
00631             if(s->bitstream_index + s->bitstream_size + buf_size > s->allocated_bitstream_size){
00632 //                printf("memmove\n");
00633                 memmove(s->bitstream, &s->bitstream[s->bitstream_index], s->bitstream_size);
00634                 s->bitstream_index=0;
00635             }
00636             memcpy(&s->bitstream[s->bitstream_index + s->bitstream_size], buf, buf_size);
00637             buf= &s->bitstream[s->bitstream_index];
00638             buf_size += s->bitstream_size;
00639             s->bitstream_size= buf_size;
00640 
00641             if(buf_size < s->max_framesize && input_buf_size){
00642 //                printf("wanna more data ...\n");
00643                 return input_buf_size;
00644             }
00645     }
00646 
00647     init_get_bits(&s->gb, buf, buf_size*8);
00648 
00649     if(metadata_parse(s))
00650         goto end;
00651 
00652         tmp = show_bits(&s->gb, 16);
00653         if((tmp & 0xFFFE) != 0xFFF8){
00654             av_log(s->avctx, AV_LOG_ERROR, "FRAME HEADER not here\n");
00655             while(get_bits_count(&s->gb)/8+2 < buf_size && (show_bits(&s->gb, 16) & 0xFFFE) != 0xFFF8)
00656                 skip_bits(&s->gb, 8);
00657             goto end; // we may not have enough bits left to decode a frame, so try next time
00658         }
00659         skip_bits(&s->gb, 16);
00660         if (decode_frame(s, alloc_data_size) < 0){
00661             av_log(s->avctx, AV_LOG_ERROR, "decode_frame() failed\n");
00662             s->bitstream_size=0;
00663             s->bitstream_index=0;
00664             return -1;
00665         }
00666 
00667 
00668 #if 0
00669     /* fix the channel order here */
00670     if (s->order == MID_SIDE)
00671     {
00672         short *left = samples;
00673         short *right = samples + s->blocksize;
00674         for (i = 0; i < s->blocksize; i += 2)
00675         {
00676             uint32_t x = s->decoded[0][i];
00677             uint32_t y = s->decoded[0][i+1];
00678 
00679             right[i] = x - (y / 2);
00680             left[i] = right[i] + y;
00681         }
00682         *data_size = 2 * s->blocksize;
00683     }
00684     else
00685     {
00686     for (i = 0; i < s->channels; i++)
00687     {
00688         switch(s->order)
00689         {
00690             case INDEPENDENT:
00691                 for (j = 0; j < s->blocksize; j++)
00692                     samples[(s->blocksize*i)+j] = s->decoded[i][j];
00693                 break;
00694             case LEFT_SIDE:
00695             case RIGHT_SIDE:
00696                 if (i == 0)
00697                     for (j = 0; j < s->blocksize; j++)
00698                         samples[(s->blocksize*i)+j] = s->decoded[0][j];
00699                 else
00700                     for (j = 0; j < s->blocksize; j++)
00701                         samples[(s->blocksize*i)+j] = s->decoded[0][j] - s->decoded[i][j];
00702                 break;
00703 //            case MID_SIDE:
00704 //                av_log(s->avctx, AV_LOG_DEBUG, "mid-side unsupported\n");
00705         }
00706         *data_size += s->blocksize;
00707     }
00708     }
00709 #else
00710 #define DECORRELATE(left, right)\
00711             assert(s->channels == 2);\
00712             for (i = 0; i < s->blocksize; i++)\
00713             {\
00714                 int a= s->decoded[0][i];\
00715                 int b= s->decoded[1][i];\
00716                 *samples++ = ((left)  << (24 - s->bps)) >> 8;\
00717                 *samples++ = ((right) << (24 - s->bps)) >> 8;\
00718             }\
00719             break;
00720 
00721     switch(s->decorrelation)
00722     {
00723         case INDEPENDENT:
00724             for (j = 0; j < s->blocksize; j++)
00725             {
00726                 for (i = 0; i < s->channels; i++)
00727                     *samples++ = (s->decoded[i][j] << (24 - s->bps)) >> 8;
00728             }
00729             break;
00730         case LEFT_SIDE:
00731             DECORRELATE(a,a-b)
00732         case RIGHT_SIDE:
00733             DECORRELATE(a+b,b)
00734         case MID_SIDE:
00735             DECORRELATE( (a-=b>>1) + b, a)
00736     }
00737 #endif
00738 
00739     *data_size = (int8_t *)samples - (int8_t *)data;
00740 //    av_log(s->avctx, AV_LOG_DEBUG, "data size: %d\n", *data_size);
00741 
00742 //    s->last_blocksize = s->blocksize;
00743 end:
00744     i= (get_bits_count(&s->gb)+7)/8;
00745     if(i > buf_size){
00746         av_log(s->avctx, AV_LOG_ERROR, "overread: %d\n", i - buf_size);
00747         s->bitstream_size=0;
00748         s->bitstream_index=0;
00749         return -1;
00750     }
00751 
00752     if(s->bitstream_size){
00753         s->bitstream_index += i;
00754         s->bitstream_size  -= i;
00755         return input_buf_size;
00756     }else
00757         return i;
00758 }
00759 
00760 static av_cold int flac_decode_close(AVCodecContext *avctx)
00761 {
00762     FLACContext *s = avctx->priv_data;
00763     int i;
00764 
00765     for (i = 0; i < s->channels; i++)
00766     {
00767         av_freep(&s->decoded[i]);
00768     }
00769     av_freep(&s->bitstream);
00770 
00771     return 0;
00772 }
00773 
00774 static void flac_flush(AVCodecContext *avctx){
00775     FLACContext *s = avctx->priv_data;
00776 
00777     s->bitstream_size=
00778     s->bitstream_index= 0;
00779 }
00780 
00781 AVCodec flac_decoder = {
00782     "flac",
00783     CODEC_TYPE_AUDIO,
00784     CODEC_ID_FLAC,
00785     sizeof(FLACContext),
00786     flac_decode_init,
00787     NULL,
00788     flac_decode_close,
00789     flac_decode_frame,
00790     CODEC_CAP_DELAY,
00791     .flush= flac_flush,
00792     .long_name= NULL_IF_CONFIG_SMALL("FLAC (Free Lossless Audio Codec)"),
00793 };

Generated on Wed Nov 19 16:44:37 2008 for libextractor by  doxygen 1.5.1