]> git.sesse.net Git - ffmpeg/blobdiff - libavcodec/motionpixels.c
avcodec: Constify AVCodecs
[ffmpeg] / libavcodec / motionpixels.c
index a88b837b3ef7ab980a44d88b4c1fea611011f948..1896330fe147ec0a2e03bf4463725ef74a87783b 100644 (file)
  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  */
 
+#include "libavutil/thread.h"
+
+#include "config.h"
+
 #include "avcodec.h"
 #include "get_bits.h"
 #include "bswapdsp.h"
@@ -29,7 +33,6 @@
 #include "motionpixels_tablegen.h"
 
 typedef struct HuffCode {
-    int code;
     uint8_t size;
     uint8_t delta;
 } HuffCode;
@@ -65,6 +68,7 @@ static av_cold int mp_decode_end(AVCodecContext *avctx)
 
 static av_cold int mp_decode_init(AVCodecContext *avctx)
 {
+    av_unused static AVOnce init_static_once = AV_ONCE_INIT;
     MotionPixelsContext *mp = avctx->priv_data;
     int w4 = (avctx->width  + 3) & ~3;
     int h4 = (avctx->height + 3) & ~3;
@@ -74,26 +78,23 @@ static av_cold int mp_decode_init(AVCodecContext *avctx)
         return AVERROR_INVALIDDATA;
     }
 
-    motionpixels_tableinit();
     mp->avctx = avctx;
     ff_bswapdsp_init(&mp->bdsp);
     mp->changes_map = av_mallocz_array(avctx->width, h4);
     mp->offset_bits_len = av_log2(avctx->width * avctx->height) + 1;
     mp->vpt = av_mallocz_array(avctx->height, sizeof(YuvPixel));
     mp->hpt = av_mallocz_array(h4 / 4, w4 / 4 * sizeof(YuvPixel));
-    if (!mp->changes_map || !mp->vpt || !mp->hpt) {
-        av_freep(&mp->changes_map);
-        av_freep(&mp->vpt);
-        av_freep(&mp->hpt);
+    if (!mp->changes_map || !mp->vpt || !mp->hpt)
         return AVERROR(ENOMEM);
-    }
     avctx->pix_fmt = AV_PIX_FMT_RGB555;
 
     mp->frame = av_frame_alloc();
-    if (!mp->frame) {
-        mp_decode_end(avctx);
+    if (!mp->frame)
         return AVERROR(ENOMEM);
-    }
+
+#if !CONFIG_HARDCODED_TABLES
+    ff_thread_once(&init_static_once, motionpixels_tableinit);
+#endif
 
     return 0;
 }
@@ -127,7 +128,7 @@ static void mp_read_changes_map(MotionPixelsContext *mp, GetBitContext *gb, int
     }
 }
 
-static int mp_get_code(MotionPixelsContext *mp, GetBitContext *gb, int size, int code)
+static int mp_get_code(MotionPixelsContext *mp, GetBitContext *gb, int size)
 {
     while (get_bits1(gb)) {
         ++size;
@@ -135,16 +136,14 @@ static int mp_get_code(MotionPixelsContext *mp, GetBitContext *gb, int size, int
             av_log(mp->avctx, AV_LOG_ERROR, "invalid code size %d/%d\n", size, mp->max_codes_bits);
             return AVERROR_INVALIDDATA;
         }
-        code <<= 1;
-        if (mp_get_code(mp, gb, size, code + 1) < 0)
+        if (mp_get_code(mp, gb, size) < 0)
             return AVERROR_INVALIDDATA;
     }
-    if (mp->current_codes_count >= MAX_HUFF_CODES) {
+    if (mp->current_codes_count >= mp->codes_count) {
         av_log(mp->avctx, AV_LOG_ERROR, "too many codes\n");
         return AVERROR_INVALIDDATA;
     }
 
-    mp->codes[mp->current_codes_count  ].code = code;
     mp->codes[mp->current_codes_count++].size = size;
     return 0;
 }
@@ -161,7 +160,7 @@ static int mp_read_codes_table(MotionPixelsContext *mp, GetBitContext *gb)
         for (i = 0; i < mp->codes_count; ++i)
             mp->codes[i].delta = get_bits(gb, 4);
         mp->current_codes_count = 0;
-        if ((ret = mp_get_code(mp, gb, 0, 0)) < 0)
+        if ((ret = mp_get_code(mp, gb, 0)) < 0)
             return ret;
         if (mp->current_codes_count < mp->codes_count) {
             av_log(mp->avctx, AV_LOG_ERROR, "too few codes\n");
@@ -171,7 +170,7 @@ static int mp_read_codes_table(MotionPixelsContext *mp, GetBitContext *gb)
    return 0;
 }
 
-static int mp_gradient(MotionPixelsContext *mp, int component, int v)
+static av_always_inline int mp_gradient(MotionPixelsContext *mp, int component, int v)
 {
     int delta;
 
@@ -196,12 +195,10 @@ static void mp_set_rgb_from_yuv(MotionPixelsContext *mp, int x, int y, const Yuv
     *(uint16_t *)&mp->frame->data[0][y * mp->frame->linesize[0] + x * 2] = color;
 }
 
-static int mp_get_vlc(MotionPixelsContext *mp, GetBitContext *gb)
+static av_always_inline int mp_get_vlc(MotionPixelsContext *mp, GetBitContext *gb)
 {
-    int i;
-
-    i = (mp->codes_count == 1) ? 0 : get_vlc2(gb, mp->vlc.table, mp->max_codes_bits, 1);
-    return mp->codes[i].delta;
+    return mp->vlc.table ? get_vlc2(gb, mp->vlc.table, mp->max_codes_bits, 1)
+                         : mp->codes[0].delta;
 }
 
 static void mp_decode_line(MotionPixelsContext *mp, GetBitContext *gb, int y)
@@ -290,7 +287,7 @@ static int mp_decode_frame(AVCodecContext *avctx,
     GetBitContext gb;
     int i, count1, count2, sz, ret;
 
-    if ((ret = ff_reget_buffer(avctx, mp->frame)) < 0)
+    if ((ret = ff_reget_buffer(avctx, mp->frame, 0)) < 0)
         return ret;
 
     /* le32 bitstream msb first */
@@ -328,10 +325,16 @@ static int mp_decode_frame(AVCodecContext *avctx,
     if (sz == 0)
         goto end;
 
-    if (mp->max_codes_bits <= 0)
-        goto end;
-    if (init_vlc(&mp->vlc, mp->max_codes_bits, mp->codes_count, &mp->codes[0].size, sizeof(HuffCode), 1, &mp->codes[0].code, sizeof(HuffCode), 4, 0))
-        goto end;
+    if (mp->codes_count > 1) {
+        /* The entries of the mp->codes array are sorted from right to left
+         * in the Huffman tree, hence -(int)sizeof(HuffCode). */
+        ret = ff_init_vlc_from_lengths(&mp->vlc, mp->max_codes_bits, mp->codes_count,
+                                       &mp->codes[mp->codes_count - 1].size,  -(int)sizeof(HuffCode),
+                                       &mp->codes[mp->codes_count - 1].delta, -(int)sizeof(HuffCode), 1,
+                                       0, 0, avctx);
+        if (ret < 0)
+            goto end;
+    }
     mp_decode_frame_helper(mp, &gb);
     ff_free_vlc(&mp->vlc);
 
@@ -342,7 +345,7 @@ end:
     return buf_size;
 }
 
-AVCodec ff_motionpixels_decoder = {
+const AVCodec ff_motionpixels_decoder = {
     .name           = "motionpixels",
     .long_name      = NULL_IF_CONFIG_SMALL("Motion Pixels video"),
     .type           = AVMEDIA_TYPE_VIDEO,
@@ -352,4 +355,5 @@ AVCodec ff_motionpixels_decoder = {
     .close          = mp_decode_end,
     .decode         = mp_decode_frame,
     .capabilities   = AV_CODEC_CAP_DR1,
+    .caps_internal  = FF_CODEC_CAP_INIT_CLEANUP | FF_CODEC_CAP_INIT_THREADSAFE,
 };