00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00027 #include "libavutil/intreadwrite.h"
00028 #include "avcodec.h"
00029
00030 typedef struct {
00031 AVCodecContext *avctx;
00032 AVFrame pic;
00033 uint16_t *prev, *cur;
00034 } KgvContext;
00035
00036 static int decode_frame(AVCodecContext *avctx, void *data, int *data_size, AVPacket *avpkt)
00037 {
00038 const uint8_t *buf = avpkt->data;
00039 const uint8_t *buf_end = buf + avpkt->size;
00040 KgvContext * const c = avctx->priv_data;
00041 int offsets[8];
00042 uint16_t *out, *prev;
00043 int outcnt = 0, maxcnt;
00044 int w, h, i;
00045
00046 if (avpkt->size < 2)
00047 return -1;
00048
00049 w = (buf[0] + 1) * 8;
00050 h = (buf[1] + 1) * 8;
00051 buf += 2;
00052
00053 if (avcodec_check_dimensions(avctx, w, h))
00054 return -1;
00055
00056 if (w != avctx->width || h != avctx->height)
00057 avcodec_set_dimensions(avctx, w, h);
00058
00059 maxcnt = w * h;
00060
00061 out = av_realloc(c->cur, w * h * 2);
00062 if (!out)
00063 return -1;
00064 c->cur = out;
00065
00066 prev = av_realloc(c->prev, w * h * 2);
00067 if (!prev)
00068 return -1;
00069 c->prev = prev;
00070
00071 for (i = 0; i < 8; i++)
00072 offsets[i] = -1;
00073
00074 while (outcnt < maxcnt && buf_end - 2 > buf) {
00075 int code = AV_RL16(buf);
00076 buf += 2;
00077
00078 if (!(code & 0x8000)) {
00079 out[outcnt++] = code;
00080 } else {
00081 int count;
00082 uint16_t *inp;
00083
00084 if ((code & 0x6000) == 0x6000) {
00085
00086 int oidx = (code >> 10) & 7;
00087 int start;
00088
00089 count = (code & 0x3FF) + 3;
00090
00091 if (offsets[oidx] < 0) {
00092 if (buf_end - 3 < buf)
00093 break;
00094 offsets[oidx] = AV_RL24(buf);
00095 buf += 3;
00096 }
00097
00098 start = (outcnt + offsets[oidx]) % maxcnt;
00099
00100 if (maxcnt - start < count)
00101 break;
00102
00103 inp = prev + start;
00104 } else {
00105
00106 int offset = (code & 0x1FFF) + 1;
00107
00108 if (!(code & 0x6000)) {
00109 count = 2;
00110 } else if ((code & 0x6000) == 0x2000) {
00111 count = 3;
00112 } else {
00113 if (buf_end - 1 < buf)
00114 break;
00115 count = 4 + *buf++;
00116 }
00117
00118 if (outcnt < offset)
00119 break;
00120
00121 inp = out + outcnt - offset;
00122 }
00123
00124 if (maxcnt - outcnt < count)
00125 break;
00126
00127 for (i = 0; i < count; i++)
00128 out[outcnt++] = inp[i];
00129 }
00130 }
00131
00132 if (outcnt - maxcnt)
00133 av_log(avctx, AV_LOG_DEBUG, "frame finished with %d diff\n", outcnt - maxcnt);
00134
00135 c->pic.data[0] = (uint8_t *)c->cur;
00136 c->pic.linesize[0] = w * 2;
00137
00138 *data_size = sizeof(AVFrame);
00139 *(AVFrame*)data = c->pic;
00140
00141 FFSWAP(uint16_t *, c->cur, c->prev);
00142
00143 return avpkt->size;
00144 }
00145
00146 static av_cold int decode_init(AVCodecContext *avctx)
00147 {
00148 KgvContext * const c = avctx->priv_data;
00149
00150 c->avctx = avctx;
00151 avctx->pix_fmt = PIX_FMT_RGB555;
00152
00153 return 0;
00154 }
00155
00156 static av_cold int decode_end(AVCodecContext *avctx)
00157 {
00158 KgvContext * const c = avctx->priv_data;
00159
00160 av_freep(&c->cur);
00161 av_freep(&c->prev);
00162
00163 return 0;
00164 }
00165
00166 AVCodec kgv1_decoder = {
00167 "kgv1",
00168 AVMEDIA_TYPE_VIDEO,
00169 CODEC_ID_KGV1,
00170 sizeof(KgvContext),
00171 decode_init,
00172 NULL,
00173 decode_end,
00174 decode_frame,
00175 .long_name = NULL_IF_CONFIG_SMALL("Kega Game Video"),
00176 };