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.
19 * 4MV & hq & b-frame encoding stuff by Michael Niedermayer <michaelni@gmx.at>
27 #include "mpegvideo.h"
30 #include "fastmemcpy.h"
33 static void encode_picture(MpegEncContext *s, int picture_number);
34 static void dct_unquantize_mpeg1_c(MpegEncContext *s,
35 DCTELEM *block, int n, int qscale);
36 static void dct_unquantize_mpeg2_c(MpegEncContext *s,
37 DCTELEM *block, int n, int qscale);
38 static void dct_unquantize_h263_c(MpegEncContext *s,
39 DCTELEM *block, int n, int qscale);
40 static void draw_edges_c(UINT8 *buf, int wrap, int width, int height, int w);
41 static int dct_quantize_c(MpegEncContext *s, DCTELEM *block, int n, int qscale, int *overflow);
43 int (*dct_quantize)(MpegEncContext *s, DCTELEM *block, int n, int qscale, int *overflow)= dct_quantize_c;
44 void (*draw_edges)(UINT8 *buf, int wrap, int width, int height, int w)= draw_edges_c;
48 /* 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 static UINT16 default_mv_penalty[MAX_FCODE+1][MAX_MV*2+1];
74 static UINT8 default_fcode_tab[MAX_MV*2+1];
76 extern UINT8 zigzag_end[64];
78 /* default motion estimation */
79 int motion_estimation_method = ME_EPZS;
81 static void convert_matrix(int (*qmat)[64], uint16_t (*qmat16)[64], uint16_t (*qmat16_bias)[64],
82 const UINT16 *quant_matrix, int bias)
86 for(qscale=1; qscale<32; qscale++){
88 if (av_fdct == jpeg_fdct_ifast) {
90 const int j= block_permute_op(i);
91 /* 16 <= qscale * quant_matrix[i] <= 7905 */
92 /* 19952 <= aanscales[i] * qscale * quant_matrix[i] <= 249205026 */
93 /* (1<<36)/19952 >= (1<<36)/(aanscales[i] * qscale * quant_matrix[i]) >= (1<<36)/249205026 */
94 /* 3444240 >= (1<<36)/(aanscales[i] * qscale * quant_matrix[i]) >= 275 */
96 qmat[qscale][j] = (int)((UINT64_C(1) << (QMAT_SHIFT + 11)) /
97 (aanscales[i] * qscale * quant_matrix[j]));
101 /* We can safely suppose that 16 <= quant_matrix[i] <= 255
102 So 16 <= qscale * quant_matrix[i] <= 7905
103 so (1<<19) / 16 >= (1<<19) / (qscale * quant_matrix[i]) >= (1<<19) / 7905
104 so 32768 >= (1<<19) / (qscale * quant_matrix[i]) >= 67
106 qmat [qscale][i] = (1 << QMAT_SHIFT_MMX) / (qscale * quant_matrix[i]);
107 qmat16[qscale][i] = (1 << QMAT_SHIFT_MMX) / (qscale * quant_matrix[block_permute_op(i)]);
109 if(qmat16[qscale][i]==0 || qmat16[qscale][i]==128*256) qmat16[qscale][i]=128*256-1;
111 qmat16_bias[qscale][i]= ROUNDED_DIV(bias<<(16-QUANT_BIAS_SHIFT), qmat16[qscale][i]);
117 /* init common structure for both encoder and decoder */
118 int MPV_common_init(MpegEncContext *s)
123 s->dct_unquantize_h263 = dct_unquantize_h263_c;
124 s->dct_unquantize_mpeg1 = dct_unquantize_mpeg1_c;
125 s->dct_unquantize_mpeg2 = dct_unquantize_mpeg2_c;
128 MPV_common_init_mmx(s);
130 //setup default unquantizers (mpeg4 might change it later)
131 if(s->out_format == FMT_H263)
132 s->dct_unquantize = s->dct_unquantize_h263;
134 s->dct_unquantize = s->dct_unquantize_mpeg1;
136 s->mb_width = (s->width + 15) / 16;
137 s->mb_height = (s->height + 15) / 16;
138 s->mb_num = s->mb_width * s->mb_height;
139 s->linesize = s->mb_width * 16 + 2 * EDGE_WIDTH;
142 int w, h, shift, pict_start;
145 h = s->mb_height * 16 + 2 * EDGE_WIDTH;
146 shift = (i == 0) ? 0 : 1;
147 c_size = (w >> shift) * (h >> shift);
148 pict_start = (w >> shift) * (EDGE_WIDTH >> shift) + (EDGE_WIDTH >> shift);
150 pict = av_mallocz(c_size);
153 s->last_picture_base[i] = pict;
154 s->last_picture[i] = pict + pict_start;
156 pict = av_mallocz(c_size);
159 s->next_picture_base[i] = pict;
160 s->next_picture[i] = pict + pict_start;
162 if (s->has_b_frames) {
163 pict = av_mallocz(c_size);
166 s->aux_picture_base[i] = pict;
167 s->aux_picture[i] = pict + pict_start;
173 int mv_table_size= (s->mb_width+2)*(s->mb_height+2);
175 /* Allocate MB type table */
176 s->mb_type = av_mallocz(s->mb_num * sizeof(char));
177 if (s->mb_type == NULL) {
182 s->mb_var = av_mallocz(s->mb_num * sizeof(INT16));
183 if (s->mb_var == NULL) {
188 /* Allocate MV tables */
189 s->p_mv_table = av_mallocz(mv_table_size * 2 * sizeof(INT16));
190 if (s->p_mv_table == NULL) {
194 s->last_p_mv_table = av_mallocz(mv_table_size * 2 * sizeof(INT16));
195 if (s->last_p_mv_table == NULL) {
199 s->b_forw_mv_table = av_mallocz(mv_table_size * 2 * sizeof(INT16));
200 if (s->b_forw_mv_table == NULL) {
204 s->b_back_mv_table = av_mallocz(mv_table_size * 2 * sizeof(INT16));
205 if (s->b_back_mv_table == NULL) {
209 s->b_bidir_forw_mv_table = av_mallocz(mv_table_size * 2 * sizeof(INT16));
210 if (s->b_bidir_forw_mv_table == NULL) {
214 s->b_bidir_back_mv_table = av_mallocz(mv_table_size * 2 * sizeof(INT16));
215 if (s->b_bidir_back_mv_table == NULL) {
219 s->b_direct_forw_mv_table = av_mallocz(mv_table_size * 2 * sizeof(INT16));
220 if (s->b_direct_forw_mv_table == NULL) {
224 s->b_direct_back_mv_table = av_mallocz(mv_table_size * 2 * sizeof(INT16));
225 if (s->b_direct_back_mv_table == NULL) {
229 s->b_direct_mv_table = av_mallocz(mv_table_size * 2 * sizeof(INT16));
230 if (s->b_direct_mv_table == NULL) {
235 s->me_scratchpad = av_mallocz( s->linesize*16*3*sizeof(uint8_t));
236 if (s->me_scratchpad == NULL) {
242 for(j=0; j<REORDER_BUFFER_SIZE; j++){
248 h = s->mb_height * 16;
249 shift = (i == 0) ? 0 : 1;
250 c_size = (w >> shift) * (h >> shift);
252 pict = av_mallocz(c_size);
255 s->picture_buffer[j][i] = pict;
261 if (s->out_format == FMT_H263 || s->encoding) {
264 size = (2 * s->mb_width + 2) * (2 * s->mb_height + 2);
265 s->motion_val = malloc(size * 2 * sizeof(INT16));
266 if (s->motion_val == NULL)
268 memset(s->motion_val, 0, size * 2 * sizeof(INT16));
271 if (s->h263_pred || s->h263_plus) {
272 int y_size, c_size, i, size;
276 y_size = (2 * s->mb_width + 2) * (2 * s->mb_height + 2);
277 c_size = (s->mb_width + 2) * (s->mb_height + 2);
278 size = y_size + 2 * c_size;
279 s->dc_val[0] = malloc(size * sizeof(INT16));
280 if (s->dc_val[0] == NULL)
282 s->dc_val[1] = s->dc_val[0] + y_size;
283 s->dc_val[2] = s->dc_val[1] + c_size;
285 s->dc_val[0][i] = 1024;
288 s->ac_val[0] = av_mallocz(size * sizeof(INT16) * 16);
289 if (s->ac_val[0] == NULL)
291 s->ac_val[1] = s->ac_val[0] + y_size;
292 s->ac_val[2] = s->ac_val[1] + c_size;
295 s->coded_block = av_mallocz(y_size);
299 /* which mb is a intra block */
300 s->mbintra_table = av_mallocz(s->mb_num);
301 if (!s->mbintra_table)
303 memset(s->mbintra_table, 1, s->mb_num);
305 /* divx501 bitstream reorder buffer */
306 s->bitstream_buffer= av_mallocz(BITSTREAM_BUFFER_SIZE);
307 if (!s->bitstream_buffer)
310 /* default structure is frame */
311 s->picture_structure = PICT_FRAME;
313 /* init macroblock skip table */
314 s->mbskip_table = av_mallocz(s->mb_num);
315 if (!s->mbskip_table)
318 s->block= s->blocks[0];
320 s->context_initialized = 1;
327 #define CHECK_FREE(p)\
333 /* init common structure for both encoder and decoder */
334 void MPV_common_end(MpegEncContext *s)
338 CHECK_FREE(s->mb_type);
339 CHECK_FREE(s->mb_var);
340 CHECK_FREE(s->p_mv_table);
341 CHECK_FREE(s->last_p_mv_table);
342 CHECK_FREE(s->b_forw_mv_table);
343 CHECK_FREE(s->b_back_mv_table);
344 CHECK_FREE(s->b_bidir_forw_mv_table);
345 CHECK_FREE(s->b_bidir_back_mv_table);
346 CHECK_FREE(s->b_direct_forw_mv_table);
347 CHECK_FREE(s->b_direct_back_mv_table);
348 CHECK_FREE(s->b_direct_mv_table);
349 CHECK_FREE(s->motion_val);
350 CHECK_FREE(s->dc_val[0]);
351 CHECK_FREE(s->ac_val[0]);
352 CHECK_FREE(s->coded_block);
353 CHECK_FREE(s->mbintra_table);
354 CHECK_FREE(s->me_scratchpad);
356 CHECK_FREE(s->mbskip_table);
357 CHECK_FREE(s->bitstream_buffer);
360 CHECK_FREE(s->last_picture_base[i]);
361 CHECK_FREE(s->next_picture_base[i]);
362 CHECK_FREE(s->aux_picture_base[i]);
363 for(j=0; j<REORDER_BUFFER_SIZE; j++){
364 CHECK_FREE(s->picture_buffer[j][i]);
367 s->context_initialized = 0;
370 /* init video encoder */
371 int MPV_encode_init(AVCodecContext *avctx)
373 MpegEncContext *s = avctx->priv_data;
376 avctx->pix_fmt = PIX_FMT_YUV420P;
378 s->bit_rate = avctx->bit_rate;
379 s->bit_rate_tolerance = avctx->bit_rate_tolerance;
380 s->frame_rate = avctx->frame_rate;
381 s->width = avctx->width;
382 s->height = avctx->height;
383 s->gop_size = avctx->gop_size;
384 s->rtp_mode = avctx->rtp_mode;
385 s->rtp_payload_size = avctx->rtp_payload_size;
386 if (avctx->rtp_callback)
387 s->rtp_callback = avctx->rtp_callback;
388 s->qmin= avctx->qmin;
389 s->qmax= avctx->qmax;
390 s->max_qdiff= avctx->max_qdiff;
391 s->qcompress= avctx->qcompress;
392 s->qblur= avctx->qblur;
393 s->b_quant_factor= avctx->b_quant_factor;
395 s->aspect_ratio_info= avctx->aspect_ratio_info;
396 s->flags= avctx->flags;
397 s->max_b_frames= avctx->max_b_frames;
398 s->rc_strategy= avctx->rc_strategy;
399 s->b_frame_strategy= avctx->b_frame_strategy;
400 s->codec_id= avctx->codec->id;
402 if (s->gop_size <= 1) {
410 if (avctx->me_method == 0)
411 /* For compatibility */
412 s->me_method = motion_estimation_method;
414 s->me_method = avctx->me_method;
417 s->fixed_qscale = (avctx->flags & CODEC_FLAG_QSCALE);
419 switch(avctx->codec->id) {
420 case CODEC_ID_MPEG1VIDEO:
421 s->out_format = FMT_MPEG1;
422 avctx->delay=0; //FIXME not sure, should check the spec
425 s->out_format = FMT_MJPEG;
426 s->intra_only = 1; /* force intra only for jpeg */
427 s->mjpeg_write_tables = 1; /* write all tables */
428 s->mjpeg_vsample[0] = 2; /* set up default sampling factors */
429 s->mjpeg_vsample[1] = 1; /* the only currently supported values */
430 s->mjpeg_vsample[2] = 1;
431 s->mjpeg_hsample[0] = 2;
432 s->mjpeg_hsample[1] = 1;
433 s->mjpeg_hsample[2] = 1;
434 if (mjpeg_init(s) < 0)
439 if (h263_get_picture_format(s->width, s->height) == 7) {
440 printf("Input picture size isn't suitable for h263 codec! try h263+\n");
443 s->out_format = FMT_H263;
447 s->out_format = FMT_H263;
449 s->rtp_payload_size = 1200;
451 s->unrestricted_mv = 1;
454 /* These are just to be sure */
460 s->out_format = FMT_H263;
465 s->out_format = FMT_H263;
467 s->unrestricted_mv = 1;
468 s->has_b_frames= s->max_b_frames ? 1 : 0;
470 avctx->delay= s->low_delay ? 0 : (s->max_b_frames + 1);
472 case CODEC_ID_MSMPEG4V1:
473 s->out_format = FMT_H263;
476 s->unrestricted_mv = 1;
477 s->msmpeg4_version= 1;
480 case CODEC_ID_MSMPEG4V2:
481 s->out_format = FMT_H263;
484 s->unrestricted_mv = 1;
485 s->msmpeg4_version= 2;
488 case CODEC_ID_MSMPEG4V3:
489 s->out_format = FMT_H263;
492 s->unrestricted_mv = 1;
493 s->msmpeg4_version= 3;
500 if((s->flags&CODEC_FLAG_4MV) && !(s->flags&CODEC_FLAG_HQ)){
501 printf("4MV is currently only supported in HQ mode\n");
505 { /* set up some save defaults, some codecs might override them later */
510 memset(default_mv_penalty, 0, sizeof(UINT16)*(MAX_FCODE+1)*(2*MAX_MV+1));
511 memset(default_fcode_tab , 0, sizeof(UINT8)*(2*MAX_MV+1));
513 for(i=-16; i<16; i++){
514 default_fcode_tab[i + MAX_MV]= 1;
518 s->mv_penalty= default_mv_penalty;
519 s->fcode_tab= default_fcode_tab;
521 if (s->out_format == FMT_H263)
523 else if (s->out_format == FMT_MPEG1)
524 mpeg1_encode_init(s);
526 /* dont use mv_penalty table for crap MV as it would be confused */
527 if (s->me_method < ME_EPZS) s->mv_penalty = default_mv_penalty;
532 if (MPV_common_init(s) < 0)
535 /* init default q matrix */
537 if(s->out_format == FMT_H263)
538 s->intra_matrix[i] = default_non_intra_matrix[i];
540 s->intra_matrix[i] = default_intra_matrix[i];
542 s->inter_matrix[i] = default_non_intra_matrix[i];
545 /* precompute matrix */
546 /* for mjpeg, we do include qscale in the matrix */
547 if (s->out_format != FMT_MJPEG) {
548 convert_matrix(s->q_intra_matrix, s->q_intra_matrix16, s->q_intra_matrix16_bias,
549 s->intra_matrix, s->intra_quant_bias);
550 convert_matrix(s->q_inter_matrix, s->q_inter_matrix16, s->q_inter_matrix16_bias,
551 s->inter_matrix, s->inter_quant_bias);
554 if(ff_rate_control_init(s) < 0)
557 s->picture_number = 0;
558 s->picture_in_gop_number = 0;
559 s->fake_picture_number = 0;
560 /* motion detector init */
567 int MPV_encode_end(AVCodecContext *avctx)
569 MpegEncContext *s = avctx->priv_data;
575 ff_rate_control_uninit(s);
578 if (s->out_format == FMT_MJPEG)
584 /* draw the edges of width 'w' of an image of size width, height */
585 static void draw_edges_c(UINT8 *buf, int wrap, int width, int height, int w)
587 UINT8 *ptr, *last_line;
590 last_line = buf + (height - 1) * wrap;
593 memcpy(buf - (i + 1) * wrap, buf, width);
594 memcpy(last_line + (i + 1) * wrap, last_line, width);
598 for(i=0;i<height;i++) {
599 memset(ptr - w, ptr[0], w);
600 memset(ptr + width, ptr[width-1], w);
605 memset(buf - (i + 1) * wrap - w, buf[0], w); /* top left */
606 memset(buf - (i + 1) * wrap + width, buf[width-1], w); /* top right */
607 memset(last_line + (i + 1) * wrap - w, last_line[0], w); /* top left */
608 memset(last_line + (i + 1) * wrap + width, last_line[width-1], w); /* top right */
612 /* generic function for encode/decode called before a frame is coded/decoded */
613 void MPV_frame_start(MpegEncContext *s)
619 if (s->pict_type == B_TYPE) {
621 s->current_picture[i] = s->aux_picture[i];
625 /* swap next and last */
626 tmp = s->last_picture[i];
627 s->last_picture[i] = s->next_picture[i];
628 s->next_picture[i] = tmp;
629 s->current_picture[i] = tmp;
634 /* generic function for encode/decode called after a frame has been coded/decoded */
635 void MPV_frame_end(MpegEncContext *s)
637 /* draw edge for correct motion prediction if outside */
638 if (s->pict_type != B_TYPE && !s->intra_only) {
639 if(s->avctx==NULL || s->avctx->codec->id!=CODEC_ID_MPEG4 || s->divx_version==500){
640 draw_edges(s->current_picture[0], s->linesize, s->mb_width*16, s->mb_height*16, EDGE_WIDTH);
641 draw_edges(s->current_picture[1], s->linesize/2, s->mb_width*8, s->mb_height*8, EDGE_WIDTH/2);
642 draw_edges(s->current_picture[2], s->linesize/2, s->mb_width*8, s->mb_height*8, EDGE_WIDTH/2);
644 /* mpeg4? / opendivx / xvid */
645 draw_edges(s->current_picture[0], s->linesize, s->width, s->height, EDGE_WIDTH);
646 draw_edges(s->current_picture[1], s->linesize/2, s->width/2, s->height/2, EDGE_WIDTH/2);
647 draw_edges(s->current_picture[2], s->linesize/2, s->width/2, s->height/2, EDGE_WIDTH/2);
652 if(s->pict_type!=B_TYPE){
653 s->last_non_b_pict_type= s->pict_type;
654 s->last_non_b_qscale= s->qscale;
655 s->last_non_b_mc_mb_var= s->mc_mb_var;
656 s->num_available_buffers++;
657 if(s->num_available_buffers>2) s->num_available_buffers= 2;
661 /* reorder input for encoding */
662 void reorder_input(MpegEncContext *s, AVPicture *pict)
666 if(s->max_b_frames > FF_MAX_B_FRAMES) s->max_b_frames= FF_MAX_B_FRAMES;
668 // delay= s->max_b_frames+1; (or 0 if no b frames cuz decoder diff)
670 for(j=0; j<REORDER_BUFFER_SIZE-1; j++){
671 s->coded_order[j]= s->coded_order[j+1];
673 s->coded_order[j].picture[0]= s->coded_order[j].picture[1]= s->coded_order[j].picture[2]= NULL; //catch uninitalized buffers
674 s->coded_order[j].pict_type=0;
676 switch(s->input_pict_type){
681 index= s->max_b_frames - s->b_frames_since_non_b;
682 s->b_frames_since_non_b=0;
685 index= s->max_b_frames + 1;
686 s->b_frames_since_non_b++;
689 //printf("index:%d type:%d strides: %d %d\n", index, s->input_pict_type, pict->linesize[0], s->linesize);
690 if( (index==0 || (s->flags&CODEC_FLAG_INPUT_PRESERVED))
691 && pict->linesize[0] == s->linesize
692 && pict->linesize[1] == s->linesize>>1
693 && pict->linesize[2] == s->linesize>>1){
696 s->coded_order[index].picture[i]= pict->data[i];
701 uint8_t *src = pict->data[i];
703 int src_wrap = pict->linesize[i];
704 int dest_wrap = s->linesize;
708 if(index==0) dest= s->last_picture[i]+16; //is current_picture indeed but the switch hapens after reordering
709 else dest= s->picture_buffer[s->picture_buffer_index][i];
717 s->coded_order[index].picture[i]= dest;
719 memcpy(dest, src, w);
725 s->picture_buffer_index++;
726 if(s->picture_buffer_index >= REORDER_BUFFER_SIZE-1) s->picture_buffer_index=0;
729 s->coded_order[index].pict_type = s->input_pict_type;
730 s->coded_order[index].qscale = s->input_qscale;
731 s->coded_order[index].force_type= s->force_input_type;
732 s->coded_order[index].picture_in_gop_number= s->input_picture_in_gop_number;
733 s->coded_order[index].picture_number= s->input_picture_number;
736 s->new_picture[i]= s->coded_order[0].picture[i];
740 int MPV_encode_picture(AVCodecContext *avctx,
741 unsigned char *buf, int buf_size, void *data)
743 MpegEncContext *s = avctx->priv_data;
744 AVPicture *pict = data;
746 s->input_qscale = avctx->quality;
748 init_put_bits(&s->pb, buf, buf_size, NULL, NULL);
750 if(avctx->flags&CODEC_FLAG_TYPE){
752 s->force_input_type= avctx->key_frame ? I_TYPE : P_TYPE;
753 }else if(s->flags&CODEC_FLAG_PASS2){
755 s->force_input_type= s->rc_context.entry[s->input_picture_number].new_pict_type;
757 s->force_input_type=0;
758 if (!s->intra_only) {
759 /* first picture of GOP is intra */
760 if (s->input_picture_in_gop_number % s->gop_size==0){
761 s->input_pict_type = I_TYPE;
762 }else if(s->max_b_frames==0){
763 s->input_pict_type = P_TYPE;
765 if(s->b_frames_since_non_b < s->max_b_frames) //FIXME more IQ
766 s->input_pict_type = B_TYPE;
768 s->input_pict_type = P_TYPE;
771 s->input_pict_type = I_TYPE;
775 if(s->input_pict_type==I_TYPE)
776 s->input_picture_in_gop_number=0;
778 reorder_input(s, pict);
781 if(s->coded_order[0].picture[0]){
783 s->pict_type= s->coded_order[0].pict_type;
784 if (s->fixed_qscale) /* the ratecontrol needs the last qscale so we dont touch it for CBR */
785 s->qscale= s->coded_order[0].qscale;
786 s->force_type= s->coded_order[0].force_type;
787 s->picture_in_gop_number= s->coded_order[0].picture_in_gop_number;
788 s->picture_number= s->coded_order[0].picture_number;
792 encode_picture(s, s->picture_number);
793 avctx->key_frame = (s->pict_type == I_TYPE);
794 avctx->header_bits = s->header_bits;
795 avctx->mv_bits = s->mv_bits;
796 avctx->misc_bits = s->misc_bits;
797 avctx->i_tex_bits = s->i_tex_bits;
798 avctx->p_tex_bits = s->p_tex_bits;
799 avctx->i_count = s->i_count;
800 avctx->p_count = s->p_count;
801 avctx->skip_count = s->skip_count;
805 if (s->out_format == FMT_MJPEG)
806 mjpeg_picture_trailer(s);
808 avctx->quality = s->qscale;
810 if(s->flags&CODEC_FLAG_PASS1)
811 ff_write_pass1_stats(s);
814 s->input_picture_number++;
815 s->input_picture_in_gop_number++;
817 flush_put_bits(&s->pb);
818 s->frame_bits = (pbBufPtr(&s->pb) - s->pb.buf) * 8;
819 if(s->pict_type==B_TYPE) s->pb_frame_bits+= s->frame_bits;
820 else s->pb_frame_bits= s->frame_bits;
822 s->total_bits += s->frame_bits;
823 avctx->frame_bits = s->frame_bits;
824 //printf("fcode: %d, type: %d, head: %d, mv: %d, misc: %d, frame: %d, itex: %d, ptex: %d\n",
825 //s->f_code, avctx->key_frame, s->header_bits, s->mv_bits, s->misc_bits, s->frame_bits, s->i_tex_bits, s->p_tex_bits);
827 if (avctx->get_psnr) {
828 /* At this point pict->data should have the original frame */
829 /* an s->current_picture should have the coded/decoded frame */
830 get_psnr(pict->data, s->current_picture,
831 pict->linesize, s->linesize, avctx);
832 // printf("%f\n", avctx->psnr_y);
834 return pbBufPtr(&s->pb) - s->pb.buf;
837 static inline void gmc1_motion(MpegEncContext *s,
838 UINT8 *dest_y, UINT8 *dest_cb, UINT8 *dest_cr,
840 UINT8 **ref_picture, int src_offset,
844 int offset, src_x, src_y, linesize;
845 int motion_x, motion_y;
847 if(s->real_sprite_warping_points>1) printf("more than 1 warp point isnt supported\n");
848 motion_x= s->sprite_offset[0][0];
849 motion_y= s->sprite_offset[0][1];
850 src_x = s->mb_x * 16 + (motion_x >> (s->sprite_warping_accuracy+1));
851 src_y = s->mb_y * 16 + (motion_y >> (s->sprite_warping_accuracy+1));
852 motion_x<<=(3-s->sprite_warping_accuracy);
853 motion_y<<=(3-s->sprite_warping_accuracy);
854 src_x = clip(src_x, -16, s->width);
855 if (src_x == s->width)
857 src_y = clip(src_y, -16, s->height);
858 if (src_y == s->height)
861 linesize = s->linesize;
862 ptr = ref_picture[0] + (src_y * linesize) + src_x + src_offset;
865 gmc1(dest_y , ptr , linesize, h, motion_x&15, motion_y&15, s->no_rounding);
866 gmc1(dest_y+8, ptr+8, linesize, h, motion_x&15, motion_y&15, s->no_rounding);
868 motion_x= s->sprite_offset[1][0];
869 motion_y= s->sprite_offset[1][1];
870 src_x = s->mb_x * 8 + (motion_x >> (s->sprite_warping_accuracy+1));
871 src_y = s->mb_y * 8 + (motion_y >> (s->sprite_warping_accuracy+1));
872 motion_x<<=(3-s->sprite_warping_accuracy);
873 motion_y<<=(3-s->sprite_warping_accuracy);
874 src_x = clip(src_x, -8, s->width>>1);
875 if (src_x == s->width>>1)
877 src_y = clip(src_y, -8, s->height>>1);
878 if (src_y == s->height>>1)
881 offset = (src_y * linesize>>1) + src_x + (src_offset>>1);
882 ptr = ref_picture[1] + offset;
883 gmc1(dest_cb + (dest_offset>>1), ptr, linesize>>1, h>>1, motion_x&15, motion_y&15, s->no_rounding);
884 ptr = ref_picture[2] + offset;
885 gmc1(dest_cr + (dest_offset>>1), ptr, linesize>>1, h>>1, motion_x&15, motion_y&15, s->no_rounding);
890 /* apply one mpeg motion vector to the three components */
891 static inline void mpeg_motion(MpegEncContext *s,
892 UINT8 *dest_y, UINT8 *dest_cb, UINT8 *dest_cr,
894 UINT8 **ref_picture, int src_offset,
895 int field_based, op_pixels_func *pix_op,
896 int motion_x, int motion_y, int h)
899 int dxy, offset, mx, my, src_x, src_y, height, linesize;
900 if(s->quarter_sample)
906 dxy = ((motion_y & 1) << 1) | (motion_x & 1);
907 src_x = s->mb_x * 16 + (motion_x >> 1);
908 src_y = s->mb_y * (16 >> field_based) + (motion_y >> 1);
910 /* WARNING: do no forget half pels */
911 height = s->height >> field_based;
912 src_x = clip(src_x, -16, s->width);
913 if (src_x == s->width)
915 src_y = clip(src_y, -16, height);
918 linesize = s->linesize << field_based;
919 ptr = ref_picture[0] + (src_y * linesize) + (src_x) + src_offset;
920 dest_y += dest_offset;
921 pix_op[dxy](dest_y, ptr, linesize, h);
922 pix_op[dxy](dest_y + 8, ptr + 8, linesize, h);
924 if (s->out_format == FMT_H263) {
926 if ((motion_x & 3) != 0)
928 if ((motion_y & 3) != 0)
935 dxy = ((my & 1) << 1) | (mx & 1);
940 src_x = s->mb_x * 8 + mx;
941 src_y = s->mb_y * (8 >> field_based) + my;
942 src_x = clip(src_x, -8, s->width >> 1);
943 if (src_x == (s->width >> 1))
945 src_y = clip(src_y, -8, height >> 1);
946 if (src_y == (height >> 1))
949 offset = (src_y * (linesize >> 1)) + src_x + (src_offset >> 1);
950 ptr = ref_picture[1] + offset;
951 pix_op[dxy](dest_cb + (dest_offset >> 1), ptr, linesize >> 1, h >> 1);
952 ptr = ref_picture[2] + offset;
953 pix_op[dxy](dest_cr + (dest_offset >> 1), ptr, linesize >> 1, h >> 1);
956 static inline void qpel_motion(MpegEncContext *s,
957 UINT8 *dest_y, UINT8 *dest_cb, UINT8 *dest_cr,
959 UINT8 **ref_picture, int src_offset,
960 int field_based, op_pixels_func *pix_op,
961 qpel_mc_func *qpix_op,
962 int motion_x, int motion_y, int h)
965 int dxy, offset, mx, my, src_x, src_y, height, linesize;
967 dxy = ((motion_y & 3) << 2) | (motion_x & 3);
968 src_x = s->mb_x * 16 + (motion_x >> 2);
969 src_y = s->mb_y * (16 >> field_based) + (motion_y >> 2);
971 height = s->height >> field_based;
972 src_x = clip(src_x, -16, s->width);
973 if (src_x == s->width)
975 src_y = clip(src_y, -16, height);
978 linesize = s->linesize << field_based;
979 ptr = ref_picture[0] + (src_y * linesize) + src_x + src_offset;
980 dest_y += dest_offset;
981 //printf("%d %d %d\n", src_x, src_y, dxy);
982 qpix_op[dxy](dest_y , ptr , linesize, linesize, motion_x&3, motion_y&3);
983 qpix_op[dxy](dest_y + 8, ptr + 8, linesize, linesize, motion_x&3, motion_y&3);
984 qpix_op[dxy](dest_y + linesize*8 , ptr + linesize*8 , linesize, linesize, motion_x&3, motion_y&3);
985 qpix_op[dxy](dest_y + linesize*8 + 8, ptr + linesize*8 + 8, linesize, linesize, motion_x&3, motion_y&3);
987 mx= (motion_x>>1) | (motion_x&1);
988 my= (motion_y>>1) | (motion_y&1);
998 src_x = s->mb_x * 8 + mx;
999 src_y = s->mb_y * (8 >> field_based) + my;
1000 src_x = clip(src_x, -8, s->width >> 1);
1001 if (src_x == (s->width >> 1))
1003 src_y = clip(src_y, -8, height >> 1);
1004 if (src_y == (height >> 1))
1007 offset = (src_y * (linesize >> 1)) + src_x + (src_offset >> 1);
1008 ptr = ref_picture[1] + offset;
1009 pix_op[dxy](dest_cb + (dest_offset >> 1), ptr, linesize >> 1, h >> 1);
1010 ptr = ref_picture[2] + offset;
1011 pix_op[dxy](dest_cr + (dest_offset >> 1), ptr, linesize >> 1, h >> 1);
1015 static inline void MPV_motion(MpegEncContext *s,
1016 UINT8 *dest_y, UINT8 *dest_cb, UINT8 *dest_cr,
1017 int dir, UINT8 **ref_picture,
1018 op_pixels_func *pix_op, qpel_mc_func *qpix_op)
1020 int dxy, offset, mx, my, src_x, src_y, motion_x, motion_y;
1027 switch(s->mv_type) {
1031 mpeg_motion(s, dest_y, dest_cb, dest_cr, 0,
1034 s->sprite_offset[0][0]>>3,
1035 s->sprite_offset[0][1]>>3,
1038 gmc1_motion(s, dest_y, dest_cb, dest_cr, 0,
1042 }else if(s->quarter_sample && dir==0){ //FIXME
1043 qpel_motion(s, dest_y, dest_cb, dest_cr, 0,
1046 s->mv[dir][0][0], s->mv[dir][0][1], 16);
1048 mpeg_motion(s, dest_y, dest_cb, dest_cr, 0,
1051 s->mv[dir][0][0], s->mv[dir][0][1], 16);
1056 motion_x = s->mv[dir][i][0];
1057 motion_y = s->mv[dir][i][1];
1059 dxy = ((motion_y & 1) << 1) | (motion_x & 1);
1060 src_x = mb_x * 16 + (motion_x >> 1) + (i & 1) * 8;
1061 src_y = mb_y * 16 + (motion_y >> 1) + (i >>1) * 8;
1063 /* WARNING: do no forget half pels */
1064 src_x = clip(src_x, -16, s->width);
1065 if (src_x == s->width)
1067 src_y = clip(src_y, -16, s->height);
1068 if (src_y == s->height)
1071 ptr = ref_picture[0] + (src_y * s->linesize) + (src_x);
1072 dest = dest_y + ((i & 1) * 8) + (i >> 1) * 8 * s->linesize;
1073 pix_op[dxy](dest, ptr, s->linesize, 8);
1075 /* In case of 8X8, we construct a single chroma motion vector
1076 with a special rounding */
1080 mx += s->mv[dir][i][0];
1081 my += s->mv[dir][i][1];
1084 mx = (h263_chroma_roundtab[mx & 0xf] + ((mx >> 3) & ~1));
1087 mx = -(h263_chroma_roundtab[mx & 0xf] + ((mx >> 3) & ~1));
1090 my = (h263_chroma_roundtab[my & 0xf] + ((my >> 3) & ~1));
1093 my = -(h263_chroma_roundtab[my & 0xf] + ((my >> 3) & ~1));
1095 dxy = ((my & 1) << 1) | (mx & 1);
1099 src_x = mb_x * 8 + mx;
1100 src_y = mb_y * 8 + my;
1101 src_x = clip(src_x, -8, s->width/2);
1102 if (src_x == s->width/2)
1104 src_y = clip(src_y, -8, s->height/2);
1105 if (src_y == s->height/2)
1108 offset = (src_y * (s->linesize >> 1)) + src_x;
1109 ptr = ref_picture[1] + offset;
1110 pix_op[dxy](dest_cb, ptr, s->linesize >> 1, 8);
1111 ptr = ref_picture[2] + offset;
1112 pix_op[dxy](dest_cr, ptr, s->linesize >> 1, 8);
1115 if (s->picture_structure == PICT_FRAME) {
1117 mpeg_motion(s, dest_y, dest_cb, dest_cr, 0,
1118 ref_picture, s->field_select[dir][0] ? s->linesize : 0,
1120 s->mv[dir][0][0], s->mv[dir][0][1], 8);
1122 mpeg_motion(s, dest_y, dest_cb, dest_cr, s->linesize,
1123 ref_picture, s->field_select[dir][1] ? s->linesize : 0,
1125 s->mv[dir][1][0], s->mv[dir][1][1], 8);
1135 /* put block[] to dest[] */
1136 static inline void put_dct(MpegEncContext *s,
1137 DCTELEM *block, int i, UINT8 *dest, int line_size)
1140 s->dct_unquantize(s, block, i, s->qscale);
1142 put_pixels_clamped(block, dest, line_size);
1145 /* add block[] to dest[] */
1146 static inline void add_dct(MpegEncContext *s,
1147 DCTELEM *block, int i, UINT8 *dest, int line_size)
1149 /* skip dequant / idct if we are really late ;) */
1150 if(s->hurry_up>1) return;
1152 if (s->block_last_index[i] >= 0) {
1154 if(s->encoding || (!s->h263_msmpeg4))
1155 s->dct_unquantize(s, block, i, s->qscale);
1158 add_pixels_clamped(block, dest, line_size);
1162 /* generic function called after a macroblock has been parsed by the
1163 decoder or after it has been encoded by the encoder.
1165 Important variables used:
1166 s->mb_intra : true if intra macroblock
1167 s->mv_dir : motion vector direction
1168 s->mv_type : motion vector type
1169 s->mv : motion vector
1170 s->interlaced_dct : true if interlaced dct used (mpeg2)
1172 void MPV_decode_mb(MpegEncContext *s, DCTELEM block[6][64])
1175 int dct_linesize, dct_offset;
1176 op_pixels_func *op_pix;
1177 qpel_mc_func *op_qpix;
1182 #ifdef FF_POSTPROCESS
1183 quant_store[mb_y][mb_x]=s->qscale;
1184 //printf("[%02d][%02d] %d\n",mb_x,mb_y,s->qscale);
1187 /* update DC predictors for P macroblocks */
1189 if (s->h263_pred || s->h263_aic) {
1190 if(s->mbintra_table[mb_x + mb_y*s->mb_width])
1193 s->mbintra_table[mb_x + mb_y*s->mb_width]=0;
1194 wrap = 2 * s->mb_width + 2;
1195 xy = 2 * mb_x + 1 + (2 * mb_y + 1) * wrap;
1198 s->dc_val[0][xy] = v;
1199 s->dc_val[0][xy + 1] = v;
1200 s->dc_val[0][xy + wrap] = v;
1201 s->dc_val[0][xy + 1 + wrap] = v;
1203 memset(s->ac_val[0][xy], 0, 16 * sizeof(INT16));
1204 memset(s->ac_val[0][xy + 1], 0, 16 * sizeof(INT16));
1205 memset(s->ac_val[0][xy + wrap], 0, 16 * sizeof(INT16));
1206 memset(s->ac_val[0][xy + 1 + wrap], 0, 16 * sizeof(INT16));
1207 if (s->h263_msmpeg4) {
1208 s->coded_block[xy] = 0;
1209 s->coded_block[xy + 1] = 0;
1210 s->coded_block[xy + wrap] = 0;
1211 s->coded_block[xy + 1 + wrap] = 0;
1214 wrap = s->mb_width + 2;
1215 xy = mb_x + 1 + (mb_y + 1) * wrap;
1216 s->dc_val[1][xy] = v;
1217 s->dc_val[2][xy] = v;
1219 memset(s->ac_val[1][xy], 0, 16 * sizeof(INT16));
1220 memset(s->ac_val[2][xy], 0, 16 * sizeof(INT16));
1223 s->last_dc[0] = 128 << s->intra_dc_precision;
1224 s->last_dc[1] = 128 << s->intra_dc_precision;
1225 s->last_dc[2] = 128 << s->intra_dc_precision;
1228 else if (s->h263_pred || s->h263_aic)
1229 s->mbintra_table[mb_x + mb_y*s->mb_width]=1;
1231 /* update motion predictor, not for B-frames as they need the motion_val from the last P/S-Frame */
1232 if (s->out_format == FMT_H263) { //FIXME move into h263.c if possible, format specific stuff shouldnt be here
1233 if(s->pict_type!=B_TYPE){
1234 int xy, wrap, motion_x, motion_y;
1236 wrap = 2 * s->mb_width + 2;
1237 xy = 2 * mb_x + 1 + (2 * mb_y + 1) * wrap;
1242 } else if (s->mv_type == MV_TYPE_16X16) {
1243 motion_x = s->mv[0][0][0];
1244 motion_y = s->mv[0][0][1];
1246 /* no update if 8X8 because it has been done during parsing */
1247 s->motion_val[xy][0] = motion_x;
1248 s->motion_val[xy][1] = motion_y;
1249 s->motion_val[xy + 1][0] = motion_x;
1250 s->motion_val[xy + 1][1] = motion_y;
1251 s->motion_val[xy + wrap][0] = motion_x;
1252 s->motion_val[xy + wrap][1] = motion_y;
1253 s->motion_val[xy + 1 + wrap][0] = motion_x;
1254 s->motion_val[xy + 1 + wrap][1] = motion_y;
1259 if (!(s->encoding && (s->intra_only || s->pict_type==B_TYPE))) {
1260 UINT8 *dest_y, *dest_cb, *dest_cr;
1263 /* avoid copy if macroblock skipped in last frame too
1264 dont touch it for B-frames as they need the skip info from the next p-frame */
1265 if (s->pict_type != B_TYPE) {
1266 mbskip_ptr = &s->mbskip_table[s->mb_y * s->mb_width + s->mb_x];
1269 /* if previous was skipped too, then nothing to do !
1270 skip only during decoding as we might trash the buffers during encoding a bit */
1271 if (*mbskip_ptr != 0 && !s->encoding)
1273 *mbskip_ptr = 1; /* indicate that this time we skiped it */
1275 *mbskip_ptr = 0; /* not skipped */
1279 dest_y = s->current_picture[0] + (mb_y * 16 * s->linesize) + mb_x * 16;
1280 dest_cb = s->current_picture[1] + (mb_y * 8 * (s->linesize >> 1)) + mb_x * 8;
1281 dest_cr = s->current_picture[2] + (mb_y * 8 * (s->linesize >> 1)) + mb_x * 8;
1283 if (s->interlaced_dct) {
1284 dct_linesize = s->linesize * 2;
1285 dct_offset = s->linesize;
1287 dct_linesize = s->linesize;
1288 dct_offset = s->linesize * 8;
1292 /* motion handling */
1293 if((s->flags&CODEC_FLAG_HQ) || (!s->encoding)){
1294 if ((!s->no_rounding) || s->pict_type==B_TYPE){
1295 op_pix = put_pixels_tab;
1296 op_qpix= qpel_mc_rnd_tab;
1298 op_pix = put_no_rnd_pixels_tab;
1299 op_qpix= qpel_mc_no_rnd_tab;
1302 if (s->mv_dir & MV_DIR_FORWARD) {
1303 MPV_motion(s, dest_y, dest_cb, dest_cr, 0, s->last_picture, op_pix, op_qpix);
1304 if ((!s->no_rounding) || s->pict_type==B_TYPE)
1305 op_pix = avg_pixels_tab;
1307 op_pix = avg_no_rnd_pixels_tab;
1309 if (s->mv_dir & MV_DIR_BACKWARD) {
1310 MPV_motion(s, dest_y, dest_cb, dest_cr, 1, s->next_picture, op_pix, op_qpix);
1314 /* add dct residue */
1315 add_dct(s, block[0], 0, dest_y, dct_linesize);
1316 add_dct(s, block[1], 1, dest_y + 8, dct_linesize);
1317 add_dct(s, block[2], 2, dest_y + dct_offset, dct_linesize);
1318 add_dct(s, block[3], 3, dest_y + dct_offset + 8, dct_linesize);
1320 add_dct(s, block[4], 4, dest_cb, s->linesize >> 1);
1321 add_dct(s, block[5], 5, dest_cr, s->linesize >> 1);
1323 /* dct only in intra block */
1324 put_dct(s, block[0], 0, dest_y, dct_linesize);
1325 put_dct(s, block[1], 1, dest_y + 8, dct_linesize);
1326 put_dct(s, block[2], 2, dest_y + dct_offset, dct_linesize);
1327 put_dct(s, block[3], 3, dest_y + dct_offset + 8, dct_linesize);
1329 put_dct(s, block[4], 4, dest_cb, s->linesize >> 1);
1330 put_dct(s, block[5], 5, dest_cr, s->linesize >> 1);
1334 emms_c(); //FIXME remove
1337 static inline void clip_coeffs(MpegEncContext *s, DCTELEM *block, int last_index)
1340 const int maxlevel= s->max_qcoeff;
1341 const int minlevel= s->min_qcoeff;
1343 for(i=0;i<=last_index; i++){
1344 const int j = zigzag_direct[i];
1345 int level = block[j];
1347 if (level>maxlevel) level=maxlevel;
1348 else if(level<minlevel) level=minlevel;
1353 static void encode_mb(MpegEncContext *s, int motion_x, int motion_y)
1355 const int mb_x= s->mb_x;
1356 const int mb_y= s->mb_y;
1359 if (s->interlaced_dct) {
1360 dct_linesize = s->linesize * 2;
1361 dct_offset = s->linesize;
1363 dct_linesize = s->linesize;
1364 dct_offset = s->linesize * 8;
1373 ptr = s->new_picture[0] + (mb_y * 16 * wrap) + mb_x * 16;
1374 get_pixels(s->block[0], ptr , wrap);
1375 get_pixels(s->block[1], ptr + 8, wrap);
1376 get_pixels(s->block[2], ptr + 8 * wrap , wrap);
1377 get_pixels(s->block[3], ptr + 8 * wrap + 8, wrap);
1380 ptr = s->new_picture[1] + (mb_y * 8 * wrap) + mb_x * 8;
1381 get_pixels(s->block[4], ptr, wrap);
1383 ptr = s->new_picture[2] + (mb_y * 8 * wrap) + mb_x * 8;
1384 get_pixels(s->block[5], ptr, wrap);
1386 op_pixels_func *op_pix;
1387 qpel_mc_func *op_qpix;
1388 UINT8 *dest_y, *dest_cb, *dest_cr;
1392 dest_y = s->current_picture[0] + (mb_y * 16 * s->linesize ) + mb_x * 16;
1393 dest_cb = s->current_picture[1] + (mb_y * 8 * (s->linesize >> 1)) + mb_x * 8;
1394 dest_cr = s->current_picture[2] + (mb_y * 8 * (s->linesize >> 1)) + mb_x * 8;
1396 if ((!s->no_rounding) || s->pict_type==B_TYPE){
1397 op_pix = put_pixels_tab;
1398 op_qpix= qpel_mc_rnd_tab;
1400 op_pix = put_no_rnd_pixels_tab;
1401 op_qpix= qpel_mc_no_rnd_tab;
1404 if (s->mv_dir & MV_DIR_FORWARD) {
1405 MPV_motion(s, dest_y, dest_cb, dest_cr, 0, s->last_picture, op_pix, op_qpix);
1406 if ((!s->no_rounding) || s->pict_type==B_TYPE)
1407 op_pix = avg_pixels_tab;
1409 op_pix = avg_no_rnd_pixels_tab;
1411 if (s->mv_dir & MV_DIR_BACKWARD) {
1412 MPV_motion(s, dest_y, dest_cb, dest_cr, 1, s->next_picture, op_pix, op_qpix);
1415 ptr = s->new_picture[0] + (mb_y * 16 * wrap) + mb_x * 16;
1416 diff_pixels(s->block[0], ptr , dest_y , wrap);
1417 diff_pixels(s->block[1], ptr + 8, dest_y + 8, wrap);
1418 diff_pixels(s->block[2], ptr + 8 * wrap , dest_y + 8 * wrap , wrap);
1419 diff_pixels(s->block[3], ptr + 8 * wrap + 8, dest_y + 8 * wrap + 8, wrap);
1422 ptr = s->new_picture[1] + (mb_y * 8 * wrap) + mb_x * 8;
1423 diff_pixels(s->block[4], ptr, dest_cb, wrap);
1425 ptr = s->new_picture[2] + (mb_y * 8 * wrap) + mb_x * 8;
1426 diff_pixels(s->block[5], ptr, dest_cr, wrap);
1433 adap_parm = ((s->avg_mb_var << 1) + s->mb_var[s->mb_width*mb_y+mb_x] + 1.0) /
1434 ((s->mb_var[s->mb_width*mb_y+mb_x] << 1) + s->avg_mb_var + 1.0);
1436 printf("\ntype=%c qscale=%2d adap=%0.2f dquant=%4.2f var=%4d avgvar=%4d",
1437 (s->mb_type[s->mb_width*mb_y+mb_x] > 0) ? 'I' : 'P',
1438 s->qscale, adap_parm, s->qscale*adap_parm,
1439 s->mb_var[s->mb_width*mb_y+mb_x], s->avg_mb_var);
1442 /* DCT & quantize */
1443 if (s->h263_pred && s->msmpeg4_version!=2) {
1445 } else if (s->h263_aic) {
1446 s->y_dc_scale = 2*s->qscale;
1447 s->c_dc_scale = 2*s->qscale;
1449 /* default quantization values */
1453 if(s->out_format==FMT_MJPEG){
1456 s->block_last_index[i] = dct_quantize(s, s->block[i], i, 8, &overflow);
1457 if (overflow) clip_coeffs(s, s->block[i], s->block_last_index[i]);
1462 s->block_last_index[i] = dct_quantize(s, s->block[i], i, s->qscale, &overflow);
1463 // FIXME we could decide to change to quantizer instead of clipping
1464 // JS: I don't think that would be a good idea it could lower quality instead
1465 // of improve it. Just INTRADC clipping deserves changes in quantizer
1466 if (overflow) clip_coeffs(s, s->block[i], s->block_last_index[i]);
1470 /* huffman encode */
1471 switch(s->out_format) {
1473 mpeg1_encode_mb(s, s->block, motion_x, motion_y);
1476 if (s->h263_msmpeg4)
1477 msmpeg4_encode_mb(s, s->block, motion_x, motion_y);
1478 else if(s->h263_pred)
1479 mpeg4_encode_mb(s, s->block, motion_x, motion_y);
1481 h263_encode_mb(s, s->block, motion_x, motion_y);
1484 mjpeg_encode_mb(s, s->block);
1489 static void copy_bits(PutBitContext *pb, UINT8 *src, int length)
1492 int bytes= length>>4;
1493 int bits= length&15;
1496 for(i=0; i<bytes; i++) put_bits(pb, 16, be2me_16(((uint16_t*)src)[i]));
1497 put_bits(pb, bits, be2me_16(((uint16_t*)src)[i])>>(16-bits));
1499 int bytes= length>>3;
1503 for(i=0; i<bytes; i++) put_bits(pb, 8, src[i]);
1504 put_bits(pb, bits, src[i]>>(8-bits));
1508 static void copy_context_before_encode(MpegEncContext *d, MpegEncContext *s, int type){
1511 memcpy(d->last_mv, s->last_mv, 2*2*2*sizeof(int)); //FIXME is memcpy faster then a loop?
1514 d->mb_incr= s->mb_incr;
1516 d->last_dc[i]= s->last_dc[i];
1519 d->mv_bits= s->mv_bits;
1520 d->i_tex_bits= s->i_tex_bits;
1521 d->p_tex_bits= s->p_tex_bits;
1522 d->i_count= s->i_count;
1523 d->p_count= s->p_count;
1524 d->skip_count= s->skip_count;
1525 d->misc_bits= s->misc_bits;
1528 d->mb_skiped= s->mb_skiped;
1531 static void copy_context_after_encode(MpegEncContext *d, MpegEncContext *s, int type){
1534 memcpy(d->mv, s->mv, 2*4*2*sizeof(int));
1535 memcpy(d->last_mv, s->last_mv, 2*2*2*sizeof(int)); //FIXME is memcpy faster then a loop?
1538 d->mb_incr= s->mb_incr;
1540 d->last_dc[i]= s->last_dc[i];
1543 d->mv_bits= s->mv_bits;
1544 d->i_tex_bits= s->i_tex_bits;
1545 d->p_tex_bits= s->p_tex_bits;
1546 d->i_count= s->i_count;
1547 d->p_count= s->p_count;
1548 d->skip_count= s->skip_count;
1549 d->misc_bits= s->misc_bits;
1551 d->mb_intra= s->mb_intra;
1552 d->mb_skiped= s->mb_skiped;
1553 d->mv_type= s->mv_type;
1554 d->mv_dir= s->mv_dir;
1558 d->block_last_index[i]= s->block_last_index[i];
1562 static void encode_picture(MpegEncContext *s, int picture_number)
1564 int mb_x, mb_y, last_gob, pdif = 0;
1567 MpegEncContext best_s, backup_s;
1568 UINT8 bit_buf[7][3000]; //FIXME check that this is ALLWAYS large enogh for a MB
1570 s->picture_number = picture_number;
1575 s->block_wrap[3]= s->mb_width*2 + 2;
1577 s->block_wrap[5]= s->mb_width + 2;
1579 /* Reset the average MB variance */
1583 /* we need to initialize some time vars before we can encode b-frames */
1584 if (s->h263_pred && !s->h263_msmpeg4)
1585 ff_set_mpeg4_time(s, s->picture_number);
1587 /* Estimate motion for every MB */
1588 if(s->pict_type != I_TYPE){
1589 // int16_t (*tmp)[2]= s->p_mv_table;
1590 // s->p_mv_table= s->last_mv_table;
1591 // s->last_mv_table= s->mv_table;
1593 for(mb_y=0; mb_y < s->mb_height; mb_y++) {
1594 s->block_index[0]= s->block_wrap[0]*(mb_y*2 + 1) - 1;
1595 s->block_index[1]= s->block_wrap[0]*(mb_y*2 + 1);
1596 s->block_index[2]= s->block_wrap[0]*(mb_y*2 + 2) - 1;
1597 s->block_index[3]= s->block_wrap[0]*(mb_y*2 + 2);
1598 for(mb_x=0; mb_x < s->mb_width; mb_x++) {
1601 s->block_index[0]+=2;
1602 s->block_index[1]+=2;
1603 s->block_index[2]+=2;
1604 s->block_index[3]+=2;
1606 /* compute motion vector & mb_type and store in context */
1607 if(s->pict_type==B_TYPE)
1608 ff_estimate_b_frame_motion(s, mb_x, mb_y);
1610 ff_estimate_p_frame_motion(s, mb_x, mb_y);
1611 // s->mb_type[mb_y*s->mb_width + mb_x]=MB_TYPE_INTER;
1615 }else if(s->pict_type == I_TYPE){
1617 //FIXME do we need to zero them?
1618 memset(s->motion_val[0], 0, sizeof(INT16)*(s->mb_width*2 + 2)*(s->mb_height*2 + 2)*2);
1619 memset(s->p_mv_table , 0, sizeof(INT16)*(s->mb_width+2)*(s->mb_height+2)*2);
1620 memset(s->mb_type , MB_TYPE_INTRA, sizeof(UINT8)*s->mb_width*s->mb_height);
1623 if(s->avg_mb_var < s->mc_mb_var && s->pict_type == P_TYPE){ //FIXME subtract MV bits
1624 s->pict_type= I_TYPE;
1625 memset(s->mb_type , MB_TYPE_INTRA, sizeof(UINT8)*s->mb_width*s->mb_height);
1626 if(s->max_b_frames==0){
1627 s->input_pict_type= I_TYPE;
1628 s->input_picture_in_gop_number=0;
1630 //printf("Scene change detected, encoding as I Frame\n");
1633 if(s->pict_type==P_TYPE || s->pict_type==S_TYPE)
1634 s->f_code= ff_get_best_fcode(s, s->p_mv_table, MB_TYPE_INTER);
1635 ff_fix_long_p_mvs(s);
1636 if(s->pict_type==B_TYPE){
1637 s->f_code= ff_get_best_fcode(s, s->b_forw_mv_table, MB_TYPE_FORWARD);
1638 s->b_code= ff_get_best_fcode(s, s->b_back_mv_table, MB_TYPE_BACKWARD);
1640 ff_fix_long_b_mvs(s, s->b_forw_mv_table, s->f_code, MB_TYPE_FORWARD);
1641 ff_fix_long_b_mvs(s, s->b_back_mv_table, s->b_code, MB_TYPE_BACKWARD);
1642 ff_fix_long_b_mvs(s, s->b_bidir_forw_mv_table, s->f_code, MB_TYPE_BIDIR);
1643 ff_fix_long_b_mvs(s, s->b_bidir_back_mv_table, s->b_code, MB_TYPE_BIDIR);
1646 //printf("f_code %d ///\n", s->f_code);
1648 // printf("%d %d\n", s->avg_mb_var, s->mc_mb_var);
1650 if(s->flags&CODEC_FLAG_PASS2)
1651 s->qscale = ff_rate_estimate_qscale_pass2(s);
1652 else if (!s->fixed_qscale)
1653 s->qscale = ff_rate_estimate_qscale(s);
1655 if (s->out_format == FMT_MJPEG) {
1656 /* for mjpeg, we do include qscale in the matrix */
1657 s->intra_matrix[0] = default_intra_matrix[0];
1659 s->intra_matrix[i] = (default_intra_matrix[i] * s->qscale) >> 3;
1660 convert_matrix(s->q_intra_matrix, s->q_intra_matrix16,
1661 s->q_intra_matrix16_bias, s->intra_matrix, s->intra_quant_bias);
1664 s->last_bits= get_bit_count(&s->pb);
1665 switch(s->out_format) {
1667 mjpeg_picture_header(s);
1670 if (s->h263_msmpeg4)
1671 msmpeg4_encode_picture_header(s, picture_number);
1672 else if (s->h263_pred)
1673 mpeg4_encode_picture_header(s, picture_number);
1674 else if (s->h263_rv10)
1675 rv10_encode_picture_header(s, picture_number);
1677 h263_encode_picture_header(s, picture_number);
1680 mpeg1_encode_picture_header(s, picture_number);
1683 bits= get_bit_count(&s->pb);
1684 s->header_bits= bits - s->last_bits;
1694 /* init last dc values */
1695 /* note: quant matrix value (8) is implied here */
1696 s->last_dc[0] = 128;
1697 s->last_dc[1] = 128;
1698 s->last_dc[2] = 128;
1700 s->last_mv[0][0][0] = 0;
1701 s->last_mv[0][0][1] = 0;
1703 /* Get the GOB height based on picture height */
1704 if (s->out_format == FMT_H263 && !s->h263_pred && !s->h263_msmpeg4) {
1705 if (s->height <= 400)
1707 else if (s->height <= 800)
1713 s->avg_mb_var = s->avg_mb_var / s->mb_num;
1715 for(mb_y=0; mb_y < s->mb_height; mb_y++) {
1716 /* Put GOB header based on RTP MTU */
1717 /* TODO: Put all this stuff in a separate generic function */
1720 s->ptr_lastgob = s->pb.buf;
1721 s->ptr_last_mb_line = s->pb.buf;
1722 } else if (s->out_format == FMT_H263 && !s->h263_pred && !s->h263_msmpeg4 && !(mb_y % s->gob_index)) {
1723 last_gob = h263_encode_gob_header(s, mb_y);
1725 s->first_gob_line = 1;
1730 s->block_index[0]= s->block_wrap[0]*(mb_y*2 + 1) - 1;
1731 s->block_index[1]= s->block_wrap[0]*(mb_y*2 + 1);
1732 s->block_index[2]= s->block_wrap[0]*(mb_y*2 + 2) - 1;
1733 s->block_index[3]= s->block_wrap[0]*(mb_y*2 + 2);
1734 s->block_index[4]= s->block_wrap[4]*(mb_y + 1) + s->block_wrap[0]*(s->mb_height*2 + 2);
1735 s->block_index[5]= s->block_wrap[4]*(mb_y + 1 + s->mb_height + 2) + s->block_wrap[0]*(s->mb_height*2 + 2);
1736 for(mb_x=0; mb_x < s->mb_width; mb_x++) {
1737 const int mb_type= s->mb_type[mb_y * s->mb_width + mb_x];
1738 const int xy= (mb_y+1) * (s->mb_width+2) + mb_x + 1;
1746 s->block_index[0]+=2;
1747 s->block_index[1]+=2;
1748 s->block_index[2]+=2;
1749 s->block_index[3]+=2;
1750 s->block_index[4]++;
1751 s->block_index[5]++;
1752 if(mb_type & (mb_type-1)){ // more than 1 MB type possible
1756 copy_context_before_encode(&backup_s, s, -1);
1758 if(mb_type&MB_TYPE_INTER){
1759 s->mv_dir = MV_DIR_FORWARD;
1760 s->mv_type = MV_TYPE_16X16;
1762 s->mv[0][0][0] = s->p_mv_table[xy][0];
1763 s->mv[0][0][1] = s->p_mv_table[xy][1];
1764 init_put_bits(&s->pb, bit_buf[1], 3000, NULL, NULL);
1765 s->block= s->blocks[next_block];
1766 s->last_bits= 0; //done in copy_context_before_encode but we skip that here
1768 encode_mb(s, s->mv[0][0][0], s->mv[0][0][1]);
1769 d= get_bit_count(&s->pb);
1771 flush_put_bits(&s->pb);
1773 copy_context_after_encode(&best_s, s, MB_TYPE_INTER);
1778 if(mb_type&MB_TYPE_INTER4V){
1779 copy_context_before_encode(s, &backup_s, MB_TYPE_INTER4V);
1780 s->mv_dir = MV_DIR_FORWARD;
1781 s->mv_type = MV_TYPE_8X8;
1784 s->mv[0][i][0] = s->motion_val[s->block_index[i]][0];
1785 s->mv[0][i][1] = s->motion_val[s->block_index[i]][1];
1787 init_put_bits(&s->pb, bit_buf[2], 3000, NULL, NULL);
1788 s->block= s->blocks[next_block];
1791 d= get_bit_count(&s->pb);
1793 flush_put_bits(&s->pb);
1795 copy_context_after_encode(&best_s, s, MB_TYPE_INTER4V);
1800 if(mb_type&MB_TYPE_FORWARD){
1801 copy_context_before_encode(s, &backup_s, MB_TYPE_FORWARD);
1802 s->mv_dir = MV_DIR_FORWARD;
1803 s->mv_type = MV_TYPE_16X16;
1805 s->mv[0][0][0] = s->b_forw_mv_table[xy][0];
1806 s->mv[0][0][1] = s->b_forw_mv_table[xy][1];
1807 init_put_bits(&s->pb, bit_buf[3], 3000, NULL, NULL);
1808 s->block= s->blocks[next_block];
1810 encode_mb(s, s->mv[0][0][0], s->mv[0][0][1]);
1811 d= get_bit_count(&s->pb);
1813 flush_put_bits(&s->pb);
1815 copy_context_after_encode(&best_s, s, MB_TYPE_FORWARD);
1820 if(mb_type&MB_TYPE_BACKWARD){
1821 copy_context_before_encode(s, &backup_s, MB_TYPE_BACKWARD);
1822 s->mv_dir = MV_DIR_BACKWARD;
1823 s->mv_type = MV_TYPE_16X16;
1825 s->mv[1][0][0] = s->b_back_mv_table[xy][0];
1826 s->mv[1][0][1] = s->b_back_mv_table[xy][1];
1827 init_put_bits(&s->pb, bit_buf[4], 3000, NULL, NULL);
1828 s->block= s->blocks[next_block];
1830 encode_mb(s, s->mv[1][0][0], s->mv[1][0][1]);
1831 d= get_bit_count(&s->pb);
1833 flush_put_bits(&s->pb);
1835 copy_context_after_encode(&best_s, s, MB_TYPE_BACKWARD);
1840 if(mb_type&MB_TYPE_BIDIR){
1841 copy_context_before_encode(s, &backup_s, MB_TYPE_BIDIR);
1842 s->mv_dir = MV_DIR_FORWARD | MV_DIR_BACKWARD;
1843 s->mv_type = MV_TYPE_16X16;
1845 s->mv[0][0][0] = s->b_bidir_forw_mv_table[xy][0];
1846 s->mv[0][0][1] = s->b_bidir_forw_mv_table[xy][1];
1847 s->mv[1][0][0] = s->b_bidir_back_mv_table[xy][0];
1848 s->mv[1][0][1] = s->b_bidir_back_mv_table[xy][1];
1849 init_put_bits(&s->pb, bit_buf[5], 3000, NULL, NULL);
1850 s->block= s->blocks[next_block];
1853 d= get_bit_count(&s->pb);
1855 flush_put_bits(&s->pb);
1857 copy_context_after_encode(&best_s, s, MB_TYPE_BIDIR);
1862 if(mb_type&MB_TYPE_DIRECT){
1863 copy_context_before_encode(s, &backup_s, MB_TYPE_DIRECT);
1864 s->mv_dir = MV_DIR_FORWARD | MV_DIR_BACKWARD | MV_DIRECT;
1865 s->mv_type = MV_TYPE_16X16; //FIXME
1867 s->mv[0][0][0] = s->b_direct_forw_mv_table[xy][0];
1868 s->mv[0][0][1] = s->b_direct_forw_mv_table[xy][1];
1869 s->mv[1][0][0] = s->b_direct_back_mv_table[xy][0];
1870 s->mv[1][0][1] = s->b_direct_back_mv_table[xy][1];
1871 init_put_bits(&s->pb, bit_buf[6], 3000, NULL, NULL);
1872 s->block= s->blocks[next_block];
1874 encode_mb(s, s->b_direct_mv_table[xy][0], s->b_direct_mv_table[xy][1]);
1875 d= get_bit_count(&s->pb);
1877 flush_put_bits(&s->pb);
1879 copy_context_after_encode(&best_s, s, MB_TYPE_DIRECT);
1884 if(mb_type&MB_TYPE_INTRA){
1885 copy_context_before_encode(s, &backup_s, MB_TYPE_INTRA);
1886 s->mv_dir = MV_DIR_FORWARD;
1887 s->mv_type = MV_TYPE_16X16;
1891 init_put_bits(&s->pb, bit_buf[0], 3000, NULL, NULL);
1892 s->block= s->blocks[next_block];
1895 d= get_bit_count(&s->pb);
1897 flush_put_bits(&s->pb);
1899 copy_context_after_encode(&best_s, s, MB_TYPE_INTRA);
1903 /* force cleaning of ac/dc pred stuff if needed ... */
1904 if(s->h263_pred || s->h263_aic)
1905 s->mbintra_table[mb_x + mb_y*s->mb_width]=1;
1907 copy_context_after_encode(s, &best_s, -1);
1908 copy_bits(&pb, bit_buf[best], dmin);
1910 s->last_bits= get_bit_count(&s->pb);
1912 int motion_x, motion_y;
1913 s->mv_type=MV_TYPE_16X16;
1914 // only one MB-Type possible
1917 s->mv_dir = MV_DIR_FORWARD;
1919 motion_x= s->mv[0][0][0] = 0;
1920 motion_y= s->mv[0][0][1] = 0;
1923 s->mv_dir = MV_DIR_FORWARD;
1925 motion_x= s->mv[0][0][0] = s->p_mv_table[xy][0];
1926 motion_y= s->mv[0][0][1] = s->p_mv_table[xy][1];
1928 case MB_TYPE_DIRECT:
1929 s->mv_dir = MV_DIR_FORWARD | MV_DIR_BACKWARD | MV_DIRECT;
1931 motion_x=s->b_direct_mv_table[xy][0];
1932 motion_y=s->b_direct_mv_table[xy][1];
1933 s->mv[0][0][0] = s->b_direct_forw_mv_table[xy][0];
1934 s->mv[0][0][1] = s->b_direct_forw_mv_table[xy][1];
1935 s->mv[1][0][0] = s->b_direct_back_mv_table[xy][0];
1936 s->mv[1][0][1] = s->b_direct_back_mv_table[xy][1];
1939 s->mv_dir = MV_DIR_FORWARD | MV_DIR_BACKWARD;
1943 s->mv[0][0][0] = s->b_bidir_forw_mv_table[xy][0];
1944 s->mv[0][0][1] = s->b_bidir_forw_mv_table[xy][1];
1945 s->mv[1][0][0] = s->b_bidir_back_mv_table[xy][0];
1946 s->mv[1][0][1] = s->b_bidir_back_mv_table[xy][1];
1948 case MB_TYPE_BACKWARD:
1949 s->mv_dir = MV_DIR_BACKWARD;
1951 motion_x= s->mv[1][0][0] = s->b_back_mv_table[xy][0];
1952 motion_y= s->mv[1][0][1] = s->b_back_mv_table[xy][1];
1954 case MB_TYPE_FORWARD:
1955 s->mv_dir = MV_DIR_FORWARD;
1957 motion_x= s->mv[0][0][0] = s->b_forw_mv_table[xy][0];
1958 motion_y= s->mv[0][0][1] = s->b_forw_mv_table[xy][1];
1959 // printf(" %d %d ", motion_x, motion_y);
1962 motion_x=motion_y=0; //gcc warning fix
1963 printf("illegal MB type\n");
1965 encode_mb(s, motion_x, motion_y);
1967 /* clean the MV table in IPS frames for direct mode in B frames */
1968 if(s->mb_intra /* && I,P,S_TYPE */){
1969 s->p_mv_table[xy][0]=0;
1970 s->p_mv_table[xy][1]=0;
1973 MPV_decode_mb(s, s->block);
1977 /* Obtain average GOB size for RTP */
1980 s->mb_line_avgsize = pbBufPtr(&s->pb) - s->ptr_last_mb_line;
1981 else if (!(mb_y % s->gob_index)) {
1982 s->mb_line_avgsize = (s->mb_line_avgsize + pbBufPtr(&s->pb) - s->ptr_last_mb_line) >> 1;
1983 s->ptr_last_mb_line = pbBufPtr(&s->pb);
1985 //fprintf(stderr, "\nMB line: %d\tSize: %u\tAvg. Size: %u", s->mb_y,
1986 // (s->pb.buf_ptr - s->ptr_last_mb_line), s->mb_line_avgsize);
1987 s->first_gob_line = 0;
1992 if (s->h263_msmpeg4 && s->msmpeg4_version<4 && s->pict_type == I_TYPE)
1993 msmpeg4_encode_ext_header(s);
1995 //if (s->gob_number)
1996 // fprintf(stderr,"\nNumber of GOB: %d", s->gob_number);
1998 /* Send the last GOB if RTP */
2000 flush_put_bits(&s->pb);
2001 pdif = pbBufPtr(&s->pb) - s->ptr_lastgob;
2002 /* Call the RTP callback to send the last GOB */
2003 if (s->rtp_callback)
2004 s->rtp_callback(s->ptr_lastgob, pdif, s->gob_number);
2005 s->ptr_lastgob = pbBufPtr(&s->pb);
2006 //fprintf(stderr,"\nGOB: %2d size: %d (last)", s->gob_number, pdif);
2010 static int dct_quantize_c(MpegEncContext *s,
2011 DCTELEM *block, int n,
2012 int qscale, int *overflow)
2014 int i, j, level, last_non_zero, q;
2018 unsigned int threshold1, threshold2;
2022 /* we need this permutation so that we correct the IDCT
2023 permutation. will be moved into DCT code */
2024 block_permute(block);
2034 /* For AIC we skip quant/dequant of INTRADC */
2037 /* note: block[0] is assumed to be positive */
2038 block[0] = (block[0] + (q >> 1)) / q;
2041 qmat = s->q_intra_matrix[qscale];
2042 bias= s->intra_quant_bias<<(QMAT_SHIFT - 3 - QUANT_BIAS_SHIFT);
2046 qmat = s->q_inter_matrix[qscale];
2047 bias= s->inter_quant_bias<<(QMAT_SHIFT - 3 - QUANT_BIAS_SHIFT);
2049 threshold1= (1<<(QMAT_SHIFT - 3)) - bias - 1;
2050 threshold2= threshold1<<1;
2053 j = zigzag_direct[i];
2055 level = level * qmat[j];
2057 // if( bias+level >= (1<<(QMAT_SHIFT - 3))
2058 // || bias-level >= (1<<(QMAT_SHIFT - 3))){
2059 if(((unsigned)(level+threshold1))>threshold2){
2061 level= (bias + level)>>(QMAT_SHIFT - 3);
2064 level= (bias - level)>>(QMAT_SHIFT - 3);
2073 *overflow= s->max_qcoeff < max; //overflow might have happend
2075 return last_non_zero;
2078 static void dct_unquantize_mpeg1_c(MpegEncContext *s,
2079 DCTELEM *block, int n, int qscale)
2081 int i, level, nCoeffs;
2082 const UINT16 *quant_matrix;
2084 if(s->alternate_scan) nCoeffs= 64;
2085 else nCoeffs= s->block_last_index[n]+1;
2089 block[0] = block[0] * s->y_dc_scale;
2091 block[0] = block[0] * s->c_dc_scale;
2092 /* XXX: only mpeg1 */
2093 quant_matrix = s->intra_matrix;
2094 for(i=1;i<nCoeffs;i++) {
2095 int j= zigzag_direct[i];
2100 level = (int)(level * qscale * quant_matrix[j]) >> 3;
2101 level = (level - 1) | 1;
2104 level = (int)(level * qscale * quant_matrix[j]) >> 3;
2105 level = (level - 1) | 1;
2108 if (level < -2048 || level > 2047)
2109 fprintf(stderr, "unquant error %d %d\n", i, level);
2116 quant_matrix = s->inter_matrix;
2117 for(;i<nCoeffs;i++) {
2118 int j= zigzag_direct[i];
2123 level = (((level << 1) + 1) * qscale *
2124 ((int) (quant_matrix[j]))) >> 4;
2125 level = (level - 1) | 1;
2128 level = (((level << 1) + 1) * qscale *
2129 ((int) (quant_matrix[j]))) >> 4;
2130 level = (level - 1) | 1;
2133 if (level < -2048 || level > 2047)
2134 fprintf(stderr, "unquant error %d %d\n", i, level);
2142 static void dct_unquantize_mpeg2_c(MpegEncContext *s,
2143 DCTELEM *block, int n, int qscale)
2145 int i, level, nCoeffs;
2146 const UINT16 *quant_matrix;
2148 if(s->alternate_scan) nCoeffs= 64;
2149 else nCoeffs= s->block_last_index[n]+1;
2153 block[0] = block[0] * s->y_dc_scale;
2155 block[0] = block[0] * s->c_dc_scale;
2156 quant_matrix = s->intra_matrix;
2157 for(i=1;i<nCoeffs;i++) {
2158 int j= zigzag_direct[i];
2163 level = (int)(level * qscale * quant_matrix[j]) >> 3;
2166 level = (int)(level * qscale * quant_matrix[j]) >> 3;
2169 if (level < -2048 || level > 2047)
2170 fprintf(stderr, "unquant error %d %d\n", i, level);
2178 quant_matrix = s->inter_matrix;
2179 for(;i<nCoeffs;i++) {
2180 int j= zigzag_direct[i];
2185 level = (((level << 1) + 1) * qscale *
2186 ((int) (quant_matrix[j]))) >> 4;
2189 level = (((level << 1) + 1) * qscale *
2190 ((int) (quant_matrix[j]))) >> 4;
2193 if (level < -2048 || level > 2047)
2194 fprintf(stderr, "unquant error %d %d\n", i, level);
2205 static void dct_unquantize_h263_c(MpegEncContext *s,
2206 DCTELEM *block, int n, int qscale)
2208 int i, level, qmul, qadd;
2214 block[0] = block[0] * s->y_dc_scale;
2216 block[0] = block[0] * s->c_dc_scale;
2219 nCoeffs= 64; //does not allways use zigzag table
2222 nCoeffs= zigzag_end[ s->block_last_index[n] ];
2225 qmul = s->qscale << 1;
2226 if (s->h263_aic && s->mb_intra)
2229 qadd = (s->qscale - 1) | 1;
2231 for(;i<nCoeffs;i++) {
2235 level = level * qmul - qadd;
2237 level = level * qmul + qadd;
2240 if (level < -2048 || level > 2047)
2241 fprintf(stderr, "unquant error %d %d\n", i, level);
2248 AVCodec mpeg1video_encoder = {
2251 CODEC_ID_MPEG1VIDEO,
2252 sizeof(MpegEncContext),
2258 AVCodec h263_encoder = {
2262 sizeof(MpegEncContext),
2268 AVCodec h263p_encoder = {
2272 sizeof(MpegEncContext),
2278 AVCodec rv10_encoder = {
2282 sizeof(MpegEncContext),
2288 AVCodec mjpeg_encoder = {
2292 sizeof(MpegEncContext),
2298 AVCodec mpeg4_encoder = {
2302 sizeof(MpegEncContext),
2308 AVCodec msmpeg4v1_encoder = {
2312 sizeof(MpegEncContext),
2318 AVCodec msmpeg4v2_encoder = {
2322 sizeof(MpegEncContext),
2328 AVCodec msmpeg4v3_encoder = {
2332 sizeof(MpegEncContext),