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
,
47 for (k
= 0; (state
->N
[Q
] << k
) < state
->A
[Q
]; k
++)
50 map
= !state
->near
&& !k
&& (2 * state
->B
[Q
] <= -state
->N
[Q
]);
54 if (err
>= (state
->range
+ 1 >> 1)) {
56 val
= 2 * FFABS(err
) - 1 - map
;
60 set_ur_golomb_jpegls(pb
, val
, k
, state
->limit
, state
->qbpp
);
62 ff_jpegls_update_state_regular(state
, Q
, err
);
66 * Encode error from run termination
68 static inline void ls_encode_runterm(JLSState
*state
, PutBitContext
*pb
,
69 int RItype
, int err
, int limit_add
)
78 temp
+= state
->N
[Q
] >> 1;
79 for (k
= 0; (state
->N
[Q
] << k
) < temp
; k
++)
82 if (!k
&& err
&& (2 * state
->B
[Q
] < state
->N
[Q
]))
86 val
= -(2 * err
) - 1 - RItype
+ map
;
88 val
= 2 * err
- RItype
- map
;
89 set_ur_golomb_jpegls(pb
, val
, k
, state
->limit
- limit_add
- 1, state
->qbpp
);
93 state
->A
[Q
] += (val
+ 1 - RItype
) >> 1;
95 ff_jpegls_downscale_state(state
, Q
);
99 * Encode run value as specified by JPEG-LS standard
101 static inline void ls_encode_run(JLSState
*state
, PutBitContext
*pb
, int run
,
104 while (run
>= (1 << ff_log2_run
[state
->run_index
[comp
]])) {
106 run
-= 1 << ff_log2_run
[state
->run_index
[comp
]];
107 if (state
->run_index
[comp
] < 31)
108 state
->run_index
[comp
]++;
110 /* if hit EOL, encode another full run, else encode aborted run */
115 if (ff_log2_run
[state
->run_index
[comp
]])
116 put_bits(pb
, ff_log2_run
[state
->run_index
[comp
]], run
);
121 * Encode one line of image
123 static inline void ls_encode_line(JLSState
*state
, PutBitContext
*pb
,
124 void *last
, void *cur
, int last2
, int w
,
125 int stride
, int comp
, int bits
)
134 /* compute gradients */
135 Ra
= x
? R(cur
, x
- stride
) : R(last
, x
);
137 Rc
= x
? R(last
, x
- stride
) : last2
;
138 Rd
= (x
>= w
- stride
) ? R(last
, x
) : R(last
, x
+ stride
);
144 if ((FFABS(D0
) <= state
->near
) &&
145 (FFABS(D1
) <= state
->near
) &&
146 (FFABS(D2
) <= state
->near
)) {
147 int RUNval
, RItype
, run
;
151 while (x
< w
&& (FFABS(R(cur
, x
) - RUNval
) <= state
->near
)) {
156 ls_encode_run(state
, pb
, run
, comp
, x
< w
);
160 RItype
= FFABS(Ra
- Rb
) <= state
->near
;
161 pred
= RItype
? Ra
: Rb
;
162 err
= R(cur
, x
) - pred
;
164 if (!RItype
&& Ra
> Rb
)
169 err
= (state
->near
+ err
) / state
->twonear
;
171 err
= -(state
->near
- err
) / state
->twonear
;
173 if (RItype
|| (Rb
>= Ra
))
174 Ra
= av_clip(pred
+ err
* state
->twonear
, 0, state
->maxval
);
176 Ra
= av_clip(pred
- err
* state
->twonear
, 0, state
->maxval
);
181 if (err
>= state
->range
+ 1 >> 1)
184 ls_encode_runterm(state
, pb
, RItype
, err
,
185 ff_log2_run
[state
->run_index
[comp
]]);
187 if (state
->run_index
[comp
] > 0)
188 state
->run_index
[comp
]--;
189 } else { /* regular mode */
192 context
= ff_jpegls_quantize(state
, D0
) * 81 +
193 ff_jpegls_quantize(state
, D1
) * 9 +
194 ff_jpegls_quantize(state
, D2
);
195 pred
= mid_pred(Ra
, Ra
+ Rb
- Rc
, Rb
);
200 pred
= av_clip(pred
- state
->C
[context
], 0, state
->maxval
);
201 err
= pred
- R(cur
, x
);
204 pred
= av_clip(pred
+ state
->C
[context
], 0, state
->maxval
);
205 err
= R(cur
, x
) - pred
;
210 err
= (state
->near
+ err
) / state
->twonear
;
212 err
= -(state
->near
- err
) / state
->twonear
;
214 Ra
= av_clip(pred
+ err
* state
->twonear
, 0, state
->maxval
);
216 Ra
= av_clip(pred
- err
* state
->twonear
, 0, state
->maxval
);
220 ls_encode_regular(state
, pb
, context
, err
);
226 static void ls_store_lse(JLSState
*state
, PutBitContext
*pb
)
228 /* Test if we have default params and don't need to store LSE */
229 JLSState state2
= { 0 };
230 state2
.bpp
= state
->bpp
;
231 state2
.near
= state
->near
;
232 ff_jpegls_reset_coding_parameters(&state2
, 1);
233 if (state
->T1
== state2
.T1
&&
234 state
->T2
== state2
.T2
&&
235 state
->T3
== state2
.T3
&&
236 state
->reset
== state2
.reset
)
238 /* store LSE type 1 */
240 put_bits(pb
, 16, 13);
242 put_bits(pb
, 16, state
->maxval
);
243 put_bits(pb
, 16, state
->T1
);
244 put_bits(pb
, 16, state
->T2
);
245 put_bits(pb
, 16, state
->T3
);
246 put_bits(pb
, 16, state
->reset
);
249 static int encode_picture_ls(AVCodecContext
*avctx
, AVPacket
*pkt
,
250 const AVFrame
*pict
, int *got_packet
)
252 const AVFrame
*const p
= pict
;
253 const int near
= avctx
->prediction_method
;
254 PutBitContext pb
, pb2
;
256 uint8_t *buf2
, *zero
, *cur
, *last
;
261 if (avctx
->pix_fmt
== AV_PIX_FMT_GRAY8
||
262 avctx
->pix_fmt
== AV_PIX_FMT_GRAY16
)
267 if ((ret
= ff_alloc_packet2(avctx
, pkt
, avctx
->width
*avctx
->height
* comps
* 4 +
268 FF_MIN_BUFFER_SIZE
)) < 0)
271 buf2
= av_malloc(pkt
->size
);
273 init_put_bits(&pb
, pkt
->data
, pkt
->size
);
274 init_put_bits(&pb2
, buf2
, pkt
->size
);
276 /* write our own JPEG header, can't use mjpeg_picture_header */
277 put_marker(&pb
, SOI
);
278 put_marker(&pb
, SOF48
);
279 put_bits(&pb
, 16, 8 + comps
* 3); // header size depends on components
280 put_bits(&pb
, 8, (avctx
->pix_fmt
== AV_PIX_FMT_GRAY16
) ? 16 : 8); // bpp
281 put_bits(&pb
, 16, avctx
->height
);
282 put_bits(&pb
, 16, avctx
->width
);
283 put_bits(&pb
, 8, comps
); // components
284 for (i
= 1; i
<= comps
; i
++) {
285 put_bits(&pb
, 8, i
); // component ID
286 put_bits(&pb
, 8, 0x11); // subsampling: none
287 put_bits(&pb
, 8, 0); // Tiq, used by JPEG-LS ext
290 put_marker(&pb
, SOS
);
291 put_bits(&pb
, 16, 6 + comps
* 2);
292 put_bits(&pb
, 8, comps
);
293 for (i
= 1; i
<= comps
; i
++) {
294 put_bits(&pb
, 8, i
); // component ID
295 put_bits(&pb
, 8, 0); // mapping index: none
297 put_bits(&pb
, 8, near
);
298 put_bits(&pb
, 8, (comps
> 1) ? 1 : 0); // interleaving: 0 - plane, 1 - line
299 put_bits(&pb
, 8, 0); // point transform: none
301 state
= av_mallocz(sizeof(JLSState
));
302 /* initialize JPEG-LS state from JPEG parameters */
304 state
->bpp
= (avctx
->pix_fmt
== AV_PIX_FMT_GRAY16
) ? 16 : 8;
305 ff_jpegls_reset_coding_parameters(state
, 0);
306 ff_jpegls_init_state(state
);
308 ls_store_lse(state
, &pb
);
310 zero
= av_mallocz(FFABS(p
->linesize
[0]));
313 return AVERROR(ENOMEM
);
317 if (avctx
->pix_fmt
== AV_PIX_FMT_GRAY8
) {
320 for (i
= 0; i
< avctx
->height
; i
++) {
321 ls_encode_line(state
, &pb2
, last
, cur
, t
, avctx
->width
, 1, 0, 8);
324 cur
+= p
->linesize
[0];
326 } else if (avctx
->pix_fmt
== AV_PIX_FMT_GRAY16
) {
329 for (i
= 0; i
< avctx
->height
; i
++) {
330 ls_encode_line(state
, &pb2
, last
, cur
, t
, avctx
->width
, 1, 0, 16);
331 t
= *((uint16_t *)last
);
333 cur
+= p
->linesize
[0];
335 } else if (avctx
->pix_fmt
== AV_PIX_FMT_RGB24
) {
337 int Rc
[3] = { 0, 0, 0 };
339 width
= avctx
->width
* 3;
340 for (i
= 0; i
< avctx
->height
; i
++) {
341 for (j
= 0; j
< 3; j
++) {
342 ls_encode_line(state
, &pb2
, last
+ j
, cur
+ j
, Rc
[j
],
347 cur
+= p
->linesize
[0];
349 } else if (avctx
->pix_fmt
== AV_PIX_FMT_BGR24
) {
351 int Rc
[3] = { 0, 0, 0 };
353 width
= avctx
->width
* 3;
354 for (i
= 0; i
< avctx
->height
; i
++) {
355 for (j
= 2; j
>= 0; j
--) {
356 ls_encode_line(state
, &pb2
, last
+ j
, cur
+ j
, Rc
[j
],
361 cur
+= p
->linesize
[0];
368 /* the specification says that after doing 0xff escaping unused bits in
369 * the last byte must be set to 0, so just append 7 "optional" zero-bits
370 * to avoid special-casing. */
371 put_bits(&pb2
, 7, 0);
372 size
= put_bits_count(&pb2
);
373 flush_put_bits(&pb2
);
374 /* do escape coding */
375 init_get_bits(&gb
, buf2
, size
);
377 while (get_bits_count(&gb
) < size
) {
379 v
= get_bits(&gb
, 8);
382 v
= get_bits(&gb
, 7);
386 avpriv_align_put_bits(&pb
);
390 put_marker(&pb
, EOI
);
395 pkt
->size
= put_bits_count(&pb
) >> 3;
396 pkt
->flags
|= AV_PKT_FLAG_KEY
;
401 static av_cold
int encode_close(AVCodecContext
*avctx
)
403 av_frame_free(&avctx
->coded_frame
);
407 static av_cold
int encode_init_ls(AVCodecContext
*ctx
)
409 ctx
->coded_frame
= av_frame_alloc();
410 if (!ctx
->coded_frame
)
411 return AVERROR(ENOMEM
);
413 ctx
->coded_frame
->pict_type
= AV_PICTURE_TYPE_I
;
414 ctx
->coded_frame
->key_frame
= 1;
416 if (ctx
->pix_fmt
!= AV_PIX_FMT_GRAY8
&&
417 ctx
->pix_fmt
!= AV_PIX_FMT_GRAY16
&&
418 ctx
->pix_fmt
!= AV_PIX_FMT_RGB24
&&
419 ctx
->pix_fmt
!= AV_PIX_FMT_BGR24
) {
420 av_log(ctx
, AV_LOG_ERROR
,
421 "Only grayscale and RGB24/BGR24 images are supported\n");
427 AVCodec ff_jpegls_encoder
= {
429 .long_name
= NULL_IF_CONFIG_SMALL("JPEG-LS"),
430 .type
= AVMEDIA_TYPE_VIDEO
,
431 .id
= AV_CODEC_ID_JPEGLS
,
432 .init
= encode_init_ls
,
433 .close
= encode_close
,
434 .capabilities
= CODEC_CAP_FRAME_THREADS
| CODEC_CAP_INTRA_ONLY
,
435 .encode2
= encode_picture_ls
,
436 .pix_fmts
= (const enum AVPixelFormat
[]) {
437 AV_PIX_FMT_BGR24
, AV_PIX_FMT_RGB24
,
438 AV_PIX_FMT_GRAY8
, AV_PIX_FMT_GRAY16
,