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
35 AVCodecContext *avctx;
39 uint8_t *val; /* First holds the lengths of vlc symbols and then their values */
42 static uint8_t vble_read_reverse_unary(GetBitContext *gb)
44 /* At most we need to read 9 bits total to get indices up to 8 */
45 uint8_t val = show_bits(gb, 8);
48 val = 7 - av_log2_16bit(ff_reverse[val]);
49 skip_bits(gb, val + 1);
57 /* Return something larger than 8 on error */
61 static int vble_unpack(VBLEContext *ctx, GetBitContext *gb)
65 /* Read all the lengths in first */
66 for (i = 0; i < ctx->size; i++) {
67 ctx->val[i] = vble_read_reverse_unary(gb);
69 if (ctx->val[i] == UINT8_MAX)
73 for (i = 0; i < ctx->size; i++) {
74 /* Check we have enough bits left */
75 if (get_bits_left(gb) < ctx->val[i])
78 /* get_bits can't take a length of 0 */
80 ctx->val[i] = (1 << ctx->val[i]) + get_bits(gb, ctx->val[i]) - 1;
86 static void vble_restore_plane(VBLEContext *ctx, int plane, int offset,
87 int width, int height)
89 AVFrame *pic = ctx->avctx->coded_frame;
90 uint8_t *dst = pic->data[plane];
91 uint8_t *val = ctx->val + offset;
92 int stride = pic->linesize[plane];
93 int i, j, left, left_top;
95 for (i = 0; i < height; i++) {
96 for (j = 0; j < width; j++)
97 val[j] = (val[j] >> 1) ^ -(val[j] & 1);
101 left_top = dst[-stride];
102 ctx->dsp.add_hfyu_median_prediction(dst, dst-stride, val,
103 width, &left, &left_top);
106 for (j = 1; j < width; j++)
107 dst[j] = val[j] + dst[j - 1];
114 static int vble_decode_frame(AVCodecContext *avctx, void *data, int *data_size,
117 VBLEContext *ctx = avctx->priv_data;
118 AVFrame *pic = avctx->coded_frame;
120 const uint8_t *src = avpkt->data;
123 int width_uv = avctx->width / 2, height_uv = avctx->height / 2;
127 /* Clear buffer if need be */
129 avctx->release_buffer(avctx, pic);
131 /* Allocate buffer */
132 if (avctx->get_buffer(avctx, pic) < 0) {
133 av_log(avctx, AV_LOG_ERROR, "Could not allocate buffer.\n");
134 return AVERROR(ENOMEM);
139 pic->pict_type = AV_PICTURE_TYPE_I;
141 /* Version should always be 1 */
142 version = AV_RL32(src);
145 av_log(avctx, AV_LOG_ERROR, "Unsupported VBLE Version: %d\n", version);
146 return AVERROR_INVALIDDATA;
149 init_get_bits(&gb, src + 4, (avpkt->size - 4) * 8);
152 if (vble_unpack(ctx, &gb) < 0) {
153 av_log(avctx, AV_LOG_ERROR, "Invalid Code\n");
154 return AVERROR_INVALIDDATA;
157 /* Restore planes. Should be almost identical to Huffyuv's. */
158 vble_restore_plane(ctx, 0, offset, avctx->width, avctx->height);
161 if (!(ctx->avctx->flags & CODEC_FLAG_GRAY)) {
162 offset += avctx->width * avctx->height;
163 vble_restore_plane(ctx, 1, offset, width_uv, height_uv);
165 offset += width_uv * height_uv;
166 vble_restore_plane(ctx, 2, offset, width_uv, height_uv);
169 *data_size = sizeof(AVFrame);
170 *(AVFrame *)data = *pic;
175 static av_cold int vble_decode_close(AVCodecContext *avctx)
177 VBLEContext *ctx = avctx->priv_data;
178 AVFrame *pic = avctx->coded_frame;
181 avctx->release_buffer(avctx, pic);
183 av_freep(&avctx->coded_frame);
189 static av_cold int vble_decode_init(AVCodecContext *avctx)
191 VBLEContext *ctx = avctx->priv_data;
193 /* Stash for later use */
195 ff_dsputil_init(&ctx->dsp, avctx);
197 avctx->pix_fmt = AV_PIX_FMT_YUV420P;
198 avctx->bits_per_raw_sample = 8;
199 avctx->coded_frame = avcodec_alloc_frame();
201 if (!avctx->coded_frame) {
202 av_log(avctx, AV_LOG_ERROR, "Could not allocate frame.\n");
203 return AVERROR(ENOMEM);
206 ctx->size = avpicture_get_size(avctx->pix_fmt,
207 avctx->width, avctx->height);
209 ctx->val = av_malloc(ctx->size * sizeof(*ctx->val));
212 av_log(avctx, AV_LOG_ERROR, "Could not allocate values buffer.\n");
213 vble_decode_close(avctx);
214 return AVERROR(ENOMEM);
220 AVCodec ff_vble_decoder = {
222 .type = AVMEDIA_TYPE_VIDEO,
223 .id = AV_CODEC_ID_VBLE,
224 .priv_data_size = sizeof(VBLEContext),
225 .init = vble_decode_init,
226 .close = vble_decode_close,
227 .decode = vble_decode_frame,
228 .capabilities = CODEC_CAP_DR1,
229 .long_name = NULL_IF_CONFIG_SMALL("VBLE Lossless Codec"),