/src/ffmpeg/libavcodec/kgv1dec.c
Line | Count | Source |
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 | 71.6k | { |
42 | 71.6k | KgvContext * const c = avctx->priv_data; |
43 | | |
44 | 71.6k | av_freep(&c->frame_buffer); |
45 | 71.6k | av_freep(&c->last_frame_buffer); |
46 | 71.6k | } |
47 | | |
48 | | static int decode_frame(AVCodecContext *avctx, AVFrame *frame, |
49 | | int *got_frame, AVPacket *avpkt) |
50 | 309k | { |
51 | 309k | const uint8_t *buf = avpkt->data; |
52 | 309k | const uint8_t *buf_end = buf + avpkt->size; |
53 | 309k | KgvContext * const c = avctx->priv_data; |
54 | 309k | int offsets[8]; |
55 | 309k | uint8_t *out, *prev; |
56 | 309k | int outcnt = 0, maxcnt; |
57 | 309k | int w, h, i, res; |
58 | | |
59 | 309k | if (avpkt->size < 2) |
60 | 169k | return AVERROR_INVALIDDATA; |
61 | | |
62 | 139k | w = (buf[0] + 1) * 8; |
63 | 139k | h = (buf[1] + 1) * 8; |
64 | 139k | buf += 2; |
65 | | |
66 | 139k | if (avpkt->size < 2 + w*h / 513) |
67 | 14.8k | return AVERROR_INVALIDDATA; |
68 | | |
69 | 125k | if (w != avctx->width || h != avctx->height) { |
70 | 11.4k | av_freep(&c->frame_buffer); |
71 | 11.4k | av_freep(&c->last_frame_buffer); |
72 | 11.4k | if ((res = ff_set_dimensions(avctx, w, h)) < 0) |
73 | 0 | return res; |
74 | 11.4k | } |
75 | | |
76 | 125k | if (!c->frame_buffer) { |
77 | 50.5k | c->frame_buffer = av_mallocz(avctx->width * avctx->height * 2); |
78 | 50.5k | c->last_frame_buffer = av_mallocz(avctx->width * avctx->height * 2); |
79 | 50.5k | if (!c->frame_buffer || !c->last_frame_buffer) { |
80 | 0 | decode_flush(avctx); |
81 | 0 | return AVERROR(ENOMEM); |
82 | 0 | } |
83 | 50.5k | } |
84 | | |
85 | 125k | maxcnt = w * h; |
86 | | |
87 | 125k | if ((res = ff_get_buffer(avctx, frame, 0)) < 0) |
88 | 0 | return res; |
89 | 125k | out = (uint8_t*)c->frame_buffer; |
90 | 125k | prev = (uint8_t*)c->last_frame_buffer; |
91 | | |
92 | 1.12M | for (i = 0; i < 8; i++) |
93 | 1.00M | offsets[i] = -1; |
94 | | |
95 | 313k | while (outcnt < maxcnt && buf_end - 2 >= buf) { |
96 | 192k | int code = AV_RL16(buf); |
97 | 192k | buf += 2; |
98 | | |
99 | 192k | if (!(code & 0x8000)) { |
100 | 117k | AV_WN16A(&out[2 * outcnt], code); // rgb555 pixel coded directly |
101 | 117k | outcnt++; |
102 | 117k | } else { |
103 | 75.0k | int count; |
104 | | |
105 | 75.0k | if ((code & 0x6000) == 0x6000) { |
106 | | // copy from previous frame |
107 | 55.3k | int oidx = (code >> 10) & 7; |
108 | 55.3k | int start; |
109 | | |
110 | 55.3k | count = (code & 0x3FF) + 3; |
111 | | |
112 | 55.3k | if (offsets[oidx] < 0) { |
113 | 3.06k | if (buf_end - 3 < buf) |
114 | 544 | break; |
115 | 2.52k | offsets[oidx] = AV_RL24(buf); |
116 | 2.52k | buf += 3; |
117 | 2.52k | } |
118 | | |
119 | 54.8k | start = (outcnt + offsets[oidx]) % maxcnt; |
120 | | |
121 | 54.8k | if (maxcnt - start < count || maxcnt - outcnt < count) |
122 | 1.40k | break; |
123 | | |
124 | 53.3k | if (!prev) { |
125 | 0 | av_log(avctx, AV_LOG_ERROR, |
126 | 0 | "Frame reference does not exist\n"); |
127 | 0 | break; |
128 | 0 | } |
129 | | |
130 | 53.3k | memcpy(out + 2 * outcnt, prev + 2 * start, 2 * count); |
131 | 53.3k | } else { |
132 | | // copy from earlier in this frame |
133 | 19.7k | int offset = (code & 0x1FFF) + 1; |
134 | | |
135 | 19.7k | if (!(code & 0x6000)) { |
136 | 5.61k | count = 2; |
137 | 14.1k | } else if ((code & 0x6000) == 0x2000) { |
138 | 6.21k | count = 3; |
139 | 7.89k | } else { |
140 | 7.89k | if (buf_end - 1 < buf) |
141 | 214 | break; |
142 | 7.67k | count = 4 + *buf++; |
143 | 7.67k | } |
144 | | |
145 | 19.5k | if (outcnt < offset || maxcnt - outcnt < count) |
146 | 1.34k | break; |
147 | | |
148 | 18.1k | av_memcpy_backptr(out + 2 * outcnt, 2 * offset, 2 * count); |
149 | 18.1k | } |
150 | 71.5k | outcnt += count; |
151 | 71.5k | } |
152 | 192k | } |
153 | | |
154 | 125k | if (outcnt - maxcnt) |
155 | 124k | av_log(avctx, AV_LOG_DEBUG, "frame finished with %d diff\n", outcnt - maxcnt); |
156 | | |
157 | 125k | av_image_copy_plane(frame->data[0], frame->linesize[0], |
158 | 125k | (const uint8_t*)c->frame_buffer, avctx->width * 2, |
159 | 125k | avctx->width * 2, avctx->height); |
160 | 125k | FFSWAP(uint16_t *, c->frame_buffer, c->last_frame_buffer); |
161 | | |
162 | 125k | *got_frame = 1; |
163 | | |
164 | 125k | return avpkt->size; |
165 | 125k | } |
166 | | |
167 | | static av_cold int decode_init(AVCodecContext *avctx) |
168 | 1.17k | { |
169 | 1.17k | avctx->pix_fmt = AV_PIX_FMT_RGB555; |
170 | | |
171 | 1.17k | return 0; |
172 | 1.17k | } |
173 | | |
174 | | static av_cold int decode_end(AVCodecContext *avctx) |
175 | 1.17k | { |
176 | 1.17k | decode_flush(avctx); |
177 | 1.17k | return 0; |
178 | 1.17k | } |
179 | | |
180 | | const FFCodec ff_kgv1_decoder = { |
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, |
188 | | FF_CODEC_DECODE_CB(decode_frame), |
189 | | .flush = decode_flush, |
190 | | .p.capabilities = AV_CODEC_CAP_DR1, |
191 | | }; |