]> git.sesse.net Git - ffmpeg/blobdiff - libavcodec/rpza.c
Merge commit 'd6a27f885b5d4cba7a82e50af423c741d2f37c3e'
[ffmpeg] / libavcodec / rpza.c
index 03427068cdfd15ffe9c4cd67ba8cc76cef5aefed..aac437e41c949700cee13faf744aa80a9260a5c4 100644 (file)
@@ -2,20 +2,20 @@
  * Quicktime Video (RPZA) Video Decoder
  * Copyright (C) 2003 the ffmpeg project
  *
- * This file is part of Libav.
+ * This file is part of FFmpeg.
  *
- * Libav is free software; you can redistribute it and/or
+ * FFmpeg is free software; you can redistribute it and/or
  * modify it under the terms of the GNU Lesser General Public
  * License as published by the Free Software Foundation; either
  * version 2.1 of the License, or (at your option) any later version.
  *
- * Libav is distributed in the hope that it will be useful,
+ * FFmpeg is distributed in the hope that it will be useful,
  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  * Lesser General Public License for more details.
  *
  * You should have received a copy of the GNU Lesser General Public
- * License along with Libav; if not, write to the Free Software
+ * License along with FFmpeg; if not, write to the Free Software
  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  */
 
@@ -74,16 +74,13 @@ static void rpza_decode_stream(RpzaContext *s)
     int stride = s->frame->linesize[0] / 2;
     int row_inc = stride - 4;
     int chunk_size;
-    uint8_t opcode;
-    int n_blocks;
     uint16_t colorA = 0, colorB;
     uint16_t color4[4];
-    uint8_t index, idx;
     uint16_t ta, tb;
     uint16_t *pixels = (uint16_t *)s->frame->data[0];
 
     int row_ptr = 0;
-    int pixel_ptr = 0;
+    int pixel_ptr = -4;
     int block_ptr;
     int pixel_x, pixel_y;
     int total_blocks;
@@ -105,9 +102,9 @@ static void rpza_decode_stream(RpzaContext *s)
 
     /* Process chunk data */
     while (bytestream2_get_bytes_left(&s->gb)) {
-        opcode = bytestream2_get_byte(&s->gb); /* Get opcode */
+        uint8_t opcode = bytestream2_get_byte(&s->gb); /* Get opcode */
 
-        n_blocks = (opcode & 0x1f) + 1; /* Extract block counter from opcode */
+        int n_blocks = (opcode & 0x1f) + 1; /* Extract block counter from opcode */
 
         /* If opcode MSbit is 0, we need more data to decide what to do */
         if ((opcode & 0x80) == 0) {
@@ -122,6 +119,8 @@ static void rpza_decode_stream(RpzaContext *s)
             }
         }
 
+        n_blocks = FFMIN(n_blocks, total_blocks);
+
         switch (opcode & 0xe0) {
 
         /* Skip blocks */
@@ -135,6 +134,7 @@ static void rpza_decode_stream(RpzaContext *s)
         case 0xa0:
             colorA = bytestream2_get_be16(&s->gb);
             while (n_blocks--) {
+                ADVANCE_BLOCK()
                 block_ptr = row_ptr + pixel_ptr;
                 for (pixel_y = 0; pixel_y < 4; pixel_y++) {
                     for (pixel_x = 0; pixel_x < 4; pixel_x++){
@@ -143,7 +143,6 @@ static void rpza_decode_stream(RpzaContext *s)
                     }
                     block_ptr += row_inc;
                 }
-                ADVANCE_BLOCK();
             }
             break;
 
@@ -180,17 +179,17 @@ static void rpza_decode_stream(RpzaContext *s)
             if (bytestream2_get_bytes_left(&s->gb) < n_blocks * 4)
                 return;
             while (n_blocks--) {
+                ADVANCE_BLOCK();
                 block_ptr = row_ptr + pixel_ptr;
                 for (pixel_y = 0; pixel_y < 4; pixel_y++) {
-                    index = bytestream2_get_byteu(&s->gb);
+                    uint8_t index = bytestream2_get_byteu(&s->gb);
                     for (pixel_x = 0; pixel_x < 4; pixel_x++){
-                        idx = (index >> (2 * (3 - pixel_x))) & 0x03;
+                        uint8_t idx = (index >> (2 * (3 - pixel_x))) & 0x03;
                         pixels[block_ptr] = color4[idx];
                         block_ptr++;
                     }
                     block_ptr += row_inc;
                 }
-                ADVANCE_BLOCK();
             }
             break;
 
@@ -198,6 +197,7 @@ static void rpza_decode_stream(RpzaContext *s)
         case 0x00:
             if (bytestream2_get_bytes_left(&s->gb) < 30)
                 return;
+            ADVANCE_BLOCK();
             block_ptr = row_ptr + pixel_ptr;
             for (pixel_y = 0; pixel_y < 4; pixel_y++) {
                 for (pixel_x = 0; pixel_x < 4; pixel_x++){
@@ -209,7 +209,6 @@ static void rpza_decode_stream(RpzaContext *s)
                 }
                 block_ptr += row_inc;
             }
-            ADVANCE_BLOCK();
             break;
 
         /* Unknown opcode */
@@ -245,10 +244,8 @@ static int rpza_decode_frame(AVCodecContext *avctx,
 
     bytestream2_init(&s->gb, avpkt->data, avpkt->size);
 
-    if ((ret = ff_reget_buffer(avctx, s->frame)) < 0) {
-        av_log(avctx, AV_LOG_ERROR, "reget_buffer() failed\n");
+    if ((ret = ff_reget_buffer(avctx, s->frame)) < 0)
         return ret;
-    }
 
     rpza_decode_stream(s);