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
34 AVCodecContext *avctx;
38 uint8_t *val; /* First holds the lengths of vlc symbols and then their values */
41 static uint8_t vble_read_reverse_unary(GetBitContext *gb)
43 /* At most we need to read 9 bits total to get indices up to 8 */
44 uint8_t val = show_bits(gb, 8);
47 val = 7 - av_log2_16bit(av_reverse[val]);
48 skip_bits(gb, val + 1);
56 /* Return something larger than 8 on error */
60 static int vble_unpack(VBLEContext *ctx, GetBitContext *gb)
64 /* Read all the lengths in first */
65 for (i = 0; i < ctx->size; i++) {
66 ctx->val[i] = vble_read_reverse_unary(gb);
68 if (ctx->val[i] == UINT8_MAX)
72 for (i = 0; i < ctx->size; i++) {
73 /* Check we have enough bits left */
74 if (get_bits_left(gb) < ctx->val[i])
77 /* get_bits can't take a length of 0 */
79 ctx->val[i] = (1 << ctx->val[i]) + get_bits(gb, ctx->val[i]) - 1;
85 static void vble_restore_plane(VBLEContext *ctx, int plane, int offset,
86 int width, int height)
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;
94 for (i = 0; i < height; i++) {
95 for (j = 0; j < width; j++)
96 val[j] = (val[j] >> 1) ^ -(val[j] & 1);
100 left_top = dst[-stride];
101 ctx->dsp.add_hfyu_median_prediction(dst, dst-stride, val,
102 width, &left, &left_top);
105 for (j = 1; j < width; j++)
106 dst[j] = val[j] + dst[j - 1];
113 static int vble_decode_frame(AVCodecContext *avctx, void *data, int *data_size,
116 VBLEContext *ctx = avctx->priv_data;
117 AVFrame *pic = avctx->coded_frame;
119 const uint8_t *src = avpkt->data;
122 int width_uv = avctx->width / 2, height_uv = avctx->height / 2;
126 /* Clear buffer if need be */
128 avctx->release_buffer(avctx, pic);
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);
138 pic->pict_type = AV_PICTURE_TYPE_I;
140 /* Version should always be 1 */
141 version = AV_RL32(src);
144 av_log(avctx, AV_LOG_ERROR, "Unsupported VBLE Version: %d\n", version);
145 return AVERROR_INVALIDDATA;
148 init_get_bits(&gb, src + 4, (avpkt->size - 4) * 8);
151 if (vble_unpack(ctx, &gb) < 0) {
152 av_log(avctx, AV_LOG_ERROR, "Invalid Code\n");
153 return AVERROR_INVALIDDATA;
156 /* Restore planes. Should be almost identical to Huffyuv's. */
157 vble_restore_plane(ctx, 0, offset, avctx->width, avctx->height);
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);
164 offset += width_uv * height_uv;
165 vble_restore_plane(ctx, 2, offset, width_uv, height_uv);
168 *data_size = sizeof(AVFrame);
169 *(AVFrame *)data = *pic;
174 static av_cold int vble_decode_close(AVCodecContext *avctx)
176 VBLEContext *ctx = avctx->priv_data;
177 AVFrame *pic = avctx->coded_frame;
180 avctx->release_buffer(avctx, pic);
182 av_freep(&avctx->coded_frame);
188 static av_cold int vble_decode_init(AVCodecContext *avctx)
190 VBLEContext *ctx = avctx->priv_data;
192 /* Stash for later use */
194 ff_dsputil_init(&ctx->dsp, avctx);
196 avctx->pix_fmt = PIX_FMT_YUV420P;
197 avctx->bits_per_raw_sample = 8;
198 avctx->coded_frame = avcodec_alloc_frame();
200 if (!avctx->coded_frame) {
201 av_log(avctx, AV_LOG_ERROR, "Could not allocate frame.\n");
202 return AVERROR(ENOMEM);
205 ctx->size = avpicture_get_size(avctx->pix_fmt,
206 avctx->width, avctx->height);
208 ctx->val = av_malloc(ctx->size * sizeof(*ctx->val));
211 av_log(avctx, AV_LOG_ERROR, "Could not allocate values buffer.\n");
212 vble_decode_close(avctx);
213 return AVERROR(ENOMEM);
219 AVCodec ff_vble_decoder = {
221 .type = AVMEDIA_TYPE_VIDEO,
223 .priv_data_size = sizeof(VBLEContext),
224 .init = vble_decode_init,
225 .close = vble_decode_close,
226 .decode = vble_decode_frame,
227 .capabilities = CODEC_CAP_DR1,
228 .long_name = NULL_IF_CONFIG_SMALL("VBLE Lossless Codec"),