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/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 
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;
133 
134  *got_frame = 1;
135 
136  return avpkt->size;
137 }
138 
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 };
AVCodec
AVCodec.
Definition: codec.h:202
AVFrame
This structure describes decoded (raw) audio or video data.
Definition: frame.h:317
internal.h
AVPacket::data
uint8_t * data
Definition: packet.h:373
ff_reverse
const uint8_t ff_reverse[256]
Definition: reverse.c:23
data
const char data[16]
Definition: mxf.c:143
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
AVFrame::data
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
Definition: frame.h:338
parse_str_int
static int parse_str_int(const uint8_t *p, const uint8_t *end, const uint8_t *key)
Definition: xbmdec.c:44
ff_xbm_decoder
const AVCodec ff_xbm_decoder
Definition: xbmdec.c:139
AVFrame::key_frame
int key_frame
1 -> keyframe, 0-> not
Definition: frame.h:409
get_nibble
static int get_nibble(uint8_t x)
Definition: xbmdec.c:29
val
static double val(void *priv, double ch)
Definition: aeval.c:76
AV_LOG_ERROR
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:180
width
#define width
key
const char * key
Definition: hwcontext_opencl.c:168
AV_PICTURE_TYPE_I
@ AV_PICTURE_TYPE_I
Intra.
Definition: avutil.h:274
mathops.h
AVFrame::pict_type
enum AVPictureType pict_type
Picture type of the frame.
Definition: frame.h:414
ff_get_buffer
int ff_get_buffer(AVCodecContext *avctx, AVFrame *frame, int flags)
Get a buffer for a frame.
Definition: decode.c:1652
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:374
NULL_IF_CONFIG_SMALL
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification.
Definition: internal.h:117
height
#define height
i
#define i(width, name, range_min, range_max)
Definition: cbs_h2645.c:271
AVCodec::name
const char * name
Name of the codec implementation.
Definition: codec.h:209
AVCodecContext::height
int height
Definition: avcodec.h:556
AV_CODEC_ID_XBM
@ AV_CODEC_ID_XBM
Definition: codec_id.h:210
AVCodecContext::pix_fmt
enum AVPixelFormat pix_fmt
Pixel format, see AV_PIX_FMT_xxx.
Definition: avcodec.h:593
avcodec.h
ret
ret
Definition: filter_design.txt:187
AVCodecContext
main external API structure.
Definition: avcodec.h:383
xbm_decode_frame
static int xbm_decode_frame(AVCodecContext *avctx, void *data, int *got_frame, AVPacket *avpkt)
Definition: xbmdec.c:66
AVMEDIA_TYPE_VIDEO
@ AVMEDIA_TYPE_VIDEO
Definition: avutil.h:201
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:86
AVPacket
This structure stores compressed data.
Definition: packet.h:350
AVCodecContext::width
int width
picture width / height.
Definition: avcodec.h:556
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:362
av_log
#define av_log(a,...)
Definition: tableprint_vlc.h:28
AVERROR_INVALIDDATA
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition: error.h:61
avstring.h