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
38
* Encode error from regular symbol
40
static inline void ls_encode_regular(JLSState *state, PutBitContext *pb, int Q, int err){
45
for(k = 0; (state->N[Q] << k) < state->A[Q]; k++);
47
map = !state->near && !k && (2 * state->B[Q] <= -state->N[Q]);
51
if(err >= ((state->range + 1) >> 1)) {
53
val = 2 * FFABS(err) - 1 - map;
57
set_ur_golomb_jpegls(pb, val, k, state->limit, state->qbpp);
59
ff_jpegls_update_state_regular(state, Q, err);
63
* Encode error from run termination
65
static inline void ls_encode_runterm(JLSState *state, PutBitContext *pb, int RItype, int err, int limit_add){
73
temp += state->N[Q] >> 1;
74
for(k = 0; (state->N[Q] << k) < temp; k++);
76
if(!k && err && (2 * state->B[Q] < state->N[Q]))
80
val = - (2 * err) - 1 - RItype + map;
82
val = 2 * err - RItype - map;
83
set_ur_golomb_jpegls(pb, val, k, state->limit - limit_add - 1, state->qbpp);
87
state->A[Q] += (val + 1 - RItype) >> 1;
89
ff_jpegls_downscale_state(state, Q);
93
* Encode run value as specified by JPEG-LS standard
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]])){
98
run -= 1 << ff_log2_run[state->run_index[comp]];
99
if(state->run_index[comp] < 31)
100
state->run_index[comp]++;
102
/* if hit EOL, encode another full run, else encode aborted run */
107
if(ff_log2_run[state->run_index[comp]])
108
put_bits(pb, ff_log2_run[state->run_index[comp]], run);
113
* Encode one line of image
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){
123
/* compute gradients */
124
Ra = x ? R(cur, x - stride) : R(last, x);
126
Rc = x ? R(last, x - stride) : last2;
127
Rd = (x >= w - stride) ? R(last, x) : R(last, x + stride);
133
if((FFABS(D0) <= state->near) && (FFABS(D1) <= state->near) && (FFABS(D2) <= state->near)) {
134
int RUNval, RItype, run;
138
while(x < w && (FFABS(R(cur, x) - RUNval) <= state->near)){
143
ls_encode_run(state, pb, run, comp, x < w);
147
RItype = (FFABS(Ra - Rb) <= state->near);
148
pred = RItype ? Ra : Rb;
149
err = R(cur, x) - pred;
151
if(!RItype && Ra > Rb)
156
err = (state->near + err) / state->twonear;
158
err = -(state->near - err) / state->twonear;
160
if(RItype || (Rb >= Ra))
161
Ra = av_clip(pred + err * state->twonear, 0, state->maxval);
163
Ra = av_clip(pred - err * state->twonear, 0, state->maxval);
168
if(err >= ((state->range + 1) >> 1))
171
ls_encode_runterm(state, pb, RItype, err, ff_log2_run[state->run_index[comp]]);
173
if(state->run_index[comp] > 0)
174
state->run_index[comp]--;
175
} else { /* regular mode */
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);
184
pred = av_clip(pred - state->C[context], 0, state->maxval);
185
err = pred - R(cur, x);
188
pred = av_clip(pred + state->C[context], 0, state->maxval);
189
err = R(cur, x) - pred;
194
err = (state->near + err) / state->twonear;
196
err = -(state->near - err) / state->twonear;
198
Ra = av_clip(pred + err * state->twonear, 0, state->maxval);
200
Ra = av_clip(pred - err * state->twonear, 0, state->maxval);
204
ls_encode_regular(state, pb, context, err);
210
static void ls_store_lse(JLSState *state, PutBitContext *pb){
211
/* Test if we have default params and don't need to store LSE */
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)
219
/* store LSE type 1 */
221
put_bits(pb, 16, 13);
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);
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;
237
uint8_t *buf2, *zero, *cur, *last;
242
buf2 = av_malloc(buf_size);
244
init_put_bits(&pb, buf, buf_size);
245
init_put_bits(&pb2, buf2, buf_size);
248
p->pict_type= FF_I_TYPE;
251
if(avctx->pix_fmt == PIX_FMT_GRAY8 || avctx->pix_fmt == PIX_FMT_GRAY16)
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
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
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
281
state = av_mallocz(sizeof(JLSState));
282
/* initialize JPEG-LS state from JPEG parameters */
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);
288
ls_store_lse(state, &pb);
290
zero = av_mallocz(p->linesize[0]);
293
if(avctx->pix_fmt == PIX_FMT_GRAY8){
296
for(i = 0; i < avctx->height; i++) {
297
ls_encode_line(state, &pb2, last, cur, t, avctx->width, 1, 0, 8);
300
cur += p->linesize[0];
302
}else if(avctx->pix_fmt == PIX_FMT_GRAY16){
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);
309
cur += p->linesize[0];
311
}else if(avctx->pix_fmt == PIX_FMT_RGB24){
313
int Rc[3] = {0, 0, 0};
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);
322
cur += s->picture.linesize[0];
324
}else if(avctx->pix_fmt == PIX_FMT_BGR24){
326
int Rc[3] = {0, 0, 0};
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);
335
cur += s->picture.linesize[0];
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);
351
while(get_bits_count(&gb) < size){
353
v = get_bits(&gb, 8);
356
v = get_bits(&gb, 7);
364
put_marker(&pb, EOI);
369
return put_bits_count(&pb) >> 3;
372
static av_cold int encode_init_ls(AVCodecContext *ctx) {
373
JpeglsContext *c = (JpeglsContext*)ctx->priv_data;
376
ctx->coded_frame = &c->picture;
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");
385
AVCodec jpegls_encoder = { //FIXME avoid MPV_* lossless JPEG should not need them
389
sizeof(JpeglsContext),
393
.pix_fmts= (const enum PixelFormat[]){PIX_FMT_BGR24, PIX_FMT_RGB24, PIX_FMT_GRAY8, PIX_FMT_GRAY16, PIX_FMT_NONE},
394
.long_name= NULL_IF_CONFIG_SMALL("JPEG-LS"),