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 void draw_edges_c(UINT8 *buf, int wrap, int width, int height, int w);
39 static int dct_quantize_c(MpegEncContext *s, DCTELEM *block, int n, int qscale);
41 int (*dct_quantize)(MpegEncContext *s, DCTELEM *block, int n, int qscale)= dct_quantize_c;
42 void (*draw_edges)(UINT8 *buf, int wrap, int width, int height, int w)= draw_edges_c;
46 /* enable all paranoid tests for rounding, overflows, etc... */
51 /* for jpeg fast DCT */
54 static const unsigned short aanscales[64] = {
55 /* precomputed values scaled up by 14 bits */
56 16384, 22725, 21407, 19266, 16384, 12873, 8867, 4520,
57 22725, 31521, 29692, 26722, 22725, 17855, 12299, 6270,
58 21407, 29692, 27969, 25172, 21407, 16819, 11585, 5906,
59 19266, 26722, 25172, 22654, 19266, 15137, 10426, 5315,
60 16384, 22725, 21407, 19266, 16384, 12873, 8867, 4520,
61 12873, 17855, 16819, 15137, 12873, 10114, 6967, 3552,
62 8867, 12299, 11585, 10426, 8867, 6967, 4799, 2446,
63 4520, 6270, 5906, 5315, 4520, 3552, 2446, 1247
66 static UINT8 h263_chroma_roundtab[16] = {
67 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2,
70 /* default motion estimation */
71 int motion_estimation_method = ME_LOG;
73 extern UINT8 zigzag_end[64];
75 static void convert_matrix(int *qmat, UINT16 *qmat16, 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 */
83 /* (1<<36)/19952 >= (1<<36)/(aanscales[i] * qscale * quant_matrix[i]) >= (1<<36)/249205026 */
84 /* 3444240 >= (1<<36)/(aanscales[i] * qscale * quant_matrix[i]) >= 275 */
86 qmat[block_permute_op(i)] = (int)((UINT64_C(1) << (QMAT_SHIFT + 11)) /
87 (aanscales[i] * qscale * quant_matrix[block_permute_op(i)]));
91 /* We can safely suppose that 16 <= quant_matrix[i] <= 255
92 So 16 <= qscale * quant_matrix[i] <= 7905
93 so (1<<19) / 16 >= (1<<19) / (qscale * quant_matrix[i]) >= (1<<19) / 7905
94 so 32768 >= (1<<19) / (qscale * quant_matrix[i]) >= 67
96 qmat[i] = (1 << QMAT_SHIFT_MMX) / (qscale * quant_matrix[i]);
97 qmat16[i] = (1 << QMAT_SHIFT_MMX) / (qscale * quant_matrix[block_permute_op(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->mb_num = s->mb_width * s->mb_height;
119 s->linesize = s->mb_width * 16 + 2 * EDGE_WIDTH;
122 int w, h, shift, pict_start;
125 h = s->mb_height * 16 + 2 * EDGE_WIDTH;
126 shift = (i == 0) ? 0 : 1;
127 c_size = (w >> shift) * (h >> shift);
128 pict_start = (w >> shift) * (EDGE_WIDTH >> shift) + (EDGE_WIDTH >> shift);
130 pict = av_mallocz(c_size);
133 s->last_picture_base[i] = pict;
134 s->last_picture[i] = pict + pict_start;
136 pict = av_mallocz(c_size);
139 s->next_picture_base[i] = pict;
140 s->next_picture[i] = pict + pict_start;
142 if (s->has_b_frames) {
143 pict = av_mallocz(c_size);
146 s->aux_picture_base[i] = pict;
147 s->aux_picture[i] = pict + pict_start;
152 /* Allocate MB type table */
153 s->mb_type = av_mallocz(s->mb_num * sizeof(char));
154 if (s->mb_type == NULL) {
159 s->mb_var = av_mallocz(s->mb_num * sizeof(INT16));
160 if (s->mb_var == NULL) {
164 /* Allocate MV table */
165 /* By now we just have one MV per MB */
166 s->mv_table[0] = av_mallocz(s->mb_num * sizeof(INT16));
167 s->mv_table[1] = av_mallocz(s->mb_num * sizeof(INT16));
168 if (s->mv_table[1] == NULL || s->mv_table[0] == NULL) {
174 if (s->out_format == FMT_H263) {
177 size = (2 * s->mb_width + 2) * (2 * s->mb_height + 2);
178 s->motion_val = malloc(size * 2 * sizeof(INT16));
179 if (s->motion_val == NULL)
181 memset(s->motion_val, 0, size * 2 * sizeof(INT16));
184 if (s->h263_pred || s->h263_plus) {
185 int y_size, c_size, i, size;
189 y_size = (2 * s->mb_width + 2) * (2 * s->mb_height + 2);
190 c_size = (s->mb_width + 2) * (s->mb_height + 2);
191 size = y_size + 2 * c_size;
192 s->dc_val[0] = malloc(size * sizeof(INT16));
193 if (s->dc_val[0] == NULL)
195 s->dc_val[1] = s->dc_val[0] + y_size;
196 s->dc_val[2] = s->dc_val[1] + c_size;
198 s->dc_val[0][i] = 1024;
201 s->ac_val[0] = av_mallocz(size * sizeof(INT16) * 16);
202 if (s->ac_val[0] == NULL)
204 s->ac_val[1] = s->ac_val[0] + y_size;
205 s->ac_val[2] = s->ac_val[1] + c_size;
208 s->coded_block = av_mallocz(y_size);
212 /* which mb is a intra block */
213 s->mbintra_table = av_mallocz(s->mb_num);
214 if (!s->mbintra_table)
216 memset(s->mbintra_table, 1, s->mb_num);
218 /* default structure is frame */
219 s->picture_structure = PICT_FRAME;
221 /* init macroblock skip table */
223 s->mbskip_table = av_mallocz(s->mb_num);
224 if (!s->mbskip_table)
228 s->context_initialized = 1;
235 /* init common structure for both encoder and decoder */
236 void MPV_common_end(MpegEncContext *s)
245 free(s->mv_table[0]);
247 free(s->mv_table[1]);
255 free(s->coded_block);
256 if (s->mbintra_table)
257 free(s->mbintra_table);
260 free(s->mbskip_table);
262 if (s->last_picture_base[i])
263 free(s->last_picture_base[i]);
264 if (s->next_picture_base[i])
265 free(s->next_picture_base[i]);
267 free(s->aux_picture_base[i]);
269 s->context_initialized = 0;
272 /* init video encoder */
273 int MPV_encode_init(AVCodecContext *avctx)
275 MpegEncContext *s = avctx->priv_data;
278 s->bit_rate = avctx->bit_rate;
279 s->frame_rate = avctx->frame_rate;
280 s->width = avctx->width;
281 s->height = avctx->height;
282 s->gop_size = avctx->gop_size;
283 s->rtp_mode = avctx->rtp_mode;
284 s->rtp_payload_size = avctx->rtp_payload_size;
285 if (avctx->rtp_callback)
286 s->rtp_callback = avctx->rtp_callback;
289 if (s->gop_size <= 1) {
295 s->full_search = motion_estimation_method;
297 s->fixed_qscale = (avctx->flags & CODEC_FLAG_QSCALE);
299 switch(avctx->codec->id) {
300 case CODEC_ID_MPEG1VIDEO:
301 s->out_format = FMT_MPEG1;
304 s->out_format = FMT_MJPEG;
305 s->intra_only = 1; /* force intra only for jpeg */
306 s->mjpeg_write_tables = 1; /* write all tables */
307 s->mjpeg_vsample[0] = 2; /* set up default sampling factors */
308 s->mjpeg_vsample[1] = 1; /* the only currently supported values */
309 s->mjpeg_vsample[2] = 1;
310 s->mjpeg_hsample[0] = 2;
311 s->mjpeg_hsample[1] = 1;
312 s->mjpeg_hsample[2] = 1;
313 if (mjpeg_init(s) < 0)
317 if (h263_get_picture_format(s->width, s->height) == 7) {
318 printf("Input picture size isn't suitable for h263 codec! try h263+\n");
321 s->out_format = FMT_H263;
324 s->out_format = FMT_H263;
326 s->rtp_payload_size = 1200;
328 s->unrestricted_mv = 1;
330 /* These are just to be sure */
335 s->out_format = FMT_H263;
339 s->out_format = FMT_H263;
341 s->unrestricted_mv = 1;
343 case CODEC_ID_MSMPEG4:
344 s->out_format = FMT_H263;
347 s->unrestricted_mv = 1;
353 if (s->out_format == FMT_H263)
354 h263_encode_init_vlc(s);
359 if (MPV_common_init(s) < 0)
362 /* init default q matrix */
364 s->intra_matrix[i] = default_intra_matrix[i];
365 s->non_intra_matrix[i] = default_non_intra_matrix[i];
368 /* rate control init */
369 rate_control_init(s);
371 s->picture_number = 0;
372 s->fake_picture_number = 0;
373 /* motion detector init */
379 int MPV_encode_end(AVCodecContext *avctx)
381 MpegEncContext *s = avctx->priv_data;
387 if (s->out_format == FMT_MJPEG)
393 /* draw the edges of width 'w' of an image of size width, height */
394 static void draw_edges_c(UINT8 *buf, int wrap, int width, int height, int w)
396 UINT8 *ptr, *last_line;
399 last_line = buf + (height - 1) * wrap;
402 memcpy(buf - (i + 1) * wrap, buf, width);
403 memcpy(last_line + (i + 1) * wrap, last_line, width);
407 for(i=0;i<height;i++) {
408 memset(ptr - w, ptr[0], w);
409 memset(ptr + width, ptr[width-1], w);
414 memset(buf - (i + 1) * wrap - w, buf[0], w); /* top left */
415 memset(buf - (i + 1) * wrap + width, buf[width-1], w); /* top right */
416 memset(last_line + (i + 1) * wrap - w, last_line[0], w); /* top left */
417 memset(last_line + (i + 1) * wrap + width, last_line[width-1], w); /* top right */
421 /* generic function for encode/decode called before a frame is coded/decoded */
422 void MPV_frame_start(MpegEncContext *s)
428 if (s->pict_type == B_TYPE) {
430 s->current_picture[i] = s->aux_picture[i];
434 /* swap next and last */
435 tmp = s->last_picture[i];
436 s->last_picture[i] = s->next_picture[i];
437 s->next_picture[i] = tmp;
438 s->current_picture[i] = tmp;
443 /* generic function for encode/decode called after a frame has been coded/decoded */
444 void MPV_frame_end(MpegEncContext *s)
446 /* draw edge for correct motion prediction if outside */
447 if (s->pict_type != B_TYPE && !s->intra_only) {
448 if(s->avctx==NULL || s->avctx->codec->id!=CODEC_ID_MPEG4){
449 draw_edges(s->current_picture[0], s->linesize, s->mb_width*16, s->mb_height*16, EDGE_WIDTH);
450 draw_edges(s->current_picture[1], s->linesize/2, s->mb_width*8, s->mb_height*8, EDGE_WIDTH/2);
451 draw_edges(s->current_picture[2], s->linesize/2, s->mb_width*8, s->mb_height*8, EDGE_WIDTH/2);
453 /* OpenDivx, but i dunno how to distinguish it from mpeg4 */
454 draw_edges(s->current_picture[0], s->linesize, s->width, s->height, EDGE_WIDTH);
455 draw_edges(s->current_picture[1], s->linesize/2, s->width/2, s->height/2, EDGE_WIDTH/2);
456 draw_edges(s->current_picture[2], s->linesize/2, s->width/2, s->height/2, EDGE_WIDTH/2);
462 int MPV_encode_picture(AVCodecContext *avctx,
463 unsigned char *buf, int buf_size, void *data)
465 MpegEncContext *s = avctx->priv_data;
466 AVPicture *pict = data;
470 s->qscale = avctx->quality;
472 init_put_bits(&s->pb, buf, buf_size, NULL, NULL);
474 if (!s->intra_only) {
475 /* first picture of GOP is intra */
476 if ((s->picture_number % s->gop_size) == 0)
477 s->pict_type = I_TYPE;
479 s->pict_type = P_TYPE;
481 s->pict_type = I_TYPE;
483 avctx->key_frame = (s->pict_type == I_TYPE);
488 UINT8 *src = pict->data[i];
489 UINT8 *dest = s->current_picture[i];
490 int src_wrap = pict->linesize[i];
491 int dest_wrap = s->linesize;
501 if(dest_wrap==src_wrap){
502 s->new_picture[i] = pict->data[i];
505 memcpy(dest, src, w);
509 s->new_picture[i] = s->current_picture[i];
513 encode_picture(s, s->picture_number);
518 if (s->out_format == FMT_MJPEG)
519 mjpeg_picture_trailer(s);
521 flush_put_bits(&s->pb);
522 s->total_bits += (pbBufPtr(&s->pb) - s->pb.buf) * 8;
524 avctx->quality = s->qscale;
525 return pbBufPtr(&s->pb) - s->pb.buf;
528 static inline int clip(int a, int amin, int amax)
538 /* apply one mpeg motion vector to the three components */
539 static inline void mpeg_motion(MpegEncContext *s,
540 UINT8 *dest_y, UINT8 *dest_cb, UINT8 *dest_cr,
542 UINT8 **ref_picture, int src_offset,
543 int field_based, op_pixels_func *pix_op,
544 int motion_x, int motion_y, int h)
547 int dxy, offset, mx, my, src_x, src_y, height, linesize;
549 dxy = ((motion_y & 1) << 1) | (motion_x & 1);
550 src_x = s->mb_x * 16 + (motion_x >> 1);
551 src_y = s->mb_y * (16 >> field_based) + (motion_y >> 1);
553 /* WARNING: do no forget half pels */
554 height = s->height >> field_based;
555 src_x = clip(src_x, -16, s->width);
556 if (src_x == s->width)
558 src_y = clip(src_y, -16, height);
561 linesize = s->linesize << field_based;
562 ptr = ref_picture[0] + (src_y * linesize) + (src_x) + src_offset;
563 dest_y += dest_offset;
564 pix_op[dxy](dest_y, ptr, linesize, h);
565 pix_op[dxy](dest_y + 8, ptr + 8, linesize, h);
567 if (s->out_format == FMT_H263) {
569 if ((motion_x & 3) != 0)
571 if ((motion_y & 3) != 0)
578 dxy = ((my & 1) << 1) | (mx & 1);
583 src_x = s->mb_x * 8 + mx;
584 src_y = s->mb_y * (8 >> field_based) + my;
585 src_x = clip(src_x, -8, s->width >> 1);
586 if (src_x == (s->width >> 1))
588 src_y = clip(src_y, -8, height >> 1);
589 if (src_y == (height >> 1))
592 offset = (src_y * (linesize >> 1)) + src_x + (src_offset >> 1);
593 ptr = ref_picture[1] + offset;
594 pix_op[dxy](dest_cb + (dest_offset >> 1), ptr, linesize >> 1, h >> 1);
595 ptr = ref_picture[2] + offset;
596 pix_op[dxy](dest_cr + (dest_offset >> 1), ptr, linesize >> 1, h >> 1);
599 static inline void MPV_motion(MpegEncContext *s,
600 UINT8 *dest_y, UINT8 *dest_cb, UINT8 *dest_cr,
601 int dir, UINT8 **ref_picture,
602 op_pixels_func *pix_op)
604 int dxy, offset, mx, my, src_x, src_y, motion_x, motion_y;
613 mpeg_motion(s, dest_y, dest_cb, dest_cr, 0,
616 s->mv[dir][0][0], s->mv[dir][0][1], 16);
620 motion_x = s->mv[dir][i][0];
621 motion_y = s->mv[dir][i][1];
623 dxy = ((motion_y & 1) << 1) | (motion_x & 1);
624 src_x = mb_x * 16 + (motion_x >> 1) + (i & 1) * 8;
625 src_y = mb_y * 16 + (motion_y >> 1) + ((i >> 1) & 1) * 8;
627 /* WARNING: do no forget half pels */
628 src_x = clip(src_x, -16, s->width);
629 if (src_x == s->width)
631 src_y = clip(src_y, -16, s->height);
632 if (src_y == s->height)
635 ptr = ref_picture[0] + (src_y * s->linesize) + (src_x);
636 dest = dest_y + ((i & 1) * 8) + (i >> 1) * 8 * s->linesize;
637 pix_op[dxy](dest, ptr, s->linesize, 8);
639 /* In case of 8X8, we construct a single chroma motion vector
640 with a special rounding */
644 mx += s->mv[dir][i][0];
645 my += s->mv[dir][i][1];
648 mx = (h263_chroma_roundtab[mx & 0xf] + ((mx >> 3) & ~1));
651 mx = -(h263_chroma_roundtab[mx & 0xf] + ((mx >> 3) & ~1));
654 my = (h263_chroma_roundtab[my & 0xf] + ((my >> 3) & ~1));
657 my = -(h263_chroma_roundtab[my & 0xf] + ((my >> 3) & ~1));
659 dxy = ((my & 1) << 1) | (mx & 1);
663 src_x = mb_x * 8 + mx;
664 src_y = mb_y * 8 + my;
665 src_x = clip(src_x, -8, s->width/2);
666 if (src_x == s->width/2)
668 src_y = clip(src_y, -8, s->height/2);
669 if (src_y == s->height/2)
672 offset = (src_y * (s->linesize >> 1)) + src_x;
673 ptr = ref_picture[1] + offset;
674 pix_op[dxy](dest_cb, ptr, s->linesize >> 1, 8);
675 ptr = ref_picture[2] + offset;
676 pix_op[dxy](dest_cr, ptr, s->linesize >> 1, 8);
679 if (s->picture_structure == PICT_FRAME) {
681 mpeg_motion(s, dest_y, dest_cb, dest_cr, 0,
682 ref_picture, s->field_select[dir][0] ? s->linesize : 0,
684 s->mv[dir][0][0], s->mv[dir][0][1], 8);
686 mpeg_motion(s, dest_y, dest_cb, dest_cr, s->linesize,
687 ref_picture, s->field_select[dir][1] ? s->linesize : 0,
689 s->mv[dir][1][0], s->mv[dir][1][1], 8);
699 /* put block[] to dest[] */
700 static inline void put_dct(MpegEncContext *s,
701 DCTELEM *block, int i, UINT8 *dest, int line_size)
704 s->dct_unquantize(s, block, i, s->qscale);
706 put_pixels_clamped(block, dest, line_size);
709 /* add block[] to dest[] */
710 static inline void add_dct(MpegEncContext *s,
711 DCTELEM *block, int i, UINT8 *dest, int line_size)
713 if (s->block_last_index[i] >= 0) {
715 if(s->encoding || (!s->h263_msmpeg4))
716 s->dct_unquantize(s, block, i, s->qscale);
718 add_pixels_clamped(block, dest, line_size);
722 /* generic function called after a macroblock has been parsed by the
723 decoder or after it has been encoded by the encoder.
725 Important variables used:
726 s->mb_intra : true if intra macroblock
727 s->mv_dir : motion vector direction
728 s->mv_type : motion vector type
729 s->mv : motion vector
730 s->interlaced_dct : true if interlaced dct used (mpeg2)
732 void MPV_decode_mb(MpegEncContext *s, DCTELEM block[6][64])
735 int dct_linesize, dct_offset;
736 op_pixels_func *op_pix;
741 #ifdef FF_POSTPROCESS
742 quant_store[mb_y][mb_x]=s->qscale;
743 //printf("[%02d][%02d] %d\n",mb_x,mb_y,s->qscale);
746 /* update DC predictors for P macroblocks */
749 if(s->mbintra_table[mb_x + mb_y*s->mb_width])
752 s->mbintra_table[mb_x + mb_y*s->mb_width]=0;
753 wrap = 2 * s->mb_width + 2;
754 xy = 2 * mb_x + 1 + (2 * mb_y + 1) * wrap;
757 s->dc_val[0][xy] = v;
758 s->dc_val[0][xy + 1] = v;
759 s->dc_val[0][xy + wrap] = v;
760 s->dc_val[0][xy + 1 + wrap] = v;
762 memset(s->ac_val[0][xy], 0, 16 * sizeof(INT16));
763 memset(s->ac_val[0][xy + 1], 0, 16 * sizeof(INT16));
764 memset(s->ac_val[0][xy + wrap], 0, 16 * sizeof(INT16));
765 memset(s->ac_val[0][xy + 1 + wrap], 0, 16 * sizeof(INT16));
766 if (s->h263_msmpeg4) {
767 s->coded_block[xy] = 0;
768 s->coded_block[xy + 1] = 0;
769 s->coded_block[xy + wrap] = 0;
770 s->coded_block[xy + 1 + wrap] = 0;
773 wrap = s->mb_width + 2;
774 xy = mb_x + 1 + (mb_y + 1) * wrap;
775 s->dc_val[1][xy] = v;
776 s->dc_val[2][xy] = v;
778 memset(s->ac_val[1][xy], 0, 16 * sizeof(INT16));
779 memset(s->ac_val[2][xy], 0, 16 * sizeof(INT16));
782 s->last_dc[0] = 128 << s->intra_dc_precision;
783 s->last_dc[1] = 128 << s->intra_dc_precision;
784 s->last_dc[2] = 128 << s->intra_dc_precision;
787 else if (s->h263_pred)
788 s->mbintra_table[mb_x + mb_y*s->mb_width]=1;
790 /* update motion predictor */
791 if (s->out_format == FMT_H263) {
792 int xy, wrap, motion_x, motion_y;
794 wrap = 2 * s->mb_width + 2;
795 xy = 2 * mb_x + 1 + (2 * mb_y + 1) * wrap;
800 } else if (s->mv_type == MV_TYPE_16X16) {
801 motion_x = s->mv[0][0][0];
802 motion_y = s->mv[0][0][1];
804 /* no update if 8X8 because it has been done during parsing */
805 s->motion_val[xy][0] = motion_x;
806 s->motion_val[xy][1] = motion_y;
807 s->motion_val[xy + 1][0] = motion_x;
808 s->motion_val[xy + 1][1] = motion_y;
809 s->motion_val[xy + wrap][0] = motion_x;
810 s->motion_val[xy + wrap][1] = motion_y;
811 s->motion_val[xy + 1 + wrap][0] = motion_x;
812 s->motion_val[xy + 1 + wrap][1] = motion_y;
816 if (!s->intra_only) {
817 UINT8 *dest_y, *dest_cb, *dest_cr;
820 /* avoid copy if macroblock skipped in last frame too */
821 if (!s->encoding && s->pict_type != B_TYPE) {
822 mbskip_ptr = &s->mbskip_table[s->mb_y * s->mb_width + s->mb_x];
825 /* if previous was skipped too, then nothing to do ! */
826 if (*mbskip_ptr != 0)
828 *mbskip_ptr = 1; /* indicate that this time we skiped it */
830 *mbskip_ptr = 0; /* not skipped */
834 dest_y = s->current_picture[0] + (mb_y * 16 * s->linesize) + mb_x * 16;
835 dest_cb = s->current_picture[1] + (mb_y * 8 * (s->linesize >> 1)) + mb_x * 8;
836 dest_cr = s->current_picture[2] + (mb_y * 8 * (s->linesize >> 1)) + mb_x * 8;
838 if (s->interlaced_dct) {
839 dct_linesize = s->linesize * 2;
840 dct_offset = s->linesize;
842 dct_linesize = s->linesize;
843 dct_offset = s->linesize * 8;
847 /* motion handling */
849 op_pix = put_pixels_tab;
851 op_pix = put_no_rnd_pixels_tab;
853 if (s->mv_dir & MV_DIR_FORWARD) {
854 MPV_motion(s, dest_y, dest_cb, dest_cr, 0, s->last_picture, op_pix);
856 op_pix = avg_pixels_tab;
858 op_pix = avg_no_rnd_pixels_tab;
860 if (s->mv_dir & MV_DIR_BACKWARD) {
861 MPV_motion(s, dest_y, dest_cb, dest_cr, 1, s->next_picture, op_pix);
864 /* add dct residue */
865 add_dct(s, block[0], 0, dest_y, dct_linesize);
866 add_dct(s, block[1], 1, dest_y + 8, dct_linesize);
867 add_dct(s, block[2], 2, dest_y + dct_offset, dct_linesize);
868 add_dct(s, block[3], 3, dest_y + dct_offset + 8, dct_linesize);
870 add_dct(s, block[4], 4, dest_cb, s->linesize >> 1);
871 add_dct(s, block[5], 5, dest_cr, s->linesize >> 1);
873 /* dct only in intra block */
874 put_dct(s, block[0], 0, dest_y, dct_linesize);
875 put_dct(s, block[1], 1, dest_y + 8, dct_linesize);
876 put_dct(s, block[2], 2, dest_y + dct_offset, dct_linesize);
877 put_dct(s, block[3], 3, dest_y + dct_offset + 8, dct_linesize);
879 put_dct(s, block[4], 4, dest_cb, s->linesize >> 1);
880 put_dct(s, block[5], 5, dest_cr, s->linesize >> 1);
887 static void encode_picture(MpegEncContext *s, int picture_number)
889 int mb_x, mb_y, wrap, last_gob, pdif = 0;
891 int i, motion_x, motion_y;
893 s->picture_number = picture_number;
894 if (!s->fixed_qscale)
895 s->qscale = rate_estimate_qscale(s);
897 /* precompute matrix */
898 if (s->out_format == FMT_MJPEG) {
899 /* for mjpeg, we do include qscale in the matrix */
900 s->intra_matrix[0] = default_intra_matrix[0];
902 s->intra_matrix[i] = (default_intra_matrix[i] * s->qscale) >> 3;
903 convert_matrix(s->q_intra_matrix, s->q_intra_matrix16, s->intra_matrix, 8);
905 convert_matrix(s->q_intra_matrix, s->q_intra_matrix16, s->intra_matrix, s->qscale);
906 convert_matrix(s->q_non_intra_matrix, s->q_non_intra_matrix16, s->non_intra_matrix, s->qscale);
909 switch(s->out_format) {
911 mjpeg_picture_header(s);
915 msmpeg4_encode_picture_header(s, picture_number);
916 else if (s->h263_pred)
917 mpeg4_encode_picture_header(s, picture_number);
918 else if (s->h263_rv10)
919 rv10_encode_picture_header(s, picture_number);
921 h263_encode_picture_header(s, picture_number);
924 mpeg1_encode_picture_header(s, picture_number);
928 /* init last dc values */
929 /* note: quant matrix value (8) is implied here */
934 s->last_mv[0][0][0] = 0;
935 s->last_mv[0][0][1] = 0;
936 s->mv_type = MV_TYPE_16X16;
937 s->mv_dir = MV_DIR_FORWARD;
939 /* Get the GOB height based on picture height */
940 if (s->out_format == FMT_H263 && !s->h263_pred && !s->h263_msmpeg4) {
941 if (s->height <= 400)
943 else if (s->height <= 800)
949 /* Reset the average MB variance */
952 /* Estimate motion for every MB */
953 for(mb_y=0; mb_y < s->mb_height; mb_y++) {
954 for(mb_x=0; mb_x < s->mb_width; mb_x++) {
958 /* compute motion vector and macro block type (intra or non intra) */
961 if (s->pict_type == P_TYPE) {
962 s->mb_intra = estimate_motion(s, mb_x, mb_y,
968 /* Store MB type and MV */
969 s->mb_type[mb_y * s->mb_width + mb_x] = s->mb_intra;
970 s->mv_table[0][mb_y * s->mb_width + mb_x] = motion_x;
971 s->mv_table[1][mb_y * s->mb_width + mb_x] = motion_y;
975 s->avg_mb_var = s->avg_mb_var / s->mb_num;
977 for(mb_y=0; mb_y < s->mb_height; mb_y++) {
978 /* Put GOB header based on RTP MTU */
979 /* TODO: Put all this stuff in a separate generic function */
982 s->ptr_lastgob = s->pb.buf;
983 s->ptr_last_mb_line = s->pb.buf;
984 } else if (s->out_format == FMT_H263 && !s->h263_pred && !s->h263_msmpeg4 && !(mb_y % s->gob_index)) {
985 last_gob = h263_encode_gob_header(s, mb_y);
987 s->first_gob_line = 1;
992 for(mb_x=0; mb_x < s->mb_width; mb_x++) {
997 /* compute motion vector and macro block type (intra or non intra) */
1000 if (s->pict_type == P_TYPE) {
1001 s->mb_intra = estimate_motion(s, mb_x, mb_y,
1009 s->mb_intra = s->mb_type[mb_y * s->mb_width + mb_x];
1010 motion_x = s->mv_table[0][mb_y * s->mb_width + mb_x];
1011 motion_y = s->mv_table[1][mb_y * s->mb_width + mb_x];
1013 /* get the pixels */
1015 ptr = s->new_picture[0] + (mb_y * 16 * wrap) + mb_x * 16;
1016 get_pixels(s->block[0], ptr, wrap);
1017 get_pixels(s->block[1], ptr + 8, wrap);
1018 get_pixels(s->block[2], ptr + 8 * wrap, wrap);
1019 get_pixels(s->block[3], ptr + 8 * wrap + 8, wrap);
1020 wrap = s->linesize >> 1;
1021 ptr = s->new_picture[1] + (mb_y * 8 * wrap) + mb_x * 8;
1022 get_pixels(s->block[4], ptr, wrap);
1024 wrap = s->linesize >> 1;
1025 ptr = s->new_picture[2] + (mb_y * 8 * wrap) + mb_x * 8;
1026 get_pixels(s->block[5], ptr, wrap);
1028 /* subtract previous frame if non intra */
1030 int dxy, offset, mx, my;
1032 dxy = ((motion_y & 1) << 1) | (motion_x & 1);
1033 ptr = s->last_picture[0] +
1034 ((mb_y * 16 + (motion_y >> 1)) * s->linesize) +
1035 (mb_x * 16 + (motion_x >> 1));
1037 sub_pixels_2(s->block[0], ptr, s->linesize, dxy);
1038 sub_pixels_2(s->block[1], ptr + 8, s->linesize, dxy);
1039 sub_pixels_2(s->block[2], ptr + s->linesize * 8, s->linesize, dxy);
1040 sub_pixels_2(s->block[3], ptr + 8 + s->linesize * 8, s->linesize ,dxy);
1042 if (s->out_format == FMT_H263) {
1043 /* special rounding for h263 */
1045 if ((motion_x & 3) != 0)
1047 if ((motion_y & 3) != 0)
1054 dxy = ((my & 1) << 1) | (mx & 1);
1058 offset = ((mb_y * 8 + my) * (s->linesize >> 1)) + (mb_x * 8 + mx);
1059 ptr = s->last_picture[1] + offset;
1060 sub_pixels_2(s->block[4], ptr, s->linesize >> 1, dxy);
1061 ptr = s->last_picture[2] + offset;
1062 sub_pixels_2(s->block[5], ptr, s->linesize >> 1, dxy);
1070 adap_parm = ((s->avg_mb_var << 1) + s->mb_var[s->mb_width*mb_y+mb_x] + 1.0) /
1071 ((s->mb_var[s->mb_width*mb_y+mb_x] << 1) + s->avg_mb_var + 1.0);
1073 printf("\ntype=%c qscale=%2d adap=%0.2f dquant=%4.2f var=%4d avgvar=%4d",
1074 (s->mb_type[s->mb_width*mb_y+mb_x] > 0) ? 'I' : 'P',
1075 s->qscale, adap_parm, s->qscale*adap_parm,
1076 s->mb_var[s->mb_width*mb_y+mb_x], s->avg_mb_var);
1079 /* DCT & quantize */
1080 if (s->h263_msmpeg4) {
1081 msmpeg4_dc_scale(s);
1082 } else if (s->h263_pred) {
1085 /* default quantization values */
1090 s->block_last_index[i] = dct_quantize(s, s->block[i], i, s->qscale);
1093 /* huffman encode */
1094 switch(s->out_format) {
1096 mpeg1_encode_mb(s, s->block, motion_x, motion_y);
1099 if (s->h263_msmpeg4)
1100 msmpeg4_encode_mb(s, s->block, motion_x, motion_y);
1102 h263_encode_mb(s, s->block, motion_x, motion_y);
1105 mjpeg_encode_mb(s, s->block);
1109 /* decompress blocks so that we keep the state of the decoder */
1110 s->mv[0][0][0] = motion_x;
1111 s->mv[0][0][1] = motion_y;
1113 MPV_decode_mb(s, s->block);
1117 /* Obtain average GOB size for RTP */
1120 s->mb_line_avgsize = pbBufPtr(&s->pb) - s->ptr_last_mb_line;
1121 else if (!(mb_y % s->gob_index)) {
1122 s->mb_line_avgsize = (s->mb_line_avgsize + pbBufPtr(&s->pb) - s->ptr_last_mb_line) >> 1;
1123 s->ptr_last_mb_line = pbBufPtr(&s->pb);
1125 //fprintf(stderr, "\nMB line: %d\tSize: %u\tAvg. Size: %u", s->mb_y,
1126 // (s->pb.buf_ptr - s->ptr_last_mb_line), s->mb_line_avgsize);
1127 s->first_gob_line = 0;
1131 if (s->h263_msmpeg4)
1132 msmpeg4_encode_ext_header(s);
1134 //if (s->gob_number)
1135 // fprintf(stderr,"\nNumber of GOB: %d", s->gob_number);
1137 /* Send the last GOB if RTP */
1139 flush_put_bits(&s->pb);
1140 pdif = pbBufPtr(&s->pb) - s->ptr_lastgob;
1141 /* Call the RTP callback to send the last GOB */
1142 if (s->rtp_callback)
1143 s->rtp_callback(s->ptr_lastgob, pdif, s->gob_number);
1144 s->ptr_lastgob = pbBufPtr(&s->pb);
1145 //fprintf(stderr,"\nGOB: %2d size: %d (last)", s->gob_number, pdif);
1150 static int dct_quantize_c(MpegEncContext *s,
1151 DCTELEM *block, int n,
1154 int i, j, level, last_non_zero, q;
1156 int minLevel, maxLevel;
1158 if(s->avctx!=NULL && s->avctx->codec->id==CODEC_ID_MPEG4){
1162 }else if(s->out_format==FMT_MPEG1){
1166 }else if(s->out_format==FMT_MJPEG){
1171 /* h263 / msmpeg4 */
1178 /* we need this permutation so that we correct the IDCT
1179 permutation. will be moved into DCT code */
1180 block_permute(block);
1189 /* note: block[0] is assumed to be positive */
1190 block[0] = (block[0] + (q >> 1)) / q;
1193 if (s->out_format == FMT_H263) {
1194 qmat = s->q_non_intra_matrix;
1196 qmat = s->q_intra_matrix;
1201 qmat = s->q_non_intra_matrix;
1205 j = zigzag_direct[i];
1207 level = level * qmat[j];
1210 static int count = 0;
1211 int level1, level2, qmat1;
1213 if (qmat == s->q_non_intra_matrix) {
1214 qmat1 = default_non_intra_matrix[j] * s->qscale;
1216 qmat1 = default_intra_matrix[j] * s->qscale;
1218 if (av_fdct != jpeg_fdct_ifast)
1219 val = ((double)block[j] * 8.0) / (double)qmat1;
1221 val = ((double)block[j] * 8.0 * 2048.0) /
1222 ((double)qmat1 * aanscales[j]);
1224 level2 = level / (1 << (QMAT_SHIFT - 3));
1225 if (level1 != level2) {
1226 fprintf(stderr, "%d: quant error qlevel=%d wanted=%d level=%d qmat1=%d qmat=%d wantedf=%0.6f\n",
1227 count, level2, level1, block[j], qmat1, qmat[j],
1234 /* XXX: slight error for the low range. Test should be equivalent to
1235 (level <= -(1 << (QMAT_SHIFT - 3)) || level >= (1 <<
1238 if (((level << (31 - (QMAT_SHIFT - 3))) >> (31 - (QMAT_SHIFT - 3))) !=
1240 level = level / (1 << (QMAT_SHIFT - 3));
1241 /* XXX: currently, this code is not optimal. the range should be:
1247 if (level > maxLevel)
1249 else if (level < minLevel)
1258 return last_non_zero;
1261 static void dct_unquantize_mpeg1_c(MpegEncContext *s,
1262 DCTELEM *block, int n, int qscale)
1264 int i, level, nCoeffs;
1265 const UINT16 *quant_matrix;
1267 if(s->alternate_scan) nCoeffs= 64;
1268 else nCoeffs= s->block_last_index[n]+1;
1272 block[0] = block[0] * s->y_dc_scale;
1274 block[0] = block[0] * s->c_dc_scale;
1275 /* XXX: only mpeg1 */
1276 quant_matrix = s->intra_matrix;
1277 for(i=1;i<nCoeffs;i++) {
1278 int j= zigzag_direct[i];
1283 level = (int)(level * qscale * quant_matrix[j]) >> 3;
1284 level = (level - 1) | 1;
1287 level = (int)(level * qscale * quant_matrix[j]) >> 3;
1288 level = (level - 1) | 1;
1291 if (level < -2048 || level > 2047)
1292 fprintf(stderr, "unquant error %d %d\n", i, level);
1299 quant_matrix = s->non_intra_matrix;
1300 for(;i<nCoeffs;i++) {
1301 int j= zigzag_direct[i];
1306 level = (((level << 1) + 1) * qscale *
1307 ((int) (quant_matrix[j]))) >> 4;
1308 level = (level - 1) | 1;
1311 level = (((level << 1) + 1) * qscale *
1312 ((int) (quant_matrix[j]))) >> 4;
1313 level = (level - 1) | 1;
1316 if (level < -2048 || level > 2047)
1317 fprintf(stderr, "unquant error %d %d\n", i, level);
1325 static void dct_unquantize_h263_c(MpegEncContext *s,
1326 DCTELEM *block, int n, int qscale)
1328 int i, level, qmul, qadd;
1333 block[0] = block[0] * s->y_dc_scale;
1335 block[0] = block[0] * s->c_dc_scale;
1337 nCoeffs= 64; //does not allways use zigzag table
1340 nCoeffs= zigzag_end[ s->block_last_index[n] ];
1343 qmul = s->qscale << 1;
1344 if (s->h263_aic && s->mb_intra)
1347 qadd = (s->qscale - 1) | 1;
1349 for(;i<nCoeffs;i++) {
1353 level = level * qmul - qadd;
1355 level = level * qmul + qadd;
1358 if (level < -2048 || level > 2047)
1359 fprintf(stderr, "unquant error %d %d\n", i, level);
1368 /* an I frame is I_FRAME_SIZE_RATIO bigger than a P frame */
1369 #define I_FRAME_SIZE_RATIO 3.0
1372 static void rate_control_init(MpegEncContext *s)
1376 if (s->intra_only) {
1377 s->I_frame_bits = ((INT64)s->bit_rate * FRAME_RATE_BASE) / s->frame_rate;
1378 s->P_frame_bits = s->I_frame_bits;
1380 s->P_frame_bits = (int) ((float)(s->gop_size * s->bit_rate) /
1381 (float)((float)s->frame_rate / FRAME_RATE_BASE * (I_FRAME_SIZE_RATIO + s->gop_size - 1)));
1382 s->I_frame_bits = (int)(s->P_frame_bits * I_FRAME_SIZE_RATIO);
1386 printf("I_frame_size=%d P_frame_size=%d\n",
1387 s->I_frame_bits, s->P_frame_bits);
1393 * This heuristic is rather poor, but at least we do not have to
1394 * change the qscale at every macroblock.
1396 static int rate_estimate_qscale(MpegEncContext *s)
1398 INT64 diff, total_bits = s->total_bits;
1402 if (s->pict_type == I_TYPE) {
1403 s->wanted_bits += s->I_frame_bits;
1405 s->wanted_bits += s->P_frame_bits;
1407 diff = s->wanted_bits - total_bits;
1408 q = 31.0 - (float)diff / (QSCALE_K * s->mb_height * s->mb_width);
1409 /* adjust for I frame */
1410 if (s->pict_type == I_TYPE && !s->intra_only) {
1411 q /= I_FRAME_SIZE_RATIO;
1414 /* using a too small Q scale leeds to problems in mpeg1 and h263
1415 because AC coefficients are clamped to 255 or 127 */
1421 qscale = (int)(q + 0.5);
1423 printf("\n%d: total=%0.0f wanted=%0.0f br=%0.1f diff=%d qest=%2.1f\n",
1426 (double)s->wanted_bits,
1427 (float)s->frame_rate / FRAME_RATE_BASE *
1428 total_bits / s->picture_number,
1434 AVCodec mpeg1video_encoder = {
1437 CODEC_ID_MPEG1VIDEO,
1438 sizeof(MpegEncContext),
1444 AVCodec h263_encoder = {
1448 sizeof(MpegEncContext),
1454 AVCodec h263p_encoder = {
1458 sizeof(MpegEncContext),
1464 AVCodec rv10_encoder = {
1468 sizeof(MpegEncContext),
1474 AVCodec mjpeg_encoder = {
1478 sizeof(MpegEncContext),
1484 AVCodec mpeg4_encoder = {
1488 sizeof(MpegEncContext),
1494 AVCodec msmpeg4_encoder = {
1498 sizeof(MpegEncContext),