FFmpeg
Loading...
Searching...
No Matches
cdgraphics.c
Go to the documentation of this file.
1/*
2 * CD Graphics Video Decoder
3 * Copyright (c) 2009 Michael Tison
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#include "avcodec.h"
23#include "bytestream.h"
24#include "codec_internal.h"
25#include "decode.h"
26
28
29/**
30 * @file
31 * @brief CD Graphics Video Decoder
32 * @author Michael Tison
33 * @see http://wiki.multimedia.cx/index.php?title=CD_Graphics
34 * @see http://www.ccs.neu.edu/home/bchafy/cdb/info/cdg
35 */
36
37/// default screen sizes
38#define CDG_FULL_WIDTH 300
39#define CDG_FULL_HEIGHT 216
40#define CDG_DISPLAY_WIDTH 294
41#define CDG_DISPLAY_HEIGHT 204
42#define CDG_BORDER_WIDTH 6
43#define CDG_BORDER_HEIGHT 12
44
45/// masks
46#define CDG_COMMAND 0x09
47#define CDG_MASK 0x3F
48
49/// instruction codes
50#define CDG_INST_MEMORY_PRESET 1
51#define CDG_INST_BORDER_PRESET 2
52#define CDG_INST_TILE_BLOCK 6
53#define CDG_INST_SCROLL_PRESET 20
54#define CDG_INST_SCROLL_COPY 24
55#define CDG_INST_TRANSPARENT_COL 28
56#define CDG_INST_LOAD_PAL_LO 30
57#define CDG_INST_LOAD_PAL_HIGH 31
58#define CDG_INST_TILE_BLOCK_XOR 38
59
60/// data sizes
61#define CDG_PACKET_SIZE 24
62#define CDG_DATA_SIZE 16
63#define CDG_TILE_HEIGHT 12
64#define CDG_TILE_WIDTH 6
65#define CDG_MINIMUM_PKT_SIZE 6
66#define CDG_MINIMUM_SCROLL_SIZE 3
67#define CDG_HEADER_SIZE 8
68#define CDG_PALETTE_SIZE 16
69
77
79{
80 CDGraphicsContext *cc = avctx->priv_data;
81
82 cc->frame = av_frame_alloc();
83 if (!cc->frame)
84 return AVERROR(ENOMEM);
85
86 for (int i = 0; i < CDG_PALETTE_SIZE; i++)
87 cc->alpha[i] = 0xFFU;
88
89 avctx->pix_fmt = AV_PIX_FMT_PAL8;
91}
92
93static void cdg_border_preset(CDGraphicsContext *cc, uint8_t *data)
94{
95 ptrdiff_t lsize = cc->frame->linesize[0];
96 uint8_t *buf = cc->frame->data[0];
97 int color = data[0] & 0x0F;
98
99 if (!(data[1] & 0x0F)) {
100 /// fill the top and bottom borders
101 for (int y = 0; y < CDG_BORDER_HEIGHT; y++)
102 memset(buf + y * lsize, color, cc->frame->width);
104 memset(buf + y * lsize, color, cc->frame->width);
105
106 /// fill the side borders
107 for (int y = CDG_BORDER_HEIGHT; y < CDG_FULL_HEIGHT - CDG_BORDER_HEIGHT; y++) {
108 memset(buf + y * lsize, color, CDG_BORDER_WIDTH);
109 memset(buf + CDG_FULL_WIDTH - CDG_BORDER_WIDTH + y * lsize,
111 }
112 }
113}
114
115static void cdg_load_palette(CDGraphicsContext *cc, uint8_t *data, int low)
116{
117 uint8_t r, g, b;
118 uint16_t color;
119 int i;
120 int array_offset = low ? 0 : 8;
121 uint32_t *palette = (uint32_t *) cc->frame->data[1];
122
123 for (i = 0; i < 8; i++) {
124 color = (data[2 * i] << 6) + (data[2 * i + 1] & 0x3F);
125 r = ((color >> 8) & 0x000F) * 17;
126 g = ((color >> 4) & 0x000F) * 17;
127 b = ((color ) & 0x000F) * 17;
128 palette[i + array_offset] = (uint32_t)cc->alpha[i + array_offset] << 24 | r << 16 | g << 8 | b;
129 }
130}
131
132static int cdg_tile_block(CDGraphicsContext *cc, uint8_t *data, int b)
133{
134 unsigned ci, ri;
135 int color;
136 int x, y;
137 int ai;
138 ptrdiff_t stride = cc->frame->linesize[0];
139 uint8_t *buf = cc->frame->data[0];
140
141 ri = (data[2] & 0x1F) * CDG_TILE_HEIGHT + cc->vscroll;
142 ci = (data[3] & 0x3F) * CDG_TILE_WIDTH + cc->hscroll;
143
144 if (ri > (CDG_FULL_HEIGHT - CDG_TILE_HEIGHT))
145 return AVERROR(EINVAL);
146 if (ci > (CDG_FULL_WIDTH - CDG_TILE_WIDTH))
147 return AVERROR(EINVAL);
148
149 for (y = 0; y < CDG_TILE_HEIGHT; y++) {
150 for (x = 0; x < CDG_TILE_WIDTH; x++) {
151 if (!((data[4 + y] >> (5 - x)) & 0x01))
152 color = data[0] & 0x0F;
153 else
154 color = data[1] & 0x0F;
155
156 ai = ci + x + (stride * (ri + y));
157 if (b)
158 color ^= buf[ai];
159 buf[ai] = color;
160 }
161 }
162
163 return 0;
164}
165
166#define UP 2
167#define DOWN 1
168#define LEFT 2
169#define RIGHT 1
170
171static void cdg_copy_rect_buf(int out_tl_x, int out_tl_y, uint8_t *out,
172 int in_tl_x, int in_tl_y, uint8_t *in,
173 int w, int h, int stride)
174{
175 int y;
176
177 in += in_tl_x + in_tl_y * stride;
178 out += out_tl_x + out_tl_y * stride;
179 for (y = 0; y < h; y++)
180 memcpy(out + y * stride, in + y * stride, w);
181}
182
183static void cdg_fill_rect_preset(int tl_x, int tl_y, uint8_t *out,
184 int color, int w, int h, int stride)
185{
186 int y;
187
188 for (y = tl_y; y < tl_y + h; y++)
189 memset(out + tl_x + y * stride, color, w);
190}
191
192static void cdg_fill_wrapper(int out_tl_x, int out_tl_y, uint8_t *out,
193 int in_tl_x, int in_tl_y, uint8_t *in,
194 int color, int w, int h, int stride, int roll)
195{
196 if (roll) {
197 cdg_copy_rect_buf(out_tl_x, out_tl_y, out, in_tl_x, in_tl_y,
198 in, w, h, stride);
199 } else {
200 cdg_fill_rect_preset(out_tl_x, out_tl_y, out, color, w, h, stride);
201 }
202}
203
204static void cdg_scroll(CDGraphicsContext *cc, uint8_t *data,
205 AVFrame *new_frame, int roll_over)
206{
207 int color;
208 int hscmd, h_off, hinc, vscmd, v_off, vinc;
209 int y;
210 ptrdiff_t stride = cc->frame->linesize[0];
211 uint8_t *in = cc->frame->data[0];
212 uint8_t *out = new_frame->data[0];
213
214 color = data[0] & 0x0F;
215 hscmd = (data[1] & 0x30) >> 4;
216 vscmd = (data[2] & 0x30) >> 4;
217
218 h_off = FFMIN(data[1] & 0x07, CDG_BORDER_WIDTH - 1);
219 v_off = FFMIN(data[2] & 0x0F, CDG_BORDER_HEIGHT - 1);
220
221 /// find the difference and save the offset for cdg_tile_block usage
222 hinc = h_off - cc->hscroll;
223 vinc = cc->vscroll - v_off;
224 cc->hscroll = h_off;
225 cc->vscroll = v_off;
226
227 if (vscmd == UP)
228 vinc -= 12;
229 if (vscmd == DOWN)
230 vinc += 12;
231 if (hscmd == LEFT)
232 hinc -= 6;
233 if (hscmd == RIGHT)
234 hinc += 6;
235
236 if (!hinc && !vinc)
237 return;
238
239 memcpy(new_frame->data[1], cc->frame->data[1], CDG_PALETTE_SIZE * 4);
240
241 for (y = FFMAX(0, vinc); y < FFMIN(CDG_FULL_HEIGHT + vinc, CDG_FULL_HEIGHT); y++)
242 memcpy(out + FFMAX(0, hinc) + stride * y,
243 in + FFMAX(0, hinc) - hinc + (y - vinc) * stride,
244 FFABS(stride) - FFABS(hinc));
245
246 if (vinc > 0)
247 cdg_fill_wrapper(0, 0, out,
248 0, CDG_FULL_HEIGHT - vinc, in, color,
249 FFABS(stride), vinc, stride, roll_over);
250 else if (vinc < 0)
252 0, 0, in, color,
253 FFABS(stride), -1 * vinc, stride, roll_over);
254
255 if (hinc > 0)
256 cdg_fill_wrapper(0, 0, out,
257 CDG_FULL_WIDTH - hinc, 0, in, color,
258 hinc, CDG_FULL_HEIGHT, stride, roll_over);
259 else if (hinc < 0)
261 0, 0, in, color,
262 -1 * hinc, CDG_FULL_HEIGHT, stride, roll_over);
263
264}
265
267 int *got_frame, AVPacket *avpkt)
268{
270 int buf_size = avpkt->size;
271 int ret;
272 uint8_t command, inst;
273 uint8_t cdg_data[CDG_DATA_SIZE] = {0};
274 CDGraphicsContext *cc = avctx->priv_data;
275
276 if (buf_size < CDG_MINIMUM_PKT_SIZE) {
277 av_log(avctx, AV_LOG_ERROR, "buffer too small for decoder\n");
278 return AVERROR(EINVAL);
279 }
280 if (buf_size > CDG_HEADER_SIZE + CDG_DATA_SIZE) {
281 av_log(avctx, AV_LOG_ERROR, "buffer too big for decoder\n");
282 return AVERROR(EINVAL);
283 }
284
285 bytestream2_init(&gb, avpkt->data, avpkt->size);
286
287 if ((ret = ff_reget_buffer(avctx, cc->frame, 0)) < 0)
288 return ret;
289 if (!cc->cleared) {
290 for (int y = 0; y < avctx->height; y++)
291 memset(cc->frame->data[0] + y * cc->frame->linesize[0], 0, avctx->width);
292 memset(cc->frame->data[1], 0, AVPALETTE_SIZE);
293 cc->cleared = 1;
294 }
295
296 command = bytestream2_get_byte(&gb);
297 inst = bytestream2_get_byte(&gb);
298 inst &= CDG_MASK;
299 bytestream2_skip(&gb, 2);
300 bytestream2_get_buffer(&gb, cdg_data, sizeof(cdg_data));
301
302 if ((command & CDG_MASK) == CDG_COMMAND) {
303 switch (inst) {
305 if (!(cdg_data[1] & 0x0F)) {
306 for (int y = 0; y < avctx->height; y++)
307 memset(cc->frame->data[0] + y * cc->frame->linesize[0],
308 cdg_data[0] & 0x0F, avctx->width);
309 }
310 break;
313 if (buf_size - CDG_HEADER_SIZE < CDG_DATA_SIZE) {
314 av_log(avctx, AV_LOG_ERROR, "buffer too small for loading palette\n");
315 return AVERROR(EINVAL);
316 }
317
318 cdg_load_palette(cc, cdg_data, inst == CDG_INST_LOAD_PAL_LO);
319 break;
321 cdg_border_preset(cc, cdg_data);
322 break;
325 if (buf_size - CDG_HEADER_SIZE < CDG_DATA_SIZE) {
326 av_log(avctx, AV_LOG_ERROR, "buffer too small for drawing tile\n");
327 return AVERROR(EINVAL);
328 }
329
330 ret = cdg_tile_block(cc, cdg_data, inst == CDG_INST_TILE_BLOCK_XOR);
331 if (ret) {
332 av_log(avctx, AV_LOG_ERROR, "tile is out of range\n");
333 return ret;
334 }
335 break;
338 if (buf_size - CDG_HEADER_SIZE < CDG_MINIMUM_SCROLL_SIZE) {
339 av_log(avctx, AV_LOG_ERROR, "buffer too small for scrolling\n");
340 return AVERROR(EINVAL);
341 }
342
343 if ((ret = ff_get_buffer(avctx, frame, AV_GET_BUFFER_FLAG_REF)) < 0)
344 return ret;
345
346 cdg_scroll(cc, cdg_data, frame, inst == CDG_INST_SCROLL_COPY);
347 ret = av_frame_replace(cc->frame, frame);
348 if (ret < 0)
349 return ret;
350 break;
352 for (int i = 0; i < CDG_PALETTE_SIZE; i++)
353 cc->alpha[i] = 255 - ((cdg_data[i] & 0x3f) << 2);
354 break;
355 default:
356 break;
357 }
358
359 if (!frame->data[0]) {
360 ret = av_frame_ref(frame, cc->frame);
361 if (ret < 0)
362 return ret;
363 }
364 *got_frame = 1;
365 } else {
366 *got_frame = 0;
367 }
368
369 return avpkt->size;
370}
371
373{
374 CDGraphicsContext *cc = avctx->priv_data;
375
376 if (!cc->frame->data[0])
377 return;
378
379 for (int y = 0; y < avctx->height; y++)
380 memset(cc->frame->data[0] + y * cc->frame->linesize[0], 0, avctx->width);
381 if (!avctx->frame_num)
382 memset(cc->frame->data[1], 0, AVPALETTE_SIZE);
383}
384
386{
387 CDGraphicsContext *cc = avctx->priv_data;
388
389 av_frame_free(&cc->frame);
390
391 return 0;
392}
393
395 .p.name = "cdgraphics",
396 CODEC_LONG_NAME("CD Graphics video"),
397 .p.type = AVMEDIA_TYPE_VIDEO,
399 .priv_data_size = sizeof(CDGraphicsContext),
403 .flush = cdg_decode_flush,
404 .p.capabilities = AV_CODEC_CAP_DR1,
405};
const FFCodec ff_cdgraphics_decoder
Definition cdgraphics.c:394
static FILE * out
static av_cold void close(AVCodecParserContext *s)
Definition apv_parser.c:197
Libavcodec external API header.
static av_always_inline unsigned int bytestream2_get_buffer(GetByteContext *g, uint8_t *dst, unsigned int size)
Definition bytestream.h:267
static av_always_inline void bytestream2_init(GetByteContext *g, const uint8_t *buf, int buf_size)
Definition bytestream.h:137
static av_always_inline void bytestream2_skip(GetByteContext *g, unsigned int size)
Definition bytestream.h:168
#define i(width, name, range_min, range_max)
Definition cbs_h264.c:63
#define CDG_COMMAND
masks
Definition cdgraphics.c:46
#define CDG_INST_SCROLL_COPY
Definition cdgraphics.c:54
#define UP
Definition cdgraphics.c:166
#define CDG_DATA_SIZE
Definition cdgraphics.c:62
#define CDG_INST_TILE_BLOCK_XOR
Definition cdgraphics.c:58
static void cdg_fill_wrapper(int out_tl_x, int out_tl_y, uint8_t *out, int in_tl_x, int in_tl_y, uint8_t *in, int color, int w, int h, int stride, int roll)
Definition cdgraphics.c:192
#define CDG_TILE_HEIGHT
Definition cdgraphics.c:63
#define DOWN
Definition cdgraphics.c:167
#define LEFT
Definition cdgraphics.c:168
#define CDG_INST_LOAD_PAL_HIGH
Definition cdgraphics.c:57
#define CDG_INST_LOAD_PAL_LO
Definition cdgraphics.c:56
#define CDG_BORDER_WIDTH
Definition cdgraphics.c:42
static void cdg_scroll(CDGraphicsContext *cc, uint8_t *data, AVFrame *new_frame, int roll_over)
Definition cdgraphics.c:204
#define CDG_PALETTE_SIZE
Definition cdgraphics.c:68
static void cdg_fill_rect_preset(int tl_x, int tl_y, uint8_t *out, int color, int w, int h, int stride)
Definition cdgraphics.c:183
#define CDG_INST_TRANSPARENT_COL
Definition cdgraphics.c:55
static void cdg_load_palette(CDGraphicsContext *cc, uint8_t *data, int low)
Definition cdgraphics.c:115
static av_cold int cdg_decode_init(AVCodecContext *avctx)
Definition cdgraphics.c:78
#define CDG_MINIMUM_SCROLL_SIZE
Definition cdgraphics.c:66
#define RIGHT
Definition cdgraphics.c:169
static int cdg_decode_frame(AVCodecContext *avctx, AVFrame *frame, int *got_frame, AVPacket *avpkt)
Definition cdgraphics.c:266
#define CDG_INST_SCROLL_PRESET
Definition cdgraphics.c:53
#define CDG_FULL_WIDTH
default screen sizes
Definition cdgraphics.c:38
static int cdg_tile_block(CDGraphicsContext *cc, uint8_t *data, int b)
Definition cdgraphics.c:132
#define CDG_INST_BORDER_PRESET
Definition cdgraphics.c:51
#define CDG_INST_MEMORY_PRESET
instruction codes
Definition cdgraphics.c:50
#define CDG_FULL_HEIGHT
Definition cdgraphics.c:39
static av_cold void cdg_decode_flush(AVCodecContext *avctx)
Definition cdgraphics.c:372
static void cdg_border_preset(CDGraphicsContext *cc, uint8_t *data)
Definition cdgraphics.c:93
#define CDG_HEADER_SIZE
Definition cdgraphics.c:67
#define CDG_MASK
Definition cdgraphics.c:47
#define CDG_TILE_WIDTH
Definition cdgraphics.c:64
#define CDG_INST_TILE_BLOCK
Definition cdgraphics.c:52
static av_cold int cdg_decode_end(AVCodecContext *avctx)
Definition cdgraphics.c:385
#define CDG_BORDER_HEIGHT
Definition cdgraphics.c:43
static void cdg_copy_rect_buf(int out_tl_x, int out_tl_y, uint8_t *out, int in_tl_x, int in_tl_y, uint8_t *in, int w, int h, int stride)
Definition cdgraphics.c:171
#define CDG_MINIMUM_PKT_SIZE
Definition cdgraphics.c:65
#define FF_CODEC_DECODE_CB(func)
#define CODEC_LONG_NAME(str)
#define FFABS(a)
Absolute value, Note, INT_MIN / INT64_MIN result in undefined behavior as they are not representable ...
Definition common.h:74
int ff_get_buffer(AVCodecContext *avctx, AVFrame *frame, int flags)
Get a buffer for a frame.
Definition decode.c:1777
int ff_reget_buffer(AVCodecContext *avctx, AVFrame *frame, int flags)
Identical in function to ff_get_buffer(), except it reuses the existing buffer if available.
Definition decode.c:1906
int ff_set_dimensions(AVCodecContext *s, int width, int height)
Definition utils.c:91
static AVFrame * frame
int(* init)(AVBSFContext *ctx)
Definition dts2pts.c:608
#define AV_CODEC_CAP_DR1
Codec uses get_buffer() or get_encode_buffer() for allocating buffers and supports custom allocators.
Definition codec.h:49
#define AV_GET_BUFFER_FLAG_REF
The decoder will keep a reference to the frame and may reuse it later.
Definition avcodec.h:415
@ AV_CODEC_ID_CDGRAPHICS
Definition codec_id.h:182
#define AVERROR(e)
Definition error.h:45
int av_frame_replace(AVFrame *dst, const AVFrame *src)
Ensure the destination frame refers to the same data described by the source frame,...
Definition frame.c:376
int av_frame_ref(AVFrame *dst, const AVFrame *src)
Set up a new reference to the data described by the source frame.
Definition frame.c:278
void av_frame_free(AVFrame **frame)
Free the frame and any dynamically allocated objects in it, e.g.
Definition frame.c:64
AVFrame * av_frame_alloc(void)
Allocate an AVFrame and set its fields to default values.
Definition frame.c:52
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition log.h:210
@ AVMEDIA_TYPE_VIDEO
Definition avutil.h:200
#define r
Definition input.c:42
#define b
Definition input.c:43
Macro definitions for various function/variable attributes.
#define av_cold
Definition attributes.h:117
uint8_t w
Definition llvidencdsp.c:39
#define FFMIN(a, b)
Definition macros.h:49
#define FFMAX(a, b)
Definition macros.h:47
const char data[16]
Definition mxf.c:149
@ AV_PIX_FMT_PAL8
8 bits with AV_PIX_FMT_RGB32 palette
Definition pixfmt.h:84
#define AVPALETTE_SIZE
Definition pixfmt.h:32
main external API structure.
Definition avcodec.h:443
enum AVPixelFormat pix_fmt
Pixel format, see AV_PIX_FMT_xxx.
Definition avcodec.h:643
int width
picture width / height.
Definition avcodec.h:604
int64_t frame_num
Frame counter, set by libavcodec.
Definition avcodec.h:1883
void * priv_data
Definition avcodec.h:470
This structure describes decoded (raw) audio or video data.
Definition frame.h:472
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
Definition frame.h:493
int width
Definition frame.h:544
int linesize[AV_NUM_DATA_POINTERS]
For video, a positive or negative value, which is typically indicating the size in bytes of each pict...
Definition frame.h:517
This structure stores compressed data.
Definition packet.h:580
int size
Definition packet.h:604
uint8_t * data
Definition packet.h:603
AVFrame * frame
Definition cdgraphics.c:71
uint8_t alpha[CDG_PALETTE_SIZE]
Definition cdgraphics.c:74
#define stride
#define av_log(a,...)
const char * g
Definition vf_curves.c:128
static int command(AVFilterContext *ctx, const char *cmd, const char *arg, char *res, int res_len, int flags)