]> git.sesse.net Git - ffmpeg/blobdiff - libavcodec/vp3.c
avcodec: Constify AVCodecs
[ffmpeg] / libavcodec / vp3.c
index 34c48f8725d8b2341cc4445127f2ad62ee297179..0cae07545233412634d1a6a4cdecdbec376c44af 100644 (file)
@@ -35,6 +35,7 @@
 #include <string.h>
 
 #include "libavutil/imgutils.h"
+#include "libavutil/mem_internal.h"
 
 #include "avcodec.h"
 #include "get_bits.h"
@@ -48,6 +49,7 @@
 #include "vp3dsp.h"
 #include "xiph.h"
 
+#define VP3_MV_VLC_BITS     6
 #define VP4_MV_VLC_BITS     6
 #define SUPERBLOCK_VLC_BITS 6
 
@@ -946,8 +948,10 @@ static int unpack_vectors(Vp3DecodeContext *s, GetBitContext *gb)
                 case MODE_INTER_PLUS_MV:
                     /* all 6 fragments use the same motion vector */
                     if (coding_mode == 0) {
-                        motion_x[0] = motion_vector_table[get_vlc2(gb, s->motion_vector_vlc.table, 6, 2)];
-                        motion_y[0] = motion_vector_table[get_vlc2(gb, s->motion_vector_vlc.table, 6, 2)];
+                        motion_x[0] = get_vlc2(gb, s->motion_vector_vlc.table,
+                                               VP3_MV_VLC_BITS, 2);
+                        motion_y[0] = get_vlc2(gb, s->motion_vector_vlc.table,
+                                               VP3_MV_VLC_BITS, 2);
                     } else if (coding_mode == 1) {
                         motion_x[0] = fixed_motion_vector_table[get_bits(gb, 6)];
                         motion_y[0] = fixed_motion_vector_table[get_bits(gb, 6)];
@@ -976,8 +980,10 @@ static int unpack_vectors(Vp3DecodeContext *s, GetBitContext *gb)
                         current_fragment = BLOCK_Y * s->fragment_width[0] + BLOCK_X;
                         if (s->all_fragments[current_fragment].coding_method != MODE_COPY) {
                             if (coding_mode == 0) {
-                                motion_x[k] = motion_vector_table[get_vlc2(gb, s->motion_vector_vlc.table, 6, 2)];
-                                motion_y[k] = motion_vector_table[get_vlc2(gb, s->motion_vector_vlc.table, 6, 2)];
+                                motion_x[k] = get_vlc2(gb, s->motion_vector_vlc.table,
+                                                       VP3_MV_VLC_BITS, 2);
+                                motion_y[k] = get_vlc2(gb, s->motion_vector_vlc.table,
+                                                       VP3_MV_VLC_BITS, 2);
                             } else if (coding_mode == 1) {
                                 motion_x[k] = fixed_motion_vector_table[get_bits(gb, 6)];
                                 motion_y[k] = fixed_motion_vector_table[get_bits(gb, 6)];
@@ -2314,20 +2320,6 @@ static av_cold int init_frames(Vp3DecodeContext *s)
     return 0;
 }
 
-static av_cold int theora_init_huffman_tables(VLC *vlc, const HuffTable *huff)
-{
-    uint32_t code = 0, codes[32];
-
-    for (unsigned i = 0; i < huff->nb_entries; i++) {
-        codes[i] = code        >> (31 - huff->entries[i].len);
-        code    += 0x80000000U >> huff->entries[i].len;
-    }
-    return ff_init_vlc_sparse(vlc, 11, huff->nb_entries,
-                              &huff->entries[0].len, sizeof(huff->entries[0]), 1,
-                              codes, 4, 4,
-                              &huff->entries[0].sym, sizeof(huff->entries[0]), 1, 0);
-}
-
 static av_cold int vp3_decode_init(AVCodecContext *avctx)
 {
     Vp3DecodeContext *s = avctx->priv_data;
@@ -2434,24 +2426,33 @@ static av_cold int vp3_decode_init(AVCodecContext *avctx)
         /* init VLC tables */
         if (s->version < 2) {
             for (i = 0; i < FF_ARRAY_ELEMS(s->coeff_vlc); i++) {
-                if ((ret = init_vlc(&s->coeff_vlc[i], 11, 32,
-                                    &vp3_bias[i][0][1], 4, 2,
-                                    &vp3_bias[i][0][0], 4, 2, 0)) < 0)
+                ret = ff_init_vlc_from_lengths(&s->coeff_vlc[i], 11, 32,
+                                               &vp3_bias[i][0][1], 2,
+                                               &vp3_bias[i][0][0], 2, 1,
+                                               0, 0, avctx);
+                if (ret < 0)
                     return ret;
             }
 #if CONFIG_VP4_DECODER
         } else { /* version >= 2 */
             for (i = 0; i < FF_ARRAY_ELEMS(s->coeff_vlc); i++) {
-                if ((ret = init_vlc(&s->coeff_vlc[i], 11, 32,
-                                    &vp4_bias[i][0][1], 4, 2,
-                                    &vp4_bias[i][0][0], 4, 2, 0)) < 0)
+                ret = ff_init_vlc_from_lengths(&s->coeff_vlc[i], 11, 32,
+                                               &vp4_bias[i][0][1], 2,
+                                               &vp4_bias[i][0][0], 2, 1,
+                                               0, 0, avctx);
+                if (ret < 0)
                     return ret;
             }
 #endif
         }
     } else {
         for (i = 0; i < FF_ARRAY_ELEMS(s->coeff_vlc); i++) {
-            ret = theora_init_huffman_tables(&s->coeff_vlc[i], &s->huffman_table[i]);
+            const HuffTable *tab = &s->huffman_table[i];
+
+            ret = ff_init_vlc_from_lengths(&s->coeff_vlc[i], 11, tab->nb_entries,
+                                           &tab->entries[0].len, sizeof(*tab->entries),
+                                           &tab->entries[0].sym, sizeof(*tab->entries), 1,
+                                           0, 0, avctx);
             if (ret < 0)
                 return ret;
         }
@@ -2463,19 +2464,23 @@ static av_cold int vp3_decode_init(AVCodecContext *avctx)
     if (ret < 0)
         return ret;
 
-    if ((ret = init_vlc(&s->fragment_run_length_vlc, 5, 30,
-                        &fragment_run_length_vlc_table[0][1], 4, 2,
-                        &fragment_run_length_vlc_table[0][0], 4, 2, 0)) < 0)
+    ret = ff_init_vlc_from_lengths(&s->fragment_run_length_vlc, 5, 30,
+                                   fragment_run_length_vlc_len, 1,
+                                   NULL, 0, 0, 0, 0, avctx);
+    if (ret < 0)
         return ret;
 
-    if ((ret = init_vlc(&s->mode_code_vlc, 3, 8,
-                        &mode_code_vlc_table[0][1], 2, 1,
-                        &mode_code_vlc_table[0][0], 2, 1, 0)) < 0)
+    ret = ff_init_vlc_from_lengths(&s->mode_code_vlc, 3, 8,
+                                   mode_code_vlc_len, 1,
+                                   NULL, 0, 0, 0, 0, avctx);
+    if (ret < 0)
         return ret;
 
-    if ((ret = init_vlc(&s->motion_vector_vlc, 6, 63,
-                        &motion_vector_vlc_table[0][1], 2, 1,
-                        &motion_vector_vlc_table[0][0], 2, 1, 0)) < 0)
+    ret = ff_init_vlc_from_lengths(&s->motion_vector_vlc, VP3_MV_VLC_BITS, 63,
+                                   &motion_vector_vlc_table[0][1], 2,
+                                   &motion_vector_vlc_table[0][0], 2, 1,
+                                   -31, 0, avctx);
+    if (ret < 0)
         return ret;
 
 #if CONFIG_VP4_DECODER
@@ -2870,6 +2875,9 @@ static int theora_decode_header(AVCodecContext *avctx, GetBitContext *gb)
     int ret;
     AVRational fps, aspect;
 
+    if (get_bits_left(gb) < 206)
+        return AVERROR_INVALIDDATA;
+
     s->theora_header = 0;
     s->theora = get_bits(gb, 24);
     av_log(avctx, AV_LOG_DEBUG, "Theora bitstream version %X\n", s->theora);
@@ -3151,7 +3159,7 @@ static av_cold int theora_decode_init(AVCodecContext *avctx)
     return vp3_decode_init(avctx);
 }
 
-AVCodec ff_theora_decoder = {
+const AVCodec ff_theora_decoder = {
     .name                  = "theora",
     .long_name             = NULL_IF_CONFIG_SMALL("Theora"),
     .type                  = AVMEDIA_TYPE_VIDEO,
@@ -3169,7 +3177,7 @@ AVCodec ff_theora_decoder = {
 };
 #endif
 
-AVCodec ff_vp3_decoder = {
+const AVCodec ff_vp3_decoder = {
     .name                  = "vp3",
     .long_name             = NULL_IF_CONFIG_SMALL("On2 VP3"),
     .type                  = AVMEDIA_TYPE_VIDEO,
@@ -3186,7 +3194,7 @@ AVCodec ff_vp3_decoder = {
 };
 
 #if CONFIG_VP4_DECODER
-AVCodec ff_vp4_decoder = {
+const AVCodec ff_vp4_decoder = {
     .name                  = "vp4",
     .long_name             = NULL_IF_CONFIG_SMALL("On2 VP4"),
     .type                  = AVMEDIA_TYPE_VIDEO,