]> git.sesse.net Git - ffmpeg/blobdiff - libavcodec/vp5.c
hevc: change the stride of the MC buffer to be in bytes instead of elements
[ffmpeg] / libavcodec / vp5.c
index 41e649720200cc24631f08333df4d739ebaf0a74..81c725d96ef236a5121d0a2c95c875b1d56a45f0 100644 (file)
@@ -1,32 +1,34 @@
-/**
- * @file libavcodec/vp5.c
- * VP5 compatible video decoder
- *
+/*
  * Copyright (C) 2006  Aurelien Jacobs <aurel@gnuage.org>
  *
- * This file is part of FFmpeg.
+ * This file is part of Libav.
  *
- * FFmpeg is free software; you can redistribute it and/or
+ * Libav 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.
  *
- * FFmpeg is distributed in the hope that it will be useful,
+ * Libav 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 FFmpeg; if not, write to the Free Software
+ * License along with Libav; if not, write to the Free Software
  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  */
 
+/**
+ * @file
+ * VP5 compatible video decoder
+ */
+
 #include <stdlib.h>
 #include <string.h>
 
 #include "avcodec.h"
-#include "dsputil.h"
 #include "get_bits.h"
+#include "internal.h"
 
 #include "vp56.h"
 #include "vp56data.h"
@@ -39,51 +41,41 @@ static int vp5_parse_header(VP56Context *s, const uint8_t *buf, int buf_size,
     VP56RangeCoder *c = &s->c;
     int rows, cols;
 
-    vp56_init_range_decoder(&s->c, buf, buf_size);
-    s->framep[VP56_FRAME_CURRENT]->key_frame = !vp56_rac_get(c);
+    ff_vp56_init_range_decoder(&s->c, buf, buf_size);
+    s->frames[VP56_FRAME_CURRENT]->key_frame = !vp56_rac_get(c);
     vp56_rac_get(c);
-    vp56_init_dequant(s, vp56_rac_gets(c, 6));
-    if (s->framep[VP56_FRAME_CURRENT]->key_frame)
+    ff_vp56_init_dequant(s, vp56_rac_gets(c, 6));
+    if (s->frames[VP56_FRAME_CURRENT]->key_frame)
     {
         vp56_rac_gets(c, 8);
         if(vp56_rac_gets(c, 5) > 5)
-            return 0;
+            return AVERROR_INVALIDDATA;
         vp56_rac_gets(c, 2);
         if (vp56_rac_get(c)) {
             av_log(s->avctx, AV_LOG_ERROR, "interlacing not supported\n");
-            return 0;
+            return AVERROR_PATCHWELCOME;
         }
         rows = vp56_rac_gets(c, 8);  /* number of stored macroblock rows */
         cols = vp56_rac_gets(c, 8);  /* number of stored macroblock cols */
+        if (!rows || !cols) {
+            av_log(s->avctx, AV_LOG_ERROR, "Invalid size %dx%d\n",
+                   cols << 4, rows << 4);
+            return AVERROR_INVALIDDATA;
+        }
         vp56_rac_gets(c, 8);  /* number of displayed macroblock rows */
         vp56_rac_gets(c, 8);  /* number of displayed macroblock cols */
         vp56_rac_gets(c, 2);
         if (!s->macroblocks || /* first frame */
             16*cols != s->avctx->coded_width ||
             16*rows != s->avctx->coded_height) {
-            avcodec_set_dimensions(s->avctx, 16*cols, 16*rows);
-            return 2;
+            int ret = ff_set_dimensions(s->avctx, 16 * cols, 16 * rows);
+            if (ret < 0)
+                return ret;
+            return VP56_SIZE_CHANGE;
         }
     } else if (!s->macroblocks)
-        return 0;
-    return 1;
-}
-
-/* Gives very similar result than the vp6 version except in a few cases */
-static int vp5_adjust(int v, int t)
-{
-    int s2, s1 = v >> 31;
-    v ^= s1;
-    v -= s1;
-    v *= v < 2*t;
-    v -= t;
-    s2 = v >> 31;
-    v ^= s2;
-    v -= s2;
-    v = t - v;
-    v += s1;
-    v ^= s1;
-    return v;
+        return AVERROR_INVALIDDATA;
+    return 0;
 }
 
 static void vp5_parse_vector_adjustment(VP56Context *s, VP56mv *vect)
@@ -98,7 +90,7 @@ static void vp5_parse_vector_adjustment(VP56Context *s, VP56mv *vect)
             int sign = vp56_rac_get_prob(c, model->vector_sig[comp]);
             di  = vp56_rac_get_prob(c, model->vector_pdi[comp][0]);
             di |= vp56_rac_get_prob(c, model->vector_pdi[comp][1]) << 1;
-            delta = vp56_rac_get_tree(c, vp56_pva_tree,
+            delta = vp56_rac_get_tree(c, ff_vp56_pva_tree,
                                       model->vector_pdv[comp]);
             delta = di | (delta << 2);
             delta = (delta ^ -sign) + sign;
@@ -133,7 +125,7 @@ static void vp5_parse_vector_models(VP56Context *s)
                 model->vector_pdv[comp][node] = vp56_rac_gets_nn(c, 7);
 }
 
-static void vp5_parse_coeff_models(VP56Context *s)
+static int vp5_parse_coeff_models(VP56Context *s)
 {
     VP56RangeCoder *c = &s->c;
     VP56Model *model = s->modelp;
@@ -149,7 +141,7 @@ static void vp5_parse_coeff_models(VP56Context *s)
             if (vp56_rac_get_prob(c, vp5_dccv_pct[pt][node])) {
                 def_prob[node] = vp56_rac_gets_nn(c, 7);
                 model->coeff_dccv[pt][node] = def_prob[node];
-            } else if (s->framep[VP56_FRAME_CURRENT]->key_frame) {
+            } else if (s->frames[VP56_FRAME_CURRENT]->key_frame) {
                 model->coeff_dccv[pt][node] = def_prob[node];
             }
 
@@ -160,7 +152,7 @@ static void vp5_parse_coeff_models(VP56Context *s)
                     if (vp56_rac_get_prob(c, vp5_ract_pct[ct][pt][cg][node])) {
                         def_prob[node] = vp56_rac_gets_nn(c, 7);
                         model->coeff_ract[pt][ct][cg][node] = def_prob[node];
-                    } else if (s->framep[VP56_FRAME_CURRENT]->key_frame) {
+                    } else if (s->frames[VP56_FRAME_CURRENT]->key_frame) {
                         model->coeff_ract[pt][ct][cg][node] = def_prob[node];
                     }
 
@@ -177,13 +169,14 @@ static void vp5_parse_coeff_models(VP56Context *s)
                 for (ctx=0; ctx<6; ctx++)
                     for (node=0; node<5; node++)
                         model->coeff_acct[pt][ct][cg][ctx][node] = av_clip(((model->coeff_ract[pt][ct][cg][node] * vp5_ract_lc[ct][cg][node][ctx][0] + 128) >> 8) + vp5_ract_lc[ct][cg][node][ctx][1], 1, 254);
+    return 0;
 }
 
 static void vp5_parse_coeff(VP56Context *s)
 {
     VP56RangeCoder *c = &s->c;
     VP56Model *model = s->modelp;
-    uint8_t *permute = s->scantable.permutated;
+    uint8_t *permute = s->idct_scantable;
     uint8_t *model1, *model2;
     int coeff, sign, coeff_idx;
     int b, i, cg, idx, ctx, ctx_last;
@@ -194,35 +187,36 @@ static void vp5_parse_coeff(VP56Context *s)
 
         if (b > 3) pt = 1;
 
-        ctx = 6*s->coeff_ctx[vp56_b6to4[b]][0]
+        ctx = 6*s->coeff_ctx[ff_vp56_b6to4[b]][0]
               + s->above_blocks[s->above_block_idx[b]].not_null_dc;
         model1 = model->coeff_dccv[pt];
         model2 = model->coeff_dcct[pt][ctx];
 
-        for (coeff_idx=0; coeff_idx<64; ) {
+        coeff_idx = 0;
+        for (;;) {
             if (vp56_rac_get_prob(c, model2[0])) {
                 if (vp56_rac_get_prob(c, model2[2])) {
                     if (vp56_rac_get_prob(c, model2[3])) {
-                        s->coeff_ctx[vp56_b6to4[b]][coeff_idx] = 4;
-                        idx = vp56_rac_get_tree(c, vp56_pc_tree, model1);
+                        s->coeff_ctx[ff_vp56_b6to4[b]][coeff_idx] = 4;
+                        idx = vp56_rac_get_tree(c, ff_vp56_pc_tree, model1);
                         sign = vp56_rac_get(c);
-                        coeff = vp56_coeff_bias[idx+5];
-                        for (i=vp56_coeff_bit_length[idx]; i>=0; i--)
-                            coeff += vp56_rac_get_prob(c, vp56_coeff_parse_table[idx][i]) << i;
+                        coeff = ff_vp56_coeff_bias[idx+5];
+                        for (i=ff_vp56_coeff_bit_length[idx]; i>=0; i--)
+                            coeff += vp56_rac_get_prob(c, ff_vp56_coeff_parse_table[idx][i]) << i;
                     } else {
                         if (vp56_rac_get_prob(c, model2[4])) {
                             coeff = 3 + vp56_rac_get_prob(c, model1[5]);
-                            s->coeff_ctx[vp56_b6to4[b]][coeff_idx] = 3;
+                            s->coeff_ctx[ff_vp56_b6to4[b]][coeff_idx] = 3;
                         } else {
                             coeff = 2;
-                            s->coeff_ctx[vp56_b6to4[b]][coeff_idx] = 2;
+                            s->coeff_ctx[ff_vp56_b6to4[b]][coeff_idx] = 2;
                         }
                         sign = vp56_rac_get(c);
                     }
                     ct = 2;
                 } else {
                     ct = 1;
-                    s->coeff_ctx[vp56_b6to4[b]][coeff_idx] = 1;
+                    s->coeff_ctx[ff_vp56_b6to4[b]][coeff_idx] = 1;
                     sign = vp56_rac_get(c);
                     coeff = 1;
                 }
@@ -234,21 +228,24 @@ static void vp5_parse_coeff(VP56Context *s)
                 if (ct && !vp56_rac_get_prob(c, model2[1]))
                     break;
                 ct = 0;
-                s->coeff_ctx[vp56_b6to4[b]][coeff_idx] = 0;
+                s->coeff_ctx[ff_vp56_b6to4[b]][coeff_idx] = 0;
             }
+            coeff_idx++;
+            if (coeff_idx >= 64)
+                break;
 
-            cg = vp5_coeff_groups[++coeff_idx];
-            ctx = s->coeff_ctx[vp56_b6to4[b]][coeff_idx];
+            cg = vp5_coeff_groups[coeff_idx];
+            ctx = s->coeff_ctx[ff_vp56_b6to4[b]][coeff_idx];
             model1 = model->coeff_ract[pt][ct][cg];
             model2 = cg > 2 ? model1 : model->coeff_acct[pt][ct][cg][ctx];
         }
 
-        ctx_last = FFMIN(s->coeff_ctx_last[vp56_b6to4[b]], 24);
-        s->coeff_ctx_last[vp56_b6to4[b]] = coeff_idx;
+        ctx_last = FFMIN(s->coeff_ctx_last[ff_vp56_b6to4[b]], 24);
+        s->coeff_ctx_last[ff_vp56_b6to4[b]] = coeff_idx;
         if (coeff_idx < ctx_last)
             for (i=coeff_idx; i<=ctx_last; i++)
-                s->coeff_ctx[vp56_b6to4[b]][i] = 5;
-        s->above_blocks[s->above_block_idx[b]].not_null_dc = s->coeff_ctx[vp56_b6to4[b]][0];
+                s->coeff_ctx[ff_vp56_b6to4[b]][i] = 5;
+        s->above_blocks[s->above_block_idx[b]].not_null_dc = s->coeff_ctx[ff_vp56_b6to4[b]][0];
     }
 }
 
@@ -263,18 +260,19 @@ static void vp5_default_models_init(VP56Context *s)
         model->vector_pdi[i][0] = 0x55;
         model->vector_pdi[i][1] = 0x80;
     }
-    memcpy(model->mb_types_stats, vp56_def_mb_types_stats, sizeof(model->mb_types_stats));
+    memcpy(model->mb_types_stats, ff_vp56_def_mb_types_stats, sizeof(model->mb_types_stats));
     memset(model->vector_pdv, 0x80, sizeof(model->vector_pdv));
 }
 
 static av_cold int vp5_decode_init(AVCodecContext *avctx)
 {
     VP56Context *s = avctx->priv_data;
+    int ret;
 
-    vp56_init(avctx, 1, 0);
+    if ((ret = ff_vp56_init(avctx, 1, 0)) < 0)
+        return ret;
     s->vp56_coord_div = vp5_coord_div;
     s->parse_vector_adjustment = vp5_parse_vector_adjustment;
-    s->adjust = vp5_adjust;
     s->parse_coeff = vp5_parse_coeff;
     s->default_models_init = vp5_default_models_init;
     s->parse_vector_models = vp5_parse_vector_models;
@@ -284,15 +282,14 @@ static av_cold int vp5_decode_init(AVCodecContext *avctx)
     return 0;
 }
 
-AVCodec vp5_decoder = {
-    "vp5",
-    AVMEDIA_TYPE_VIDEO,
-    CODEC_ID_VP5,
-    sizeof(VP56Context),
-    vp5_decode_init,
-    NULL,
-    vp56_free,
-    vp56_decode_frame,
-    CODEC_CAP_DR1,
-    .long_name = NULL_IF_CONFIG_SMALL("On2 VP5"),
+AVCodec ff_vp5_decoder = {
+    .name           = "vp5",
+    .long_name      = NULL_IF_CONFIG_SMALL("On2 VP5"),
+    .type           = AVMEDIA_TYPE_VIDEO,
+    .id             = AV_CODEC_ID_VP5,
+    .priv_data_size = sizeof(VP56Context),
+    .init           = vp5_decode_init,
+    .close          = ff_vp56_free,
+    .decode         = ff_vp56_decode_frame,
+    .capabilities   = AV_CODEC_CAP_DR1,
 };