Use FFABS instead of abs.
[FFMpeg-mirror/ordered_chapters.git] / libavcodec / jpeglsenc.c
blobd8e8bda763203742168286fa60c6ea00efddba11
1 /*
2 * JPEG-LS encoder
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
23 /**
24 * @file jpeglsenc.c
25 * JPEG-LS encoder.
28 #include "avcodec.h"
29 #include "bitstream.h"
30 #include "golomb.h"
31 #include "mjpeg.h"
32 #include "jpegls.h"
35 /**
36 * Encode error from regular symbol
38 static inline void ls_encode_regular(JLSState *state, PutBitContext *pb, int Q, int err){
39 int k;
40 int val;
41 int map;
43 for(k = 0; (state->N[Q] << k) < state->A[Q]; k++);
45 map = !state->near && !k && (2 * state->B[Q] <= -state->N[Q]);
47 if(err < 0)
48 err += state->range;
49 if(err >= ((state->range + 1) >> 1)) {
50 err -= state->range;
51 val = 2 * FFABS(err) - 1 - map;
52 } else
53 val = 2 * err + map;
55 set_ur_golomb_jpegls(pb, val, k, state->limit, state->qbpp);
57 ff_jpegls_update_state_regular(state, Q, err);
60 /**
61 * Encode error from run termination
63 static inline void ls_encode_runterm(JLSState *state, PutBitContext *pb, int RItype, int err, int limit_add){
64 int k;
65 int val, map;
66 int Q = 365 + RItype;
67 int temp;
69 temp = state->A[Q];
70 if(RItype)
71 temp += state->N[Q] >> 1;
72 for(k = 0; (state->N[Q] << k) < temp; k++);
73 map = 0;
74 if(!k && err && (2 * state->B[Q] < state->N[Q]))
75 map = 1;
77 if(err < 0)
78 val = - (2 * err) - 1 - RItype + map;
79 else
80 val = 2 * err - RItype - map;
81 set_ur_golomb_jpegls(pb, val, k, state->limit - limit_add - 1, state->qbpp);
83 if(err < 0)
84 state->B[Q]++;
85 state->A[Q] += (val + 1 - RItype) >> 1;
87 ff_jpegls_downscale_state(state, Q);
90 /**
91 * Encode run value as specified by JPEG-LS standard
93 static inline void ls_encode_run(JLSState *state, PutBitContext *pb, int run, int comp, int trail){
94 while(run >= (1 << ff_log2_run[state->run_index[comp]])){
95 put_bits(pb, 1, 1);
96 run -= 1 << ff_log2_run[state->run_index[comp]];
97 if(state->run_index[comp] < 31)
98 state->run_index[comp]++;
100 /* if hit EOL, encode another full run, else encode aborted run */
101 if(!trail && run) {
102 put_bits(pb, 1, 1);
103 }else if(trail){
104 put_bits(pb, 1, 0);
105 if(ff_log2_run[state->run_index[comp]])
106 put_bits(pb, ff_log2_run[state->run_index[comp]], run);
111 * Encode one line of image
113 static inline void ls_encode_line(JLSState *state, PutBitContext *pb, void *last, void *cur, int last2, int w, int stride, int comp, int bits){
114 int x = 0;
115 int Ra, Rb, Rc, Rd;
116 int D0, D1, D2;
118 while(x < w) {
119 int err, pred, sign;
121 /* compute gradients */
122 Ra = x ? R(cur, x - stride) : R(last, x);
123 Rb = R(last, x);
124 Rc = x ? R(last, x - stride) : last2;
125 Rd = (x >= w - stride) ? R(last, x) : R(last, x + stride);
126 D0 = Rd - Rb;
127 D1 = Rb - Rc;
128 D2 = Rc - Ra;
130 /* run mode */
131 if((FFABS(D0) <= state->near) && (FFABS(D1) <= state->near) && (FFABS(D2) <= state->near)) {
132 int RUNval, RItype, run;
134 run = 0;
135 RUNval = Ra;
136 while(x < w && (FFABS(R(cur, x) - RUNval) <= state->near)){
137 run++;
138 W(cur, x, Ra);
139 x += stride;
141 ls_encode_run(state, pb, run, comp, x < w);
142 if(x >= w)
143 return;
144 Rb = R(last, x);
145 RItype = (FFABS(Ra - Rb) <= state->near);
146 pred = RItype ? Ra : Rb;
147 err = R(cur, x) - pred;
149 if(!RItype && Ra > Rb)
150 err = -err;
152 if(state->near){
153 if(err > 0)
154 err = (state->near + err) / state->twonear;
155 else
156 err = -(state->near - err) / state->twonear;
158 if(RItype || (Rb >= Ra))
159 Ra = av_clip(pred + err * state->twonear, 0, state->maxval);
160 else
161 Ra = av_clip(pred - err * state->twonear, 0, state->maxval);
162 W(cur, x, Ra);
164 if(err < 0)
165 err += state->range;
166 if(err >= ((state->range + 1) >> 1))
167 err -= state->range;
169 ls_encode_runterm(state, pb, RItype, err, ff_log2_run[state->run_index[comp]]);
171 if(state->run_index[comp] > 0)
172 state->run_index[comp]--;
173 } else { /* regular mode */
174 int context;
176 context = ff_jpegls_quantize(state, D0) * 81 + ff_jpegls_quantize(state, D1) * 9 + ff_jpegls_quantize(state, D2);
177 pred = mid_pred(Ra, Ra + Rb - Rc, Rb);
179 if(context < 0){
180 context = -context;
181 sign = 1;
182 pred = av_clip(pred - state->C[context], 0, state->maxval);
183 err = pred - R(cur, x);
184 }else{
185 sign = 0;
186 pred = av_clip(pred + state->C[context], 0, state->maxval);
187 err = R(cur, x) - pred;
190 if(state->near){
191 if(err > 0)
192 err = (state->near + err) / state->twonear;
193 else
194 err = -(state->near - err) / state->twonear;
195 if(!sign)
196 Ra = av_clip(pred + err * state->twonear, 0, state->maxval);
197 else
198 Ra = av_clip(pred - err * state->twonear, 0, state->maxval);
199 W(cur, x, Ra);
202 ls_encode_regular(state, pb, context, err);
204 x += stride;
208 static void ls_store_lse(JLSState *state, PutBitContext *pb){
209 /* Test if we have default params and don't need to store LSE */
210 JLSState state2;
211 memset(&state2, 0, sizeof(JLSState));
212 state2.bpp = state->bpp;
213 state2.near = state->near;
214 ff_jpegls_reset_coding_parameters(&state2, 1);
215 if(state->T1 == state2.T1 && state->T2 == state2.T2 && state->T3 == state2.T3 && state->reset == state2.reset)
216 return;
217 /* store LSE type 1 */
218 put_marker(pb, LSE);
219 put_bits(pb, 16, 13);
220 put_bits(pb, 8, 1);
221 put_bits(pb, 16, state->maxval);
222 put_bits(pb, 16, state->T1);
223 put_bits(pb, 16, state->T2);
224 put_bits(pb, 16, state->T3);
225 put_bits(pb, 16, state->reset);
228 static int encode_picture_ls(AVCodecContext *avctx, unsigned char *buf, int buf_size, void *data){
229 JpeglsContext * const s = avctx->priv_data;
230 AVFrame *pict = data;
231 AVFrame * const p= (AVFrame*)&s->picture;
232 const int near = avctx->prediction_method;
233 PutBitContext pb, pb2;
234 GetBitContext gb;
235 uint8_t *buf2, *zero, *cur, *last;
236 JLSState *state;
237 int i, size;
238 int comps;
240 buf2 = av_malloc(buf_size);
242 init_put_bits(&pb, buf, buf_size);
243 init_put_bits(&pb2, buf2, buf_size);
245 *p = *pict;
246 p->pict_type= FF_I_TYPE;
247 p->key_frame= 1;
249 if(avctx->pix_fmt == PIX_FMT_GRAY8 || avctx->pix_fmt == PIX_FMT_GRAY16)
250 comps = 1;
251 else
252 comps = 3;
254 /* write our own JPEG header, can't use mjpeg_picture_header */
255 put_marker(&pb, SOI);
256 put_marker(&pb, SOF48);
257 put_bits(&pb, 16, 8 + comps * 3); // header size depends on components
258 put_bits(&pb, 8, (avctx->pix_fmt == PIX_FMT_GRAY16) ? 16 : 8); // bpp
259 put_bits(&pb, 16, avctx->height);
260 put_bits(&pb, 16, avctx->width);
261 put_bits(&pb, 8, comps); // components
262 for(i = 1; i <= comps; i++) {
263 put_bits(&pb, 8, i); // component ID
264 put_bits(&pb, 8, 0x11); // subsampling: none
265 put_bits(&pb, 8, 0); // Tiq, used by JPEG-LS ext
268 put_marker(&pb, SOS);
269 put_bits(&pb, 16, 6 + comps * 2);
270 put_bits(&pb, 8, comps);
271 for(i = 1; i <= comps; i++) {
272 put_bits(&pb, 8, i); // component ID
273 put_bits(&pb, 8, 0); // mapping index: none
275 put_bits(&pb, 8, near);
276 put_bits(&pb, 8, (comps > 1) ? 1 : 0); // interleaving: 0 - plane, 1 - line
277 put_bits(&pb, 8, 0); // point transform: none
279 state = av_mallocz(sizeof(JLSState));
280 /* initialize JPEG-LS state from JPEG parameters */
281 state->near = near;
282 state->bpp = (avctx->pix_fmt == PIX_FMT_GRAY16) ? 16 : 8;
283 ff_jpegls_reset_coding_parameters(state, 0);
284 ff_jpegls_init_state(state);
286 ls_store_lse(state, &pb);
288 zero = av_mallocz(p->linesize[0]);
289 last = zero;
290 cur = p->data[0];
291 if(avctx->pix_fmt == PIX_FMT_GRAY8){
292 int t = 0;
294 for(i = 0; i < avctx->height; i++) {
295 ls_encode_line(state, &pb2, last, cur, t, avctx->width, 1, 0, 8);
296 t = last[0];
297 last = cur;
298 cur += p->linesize[0];
300 }else if(avctx->pix_fmt == PIX_FMT_GRAY16){
301 int t = 0;
303 for(i = 0; i < avctx->height; i++) {
304 ls_encode_line(state, &pb2, last, cur, t, avctx->width, 1, 0, 16);
305 t = *((uint16_t*)last);
306 last = cur;
307 cur += p->linesize[0];
309 }else if(avctx->pix_fmt == PIX_FMT_RGB24){
310 int j, width;
311 int Rc[3] = {0, 0, 0};
313 width = avctx->width * 3;
314 for(i = 0; i < avctx->height; i++) {
315 for(j = 0; j < 3; j++) {
316 ls_encode_line(state, &pb2, last + j, cur + j, Rc[j], width, 3, j, 8);
317 Rc[j] = last[j];
319 last = cur;
320 cur += s->picture.linesize[0];
322 }else if(avctx->pix_fmt == PIX_FMT_BGR24){
323 int j, width;
324 int Rc[3] = {0, 0, 0};
326 width = avctx->width * 3;
327 for(i = 0; i < avctx->height; i++) {
328 for(j = 2; j >= 0; j--) {
329 ls_encode_line(state, &pb2, last + j, cur + j, Rc[j], width, 3, j, 8);
330 Rc[j] = last[j];
332 last = cur;
333 cur += s->picture.linesize[0];
337 av_free(zero);
338 av_free(state);
340 // the specification says that after doing 0xff escaping unused bits in the
341 // last byte must be set to 0, so just append 7 "optional" zero-bits to
342 // avoid special-casing.
343 put_bits(&pb2, 7, 0);
344 size = put_bits_count(&pb2);
345 flush_put_bits(&pb2);
346 /* do escape coding */
347 init_get_bits(&gb, buf2, size);
348 size -= 7;
349 while(get_bits_count(&gb) < size){
350 int v;
351 v = get_bits(&gb, 8);
352 put_bits(&pb, 8, v);
353 if(v == 0xFF){
354 v = get_bits(&gb, 7);
355 put_bits(&pb, 8, v);
358 align_put_bits(&pb);
359 av_free(buf2);
361 /* End of image */
362 put_marker(&pb, EOI);
363 flush_put_bits(&pb);
365 emms_c();
367 return put_bits_count(&pb) >> 3;
370 static int encode_init_ls(AVCodecContext *ctx) {
371 JpeglsContext *c = (JpeglsContext*)ctx->priv_data;
373 c->avctx = ctx;
374 ctx->coded_frame = &c->picture;
376 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){
377 av_log(ctx, AV_LOG_ERROR, "Only grayscale and RGB24/BGR24 images are supported\n");
378 return -1;
380 return 0;
383 AVCodec jpegls_encoder = { //FIXME avoid MPV_* lossless jpeg shouldnt need them
384 "jpegls",
385 CODEC_TYPE_VIDEO,
386 CODEC_ID_JPEGLS,
387 sizeof(JpeglsContext),
388 encode_init_ls,
389 encode_picture_ls,
390 NULL,
391 .pix_fmts= (enum PixelFormat[]){PIX_FMT_BGR24, PIX_FMT_RGB24, PIX_FMT_GRAY8, PIX_FMT_GRAY16, -1},