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
35 #include "mpegvideo.h"
39 /* use two quantizer tables (one for luminance and one for chrominance) */
44 av_cold int ff_mjpeg_encode_init(MpegEncContext *s)
48 if (s->width > 65500 || s->height > 65500) {
49 av_log(s, AV_LOG_ERROR, "JPEG does not support resolutions above 65500x65500\n");
53 m = av_malloc(sizeof(MJpegContext));
60 /* build all the huffman tables */
61 ff_mjpeg_build_huffman_codes(m->huff_size_dc_luminance,
62 m->huff_code_dc_luminance,
63 avpriv_mjpeg_bits_dc_luminance,
65 ff_mjpeg_build_huffman_codes(m->huff_size_dc_chrominance,
66 m->huff_code_dc_chrominance,
67 avpriv_mjpeg_bits_dc_chrominance,
69 ff_mjpeg_build_huffman_codes(m->huff_size_ac_luminance,
70 m->huff_code_ac_luminance,
71 avpriv_mjpeg_bits_ac_luminance,
72 avpriv_mjpeg_val_ac_luminance);
73 ff_mjpeg_build_huffman_codes(m->huff_size_ac_chrominance,
74 m->huff_code_ac_chrominance,
75 avpriv_mjpeg_bits_ac_chrominance,
76 avpriv_mjpeg_val_ac_chrominance);
82 void ff_mjpeg_encode_close(MpegEncContext *s)
84 av_free(s->mjpeg_ctx);
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)
91 PutBitContext *p = &s->pb;
94 put_bits(p, 4, table_class);
95 put_bits(p, 4, table_id);
100 put_bits(p, 8, bits_table[i]);
104 put_bits(p, 8, value_table[i]);
109 static void jpeg_table_header(MpegEncContext *s)
111 PutBitContext *p = &s->pb;
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]);
137 if(s->avctx->active_thread_type & FF_THREAD_SLICE){
140 put_bits(p, 16, (s->width-1)/(8*s->mjpeg_hsample[0]) + 1);
146 ptr = put_bits_ptr(p);
147 put_bits(p, 16, 0); /* patched later */
149 size += put_huffman_table(s, 0, 0, avpriv_mjpeg_bits_dc_luminance,
150 avpriv_mjpeg_val_dc);
151 size += put_huffman_table(s, 0, 1, avpriv_mjpeg_bits_dc_chrominance,
152 avpriv_mjpeg_val_dc);
154 size += put_huffman_table(s, 1, 0, avpriv_mjpeg_bits_ac_luminance,
155 avpriv_mjpeg_val_ac_luminance);
156 size += put_huffman_table(s, 1, 1, avpriv_mjpeg_bits_ac_chrominance,
157 avpriv_mjpeg_val_ac_chrominance);
161 static void jpeg_put_comments(MpegEncContext *s)
163 PutBitContext *p = &s->pb;
167 if (s->avctx->sample_aspect_ratio.num /* && !lossless */)
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 */
175 put_bits(p, 16, s->avctx->sample_aspect_ratio.num);
176 put_bits(p, 16, s->avctx->sample_aspect_ratio.den);
177 put_bits(p, 8, 0); /* thumbnail width */
178 put_bits(p, 8, 0); /* thumbnail height */
182 if(!(s->flags & CODEC_FLAG_BITEXACT)){
185 ptr = put_bits_ptr(p);
186 put_bits(p, 16, 0); /* patched later */
187 avpriv_put_string(p, LIBAVCODEC_IDENT, 1);
188 size = strlen(LIBAVCODEC_IDENT)+3;
192 if( s->avctx->pix_fmt == AV_PIX_FMT_YUV420P
193 ||s->avctx->pix_fmt == AV_PIX_FMT_YUV422P
194 ||s->avctx->pix_fmt == AV_PIX_FMT_YUV444P){
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;
205 void ff_mjpeg_encode_picture_header(MpegEncContext *s)
207 const int lossless= s->avctx->codec_id != AV_CODEC_ID_MJPEG;
210 put_marker(&s->pb, SOI);
212 // hack for AMV mjpeg format
213 if(s->avctx->codec_id == AV_CODEC_ID_AMV) goto end;
215 jpeg_put_comments(s);
217 jpeg_table_header(s);
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);
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 */
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 */
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 */
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 */
247 put_bits(&s->pb, 8, lossless ? 0 : 1); /* select matrix */
249 put_bits(&s->pb, 8, 0); /* select matrix */
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 */
257 put_bits(&s->pb, 8, lossless ? 0 : 1); /* select matrix */
259 put_bits(&s->pb, 8, 0); /* select matrix */
263 put_marker(&s->pb, SOS);
264 put_bits(&s->pb, 16, 12); /* length */
265 put_bits(&s->pb, 8, 3); /* 3 components */
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 */
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 */
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 */
282 put_bits(&s->pb, 8, lossless ? s->avctx->prediction_method+1 : 0); /* Ss (not used) */
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);
290 put_bits(&s->pb, 8, 0); /* Ah/Al (not used) */
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;
298 static void escape_FF(MpegEncContext *s, int start)
300 int size= put_bits_count(&s->pb) - start*8;
302 uint8_t *buf= s->pb.buf + start;
303 int align= (-(size_t)(buf))&3;
305 av_assert1((size&7) == 0);
309 for(i=0; i<size && i<align; i++){
310 if(buf[i]==0xFF) ff_count++;
312 for(; i<size-15; i+=16){
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;
330 if(buf[i]==0xFF) ff_count++;
333 if(ff_count==0) return;
335 flush_put_bits(&s->pb);
336 skip_put_bytes(&s->pb, ff_count);
338 for(i=size-1; ff_count; i--){
350 void ff_mjpeg_encode_stuffing(MpegEncContext *s)
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);
358 flush_put_bits(&s->pb);
359 escape_FF(s, s->esc_pos);
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;
366 s->last_dc[i] = 128 << s->intra_dc_precision;
369 void ff_mjpeg_encode_picture_trailer(MpegEncContext *s)
372 av_assert1((s->header_bits&7)==0);
375 put_marker(&s->pb, EOI);
378 void ff_mjpeg_encode_dc(MpegEncContext *s, int val,
379 uint8_t *huff_size, uint16_t *huff_code)
384 put_bits(&s->pb, huff_size[0], huff_code[0]);
392 nbits= av_log2_16bit(val) + 1;
394 put_bits(&s->pb, huff_size[nbits], huff_code[nbits]);
396 put_sbits(&s->pb, nbits, mant);
400 static void encode_block(MpegEncContext *s, int16_t *block, int n)
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;
409 component = (n <= 3 ? 0 : (n&1) + 1);
410 dc = block[0]; /* overflow is impossible */
411 val = dc - s->last_dc[component];
413 ff_mjpeg_encode_dc(s, val, m->huff_size_dc_luminance, m->huff_code_dc_luminance);
414 huff_size_ac = m->huff_size_ac_luminance;
415 huff_code_ac = m->huff_code_ac_luminance;
417 ff_mjpeg_encode_dc(s, val, m->huff_size_dc_chrominance, m->huff_code_dc_chrominance);
418 huff_size_ac = m->huff_size_ac_chrominance;
419 huff_code_ac = m->huff_code_ac_chrominance;
421 s->last_dc[component] = dc;
426 last_index = s->block_last_index[n];
427 for(i=1;i<=last_index;i++) {
428 j = s->intra_scantable.permutated[i];
434 put_bits(&s->pb, huff_size_ac[0xf0], huff_code_ac[0xf0]);
443 nbits= av_log2(val) + 1;
444 code = (run << 4) | nbits;
446 put_bits(&s->pb, huff_size_ac[code], huff_code_ac[code]);
448 put_sbits(&s->pb, nbits, mant);
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]);
458 void ff_mjpeg_encode_mb(MpegEncContext *s, int16_t block[6][64])
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);
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);
479 encode_block(s, block[i], i);
481 if (s->chroma_format == CHROMA_420) {
482 encode_block(s, block[5], 5);
484 encode_block(s, block[6], 6);
485 encode_block(s, block[5], 5);
486 encode_block(s, block[7], 7);
490 s->i_tex_bits += get_bits_diff(s);
493 // maximum over s->mjpeg_vsample[i]
495 static int amv_encode_picture(AVCodecContext *avctx, AVPacket *pkt,
496 const AVFrame *pic_arg, int *got_packet)
499 MpegEncContext *s = avctx->priv_data;
500 AVFrame pic = *pic_arg;
503 //CODEC_FLAG_EMU_EDGE have to be cleared
504 if(s->avctx->flags & CODEC_FLAG_EMU_EDGE)
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;
512 return ff_MPV_encode_picture(avctx, pkt, &pic, got_packet);
515 #if CONFIG_MJPEG_ENCODER
516 AVCodec ff_mjpeg_encoder = {
518 .type = AVMEDIA_TYPE_VIDEO,
519 .id = AV_CODEC_ID_MJPEG,
520 .priv_data_size = sizeof(MpegEncContext),
521 .init = ff_MPV_encode_init,
522 .encode2 = ff_MPV_encode_picture,
523 .close = ff_MPV_encode_end,
524 .capabilities = CODEC_CAP_SLICE_THREADS | CODEC_CAP_FRAME_THREADS | CODEC_CAP_INTRA_ONLY,
525 .pix_fmts = (const enum AVPixelFormat[]){
526 AV_PIX_FMT_YUVJ420P, AV_PIX_FMT_YUVJ422P, AV_PIX_FMT_YUVJ444P, AV_PIX_FMT_NONE
528 .long_name = NULL_IF_CONFIG_SMALL("MJPEG (Motion JPEG)"),
531 #if CONFIG_AMV_ENCODER
532 AVCodec ff_amv_encoder = {
534 .type = AVMEDIA_TYPE_VIDEO,
535 .id = AV_CODEC_ID_AMV,
536 .priv_data_size = sizeof(MpegEncContext),
537 .init = ff_MPV_encode_init,
538 .encode2 = amv_encode_picture,
539 .close = ff_MPV_encode_end,
540 .pix_fmts = (const enum AVPixelFormat[]){
541 AV_PIX_FMT_YUVJ420P, AV_PIX_FMT_YUVJ422P, AV_PIX_FMT_NONE
543 .long_name = NULL_IF_CONFIG_SMALL("AMV Video"),