Libav
kgv1dec.c
Go to the documentation of this file.
1 /*
2  * Kega Game Video (KGV1) decoder
3  * Copyright (c) 2010 Daniel Verkamp
4  *
5  * This file is part of Libav.
6  *
7  * Libav is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Lesser General Public
9  * License as published by the Free Software Foundation; either
10  * version 2.1 of the License, or (at your option) any later version.
11  *
12  * Libav is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15  * Lesser General Public License for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public
18  * License along with Libav; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20  */
21 
27 #include "libavutil/common.h"
28 #include "libavutil/intreadwrite.h"
29 #include "libavutil/imgutils.h"
30 #include "avcodec.h"
31 #include "internal.h"
32 
33 typedef struct {
35  uint16_t *frame_buffer;
36  uint16_t *last_frame_buffer;
37 } KgvContext;
38 
39 static void decode_flush(AVCodecContext *avctx)
40 {
41  KgvContext * const c = avctx->priv_data;
42 
45 }
46 
47 static int decode_frame(AVCodecContext *avctx, void *data, int *got_frame,
48  AVPacket *avpkt)
49 {
50  AVFrame *frame = data;
51  const uint8_t *buf = avpkt->data;
52  const uint8_t *buf_end = buf + avpkt->size;
53  KgvContext * const c = avctx->priv_data;
54  int offsets[8];
55  uint16_t *out, *prev;
56  int outcnt = 0, maxcnt;
57  int w, h, i, res;
58 
59  if (avpkt->size < 2)
60  return AVERROR_INVALIDDATA;
61 
62  w = (buf[0] + 1) * 8;
63  h = (buf[1] + 1) * 8;
64  buf += 2;
65 
66  if (w != avctx->width || h != avctx->height) {
69  if ((res = ff_set_dimensions(avctx, w, h)) < 0)
70  return res;
71  }
72 
73  if (!c->frame_buffer) {
74  c->frame_buffer = av_mallocz(avctx->width * avctx->height * 2);
75  c->last_frame_buffer = av_mallocz(avctx->width * avctx->height * 2);
76  if (!c->frame_buffer || !c->last_frame_buffer) {
77  decode_flush(avctx);
78  return AVERROR(ENOMEM);
79  }
80  }
81 
82  maxcnt = w * h;
83 
84  if ((res = ff_get_buffer(avctx, frame, 0)) < 0)
85  return res;
86  out = c->frame_buffer;
87  prev = c->last_frame_buffer;
88 
89  for (i = 0; i < 8; i++)
90  offsets[i] = -1;
91 
92  while (outcnt < maxcnt && buf_end - 2 > buf) {
93  int code = AV_RL16(buf);
94  buf += 2;
95 
96  if (!(code & 0x8000)) {
97  out[outcnt++] = code; // rgb555 pixel coded directly
98  } else {
99  int count;
100  int inp_off;
101  uint16_t *inp;
102 
103  if ((code & 0x6000) == 0x6000) {
104  // copy from previous frame
105  int oidx = (code >> 10) & 7;
106  int start;
107 
108  count = (code & 0x3FF) + 3;
109 
110  if (offsets[oidx] < 0) {
111  if (buf_end - 3 < buf)
112  break;
113  offsets[oidx] = AV_RL24(buf);
114  buf += 3;
115  }
116 
117  start = (outcnt + offsets[oidx]) % maxcnt;
118 
119  if (maxcnt - start < count)
120  break;
121 
122  if (!prev) {
123  av_log(avctx, AV_LOG_ERROR,
124  "Frame reference does not exist\n");
125  break;
126  }
127 
128  inp = prev;
129  inp_off = start;
130  } else {
131  // copy from earlier in this frame
132  int offset = (code & 0x1FFF) + 1;
133 
134  if (!(code & 0x6000)) {
135  count = 2;
136  } else if ((code & 0x6000) == 0x2000) {
137  count = 3;
138  } else {
139  if (buf_end - 1 < buf)
140  break;
141  count = 4 + *buf++;
142  }
143 
144  if (outcnt < offset)
145  break;
146 
147  inp = out;
148  inp_off = outcnt - offset;
149  }
150 
151  if (maxcnt - outcnt < count)
152  break;
153 
154  for (i = inp_off; i < count + inp_off; i++) {
155  out[outcnt++] = inp[i];
156  }
157  }
158  }
159 
160  if (outcnt - maxcnt)
161  av_log(avctx, AV_LOG_DEBUG, "frame finished with %d diff\n", outcnt - maxcnt);
162 
163  av_image_copy_plane(frame->data[0], frame->linesize[0],
164  (const uint8_t*)c->frame_buffer, avctx->width * 2,
165  avctx->width * 2, avctx->height);
166  FFSWAP(uint16_t *, c->frame_buffer, c->last_frame_buffer);
167 
168  *got_frame = 1;
169 
170  return avpkt->size;
171 }
172 
174 {
175  KgvContext * const c = avctx->priv_data;
176 
177  c->avctx = avctx;
178  avctx->pix_fmt = AV_PIX_FMT_RGB555;
179 
180  return 0;
181 }
182 
184 {
185  decode_flush(avctx);
186  return 0;
187 }
188 
190  .name = "kgv1",
191  .long_name = NULL_IF_CONFIG_SMALL("Kega Game Video"),
192  .type = AVMEDIA_TYPE_VIDEO,
193  .id = AV_CODEC_ID_KGV1,
194  .priv_data_size = sizeof(KgvContext),
195  .init = decode_init,
196  .close = decode_end,
197  .decode = decode_frame,
198  .flush = decode_flush,
199  .capabilities = CODEC_CAP_DR1,
200 };