Imported Upstream version 6.1
[platform/upstream/ffmpeg.git] / libavcodec / flac_parser.c
index 81b6f12..bd91cc1 100644 (file)
@@ -36,7 +36,7 @@
 #include "libavutil/crc.h"
 #include "bytestream.h"
 #include "parser.h"
-#include "flac.h"
+#include "flac_parse.h"
 
 /** maximum number of adjacent headers that compare CRCs against each other   */
 #define FLAC_MAX_SEQUENTIAL_HEADERS 4
@@ -455,7 +455,7 @@ static int check_header_mismatch(FLACParseContext  *fpc,
                                  int                log_level_offset)
 {
     FLACFrameInfo  *header_fi = &header->fi, *child_fi = &child->fi;
-    int deduction, deduction_expected = 0, i;
+    int check_crc, deduction, deduction_expected = 0, i;
     deduction = check_header_fi_mismatch(fpc, header_fi, child_fi,
                                          log_level_offset);
     /* Check sample and frame numbers. */
@@ -491,8 +491,22 @@ static int check_header_mismatch(FLACParseContext  *fpc,
                    "sample/frame number mismatch in adjacent frames\n");
     }
 
+    if (fpc->last_fi.is_var_size == header_fi->is_var_size) {
+        if (fpc->last_fi.is_var_size &&
+            fpc->last_fi.frame_or_sample_num + fpc->last_fi.blocksize == header_fi->frame_or_sample_num) {
+            check_crc = 0;
+        } else if (!fpc->last_fi.is_var_size &&
+                   fpc->last_fi.frame_or_sample_num + 1 == header_fi->frame_or_sample_num) {
+            check_crc = 0;
+        } else {
+            check_crc = !deduction && !deduction_expected;
+        }
+    } else {
+        check_crc = !deduction && !deduction_expected;
+    }
+
     /* If we have suspicious headers, check the CRC between them */
-    if (deduction && !deduction_expected) {
+    if (check_crc || (deduction && !deduction_expected)) {
         FLACHeaderMarker *curr;
         int read_len;
         uint8_t *buf;
@@ -600,7 +614,7 @@ static int score_header(FLACParseContext *fpc, FLACHeaderMarker *header)
 static void score_sequences(FLACParseContext *fpc)
 {
     FLACHeaderMarker *curr;
-    int best_score = 0;//FLAC_HEADER_NOT_SCORED_YET;
+    int best_score = FLAC_HEADER_NOT_SCORED_YET;
     /* First pass to clear all old scores. */
     for (curr = fpc->headers; curr; curr = curr->next)
         curr->max_score = FLAC_HEADER_NOT_SCORED_YET;
@@ -649,8 +663,11 @@ static int get_best_header(FLACParseContext *fpc, const uint8_t **poutbuf,
 
     /* Return the negative overread index so the client can compute pos.
        This should be the amount overread to the beginning of the child */
-    if (child)
-        return child->offset - flac_fifo_size(&fpc->fifo_buf);
+    if (child) {
+        int64_t offset = child->offset - flac_fifo_size(&fpc->fifo_buf);
+        if (offset > -(1 << 28))
+            return offset;
+    }
     return 0;
 }
 
@@ -682,7 +699,7 @@ static int flac_parse(AVCodecParserContext *s, AVCodecContext *avctx,
     }
 
     fpc->avctx = avctx;
-    if (fpc->best_header_valid)
+    if (fpc->best_header_valid && fpc->nb_headers_buffered >= FLAC_MIN_HEADERS)
         return get_best_header(fpc, poutbuf, poutbuf_size);
 
     /* If a best_header was found last call remove it with the buffer data. */