FFmpeg
 All Data Structures Namespaces 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 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 (w != avctx->width || h != avctx->height) {
68  if ((res = ff_set_dimensions(avctx, w, h)) < 0)
69  return res;
70  }
71 
72  if (!c->frame_buffer) {
73  c->frame_buffer = av_mallocz(avctx->width * avctx->height * 2);
74  c->last_frame_buffer = av_mallocz(avctx->width * avctx->height * 2);
75  if (!c->frame_buffer || !c->last_frame_buffer) {
76  decode_flush(avctx);
77  return AVERROR(ENOMEM);
78  }
79  }
80 
81  maxcnt = w * h;
82 
83  if ((res = ff_get_buffer(avctx, frame, 0)) < 0)
84  return res;
85  out = (uint8_t*)c->frame_buffer;
86  prev = (uint8_t*)c->last_frame_buffer;
87 
88  for (i = 0; i < 8; i++)
89  offsets[i] = -1;
90 
91  while (outcnt < maxcnt && buf_end - 2 >= buf) {
92  int code = AV_RL16(buf);
93  buf += 2;
94 
95  if (!(code & 0x8000)) {
96  AV_WN16A(&out[2 * outcnt], code); // rgb555 pixel coded directly
97  outcnt++;
98  } else {
99  int count;
100 
101  if ((code & 0x6000) == 0x6000) {
102  // copy from previous frame
103  int oidx = (code >> 10) & 7;
104  int start;
105 
106  count = (code & 0x3FF) + 3;
107 
108  if (offsets[oidx] < 0) {
109  if (buf_end - 3 < buf)
110  break;
111  offsets[oidx] = AV_RL24(buf);
112  buf += 3;
113  }
114 
115  start = (outcnt + offsets[oidx]) % maxcnt;
116 
117  if (maxcnt - start < count || maxcnt - outcnt < count)
118  break;
119 
120  if (!prev) {
121  av_log(avctx, AV_LOG_ERROR,
122  "Frame reference does not exist\n");
123  break;
124  }
125 
126  memcpy(out + 2 * outcnt, prev + 2 * start, 2 * count);
127  } else {
128  // copy from earlier in this frame
129  int offset = (code & 0x1FFF) + 1;
130 
131  if (!(code & 0x6000)) {
132  count = 2;
133  } else if ((code & 0x6000) == 0x2000) {
134  count = 3;
135  } else {
136  if (buf_end - 1 < buf)
137  break;
138  count = 4 + *buf++;
139  }
140 
141  if (outcnt < offset || maxcnt - outcnt < count)
142  break;
143 
144  av_memcpy_backptr(out + 2 * outcnt, 2 * offset, 2 * count);
145  }
146  outcnt += count;
147  }
148  }
149 
150  if (outcnt - maxcnt)
151  av_log(avctx, AV_LOG_DEBUG, "frame finished with %d diff\n", outcnt - maxcnt);
152 
153  av_image_copy_plane(frame->data[0], frame->linesize[0],
154  (const uint8_t*)c->frame_buffer, avctx->width * 2,
155  avctx->width * 2, avctx->height);
156  FFSWAP(uint16_t *, c->frame_buffer, c->last_frame_buffer);
157 
158  *got_frame = 1;
159 
160  return avpkt->size;
161 }
162 
164 {
165  avctx->pix_fmt = AV_PIX_FMT_RGB555;
166 
167  return 0;
168 }
169 
171 {
172  decode_flush(avctx);
173  return 0;
174 }
175 
177  .name = "kgv1",
178  .long_name = NULL_IF_CONFIG_SMALL("Kega Game Video"),
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  .capabilities = CODEC_CAP_DR1,
187 };
#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:171
ptrdiff_t const GLvoid * data
Definition: opengl_enc.c:101
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:229
static av_cold int init(AVCodecContext *avctx)
Definition: avrndec.c:35
int size
Definition: avcodec.h:1163
enum AVPixelFormat pix_fmt
Pixel format, see AV_PIX_FMT_xxx.
Definition: avcodec.h:1444
static av_cold int decode_end(AVCodecContext *avctx)
Definition: kgv1dec.c:170
AVCodec.
Definition: avcodec.h:3181
uint64_t_TMPL AV_WL64 unsigned int_TMPL AV_WL32 unsigned int_TMPL AV_WL24 unsigned int_TMPL AV_RL16
Definition: bytestream.h:85
uint8_t
#define av_cold
Definition: attributes.h:74
#define CODEC_CAP_DR1
Codec uses get_buffer() for allocating buffers and supports custom allocators.
Definition: avcodec.h:789
static AVFrame * frame
uint8_t * data
Definition: avcodec.h:1162
#define av_log(a,...)
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:176
#define AVERROR(e)
Definition: error.h:43
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
Definition: internal.h:175
#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:3188
uint64_t_TMPL AV_WL64 unsigned int_TMPL AV_WL32 unsigned int_TMPL AV_RL24
Definition: bytestream.h:85
static const uint8_t offset[127][2]
Definition: vf_spp.c:92
GLsizei count
Definition: opengl_enc.c:109
Libavcodec external API header.
AVCodec ff_kgv1_decoder
Definition: kgv1dec.c:176
int width
picture width / height.
Definition: avcodec.h:1414
#define AV_WN16A(p, v)
Definition: intreadwrite.h:534
static void flush(AVCodecContext *avctx)
Definition: aacdec.c:514
static int decode_frame(AVCodecContext *avctx, void *data, int *got_frame, AVPacket *avpkt)
Definition: kgv1dec.c:46
int linesize[AV_NUM_DATA_POINTERS]
For video, size in bytes of each picture line.
Definition: frame.h:199
main external API structure.
Definition: avcodec.h:1241
int ff_get_buffer(AVCodecContext *avctx, AVFrame *frame, int flags)
Get a buffer for a frame.
Definition: utils.c:1035
void * buf
Definition: avisynth_c.h:553
static void decode_flush(AVCodecContext *avctx)
Definition: kgv1dec.c:38
static av_cold int decode_init(AVCodecContext *avctx)
Definition: kgv1dec.c:163
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
Definition: frame.h:182
static int decode(AVCodecContext *avctx, void *data, int *got_sub, AVPacket *avpkt)
Definition: ccaption_dec.c:522
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
static double c[64]
#define AV_PIX_FMT_RGB555
Definition: pixfmt.h:352
void * priv_data
Definition: avcodec.h:1283
uint8_t pi<< 24) CONV_FUNC_GROUP(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_U8, uint8_t,(*(constuint8_t *) pi-0x80)*(1.0f/(1<< 7))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_U8, uint8_t,(*(constuint8_t *) pi-0x80)*(1.0/(1<< 7))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_S16, int16_t,(*(constint16_t *) pi >>8)+0x80) CONV_FUNC_GROUP(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_S16, int16_t,*(constint16_t *) pi *(1.0f/(1<< 15))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_S16, int16_t,*(constint16_t *) pi *(1.0/(1<< 15))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_S32, int32_t,(*(constint32_t *) pi >>24)+0x80) CONV_FUNC_GROUP(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_S32, int32_t,*(constint32_t *) pi *(1.0f/(1U<< 31))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_S32, int32_t,*(constint32_t *) pi *(1.0/(1U<< 31))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_FLT, float, av_clip_uint8(lrintf(*(constfloat *) pi *(1<< 7))+0x80)) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S16, int16_t, AV_SAMPLE_FMT_FLT, float, av_clip_int16(lrintf(*(constfloat *) pi *(1<< 15)))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S32, int32_t, AV_SAMPLE_FMT_FLT, float, av_clipl_int32(llrintf(*(constfloat *) pi *(1U<< 31)))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_DBL, double, av_clip_uint8(lrint(*(constdouble *) pi *(1<< 7))+0x80)) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S16, int16_t, AV_SAMPLE_FMT_DBL, double, av_clip_int16(lrint(*(constdouble *) pi *(1<< 15)))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S32, int32_t, AV_SAMPLE_FMT_DBL, double, av_clipl_int32(llrint(*(constdouble *) pi *(1U<< 31))))#defineSET_CONV_FUNC_GROUP(ofmt, ifmt) staticvoidset_generic_function(AudioConvert *ac){}voidff_audio_convert_free(AudioConvert **ac){if(!*ac) return;ff_dither_free(&(*ac) ->dc);av_freep(ac);}AudioConvert *ff_audio_convert_alloc(AVAudioResampleContext *avr, enumAVSampleFormatout_fmt, enumAVSampleFormatin_fmt, intchannels, intsample_rate, intapply_map){AudioConvert *ac;intin_planar, out_planar;ac=av_mallocz(sizeof(*ac));if(!ac) returnNULL;ac->avr=avr;ac->out_fmt=out_fmt;ac->in_fmt=in_fmt;ac->channels=channels;ac->apply_map=apply_map;if(avr->dither_method!=AV_RESAMPLE_DITHER_NONE &&av_get_packed_sample_fmt(out_fmt)==AV_SAMPLE_FMT_S16 &&av_get_bytes_per_sample(in_fmt)>2){ac->dc=ff_dither_alloc(avr, out_fmt, in_fmt, channels, sample_rate, apply_map);if(!ac->dc){av_free(ac);returnNULL;}returnac;}in_planar=ff_sample_fmt_is_planar(in_fmt, channels);out_planar=ff_sample_fmt_is_planar(out_fmt, channels);if(in_planar==out_planar){ac->func_type=CONV_FUNC_TYPE_FLAT;ac->planes=in_planar?ac->channels:1;}elseif(in_planar) ac->func_type=CONV_FUNC_TYPE_INTERLEAVE;elseac->func_type=CONV_FUNC_TYPE_DEINTERLEAVE;set_generic_function(ac);if(ARCH_AARCH64) ff_audio_convert_init_aarch64(ac);if(ARCH_ARM) ff_audio_convert_init_arm(ac);if(ARCH_X86) ff_audio_convert_init_x86(ac);returnac;}intff_audio_convert(AudioConvert *ac, AudioData *out, AudioData *in){intuse_generic=1;intlen=in->nb_samples;intp;if(ac->dc){av_log(ac->avr, AV_LOG_TRACE,"%dsamples-audio_convert:%sto%s(dithered)\n", len, av_get_sample_fmt_name(ac->in_fmt), av_get_sample_fmt_name(ac->out_fmt));returnff_convert_dither(ac-> out
#define av_freep(p)
void INT64 start
Definition: avisynth_c.h:553
void av_memcpy_backptr(uint8_t *dst, int back, int cnt)
deliberately overlapping memcpy implementation
Definition: mem.c:428
#define FFSWAP(type, a, b)
Definition: common.h:69
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:273
This structure stores compressed data.
Definition: avcodec.h:1139
void * av_mallocz(size_t size)
Allocate a block of size bytes with alignment suitable for all memory accesses (including vectors if ...
Definition: mem.c:250