lavfi: switch to AVFrame.
[FFMpeg-mirror/mplayer-patches.git] / libavcodec / jpeglsdec.c
blob8a558476c318a4f189f42c139c36492059a993b1
1 /*
2 * JPEG-LS decoder
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
23 /**
24 * @file
25 * JPEG-LS decoder.
28 #include "avcodec.h"
29 #include "get_bits.h"
30 #include "golomb.h"
31 #include "mathops.h"
32 #include "mjpeg.h"
33 #include "mjpegdec.h"
34 #include "jpegls.h"
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
44 * on this errors.
46 //#define JLS_BROKEN
49 /**
50 * Decode LSE block with initialization parameters
52 int ff_jpegls_decode_lse(MJpegDecodeContext *s)
54 int id;
56 skip_bits(&s->gb, 16); /* length: FIXME: verify field validity */
57 id = get_bits(&s->gb, 8);
59 switch(id){
60 case 1:
61 s->maxval= get_bits(&s->gb, 16);
62 s->t1= get_bits(&s->gb, 16);
63 s->t2= get_bits(&s->gb, 16);
64 s->t3= get_bits(&s->gb, 16);
65 s->reset= get_bits(&s->gb, 16);
67 // ff_jpegls_reset_coding_parameters(s, 0);
68 //FIXME quant table?
69 break;
70 case 2:
71 case 3:
72 av_log(s->avctx, AV_LOG_ERROR, "palette not supported\n");
73 return -1;
74 case 4:
75 av_log(s->avctx, AV_LOG_ERROR, "oversize image not supported\n");
76 return -1;
77 default:
78 av_log(s->avctx, AV_LOG_ERROR, "invalid id %d\n", id);
79 return -1;
81 av_dlog(s->avctx, "ID=%i, T=%i,%i,%i\n", id, s->t1, s->t2, s->t3);
83 return 0;
86 /**
87 * Get context-dependent Golomb code, decode it and update context
89 static inline int ls_get_code_regular(GetBitContext *gb, JLSState *state, int Q){
90 int k, ret;
92 for(k = 0; (state->N[Q] << k) < state->A[Q]; k++);
94 #ifdef JLS_BROKEN
95 if(!show_bits_long(gb, 32))return -1;
96 #endif
97 ret = get_ur_golomb_jpegls(gb, k, state->limit, state->qbpp);
99 /* decode mapped error */
100 if(ret & 1)
101 ret = -((ret + 1) >> 1);
102 else
103 ret >>= 1;
105 /* for NEAR=0, k=0 and 2*B[Q] <= - N[Q] mapping is reversed */
106 if(!state->near && !k && (2 * state->B[Q] <= -state->N[Q]))
107 ret = -(ret + 1);
109 ret= ff_jpegls_update_state_regular(state, Q, ret);
111 return ret;
115 * Get Golomb code, decode it and update state for run termination
117 static inline int ls_get_code_runterm(GetBitContext *gb, JLSState *state, int RItype, int limit_add){
118 int k, ret, temp, map;
119 int Q = 365 + RItype;
121 temp= state->A[Q];
122 if(RItype)
123 temp += state->N[Q] >> 1;
125 for(k = 0; (state->N[Q] << k) < temp; k++);
127 #ifdef JLS_BROKEN
128 if(!show_bits_long(gb, 32))return -1;
129 #endif
130 ret = get_ur_golomb_jpegls(gb, k, state->limit - limit_add - 1, state->qbpp);
132 /* decode mapped error */
133 map = 0;
134 if(!k && (RItype || ret) && (2 * state->B[Q] < state->N[Q]))
135 map = 1;
136 ret += RItype + map;
138 if(ret & 1){
139 ret = map - ((ret + 1) >> 1);
140 state->B[Q]++;
141 } else {
142 ret = ret >> 1;
145 /* update state */
146 state->A[Q] += FFABS(ret) - RItype;
147 ret *= state->twonear;
148 ff_jpegls_downscale_state(state, Q);
150 return ret;
154 * Decode one line of image
156 static inline void ls_decode_line(JLSState *state, MJpegDecodeContext *s, void *last, void *dst, int last2, int w, int stride, int comp, int bits){
157 int i, x = 0;
158 int Ra, Rb, Rc, Rd;
159 int D0, D1, D2;
161 while(x < w) {
162 int err, pred;
164 /* compute gradients */
165 Ra = x ? R(dst, x - stride) : R(last, x);
166 Rb = R(last, x);
167 Rc = x ? R(last, x - stride) : last2;
168 Rd = (x >= w - stride) ? R(last, x) : R(last, x + stride);
169 D0 = Rd - Rb;
170 D1 = Rb - Rc;
171 D2 = Rc - Ra;
172 /* run mode */
173 if((FFABS(D0) <= state->near) && (FFABS(D1) <= state->near) && (FFABS(D2) <= state->near)) {
174 int r;
175 int RItype;
177 /* decode full runs while available */
178 while(get_bits1(&s->gb)) {
179 int r;
180 r = 1 << ff_log2_run[state->run_index[comp]];
181 if(x + r * stride > w) {
182 r = (w - x) / stride;
184 for(i = 0; i < r; i++) {
185 W(dst, x, Ra);
186 x += stride;
188 /* if EOL reached, we stop decoding */
189 if(r != (1 << ff_log2_run[state->run_index[comp]]))
190 return;
191 if(state->run_index[comp] < 31)
192 state->run_index[comp]++;
193 if(x + stride > w)
194 return;
196 /* decode aborted run */
197 r = ff_log2_run[state->run_index[comp]];
198 if(r)
199 r = get_bits_long(&s->gb, r);
200 for(i = 0; i < r; i++) {
201 W(dst, x, Ra);
202 x += stride;
205 /* decode run termination value */
206 Rb = R(last, x);
207 RItype = (FFABS(Ra - Rb) <= state->near) ? 1 : 0;
208 err = ls_get_code_runterm(&s->gb, state, RItype, ff_log2_run[state->run_index[comp]]);
209 if(state->run_index[comp])
210 state->run_index[comp]--;
212 if(state->near && RItype){
213 pred = Ra + err;
214 } else {
215 if(Rb < Ra)
216 pred = Rb - err;
217 else
218 pred = Rb + err;
220 } else { /* regular mode */
221 int context, sign;
223 context = ff_jpegls_quantize(state, D0) * 81 + ff_jpegls_quantize(state, D1) * 9 + ff_jpegls_quantize(state, D2);
224 pred = mid_pred(Ra, Ra + Rb - Rc, Rb);
226 if(context < 0){
227 context = -context;
228 sign = 1;
229 }else{
230 sign = 0;
233 if(sign){
234 pred = av_clip(pred - state->C[context], 0, state->maxval);
235 err = -ls_get_code_regular(&s->gb, state, context);
236 } else {
237 pred = av_clip(pred + state->C[context], 0, state->maxval);
238 err = ls_get_code_regular(&s->gb, state, context);
241 /* we have to do something more for near-lossless coding */
242 pred += err;
244 if(state->near){
245 if(pred < -state->near)
246 pred += state->range * state->twonear;
247 else if(pred > state->maxval + state->near)
248 pred -= state->range * state->twonear;
249 pred = av_clip(pred, 0, state->maxval);
252 pred &= state->maxval;
253 W(dst, x, pred);
254 x += stride;
258 int ff_jpegls_decode_picture(MJpegDecodeContext *s, int near, int point_transform, int ilv){
259 int i, t = 0;
260 uint8_t *zero, *last, *cur;
261 JLSState *state;
262 int off = 0, stride = 1, width, shift;
264 zero = av_mallocz(s->picture_ptr->linesize[0]);
265 last = zero;
266 cur = s->picture_ptr->data[0];
268 state = av_mallocz(sizeof(JLSState));
269 /* initialize JPEG-LS state from JPEG parameters */
270 state->near = near;
271 state->bpp = (s->bits < 2) ? 2 : s->bits;
272 state->maxval = s->maxval;
273 state->T1 = s->t1;
274 state->T2 = s->t2;
275 state->T3 = s->t3;
276 state->reset = s->reset;
277 ff_jpegls_reset_coding_parameters(state, 0);
278 ff_jpegls_init_state(state);
280 if(s->bits <= 8)
281 shift = point_transform + (8 - s->bits);
282 else
283 shift = point_transform + (16 - s->bits);
285 av_dlog(s->avctx, "JPEG-LS params: %ix%i NEAR=%i MV=%i T(%i,%i,%i) RESET=%i, LIMIT=%i, qbpp=%i, RANGE=%i\n",
286 s->width, s->height, state->near, state->maxval,
287 state->T1, state->T2, state->T3,
288 state->reset, state->limit, state->qbpp, state->range);
289 av_dlog(s->avctx, "JPEG params: ILV=%i Pt=%i BPP=%i, scan = %i\n",
290 ilv, point_transform, s->bits, s->cur_scan);
291 if(ilv == 0) { /* separate planes */
292 off = s->cur_scan - 1;
293 stride = (s->nb_components > 1) ? 3 : 1;
294 width = s->width * stride;
295 cur += off;
296 for(i = 0; i < s->height; i++) {
297 if(s->bits <= 8){
298 ls_decode_line(state, s, last, cur, t, width, stride, off, 8);
299 t = last[0];
300 }else{
301 ls_decode_line(state, s, last, cur, t, width, stride, off, 16);
302 t = *((uint16_t*)last);
304 last = cur;
305 cur += s->picture_ptr->linesize[0];
307 if (s->restart_interval && !--s->restart_count) {
308 align_get_bits(&s->gb);
309 skip_bits(&s->gb, 16); /* skip RSTn */
312 } else if(ilv == 1) { /* line interleaving */
313 int j;
314 int Rc[3] = {0, 0, 0};
315 memset(cur, 0, s->picture_ptr->linesize[0]);
316 width = s->width * 3;
317 for(i = 0; i < s->height; i++) {
318 for(j = 0; j < 3; j++) {
319 ls_decode_line(state, s, last + j, cur + j, Rc[j], width, 3, j, 8);
320 Rc[j] = last[j];
322 if (s->restart_interval && !--s->restart_count) {
323 align_get_bits(&s->gb);
324 skip_bits(&s->gb, 16); /* skip RSTn */
327 last = cur;
328 cur += s->picture_ptr->linesize[0];
330 } else if(ilv == 2) { /* sample interleaving */
331 av_log(s->avctx, AV_LOG_ERROR, "Sample interleaved images are not supported.\n");
332 av_free(state);
333 av_free(zero);
334 return -1;
337 if(shift){ /* we need to do point transform or normalize samples */
338 int x, w;
340 w = s->width * s->nb_components;
342 if(s->bits <= 8){
343 uint8_t *src = s->picture_ptr->data[0];
345 for(i = 0; i < s->height; i++){
346 for(x = off; x < w; x+= stride){
347 src[x] <<= shift;
349 src += s->picture_ptr->linesize[0];
351 }else{
352 uint16_t *src = (uint16_t*) s->picture_ptr->data[0];
354 for(i = 0; i < s->height; i++){
355 for(x = 0; x < w; x++){
356 src[x] <<= shift;
358 src += s->picture_ptr->linesize[0]/2;
362 av_free(state);
363 av_free(zero);
365 return 0;
369 AVCodec ff_jpegls_decoder = {
370 .name = "jpegls",
371 .type = AVMEDIA_TYPE_VIDEO,
372 .id = AV_CODEC_ID_JPEGLS,
373 .priv_data_size = sizeof(MJpegDecodeContext),
374 .init = ff_mjpeg_decode_init,
375 .close = ff_mjpeg_decode_end,
376 .decode = ff_mjpeg_decode_frame,
377 .capabilities = CODEC_CAP_DR1,
378 .long_name = NULL_IF_CONFIG_SMALL("JPEG-LS"),