Libav
xbmdec.c
Go to the documentation of this file.
1 /*
2  * XBM image format
3  *
4  * This file is part of Libav.
5  *
6  * Libav is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or (at your option) any later version.
10  *
11  * Libav is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with Libav; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19  */
20 
21 #include "libavutil/avstring.h"
22 
23 #include "avcodec.h"
24 #include "internal.h"
25 #include "mathops.h"
26 
27 static int xbm_decode_frame(AVCodecContext *avctx, void *data,
28  int *got_frame, AVPacket *avpkt)
29 {
30  AVFrame *p = data;
31  int ret, linesize, i;
32  int width = 0;
33  int height = 0;
34  const uint8_t *ptr = avpkt->data;
35  uint8_t *dst;
36 
38  while (!width || !height) {
39  ptr += strcspn(ptr, "#");
40  if (ptr >= avpkt->data + avpkt->size) {
41  av_log(avctx, AV_LOG_ERROR, "End of file reached.\n");
42  return AVERROR_INVALIDDATA;
43  }
44  if (strncmp(ptr, "#define", 7) != 0) {
45  av_log(avctx, AV_LOG_ERROR,
46  "Unexpected preprocessor directive.\n");
47  return AVERROR_INVALIDDATA;
48  }
49  // skip the name
50  ptr += strcspn(ptr, "_") + 1;
51  // get width or height
52  if (strncmp(ptr, "width", 5) == 0) {
53  ptr += strcspn(ptr, " ");
54  width = strtol(ptr, NULL, 10);
55  } else if (strncmp(ptr, "height", 6) == 0) {
56  ptr += strcspn(ptr, " ");
57  height = strtol(ptr, NULL, 10);
58  } else {
59  // skip offset and unknown variables
60  av_log(avctx, AV_LOG_VERBOSE,
61  "Ignoring preprocessor directive.\n");
62  }
63  }
64 
65  if ((ret = ff_set_dimensions(avctx, width, height)) < 0)
66  return ret;
67 
68  if ((ret = ff_get_buffer(avctx, p, 0)) < 0)
69  return ret;
70 
71  // go to start of image data
72  ptr += strcspn(ptr, "{");
73 
74  linesize = (avctx->width + 7) / 8;
75  for (i = 0; i < avctx->height; i++) {
76  int eol = 0, e = 0;
77  dst = p->data[0] + i * p->linesize[0];
78  if (ptr >= avpkt->data + avpkt->size) {
79  av_log(avctx, AV_LOG_ERROR, "End of file reached.\n");
80  return AVERROR_INVALIDDATA;
81  }
82  do {
83  int val;
84  uint8_t *endptr;
85 
86  ptr += strcspn(ptr, "x") - 1; // -1 to get 0x
87  val = strtol(ptr, (char **)&endptr, 16);
88 
89  if (endptr - ptr == 4) {
90  // XBM X11 format
91  *dst++ = ff_reverse[val];
92  eol = linesize;
93  } else if (endptr - ptr == 6) {
94  // XBM X10 format
95  *dst++ = ff_reverse[val >> 8];
96  *dst++ = ff_reverse[val & 0xFF];
97  eol = linesize / 2; // 2 bytes read
98  } else {
99  av_log(avctx, AV_LOG_ERROR,
100  "Unexpected data at %.8s.\n", ptr);
101  return AVERROR_INVALIDDATA;
102  }
103  ptr = endptr;
104  } while (++e < eol);
105  }
106 
107  p->key_frame = 1;
109 
110  *got_frame = 1;
111 
112  return avpkt->size;
113 }
114 
116  .name = "xbm",
117  .long_name = NULL_IF_CONFIG_SMALL("XBM (X BitMap) image"),
118  .type = AVMEDIA_TYPE_VIDEO,
119  .id = AV_CODEC_ID_XBM,
120  .decode = xbm_decode_frame,
121  .capabilities = CODEC_CAP_DR1,
122 };
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition: error.h:54
This structure describes decoded (raw) audio or video data.
Definition: frame.h:135
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:133
int size
Definition: avcodec.h:974
enum AVPixelFormat pix_fmt
Pixel format, see AV_PIX_FMT_xxx.
Definition: avcodec.h:1270
AVCodec.
Definition: avcodec.h:2812
AVCodec ff_xbm_decoder
Definition: xbmdec.c:115
uint8_t
#define CODEC_CAP_DR1
Codec uses get_buffer() for allocating buffers and supports custom allocators.
Definition: avcodec.h:684
static int xbm_decode_frame(AVCodecContext *avctx, void *data, int *got_frame, AVPacket *avpkt)
Definition: xbmdec.c:27
const char data[16]
Definition: mxf.c:70
uint8_t * data
Definition: avcodec.h:973
#define AV_LOG_VERBOSE
Detailed information.
Definition: log.h:139
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:123
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
Definition: internal.h:145
void av_log(void *avcl, int level, const char *fmt,...)
Definition: log.c:169
const char * name
Name of the codec implementation.
Definition: avcodec.h:2819
enum AVPictureType pict_type
Picture type of the frame.
Definition: frame.h:196
int width
picture width / height.
Definition: avcodec.h:1229
NULL
Definition: eval.c:55
static int width
Definition: utils.c:156
Libavcodec external API header.
int linesize[AV_NUM_DATA_POINTERS]
For video, size in bytes of each picture line.
Definition: frame.h:153
main external API structure.
Definition: avcodec.h:1050
int ff_get_buffer(AVCodecContext *avctx, AVFrame *frame, int flags)
Get a buffer for a frame.
Definition: utils.c:612
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
Definition: frame.h:141
int height
Definition: gxfenc.c:72
common internal api header.
Y , 1bpp, 0 is white, 1 is black, in each byte pixels are ordered from the msb to the lsb...
Definition: pixfmt.h:74
int key_frame
1 -> keyframe, 0-> not
Definition: frame.h:191
const uint8_t ff_reverse[256]
Definition: mathtables.c:72
This structure stores compressed data.
Definition: avcodec.h:950