]> git.sesse.net Git - ffmpeg/blobdiff - libavcodec/mobiclip.c
avcodec: Constify AVCodecs
[ffmpeg] / libavcodec / mobiclip.c
index e50e4d18c543206c1175a744795fe1d7c3d31684..6ea1c3eefa170c8e6a4f007ec0450be77bf2c46f 100644 (file)
@@ -1,5 +1,6 @@
 /*
  * MobiClip Video decoder
+ * Copyright (c) 2015-2016 Florian Nouwt
  * Copyright (c) 2017 Adib Surani
  * Copyright (c) 2020 Paul B Mahol
  *
@@ -23,6 +24,7 @@
 #include <inttypes.h>
 
 #include "libavutil/avassert.h"
+#include "libavutil/thread.h"
 
 #include "avcodec.h"
 #include "bytestream.h"
@@ -160,80 +162,80 @@ static const uint8_t mv_len[16] =
 static const uint8_t mv_bits[2][16][10] =
 {
     {
-    { 2, 3, 3, 5, 5, 4, 4, 5, 5, 2 },
-    { 2, 3, 4, 4, 3, 4, 4, 2 },
-    { 3, 4, 4, 2, 4, 4, 3, 2 },
-    { 1, 3, 4, 5, 5, 3, 3 },
-    { 2, 4, 4, 3, 3, 4, 4, 2 },
-    { 2, 3, 4, 4, 4, 4, 3, 2 },
-    { 2, 3, 4, 4, 4, 4, 3, 2 },
-    { 2, 2, 3, 4, 5, 5, 2 },
-    { 2, 3, 4, 4, 3, 4, 4, 2 },
-    { 2, 4, 4, 3, 4, 4, 3, 2 },
-    { 2, 3, 3, 5, 5, 4, 3, 2 },
-    { 2, 3, 4, 4, 3, 3, 2 },
-    { 1, 4, 4, 3, 3, 4, 4 },
-    { 2, 3, 4, 4, 3, 3, 2 },
-    { 2, 3, 4, 4, 3, 3, 2 },
-    { 3, 3, 2, 2, 3, 3 },
+        { 2, 3, 3, 5, 5, 4, 4, 5, 5, 2 },
+        { 2, 3, 4, 4, 3, 4, 4, 2 },
+        { 3, 4, 4, 2, 4, 4, 3, 2 },
+        { 1, 3, 4, 5, 5, 3, 3 },
+        { 2, 4, 4, 3, 3, 4, 4, 2 },
+        { 2, 3, 4, 4, 4, 4, 3, 2 },
+        { 2, 3, 4, 4, 4, 4, 3, 2 },
+        { 2, 2, 3, 4, 5, 5, 2 },
+        { 2, 3, 4, 4, 3, 4, 4, 2 },
+        { 2, 4, 4, 3, 4, 4, 3, 2 },
+        { 2, 3, 3, 5, 5, 4, 3, 2 },
+        { 2, 3, 4, 4, 3, 3, 2 },
+        { 1, 4, 4, 3, 3, 4, 4 },
+        { 2, 3, 4, 4, 3, 3, 2 },
+        { 2, 3, 4, 4, 3, 3, 2 },
+        { 3, 3, 2, 2, 3, 3 },
     },
     {
-    { 3, 4, 5, 5, 3, 5, 6, 6, 4, 1 },
-    { 2, 3, 4, 5, 5, 2, 3, 3 },
-    { 2, 4, 4, 3, 3, 4, 4, 2 },
-    { 1, 4, 4, 3, 4, 4, 3 },
-    { 3, 3, 2, 4, 5, 5, 3, 2 },
-    { 3, 4, 4, 3, 3, 3, 3, 2 },
-    { 1, 3, 3, 4, 4, 4, 5, 5 },
-    { 1, 4, 4, 3, 3, 4, 4 },
-    { 2, 4, 4, 3, 3, 4, 4, 2 },
-    { 1, 3, 3, 4, 4, 4, 5, 5 },
-    { 2, 3, 4, 4, 4, 4, 3, 2 },
-    { 2, 3, 3, 4, 4, 3, 2 },
-    { 1, 4, 4, 3, 3, 4, 4 },
-    { 1, 4, 4, 3, 3, 4, 4 },
-    { 2, 3, 3, 4, 4, 3, 2 },
-    { 2, 3, 3, 3, 3, 2 },
+        { 3, 4, 5, 5, 3, 5, 6, 6, 4, 1 },
+        { 2, 3, 4, 5, 5, 2, 3, 3 },
+        { 2, 4, 4, 3, 3, 4, 4, 2 },
+        { 1, 4, 4, 3, 4, 4, 3 },
+        { 3, 3, 2, 4, 5, 5, 3, 2 },
+        { 3, 4, 4, 3, 3, 3, 3, 2 },
+        { 1, 3, 3, 4, 4, 4, 5, 5 },
+        { 1, 4, 4, 3, 3, 4, 4 },
+        { 2, 4, 4, 3, 3, 4, 4, 2 },
+        { 1, 3, 3, 4, 4, 4, 5, 5 },
+        { 2, 3, 4, 4, 4, 4, 3, 2 },
+        { 2, 3, 3, 4, 4, 3, 2 },
+        { 1, 4, 4, 3, 3, 4, 4 },
+        { 1, 4, 4, 3, 3, 4, 4 },
+        { 2, 3, 3, 4, 4, 3, 2 },
+        { 2, 3, 3, 3, 3, 2 },
     }
 };
 
 static const uint8_t mv_syms[2][16][10] =
 {
     {
-    { 1, 8, 9, 4, 3, 2, 7, 5, 6, 0 },
-    { 0, 9, 5, 4, 2, 3, 8, 1 },
-    { 3, 9, 5, 0, 4, 8, 2, 1 },
-    { 1, 3, 4, 8, 5, 2, 0 },
-    { 0, 5, 4, 8, 2, 3, 9, 1 },
-    { 0, 3, 5, 9, 4, 8, 2, 1 },
-    { 0, 3, 9, 5, 8, 4, 2, 1 },
-    { 0, 2, 3, 4, 8, 5, 1 },
-    { 0, 3, 8, 4, 2, 5, 9, 1 },
-    { 2, 8, 9, 3, 5, 4, 0, 1 },
-    { 0, 4, 3, 8, 9, 5, 2, 1 },
-    { 0, 4, 8, 5, 3, 2, 1 },
-    { 1, 9, 4, 2, 0, 5, 3 },
-    { 2, 4, 9, 5, 3, 0, 1 },
-    { 0, 4, 9, 5, 3, 2, 1 },
-    { 5, 4, 1, 0, 3, 2 },
+        { 1, 8, 9, 4, 3, 2, 7, 5, 6, 0 },
+        { 0, 9, 5, 4, 2, 3, 8, 1 },
+        { 3, 9, 5, 0, 4, 8, 2, 1 },
+        { 1, 3, 4, 8, 5, 2, 0 },
+        { 0, 5, 4, 8, 2, 3, 9, 1 },
+        { 0, 3, 5, 9, 4, 8, 2, 1 },
+        { 0, 3, 9, 5, 8, 4, 2, 1 },
+        { 0, 2, 3, 4, 8, 5, 1 },
+        { 0, 3, 8, 4, 2, 5, 9, 1 },
+        { 2, 8, 9, 3, 5, 4, 0, 1 },
+        { 0, 4, 3, 8, 9, 5, 2, 1 },
+        { 0, 4, 8, 5, 3, 2, 1 },
+        { 1, 9, 4, 2, 0, 5, 3 },
+        { 2, 4, 9, 5, 3, 0, 1 },
+        { 0, 4, 9, 5, 3, 2, 1 },
+        { 5, 4, 1, 0, 3, 2 },
     },
     {
-    { 8, 2, 3, 6, 1, 7, 5, 4, 9, 0 },
-    { 9, 2, 3, 5, 4, 1, 8, 0 },
-    { 0, 5, 4, 2, 9, 3, 8, 1 },
-    { 1, 5, 4, 2, 8, 3, 0 },
-    { 2, 9, 8, 3, 5, 4, 0, 1 },
-    { 3, 5, 4, 2, 9, 8, 0, 1 },
-    { 1, 2, 0, 9, 8, 3, 5, 4 },
-    { 1, 8, 5, 2, 0, 4, 3 },
-    { 0, 5, 4, 2, 8, 3, 9, 1 },
-    { 1, 2, 0, 9, 8, 3, 5, 4 },
-    { 0, 3, 9, 8, 5, 4, 2, 1 },
-    { 0, 4, 3, 8, 5, 2, 1 },
-    { 1, 5, 4, 2, 0, 9, 3 },
-    { 1, 9, 5, 2, 0, 4, 3 },
-    { 0, 5, 3, 9, 4, 2, 1 },
-    { 0, 4, 5, 3, 2, 1 },
+        { 8, 2, 3, 6, 1, 7, 5, 4, 9, 0 },
+        { 9, 2, 3, 5, 4, 1, 8, 0 },
+        { 0, 5, 4, 2, 9, 3, 8, 1 },
+        { 1, 5, 4, 2, 8, 3, 0 },
+        { 2, 9, 8, 3, 5, 4, 0, 1 },
+        { 3, 5, 4, 2, 9, 8, 0, 1 },
+        { 1, 2, 0, 9, 8, 3, 5, 4 },
+        { 1, 8, 5, 2, 0, 4, 3 },
+        { 0, 5, 4, 2, 8, 3, 9, 1 },
+        { 1, 2, 0, 9, 8, 3, 5, 4 },
+        { 0, 3, 9, 8, 5, 4, 2, 1 },
+        { 0, 4, 3, 8, 5, 2, 1 },
+        { 1, 5, 4, 2, 0, 9, 3 },
+        { 1, 9, 5, 2, 0, 4, 3 },
+        { 0, 5, 3, 9, 4, 2, 1 },
+        { 0, 4, 5, 3, 2, 1 },
     }
 };
 
@@ -274,8 +276,32 @@ typedef struct MobiClipContext {
 static VLC rl_vlc[2];
 static VLC mv_vlc[2][16];
 
+static av_cold void mobiclip_init_static(void)
+{
+    INIT_VLC_STATIC_FROM_LENGTHS(&rl_vlc[0], MOBI_RL_VLC_BITS, 104,
+                                 bits0, sizeof(*bits0),
+                                 syms0, sizeof(*syms0), sizeof(*syms0),
+                                 0, 0, 1 << MOBI_RL_VLC_BITS);
+    INIT_VLC_STATIC_FROM_LENGTHS(&rl_vlc[1], MOBI_RL_VLC_BITS, 104,
+                                 bits0, sizeof(*bits0),
+                                 syms1, sizeof(*syms1), sizeof(*syms1),
+                                 0, 0, 1 << MOBI_RL_VLC_BITS);
+    for (int i = 0; i < 2; i++) {
+        static VLC_TYPE vlc_buf[2 * 16 << MOBI_MV_VLC_BITS][2];
+        for (int j = 0; j < 16; j++) {
+            mv_vlc[i][j].table           = &vlc_buf[(16 * i + j) << MOBI_MV_VLC_BITS];
+            mv_vlc[i][j].table_allocated = 1 << MOBI_MV_VLC_BITS;
+            ff_init_vlc_from_lengths(&mv_vlc[i][j], MOBI_MV_VLC_BITS, mv_len[j],
+                                     mv_bits[i][j], sizeof(*mv_bits[i][j]),
+                                     mv_syms[i][j], sizeof(*mv_syms[i][j]), sizeof(*mv_syms[i][j]),
+                                     0, INIT_VLC_USE_NEW_STATIC, NULL);
+        }
+    }
+}
+
 static av_cold int mobiclip_init(AVCodecContext *avctx)
 {
+    static AVOnce init_static_once = AV_ONCE_INIT;
     MobiClipContext *s = avctx->priv_data;
 
     if (avctx->width & 15 || avctx->height & 15) {
@@ -298,25 +324,7 @@ static av_cold int mobiclip_init(AVCodecContext *avctx)
             return AVERROR(ENOMEM);
     }
 
-    INIT_VLC_STATIC_FROM_LENGTHS(&rl_vlc[0], MOBI_RL_VLC_BITS, 104,
-                                 bits0, sizeof(*bits0),
-                           syms0,  sizeof(*syms0),  sizeof(*syms0),
-                                 0, 0, 1 << MOBI_RL_VLC_BITS);
-    INIT_VLC_STATIC_FROM_LENGTHS(&rl_vlc[1], MOBI_RL_VLC_BITS, 104,
-                                 bits0, sizeof(*bits0),
-                           syms1,  sizeof(*syms1),  sizeof(*syms1),
-                                 0, 0, 1 << MOBI_RL_VLC_BITS);
-    for (int i = 0; i < 2; i++) {
-        static VLC_TYPE vlc_buf[2 * 16 << MOBI_MV_VLC_BITS][2];
-        for (int j = 0; j < 16; j++) {
-            mv_vlc[i][j].table           = &vlc_buf[(16 * i + j) << MOBI_MV_VLC_BITS];
-            mv_vlc[i][j].table_allocated = 1 << MOBI_MV_VLC_BITS;
-            ff_init_vlc_from_lengths(&mv_vlc[i][j], MOBI_MV_VLC_BITS, mv_len[j],
-                                     mv_bits[i][j], sizeof(*mv_bits[i][j]),
-                                     mv_syms[i][j], sizeof(*mv_syms[i][j]), sizeof(*mv_syms[i][j]),
-                                     0, INIT_VLC_USE_NEW_STATIC, NULL);
-        }
-    }
+    ff_thread_once(&init_static_once, mobiclip_init_static);
 
     return 0;
 }
@@ -537,7 +545,7 @@ static uint8_t half(int a, int b)
 
 static uint8_t half3(int a, int b, int c)
 {
-    return ((a + b + b + c) * 2 / 4 + 1) / 2;;
+    return ((a + b + b + c) * 2 / 4 + 1) / 2;
 }
 
 static uint8_t pick_above(BlockXY bxy)
@@ -841,7 +849,7 @@ static int predict_intra(AVCodecContext *avctx, AVFrame *frame, int ax, int ay,
             uint8_t *left = frame->data[plane] + ay * frame->linesize[plane] + FFMAX(ax - 1, 0);
             int bottommost = frame->data[plane][(ay + size - 1) * frame->linesize[plane] + FFMAX(ax - 1, 0)];
             int rightmost = frame->data[plane][FFMAX(ay - 1, 0) * frame->linesize[plane] + ax + size - 1];
-            int avg = (bottommost + rightmost + 1) / 2 + 2 * get_se_golomb(gb);
+            int avg = (bottommost + rightmost + 1) / 2 + 2 * av_clip(get_se_golomb(gb), -(1<<16), 1<<16);
             int r6 = adjust(avg - bottommost, size);
             int r9 = adjust(avg - rightmost, size);
             int shift = adjust(size, size) == 8 ? 3 : 2;
@@ -1084,8 +1092,8 @@ static int predict_motion(AVCodecContext *avctx,
             sidx += 6;
 
         if (index > 0) {
-            mv.x = mv.x + get_se_golomb(gb);
-            mv.y = mv.y + get_se_golomb(gb);
+            mv.x = mv.x + (unsigned)get_se_golomb(gb);
+            mv.y = mv.y + (unsigned)get_se_golomb(gb);
         }
         if (mv.x >= INT_MAX || mv.y >= INT_MAX)
             return AVERROR_INVALIDDATA;
@@ -1332,7 +1340,7 @@ static av_cold int mobiclip_close(AVCodecContext *avctx)
     return 0;
 }
 
-AVCodec ff_mobiclip_decoder = {
+const AVCodec ff_mobiclip_decoder = {
     .name           = "mobiclip",
     .long_name      = NULL_IF_CONFIG_SMALL("MobiClip Video"),
     .type           = AVMEDIA_TYPE_VIDEO,
@@ -1343,5 +1351,5 @@ AVCodec ff_mobiclip_decoder = {
     .flush          = mobiclip_flush,
     .close          = mobiclip_close,
     .capabilities   = AV_CODEC_CAP_DR1,
-    .caps_internal  = FF_CODEC_CAP_INIT_CLEANUP,
+    .caps_internal  = FF_CODEC_CAP_INIT_THREADSAFE | FF_CODEC_CAP_INIT_CLEANUP,
 };