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