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"
27 /* Stuff below is useful only for mplayer project */
29 #include "../config.h"
33 #include "fastmemcpy.h"
36 static void encode_picture(MpegEncContext *s, int picture_number);
37 static void rate_control_init(MpegEncContext *s);
38 static int rate_estimate_qscale(MpegEncContext *s);
39 static void dct_unquantize_mpeg1_c(MpegEncContext *s,
40 DCTELEM *block, int n, int qscale);
41 static void dct_unquantize_h263_c(MpegEncContext *s,
42 DCTELEM *block, int n, int qscale);
43 static int dct_quantize(MpegEncContext *s, DCTELEM *block, int n, int qscale);
44 static int dct_quantize_mmx(MpegEncContext *s,
45 DCTELEM *block, int n,
49 /* enable all paranoid tests for rounding, overflows, etc... */
54 /* for jpeg fast DCT */
57 static const unsigned short aanscales[64] = {
58 /* precomputed values scaled up by 14 bits */
59 16384, 22725, 21407, 19266, 16384, 12873, 8867, 4520,
60 22725, 31521, 29692, 26722, 22725, 17855, 12299, 6270,
61 21407, 29692, 27969, 25172, 21407, 16819, 11585, 5906,
62 19266, 26722, 25172, 22654, 19266, 15137, 10426, 5315,
63 16384, 22725, 21407, 19266, 16384, 12873, 8867, 4520,
64 12873, 17855, 16819, 15137, 12873, 10114, 6967, 3552,
65 8867, 12299, 11585, 10426, 8867, 6967, 4799, 2446,
66 4520, 6270, 5906, 5315, 4520, 3552, 2446, 1247
69 static UINT8 h263_chroma_roundtab[16] = {
70 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2,
73 /* default motion estimation */
74 int motion_estimation_method = ME_LOG;
76 /* XXX: should use variable shift ? */
77 #define QMAT_SHIFT_MMX 19
80 static void convert_matrix(int *qmat, const UINT16 *quant_matrix, int qscale)
84 if (av_fdct == jpeg_fdct_ifast) {
86 /* 16 <= qscale * quant_matrix[i] <= 7905 */
87 /* 19952 <= aanscales[i] * qscale * quant_matrix[i] <= 249205026 */
89 qmat[i] = (int)((1ULL << (QMAT_SHIFT + 11)) / (aanscales[i] * qscale * quant_matrix[i]));
93 /* We can safely suppose that 16 <= quant_matrix[i] <= 255
94 So 16 <= qscale * quant_matrix[i] <= 7905
95 so (1 << QMAT_SHIFT) / 16 >= qmat[i] >= (1 << QMAT_SHIFT) / 7905
97 qmat[i] = (1 << QMAT_SHIFT_MMX) / (qscale * quant_matrix[i]);
102 /* init common structure for both encoder and decoder */
103 int MPV_common_init(MpegEncContext *s)
108 if (s->out_format == FMT_H263)
109 s->dct_unquantize = dct_unquantize_h263_c;
111 s->dct_unquantize = dct_unquantize_mpeg1_c;
114 MPV_common_init_mmx(s);
116 s->mb_width = (s->width + 15) / 16;
117 s->mb_height = (s->height + 15) / 16;
118 s->linesize = s->mb_width * 16 + 2 * EDGE_WIDTH;
121 int w, h, shift, pict_start;
124 h = s->mb_height * 16 + 2 * EDGE_WIDTH;
125 shift = (i == 0) ? 0 : 1;
126 c_size = (w >> shift) * (h >> shift);
127 pict_start = (w >> shift) * (EDGE_WIDTH >> shift) + (EDGE_WIDTH >> shift);
129 pict = av_mallocz(c_size);
132 s->last_picture_base[i] = pict;
133 s->last_picture[i] = pict + pict_start;
135 pict = av_mallocz(c_size);
138 s->next_picture_base[i] = pict;
139 s->next_picture[i] = pict + pict_start;
141 if (s->has_b_frames) {
142 pict = av_mallocz(c_size);
145 s->aux_picture_base[i] = pict;
146 s->aux_picture[i] = pict + pict_start;
150 if (s->out_format == FMT_H263) {
153 size = (2 * s->mb_width + 2) * (2 * s->mb_height + 2);
154 s->motion_val = malloc(size * 2 * sizeof(INT16));
155 if (s->motion_val == NULL)
157 memset(s->motion_val, 0, size * 2 * sizeof(INT16));
161 int y_size, c_size, i, size;
165 y_size = (2 * s->mb_width + 2) * (2 * s->mb_height + 2);
166 c_size = (s->mb_width + 2) * (s->mb_height + 2);
167 size = y_size + 2 * c_size;
168 s->dc_val[0] = malloc(size * sizeof(INT16));
169 if (s->dc_val[0] == NULL)
171 s->dc_val[1] = s->dc_val[0] + y_size;
172 s->dc_val[2] = s->dc_val[1] + c_size;
174 s->dc_val[0][i] = 1024;
177 s->ac_val[0] = av_mallocz(size * sizeof(INT16) * 16);
178 if (s->ac_val[0] == NULL)
180 s->ac_val[1] = s->ac_val[0] + y_size;
181 s->ac_val[2] = s->ac_val[1] + c_size;
184 s->coded_block = av_mallocz(y_size);
188 /* default structure is frame */
189 s->picture_structure = PICT_FRAME;
191 /* init default q matrix (only for mpeg and mjpeg) */
193 s->intra_matrix[i] = default_intra_matrix[i];
194 s->chroma_intra_matrix[i] = default_intra_matrix[i];
195 s->non_intra_matrix[i] = default_non_intra_matrix[i];
196 s->chroma_non_intra_matrix[i] = default_non_intra_matrix[i];
198 /* init macroblock skip table */
200 s->mbskip_table = av_mallocz(s->mb_width * s->mb_height);
201 if (!s->mbskip_table)
205 s->context_initialized = 1;
215 free(s->coded_block);
217 free(s->mbskip_table);
219 if (s->last_picture_base[i])
220 free(s->last_picture_base[i]);
221 if (s->next_picture_base[i])
222 free(s->next_picture_base[i]);
223 if (s->aux_picture_base[i])
224 free(s->aux_picture_base[i]);
229 /* init common structure for both encoder and decoder */
230 void MPV_common_end(MpegEncContext *s)
239 free(s->coded_block);
242 free(s->mbskip_table);
244 free(s->last_picture_base[i]);
245 free(s->next_picture_base[i]);
247 free(s->aux_picture_base[i]);
249 s->context_initialized = 0;
252 /* init video encoder */
253 int MPV_encode_init(AVCodecContext *avctx)
255 MpegEncContext *s = avctx->priv_data;
257 s->bit_rate = avctx->bit_rate;
258 s->frame_rate = avctx->frame_rate;
259 s->width = avctx->width;
260 s->height = avctx->height;
261 s->gop_size = avctx->gop_size;
262 if (s->gop_size <= 1) {
268 s->full_search = motion_estimation_method;
270 s->fixed_qscale = (avctx->flags & CODEC_FLAG_QSCALE);
272 switch(avctx->codec->id) {
273 case CODEC_ID_MPEG1VIDEO:
274 s->out_format = FMT_MPEG1;
277 s->out_format = FMT_MJPEG;
278 s->intra_only = 1; /* force intra only for jpeg */
279 if (mjpeg_init(s) < 0)
283 if (h263_get_picture_format(s->width, s->height) == 7)
285 s->out_format = FMT_H263;
288 s->out_format = FMT_H263;
290 /* XXX: not unrectricted mv yet */
293 s->out_format = FMT_H263;
296 case CODEC_ID_OPENDIVX:
297 s->out_format = FMT_H263;
299 s->unrestricted_mv = 1;
301 case CODEC_ID_MSMPEG4:
302 s->out_format = FMT_H263;
305 s->unrestricted_mv = 1;
311 if (s->out_format == FMT_H263)
312 h263_encode_init_vlc(s);
317 if (MPV_common_init(s) < 0)
320 /* rate control init */
321 rate_control_init(s);
323 s->picture_number = 0;
324 s->fake_picture_number = 0;
325 /* motion detector init */
331 int MPV_encode_end(AVCodecContext *avctx)
333 MpegEncContext *s = avctx->priv_data;
339 if (s->out_format == FMT_MJPEG)
344 /* draw the edges of width 'w' of an image of size width, height */
345 static void draw_edges(UINT8 *buf, int wrap, int width, int height, int w)
347 UINT8 *ptr, *last_line;
350 last_line = buf + (height - 1) * wrap;
353 memcpy(buf - (i + 1) * wrap, buf, width);
354 memcpy(last_line + (i + 1) * wrap, last_line, width);
358 for(i=0;i<height;i++) {
359 memset(ptr - w, ptr[0], w);
360 memset(ptr + width, ptr[width-1], w);
365 memset(buf - (i + 1) * wrap - w, buf[0], w); /* top left */
366 memset(buf - (i + 1) * wrap + width, buf[width-1], w); /* top right */
367 memset(last_line + (i + 1) * wrap - w, last_line[0], w); /* top left */
368 memset(last_line + (i + 1) * wrap + width, last_line[width-1], w); /* top right */
372 /* generic function for encode/decode called before a frame is coded/decoded */
373 void MPV_frame_start(MpegEncContext *s)
378 if (s->pict_type == B_TYPE) {
380 s->current_picture[i] = s->aux_picture[i];
384 /* swap next and last */
385 tmp = s->last_picture[i];
386 s->last_picture[i] = s->next_picture[i];
387 s->next_picture[i] = tmp;
388 s->current_picture[i] = tmp;
393 /* generic function for encode/decode called after a frame has been coded/decoded */
394 void MPV_frame_end(MpegEncContext *s)
396 /* draw edge for correct motion prediction if outside */
397 if (s->pict_type != B_TYPE) {
398 draw_edges(s->current_picture[0], s->linesize, s->width, s->height, EDGE_WIDTH);
399 draw_edges(s->current_picture[1], s->linesize/2, s->width/2, s->height/2, EDGE_WIDTH/2);
400 draw_edges(s->current_picture[2], s->linesize/2, s->width/2, s->height/2, EDGE_WIDTH/2);
404 int MPV_encode_picture(AVCodecContext *avctx,
405 unsigned char *buf, int buf_size, void *data)
407 MpegEncContext *s = avctx->priv_data;
408 AVPicture *pict = data;
412 s->qscale = avctx->quality;
414 init_put_bits(&s->pb, buf, buf_size, NULL, NULL);
416 if (!s->intra_only) {
417 /* first picture of GOP is intra */
418 if ((s->picture_number % s->gop_size) == 0)
419 s->pict_type = I_TYPE;
421 s->pict_type = P_TYPE;
423 s->pict_type = I_TYPE;
425 avctx->key_frame = (s->pict_type == I_TYPE);
430 UINT8 *src = pict->data[i];
431 UINT8 *dest = s->current_picture[i];
432 int src_wrap = pict->linesize[i];
433 int dest_wrap = s->linesize;
444 memcpy(dest, src, w);
448 s->new_picture[i] = s->current_picture[i];
451 encode_picture(s, s->picture_number);
456 if (s->out_format == FMT_MJPEG)
457 mjpeg_picture_trailer(s);
459 flush_put_bits(&s->pb);
460 s->total_bits += (s->pb.buf_ptr - s->pb.buf) * 8;
461 avctx->quality = s->qscale;
462 return s->pb.buf_ptr - s->pb.buf;
465 static inline int clip(int a, int amin, int amax)
475 /* apply one mpeg motion vector to the three components */
476 static inline void mpeg_motion(MpegEncContext *s,
477 UINT8 *dest_y, UINT8 *dest_cb, UINT8 *dest_cr,
479 UINT8 **ref_picture, int src_offset,
480 int field_based, op_pixels_func *pix_op,
481 int motion_x, int motion_y, int h)
484 int dxy, offset, mx, my, src_x, src_y, height, linesize;
486 dxy = ((motion_y & 1) << 1) | (motion_x & 1);
487 src_x = s->mb_x * 16 + (motion_x >> 1);
488 src_y = s->mb_y * (16 >> field_based) + (motion_y >> 1);
490 /* WARNING: do no forget half pels */
491 height = s->height >> field_based;
492 src_x = clip(src_x, -16, s->width);
493 if (src_x == s->width)
495 src_y = clip(src_y, -16, height);
498 linesize = s->linesize << field_based;
499 ptr = ref_picture[0] + (src_y * linesize) + (src_x) + src_offset;
500 dest_y += dest_offset;
501 pix_op[dxy](dest_y, ptr, linesize, h);
502 pix_op[dxy](dest_y + 8, ptr + 8, linesize, h);
504 if (s->out_format == FMT_H263) {
506 if ((motion_x & 3) != 0)
508 if ((motion_y & 3) != 0)
515 dxy = ((my & 1) << 1) | (mx & 1);
520 src_x = s->mb_x * 8 + mx;
521 src_y = s->mb_y * (8 >> field_based) + my;
522 src_x = clip(src_x, -8, s->width >> 1);
523 if (src_x == (s->width >> 1))
525 src_y = clip(src_y, -8, height >> 1);
526 if (src_y == (height >> 1))
529 offset = (src_y * (linesize >> 1)) + src_x + (src_offset >> 1);
530 ptr = ref_picture[1] + offset;
531 pix_op[dxy](dest_cb + (dest_offset >> 1), ptr, linesize >> 1, h >> 1);
532 ptr = ref_picture[2] + offset;
533 pix_op[dxy](dest_cr + (dest_offset >> 1), ptr, linesize >> 1, h >> 1);
536 static inline void MPV_motion(MpegEncContext *s,
537 UINT8 *dest_y, UINT8 *dest_cb, UINT8 *dest_cr,
538 int dir, UINT8 **ref_picture,
539 op_pixels_func *pix_op)
541 int dxy, offset, mx, my, src_x, src_y, motion_x, motion_y;
550 mpeg_motion(s, dest_y, dest_cb, dest_cr, 0,
553 s->mv[dir][0][0], s->mv[dir][0][1], 16);
557 motion_x = s->mv[dir][i][0];
558 motion_y = s->mv[dir][i][1];
560 dxy = ((motion_y & 1) << 1) | (motion_x & 1);
561 src_x = mb_x * 16 + (motion_x >> 1) + (i & 1) * 8;
562 src_y = mb_y * 16 + (motion_y >> 1) + ((i >> 1) & 1) * 8;
564 /* WARNING: do no forget half pels */
565 src_x = clip(src_x, -16, s->width);
566 if (src_x == s->width)
568 src_y = clip(src_y, -16, s->height);
569 if (src_y == s->height)
572 ptr = ref_picture[0] + (src_y * s->linesize) + (src_x);
573 dest = dest_y + ((i & 1) * 8) + (i >> 1) * 8 * s->linesize;
574 pix_op[dxy](dest, ptr, s->linesize, 8);
576 /* In case of 8X8, we construct a single chroma motion vector
577 with a special rounding */
581 mx += s->mv[dir][i][0];
582 my += s->mv[dir][i][1];
585 mx = (h263_chroma_roundtab[mx & 0xf] + ((mx >> 3) & ~1));
588 mx = -(h263_chroma_roundtab[mx & 0xf] + ((mx >> 3) & ~1));
591 my = (h263_chroma_roundtab[my & 0xf] + ((my >> 3) & ~1));
594 my = -(h263_chroma_roundtab[my & 0xf] + ((my >> 3) & ~1));
596 dxy = ((my & 1) << 1) | (mx & 1);
600 src_x = mb_x * 8 + mx;
601 src_y = mb_y * 8 + my;
602 src_x = clip(src_x, -8, s->width/2);
603 if (src_x == s->width/2)
605 src_y = clip(src_y, -8, s->height/2);
606 if (src_y == s->height/2)
609 offset = (src_y * (s->linesize >> 1)) + src_x;
610 ptr = ref_picture[1] + offset;
611 pix_op[dxy](dest_cb, ptr, s->linesize >> 1, 8);
612 ptr = ref_picture[2] + offset;
613 pix_op[dxy](dest_cr, ptr, s->linesize >> 1, 8);
616 if (s->picture_structure == PICT_FRAME) {
618 mpeg_motion(s, dest_y, dest_cb, dest_cr, 0,
619 ref_picture, s->field_select[dir][0] ? s->linesize : 0,
621 s->mv[dir][0][0], s->mv[dir][0][1], 8);
623 mpeg_motion(s, dest_y, dest_cb, dest_cr, s->linesize,
624 ref_picture, s->field_select[dir][1] ? s->linesize : 0,
626 s->mv[dir][1][0], s->mv[dir][1][1], 8);
636 /* put block[] to dest[] */
637 static inline void put_dct(MpegEncContext *s,
638 DCTELEM *block, int i, UINT8 *dest, int line_size)
641 s->dct_unquantize(s, block, i, s->qscale);
643 put_pixels_clamped(block, dest, line_size);
646 /* add block[] to dest[] */
647 static inline void add_dct(MpegEncContext *s,
648 DCTELEM *block, int i, UINT8 *dest, int line_size)
650 if (s->block_last_index[i] >= 0) {
652 s->dct_unquantize(s, block, i, s->qscale);
654 add_pixels_clamped(block, dest, line_size);
658 /* generic function called after a macroblock has been parsed by the
659 decoder or after it has been encoded by the encoder.
661 Important variables used:
662 s->mb_intra : true if intra macroblock
663 s->mv_dir : motion vector direction
664 s->mv_type : motion vector type
665 s->mv : motion vector
666 s->interlaced_dct : true if interlaced dct used (mpeg2)
668 void MPV_decode_mb(MpegEncContext *s, DCTELEM block[6][64])
670 int mb_x, mb_y, motion_x, motion_y;
671 int dct_linesize, dct_offset;
672 op_pixels_func *op_pix;
677 /* update DC predictors for P macroblocks */
681 wrap = 2 * s->mb_width + 2;
685 s->dc_val[0][(x) + (y) * wrap] = v;
686 s->dc_val[0][(x + 1) + (y) * wrap] = v;
687 s->dc_val[0][(x) + (y + 1) * wrap] = v;
688 s->dc_val[0][(x + 1) + (y + 1) * wrap] = v;
690 memset(s->ac_val[0][(x) + (y) * wrap], 0, 16 * sizeof(INT16));
691 memset(s->ac_val[0][(x + 1) + (y) * wrap], 0, 16 * sizeof(INT16));
692 memset(s->ac_val[0][(x) + (y + 1) * wrap], 0, 16 * sizeof(INT16));
693 memset(s->ac_val[0][(x + 1) + (y + 1) * wrap], 0, 16 * sizeof(INT16));
694 if (s->h263_msmpeg4) {
695 s->coded_block[(x) + (y) * wrap] = 0;
696 s->coded_block[(x + 1) + (y) * wrap] = 0;
697 s->coded_block[(x) + (y + 1) * wrap] = 0;
698 s->coded_block[(x + 1) + (y + 1) * wrap] = 0;
701 wrap = s->mb_width + 2;
704 s->dc_val[1][(x) + (y) * wrap] = v;
705 s->dc_val[2][(x) + (y) * wrap] = v;
707 memset(s->ac_val[1][(x) + (y) * wrap], 0, 16 * sizeof(INT16));
708 memset(s->ac_val[2][(x) + (y) * wrap], 0, 16 * sizeof(INT16));
710 s->last_dc[0] = 128 << s->intra_dc_precision;
711 s->last_dc[1] = 128 << s->intra_dc_precision;
712 s->last_dc[2] = 128 << s->intra_dc_precision;
716 /* update motion predictor */
717 if (s->out_format == FMT_H263) {
722 wrap = 2 * s->mb_width + 2;
727 } else if (s->mv_type == MV_TYPE_16X16) {
728 motion_x = s->mv[0][0][0];
729 motion_y = s->mv[0][0][1];
731 /* no update if 8X8 because it has been done during parsing */
732 s->motion_val[(x) + (y) * wrap][0] = motion_x;
733 s->motion_val[(x) + (y) * wrap][1] = motion_y;
734 s->motion_val[(x + 1) + (y) * wrap][0] = motion_x;
735 s->motion_val[(x + 1) + (y) * wrap][1] = motion_y;
736 s->motion_val[(x) + (y + 1) * wrap][0] = motion_x;
737 s->motion_val[(x) + (y + 1) * wrap][1] = motion_y;
738 s->motion_val[(x + 1) + (y + 1) * wrap][0] = motion_x;
739 s->motion_val[(x + 1) + (y + 1) * wrap][1] = motion_y;
743 if (!s->intra_only) {
744 UINT8 *dest_y, *dest_cb, *dest_cr;
747 /* avoid copy if macroblock skipped in last frame too */
748 if (!s->encoding && s->pict_type != B_TYPE) {
749 mbskip_ptr = &s->mbskip_table[s->mb_y * s->mb_width + s->mb_x];
752 /* if previous was skipped too, then nothing to do ! */
753 if (*mbskip_ptr != 0)
755 *mbskip_ptr = 1; /* indicate that this time we skiped it */
757 *mbskip_ptr = 0; /* not skipped */
761 dest_y = s->current_picture[0] + (mb_y * 16 * s->linesize) + mb_x * 16;
762 dest_cb = s->current_picture[1] + (mb_y * 8 * (s->linesize >> 1)) + mb_x * 8;
763 dest_cr = s->current_picture[2] + (mb_y * 8 * (s->linesize >> 1)) + mb_x * 8;
765 if (s->interlaced_dct) {
766 dct_linesize = s->linesize * 2;
767 dct_offset = s->linesize;
769 dct_linesize = s->linesize;
770 dct_offset = s->linesize * 8;
774 /* motion handling */
776 op_pix = put_pixels_tab;
778 op_pix = put_no_rnd_pixels_tab;
780 if (s->mv_dir & MV_DIR_FORWARD) {
781 MPV_motion(s, dest_y, dest_cb, dest_cr, 0, s->last_picture, op_pix);
783 op_pix = avg_pixels_tab;
785 op_pix = avg_no_rnd_pixels_tab;
787 if (s->mv_dir & MV_DIR_BACKWARD) {
788 MPV_motion(s, dest_y, dest_cb, dest_cr, 1, s->next_picture, op_pix);
791 /* add dct residue */
792 add_dct(s, block[0], 0, dest_y, dct_linesize);
793 add_dct(s, block[1], 1, dest_y + 8, dct_linesize);
794 add_dct(s, block[2], 2, dest_y + dct_offset, dct_linesize);
795 add_dct(s, block[3], 3, dest_y + dct_offset + 8, dct_linesize);
797 add_dct(s, block[4], 4, dest_cb, dct_linesize >> 1);
798 add_dct(s, block[5], 5, dest_cr, dct_linesize >> 1);
800 /* dct only in intra block */
801 put_dct(s, block[0], 0, dest_y, dct_linesize);
802 put_dct(s, block[1], 1, dest_y + 8, dct_linesize);
803 put_dct(s, block[2], 2, dest_y + dct_offset, dct_linesize);
804 put_dct(s, block[3], 3, dest_y + dct_offset + 8, dct_linesize);
806 put_dct(s, block[4], 4, dest_cb, dct_linesize >> 1);
807 put_dct(s, block[5], 5, dest_cr, dct_linesize >> 1);
814 static void encode_picture(MpegEncContext *s, int picture_number)
816 int mb_x, mb_y, wrap;
818 int i, motion_x, motion_y;
820 s->picture_number = picture_number;
821 if (!s->fixed_qscale)
822 s->qscale = rate_estimate_qscale(s);
824 /* precompute matrix */
825 if (s->out_format == FMT_MJPEG) {
826 /* for mjpeg, we do include qscale in the matrix */
827 s->intra_matrix[0] = default_intra_matrix[0];
829 s->intra_matrix[i] = (default_intra_matrix[i] * s->qscale) >> 3;
830 convert_matrix(s->q_intra_matrix, s->intra_matrix, 8);
832 convert_matrix(s->q_intra_matrix, s->intra_matrix, s->qscale);
833 convert_matrix(s->q_non_intra_matrix, s->non_intra_matrix, s->qscale);
836 switch(s->out_format) {
838 mjpeg_picture_header(s);
842 msmpeg4_encode_picture_header(s, picture_number);
843 else if (s->h263_pred)
844 mpeg4_encode_picture_header(s, picture_number);
845 else if (s->h263_rv10)
846 rv10_encode_picture_header(s, picture_number);
848 h263_encode_picture_header(s, picture_number);
851 mpeg1_encode_picture_header(s, picture_number);
855 /* init last dc values */
856 /* note: quant matrix value (8) is implied here */
861 s->last_mv[0][0][0] = 0;
862 s->last_mv[0][0][1] = 0;
863 s->mv_type = MV_TYPE_16X16;
864 s->mv_dir = MV_DIR_FORWARD;
866 for(mb_y=0; mb_y < s->mb_height; mb_y++) {
867 for(mb_x=0; mb_x < s->mb_width; mb_x++) {
872 /* compute motion vector and macro block type (intra or non intra) */
875 if (s->pict_type == P_TYPE) {
876 s->mb_intra = estimate_motion(s, mb_x, mb_y,
885 ptr = s->new_picture[0] + (mb_y * 16 * wrap) + mb_x * 16;
886 get_pixels(s->block[0], ptr, wrap);
887 get_pixels(s->block[1], ptr + 8, wrap);
888 get_pixels(s->block[2], ptr + 8 * wrap, wrap);
889 get_pixels(s->block[3], ptr + 8 * wrap + 8, wrap);
890 wrap = s->linesize >> 1;
891 ptr = s->new_picture[1] + (mb_y * 8 * wrap) + mb_x * 8;
892 get_pixels(s->block[4], ptr, wrap);
894 wrap = s->linesize >> 1;
895 ptr = s->new_picture[2] + (mb_y * 8 * wrap) + mb_x * 8;
896 get_pixels(s->block[5], ptr, wrap);
898 /* subtract previous frame if non intra */
900 int dxy, offset, mx, my;
902 dxy = ((motion_y & 1) << 1) | (motion_x & 1);
903 ptr = s->last_picture[0] +
904 ((mb_y * 16 + (motion_y >> 1)) * s->linesize) +
905 (mb_x * 16 + (motion_x >> 1));
907 sub_pixels_2(s->block[0], ptr, s->linesize, dxy);
908 sub_pixels_2(s->block[1], ptr + 8, s->linesize, dxy);
909 sub_pixels_2(s->block[2], ptr + s->linesize * 8, s->linesize, dxy);
910 sub_pixels_2(s->block[3], ptr + 8 + s->linesize * 8, s->linesize ,dxy);
912 if (s->out_format == FMT_H263) {
913 /* special rounding for h263 */
915 if ((motion_x & 3) != 0)
917 if ((motion_y & 3) != 0)
924 dxy = ((my & 1) << 1) | (mx & 1);
928 offset = ((mb_y * 8 + my) * (s->linesize >> 1)) + (mb_x * 8 + mx);
929 ptr = s->last_picture[1] + offset;
930 sub_pixels_2(s->block[4], ptr, s->linesize >> 1, dxy);
931 ptr = s->last_picture[2] + offset;
932 sub_pixels_2(s->block[5], ptr, s->linesize >> 1, dxy);
937 if (s->h263_msmpeg4) {
939 } else if (s->h263_pred) {
942 /* default quantization values */
949 if (av_fdct == jpeg_fdct_ifast)
950 last_index = dct_quantize(s, s->block[i], i, s->qscale);
952 last_index = dct_quantize_mmx(s, s->block[i], i, s->qscale);
953 s->block_last_index[i] = last_index;
957 switch(s->out_format) {
959 mpeg1_encode_mb(s, s->block, motion_x, motion_y);
963 msmpeg4_encode_mb(s, s->block, motion_x, motion_y);
965 h263_encode_mb(s, s->block, motion_x, motion_y);
968 mjpeg_encode_mb(s, s->block);
972 /* decompress blocks so that we keep the state of the decoder */
973 s->mv[0][0][0] = motion_x;
974 s->mv[0][0][1] = motion_y;
976 MPV_decode_mb(s, s->block);
981 static int dct_quantize(MpegEncContext *s,
982 DCTELEM *block, int n,
985 int i, j, level, last_non_zero, q;
997 /* note: block[0] is assumed to be positive */
998 block[0] = (block[0] + (q >> 1)) / q;
1001 if (s->out_format == FMT_H263) {
1002 qmat = s->q_non_intra_matrix;
1004 qmat = s->q_intra_matrix;
1009 qmat = s->q_non_intra_matrix;
1013 j = zigzag_direct[i];
1015 level = level * qmat[j];
1018 static int count = 0;
1019 int level1, level2, qmat1;
1021 if (qmat == s->q_non_intra_matrix) {
1022 qmat1 = default_non_intra_matrix[j] * s->qscale;
1024 qmat1 = default_intra_matrix[j] * s->qscale;
1026 if (av_fdct != jpeg_fdct_ifast)
1027 val = ((double)block[j] * 8.0) / (double)qmat1;
1029 val = ((double)block[j] * 8.0 * 2048.0) /
1030 ((double)qmat1 * aanscales[j]);
1032 level2 = level / (1 << (QMAT_SHIFT - 3));
1033 if (level1 != level2) {
1034 fprintf(stderr, "%d: quant error qlevel=%d wanted=%d level=%d qmat1=%d qmat=%d wantedf=%0.6f\n",
1035 count, level2, level1, block[j], qmat1, qmat[j],
1042 /* XXX: slight error for the low range. Test should be equivalent to
1043 (level <= -(1 << (QMAT_SHIFT - 3)) || level >= (1 <<
1046 if (((level << (31 - (QMAT_SHIFT - 3))) >> (31 - (QMAT_SHIFT - 3))) !=
1048 level = level / (1 << (QMAT_SHIFT - 3));
1049 /* XXX: currently, this code is not optimal. the range should be:
1057 else if (level < -128)
1065 return last_non_zero;
1068 static int dct_quantize_mmx(MpegEncContext *s,
1069 DCTELEM *block, int n,
1072 int i, j, level, last_non_zero, q;
1083 /* note: block[0] is assumed to be positive */
1084 block[0] = (block[0] + (q >> 1)) / q;
1087 if (s->out_format == FMT_H263) {
1088 qmat = s->q_non_intra_matrix;
1090 qmat = s->q_intra_matrix;
1095 qmat = s->q_non_intra_matrix;
1099 j = zigzag_direct[i];
1101 level = level * qmat[j];
1102 /* XXX: slight error for the low range. Test should be equivalent to
1103 (level <= -(1 << (QMAT_SHIFT_MMX - 3)) || level >= (1 <<
1104 (QMAT_SHIFT_MMX - 3)))
1106 if (((level << (31 - (QMAT_SHIFT_MMX - 3))) >> (31 - (QMAT_SHIFT_MMX - 3))) !=
1108 level = level / (1 << (QMAT_SHIFT_MMX - 3));
1109 /* XXX: currently, this code is not optimal. the range should be:
1117 else if (level < -128)
1125 return last_non_zero;
1128 static void dct_unquantize_mpeg1_c(MpegEncContext *s,
1129 DCTELEM *block, int n, int qscale)
1132 const UINT16 *quant_matrix;
1136 block[0] = block[0] * s->y_dc_scale;
1138 block[0] = block[0] * s->c_dc_scale;
1139 /* XXX: only mpeg1 */
1140 quant_matrix = s->intra_matrix;
1146 level = (int)(level * qscale * quant_matrix[i]) >> 3;
1147 level = (level - 1) | 1;
1150 level = (int)(level * qscale * quant_matrix[i]) >> 3;
1151 level = (level - 1) | 1;
1154 if (level < -2048 || level > 2047)
1155 fprintf(stderr, "unquant error %d %d\n", i, level);
1162 quant_matrix = s->non_intra_matrix;
1168 level = (((level << 1) + 1) * qscale *
1169 ((int) (quant_matrix[i]))) >> 4;
1170 level = (level - 1) | 1;
1173 level = (((level << 1) + 1) * qscale *
1174 ((int) (quant_matrix[i]))) >> 4;
1175 level = (level - 1) | 1;
1178 if (level < -2048 || level > 2047)
1179 fprintf(stderr, "unquant error %d %d\n", i, level);
1187 static void dct_unquantize_h263_c(MpegEncContext *s,
1188 DCTELEM *block, int n, int qscale)
1190 int i, level, qmul, qadd;
1194 block[0] = block[0] * s->y_dc_scale;
1196 block[0] = block[0] * s->c_dc_scale;
1202 qmul = s->qscale << 1;
1203 qadd = (s->qscale - 1) | 1;
1209 level = level * qmul - qadd;
1211 level = level * qmul + qadd;
1214 if (level < -2048 || level > 2047)
1215 fprintf(stderr, "unquant error %d %d\n", i, level);
1224 /* an I frame is I_FRAME_SIZE_RATIO bigger than a P frame */
1225 #define I_FRAME_SIZE_RATIO 3.0
1228 static void rate_control_init(MpegEncContext *s)
1232 if (s->intra_only) {
1233 s->I_frame_bits = ((INT64)s->bit_rate * FRAME_RATE_BASE) / s->frame_rate;
1234 s->P_frame_bits = s->I_frame_bits;
1236 s->P_frame_bits = (int) ((float)(s->gop_size * s->bit_rate) /
1237 (float)((float)s->frame_rate / FRAME_RATE_BASE * (I_FRAME_SIZE_RATIO + s->gop_size - 1)));
1238 s->I_frame_bits = (int)(s->P_frame_bits * I_FRAME_SIZE_RATIO);
1242 printf("I_frame_size=%d P_frame_size=%d\n",
1243 s->I_frame_bits, s->P_frame_bits);
1249 * This heuristic is rather poor, but at least we do not have to
1250 * change the qscale at every macroblock.
1252 static int rate_estimate_qscale(MpegEncContext *s)
1254 long long total_bits = s->total_bits;
1256 int qscale, diff, qmin;
1258 if (s->pict_type == I_TYPE) {
1259 s->wanted_bits += s->I_frame_bits;
1261 s->wanted_bits += s->P_frame_bits;
1263 diff = s->wanted_bits - total_bits;
1264 q = 31.0 - (float)diff / (QSCALE_K * s->mb_height * s->mb_width);
1265 /* adjust for I frame */
1266 if (s->pict_type == I_TYPE && !s->intra_only) {
1267 q /= I_FRAME_SIZE_RATIO;
1270 /* using a too small Q scale leeds to problems in mpeg1 and h263
1271 because AC coefficients are clamped to 255 or 127 */
1277 qscale = (int)(q + 0.5);
1279 printf("%d: total=%Ld br=%0.1f diff=%d qest=%0.1f\n",
1282 (float)s->frame_rate / FRAME_RATE_BASE *
1283 total_bits / s->picture_number,
1289 AVCodec mpeg1video_encoder = {
1292 CODEC_ID_MPEG1VIDEO,
1293 sizeof(MpegEncContext),
1299 AVCodec h263_encoder = {
1303 sizeof(MpegEncContext),
1309 AVCodec h263p_encoder = {
1313 sizeof(MpegEncContext),
1319 AVCodec rv10_encoder = {
1323 sizeof(MpegEncContext),
1329 AVCodec mjpeg_encoder = {
1333 sizeof(MpegEncContext),
1339 AVCodec opendivx_encoder = {
1343 sizeof(MpegEncContext),
1349 AVCodec msmpeg4_encoder = {
1353 sizeof(MpegEncContext),