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 if (s->width > 65500 || s->height > 65500) {
52 av_log(s, AV_LOG_ERROR, "JPEG does not support resolutions above 65500x65500\n");
56 m = av_malloc(sizeof(MJpegContext));
63 /* build all the huffman tables */
64 ff_mjpeg_build_huffman_codes(m->huff_size_dc_luminance,
65 m->huff_code_dc_luminance,
66 ff_mjpeg_bits_dc_luminance,
68 ff_mjpeg_build_huffman_codes(m->huff_size_dc_chrominance,
69 m->huff_code_dc_chrominance,
70 ff_mjpeg_bits_dc_chrominance,
72 ff_mjpeg_build_huffman_codes(m->huff_size_ac_luminance,
73 m->huff_code_ac_luminance,
74 ff_mjpeg_bits_ac_luminance,
75 ff_mjpeg_val_ac_luminance);
76 ff_mjpeg_build_huffman_codes(m->huff_size_ac_chrominance,
77 m->huff_code_ac_chrominance,
78 ff_mjpeg_bits_ac_chrominance,
79 ff_mjpeg_val_ac_chrominance);
85 void ff_mjpeg_encode_close(MpegEncContext *s)
87 av_free(s->mjpeg_ctx);
90 /* table_class: 0 = DC coef, 1 = AC coefs */
91 static int put_huffman_table(MpegEncContext *s, int table_class, int table_id,
92 const uint8_t *bits_table, const uint8_t *value_table)
94 PutBitContext *p = &s->pb;
97 put_bits(p, 4, table_class);
98 put_bits(p, 4, table_id);
103 put_bits(p, 8, bits_table[i]);
107 put_bits(p, 8, value_table[i]);
112 static void jpeg_table_header(MpegEncContext *s)
114 PutBitContext *p = &s->pb;
121 put_bits(p, 16, 2 + 2 * (1 + 64));
123 put_bits(p, 16, 2 + 1 * (1 + 64));
125 put_bits(p, 4, 0); /* 8 bit precision */
126 put_bits(p, 4, 0); /* table 0 */
128 j = s->intra_scantable.permutated[i];
129 put_bits(p, 8, s->intra_matrix[j]);
132 put_bits(p, 4, 0); /* 8 bit precision */
133 put_bits(p, 4, 1); /* table 1 */
135 j = s->intra_scantable.permutated[i];
136 put_bits(p, 8, s->chroma_intra_matrix[j]);
143 ptr = put_bits_ptr(p);
144 put_bits(p, 16, 0); /* patched later */
146 size += put_huffman_table(s, 0, 0, ff_mjpeg_bits_dc_luminance,
148 size += put_huffman_table(s, 0, 1, ff_mjpeg_bits_dc_chrominance,
151 size += put_huffman_table(s, 1, 0, ff_mjpeg_bits_ac_luminance,
152 ff_mjpeg_val_ac_luminance);
153 size += put_huffman_table(s, 1, 1, ff_mjpeg_bits_ac_chrominance,
154 ff_mjpeg_val_ac_chrominance);
158 static void jpeg_put_comments(MpegEncContext *s)
160 PutBitContext *p = &s->pb;
164 if (s->avctx->sample_aspect_ratio.num /* && !lossless */)
169 ff_put_string(p, "JFIF", 1); /* this puts the trailing zero-byte too */
170 put_bits(p, 16, 0x0102); /* v 1.02 */
171 put_bits(p, 8, 0); /* units type: 0 - aspect ratio */
172 put_bits(p, 16, s->avctx->sample_aspect_ratio.num);
173 put_bits(p, 16, s->avctx->sample_aspect_ratio.den);
174 put_bits(p, 8, 0); /* thumbnail width */
175 put_bits(p, 8, 0); /* thumbnail height */
179 if(!(s->flags & CODEC_FLAG_BITEXACT)){
182 ptr = put_bits_ptr(p);
183 put_bits(p, 16, 0); /* patched later */
184 ff_put_string(p, LIBAVCODEC_IDENT, 1);
185 size = strlen(LIBAVCODEC_IDENT)+3;
189 if( s->avctx->pix_fmt == PIX_FMT_YUV420P
190 ||s->avctx->pix_fmt == PIX_FMT_YUV422P
191 ||s->avctx->pix_fmt == PIX_FMT_YUV444P){
194 ptr = put_bits_ptr(p);
195 put_bits(p, 16, 0); /* patched later */
196 ff_put_string(p, "CS=ITU601", 1);
197 size = strlen("CS=ITU601")+3;
202 void ff_mjpeg_encode_picture_header(MpegEncContext *s)
204 const int lossless= s->avctx->codec_id != CODEC_ID_MJPEG;
206 put_marker(&s->pb, SOI);
208 // hack for AMV mjpeg format
209 if(s->avctx->codec_id == CODEC_ID_AMV) return;
211 jpeg_put_comments(s);
213 jpeg_table_header(s);
215 switch(s->avctx->codec_id){
216 case CODEC_ID_MJPEG: put_marker(&s->pb, SOF0 ); break;
217 case CODEC_ID_LJPEG: put_marker(&s->pb, SOF3 ); break;
221 put_bits(&s->pb, 16, 17);
222 if(lossless && (s->avctx->pix_fmt == PIX_FMT_BGR0
223 || s->avctx->pix_fmt == PIX_FMT_BGRA
224 || s->avctx->pix_fmt == PIX_FMT_BGR24))
225 put_bits(&s->pb, 8, 9); /* 9 bits/component RCT */
227 put_bits(&s->pb, 8, 8); /* 8 bits/component */
228 put_bits(&s->pb, 16, s->height);
229 put_bits(&s->pb, 16, s->width);
230 put_bits(&s->pb, 8, 3); /* 3 components */
233 put_bits(&s->pb, 8, 1); /* component number */
234 put_bits(&s->pb, 4, s->mjpeg_hsample[0]); /* H factor */
235 put_bits(&s->pb, 4, s->mjpeg_vsample[0]); /* V factor */
236 put_bits(&s->pb, 8, 0); /* select matrix */
239 put_bits(&s->pb, 8, 2); /* component number */
240 put_bits(&s->pb, 4, s->mjpeg_hsample[1]); /* H factor */
241 put_bits(&s->pb, 4, s->mjpeg_vsample[1]); /* V factor */
243 put_bits(&s->pb, 8, lossless ? 0 : 1); /* select matrix */
245 put_bits(&s->pb, 8, 0); /* select matrix */
249 put_bits(&s->pb, 8, 3); /* component number */
250 put_bits(&s->pb, 4, s->mjpeg_hsample[2]); /* H factor */
251 put_bits(&s->pb, 4, s->mjpeg_vsample[2]); /* V factor */
253 put_bits(&s->pb, 8, lossless ? 0 : 1); /* select matrix */
255 put_bits(&s->pb, 8, 0); /* select matrix */
259 put_marker(&s->pb, SOS);
260 put_bits(&s->pb, 16, 12); /* length */
261 put_bits(&s->pb, 8, 3); /* 3 components */
264 put_bits(&s->pb, 8, 1); /* index */
265 put_bits(&s->pb, 4, 0); /* DC huffman table index */
266 put_bits(&s->pb, 4, 0); /* AC huffman table index */
269 put_bits(&s->pb, 8, 2); /* index */
270 put_bits(&s->pb, 4, 1); /* DC huffman table index */
271 put_bits(&s->pb, 4, lossless ? 0 : 1); /* AC huffman table index */
274 put_bits(&s->pb, 8, 3); /* 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 */
278 put_bits(&s->pb, 8, lossless ? s->avctx->prediction_method+1 : 0); /* Ss (not used) */
280 switch(s->avctx->codec_id){
281 case CODEC_ID_MJPEG: put_bits(&s->pb, 8, 63); break; /* Se (not used) */
282 case CODEC_ID_LJPEG: put_bits(&s->pb, 8, 0); break; /* not used */
286 put_bits(&s->pb, 8, 0); /* Ah/Al (not used) */
289 static void escape_FF(MpegEncContext *s, int start)
291 int size= put_bits_count(&s->pb) - start*8;
293 uint8_t *buf= s->pb.buf + start;
294 int align= (-(size_t)(buf))&3;
296 assert((size&7) == 0);
300 for(i=0; i<size && i<align; i++){
301 if(buf[i]==0xFF) ff_count++;
303 for(; i<size-15; i+=16){
306 v= *(uint32_t*)(&buf[i]);
307 acc= (((v & (v>>4))&0x0F0F0F0F)+0x01010101)&0x10101010;
308 v= *(uint32_t*)(&buf[i+4]);
309 acc+=(((v & (v>>4))&0x0F0F0F0F)+0x01010101)&0x10101010;
310 v= *(uint32_t*)(&buf[i+8]);
311 acc+=(((v & (v>>4))&0x0F0F0F0F)+0x01010101)&0x10101010;
312 v= *(uint32_t*)(&buf[i+12]);
313 acc+=(((v & (v>>4))&0x0F0F0F0F)+0x01010101)&0x10101010;
321 if(buf[i]==0xFF) ff_count++;
324 if(ff_count==0) return;
326 flush_put_bits(&s->pb);
327 skip_put_bytes(&s->pb, ff_count);
329 for(i=size-1; ff_count; i--){
333 //printf("%d %d\n", i, ff_count);
342 void ff_mjpeg_encode_stuffing(PutBitContext * pbc)
345 length= (-put_bits_count(pbc))&7;
346 if(length) put_bits(pbc, length, (1<<length)-1);
349 void ff_mjpeg_encode_picture_trailer(MpegEncContext *s)
351 ff_mjpeg_encode_stuffing(&s->pb);
352 flush_put_bits(&s->pb);
354 assert((s->header_bits&7)==0);
356 escape_FF(s, s->header_bits>>3);
358 put_marker(&s->pb, EOI);
361 void ff_mjpeg_encode_dc(MpegEncContext *s, int val,
362 uint8_t *huff_size, uint16_t *huff_code)
367 put_bits(&s->pb, huff_size[0], huff_code[0]);
375 nbits= av_log2_16bit(val) + 1;
377 put_bits(&s->pb, huff_size[nbits], huff_code[nbits]);
379 put_sbits(&s->pb, nbits, mant);
383 static void encode_block(MpegEncContext *s, DCTELEM *block, int n)
385 int mant, nbits, code, i, j;
386 int component, dc, run, last_index, val;
387 MJpegContext *m = s->mjpeg_ctx;
388 uint8_t *huff_size_ac;
389 uint16_t *huff_code_ac;
392 component = (n <= 3 ? 0 : (n&1) + 1);
393 dc = block[0]; /* overflow is impossible */
394 val = dc - s->last_dc[component];
396 ff_mjpeg_encode_dc(s, val, m->huff_size_dc_luminance, m->huff_code_dc_luminance);
397 huff_size_ac = m->huff_size_ac_luminance;
398 huff_code_ac = m->huff_code_ac_luminance;
400 ff_mjpeg_encode_dc(s, val, m->huff_size_dc_chrominance, m->huff_code_dc_chrominance);
401 huff_size_ac = m->huff_size_ac_chrominance;
402 huff_code_ac = m->huff_code_ac_chrominance;
404 s->last_dc[component] = dc;
409 last_index = s->block_last_index[n];
410 for(i=1;i<=last_index;i++) {
411 j = s->intra_scantable.permutated[i];
417 put_bits(&s->pb, huff_size_ac[0xf0], huff_code_ac[0xf0]);
426 nbits= av_log2(val) + 1;
427 code = (run << 4) | nbits;
429 put_bits(&s->pb, huff_size_ac[code], huff_code_ac[code]);
431 put_sbits(&s->pb, nbits, mant);
436 /* output EOB only if not already 64 values */
437 if (last_index < 63 || run != 0)
438 put_bits(&s->pb, huff_size_ac[0], huff_code_ac[0]);
441 void ff_mjpeg_encode_mb(MpegEncContext *s, DCTELEM block[6][64])
445 encode_block(s, block[i], i);
447 if (s->chroma_format == CHROMA_420) {
448 encode_block(s, block[5], 5);
450 encode_block(s, block[6], 6);
451 encode_block(s, block[5], 5);
452 encode_block(s, block[7], 7);
455 s->i_tex_bits += get_bits_diff(s);
458 // maximum over s->mjpeg_vsample[i]
460 static int amv_encode_picture(AVCodecContext *avctx, AVPacket *pkt,
461 const AVFrame *pic_arg, int *got_packet)
464 MpegEncContext *s = avctx->priv_data;
465 AVFrame pic = *pic_arg;
468 //CODEC_FLAG_EMU_EDGE have to be cleared
469 if(s->avctx->flags & CODEC_FLAG_EMU_EDGE)
472 //picture should be flipped upside-down
473 for(i=0; i < 3; i++) {
474 pic.data[i] += (pic.linesize[i] * (s->mjpeg_vsample[i] * (8 * s->mb_height -((s->height/V_MAX)&7)) - 1 ));
475 pic.linesize[i] *= -1;
477 return ff_MPV_encode_picture(avctx, pkt, &pic, got_packet);
480 AVCodec ff_mjpeg_encoder = {
482 .type = AVMEDIA_TYPE_VIDEO,
483 .id = CODEC_ID_MJPEG,
484 .priv_data_size = sizeof(MpegEncContext),
485 .init = ff_MPV_encode_init,
486 .encode2 = ff_MPV_encode_picture,
487 .close = ff_MPV_encode_end,
488 .pix_fmts = (const enum PixelFormat[]){
489 PIX_FMT_YUVJ420P, PIX_FMT_YUVJ422P, PIX_FMT_NONE
491 .long_name = NULL_IF_CONFIG_SMALL("MJPEG (Motion JPEG)"),
494 AVCodec ff_amv_encoder = {
496 .type = AVMEDIA_TYPE_VIDEO,
498 .priv_data_size = sizeof(MpegEncContext),
499 .init = ff_MPV_encode_init,
500 .encode2 = amv_encode_picture,
501 .close = ff_MPV_encode_end,
502 .pix_fmts= (const enum PixelFormat[]){PIX_FMT_YUVJ420P, PIX_FMT_YUVJ422P, PIX_FMT_NONE},
503 .long_name = NULL_IF_CONFIG_SMALL("AMV Video"),