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
36 #include "jpeglsdec.h"
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.
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
49 * Decode LSE block with initialization parameters
51 int ff_jpegls_decode_lse(MJpegDecodeContext *s)
54 int tid, wt, maxtab, i, j;
56 int len = get_bits(&s->gb, 16);
57 id = get_bits(&s->gb, 8);
62 return AVERROR_INVALIDDATA;
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);
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);
75 // ff_jpegls_reset_coding_parameters(s, 0);
81 tid= get_bits(&s->gb, 8);
82 wt = get_bits(&s->gb, 8);
85 return AVERROR_INVALIDDATA;
87 if (wt < 1 || wt > MAX_COMPONENTS) {
88 avpriv_request_sample(s->avctx, "wt %d", wt);
89 return AVERROR_PATCHWELCOME;
94 else if ((5 + wt*(s->maxval+1)) < 65535)
97 maxtab = 65530/wt - 1;
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);
103 avpriv_request_sample(s->avctx, ">8bit palette");
104 return AVERROR_PATCHWELCOME;
106 maxtab = FFMIN(maxtab, (len - 5) / wt + s->palette_index);
108 if (s->palette_index > maxtab)
109 return AVERROR_INVALIDDATA;
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];
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;
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;
126 for (j=0; j<wt; j++) {
127 pal[k] |= get_bits(&s->gb, 8) << (8*(wt-j-1));
130 s->palette_index = i;
134 avpriv_request_sample(s->avctx, "oversize image");
135 return AVERROR(ENOSYS);
137 av_log(s->avctx, AV_LOG_ERROR, "invalid id %d\n", id);
138 return AVERROR_INVALIDDATA;
140 ff_dlog(s->avctx, "ID=%i, T=%i,%i,%i\n", id, s->t1, s->t2, s->t3);
146 * Get context-dependent Golomb code, decode it and update context
148 static inline int ls_get_code_regular(GetBitContext *gb, JLSState *state, int Q)
152 for (k = 0; (state->N[Q] << k) < state->A[Q]; k++)
156 if (!show_bits_long(gb, 32))
159 ret = get_ur_golomb_jpegls(gb, k, state->limit, state->qbpp);
161 /* decode mapped error */
163 ret = -(ret + 1 >> 1);
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]))
171 ret = ff_jpegls_update_state_regular(state, Q, ret);
177 * Get Golomb code, decode it and update state for run termination
179 static inline int ls_get_code_runterm(GetBitContext *gb, JLSState *state,
180 int RItype, int limit_add)
182 int k, ret, temp, map;
183 int Q = 365 + RItype;
187 temp += state->N[Q] >> 1;
189 for (k = 0; (state->N[Q] << k) < temp; k++)
193 if (!show_bits_long(gb, 32))
196 ret = get_ur_golomb_jpegls(gb, k, state->limit - limit_add - 1,
199 /* decode mapped error */
201 if (!k && (RItype || ret) && (2 * state->B[Q] < state->N[Q]))
206 ret = map - (ret + 1 >> 1);
212 if(FFABS(ret) > 0xFFFF)
215 state->A[Q] += FFABS(ret) - RItype;
216 ret *= state->twonear;
217 ff_jpegls_downscale_state(state, Q);
223 * Decode one line of image
225 static inline void ls_decode_line(JLSState *state, MJpegDecodeContext *s,
226 void *last, void *dst, int last2, int w,
227 int stride, int comp, int bits)
236 /* compute gradients */
237 Ra = x ? R(dst, x - stride) : R(last, x);
239 Rc = x ? R(last, x - stride) : last2;
240 Rd = (x >= w - stride) ? R(last, x) : R(last, x + stride);
245 if ((FFABS(D0) <= state->near) &&
246 (FFABS(D1) <= state->near) &&
247 (FFABS(D2) <= state->near)) {
251 /* decode full runs while available */
252 while (get_bits1(&s->gb)) {
254 r = 1 << ff_log2_run[state->run_index[comp]];
255 if (x + r * stride > w)
256 r = (w - x) / stride;
257 for (i = 0; i < r; i++) {
261 /* if EOL reached, we stop decoding */
262 if (r != 1 << ff_log2_run[state->run_index[comp]])
264 if (state->run_index[comp] < 31)
265 state->run_index[comp]++;
269 /* decode aborted run */
270 r = ff_log2_run[state->run_index[comp]];
272 r = get_bits_long(&s->gb, r);
273 if (x + r * stride > w) {
274 r = (w - x) / stride;
276 for (i = 0; i < r; i++) {
282 av_log(NULL, AV_LOG_ERROR, "run overflow\n");
287 /* decode run termination value */
289 RItype = (FFABS(Ra - Rb) <= state->near) ? 1 : 0;
290 err = ls_get_code_runterm(&s->gb, state, RItype,
291 ff_log2_run[state->run_index[comp]]);
292 if (state->run_index[comp])
293 state->run_index[comp]--;
295 if (state->near && RItype) {
303 } else { /* regular mode */
306 context = ff_jpegls_quantize(state, D0) * 81 +
307 ff_jpegls_quantize(state, D1) * 9 +
308 ff_jpegls_quantize(state, D2);
309 pred = mid_pred(Ra, Ra + Rb - Rc, Rb);
319 pred = av_clip(pred - state->C[context], 0, state->maxval);
320 err = -ls_get_code_regular(&s->gb, state, context);
322 pred = av_clip(pred + state->C[context], 0, state->maxval);
323 err = ls_get_code_regular(&s->gb, state, context);
326 /* we have to do something more for near-lossless coding */
330 if (pred < -state->near)
331 pred += state->range * state->twonear;
332 else if (pred > state->maxval + state->near)
333 pred -= state->range * state->twonear;
334 pred = av_clip(pred, 0, state->maxval);
337 pred &= state->maxval;
343 int ff_jpegls_decode_picture(MJpegDecodeContext *s, int near,
344 int point_transform, int ilv)
347 uint8_t *zero, *last, *cur;
349 int off = 0, stride = 1, width, shift, ret = 0;
351 zero = av_mallocz(s->picture_ptr->linesize[0]);
353 return AVERROR(ENOMEM);
355 cur = s->picture_ptr->data[0];
357 state = av_mallocz(sizeof(JLSState));
360 return AVERROR(ENOMEM);
362 /* initialize JPEG-LS state from JPEG parameters */
364 state->bpp = (s->bits < 2) ? 2 : s->bits;
365 state->maxval = s->maxval;
369 state->reset = s->reset;
370 ff_jpegls_reset_coding_parameters(state, 0);
371 ff_jpegls_init_state(state);
374 shift = point_transform + (8 - s->bits);
376 shift = point_transform + (16 - s->bits);
379 ret = AVERROR_INVALIDDATA;
383 if (s->avctx->debug & FF_DEBUG_PICT_INFO) {
384 av_log(s->avctx, AV_LOG_DEBUG,
385 "JPEG-LS params: %ix%i NEAR=%i MV=%i T(%i,%i,%i) "
386 "RESET=%i, LIMIT=%i, qbpp=%i, RANGE=%i\n",
387 s->width, s->height, state->near, state->maxval,
388 state->T1, state->T2, state->T3,
389 state->reset, state->limit, state->qbpp, state->range);
390 av_log(s->avctx, AV_LOG_DEBUG, "JPEG params: ILV=%i Pt=%i BPP=%i, scan = %i\n",
391 ilv, point_transform, s->bits, s->cur_scan);
393 if (ilv == 0) { /* separate planes */
394 if (s->cur_scan > s->nb_components) {
395 ret = AVERROR_INVALIDDATA;
398 stride = (s->nb_components > 1) ? 3 : 1;
399 off = av_clip(s->cur_scan - 1, 0, stride - 1);
400 width = s->width * stride;
402 for (i = 0; i < s->height; i++) {
404 ls_decode_line(state, s, last, cur, t, width, stride, off, 8);
407 ls_decode_line(state, s, last, cur, t, width, stride, off, 16);
408 t = *((uint16_t *)last);
411 cur += s->picture_ptr->linesize[0];
413 if (s->restart_interval && !--s->restart_count) {
414 align_get_bits(&s->gb);
415 skip_bits(&s->gb, 16); /* skip RSTn */
418 } else if (ilv == 1) { /* line interleaving */
420 int Rc[3] = { 0, 0, 0 };
421 stride = (s->nb_components > 1) ? 3 : 1;
422 memset(cur, 0, s->picture_ptr->linesize[0]);
423 width = s->width * stride;
424 for (i = 0; i < s->height; i++) {
425 for (j = 0; j < stride; j++) {
426 ls_decode_line(state, s, last + j, cur + j,
427 Rc[j], width, stride, j, 8);
430 if (s->restart_interval && !--s->restart_count) {
431 align_get_bits(&s->gb);
432 skip_bits(&s->gb, 16); /* skip RSTn */
436 cur += s->picture_ptr->linesize[0];
438 } else if (ilv == 2) { /* sample interleaving */
439 avpriv_report_missing_feature(s->avctx, "Sample interleaved images");
440 ret = AVERROR_PATCHWELCOME;
444 if (s->xfrm && s->nb_components == 3) {
447 w = s->width * s->nb_components;
450 uint8_t *src = s->picture_ptr->data[0];
452 for (i = 0; i < s->height; i++) {
455 for (x = off; x < w; x += 3) {
456 src[x ] += src[x+1] + 128;
457 src[x+2] += src[x+1] + 128;
461 for (x = off; x < w; x += 3) {
462 src[x ] += src[x+1] + 128;
463 src[x+2] += ((src[x ] + src[x+1])>>1) + 128;
467 for (x = off; x < w; x += 3) {
468 int g = src[x+0] - ((src[x+2]+src[x+1])>>2) + 64;
469 src[x+0] = src[x+2] + g + 128;
470 src[x+2] = src[x+1] + g + 128;
475 for (x = off; x < w; x += 3) {
476 int r = src[x+0] - (( 359 * (src[x+2]-128) + 490) >> 8);
477 int g = src[x+0] - (( 88 * (src[x+1]-128) - 183 * (src[x+2]-128) + 30) >> 8);
478 int b = src[x+0] + ((454 * (src[x+1]-128) + 574) >> 8);
479 src[x+0] = av_clip_uint8(r);
480 src[x+1] = av_clip_uint8(g);
481 src[x+2] = av_clip_uint8(b);
485 src += s->picture_ptr->linesize[0];
488 avpriv_report_missing_feature(s->avctx, "16bit xfrm");
491 if (shift) { /* we need to do point transform or normalize samples */
494 w = s->width * s->nb_components;
497 uint8_t *src = s->picture_ptr->data[0];
499 for (i = 0; i < s->height; i++) {
500 for (x = off; x < w; x += stride)
502 src += s->picture_ptr->linesize[0];
505 uint16_t *src = (uint16_t *)s->picture_ptr->data[0];
507 for (i = 0; i < s->height; i++) {
508 for (x = 0; x < w; x++)
510 src += s->picture_ptr->linesize[0] / 2;
522 AVCodec ff_jpegls_decoder = {
524 .long_name = NULL_IF_CONFIG_SMALL("JPEG-LS"),
525 .type = AVMEDIA_TYPE_VIDEO,
526 .id = AV_CODEC_ID_JPEGLS,
527 .priv_data_size = sizeof(MJpegDecodeContext),
528 .init = ff_mjpeg_decode_init,
529 .close = ff_mjpeg_decode_end,
530 .decode = ff_mjpeg_decode_frame,
531 .capabilities = AV_CODEC_CAP_DR1,
532 .caps_internal = FF_CODEC_CAP_INIT_THREADSAFE,