FFmpeg
Loading...
Searching...
No Matches
xbmdec.c
Go to the documentation of this file.
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/reverse.h"
24
25#include "avcodec.h"
26#include "codec_internal.h"
27#include "decode.h"
28
29static int get_nibble(uint8_t x)
30{
31#define TIMES256(idx) \
32TIMES64(4 * (idx)) TIMES64(4 * (idx) + 1) TIMES64(4 * (idx) + 2) TIMES64(4 * (idx) + 3)
33#define TIMES64(idx) \
34TIMES16(4 * (idx)) TIMES16(4 * (idx) + 1) TIMES16(4 * (idx) + 2) TIMES16(4 * (idx) + 3)
35#define TIMES16(idx) \
36TIMES4(4 * (idx)) TIMES4(4 * (idx) + 1) TIMES4(4 * (idx) + 2) TIMES4(4 * (idx) + 3)
37#define TIMES4(idx) \
38ENTRY(4 * (idx)) ENTRY(4 * (idx) + 1) ENTRY(4 * (idx) + 2) ENTRY(4 * (idx) + 3)
39#define ENTRY(x) [x] = ((x) >= 'a' && (x) <= 'f') ? (x) - ('a' - 10) : \
40 ((x) >= 'A' && (x) <= 'F') ? (x) - ('A' - 10) : \
41 ((x) >= '0' && (x) <= '9') ? (x) - '0' : 255,
42
43 static const uint8_t lut[] = {
44 TIMES256(0)
45 };
46 return lut[x];
47}
48
49static int parse_str_int(const uint8_t *p, const uint8_t *end, const uint8_t *key)
50{
51 int keylen = strlen(key);
52 const uint8_t *e = end - keylen;
53
54 for(; p < e; p++) {
55 if (!memcmp(p, key, keylen))
56 break;
57 }
58 p += keylen;
59 if (p >= end)
60 return INT_MIN;
61
62 for(; p<end; p++) {
63 char *eptr;
64 int64_t ret = strtol(p, &eptr, 10);
65 if ((const uint8_t *)eptr != p)
66 return ret;
67 }
68 return INT_MIN;
69}
70
72 int *got_frame, AVPacket *avpkt)
73{
74 int ret, linesize, i, j;
75 int width = 0;
76 int height = 0;
77 const uint8_t *end, *ptr = avpkt->data;
78 const uint8_t *next;
79 uint8_t *dst;
80
82 end = avpkt->data + avpkt->size;
83
84 width = parse_str_int(avpkt->data, end, "_width");
85 height = parse_str_int(avpkt->data, end, "_height");
86
87 if ((ret = ff_set_dimensions(avctx, width, height)) < 0)
88 return ret;
89
90 if (avctx->skip_frame >= AVDISCARD_ALL)
91 return avpkt->size;
92
93 if ((ret = ff_get_buffer(avctx, p, 0)) < 0)
94 return ret;
95
96 // goto start of image data
97 next = memchr(ptr, '{', avpkt->size);
98 if (!next)
99 next = memchr(ptr, '(', avpkt->size);
100 if (!next)
101 return AVERROR_INVALIDDATA;
102 ptr = next + 1;
103
104 linesize = (avctx->width + 7) / 8;
105 for (i = 0; i < avctx->height; i++) {
106 dst = p->data[0] + i * p->linesize[0];
107 for (j = 0; j < linesize; j++) {
108 uint8_t nib, val;
109
110 while (ptr < end && *ptr != 'x' && *ptr != '$')
111 ptr++;
112
113 ptr ++;
114 if (ptr < end && (val = get_nibble(*ptr)) <= 15) {
115 ptr++;
116 if ((nib = get_nibble(*ptr)) <= 15) {
117 val = (val << 4) + nib;
118 ptr++;
119 }
120 *dst++ = ff_reverse[val];
121 if ((val = get_nibble(*ptr)) <= 15 && j+1 < linesize) {
122 j++;
123 ptr++;
124 if ((nib = get_nibble(*ptr)) <= 15) {
125 val = (val << 4) + nib;
126 ptr++;
127 }
128 *dst++ = ff_reverse[val];
129 }
130 } else {
131 av_log(avctx, AV_LOG_ERROR,
132 "Unexpected data at %.8s.\n", ptr);
133 return AVERROR_INVALIDDATA;
134 }
135 }
136 }
137
138 *got_frame = 1;
139
140 return avpkt->size;
141}
142
144 .p.name = "xbm",
145 CODEC_LONG_NAME("XBM (X BitMap) image"),
146 .p.type = AVMEDIA_TYPE_VIDEO,
147 .p.id = AV_CODEC_ID_XBM,
148 .p.capabilities = AV_CODEC_CAP_DR1,
149 .caps_internal = FF_CODEC_CAP_SKIP_FRAME_FILL_PARAM,
151};
uint8_t ptrdiff_t const uint8_t ptrdiff_t int intptr_t intptr_t int int16_t * dst
Definition dsp.h:87
static double val(void *priv, double ch)
Definition aeval.c:77
const FFCodec ff_xbm_decoder
Definition xbmdec.c:143
Libavcodec external API header.
#define i(width, name, range_min, range_max)
Definition cbs_h264.c:63
#define FF_CODEC_CAP_SKIP_FRAME_FILL_PARAM
The decoder extracts and fills its parameters even if the frame is skipped due to the skip_frame sett...
#define FF_CODEC_DECODE_CB(func)
#define CODEC_LONG_NAME(str)
long long int64_t
Definition coverity.c:34
int ff_get_buffer(AVCodecContext *avctx, AVFrame *frame, int flags)
Get a buffer for a frame.
Definition decode.c:1777
int ff_set_dimensions(AVCodecContext *s, int width, int height)
Definition utils.c:91
const char * key
#define AV_CODEC_CAP_DR1
Codec uses get_buffer() or get_encode_buffer() for allocating buffers and supports custom allocators.
Definition codec.h:49
@ AV_CODEC_ID_XBM
Definition codec_id.h:209
@ AVDISCARD_ALL
discard all
Definition defs.h:232
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition error.h:61
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition log.h:210
@ AVMEDIA_TYPE_VIDEO
Definition avutil.h:200
const uint8_t ff_reverse[256]
Definition reverse.c:23
@ AV_PIX_FMT_MONOWHITE
Y , 1bpp, 0 is white, 1 is black, in each byte pixels are ordered from the msb to the lsb.
Definition pixfmt.h:82
main external API structure.
Definition avcodec.h:443
enum AVPixelFormat pix_fmt
Pixel format, see AV_PIX_FMT_xxx.
Definition avcodec.h:643
int width
picture width / height.
Definition avcodec.h:604
enum AVDiscard skip_frame
Skip decoding for selected frames.
Definition avcodec.h:1667
This structure describes decoded (raw) audio or video data.
Definition frame.h:472
This structure stores compressed data.
Definition packet.h:580
int size
Definition packet.h:604
uint8_t * data
Definition packet.h:603
#define av_log(a,...)
#define height
Definition dsp.h:89
#define width
Definition dsp.h:89
#define TIMES256(idx)
static int parse_str_int(const uint8_t *p, const uint8_t *end, const uint8_t *key)
Definition xbmdec.c:49
static int get_nibble(uint8_t x)
Definition xbmdec.c:29
static int xbm_decode_frame(AVCodecContext *avctx, AVFrame *p, int *got_frame, AVPacket *avpkt)
Definition xbmdec.c:71