FFmpeg  4.0
mjpegenc_common.c
Go to the documentation of this file.
1 /*
2  * lossless JPEG shared bits
3  * Copyright (c) 2000, 2001 Fabrice Bellard
4  * Copyright (c) 2003 Alex Beregszaszi
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 #include <stdint.h>
24 #include <string.h>
25 
26 #include "libavutil/common.h"
27 #include "libavutil/pixdesc.h"
28 #include "libavutil/pixfmt.h"
29 
30 #include "avcodec.h"
31 #include "idctdsp.h"
32 #include "jpegtables.h"
33 #include "put_bits.h"
34 #include "mjpegenc.h"
35 #include "mjpegenc_common.h"
36 #include "mjpegenc_huffman.h"
37 #include "mjpeg.h"
38 
39 av_cold void ff_init_uni_ac_vlc(const uint8_t huff_size_ac[256], uint8_t *uni_ac_vlc_len)
40 {
41  int i;
42 
43  for (i = 0; i < 128; i++) {
44  int level = i - 64;
45  int run;
46  if (!level)
47  continue;
48  for (run = 0; run < 64; run++) {
49  int len, code, nbits;
50  int alevel = FFABS(level);
51 
52  len = (run >> 4) * huff_size_ac[0xf0];
53 
54  nbits= av_log2_16bit(alevel) + 1;
55  code = ((15&run) << 4) | nbits;
56 
57  len += huff_size_ac[code] + nbits;
58 
59  uni_ac_vlc_len[UNI_AC_ENC_INDEX(run, i)] = len;
60  // We ignore EOB as its just a constant which does not change generally
61  }
62  }
63 }
64 
65 /* table_class: 0 = DC coef, 1 = AC coefs */
66 static int put_huffman_table(PutBitContext *p, int table_class, int table_id,
67  const uint8_t *bits_table, const uint8_t *value_table)
68 {
69  int n, i;
70 
71  put_bits(p, 4, table_class);
72  put_bits(p, 4, table_id);
73 
74  n = 0;
75  for(i=1;i<=16;i++) {
76  n += bits_table[i];
77  put_bits(p, 8, bits_table[i]);
78  }
79 
80  for(i=0;i<n;i++)
81  put_bits(p, 8, value_table[i]);
82 
83  return n + 17;
84 }
85 
87  ScanTable *intra_scantable,
88  uint16_t luma_intra_matrix[64],
89  uint16_t chroma_intra_matrix[64],
90  int hsample[3])
91 {
92  int i, j, size;
93  uint8_t *ptr;
95 
96  /* Since avctx->priv_data will point to LJpegEncContext in this case */
97  if (avctx->codec_id != AV_CODEC_ID_LJPEG)
98  s = avctx->priv_data;
99 
100  if (avctx->codec_id != AV_CODEC_ID_LJPEG) {
101  int matrix_count = 1 + !!memcmp(luma_intra_matrix,
102  chroma_intra_matrix,
103  sizeof(luma_intra_matrix[0]) * 64);
104  if (s && s->force_duplicated_matrix)
105  matrix_count = 2;
106  /* quant matrixes */
107  put_marker(p, DQT);
108  put_bits(p, 16, 2 + matrix_count * (1 + 64));
109  put_bits(p, 4, 0); /* 8 bit precision */
110  put_bits(p, 4, 0); /* table 0 */
111  for(i=0;i<64;i++) {
112  j = intra_scantable->permutated[i];
113  put_bits(p, 8, luma_intra_matrix[j]);
114  }
115 
116  if (matrix_count > 1) {
117  put_bits(p, 4, 0); /* 8 bit precision */
118  put_bits(p, 4, 1); /* table 1 */
119  for(i=0;i<64;i++) {
120  j = intra_scantable->permutated[i];
121  put_bits(p, 8, chroma_intra_matrix[j]);
122  }
123  }
124  }
125 
126  if(avctx->active_thread_type & FF_THREAD_SLICE){
127  put_marker(p, DRI);
128  put_bits(p, 16, 4);
129  put_bits(p, 16, (avctx->width-1)/(8*hsample[0]) + 1);
130  }
131 
132  /* huffman table */
133  put_marker(p, DHT);
134  flush_put_bits(p);
135  ptr = put_bits_ptr(p);
136  put_bits(p, 16, 0); /* patched later */
137  size = 2;
138 
139  // Only MJPEG can have a variable Huffman variable. All other
140  // formats use the default Huffman table.
141  if (s && s->huffman == HUFFMAN_TABLE_OPTIMAL) {
142  size += put_huffman_table(p, 0, 0, s->mjpeg_ctx->bits_dc_luminance,
144  size += put_huffman_table(p, 0, 1, s->mjpeg_ctx->bits_dc_chrominance,
146 
147  size += put_huffman_table(p, 1, 0, s->mjpeg_ctx->bits_ac_luminance,
149  size += put_huffman_table(p, 1, 1, s->mjpeg_ctx->bits_ac_chrominance,
151  } else {
156 
161  }
162  AV_WB16(ptr, size);
163 }
164 
166 {
167  int size;
168  uint8_t *ptr;
169 
170  if (avctx->sample_aspect_ratio.num > 0 && avctx->sample_aspect_ratio.den > 0) {
171  AVRational sar = avctx->sample_aspect_ratio;
172 
173  if (sar.num > 65535 || sar.den > 65535) {
174  if (!av_reduce(&sar.num, &sar.den, avctx->sample_aspect_ratio.num, avctx->sample_aspect_ratio.den, 65535))
175  av_log(avctx, AV_LOG_WARNING,
176  "Cannot store exact aspect ratio %d:%d\n",
177  avctx->sample_aspect_ratio.num,
178  avctx->sample_aspect_ratio.den);
179  }
180 
181  /* JFIF header */
182  put_marker(p, APP0);
183  put_bits(p, 16, 16);
184  avpriv_put_string(p, "JFIF", 1); /* this puts the trailing zero-byte too */
185  /* The most significant byte is used for major revisions, the least
186  * significant byte for minor revisions. Version 1.02 is the current
187  * released revision. */
188  put_bits(p, 16, 0x0102);
189  put_bits(p, 8, 0); /* units type: 0 - aspect ratio */
190  put_bits(p, 16, sar.num);
191  put_bits(p, 16, sar.den);
192  put_bits(p, 8, 0); /* thumbnail width */
193  put_bits(p, 8, 0); /* thumbnail height */
194  }
195 
196  /* comment */
197  if (!(avctx->flags & AV_CODEC_FLAG_BITEXACT)) {
198  put_marker(p, COM);
199  flush_put_bits(p);
200  ptr = put_bits_ptr(p);
201  put_bits(p, 16, 0); /* patched later */
203  size = strlen(LIBAVCODEC_IDENT)+3;
204  AV_WB16(ptr, size);
205  }
206 
207  if (((avctx->pix_fmt == AV_PIX_FMT_YUV420P ||
208  avctx->pix_fmt == AV_PIX_FMT_YUV422P ||
209  avctx->pix_fmt == AV_PIX_FMT_YUV444P) && avctx->color_range != AVCOL_RANGE_JPEG)
210  || avctx->color_range == AVCOL_RANGE_MPEG) {
211  put_marker(p, COM);
212  flush_put_bits(p);
213  ptr = put_bits_ptr(p);
214  put_bits(p, 16, 0); /* patched later */
215  avpriv_put_string(p, "CS=ITU601", 1);
216  size = strlen("CS=ITU601")+3;
217  AV_WB16(ptr, size);
218  }
219 }
220 
221 void ff_mjpeg_init_hvsample(AVCodecContext *avctx, int hsample[4], int vsample[4])
222 {
223  int chroma_h_shift, chroma_v_shift;
224 
225  av_pix_fmt_get_chroma_sub_sample(avctx->pix_fmt, &chroma_h_shift,
226  &chroma_v_shift);
227  if (avctx->codec->id == AV_CODEC_ID_LJPEG &&
228  ( avctx->pix_fmt == AV_PIX_FMT_BGR0
229  || avctx->pix_fmt == AV_PIX_FMT_BGRA
230  || avctx->pix_fmt == AV_PIX_FMT_BGR24)) {
231  vsample[0] = hsample[0] =
232  vsample[1] = hsample[1] =
233  vsample[2] = hsample[2] =
234  vsample[3] = hsample[3] = 1;
235  } else if (avctx->pix_fmt == AV_PIX_FMT_YUV444P || avctx->pix_fmt == AV_PIX_FMT_YUVJ444P) {
236  vsample[0] = vsample[1] = vsample[2] = 2;
237  hsample[0] = hsample[1] = hsample[2] = 1;
238  } else {
239  vsample[0] = 2;
240  vsample[1] = 2 >> chroma_v_shift;
241  vsample[2] = 2 >> chroma_v_shift;
242  hsample[0] = 2;
243  hsample[1] = 2 >> chroma_h_shift;
244  hsample[2] = 2 >> chroma_h_shift;
245  }
246 }
247 
249  ScanTable *intra_scantable, int pred,
250  uint16_t luma_intra_matrix[64],
251  uint16_t chroma_intra_matrix[64])
252 {
253  const int lossless = avctx->codec_id != AV_CODEC_ID_MJPEG && avctx->codec_id != AV_CODEC_ID_AMV;
254  int hsample[4], vsample[4];
255  int i;
256  int components = 3 + (avctx->pix_fmt == AV_PIX_FMT_BGRA);
257  int chroma_matrix = !!memcmp(luma_intra_matrix,
258  chroma_intra_matrix,
259  sizeof(luma_intra_matrix[0])*64);
260 
261  ff_mjpeg_init_hvsample(avctx, hsample, vsample);
262 
263  put_marker(pb, SOI);
264 
265  // hack for AMV mjpeg format
266  if(avctx->codec_id == AV_CODEC_ID_AMV) goto end;
267 
268  jpeg_put_comments(avctx, pb);
269 
270  jpeg_table_header(avctx, pb, intra_scantable, luma_intra_matrix, chroma_intra_matrix, hsample);
271 
272  switch (avctx->codec_id) {
273  case AV_CODEC_ID_MJPEG: put_marker(pb, SOF0 ); break;
274  case AV_CODEC_ID_LJPEG: put_marker(pb, SOF3 ); break;
275  default: av_assert0(0);
276  }
277 
278  put_bits(pb, 16, 17);
279  if (lossless && ( avctx->pix_fmt == AV_PIX_FMT_BGR0
280  || avctx->pix_fmt == AV_PIX_FMT_BGRA
281  || avctx->pix_fmt == AV_PIX_FMT_BGR24))
282  put_bits(pb, 8, 9); /* 9 bits/component RCT */
283  else
284  put_bits(pb, 8, 8); /* 8 bits/component */
285  put_bits(pb, 16, avctx->height);
286  put_bits(pb, 16, avctx->width);
287  put_bits(pb, 8, components); /* 3 or 4 components */
288 
289  /* Y component */
290  put_bits(pb, 8, 1); /* component number */
291  put_bits(pb, 4, hsample[0]); /* H factor */
292  put_bits(pb, 4, vsample[0]); /* V factor */
293  put_bits(pb, 8, 0); /* select matrix */
294 
295  /* Cb component */
296  put_bits(pb, 8, 2); /* component number */
297  put_bits(pb, 4, hsample[1]); /* H factor */
298  put_bits(pb, 4, vsample[1]); /* V factor */
299  put_bits(pb, 8, lossless ? 0 : chroma_matrix); /* select matrix */
300 
301  /* Cr component */
302  put_bits(pb, 8, 3); /* component number */
303  put_bits(pb, 4, hsample[2]); /* H factor */
304  put_bits(pb, 4, vsample[2]); /* V factor */
305  put_bits(pb, 8, lossless ? 0 : chroma_matrix); /* select matrix */
306 
307  if (components == 4) {
308  put_bits(pb, 8, 4); /* component number */
309  put_bits(pb, 4, hsample[3]); /* H factor */
310  put_bits(pb, 4, vsample[3]); /* V factor */
311  put_bits(pb, 8, 0); /* select matrix */
312  }
313 
314  /* scan header */
315  put_marker(pb, SOS);
316  put_bits(pb, 16, 6 + 2*components); /* length */
317  put_bits(pb, 8, components); /* 3 components */
318 
319  /* Y component */
320  put_bits(pb, 8, 1); /* index */
321  put_bits(pb, 4, 0); /* DC huffman table index */
322  put_bits(pb, 4, 0); /* AC huffman table index */
323 
324  /* Cb component */
325  put_bits(pb, 8, 2); /* index */
326  put_bits(pb, 4, 1); /* DC huffman table index */
327  put_bits(pb, 4, lossless ? 0 : 1); /* AC huffman table index */
328 
329  /* Cr component */
330  put_bits(pb, 8, 3); /* index */
331  put_bits(pb, 4, 1); /* DC huffman table index */
332  put_bits(pb, 4, lossless ? 0 : 1); /* AC huffman table index */
333 
334  if (components == 4) {
335  /* Alpha component */
336  put_bits(pb, 8, 4); /* index */
337  put_bits(pb, 4, 0); /* DC huffman table index */
338  put_bits(pb, 4, 0); /* AC huffman table index */
339  }
340 
341  put_bits(pb, 8, lossless ? pred : 0); /* Ss (not used) */
342 
343  switch (avctx->codec_id) {
344  case AV_CODEC_ID_MJPEG: put_bits(pb, 8, 63); break; /* Se (not used) */
345  case AV_CODEC_ID_LJPEG: put_bits(pb, 8, 0); break; /* not used */
346  default: av_assert0(0);
347  }
348 
349  put_bits(pb, 8, 0); /* Ah/Al (not used) */
350 
351 end:
352  if (!lossless) {
353  MpegEncContext *s = avctx->priv_data;
354  av_assert0(avctx->codec->priv_data_size == sizeof(MpegEncContext));
355 
356  s->esc_pos = put_bits_count(pb) >> 3;
357  for(i=1; i<s->slice_context_count; i++)
358  s->thread_context[i]->esc_pos = 0;
359  }
360 }
361 
362 /**
363  * Encodes and outputs the entire frame in the JPEG format.
364  *
365  * @param s The MpegEncContext.
366  */
368 {
369  int i, nbits, code, table_id;
370  MJpegContext *m = s->mjpeg_ctx;
371  uint8_t *huff_size[4] = {m->huff_size_dc_luminance,
375  uint16_t *huff_code[4] = {m->huff_code_dc_luminance,
379  size_t total_bits = 0;
380  size_t bytes_needed;
381 
382  s->header_bits = get_bits_diff(s);
383  // Estimate the total size first
384  for (i = 0; i < m->huff_ncode; i++) {
385  table_id = m->huff_buffer[i].table_id;
386  code = m->huff_buffer[i].code;
387  nbits = code & 0xf;
388 
389  total_bits += huff_size[table_id][code] + nbits;
390  }
391 
392  bytes_needed = (total_bits + 7) / 8;
393  ff_mpv_reallocate_putbitbuffer(s, bytes_needed, bytes_needed);
394 
395  for (i = 0; i < m->huff_ncode; i++) {
396  table_id = m->huff_buffer[i].table_id;
397  code = m->huff_buffer[i].code;
398  nbits = code & 0xf;
399 
400  put_bits(&s->pb, huff_size[table_id][code], huff_code[table_id][code]);
401  if (nbits != 0) {
402  put_sbits(&s->pb, nbits, m->huff_buffer[i].mant);
403  }
404  }
405 
406  m->huff_ncode = 0;
407  s->i_tex_bits = get_bits_diff(s);
408 }
409 
411 {
412  int size;
413  int i, ff_count;
414  uint8_t *buf = pb->buf + start;
415  int align= (-(size_t)(buf))&3;
416  int pad = (-put_bits_count(pb))&7;
417 
418  if (pad)
419  put_bits(pb, pad, (1<<pad)-1);
420 
421  flush_put_bits(pb);
422  size = put_bits_count(pb) - start * 8;
423 
424  av_assert1((size&7) == 0);
425  size >>= 3;
426 
427  ff_count=0;
428  for(i=0; i<size && i<align; i++){
429  if(buf[i]==0xFF) ff_count++;
430  }
431  for(; i<size-15; i+=16){
432  int acc, v;
433 
434  v= *(uint32_t*)(&buf[i]);
435  acc= (((v & (v>>4))&0x0F0F0F0F)+0x01010101)&0x10101010;
436  v= *(uint32_t*)(&buf[i+4]);
437  acc+=(((v & (v>>4))&0x0F0F0F0F)+0x01010101)&0x10101010;
438  v= *(uint32_t*)(&buf[i+8]);
439  acc+=(((v & (v>>4))&0x0F0F0F0F)+0x01010101)&0x10101010;
440  v= *(uint32_t*)(&buf[i+12]);
441  acc+=(((v & (v>>4))&0x0F0F0F0F)+0x01010101)&0x10101010;
442 
443  acc>>=4;
444  acc+= (acc>>16);
445  acc+= (acc>>8);
446  ff_count+= acc&0xFF;
447  }
448  for(; i<size; i++){
449  if(buf[i]==0xFF) ff_count++;
450  }
451 
452  if(ff_count==0) return;
453 
454  flush_put_bits(pb);
455  skip_put_bytes(pb, ff_count);
456 
457  for(i=size-1; ff_count; i--){
458  int v= buf[i];
459 
460  if(v==0xFF){
461  buf[i+ff_count]= 0;
462  ff_count--;
463  }
464 
465  buf[i+ff_count]= v;
466  }
467 }
468 
469 /**
470  * Builds all 4 optimal Huffman tables.
471  *
472  * Uses the data stored in the JPEG buffer to compute the tables.
473  * Stores the Huffman tables in the bits_* and val_* arrays in the MJpegContext.
474  *
475  * @param m MJpegContext containing the JPEG buffer.
476  */
478 {
479  int i, table_id, code;
480 
481  MJpegEncHuffmanContext dc_luminance_ctx;
482  MJpegEncHuffmanContext dc_chrominance_ctx;
483  MJpegEncHuffmanContext ac_luminance_ctx;
484  MJpegEncHuffmanContext ac_chrominance_ctx;
485  MJpegEncHuffmanContext *ctx[4] = {&dc_luminance_ctx,
486  &dc_chrominance_ctx,
487  &ac_luminance_ctx,
488  &ac_chrominance_ctx};
489  for (i = 0; i < 4; i++) {
491  }
492  for (i = 0; i < m->huff_ncode; i++) {
493  table_id = m->huff_buffer[i].table_id;
494  code = m->huff_buffer[i].code;
495 
496  ff_mjpeg_encode_huffman_increment(ctx[table_id], code);
497  }
498 
499  ff_mjpeg_encode_huffman_close(&dc_luminance_ctx,
501  m->val_dc_luminance, 12);
502  ff_mjpeg_encode_huffman_close(&dc_chrominance_ctx,
504  m->val_dc_chrominance, 12);
505  ff_mjpeg_encode_huffman_close(&ac_luminance_ctx,
507  m->val_ac_luminance, 256);
508  ff_mjpeg_encode_huffman_close(&ac_chrominance_ctx,
510  m->val_ac_chrominance, 256);
511 
515  m->val_dc_luminance);
519  m->val_dc_chrominance);
523  m->val_ac_luminance);
527  m->val_ac_chrominance);
528 }
529 
530 /**
531  * Writes the complete JPEG frame when optimal huffman tables are enabled,
532  * otherwise writes the stuffing.
533  *
534  * Header + values + stuffing.
535  *
536  * @param s The MpegEncContext.
537  * @return int Error code, 0 if successful.
538  */
540 {
541  int i;
542  PutBitContext *pbc = &s->pb;
543  int mb_y = s->mb_y - !s->mb_x;
544  int ret;
545  MJpegContext *m;
546 
547  m = s->mjpeg_ctx;
548 
549  if (s->huffman == HUFFMAN_TABLE_OPTIMAL) {
551 
552  // Replace the VLCs with the optimal ones.
553  // The default ones may be used for trellis during quantization.
560 
564  }
565 
566  ret = ff_mpv_reallocate_putbitbuffer(s, put_bits_count(&s->pb) / 8 + 100,
567  put_bits_count(&s->pb) / 4 + 1000);
568 
569  if (ret < 0) {
570  av_log(s->avctx, AV_LOG_ERROR, "Buffer reallocation failed\n");
571  goto fail;
572  }
573 
574  ff_mjpeg_escape_FF(pbc, s->esc_pos);
575 
576  if((s->avctx->active_thread_type & FF_THREAD_SLICE) && mb_y < s->mb_height)
577  put_marker(pbc, RST0 + (mb_y&7));
578  s->esc_pos = put_bits_count(pbc) >> 3;
579 fail:
580 
581  for(i=0; i<3; i++)
582  s->last_dc[i] = 128 << s->intra_dc_precision;
583 
584  return ret;
585 }
586 
588 {
589  av_assert1((header_bits & 7) == 0);
590 
591  put_marker(pb, EOI);
592 }
593 
595  uint8_t *huff_size, uint16_t *huff_code)
596 {
597  int mant, nbits;
598 
599  if (val == 0) {
600  put_bits(pb, huff_size[0], huff_code[0]);
601  } else {
602  mant = val;
603  if (val < 0) {
604  val = -val;
605  mant--;
606  }
607 
608  nbits= av_log2_16bit(val) + 1;
609 
610  put_bits(pb, huff_size[nbits], huff_code[nbits]);
611 
612  put_sbits(pb, nbits, mant);
613  }
614 }
#define NULL
Definition: coverity.c:32
const struct AVCodec * codec
Definition: avcodec.h:1527
const char const char void * val
Definition: avisynth_c.h:771
const char * s
Definition: avisynth_c.h:768
Holds JPEG frame data and Huffman table data.
Definition: mjpegenc.h:59
int size
Definition: mjpeg.h:71
Definition: mjpeg.h:111
Definition: mjpeg.h:73
static void put_sbits(PutBitContext *pb, int n, int32_t value)
Definition: put_bits.h:240
planar YUV 4:4:4, 24bpp, (1 Cr & Cb sample per 1x1 Y samples)
Definition: pixfmt.h:67
static void put_bits(Jpeg2000EncoderContext *s, int val, int n)
put n times val bit
Definition: j2kenc.c:207
#define AV_LOG_WARNING
Something somehow does not look correct.
Definition: log.h:182
Definition: mjpeg.h:42
uint16_t chroma_intra_matrix[64]
Definition: mpegvideo.h:301
int acc
Definition: yuv2rgb.c:554
MJPEG encoder.
enum AVColorRange color_range
MPEG vs JPEG YUV range.
Definition: avcodec.h:2148
Scantable.
Definition: idctdsp.h:31
int num
Numerator.
Definition: rational.h:59
void ff_mjpeg_init_hvsample(AVCodecContext *avctx, int hsample[4], int vsample[4])
AVRational sample_aspect_ratio
sample aspect ratio (0 if unknown) That is the width of a pixel divided by the height of the pixel...
Definition: avcodec.h:1896
enum AVPixelFormat pix_fmt
Pixel format, see AV_PIX_FMT_xxx.
Definition: avcodec.h:1727
Definition: mjpeg.h:75
uint8_t permutated[64]
Definition: idctdsp.h:33
uint8_t run
Definition: svq3.c:206
uint8_t * intra_ac_vlc_length
Definition: mpegvideo.h:311
#define UNI_AC_ENC_INDEX(run, level)
Definition: mpegvideo.h:318
av_cold void ff_init_uni_ac_vlc(const uint8_t huff_size_ac[256], uint8_t *uni_ac_vlc_len)
MJPEG encoder and decoder.
uint16_t huff_code_dc_chrominance[12]
DC chrominance Huffman table codes.
Definition: mjpegenc.h:64
static void ff_mjpeg_build_optimal_huffman(MJpegContext *m)
Builds all 4 optimal Huffman tables.
#define av_assert0(cond)
assert() equivalent, that is always enabled.
Definition: avassert.h:37
void ff_mjpeg_build_huffman_codes(uint8_t *huff_size, uint16_t *huff_code, const uint8_t *bits_table, const uint8_t *val_table)
Definition: jpegtables.c:127
Definition: mjpeg.h:72
uint16_t huff_code_ac_luminance[256]
AC luminance Huffman table codes.
Definition: mjpegenc.h:67
uint8_t
#define av_cold
Definition: attributes.h:82
uint8_t val_ac_luminance[256]
AC luminance Huffman values.
Definition: mjpegenc.h:84
static av_cold int end(AVCodecContext *avctx)
Definition: avrndec.c:90
uint8_t val_dc_chrominance[12]
DC chrominance Huffman values.
Definition: mjpegenc.h:80
uint8_t uni_chroma_ac_vlc_len[64 *64 *2]
Storage for AC chrominance VLC (in MpegEncContext)
Definition: mjpegenc.h:74
int force_duplicated_matrix
Force duplication of mjpeg matrices, useful for rtp streaming.
Definition: mpegvideo.h:304
const uint8_t avpriv_mjpeg_bits_dc_luminance[17]
Definition: jpegtables.c:65
int intra_dc_precision
Definition: mpegvideo.h:463
uint8_t huff_size_dc_chrominance[12]
DC chrominance Huffman table size.
Definition: mjpegenc.h:63
void ff_mjpeg_encode_huffman_close(MJpegEncHuffmanContext *s, uint8_t bits[17], uint8_t val[], int max_nval)
Produces a Huffman encoding with a given input.
int av_reduce(int *dst_num, int *dst_den, int64_t num, int64_t den, int64_t max)
Reduce a fraction.
Definition: rational.c:35
#define AV_WB16(p, v)
Definition: intreadwrite.h:405
#define av_log(a,...)
uint8_t table_id
The Huffman table id associated with the data.
Definition: mjpegenc.h:51
static int get_bits_diff(MpegEncContext *s)
Definition: mpegvideo.h:754
uint16_t huff_code_ac_chrominance[256]
AC chrominance Huffman table codes.
Definition: mjpegenc.h:69
enum AVCodecID id
Definition: avcodec.h:3422
int slice_context_count
number of used thread_contexts
Definition: mpegvideo.h:156
uint8_t huff_size_ac_luminance[256]
AC luminance Huffman table size.
Definition: mjpegenc.h:66
uint16_t mant
The mantissa.
Definition: mjpegenc.h:53
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:176
static uint8_t * put_bits_ptr(PutBitContext *s)
Return the pointer to the byte where the bitstream writer will put the next bit.
Definition: put_bits.h:324
int last_dc[3]
last DC values for MPEG-1
Definition: mpegvideo.h:185
const uint8_t avpriv_mjpeg_bits_dc_chrominance[17]
Definition: jpegtables.c:70
int av_pix_fmt_get_chroma_sub_sample(enum AVPixelFormat pix_fmt, int *h_shift, int *v_shift)
Utility function to access log2_chroma_w log2_chroma_h from the pixel format AVPixFmtDescriptor.
Definition: pixdesc.c:2391
int active_thread_type
Which multithreading methods are in use by the codec.
Definition: avcodec.h:2788
packed BGRA 8:8:8:8, 32bpp, BGRABGRA...
Definition: pixfmt.h:91
uint8_t * intra_chroma_ac_vlc_length
Definition: mpegvideo.h:313
int flags
AV_CODEC_FLAG_*.
Definition: avcodec.h:1598
uint8_t * buf
Definition: put_bits.h:38
uint8_t huff_size_ac_chrominance[256]
AC chrominance Huffman table size.
Definition: mjpegenc.h:68
void ff_mjpeg_encode_picture_trailer(PutBitContext *pb, int header_bits)
uint8_t bits_dc_luminance[17]
DC luminance Huffman bits.
Definition: mjpegenc.h:77
#define fail()
Definition: checkasm.h:116
Definition: mjpeg.h:39
Definition: mjpeg.h:70
static int put_bits_count(PutBitContext *s)
Definition: put_bits.h:85
planar YUV 4:2:2, 16bpp, (1 Cr & Cb sample per 2x1 Y samples)
Definition: pixfmt.h:66
int ff_mpv_reallocate_putbitbuffer(MpegEncContext *s, size_t threshold, size_t size_increase)
uint8_t * intra_ac_vlc_last_length
Definition: mpegvideo.h:312
Definition: mjpeg.h:79
static void put_marker(PutBitContext *p, enum JpegMarker code)
Definition: mjpegenc.h:101
Definition: mjpeg.h:56
static void skip_put_bytes(PutBitContext *s, int n)
Skip the given number of bytes.
Definition: put_bits.h:333
#define AV_CODEC_FLAG_BITEXACT
Use only bitexact stuff (except (I)DCT).
Definition: avcodec.h:886
#define av_assert1(cond)
assert() equivalent, that does not lie in speed critical code.
Definition: avassert.h:53
uint8_t val_ac_chrominance[256]
AC chrominance Huffman values.
Definition: mjpegenc.h:86
int width
picture width / height.
Definition: avcodec.h:1690
int priv_data_size
Definition: avcodec.h:3456
AVFormatContext * ctx
Definition: movenc.c:48
uint8_t uni_ac_vlc_len[64 *64 *2]
Storage for AC luminance VLC (in MpegEncContext)
Definition: mjpegenc.h:72
#define FFABS(a)
Absolute value, Note, INT_MIN / INT64_MIN result in undefined behavior as they are not representable ...
Definition: common.h:72
#define FF_THREAD_SLICE
Decode more than one part of a single frame at once.
Definition: avcodec.h:2781
void ff_mjpeg_escape_FF(PutBitContext *pb, int start)
int n
Definition: avisynth_c.h:684
packed RGB 8:8:8, 24bpp, BGRBGR...
Definition: pixfmt.h:65
Compute and use optimal Huffman tables.
Definition: mjpegenc.h:97
the normal 2^n-1 "JPEG" YUV ranges
Definition: pixfmt.h:499
static const float pred[4]
Definition: siprdata.h:259
struct MJpegContext * mjpeg_ctx
Definition: mpegvideo.h:424
const AVS_VideoInfo int align
Definition: avisynth_c.h:795
struct MpegEncContext * thread_context[MAX_THREADS]
Definition: mpegvideo.h:155
void ff_mjpeg_encode_dc(PutBitContext *pb, int val, uint8_t *huff_size, uint16_t *huff_code)
Libavcodec external API header.
enum AVCodecID codec_id
Definition: avcodec.h:1528
static void ff_mjpeg_encode_huffman_increment(MJpegEncHuffmanContext *s, uint8_t val)
uint8_t * intra_chroma_ac_vlc_last_length
Definition: mpegvideo.h:314
main external API structure.
Definition: avcodec.h:1518
ScanTable intra_scantable
Definition: mpegvideo.h:91
void * buf
Definition: avisynth_c.h:690
static void jpeg_table_header(AVCodecContext *avctx, PutBitContext *p, ScanTable *intra_scantable, uint16_t luma_intra_matrix[64], uint16_t chroma_intra_matrix[64], int hsample[3])
const uint8_t avpriv_mjpeg_val_dc[12]
Definition: jpegtables.c:67
uint8_t bits_ac_chrominance[17]
AC chrominance Huffman bits.
Definition: mjpegenc.h:85
Rational number (pair of numerator and denominator).
Definition: rational.h:58
packed BGR 8:8:8, 32bpp, BGRXBGRX... X=unused/undefined
Definition: pixfmt.h:236
int ff_mjpeg_encode_stuffing(MpegEncContext *s)
Writes the complete JPEG frame when optimal huffman tables are enabled, otherwise writes the stuffing...
const uint8_t avpriv_mjpeg_bits_ac_chrominance[17]
Definition: jpegtables.c:99
void ff_mjpeg_encode_picture_header(AVCodecContext *avctx, PutBitContext *pb, ScanTable *intra_scantable, int pred, uint16_t luma_intra_matrix[64], uint16_t chroma_intra_matrix[64])
const uint8_t avpriv_mjpeg_val_ac_chrominance[]
Definition: jpegtables.c:102
#define av_log2_16bit
Definition: intmath.h:84
Huffman table generation for MJPEG encoder.
uint8_t val_dc_luminance[12]
DC luminance Huffman values.
Definition: mjpegenc.h:78
uint8_t level
Definition: svq3.c:207
the normal 219*2^(n-8) "MPEG" YUV ranges
Definition: pixfmt.h:498
MpegEncContext.
Definition: mpegvideo.h:81
struct AVCodecContext * avctx
Definition: mpegvideo.h:98
uint16_t huff_code_dc_luminance[12]
DC luminance Huffman table codes.
Definition: mjpegenc.h:62
PutBitContext pb
bit output
Definition: mpegvideo.h:151
uint8_t bits_dc_chrominance[17]
DC chrominance Huffman bits.
Definition: mjpegenc.h:79
uint8_t huff_size_dc_luminance[12]
DC luminance Huffman table size.
Definition: mjpegenc.h:61
planar YUV 4:2:0, 12bpp, (1 Cr & Cb sample per 2x2 Y samples)
Definition: pixfmt.h:62
static void flush_put_bits(PutBitContext *s)
Pad the end of the output stream with zeros.
Definition: put_bits.h:101
uint8_t bits_ac_luminance[17]
AC luminance Huffman bits.
Definition: mjpegenc.h:83
common internal and external API header
uint8_t code
The exponent.
Definition: mjpegenc.h:52
size_t huff_ncode
Number of current entries in the buffer.
Definition: mjpegenc.h:88
planar YUV 4:4:4, 24bpp, full scale (JPEG), deprecated in favor of AV_PIX_FMT_YUV444P and setting col...
Definition: pixfmt.h:76
int den
Denominator.
Definition: rational.h:60
const uint8_t avpriv_mjpeg_bits_ac_luminance[17]
Definition: jpegtables.c:73
void * priv_data
Definition: avcodec.h:1545
pixel format definitions
int len
const uint8_t avpriv_mjpeg_val_ac_luminance[]
Definition: jpegtables.c:75
static int put_huffman_table(PutBitContext *p, int table_class, int table_id, const uint8_t *bits_table, const uint8_t *value_table)
void ff_mjpeg_encode_picture_frame(MpegEncContext *s)
Encodes and outputs the entire frame in the JPEG format.
#define LIBAVCODEC_IDENT
Definition: version.h:42
void avpriv_put_string(PutBitContext *pb, const char *string, int terminate_string)
Put the string string in the bitstream.
Definition: bitstream.c:53
uint16_t intra_matrix[64]
matrix transmitted in the bitstream
Definition: mpegvideo.h:300
void INT64 start
Definition: avisynth_c.h:690
static void jpeg_put_comments(AVCodecContext *avctx, PutBitContext *p)
MJpegHuffmanCode * huff_buffer
Buffer for Huffman code values.
Definition: mjpegenc.h:89
void ff_mjpeg_encode_huffman_init(MJpegEncHuffmanContext *s)
Definition: mjpeg.h:61
bitstream writer API