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 #define BITSTREAM_READER_LE
28 
29 #include "avcodec.h"
30 #include "dsputil.h"
31 #include "get_bits.h"
32 
33 typedef struct {
36 
37  int size;
38  uint8_t *val; /* First holds the lengths of vlc symbols and then their values */
39 } VBLEContext;
40 
42 {
43  /* At most we need to read 9 bits total to get indices up to 8 */
44  uint8_t val = show_bits(gb, 8);
45 
46  if (val) {
47  val = 7 - av_log2_16bit(av_reverse[val]);
48  skip_bits(gb, val + 1);
49  return val;
50  } else {
51  skip_bits(gb, 8);
52  if (get_bits1(gb))
53  return 8;
54  }
55 
56  /* Return something larger than 8 on error */
57  return UINT8_MAX;
58 }
59 
60 static int vble_unpack(VBLEContext *ctx, GetBitContext *gb)
61 {
62  int i;
63 
64  /* Read all the lengths in first */
65  for (i = 0; i < ctx->size; i++) {
66  ctx->val[i] = vble_read_reverse_unary(gb);
67 
68  if (ctx->val[i] == UINT8_MAX)
69  return -1;
70  }
71 
72  for (i = 0; i < ctx->size; i++) {
73  /* Check we have enough bits left */
74  if (get_bits_left(gb) < ctx->val[i])
75  return -1;
76 
77  /* get_bits can't take a length of 0 */
78  if (ctx->val[i])
79  ctx->val[i] = (1 << ctx->val[i]) + get_bits(gb, ctx->val[i]) - 1;
80  }
81 
82  return 0;
83 }
84 
85 static void vble_restore_plane(VBLEContext *ctx, int plane, int offset,
86  int width, int height)
87 {
88  AVFrame *pic = ctx->avctx->coded_frame;
89  uint8_t *dst = pic->data[plane];
90  uint8_t *val = ctx->val + offset;
91  int stride = pic->linesize[plane];
92  int i, j, left, left_top;
93 
94  for (i = 0; i < height; i++) {
95  for (j = 0; j < width; j++)
96  val[j] = (val[j] >> 1) ^ -(val[j] & 1);
97 
98  if (i) {
99  left = 0;
100  left_top = dst[-stride];
101  ctx->dsp.add_hfyu_median_prediction(dst, dst-stride, val,
102  width, &left, &left_top);
103  } else {
104  dst[0] = val[0];
105  for (j = 1; j < width; j++)
106  dst[j] = val[j] + dst[j - 1];
107  }
108  dst += stride;
109  val += width;
110  }
111 }
112 
113 static int vble_decode_frame(AVCodecContext *avctx, void *data, int *data_size,
114  AVPacket *avpkt)
115 {
116  VBLEContext *ctx = avctx->priv_data;
117  AVFrame *pic = avctx->coded_frame;
118  GetBitContext gb;
119  const uint8_t *src = avpkt->data;
120  int version;
121  int offset = 0;
122  int width_uv = avctx->width / 2, height_uv = avctx->height / 2;
123 
124  pic->reference = 0;
125 
126  /* Clear buffer if need be */
127  if (pic->data[0])
128  avctx->release_buffer(avctx, pic);
129 
130  /* Allocate buffer */
131  if (avctx->get_buffer(avctx, pic) < 0) {
132  av_log(avctx, AV_LOG_ERROR, "Could not allocate buffer.\n");
133  return AVERROR(ENOMEM);
134  }
135 
136  /* Set flags */
137  pic->key_frame = 1;
139 
140  /* Version should always be 1 */
141  version = AV_RL32(src);
142 
143  if (version != 1) {
144  av_log(avctx, AV_LOG_ERROR, "Unsupported VBLE Version: %d\n", version);
145  return AVERROR_INVALIDDATA;
146  }
147 
148  init_get_bits(&gb, src + 4, (avpkt->size - 4) * 8);
149 
150  /* Unpack */
151  if (vble_unpack(ctx, &gb) < 0) {
152  av_log(avctx, AV_LOG_ERROR, "Invalid Code\n");
153  return AVERROR_INVALIDDATA;
154  }
155 
156  /* Restore planes. Should be almost identical to Huffyuv's. */
157  vble_restore_plane(ctx, 0, offset, avctx->width, avctx->height);
158 
159  /* Chroma */
160  if (!(ctx->avctx->flags & CODEC_FLAG_GRAY)) {
161  offset += avctx->width * avctx->height;
162  vble_restore_plane(ctx, 1, offset, width_uv, height_uv);
163 
164  offset += width_uv * height_uv;
165  vble_restore_plane(ctx, 2, offset, width_uv, height_uv);
166  }
167 
168  *data_size = sizeof(AVFrame);
169  *(AVFrame *)data = *pic;
170 
171  return avpkt->size;
172 }
173 
175 {
176  VBLEContext *ctx = avctx->priv_data;
177  AVFrame *pic = avctx->coded_frame;
178 
179  if (pic->data[0])
180  avctx->release_buffer(avctx, pic);
181 
182  av_freep(&avctx->coded_frame);
183  av_freep(&ctx->val);
184 
185  return 0;
186 }
187 
189 {
190  VBLEContext *ctx = avctx->priv_data;
191 
192  /* Stash for later use */
193  ctx->avctx = avctx;
194  dsputil_init(&ctx->dsp, avctx);
195 
196  avctx->pix_fmt = PIX_FMT_YUV420P;
197  avctx->bits_per_raw_sample = 8;
198  avctx->coded_frame = avcodec_alloc_frame();
199 
200  if (!avctx->coded_frame) {
201  av_log(avctx, AV_LOG_ERROR, "Could not allocate frame.\n");
202  return AVERROR(ENOMEM);
203  }
204 
205  ctx->size = avpicture_get_size(avctx->pix_fmt,
206  avctx->width, avctx->height);
207 
208  ctx->val = av_malloc(ctx->size * sizeof(*ctx->val));
209 
210  if (!ctx->val) {
211  av_log(avctx, AV_LOG_ERROR, "Could not allocate values buffer.\n");
212  vble_decode_close(avctx);
213  return AVERROR(ENOMEM);
214  }
215 
216  return 0;
217 }
218 
220  .name = "vble",
221  .type = AVMEDIA_TYPE_VIDEO,
222  .id = CODEC_ID_VBLE,
223  .priv_data_size = sizeof(VBLEContext),
227  .capabilities = CODEC_CAP_DR1,
228  .long_name = NULL_IF_CONFIG_SMALL("VBLE Lossless Codec"),
229 };
int(* get_buffer)(struct AVCodecContext *c, AVFrame *pic)
Called at the beginning of each frame to get a buffer for it.
Definition: avcodec.h:1726
const uint8_t av_reverse[256]
Definition: mathematics.c:52
enum PixelFormat pix_fmt
Pixel format, see PIX_FMT_xxx.
Definition: avcodec.h:1426
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition: error.h:54
Audio Video Frame.
Definition: avcodec.h:985
void(* release_buffer)(struct AVCodecContext *c, AVFrame *pic)
Called to release buffers which were allocated with get_buffer.
Definition: avcodec.h:1737
static unsigned int get_bits(GetBitContext *s, int n)
Read 1-25 bits.
Definition: get_bits.h:237
AVFrame * coded_frame
the picture in the bitstream
Definition: avcodec.h:2000
AVCodec ff_vble_decoder
Definition: vble.c:219
int size
Definition: avcodec.h:909
int width
Definition: rotozoom.c:164
int bits_per_raw_sample
Bits per sample/pixel of internal libavcodec pixel/sample format.
Definition: avcodec.h:2863
int stride
Definition: mace.c:143
AVCodec.
Definition: avcodec.h:3189
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:147
static int decode(MimicContext *ctx, int quality, int num_coeffs, int is_iframe)
Definition: mimic.c:228
planar YUV 4:2:0, 12bpp, (1 Cr & Cb sample per 2x2 Y samples)
Definition: pixfmt.h:64
#define av_cold
Definition: attributes.h:71
static int vble_unpack(VBLEContext *ctx, GetBitContext *gb)
Definition: vble.c:60
const char data[16]
Definition: mxf.c:60
uint8_t * data
Definition: avcodec.h:908
bitstream reader API header.
int avpicture_get_size(enum PixelFormat pix_fmt, int width, int height)
Calculate the size in bytes that a picture of the given width and height would occupy if stored in th...
Definition: imgconvert.c:476
av_cold void dsputil_init(DSPContext *c, AVCodecContext *avctx)
Definition: dsputil.c:2789
static int init(AVCodecParserContext *s)
Definition: h264_parser.c:336
#define CODEC_FLAG_GRAY
Only decode/encode grayscale.
Definition: avcodec.h:640
#define CODEC_CAP_DR1
Codec uses get_buffer() for allocating buffers and supports custom allocators.
Definition: avcodec.h:719
static int get_bits_left(GetBitContext *gb)
Definition: get_bits.h:513
int reference
is this picture used as reference The values for this are the same as the MpegEncContext.picture_structure variable, that is 1->top field, 2->bottom field, 3->frame/both fields.
Definition: avcodec.h:1074
#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:191
AV_RL32
Definition: bytestream.h:89
int flags
CODEC_FLAG_*.
Definition: avcodec.h:1355
void av_log(void *avcl, int level, const char *fmt,...)
Definition: log.c:140
const char * name
Name of the codec implementation.
Definition: avcodec.h:3196
AVFrame * avcodec_alloc_frame(void)
Allocate an AVFrame and set its fields to default values.
Definition: utils.c:618
void(* add_hfyu_median_prediction)(uint8_t *dst, const uint8_t *top, const uint8_t *diff, int w, int *left, int *left_top)
Definition: dsputil.h:388
enum AVPictureType pict_type
Picture type of the frame, see ?_TYPE below.
Definition: avcodec.h:1029
static void vble_restore_plane(VBLEContext *ctx, int plane, int offset, int width, int height)
Definition: vble.c:85
int width
picture width / height.
Definition: avcodec.h:1408
struct AVFrame AVFrame
Audio Video Frame.
static unsigned int show_bits(GetBitContext *s, int n)
Show 1-25 bits.
Definition: get_bits.h:251
version
Definition: libspeexenc.c:304
int size
Definition: vble.c:37
external API header
int linesize[AV_NUM_DATA_POINTERS]
Size, in bytes, of the data for each picture/channel plane.
Definition: avcodec.h:1008
main external API structure.
Definition: avcodec.h:1329
static void close(AVCodecParserContext *s)
Definition: h264_parser.c:327
static int vble_decode_frame(AVCodecContext *avctx, void *data, int *data_size, AVPacket *avpkt)
Definition: vble.c:113
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:111
static av_cold int vble_decode_close(AVCodecContext *avctx)
Definition: vble.c:174
static unsigned int get_bits1(GetBitContext *s)
Definition: get_bits.h:268
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:64
static void skip_bits(GetBitContext *s, int n)
Definition: get_bits.h:260
uint8_t * val
Definition: vble.c:38
static uint8_t vble_read_reverse_unary(GetBitContext *gb)
Definition: vble.c:41
static int init_get_bits(GetBitContext *s, const uint8_t *buffer, int bit_size)
Initialize GetBitContext.
Definition: get_bits.h:354
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
Definition: avcodec.h:997
int height
Definition: gxfenc.c:73
AVCodecContext * avctx
Definition: vble.c:34
DSP utils.
void * priv_data
Definition: avcodec.h:1531
int key_frame
1 -> keyframe, 0-> not
Definition: avcodec.h:1022
static av_cold int vble_decode_init(AVCodecContext *avctx)
Definition: vble.c:188
DSPContext.
Definition: dsputil.h:226
DSPContext dsp
Definition: vble.c:35