]> git.sesse.net Git - ffmpeg/blob - libavcodec/xbmdec.c
Merge remote-tracking branch 'qatar/master'
[ffmpeg] / libavcodec / xbmdec.c
1 /*
2  * XBM image format
3  *
4  * Copyright (c) 2012 Paul B Mahol
5  *
6  * This file is part of FFmpeg.
7  *
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.
12  *
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.
17  *
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
21  */
22
23 #include "avcodec.h"
24 #include "internal.h"
25
26 static av_cold int xbm_decode_init(AVCodecContext *avctx)
27 {
28     avctx->coded_frame = avcodec_alloc_frame();
29     if (!avctx->coded_frame)
30         return AVERROR(ENOMEM);
31
32     return 0;
33 }
34
35 static int convert(uint8_t x)
36 {
37     if (x >= 'a')
38         x -= 87;
39     else if (x >= 'A')
40         x -= 55;
41     else
42         x -= '0';
43     return x;
44 }
45
46 static int xbm_decode_frame(AVCodecContext *avctx, void *data,
47                             int *data_size, AVPacket *avpkt)
48 {
49     AVFrame *p = avctx->coded_frame;
50     const uint8_t *end, *ptr = avpkt->data;
51     uint8_t *dst;
52     int ret, linesize, i, j;
53
54     end = avpkt->data + avpkt->size;
55     while (!avctx->width || !avctx->height) {
56         char name[256];
57         int number, len;
58
59         ptr += strcspn(ptr, "#");
60         if (sscanf(ptr, "#define %256s %u", name, &number) != 2)
61             return AVERROR_INVALIDDATA;
62
63         len = strlen(name);
64         if ((len > 6) && !avctx->height && !memcmp(name + len - 7, "_height", 7)) {
65                 avctx->height = number;
66         } else if ((len > 5) && !avctx->width && !memcmp(name + len - 6, "_width", 6)) {
67                 avctx->width = number;
68         } else {
69             return AVERROR_INVALIDDATA;
70         }
71         ptr += strcspn(ptr, "\n\r") + 1;
72     }
73
74     avctx->pix_fmt = PIX_FMT_MONOWHITE;
75
76     if (p->data[0])
77         avctx->release_buffer(avctx, p);
78
79     p->reference = 0;
80     if ((ret = avctx->get_buffer(avctx, p)) < 0)
81         return ret;
82
83     linesize = (avctx->width + 7) / 8;
84     for (i = 0; i < avctx->height; i++) {
85         dst = p->data[0] + i * p->linesize[0];
86         for (j = 0; j < linesize; j++) {
87             uint8_t val;
88
89             ptr += strcspn(ptr, "x") + 1;
90             if (ptr < end && isxdigit(*ptr)) {
91                 val = convert(*ptr);
92                 ptr++;
93                 if (isxdigit(*ptr))
94                     val = (val << 4) + convert(*ptr);
95                 *dst++ = av_reverse[val];
96             } else {
97                 return AVERROR_INVALIDDATA;
98             }
99         }
100     }
101
102     p->key_frame = 1;
103     p->pict_type = AV_PICTURE_TYPE_I;
104
105     *data_size       = sizeof(AVFrame);
106     *(AVFrame *)data = *p;
107
108     return avpkt->size;
109 }
110
111 static av_cold int xbm_decode_close(AVCodecContext *avctx)
112 {
113     if (avctx->coded_frame->data[0])
114         avctx->release_buffer(avctx, avctx->coded_frame);
115
116     av_freep(&avctx->coded_frame);
117
118     return 0;
119 }
120
121 AVCodec ff_xbm_decoder = {
122     .name         = "xbm",
123     .type         = AVMEDIA_TYPE_VIDEO,
124     .id           = CODEC_ID_XBM,
125     .init         = xbm_decode_init,
126     .close        = xbm_decode_close,
127     .decode       = xbm_decode_frame,
128     .capabilities = CODEC_CAP_DR1,
129     .long_name    = NULL_IF_CONFIG_SMALL("XBM (X BitMap) image"),
130 };