From fd6fd470ce47d1b68067a16120ffc87fa25ffa01 Mon Sep 17 00:00:00 2001
From: Justin Ruggles <justin.ruggles@gmail.com>
Date: Thu, 5 Mar 2009 01:15:38 +0000
Subject: [PATCH] flacdec: Simplify frame sync by not using the bitstream
 reader.

Originally committed as revision 17818 to svn://svn.ffmpeg.org/ffmpeg/trunk
---
 libavcodec/flacdec.c | 20 +++++++++++---------
 1 file changed, 11 insertions(+), 9 deletions(-)

diff --git a/libavcodec/flacdec.c b/libavcodec/flacdec.c
index 29e3739688..893eab5705 100644
--- a/libavcodec/flacdec.c
+++ b/libavcodec/flacdec.c
@@ -610,7 +610,7 @@ static int flac_decode_frame(AVCodecContext *avctx,
                             const uint8_t *buf, int buf_size)
 {
     FLACContext *s = avctx->priv_data;
-    int tmp = 0, i, j = 0, input_buf_size = 0, bytes_read = 0;
+    int i, j = 0, input_buf_size = 0, bytes_read = 0;
     int16_t *samples_16 = data;
     int32_t *samples_32 = data;
     int alloc_data_size= *data_size;
@@ -664,15 +664,18 @@ static int flac_decode_frame(AVCodecContext *avctx,
         goto end;
     }
 
-    init_get_bits(&s->gb, buf, buf_size*8);
-
-    tmp = show_bits(&s->gb, 16);
-    if ((tmp & 0xFFFE) != 0xFFF8) {
+    /* check for frame sync code and resync stream if necessary */
+    if ((AV_RB16(buf) & 0xFFFE) != 0xFFF8) {
+        const uint8_t *buf_end = buf + buf_size;
         av_log(s->avctx, AV_LOG_ERROR, "FRAME HEADER not here\n");
-        while (get_bits_count(&s->gb)/8+2 < buf_size && (show_bits(&s->gb, 16) & 0xFFFE) != 0xFFF8)
-            skip_bits(&s->gb, 8);
-        goto hdr_end; // we may not have enough bits left to decode a frame, so try next time
+        while (buf+2 < buf_end && (AV_RB16(buf) & 0xFFFE) != 0xFFF8)
+            buf++;
+        bytes_read = buf_size - (buf_end - buf);
+        goto end; // we may not have enough bits left to decode a frame, so try next time
     }
+
+    /* decode frame */
+    init_get_bits(&s->gb, buf, buf_size*8);
     skip_bits(&s->gb, 16);
     if (decode_frame(s, alloc_data_size) < 0) {
         av_log(s->avctx, AV_LOG_ERROR, "decode_frame() failed\n");
@@ -717,7 +720,6 @@ static int flac_decode_frame(AVCodecContext *avctx,
 
     *data_size = s->blocksize * s->channels * (s->is32 ? 4 : 2);
 
-hdr_end:
     bytes_read = (get_bits_count(&s->gb)+7)/8;
 end:
     if (bytes_read > buf_size) {