Revision ac2570a8

View differences:

libavcodec/Makefile
19 19
      vp3.o asv1.o 4xm.o cabac.o ffv1.o ra144.o ra288.o vcr1.o cljr.o \
20 20
      roqvideo.o dpcm.o interplayvideo.o xan.o rpza.o cinepak.o msrle.o \
21 21
      msvideo1.o vqavideo.o idcinvideo.o adx.o rational.o faandct.o 8bps.o \
22
      smc.o parser.o flicvideo.o truemotion1.o vmdav.o lcl.o qtrle.o g726.o
22
      smc.o parser.o flicvideo.o truemotion1.o vmdav.o lcl.o qtrle.o g726.o \
23
      flac.o
23 24

  
24 25
ifeq ($(AMR_NB),yes)
25 26
ifeq ($(AMR_NB_FIXED),yes)
libavcodec/allcodecs.c
154 154
    register_avcodec(&interplay_dpcm_decoder);
155 155
    register_avcodec(&xan_dpcm_decoder);
156 156
    register_avcodec(&qtrle_decoder);
157
    register_avcodec(&flac_decoder);
157 158
#endif /* CONFIG_DECODERS */
158 159

  
159 160
#ifdef AMR_NB
libavcodec/avcodec.h
17 17

  
18 18
#define FFMPEG_VERSION_INT     0x000408
19 19
#define FFMPEG_VERSION         "0.4.8"
20
#define LIBAVCODEC_BUILD       4702
20
#define LIBAVCODEC_BUILD       4703
21 21

  
22 22
#define LIBAVCODEC_VERSION_INT FFMPEG_VERSION_INT
23 23
#define LIBAVCODEC_VERSION     FFMPEG_VERSION
......
132 132
    CODEC_ID_INTERPLAY_DPCM,
133 133
    CODEC_ID_XAN_DPCM,
134 134
    
135
    CODEC_ID_FLAC,
136
    
135 137
    CODEC_ID_MPEG2TS, /* _FAKE_ codec to indicate a raw MPEG2 transport
136 138
                         stream (only used by libavformat) */
137 139
};
......
623 625
     */
624 626
    int width, height;
625 627
    
626
#define FF_ASPECT_SQUARE 1
627
#define FF_ASPECT_4_3_625 2
628
#define FF_ASPECT_4_3_525 3
629
#define FF_ASPECT_16_9_625 4
630
#define FF_ASPECT_16_9_525 5
631 628
#define FF_ASPECT_EXTENDED 15
632 629

  
633 630
    /**
......
1729 1726
extern AVCodec interplay_dpcm_decoder;
1730 1727
extern AVCodec xan_dpcm_decoder;
1731 1728
extern AVCodec qtrle_decoder;
1729
extern AVCodec flac_decoder;
1732 1730

  
1733 1731
/* pcm codecs */
1734 1732
#define PCM_CODEC(id, name) \
libavcodec/flac.c
23 23
 * @author Alex Beregszaszi
24 24
 */
25 25
 
26
#include <limits.h>
27
 
26 28
#include "avcodec.h"
27 29
#include "golomb.h"
28 30

  
31
#undef NDEBUG
32
#include <assert.h>
33

  
29 34
#define MAX_CHANNELS 8
30 35
#define MAX_BLOCKSIZE 65535
31 36

  
......
43 48
    int min_blocksize, max_blocksize;
44 49
    int min_framesize, max_framesize;
45 50
    int samplerate, channels;
46
    int blocksize, last_blocksize;
51
    int blocksize/*, last_blocksize*/;
47 52
    int bps, curr_bps;
48 53
    enum channel_order order;
49 54

  
50
    uint8_t *residual[MAX_CHANNELS];
51
    uint32_t *decoded[MAX_CHANNELS];
55
    int32_t *residual[MAX_CHANNELS];
56
    int32_t *decoded[MAX_CHANNELS];
57
    uint8_t *bitstream;
58
    int bitstream_size;
59
    int bitstream_index;
60
    int allocated_bitstream_size;
52 61
} FLACContext;
53 62

  
54 63
#define METADATA_TYPE_STREAMINFO 0
......
61 70
static int sample_size_table[] = 
62 71
{ 0, 8, 12, 0, 16, 20, 24, 0 };
63 72

  
64
static uint64_t get_uvlc(GetBitContext *gb, int is64)
73
static int64_t get_utf8(GetBitContext *gb)
65 74
{
66
    uint64_t val = 0;
67
    int i = 0;
75
    uint64_t val;
76
    int ones=0, bytes;
77
    
78
    while(get_bits1(gb))
79
        ones++;
68 80

  
69
    while(i++ < 5+is64)
70
    {
81
    if     (ones==0) bytes=0;
82
    else if(ones==1) return -1;
83
    else             bytes= ones - 1;
84
    
85
    val= get_bits(gb, 7-ones);
86
    while(bytes--){
71 87
        const int tmp = get_bits(gb, 8);
72 88
        
73
        if (tmp & 0x80)
74
            val = (val << 7) + tmp - 0x80;
75
        else
76
            return (val << 7) + tmp;
89
        if((tmp>>6) != 2)
90
            return -1;
91
        val<<=6;
92
        val|= tmp&0x3F;
77 93
    }
78
    return -1;
94
    return val;
79 95
}
80 96

  
81 97
static int flac_decode_init(AVCodecContext * avctx)
......
85 101

  
86 102
static void dump_headers(FLACContext *s)
87 103
{
88
    printf("  Blocksize: %d .. %d (%d)\n", s->min_blocksize, s->max_blocksize, s->blocksize);
89
    printf("  Framesize: %d .. %d\n", s->min_framesize, s->max_framesize);
90
    printf("  Samplerate: %d\n", s->samplerate);
91
    printf("  Channels: %d\n", s->channels);
92
    printf("  Bits: %d\n", s->bps);
104
    av_log(s->avctx, AV_LOG_DEBUG, "  Blocksize: %d .. %d (%d)\n", s->min_blocksize, s->max_blocksize, s->blocksize);
105
    av_log(s->avctx, AV_LOG_DEBUG, "  Framesize: %d .. %d\n", s->min_framesize, s->max_framesize);
106
    av_log(s->avctx, AV_LOG_DEBUG, "  Samplerate: %d\n", s->samplerate);
107
    av_log(s->avctx, AV_LOG_DEBUG, "  Channels: %d\n", s->channels);
108
    av_log(s->avctx, AV_LOG_DEBUG, "  Bits: %d\n", s->bps);
93 109
}
94 110

  
95
static void metadata_streaminfo(FLACContext *s)
96
{
111
static void allocate_buffers(FLACContext *s){
97 112
    int i;
98 113

  
114
    assert(s->max_blocksize);
115

  
116
    if(s->max_framesize == 0 && s->max_blocksize){
117
        s->max_framesize= (s->channels * s->bps * s->max_blocksize + 7)/ 8; //FIXME header overhead
118
    }
119

  
120
    for (i = 0; i < s->channels; i++)
121
    {
122
        s->decoded[i] = av_realloc(s->decoded[i], sizeof(int32_t)*s->max_blocksize);
123
        s->residual[i] = av_realloc(s->residual[i], sizeof(int32_t)*s->max_blocksize);
124
    }
125

  
126
    s->bitstream= av_fast_realloc(s->bitstream, &s->allocated_bitstream_size, s->max_framesize);
127
//    s->bitstream= av_realloc(s->bitstream, s->max_framesize);
128
}
129

  
130
static void metadata_streaminfo(FLACContext *s)
131
{
99 132
    /* mandatory streaminfo */
100 133
    s->min_blocksize = get_bits(&s->gb, 16);
101 134
    s->max_blocksize = get_bits(&s->gb, 16);
......
114 147
    
115 148
    skip_bits(&s->gb, 64); /* md5 sum */
116 149
    skip_bits(&s->gb, 64); /* md5 sum */
117

  
118
    for (i = 0; i < s->channels; i++)
119
    {
120
        s->decoded[i] = av_realloc(s->decoded[i], sizeof(uint32_t)*s->max_blocksize);
121
        s->residual[i] = av_realloc(s->residual[i], sizeof(uint8_t)*s->max_blocksize);
122
    }
150
    
151
    allocate_buffers(s);
123 152
}
124 153

  
125 154
static int decode_residuals(FLACContext *s, int channel, int pred_order)
......
128 157
    int sample = 0, samples;
129 158

  
130 159
    method_type = get_bits(&s->gb, 2);
131
    if (method_type != 0)
160
    if (method_type != 0){
161
        av_log(s->avctx, AV_LOG_DEBUG, "illegal residual coding method %d\n", method_type);
132 162
        return -1;
163
    }
133 164
    
134 165
    rice_order = get_bits(&s->gb, 4);
135 166

  
136
    samples = (rice_order > 0) ?
137
        (s->blocksize >> rice_order) : (s->blocksize - pred_order);
167
    samples= s->blocksize >> rice_order;
138 168

  
169
    sample= 
170
    i= pred_order;
139 171
    for (partition = 0; partition < (1 << rice_order); partition++)
140 172
    {
141 173
        tmp = get_bits(&s->gb, 4);
142
        if (tmp == 0)
143
        {
144
            i = (!rice_order || partition) ? 0 : pred_order;
145
            for (; i < samples; i++, sample++)
146
                s->residual[channel][sample] = get_sr_golomb_flac(&s->gb, tmp, 0, 0);
147
            printf("zero k\n");
148
        }
149
        else if (tmp == 15)
174
        if (tmp == 15)
150 175
        {
151
            printf("fixed len partition\n");
176
            av_log(s->avctx, AV_LOG_DEBUG, "fixed len partition\n");
152 177
            tmp = get_bits(&s->gb, 5);
153
            i = (!rice_order || partition) ? 0 : pred_order;
154 178
            for (; i < samples; i++, sample++)
155
                s->residual[channel][sample] = get_bits(&s->gb, tmp);
179
                s->residual[channel][sample] = get_sbits(&s->gb, tmp);
156 180
        }
157 181
        else
158 182
        {
159
//            printf("rice coded partition\n");
160
#if 1
161
            i = (!rice_order || partition) ? 0 : pred_order;
162
            for (; i < samples; i++, sample++)
163
                s->residual[channel][sample] = get_sr_golomb_flac(&s->gb, tmp, 0, 0);
164
#else
165
            i = ((!rice_order || partition) ? samples : samples - pred_order) + sample;
166
            for (; sample < i; sample++)
167
                s->residual[channel][sample] = get_ur_golomb(&s->gb, tmp, 0, 0);
168
//                s->residual[channel][sample] = get_se_golomb(&s->gb);
169
#endif
183
//            av_log(s->avctx, AV_LOG_DEBUG, "rice coded partition k=%d\n", tmp);
184
            for (; i < samples; i++, sample++){
185
                s->residual[channel][sample] = get_sr_golomb_flac(&s->gb, tmp, INT_MAX, 0);
186
                if(get_bits_count(&s->gb) > s->gb.size_in_bits){
187
                    av_log(s->avctx, AV_LOG_ERROR, "fucking FLAC\n");
188
                    return -1;
189
                }
190
            }
170 191
        }
192
        i= 0;
171 193
    }
172 194

  
173
    printf("partitions: %d, samples: %d\n", 1 << rice_order, sample);
195
//    av_log(s->avctx, AV_LOG_DEBUG, "partitions: %d, samples: %d\n", 1 << rice_order, sample);
174 196

  
175 197
    return 0;
176 198
}    
......
179 201
{
180 202
    int i;
181 203
        
182
    printf("  SUBFRAME FIXED\n");
204
    av_log(s->avctx, AV_LOG_DEBUG, "  SUBFRAME FIXED\n");
183 205
        
184 206
    /* warm up samples */
185
    printf("   warm up samples: %d\n", pred_order);
207
    av_log(s->avctx, AV_LOG_DEBUG, "   warm up samples: %d\n", pred_order);
186 208
        
187 209
    for (i = 0; i < pred_order; i++)
188 210
    {
189
        s->decoded[channel][i] = get_bits(&s->gb, s->curr_bps);
190
        printf("    %d: %d\n", i, s->decoded[channel][i]);
211
        s->decoded[channel][i] = get_sbits(&s->gb, s->curr_bps);
212
//        av_log(s->avctx, AV_LOG_DEBUG, "    %d: %d\n", i, s->decoded[channel][i]);
191 213
    }
192 214
    
193 215
    if (decode_residuals(s, channel, pred_order) < 0)
......
213 235
        case 3:
214 236
            for (i = pred_order; i < s->blocksize; i++)
215 237
                s->decoded[channel][i] = s->residual[channel][i] +
216
                                        (((s->decoded[channel][i-1] -
217
                                        s->decoded[channel][i-2]) << 1) +
218
                                        (s->decoded[channel][i-1] -
219
                                        s->decoded[channel][i-2])) +
220
                                        s->decoded[channel][i-3];
238
                                        3*(s->decoded[channel][i-1] - s->decoded[channel][i-2])
239
                                        + s->decoded[channel][i-3];
221 240
            break;
222 241
        case 4:
223 242
            for (i = pred_order; i < s->blocksize; i++)
......
228 247
                                        (s->decoded[channel][i-2] << 1)) -
229 248
                                        s->decoded[channel][i-4];
230 249
            break;
250
        default:
251
            av_log(s->avctx, AV_LOG_ERROR, "illegal pred order %d\n", pred_order);
252
            return -1;
231 253
    }
232
    
254

  
233 255
    return 0;
234 256
}
235 257

  
......
239 261
    int coeff_prec, qlevel;
240 262
    int coeffs[pred_order];
241 263
        
242
    printf("  SUBFRAME LPC\n");
264
//    av_log(s->avctx, AV_LOG_DEBUG, "  SUBFRAME LPC\n");
243 265
        
244 266
    /* warm up samples */
245
    printf("   warm up samples: %d\n", pred_order);
267
//    av_log(s->avctx, AV_LOG_DEBUG, "   warm up samples: %d\n", pred_order);
246 268
        
247 269
    for (i = 0; i < pred_order; i++)
248 270
    {
249
        s->decoded[channel][i] = get_bits(&s->gb, s->curr_bps);
250
        printf("    %d: %d\n", i, s->decoded[channel][i]);
271
        s->decoded[channel][i] = get_sbits(&s->gb, s->curr_bps);
272
//        av_log(s->avctx, AV_LOG_DEBUG, "    %d: %d\n", i, s->decoded[channel][i]);
251 273
    }
252 274
    
253 275
    coeff_prec = get_bits(&s->gb, 4) + 1;
254 276
    if (coeff_prec == 16)
255 277
    {
256
        printf("invalid coeff precision\n");
278
        av_log(s->avctx, AV_LOG_DEBUG, "invalid coeff precision\n");
257 279
        return -1;
258 280
    }
259
    printf("   qlp coeff prec: %d\n", coeff_prec);
260
    qlevel = get_bits(&s->gb, 5);
261
    printf("   quant level: %d\n", qlevel);
262
    
281
    av_log(s->avctx, AV_LOG_DEBUG, "   qlp coeff prec: %d\n", coeff_prec);
282
    qlevel = get_sbits(&s->gb, 5);
283
    av_log(s->avctx, AV_LOG_DEBUG, "   quant level: %d\n", qlevel);
284
assert(qlevel >= 0); //FIXME
263 285
    for (i = 0; i < pred_order; i++)
264 286
    {
265
        coeffs[i] = get_bits(&s->gb, coeff_prec);
266
        printf("    %d: %d\n", i, coeffs[i]);
287
        coeffs[i] = get_sbits(&s->gb, coeff_prec);
288
//        av_log(s->avctx, AV_LOG_DEBUG, "    %d: %d\n", i, coeffs[i]);
267 289
    }
268 290
    
269 291
    if (decode_residuals(s, channel, pred_order) < 0)
......
286 308
    int i, tmp;
287 309
    
288 310
    s->curr_bps = s->bps;
289
    
311
    if(channel == 0){
312
        if(s->order == RIGHT_SIDE)
313
            s->curr_bps++;
314
    }else{
315
        if(s->order == LEFT_SIDE || s->order == MID_SIDE)
316
            s->curr_bps++;
317
    }
318

  
290 319
    if (get_bits1(&s->gb))
291 320
    {
292
        printf("invalid subframe padding\n");
321
        av_log(s->avctx, AV_LOG_DEBUG, "invalid subframe padding\n");
293 322
        return -1;
294 323
    }
295 324
    type = get_bits(&s->gb, 6);
......
303 332
//            wasted++;
304 333
//        s->curr_bps -= wasted;
305 334
//    }
306

  
335
#if 0
336
    wasted= 16 - av_log2(show_bits(&s->gb, 17));
337
    skip_bits(&s->gb, wasted+1);
338
    s->curr_bps -= wasted;
339
#else
307 340
    if (get_bits1(&s->gb))
308 341
    {
309 342
        wasted = 1;
310 343
        while (!get_bits1(&s->gb))
311 344
            wasted++;
312 345
        s->curr_bps -= wasted;
346
        av_log(s->avctx, AV_LOG_DEBUG, "%d wasted bits\n", wasted);
313 347
    }
314

  
348
#endif
349
//FIXME use av_log2 for types
315 350
    if (type == 0)
316 351
    {
317
        printf("coding type: constant\n");
318
        tmp = get_bits(&s->gb, s->curr_bps);
352
        av_log(s->avctx, AV_LOG_DEBUG, "coding type: constant\n");
353
        tmp = get_sbits(&s->gb, s->curr_bps);
319 354
        for (i = 0; i < s->blocksize; i++)
320 355
            s->decoded[channel][i] = tmp;
321 356
    }
322 357
    else if (type == 1)
323 358
    {
324
        printf("coding type: verbatim\n");
359
        av_log(s->avctx, AV_LOG_DEBUG, "coding type: verbatim\n");
325 360
        for (i = 0; i < s->blocksize; i++)
326
            s->decoded[channel][i] = get_bits(&s->gb, s->curr_bps);
361
            s->decoded[channel][i] = get_sbits(&s->gb, s->curr_bps);
327 362
    }
328 363
    else if ((type >= 8) && (type <= 12))
329 364
    {
330
        printf("coding type: fixed\n");
365
        av_log(s->avctx, AV_LOG_DEBUG, "coding type: fixed\n");
331 366
        if (decode_subframe_fixed(s, channel, type & ~0x8) < 0)
332 367
            return -1;
333 368
    }
334 369
    else if (type >= 32)
335 370
    {
336
        printf("coding type: lpc\n");
371
//        av_log(s->avctx, AV_LOG_DEBUG, "coding type: lpc\n");
337 372
        if (decode_subframe_lpc(s, channel, (type & ~0x20)+1) < 0)
338 373
            return -1;
339 374
    }
340 375
    else
341 376
    {
342
        printf("invalid coding type\n");
377
        av_log(s->avctx, AV_LOG_DEBUG, "invalid coding type\n");
343 378
        return -1;
344 379
    }
345 380
        
......
358 393
    int blocksize_code, sample_rate_code, sample_size_code, assignment, i;
359 394
    
360 395
    blocksize_code = get_bits(&s->gb, 4);
361
    if (blocksize_code == 0)
362
        s->blocksize = s->min_blocksize;
363
    else if (blocksize_code == 1)
364
        s->blocksize = 192;
365
    else if (blocksize_code <= 5)
366
        s->blocksize = 576 << (blocksize_code - 2);
367
    else if (blocksize_code >= 8)
368
        s->blocksize = 256 << (blocksize_code - 8);
369 396

  
370 397
    sample_rate_code = get_bits(&s->gb, 4);
371
    if ((sample_rate_code > 3) && (sample_rate_code < 12))
372
        s->samplerate = sample_rate_table[sample_rate_code];
373 398
    
374 399
    assignment = get_bits(&s->gb, 4); /* channel assignment */
375 400
    if (assignment < 8)
376 401
    {
377 402
        s->order = INDEPENDENT;
378 403
        if (s->channels != assignment+1)
379
            printf("channel number and number of assigned channels differ!\n");
380
        printf("channels: %d\n", assignment+1);
404
            av_log(s->avctx, AV_LOG_DEBUG, "channel number and number of assigned channels differ!\n");
405
        av_log(s->avctx, AV_LOG_DEBUG, "channels: %d\n", assignment+1);
381 406
    }
382 407
    else if (assignment == 8)
383 408
    {
384 409
        s->order = LEFT_SIDE;
385
        printf("left/side\n");
410
        av_log(s->avctx, AV_LOG_DEBUG, "left/side\n");
386 411
    }
387 412
    else if (assignment == 9)
388 413
    {
389 414
        s->order = RIGHT_SIDE;
390
        printf("right/side\n");
415
        av_log(s->avctx, AV_LOG_DEBUG, "right/side\n");
391 416
    }
392 417
    else if (assignment == 10)
393 418
    {
394 419
        s->order = MID_SIDE;
395
        printf("mid/side\n");
420
        av_log(s->avctx, AV_LOG_DEBUG, "mid/side\n");
396 421
    }
397 422
    else
398 423
    {
399
        printf("unsupported channel assignment\n");
424
        av_log(s->avctx, AV_LOG_DEBUG, "unsupported channel assignment\n");
400 425
        return -1;
401 426
    }
402 427

  
......
406 431
    }
407 432
        
408 433
    sample_size_code = get_bits(&s->gb, 3);
409
    if (s->bps != 0)
434
    if (sample_size_code != 0)
410 435
        s->bps = sample_size_table[sample_size_code];
411 436

  
412 437
    if ((sample_size_code == 3) || (sample_size_code == 7))
413 438
    {
414
        printf("invalid sample size code (%d)\n", sample_size_code);
439
        av_log(s->avctx, AV_LOG_DEBUG, "invalid sample size code (%d)\n", sample_size_code);
415 440
        return -1;
416 441
    }
417 442

  
418 443
    if (get_bits1(&s->gb))
419 444
    {
420
        printf("broken stream, invalid padding\n");
445
        av_log(s->avctx, AV_LOG_DEBUG, "broken stream, invalid padding\n");
421 446
//        return -1;
422 447
    }
423 448
    
424
    if (((blocksize_code == 6) || (blocksize_code == 7)) &&
425
        (s->min_blocksize != s->max_blocksize))
426
    {
427
        get_uvlc(&s->gb, 1);
449
    if (/*((blocksize_code == 6) || (blocksize_code == 7)) &&*/
450
        (s->min_blocksize != s->max_blocksize)){
451
        if(get_utf8(&s->gb) < 0){
452
            av_log(s->avctx, AV_LOG_ERROR, "utf8 fscked\n");
453
            return -1;
454
        }
455
    }else{
456
        if(get_utf8(&s->gb) < 0){
457
            av_log(s->avctx, AV_LOG_ERROR, "utf8 fscked\n");
458
            return -1;
459
        }
428 460
    }
429
    else
430
        get_uvlc(&s->gb, 0);
431
    
432
    if (blocksize_code == 6)
461

  
462
    if (blocksize_code == 0)
463
        s->blocksize = s->min_blocksize;
464
    else if (blocksize_code == 1)
465
        s->blocksize = 192;
466
    else if (blocksize_code <= 5)
467
        s->blocksize = 576 << (blocksize_code - 2);
468
    else if (blocksize_code == 6)
433 469
        s->blocksize = get_bits(&s->gb, 8)+1;
434
    if (blocksize_code == 7)
470
    else if (blocksize_code == 7)
435 471
        s->blocksize = get_bits(&s->gb, 16)+1;
472
    else if (blocksize_code >= 8)
473
        s->blocksize = 256 << (blocksize_code - 8);
436 474

  
437
    if ((sample_rate_code > 11) && (sample_rate_code < 15))
438
    {
439
        switch(sample_rate_code)
440
        {
441
            case 12:
475
    if(s->blocksize > s->max_blocksize){
476
        av_log(s->avctx, AV_LOG_ERROR, "blocksize %d > %d\n", s->blocksize, s->max_blocksize);
477
        return -1;
478
    }
479

  
480
    if (sample_rate_code == 0){
481
        //Streaminfo
482
    }else if ((sample_rate_code > 3) && (sample_rate_code < 12))
483
        s->samplerate = sample_rate_table[sample_rate_code];
484
    else if (sample_rate_code == 12)
442 485
                s->samplerate = get_bits(&s->gb, 8) * 1000;
443
                break;
444
            case 13:
486
    else if (sample_rate_code == 13)
445 487
                s->samplerate = get_bits(&s->gb, 16);
446
                break;
447
            case 14:
488
    else if (sample_rate_code == 14)
448 489
                s->samplerate = get_bits(&s->gb, 16) * 10;
449
                break;
450
        }
490
    else{
491
        av_log(s->avctx, AV_LOG_ERROR, "illegal sample rate code %d\n", sample_rate_code);
492
        return -1;
451 493
    }
452 494

  
453 495
    skip_bits(&s->gb, 8); /* header crc */
454 496

  
455
    dump_headers(s);
497
//    dump_headers(s);
456 498

  
457 499
    /* subframes */
458 500
    for (i = 0; i < s->channels; i++)
459 501
    {
460
        if (s->blocksize != s->last_blocksize)
502
/*        if (s->blocksize != s->last_blocksize)
461 503
        {
462 504
            s->decoded[i] = av_realloc(s->decoded[i], sizeof(uint32_t)*s->blocksize);
463 505
            s->residual[i] = av_realloc(s->residual[i], sizeof(uint8_t)*s->blocksize);
464
        }
465
        printf("decoded: %x residual: %x\n", s->decoded[i], s->residual[i]);
506
        }*/
507
//        av_log(s->avctx, AV_LOG_DEBUG, "decoded: %x residual: %x\n", s->decoded[i], s->residual[i]);
466 508
        if (decode_subframe(s, i) < 0)
467 509
            return -1;
468 510
    }
......
480 522
                            uint8_t *buf, int buf_size)
481 523
{
482 524
    FLACContext *s = avctx->priv_data;
483
    int metadata_flag, metadata_type, metadata_size;
484
    int tmp = 0, i, j = 0;
525
    int metadata_last, metadata_type, metadata_size;
526
    int tmp = 0, i, j = 0, input_buf_size;
485 527
    int16_t *samples = data, *left, *right;
486 528

  
487 529
    *data_size = 0;
488 530

  
489 531
    s->avctx = avctx;
532
    
533
    if(s->max_framesize == 0){
534
        s->max_framesize= 8192; // should hopefully be enough for the first header
535
        s->bitstream= av_fast_realloc(s->bitstream, &s->allocated_bitstream_size, s->max_framesize);
536
    }
537

  
538
    if(1 && s->max_framesize){//FIXME truncated
539
            buf_size= FFMIN(buf_size, s->max_framesize - s->bitstream_size);
540
            input_buf_size= buf_size;
541

  
542
            if(s->bitstream_index + s->bitstream_size + buf_size > s->allocated_bitstream_size){
543
//                printf("memmove\n");
544
                memmove(s->bitstream, &s->bitstream[s->bitstream_index], s->bitstream_size);
545
                s->bitstream_index=0;
546
            }
547
            memcpy(&s->bitstream[s->bitstream_index + s->bitstream_size], buf, buf_size);
548
            buf= &s->bitstream[s->bitstream_index];
549
            buf_size += s->bitstream_size;
550
            s->bitstream_size= buf_size;
551
            
552
            if(buf_size < s->max_framesize){
553
//                printf("wanna more data ...\n");
554
                return input_buf_size;
555
            }
556
    }
490 557

  
491 558
    init_get_bits(&s->gb, buf, buf_size*8);
492 559
    
493 560
    /* fLaC signature (be) */
494
    if (get_bits_long(&s->gb, 32) == bswap_32(ff_get_fourcc("fLaC")))
561
    if (show_bits_long(&s->gb, 32) == bswap_32(ff_get_fourcc("fLaC")))
495 562
    {
496
        printf("STREAM HEADER\n");
563
        skip_bits(&s->gb, 32);
564

  
565
        av_log(s->avctx, AV_LOG_DEBUG, "STREAM HEADER\n");
497 566
        do {
498
            metadata_flag = get_bits(&s->gb, 1);
567
            metadata_last = get_bits(&s->gb, 1);
499 568
            metadata_type = get_bits(&s->gb, 7);
500 569
            metadata_size = get_bits_long(&s->gb, 24);
501 570
            
502
            printf(" metadata block: flag = %d, type = %d, size = %d\n",
503
                metadata_flag, metadata_type,
571
            av_log(s->avctx, AV_LOG_DEBUG, " metadata block: flag = %d, type = %d, size = %d\n",
572
                metadata_last, metadata_type,
504 573
                metadata_size);
505

  
506
            switch(metadata_type)
507
            {
574
            if(metadata_size){
575
                switch(metadata_type)
576
                {
508 577
                case METADATA_TYPE_STREAMINFO:
578
                    if(metadata_size == 0)
579
                        av_log(s->avctx, AV_LOG_DEBUG, "size= 0 WTF!?\n");
580
                    
509 581
                    metadata_streaminfo(s);
510 582
                    dump_headers(s);
511 583
                    break;
512 584
                default:
513
                    while ((metadata_size -= 8) > 0)
585
                    for(i=0; i<metadata_size; i++)
514 586
                        skip_bits(&s->gb, 8);
587
                }
515 588
            }
516
        } while(metadata_flag != 1);
589
        } while(!metadata_last);
517 590
    }
518 591
    else
519 592
    {
520
        init_get_bits(&s->gb, buf, buf_size*8);
521
        tmp = get_bits(&s->gb, 16);
522
        if (tmp == 0xfff8)
523
            printf("FRAME HEADER\n");
524

  
593
        
594
         tmp = show_bits(&s->gb, 16);
595
        if(tmp != 0xFFF8){
596
            av_log(s->avctx, AV_LOG_ERROR, "FRAME HEADER not here\n");
597
            while(get_bits_count(&s->gb)/8+2 < buf_size && show_bits(&s->gb, 16) != 0xFFF8)
598
                skip_bits(&s->gb, 8);
599
            goto end; // we may not have enough bits left to decode a frame, so try next time
600
        }
601
        skip_bits(&s->gb, 16);
525 602
        if (decode_frame(s) < 0)
526 603
            return -1;
527 604
    }
605

  
528 606
    
529 607
#if 0
530 608
    /* fix the channel order here */
......
562 640
                        samples[(s->blocksize*i)+j] = s->decoded[0][j] - s->decoded[i][j];
563 641
                break;
564 642
//            case MID_SIDE:
565
//                printf("mid-side unsupported\n");
643
//                av_log(s->avctx, AV_LOG_DEBUG, "mid-side unsupported\n");
566 644
        }
567 645
        *data_size += s->blocksize;
568 646
    }
......
571 649
    switch(s->order)
572 650
    {
573 651
        case INDEPENDENT:
574
            for (i = 0; i < s->channels; i++)
652
            for (j = 0; j < s->blocksize; j++)
575 653
            {
576
                for (j = 0; j < s->blocksize; j++)
654
                for (i = 0; i < s->channels; i++)
577 655
                    *(samples++) = s->decoded[i][j];
578
                *data_size += s->blocksize;
579 656
            }
580 657
            break;
581 658
        case LEFT_SIDE:
......
585 662
                *(samples++) = s->decoded[0][i];
586 663
                *(samples++) = s->decoded[0][i] - s->decoded[1][i];
587 664
            }
588
            *data_size = 2*s->blocksize;
589 665
            break;
590 666
        case RIGHT_SIDE:
591 667
            assert(s->channels == 2);
......
594 670
                *(samples++) = s->decoded[0][i] + s->decoded[1][i];
595 671
                *(samples++) = s->decoded[1][i];
596 672
            }
597
            *data_size = 2*s->blocksize;
598 673
            break;
599 674
        case MID_SIDE:
600 675
            assert(s->channels == 2);
601 676
            for (i = 0; i < s->blocksize; i++)
602 677
            {
603
                int16_t mid, side;
678
                int mid, side;
604 679
                mid = s->decoded[0][i];
605 680
                side = s->decoded[1][i];
606 681
                
......
610 685
                *(samples++) = (mid + side) >> 1;
611 686
                *(samples++) = (mid - side) >> 1;
612 687
            }
613
            *data_size = 2*s->blocksize;
614 688
            break;
615 689
    }
616 690
#endif
617 691

  
618
//    *data_size = (int8_t *)samples - (int8_t *)data;
619
    printf("data size: %d\n", *data_size);
692
    *data_size = (int8_t *)samples - (int8_t *)data;
693
    av_log(s->avctx, AV_LOG_DEBUG, "data size: %d\n", *data_size);
620 694

  
621
    s->last_blocksize = s->blocksize;
695
//    s->last_blocksize = s->blocksize;
696
end:
697
    i= (get_bits_count(&s->gb)+7)/8;;
698
    if(i > buf_size){
699
        av_log(s->avctx, AV_LOG_ERROR, "overread: %d\n", i - buf_size);
700
        return -1;
701
    }
622 702

  
623
    return (get_bits_count(&s->gb)+7)/8;
703
    if(s->bitstream_size){
704
        s->bitstream_index += i;
705
        s->bitstream_size  -= i;
706
        return input_buf_size;
707
    }else 
708
        return i;
624 709
}
625 710

  
626 711
static int flac_decode_close(AVCodecContext *avctx)
......
630 715
    
631 716
    for (i = 0; i < s->channels; i++)
632 717
    {
633
        if (s->decoded[i])
634
            av_free(s->decoded[i]);
635
        if (s->residual[i])
636
            av_free(s->residual[i]);
718
        av_freep(&s->decoded[i]);
719
        av_freep(&s->residual[i]);
637 720
    }
721
    av_freep(&s->bitstream);
638 722
    
639 723
    return 0;
640 724
}
libavcodec/golomb.h
256 256
    }
257 257
}
258 258

  
259
/**
260
 * read unsigned golomb rice code (flac).
261
 */
262
static inline int get_sr_golomb_flac(GetBitContext *gb, int k, int limit, int esc_len){
263
    int v= get_ur_golomb_jpegls(gb, k, limit, esc_len);
264
    return (v>>1) ^ -(v&1);
265
}
266

  
259 267
#ifdef TRACE
260 268

  
261 269
static inline int get_ue(GetBitContext *s, char *file, char *func, int line){

Also available in: Unified diff