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 "internal.h"
00032 #include "mathops.h"
00033 #include "dsputil.h"
00034 #include "mjpeg.h"
00035 #include "jpegls.h"
00036
00037
00041 static inline void ls_encode_regular(JLSState *state, PutBitContext *pb, int Q, int err){
00042 int k;
00043 int val;
00044 int map;
00045
00046 for(k = 0; (state->N[Q] << k) < state->A[Q]; k++);
00047
00048 map = !state->near && !k && (2 * state->B[Q] <= -state->N[Q]);
00049
00050 if(err < 0)
00051 err += state->range;
00052 if(err >= ((state->range + 1) >> 1)) {
00053 err -= state->range;
00054 val = 2 * FFABS(err) - 1 - map;
00055 } else
00056 val = 2 * err + map;
00057
00058 set_ur_golomb_jpegls(pb, val, k, state->limit, state->qbpp);
00059
00060 ff_jpegls_update_state_regular(state, Q, err);
00061 }
00062
00066 static inline void ls_encode_runterm(JLSState *state, PutBitContext *pb, int RItype, int err, int limit_add){
00067 int k;
00068 int val, map;
00069 int Q = 365 + RItype;
00070 int temp;
00071
00072 temp = state->A[Q];
00073 if(RItype)
00074 temp += state->N[Q] >> 1;
00075 for(k = 0; (state->N[Q] << k) < temp; k++);
00076 map = 0;
00077 if(!k && err && (2 * state->B[Q] < state->N[Q]))
00078 map = 1;
00079
00080 if(err < 0)
00081 val = - (2 * err) - 1 - RItype + map;
00082 else
00083 val = 2 * err - RItype - map;
00084 set_ur_golomb_jpegls(pb, val, k, state->limit - limit_add - 1, state->qbpp);
00085
00086 if(err < 0)
00087 state->B[Q]++;
00088 state->A[Q] += (val + 1 - RItype) >> 1;
00089
00090 ff_jpegls_downscale_state(state, Q);
00091 }
00092
00096 static inline void ls_encode_run(JLSState *state, PutBitContext *pb, int run, int comp, int trail){
00097 while(run >= (1 << ff_log2_run[state->run_index[comp]])){
00098 put_bits(pb, 1, 1);
00099 run -= 1 << ff_log2_run[state->run_index[comp]];
00100 if(state->run_index[comp] < 31)
00101 state->run_index[comp]++;
00102 }
00103
00104 if(!trail && run) {
00105 put_bits(pb, 1, 1);
00106 }else if(trail){
00107 put_bits(pb, 1, 0);
00108 if(ff_log2_run[state->run_index[comp]])
00109 put_bits(pb, ff_log2_run[state->run_index[comp]], run);
00110 }
00111 }
00112
00116 static inline void ls_encode_line(JLSState *state, PutBitContext *pb, void *last, void *cur, int last2, int w, int stride, int comp, int bits){
00117 int x = 0;
00118 int Ra, Rb, Rc, Rd;
00119 int D0, D1, D2;
00120
00121 while(x < w) {
00122 int err, pred, sign;
00123
00124
00125 Ra = x ? R(cur, x - stride) : R(last, x);
00126 Rb = R(last, x);
00127 Rc = x ? R(last, x - stride) : last2;
00128 Rd = (x >= w - stride) ? R(last, x) : R(last, x + stride);
00129 D0 = Rd - Rb;
00130 D1 = Rb - Rc;
00131 D2 = Rc - Ra;
00132
00133
00134 if((FFABS(D0) <= state->near) && (FFABS(D1) <= state->near) && (FFABS(D2) <= state->near)) {
00135 int RUNval, RItype, run;
00136
00137 run = 0;
00138 RUNval = Ra;
00139 while(x < w && (FFABS(R(cur, x) - RUNval) <= state->near)){
00140 run++;
00141 W(cur, x, Ra);
00142 x += stride;
00143 }
00144 ls_encode_run(state, pb, run, comp, x < w);
00145 if(x >= w)
00146 return;
00147 Rb = R(last, x);
00148 RItype = (FFABS(Ra - Rb) <= state->near);
00149 pred = RItype ? Ra : Rb;
00150 err = R(cur, x) - pred;
00151
00152 if(!RItype && Ra > Rb)
00153 err = -err;
00154
00155 if(state->near){
00156 if(err > 0)
00157 err = (state->near + err) / state->twonear;
00158 else
00159 err = -(state->near - err) / state->twonear;
00160
00161 if(RItype || (Rb >= Ra))
00162 Ra = av_clip(pred + err * state->twonear, 0, state->maxval);
00163 else
00164 Ra = av_clip(pred - err * state->twonear, 0, state->maxval);
00165 W(cur, x, Ra);
00166 }
00167 if(err < 0)
00168 err += state->range;
00169 if(err >= ((state->range + 1) >> 1))
00170 err -= state->range;
00171
00172 ls_encode_runterm(state, pb, RItype, err, ff_log2_run[state->run_index[comp]]);
00173
00174 if(state->run_index[comp] > 0)
00175 state->run_index[comp]--;
00176 } else {
00177 int context;
00178
00179 context = ff_jpegls_quantize(state, D0) * 81 + ff_jpegls_quantize(state, D1) * 9 + ff_jpegls_quantize(state, D2);
00180 pred = mid_pred(Ra, Ra + Rb - Rc, Rb);
00181
00182 if(context < 0){
00183 context = -context;
00184 sign = 1;
00185 pred = av_clip(pred - state->C[context], 0, state->maxval);
00186 err = pred - R(cur, x);
00187 }else{
00188 sign = 0;
00189 pred = av_clip(pred + state->C[context], 0, state->maxval);
00190 err = R(cur, x) - pred;
00191 }
00192
00193 if(state->near){
00194 if(err > 0)
00195 err = (state->near + err) / state->twonear;
00196 else
00197 err = -(state->near - err) / state->twonear;
00198 if(!sign)
00199 Ra = av_clip(pred + err * state->twonear, 0, state->maxval);
00200 else
00201 Ra = av_clip(pred - err * state->twonear, 0, state->maxval);
00202 W(cur, x, Ra);
00203 }
00204
00205 ls_encode_regular(state, pb, context, err);
00206 }
00207 x += stride;
00208 }
00209 }
00210
00211 static void ls_store_lse(JLSState *state, PutBitContext *pb){
00212
00213 JLSState state2 = { 0 };
00214 state2.bpp = state->bpp;
00215 state2.near = state->near;
00216 ff_jpegls_reset_coding_parameters(&state2, 1);
00217 if(state->T1 == state2.T1 && state->T2 == state2.T2 && state->T3 == state2.T3 && state->reset == state2.reset)
00218 return;
00219
00220 put_marker(pb, LSE);
00221 put_bits(pb, 16, 13);
00222 put_bits(pb, 8, 1);
00223 put_bits(pb, 16, state->maxval);
00224 put_bits(pb, 16, state->T1);
00225 put_bits(pb, 16, state->T2);
00226 put_bits(pb, 16, state->T3);
00227 put_bits(pb, 16, state->reset);
00228 }
00229
00230 static int encode_picture_ls(AVCodecContext *avctx, AVPacket *pkt,
00231 const AVFrame *pict, int *got_packet)
00232 {
00233 JpeglsContext * const s = avctx->priv_data;
00234 AVFrame * const p = &s->picture;
00235 const int near = avctx->prediction_method;
00236 PutBitContext pb, pb2;
00237 GetBitContext gb;
00238 uint8_t *buf2, *zero, *cur, *last;
00239 JLSState *state;
00240 int i, size, ret;
00241 int comps;
00242
00243 *p = *pict;
00244 p->pict_type= AV_PICTURE_TYPE_I;
00245 p->key_frame= 1;
00246
00247 if(avctx->pix_fmt == AV_PIX_FMT_GRAY8 || avctx->pix_fmt == AV_PIX_FMT_GRAY16)
00248 comps = 1;
00249 else
00250 comps = 3;
00251
00252 if ((ret = ff_alloc_packet2(avctx, pkt, avctx->width*avctx->height*comps*4 +
00253 FF_MIN_BUFFER_SIZE)) < 0)
00254 return ret;
00255
00256 buf2 = av_malloc(pkt->size);
00257
00258 init_put_bits(&pb, pkt->data, pkt->size);
00259 init_put_bits(&pb2, buf2, pkt->size);
00260
00261
00262 put_marker(&pb, SOI);
00263 put_marker(&pb, SOF48);
00264 put_bits(&pb, 16, 8 + comps * 3);
00265 put_bits(&pb, 8, (avctx->pix_fmt == AV_PIX_FMT_GRAY16) ? 16 : 8);
00266 put_bits(&pb, 16, avctx->height);
00267 put_bits(&pb, 16, avctx->width);
00268 put_bits(&pb, 8, comps);
00269 for(i = 1; i <= comps; i++) {
00270 put_bits(&pb, 8, i);
00271 put_bits(&pb, 8, 0x11);
00272 put_bits(&pb, 8, 0);
00273 }
00274
00275 put_marker(&pb, SOS);
00276 put_bits(&pb, 16, 6 + comps * 2);
00277 put_bits(&pb, 8, comps);
00278 for(i = 1; i <= comps; i++) {
00279 put_bits(&pb, 8, i);
00280 put_bits(&pb, 8, 0);
00281 }
00282 put_bits(&pb, 8, near);
00283 put_bits(&pb, 8, (comps > 1) ? 1 : 0);
00284 put_bits(&pb, 8, 0);
00285
00286 state = av_mallocz(sizeof(JLSState));
00287
00288 state->near = near;
00289 state->bpp = (avctx->pix_fmt == AV_PIX_FMT_GRAY16) ? 16 : 8;
00290 ff_jpegls_reset_coding_parameters(state, 0);
00291 ff_jpegls_init_state(state);
00292
00293 ls_store_lse(state, &pb);
00294
00295 zero = av_mallocz(FFABS(p->linesize[0]));
00296 if (!zero) {
00297 av_free(state);
00298 return AVERROR(ENOMEM);
00299 }
00300 last = zero;
00301 cur = p->data[0];
00302 if(avctx->pix_fmt == AV_PIX_FMT_GRAY8){
00303 int t = 0;
00304
00305 for(i = 0; i < avctx->height; i++) {
00306 ls_encode_line(state, &pb2, last, cur, t, avctx->width, 1, 0, 8);
00307 t = last[0];
00308 last = cur;
00309 cur += p->linesize[0];
00310 }
00311 }else if(avctx->pix_fmt == AV_PIX_FMT_GRAY16){
00312 int t = 0;
00313
00314 for(i = 0; i < avctx->height; i++) {
00315 ls_encode_line(state, &pb2, last, cur, t, avctx->width, 1, 0, 16);
00316 t = *((uint16_t*)last);
00317 last = cur;
00318 cur += p->linesize[0];
00319 }
00320 }else if(avctx->pix_fmt == AV_PIX_FMT_RGB24){
00321 int j, width;
00322 int Rc[3] = {0, 0, 0};
00323
00324 width = avctx->width * 3;
00325 for(i = 0; i < avctx->height; i++) {
00326 for(j = 0; j < 3; j++) {
00327 ls_encode_line(state, &pb2, last + j, cur + j, Rc[j], width, 3, j, 8);
00328 Rc[j] = last[j];
00329 }
00330 last = cur;
00331 cur += s->picture.linesize[0];
00332 }
00333 }else if(avctx->pix_fmt == AV_PIX_FMT_BGR24){
00334 int j, width;
00335 int Rc[3] = {0, 0, 0};
00336
00337 width = avctx->width * 3;
00338 for(i = 0; i < avctx->height; i++) {
00339 for(j = 2; j >= 0; j--) {
00340 ls_encode_line(state, &pb2, last + j, cur + j, Rc[j], width, 3, j, 8);
00341 Rc[j] = last[j];
00342 }
00343 last = cur;
00344 cur += s->picture.linesize[0];
00345 }
00346 }
00347
00348 av_freep(&zero);
00349 av_freep(&state);
00350
00351
00352
00353
00354 put_bits(&pb2, 7, 0);
00355 size = put_bits_count(&pb2);
00356 flush_put_bits(&pb2);
00357
00358 init_get_bits(&gb, buf2, size);
00359 size -= 7;
00360 while(get_bits_count(&gb) < size){
00361 int v;
00362 v = get_bits(&gb, 8);
00363 put_bits(&pb, 8, v);
00364 if(v == 0xFF){
00365 v = get_bits(&gb, 7);
00366 put_bits(&pb, 8, v);
00367 }
00368 }
00369 avpriv_align_put_bits(&pb);
00370 av_free(buf2);
00371
00372
00373 put_marker(&pb, EOI);
00374 flush_put_bits(&pb);
00375
00376 emms_c();
00377
00378 pkt->size = put_bits_count(&pb) >> 3;
00379 pkt->flags |= AV_PKT_FLAG_KEY;
00380 *got_packet = 1;
00381 return 0;
00382 }
00383
00384 static av_cold int encode_init_ls(AVCodecContext *ctx) {
00385 JpeglsContext *c = (JpeglsContext*)ctx->priv_data;
00386
00387 c->avctx = ctx;
00388 ctx->coded_frame = &c->picture;
00389
00390 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){
00391 av_log(ctx, AV_LOG_ERROR, "Only grayscale and RGB24/BGR24 images are supported\n");
00392 return -1;
00393 }
00394 return 0;
00395 }
00396
00397 AVCodec ff_jpegls_encoder = {
00398 .name = "jpegls",
00399 .type = AVMEDIA_TYPE_VIDEO,
00400 .id = AV_CODEC_ID_JPEGLS,
00401 .priv_data_size = sizeof(JpeglsContext),
00402 .init = encode_init_ls,
00403 .encode2 = encode_picture_ls,
00404 .pix_fmts = (const enum AVPixelFormat[]){
00405 AV_PIX_FMT_BGR24, AV_PIX_FMT_RGB24, AV_PIX_FMT_GRAY8, AV_PIX_FMT_GRAY16,
00406 AV_PIX_FMT_NONE
00407 },
00408 .long_name = NULL_IF_CONFIG_SMALL("JPEG-LS"),
00409 };