]> git.sesse.net Git - ffmpeg/blob - libavcodec/ansi.c
ansi: stop using deprecated avcodec_set_dimensions
[ffmpeg] / 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         ff_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             avpriv_request_sample(avctx, "Unsupported screen mode");
226         }
227         if (width != avctx->width || height != avctx->height) {
228             av_frame_unref(s->frame);
229             ret = ff_set_dimensions(avctx, width, height);
230             if (ret < 0)
231                 return ret;
232             ret = ff_get_buffer(avctx, s->frame, AV_GET_BUFFER_FLAG_REF);
233             if (ret < 0) {
234                 av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
235                 return ret;
236             }
237             s->frame->pict_type           = AV_PICTURE_TYPE_I;
238             s->frame->palette_has_changed = 1;
239             memcpy(s->frame->data[1], ff_cga_palette, 16 * 4);
240             erase_screen(avctx);
241         } else if (c == 'l') {
242             erase_screen(avctx);
243         }
244         break;
245     case 'J': //Erase in Page
246         switch (s->args[0]) {
247         case 0:
248             erase_line(avctx, s->x, avctx->width - s->x);
249             if (s->y < avctx->height - s->font_height)
250                 memset(s->frame->data[0] + (s->y + s->font_height)*s->frame->linesize[0],
251                     DEFAULT_BG_COLOR, (avctx->height - s->y - s->font_height)*s->frame->linesize[0]);
252             break;
253         case 1:
254             erase_line(avctx, 0, s->x);
255             if (s->y > 0)
256                 memset(s->frame->data[0], DEFAULT_BG_COLOR, s->y * s->frame->linesize[0]);
257             break;
258         case 2:
259             erase_screen(avctx);
260         }
261         break;
262     case 'K': //Erase in Line
263         switch(s->args[0]) {
264         case 0:
265             erase_line(avctx, s->x, avctx->width - s->x);
266             break;
267         case 1:
268             erase_line(avctx, 0, s->x);
269             break;
270         case 2:
271             erase_line(avctx, 0, avctx->width);
272         }
273         break;
274     case 'm': //Select Graphics Rendition
275         if (s->nb_args == 0) {
276             s->nb_args = 1;
277             s->args[0] = 0;
278         }
279         for (i = 0; i < FFMIN(s->nb_args, MAX_NB_ARGS); i++) {
280             int m = s->args[i];
281             if (m == 0) {
282                 s->attributes = 0;
283                 s->fg = DEFAULT_FG_COLOR;
284                 s->bg = DEFAULT_BG_COLOR;
285             } else if (m == 1 || m == 2 || m == 4 || m == 5 || m == 7 || m == 8) {
286                 s->attributes |= 1 << (m - 1);
287             } else if (m >= 30 && m <= 38) {
288                 s->fg = ansi_to_cga[m - 30];
289             } else if (m == 39) {
290                 s->fg = ansi_to_cga[DEFAULT_FG_COLOR];
291             } else if (m >= 40 && m <= 47) {
292                 s->bg = ansi_to_cga[m - 40];
293             } else if (m == 49) {
294                 s->fg = ansi_to_cga[DEFAULT_BG_COLOR];
295             } else {
296                 avpriv_request_sample(avctx, "Unsupported rendition parameter");
297             }
298         }
299         break;
300     case 'n': //Device Status Report
301     case 'R': //report current line and column
302         /* ignore */
303         break;
304     case 's': //Save Cursor Position
305         s->sx = s->x;
306         s->sy = s->y;
307         break;
308     case 'u': //Restore Cursor Position
309         s->x = av_clip(s->sx, 0, avctx->width  - FONT_WIDTH);
310         s->y = av_clip(s->sy, 0, avctx->height - s->font_height);
311         break;
312     default:
313         avpriv_request_sample(avctx, "Unknown escape code");
314         break;
315     }
316     return 0;
317 }
318
319 static int decode_frame(AVCodecContext *avctx,
320                             void *data, int *got_frame,
321                             AVPacket *avpkt)
322 {
323     AnsiContext *s = avctx->priv_data;
324     uint8_t *buf = avpkt->data;
325     int buf_size = avpkt->size;
326     const uint8_t *buf_end   = buf+buf_size;
327     int ret, i, count;
328
329     ret = ff_reget_buffer(avctx, s->frame);
330     if (ret < 0){
331         av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
332         return ret;
333     }
334     if (!avctx->frame_number) {
335         memset(s->frame->data[0], 0, avctx->height * FFABS(s->frame->linesize[0]));
336         memset(s->frame->data[1], 0, AVPALETTE_SIZE);
337     }
338
339     s->frame->pict_type           = AV_PICTURE_TYPE_I;
340     s->frame->palette_has_changed = 1;
341     memcpy(s->frame->data[1], ff_cga_palette, 16 * 4);
342
343     while(buf < buf_end) {
344         switch(s->state) {
345         case STATE_NORMAL:
346             switch (buf[0]) {
347             case 0x00: //NUL
348             case 0x07: //BEL
349             case 0x1A: //SUB
350                 /* ignore */
351                 break;
352             case 0x08: //BS
353                 s->x = FFMAX(s->x - 1, 0);
354                 break;
355             case 0x09: //HT
356                 i = s->x / FONT_WIDTH;
357                 count = ((i + 8) & ~7) - i;
358                 for (i = 0; i < count; i++)
359                     draw_char(avctx, ' ');
360                 break;
361             case 0x0A: //LF
362                 hscroll(avctx);
363             case 0x0D: //CR
364                 s->x = 0;
365                 break;
366             case 0x0C: //FF
367                 erase_screen(avctx);
368                 break;
369             case 0x1B: //ESC
370                 s->state = STATE_ESCAPE;
371                 break;
372             default:
373                 draw_char(avctx, buf[0]);
374             }
375             break;
376         case STATE_ESCAPE:
377             if (buf[0] == '[') {
378                 s->state   = STATE_CODE;
379                 s->nb_args = 0;
380                 s->args[0] = 0;
381             } else {
382                 s->state = STATE_NORMAL;
383                 draw_char(avctx, 0x1B);
384                 continue;
385             }
386             break;
387         case STATE_CODE:
388             switch(buf[0]) {
389             case '0': case '1': case '2': case '3': case '4':
390             case '5': case '6': case '7': case '8': case '9':
391                 if (s->nb_args < MAX_NB_ARGS)
392                     s->args[s->nb_args] = s->args[s->nb_args] * 10 + buf[0] - '0';
393                 break;
394             case ';':
395                 s->nb_args++;
396                 if (s->nb_args < MAX_NB_ARGS)
397                     s->args[s->nb_args] = 0;
398                 break;
399             case 'M':
400                 s->state = STATE_MUSIC_PREAMBLE;
401                 break;
402             case '=': case '?':
403                 /* ignore */
404                 break;
405             default:
406                 if (s->nb_args > MAX_NB_ARGS)
407                     av_log(avctx, AV_LOG_WARNING, "args overflow (%i)\n", s->nb_args);
408                 if (s->nb_args < MAX_NB_ARGS && s->args[s->nb_args])
409                     s->nb_args++;
410                 if ((ret = execute_code(avctx, buf[0])) < 0)
411                     return ret;
412                 s->state = STATE_NORMAL;
413             }
414             break;
415         case STATE_MUSIC_PREAMBLE:
416             if (buf[0] == 0x0E || buf[0] == 0x1B)
417                 s->state = STATE_NORMAL;
418             /* ignore music data */
419             break;
420         }
421         buf++;
422     }
423
424     *got_frame = 1;
425     if ((ret = av_frame_ref(data, s->frame)) < 0)
426         return ret;
427     return buf_size;
428 }
429
430 static av_cold int decode_close(AVCodecContext *avctx)
431 {
432     AnsiContext *s = avctx->priv_data;
433
434     av_frame_free(&s->frame);
435     return 0;
436 }
437
438 AVCodec ff_ansi_decoder = {
439     .name           = "ansi",
440     .long_name      = NULL_IF_CONFIG_SMALL("ASCII/ANSI art"),
441     .type           = AVMEDIA_TYPE_VIDEO,
442     .id             = AV_CODEC_ID_ANSI,
443     .priv_data_size = sizeof(AnsiContext),
444     .init           = decode_init,
445     .close          = decode_close,
446     .decode         = decode_frame,
447     .capabilities   = CODEC_CAP_DR1,
448 };