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  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  uint16_t *out, *prev;
54  int outcnt = 0, maxcnt;
55  int w, h, i, res;
56 
57  if (avpkt->size < 2)
58  return -1;
59 
60  w = (buf[0] + 1) * 8;
61  h = (buf[1] + 1) * 8;
62  buf += 2;
63 
64  if (av_image_check_size(w, h, 0, avctx))
65  return -1;
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 = (uint16_t *) c->cur.data[0];
79  if (c->prev.data[0]) {
80  prev = (uint16_t *) 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  out[outcnt++] = code; // rgb555 pixel coded directly
94  } else {
95  int count;
96  int inp_off;
97  uint16_t *inp;
98 
99  if ((code & 0x6000) == 0x6000) {
100  // copy from previous frame
101  int oidx = (code >> 10) & 7;
102  int start;
103 
104  count = (code & 0x3FF) + 3;
105 
106  if (offsets[oidx] < 0) {
107  if (buf_end - 3 < buf)
108  break;
109  offsets[oidx] = AV_RL24(buf);
110  buf += 3;
111  }
112 
113  start = (outcnt + offsets[oidx]) % maxcnt;
114 
115  if (maxcnt - start < count)
116  break;
117 
118  if (!prev) {
119  av_log(avctx, AV_LOG_ERROR,
120  "Frame reference does not exist\n");
121  break;
122  }
123 
124  inp = prev;
125  inp_off = start;
126  } else {
127  // copy from earlier in this frame
128  int offset = (code & 0x1FFF) + 1;
129 
130  if (!(code & 0x6000)) {
131  count = 2;
132  } else if ((code & 0x6000) == 0x2000) {
133  count = 3;
134  } else {
135  if (buf_end - 1 < buf)
136  break;
137  count = 4 + *buf++;
138  }
139 
140  if (outcnt < offset)
141  break;
142 
143  inp = out;
144  inp_off = outcnt - offset;
145  }
146 
147  if (maxcnt - outcnt < count)
148  break;
149 
150  for (i = inp_off; i < count + inp_off; i++) {
151  out[outcnt++] = inp[i];
152  }
153  }
154  }
155 
156  if (outcnt - maxcnt)
157  av_log(avctx, AV_LOG_DEBUG, "frame finished with %d diff\n", outcnt - maxcnt);
158 
159  *got_frame = 1;
160  *(AVFrame*)data = c->cur;
161 
162  if (c->prev.data[0])
163  avctx->release_buffer(avctx, &c->prev);
164  FFSWAP(AVFrame, c->cur, c->prev);
165 
166  return avpkt->size;
167 }
168 
170 {
171  KgvContext * const c = avctx->priv_data;
172 
173  c->avctx = avctx;
174  avctx->pix_fmt = AV_PIX_FMT_RGB555;
175  avctx->flags |= CODEC_FLAG_EMU_EDGE;
176 
177  return 0;
178 }
179 
181 {
182  decode_flush(avctx);
183  return 0;
184 }
185 
187  .name = "kgv1",
188  .type = AVMEDIA_TYPE_VIDEO,
189  .id = AV_CODEC_ID_KGV1,
190  .priv_data_size = sizeof(KgvContext),
191  .init = decode_init,
192  .close = decode_end,
193  .decode = decode_frame,
194  .flush = decode_flush,
195  .long_name = NULL_IF_CONFIG_SMALL("Kega Game Video"),
196  .capabilities = CODEC_CAP_DR1,
197 };
This structure describes decoded (raw) audio or video data.
Definition: avcodec.h:989
void(* release_buffer)(struct AVCodecContext *c, AVFrame *pic)
Called to release buffers which were allocated with get_buffer.
Definition: avcodec.h:2259
misc image utilities
void avcodec_set_dimensions(AVCodecContext *s, int width, int height)
Definition: utils.c:149
int size
Definition: avcodec.h:916
enum AVPixelFormat pix_fmt
Pixel format, see AV_PIX_FMT_xxx.
Definition: avcodec.h:1533
#define AV_RL16
Definition: intreadwrite.h:42
static av_cold int decode_end(AVCodecContext *avctx)
Definition: kgv1dec.c:180
AVFrame prev
Definition: kgv1dec.c:35
AVCodec.
Definition: avcodec.h:2960
static int decode(MimicContext *ctx, int quality, int num_coeffs, int is_iframe)
Definition: mimic.c:228
uint8_t
const char data[16]
Definition: mxf.c:66
uint8_t * data
Definition: avcodec.h:915
static int init(AVCodecParserContext *s)
Definition: h264_parser.c:335
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:1132
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
Definition: internal.h:88
int flags
CODEC_FLAG_*.
Definition: avcodec.h:1434
void av_log(void *avcl, int level, const char *fmt,...)
Definition: log.c:146
const char * name
Name of the codec implementation.
Definition: avcodec.h:2967
AVFrame cur
Definition: kgv1dec.c:35
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:220
AVCodec ff_kgv1_decoder
Definition: kgv1dec.c:186
int width
picture width / height.
Definition: avcodec.h:1508
int ff_get_buffer(AVCodecContext *avctx, AVFrame *frame)
Get a buffer for a frame.
Definition: utils.c:464
NULL
Definition: eval.c:52
static int decode_frame(AVCodecContext *avctx, void *data, int *got_frame, AVPacket *avpkt)
Definition: kgv1dec.c:46
external API header
main external API structure.
Definition: avcodec.h:1339
static void close(AVCodecParserContext *s)
Definition: h264_parser.c:326
static void decode_flush(AVCodecContext *avctx)
Definition: kgv1dec.c:38
AVCodecContext * avctx
Definition: kgv1dec.c:34
static av_cold int decode_init(AVCodecContext *avctx)
Definition: kgv1dec.c:169
#define AV_RL24
Definition: intreadwrite.h:78
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
Definition: avcodec.h:997
common internal api header.
common internal and external API header
static av_cold void flush(AVCodecContext *avctx)
Flush (reset) the frame ID after seeking.
Definition: alsdec.c:1772
void * priv_data
Definition: avcodec.h:1382
This structure stores compressed data.
Definition: avcodec.h:898
if(!(ptr_align%ac->ptr_align)&&samples_align >=aligned_len)