*/
#include "libavutil/attributes.h"
+#include "libavutil/thread.h"
#define BITSTREAM_READER_LE
#include "avcodec.h"
/* Indeo 2 codes are in range 0x01..0x7F and 0x81..0x90 */
static inline int ir2_get_code(GetBitContext *gb)
{
- return get_vlc2(gb, ir2_vlc.table, CODE_VLC_BITS, 1) + 1;
+ return get_vlc2(gb, ir2_vlc.table, CODE_VLC_BITS, 1);
}
static int ir2_decode_plane(Ir2Context *ctx, int width, int height, uint8_t *dst,
int j;
int out = 0;
- if (width & 1)
+ if ((width & 1) || width * height / (2*(IR2_CODES - 0x7F)) > get_bits_left(&ctx->gb))
return AVERROR_INVALIDDATA;
/* first line contain absolute values, other lines contain deltas */
for (j = 1; j < height; j++) {
out = 0;
- if (get_bits_left(&ctx->gb) <= 0)
- return AVERROR_INVALIDDATA;
while (out < width) {
- int c = ir2_get_code(&ctx->gb);
+ int c;
+ if (get_bits_left(&ctx->gb) <= 0)
+ return AVERROR_INVALIDDATA;
+ c = ir2_get_code(&ctx->gb);
if (c >= 0x80) { /* we have a skip */
c -= 0x7F;
if (out + c*2 > width)
for (j = 0; j < height; j++) {
out = 0;
- if (get_bits_left(&ctx->gb) <= 0)
- return AVERROR_INVALIDDATA;
while (out < width) {
+ if (get_bits_left(&ctx->gb) <= 0)
+ return AVERROR_INVALIDDATA;
c = ir2_get_code(&ctx->gb);
if (c >= 0x80) { /* we have a skip */
c -= 0x7F;
int start, ret;
int ltab, ctab;
- if ((ret = ff_reget_buffer(avctx, p)) < 0)
+ if ((ret = ff_reget_buffer(avctx, p, 0)) < 0)
return ret;
start = 48; /* hardcoded for now */
s->decode_delta = buf[18];
/* decide whether frame uses deltas or not */
-#ifndef BITSTREAM_READER_LE
- for (i = 0; i < buf_size; i++)
- buf[i] = ff_reverse[buf[i]];
-#endif
if ((ret = init_get_bits8(&s->gb, buf + start, buf_size - start)) < 0)
return ret;
return buf_size;
}
+static av_cold void ir2_init_static(void)
+{
+ INIT_VLC_STATIC_FROM_LENGTHS(&ir2_vlc, CODE_VLC_BITS, IR2_CODES,
+ &ir2_tab[0][1], 2, &ir2_tab[0][0], 2, 1,
+ 0, INIT_VLC_OUTPUT_LE, 1 << CODE_VLC_BITS);
+}
+
static av_cold int ir2_decode_init(AVCodecContext *avctx)
{
+ static AVOnce init_static_once = AV_ONCE_INIT;
Ir2Context * const ic = avctx->priv_data;
- static VLC_TYPE vlc_tables[1 << CODE_VLC_BITS][2];
ic->avctx = avctx;
if (!ic->picture)
return AVERROR(ENOMEM);
- ir2_vlc.table = vlc_tables;
- ir2_vlc.table_allocated = 1 << CODE_VLC_BITS;
-#ifdef BITSTREAM_READER_LE
- init_vlc(&ir2_vlc, CODE_VLC_BITS, IR2_CODES,
- &ir2_codes[0][1], 4, 2,
- &ir2_codes[0][0], 4, 2, INIT_VLC_USE_NEW_STATIC | INIT_VLC_LE);
-#else
- init_vlc(&ir2_vlc, CODE_VLC_BITS, IR2_CODES,
- &ir2_codes[0][1], 4, 2,
- &ir2_codes[0][0], 4, 2, INIT_VLC_USE_NEW_STATIC);
-#endif
+ ff_thread_once(&init_static_once, ir2_init_static);
return 0;
}
.close = ir2_decode_end,
.decode = ir2_decode_frame,
.capabilities = AV_CODEC_CAP_DR1,
+ .caps_internal = FF_CODEC_CAP_INIT_THREADSAFE,
};