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