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
38 #include "mpegvideo.h"
42 /* use two quantizer tables (one for luminance and one for chrominance) */
47 av_cold int ff_mjpeg_encode_init(MpegEncContext *s)
51 m = av_malloc(sizeof(MJpegContext));
58 /* build all the huffman tables */
59 ff_mjpeg_build_huffman_codes(m->huff_size_dc_luminance,
60 m->huff_code_dc_luminance,
61 ff_mjpeg_bits_dc_luminance,
63 ff_mjpeg_build_huffman_codes(m->huff_size_dc_chrominance,
64 m->huff_code_dc_chrominance,
65 ff_mjpeg_bits_dc_chrominance,
67 ff_mjpeg_build_huffman_codes(m->huff_size_ac_luminance,
68 m->huff_code_ac_luminance,
69 ff_mjpeg_bits_ac_luminance,
70 ff_mjpeg_val_ac_luminance);
71 ff_mjpeg_build_huffman_codes(m->huff_size_ac_chrominance,
72 m->huff_code_ac_chrominance,
73 ff_mjpeg_bits_ac_chrominance,
74 ff_mjpeg_val_ac_chrominance);
80 void ff_mjpeg_encode_close(MpegEncContext *s)
82 av_free(s->mjpeg_ctx);
85 /* table_class: 0 = DC coef, 1 = AC coefs */
86 static int put_huffman_table(MpegEncContext *s, int table_class, int table_id,
87 const uint8_t *bits_table, const uint8_t *value_table)
89 PutBitContext *p = &s->pb;
92 put_bits(p, 4, table_class);
93 put_bits(p, 4, table_id);
98 put_bits(p, 8, bits_table[i]);
102 put_bits(p, 8, value_table[i]);
107 static void jpeg_table_header(MpegEncContext *s)
109 PutBitContext *p = &s->pb;
116 put_bits(p, 16, 2 + 2 * (1 + 64));
118 put_bits(p, 16, 2 + 1 * (1 + 64));
120 put_bits(p, 4, 0); /* 8 bit precision */
121 put_bits(p, 4, 0); /* table 0 */
123 j = s->intra_scantable.permutated[i];
124 put_bits(p, 8, s->intra_matrix[j]);
127 put_bits(p, 4, 0); /* 8 bit precision */
128 put_bits(p, 4, 1); /* table 1 */
130 j = s->intra_scantable.permutated[i];
131 put_bits(p, 8, s->chroma_intra_matrix[j]);
138 ptr = put_bits_ptr(p);
139 put_bits(p, 16, 0); /* patched later */
141 size += put_huffman_table(s, 0, 0, ff_mjpeg_bits_dc_luminance,
143 size += put_huffman_table(s, 0, 1, ff_mjpeg_bits_dc_chrominance,
146 size += put_huffman_table(s, 1, 0, ff_mjpeg_bits_ac_luminance,
147 ff_mjpeg_val_ac_luminance);
148 size += put_huffman_table(s, 1, 1, ff_mjpeg_bits_ac_chrominance,
149 ff_mjpeg_val_ac_chrominance);
153 static void jpeg_put_comments(MpegEncContext *s)
155 PutBitContext *p = &s->pb;
159 if (s->avctx->sample_aspect_ratio.num /* && !lossless */)
164 ff_put_string(p, "JFIF", 1); /* this puts the trailing zero-byte too */
165 put_bits(p, 16, 0x0102); /* v 1.02 */
166 put_bits(p, 8, 0); /* units type: 0 - aspect ratio */
167 put_bits(p, 16, s->avctx->sample_aspect_ratio.num);
168 put_bits(p, 16, s->avctx->sample_aspect_ratio.den);
169 put_bits(p, 8, 0); /* thumbnail width */
170 put_bits(p, 8, 0); /* thumbnail height */
174 if(!(s->flags & CODEC_FLAG_BITEXACT)){
177 ptr = put_bits_ptr(p);
178 put_bits(p, 16, 0); /* patched later */
179 ff_put_string(p, LIBAVCODEC_IDENT, 1);
180 size = strlen(LIBAVCODEC_IDENT)+3;
184 if( s->avctx->pix_fmt == PIX_FMT_YUV420P
185 ||s->avctx->pix_fmt == PIX_FMT_YUV422P
186 ||s->avctx->pix_fmt == PIX_FMT_YUV444P){
189 ptr = put_bits_ptr(p);
190 put_bits(p, 16, 0); /* patched later */
191 ff_put_string(p, "CS=ITU601", 1);
192 size = strlen("CS=ITU601")+3;
197 void ff_mjpeg_encode_picture_header(MpegEncContext *s)
199 const int lossless= s->avctx->codec_id != CODEC_ID_MJPEG;
201 put_marker(&s->pb, SOI);
203 // hack for AMV mjpeg format
204 if(s->avctx->codec_id == CODEC_ID_AMV) return;
206 jpeg_put_comments(s);
208 jpeg_table_header(s);
210 switch(s->avctx->codec_id){
211 case CODEC_ID_MJPEG: put_marker(&s->pb, SOF0 ); break;
212 case CODEC_ID_LJPEG: put_marker(&s->pb, SOF3 ); break;
216 put_bits(&s->pb, 16, 17);
217 if(lossless && s->avctx->pix_fmt == PIX_FMT_BGRA)
218 put_bits(&s->pb, 8, 9); /* 9 bits/component RCT */
220 put_bits(&s->pb, 8, 8); /* 8 bits/component */
221 put_bits(&s->pb, 16, s->height);
222 put_bits(&s->pb, 16, s->width);
223 put_bits(&s->pb, 8, 3); /* 3 components */
226 put_bits(&s->pb, 8, 1); /* component number */
227 put_bits(&s->pb, 4, s->mjpeg_hsample[0]); /* H factor */
228 put_bits(&s->pb, 4, s->mjpeg_vsample[0]); /* V factor */
229 put_bits(&s->pb, 8, 0); /* select matrix */
232 put_bits(&s->pb, 8, 2); /* component number */
233 put_bits(&s->pb, 4, s->mjpeg_hsample[1]); /* H factor */
234 put_bits(&s->pb, 4, s->mjpeg_vsample[1]); /* V factor */
236 put_bits(&s->pb, 8, lossless ? 0 : 1); /* select matrix */
238 put_bits(&s->pb, 8, 0); /* select matrix */
242 put_bits(&s->pb, 8, 3); /* component number */
243 put_bits(&s->pb, 4, s->mjpeg_hsample[2]); /* H factor */
244 put_bits(&s->pb, 4, s->mjpeg_vsample[2]); /* V factor */
246 put_bits(&s->pb, 8, lossless ? 0 : 1); /* select matrix */
248 put_bits(&s->pb, 8, 0); /* select matrix */
252 put_marker(&s->pb, SOS);
253 put_bits(&s->pb, 16, 12); /* length */
254 put_bits(&s->pb, 8, 3); /* 3 components */
257 put_bits(&s->pb, 8, 1); /* index */
258 put_bits(&s->pb, 4, 0); /* DC huffman table index */
259 put_bits(&s->pb, 4, 0); /* AC huffman table index */
262 put_bits(&s->pb, 8, 2); /* index */
263 put_bits(&s->pb, 4, 1); /* DC huffman table index */
264 put_bits(&s->pb, 4, lossless ? 0 : 1); /* AC huffman table index */
267 put_bits(&s->pb, 8, 3); /* index */
268 put_bits(&s->pb, 4, 1); /* DC huffman table index */
269 put_bits(&s->pb, 4, lossless ? 0 : 1); /* AC huffman table index */
271 put_bits(&s->pb, 8, lossless ? s->avctx->prediction_method+1 : 0); /* Ss (not used) */
273 switch(s->avctx->codec_id){
274 case CODEC_ID_MJPEG: put_bits(&s->pb, 8, 63); break; /* Se (not used) */
275 case CODEC_ID_LJPEG: put_bits(&s->pb, 8, 0); break; /* not used */
279 put_bits(&s->pb, 8, 0); /* Ah/Al (not used) */
282 static void escape_FF(MpegEncContext *s, int start)
284 int size= put_bits_count(&s->pb) - start*8;
286 uint8_t *buf= s->pb.buf + start;
287 int align= (-(size_t)(buf))&3;
289 assert((size&7) == 0);
293 for(i=0; i<size && i<align; i++){
294 if(buf[i]==0xFF) ff_count++;
296 for(; i<size-15; i+=16){
299 v= *(uint32_t*)(&buf[i]);
300 acc= (((v & (v>>4))&0x0F0F0F0F)+0x01010101)&0x10101010;
301 v= *(uint32_t*)(&buf[i+4]);
302 acc+=(((v & (v>>4))&0x0F0F0F0F)+0x01010101)&0x10101010;
303 v= *(uint32_t*)(&buf[i+8]);
304 acc+=(((v & (v>>4))&0x0F0F0F0F)+0x01010101)&0x10101010;
305 v= *(uint32_t*)(&buf[i+12]);
306 acc+=(((v & (v>>4))&0x0F0F0F0F)+0x01010101)&0x10101010;
314 if(buf[i]==0xFF) ff_count++;
317 if(ff_count==0) return;
319 flush_put_bits(&s->pb);
320 skip_put_bytes(&s->pb, ff_count);
322 for(i=size-1; ff_count; i--){
326 //printf("%d %d\n", i, ff_count);
335 void ff_mjpeg_encode_stuffing(PutBitContext * pbc)
338 length= (-put_bits_count(pbc))&7;
339 if(length) put_bits(pbc, length, (1<<length)-1);
342 void ff_mjpeg_encode_picture_trailer(MpegEncContext *s)
344 ff_mjpeg_encode_stuffing(&s->pb);
345 flush_put_bits(&s->pb);
347 assert((s->header_bits&7)==0);
349 escape_FF(s, s->header_bits>>3);
351 put_marker(&s->pb, EOI);
354 void ff_mjpeg_encode_dc(MpegEncContext *s, int val,
355 uint8_t *huff_size, uint16_t *huff_code)
360 put_bits(&s->pb, huff_size[0], huff_code[0]);
368 nbits= av_log2_16bit(val) + 1;
370 put_bits(&s->pb, huff_size[nbits], huff_code[nbits]);
372 put_sbits(&s->pb, nbits, mant);
376 static void encode_block(MpegEncContext *s, DCTELEM *block, int n)
378 int mant, nbits, code, i, j;
379 int component, dc, run, last_index, val;
380 MJpegContext *m = s->mjpeg_ctx;
381 uint8_t *huff_size_ac;
382 uint16_t *huff_code_ac;
385 component = (n <= 3 ? 0 : (n&1) + 1);
386 dc = block[0]; /* overflow is impossible */
387 val = dc - s->last_dc[component];
389 ff_mjpeg_encode_dc(s, val, m->huff_size_dc_luminance, m->huff_code_dc_luminance);
390 huff_size_ac = m->huff_size_ac_luminance;
391 huff_code_ac = m->huff_code_ac_luminance;
393 ff_mjpeg_encode_dc(s, val, m->huff_size_dc_chrominance, m->huff_code_dc_chrominance);
394 huff_size_ac = m->huff_size_ac_chrominance;
395 huff_code_ac = m->huff_code_ac_chrominance;
397 s->last_dc[component] = dc;
402 last_index = s->block_last_index[n];
403 for(i=1;i<=last_index;i++) {
404 j = s->intra_scantable.permutated[i];
410 put_bits(&s->pb, huff_size_ac[0xf0], huff_code_ac[0xf0]);
419 nbits= av_log2(val) + 1;
420 code = (run << 4) | nbits;
422 put_bits(&s->pb, huff_size_ac[code], huff_code_ac[code]);
424 put_sbits(&s->pb, nbits, mant);
429 /* output EOB only if not already 64 values */
430 if (last_index < 63 || run != 0)
431 put_bits(&s->pb, huff_size_ac[0], huff_code_ac[0]);
434 void ff_mjpeg_encode_mb(MpegEncContext *s, DCTELEM block[6][64])
438 encode_block(s, block[i], i);
440 if (s->chroma_format == CHROMA_420) {
441 encode_block(s, block[5], 5);
443 encode_block(s, block[6], 6);
444 encode_block(s, block[5], 5);
445 encode_block(s, block[7], 7);
448 s->i_tex_bits += get_bits_diff(s);
451 // maximum over s->mjpeg_vsample[i]
453 static int amv_encode_picture(AVCodecContext *avctx,
454 unsigned char *buf, int buf_size, void *data)
458 MpegEncContext *s = avctx->priv_data;
461 //CODEC_FLAG_EMU_EDGE have to be cleared
462 if(s->avctx->flags & CODEC_FLAG_EMU_EDGE)
465 //picture should be flipped upside-down
466 for(i=0; i < 3; i++) {
467 pic->data[i] += (pic->linesize[i] * (s->mjpeg_vsample[i] * (8 * s->mb_height -((s->height/V_MAX)&7)) - 1 ));
468 pic->linesize[i] *= -1;
470 return MPV_encode_picture(avctx,buf, buf_size, pic);
473 AVCodec ff_mjpeg_encoder = {
475 .type = AVMEDIA_TYPE_VIDEO,
476 .id = CODEC_ID_MJPEG,
477 .priv_data_size = sizeof(MpegEncContext),
478 .init = MPV_encode_init,
479 .encode = MPV_encode_picture,
480 .close = MPV_encode_end,
481 .pix_fmts= (const enum PixelFormat[]){PIX_FMT_YUVJ420P, PIX_FMT_YUVJ422P, PIX_FMT_NONE},
482 .long_name= NULL_IF_CONFIG_SMALL("MJPEG (Motion JPEG)"),
485 AVCodec ff_amv_encoder = {
487 .type = AVMEDIA_TYPE_VIDEO,
489 .priv_data_size = sizeof(MpegEncContext),
490 .init = MPV_encode_init,
491 .encode = amv_encode_picture,
492 .close = MPV_encode_end,
493 .pix_fmts= (enum PixelFormat[]){PIX_FMT_YUVJ420P, PIX_FMT_YUVJ422P, -1},
494 .long_name = NULL_IF_CONFIG_SMALL("AMV Video"),