LCOV - code coverage report
Current view: top level - src/libavcodec - ansi.c (source / functions) Hit Total Coverage
Test: coverage.info Lines: 153 269 56.9 %
Date: 2017-05-26 21:11:57 Functions: 8 9 88.9 %

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

Generated by: LCOV version 1.13