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