OSDN Git Service

Merge remote-tracking branch 'qatar/master'
[android-x86/external-ffmpeg.git] / 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 FFmpeg.
7  *
8  * FFmpeg 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  * FFmpeg 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 FFmpeg; 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, FFmpeg 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 AVERROR(ENOSYS);
72     case 4:
73         av_log(s->avctx, AV_LOG_ERROR, "oversize image not supported\n");
74         return AVERROR(ENOSYS);
75     default:
76         av_log(s->avctx, AV_LOG_ERROR, "invalid id %d\n", id);
77         return AVERROR_INVALIDDATA;
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             if (x + r * stride > w) {
211                 r = (w - x) / stride;
212             }
213             for (i = 0; i < r; i++) {
214                 W(dst, x, Ra);
215                 x += stride;
216             }
217
218             /* decode run termination value */
219             Rb     = R(last, x);
220             RItype = (FFABS(Ra - Rb) <= state->near) ? 1 : 0;
221             err    = ls_get_code_runterm(&s->gb, state, RItype,
222                                          ff_log2_run[state->run_index[comp]]);
223             if (state->run_index[comp])
224                 state->run_index[comp]--;
225
226             if (state->near && RItype) {
227                 pred = Ra + err;
228             } else {
229                 if (Rb < Ra)
230                     pred = Rb - err;
231                 else
232                     pred = Rb + err;
233             }
234         } else { /* regular mode */
235             int context, sign;
236
237             context = ff_jpegls_quantize(state, D0) * 81 +
238                       ff_jpegls_quantize(state, D1) *  9 +
239                       ff_jpegls_quantize(state, D2);
240             pred    = mid_pred(Ra, Ra + Rb - Rc, Rb);
241
242             if (context < 0) {
243                 context = -context;
244                 sign    = 1;
245             } else {
246                 sign = 0;
247             }
248
249             if (sign) {
250                 pred = av_clip(pred - state->C[context], 0, state->maxval);
251                 err  = -ls_get_code_regular(&s->gb, state, context);
252             } else {
253                 pred = av_clip(pred + state->C[context], 0, state->maxval);
254                 err  = ls_get_code_regular(&s->gb, state, context);
255             }
256
257             /* we have to do something more for near-lossless coding */
258             pred += err;
259         }
260         if (state->near) {
261             if (pred < -state->near)
262                 pred += state->range * state->twonear;
263             else if (pred > state->maxval + state->near)
264                 pred -= state->range * state->twonear;
265             pred = av_clip(pred, 0, state->maxval);
266         }
267
268         pred &= state->maxval;
269         W(dst, x, pred);
270         x += stride;
271     }
272 }
273
274 int ff_jpegls_decode_picture(MJpegDecodeContext *s, int near,
275                              int point_transform, int ilv)
276 {
277     int i, t = 0;
278     uint8_t *zero, *last, *cur;
279     JLSState *state;
280     int off = 0, stride = 1, width, shift, ret = 0;
281
282     zero = av_mallocz(s->picture.linesize[0]);
283     last = zero;
284     cur  = s->picture.data[0];
285
286     state = av_mallocz(sizeof(JLSState));
287     /* initialize JPEG-LS state from JPEG parameters */
288     state->near   = near;
289     state->bpp    = (s->bits < 2) ? 2 : s->bits;
290     state->maxval = s->maxval;
291     state->T1     = s->t1;
292     state->T2     = s->t2;
293     state->T3     = s->t3;
294     state->reset  = s->reset;
295     ff_jpegls_reset_coding_parameters(state, 0);
296     ff_jpegls_init_state(state);
297
298     if (s->bits <= 8)
299         shift = point_transform + (8 - s->bits);
300     else
301         shift = point_transform + (16 - s->bits);
302
303     if (s->avctx->debug & FF_DEBUG_PICT_INFO) {
304         av_log(s->avctx, AV_LOG_DEBUG,
305                "JPEG-LS params: %ix%i NEAR=%i MV=%i T(%i,%i,%i) "
306                "RESET=%i, LIMIT=%i, qbpp=%i, RANGE=%i\n",
307                 s->width, s->height, state->near, state->maxval,
308                 state->T1, state->T2, state->T3,
309                 state->reset, state->limit, state->qbpp, state->range);
310         av_log(s->avctx, AV_LOG_DEBUG, "JPEG params: ILV=%i Pt=%i BPP=%i, scan = %i\n",
311                 ilv, point_transform, s->bits, s->cur_scan);
312     }
313     if (ilv == 0) { /* separate planes */
314         if (s->cur_scan > s->nb_components) {
315             ret = AVERROR_INVALIDDATA;
316             goto end;
317         }
318         stride = (s->nb_components > 1) ? 3 : 1;
319         off    = av_clip(s->cur_scan - 1, 0, stride - 1);
320         width  = s->width * stride;
321         cur   += off;
322         for (i = 0; i < s->height; i++) {
323             if (s->bits <= 8) {
324                 ls_decode_line(state, s, last, cur, t, width, stride, off, 8);
325                 t = last[0];
326             } else {
327                 ls_decode_line(state, s, last, cur, t, width, stride, off, 16);
328                 t = *((uint16_t *)last);
329             }
330             last = cur;
331             cur += s->picture.linesize[0];
332
333             if (s->restart_interval && !--s->restart_count) {
334                 align_get_bits(&s->gb);
335                 skip_bits(&s->gb, 16); /* skip RSTn */
336             }
337         }
338     } else if (ilv == 1) { /* line interleaving */
339         int j;
340         int Rc[3] = { 0, 0, 0 };
341         stride = (s->nb_components > 1) ? 3 : 1;
342         memset(cur, 0, s->picture.linesize[0]);
343         width = s->width * stride;
344         for (i = 0; i < s->height; i++) {
345             for (j = 0; j < stride; j++) {
346                 ls_decode_line(state, s, last + j, cur + j,
347                                Rc[j], width, stride, j, 8);
348                 Rc[j] = last[j];
349
350                 if (s->restart_interval && !--s->restart_count) {
351                     align_get_bits(&s->gb);
352                     skip_bits(&s->gb, 16); /* skip RSTn */
353                 }
354             }
355             last = cur;
356             cur += s->picture.linesize[0];
357         }
358     } else if (ilv == 2) { /* sample interleaving */
359         avpriv_report_missing_feature(s->avctx, "Sample interleaved images");
360         ret = AVERROR_PATCHWELCOME;
361         goto end;
362     }
363
364     if (s->xfrm && s->nb_components == 3) {
365         int x, w;
366
367         w = s->width * s->nb_components;
368
369         if (s->bits <= 8) {
370             uint8_t *src = s->picture.data[0];
371
372             for (i = 0; i < s->height; i++) {
373                 switch(s->xfrm) {
374                 case 1:
375                     for (x = off; x < w; x += 3) {
376                         src[x  ] += src[x+1] + 128;
377                         src[x+2] += src[x+1] + 128;
378                     }
379                     break;
380                 case 2:
381                     for (x = off; x < w; x += 3) {
382                         src[x  ] += src[x+1] + 128;
383                         src[x+2] += ((src[x  ] + src[x+1])>>1) + 128;
384                     }
385                     break;
386                 case 3:
387                     for (x = off; x < w; x += 3) {
388                         int g = src[x+0] - ((src[x+2]+src[x+1])>>2) + 64;
389                         src[x+0] = src[x+2] + g + 128;
390                         src[x+2] = src[x+1] + g + 128;
391                         src[x+1] = g;
392                     }
393                     break;
394                 case 4:
395                     for (x = off; x < w; x += 3) {
396                         int r    = src[x+0] - ((                       359 * (src[x+2]-128) + 490) >> 8);
397                         int g    = src[x+0] - (( 88 * (src[x+1]-128) - 183 * (src[x+2]-128) +  30) >> 8);
398                         int b    = src[x+0] + ((454 * (src[x+1]-128)                        + 574) >> 8);
399                         src[x+0] = av_clip_uint8(r);
400                         src[x+1] = av_clip_uint8(g);
401                         src[x+2] = av_clip_uint8(b);
402                     }
403                     break;
404                 }
405                 src += s->picture.linesize[0];
406             }
407         }else
408             avpriv_report_missing_feature(s->avctx, "16bit xfrm");
409     }
410
411     if (shift) { /* we need to do point transform or normalize samples */
412         int x, w;
413
414         w = s->width * s->nb_components;
415
416         if (s->bits <= 8) {
417             uint8_t *src = s->picture.data[0];
418
419             for (i = 0; i < s->height; i++) {
420                 for (x = off; x < w; x += stride)
421                     src[x] <<= shift;
422                 src += s->picture.linesize[0];
423             }
424         } else {
425             uint16_t *src = (uint16_t *)s->picture.data[0];
426
427             for (i = 0; i < s->height; i++) {
428                 for (x = 0; x < w; x++)
429                     src[x] <<= shift;
430                 src += s->picture.linesize[0] / 2;
431             }
432         }
433     }
434
435 end:
436     av_free(state);
437     av_free(zero);
438
439     return ret;
440 }
441
442 AVCodec ff_jpegls_decoder = {
443     .name           = "jpegls",
444     .long_name      = NULL_IF_CONFIG_SMALL("JPEG-LS"),
445     .type           = AVMEDIA_TYPE_VIDEO,
446     .id             = AV_CODEC_ID_JPEGLS,
447     .priv_data_size = sizeof(MJpegDecodeContext),
448     .init           = ff_mjpeg_decode_init,
449     .close          = ff_mjpeg_decode_end,
450     .decode         = ff_mjpeg_decode_frame,
451     .capabilities   = CODEC_CAP_DR1,
452 };