FFmpeg
wbmpdec.c
Go to the documentation of this file.
1 /*
2  * WBMP (Wireless Application Protocol Bitmap) image
3  *
4  * This file is part of FFmpeg.
5  *
6  * FFmpeg 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  * FFmpeg 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 FFmpeg; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19  */
20 
21 #include "avcodec.h"
22 #include "bytestream.h"
23 #include "codec_internal.h"
24 #include "decode.h"
25 #include "thread.h"
26 
27 static unsigned int getv(GetByteContext * gb)
28 {
29  int i;
30  unsigned int v = 0;
31 
32  do {
33  i = bytestream2_get_byte(gb);
34  v = (v << 7) | (i & 0x7F);
35  } while (i & 0x80);
36  return v;
37 }
38 
39 static void readbits(uint8_t * dst, int width, int height, int linesize, const uint8_t * src, int size)
40 {
41  int wpad = (width + 7) / 8;
42  for (int j = 0; j < height && size > 0; j++) {
43  memcpy(dst, src, FFMIN(wpad, size));
44  src += wpad;
45  size -= wpad;
46  dst += linesize;
47  }
48 }
49 
51  int *got_frame, AVPacket *avpkt)
52 {
53  const uint8_t *buf = avpkt->data;
54  int buf_size = avpkt->size, width, height, ret;
55  GetByteContext gb;
56 
57  bytestream2_init(&gb, buf, buf_size);
58 
59  if (getv(&gb))
60  return AVERROR_INVALIDDATA;
61  bytestream2_skip(&gb, 1);
62  width = getv(&gb);
63  height = getv(&gb);
64 
65  if ((ret = ff_set_dimensions(avctx, width, height)) < 0)
66  return ret;
67 
69  if ((ret = ff_thread_get_buffer(avctx, p, 0)) < 0)
70  return ret;
71 
72  if (p->linesize[0] == (width + 7) / 8)
73  bytestream2_get_buffer(&gb, p->data[0], height * ((width + 7) / 8));
74  else
75  readbits(p->data[0], width, height, p->linesize[0], gb.buffer, gb.buffer_end - gb.buffer);
76 
77  *got_frame = 1;
78 
79  return buf_size;
80 }
81 
83  .p.name = "wbmp",
84  CODEC_LONG_NAME("WBMP (Wireless Application Protocol Bitmap) image"),
85  .p.type = AVMEDIA_TYPE_VIDEO,
86  .p.id = AV_CODEC_ID_WBMP,
87  .p.capabilities = AV_CODEC_CAP_DR1 | AV_CODEC_CAP_FRAME_THREADS,
89 };
GetByteContext
Definition: bytestream.h:33
AVFrame
This structure describes decoded (raw) audio or video data.
Definition: frame.h:389
AVPacket::data
uint8_t * data
Definition: packet.h:539
FFCodec
Definition: codec_internal.h:127
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:94
thread.h
AVFrame::data
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
Definition: frame.h:410
bytestream2_skip
static av_always_inline void bytestream2_skip(GetByteContext *g, unsigned int size)
Definition: bytestream.h:168
FFCodec::p
AVCodec p
The public AVCodec.
Definition: codec_internal.h:131
FF_CODEC_DECODE_CB
#define FF_CODEC_DECODE_CB(func)
Definition: codec_internal.h:311
ff_thread_get_buffer
int ff_thread_get_buffer(AVCodecContext *avctx, AVFrame *f, int flags)
Wrapper around get_buffer() for frame-multithreaded codecs.
Definition: pthread_frame.c:1049
GetByteContext::buffer
const uint8_t * buffer
Definition: bytestream.h:34
decode.h
CODEC_LONG_NAME
#define CODEC_LONG_NAME(str)
Definition: codec_internal.h:296
AV_CODEC_CAP_FRAME_THREADS
#define AV_CODEC_CAP_FRAME_THREADS
Codec supports frame-level multithreading.
Definition: codec.h:110
AV_PIX_FMT_MONOBLACK
@ AV_PIX_FMT_MONOBLACK
Y , 1bpp, 0 is black, 1 is white, in each byte pixels are ordered from the msb to the lsb.
Definition: pixfmt.h:83
bytestream2_get_buffer
static av_always_inline unsigned int bytestream2_get_buffer(GetByteContext *g, uint8_t *dst, unsigned int size)
Definition: bytestream.h:267
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:540
height
#define height
Definition: dsp.h:85
codec_internal.h
dst
uint8_t ptrdiff_t const uint8_t ptrdiff_t int intptr_t intptr_t int int16_t * dst
Definition: dsp.h:83
size
int size
Definition: twinvq_data.h:10344
i
#define i(width, name, range_min, range_max)
Definition: cbs_h2645.c:256
FFMIN
#define FFMIN(a, b)
Definition: macros.h:49
wbmp_decode_frame
static int wbmp_decode_frame(AVCodecContext *avctx, AVFrame *p, int *got_frame, AVPacket *avpkt)
Definition: wbmpdec.c:50
AVCodec::name
const char * name
Name of the codec implementation.
Definition: codec.h:194
readbits
static void readbits(uint8_t *dst, int width, int height, int linesize, const uint8_t *src, int size)
Definition: wbmpdec.c:39
GetByteContext::buffer_end
const uint8_t * buffer_end
Definition: bytestream.h:34
AVCodecContext::pix_fmt
enum AVPixelFormat pix_fmt
Pixel format, see AV_PIX_FMT_xxx.
Definition: avcodec.h:663
getv
static unsigned int getv(GetByteContext *gb)
Definition: wbmpdec.c:27
avcodec.h
ret
ret
Definition: filter_design.txt:187
AVCodecContext
main external API structure.
Definition: avcodec.h:451
AV_CODEC_ID_WBMP
@ AV_CODEC_ID_WBMP
Definition: codec_id.h:317
AVMEDIA_TYPE_VIDEO
@ AVMEDIA_TYPE_VIDEO
Definition: avutil.h:201
AVPacket
This structure stores compressed data.
Definition: packet.h:516
bytestream.h
bytestream2_init
static av_always_inline void bytestream2_init(GetByteContext *g, const uint8_t *buf, int buf_size)
Definition: bytestream.h:137
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:434
AVERROR_INVALIDDATA
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition: error.h:61
width
#define width
Definition: dsp.h:85
ff_wbmp_decoder
const FFCodec ff_wbmp_decoder
Definition: wbmpdec.c:82
src
#define src
Definition: vp8dsp.c:248