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