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] = FFMAX((state->range + 32) >> 6, 2);
87 * Calculate quantized gradient value, used for context determination
89 static inline int quantize(JLSState *s, int v){ //FIXME optimize
92 if(v <= -s->T3) return -4;
93 if(v <= -s->T2) return -3;
94 if(v <= -s->T1) return -2;
95 if(v < -s->near) return -1;
98 if(v <= s->near) return 0;
99 if(v < s->T1) return 1;
100 if(v < s->T2) return 2;
101 if(v < s->T3) return 3;
107 * Custom value clipping function used in T1, T2, T3 calculation
109 static inline int iso_clip(int v, int vmin, int vmax){
110 if(v > vmax || v < vmin) return vmin;
115 * Calculate JPEG-LS codec values
117 static void reset_ls_coding_parameters(JLSState *s, int reset_all){
118 const int basic_t1= 3;
119 const int basic_t2= 7;
120 const int basic_t3= 21;
123 if(s->maxval==0 || reset_all) s->maxval= (1 << s->bpp) - 1;
126 factor= (FFMIN(s->maxval, 4095) + 128)>>8;
128 if(s->T1==0 || reset_all)
129 s->T1= iso_clip(factor*(basic_t1-2) + 2 + 3*s->near, s->near+1, s->maxval);
130 if(s->T2==0 || reset_all)
131 s->T2= iso_clip(factor*(basic_t2-3) + 3 + 5*s->near, s->T1, s->maxval);
132 if(s->T3==0 || reset_all)
133 s->T3= iso_clip(factor*(basic_t3-4) + 4 + 7*s->near, s->T2, s->maxval);
135 factor= 256 / (s->maxval + 1);
137 if(s->T1==0 || reset_all)
138 s->T1= iso_clip(FFMAX(2, basic_t1/factor + 3*s->near), s->near+1, s->maxval);
139 if(s->T2==0 || reset_all)
140 s->T2= iso_clip(FFMAX(3, basic_t2/factor + 5*s->near), s->T1, s->maxval);
141 if(s->T3==0 || reset_all)
142 s->T3= iso_clip(FFMAX(4, basic_t3/factor + 6*s->near), s->T2, s->maxval);
145 if(s->reset==0 || reset_all) s->reset= 64;
146 // av_log(NULL, AV_LOG_DEBUG, "[JPEG-LS RESET] T=%i,%i,%i\n", s->T1, s->T2, s->T3);
150 /********** Decoder-specific functions **********/
153 * Decode LSE block with initialization parameters
155 static int decode_lse(MJpegDecodeContext *s)
159 /* XXX: verify len field validity */
160 len = get_bits(&s->gb, 16);
161 id = get_bits(&s->gb, 8);
165 s->maxval= get_bits(&s->gb, 16);
166 s->t1= get_bits(&s->gb, 16);
167 s->t2= get_bits(&s->gb, 16);
168 s->t3= get_bits(&s->gb, 16);
169 s->reset= get_bits(&s->gb, 16);
171 // reset_ls_coding_parameters(s, 0);
176 av_log(s->avctx, AV_LOG_ERROR, "palette not supported\n");
179 av_log(s->avctx, AV_LOG_ERROR, "oversize image not supported\n");
182 av_log(s->avctx, AV_LOG_ERROR, "invalid id %d\n", id);
185 // av_log(s->avctx, AV_LOG_DEBUG, "ID=%i, T=%i,%i,%i\n", id, s->t1, s->t2, s->t3);
192 * Get context-dependent Golomb code, decode it and update context
194 static inline int ls_get_code_regular(GetBitContext *gb, JLSState *state, int Q){
197 for(k = 0; (state->N[Q] << k) < state->A[Q]; k++);
200 if(!show_bits_long(gb, 32))return -1;
202 ret = get_ur_golomb_jpegls(gb, k, state->limit, state->qbpp);
204 /* decode mapped error */
206 ret = -((ret + 1) >> 1);
210 /* for NEAR=0, k=0 and 2*B[Q] <= - N[Q] mapping is reversed */
211 if(!state->near && !k && (2 * state->B[Q] <= -state->N[Q]))
214 state->A[Q] += FFABS(ret);
215 ret *= state->twonear;
218 if(state->N[Q] == state->reset) {
225 if(state->B[Q] <= -state->N[Q]) {
226 state->B[Q] += state->N[Q];
227 if(state->C[Q] > -128)
229 if(state->B[Q] <= -state->N[Q])
230 state->B[Q] = -state->N[Q] + 1;
231 }else if(state->B[Q] > 0){
232 state->B[Q] -= state->N[Q];
233 if(state->C[Q] < 127)
243 * Get Golomb code, decode it and update state for run termination
245 static inline int ls_get_code_runterm(GetBitContext *gb, JLSState *state, int RItype, int limit_add){
246 int k, ret, temp, map;
247 int Q = 365 + RItype;
251 temp += state->N[Q] >> 1;
253 for(k = 0; (state->N[Q] << k) < temp; k++);
256 if(!show_bits_long(gb, 32))return -1;
258 ret = get_ur_golomb_jpegls(gb, k, state->limit - limit_add - 1, state->qbpp);
260 /* decode mapped error */
262 if(!k && (RItype || ret) && (2 * state->B[Q] < state->N[Q]))
267 ret = map - ((ret + 1) >> 1);
274 state->A[Q] += FFABS(ret) - RItype;
275 ret *= state->twonear;
276 if(state->N[Q] == state->reset){
286 #define R(a, i ) (bits == 8 ? ((uint8_t*)(a))[i] : ((uint16_t*)(a))[i] )
287 #define W(a, i, v) (bits == 8 ? (((uint8_t*)(a))[i]=v) : (((uint16_t*)(a))[i]=v))
289 * Decode one line of image
291 static inline void ls_decode_line(JLSState *state, MJpegDecodeContext *s, void *last, void *dst, int last2, int w, int stride, int comp, int bits){
299 /* compute gradients */
300 Ra = x ? R(dst, x - stride) : R(last, x);
302 Rc = x ? R(last, x - stride) : last2;
303 Rd = (x >= w - stride) ? R(last, x) : R(last, x + stride);
308 if((FFABS(D0) <= state->near) && (FFABS(D1) <= state->near) && (FFABS(D2) <= state->near)) {
312 /* decode full runs while available */
313 while(get_bits1(&s->gb)) {
315 r = 1 << log2_run[state->run_index[comp]];
316 if(x + r * stride > w) {
317 r = (w - x) / stride;
319 for(i = 0; i < r; i++) {
323 /* if EOL reached, we stop decoding */
324 if(r != (1 << log2_run[state->run_index[comp]]))
326 if(state->run_index[comp] < 31)
327 state->run_index[comp]++;
331 /* decode aborted run */
332 r = log2_run[state->run_index[comp]];
334 r = get_bits_long(&s->gb, r);
335 for(i = 0; i < r; i++) {
340 /* decode run termination value */
342 RItype = (FFABS(Ra - Rb) <= state->near) ? 1 : 0;
343 err = ls_get_code_runterm(&s->gb, state, RItype, log2_run[state->run_index[comp]]);
344 if(state->run_index[comp])
345 state->run_index[comp]--;
347 if(state->near && RItype){
355 } else { /* regular mode */
358 context = quantize(state, D0) * 81 + quantize(state, D1) * 9 + quantize(state, D2);
359 pred = mid_pred(Ra, Ra + Rb - Rc, Rb);
369 pred = clip(pred - state->C[context], 0, state->maxval);
370 err = -ls_get_code_regular(&s->gb, state, context);
372 pred = clip(pred + state->C[context], 0, state->maxval);
373 err = ls_get_code_regular(&s->gb, state, context);
376 /* we have to do something more for near-lossless coding */
380 if(pred < -state->near)
381 pred += state->range * state->twonear;
382 else if(pred > state->maxval + state->near)
383 pred -= state->range * state->twonear;
384 pred = clip(pred, 0, state->maxval);
392 static int ls_decode_picture(MJpegDecodeContext *s, int near, int point_transform, int ilv){
394 uint8_t *zero, *last, *cur;
396 int off = 0, stride = 1, width, shift;
398 zero = av_mallocz(s->picture.linesize[0]);
400 cur = s->picture.data[0];
402 state = av_mallocz(sizeof(JLSState));
403 /* initialize JPEG-LS state from JPEG parameters */
405 state->bpp = (s->bits < 2) ? 2 : s->bits;
406 state->maxval = s->maxval;
410 state->reset = s->reset;
411 reset_ls_coding_parameters(state, 0);
412 ls_init_state(state);
415 shift = point_transform + (8 - s->bits);
417 shift = point_transform + (16 - s->bits);
419 // 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);
420 // 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);
421 if(ilv == 0) { /* separate planes */
422 off = s->cur_scan - 1;
423 stride = (s->nb_components > 1) ? 3 : 1;
424 width = s->width * stride;
426 for(i = 0; i < s->height; i++) {
428 ls_decode_line(state, s, last, cur, t, width, stride, off, 8);
431 ls_decode_line(state, s, last, cur, t, width, stride, off, 16);
432 t = *((uint16_t*)last);
435 cur += s->picture.linesize[0];
437 if (s->restart_interval && !--s->restart_count) {
438 align_get_bits(&s->gb);
439 skip_bits(&s->gb, 16); /* skip RSTn */
442 } else if(ilv == 1) { /* line interleaving */
444 int Rc[3] = {0, 0, 0};
445 memset(cur, 0, s->picture.linesize[0]);
446 width = s->width * 3;
447 for(i = 0; i < s->height; i++) {
448 for(j = 0; j < 3; j++) {
449 ls_decode_line(state, s, last + j, cur + j, Rc[j], width, 3, j, 8);
452 if (s->restart_interval && !--s->restart_count) {
453 align_get_bits(&s->gb);
454 skip_bits(&s->gb, 16); /* skip RSTn */
458 cur += s->picture.linesize[0];
460 } else if(ilv == 2) { /* sample interleaving */
461 av_log(s->avctx, AV_LOG_ERROR, "Sample interleaved images are not supported.\n");
467 if(shift){ /* we need to do point transform or normalize samples */
470 w = s->width * s->nb_components;
473 uint8_t *src = s->picture.data[0];
475 for(i = 0; i < s->height; i++){
476 for(x = off; x < w; x+= stride){
479 src += s->picture.linesize[0];
482 uint16_t *src = s->picture.data[0];
484 for(i = 0; i < s->height; i++){
485 for(x = 0; x < w; x++){
488 src += s->picture.linesize[0]/2;
498 #if defined(CONFIG_ENCODERS) && defined(CONFIG_JPEGLS_ENCODER)
499 /********** Encoder-specific functions **********/
502 * Encode error from regular symbol
504 static inline void ls_encode_regular(JLSState *state, PutBitContext *pb, int Q, int err){
509 for(k = 0; (state->N[Q] << k) < state->A[Q]; k++);
511 map = !state->near && !k && (2 * state->B[Q] <= -state->N[Q]);
515 if(err >= ((state->range + 1) >> 1)) {
517 val = 2 * FFABS(err) - 1 - map;
521 set_ur_golomb_jpegls(pb, val, k, state->limit, state->qbpp);
523 state->A[Q] += FFABS(err);
524 state->B[Q] += err * state->twonear;
526 if(state->N[Q] == state->reset) {
533 if(state->B[Q] <= -state->N[Q]) {
534 state->B[Q] += state->N[Q];
535 if(state->C[Q] > -128)
537 if(state->B[Q] <= -state->N[Q])
538 state->B[Q] = -state->N[Q] + 1;
539 }else if(state->B[Q] > 0){
540 state->B[Q] -= state->N[Q];
541 if(state->C[Q] < 127)
549 * Encode error from run termination
551 static inline void ls_encode_runterm(JLSState *state, PutBitContext *pb, int RItype, int err, int limit_add){
554 int Q = 365 + RItype;
559 temp += state->N[Q] >> 1;
560 for(k = 0; (state->N[Q] << k) < temp; k++);
562 if(!k && err && (2 * state->B[Q] < state->N[Q]))
566 val = - (2 * err) - 1 - RItype + map;
568 val = 2 * err - RItype - map;
569 set_ur_golomb_jpegls(pb, val, k, state->limit - limit_add - 1, state->qbpp);
573 state->A[Q] += (val + 1 - RItype) >> 1;
575 if(state->N[Q] == state->reset) {
584 * Encode run value as specified by JPEG-LS standard
586 static inline void ls_encode_run(JLSState *state, PutBitContext *pb, int run, int comp, int trail){
587 while(run >= (1 << log2_run[state->run_index[comp]])){
589 run -= 1 << log2_run[state->run_index[comp]];
590 if(state->run_index[comp] < 31)
591 state->run_index[comp]++;
593 /* if hit EOL, encode another full run, else encode aborted run */
598 if(log2_run[state->run_index[comp]])
599 put_bits(pb, log2_run[state->run_index[comp]], run);
604 * Encode one line of image
606 static inline void ls_encode_line(JLSState *state, PutBitContext *pb, void *last, void *cur, int last2, int w, int stride, int comp, int bits){
614 /* compute gradients */
615 Ra = x ? R(cur, x - stride) : R(last, x);
617 Rc = x ? R(last, x - stride) : last2;
618 Rd = (x >= w - stride) ? R(last, x) : R(last, x + stride);
624 if((FFABS(D0) <= state->near) && (FFABS(D1) <= state->near) && (FFABS(D2) <= state->near)) {
625 int RUNval, RItype, run;
629 while(x < w && (FFABS(R(cur, x) - RUNval) <= state->near)){
634 ls_encode_run(state, pb, run, comp, x < w);
638 RItype = (FFABS(Ra - Rb) <= state->near);
639 pred = RItype ? Ra : Rb;
640 err = R(cur, x) - pred;
642 if(!RItype && Ra > Rb)
647 err = (state->near + err) / state->twonear;
649 err = -(state->near - err) / state->twonear;
651 if(RItype || (Rb >= Ra))
652 Ra = clip(pred + err * state->twonear, 0, state->maxval);
654 Ra = clip(pred - err * state->twonear, 0, state->maxval);
659 if(err >= ((state->range + 1) >> 1))
662 ls_encode_runterm(state, pb, RItype, err, log2_run[state->run_index[comp]]);
664 if(state->run_index[comp] > 0)
665 state->run_index[comp]--;
666 } else { /* regular mode */
669 context = quantize(state, D0) * 81 + quantize(state, D1) * 9 + quantize(state, D2);
670 pred = mid_pred(Ra, Ra + Rb - Rc, Rb);
675 pred = clip(pred - state->C[context], 0, state->maxval);
676 err = pred - R(cur, x);
679 pred = clip(pred + state->C[context], 0, state->maxval);
680 err = R(cur, x) - pred;
685 err = (state->near + err) / state->twonear;
687 err = -(state->near - err) / state->twonear;
689 Ra = clip(pred + err * state->twonear, 0, state->maxval);
691 Ra = clip(pred - err * state->twonear, 0, state->maxval);
695 ls_encode_regular(state, pb, context, err);
701 static void ls_store_lse(JLSState *state, PutBitContext *pb){
702 /* Test if we have default params and don't need to store LSE */
704 memset(&state2, 0, sizeof(JLSState));
705 state2.bpp = state->bpp;
706 state2.near = state->near;
707 reset_ls_coding_parameters(&state2, 1);
708 if(state->T1 == state2.T1 && state->T2 == state2.T2 && state->T3 == state2.T3 && state->reset == state2.reset)
710 /* store LSE type 1 */
712 put_bits(pb, 16, 13);
714 put_bits(pb, 16, state->maxval);
715 put_bits(pb, 16, state->T1);
716 put_bits(pb, 16, state->T2);
717 put_bits(pb, 16, state->T3);
718 put_bits(pb, 16, state->reset);
721 static int encode_picture_ls(AVCodecContext *avctx, unsigned char *buf, int buf_size, void *data){
722 JpeglsContext * const s = avctx->priv_data;
723 AVFrame *pict = data;
724 AVFrame * const p= (AVFrame*)&s->picture;
725 const int near = avctx->prediction_method;
726 PutBitContext pb, pb2;
728 uint8_t *buf2, *zero, *cur, *last;
733 buf2 = av_malloc(buf_size);
735 init_put_bits(&pb, buf, buf_size);
736 init_put_bits(&pb2, buf2, buf_size);
739 p->pict_type= FF_I_TYPE;
742 if(avctx->pix_fmt == PIX_FMT_GRAY8 || avctx->pix_fmt == PIX_FMT_GRAY16)
747 /* write our own JPEG header, can't use mjpeg_picture_header */
748 put_marker(&pb, SOI);
749 put_marker(&pb, SOF48);
750 put_bits(&pb, 16, 8 + comps * 3); // header size depends on components
751 put_bits(&pb, 8, (avctx->pix_fmt == PIX_FMT_GRAY16) ? 16 : 8); // bpp
752 put_bits(&pb, 16, avctx->height);
753 put_bits(&pb, 16, avctx->width);
754 put_bits(&pb, 8, comps); // components
755 for(i = 1; i <= comps; i++) {
756 put_bits(&pb, 8, i); // component ID
757 put_bits(&pb, 8, 0x11); // subsampling: none
758 put_bits(&pb, 8, 0); // Tiq, used by JPEG-LS ext
761 put_marker(&pb, SOS);
762 put_bits(&pb, 16, 6 + comps * 2);
763 put_bits(&pb, 8, comps);
764 for(i = 1; i <= comps; i++) {
765 put_bits(&pb, 8, i); // component ID
766 put_bits(&pb, 8, 0); // mapping index: none
768 put_bits(&pb, 8, near);
769 put_bits(&pb, 8, (comps > 1) ? 1 : 0); // interleaving: 0 - plane, 1 - line
770 put_bits(&pb, 8, 0); // point transform: none
772 state = av_mallocz(sizeof(JLSState));
773 /* initialize JPEG-LS state from JPEG parameters */
775 state->bpp = (avctx->pix_fmt == PIX_FMT_GRAY16) ? 16 : 8;
776 reset_ls_coding_parameters(state, 0);
777 ls_init_state(state);
779 ls_store_lse(state, &pb);
781 zero = av_mallocz(p->linesize[0]);
784 if(avctx->pix_fmt == PIX_FMT_GRAY8){
787 for(i = 0; i < avctx->height; i++) {
788 ls_encode_line(state, &pb2, last, cur, t, avctx->width, 1, 0, 8);
791 cur += p->linesize[0];
793 }else if(avctx->pix_fmt == PIX_FMT_GRAY16){
796 for(i = 0; i < avctx->height; i++) {
797 ls_encode_line(state, &pb2, last, cur, t, avctx->width, 1, 0, 16);
798 t = *((uint16_t*)last);
800 cur += p->linesize[0];
802 }else if(avctx->pix_fmt == PIX_FMT_RGB24){
804 int Rc[3] = {0, 0, 0};
806 width = avctx->width * 3;
807 for(i = 0; i < avctx->height; i++) {
808 for(j = 0; j < 3; j++) {
809 ls_encode_line(state, &pb2, last + j, cur + j, Rc[j], width, 3, j, 8);
813 cur += s->picture.linesize[0];
815 }else if(avctx->pix_fmt == PIX_FMT_BGR24){
817 int Rc[3] = {0, 0, 0};
819 width = avctx->width * 3;
820 for(i = 0; i < avctx->height; i++) {
821 for(j = 2; j >= 0; j--) {
822 ls_encode_line(state, &pb2, last + j, cur + j, Rc[j], width, 3, j, 8);
826 cur += s->picture.linesize[0];
833 flush_put_bits(&pb2);
834 /* do escape coding */
835 size = put_bits_count(&pb2) >> 3;
836 init_get_bits(&gb, buf2, size);
837 while(get_bits_count(&gb) < size * 8){
839 v = get_bits(&gb, 8);
842 v = get_bits(&gb, 7);
850 put_marker(&pb, EOI);
855 return put_bits_count(&pb) >> 3;
858 static int encode_init_ls(AVCodecContext *ctx) {
859 JpeglsContext *c = (JpeglsContext*)ctx->priv_data;
862 ctx->coded_frame = &c->picture;
864 if(ctx->pix_fmt != PIX_FMT_GRAY8 && ctx->pix_fmt != PIX_FMT_GRAY16 && ctx->pix_fmt != PIX_FMT_RGB24 && ctx->pix_fmt != PIX_FMT_BGR24){
865 av_log(ctx, AV_LOG_ERROR, "Only grayscale and RGB24/BGR24 images are supported\n");
871 AVCodec jpegls_encoder = { //FIXME avoid MPV_* lossless jpeg shouldnt need them
875 sizeof(JpeglsContext),
879 .pix_fmts= (enum PixelFormat[]){PIX_FMT_BGR24, PIX_FMT_RGB24, PIX_FMT_GRAY8, PIX_FMT_GRAY16, -1},