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