FFmpeg
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 
29 static int get_nibble(uint8_t x)
30 {
31 #define TIMES256(idx) \
32 TIMES64(4 * (idx)) TIMES64(4 * (idx) + 1) TIMES64(4 * (idx) + 2) TIMES64(4 * (idx) + 3)
33 #define TIMES64(idx) \
34 TIMES16(4 * (idx)) TIMES16(4 * (idx) + 1) TIMES16(4 * (idx) + 2) TIMES16(4 * (idx) + 3)
35 #define TIMES16(idx) \
36 TIMES4(4 * (idx)) TIMES4(4 * (idx) + 1) TIMES4(4 * (idx) + 2) TIMES4(4 * (idx) + 3)
37 #define TIMES4(idx) \
38 ENTRY(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 
49 static 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 
71 static int xbm_decode_frame(AVCodecContext *avctx, AVFrame *p,
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  p->flags |= AV_FRAME_FLAG_KEY;
140 
141  *got_frame = 1;
142 
143  return avpkt->size;
144 }
145 
147  .p.name = "xbm",
148  CODEC_LONG_NAME("XBM (X BitMap) image"),
149  .p.type = AVMEDIA_TYPE_VIDEO,
150  .p.id = AV_CODEC_ID_XBM,
151  .p.capabilities = AV_CODEC_CAP_DR1,
152  .caps_internal = FF_CODEC_CAP_SKIP_FRAME_FILL_PARAM,
154 };
ff_xbm_decoder
const FFCodec ff_xbm_decoder
Definition: xbmdec.c:146
AVFrame
This structure describes decoded (raw) audio or video data.
Definition: frame.h:375
AVPacket::data
uint8_t * data
Definition: packet.h:522
ff_reverse
const uint8_t ff_reverse[256]
Definition: reverse.c:23
AV_PIX_FMT_MONOWHITE
@ 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
FFCodec
Definition: codec_internal.h:127
reverse.h
AVFrame::flags
int flags
Frame flags, a combination of AV_FRAME_FLAGS.
Definition: frame.h:647
ff_set_dimensions
int ff_set_dimensions(AVCodecContext *s, int width, int height)
Check that the provided frame dimensions are valid and set them on the codec context.
Definition: utils.c:94
AVFrame::data
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
Definition: frame.h:396
parse_str_int
static int parse_str_int(const uint8_t *p, const uint8_t *end, const uint8_t *key)
Definition: xbmdec.c:49
FFCodec::p
AVCodec p
The public AVCodec.
Definition: codec_internal.h:131
AVCodecContext::skip_frame
enum AVDiscard skip_frame
Skip decoding for selected frames.
Definition: avcodec.h:1819
get_nibble
static int get_nibble(uint8_t x)
Definition: xbmdec.c:29
val
static double val(void *priv, double ch)
Definition: aeval.c:78
AV_LOG_ERROR
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:180
AV_FRAME_FLAG_KEY
#define AV_FRAME_FLAG_KEY
A flag to mark frames that are keyframes.
Definition: frame.h:626
width
#define width
FF_CODEC_DECODE_CB
#define FF_CODEC_DECODE_CB(func)
Definition: codec_internal.h:287
decode.h
key
const char * key
Definition: hwcontext_opencl.c:189
CODEC_LONG_NAME
#define CODEC_LONG_NAME(str)
Definition: codec_internal.h:272
AVDISCARD_ALL
@ AVDISCARD_ALL
discard all
Definition: defs.h:219
AV_PICTURE_TYPE_I
@ AV_PICTURE_TYPE_I
Intra.
Definition: avutil.h:279
xbm_decode_frame
static int xbm_decode_frame(AVCodecContext *avctx, AVFrame *p, int *got_frame, AVPacket *avpkt)
Definition: xbmdec.c:71
AVFrame::pict_type
enum AVPictureType pict_type
Picture type of the frame.
Definition: frame.h:477
ff_get_buffer
int ff_get_buffer(AVCodecContext *avctx, AVFrame *frame, int flags)
Get a buffer for a frame.
Definition: decode.c:1553
AV_CODEC_CAP_DR1
#define AV_CODEC_CAP_DR1
Codec uses get_buffer() or get_encode_buffer() for allocating buffers and supports custom allocators.
Definition: codec.h:52
AVPacket::size
int size
Definition: packet.h:523
codec_internal.h
FF_CODEC_CAP_SKIP_FRAME_FILL_PARAM
#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...
Definition: codec_internal.h:54
height
#define height
i
#define i(width, name, range_min, range_max)
Definition: cbs_h2645.c:255
AVCodec::name
const char * name
Name of the codec implementation.
Definition: codec.h:194
AVCodecContext::height
int height
Definition: avcodec.h:618
AV_CODEC_ID_XBM
@ AV_CODEC_ID_XBM
Definition: codec_id.h:212
AVCodecContext::pix_fmt
enum AVPixelFormat pix_fmt
Pixel format, see AV_PIX_FMT_xxx.
Definition: avcodec.h:657
avcodec.h
ret
ret
Definition: filter_design.txt:187
AVCodecContext
main external API structure.
Definition: avcodec.h:445
AVMEDIA_TYPE_VIDEO
@ AVMEDIA_TYPE_VIDEO
Definition: avutil.h:201
AVPacket
This structure stores compressed data.
Definition: packet.h:499
AVCodecContext::width
int width
picture width / height.
Definition: avcodec.h:618
AVFrame::linesize
int linesize[AV_NUM_DATA_POINTERS]
For video, a positive or negative value, which is typically indicating the size in bytes of each pict...
Definition: frame.h:420
av_log
#define av_log(a,...)
Definition: tableprint_vlc.h:27
AVERROR_INVALIDDATA
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition: error.h:61
TIMES256
#define TIMES256(idx)