]> git.sesse.net Git - ffmpeg/blobdiff - libavcodec/flac.c
Fix typo
[ffmpeg] / libavcodec / flac.c
index 6c64ad0a1b83525a0e5894b320dab87692cfa92e..c20a6b5e1e41390ba9677238fd81d67d3e58e39d 100644 (file)
@@ -225,6 +225,10 @@ static int decode_residuals(FLACContext *s, int channel, int pred_order)
     rice_order = get_bits(&s->gb, 4);
 
     samples= s->blocksize >> rice_order;
+    if (pred_order > samples) {
+        av_log(s->avctx, AV_LOG_ERROR, "invalid predictor order: %i > %i\n", pred_order, samples);
+        return -1;
+    }
 
     sample=
     i= pred_order;
@@ -454,7 +458,7 @@ static inline int decode_subframe(FLACContext *s, int channel)
     return 0;
 }
 
-static int decode_frame(FLACContext *s)
+static int decode_frame(FLACContext *s, int alloc_data_size)
 {
     int blocksize_code, sample_rate_code, sample_size_code, assignment, i, crc8;
     int decorrelation, bps, blocksize, samplerate;
@@ -516,6 +520,9 @@ static int decode_frame(FLACContext *s)
         return -1;
     }
 
+    if(blocksize * s->channels * sizeof(int16_t) > alloc_data_size)
+        return -1;
+
     if (sample_rate_code == 0){
         samplerate= s->samplerate;
     }else if ((sample_rate_code > 3) && (sample_rate_code < 12))
@@ -561,17 +568,6 @@ static int decode_frame(FLACContext *s)
     return 0;
 }
 
-static inline int16_t shift_to_16_bits(int32_t data, int bps)
-{
-    if (bps == 24) {
-        return (data >> 8);
-    } else if (bps == 20) {
-        return (data >> 4);
-    } else {
-        return data;
-    }
-}
-
 static int flac_decode_frame(AVCodecContext *avctx,
                             void *data, int *data_size,
                             uint8_t *buf, int buf_size)
@@ -579,6 +575,9 @@ static int flac_decode_frame(AVCodecContext *avctx,
     FLACContext *s = avctx->priv_data;
     int tmp = 0, i, j = 0, input_buf_size = 0;
     int16_t *samples = data;
+    int alloc_data_size= *data_size;
+
+    *data_size=0;
 
     if(s->max_framesize == 0){
         s->max_framesize= 65536; // should hopefully be enough for the first header
@@ -617,7 +616,7 @@ static int flac_decode_frame(AVCodecContext *avctx,
             goto end; // we may not have enough bits left to decode a frame, so try next time
         }
         skip_bits(&s->gb, 16);
-        if (decode_frame(s) < 0){
+        if (decode_frame(s, alloc_data_size) < 0){
             av_log(s->avctx, AV_LOG_ERROR, "decode_frame() failed\n");
             s->bitstream_size=0;
             s->bitstream_index=0;
@@ -674,8 +673,8 @@ static int flac_decode_frame(AVCodecContext *avctx,
             {\
                 int a= s->decoded[0][i];\
                 int b= s->decoded[1][i];\
-                *(samples++) = (left ) >> (16 - s->bps);\
-                *(samples++) = (right) >> (16 - s->bps);\
+                *samples++ = ((left)  << (24 - s->bps)) >> 8;\
+                *samples++ = ((right) << (24 - s->bps)) >> 8;\
             }\
             break;
 
@@ -685,7 +684,7 @@ static int flac_decode_frame(AVCodecContext *avctx,
             for (j = 0; j < s->blocksize; j++)
             {
                 for (i = 0; i < s->channels; i++)
-                    *(samples++) = shift_to_16_bits(s->decoded[i][j], s->bps);
+                    *samples++ = (s->decoded[i][j] << (24 - s->bps)) >> 8;
             }
             break;
         case LEFT_SIDE: