Libav
vble.c
Go to the documentation of this file.
1 /*
2  * VBLE Decoder
3  * Copyright (c) 2011 Derek Buitenhuis
4  *
5  * This file is part of Libav.
6  *
7  * Libav is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Lesser General Public
9  * License as published by the Free Software Foundation; either
10  * version 2.1 of the License, or (at your option) any later version.
11  *
12  * Libav is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15  * Lesser General Public License for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public
18  * License along with Libav; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20  */
21 
27 #include "libavutil/imgutils.h"
28 
29 #define BITSTREAM_READER_LE
30 #include "avcodec.h"
31 #include "get_bits.h"
32 #include "huffyuvdsp.h"
33 #include "internal.h"
34 #include "mathops.h"
35 
36 typedef struct VBLEContext {
39 
40  int size;
41  uint8_t *val; /* First holds the lengths of vlc symbols and then their values */
42 } VBLEContext;
43 
45 {
46  /* At most we need to read 9 bits total to get indices up to 8 */
47  uint8_t val = show_bits(gb, 8);
48 
49  if (val) {
50  val = 7 - av_log2_16bit(ff_reverse[val]);
51  skip_bits(gb, val + 1);
52  return val;
53  } else {
54  skip_bits(gb, 8);
55  if (get_bits1(gb))
56  return 8;
57  }
58 
59  /* Return something larger than 8 on error */
60  return UINT8_MAX;
61 }
62 
64 {
65  int i;
66 
67  /* Read all the lengths in first */
68  for (i = 0; i < ctx->size; i++) {
69  ctx->val[i] = vble_read_reverse_unary(gb);
70 
71  if (ctx->val[i] == UINT8_MAX)
72  return -1;
73  }
74 
75  for (i = 0; i < ctx->size; i++) {
76  /* Check we have enough bits left */
77  if (get_bits_left(gb) < ctx->val[i])
78  return -1;
79 
80  /* get_bits can't take a length of 0 */
81  if (ctx->val[i])
82  ctx->val[i] = (1 << ctx->val[i]) + get_bits(gb, ctx->val[i]) - 1;
83  }
84 
85  return 0;
86 }
87 
89  int plane, int offset,
90  int width, int height)
91 {
92  uint8_t *dst = pic->data[plane];
93  uint8_t *val = ctx->val + offset;
94  int stride = pic->linesize[plane];
95  int i, j, left, left_top;
96 
97  for (i = 0; i < height; i++) {
98  for (j = 0; j < width; j++)
99  val[j] = (val[j] >> 1) ^ -(val[j] & 1);
100 
101  if (i) {
102  left = 0;
103  left_top = dst[-stride];
104  ctx->hdsp.add_hfyu_median_pred(dst, dst - stride, val,
105  width, &left, &left_top);
106  } else {
107  dst[0] = val[0];
108  for (j = 1; j < width; j++)
109  dst[j] = val[j] + dst[j - 1];
110  }
111  dst += stride;
112  val += width;
113  }
114 }
115 
116 static int vble_decode_frame(AVCodecContext *avctx, void *data, int *got_frame,
117  AVPacket *avpkt)
118 {
119  VBLEContext *ctx = avctx->priv_data;
120  AVFrame *pic = data;
121  GetBitContext gb;
122  const uint8_t *src = avpkt->data;
123  int version;
124  int offset = 0;
125  int width_uv = avctx->width / 2, height_uv = avctx->height / 2;
126 
127  /* Allocate buffer */
128  if (ff_get_buffer(avctx, pic, 0) < 0) {
129  av_log(avctx, AV_LOG_ERROR, "Could not allocate buffer.\n");
130  return AVERROR(ENOMEM);
131  }
132 
133  /* Set flags */
134  pic->key_frame = 1;
136 
137  /* Version should always be 1 */
138  version = AV_RL32(src);
139 
140  if (version != 1)
141  av_log(avctx, AV_LOG_WARNING, "Unsupported VBLE Version: %d\n", version);
142 
143  init_get_bits(&gb, src + 4, (avpkt->size - 4) * 8);
144 
145  /* Unpack */
146  if (vble_unpack(ctx, &gb) < 0) {
147  av_log(avctx, AV_LOG_ERROR, "Invalid Code\n");
148  return AVERROR_INVALIDDATA;
149  }
150 
151  /* Restore planes. Should be almost identical to Huffyuv's. */
152  vble_restore_plane(ctx, pic, 0, offset, avctx->width, avctx->height);
153 
154  /* Chroma */
155  if (!(ctx->avctx->flags & AV_CODEC_FLAG_GRAY)) {
156  offset += avctx->width * avctx->height;
157  vble_restore_plane(ctx, pic, 1, offset, width_uv, height_uv);
158 
159  offset += width_uv * height_uv;
160  vble_restore_plane(ctx, pic, 2, offset, width_uv, height_uv);
161  }
162 
163  *got_frame = 1;
164 
165  return avpkt->size;
166 }
167 
169 {
170  VBLEContext *ctx = avctx->priv_data;
171  av_freep(&ctx->val);
172 
173  return 0;
174 }
175 
177 {
178  VBLEContext *ctx = avctx->priv_data;
179 
180  /* Stash for later use */
181  ctx->avctx = avctx;
182  ff_huffyuvdsp_init(&ctx->hdsp);
183 
184  avctx->pix_fmt = AV_PIX_FMT_YUV420P;
185  avctx->bits_per_raw_sample = 8;
186 
187  ctx->size = av_image_get_buffer_size(avctx->pix_fmt,
188  avctx->width, avctx->height, 1);
189 
190  ctx->val = av_malloc(ctx->size * sizeof(*ctx->val));
191 
192  if (!ctx->val) {
193  av_log(avctx, AV_LOG_ERROR, "Could not allocate values buffer.\n");
194  vble_decode_close(avctx);
195  return AVERROR(ENOMEM);
196  }
197 
198  return 0;
199 }
200 
202  .name = "vble",
203  .long_name = NULL_IF_CONFIG_SMALL("VBLE Lossless Codec"),
204  .type = AVMEDIA_TYPE_VIDEO,
205  .id = AV_CODEC_ID_VBLE,
206  .priv_data_size = sizeof(VBLEContext),
208  .close = vble_decode_close,
210  .capabilities = AV_CODEC_CAP_DR1,
211 };
void * av_malloc(size_t size)
Allocate a block of size bytes with alignment suitable for all memory accesses (including vectors if ...
Definition: mem.c:62
#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:140
misc image utilities
static unsigned int get_bits(GetBitContext *s, int n)
Read 1-25 bits.
Definition: get_bits.h:228
#define AV_LOG_WARNING
Something somehow does not look correct.
Definition: log.h:130
AVCodec ff_vble_decoder
Definition: vble.c:201
static int vble_decode_frame(AVCodecContext *avctx, void *data, int *got_frame, AVPacket *avpkt)
Definition: vble.c:116
int size
Definition: avcodec.h:1347
av_log(ac->avr, AV_LOG_TRACE, "%d samples - audio_convert: %s to %s (%s)\, len, av_get_sample_fmt_name(ac->in_fmt), av_get_sample_fmt_name(ac->out_fmt), use_generic ? ac->func_descr_generic :ac->func_descr)
av_cold void ff_huffyuvdsp_init(HuffYUVDSPContext *c)
Definition: huffyuvdsp.c:123
enum AVPixelFormat pix_fmt
Pixel format, see AV_PIX_FMT_xxx.
Definition: avcodec.h:1621
int bits_per_raw_sample
Bits per sample/pixel of internal libavcodec pixel/sample format.
Definition: avcodec.h:2776
int stride
Definition: mace.c:144
AVCodec.
Definition: avcodec.h:3120
void av_freep(void *arg)
Free a memory block which has been allocated with av_malloc(z)() or av_realloc() and set the pointer ...
Definition: mem.c:202
uint8_t
#define av_cold
Definition: attributes.h:66
static int vble_unpack(VBLEContext *ctx, GetBitContext *gb)
Definition: vble.c:63
void(* add_hfyu_median_pred)(uint8_t *dst, const uint8_t *top, const uint8_t *diff, int w, int *left, int *left_top)
Definition: huffyuvdsp.h:27
const char data[16]
Definition: mxf.c:70
uint8_t * data
Definition: avcodec.h:1346
bitstream reader API header.
HuffYUVDSPContext hdsp
Definition: vble.c:38
#define AV_CODEC_FLAG_GRAY
Only decode/encode grayscale.
Definition: avcodec.h:763
#define src
Definition: vp8dsp.c:254
static int get_bits_left(GetBitContext *gb)
Definition: get_bits.h:526
int av_image_get_buffer_size(enum AVPixelFormat pix_fmt, int width, int height, int align)
Return the size in bytes of the amount of data required to store an image with the given parameters...
Definition: imgutils.c:323
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:124
#define AVERROR(e)
Definition: error.h:43
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
Definition: internal.h:148
int flags
AV_CODEC_FLAG_*.
Definition: avcodec.h:1503
const char * name
Name of the codec implementation.
Definition: avcodec.h:3127
static void vble_restore_plane(VBLEContext *ctx, AVFrame *pic, int plane, int offset, int width, int height)
Definition: vble.c:88
enum AVPictureType pict_type
Picture type of the frame.
Definition: frame.h:201
int width
picture width / height.
Definition: avcodec.h:1580
static int decode(AVCodecContext *avctx, AVFrame *frame, int *got_frame, AVPacket *pkt)
Definition: avconv.c:1288
AVFormatContext * ctx
Definition: movenc.c:48
static unsigned int show_bits(GetBitContext *s, int n)
Show 1-25 bits.
Definition: get_bits.h:250
#define AV_RL32
Definition: intreadwrite.h:146
int size
Definition: vble.c:40
static int width
Definition: utils.c:156
Libavcodec external API header.
version
Definition: ffv1enc.c:1091
int linesize[AV_NUM_DATA_POINTERS]
For video, size in bytes of each picture line.
Definition: frame.h:158
main external API structure.
Definition: avcodec.h:1409
int ff_get_buffer(AVCodecContext *avctx, AVFrame *frame, int flags)
Get a buffer for a frame.
Definition: utils.c:589
static av_cold int vble_decode_close(AVCodecContext *avctx)
Definition: vble.c:168
static unsigned int get_bits1(GetBitContext *s)
Definition: get_bits.h:267
static void skip_bits(GetBitContext *s, int n)
Definition: get_bits.h:259
uint8_t * val
Definition: vble.c:41
static uint8_t vble_read_reverse_unary(GetBitContext *gb)
Definition: vble.c:44
static int init_get_bits(GetBitContext *s, const uint8_t *buffer, int bit_size)
Initialize GetBitContext.
Definition: get_bits.h:362
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
Definition: frame.h:146
int height
Definition: gxfenc.c:72
planar YUV 4:2:0, 12bpp, (1 Cr & Cb sample per 2x2 Y samples)
Definition: pixfmt.h:59
common internal api header.
AVCodecContext * avctx
Definition: vble.c:37
static av_cold int init(AVCodecParserContext *s)
Definition: h264_parser.c:582
void * priv_data
Definition: avcodec.h:1451
#define av_log2_16bit
Definition: intmath.h:86
int key_frame
1 -> keyframe, 0-> not
Definition: frame.h:196
static av_cold int vble_decode_init(AVCodecContext *avctx)
Definition: vble.c:176
const uint8_t ff_reverse[256]
Definition: mathtables.c:72
This structure stores compressed data.
Definition: avcodec.h:1323
#define AV_CODEC_CAP_DR1
Codec uses get_buffer() for allocating buffers and supports custom allocators.
Definition: avcodec.h:838