lavc: fix decode_frame() third parameter semantics for video decoders
[libav.git] / libavcodec / ansi.c
1 /*
2 * ASCII/ANSI art decoder
3 * Copyright (c) 2010 Peter Ross <pross@xvid.org>
4 *
5 * This file is part of Libav.
6 *
7 * Libav 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 * Libav 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 Libav; 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 * ASCII/ANSI art decoder
25 */
26
27 #include "libavutil/common.h"
28 #include "libavutil/lfg.h"
29 #include "avcodec.h"
30 #include "cga_data.h"
31 #include "internal.h"
32
33 #define ATTR_BOLD 0x01 /**< Bold/Bright-foreground (mode 1) */
34 #define ATTR_FAINT 0x02 /**< Faint (mode 2) */
35 #define ATTR_UNDERLINE 0x08 /**< Underline (mode 4) */
36 #define ATTR_BLINK 0x10 /**< Blink/Bright-background (mode 5) */
37 #define ATTR_REVERSE 0x40 /**< Reverse (mode 7) */
38 #define ATTR_CONCEALED 0x80 /**< Concealed (mode 8) */
39
40 #define DEFAULT_FG_COLOR 7 /**< CGA color index */
41 #define DEFAULT_BG_COLOR 0
42 #define DEFAULT_SCREEN_MODE 3 /**< 80x25 */
43
44 #define FONT_WIDTH 8 /**< Font width */
45
46 /** map ansi color index to cga palette index */
47 static const uint8_t ansi_to_cga[16] = {
48 0, 4, 2, 6, 1, 5, 3, 7, 8, 12, 10, 14, 9, 13, 11, 15
49 };
50
51 typedef struct {
52 AVFrame frame;
53 int x; /**< x cursor position (pixels) */
54 int y; /**< y cursor position (pixels) */
55 int sx; /**< saved x cursor position (pixels) */
56 int sy; /**< saved y cursor position (pixels) */
57 const uint8_t* font; /**< font */
58 int font_height; /**< font height */
59 int attributes; /**< attribute flags */
60 int fg; /**< foreground color */
61 int bg; /**< background color */
62
63 /* ansi parser state machine */
64 enum {
65 STATE_NORMAL = 0,
66 STATE_ESCAPE,
67 STATE_CODE,
68 STATE_MUSIC_PREAMBLE
69 } state;
70 #define MAX_NB_ARGS 4
71 int args[MAX_NB_ARGS];
72 int nb_args; /**< number of arguments (may exceed MAX_NB_ARGS) */
73 } AnsiContext;
74
75 static av_cold int decode_init(AVCodecContext *avctx)
76 {
77 AnsiContext *s = avctx->priv_data;
78 avctx->pix_fmt = AV_PIX_FMT_PAL8;
79
80 /* defaults */
81 s->font = ff_vga16_font;
82 s->font_height = 16;
83 s->fg = DEFAULT_FG_COLOR;
84 s->bg = DEFAULT_BG_COLOR;
85
86 if (!avctx->width || !avctx->height)
87 avcodec_set_dimensions(avctx, 80<<3, 25<<4);
88
89 return 0;
90 }
91
92 static void hscroll(AVCodecContext *avctx)
93 {
94 AnsiContext *s = avctx->priv_data;
95 int i;
96
97 if (s->y < avctx->height - s->font_height) {
98 s->y += s->font_height;
99 return;
100 }
101
102 i = 0;
103 for (; i < avctx->height - s->font_height; i++)
104 memcpy(s->frame.data[0] + i * s->frame.linesize[0],
105 s->frame.data[0] + (i + s->font_height) * s->frame.linesize[0],
106 avctx->width);
107 for (; i < avctx->height; i++)
108 memset(s->frame.data[0] + i * s->frame.linesize[0],
109 DEFAULT_BG_COLOR, avctx->width);
110 }
111
112 static void erase_line(AVCodecContext * avctx, int xoffset, int xlength)
113 {
114 AnsiContext *s = avctx->priv_data;
115 int i;
116 for (i = 0; i < s->font_height; i++)
117 memset(s->frame.data[0] + (s->y + i)*s->frame.linesize[0] + xoffset,
118 DEFAULT_BG_COLOR, xlength);
119 }
120
121 static void erase_screen(AVCodecContext *avctx)
122 {
123 AnsiContext *s = avctx->priv_data;
124 int i;
125 for (i = 0; i < avctx->height; i++)
126 memset(s->frame.data[0] + i * s->frame.linesize[0], DEFAULT_BG_COLOR, avctx->width);
127 s->x = s->y = 0;
128 }
129
130 /**
131 * Draw character to screen
132 */
133 static void draw_char(AVCodecContext *avctx, int c)
134 {
135 AnsiContext *s = avctx->priv_data;
136 int fg = s->fg;
137 int bg = s->bg;
138
139 if ((s->attributes & ATTR_BOLD))
140 fg += 8;
141 if ((s->attributes & ATTR_BLINK))
142 bg += 8;
143 if ((s->attributes & ATTR_REVERSE))
144 FFSWAP(int, fg, bg);
145 if ((s->attributes & ATTR_CONCEALED))
146 fg = bg;
147 ff_draw_pc_font(s->frame.data[0] + s->y * s->frame.linesize[0] + s->x,
148 s->frame.linesize[0], s->font, s->font_height, c, fg, bg);
149 s->x += FONT_WIDTH;
150 if (s->x >= avctx->width) {
151 s->x = 0;
152 hscroll(avctx);
153 }
154 }
155
156 /**
157 * Execute ANSI escape code
158 * @return 0 on success, negative on error
159 */
160 static int execute_code(AVCodecContext * avctx, int c)
161 {
162 AnsiContext *s = avctx->priv_data;
163 int ret, i, width, height;
164 switch(c) {
165 case 'A': //Cursor Up
166 s->y = FFMAX(s->y - (s->nb_args > 0 ? s->args[0]*s->font_height : s->font_height), 0);
167 break;
168 case 'B': //Cursor Down
169 s->y = FFMIN(s->y + (s->nb_args > 0 ? s->args[0]*s->font_height : s->font_height), avctx->height - s->font_height);
170 break;
171 case 'C': //Cursor Right
172 s->x = FFMIN(s->x + (s->nb_args > 0 ? s->args[0]*FONT_WIDTH : FONT_WIDTH), avctx->width - FONT_WIDTH);
173 break;
174 case 'D': //Cursor Left
175 s->x = FFMAX(s->x - (s->nb_args > 0 ? s->args[0]*FONT_WIDTH : FONT_WIDTH), 0);
176 break;
177 case 'H': //Cursor Position
178 case 'f': //Horizontal and Vertical Position
179 s->y = s->nb_args > 0 ? av_clip((s->args[0] - 1)*s->font_height, 0, avctx->height - s->font_height) : 0;
180 s->x = s->nb_args > 1 ? av_clip((s->args[1] - 1)*FONT_WIDTH, 0, avctx->width - FONT_WIDTH) : 0;
181 break;
182 case 'h': //set creen mode
183 case 'l': //reset screen mode
184 if (s->nb_args < 2)
185 s->args[0] = DEFAULT_SCREEN_MODE;
186 switch(s->args[0]) {
187 case 0: case 1: case 4: case 5: case 13: case 19: //320x200 (25 rows)
188 s->font = ff_cga_font;
189 s->font_height = 8;
190 width = 40<<3;
191 height = 25<<3;
192 break;
193 case 2: case 3: //640x400 (25 rows)
194 s->font = ff_vga16_font;
195 s->font_height = 16;
196 width = 80<<3;
197 height = 25<<4;
198 break;
199 case 6: case 14: //640x200 (25 rows)
200 s->font = ff_cga_font;
201 s->font_height = 8;
202 width = 80<<3;
203 height = 25<<3;
204 break;
205 case 7: //set line wrapping
206 break;
207 case 15: case 16: //640x350 (43 rows)
208 s->font = ff_cga_font;
209 s->font_height = 8;
210 width = 80<<3;
211 height = 43<<3;
212 break;
213 case 17: case 18: //640x480 (60 rows)
214 s->font = ff_cga_font;
215 s->font_height = 8;
216 width = 80<<3;
217 height = 60<<4;
218 break;
219 default:
220 av_log_ask_for_sample(avctx, "unsupported screen mode\n");
221 }
222 if (width != avctx->width || height != avctx->height) {
223 if (s->frame.data[0])
224 avctx->release_buffer(avctx, &s->frame);
225 avcodec_set_dimensions(avctx, width, height);
226 ret = ff_get_buffer(avctx, &s->frame);
227 if (ret < 0) {
228 av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
229 return ret;
230 }
231 s->frame.pict_type = AV_PICTURE_TYPE_I;
232 s->frame.palette_has_changed = 1;
233 memcpy(s->frame.data[1], ff_cga_palette, 16 * 4);
234 erase_screen(avctx);
235 } else if (c == 'l') {
236 erase_screen(avctx);
237 }
238 break;
239 case 'J': //Erase in Page
240 switch (s->args[0]) {
241 case 0:
242 erase_line(avctx, s->x, avctx->width - s->x);
243 if (s->y < avctx->height - s->font_height)
244 memset(s->frame.data[0] + (s->y + s->font_height)*s->frame.linesize[0],
245 DEFAULT_BG_COLOR, (avctx->height - s->y - s->font_height)*s->frame.linesize[0]);
246 break;
247 case 1:
248 erase_line(avctx, 0, s->x);
249 if (s->y > 0)
250 memset(s->frame.data[0], DEFAULT_BG_COLOR, s->y * s->frame.linesize[0]);
251 break;
252 case 2:
253 erase_screen(avctx);
254 }
255 break;
256 case 'K': //Erase in Line
257 switch(s->args[0]) {
258 case 0:
259 erase_line(avctx, s->x, avctx->width - s->x);
260 break;
261 case 1:
262 erase_line(avctx, 0, s->x);
263 break;
264 case 2:
265 erase_line(avctx, 0, avctx->width);
266 }
267 break;
268 case 'm': //Select Graphics Rendition
269 if (s->nb_args == 0) {
270 s->nb_args = 1;
271 s->args[0] = 0;
272 }
273 for (i = 0; i < FFMIN(s->nb_args, MAX_NB_ARGS); i++) {
274 int m = s->args[i];
275 if (m == 0) {
276 s->attributes = 0;
277 s->fg = DEFAULT_FG_COLOR;
278 s->bg = DEFAULT_BG_COLOR;
279 } else if (m == 1 || m == 2 || m == 4 || m == 5 || m == 7 || m == 8) {
280 s->attributes |= 1 << (m - 1);
281 } else if (m >= 30 && m <= 38) {
282 s->fg = ansi_to_cga[m - 30];
283 } else if (m == 39) {
284 s->fg = ansi_to_cga[DEFAULT_FG_COLOR];
285 } else if (m >= 40 && m <= 47) {
286 s->bg = ansi_to_cga[m - 40];
287 } else if (m == 49) {
288 s->fg = ansi_to_cga[DEFAULT_BG_COLOR];
289 } else {
290 av_log_ask_for_sample(avctx, "unsupported rendition parameter\n");
291 }
292 }
293 break;
294 case 'n': //Device Status Report
295 case 'R': //report current line and column
296 /* ignore */
297 break;
298 case 's': //Save Cursor Position
299 s->sx = s->x;
300 s->sy = s->y;
301 break;
302 case 'u': //Restore Cursor Position
303 s->x = av_clip(s->sx, 0, avctx->width - FONT_WIDTH);
304 s->y = av_clip(s->sy, 0, avctx->height - s->font_height);
305 break;
306 default:
307 av_log_ask_for_sample(avctx, "unsupported escape code\n");
308 break;
309 }
310 return 0;
311 }
312
313 static int decode_frame(AVCodecContext *avctx,
314 void *data, int *got_frame,
315 AVPacket *avpkt)
316 {
317 AnsiContext *s = avctx->priv_data;
318 uint8_t *buf = avpkt->data;
319 int buf_size = avpkt->size;
320 const uint8_t *buf_end = buf+buf_size;
321 int ret, i, count;
322
323 ret = avctx->reget_buffer(avctx, &s->frame);
324 if (ret < 0){
325 av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
326 return ret;
327 }
328 s->frame.pict_type = AV_PICTURE_TYPE_I;
329 s->frame.palette_has_changed = 1;
330 memcpy(s->frame.data[1], ff_cga_palette, 16 * 4);
331
332 while(buf < buf_end) {
333 switch(s->state) {
334 case STATE_NORMAL:
335 switch (buf[0]) {
336 case 0x00: //NUL
337 case 0x07: //BEL
338 case 0x1A: //SUB
339 /* ignore */
340 break;
341 case 0x08: //BS
342 s->x = FFMAX(s->x - 1, 0);
343 break;
344 case 0x09: //HT
345 i = s->x / FONT_WIDTH;
346 count = ((i + 8) & ~7) - i;
347 for (i = 0; i < count; i++)
348 draw_char(avctx, ' ');
349 break;
350 case 0x0A: //LF
351 hscroll(avctx);
352 case 0x0D: //CR
353 s->x = 0;
354 break;
355 case 0x0C: //FF
356 erase_screen(avctx);
357 break;
358 case 0x1B: //ESC
359 s->state = STATE_ESCAPE;
360 break;
361 default:
362 draw_char(avctx, buf[0]);
363 }
364 break;
365 case STATE_ESCAPE:
366 if (buf[0] == '[') {
367 s->state = STATE_CODE;
368 s->nb_args = 0;
369 s->args[0] = 0;
370 } else {
371 s->state = STATE_NORMAL;
372 draw_char(avctx, 0x1B);
373 return -1;
374 continue;
375 }
376 break;
377 case STATE_CODE:
378 switch(buf[0]) {
379 case '0': case '1': case '2': case '3': case '4':
380 case '5': case '6': case '7': case '8': case '9':
381 if (s->nb_args < MAX_NB_ARGS)
382 s->args[s->nb_args] = s->args[s->nb_args] * 10 + buf[0] - '0';
383 break;
384 case ';':
385 s->nb_args++;
386 if (s->nb_args < MAX_NB_ARGS)
387 s->args[s->nb_args] = 0;
388 break;
389 case 'M':
390 s->state = STATE_MUSIC_PREAMBLE;
391 break;
392 case '=': case '?':
393 /* ignore */
394 break;
395 default:
396 if (s->nb_args > MAX_NB_ARGS)
397 av_log(avctx, AV_LOG_WARNING, "args overflow (%i)\n", s->nb_args);
398 if (s->nb_args < MAX_NB_ARGS && s->args[s->nb_args])
399 s->nb_args++;
400 if (execute_code(avctx, buf[0]) < 0)
401 return -1;
402 s->state = STATE_NORMAL;
403 }
404 break;
405 case STATE_MUSIC_PREAMBLE:
406 if (buf[0] == 0x0E || buf[0] == 0x1B)
407 s->state = STATE_NORMAL;
408 /* ignore music data */
409 break;
410 }
411 buf++;
412 }
413
414 *got_frame = 1;
415 *(AVFrame*)data = s->frame;
416 return buf_size;
417 }
418
419 static av_cold int decode_close(AVCodecContext *avctx)
420 {
421 AnsiContext *s = avctx->priv_data;
422 if (s->frame.data[0])
423 avctx->release_buffer(avctx, &s->frame);
424 return 0;
425 }
426
427 AVCodec ff_ansi_decoder = {
428 .name = "ansi",
429 .type = AVMEDIA_TYPE_VIDEO,
430 .id = AV_CODEC_ID_ANSI,
431 .priv_data_size = sizeof(AnsiContext),
432 .init = decode_init,
433 .close = decode_close,
434 .decode = decode_frame,
435 .capabilities = CODEC_CAP_DR1,
436 .long_name = NULL_IF_CONFIG_SMALL("ASCII/ANSI art"),
437 };