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