00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00028 #include "avcodec.h"
00029 #include "get_bits.h"
00030 #include "golomb.h"
00031 #include "mathops.h"
00032 #include "mjpeg.h"
00033 #include "mjpegdec.h"
00034 #include "jpegls.h"
00035 #include "jpeglsdec.h"
00036
00037
00038
00039
00040
00041
00042
00043
00044
00045
00046
00047
00048
00052 int ff_jpegls_decode_lse(MJpegDecodeContext *s)
00053 {
00054 int len, id;
00055
00056
00057 len = get_bits(&s->gb, 16);
00058 id = get_bits(&s->gb, 8);
00059
00060 switch(id){
00061 case 1:
00062 s->maxval= get_bits(&s->gb, 16);
00063 s->t1= get_bits(&s->gb, 16);
00064 s->t2= get_bits(&s->gb, 16);
00065 s->t3= get_bits(&s->gb, 16);
00066 s->reset= get_bits(&s->gb, 16);
00067
00068
00069
00070 break;
00071 case 2:
00072 case 3:
00073 av_log(s->avctx, AV_LOG_ERROR, "palette not supported\n");
00074 return -1;
00075 case 4:
00076 av_log(s->avctx, AV_LOG_ERROR, "oversize image not supported\n");
00077 return -1;
00078 default:
00079 av_log(s->avctx, AV_LOG_ERROR, "invalid id %d\n", id);
00080 return -1;
00081 }
00082
00083
00084 return 0;
00085 }
00086
00090 static inline int ls_get_code_regular(GetBitContext *gb, JLSState *state, int Q){
00091 int k, ret;
00092
00093 for(k = 0; (state->N[Q] << k) < state->A[Q]; k++);
00094
00095 #ifdef JLS_BROKEN
00096 if(!show_bits_long(gb, 32))return -1;
00097 #endif
00098 ret = get_ur_golomb_jpegls(gb, k, state->limit, state->qbpp);
00099
00100
00101 if(ret & 1)
00102 ret = -((ret + 1) >> 1);
00103 else
00104 ret >>= 1;
00105
00106
00107 if(!state->near && !k && (2 * state->B[Q] <= -state->N[Q]))
00108 ret = -(ret + 1);
00109
00110 ret= ff_jpegls_update_state_regular(state, Q, ret);
00111
00112 return ret;
00113 }
00114
00118 static inline int ls_get_code_runterm(GetBitContext *gb, JLSState *state, int RItype, int limit_add){
00119 int k, ret, temp, map;
00120 int Q = 365 + RItype;
00121
00122 temp= state->A[Q];
00123 if(RItype)
00124 temp += state->N[Q] >> 1;
00125
00126 for(k = 0; (state->N[Q] << k) < temp; k++);
00127
00128 #ifdef JLS_BROKEN
00129 if(!show_bits_long(gb, 32))return -1;
00130 #endif
00131 ret = get_ur_golomb_jpegls(gb, k, state->limit - limit_add - 1, state->qbpp);
00132
00133
00134 map = 0;
00135 if(!k && (RItype || ret) && (2 * state->B[Q] < state->N[Q]))
00136 map = 1;
00137 ret += RItype + map;
00138
00139 if(ret & 1){
00140 ret = map - ((ret + 1) >> 1);
00141 state->B[Q]++;
00142 } else {
00143 ret = ret >> 1;
00144 }
00145
00146
00147 state->A[Q] += FFABS(ret) - RItype;
00148 ret *= state->twonear;
00149 ff_jpegls_downscale_state(state, Q);
00150
00151 return ret;
00152 }
00153
00157 static inline void ls_decode_line(JLSState *state, MJpegDecodeContext *s, void *last, void *dst, int last2, int w, int stride, int comp, int bits){
00158 int i, x = 0;
00159 int Ra, Rb, Rc, Rd;
00160 int D0, D1, D2;
00161
00162 while(x < w) {
00163 int err, pred;
00164
00165
00166 Ra = x ? R(dst, x - stride) : R(last, x);
00167 Rb = R(last, x);
00168 Rc = x ? R(last, x - stride) : last2;
00169 Rd = (x >= w - stride) ? R(last, x) : R(last, x + stride);
00170 D0 = Rd - Rb;
00171 D1 = Rb - Rc;
00172 D2 = Rc - Ra;
00173
00174 if((FFABS(D0) <= state->near) && (FFABS(D1) <= state->near) && (FFABS(D2) <= state->near)) {
00175 int r;
00176 int RItype;
00177
00178
00179 while(get_bits1(&s->gb)) {
00180 int r;
00181 r = 1 << ff_log2_run[state->run_index[comp]];
00182 if(x + r * stride > w) {
00183 r = (w - x) / stride;
00184 }
00185 for(i = 0; i < r; i++) {
00186 W(dst, x, Ra);
00187 x += stride;
00188 }
00189
00190 if(r != (1 << ff_log2_run[state->run_index[comp]]))
00191 return;
00192 if(state->run_index[comp] < 31)
00193 state->run_index[comp]++;
00194 if(x + stride > w)
00195 return;
00196 }
00197
00198 r = ff_log2_run[state->run_index[comp]];
00199 if(r)
00200 r = get_bits_long(&s->gb, r);
00201 if(x + r * stride > w) {
00202 r = (w - x) / stride;
00203 }
00204 for(i = 0; i < r; i++) {
00205 W(dst, x, Ra);
00206 x += stride;
00207 }
00208
00209
00210 Rb = R(last, x);
00211 RItype = (FFABS(Ra - Rb) <= state->near) ? 1 : 0;
00212 err = ls_get_code_runterm(&s->gb, state, RItype, ff_log2_run[state->run_index[comp]]);
00213 if(state->run_index[comp])
00214 state->run_index[comp]--;
00215
00216 if(state->near && RItype){
00217 pred = Ra + err;
00218 } else {
00219 if(Rb < Ra)
00220 pred = Rb - err;
00221 else
00222 pred = Rb + err;
00223 }
00224 } else {
00225 int context, sign;
00226
00227 context = ff_jpegls_quantize(state, D0) * 81 + ff_jpegls_quantize(state, D1) * 9 + ff_jpegls_quantize(state, D2);
00228 pred = mid_pred(Ra, Ra + Rb - Rc, Rb);
00229
00230 if(context < 0){
00231 context = -context;
00232 sign = 1;
00233 }else{
00234 sign = 0;
00235 }
00236
00237 if(sign){
00238 pred = av_clip(pred - state->C[context], 0, state->maxval);
00239 err = -ls_get_code_regular(&s->gb, state, context);
00240 } else {
00241 pred = av_clip(pred + state->C[context], 0, state->maxval);
00242 err = ls_get_code_regular(&s->gb, state, context);
00243 }
00244
00245
00246 pred += err;
00247 }
00248 if(state->near){
00249 if(pred < -state->near)
00250 pred += state->range * state->twonear;
00251 else if(pred > state->maxval + state->near)
00252 pred -= state->range * state->twonear;
00253 pred = av_clip(pred, 0, state->maxval);
00254 }
00255
00256 pred &= state->maxval;
00257 W(dst, x, pred);
00258 x += stride;
00259 }
00260 }
00261
00262 int ff_jpegls_decode_picture(MJpegDecodeContext *s, int near, int point_transform, int ilv){
00263 int i, t = 0;
00264 uint8_t *zero, *last, *cur;
00265 JLSState *state;
00266 int off = 0, stride = 1, width, shift;
00267
00268 zero = av_mallocz(s->picture.linesize[0]);
00269 last = zero;
00270 cur = s->picture.data[0];
00271
00272 state = av_mallocz(sizeof(JLSState));
00273
00274 state->near = near;
00275 state->bpp = (s->bits < 2) ? 2 : s->bits;
00276 state->maxval = s->maxval;
00277 state->T1 = s->t1;
00278 state->T2 = s->t2;
00279 state->T3 = s->t3;
00280 state->reset = s->reset;
00281 ff_jpegls_reset_coding_parameters(state, 0);
00282 ff_jpegls_init_state(state);
00283
00284 if(s->bits <= 8)
00285 shift = point_transform + (8 - s->bits);
00286 else
00287 shift = point_transform + (16 - s->bits);
00288
00289
00290
00291 if(ilv == 0) {
00292 stride = (s->nb_components > 1) ? 3 : 1;
00293 off = av_clip(s->cur_scan - 1, 0, stride - 1);
00294 width = s->width * stride;
00295 cur += off;
00296 for(i = 0; i < s->height; i++) {
00297 if(s->bits <= 8){
00298 ls_decode_line(state, s, last, cur, t, width, stride, off, 8);
00299 t = last[0];
00300 }else{
00301 ls_decode_line(state, s, last, cur, t, width, stride, off, 16);
00302 t = *((uint16_t*)last);
00303 }
00304 last = cur;
00305 cur += s->picture.linesize[0];
00306
00307 if (s->restart_interval && !--s->restart_count) {
00308 align_get_bits(&s->gb);
00309 skip_bits(&s->gb, 16);
00310 }
00311 }
00312 } else if(ilv == 1) {
00313 int j;
00314 int Rc[3] = {0, 0, 0};
00315 stride = (s->nb_components > 1) ? 3 : 1;
00316 memset(cur, 0, s->picture.linesize[0]);
00317 width = s->width * stride;
00318 for(i = 0; i < s->height; i++) {
00319 for(j = 0; j < stride; j++) {
00320 ls_decode_line(state, s, last + j, cur + j, Rc[j], width, stride, j, 8);
00321 Rc[j] = last[j];
00322
00323 if (s->restart_interval && !--s->restart_count) {
00324 align_get_bits(&s->gb);
00325 skip_bits(&s->gb, 16);
00326 }
00327 }
00328 last = cur;
00329 cur += s->picture.linesize[0];
00330 }
00331 } else if(ilv == 2) {
00332 av_log(s->avctx, AV_LOG_ERROR, "Sample interleaved images are not supported.\n");
00333 av_free(state);
00334 av_free(zero);
00335 return -1;
00336 }
00337
00338 if(shift){
00339 int x, w;
00340
00341 w = s->width * s->nb_components;
00342
00343 if(s->bits <= 8){
00344 uint8_t *src = s->picture.data[0];
00345
00346 for(i = 0; i < s->height; i++){
00347 for(x = off; x < w; x+= stride){
00348 src[x] <<= shift;
00349 }
00350 src += s->picture.linesize[0];
00351 }
00352 }else{
00353 uint16_t *src = (uint16_t*) s->picture.data[0];
00354
00355 for(i = 0; i < s->height; i++){
00356 for(x = 0; x < w; x++){
00357 src[x] <<= shift;
00358 }
00359 src += s->picture.linesize[0]/2;
00360 }
00361 }
00362 }
00363 av_free(state);
00364 av_free(zero);
00365
00366 return 0;
00367 }
00368
00369
00370 AVCodec ff_jpegls_decoder = {
00371 .name = "jpegls",
00372 .type = AVMEDIA_TYPE_VIDEO,
00373 .id = CODEC_ID_JPEGLS,
00374 .priv_data_size = sizeof(MJpegDecodeContext),
00375 .init = ff_mjpeg_decode_init,
00376 .close = ff_mjpeg_decode_end,
00377 .decode = ff_mjpeg_decode_frame,
00378 .capabilities = CODEC_CAP_DR1,
00379 .long_name = NULL_IF_CONFIG_SMALL("JPEG-LS"),
00380 };