3 * Copyright (c) 2011 Derek Buitenhuis
5 * This file is part of FFmpeg.
7 * FFmpeg 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 * FFmpeg 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 FFmpeg; 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
36 AVCodecContext *avctx;
40 uint8_t *val; ///< This array first holds the lengths of vlc symbols and then their value.
43 static int vble_unpack(VBLEContext *ctx, GetBitContext *gb)
47 static const uint8_t LUT[256] = {
48 8,0,1,0,2,0,1,0,3,0,1,0,2,0,1,0,4,0,1,0,2,0,1,0,3,0,1,0,2,0,1,0,
49 5,0,1,0,2,0,1,0,3,0,1,0,2,0,1,0,4,0,1,0,2,0,1,0,3,0,1,0,2,0,1,0,
50 6,0,1,0,2,0,1,0,3,0,1,0,2,0,1,0,4,0,1,0,2,0,1,0,3,0,1,0,2,0,1,0,
51 5,0,1,0,2,0,1,0,3,0,1,0,2,0,1,0,4,0,1,0,2,0,1,0,3,0,1,0,2,0,1,0,
52 7,0,1,0,2,0,1,0,3,0,1,0,2,0,1,0,4,0,1,0,2,0,1,0,3,0,1,0,2,0,1,0,
53 5,0,1,0,2,0,1,0,3,0,1,0,2,0,1,0,4,0,1,0,2,0,1,0,3,0,1,0,2,0,1,0,
54 6,0,1,0,2,0,1,0,3,0,1,0,2,0,1,0,4,0,1,0,2,0,1,0,3,0,1,0,2,0,1,0,
55 5,0,1,0,2,0,1,0,3,0,1,0,2,0,1,0,4,0,1,0,2,0,1,0,3,0,1,0,2,0,1,0,
58 /* Read all the lengths in first */
59 for (i = 0; i < ctx->size; i++) {
60 /* At most we need to read 9 bits total to get indices up to 8 */
61 int val = show_bits(gb, 8);
66 skip_bits(gb, val + 1);
74 allbits += ctx->val[i];
77 /* Check we have enough bits left */
78 if (get_bits_left(gb) < allbits)
83 static void vble_restore_plane(VBLEContext *ctx, AVFrame *pic,
84 GetBitContext *gb, int plane,
85 int offset, int width, int height)
87 uint8_t *dst = pic->data[plane];
88 uint8_t *val = ctx->val + offset;
89 int stride = pic->linesize[plane];
90 int i, j, left, left_top;
92 for (i = 0; i < height; i++) {
93 for (j = 0; j < width; j++) {
94 /* get_bits can't take a length of 0 */
96 int v = (1 << val[j]) + get_bits(gb, val[j]) - 1;
97 val[j] = (v >> 1) ^ -(v & 1);
102 left_top = dst[-stride];
103 ctx->dsp.add_hfyu_median_prediction(dst, dst-stride, val, 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 *got_frame,
117 VBLEContext *ctx = avctx->priv_data;
120 const uint8_t *src = avpkt->data;
123 int width_uv = avctx->width / 2, height_uv = avctx->height / 2;
126 if (avpkt->size < 4 || avpkt->size - 4 > INT_MAX/8) {
127 av_log(avctx, AV_LOG_ERROR, "Invalid packet size\n");
128 return AVERROR_INVALIDDATA;
131 /* Allocate buffer */
132 if ((ret = ff_get_buffer(avctx, pic, 0)) < 0)
137 pic->pict_type = AV_PICTURE_TYPE_I;
139 /* Version should always be 1 */
140 version = AV_RL32(src);
143 av_log(avctx, AV_LOG_WARNING, "Unsupported VBLE Version: %d\n", version);
145 init_get_bits(&gb, src + 4, (avpkt->size - 4) * 8);
148 if (vble_unpack(ctx, &gb) < 0) {
149 av_log(avctx, AV_LOG_ERROR, "Invalid Code\n");
150 return AVERROR_INVALIDDATA;
153 /* Restore planes. Should be almost identical to Huffyuv's. */
154 vble_restore_plane(ctx, pic, &gb, 0, offset, avctx->width, avctx->height);
157 if (!(ctx->avctx->flags & CODEC_FLAG_GRAY)) {
158 offset += avctx->width * avctx->height;
159 vble_restore_plane(ctx, pic, &gb, 1, offset, width_uv, height_uv);
161 offset += width_uv * height_uv;
162 vble_restore_plane(ctx, pic, &gb, 2, offset, width_uv, height_uv);
170 static av_cold int vble_decode_close(AVCodecContext *avctx)
172 VBLEContext *ctx = avctx->priv_data;
178 static av_cold int vble_decode_init(AVCodecContext *avctx)
180 VBLEContext *ctx = avctx->priv_data;
182 /* Stash for later use */
184 ff_dsputil_init(&ctx->dsp, avctx);
186 avctx->pix_fmt = AV_PIX_FMT_YUV420P;
187 avctx->bits_per_raw_sample = 8;
189 ctx->size = avpicture_get_size(avctx->pix_fmt,
190 avctx->width, avctx->height);
192 ctx->val = av_malloc(ctx->size * sizeof(*ctx->val));
195 av_log(avctx, AV_LOG_ERROR, "Could not allocate values buffer.\n");
196 vble_decode_close(avctx);
197 return AVERROR(ENOMEM);
203 AVCodec ff_vble_decoder = {
205 .long_name = NULL_IF_CONFIG_SMALL("VBLE Lossless Codec"),
206 .type = AVMEDIA_TYPE_VIDEO,
207 .id = AV_CODEC_ID_VBLE,
208 .priv_data_size = sizeof(VBLEContext),
209 .init = vble_decode_init,
210 .close = vble_decode_close,
211 .decode = vble_decode_frame,
212 .capabilities = CODEC_CAP_DR1,