jpeglsenc.c
Go to the documentation of this file.
1 /*
2  * JPEG-LS encoder
3  * Copyright (c) 2003 Michael Niedermayer
4  * Copyright (c) 2006 Konstantin Shishkov
5  *
6  * This file is part of Libav.
7  *
8  * Libav 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  * Libav 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 Libav; if not, write to the Free Software
20  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21  */
22 
28 #include "avcodec.h"
29 #include "get_bits.h"
30 #include "golomb.h"
31 #include "internal.h"
32 #include "mathops.h"
33 #include "dsputil.h"
34 #include "mjpeg.h"
35 #include "jpegls.h"
36 
37 
41 static inline void ls_encode_regular(JLSState *state, PutBitContext *pb, int Q, int err){
42  int k;
43  int val;
44  int map;
45 
46  for(k = 0; (state->N[Q] << k) < state->A[Q]; k++);
47 
48  map = !state->near && !k && (2 * state->B[Q] <= -state->N[Q]);
49 
50  if(err < 0)
51  err += state->range;
52  if(err >= ((state->range + 1) >> 1)) {
53  err -= state->range;
54  val = 2 * FFABS(err) - 1 - map;
55  } else
56  val = 2 * err + map;
57 
58  set_ur_golomb_jpegls(pb, val, k, state->limit, state->qbpp);
59 
60  ff_jpegls_update_state_regular(state, Q, err);
61 }
62 
66 static inline void ls_encode_runterm(JLSState *state, PutBitContext *pb, int RItype, int err, int limit_add){
67  int k;
68  int val, map;
69  int Q = 365 + RItype;
70  int temp;
71 
72  temp = state->A[Q];
73  if(RItype)
74  temp += state->N[Q] >> 1;
75  for(k = 0; (state->N[Q] << k) < temp; k++);
76  map = 0;
77  if(!k && err && (2 * state->B[Q] < state->N[Q]))
78  map = 1;
79 
80  if(err < 0)
81  val = - (2 * err) - 1 - RItype + map;
82  else
83  val = 2 * err - RItype - map;
84  set_ur_golomb_jpegls(pb, val, k, state->limit - limit_add - 1, state->qbpp);
85 
86  if(err < 0)
87  state->B[Q]++;
88  state->A[Q] += (val + 1 - RItype) >> 1;
89 
90  ff_jpegls_downscale_state(state, Q);
91 }
92 
96 static inline void ls_encode_run(JLSState *state, PutBitContext *pb, int run, int comp, int trail){
97  while(run >= (1 << ff_log2_run[state->run_index[comp]])){
98  put_bits(pb, 1, 1);
99  run -= 1 << ff_log2_run[state->run_index[comp]];
100  if(state->run_index[comp] < 31)
101  state->run_index[comp]++;
102  }
103  /* if hit EOL, encode another full run, else encode aborted run */
104  if(!trail && run) {
105  put_bits(pb, 1, 1);
106  }else if(trail){
107  put_bits(pb, 1, 0);
108  if(ff_log2_run[state->run_index[comp]])
109  put_bits(pb, ff_log2_run[state->run_index[comp]], run);
110  }
111 }
112 
116 static inline void ls_encode_line(JLSState *state, PutBitContext *pb, void *last, void *cur, int last2, int w, int stride, int comp, int bits){
117  int x = 0;
118  int Ra, Rb, Rc, Rd;
119  int D0, D1, D2;
120 
121  while(x < w) {
122  int err, pred, sign;
123 
124  /* compute gradients */
125  Ra = x ? R(cur, x - stride) : R(last, x);
126  Rb = R(last, x);
127  Rc = x ? R(last, x - stride) : last2;
128  Rd = (x >= w - stride) ? R(last, x) : R(last, x + stride);
129  D0 = Rd - Rb;
130  D1 = Rb - Rc;
131  D2 = Rc - Ra;
132 
133  /* run mode */
134  if((FFABS(D0) <= state->near) && (FFABS(D1) <= state->near) && (FFABS(D2) <= state->near)) {
135  int RUNval, RItype, run;
136 
137  run = 0;
138  RUNval = Ra;
139  while(x < w && (FFABS(R(cur, x) - RUNval) <= state->near)){
140  run++;
141  W(cur, x, Ra);
142  x += stride;
143  }
144  ls_encode_run(state, pb, run, comp, x < w);
145  if(x >= w)
146  return;
147  Rb = R(last, x);
148  RItype = (FFABS(Ra - Rb) <= state->near);
149  pred = RItype ? Ra : Rb;
150  err = R(cur, x) - pred;
151 
152  if(!RItype && Ra > Rb)
153  err = -err;
154 
155  if(state->near){
156  if(err > 0)
157  err = (state->near + err) / state->twonear;
158  else
159  err = -(state->near - err) / state->twonear;
160 
161  if(RItype || (Rb >= Ra))
162  Ra = av_clip(pred + err * state->twonear, 0, state->maxval);
163  else
164  Ra = av_clip(pred - err * state->twonear, 0, state->maxval);
165  W(cur, x, Ra);
166  }
167  if(err < 0)
168  err += state->range;
169  if(err >= ((state->range + 1) >> 1))
170  err -= state->range;
171 
172  ls_encode_runterm(state, pb, RItype, err, ff_log2_run[state->run_index[comp]]);
173 
174  if(state->run_index[comp] > 0)
175  state->run_index[comp]--;
176  } else { /* regular mode */
177  int context;
178 
179  context = ff_jpegls_quantize(state, D0) * 81 + ff_jpegls_quantize(state, D1) * 9 + ff_jpegls_quantize(state, D2);
180  pred = mid_pred(Ra, Ra + Rb - Rc, Rb);
181 
182  if(context < 0){
183  context = -context;
184  sign = 1;
185  pred = av_clip(pred - state->C[context], 0, state->maxval);
186  err = pred - R(cur, x);
187  }else{
188  sign = 0;
189  pred = av_clip(pred + state->C[context], 0, state->maxval);
190  err = R(cur, x) - pred;
191  }
192 
193  if(state->near){
194  if(err > 0)
195  err = (state->near + err) / state->twonear;
196  else
197  err = -(state->near - err) / state->twonear;
198  if(!sign)
199  Ra = av_clip(pred + err * state->twonear, 0, state->maxval);
200  else
201  Ra = av_clip(pred - err * state->twonear, 0, state->maxval);
202  W(cur, x, Ra);
203  }
204 
205  ls_encode_regular(state, pb, context, err);
206  }
207  x += stride;
208  }
209 }
210 
212  /* Test if we have default params and don't need to store LSE */
213  JLSState state2 = { 0 };
214  state2.bpp = state->bpp;
215  state2.near = state->near;
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, AVPacket *pkt,
231  const AVFrame *pict, int *got_packet)
232 {
233  JpeglsContext * const s = avctx->priv_data;
234  AVFrame * const p = &s->picture;
235  const int near = avctx->prediction_method;
236  PutBitContext pb, pb2;
237  GetBitContext gb;
238  uint8_t *buf2, *zero, *cur, *last;
239  JLSState *state;
240  int i, size, ret;
241  int comps;
242 
243  *p = *pict;
245  p->key_frame= 1;
246 
247  if(avctx->pix_fmt == AV_PIX_FMT_GRAY8 || avctx->pix_fmt == AV_PIX_FMT_GRAY16)
248  comps = 1;
249  else
250  comps = 3;
251 
252  if ((ret = ff_alloc_packet(pkt, avctx->width*avctx->height*comps*4 +
253  FF_MIN_BUFFER_SIZE)) < 0) {
254  av_log(avctx, AV_LOG_ERROR, "Error getting output packet.\n");
255  return ret;
256  }
257 
258  buf2 = av_malloc(pkt->size);
259 
260  init_put_bits(&pb, pkt->data, pkt->size);
261  init_put_bits(&pb2, buf2, pkt->size);
262 
263  /* write our own JPEG header, can't use mjpeg_picture_header */
264  put_marker(&pb, SOI);
265  put_marker(&pb, SOF48);
266  put_bits(&pb, 16, 8 + comps * 3); // header size depends on components
267  put_bits(&pb, 8, (avctx->pix_fmt == AV_PIX_FMT_GRAY16) ? 16 : 8); // bpp
268  put_bits(&pb, 16, avctx->height);
269  put_bits(&pb, 16, avctx->width);
270  put_bits(&pb, 8, comps); // components
271  for(i = 1; i <= comps; i++) {
272  put_bits(&pb, 8, i); // component ID
273  put_bits(&pb, 8, 0x11); // subsampling: none
274  put_bits(&pb, 8, 0); // Tiq, used by JPEG-LS ext
275  }
276 
277  put_marker(&pb, SOS);
278  put_bits(&pb, 16, 6 + comps * 2);
279  put_bits(&pb, 8, comps);
280  for(i = 1; i <= comps; i++) {
281  put_bits(&pb, 8, i); // component ID
282  put_bits(&pb, 8, 0); // mapping index: none
283  }
284  put_bits(&pb, 8, near);
285  put_bits(&pb, 8, (comps > 1) ? 1 : 0); // interleaving: 0 - plane, 1 - line
286  put_bits(&pb, 8, 0); // point transform: none
287 
288  state = av_mallocz(sizeof(JLSState));
289  /* initialize JPEG-LS state from JPEG parameters */
290  state->near = near;
291  state->bpp = (avctx->pix_fmt == AV_PIX_FMT_GRAY16) ? 16 : 8;
293  ff_jpegls_init_state(state);
294 
295  ls_store_lse(state, &pb);
296 
297  zero = av_mallocz(p->linesize[0]);
298  last = zero;
299  cur = p->data[0];
300  if(avctx->pix_fmt == AV_PIX_FMT_GRAY8){
301  int t = 0;
302 
303  for(i = 0; i < avctx->height; i++) {
304  ls_encode_line(state, &pb2, last, cur, t, avctx->width, 1, 0, 8);
305  t = last[0];
306  last = cur;
307  cur += p->linesize[0];
308  }
309  }else if(avctx->pix_fmt == AV_PIX_FMT_GRAY16){
310  int t = 0;
311 
312  for(i = 0; i < avctx->height; i++) {
313  ls_encode_line(state, &pb2, last, cur, t, avctx->width, 1, 0, 16);
314  t = *((uint16_t*)last);
315  last = cur;
316  cur += p->linesize[0];
317  }
318  }else if(avctx->pix_fmt == AV_PIX_FMT_RGB24){
319  int j, width;
320  int Rc[3] = {0, 0, 0};
321 
322  width = avctx->width * 3;
323  for(i = 0; i < avctx->height; i++) {
324  for(j = 0; j < 3; j++) {
325  ls_encode_line(state, &pb2, last + j, cur + j, Rc[j], width, 3, j, 8);
326  Rc[j] = last[j];
327  }
328  last = cur;
329  cur += s->picture.linesize[0];
330  }
331  }else if(avctx->pix_fmt == AV_PIX_FMT_BGR24){
332  int j, width;
333  int Rc[3] = {0, 0, 0};
334 
335  width = avctx->width * 3;
336  for(i = 0; i < avctx->height; i++) {
337  for(j = 2; j >= 0; j--) {
338  ls_encode_line(state, &pb2, last + j, cur + j, Rc[j], width, 3, j, 8);
339  Rc[j] = last[j];
340  }
341  last = cur;
342  cur += s->picture.linesize[0];
343  }
344  }
345 
346  av_free(zero);
347  av_free(state);
348 
349  // the specification says that after doing 0xff escaping unused bits in the
350  // last byte must be set to 0, so just append 7 "optional" zero-bits to
351  // avoid special-casing.
352  put_bits(&pb2, 7, 0);
353  size = put_bits_count(&pb2);
354  flush_put_bits(&pb2);
355  /* do escape coding */
356  init_get_bits(&gb, buf2, size);
357  size -= 7;
358  while(get_bits_count(&gb) < size){
359  int v;
360  v = get_bits(&gb, 8);
361  put_bits(&pb, 8, v);
362  if(v == 0xFF){
363  v = get_bits(&gb, 7);
364  put_bits(&pb, 8, v);
365  }
366  }
368  av_free(buf2);
369 
370  /* End of image */
371  put_marker(&pb, EOI);
372  flush_put_bits(&pb);
373 
374  emms_c();
375 
376  pkt->size = put_bits_count(&pb) >> 3;
377  pkt->flags |= AV_PKT_FLAG_KEY;
378  *got_packet = 1;
379  return 0;
380 }
381 
384 
385  c->avctx = ctx;
386  ctx->coded_frame = &c->picture;
387 
389  av_log(ctx, AV_LOG_ERROR, "Only grayscale and RGB24/BGR24 images are supported\n");
390  return -1;
391  }
392  return 0;
393 }
394 
395 AVCodec ff_jpegls_encoder = { //FIXME avoid MPV_* lossless JPEG should not need them
396  .name = "jpegls",
397  .type = AVMEDIA_TYPE_VIDEO,
398  .id = AV_CODEC_ID_JPEGLS,
399  .priv_data_size = sizeof(JpeglsContext),
400  .init = encode_init_ls,
401  .encode2 = encode_picture_ls,
402  .pix_fmts = (const enum AVPixelFormat[]){
405  },
406  .long_name = NULL_IF_CONFIG_SMALL("JPEG-LS"),
407 };
int reset
Definition: jpegls.h:42
const uint8_t ff_log2_run[41]
Definition: bitstream.c:36
void * av_malloc(size_t size)
Allocate a block of size bytes with alignment suitable for all memory accesses (including vectors if ...
Definition: mem.c:61
int T2
Definition: jpegls.h:40
int size
This structure describes decoded (raw) audio or video data.
Definition: avcodec.h:989
AVCodec ff_jpegls_encoder
Definition: jpeglsenc.c:395
JPEG-LS.
Definition: mjpeg.h:107
static unsigned int get_bits(GetBitContext *s, int n)
Read 1-25 bits.
Definition: get_bits.h:237
packed RGB 8:8:8, 24bpp, RGBRGB...
Definition: pixfmt.h:67
static void ls_encode_regular(JLSState *state, PutBitContext *pb, int Q, int err)
Encode error from regular symbol.
Definition: jpeglsenc.c:41
AVFrame * coded_frame
the picture in the bitstream
Definition: avcodec.h:2725
int size
Definition: avcodec.h:916
int C[365]
Definition: jpegls.h:41
void avpriv_align_put_bits(PutBitContext *s)
Pad the bitstream with zeros up to the next byte boundary.
Definition: bitstream.c:45
enum AVPixelFormat pix_fmt
Pixel format, see AV_PIX_FMT_xxx.
Definition: avcodec.h:1533
Definition: mjpeg.h:74
int twonear
Definition: jpegls.h:43
uint8_t run
Definition: svq3.c:124
AVFrame picture
Definition: jpegls.h:36
int limit
Definition: jpegls.h:42
int stride
Definition: mace.c:144
AVCodec.
Definition: avcodec.h:2960
MJPEG encoder and decoder.
int bpp
Definition: jpegls.h:42
int maxval
Definition: jpegls.h:42
uint8_t bits
Definition: crc.c:31
uint8_t
#define emms_c()
Definition: internal.h:145
#define R
Definition: dsputil.c:1899
uint8_t * data
Definition: avcodec.h:915
static int get_bits_count(const GetBitContext *s)
Definition: get_bits.h:192
static void ls_encode_line(JLSState *state, PutBitContext *pb, void *last, void *cur, int last2, int w, int stride, int comp, int bits)
Encode one line of image.
Definition: jpeglsenc.c:116
bitstream reader API header.
static float t
static int init(AVCodecParserContext *s)
Definition: h264_parser.c:335
static void ls_encode_run(JLSState *state, PutBitContext *pb, int run, int comp, int trail)
Encode run value as specified by JPEG-LS standard.
Definition: jpeglsenc.c:96
void av_free(void *ptr)
Free a memory block which has been allocated with av_malloc(z)() or av_realloc(). ...
Definition: mem.c:139
static void ls_store_lse(JLSState *state, PutBitContext *pb)
Definition: jpeglsenc.c:211
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
Definition: internal.h:88
#define zero
Definition: regdef.h:64
int run_index[3]
Definition: jpegls.h:44
void ff_jpegls_reset_coding_parameters(JLSState *s, int reset_all)
Calculate JPEG-LS codec values.
Definition: jpegls.c:59
void av_log(void *avcl, int level, const char *fmt,...)
Definition: log.c:146
const char * name
Name of the codec implementation.
Definition: avcodec.h:2967
static int ff_jpegls_update_state_regular(JLSState *state, int Q, int err)
Definition: jpegls.h:89
int B[367]
Definition: jpegls.h:41
static int encode_picture_ls(AVCodecContext *avctx, AVPacket *pkt, const AVFrame *pict, int *got_packet)
Definition: jpeglsenc.c:230
static void put_bits(PutBitContext *s, int n, unsigned int value)
Write up to 31 bits into a bitstream.
Definition: put_bits.h:136
int flags
A combination of AV_PKT_FLAG values.
Definition: avcodec.h:921
int near
Definition: jpegls.h:43
static int put_bits_count(PutBitContext *s)
Definition: put_bits.h:70
enum AVPictureType pict_type
Picture type of the frame, see ?_TYPE below.
Definition: avcodec.h:1065
Definition: mjpeg.h:76
int width
picture width / height.
Definition: avcodec.h:1508
int ff_alloc_packet(AVPacket *avpkt, int size)
Check AVPacket size and/or allocate data.
Definition: utils.c:880
static void ls_encode_runterm(JLSState *state, PutBitContext *pb, int RItype, int err, int limit_add)
Encode error from run termination.
Definition: jpeglsenc.c:66
packed RGB 8:8:8, 24bpp, BGRBGR...
Definition: pixfmt.h:68
static void set_ur_golomb_jpegls(PutBitContext *pb, int i, int k, int limit, int esc_len)
write unsigned golomb rice code (jpegls).
Definition: golomb.h:499
Definition: mjpeg.h:75
static const float pred[4]
Definition: siprdata.h:259
int qbpp
Definition: jpegls.h:42
static int width
Definition: utils.c:156
external API header
AVCodecContext * avctx
Definition: jpegls.h:35
int linesize[AV_NUM_DATA_POINTERS]
Size, in bytes, of the data for each picture/channel plane.
Definition: avcodec.h:1008
main external API structure.
Definition: avcodec.h:1339
int A[367]
Definition: jpegls.h:41
#define W(a, i, v)
Definition: jpegls.h:110
JPEG-LS common code.
int T1
Definition: jpegls.h:40
static int init_get_bits(GetBitContext *s, const uint8_t *buffer, int bit_size)
Initialize GetBitContext.
Definition: get_bits.h:372
int range
Definition: jpegls.h:42
int N[367]
Definition: jpegls.h:41
#define mid_pred
Definition: mathops.h:94
static av_cold int encode_init_ls(AVCodecContext *ctx)
Definition: jpeglsenc.c:382
static uint32_t state
Definition: trasher.c:27
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
Definition: avcodec.h:997
Y , 8bpp.
Definition: pixfmt.h:73
common internal api header.
static void flush_put_bits(PutBitContext *s)
Pad the end of the output stream with zeros.
Definition: put_bits.h:86
int prediction_method
prediction method (needed for huffyuv)
Definition: avcodec.h:1705
static void init_put_bits(PutBitContext *s, uint8_t *buffer, int buffer_size)
Initialize the PutBitContext s.
Definition: put_bits.h:52
DSP utils.
void * priv_data
Definition: avcodec.h:1382
static void put_marker(PutBitContext *p, int code)
Definition: mjpeg.h:122
static int ff_jpegls_quantize(JLSState *s, int v)
Calculate quantized gradient value, used for context determination.
Definition: jpegls.h:57
JPEG-LS extension parameters.
Definition: mjpeg.h:108
int key_frame
1 -> keyframe, 0-> not
Definition: avcodec.h:1058
static void comp(unsigned char *dst, int dst_stride, unsigned char *src, int src_stride, int add)
Definition: eamad.c:74
int T3
Definition: jpegls.h:40
void ff_jpegls_init_state(JLSState *state)
Calculate initial JPEG-LS parameters.
Definition: jpegls.c:30
exp golomb vlc stuff
AVPixelFormat
Pixel format.
Definition: pixfmt.h:63
This structure stores compressed data.
Definition: avcodec.h:898
void * av_mallocz(size_t size)
Allocate a block of size bytes with alignment suitable for all memory accesses (including vectors if ...
Definition: mem.c:158
static void ff_jpegls_downscale_state(JLSState *state, int Q)
Definition: jpegls.h:80
struct JpeglsContext JpeglsContext