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
35 AVCodecContext *avctx;
39 uint8_t *val; ///< This array first holds the lengths of vlc symbols and then their value.
42 static int vble_unpack(VBLEContext *ctx, GetBitContext *gb)
46 static const uint8_t LUT[256] = {
47 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,
48 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,
49 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,
50 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,
51 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,
52 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,
53 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,
54 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,
57 /* Read all the lengths in first */
58 for (i = 0; i < ctx->size; i++) {
59 /* At most we need to read 9 bits total to get indices up to 8 */
60 int val = show_bits(gb, 8);
65 skip_bits(gb, val + 1);
73 allbits += ctx->val[i];
76 /* Check we have enough bits left */
77 if (get_bits_left(gb) < allbits)
82 static void vble_restore_plane(VBLEContext *ctx, GetBitContext *gb, int plane,
83 int offset, int width, int height)
85 AVFrame *pic = ctx->avctx->coded_frame;
86 uint8_t *dst = pic->data[plane];
87 uint8_t *val = ctx->val + offset;
88 int stride = pic->linesize[plane];
89 int i, j, left, left_top;
91 for (i = 0; i < height; i++) {
92 for (j = 0; j < width; j++) {
93 /* get_bits can't take a length of 0 */
95 int v = (1 << val[j]) + get_bits(gb, val[j]) - 1;
96 val[j] = (v >> 1) ^ -(v & 1);
101 left_top = dst[-stride];
102 ctx->dsp.add_hfyu_median_prediction(dst, dst-stride, val, 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 if (avpkt->size < 4 || avpkt->size - 4 > INT_MAX/8) {
131 av_log(avctx, AV_LOG_ERROR, "Invalid packet size\n");
132 return AVERROR_INVALIDDATA;
135 /* Allocate buffer */
136 if (avctx->get_buffer(avctx, pic) < 0) {
137 av_log(avctx, AV_LOG_ERROR, "Could not allocate buffer.\n");
138 return AVERROR(ENOMEM);
143 pic->pict_type = AV_PICTURE_TYPE_I;
145 /* Version should always be 1 */
146 version = AV_RL32(src);
149 av_log(avctx, AV_LOG_WARNING, "Unsupported VBLE Version: %d\n", version);
151 init_get_bits(&gb, src + 4, (avpkt->size - 4) * 8);
154 if (vble_unpack(ctx, &gb) < 0) {
155 av_log(avctx, AV_LOG_ERROR, "Invalid Code\n");
156 return AVERROR_INVALIDDATA;
159 /* Restore planes. Should be almost identical to Huffyuv's. */
160 vble_restore_plane(ctx, &gb, 0, offset, avctx->width, avctx->height);
163 if (!(ctx->avctx->flags & CODEC_FLAG_GRAY)) {
164 offset += avctx->width * avctx->height;
165 vble_restore_plane(ctx, &gb, 1, offset, width_uv, height_uv);
167 offset += width_uv * height_uv;
168 vble_restore_plane(ctx, &gb, 2, offset, width_uv, height_uv);
171 *data_size = sizeof(AVFrame);
172 *(AVFrame *)data = *pic;
177 static av_cold int vble_decode_close(AVCodecContext *avctx)
179 VBLEContext *ctx = avctx->priv_data;
180 AVFrame *pic = avctx->coded_frame;
183 avctx->release_buffer(avctx, pic);
185 av_freep(&avctx->coded_frame);
191 static av_cold int vble_decode_init(AVCodecContext *avctx)
193 VBLEContext *ctx = avctx->priv_data;
195 /* Stash for later use */
197 ff_dsputil_init(&ctx->dsp, avctx);
199 avctx->pix_fmt = AV_PIX_FMT_YUV420P;
200 avctx->bits_per_raw_sample = 8;
201 avctx->coded_frame = avcodec_alloc_frame();
203 if (!avctx->coded_frame) {
204 av_log(avctx, AV_LOG_ERROR, "Could not allocate frame.\n");
205 return AVERROR(ENOMEM);
208 ctx->size = avpicture_get_size(avctx->pix_fmt,
209 avctx->width, avctx->height);
211 ctx->val = av_malloc(ctx->size * sizeof(*ctx->val));
214 av_log(avctx, AV_LOG_ERROR, "Could not allocate values buffer.\n");
215 vble_decode_close(avctx);
216 return AVERROR(ENOMEM);
222 AVCodec ff_vble_decoder = {
224 .type = AVMEDIA_TYPE_VIDEO,
225 .id = AV_CODEC_ID_VBLE,
226 .priv_data_size = sizeof(VBLEContext),
227 .init = vble_decode_init,
228 .close = vble_decode_close,
229 .decode = vble_decode_frame,
230 .capabilities = CODEC_CAP_DR1,
231 .long_name = NULL_IF_CONFIG_SMALL("VBLE Lossless Codec"),