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/intreadwrite.h"
28 #include "libavutil/imgutils.h"
29 #include "avcodec.h"
30 #include "internal.h"
31 
32 typedef struct {
34  AVFrame prev, cur;
35 } KgvContext;
36 
37 static void decode_flush(AVCodecContext *avctx)
38 {
39  KgvContext * const c = avctx->priv_data;
40 
41  if (c->prev.data[0])
42  avctx->release_buffer(avctx, &c->prev);
43 }
44 
45 static int decode_frame(AVCodecContext *avctx, void *data, int *data_size, AVPacket *avpkt)
46 {
47  const uint8_t *buf = avpkt->data;
48  const uint8_t *buf_end = buf + avpkt->size;
49  KgvContext * const c = avctx->priv_data;
50  int offsets[8];
51  uint16_t *out, *prev;
52  int outcnt = 0, maxcnt;
53  int w, h, i, res;
54 
55  if (avpkt->size < 2)
56  return -1;
57 
58  w = (buf[0] + 1) * 8;
59  h = (buf[1] + 1) * 8;
60  buf += 2;
61 
62  if (av_image_check_size(w, h, 0, avctx))
63  return -1;
64 
65  if (w != avctx->width || h != avctx->height) {
66  if (c->prev.data[0])
67  avctx->release_buffer(avctx, &c->prev);
68  avcodec_set_dimensions(avctx, w, h);
69  }
70 
71  maxcnt = w * h;
72 
73  c->cur.reference = 3;
74  if ((res = ff_get_buffer(avctx, &c->cur)) < 0)
75  return res;
76  out = (uint16_t *) c->cur.data[0];
77  if (c->prev.data[0]) {
78  prev = (uint16_t *) c->prev.data[0];
79  } else {
80  prev = NULL;
81  }
82 
83  for (i = 0; i < 8; i++)
84  offsets[i] = -1;
85 
86  while (outcnt < maxcnt && buf_end - 2 > buf) {
87  int code = AV_RL16(buf);
88  buf += 2;
89 
90  if (!(code & 0x8000)) {
91  out[outcnt++] = code; // rgb555 pixel coded directly
92  } else {
93  int count;
94  int inp_off;
95  uint16_t *inp;
96 
97  if ((code & 0x6000) == 0x6000) {
98  // copy from previous frame
99  int oidx = (code >> 10) & 7;
100  int start;
101 
102  count = (code & 0x3FF) + 3;
103 
104  if (offsets[oidx] < 0) {
105  if (buf_end - 3 < buf)
106  break;
107  offsets[oidx] = AV_RL24(buf);
108  buf += 3;
109  }
110 
111  start = (outcnt + offsets[oidx]) % maxcnt;
112 
113  if (maxcnt - start < count)
114  break;
115 
116  if (!prev) {
117  av_log(avctx, AV_LOG_ERROR,
118  "Frame reference does not exist\n");
119  break;
120  }
121 
122  inp = prev;
123  inp_off = start;
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)
139  break;
140 
141  inp = out;
142  inp_off = outcnt - offset;
143  }
144 
145  if (maxcnt - outcnt < count)
146  break;
147 
148  for (i = inp_off; i < count + inp_off; i++) {
149  out[outcnt++] = inp[i];
150  }
151  }
152  }
153 
154  if (outcnt - maxcnt)
155  av_log(avctx, AV_LOG_DEBUG, "frame finished with %d diff\n", outcnt - maxcnt);
156 
157  *data_size = sizeof(AVFrame);
158  *(AVFrame*)data = c->cur;
159 
160  if (c->prev.data[0])
161  avctx->release_buffer(avctx, &c->prev);
162  FFSWAP(AVFrame, c->cur, c->prev);
163 
164  return avpkt->size;
165 }
166 
168 {
169  KgvContext * const c = avctx->priv_data;
170 
171  c->avctx = avctx;
172  avctx->pix_fmt = PIX_FMT_RGB555;
173  avctx->flags |= CODEC_FLAG_EMU_EDGE;
174 
175  return 0;
176 }
177 
179 {
180  decode_flush(avctx);
181  return 0;
182 }
183 
185  .name = "kgv1",
186  .type = AVMEDIA_TYPE_VIDEO,
187  .id = CODEC_ID_KGV1,
188  .priv_data_size = sizeof(KgvContext),
189  .init = decode_init,
190  .close = decode_end,
191  .decode = decode_frame,
192  .flush = decode_flush,
193  .long_name = NULL_IF_CONFIG_SMALL("Kega Game Video"),
194 };
enum PixelFormat pix_fmt
Pixel format, see PIX_FMT_xxx.
Definition: avcodec.h:1426
Audio Video Frame.
Definition: avcodec.h:985
void(* release_buffer)(struct AVCodecContext *c, AVFrame *pic)
Called to release buffers which were allocated with get_buffer.
Definition: avcodec.h:1737
misc image utilities
int size
Definition: avcodec.h:909
static av_cold int decode_end(AVCodecContext *avctx)
Definition: kgv1dec.c:178
AVFrame prev
Definition: kgv1dec.c:34
AVCodec.
Definition: avcodec.h:3189
static int decode_frame(AVCodecContext *avctx, void *data, int *data_size, AVPacket *avpkt)
Definition: kgv1dec.c:45
static int decode(MimicContext *ctx, int quality, int num_coeffs, int is_iframe)
Definition: mimic.c:228
#define av_cold
Definition: attributes.h:71
AV_WL32 AV_RL24
Definition: bytestream.h:89
#define PIX_FMT_RGB555
Definition: pixfmt.h:177
const char data[16]
Definition: mxf.c:60
uint8_t * data
Definition: avcodec.h:908
void avcodec_set_dimensions(AVCodecContext *s, int width, int height)
Definition: utils.c:133
static int init(AVCodecParserContext *s)
Definition: h264_parser.c:336
int reference
is this picture used as reference The values for this are the same as the MpegEncContext.picture_structure variable, that is 1->top field, 2->bottom field, 3->frame/both fields.
Definition: avcodec.h:1074
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
Definition: internal.h:191
int flags
CODEC_FLAG_*.
Definition: avcodec.h:1355
void av_log(void *avcl, int level, const char *fmt,...)
Definition: log.c:140
const char * name
Name of the codec implementation.
Definition: avcodec.h:3196
AVFrame cur
Definition: kgv1dec.c:34
#define CODEC_FLAG_EMU_EDGE
Don't draw edges.
Definition: avcodec.h:641
int av_image_check_size(unsigned int w, unsigned int h, int log_offset, void *log_ctx)
Check if the given dimension of an image is valid, meaning that all bytes of the image can be address...
Definition: imgutils.c:213
AVCodec ff_kgv1_decoder
Definition: kgv1dec.c:184
int width
picture width / height.
Definition: avcodec.h:1408
struct AVFrame AVFrame
Audio Video Frame.
int ff_get_buffer(AVCodecContext *avctx, AVFrame *frame)
Definition: utils.c:1867
NULL
Definition: eval.c:50
external API header
main external API structure.
Definition: avcodec.h:1329
static void close(AVCodecParserContext *s)
Definition: h264_parser.c:327
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:111
static void decode_flush(AVCodecContext *avctx)
Definition: kgv1dec.c:37
AVCodecContext * avctx
Definition: kgv1dec.c:33
static av_cold int decode_init(AVCodecContext *avctx)
Definition: kgv1dec.c:167
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
Definition: avcodec.h:997
AV_WL32 AV_WL24 AV_RL16
Definition: bytestream.h:89
common internal api header.
#define AV_LOG_DEBUG
Stuff which is only useful for libav* developers.
Definition: log.h:125
static av_cold void flush(AVCodecContext *avctx)
Flush (reset) the frame ID after seeking.
Definition: alsdec.c:1779
void * priv_data
Definition: avcodec.h:1531
#define FFSWAP(type, a, b)
Definition: common.h:58