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 "libavutil/mem.h"
31 #include "avcodec.h"
32 #include "codec_internal.h"
33 #include "decode.h"
34 
35 typedef struct KgvContext {
36  uint16_t *frame_buffer;
37  uint16_t *last_frame_buffer;
38 } KgvContext;
39 
40 static void decode_flush(AVCodecContext *avctx)
41 {
42  KgvContext * const c = avctx->priv_data;
43 
44  av_freep(&c->frame_buffer);
45  av_freep(&c->last_frame_buffer);
46 }
47 
49  int *got_frame, AVPacket *avpkt)
50 {
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  uint8_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 (avpkt->size < 2 + w*h / 513)
67  return AVERROR_INVALIDDATA;
68 
69  if (w != avctx->width || h != avctx->height) {
70  av_freep(&c->frame_buffer);
71  av_freep(&c->last_frame_buffer);
72  if ((res = ff_set_dimensions(avctx, w, h)) < 0)
73  return res;
74  }
75 
76  if (!c->frame_buffer) {
77  c->frame_buffer = av_mallocz(avctx->width * avctx->height * 2);
78  c->last_frame_buffer = av_mallocz(avctx->width * avctx->height * 2);
79  if (!c->frame_buffer || !c->last_frame_buffer) {
80  decode_flush(avctx);
81  return AVERROR(ENOMEM);
82  }
83  }
84 
85  maxcnt = w * h;
86 
87  if ((res = ff_get_buffer(avctx, frame, 0)) < 0)
88  return res;
89  out = (uint8_t*)c->frame_buffer;
90  prev = (uint8_t*)c->last_frame_buffer;
91 
92  for (i = 0; i < 8; i++)
93  offsets[i] = -1;
94 
95  while (outcnt < maxcnt && buf_end - 2 >= buf) {
96  int code = AV_RL16(buf);
97  buf += 2;
98 
99  if (!(code & 0x8000)) {
100  AV_WN16A(&out[2 * outcnt], code); // rgb555 pixel coded directly
101  outcnt++;
102  } else {
103  int count;
104 
105  if ((code & 0x6000) == 0x6000) {
106  // copy from previous frame
107  int oidx = (code >> 10) & 7;
108  int start;
109 
110  count = (code & 0x3FF) + 3;
111 
112  if (offsets[oidx] < 0) {
113  if (buf_end - 3 < buf)
114  break;
115  offsets[oidx] = AV_RL24(buf);
116  buf += 3;
117  }
118 
119  start = (outcnt + offsets[oidx]) % maxcnt;
120 
121  if (maxcnt - start < count || maxcnt - outcnt < count)
122  break;
123 
124  if (!prev) {
125  av_log(avctx, AV_LOG_ERROR,
126  "Frame reference does not exist\n");
127  break;
128  }
129 
130  memcpy(out + 2 * outcnt, prev + 2 * start, 2 * count);
131  } else {
132  // copy from earlier in this frame
133  int offset = (code & 0x1FFF) + 1;
134 
135  if (!(code & 0x6000)) {
136  count = 2;
137  } else if ((code & 0x6000) == 0x2000) {
138  count = 3;
139  } else {
140  if (buf_end - 1 < buf)
141  break;
142  count = 4 + *buf++;
143  }
144 
145  if (outcnt < offset || maxcnt - outcnt < count)
146  break;
147 
148  av_memcpy_backptr(out + 2 * outcnt, 2 * offset, 2 * count);
149  }
150  outcnt += count;
151  }
152  }
153 
154  if (outcnt - maxcnt)
155  av_log(avctx, AV_LOG_DEBUG, "frame finished with %d diff\n", outcnt - maxcnt);
156 
157  av_image_copy_plane(frame->data[0], frame->linesize[0],
158  (const uint8_t*)c->frame_buffer, avctx->width * 2,
159  avctx->width * 2, avctx->height);
160  FFSWAP(uint16_t *, c->frame_buffer, c->last_frame_buffer);
161 
162  *got_frame = 1;
163 
164  return avpkt->size;
165 }
166 
168 {
169  avctx->pix_fmt = AV_PIX_FMT_RGB555;
170 
171  return 0;
172 }
173 
175 {
176  decode_flush(avctx);
177  return 0;
178 }
179 
181  .p.name = "kgv1",
182  CODEC_LONG_NAME("Kega Game Video"),
183  .p.type = AVMEDIA_TYPE_VIDEO,
184  .p.id = AV_CODEC_ID_KGV1,
185  .priv_data_size = sizeof(KgvContext),
186  .init = decode_init,
187  .close = decode_end,
189  .flush = decode_flush,
190  .p.capabilities = AV_CODEC_CAP_DR1,
191 };
AVERROR
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
out
FILE * out
Definition: movenc.c:55
AVFrame
This structure describes decoded (raw) audio or video data.
Definition: frame.h:374
w
uint8_t w
Definition: llviddspenc.c:38
AVPacket::data
uint8_t * data
Definition: packet.h:524
decode_frame
static int decode_frame(AVCodecContext *avctx, AVFrame *frame, int *got_frame, AVPacket *avpkt)
Definition: kgv1dec.c:48
FFCodec
Definition: codec_internal.h:127
ff_set_dimensions
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:94
av_image_copy_plane
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:374
FFCodec::p
AVCodec p
The public AVCodec.
Definition: codec_internal.h:131
KgvContext
Definition: kgv1dec.c:35
AV_LOG_ERROR
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:180
av_cold
#define av_cold
Definition: attributes.h:90
av_memcpy_backptr
void av_memcpy_backptr(uint8_t *dst, int back, int cnt)
Overlapping memcpy() implementation.
Definition: mem.c:447
FF_CODEC_DECODE_CB
#define FF_CODEC_DECODE_CB(func)
Definition: codec_internal.h:287
intreadwrite.h
offsets
static const int offsets[]
Definition: hevc_pel.c:34
AV_LOG_DEBUG
#define AV_LOG_DEBUG
Stuff which is only useful for libav* developers.
Definition: log.h:201
decode.h
AV_RL16
uint64_t_TMPL AV_WL64 unsigned int_TMPL AV_WL32 unsigned int_TMPL AV_WL24 unsigned int_TMPL AV_RL16
Definition: bytestream.h:94
AV_WN16A
#define AV_WN16A(p, v)
Definition: intreadwrite.h:532
CODEC_LONG_NAME
#define CODEC_LONG_NAME(str)
Definition: codec_internal.h:272
decode_flush
static void decode_flush(AVCodecContext *avctx)
Definition: kgv1dec.c:40
c
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
ff_get_buffer
int ff_get_buffer(AVCodecContext *avctx, AVFrame *frame, int flags)
Get a buffer for a frame.
Definition: decode.c:1554
init
int(* init)(AVBSFContext *ctx)
Definition: dts2pts.c:366
AV_CODEC_CAP_DR1
#define AV_CODEC_CAP_DR1
Codec uses get_buffer() or get_encode_buffer() for allocating buffers and supports custom allocators.
Definition: codec.h:52
AVPacket::size
int size
Definition: packet.h:525
codec_internal.h
AV_RL24
uint64_t_TMPL AV_WL64 unsigned int_TMPL AV_WL32 unsigned int_TMPL AV_RL24
Definition: bytestream.h:93
offset
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
Definition: writing_filters.txt:86
i
#define i(width, name, range_min, range_max)
Definition: cbs_h2645.c:256
code
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
Definition: filter_design.txt:178
common.h
AV_PIX_FMT_RGB555
#define AV_PIX_FMT_RGB555
Definition: pixfmt.h:466
AV_CODEC_ID_KGV1
@ AV_CODEC_ID_KGV1
Definition: codec_id.h:190
ff_kgv1_decoder
const FFCodec ff_kgv1_decoder
Definition: kgv1dec.c:180
av_mallocz
void * av_mallocz(size_t size)
Allocate a memory block with alignment suitable for all memory accesses (including vectors if availab...
Definition: mem.c:256
AVCodec::name
const char * name
Name of the codec implementation.
Definition: codec.h:194
decode_init
static av_cold int decode_init(AVCodecContext *avctx)
Definition: kgv1dec.c:167
AVCodecContext::height
int height
Definition: avcodec.h:618
AVCodecContext::pix_fmt
enum AVPixelFormat pix_fmt
Pixel format, see AV_PIX_FMT_xxx.
Definition: avcodec.h:657
avcodec.h
FFSWAP
#define FFSWAP(type, a, b)
Definition: macros.h:52
frame
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
Definition: filter_design.txt:264
AVCodecContext
main external API structure.
Definition: avcodec.h:445
AVMEDIA_TYPE_VIDEO
@ AVMEDIA_TYPE_VIDEO
Definition: avutil.h:201
mem.h
KgvContext::frame_buffer
uint16_t * frame_buffer
Definition: kgv1dec.c:36
KgvContext::last_frame_buffer
uint16_t * last_frame_buffer
Definition: kgv1dec.c:37
AVPacket
This structure stores compressed data.
Definition: packet.h:501
AVCodecContext::priv_data
void * priv_data
Definition: avcodec.h:472
av_freep
#define av_freep(p)
Definition: tableprint_vlc.h:34
AVCodecContext::width
int width
picture width / height.
Definition: avcodec.h:618
imgutils.h
av_log
#define av_log(a,...)
Definition: tableprint_vlc.h:27
AVERROR_INVALIDDATA
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition: error.h:61
h
h
Definition: vp9dsp_template.c:2038
decode_end
static av_cold int decode_end(AVCodecContext *avctx)
Definition: kgv1dec.c:174