libavcodec/jvdec.c
2f46a0af
 /*
  * Bitmap Brothers JV video decoder
  * Copyright (c) 2011 Peter Ross <pross@xvid.org>
  *
  * This file is part of FFmpeg.
  *
  * 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.
  *
  * 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 FFmpeg; if not, write to the Free Software
  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  */
 
 /**
  * @file
  * Bitmap Brothers JV video decoder
  * @author Peter Ross <pross@xvid.org>
  */
 
d509ae5b
 #include "libavutil/intreadwrite.h"
 
2f46a0af
 #include "avcodec.h"
e74433a8
 #include "blockdsp.h"
2f46a0af
 #include "get_bits.h"
759001c5
 #include "internal.h"
2f46a0af
 
 typedef struct JvContext {
e74433a8
     BlockDSPContext bdsp;
678431d3
     AVFrame   *frame;
2f46a0af
     uint32_t   palette[AVPALETTE_COUNT];
     int        palette_has_changed;
 } JvContext;
 
 static av_cold int decode_init(AVCodecContext *avctx)
 {
     JvContext *s = avctx->priv_data;
b7462a39
 
88626e5a
     if (!avctx->width || !avctx->height ||
         (avctx->width & 7) || (avctx->height & 7)) {
         av_log(avctx, AV_LOG_ERROR, "Invalid video dimensions: %dx%d\n",
                avctx->width, avctx->height);
         return AVERROR(EINVAL);
     }
 
678431d3
     s->frame = av_frame_alloc();
     if (!s->frame)
         return AVERROR(ENOMEM);
b7462a39
 
716d413c
     avctx->pix_fmt = AV_PIX_FMT_PAL8;
e74433a8
     ff_blockdsp_init(&s->bdsp, avctx);
2f46a0af
     return 0;
 }
 
 /**
  * Decode 2x2 block
  */
 static inline void decode2x2(GetBitContext *gb, uint8_t *dst, int linesize)
 {
     int i, j, v[2];
 
     switch (get_bits(gb, 2)) {
     case 1:
         v[0] = get_bits(gb, 8);
         for (j = 0; j < 2; j++)
d509ae5b
             memset(dst + j * linesize, v[0], 2);
2f46a0af
         break;
     case 2:
         v[0] = get_bits(gb, 8);
         v[1] = get_bits(gb, 8);
         for (j = 0; j < 2; j++)
             for (i = 0; i < 2; i++)
d509ae5b
                 dst[j * linesize + i] = v[get_bits1(gb)];
2f46a0af
         break;
     case 3:
         for (j = 0; j < 2; j++)
             for (i = 0; i < 2; i++)
d509ae5b
                 dst[j * linesize + i] = get_bits(gb, 8);
2f46a0af
     }
 }
 
 /**
  * Decode 4x4 block
  */
 static inline void decode4x4(GetBitContext *gb, uint8_t *dst, int linesize)
 {
     int i, j, v[2];
 
     switch (get_bits(gb, 2)) {
     case 1:
         v[0] = get_bits(gb, 8);
         for (j = 0; j < 4; j++)
d509ae5b
             memset(dst + j * linesize, v[0], 4);
2f46a0af
         break;
     case 2:
         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++)
d509ae5b
                 dst[j * linesize + i] = v[get_bits1(gb)];
2f46a0af
             for (i = 0; i < 4; i++)
d509ae5b
                 dst[(j + 1) * linesize + i] = v[get_bits1(gb)];
2f46a0af
         }
         break;
     case 3:
         for (j = 0; j < 4; j += 2)
             for (i = 0; i < 4; i += 2)
d509ae5b
                 decode2x2(gb, dst + j * linesize + i, linesize);
2f46a0af
     }
 }
 
 /**
  * Decode 8x8 block
  */
d509ae5b
 static inline void decode8x8(GetBitContext *gb, uint8_t *dst, int linesize,
e74433a8
                              BlockDSPContext *bdsp)
2f46a0af
 {
     int i, j, v[2];
 
     switch (get_bits(gb, 2)) {
     case 1:
         v[0] = get_bits(gb, 8);
e74433a8
         bdsp->fill_block_tab[1](dst, v[0], linesize, 8);
2f46a0af
         break;
     case 2:
         v[0] = get_bits(gb, 8);
         v[1] = get_bits(gb, 8);
         for (j = 7; j >= 0; j--)
d509ae5b
             for (i = 0; i < 8; i++)
                 dst[j * linesize + i] = v[get_bits1(gb)];
2f46a0af
         break;
     case 3:
         for (j = 0; j < 8; j += 4)
             for (i = 0; i < 8; i += 4)
d509ae5b
                 decode4x4(gb, dst + j * linesize + i, linesize);
2f46a0af
     }
 }
 
d509ae5b
 static int decode_frame(AVCodecContext *avctx, void *data, int *got_frame,
2f46a0af
                         AVPacket *avpkt)
 {
d509ae5b
     JvContext *s = avctx->priv_data;
     const uint8_t *buf = avpkt->data;
1bc9e4c5
     const uint8_t *buf_end = buf + avpkt->size;
759001c5
     int video_size, video_type, i, j, ret;
2f46a0af
 
5d171b1f
     if (avpkt->size < 6)
         return AVERROR_INVALIDDATA;
 
2f46a0af
     video_size = AV_RL32(buf);
     video_type = buf[4];
     buf += 5;
 
     if (video_size) {
f8a81755
         if (video_size < 0 || video_size > avpkt->size - 5) {
b4904e80
             av_log(avctx, AV_LOG_ERROR, "video size %d invalid\n", video_size);
             return AVERROR_INVALIDDATA;
         }
2f46a0af
 
         if (video_type == 0 || video_type == 1) {
             GetBitContext gb;
f8a81755
             init_get_bits(&gb, buf, 8 * video_size);
2f46a0af
 
cc5257aa
             if ((ret = ff_reget_buffer(avctx, s->frame)) < 0)
                 return ret;
 
3891dbf4
             if (avctx->height/8 * (avctx->width/8) > 4 * video_size) {
                 av_log(avctx, AV_LOG_ERROR, "Insufficient input data for dimensions\n");
                 return AVERROR_INVALIDDATA;
             }
 
2f46a0af
             for (j = 0; j < avctx->height; j += 8)
                 for (i = 0; i < avctx->width; i += 8)
d509ae5b
                     decode8x8(&gb,
                               s->frame->data[0] + j * s->frame->linesize[0] + i,
e74433a8
                               s->frame->linesize[0], &s->bdsp);
2f46a0af
 
             buf += video_size;
         } else if (video_type == 2) {
5d171b1f
             int v = *buf++;
cc5257aa
 
             av_frame_unref(s->frame);
             if ((ret = ff_get_buffer(avctx, s->frame, AV_GET_BUFFER_FLAG_REF)) < 0)
                 return ret;
 
5d171b1f
             for (j = 0; j < avctx->height; j++)
dcbc748a
                 memset(s->frame->data[0] + j * s->frame->linesize[0],
                        v, avctx->width);
2f46a0af
         } else {
d509ae5b
             av_log(avctx, AV_LOG_WARNING,
                    "unsupported frame type %i\n", video_type);
2f46a0af
             return AVERROR_INVALIDDATA;
         }
     }
 
1c638cfc
     if (buf_end - buf >= AVPALETTE_COUNT * 3) {
         for (i = 0; i < AVPALETTE_COUNT; i++) {
3230c756
             uint32_t pal = AV_RB24(buf);
b12d92ef
             s->palette[i] = 0xFFU << 24 | pal << 2 | ((pal >> 4) & 0x30303);
2f46a0af
             buf += 3;
         }
         s->palette_has_changed = 1;
     }
 
     if (video_size) {
678431d3
         s->frame->key_frame           = 1;
         s->frame->pict_type           = AV_PICTURE_TYPE_I;
         s->frame->palette_has_changed = s->palette_has_changed;
d509ae5b
         s->palette_has_changed        = 0;
678431d3
         memcpy(s->frame->data[1], s->palette, AVPALETTE_SIZE);
2f46a0af
 
678431d3
         if ((ret = av_frame_ref(data, s->frame)) < 0)
759001c5
             return ret;
df9b9567
         *got_frame = 1;
2f46a0af
     }
 
1bc9e4c5
     return avpkt->size;
2f46a0af
 }
 
 static av_cold int decode_close(AVCodecContext *avctx)
 {
     JvContext *s = avctx->priv_data;
 
678431d3
     av_frame_free(&s->frame);
2f46a0af
 
     return 0;
 }
 
 AVCodec ff_jv_decoder = {
     .name           = "jv",
     .long_name      = NULL_IF_CONFIG_SMALL("Bitmap Brothers JV video"),
b4b167ec
     .type           = AVMEDIA_TYPE_VIDEO,
36ef5369
     .id             = AV_CODEC_ID_JV,
2f46a0af
     .priv_data_size = sizeof(JvContext),
     .init           = decode_init,
     .close          = decode_close,
     .decode         = decode_frame,
def97856
     .capabilities   = AV_CODEC_CAP_DR1,
2f46a0af
 };