2 * The simplest mpeg encoder (well, it was the simplest!)
3 * Copyright (c) 2000,2001 Gerard Lantau.
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
25 #include "mpegvideo.h"
28 #include "fastmemcpy.h"
31 static void encode_picture(MpegEncContext *s, int picture_number);
32 static void rate_control_init(MpegEncContext *s);
33 static int rate_estimate_qscale(MpegEncContext *s);
34 static void dct_unquantize_mpeg1_c(MpegEncContext *s,
35 DCTELEM *block, int n, int qscale);
36 static void dct_unquantize_h263_c(MpegEncContext *s,
37 DCTELEM *block, int n, int qscale);
38 static int dct_quantize(MpegEncContext *s, DCTELEM *block, int n, int qscale);
39 static int dct_quantize_mmx(MpegEncContext *s,
40 DCTELEM *block, int n,
44 /* enable all paranoid tests for rounding, overflows, etc... */
49 /* for jpeg fast DCT */
52 static const unsigned short aanscales[64] = {
53 /* precomputed values scaled up by 14 bits */
54 16384, 22725, 21407, 19266, 16384, 12873, 8867, 4520,
55 22725, 31521, 29692, 26722, 22725, 17855, 12299, 6270,
56 21407, 29692, 27969, 25172, 21407, 16819, 11585, 5906,
57 19266, 26722, 25172, 22654, 19266, 15137, 10426, 5315,
58 16384, 22725, 21407, 19266, 16384, 12873, 8867, 4520,
59 12873, 17855, 16819, 15137, 12873, 10114, 6967, 3552,
60 8867, 12299, 11585, 10426, 8867, 6967, 4799, 2446,
61 4520, 6270, 5906, 5315, 4520, 3552, 2446, 1247
64 static UINT8 h263_chroma_roundtab[16] = {
65 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2,
68 /* default motion estimation */
69 int motion_estimation_method = ME_LOG;
71 /* XXX: should use variable shift ? */
72 #define QMAT_SHIFT_MMX 19
75 static void convert_matrix(int *qmat, const UINT16 *quant_matrix, int qscale)
79 if (av_fdct == jpeg_fdct_ifast) {
81 /* 16 <= qscale * quant_matrix[i] <= 7905 */
82 /* 19952 <= aanscales[i] * qscale * quant_matrix[i] <= 249205026 */
84 qmat[i] = (int)((UINT64_C(1) << (QMAT_SHIFT + 11)) /
85 (aanscales[i] * qscale * quant_matrix[i]));
89 /* We can safely suppose that 16 <= quant_matrix[i] <= 255
90 So 16 <= qscale * quant_matrix[i] <= 7905
91 so (1 << QMAT_SHIFT) / 16 >= qmat[i] >= (1 << QMAT_SHIFT) / 7905
93 qmat[i] = (1 << QMAT_SHIFT_MMX) / (qscale * quant_matrix[i]);
98 /* init common structure for both encoder and decoder */
99 int MPV_common_init(MpegEncContext *s)
104 if (s->out_format == FMT_H263)
105 s->dct_unquantize = dct_unquantize_h263_c;
107 s->dct_unquantize = dct_unquantize_mpeg1_c;
110 MPV_common_init_mmx(s);
112 s->mb_width = (s->width + 15) / 16;
113 s->mb_height = (s->height + 15) / 16;
114 s->linesize = s->mb_width * 16 + 2 * EDGE_WIDTH;
117 int w, h, shift, pict_start;
120 h = s->mb_height * 16 + 2 * EDGE_WIDTH;
121 shift = (i == 0) ? 0 : 1;
122 c_size = (w >> shift) * (h >> shift);
123 pict_start = (w >> shift) * (EDGE_WIDTH >> shift) + (EDGE_WIDTH >> shift);
125 pict = av_mallocz(c_size);
128 s->last_picture_base[i] = pict;
129 s->last_picture[i] = pict + pict_start;
131 pict = av_mallocz(c_size);
134 s->next_picture_base[i] = pict;
135 s->next_picture[i] = pict + pict_start;
137 if (s->has_b_frames) {
138 pict = av_mallocz(c_size);
141 s->aux_picture_base[i] = pict;
142 s->aux_picture[i] = pict + pict_start;
146 if (s->out_format == FMT_H263) {
149 size = (2 * s->mb_width + 2) * (2 * s->mb_height + 2);
150 s->motion_val = malloc(size * 2 * sizeof(INT16));
151 if (s->motion_val == NULL)
153 memset(s->motion_val, 0, size * 2 * sizeof(INT16));
157 int y_size, c_size, i, size;
161 y_size = (2 * s->mb_width + 2) * (2 * s->mb_height + 2);
162 c_size = (s->mb_width + 2) * (s->mb_height + 2);
163 size = y_size + 2 * c_size;
164 s->dc_val[0] = malloc(size * sizeof(INT16));
165 if (s->dc_val[0] == NULL)
167 s->dc_val[1] = s->dc_val[0] + y_size;
168 s->dc_val[2] = s->dc_val[1] + c_size;
170 s->dc_val[0][i] = 1024;
173 s->ac_val[0] = av_mallocz(size * sizeof(INT16) * 16);
174 if (s->ac_val[0] == NULL)
176 s->ac_val[1] = s->ac_val[0] + y_size;
177 s->ac_val[2] = s->ac_val[1] + c_size;
180 s->coded_block = av_mallocz(y_size);
185 /* which mb is a intra block */
186 s->mbintra_table = av_mallocz(s->mb_width * s->mb_height);
187 if (!s->mbintra_table)
189 memset(s->mbintra_table, 1, s->mb_width * s->mb_height);
190 /* default structure is frame */
191 s->picture_structure = PICT_FRAME;
193 /* init macroblock skip table */
195 s->mbskip_table = av_mallocz(s->mb_width * s->mb_height);
196 if (!s->mbskip_table)
200 s->context_initialized = 1;
210 free(s->coded_block);
211 if (s->mbintra_table)
212 { free(s->mbintra_table);s->mbintra_table=NULL; }
214 free(s->mbskip_table);
216 if (s->last_picture_base[i])
217 free(s->last_picture_base[i]);
218 if (s->next_picture_base[i])
219 free(s->next_picture_base[i]);
220 if (s->aux_picture_base[i])
221 free(s->aux_picture_base[i]);
226 /* init common structure for both encoder and decoder */
227 void MPV_common_end(MpegEncContext *s)
236 free(s->coded_block);
237 { free(s->mbintra_table);s->mbintra_table=NULL; }
240 free(s->mbskip_table);
242 free(s->last_picture_base[i]);
243 free(s->next_picture_base[i]);
245 free(s->aux_picture_base[i]);
247 s->context_initialized = 0;
250 /* init video encoder */
251 int MPV_encode_init(AVCodecContext *avctx)
253 MpegEncContext *s = avctx->priv_data;
256 s->bit_rate = avctx->bit_rate;
257 s->frame_rate = avctx->frame_rate;
258 s->width = avctx->width;
259 s->height = avctx->height;
260 s->gop_size = avctx->gop_size;
261 s->rtp_mode = avctx->rtp_mode;
262 s->rtp_payload_size = avctx->rtp_payload_size;
265 if (s->gop_size <= 1) {
271 s->full_search = motion_estimation_method;
273 s->fixed_qscale = (avctx->flags & CODEC_FLAG_QSCALE);
275 switch(avctx->codec->id) {
276 case CODEC_ID_MPEG1VIDEO:
277 s->out_format = FMT_MPEG1;
280 s->out_format = FMT_MJPEG;
281 s->intra_only = 1; /* force intra only for jpeg */
282 if (mjpeg_init(s) < 0)
286 if (h263_get_picture_format(s->width, s->height) == 7){
287 printf("Input picture size isn't suitable for h263 codec! try h263+\n");
290 s->out_format = FMT_H263;
293 s->out_format = FMT_H263;
295 s->rtp_payload_size = 1200;
297 s->unrestricted_mv = 1;
299 /* These are just to be sure */
304 s->out_format = FMT_H263;
308 s->out_format = FMT_H263;
310 s->unrestricted_mv = 1;
312 case CODEC_ID_MSMPEG4:
313 s->out_format = FMT_H263;
316 s->unrestricted_mv = 1;
322 if (s->out_format == FMT_H263)
323 h263_encode_init_vlc(s);
328 if (MPV_common_init(s) < 0)
331 /* init default q matrix */
333 s->intra_matrix[i] = default_intra_matrix[i];
334 s->non_intra_matrix[i] = default_non_intra_matrix[i];
337 /* rate control init */
338 rate_control_init(s);
340 s->picture_number = 0;
341 s->fake_picture_number = 0;
342 /* motion detector init */
348 int MPV_encode_end(AVCodecContext *avctx)
350 MpegEncContext *s = avctx->priv_data;
356 if (s->out_format == FMT_MJPEG)
361 /* draw the edges of width 'w' of an image of size width, height */
362 static void draw_edges(UINT8 *buf, int wrap, int width, int height, int w)
364 UINT8 *ptr, *last_line;
367 last_line = buf + (height - 1) * wrap;
370 memcpy(buf - (i + 1) * wrap, buf, width);
371 memcpy(last_line + (i + 1) * wrap, last_line, width);
375 for(i=0;i<height;i++) {
376 memset(ptr - w, ptr[0], w);
377 memset(ptr + width, ptr[width-1], w);
382 memset(buf - (i + 1) * wrap - w, buf[0], w); /* top left */
383 memset(buf - (i + 1) * wrap + width, buf[width-1], w); /* top right */
384 memset(last_line + (i + 1) * wrap - w, last_line[0], w); /* top left */
385 memset(last_line + (i + 1) * wrap + width, last_line[width-1], w); /* top right */
389 /* generic function for encode/decode called before a frame is coded/decoded */
390 void MPV_frame_start(MpegEncContext *s)
396 if (s->pict_type == B_TYPE) {
398 s->current_picture[i] = s->aux_picture[i];
402 /* swap next and last */
403 tmp = s->last_picture[i];
404 s->last_picture[i] = s->next_picture[i];
405 s->next_picture[i] = tmp;
406 s->current_picture[i] = tmp;
411 /* generic function for encode/decode called after a frame has been coded/decoded */
412 void MPV_frame_end(MpegEncContext *s)
414 /* draw edge for correct motion prediction if outside */
415 if (s->pict_type != B_TYPE) {
416 if(s->avctx->codec->id!=CODEC_ID_MPEG4){
417 draw_edges(s->current_picture[0], s->linesize, s->mb_width*16, s->mb_height*16, EDGE_WIDTH);
418 draw_edges(s->current_picture[1], s->linesize/2, s->mb_width*8, s->mb_height*8, EDGE_WIDTH/2);
419 draw_edges(s->current_picture[2], s->linesize/2, s->mb_width*8, s->mb_height*8, EDGE_WIDTH/2);
421 /* OpenDivx, but i dunno how to distinguish it from mpeg4 */
422 draw_edges(s->current_picture[0], s->linesize, s->width, s->height, EDGE_WIDTH);
423 draw_edges(s->current_picture[1], s->linesize/2, s->width/2, s->height/2, EDGE_WIDTH/2);
424 draw_edges(s->current_picture[2], s->linesize/2, s->width/2, s->height/2, EDGE_WIDTH/2);
429 int MPV_encode_picture(AVCodecContext *avctx,
430 unsigned char *buf, int buf_size, void *data)
432 MpegEncContext *s = avctx->priv_data;
433 AVPicture *pict = data;
437 s->qscale = avctx->quality;
439 init_put_bits(&s->pb, buf, buf_size, NULL, NULL);
441 if (!s->intra_only) {
442 /* first picture of GOP is intra */
443 if ((s->picture_number % s->gop_size) == 0)
444 s->pict_type = I_TYPE;
446 s->pict_type = P_TYPE;
448 s->pict_type = I_TYPE;
450 avctx->key_frame = (s->pict_type == I_TYPE);
455 UINT8 *src = pict->data[i];
456 UINT8 *dest = s->current_picture[i];
457 int src_wrap = pict->linesize[i];
458 int dest_wrap = s->linesize;
469 memcpy(dest, src, w);
473 s->new_picture[i] = s->current_picture[i];
476 encode_picture(s, s->picture_number);
481 if (s->out_format == FMT_MJPEG)
482 mjpeg_picture_trailer(s);
484 flush_put_bits(&s->pb);
485 s->total_bits += (s->pb.buf_ptr - s->pb.buf) * 8;
486 avctx->quality = s->qscale;
487 return s->pb.buf_ptr - s->pb.buf;
490 static inline int clip(int a, int amin, int amax)
500 /* apply one mpeg motion vector to the three components */
501 static inline void mpeg_motion(MpegEncContext *s,
502 UINT8 *dest_y, UINT8 *dest_cb, UINT8 *dest_cr,
504 UINT8 **ref_picture, int src_offset,
505 int field_based, op_pixels_func *pix_op,
506 int motion_x, int motion_y, int h)
509 int dxy, offset, mx, my, src_x, src_y, height, linesize;
511 dxy = ((motion_y & 1) << 1) | (motion_x & 1);
512 src_x = s->mb_x * 16 + (motion_x >> 1);
513 src_y = s->mb_y * (16 >> field_based) + (motion_y >> 1);
515 /* WARNING: do no forget half pels */
516 height = s->height >> field_based;
517 src_x = clip(src_x, -16, s->width);
518 if (src_x == s->width)
520 src_y = clip(src_y, -16, height);
523 linesize = s->linesize << field_based;
524 ptr = ref_picture[0] + (src_y * linesize) + (src_x) + src_offset;
525 dest_y += dest_offset;
526 pix_op[dxy](dest_y, ptr, linesize, h);
527 pix_op[dxy](dest_y + 8, ptr + 8, linesize, h);
529 if (s->out_format == FMT_H263) {
531 if ((motion_x & 3) != 0)
533 if ((motion_y & 3) != 0)
540 dxy = ((my & 1) << 1) | (mx & 1);
545 src_x = s->mb_x * 8 + mx;
546 src_y = s->mb_y * (8 >> field_based) + my;
547 src_x = clip(src_x, -8, s->width >> 1);
548 if (src_x == (s->width >> 1))
550 src_y = clip(src_y, -8, height >> 1);
551 if (src_y == (height >> 1))
554 offset = (src_y * (linesize >> 1)) + src_x + (src_offset >> 1);
555 ptr = ref_picture[1] + offset;
556 pix_op[dxy](dest_cb + (dest_offset >> 1), ptr, linesize >> 1, h >> 1);
557 ptr = ref_picture[2] + offset;
558 pix_op[dxy](dest_cr + (dest_offset >> 1), ptr, linesize >> 1, h >> 1);
561 static inline void MPV_motion(MpegEncContext *s,
562 UINT8 *dest_y, UINT8 *dest_cb, UINT8 *dest_cr,
563 int dir, UINT8 **ref_picture,
564 op_pixels_func *pix_op)
566 int dxy, offset, mx, my, src_x, src_y, motion_x, motion_y;
575 mpeg_motion(s, dest_y, dest_cb, dest_cr, 0,
578 s->mv[dir][0][0], s->mv[dir][0][1], 16);
582 motion_x = s->mv[dir][i][0];
583 motion_y = s->mv[dir][i][1];
585 dxy = ((motion_y & 1) << 1) | (motion_x & 1);
586 src_x = mb_x * 16 + (motion_x >> 1) + (i & 1) * 8;
587 src_y = mb_y * 16 + (motion_y >> 1) + ((i >> 1) & 1) * 8;
589 /* WARNING: do no forget half pels */
590 src_x = clip(src_x, -16, s->width);
591 if (src_x == s->width)
593 src_y = clip(src_y, -16, s->height);
594 if (src_y == s->height)
597 ptr = ref_picture[0] + (src_y * s->linesize) + (src_x);
598 dest = dest_y + ((i & 1) * 8) + (i >> 1) * 8 * s->linesize;
599 pix_op[dxy](dest, ptr, s->linesize, 8);
601 /* In case of 8X8, we construct a single chroma motion vector
602 with a special rounding */
606 mx += s->mv[dir][i][0];
607 my += s->mv[dir][i][1];
610 mx = (h263_chroma_roundtab[mx & 0xf] + ((mx >> 3) & ~1));
613 mx = -(h263_chroma_roundtab[mx & 0xf] + ((mx >> 3) & ~1));
616 my = (h263_chroma_roundtab[my & 0xf] + ((my >> 3) & ~1));
619 my = -(h263_chroma_roundtab[my & 0xf] + ((my >> 3) & ~1));
621 dxy = ((my & 1) << 1) | (mx & 1);
625 src_x = mb_x * 8 + mx;
626 src_y = mb_y * 8 + my;
627 src_x = clip(src_x, -8, s->width/2);
628 if (src_x == s->width/2)
630 src_y = clip(src_y, -8, s->height/2);
631 if (src_y == s->height/2)
634 offset = (src_y * (s->linesize >> 1)) + src_x;
635 ptr = ref_picture[1] + offset;
636 pix_op[dxy](dest_cb, ptr, s->linesize >> 1, 8);
637 ptr = ref_picture[2] + offset;
638 pix_op[dxy](dest_cr, ptr, s->linesize >> 1, 8);
641 if (s->picture_structure == PICT_FRAME) {
643 mpeg_motion(s, dest_y, dest_cb, dest_cr, 0,
644 ref_picture, s->field_select[dir][0] ? s->linesize : 0,
646 s->mv[dir][0][0], s->mv[dir][0][1], 8);
648 mpeg_motion(s, dest_y, dest_cb, dest_cr, s->linesize,
649 ref_picture, s->field_select[dir][1] ? s->linesize : 0,
651 s->mv[dir][1][0], s->mv[dir][1][1], 8);
661 /* put block[] to dest[] */
662 static inline void put_dct(MpegEncContext *s,
663 DCTELEM *block, int i, UINT8 *dest, int line_size)
666 s->dct_unquantize(s, block, i, s->qscale);
668 put_pixels_clamped(block, dest, line_size);
671 /* add block[] to dest[] */
672 static inline void add_dct(MpegEncContext *s,
673 DCTELEM *block, int i, UINT8 *dest, int line_size)
675 if (s->block_last_index[i] >= 0) {
677 s->dct_unquantize(s, block, i, s->qscale);
679 add_pixels_clamped(block, dest, line_size);
683 /* generic function called after a macroblock has been parsed by the
684 decoder or after it has been encoded by the encoder.
686 Important variables used:
687 s->mb_intra : true if intra macroblock
688 s->mv_dir : motion vector direction
689 s->mv_type : motion vector type
690 s->mv : motion vector
691 s->interlaced_dct : true if interlaced dct used (mpeg2)
693 void MPV_decode_mb(MpegEncContext *s, DCTELEM block[6][64])
695 int mb_x, mb_y, motion_x, motion_y;
696 int dct_linesize, dct_offset;
697 op_pixels_func *op_pix;
702 #ifdef FF_POSTPROCESS
703 quant_store[mb_y][mb_x]=s->qscale;
704 //printf("[%02d][%02d] %d\n",mb_x,mb_y,s->qscale);
707 /* update DC predictors for P macroblocks */
710 if(s->mbintra_table[mb_x + mb_y*s->mb_width])
713 s->mbintra_table[mb_x + mb_y*s->mb_width]=0;
715 wrap = 2 * s->mb_width + 2;
720 s->dc_val[0][(x) + (y) * wrap] = v;
721 s->dc_val[0][(x + 1) + (y) * wrap] = v;
722 s->dc_val[0][(x) + (y + 1) * wrap] = v;
723 s->dc_val[0][(x + 1) + (y + 1) * wrap] = v;
725 memset(s->ac_val[0][(x) + (y) * wrap], 0, 16 * sizeof(INT16));
726 memset(s->ac_val[0][(x + 1) + (y) * wrap], 0, 16 * sizeof(INT16));
727 memset(s->ac_val[0][(x) + (y + 1) * wrap], 0, 16 * sizeof(INT16));
728 memset(s->ac_val[0][(x + 1) + (y + 1) * wrap], 0, 16 * sizeof(INT16));
729 if (s->h263_msmpeg4) {
730 s->coded_block[(x) + (y) * wrap] = 0;
731 s->coded_block[(x + 1) + (y) * wrap] = 0;
732 s->coded_block[(x) + (y + 1) * wrap] = 0;
733 s->coded_block[(x + 1) + (y + 1) * wrap] = 0;
736 wrap = s->mb_width + 2;
739 s->dc_val[1][(x) + (y) * wrap] = v;
740 s->dc_val[2][(x) + (y) * wrap] = v;
742 memset(s->ac_val[1][(x) + (y) * wrap], 0, 16 * sizeof(INT16));
743 memset(s->ac_val[2][(x) + (y) * wrap], 0, 16 * sizeof(INT16));
746 s->last_dc[0] = 128 << s->intra_dc_precision;
747 s->last_dc[1] = 128 << s->intra_dc_precision;
748 s->last_dc[2] = 128 << s->intra_dc_precision;
752 s->mbintra_table[mb_x + mb_y*s->mb_width]=1;
754 /* update motion predictor */
755 if (s->out_format == FMT_H263) {
760 wrap = 2 * s->mb_width + 2;
765 } else if (s->mv_type == MV_TYPE_16X16) {
766 motion_x = s->mv[0][0][0];
767 motion_y = s->mv[0][0][1];
769 /* no update if 8X8 because it has been done during parsing */
770 s->motion_val[(x) + (y) * wrap][0] = motion_x;
771 s->motion_val[(x) + (y) * wrap][1] = motion_y;
772 s->motion_val[(x + 1) + (y) * wrap][0] = motion_x;
773 s->motion_val[(x + 1) + (y) * wrap][1] = motion_y;
774 s->motion_val[(x) + (y + 1) * wrap][0] = motion_x;
775 s->motion_val[(x) + (y + 1) * wrap][1] = motion_y;
776 s->motion_val[(x + 1) + (y + 1) * wrap][0] = motion_x;
777 s->motion_val[(x + 1) + (y + 1) * wrap][1] = motion_y;
781 if (!s->intra_only) {
782 UINT8 *dest_y, *dest_cb, *dest_cr;
785 /* avoid copy if macroblock skipped in last frame too */
786 if (!s->encoding && s->pict_type != B_TYPE) {
787 mbskip_ptr = &s->mbskip_table[s->mb_y * s->mb_width + s->mb_x];
790 /* if previous was skipped too, then nothing to do ! */
791 if (*mbskip_ptr != 0)
793 *mbskip_ptr = 1; /* indicate that this time we skiped it */
795 *mbskip_ptr = 0; /* not skipped */
799 dest_y = s->current_picture[0] + (mb_y * 16 * s->linesize) + mb_x * 16;
800 dest_cb = s->current_picture[1] + (mb_y * 8 * (s->linesize >> 1)) + mb_x * 8;
801 dest_cr = s->current_picture[2] + (mb_y * 8 * (s->linesize >> 1)) + mb_x * 8;
803 if (s->interlaced_dct) {
804 dct_linesize = s->linesize * 2;
805 dct_offset = s->linesize;
807 dct_linesize = s->linesize;
808 dct_offset = s->linesize * 8;
812 /* motion handling */
814 op_pix = put_pixels_tab;
816 op_pix = put_no_rnd_pixels_tab;
818 if (s->mv_dir & MV_DIR_FORWARD) {
819 MPV_motion(s, dest_y, dest_cb, dest_cr, 0, s->last_picture, op_pix);
821 op_pix = avg_pixels_tab;
823 op_pix = avg_no_rnd_pixels_tab;
825 if (s->mv_dir & MV_DIR_BACKWARD) {
826 MPV_motion(s, dest_y, dest_cb, dest_cr, 1, s->next_picture, op_pix);
829 /* add dct residue */
830 add_dct(s, block[0], 0, dest_y, dct_linesize);
831 add_dct(s, block[1], 1, dest_y + 8, dct_linesize);
832 add_dct(s, block[2], 2, dest_y + dct_offset, dct_linesize);
833 add_dct(s, block[3], 3, dest_y + dct_offset + 8, dct_linesize);
835 add_dct(s, block[4], 4, dest_cb, s->linesize >> 1);
836 add_dct(s, block[5], 5, dest_cr, s->linesize >> 1);
838 /* dct only in intra block */
839 put_dct(s, block[0], 0, dest_y, dct_linesize);
840 put_dct(s, block[1], 1, dest_y + 8, dct_linesize);
841 put_dct(s, block[2], 2, dest_y + dct_offset, dct_linesize);
842 put_dct(s, block[3], 3, dest_y + dct_offset + 8, dct_linesize);
844 put_dct(s, block[4], 4, dest_cb, s->linesize >> 1);
845 put_dct(s, block[5], 5, dest_cr, s->linesize >> 1);
852 static void encode_picture(MpegEncContext *s, int picture_number)
854 int mb_x, mb_y, wrap, last_gob;
856 int i, motion_x, motion_y;
858 s->picture_number = picture_number;
859 if (!s->fixed_qscale)
860 s->qscale = rate_estimate_qscale(s);
862 /* precompute matrix */
863 if (s->out_format == FMT_MJPEG) {
864 /* for mjpeg, we do include qscale in the matrix */
865 s->intra_matrix[0] = default_intra_matrix[0];
867 s->intra_matrix[i] = (default_intra_matrix[i] * s->qscale) >> 3;
868 convert_matrix(s->q_intra_matrix, s->intra_matrix, 8);
870 convert_matrix(s->q_intra_matrix, s->intra_matrix, s->qscale);
871 convert_matrix(s->q_non_intra_matrix, s->non_intra_matrix, s->qscale);
874 switch(s->out_format) {
876 mjpeg_picture_header(s);
880 msmpeg4_encode_picture_header(s, picture_number);
881 else if (s->h263_pred)
882 mpeg4_encode_picture_header(s, picture_number);
883 else if (s->h263_rv10)
884 rv10_encode_picture_header(s, picture_number);
886 h263_encode_picture_header(s, picture_number);
889 mpeg1_encode_picture_header(s, picture_number);
893 /* init last dc values */
894 /* note: quant matrix value (8) is implied here */
899 s->last_mv[0][0][0] = 0;
900 s->last_mv[0][0][1] = 0;
901 s->mv_type = MV_TYPE_16X16;
902 s->mv_dir = MV_DIR_FORWARD;
904 /* Get the GOB height based on picture height */
905 if (s->out_format == FMT_H263 && s->h263_plus) {
906 if (s->height <= 400)
908 else if (s->height <= 800)
914 for(mb_y=0; mb_y < s->mb_height; mb_y++) {
915 /* Put GOB header based on RTP MTU */
917 s->ptr_lastgob = s->pb.buf_ptr;
918 s->ptr_last_mb_line = s->pb.buf_ptr;
919 } else if (s->out_format == FMT_H263 && s->h263_plus) {
920 last_gob = h263_encode_gob_header(s, mb_y);
922 //fprintf(stderr,"\nLast GOB size: %d", last_gob);
923 s->first_gob_line = 1;
925 s->first_gob_line = 0;
927 for(mb_x=0; mb_x < s->mb_width; mb_x++) {
932 /* compute motion vector and macro block type (intra or non intra) */
935 if (s->pict_type == P_TYPE) {
936 s->mb_intra = estimate_motion(s, mb_x, mb_y,
945 ptr = s->new_picture[0] + (mb_y * 16 * wrap) + mb_x * 16;
946 get_pixels(s->block[0], ptr, wrap);
947 get_pixels(s->block[1], ptr + 8, wrap);
948 get_pixels(s->block[2], ptr + 8 * wrap, wrap);
949 get_pixels(s->block[3], ptr + 8 * wrap + 8, wrap);
950 wrap = s->linesize >> 1;
951 ptr = s->new_picture[1] + (mb_y * 8 * wrap) + mb_x * 8;
952 get_pixels(s->block[4], ptr, wrap);
954 wrap = s->linesize >> 1;
955 ptr = s->new_picture[2] + (mb_y * 8 * wrap) + mb_x * 8;
956 get_pixels(s->block[5], ptr, wrap);
958 /* subtract previous frame if non intra */
960 int dxy, offset, mx, my;
962 dxy = ((motion_y & 1) << 1) | (motion_x & 1);
963 ptr = s->last_picture[0] +
964 ((mb_y * 16 + (motion_y >> 1)) * s->linesize) +
965 (mb_x * 16 + (motion_x >> 1));
967 sub_pixels_2(s->block[0], ptr, s->linesize, dxy);
968 sub_pixels_2(s->block[1], ptr + 8, s->linesize, dxy);
969 sub_pixels_2(s->block[2], ptr + s->linesize * 8, s->linesize, dxy);
970 sub_pixels_2(s->block[3], ptr + 8 + s->linesize * 8, s->linesize ,dxy);
972 if (s->out_format == FMT_H263) {
973 /* special rounding for h263 */
975 if ((motion_x & 3) != 0)
977 if ((motion_y & 3) != 0)
984 dxy = ((my & 1) << 1) | (mx & 1);
988 offset = ((mb_y * 8 + my) * (s->linesize >> 1)) + (mb_x * 8 + mx);
989 ptr = s->last_picture[1] + offset;
990 sub_pixels_2(s->block[4], ptr, s->linesize >> 1, dxy);
991 ptr = s->last_picture[2] + offset;
992 sub_pixels_2(s->block[5], ptr, s->linesize >> 1, dxy);
997 if (s->h263_msmpeg4) {
999 } else if (s->h263_pred) {
1002 /* default quantization values */
1009 if (av_fdct == jpeg_fdct_ifast)
1010 last_index = dct_quantize(s, s->block[i], i, s->qscale);
1012 last_index = dct_quantize_mmx(s, s->block[i], i, s->qscale);
1013 s->block_last_index[i] = last_index;
1016 /* huffman encode */
1017 switch(s->out_format) {
1019 mpeg1_encode_mb(s, s->block, motion_x, motion_y);
1022 if (s->h263_msmpeg4)
1023 msmpeg4_encode_mb(s, s->block, motion_x, motion_y);
1025 h263_encode_mb(s, s->block, motion_x, motion_y);
1028 mjpeg_encode_mb(s, s->block);
1032 /* decompress blocks so that we keep the state of the decoder */
1033 s->mv[0][0][0] = motion_x;
1034 s->mv[0][0][1] = motion_y;
1036 MPV_decode_mb(s, s->block);
1038 /* Obtain average MB line size for RTP */
1040 s->mb_line_avgsize = s->pb.buf_ptr - s->ptr_last_mb_line;
1042 s->mb_line_avgsize = (s->mb_line_avgsize + s->pb.buf_ptr - s->ptr_last_mb_line) >> 1;
1043 //fprintf(stderr, "\nMB line: %d\tSize: %u\tAvg. Size: %u", s->mb_y,
1044 // (s->pb.buf_ptr - s->ptr_last_mb_line), s->mb_line_avgsize);
1045 s->ptr_last_mb_line = s->pb.buf_ptr;
1047 //if (s->gob_number)
1048 // fprintf(stderr,"\nNumber of GOB: %d", s->gob_number);
1051 static int dct_quantize(MpegEncContext *s,
1052 DCTELEM *block, int n,
1055 int i, j, level, last_non_zero, q;
1060 /* we need this permutation so that we correct the IDCT
1061 permutation. will be moved into DCT code */
1062 block_permute(block);
1071 /* note: block[0] is assumed to be positive */
1072 block[0] = (block[0] + (q >> 1)) / q;
1075 if (s->out_format == FMT_H263) {
1076 qmat = s->q_non_intra_matrix;
1078 qmat = s->q_intra_matrix;
1083 qmat = s->q_non_intra_matrix;
1087 j = zigzag_direct[i];
1089 level = level * qmat[j];
1092 static int count = 0;
1093 int level1, level2, qmat1;
1095 if (qmat == s->q_non_intra_matrix) {
1096 qmat1 = default_non_intra_matrix[j] * s->qscale;
1098 qmat1 = default_intra_matrix[j] * s->qscale;
1100 if (av_fdct != jpeg_fdct_ifast)
1101 val = ((double)block[j] * 8.0) / (double)qmat1;
1103 val = ((double)block[j] * 8.0 * 2048.0) /
1104 ((double)qmat1 * aanscales[j]);
1106 level2 = level / (1 << (QMAT_SHIFT - 3));
1107 if (level1 != level2) {
1108 fprintf(stderr, "%d: quant error qlevel=%d wanted=%d level=%d qmat1=%d qmat=%d wantedf=%0.6f\n",
1109 count, level2, level1, block[j], qmat1, qmat[j],
1116 /* XXX: slight error for the low range. Test should be equivalent to
1117 (level <= -(1 << (QMAT_SHIFT - 3)) || level >= (1 <<
1120 if (((level << (31 - (QMAT_SHIFT - 3))) >> (31 - (QMAT_SHIFT - 3))) !=
1122 level = level / (1 << (QMAT_SHIFT - 3));
1123 /* XXX: currently, this code is not optimal. the range should be:
1131 else if (level < -128)
1139 return last_non_zero;
1142 static int dct_quantize_mmx(MpegEncContext *s,
1143 DCTELEM *block, int n,
1146 int i, j, level, last_non_zero, q;
1151 /* we need this permutation so that we correct the IDCT
1152 permutation. will be moved into DCT code */
1153 block_permute(block);
1161 /* note: block[0] is assumed to be positive */
1162 block[0] = (block[0] + (q >> 1)) / q;
1165 if (s->out_format == FMT_H263) {
1166 qmat = s->q_non_intra_matrix;
1168 qmat = s->q_intra_matrix;
1173 qmat = s->q_non_intra_matrix;
1177 j = zigzag_direct[i];
1179 level = level * qmat[j];
1180 /* XXX: slight error for the low range. Test should be equivalent to
1181 (level <= -(1 << (QMAT_SHIFT_MMX - 3)) || level >= (1 <<
1182 (QMAT_SHIFT_MMX - 3)))
1184 if (((level << (31 - (QMAT_SHIFT_MMX - 3))) >> (31 - (QMAT_SHIFT_MMX - 3))) !=
1186 level = level / (1 << (QMAT_SHIFT_MMX - 3));
1187 /* XXX: currently, this code is not optimal. the range should be:
1195 else if (level < -128)
1203 return last_non_zero;
1206 static void dct_unquantize_mpeg1_c(MpegEncContext *s,
1207 DCTELEM *block, int n, int qscale)
1210 const UINT16 *quant_matrix;
1214 block[0] = block[0] * s->y_dc_scale;
1216 block[0] = block[0] * s->c_dc_scale;
1217 /* XXX: only mpeg1 */
1218 quant_matrix = s->intra_matrix;
1224 level = (int)(level * qscale * quant_matrix[i]) >> 3;
1225 level = (level - 1) | 1;
1228 level = (int)(level * qscale * quant_matrix[i]) >> 3;
1229 level = (level - 1) | 1;
1232 if (level < -2048 || level > 2047)
1233 fprintf(stderr, "unquant error %d %d\n", i, level);
1240 quant_matrix = s->non_intra_matrix;
1246 level = (((level << 1) + 1) * qscale *
1247 ((int) (quant_matrix[i]))) >> 4;
1248 level = (level - 1) | 1;
1251 level = (((level << 1) + 1) * qscale *
1252 ((int) (quant_matrix[i]))) >> 4;
1253 level = (level - 1) | 1;
1256 if (level < -2048 || level > 2047)
1257 fprintf(stderr, "unquant error %d %d\n", i, level);
1265 static void dct_unquantize_h263_c(MpegEncContext *s,
1266 DCTELEM *block, int n, int qscale)
1268 int i, level, qmul, qadd;
1272 block[0] = block[0] * s->y_dc_scale;
1274 block[0] = block[0] * s->c_dc_scale;
1280 qmul = s->qscale << 1;
1281 qadd = (s->qscale - 1) | 1;
1287 level = level * qmul - qadd;
1289 level = level * qmul + qadd;
1292 if (level < -2048 || level > 2047)
1293 fprintf(stderr, "unquant error %d %d\n", i, level);
1302 /* an I frame is I_FRAME_SIZE_RATIO bigger than a P frame */
1303 #define I_FRAME_SIZE_RATIO 3.0
1306 static void rate_control_init(MpegEncContext *s)
1310 if (s->intra_only) {
1311 s->I_frame_bits = ((INT64)s->bit_rate * FRAME_RATE_BASE) / s->frame_rate;
1312 s->P_frame_bits = s->I_frame_bits;
1314 s->P_frame_bits = (int) ((float)(s->gop_size * s->bit_rate) /
1315 (float)((float)s->frame_rate / FRAME_RATE_BASE * (I_FRAME_SIZE_RATIO + s->gop_size - 1)));
1316 s->I_frame_bits = (int)(s->P_frame_bits * I_FRAME_SIZE_RATIO);
1320 printf("I_frame_size=%d P_frame_size=%d\n",
1321 s->I_frame_bits, s->P_frame_bits);
1327 * This heuristic is rather poor, but at least we do not have to
1328 * change the qscale at every macroblock.
1330 static int rate_estimate_qscale(MpegEncContext *s)
1332 INT64 diff, total_bits = s->total_bits;
1336 if (s->pict_type == I_TYPE) {
1337 s->wanted_bits += s->I_frame_bits;
1339 s->wanted_bits += s->P_frame_bits;
1341 diff = s->wanted_bits - total_bits;
1342 q = 31.0 - (float)diff / (QSCALE_K * s->mb_height * s->mb_width);
1343 /* adjust for I frame */
1344 if (s->pict_type == I_TYPE && !s->intra_only) {
1345 q /= I_FRAME_SIZE_RATIO;
1348 /* using a too small Q scale leeds to problems in mpeg1 and h263
1349 because AC coefficients are clamped to 255 or 127 */
1355 qscale = (int)(q + 0.5);
1357 printf("%d: total=%0.0f br=%0.1f diff=%d qest=%0.1f\n",
1360 (float)s->frame_rate / FRAME_RATE_BASE *
1361 total_bits / s->picture_number,
1367 AVCodec mpeg1video_encoder = {
1370 CODEC_ID_MPEG1VIDEO,
1371 sizeof(MpegEncContext),
1377 AVCodec h263_encoder = {
1381 sizeof(MpegEncContext),
1387 AVCodec h263p_encoder = {
1391 sizeof(MpegEncContext),
1397 AVCodec rv10_encoder = {
1401 sizeof(MpegEncContext),
1407 AVCodec mjpeg_encoder = {
1411 sizeof(MpegEncContext),
1417 AVCodec mpeg4_encoder = {
1421 sizeof(MpegEncContext),
1427 AVCodec msmpeg4_encoder = {
1431 sizeof(MpegEncContext),