]> git.sesse.net Git - ffmpeg/blob - libavcodec/vble.c
Merge commit '3c370f5abc55739a261534b9f9bdc739cedbbbb9'
[ffmpeg] / libavcodec / vble.c
1 /*
2  * VBLE Decoder
3  * Copyright (c) 2011 Derek Buitenhuis
4  *
5  * This file is part of FFmpeg.
6  *
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.
11  *
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.
16  *
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
20  */
21
22 /**
23  * @file
24  * VBLE Decoder
25  */
26
27 #define BITSTREAM_READER_LE
28
29 #include "avcodec.h"
30 #include "dsputil.h"
31 #include "get_bits.h"
32 #include "mathops.h"
33
34 typedef struct {
35     AVCodecContext *avctx;
36     DSPContext dsp;
37
38     int            size;
39     uint8_t        *val; ///< This array first holds the lengths of vlc symbols and then their value.
40 } VBLEContext;
41
42 static int vble_unpack(VBLEContext *ctx, GetBitContext *gb)
43 {
44     int i;
45     int allbits = 0;
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,
55     };
56
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);
61
62         // read reverse unary
63         if (val) {
64             val = LUT[val];
65             skip_bits(gb, val + 1);
66             ctx->val[i] = val;
67         } else {
68             skip_bits(gb, 8);
69             if (!get_bits1(gb))
70                 return -1;
71             ctx->val[i] = 8;
72         }
73         allbits += ctx->val[i];
74     }
75
76     /* Check we have enough bits left */
77     if (get_bits_left(gb) < allbits)
78         return -1;
79     return 0;
80 }
81
82 static void vble_restore_plane(VBLEContext *ctx, GetBitContext *gb, int plane,
83                                int offset, int width, int height)
84 {
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;
90
91     for (i = 0; i < height; i++) {
92         for (j = 0; j < width; j++) {
93             /* get_bits can't take a length of 0 */
94             if (val[j]) {
95                 int v = (1 << val[j]) + get_bits(gb, val[j]) - 1;
96                 val[j] = (v >> 1) ^ -(v & 1);
97             }
98         }
99         if (i) {
100             left = 0;
101             left_top = dst[-stride];
102             ctx->dsp.add_hfyu_median_prediction(dst, dst-stride, val, width, &left, &left_top);
103         } else {
104             dst[0] = val[0];
105             for (j = 1; j < width; j++)
106                 dst[j] = val[j] + dst[j - 1];
107         }
108         dst += stride;
109         val += width;
110     }
111 }
112
113 static int vble_decode_frame(AVCodecContext *avctx, void *data, int *data_size,
114                              AVPacket *avpkt)
115 {
116     VBLEContext *ctx = avctx->priv_data;
117     AVFrame *pic = avctx->coded_frame;
118     GetBitContext gb;
119     const uint8_t *src = avpkt->data;
120     int version;
121     int offset = 0;
122     int width_uv = avctx->width / 2, height_uv = avctx->height / 2;
123
124     pic->reference = 0;
125
126     /* Clear buffer if need be */
127     if (pic->data[0])
128         avctx->release_buffer(avctx, pic);
129
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);
134     }
135
136     /* Set flags */
137     pic->key_frame = 1;
138     pic->pict_type = AV_PICTURE_TYPE_I;
139
140     /* Version should always be 1 */
141     version = AV_RL32(src);
142
143     if (version != 1)
144         av_log(avctx, AV_LOG_WARNING, "Unsupported VBLE Version: %d\n", version);
145
146     init_get_bits(&gb, src + 4, (avpkt->size - 4) * 8);
147
148     /* Unpack */
149     if (vble_unpack(ctx, &gb) < 0) {
150         av_log(avctx, AV_LOG_ERROR, "Invalid Code\n");
151         return AVERROR_INVALIDDATA;
152     }
153
154     /* Restore planes. Should be almost identical to Huffyuv's. */
155     vble_restore_plane(ctx, &gb, 0, offset, avctx->width, avctx->height);
156
157     /* Chroma */
158     if (!(ctx->avctx->flags & CODEC_FLAG_GRAY)) {
159         offset += avctx->width * avctx->height;
160         vble_restore_plane(ctx, &gb, 1, offset, width_uv, height_uv);
161
162         offset += width_uv * height_uv;
163         vble_restore_plane(ctx, &gb, 2, offset, width_uv, height_uv);
164     }
165
166     *data_size = sizeof(AVFrame);
167     *(AVFrame *)data = *pic;
168
169     return avpkt->size;
170 }
171
172 static av_cold int vble_decode_close(AVCodecContext *avctx)
173 {
174     VBLEContext *ctx = avctx->priv_data;
175     AVFrame *pic = avctx->coded_frame;
176
177     if (pic->data[0])
178         avctx->release_buffer(avctx, pic);
179
180     av_freep(&avctx->coded_frame);
181     av_freep(&ctx->val);
182
183     return 0;
184 }
185
186 static av_cold int vble_decode_init(AVCodecContext *avctx)
187 {
188     VBLEContext *ctx = avctx->priv_data;
189
190     /* Stash for later use */
191     ctx->avctx = avctx;
192     ff_dsputil_init(&ctx->dsp, avctx);
193
194     avctx->pix_fmt = AV_PIX_FMT_YUV420P;
195     avctx->bits_per_raw_sample = 8;
196     avctx->coded_frame = avcodec_alloc_frame();
197
198     if (!avctx->coded_frame) {
199         av_log(avctx, AV_LOG_ERROR, "Could not allocate frame.\n");
200         return AVERROR(ENOMEM);
201     }
202
203     ctx->size = avpicture_get_size(avctx->pix_fmt,
204                                    avctx->width, avctx->height);
205
206     ctx->val = av_malloc(ctx->size * sizeof(*ctx->val));
207
208     if (!ctx->val) {
209         av_log(avctx, AV_LOG_ERROR, "Could not allocate values buffer.\n");
210         vble_decode_close(avctx);
211         return AVERROR(ENOMEM);
212     }
213
214     return 0;
215 }
216
217 AVCodec ff_vble_decoder = {
218     .name           = "vble",
219     .type           = AVMEDIA_TYPE_VIDEO,
220     .id             = AV_CODEC_ID_VBLE,
221     .priv_data_size = sizeof(VBLEContext),
222     .init           = vble_decode_init,
223     .close          = vble_decode_close,
224     .decode         = vble_decode_frame,
225     .capabilities   = CODEC_CAP_DR1,
226     .long_name      = NULL_IF_CONFIG_SMALL("VBLE Lossless Codec"),
227 };