3 * Copyright (c) 2003 Michael Niedermayer
4 * Copyright (c) 2006 Konstantin Shishkov
6 * This file is part of FFmpeg.
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.
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.
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
35 #include "jpeglsdec.h"
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.
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
48 * Decode LSE block with initialization parameters
50 int ff_jpegls_decode_lse(MJpegDecodeContext *s)
53 int tid, wt, maxtab, i, j;
55 int len = get_bits(&s->gb, 16);
56 id = get_bits(&s->gb, 8);
61 return AVERROR_INVALIDDATA;
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);
69 // ff_jpegls_reset_coding_parameters(s, 0);
75 tid= get_bits(&s->gb, 8);
76 wt = get_bits(&s->gb, 8);
79 return AVERROR_INVALIDDATA;
81 if (wt < 1 || wt > MAX_COMPONENTS) {
82 avpriv_request_sample(s->avctx, "wt %d", wt);
83 return AVERROR_PATCHWELCOME;
88 else if ((5 + wt*(s->maxval+1)) < 65535)
91 maxtab = 65530/wt - 1;
93 if(s->avctx->debug & FF_DEBUG_PICT_INFO) {
94 av_log(s->avctx, AV_LOG_DEBUG, "LSE palette %d tid:%d wt:%d maxtab:%d\n", id, tid, wt, maxtab);
97 avpriv_request_sample(s->avctx, ">8bit palette");
98 return AVERROR_PATCHWELCOME;
100 maxtab = FFMIN(maxtab, (len - 5) / wt + s->palette_index);
102 if (s->palette_index > maxtab)
103 return AVERROR_INVALIDDATA;
105 if ((s->avctx->pix_fmt == AV_PIX_FMT_GRAY8 || s->avctx->pix_fmt == AV_PIX_FMT_PAL8) &&
106 (s->picture_ptr->format == AV_PIX_FMT_GRAY8 || s->picture_ptr->format == AV_PIX_FMT_PAL8)) {
107 uint32_t *pal = s->picture_ptr->data[1];
108 s->picture_ptr->format =
109 s->avctx->pix_fmt = AV_PIX_FMT_PAL8;
110 for (i=s->palette_index; i<=maxtab; i++) {
112 for (j=0; j<wt; j++) {
113 pal[i] |= get_bits(&s->gb, 8) << (8*(wt-j-1));
116 s->palette_index = i;
120 avpriv_request_sample(s->avctx, "oversize image");
121 return AVERROR(ENOSYS);
123 av_log(s->avctx, AV_LOG_ERROR, "invalid id %d\n", id);
124 return AVERROR_INVALIDDATA;
126 av_dlog(s->avctx, "ID=%i, T=%i,%i,%i\n", id, s->t1, s->t2, s->t3);
132 * Get context-dependent Golomb code, decode it and update context
134 static inline int ls_get_code_regular(GetBitContext *gb, JLSState *state, int Q)
138 for (k = 0; (state->N[Q] << k) < state->A[Q]; k++)
142 if (!show_bits_long(gb, 32))
145 ret = get_ur_golomb_jpegls(gb, k, state->limit, state->qbpp);
147 /* decode mapped error */
149 ret = -(ret + 1 >> 1);
153 /* for NEAR=0, k=0 and 2*B[Q] <= - N[Q] mapping is reversed */
154 if (!state->near && !k && (2 * state->B[Q] <= -state->N[Q]))
157 ret = ff_jpegls_update_state_regular(state, Q, ret);
163 * Get Golomb code, decode it and update state for run termination
165 static inline int ls_get_code_runterm(GetBitContext *gb, JLSState *state,
166 int RItype, int limit_add)
168 int k, ret, temp, map;
169 int Q = 365 + RItype;
173 temp += state->N[Q] >> 1;
175 for (k = 0; (state->N[Q] << k) < temp; k++)
179 if (!show_bits_long(gb, 32))
182 ret = get_ur_golomb_jpegls(gb, k, state->limit - limit_add - 1,
185 /* decode mapped error */
187 if (!k && (RItype || ret) && (2 * state->B[Q] < state->N[Q]))
192 ret = map - (ret + 1 >> 1);
198 if(FFABS(ret) > 0xFFFF)
201 state->A[Q] += FFABS(ret) - RItype;
202 ret *= state->twonear;
203 ff_jpegls_downscale_state(state, Q);
209 * Decode one line of image
211 static inline void ls_decode_line(JLSState *state, MJpegDecodeContext *s,
212 void *last, void *dst, int last2, int w,
213 int stride, int comp, int bits)
222 /* compute gradients */
223 Ra = x ? R(dst, x - stride) : R(last, x);
225 Rc = x ? R(last, x - stride) : last2;
226 Rd = (x >= w - stride) ? R(last, x) : R(last, x + stride);
231 if ((FFABS(D0) <= state->near) &&
232 (FFABS(D1) <= state->near) &&
233 (FFABS(D2) <= state->near)) {
237 /* decode full runs while available */
238 while (get_bits1(&s->gb)) {
240 r = 1 << ff_log2_run[state->run_index[comp]];
241 if (x + r * stride > w)
242 r = (w - x) / stride;
243 for (i = 0; i < r; i++) {
247 /* if EOL reached, we stop decoding */
248 if (r != 1 << ff_log2_run[state->run_index[comp]])
250 if (state->run_index[comp] < 31)
251 state->run_index[comp]++;
255 /* decode aborted run */
256 r = ff_log2_run[state->run_index[comp]];
258 r = get_bits_long(&s->gb, r);
259 if (x + r * stride > w) {
260 r = (w - x) / stride;
262 for (i = 0; i < r; i++) {
267 /* decode run termination value */
269 RItype = (FFABS(Ra - Rb) <= state->near) ? 1 : 0;
270 err = ls_get_code_runterm(&s->gb, state, RItype,
271 ff_log2_run[state->run_index[comp]]);
272 if (state->run_index[comp])
273 state->run_index[comp]--;
275 if (state->near && RItype) {
283 } else { /* regular mode */
286 context = ff_jpegls_quantize(state, D0) * 81 +
287 ff_jpegls_quantize(state, D1) * 9 +
288 ff_jpegls_quantize(state, D2);
289 pred = mid_pred(Ra, Ra + Rb - Rc, Rb);
299 pred = av_clip(pred - state->C[context], 0, state->maxval);
300 err = -ls_get_code_regular(&s->gb, state, context);
302 pred = av_clip(pred + state->C[context], 0, state->maxval);
303 err = ls_get_code_regular(&s->gb, state, context);
306 /* we have to do something more for near-lossless coding */
310 if (pred < -state->near)
311 pred += state->range * state->twonear;
312 else if (pred > state->maxval + state->near)
313 pred -= state->range * state->twonear;
314 pred = av_clip(pred, 0, state->maxval);
317 pred &= state->maxval;
323 int ff_jpegls_decode_picture(MJpegDecodeContext *s, int near,
324 int point_transform, int ilv)
327 uint8_t *zero, *last, *cur;
329 int off = 0, stride = 1, width, shift, ret = 0;
331 zero = av_mallocz(s->picture_ptr->linesize[0]);
333 cur = s->picture_ptr->data[0];
335 state = av_mallocz(sizeof(JLSState));
336 /* initialize JPEG-LS state from JPEG parameters */
338 state->bpp = (s->bits < 2) ? 2 : s->bits;
339 state->maxval = s->maxval;
343 state->reset = s->reset;
344 ff_jpegls_reset_coding_parameters(state, 0);
345 ff_jpegls_init_state(state);
348 shift = point_transform + (8 - s->bits);
350 shift = point_transform + (16 - s->bits);
352 if (s->avctx->debug & FF_DEBUG_PICT_INFO) {
353 av_log(s->avctx, AV_LOG_DEBUG,
354 "JPEG-LS params: %ix%i NEAR=%i MV=%i T(%i,%i,%i) "
355 "RESET=%i, LIMIT=%i, qbpp=%i, RANGE=%i\n",
356 s->width, s->height, state->near, state->maxval,
357 state->T1, state->T2, state->T3,
358 state->reset, state->limit, state->qbpp, state->range);
359 av_log(s->avctx, AV_LOG_DEBUG, "JPEG params: ILV=%i Pt=%i BPP=%i, scan = %i\n",
360 ilv, point_transform, s->bits, s->cur_scan);
362 if (ilv == 0) { /* separate planes */
363 if (s->cur_scan > s->nb_components) {
364 ret = AVERROR_INVALIDDATA;
367 stride = (s->nb_components > 1) ? 3 : 1;
368 off = av_clip(s->cur_scan - 1, 0, stride - 1);
369 width = s->width * stride;
371 for (i = 0; i < s->height; i++) {
373 ls_decode_line(state, s, last, cur, t, width, stride, off, 8);
376 ls_decode_line(state, s, last, cur, t, width, stride, off, 16);
377 t = *((uint16_t *)last);
380 cur += s->picture_ptr->linesize[0];
382 if (s->restart_interval && !--s->restart_count) {
383 align_get_bits(&s->gb);
384 skip_bits(&s->gb, 16); /* skip RSTn */
387 } else if (ilv == 1) { /* line interleaving */
389 int Rc[3] = { 0, 0, 0 };
390 stride = (s->nb_components > 1) ? 3 : 1;
391 memset(cur, 0, s->picture_ptr->linesize[0]);
392 width = s->width * stride;
393 for (i = 0; i < s->height; i++) {
394 for (j = 0; j < stride; j++) {
395 ls_decode_line(state, s, last + j, cur + j,
396 Rc[j], width, stride, j, 8);
399 if (s->restart_interval && !--s->restart_count) {
400 align_get_bits(&s->gb);
401 skip_bits(&s->gb, 16); /* skip RSTn */
405 cur += s->picture_ptr->linesize[0];
407 } else if (ilv == 2) { /* sample interleaving */
408 avpriv_report_missing_feature(s->avctx, "Sample interleaved images");
409 ret = AVERROR_PATCHWELCOME;
413 if (s->xfrm && s->nb_components == 3) {
416 w = s->width * s->nb_components;
419 uint8_t *src = s->picture_ptr->data[0];
421 for (i = 0; i < s->height; i++) {
424 for (x = off; x < w; x += 3) {
425 src[x ] += src[x+1] + 128;
426 src[x+2] += src[x+1] + 128;
430 for (x = off; x < w; x += 3) {
431 src[x ] += src[x+1] + 128;
432 src[x+2] += ((src[x ] + src[x+1])>>1) + 128;
436 for (x = off; x < w; x += 3) {
437 int g = src[x+0] - ((src[x+2]+src[x+1])>>2) + 64;
438 src[x+0] = src[x+2] + g + 128;
439 src[x+2] = src[x+1] + g + 128;
444 for (x = off; x < w; x += 3) {
445 int r = src[x+0] - (( 359 * (src[x+2]-128) + 490) >> 8);
446 int g = src[x+0] - (( 88 * (src[x+1]-128) - 183 * (src[x+2]-128) + 30) >> 8);
447 int b = src[x+0] + ((454 * (src[x+1]-128) + 574) >> 8);
448 src[x+0] = av_clip_uint8(r);
449 src[x+1] = av_clip_uint8(g);
450 src[x+2] = av_clip_uint8(b);
454 src += s->picture_ptr->linesize[0];
457 avpriv_report_missing_feature(s->avctx, "16bit xfrm");
460 if (shift) { /* we need to do point transform or normalize samples */
463 w = s->width * s->nb_components;
466 uint8_t *src = s->picture_ptr->data[0];
468 for (i = 0; i < s->height; i++) {
469 for (x = off; x < w; x += stride)
471 src += s->picture_ptr->linesize[0];
474 uint16_t *src = (uint16_t *)s->picture_ptr->data[0];
476 for (i = 0; i < s->height; i++) {
477 for (x = 0; x < w; x++)
479 src += s->picture_ptr->linesize[0] / 2;
491 AVCodec ff_jpegls_decoder = {
493 .long_name = NULL_IF_CONFIG_SMALL("JPEG-LS"),
494 .type = AVMEDIA_TYPE_VIDEO,
495 .id = AV_CODEC_ID_JPEGLS,
496 .priv_data_size = sizeof(MJpegDecodeContext),
497 .init = ff_mjpeg_decode_init,
498 .close = ff_mjpeg_decode_end,
499 .decode = ff_mjpeg_decode_frame,
500 .capabilities = CODEC_CAP_DR1,