1 | /*
|
---|
2 | * JPEG-LS encoder and decoder
|
---|
3 | * Copyright (c) 2003 Michael Niedermayer
|
---|
4 | * Copyright (c) 2006 Konstantin Shishkov
|
---|
5 | *
|
---|
6 | * This library is free software; you can redistribute it and/or
|
---|
7 | * modify it under the terms of the GNU Lesser General Public
|
---|
8 | * License as published by the Free Software Foundation; either
|
---|
9 | * version 2 of the License, or (at your option) any later version.
|
---|
10 | *
|
---|
11 | * This library is distributed in the hope that it will be useful,
|
---|
12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
---|
14 | * Lesser General Public License for more details.
|
---|
15 | *
|
---|
16 | * You should have received a copy of the GNU Lesser General Public
|
---|
17 | * License along with this library; if not, write to the Free Software
|
---|
18 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
---|
19 | */
|
---|
20 |
|
---|
21 | #include "golomb.h"
|
---|
22 |
|
---|
23 | /**
|
---|
24 | * @file jpeg_ls.c
|
---|
25 | * JPEG-LS encoder and decoder.
|
---|
26 | */
|
---|
27 |
|
---|
28 | typedef struct JpeglsContext{
|
---|
29 | AVCodecContext *avctx;
|
---|
30 | AVFrame picture;
|
---|
31 | }JpeglsContext;
|
---|
32 |
|
---|
33 | typedef struct JLSState{
|
---|
34 | int T1, T2, T3;
|
---|
35 | int A[367], B[367], C[365], N[367];
|
---|
36 | int limit, reset, bpp, qbpp, maxval, range;
|
---|
37 | int near, twonear;
|
---|
38 | int run_index[3];
|
---|
39 | }JLSState;
|
---|
40 |
|
---|
41 | static const uint8_t log2_run[32]={
|
---|
42 | 0, 0, 0, 0, 1, 1, 1, 1,
|
---|
43 | 2, 2, 2, 2, 3, 3, 3, 3,
|
---|
44 | 4, 4, 5, 5, 6, 6, 7, 7,
|
---|
45 | 8, 9,10,11,12,13,14,15
|
---|
46 | };
|
---|
47 |
|
---|
48 | /*
|
---|
49 | * Uncomment this to significantly speed up decoding of broken JPEG-LS
|
---|
50 | * (or test broken JPEG-LS decoder) and slow down ordinary decoding a bit.
|
---|
51 | *
|
---|
52 | * There is no Golomb code with length >= 32 bits possible, so check and
|
---|
53 | * avoid situation of 32 zeros, FFmpeg Golomb decoder is painfully slow
|
---|
54 | * on this errors.
|
---|
55 | */
|
---|
56 | //#define JLS_BROKEN
|
---|
57 |
|
---|
58 | /********** Functions for both encoder and decoder **********/
|
---|
59 |
|
---|
60 | /**
|
---|
61 | * Calculate initial JPEG-LS parameters
|
---|
62 | */
|
---|
63 | static void ls_init_state(JLSState *state){
|
---|
64 | int i;
|
---|
65 |
|
---|
66 | state->twonear = state->near * 2 + 1;
|
---|
67 | state->range = ((state->maxval + state->twonear - 1) / state->twonear) + 1;
|
---|
68 |
|
---|
69 | // QBPP = ceil(log2(RANGE))
|
---|
70 | for(state->qbpp = 0; (1 << state->qbpp) < state->range; state->qbpp++);
|
---|
71 |
|
---|
72 | if(state->bpp < 8)
|
---|
73 | state->limit = 16 + 2 * state->bpp - state->qbpp;
|
---|
74 | else
|
---|
75 | state->limit = (4 * state->bpp) - state->qbpp;
|
---|
76 |
|
---|
77 | for(i = 0; i < 367; i++) {
|
---|
78 | state->A[i] = (state->range + 32) >> 6;
|
---|
79 | if(state->A[i] < 2)
|
---|
80 | state->A[i] = 2;
|
---|
81 | state->N[i] = 1;
|
---|
82 | }
|
---|
83 |
|
---|
84 | }
|
---|
85 |
|
---|
86 | /**
|
---|
87 | * Calculate quantized gradient value, used for context determination
|
---|
88 | */
|
---|
89 | static inline int quantize(JLSState *s, int v){ //FIXME optimize
|
---|
90 | if(v==0) return 0;
|
---|
91 | if(v < 0){
|
---|
92 | if(v <= -s->T3) return -4;
|
---|
93 | if(v <= -s->T2) return -3;
|
---|
94 | if(v <= -s->T1) return -2;
|
---|
95 | if(v < -s->near) return -1;
|
---|
96 | return 0;
|
---|
97 | }else{
|
---|
98 | if(v <= s->near) return 0;
|
---|
99 | if(v < s->T1) return 1;
|
---|
100 | if(v < s->T2) return 2;
|
---|
101 | if(v < s->T3) return 3;
|
---|
102 | return 4;
|
---|
103 | }
|
---|
104 | }
|
---|
105 |
|
---|
106 | /**
|
---|
107 | * Custom value clipping function used in T1, T2, T3 calculation
|
---|
108 | */
|
---|
109 | static inline int iso_clip(int v, int vmin, int vmax){
|
---|
110 | if(v > vmax || v < vmin) return vmin;
|
---|
111 | else return v;
|
---|
112 | }
|
---|
113 |
|
---|
114 | /**
|
---|
115 | * Calculate JPEG-LS codec values
|
---|
116 | */
|
---|
117 | static void reset_ls_coding_parameters(JLSState *s, int reset_all){
|
---|
118 | const int basic_t1= 3;
|
---|
119 | const int basic_t2= 7;
|
---|
120 | const int basic_t3= 21;
|
---|
121 | int factor;
|
---|
122 |
|
---|
123 | if(s->maxval==0 || reset_all) s->maxval= (1 << s->bpp) - 1;
|
---|
124 |
|
---|
125 | if(s->maxval >=128){
|
---|
126 | factor= (FFMIN(s->maxval, 4095) + 128)>>8;
|
---|
127 |
|
---|
128 | if(s->T1==0 || reset_all)
|
---|
129 | s->T1= iso_clip(factor*(basic_t1-2) + 2 + 3*s->near, s->near+1, s->maxval);
|
---|
130 | if(s->T2==0 || reset_all)
|
---|
131 | s->T2= iso_clip(factor*(basic_t2-3) + 3 + 5*s->near, s->T1, s->maxval);
|
---|
132 | if(s->T3==0 || reset_all)
|
---|
133 | s->T3= iso_clip(factor*(basic_t3-4) + 4 + 7*s->near, s->T2, s->maxval);
|
---|
134 | }else{
|
---|
135 | factor= 256 / (s->maxval + 1);
|
---|
136 |
|
---|
137 | if(s->T1==0 || reset_all)
|
---|
138 | s->T1= iso_clip(FFMAX(2, basic_t1/factor + 3*s->near), s->near+1, s->maxval);
|
---|
139 | if(s->T2==0 || reset_all)
|
---|
140 | s->T2= iso_clip(FFMAX(3, basic_t2/factor + 5*s->near), s->T1, s->maxval);
|
---|
141 | if(s->T3==0 || reset_all)
|
---|
142 | s->T3= iso_clip(FFMAX(4, basic_t3/factor + 6*s->near), s->T2, s->maxval);
|
---|
143 | }
|
---|
144 |
|
---|
145 | if(s->reset==0 || reset_all) s->reset= 64;
|
---|
146 | // av_log(NULL, AV_LOG_DEBUG, "[JPEG-LS RESET] T=%i,%i,%i\n", s->T1, s->T2, s->T3);
|
---|
147 | }
|
---|
148 |
|
---|
149 |
|
---|
150 | /********** Decoder-specific functions **********/
|
---|
151 |
|
---|
152 | /**
|
---|
153 | * Decode LSE block with initialization parameters
|
---|
154 | */
|
---|
155 | static int decode_lse(MJpegDecodeContext *s)
|
---|
156 | {
|
---|
157 | int len, id;
|
---|
158 |
|
---|
159 | /* XXX: verify len field validity */
|
---|
160 | len = get_bits(&s->gb, 16);
|
---|
161 | id = get_bits(&s->gb, 8);
|
---|
162 |
|
---|
163 | switch(id){
|
---|
164 | case 1:
|
---|
165 | s->maxval= get_bits(&s->gb, 16);
|
---|
166 | s->t1= get_bits(&s->gb, 16);
|
---|
167 | s->t2= get_bits(&s->gb, 16);
|
---|
168 | s->t3= get_bits(&s->gb, 16);
|
---|
169 | s->reset= get_bits(&s->gb, 16);
|
---|
170 |
|
---|
171 | // reset_ls_coding_parameters(s, 0);
|
---|
172 | //FIXME quant table?
|
---|
173 | break;
|
---|
174 | case 2:
|
---|
175 | case 3:
|
---|
176 | av_log(s->avctx, AV_LOG_ERROR, "palette not supported\n");
|
---|
177 | return -1;
|
---|
178 | case 4:
|
---|
179 | av_log(s->avctx, AV_LOG_ERROR, "oversize image not supported\n");
|
---|
180 | return -1;
|
---|
181 | default:
|
---|
182 | av_log(s->avctx, AV_LOG_ERROR, "invalid id %d\n", id);
|
---|
183 | return -1;
|
---|
184 | }
|
---|
185 | // av_log(s->avctx, AV_LOG_DEBUG, "ID=%i, T=%i,%i,%i\n", id, s->t1, s->t2, s->t3);
|
---|
186 |
|
---|
187 | return 0;
|
---|
188 | }
|
---|
189 |
|
---|
190 |
|
---|
191 | /**
|
---|
192 | * Get context-dependent Golomb code, decode it and update context
|
---|
193 | */
|
---|
194 | static inline int ls_get_code_regular(GetBitContext *gb, JLSState *state, int Q){
|
---|
195 | int k, ret;
|
---|
196 |
|
---|
197 | for(k = 0; (state->N[Q] << k) < state->A[Q]; k++);
|
---|
198 |
|
---|
199 | #ifdef JLS_BROKEN
|
---|
200 | if(!show_bits_long(gb, 32))return -1;
|
---|
201 | #endif
|
---|
202 | ret = get_ur_golomb_jpegls(gb, k, state->limit, state->qbpp);
|
---|
203 |
|
---|
204 | /* decode mapped error */
|
---|
205 | if(ret & 1)
|
---|
206 | ret = -((ret + 1) >> 1);
|
---|
207 | else
|
---|
208 | ret >>= 1;
|
---|
209 |
|
---|
210 | /* for NEAR=0, k=0 and 2*B[Q] <= - N[Q] mapping is reversed */
|
---|
211 | if(!state->near && !k && (2 * state->B[Q] <= -state->N[Q]))
|
---|
212 | ret = -(ret + 1);
|
---|
213 |
|
---|
214 | state->A[Q] += ABS(ret);
|
---|
215 | ret *= state->twonear;
|
---|
216 | state->B[Q] += ret;
|
---|
217 |
|
---|
218 | if(state->N[Q] == state->reset) {
|
---|
219 | state->A[Q] >>= 1;
|
---|
220 | state->B[Q] >>= 1;
|
---|
221 | state->N[Q] >>= 1;
|
---|
222 | }
|
---|
223 | state->N[Q]++;
|
---|
224 |
|
---|
225 | if(state->B[Q] <= -state->N[Q]) {
|
---|
226 | state->B[Q] += state->N[Q];
|
---|
227 | if(state->C[Q] > -128)
|
---|
228 | state->C[Q]--;
|
---|
229 | if(state->B[Q] <= -state->N[Q])
|
---|
230 | state->B[Q] = -state->N[Q] + 1;
|
---|
231 | }else if(state->B[Q] > 0){
|
---|
232 | state->B[Q] -= state->N[Q];
|
---|
233 | if(state->C[Q] < 127)
|
---|
234 | state->C[Q]++;
|
---|
235 | if(state->B[Q] > 0)
|
---|
236 | state->B[Q] = 0;
|
---|
237 | }
|
---|
238 |
|
---|
239 | return ret;
|
---|
240 | }
|
---|
241 |
|
---|
242 | /**
|
---|
243 | * Get Golomb code, decode it and update state for run termination
|
---|
244 | */
|
---|
245 | static inline int ls_get_code_runterm(GetBitContext *gb, JLSState *state, int RItype, int limit_add){
|
---|
246 | int k, ret, temp, map;
|
---|
247 | int Q = 365 + RItype;
|
---|
248 |
|
---|
249 | if(!RItype)
|
---|
250 | temp = state->A[Q];
|
---|
251 | else
|
---|
252 | temp = state->A[Q] + (state->N[Q] >> 1);
|
---|
253 |
|
---|
254 | for(k = 0; (state->N[Q] << k) < temp; k++);
|
---|
255 |
|
---|
256 | #ifdef JLS_BROKEN
|
---|
257 | if(!show_bits_long(gb, 32))return -1;
|
---|
258 | #endif
|
---|
259 | ret = get_ur_golomb_jpegls(gb, k, state->limit - limit_add - 1, state->qbpp);
|
---|
260 |
|
---|
261 | /* decode mapped error */
|
---|
262 | map = 0;
|
---|
263 | if(!k && (RItype || ret) && (2 * state->B[Q] < state->N[Q]))
|
---|
264 | map = 1;
|
---|
265 | ret += RItype + map;
|
---|
266 |
|
---|
267 | if(ret & 1){
|
---|
268 | ret = map - ((ret + 1) >> 1);
|
---|
269 | state->B[Q]++;
|
---|
270 | } else {
|
---|
271 | ret = ret >> 1;
|
---|
272 | }
|
---|
273 |
|
---|
274 | /* update state */
|
---|
275 | state->A[Q] += ABS(ret) - RItype;
|
---|
276 | ret *= state->twonear;
|
---|
277 | if(state->N[Q] == state->reset){
|
---|
278 | state->A[Q] >>=1;
|
---|
279 | state->B[Q] >>=1;
|
---|
280 | state->N[Q] >>=1;
|
---|
281 | }
|
---|
282 | state->N[Q]++;
|
---|
283 |
|
---|
284 | return ret;
|
---|
285 | }
|
---|
286 |
|
---|
287 | /**
|
---|
288 | * Decode one line of image
|
---|
289 | */
|
---|
290 | static inline void ls_decode_line(JLSState *state, MJpegDecodeContext *s, uint8_t *last, uint8_t *dst, int last2, int w, int stride, int comp){
|
---|
291 | int i, x = 0;
|
---|
292 | int Ra, Rb, Rc, Rd;
|
---|
293 | int D0, D1, D2;
|
---|
294 |
|
---|
295 | while(x < w) {
|
---|
296 | int err, pred;
|
---|
297 |
|
---|
298 | /* compute gradients */
|
---|
299 | Ra = x ? dst[x - stride] : last[x];
|
---|
300 | Rb = last[x];
|
---|
301 | Rc = x ? last[x - stride] : last2;
|
---|
302 | Rd = (x >= w - stride) ? last[x] : last[x + stride];
|
---|
303 | D0 = Rd - Rb;
|
---|
304 | D1 = Rb - Rc;
|
---|
305 | D2 = Rc - Ra;
|
---|
306 | /* run mode */
|
---|
307 | if((ABS(D0) <= state->near) && (ABS(D1) <= state->near) && (ABS(D2) <= state->near)) {
|
---|
308 | int r;
|
---|
309 | int RItype;
|
---|
310 |
|
---|
311 | /* decode full runs while available */
|
---|
312 | while(get_bits1(&s->gb)) {
|
---|
313 | int r;
|
---|
314 | r = 1 << log2_run[state->run_index[comp]];
|
---|
315 | if(x + r * stride > w) {
|
---|
316 | r = (w - x) / stride;
|
---|
317 | }
|
---|
318 | for(i = 0; i < r; i++) {
|
---|
319 | dst[x] = Ra;
|
---|
320 | x += stride;
|
---|
321 | }
|
---|
322 | /* if EOL reached, we stop decoding */
|
---|
323 | if(r != (1 << log2_run[state->run_index[comp]]))
|
---|
324 | return;
|
---|
325 | if(state->run_index[comp] < 31)
|
---|
326 | state->run_index[comp]++;
|
---|
327 | if(x + stride > w)
|
---|
328 | return;
|
---|
329 | }
|
---|
330 | /* decode aborted run */
|
---|
331 | r = log2_run[state->run_index[comp]];
|
---|
332 | if(r)
|
---|
333 | r = get_bits_long(&s->gb, r);
|
---|
334 | for(i = 0; i < r; i++) {
|
---|
335 | dst[x] = Ra;
|
---|
336 | x += stride;
|
---|
337 | }
|
---|
338 |
|
---|
339 | /* decode run termination value */
|
---|
340 | Rb = last[x];
|
---|
341 | RItype = (ABS(Ra - Rb) <= state->near) ? 1 : 0;
|
---|
342 | err = ls_get_code_runterm(&s->gb, state, RItype, log2_run[state->run_index[comp]]);
|
---|
343 | if(state->run_index[comp])
|
---|
344 | state->run_index[comp]--;
|
---|
345 |
|
---|
346 | if(state->near && RItype){
|
---|
347 | pred = Ra + err;
|
---|
348 | } else {
|
---|
349 | if(Rb < Ra)
|
---|
350 | pred = Rb - err;
|
---|
351 | else
|
---|
352 | pred = Rb + err;
|
---|
353 | }
|
---|
354 |
|
---|
355 | if(state->near){
|
---|
356 | if(pred < -state->near)
|
---|
357 | pred += state->range * state->twonear;
|
---|
358 | else if(pred > state->maxval + state->near)
|
---|
359 | pred -= state->range * state->twonear;
|
---|
360 | pred = clip(pred, 0, state->maxval);
|
---|
361 | }
|
---|
362 |
|
---|
363 | dst[x] = pred;
|
---|
364 | x += stride;
|
---|
365 | } else { /* regular mode */
|
---|
366 | int context, sign;
|
---|
367 |
|
---|
368 | context = quantize(state, D0) * 81 + quantize(state, D1) * 9 + quantize(state, D2);
|
---|
369 | pred = mid_pred(Ra, Ra + Rb - Rc, Rb);
|
---|
370 |
|
---|
371 | if(context < 0){
|
---|
372 | context = -context;
|
---|
373 | sign = 1;
|
---|
374 | }else{
|
---|
375 | sign = 0;
|
---|
376 | }
|
---|
377 |
|
---|
378 | if(sign){
|
---|
379 | pred = clip(pred - state->C[context], 0, state->maxval);
|
---|
380 | err = -ls_get_code_regular(&s->gb, state, context);
|
---|
381 | } else {
|
---|
382 | pred = clip(pred + state->C[context], 0, state->maxval);
|
---|
383 | err = ls_get_code_regular(&s->gb, state, context);
|
---|
384 | }
|
---|
385 |
|
---|
386 | /* we have to do something more for near-lossless coding */
|
---|
387 | pred += err;
|
---|
388 | if(state->near) {
|
---|
389 | if(pred < -state->near)
|
---|
390 | pred += state->range * state->twonear;
|
---|
391 | else if(pred > state->maxval + state->near)
|
---|
392 | pred -= state->range * state->twonear;
|
---|
393 | pred = clip(pred, 0, state->maxval);
|
---|
394 | }
|
---|
395 |
|
---|
396 | dst[x] = pred;
|
---|
397 | x += stride;
|
---|
398 | }
|
---|
399 | }
|
---|
400 | }
|
---|
401 |
|
---|
402 | static int ls_decode_picture(MJpegDecodeContext *s, int near, int point_transform, int ilv){
|
---|
403 | int i, t = 0;
|
---|
404 | uint8_t *zero, *last, *cur;
|
---|
405 | JLSState *state;
|
---|
406 | int off, stride, width;
|
---|
407 |
|
---|
408 | zero = av_mallocz(s->picture.linesize[0]);
|
---|
409 | last = zero;
|
---|
410 | cur = s->picture.data[0];
|
---|
411 |
|
---|
412 | state = av_mallocz(sizeof(JLSState));
|
---|
413 | /* initialize JPEG-LS state from JPEG parameters */
|
---|
414 | state->near = near;
|
---|
415 | state->bpp = (s->bits < 2) ? 2 : s->bits;
|
---|
416 | state->maxval = s->maxval;
|
---|
417 | state->T1 = s->t1;
|
---|
418 | state->T2 = s->t2;
|
---|
419 | state->T3 = s->t3;
|
---|
420 | state->reset = s->reset;
|
---|
421 | reset_ls_coding_parameters(state, 0);
|
---|
422 | ls_init_state(state);
|
---|
423 |
|
---|
424 | // av_log(s->avctx, AV_LOG_DEBUG, "JPEG-LS params: %ix%i NEAR=%i MV=%i T(%i,%i,%i) RESET=%i, LIMIT=%i, qbpp=%i, RANGE=%i\n",s->width,s->height,state->near,state->maxval,state->T1,state->T2,state->T3,state->reset,state->limit,state->qbpp, state->range);
|
---|
425 | // av_log(s->avctx, AV_LOG_DEBUG, "JPEG params: ILV=%i Pt=%i BPP=%i, scan = %i\n", ilv, point_transform, s->bits, s->cur_scan);
|
---|
426 | if(ilv == 0) { /* separate planes */
|
---|
427 | off = s->cur_scan - 1;
|
---|
428 | stride = (s->nb_components > 1) ? 3 : 1;
|
---|
429 | width = s->width * stride;
|
---|
430 | cur += off;
|
---|
431 | for(i = 0; i < s->height; i++) {
|
---|
432 | ls_decode_line(state, s, last, cur, t, width, stride, off);
|
---|
433 | t = last[0];
|
---|
434 | last = cur;
|
---|
435 | cur += s->picture.linesize[0];
|
---|
436 |
|
---|
437 | if (s->restart_interval && !--s->restart_count) {
|
---|
438 | align_get_bits(&s->gb);
|
---|
439 | skip_bits(&s->gb, 16); /* skip RSTn */
|
---|
440 | }
|
---|
441 | }
|
---|
442 | } else if(ilv == 1) { /* line interleaving */
|
---|
443 | int j;
|
---|
444 | int Rc[3] = {0, 0, 0};
|
---|
445 | memset(cur, 0, s->picture.linesize[0]);
|
---|
446 | width = s->width * 3;
|
---|
447 | for(i = 0; i < s->height; i++) {
|
---|
448 | for(j = 0; j < 3; j++) {
|
---|
449 | ls_decode_line(state, s, last + j, cur + j, Rc[j], width, 3, j);
|
---|
450 | Rc[j] = last[j];
|
---|
451 |
|
---|
452 | if (s->restart_interval && !--s->restart_count) {
|
---|
453 | align_get_bits(&s->gb);
|
---|
454 | skip_bits(&s->gb, 16); /* skip RSTn */
|
---|
455 | }
|
---|
456 | }
|
---|
457 | last = cur;
|
---|
458 | cur += s->picture.linesize[0];
|
---|
459 | }
|
---|
460 | } else if(ilv == 2) { /* sample interleaving */
|
---|
461 | av_log(s->avctx, AV_LOG_ERROR, "Sample interleaved images are not supported.\n");
|
---|
462 | return -1;
|
---|
463 | }
|
---|
464 |
|
---|
465 | av_free(state);
|
---|
466 | av_free(zero);
|
---|
467 |
|
---|
468 | return 0;
|
---|
469 | }
|
---|
470 |
|
---|
471 | #if defined(CONFIG_ENCODERS) && defined(CONFIG_JPEGLS_ENCODER)
|
---|
472 | /********** Encoder-specific functions **********/
|
---|
473 |
|
---|
474 | /**
|
---|
475 | * Encode error from regular symbol
|
---|
476 | */
|
---|
477 | static inline void ls_encode_regular(JLSState *state, PutBitContext *pb, int Q, int err){
|
---|
478 | int k;
|
---|
479 | int val;
|
---|
480 | int map;
|
---|
481 |
|
---|
482 | for(k = 0; (state->N[Q] << k) < state->A[Q]; k++);
|
---|
483 |
|
---|
484 | map = !state->near && !k && (2 * state->B[Q] <= -state->N[Q]);
|
---|
485 |
|
---|
486 | if(err < 0)
|
---|
487 | err += state->range;
|
---|
488 | if(err >= ((state->range + 1) >> 1)) {
|
---|
489 | err -= state->range;
|
---|
490 | val = 2 * ABS(err) - 1 - map;
|
---|
491 | } else
|
---|
492 | val = 2 * err + map;
|
---|
493 |
|
---|
494 | set_ur_golomb_jpegls(pb, val, k, state->limit, state->qbpp);
|
---|
495 |
|
---|
496 | state->A[Q] += ABS(err);
|
---|
497 | state->B[Q] += err * state->twonear;
|
---|
498 |
|
---|
499 | if(state->N[Q] == state->reset) {
|
---|
500 | state->A[Q] >>= 1;
|
---|
501 | state->B[Q] >>= 1;
|
---|
502 | state->N[Q] >>= 1;
|
---|
503 | }
|
---|
504 | state->N[Q]++;
|
---|
505 |
|
---|
506 | if(state->B[Q] <= -state->N[Q]) {
|
---|
507 | state->B[Q] += state->N[Q];
|
---|
508 | if(state->C[Q] > -128)
|
---|
509 | state->C[Q]--;
|
---|
510 | if(state->B[Q] <= -state->N[Q])
|
---|
511 | state->B[Q] = -state->N[Q] + 1;
|
---|
512 | }else if(state->B[Q] > 0){
|
---|
513 | state->B[Q] -= state->N[Q];
|
---|
514 | if(state->C[Q] < 127)
|
---|
515 | state->C[Q]++;
|
---|
516 | if(state->B[Q] > 0)
|
---|
517 | state->B[Q] = 0;
|
---|
518 | }
|
---|
519 | }
|
---|
520 |
|
---|
521 | /**
|
---|
522 | * Encode error from run termination
|
---|
523 | */
|
---|
524 | static inline void ls_encode_runterm(JLSState *state, PutBitContext *pb, int RItype, int err, int limit_add){
|
---|
525 | int k;
|
---|
526 | int val, map;
|
---|
527 | int Q = 365 + RItype;
|
---|
528 | int temp;
|
---|
529 |
|
---|
530 | temp = state->A[Q];
|
---|
531 | if(RItype)
|
---|
532 | temp += state->N[Q] >> 1;
|
---|
533 | for(k = 0; (state->N[Q] << k) < temp; k++);
|
---|
534 | map = 0;
|
---|
535 | if(!k && err && (2 * state->B[Q] < state->N[Q]))
|
---|
536 | map = 1;
|
---|
537 |
|
---|
538 | if(err < 0)
|
---|
539 | val = - (2 * err) - 1 - RItype + map;
|
---|
540 | else
|
---|
541 | val = 2 * err - RItype - map;
|
---|
542 | set_ur_golomb_jpegls(pb, val, k, state->limit - limit_add - 1, state->qbpp);
|
---|
543 |
|
---|
544 | if(err < 0)
|
---|
545 | state->B[Q]++;
|
---|
546 | state->A[Q] += (val + 1 - RItype) >> 1;
|
---|
547 |
|
---|
548 | if(state->N[Q] == state->reset) {
|
---|
549 | state->A[Q] >>= 1;
|
---|
550 | state->B[Q] >>= 1;
|
---|
551 | state->N[Q] >>= 1;
|
---|
552 | }
|
---|
553 | state->N[Q]++;
|
---|
554 | }
|
---|
555 |
|
---|
556 | /**
|
---|
557 | * Encode run value as specified by JPEG-LS standard
|
---|
558 | */
|
---|
559 | static inline void ls_encode_run(JLSState *state, PutBitContext *pb, int run, int comp, int trail){
|
---|
560 | while(run >= (1 << log2_run[state->run_index[comp]])){
|
---|
561 | put_bits(pb, 1, 1);
|
---|
562 | run -= 1 << log2_run[state->run_index[comp]];
|
---|
563 | if(state->run_index[comp] < 31)
|
---|
564 | state->run_index[comp]++;
|
---|
565 | }
|
---|
566 | /* if hit EOL, encode another full run, else encode aborted run */
|
---|
567 | if(!trail && run) {
|
---|
568 | put_bits(pb, 1, 1);
|
---|
569 | }else if(trail){
|
---|
570 | put_bits(pb, 1, 0);
|
---|
571 | if(log2_run[state->run_index[comp]])
|
---|
572 | put_bits(pb, log2_run[state->run_index[comp]], run);
|
---|
573 | }
|
---|
574 | }
|
---|
575 |
|
---|
576 | /**
|
---|
577 | * Encode one line of image
|
---|
578 | */
|
---|
579 | static inline void ls_encode_line(JLSState *state, PutBitContext *pb, uint8_t *last, uint8_t *cur, int last2, int w, int stride, int comp){
|
---|
580 | int x = 0;
|
---|
581 | int Ra, Rb, Rc, Rd;
|
---|
582 | int D0, D1, D2;
|
---|
583 |
|
---|
584 | while(x < w) {
|
---|
585 | int err, pred, sign;
|
---|
586 |
|
---|
587 | /* compute gradients */
|
---|
588 | Ra = x ? cur[x - stride] : last[x];
|
---|
589 | Rb = last[x];
|
---|
590 | Rc = x ? last[x - stride] : last2;
|
---|
591 | Rd = (x >= w - stride) ? last[x] : last[x + stride];
|
---|
592 | D0 = Rd - Rb;
|
---|
593 | D1 = Rb - Rc;
|
---|
594 | D2 = Rc - Ra;
|
---|
595 |
|
---|
596 | /* run mode */
|
---|
597 | if((ABS(D0) <= state->near) && (ABS(D1) <= state->near) && (ABS(D2) <= state->near)) {
|
---|
598 | int RUNval, RItype, run;
|
---|
599 |
|
---|
600 | run = 0;
|
---|
601 | RUNval = Ra;
|
---|
602 | while(x < w && (ABS(cur[x] - RUNval) <= state->near)){
|
---|
603 | run++;
|
---|
604 | cur[x] = Ra;
|
---|
605 | x += stride;
|
---|
606 | }
|
---|
607 | ls_encode_run(state, pb, run, comp, x < w);
|
---|
608 | if(x >= w)
|
---|
609 | return;
|
---|
610 | Rb = last[x];
|
---|
611 | RItype = (ABS(Ra - Rb) <= state->near);
|
---|
612 | pred = RItype ? Ra : Rb;
|
---|
613 | err = cur[x] - pred;
|
---|
614 |
|
---|
615 | if(!RItype && Ra > Rb)
|
---|
616 | err = -err;
|
---|
617 |
|
---|
618 | if(state->near){
|
---|
619 | if(err > 0)
|
---|
620 | err = (state->near + err) / state->twonear;
|
---|
621 | else
|
---|
622 | err = -(state->near - err) / state->twonear;
|
---|
623 |
|
---|
624 | if(RItype || (Rb >= Ra))
|
---|
625 | Ra = clip(pred + err * state->twonear, 0, state->maxval);
|
---|
626 | else
|
---|
627 | Ra = clip(pred - err * state->twonear, 0, state->maxval);
|
---|
628 | cur[x] = Ra;
|
---|
629 | }
|
---|
630 | if(err < 0)
|
---|
631 | err += state->range;
|
---|
632 | if(err >= ((state->range + 1) >> 1))
|
---|
633 | err -= state->range;
|
---|
634 |
|
---|
635 | ls_encode_runterm(state, pb, RItype, err, log2_run[state->run_index[comp]]);
|
---|
636 |
|
---|
637 | if(state->run_index[comp] > 0)
|
---|
638 | state->run_index[comp]--;
|
---|
639 | x += stride;
|
---|
640 | } else { /* regular mode */
|
---|
641 | int context;
|
---|
642 |
|
---|
643 | context = quantize(state, D0) * 81 + quantize(state, D1) * 9 + quantize(state, D2);
|
---|
644 | pred = mid_pred(Ra, Ra + Rb - Rc, Rb);
|
---|
645 |
|
---|
646 | if(context < 0){
|
---|
647 | context = -context;
|
---|
648 | sign = 1;
|
---|
649 | pred = clip(pred - state->C[context], 0, state->maxval);
|
---|
650 | err = pred - cur[x];
|
---|
651 | }else{
|
---|
652 | sign = 0;
|
---|
653 | pred = clip(pred + state->C[context], 0, state->maxval);
|
---|
654 | err = cur[x] - pred;
|
---|
655 | }
|
---|
656 |
|
---|
657 | if(state->near){
|
---|
658 | if(err > 0)
|
---|
659 | err = (state->near + err) / state->twonear;
|
---|
660 | else
|
---|
661 | err = -(state->near - err) / state->twonear;
|
---|
662 | if(!sign)
|
---|
663 | Ra = clip(pred + err * state->twonear, 0, state->maxval);
|
---|
664 | else
|
---|
665 | Ra = clip(pred - err * state->twonear, 0, state->maxval);
|
---|
666 | cur[x] = Ra;
|
---|
667 | }
|
---|
668 |
|
---|
669 | ls_encode_regular(state, pb, context, err);
|
---|
670 | x += stride;
|
---|
671 | }
|
---|
672 | }
|
---|
673 | }
|
---|
674 |
|
---|
675 | static void ls_store_lse(JLSState *state, PutBitContext *pb){
|
---|
676 | /* Test if we have default params and don't need to store LSE */
|
---|
677 | JLSState state2;
|
---|
678 | memset(&state2, 0, sizeof(JLSState));
|
---|
679 | state2.bpp = 8;
|
---|
680 | state2.near = state->near;
|
---|
681 | reset_ls_coding_parameters(&state2, 1);
|
---|
682 | if(state->T1 == state2.T1 && state->T2 == state2.T2 && state->T3 == state2.T3 && state->reset == state2.reset)
|
---|
683 | return;
|
---|
684 | /* store LSE type 1 */
|
---|
685 | put_marker(pb, LSE);
|
---|
686 | put_bits(pb, 16, 13);
|
---|
687 | put_bits(pb, 8, 1);
|
---|
688 | put_bits(pb, 16, state->maxval);
|
---|
689 | put_bits(pb, 16, state->T1);
|
---|
690 | put_bits(pb, 16, state->T2);
|
---|
691 | put_bits(pb, 16, state->T3);
|
---|
692 | put_bits(pb, 16, state->reset);
|
---|
693 | }
|
---|
694 |
|
---|
695 | static int encode_picture_ls(AVCodecContext *avctx, unsigned char *buf, int buf_size, void *data){
|
---|
696 | JpeglsContext * const s = avctx->priv_data;
|
---|
697 | AVFrame *pict = data;
|
---|
698 | AVFrame * const p= (AVFrame*)&s->picture;
|
---|
699 | const int near = avctx->prediction_method;
|
---|
700 | PutBitContext pb, pb2;
|
---|
701 | GetBitContext gb;
|
---|
702 | uint8_t *buf2, *zero, *cur, *last;
|
---|
703 | JLSState *state;
|
---|
704 | int i, size;
|
---|
705 | int comps;
|
---|
706 |
|
---|
707 | buf2 = av_malloc(buf_size);
|
---|
708 |
|
---|
709 | init_put_bits(&pb, buf, buf_size);
|
---|
710 | init_put_bits(&pb2, buf2, buf_size);
|
---|
711 |
|
---|
712 | *p = *pict;
|
---|
713 | p->pict_type= FF_I_TYPE;
|
---|
714 | p->key_frame= 1;
|
---|
715 |
|
---|
716 | comps = (avctx->pix_fmt == PIX_FMT_GRAY8) ? 1 : 3;
|
---|
717 |
|
---|
718 | /* write our own JPEG header, can't use mjpeg_picture_header */
|
---|
719 | put_marker(&pb, SOI);
|
---|
720 | put_marker(&pb, SOF48);
|
---|
721 | put_bits(&pb, 16, 8 + comps * 3); // header size depends on components
|
---|
722 | put_bits(&pb, 8, 8); // bpp
|
---|
723 | put_bits(&pb, 16, avctx->height);
|
---|
724 | put_bits(&pb, 16, avctx->width);
|
---|
725 | put_bits(&pb, 8, comps); // components
|
---|
726 | for(i = 1; i <= comps; i++) {
|
---|
727 | put_bits(&pb, 8, i); // component ID
|
---|
728 | put_bits(&pb, 8, 0x11); // subsampling: none
|
---|
729 | put_bits(&pb, 8, 0); // Tiq, used by JPEG-LS ext
|
---|
730 | }
|
---|
731 |
|
---|
732 | put_marker(&pb, SOS);
|
---|
733 | put_bits(&pb, 16, 6 + comps * 2);
|
---|
734 | put_bits(&pb, 8, comps);
|
---|
735 | for(i = 1; i <= comps; i++) {
|
---|
736 | put_bits(&pb, 8, i); // component ID
|
---|
737 | put_bits(&pb, 8, 0); // mapping index: none
|
---|
738 | }
|
---|
739 | put_bits(&pb, 8, near);
|
---|
740 | put_bits(&pb, 8, (comps > 1) ? 1 : 0); // interleaving: 0 - plane, 1 - line
|
---|
741 | put_bits(&pb, 8, 0); // point transform: none
|
---|
742 |
|
---|
743 | state = av_mallocz(sizeof(JLSState));
|
---|
744 | /* initialize JPEG-LS state from JPEG parameters */
|
---|
745 | state->near = near;
|
---|
746 | state->bpp = 8;
|
---|
747 | reset_ls_coding_parameters(state, 0);
|
---|
748 | ls_init_state(state);
|
---|
749 |
|
---|
750 | ls_store_lse(state, &pb);
|
---|
751 |
|
---|
752 | zero = av_mallocz(p->linesize[0]);
|
---|
753 | last = zero;
|
---|
754 | cur = p->data[0];
|
---|
755 | if(avctx->pix_fmt == PIX_FMT_GRAY8){
|
---|
756 | int t = 0;
|
---|
757 |
|
---|
758 | for(i = 0; i < avctx->height; i++) {
|
---|
759 | ls_encode_line(state, &pb2, last, cur, t, avctx->width, 1, 0);
|
---|
760 | t = last[0];
|
---|
761 | last = cur;
|
---|
762 | cur += p->linesize[0];
|
---|
763 | }
|
---|
764 | }else if(avctx->pix_fmt == PIX_FMT_RGB24){
|
---|
765 | int j, width;
|
---|
766 | int Rc[3] = {0, 0, 0};
|
---|
767 |
|
---|
768 | width = avctx->width * 3;
|
---|
769 | for(i = 0; i < avctx->height; i++) {
|
---|
770 | for(j = 0; j < 3; j++) {
|
---|
771 | ls_encode_line(state, &pb2, last + j, cur + j, Rc[j], width, 3, j);
|
---|
772 | Rc[j] = last[j];
|
---|
773 | }
|
---|
774 | last = cur;
|
---|
775 | cur += s->picture.linesize[0];
|
---|
776 | }
|
---|
777 | }else if(avctx->pix_fmt == PIX_FMT_BGR24){
|
---|
778 | int j, width;
|
---|
779 | int Rc[3] = {0, 0, 0};
|
---|
780 |
|
---|
781 | width = avctx->width * 3;
|
---|
782 | for(i = 0; i < avctx->height; i++) {
|
---|
783 | for(j = 2; j >= 0; j--) {
|
---|
784 | ls_encode_line(state, &pb2, last + j, cur + j, Rc[j], width, 3, j);
|
---|
785 | Rc[j] = last[j];
|
---|
786 | }
|
---|
787 | last = cur;
|
---|
788 | cur += s->picture.linesize[0];
|
---|
789 | }
|
---|
790 | }
|
---|
791 |
|
---|
792 | av_free(zero);
|
---|
793 | av_free(state);
|
---|
794 |
|
---|
795 | flush_put_bits(&pb2);
|
---|
796 | /* do escape coding */
|
---|
797 | size = put_bits_count(&pb2) >> 3;
|
---|
798 | init_get_bits(&gb, buf2, size);
|
---|
799 | while(get_bits_count(&gb) < size * 8){
|
---|
800 | int v;
|
---|
801 | v = get_bits(&gb, 8);
|
---|
802 | put_bits(&pb, 8, v);
|
---|
803 | if(v == 0xFF){
|
---|
804 | v = get_bits(&gb, 7);
|
---|
805 | put_bits(&pb, 8, v);
|
---|
806 | }
|
---|
807 | }
|
---|
808 | align_put_bits(&pb);
|
---|
809 | av_free(buf2);
|
---|
810 |
|
---|
811 | /* End of image */
|
---|
812 | put_marker(&pb, EOI);
|
---|
813 | flush_put_bits(&pb);
|
---|
814 |
|
---|
815 | emms_c();
|
---|
816 |
|
---|
817 | return put_bits_count(&pb) >> 3;
|
---|
818 | }
|
---|
819 |
|
---|
820 | static int encode_init_ls(AVCodecContext *ctx) {
|
---|
821 | JpeglsContext *c = (JpeglsContext*)ctx->priv_data;
|
---|
822 |
|
---|
823 | c->avctx = ctx;
|
---|
824 | ctx->coded_frame = &c->picture;
|
---|
825 |
|
---|
826 | if(ctx->pix_fmt != PIX_FMT_GRAY8 && ctx->pix_fmt != PIX_FMT_RGB24 && ctx->pix_fmt != PIX_FMT_BGR24){
|
---|
827 | av_log(ctx, AV_LOG_ERROR, "Only grayscale and RGB24/BGR24 images are supported\n");
|
---|
828 | return -1;
|
---|
829 | }
|
---|
830 | return 0;
|
---|
831 | }
|
---|
832 |
|
---|
833 | AVCodec jpegls_encoder = { //FIXME avoid MPV_* lossless jpeg shouldnt need them
|
---|
834 | "jpegls",
|
---|
835 | CODEC_TYPE_VIDEO,
|
---|
836 | CODEC_ID_JPEGLS,
|
---|
837 | sizeof(JpeglsContext),
|
---|
838 | encode_init_ls,
|
---|
839 | encode_picture_ls,
|
---|
840 | NULL,
|
---|
841 | .pix_fmts= (enum PixelFormat[]){PIX_FMT_BGR24, PIX_FMT_RGB24, PIX_FMT_GRAY8, -1},
|
---|
842 | };
|
---|
843 | #endif
|
---|