2 * JPEG-LS encoder and decoder
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
27 * JPEG-LS encoder and decoder.
30 typedef struct JpeglsContext{
31 AVCodecContext *avctx;
35 typedef struct JLSState{
37 int A[367], B[367], C[365], N[367];
38 int limit, reset, bpp, qbpp, maxval, range;
43 static const uint8_t log2_run[32]={
44 0, 0, 0, 0, 1, 1, 1, 1,
45 2, 2, 2, 2, 3, 3, 3, 3,
46 4, 4, 5, 5, 6, 6, 7, 7,
47 8, 9,10,11,12,13,14,15
51 * Uncomment this to significantly speed up decoding of broken JPEG-LS
52 * (or test broken JPEG-LS decoder) and slow down ordinary decoding a bit.
54 * There is no Golomb code with length >= 32 bits possible, so check and
55 * avoid situation of 32 zeros, FFmpeg Golomb decoder is painfully slow
60 /********** Functions for both encoder and decoder **********/
63 * Calculate initial JPEG-LS parameters
65 static void ls_init_state(JLSState *state){
68 state->twonear = state->near * 2 + 1;
69 state->range = ((state->maxval + state->twonear - 1) / state->twonear) + 1;
71 // QBPP = ceil(log2(RANGE))
72 for(state->qbpp = 0; (1 << state->qbpp) < state->range; state->qbpp++);
75 state->limit = 16 + 2 * state->bpp - state->qbpp;
77 state->limit = (4 * state->bpp) - state->qbpp;
79 for(i = 0; i < 367; i++) {
80 state->A[i] = (state->range + 32) >> 6;
89 * Calculate quantized gradient value, used for context determination
91 static inline int quantize(JLSState *s, int v){ //FIXME optimize
94 if(v <= -s->T3) return -4;
95 if(v <= -s->T2) return -3;
96 if(v <= -s->T1) return -2;
97 if(v < -s->near) return -1;
100 if(v <= s->near) return 0;
101 if(v < s->T1) return 1;
102 if(v < s->T2) return 2;
103 if(v < s->T3) return 3;
109 * Custom value clipping function used in T1, T2, T3 calculation
111 static inline int iso_clip(int v, int vmin, int vmax){
112 if(v > vmax || v < vmin) return vmin;
117 * Calculate JPEG-LS codec values
119 static void reset_ls_coding_parameters(JLSState *s, int reset_all){
120 const int basic_t1= 3;
121 const int basic_t2= 7;
122 const int basic_t3= 21;
125 if(s->maxval==0 || reset_all) s->maxval= (1 << s->bpp) - 1;
128 factor= (FFMIN(s->maxval, 4095) + 128)>>8;
130 if(s->T1==0 || reset_all)
131 s->T1= iso_clip(factor*(basic_t1-2) + 2 + 3*s->near, s->near+1, s->maxval);
132 if(s->T2==0 || reset_all)
133 s->T2= iso_clip(factor*(basic_t2-3) + 3 + 5*s->near, s->T1, s->maxval);
134 if(s->T3==0 || reset_all)
135 s->T3= iso_clip(factor*(basic_t3-4) + 4 + 7*s->near, s->T2, s->maxval);
137 factor= 256 / (s->maxval + 1);
139 if(s->T1==0 || reset_all)
140 s->T1= iso_clip(FFMAX(2, basic_t1/factor + 3*s->near), s->near+1, s->maxval);
141 if(s->T2==0 || reset_all)
142 s->T2= iso_clip(FFMAX(3, basic_t2/factor + 5*s->near), s->T1, s->maxval);
143 if(s->T3==0 || reset_all)
144 s->T3= iso_clip(FFMAX(4, basic_t3/factor + 6*s->near), s->T2, s->maxval);
147 if(s->reset==0 || reset_all) s->reset= 64;
148 // av_log(NULL, AV_LOG_DEBUG, "[JPEG-LS RESET] T=%i,%i,%i\n", s->T1, s->T2, s->T3);
152 /********** Decoder-specific functions **********/
155 * Decode LSE block with initialization parameters
157 static int decode_lse(MJpegDecodeContext *s)
161 /* XXX: verify len field validity */
162 len = get_bits(&s->gb, 16);
163 id = get_bits(&s->gb, 8);
167 s->maxval= get_bits(&s->gb, 16);
168 s->t1= get_bits(&s->gb, 16);
169 s->t2= get_bits(&s->gb, 16);
170 s->t3= get_bits(&s->gb, 16);
171 s->reset= get_bits(&s->gb, 16);
173 // reset_ls_coding_parameters(s, 0);
178 av_log(s->avctx, AV_LOG_ERROR, "palette not supported\n");
181 av_log(s->avctx, AV_LOG_ERROR, "oversize image not supported\n");
184 av_log(s->avctx, AV_LOG_ERROR, "invalid id %d\n", id);
187 // av_log(s->avctx, AV_LOG_DEBUG, "ID=%i, T=%i,%i,%i\n", id, s->t1, s->t2, s->t3);
194 * Get context-dependent Golomb code, decode it and update context
196 static inline int ls_get_code_regular(GetBitContext *gb, JLSState *state, int Q){
199 for(k = 0; (state->N[Q] << k) < state->A[Q]; k++);
202 if(!show_bits_long(gb, 32))return -1;
204 ret = get_ur_golomb_jpegls(gb, k, state->limit, state->qbpp);
206 /* decode mapped error */
208 ret = -((ret + 1) >> 1);
212 /* for NEAR=0, k=0 and 2*B[Q] <= - N[Q] mapping is reversed */
213 if(!state->near && !k && (2 * state->B[Q] <= -state->N[Q]))
216 state->A[Q] += FFABS(ret);
217 ret *= state->twonear;
220 if(state->N[Q] == state->reset) {
227 if(state->B[Q] <= -state->N[Q]) {
228 state->B[Q] += state->N[Q];
229 if(state->C[Q] > -128)
231 if(state->B[Q] <= -state->N[Q])
232 state->B[Q] = -state->N[Q] + 1;
233 }else if(state->B[Q] > 0){
234 state->B[Q] -= state->N[Q];
235 if(state->C[Q] < 127)
245 * Get Golomb code, decode it and update state for run termination
247 static inline int ls_get_code_runterm(GetBitContext *gb, JLSState *state, int RItype, int limit_add){
248 int k, ret, temp, map;
249 int Q = 365 + RItype;
254 temp = state->A[Q] + (state->N[Q] >> 1);
256 for(k = 0; (state->N[Q] << k) < temp; k++);
259 if(!show_bits_long(gb, 32))return -1;
261 ret = get_ur_golomb_jpegls(gb, k, state->limit - limit_add - 1, state->qbpp);
263 /* decode mapped error */
265 if(!k && (RItype || ret) && (2 * state->B[Q] < state->N[Q]))
270 ret = map - ((ret + 1) >> 1);
277 state->A[Q] += FFABS(ret) - RItype;
278 ret *= state->twonear;
279 if(state->N[Q] == state->reset){
290 * Decode one line of image
292 static inline void ls_decode_line(JLSState *state, MJpegDecodeContext *s, uint8_t *last, uint8_t *dst, int last2, int w, int stride, int comp){
300 /* compute gradients */
301 Ra = x ? dst[x - stride] : last[x];
303 Rc = x ? last[x - stride] : last2;
304 Rd = (x >= w - stride) ? last[x] : last[x + stride];
309 if((FFABS(D0) <= state->near) && (FFABS(D1) <= state->near) && (FFABS(D2) <= state->near)) {
313 /* decode full runs while available */
314 while(get_bits1(&s->gb)) {
316 r = 1 << log2_run[state->run_index[comp]];
317 if(x + r * stride > w) {
318 r = (w - x) / stride;
320 for(i = 0; i < r; i++) {
324 /* if EOL reached, we stop decoding */
325 if(r != (1 << log2_run[state->run_index[comp]]))
327 if(state->run_index[comp] < 31)
328 state->run_index[comp]++;
332 /* decode aborted run */
333 r = log2_run[state->run_index[comp]];
335 r = get_bits_long(&s->gb, r);
336 for(i = 0; i < r; i++) {
341 /* decode run termination value */
343 RItype = (FFABS(Ra - Rb) <= state->near) ? 1 : 0;
344 err = ls_get_code_runterm(&s->gb, state, RItype, log2_run[state->run_index[comp]]);
345 if(state->run_index[comp])
346 state->run_index[comp]--;
348 if(state->near && RItype){
358 if(pred < -state->near)
359 pred += state->range * state->twonear;
360 else if(pred > state->maxval + state->near)
361 pred -= state->range * state->twonear;
362 pred = clip(pred, 0, state->maxval);
367 } else { /* regular mode */
370 context = quantize(state, D0) * 81 + quantize(state, D1) * 9 + quantize(state, D2);
371 pred = mid_pred(Ra, Ra + Rb - Rc, Rb);
381 pred = clip(pred - state->C[context], 0, state->maxval);
382 err = -ls_get_code_regular(&s->gb, state, context);
384 pred = clip(pred + state->C[context], 0, state->maxval);
385 err = ls_get_code_regular(&s->gb, state, context);
388 /* we have to do something more for near-lossless coding */
391 if(pred < -state->near)
392 pred += state->range * state->twonear;
393 else if(pred > state->maxval + state->near)
394 pred -= state->range * state->twonear;
395 pred = clip(pred, 0, state->maxval);
404 static int ls_decode_picture(MJpegDecodeContext *s, int near, int point_transform, int ilv){
406 uint8_t *zero, *last, *cur;
408 int off, stride, width;
410 zero = av_mallocz(s->picture.linesize[0]);
412 cur = s->picture.data[0];
414 state = av_mallocz(sizeof(JLSState));
415 /* initialize JPEG-LS state from JPEG parameters */
417 state->bpp = (s->bits < 2) ? 2 : s->bits;
418 state->maxval = s->maxval;
422 state->reset = s->reset;
423 reset_ls_coding_parameters(state, 0);
424 ls_init_state(state);
426 // av_log(s->avctx, AV_LOG_DEBUG, "JPEG-LS params: %ix%i NEAR=%i MV=%i T(%i,%i,%i) RESET=%i, LIMIT=%i, qbpp=%i, RANGE=%i\n",s->width,s->height,state->near,state->maxval,state->T1,state->T2,state->T3,state->reset,state->limit,state->qbpp, state->range);
427 // av_log(s->avctx, AV_LOG_DEBUG, "JPEG params: ILV=%i Pt=%i BPP=%i, scan = %i\n", ilv, point_transform, s->bits, s->cur_scan);
428 if(ilv == 0) { /* separate planes */
429 off = s->cur_scan - 1;
430 stride = (s->nb_components > 1) ? 3 : 1;
431 width = s->width * stride;
433 for(i = 0; i < s->height; i++) {
434 ls_decode_line(state, s, last, cur, t, width, stride, off);
437 cur += s->picture.linesize[0];
439 if (s->restart_interval && !--s->restart_count) {
440 align_get_bits(&s->gb);
441 skip_bits(&s->gb, 16); /* skip RSTn */
444 } else if(ilv == 1) { /* line interleaving */
446 int Rc[3] = {0, 0, 0};
447 memset(cur, 0, s->picture.linesize[0]);
448 width = s->width * 3;
449 for(i = 0; i < s->height; i++) {
450 for(j = 0; j < 3; j++) {
451 ls_decode_line(state, s, last + j, cur + j, Rc[j], width, 3, j);
454 if (s->restart_interval && !--s->restart_count) {
455 align_get_bits(&s->gb);
456 skip_bits(&s->gb, 16); /* skip RSTn */
460 cur += s->picture.linesize[0];
462 } else if(ilv == 2) { /* sample interleaving */
463 av_log(s->avctx, AV_LOG_ERROR, "Sample interleaved images are not supported.\n");
475 #if defined(CONFIG_ENCODERS) && defined(CONFIG_JPEGLS_ENCODER)
476 /********** Encoder-specific functions **********/
479 * Encode error from regular symbol
481 static inline void ls_encode_regular(JLSState *state, PutBitContext *pb, int Q, int err){
486 for(k = 0; (state->N[Q] << k) < state->A[Q]; k++);
488 map = !state->near && !k && (2 * state->B[Q] <= -state->N[Q]);
492 if(err >= ((state->range + 1) >> 1)) {
494 val = 2 * FFABS(err) - 1 - map;
498 set_ur_golomb_jpegls(pb, val, k, state->limit, state->qbpp);
500 state->A[Q] += FFABS(err);
501 state->B[Q] += err * state->twonear;
503 if(state->N[Q] == state->reset) {
510 if(state->B[Q] <= -state->N[Q]) {
511 state->B[Q] += state->N[Q];
512 if(state->C[Q] > -128)
514 if(state->B[Q] <= -state->N[Q])
515 state->B[Q] = -state->N[Q] + 1;
516 }else if(state->B[Q] > 0){
517 state->B[Q] -= state->N[Q];
518 if(state->C[Q] < 127)
526 * Encode error from run termination
528 static inline void ls_encode_runterm(JLSState *state, PutBitContext *pb, int RItype, int err, int limit_add){
531 int Q = 365 + RItype;
536 temp += state->N[Q] >> 1;
537 for(k = 0; (state->N[Q] << k) < temp; k++);
539 if(!k && err && (2 * state->B[Q] < state->N[Q]))
543 val = - (2 * err) - 1 - RItype + map;
545 val = 2 * err - RItype - map;
546 set_ur_golomb_jpegls(pb, val, k, state->limit - limit_add - 1, state->qbpp);
550 state->A[Q] += (val + 1 - RItype) >> 1;
552 if(state->N[Q] == state->reset) {
561 * Encode run value as specified by JPEG-LS standard
563 static inline void ls_encode_run(JLSState *state, PutBitContext *pb, int run, int comp, int trail){
564 while(run >= (1 << log2_run[state->run_index[comp]])){
566 run -= 1 << log2_run[state->run_index[comp]];
567 if(state->run_index[comp] < 31)
568 state->run_index[comp]++;
570 /* if hit EOL, encode another full run, else encode aborted run */
575 if(log2_run[state->run_index[comp]])
576 put_bits(pb, log2_run[state->run_index[comp]], run);
581 * Encode one line of image
583 static inline void ls_encode_line(JLSState *state, PutBitContext *pb, uint8_t *last, uint8_t *cur, int last2, int w, int stride, int comp){
591 /* compute gradients */
592 Ra = x ? cur[x - stride] : last[x];
594 Rc = x ? last[x - stride] : last2;
595 Rd = (x >= w - stride) ? last[x] : last[x + stride];
601 if((FFABS(D0) <= state->near) && (FFABS(D1) <= state->near) && (FFABS(D2) <= state->near)) {
602 int RUNval, RItype, run;
606 while(x < w && (FFABS(cur[x] - RUNval) <= state->near)){
611 ls_encode_run(state, pb, run, comp, x < w);
615 RItype = (FFABS(Ra - Rb) <= state->near);
616 pred = RItype ? Ra : Rb;
619 if(!RItype && Ra > Rb)
624 err = (state->near + err) / state->twonear;
626 err = -(state->near - err) / state->twonear;
628 if(RItype || (Rb >= Ra))
629 Ra = clip(pred + err * state->twonear, 0, state->maxval);
631 Ra = clip(pred - err * state->twonear, 0, state->maxval);
636 if(err >= ((state->range + 1) >> 1))
639 ls_encode_runterm(state, pb, RItype, err, log2_run[state->run_index[comp]]);
641 if(state->run_index[comp] > 0)
642 state->run_index[comp]--;
644 } else { /* regular mode */
647 context = quantize(state, D0) * 81 + quantize(state, D1) * 9 + quantize(state, D2);
648 pred = mid_pred(Ra, Ra + Rb - Rc, Rb);
653 pred = clip(pred - state->C[context], 0, state->maxval);
657 pred = clip(pred + state->C[context], 0, state->maxval);
663 err = (state->near + err) / state->twonear;
665 err = -(state->near - err) / state->twonear;
667 Ra = clip(pred + err * state->twonear, 0, state->maxval);
669 Ra = clip(pred - err * state->twonear, 0, state->maxval);
673 ls_encode_regular(state, pb, context, err);
679 static void ls_store_lse(JLSState *state, PutBitContext *pb){
680 /* Test if we have default params and don't need to store LSE */
682 memset(&state2, 0, sizeof(JLSState));
684 state2.near = state->near;
685 reset_ls_coding_parameters(&state2, 1);
686 if(state->T1 == state2.T1 && state->T2 == state2.T2 && state->T3 == state2.T3 && state->reset == state2.reset)
688 /* store LSE type 1 */
690 put_bits(pb, 16, 13);
692 put_bits(pb, 16, state->maxval);
693 put_bits(pb, 16, state->T1);
694 put_bits(pb, 16, state->T2);
695 put_bits(pb, 16, state->T3);
696 put_bits(pb, 16, state->reset);
699 static int encode_picture_ls(AVCodecContext *avctx, unsigned char *buf, int buf_size, void *data){
700 JpeglsContext * const s = avctx->priv_data;
701 AVFrame *pict = data;
702 AVFrame * const p= (AVFrame*)&s->picture;
703 const int near = avctx->prediction_method;
704 PutBitContext pb, pb2;
706 uint8_t *buf2, *zero, *cur, *last;
711 buf2 = av_malloc(buf_size);
713 init_put_bits(&pb, buf, buf_size);
714 init_put_bits(&pb2, buf2, buf_size);
717 p->pict_type= FF_I_TYPE;
720 comps = (avctx->pix_fmt == PIX_FMT_GRAY8) ? 1 : 3;
722 /* write our own JPEG header, can't use mjpeg_picture_header */
723 put_marker(&pb, SOI);
724 put_marker(&pb, SOF48);
725 put_bits(&pb, 16, 8 + comps * 3); // header size depends on components
726 put_bits(&pb, 8, 8); // bpp
727 put_bits(&pb, 16, avctx->height);
728 put_bits(&pb, 16, avctx->width);
729 put_bits(&pb, 8, comps); // components
730 for(i = 1; i <= comps; i++) {
731 put_bits(&pb, 8, i); // component ID
732 put_bits(&pb, 8, 0x11); // subsampling: none
733 put_bits(&pb, 8, 0); // Tiq, used by JPEG-LS ext
736 put_marker(&pb, SOS);
737 put_bits(&pb, 16, 6 + comps * 2);
738 put_bits(&pb, 8, comps);
739 for(i = 1; i <= comps; i++) {
740 put_bits(&pb, 8, i); // component ID
741 put_bits(&pb, 8, 0); // mapping index: none
743 put_bits(&pb, 8, near);
744 put_bits(&pb, 8, (comps > 1) ? 1 : 0); // interleaving: 0 - plane, 1 - line
745 put_bits(&pb, 8, 0); // point transform: none
747 state = av_mallocz(sizeof(JLSState));
748 /* initialize JPEG-LS state from JPEG parameters */
751 reset_ls_coding_parameters(state, 0);
752 ls_init_state(state);
754 ls_store_lse(state, &pb);
756 zero = av_mallocz(p->linesize[0]);
759 if(avctx->pix_fmt == PIX_FMT_GRAY8){
762 for(i = 0; i < avctx->height; i++) {
763 ls_encode_line(state, &pb2, last, cur, t, avctx->width, 1, 0);
766 cur += p->linesize[0];
768 }else if(avctx->pix_fmt == PIX_FMT_RGB24){
770 int Rc[3] = {0, 0, 0};
772 width = avctx->width * 3;
773 for(i = 0; i < avctx->height; i++) {
774 for(j = 0; j < 3; j++) {
775 ls_encode_line(state, &pb2, last + j, cur + j, Rc[j], width, 3, j);
779 cur += s->picture.linesize[0];
781 }else if(avctx->pix_fmt == PIX_FMT_BGR24){
783 int Rc[3] = {0, 0, 0};
785 width = avctx->width * 3;
786 for(i = 0; i < avctx->height; i++) {
787 for(j = 2; j >= 0; j--) {
788 ls_encode_line(state, &pb2, last + j, cur + j, Rc[j], width, 3, j);
792 cur += s->picture.linesize[0];
799 flush_put_bits(&pb2);
800 /* do escape coding */
801 size = put_bits_count(&pb2) >> 3;
802 init_get_bits(&gb, buf2, size);
803 while(get_bits_count(&gb) < size * 8){
805 v = get_bits(&gb, 8);
808 v = get_bits(&gb, 7);
816 put_marker(&pb, EOI);
821 return put_bits_count(&pb) >> 3;
824 static int encode_init_ls(AVCodecContext *ctx) {
825 JpeglsContext *c = (JpeglsContext*)ctx->priv_data;
828 ctx->coded_frame = &c->picture;
830 if(ctx->pix_fmt != PIX_FMT_GRAY8 && ctx->pix_fmt != PIX_FMT_RGB24 && ctx->pix_fmt != PIX_FMT_BGR24){
831 av_log(ctx, AV_LOG_ERROR, "Only grayscale and RGB24/BGR24 images are supported\n");
837 AVCodec jpegls_encoder = { //FIXME avoid MPV_* lossless jpeg shouldnt need them
841 sizeof(JpeglsContext),
845 .pix_fmts= (enum PixelFormat[]){PIX_FMT_BGR24, PIX_FMT_RGB24, PIX_FMT_GRAY8, -1},