FFmpeg  2.6.9
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Modules 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  const AVFrame *const p = pict;
253  const int near = avctx->prediction_method;
254  PutBitContext pb, pb2;
255  GetBitContext gb;
256  uint8_t *buf2 = NULL;
257  uint8_t *zero, *cur, *last;
258  JLSState *state = NULL;
259  int i, size, ret;
260  int comps;
261 
262  if (avctx->pix_fmt == AV_PIX_FMT_GRAY8 ||
263  avctx->pix_fmt == AV_PIX_FMT_GRAY16)
264  comps = 1;
265  else
266  comps = 3;
267 
268  if ((ret = ff_alloc_packet2(avctx, pkt, avctx->width *avctx->height * comps * 4 +
269  FF_MIN_BUFFER_SIZE)) < 0)
270  return ret;
271 
272  buf2 = av_malloc(pkt->size);
273  if (!buf2)
274  goto fail;
275 
276  init_put_bits(&pb, pkt->data, pkt->size);
277  init_put_bits(&pb2, buf2, pkt->size);
278 
279  /* write our own JPEG header, can't use mjpeg_picture_header */
280  put_marker(&pb, SOI);
281  put_marker(&pb, SOF48);
282  put_bits(&pb, 16, 8 + comps * 3); // header size depends on components
283  put_bits(&pb, 8, (avctx->pix_fmt == AV_PIX_FMT_GRAY16) ? 16 : 8); // bpp
284  put_bits(&pb, 16, avctx->height);
285  put_bits(&pb, 16, avctx->width);
286  put_bits(&pb, 8, comps); // components
287  for (i = 1; i <= comps; i++) {
288  put_bits(&pb, 8, i); // component ID
289  put_bits(&pb, 8, 0x11); // subsampling: none
290  put_bits(&pb, 8, 0); // Tiq, used by JPEG-LS ext
291  }
292 
293  put_marker(&pb, SOS);
294  put_bits(&pb, 16, 6 + comps * 2);
295  put_bits(&pb, 8, comps);
296  for (i = 1; i <= comps; i++) {
297  put_bits(&pb, 8, i); // component ID
298  put_bits(&pb, 8, 0); // mapping index: none
299  }
300  put_bits(&pb, 8, near);
301  put_bits(&pb, 8, (comps > 1) ? 1 : 0); // interleaving: 0 - plane, 1 - line
302  put_bits(&pb, 8, 0); // point transform: none
303 
304  state = av_mallocz(sizeof(JLSState));
305  if (!state)
306  goto fail;
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  goto fail;
318 
319  last = zero;
320  cur = p->data[0];
321  if (avctx->pix_fmt == AV_PIX_FMT_GRAY8) {
322  int t = 0;
323 
324  for (i = 0; i < avctx->height; i++) {
325  ls_encode_line(state, &pb2, last, cur, t, avctx->width, 1, 0, 8);
326  t = last[0];
327  last = cur;
328  cur += p->linesize[0];
329  }
330  } else if (avctx->pix_fmt == AV_PIX_FMT_GRAY16) {
331  int t = 0;
332 
333  for (i = 0; i < avctx->height; i++) {
334  ls_encode_line(state, &pb2, last, cur, t, avctx->width, 1, 0, 16);
335  t = *((uint16_t *)last);
336  last = cur;
337  cur += p->linesize[0];
338  }
339  } else if (avctx->pix_fmt == AV_PIX_FMT_RGB24) {
340  int j, width;
341  int Rc[3] = { 0, 0, 0 };
342 
343  width = avctx->width * 3;
344  for (i = 0; i < avctx->height; i++) {
345  for (j = 0; j < 3; j++) {
346  ls_encode_line(state, &pb2, last + j, cur + j, Rc[j],
347  width, 3, j, 8);
348  Rc[j] = last[j];
349  }
350  last = cur;
351  cur += p->linesize[0];
352  }
353  } else if (avctx->pix_fmt == AV_PIX_FMT_BGR24) {
354  int j, width;
355  int Rc[3] = { 0, 0, 0 };
356 
357  width = avctx->width * 3;
358  for (i = 0; i < avctx->height; i++) {
359  for (j = 2; j >= 0; j--) {
360  ls_encode_line(state, &pb2, last + j, cur + j, Rc[j],
361  width, 3, j, 8);
362  Rc[j] = last[j];
363  }
364  last = cur;
365  cur += p->linesize[0];
366  }
367  }
368 
369  av_freep(&zero);
370  av_freep(&state);
371 
372  /* the specification says that after doing 0xff escaping unused bits in
373  * the last byte must be set to 0, so just append 7 "optional" zero-bits
374  * to avoid special-casing. */
375  put_bits(&pb2, 7, 0);
376  size = put_bits_count(&pb2);
377  flush_put_bits(&pb2);
378  /* do escape coding */
379  init_get_bits(&gb, buf2, size);
380  size -= 7;
381  while (get_bits_count(&gb) < size) {
382  int v;
383  v = get_bits(&gb, 8);
384  put_bits(&pb, 8, v);
385  if (v == 0xFF) {
386  v = get_bits(&gb, 7);
387  put_bits(&pb, 8, v);
388  }
389  }
391  av_freep(&buf2);
392 
393  /* End of image */
394  put_marker(&pb, EOI);
395  flush_put_bits(&pb);
396 
397  emms_c();
398 
399  pkt->size = put_bits_count(&pb) >> 3;
400  pkt->flags |= AV_PKT_FLAG_KEY;
401  *got_packet = 1;
402  return 0;
403 fail:
404  av_freep(&buf2);
405  av_freep(&state);
406 
407  return AVERROR(ENOMEM);
408 }
409 
411 {
412  av_frame_free(&avctx->coded_frame);
413  return 0;
414 }
415 
417 {
418  ctx->coded_frame = av_frame_alloc();
419  if (!ctx->coded_frame)
420  return AVERROR(ENOMEM);
421 
423  ctx->coded_frame->key_frame = 1;
424 
425  if (ctx->pix_fmt != AV_PIX_FMT_GRAY8 &&
426  ctx->pix_fmt != AV_PIX_FMT_GRAY16 &&
427  ctx->pix_fmt != AV_PIX_FMT_RGB24 &&
428  ctx->pix_fmt != AV_PIX_FMT_BGR24) {
429  av_log(ctx, AV_LOG_ERROR,
430  "Only grayscale and RGB24/BGR24 images are supported\n");
431  return -1;
432  }
433  return 0;
434 }
435 
437  .name = "jpegls",
438  .long_name = NULL_IF_CONFIG_SMALL("JPEG-LS"),
439  .type = AVMEDIA_TYPE_VIDEO,
440  .id = AV_CODEC_ID_JPEGLS,
441  .init = encode_init_ls,
442  .close = encode_close,
444  .encode2 = encode_picture_ls,
445  .pix_fmts = (const enum AVPixelFormat[]) {
449  },
450 };
int reset
Definition: jpegls.h:42
#define NULL
Definition: coverity.c:32
const uint8_t ff_log2_run[41]
Definition: bitstream.c:38
int ff_alloc_packet2(AVCodecContext *avctx, AVPacket *avpkt, int64_t size)
Check AVPacket size and/or allocate data.
Definition: utils.c:1724
const char const char void * val
Definition: avisynth_c.h:672
float v
int T2
Definition: jpegls.h:40
This structure describes decoded (raw) audio or video data.
Definition: frame.h:163
AVCodec ff_jpegls_encoder
Definition: jpeglsenc.c:436
JPEG-LS.
Definition: mjpeg.h:108
static void put_bits(Jpeg2000EncoderContext *s, int val, int n)
put n times val bit
Definition: j2kenc.c:198
static unsigned int get_bits(GetBitContext *s, int n)
Read 1-25 bits.
Definition: get_bits.h:260
packed RGB 8:8:8, 24bpp, RGBRGB...
Definition: pixfmt.h:70
static void ls_encode_regular(JLSState *state, PutBitContext *pb, int Q, int err)
Encode error from regular symbol.
Definition: jpeglsenc.c:40
else temp
Definition: vf_mcdeint.c:257
AVFrame * coded_frame
the picture in the bitstream
Definition: avcodec.h:2743
static av_cold int encode_close(AVCodecContext *avctx)
Definition: jpeglsenc.c:410
int size
Definition: avcodec.h:1161
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:47
enum AVPixelFormat pix_fmt
Pixel format, see AV_PIX_FMT_xxx.
Definition: avcodec.h:1442
Definition: mjpeg.h:75
int twonear
Definition: jpegls.h:43
uint8_t run
Definition: svq3.c:149
static AVPacket pkt
int limit
Definition: jpegls.h:42
AVCodec.
Definition: avcodec.h:3173
MJPEG encoder and decoder.
int bpp
Definition: jpegls.h:42
int maxval
Definition: jpegls.h:42
uint8_t bits
Definition: crc.c:295
uint8_t
#define av_cold
Definition: attributes.h:74
#define av_malloc(s)
AVFrame * av_frame_alloc(void)
Allocate an AVFrame and set its fields to default values.
Definition: frame.c:135
#define CODEC_CAP_INTRA_ONLY
Codec is intra only.
Definition: avcodec.h:884
#define emms_c()
Definition: internal.h:50
uint8_t * data
Definition: avcodec.h:1160
static int get_bits_count(const GetBitContext *s)
Definition: get_bits.h:212
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:123
bitstream reader API header.
Definition: vf_geq.c:45
ptrdiff_t size
Definition: opengl_enc.c:101
#define av_log(a,...)
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:101
#define AV_PKT_FLAG_KEY
The packet contains a keyframe.
Definition: avcodec.h:1206
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:175
#define AVERROR(e)
Definition: error.h:43
void av_frame_free(AVFrame **frame)
Free the frame and any dynamically allocated objects in it, e.g.
Definition: frame.c:148
static void ls_store_lse(JLSState *state, PutBitContext *pb)
Definition: jpeglsenc.c:226
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
Definition: internal.h:180
#define zero
Definition: regdef.h:64
void ff_jpegls_reset_coding_parameters(JLSState *s, int reset_all)
Calculate JPEG-LS codec values.
Definition: jpegls.c:61
const char * name
Name of the codec implementation.
Definition: avcodec.h:3180
static int ff_jpegls_update_state_regular(JLSState *state, int Q, int err)
Definition: jpegls.h:97
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:249
Libavcodec external API header.
int flags
A combination of AV_PKT_FLAG values.
Definition: avcodec.h:1166
int near
Definition: jpegls.h:43
static int put_bits_count(PutBitContext *s)
Definition: put_bits.h:85
enum AVPictureType pict_type
Picture type of the frame.
Definition: frame.h:234
#define AV_PIX_FMT_GRAY16
Definition: pixfmt.h:342
Definition: mjpeg.h:77
#define FF_MIN_BUFFER_SIZE
minimum encoding buffer size Used to avoid some checks during header writing.
Definition: avcodec.h:635
ret
Definition: avfilter.c:974
int width
picture width / height.
Definition: avcodec.h:1412
#define FFABS(a)
Absolute value, Note, INT_MIN / INT64_MIN result in undefined behavior as they are not representable ...
Definition: common.h:68
static void ls_encode_runterm(JLSState *state, PutBitContext *pb, int RItype, int err, int limit_add)
Encode error from run termination.
Definition: jpeglsenc.c:68
packed RGB 8:8:8, 24bpp, BGRBGR...
Definition: pixfmt.h:71
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:544
Definition: mjpeg.h:76
static const float pred[4]
Definition: siprdata.h:259
int qbpp
Definition: jpegls.h:42
int linesize[AV_NUM_DATA_POINTERS]
For video, size in bytes of each picture line.
Definition: frame.h:191
main external API structure.
Definition: avcodec.h:1239
int A[367]
Definition: jpegls.h:41
#define W(a, i, v)
Definition: jpegls.h:122
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:410
int range
Definition: jpegls.h:42
int N[367]
Definition: jpegls.h:41
#define mid_pred
Definition: mathops.h:96
static av_cold int encode_init_ls(AVCodecContext *ctx)
Definition: jpeglsenc.c:416
static uint32_t state
Definition: trasher.c:27
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
Definition: frame.h:174
GLint GLenum GLboolean GLsizei stride
Definition: opengl_enc.c:105
Y , 8bpp.
Definition: pixfmt.h:76
common internal api header.
static void flush_put_bits(PutBitContext *s)
Pad the end of the output stream with zeros.
Definition: put_bits.h:101
#define CODEC_CAP_FRAME_THREADS
Codec supports frame-level multithreading.
Definition: avcodec.h:864
int prediction_method
prediction method (needed for huffyuv)
Definition: avcodec.h:1602
static void init_put_bits(PutBitContext *s, uint8_t *buffer, int buffer_size)
Initialize the PutBitContext s.
Definition: put_bits.h:48
static void put_marker(PutBitContext *p, int code)
Definition: mjpeg.h:123
static int ff_jpegls_quantize(JLSState *s, int v)
Calculate quantized gradient value, used for context determination.
Definition: jpegls.h:55
JPEG-LS extension parameters.
Definition: mjpeg.h:109
int key_frame
1 -> keyframe, 0-> not
Definition: frame.h:229
#define av_freep(p)
static void comp(unsigned char *dst, int dst_stride, unsigned char *src, int src_stride, int add)
Definition: eamad.c:83
int T3
Definition: jpegls.h:40
void ff_jpegls_init_state(JLSState *state)
Calculate initial JPEG-LS parameters.
Definition: jpegls.c:30
#define stride
exp golomb vlc stuff
AVPixelFormat
Pixel format.
Definition: pixfmt.h:66
This structure stores compressed data.
Definition: avcodec.h:1137
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:250
int run_index[4]
Definition: jpegls.h:44
static void ff_jpegls_downscale_state(JLSState *state, int Q)
Definition: jpegls.h:87
static int width
bitstream writer API