FFmpeg
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 KgvContext {
34  uint16_t *frame_buffer;
35  uint16_t *last_frame_buffer;
36 } KgvContext;
37 
38 static void decode_flush(AVCodecContext *avctx)
39 {
40  KgvContext * const c = avctx->priv_data;
41 
44 }
45 
46 static int decode_frame(AVCodecContext *avctx, void *data, int *got_frame,
47  AVPacket *avpkt)
48 {
49  AVFrame *frame = data;
50  const uint8_t *buf = avpkt->data;
51  const uint8_t *buf_end = buf + avpkt->size;
52  KgvContext * const c = avctx->priv_data;
53  int offsets[8];
54  uint8_t *out, *prev;
55  int outcnt = 0, maxcnt;
56  int w, h, i, res;
57 
58  if (avpkt->size < 2)
59  return AVERROR_INVALIDDATA;
60 
61  w = (buf[0] + 1) * 8;
62  h = (buf[1] + 1) * 8;
63  buf += 2;
64 
65  if (avpkt->size < 2 + w*h / 513)
66  return AVERROR_INVALIDDATA;
67 
68  if (w != avctx->width || h != avctx->height) {
71  if ((res = ff_set_dimensions(avctx, w, h)) < 0)
72  return res;
73  }
74 
75  if (!c->frame_buffer) {
76  c->frame_buffer = av_mallocz(avctx->width * avctx->height * 2);
77  c->last_frame_buffer = av_mallocz(avctx->width * avctx->height * 2);
78  if (!c->frame_buffer || !c->last_frame_buffer) {
79  decode_flush(avctx);
80  return AVERROR(ENOMEM);
81  }
82  }
83 
84  maxcnt = w * h;
85 
86  if ((res = ff_get_buffer(avctx, frame, 0)) < 0)
87  return res;
88  out = (uint8_t*)c->frame_buffer;
89  prev = (uint8_t*)c->last_frame_buffer;
90 
91  for (i = 0; i < 8; i++)
92  offsets[i] = -1;
93 
94  while (outcnt < maxcnt && buf_end - 2 >= buf) {
95  int code = AV_RL16(buf);
96  buf += 2;
97 
98  if (!(code & 0x8000)) {
99  AV_WN16A(&out[2 * outcnt], code); // rgb555 pixel coded directly
100  outcnt++;
101  } else {
102  int count;
103 
104  if ((code & 0x6000) == 0x6000) {
105  // copy from previous frame
106  int oidx = (code >> 10) & 7;
107  int start;
108 
109  count = (code & 0x3FF) + 3;
110 
111  if (offsets[oidx] < 0) {
112  if (buf_end - 3 < buf)
113  break;
114  offsets[oidx] = AV_RL24(buf);
115  buf += 3;
116  }
117 
118  start = (outcnt + offsets[oidx]) % maxcnt;
119 
120  if (maxcnt - start < count || maxcnt - outcnt < count)
121  break;
122 
123  if (!prev) {
124  av_log(avctx, AV_LOG_ERROR,
125  "Frame reference does not exist\n");
126  break;
127  }
128 
129  memcpy(out + 2 * outcnt, prev + 2 * start, 2 * count);
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 || maxcnt - outcnt < count)
145  break;
146 
147  av_memcpy_backptr(out + 2 * outcnt, 2 * offset, 2 * count);
148  }
149  outcnt += count;
150  }
151  }
152 
153  if (outcnt - maxcnt)
154  av_log(avctx, AV_LOG_DEBUG, "frame finished with %d diff\n", outcnt - maxcnt);
155 
156  av_image_copy_plane(frame->data[0], frame->linesize[0],
157  (const uint8_t*)c->frame_buffer, avctx->width * 2,
158  avctx->width * 2, avctx->height);
159  FFSWAP(uint16_t *, c->frame_buffer, c->last_frame_buffer);
160 
161  *got_frame = 1;
162 
163  return avpkt->size;
164 }
165 
167 {
168  avctx->pix_fmt = AV_PIX_FMT_RGB555;
169 
170  return 0;
171 }
172 
174 {
175  decode_flush(avctx);
176  return 0;
177 }
178 
180  .name = "kgv1",
181  .long_name = NULL_IF_CONFIG_SMALL("Kega Game Video"),
182  .type = AVMEDIA_TYPE_VIDEO,
183  .id = AV_CODEC_ID_KGV1,
184  .priv_data_size = sizeof(KgvContext),
185  .init = decode_init,
186  .close = decode_end,
187  .decode = decode_frame,
188  .flush = decode_flush,
189  .capabilities = AV_CODEC_CAP_DR1,
190 };
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition: error.h:59
This structure describes decoded (raw) audio or video data.
Definition: frame.h:295
ptrdiff_t const GLvoid * data
Definition: opengl_enc.c:100
static void flush(AVCodecContext *avctx)
misc image utilities
int ff_set_dimensions(AVCodecContext *s, int width, int height)
Check that the provided frame dimensions are valid and set them on the codec context.
Definition: utils.c:104
static av_cold int init(AVCodecContext *avctx)
Definition: avrndec.c:35
int size
Definition: avcodec.h:1481
enum AVPixelFormat pix_fmt
Pixel format, see AV_PIX_FMT_xxx.
Definition: avcodec.h:1778
static av_cold int decode_end(AVCodecContext *avctx)
Definition: kgv1dec.c:173
void * av_mallocz(size_t size)
Allocate a memory block with alignment suitable for all memory accesses (including vectors if availab...
Definition: mem.c:236
AVCodec.
Definition: avcodec.h:3492
uint64_t_TMPL AV_WL64 unsigned int_TMPL AV_WL32 unsigned int_TMPL AV_WL24 unsigned int_TMPL AV_RL16
Definition: bytestream.h:87
static void decode(AVCodecContext *dec_ctx, AVPacket *pkt, AVFrame *frame, FILE *outfile)
Definition: decode_audio.c:71
uint8_t
#define av_cold
Definition: attributes.h:82
it s the only field you need to keep assuming you have a context There is some magic you don t need to care about around this just let it vf offset
Undefined Behavior In the C some operations are like signed integer dereferencing freed accessing outside allocated Undefined Behavior must not occur in a C it is not safe even if the output of undefined operations is unused The unsafety may seem nit picking but Optimizing compilers have in fact optimized code on the assumption that no undefined Behavior occurs Optimizing code based on wrong assumptions can and has in some cases lead to effects beyond the output of computations The signed integer overflow problem in speed critical code Code which is highly optimized and works with signed integers sometimes has the problem that often the output of the computation does not c
Definition: undefined.txt:32
uint8_t * data
Definition: avcodec.h:1480
void av_memcpy_backptr(uint8_t *dst, int back, int cnt)
Overlapping memcpy() implementation.
Definition: mem.c:426
#define av_log(a,...)
#define i(width, name, range_min, range_max)
Definition: cbs_h2645.c:259
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:176
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
Definition: internal.h:186
#define AV_LOG_DEBUG
Stuff which is only useful for libav* developers.
Definition: log.h:197
const char * name
Name of the codec implementation.
Definition: avcodec.h:3499
uint64_t_TMPL AV_WL64 unsigned int_TMPL AV_WL32 unsigned int_TMPL AV_RL24
Definition: bytestream.h:87
GLsizei count
Definition: opengl_enc.c:108
AVCodec ff_kgv1_decoder
Definition: kgv1dec.c:179
int width
picture width / height.
Definition: avcodec.h:1741
uint8_t w
Definition: llviddspenc.c:38
these buffered frames must be flushed immediately if a new input produces new the filter must not call request_frame to get more It must just process the frame or queue it The task of requesting more frames is left to the filter s request_frame method or the application If a filter has several the filter must be ready for frames arriving randomly on any input any filter with several inputs will most likely require some kind of queuing mechanism It is perfectly acceptable to have a limited queue and to drop frames when the inputs are too unbalanced request_frame For filters that do not use the this method is called when a frame is wanted on an output For a it should directly call filter_frame on the corresponding output For a if there are queued frames already one of these frames should be pushed If the filter should request a frame on one of its repeatedly until at least one frame has been pushed Return or at least make progress towards producing a frame
#define AV_WN16A(p, v)
Definition: intreadwrite.h:534
static int decode_frame(AVCodecContext *avctx, void *data, int *got_frame, AVPacket *avpkt)
Definition: kgv1dec.c:46
Libavcodec external API header.
int linesize[AV_NUM_DATA_POINTERS]
For video, size in bytes of each picture line.
Definition: frame.h:326
main external API structure.
Definition: avcodec.h:1568
int ff_get_buffer(AVCodecContext *avctx, AVFrame *frame, int flags)
Get a buffer for a frame.
Definition: decode.c:1968
void * buf
Definition: avisynth_c.h:766
static void decode_flush(AVCodecContext *avctx)
Definition: kgv1dec.c:38
static av_cold int decode_init(AVCodecContext *avctx)
Definition: kgv1dec.c:166
and forward the test the status of outputs and forward it to the corresponding return FFERROR_NOT_READY If the filters stores internally one or a few frame for some it can consider them to be part of the FIFO and delay acknowledging a status change accordingly Example code
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
Definition: frame.h:309
uint16_t * frame_buffer
Definition: kgv1dec.c:34
uint16_t * last_frame_buffer
Definition: kgv1dec.c:35
common internal api header.
common internal and external API header
#define AV_PIX_FMT_RGB555
Definition: pixfmt.h:375
void * priv_data
Definition: avcodec.h:1595
FILE * out
Definition: movenc.c:54
#define av_freep(p)
void INT64 start
Definition: avisynth_c.h:766
#define FFSWAP(type, a, b)
Definition: common.h:99
void av_image_copy_plane(uint8_t *dst, int dst_linesize, const uint8_t *src, int src_linesize, int bytewidth, int height)
Copy image plane from src to dst.
Definition: imgutils.c:338
Filter the word “frame” indicates either a video frame or a group of audio as stored in an AVFrame structure Format for each input and each output the list of supported formats For video that means pixel format For audio that means channel sample they are references to shared objects When the negotiation mechanism computes the intersection of the formats supported at each end of a all references to both lists are replaced with a reference to the intersection And when a single format is eventually chosen for a link amongst the remaining all references to the list are updated That means that if a filter requires that its input and output have the same format amongst a supported all it has to do is use a reference to the same list of formats query_formats can leave some formats unset and return AVERROR(EAGAIN) to cause the negotiation mechanism toagain later.That can be used by filters with complex requirements to use the format negotiated on one link to set the formats supported on another.Frame references ownership and permissions
This structure stores compressed data.
Definition: avcodec.h:1457
#define AV_CODEC_CAP_DR1
Codec uses get_buffer() for allocating buffers and supports custom allocators.
Definition: avcodec.h:984