FFmpeg
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
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 FFmpeg.
6  *
7  * FFmpeg 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  * FFmpeg 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 FFmpeg; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20  */
21 
22 /**
23  * @file
24  * Kega Game Video decoder
25  */
26 
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  AVFrame prev, cur;
36 } KgvContext;
37 
38 static void decode_flush(AVCodecContext *avctx)
39 {
40  KgvContext * const c = avctx->priv_data;
41 
42  if (c->prev.data[0])
43  avctx->release_buffer(avctx, &c->prev);
44 }
45 
46 static int decode_frame(AVCodecContext *avctx, void *data, int *got_frame,
47  AVPacket *avpkt)
48 {
49  const uint8_t *buf = avpkt->data;
50  const uint8_t *buf_end = buf + avpkt->size;
51  KgvContext * const c = avctx->priv_data;
52  int offsets[8];
53  uint8_t *out, *prev;
54  int outcnt = 0, maxcnt;
55  int w, h, i, res;
56 
57  if (avpkt->size < 2)
58  return AVERROR_INVALIDDATA;
59 
60  w = (buf[0] + 1) * 8;
61  h = (buf[1] + 1) * 8;
62  buf += 2;
63 
64  if ((res = av_image_check_size(w, h, 0, avctx)) < 0)
65  return res;
66 
67  if (w != avctx->width || h != avctx->height) {
68  if (c->prev.data[0])
69  avctx->release_buffer(avctx, &c->prev);
70  avcodec_set_dimensions(avctx, w, h);
71  }
72 
73  maxcnt = w * h;
74 
75  c->cur.reference = 3;
76  if ((res = ff_get_buffer(avctx, &c->cur)) < 0)
77  return res;
78  out = c->cur.data[0];
79  if (c->prev.data[0]) {
80  prev = c->prev.data[0];
81  } else {
82  prev = NULL;
83  }
84 
85  for (i = 0; i < 8; i++)
86  offsets[i] = -1;
87 
88  while (outcnt < maxcnt && buf_end - 2 >= buf) {
89  int code = AV_RL16(buf);
90  buf += 2;
91 
92  if (!(code & 0x8000)) {
93  AV_WN16A(&out[2 * outcnt], code); // rgb555 pixel coded directly
94  outcnt++;
95  } else {
96  int count;
97 
98  if ((code & 0x6000) == 0x6000) {
99  // copy from previous frame
100  int oidx = (code >> 10) & 7;
101  int start;
102 
103  count = (code & 0x3FF) + 3;
104 
105  if (offsets[oidx] < 0) {
106  if (buf_end - 3 < buf)
107  break;
108  offsets[oidx] = AV_RL24(buf);
109  buf += 3;
110  }
111 
112  start = (outcnt + offsets[oidx]) % maxcnt;
113 
114  if (maxcnt - start < count || maxcnt - outcnt < count)
115  break;
116 
117  if (!prev) {
118  av_log(avctx, AV_LOG_ERROR,
119  "Frame reference does not exist\n");
120  break;
121  }
122 
123  memcpy(out + 2 * outcnt, prev + 2 * start, 2 * count);
124  } else {
125  // copy from earlier in this frame
126  int offset = (code & 0x1FFF) + 1;
127 
128  if (!(code & 0x6000)) {
129  count = 2;
130  } else if ((code & 0x6000) == 0x2000) {
131  count = 3;
132  } else {
133  if (buf_end - 1 < buf)
134  break;
135  count = 4 + *buf++;
136  }
137 
138  if (outcnt < offset || maxcnt - outcnt < count)
139  break;
140 
141  av_memcpy_backptr(out + 2 * outcnt, 2 * offset, 2 * count);
142  }
143  outcnt += count;
144  }
145  }
146 
147  if (outcnt - maxcnt)
148  av_log(avctx, AV_LOG_DEBUG, "frame finished with %d diff\n", outcnt - maxcnt);
149 
150  *got_frame = 1;
151  *(AVFrame*)data = c->cur;
152 
153  if (c->prev.data[0])
154  avctx->release_buffer(avctx, &c->prev);
155  FFSWAP(AVFrame, c->cur, c->prev);
156 
157  return avpkt->size;
158 }
159 
161 {
162  KgvContext * const c = avctx->priv_data;
163 
164  c->avctx = avctx;
165  avctx->pix_fmt = AV_PIX_FMT_RGB555;
166  avctx->flags |= CODEC_FLAG_EMU_EDGE;
167 
168  return 0;
169 }
170 
172 {
173  decode_flush(avctx);
174  return 0;
175 }
176 
178  .name = "kgv1",
179  .type = AVMEDIA_TYPE_VIDEO,
180  .id = AV_CODEC_ID_KGV1,
181  .priv_data_size = sizeof(KgvContext),
182  .init = decode_init,
183  .close = decode_end,
184  .decode = decode_frame,
185  .flush = decode_flush,
186  .long_name = NULL_IF_CONFIG_SMALL("Kega Game Video"),
187  .capabilities = CODEC_CAP_DR1,
188 };