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