3 * Copyright (c) 2000, 2001 Fabrice Bellard.
4 * Copyright (c) 2003 Alex Beregszaszi
5 * Copyright (c) 2003-2004 Michael Niedermayer
7 * This file is part of FFmpeg.
9 * FFmpeg is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU Lesser General Public
11 * License as published by the Free Software Foundation; either
12 * version 2.1 of the License, or (at your option) any later version.
14 * FFmpeg is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * Lesser General Public License for more details.
19 * You should have received a copy of the GNU Lesser General Public
20 * License along with FFmpeg; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
23 * Support for external huffman table, various fixes (AVID workaround),
24 * aspecting, new decode_frame mechanism and apple mjpeg-b support
38 #include "mpegvideo.h"
41 /* use two quantizer tables (one for luminance and one for chrominance) */
45 typedef struct MJpegContext {
46 uint8_t huff_size_dc_luminance[12]; //FIXME use array [3] instead of lumi / chrom, for easier addressing
47 uint16_t huff_code_dc_luminance[12];
48 uint8_t huff_size_dc_chrominance[12];
49 uint16_t huff_code_dc_chrominance[12];
51 uint8_t huff_size_ac_luminance[256];
52 uint16_t huff_code_ac_luminance[256];
53 uint8_t huff_size_ac_chrominance[256];
54 uint16_t huff_code_ac_chrominance[256];
58 int mjpeg_init(MpegEncContext *s)
62 m = av_malloc(sizeof(MJpegContext));
69 /* build all the huffman tables */
70 build_huffman_codes(m->huff_size_dc_luminance,
71 m->huff_code_dc_luminance,
74 build_huffman_codes(m->huff_size_dc_chrominance,
75 m->huff_code_dc_chrominance,
78 build_huffman_codes(m->huff_size_ac_luminance,
79 m->huff_code_ac_luminance,
82 build_huffman_codes(m->huff_size_ac_chrominance,
83 m->huff_code_ac_chrominance,
91 void mjpeg_close(MpegEncContext *s)
93 av_free(s->mjpeg_ctx);
96 /* table_class: 0 = DC coef, 1 = AC coefs */
97 static int put_huffman_table(MpegEncContext *s, int table_class, int table_id,
98 const uint8_t *bits_table, const uint8_t *value_table)
100 PutBitContext *p = &s->pb;
103 put_bits(p, 4, table_class);
104 put_bits(p, 4, table_id);
109 put_bits(p, 8, bits_table[i]);
113 put_bits(p, 8, value_table[i]);
118 static void jpeg_table_header(MpegEncContext *s)
120 PutBitContext *p = &s->pb;
127 put_bits(p, 16, 2 + 2 * (1 + 64));
129 put_bits(p, 16, 2 + 1 * (1 + 64));
131 put_bits(p, 4, 0); /* 8 bit precision */
132 put_bits(p, 4, 0); /* table 0 */
134 j = s->intra_scantable.permutated[i];
135 put_bits(p, 8, s->intra_matrix[j]);
138 put_bits(p, 4, 0); /* 8 bit precision */
139 put_bits(p, 4, 1); /* table 1 */
141 j = s->intra_scantable.permutated[i];
142 put_bits(p, 8, s->chroma_intra_matrix[j]);
150 put_bits(p, 16, 0); /* patched later */
152 size += put_huffman_table(s, 0, 0, bits_dc_luminance, val_dc_luminance);
153 size += put_huffman_table(s, 0, 1, bits_dc_chrominance, val_dc_chrominance);
155 size += put_huffman_table(s, 1, 0, bits_ac_luminance, val_ac_luminance);
156 size += put_huffman_table(s, 1, 1, bits_ac_chrominance, val_ac_chrominance);
161 static void jpeg_put_comments(MpegEncContext *s)
163 PutBitContext *p = &s->pb;
167 if (s->aspect_ratio_info /* && !lossless */)
172 ff_put_string(p, "JFIF", 1); /* this puts the trailing zero-byte too */
173 put_bits(p, 16, 0x0201); /* 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)){
186 put_bits(p, 16, 0); /* patched later */
187 ff_put_string(p, LIBAVCODEC_IDENT, 1);
188 size = strlen(LIBAVCODEC_IDENT)+3;
193 if( s->avctx->pix_fmt == PIX_FMT_YUV420P
194 ||s->avctx->pix_fmt == PIX_FMT_YUV422P
195 ||s->avctx->pix_fmt == PIX_FMT_YUV444P){
199 put_bits(p, 16, 0); /* patched later */
200 ff_put_string(p, "CS=ITU601", 1);
201 size = strlen("CS=ITU601")+3;
207 void mjpeg_picture_header(MpegEncContext *s)
209 const int lossless= s->avctx->codec_id != CODEC_ID_MJPEG;
211 put_marker(&s->pb, SOI);
213 jpeg_put_comments(s);
215 jpeg_table_header(s);
217 switch(s->avctx->codec_id){
218 case CODEC_ID_MJPEG: put_marker(&s->pb, SOF0 ); break;
219 case CODEC_ID_LJPEG: put_marker(&s->pb, SOF3 ); break;
223 put_bits(&s->pb, 16, 17);
224 if(lossless && s->avctx->pix_fmt == PIX_FMT_RGB32)
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;
327 for(i=0; i<ff_count-3; i+=4)
328 put_bits(&s->pb, 32, 0);
329 put_bits(&s->pb, (ff_count-i)*8, 0);
330 flush_put_bits(&s->pb);
332 for(i=size-1; ff_count; i--){
336 //printf("%d %d\n", i, ff_count);
345 void ff_mjpeg_stuffing(PutBitContext * pbc)
348 length= (-put_bits_count(pbc))&7;
349 if(length) put_bits(pbc, length, (1<<length)-1);
352 void mjpeg_picture_trailer(MpegEncContext *s)
354 ff_mjpeg_stuffing(&s->pb);
355 flush_put_bits(&s->pb);
357 assert((s->header_bits&7)==0);
359 escape_FF(s, s->header_bits>>3);
361 put_marker(&s->pb, EOI);
364 static inline void mjpeg_encode_dc(MpegEncContext *s, int val,
365 uint8_t *huff_size, uint16_t *huff_code)
370 put_bits(&s->pb, huff_size[0], huff_code[0]);
378 nbits= av_log2_16bit(val) + 1;
380 put_bits(&s->pb, huff_size[nbits], huff_code[nbits]);
382 put_bits(&s->pb, nbits, mant & ((1 << nbits) - 1));
386 static void encode_block(MpegEncContext *s, DCTELEM *block, int n)
388 int mant, nbits, code, i, j;
389 int component, dc, run, last_index, val;
390 MJpegContext *m = s->mjpeg_ctx;
391 uint8_t *huff_size_ac;
392 uint16_t *huff_code_ac;
395 component = (n <= 3 ? 0 : (n&1) + 1);
396 dc = block[0]; /* overflow is impossible */
397 val = dc - s->last_dc[component];
399 mjpeg_encode_dc(s, val, m->huff_size_dc_luminance, m->huff_code_dc_luminance);
400 huff_size_ac = m->huff_size_ac_luminance;
401 huff_code_ac = m->huff_code_ac_luminance;
403 mjpeg_encode_dc(s, val, m->huff_size_dc_chrominance, m->huff_code_dc_chrominance);
404 huff_size_ac = m->huff_size_ac_chrominance;
405 huff_code_ac = m->huff_code_ac_chrominance;
407 s->last_dc[component] = dc;
412 last_index = s->block_last_index[n];
413 for(i=1;i<=last_index;i++) {
414 j = s->intra_scantable.permutated[i];
420 put_bits(&s->pb, huff_size_ac[0xf0], huff_code_ac[0xf0]);
429 nbits= av_log2(val) + 1;
430 code = (run << 4) | nbits;
432 put_bits(&s->pb, huff_size_ac[code], huff_code_ac[code]);
434 put_bits(&s->pb, nbits, mant & ((1 << nbits) - 1));
439 /* output EOB only if not already 64 values */
440 if (last_index < 63 || run != 0)
441 put_bits(&s->pb, huff_size_ac[0], huff_code_ac[0]);
444 void mjpeg_encode_mb(MpegEncContext *s,
445 DCTELEM block[6][64])
449 encode_block(s, block[i], i);
451 if (s->chroma_format == CHROMA_420) {
452 encode_block(s, block[5], 5);
454 encode_block(s, block[6], 6);
455 encode_block(s, block[5], 5);
456 encode_block(s, block[7], 7);
460 static int encode_picture_lossless(AVCodecContext *avctx, unsigned char *buf, int buf_size, void *data){
461 MpegEncContext * const s = avctx->priv_data;
462 MJpegContext * const m = s->mjpeg_ctx;
463 AVFrame *pict = data;
464 const int width= s->width;
465 const int height= s->height;
466 AVFrame * const p= (AVFrame*)&s->current_picture;
467 const int predictor= avctx->prediction_method+1;
469 init_put_bits(&s->pb, buf, buf_size);
472 p->pict_type= FF_I_TYPE;
475 mjpeg_picture_header(s);
477 s->header_bits= put_bits_count(&s->pb);
479 if(avctx->pix_fmt == PIX_FMT_RGB32){
481 const int linesize= p->linesize[0];
482 uint16_t (*buffer)[4]= (void *) s->rd_scratchpad;
483 int left[3], top[3], topleft[3];
486 buffer[0][i]= 1 << (9 - 1);
489 for(y = 0; y < height; y++) {
490 const int modified_predictor= y ? predictor : 1;
491 uint8_t *ptr = p->data[0] + (linesize * y);
493 if(s->pb.buf_end - s->pb.buf - (put_bits_count(&s->pb)>>3) < width*3*4){
494 av_log(s->avctx, AV_LOG_ERROR, "encoded frame too large\n");
499 top[i]= left[i]= topleft[i]= buffer[0][i];
501 for(x = 0; x < width; x++) {
502 buffer[x][1] = ptr[4*x+0] - ptr[4*x+1] + 0x100;
503 buffer[x][2] = ptr[4*x+2] - ptr[4*x+1] + 0x100;
504 buffer[x][0] = (ptr[4*x+0] + 2*ptr[4*x+1] + ptr[4*x+2])>>2;
509 PREDICT(pred, topleft[i], top[i], left[i], modified_predictor);
512 top[i]= buffer[x+1][i];
514 left[i]= buffer[x][i];
516 diff= ((left[i] - pred + 0x100)&0x1FF) - 0x100;
519 mjpeg_encode_dc(s, diff, m->huff_size_dc_luminance, m->huff_code_dc_luminance); //FIXME ugly
521 mjpeg_encode_dc(s, diff, m->huff_size_dc_chrominance, m->huff_code_dc_chrominance);
527 const int mb_width = (width + s->mjpeg_hsample[0] - 1) / s->mjpeg_hsample[0];
528 const int mb_height = (height + s->mjpeg_vsample[0] - 1) / s->mjpeg_vsample[0];
530 for(mb_y = 0; mb_y < mb_height; mb_y++) {
531 if(s->pb.buf_end - s->pb.buf - (put_bits_count(&s->pb)>>3) < mb_width * 4 * 3 * s->mjpeg_hsample[0] * s->mjpeg_vsample[0]){
532 av_log(s->avctx, AV_LOG_ERROR, "encoded frame too large\n");
535 for(mb_x = 0; mb_x < mb_width; mb_x++) {
536 if(mb_x==0 || mb_y==0){
539 int x, y, h, v, linesize;
540 h = s->mjpeg_hsample[i];
541 v = s->mjpeg_vsample[i];
542 linesize= p->linesize[i];
548 ptr = p->data[i] + (linesize * (v * mb_y + y)) + (h * mb_x + x); //FIXME optimize this crap
557 pred= ptr[-linesize];
559 PREDICT(pred, ptr[-linesize-1], ptr[-linesize], ptr[-1], predictor);
564 mjpeg_encode_dc(s, (int8_t)(*ptr - pred), m->huff_size_dc_luminance, m->huff_code_dc_luminance); //FIXME ugly
566 mjpeg_encode_dc(s, (int8_t)(*ptr - pred), m->huff_size_dc_chrominance, m->huff_code_dc_chrominance);
573 int x, y, h, v, linesize;
574 h = s->mjpeg_hsample[i];
575 v = s->mjpeg_vsample[i];
576 linesize= p->linesize[i];
582 ptr = p->data[i] + (linesize * (v * mb_y + y)) + (h * mb_x + x); //FIXME optimize this crap
583 //printf("%d %d %d %d %8X\n", mb_x, mb_y, x, y, ptr);
584 PREDICT(pred, ptr[-linesize-1], ptr[-linesize], ptr[-1], predictor);
587 mjpeg_encode_dc(s, (int8_t)(*ptr - pred), m->huff_size_dc_luminance, m->huff_code_dc_luminance); //FIXME ugly
589 mjpeg_encode_dc(s, (int8_t)(*ptr - pred), m->huff_size_dc_chrominance, m->huff_code_dc_chrominance);
600 mjpeg_picture_trailer(s);
603 flush_put_bits(&s->pb);
604 return pbBufPtr(&s->pb) - s->pb.buf;
605 // return (put_bits_count(&f->pb)+7)/8;
609 AVCodec ljpeg_encoder = { //FIXME avoid MPV_* lossless jpeg shouldnt need them
613 sizeof(MpegEncContext),
615 encode_picture_lossless,