mjpegenc.c
Go to the documentation of this file.
1 /*
2  * MJPEG encoder
3  * Copyright (c) 2000, 2001 Fabrice Bellard
4  * Copyright (c) 2003 Alex Beregszaszi
5  * Copyright (c) 2003-2004 Michael Niedermayer
6  *
7  * Support for external huffman table, various fixes (AVID workaround),
8  * aspecting, new decode_frame mechanism and apple mjpeg-b support
9  * by Alex Beregszaszi
10  *
11  * This file is part of Libav.
12  *
13  * Libav is free software; you can redistribute it and/or
14  * modify it under the terms of the GNU Lesser General Public
15  * License as published by the Free Software Foundation; either
16  * version 2.1 of the License, or (at your option) any later version.
17  *
18  * Libav is distributed in the hope that it will be useful,
19  * but WITHOUT ANY WARRANTY; without even the implied warranty of
20  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
21  * Lesser General Public License for more details.
22  *
23  * You should have received a copy of the GNU Lesser General Public
24  * License along with Libav; if not, write to the Free Software
25  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
26  */
27 
33 //#define DEBUG
34 #include <assert.h>
35 
36 #include "avcodec.h"
37 #include "dsputil.h"
38 #include "mpegvideo.h"
39 #include "mjpeg.h"
40 #include "mjpegenc.h"
41 
42 /* use two quantizer tables (one for luminance and one for chrominance) */
43 /* not yet working */
44 #undef TWOMATRIXES
45 
46 
48 {
49  MJpegContext *m;
50 
51  m = av_malloc(sizeof(MJpegContext));
52  if (!m)
53  return -1;
54 
55  s->min_qcoeff=-1023;
56  s->max_qcoeff= 1023;
57 
58  /* build all the huffman tables */
75 
76  s->mjpeg_ctx = m;
77  return 0;
78 }
79 
81 {
82  av_free(s->mjpeg_ctx);
83 }
84 
85 /* table_class: 0 = DC coef, 1 = AC coefs */
86 static int put_huffman_table(MpegEncContext *s, int table_class, int table_id,
87  const uint8_t *bits_table, const uint8_t *value_table)
88 {
89  PutBitContext *p = &s->pb;
90  int n, i;
91 
92  put_bits(p, 4, table_class);
93  put_bits(p, 4, table_id);
94 
95  n = 0;
96  for(i=1;i<=16;i++) {
97  n += bits_table[i];
98  put_bits(p, 8, bits_table[i]);
99  }
100 
101  for(i=0;i<n;i++)
102  put_bits(p, 8, value_table[i]);
103 
104  return n + 17;
105 }
106 
108 {
109  PutBitContext *p = &s->pb;
110  int i, j, size;
111  uint8_t *ptr;
112 
113  /* quant matrixes */
114  put_marker(p, DQT);
115 #ifdef TWOMATRIXES
116  put_bits(p, 16, 2 + 2 * (1 + 64));
117 #else
118  put_bits(p, 16, 2 + 1 * (1 + 64));
119 #endif
120  put_bits(p, 4, 0); /* 8 bit precision */
121  put_bits(p, 4, 0); /* table 0 */
122  for(i=0;i<64;i++) {
123  j = s->intra_scantable.permutated[i];
124  put_bits(p, 8, s->intra_matrix[j]);
125  }
126 #ifdef TWOMATRIXES
127  put_bits(p, 4, 0); /* 8 bit precision */
128  put_bits(p, 4, 1); /* table 1 */
129  for(i=0;i<64;i++) {
130  j = s->intra_scantable.permutated[i];
131  put_bits(p, 8, s->chroma_intra_matrix[j]);
132  }
133 #endif
134 
135  /* huffman table */
136  put_marker(p, DHT);
137  flush_put_bits(p);
138  ptr = put_bits_ptr(p);
139  put_bits(p, 16, 0); /* patched later */
140  size = 2;
145 
150  AV_WB16(ptr, size);
151 }
152 
154 {
155  PutBitContext *p = &s->pb;
156  int size;
157  uint8_t *ptr;
158 
159  if (s->aspect_ratio_info /* && !lossless */)
160  {
161  /* JFIF header */
162  put_marker(p, APP0);
163  put_bits(p, 16, 16);
164  avpriv_put_string(p, "JFIF", 1); /* this puts the trailing zero-byte too */
165  put_bits(p, 16, 0x0201); /* v 1.02 */
166  put_bits(p, 8, 0); /* units type: 0 - aspect ratio */
169  put_bits(p, 8, 0); /* thumbnail width */
170  put_bits(p, 8, 0); /* thumbnail height */
171  }
172 
173  /* comment */
174  if(!(s->flags & CODEC_FLAG_BITEXACT)){
175  put_marker(p, COM);
176  flush_put_bits(p);
177  ptr = put_bits_ptr(p);
178  put_bits(p, 16, 0); /* patched later */
180  size = strlen(LIBAVCODEC_IDENT)+3;
181  AV_WB16(ptr, size);
182  }
183 
184  if( s->avctx->pix_fmt == AV_PIX_FMT_YUV420P
186  ||s->avctx->pix_fmt == AV_PIX_FMT_YUV444P){
187  put_marker(p, COM);
188  flush_put_bits(p);
189  ptr = put_bits_ptr(p);
190  put_bits(p, 16, 0); /* patched later */
191  avpriv_put_string(p, "CS=ITU601", 1);
192  size = strlen("CS=ITU601")+3;
193  AV_WB16(ptr, size);
194  }
195 }
196 
198 {
199  const int lossless= s->avctx->codec_id != AV_CODEC_ID_MJPEG;
200 
201  put_marker(&s->pb, SOI);
202 
204 
206 
207  switch(s->avctx->codec_id){
208  case AV_CODEC_ID_MJPEG: put_marker(&s->pb, SOF0 ); break;
209  case AV_CODEC_ID_LJPEG: put_marker(&s->pb, SOF3 ); break;
210  default: assert(0);
211  }
212 
213  put_bits(&s->pb, 16, 17);
214  if(lossless && s->avctx->pix_fmt == AV_PIX_FMT_BGRA)
215  put_bits(&s->pb, 8, 9); /* 9 bits/component RCT */
216  else
217  put_bits(&s->pb, 8, 8); /* 8 bits/component */
218  put_bits(&s->pb, 16, s->height);
219  put_bits(&s->pb, 16, s->width);
220  put_bits(&s->pb, 8, 3); /* 3 components */
221 
222  /* Y component */
223  put_bits(&s->pb, 8, 1); /* component number */
224  put_bits(&s->pb, 4, s->mjpeg_hsample[0]); /* H factor */
225  put_bits(&s->pb, 4, s->mjpeg_vsample[0]); /* V factor */
226  put_bits(&s->pb, 8, 0); /* select matrix */
227 
228  /* Cb component */
229  put_bits(&s->pb, 8, 2); /* component number */
230  put_bits(&s->pb, 4, s->mjpeg_hsample[1]); /* H factor */
231  put_bits(&s->pb, 4, s->mjpeg_vsample[1]); /* V factor */
232 #ifdef TWOMATRIXES
233  put_bits(&s->pb, 8, lossless ? 0 : 1); /* select matrix */
234 #else
235  put_bits(&s->pb, 8, 0); /* select matrix */
236 #endif
237 
238  /* Cr component */
239  put_bits(&s->pb, 8, 3); /* component number */
240  put_bits(&s->pb, 4, s->mjpeg_hsample[2]); /* H factor */
241  put_bits(&s->pb, 4, s->mjpeg_vsample[2]); /* V factor */
242 #ifdef TWOMATRIXES
243  put_bits(&s->pb, 8, lossless ? 0 : 1); /* select matrix */
244 #else
245  put_bits(&s->pb, 8, 0); /* select matrix */
246 #endif
247 
248  /* scan header */
249  put_marker(&s->pb, SOS);
250  put_bits(&s->pb, 16, 12); /* length */
251  put_bits(&s->pb, 8, 3); /* 3 components */
252 
253  /* Y component */
254  put_bits(&s->pb, 8, 1); /* index */
255  put_bits(&s->pb, 4, 0); /* DC huffman table index */
256  put_bits(&s->pb, 4, 0); /* AC huffman table index */
257 
258  /* Cb component */
259  put_bits(&s->pb, 8, 2); /* index */
260  put_bits(&s->pb, 4, 1); /* DC huffman table index */
261  put_bits(&s->pb, 4, lossless ? 0 : 1); /* AC huffman table index */
262 
263  /* Cr component */
264  put_bits(&s->pb, 8, 3); /* index */
265  put_bits(&s->pb, 4, 1); /* DC huffman table index */
266  put_bits(&s->pb, 4, lossless ? 0 : 1); /* AC huffman table index */
267 
268  put_bits(&s->pb, 8, lossless ? s->avctx->prediction_method+1 : 0); /* Ss (not used) */
269 
270  switch(s->avctx->codec_id){
271  case AV_CODEC_ID_MJPEG: put_bits(&s->pb, 8, 63); break; /* Se (not used) */
272  case AV_CODEC_ID_LJPEG: put_bits(&s->pb, 8, 0); break; /* not used */
273  default: assert(0);
274  }
275 
276  put_bits(&s->pb, 8, 0); /* Ah/Al (not used) */
277 }
278 
279 static void escape_FF(MpegEncContext *s, int start)
280 {
281  int size= put_bits_count(&s->pb) - start*8;
282  int i, ff_count;
283  uint8_t *buf= s->pb.buf + start;
284  int align= (-(size_t)(buf))&3;
285 
286  assert((size&7) == 0);
287  size >>= 3;
288 
289  ff_count=0;
290  for(i=0; i<size && i<align; i++){
291  if(buf[i]==0xFF) ff_count++;
292  }
293  for(; i<size-15; i+=16){
294  int acc, v;
295 
296  v= *(uint32_t*)(&buf[i]);
297  acc= (((v & (v>>4))&0x0F0F0F0F)+0x01010101)&0x10101010;
298  v= *(uint32_t*)(&buf[i+4]);
299  acc+=(((v & (v>>4))&0x0F0F0F0F)+0x01010101)&0x10101010;
300  v= *(uint32_t*)(&buf[i+8]);
301  acc+=(((v & (v>>4))&0x0F0F0F0F)+0x01010101)&0x10101010;
302  v= *(uint32_t*)(&buf[i+12]);
303  acc+=(((v & (v>>4))&0x0F0F0F0F)+0x01010101)&0x10101010;
304 
305  acc>>=4;
306  acc+= (acc>>16);
307  acc+= (acc>>8);
308  ff_count+= acc&0xFF;
309  }
310  for(; i<size; i++){
311  if(buf[i]==0xFF) ff_count++;
312  }
313 
314  if(ff_count==0) return;
315 
316  flush_put_bits(&s->pb);
317  skip_put_bytes(&s->pb, ff_count);
318 
319  for(i=size-1; ff_count; i--){
320  int v= buf[i];
321 
322  if(v==0xFF){
323  buf[i+ff_count]= 0;
324  ff_count--;
325  }
326 
327  buf[i+ff_count]= v;
328  }
329 }
330 
332 {
333  int length;
334  length= (-put_bits_count(pbc))&7;
335  if(length) put_bits(pbc, length, (1<<length)-1);
336 }
337 
339 {
341  flush_put_bits(&s->pb);
342 
343  assert((s->header_bits&7)==0);
344 
345  escape_FF(s, s->header_bits>>3);
346 
347  put_marker(&s->pb, EOI);
348 }
349 
351  uint8_t *huff_size, uint16_t *huff_code)
352 {
353  int mant, nbits;
354 
355  if (val == 0) {
356  put_bits(&s->pb, huff_size[0], huff_code[0]);
357  } else {
358  mant = val;
359  if (val < 0) {
360  val = -val;
361  mant--;
362  }
363 
364  nbits= av_log2_16bit(val) + 1;
365 
366  put_bits(&s->pb, huff_size[nbits], huff_code[nbits]);
367 
368  put_sbits(&s->pb, nbits, mant);
369  }
370 }
371 
372 static void encode_block(MpegEncContext *s, DCTELEM *block, int n)
373 {
374  int mant, nbits, code, i, j;
375  int component, dc, run, last_index, val;
376  MJpegContext *m = s->mjpeg_ctx;
377  uint8_t *huff_size_ac;
378  uint16_t *huff_code_ac;
379 
380  /* DC coef */
381  component = (n <= 3 ? 0 : (n&1) + 1);
382  dc = block[0]; /* overflow is impossible */
383  val = dc - s->last_dc[component];
384  if (n < 4) {
386  huff_size_ac = m->huff_size_ac_luminance;
387  huff_code_ac = m->huff_code_ac_luminance;
388  } else {
390  huff_size_ac = m->huff_size_ac_chrominance;
391  huff_code_ac = m->huff_code_ac_chrominance;
392  }
393  s->last_dc[component] = dc;
394 
395  /* AC coefs */
396 
397  run = 0;
398  last_index = s->block_last_index[n];
399  for(i=1;i<=last_index;i++) {
400  j = s->intra_scantable.permutated[i];
401  val = block[j];
402  if (val == 0) {
403  run++;
404  } else {
405  while (run >= 16) {
406  put_bits(&s->pb, huff_size_ac[0xf0], huff_code_ac[0xf0]);
407  run -= 16;
408  }
409  mant = val;
410  if (val < 0) {
411  val = -val;
412  mant--;
413  }
414 
415  nbits= av_log2(val) + 1;
416  code = (run << 4) | nbits;
417 
418  put_bits(&s->pb, huff_size_ac[code], huff_code_ac[code]);
419 
420  put_sbits(&s->pb, nbits, mant);
421  run = 0;
422  }
423  }
424 
425  /* output EOB only if not already 64 values */
426  if (last_index < 63 || run != 0)
427  put_bits(&s->pb, huff_size_ac[0], huff_code_ac[0]);
428 }
429 
431 {
432  int i;
433  for(i=0;i<5;i++) {
434  encode_block(s, block[i], i);
435  }
436  if (s->chroma_format == CHROMA_420) {
437  encode_block(s, block[5], 5);
438  } else {
439  encode_block(s, block[6], 6);
440  encode_block(s, block[5], 5);
441  encode_block(s, block[7], 7);
442  }
443 
444  s->i_tex_bits += get_bits_diff(s);
445 }
446 
448  .name = "mjpeg",
449  .type = AVMEDIA_TYPE_VIDEO,
450  .id = AV_CODEC_ID_MJPEG,
451  .priv_data_size = sizeof(MpegEncContext),
453  .encode2 = ff_MPV_encode_picture,
455  .pix_fmts = (const enum AVPixelFormat[]){
457  },
458  .long_name = NULL_IF_CONFIG_SMALL("MJPEG (Motion JPEG)"),
459 };
Definition: mjpeg.h:115
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 aspect_ratio_info
Definition: mpegvideo.h:551
int size
struct MpegEncContext MpegEncContext
MpegEncContext.
static void put_sbits(PutBitContext *pb, int n, int32_t value)
Definition: put_bits.h:174
planar YUV 4:4:4, 24bpp, (1 Cr & Cb sample per 1x1 Y samples)
Definition: pixfmt.h:70
const uint8_t avpriv_mjpeg_bits_ac_luminance[17]
Definition: mjpeg.c:73
uint16_t chroma_intra_matrix[64]
Definition: mpegvideo.h:442
int acc
Definition: yuv2rgb.c:476
MJPEG encoder.
int ff_MPV_encode_end(AVCodecContext *avctx)
int mjpeg_hsample[3]
horizontal sampling factors, default = {2, 1, 1}
Definition: mpegvideo.h:593
int num
numerator
Definition: rational.h:44
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:1724
enum AVPixelFormat pix_fmt
Pixel format, see AV_PIX_FMT_xxx.
Definition: avcodec.h:1533
Definition: mjpeg.h:74
int min_qcoeff
minimum encodable coefficient
Definition: mpegvideo.h:448
const uint8_t avpriv_mjpeg_bits_dc_chrominance[17]
Definition: mjpeg.c:70
mpegvideo header.
const uint8_t avpriv_mjpeg_bits_ac_chrominance[17]
Definition: mjpeg.c:99
av_cold int ff_mjpeg_encode_init(MpegEncContext *s)
Definition: mjpegenc.c:47
uint8_t permutated[64]
Definition: dsputil.h:183
uint8_t run
Definition: svq3.c:132
AVCodec.
Definition: avcodec.h:2960
MJPEG encoder and decoder.
uint16_t huff_code_dc_chrominance[12]
Definition: mjpegenc.h:43
uint16_t huff_code_ac_luminance[256]
Definition: mjpegenc.h:46
uint8_t
#define CHROMA_420
Definition: mpegvideo.h:641
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: mjpeg.c:127
planar YUV 4:2:2, 16bpp, full scale (JPEG), deprecated in favor of PIX_FMT_YUV422P and setting color_...
Definition: pixfmt.h:78
uint8_t huff_size_dc_chrominance[12]
Definition: mjpegenc.h:42
Definition: mjpeg.h:77
int max_qcoeff
maximum encodable coefficient
Definition: mpegvideo.h:449
Definition: mjpeg.h:83
static int init(AVCodecParserContext *s)
Definition: h264_parser.c:335
int ff_MPV_encode_init(AVCodecContext *avctx)
static int get_bits_diff(MpegEncContext *s)
Definition: mpegvideo.h:826
uint16_t huff_code_ac_chrominance[256]
Definition: mjpegenc.h:48
AVCodec ff_mjpeg_encoder
Definition: mjpegenc.c:447
uint8_t huff_size_ac_luminance[256]
Definition: mjpegenc.h:45
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:201
void av_free(void *ptr)
Free a memory block which has been allocated with av_malloc(z)() or av_realloc(). ...
Definition: mem.c:139
int last_dc[3]
last DC values for MPEG1
Definition: mpegvideo.h:322
Definition: mjpeg.h:43
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
Definition: internal.h:88
packed BGRA 8:8:8:8, 32bpp, BGRABGRA...
Definition: pixfmt.h:96
Definition: mjpeg.h:60
uint8_t * buf
Definition: put_bits.h:42
uint8_t huff_size_ac_chrominance[256]
Definition: mjpegenc.h:47
const char * name
Name of the codec implementation.
Definition: avcodec.h:2967
static void put_bits(PutBitContext *s, int n, unsigned int value)
Write up to 31 bits into a bitstream.
Definition: put_bits.h:136
static int put_bits_count(PutBitContext *s)
Definition: put_bits.h:70
planar YUV 4:2:2, 16bpp, (1 Cr & Cb sample per 2x1 Y samples)
Definition: pixfmt.h:69
static DCTELEM block[64]
Definition: dct-test.c:169
static void skip_put_bytes(PutBitContext *s, int n)
Skip the given number of bytes.
Definition: put_bits.h:210
void ff_mjpeg_encode_picture_header(MpegEncContext *s)
Definition: mjpegenc.c:197
Definition: mjpeg.h:76
planar YUV 4:2:0, 12bpp, full scale (JPEG), deprecated in favor of PIX_FMT_YUV420P and setting color_...
Definition: pixfmt.h:77
static void jpeg_put_comments(MpegEncContext *s)
Definition: mjpegenc.c:153
void ff_mjpeg_encode_mb(MpegEncContext *s, DCTELEM block[6][64])
Definition: mjpegenc.c:430
const uint8_t avpriv_mjpeg_bits_dc_luminance[17]
Definition: mjpeg.c:65
const uint8_t avpriv_mjpeg_val_dc[12]
Definition: mjpeg.c:67
int block_last_index[12]
last non zero coefficient in block
Definition: mpegvideo.h:262
Definition: mjpeg.h:46
Definition: mjpeg.h:75
struct MJpegContext * mjpeg_ctx
Definition: mpegvideo.h:591
int mjpeg_vsample[3]
vertical sampling factors, default = {2, 1, 1}
Definition: mpegvideo.h:592
external API header
enum AVCodecID codec_id
Definition: avcodec.h:1350
void ff_mjpeg_encode_close(MpegEncContext *s)
Definition: mjpegenc.c:80
static void close(AVCodecParserContext *s)
Definition: h264_parser.c:326
ScanTable intra_scantable
Definition: mpegvideo.h:267
int height
picture size. must be a multiple of 16
Definition: mpegvideo.h:216
static void encode_block(MpegEncContext *s, DCTELEM *block, int n)
Definition: mjpegenc.c:372
void ff_mjpeg_encode_stuffing(PutBitContext *pbc)
Definition: mjpegenc.c:331
void ff_mjpeg_encode_picture_trailer(MpegEncContext *s)
Definition: mjpegenc.c:338
static void jpeg_table_header(MpegEncContext *s)
Definition: mjpegenc.c:107
#define AV_WB16(p, d)
Definition: intreadwrite.h:213
int ff_MPV_encode_picture(AVCodecContext *avctx, AVPacket *pkt, const AVFrame *frame, int *got_packet)
short DCTELEM
Definition: dsputil.h:39
static int put_huffman_table(MpegEncContext *s, int table_class, int table_id, const uint8_t *bits_table, const uint8_t *value_table)
Definition: mjpegenc.c:86
MpegEncContext.
Definition: mpegvideo.h:212
const uint8_t avpriv_mjpeg_val_ac_luminance[]
Definition: mjpeg.c:75
struct AVCodecContext * avctx
Definition: mpegvideo.h:214
uint16_t huff_code_dc_luminance[12]
Definition: mjpegenc.h:41
PutBitContext pb
bit output
Definition: mpegvideo.h:285
uint8_t huff_size_dc_luminance[12]
Definition: mjpegenc.h:40
planar YUV 4:2:0, 12bpp, (1 Cr & Cb sample per 2x2 Y samples)
Definition: pixfmt.h:65
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
int den
denominator
Definition: rational.h:45
DSP utils.
static void put_marker(PutBitContext *p, int code)
Definition: mjpeg.h:122
const uint8_t avpriv_mjpeg_val_ac_chrominance[]
Definition: mjpeg.c:102
#define av_log2_16bit
Definition: intmath.h:86
#define av_log2
Definition: intmath.h:85
void ff_mjpeg_encode_dc(MpegEncContext *s, int val, uint8_t *huff_size, uint16_t *huff_code)
Definition: mjpegenc.c:350
void avpriv_put_string(PutBitContext *pb, const char *string, int terminate_string)
Put the string string in the bitstream.
Definition: bitstream.c:50
int flags
AVCodecContext.flags (HQ, MV4, ...)
Definition: mpegvideo.h:231
uint16_t intra_matrix[64]
matrix transmitted in the bitstream
Definition: mpegvideo.h:441
static void escape_FF(MpegEncContext *s, int start)
Definition: mjpegenc.c:279
AVPixelFormat
Pixel format.
Definition: pixfmt.h:63