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

libavcodec/flacdec.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 
00036 #include <limits.h>
00037 
00038 #include "libavutil/crc.h"
00039 #include "avcodec.h"
00040 #include "internal.h"
00041 #include "get_bits.h"
00042 #include "bytestream.h"
00043 #include "golomb.h"
00044 #include "flac.h"
00045 #include "flacdata.h"
00046 
00047 #undef NDEBUG
00048 #include <assert.h>
00049 
00050 typedef struct FLACContext {
00051     FLACSTREAMINFO
00052 
00053     AVCodecContext *avctx;                  
00054     GetBitContext gb;                       
00055 
00056     int blocksize;                          
00057     int curr_bps;                           
00058     int sample_shift;                       
00059     int is32;                               
00060     int ch_mode;                            
00061     int got_streaminfo;                     
00062 
00063     int32_t *decoded[FLAC_MAX_CHANNELS];    
00064     uint8_t *bitstream;
00065     unsigned int bitstream_size;
00066     unsigned int bitstream_index;
00067     unsigned int allocated_bitstream_size;
00068 } FLACContext;
00069 
00070 static const int sample_size_table[] =
00071 { 0, 8, 12, 0, 16, 20, 24, 0 };
00072 
00073 static int64_t get_utf8(GetBitContext *gb)
00074 {
00075     int64_t val;
00076     GET_UTF8(val, get_bits(gb, 8), return -1;)
00077     return val;
00078 }
00079 
00080 static void allocate_buffers(FLACContext *s);
00081 
00082 int ff_flac_is_extradata_valid(AVCodecContext *avctx,
00083                                enum FLACExtradataFormat *format,
00084                                uint8_t **streaminfo_start)
00085 {
00086     if (!avctx->extradata || avctx->extradata_size < FLAC_STREAMINFO_SIZE) {
00087         av_log(avctx, AV_LOG_ERROR, "extradata NULL or too small.\n");
00088         return 0;
00089     }
00090     if (AV_RL32(avctx->extradata) != MKTAG('f','L','a','C')) {
00091         /* extradata contains STREAMINFO only */
00092         if (avctx->extradata_size != FLAC_STREAMINFO_SIZE) {
00093             av_log(avctx, AV_LOG_WARNING, "extradata contains %d bytes too many.\n",
00094                    FLAC_STREAMINFO_SIZE-avctx->extradata_size);
00095         }
00096         *format = FLAC_EXTRADATA_FORMAT_STREAMINFO;
00097         *streaminfo_start = avctx->extradata;
00098     } else {
00099         if (avctx->extradata_size < 8+FLAC_STREAMINFO_SIZE) {
00100             av_log(avctx, AV_LOG_ERROR, "extradata too small.\n");
00101             return 0;
00102         }
00103         *format = FLAC_EXTRADATA_FORMAT_FULL_HEADER;
00104         *streaminfo_start = &avctx->extradata[8];
00105     }
00106     return 1;
00107 }
00108 
00109 static av_cold int flac_decode_init(AVCodecContext *avctx)
00110 {
00111     enum FLACExtradataFormat format;
00112     uint8_t *streaminfo;
00113     FLACContext *s = avctx->priv_data;
00114     s->avctx = avctx;
00115 
00116     avctx->sample_fmt = SAMPLE_FMT_S16;
00117 
00118     /* for now, the raw FLAC header is allowed to be passed to the decoder as
00119        frame data instead of extradata. */
00120     if (!avctx->extradata)
00121         return 0;
00122 
00123     if (!ff_flac_is_extradata_valid(avctx, &format, &streaminfo))
00124         return -1;
00125 
00126     /* initialize based on the demuxer-supplied streamdata header */
00127     ff_flac_parse_streaminfo(avctx, (FLACStreaminfo *)s, streaminfo);
00128     if (s->bps > 16)
00129         avctx->sample_fmt = SAMPLE_FMT_S32;
00130     else
00131         avctx->sample_fmt = SAMPLE_FMT_S16;
00132     allocate_buffers(s);
00133     s->got_streaminfo = 1;
00134 
00135     return 0;
00136 }
00137 
00138 static void dump_headers(AVCodecContext *avctx, FLACStreaminfo *s)
00139 {
00140     av_log(avctx, AV_LOG_DEBUG, "  Max Blocksize: %d\n", s->max_blocksize);
00141     av_log(avctx, AV_LOG_DEBUG, "  Max Framesize: %d\n", s->max_framesize);
00142     av_log(avctx, AV_LOG_DEBUG, "  Samplerate: %d\n", s->samplerate);
00143     av_log(avctx, AV_LOG_DEBUG, "  Channels: %d\n", s->channels);
00144     av_log(avctx, AV_LOG_DEBUG, "  Bits: %d\n", s->bps);
00145 }
00146 
00147 static void allocate_buffers(FLACContext *s)
00148 {
00149     int i;
00150 
00151     assert(s->max_blocksize);
00152 
00153     if (s->max_framesize == 0 && s->max_blocksize) {
00154         s->max_framesize = ff_flac_get_max_frame_size(s->max_blocksize,
00155                                                       s->channels, s->bps);
00156     }
00157 
00158     for (i = 0; i < s->channels; i++) {
00159         s->decoded[i] = av_realloc(s->decoded[i],
00160                                    sizeof(int32_t)*s->max_blocksize);
00161     }
00162 
00163     if (s->allocated_bitstream_size < s->max_framesize)
00164         s->bitstream= av_fast_realloc(s->bitstream,
00165                                       &s->allocated_bitstream_size,
00166                                       s->max_framesize);
00167 }
00168 
00169 void ff_flac_parse_streaminfo(AVCodecContext *avctx, struct FLACStreaminfo *s,
00170                               const uint8_t *buffer)
00171 {
00172     GetBitContext gb;
00173     init_get_bits(&gb, buffer, FLAC_STREAMINFO_SIZE*8);
00174 
00175     skip_bits(&gb, 16); /* skip min blocksize */
00176     s->max_blocksize = get_bits(&gb, 16);
00177     if (s->max_blocksize < FLAC_MIN_BLOCKSIZE) {
00178         av_log(avctx, AV_LOG_WARNING, "invalid max blocksize: %d\n",
00179                s->max_blocksize);
00180         s->max_blocksize = 16;
00181     }
00182 
00183     skip_bits(&gb, 24); /* skip min frame size */
00184     s->max_framesize = get_bits_long(&gb, 24);
00185 
00186     s->samplerate = get_bits_long(&gb, 20);
00187     s->channels = get_bits(&gb, 3) + 1;
00188     s->bps = get_bits(&gb, 5) + 1;
00189 
00190     avctx->channels = s->channels;
00191     avctx->sample_rate = s->samplerate;
00192     avctx->bits_per_raw_sample = s->bps;
00193 
00194     s->samples  = get_bits_long(&gb, 32) << 4;
00195     s->samples |= get_bits(&gb, 4);
00196 
00197     skip_bits_long(&gb, 64); /* md5 sum */
00198     skip_bits_long(&gb, 64); /* md5 sum */
00199 
00200     dump_headers(avctx, s);
00201 }
00202 
00203 void ff_flac_parse_block_header(const uint8_t *block_header,
00204                                 int *last, int *type, int *size)
00205 {
00206     int tmp = bytestream_get_byte(&block_header);
00207     if (last)
00208         *last = tmp & 0x80;
00209     if (type)
00210         *type = tmp & 0x7F;
00211     if (size)
00212         *size = bytestream_get_be24(&block_header);
00213 }
00214 
00222 static int parse_streaminfo(FLACContext *s, const uint8_t *buf, int buf_size)
00223 {
00224     int metadata_type, metadata_size;
00225 
00226     if (buf_size < FLAC_STREAMINFO_SIZE+8) {
00227         /* need more data */
00228         return 0;
00229     }
00230     ff_flac_parse_block_header(&buf[4], NULL, &metadata_type, &metadata_size);
00231     if (metadata_type != FLAC_METADATA_TYPE_STREAMINFO ||
00232         metadata_size != FLAC_STREAMINFO_SIZE) {
00233         return AVERROR_INVALIDDATA;
00234     }
00235     ff_flac_parse_streaminfo(s->avctx, (FLACStreaminfo *)s, &buf[8]);
00236     allocate_buffers(s);
00237     s->got_streaminfo = 1;
00238 
00239     return 0;
00240 }
00241 
00248 static int get_metadata_size(const uint8_t *buf, int buf_size)
00249 {
00250     int metadata_last, metadata_size;
00251     const uint8_t *buf_end = buf + buf_size;
00252 
00253     buf += 4;
00254     do {
00255         ff_flac_parse_block_header(buf, &metadata_last, NULL, &metadata_size);
00256         buf += 4;
00257         if (buf + metadata_size > buf_end) {
00258             /* need more data in order to read the complete header */
00259             return 0;
00260         }
00261         buf += metadata_size;
00262     } while (!metadata_last);
00263 
00264     return buf_size - (buf_end - buf);
00265 }
00266 
00267 static int decode_residuals(FLACContext *s, int channel, int pred_order)
00268 {
00269     int i, tmp, partition, method_type, rice_order;
00270     int sample = 0, samples;
00271 
00272     method_type = get_bits(&s->gb, 2);
00273     if (method_type > 1) {
00274         av_log(s->avctx, AV_LOG_ERROR, "illegal residual coding method %d\n",
00275                method_type);
00276         return -1;
00277     }
00278 
00279     rice_order = get_bits(&s->gb, 4);
00280 
00281     samples= s->blocksize >> rice_order;
00282     if (pred_order > samples) {
00283         av_log(s->avctx, AV_LOG_ERROR, "invalid predictor order: %i > %i\n",
00284                pred_order, samples);
00285         return -1;
00286     }
00287 
00288     sample=
00289     i= pred_order;
00290     for (partition = 0; partition < (1 << rice_order); partition++) {
00291         tmp = get_bits(&s->gb, method_type == 0 ? 4 : 5);
00292         if (tmp == (method_type == 0 ? 15 : 31)) {
00293             tmp = get_bits(&s->gb, 5);
00294             for (; i < samples; i++, sample++)
00295                 s->decoded[channel][sample] = get_sbits_long(&s->gb, tmp);
00296         } else {
00297             for (; i < samples; i++, sample++) {
00298                 s->decoded[channel][sample] = get_sr_golomb_flac(&s->gb, tmp, INT_MAX, 0);
00299             }
00300         }
00301         i= 0;
00302     }
00303 
00304     return 0;
00305 }
00306 
00307 static int decode_subframe_fixed(FLACContext *s, int channel, int pred_order)
00308 {
00309     const int blocksize = s->blocksize;
00310     int32_t *decoded = s->decoded[channel];
00311     int av_uninit(a), av_uninit(b), av_uninit(c), av_uninit(d), i;
00312 
00313     /* warm up samples */
00314     for (i = 0; i < pred_order; i++) {
00315         decoded[i] = get_sbits_long(&s->gb, s->curr_bps);
00316     }
00317 
00318     if (decode_residuals(s, channel, pred_order) < 0)
00319         return -1;
00320 
00321     if (pred_order > 0)
00322         a = decoded[pred_order-1];
00323     if (pred_order > 1)
00324         b = a - decoded[pred_order-2];
00325     if (pred_order > 2)
00326         c = b - decoded[pred_order-2] + decoded[pred_order-3];
00327     if (pred_order > 3)
00328         d = c - decoded[pred_order-2] + 2*decoded[pred_order-3] - decoded[pred_order-4];
00329 
00330     switch (pred_order) {
00331     case 0:
00332         break;
00333     case 1:
00334         for (i = pred_order; i < blocksize; i++)
00335             decoded[i] = a += decoded[i];
00336         break;
00337     case 2:
00338         for (i = pred_order; i < blocksize; i++)
00339             decoded[i] = a += b += decoded[i];
00340         break;
00341     case 3:
00342         for (i = pred_order; i < blocksize; i++)
00343             decoded[i] = a += b += c += decoded[i];
00344         break;
00345     case 4:
00346         for (i = pred_order; i < blocksize; i++)
00347             decoded[i] = a += b += c += d += decoded[i];
00348         break;
00349     default:
00350         av_log(s->avctx, AV_LOG_ERROR, "illegal pred order %d\n", pred_order);
00351         return -1;
00352     }
00353 
00354     return 0;
00355 }
00356 
00357 static int decode_subframe_lpc(FLACContext *s, int channel, int pred_order)
00358 {
00359     int i, j;
00360     int coeff_prec, qlevel;
00361     int coeffs[32];
00362     int32_t *decoded = s->decoded[channel];
00363 
00364     /* warm up samples */
00365     for (i = 0; i < pred_order; i++) {
00366         decoded[i] = get_sbits_long(&s->gb, s->curr_bps);
00367     }
00368 
00369     coeff_prec = get_bits(&s->gb, 4) + 1;
00370     if (coeff_prec == 16) {
00371         av_log(s->avctx, AV_LOG_ERROR, "invalid coeff precision\n");
00372         return -1;
00373     }
00374     qlevel = get_sbits(&s->gb, 5);
00375     if (qlevel < 0) {
00376         av_log(s->avctx, AV_LOG_ERROR, "qlevel %d not supported, maybe buggy stream\n",
00377                qlevel);
00378         return -1;
00379     }
00380 
00381     for (i = 0; i < pred_order; i++) {
00382         coeffs[i] = get_sbits(&s->gb, coeff_prec);
00383     }
00384 
00385     if (decode_residuals(s, channel, pred_order) < 0)
00386         return -1;
00387 
00388     if (s->bps > 16) {
00389         int64_t sum;
00390         for (i = pred_order; i < s->blocksize; i++) {
00391             sum = 0;
00392             for (j = 0; j < pred_order; j++)
00393                 sum += (int64_t)coeffs[j] * decoded[i-j-1];
00394             decoded[i] += sum >> qlevel;
00395         }
00396     } else {
00397         for (i = pred_order; i < s->blocksize-1; i += 2) {
00398             int c;
00399             int d = decoded[i-pred_order];
00400             int s0 = 0, s1 = 0;
00401             for (j = pred_order-1; j > 0; j--) {
00402                 c = coeffs[j];
00403                 s0 += c*d;
00404                 d = decoded[i-j];
00405                 s1 += c*d;
00406             }
00407             c = coeffs[0];
00408             s0 += c*d;
00409             d = decoded[i] += s0 >> qlevel;
00410             s1 += c*d;
00411             decoded[i+1] += s1 >> qlevel;
00412         }
00413         if (i < s->blocksize) {
00414             int sum = 0;
00415             for (j = 0; j < pred_order; j++)
00416                 sum += coeffs[j] * decoded[i-j-1];
00417             decoded[i] += sum >> qlevel;
00418         }
00419     }
00420 
00421     return 0;
00422 }
00423 
00424 static inline int decode_subframe(FLACContext *s, int channel)
00425 {
00426     int type, wasted = 0;
00427     int i, tmp;
00428 
00429     s->curr_bps = s->bps;
00430     if (channel == 0) {
00431         if (s->ch_mode == FLAC_CHMODE_RIGHT_SIDE)
00432             s->curr_bps++;
00433     } else {
00434         if (s->ch_mode == FLAC_CHMODE_LEFT_SIDE || s->ch_mode == FLAC_CHMODE_MID_SIDE)
00435             s->curr_bps++;
00436     }
00437 
00438     if (get_bits1(&s->gb)) {
00439         av_log(s->avctx, AV_LOG_ERROR, "invalid subframe padding\n");
00440         return -1;
00441     }
00442     type = get_bits(&s->gb, 6);
00443 
00444     if (get_bits1(&s->gb)) {
00445         wasted = 1;
00446         while (!get_bits1(&s->gb))
00447             wasted++;
00448         s->curr_bps -= wasted;
00449     }
00450     if (s->curr_bps > 32) {
00451         av_log_missing_feature(s->avctx, "decorrelated bit depth > 32", 0);
00452         return -1;
00453     }
00454 
00455 //FIXME use av_log2 for types
00456     if (type == 0) {
00457         tmp = get_sbits_long(&s->gb, s->curr_bps);
00458         for (i = 0; i < s->blocksize; i++)
00459             s->decoded[channel][i] = tmp;
00460     } else if (type == 1) {
00461         for (i = 0; i < s->blocksize; i++)
00462             s->decoded[channel][i] = get_sbits_long(&s->gb, s->curr_bps);
00463     } else if ((type >= 8) && (type <= 12)) {
00464         if (decode_subframe_fixed(s, channel, type & ~0x8) < 0)
00465             return -1;
00466     } else if (type >= 32) {
00467         if (decode_subframe_lpc(s, channel, (type & ~0x20)+1) < 0)
00468             return -1;
00469     } else {
00470         av_log(s->avctx, AV_LOG_ERROR, "invalid coding type\n");
00471         return -1;
00472     }
00473 
00474     if (wasted) {
00475         int i;
00476         for (i = 0; i < s->blocksize; i++)
00477             s->decoded[channel][i] <<= wasted;
00478     }
00479 
00480     return 0;
00481 }
00482 
00490 static int decode_frame_header(AVCodecContext *avctx, GetBitContext *gb,
00491                                FLACFrameInfo *fi)
00492 {
00493     int bs_code, sr_code, bps_code;
00494 
00495     /* frame sync code */
00496     skip_bits(gb, 16);
00497 
00498     /* block size and sample rate codes */
00499     bs_code = get_bits(gb, 4);
00500     sr_code = get_bits(gb, 4);
00501 
00502     /* channels and decorrelation */
00503     fi->ch_mode = get_bits(gb, 4);
00504     if (fi->ch_mode < FLAC_MAX_CHANNELS) {
00505         fi->channels = fi->ch_mode + 1;
00506         fi->ch_mode = FLAC_CHMODE_INDEPENDENT;
00507     } else if (fi->ch_mode <= FLAC_CHMODE_MID_SIDE) {
00508         fi->channels = 2;
00509     } else {
00510         av_log(avctx, AV_LOG_ERROR, "invalid channel mode: %d\n", fi->ch_mode);
00511         return -1;
00512     }
00513 
00514     /* bits per sample */
00515     bps_code = get_bits(gb, 3);
00516     if (bps_code == 3 || bps_code == 7) {
00517         av_log(avctx, AV_LOG_ERROR, "invalid sample size code (%d)\n",
00518                bps_code);
00519         return -1;
00520     }
00521     fi->bps = sample_size_table[bps_code];
00522 
00523     /* reserved bit */
00524     if (get_bits1(gb)) {
00525         av_log(avctx, AV_LOG_ERROR, "broken stream, invalid padding\n");
00526         return -1;
00527     }
00528 
00529     /* sample or frame count */
00530     if (get_utf8(gb) < 0) {
00531         av_log(avctx, AV_LOG_ERROR, "utf8 fscked\n");
00532         return -1;
00533     }
00534 
00535     /* blocksize */
00536     if (bs_code == 0) {
00537         av_log(avctx, AV_LOG_ERROR, "reserved blocksize code: 0\n");
00538         return -1;
00539     } else if (bs_code == 6) {
00540         fi->blocksize = get_bits(gb, 8) + 1;
00541     } else if (bs_code == 7) {
00542         fi->blocksize = get_bits(gb, 16) + 1;
00543     } else {
00544         fi->blocksize = ff_flac_blocksize_table[bs_code];
00545     }
00546 
00547     /* sample rate */
00548     if (sr_code < 12) {
00549         fi->samplerate = ff_flac_sample_rate_table[sr_code];
00550     } else if (sr_code == 12) {
00551         fi->samplerate = get_bits(gb, 8) * 1000;
00552     } else if (sr_code == 13) {
00553         fi->samplerate = get_bits(gb, 16);
00554     } else if (sr_code == 14) {
00555         fi->samplerate = get_bits(gb, 16) * 10;
00556     } else {
00557         av_log(avctx, AV_LOG_ERROR, "illegal sample rate code %d\n",
00558                sr_code);
00559         return -1;
00560     }
00561 
00562     /* header CRC-8 check */
00563     skip_bits(gb, 8);
00564     if (av_crc(av_crc_get_table(AV_CRC_8_ATM), 0, gb->buffer,
00565                get_bits_count(gb)/8)) {
00566         av_log(avctx, AV_LOG_ERROR, "header crc mismatch\n");
00567         return -1;
00568     }
00569 
00570     return 0;
00571 }
00572 
00573 static int decode_frame(FLACContext *s)
00574 {
00575     int i;
00576     GetBitContext *gb = &s->gb;
00577     FLACFrameInfo fi;
00578 
00579     if (decode_frame_header(s->avctx, gb, &fi)) {
00580         av_log(s->avctx, AV_LOG_ERROR, "invalid frame header\n");
00581         return -1;
00582     }
00583 
00584     if (fi.channels != s->channels) {
00585         av_log(s->avctx, AV_LOG_ERROR, "switching channel layout mid-stream "
00586                                        "is not supported\n");
00587         return -1;
00588     }
00589     s->ch_mode = fi.ch_mode;
00590 
00591     if (fi.bps && fi.bps != s->bps) {
00592         av_log(s->avctx, AV_LOG_ERROR, "switching bps mid-stream is not "
00593                                        "supported\n");
00594         return -1;
00595     }
00596     if (s->bps > 16) {
00597         s->avctx->sample_fmt = SAMPLE_FMT_S32;
00598         s->sample_shift = 32 - s->bps;
00599         s->is32 = 1;
00600     } else {
00601         s->avctx->sample_fmt = SAMPLE_FMT_S16;
00602         s->sample_shift = 16 - s->bps;
00603         s->is32 = 0;
00604     }
00605 
00606     if (fi.blocksize > s->max_blocksize) {
00607         av_log(s->avctx, AV_LOG_ERROR, "blocksize %d > %d\n", fi.blocksize,
00608                s->max_blocksize);
00609         return -1;
00610     }
00611     s->blocksize = fi.blocksize;
00612 
00613     if (fi.samplerate == 0) {
00614         fi.samplerate = s->samplerate;
00615     } else if (fi.samplerate != s->samplerate) {
00616         av_log(s->avctx, AV_LOG_WARNING, "sample rate changed from %d to %d\n",
00617                s->samplerate, fi.samplerate);
00618     }
00619     s->samplerate = s->avctx->sample_rate = fi.samplerate;
00620 
00621 //    dump_headers(s->avctx, (FLACStreaminfo *)s);
00622 
00623     /* subframes */
00624     for (i = 0; i < s->channels; i++) {
00625         if (decode_subframe(s, i) < 0)
00626             return -1;
00627     }
00628 
00629     align_get_bits(gb);
00630 
00631     /* frame footer */
00632     skip_bits(gb, 16); /* data crc */
00633 
00634     return 0;
00635 }
00636 
00637 static int flac_decode_frame(AVCodecContext *avctx,
00638                             void *data, int *data_size,
00639                             AVPacket *avpkt)
00640 {
00641     const uint8_t *buf = avpkt->data;
00642     int buf_size = avpkt->size;
00643     FLACContext *s = avctx->priv_data;
00644     int i, j = 0, input_buf_size = 0, bytes_read = 0;
00645     int16_t *samples_16 = data;
00646     int32_t *samples_32 = data;
00647     int alloc_data_size= *data_size;
00648     int output_size;
00649 
00650     *data_size=0;
00651 
00652     if (s->max_framesize == 0) {
00653         s->max_framesize= FFMAX(4, buf_size); // should hopefully be enough for the first header
00654         s->bitstream= av_fast_realloc(s->bitstream, &s->allocated_bitstream_size, s->max_framesize);
00655     }
00656 
00657     if (1 && s->max_framesize) { //FIXME truncated
00658         if (s->bitstream_size < 4 || AV_RL32(s->bitstream) != MKTAG('f','L','a','C'))
00659             buf_size= FFMIN(buf_size, s->max_framesize - FFMIN(s->bitstream_size, s->max_framesize));
00660         input_buf_size= buf_size;
00661 
00662         if (s->bitstream_size + buf_size < buf_size || s->bitstream_index + s->bitstream_size + buf_size < s->bitstream_index)
00663             return -1;
00664 
00665         if (s->allocated_bitstream_size < s->bitstream_size + buf_size)
00666             s->bitstream= av_fast_realloc(s->bitstream, &s->allocated_bitstream_size, s->bitstream_size + buf_size);
00667 
00668         if (s->bitstream_index + s->bitstream_size + buf_size > s->allocated_bitstream_size) {
00669             memmove(s->bitstream, &s->bitstream[s->bitstream_index],
00670                     s->bitstream_size);
00671             s->bitstream_index=0;
00672         }
00673         memcpy(&s->bitstream[s->bitstream_index + s->bitstream_size],
00674                buf, buf_size);
00675         buf= &s->bitstream[s->bitstream_index];
00676         buf_size += s->bitstream_size;
00677         s->bitstream_size= buf_size;
00678 
00679         if (buf_size < s->max_framesize && input_buf_size) {
00680             return input_buf_size;
00681         }
00682     }
00683 
00684     /* check that there is at least the smallest decodable amount of data.
00685        this amount corresponds to the smallest valid FLAC frame possible.
00686        FF F8 69 02 00 00 9A 00 00 34 46 */
00687     if (buf_size < 11)
00688         goto end;
00689 
00690     /* check for inline header */
00691     if (AV_RB32(buf) == MKBETAG('f','L','a','C')) {
00692         if (!s->got_streaminfo && parse_streaminfo(s, buf, buf_size)) {
00693             av_log(s->avctx, AV_LOG_ERROR, "invalid header\n");
00694             return -1;
00695         }
00696         bytes_read = get_metadata_size(buf, buf_size);
00697         goto end;
00698     }
00699 
00700     /* check for frame sync code and resync stream if necessary */
00701     if ((AV_RB16(buf) & 0xFFFE) != 0xFFF8) {
00702         const uint8_t *buf_end = buf + buf_size;
00703         av_log(s->avctx, AV_LOG_ERROR, "FRAME HEADER not here\n");
00704         while (buf+2 < buf_end && (AV_RB16(buf) & 0xFFFE) != 0xFFF8)
00705             buf++;
00706         bytes_read = buf_size - (buf_end - buf);
00707         goto end; // we may not have enough bits left to decode a frame, so try next time
00708     }
00709 
00710     /* decode frame */
00711     init_get_bits(&s->gb, buf, buf_size*8);
00712     if (decode_frame(s) < 0) {
00713         av_log(s->avctx, AV_LOG_ERROR, "decode_frame() failed\n");
00714         s->bitstream_size=0;
00715         s->bitstream_index=0;
00716         return -1;
00717     }
00718     bytes_read = (get_bits_count(&s->gb)+7)/8;
00719 
00720     /* check if allocated data size is large enough for output */
00721     output_size = s->blocksize * s->channels * (s->is32 ? 4 : 2);
00722     if (output_size > alloc_data_size) {
00723         av_log(s->avctx, AV_LOG_ERROR, "output data size is larger than "
00724                                        "allocated data size\n");
00725         goto end;
00726     }
00727     *data_size = output_size;
00728 
00729 #define DECORRELATE(left, right)\
00730             assert(s->channels == 2);\
00731             for (i = 0; i < s->blocksize; i++) {\
00732                 int a= s->decoded[0][i];\
00733                 int b= s->decoded[1][i];\
00734                 if (s->is32) {\
00735                     *samples_32++ = (left)  << s->sample_shift;\
00736                     *samples_32++ = (right) << s->sample_shift;\
00737                 } else {\
00738                     *samples_16++ = (left)  << s->sample_shift;\
00739                     *samples_16++ = (right) << s->sample_shift;\
00740                 }\
00741             }\
00742             break;
00743 
00744     switch (s->ch_mode) {
00745     case FLAC_CHMODE_INDEPENDENT:
00746         for (j = 0; j < s->blocksize; j++) {
00747             for (i = 0; i < s->channels; i++) {
00748                 if (s->is32)
00749                     *samples_32++ = s->decoded[i][j] << s->sample_shift;
00750                 else
00751                     *samples_16++ = s->decoded[i][j] << s->sample_shift;
00752             }
00753         }
00754         break;
00755     case FLAC_CHMODE_LEFT_SIDE:
00756         DECORRELATE(a,a-b)
00757     case FLAC_CHMODE_RIGHT_SIDE:
00758         DECORRELATE(a+b,b)
00759     case FLAC_CHMODE_MID_SIDE:
00760         DECORRELATE( (a-=b>>1) + b, a)
00761     }
00762 
00763 end:
00764     if (bytes_read > buf_size) {
00765         av_log(s->avctx, AV_LOG_ERROR, "overread: %d\n", bytes_read - buf_size);
00766         s->bitstream_size=0;
00767         s->bitstream_index=0;
00768         return -1;
00769     }
00770 
00771     if (s->bitstream_size) {
00772         s->bitstream_index += bytes_read;
00773         s->bitstream_size  -= bytes_read;
00774         return input_buf_size;
00775     } else
00776         return bytes_read;
00777 }
00778 
00779 static av_cold int flac_decode_close(AVCodecContext *avctx)
00780 {
00781     FLACContext *s = avctx->priv_data;
00782     int i;
00783 
00784     for (i = 0; i < s->channels; i++) {
00785         av_freep(&s->decoded[i]);
00786     }
00787     av_freep(&s->bitstream);
00788 
00789     return 0;
00790 }
00791 
00792 static void flac_flush(AVCodecContext *avctx)
00793 {
00794     FLACContext *s = avctx->priv_data;
00795 
00796     s->bitstream_size=
00797     s->bitstream_index= 0;
00798 }
00799 
00800 AVCodec flac_decoder = {
00801     "flac",
00802     AVMEDIA_TYPE_AUDIO,
00803     CODEC_ID_FLAC,
00804     sizeof(FLACContext),
00805     flac_decode_init,
00806     NULL,
00807     flac_decode_close,
00808     flac_decode_frame,
00809     CODEC_CAP_DELAY | CODEC_CAP_SUBFRAMES, /* FIXME: add a FLAC parser so that
00810                                               we will not need to use either
00811                                               of these capabilities */
00812     .flush= flac_flush,
00813     .long_name= NULL_IF_CONFIG_SMALL("FLAC (Free Lossless Audio Codec)"),
00814 };

Generated on Fri Sep 16 2011 17:17:36 for FFmpeg by  doxygen 1.7.1