Skip to content

Instantly share code, notes, and snippets.

@adl1995
Created March 11, 2017 19:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save adl1995/6260b8a88c7bea49393be34b801635bc to your computer and use it in GitHub Desktop.
Save adl1995/6260b8a88c7bea49393be34b801635bc to your computer and use it in GitHub Desktop.
/*
* XPM image format
*
* Copyright (c) 2012 Paul B Mahol
*
* This file is part of FFmpeg.
*
* FFmpeg is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* FFmpeg is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with FFmpeg; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include "libavutil/parseutils.h"
#include "avcodec.h"
#include "internal.h"
typedef struct XPMContext {
AVFrame *frame;
uint8_t *pixels;
int pixels_size;
} XPMDecContext;
static av_cold int xpm_decode_init(AVCodecContext *avctx)
{
XPMDecContext *x = avctx->priv_data;
avctx->coded_frame = &x->frame;
return 0;
}
static int ascii2index(const uint8_t *cpixel, int cpp)
{
const uint8_t *p = cpixel;
int n = 0, m = 1, i;
for (i = 0; i < cpp; i++) {
if (*p < ' ' || *p > '~')
return AVERROR_INVALIDDATA;
n += (*p++ - ' ') * m;
m *= 94;
}
return n;
}
static size_t skip_chars_comments(uint8_t **cpixel, char *delimiter)
{
int len;
len = strcspn(*cpixel, delimiter) + 1;
if (strcspn(*cpixel, "//") != 0) {
*cpixel += strcspn(*cpixel, "//");
if (strcspn(*cpixel, "\n") != 0) {
*cpixel += strcspn(*cpixel, "\n");
}
}
if (strcspn(*cpixel, "/*") != 0) {
*cpixel += strcspn(*cpixel, "/*");
if (strcspn(*cpixel, "*/") != 0) {
*cpixel += strcspn(*cpixel, "*/");
}
}
return len;
}
static int parse_str_int(const uint8_t *p, int len, const uint8_t *key)
{
const uint8_t *end = p + len;
for(; p<end - strlen(key); p++) {
if (!memcmp(p, key, strlen(key)))
break;
}
p += strlen(key);
if (p >= end)
return INT_MIN;
for(; p<end; p++) {
char *eptr;
int64_t ret = strtol(p, &eptr, 10);
if ((const uint8_t *)eptr != p)
return ret;
}
return INT_MIN;
}
static int xpm_decode_frame(AVCodecContext *avctx, void *data,
int *data_size, AVPacket *avpkt)
{
AVFrame *p = data;
XPMDecContext *x = avctx->priv_data;
const uint8_t *end, *ptr = avpkt->data;
int ncolors, cpp, ret, i, j, k, t;
int size;
uint8_t *dst, rgba[4];
end = avpkt->data + avpkt->size;
if (memcmp(ptr, "/* XPM */", 9)) {
av_log(avctx, AV_LOG_ERROR, "missing signature\n");
return AVERROR_INVALIDDATA;
}
ptr += strcspn(ptr, "\"");
if (sscanf(ptr, "\"%u %u %u %u\",",
&avctx->width, &avctx->height, &ncolors, &cpp) != 4) {
av_log(avctx, AV_LOG_ERROR, "missing image parameters\n");
return AVERROR_INVALIDDATA;
}
if (ncolors <= 0 || cpp <= 0) {
av_log(avctx, AV_LOG_ERROR, "invalid number of colors or chars per pixel\n");
return AVERROR_INVALIDDATA;
}
if (cpp != 1 && cpp != 2 && cpp != 3) {
av_log(avctx, AV_LOG_ERROR, "unsupported number of chars per pixel\n");
return AVERROR_PATCHWELCOME;
}
avctx->pix_fmt = AV_PIX_FMT_RGBA;
if ((ret = ff_get_buffer(avctx, p, 0)) < 0)
return ret;
size = 1;
for (j = 0; j < cpp; ++j)
{
t = 94;
for (k = j; k > 0; --k)
t *= t;
size += t;
}
av_fast_padded_malloc(&x->pixels, &x->pixels_size, size);
ptr += skip_chars_comments(&ptr, ",");
for (i = 0; i < ncolors; i++) {
const uint8_t *index;
int len;
ptr += skip_chars_comments(&ptr, "\"");
index = ptr;
ptr += cpp;
ptr = strstr(ptr, "c");
if (ptr)
ptr += 2;
else
return AVERROR_INVALIDDATA;
len = strcspn(ptr, "\"");
if ((ret = av_parse_color(rgba, ptr, len, avctx)) < 0)
return ret;
(p->data[0][i * 4 ]) = rgba[0];
(p->data[0][i * 4 + 1]) = rgba[1];
(p->data[0][i * 4 + 2]) = rgba[2];
(p->data[0][i * 4 + 3]) = rgba[3];
if ((ret = ascii2index(index, cpp)) < 0)
return ret;
x->pixels[ret] = i;
ptr += skip_chars_comments(&ptr, ",");
}
for (i = 0; i < avctx->height; i++) {
dst = p->data[0] + i * p->linesize[0];
ptr += skip_chars_comments(&ptr, "\"");
for (j = 0; j < avctx->width; j++) {
if (ptr + cpp > end) {
return AVERROR_INVALIDDATA;
}
*dst++ = ascii2index(ptr, cpp);
ptr += cpp;
}
ptr += skip_chars_comments(&ptr, ",");
}
p->key_frame = 1;
p->pict_type = AV_PICTURE_TYPE_I;
*data_size = sizeof(AVFrame);
return avpkt->size;
}
static av_cold int xpm_decode_close(AVCodecContext *avctx)
{
XPMDecContext *x = avctx->priv_data;
av_freep(&x->pixels);
return 0;
}
AVCodec ff_xpm_decoder = {
.name = "xpm",
.type = AVMEDIA_TYPE_VIDEO,
.id = AV_CODEC_ID_XPM,
.priv_data_size = sizeof(XPMDecContext),
.init = xpm_decode_init,
.close = xpm_decode_close,
.decode = xpm_decode_frame,
.capabilities = CODEC_CAP_DR1,
.long_name = NULL_IF_CONFIG_SMALL("XPM (X PixMap) image"),
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment