3 * Copyright (c) 2003 Michael Niedermayer
4 * Copyright (c) 2006 Konstantin Shishkov
6 * This file is part of Libav.
8 * Libav is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; either
11 * version 2.1 of the License, or (at your option) any later version.
13 * Libav is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with Libav; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
35 #include "jpeglsdec.h"
39 * Uncomment this to significantly speed up decoding of broken JPEG-LS
40 * (or test broken JPEG-LS decoder) and slow down ordinary decoding a bit.
42 * There is no Golomb code with length >= 32 bits possible, so check and
43 * avoid situation of 32 zeros, Libav Golomb decoder is painfully slow
50 * Decode LSE block with initialization parameters
52 int ff_jpegls_decode_lse(MJpegDecodeContext *s)
56 /* XXX: verify len field validity */
57 len = get_bits(&s->gb, 16);
58 id = get_bits(&s->gb, 8);
62 s->maxval= get_bits(&s->gb, 16);
63 s->t1= get_bits(&s->gb, 16);
64 s->t2= get_bits(&s->gb, 16);
65 s->t3= get_bits(&s->gb, 16);
66 s->reset= get_bits(&s->gb, 16);
68 // ff_jpegls_reset_coding_parameters(s, 0);
73 av_log(s->avctx, AV_LOG_ERROR, "palette not supported\n");
76 av_log(s->avctx, AV_LOG_ERROR, "oversize image not supported\n");
79 av_log(s->avctx, AV_LOG_ERROR, "invalid id %d\n", id);
82 // av_log(s->avctx, AV_LOG_DEBUG, "ID=%i, T=%i,%i,%i\n", id, s->t1, s->t2, s->t3);
88 * Get context-dependent Golomb code, decode it and update context
90 static inline int ls_get_code_regular(GetBitContext *gb, JLSState *state, int Q){
93 for(k = 0; (state->N[Q] << k) < state->A[Q]; k++);
96 if(!show_bits_long(gb, 32))return -1;
98 ret = get_ur_golomb_jpegls(gb, k, state->limit, state->qbpp);
100 /* decode mapped error */
102 ret = -((ret + 1) >> 1);
106 /* for NEAR=0, k=0 and 2*B[Q] <= - N[Q] mapping is reversed */
107 if(!state->near && !k && (2 * state->B[Q] <= -state->N[Q]))
110 ret= ff_jpegls_update_state_regular(state, Q, ret);
116 * Get Golomb code, decode it and update state for run termination
118 static inline int ls_get_code_runterm(GetBitContext *gb, JLSState *state, int RItype, int limit_add){
119 int k, ret, temp, map;
120 int Q = 365 + RItype;
124 temp += state->N[Q] >> 1;
126 for(k = 0; (state->N[Q] << k) < temp; k++);
129 if(!show_bits_long(gb, 32))return -1;
131 ret = get_ur_golomb_jpegls(gb, k, state->limit - limit_add - 1, state->qbpp);
133 /* decode mapped error */
135 if(!k && (RItype || ret) && (2 * state->B[Q] < state->N[Q]))
140 ret = map - ((ret + 1) >> 1);
147 state->A[Q] += FFABS(ret) - RItype;
148 ret *= state->twonear;
149 ff_jpegls_downscale_state(state, Q);
155 * Decode one line of image
157 static inline void ls_decode_line(JLSState *state, MJpegDecodeContext *s, void *last, void *dst, int last2, int w, int stride, int comp, int bits){
165 /* compute gradients */
166 Ra = x ? R(dst, x - stride) : R(last, x);
168 Rc = x ? R(last, x - stride) : last2;
169 Rd = (x >= w - stride) ? R(last, x) : R(last, x + stride);
174 if((FFABS(D0) <= state->near) && (FFABS(D1) <= state->near) && (FFABS(D2) <= state->near)) {
178 /* decode full runs while available */
179 while(get_bits1(&s->gb)) {
181 r = 1 << ff_log2_run[state->run_index[comp]];
182 if(x + r * stride > w) {
183 r = (w - x) / stride;
185 for(i = 0; i < r; i++) {
189 /* if EOL reached, we stop decoding */
190 if(r != (1 << ff_log2_run[state->run_index[comp]]))
192 if(state->run_index[comp] < 31)
193 state->run_index[comp]++;
197 /* decode aborted run */
198 r = ff_log2_run[state->run_index[comp]];
200 r = get_bits_long(&s->gb, r);
201 for(i = 0; i < r; i++) {
206 /* decode run termination value */
208 RItype = (FFABS(Ra - Rb) <= state->near) ? 1 : 0;
209 err = ls_get_code_runterm(&s->gb, state, RItype, ff_log2_run[state->run_index[comp]]);
210 if(state->run_index[comp])
211 state->run_index[comp]--;
213 if(state->near && RItype){
221 } else { /* regular mode */
224 context = ff_jpegls_quantize(state, D0) * 81 + ff_jpegls_quantize(state, D1) * 9 + ff_jpegls_quantize(state, D2);
225 pred = mid_pred(Ra, Ra + Rb - Rc, Rb);
235 pred = av_clip(pred - state->C[context], 0, state->maxval);
236 err = -ls_get_code_regular(&s->gb, state, context);
238 pred = av_clip(pred + state->C[context], 0, state->maxval);
239 err = ls_get_code_regular(&s->gb, state, context);
242 /* we have to do something more for near-lossless coding */
246 if(pred < -state->near)
247 pred += state->range * state->twonear;
248 else if(pred > state->maxval + state->near)
249 pred -= state->range * state->twonear;
250 pred = av_clip(pred, 0, state->maxval);
253 pred &= state->maxval;
259 int ff_jpegls_decode_picture(MJpegDecodeContext *s, int near, int point_transform, int ilv){
261 uint8_t *zero, *last, *cur;
263 int off = 0, stride = 1, width, shift;
265 zero = av_mallocz(s->picture_ptr->linesize[0]);
267 cur = s->picture_ptr->data[0];
269 state = av_mallocz(sizeof(JLSState));
270 /* initialize JPEG-LS state from JPEG parameters */
272 state->bpp = (s->bits < 2) ? 2 : s->bits;
273 state->maxval = s->maxval;
277 state->reset = s->reset;
278 ff_jpegls_reset_coding_parameters(state, 0);
279 ff_jpegls_init_state(state);
282 shift = point_transform + (8 - s->bits);
284 shift = point_transform + (16 - s->bits);
286 // 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);
287 // 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);
288 if(ilv == 0) { /* separate planes */
289 off = s->cur_scan - 1;
290 stride = (s->nb_components > 1) ? 3 : 1;
291 width = s->width * stride;
293 for(i = 0; i < s->height; i++) {
295 ls_decode_line(state, s, last, cur, t, width, stride, off, 8);
298 ls_decode_line(state, s, last, cur, t, width, stride, off, 16);
299 t = *((uint16_t*)last);
302 cur += s->picture_ptr->linesize[0];
304 if (s->restart_interval && !--s->restart_count) {
305 align_get_bits(&s->gb);
306 skip_bits(&s->gb, 16); /* skip RSTn */
309 } else if(ilv == 1) { /* line interleaving */
311 int Rc[3] = {0, 0, 0};
312 memset(cur, 0, s->picture_ptr->linesize[0]);
313 width = s->width * 3;
314 for(i = 0; i < s->height; i++) {
315 for(j = 0; j < 3; j++) {
316 ls_decode_line(state, s, last + j, cur + j, Rc[j], width, 3, j, 8);
319 if (s->restart_interval && !--s->restart_count) {
320 align_get_bits(&s->gb);
321 skip_bits(&s->gb, 16); /* skip RSTn */
325 cur += s->picture_ptr->linesize[0];
327 } else if(ilv == 2) { /* sample interleaving */
328 av_log(s->avctx, AV_LOG_ERROR, "Sample interleaved images are not supported.\n");
334 if(shift){ /* we need to do point transform or normalize samples */
337 w = s->width * s->nb_components;
340 uint8_t *src = s->picture_ptr->data[0];
342 for(i = 0; i < s->height; i++){
343 for(x = off; x < w; x+= stride){
346 src += s->picture_ptr->linesize[0];
349 uint16_t *src = (uint16_t*) s->picture_ptr->data[0];
351 for(i = 0; i < s->height; i++){
352 for(x = 0; x < w; x++){
355 src += s->picture_ptr->linesize[0]/2;
366 AVCodec ff_jpegls_decoder = {
368 .type = AVMEDIA_TYPE_VIDEO,
369 .id = CODEC_ID_JPEGLS,
370 .priv_data_size = sizeof(MJpegDecodeContext),
371 .init = ff_mjpeg_decode_init,
372 .close = ff_mjpeg_decode_end,
373 .decode = ff_mjpeg_decode_frame,
374 .capabilities = CODEC_CAP_DR1,
375 .long_name = NULL_IF_CONFIG_SMALL("JPEG-LS"),