Libav
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 #include <assert.h>
34 
35 #include "libavutil/pixdesc.h"
36 
37 #include "avcodec.h"
38 #include "mpegvideo.h"
39 #include "mjpeg.h"
40 #include "mjpegenc.h"
41 
43 {
44  MJpegContext *m;
45 
46  m = av_malloc(sizeof(MJpegContext));
47  if (!m)
48  return -1;
49 
50  s->min_qcoeff=-1023;
51  s->max_qcoeff= 1023;
52 
53  /* build all the huffman tables */
70 
71  s->mjpeg_ctx = m;
72  return 0;
73 }
74 
76 {
77  av_free(s->mjpeg_ctx);
78 }
79 
80 /* table_class: 0 = DC coef, 1 = AC coefs */
81 static int put_huffman_table(PutBitContext *p, int table_class, int table_id,
82  const uint8_t *bits_table, const uint8_t *value_table)
83 {
84  int n, i;
85 
86  put_bits(p, 4, table_class);
87  put_bits(p, 4, table_id);
88 
89  n = 0;
90  for(i=1;i<=16;i++) {
91  n += bits_table[i];
92  put_bits(p, 8, bits_table[i]);
93  }
94 
95  for(i=0;i<n;i++)
96  put_bits(p, 8, value_table[i]);
97 
98  return n + 17;
99 }
100 
101 static void jpeg_table_header(PutBitContext *p, ScanTable *intra_scantable,
102  uint16_t intra_matrix[64])
103 {
104  int i, j, size;
105  uint8_t *ptr;
106 
107  /* quant matrixes */
108  put_marker(p, DQT);
109  put_bits(p, 16, 2 + 1 * (1 + 64));
110  put_bits(p, 4, 0); /* 8 bit precision */
111  put_bits(p, 4, 0); /* table 0 */
112  for(i=0;i<64;i++) {
113  j = intra_scantable->permutated[i];
114  put_bits(p, 8, intra_matrix[j]);
115  }
116 
117  /* huffman table */
118  put_marker(p, DHT);
119  flush_put_bits(p);
120  ptr = put_bits_ptr(p);
121  put_bits(p, 16, 0); /* patched later */
122  size = 2;
127 
132  AV_WB16(ptr, size);
133 }
134 
136 {
137  int size;
138  uint8_t *ptr;
139 
140  if (avctx->sample_aspect_ratio.num > 0 && avctx->sample_aspect_ratio.den > 0) {
141  /* JFIF header */
142  put_marker(p, APP0);
143  put_bits(p, 16, 16);
144  avpriv_put_string(p, "JFIF", 1); /* this puts the trailing zero-byte too */
145  put_bits(p, 16, 0x0201); /* v 1.02 */
146  put_bits(p, 8, 0); /* units type: 0 - aspect ratio */
147  put_bits(p, 16, avctx->sample_aspect_ratio.num);
148  put_bits(p, 16, avctx->sample_aspect_ratio.den);
149  put_bits(p, 8, 0); /* thumbnail width */
150  put_bits(p, 8, 0); /* thumbnail height */
151  }
152 
153  /* comment */
154  if (!(avctx->flags & CODEC_FLAG_BITEXACT)) {
155  put_marker(p, COM);
156  flush_put_bits(p);
157  ptr = put_bits_ptr(p);
158  put_bits(p, 16, 0); /* patched later */
160  size = strlen(LIBAVCODEC_IDENT)+3;
161  AV_WB16(ptr, size);
162  }
163 
164  if (avctx->pix_fmt == AV_PIX_FMT_YUV420P ||
165  avctx->pix_fmt == AV_PIX_FMT_YUV422P ||
166  avctx->pix_fmt == AV_PIX_FMT_YUV444P) {
167  put_marker(p, COM);
168  flush_put_bits(p);
169  ptr = put_bits_ptr(p);
170  put_bits(p, 16, 0); /* patched later */
171  avpriv_put_string(p, "CS=ITU601", 1);
172  size = strlen("CS=ITU601")+3;
173  AV_WB16(ptr, size);
174  }
175 }
176 
178  ScanTable *intra_scantable,
179  uint16_t intra_matrix[64])
180 {
181  int chroma_h_shift, chroma_v_shift;
182  const int lossless = avctx->codec_id != AV_CODEC_ID_MJPEG;
183  int hsample[3], vsample[3];
184 
185  av_pix_fmt_get_chroma_sub_sample(avctx->pix_fmt, &chroma_h_shift,
186  &chroma_v_shift);
187 
188  if (avctx->codec->id == AV_CODEC_ID_LJPEG &&
189  avctx->pix_fmt == AV_PIX_FMT_BGR24) {
190  vsample[0] = hsample[0] =
191  vsample[1] = hsample[1] =
192  vsample[2] = hsample[2] = 1;
193  } else {
194  vsample[0] = 2;
195  vsample[1] = 2 >> chroma_v_shift;
196  vsample[2] = 2 >> chroma_v_shift;
197  hsample[0] = 2;
198  hsample[1] = 2 >> chroma_h_shift;
199  hsample[2] = 2 >> chroma_h_shift;
200  }
201 
202  put_marker(pb, SOI);
203 
204  jpeg_put_comments(avctx, pb);
205 
206  jpeg_table_header(pb, intra_scantable, intra_matrix);
207 
208  switch (avctx->codec_id) {
209  case AV_CODEC_ID_MJPEG: put_marker(pb, SOF0 ); break;
210  case AV_CODEC_ID_LJPEG: put_marker(pb, SOF3 ); break;
211  default: assert(0);
212  }
213 
214  put_bits(pb, 16, 17);
215  if (lossless && avctx->pix_fmt == AV_PIX_FMT_BGR24)
216  put_bits(pb, 8, 9); /* 9 bits/component RCT */
217  else
218  put_bits(pb, 8, 8); /* 8 bits/component */
219  put_bits(pb, 16, avctx->height);
220  put_bits(pb, 16, avctx->width);
221  put_bits(pb, 8, 3); /* 3 components */
222 
223  /* Y component */
224  put_bits(pb, 8, 1); /* component number */
225  put_bits(pb, 4, hsample[0]); /* H factor */
226  put_bits(pb, 4, vsample[0]); /* V factor */
227  put_bits(pb, 8, 0); /* select matrix */
228 
229  /* Cb component */
230  put_bits(pb, 8, 2); /* component number */
231  put_bits(pb, 4, hsample[1]); /* H factor */
232  put_bits(pb, 4, vsample[1]); /* V factor */
233  put_bits(pb, 8, 0); /* select matrix */
234 
235  /* Cr component */
236  put_bits(pb, 8, 3); /* component number */
237  put_bits(pb, 4, hsample[2]); /* H factor */
238  put_bits(pb, 4, vsample[2]); /* V factor */
239  put_bits(pb, 8, 0); /* select matrix */
240 
241  /* scan header */
242  put_marker(pb, SOS);
243  put_bits(pb, 16, 12); /* length */
244  put_bits(pb, 8, 3); /* 3 components */
245 
246  /* Y component */
247  put_bits(pb, 8, 1); /* index */
248  put_bits(pb, 4, 0); /* DC huffman table index */
249  put_bits(pb, 4, 0); /* AC huffman table index */
250 
251  /* Cb component */
252  put_bits(pb, 8, 2); /* index */
253  put_bits(pb, 4, 1); /* DC huffman table index */
254  put_bits(pb, 4, lossless ? 0 : 1); /* AC huffman table index */
255 
256  /* Cr component */
257  put_bits(pb, 8, 3); /* index */
258  put_bits(pb, 4, 1); /* DC huffman table index */
259  put_bits(pb, 4, lossless ? 0 : 1); /* AC huffman table index */
260 
261  put_bits(pb, 8, lossless ? avctx->prediction_method + 1 : 0); /* Ss (not used) */
262 
263  switch (avctx->codec_id) {
264  case AV_CODEC_ID_MJPEG: put_bits(pb, 8, 63); break; /* Se (not used) */
265  case AV_CODEC_ID_LJPEG: put_bits(pb, 8, 0); break; /* not used */
266  default: assert(0);
267  }
268 
269  put_bits(pb, 8, 0); /* Ah/Al (not used) */
270 }
271 
272 static void escape_FF(PutBitContext *pb, int start)
273 {
274  int size = put_bits_count(pb) - start * 8;
275  int i, ff_count;
276  uint8_t *buf = pb->buf + start;
277  int align= (-(size_t)(buf))&3;
278 
279  assert((size&7) == 0);
280  size >>= 3;
281 
282  ff_count=0;
283  for(i=0; i<size && i<align; i++){
284  if(buf[i]==0xFF) ff_count++;
285  }
286  for(; i<size-15; i+=16){
287  int acc, v;
288 
289  v= *(uint32_t*)(&buf[i]);
290  acc= (((v & (v>>4))&0x0F0F0F0F)+0x01010101)&0x10101010;
291  v= *(uint32_t*)(&buf[i+4]);
292  acc+=(((v & (v>>4))&0x0F0F0F0F)+0x01010101)&0x10101010;
293  v= *(uint32_t*)(&buf[i+8]);
294  acc+=(((v & (v>>4))&0x0F0F0F0F)+0x01010101)&0x10101010;
295  v= *(uint32_t*)(&buf[i+12]);
296  acc+=(((v & (v>>4))&0x0F0F0F0F)+0x01010101)&0x10101010;
297 
298  acc>>=4;
299  acc+= (acc>>16);
300  acc+= (acc>>8);
301  ff_count+= acc&0xFF;
302  }
303  for(; i<size; i++){
304  if(buf[i]==0xFF) ff_count++;
305  }
306 
307  if(ff_count==0) return;
308 
309  flush_put_bits(pb);
310  skip_put_bytes(pb, ff_count);
311 
312  for(i=size-1; ff_count; i--){
313  int v= buf[i];
314 
315  if(v==0xFF){
316  buf[i+ff_count]= 0;
317  ff_count--;
318  }
319 
320  buf[i+ff_count]= v;
321  }
322 }
323 
325 {
326  int length;
327  length= (-put_bits_count(pbc))&7;
328  if(length) put_bits(pbc, length, (1<<length)-1);
329 }
330 
332 {
334  flush_put_bits(pb);
335 
336  assert((header_bits & 7) == 0);
337 
338  escape_FF(pb, header_bits >> 3);
339 
340  put_marker(pb, EOI);
341 }
342 
344  uint8_t *huff_size, uint16_t *huff_code)
345 {
346  int mant, nbits;
347 
348  if (val == 0) {
349  put_bits(pb, huff_size[0], huff_code[0]);
350  } else {
351  mant = val;
352  if (val < 0) {
353  val = -val;
354  mant--;
355  }
356 
357  nbits= av_log2_16bit(val) + 1;
358 
359  put_bits(pb, huff_size[nbits], huff_code[nbits]);
360 
361  put_sbits(pb, nbits, mant);
362  }
363 }
364 
365 static void encode_block(MpegEncContext *s, int16_t *block, int n)
366 {
367  int mant, nbits, code, i, j;
368  int component, dc, run, last_index, val;
369  MJpegContext *m = s->mjpeg_ctx;
370  uint8_t *huff_size_ac;
371  uint16_t *huff_code_ac;
372 
373  /* DC coef */
374  component = (n <= 3 ? 0 : (n&1) + 1);
375  dc = block[0]; /* overflow is impossible */
376  val = dc - s->last_dc[component];
377  if (n < 4) {
379  huff_size_ac = m->huff_size_ac_luminance;
380  huff_code_ac = m->huff_code_ac_luminance;
381  } else {
383  huff_size_ac = m->huff_size_ac_chrominance;
384  huff_code_ac = m->huff_code_ac_chrominance;
385  }
386  s->last_dc[component] = dc;
387 
388  /* AC coefs */
389 
390  run = 0;
391  last_index = s->block_last_index[n];
392  for(i=1;i<=last_index;i++) {
393  j = s->intra_scantable.permutated[i];
394  val = block[j];
395  if (val == 0) {
396  run++;
397  } else {
398  while (run >= 16) {
399  put_bits(&s->pb, huff_size_ac[0xf0], huff_code_ac[0xf0]);
400  run -= 16;
401  }
402  mant = val;
403  if (val < 0) {
404  val = -val;
405  mant--;
406  }
407 
408  nbits= av_log2(val) + 1;
409  code = (run << 4) | nbits;
410 
411  put_bits(&s->pb, huff_size_ac[code], huff_code_ac[code]);
412 
413  put_sbits(&s->pb, nbits, mant);
414  run = 0;
415  }
416  }
417 
418  /* output EOB only if not already 64 values */
419  if (last_index < 63 || run != 0)
420  put_bits(&s->pb, huff_size_ac[0], huff_code_ac[0]);
421 }
422 
423 void ff_mjpeg_encode_mb(MpegEncContext *s, int16_t block[6][64])
424 {
425  int i;
426  for(i=0;i<5;i++) {
427  encode_block(s, block[i], i);
428  }
429  if (s->chroma_format == CHROMA_420) {
430  encode_block(s, block[5], 5);
431  } else {
432  encode_block(s, block[6], 6);
433  encode_block(s, block[5], 5);
434  encode_block(s, block[7], 7);
435  }
436 
437  s->i_tex_bits += get_bits_diff(s);
438 }
439 
441  .name = "mjpeg",
442  .long_name = NULL_IF_CONFIG_SMALL("MJPEG (Motion JPEG)"),
443  .type = AVMEDIA_TYPE_VIDEO,
444  .id = AV_CODEC_ID_MJPEG,
445  .priv_data_size = sizeof(MpegEncContext),
447  .encode2 = ff_MPV_encode_picture,
449  .pix_fmts = (const enum AVPixelFormat[]){
451  },
452 };