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 #define BITSTREAM_READER_LE
28 
29 #include "avcodec.h"
30 #include "get_bits.h"
31 #include "huffyuvdsp.h"
32 #include "internal.h"
33 #include "mathops.h"
34 
35 typedef struct {
38 
39  int size;
40  uint8_t *val; /* First holds the lengths of vlc symbols and then their values */
41 } VBLEContext;
42 
44 {
45  /* At most we need to read 9 bits total to get indices up to 8 */
46  uint8_t val = show_bits(gb, 8);
47 
48  if (val) {
49  val = 7 - av_log2_16bit(ff_reverse[val]);
50  skip_bits(gb, val + 1);
51  return val;
52  } else {
53  skip_bits(gb, 8);
54  if (get_bits1(gb))
55  return 8;
56  }
57 
58  /* Return something larger than 8 on error */
59  return UINT8_MAX;
60 }
61 
62 static int vble_unpack(VBLEContext *ctx, GetBitContext *gb)
63 {
64  int i;
65 
66  /* Read all the lengths in first */
67  for (i = 0; i < ctx->size; i++) {
68  ctx->val[i] = vble_read_reverse_unary(gb);
69 
70  if (ctx->val[i] == UINT8_MAX)
71  return -1;
72  }
73 
74  for (i = 0; i < ctx->size; i++) {
75  /* Check we have enough bits left */
76  if (get_bits_left(gb) < ctx->val[i])
77  return -1;
78 
79  /* get_bits can't take a length of 0 */
80  if (ctx->val[i])
81  ctx->val[i] = (1 << ctx->val[i]) + get_bits(gb, ctx->val[i]) - 1;
82  }
83 
84  return 0;
85 }
86 
87 static void vble_restore_plane(VBLEContext *ctx, AVFrame *pic,
88  int plane, int offset,
89  int width, int height)
90 {
91  uint8_t *dst = pic->data[plane];
92  uint8_t *val = ctx->val + offset;
93  int stride = pic->linesize[plane];
94  int i, j, left, left_top;
95 
96  for (i = 0; i < height; i++) {
97  for (j = 0; j < width; j++)
98  val[j] = (val[j] >> 1) ^ -(val[j] & 1);
99 
100  if (i) {
101  left = 0;
102  left_top = dst[-stride];
103  ctx->hdsp.add_hfyu_median_pred(dst, dst - stride, val,
104  width, &left, &left_top);
105  } else {
106  dst[0] = val[0];
107  for (j = 1; j < width; j++)
108  dst[j] = val[j] + dst[j - 1];
109  }
110  dst += stride;
111  val += width;
112  }
113 }
114 
115 static int vble_decode_frame(AVCodecContext *avctx, void *data, int *got_frame,
116  AVPacket *avpkt)
117 {
118  VBLEContext *ctx = avctx->priv_data;
119  AVFrame *pic = data;
120  GetBitContext gb;
121  const uint8_t *src = avpkt->data;
122  int version;
123  int offset = 0;
124  int width_uv = avctx->width / 2, height_uv = avctx->height / 2;
125 
126  /* Allocate buffer */
127  if (ff_get_buffer(avctx, pic, 0) < 0) {
128  av_log(avctx, AV_LOG_ERROR, "Could not allocate buffer.\n");
129  return AVERROR(ENOMEM);
130  }
131 
132  /* Set flags */
133  pic->key_frame = 1;
135 
136  /* Version should always be 1 */
137  version = AV_RL32(src);
138 
139  if (version != 1)
140  av_log(avctx, AV_LOG_WARNING, "Unsupported VBLE Version: %d\n", version);
141 
142  init_get_bits(&gb, src + 4, (avpkt->size - 4) * 8);
143 
144  /* Unpack */
145  if (vble_unpack(ctx, &gb) < 0) {
146  av_log(avctx, AV_LOG_ERROR, "Invalid Code\n");
147  return AVERROR_INVALIDDATA;
148  }
149 
150  /* Restore planes. Should be almost identical to Huffyuv's. */
151  vble_restore_plane(ctx, pic, 0, offset, avctx->width, avctx->height);
152 
153  /* Chroma */
154  if (!(ctx->avctx->flags & CODEC_FLAG_GRAY)) {
155  offset += avctx->width * avctx->height;
156  vble_restore_plane(ctx, pic, 1, offset, width_uv, height_uv);
157 
158  offset += width_uv * height_uv;
159  vble_restore_plane(ctx, pic, 2, offset, width_uv, height_uv);
160  }
161 
162  *got_frame = 1;
163 
164  return avpkt->size;
165 }
166 
168 {
169  VBLEContext *ctx = avctx->priv_data;
170  av_freep(&ctx->val);
171 
172  return 0;
173 }
174 
176 {
177  VBLEContext *ctx = avctx->priv_data;
178 
179  /* Stash for later use */
180  ctx->avctx = avctx;
181  ff_huffyuvdsp_init(&ctx->hdsp);
182 
183  avctx->pix_fmt = AV_PIX_FMT_YUV420P;
184  avctx->bits_per_raw_sample = 8;
185 
186  ctx->size = avpicture_get_size(avctx->pix_fmt,
187  avctx->width, avctx->height);
188 
189  ctx->val = av_malloc(ctx->size * sizeof(*ctx->val));
190 
191  if (!ctx->val) {
192  av_log(avctx, AV_LOG_ERROR, "Could not allocate values buffer.\n");
193  vble_decode_close(avctx);
194  return AVERROR(ENOMEM);
195  }
196 
197  return 0;
198 }
199 
201  .name = "vble",
202  .long_name = NULL_IF_CONFIG_SMALL("VBLE Lossless Codec"),
203  .type = AVMEDIA_TYPE_VIDEO,
204  .id = AV_CODEC_ID_VBLE,
205  .priv_data_size = sizeof(VBLEContext),
209  .capabilities = CODEC_CAP_DR1,
210 };
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:135
static unsigned int get_bits(GetBitContext *s, int n)
Read 1-25 bits.
Definition: get_bits.h:240
#define AV_LOG_WARNING
Something somehow does not look correct.
Definition: log.h:129
AVCodec ff_vble_decoder
Definition: vble.c:200
static int vble_decode_frame(AVCodecContext *avctx, void *data, int *got_frame, AVPacket *avpkt)
Definition: vble.c:115
int size
Definition: avcodec.h:974
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:1270
int bits_per_raw_sample
Bits per sample/pixel of internal libavcodec pixel/sample format.
Definition: avcodec.h:2530
int stride
Definition: mace.c:144
AVCodec.
Definition: avcodec.h:2812
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:198
static int decode(MimicContext *ctx, int quality, int num_coeffs, int is_iframe)
Definition: mimic.c:275
uint8_t
#define av_cold
Definition: attributes.h:66
static int vble_unpack(VBLEContext *ctx, GetBitContext *gb)
Definition: vble.c:62
#define CODEC_CAP_DR1
Codec uses get_buffer() for allocating buffers and supports custom allocators.
Definition: avcodec.h:684
const char data[16]
Definition: mxf.c:70
uint8_t * data
Definition: avcodec.h:973
bitstream reader API header.
HuffYUVDSPContext hdsp
Definition: vble.c:37
static int get_bits_left(GetBitContext *gb)
Definition: get_bits.h:555
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:123
#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:145
int flags
CODEC_FLAG_*.
Definition: avcodec.h:1144
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
static void vble_restore_plane(VBLEContext *ctx, AVFrame *pic, int plane, int offset, int width, int height)
Definition: vble.c:87
enum AVPictureType pict_type
Picture type of the frame.
Definition: frame.h:196
int width
picture width / height.
Definition: avcodec.h:1229
static unsigned int show_bits(GetBitContext *s, int n)
Show 1-25 bits.
Definition: get_bits.h:254
#define AV_RL32
Definition: intreadwrite.h:146
int size
Definition: vble.c:39
static int width
Definition: utils.c:156
Libavcodec external API header.
version
Definition: ffv1enc.c:1080
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
static void close(AVCodecParserContext *s)
Definition: h264_parser.c:490
int ff_get_buffer(AVCodecContext *avctx, AVFrame *frame, int flags)
Get a buffer for a frame.
Definition: utils.c:612
static av_cold int vble_decode_close(AVCodecContext *avctx)
Definition: vble.c:167
static unsigned int get_bits1(GetBitContext *s)
Definition: get_bits.h:271
static void skip_bits(GetBitContext *s, int n)
Definition: get_bits.h:263
uint8_t * val
Definition: vble.c:40
static uint8_t vble_read_reverse_unary(GetBitContext *gb)
Definition: vble.c:43
static int init_get_bits(GetBitContext *s, const uint8_t *buffer, int bit_size)
Initialize GetBitContext.
Definition: get_bits.h:375
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
Definition: frame.h:141
int height
Definition: gxfenc.c:72
planar YUV 4:2:0, 12bpp, (1 Cr & Cb sample per 2x2 Y samples)
Definition: pixfmt.h:65
common internal api header.
#define CODEC_FLAG_GRAY
Only decode/encode grayscale.
Definition: avcodec.h:637
AVCodecContext * avctx
Definition: vble.c:36
static av_cold int init(AVCodecParserContext *s)
Definition: h264_parser.c:499
void * priv_data
Definition: avcodec.h:1092
#define av_log2_16bit
Definition: intmath.h:86
int avpicture_get_size(enum AVPixelFormat 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: avpicture.c:85
int key_frame
1 -> keyframe, 0-> not
Definition: frame.h:191
static av_cold int vble_decode_init(AVCodecContext *avctx)
Definition: vble.c:175
const uint8_t ff_reverse[256]
Definition: mathtables.c:72
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
This structure stores compressed data.
Definition: avcodec.h:950