Line | Branch | Exec | 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 | 5 | static int vble_unpack(VBLEContext *ctx, GetBitContext *gb) | |
46 | { | ||
47 | int i; | ||
48 | 5 | int allbits = 0; | |
49 | static const uint8_t LUT[256] = { | ||
50 | 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 | 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 | 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 | 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 | 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 | 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 | }; | ||
59 | |||
60 | /* Read all the lengths in first */ | ||
61 |
2/2✓ Branch 0 taken 5888021 times.
✓ Branch 1 taken 4 times.
|
5888025 | for (i = 0; i < ctx->size; i++) { |
62 | /* At most we need to read 9 bits total to get indices up to 8 */ | ||
63 | 5888021 | int val = show_bits(gb, 8); | |
64 | |||
65 | // read reverse unary | ||
66 |
2/2✓ Branch 0 taken 5887971 times.
✓ Branch 1 taken 50 times.
|
5888021 | if (val) { |
67 | 5887971 | val = LUT[val]; | |
68 | 5887971 | skip_bits(gb, val + 1); | |
69 | 5887971 | ctx->val[i] = val; | |
70 | } else { | ||
71 | 50 | skip_bits(gb, 8); | |
72 |
2/2✓ Branch 1 taken 1 times.
✓ Branch 2 taken 49 times.
|
50 | if (!get_bits1(gb)) |
73 | 1 | return -1; | |
74 | 49 | ctx->val[i] = 8; | |
75 | } | ||
76 | 5888020 | allbits += ctx->val[i]; | |
77 | } | ||
78 | |||
79 | /* Check we have enough bits left */ | ||
80 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 4 times.
|
4 | if (get_bits_left(gb) < allbits) |
81 | ✗ | return -1; | |
82 | 4 | return 0; | |
83 | } | ||
84 | |||
85 | 12 | static void vble_restore_plane(VBLEContext *ctx, AVFrame *pic, | |
86 | GetBitContext *gb, int plane, | ||
87 | int offset, int width, int height) | ||
88 | { | ||
89 | 12 | uint8_t *dst = pic->data[plane]; | |
90 | 12 | uint8_t *val = ctx->val + offset; | |
91 | 12 | int stride = pic->linesize[plane]; | |
92 | int i, j, left, left_top; | ||
93 | |||
94 |
2/2✓ Branch 0 taken 5760 times.
✓ Branch 1 taken 12 times.
|
5772 | for (i = 0; i < height; i++) { |
95 |
2/2✓ Branch 0 taken 5529600 times.
✓ Branch 1 taken 5760 times.
|
5535360 | for (j = 0; j < width; j++) { |
96 | /* get_bits can't take a length of 0 */ | ||
97 |
2/2✓ Branch 0 taken 3143754 times.
✓ Branch 1 taken 2385846 times.
|
5529600 | if (val[j]) { |
98 | 3143754 | int v = (1 << val[j]) + get_bits(gb, val[j]) - 1; | |
99 | 3143754 | val[j] = (v >> 1) ^ -(v & 1); | |
100 | } | ||
101 | } | ||
102 |
2/2✓ Branch 0 taken 5748 times.
✓ Branch 1 taken 12 times.
|
5760 | if (i) { |
103 | 5748 | left = 0; | |
104 | 5748 | left_top = dst[-stride]; | |
105 | 5748 | ctx->llviddsp.add_median_pred(dst, dst - stride, val, | |
106 | width, &left, &left_top); | ||
107 | } else { | ||
108 | 12 | dst[0] = val[0]; | |
109 |
2/2✓ Branch 0 taken 10228 times.
✓ Branch 1 taken 12 times.
|
10240 | for (j = 1; j < width; j++) |
110 | 10228 | dst[j] = val[j] + dst[j - 1]; | |
111 | } | ||
112 | 5760 | dst += stride; | |
113 | 5760 | val += width; | |
114 | } | ||
115 | 12 | } | |
116 | |||
117 | 5 | static int vble_decode_frame(AVCodecContext *avctx, AVFrame *pic, | |
118 | int *got_frame, AVPacket *avpkt) | ||
119 | { | ||
120 | 5 | VBLEContext *ctx = avctx->priv_data; | |
121 | GetBitContext gb; | ||
122 | 5 | const uint8_t *src = avpkt->data; | |
123 | int version; | ||
124 | 5 | int offset = 0; | |
125 | 5 | int width_uv = avctx->width / 2, height_uv = avctx->height / 2; | |
126 | int ret; | ||
127 | |||
128 |
2/4✓ Branch 0 taken 5 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 5 times.
|
5 | if (avpkt->size < 4 || avpkt->size - 4 > INT_MAX/8) { |
129 | ✗ | av_log(avctx, AV_LOG_ERROR, "Invalid packet size\n"); | |
130 | ✗ | return AVERROR_INVALIDDATA; | |
131 | } | ||
132 | |||
133 | /* Version should always be 1 */ | ||
134 | 5 | version = AV_RL32(src); | |
135 | |||
136 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 5 times.
|
5 | if (version != 1) |
137 | ✗ | av_log(avctx, AV_LOG_WARNING, "Unsupported VBLE Version: %d\n", version); | |
138 | |||
139 | 5 | init_get_bits(&gb, src + 4, (avpkt->size - 4) * 8); | |
140 | |||
141 | /* Unpack */ | ||
142 |
2/2✓ Branch 1 taken 1 times.
✓ Branch 2 taken 4 times.
|
5 | if (vble_unpack(ctx, &gb) < 0) { |
143 | 1 | av_log(avctx, AV_LOG_ERROR, "Invalid Code\n"); | |
144 | 1 | return AVERROR_INVALIDDATA; | |
145 | } | ||
146 | |||
147 | /* Allocate buffer */ | ||
148 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 4 times.
|
4 | if ((ret = ff_thread_get_buffer(avctx, pic, 0)) < 0) |
149 | ✗ | return ret; | |
150 | |||
151 | /* Restore planes. Should be almost identical to Huffyuv's. */ | ||
152 | 4 | vble_restore_plane(ctx, pic, &gb, 0, offset, avctx->width, avctx->height); | |
153 | |||
154 | /* Chroma */ | ||
155 |
1/2✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
|
4 | if (!(ctx->avctx->flags & AV_CODEC_FLAG_GRAY)) { |
156 | 4 | offset += avctx->width * avctx->height; | |
157 | 4 | vble_restore_plane(ctx, pic, &gb, 1, offset, width_uv, height_uv); | |
158 | |||
159 | 4 | offset += width_uv * height_uv; | |
160 | 4 | vble_restore_plane(ctx, pic, &gb, 2, offset, width_uv, height_uv); | |
161 | } | ||
162 | |||
163 | 4 | *got_frame = 1; | |
164 | |||
165 | 4 | return avpkt->size; | |
166 | } | ||
167 | |||
168 | 2 | static av_cold int vble_decode_close(AVCodecContext *avctx) | |
169 | { | ||
170 | 2 | VBLEContext *ctx = avctx->priv_data; | |
171 | 2 | av_freep(&ctx->val); | |
172 | |||
173 | 2 | return 0; | |
174 | } | ||
175 | |||
176 | 2 | static av_cold int vble_decode_init(AVCodecContext *avctx) | |
177 | { | ||
178 | 2 | VBLEContext *ctx = avctx->priv_data; | |
179 | |||
180 | /* Stash for later use */ | ||
181 | 2 | ctx->avctx = avctx; | |
182 | 2 | ff_llviddsp_init(&ctx->llviddsp); | |
183 | |||
184 | 2 | avctx->pix_fmt = AV_PIX_FMT_YUV420P; | |
185 | 2 | avctx->bits_per_raw_sample = 8; | |
186 | |||
187 | 2 | ctx->size = av_image_get_buffer_size(avctx->pix_fmt, | |
188 | avctx->width, avctx->height, 1); | ||
189 | |||
190 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
|
2 | if (ctx->size < 0) |
191 | ✗ | return ctx->size; | |
192 | |||
193 | 2 | ctx->val = av_malloc_array(ctx->size, sizeof(*ctx->val)); | |
194 | |||
195 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
|
2 | if (!ctx->val) { |
196 | ✗ | av_log(avctx, AV_LOG_ERROR, "Could not allocate values buffer.\n"); | |
197 | ✗ | return AVERROR(ENOMEM); | |
198 | } | ||
199 | |||
200 | 2 | return 0; | |
201 | } | ||
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 | }; | ||
214 |