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 ALT_BITSTREAM_READER_LE
33 AVCodecContext *avctx;
36 uint8_t *val; ///< This array first holds the lengths of vlc symbols and then their value.
39 static int vble_unpack(VBLEContext *ctx, GetBitContext *gb)
43 static const uint8_t LUT[256] = {
44 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,
45 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,
46 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,
47 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,
48 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,
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,
54 /* Read all the lengths in first */
55 for (i = 0; i < ctx->size; i++) {
56 /* At most we need to read 9 bits total to get indices up to 8 */
57 int val = show_bits(gb, 8);
62 skip_bits(gb, val + 1);
70 allbits += ctx->val[i];
73 /* Check we have enough bits left */
74 if (get_bits_left(gb) < allbits)
77 for (i = 0; i < ctx->size; 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;
93 int stride = pic->linesize[plane];
96 for (i = 0; i < height; i++) {
97 for (j = 0; j < width; j++) {
98 dst[j] = (val[j] >> 1) ^ -(val[j] & 1);
100 /* Top line and left column are not predicted */
105 dst[j] += dst[j - 1];
111 c = a + b - dst[j - 1 - stride];
112 dst[j] += mid_pred(a, b, c);
119 static int vble_decode_frame(AVCodecContext *avctx, void *data, int *data_size,
122 VBLEContext *ctx = avctx->priv_data;
123 AVFrame *pic = avctx->coded_frame;
125 const uint8_t *src = avpkt->data;
128 int width_uv = avctx->width / 2, height_uv = avctx->height / 2;
132 /* Clear buffer if need be */
134 avctx->release_buffer(avctx, pic);
136 /* Allocate buffer */
137 if (avctx->get_buffer(avctx, pic) < 0) {
138 av_log(avctx, AV_LOG_ERROR, "Could not allocate buffer.\n");
139 return AVERROR(ENOMEM);
144 pic->pict_type = FF_I_TYPE;
146 /* Version should always be 1 */
147 version = AV_RL32(src);
150 av_log(avctx, AV_LOG_ERROR, "Unsupported VBLE Version: %d\n", version);
151 return AVERROR_INVALIDDATA;
154 init_get_bits(&gb, src + 4, (avpkt->size - 4) * 8);
157 if (vble_unpack(ctx, &gb) < 0) {
158 av_log(avctx, AV_LOG_ERROR, "Invalid Code\n");
159 return AVERROR_INVALIDDATA;
162 /* Restore planes. Should be almost identical to Huffyuv's. */
163 vble_restore_plane(ctx, 0, offset, avctx->width, avctx->height);
166 if (!(ctx->avctx->flags & CODEC_FLAG_GRAY)) {
167 offset += avctx->width * avctx->height;
168 vble_restore_plane(ctx, 1, offset, width_uv, height_uv);
170 offset += width_uv * height_uv;
171 vble_restore_plane(ctx, 2, offset, width_uv, height_uv);
174 *data_size = sizeof(AVFrame);
175 *(AVFrame *)data = *pic;
180 static av_cold int vble_decode_close(AVCodecContext *avctx)
182 VBLEContext *ctx = avctx->priv_data;
183 AVFrame *pic = avctx->coded_frame;
186 avctx->release_buffer(avctx, pic);
188 av_freep(&avctx->coded_frame);
194 static av_cold int vble_decode_init(AVCodecContext *avctx)
196 VBLEContext *ctx = avctx->priv_data;
198 /* Stash for later use */
201 avctx->pix_fmt = PIX_FMT_YUV420P;
202 avctx->bits_per_raw_sample = 8;
203 avctx->coded_frame = avcodec_alloc_frame();
205 if (!avctx->coded_frame) {
206 av_log(avctx, AV_LOG_ERROR, "Could not allocate frame.\n");
207 return AVERROR(ENOMEM);
210 ctx->size = avpicture_get_size(avctx->pix_fmt,
211 avctx->width, avctx->height);
213 ctx->val = av_malloc(ctx->size * sizeof(*ctx->val));
216 av_log(avctx, AV_LOG_ERROR, "Could not allocate values buffer.\n");
217 vble_decode_close(avctx);
218 return AVERROR(ENOMEM);
224 AVCodec ff_vble_decoder = {
226 .type = AVMEDIA_TYPE_VIDEO,
228 .priv_data_size = sizeof(VBLEContext),
229 .init = vble_decode_init,
230 .close = vble_decode_close,
231 .decode = vble_decode_frame,
232 .capabilities = CODEC_CAP_DR1,
233 .long_name = NULL_IF_CONFIG_SMALL("VBLE Lossless Codec"),