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