[FFmpeg-devel] [PATCH] start FLAC decoding mid-stream

Michael Niedermayer michaelni
Mon Dec 8 13:55:44 CET 2008


On Sun, Dec 07, 2008 at 05:32:02PM -0500, Justin Ruggles wrote:
> Justin Ruggles wrote:
> > Hi,
> > 
> > As mentioned in another thread, our FLAC decoder cannot start decoding
> > mid-stream without having the STREAMINFO.  The FLAC format allows for
> > doing this as long as each frame header supplies all the necessary info.
> > 
> > Here is a patch to allow decoding starting anywhere in a FLAC stream.
> > 
> > Thanks,
> > Justin
> 
> Here is a slightly better version which only allocates buffers once if
> starting mid-stream.
> 
> -Justin
> 

> Index: libavcodec/flac.c
> ===================================================================
> --- libavcodec/flac.c	(revision 16030)
> +++ libavcodec/flac.c	(working copy)
> @@ -102,6 +102,11 @@ static av_cold int flac_decode_init(AVCodecContext
>      FLACContext *s = avctx->priv_data;
>      s->avctx = avctx;
>  
> +    s->min_blocksize = 16;
> +    s->max_blocksize = 32768;

> +    s->samplerate = -1;
> +    s->channels = -1;
> +    s->bps = -1;

is 0 for these not good enough?


>      if (avctx->extradata_size > 4) {
>          /* initialize based on the demuxer-supplied streamdata header */
>          if (avctx->extradata_size == FLAC_STREAMINFO_SIZE) {
> @@ -131,7 +136,7 @@ static void allocate_buffers(FLACContext *s){
>  
>      assert(s->max_blocksize);
>  
> -    if(s->max_framesize == 0 && s->max_blocksize){
> +    if(s->max_blocksize){
>          s->max_framesize= (s->channels * s->bps * s->max_blocksize + 7)/ 8; //FIXME header overhead
>      }
>  

why?


> @@ -495,12 +500,25 @@ static int decode_frame(FLACContext *s, int alloc_
>  {
>      int blocksize_code, sample_rate_code, sample_size_code, assignment, i, crc8;
>      int decorrelation, bps, blocksize, samplerate;
> +    int allocate_buffers = 0;
>  
>      blocksize_code = get_bits(&s->gb, 4);
>  
>      sample_rate_code = get_bits(&s->gb, 4);
>  
>      assignment = get_bits(&s->gb, 4); /* channel assignment */
> +    if (s->channels < 0)
> +    {
> +        if (assignment < 8)
> +        {
> +            s->channels = assignment + 1;
> +        }
> +        else
> +        {
> +            s->channels = 2;
> +        }
> +        allocate_buffers = 1;
> +    }

why is this under channels < 0 ?


[...]
> @@ -570,6 +605,12 @@ static int decode_frame(FLACContext *s, int alloc_
>          av_log(s->avctx, AV_LOG_ERROR, "illegal sample rate code %d\n", sample_rate_code);
>          return -1;
>      }
> +    if (s->samplerate < 0) {
> +        s->avctx->sample_rate = samplerate;

> +    } else if (samplerate != s->avctx->sample_rate) {
> +        av_log(s->avctx, AV_LOG_ERROR, "cannot change sample rate mid-stream\n");
> +        return -1;
> +    }
>  
>      skip_bits(&s->gb, 8);
>      crc8 = av_crc(av_crc_get_table(AV_CRC_8_ATM), 0,

what is the problem with it changing?


> @@ -584,6 +625,9 @@ static int decode_frame(FLACContext *s, int alloc_
>      s->bps          = bps;
>      s->decorrelation= decorrelation;
>  
> +    if (allocate_buffers)
> +        return 1;
> +
>  //    dump_headers(s->avctx, (FLACStreaminfo *)s);
>  
>      /* subframes */
> @@ -658,11 +702,17 @@ static int flac_decode_frame(AVCodecContext *avctx
>              goto end; // we may not have enough bits left to decode a frame, so try next time
>          }
>          skip_bits(&s->gb, 16);
> -        if (decode_frame(s, alloc_data_size) < 0){
> -            av_log(s->avctx, AV_LOG_ERROR, "decode_frame() failed\n");
> +        tmp = decode_frame(s, alloc_data_size);
> +        if (tmp) {
>              s->bitstream_size=0;
>              s->bitstream_index=0;

> +            if (tmp < 0) {
> +            av_log(s->avctx, AV_LOG_ERROR, "decode_frame() failed\n");

one of these 2 lines is not correctly indented

[...]
-- 
Michael     GnuPG fingerprint: 9FF2128B147EF6730BADF133611EC787040B0FAB

Observe your enemies, for they first find out your faults. -- Antisthenes
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 189 bytes
Desc: Digital signature
URL: <http://lists.mplayerhq.hu/pipermail/ffmpeg-devel/attachments/20081208/49d271a0/attachment.pgp>



More information about the ffmpeg-devel mailing list