LCOV - code coverage report
Current view: top level - libavcodec - xpmdec.c (source / functions) Hit Total Coverage
Test: coverage.info Lines: 0 157 0.0 %
Date: 2017-12-15 11:05:35 Functions: 0 7 0.0 %

          Line data    Source code
       1             : /*
       2             :  * XPM image format
       3             :  *
       4             :  * Copyright (c) 2012 Paul B Mahol
       5             :  * Copyright (c) 2017 Paras Chadha
       6             :  *
       7             :  * This file is part of FFmpeg.
       8             :  *
       9             :  * FFmpeg is free software; you can redistribute it and/or
      10             :  * modify it under the terms of the GNU Lesser General Public
      11             :  * License as published by the Free Software Foundation; either
      12             :  * version 2.1 of the License, or (at your option) any later version.
      13             :  *
      14             :  * FFmpeg is distributed in the hope that it will be useful,
      15             :  * but WITHOUT ANY WARRANTY; without even the implied warranty of
      16             :  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
      17             :  * Lesser General Public License for more details.
      18             :  *
      19             :  * You should have received a copy of the GNU Lesser General Public
      20             :  * License along with FFmpeg; if not, write to the Free Software
      21             :  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
      22             :  */
      23             : 
      24             : #include "libavutil/parseutils.h"
      25             : #include "libavutil/avstring.h"
      26             : #include "avcodec.h"
      27             : #include "internal.h"
      28             : 
      29             : typedef struct XPMContext {
      30             :     uint32_t  *pixels;
      31             :     int        pixels_size;
      32             :     uint8_t   *buf;
      33             :     int        buf_size;
      34             : } XPMDecContext;
      35             : 
      36             : typedef struct ColorEntry {
      37             :     const char *name;         ///< a string representing the name of the color
      38             :     uint32_t    rgb_color;    ///< RGB values for the color
      39             : } ColorEntry;
      40             : 
      41           0 : static int color_table_compare(const void *lhs, const void *rhs)
      42             : {
      43           0 :     return av_strcasecmp(lhs, ((const ColorEntry *)rhs)->name);
      44             : }
      45             : 
      46             : static const ColorEntry color_table[] = {
      47             :     { "AliceBlue",            0xFFF0F8FF },
      48             :     { "AntiqueWhite",         0xFFFAEBD7 },
      49             :     { "Aqua",                 0xFF00FFFF },
      50             :     { "Aquamarine",           0xFF7FFFD4 },
      51             :     { "Azure",                0xFFF0FFFF },
      52             :     { "Beige",                0xFFF5F5DC },
      53             :     { "Bisque",               0xFFFFE4C4 },
      54             :     { "Black",                0xFF000000 },
      55             :     { "BlanchedAlmond",       0xFFFFEBCD },
      56             :     { "Blue",                 0xFF0000FF },
      57             :     { "BlueViolet",           0xFF8A2BE2 },
      58             :     { "Brown",                0xFFA52A2A },
      59             :     { "BurlyWood",            0xFFDEB887 },
      60             :     { "CadetBlue",            0xFF5F9EA0 },
      61             :     { "Chartreuse",           0xFF7FFF00 },
      62             :     { "Chocolate",            0xFFD2691E },
      63             :     { "Coral",                0xFFFF7F50 },
      64             :     { "CornflowerBlue",       0xFF6495ED },
      65             :     { "Cornsilk",             0xFFFFF8DC },
      66             :     { "Crimson",              0xFFDC143C },
      67             :     { "Cyan",                 0xFF00FFFF },
      68             :     { "DarkBlue",             0xFF00008B },
      69             :     { "DarkCyan",             0xFF008B8B },
      70             :     { "DarkGoldenRod",        0xFFB8860B },
      71             :     { "DarkGray",             0xFFA9A9A9 },
      72             :     { "DarkGreen",            0xFF006400 },
      73             :     { "DarkKhaki",            0xFFBDB76B },
      74             :     { "DarkMagenta",          0xFF8B008B },
      75             :     { "DarkOliveGreen",       0xFF556B2F },
      76             :     { "Darkorange",           0xFFFF8C00 },
      77             :     { "DarkOrchid",           0xFF9932CC },
      78             :     { "DarkRed",              0xFF8B0000 },
      79             :     { "DarkSalmon",           0xFFE9967A },
      80             :     { "DarkSeaGreen",         0xFF8FBC8F },
      81             :     { "DarkSlateBlue",        0xFF483D8B },
      82             :     { "DarkSlateGray",        0xFF2F4F4F },
      83             :     { "DarkTurquoise",        0xFF00CED1 },
      84             :     { "DarkViolet",           0xFF9400D3 },
      85             :     { "DeepPink",             0xFFFF1493 },
      86             :     { "DeepSkyBlue",          0xFF00BFFF },
      87             :     { "DimGray",              0xFF696969 },
      88             :     { "DodgerBlue",           0xFF1E90FF },
      89             :     { "FireBrick",            0xFFB22222 },
      90             :     { "FloralWhite",          0xFFFFFAF0 },
      91             :     { "ForestGreen",          0xFF228B22 },
      92             :     { "Fuchsia",              0xFFFF00FF },
      93             :     { "Gainsboro",            0xFFDCDCDC },
      94             :     { "GhostWhite",           0xFFF8F8FF },
      95             :     { "Gold",                 0xFFFFD700 },
      96             :     { "GoldenRod",            0xFFDAA520 },
      97             :     { "Gray",                 0xFFBEBEBE },
      98             :     { "Green",                0xFF00FF00 },
      99             :     { "GreenYellow",          0xFFADFF2F },
     100             :     { "HoneyDew",             0xFFF0FFF0 },
     101             :     { "HotPink",              0xFFFF69B4 },
     102             :     { "IndianRed",            0xFFCD5C5C },
     103             :     { "Indigo",               0xFF4B0082 },
     104             :     { "Ivory",                0xFFFFFFF0 },
     105             :     { "Khaki",                0xFFF0E68C },
     106             :     { "Lavender",             0xFFE6E6FA },
     107             :     { "LavenderBlush",        0xFFFFF0F5 },
     108             :     { "LawnGreen",            0xFF7CFC00 },
     109             :     { "LemonChiffon",         0xFFFFFACD },
     110             :     { "LightBlue",            0xFFADD8E6 },
     111             :     { "LightCoral",           0xFFF08080 },
     112             :     { "LightCyan",            0xFFE0FFFF },
     113             :     { "LightGoldenRodYellow", 0xFFFAFAD2 },
     114             :     { "LightGreen",           0xFF90EE90 },
     115             :     { "LightGrey",            0xFFD3D3D3 },
     116             :     { "LightPink",            0xFFFFB6C1 },
     117             :     { "LightSalmon",          0xFFFFA07A },
     118             :     { "LightSeaGreen",        0xFF20B2AA },
     119             :     { "LightSkyBlue",         0xFF87CEFA },
     120             :     { "LightSlateGray",       0xFF778899 },
     121             :     { "LightSteelBlue",       0xFFB0C4DE },
     122             :     { "LightYellow",          0xFFFFFFE0 },
     123             :     { "Lime",                 0xFF00FF00 },
     124             :     { "LimeGreen",            0xFF32CD32 },
     125             :     { "Linen",                0xFFFAF0E6 },
     126             :     { "Magenta",              0xFFFF00FF },
     127             :     { "Maroon",               0xFFB03060 },
     128             :     { "MediumAquaMarine",     0xFF66CDAA },
     129             :     { "MediumBlue",           0xFF0000CD },
     130             :     { "MediumOrchid",         0xFFBA55D3 },
     131             :     { "MediumPurple",         0xFF9370D8 },
     132             :     { "MediumSeaGreen",       0xFF3CB371 },
     133             :     { "MediumSlateBlue",      0xFF7B68EE },
     134             :     { "MediumSpringGreen",    0xFF00FA9A },
     135             :     { "MediumTurquoise",      0xFF48D1CC },
     136             :     { "MediumVioletRed",      0xFFC71585 },
     137             :     { "MidnightBlue",         0xFF191970 },
     138             :     { "MintCream",            0xFFF5FFFA },
     139             :     { "MistyRose",            0xFFFFE4E1 },
     140             :     { "Moccasin",             0xFFFFE4B5 },
     141             :     { "NavajoWhite",          0xFFFFDEAD },
     142             :     { "Navy",                 0xFF000080 },
     143             :     { "None",                 0x00000000 },
     144             :     { "OldLace",              0xFFFDF5E6 },
     145             :     { "Olive",                0xFF808000 },
     146             :     { "OliveDrab",            0xFF6B8E23 },
     147             :     { "Orange",               0xFFFFA500 },
     148             :     { "OrangeRed",            0xFFFF4500 },
     149             :     { "Orchid",               0xFFDA70D6 },
     150             :     { "PaleGoldenRod",        0xFFEEE8AA },
     151             :     { "PaleGreen",            0xFF98FB98 },
     152             :     { "PaleTurquoise",        0xFFAFEEEE },
     153             :     { "PaleVioletRed",        0xFFD87093 },
     154             :     { "PapayaWhip",           0xFFFFEFD5 },
     155             :     { "PeachPuff",            0xFFFFDAB9 },
     156             :     { "Peru",                 0xFFCD853F },
     157             :     { "Pink",                 0xFFFFC0CB },
     158             :     { "Plum",                 0xFFDDA0DD },
     159             :     { "PowderBlue",           0xFFB0E0E6 },
     160             :     { "Purple",               0xFFA020F0 },
     161             :     { "Red",                  0xFFFF0000 },
     162             :     { "RosyBrown",            0xFFBC8F8F },
     163             :     { "RoyalBlue",            0xFF4169E1 },
     164             :     { "SaddleBrown",          0xFF8B4513 },
     165             :     { "Salmon",               0xFFFA8072 },
     166             :     { "SandyBrown",           0xFFF4A460 },
     167             :     { "SeaGreen",             0xFF2E8B57 },
     168             :     { "SeaShell",             0xFFFFF5EE },
     169             :     { "Sienna",               0xFFA0522D },
     170             :     { "Silver",               0xFFC0C0C0 },
     171             :     { "SkyBlue",              0xFF87CEEB },
     172             :     { "SlateBlue",            0xFF6A5ACD },
     173             :     { "SlateGray",            0xFF708090 },
     174             :     { "Snow",                 0xFFFFFAFA },
     175             :     { "SpringGreen",          0xFF00FF7F },
     176             :     { "SteelBlue",            0xFF4682B4 },
     177             :     { "Tan",                  0xFFD2B48C },
     178             :     { "Teal",                 0xFF008080 },
     179             :     { "Thistle",              0xFFD8BFD8 },
     180             :     { "Tomato",               0xFFFF6347 },
     181             :     { "Turquoise",            0xFF40E0D0 },
     182             :     { "Violet",               0xFFEE82EE },
     183             :     { "Wheat",                0xFFF5DEB3 },
     184             :     { "White",                0xFFFFFFFF },
     185             :     { "WhiteSmoke",           0xFFF5F5F5 },
     186             :     { "Yellow",               0xFFFFFF00 },
     187             :     { "YellowGreen",          0xFF9ACD32 }
     188             : };
     189             : 
     190           0 : static unsigned hex_char_to_number(uint8_t x)
     191             : {
     192           0 :     if (x >= 'a' && x <= 'f')
     193           0 :         x -= 'a' - 10;
     194           0 :     else if (x >= 'A' && x <= 'F')
     195           0 :         x -= 'A' - 10;
     196           0 :     else if (x >= '0' && x <= '9')
     197           0 :         x -= '0';
     198             :     else
     199           0 :         x = 0;
     200           0 :     return x;
     201             : }
     202             : 
     203             : /*
     204             :  * Function same as strcspn but ignores characters if they are inside a C style comments
     205             :  */
     206           0 : static size_t mod_strcspn(const char *string, const char *reject)
     207             : {
     208             :     int i, j;
     209             : 
     210           0 :     for (i = 0; string && string[i]; i++) {
     211           0 :         if (string[i] == '/' && string[i+1] == '*') {
     212           0 :             i += 2;
     213           0 :             while ( string && string[i] && (string[i] != '*' || string[i+1] != '/') )
     214           0 :                 i++;
     215           0 :             i++;
     216           0 :         } else if (string[i] == '/' && string[i+1] == '/') {
     217           0 :             i += 2;
     218           0 :             while ( string && string[i] && string[i] != '\n' )
     219           0 :                 i++;
     220             :         } else {
     221           0 :             for (j = 0; reject && reject[j]; j++) {
     222           0 :                 if (string[i] == reject[j])
     223           0 :                     break;
     224             :             }
     225           0 :             if (reject && reject[j])
     226           0 :                 break;
     227             :         }
     228             :     }
     229           0 :     return i;
     230             : }
     231             : 
     232           0 : static uint32_t color_string_to_rgba(const char *p, int len)
     233             : {
     234           0 :     uint32_t ret = 0xFF000000;
     235             :     const ColorEntry *entry;
     236             :     char color_name[100];
     237             : 
     238           0 :     len = FFMIN(FFMAX(len, 0), sizeof(color_name) - 1);
     239             : 
     240           0 :     if (*p == '#') {
     241           0 :         p++;
     242           0 :         len--;
     243           0 :         if (len == 3) {
     244           0 :             ret |= (hex_char_to_number(p[2]) <<  4) |
     245           0 :                    (hex_char_to_number(p[1]) << 12) |
     246           0 :                    (hex_char_to_number(p[0]) << 20);
     247           0 :         } else if (len == 4) {
     248           0 :             ret  = (hex_char_to_number(p[3]) <<  4) |
     249           0 :                    (hex_char_to_number(p[2]) << 12) |
     250           0 :                    (hex_char_to_number(p[1]) << 20) |
     251           0 :                    (hex_char_to_number(p[0]) << 28);
     252           0 :         } else if (len == 6) {
     253           0 :             ret |=  hex_char_to_number(p[5])        |
     254           0 :                    (hex_char_to_number(p[4]) <<  4) |
     255           0 :                    (hex_char_to_number(p[3]) <<  8) |
     256           0 :                    (hex_char_to_number(p[2]) << 12) |
     257           0 :                    (hex_char_to_number(p[1]) << 16) |
     258           0 :                    (hex_char_to_number(p[0]) << 20);
     259           0 :         } else if (len == 8) {
     260           0 :             ret  =  hex_char_to_number(p[7])        |
     261           0 :                    (hex_char_to_number(p[6]) <<  4) |
     262           0 :                    (hex_char_to_number(p[5]) <<  8) |
     263           0 :                    (hex_char_to_number(p[4]) << 12) |
     264           0 :                    (hex_char_to_number(p[3]) << 16) |
     265           0 :                    (hex_char_to_number(p[2]) << 20) |
     266           0 :                    (hex_char_to_number(p[1]) << 24) |
     267           0 :                    (hex_char_to_number(p[0]) << 28);
     268             :         }
     269             :     } else {
     270           0 :         strncpy(color_name, p, len);
     271           0 :         color_name[len] = '\0';
     272             : 
     273           0 :         entry = bsearch(color_name,
     274             :                         color_table,
     275             :                         FF_ARRAY_ELEMS(color_table),
     276             :                         sizeof(ColorEntry),
     277             :                         color_table_compare);
     278             : 
     279           0 :         if (!entry)
     280           0 :             return ret;
     281             : 
     282           0 :         ret = entry->rgb_color;
     283             :     }
     284           0 :     return ret;
     285             : }
     286             : 
     287           0 : static int ascii2index(const uint8_t *cpixel, int cpp)
     288             : {
     289           0 :     const uint8_t *p = cpixel;
     290           0 :     int n = 0, m = 1, i;
     291             : 
     292           0 :     for (i = 0; i < cpp; i++) {
     293           0 :         if (*p < ' ' || *p > '~')
     294           0 :             return AVERROR_INVALIDDATA;
     295           0 :         n += (*p++ - ' ') * m;
     296           0 :         m *= 95;
     297             :     }
     298           0 :     return n;
     299             : }
     300             : 
     301           0 : static int xpm_decode_frame(AVCodecContext *avctx, void *data,
     302             :                             int *got_frame, AVPacket *avpkt)
     303             : {
     304           0 :     XPMDecContext *x = avctx->priv_data;
     305           0 :     AVFrame *p=data;
     306             :     const uint8_t *end, *ptr;
     307             :     int ncolors, cpp, ret, i, j;
     308             :     int64_t size;
     309             :     uint32_t *dst;
     310             : 
     311           0 :     avctx->pix_fmt = AV_PIX_FMT_BGRA;
     312             : 
     313           0 :     av_fast_padded_malloc(&x->buf, &x->buf_size, avpkt->size);
     314           0 :     if (!x->buf)
     315           0 :         return AVERROR(ENOMEM);
     316           0 :     memcpy(x->buf, avpkt->data, avpkt->size);
     317           0 :     x->buf[avpkt->size] = 0;
     318             : 
     319           0 :     ptr = x->buf;
     320           0 :     end = x->buf + avpkt->size;
     321           0 :     while (end - ptr > 9 && memcmp(ptr, "/* XPM */", 9))
     322           0 :         ptr++;
     323             : 
     324           0 :     if (end - ptr <= 9) {
     325           0 :         av_log(avctx, AV_LOG_ERROR, "missing signature\n");
     326           0 :         return AVERROR_INVALIDDATA;
     327             :     }
     328             : 
     329           0 :     ptr += mod_strcspn(ptr, "\"");
     330           0 :     if (sscanf(ptr, "\"%u %u %u %u\",",
     331             :                &avctx->width, &avctx->height, &ncolors, &cpp) != 4) {
     332           0 :         av_log(avctx, AV_LOG_ERROR, "missing image parameters\n");
     333           0 :         return AVERROR_INVALIDDATA;
     334             :     }
     335             : 
     336           0 :     if ((ret = ff_set_dimensions(avctx, avctx->width, avctx->height)) < 0)
     337           0 :         return ret;
     338             : 
     339           0 :     if ((ret = ff_get_buffer(avctx, p, 0)) < 0)
     340           0 :         return ret;
     341             : 
     342           0 :     if (cpp <= 0 || cpp >= 5) {
     343           0 :         av_log(avctx, AV_LOG_ERROR, "unsupported/invalid number of chars per pixel: %d\n", cpp);
     344           0 :         return AVERROR_INVALIDDATA;
     345             :     }
     346             : 
     347           0 :     size = 1;
     348           0 :     for (i = 0; i < cpp; i++)
     349           0 :         size *= 95;
     350             : 
     351           0 :     if (ncolors <= 0 || ncolors > size) {
     352           0 :         av_log(avctx, AV_LOG_ERROR, "invalid number of colors: %d\n", ncolors);
     353           0 :         return AVERROR_INVALIDDATA;
     354             :     }
     355             : 
     356           0 :     size *= 4;
     357             : 
     358           0 :     av_fast_padded_malloc(&x->pixels, &x->pixels_size, size);
     359           0 :     if (!x->pixels)
     360           0 :         return AVERROR(ENOMEM);
     361             : 
     362           0 :     ptr += mod_strcspn(ptr, ",") + 1;
     363           0 :     if (end - ptr < 1)
     364           0 :         return AVERROR_INVALIDDATA;
     365             : 
     366           0 :     for (i = 0; i < ncolors; i++) {
     367             :         const uint8_t *index;
     368             :         int len;
     369             : 
     370           0 :         ptr += mod_strcspn(ptr, "\"") + 1;
     371           0 :         if (end - ptr < cpp)
     372           0 :             return AVERROR_INVALIDDATA;
     373           0 :         index = ptr;
     374           0 :         ptr += cpp;
     375             : 
     376           0 :         ptr = strstr(ptr, "c ");
     377           0 :         if (ptr) {
     378           0 :             ptr += 2;
     379             :         } else {
     380           0 :             return AVERROR_INVALIDDATA;
     381             :         }
     382             : 
     383           0 :         len = strcspn(ptr, "\" ");
     384             : 
     385           0 :         if ((ret = ascii2index(index, cpp)) < 0)
     386           0 :             return ret;
     387             : 
     388           0 :         x->pixels[ret] = color_string_to_rgba(ptr, len);
     389           0 :         ptr += mod_strcspn(ptr, ",") + 1;
     390           0 :         if (end - ptr < 1)
     391           0 :             return AVERROR_INVALIDDATA;
     392             :     }
     393             : 
     394           0 :     for (i = 0; i < avctx->height; i++) {
     395           0 :         dst = (uint32_t *)(p->data[0] + i * p->linesize[0]);
     396           0 :         if (end - ptr < 1)
     397           0 :             return AVERROR_INVALIDDATA;
     398           0 :         ptr += mod_strcspn(ptr, "\"") + 1;
     399           0 :         if (end - ptr < 1)
     400           0 :             return AVERROR_INVALIDDATA;
     401             : 
     402           0 :         for (j = 0; j < avctx->width; j++) {
     403           0 :             if (end - ptr < cpp)
     404           0 :                 return AVERROR_INVALIDDATA;
     405             : 
     406           0 :             if ((ret = ascii2index(ptr, cpp)) < 0)
     407           0 :                 return ret;
     408             : 
     409           0 :             *dst++ = x->pixels[ret];
     410           0 :             ptr += cpp;
     411             :         }
     412           0 :         ptr += mod_strcspn(ptr, ",") + 1;
     413             :     }
     414             : 
     415           0 :     p->key_frame = 1;
     416           0 :     p->pict_type = AV_PICTURE_TYPE_I;
     417             : 
     418           0 :     *got_frame = 1;
     419             : 
     420           0 :     return avpkt->size;
     421             : }
     422             : 
     423           0 : static av_cold int xpm_decode_close(AVCodecContext *avctx)
     424             : {
     425           0 :     XPMDecContext *x = avctx->priv_data;
     426           0 :     av_freep(&x->pixels);
     427             : 
     428           0 :     av_freep(&x->buf);
     429           0 :     x->buf_size = 0;
     430             : 
     431           0 :     return 0;
     432             : }
     433             : 
     434             : AVCodec ff_xpm_decoder = {
     435             :     .name           = "xpm",
     436             :     .type           = AVMEDIA_TYPE_VIDEO,
     437             :     .id             = AV_CODEC_ID_XPM,
     438             :     .priv_data_size = sizeof(XPMDecContext),
     439             :     .close          = xpm_decode_close,
     440             :     .decode         = xpm_decode_frame,
     441             :     .capabilities   = AV_CODEC_CAP_DR1,
     442             :     .long_name      = NULL_IF_CONFIG_SMALL("XPM (X PixMap) image")
     443             : };

Generated by: LCOV version 1.13