OSDN Git Service

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