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