]> git.sesse.net Git - ffmpeg/blob - libavcodec/jpeglsdec.c
4ab0cb5bd8205740ae00ffebd92417250e95b7b4
[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 "internal.h"
32 #include "mathops.h"
33 #include "mjpeg.h"
34 #include "mjpegdec.h"
35 #include "jpegls.h"
36 #include "jpeglsdec.h"
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  * Decode LSE block with initialization parameters
50  */
51 int ff_jpegls_decode_lse(MJpegDecodeContext *s)
52 {
53     int id;
54     int tid, wt, maxtab, i, j;
55
56     int len = get_bits(&s->gb, 16);
57     id = get_bits(&s->gb, 8);
58
59     switch (id) {
60     case 1:
61         if (len < 13)
62             return AVERROR_INVALIDDATA;
63
64         s->maxval = get_bits(&s->gb, 16);
65         s->t1     = get_bits(&s->gb, 16);
66         s->t2     = get_bits(&s->gb, 16);
67         s->t3     = get_bits(&s->gb, 16);
68         s->reset  = get_bits(&s->gb, 16);
69
70         if(s->avctx->debug & FF_DEBUG_PICT_INFO) {
71             av_log(s->avctx, AV_LOG_DEBUG, "Coding parameters maxval:%d T1:%d T2:%d T3:%d reset:%d\n",
72                    s->maxval, s->t1, s->t2, s->t3, s->reset);
73         }
74
75 //        ff_jpegls_reset_coding_parameters(s, 0);
76         //FIXME quant table?
77         break;
78     case 2:
79         s->palette_index = 0;
80     case 3:
81         tid= get_bits(&s->gb, 8);
82         wt = get_bits(&s->gb, 8);
83
84         if (len < 5)
85             return AVERROR_INVALIDDATA;
86
87         if (wt < 1 || wt > MAX_COMPONENTS) {
88             avpriv_request_sample(s->avctx, "wt %d", wt);
89             return AVERROR_PATCHWELCOME;
90         }
91
92         if (!s->maxval)
93             maxtab = 255;
94         else if ((5 + wt*(s->maxval+1)) < 65535)
95             maxtab = s->maxval;
96         else
97             maxtab = 65530/wt - 1;
98
99         if(s->avctx->debug & FF_DEBUG_PICT_INFO) {
100             av_log(s->avctx, AV_LOG_DEBUG, "LSE palette %d tid:%d wt:%d maxtab:%d\n", id, tid, wt, maxtab);
101         }
102         if (maxtab >= 256) {
103             avpriv_request_sample(s->avctx, ">8bit palette");
104             return AVERROR_PATCHWELCOME;
105         }
106         maxtab = FFMIN(maxtab, (len - 5) / wt + s->palette_index);
107
108         if (s->palette_index > maxtab)
109             return AVERROR_INVALIDDATA;
110
111         if (s->avctx->pix_fmt == AV_PIX_FMT_GRAY8 || s->avctx->pix_fmt == AV_PIX_FMT_PAL8) {
112             uint32_t *pal = s->palette;
113             int shift = 0;
114
115             if (s->avctx->bits_per_raw_sample > 0 && s->avctx->bits_per_raw_sample < 8) {
116                 maxtab = FFMIN(maxtab, (1<<s->avctx->bits_per_raw_sample)-1);
117                 shift = 8 - s->avctx->bits_per_raw_sample;
118             }
119
120             s->avctx->pix_fmt = AV_PIX_FMT_PAL8;
121             for (i=s->palette_index; i<=maxtab; i++) {
122                 uint8_t k = i << shift;
123                 pal[k] = 0;
124                 for (j=0; j<wt; j++) {
125                     pal[k] |= get_bits(&s->gb, 8) << (8*(wt-j-1));
126                 }
127             }
128             s->palette_index = i;
129         }
130         break;
131     case 4:
132         avpriv_request_sample(s->avctx, "oversize image");
133         return AVERROR(ENOSYS);
134     default:
135         av_log(s->avctx, AV_LOG_ERROR, "invalid id %d\n", id);
136         return AVERROR_INVALIDDATA;
137     }
138     ff_dlog(s->avctx, "ID=%i, T=%i,%i,%i\n", id, s->t1, s->t2, s->t3);
139
140     return 0;
141 }
142
143 /**
144  * Get context-dependent Golomb code, decode it and update context
145  */
146 static inline int ls_get_code_regular(GetBitContext *gb, JLSState *state, int Q)
147 {
148     int k, ret;
149
150     for (k = 0; ((unsigned)state->N[Q] << k) < state->A[Q]; k++)
151         ;
152
153 #ifdef JLS_BROKEN
154     if (!show_bits_long(gb, 32))
155         return -1;
156 #endif
157     ret = get_ur_golomb_jpegls(gb, k, state->limit, state->qbpp);
158
159     /* decode mapped error */
160     if (ret & 1)
161         ret = -(ret + 1 >> 1);
162     else
163         ret >>= 1;
164
165     /* for NEAR=0, k=0 and 2*B[Q] <= - N[Q] mapping is reversed */
166     if (!state->near && !k && (2 * state->B[Q] <= -state->N[Q]))
167         ret = -(ret + 1);
168
169     ret = ff_jpegls_update_state_regular(state, Q, ret);
170
171     return ret;
172 }
173
174 /**
175  * Get Golomb code, decode it and update state for run termination
176  */
177 static inline int ls_get_code_runterm(GetBitContext *gb, JLSState *state,
178                                       int RItype, int limit_add)
179 {
180     int k, ret, temp, map;
181     int Q = 365 + RItype;
182
183     temp = state->A[Q];
184     if (RItype)
185         temp += state->N[Q] >> 1;
186
187     for (k = 0; (state->N[Q] << k) < temp; k++)
188         ;
189
190 #ifdef JLS_BROKEN
191     if (!show_bits_long(gb, 32))
192         return -1;
193 #endif
194     ret = get_ur_golomb_jpegls(gb, k, state->limit - limit_add - 1,
195                                state->qbpp);
196
197     /* decode mapped error */
198     map = 0;
199     if (!k && (RItype || ret) && (2 * state->B[Q] < state->N[Q]))
200         map = 1;
201     ret += RItype + map;
202
203     if (ret & 1) {
204         ret = map - (ret + 1 >> 1);
205         state->B[Q]++;
206     } else {
207         ret = ret >> 1;
208     }
209
210     if(FFABS(ret) > 0xFFFF)
211         return -0x10000;
212     /* update state */
213     state->A[Q] += FFABS(ret) - RItype;
214     ret         *= state->twonear;
215     ff_jpegls_downscale_state(state, Q);
216
217     return ret;
218 }
219
220 /**
221  * Decode one line of image
222  */
223 static inline int ls_decode_line(JLSState *state, MJpegDecodeContext *s,
224                                   void *last, void *dst, int last2, int w,
225                                   int stride, int comp, int bits)
226 {
227     int i, x = 0;
228     int Ra, Rb, Rc, Rd;
229     int D0, D1, D2;
230
231     while (x < w) {
232         int err, pred;
233
234         if (get_bits_left(&s->gb) <= 0)
235             return AVERROR_INVALIDDATA;
236
237         /* compute gradients */
238         Ra = x ? R(dst, x - stride) : R(last, x);
239         Rb = R(last, x);
240         Rc = x ? R(last, x - stride) : last2;
241         Rd = (x >= w - stride) ? R(last, x) : R(last, x + stride);
242         D0 = Rd - Rb;
243         D1 = Rb - Rc;
244         D2 = Rc - Ra;
245         /* run mode */
246         if ((FFABS(D0) <= state->near) &&
247             (FFABS(D1) <= state->near) &&
248             (FFABS(D2) <= state->near)) {
249             int r;
250             int RItype;
251
252             /* decode full runs while available */
253             while (get_bits1(&s->gb)) {
254                 int r;
255                 r = 1 << ff_log2_run[state->run_index[comp]];
256                 if (x + r * stride > w)
257                     r = (w - x) / stride;
258                 for (i = 0; i < r; i++) {
259                     W(dst, x, Ra);
260                     x += stride;
261                 }
262                 /* if EOL reached, we stop decoding */
263                 if (r != 1 << ff_log2_run[state->run_index[comp]])
264                     return 0;
265                 if (state->run_index[comp] < 31)
266                     state->run_index[comp]++;
267                 if (x + stride > w)
268                     return 0;
269             }
270             /* decode aborted run */
271             r = ff_log2_run[state->run_index[comp]];
272             if (r)
273                 r = get_bits_long(&s->gb, r);
274             if (x + r * stride > w) {
275                 r = (w - x) / stride;
276             }
277             for (i = 0; i < r; i++) {
278                 W(dst, x, Ra);
279                 x += stride;
280             }
281
282             if (x >= w) {
283                 av_log(NULL, AV_LOG_ERROR, "run overflow\n");
284                 av_assert0(x <= w);
285                 return AVERROR_INVALIDDATA;
286             }
287
288             /* decode run termination value */
289             Rb     = R(last, x);
290             RItype = (FFABS(Ra - Rb) <= state->near) ? 1 : 0;
291             err    = ls_get_code_runterm(&s->gb, state, RItype,
292                                          ff_log2_run[state->run_index[comp]]);
293             if (state->run_index[comp])
294                 state->run_index[comp]--;
295
296             if (state->near && RItype) {
297                 pred = Ra + err;
298             } else {
299                 if (Rb < Ra)
300                     pred = Rb - err;
301                 else
302                     pred = Rb + err;
303             }
304         } else { /* regular mode */
305             int context, sign;
306
307             context = ff_jpegls_quantize(state, D0) * 81 +
308                       ff_jpegls_quantize(state, D1) *  9 +
309                       ff_jpegls_quantize(state, D2);
310             pred    = mid_pred(Ra, Ra + Rb - Rc, Rb);
311
312             if (context < 0) {
313                 context = -context;
314                 sign    = 1;
315             } else {
316                 sign = 0;
317             }
318
319             if (sign) {
320                 pred = av_clip(pred - state->C[context], 0, state->maxval);
321                 err  = -ls_get_code_regular(&s->gb, state, context);
322             } else {
323                 pred = av_clip(pred + state->C[context], 0, state->maxval);
324                 err  = ls_get_code_regular(&s->gb, state, context);
325             }
326
327             /* we have to do something more for near-lossless coding */
328             pred += err;
329         }
330         if (state->near) {
331             if (pred < -state->near)
332                 pred += state->range * state->twonear;
333             else if (pred > state->maxval + state->near)
334                 pred -= state->range * state->twonear;
335             pred = av_clip(pred, 0, state->maxval);
336         }
337
338         pred &= state->maxval;
339         W(dst, x, pred);
340         x += stride;
341     }
342
343     return 0;
344 }
345
346 int ff_jpegls_decode_picture(MJpegDecodeContext *s, int near,
347                              int point_transform, int ilv)
348 {
349     int i, t = 0;
350     uint8_t *zero, *last, *cur;
351     JLSState *state = s->jls_state;
352     int off = 0, stride = 1, width, shift, ret = 0;
353     int decoded_height = 0;
354
355     if (!state) {
356         state = av_malloc(sizeof(*state));
357         if (!state)
358             return AVERROR(ENOMEM);
359         s->jls_state = state;
360     }
361     zero = av_mallocz(s->picture_ptr->linesize[0]);
362     if (!zero)
363         return AVERROR(ENOMEM);
364     last = zero;
365     cur  = s->picture_ptr->data[0];
366
367     /* initialize JPEG-LS state from JPEG parameters */
368     memset(state, 0, sizeof(*state));
369     state->near   = near;
370     state->bpp    = (s->bits < 2) ? 2 : s->bits;
371     state->maxval = s->maxval;
372     state->T1     = s->t1;
373     state->T2     = s->t2;
374     state->T3     = s->t3;
375     state->reset  = s->reset;
376     ff_jpegls_reset_coding_parameters(state, 0);
377     ff_jpegls_init_state(state);
378
379     if (s->bits <= 8)
380         shift = point_transform + (8 - s->bits);
381     else
382         shift = point_transform + (16 - s->bits);
383
384     if (shift >= 16) {
385         ret = AVERROR_INVALIDDATA;
386         goto end;
387     }
388
389     if (s->avctx->debug & FF_DEBUG_PICT_INFO) {
390         av_log(s->avctx, AV_LOG_DEBUG,
391                "JPEG-LS params: %ix%i NEAR=%i MV=%i T(%i,%i,%i) "
392                "RESET=%i, LIMIT=%i, qbpp=%i, RANGE=%i\n",
393                 s->width, s->height, state->near, state->maxval,
394                 state->T1, state->T2, state->T3,
395                 state->reset, state->limit, state->qbpp, state->range);
396         av_log(s->avctx, AV_LOG_DEBUG, "JPEG params: ILV=%i Pt=%i BPP=%i, scan = %i\n",
397                 ilv, point_transform, s->bits, s->cur_scan);
398     }
399     if (get_bits_left(&s->gb) < s->height) {
400         ret = AVERROR_INVALIDDATA;
401         goto end;
402     }
403     if (ilv == 0) { /* separate planes */
404         if (s->cur_scan > s->nb_components) {
405             ret = AVERROR_INVALIDDATA;
406             goto end;
407         }
408         stride = (s->nb_components > 1) ? 3 : 1;
409         off    = av_clip(s->cur_scan - 1, 0, stride - 1);
410         width  = s->width * stride;
411         cur   += off;
412         for (i = 0; i < s->height; i++) {
413             int ret;
414             if (s->bits <= 8) {
415                 ret = ls_decode_line(state, s, last, cur, t, width, stride, off, 8);
416                 t = last[0];
417             } else {
418                 ret = ls_decode_line(state, s, last, cur, t, width, stride, off, 16);
419                 t = *((uint16_t *)last);
420             }
421             if (ret < 0)
422                 break;
423             last = cur;
424             cur += s->picture_ptr->linesize[0];
425
426             if (s->restart_interval && !--s->restart_count) {
427                 align_get_bits(&s->gb);
428                 skip_bits(&s->gb, 16); /* skip RSTn */
429             }
430         }
431         decoded_height = i;
432     } else if (ilv == 1) { /* line interleaving */
433         int j;
434         int Rc[3] = { 0, 0, 0 };
435         stride = (s->nb_components > 1) ? 3 : 1;
436         memset(cur, 0, s->picture_ptr->linesize[0]);
437         width = s->width * stride;
438         for (i = 0; i < s->height; i++) {
439             int ret;
440             for (j = 0; j < stride; j++) {
441                 ret = ls_decode_line(state, s, last + j, cur + j,
442                                Rc[j], width, stride, j, 8);
443                 if (ret < 0)
444                     break;
445                 Rc[j] = last[j];
446
447                 if (s->restart_interval && !--s->restart_count) {
448                     align_get_bits(&s->gb);
449                     skip_bits(&s->gb, 16); /* skip RSTn */
450                 }
451             }
452             if (ret < 0)
453                 break;
454             last = cur;
455             cur += s->picture_ptr->linesize[0];
456         }
457         decoded_height = i;
458     } else if (ilv == 2) { /* sample interleaving */
459         avpriv_report_missing_feature(s->avctx, "Sample interleaved images");
460         ret = AVERROR_PATCHWELCOME;
461         goto end;
462     } else { /* unknown interleaving */
463         avpriv_report_missing_feature(s->avctx, "Unknown interleaved images");
464         ret = AVERROR_PATCHWELCOME;
465         goto end;
466     }
467
468     if (s->xfrm && s->nb_components == 3) {
469         int x, w;
470
471         w = s->width * s->nb_components;
472
473         if (s->bits <= 8) {
474             uint8_t *src = s->picture_ptr->data[0];
475
476             for (i = 0; i < s->height; i++) {
477                 switch(s->xfrm) {
478                 case 1:
479                     for (x = off; x < w; x += 3) {
480                         src[x  ] += src[x+1] + 128;
481                         src[x+2] += src[x+1] + 128;
482                     }
483                     break;
484                 case 2:
485                     for (x = off; x < w; x += 3) {
486                         src[x  ] += src[x+1] + 128;
487                         src[x+2] += ((src[x  ] + src[x+1])>>1) + 128;
488                     }
489                     break;
490                 case 3:
491                     for (x = off; x < w; x += 3) {
492                         int g = src[x+0] - ((src[x+2]+src[x+1])>>2) + 64;
493                         src[x+0] = src[x+2] + g + 128;
494                         src[x+2] = src[x+1] + g + 128;
495                         src[x+1] = g;
496                     }
497                     break;
498                 case 4:
499                     for (x = off; x < w; x += 3) {
500                         int r    = src[x+0] - ((                       359 * (src[x+2]-128) + 490) >> 8);
501                         int g    = src[x+0] - (( 88 * (src[x+1]-128) - 183 * (src[x+2]-128) +  30) >> 8);
502                         int b    = src[x+0] + ((454 * (src[x+1]-128)                        + 574) >> 8);
503                         src[x+0] = av_clip_uint8(r);
504                         src[x+1] = av_clip_uint8(g);
505                         src[x+2] = av_clip_uint8(b);
506                     }
507                     break;
508                 }
509                 src += s->picture_ptr->linesize[0];
510             }
511         }else
512             avpriv_report_missing_feature(s->avctx, "16bit xfrm");
513     }
514
515     if (shift) { /* we need to do point transform or normalize samples */
516         int x, w;
517
518         w = s->width * s->nb_components;
519
520         if (s->bits <= 8) {
521             uint8_t *src = s->picture_ptr->data[0];
522
523             for (i = 0; i < decoded_height; i++) {
524                 for (x = off; x < w; x += stride)
525                     src[x] <<= shift;
526                 src += s->picture_ptr->linesize[0];
527             }
528         } else {
529             uint16_t *src = (uint16_t *)s->picture_ptr->data[0];
530
531             for (i = 0; i < decoded_height; i++) {
532                 for (x = 0; x < w; x++)
533                     src[x] <<= shift;
534                 src += s->picture_ptr->linesize[0] / 2;
535             }
536         }
537     }
538
539 end:
540     av_free(zero);
541
542     return ret;
543 }
544
545 AVCodec ff_jpegls_decoder = {
546     .name           = "jpegls",
547     .long_name      = NULL_IF_CONFIG_SMALL("JPEG-LS"),
548     .type           = AVMEDIA_TYPE_VIDEO,
549     .id             = AV_CODEC_ID_JPEGLS,
550     .priv_data_size = sizeof(MJpegDecodeContext),
551     .init           = ff_mjpeg_decode_init,
552     .close          = ff_mjpeg_decode_end,
553     .receive_frame  = ff_mjpeg_receive_frame,
554     .capabilities   = AV_CODEC_CAP_DR1,
555     .caps_internal  = FF_CODEC_CAP_INIT_THREADSAFE | FF_CODEC_CAP_INIT_CLEANUP |
556                       FF_CODEC_CAP_SETS_PKT_DTS,
557 };