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);
43 int (*dct_quantize)(MpegEncContext *s, DCTELEM *block, int n, int qscale)= 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, UINT16 *qmat16, const UINT16 *quant_matrix, int qscale)
85 if (av_fdct == jpeg_fdct_ifast) {
87 /* 16 <= qscale * quant_matrix[i] <= 7905 */
88 /* 19952 <= aanscales[i] * qscale * quant_matrix[i] <= 249205026 */
89 /* (1<<36)/19952 >= (1<<36)/(aanscales[i] * qscale * quant_matrix[i]) >= (1<<36)/249205026 */
90 /* 3444240 >= (1<<36)/(aanscales[i] * qscale * quant_matrix[i]) >= 275 */
92 qmat[block_permute_op(i)] = (int)((UINT64_C(1) << (QMAT_SHIFT + 11)) /
93 (aanscales[i] * qscale * quant_matrix[block_permute_op(i)]));
97 /* We can safely suppose that 16 <= quant_matrix[i] <= 255
98 So 16 <= qscale * quant_matrix[i] <= 7905
99 so (1<<19) / 16 >= (1<<19) / (qscale * quant_matrix[i]) >= (1<<19) / 7905
100 so 32768 >= (1<<19) / (qscale * quant_matrix[i]) >= 67
102 qmat[i] = (1 << QMAT_SHIFT_MMX) / (qscale * quant_matrix[i]);
103 qmat16[i] = (1 << QMAT_SHIFT_MMX) / (qscale * quant_matrix[block_permute_op(i)]);
108 /* init common structure for both encoder and decoder */
109 int MPV_common_init(MpegEncContext *s)
114 s->dct_unquantize_h263 = dct_unquantize_h263_c;
115 s->dct_unquantize_mpeg1 = dct_unquantize_mpeg1_c;
116 s->dct_unquantize_mpeg2 = dct_unquantize_mpeg2_c;
119 MPV_common_init_mmx(s);
121 //setup default unquantizers (mpeg4 might change it later)
122 if(s->out_format == FMT_H263)
123 s->dct_unquantize = s->dct_unquantize_h263;
125 s->dct_unquantize = s->dct_unquantize_mpeg1;
127 s->mb_width = (s->width + 15) / 16;
128 s->mb_height = (s->height + 15) / 16;
129 s->mb_num = s->mb_width * s->mb_height;
130 s->linesize = s->mb_width * 16 + 2 * EDGE_WIDTH;
133 int w, h, shift, pict_start;
136 h = s->mb_height * 16 + 2 * EDGE_WIDTH;
137 shift = (i == 0) ? 0 : 1;
138 c_size = (w >> shift) * (h >> shift);
139 pict_start = (w >> shift) * (EDGE_WIDTH >> shift) + (EDGE_WIDTH >> shift);
141 pict = av_mallocz(c_size);
144 s->last_picture_base[i] = pict;
145 s->last_picture[i] = pict + pict_start;
147 pict = av_mallocz(c_size);
150 s->next_picture_base[i] = pict;
151 s->next_picture[i] = pict + pict_start;
153 if (s->has_b_frames) {
154 pict = av_mallocz(c_size);
157 s->aux_picture_base[i] = pict;
158 s->aux_picture[i] = pict + pict_start;
164 int mv_table_size= (s->mb_width+2)*(s->mb_height+2);
166 /* Allocate MB type table */
167 s->mb_type = av_mallocz(s->mb_num * sizeof(char));
168 if (s->mb_type == NULL) {
173 s->mb_var = av_mallocz(s->mb_num * sizeof(INT16));
174 if (s->mb_var == NULL) {
179 /* Allocate MV tables */
180 s->p_mv_table = av_mallocz(mv_table_size * 2 * sizeof(INT16));
181 if (s->p_mv_table == NULL) {
185 s->last_p_mv_table = av_mallocz(mv_table_size * 2 * sizeof(INT16));
186 if (s->last_p_mv_table == NULL) {
190 s->b_forw_mv_table = av_mallocz(mv_table_size * 2 * sizeof(INT16));
191 if (s->b_forw_mv_table == NULL) {
195 s->b_back_mv_table = av_mallocz(mv_table_size * 2 * sizeof(INT16));
196 if (s->b_back_mv_table == NULL) {
200 s->b_bidir_forw_mv_table = av_mallocz(mv_table_size * 2 * sizeof(INT16));
201 if (s->b_bidir_forw_mv_table == NULL) {
205 s->b_bidir_back_mv_table = av_mallocz(mv_table_size * 2 * sizeof(INT16));
206 if (s->b_bidir_back_mv_table == NULL) {
210 s->b_direct_forw_mv_table = av_mallocz(mv_table_size * 2 * sizeof(INT16));
211 if (s->b_direct_forw_mv_table == NULL) {
215 s->b_direct_back_mv_table = av_mallocz(mv_table_size * 2 * sizeof(INT16));
216 if (s->b_direct_back_mv_table == NULL) {
220 s->b_direct_mv_table = av_mallocz(mv_table_size * 2 * sizeof(INT16));
221 if (s->b_direct_mv_table == NULL) {
226 s->me_scratchpad = av_mallocz( s->linesize*16*3*sizeof(uint8_t));
227 if (s->me_scratchpad == NULL) {
233 for(j=0; j<REORDER_BUFFER_SIZE; j++){
239 h = s->mb_height * 16;
240 shift = (i == 0) ? 0 : 1;
241 c_size = (w >> shift) * (h >> shift);
243 pict = av_mallocz(c_size);
246 s->picture_buffer[j][i] = pict;
252 if (s->out_format == FMT_H263 || s->encoding) {
255 size = (2 * s->mb_width + 2) * (2 * s->mb_height + 2);
256 s->motion_val = malloc(size * 2 * sizeof(INT16));
257 if (s->motion_val == NULL)
259 memset(s->motion_val, 0, size * 2 * sizeof(INT16));
262 if (s->h263_pred || s->h263_plus) {
263 int y_size, c_size, i, size;
267 y_size = (2 * s->mb_width + 2) * (2 * s->mb_height + 2);
268 c_size = (s->mb_width + 2) * (s->mb_height + 2);
269 size = y_size + 2 * c_size;
270 s->dc_val[0] = malloc(size * sizeof(INT16));
271 if (s->dc_val[0] == NULL)
273 s->dc_val[1] = s->dc_val[0] + y_size;
274 s->dc_val[2] = s->dc_val[1] + c_size;
276 s->dc_val[0][i] = 1024;
279 s->ac_val[0] = av_mallocz(size * sizeof(INT16) * 16);
280 if (s->ac_val[0] == NULL)
282 s->ac_val[1] = s->ac_val[0] + y_size;
283 s->ac_val[2] = s->ac_val[1] + c_size;
286 s->coded_block = av_mallocz(y_size);
290 /* which mb is a intra block */
291 s->mbintra_table = av_mallocz(s->mb_num);
292 if (!s->mbintra_table)
294 memset(s->mbintra_table, 1, s->mb_num);
296 /* divx501 bitstream reorder buffer */
297 s->bitstream_buffer= av_mallocz(BITSTREAM_BUFFER_SIZE);
298 if (!s->bitstream_buffer)
301 /* default structure is frame */
302 s->picture_structure = PICT_FRAME;
304 /* init macroblock skip table */
305 s->mbskip_table = av_mallocz(s->mb_num);
306 if (!s->mbskip_table)
309 s->block= s->blocks[0];
311 s->context_initialized = 1;
318 #define CHECK_FREE(p)\
324 /* init common structure for both encoder and decoder */
325 void MPV_common_end(MpegEncContext *s)
329 CHECK_FREE(s->mb_type);
330 CHECK_FREE(s->mb_var);
331 CHECK_FREE(s->p_mv_table);
332 CHECK_FREE(s->last_p_mv_table);
333 CHECK_FREE(s->b_forw_mv_table);
334 CHECK_FREE(s->b_back_mv_table);
335 CHECK_FREE(s->b_bidir_forw_mv_table);
336 CHECK_FREE(s->b_bidir_back_mv_table);
337 CHECK_FREE(s->b_direct_forw_mv_table);
338 CHECK_FREE(s->b_direct_back_mv_table);
339 CHECK_FREE(s->b_direct_mv_table);
340 CHECK_FREE(s->motion_val);
341 CHECK_FREE(s->dc_val[0]);
342 CHECK_FREE(s->ac_val[0]);
343 CHECK_FREE(s->coded_block);
344 CHECK_FREE(s->mbintra_table);
345 CHECK_FREE(s->me_scratchpad);
347 CHECK_FREE(s->mbskip_table);
348 CHECK_FREE(s->bitstream_buffer);
351 CHECK_FREE(s->last_picture_base[i]);
352 CHECK_FREE(s->next_picture_base[i]);
353 CHECK_FREE(s->aux_picture_base[i]);
354 for(j=0; j<REORDER_BUFFER_SIZE; j++){
355 CHECK_FREE(s->picture_buffer[j][i]);
358 s->context_initialized = 0;
361 /* init video encoder */
362 int MPV_encode_init(AVCodecContext *avctx)
364 MpegEncContext *s = avctx->priv_data;
367 avctx->pix_fmt = PIX_FMT_YUV420P;
369 s->bit_rate = avctx->bit_rate;
370 s->bit_rate_tolerance = avctx->bit_rate_tolerance;
371 s->frame_rate = avctx->frame_rate;
372 s->width = avctx->width;
373 s->height = avctx->height;
374 s->gop_size = avctx->gop_size;
375 s->rtp_mode = avctx->rtp_mode;
376 s->rtp_payload_size = avctx->rtp_payload_size;
377 if (avctx->rtp_callback)
378 s->rtp_callback = avctx->rtp_callback;
379 s->qmin= avctx->qmin;
380 s->qmax= avctx->qmax;
381 s->max_qdiff= avctx->max_qdiff;
382 s->qcompress= avctx->qcompress;
383 s->qblur= avctx->qblur;
384 s->b_quant_factor= avctx->b_quant_factor;
386 s->aspect_ratio_info= avctx->aspect_ratio_info;
387 s->flags= avctx->flags;
388 s->max_b_frames= avctx->max_b_frames;
389 s->rc_strategy= avctx->rc_strategy;
390 s->b_frame_strategy= avctx->b_frame_strategy;
392 if (s->gop_size <= 1) {
400 if (avctx->me_method == 0)
401 /* For compatibility */
402 s->me_method = motion_estimation_method;
404 s->me_method = avctx->me_method;
407 s->fixed_qscale = (avctx->flags & CODEC_FLAG_QSCALE);
409 switch(avctx->codec->id) {
410 case CODEC_ID_MPEG1VIDEO:
411 s->out_format = FMT_MPEG1;
414 s->out_format = FMT_MJPEG;
415 s->intra_only = 1; /* force intra only for jpeg */
416 s->mjpeg_write_tables = 1; /* write all tables */
417 s->mjpeg_vsample[0] = 2; /* set up default sampling factors */
418 s->mjpeg_vsample[1] = 1; /* the only currently supported values */
419 s->mjpeg_vsample[2] = 1;
420 s->mjpeg_hsample[0] = 2;
421 s->mjpeg_hsample[1] = 1;
422 s->mjpeg_hsample[2] = 1;
423 if (mjpeg_init(s) < 0)
427 if (h263_get_picture_format(s->width, s->height) == 7) {
428 printf("Input picture size isn't suitable for h263 codec! try h263+\n");
431 s->out_format = FMT_H263;
434 s->out_format = FMT_H263;
436 s->rtp_payload_size = 1200;
438 s->unrestricted_mv = 1;
440 /* These are just to be sure */
445 s->out_format = FMT_H263;
449 s->out_format = FMT_H263;
451 s->unrestricted_mv = 1;
452 s->has_b_frames= s->max_b_frames ? 1 : 0;
454 case CODEC_ID_MSMPEG4V1:
455 s->out_format = FMT_H263;
458 s->unrestricted_mv = 1;
459 s->msmpeg4_version= 1;
461 case CODEC_ID_MSMPEG4V2:
462 s->out_format = FMT_H263;
465 s->unrestricted_mv = 1;
466 s->msmpeg4_version= 2;
468 case CODEC_ID_MSMPEG4V3:
469 s->out_format = FMT_H263;
472 s->unrestricted_mv = 1;
473 s->msmpeg4_version= 3;
479 if((s->flags&CODEC_FLAG_4MV) && !(s->flags&CODEC_FLAG_HQ)){
480 printf("4MV is currently only supported in HQ mode\n");
484 { /* set up some save defaults, some codecs might override them later */
489 memset(default_mv_penalty, 0, sizeof(UINT16)*(MAX_FCODE+1)*(2*MAX_MV+1));
490 memset(default_fcode_tab , 0, sizeof(UINT8)*(2*MAX_MV+1));
492 for(i=-16; i<16; i++){
493 default_fcode_tab[i + MAX_MV]= 1;
497 s->mv_penalty= default_mv_penalty;
498 s->fcode_tab= default_fcode_tab;
500 if (s->out_format == FMT_H263)
502 else if (s->out_format == FMT_MPEG1)
503 mpeg1_encode_init(s);
505 /* dont use mv_penalty table for crap MV as it would be confused */
506 if (s->me_method < ME_EPZS) s->mv_penalty = default_mv_penalty;
511 if (MPV_common_init(s) < 0)
514 /* init default q matrix */
516 s->intra_matrix[i] = default_intra_matrix[i];
517 s->non_intra_matrix[i] = default_non_intra_matrix[i];
520 if(ff_rate_control_init(s) < 0)
523 s->picture_number = 0;
524 s->picture_in_gop_number = 0;
525 s->fake_picture_number = 0;
526 /* motion detector init */
533 int MPV_encode_end(AVCodecContext *avctx)
535 MpegEncContext *s = avctx->priv_data;
541 ff_rate_control_uninit(s);
544 if (s->out_format == FMT_MJPEG)
550 /* draw the edges of width 'w' of an image of size width, height */
551 static void draw_edges_c(UINT8 *buf, int wrap, int width, int height, int w)
553 UINT8 *ptr, *last_line;
556 last_line = buf + (height - 1) * wrap;
559 memcpy(buf - (i + 1) * wrap, buf, width);
560 memcpy(last_line + (i + 1) * wrap, last_line, width);
564 for(i=0;i<height;i++) {
565 memset(ptr - w, ptr[0], w);
566 memset(ptr + width, ptr[width-1], w);
571 memset(buf - (i + 1) * wrap - w, buf[0], w); /* top left */
572 memset(buf - (i + 1) * wrap + width, buf[width-1], w); /* top right */
573 memset(last_line + (i + 1) * wrap - w, last_line[0], w); /* top left */
574 memset(last_line + (i + 1) * wrap + width, last_line[width-1], w); /* top right */
578 /* generic function for encode/decode called before a frame is coded/decoded */
579 void MPV_frame_start(MpegEncContext *s)
585 if (s->pict_type == B_TYPE) {
587 s->current_picture[i] = s->aux_picture[i];
591 /* swap next and last */
592 tmp = s->last_picture[i];
593 s->last_picture[i] = s->next_picture[i];
594 s->next_picture[i] = tmp;
595 s->current_picture[i] = tmp;
600 /* generic function for encode/decode called after a frame has been coded/decoded */
601 void MPV_frame_end(MpegEncContext *s)
603 /* draw edge for correct motion prediction if outside */
604 if (s->pict_type != B_TYPE && !s->intra_only) {
605 if(s->avctx==NULL || s->avctx->codec->id!=CODEC_ID_MPEG4 || s->divx_version==500){
606 draw_edges(s->current_picture[0], s->linesize, s->mb_width*16, s->mb_height*16, EDGE_WIDTH);
607 draw_edges(s->current_picture[1], s->linesize/2, s->mb_width*8, s->mb_height*8, EDGE_WIDTH/2);
608 draw_edges(s->current_picture[2], s->linesize/2, s->mb_width*8, s->mb_height*8, EDGE_WIDTH/2);
610 /* mpeg4? / opendivx / xvid */
611 draw_edges(s->current_picture[0], s->linesize, s->width, s->height, EDGE_WIDTH);
612 draw_edges(s->current_picture[1], s->linesize/2, s->width/2, s->height/2, EDGE_WIDTH/2);
613 draw_edges(s->current_picture[2], s->linesize/2, s->width/2, s->height/2, EDGE_WIDTH/2);
618 if(s->pict_type!=B_TYPE){
619 s->last_non_b_pict_type= s->pict_type;
620 s->last_non_b_qscale= s->qscale;
621 s->last_non_b_mc_mb_var= s->mc_mb_var;
625 /* reorder input for encoding */
626 void reorder_input(MpegEncContext *s, AVPicture *pict)
630 if(s->max_b_frames > FF_MAX_B_FRAMES) s->max_b_frames= FF_MAX_B_FRAMES;
632 // delay= s->max_b_frames+1; (or 0 if no b frames cuz decoder diff)
634 for(j=0; j<REORDER_BUFFER_SIZE-1; j++){
635 s->coded_order[j]= s->coded_order[j+1];
637 s->coded_order[j].picture[0]= s->coded_order[j].picture[1]= s->coded_order[j].picture[2]= NULL; //catch uninitalized buffers
639 switch(s->input_pict_type){
644 index= s->max_b_frames - s->b_frames_since_non_b;
645 s->b_frames_since_non_b=0;
648 index= s->max_b_frames + 1;
649 s->b_frames_since_non_b++;
652 //printf("index:%d type:%d strides: %d %d\n", index, s->input_pict_type, pict->linesize[0], s->linesize);
653 if( (index==0 || (s->flags&CODEC_FLAG_INPUT_PRESERVED))
654 && pict->linesize[0] == s->linesize
655 && pict->linesize[1] == s->linesize>>1
656 && pict->linesize[2] == s->linesize>>1){
659 s->coded_order[index].picture[i]= pict->data[i];
664 uint8_t *src = pict->data[i];
666 int src_wrap = pict->linesize[i];
667 int dest_wrap = s->linesize;
671 if(index==0) dest= s->last_picture[i]+16; //is current_picture indeed but the switch hapens after reordering
672 else dest= s->picture_buffer[s->picture_buffer_index][i];
680 s->coded_order[index].picture[i]= dest;
682 memcpy(dest, src, w);
688 s->picture_buffer_index++;
689 if(s->picture_buffer_index >= REORDER_BUFFER_SIZE-1) s->picture_buffer_index=0;
692 s->coded_order[index].pict_type = s->input_pict_type;
693 s->coded_order[index].qscale = s->input_qscale;
694 s->coded_order[index].force_type= s->force_input_type;
695 s->coded_order[index].picture_in_gop_number= s->input_picture_in_gop_number;
696 s->coded_order[index].picture_number= s->input_picture_number;
699 s->new_picture[i]= s->coded_order[0].picture[i];
703 int MPV_encode_picture(AVCodecContext *avctx,
704 unsigned char *buf, int buf_size, void *data)
706 MpegEncContext *s = avctx->priv_data;
707 AVPicture *pict = data;
709 s->input_qscale = avctx->quality;
711 init_put_bits(&s->pb, buf, buf_size, NULL, NULL);
713 if(avctx->flags&CODEC_FLAG_TYPE){
715 s->force_input_type= avctx->key_frame ? I_TYPE : P_TYPE;
716 }else if(s->flags&CODEC_FLAG_PASS2){
718 s->force_input_type= s->rc_context.entry[s->input_picture_number].new_pict_type;
720 s->force_input_type=0;
721 if (!s->intra_only) {
722 /* first picture of GOP is intra */
723 if (s->input_picture_in_gop_number % s->gop_size==0){
724 s->input_pict_type = I_TYPE;
725 }else if(s->max_b_frames==0){
726 s->input_pict_type = P_TYPE;
728 if(s->b_frames_since_non_b < s->max_b_frames) //FIXME more IQ
729 s->input_pict_type = B_TYPE;
731 s->input_pict_type = P_TYPE;
734 s->input_pict_type = I_TYPE;
738 if(s->input_pict_type==I_TYPE)
739 s->input_picture_in_gop_number=0;
741 reorder_input(s, pict);
744 if(s->coded_order[0].picture[0]){
746 s->pict_type= s->coded_order[0].pict_type;
747 if (s->fixed_qscale) /* the ratecontrol needs the last qscale so we dont touch it for CBR */
748 s->qscale= s->coded_order[0].qscale;
749 s->force_type= s->coded_order[0].force_type;
750 s->picture_in_gop_number= s->coded_order[0].picture_in_gop_number;
751 s->picture_number= s->coded_order[0].picture_number;
755 encode_picture(s, s->picture_number);
756 avctx->key_frame = (s->pict_type == I_TYPE);
757 avctx->header_bits = s->header_bits;
758 avctx->mv_bits = s->mv_bits;
759 avctx->misc_bits = s->misc_bits;
760 avctx->i_tex_bits = s->i_tex_bits;
761 avctx->p_tex_bits = s->p_tex_bits;
762 avctx->i_count = s->i_count;
763 avctx->p_count = s->p_count;
764 avctx->skip_count = s->skip_count;
768 if (s->out_format == FMT_MJPEG)
769 mjpeg_picture_trailer(s);
771 avctx->quality = s->qscale;
773 if(s->flags&CODEC_FLAG_PASS1)
774 ff_write_pass1_stats(s);
777 s->input_picture_number++;
778 s->input_picture_in_gop_number++;
780 flush_put_bits(&s->pb);
781 s->frame_bits = (pbBufPtr(&s->pb) - s->pb.buf) * 8;
782 if(s->pict_type==B_TYPE) s->pb_frame_bits+= s->frame_bits;
783 else s->pb_frame_bits= s->frame_bits;
785 s->total_bits += s->frame_bits;
786 avctx->frame_bits = s->frame_bits;
787 //printf("fcode: %d, type: %d, head: %d, mv: %d, misc: %d, frame: %d, itex: %d, ptex: %d\n",
788 //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);
790 if (avctx->get_psnr) {
791 /* At this point pict->data should have the original frame */
792 /* an s->current_picture should have the coded/decoded frame */
793 get_psnr(pict->data, s->current_picture,
794 pict->linesize, s->linesize, avctx);
795 // printf("%f\n", avctx->psnr_y);
797 return pbBufPtr(&s->pb) - s->pb.buf;
800 static inline void gmc1_motion(MpegEncContext *s,
801 UINT8 *dest_y, UINT8 *dest_cb, UINT8 *dest_cr,
803 UINT8 **ref_picture, int src_offset,
807 int offset, src_x, src_y, linesize;
808 int motion_x, motion_y;
810 if(s->real_sprite_warping_points>1) printf("more than 1 warp point isnt supported\n");
811 motion_x= s->sprite_offset[0][0];
812 motion_y= s->sprite_offset[0][1];
813 src_x = s->mb_x * 16 + (motion_x >> (s->sprite_warping_accuracy+1));
814 src_y = s->mb_y * 16 + (motion_y >> (s->sprite_warping_accuracy+1));
815 motion_x<<=(3-s->sprite_warping_accuracy);
816 motion_y<<=(3-s->sprite_warping_accuracy);
817 src_x = clip(src_x, -16, s->width);
818 if (src_x == s->width)
820 src_y = clip(src_y, -16, s->height);
821 if (src_y == s->height)
824 linesize = s->linesize;
825 ptr = ref_picture[0] + (src_y * linesize) + src_x + src_offset;
828 gmc1(dest_y , ptr , linesize, h, motion_x&15, motion_y&15, s->no_rounding);
829 gmc1(dest_y+8, ptr+8, linesize, h, motion_x&15, motion_y&15, s->no_rounding);
831 motion_x= s->sprite_offset[1][0];
832 motion_y= s->sprite_offset[1][1];
833 src_x = s->mb_x * 8 + (motion_x >> (s->sprite_warping_accuracy+1));
834 src_y = s->mb_y * 8 + (motion_y >> (s->sprite_warping_accuracy+1));
835 motion_x<<=(3-s->sprite_warping_accuracy);
836 motion_y<<=(3-s->sprite_warping_accuracy);
837 src_x = clip(src_x, -8, s->width>>1);
838 if (src_x == s->width>>1)
840 src_y = clip(src_y, -8, s->height>>1);
841 if (src_y == s->height>>1)
844 offset = (src_y * linesize>>1) + src_x + (src_offset>>1);
845 ptr = ref_picture[1] + offset;
846 gmc1(dest_cb + (dest_offset>>1), ptr, linesize>>1, h>>1, motion_x&15, motion_y&15, s->no_rounding);
847 ptr = ref_picture[2] + offset;
848 gmc1(dest_cr + (dest_offset>>1), ptr, linesize>>1, h>>1, motion_x&15, motion_y&15, s->no_rounding);
853 /* apply one mpeg motion vector to the three components */
854 static inline void mpeg_motion(MpegEncContext *s,
855 UINT8 *dest_y, UINT8 *dest_cb, UINT8 *dest_cr,
857 UINT8 **ref_picture, int src_offset,
858 int field_based, op_pixels_func *pix_op,
859 int motion_x, int motion_y, int h)
862 int dxy, offset, mx, my, src_x, src_y, height, linesize;
863 if(s->quarter_sample)
869 dxy = ((motion_y & 1) << 1) | (motion_x & 1);
870 src_x = s->mb_x * 16 + (motion_x >> 1);
871 src_y = s->mb_y * (16 >> field_based) + (motion_y >> 1);
873 /* WARNING: do no forget half pels */
874 height = s->height >> field_based;
875 src_x = clip(src_x, -16, s->width);
876 if (src_x == s->width)
878 src_y = clip(src_y, -16, height);
881 linesize = s->linesize << field_based;
882 ptr = ref_picture[0] + (src_y * linesize) + (src_x) + src_offset;
883 dest_y += dest_offset;
884 pix_op[dxy](dest_y, ptr, linesize, h);
885 pix_op[dxy](dest_y + 8, ptr + 8, linesize, h);
887 if (s->out_format == FMT_H263) {
889 if ((motion_x & 3) != 0)
891 if ((motion_y & 3) != 0)
898 dxy = ((my & 1) << 1) | (mx & 1);
903 src_x = s->mb_x * 8 + mx;
904 src_y = s->mb_y * (8 >> field_based) + my;
905 src_x = clip(src_x, -8, s->width >> 1);
906 if (src_x == (s->width >> 1))
908 src_y = clip(src_y, -8, height >> 1);
909 if (src_y == (height >> 1))
912 offset = (src_y * (linesize >> 1)) + src_x + (src_offset >> 1);
913 ptr = ref_picture[1] + offset;
914 pix_op[dxy](dest_cb + (dest_offset >> 1), ptr, linesize >> 1, h >> 1);
915 ptr = ref_picture[2] + offset;
916 pix_op[dxy](dest_cr + (dest_offset >> 1), ptr, linesize >> 1, h >> 1);
919 static inline void qpel_motion(MpegEncContext *s,
920 UINT8 *dest_y, UINT8 *dest_cb, UINT8 *dest_cr,
922 UINT8 **ref_picture, int src_offset,
923 int field_based, op_pixels_func *pix_op,
924 qpel_mc_func *qpix_op,
925 int motion_x, int motion_y, int h)
928 int dxy, offset, mx, my, src_x, src_y, height, linesize;
930 dxy = ((motion_y & 3) << 2) | (motion_x & 3);
931 src_x = s->mb_x * 16 + (motion_x >> 2);
932 src_y = s->mb_y * (16 >> field_based) + (motion_y >> 2);
934 height = s->height >> field_based;
935 src_x = clip(src_x, -16, s->width);
936 if (src_x == s->width)
938 src_y = clip(src_y, -16, height);
941 linesize = s->linesize << field_based;
942 ptr = ref_picture[0] + (src_y * linesize) + src_x + src_offset;
943 dest_y += dest_offset;
944 //printf("%d %d %d\n", src_x, src_y, dxy);
945 qpix_op[dxy](dest_y , ptr , linesize, linesize, motion_x&3, motion_y&3);
946 qpix_op[dxy](dest_y + 8, ptr + 8, linesize, linesize, motion_x&3, motion_y&3);
947 qpix_op[dxy](dest_y + linesize*8 , ptr + linesize*8 , linesize, linesize, motion_x&3, motion_y&3);
948 qpix_op[dxy](dest_y + linesize*8 + 8, ptr + linesize*8 + 8, linesize, linesize, motion_x&3, motion_y&3);
950 mx= (motion_x>>1) | (motion_x&1);
951 my= (motion_y>>1) | (motion_y&1);
961 src_x = s->mb_x * 8 + mx;
962 src_y = s->mb_y * (8 >> field_based) + my;
963 src_x = clip(src_x, -8, s->width >> 1);
964 if (src_x == (s->width >> 1))
966 src_y = clip(src_y, -8, height >> 1);
967 if (src_y == (height >> 1))
970 offset = (src_y * (linesize >> 1)) + src_x + (src_offset >> 1);
971 ptr = ref_picture[1] + offset;
972 pix_op[dxy](dest_cb + (dest_offset >> 1), ptr, linesize >> 1, h >> 1);
973 ptr = ref_picture[2] + offset;
974 pix_op[dxy](dest_cr + (dest_offset >> 1), ptr, linesize >> 1, h >> 1);
978 static inline void MPV_motion(MpegEncContext *s,
979 UINT8 *dest_y, UINT8 *dest_cb, UINT8 *dest_cr,
980 int dir, UINT8 **ref_picture,
981 op_pixels_func *pix_op, qpel_mc_func *qpix_op)
983 int dxy, offset, mx, my, src_x, src_y, motion_x, motion_y;
994 mpeg_motion(s, dest_y, dest_cb, dest_cr, 0,
997 s->sprite_offset[0][0]>>3,
998 s->sprite_offset[0][1]>>3,
1001 gmc1_motion(s, dest_y, dest_cb, dest_cr, 0,
1005 }else if(s->quarter_sample && dir==0){ //FIXME
1006 qpel_motion(s, dest_y, dest_cb, dest_cr, 0,
1009 s->mv[dir][0][0], s->mv[dir][0][1], 16);
1011 mpeg_motion(s, dest_y, dest_cb, dest_cr, 0,
1014 s->mv[dir][0][0], s->mv[dir][0][1], 16);
1019 motion_x = s->mv[dir][i][0];
1020 motion_y = s->mv[dir][i][1];
1022 dxy = ((motion_y & 1) << 1) | (motion_x & 1);
1023 src_x = mb_x * 16 + (motion_x >> 1) + (i & 1) * 8;
1024 src_y = mb_y * 16 + (motion_y >> 1) + (i >>1) * 8;
1026 /* WARNING: do no forget half pels */
1027 src_x = clip(src_x, -16, s->width);
1028 if (src_x == s->width)
1030 src_y = clip(src_y, -16, s->height);
1031 if (src_y == s->height)
1034 ptr = ref_picture[0] + (src_y * s->linesize) + (src_x);
1035 dest = dest_y + ((i & 1) * 8) + (i >> 1) * 8 * s->linesize;
1036 pix_op[dxy](dest, ptr, s->linesize, 8);
1038 /* In case of 8X8, we construct a single chroma motion vector
1039 with a special rounding */
1043 mx += s->mv[dir][i][0];
1044 my += s->mv[dir][i][1];
1047 mx = (h263_chroma_roundtab[mx & 0xf] + ((mx >> 3) & ~1));
1050 mx = -(h263_chroma_roundtab[mx & 0xf] + ((mx >> 3) & ~1));
1053 my = (h263_chroma_roundtab[my & 0xf] + ((my >> 3) & ~1));
1056 my = -(h263_chroma_roundtab[my & 0xf] + ((my >> 3) & ~1));
1058 dxy = ((my & 1) << 1) | (mx & 1);
1062 src_x = mb_x * 8 + mx;
1063 src_y = mb_y * 8 + my;
1064 src_x = clip(src_x, -8, s->width/2);
1065 if (src_x == s->width/2)
1067 src_y = clip(src_y, -8, s->height/2);
1068 if (src_y == s->height/2)
1071 offset = (src_y * (s->linesize >> 1)) + src_x;
1072 ptr = ref_picture[1] + offset;
1073 pix_op[dxy](dest_cb, ptr, s->linesize >> 1, 8);
1074 ptr = ref_picture[2] + offset;
1075 pix_op[dxy](dest_cr, ptr, s->linesize >> 1, 8);
1078 if (s->picture_structure == PICT_FRAME) {
1080 mpeg_motion(s, dest_y, dest_cb, dest_cr, 0,
1081 ref_picture, s->field_select[dir][0] ? s->linesize : 0,
1083 s->mv[dir][0][0], s->mv[dir][0][1], 8);
1085 mpeg_motion(s, dest_y, dest_cb, dest_cr, s->linesize,
1086 ref_picture, s->field_select[dir][1] ? s->linesize : 0,
1088 s->mv[dir][1][0], s->mv[dir][1][1], 8);
1098 /* put block[] to dest[] */
1099 static inline void put_dct(MpegEncContext *s,
1100 DCTELEM *block, int i, UINT8 *dest, int line_size)
1103 s->dct_unquantize(s, block, i, s->qscale);
1105 put_pixels_clamped(block, dest, line_size);
1108 /* add block[] to dest[] */
1109 static inline void add_dct(MpegEncContext *s,
1110 DCTELEM *block, int i, UINT8 *dest, int line_size)
1112 if (s->block_last_index[i] >= 0) {
1114 if(s->encoding || (!s->h263_msmpeg4))
1115 s->dct_unquantize(s, block, i, s->qscale);
1118 add_pixels_clamped(block, dest, line_size);
1122 /* generic function called after a macroblock has been parsed by the
1123 decoder or after it has been encoded by the encoder.
1125 Important variables used:
1126 s->mb_intra : true if intra macroblock
1127 s->mv_dir : motion vector direction
1128 s->mv_type : motion vector type
1129 s->mv : motion vector
1130 s->interlaced_dct : true if interlaced dct used (mpeg2)
1132 void MPV_decode_mb(MpegEncContext *s, DCTELEM block[6][64])
1135 int dct_linesize, dct_offset;
1136 op_pixels_func *op_pix;
1137 qpel_mc_func *op_qpix;
1142 #ifdef FF_POSTPROCESS
1143 quant_store[mb_y][mb_x]=s->qscale;
1144 //printf("[%02d][%02d] %d\n",mb_x,mb_y,s->qscale);
1147 /* update DC predictors for P macroblocks */
1149 if (s->h263_pred || s->h263_aic) {
1150 if(s->mbintra_table[mb_x + mb_y*s->mb_width])
1153 s->mbintra_table[mb_x + mb_y*s->mb_width]=0;
1154 wrap = 2 * s->mb_width + 2;
1155 xy = 2 * mb_x + 1 + (2 * mb_y + 1) * wrap;
1158 s->dc_val[0][xy] = v;
1159 s->dc_val[0][xy + 1] = v;
1160 s->dc_val[0][xy + wrap] = v;
1161 s->dc_val[0][xy + 1 + wrap] = v;
1163 memset(s->ac_val[0][xy], 0, 16 * sizeof(INT16));
1164 memset(s->ac_val[0][xy + 1], 0, 16 * sizeof(INT16));
1165 memset(s->ac_val[0][xy + wrap], 0, 16 * sizeof(INT16));
1166 memset(s->ac_val[0][xy + 1 + wrap], 0, 16 * sizeof(INT16));
1167 if (s->h263_msmpeg4) {
1168 s->coded_block[xy] = 0;
1169 s->coded_block[xy + 1] = 0;
1170 s->coded_block[xy + wrap] = 0;
1171 s->coded_block[xy + 1 + wrap] = 0;
1174 wrap = s->mb_width + 2;
1175 xy = mb_x + 1 + (mb_y + 1) * wrap;
1176 s->dc_val[1][xy] = v;
1177 s->dc_val[2][xy] = v;
1179 memset(s->ac_val[1][xy], 0, 16 * sizeof(INT16));
1180 memset(s->ac_val[2][xy], 0, 16 * sizeof(INT16));
1183 s->last_dc[0] = 128 << s->intra_dc_precision;
1184 s->last_dc[1] = 128 << s->intra_dc_precision;
1185 s->last_dc[2] = 128 << s->intra_dc_precision;
1188 else if (s->h263_pred || s->h263_aic)
1189 s->mbintra_table[mb_x + mb_y*s->mb_width]=1;
1191 /* update motion predictor, not for B-frames as they need the motion_val from the last P/S-Frame */
1192 if (s->out_format == FMT_H263) { //FIXME move into h263.c if possible, format specific stuff shouldnt be here
1193 if(s->pict_type!=B_TYPE){
1194 int xy, wrap, motion_x, motion_y;
1196 wrap = 2 * s->mb_width + 2;
1197 xy = 2 * mb_x + 1 + (2 * mb_y + 1) * wrap;
1202 } else if (s->mv_type == MV_TYPE_16X16) {
1203 motion_x = s->mv[0][0][0];
1204 motion_y = s->mv[0][0][1];
1206 /* no update if 8X8 because it has been done during parsing */
1207 s->motion_val[xy][0] = motion_x;
1208 s->motion_val[xy][1] = motion_y;
1209 s->motion_val[xy + 1][0] = motion_x;
1210 s->motion_val[xy + 1][1] = motion_y;
1211 s->motion_val[xy + wrap][0] = motion_x;
1212 s->motion_val[xy + wrap][1] = motion_y;
1213 s->motion_val[xy + 1 + wrap][0] = motion_x;
1214 s->motion_val[xy + 1 + wrap][1] = motion_y;
1219 if (!(s->encoding && (s->intra_only || s->pict_type==B_TYPE))) {
1220 UINT8 *dest_y, *dest_cb, *dest_cr;
1223 /* avoid copy if macroblock skipped in last frame too
1224 dont touch it for B-frames as they need the skip info from the next p-frame */
1225 if (s->pict_type != B_TYPE) {
1226 mbskip_ptr = &s->mbskip_table[s->mb_y * s->mb_width + s->mb_x];
1229 /* if previous was skipped too, then nothing to do !
1230 skip only during decoding as we might trash the buffers during encoding a bit */
1231 if (*mbskip_ptr != 0 && !s->encoding)
1233 *mbskip_ptr = 1; /* indicate that this time we skiped it */
1235 *mbskip_ptr = 0; /* not skipped */
1239 dest_y = s->current_picture[0] + (mb_y * 16 * s->linesize) + mb_x * 16;
1240 dest_cb = s->current_picture[1] + (mb_y * 8 * (s->linesize >> 1)) + mb_x * 8;
1241 dest_cr = s->current_picture[2] + (mb_y * 8 * (s->linesize >> 1)) + mb_x * 8;
1243 if (s->interlaced_dct) {
1244 dct_linesize = s->linesize * 2;
1245 dct_offset = s->linesize;
1247 dct_linesize = s->linesize;
1248 dct_offset = s->linesize * 8;
1252 /* motion handling */
1253 if((s->flags&CODEC_FLAG_HQ) || (!s->encoding)){
1254 if ((!s->no_rounding) || s->pict_type==B_TYPE){
1255 op_pix = put_pixels_tab;
1256 op_qpix= qpel_mc_rnd_tab;
1258 op_pix = put_no_rnd_pixels_tab;
1259 op_qpix= qpel_mc_no_rnd_tab;
1262 if (s->mv_dir & MV_DIR_FORWARD) {
1263 MPV_motion(s, dest_y, dest_cb, dest_cr, 0, s->last_picture, op_pix, op_qpix);
1264 if ((!s->no_rounding) || s->pict_type==B_TYPE)
1265 op_pix = avg_pixels_tab;
1267 op_pix = avg_no_rnd_pixels_tab;
1269 if (s->mv_dir & MV_DIR_BACKWARD) {
1270 MPV_motion(s, dest_y, dest_cb, dest_cr, 1, s->next_picture, op_pix, op_qpix);
1274 /* add dct residue */
1275 add_dct(s, block[0], 0, dest_y, dct_linesize);
1276 add_dct(s, block[1], 1, dest_y + 8, dct_linesize);
1277 add_dct(s, block[2], 2, dest_y + dct_offset, dct_linesize);
1278 add_dct(s, block[3], 3, dest_y + dct_offset + 8, dct_linesize);
1280 add_dct(s, block[4], 4, dest_cb, s->linesize >> 1);
1281 add_dct(s, block[5], 5, dest_cr, s->linesize >> 1);
1283 /* dct only in intra block */
1284 put_dct(s, block[0], 0, dest_y, dct_linesize);
1285 put_dct(s, block[1], 1, dest_y + 8, dct_linesize);
1286 put_dct(s, block[2], 2, dest_y + dct_offset, dct_linesize);
1287 put_dct(s, block[3], 3, dest_y + dct_offset + 8, dct_linesize);
1289 put_dct(s, block[4], 4, dest_cb, s->linesize >> 1);
1290 put_dct(s, block[5], 5, dest_cr, s->linesize >> 1);
1294 emms_c(); //FIXME remove
1298 static void encode_mb(MpegEncContext *s, int motion_x, int motion_y)
1300 const int mb_x= s->mb_x;
1301 const int mb_y= s->mb_y;
1304 if (s->interlaced_dct) {
1305 dct_linesize = s->linesize * 2;
1306 dct_offset = s->linesize;
1308 dct_linesize = s->linesize;
1309 dct_offset = s->linesize * 8;
1318 ptr = s->new_picture[0] + (mb_y * 16 * wrap) + mb_x * 16;
1319 get_pixels(s->block[0], ptr , wrap);
1320 get_pixels(s->block[1], ptr + 8, wrap);
1321 get_pixels(s->block[2], ptr + 8 * wrap , wrap);
1322 get_pixels(s->block[3], ptr + 8 * wrap + 8, wrap);
1325 ptr = s->new_picture[1] + (mb_y * 8 * wrap) + mb_x * 8;
1326 get_pixels(s->block[4], ptr, wrap);
1328 ptr = s->new_picture[2] + (mb_y * 8 * wrap) + mb_x * 8;
1329 get_pixels(s->block[5], ptr, wrap);
1331 op_pixels_func *op_pix;
1332 qpel_mc_func *op_qpix;
1333 UINT8 *dest_y, *dest_cb, *dest_cr;
1337 dest_y = s->current_picture[0] + (mb_y * 16 * s->linesize ) + mb_x * 16;
1338 dest_cb = s->current_picture[1] + (mb_y * 8 * (s->linesize >> 1)) + mb_x * 8;
1339 dest_cr = s->current_picture[2] + (mb_y * 8 * (s->linesize >> 1)) + mb_x * 8;
1341 if ((!s->no_rounding) || s->pict_type==B_TYPE){
1342 op_pix = put_pixels_tab;
1343 op_qpix= qpel_mc_rnd_tab;
1345 op_pix = put_no_rnd_pixels_tab;
1346 op_qpix= qpel_mc_no_rnd_tab;
1349 if (s->mv_dir & MV_DIR_FORWARD) {
1350 MPV_motion(s, dest_y, dest_cb, dest_cr, 0, s->last_picture, op_pix, op_qpix);
1351 if ((!s->no_rounding) || s->pict_type==B_TYPE)
1352 op_pix = avg_pixels_tab;
1354 op_pix = avg_no_rnd_pixels_tab;
1356 if (s->mv_dir & MV_DIR_BACKWARD) {
1357 MPV_motion(s, dest_y, dest_cb, dest_cr, 1, s->next_picture, op_pix, op_qpix);
1360 ptr = s->new_picture[0] + (mb_y * 16 * wrap) + mb_x * 16;
1361 diff_pixels(s->block[0], ptr , dest_y , wrap);
1362 diff_pixels(s->block[1], ptr + 8, dest_y + 8, wrap);
1363 diff_pixels(s->block[2], ptr + 8 * wrap , dest_y + 8 * wrap , wrap);
1364 diff_pixels(s->block[3], ptr + 8 * wrap + 8, dest_y + 8 * wrap + 8, wrap);
1367 ptr = s->new_picture[1] + (mb_y * 8 * wrap) + mb_x * 8;
1368 diff_pixels(s->block[4], ptr, dest_cb, wrap);
1370 ptr = s->new_picture[2] + (mb_y * 8 * wrap) + mb_x * 8;
1371 diff_pixels(s->block[5], ptr, dest_cr, wrap);
1378 adap_parm = ((s->avg_mb_var << 1) + s->mb_var[s->mb_width*mb_y+mb_x] + 1.0) /
1379 ((s->mb_var[s->mb_width*mb_y+mb_x] << 1) + s->avg_mb_var + 1.0);
1381 printf("\ntype=%c qscale=%2d adap=%0.2f dquant=%4.2f var=%4d avgvar=%4d",
1382 (s->mb_type[s->mb_width*mb_y+mb_x] > 0) ? 'I' : 'P',
1383 s->qscale, adap_parm, s->qscale*adap_parm,
1384 s->mb_var[s->mb_width*mb_y+mb_x], s->avg_mb_var);
1387 /* DCT & quantize */
1388 if (s->h263_msmpeg4) {
1389 msmpeg4_dc_scale(s);
1390 } else if (s->h263_pred) {
1393 /* default quantization values */
1398 s->block_last_index[i] = dct_quantize(s, s->block[i], i, s->qscale);
1401 /* huffman encode */
1402 switch(s->out_format) {
1404 mpeg1_encode_mb(s, s->block, motion_x, motion_y);
1407 if (s->h263_msmpeg4)
1408 msmpeg4_encode_mb(s, s->block, motion_x, motion_y);
1409 else if(s->h263_pred)
1410 mpeg4_encode_mb(s, s->block, motion_x, motion_y);
1412 h263_encode_mb(s, s->block, motion_x, motion_y);
1415 mjpeg_encode_mb(s, s->block);
1420 static void copy_bits(PutBitContext *pb, UINT8 *src, int length)
1423 int bytes= length>>4;
1424 int bits= length&15;
1427 for(i=0; i<bytes; i++) put_bits(pb, 16, be2me_16(((uint16_t*)src)[i]));
1428 put_bits(pb, bits, be2me_16(((uint16_t*)src)[i])>>(16-bits));
1430 int bytes= length>>3;
1434 for(i=0; i<bytes; i++) put_bits(pb, 8, src[i]);
1435 put_bits(pb, bits, src[i]>>(8-bits));
1439 static void copy_context_before_encode(MpegEncContext *d, MpegEncContext *s, int type){
1442 memcpy(d->last_mv, s->last_mv, 2*2*2*sizeof(int)); //FIXME is memcpy faster then a loop?
1445 d->mb_incr= s->mb_incr;
1447 d->last_dc[i]= s->last_dc[i];
1450 d->mv_bits= s->mv_bits;
1451 d->i_tex_bits= s->i_tex_bits;
1452 d->p_tex_bits= s->p_tex_bits;
1453 d->i_count= s->i_count;
1454 d->p_count= s->p_count;
1455 d->skip_count= s->skip_count;
1456 d->misc_bits= s->misc_bits;
1459 d->mb_skiped= s->mb_skiped;
1462 static void copy_context_after_encode(MpegEncContext *d, MpegEncContext *s, int type){
1465 memcpy(d->mv, s->mv, 2*4*2*sizeof(int));
1466 memcpy(d->last_mv, s->last_mv, 2*2*2*sizeof(int)); //FIXME is memcpy faster then a loop?
1469 d->mb_incr= s->mb_incr;
1471 d->last_dc[i]= s->last_dc[i];
1474 d->mv_bits= s->mv_bits;
1475 d->i_tex_bits= s->i_tex_bits;
1476 d->p_tex_bits= s->p_tex_bits;
1477 d->i_count= s->i_count;
1478 d->p_count= s->p_count;
1479 d->skip_count= s->skip_count;
1480 d->misc_bits= s->misc_bits;
1482 d->mb_intra= s->mb_intra;
1483 d->mb_skiped= s->mb_skiped;
1484 d->mv_type= s->mv_type;
1485 d->mv_dir= s->mv_dir;
1489 d->block_last_index[i]= s->block_last_index[i];
1493 static void encode_picture(MpegEncContext *s, int picture_number)
1495 int mb_x, mb_y, last_gob, pdif = 0;
1498 MpegEncContext best_s, backup_s;
1499 UINT8 bit_buf[7][3000]; //FIXME check that this is ALLWAYS large enogh for a MB
1501 s->picture_number = picture_number;
1506 s->block_wrap[3]= s->mb_width*2 + 2;
1508 s->block_wrap[5]= s->mb_width + 2;
1510 /* Reset the average MB variance */
1514 /* we need to initialize some time vars before we can encode b-frames */
1515 if (s->h263_pred && !s->h263_msmpeg4)
1516 ff_set_mpeg4_time(s, s->picture_number);
1518 /* Estimate motion for every MB */
1519 if(s->pict_type != I_TYPE){
1520 // int16_t (*tmp)[2]= s->p_mv_table;
1521 // s->p_mv_table= s->last_mv_table;
1522 // s->last_mv_table= s->mv_table;
1524 for(mb_y=0; mb_y < s->mb_height; mb_y++) {
1525 s->block_index[0]= s->block_wrap[0]*(mb_y*2 + 1) - 1;
1526 s->block_index[1]= s->block_wrap[0]*(mb_y*2 + 1);
1527 s->block_index[2]= s->block_wrap[0]*(mb_y*2 + 2) - 1;
1528 s->block_index[3]= s->block_wrap[0]*(mb_y*2 + 2);
1529 for(mb_x=0; mb_x < s->mb_width; mb_x++) {
1532 s->block_index[0]+=2;
1533 s->block_index[1]+=2;
1534 s->block_index[2]+=2;
1535 s->block_index[3]+=2;
1537 /* compute motion vector & mb_type and store in context */
1538 if(s->pict_type==B_TYPE)
1539 ff_estimate_b_frame_motion(s, mb_x, mb_y);
1541 ff_estimate_p_frame_motion(s, mb_x, mb_y);
1542 // s->mb_type[mb_y*s->mb_width + mb_x]=MB_TYPE_INTER;
1546 }else if(s->pict_type == I_TYPE){
1548 //FIXME do we need to zero them?
1549 memset(s->motion_val[0], 0, sizeof(INT16)*(s->mb_width*2 + 2)*(s->mb_height*2 + 2)*2);
1550 memset(s->p_mv_table , 0, sizeof(INT16)*(s->mb_width+2)*(s->mb_height+2)*2);
1551 memset(s->mb_type , MB_TYPE_INTRA, sizeof(UINT8)*s->mb_width*s->mb_height);
1554 if(s->avg_mb_var < s->mc_mb_var && s->pict_type == P_TYPE){ //FIXME subtract MV bits
1555 s->pict_type= I_TYPE;
1556 memset(s->mb_type , MB_TYPE_INTRA, sizeof(UINT8)*s->mb_width*s->mb_height);
1557 if(s->max_b_frames==0){
1558 s->input_pict_type= I_TYPE;
1559 s->input_picture_in_gop_number=0;
1561 //printf("Scene change detected, encoding as I Frame\n");
1564 if(s->pict_type==P_TYPE || s->pict_type==S_TYPE)
1565 s->f_code= ff_get_best_fcode(s, s->p_mv_table, MB_TYPE_INTER);
1566 ff_fix_long_p_mvs(s);
1567 if(s->pict_type==B_TYPE){
1568 s->f_code= ff_get_best_fcode(s, s->b_forw_mv_table, MB_TYPE_FORWARD);
1569 s->b_code= ff_get_best_fcode(s, s->b_back_mv_table, MB_TYPE_BACKWARD);
1571 ff_fix_long_b_mvs(s, s->b_forw_mv_table, s->f_code, MB_TYPE_FORWARD);
1572 ff_fix_long_b_mvs(s, s->b_back_mv_table, s->b_code, MB_TYPE_BACKWARD);
1573 ff_fix_long_b_mvs(s, s->b_bidir_forw_mv_table, s->f_code, MB_TYPE_BIDIR);
1574 ff_fix_long_b_mvs(s, s->b_bidir_back_mv_table, s->b_code, MB_TYPE_BIDIR);
1577 //printf("f_code %d ///\n", s->f_code);
1579 // printf("%d %d\n", s->avg_mb_var, s->mc_mb_var);
1581 if(s->flags&CODEC_FLAG_PASS2)
1582 s->qscale = ff_rate_estimate_qscale_pass2(s);
1583 else if (!s->fixed_qscale)
1584 s->qscale = ff_rate_estimate_qscale(s);
1587 /* precompute matrix */
1588 if (s->out_format == FMT_MJPEG) {
1589 /* for mjpeg, we do include qscale in the matrix */
1590 s->intra_matrix[0] = default_intra_matrix[0];
1592 s->intra_matrix[i] = (default_intra_matrix[i] * s->qscale) >> 3;
1593 convert_matrix(s->q_intra_matrix, s->q_intra_matrix16, s->intra_matrix, 8);
1595 convert_matrix(s->q_intra_matrix, s->q_intra_matrix16, s->intra_matrix, s->qscale);
1596 convert_matrix(s->q_non_intra_matrix, s->q_non_intra_matrix16, s->non_intra_matrix, s->qscale);
1599 s->last_bits= get_bit_count(&s->pb);
1600 switch(s->out_format) {
1602 mjpeg_picture_header(s);
1605 if (s->h263_msmpeg4)
1606 msmpeg4_encode_picture_header(s, picture_number);
1607 else if (s->h263_pred)
1608 mpeg4_encode_picture_header(s, picture_number);
1609 else if (s->h263_rv10)
1610 rv10_encode_picture_header(s, picture_number);
1612 h263_encode_picture_header(s, picture_number);
1615 mpeg1_encode_picture_header(s, picture_number);
1618 bits= get_bit_count(&s->pb);
1619 s->header_bits= bits - s->last_bits;
1629 /* init last dc values */
1630 /* note: quant matrix value (8) is implied here */
1631 s->last_dc[0] = 128;
1632 s->last_dc[1] = 128;
1633 s->last_dc[2] = 128;
1635 s->last_mv[0][0][0] = 0;
1636 s->last_mv[0][0][1] = 0;
1638 /* Get the GOB height based on picture height */
1639 if (s->out_format == FMT_H263 && !s->h263_pred && !s->h263_msmpeg4) {
1640 if (s->height <= 400)
1642 else if (s->height <= 800)
1648 s->avg_mb_var = s->avg_mb_var / s->mb_num;
1650 for(mb_y=0; mb_y < s->mb_height; mb_y++) {
1651 /* Put GOB header based on RTP MTU */
1652 /* TODO: Put all this stuff in a separate generic function */
1655 s->ptr_lastgob = s->pb.buf;
1656 s->ptr_last_mb_line = s->pb.buf;
1657 } else if (s->out_format == FMT_H263 && !s->h263_pred && !s->h263_msmpeg4 && !(mb_y % s->gob_index)) {
1658 last_gob = h263_encode_gob_header(s, mb_y);
1660 s->first_gob_line = 1;
1665 s->block_index[0]= s->block_wrap[0]*(mb_y*2 + 1) - 1;
1666 s->block_index[1]= s->block_wrap[0]*(mb_y*2 + 1);
1667 s->block_index[2]= s->block_wrap[0]*(mb_y*2 + 2) - 1;
1668 s->block_index[3]= s->block_wrap[0]*(mb_y*2 + 2);
1669 s->block_index[4]= s->block_wrap[4]*(mb_y + 1) + s->block_wrap[0]*(s->mb_height*2 + 2);
1670 s->block_index[5]= s->block_wrap[4]*(mb_y + 1 + s->mb_height + 2) + s->block_wrap[0]*(s->mb_height*2 + 2);
1671 for(mb_x=0; mb_x < s->mb_width; mb_x++) {
1672 const int mb_type= s->mb_type[mb_y * s->mb_width + mb_x];
1673 const int xy= (mb_y+1) * (s->mb_width+2) + mb_x + 1;
1681 s->block_index[0]+=2;
1682 s->block_index[1]+=2;
1683 s->block_index[2]+=2;
1684 s->block_index[3]+=2;
1685 s->block_index[4]++;
1686 s->block_index[5]++;
1687 if(mb_type & (mb_type-1)){ // more than 1 MB type possible
1691 copy_context_before_encode(&backup_s, s, -1);
1693 if(mb_type&MB_TYPE_INTER){
1694 s->mv_dir = MV_DIR_FORWARD;
1695 s->mv_type = MV_TYPE_16X16;
1697 s->mv[0][0][0] = s->p_mv_table[xy][0];
1698 s->mv[0][0][1] = s->p_mv_table[xy][1];
1699 init_put_bits(&s->pb, bit_buf[1], 3000, NULL, NULL);
1700 s->block= s->blocks[next_block];
1701 s->last_bits= 0; //done in copy_context_before_encode but we skip that here
1703 encode_mb(s, s->mv[0][0][0], s->mv[0][0][1]);
1704 d= get_bit_count(&s->pb);
1706 flush_put_bits(&s->pb);
1708 copy_context_after_encode(&best_s, s, MB_TYPE_INTER);
1713 if(mb_type&MB_TYPE_INTER4V){
1714 copy_context_before_encode(s, &backup_s, MB_TYPE_INTER4V);
1715 s->mv_dir = MV_DIR_FORWARD;
1716 s->mv_type = MV_TYPE_8X8;
1719 s->mv[0][i][0] = s->motion_val[s->block_index[i]][0];
1720 s->mv[0][i][1] = s->motion_val[s->block_index[i]][1];
1722 init_put_bits(&s->pb, bit_buf[2], 3000, NULL, NULL);
1723 s->block= s->blocks[next_block];
1726 d= get_bit_count(&s->pb);
1728 flush_put_bits(&s->pb);
1730 copy_context_after_encode(&best_s, s, MB_TYPE_INTER4V);
1735 if(mb_type&MB_TYPE_FORWARD){
1736 copy_context_before_encode(s, &backup_s, MB_TYPE_FORWARD);
1737 s->mv_dir = MV_DIR_FORWARD;
1738 s->mv_type = MV_TYPE_16X16;
1740 s->mv[0][0][0] = s->b_forw_mv_table[xy][0];
1741 s->mv[0][0][1] = s->b_forw_mv_table[xy][1];
1742 init_put_bits(&s->pb, bit_buf[3], 3000, NULL, NULL);
1743 s->block= s->blocks[next_block];
1745 encode_mb(s, s->mv[0][0][0], s->mv[0][0][1]);
1746 d= get_bit_count(&s->pb);
1748 flush_put_bits(&s->pb);
1750 copy_context_after_encode(&best_s, s, MB_TYPE_FORWARD);
1755 if(mb_type&MB_TYPE_BACKWARD){
1756 copy_context_before_encode(s, &backup_s, MB_TYPE_BACKWARD);
1757 s->mv_dir = MV_DIR_BACKWARD;
1758 s->mv_type = MV_TYPE_16X16;
1760 s->mv[1][0][0] = s->b_back_mv_table[xy][0];
1761 s->mv[1][0][1] = s->b_back_mv_table[xy][1];
1762 init_put_bits(&s->pb, bit_buf[4], 3000, NULL, NULL);
1763 s->block= s->blocks[next_block];
1765 encode_mb(s, s->mv[1][0][0], s->mv[1][0][1]);
1766 d= get_bit_count(&s->pb);
1768 flush_put_bits(&s->pb);
1770 copy_context_after_encode(&best_s, s, MB_TYPE_BACKWARD);
1775 if(mb_type&MB_TYPE_BIDIR){
1776 copy_context_before_encode(s, &backup_s, MB_TYPE_BIDIR);
1777 s->mv_dir = MV_DIR_FORWARD | MV_DIR_BACKWARD;
1778 s->mv_type = MV_TYPE_16X16;
1780 s->mv[0][0][0] = s->b_bidir_forw_mv_table[xy][0];
1781 s->mv[0][0][1] = s->b_bidir_forw_mv_table[xy][1];
1782 s->mv[1][0][0] = s->b_bidir_back_mv_table[xy][0];
1783 s->mv[1][0][1] = s->b_bidir_back_mv_table[xy][1];
1784 init_put_bits(&s->pb, bit_buf[5], 3000, NULL, NULL);
1785 s->block= s->blocks[next_block];
1788 d= get_bit_count(&s->pb);
1790 flush_put_bits(&s->pb);
1792 copy_context_after_encode(&best_s, s, MB_TYPE_BIDIR);
1797 if(mb_type&MB_TYPE_DIRECT){
1798 copy_context_before_encode(s, &backup_s, MB_TYPE_DIRECT);
1799 s->mv_dir = MV_DIR_FORWARD | MV_DIR_BACKWARD | MV_DIRECT;
1800 s->mv_type = MV_TYPE_16X16; //FIXME
1802 s->mv[0][0][0] = s->b_direct_forw_mv_table[xy][0];
1803 s->mv[0][0][1] = s->b_direct_forw_mv_table[xy][1];
1804 s->mv[1][0][0] = s->b_direct_back_mv_table[xy][0];
1805 s->mv[1][0][1] = s->b_direct_back_mv_table[xy][1];
1806 init_put_bits(&s->pb, bit_buf[6], 3000, NULL, NULL);
1807 s->block= s->blocks[next_block];
1809 encode_mb(s, s->b_direct_mv_table[xy][0], s->b_direct_mv_table[xy][1]);
1810 d= get_bit_count(&s->pb);
1812 flush_put_bits(&s->pb);
1814 copy_context_after_encode(&best_s, s, MB_TYPE_DIRECT);
1819 if(mb_type&MB_TYPE_INTRA){
1820 copy_context_before_encode(s, &backup_s, MB_TYPE_INTRA);
1821 s->mv_dir = MV_DIR_FORWARD;
1822 s->mv_type = MV_TYPE_16X16;
1826 init_put_bits(&s->pb, bit_buf[0], 3000, NULL, NULL);
1827 s->block= s->blocks[next_block];
1830 d= get_bit_count(&s->pb);
1832 flush_put_bits(&s->pb);
1834 copy_context_after_encode(&best_s, s, MB_TYPE_INTRA);
1838 /* force cleaning of ac/dc pred stuff if needed ... */
1839 if(s->h263_pred || s->h263_aic)
1840 s->mbintra_table[mb_x + mb_y*s->mb_width]=1;
1842 copy_context_after_encode(s, &best_s, -1);
1843 copy_bits(&pb, bit_buf[best], dmin);
1845 s->last_bits= get_bit_count(&s->pb);
1847 int motion_x, motion_y;
1848 s->mv_type=MV_TYPE_16X16;
1849 // only one MB-Type possible
1852 s->mv_dir = MV_DIR_FORWARD;
1854 motion_x= s->mv[0][0][0] = 0;
1855 motion_y= s->mv[0][0][1] = 0;
1858 s->mv_dir = MV_DIR_FORWARD;
1860 motion_x= s->mv[0][0][0] = s->p_mv_table[xy][0];
1861 motion_y= s->mv[0][0][1] = s->p_mv_table[xy][1];
1863 case MB_TYPE_DIRECT:
1864 s->mv_dir = MV_DIR_FORWARD | MV_DIR_BACKWARD | MV_DIRECT;
1866 motion_x=s->b_direct_mv_table[xy][0];
1867 motion_y=s->b_direct_mv_table[xy][1];
1868 s->mv[0][0][0] = s->b_direct_forw_mv_table[xy][0];
1869 s->mv[0][0][1] = s->b_direct_forw_mv_table[xy][1];
1870 s->mv[1][0][0] = s->b_direct_back_mv_table[xy][0];
1871 s->mv[1][0][1] = s->b_direct_back_mv_table[xy][1];
1874 s->mv_dir = MV_DIR_FORWARD | MV_DIR_BACKWARD;
1878 s->mv[0][0][0] = s->b_bidir_forw_mv_table[xy][0];
1879 s->mv[0][0][1] = s->b_bidir_forw_mv_table[xy][1];
1880 s->mv[1][0][0] = s->b_bidir_back_mv_table[xy][0];
1881 s->mv[1][0][1] = s->b_bidir_back_mv_table[xy][1];
1883 case MB_TYPE_BACKWARD:
1884 s->mv_dir = MV_DIR_BACKWARD;
1886 motion_x= s->mv[1][0][0] = s->b_back_mv_table[xy][0];
1887 motion_y= s->mv[1][0][1] = s->b_back_mv_table[xy][1];
1889 case MB_TYPE_FORWARD:
1890 s->mv_dir = MV_DIR_FORWARD;
1892 motion_x= s->mv[0][0][0] = s->b_forw_mv_table[xy][0];
1893 motion_y= s->mv[0][0][1] = s->b_forw_mv_table[xy][1];
1894 // printf(" %d %d ", motion_x, motion_y);
1897 motion_x=motion_y=0; //gcc warning fix
1898 printf("illegal MB type\n");
1900 encode_mb(s, motion_x, motion_y);
1902 /* clean the MV table in IPS frames for direct mode in B frames */
1903 if(s->mb_intra /* && I,P,S_TYPE */){
1904 s->p_mv_table[xy][0]=0;
1905 s->p_mv_table[xy][1]=0;
1908 MPV_decode_mb(s, s->block);
1912 /* Obtain average GOB size for RTP */
1915 s->mb_line_avgsize = pbBufPtr(&s->pb) - s->ptr_last_mb_line;
1916 else if (!(mb_y % s->gob_index)) {
1917 s->mb_line_avgsize = (s->mb_line_avgsize + pbBufPtr(&s->pb) - s->ptr_last_mb_line) >> 1;
1918 s->ptr_last_mb_line = pbBufPtr(&s->pb);
1920 //fprintf(stderr, "\nMB line: %d\tSize: %u\tAvg. Size: %u", s->mb_y,
1921 // (s->pb.buf_ptr - s->ptr_last_mb_line), s->mb_line_avgsize);
1922 s->first_gob_line = 0;
1927 if (s->h263_msmpeg4 && s->msmpeg4_version<4 && s->pict_type == I_TYPE)
1928 msmpeg4_encode_ext_header(s);
1930 //if (s->gob_number)
1931 // fprintf(stderr,"\nNumber of GOB: %d", s->gob_number);
1933 /* Send the last GOB if RTP */
1935 flush_put_bits(&s->pb);
1936 pdif = pbBufPtr(&s->pb) - s->ptr_lastgob;
1937 /* Call the RTP callback to send the last GOB */
1938 if (s->rtp_callback)
1939 s->rtp_callback(s->ptr_lastgob, pdif, s->gob_number);
1940 s->ptr_lastgob = pbBufPtr(&s->pb);
1941 //fprintf(stderr,"\nGOB: %2d size: %d (last)", s->gob_number, pdif);
1945 static int dct_quantize_c(MpegEncContext *s,
1946 DCTELEM *block, int n,
1949 int i, j, level, last_non_zero, q;
1951 int minLevel, maxLevel;
1953 if(s->avctx!=NULL && s->avctx->codec->id==CODEC_ID_MPEG4){
1957 }else if(s->out_format==FMT_MPEG1){
1961 }else if(s->out_format==FMT_MJPEG){
1966 /* h263 / msmpeg4 */
1973 /* we need this permutation so that we correct the IDCT
1974 permutation. will be moved into DCT code */
1975 block_permute(block);
1984 /* note: block[0] is assumed to be positive */
1985 block[0] = (block[0] + (q >> 1)) / q;
1988 if (s->out_format == FMT_H263) {
1989 qmat = s->q_non_intra_matrix;
1991 qmat = s->q_intra_matrix;
1996 qmat = s->q_non_intra_matrix;
2000 j = zigzag_direct[i];
2002 level = level * qmat[j];
2005 static int count = 0;
2006 int level1, level2, qmat1;
2008 if (qmat == s->q_non_intra_matrix) {
2009 qmat1 = default_non_intra_matrix[j] * s->qscale;
2011 qmat1 = default_intra_matrix[j] * s->qscale;
2013 if (av_fdct != jpeg_fdct_ifast)
2014 val = ((double)block[j] * 8.0) / (double)qmat1;
2016 val = ((double)block[j] * 8.0 * 2048.0) /
2017 ((double)qmat1 * aanscales[j]);
2019 level2 = level / (1 << (QMAT_SHIFT - 3));
2020 if (level1 != level2) {
2021 fprintf(stderr, "%d: quant error qlevel=%d wanted=%d level=%d qmat1=%d qmat=%d wantedf=%0.6f\n",
2022 count, level2, level1, block[j], qmat1, qmat[j],
2029 /* XXX: slight error for the low range. Test should be equivalent to
2030 (level <= -(1 << (QMAT_SHIFT - 3)) || level >= (1 <<
2033 if (((level << (31 - (QMAT_SHIFT - 3))) >> (31 - (QMAT_SHIFT - 3))) !=
2035 level = level / (1 << (QMAT_SHIFT - 3));
2036 /* XXX: currently, this code is not optimal. the range should be:
2042 if (level > maxLevel)
2044 else if (level < minLevel)
2053 return last_non_zero;
2056 static void dct_unquantize_mpeg1_c(MpegEncContext *s,
2057 DCTELEM *block, int n, int qscale)
2059 int i, level, nCoeffs;
2060 const UINT16 *quant_matrix;
2062 if(s->alternate_scan) nCoeffs= 64;
2063 else nCoeffs= s->block_last_index[n]+1;
2067 block[0] = block[0] * s->y_dc_scale;
2069 block[0] = block[0] * s->c_dc_scale;
2070 /* XXX: only mpeg1 */
2071 quant_matrix = s->intra_matrix;
2072 for(i=1;i<nCoeffs;i++) {
2073 int j= zigzag_direct[i];
2078 level = (int)(level * qscale * quant_matrix[j]) >> 3;
2079 level = (level - 1) | 1;
2082 level = (int)(level * qscale * quant_matrix[j]) >> 3;
2083 level = (level - 1) | 1;
2086 if (level < -2048 || level > 2047)
2087 fprintf(stderr, "unquant error %d %d\n", i, level);
2094 quant_matrix = s->non_intra_matrix;
2095 for(;i<nCoeffs;i++) {
2096 int j= zigzag_direct[i];
2101 level = (((level << 1) + 1) * qscale *
2102 ((int) (quant_matrix[j]))) >> 4;
2103 level = (level - 1) | 1;
2106 level = (((level << 1) + 1) * qscale *
2107 ((int) (quant_matrix[j]))) >> 4;
2108 level = (level - 1) | 1;
2111 if (level < -2048 || level > 2047)
2112 fprintf(stderr, "unquant error %d %d\n", i, level);
2120 static void dct_unquantize_mpeg2_c(MpegEncContext *s,
2121 DCTELEM *block, int n, int qscale)
2123 int i, level, nCoeffs;
2124 const UINT16 *quant_matrix;
2126 if(s->alternate_scan) nCoeffs= 64;
2127 else nCoeffs= s->block_last_index[n]+1;
2131 block[0] = block[0] * s->y_dc_scale;
2133 block[0] = block[0] * s->c_dc_scale;
2134 quant_matrix = s->intra_matrix;
2135 for(i=1;i<nCoeffs;i++) {
2136 int j= zigzag_direct[i];
2141 level = (int)(level * qscale * quant_matrix[j]) >> 3;
2144 level = (int)(level * qscale * quant_matrix[j]) >> 3;
2147 if (level < -2048 || level > 2047)
2148 fprintf(stderr, "unquant error %d %d\n", i, level);
2156 quant_matrix = s->non_intra_matrix;
2157 for(;i<nCoeffs;i++) {
2158 int j= zigzag_direct[i];
2163 level = (((level << 1) + 1) * qscale *
2164 ((int) (quant_matrix[j]))) >> 4;
2167 level = (((level << 1) + 1) * qscale *
2168 ((int) (quant_matrix[j]))) >> 4;
2171 if (level < -2048 || level > 2047)
2172 fprintf(stderr, "unquant error %d %d\n", i, level);
2183 static void dct_unquantize_h263_c(MpegEncContext *s,
2184 DCTELEM *block, int n, int qscale)
2186 int i, level, qmul, qadd;
2192 block[0] = block[0] * s->y_dc_scale;
2194 block[0] = block[0] * s->c_dc_scale;
2197 nCoeffs= 64; //does not allways use zigzag table
2200 nCoeffs= zigzag_end[ s->block_last_index[n] ];
2203 qmul = s->qscale << 1;
2204 if (s->h263_aic && s->mb_intra)
2207 qadd = (s->qscale - 1) | 1;
2209 for(;i<nCoeffs;i++) {
2213 level = level * qmul - qadd;
2215 level = level * qmul + qadd;
2218 if (level < -2048 || level > 2047)
2219 fprintf(stderr, "unquant error %d %d\n", i, level);
2226 AVCodec mpeg1video_encoder = {
2229 CODEC_ID_MPEG1VIDEO,
2230 sizeof(MpegEncContext),
2236 AVCodec h263_encoder = {
2240 sizeof(MpegEncContext),
2246 AVCodec h263p_encoder = {
2250 sizeof(MpegEncContext),
2256 AVCodec rv10_encoder = {
2260 sizeof(MpegEncContext),
2266 AVCodec mjpeg_encoder = {
2270 sizeof(MpegEncContext),
2276 AVCodec mpeg4_encoder = {
2280 sizeof(MpegEncContext),
2286 AVCodec msmpeg4v1_encoder = {
2290 sizeof(MpegEncContext),
2296 AVCodec msmpeg4v2_encoder = {
2300 sizeof(MpegEncContext),
2306 AVCodec msmpeg4v3_encoder = {
2310 sizeof(MpegEncContext),