3 * Copyright (c) 2011 Derek Buitenhuis
5 * This file is part of Libav.
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.
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.
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
27 #include "libavutil/imgutils.h"
29 #define BITSTREAM_READER_LE
31 #include "bitstream.h"
32 #include "huffyuvdsp.h"
36 typedef struct VBLEContext {
37 AVCodecContext *avctx;
38 HuffYUVDSPContext hdsp;
41 uint8_t *val; /* First holds the lengths of vlc symbols and then their values */
44 static uint8_t vble_read_reverse_unary(BitstreamContext *bc)
46 /* At most we need to read 9 bits total to get indices up to 8 */
47 uint8_t val = bitstream_peek(bc, 8);
50 val = 7 - av_log2_16bit(ff_reverse[val]);
51 bitstream_skip(bc, val + 1);
54 bitstream_skip(bc, 8);
55 if (bitstream_read_bit(bc))
59 /* Return something larger than 8 on error */
63 static int vble_unpack(VBLEContext *ctx, BitstreamContext *bc)
67 /* Read all the lengths in first */
68 for (i = 0; i < ctx->size; i++) {
69 ctx->val[i] = vble_read_reverse_unary(bc);
71 if (ctx->val[i] == UINT8_MAX)
75 for (i = 0; i < ctx->size; i++) {
76 /* Check we have enough bits left */
77 if (bitstream_bits_left(bc) < ctx->val[i])
80 /* get_bits can't take a length of 0 */
82 ctx->val[i] = (1 << ctx->val[i]) + bitstream_read(bc, ctx->val[i]) - 1;
88 static void vble_restore_plane(VBLEContext *ctx, AVFrame *pic,
89 int plane, int offset,
90 int width, int height)
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;
97 for (i = 0; i < height; i++) {
98 for (j = 0; j < width; j++)
99 val[j] = (val[j] >> 1) ^ -(val[j] & 1);
103 left_top = dst[-stride];
104 ctx->hdsp.add_hfyu_median_pred(dst, dst - stride, val,
105 width, &left, &left_top);
108 for (j = 1; j < width; j++)
109 dst[j] = val[j] + dst[j - 1];
116 static int vble_decode_frame(AVCodecContext *avctx, void *data, int *got_frame,
119 VBLEContext *ctx = avctx->priv_data;
122 const uint8_t *src = avpkt->data;
125 int width_uv = avctx->width / 2, height_uv = avctx->height / 2;
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);
135 pic->pict_type = AV_PICTURE_TYPE_I;
137 /* Version should always be 1 */
138 version = AV_RL32(src);
141 av_log(avctx, AV_LOG_WARNING, "Unsupported VBLE Version: %d\n", version);
143 bitstream_init8(&bc, src + 4, avpkt->size - 4);
146 if (vble_unpack(ctx, &bc) < 0) {
147 av_log(avctx, AV_LOG_ERROR, "Invalid Code\n");
148 return AVERROR_INVALIDDATA;
151 /* Restore planes. Should be almost identical to Huffyuv's. */
152 vble_restore_plane(ctx, pic, 0, offset, avctx->width, avctx->height);
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);
159 offset += width_uv * height_uv;
160 vble_restore_plane(ctx, pic, 2, offset, width_uv, height_uv);
168 static av_cold int vble_decode_close(AVCodecContext *avctx)
170 VBLEContext *ctx = avctx->priv_data;
176 static av_cold int vble_decode_init(AVCodecContext *avctx)
178 VBLEContext *ctx = avctx->priv_data;
180 /* Stash for later use */
182 ff_huffyuvdsp_init(&ctx->hdsp);
184 avctx->pix_fmt = AV_PIX_FMT_YUV420P;
185 avctx->bits_per_raw_sample = 8;
187 ctx->size = av_image_get_buffer_size(avctx->pix_fmt,
188 avctx->width, avctx->height, 1);
190 ctx->val = av_malloc(ctx->size * sizeof(*ctx->val));
193 av_log(avctx, AV_LOG_ERROR, "Could not allocate values buffer.\n");
194 vble_decode_close(avctx);
195 return AVERROR(ENOMEM);
201 AVCodec ff_vble_decoder = {
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),
207 .init = vble_decode_init,
208 .close = vble_decode_close,
209 .decode = vble_decode_frame,
210 .capabilities = AV_CODEC_CAP_DR1,
211 .caps_internal = FF_CODEC_CAP_INIT_THREADSAFE,