• Main Page
  • Related Pages
  • Modules
  • Data Structures
  • Files
  • Examples
  • File List
  • Globals

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 
00031 typedef struct {
00032     AVCodecContext *avctx;
00033     AVFrame prev, cur;
00034 } KgvContext;
00035 
00036 static void decode_flush(AVCodecContext *avctx)
00037 {
00038     KgvContext * const c = avctx->priv_data;
00039 
00040     if (c->prev.data[0])
00041         avctx->release_buffer(avctx, &c->prev);
00042 }
00043 
00044 static int decode_frame(AVCodecContext *avctx, void *data, int *data_size, AVPacket *avpkt)
00045 {
00046     const uint8_t *buf = avpkt->data;
00047     const uint8_t *buf_end = buf + avpkt->size;
00048     KgvContext * const c = avctx->priv_data;
00049     int offsets[8];
00050     uint16_t *out, *prev;
00051     int outcnt = 0, maxcnt;
00052     int w, h, i, res;
00053 
00054     if (avpkt->size < 2)
00055         return -1;
00056 
00057     w = (buf[0] + 1) * 8;
00058     h = (buf[1] + 1) * 8;
00059     buf += 2;
00060 
00061     if (av_image_check_size(w, h, 0, avctx))
00062         return -1;
00063 
00064     if (w != avctx->width || h != avctx->height) {
00065         if (c->prev.data[0])
00066             avctx->release_buffer(avctx, &c->prev);
00067         avcodec_set_dimensions(avctx, w, h);
00068     }
00069 
00070     maxcnt = w * h;
00071 
00072     c->cur.reference = 3;
00073     if ((res = avctx->get_buffer(avctx, &c->cur)) < 0)
00074         return res;
00075     out  = (uint16_t *) c->cur.data[0];
00076     if (c->prev.data[0]) {
00077         prev = (uint16_t *) c->prev.data[0];
00078     } else {
00079         prev = NULL;
00080     }
00081 
00082     for (i = 0; i < 8; i++)
00083         offsets[i] = -1;
00084 
00085     while (outcnt < maxcnt && buf_end - 2 > buf) {
00086         int code = AV_RL16(buf);
00087         buf += 2;
00088 
00089         if (!(code & 0x8000)) {
00090             out[outcnt++] = code; // rgb555 pixel coded directly
00091         } else {
00092             int count;
00093             int inp_off;
00094             uint16_t *inp;
00095 
00096             if ((code & 0x6000) == 0x6000) {
00097                 // copy from previous frame
00098                 int oidx = (code >> 10) & 7;
00099                 int start;
00100 
00101                 count = (code & 0x3FF) + 3;
00102 
00103                 if (offsets[oidx] < 0) {
00104                     if (buf_end - 3 < buf)
00105                         break;
00106                     offsets[oidx] = AV_RL24(buf);
00107                     buf += 3;
00108                 }
00109 
00110                 start = (outcnt + offsets[oidx]) % maxcnt;
00111 
00112                 if (maxcnt - start < count)
00113                     break;
00114 
00115                 if (!prev) {
00116                     av_log(avctx, AV_LOG_ERROR,
00117                            "Frame reference does not exist\n");
00118                     break;
00119                 }
00120 
00121                 inp = prev;
00122                 inp_off = start;
00123             } else {
00124                 // copy from earlier in this frame
00125                 int offset = (code & 0x1FFF) + 1;
00126 
00127                 if (!(code & 0x6000)) {
00128                     count = 2;
00129                 } else if ((code & 0x6000) == 0x2000) {
00130                     count = 3;
00131                 } else {
00132                     if (buf_end - 1 < buf)
00133                         break;
00134                     count = 4 + *buf++;
00135                 }
00136 
00137                 if (outcnt < offset)
00138                     break;
00139 
00140                 inp = out;
00141                 inp_off = outcnt - offset;
00142             }
00143 
00144             if (maxcnt - outcnt < count)
00145                 break;
00146 
00147             for (i = inp_off; i < count + inp_off; i++) {
00148                 out[outcnt++] = inp[i];
00149             }
00150         }
00151     }
00152 
00153     if (outcnt - maxcnt)
00154         av_log(avctx, AV_LOG_DEBUG, "frame finished with %d diff\n", outcnt - maxcnt);
00155 
00156     *data_size = sizeof(AVFrame);
00157     *(AVFrame*)data = c->cur;
00158 
00159     if (c->prev.data[0])
00160         avctx->release_buffer(avctx, &c->prev);
00161     FFSWAP(AVFrame, c->cur, c->prev);
00162 
00163     return avpkt->size;
00164 }
00165 
00166 static av_cold int decode_init(AVCodecContext *avctx)
00167 {
00168     KgvContext * const c = avctx->priv_data;
00169 
00170     c->avctx = avctx;
00171     avctx->pix_fmt = PIX_FMT_RGB555;
00172     avctx->flags  |= CODEC_FLAG_EMU_EDGE;
00173 
00174     return 0;
00175 }
00176 
00177 static av_cold int decode_end(AVCodecContext *avctx)
00178 {
00179     decode_flush(avctx);
00180     return 0;
00181 }
00182 
00183 AVCodec ff_kgv1_decoder = {
00184     .name           = "kgv1",
00185     .type           = AVMEDIA_TYPE_VIDEO,
00186     .id             = CODEC_ID_KGV1,
00187     .priv_data_size = sizeof(KgvContext),
00188     .init           = decode_init,
00189     .close          = decode_end,
00190     .decode         = decode_frame,
00191     .flush          = decode_flush,
00192     .long_name = NULL_IF_CONFIG_SMALL("Kega Game Video"),
00193 };
Generated on Thu Jan 24 2013 17:08:52 for Libav by doxygen 1.7.1