libavcodec/kgv1dec.c
Go to the documentation of this file.
00001 /*
00002  * Kega Game Video (KGV1) decoder
00003  * Copyright (c) 2010 Daniel Verkamp
00004  *
00005  * This file is part of Libav.
00006  *
00007  * Libav is free software; you can redistribute it and/or
00008  * modify it under the terms of the GNU Lesser General Public
00009  * License as published by the Free Software Foundation; either
00010  * version 2.1 of the License, or (at your option) any later version.
00011  *
00012  * Libav is distributed in the hope that it will be useful,
00013  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00014  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00015  * Lesser General Public License for more details.
00016  *
00017  * You should have received a copy of the GNU Lesser General Public
00018  * License along with Libav; if not, write to the Free Software
00019  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
00020  */
00021 
00027 #include "libavutil/intreadwrite.h"
00028 #include "libavutil/imgutils.h"
00029 #include "avcodec.h"
00030 #include "internal.h"
00031 
00032 typedef struct {
00033     AVCodecContext *avctx;
00034     AVFrame prev, cur;
00035 } KgvContext;
00036 
00037 static void decode_flush(AVCodecContext *avctx)
00038 {
00039     KgvContext * const c = avctx->priv_data;
00040 
00041     if (c->prev.data[0])
00042         avctx->release_buffer(avctx, &c->prev);
00043 }
00044 
00045 static int decode_frame(AVCodecContext *avctx, void *data, int *data_size, AVPacket *avpkt)
00046 {
00047     const uint8_t *buf = avpkt->data;
00048     const uint8_t *buf_end = buf + avpkt->size;
00049     KgvContext * const c = avctx->priv_data;
00050     int offsets[8];
00051     uint16_t *out, *prev;
00052     int outcnt = 0, maxcnt;
00053     int w, h, i, res;
00054 
00055     if (avpkt->size < 2)
00056         return -1;
00057 
00058     w = (buf[0] + 1) * 8;
00059     h = (buf[1] + 1) * 8;
00060     buf += 2;
00061 
00062     if (av_image_check_size(w, h, 0, avctx))
00063         return -1;
00064 
00065     if (w != avctx->width || h != avctx->height) {
00066         if (c->prev.data[0])
00067             avctx->release_buffer(avctx, &c->prev);
00068         avcodec_set_dimensions(avctx, w, h);
00069     }
00070 
00071     maxcnt = w * h;
00072 
00073     c->cur.reference = 3;
00074     if ((res = ff_get_buffer(avctx, &c->cur)) < 0)
00075         return res;
00076     out  = (uint16_t *) c->cur.data[0];
00077     if (c->prev.data[0]) {
00078         prev = (uint16_t *) c->prev.data[0];
00079     } else {
00080         prev = NULL;
00081     }
00082 
00083     for (i = 0; i < 8; i++)
00084         offsets[i] = -1;
00085 
00086     while (outcnt < maxcnt && buf_end - 2 > buf) {
00087         int code = AV_RL16(buf);
00088         buf += 2;
00089 
00090         if (!(code & 0x8000)) {
00091             out[outcnt++] = code; // rgb555 pixel coded directly
00092         } else {
00093             int count;
00094             int inp_off;
00095             uint16_t *inp;
00096 
00097             if ((code & 0x6000) == 0x6000) {
00098                 // copy from previous frame
00099                 int oidx = (code >> 10) & 7;
00100                 int start;
00101 
00102                 count = (code & 0x3FF) + 3;
00103 
00104                 if (offsets[oidx] < 0) {
00105                     if (buf_end - 3 < buf)
00106                         break;
00107                     offsets[oidx] = AV_RL24(buf);
00108                     buf += 3;
00109                 }
00110 
00111                 start = (outcnt + offsets[oidx]) % maxcnt;
00112 
00113                 if (maxcnt - start < count)
00114                     break;
00115 
00116                 if (!prev) {
00117                     av_log(avctx, AV_LOG_ERROR,
00118                            "Frame reference does not exist\n");
00119                     break;
00120                 }
00121 
00122                 inp = prev;
00123                 inp_off = start;
00124             } else {
00125                 // copy from earlier in this frame
00126                 int offset = (code & 0x1FFF) + 1;
00127 
00128                 if (!(code & 0x6000)) {
00129                     count = 2;
00130                 } else if ((code & 0x6000) == 0x2000) {
00131                     count = 3;
00132                 } else {
00133                     if (buf_end - 1 < buf)
00134                         break;
00135                     count = 4 + *buf++;
00136                 }
00137 
00138                 if (outcnt < offset)
00139                     break;
00140 
00141                 inp = out;
00142                 inp_off = outcnt - offset;
00143             }
00144 
00145             if (maxcnt - outcnt < count)
00146                 break;
00147 
00148             for (i = inp_off; i < count + inp_off; i++) {
00149                 out[outcnt++] = inp[i];
00150             }
00151         }
00152     }
00153 
00154     if (outcnt - maxcnt)
00155         av_log(avctx, AV_LOG_DEBUG, "frame finished with %d diff\n", outcnt - maxcnt);
00156 
00157     *data_size = sizeof(AVFrame);
00158     *(AVFrame*)data = c->cur;
00159 
00160     if (c->prev.data[0])
00161         avctx->release_buffer(avctx, &c->prev);
00162     FFSWAP(AVFrame, c->cur, c->prev);
00163 
00164     return avpkt->size;
00165 }
00166 
00167 static av_cold int decode_init(AVCodecContext *avctx)
00168 {
00169     KgvContext * const c = avctx->priv_data;
00170 
00171     c->avctx = avctx;
00172     avctx->pix_fmt = PIX_FMT_RGB555;
00173     avctx->flags  |= CODEC_FLAG_EMU_EDGE;
00174 
00175     return 0;
00176 }
00177 
00178 static av_cold int decode_end(AVCodecContext *avctx)
00179 {
00180     decode_flush(avctx);
00181     return 0;
00182 }
00183 
00184 AVCodec ff_kgv1_decoder = {
00185     .name           = "kgv1",
00186     .type           = AVMEDIA_TYPE_VIDEO,
00187     .id             = CODEC_ID_KGV1,
00188     .priv_data_size = sizeof(KgvContext),
00189     .init           = decode_init,
00190     .close          = decode_end,
00191     .decode         = decode_frame,
00192     .flush          = decode_flush,
00193     .long_name = NULL_IF_CONFIG_SMALL("Kega Game Video"),
00194 };