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"
29 /* enable all paranoid tests for rounding, overflows, etc... */
34 /* for jpeg fast DCT */
37 static const unsigned short aanscales[64] = {
38 /* precomputed values scaled up by 14 bits */
39 16384, 22725, 21407, 19266, 16384, 12873, 8867, 4520,
40 22725, 31521, 29692, 26722, 22725, 17855, 12299, 6270,
41 21407, 29692, 27969, 25172, 21407, 16819, 11585, 5906,
42 19266, 26722, 25172, 22654, 19266, 15137, 10426, 5315,
43 16384, 22725, 21407, 19266, 16384, 12873, 8867, 4520,
44 12873, 17855, 16819, 15137, 12873, 10114, 6967, 3552,
45 8867, 12299, 11585, 10426, 8867, 6967, 4799, 2446,
46 4520, 6270, 5906, 5315, 4520, 3552, 2446, 1247
49 static UINT8 h263_chroma_roundtab[16] = {
50 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2,
53 static void encode_picture(MpegEncContext *s, int picture_number);
54 static void rate_control_init(MpegEncContext *s);
55 static int rate_estimate_qscale(MpegEncContext *s);
57 /* default motion estimation */
58 int motion_estimation_method = ME_LOG;
60 /* XXX: should use variable shift ? */
61 #define QMAT_SHIFT_MMX 19
64 static void convert_matrix(int *qmat, const UINT16 *quant_matrix, int qscale)
68 if (av_fdct == jpeg_fdct_ifast) {
70 /* 16 <= qscale * quant_matrix[i] <= 7905 */
71 /* 19952 <= aanscales[i] * qscale * quant_matrix[i] <= 249205026 */
73 qmat[i] = (int)((1ULL << (QMAT_SHIFT + 11)) / (aanscales[i] * qscale * quant_matrix[i]));
77 /* We can safely suppose that 16 <= quant_matrix[i] <= 255
78 So 16 <= qscale * quant_matrix[i] <= 7905
79 so (1 << QMAT_SHIFT) / 16 >= qmat[i] >= (1 << QMAT_SHIFT) / 7905
81 qmat[i] = (1 << QMAT_SHIFT_MMX) / (qscale * quant_matrix[i]);
86 /* init common structure for both encoder and decoder */
87 int MPV_common_init(MpegEncContext *s)
92 s->mb_width = (s->width + 15) / 16;
93 s->mb_height = (s->height + 15) / 16;
94 s->linesize = s->mb_width * 16 + 2 * EDGE_WIDTH;
97 int w, h, shift, pict_start;
100 h = s->mb_height * 16 + 2 * EDGE_WIDTH;
101 shift = (i == 0) ? 0 : 1;
102 c_size = (w >> shift) * (h >> shift);
103 pict_start = (w >> shift) * (EDGE_WIDTH >> shift) + (EDGE_WIDTH >> shift);
105 pict = av_mallocz(c_size);
108 s->last_picture_base[i] = pict;
109 s->last_picture[i] = pict + pict_start;
111 pict = av_mallocz(c_size);
114 s->next_picture_base[i] = pict;
115 s->next_picture[i] = pict + pict_start;
117 if (s->has_b_frames) {
118 pict = av_mallocz(c_size);
121 s->aux_picture_base[i] = pict;
122 s->aux_picture[i] = pict + pict_start;
126 if (s->out_format == FMT_H263) {
129 size = (2 * s->mb_width + 2) * (2 * s->mb_height + 2);
130 s->motion_val = malloc(size * 2 * sizeof(INT16));
131 if (s->motion_val == NULL)
133 memset(s->motion_val, 0, size * 2 * sizeof(INT16));
137 int y_size, c_size, i, size;
141 y_size = (2 * s->mb_width + 2) * (2 * s->mb_height + 2);
142 c_size = (s->mb_width + 2) * (s->mb_height + 2);
143 size = y_size + 2 * c_size;
144 s->dc_val[0] = malloc(size * sizeof(INT16));
145 if (s->dc_val[0] == NULL)
147 s->dc_val[1] = s->dc_val[0] + y_size;
148 s->dc_val[2] = s->dc_val[1] + c_size;
150 s->dc_val[0][i] = 1024;
153 s->ac_val[0] = av_mallocz(size * sizeof(INT16) * 16);
154 if (s->ac_val[0] == NULL)
156 s->ac_val[1] = s->ac_val[0] + y_size;
157 s->ac_val[2] = s->ac_val[1] + c_size;
160 s->coded_block = av_mallocz(y_size);
164 /* default structure is frame */
165 s->picture_structure = PICT_FRAME;
167 /* init default q matrix (only for mpeg and mjpeg) */
169 s->intra_matrix[i] = default_intra_matrix[i];
170 s->chroma_intra_matrix[i] = default_intra_matrix[i];
171 s->non_intra_matrix[i] = default_non_intra_matrix[i];
172 s->chroma_non_intra_matrix[i] = default_non_intra_matrix[i];
174 /* init macroblock skip table */
176 s->mbskip_table = av_mallocz(s->mb_width * s->mb_height);
177 if (!s->mbskip_table)
181 s->context_initialized = 1;
191 free(s->coded_block);
193 free(s->mbskip_table);
195 if (s->last_picture_base[i])
196 free(s->last_picture_base[i]);
197 if (s->next_picture_base[i])
198 free(s->next_picture_base[i]);
199 if (s->aux_picture_base[i])
200 free(s->aux_picture_base[i]);
205 /* init common structure for both encoder and decoder */
206 void MPV_common_end(MpegEncContext *s)
215 free(s->coded_block);
218 free(s->mbskip_table);
220 free(s->last_picture_base[i]);
221 free(s->next_picture_base[i]);
223 free(s->aux_picture_base[i]);
225 s->context_initialized = 0;
228 /* init video encoder */
229 int MPV_encode_init(AVCodecContext *avctx)
231 MpegEncContext *s = avctx->priv_data;
233 s->bit_rate = avctx->bit_rate;
234 s->frame_rate = avctx->frame_rate;
235 s->width = avctx->width;
236 s->height = avctx->height;
237 s->gop_size = avctx->gop_size;
238 if (s->gop_size <= 1) {
244 s->full_search = motion_estimation_method;
246 s->fixed_qscale = (avctx->flags & CODEC_FLAG_QSCALE);
248 switch(avctx->codec->id) {
249 case CODEC_ID_MPEG1VIDEO:
250 s->out_format = FMT_MPEG1;
253 s->out_format = FMT_MJPEG;
254 s->intra_only = 1; /* force intra only for jpeg */
255 if (mjpeg_init(s) < 0)
259 if (h263_get_picture_format(s->width, s->height) == 7)
261 s->out_format = FMT_H263;
264 s->out_format = FMT_H263;
266 /* XXX: not unrectricted mv yet */
269 s->out_format = FMT_H263;
272 case CODEC_ID_OPENDIVX:
273 s->out_format = FMT_H263;
275 s->unrestricted_mv = 1;
277 case CODEC_ID_MSMPEG4:
278 s->out_format = FMT_H263;
281 s->unrestricted_mv = 1;
287 if (s->out_format == FMT_H263)
288 h263_encode_init_vlc(s);
293 if (MPV_common_init(s) < 0)
296 /* rate control init */
297 rate_control_init(s);
299 s->picture_number = 0;
300 s->fake_picture_number = 0;
301 /* motion detector init */
307 int MPV_encode_end(AVCodecContext *avctx)
309 MpegEncContext *s = avctx->priv_data;
315 if (s->out_format == FMT_MJPEG)
320 /* draw the edges of width 'w' of an image of size width, height */
321 static void draw_edges(UINT8 *buf, int wrap, int width, int height, int w)
323 UINT8 *ptr, *last_line;
326 last_line = buf + (height - 1) * wrap;
329 memcpy(buf - (i + 1) * wrap, buf, width);
330 memcpy(last_line + (i + 1) * wrap, last_line, width);
334 for(i=0;i<height;i++) {
335 memset(ptr - w, ptr[0], w);
336 memset(ptr + width, ptr[width-1], w);
341 memset(buf - (i + 1) * wrap - w, buf[0], w); /* top left */
342 memset(buf - (i + 1) * wrap + width, buf[width-1], w); /* top right */
343 memset(last_line + (i + 1) * wrap - w, last_line[0], w); /* top left */
344 memset(last_line + (i + 1) * wrap + width, last_line[width-1], w); /* top right */
349 /* generic function for encode/decode called before a frame is coded/decoded */
350 void MPV_frame_start(MpegEncContext *s)
355 if (s->pict_type == B_TYPE) {
357 s->current_picture[i] = s->aux_picture[i];
361 /* swap next and last */
362 tmp = s->last_picture[i];
363 s->last_picture[i] = s->next_picture[i];
364 s->next_picture[i] = tmp;
365 s->current_picture[i] = tmp;
370 /* generic function for encode/decode called after a frame has been coded/decoded */
371 void MPV_frame_end(MpegEncContext *s)
373 /* draw edge for correct motion prediction if outside */
374 if (s->pict_type != B_TYPE) {
375 draw_edges(s->current_picture[0], s->linesize, s->width, s->height, EDGE_WIDTH);
376 draw_edges(s->current_picture[1], s->linesize/2, s->width/2, s->height/2, EDGE_WIDTH/2);
377 draw_edges(s->current_picture[2], s->linesize/2, s->width/2, s->height/2, EDGE_WIDTH/2);
381 int MPV_encode_picture(AVCodecContext *avctx,
382 unsigned char *buf, int buf_size, void *data)
384 MpegEncContext *s = avctx->priv_data;
385 AVPicture *pict = data;
389 s->qscale = avctx->quality;
391 init_put_bits(&s->pb, buf, buf_size, NULL, NULL);
393 if (!s->intra_only) {
394 /* first picture of GOP is intra */
395 if ((s->picture_number % s->gop_size) == 0)
396 s->pict_type = I_TYPE;
398 s->pict_type = P_TYPE;
400 s->pict_type = I_TYPE;
402 avctx->key_frame = (s->pict_type == I_TYPE);
407 UINT8 *src = pict->data[i];
408 UINT8 *dest = s->current_picture[i];
409 int src_wrap = pict->linesize[i];
410 int dest_wrap = s->linesize;
421 memcpy(dest, src, w);
425 s->new_picture[i] = s->current_picture[i];
428 encode_picture(s, s->picture_number);
433 if (s->out_format == FMT_MJPEG)
434 mjpeg_picture_trailer(s);
436 flush_put_bits(&s->pb);
437 s->total_bits += (s->pb.buf_ptr - s->pb.buf) * 8;
438 avctx->quality = s->qscale;
439 return s->pb.buf_ptr - s->pb.buf;
442 static inline int clip(int a, int amin, int amax)
452 static int dct_quantize(MpegEncContext *s, DCTELEM *block, int n, int qscale);
453 static int dct_quantize_mmx(MpegEncContext *s,
454 DCTELEM *block, int n,
456 static void dct_unquantize(MpegEncContext *s, DCTELEM *block, int n, int qscale);
458 /* apply one mpeg motion vector to the three components */
459 static inline void mpeg_motion(MpegEncContext *s,
460 UINT8 *dest_y, UINT8 *dest_cb, UINT8 *dest_cr,
462 UINT8 **ref_picture, int src_offset,
463 int field_based, op_pixels_func *pix_op,
464 int motion_x, int motion_y, int h)
467 int dxy, offset, mx, my, src_x, src_y, height, linesize;
469 dxy = ((motion_y & 1) << 1) | (motion_x & 1);
470 src_x = s->mb_x * 16 + (motion_x >> 1);
471 src_y = s->mb_y * (16 >> field_based) + (motion_y >> 1);
473 /* WARNING: do no forget half pels */
474 height = s->height >> field_based;
475 src_x = clip(src_x, -16, s->width);
476 if (src_x == s->width)
478 src_y = clip(src_y, -16, height);
481 linesize = s->linesize << field_based;
482 ptr = ref_picture[0] + (src_y * linesize) + (src_x) + src_offset;
483 dest_y += dest_offset;
484 pix_op[dxy](dest_y, ptr, linesize, h);
485 pix_op[dxy](dest_y + 8, ptr + 8, linesize, h);
487 if (s->out_format == FMT_H263) {
489 if ((motion_x & 3) != 0)
491 if ((motion_y & 3) != 0)
498 dxy = ((my & 1) << 1) | (mx & 1);
503 src_x = s->mb_x * 8 + mx;
504 src_y = s->mb_y * (8 >> field_based) + my;
505 src_x = clip(src_x, -8, s->width >> 1);
506 if (src_x == (s->width >> 1))
508 src_y = clip(src_y, -8, height >> 1);
509 if (src_y == (height >> 1))
512 offset = (src_y * (linesize >> 1)) + src_x + (src_offset >> 1);
513 ptr = ref_picture[1] + offset;
514 pix_op[dxy](dest_cb + (dest_offset >> 1), ptr, linesize >> 1, h >> 1);
515 ptr = ref_picture[2] + offset;
516 pix_op[dxy](dest_cr + (dest_offset >> 1), ptr, linesize >> 1, h >> 1);
519 static inline void MPV_motion(MpegEncContext *s,
520 UINT8 *dest_y, UINT8 *dest_cb, UINT8 *dest_cr,
521 int dir, UINT8 **ref_picture,
522 op_pixels_func *pix_op)
524 int dxy, offset, mx, my, src_x, src_y, motion_x, motion_y;
533 mpeg_motion(s, dest_y, dest_cb, dest_cr, 0,
536 s->mv[dir][0][0], s->mv[dir][0][1], 16);
540 motion_x = s->mv[dir][i][0];
541 motion_y = s->mv[dir][i][1];
543 dxy = ((motion_y & 1) << 1) | (motion_x & 1);
544 src_x = mb_x * 16 + (motion_x >> 1) + (i & 1) * 8;
545 src_y = mb_y * 16 + (motion_y >> 1) + ((i >> 1) & 1) * 8;
547 /* WARNING: do no forget half pels */
548 src_x = clip(src_x, -16, s->width);
549 if (src_x == s->width)
551 src_y = clip(src_y, -16, s->height);
552 if (src_y == s->height)
555 ptr = ref_picture[0] + (src_y * s->linesize) + (src_x);
556 dest = dest_y + ((i & 1) * 8) + (i >> 1) * 8 * s->linesize;
557 pix_op[dxy](dest, ptr, s->linesize, 8);
559 /* In case of 8X8, we construct a single chroma motion vector
560 with a special rounding */
564 mx += s->mv[dir][i][0];
565 my += s->mv[dir][i][1];
568 mx = (h263_chroma_roundtab[mx & 0xf] + ((mx >> 3) & ~1));
571 mx = -(h263_chroma_roundtab[mx & 0xf] + ((mx >> 3) & ~1));
574 my = (h263_chroma_roundtab[my & 0xf] + ((my >> 3) & ~1));
577 my = -(h263_chroma_roundtab[my & 0xf] + ((my >> 3) & ~1));
579 dxy = ((my & 1) << 1) | (mx & 1);
583 src_x = mb_x * 8 + mx;
584 src_y = mb_y * 8 + my;
585 src_x = clip(src_x, -8, s->width/2);
586 if (src_x == s->width/2)
588 src_y = clip(src_y, -8, s->height/2);
589 if (src_y == s->height/2)
592 offset = (src_y * (s->linesize >> 1)) + src_x;
593 ptr = ref_picture[1] + offset;
594 pix_op[dxy](dest_cb, ptr, s->linesize >> 1, 8);
595 ptr = ref_picture[2] + offset;
596 pix_op[dxy](dest_cr, ptr, s->linesize >> 1, 8);
599 if (s->picture_structure == PICT_FRAME) {
601 mpeg_motion(s, dest_y, dest_cb, dest_cr, 0,
602 ref_picture, s->field_select[dir][0] ? s->linesize : 0,
604 s->mv[dir][0][0], s->mv[dir][0][1], 8);
606 mpeg_motion(s, dest_y, dest_cb, dest_cr, s->linesize,
607 ref_picture, s->field_select[dir][1] ? s->linesize : 0,
609 s->mv[dir][1][0], s->mv[dir][1][1], 8);
619 /* put block[] to dest[] */
620 static inline void put_dct(MpegEncContext *s,
621 DCTELEM *block, int i, UINT8 *dest, int line_size)
624 dct_unquantize(s, block, i, s->qscale);
626 put_pixels_clamped(block, dest, line_size);
629 /* add block[] to dest[] */
630 static inline void add_dct(MpegEncContext *s,
631 DCTELEM *block, int i, UINT8 *dest, int line_size)
633 if (s->block_last_index[i] >= 0) {
635 dct_unquantize(s, block, i, s->qscale);
637 add_pixels_clamped(block, dest, line_size);
641 /* generic function called after a macroblock has been parsed by the
642 decoder or after it has been encoded by the encoder.
644 Important variables used:
645 s->mb_intra : true if intra macroblock
646 s->mv_dir : motion vector direction
647 s->mv_type : motion vector type
648 s->mv : motion vector
649 s->interlaced_dct : true if interlaced dct used (mpeg2)
651 void MPV_decode_mb(MpegEncContext *s, DCTELEM block[6][64])
653 int mb_x, mb_y, motion_x, motion_y;
654 int dct_linesize, dct_offset;
655 op_pixels_func *op_pix;
660 /* update DC predictors for P macroblocks */
664 wrap = 2 * s->mb_width + 2;
668 s->dc_val[0][(x) + (y) * wrap] = v;
669 s->dc_val[0][(x + 1) + (y) * wrap] = v;
670 s->dc_val[0][(x) + (y + 1) * wrap] = v;
671 s->dc_val[0][(x + 1) + (y + 1) * wrap] = v;
673 memset(s->ac_val[0][(x) + (y) * wrap], 0, 16 * sizeof(INT16));
674 memset(s->ac_val[0][(x + 1) + (y) * wrap], 0, 16 * sizeof(INT16));
675 memset(s->ac_val[0][(x) + (y + 1) * wrap], 0, 16 * sizeof(INT16));
676 memset(s->ac_val[0][(x + 1) + (y + 1) * wrap], 0, 16 * sizeof(INT16));
677 if (s->h263_msmpeg4) {
678 s->coded_block[(x) + (y) * wrap] = 0;
679 s->coded_block[(x + 1) + (y) * wrap] = 0;
680 s->coded_block[(x) + (y + 1) * wrap] = 0;
681 s->coded_block[(x + 1) + (y + 1) * wrap] = 0;
684 wrap = s->mb_width + 2;
687 s->dc_val[1][(x) + (y) * wrap] = v;
688 s->dc_val[2][(x) + (y) * wrap] = v;
690 memset(s->ac_val[1][(x) + (y) * wrap], 0, 16 * sizeof(INT16));
691 memset(s->ac_val[2][(x) + (y) * wrap], 0, 16 * sizeof(INT16));
693 s->last_dc[0] = 128 << s->intra_dc_precision;
694 s->last_dc[1] = 128 << s->intra_dc_precision;
695 s->last_dc[2] = 128 << s->intra_dc_precision;
699 /* update motion predictor */
700 if (s->out_format == FMT_H263) {
705 wrap = 2 * s->mb_width + 2;
710 } else if (s->mv_type == MV_TYPE_16X16) {
711 motion_x = s->mv[0][0][0];
712 motion_y = s->mv[0][0][1];
714 /* no update if 8X8 because it has been done during parsing */
715 s->motion_val[(x) + (y) * wrap][0] = motion_x;
716 s->motion_val[(x) + (y) * wrap][1] = motion_y;
717 s->motion_val[(x + 1) + (y) * wrap][0] = motion_x;
718 s->motion_val[(x + 1) + (y) * wrap][1] = motion_y;
719 s->motion_val[(x) + (y + 1) * wrap][0] = motion_x;
720 s->motion_val[(x) + (y + 1) * wrap][1] = motion_y;
721 s->motion_val[(x + 1) + (y + 1) * wrap][0] = motion_x;
722 s->motion_val[(x + 1) + (y + 1) * wrap][1] = motion_y;
726 if (!s->intra_only) {
727 UINT8 *dest_y, *dest_cb, *dest_cr;
730 /* avoid copy if macroblock skipped in last frame too */
732 mbskip_ptr = &s->mbskip_table[s->mb_y * s->mb_width + s->mb_x];
735 /* if previous was skipped too, then nothing to do ! */
736 if (*mbskip_ptr != 0)
738 *mbskip_ptr = 1; /* indicate that this time we skiped it */
740 *mbskip_ptr = 0; /* not skipped */
744 dest_y = s->current_picture[0] + (mb_y * 16 * s->linesize) + mb_x * 16;
745 dest_cb = s->current_picture[1] + (mb_y * 8 * (s->linesize >> 1)) + mb_x * 8;
746 dest_cr = s->current_picture[2] + (mb_y * 8 * (s->linesize >> 1)) + mb_x * 8;
748 if (s->interlaced_dct) {
749 dct_linesize = s->linesize * 2;
750 dct_offset = s->linesize;
752 dct_linesize = s->linesize;
753 dct_offset = s->linesize * 8;
757 /* motion handling */
759 op_pix = put_pixels_tab;
761 op_pix = put_no_rnd_pixels_tab;
763 if (s->mv_dir & MV_DIR_FORWARD) {
764 MPV_motion(s, dest_y, dest_cb, dest_cr, 0, s->last_picture, op_pix);
766 op_pix = avg_pixels_tab;
768 op_pix = avg_no_rnd_pixels_tab;
770 if (s->mv_dir & MV_DIR_BACKWARD) {
771 MPV_motion(s, dest_y, dest_cb, dest_cr, 1, s->next_picture, op_pix);
774 /* add dct residue */
775 add_dct(s, block[0], 0, dest_y, dct_linesize);
776 add_dct(s, block[1], 1, dest_y + 8, dct_linesize);
777 add_dct(s, block[2], 2, dest_y + dct_offset, dct_linesize);
778 add_dct(s, block[3], 3, dest_y + dct_offset + 8, dct_linesize);
780 add_dct(s, block[4], 4, dest_cb, dct_linesize >> 1);
781 add_dct(s, block[5], 5, dest_cr, dct_linesize >> 1);
783 /* dct only in intra block */
784 put_dct(s, block[0], 0, dest_y, dct_linesize);
785 put_dct(s, block[1], 1, dest_y + 8, dct_linesize);
786 put_dct(s, block[2], 2, dest_y + dct_offset, dct_linesize);
787 put_dct(s, block[3], 3, dest_y + dct_offset + 8, dct_linesize);
789 put_dct(s, block[4], 4, dest_cb, dct_linesize >> 1);
790 put_dct(s, block[5], 5, dest_cr, dct_linesize >> 1);
797 static void encode_picture(MpegEncContext *s, int picture_number)
799 int mb_x, mb_y, wrap;
801 DCTELEM block[6][64];
802 int i, motion_x, motion_y;
804 s->picture_number = picture_number;
805 if (!s->fixed_qscale)
806 s->qscale = rate_estimate_qscale(s);
808 /* precompute matrix */
809 if (s->out_format == FMT_MJPEG) {
810 /* for mjpeg, we do include qscale in the matrix */
811 s->intra_matrix[0] = default_intra_matrix[0];
813 s->intra_matrix[i] = (default_intra_matrix[i] * s->qscale) >> 3;
814 convert_matrix(s->q_intra_matrix, s->intra_matrix, 8);
816 convert_matrix(s->q_intra_matrix, s->intra_matrix, s->qscale);
817 convert_matrix(s->q_non_intra_matrix, s->non_intra_matrix, s->qscale);
820 switch(s->out_format) {
822 mjpeg_picture_header(s);
826 msmpeg4_encode_picture_header(s, picture_number);
827 else if (s->h263_pred)
828 mpeg4_encode_picture_header(s, picture_number);
829 else if (s->h263_rv10)
830 rv10_encode_picture_header(s, picture_number);
832 h263_encode_picture_header(s, picture_number);
835 mpeg1_encode_picture_header(s, picture_number);
839 /* init last dc values */
840 /* note: quant matrix value (8) is implied here */
845 s->last_mv[0][0][0] = 0;
846 s->last_mv[0][0][1] = 0;
847 s->mv_type = MV_TYPE_16X16;
848 s->mv_dir = MV_DIR_FORWARD;
850 for(mb_y=0; mb_y < s->mb_height; mb_y++) {
851 for(mb_x=0; mb_x < s->mb_width; mb_x++) {
856 /* compute motion vector and macro block type (intra or non intra) */
859 if (s->pict_type == P_TYPE) {
860 s->mb_intra = estimate_motion(s, mb_x, mb_y,
869 ptr = s->new_picture[0] + (mb_y * 16 * wrap) + mb_x * 16;
870 get_pixels(block[0], ptr, wrap);
871 get_pixels(block[1], ptr + 8, wrap);
872 get_pixels(block[2], ptr + 8 * wrap, wrap);
873 get_pixels(block[3], ptr + 8 * wrap + 8, wrap);
874 wrap = s->linesize >> 1;
875 ptr = s->new_picture[1] + (mb_y * 8 * wrap) + mb_x * 8;
876 get_pixels(block[4], ptr, wrap);
878 wrap = s->linesize >> 1;
879 ptr = s->new_picture[2] + (mb_y * 8 * wrap) + mb_x * 8;
880 get_pixels(block[5], ptr, wrap);
882 /* subtract previous frame if non intra */
884 int dxy, offset, mx, my;
886 dxy = ((motion_y & 1) << 1) | (motion_x & 1);
887 ptr = s->last_picture[0] +
888 ((mb_y * 16 + (motion_y >> 1)) * s->linesize) +
889 (mb_x * 16 + (motion_x >> 1));
891 sub_pixels_2(block[0], ptr, s->linesize, dxy);
892 sub_pixels_2(block[1], ptr + 8, s->linesize, dxy);
893 sub_pixels_2(block[2], ptr + s->linesize * 8, s->linesize, dxy);
894 sub_pixels_2(block[3], ptr + 8 + s->linesize * 8, s->linesize ,dxy);
896 if (s->out_format == FMT_H263) {
897 /* special rounding for h263 */
899 if ((motion_x & 3) != 0)
901 if ((motion_y & 3) != 0)
908 dxy = ((my & 1) << 1) | (mx & 1);
912 offset = ((mb_y * 8 + my) * (s->linesize >> 1)) + (mb_x * 8 + mx);
913 ptr = s->last_picture[1] + offset;
914 sub_pixels_2(block[4], ptr, s->linesize >> 1, dxy);
915 ptr = s->last_picture[2] + offset;
916 sub_pixels_2(block[5], ptr, s->linesize >> 1, dxy);
921 if (s->h263_msmpeg4) {
923 } else if (s->h263_pred) {
926 /* default quantization values */
933 if (av_fdct == jpeg_fdct_ifast)
934 last_index = dct_quantize(s, block[i], i, s->qscale);
936 last_index = dct_quantize_mmx(s, block[i], i, s->qscale);
937 s->block_last_index[i] = last_index;
941 switch(s->out_format) {
943 mpeg1_encode_mb(s, block, motion_x, motion_y);
947 msmpeg4_encode_mb(s, block, motion_x, motion_y);
949 h263_encode_mb(s, block, motion_x, motion_y);
952 mjpeg_encode_mb(s, block);
956 /* decompress blocks so that we keep the state of the decoder */
957 s->mv[0][0][0] = motion_x;
958 s->mv[0][0][1] = motion_y;
960 MPV_decode_mb(s, block);
965 static int dct_quantize(MpegEncContext *s,
966 DCTELEM *block, int n,
969 int i, j, level, last_non_zero, q;
981 /* note: block[0] is assumed to be positive */
982 block[0] = (block[0] + (q >> 1)) / q;
985 if (s->out_format == FMT_H263) {
986 qmat = s->q_non_intra_matrix;
988 qmat = s->q_intra_matrix;
993 qmat = s->q_non_intra_matrix;
997 j = zigzag_direct[i];
999 level = level * qmat[j];
1002 static int count = 0;
1003 int level1, level2, qmat1;
1005 if (qmat == s->q_non_intra_matrix) {
1006 qmat1 = default_non_intra_matrix[j] * s->qscale;
1008 qmat1 = default_intra_matrix[j] * s->qscale;
1010 if (av_fdct != jpeg_fdct_ifast)
1011 val = ((double)block[j] * 8.0) / (double)qmat1;
1013 val = ((double)block[j] * 8.0 * 2048.0) /
1014 ((double)qmat1 * aanscales[j]);
1016 level2 = level / (1 << (QMAT_SHIFT - 3));
1017 if (level1 != level2) {
1018 fprintf(stderr, "%d: quant error qlevel=%d wanted=%d level=%d qmat1=%d qmat=%d wantedf=%0.6f\n",
1019 count, level2, level1, block[j], qmat1, qmat[j],
1026 /* XXX: slight error for the low range. Test should be equivalent to
1027 (level <= -(1 << (QMAT_SHIFT - 3)) || level >= (1 <<
1030 if (((level << (31 - (QMAT_SHIFT - 3))) >> (31 - (QMAT_SHIFT - 3))) !=
1032 level = level / (1 << (QMAT_SHIFT - 3));
1033 /* XXX: currently, this code is not optimal. the range should be:
1041 else if (level < -128)
1049 return last_non_zero;
1052 static int dct_quantize_mmx(MpegEncContext *s,
1053 DCTELEM *block, int n,
1056 int i, j, level, last_non_zero, q;
1067 /* note: block[0] is assumed to be positive */
1068 block[0] = (block[0] + (q >> 1)) / q;
1071 if (s->out_format == FMT_H263) {
1072 qmat = s->q_non_intra_matrix;
1074 qmat = s->q_intra_matrix;
1079 qmat = s->q_non_intra_matrix;
1083 j = zigzag_direct[i];
1085 level = level * qmat[j];
1086 /* XXX: slight error for the low range. Test should be equivalent to
1087 (level <= -(1 << (QMAT_SHIFT_MMX - 3)) || level >= (1 <<
1088 (QMAT_SHIFT_MMX - 3)))
1090 if (((level << (31 - (QMAT_SHIFT_MMX - 3))) >> (31 - (QMAT_SHIFT_MMX - 3))) !=
1092 level = level / (1 << (QMAT_SHIFT_MMX - 3));
1093 /* XXX: currently, this code is not optimal. the range should be:
1101 else if (level < -128)
1109 return last_non_zero;
1112 static void dct_unquantize(MpegEncContext *s,
1113 DCTELEM *block, int n, int qscale)
1116 const UINT16 *quant_matrix;
1120 block[0] = block[0] * s->y_dc_scale;
1122 block[0] = block[0] * s->c_dc_scale;
1123 if (s->out_format == FMT_H263) {
1127 /* XXX: only mpeg1 */
1128 quant_matrix = s->intra_matrix;
1134 level = (int)(level * qscale * quant_matrix[i]) >> 3;
1135 level = (level - 1) | 1;
1138 level = (int)(level * qscale * quant_matrix[i]) >> 3;
1139 level = (level - 1) | 1;
1142 if (level < -2048 || level > 2047)
1143 fprintf(stderr, "unquant error %d %d\n", i, level);
1151 quant_matrix = s->non_intra_matrix;
1157 level = (((level << 1) + 1) * qscale *
1158 ((int) (quant_matrix[i]))) >> 4;
1159 level = (level - 1) | 1;
1162 level = (((level << 1) + 1) * qscale *
1163 ((int) (quant_matrix[i]))) >> 4;
1164 level = (level - 1) | 1;
1167 if (level < -2048 || level > 2047)
1168 fprintf(stderr, "unquant error %d %d\n", i, level);
1179 /* an I frame is I_FRAME_SIZE_RATIO bigger than a P frame */
1180 #define I_FRAME_SIZE_RATIO 3.0
1183 static void rate_control_init(MpegEncContext *s)
1187 if (s->intra_only) {
1188 s->I_frame_bits = ((INT64)s->bit_rate * FRAME_RATE_BASE) / s->frame_rate;
1189 s->P_frame_bits = s->I_frame_bits;
1191 s->P_frame_bits = (int) ((float)(s->gop_size * s->bit_rate) /
1192 (float)((float)s->frame_rate / FRAME_RATE_BASE * (I_FRAME_SIZE_RATIO + s->gop_size - 1)));
1193 s->I_frame_bits = (int)(s->P_frame_bits * I_FRAME_SIZE_RATIO);
1197 printf("I_frame_size=%d P_frame_size=%d\n",
1198 s->I_frame_bits, s->P_frame_bits);
1204 * This heuristic is rather poor, but at least we do not have to
1205 * change the qscale at every macroblock.
1207 static int rate_estimate_qscale(MpegEncContext *s)
1209 long long total_bits = s->total_bits;
1211 int qscale, diff, qmin;
1213 if (s->pict_type == I_TYPE) {
1214 s->wanted_bits += s->I_frame_bits;
1216 s->wanted_bits += s->P_frame_bits;
1218 diff = s->wanted_bits - total_bits;
1219 q = 31.0 - (float)diff / (QSCALE_K * s->mb_height * s->mb_width);
1220 /* adjust for I frame */
1221 if (s->pict_type == I_TYPE && !s->intra_only) {
1222 q /= I_FRAME_SIZE_RATIO;
1225 /* using a too small Q scale leeds to problems in mpeg1 and h263
1226 because AC coefficients are clamped to 255 or 127 */
1232 qscale = (int)(q + 0.5);
1234 printf("%d: total=%Ld br=%0.1f diff=%d qest=%0.1f\n",
1237 (float)s->frame_rate / FRAME_RATE_BASE *
1238 total_bits / s->picture_number,
1244 AVCodec mpeg1video_encoder = {
1247 CODEC_ID_MPEG1VIDEO,
1248 sizeof(MpegEncContext),
1254 AVCodec h263_encoder = {
1258 sizeof(MpegEncContext),
1264 AVCodec h263p_encoder = {
1268 sizeof(MpegEncContext),
1274 AVCodec rv10_encoder = {
1278 sizeof(MpegEncContext),
1284 AVCodec mjpeg_encoder = {
1288 sizeof(MpegEncContext),
1294 AVCodec opendivx_encoder = {
1298 sizeof(MpegEncContext),
1304 AVCodec msmpeg4_encoder = {
1308 sizeof(MpegEncContext),