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 #define BITSTREAM_READER_LE
29 #include "libavutil/imgutils.h"
33 #include "huffyuvdsp.h"
37 typedef struct VBLEContext {
38 AVCodecContext *avctx;
39 HuffYUVDSPContext hdsp;
42 uint8_t *val; /* First holds the lengths of vlc symbols and then their values */
45 static uint8_t vble_read_reverse_unary(GetBitContext *gb)
47 /* At most we need to read 9 bits total to get indices up to 8 */
48 uint8_t val = show_bits(gb, 8);
51 val = 7 - av_log2_16bit(ff_reverse[val]);
52 skip_bits(gb, val + 1);
60 /* Return something larger than 8 on error */
64 static int vble_unpack(VBLEContext *ctx, GetBitContext *gb)
68 /* Read all the lengths in first */
69 for (i = 0; i < ctx->size; i++) {
70 ctx->val[i] = vble_read_reverse_unary(gb);
72 if (ctx->val[i] == UINT8_MAX)
76 for (i = 0; i < ctx->size; i++) {
77 /* Check we have enough bits left */
78 if (get_bits_left(gb) < ctx->val[i])
81 /* get_bits can't take a length of 0 */
83 ctx->val[i] = (1 << ctx->val[i]) + get_bits(gb, ctx->val[i]) - 1;
89 static void vble_restore_plane(VBLEContext *ctx, AVFrame *pic,
90 int plane, int offset,
91 int width, int height)
93 uint8_t *dst = pic->data[plane];
94 uint8_t *val = ctx->val + offset;
95 int stride = pic->linesize[plane];
96 int i, j, left, left_top;
98 for (i = 0; i < height; i++) {
99 for (j = 0; j < width; j++)
100 val[j] = (val[j] >> 1) ^ -(val[j] & 1);
104 left_top = dst[-stride];
105 ctx->hdsp.add_hfyu_median_pred(dst, dst - stride, val,
106 width, &left, &left_top);
109 for (j = 1; j < width; j++)
110 dst[j] = val[j] + dst[j - 1];
117 static int vble_decode_frame(AVCodecContext *avctx, void *data, int *got_frame,
120 VBLEContext *ctx = avctx->priv_data;
123 const uint8_t *src = avpkt->data;
126 int width_uv = avctx->width / 2, height_uv = avctx->height / 2;
128 /* Allocate buffer */
129 if (ff_get_buffer(avctx, pic, 0) < 0) {
130 av_log(avctx, AV_LOG_ERROR, "Could not allocate buffer.\n");
131 return AVERROR(ENOMEM);
136 pic->pict_type = AV_PICTURE_TYPE_I;
138 /* Version should always be 1 */
139 version = AV_RL32(src);
142 av_log(avctx, AV_LOG_WARNING, "Unsupported VBLE Version: %d\n", version);
144 init_get_bits(&gb, src + 4, (avpkt->size - 4) * 8);
147 if (vble_unpack(ctx, &gb) < 0) {
148 av_log(avctx, AV_LOG_ERROR, "Invalid Code\n");
149 return AVERROR_INVALIDDATA;
152 /* Restore planes. Should be almost identical to Huffyuv's. */
153 vble_restore_plane(ctx, pic, 0, offset, avctx->width, avctx->height);
156 if (!(ctx->avctx->flags & AV_CODEC_FLAG_GRAY)) {
157 offset += avctx->width * avctx->height;
158 vble_restore_plane(ctx, pic, 1, offset, width_uv, height_uv);
160 offset += width_uv * height_uv;
161 vble_restore_plane(ctx, pic, 2, offset, width_uv, height_uv);
169 static av_cold int vble_decode_close(AVCodecContext *avctx)
171 VBLEContext *ctx = avctx->priv_data;
177 static av_cold int vble_decode_init(AVCodecContext *avctx)
179 VBLEContext *ctx = avctx->priv_data;
181 /* Stash for later use */
183 ff_huffyuvdsp_init(&ctx->hdsp);
185 avctx->pix_fmt = AV_PIX_FMT_YUV420P;
186 avctx->bits_per_raw_sample = 8;
188 ctx->size = av_image_get_buffer_size(avctx->pix_fmt,
189 avctx->width, avctx->height, 1);
191 ctx->val = av_malloc(ctx->size * sizeof(*ctx->val));
194 av_log(avctx, AV_LOG_ERROR, "Could not allocate values buffer.\n");
195 vble_decode_close(avctx);
196 return AVERROR(ENOMEM);
202 AVCodec ff_vble_decoder = {
204 .long_name = NULL_IF_CONFIG_SMALL("VBLE Lossless Codec"),
205 .type = AVMEDIA_TYPE_VIDEO,
206 .id = AV_CODEC_ID_VBLE,
207 .priv_data_size = sizeof(VBLEContext),
208 .init = vble_decode_init,
209 .close = vble_decode_close,
210 .decode = vble_decode_frame,
211 .capabilities = AV_CODEC_CAP_DR1,