Mercurial > libavcodec.hg
annotate jpeglsenc.c @ 9971:4b6f16da6652 libavcodec
Be sure to increment our position in the coefficient array when skipping a zero
band in the twoloop scalefactor search.
author | alexc |
---|---|
date | Wed, 22 Jul 2009 03:53:30 +0000 |
parents | 0dce4fe6e6f3 |
children | 38cfe222e1a4 |
rev | line source |
---|---|
5003 | 1 /* |
2 * JPEG-LS encoder | |
3 * Copyright (c) 2003 Michael Niedermayer | |
4 * Copyright (c) 2006 Konstantin Shishkov | |
5 * | |
6 * This file is part of FFmpeg. | |
7 * | |
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. | |
12 * | |
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. | |
17 * | |
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 | |
21 */ | |
22 | |
23 /** | |
8718
e9d9d946f213
Use full internal pathname in doxygen @file directives.
diego
parents:
8627
diff
changeset
|
24 * @file libavcodec/jpeglsenc.c |
5003 | 25 * JPEG-LS encoder. |
26 */ | |
27 | |
28 #include "avcodec.h" | |
9428 | 29 #include "get_bits.h" |
5003 | 30 #include "golomb.h" |
8627
d6bab465b82c
moves mid_pred() into mathops.h (with arch specific code split by directory)
aurel
parents:
7040
diff
changeset
|
31 #include "mathops.h" |
5045
f0e079daad30
move MJpegDecodeContext declaration from mjpeg.h to mjpegdec.h
aurel
parents:
5003
diff
changeset
|
32 #include "dsputil.h" |
5003 | 33 #include "mjpeg.h" |
34 #include "jpegls.h" | |
35 | |
36 | |
37 /** | |
38 * Encode error from regular symbol | |
39 */ | |
40 static inline void ls_encode_regular(JLSState *state, PutBitContext *pb, int Q, int err){ | |
41 int k; | |
42 int val; | |
43 int map; | |
44 | |
45 for(k = 0; (state->N[Q] << k) < state->A[Q]; k++); | |
46 | |
47 map = !state->near && !k && (2 * state->B[Q] <= -state->N[Q]); | |
48 | |
49 if(err < 0) | |
50 err += state->range; | |
51 if(err >= ((state->range + 1) >> 1)) { | |
52 err -= state->range; | |
53 val = 2 * FFABS(err) - 1 - map; | |
54 } else | |
55 val = 2 * err + map; | |
56 | |
57 set_ur_golomb_jpegls(pb, val, k, state->limit, state->qbpp); | |
58 | |
59 ff_jpegls_update_state_regular(state, Q, err); | |
60 } | |
61 | |
62 /** | |
63 * Encode error from run termination | |
64 */ | |
65 static inline void ls_encode_runterm(JLSState *state, PutBitContext *pb, int RItype, int err, int limit_add){ | |
66 int k; | |
67 int val, map; | |
68 int Q = 365 + RItype; | |
69 int temp; | |
70 | |
71 temp = state->A[Q]; | |
72 if(RItype) | |
73 temp += state->N[Q] >> 1; | |
74 for(k = 0; (state->N[Q] << k) < temp; k++); | |
75 map = 0; | |
76 if(!k && err && (2 * state->B[Q] < state->N[Q])) | |
77 map = 1; | |
78 | |
79 if(err < 0) | |
80 val = - (2 * err) - 1 - RItype + map; | |
81 else | |
82 val = 2 * err - RItype - map; | |
83 set_ur_golomb_jpegls(pb, val, k, state->limit - limit_add - 1, state->qbpp); | |
84 | |
85 if(err < 0) | |
86 state->B[Q]++; | |
87 state->A[Q] += (val + 1 - RItype) >> 1; | |
88 | |
89 ff_jpegls_downscale_state(state, Q); | |
90 } | |
91 | |
92 /** | |
93 * Encode run value as specified by JPEG-LS standard | |
94 */ | |
95 static inline void ls_encode_run(JLSState *state, PutBitContext *pb, int run, int comp, int trail){ | |
96 while(run >= (1 << ff_log2_run[state->run_index[comp]])){ | |
97 put_bits(pb, 1, 1); | |
98 run -= 1 << ff_log2_run[state->run_index[comp]]; | |
99 if(state->run_index[comp] < 31) | |
100 state->run_index[comp]++; | |
101 } | |
102 /* if hit EOL, encode another full run, else encode aborted run */ | |
103 if(!trail && run) { | |
104 put_bits(pb, 1, 1); | |
105 }else if(trail){ | |
106 put_bits(pb, 1, 0); | |
107 if(ff_log2_run[state->run_index[comp]]) | |
108 put_bits(pb, ff_log2_run[state->run_index[comp]], run); | |
109 } | |
110 } | |
111 | |
112 /** | |
113 * Encode one line of image | |
114 */ | |
115 static inline void ls_encode_line(JLSState *state, PutBitContext *pb, void *last, void *cur, int last2, int w, int stride, int comp, int bits){ | |
116 int x = 0; | |
117 int Ra, Rb, Rc, Rd; | |
118 int D0, D1, D2; | |
119 | |
120 while(x < w) { | |
121 int err, pred, sign; | |
122 | |
123 /* compute gradients */ | |
124 Ra = x ? R(cur, x - stride) : R(last, x); | |
125 Rb = R(last, x); | |
126 Rc = x ? R(last, x - stride) : last2; | |
127 Rd = (x >= w - stride) ? R(last, x) : R(last, x + stride); | |
128 D0 = Rd - Rb; | |
129 D1 = Rb - Rc; | |
130 D2 = Rc - Ra; | |
131 | |
132 /* run mode */ | |
133 if((FFABS(D0) <= state->near) && (FFABS(D1) <= state->near) && (FFABS(D2) <= state->near)) { | |
134 int RUNval, RItype, run; | |
135 | |
136 run = 0; | |
137 RUNval = Ra; | |
138 while(x < w && (FFABS(R(cur, x) - RUNval) <= state->near)){ | |
139 run++; | |
140 W(cur, x, Ra); | |
141 x += stride; | |
142 } | |
143 ls_encode_run(state, pb, run, comp, x < w); | |
144 if(x >= w) | |
145 return; | |
146 Rb = R(last, x); | |
147 RItype = (FFABS(Ra - Rb) <= state->near); | |
148 pred = RItype ? Ra : Rb; | |
149 err = R(cur, x) - pred; | |
150 | |
151 if(!RItype && Ra > Rb) | |
152 err = -err; | |
153 | |
154 if(state->near){ | |
155 if(err > 0) | |
156 err = (state->near + err) / state->twonear; | |
157 else | |
158 err = -(state->near - err) / state->twonear; | |
159 | |
160 if(RItype || (Rb >= Ra)) | |
161 Ra = av_clip(pred + err * state->twonear, 0, state->maxval); | |
162 else | |
163 Ra = av_clip(pred - err * state->twonear, 0, state->maxval); | |
164 W(cur, x, Ra); | |
165 } | |
166 if(err < 0) | |
167 err += state->range; | |
168 if(err >= ((state->range + 1) >> 1)) | |
169 err -= state->range; | |
170 | |
171 ls_encode_runterm(state, pb, RItype, err, ff_log2_run[state->run_index[comp]]); | |
172 | |
173 if(state->run_index[comp] > 0) | |
174 state->run_index[comp]--; | |
175 } else { /* regular mode */ | |
176 int context; | |
177 | |
178 context = ff_jpegls_quantize(state, D0) * 81 + ff_jpegls_quantize(state, D1) * 9 + ff_jpegls_quantize(state, D2); | |
179 pred = mid_pred(Ra, Ra + Rb - Rc, Rb); | |
180 | |
181 if(context < 0){ | |
182 context = -context; | |
183 sign = 1; | |
184 pred = av_clip(pred - state->C[context], 0, state->maxval); | |
185 err = pred - R(cur, x); | |
186 }else{ | |
187 sign = 0; | |
188 pred = av_clip(pred + state->C[context], 0, state->maxval); | |
189 err = R(cur, x) - pred; | |
190 } | |
191 | |
192 if(state->near){ | |
193 if(err > 0) | |
194 err = (state->near + err) / state->twonear; | |
195 else | |
196 err = -(state->near - err) / state->twonear; | |
197 if(!sign) | |
198 Ra = av_clip(pred + err * state->twonear, 0, state->maxval); | |
199 else | |
200 Ra = av_clip(pred - err * state->twonear, 0, state->maxval); | |
201 W(cur, x, Ra); | |
202 } | |
203 | |
204 ls_encode_regular(state, pb, context, err); | |
205 } | |
206 x += stride; | |
207 } | |
208 } | |
209 | |
210 static void ls_store_lse(JLSState *state, PutBitContext *pb){ | |
211 /* Test if we have default params and don't need to store LSE */ | |
212 JLSState state2; | |
213 memset(&state2, 0, sizeof(JLSState)); | |
214 state2.bpp = state->bpp; | |
215 state2.near = state->near; | |
216 ff_jpegls_reset_coding_parameters(&state2, 1); | |
217 if(state->T1 == state2.T1 && state->T2 == state2.T2 && state->T3 == state2.T3 && state->reset == state2.reset) | |
218 return; | |
219 /* store LSE type 1 */ | |
220 put_marker(pb, LSE); | |
221 put_bits(pb, 16, 13); | |
222 put_bits(pb, 8, 1); | |
223 put_bits(pb, 16, state->maxval); | |
224 put_bits(pb, 16, state->T1); | |
225 put_bits(pb, 16, state->T2); | |
226 put_bits(pb, 16, state->T3); | |
227 put_bits(pb, 16, state->reset); | |
228 } | |
229 | |
230 static int encode_picture_ls(AVCodecContext *avctx, unsigned char *buf, int buf_size, void *data){ | |
231 JpeglsContext * const s = avctx->priv_data; | |
232 AVFrame *pict = data; | |
233 AVFrame * const p= (AVFrame*)&s->picture; | |
234 const int near = avctx->prediction_method; | |
235 PutBitContext pb, pb2; | |
236 GetBitContext gb; | |
237 uint8_t *buf2, *zero, *cur, *last; | |
238 JLSState *state; | |
239 int i, size; | |
240 int comps; | |
241 | |
242 buf2 = av_malloc(buf_size); | |
243 | |
244 init_put_bits(&pb, buf, buf_size); | |
245 init_put_bits(&pb2, buf2, buf_size); | |
246 | |
247 *p = *pict; | |
248 p->pict_type= FF_I_TYPE; | |
249 p->key_frame= 1; | |
250 | |
251 if(avctx->pix_fmt == PIX_FMT_GRAY8 || avctx->pix_fmt == PIX_FMT_GRAY16) | |
252 comps = 1; | |
253 else | |
254 comps = 3; | |
255 | |
256 /* write our own JPEG header, can't use mjpeg_picture_header */ | |
257 put_marker(&pb, SOI); | |
258 put_marker(&pb, SOF48); | |
259 put_bits(&pb, 16, 8 + comps * 3); // header size depends on components | |
260 put_bits(&pb, 8, (avctx->pix_fmt == PIX_FMT_GRAY16) ? 16 : 8); // bpp | |
261 put_bits(&pb, 16, avctx->height); | |
262 put_bits(&pb, 16, avctx->width); | |
263 put_bits(&pb, 8, comps); // components | |
264 for(i = 1; i <= comps; i++) { | |
265 put_bits(&pb, 8, i); // component ID | |
266 put_bits(&pb, 8, 0x11); // subsampling: none | |
267 put_bits(&pb, 8, 0); // Tiq, used by JPEG-LS ext | |
268 } | |
269 | |
270 put_marker(&pb, SOS); | |
271 put_bits(&pb, 16, 6 + comps * 2); | |
272 put_bits(&pb, 8, comps); | |
273 for(i = 1; i <= comps; i++) { | |
274 put_bits(&pb, 8, i); // component ID | |
275 put_bits(&pb, 8, 0); // mapping index: none | |
276 } | |
277 put_bits(&pb, 8, near); | |
278 put_bits(&pb, 8, (comps > 1) ? 1 : 0); // interleaving: 0 - plane, 1 - line | |
279 put_bits(&pb, 8, 0); // point transform: none | |
280 | |
281 state = av_mallocz(sizeof(JLSState)); | |
282 /* initialize JPEG-LS state from JPEG parameters */ | |
283 state->near = near; | |
284 state->bpp = (avctx->pix_fmt == PIX_FMT_GRAY16) ? 16 : 8; | |
285 ff_jpegls_reset_coding_parameters(state, 0); | |
286 ff_jpegls_init_state(state); | |
287 | |
288 ls_store_lse(state, &pb); | |
289 | |
290 zero = av_mallocz(p->linesize[0]); | |
291 last = zero; | |
292 cur = p->data[0]; | |
293 if(avctx->pix_fmt == PIX_FMT_GRAY8){ | |
294 int t = 0; | |
295 | |
296 for(i = 0; i < avctx->height; i++) { | |
297 ls_encode_line(state, &pb2, last, cur, t, avctx->width, 1, 0, 8); | |
298 t = last[0]; | |
299 last = cur; | |
300 cur += p->linesize[0]; | |
301 } | |
302 }else if(avctx->pix_fmt == PIX_FMT_GRAY16){ | |
303 int t = 0; | |
304 | |
305 for(i = 0; i < avctx->height; i++) { | |
306 ls_encode_line(state, &pb2, last, cur, t, avctx->width, 1, 0, 16); | |
307 t = *((uint16_t*)last); | |
308 last = cur; | |
309 cur += p->linesize[0]; | |
310 } | |
311 }else if(avctx->pix_fmt == PIX_FMT_RGB24){ | |
312 int j, width; | |
313 int Rc[3] = {0, 0, 0}; | |
314 | |
315 width = avctx->width * 3; | |
316 for(i = 0; i < avctx->height; i++) { | |
317 for(j = 0; j < 3; j++) { | |
318 ls_encode_line(state, &pb2, last + j, cur + j, Rc[j], width, 3, j, 8); | |
319 Rc[j] = last[j]; | |
320 } | |
321 last = cur; | |
322 cur += s->picture.linesize[0]; | |
323 } | |
324 }else if(avctx->pix_fmt == PIX_FMT_BGR24){ | |
325 int j, width; | |
326 int Rc[3] = {0, 0, 0}; | |
327 | |
328 width = avctx->width * 3; | |
329 for(i = 0; i < avctx->height; i++) { | |
330 for(j = 2; j >= 0; j--) { | |
331 ls_encode_line(state, &pb2, last + j, cur + j, Rc[j], width, 3, j, 8); | |
332 Rc[j] = last[j]; | |
333 } | |
334 last = cur; | |
335 cur += s->picture.linesize[0]; | |
336 } | |
337 } | |
338 | |
339 av_free(zero); | |
340 av_free(state); | |
341 | |
342 // the specification says that after doing 0xff escaping unused bits in the | |
343 // last byte must be set to 0, so just append 7 "optional" zero-bits to | |
344 // avoid special-casing. | |
345 put_bits(&pb2, 7, 0); | |
346 size = put_bits_count(&pb2); | |
347 flush_put_bits(&pb2); | |
348 /* do escape coding */ | |
349 init_get_bits(&gb, buf2, size); | |
350 size -= 7; | |
351 while(get_bits_count(&gb) < size){ | |
352 int v; | |
353 v = get_bits(&gb, 8); | |
354 put_bits(&pb, 8, v); | |
355 if(v == 0xFF){ | |
356 v = get_bits(&gb, 7); | |
357 put_bits(&pb, 8, v); | |
358 } | |
359 } | |
360 align_put_bits(&pb); | |
361 av_free(buf2); | |
362 | |
363 /* End of image */ | |
364 put_marker(&pb, EOI); | |
365 flush_put_bits(&pb); | |
366 | |
367 emms_c(); | |
368 | |
369 return put_bits_count(&pb) >> 3; | |
370 } | |
371 | |
6517
48759bfbd073
Apply 'cold' attribute to init/uninit functions in libavcodec
zuxy
parents:
5127
diff
changeset
|
372 static av_cold int encode_init_ls(AVCodecContext *ctx) { |
5003 | 373 JpeglsContext *c = (JpeglsContext*)ctx->priv_data; |
374 | |
375 c->avctx = ctx; | |
376 ctx->coded_frame = &c->picture; | |
377 | |
378 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){ | |
379 av_log(ctx, AV_LOG_ERROR, "Only grayscale and RGB24/BGR24 images are supported\n"); | |
380 return -1; | |
381 } | |
382 return 0; | |
383 } | |
384 | |
5127 | 385 AVCodec jpegls_encoder = { //FIXME avoid MPV_* lossless JPEG should not need them |
5003 | 386 "jpegls", |
387 CODEC_TYPE_VIDEO, | |
388 CODEC_ID_JPEGLS, | |
389 sizeof(JpeglsContext), | |
390 encode_init_ls, | |
391 encode_picture_ls, | |
392 NULL, | |
6788 | 393 .pix_fmts= (enum PixelFormat[]){PIX_FMT_BGR24, PIX_FMT_RGB24, PIX_FMT_GRAY8, PIX_FMT_GRAY16, PIX_FMT_NONE}, |
7040
e943e1409077
Make AVCodec long_names definition conditional depending on CONFIG_SMALL.
stefano
parents:
6788
diff
changeset
|
394 .long_name= NULL_IF_CONFIG_SMALL("JPEG-LS"), |
5003 | 395 }; |