4 * This file is part of Libav.
6 * Libav is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * Libav is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with Libav; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21 #include "libavutil/avstring.h"
27 static int xbm_decode_frame(AVCodecContext *avctx, void *data,
28 int *got_frame, AVPacket *avpkt)
34 const uint8_t *ptr = avpkt->data;
37 avctx->pix_fmt = AV_PIX_FMT_MONOWHITE;
38 while (!width || !height) {
39 ptr += strcspn(ptr, "#");
40 if (ptr >= avpkt->data + avpkt->size) {
41 av_log(avctx, AV_LOG_ERROR, "End of file reached.\n");
42 return AVERROR_INVALIDDATA;
44 if (strncmp(ptr, "#define", 7) != 0) {
45 av_log(avctx, AV_LOG_ERROR,
46 "Unexpected preprocessor directive.\n");
47 return AVERROR_INVALIDDATA;
50 ptr += strcspn(ptr, "_") + 1;
51 // get width or height
52 if (strncmp(ptr, "width", 5) == 0) {
53 ptr += strcspn(ptr, " ");
54 width = strtol(ptr, NULL, 10);
55 } else if (strncmp(ptr, "height", 6) == 0) {
56 ptr += strcspn(ptr, " ");
57 height = strtol(ptr, NULL, 10);
59 // skip offset and unknown variables
60 av_log(avctx, AV_LOG_VERBOSE,
61 "Ignoring preprocessor directive.\n");
65 if ((ret = ff_set_dimensions(avctx, width, height)) < 0)
68 if ((ret = ff_get_buffer(avctx, p, 0)) < 0)
71 // go to start of image data
72 ptr += strcspn(ptr, "{");
74 linesize = (avctx->width + 7) / 8;
75 for (i = 0; i < avctx->height; i++) {
77 dst = p->data[0] + i * p->linesize[0];
78 if (ptr >= avpkt->data + avpkt->size) {
79 av_log(avctx, AV_LOG_ERROR, "End of file reached.\n");
80 return AVERROR_INVALIDDATA;
86 ptr += strcspn(ptr, "x") - 1; // -1 to get 0x
87 val = strtol(ptr, (char **)&endptr, 16);
89 if (endptr - ptr == 4) {
91 *dst++ = ff_reverse[val];
93 } else if (endptr - ptr == 6) {
95 *dst++ = ff_reverse[val >> 8];
96 *dst++ = ff_reverse[val & 0xFF];
97 eol = linesize / 2; // 2 bytes read
99 av_log(avctx, AV_LOG_ERROR,
100 "Unexpected data at %.8s.\n", ptr);
101 return AVERROR_INVALIDDATA;
108 p->pict_type = AV_PICTURE_TYPE_I;
115 AVCodec ff_xbm_decoder = {
117 .long_name = NULL_IF_CONFIG_SMALL("XBM (X BitMap) image"),
118 .type = AVMEDIA_TYPE_VIDEO,
119 .id = AV_CODEC_ID_XBM,
120 .decode = xbm_decode_frame,
121 .capabilities = CODEC_CAP_DR1,