]> git.sesse.net Git - ffmpeg/blob - libavcodec/xbmdec.c
avcodec: Constify AVCodecs
[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 "libavutil/avstring.h"
24
25 #include "avcodec.h"
26 #include "internal.h"
27 #include "mathops.h"
28
29 static int get_nibble(uint8_t x)
30 {
31     int ret = 255;
32
33     if (x <= '9') {
34         if (x >= '0')
35             ret = x - '0';
36     } else if (x >= 'a') {
37         if (x <= 'f')
38             ret = x - ('a' - 10);
39     } else if (x >= 'A' && x <= 'F')
40         ret = x - ('A' - 10);
41     return ret;
42 }
43
44 static int parse_str_int(const uint8_t *p, const uint8_t *end, const uint8_t *key)
45 {
46     int keylen = strlen(key);
47     const uint8_t *e = end - keylen;
48
49     for(; p < e; p++) {
50         if (!memcmp(p, key, keylen))
51             break;
52     }
53     p += keylen;
54     if (p >= end)
55         return INT_MIN;
56
57     for(; p<end; p++) {
58         char *eptr;
59         int64_t ret = strtol(p, &eptr, 10);
60         if ((const uint8_t *)eptr != p)
61             return ret;
62     }
63     return INT_MIN;
64 }
65
66 static int xbm_decode_frame(AVCodecContext *avctx, void *data,
67                             int *got_frame, AVPacket *avpkt)
68 {
69     AVFrame *p = data;
70     int ret, linesize, i, j;
71     int width  = 0;
72     int height = 0;
73     const uint8_t *end, *ptr = avpkt->data;
74     const uint8_t *next;
75     uint8_t *dst;
76
77     avctx->pix_fmt = AV_PIX_FMT_MONOWHITE;
78     end = avpkt->data + avpkt->size;
79
80     width  = parse_str_int(avpkt->data, end, "_width");
81     height = parse_str_int(avpkt->data, end, "_height");
82
83     if ((ret = ff_set_dimensions(avctx, width, height)) < 0)
84         return ret;
85
86     if ((ret = ff_get_buffer(avctx, p, 0)) < 0)
87         return ret;
88
89     // goto start of image data
90     next = memchr(ptr, '{', avpkt->size);
91     if (!next)
92         next = memchr(ptr, '(', avpkt->size);
93     if (!next)
94         return AVERROR_INVALIDDATA;
95     ptr = next + 1;
96
97     linesize = (avctx->width + 7) / 8;
98     for (i = 0; i < avctx->height; i++) {
99         dst = p->data[0] + i * p->linesize[0];
100         for (j = 0; j < linesize; j++) {
101             uint8_t nib, val;
102
103             while (ptr < end && *ptr != 'x' && *ptr != '$')
104                 ptr++;
105
106             ptr ++;
107             if (ptr < end && (val = get_nibble(*ptr)) <= 15) {
108                 ptr++;
109                 if ((nib = get_nibble(*ptr)) <= 15) {
110                     val = (val << 4) + nib;
111                     ptr++;
112                 }
113                 *dst++ = ff_reverse[val];
114                 if ((val = get_nibble(*ptr)) <= 15 && j+1 < linesize) {
115                     j++;
116                     ptr++;
117                     if ((nib = get_nibble(*ptr)) <= 15) {
118                         val = (val << 4) + nib;
119                         ptr++;
120                     }
121                     *dst++ = ff_reverse[val];
122                 }
123             } else {
124                 av_log(avctx, AV_LOG_ERROR,
125                        "Unexpected data at %.8s.\n", ptr);
126                 return AVERROR_INVALIDDATA;
127             }
128         }
129     }
130
131     p->key_frame = 1;
132     p->pict_type = AV_PICTURE_TYPE_I;
133
134     *got_frame       = 1;
135
136     return avpkt->size;
137 }
138
139 const AVCodec ff_xbm_decoder = {
140     .name         = "xbm",
141     .long_name    = NULL_IF_CONFIG_SMALL("XBM (X BitMap) image"),
142     .type         = AVMEDIA_TYPE_VIDEO,
143     .id           = AV_CODEC_ID_XBM,
144     .decode       = xbm_decode_frame,
145     .capabilities = AV_CODEC_CAP_DR1,
146 };