Browse code

Merge remote-tracking branch 'qatar/master'

* qatar/master: (51 commits)
cin audio: use sign_extend() instead of casting to int16_t
cin audio: restructure decoding loop to avoid a separate counter variable
cin audio: use local variable for delta value
cin audio: remove unneeded cast from void*
cin audio: validate the channel count
cin audio: remove unneeded AVCodecContext pointer from CinAudioContext
dsicin: fix several audio-related fields in the CIN demuxer
flacdec: use av_get_bytes_per_sample() to get sample size
dca: handle errors from dca_decode_block()
dca: return error if the frame header is invalid
dca: return proper error codes instead of -1
utvideo: handle empty Huffman trees
binkaudio: change short to int16_t
binkaudio: only decode one block at a time.
binkaudio: store interleaved overlap samples in BinkAudioContext.
binkaudio: pre-calculate quantization factors
binkaudio: add some buffer overread checks.
atrac3: support float or int16 output using request_sample_fmt
atrac3: add CODEC_CAP_SUBFRAMES capability
atrac3: return appropriate error codes instead of -1
...

Conflicts:
libavcodec/atrac1.c
libavcodec/dca.c
libavformat/mov.c

Merged-by: Michael Niedermayer <michaelni@gmx.at>

Michael Niedermayer authored on 2011/10/30 08:33:41
Showing 14 changed files
... ...
@@ -36,6 +36,7 @@
36 36
 #include "get_bits.h"
37 37
 #include "dsputil.h"
38 38
 #include "fft.h"
39
+#include "fmtconvert.h"
39 40
 #include "sinewin.h"
40 41
 
41 42
 #include "atrac.h"
... ...
@@ -78,10 +79,11 @@ typedef struct {
78 78
     DECLARE_ALIGNED(32, float,  mid)[256];
79 79
     DECLARE_ALIGNED(32, float, high)[512];
80 80
     float*              bands[3];
81
-    DECLARE_ALIGNED(32, float, out_samples)[AT1_MAX_CHANNELS][AT1_SU_SAMPLES];
81
+    float              *out_samples[AT1_MAX_CHANNELS];
82 82
     FFTContext          mdct_ctx[3];
83 83
     int                 channels;
84 84
     DSPContext          dsp;
85
+    FmtConvertContext   fmt_conv;
85 86
 } AT1Ctx;
86 87
 
87 88
 /** size of the transform in samples in the long mode for each QMF band */
... ...
@@ -129,7 +131,7 @@ static int at1_imdct_block(AT1SUCtx* su, AT1Ctx *q)
129 129
             nbits = mdct_long_nbits[band_num] - log2_block_count;
130 130
 
131 131
             if (nbits != 5 && nbits != 7 && nbits != 8)
132
-                return -1;
132
+                return AVERROR_INVALIDDATA;
133 133
         } else {
134 134
             block_size = 32;
135 135
             nbits = 5;
... ...
@@ -173,14 +175,14 @@ static int at1_parse_bsm(GetBitContext* gb, int log2_block_cnt[AT1_QMF_BANDS])
173 173
         /* low and mid band */
174 174
         log2_block_count_tmp = get_bits(gb, 2);
175 175
         if (log2_block_count_tmp & 1)
176
-            return -1;
176
+            return AVERROR_INVALIDDATA;
177 177
         log2_block_cnt[i] = 2 - log2_block_count_tmp;
178 178
     }
179 179
 
180 180
     /* high band */
181 181
     log2_block_count_tmp = get_bits(gb, 2);
182 182
     if (log2_block_count_tmp != 0 && log2_block_count_tmp != 3)
183
-        return -1;
183
+        return AVERROR_INVALIDDATA;
184 184
     log2_block_cnt[IDX_HIGH_BAND] = 3 - log2_block_count_tmp;
185 185
 
186 186
     skip_bits(gb, 2);
... ...
@@ -229,7 +231,7 @@ static int at1_unpack_dequant(GetBitContext* gb, AT1SUCtx* su,
229 229
 
230 230
             /* check for bitstream overflow */
231 231
             if (bits_used > AT1_SU_MAX_BITS)
232
-                return -1;
232
+                return AVERROR_INVALIDDATA;
233 233
 
234 234
             /* get the position of the 1st spec according to the block size mode */
235 235
             pos = su->log2_block_count[band_num] ? bfu_start_short[bfu_num] : bfu_start_long[bfu_num];
... ...
@@ -276,14 +278,21 @@ static int atrac1_decode_frame(AVCodecContext *avctx, void *data,
276 276
     const uint8_t *buf = avpkt->data;
277 277
     int buf_size       = avpkt->size;
278 278
     AT1Ctx *q          = avctx->priv_data;
279
-    int ch, ret, i;
279
+    int ch, ret, out_size;
280 280
     GetBitContext gb;
281 281
     float* samples = data;
282 282
 
283 283
 
284 284
     if (buf_size < 212 * q->channels) {
285
-        av_log(avctx, AV_LOG_ERROR,"Not enought data to decode!\n");
286
-        return -1;
285
+        av_log(avctx,AV_LOG_ERROR,"Not enough data to decode!\n");
286
+        return AVERROR_INVALIDDATA;
287
+    }
288
+
289
+    out_size = q->channels * AT1_SU_SAMPLES *
290
+               av_get_bytes_per_sample(avctx->sample_fmt);
291
+    if (*data_size < out_size) {
292
+        av_log(avctx, AV_LOG_ERROR, "Output buffer is too small\n");
293
+        return AVERROR(EINVAL);
287 294
     }
288 295
 
289 296
     for (ch = 0; ch < q->channels; ch++) {
... ...
@@ -303,44 +312,72 @@ static int atrac1_decode_frame(AVCodecContext *avctx, void *data,
303 303
         ret = at1_imdct_block(su, q);
304 304
         if (ret < 0)
305 305
             return ret;
306
-        at1_subband_synthesis(q, su, q->out_samples[ch]);
306
+        at1_subband_synthesis(q, su, q->channels == 1 ? samples : q->out_samples[ch]);
307 307
     }
308 308
 
309
-    /* interleave; FIXME, should create/use a DSP function */
310
-    if (q->channels == 1) {
311
-        /* mono */
312
-        memcpy(samples, q->out_samples[0], AT1_SU_SAMPLES * 4);
313
-    } else {
314
-        /* stereo */
315
-        for (i = 0; i < AT1_SU_SAMPLES; i++) {
316
-            samples[i * 2]     = q->out_samples[0][i];
317
-            samples[i * 2 + 1] = q->out_samples[1][i];
318
-        }
309
+    /* interleave */
310
+    if (q->channels == 2) {
311
+        q->fmt_conv.float_interleave(samples, (const float **)q->out_samples,
312
+                                     AT1_SU_SAMPLES, 2);
319 313
     }
320 314
 
321
-    *data_size = q->channels * AT1_SU_SAMPLES * sizeof(*samples);
315
+    *data_size = out_size;
322 316
     return avctx->block_align;
323 317
 }
324 318
 
325 319
 
320
+static av_cold int atrac1_decode_end(AVCodecContext * avctx)
321
+{
322
+    AT1Ctx *q = avctx->priv_data;
323
+
324
+    av_freep(&q->out_samples[0]);
325
+
326
+    ff_mdct_end(&q->mdct_ctx[0]);
327
+    ff_mdct_end(&q->mdct_ctx[1]);
328
+    ff_mdct_end(&q->mdct_ctx[2]);
329
+
330
+    return 0;
331
+}
332
+
333
+
326 334
 static av_cold int atrac1_decode_init(AVCodecContext *avctx)
327 335
 {
328 336
     AT1Ctx *q = avctx->priv_data;
337
+    int ret;
329 338
 
330 339
     avctx->sample_fmt = AV_SAMPLE_FMT_FLT;
331 340
 
341
+    if (avctx->channels < 1 || avctx->channels > AT1_MAX_CHANNELS) {
342
+        av_log(avctx, AV_LOG_ERROR, "Unsupported number of channels: %d\n",
343
+               avctx->channels);
344
+        return AVERROR(EINVAL);
345
+    }
332 346
     q->channels = avctx->channels;
333 347
 
348
+    if (avctx->channels == 2) {
349
+        q->out_samples[0] = av_malloc(2 * AT1_SU_SAMPLES * sizeof(*q->out_samples[0]));
350
+        q->out_samples[1] = q->out_samples[0] + AT1_SU_SAMPLES;
351
+        if (!q->out_samples[0]) {
352
+            av_freep(&q->out_samples[0]);
353
+            return AVERROR(ENOMEM);
354
+        }
355
+    }
356
+
334 357
     /* Init the mdct transforms */
335
-    ff_mdct_init(&q->mdct_ctx[0], 6, 1, -1.0/ (1 << 15));
336
-    ff_mdct_init(&q->mdct_ctx[1], 8, 1, -1.0/ (1 << 15));
337
-    ff_mdct_init(&q->mdct_ctx[2], 9, 1, -1.0/ (1 << 15));
358
+    if ((ret = ff_mdct_init(&q->mdct_ctx[0], 6, 1, -1.0/ (1 << 15))) ||
359
+        (ret = ff_mdct_init(&q->mdct_ctx[1], 8, 1, -1.0/ (1 << 15))) ||
360
+        (ret = ff_mdct_init(&q->mdct_ctx[2], 9, 1, -1.0/ (1 << 15)))) {
361
+        av_log(avctx, AV_LOG_ERROR, "Error initializing MDCT\n");
362
+        atrac1_decode_end(avctx);
363
+        return ret;
364
+    }
338 365
 
339 366
     ff_init_ff_sine_windows(5);
340 367
 
341 368
     atrac_generate_tables();
342 369
 
343 370
     dsputil_init(&q->dsp, avctx);
371
+    ff_fmt_convert_init(&q->fmt_conv, avctx);
344 372
 
345 373
     q->bands[0] = q->low;
346 374
     q->bands[1] = q->mid;
... ...
@@ -356,16 +393,6 @@ static av_cold int atrac1_decode_init(AVCodecContext *avctx)
356 356
 }
357 357
 
358 358
 
359
-static av_cold int atrac1_decode_end(AVCodecContext * avctx) {
360
-    AT1Ctx *q = avctx->priv_data;
361
-
362
-    ff_mdct_end(&q->mdct_ctx[0]);
363
-    ff_mdct_end(&q->mdct_ctx[1]);
364
-    ff_mdct_end(&q->mdct_ctx[2]);
365
-    return 0;
366
-}
367
-
368
-
369 359
 AVCodec ff_atrac1_decoder = {
370 360
     .name = "atrac1",
371 361
     .type = AVMEDIA_TYPE_AUDIO,
... ...
@@ -41,6 +41,7 @@
41 41
 #include "dsputil.h"
42 42
 #include "bytestream.h"
43 43
 #include "fft.h"
44
+#include "fmtconvert.h"
44 45
 
45 46
 #include "atrac.h"
46 47
 #include "atrac3data.h"
... ...
@@ -48,6 +49,8 @@
48 48
 #define JOINT_STEREO    0x12
49 49
 #define STEREO          0x2
50 50
 
51
+#define SAMPLES_PER_FRAME 1024
52
+#define MDCT_SIZE          512
51 53
 
52 54
 /* These structures are needed to store the parsed gain control data. */
53 55
 typedef struct {
... ...
@@ -70,12 +73,12 @@ typedef struct {
70 70
     int               bandsCoded;
71 71
     int               numComponents;
72 72
     tonal_component   components[64];
73
-    float             prevFrame[1024];
73
+    float             prevFrame[SAMPLES_PER_FRAME];
74 74
     int               gcBlkSwitch;
75 75
     gain_block        gainBlock[2];
76 76
 
77
-    DECLARE_ALIGNED(32, float, spectrum)[1024];
78
-    DECLARE_ALIGNED(32, float, IMDCT_buf)[1024];
77
+    DECLARE_ALIGNED(32, float, spectrum)[SAMPLES_PER_FRAME];
78
+    DECLARE_ALIGNED(32, float, IMDCT_buf)[SAMPLES_PER_FRAME];
79 79
 
80 80
     float             delayBuf1[46]; ///<qmf delay buffers
81 81
     float             delayBuf2[46];
... ...
@@ -107,7 +110,7 @@ typedef struct {
107 107
     //@}
108 108
     //@{
109 109
     /** data buffers */
110
-    float               outSamples[2048];
110
+    float              *outSamples[2];
111 111
     uint8_t*            decoded_bytes_buffer;
112 112
     float               tempBuf[1070];
113 113
     //@}
... ...
@@ -120,9 +123,10 @@ typedef struct {
120 120
     //@}
121 121
 
122 122
     FFTContext          mdct_ctx;
123
+    FmtConvertContext   fmt_conv;
123 124
 } ATRAC3Context;
124 125
 
125
-static DECLARE_ALIGNED(32, float, mdct_window)[512];
126
+static DECLARE_ALIGNED(32, float, mdct_window)[MDCT_SIZE];
126 127
 static VLC              spectral_coeff_tab[7];
127 128
 static float            gain_tab1[16];
128 129
 static float            gain_tab2[31];
... ...
@@ -159,7 +163,7 @@ static void IMLT(ATRAC3Context *q, float *pInput, float *pOutput, int odd_band)
159 159
     q->mdct_ctx.imdct_calc(&q->mdct_ctx,pOutput,pInput);
160 160
 
161 161
     /* Perform windowing on the output. */
162
-    dsp.vector_fmul(pOutput, pOutput, mdct_window, 512);
162
+    dsp.vector_fmul(pOutput, pOutput, mdct_window, MDCT_SIZE);
163 163
 
164 164
 }
165 165
 
... ...
@@ -192,7 +196,7 @@ static int decode_bytes(const uint8_t* inbuffer, uint8_t* out, int bytes){
192 192
 }
193 193
 
194 194
 
195
-static av_cold void init_atrac3_transforms(ATRAC3Context *q) {
195
+static av_cold int init_atrac3_transforms(ATRAC3Context *q, int is_float) {
196 196
     float enc_window[256];
197 197
     int i;
198 198
 
... ...
@@ -208,7 +212,7 @@ static av_cold void init_atrac3_transforms(ATRAC3Context *q) {
208 208
         }
209 209
 
210 210
     /* Initialize the MDCT transform. */
211
-    ff_mdct_init(&q->mdct_ctx, 9, 1, 1.0);
211
+    return ff_mdct_init(&q->mdct_ctx, 9, 1, is_float ? 1.0 / 32768 : 1.0);
212 212
 }
213 213
 
214 214
 /**
... ...
@@ -221,6 +225,8 @@ static av_cold int atrac3_decode_close(AVCodecContext *avctx)
221 221
 
222 222
     av_free(q->pUnits);
223 223
     av_free(q->decoded_bytes_buffer);
224
+    av_freep(&q->outSamples[0]);
225
+
224 226
     ff_mdct_end(&q->mdct_ctx);
225 227
 
226 228
     return 0;
... ...
@@ -340,7 +346,7 @@ static int decodeSpectrum (GetBitContext *gb, float *pOut)
340 340
 
341 341
     /* Clear the subbands that were not coded. */
342 342
     first = subbandTab[cnt];
343
-    memset(pOut+first, 0, (1024 - first) * sizeof(float));
343
+    memset(pOut+first, 0, (SAMPLES_PER_FRAME - first) * sizeof(float));
344 344
     return numSubbands;
345 345
 }
346 346
 
... ...
@@ -370,7 +376,7 @@ static int decodeTonalComponents (GetBitContext *gb, tonal_component *pComponent
370 370
 
371 371
     coding_mode_selector = get_bits(gb,2);
372 372
     if (coding_mode_selector == 2)
373
-        return -1;
373
+        return AVERROR_INVALIDDATA;
374 374
 
375 375
     coding_mode = coding_mode_selector & 1;
376 376
 
... ...
@@ -382,7 +388,7 @@ static int decodeTonalComponents (GetBitContext *gb, tonal_component *pComponent
382 382
 
383 383
         quant_step_index = get_bits(gb,3);
384 384
         if (quant_step_index <= 1)
385
-            return -1;
385
+            return AVERROR_INVALIDDATA;
386 386
 
387 387
         if (coding_mode_selector == 3)
388 388
             coding_mode = get_bits1(gb);
... ...
@@ -396,7 +402,7 @@ static int decodeTonalComponents (GetBitContext *gb, tonal_component *pComponent
396 396
             for (k=0; k<coded_components; k++) {
397 397
                 sfIndx = get_bits(gb,6);
398 398
                 pComponent[component_count].pos = j * 64 + (get_bits(gb,6));
399
-                max_coded_values = 1024 - pComponent[component_count].pos;
399
+                max_coded_values = SAMPLES_PER_FRAME - pComponent[component_count].pos;
400 400
                 coded_values = coded_values_per_component + 1;
401 401
                 coded_values = FFMIN(max_coded_values,coded_values);
402 402
 
... ...
@@ -445,7 +451,7 @@ static int decodeGainControl (GetBitContext *gb, gain_block *pGb, int numBands)
445 445
             pLevel[cf]= get_bits(gb,4);
446 446
             pLoc  [cf]= get_bits(gb,5);
447 447
             if(cf && pLoc[cf] <= pLoc[cf-1])
448
-                return -1;
448
+                return AVERROR_INVALIDDATA;
449 449
         }
450 450
     }
451 451
 
... ...
@@ -662,12 +668,12 @@ static int decodeChannelSoundUnit (ATRAC3Context *q, GetBitContext *gb, channel_
662 662
     if (codingMode == JOINT_STEREO && channelNum == 1) {
663 663
         if (get_bits(gb,2) != 3) {
664 664
             av_log(NULL,AV_LOG_ERROR,"JS mono Sound Unit id != 3.\n");
665
-            return -1;
665
+            return AVERROR_INVALIDDATA;
666 666
         }
667 667
     } else {
668 668
         if (get_bits(gb,6) != 0x28) {
669 669
             av_log(NULL,AV_LOG_ERROR,"Sound Unit id != 0x28.\n");
670
-            return -1;
670
+            return AVERROR_INVALIDDATA;
671 671
         }
672 672
     }
673 673
 
... ...
@@ -719,7 +725,8 @@ static int decodeChannelSoundUnit (ATRAC3Context *q, GetBitContext *gb, channel_
719 719
  * @param databuf       the input data
720 720
  */
721 721
 
722
-static int decodeFrame(ATRAC3Context *q, const uint8_t* databuf)
722
+static int decodeFrame(ATRAC3Context *q, const uint8_t* databuf,
723
+                       float **out_samples)
723 724
 {
724 725
     int   result, i;
725 726
     float   *p1, *p2, *p3, *p4;
... ...
@@ -731,7 +738,7 @@ static int decodeFrame(ATRAC3Context *q, const uint8_t* databuf)
731 731
         /* decode Sound Unit 1 */
732 732
         init_get_bits(&q->gb,databuf,q->bits_per_frame);
733 733
 
734
-        result = decodeChannelSoundUnit(q,&q->gb, q->pUnits, q->outSamples, 0, JOINT_STEREO);
734
+        result = decodeChannelSoundUnit(q,&q->gb, q->pUnits, out_samples[0], 0, JOINT_STEREO);
735 735
         if (result != 0)
736 736
             return (result);
737 737
 
... ...
@@ -753,7 +760,7 @@ static int decodeFrame(ATRAC3Context *q, const uint8_t* databuf)
753 753
         ptr1 = q->decoded_bytes_buffer;
754 754
         for (i = 4; *ptr1 == 0xF8; i++, ptr1++) {
755 755
             if (i >= q->bytes_per_frame)
756
-                return -1;
756
+                return AVERROR_INVALIDDATA;
757 757
         }
758 758
 
759 759
 
... ...
@@ -772,14 +779,14 @@ static int decodeFrame(ATRAC3Context *q, const uint8_t* databuf)
772 772
         }
773 773
 
774 774
         /* Decode Sound Unit 2. */
775
-        result = decodeChannelSoundUnit(q,&q->gb, &q->pUnits[1], &q->outSamples[1024], 1, JOINT_STEREO);
775
+        result = decodeChannelSoundUnit(q,&q->gb, &q->pUnits[1], out_samples[1], 1, JOINT_STEREO);
776 776
         if (result != 0)
777 777
             return (result);
778 778
 
779 779
         /* Reconstruct the channel coefficients. */
780
-        reverseMatrixing(q->outSamples, &q->outSamples[1024], q->matrix_coeff_index_prev, q->matrix_coeff_index_now);
780
+        reverseMatrixing(out_samples[0], out_samples[1], q->matrix_coeff_index_prev, q->matrix_coeff_index_now);
781 781
 
782
-        channelWeighting(q->outSamples, &q->outSamples[1024], q->weighting_delay);
782
+        channelWeighting(out_samples[0], out_samples[1], q->weighting_delay);
783 783
 
784 784
     } else {
785 785
         /* normal stereo mode or mono */
... ...
@@ -789,22 +796,21 @@ static int decodeFrame(ATRAC3Context *q, const uint8_t* databuf)
789 789
             /* Set the bitstream reader at the start of a channel sound unit. */
790 790
             init_get_bits(&q->gb, databuf+((i*q->bytes_per_frame)/q->channels), (q->bits_per_frame)/q->channels);
791 791
 
792
-            result = decodeChannelSoundUnit(q,&q->gb, &q->pUnits[i], &q->outSamples[i*1024], i, q->codingMode);
792
+            result = decodeChannelSoundUnit(q,&q->gb, &q->pUnits[i], out_samples[i], i, q->codingMode);
793 793
             if (result != 0)
794 794
                 return (result);
795 795
         }
796 796
     }
797 797
 
798 798
     /* Apply the iQMF synthesis filter. */
799
-    p1= q->outSamples;
800 799
     for (i=0 ; i<q->channels ; i++) {
800
+        p1 = out_samples[i];
801 801
         p2= p1+256;
802 802
         p3= p2+256;
803 803
         p4= p3+256;
804 804
         atrac_iqmf (p1, p2, 256, p1, q->pUnits[i].delayBuf1, q->tempBuf);
805 805
         atrac_iqmf (p4, p3, 256, p3, q->pUnits[i].delayBuf2, q->tempBuf);
806 806
         atrac_iqmf (p1, p3, 512, p1, q->pUnits[i].delayBuf3, q->tempBuf);
807
-        p1 +=1024;
808 807
     }
809 808
 
810 809
     return 0;
... ...
@@ -823,15 +829,22 @@ static int atrac3_decode_frame(AVCodecContext *avctx,
823 823
     const uint8_t *buf = avpkt->data;
824 824
     int buf_size = avpkt->size;
825 825
     ATRAC3Context *q = avctx->priv_data;
826
-    int result = 0, i;
826
+    int result = 0, out_size;
827 827
     const uint8_t* databuf;
828
-    int16_t* samples = data;
828
+    float   *samples_flt = data;
829
+    int16_t *samples_s16 = data;
829 830
 
830 831
     if (buf_size < avctx->block_align) {
831 832
         av_log(avctx, AV_LOG_ERROR,
832 833
                "Frame too small (%d bytes). Truncated file?\n", buf_size);
833
-        *data_size = 0;
834
-        return buf_size;
834
+        return AVERROR_INVALIDDATA;
835
+    }
836
+
837
+    out_size = SAMPLES_PER_FRAME * q->channels *
838
+               av_get_bytes_per_sample(avctx->sample_fmt);
839
+    if (*data_size < out_size) {
840
+        av_log(avctx, AV_LOG_ERROR, "Output buffer is too small\n");
841
+        return AVERROR(EINVAL);
835 842
     }
836 843
 
837 844
     /* Check if we need to descramble and what buffer to pass on. */
... ...
@@ -842,26 +855,27 @@ static int atrac3_decode_frame(AVCodecContext *avctx,
842 842
         databuf = buf;
843 843
     }
844 844
 
845
-    result = decodeFrame(q, databuf);
845
+    if (q->channels == 1 && avctx->sample_fmt == AV_SAMPLE_FMT_FLT)
846
+        result = decodeFrame(q, databuf, &samples_flt);
847
+    else
848
+        result = decodeFrame(q, databuf, q->outSamples);
846 849
 
847 850
     if (result != 0) {
848 851
         av_log(NULL,AV_LOG_ERROR,"Frame decoding error!\n");
849
-        return -1;
852
+        return result;
850 853
     }
851 854
 
852
-    if (q->channels == 1) {
853
-        /* mono */
854
-        for (i = 0; i<1024; i++)
855
-            samples[i] = av_clip_int16(round(q->outSamples[i]));
856
-        *data_size = 1024 * sizeof(int16_t);
857
-    } else {
858
-        /* stereo */
859
-        for (i = 0; i < 1024; i++) {
860
-            samples[i*2] = av_clip_int16(round(q->outSamples[i]));
861
-            samples[i*2+1] = av_clip_int16(round(q->outSamples[1024+i]));
862
-        }
863
-        *data_size = 2048 * sizeof(int16_t);
855
+    /* interleave */
856
+    if (q->channels == 2 && avctx->sample_fmt == AV_SAMPLE_FMT_FLT) {
857
+        q->fmt_conv.float_interleave(samples_flt,
858
+                                     (const float **)q->outSamples,
859
+                                     SAMPLES_PER_FRAME, 2);
860
+    } else if (avctx->sample_fmt == AV_SAMPLE_FMT_S16) {
861
+        q->fmt_conv.float_to_int16_interleave(samples_s16,
862
+                                              (const float **)q->outSamples,
863
+                                              SAMPLES_PER_FRAME, q->channels);
864 864
     }
865
+    *data_size = out_size;
865 866
 
866 867
     return avctx->block_align;
867 868
 }
... ...
@@ -875,7 +889,7 @@ static int atrac3_decode_frame(AVCodecContext *avctx,
875 875
 
876 876
 static av_cold int atrac3_decode_init(AVCodecContext *avctx)
877 877
 {
878
-    int i;
878
+    int i, ret;
879 879
     const uint8_t *edata_ptr = avctx->extradata;
880 880
     ATRAC3Context *q = avctx->priv_data;
881 881
     static VLC_TYPE atrac3_vlc_table[4096][2];
... ...
@@ -899,7 +913,7 @@ static av_cold int atrac3_decode_init(AVCodecContext *avctx)
899 899
         av_log(avctx,AV_LOG_DEBUG,"[12-13] %d\n",bytestream_get_le16(&edata_ptr));  //Unknown always 0
900 900
 
901 901
         /* setup */
902
-        q->samples_per_frame = 1024 * q->channels;
902
+        q->samples_per_frame = SAMPLES_PER_FRAME * q->channels;
903 903
         q->atrac3version = 4;
904 904
         q->delay = 0x88E;
905 905
         if (q->codingMode)
... ...
@@ -912,7 +926,7 @@ static av_cold int atrac3_decode_init(AVCodecContext *avctx)
912 912
         if ((q->bytes_per_frame == 96*q->channels*q->frame_factor) || (q->bytes_per_frame == 152*q->channels*q->frame_factor) || (q->bytes_per_frame == 192*q->channels*q->frame_factor)) {
913 913
         } else {
914 914
             av_log(avctx,AV_LOG_ERROR,"Unknown frame/channel/frame_factor configuration %d/%d/%d\n", q->bytes_per_frame, q->channels, q->frame_factor);
915
-            return -1;
915
+            return AVERROR_INVALIDDATA;
916 916
         }
917 917
 
918 918
     } else if (avctx->extradata_size == 10) {
... ...
@@ -932,17 +946,17 @@ static av_cold int atrac3_decode_init(AVCodecContext *avctx)
932 932
 
933 933
     if (q->atrac3version != 4) {
934 934
         av_log(avctx,AV_LOG_ERROR,"Version %d != 4.\n",q->atrac3version);
935
-        return -1;
935
+        return AVERROR_INVALIDDATA;
936 936
     }
937 937
 
938
-    if (q->samples_per_frame != 1024 && q->samples_per_frame != 2048) {
938
+    if (q->samples_per_frame != SAMPLES_PER_FRAME && q->samples_per_frame != SAMPLES_PER_FRAME*2) {
939 939
         av_log(avctx,AV_LOG_ERROR,"Unknown amount of samples per frame %d.\n",q->samples_per_frame);
940
-        return -1;
940
+        return AVERROR_INVALIDDATA;
941 941
     }
942 942
 
943 943
     if (q->delay != 0x88E) {
944 944
         av_log(avctx,AV_LOG_ERROR,"Unknown amount of delay %x != 0x88E.\n",q->delay);
945
-        return -1;
945
+        return AVERROR_INVALIDDATA;
946 946
     }
947 947
 
948 948
     if (q->codingMode == STEREO) {
... ...
@@ -951,17 +965,17 @@ static av_cold int atrac3_decode_init(AVCodecContext *avctx)
951 951
         av_log(avctx,AV_LOG_DEBUG,"Joint stereo detected.\n");
952 952
     } else {
953 953
         av_log(avctx,AV_LOG_ERROR,"Unknown channel coding mode %x!\n",q->codingMode);
954
-        return -1;
954
+        return AVERROR_INVALIDDATA;
955 955
     }
956 956
 
957 957
     if (avctx->channels <= 0 || avctx->channels > 2 /*|| ((avctx->channels * 1024) != q->samples_per_frame)*/) {
958 958
         av_log(avctx,AV_LOG_ERROR,"Channel configuration error!\n");
959
-        return -1;
959
+        return AVERROR(EINVAL);
960 960
     }
961 961
 
962 962
 
963 963
     if(avctx->block_align >= UINT_MAX/2)
964
-        return -1;
964
+        return AVERROR(EINVAL);
965 965
 
966 966
     /* Pad the data buffer with FF_INPUT_BUFFER_PADDING_SIZE,
967 967
      * this is for the bitstream reader. */
... ...
@@ -981,7 +995,16 @@ static av_cold int atrac3_decode_init(AVCodecContext *avctx)
981 981
         vlcs_initialized = 1;
982 982
     }
983 983
 
984
-    init_atrac3_transforms(q);
984
+    if (avctx->request_sample_fmt == AV_SAMPLE_FMT_FLT)
985
+        avctx->sample_fmt = AV_SAMPLE_FMT_FLT;
986
+    else
987
+        avctx->sample_fmt = AV_SAMPLE_FMT_S16;
988
+
989
+    if ((ret = init_atrac3_transforms(q, avctx->sample_fmt == AV_SAMPLE_FMT_FLT))) {
990
+        av_log(avctx, AV_LOG_ERROR, "Error initializing MDCT\n");
991
+        av_freep(&q->decoded_bytes_buffer);
992
+        return ret;
993
+    }
985 994
 
986 995
     atrac_generate_tables();
987 996
 
... ...
@@ -1007,14 +1030,23 @@ static av_cold int atrac3_decode_init(AVCodecContext *avctx)
1007 1007
     }
1008 1008
 
1009 1009
     dsputil_init(&dsp, avctx);
1010
+    ff_fmt_convert_init(&q->fmt_conv, avctx);
1010 1011
 
1011 1012
     q->pUnits = av_mallocz(sizeof(channel_unit)*q->channels);
1012 1013
     if (!q->pUnits) {
1013
-        av_free(q->decoded_bytes_buffer);
1014
+        atrac3_decode_close(avctx);
1014 1015
         return AVERROR(ENOMEM);
1015 1016
     }
1016 1017
 
1017
-    avctx->sample_fmt = AV_SAMPLE_FMT_S16;
1018
+    if (avctx->channels > 1 || avctx->sample_fmt == AV_SAMPLE_FMT_S16) {
1019
+        q->outSamples[0] = av_mallocz(SAMPLES_PER_FRAME * avctx->channels * sizeof(*q->outSamples[0]));
1020
+        q->outSamples[1] = q->outSamples[0] + SAMPLES_PER_FRAME;
1021
+        if (!q->outSamples[0]) {
1022
+            atrac3_decode_close(avctx);
1023
+            return AVERROR(ENOMEM);
1024
+        }
1025
+    }
1026
+
1018 1027
     return 0;
1019 1028
 }
1020 1029
 
... ...
@@ -1028,5 +1060,6 @@ AVCodec ff_atrac3_decoder =
1028 1028
     .init = atrac3_decode_init,
1029 1029
     .close = atrac3_decode_close,
1030 1030
     .decode = atrac3_decode_frame,
1031
+    .capabilities = CODEC_CAP_SUBFRAMES,
1031 1032
     .long_name = NULL_IF_CONFIG_SMALL("Atrac 3 (Adaptive TRansform Acoustic Coding 3)"),
1032 1033
 };
... ...
@@ -39,6 +39,8 @@
39 39
 
40 40
 extern const uint16_t ff_wma_critical_freqs[25];
41 41
 
42
+static float quant_table[95];
43
+
42 44
 #define MAX_CHANNELS 2
43 45
 #define BINK_BLOCK_MAX_SIZE (MAX_CHANNELS << 11)
44 46
 
... ...
@@ -56,8 +58,11 @@ typedef struct {
56 56
     unsigned int *bands;
57 57
     float root;
58 58
     DECLARE_ALIGNED(32, FFTSample, coeffs)[BINK_BLOCK_MAX_SIZE];
59
-    DECLARE_ALIGNED(16, short, previous)[BINK_BLOCK_MAX_SIZE / 16];  ///< coeffs from previous audio block
59
+    DECLARE_ALIGNED(16, int16_t, previous)[BINK_BLOCK_MAX_SIZE / 16];  ///< coeffs from previous audio block
60
+    DECLARE_ALIGNED(16, int16_t, current)[BINK_BLOCK_MAX_SIZE / 16];
60 61
     float *coeffs_ptr[MAX_CHANNELS]; ///< pointers to the coeffs arrays for float_to_int16_interleave
62
+    float *prev_ptr[MAX_CHANNELS];   ///< pointers to the overlap points in the coeffs array
63
+    uint8_t *packet_buffer;
61 64
     union {
62 65
         RDFTContext rdft;
63 66
         DCTContext dct;
... ...
@@ -107,6 +112,10 @@ static av_cold int decode_init(AVCodecContext *avctx)
107 107
     s->block_size    = (s->frame_len - s->overlap_len) * s->channels;
108 108
     sample_rate_half = (sample_rate + 1) / 2;
109 109
     s->root          = 2.0 / sqrt(s->frame_len);
110
+    for (i = 0; i < 95; i++) {
111
+        /* constant is result of 0.066399999/log10(M_E) */
112
+        quant_table[i] = expf(i * 0.15289164787221953823f) * s->root;
113
+    }
110 114
 
111 115
     /* calculate number of bands */
112 116
     for (s->num_bands = 1; s->num_bands < 25; s->num_bands++)
... ...
@@ -126,8 +135,10 @@ static av_cold int decode_init(AVCodecContext *avctx)
126 126
     s->first = 1;
127 127
     avctx->sample_fmt = AV_SAMPLE_FMT_S16;
128 128
 
129
-    for (i = 0; i < s->channels; i++)
129
+    for (i = 0; i < s->channels; i++) {
130 130
         s->coeffs_ptr[i] = s->coeffs + i * s->frame_len;
131
+        s->prev_ptr[i]   = s->coeffs_ptr[i] + s->frame_len - s->overlap_len;
132
+    }
131 133
 
132 134
     if (CONFIG_BINKAUDIO_RDFT_DECODER && avctx->codec->id == CODEC_ID_BINKAUDIO_RDFT)
133 135
         ff_rdft_init(&s->trans.rdft, frame_len_bits, DFT_C2R);
... ...
@@ -152,11 +163,18 @@ static const uint8_t rle_length_tab[16] = {
152 152
     2, 3, 4, 5, 6, 8, 9, 10, 11, 12, 13, 14, 15, 16, 32, 64
153 153
 };
154 154
 
155
+#define GET_BITS_SAFE(out, nbits) do {  \
156
+    if (get_bits_left(gb) < nbits)      \
157
+        return AVERROR_INVALIDDATA;     \
158
+    out = get_bits(gb, nbits);          \
159
+} while (0)
160
+
155 161
 /**
156 162
  * Decode Bink Audio block
157 163
  * @param[out] out Output buffer (must contain s->block_size elements)
164
+ * @return 0 on success, negative error code on failure
158 165
  */
159
-static void decode_block(BinkAudioContext *s, short *out, int use_dct)
166
+static int decode_block(BinkAudioContext *s, int16_t *out, int use_dct)
160 167
 {
161 168
     int ch, i, j, k;
162 169
     float q, quant[25];
... ...
@@ -169,17 +187,22 @@ static void decode_block(BinkAudioContext *s, short *out, int use_dct)
169 169
     for (ch = 0; ch < s->channels; ch++) {
170 170
         FFTSample *coeffs = s->coeffs_ptr[ch];
171 171
         if (s->version_b) {
172
+            if (get_bits_left(gb) < 64)
173
+                return AVERROR_INVALIDDATA;
172 174
             coeffs[0] = av_int2flt(get_bits(gb, 32)) * s->root;
173 175
             coeffs[1] = av_int2flt(get_bits(gb, 32)) * s->root;
174 176
         } else {
177
+            if (get_bits_left(gb) < 58)
178
+                return AVERROR_INVALIDDATA;
175 179
             coeffs[0] = get_float(gb) * s->root;
176 180
             coeffs[1] = get_float(gb) * s->root;
177 181
         }
178 182
 
183
+        if (get_bits_left(gb) < s->num_bands * 8)
184
+            return AVERROR_INVALIDDATA;
179 185
         for (i = 0; i < s->num_bands; i++) {
180
-            /* constant is result of 0.066399999/log10(M_E) */
181 186
             int value = get_bits(gb, 8);
182
-            quant[i] = expf(FFMIN(value, 95) * 0.15289164787221953823f) * s->root;
187
+            quant[i]  = quant_table[FFMIN(value, 95)];
183 188
         }
184 189
 
185 190
         k = 0;
... ...
@@ -190,15 +213,20 @@ static void decode_block(BinkAudioContext *s, short *out, int use_dct)
190 190
         while (i < s->frame_len) {
191 191
             if (s->version_b) {
192 192
                 j = i + 16;
193
-            } else if (get_bits1(gb)) {
194
-                j = i + rle_length_tab[get_bits(gb, 4)] * 8;
195 193
             } else {
196
-                j = i + 8;
194
+                int v;
195
+                GET_BITS_SAFE(v, 1);
196
+                if (v) {
197
+                    GET_BITS_SAFE(v, 4);
198
+                    j = i + rle_length_tab[v] * 8;
199
+                } else {
200
+                    j = i + 8;
201
+                }
197 202
             }
198 203
 
199 204
             j = FFMIN(j, s->frame_len);
200 205
 
201
-            width = get_bits(gb, 4);
206
+            GET_BITS_SAFE(width, 4);
202 207
             if (width == 0) {
203 208
                 memset(coeffs + i, 0, (j - i) * sizeof(*coeffs));
204 209
                 i = j;
... ...
@@ -208,9 +236,11 @@ static void decode_block(BinkAudioContext *s, short *out, int use_dct)
208 208
                 while (i < j) {
209 209
                     if (s->bands[k] == i)
210 210
                         q = quant[k++];
211
-                    coeff = get_bits(gb, width);
211
+                    GET_BITS_SAFE(coeff, width);
212 212
                     if (coeff) {
213
-                        if (get_bits1(gb))
213
+                        int v;
214
+                        GET_BITS_SAFE(v, 1);
215
+                        if (v)
214 216
                             coeffs[i] = -q * coeff;
215 217
                         else
216 218
                             coeffs[i] =  q * coeff;
... ...
@@ -231,8 +261,12 @@ static void decode_block(BinkAudioContext *s, short *out, int use_dct)
231 231
             s->trans.rdft.rdft_calc(&s->trans.rdft, coeffs);
232 232
     }
233 233
 
234
+    s->fmt_conv.float_to_int16_interleave(s->current,
235
+                                          (const float **)s->prev_ptr,
236
+                                          s->overlap_len, s->channels);
234 237
     s->fmt_conv.float_to_int16_interleave(out, (const float **)s->coeffs_ptr,
235
-                                          s->frame_len, s->channels);
238
+                                          s->frame_len - s->overlap_len,
239
+                                          s->channels);
236 240
 
237 241
     if (!s->first) {
238 242
         int count = s->overlap_len * s->channels;
... ...
@@ -242,16 +276,19 @@ static void decode_block(BinkAudioContext *s, short *out, int use_dct)
242 242
         }
243 243
     }
244 244
 
245
-    memcpy(s->previous, out + s->block_size,
246
-           s->overlap_len * s->channels * sizeof(*out));
245
+    memcpy(s->previous, s->current,
246
+           s->overlap_len * s->channels * sizeof(*s->previous));
247 247
 
248 248
     s->first = 0;
249
+
250
+    return 0;
249 251
 }
250 252
 
251 253
 static av_cold int decode_end(AVCodecContext *avctx)
252 254
 {
253 255
     BinkAudioContext * s = avctx->priv_data;
254 256
     av_freep(&s->bands);
257
+    av_freep(&s->packet_buffer);
255 258
     if (CONFIG_BINKAUDIO_RDFT_DECODER && avctx->codec->id == CODEC_ID_BINKAUDIO_RDFT)
256 259
         ff_rdft_end(&s->trans.rdft);
257 260
     else if (CONFIG_BINKAUDIO_DCT_DECODER)
... ...
@@ -270,25 +307,47 @@ static int decode_frame(AVCodecContext *avctx,
270 270
                         AVPacket *avpkt)
271 271
 {
272 272
     BinkAudioContext *s = avctx->priv_data;
273
-    const uint8_t *buf  = avpkt->data;
274
-    int buf_size        = avpkt->size;
275
-    short *samples      = data;
276
-    short *samples_end  = (short*)((uint8_t*)data + *data_size);
277
-    int reported_size;
273
+    int16_t *samples      = data;
278 274
     GetBitContext *gb = &s->gb;
275
+    int out_size, consumed = 0;
276
+
277
+    if (!get_bits_left(gb)) {
278
+        uint8_t *buf;
279
+        /* handle end-of-stream */
280
+        if (!avpkt->size) {
281
+            *data_size = 0;
282
+            return 0;
283
+        }
284
+        if (avpkt->size < 4) {
285
+            av_log(avctx, AV_LOG_ERROR, "Packet is too small\n");
286
+            return AVERROR_INVALIDDATA;
287
+        }
288
+        buf = av_realloc(s->packet_buffer, avpkt->size + FF_INPUT_BUFFER_PADDING_SIZE);
289
+        if (!buf)
290
+            return AVERROR(ENOMEM);
291
+        s->packet_buffer = buf;
292
+        memcpy(s->packet_buffer, avpkt->data, avpkt->size);
293
+        init_get_bits(gb, s->packet_buffer, avpkt->size * 8);
294
+        consumed = avpkt->size;
295
+
296
+        /* skip reported size */
297
+        skip_bits_long(gb, 32);
298
+    }
279 299
 
280
-    init_get_bits(gb, buf, buf_size * 8);
300
+    out_size = s->block_size * av_get_bytes_per_sample(avctx->sample_fmt);
301
+    if (*data_size < out_size) {
302
+        av_log(avctx, AV_LOG_ERROR, "Output buffer is too small\n");
303
+        return AVERROR(EINVAL);
304
+    }
281 305
 
282
-    reported_size = get_bits_long(gb, 32);
283
-    while (get_bits_count(gb) / 8 < buf_size &&
284
-           samples + s->block_size <= samples_end) {
285
-        decode_block(s, samples, avctx->codec->id == CODEC_ID_BINKAUDIO_DCT);
286
-        samples += s->block_size;
287
-        get_bits_align32(gb);
306
+    if (decode_block(s, samples, avctx->codec->id == CODEC_ID_BINKAUDIO_DCT)) {
307
+        av_log(avctx, AV_LOG_ERROR, "Incomplete packet\n");
308
+        return AVERROR_INVALIDDATA;
288 309
     }
310
+    get_bits_align32(gb);
289 311
 
290
-    *data_size = FFMIN(reported_size, (uint8_t*)samples - (uint8_t*)data);
291
-    return buf_size;
312
+    *data_size = out_size;
313
+    return consumed;
292 314
 }
293 315
 
294 316
 AVCodec ff_binkaudio_rdft_decoder = {
... ...
@@ -299,6 +358,7 @@ AVCodec ff_binkaudio_rdft_decoder = {
299 299
     .init           = decode_init,
300 300
     .close          = decode_end,
301 301
     .decode         = decode_frame,
302
+    .capabilities   = CODEC_CAP_DELAY,
302 303
     .long_name = NULL_IF_CONFIG_SMALL("Bink Audio (RDFT)")
303 304
 };
304 305
 
... ...
@@ -310,5 +370,6 @@ AVCodec ff_binkaudio_dct_decoder = {
310 310
     .init           = decode_init,
311 311
     .close          = decode_end,
312 312
     .decode         = decode_frame,
313
+    .capabilities   = CODEC_CAP_DELAY,
313 314
     .long_name = NULL_IF_CONFIG_SMALL("Bink Audio (DCT)")
314 315
 };
... ...
@@ -42,12 +42,7 @@
42 42
  * available.
43 43
  */
44 44
 
45
-#include <math.h>
46
-#include <stddef.h>
47
-#include <stdio.h>
48
-
49 45
 #include "libavutil/lfg.h"
50
-#include "libavutil/random_seed.h"
51 46
 #include "avcodec.h"
52 47
 #include "get_bits.h"
53 48
 #include "dsputil.h"
... ...
@@ -124,7 +119,7 @@ typedef struct cook {
124 124
     void (* interpolate) (struct cook *q, float* buffer,
125 125
                           int gain_index, int gain_index_next);
126 126
 
127
-    void (* saturate_output) (struct cook *q, int chan, int16_t *out);
127
+    void (* saturate_output) (struct cook *q, int chan, float *out);
128 128
 
129 129
     AVCodecContext*     avctx;
130 130
     GetBitContext       gb;
... ...
@@ -217,11 +212,11 @@ static av_cold int init_cook_vlc_tables(COOKContext *q) {
217 217
 }
218 218
 
219 219
 static av_cold int init_cook_mlt(COOKContext *q) {
220
-    int j;
220
+    int j, ret;
221 221
     int mlt_size = q->samples_per_channel;
222 222
 
223
-    if ((q->mlt_window = av_malloc(sizeof(float)*mlt_size)) == 0)
224
-      return -1;
223
+    if ((q->mlt_window = av_malloc(mlt_size * sizeof(*q->mlt_window))) == 0)
224
+        return AVERROR(ENOMEM);
225 225
 
226 226
     /* Initialize the MLT window: simple sine window. */
227 227
     ff_sine_window_init(q->mlt_window, mlt_size);
... ...
@@ -229,9 +224,9 @@ static av_cold int init_cook_mlt(COOKContext *q) {
229 229
         q->mlt_window[j] *= sqrt(2.0 / q->samples_per_channel);
230 230
 
231 231
     /* Initialize the MDCT. */
232
-    if (ff_mdct_init(&q->mdct_ctx, av_log2(mlt_size)+1, 1, 1.0)) {
233
-      av_free(q->mlt_window);
234
-      return -1;
232
+    if ((ret = ff_mdct_init(&q->mdct_ctx, av_log2(mlt_size)+1, 1, 1.0/32768.0))) {
233
+        av_free(q->mlt_window);
234
+        return ret;
235 235
     }
236 236
     av_log(q->avctx,AV_LOG_DEBUG,"MDCT initialized, order = %d.\n",
237 237
            av_log2(mlt_size)+1);
... ...
@@ -410,9 +405,9 @@ static void categorize(COOKContext *q, COOKSubpacket *p, int* quant_index_table,
410 410
         //av_log(q->avctx, AV_LOG_ERROR, "bits_left = %d\n",bits_left);
411 411
     }
412 412
 
413
-    memset(&exp_index1,0,102*sizeof(int));
414
-    memset(&exp_index2,0,102*sizeof(int));
415
-    memset(&tmp_categorize_array,0,128*2*sizeof(int));
413
+    memset(&exp_index1,           0, sizeof(exp_index1));
414
+    memset(&exp_index2,           0, sizeof(exp_index2));
415
+    memset(&tmp_categorize_array, 0, sizeof(tmp_categorize_array));
416 416
 
417 417
     bias=-32;
418 418
 
... ...
@@ -633,8 +628,8 @@ static void mono_decode(COOKContext *q, COOKSubpacket *p, float* mlt_buffer) {
633 633
     int quant_index_table[102];
634 634
     int category[128];
635 635
 
636
-    memset(&category, 0, 128*sizeof(int));
637
-    memset(&category_index, 0, 128*sizeof(int));
636
+    memset(&category,       0, sizeof(category));
637
+    memset(&category_index, 0, sizeof(category_index));
638 638
 
639 639
     decode_envelope(q, p, quant_index_table);
640 640
     q->num_vectors = get_bits(&q->gb,p->log2_numvector_size);
... ...
@@ -663,14 +658,12 @@ static void interpolate_float(COOKContext *q, float* buffer,
663 663
         for(i=0 ; i<q->gain_size_factor ; i++){
664 664
             buffer[i]*=fc1;
665 665
         }
666
-        return;
667 666
     } else {                                        //smooth gain
668 667
         fc2 = q->gain_table[11 + (gain_index_next-gain_index)];
669 668
         for(i=0 ; i<q->gain_size_factor ; i++){
670 669
             buffer[i]*=fc1;
671 670
             fc1*=fc2;
672 671
         }
673
-        return;
674 672
     }
675 673
 }
676 674
 
... ...
@@ -733,7 +726,8 @@ static void imlt_gain(COOKContext *q, float *inbuffer,
733 733
     }
734 734
 
735 735
     /* Save away the current to be previous block. */
736
-    memcpy(previous_buffer, buffer0, sizeof(float)*q->samples_per_channel);
736
+    memcpy(previous_buffer, buffer0,
737
+           q->samples_per_channel * sizeof(*previous_buffer));
737 738
 }
738 739
 
739 740
 
... ...
@@ -744,27 +738,24 @@ static void imlt_gain(COOKContext *q, float *inbuffer,
744 744
  * @param decouple_tab      decoupling array
745 745
  *
746 746
  */
747
+static void decouple_info(COOKContext *q, COOKSubpacket *p, int *decouple_tab)
748
+{
749
+    int i;
750
+    int vlc    = get_bits1(&q->gb);
751
+    int start  = cplband[p->js_subband_start];
752
+    int end    = cplband[p->subbands-1];
753
+    int length = end - start + 1;
747 754
 
748
-static void decouple_info(COOKContext *q, COOKSubpacket *p, int* decouple_tab){
749
-    int length, i;
750
-
751
-    if(get_bits1(&q->gb)) {
752
-        if(cplband[p->js_subband_start] > cplband[p->subbands-1]) return;
753
-
754
-        length = cplband[p->subbands-1] - cplband[p->js_subband_start] + 1;
755
-        for (i=0 ; i<length ; i++) {
756
-            decouple_tab[cplband[p->js_subband_start] + i] = get_vlc2(&q->gb, p->ccpl.table, p->ccpl.bits, 2);
757
-        }
755
+    if (start > end)
758 756
         return;
759
-    }
760
-
761
-    if(cplband[p->js_subband_start] > cplband[p->subbands-1]) return;
762 757
 
763
-    length = cplband[p->subbands-1] - cplband[p->js_subband_start] + 1;
764
-    for (i=0 ; i<length ; i++) {
765
-       decouple_tab[cplband[p->js_subband_start] + i] = get_bits(&q->gb, p->js_vlc_bits);
758
+    if (vlc) {
759
+        for (i = 0; i < length; i++)
760
+            decouple_tab[start + i] = get_vlc2(&q->gb, p->ccpl.table, p->ccpl.bits, 2);
761
+    } else {
762
+        for (i = 0; i < length; i++)
763
+            decouple_tab[start + i] = get_bits(&q->gb, p->js_vlc_bits);
766 764
     }
767
-    return;
768 765
 }
769 766
 
770 767
 /*
... ...
@@ -811,11 +802,11 @@ static void joint_decode(COOKContext *q, COOKSubpacket *p, float* mlt_buffer1,
811 811
     const float* cplscale;
812 812
 
813 813
     memset(decouple_tab, 0, sizeof(decouple_tab));
814
-    memset(decode_buffer, 0, sizeof(decode_buffer));
814
+    memset(decode_buffer, 0, sizeof(q->decode_buffer_0));
815 815
 
816 816
     /* Make sure the buffers are zeroed out. */
817
-    memset(mlt_buffer1,0, 1024*sizeof(float));
818
-    memset(mlt_buffer2,0, 1024*sizeof(float));
817
+    memset(mlt_buffer1, 0, 1024 * sizeof(*mlt_buffer1));
818
+    memset(mlt_buffer2, 0, 1024 * sizeof(*mlt_buffer2));
819 819
     decouple_info(q, p, decouple_tab);
820 820
     mono_decode(q, p, decode_buffer);
821 821
 
... ...
@@ -867,22 +858,18 @@ decode_bytes_and_gain(COOKContext *q, COOKSubpacket *p, const uint8_t *inbuffer,
867 867
 }
868 868
 
869 869
  /**
870
- * Saturate the output signal to signed 16bit integers.
870
+ * Saturate the output signal and interleave.
871 871
  *
872 872
  * @param q                 pointer to the COOKContext
873 873
  * @param chan              channel to saturate
874 874
  * @param out               pointer to the output vector
875 875
  */
876
-static void
877
-saturate_output_float (COOKContext *q, int chan, int16_t *out)
876
+static void saturate_output_float(COOKContext *q, int chan, float *out)
878 877
 {
879 878
     int j;
880 879
     float *output = q->mono_mdct_output + q->samples_per_channel;
881
-    /* Clip and convert floats to 16 bits.
882
-     */
883 880
     for (j = 0; j < q->samples_per_channel; j++) {
884
-        out[chan + q->nb_channels * j] =
885
-          av_clip_int16(lrintf(output[j]));
881
+        out[chan + q->nb_channels * j] = av_clipf(output[j], -1.0, 1.0);
886 882
     }
887 883
 }
888 884
 
... ...
@@ -902,7 +889,7 @@ saturate_output_float (COOKContext *q, int chan, int16_t *out)
902 902
 static inline void
903 903
 mlt_compensate_output(COOKContext *q, float *decode_buffer,
904 904
                       cook_gains *gains_ptr, float *previous_buffer,
905
-                      int16_t *out, int chan)
905
+                      float *out, int chan)
906 906
 {
907 907
     imlt_gain(q, decode_buffer, gains_ptr, previous_buffer);
908 908
     q->saturate_output (q, chan, out);
... ...
@@ -917,7 +904,9 @@ mlt_compensate_output(COOKContext *q, float *decode_buffer,
917 917
  * @param inbuffer          pointer to the inbuffer
918 918
  * @param outbuffer         pointer to the outbuffer
919 919
  */
920
-static void decode_subpacket(COOKContext *q, COOKSubpacket* p, const uint8_t *inbuffer, int16_t *outbuffer) {
920
+static void decode_subpacket(COOKContext *q, COOKSubpacket *p,
921
+                             const uint8_t *inbuffer, float *outbuffer)
922
+{
921 923
     int sub_packet_size = p->size;
922 924
     /* packet dump */
923 925
 //    for (i=0 ; i<sub_packet_size ; i++) {
... ...
@@ -966,13 +955,20 @@ static int cook_decode_frame(AVCodecContext *avctx,
966 966
     const uint8_t *buf = avpkt->data;
967 967
     int buf_size = avpkt->size;
968 968
     COOKContext *q = avctx->priv_data;
969
-    int i;
969
+    int i, out_size;
970 970
     int offset = 0;
971 971
     int chidx = 0;
972 972
 
973 973
     if (buf_size < avctx->block_align)
974 974
         return buf_size;
975 975
 
976
+    out_size = q->nb_channels * q->samples_per_channel *
977
+               av_get_bytes_per_sample(avctx->sample_fmt);
978
+    if (*data_size < out_size) {
979
+        av_log(avctx, AV_LOG_ERROR, "Output buffer is too small\n");
980
+        return AVERROR(EINVAL);
981
+    }
982
+
976 983
     /* estimate subpacket sizes */
977 984
     q->subpacket[0].size = avctx->block_align;
978 985
 
... ...
@@ -981,22 +977,21 @@ static int cook_decode_frame(AVCodecContext *avctx,
981 981
         q->subpacket[0].size -= q->subpacket[i].size + 1;
982 982
         if (q->subpacket[0].size < 0) {
983 983
             av_log(avctx,AV_LOG_DEBUG,"frame subpacket size total > avctx->block_align!\n");
984
-            return -1;
984
+            return AVERROR_INVALIDDATA;
985 985
         }
986 986
     }
987 987
 
988 988
     /* decode supbackets */
989
-    *data_size = 0;
990 989
     for(i=0;i<q->num_subpackets;i++){
991 990
         q->subpacket[i].bits_per_subpacket = (q->subpacket[i].size*8)>>q->subpacket[i].bits_per_subpdiv;
992 991
         q->subpacket[i].ch_idx = chidx;
993 992
         av_log(avctx,AV_LOG_DEBUG,"subpacket[%i] size %i js %i %i block_align %i\n",i,q->subpacket[i].size,q->subpacket[i].joint_stereo,offset,avctx->block_align);
994
-        decode_subpacket(q, &q->subpacket[i], buf + offset, (int16_t*)data);
993
+        decode_subpacket(q, &q->subpacket[i], buf + offset, data);
995 994
         offset += q->subpacket[i].size;
996 995
         chidx += q->subpacket[i].num_channels;
997 996
         av_log(avctx,AV_LOG_DEBUG,"subpacket[%i] %i %i\n",i,q->subpacket[i].size * 8,get_bits_count(&q->gb));
998 997
     }
999
-    *data_size = sizeof(int16_t) * q->nb_channels * q->samples_per_channel;
998
+    *data_size = out_size;
1000 999
 
1001 1000
     /* Discard the first two frames: no valid audio. */
1002 1001
     if (avctx->frame_number < 2) *data_size = 0;
... ...
@@ -1053,12 +1048,13 @@ static av_cold int cook_decode_init(AVCodecContext *avctx)
1053 1053
     int extradata_size = avctx->extradata_size;
1054 1054
     int s = 0;
1055 1055
     unsigned int channel_mask = 0;
1056
+    int ret;
1056 1057
     q->avctx = avctx;
1057 1058
 
1058 1059
     /* Take care of the codec specific extradata. */
1059 1060
     if (extradata_size <= 0) {
1060 1061
         av_log(avctx,AV_LOG_ERROR,"Necessary extradata missing!\n");
1061
-        return -1;
1062
+        return AVERROR_INVALIDDATA;
1062 1063
     }
1063 1064
     av_log(avctx,AV_LOG_DEBUG,"codecdata_length=%d\n",avctx->extradata_size);
1064 1065
 
... ...
@@ -1103,7 +1099,7 @@ static av_cold int cook_decode_init(AVCodecContext *avctx)
1103 1103
             case MONO:
1104 1104
                 if (q->nb_channels != 1) {
1105 1105
                     av_log_ask_for_sample(avctx, "Container channels != 1.\n");
1106
-                    return -1;
1106
+                    return AVERROR_PATCHWELCOME;
1107 1107
                 }
1108 1108
                 av_log(avctx,AV_LOG_DEBUG,"MONO\n");
1109 1109
                 break;
... ...
@@ -1117,7 +1113,7 @@ static av_cold int cook_decode_init(AVCodecContext *avctx)
1117 1117
             case JOINT_STEREO:
1118 1118
                 if (q->nb_channels != 2) {
1119 1119
                     av_log_ask_for_sample(avctx, "Container channels != 2.\n");
1120
-                    return -1;
1120
+                    return AVERROR_PATCHWELCOME;
1121 1121
                 }
1122 1122
                 av_log(avctx,AV_LOG_DEBUG,"JOINT_STEREO\n");
1123 1123
                 if (avctx->extradata_size >= 16){
... ...
@@ -1155,12 +1151,12 @@ static av_cold int cook_decode_init(AVCodecContext *avctx)
1155 1155
                 break;
1156 1156
             default:
1157 1157
                 av_log_ask_for_sample(avctx, "Unknown Cook version.\n");
1158
-                return -1;
1158
+                return AVERROR_PATCHWELCOME;
1159 1159
         }
1160 1160
 
1161 1161
         if(s > 1 && q->subpacket[s].samples_per_channel != q->samples_per_channel) {
1162 1162
             av_log(avctx,AV_LOG_ERROR,"different number of samples per channel!\n");
1163
-            return -1;
1163
+            return AVERROR_INVALIDDATA;
1164 1164
         } else
1165 1165
             q->samples_per_channel = q->subpacket[0].samples_per_channel;
1166 1166
 
... ...
@@ -1171,18 +1167,18 @@ static av_cold int cook_decode_init(AVCodecContext *avctx)
1171 1171
         /* Try to catch some obviously faulty streams, othervise it might be exploitable */
1172 1172
         if (q->subpacket[s].total_subbands > 53) {
1173 1173
             av_log_ask_for_sample(avctx, "total_subbands > 53\n");
1174
-            return -1;
1174
+            return AVERROR_PATCHWELCOME;
1175 1175
         }
1176 1176
 
1177 1177
         if ((q->subpacket[s].js_vlc_bits > 6) || (q->subpacket[s].js_vlc_bits < 2*q->subpacket[s].joint_stereo)) {
1178 1178
             av_log(avctx,AV_LOG_ERROR,"js_vlc_bits = %d, only >= %d and <= 6 allowed!\n",
1179 1179
                    q->subpacket[s].js_vlc_bits, 2*q->subpacket[s].joint_stereo);
1180
-            return -1;
1180
+            return AVERROR_INVALIDDATA;
1181 1181
         }
1182 1182
 
1183 1183
         if (q->subpacket[s].subbands > 50) {
1184 1184
             av_log_ask_for_sample(avctx, "subbands > 50\n");
1185
-            return -1;
1185
+            return AVERROR_PATCHWELCOME;
1186 1186
         }
1187 1187
         q->subpacket[s].gains1.now      = q->subpacket[s].gain_1;
1188 1188
         q->subpacket[s].gains1.previous = q->subpacket[s].gain_2;
... ...
@@ -1193,7 +1189,7 @@ static av_cold int cook_decode_init(AVCodecContext *avctx)
1193 1193
         s++;
1194 1194
         if (s > MAX_SUBPACKETS) {
1195 1195
             av_log_ask_for_sample(avctx, "Too many subpackets > 5\n");
1196
-            return -1;
1196
+            return AVERROR_PATCHWELCOME;
1197 1197
         }
1198 1198
     }
1199 1199
     /* Generate tables */
... ...
@@ -1201,12 +1197,12 @@ static av_cold int cook_decode_init(AVCodecContext *avctx)
1201 1201
     init_gain_table(q);
1202 1202
     init_cplscales_table(q);
1203 1203
 
1204
-    if (init_cook_vlc_tables(q) != 0)
1205
-        return -1;
1204
+    if ((ret = init_cook_vlc_tables(q)))
1205
+        return ret;
1206 1206
 
1207 1207
 
1208 1208
     if(avctx->block_align >= UINT_MAX/2)
1209
-        return -1;
1209
+        return AVERROR(EINVAL);
1210 1210
 
1211 1211
     /* Pad the databuffer with:
1212 1212
        DECODE_BYTES_PAD1 or DECODE_BYTES_PAD2 for decode_bytes(),
... ...
@@ -1216,11 +1212,11 @@ static av_cold int cook_decode_init(AVCodecContext *avctx)
1216 1216
                      + DECODE_BYTES_PAD1(avctx->block_align)
1217 1217
                      + FF_INPUT_BUFFER_PADDING_SIZE);
1218 1218
     if (q->decoded_bytes_buffer == NULL)
1219
-        return -1;
1219
+        return AVERROR(ENOMEM);
1220 1220
 
1221 1221
     /* Initialize transform. */
1222
-    if ( init_cook_mlt(q) != 0 )
1223
-        return -1;
1222
+    if ((ret = init_cook_mlt(q)))
1223
+        return ret;
1224 1224
 
1225 1225
     /* Initialize COOK signal arithmetic handling */
1226 1226
     if (1) {
... ...
@@ -1237,10 +1233,10 @@ static av_cold int cook_decode_init(AVCodecContext *avctx)
1237 1237
         av_log_ask_for_sample(avctx,
1238 1238
                               "unknown amount of samples_per_channel = %d\n",
1239 1239
                               q->samples_per_channel);
1240
-        return -1;
1240
+        return AVERROR_PATCHWELCOME;
1241 1241
     }
1242 1242
 
1243
-    avctx->sample_fmt = AV_SAMPLE_FMT_S16;
1243
+    avctx->sample_fmt = AV_SAMPLE_FMT_FLT;
1244 1244
     if (channel_mask)
1245 1245
         avctx->channel_layout = channel_mask;
1246 1246
     else
... ...
@@ -528,15 +528,15 @@ static int dca_parse_frame_header(DCAContext * s)
528 528
     s->sample_blocks     = get_bits(&s->gb, 7) + 1;
529 529
     s->frame_size        = get_bits(&s->gb, 14) + 1;
530 530
     if (s->frame_size < 95)
531
-        return -1;
531
+        return AVERROR_INVALIDDATA;
532 532
     s->amode             = get_bits(&s->gb, 6);
533 533
     s->sample_rate       = dca_sample_rates[get_bits(&s->gb, 4)];
534 534
     if (!s->sample_rate)
535
-        return -1;
535
+        return AVERROR_INVALIDDATA;
536 536
     s->bit_rate_index    = get_bits(&s->gb, 5);
537 537
     s->bit_rate          = dca_bit_rates[s->bit_rate_index];
538 538
     if (!s->bit_rate)
539
-        return -1;
539
+        return AVERROR_INVALIDDATA;
540 540
 
541 541
     s->downmix           = get_bits(&s->gb, 1);
542 542
     s->dynrange          = get_bits(&s->gb, 1);
... ...
@@ -626,7 +626,7 @@ static int dca_subframe_header(DCAContext * s, int base_channel, int block_index
626 626
     int j, k;
627 627
 
628 628
     if (get_bits_left(&s->gb) < 0)
629
-        return -1;
629
+        return AVERROR_INVALIDDATA;
630 630
 
631 631
     if (!base_channel) {
632 632
         s->subsubframes[s->current_subframe] = get_bits(&s->gb, 2) + 1;
... ...
@@ -658,7 +658,7 @@ static int dca_subframe_header(DCAContext * s, int base_channel, int block_index
658 658
             else if (s->bitalloc_huffman[j] == 7) {
659 659
                 av_log(s->avctx, AV_LOG_ERROR,
660 660
                        "Invalid bit allocation index\n");
661
-                return -1;
661
+                return AVERROR_INVALIDDATA;
662 662
             } else {
663 663
                 s->bitalloc[j][k] =
664 664
                     get_bitalloc(&s->gb, &dca_bitalloc_index, s->bitalloc_huffman[j]);
... ...
@@ -667,7 +667,7 @@ static int dca_subframe_header(DCAContext * s, int base_channel, int block_index
667 667
             if (s->bitalloc[j][k] > 26) {
668 668
 //                 av_log(s->avctx,AV_LOG_DEBUG,"bitalloc index [%i][%i] too big (%i)\n",
669 669
 //                          j, k, s->bitalloc[j][k]);
670
-                return -1;
670
+                return AVERROR_INVALIDDATA;
671 671
             }
672 672
         }
673 673
     }
... ...
@@ -685,7 +685,7 @@ static int dca_subframe_header(DCAContext * s, int base_channel, int block_index
685 685
     }
686 686
 
687 687
     if (get_bits_left(&s->gb) < 0)
688
-        return -1;
688
+        return AVERROR_INVALIDDATA;
689 689
 
690 690
     for (j = base_channel; j < s->prim_channels; j++) {
691 691
         const uint32_t *scale_table;
... ...
@@ -723,7 +723,7 @@ static int dca_subframe_header(DCAContext * s, int base_channel, int block_index
723 723
     }
724 724
 
725 725
     if (get_bits_left(&s->gb) < 0)
726
-        return -1;
726
+        return AVERROR_INVALIDDATA;
727 727
 
728 728
     /* Scale factors for joint subband coding */
729 729
     for (j = base_channel; j < s->prim_channels; j++) {
... ...
@@ -1055,7 +1055,7 @@ static int decode_blockcode(int code, int levels, int *values)
1055 1055
         return 0;
1056 1056
     else {
1057 1057
         av_log(NULL, AV_LOG_ERROR, "ERROR: block code look-up failed\n");
1058
-        return -1;
1058
+        return AVERROR_INVALIDDATA;
1059 1059
     }
1060 1060
 }
1061 1061
 #endif
... ...
@@ -1096,7 +1096,7 @@ static int dca_subsubframe(DCAContext * s, int base_channel, int block_index)
1096 1096
 
1097 1097
     for (k = base_channel; k < s->prim_channels; k++) {
1098 1098
         if (get_bits_left(&s->gb) < 0)
1099
-            return -1;
1099
+            return AVERROR_INVALIDDATA;
1100 1100
 
1101 1101
         for (l = 0; l < s->vq_start_subband[k]; l++) {
1102 1102
             int m;
... ...
@@ -1275,12 +1275,13 @@ static int dca_subframe_footer(DCAContext * s, int base_channel)
1275 1275
 
1276 1276
 static int dca_decode_block(DCAContext * s, int base_channel, int block_index)
1277 1277
 {
1278
+    int ret;
1278 1279
 
1279 1280
     /* Sanity check */
1280 1281
     if (s->current_subframe >= s->subframes) {
1281 1282
         av_log(s->avctx, AV_LOG_DEBUG, "check failed: %i>%i",
1282 1283
                s->current_subframe, s->subframes);
1283
-        return -1;
1284
+        return AVERROR_INVALIDDATA;
1284 1285
     }
1285 1286
 
1286 1287
     if (!s->current_subsubframe) {
... ...
@@ -1288,16 +1289,16 @@ static int dca_decode_block(DCAContext * s, int base_channel, int block_index)
1288 1288
         av_log(s->avctx, AV_LOG_DEBUG, "DSYNC dca_subframe_header\n");
1289 1289
 #endif
1290 1290
         /* Read subframe header */
1291
-        if (dca_subframe_header(s, base_channel, block_index))
1292
-            return -1;
1291
+        if ((ret = dca_subframe_header(s, base_channel, block_index)))
1292
+            return ret;
1293 1293
     }
1294 1294
 
1295 1295
     /* Read subsubframe */
1296 1296
 #ifdef TRACE
1297 1297
     av_log(s->avctx, AV_LOG_DEBUG, "DSYNC dca_subsubframe\n");
1298 1298
 #endif
1299
-    if (dca_subsubframe(s, base_channel, block_index))
1300
-        return -1;
1299
+    if ((ret = dca_subsubframe(s, base_channel, block_index)))
1300
+        return ret;
1301 1301
 
1302 1302
     /* Update state */
1303 1303
     s->current_subsubframe++;
... ...
@@ -1310,8 +1311,8 @@ static int dca_decode_block(DCAContext * s, int base_channel, int block_index)
1310 1310
         av_log(s->avctx, AV_LOG_DEBUG, "DSYNC dca_subframe_footer\n");
1311 1311
 #endif
1312 1312
         /* Read subframe footer */
1313
-        if (dca_subframe_footer(s, base_channel))
1314
-            return -1;
1313
+        if ((ret = dca_subframe_footer(s, base_channel)))
1314
+            return ret;
1315 1315
     }
1316 1316
 
1317 1317
     return 0;
... ...
@@ -1354,7 +1355,7 @@ static int dca_convert_bitstream(const uint8_t * src, int src_size, uint8_t * ds
1354 1354
         flush_put_bits(&pb);
1355 1355
         return (put_bits_count(&pb) + 7) >> 3;
1356 1356
     default:
1357
-        return -1;
1357
+        return AVERROR_INVALIDDATA;
1358 1358
     }
1359 1359
 }
1360 1360
 
... ...
@@ -1637,7 +1638,7 @@ static int dca_decode_frame(AVCodecContext * avctx,
1637 1637
 
1638 1638
     int lfe_samples;
1639 1639
     int num_core_channels = 0;
1640
-    int i;
1640
+    int i, ret;
1641 1641
     float   *samples_flt = data;
1642 1642
     int16_t *samples_s16 = data;
1643 1643
     int out_size;
... ...
@@ -1650,16 +1651,15 @@ static int dca_decode_frame(AVCodecContext * avctx,
1650 1650
 
1651 1651
     s->dca_buffer_size = dca_convert_bitstream(buf, buf_size, s->dca_buffer,
1652 1652
                                                DCA_MAX_FRAME_SIZE + DCA_MAX_EXSS_HEADER_SIZE);
1653
-    if (s->dca_buffer_size == -1) {
1653
+    if (s->dca_buffer_size == AVERROR_INVALIDDATA) {
1654 1654
         av_log(avctx, AV_LOG_ERROR, "Not a valid DCA frame\n");
1655
-        return -1;
1655
+        return AVERROR_INVALIDDATA;
1656 1656
     }
1657 1657
 
1658 1658
     init_get_bits(&s->gb, s->dca_buffer, s->dca_buffer_size * 8);
1659
-    if (dca_parse_frame_header(s) < 0) {
1659
+    if ((ret = dca_parse_frame_header(s)) < 0) {
1660 1660
         //seems like the frame is corrupt, try with the next one
1661
-        *data_size=0;
1662
-        return buf_size;
1661
+        return ret;
1663 1662
     }
1664 1663
     //set AVCodec values with parsed data
1665 1664
     avctx->sample_rate = s->sample_rate;
... ...
@@ -1669,7 +1669,10 @@ static int dca_decode_frame(AVCodecContext * avctx,
1669 1669
     s->profile = FF_PROFILE_DTS;
1670 1670
 
1671 1671
     for (i = 0; i < (s->sample_blocks / 8); i++) {
1672
-        dca_decode_block(s, 0, i);
1672
+        if ((ret = dca_decode_block(s, 0, i))) {
1673
+            av_log(avctx, AV_LOG_ERROR, "error decoding block\n");
1674
+            return ret;
1675
+        }
1673 1676
     }
1674 1677
 
1675 1678
     /* record number of core channels incase less than max channels are requested */
... ...
@@ -1725,7 +1728,10 @@ static int dca_decode_frame(AVCodecContext * avctx,
1725 1725
             dca_parse_audio_coding_header(s, s->xch_base_channel);
1726 1726
 
1727 1727
             for (i = 0; i < (s->sample_blocks / 8); i++) {
1728
-                dca_decode_block(s, s->xch_base_channel, i);
1728
+                if ((ret = dca_decode_block(s, s->xch_base_channel, i))) {
1729
+                    av_log(avctx, AV_LOG_ERROR, "error decoding XCh extension\n");
1730
+                    continue;
1731
+                }
1729 1732
             }
1730 1733
 
1731 1734
             s->xch_present = 1;
... ...
@@ -1799,7 +1805,7 @@ static int dca_decode_frame(AVCodecContext * avctx,
1799 1799
 
1800 1800
         if (channels > !!s->lfe &&
1801 1801
             s->channel_order_tab[channels - 1 - !!s->lfe] < 0)
1802
-            return -1;
1802
+            return AVERROR_INVALIDDATA;
1803 1803
 
1804 1804
         if (avctx->request_channels == 2 && s->prim_channels > 2) {
1805 1805
             channels = 2;
... ...
@@ -1812,7 +1818,7 @@ static int dca_decode_frame(AVCodecContext * avctx,
1812 1812
         }
1813 1813
     } else {
1814 1814
         av_log(avctx, AV_LOG_ERROR, "Non standard configuration %d !\n",s->amode);
1815
-        return -1;
1815
+        return AVERROR_INVALIDDATA;
1816 1816
     }
1817 1817
 
1818 1818
     if (avctx->channels != channels) {
... ...
@@ -1824,7 +1830,7 @@ static int dca_decode_frame(AVCodecContext * avctx,
1824 1824
     out_size = 256 / 8 * s->sample_blocks * channels *
1825 1825
                av_get_bytes_per_sample(avctx->sample_fmt);
1826 1826
     if (*data_size < out_size)
1827
-        return -1;
1827
+        return AVERROR(EINVAL);
1828 1828
     *data_size = out_size;
1829 1829
 
1830 1830
     /* filter to get final output */
... ...
@@ -26,6 +26,7 @@
26 26
 
27 27
 #include "avcodec.h"
28 28
 #include "bytestream.h"
29
+#include "mathops.h"
29 30
 
30 31
 
31 32
 typedef enum CinVideoBitmapIndex {
... ...
@@ -43,7 +44,6 @@ typedef struct CinVideoContext {
43 43
 } CinVideoContext;
44 44
 
45 45
 typedef struct CinAudioContext {
46
-    AVCodecContext *avctx;
47 46
     int initial_decode_frame;
48 47
     int delta;
49 48
 } CinAudioContext;
... ...
@@ -309,7 +309,11 @@ static av_cold int cinaudio_decode_init(AVCodecContext *avctx)
309 309
 {
310 310
     CinAudioContext *cin = avctx->priv_data;
311 311
 
312
-    cin->avctx = avctx;
312
+    if (avctx->channels != 1) {
313
+        av_log_ask_for_sample(avctx, "Number of channels is not supported\n");
314
+        return AVERROR_PATCHWELCOME;
315
+    }
316
+
313 317
     cin->initial_decode_frame = 1;
314 318
     cin->delta = 0;
315 319
     avctx->sample_fmt = AV_SAMPLE_FMT_S16;
... ...
@@ -322,29 +326,35 @@ static int cinaudio_decode_frame(AVCodecContext *avctx,
322 322
                                  AVPacket *avpkt)
323 323
 {
324 324
     const uint8_t *buf = avpkt->data;
325
-    int buf_size = avpkt->size;
326 325
     CinAudioContext *cin = avctx->priv_data;
327
-    const uint8_t *src = buf;
328
-    int16_t *samples = (int16_t *)data;
329
-
330
-    buf_size = FFMIN(buf_size, *data_size/2);
326
+    const uint8_t *buf_end = buf + avpkt->size;
327
+    int16_t *samples = data;
328
+    int delta, out_size;
329
+
330
+    out_size = (avpkt->size - cin->initial_decode_frame) *
331
+               av_get_bytes_per_sample(avctx->sample_fmt);
332
+    if (*data_size < out_size) {
333
+        av_log(avctx, AV_LOG_ERROR, "Output buffer is too small\n");
334
+        return AVERROR(EINVAL);
335
+    }
331 336
 
337
+    delta = cin->delta;
332 338
     if (cin->initial_decode_frame) {
333 339
         cin->initial_decode_frame = 0;
334
-        cin->delta = (int16_t)AV_RL16(src); src += 2;
335
-        *samples++ = cin->delta;
336
-        buf_size -= 2;
340
+        delta = sign_extend(AV_RL16(buf), 16);
341
+        buf += 2;
342
+        *samples++ = delta;
337 343
     }
338
-    while (buf_size > 0) {
339
-        cin->delta += cinaudio_delta16_table[*src++];
340
-        cin->delta = av_clip_int16(cin->delta);
341
-        *samples++ = cin->delta;
342
-        --buf_size;
344
+    while (buf < buf_end) {
345
+        delta += cinaudio_delta16_table[*buf++];
346
+        delta = av_clip_int16(delta);
347
+        *samples++ = delta;
343 348
     }
349
+    cin->delta = delta;
344 350
 
345
-    *data_size = (uint8_t *)samples - (uint8_t *)data;
351
+    *data_size = out_size;
346 352
 
347
-    return src - buf;
353
+    return avpkt->size;
348 354
 }
349 355
 
350 356
 
... ...
@@ -587,7 +587,8 @@ static int flac_decode_frame(AVCodecContext *avctx,
587 587
     bytes_read = (get_bits_count(&s->gb)+7)/8;
588 588
 
589 589
     /* check if allocated data size is large enough for output */
590
-    output_size = s->blocksize * s->channels * (s->is32 ? 4 : 2);
590
+    output_size = s->blocksize * s->channels *
591
+                  av_get_bytes_per_sample(avctx->sample_fmt);
591 592
     if (output_size > alloc_data_size) {
592 593
         av_log(s->avctx, AV_LOG_ERROR, "output data size is larger than "
593 594
                                        "allocated data size\n");
... ...
@@ -1815,7 +1815,7 @@ static av_always_inline void hl_decode_mb_predict_luma(H264Context *h, int mb_ty
1815 1815
                     static const uint8_t dc_mapping[16] = { 0*16, 1*16, 4*16, 5*16, 2*16, 3*16, 6*16, 7*16,
1816 1816
                                                             8*16, 9*16,12*16,13*16,10*16,11*16,14*16,15*16};
1817 1817
                     for(i = 0; i < 16; i++)
1818
-                        dctcoef_set(h->mb+p*256, pixel_shift, dc_mapping[i], dctcoef_get(h->mb_luma_dc[p], pixel_shift, i));
1818
+                        dctcoef_set(h->mb+(p*256 << pixel_shift), pixel_shift, dc_mapping[i], dctcoef_get(h->mb_luma_dc[p], pixel_shift, i));
1819 1819
                 }
1820 1820
             }
1821 1821
         }else
... ...
@@ -2033,7 +2033,7 @@ static av_always_inline void hl_decode_mb_internal(H264Context *h, int simple, i
2033 2033
                         }
2034 2034
                         if (chroma422) {
2035 2035
                             for(i=j*16+4; i<j*16+8; i++){
2036
-                                if(h->non_zero_count_cache[ scan8[i] ] || dctcoef_get(h->mb, pixel_shift, i*16))
2036
+                                if(h->non_zero_count_cache[ scan8[i+4] ] || dctcoef_get(h->mb, pixel_shift, i*16))
2037 2037
                                     idct_add   (dest[j-1] + block_offset[i+4], h->mb + (i*16 << pixel_shift), uvlinesize);
2038 2038
                             }
2039 2039
                         }
... ...
@@ -66,7 +66,7 @@ static int huff_cmp(const void *a, const void *b)
66 66
     return (aa->len - bb->len)*256 + aa->sym - bb->sym;
67 67
 }
68 68
 
69
-static int build_huff(const uint8_t *src, VLC *vlc)
69
+static int build_huff(const uint8_t *src, VLC *vlc, int *fsym)
70 70
 {
71 71
     int i;
72 72
     HuffEntry he[256];
... ...
@@ -76,13 +76,18 @@ static int build_huff(const uint8_t *src, VLC *vlc)
76 76
     uint8_t syms[256];
77 77
     uint32_t code;
78 78
 
79
+    *fsym = -1;
79 80
     for (i = 0; i < 256; i++) {
80 81
         he[i].sym = i;
81 82
         he[i].len = *src++;
82 83
     }
83 84
     qsort(he, 256, sizeof(*he), huff_cmp);
84 85
 
85
-    if (!he[0].len || he[0].len > 32)
86
+    if (!he[0].len) {
87
+        *fsym = he[0].sym;
88
+        return 0;
89
+    }
90
+    if (he[0].len > 32)
86 91
         return -1;
87 92
 
88 93
     last = 255;
... ...
@@ -112,12 +117,37 @@ static int decode_plane(UtvideoContext *c, int plane_no,
112 112
     int sstart, send;
113 113
     VLC vlc;
114 114
     GetBitContext gb;
115
-    int prev;
115
+    int prev, fsym;
116
+    const int cmask = ~(!plane_no && c->avctx->pix_fmt == PIX_FMT_YUV420P);
116 117
 
117
-    if (build_huff(src, &vlc)) {
118
+    if (build_huff(src, &vlc, &fsym)) {
118 119
         av_log(c->avctx, AV_LOG_ERROR, "Cannot build Huffman codes\n");
119 120
         return AVERROR_INVALIDDATA;
120 121
     }
122
+    if (fsym >= 0) { // build_huff reported a symbol to fill slices with
123
+        send = 0;
124
+        for (slice = 0; slice < c->slices; slice++) {
125
+            uint8_t *dest;
126
+
127
+            sstart = send;
128
+            send   = (height * (slice + 1) / c->slices) & cmask;
129
+            dest   = dst + sstart * stride;
130
+
131
+            prev = 0x80;
132
+            for (j = sstart; j < send; j++) {
133
+                for (i = 0; i < width * step; i += step) {
134
+                    pix = fsym;
135
+                    if (use_pred) {
136
+                        prev += pix;
137
+                        pix   = prev;
138
+                    }
139
+                    dest[i] = pix;
140
+                }
141
+                dest += stride;
142
+            }
143
+        }
144
+        return 0;
145
+    }
121 146
 
122 147
     src      += 256;
123 148
     src_size -= 256;
... ...
@@ -128,7 +158,7 @@ static int decode_plane(UtvideoContext *c, int plane_no,
128 128
         int slice_data_start, slice_data_end, slice_size;
129 129
 
130 130
         sstart = send;
131
-        send   = height * (slice + 1) / c->slices;
131
+        send   = (height * (slice + 1) / c->slices) & cmask;
132 132
         dest   = dst + sstart * stride;
133 133
 
134 134
         // slice offset and size validation was done earlier
... ...
@@ -204,16 +234,17 @@ static void restore_rgb_planes(uint8_t *src, int step, int stride, int width, in
204 204
 }
205 205
 
206 206
 static void restore_median(uint8_t *src, int step, int stride,
207
-                           int width, int height, int slices)
207
+                           int width, int height, int slices, int rmode)
208 208
 {
209 209
     int i, j, slice;
210 210
     int A, B, C;
211 211
     uint8_t *bsrc;
212 212
     int slice_start, slice_height;
213
+    const int cmask = ~rmode;
213 214
 
214 215
     for (slice = 0; slice < slices; slice++) {
215
-        slice_start = (slice * height) / slices;
216
-        slice_height = ((slice + 1) * height) / slices - slice_start;
216
+        slice_start = ((slice * height) / slices) & cmask;
217
+        slice_height = ((((slice + 1) * height) / slices) & cmask) - slice_start;
217 218
 
218 219
         bsrc = src + slice_start * stride;
219 220
 
... ...
@@ -337,7 +368,7 @@ static int decode_frame(AVCodecContext *avctx, void *data, int *data_size, AVPac
337 337
             if (c->frame_pred == PRED_MEDIAN)
338 338
                 restore_median(c->pic.data[0] + rgb_order[i], c->planes,
339 339
                                c->pic.linesize[0], avctx->width, avctx->height,
340
-                               c->slices);
340
+                               c->slices, 0);
341 341
         }
342 342
         restore_rgb_planes(c->pic.data[0], c->planes, c->pic.linesize[0],
343 343
                            avctx->width, avctx->height);
... ...
@@ -353,7 +384,7 @@ static int decode_frame(AVCodecContext *avctx, void *data, int *data_size, AVPac
353 353
             if (c->frame_pred == PRED_MEDIAN)
354 354
                 restore_median(c->pic.data[i], 1, c->pic.linesize[i],
355 355
                                avctx->width >> !!i, avctx->height >> !!i,
356
-                               c->slices);
356
+                               c->slices, !i);
357 357
         }
358 358
         break;
359 359
     case PIX_FMT_YUV422P:
... ...
@@ -366,7 +397,7 @@ static int decode_frame(AVCodecContext *avctx, void *data, int *data_size, AVPac
366 366
                 return ret;
367 367
             if (c->frame_pred == PRED_MEDIAN)
368 368
                 restore_median(c->pic.data[i], 1, c->pic.linesize[i],
369
-                               avctx->width >> !!i, avctx->height, c->slices);
369
+                               avctx->width >> !!i, avctx->height, c->slices, 0);
370 370
         }
371 371
         break;
372 372
     }
... ...
@@ -45,6 +45,7 @@
45 45
 #define FRAGMENT_PIXELS 8
46 46
 
47 47
 static av_cold int vp3_decode_end(AVCodecContext *avctx);
48
+static void vp3_decode_flush(AVCodecContext *avctx);
48 49
 
49 50
 //FIXME split things out into their own arrays
50 51
 typedef struct Vp3Fragment {
... ...
@@ -890,7 +891,7 @@ static int unpack_vlcs(Vp3DecodeContext *s, GetBitContext *gb,
890 890
             /* decode a VLC into a token */
891 891
             token = get_vlc2(gb, vlc_table, 11, 3);
892 892
             /* use the token to get a zero run, a coefficient, and an eob run */
893
-            if (token <= 6) {
893
+            if ((unsigned) token <= 6U) {
894 894
                 eob_run = eob_run_base[token];
895 895
                 if (eob_run_get_bits[token])
896 896
                     eob_run += get_bits(gb, eob_run_get_bits[token]);
... ...
@@ -908,7 +909,7 @@ static int unpack_vlcs(Vp3DecodeContext *s, GetBitContext *gb,
908 908
                     coeff_i        += eob_run;
909 909
                     eob_run = 0;
910 910
                 }
911
-            } else {
911
+            } else if (token >= 0) {
912 912
                 bits_to_get = coeff_get_bits[token];
913 913
                 if (bits_to_get)
914 914
                     bits_to_get = get_bits(gb, bits_to_get);
... ...
@@ -942,6 +943,10 @@ static int unpack_vlcs(Vp3DecodeContext *s, GetBitContext *gb,
942 942
                 for (i = coeff_index+1; i <= coeff_index+zero_run; i++)
943 943
                     s->num_coded_frags[plane][i]--;
944 944
                 coeff_i++;
945
+            } else {
946
+                av_log(s->avctx, AV_LOG_ERROR,
947
+                       "Invalid token %d\n", token);
948
+                return -1;
945 949
             }
946 950
     }
947 951
 
... ...
@@ -991,6 +996,8 @@ static int unpack_dct_coeffs(Vp3DecodeContext *s, GetBitContext *gb)
991 991
     /* unpack the Y plane DC coefficients */
992 992
     residual_eob_run = unpack_vlcs(s, gb, &s->dc_vlc[dc_y_table], 0,
993 993
         0, residual_eob_run);
994
+    if (residual_eob_run < 0)
995
+        return residual_eob_run;
994 996
 
995 997
     /* reverse prediction of the Y-plane DC coefficients */
996 998
     reverse_dc_prediction(s, 0, s->fragment_width[0], s->fragment_height[0]);
... ...
@@ -998,8 +1005,12 @@ static int unpack_dct_coeffs(Vp3DecodeContext *s, GetBitContext *gb)
998 998
     /* unpack the C plane DC coefficients */
999 999
     residual_eob_run = unpack_vlcs(s, gb, &s->dc_vlc[dc_c_table], 0,
1000 1000
         1, residual_eob_run);
1001
+    if (residual_eob_run < 0)
1002
+        return residual_eob_run;
1001 1003
     residual_eob_run = unpack_vlcs(s, gb, &s->dc_vlc[dc_c_table], 0,
1002 1004
         2, residual_eob_run);
1005
+    if (residual_eob_run < 0)
1006
+        return residual_eob_run;
1003 1007
 
1004 1008
     /* reverse prediction of the C-plane DC coefficients */
1005 1009
     if (!(s->avctx->flags & CODEC_FLAG_GRAY))
... ...
@@ -1036,11 +1047,17 @@ static int unpack_dct_coeffs(Vp3DecodeContext *s, GetBitContext *gb)
1036 1036
     for (i = 1; i <= 63; i++) {
1037 1037
             residual_eob_run = unpack_vlcs(s, gb, y_tables[i], i,
1038 1038
                 0, residual_eob_run);
1039
+            if (residual_eob_run < 0)
1040
+                return residual_eob_run;
1039 1041
 
1040 1042
             residual_eob_run = unpack_vlcs(s, gb, c_tables[i], i,
1041 1043
                 1, residual_eob_run);
1044
+            if (residual_eob_run < 0)
1045
+                return residual_eob_run;
1042 1046
             residual_eob_run = unpack_vlcs(s, gb, c_tables[i], i,
1043 1047
                 2, residual_eob_run);
1048
+            if (residual_eob_run < 0)
1049
+                return residual_eob_run;
1044 1050
     }
1045 1051
 
1046 1052
     return 0;
... ...
@@ -1777,10 +1794,15 @@ static int vp3_update_thread_context(AVCodecContext *dst, const AVCodecContext *
1777 1777
     Vp3DecodeContext *s = dst->priv_data, *s1 = src->priv_data;
1778 1778
     int qps_changed = 0, i, err;
1779 1779
 
1780
+#define copy_fields(to, from, start_field, end_field) memcpy(&to->start_field, &from->start_field, (char*)&to->end_field - (char*)&to->start_field)
1781
+
1780 1782
     if (!s1->current_frame.data[0]
1781 1783
         ||s->width != s1->width
1782
-        ||s->height!= s1->height)
1784
+        ||s->height!= s1->height) {
1785
+        if (s != s1)
1786
+            copy_fields(s, s1, golden_frame, current_frame);
1783 1787
         return -1;
1788
+    }
1784 1789
 
1785 1790
     if (s != s1) {
1786 1791
         // init tables if the first frame hasn't been decoded
... ...
@@ -1796,8 +1818,6 @@ static int vp3_update_thread_context(AVCodecContext *dst, const AVCodecContext *
1796 1796
             memcpy(s->motion_val[1], s1->motion_val[1], c_fragment_count * sizeof(*s->motion_val[1]));
1797 1797
         }
1798 1798
 
1799
-#define copy_fields(to, from, start_field, end_field) memcpy(&to->start_field, &from->start_field, (char*)&to->end_field - (char*)&to->start_field)
1800
-
1801 1799
         // copy previous frame data
1802 1800
         copy_fields(s, s1, golden_frame, dsp);
1803 1801
 
... ...
@@ -1990,9 +2010,6 @@ static av_cold int vp3_decode_end(AVCodecContext *avctx)
1990 1990
     Vp3DecodeContext *s = avctx->priv_data;
1991 1991
     int i;
1992 1992
 
1993
-    if (avctx->is_copy && !s->current_frame.data[0])
1994
-        return 0;
1995
-
1996 1993
     av_free(s->superblock_coding);
1997 1994
     av_free(s->all_fragments);
1998 1995
     av_free(s->coded_fragment_list[0]);
... ...
@@ -2339,6 +2356,23 @@ static void vp3_decode_flush(AVCodecContext *avctx)
2339 2339
         ff_thread_release_buffer(avctx, &s->current_frame);
2340 2340
 }
2341 2341
 
2342
+static int vp3_init_thread_copy(AVCodecContext *avctx)
2343
+{
2344
+    Vp3DecodeContext *s = avctx->priv_data;
2345
+
2346
+    s->superblock_coding      = NULL;
2347
+    s->all_fragments          = NULL;
2348
+    s->coded_fragment_list[0] = NULL;
2349
+    s->dct_tokens_base        = NULL;
2350
+    s->superblock_fragments   = NULL;
2351
+    s->macroblock_coding      = NULL;
2352
+    s->motion_val[0]          = NULL;
2353
+    s->motion_val[1]          = NULL;
2354
+    s->edge_emu_buffer        = NULL;
2355
+
2356
+    return 0;
2357
+}
2358
+
2342 2359
 AVCodec ff_theora_decoder = {
2343 2360
     .name           = "theora",
2344 2361
     .type           = AVMEDIA_TYPE_VIDEO,
... ...
@@ -2350,6 +2384,7 @@ AVCodec ff_theora_decoder = {
2350 2350
     .capabilities   = CODEC_CAP_DR1 | CODEC_CAP_DRAW_HORIZ_BAND | CODEC_CAP_FRAME_THREADS,
2351 2351
     .flush = vp3_decode_flush,
2352 2352
     .long_name = NULL_IF_CONFIG_SMALL("Theora"),
2353
+    .init_thread_copy      = ONLY_IF_THREADS_ENABLED(vp3_init_thread_copy),
2353 2354
     .update_thread_context = ONLY_IF_THREADS_ENABLED(vp3_update_thread_context)
2354 2355
 };
2355 2356
 #endif
... ...
@@ -2365,5 +2400,6 @@ AVCodec ff_vp3_decoder = {
2365 2365
     .capabilities   = CODEC_CAP_DR1 | CODEC_CAP_DRAW_HORIZ_BAND | CODEC_CAP_FRAME_THREADS,
2366 2366
     .flush = vp3_decode_flush,
2367 2367
     .long_name = NULL_IF_CONFIG_SMALL("On2 VP3"),
2368
+    .init_thread_copy      = ONLY_IF_THREADS_ENABLED(vp3_init_thread_copy),
2368 2369
     .update_thread_context = ONLY_IF_THREADS_ENABLED(vp3_update_thread_context)
2369 2370
 };
... ...
@@ -50,7 +50,8 @@ static int vp8_alloc_frame(VP8Context *s, AVFrame *f)
50 50
     int ret;
51 51
     if ((ret = ff_thread_get_buffer(s->avctx, f)) < 0)
52 52
         return ret;
53
-    if (!s->maps_are_invalid && s->num_maps_to_be_freed) {
53
+    if (s->num_maps_to_be_freed) {
54
+        assert(!s->maps_are_invalid);
54 55
         f->ref_index[0] = s->segmentation_maps[--s->num_maps_to_be_freed];
55 56
     } else if (!(f->ref_index[0] = av_mallocz(s->mb_width * s->mb_height))) {
56 57
         ff_thread_release_buffer(s->avctx, f);
... ...
@@ -59,39 +60,50 @@ static int vp8_alloc_frame(VP8Context *s, AVFrame *f)
59 59
     return 0;
60 60
 }
61 61
 
62
-static void vp8_release_frame(VP8Context *s, AVFrame *f, int is_close)
62
+static void vp8_release_frame(VP8Context *s, AVFrame *f, int prefer_delayed_free, int can_direct_free)
63 63
 {
64
-    if (!is_close) {
65
-        if (f->ref_index[0]) {
66
-            assert(s->num_maps_to_be_freed < FF_ARRAY_ELEMS(s->segmentation_maps));
67
-            s->segmentation_maps[s->num_maps_to_be_freed++] = f->ref_index[0];
64
+    if (f->ref_index[0]) {
65
+        if (prefer_delayed_free) {
66
+            /* Upon a size change, we want to free the maps but other threads may still
67
+             * be using them, so queue them. Upon a seek, all threads are inactive so
68
+             * we want to cache one to prevent re-allocation in the next decoding
69
+             * iteration, but the rest we can free directly. */
70
+            int max_queued_maps = can_direct_free ? 1 : FF_ARRAY_ELEMS(s->segmentation_maps);
71
+            if (s->num_maps_to_be_freed < max_queued_maps) {
72
+                s->segmentation_maps[s->num_maps_to_be_freed++] = f->ref_index[0];
73
+            } else if (can_direct_free) /* vp8_decode_flush(), but our queue is full */ {
74
+                av_free(f->ref_index[0]);
75
+            } /* else: MEMLEAK (should never happen, but better that than crash) */
68 76
             f->ref_index[0] = NULL;
77
+        } else /* vp8_decode_free() */ {
78
+            av_free(f->ref_index[0]);
69 79
         }
70
-    } else {
71
-        av_freep(&f->ref_index[0]);
72 80
     }
73 81
     ff_thread_release_buffer(s->avctx, f);
74 82
 }
75 83
 
76
-static void vp8_decode_flush_impl(AVCodecContext *avctx, int force, int is_close)
84
+static void vp8_decode_flush_impl(AVCodecContext *avctx,
85
+                                  int prefer_delayed_free, int can_direct_free, int free_mem)
77 86
 {
78 87
     VP8Context *s = avctx->priv_data;
79 88
     int i;
80 89
 
81
-    if (!avctx->is_copy || force) {
90
+    if (!avctx->is_copy) {
82 91
         for (i = 0; i < 5; i++)
83 92
             if (s->frames[i].data[0])
84
-                vp8_release_frame(s, &s->frames[i], is_close);
93
+                vp8_release_frame(s, &s->frames[i], prefer_delayed_free, can_direct_free);
85 94
     }
86 95
     memset(s->framep, 0, sizeof(s->framep));
87 96
 
88
-    free_buffers(s);
89
-    s->maps_are_invalid = 1;
97
+    if (free_mem) {
98
+        free_buffers(s);
99
+        s->maps_are_invalid = 1;
100
+    }
90 101
 }
91 102
 
92 103
 static void vp8_decode_flush(AVCodecContext *avctx)
93 104
 {
94
-    vp8_decode_flush_impl(avctx, 0, 0);
105
+    vp8_decode_flush_impl(avctx, 1, 1, 0);
95 106
 }
96 107
 
97 108
 static int update_dimensions(VP8Context *s, int width, int height)
... ...
@@ -101,7 +113,7 @@ static int update_dimensions(VP8Context *s, int width, int height)
101 101
         if (av_image_check_size(width, height, 0, s->avctx))
102 102
             return AVERROR_INVALIDDATA;
103 103
 
104
-        vp8_decode_flush_impl(s->avctx, 1, 0);
104
+        vp8_decode_flush_impl(s->avctx, 1, 0, 1);
105 105
 
106 106
         avcodec_set_dimensions(s->avctx, width, height);
107 107
     }
... ...
@@ -1581,7 +1593,7 @@ static int vp8_decode_frame(AVCodecContext *avctx, void *data, int *data_size,
1581 1581
             &s->frames[i] != s->framep[VP56_FRAME_PREVIOUS] &&
1582 1582
             &s->frames[i] != s->framep[VP56_FRAME_GOLDEN] &&
1583 1583
             &s->frames[i] != s->framep[VP56_FRAME_GOLDEN2])
1584
-            vp8_release_frame(s, &s->frames[i], 0);
1584
+            vp8_release_frame(s, &s->frames[i], 1, 0);
1585 1585
 
1586 1586
     // find a free buffer
1587 1587
     for (i = 0; i < 5; i++)
... ...
@@ -1597,7 +1609,7 @@ static int vp8_decode_frame(AVCodecContext *avctx, void *data, int *data_size,
1597 1597
         abort();
1598 1598
     }
1599 1599
     if (curframe->data[0])
1600
-        ff_thread_release_buffer(avctx, curframe);
1600
+        vp8_release_frame(s, curframe, 1, 0);
1601 1601
 
1602 1602
     curframe->key_frame = s->keyframe;
1603 1603
     curframe->pict_type = s->keyframe ? AV_PICTURE_TYPE_I : AV_PICTURE_TYPE_P;
... ...
@@ -1778,7 +1790,7 @@ static av_cold int vp8_decode_init(AVCodecContext *avctx)
1778 1778
 
1779 1779
 static av_cold int vp8_decode_free(AVCodecContext *avctx)
1780 1780
 {
1781
-    vp8_decode_flush_impl(avctx, 0, 1);
1781
+    vp8_decode_flush_impl(avctx, 0, 1, 1);
1782 1782
     release_queued_segmaps(avctx->priv_data, 1);
1783 1783
     return 0;
1784 1784
 }
... ...
@@ -676,6 +676,7 @@ typedef struct AVStream {
676 676
         int duration_count;
677 677
         double duration_error[2][2][MAX_STD_TIMEBASES];
678 678
         int64_t codec_info_duration;
679
+        int nb_decoded_frames;
679 680
     } *info;
680 681
 
681 682
     /**
... ...
@@ -131,9 +131,8 @@ static int cin_read_header(AVFormatContext *s, AVFormatParameters *ap)
131 131
     st->codec->codec_tag = 0;  /* no tag */
132 132
     st->codec->channels = 1;
133 133
     st->codec->sample_rate = 22050;
134
-    st->codec->bits_per_coded_sample = 16;
134
+    st->codec->bits_per_coded_sample = 8;
135 135
     st->codec->bit_rate = st->codec->sample_rate * st->codec->bits_per_coded_sample * st->codec->channels;
136
-    st->codec->block_align = st->codec->channels * st->codec->bits_per_coded_sample;
137 136
 
138 137
     return 0;
139 138
 }
... ...
@@ -211,7 +210,8 @@ static int cin_read_packet(AVFormatContext *s, AVPacket *pkt)
211 211
 
212 212
     pkt->stream_index = cin->audio_stream_index;
213 213
     pkt->pts = cin->audio_stream_pts;
214
-    cin->audio_stream_pts += cin->audio_buffer_size * 2 / cin->file_header.audio_frame_size;
214
+    pkt->duration = cin->audio_buffer_size - (pkt->pts == 0);
215
+    cin->audio_stream_pts += pkt->duration;
215 216
     cin->audio_buffer_size = 0;
216 217
     return 0;
217 218
 }
... ...
@@ -2200,7 +2200,7 @@ static int has_codec_parameters(AVCodecContext *avctx)
2200 2200
 static int has_decode_delay_been_guessed(AVStream *st)
2201 2201
 {
2202 2202
     return st->codec->codec_id != CODEC_ID_H264 ||
2203
-        st->codec_info_nb_frames >= 6 + st->codec->has_b_frames;
2203
+        st->info->nb_decoded_frames >= 6;
2204 2204
 }
2205 2205
 
2206 2206
 static int try_decode_frame(AVStream *st, AVPacket *avpkt, AVDictionary **options)
... ...
@@ -2226,6 +2226,8 @@ static int try_decode_frame(AVStream *st, AVPacket *avpkt, AVDictionary **option
2226 2226
             avcodec_get_frame_defaults(&picture);
2227 2227
             ret = avcodec_decode_video2(st->codec, &picture,
2228 2228
                                         &got_picture, avpkt);
2229
+            if (got_picture)
2230
+                st->info->nb_decoded_frames++;
2229 2231
             break;
2230 2232
         case AVMEDIA_TYPE_AUDIO:
2231 2233
             data_size = FFMAX(avpkt->size, AVCODEC_MAX_AUDIO_FRAME_SIZE);