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