4 * Copyright (c) 2012 Paul B Mahol
6 * This file is part of FFmpeg.
8 * FFmpeg is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; either
11 * version 2.1 of the License, or (at your option) any later version.
13 * FFmpeg is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with FFmpeg; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
26 #include "libavutil/avstring.h"
28 static av_cold int xbm_decode_init(AVCodecContext *avctx)
34 static int convert(uint8_t x)
45 static int xbm_decode_frame(AVCodecContext *avctx, void *data,
46 int *got_frame, AVPacket *avpkt)
49 const uint8_t *end, *ptr = avpkt->data;
51 int ret, linesize, i, j;
53 end = avpkt->data + avpkt->size;
54 while (!avctx->width || !avctx->height) {
58 ptr += strcspn(ptr, "#");
59 if (sscanf(ptr, "#define %256s %u", name, &number) != 2) {
60 av_log(avctx, AV_LOG_ERROR, "Unexpected preprocessor directive\n");
61 return AVERROR_INVALIDDATA;
65 if ((len > 6) && !avctx->height && !memcmp(name + len - 7, "_height", 7)) {
66 avctx->height = number;
67 } else if ((len > 5) && !avctx->width && !memcmp(name + len - 6, "_width", 6)) {
68 avctx->width = number;
70 av_log(avctx, AV_LOG_ERROR, "Unknown define '%s'\n", name);
71 return AVERROR_INVALIDDATA;
73 ptr += strcspn(ptr, "\n\r") + 1;
76 avctx->pix_fmt = AV_PIX_FMT_MONOWHITE;
78 if ((ret = ff_get_buffer(avctx, p, 0)) < 0)
81 // goto start of image data
82 ptr += strcspn(ptr, "{") + 1;
84 linesize = (avctx->width + 7) / 8;
85 for (i = 0; i < avctx->height; i++) {
86 dst = p->data[0] + i * p->linesize[0];
87 for (j = 0; j < linesize; j++) {
90 ptr += strcspn(ptr, "x") + 1;
91 if (ptr < end && av_isxdigit(*ptr)) {
94 if (av_isxdigit(*ptr))
95 val = (val << 4) + convert(*ptr);
96 *dst++ = ff_reverse[val];
98 av_log(avctx, AV_LOG_ERROR, "Unexpected data at '%.8s'\n", ptr);
99 return AVERROR_INVALIDDATA;
105 p->pict_type = AV_PICTURE_TYPE_I;
112 static av_cold int xbm_decode_close(AVCodecContext *avctx)
118 AVCodec ff_xbm_decoder = {
120 .type = AVMEDIA_TYPE_VIDEO,
121 .id = AV_CODEC_ID_XBM,
122 .init = xbm_decode_init,
123 .close = xbm_decode_close,
124 .decode = xbm_decode_frame,
125 .capabilities = CODEC_CAP_DR1,
126 .long_name = NULL_IF_CONFIG_SMALL("XBM (X BitMap) image"),