summaryrefslogtreecommitdiff
path: root/libavcodec/jvdec.c
diff options
context:
space:
mode:
Diffstat (limited to 'libavcodec/jvdec.c')
-rw-r--r--libavcodec/jvdec.c95
1 files changed, 49 insertions, 46 deletions
diff --git a/libavcodec/jvdec.c b/libavcodec/jvdec.c
index 3a92b584c3..cbe83d3c10 100644
--- a/libavcodec/jvdec.c
+++ b/libavcodec/jvdec.c
@@ -2,20 +2,20 @@
* Bitmap Brothers JV video decoder
* Copyright (c) 2011 Peter Ross <pross@xvid.org>
*
- * This file is part of Libav.
+ * This file is part of FFmpeg.
*
- * Libav is free software; you can redistribute it and/or
+ * FFmpeg 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.
*
- * Libav is distributed in the hope that it will be useful,
+ * FFmpeg 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 Libav; if not, write to the Free Software
+ * License along with FFmpeg; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
@@ -28,8 +28,8 @@
#include "libavutil/intreadwrite.h"
#include "avcodec.h"
-#include "bitstream.h"
#include "blockdsp.h"
+#include "get_bits.h"
#include "internal.h"
typedef struct JvContext {
@@ -55,91 +55,91 @@ static av_cold int decode_init(AVCodecContext *avctx)
return AVERROR(ENOMEM);
avctx->pix_fmt = AV_PIX_FMT_PAL8;
- ff_blockdsp_init(&s->bdsp);
+ ff_blockdsp_init(&s->bdsp, avctx);
return 0;
}
/**
* Decode 2x2 block
*/
-static inline void decode2x2(BitstreamContext *bc, uint8_t *dst, int linesize)
+static inline void decode2x2(GetBitContext *gb, uint8_t *dst, int linesize)
{
int i, j, v[2];
- switch (bitstream_read(bc, 2)) {
+ switch (get_bits(gb, 2)) {
case 1:
- v[0] = bitstream_read(bc, 8);
+ v[0] = get_bits(gb, 8);
for (j = 0; j < 2; j++)
memset(dst + j * linesize, v[0], 2);
break;
case 2:
- v[0] = bitstream_read(bc, 8);
- v[1] = bitstream_read(bc, 8);
+ v[0] = get_bits(gb, 8);
+ v[1] = get_bits(gb, 8);
for (j = 0; j < 2; j++)
for (i = 0; i < 2; i++)
- dst[j * linesize + i] = v[bitstream_read_bit(bc)];
+ dst[j * linesize + i] = v[get_bits1(gb)];
break;
case 3:
for (j = 0; j < 2; j++)
for (i = 0; i < 2; i++)
- dst[j * linesize + i] = bitstream_read(bc, 8);
+ dst[j * linesize + i] = get_bits(gb, 8);
}
}
/**
* Decode 4x4 block
*/
-static inline void decode4x4(BitstreamContext *bc, uint8_t *dst, int linesize)
+static inline void decode4x4(GetBitContext *gb, uint8_t *dst, int linesize)
{
int i, j, v[2];
- switch (bitstream_read(bc, 2)) {
+ switch (get_bits(gb, 2)) {
case 1:
- v[0] = bitstream_read(bc, 8);
+ v[0] = get_bits(gb, 8);
for (j = 0; j < 4; j++)
memset(dst + j * linesize, v[0], 4);
break;
case 2:
- v[0] = bitstream_read(bc, 8);
- v[1] = bitstream_read(bc, 8);
+ v[0] = get_bits(gb, 8);
+ v[1] = get_bits(gb, 8);
for (j = 2; j >= 0; j -= 2) {
for (i = 0; i < 4; i++)
- dst[j * linesize + i] = v[bitstream_read_bit(bc)];
+ dst[j * linesize + i] = v[get_bits1(gb)];
for (i = 0; i < 4; i++)
- dst[(j + 1) * linesize + i] = v[bitstream_read_bit(bc)];
+ dst[(j + 1) * linesize + i] = v[get_bits1(gb)];
}
break;
case 3:
for (j = 0; j < 4; j += 2)
for (i = 0; i < 4; i += 2)
- decode2x2(bc, dst + j * linesize + i, linesize);
+ decode2x2(gb, dst + j * linesize + i, linesize);
}
}
/**
* Decode 8x8 block
*/
-static inline void decode8x8(BitstreamContext *bc, uint8_t *dst, int linesize,
+static inline void decode8x8(GetBitContext *gb, uint8_t *dst, int linesize,
BlockDSPContext *bdsp)
{
int i, j, v[2];
- switch (bitstream_read(bc, 2)) {
+ switch (get_bits(gb, 2)) {
case 1:
- v[0] = bitstream_read(bc, 8);
+ v[0] = get_bits(gb, 8);
bdsp->fill_block_tab[1](dst, v[0], linesize, 8);
break;
case 2:
- v[0] = bitstream_read(bc, 8);
- v[1] = bitstream_read(bc, 8);
+ v[0] = get_bits(gb, 8);
+ v[1] = get_bits(gb, 8);
for (j = 7; j >= 0; j--)
for (i = 0; i < 8; i++)
- dst[j * linesize + i] = v[bitstream_read_bit(bc)];
+ dst[j * linesize + i] = v[get_bits1(gb)];
break;
case 3:
for (j = 0; j < 8; j += 4)
for (i = 0; i < 8; i += 4)
- decode4x4(bc, dst + j * linesize + i, linesize);
+ decode4x4(gb, dst + j * linesize + i, linesize);
}
}
@@ -147,39 +147,41 @@ static int decode_frame(AVCodecContext *avctx, void *data, int *got_frame,
AVPacket *avpkt)
{
JvContext *s = avctx->priv_data;
- int buf_size = avpkt->size;
const uint8_t *buf = avpkt->data;
- const uint8_t *buf_end = buf + buf_size;
+ const uint8_t *buf_end = buf + avpkt->size;
int video_size, video_type, i, j, ret;
+ if (avpkt->size < 6)
+ return AVERROR_INVALIDDATA;
+
video_size = AV_RL32(buf);
video_type = buf[4];
buf += 5;
if (video_size) {
- if ((ret = ff_reget_buffer(avctx, s->frame)) < 0) {
- av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
- return ret;
+ if (video_size < 0 || video_size > avpkt->size - 5) {
+ av_log(avctx, AV_LOG_ERROR, "video size %d invalid\n", video_size);
+ return AVERROR_INVALIDDATA;
}
+ if ((ret = ff_reget_buffer(avctx, s->frame)) < 0)
+ return ret;
if (video_type == 0 || video_type == 1) {
- BitstreamContext bc;
- bitstream_init8(&bc, buf, FFMIN(video_size, buf_end - buf));
+ GetBitContext gb;
+ init_get_bits(&gb, buf, 8 * video_size);
for (j = 0; j < avctx->height; j += 8)
for (i = 0; i < avctx->width; i += 8)
- decode8x8(&bc,
+ decode8x8(&gb,
s->frame->data[0] + j * s->frame->linesize[0] + i,
s->frame->linesize[0], &s->bdsp);
buf += video_size;
} else if (video_type == 2) {
- if (buf + 1 <= buf_end) {
- int v = *buf++;
- for (j = 0; j < avctx->height; j++)
- memset(s->frame->data[0] + j * s->frame->linesize[0],
- v, avctx->width);
- }
+ int v = *buf++;
+ for (j = 0; j < avctx->height; j++)
+ memset(s->frame->data[0] + j * s->frame->linesize[0],
+ v, avctx->width);
} else {
av_log(avctx, AV_LOG_WARNING,
"unsupported frame type %i\n", video_type);
@@ -187,9 +189,10 @@ static int decode_frame(AVCodecContext *avctx, void *data, int *got_frame,
}
}
- if (buf < buf_end) {
- for (i = 0; i < AVPALETTE_COUNT && buf + 3 <= buf_end; i++) {
- s->palette[i] = AV_RB24(buf) << 2;
+ if (buf_end - buf >= AVPALETTE_COUNT * 3) {
+ for (i = 0; i < AVPALETTE_COUNT; i++) {
+ uint32_t pal = AV_RB24(buf);
+ s->palette[i] = 0xFFU << 24 | pal << 2 | ((pal >> 4) & 0x30303);
buf += 3;
}
s->palette_has_changed = 1;
@@ -207,7 +210,7 @@ static int decode_frame(AVCodecContext *avctx, void *data, int *got_frame,
*got_frame = 1;
}
- return buf_size;
+ return avpkt->size;
}
static av_cold int decode_close(AVCodecContext *avctx)