Details | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
6147 | serge | 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 |
||
25 | * JPEG-LS encoder. |
||
26 | */ |
||
27 | |||
28 | #include "avcodec.h" |
||
29 | #include "get_bits.h" |
||
30 | #include "put_bits.h" |
||
31 | #include "golomb.h" |
||
32 | #include "internal.h" |
||
33 | #include "mathops.h" |
||
34 | #include "mjpeg.h" |
||
35 | #include "mjpegenc.h" |
||
36 | #include "jpegls.h" |
||
37 | |||
38 | /** |
||
39 | * Encode error from regular symbol |
||
40 | */ |
||
41 | static inline void ls_encode_regular(JLSState *state, PutBitContext *pb, int Q, |
||
42 | int err) |
||
43 | { |
||
44 | int k; |
||
45 | int val; |
||
46 | int map; |
||
47 | |||
48 | for (k = 0; (state->N[Q] << k) < state->A[Q]; k++) |
||
49 | ; |
||
50 | |||
51 | map = !state->near && !k && (2 * state->B[Q] <= -state->N[Q]); |
||
52 | |||
53 | if (err < 0) |
||
54 | err += state->range; |
||
55 | if (err >= (state->range + 1 >> 1)) { |
||
56 | err -= state->range; |
||
57 | val = 2 * FFABS(err) - 1 - map; |
||
58 | } else |
||
59 | val = 2 * err + map; |
||
60 | |||
61 | set_ur_golomb_jpegls(pb, val, k, state->limit, state->qbpp); |
||
62 | |||
63 | ff_jpegls_update_state_regular(state, Q, err); |
||
64 | } |
||
65 | |||
66 | /** |
||
67 | * Encode error from run termination |
||
68 | */ |
||
69 | static inline void ls_encode_runterm(JLSState *state, PutBitContext *pb, |
||
70 | int RItype, int err, int limit_add) |
||
71 | { |
||
72 | int k; |
||
73 | int val, map; |
||
74 | int Q = 365 + RItype; |
||
75 | int temp; |
||
76 | |||
77 | temp = state->A[Q]; |
||
78 | if (RItype) |
||
79 | temp += state->N[Q] >> 1; |
||
80 | for (k = 0; (state->N[Q] << k) < temp; k++) |
||
81 | ; |
||
82 | map = 0; |
||
83 | if (!k && err && (2 * state->B[Q] < state->N[Q])) |
||
84 | map = 1; |
||
85 | |||
86 | if (err < 0) |
||
87 | val = -(2 * err) - 1 - RItype + map; |
||
88 | else |
||
89 | val = 2 * err - RItype - map; |
||
90 | set_ur_golomb_jpegls(pb, val, k, state->limit - limit_add - 1, state->qbpp); |
||
91 | |||
92 | if (err < 0) |
||
93 | state->B[Q]++; |
||
94 | state->A[Q] += (val + 1 - RItype) >> 1; |
||
95 | |||
96 | ff_jpegls_downscale_state(state, Q); |
||
97 | } |
||
98 | |||
99 | /** |
||
100 | * Encode run value as specified by JPEG-LS standard |
||
101 | */ |
||
102 | static inline void ls_encode_run(JLSState *state, PutBitContext *pb, int run, |
||
103 | int comp, int trail) |
||
104 | { |
||
105 | while (run >= (1 << ff_log2_run[state->run_index[comp]])) { |
||
106 | put_bits(pb, 1, 1); |
||
107 | run -= 1 << ff_log2_run[state->run_index[comp]]; |
||
108 | if (state->run_index[comp] < 31) |
||
109 | state->run_index[comp]++; |
||
110 | } |
||
111 | /* if hit EOL, encode another full run, else encode aborted run */ |
||
112 | if (!trail && run) { |
||
113 | put_bits(pb, 1, 1); |
||
114 | } else if (trail) { |
||
115 | put_bits(pb, 1, 0); |
||
116 | if (ff_log2_run[state->run_index[comp]]) |
||
117 | put_bits(pb, ff_log2_run[state->run_index[comp]], run); |
||
118 | } |
||
119 | } |
||
120 | |||
121 | /** |
||
122 | * Encode one line of image |
||
123 | */ |
||
124 | static inline void ls_encode_line(JLSState *state, PutBitContext *pb, |
||
125 | void *last, void *cur, int last2, int w, |
||
126 | int stride, int comp, int bits) |
||
127 | { |
||
128 | int x = 0; |
||
129 | int Ra, Rb, Rc, Rd; |
||
130 | int D0, D1, D2; |
||
131 | |||
132 | while (x < w) { |
||
133 | int err, pred, sign; |
||
134 | |||
135 | /* compute gradients */ |
||
136 | Ra = x ? R(cur, x - stride) : R(last, x); |
||
137 | Rb = R(last, x); |
||
138 | Rc = x ? R(last, x - stride) : last2; |
||
139 | Rd = (x >= w - stride) ? R(last, x) : R(last, x + stride); |
||
140 | D0 = Rd - Rb; |
||
141 | D1 = Rb - Rc; |
||
142 | D2 = Rc - Ra; |
||
143 | |||
144 | /* run mode */ |
||
145 | if ((FFABS(D0) <= state->near) && |
||
146 | (FFABS(D1) <= state->near) && |
||
147 | (FFABS(D2) <= state->near)) { |
||
148 | int RUNval, RItype, run; |
||
149 | |||
150 | run = 0; |
||
151 | RUNval = Ra; |
||
152 | while (x < w && (FFABS(R(cur, x) - RUNval) <= state->near)) { |
||
153 | run++; |
||
154 | W(cur, x, Ra); |
||
155 | x += stride; |
||
156 | } |
||
157 | ls_encode_run(state, pb, run, comp, x < w); |
||
158 | if (x >= w) |
||
159 | return; |
||
160 | Rb = R(last, x); |
||
161 | RItype = FFABS(Ra - Rb) <= state->near; |
||
162 | pred = RItype ? Ra : Rb; |
||
163 | err = R(cur, x) - pred; |
||
164 | |||
165 | if (!RItype && Ra > Rb) |
||
166 | err = -err; |
||
167 | |||
168 | if (state->near) { |
||
169 | if (err > 0) |
||
170 | err = (state->near + err) / state->twonear; |
||
171 | else |
||
172 | err = -(state->near - err) / state->twonear; |
||
173 | |||
174 | if (RItype || (Rb >= Ra)) |
||
175 | Ra = av_clip(pred + err * state->twonear, 0, state->maxval); |
||
176 | else |
||
177 | Ra = av_clip(pred - err * state->twonear, 0, state->maxval); |
||
178 | W(cur, x, Ra); |
||
179 | } |
||
180 | if (err < 0) |
||
181 | err += state->range; |
||
182 | if (err >= state->range + 1 >> 1) |
||
183 | err -= state->range; |
||
184 | |||
185 | ls_encode_runterm(state, pb, RItype, err, |
||
186 | ff_log2_run[state->run_index[comp]]); |
||
187 | |||
188 | if (state->run_index[comp] > 0) |
||
189 | state->run_index[comp]--; |
||
190 | } else { /* regular mode */ |
||
191 | int context; |
||
192 | |||
193 | context = ff_jpegls_quantize(state, D0) * 81 + |
||
194 | ff_jpegls_quantize(state, D1) * 9 + |
||
195 | ff_jpegls_quantize(state, D2); |
||
196 | pred = mid_pred(Ra, Ra + Rb - Rc, Rb); |
||
197 | |||
198 | if (context < 0) { |
||
199 | context = -context; |
||
200 | sign = 1; |
||
201 | pred = av_clip(pred - state->C[context], 0, state->maxval); |
||
202 | err = pred - R(cur, x); |
||
203 | } else { |
||
204 | sign = 0; |
||
205 | pred = av_clip(pred + state->C[context], 0, state->maxval); |
||
206 | err = R(cur, x) - pred; |
||
207 | } |
||
208 | |||
209 | if (state->near) { |
||
210 | if (err > 0) |
||
211 | err = (state->near + err) / state->twonear; |
||
212 | else |
||
213 | err = -(state->near - err) / state->twonear; |
||
214 | if (!sign) |
||
215 | Ra = av_clip(pred + err * state->twonear, 0, state->maxval); |
||
216 | else |
||
217 | Ra = av_clip(pred - err * state->twonear, 0, state->maxval); |
||
218 | W(cur, x, Ra); |
||
219 | } |
||
220 | |||
221 | ls_encode_regular(state, pb, context, err); |
||
222 | } |
||
223 | x += stride; |
||
224 | } |
||
225 | } |
||
226 | |||
227 | static void ls_store_lse(JLSState *state, PutBitContext *pb) |
||
228 | { |
||
229 | /* Test if we have default params and don't need to store LSE */ |
||
230 | JLSState state2 = { 0 }; |
||
231 | state2.bpp = state->bpp; |
||
232 | state2.near = state->near; |
||
233 | ff_jpegls_reset_coding_parameters(&state2, 1); |
||
234 | if (state->T1 == state2.T1 && |
||
235 | state->T2 == state2.T2 && |
||
236 | state->T3 == state2.T3 && |
||
237 | state->reset == state2.reset) |
||
238 | return; |
||
239 | /* store LSE type 1 */ |
||
240 | put_marker(pb, LSE); |
||
241 | put_bits(pb, 16, 13); |
||
242 | put_bits(pb, 8, 1); |
||
243 | put_bits(pb, 16, state->maxval); |
||
244 | put_bits(pb, 16, state->T1); |
||
245 | put_bits(pb, 16, state->T2); |
||
246 | put_bits(pb, 16, state->T3); |
||
247 | put_bits(pb, 16, state->reset); |
||
248 | } |
||
249 | |||
250 | static int encode_picture_ls(AVCodecContext *avctx, AVPacket *pkt, |
||
251 | const AVFrame *pict, int *got_packet) |
||
252 | { |
||
253 | const AVFrame *const p = pict; |
||
254 | const int near = avctx->prediction_method; |
||
255 | PutBitContext pb, pb2; |
||
256 | GetBitContext gb; |
||
257 | uint8_t *buf2 = NULL; |
||
258 | uint8_t *zero = NULL; |
||
259 | uint8_t *cur = NULL; |
||
260 | uint8_t *last = NULL; |
||
261 | JLSState *state = NULL; |
||
262 | int i, size, ret; |
||
263 | int comps; |
||
264 | |||
265 | if (avctx->pix_fmt == AV_PIX_FMT_GRAY8 || |
||
266 | avctx->pix_fmt == AV_PIX_FMT_GRAY16) |
||
267 | comps = 1; |
||
268 | else |
||
269 | comps = 3; |
||
270 | |||
271 | if ((ret = ff_alloc_packet2(avctx, pkt, avctx->width *avctx->height * comps * 4 + |
||
272 | AV_INPUT_BUFFER_MIN_SIZE, 0)) < 0) |
||
273 | return ret; |
||
274 | |||
275 | buf2 = av_malloc(pkt->size); |
||
276 | if (!buf2) |
||
277 | goto memfail; |
||
278 | |||
279 | init_put_bits(&pb, pkt->data, pkt->size); |
||
280 | init_put_bits(&pb2, buf2, pkt->size); |
||
281 | |||
282 | /* write our own JPEG header, can't use mjpeg_picture_header */ |
||
283 | put_marker(&pb, SOI); |
||
284 | put_marker(&pb, SOF48); |
||
285 | put_bits(&pb, 16, 8 + comps * 3); // header size depends on components |
||
286 | put_bits(&pb, 8, (avctx->pix_fmt == AV_PIX_FMT_GRAY16) ? 16 : 8); // bpp |
||
287 | put_bits(&pb, 16, avctx->height); |
||
288 | put_bits(&pb, 16, avctx->width); |
||
289 | put_bits(&pb, 8, comps); // components |
||
290 | for (i = 1; i <= comps; i++) { |
||
291 | put_bits(&pb, 8, i); // component ID |
||
292 | put_bits(&pb, 8, 0x11); // subsampling: none |
||
293 | put_bits(&pb, 8, 0); // Tiq, used by JPEG-LS ext |
||
294 | } |
||
295 | |||
296 | put_marker(&pb, SOS); |
||
297 | put_bits(&pb, 16, 6 + comps * 2); |
||
298 | put_bits(&pb, 8, comps); |
||
299 | for (i = 1; i <= comps; i++) { |
||
300 | put_bits(&pb, 8, i); // component ID |
||
301 | put_bits(&pb, 8, 0); // mapping index: none |
||
302 | } |
||
303 | put_bits(&pb, 8, near); |
||
304 | put_bits(&pb, 8, (comps > 1) ? 1 : 0); // interleaving: 0 - plane, 1 - line |
||
305 | put_bits(&pb, 8, 0); // point transform: none |
||
306 | |||
307 | state = av_mallocz(sizeof(JLSState)); |
||
308 | if (!state) |
||
309 | goto memfail; |
||
310 | |||
311 | /* initialize JPEG-LS state from JPEG parameters */ |
||
312 | state->near = near; |
||
313 | state->bpp = (avctx->pix_fmt == AV_PIX_FMT_GRAY16) ? 16 : 8; |
||
314 | ff_jpegls_reset_coding_parameters(state, 0); |
||
315 | ff_jpegls_init_state(state); |
||
316 | |||
317 | ls_store_lse(state, &pb); |
||
318 | |||
319 | zero = last = av_mallocz(FFABS(p->linesize[0])); |
||
320 | if (!zero) |
||
321 | goto memfail; |
||
322 | |||
323 | cur = p->data[0]; |
||
324 | if (avctx->pix_fmt == AV_PIX_FMT_GRAY8) { |
||
325 | int t = 0; |
||
326 | |||
327 | for (i = 0; i < avctx->height; i++) { |
||
328 | ls_encode_line(state, &pb2, last, cur, t, avctx->width, 1, 0, 8); |
||
329 | t = last[0]; |
||
330 | last = cur; |
||
331 | cur += p->linesize[0]; |
||
332 | } |
||
333 | } else if (avctx->pix_fmt == AV_PIX_FMT_GRAY16) { |
||
334 | int t = 0; |
||
335 | |||
336 | for (i = 0; i < avctx->height; i++) { |
||
337 | ls_encode_line(state, &pb2, last, cur, t, avctx->width, 1, 0, 16); |
||
338 | t = *((uint16_t *)last); |
||
339 | last = cur; |
||
340 | cur += p->linesize[0]; |
||
341 | } |
||
342 | } else if (avctx->pix_fmt == AV_PIX_FMT_RGB24) { |
||
343 | int j, width; |
||
344 | int Rc[3] = { 0, 0, 0 }; |
||
345 | |||
346 | width = avctx->width * 3; |
||
347 | for (i = 0; i < avctx->height; i++) { |
||
348 | for (j = 0; j < 3; j++) { |
||
349 | ls_encode_line(state, &pb2, last + j, cur + j, Rc[j], |
||
350 | width, 3, j, 8); |
||
351 | Rc[j] = last[j]; |
||
352 | } |
||
353 | last = cur; |
||
354 | cur += p->linesize[0]; |
||
355 | } |
||
356 | } else if (avctx->pix_fmt == AV_PIX_FMT_BGR24) { |
||
357 | int j, width; |
||
358 | int Rc[3] = { 0, 0, 0 }; |
||
359 | |||
360 | width = avctx->width * 3; |
||
361 | for (i = 0; i < avctx->height; i++) { |
||
362 | for (j = 2; j >= 0; j--) { |
||
363 | ls_encode_line(state, &pb2, last + j, cur + j, Rc[j], |
||
364 | width, 3, j, 8); |
||
365 | Rc[j] = last[j]; |
||
366 | } |
||
367 | last = cur; |
||
368 | cur += p->linesize[0]; |
||
369 | } |
||
370 | } |
||
371 | |||
372 | av_freep(&zero); |
||
373 | av_freep(&state); |
||
374 | |||
375 | /* the specification says that after doing 0xff escaping unused bits in |
||
376 | * the last byte must be set to 0, so just append 7 "optional" zero-bits |
||
377 | * to avoid special-casing. */ |
||
378 | put_bits(&pb2, 7, 0); |
||
379 | size = put_bits_count(&pb2); |
||
380 | flush_put_bits(&pb2); |
||
381 | /* do escape coding */ |
||
382 | init_get_bits(&gb, buf2, size); |
||
383 | size -= 7; |
||
384 | while (get_bits_count(&gb) < size) { |
||
385 | int v; |
||
386 | v = get_bits(&gb, 8); |
||
387 | put_bits(&pb, 8, v); |
||
388 | if (v == 0xFF) { |
||
389 | v = get_bits(&gb, 7); |
||
390 | put_bits(&pb, 8, v); |
||
391 | } |
||
392 | } |
||
393 | avpriv_align_put_bits(&pb); |
||
394 | av_freep(&buf2); |
||
395 | |||
396 | /* End of image */ |
||
397 | put_marker(&pb, EOI); |
||
398 | flush_put_bits(&pb); |
||
399 | |||
400 | emms_c(); |
||
401 | |||
402 | pkt->size = put_bits_count(&pb) >> 3; |
||
403 | pkt->flags |= AV_PKT_FLAG_KEY; |
||
404 | *got_packet = 1; |
||
405 | return 0; |
||
406 | |||
407 | memfail: |
||
408 | av_free_packet(pkt); |
||
409 | av_freep(&buf2); |
||
410 | av_freep(&state); |
||
411 | av_freep(&zero); |
||
412 | return AVERROR(ENOMEM); |
||
413 | } |
||
414 | |||
415 | static av_cold int encode_init_ls(AVCodecContext *ctx) |
||
416 | { |
||
417 | #if FF_API_CODED_FRAME |
||
418 | FF_DISABLE_DEPRECATION_WARNINGS |
||
419 | ctx->coded_frame->pict_type = AV_PICTURE_TYPE_I; |
||
420 | ctx->coded_frame->key_frame = 1; |
||
421 | FF_ENABLE_DEPRECATION_WARNINGS |
||
422 | #endif |
||
423 | |||
424 | if (ctx->pix_fmt != AV_PIX_FMT_GRAY8 && |
||
425 | ctx->pix_fmt != AV_PIX_FMT_GRAY16 && |
||
426 | ctx->pix_fmt != AV_PIX_FMT_RGB24 && |
||
427 | ctx->pix_fmt != AV_PIX_FMT_BGR24) { |
||
428 | av_log(ctx, AV_LOG_ERROR, |
||
429 | "Only grayscale and RGB24/BGR24 images are supported\n"); |
||
430 | return -1; |
||
431 | } |
||
432 | return 0; |
||
433 | } |
||
434 | |||
435 | AVCodec ff_jpegls_encoder = { |
||
436 | .name = "jpegls", |
||
437 | .long_name = NULL_IF_CONFIG_SMALL("JPEG-LS"), |
||
438 | .type = AVMEDIA_TYPE_VIDEO, |
||
439 | .id = AV_CODEC_ID_JPEGLS, |
||
440 | .init = encode_init_ls, |
||
441 | .capabilities = AV_CODEC_CAP_FRAME_THREADS | AV_CODEC_CAP_INTRA_ONLY, |
||
442 | .encode2 = encode_picture_ls, |
||
443 | .pix_fmts = (const enum AVPixelFormat[]) { |
||
444 | AV_PIX_FMT_BGR24, AV_PIX_FMT_RGB24, |
||
445 | AV_PIX_FMT_GRAY8, AV_PIX_FMT_GRAY16, |
||
446 | AV_PIX_FMT_NONE |
||
447 | }, |
||
448 | .caps_internal = FF_CODEC_CAP_INIT_THREADSAFE | |
||
449 | FF_CODEC_CAP_INIT_CLEANUP, |
||
450 | };>>>>>>=>=>>>>=>>=>>=>=>=>>>><>><>>>>>><>>=>>><> |