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  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, AVFrame *p,
67  int *got_frame, AVPacket *avpkt)
68 {
69  int ret, linesize, i, j;
70  int width = 0;
71  int height = 0;
72  const uint8_t *end, *ptr = avpkt->data;
73  const uint8_t *next;
74  uint8_t *dst;
75 
77  end = avpkt->data + avpkt->size;
78 
79  width = parse_str_int(avpkt->data, end, "_width");
80  height = parse_str_int(avpkt->data, end, "_height");
81 
82  if ((ret = ff_set_dimensions(avctx, width, height)) < 0)
83  return ret;
84 
85  if (avctx->skip_frame >= AVDISCARD_ALL)
86  return avpkt->size;
87 
88  if ((ret = ff_get_buffer(avctx, p, 0)) < 0)
89  return ret;
90 
91  // goto start of image data
92  next = memchr(ptr, '{', avpkt->size);
93  if (!next)
94  next = memchr(ptr, '(', avpkt->size);
95  if (!next)
96  return AVERROR_INVALIDDATA;
97  ptr = next + 1;
98 
99  linesize = (avctx->width + 7) / 8;
100  for (i = 0; i < avctx->height; i++) {
101  dst = p->data[0] + i * p->linesize[0];
102  for (j = 0; j < linesize; j++) {
103  uint8_t nib, val;
104 
105  while (ptr < end && *ptr != 'x' && *ptr != '$')
106  ptr++;
107 
108  ptr ++;
109  if (ptr < end && (val = get_nibble(*ptr)) <= 15) {
110  ptr++;
111  if ((nib = get_nibble(*ptr)) <= 15) {
112  val = (val << 4) + nib;
113  ptr++;
114  }
115  *dst++ = ff_reverse[val];
116  if ((val = get_nibble(*ptr)) <= 15 && j+1 < linesize) {
117  j++;
118  ptr++;
119  if ((nib = get_nibble(*ptr)) <= 15) {
120  val = (val << 4) + nib;
121  ptr++;
122  }
123  *dst++ = ff_reverse[val];
124  }
125  } else {
126  av_log(avctx, AV_LOG_ERROR,
127  "Unexpected data at %.8s.\n", ptr);
128  return AVERROR_INVALIDDATA;
129  }
130  }
131  }
132 
133  p->key_frame = 1;
135 
136  *got_frame = 1;
137 
138  return avpkt->size;
139 }
140 
142  .p.name = "xbm",
143  CODEC_LONG_NAME("XBM (X BitMap) image"),
144  .p.type = AVMEDIA_TYPE_VIDEO,
145  .p.id = AV_CODEC_ID_XBM,
146  .p.capabilities = AV_CODEC_CAP_DR1,
147  .caps_internal = FF_CODEC_CAP_SKIP_FRAME_FILL_PARAM,
149 };
ff_xbm_decoder
const FFCodec ff_xbm_decoder
Definition: xbmdec.c:141
AVFrame
This structure describes decoded (raw) audio or video data.
Definition: frame.h:330
AVPacket::data
uint8_t * data
Definition: packet.h:374
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:75
FFCodec
Definition: codec_internal.h:127
reverse.h
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:91
AVFrame::data
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
Definition: frame.h:351
parse_str_int
static int parse_str_int(const uint8_t *p, const uint8_t *end, const uint8_t *key)
Definition: xbmdec.c:44
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:1713
AVFrame::key_frame
int key_frame
1 -> keyframe, 0-> not
Definition: frame.h:422
get_nibble
static int get_nibble(uint8_t x)
Definition: xbmdec.c:29
val
static double val(void *priv, double ch)
Definition: aeval.c:77
AV_LOG_ERROR
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:180
width
#define width
FF_CODEC_DECODE_CB
#define FF_CODEC_DECODE_CB(func)
Definition: codec_internal.h:306
decode.h
key
const char * key
Definition: hwcontext_opencl.c:174
CODEC_LONG_NAME
#define CODEC_LONG_NAME(str)
Definition: codec_internal.h:272
AVDISCARD_ALL
@ AVDISCARD_ALL
discard all
Definition: defs.h:76
AV_PICTURE_TYPE_I
@ AV_PICTURE_TYPE_I
Intra.
Definition: avutil.h:274
xbm_decode_frame
static int xbm_decode_frame(AVCodecContext *avctx, AVFrame *p, int *got_frame, AVPacket *avpkt)
Definition: xbmdec.c:66
AVFrame::pict_type
enum AVPictureType pict_type
Picture type of the frame.
Definition: frame.h:427
ff_get_buffer
int ff_get_buffer(AVCodecContext *avctx, AVFrame *frame, int flags)
Get a buffer for a frame.
Definition: decode.c:1473
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:375
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:269
AVCodec::name
const char * name
Name of the codec implementation.
Definition: codec.h:191
AVCodecContext::height
int height
Definition: avcodec.h:598
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:635
avcodec.h
ret
ret
Definition: filter_design.txt:187
AVCodecContext
main external API structure.
Definition: avcodec.h:426
AVMEDIA_TYPE_VIDEO
@ AVMEDIA_TYPE_VIDEO
Definition: avutil.h:201
AVPacket
This structure stores compressed data.
Definition: packet.h:351
AVCodecContext::width
int width
picture width / height.
Definition: avcodec.h:598
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:375
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