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