OSDN Git Service

Add support for picture_ptr field in MJpegDecodeContext
[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     if(FFABS(ret) > 0xFFFF)
152         return -0x10000;
153     /* update state */
154     state->A[Q] += FFABS(ret) - RItype;
155     ret         *= state->twonear;
156     ff_jpegls_downscale_state(state, Q);
157
158     return ret;
159 }
160
161 /**
162  * Decode one line of image
163  */
164 static inline void ls_decode_line(JLSState *state, MJpegDecodeContext *s,
165                                   void *last, void *dst, int last2, int w,
166                                   int stride, int comp, int bits)
167 {
168     int i, x = 0;
169     int Ra, Rb, Rc, Rd;
170     int D0, D1, D2;
171
172     while (x < w) {
173         int err, pred;
174
175         /* compute gradients */
176         Ra = x ? R(dst, x - stride) : R(last, x);
177         Rb = R(last, x);
178         Rc = x ? R(last, x - stride) : last2;
179         Rd = (x >= w - stride) ? R(last, x) : R(last, x + stride);
180         D0 = Rd - Rb;
181         D1 = Rb - Rc;
182         D2 = Rc - Ra;
183         /* run mode */
184         if ((FFABS(D0) <= state->near) &&
185             (FFABS(D1) <= state->near) &&
186             (FFABS(D2) <= state->near)) {
187             int r;
188             int RItype;
189
190             /* decode full runs while available */
191             while (get_bits1(&s->gb)) {
192                 int r;
193                 r = 1 << ff_log2_run[state->run_index[comp]];
194                 if (x + r * stride > w)
195                     r = (w - x) / stride;
196                 for (i = 0; i < r; i++) {
197                     W(dst, x, Ra);
198                     x += stride;
199                 }
200                 /* if EOL reached, we stop decoding */
201                 if (r != 1 << ff_log2_run[state->run_index[comp]])
202                     return;
203                 if (state->run_index[comp] < 31)
204                     state->run_index[comp]++;
205                 if (x + stride > w)
206                     return;
207             }
208             /* decode aborted run */
209             r = ff_log2_run[state->run_index[comp]];
210             if (r)
211                 r = get_bits_long(&s->gb, r);
212             if (x + r * stride > w) {
213                 r = (w - x) / stride;
214             }
215             for (i = 0; i < r; i++) {
216                 W(dst, x, Ra);
217                 x += stride;
218             }
219
220             /* decode run termination value */
221             Rb     = R(last, x);
222             RItype = (FFABS(Ra - Rb) <= state->near) ? 1 : 0;
223             err    = ls_get_code_runterm(&s->gb, state, RItype,
224                                          ff_log2_run[state->run_index[comp]]);
225             if (state->run_index[comp])
226                 state->run_index[comp]--;
227
228             if (state->near && RItype) {
229                 pred = Ra + err;
230             } else {
231                 if (Rb < Ra)
232                     pred = Rb - err;
233                 else
234                     pred = Rb + err;
235             }
236         } else { /* regular mode */
237             int context, sign;
238
239             context = ff_jpegls_quantize(state, D0) * 81 +
240                       ff_jpegls_quantize(state, D1) *  9 +
241                       ff_jpegls_quantize(state, D2);
242             pred    = mid_pred(Ra, Ra + Rb - Rc, Rb);
243
244             if (context < 0) {
245                 context = -context;
246                 sign    = 1;
247             } else {
248                 sign = 0;
249             }
250
251             if (sign) {
252                 pred = av_clip(pred - state->C[context], 0, state->maxval);
253                 err  = -ls_get_code_regular(&s->gb, state, context);
254             } else {
255                 pred = av_clip(pred + state->C[context], 0, state->maxval);
256                 err  = ls_get_code_regular(&s->gb, state, context);
257             }
258
259             /* we have to do something more for near-lossless coding */
260             pred += err;
261         }
262         if (state->near) {
263             if (pred < -state->near)
264                 pred += state->range * state->twonear;
265             else if (pred > state->maxval + state->near)
266                 pred -= state->range * state->twonear;
267             pred = av_clip(pred, 0, state->maxval);
268         }
269
270         pred &= state->maxval;
271         W(dst, x, pred);
272         x += stride;
273     }
274 }
275
276 int ff_jpegls_decode_picture(MJpegDecodeContext *s, int near,
277                              int point_transform, int ilv)
278 {
279     int i, t = 0;
280     uint8_t *zero, *last, *cur;
281     JLSState *state;
282     int off = 0, stride = 1, width, shift, ret = 0;
283
284     zero = av_mallocz(s->picture_ptr->linesize[0]);
285     last = zero;
286     cur  = s->picture_ptr->data[0];
287
288     state = av_mallocz(sizeof(JLSState));
289     /* initialize JPEG-LS state from JPEG parameters */
290     state->near   = near;
291     state->bpp    = (s->bits < 2) ? 2 : s->bits;
292     state->maxval = s->maxval;
293     state->T1     = s->t1;
294     state->T2     = s->t2;
295     state->T3     = s->t3;
296     state->reset  = s->reset;
297     ff_jpegls_reset_coding_parameters(state, 0);
298     ff_jpegls_init_state(state);
299
300     if (s->bits <= 8)
301         shift = point_transform + (8 - s->bits);
302     else
303         shift = point_transform + (16 - s->bits);
304
305     if (s->avctx->debug & FF_DEBUG_PICT_INFO) {
306         av_log(s->avctx, AV_LOG_DEBUG,
307                "JPEG-LS params: %ix%i NEAR=%i MV=%i T(%i,%i,%i) "
308                "RESET=%i, LIMIT=%i, qbpp=%i, RANGE=%i\n",
309                 s->width, s->height, state->near, state->maxval,
310                 state->T1, state->T2, state->T3,
311                 state->reset, state->limit, state->qbpp, state->range);
312         av_log(s->avctx, AV_LOG_DEBUG, "JPEG params: ILV=%i Pt=%i BPP=%i, scan = %i\n",
313                 ilv, point_transform, s->bits, s->cur_scan);
314     }
315     if (ilv == 0) { /* separate planes */
316         if (s->cur_scan > s->nb_components) {
317             ret = AVERROR_INVALIDDATA;
318             goto end;
319         }
320         stride = (s->nb_components > 1) ? 3 : 1;
321         off    = av_clip(s->cur_scan - 1, 0, stride - 1);
322         width  = s->width * stride;
323         cur   += off;
324         for (i = 0; i < s->height; i++) {
325             if (s->bits <= 8) {
326                 ls_decode_line(state, s, last, cur, t, width, stride, off, 8);
327                 t = last[0];
328             } else {
329                 ls_decode_line(state, s, last, cur, t, width, stride, off, 16);
330                 t = *((uint16_t *)last);
331             }
332             last = cur;
333             cur += s->picture_ptr->linesize[0];
334
335             if (s->restart_interval && !--s->restart_count) {
336                 align_get_bits(&s->gb);
337                 skip_bits(&s->gb, 16); /* skip RSTn */
338             }
339         }
340     } else if (ilv == 1) { /* line interleaving */
341         int j;
342         int Rc[3] = { 0, 0, 0 };
343         stride = (s->nb_components > 1) ? 3 : 1;
344         memset(cur, 0, s->picture_ptr->linesize[0]);
345         width = s->width * stride;
346         for (i = 0; i < s->height; i++) {
347             for (j = 0; j < stride; j++) {
348                 ls_decode_line(state, s, last + j, cur + j,
349                                Rc[j], width, stride, j, 8);
350                 Rc[j] = last[j];
351
352                 if (s->restart_interval && !--s->restart_count) {
353                     align_get_bits(&s->gb);
354                     skip_bits(&s->gb, 16); /* skip RSTn */
355                 }
356             }
357             last = cur;
358             cur += s->picture_ptr->linesize[0];
359         }
360     } else if (ilv == 2) { /* sample interleaving */
361         avpriv_report_missing_feature(s->avctx, "Sample interleaved images");
362         ret = AVERROR_PATCHWELCOME;
363         goto end;
364     }
365
366     if (s->xfrm && s->nb_components == 3) {
367         int x, w;
368
369         w = s->width * s->nb_components;
370
371         if (s->bits <= 8) {
372             uint8_t *src = s->picture_ptr->data[0];
373
374             for (i = 0; i < s->height; i++) {
375                 switch(s->xfrm) {
376                 case 1:
377                     for (x = off; x < w; x += 3) {
378                         src[x  ] += src[x+1] + 128;
379                         src[x+2] += src[x+1] + 128;
380                     }
381                     break;
382                 case 2:
383                     for (x = off; x < w; x += 3) {
384                         src[x  ] += src[x+1] + 128;
385                         src[x+2] += ((src[x  ] + src[x+1])>>1) + 128;
386                     }
387                     break;
388                 case 3:
389                     for (x = off; x < w; x += 3) {
390                         int g = src[x+0] - ((src[x+2]+src[x+1])>>2) + 64;
391                         src[x+0] = src[x+2] + g + 128;
392                         src[x+2] = src[x+1] + g + 128;
393                         src[x+1] = g;
394                     }
395                     break;
396                 case 4:
397                     for (x = off; x < w; x += 3) {
398                         int r    = src[x+0] - ((                       359 * (src[x+2]-128) + 490) >> 8);
399                         int g    = src[x+0] - (( 88 * (src[x+1]-128) - 183 * (src[x+2]-128) +  30) >> 8);
400                         int b    = src[x+0] + ((454 * (src[x+1]-128)                        + 574) >> 8);
401                         src[x+0] = av_clip_uint8(r);
402                         src[x+1] = av_clip_uint8(g);
403                         src[x+2] = av_clip_uint8(b);
404                     }
405                     break;
406                 }
407                 src += s->picture_ptr->linesize[0];
408             }
409         }else
410             avpriv_report_missing_feature(s->avctx, "16bit xfrm");
411     }
412
413     if (shift) { /* we need to do point transform or normalize samples */
414         int x, w;
415
416         w = s->width * s->nb_components;
417
418         if (s->bits <= 8) {
419             uint8_t *src = s->picture_ptr->data[0];
420
421             for (i = 0; i < s->height; i++) {
422                 for (x = off; x < w; x += stride)
423                     src[x] <<= shift;
424                 src += s->picture_ptr->linesize[0];
425             }
426         } else {
427             uint16_t *src = (uint16_t *)s->picture_ptr->data[0];
428
429             for (i = 0; i < s->height; i++) {
430                 for (x = 0; x < w; x++)
431                     src[x] <<= shift;
432                 src += s->picture_ptr->linesize[0] / 2;
433             }
434         }
435     }
436
437 end:
438     av_free(state);
439     av_free(zero);
440
441     return ret;
442 }
443
444 AVCodec ff_jpegls_decoder = {
445     .name           = "jpegls",
446     .long_name      = NULL_IF_CONFIG_SMALL("JPEG-LS"),
447     .type           = AVMEDIA_TYPE_VIDEO,
448     .id             = AV_CODEC_ID_JPEGLS,
449     .priv_data_size = sizeof(MJpegDecodeContext),
450     .init           = ff_mjpeg_decode_init,
451     .close          = ff_mjpeg_decode_end,
452     .decode         = ff_mjpeg_decode_frame,
453     .capabilities   = CODEC_CAP_DR1,
454 };