comparison mpegvideo.c @ 324:9c6f056f0e41 libavcodec

fixed mpeg4 time stuff on encoding mpeg4 b-frame enoding support removed old, out-commented ratecontrol reuse motion compensation code between encoding & decoding prefix newly added global functions with ff_ to reduce namespace polution b-frame ME (unfinished, but working) added some comments to mpegvideo.h do MC on encoding only once if possible bugs? ;)
author michaelni
date Wed, 17 Apr 2002 04:32:12 +0000
parents 2b00e171b1d4
children 15efd80cf51e
comparison
equal deleted inserted replaced
323:68cc7650c645 324:9c6f056f0e41
146 pict = av_mallocz(c_size); 146 pict = av_mallocz(c_size);
147 if (pict == NULL) 147 if (pict == NULL)
148 goto fail; 148 goto fail;
149 s->next_picture_base[i] = pict; 149 s->next_picture_base[i] = pict;
150 s->next_picture[i] = pict + pict_start; 150 s->next_picture[i] = pict + pict_start;
151 151
152 if (s->has_b_frames) { 152 if (s->has_b_frames) {
153 pict = av_mallocz(c_size); 153 pict = av_mallocz(c_size);
154 if (pict == NULL) 154 if (pict == NULL)
155 goto fail; 155 goto fail;
156 s->aux_picture_base[i] = pict; 156 s->aux_picture_base[i] = pict;
157 s->aux_picture[i] = pict + pict_start; 157 s->aux_picture[i] = pict + pict_start;
158 } 158 }
159 } 159 }
160 160
161 if (s->encoding) { 161 if (s->encoding) {
162 int j;
163 int mv_table_size= (s->mb_width+2)*(s->mb_height+2);
164
162 /* Allocate MB type table */ 165 /* Allocate MB type table */
163 s->mb_type = av_mallocz(s->mb_num * sizeof(char)); 166 s->mb_type = av_mallocz(s->mb_num * sizeof(char));
164 if (s->mb_type == NULL) { 167 if (s->mb_type == NULL) {
165 perror("malloc"); 168 perror("malloc");
166 goto fail; 169 goto fail;
169 s->mb_var = av_mallocz(s->mb_num * sizeof(INT16)); 172 s->mb_var = av_mallocz(s->mb_num * sizeof(INT16));
170 if (s->mb_var == NULL) { 173 if (s->mb_var == NULL) {
171 perror("malloc"); 174 perror("malloc");
172 goto fail; 175 goto fail;
173 } 176 }
174 /* Allocate MV table */ 177
175 /* By now we just have one MV per MB */ 178 /* Allocate MV tables */
176 s->mv_table[0] = av_mallocz(s->mb_num * sizeof(INT16)); 179 s->p_mv_table = av_mallocz(mv_table_size * 2 * sizeof(INT16));
177 s->mv_table[1] = av_mallocz(s->mb_num * sizeof(INT16)); 180 if (s->p_mv_table == NULL) {
178 if (s->mv_table[1] == NULL || s->mv_table[0] == NULL) {
179 perror("malloc"); 181 perror("malloc");
180 goto fail; 182 goto fail;
183 }
184 s->last_p_mv_table = av_mallocz(mv_table_size * 2 * sizeof(INT16));
185 if (s->last_p_mv_table == NULL) {
186 perror("malloc");
187 goto fail;
188 }
189 s->b_forw_mv_table = av_mallocz(mv_table_size * 2 * sizeof(INT16));
190 if (s->b_forw_mv_table == NULL) {
191 perror("malloc");
192 goto fail;
193 }
194 s->b_back_mv_table = av_mallocz(mv_table_size * 2 * sizeof(INT16));
195 if (s->b_back_mv_table == NULL) {
196 perror("malloc");
197 goto fail;
198 }
199 s->b_bidir_forw_mv_table = av_mallocz(mv_table_size * 2 * sizeof(INT16));
200 if (s->b_bidir_forw_mv_table == NULL) {
201 perror("malloc");
202 goto fail;
203 }
204 s->b_bidir_back_mv_table = av_mallocz(mv_table_size * 2 * sizeof(INT16));
205 if (s->b_bidir_back_mv_table == NULL) {
206 perror("malloc");
207 goto fail;
208 }
209 s->b_direct_forw_mv_table = av_mallocz(mv_table_size * 2 * sizeof(INT16));
210 if (s->b_direct_forw_mv_table == NULL) {
211 perror("malloc");
212 goto fail;
213 }
214 s->b_direct_back_mv_table = av_mallocz(mv_table_size * 2 * sizeof(INT16));
215 if (s->b_direct_back_mv_table == NULL) {
216 perror("malloc");
217 goto fail;
218 }
219 s->b_direct_mv_table = av_mallocz(mv_table_size * 2 * sizeof(INT16));
220 if (s->b_direct_mv_table == NULL) {
221 perror("malloc");
222 goto fail;
223 }
224
225 if(s->max_b_frames){
226 for(j=0; j<REORDER_BUFFER_SIZE; j++){
227 int i;
228 for(i=0;i<3;i++) {
229 int w, h, shift;
230
231 w = s->linesize;
232 h = s->mb_height * 16;
233 shift = (i == 0) ? 0 : 1;
234 c_size = (w >> shift) * (h >> shift);
235
236 pict = av_mallocz(c_size);
237 if (pict == NULL)
238 goto fail;
239 s->picture_buffer[j][i] = pict;
240 }
241 }
181 } 242 }
182 } 243 }
183 244
184 if (s->out_format == FMT_H263 || s->encoding) { 245 if (s->out_format == FMT_H263 || s->encoding) {
185 int size; 246 int size;
227 } 288 }
228 /* default structure is frame */ 289 /* default structure is frame */
229 s->picture_structure = PICT_FRAME; 290 s->picture_structure = PICT_FRAME;
230 291
231 /* init macroblock skip table */ 292 /* init macroblock skip table */
232 if (!s->encoding) { 293 s->mbskip_table = av_mallocz(s->mb_num);
233 s->mbskip_table = av_mallocz(s->mb_num); 294 if (!s->mbskip_table)
234 if (!s->mbskip_table) 295 goto fail;
235 goto fail;
236 }
237 296
238 s->block= s->intra_block; 297 s->block= s->intra_block;
239 298
240 s->context_initialized = 1; 299 s->context_initialized = 1;
241 return 0; 300 return 0;
242 fail: 301 fail:
243 MPV_common_end(s); 302 MPV_common_end(s);
244 return -1; 303 return -1;
245 } 304 }
246 305
306 #define CHECK_FREE(p)\
307 {\
308 if(p) free(p);\
309 p= NULL;\
310 }
311
247 /* init common structure for both encoder and decoder */ 312 /* init common structure for both encoder and decoder */
248 void MPV_common_end(MpegEncContext *s) 313 void MPV_common_end(MpegEncContext *s)
249 { 314 {
250 int i; 315 int i;
251 316
252 if (s->mb_type) 317 CHECK_FREE(s->mb_type);
253 free(s->mb_type); 318 CHECK_FREE(s->mb_var);
254 if (s->mb_var) 319 CHECK_FREE(s->p_mv_table);
255 free(s->mb_var); 320 CHECK_FREE(s->last_p_mv_table);
256 if (s->mv_table[0]) 321 CHECK_FREE(s->b_forw_mv_table);
257 free(s->mv_table[0]); 322 CHECK_FREE(s->b_back_mv_table);
258 if (s->mv_table[1]) 323 CHECK_FREE(s->b_bidir_forw_mv_table);
259 free(s->mv_table[1]); 324 CHECK_FREE(s->b_bidir_back_mv_table);
260 if (s->motion_val) 325 CHECK_FREE(s->b_direct_forw_mv_table);
261 free(s->motion_val); 326 CHECK_FREE(s->b_direct_back_mv_table);
262 if (s->dc_val[0]) 327 CHECK_FREE(s->b_direct_mv_table);
263 free(s->dc_val[0]); 328 CHECK_FREE(s->motion_val);
264 if (s->ac_val[0]) 329 CHECK_FREE(s->dc_val[0]);
265 free(s->ac_val[0]); 330 CHECK_FREE(s->ac_val[0]);
266 if (s->coded_block) 331 CHECK_FREE(s->coded_block);
267 free(s->coded_block); 332 CHECK_FREE(s->mbintra_table);
268 if (s->mbintra_table) 333
269 free(s->mbintra_table); 334 CHECK_FREE(s->mbskip_table);
270
271 if (s->mbskip_table)
272 free(s->mbskip_table);
273 for(i=0;i<3;i++) { 335 for(i=0;i<3;i++) {
274 if (s->last_picture_base[i]) 336 int j;
275 free(s->last_picture_base[i]); 337 CHECK_FREE(s->last_picture_base[i]);
276 if (s->next_picture_base[i]) 338 CHECK_FREE(s->next_picture_base[i]);
277 free(s->next_picture_base[i]); 339 CHECK_FREE(s->aux_picture_base[i]);
278 if (s->has_b_frames) 340 for(j=0; j<REORDER_BUFFER_SIZE; j++){
279 free(s->aux_picture_base[i]); 341 CHECK_FREE(s->picture_buffer[j][i]);
342 }
280 } 343 }
281 s->context_initialized = 0; 344 s->context_initialized = 0;
282 } 345 }
283 346
284 /* init video encoder */ 347 /* init video encoder */
305 s->qcompress= avctx->qcompress; 368 s->qcompress= avctx->qcompress;
306 s->qblur= avctx->qblur; 369 s->qblur= avctx->qblur;
307 s->avctx = avctx; 370 s->avctx = avctx;
308 s->aspect_ratio_info= avctx->aspect_ratio_info; 371 s->aspect_ratio_info= avctx->aspect_ratio_info;
309 s->flags= avctx->flags; 372 s->flags= avctx->flags;
373 s->max_b_frames= avctx->max_b_frames;
310 374
311 if (s->gop_size <= 1) { 375 if (s->gop_size <= 1) {
312 s->intra_only = 1; 376 s->intra_only = 1;
313 s->gop_size = 12; 377 s->gop_size = 12;
314 } else { 378 } else {
366 break; 430 break;
367 case CODEC_ID_MPEG4: 431 case CODEC_ID_MPEG4:
368 s->out_format = FMT_H263; 432 s->out_format = FMT_H263;
369 s->h263_pred = 1; 433 s->h263_pred = 1;
370 s->unrestricted_mv = 1; 434 s->unrestricted_mv = 1;
435 s->has_b_frames= s->max_b_frames ? 1 : 0;
371 break; 436 break;
372 case CODEC_ID_MSMPEG4V1: 437 case CODEC_ID_MSMPEG4V1:
373 s->out_format = FMT_H263; 438 s->out_format = FMT_H263;
374 s->h263_msmpeg4 = 1; 439 s->h263_msmpeg4 = 1;
375 s->h263_pred = 1; 440 s->h263_pred = 1;
419 h263_encode_init(s); 484 h263_encode_init(s);
420 else if (s->out_format == FMT_MPEG1) 485 else if (s->out_format == FMT_MPEG1)
421 mpeg1_encode_init(s); 486 mpeg1_encode_init(s);
422 487
423 /* dont use mv_penalty table for crap MV as it would be confused */ 488 /* dont use mv_penalty table for crap MV as it would be confused */
424 if (s->me_method < 5) s->mv_penalty = default_mv_penalty; 489 if (s->me_method < ME_EPZS) s->mv_penalty = default_mv_penalty;
425 490
426 s->encoding = 1; 491 s->encoding = 1;
427 492
428 /* init */ 493 /* init */
429 if (MPV_common_init(s) < 0) 494 if (MPV_common_init(s) < 0)
441 s->picture_number = 0; 506 s->picture_number = 0;
442 s->picture_in_gop_number = 0; 507 s->picture_in_gop_number = 0;
443 s->fake_picture_number = 0; 508 s->fake_picture_number = 0;
444 /* motion detector init */ 509 /* motion detector init */
445 s->f_code = 1; 510 s->f_code = 1;
511 s->b_code = 1;
446 512
447 return 0; 513 return 0;
448 } 514 }
449 515
450 int MPV_encode_end(AVCodecContext *avctx) 516 int MPV_encode_end(AVCodecContext *avctx)
529 } 595 }
530 } 596 }
531 emms_c(); 597 emms_c();
532 } 598 }
533 599
534 int MPV_encode_picture(AVCodecContext *avctx, 600 /* reorder input for encoding */
535 unsigned char *buf, int buf_size, void *data) 601 void reorder_input(MpegEncContext *s, AVPicture *pict)
536 { 602 {
537 MpegEncContext *s = avctx->priv_data; 603 int i, j, index;
538 AVPicture *pict = data; 604
539 int i, j; 605 if(s->max_b_frames > FF_MAX_B_FRAMES) s->max_b_frames= FF_MAX_B_FRAMES;
540 606
541 if (s->fixed_qscale) 607 // delay= s->max_b_frames+1; (or 0 if no b frames cuz decoder diff)
542 s->qscale = avctx->quality; 608
543 609 for(j=0; j<REORDER_BUFFER_SIZE-1; j++){
544 init_put_bits(&s->pb, buf, buf_size, NULL, NULL); 610 s->coded_order[j]= s->coded_order[j+1];
545 611 }
546 s->force_type= (avctx->flags&CODEC_FLAG_TYPE) ? 612 s->coded_order[j].picture[0]= s->coded_order[j].picture[1]= s->coded_order[j].picture[2]= NULL; //catch uninitalized buffers
547 (avctx->key_frame ? I_TYPE : P_TYPE) : 0; 613
548 if (!s->intra_only) { 614 switch(s->input_pict_type){
549 /* first picture of GOP is intra */ 615 default:
550 if (s->picture_in_gop_number % s->gop_size==0 || s->force_type==I_TYPE){ 616 case I_TYPE:
551 s->picture_in_gop_number=0; 617 case S_TYPE:
552 s->pict_type = I_TYPE; 618 case P_TYPE:
553 }else 619 index= s->max_b_frames - s->b_frames_since_non_b;
554 s->pict_type = P_TYPE; 620 s->b_frames_since_non_b=0;
555 } else { 621 break;
556 s->pict_type = I_TYPE; 622 case B_TYPE:
557 } 623 index= s->max_b_frames + 1;
558 624 s->b_frames_since_non_b++;
559 MPV_frame_start(s); 625 break;
560 626 }
561 for(i=0;i<3;i++) { 627 //printf("index:%d type:%d strides: %d %d\n", index, s->input_pict_type, pict->linesize[0], s->linesize);
562 UINT8 *src = pict->data[i]; 628 if( (index==0 || (s->flags&CODEC_FLAG_INPUT_PRESERVED))
563 UINT8 *dest = s->current_picture[i]; 629 && pict->linesize[0] == s->linesize
564 int src_wrap = pict->linesize[i]; 630 && pict->linesize[1] == s->linesize>>1
565 int dest_wrap = s->linesize; 631 && pict->linesize[2] == s->linesize>>1){
566 int w = s->width; 632 //printf("ptr\n");
567 int h = s->height; 633 for(i=0; i<3; i++){
568 634 s->coded_order[index].picture[i]= pict->data[i];
569 if (i >= 1) { 635 }
570 dest_wrap >>= 1; 636 }else{
571 w >>= 1; 637 //printf("copy\n");
572 h >>= 1; 638 for(i=0; i<3; i++){
573 } 639 uint8_t *src = pict->data[i];
574 640 uint8_t *dest;
575 if(dest_wrap==src_wrap){ 641 int src_wrap = pict->linesize[i];
576 s->new_picture[i] = pict->data[i]; 642 int dest_wrap = s->linesize;
577 } else { 643 int w = s->width;
644 int h = s->height;
645
646 if(index==0) dest= s->last_picture[i]+16; //is current_picture indeed but the switch hapens after reordering
647 else dest= s->picture_buffer[s->picture_buffer_index][i];
648
649 if (i >= 1) {
650 dest_wrap >>= 1;
651 w >>= 1;
652 h >>= 1;
653 }
654
655 s->coded_order[index].picture[i]= dest;
578 for(j=0;j<h;j++) { 656 for(j=0;j<h;j++) {
579 memcpy(dest, src, w); 657 memcpy(dest, src, w);
580 dest += dest_wrap; 658 dest += dest_wrap;
581 src += src_wrap; 659 src += src_wrap;
582 } 660 }
583 s->new_picture[i] = s->current_picture[i]; 661 }
584 } 662 if(index!=0){
585 } 663 s->picture_buffer_index++;
586 664 if(s->picture_buffer_index >= REORDER_BUFFER_SIZE-1) s->picture_buffer_index=0;
587 encode_picture(s, s->picture_number); 665 }
588 avctx->key_frame = (s->pict_type == I_TYPE); 666 }
589 avctx->header_bits = s->header_bits; 667 s->coded_order[index].pict_type = s->input_pict_type;
590 avctx->mv_bits = s->mv_bits; 668 s->coded_order[index].qscale = s->input_qscale;
591 avctx->misc_bits = s->misc_bits; 669 s->coded_order[index].force_type= s->force_input_type;
592 avctx->i_tex_bits = s->i_tex_bits; 670 s->coded_order[index].picture_in_gop_number= s->input_picture_in_gop_number;
593 avctx->p_tex_bits = s->p_tex_bits; 671 s->coded_order[index].picture_number= s->input_picture_number;
594 avctx->i_count = s->i_count; 672
595 avctx->p_count = s->p_count; 673 for(i=0; i<3; i++){
596 avctx->skip_count = s->skip_count; 674 s->new_picture[i]= s->coded_order[0].picture[i];
597 675 }
598 MPV_frame_end(s); 676 }
599 s->picture_number++; 677
600 s->picture_in_gop_number++; 678 int MPV_encode_picture(AVCodecContext *avctx,
601 679 unsigned char *buf, int buf_size, void *data)
602 if (s->out_format == FMT_MJPEG) 680 {
603 mjpeg_picture_trailer(s); 681 MpegEncContext *s = avctx->priv_data;
682 AVPicture *pict = data;
683
684 s->input_qscale = avctx->quality;
685
686 init_put_bits(&s->pb, buf, buf_size, NULL, NULL);
687
688 s->force_input_type= (avctx->flags&CODEC_FLAG_TYPE) ?
689 (avctx->key_frame ? I_TYPE : P_TYPE) : 0;
690 if (!s->intra_only) {
691 /* first picture of GOP is intra */
692 if (s->input_picture_in_gop_number % s->gop_size==0 || s->force_input_type==I_TYPE){
693 s->input_picture_in_gop_number=0;
694 s->input_pict_type = I_TYPE;
695 }else if(s->max_b_frames==0){
696 s->input_pict_type = P_TYPE;
697 }else{
698 if(s->b_frames_since_non_b < s->max_b_frames) //FIXME more IQ
699 s->input_pict_type = B_TYPE;
700 else
701 s->input_pict_type = P_TYPE;
702 }
703 } else {
704 s->input_pict_type = I_TYPE;
705 }
706
707 reorder_input(s, pict);
708
709 /* output? */
710 if(s->coded_order[0].picture[0]){
711
712 s->pict_type= s->coded_order[0].pict_type;
713 if (s->fixed_qscale) /* the ratecontrol needs the last qscale so we dont touch it for CBR */
714 s->qscale= s->coded_order[0].qscale;
715 s->force_type= s->coded_order[0].force_type;
716 s->picture_in_gop_number= s->coded_order[0].picture_in_gop_number;
717 s->picture_number= s->coded_order[0].picture_number;
718
719 MPV_frame_start(s);
720
721 encode_picture(s, s->picture_number);
722 avctx->key_frame = (s->pict_type == I_TYPE);
723 avctx->header_bits = s->header_bits;
724 avctx->mv_bits = s->mv_bits;
725 avctx->misc_bits = s->misc_bits;
726 avctx->i_tex_bits = s->i_tex_bits;
727 avctx->p_tex_bits = s->p_tex_bits;
728 avctx->i_count = s->i_count;
729 avctx->p_count = s->p_count;
730 avctx->skip_count = s->skip_count;
731
732 MPV_frame_end(s);
733
734 if (s->out_format == FMT_MJPEG)
735 mjpeg_picture_trailer(s);
736
737 avctx->quality = s->qscale;
738 }
739
740 s->input_picture_number++;
741 s->input_picture_in_gop_number++;
604 742
605 flush_put_bits(&s->pb); 743 flush_put_bits(&s->pb);
606 s->last_frame_bits= s->frame_bits; 744 s->last_frame_bits= s->frame_bits;
607 s->frame_bits = (pbBufPtr(&s->pb) - s->pb.buf) * 8; 745 s->frame_bits = (pbBufPtr(&s->pb) - s->pb.buf) * 8;
608 s->total_bits += s->frame_bits; 746 s->total_bits += s->frame_bits;
609 avctx->frame_bits = s->frame_bits; 747 avctx->frame_bits = s->frame_bits;
610 //printf("fcode: %d, type: %d, head: %d, mv: %d, misc: %d, frame: %d, itex: %d, ptex: %d\n", 748 //printf("fcode: %d, type: %d, head: %d, mv: %d, misc: %d, frame: %d, itex: %d, ptex: %d\n",
611 //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); 749 //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);
612 750
613 avctx->quality = s->qscale;
614 if (avctx->get_psnr) { 751 if (avctx->get_psnr) {
615 /* At this point pict->data should have the original frame */ 752 /* At this point pict->data should have the original frame */
616 /* an s->current_picture should have the coded/decoded frame */ 753 /* an s->current_picture should have the coded/decoded frame */
617 get_psnr(pict->data, s->current_picture, 754 get_psnr(pict->data, s->current_picture,
618 pict->linesize, s->linesize, avctx); 755 pict->linesize, s->linesize, avctx);
756 // printf("%f\n", avctx->psnr_y);
619 } 757 }
620 return pbBufPtr(&s->pb) - s->pb.buf; 758 return pbBufPtr(&s->pb) - s->pb.buf;
621 } 759 }
622 760
623 static inline int clip(int a, int amin, int amax) 761 static inline int clip(int a, int amin, int amax)
635 int dest_offset, 773 int dest_offset,
636 UINT8 **ref_picture, int src_offset, 774 UINT8 **ref_picture, int src_offset,
637 int h) 775 int h)
638 { 776 {
639 UINT8 *ptr; 777 UINT8 *ptr;
640 int dxy, offset, mx, my, src_x, src_y, height, linesize; 778 int offset, src_x, src_y, linesize;
641 int motion_x, motion_y; 779 int motion_x, motion_y;
642 780
643 if(s->real_sprite_warping_points>1) printf("more than 1 warp point isnt supported\n"); 781 if(s->real_sprite_warping_points>1) printf("more than 1 warp point isnt supported\n");
644 motion_x= s->sprite_offset[0][0]; 782 motion_x= s->sprite_offset[0][0];
645 motion_y= s->sprite_offset[0][1]; 783 motion_y= s->sprite_offset[0][1];
696 if(s->quarter_sample) 834 if(s->quarter_sample)
697 { 835 {
698 motion_x>>=1; 836 motion_x>>=1;
699 motion_y>>=1; 837 motion_y>>=1;
700 } 838 }
839
701 dxy = ((motion_y & 1) << 1) | (motion_x & 1); 840 dxy = ((motion_y & 1) << 1) | (motion_x & 1);
702 src_x = s->mb_x * 16 + (motion_x >> 1); 841 src_x = s->mb_x * 16 + (motion_x >> 1);
703 src_y = s->mb_y * (16 >> field_based) + (motion_y >> 1); 842 src_y = s->mb_y * (16 >> field_based) + (motion_y >> 1);
704 843
705 /* WARNING: do no forget half pels */ 844 /* WARNING: do no forget half pels */
943 { 1082 {
944 if (s->block_last_index[i] >= 0) { 1083 if (s->block_last_index[i] >= 0) {
945 if (!s->mpeg2) 1084 if (!s->mpeg2)
946 if(s->encoding || (!s->h263_msmpeg4)) 1085 if(s->encoding || (!s->h263_msmpeg4))
947 s->dct_unquantize(s, block, i, s->qscale); 1086 s->dct_unquantize(s, block, i, s->qscale);
1087
948 ff_idct (block); 1088 ff_idct (block);
949 add_pixels_clamped(block, dest, line_size); 1089 add_pixels_clamped(block, dest, line_size);
950 } 1090 }
951 } 1091 }
952 1092
1018 } 1158 }
1019 else if (s->h263_pred || s->h263_aic) 1159 else if (s->h263_pred || s->h263_aic)
1020 s->mbintra_table[mb_x + mb_y*s->mb_width]=1; 1160 s->mbintra_table[mb_x + mb_y*s->mb_width]=1;
1021 1161
1022 /* update motion predictor, not for B-frames as they need the motion_val from the last P/S-Frame */ 1162 /* update motion predictor, not for B-frames as they need the motion_val from the last P/S-Frame */
1023 if (s->out_format == FMT_H263) { 1163 if (s->out_format == FMT_H263) { //FIXME move into h263.c if possible, format specific stuff shouldnt be here
1024 if(s->pict_type!=B_TYPE){ 1164 if(s->pict_type!=B_TYPE){
1025 int xy, wrap, motion_x, motion_y; 1165 int xy, wrap, motion_x, motion_y;
1026 1166
1027 wrap = 2 * s->mb_width + 2; 1167 wrap = 2 * s->mb_width + 2;
1028 xy = 2 * mb_x + 1 + (2 * mb_y + 1) * wrap; 1168 xy = 2 * mb_x + 1 + (2 * mb_y + 1) * wrap;
1045 s->motion_val[xy + 1 + wrap][1] = motion_y; 1185 s->motion_val[xy + 1 + wrap][1] = motion_y;
1046 } 1186 }
1047 } 1187 }
1048 } 1188 }
1049 1189
1050 if (!s->intra_only) { 1190 if (!(s->encoding && (s->intra_only || s->pict_type==B_TYPE))) {
1051 UINT8 *dest_y, *dest_cb, *dest_cr; 1191 UINT8 *dest_y, *dest_cb, *dest_cr;
1052 UINT8 *mbskip_ptr; 1192 UINT8 *mbskip_ptr;
1053 1193
1054 /* avoid copy if macroblock skipped in last frame too */ 1194 /* avoid copy if macroblock skipped in last frame too
1055 if (!s->encoding && s->pict_type != B_TYPE) { 1195 dont touch it for B-frames as they need the skip info from the next p-frame */
1196 if (s->pict_type != B_TYPE) {
1056 mbskip_ptr = &s->mbskip_table[s->mb_y * s->mb_width + s->mb_x]; 1197 mbskip_ptr = &s->mbskip_table[s->mb_y * s->mb_width + s->mb_x];
1057 if (s->mb_skiped) { 1198 if (s->mb_skiped) {
1058 s->mb_skiped = 0; 1199 s->mb_skiped = 0;
1059 /* if previous was skipped too, then nothing to do ! */ 1200 /* if previous was skipped too, then nothing to do !
1060 if (*mbskip_ptr != 0) 1201 skip only during decoding as we might trash the buffers during encoding a bit */
1202 if (*mbskip_ptr != 0 && !s->encoding)
1061 goto the_end; 1203 goto the_end;
1062 *mbskip_ptr = 1; /* indicate that this time we skiped it */ 1204 *mbskip_ptr = 1; /* indicate that this time we skiped it */
1063 } else { 1205 } else {
1064 *mbskip_ptr = 0; /* not skipped */ 1206 *mbskip_ptr = 0; /* not skipped */
1065 } 1207 }
1077 dct_offset = s->linesize * 8; 1219 dct_offset = s->linesize * 8;
1078 } 1220 }
1079 1221
1080 if (!s->mb_intra) { 1222 if (!s->mb_intra) {
1081 /* motion handling */ 1223 /* motion handling */
1082 if (!s->no_rounding){ 1224 if((s->flags&CODEC_FLAG_HQ) || (!s->encoding)){
1083 op_pix = put_pixels_tab; 1225 if (!s->no_rounding){
1084 op_qpix= qpel_mc_rnd_tab; 1226 op_pix = put_pixels_tab;
1085 }else{ 1227 op_qpix= qpel_mc_rnd_tab;
1086 op_pix = put_no_rnd_pixels_tab; 1228 }else{
1087 op_qpix= qpel_mc_no_rnd_tab; 1229 op_pix = put_no_rnd_pixels_tab;
1088 } 1230 op_qpix= qpel_mc_no_rnd_tab;
1089 1231 }
1090 if (s->mv_dir & MV_DIR_FORWARD) { 1232
1091 MPV_motion(s, dest_y, dest_cb, dest_cr, 0, s->last_picture, op_pix, op_qpix); 1233 if (s->mv_dir & MV_DIR_FORWARD) {
1092 if (!s->no_rounding) 1234 MPV_motion(s, dest_y, dest_cb, dest_cr, 0, s->last_picture, op_pix, op_qpix);
1093 op_pix = avg_pixels_tab; 1235 if (!s->no_rounding)
1094 else 1236 op_pix = avg_pixels_tab;
1095 op_pix = avg_no_rnd_pixels_tab; 1237 else
1096 } 1238 op_pix = avg_no_rnd_pixels_tab;
1097 if (s->mv_dir & MV_DIR_BACKWARD) { 1239 }
1098 MPV_motion(s, dest_y, dest_cb, dest_cr, 1, s->next_picture, op_pix, op_qpix); 1240 if (s->mv_dir & MV_DIR_BACKWARD) {
1241 MPV_motion(s, dest_y, dest_cb, dest_cr, 1, s->next_picture, op_pix, op_qpix);
1242 }
1099 } 1243 }
1100 1244
1101 /* add dct residue */ 1245 /* add dct residue */
1102 add_dct(s, block[0], 0, dest_y, dct_linesize); 1246 add_dct(s, block[0], 0, dest_y, dct_linesize);
1103 add_dct(s, block[1], 1, dest_y + 8, dct_linesize); 1247 add_dct(s, block[1], 1, dest_y + 8, dct_linesize);
1119 } 1263 }
1120 the_end: 1264 the_end:
1121 emms_c(); //FIXME remove 1265 emms_c(); //FIXME remove
1122 } 1266 }
1123 1267
1124 static void encode_mb(MpegEncContext *s) 1268
1125 { 1269 static void encode_mb(MpegEncContext *s, int motion_x, int motion_y)
1126 int wrap; 1270 {
1127 const int mb_x= s->mb_x; 1271 const int mb_x= s->mb_x;
1128 const int mb_y= s->mb_y; 1272 const int mb_y= s->mb_y;
1129 UINT8 *ptr;
1130 const int motion_x= s->mv[0][0][0];
1131 const int motion_y= s->mv[0][0][1];
1132 int i; 1273 int i;
1133 1274 #if 0
1134 /* get the pixels */ 1275 if (s->interlaced_dct) {
1135 wrap = s->linesize; 1276 dct_linesize = s->linesize * 2;
1136 ptr = s->new_picture[0] + (mb_y * 16 * wrap) + mb_x * 16; 1277 dct_offset = s->linesize;
1137 get_pixels(s->block[0], ptr, wrap); 1278 } else {
1138 get_pixels(s->block[1], ptr + 8, wrap); 1279 dct_linesize = s->linesize;
1139 get_pixels(s->block[2], ptr + 8 * wrap, wrap); 1280 dct_offset = s->linesize * 8;
1140 get_pixels(s->block[3], ptr + 8 * wrap + 8, wrap); 1281 }
1141 wrap = s->linesize >> 1; 1282 #endif
1142 ptr = s->new_picture[1] + (mb_y * 8 * wrap) + mb_x * 8; 1283
1143 get_pixels(s->block[4], ptr, wrap); 1284 if (s->mb_intra) {
1144 1285 UINT8 *ptr;
1145 wrap = s->linesize >> 1; 1286 int wrap;
1146 ptr = s->new_picture[2] + (mb_y * 8 * wrap) + mb_x * 8; 1287
1147 get_pixels(s->block[5], ptr, wrap); 1288 wrap = s->linesize;
1148 1289 ptr = s->new_picture[0] + (mb_y * 16 * wrap) + mb_x * 16;
1149 /* subtract previous frame if non intra */ 1290 get_pixels(s->block[0], ptr , wrap);
1150 if (!s->mb_intra) { 1291 get_pixels(s->block[1], ptr + 8, wrap);
1151 int dxy, offset, mx, my; 1292 get_pixels(s->block[2], ptr + 8 * wrap , wrap);
1152 1293 get_pixels(s->block[3], ptr + 8 * wrap + 8, wrap);
1153 if(s->mv_type==MV_TYPE_16X16){ 1294
1154 dxy = ((motion_y & 1) << 1) | (motion_x & 1); 1295 wrap >>=1;
1155 ptr = s->last_picture[0] + 1296 ptr = s->new_picture[1] + (mb_y * 8 * wrap) + mb_x * 8;
1156 ((mb_y * 16 + (motion_y >> 1)) * s->linesize) + 1297 get_pixels(s->block[4], ptr, wrap);
1157 (mb_x * 16 + (motion_x >> 1)); 1298
1158 1299 ptr = s->new_picture[2] + (mb_y * 8 * wrap) + mb_x * 8;
1159 sub_pixels_2(s->block[0], ptr, s->linesize, dxy); 1300 get_pixels(s->block[5], ptr, wrap);
1160 sub_pixels_2(s->block[1], ptr + 8, s->linesize, dxy); 1301 }else{
1161 sub_pixels_2(s->block[2], ptr + s->linesize * 8, s->linesize, dxy); 1302 op_pixels_func *op_pix;
1162 sub_pixels_2(s->block[3], ptr + 8 + s->linesize * 8, s->linesize ,dxy); 1303 qpel_mc_func *op_qpix;
1163 1304 UINT8 *dest_y, *dest_cb, *dest_cr;
1164 if (s->out_format == FMT_H263) { 1305 UINT8 *ptr;
1165 /* special rounding for h263 */ 1306 int wrap;
1166 dxy = 0; 1307
1167 if ((motion_x & 3) != 0) 1308 dest_y = s->current_picture[0] + (mb_y * 16 * s->linesize ) + mb_x * 16;
1168 dxy |= 1; 1309 dest_cb = s->current_picture[1] + (mb_y * 8 * (s->linesize >> 1)) + mb_x * 8;
1169 if ((motion_y & 3) != 0) 1310 dest_cr = s->current_picture[2] + (mb_y * 8 * (s->linesize >> 1)) + mb_x * 8;
1170 dxy |= 2; 1311
1171 mx = motion_x >> 2; 1312 if (!s->no_rounding){
1172 my = motion_y >> 2; 1313 op_pix = put_pixels_tab;
1173 } else { 1314 op_qpix= qpel_mc_rnd_tab;
1174 mx = motion_x / 2;
1175 my = motion_y / 2;
1176 dxy = ((my & 1) << 1) | (mx & 1);
1177 mx >>= 1;
1178 my >>= 1;
1179 }
1180 offset = ((mb_y * 8 + my) * (s->linesize >> 1)) + (mb_x * 8 + mx);
1181 ptr = s->last_picture[1] + offset;
1182 sub_pixels_2(s->block[4], ptr, s->linesize >> 1, dxy);
1183 ptr = s->last_picture[2] + offset;
1184 sub_pixels_2(s->block[5], ptr, s->linesize >> 1, dxy);
1185 }else{ 1315 }else{
1186 int src_x, src_y; 1316 op_pix = put_no_rnd_pixels_tab;
1187 1317 op_qpix= qpel_mc_no_rnd_tab;
1188 for(i=0;i<4;i++) { 1318 }
1189 int motion_x = s->mv[0][i][0]; 1319
1190 int motion_y = s->mv[0][i][1]; 1320 if (s->mv_dir & MV_DIR_FORWARD) {
1191 1321 MPV_motion(s, dest_y, dest_cb, dest_cr, 0, s->last_picture, op_pix, op_qpix);
1192 dxy = ((motion_y & 1) << 1) | (motion_x & 1); 1322 if (!s->no_rounding)
1193 src_x = mb_x * 16 + (motion_x >> 1) + (i & 1) * 8; 1323 op_pix = avg_pixels_tab;
1194 src_y = mb_y * 16 + (motion_y >> 1) + (i >>1) * 8; 1324 else
1195 1325 op_pix = avg_no_rnd_pixels_tab;
1196 ptr = s->last_picture[0] + (src_y * s->linesize) + (src_x); 1326 }
1197 sub_pixels_2(s->block[i], ptr, s->linesize, dxy); 1327 if (s->mv_dir & MV_DIR_BACKWARD) {
1198 } 1328 MPV_motion(s, dest_y, dest_cb, dest_cr, 1, s->next_picture, op_pix, op_qpix);
1199 /* In case of 8X8, we construct a single chroma motion vector 1329 }
1200 with a special rounding */ 1330 wrap = s->linesize;
1201 mx = 0; 1331 ptr = s->new_picture[0] + (mb_y * 16 * wrap) + mb_x * 16;
1202 my = 0; 1332 diff_pixels(s->block[0], ptr , dest_y , wrap);
1203 for(i=0;i<4;i++) { 1333 diff_pixels(s->block[1], ptr + 8, dest_y + 8, wrap);
1204 mx += s->mv[0][i][0]; 1334 diff_pixels(s->block[2], ptr + 8 * wrap , dest_y + 8 * wrap , wrap);
1205 my += s->mv[0][i][1]; 1335 diff_pixels(s->block[3], ptr + 8 * wrap + 8, dest_y + 8 * wrap + 8, wrap);
1206 } 1336
1207 if (mx >= 0) 1337 wrap >>=1;
1208 mx = (h263_chroma_roundtab[mx & 0xf] + ((mx >> 3) & ~1)); 1338 ptr = s->new_picture[1] + (mb_y * 8 * wrap) + mb_x * 8;
1209 else { 1339 diff_pixels(s->block[4], ptr, dest_cb, wrap);
1210 mx = -mx; 1340
1211 mx = -(h263_chroma_roundtab[mx & 0xf] + ((mx >> 3) & ~1)); 1341 ptr = s->new_picture[2] + (mb_y * 8 * wrap) + mb_x * 8;
1212 } 1342 diff_pixels(s->block[5], ptr, dest_cr, wrap);
1213 if (my >= 0)
1214 my = (h263_chroma_roundtab[my & 0xf] + ((my >> 3) & ~1));
1215 else {
1216 my = -my;
1217 my = -(h263_chroma_roundtab[my & 0xf] + ((my >> 3) & ~1));
1218 }
1219 dxy = ((my & 1) << 1) | (mx & 1);
1220 mx >>= 1;
1221 my >>= 1;
1222
1223 src_x = mb_x * 8 + mx;
1224 src_y = mb_y * 8 + my;
1225 src_x = clip(src_x, -8, s->width/2);
1226 if (src_x == s->width/2)
1227 dxy &= ~1;
1228 src_y = clip(src_y, -8, s->height/2);
1229 if (src_y == s->height/2)
1230 dxy &= ~2;
1231
1232 offset = (src_y * (s->linesize >> 1)) + src_x;
1233 ptr = s->last_picture[1] + offset;
1234 sub_pixels_2(s->block[4], ptr, s->linesize >> 1, dxy);
1235 ptr = s->last_picture[2] + offset;
1236 sub_pixels_2(s->block[5], ptr, s->linesize >> 1, dxy);
1237 }
1238 } 1343 }
1239 1344
1240 #if 0 1345 #if 0
1241 { 1346 {
1242 float adap_parm; 1347 float adap_parm;
1312 1417
1313 s->last_mc_mb_var = s->mc_mb_var; 1418 s->last_mc_mb_var = s->mc_mb_var;
1314 /* Reset the average MB variance */ 1419 /* Reset the average MB variance */
1315 s->avg_mb_var = 0; 1420 s->avg_mb_var = 0;
1316 s->mc_mb_var = 0; 1421 s->mc_mb_var = 0;
1422
1317 /* Estimate motion for every MB */ 1423 /* Estimate motion for every MB */
1318 if(s->pict_type == P_TYPE){ 1424 if(s->pict_type != I_TYPE){
1425 // int16_t (*tmp)[2]= s->p_mv_table;
1426 // s->p_mv_table= s->last_mv_table;
1427 // s->last_mv_table= s->mv_table;
1428
1319 for(mb_y=0; mb_y < s->mb_height; mb_y++) { 1429 for(mb_y=0; mb_y < s->mb_height; mb_y++) {
1320 s->block_index[0]= s->block_wrap[0]*(mb_y*2 + 1) - 1; 1430 s->block_index[0]= s->block_wrap[0]*(mb_y*2 + 1) - 1;
1321 s->block_index[1]= s->block_wrap[0]*(mb_y*2 + 1); 1431 s->block_index[1]= s->block_wrap[0]*(mb_y*2 + 1);
1322 s->block_index[2]= s->block_wrap[0]*(mb_y*2 + 2) - 1; 1432 s->block_index[2]= s->block_wrap[0]*(mb_y*2 + 2) - 1;
1323 s->block_index[3]= s->block_wrap[0]*(mb_y*2 + 2); 1433 s->block_index[3]= s->block_wrap[0]*(mb_y*2 + 2);
1328 s->block_index[1]+=2; 1438 s->block_index[1]+=2;
1329 s->block_index[2]+=2; 1439 s->block_index[2]+=2;
1330 s->block_index[3]+=2; 1440 s->block_index[3]+=2;
1331 1441
1332 /* compute motion vector & mb_type and store in context */ 1442 /* compute motion vector & mb_type and store in context */
1333 estimate_motion(s, mb_x, mb_y); 1443 if(s->pict_type==B_TYPE)
1444 ff_estimate_b_frame_motion(s, mb_x, mb_y);
1445 else
1446 ff_estimate_p_frame_motion(s, mb_x, mb_y);
1334 // s->mb_type[mb_y*s->mb_width + mb_x]=MB_TYPE_INTER; 1447 // s->mb_type[mb_y*s->mb_width + mb_x]=MB_TYPE_INTER;
1335 } 1448 }
1336 } 1449 }
1337 emms_c(); 1450 emms_c();
1338 }else{ 1451 }else if(s->pict_type == I_TYPE){
1339 /* I-Frame */ 1452 /* I-Frame */
1340 //FIXME do we need to zero them? 1453 //FIXME do we need to zero them?
1341 memset(s->motion_val[0], 0, sizeof(INT16)*(s->mb_width*2 + 2)*(s->mb_height*2 + 2)*2); 1454 memset(s->motion_val[0], 0, sizeof(INT16)*(s->mb_width*2 + 2)*(s->mb_height*2 + 2)*2);
1342 memset(s->mv_table[0] , 0, sizeof(INT16)*s->mb_width*s->mb_height); 1455 memset(s->p_mv_table , 0, sizeof(INT16)*(s->mb_width+2)*(s->mb_height+2)*2);
1343 memset(s->mv_table[1] , 0, sizeof(INT16)*s->mb_width*s->mb_height);
1344 memset(s->mb_type , MB_TYPE_INTRA, sizeof(UINT8)*s->mb_width*s->mb_height); 1456 memset(s->mb_type , MB_TYPE_INTRA, sizeof(UINT8)*s->mb_width*s->mb_height);
1345 } 1457 }
1346 1458
1347 if(s->avg_mb_var < s->mc_mb_var && s->pict_type != B_TYPE && (!s->force_type)){ //FIXME subtract MV bits 1459 if(s->avg_mb_var < s->mc_mb_var && s->pict_type != B_TYPE && (!s->force_type) && s->max_b_frames==0){ //FIXME subtract MV bits
1460 // FIXME b-frames & scene change detection
1461 s->input_pict_type= I_TYPE;
1348 s->pict_type= I_TYPE; 1462 s->pict_type= I_TYPE;
1349 s->picture_in_gop_number=0; 1463 s->input_picture_in_gop_number=0;
1350 memset(s->mb_type , MB_TYPE_INTRA, sizeof(UINT8)*s->mb_width*s->mb_height); 1464 memset(s->mb_type , MB_TYPE_INTRA, sizeof(UINT8)*s->mb_width*s->mb_height);
1351 //printf("Scene change detected, encoding as I Frame\n"); 1465 //printf("Scene change detected, encoding as I Frame\n");
1352 } 1466 }
1353 1467
1354 /* find best f_code for ME which do unlimited searches */ 1468 if(s->pict_type==P_TYPE || s->pict_type==S_TYPE)
1355 if(s->pict_type == P_TYPE && s->me_method >= 5){ 1469 s->f_code= ff_get_best_fcode(s, s->p_mv_table, MB_TYPE_INTER);
1356 int mv_num[8]; 1470 ff_fix_long_p_mvs(s);
1357 int i; 1471 if(s->pict_type==B_TYPE){
1358 int loose=0; 1472 s->f_code= ff_get_best_fcode(s, s->b_forw_mv_table, MB_TYPE_FORWARD);
1359 UINT8 * fcode_tab= s->fcode_tab; 1473 s->b_code= ff_get_best_fcode(s, s->b_back_mv_table, MB_TYPE_BACKWARD);
1360 1474 //FIXME if BIDIR != for&back
1361 for(i=0; i<8; i++) mv_num[i]=0; 1475 ff_fix_long_b_mvs(s, s->b_forw_mv_table, s->f_code, MB_TYPE_FORWARD |MB_TYPE_BIDIR);
1362 1476 ff_fix_long_b_mvs(s, s->b_back_mv_table, s->b_code, MB_TYPE_BACKWARD|MB_TYPE_BIDIR);
1363 for(i=0; i<s->mb_num; i++){ 1477 }
1364 if(s->mb_type[i] & MB_TYPE_INTER){ 1478
1365 mv_num[ fcode_tab[s->mv_table[0][i] + MAX_MV] ]++;
1366 mv_num[ fcode_tab[s->mv_table[1][i] + MAX_MV] ]++;
1367 //printf("%d %d %d\n", s->mv_table[0][i], fcode_tab[s->mv_table[0][i] + MAX_MV], i);
1368 }
1369 //else printf("I");
1370 }
1371
1372 for(i=MAX_FCODE; i>1; i--){
1373 loose+= mv_num[i];
1374 if(loose > 10) break; //FIXME this is pretty ineffective
1375 }
1376 s->f_code= i;
1377 /* for(i=0; i<=MAX_FCODE; i++){
1378 printf("%d ", mv_num[i]);
1379 }
1380 printf("\n");*/
1381 }else{
1382 s->f_code= 1;
1383 }
1384
1385 //printf("f_code %d ///\n", s->f_code); 1479 //printf("f_code %d ///\n", s->f_code);
1386 /* convert MBs with too long MVs to I-Blocks */
1387 if(s->pict_type==P_TYPE){
1388 int i, x, y;
1389 const int f_code= s->f_code;
1390 UINT8 * fcode_tab= s->fcode_tab;
1391 //FIXME try to clip instead of intra izing ;)
1392 /* clip / convert to intra 16x16 type MVs */
1393 for(i=0; i<s->mb_num; i++){
1394 if(s->mb_type[i]&MB_TYPE_INTER){
1395 if( fcode_tab[s->mv_table[0][i] + MAX_MV] > f_code
1396 || fcode_tab[s->mv_table[0][i] + MAX_MV] == 0
1397 || fcode_tab[s->mv_table[1][i] + MAX_MV] > f_code
1398 || fcode_tab[s->mv_table[1][i] + MAX_MV] == 0 ){
1399 s->mb_type[i] &= ~MB_TYPE_INTER;
1400 s->mb_type[i] |= MB_TYPE_INTRA;
1401 s->mv_table[0][i] = 0;
1402 s->mv_table[1][i] = 0;
1403 }
1404 }
1405 }
1406
1407 if(s->flags&CODEC_FLAG_4MV){
1408 int wrap= 2+ s->mb_width*2;
1409
1410 /* clip / convert to intra 8x8 type MVs */
1411 for(y=0; y<s->mb_height; y++){
1412 int xy= (y*2 + 1)*wrap + 1;
1413 i= y*s->mb_width;
1414
1415 for(x=0; x<s->mb_width; x++){
1416 if(s->mb_type[i]&MB_TYPE_INTER4V){
1417 int block;
1418 for(block=0; block<4; block++){
1419 int off= (block& 1) + (block>>1)*wrap;
1420 int mx= s->motion_val[ xy + off ][0];
1421 int my= s->motion_val[ xy + off ][1];
1422
1423 if( fcode_tab[mx + MAX_MV] > f_code
1424 || fcode_tab[mx + MAX_MV] == 0
1425 || fcode_tab[my + MAX_MV] > f_code
1426 || fcode_tab[my + MAX_MV] == 0 ){
1427 s->mb_type[i] &= ~MB_TYPE_INTER4V;
1428 s->mb_type[i] |= MB_TYPE_INTRA;
1429 }
1430 }
1431 xy+=2;
1432 i++;
1433 }
1434 }
1435 }
1436 }
1437 }
1438 1480
1439 // printf("%d %d\n", s->avg_mb_var, s->mc_mb_var); 1481 // printf("%d %d\n", s->avg_mb_var, s->mc_mb_var);
1440 1482
1441 if (!s->fixed_qscale) 1483 if (!s->fixed_qscale)
1442 s->qscale = rate_estimate_qscale(s); 1484 s->qscale = rate_estimate_qscale(s);
1524 s->block_index[2]= s->block_wrap[0]*(mb_y*2 + 2) - 1; 1566 s->block_index[2]= s->block_wrap[0]*(mb_y*2 + 2) - 1;
1525 s->block_index[3]= s->block_wrap[0]*(mb_y*2 + 2); 1567 s->block_index[3]= s->block_wrap[0]*(mb_y*2 + 2);
1526 s->block_index[4]= s->block_wrap[4]*(mb_y + 1) + s->block_wrap[0]*(s->mb_height*2 + 2); 1568 s->block_index[4]= s->block_wrap[4]*(mb_y + 1) + s->block_wrap[0]*(s->mb_height*2 + 2);
1527 s->block_index[5]= s->block_wrap[4]*(mb_y + 1 + s->mb_height + 2) + s->block_wrap[0]*(s->mb_height*2 + 2); 1569 s->block_index[5]= s->block_wrap[4]*(mb_y + 1 + s->mb_height + 2) + s->block_wrap[0]*(s->mb_height*2 + 2);
1528 for(mb_x=0; mb_x < s->mb_width; mb_x++) { 1570 for(mb_x=0; mb_x < s->mb_width; mb_x++) {
1529 const int mb_type= s->mb_type[mb_y * s->mb_width + mb_x]; 1571 /*const */int mb_type= s->mb_type[mb_y * s->mb_width + mb_x];
1530 PutBitContext pb; 1572 PutBitContext pb;
1531 int d; 1573 int d;
1532 int dmin=10000000; 1574 int dmin=10000000;
1533 int best=0; 1575 int best=0;
1534 1576
1539 s->block_index[2]+=2; 1581 s->block_index[2]+=2;
1540 s->block_index[3]+=2; 1582 s->block_index[3]+=2;
1541 s->block_index[4]++; 1583 s->block_index[4]++;
1542 s->block_index[5]++; 1584 s->block_index[5]++;
1543 1585
1544 s->mv_dir = MV_DIR_FORWARD;
1545 if(mb_type & (mb_type-1)){ // more than 1 MB type possible 1586 if(mb_type & (mb_type-1)){ // more than 1 MB type possible
1546 pb= s->pb; 1587 pb= s->pb;
1588 s->mv_dir = MV_DIR_FORWARD;
1547 if(mb_type&MB_TYPE_INTER){ 1589 if(mb_type&MB_TYPE_INTER){
1590 int xy= (mb_y+1) * (s->mb_width+2) + mb_x + 1;
1548 s->mv_type = MV_TYPE_16X16; 1591 s->mv_type = MV_TYPE_16X16;
1549 s->mb_intra= 0; 1592 s->mb_intra= 0;
1550 s->mv[0][0][0] = s->mv_table[0][mb_y * s->mb_width + mb_x]; 1593 s->mv[0][0][0] = s->p_mv_table[xy][0];
1551 s->mv[0][0][1] = s->mv_table[1][mb_y * s->mb_width + mb_x]; 1594 s->mv[0][0][1] = s->p_mv_table[xy][1];
1552 init_put_bits(&s->pb, bit_buf[1], 3000, NULL, NULL); 1595 init_put_bits(&s->pb, bit_buf[1], 3000, NULL, NULL);
1553 s->block= s->inter_block; 1596 s->block= s->inter_block;
1554 1597
1555 encode_mb(s); 1598 encode_mb(s, s->mv[0][0][0], s->mv[0][0][1]);
1556 d= get_bit_count(&s->pb); 1599 d= get_bit_count(&s->pb);
1557 if(d<dmin){ 1600 if(d<dmin){
1558 flush_put_bits(&s->pb); 1601 flush_put_bits(&s->pb);
1559 dmin=d; 1602 dmin=d;
1560 best_s.mv[0][0][0]= s->mv[0][0][0]; 1603 best_s.mv[0][0][0]= s->mv[0][0][0];
1576 s->mv[0][i][1] = s->motion_val[s->block_index[i]][1]; 1619 s->mv[0][i][1] = s->motion_val[s->block_index[i]][1];
1577 } 1620 }
1578 init_put_bits(&s->pb, bit_buf[2], 3000, NULL, NULL); 1621 init_put_bits(&s->pb, bit_buf[2], 3000, NULL, NULL);
1579 s->block= s->inter4v_block; 1622 s->block= s->inter4v_block;
1580 1623
1581 encode_mb(s); 1624 encode_mb(s, 0, 0);
1582 d= get_bit_count(&s->pb); 1625 d= get_bit_count(&s->pb);
1583 if(d<dmin){ 1626 if(d<dmin && 0){
1584 flush_put_bits(&s->pb); 1627 flush_put_bits(&s->pb);
1585 dmin=d; 1628 dmin=d;
1586 for(i=0; i<4; i++){ 1629 for(i=0; i<4; i++){
1587 best_s.mv[0][i][0] = s->mv[0][i][0]; 1630 best_s.mv[0][i][0] = s->mv[0][i][0];
1588 best_s.mv[0][i][1] = s->mv[0][i][1]; 1631 best_s.mv[0][i][1] = s->mv[0][i][1];
1602 s->mv[0][0][0] = 0; 1645 s->mv[0][0][0] = 0;
1603 s->mv[0][0][1] = 0; 1646 s->mv[0][0][1] = 0;
1604 init_put_bits(&s->pb, bit_buf[0], 3000, NULL, NULL); 1647 init_put_bits(&s->pb, bit_buf[0], 3000, NULL, NULL);
1605 s->block= s->intra_block; 1648 s->block= s->intra_block;
1606 1649
1607 encode_mb(s); 1650 encode_mb(s, 0, 0);
1608 d= get_bit_count(&s->pb); 1651 d= get_bit_count(&s->pb);
1609 if(d<dmin){ 1652 if(d<dmin){
1610 flush_put_bits(&s->pb); 1653 flush_put_bits(&s->pb);
1611 dmin=d; 1654 dmin=d;
1612 best_s.mv[0][0][0]= 0; 1655 best_s.mv[0][0][0]= 0;
1632 s->block_last_index[i]= best_s.block_last_index[i]; 1675 s->block_last_index[i]= best_s.block_last_index[i];
1633 copy_bits(&pb, bit_buf[best], dmin); 1676 copy_bits(&pb, bit_buf[best], dmin);
1634 s->block= best_s.block; 1677 s->block= best_s.block;
1635 s->pb= pb; 1678 s->pb= pb;
1636 } else { 1679 } else {
1680 int motion_x, motion_y;
1681 s->mv_type=MV_TYPE_16X16;
1637 // only one MB-Type possible 1682 // only one MB-Type possible
1683 //FIXME convert to swicth()
1638 if(mb_type&MB_TYPE_INTRA){ 1684 if(mb_type&MB_TYPE_INTRA){
1685 s->mv_dir = MV_DIR_FORWARD;
1639 s->mb_intra= 1; 1686 s->mb_intra= 1;
1687 motion_x= s->mv[0][0][0] = 0;
1688 motion_y= s->mv[0][0][1] = 0;
1689 }else if(mb_type&MB_TYPE_INTER){
1690 int xy= (mb_y+1) * (s->mb_width+2) + mb_x + 1;
1691 s->mv_dir = MV_DIR_FORWARD;
1692 s->mb_intra= 0;
1693 motion_x= s->mv[0][0][0] = s->p_mv_table[xy][0];
1694 motion_y= s->mv[0][0][1] = s->p_mv_table[xy][1];
1695 }else if(mb_type&MB_TYPE_DIRECT){
1696 int xy= (mb_y+1) * (s->mb_width+2) + mb_x + 1;
1697 s->mv_dir = MV_DIR_FORWARD | MV_DIR_BACKWARD | MV_DIRECT;
1698 s->mb_intra= 0;
1699 motion_x=0;
1700 motion_y=0;
1640 s->mv[0][0][0] = 0; 1701 s->mv[0][0][0] = 0;
1641 s->mv[0][0][1] = 0; 1702 s->mv[0][0][1] = 0;
1703 s->mv[1][0][0] = 0;
1704 s->mv[1][0][1] = 0;
1705 }else if(mb_type&MB_TYPE_BIDIR){
1706 int xy= (mb_y+1) * (s->mb_width+2) + mb_x + 1;
1707 s->mv_dir = MV_DIR_FORWARD | MV_DIR_BACKWARD;
1708 s->mb_intra= 0;
1709 motion_x=0;
1710 motion_y=0;
1711 s->mv[0][0][0] = s->b_bidir_forw_mv_table[xy][0];
1712 s->mv[0][0][1] = s->b_bidir_forw_mv_table[xy][1];
1713 s->mv[1][0][0] = s->b_bidir_back_mv_table[xy][0];
1714 s->mv[1][0][1] = s->b_bidir_back_mv_table[xy][1];
1715 }else if(mb_type&MB_TYPE_BACKWARD){
1716 int xy= (mb_y+1) * (s->mb_width+2) + mb_x + 1;
1717 s->mv_dir = MV_DIR_BACKWARD;
1718 s->mb_intra= 0;
1719 motion_x= s->mv[1][0][0] = s->b_back_mv_table[xy][0];
1720 motion_y= s->mv[1][0][1] = s->b_back_mv_table[xy][1];
1721 }else if(mb_type&MB_TYPE_FORWARD){
1722 int xy= (mb_y+1) * (s->mb_width+2) + mb_x + 1;
1723 s->mv_dir = MV_DIR_FORWARD;
1724 s->mb_intra= 0;
1725 motion_x= s->mv[0][0][0] = s->b_forw_mv_table[xy][0];
1726 motion_y= s->mv[0][0][1] = s->b_forw_mv_table[xy][1];
1727 // printf(" %d %d ", motion_x, motion_y);
1642 }else{ 1728 }else{
1643 s->mb_intra= 0; 1729 motion_x=motion_y=0; //gcc warning fix
1644 s->mv[0][0][0] = s->mv_table[0][mb_y * s->mb_width + mb_x]; 1730 printf("illegal MB type\n");
1645 s->mv[0][0][1] = s->mv_table[1][mb_y * s->mb_width + mb_x];
1646 } 1731 }
1647 encode_mb(s); 1732 encode_mb(s, motion_x, motion_y);
1648 } 1733 }
1649 1734
1650 MPV_decode_mb(s, s->block); 1735 MPV_decode_mb(s, s->block);
1651 } 1736 }
1652 1737
1902 } 1987 }
1903 } 1988 }
1904 1989
1905 /* rate control */ 1990 /* rate control */
1906 1991
1907 /* an I frame is I_FRAME_SIZE_RATIO bigger than a P frame */
1908 #define I_FRAME_SIZE_RATIO 3.0
1909 #define QSCALE_K 20
1910
1911 static void rate_control_init(MpegEncContext *s) 1992 static void rate_control_init(MpegEncContext *s)
1912 { 1993 {
1913 #if 1
1914 emms_c(); 1994 emms_c();
1915 1995
1916 //initial values, they dont really matter as they will be totally different within a few frames 1996 //initial values, they dont really matter as they will be totally different within a few frames
1917 s->i_pred.coeff= s->p_pred.coeff= 7.0; 1997 s->i_pred.coeff= s->p_pred.coeff= 7.0;
1918 s->i_pred.count= s->p_pred.count= 1.0; 1998 s->i_pred.count= s->p_pred.count= 1.0;
1923 s->qsum=100; 2003 s->qsum=100;
1924 s->qcount=100; 2004 s->qcount=100;
1925 2005
1926 s->short_term_qsum=0.001; 2006 s->short_term_qsum=0.001;
1927 s->short_term_qcount=0.001; 2007 s->short_term_qcount=0.001;
1928 #else
1929 s->wanted_bits = 0;
1930
1931 if (s->intra_only) {
1932 s->I_frame_bits = ((INT64)s->bit_rate * FRAME_RATE_BASE) / s->frame_rate;
1933 s->P_frame_bits = s->I_frame_bits;
1934 } else {
1935 s->P_frame_bits = (int) ((float)(s->gop_size * s->bit_rate) /
1936 (float)((float)s->frame_rate / FRAME_RATE_BASE * (I_FRAME_SIZE_RATIO + s->gop_size - 1)));
1937 s->I_frame_bits = (int)(s->P_frame_bits * I_FRAME_SIZE_RATIO);
1938 }
1939
1940 #if defined(DEBUG)
1941 printf("I_frame_size=%d P_frame_size=%d\n",
1942 s->I_frame_bits, s->P_frame_bits);
1943 #endif
1944 #endif
1945 } 2008 }
1946 2009
1947 static double predict(Predictor *p, double q, double var) 2010 static double predict(Predictor *p, double q, double var)
1948 { 2011 {
1949 return p->coeff*var / (q*p->count); 2012 return p->coeff*var / (q*p->count);
1970 p->coeff+= new_coeff; 2033 p->coeff+= new_coeff;
1971 } 2034 }
1972 2035
1973 static int rate_estimate_qscale(MpegEncContext *s) 2036 static int rate_estimate_qscale(MpegEncContext *s)
1974 { 2037 {
1975 #if 1
1976 int qmin= s->qmin; 2038 int qmin= s->qmin;
1977 int qmax= s->qmax; 2039 int qmax= s->qmax;
1978 int rate_q=5; 2040 int rate_q=5;
1979 float q; 2041 float q;
1980 int qscale; 2042 int qscale;
2047 s->last_pict_type= s->pict_type; 2109 s->last_pict_type= s->pict_type;
2048 //printf("q:%d diff:%d comp:%f rate_q:%d st_q:%f fvar:%d last_size:%d\n", qscale, (int)diff, br_compensation, 2110 //printf("q:%d diff:%d comp:%f rate_q:%d st_q:%f fvar:%d last_size:%d\n", qscale, (int)diff, br_compensation,
2049 // rate_q, short_term_q, s->mc_mb_var, s->frame_bits); 2111 // rate_q, short_term_q, s->mc_mb_var, s->frame_bits);
2050 //printf("%d %d\n", s->bit_rate, (int)fps); 2112 //printf("%d %d\n", s->bit_rate, (int)fps);
2051 return qscale; 2113 return qscale;
2052 #else
2053 INT64 diff, total_bits = s->total_bits;
2054 float q;
2055 int qscale;
2056 if (s->pict_type == I_TYPE) {
2057 s->wanted_bits += s->I_frame_bits;
2058 } else {
2059 s->wanted_bits += s->P_frame_bits;
2060 }
2061 diff = s->wanted_bits - total_bits;
2062 q = 31.0 - (float)diff / (QSCALE_K * s->mb_height * s->mb_width);
2063 /* adjust for I frame */
2064 if (s->pict_type == I_TYPE && !s->intra_only) {
2065 q /= I_FRAME_SIZE_RATIO;
2066 }
2067
2068 /* using a too small Q scale leeds to problems in mpeg1 and h263
2069 because AC coefficients are clamped to 255 or 127 */
2070 qmin = 3;
2071 if (q < qmin)
2072 q = qmin;
2073 else if (q > 31)
2074 q = 31;
2075 qscale = (int)(q + 0.5);
2076 #if defined(DEBUG)
2077 printf("\n%d: total=%0.0f wanted=%0.0f br=%0.1f diff=%d qest=%2.1f\n",
2078 s->picture_number,
2079 (double)total_bits,
2080 (double)s->wanted_bits,
2081 (float)s->frame_rate / FRAME_RATE_BASE *
2082 total_bits / s->picture_number,
2083 (int)diff, q);
2084 #endif
2085 return qscale;
2086 #endif
2087 } 2114 }
2088 2115
2089 AVCodec mpeg1video_encoder = { 2116 AVCodec mpeg1video_encoder = {
2090 "mpeg1video", 2117 "mpeg1video",
2091 CODEC_TYPE_VIDEO, 2118 CODEC_TYPE_VIDEO,