Libav
ansi.c
Go to the documentation of this file.
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 
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
35 #define ATTR_FAINT 0x02
36 #define ATTR_UNDERLINE 0x08
37 #define ATTR_BLINK 0x10
38 #define ATTR_REVERSE 0x40
39 #define ATTR_CONCEALED 0x80
41 #define DEFAULT_FG_COLOR 7
42 #define DEFAULT_BG_COLOR 0
43 #define DEFAULT_SCREEN_MODE 3
45 #define FONT_WIDTH 8
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 {
54  int x;
55  int y;
56  int sx;
57  int sy;
58  const uint8_t* font;
60  int attributes;
61  int fg;
62  int bg;
64  /* ansi parser state machine */
65  enum {
66  STATE_NORMAL = 0,
69  STATE_MUSIC_PREAMBLE
70  } state;
71 #define MAX_NB_ARGS 4
72  int args[MAX_NB_ARGS];
73  int nb_args;
74 } AnsiContext;
75 
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  int ret = ff_set_dimensions(avctx, 80 << 3, 25 << 4);
93  if (ret < 0)
94  return ret;
95  }
96  return 0;
97 }
98 
99 static void hscroll(AVCodecContext *avctx)
100 {
101  AnsiContext *s = avctx->priv_data;
102  int i;
103 
104  if (s->y < avctx->height - s->font_height) {
105  s->y += s->font_height;
106  return;
107  }
108 
109  i = 0;
110  for (; i < avctx->height - s->font_height; i++)
111  memcpy(s->frame->data[0] + i * s->frame->linesize[0],
112  s->frame->data[0] + (i + s->font_height) * s->frame->linesize[0],
113  avctx->width);
114  for (; i < avctx->height; i++)
115  memset(s->frame->data[0] + i * s->frame->linesize[0],
116  DEFAULT_BG_COLOR, avctx->width);
117 }
118 
119 static void erase_line(AVCodecContext * avctx, int xoffset, int xlength)
120 {
121  AnsiContext *s = avctx->priv_data;
122  int i;
123  for (i = 0; i < s->font_height; i++)
124  memset(s->frame->data[0] + (s->y + i)*s->frame->linesize[0] + xoffset,
125  DEFAULT_BG_COLOR, xlength);
126 }
127 
128 static void erase_screen(AVCodecContext *avctx)
129 {
130  AnsiContext *s = avctx->priv_data;
131  int i;
132  for (i = 0; i < avctx->height; i++)
133  memset(s->frame->data[0] + i * s->frame->linesize[0], DEFAULT_BG_COLOR, avctx->width);
134  s->x = s->y = 0;
135 }
136 
140 static void draw_char(AVCodecContext *avctx, int c)
141 {
142  AnsiContext *s = avctx->priv_data;
143  int fg = s->fg;
144  int bg = s->bg;
145 
146  if ((s->attributes & ATTR_BOLD))
147  fg += 8;
148  if ((s->attributes & ATTR_BLINK))
149  bg += 8;
150  if ((s->attributes & ATTR_REVERSE))
151  FFSWAP(int, fg, bg);
152  if ((s->attributes & ATTR_CONCEALED))
153  fg = bg;
154  ff_draw_pc_font(s->frame->data[0] + s->y * s->frame->linesize[0] + s->x,
155  s->frame->linesize[0], s->font, s->font_height, c, fg, bg);
156  s->x += FONT_WIDTH;
157  if (s->x >= avctx->width) {
158  s->x = 0;
159  hscroll(avctx);
160  }
161 }
162 
167 static int execute_code(AVCodecContext * avctx, int c)
168 {
169  AnsiContext *s = avctx->priv_data;
170  int ret, i;
171  int width = 0;
172  int height = 0;
173 
174  switch(c) {
175  case 'A': //Cursor Up
176  s->y = FFMAX(s->y - (s->nb_args > 0 ? s->args[0]*s->font_height : s->font_height), 0);
177  break;
178  case 'B': //Cursor Down
179  s->y = FFMIN(s->y + (s->nb_args > 0 ? s->args[0]*s->font_height : s->font_height), avctx->height - s->font_height);
180  break;
181  case 'C': //Cursor Right
182  s->x = FFMIN(s->x + (s->nb_args > 0 ? s->args[0]*FONT_WIDTH : FONT_WIDTH), avctx->width - FONT_WIDTH);
183  break;
184  case 'D': //Cursor Left
185  s->x = FFMAX(s->x - (s->nb_args > 0 ? s->args[0]*FONT_WIDTH : FONT_WIDTH), 0);
186  break;
187  case 'H': //Cursor Position
188  case 'f': //Horizontal and Vertical Position
189  s->y = s->nb_args > 0 ? av_clip((s->args[0] - 1)*s->font_height, 0, avctx->height - s->font_height) : 0;
190  s->x = s->nb_args > 1 ? av_clip((s->args[1] - 1)*FONT_WIDTH, 0, avctx->width - FONT_WIDTH) : 0;
191  break;
192  case 'h': //set creen mode
193  case 'l': //reset screen mode
194  if (s->nb_args < 2)
195  s->args[0] = DEFAULT_SCREEN_MODE;
196  switch(s->args[0]) {
197  case 0: case 1: case 4: case 5: case 13: case 19: //320x200 (25 rows)
198  s->font = ff_cga_font;
199  s->font_height = 8;
200  width = 40<<3;
201  height = 25<<3;
202  break;
203  case 2: case 3: //640x400 (25 rows)
204  s->font = ff_vga16_font;
205  s->font_height = 16;
206  width = 80<<3;
207  height = 25<<4;
208  break;
209  case 6: case 14: //640x200 (25 rows)
210  s->font = ff_cga_font;
211  s->font_height = 8;
212  width = 80<<3;
213  height = 25<<3;
214  break;
215  case 7: //set line wrapping
216  break;
217  case 15: case 16: //640x350 (43 rows)
218  s->font = ff_cga_font;
219  s->font_height = 8;
220  width = 80<<3;
221  height = 43<<3;
222  break;
223  case 17: case 18: //640x480 (60 rows)
224  s->font = ff_cga_font;
225  s->font_height = 8;
226  width = 80<<3;
227  height = 60<<4;
228  break;
229  default:
230  avpriv_request_sample(avctx, "Unsupported screen mode");
231  }
232  if (width != 0 && height != 0 &&
233  (width != avctx->width || height != avctx->height)) {
234  av_frame_unref(s->frame);
235  ret = ff_set_dimensions(avctx, width, height);
236  if (ret < 0)
237  return ret;
238  ret = ff_get_buffer(avctx, s->frame, AV_GET_BUFFER_FLAG_REF);
239  if (ret < 0) {
240  av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
241  return ret;
242  }
244  s->frame->palette_has_changed = 1;
245  memcpy(s->frame->data[1], ff_cga_palette, 16 * 4);
246  erase_screen(avctx);
247  } else if (c == 'l') {
248  erase_screen(avctx);
249  }
250  break;
251  case 'J': //Erase in Page
252  switch (s->args[0]) {
253  case 0:
254  erase_line(avctx, s->x, avctx->width - s->x);
255  if (s->y < avctx->height - s->font_height)
256  memset(s->frame->data[0] + (s->y + s->font_height)*s->frame->linesize[0],
257  DEFAULT_BG_COLOR, (avctx->height - s->y - s->font_height)*s->frame->linesize[0]);
258  break;
259  case 1:
260  erase_line(avctx, 0, s->x);
261  if (s->y > 0)
262  memset(s->frame->data[0], DEFAULT_BG_COLOR, s->y * s->frame->linesize[0]);
263  break;
264  case 2:
265  erase_screen(avctx);
266  }
267  break;
268  case 'K': //Erase in Line
269  switch(s->args[0]) {
270  case 0:
271  erase_line(avctx, s->x, avctx->width - s->x);
272  break;
273  case 1:
274  erase_line(avctx, 0, s->x);
275  break;
276  case 2:
277  erase_line(avctx, 0, avctx->width);
278  }
279  break;
280  case 'm': //Select Graphics Rendition
281  if (s->nb_args == 0) {
282  s->nb_args = 1;
283  s->args[0] = 0;
284  }
285  for (i = 0; i < FFMIN(s->nb_args, MAX_NB_ARGS); i++) {
286  int m = s->args[i];
287  if (m == 0) {
288  s->attributes = 0;
289  s->fg = DEFAULT_FG_COLOR;
290  s->bg = DEFAULT_BG_COLOR;
291  } else if (m == 1 || m == 2 || m == 4 || m == 5 || m == 7 || m == 8) {
292  s->attributes |= 1 << (m - 1);
293  } else if (m >= 30 && m <= 38) {
294  s->fg = ansi_to_cga[m - 30];
295  } else if (m == 39) {
297  } else if (m >= 40 && m <= 47) {
298  s->bg = ansi_to_cga[m - 40];
299  } else if (m == 49) {
301  } else {
302  avpriv_request_sample(avctx, "Unsupported rendition parameter");
303  }
304  }
305  break;
306  case 'n': //Device Status Report
307  case 'R': //report current line and column
308  /* ignore */
309  break;
310  case 's': //Save Cursor Position
311  s->sx = s->x;
312  s->sy = s->y;
313  break;
314  case 'u': //Restore Cursor Position
315  s->x = av_clip(s->sx, 0, avctx->width - FONT_WIDTH);
316  s->y = av_clip(s->sy, 0, avctx->height - s->font_height);
317  break;
318  default:
319  avpriv_request_sample(avctx, "Unknown escape code");
320  break;
321  }
322  return 0;
323 }
324 
325 static int decode_frame(AVCodecContext *avctx,
326  void *data, int *got_frame,
327  AVPacket *avpkt)
328 {
329  AnsiContext *s = avctx->priv_data;
330  uint8_t *buf = avpkt->data;
331  int buf_size = avpkt->size;
332  const uint8_t *buf_end = buf+buf_size;
333  int ret, i, count;
334 
335  ret = ff_reget_buffer(avctx, s->frame);
336  if (ret < 0){
337  av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
338  return ret;
339  }
340  if (!avctx->frame_number) {
341  memset(s->frame->data[0], 0, avctx->height * FFABS(s->frame->linesize[0]));
342  memset(s->frame->data[1], 0, AVPALETTE_SIZE);
343  }
344 
346  s->frame->palette_has_changed = 1;
347  memcpy(s->frame->data[1], ff_cga_palette, 16 * 4);
348 
349  while(buf < buf_end) {
350  switch(s->state) {
351  case STATE_NORMAL:
352  switch (buf[0]) {
353  case 0x00: //NUL
354  case 0x07: //BEL
355  case 0x1A: //SUB
356  /* ignore */
357  break;
358  case 0x08: //BS
359  s->x = FFMAX(s->x - 1, 0);
360  break;
361  case 0x09: //HT
362  i = s->x / FONT_WIDTH;
363  count = ((i + 8) & ~7) - i;
364  for (i = 0; i < count; i++)
365  draw_char(avctx, ' ');
366  break;
367  case 0x0A: //LF
368  hscroll(avctx);
369  case 0x0D: //CR
370  s->x = 0;
371  break;
372  case 0x0C: //FF
373  erase_screen(avctx);
374  break;
375  case 0x1B: //ESC
376  s->state = STATE_ESCAPE;
377  break;
378  default:
379  draw_char(avctx, buf[0]);
380  }
381  break;
382  case STATE_ESCAPE:
383  if (buf[0] == '[') {
384  s->state = STATE_CODE;
385  s->nb_args = 0;
386  s->args[0] = 0;
387  } else {
388  s->state = STATE_NORMAL;
389  draw_char(avctx, 0x1B);
390  continue;
391  }
392  break;
393  case STATE_CODE:
394  switch(buf[0]) {
395  case '0': case '1': case '2': case '3': case '4':
396  case '5': case '6': case '7': case '8': case '9':
397  if (s->nb_args < MAX_NB_ARGS)
398  s->args[s->nb_args] = s->args[s->nb_args] * 10 + buf[0] - '0';
399  break;
400  case ';':
401  s->nb_args++;
402  if (s->nb_args < MAX_NB_ARGS)
403  s->args[s->nb_args] = 0;
404  break;
405  case 'M':
406  s->state = STATE_MUSIC_PREAMBLE;
407  break;
408  case '=': case '?':
409  /* ignore */
410  break;
411  default:
412  if (s->nb_args > MAX_NB_ARGS)
413  av_log(avctx, AV_LOG_WARNING, "args overflow (%i)\n", s->nb_args);
414  if (s->nb_args < MAX_NB_ARGS && s->args[s->nb_args])
415  s->nb_args++;
416  if ((ret = execute_code(avctx, buf[0])) < 0)
417  return ret;
418  s->state = STATE_NORMAL;
419  }
420  break;
421  case STATE_MUSIC_PREAMBLE:
422  if (buf[0] == 0x0E || buf[0] == 0x1B)
423  s->state = STATE_NORMAL;
424  /* ignore music data */
425  break;
426  }
427  buf++;
428  }
429 
430  *got_frame = 1;
431  if ((ret = av_frame_ref(data, s->frame)) < 0)
432  return ret;
433  return buf_size;
434 }
435 
437 {
438  AnsiContext *s = avctx->priv_data;
439 
440  av_frame_free(&s->frame);
441  return 0;
442 }
443 
445  .name = "ansi",
446  .long_name = NULL_IF_CONFIG_SMALL("ASCII/ANSI art"),
447  .type = AVMEDIA_TYPE_VIDEO,
448  .id = AV_CODEC_ID_ANSI,
449  .priv_data_size = sizeof(AnsiContext),
450  .init = decode_init,
451  .close = decode_close,
452  .decode = decode_frame,
453  .capabilities = CODEC_CAP_DR1,
454 };
#define AVPALETTE_SIZE
Definition: avcodec.h:3051
static void draw_char(AVCodecContext *avctx, int c)
Draw character to screen.
Definition: ansi.c:140
#define FONT_WIDTH
Font width.
Definition: ansi.c:45
const uint8_t ff_cga_font[2048]
Definition: cga_data.c:29
int x
x cursor position (pixels)
Definition: ansi.c:54
#define ATTR_REVERSE
Reverse (mode 7)
Definition: ansi.c:38
This structure describes decoded (raw) audio or video data.
Definition: frame.h:135
int bg
background color
Definition: ansi.c:62
#define ATTR_CONCEALED
Concealed (mode 8)
Definition: ansi.c:39
#define AV_LOG_WARNING
Something somehow does not look correct.
Definition: log.h:129
int ff_set_dimensions(AVCodecContext *s, int width, int height)
Check that the provided frame dimensions are valid and set them on the codec context.
Definition: utils.c:133
int nb_args
number of arguments (may exceed MAX_NB_ARGS)
Definition: ansi.c:73
int size
Definition: avcodec.h:974
enum AVPixelFormat pix_fmt
Pixel format, see AV_PIX_FMT_xxx.
Definition: avcodec.h:1270
#define DEFAULT_SCREEN_MODE
80x25
Definition: ansi.c:43
const uint8_t ff_vga16_font[4096]
Definition: cga_data.c:160
AVCodec.
Definition: avcodec.h:2812
static void erase_screen(AVCodecContext *avctx)
Definition: ansi.c:128
static int execute_code(AVCodecContext *avctx, int c)
Execute ANSI escape code.
Definition: ansi.c:167
static void hscroll(AVCodecContext *avctx)
Definition: ansi.c:99
enum AnsiContext::@10 state
static int decode(MimicContext *ctx, int quality, int num_coeffs, int is_iframe)
Definition: mimic.c:275
void void avpriv_request_sample(void *avc, const char *msg,...) av_printf_format(2
Log a generic warning message about a missing feature.
uint8_t
#define av_cold
Definition: attributes.h:66
AVFrame * av_frame_alloc(void)
Allocate an AVFrame and set its fields to default values.
Definition: frame.c:57
8 bit with PIX_FMT_RGB32 palette
Definition: pixfmt.h:76
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:188
#define CODEC_CAP_DR1
Codec uses get_buffer() for allocating buffers and supports custom allocators.
Definition: avcodec.h:684
const char data[16]
Definition: mxf.c:70
uint8_t * data
Definition: avcodec.h:973
CGA/EGA/VGA ROM data.
#define DEFAULT_BG_COLOR
Definition: ansi.c:42
static av_cold int decode_close(AVCodecContext *avctx)
Definition: ansi.c:436
const uint8_t * font
font
Definition: ansi.c:58
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:123
int sy
saved y cursor position (pixels)
Definition: ansi.c:57
#define AVERROR(e)
Definition: error.h:43
void av_frame_free(AVFrame **frame)
Free the frame and any dynamically allocated objects in it, e.g.
Definition: frame.c:69
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
Definition: internal.h:145
static void erase_line(AVCodecContext *avctx, int xoffset, int xlength)
Definition: ansi.c:119
void av_log(void *avcl, int level, const char *fmt,...)
Definition: log.c:169
const char * name
Name of the codec implementation.
Definition: avcodec.h:2819
#define FFMAX(a, b)
Definition: common.h:55
reference-counted frame API
#define ATTR_BLINK
Blink/Bright-background (mode 5)
Definition: ansi.c:37
int ff_reget_buffer(AVCodecContext *avctx, AVFrame *frame)
Identical in function to av_frame_make_writable(), except it uses ff_get_buffer() to allocate the buf...
Definition: utils.c:820
enum AVPictureType pict_type
Picture type of the frame.
Definition: frame.h:196
#define FFMIN(a, b)
Definition: common.h:57
int width
picture width / height.
Definition: avcodec.h:1229
#define FFABS(a)
Definition: common.h:52
static av_cold int decode_init(AVCodecContext *avctx)
Definition: ansi.c:76
#define ATTR_BOLD
Bold/Bright-foreground (mode 1)
Definition: ansi.c:34
void ff_draw_pc_font(uint8_t *dst, int linesize, const uint8_t *font, int font_height, int ch, int fg, int bg)
Draw CGA/EGA/VGA font to 8-bit pixel buffer.
Definition: cga_data.c:435
static int width
Definition: utils.c:156
Libavcodec external API header.
int font_height
font height
Definition: ansi.c:59
int args[MAX_NB_ARGS]
Definition: ansi.c:72
int linesize[AV_NUM_DATA_POINTERS]
For video, size in bytes of each picture line.
Definition: frame.h:153
main external API structure.
Definition: avcodec.h:1050
static void close(AVCodecParserContext *s)
Definition: h264_parser.c:490
int ff_get_buffer(AVCodecContext *avctx, AVFrame *frame, int flags)
Get a buffer for a frame.
Definition: utils.c:612
int palette_has_changed
Tell user application that palette has changed from previous frame.
Definition: frame.h:330
int fg
foreground color
Definition: ansi.c:61
static const uint8_t ansi_to_cga[16]
map ansi color index to cga palette index
Definition: ansi.c:48
const uint32_t ff_cga_palette[16]
Definition: cga_data.c:419
void av_frame_unref(AVFrame *frame)
Unreference all the buffers referenced by frame and reset the frame fields.
Definition: frame.c:283
static uint32_t state
Definition: trasher.c:27
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
Definition: frame.h:141
int height
Definition: gxfenc.c:72
int sx
saved x cursor position (pixels)
Definition: ansi.c:56
common internal api header.
common internal and external API header
AVFrame * frame
Definition: ansi.c:53
int y
y cursor position (pixels)
Definition: ansi.c:55
static int decode_frame(AVCodecContext *avctx, void *data, int *got_frame, AVPacket *avpkt)
Definition: ansi.c:325
static av_cold int init(AVCodecParserContext *s)
Definition: h264_parser.c:499
void * priv_data
Definition: avcodec.h:1092
int frame_number
Frame counter, set by libavcodec.
Definition: avcodec.h:1838
#define MAX_NB_ARGS
Definition: ansi.c:71
#define DEFAULT_FG_COLOR
CGA color index.
Definition: ansi.c:41
#define FFSWAP(type, a, b)
Definition: common.h:60
int attributes
attribute flags
Definition: ansi.c:60
This structure stores compressed data.
Definition: avcodec.h:950
#define AV_GET_BUFFER_FLAG_REF
The decoder will keep a reference to the frame and may reuse it later.
Definition: avcodec.h:850
AVCodec ff_ansi_decoder
Definition: ansi.c:444