History

jpegls: K&R formatting cosmetics

Luca Barbato committed 13 years ago

Commit ff4fc5e1 parent 9cacdab5 files · +336 −266Browse files

Unified · Split

5 files
  1. libavcodec/jpegls.c +43 −30
  2. libavcodec/jpegls.h +45 −33
  3. libavcodec/jpeglsdec.c +112 −96
  4. libavcodec/jpeglsdec.h +2 −1
  5. libavcodec/jpeglsenc.c +134 −106
libavcodec/jpegls.c+43−30
@@ -27,63 +27,76 @@ · 26 unmodified lines
27 27
28#include "jpegls.h"28#include "jpegls.h"
29 29
30void ff_jpegls_init_state(JLSState *state){30void ff_jpegls_init_state(JLSState *state)
31{
31 int i;32 int i;
32 33
33 state->twonear = state->near * 2 + 1;34 state->twonear = state->near * 2 + 1;
34 state->range = (state->maxval + state->twonear - 1) / state->twonear + 1;35 state->range = (state->maxval + state->twonear - 1) / state->twonear + 1;
35 36
36 // QBPP = ceil(log2(RANGE))37 // QBPP = ceil(log2(RANGE))
37 for(state->qbpp = 0; (1 << state->qbpp) < state->range; state->qbpp++);38 for (state->qbpp = 0; (1 << state->qbpp) < state->range; state->qbpp++)
39 ;
38 40
39 if(state->bpp < 8)41 if (state->bpp < 8)
40 state->limit = 16 + 2 * state->bpp - state->qbpp;42 state->limit = 2 * state->bpp - state->qbpp + 16;
41 else43 else
42 state->limit = 4 * state->bpp - state->qbpp;44 state->limit = 4 * state->bpp - state->qbpp;
43 45
44 for(i = 0; i < 367; i++) {46 for (i = 0; i < 367; i++) {
45 state->A[i] = FFMAX(state->range + 32 >> 6, 2);47 state->A[i] = FFMAX(state->range + 32 >> 6, 2);
46 state->N[i] = 1;48 state->N[i] = 1;
47 }49 }
48
49}50}
50 51
51/**52/**
52 * Custom value clipping function used in T1, T2, T3 calculation53 * Custom value clipping function used in T1, T2, T3 calculation
53 */54 */
54static inline int iso_clip(int v, int vmin, int vmax){55static inline int iso_clip(int v, int vmin, int vmax)
55 if(v > vmax || v < vmin) return vmin;56{
56 else return v;57 if (v > vmax || v < vmin)
58 return vmin;
59 else
60 return v;
57}61}
58 62
59void ff_jpegls_reset_coding_parameters(JLSState *s, int reset_all){63void ff_jpegls_reset_coding_parameters(JLSState *s, int reset_all)
60 const int basic_t1= 3;64{
61 const int basic_t2= 7;65 const int basic_t1 = 3;
62 const int basic_t3= 21;66 const int basic_t2 = 7;
67 const int basic_t3 = 21;
63 int factor;68 int factor;
64 69
65 if(s->maxval==0 || reset_all) s->maxval= (1 << s->bpp) - 1;70 if (s->maxval == 0 || reset_all)
71 s->maxval = (1 << s->bpp) - 1;
66 72
67 if(s->maxval >=128){73 if (s->maxval >= 128) {
68 factor = FFMIN(s->maxval, 4095) + 128 >> 8;74 factor = FFMIN(s->maxval, 4095) + 128 >> 8;
69 75
70 if(s->T1==0 || reset_all)76 if (s->T1 == 0 || reset_all)
71 s->T1= iso_clip(factor*(basic_t1-2) + 2 + 3*s->near, s->near+1, s->maxval);77 s->T1 = iso_clip(factor * (basic_t1 - 2) + 2 + 3 * s->near,
72 if(s->T2==0 || reset_all)78 s->near + 1, s->maxval);
73 s->T2= iso_clip(factor*(basic_t2-3) + 3 + 5*s->near, s->T1, s->maxval);79 if (s->T2 == 0 || reset_all)
74 if(s->T3==0 || reset_all)80 s->T2 = iso_clip(factor * (basic_t2 - 3) + 3 + 5 * s->near,
75 s->T3= iso_clip(factor*(basic_t3-4) + 4 + 7*s->near, s->T2, s->maxval);81 s->T1, s->maxval);
76 }else{82 if (s->T3 == 0 || reset_all)
77 factor= 256 / (s->maxval + 1);83 s->T3 = iso_clip(factor * (basic_t3 - 4) + 4 + 7 * s->near,
84 s->T2, s->maxval);
85 } else {
86 factor = 256 / (s->maxval + 1);
78 87
79 if(s->T1==0 || reset_all)88 if (s->T1 == 0 || reset_all)
80 s->T1= iso_clip(FFMAX(2, basic_t1/factor + 3*s->near), s->near+1, s->maxval);89 s->T1 = iso_clip(FFMAX(2, basic_t1 / factor + 3 * s->near),
81 if(s->T2==0 || reset_all)90 s->near + 1, s->maxval);
82 s->T2= iso_clip(FFMAX(3, basic_t2/factor + 5*s->near), s->T1, s->maxval);91 if (s->T2 == 0 || reset_all)
83 if(s->T3==0 || reset_all)92 s->T2 = iso_clip(FFMAX(3, basic_t2 / factor + 5 * s->near),
84 s->T3= iso_clip(FFMAX(4, basic_t3/factor + 7*s->near), s->T2, s->maxval);93 s->T1, s->maxval);
94 if (s->T3 == 0 || reset_all)
95 s->T3 = iso_clip(FFMAX(4, basic_t3 / factor + 7 * s->near),
96 s->T2, s->maxval);
85 }97 }
86 98
87 if(s->reset==0 || reset_all) s->reset= 64;99 if (s->reset == 0 || reset_all)
100 s->reset = 64;
88 av_dlog(NULL, "[JPEG-LS RESET] T=%i,%i,%i\n", s->T1, s->T2, s->T3);101 av_dlog(NULL, "[JPEG-LS RESET] T=%i,%i,%i\n", s->T1, s->T2, s->T3);
89}102}
libavcodec/jpegls.h+45−33
@@ -28,21 +28,21 @@ · 27 unmodified lines
28#ifndef AVCODEC_JPEGLS_H28#ifndef AVCODEC_JPEGLS_H
29#define AVCODEC_JPEGLS_H29#define AVCODEC_JPEGLS_H
30 30
31#include "avcodec.h"
32#include "libavutil/common.h"31#include "libavutil/common.h"
32#include "avcodec.h"
33 33
34typedef struct JpeglsContext{34typedef struct JpeglsContext {
35 AVCodecContext *avctx;35 AVCodecContext *avctx;
36 AVFrame picture;36 AVFrame picture;
37}JpeglsContext;37} JpeglsContext;
38 38
39typedef struct JLSState{39typedef struct JLSState {
40 int T1, T2, T3;40 int T1, T2, T3;
41 int A[367], B[367], C[365], N[367];41 int A[367], B[367], C[365], N[367];
42 int limit, reset, bpp, qbpp, maxval, range;42 int limit, reset, bpp, qbpp, maxval, range;
43 int near, twonear;43 int near, twonear;
44 int run_index[3];44 int run_index[3];
45}JLSState;45} JLSState;
46 46
47extern const uint8_t ff_log2_run[32];47extern const uint8_t ff_log2_run[32];
48 48
@@ -54,19 +54,29 @@ void ff_jpegls_init_state(JLSState *state); · 5 unmodified lines
54/**54/**
55 * Calculate quantized gradient value, used for context determination55 * Calculate quantized gradient value, used for context determination
56 */56 */
57static inline int ff_jpegls_quantize(JLSState *s, int v){ //FIXME optimize57static inline int ff_jpegls_quantize(JLSState *s, int v)
58 if(v==0) return 0;58{
59 if(v < 0){59 if (v == 0)
60 if(v <= -s->T3) return -4;
61 if(v <= -s->T2) return -3;
62 if(v <= -s->T1) return -2;
63 if(v < -s->near) return -1;
64 return 0;60 return 0;
65 }else{61 if (v < 0) {
66 if(v <= s->near) return 0;62 if (v <= -s->T3)
67 if(v < s->T1) return 1;63 return -4;
68 if(v < s->T2) return 2;64 if (v <= -s->T2)
69 if(v < s->T3) return 3;65 return -3;
66 if (v <= -s->T1)
67 return -2;
68 if (v < -s->near)
69 return -1;
70 return 0;
71 } else {
72 if (v <= s->near)
73 return 0;
74 if (v < s->T1)
75 return 1;
76 if (v < s->T2)
77 return 2;
78 if (v < s->T3)
79 return 3;
70 return 4;80 return 4;
71 }81 }
72}82}
@@ -76,37 +86,39 @@ static inline int ff_jpegls_quantize(JLSState *s, int v){ //FIXME optimize · 3 unmodified lines
76 */86 */
77void ff_jpegls_reset_coding_parameters(JLSState *s, int reset_all);87void ff_jpegls_reset_coding_parameters(JLSState *s, int reset_all);
78 88
79 89static inline void ff_jpegls_downscale_state(JLSState *state, int Q)
80static inline void ff_jpegls_downscale_state(JLSState *state, int Q){90{
81 if(state->N[Q] == state->reset){91 if (state->N[Q] == state->reset) {
82 state->A[Q] >>=1;92 state->A[Q] >>= 1;
83 state->B[Q] >>=1;93 state->B[Q] >>= 1;
84 state->N[Q] >>=1;94 state->N[Q] >>= 1;
85 }95 }
86 state->N[Q]++;96 state->N[Q]++;
87}97}
88 98
89static inline int ff_jpegls_update_state_regular(JLSState *state, int Q, int err){99static inline int ff_jpegls_update_state_regular(JLSState *state,
100 int Q, int err)
101{
90 state->A[Q] += FFABS(err);102 state->A[Q] += FFABS(err);
91 err *= state->twonear;103 err *= state->twonear;
92 state->B[Q] += err;104 state->B[Q] += err;
93 105
94 ff_jpegls_downscale_state(state, Q);106 ff_jpegls_downscale_state(state, Q);
95 107
96 if(state->B[Q] <= -state->N[Q]) {108 if (state->B[Q] <= -state->N[Q]) {
97 state->B[Q]= FFMAX(state->B[Q] + state->N[Q], 1-state->N[Q]);109 state->B[Q] = FFMAX(state->B[Q] + state->N[Q], 1 - state->N[Q]);
98 if(state->C[Q] > -128)110 if (state->C[Q] > -128)
99 state->C[Q]--;111 state->C[Q]--;
100 }else if(state->B[Q] > 0){112 } else if (state->B[Q] > 0) {
101 state->B[Q]= FFMIN(state->B[Q] - state->N[Q], 0);113 state->B[Q] = FFMIN(state->B[Q] - state->N[Q], 0);
102 if(state->C[Q] < 127)114 if (state->C[Q] < 127)
103 state->C[Q]++;115 state->C[Q]++;
104 }116 }
105 117
106 return err;118 return err;
107}119}
108 120
109#define R(a, i ) (bits == 8 ? ((uint8_t*)(a))[i] : ((uint16_t*)(a))[i] )121#define R(a, i) (bits == 8 ? ((uint8_t *)(a))[i] : ((uint16_t *)(a))[i])
110#define W(a, i, v) (bits == 8 ? (((uint8_t*)(a))[i]=v) : (((uint16_t*)(a))[i]=v))122#define W(a, i, v) (bits == 8 ? (((uint8_t *)(a))[i] = v) : (((uint16_t *)(a))[i] = v))
111 123
112#endif /* AVCODEC_JPEGLS_H */124#endif /* AVCODEC_JPEGLS_H */
libavcodec/jpeglsdec.c+112−96
@@ -34,18 +34,16 @@ · 33 unmodified lines
34#include "jpegls.h"34#include "jpegls.h"
35#include "jpeglsdec.h"35#include "jpeglsdec.h"
36 36
37
38/*37/*
39* Uncomment this to significantly speed up decoding of broken JPEG-LS38 * 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.39 * (or test broken JPEG-LS decoder) and slow down ordinary decoding a bit.
41*40 *
42* There is no Golomb code with length >= 32 bits possible, so check and41 * There is no Golomb code with length >= 32 bits possible, so check and
43* avoid situation of 32 zeros, Libav Golomb decoder is painfully slow42 * avoid situation of 32 zeros, Libav Golomb decoder is painfully slow
44* on this errors.43 * on this errors.
45*/44 */
46//#define JLS_BROKEN45//#define JLS_BROKEN
47 46
48
49/**47/**
50 * Decode LSE block with initialization parameters48 * Decode LSE block with initialization parameters
51 */49 */
@@ -56,13 +54,13 @@ int ff_jpegls_decode_lse(MJpegDecodeContext *s) · 4 unmodified lines
56 skip_bits(&s->gb, 16); /* length: FIXME: verify field validity */54 skip_bits(&s->gb, 16); /* length: FIXME: verify field validity */
57 id = get_bits(&s->gb, 8);55 id = get_bits(&s->gb, 8);
58 56
59 switch(id){57 switch (id) {
60 case 1:58 case 1:
61 s->maxval= get_bits(&s->gb, 16);59 s->maxval = get_bits(&s->gb, 16);
62 s->t1= get_bits(&s->gb, 16);60 s->t1 = get_bits(&s->gb, 16);
63 s->t2= get_bits(&s->gb, 16);61 s->t2 = get_bits(&s->gb, 16);
64 s->t3= get_bits(&s->gb, 16);62 s->t3 = get_bits(&s->gb, 16);
65 s->reset= get_bits(&s->gb, 16);63 s->reset = get_bits(&s->gb, 16);
66 64
67// ff_jpegls_reset_coding_parameters(s, 0);65// ff_jpegls_reset_coding_parameters(s, 0);
68 //FIXME quant table?66 //FIXME quant table?
@@ -86,27 +84,30 @@ int ff_jpegls_decode_lse(MJpegDecodeContext *s) · 17 unmodified lines
86/**84/**
87 * Get context-dependent Golomb code, decode it and update context85 * Get context-dependent Golomb code, decode it and update context
88 */86 */
89static inline int ls_get_code_regular(GetBitContext *gb, JLSState *state, int Q){87static inline int ls_get_code_regular(GetBitContext *gb, JLSState *state, int Q)
88{
90 int k, ret;89 int k, ret;
91 90
92 for(k = 0; (state->N[Q] << k) < state->A[Q]; k++);91 for (k = 0; (state->N[Q] << k) < state->A[Q]; k++)
92 ;
93 93
94#ifdef JLS_BROKEN94#ifdef JLS_BROKEN
95 if(!show_bits_long(gb, 32))return -1;95 if (!show_bits_long(gb, 32))
96 return -1;
96#endif97#endif
97 ret = get_ur_golomb_jpegls(gb, k, state->limit, state->qbpp);98 ret = get_ur_golomb_jpegls(gb, k, state->limit, state->qbpp);
98 99
99 /* decode mapped error */100 /* decode mapped error */
100 if(ret & 1)101 if (ret & 1)
101 ret = -(ret + 1 >> 1);102 ret = -(ret + 1 >> 1);
102 else103 else
103 ret >>= 1;104 ret >>= 1;
104 105
105 /* for NEAR=0, k=0 and 2*B[Q] <= - N[Q] mapping is reversed */106 /* for NEAR=0, k=0 and 2*B[Q] <= - N[Q] mapping is reversed */
106 if(!state->near && !k && (2 * state->B[Q] <= -state->N[Q]))107 if (!state->near && !k && (2 * state->B[Q] <= -state->N[Q]))
107 ret = -(ret + 1);108 ret = -(ret + 1);
108 109
109 ret= ff_jpegls_update_state_regular(state, Q, ret);110 ret = ff_jpegls_update_state_regular(state, Q, ret);
110 111
111 return ret;112 return ret;
112}113}
@@ -114,28 +115,33 @@ static inline int ls_get_code_regular(GetBitContext *gb, JLSState *state, int Q) · 1 unmodified line
114/**115/**
115 * Get Golomb code, decode it and update state for run termination116 * Get Golomb code, decode it and update state for run termination
116 */117 */
117static inline int ls_get_code_runterm(GetBitContext *gb, JLSState *state, int RItype, int limit_add){118static inline int ls_get_code_runterm(GetBitContext *gb, JLSState *state,
119 int RItype, int limit_add)
120{
118 int k, ret, temp, map;121 int k, ret, temp, map;
119 int Q = 365 + RItype;122 int Q = 365 + RItype;
120 123
121 temp= state->A[Q];124 temp = state->A[Q];
122 if(RItype)125 if (RItype)
123 temp += state->N[Q] >> 1;126 temp += state->N[Q] >> 1;
124 127
125 for(k = 0; (state->N[Q] << k) < temp; k++);128 for (k = 0; (state->N[Q] << k) < temp; k++)
129 ;
126 130
127#ifdef JLS_BROKEN131#ifdef JLS_BROKEN
128 if(!show_bits_long(gb, 32))return -1;132 if (!show_bits_long(gb, 32))
133 return -1;
129#endif134#endif
130 ret = get_ur_golomb_jpegls(gb, k, state->limit - limit_add - 1, state->qbpp);135 ret = get_ur_golomb_jpegls(gb, k, state->limit - limit_add - 1,
136 state->qbpp);
131 137
132 /* decode mapped error */138 /* decode mapped error */
133 map = 0;139 map = 0;
134 if(!k && (RItype || ret) && (2 * state->B[Q] < state->N[Q]))140 if (!k && (RItype || ret) && (2 * state->B[Q] < state->N[Q]))
135 map = 1;141 map = 1;
136 ret += RItype + map;142 ret += RItype + map;
137 143
138 if(ret & 1){144 if (ret & 1) {
139 ret = map - (ret + 1 >> 1);145 ret = map - (ret + 1 >> 1);
140 state->B[Q]++;146 state->B[Q]++;
141 } else {147 } else {
@@ -144,7 +150,7 @@ static inline int ls_get_code_runterm(GetBitContext *gb, JLSState *state, int RI · 2 unmodified lines
144 150
145 /* update state */151 /* update state */
146 state->A[Q] += FFABS(ret) - RItype;152 state->A[Q] += FFABS(ret) - RItype;
147 ret *= state->twonear;153 ret *= state->twonear;
148 ff_jpegls_downscale_state(state, Q);154 ff_jpegls_downscale_state(state, Q);
149 155
150 return ret;156 return ret;
@@ -153,12 +159,15 @@ static inline int ls_get_code_runterm(GetBitContext *gb, JLSState *state, int RI · 2 unmodified lines
153/**159/**
154 * Decode one line of image160 * Decode one line of image
155 */161 */
156static inline void ls_decode_line(JLSState *state, MJpegDecodeContext *s, void *last, void *dst, int last2, int w, int stride, int comp, int bits){162static inline void ls_decode_line(JLSState *state, MJpegDecodeContext *s,
163 void *last, void *dst, int last2, int w,
164 int stride, int comp, int bits)
165{
157 int i, x = 0;166 int i, x = 0;
158 int Ra, Rb, Rc, Rd;167 int Ra, Rb, Rc, Rd;
159 int D0, D1, D2;168 int D0, D1, D2;
160 169
161 while(x < w) {170 while (x < w) {
162 int err, pred;171 int err, pred;
163 172
164 /* compute gradients */173 /* compute gradients */
@@ -170,49 +179,51 @@ static inline void ls_decode_line(JLSState *state, MJpegDecodeContext *s, void * · 5 unmodified lines
170 D1 = Rb - Rc;179 D1 = Rb - Rc;
171 D2 = Rc - Ra;180 D2 = Rc - Ra;
172 /* run mode */181 /* run mode */
173 if((FFABS(D0) <= state->near) && (FFABS(D1) <= state->near) && (FFABS(D2) <= state->near)) {182 if ((FFABS(D0) <= state->near) &&
183 (FFABS(D1) <= state->near) &&
184 (FFABS(D2) <= state->near)) {
174 int r;185 int r;
175 int RItype;186 int RItype;
176 187
177 /* decode full runs while available */188 /* decode full runs while available */
178 while(get_bits1(&s->gb)) {189 while (get_bits1(&s->gb)) {
179 int r;190 int r;
180 r = 1 << ff_log2_run[state->run_index[comp]];191 r = 1 << ff_log2_run[state->run_index[comp]];
181 if(x + r * stride > w) {192 if (x + r * stride > w)
182 r = (w - x) / stride;193 r = (w - x) / stride;
183 }194 for (i = 0; i < r; i++) {
184 for(i = 0; i < r; i++) {
185 W(dst, x, Ra);195 W(dst, x, Ra);
186 x += stride;196 x += stride;
187 }197 }
188 /* if EOL reached, we stop decoding */198 /* if EOL reached, we stop decoding */
189 if (r != 1 << ff_log2_run[state->run_index[comp]])199 if (r != 1 << ff_log2_run[state->run_index[comp]])
190 return;200 return;
191 if(state->run_index[comp] < 31)201 if (state->run_index[comp] < 31)
192 state->run_index[comp]++;202 state->run_index[comp]++;
193 if(x + stride > w)203 if (x + stride > w)
194 return;204 return;
195 }205 }
196 /* decode aborted run */206 /* decode aborted run */
197 r = ff_log2_run[state->run_index[comp]];207 r = ff_log2_run[state->run_index[comp]];
198 if(r)208 if (r)
199 r = get_bits_long(&s->gb, r);209 r = get_bits_long(&s->gb, r);
200 for(i = 0; i < r; i++) {210 for (i = 0; i < r; i++) {
201 W(dst, x, Ra);211 W(dst, x, Ra);
202 x += stride;212 x += stride;
203 }213 }
204 214
205 /* decode run termination value */215 /* decode run termination value */
206 Rb = R(last, x);216 Rb = R(last, x);
207 RItype = (FFABS(Ra - Rb) <= state->near) ? 1 : 0;217 RItype = (FFABS(Ra - Rb) <= state->near) ? 1 : 0;
208 err = ls_get_code_runterm(&s->gb, state, RItype, ff_log2_run[state->run_index[comp]]);218 err = ls_get_code_runterm(&s->gb, state, RItype,
209 if(state->run_index[comp])219 ff_log2_run[state->run_index[comp]]);
220 if (state->run_index[comp])
210 state->run_index[comp]--;221 state->run_index[comp]--;
211 222
212 if(state->near && RItype){223 if (state->near && RItype) {
213 pred = Ra + err;224 pred = Ra + err;
214 } else {225 } else {
215 if(Rb < Ra)226 if (Rb < Ra)
216 pred = Rb - err;227 pred = Rb - err;
217 else228 else
218 pred = Rb + err;229 pred = Rb + err;
@@ -220,31 +231,33 @@ static inline void ls_decode_line(JLSState *state, MJpegDecodeContext *s, void * · 1 unmodified line
220 } else { /* regular mode */231 } else { /* regular mode */
221 int context, sign;232 int context, sign;
222 233
223 context = ff_jpegls_quantize(state, D0) * 81 + ff_jpegls_quantize(state, D1) * 9 + ff_jpegls_quantize(state, D2);234 context = ff_jpegls_quantize(state, D0) * 81 +
224 pred = mid_pred(Ra, Ra + Rb - Rc, Rb);235 ff_jpegls_quantize(state, D1) * 9 +
236 ff_jpegls_quantize(state, D2);
237 pred = mid_pred(Ra, Ra + Rb - Rc, Rb);
225 238
226 if(context < 0){239 if (context < 0) {
227 context = -context;240 context = -context;
228 sign = 1;241 sign = 1;
229 }else{242 } else {
230 sign = 0;243 sign = 0;
231 }244 }
232 245
233 if(sign){246 if (sign) {
234 pred = av_clip(pred - state->C[context], 0, state->maxval);247 pred = av_clip(pred - state->C[context], 0, state->maxval);
235 err = -ls_get_code_regular(&s->gb, state, context);248 err = -ls_get_code_regular(&s->gb, state, context);
236 } else {249 } else {
237 pred = av_clip(pred + state->C[context], 0, state->maxval);250 pred = av_clip(pred + state->C[context], 0, state->maxval);
238 err = ls_get_code_regular(&s->gb, state, context);251 err = ls_get_code_regular(&s->gb, state, context);
239 }252 }
240 253
241 /* we have to do something more for near-lossless coding */254 /* we have to do something more for near-lossless coding */
242 pred += err;255 pred += err;
243 }256 }
244 if(state->near){257 if (state->near) {
245 if(pred < -state->near)258 if (pred < -state->near)
246 pred += state->range * state->twonear;259 pred += state->range * state->twonear;
247 else if(pred > state->maxval + state->near)260 else if (pred > state->maxval + state->near)
248 pred -= state->range * state->twonear;261 pred -= state->range * state->twonear;
249 pred = av_clip(pred, 0, state->maxval);262 pred = av_clip(pred, 0, state->maxval);
250 }263 }
@@ -255,7 +268,9 @@ static inline void ls_decode_line(JLSState *state, MJpegDecodeContext *s, void * · 4 unmodified lines
255 }268 }
256}269}
257 270
258int ff_jpegls_decode_picture(MJpegDecodeContext *s, int near, int point_transform, int ilv){271int ff_jpegls_decode_picture(MJpegDecodeContext *s, int near,
272 int point_transform, int ilv)
273{
259 int i, t = 0;274 int i, t = 0;
260 uint8_t *zero, *last, *cur;275 uint8_t *zero, *last, *cur;
261 JLSState *state;276 JLSState *state;
@@ -263,43 +278,45 @@ int ff_jpegls_decode_picture(MJpegDecodeContext *s, int near, int point_transfor · 1 unmodified line
263 278
264 zero = av_mallocz(s->picture_ptr->linesize[0]);279 zero = av_mallocz(s->picture_ptr->linesize[0]);
265 last = zero;280 last = zero;
266 cur = s->picture_ptr->data[0];281 cur = s->picture_ptr->data[0];
267 282
268 state = av_mallocz(sizeof(JLSState));283 state = av_mallocz(sizeof(JLSState));
269 /* initialize JPEG-LS state from JPEG parameters */284 /* initialize JPEG-LS state from JPEG parameters */
270 state->near = near;285 state->near = near;
271 state->bpp = (s->bits < 2) ? 2 : s->bits;286 state->bpp = (s->bits < 2) ? 2 : s->bits;
272 state->maxval = s->maxval;287 state->maxval = s->maxval;
273 state->T1 = s->t1;288 state->T1 = s->t1;
274 state->T2 = s->t2;289 state->T2 = s->t2;
275 state->T3 = s->t3;290 state->T3 = s->t3;
276 state->reset = s->reset;291 state->reset = s->reset;
277 ff_jpegls_reset_coding_parameters(state, 0);292 ff_jpegls_reset_coding_parameters(state, 0);
278 ff_jpegls_init_state(state);293 ff_jpegls_init_state(state);
279 294
280 if(s->bits <= 8)295 if (s->bits <= 8)
281 shift = point_transform + (8 - s->bits);296 shift = point_transform + (8 - s->bits);
282 else297 else
283 shift = point_transform + (16 - s->bits);298 shift = point_transform + (16 - s->bits);
284 299
285 av_dlog(s->avctx, "JPEG-LS params: %ix%i NEAR=%i MV=%i T(%i,%i,%i) RESET=%i, LIMIT=%i, qbpp=%i, RANGE=%i\n",300 av_dlog(s->avctx,
301 "JPEG-LS params: %ix%i NEAR=%i MV=%i T(%i,%i,%i) "
302 "RESET=%i, LIMIT=%i, qbpp=%i, RANGE=%i\n",
286 s->width, s->height, state->near, state->maxval,303 s->width, s->height, state->near, state->maxval,
287 state->T1, state->T2, state->T3,304 state->T1, state->T2, state->T3,
288 state->reset, state->limit, state->qbpp, state->range);305 state->reset, state->limit, state->qbpp, state->range);
289 av_dlog(s->avctx, "JPEG params: ILV=%i Pt=%i BPP=%i, scan = %i\n",306 av_dlog(s->avctx, "JPEG params: ILV=%i Pt=%i BPP=%i, scan = %i\n",
290 ilv, point_transform, s->bits, s->cur_scan);307 ilv, point_transform, s->bits, s->cur_scan);
291 if(ilv == 0) { /* separate planes */308 if (ilv == 0) { /* separate planes */
292 off = s->cur_scan - 1;309 off = s->cur_scan - 1;
293 stride = (s->nb_components > 1) ? 3 : 1;310 stride = (s->nb_components > 1) ? 3 : 1;
294 width = s->width * stride;311 width = s->width * stride;
295 cur += off;312 cur += off;
296 for(i = 0; i < s->height; i++) {313 for (i = 0; i < s->height; i++) {
297 if(s->bits <= 8){314 if (s->bits <= 8) {
298 ls_decode_line(state, s, last, cur, t, width, stride, off, 8);315 ls_decode_line(state, s, last, cur, t, width, stride, off, 8);
299 t = last[0];316 t = last[0];
300 }else{317 } else {
301 ls_decode_line(state, s, last, cur, t, width, stride, off, 16);318 ls_decode_line(state, s, last, cur, t, width, stride, off, 16);
302 t = *((uint16_t*)last);319 t = *((uint16_t *)last);
303 }320 }
304 last = cur;321 last = cur;
305 cur += s->picture_ptr->linesize[0];322 cur += s->picture_ptr->linesize[0];
@@ -309,14 +326,15 @@ int ff_jpegls_decode_picture(MJpegDecodeContext *s, int near, int point_transfor · 3 unmodified lines
309 skip_bits(&s->gb, 16); /* skip RSTn */326 skip_bits(&s->gb, 16); /* skip RSTn */
310 }327 }
311 }328 }
312 } else if(ilv == 1) { /* line interleaving */329 } else if (ilv == 1) { /* line interleaving */
313 int j;330 int j;
314 int Rc[3] = {0, 0, 0};331 int Rc[3] = { 0, 0, 0 };
315 memset(cur, 0, s->picture_ptr->linesize[0]);332 memset(cur, 0, s->picture_ptr->linesize[0]);
316 width = s->width * 3;333 width = s->width * 3;
317 for(i = 0; i < s->height; i++) {334 for (i = 0; i < s->height; i++) {
318 for(j = 0; j < 3; j++) {335 for (j = 0; j < 3; j++) {
319 ls_decode_line(state, s, last + j, cur + j, Rc[j], width, 3, j, 8);336 ls_decode_line(state, s, last + j, cur + j,
337 Rc[j], width, 3, j, 8);
320 Rc[j] = last[j];338 Rc[j] = last[j];
321 339
322 if (s->restart_interval && !--s->restart_count) {340 if (s->restart_interval && !--s->restart_count) {
@@ -327,35 +345,34 @@ int ff_jpegls_decode_picture(MJpegDecodeContext *s, int near, int point_transfor · 4 unmodified lines
327 last = cur;345 last = cur;
328 cur += s->picture_ptr->linesize[0];346 cur += s->picture_ptr->linesize[0];
329 }347 }
330 } else if(ilv == 2) { /* sample interleaving */348 } else if (ilv == 2) { /* sample interleaving */
331 av_log(s->avctx, AV_LOG_ERROR, "Sample interleaved images are not supported.\n");349 av_log(s->avctx, AV_LOG_ERROR,
350 "Sample interleaved images are not supported.\n");
332 av_free(state);351 av_free(state);
333 av_free(zero);352 av_free(zero);
334 return -1;353 return -1;
335 }354 }
336 355
337 if(shift){ /* we need to do point transform or normalize samples */356 if (shift) { /* we need to do point transform or normalize samples */
338 int x, w;357 int x, w;
339 358
340 w = s->width * s->nb_components;359 w = s->width * s->nb_components;
341 360
342 if(s->bits <= 8){361 if (s->bits <= 8) {
343 uint8_t *src = s->picture_ptr->data[0];362 uint8_t *src = s->picture_ptr->data[0];
344 363
345 for(i = 0; i < s->height; i++){364 for (i = 0; i < s->height; i++) {
346 for(x = off; x < w; x+= stride){365 for (x = off; x < w; x += stride)
347 src[x] <<= shift;366 src[x] <<= shift;
348 }
349 src += s->picture_ptr->linesize[0];367 src += s->picture_ptr->linesize[0];
350 }368 }
351 }else{369 } else {
352 uint16_t *src = (uint16_t*) s->picture_ptr->data[0];370 uint16_t *src = (uint16_t *)s->picture_ptr->data[0];
353 371
354 for(i = 0; i < s->height; i++){372 for (i = 0; i < s->height; i++) {
355 for(x = 0; x < w; x++){373 for (x = 0; x < w; x++)
356 src[x] <<= shift;374 src[x] <<= shift;
357 }375 src += s->picture_ptr->linesize[0] / 2;
358 src += s->picture_ptr->linesize[0]/2;
359 }376 }
360 }377 }
361 }378 }
@@ -365,9 +382,9 @@ int ff_jpegls_decode_picture(MJpegDecodeContext *s, int near, int point_transfor · 3 unmodified lines
365 return 0;382 return 0;
366}383}
367 384
368
369AVCodec ff_jpegls_decoder = {385AVCodec ff_jpegls_decoder = {
370 .name = "jpegls",386 .name = "jpegls",
387 .long_name = NULL_IF_CONFIG_SMALL("JPEG-LS"),
371 .type = AVMEDIA_TYPE_VIDEO,388 .type = AVMEDIA_TYPE_VIDEO,
372 .id = AV_CODEC_ID_JPEGLS,389 .id = AV_CODEC_ID_JPEGLS,
373 .priv_data_size = sizeof(MJpegDecodeContext),390 .priv_data_size = sizeof(MJpegDecodeContext),
@@ -375,5 +392,4 @@ AVCodec ff_jpegls_decoder = { · 1 unmodified line
375 .close = ff_mjpeg_decode_end,392 .close = ff_mjpeg_decode_end,
376 .decode = ff_mjpeg_decode_frame,393 .decode = ff_mjpeg_decode_frame,
377 .capabilities = CODEC_CAP_DR1,394 .capabilities = CODEC_CAP_DR1,
378 .long_name = NULL_IF_CONFIG_SMALL("JPEG-LS"),
379};395};
libavcodec/jpeglsdec.h+2−1
@@ -36,6 +36,7 @@ · 35 unmodified lines
36 */36 */
37int ff_jpegls_decode_lse(MJpegDecodeContext *s);37int ff_jpegls_decode_lse(MJpegDecodeContext *s);
38 38
39int ff_jpegls_decode_picture(MJpegDecodeContext *s, int near, int point_transform, int ilv);39int ff_jpegls_decode_picture(MJpegDecodeContext *s, int near,
40 int point_transform, int ilv);
40 41
41#endif /* AVCODEC_JPEGLSDEC_H */42#endif /* AVCODEC_JPEGLSDEC_H */
libavcodec/jpeglsenc.c+134−106
@@ -33,24 +33,26 @@ · 32 unmodified lines
33#include "mjpeg.h"33#include "mjpeg.h"
34#include "jpegls.h"34#include "jpegls.h"
35 35
36
37/**36/**
38 * Encode error from regular symbol37 * Encode error from regular symbol
39 */38 */
40static inline void ls_encode_regular(JLSState *state, PutBitContext *pb, int Q, int err){39static inline void ls_encode_regular(JLSState *state, PutBitContext *pb, int Q,
40 int err)
41{
41 int k;42 int k;
42 int val;43 int val;
43 int map;44 int map;
44 45
45 for(k = 0; (state->N[Q] << k) < state->A[Q]; k++);46 for (k = 0; (state->N[Q] << k) < state->A[Q]; k++)
47 ;
46 48
47 map = !state->near && !k && (2 * state->B[Q] <= -state->N[Q]);49 map = !state->near && !k && (2 * state->B[Q] <= -state->N[Q]);
48 50
49 if(err < 0)51 if (err < 0)
50 err += state->range;52 err += state->range;
51 if (err >= (state->range + 1 >> 1)) {53 if (err >= (state->range + 1 >> 1)) {
52 err -= state->range;54 err -= state->range;
53 val = 2 * FFABS(err) - 1 - map;55 val = 2 * FFABS(err) - 1 - map;
54 } else56 } else
55 val = 2 * err + map;57 val = 2 * err + map;
56 58
@@ -62,27 +64,30 @@ static inline void ls_encode_regular(JLSState *state, PutBitContext *pb, int Q, · 5 unmodified lines
62/**64/**
63 * Encode error from run termination65 * Encode error from run termination
64 */66 */
65static inline void ls_encode_runterm(JLSState *state, PutBitContext *pb, int RItype, int err, int limit_add){67static inline void ls_encode_runterm(JLSState *state, PutBitContext *pb,
68 int RItype, int err, int limit_add)
69{
66 int k;70 int k;
67 int val, map;71 int val, map;
68 int Q = 365 + RItype;72 int Q = 365 + RItype;
69 int temp;73 int temp;
70 74
71 temp = state->A[Q];75 temp = state->A[Q];
72 if(RItype)76 if (RItype)
73 temp += state->N[Q] >> 1;77 temp += state->N[Q] >> 1;
74 for(k = 0; (state->N[Q] << k) < temp; k++);78 for (k = 0; (state->N[Q] << k) < temp; k++)
79 ;
75 map = 0;80 map = 0;
76 if(!k && err && (2 * state->B[Q] < state->N[Q]))81 if (!k && err && (2 * state->B[Q] < state->N[Q]))
77 map = 1;82 map = 1;
78 83
79 if(err < 0)84 if (err < 0)
80 val = - (2 * err) - 1 - RItype + map;85 val = -(2 * err) - 1 - RItype + map;
81 else86 else
82 val = 2 * err - RItype - map;87 val = 2 * err - RItype - map;
83 set_ur_golomb_jpegls(pb, val, k, state->limit - limit_add - 1, state->qbpp);88 set_ur_golomb_jpegls(pb, val, k, state->limit - limit_add - 1, state->qbpp);
84 89
85 if(err < 0)90 if (err < 0)
86 state->B[Q]++;91 state->B[Q]++;
87 state->A[Q] += (val + 1 - RItype) >> 1;92 state->A[Q] += (val + 1 - RItype) >> 1;
88 93
@@ -92,19 +97,21 @@ static inline void ls_encode_runterm(JLSState *state, PutBitContext *pb, int RIt · 3 unmodified lines
92/**97/**
93 * Encode run value as specified by JPEG-LS standard98 * Encode run value as specified by JPEG-LS standard
94 */99 */
95static inline void ls_encode_run(JLSState *state, PutBitContext *pb, int run, int comp, int trail){100static inline void ls_encode_run(JLSState *state, PutBitContext *pb, int run,
96 while(run >= (1 << ff_log2_run[state->run_index[comp]])){101 int comp, int trail)
102{
103 while (run >= (1 << ff_log2_run[state->run_index[comp]])) {
97 put_bits(pb, 1, 1);104 put_bits(pb, 1, 1);
98 run -= 1 << ff_log2_run[state->run_index[comp]];105 run -= 1 << ff_log2_run[state->run_index[comp]];
99 if(state->run_index[comp] < 31)106 if (state->run_index[comp] < 31)
100 state->run_index[comp]++;107 state->run_index[comp]++;
101 }108 }
102 /* if hit EOL, encode another full run, else encode aborted run */109 /* if hit EOL, encode another full run, else encode aborted run */
103 if(!trail && run) {110 if (!trail && run) {
104 put_bits(pb, 1, 1);111 put_bits(pb, 1, 1);
105 }else if(trail){112 } else if (trail) {
106 put_bits(pb, 1, 0);113 put_bits(pb, 1, 0);
107 if(ff_log2_run[state->run_index[comp]])114 if (ff_log2_run[state->run_index[comp]])
108 put_bits(pb, ff_log2_run[state->run_index[comp]], run);115 put_bits(pb, ff_log2_run[state->run_index[comp]], run);
109 }116 }
110}117}
@@ -112,12 +119,15 @@ static inline void ls_encode_run(JLSState *state, PutBitContext *pb, int run, in · 1 unmodified line
112/**119/**
113 * Encode one line of image120 * Encode one line of image
114 */121 */
115static inline void ls_encode_line(JLSState *state, PutBitContext *pb, void *last, void *cur, int last2, int w, int stride, int comp, int bits){122static inline void ls_encode_line(JLSState *state, PutBitContext *pb,
123 void *last, void *cur, int last2, int w,
124 int stride, int comp, int bits)
125{
116 int x = 0;126 int x = 0;
117 int Ra, Rb, Rc, Rd;127 int Ra, Rb, Rc, Rd;
118 int D0, D1, D2;128 int D0, D1, D2;
119 129
120 while(x < w) {130 while (x < w) {
121 int err, pred, sign;131 int err, pred, sign;
122 132
123 /* compute gradients */133 /* compute gradients */
@@ -130,71 +140,76 @@ static inline void ls_encode_line(JLSState *state, PutBitContext *pb, void *last · 6 unmodified lines
130 D2 = Rc - Ra;140 D2 = Rc - Ra;
131 141
132 /* run mode */142 /* run mode */
133 if((FFABS(D0) <= state->near) && (FFABS(D1) <= state->near) && (FFABS(D2) <= state->near)) {143 if ((FFABS(D0) <= state->near) &&
144 (FFABS(D1) <= state->near) &&
145 (FFABS(D2) <= state->near)) {
134 int RUNval, RItype, run;146 int RUNval, RItype, run;
135 147
136 run = 0;148 run = 0;
137 RUNval = Ra;149 RUNval = Ra;
138 while(x < w && (FFABS(R(cur, x) - RUNval) <= state->near)){150 while (x < w && (FFABS(R(cur, x) - RUNval) <= state->near)) {
139 run++;151 run++;
140 W(cur, x, Ra);152 W(cur, x, Ra);
141 x += stride;153 x += stride;
142 }154 }
143 ls_encode_run(state, pb, run, comp, x < w);155 ls_encode_run(state, pb, run, comp, x < w);
144 if(x >= w)156 if (x >= w)
145 return;157 return;
146 Rb = R(last, x);158 Rb = R(last, x);
147 RItype = FFABS(Ra - Rb) <= state->near;159 RItype = FFABS(Ra - Rb) <= state->near;
148 pred = RItype ? Ra : Rb;160 pred = RItype ? Ra : Rb;
149 err = R(cur, x) - pred;161 err = R(cur, x) - pred;
150 162
151 if(!RItype && Ra > Rb)163 if (!RItype && Ra > Rb)
152 err = -err;164 err = -err;
153 165
154 if(state->near){166 if (state->near) {
155 if(err > 0)167 if (err > 0)
156 err = (state->near + err) / state->twonear;168 err = (state->near + err) / state->twonear;
157 else169 else
158 err = -(state->near - err) / state->twonear;170 err = -(state->near - err) / state->twonear;
159 171
160 if(RItype || (Rb >= Ra))172 if (RItype || (Rb >= Ra))
161 Ra = av_clip(pred + err * state->twonear, 0, state->maxval);173 Ra = av_clip(pred + err * state->twonear, 0, state->maxval);
162 else174 else
163 Ra = av_clip(pred - err * state->twonear, 0, state->maxval);175 Ra = av_clip(pred - err * state->twonear, 0, state->maxval);
164 W(cur, x, Ra);176 W(cur, x, Ra);
165 }177 }
166 if(err < 0)178 if (err < 0)
167 err += state->range;179 err += state->range;
168 if (err >= state->range + 1 >> 1)180 if (err >= state->range + 1 >> 1)
169 err -= state->range;181 err -= state->range;
170 182
171 ls_encode_runterm(state, pb, RItype, err, ff_log2_run[state->run_index[comp]]);183 ls_encode_runterm(state, pb, RItype, err,
184 ff_log2_run[state->run_index[comp]]);
172 185
173 if(state->run_index[comp] > 0)186 if (state->run_index[comp] > 0)
174 state->run_index[comp]--;187 state->run_index[comp]--;
175 } else { /* regular mode */188 } else { /* regular mode */
176 int context;189 int context;
177 190
178 context = ff_jpegls_quantize(state, D0) * 81 + ff_jpegls_quantize(state, D1) * 9 + ff_jpegls_quantize(state, D2);191 context = ff_jpegls_quantize(state, D0) * 81 +
179 pred = mid_pred(Ra, Ra + Rb - Rc, Rb);192 ff_jpegls_quantize(state, D1) * 9 +
193 ff_jpegls_quantize(state, D2);
194 pred = mid_pred(Ra, Ra + Rb - Rc, Rb);
180 195
181 if(context < 0){196 if (context < 0) {
182 context = -context;197 context = -context;
183 sign = 1;198 sign = 1;
184 pred = av_clip(pred - state->C[context], 0, state->maxval);199 pred = av_clip(pred - state->C[context], 0, state->maxval);
185 err = pred - R(cur, x);200 err = pred - R(cur, x);
186 }else{201 } else {
187 sign = 0;202 sign = 0;
188 pred = av_clip(pred + state->C[context], 0, state->maxval);203 pred = av_clip(pred + state->C[context], 0, state->maxval);
189 err = R(cur, x) - pred;204 err = R(cur, x) - pred;
190 }205 }
191 206
192 if(state->near){207 if (state->near) {
193 if(err > 0)208 if (err > 0)
194 err = (state->near + err) / state->twonear;209 err = (state->near + err) / state->twonear;
195 else210 else
196 err = -(state->near - err) / state->twonear;211 err = -(state->near - err) / state->twonear;
197 if(!sign)212 if (!sign)
198 Ra = av_clip(pred + err * state->twonear, 0, state->maxval);213 Ra = av_clip(pred + err * state->twonear, 0, state->maxval);
199 else214 else
200 Ra = av_clip(pred - err * state->twonear, 0, state->maxval);215 Ra = av_clip(pred - err * state->twonear, 0, state->maxval);
@@ -207,18 +222,22 @@ static inline void ls_encode_line(JLSState *state, PutBitContext *pb, void *last · 6 unmodified lines
207 }222 }
208}223}
209 224
210static void ls_store_lse(JLSState *state, PutBitContext *pb){225static void ls_store_lse(JLSState *state, PutBitContext *pb)
226{
211 /* Test if we have default params and don't need to store LSE */227 /* Test if we have default params and don't need to store LSE */
212 JLSState state2 = { 0 };228 JLSState state2 = { 0 };
213 state2.bpp = state->bpp;229 state2.bpp = state->bpp;
214 state2.near = state->near;230 state2.near = state->near;
215 ff_jpegls_reset_coding_parameters(&state2, 1);231 ff_jpegls_reset_coding_parameters(&state2, 1);
216 if(state->T1 == state2.T1 && state->T2 == state2.T2 && state->T3 == state2.T3 && state->reset == state2.reset)232 if (state->T1 == state2.T1 &&
233 state->T2 == state2.T2 &&
234 state->T3 == state2.T3 &&
235 state->reset == state2.reset)
217 return;236 return;
218 /* store LSE type 1 */237 /* store LSE type 1 */
219 put_marker(pb, LSE);238 put_marker(pb, LSE);
220 put_bits(pb, 16, 13);239 put_bits(pb, 16, 13);
221 put_bits(pb, 8, 1);240 put_bits(pb, 8, 1);
222 put_bits(pb, 16, state->maxval);241 put_bits(pb, 16, state->maxval);
223 put_bits(pb, 16, state->T1);242 put_bits(pb, 16, state->T1);
224 put_bits(pb, 16, state->T2);243 put_bits(pb, 16, state->T2);
@@ -229,9 +248,9 @@ static void ls_store_lse(JLSState *state, PutBitContext *pb){ · 4 unmodified lines
229static int encode_picture_ls(AVCodecContext *avctx, AVPacket *pkt,248static int encode_picture_ls(AVCodecContext *avctx, AVPacket *pkt,
230 const AVFrame *pict, int *got_packet)249 const AVFrame *pict, int *got_packet)
231{250{
232 JpeglsContext * const s = avctx->priv_data;251 JpeglsContext *const s = avctx->priv_data;
233 AVFrame * const p = &s->picture;252 AVFrame *const p = &s->picture;
234 const int near = avctx->prediction_method;253 const int near = avctx->prediction_method;
235 PutBitContext pb, pb2;254 PutBitContext pb, pb2;
236 GetBitContext gb;255 GetBitContext gb;
237 uint8_t *buf2, *zero, *cur, *last;256 uint8_t *buf2, *zero, *cur, *last;
@@ -239,17 +258,18 @@ static int encode_picture_ls(AVCodecContext *avctx, AVPacket *pkt, · 1 unmodified line
239 int i, size, ret;258 int i, size, ret;
240 int comps;259 int comps;
241 260
242 *p = *pict;261 *p = *pict;
243 p->pict_type= AV_PICTURE_TYPE_I;262 p->pict_type = AV_PICTURE_TYPE_I;
244 p->key_frame= 1;263 p->key_frame = 1;
245 264
246 if(avctx->pix_fmt == AV_PIX_FMT_GRAY8 || avctx->pix_fmt == AV_PIX_FMT_GRAY16)265 if (avctx->pix_fmt == AV_PIX_FMT_GRAY8 ||
266 avctx->pix_fmt == AV_PIX_FMT_GRAY16)
247 comps = 1;267 comps = 1;
248 else268 else
249 comps = 3;269 comps = 3;
250 270
251 if ((ret = ff_alloc_packet(pkt, avctx->width*avctx->height*comps*4 +271 if ((ret = ff_alloc_packet(pkt, avctx->width * avctx->height * comps * 4 +
252 FF_MIN_BUFFER_SIZE)) < 0) {272 FF_MIN_BUFFER_SIZE)) < 0) {
253 av_log(avctx, AV_LOG_ERROR, "Error getting output packet.\n");273 av_log(avctx, AV_LOG_ERROR, "Error getting output packet.\n");
254 return ret;274 return ret;
255 }275 }
@@ -263,31 +283,31 @@ static int encode_picture_ls(AVCodecContext *avctx, AVPacket *pkt, · 7 unmodified lines
263 put_marker(&pb, SOI);283 put_marker(&pb, SOI);
264 put_marker(&pb, SOF48);284 put_marker(&pb, SOF48);
265 put_bits(&pb, 16, 8 + comps * 3); // header size depends on components285 put_bits(&pb, 16, 8 + comps * 3); // header size depends on components
266 put_bits(&pb, 8, (avctx->pix_fmt == AV_PIX_FMT_GRAY16) ? 16 : 8); // bpp286 put_bits(&pb, 8, (avctx->pix_fmt == AV_PIX_FMT_GRAY16) ? 16 : 8); // bpp
267 put_bits(&pb, 16, avctx->height);287 put_bits(&pb, 16, avctx->height);
268 put_bits(&pb, 16, avctx->width);288 put_bits(&pb, 16, avctx->width);
269 put_bits(&pb, 8, comps); // components289 put_bits(&pb, 8, comps); // components
270 for(i = 1; i <= comps; i++) {290 for (i = 1; i <= comps; i++) {
271 put_bits(&pb, 8, i); // component ID291 put_bits(&pb, 8, i); // component ID
272 put_bits(&pb, 8, 0x11); // subsampling: none292 put_bits(&pb, 8, 0x11); // subsampling: none
273 put_bits(&pb, 8, 0); // Tiq, used by JPEG-LS ext293 put_bits(&pb, 8, 0); // Tiq, used by JPEG-LS ext
274 }294 }
275 295
276 put_marker(&pb, SOS);296 put_marker(&pb, SOS);
277 put_bits(&pb, 16, 6 + comps * 2);297 put_bits(&pb, 16, 6 + comps * 2);
278 put_bits(&pb, 8, comps);298 put_bits(&pb, 8, comps);
279 for(i = 1; i <= comps; i++) {299 for (i = 1; i <= comps; i++) {
280 put_bits(&pb, 8, i); // component ID300 put_bits(&pb, 8, i); // component ID
281 put_bits(&pb, 8, 0); // mapping index: none301 put_bits(&pb, 8, 0); // mapping index: none
282 }302 }
283 put_bits(&pb, 8, near);303 put_bits(&pb, 8, near);
284 put_bits(&pb, 8, (comps > 1) ? 1 : 0); // interleaving: 0 - plane, 1 - line304 put_bits(&pb, 8, (comps > 1) ? 1 : 0); // interleaving: 0 - plane, 1 - line
285 put_bits(&pb, 8, 0); // point transform: none305 put_bits(&pb, 8, 0); // point transform: none
286 306
287 state = av_mallocz(sizeof(JLSState));307 state = av_mallocz(sizeof(JLSState));
288 /* initialize JPEG-LS state from JPEG parameters */308 /* initialize JPEG-LS state from JPEG parameters */
289 state->near = near;309 state->near = near;
290 state->bpp = (avctx->pix_fmt == AV_PIX_FMT_GRAY16) ? 16 : 8;310 state->bpp = (avctx->pix_fmt == AV_PIX_FMT_GRAY16) ? 16 : 8;
291 ff_jpegls_reset_coding_parameters(state, 0);311 ff_jpegls_reset_coding_parameters(state, 0);
292 ff_jpegls_init_state(state);312 ff_jpegls_init_state(state);
293 313
@@ -295,46 +315,48 @@ static int encode_picture_ls(AVCodecContext *avctx, AVPacket *pkt, · 1 unmodified line
295 315
296 zero = av_mallocz(p->linesize[0]);316 zero = av_mallocz(p->linesize[0]);
297 last = zero;317 last = zero;
298 cur = p->data[0];318 cur = p->data[0];
299 if(avctx->pix_fmt == AV_PIX_FMT_GRAY8){319 if (avctx->pix_fmt == AV_PIX_FMT_GRAY8) {
300 int t = 0;320 int t = 0;
301 321
302 for(i = 0; i < avctx->height; i++) {322 for (i = 0; i < avctx->height; i++) {
303 ls_encode_line(state, &pb2, last, cur, t, avctx->width, 1, 0, 8);323 ls_encode_line(state, &pb2, last, cur, t, avctx->width, 1, 0, 8);
304 t = last[0];324 t = last[0];
305 last = cur;325 last = cur;
306 cur += p->linesize[0];326 cur += p->linesize[0];
307 }327 }
308 }else if(avctx->pix_fmt == AV_PIX_FMT_GRAY16){328 } else if (avctx->pix_fmt == AV_PIX_FMT_GRAY16) {
309 int t = 0;329 int t = 0;
310 330
311 for(i = 0; i < avctx->height; i++) {331 for (i = 0; i < avctx->height; i++) {
312 ls_encode_line(state, &pb2, last, cur, t, avctx->width, 1, 0, 16);332 ls_encode_line(state, &pb2, last, cur, t, avctx->width, 1, 0, 16);
313 t = *((uint16_t*)last);333 t = *((uint16_t *)last);
314 last = cur;334 last = cur;
315 cur += p->linesize[0];335 cur += p->linesize[0];
316 }336 }
317 }else if(avctx->pix_fmt == AV_PIX_FMT_RGB24){337 } else if (avctx->pix_fmt == AV_PIX_FMT_RGB24) {
318 int j, width;338 int j, width;
319 int Rc[3] = {0, 0, 0};339 int Rc[3] = { 0, 0, 0 };
320 340
321 width = avctx->width * 3;341 width = avctx->width * 3;
322 for(i = 0; i < avctx->height; i++) {342 for (i = 0; i < avctx->height; i++) {
323 for(j = 0; j < 3; j++) {343 for (j = 0; j < 3; j++) {
324 ls_encode_line(state, &pb2, last + j, cur + j, Rc[j], width, 3, j, 8);344 ls_encode_line(state, &pb2, last + j, cur + j, Rc[j],
345 width, 3, j, 8);
325 Rc[j] = last[j];346 Rc[j] = last[j];
326 }347 }
327 last = cur;348 last = cur;
328 cur += s->picture.linesize[0];349 cur += s->picture.linesize[0];
329 }350 }
330 }else if(avctx->pix_fmt == AV_PIX_FMT_BGR24){351 } else if (avctx->pix_fmt == AV_PIX_FMT_BGR24) {
331 int j, width;352 int j, width;
332 int Rc[3] = {0, 0, 0};353 int Rc[3] = { 0, 0, 0 };
333 354
334 width = avctx->width * 3;355 width = avctx->width * 3;
335 for(i = 0; i < avctx->height; i++) {356 for (i = 0; i < avctx->height; i++) {
336 for(j = 2; j >= 0; j--) {357 for (j = 2; j >= 0; j--) {
337 ls_encode_line(state, &pb2, last + j, cur + j, Rc[j], width, 3, j, 8);358 ls_encode_line(state, &pb2, last + j, cur + j, Rc[j],
359 width, 3, j, 8);
338 Rc[j] = last[j];360 Rc[j] = last[j];
339 }361 }
340 last = cur;362 last = cur;
@@ -345,20 +367,20 @@ static int encode_picture_ls(AVCodecContext *avctx, AVPacket *pkt, · 4 unmodified lines
345 av_free(zero);367 av_free(zero);
346 av_free(state);368 av_free(state);
347 369
348 // the specification says that after doing 0xff escaping unused bits in the370 /* the specification says that after doing 0xff escaping unused bits in
349 // last byte must be set to 0, so just append 7 "optional" zero-bits to371 * the last byte must be set to 0, so just append 7 "optional" zero-bits
350 // avoid special-casing.372 * to avoid special-casing. */
351 put_bits(&pb2, 7, 0);373 put_bits(&pb2, 7, 0);
352 size = put_bits_count(&pb2);374 size = put_bits_count(&pb2);
353 flush_put_bits(&pb2);375 flush_put_bits(&pb2);
354 /* do escape coding */376 /* do escape coding */
355 init_get_bits(&gb, buf2, size);377 init_get_bits(&gb, buf2, size);
356 size -= 7;378 size -= 7;
357 while(get_bits_count(&gb) < size){379 while (get_bits_count(&gb) < size) {
358 int v;380 int v;
359 v = get_bits(&gb, 8);381 v = get_bits(&gb, 8);
360 put_bits(&pb, 8, v);382 put_bits(&pb, 8, v);
361 if(v == 0xFF){383 if (v == 0xFF) {
362 v = get_bits(&gb, 7);384 v = get_bits(&gb, 7);
363 put_bits(&pb, 8, v);385 put_bits(&pb, 8, v);
364 }386 }
@@ -378,29 +400,35 @@ static int encode_picture_ls(AVCodecContext *avctx, AVPacket *pkt, · 13 unmodified lines
378 return 0;400 return 0;
379}401}
380 402
381static av_cold int encode_init_ls(AVCodecContext *ctx) {403static av_cold int encode_init_ls(AVCodecContext *ctx)
382 JpeglsContext *c = (JpeglsContext*)ctx->priv_data;404{
405 JpeglsContext *c = (JpeglsContext *)ctx->priv_data;
383 406
384 c->avctx = ctx;407 c->avctx = ctx;
385 ctx->coded_frame = &c->picture;408 ctx->coded_frame = &c->picture;
386 409
387 if(ctx->pix_fmt != AV_PIX_FMT_GRAY8 && ctx->pix_fmt != AV_PIX_FMT_GRAY16 && ctx->pix_fmt != AV_PIX_FMT_RGB24 && ctx->pix_fmt != AV_PIX_FMT_BGR24){410 if (ctx->pix_fmt != AV_PIX_FMT_GRAY8 &&
388 av_log(ctx, AV_LOG_ERROR, "Only grayscale and RGB24/BGR24 images are supported\n");411 ctx->pix_fmt != AV_PIX_FMT_GRAY16 &&
412 ctx->pix_fmt != AV_PIX_FMT_RGB24 &&
413 ctx->pix_fmt != AV_PIX_FMT_BGR24) {
414 av_log(ctx, AV_LOG_ERROR,
415 "Only grayscale and RGB24/BGR24 images are supported\n");
389 return -1;416 return -1;
390 }417 }
391 return 0;418 return 0;
392}419}
393 420
394AVCodec ff_jpegls_encoder = { //FIXME avoid MPV_* lossless JPEG should not need them421AVCodec ff_jpegls_encoder = {
395 .name = "jpegls",422 .name = "jpegls",
423 .long_name = NULL_IF_CONFIG_SMALL("JPEG-LS"),
396 .type = AVMEDIA_TYPE_VIDEO,424 .type = AVMEDIA_TYPE_VIDEO,
397 .id = AV_CODEC_ID_JPEGLS,425 .id = AV_CODEC_ID_JPEGLS,
398 .priv_data_size = sizeof(JpeglsContext),426 .priv_data_size = sizeof(JpeglsContext),
399 .init = encode_init_ls,427 .init = encode_init_ls,
400 .encode2 = encode_picture_ls,428 .encode2 = encode_picture_ls,
401 .pix_fmts = (const enum AVPixelFormat[]){429 .pix_fmts = (const enum AVPixelFormat[]) {
402 AV_PIX_FMT_BGR24, AV_PIX_FMT_RGB24, AV_PIX_FMT_GRAY8, AV_PIX_FMT_GRAY16,430 AV_PIX_FMT_BGR24, AV_PIX_FMT_RGB24,
431 AV_PIX_FMT_GRAY8, AV_PIX_FMT_GRAY16,
403 AV_PIX_FMT_NONE432 AV_PIX_FMT_NONE
404 },433 },
405 .long_name = NULL_IF_CONFIG_SMALL("JPEG-LS"),
406};434};

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t