3 * Copyright (c) 2000, 2001 Fabrice Bellard
4 * Copyright (c) 2003 Alex Beregszaszi
5 * Copyright (c) 2003-2004 Michael Niedermayer
7 * Support for external huffman table, various fixes (AVID workaround),
8 * aspecting, new decode_frame mechanism and apple mjpeg-b support
11 * This file is part of FFmpeg.
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.
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.
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
34 #include "mpegvideo.h"
38 /* use two quantizer tables (one for luminance and one for chrominance) */
43 av_cold int ff_mjpeg_encode_init(MpegEncContext *s)
47 if (s->width > 65500 || s->height > 65500) {
48 av_log(s, AV_LOG_ERROR, "JPEG does not support resolutions above 65500x65500\n");
52 m = av_malloc(sizeof(MJpegContext));
59 /* build all the huffman tables */
60 ff_mjpeg_build_huffman_codes(m->huff_size_dc_luminance,
61 m->huff_code_dc_luminance,
62 avpriv_mjpeg_bits_dc_luminance,
64 ff_mjpeg_build_huffman_codes(m->huff_size_dc_chrominance,
65 m->huff_code_dc_chrominance,
66 avpriv_mjpeg_bits_dc_chrominance,
68 ff_mjpeg_build_huffman_codes(m->huff_size_ac_luminance,
69 m->huff_code_ac_luminance,
70 avpriv_mjpeg_bits_ac_luminance,
71 avpriv_mjpeg_val_ac_luminance);
72 ff_mjpeg_build_huffman_codes(m->huff_size_ac_chrominance,
73 m->huff_code_ac_chrominance,
74 avpriv_mjpeg_bits_ac_chrominance,
75 avpriv_mjpeg_val_ac_chrominance);
81 void ff_mjpeg_encode_close(MpegEncContext *s)
83 av_free(s->mjpeg_ctx);
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)
90 PutBitContext *p = &s->pb;
93 put_bits(p, 4, table_class);
94 put_bits(p, 4, table_id);
99 put_bits(p, 8, bits_table[i]);
103 put_bits(p, 8, value_table[i]);
108 static void jpeg_table_header(MpegEncContext *s)
110 PutBitContext *p = &s->pb;
114 if (s->avctx->codec_id != AV_CODEC_ID_LJPEG) {
118 put_bits(p, 16, 2 + 2 * (1 + 64));
120 put_bits(p, 16, 2 + 1 * (1 + 64));
122 put_bits(p, 4, 0); /* 8 bit precision */
123 put_bits(p, 4, 0); /* table 0 */
125 j = s->intra_scantable.permutated[i];
126 put_bits(p, 8, s->intra_matrix[j]);
129 put_bits(p, 4, 0); /* 8 bit precision */
130 put_bits(p, 4, 1); /* table 1 */
132 j = s->intra_scantable.permutated[i];
133 put_bits(p, 8, s->chroma_intra_matrix[j]);
138 if(s->avctx->active_thread_type & FF_THREAD_SLICE){
141 put_bits(p, 16, (s->width-1)/(8*s->mjpeg_hsample[0]) + 1);
147 ptr = put_bits_ptr(p);
148 put_bits(p, 16, 0); /* patched later */
150 size += put_huffman_table(s, 0, 0, avpriv_mjpeg_bits_dc_luminance,
151 avpriv_mjpeg_val_dc);
152 size += put_huffman_table(s, 0, 1, avpriv_mjpeg_bits_dc_chrominance,
153 avpriv_mjpeg_val_dc);
155 size += put_huffman_table(s, 1, 0, avpriv_mjpeg_bits_ac_luminance,
156 avpriv_mjpeg_val_ac_luminance);
157 size += put_huffman_table(s, 1, 1, avpriv_mjpeg_bits_ac_chrominance,
158 avpriv_mjpeg_val_ac_chrominance);
162 static void jpeg_put_comments(MpegEncContext *s)
164 PutBitContext *p = &s->pb;
168 if (s->avctx->sample_aspect_ratio.num /* && !lossless */)
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 */
176 put_bits(p, 16, s->avctx->sample_aspect_ratio.num);
177 put_bits(p, 16, s->avctx->sample_aspect_ratio.den);
178 put_bits(p, 8, 0); /* thumbnail width */
179 put_bits(p, 8, 0); /* thumbnail height */
183 if(!(s->flags & CODEC_FLAG_BITEXACT)){
186 ptr = put_bits_ptr(p);
187 put_bits(p, 16, 0); /* patched later */
188 avpriv_put_string(p, LIBAVCODEC_IDENT, 1);
189 size = strlen(LIBAVCODEC_IDENT)+3;
193 if( s->avctx->pix_fmt == AV_PIX_FMT_YUV420P
194 ||s->avctx->pix_fmt == AV_PIX_FMT_YUV422P
195 ||s->avctx->pix_fmt == AV_PIX_FMT_YUV444P){
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;
206 void ff_mjpeg_encode_picture_header(MpegEncContext *s)
208 const int lossless= s->avctx->codec_id != AV_CODEC_ID_MJPEG;
211 put_marker(&s->pb, SOI);
213 // hack for AMV mjpeg format
214 if(s->avctx->codec_id == AV_CODEC_ID_AMV) goto end;
216 jpeg_put_comments(s);
218 jpeg_table_header(s);
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);
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 */
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 */
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 */
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 */
248 put_bits(&s->pb, 8, lossless ? 0 : 1); /* select matrix */
250 put_bits(&s->pb, 8, 0); /* select matrix */
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 */
258 put_bits(&s->pb, 8, lossless ? 0 : 1); /* select matrix */
260 put_bits(&s->pb, 8, 0); /* select matrix */
264 put_marker(&s->pb, SOS);
265 put_bits(&s->pb, 16, 12); /* length */
266 put_bits(&s->pb, 8, 3); /* 3 components */
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 */
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 */
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 */
283 put_bits(&s->pb, 8, lossless ? s->avctx->prediction_method+1 : 0); /* Ss (not used) */
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);
291 put_bits(&s->pb, 8, 0); /* Ah/Al (not used) */
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;
299 static void escape_FF(MpegEncContext *s, int start)
301 int size= put_bits_count(&s->pb) - start*8;
303 uint8_t *buf= s->pb.buf + start;
304 int align= (-(size_t)(buf))&3;
306 av_assert1((size&7) == 0);
310 for(i=0; i<size && i<align; i++){
311 if(buf[i]==0xFF) ff_count++;
313 for(; i<size-15; i+=16){
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;
331 if(buf[i]==0xFF) ff_count++;
334 if(ff_count==0) return;
336 flush_put_bits(&s->pb);
337 skip_put_bytes(&s->pb, ff_count);
339 for(i=size-1; ff_count; i--){
351 void ff_mjpeg_encode_stuffing(MpegEncContext *s)
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);
359 flush_put_bits(&s->pb);
360 escape_FF(s, s->esc_pos);
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;
367 s->last_dc[i] = 128 << s->intra_dc_precision;
370 void ff_mjpeg_encode_picture_trailer(MpegEncContext *s)
373 av_assert1((s->header_bits&7)==0);
376 put_marker(&s->pb, EOI);
379 void ff_mjpeg_encode_dc(MpegEncContext *s, int val,
380 uint8_t *huff_size, uint16_t *huff_code)
385 put_bits(&s->pb, huff_size[0], huff_code[0]);
393 nbits= av_log2_16bit(val) + 1;
395 put_bits(&s->pb, huff_size[nbits], huff_code[nbits]);
397 put_sbits(&s->pb, nbits, mant);
401 static void encode_block(MpegEncContext *s, int16_t *block, int n)
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;
410 component = (n <= 3 ? 0 : (n&1) + 1);
411 dc = block[0]; /* overflow is impossible */
412 val = dc - s->last_dc[component];
414 ff_mjpeg_encode_dc(s, val, m->huff_size_dc_luminance, m->huff_code_dc_luminance);
415 huff_size_ac = m->huff_size_ac_luminance;
416 huff_code_ac = m->huff_code_ac_luminance;
418 ff_mjpeg_encode_dc(s, val, m->huff_size_dc_chrominance, m->huff_code_dc_chrominance);
419 huff_size_ac = m->huff_size_ac_chrominance;
420 huff_code_ac = m->huff_code_ac_chrominance;
422 s->last_dc[component] = dc;
427 last_index = s->block_last_index[n];
428 for(i=1;i<=last_index;i++) {
429 j = s->intra_scantable.permutated[i];
435 put_bits(&s->pb, huff_size_ac[0xf0], huff_code_ac[0xf0]);
444 nbits= av_log2(val) + 1;
445 code = (run << 4) | nbits;
447 put_bits(&s->pb, huff_size_ac[code], huff_code_ac[code]);
449 put_sbits(&s->pb, nbits, mant);
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]);
459 void ff_mjpeg_encode_mb(MpegEncContext *s, int16_t block[6][64])
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);
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);
480 encode_block(s, block[i], i);
482 if (s->chroma_format == CHROMA_420) {
483 encode_block(s, block[5], 5);
485 encode_block(s, block[6], 6);
486 encode_block(s, block[5], 5);
487 encode_block(s, block[7], 7);
491 s->i_tex_bits += get_bits_diff(s);
494 // maximum over s->mjpeg_vsample[i]
496 static int amv_encode_picture(AVCodecContext *avctx, AVPacket *pkt,
497 const AVFrame *pic_arg, int *got_packet)
500 MpegEncContext *s = avctx->priv_data;
501 AVFrame pic = *pic_arg;
504 //CODEC_FLAG_EMU_EDGE have to be cleared
505 if(s->avctx->flags & CODEC_FLAG_EMU_EDGE)
508 //picture should be flipped upside-down
509 for(i=0; i < 3; i++) {
510 pic.data[i] += (pic.linesize[i] * (s->mjpeg_vsample[i] * (8 * s->mb_height -((s->height/V_MAX)&7)) - 1 ));
511 pic.linesize[i] *= -1;
513 return ff_MPV_encode_picture(avctx, pkt, &pic, got_packet);
516 #if CONFIG_MJPEG_ENCODER
517 AVCodec ff_mjpeg_encoder = {
519 .long_name = NULL_IF_CONFIG_SMALL("MJPEG (Motion JPEG)"),
520 .type = AVMEDIA_TYPE_VIDEO,
521 .id = AV_CODEC_ID_MJPEG,
522 .priv_data_size = sizeof(MpegEncContext),
523 .init = ff_MPV_encode_init,
524 .encode2 = ff_MPV_encode_picture,
525 .close = ff_MPV_encode_end,
526 .capabilities = CODEC_CAP_SLICE_THREADS | CODEC_CAP_FRAME_THREADS | CODEC_CAP_INTRA_ONLY,
527 .pix_fmts = (const enum AVPixelFormat[]){
528 AV_PIX_FMT_YUVJ420P, AV_PIX_FMT_YUVJ422P, AV_PIX_FMT_YUVJ444P, AV_PIX_FMT_NONE
532 #if CONFIG_AMV_ENCODER
533 AVCodec ff_amv_encoder = {
535 .long_name = NULL_IF_CONFIG_SMALL("AMV Video"),
536 .type = AVMEDIA_TYPE_VIDEO,
537 .id = AV_CODEC_ID_AMV,
538 .priv_data_size = sizeof(MpegEncContext),
539 .init = ff_MPV_encode_init,
540 .encode2 = amv_encode_picture,
541 .close = ff_MPV_encode_end,
542 .pix_fmts = (const enum AVPixelFormat[]){
543 AV_PIX_FMT_YUVJ420P, AV_PIX_FMT_YUVJ422P, AV_PIX_FMT_NONE