]> git.sesse.net Git - ffmpeg/blob - libavcodec/jpeglsdec.c
jpegls: K&R formatting cosmetics
[ffmpeg] / libavcodec / jpeglsdec.c
1 /*
2  * JPEG-LS decoder
3  * Copyright (c) 2003 Michael Niedermayer
4  * Copyright (c) 2006 Konstantin Shishkov
5  *
6  * This file is part of Libav.
7  *
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.
12  *
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.
17  *
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
21  */
22
23 /**
24  * @file
25  * JPEG-LS decoder.
26  */
27
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"
36
37 /*
38  * Uncomment this to significantly speed up decoding of broken JPEG-LS
39  * (or test broken JPEG-LS decoder) and slow down ordinary decoding a bit.
40  *
41  * There is no Golomb code with length >= 32 bits possible, so check and
42  * avoid situation of 32 zeros, Libav Golomb decoder is painfully slow
43  * on this errors.
44  */
45 //#define JLS_BROKEN
46
47 /**
48  * Decode LSE block with initialization parameters
49  */
50 int ff_jpegls_decode_lse(MJpegDecodeContext *s)
51 {
52     int id;
53
54     skip_bits(&s->gb, 16);  /* length: FIXME: verify field validity */
55     id = get_bits(&s->gb, 8);
56
57     switch (id) {
58     case 1:
59         s->maxval = get_bits(&s->gb, 16);
60         s->t1     = get_bits(&s->gb, 16);
61         s->t2     = get_bits(&s->gb, 16);
62         s->t3     = get_bits(&s->gb, 16);
63         s->reset  = get_bits(&s->gb, 16);
64
65 //        ff_jpegls_reset_coding_parameters(s, 0);
66         //FIXME quant table?
67         break;
68     case 2:
69     case 3:
70         av_log(s->avctx, AV_LOG_ERROR, "palette not supported\n");
71         return -1;
72     case 4:
73         av_log(s->avctx, AV_LOG_ERROR, "oversize image not supported\n");
74         return -1;
75     default:
76         av_log(s->avctx, AV_LOG_ERROR, "invalid id %d\n", id);
77         return -1;
78     }
79     av_dlog(s->avctx, "ID=%i, T=%i,%i,%i\n", id, s->t1, s->t2, s->t3);
80
81     return 0;
82 }
83
84 /**
85  * Get context-dependent Golomb code, decode it and update context
86  */
87 static inline int ls_get_code_regular(GetBitContext *gb, JLSState *state, int Q)
88 {
89     int k, ret;
90
91     for (k = 0; (state->N[Q] << k) < state->A[Q]; k++)
92         ;
93
94 #ifdef JLS_BROKEN
95     if (!show_bits_long(gb, 32))
96         return -1;
97 #endif
98     ret = get_ur_golomb_jpegls(gb, k, state->limit, state->qbpp);
99
100     /* decode mapped error */
101     if (ret & 1)
102         ret = -(ret + 1 >> 1);
103     else
104         ret >>= 1;
105
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]))
108         ret = -(ret + 1);
109
110     ret = ff_jpegls_update_state_regular(state, Q, ret);
111
112     return ret;
113 }
114
115 /**
116  * Get Golomb code, decode it and update state for run termination
117  */
118 static inline int ls_get_code_runterm(GetBitContext *gb, JLSState *state,
119                                       int RItype, int limit_add)
120 {
121     int k, ret, temp, map;
122     int Q = 365 + RItype;
123
124     temp = state->A[Q];
125     if (RItype)
126         temp += state->N[Q] >> 1;
127
128     for (k = 0; (state->N[Q] << k) < temp; k++)
129         ;
130
131 #ifdef JLS_BROKEN
132     if (!show_bits_long(gb, 32))
133         return -1;
134 #endif
135     ret = get_ur_golomb_jpegls(gb, k, state->limit - limit_add - 1,
136                                state->qbpp);
137
138     /* decode mapped error */
139     map = 0;
140     if (!k && (RItype || ret) && (2 * state->B[Q] < state->N[Q]))
141         map = 1;
142     ret += RItype + map;
143
144     if (ret & 1) {
145         ret = map - (ret + 1 >> 1);
146         state->B[Q]++;
147     } else {
148         ret = ret >> 1;
149     }
150
151     /* update state */
152     state->A[Q] += FFABS(ret) - RItype;
153     ret         *= state->twonear;
154     ff_jpegls_downscale_state(state, Q);
155
156     return ret;
157 }
158
159 /**
160  * Decode one line of image
161  */
162 static inline void ls_decode_line(JLSState *state, MJpegDecodeContext *s,
163                                   void *last, void *dst, int last2, int w,
164                                   int stride, int comp, int bits)
165 {
166     int i, x = 0;
167     int Ra, Rb, Rc, Rd;
168     int D0, D1, D2;
169
170     while (x < w) {
171         int err, pred;
172
173         /* compute gradients */
174         Ra = x ? R(dst, x - stride) : R(last, x);
175         Rb = R(last, x);
176         Rc = x ? R(last, x - stride) : last2;
177         Rd = (x >= w - stride) ? R(last, x) : R(last, x + stride);
178         D0 = Rd - Rb;
179         D1 = Rb - Rc;
180         D2 = Rc - Ra;
181         /* run mode */
182         if ((FFABS(D0) <= state->near) &&
183             (FFABS(D1) <= state->near) &&
184             (FFABS(D2) <= state->near)) {
185             int r;
186             int RItype;
187
188             /* decode full runs while available */
189             while (get_bits1(&s->gb)) {
190                 int r;
191                 r = 1 << ff_log2_run[state->run_index[comp]];
192                 if (x + r * stride > w)
193                     r = (w - x) / stride;
194                 for (i = 0; i < r; i++) {
195                     W(dst, x, Ra);
196                     x += stride;
197                 }
198                 /* if EOL reached, we stop decoding */
199                 if (r != 1 << ff_log2_run[state->run_index[comp]])
200                     return;
201                 if (state->run_index[comp] < 31)
202                     state->run_index[comp]++;
203                 if (x + stride > w)
204                     return;
205             }
206             /* decode aborted run */
207             r = ff_log2_run[state->run_index[comp]];
208             if (r)
209                 r = get_bits_long(&s->gb, r);
210             for (i = 0; i < r; i++) {
211                 W(dst, x, Ra);
212                 x += stride;
213             }
214
215             /* decode run termination value */
216             Rb     = R(last, x);
217             RItype = (FFABS(Ra - Rb) <= state->near) ? 1 : 0;
218             err    = ls_get_code_runterm(&s->gb, state, RItype,
219                                          ff_log2_run[state->run_index[comp]]);
220             if (state->run_index[comp])
221                 state->run_index[comp]--;
222
223             if (state->near && RItype) {
224                 pred = Ra + err;
225             } else {
226                 if (Rb < Ra)
227                     pred = Rb - err;
228                 else
229                     pred = Rb + err;
230             }
231         } else { /* regular mode */
232             int context, sign;
233
234             context = ff_jpegls_quantize(state, D0) * 81 +
235                       ff_jpegls_quantize(state, D1) *  9 +
236                       ff_jpegls_quantize(state, D2);
237             pred    = mid_pred(Ra, Ra + Rb - Rc, Rb);
238
239             if (context < 0) {
240                 context = -context;
241                 sign    = 1;
242             } else {
243                 sign = 0;
244             }
245
246             if (sign) {
247                 pred = av_clip(pred - state->C[context], 0, state->maxval);
248                 err  = -ls_get_code_regular(&s->gb, state, context);
249             } else {
250                 pred = av_clip(pred + state->C[context], 0, state->maxval);
251                 err  = ls_get_code_regular(&s->gb, state, context);
252             }
253
254             /* we have to do something more for near-lossless coding */
255             pred += err;
256         }
257         if (state->near) {
258             if (pred < -state->near)
259                 pred += state->range * state->twonear;
260             else if (pred > state->maxval + state->near)
261                 pred -= state->range * state->twonear;
262             pred = av_clip(pred, 0, state->maxval);
263         }
264
265         pred &= state->maxval;
266         W(dst, x, pred);
267         x += stride;
268     }
269 }
270
271 int ff_jpegls_decode_picture(MJpegDecodeContext *s, int near,
272                              int point_transform, int ilv)
273 {
274     int i, t = 0;
275     uint8_t *zero, *last, *cur;
276     JLSState *state;
277     int off = 0, stride = 1, width, shift;
278
279     zero = av_mallocz(s->picture_ptr->linesize[0]);
280     last = zero;
281     cur  = s->picture_ptr->data[0];
282
283     state = av_mallocz(sizeof(JLSState));
284     /* initialize JPEG-LS state from JPEG parameters */
285     state->near   = near;
286     state->bpp    = (s->bits < 2) ? 2 : s->bits;
287     state->maxval = s->maxval;
288     state->T1     = s->t1;
289     state->T2     = s->t2;
290     state->T3     = s->t3;
291     state->reset  = s->reset;
292     ff_jpegls_reset_coding_parameters(state, 0);
293     ff_jpegls_init_state(state);
294
295     if (s->bits <= 8)
296         shift = point_transform + (8 - s->bits);
297     else
298         shift = point_transform + (16 - s->bits);
299
300     av_dlog(s->avctx,
301             "JPEG-LS params: %ix%i NEAR=%i MV=%i T(%i,%i,%i) "
302             "RESET=%i, LIMIT=%i, qbpp=%i, RANGE=%i\n",
303             s->width, s->height, state->near, state->maxval,
304             state->T1, state->T2, state->T3,
305             state->reset, state->limit, state->qbpp, state->range);
306     av_dlog(s->avctx, "JPEG params: ILV=%i Pt=%i BPP=%i, scan = %i\n",
307             ilv, point_transform, s->bits, s->cur_scan);
308     if (ilv == 0) { /* separate planes */
309         off    = s->cur_scan - 1;
310         stride = (s->nb_components > 1) ? 3 : 1;
311         width  = s->width * stride;
312         cur   += off;
313         for (i = 0; i < s->height; i++) {
314             if (s->bits <= 8) {
315                 ls_decode_line(state, s, last, cur, t, width, stride, off, 8);
316                 t = last[0];
317             } else {
318                 ls_decode_line(state, s, last, cur, t, width, stride, off, 16);
319                 t = *((uint16_t *)last);
320             }
321             last = cur;
322             cur += s->picture_ptr->linesize[0];
323
324             if (s->restart_interval && !--s->restart_count) {
325                 align_get_bits(&s->gb);
326                 skip_bits(&s->gb, 16); /* skip RSTn */
327             }
328         }
329     } else if (ilv == 1) { /* line interleaving */
330         int j;
331         int Rc[3] = { 0, 0, 0 };
332         memset(cur, 0, s->picture_ptr->linesize[0]);
333         width = s->width * 3;
334         for (i = 0; i < s->height; i++) {
335             for (j = 0; j < 3; j++) {
336                 ls_decode_line(state, s, last + j, cur + j,
337                                Rc[j], width, 3, j, 8);
338                 Rc[j] = last[j];
339
340                 if (s->restart_interval && !--s->restart_count) {
341                     align_get_bits(&s->gb);
342                     skip_bits(&s->gb, 16); /* skip RSTn */
343                 }
344             }
345             last = cur;
346             cur += s->picture_ptr->linesize[0];
347         }
348     } else if (ilv == 2) { /* sample interleaving */
349         av_log(s->avctx, AV_LOG_ERROR,
350                "Sample interleaved images are not supported.\n");
351         av_free(state);
352         av_free(zero);
353         return -1;
354     }
355
356     if (shift) { /* we need to do point transform or normalize samples */
357         int x, w;
358
359         w = s->width * s->nb_components;
360
361         if (s->bits <= 8) {
362             uint8_t *src = s->picture_ptr->data[0];
363
364             for (i = 0; i < s->height; i++) {
365                 for (x = off; x < w; x += stride)
366                     src[x] <<= shift;
367                 src += s->picture_ptr->linesize[0];
368             }
369         } else {
370             uint16_t *src = (uint16_t *)s->picture_ptr->data[0];
371
372             for (i = 0; i < s->height; i++) {
373                 for (x = 0; x < w; x++)
374                     src[x] <<= shift;
375                 src += s->picture_ptr->linesize[0] / 2;
376             }
377         }
378     }
379     av_free(state);
380     av_free(zero);
381
382     return 0;
383 }
384
385 AVCodec ff_jpegls_decoder = {
386     .name           = "jpegls",
387     .long_name      = NULL_IF_CONFIG_SMALL("JPEG-LS"),
388     .type           = AVMEDIA_TYPE_VIDEO,
389     .id             = AV_CODEC_ID_JPEGLS,
390     .priv_data_size = sizeof(MJpegDecodeContext),
391     .init           = ff_mjpeg_decode_init,
392     .close          = ff_mjpeg_decode_end,
393     .decode         = ff_mjpeg_decode_frame,
394     .capabilities   = CODEC_CAP_DR1,
395 };