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 #include "libavutil/reverse.h"
25 
26 #include "avcodec.h"
27 #include "codec_internal.h"
28 #include "internal.h"
29 #include "mathops.h"
30 
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 
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 
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 
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;
134 
135  *got_frame = 1;
136 
137  return avpkt->size;
138 }
139 
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,
147 };
ff_xbm_decoder
const FFCodec ff_xbm_decoder
Definition: xbmdec.c:140
AVFrame
This structure describes decoded (raw) audio or video data.
Definition: frame.h:325
internal.h
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:118
reverse.h
AVFrame::data
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
Definition: frame.h:346
parse_str_int
static int parse_str_int(const uint8_t *p, const uint8_t *end, const uint8_t *key)
Definition: xbmdec.c:46
FFCodec::p
AVCodec p
The public AVCodec.
Definition: codec_internal.h:122
AVFrame::key_frame
int key_frame
1 -> keyframe, 0-> not
Definition: frame.h:417
get_nibble
static int get_nibble(uint8_t x)
Definition: xbmdec.c:31
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:260
key
const char * key
Definition: hwcontext_opencl.c:174
AV_PICTURE_TYPE_I
@ AV_PICTURE_TYPE_I
Intra.
Definition: avutil.h:274
mathops.h
xbm_decode_frame
static int xbm_decode_frame(AVCodecContext *avctx, AVFrame *p, int *got_frame, AVPacket *avpkt)
Definition: xbmdec.c:68
AVFrame::pict_type
enum AVPictureType pict_type
Picture type of the frame.
Definition: frame.h:422
ff_get_buffer
int ff_get_buffer(AVCodecContext *avctx, AVFrame *frame, int flags)
Get a buffer for a frame.
Definition: decode.c:1462
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
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
codec_internal.h
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:211
AVCodecContext::height
int height
Definition: avcodec.h:571
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:608
avcodec.h
ret
ret
Definition: filter_design.txt:187
AVCodecContext
main external API structure.
Definition: avcodec.h:398
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:90
AVPacket
This structure stores compressed data.
Definition: packet.h:351
AVCodecContext::width
int width
picture width / height.
Definition: avcodec.h:571
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:370
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
avstring.h