libavcodec/kgv1dec.c
bf354122
 /*
  * Kega Game Video (KGV1) decoder
  * Copyright (c) 2010 Daniel Verkamp
  *
  * 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
  */
 
 /**
ba87f080
  * @file
bf354122
  * Kega Game Video decoder
  */
 
1d9c2dc8
 #include "libavutil/common.h"
bf354122
 #include "libavutil/intreadwrite.h"
7ffe76e5
 #include "libavutil/imgutils.h"
bf354122
 #include "avcodec.h"
594d4d5d
 #include "internal.h"
bf354122
 
 typedef struct {
adb199d1
     uint16_t *frame_buffer;
     uint16_t *last_frame_buffer;
bf354122
 } KgvContext;
 
33cd32b3
 static void decode_flush(AVCodecContext *avctx)
 {
     KgvContext * const c = avctx->priv_data;
 
adb199d1
     av_freep(&c->frame_buffer);
     av_freep(&c->last_frame_buffer);
33cd32b3
 }
 
df9b9567
 static int decode_frame(AVCodecContext *avctx, void *data, int *got_frame,
                         AVPacket *avpkt)
bf354122
 {
759001c5
     AVFrame *frame = data;
bf354122
     const uint8_t *buf = avpkt->data;
     const uint8_t *buf_end = buf + avpkt->size;
     KgvContext * const c = avctx->priv_data;
807a045a
     int offsets[8];
96d2e4d6
     uint8_t *out, *prev;
bf354122
     int outcnt = 0, maxcnt;
33cd32b3
     int w, h, i, res;
bf354122
 
     if (avpkt->size < 2)
405486c2
         return AVERROR_INVALIDDATA;
bf354122
 
     w = (buf[0] + 1) * 8;
     h = (buf[1] + 1) * 8;
     buf += 2;
 
6c4c27ad
     if (w != avctx->width || h != avctx->height) {
adb199d1
         av_freep(&c->frame_buffer);
         av_freep(&c->last_frame_buffer);
d62b24a3
         if ((res = ff_set_dimensions(avctx, w, h)) < 0)
             return res;
6c4c27ad
     }
bf354122
 
adb199d1
     if (!c->frame_buffer) {
         c->frame_buffer      = av_mallocz(avctx->width * avctx->height * 2);
         c->last_frame_buffer = av_mallocz(avctx->width * avctx->height * 2);
         if (!c->frame_buffer || !c->last_frame_buffer) {
             decode_flush(avctx);
             return AVERROR(ENOMEM);
         }
     }
 
bf354122
     maxcnt = w * h;
 
adb199d1
     if ((res = ff_get_buffer(avctx, frame, 0)) < 0)
33cd32b3
         return res;
adb199d1
     out  = c->frame_buffer;
     prev = c->last_frame_buffer;
bf354122
 
807a045a
     for (i = 0; i < 8; i++)
bf354122
         offsets[i] = -1;
 
de0aa9e5
     while (outcnt < maxcnt && buf_end - 2 >= buf) {
bf354122
         int code = AV_RL16(buf);
         buf += 2;
 
         if (!(code & 0x8000)) {
96d2e4d6
             AV_WN16A(&out[2 * outcnt], code); // rgb555 pixel coded directly
             outcnt++;
bf354122
         } else {
             int count;
 
             if ((code & 0x6000) == 0x6000) {
                 // copy from previous frame
                 int oidx = (code >> 10) & 7;
                 int start;
 
                 count = (code & 0x3FF) + 3;
 
                 if (offsets[oidx] < 0) {
                     if (buf_end - 3 < buf)
                         break;
                     offsets[oidx] = AV_RL24(buf);
                     buf += 3;
                 }
 
                 start = (outcnt + offsets[oidx]) % maxcnt;
 
940b06ae
                 if (maxcnt - start < count || maxcnt - outcnt < count)
bf354122
                     break;
 
33cd32b3
                 if (!prev) {
                     av_log(avctx, AV_LOG_ERROR,
                            "Frame reference does not exist\n");
                     break;
                 }
 
96d2e4d6
                 memcpy(out + 2 * outcnt, prev + 2 * start, 2 * count);
bf354122
             } else {
                 // copy from earlier in this frame
                 int offset = (code & 0x1FFF) + 1;
 
                 if (!(code & 0x6000)) {
                     count = 2;
                 } else if ((code & 0x6000) == 0x2000) {
                     count = 3;
                 } else {
                     if (buf_end - 1 < buf)
                         break;
                     count = 4 + *buf++;
                 }
 
940b06ae
                 if (outcnt < offset || maxcnt - outcnt < count)
bf354122
                     break;
 
96d2e4d6
                 av_memcpy_backptr(out + 2 * outcnt, 2 * offset, 2 * count);
33cd32b3
             }
940b06ae
             outcnt += count;
bf354122
         }
     }
 
     if (outcnt - maxcnt)
         av_log(avctx, AV_LOG_DEBUG, "frame finished with %d diff\n", outcnt - maxcnt);
 
adb199d1
     av_image_copy_plane(frame->data[0], frame->linesize[0],
                         (const uint8_t*)c->frame_buffer,  avctx->width * 2,
                         avctx->width * 2, avctx->height);
     FFSWAP(uint16_t *, c->frame_buffer, c->last_frame_buffer);
bf354122
 
759001c5
     *got_frame = 1;
bf354122
 
     return avpkt->size;
 }
 
 static av_cold int decode_init(AVCodecContext *avctx)
 {
716d413c
     avctx->pix_fmt = AV_PIX_FMT_RGB555;
bf354122
 
     return 0;
 }
 
d0004a19
 static av_cold int decode_end(AVCodecContext *avctx)
 {
     decode_flush(avctx);
     return 0;
 }
 
e7e2df27
 AVCodec ff_kgv1_decoder = {
ec6402b7
     .name           = "kgv1",
b2bed932
     .long_name      = NULL_IF_CONFIG_SMALL("Kega Game Video"),
ec6402b7
     .type           = AVMEDIA_TYPE_VIDEO,
36ef5369
     .id             = AV_CODEC_ID_KGV1,
ec6402b7
     .priv_data_size = sizeof(KgvContext),
     .init           = decode_init,
d0004a19
     .close          = decode_end,
ec6402b7
     .decode         = decode_frame,
33cd32b3
     .flush          = decode_flush,
f174fbac
     .capabilities   = CODEC_CAP_DR1,
bf354122
 };