FFmpeg  2.1.1
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 "avcodec.h"
24 #include "internal.h"
25 #include "mathops.h"
26 #include "libavutil/avstring.h"
27 
29 {
31 
32  return 0;
33 }
34 
35 static int convert(uint8_t x)
36 {
37  if (x >= 'a')
38  x -= 87;
39  else if (x >= 'A')
40  x -= 55;
41  else
42  x -= '0';
43  return x;
44 }
45 
46 static int xbm_decode_frame(AVCodecContext *avctx, void *data,
47  int *got_frame, AVPacket *avpkt)
48 {
49  AVFrame *p = data;
50  const uint8_t *end, *ptr = avpkt->data;
51  uint8_t *dst;
52  int ret, linesize, i, j;
53 
54  end = avpkt->data + avpkt->size;
55  while (!avctx->width || !avctx->height) {
56  char name[256];
57  int number, len;
58 
59  ptr += strcspn(ptr, "#");
60  if (sscanf(ptr, "#define %255s %u", name, &number) != 2) {
61  av_log(avctx, AV_LOG_ERROR, "Unexpected preprocessor directive\n");
62  return AVERROR_INVALIDDATA;
63  }
64 
65  len = strlen(name);
66  if ((len > 6) && !avctx->height && !memcmp(name + len - 7, "_height", 7)) {
67  avctx->height = number;
68  } else if ((len > 5) && !avctx->width && !memcmp(name + len - 6, "_width", 6)) {
69  avctx->width = number;
70  } else {
71  av_log(avctx, AV_LOG_ERROR, "Unknown define '%s'\n", name);
72  return AVERROR_INVALIDDATA;
73  }
74  ptr += strcspn(ptr, "\n\r") + 1;
75  }
76 
77  if ((ret = ff_get_buffer(avctx, p, 0)) < 0)
78  return ret;
79 
80  // goto start of image data
81  ptr += strcspn(ptr, "{") + 1;
82 
83  linesize = (avctx->width + 7) / 8;
84  for (i = 0; i < avctx->height; i++) {
85  dst = p->data[0] + i * p->linesize[0];
86  for (j = 0; j < linesize; j++) {
87  uint8_t val;
88 
89  ptr += strcspn(ptr, "x") + 1;
90  if (ptr < end && av_isxdigit(*ptr)) {
91  val = convert(*ptr);
92  ptr++;
93  if (av_isxdigit(*ptr))
94  val = (val << 4) + convert(*ptr);
95  *dst++ = ff_reverse[val];
96  } else {
97  av_log(avctx, AV_LOG_ERROR, "Unexpected data at '%.8s'\n", ptr);
98  return AVERROR_INVALIDDATA;
99  }
100  }
101  }
102 
103  p->key_frame = 1;
105 
106  *got_frame = 1;
107 
108  return avpkt->size;
109 }
110 
112  .name = "xbm",
113  .long_name = NULL_IF_CONFIG_SMALL("XBM (X BitMap) image"),
114  .type = AVMEDIA_TYPE_VIDEO,
115  .id = AV_CODEC_ID_XBM,
116  .init = xbm_decode_init,
117  .decode = xbm_decode_frame,
118  .capabilities = CODEC_CAP_DR1,
119 };
const char * name
Definition: avisynth_c.h:675
const char const char void * val
Definition: avisynth_c.h:671
This structure describes decoded (raw) audio or video data.
Definition: frame.h:96
int av_isxdigit(int c)
Locale-independent conversion of ASCII isxdigit.
Definition: avstring.c:304
int size
Definition: avcodec.h:1064
void av_log(void *avcl, int level, const char *fmt,...) av_printf_format(3
Send the specified message to the log if the level is less than or equal to the current av_log_level...
enum AVPixelFormat pix_fmt
Pixel format, see AV_PIX_FMT_xxx.
Definition: avcodec.h:1342
AVCodec.
Definition: avcodec.h:2922
#define av_cold
Definition: avcodec.h:653
AVCodec ff_xbm_decoder
Definition: xbmdec.c:111
uint8_t
static av_cold int end(AVCodecContext *avctx)
Definition: avrndec.c:67
const char * name
Name of the codec implementation.
Definition: avcodec.h:2929
#define CODEC_CAP_DR1
Codec uses get_buffer() for allocating buffers and supports custom allocators.
Definition: avcodec.h:742
static int xbm_decode_frame(AVCodecContext *avctx, void *data, int *got_frame, AVPacket *avpkt)
Definition: xbmdec.c:46
const char data[16]
Definition: mxf.c:68
static av_cold int xbm_decode_init(AVCodecContext *avctx)
Definition: xbmdec.c:28
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: avcodec.h:4147
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
Definition: internal.h:151
Libavcodec external API header.
enum AVPictureType pict_type
Picture type of the frame.
Definition: frame.h:167
ret
Definition: avfilter.c:961
int width
picture width / height.
Definition: avcodec.h:1314
static int convert(uint8_t x)
Definition: xbmdec.c:35
main external API structure.
Definition: avcodec.h:1146
int ff_get_buffer(AVCodecContext *avctx, AVFrame *frame, int flags)
Get a buffer for a frame.
Definition: utils.c:941
uint8_t * data
Definition: avcodec.h:1063
int linesize[AV_NUM_DATA_POINTERS]
For video, size in bytes of each picture line.
Definition: frame.h:124
Y , 1bpp, 0 is white, 1 is black, in each byte pixels are ordered from the msb to the lsb...
Definition: avcodec.h:4543
common internal api header.
#define AVERROR_INVALIDDATA
int len
int key_frame
1 -&gt; keyframe, 0-&gt; not
Definition: frame.h:162
const uint8_t ff_reverse[256]
Definition: mathtables.c:72
This structure stores compressed data.
Definition: avcodec.h:1040
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
Definition: frame.h:107