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