FFmpeg
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
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 FFmpeg.
12  *
13  * FFmpeg 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  * FFmpeg 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 FFmpeg; if not, write to the Free Software
25  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
26  */
27 
28 /**
29  * @file
30  * MJPEG encoder.
31  */
32 
33 #include "avcodec.h"
34 #include "dsputil.h"
35 #include "mpegvideo.h"
36 #include "mjpeg.h"
37 #include "mjpegenc.h"
38 
39 /* use two quantizer tables (one for luminance and one for chrominance) */
40 /* not yet working */
41 #undef TWOMATRIXES
42 
43 
45 {
46  MJpegContext *m;
47 
48  if (s->width > 65500 || s->height > 65500) {
49  av_log(s, AV_LOG_ERROR, "JPEG does not support resolutions above 65500x65500\n");
50  return -1;
51  }
52 
53  m = av_malloc(sizeof(MJpegContext));
54  if (!m)
55  return -1;
56 
57  s->min_qcoeff=-1023;
58  s->max_qcoeff= 1023;
59 
60  /* build all the huffman tables */
77 
78  s->mjpeg_ctx = m;
79  return 0;
80 }
81 
83 {
84  av_free(s->mjpeg_ctx);
85 }
86 
87 /* table_class: 0 = DC coef, 1 = AC coefs */
88 static int put_huffman_table(MpegEncContext *s, int table_class, int table_id,
89  const uint8_t *bits_table, const uint8_t *value_table)
90 {
91  PutBitContext *p = &s->pb;
92  int n, i;
93 
94  put_bits(p, 4, table_class);
95  put_bits(p, 4, table_id);
96 
97  n = 0;
98  for(i=1;i<=16;i++) {
99  n += bits_table[i];
100  put_bits(p, 8, bits_table[i]);
101  }
102 
103  for(i=0;i<n;i++)
104  put_bits(p, 8, value_table[i]);
105 
106  return n + 17;
107 }
108 
110 {
111  PutBitContext *p = &s->pb;
112  int i, j, size;
113  uint8_t *ptr;
114 
115  /* quant matrixes */
116  put_marker(p, DQT);
117 #ifdef TWOMATRIXES
118  put_bits(p, 16, 2 + 2 * (1 + 64));
119 #else
120  put_bits(p, 16, 2 + 1 * (1 + 64));
121 #endif
122  put_bits(p, 4, 0); /* 8 bit precision */
123  put_bits(p, 4, 0); /* table 0 */
124  for(i=0;i<64;i++) {
125  j = s->intra_scantable.permutated[i];
126  put_bits(p, 8, s->intra_matrix[j]);
127  }
128 #ifdef TWOMATRIXES
129  put_bits(p, 4, 0); /* 8 bit precision */
130  put_bits(p, 4, 1); /* table 1 */
131  for(i=0;i<64;i++) {
132  j = s->intra_scantable.permutated[i];
133  put_bits(p, 8, s->chroma_intra_matrix[j]);
134  }
135 #endif
136 
138  put_marker(p, DRI);
139  put_bits(p, 16, 4);
140  put_bits(p, 16, (s->width-1)/(8*s->mjpeg_hsample[0]) + 1);
141  }
142 
143  /* huffman table */
144  put_marker(p, DHT);
145  flush_put_bits(p);
146  ptr = put_bits_ptr(p);
147  put_bits(p, 16, 0); /* patched later */
148  size = 2;
153 
158  AV_WB16(ptr, size);
159 }
160 
162 {
163  PutBitContext *p = &s->pb;
164  int size;
165  uint8_t *ptr;
166 
167  if (s->avctx->sample_aspect_ratio.num /* && !lossless */)
168  {
169  /* JFIF header */
170  put_marker(p, APP0);
171  put_bits(p, 16, 16);
172  avpriv_put_string(p, "JFIF", 1); /* this puts the trailing zero-byte too */
173  put_bits(p, 16, 0x0102); /* v 1.02 */
174  put_bits(p, 8, 0); /* units type: 0 - aspect ratio */
177  put_bits(p, 8, 0); /* thumbnail width */
178  put_bits(p, 8, 0); /* thumbnail height */
179  }
180 
181  /* comment */
182  if(!(s->flags & CODEC_FLAG_BITEXACT)){
183  put_marker(p, COM);
184  flush_put_bits(p);
185  ptr = put_bits_ptr(p);
186  put_bits(p, 16, 0); /* patched later */
188  size = strlen(LIBAVCODEC_IDENT)+3;
189  AV_WB16(ptr, size);
190  }
191 
192  if( s->avctx->pix_fmt == AV_PIX_FMT_YUV420P
194  ||s->avctx->pix_fmt == AV_PIX_FMT_YUV444P){
195  put_marker(p, COM);
196  flush_put_bits(p);
197  ptr = put_bits_ptr(p);
198  put_bits(p, 16, 0); /* patched later */
199  avpriv_put_string(p, "CS=ITU601", 1);
200  size = strlen("CS=ITU601")+3;
201  AV_WB16(ptr, size);
202  }
203 }
204 
206 {
207  const int lossless= s->avctx->codec_id != AV_CODEC_ID_MJPEG;
208  int i;
209 
210  put_marker(&s->pb, SOI);
211 
212  // hack for AMV mjpeg format
213  if(s->avctx->codec_id == AV_CODEC_ID_AMV) goto end;
214 
216 
218 
219  switch(s->avctx->codec_id){
220  case AV_CODEC_ID_MJPEG: put_marker(&s->pb, SOF0 ); break;
221  case AV_CODEC_ID_LJPEG: put_marker(&s->pb, SOF3 ); break;
222  default: av_assert0(0);
223  }
224 
225  put_bits(&s->pb, 16, 17);
226  if(lossless && (s->avctx->pix_fmt == AV_PIX_FMT_BGR0
227  || s->avctx->pix_fmt == AV_PIX_FMT_BGRA
228  || s->avctx->pix_fmt == AV_PIX_FMT_BGR24))
229  put_bits(&s->pb, 8, 9); /* 9 bits/component RCT */
230  else
231  put_bits(&s->pb, 8, 8); /* 8 bits/component */
232  put_bits(&s->pb, 16, s->height);
233  put_bits(&s->pb, 16, s->width);
234  put_bits(&s->pb, 8, 3); /* 3 components */
235 
236  /* Y component */
237  put_bits(&s->pb, 8, 1); /* component number */
238  put_bits(&s->pb, 4, s->mjpeg_hsample[0]); /* H factor */
239  put_bits(&s->pb, 4, s->mjpeg_vsample[0]); /* V factor */
240  put_bits(&s->pb, 8, 0); /* select matrix */
241 
242  /* Cb component */
243  put_bits(&s->pb, 8, 2); /* component number */
244  put_bits(&s->pb, 4, s->mjpeg_hsample[1]); /* H factor */
245  put_bits(&s->pb, 4, s->mjpeg_vsample[1]); /* V factor */
246 #ifdef TWOMATRIXES
247  put_bits(&s->pb, 8, lossless ? 0 : 1); /* select matrix */
248 #else
249  put_bits(&s->pb, 8, 0); /* select matrix */
250 #endif
251 
252  /* Cr component */
253  put_bits(&s->pb, 8, 3); /* component number */
254  put_bits(&s->pb, 4, s->mjpeg_hsample[2]); /* H factor */
255  put_bits(&s->pb, 4, s->mjpeg_vsample[2]); /* V factor */
256 #ifdef TWOMATRIXES
257  put_bits(&s->pb, 8, lossless ? 0 : 1); /* select matrix */
258 #else
259  put_bits(&s->pb, 8, 0); /* select matrix */
260 #endif
261 
262  /* scan header */
263  put_marker(&s->pb, SOS);
264  put_bits(&s->pb, 16, 12); /* length */
265  put_bits(&s->pb, 8, 3); /* 3 components */
266 
267  /* Y component */
268  put_bits(&s->pb, 8, 1); /* index */
269  put_bits(&s->pb, 4, 0); /* DC huffman table index */
270  put_bits(&s->pb, 4, 0); /* AC huffman table index */
271 
272  /* Cb component */
273  put_bits(&s->pb, 8, 2); /* index */
274  put_bits(&s->pb, 4, 1); /* DC huffman table index */
275  put_bits(&s->pb, 4, lossless ? 0 : 1); /* AC huffman table index */
276 
277  /* Cr component */
278  put_bits(&s->pb, 8, 3); /* index */
279  put_bits(&s->pb, 4, 1); /* DC huffman table index */
280  put_bits(&s->pb, 4, lossless ? 0 : 1); /* AC huffman table index */
281 
282  put_bits(&s->pb, 8, lossless ? s->avctx->prediction_method+1 : 0); /* Ss (not used) */
283 
284  switch(s->avctx->codec_id){
285  case AV_CODEC_ID_MJPEG: put_bits(&s->pb, 8, 63); break; /* Se (not used) */
286  case AV_CODEC_ID_LJPEG: put_bits(&s->pb, 8, 0); break; /* not used */
287  default: av_assert0(0);
288  }
289 
290  put_bits(&s->pb, 8, 0); /* Ah/Al (not used) */
291 
292 end:
293  s->esc_pos = put_bits_count(&s->pb) >> 3;
294  for(i=1; i<s->slice_context_count; i++)
295  s->thread_context[i]->esc_pos = 0;
296 }
297 
298 static void escape_FF(MpegEncContext *s, int start)
299 {
300  int size= put_bits_count(&s->pb) - start*8;
301  int i, ff_count;
302  uint8_t *buf= s->pb.buf + start;
303  int align= (-(size_t)(buf))&3;
304 
305  av_assert1((size&7) == 0);
306  size >>= 3;
307 
308  ff_count=0;
309  for(i=0; i<size && i<align; i++){
310  if(buf[i]==0xFF) ff_count++;
311  }
312  for(; i<size-15; i+=16){
313  int acc, v;
314 
315  v= *(uint32_t*)(&buf[i]);
316  acc= (((v & (v>>4))&0x0F0F0F0F)+0x01010101)&0x10101010;
317  v= *(uint32_t*)(&buf[i+4]);
318  acc+=(((v & (v>>4))&0x0F0F0F0F)+0x01010101)&0x10101010;
319  v= *(uint32_t*)(&buf[i+8]);
320  acc+=(((v & (v>>4))&0x0F0F0F0F)+0x01010101)&0x10101010;
321  v= *(uint32_t*)(&buf[i+12]);
322  acc+=(((v & (v>>4))&0x0F0F0F0F)+0x01010101)&0x10101010;
323 
324  acc>>=4;
325  acc+= (acc>>16);
326  acc+= (acc>>8);
327  ff_count+= acc&0xFF;
328  }
329  for(; i<size; i++){
330  if(buf[i]==0xFF) ff_count++;
331  }
332 
333  if(ff_count==0) return;
334 
335  flush_put_bits(&s->pb);
336  skip_put_bytes(&s->pb, ff_count);
337 
338  for(i=size-1; ff_count; i--){
339  int v= buf[i];
340 
341  if(v==0xFF){
342  buf[i+ff_count]= 0;
343  ff_count--;
344  }
345 
346  buf[i+ff_count]= v;
347  }
348 }
349 
351 {
352  int length, i;
353  PutBitContext *pbc = &s->pb;
354  int mb_y = s->mb_y - !s->mb_x;
355  length= (-put_bits_count(pbc))&7;
356  if(length) put_bits(pbc, length, (1<<length)-1);
357 
358  flush_put_bits(&s->pb);
359  escape_FF(s, s->esc_pos);
360 
361  if((s->avctx->active_thread_type & FF_THREAD_SLICE) && mb_y < s->mb_height)
362  put_marker(pbc, RST0 + (mb_y&7));
363  s->esc_pos = put_bits_count(pbc) >> 3;
364 
365  for(i=0; i<3; i++)
366  s->last_dc[i] = 128 << s->intra_dc_precision;
367 }
368 
370 {
371 
372  av_assert1((s->header_bits&7)==0);
373 
374 
375  put_marker(&s->pb, EOI);
376 }
377 
379  uint8_t *huff_size, uint16_t *huff_code)
380 {
381  int mant, nbits;
382 
383  if (val == 0) {
384  put_bits(&s->pb, huff_size[0], huff_code[0]);
385  } else {
386  mant = val;
387  if (val < 0) {
388  val = -val;
389  mant--;
390  }
391 
392  nbits= av_log2_16bit(val) + 1;
393 
394  put_bits(&s->pb, huff_size[nbits], huff_code[nbits]);
395 
396  put_sbits(&s->pb, nbits, mant);
397  }
398 }
399 
400 static void encode_block(MpegEncContext *s, DCTELEM *block, int n)
401 {
402  int mant, nbits, code, i, j;
403  int component, dc, run, last_index, val;
404  MJpegContext *m = s->mjpeg_ctx;
405  uint8_t *huff_size_ac;
406  uint16_t *huff_code_ac;
407 
408  /* DC coef */
409  component = (n <= 3 ? 0 : (n&1) + 1);
410  dc = block[0]; /* overflow is impossible */
411  val = dc - s->last_dc[component];
412  if (n < 4) {
414  huff_size_ac = m->huff_size_ac_luminance;
415  huff_code_ac = m->huff_code_ac_luminance;
416  } else {
418  huff_size_ac = m->huff_size_ac_chrominance;
419  huff_code_ac = m->huff_code_ac_chrominance;
420  }
421  s->last_dc[component] = dc;
422 
423  /* AC coefs */
424 
425  run = 0;
426  last_index = s->block_last_index[n];
427  for(i=1;i<=last_index;i++) {
428  j = s->intra_scantable.permutated[i];
429  val = block[j];
430  if (val == 0) {
431  run++;
432  } else {
433  while (run >= 16) {
434  put_bits(&s->pb, huff_size_ac[0xf0], huff_code_ac[0xf0]);
435  run -= 16;
436  }
437  mant = val;
438  if (val < 0) {
439  val = -val;
440  mant--;
441  }
442 
443  nbits= av_log2(val) + 1;
444  code = (run << 4) | nbits;
445 
446  put_bits(&s->pb, huff_size_ac[code], huff_code_ac[code]);
447 
448  put_sbits(&s->pb, nbits, mant);
449  run = 0;
450  }
451  }
452 
453  /* output EOB only if not already 64 values */
454  if (last_index < 63 || run != 0)
455  put_bits(&s->pb, huff_size_ac[0], huff_code_ac[0]);
456 }
457 
459 {
460  int i;
461  if (s->chroma_format == CHROMA_444) {
462  encode_block(s, block[0], 0);
463  encode_block(s, block[2], 2);
464  encode_block(s, block[4], 4);
465  encode_block(s, block[8], 8);
466  encode_block(s, block[5], 5);
467  encode_block(s, block[9], 9);
468 
469  if (16*s->mb_x+8 < s->width) {
470  encode_block(s, block[1], 1);
471  encode_block(s, block[3], 3);
472  encode_block(s, block[6], 6);
473  encode_block(s, block[10], 10);
474  encode_block(s, block[7], 7);
475  encode_block(s, block[11], 11);
476  }
477  } else {
478  for(i=0;i<5;i++) {
479  encode_block(s, block[i], i);
480  }
481  if (s->chroma_format == CHROMA_420) {
482  encode_block(s, block[5], 5);
483  } else {
484  encode_block(s, block[6], 6);
485  encode_block(s, block[5], 5);
486  encode_block(s, block[7], 7);
487  }
488  }
489 
490  s->i_tex_bits += get_bits_diff(s);
491 }
492 
493 // maximum over s->mjpeg_vsample[i]
494 #define V_MAX 2
496  const AVFrame *pic_arg, int *got_packet)
497 
498 {
499  MpegEncContext *s = avctx->priv_data;
500  AVFrame pic = *pic_arg;
501  int i;
502 
503  //CODEC_FLAG_EMU_EDGE have to be cleared
505  return -1;
506 
507  //picture should be flipped upside-down
508  for(i=0; i < 3; i++) {
509  pic.data[i] += (pic.linesize[i] * (s->mjpeg_vsample[i] * (8 * s->mb_height -((s->height/V_MAX)&7)) - 1 ));
510  pic.linesize[i] *= -1;
511  }
512  return ff_MPV_encode_picture(avctx, pkt, &pic, got_packet);
513 }
514 
515 #if CONFIG_MJPEG_ENCODER
516 AVCodec ff_mjpeg_encoder = {
517  .name = "mjpeg",
518  .type = AVMEDIA_TYPE_VIDEO,
519  .id = AV_CODEC_ID_MJPEG,
520  .priv_data_size = sizeof(MpegEncContext),
522  .encode2 = ff_MPV_encode_picture,
525  .pix_fmts = (const enum AVPixelFormat[]){
527  },
528  .long_name = NULL_IF_CONFIG_SMALL("MJPEG (Motion JPEG)"),
529 };
530 #endif
531 #if CONFIG_AMV_ENCODER
532 AVCodec ff_amv_encoder = {
533  .name = "amv",
534  .type = AVMEDIA_TYPE_VIDEO,
535  .id = AV_CODEC_ID_AMV,
536  .priv_data_size = sizeof(MpegEncContext),
538  .encode2 = amv_encode_picture,
540  .pix_fmts = (const enum AVPixelFormat[]){
542  },
543  .long_name = NULL_IF_CONFIG_SMALL("AMV Video"),
544 };
545 #endif