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