comparison mpegvideo.c @ 2967:ef2149182f1c libavcodec

COSMETICS: Remove all trailing whitespace.
author diego
date Sat, 17 Dec 2005 18:14:38 +0000
parents fde28cb7e3d5
children 4b4ee324b4c4
comparison
equal deleted inserted replaced
2966:564788471dd4 2967:ef2149182f1c
17 * License along with this library; if not, write to the Free Software 17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 * 19 *
20 * 4MV & hq & b-frame encoding stuff by Michael Niedermayer <michaelni@gmx.at> 20 * 4MV & hq & b-frame encoding stuff by Michael Niedermayer <michaelni@gmx.at>
21 */ 21 */
22 22
23 /** 23 /**
24 * @file mpegvideo.c 24 * @file mpegvideo.c
25 * The simplest mpeg encoder (well, it was the simplest!). 25 * The simplest mpeg encoder (well, it was the simplest!).
26 */ 26 */
27 27
28 #include "avcodec.h" 28 #include "avcodec.h"
29 #include "dsputil.h" 29 #include "dsputil.h"
30 #include "mpegvideo.h" 30 #include "mpegvideo.h"
31 #include "faandct.h" 31 #include "faandct.h"
32 #include <limits.h> 32 #include <limits.h>
39 //#include <assert.h> 39 //#include <assert.h>
40 40
41 #ifdef CONFIG_ENCODERS 41 #ifdef CONFIG_ENCODERS
42 static void encode_picture(MpegEncContext *s, int picture_number); 42 static void encode_picture(MpegEncContext *s, int picture_number);
43 #endif //CONFIG_ENCODERS 43 #endif //CONFIG_ENCODERS
44 static void dct_unquantize_mpeg1_intra_c(MpegEncContext *s, 44 static void dct_unquantize_mpeg1_intra_c(MpegEncContext *s,
45 DCTELEM *block, int n, int qscale); 45 DCTELEM *block, int n, int qscale);
46 static void dct_unquantize_mpeg1_inter_c(MpegEncContext *s, 46 static void dct_unquantize_mpeg1_inter_c(MpegEncContext *s,
47 DCTELEM *block, int n, int qscale); 47 DCTELEM *block, int n, int qscale);
48 static void dct_unquantize_mpeg2_intra_c(MpegEncContext *s, 48 static void dct_unquantize_mpeg2_intra_c(MpegEncContext *s,
49 DCTELEM *block, int n, int qscale); 49 DCTELEM *block, int n, int qscale);
50 static void dct_unquantize_mpeg2_inter_c(MpegEncContext *s, 50 static void dct_unquantize_mpeg2_inter_c(MpegEncContext *s,
51 DCTELEM *block, int n, int qscale); 51 DCTELEM *block, int n, int qscale);
52 static void dct_unquantize_h263_intra_c(MpegEncContext *s, 52 static void dct_unquantize_h263_intra_c(MpegEncContext *s,
53 DCTELEM *block, int n, int qscale); 53 DCTELEM *block, int n, int qscale);
54 static void dct_unquantize_h263_inter_c(MpegEncContext *s, 54 static void dct_unquantize_h263_inter_c(MpegEncContext *s,
55 DCTELEM *block, int n, int qscale); 55 DCTELEM *block, int n, int qscale);
56 static void draw_edges_c(uint8_t *buf, int wrap, int width, int height, int w); 56 static void draw_edges_c(uint8_t *buf, int wrap, int width, int height, int w);
57 #ifdef CONFIG_ENCODERS 57 #ifdef CONFIG_ENCODERS
58 static int dct_quantize_c(MpegEncContext *s, DCTELEM *block, int n, int qscale, int *overflow); 58 static int dct_quantize_c(MpegEncContext *s, DCTELEM *block, int n, int qscale, int *overflow);
59 static int dct_quantize_trellis_c(MpegEncContext *s, DCTELEM *block, int n, int qscale, int *overflow); 59 static int dct_quantize_trellis_c(MpegEncContext *s, DCTELEM *block, int n, int qscale, int *overflow);
114 int qscale; 114 int qscale;
115 int shift=0; 115 int shift=0;
116 116
117 for(qscale=qmin; qscale<=qmax; qscale++){ 117 for(qscale=qmin; qscale<=qmax; qscale++){
118 int i; 118 int i;
119 if (dsp->fdct == ff_jpeg_fdct_islow 119 if (dsp->fdct == ff_jpeg_fdct_islow
120 #ifdef FAAN_POSTSCALE 120 #ifdef FAAN_POSTSCALE
121 || dsp->fdct == ff_faandct 121 || dsp->fdct == ff_faandct
122 #endif 122 #endif
123 ) { 123 ) {
124 for(i=0;i<64;i++) { 124 for(i=0;i<64;i++) {
125 const int j= dsp->idct_permutation[i]; 125 const int j= dsp->idct_permutation[i];
126 /* 16 <= qscale * quant_matrix[i] <= 7905 */ 126 /* 16 <= qscale * quant_matrix[i] <= 7905 */
127 /* 19952 <= aanscales[i] * qscale * quant_matrix[i] <= 249205026 */ 127 /* 19952 <= aanscales[i] * qscale * quant_matrix[i] <= 249205026 */
128 /* (1<<36)/19952 >= (1<<36)/(aanscales[i] * qscale * quant_matrix[i]) >= (1<<36)/249205026 */ 128 /* (1<<36)/19952 >= (1<<36)/(aanscales[i] * qscale * quant_matrix[i]) >= (1<<36)/249205026 */
129 /* 3444240 >= (1<<36)/(aanscales[i] * qscale * quant_matrix[i]) >= 275 */ 129 /* 3444240 >= (1<<36)/(aanscales[i] * qscale * quant_matrix[i]) >= 275 */
130 130
131 qmat[qscale][i] = (int)((uint64_t_C(1) << QMAT_SHIFT) / 131 qmat[qscale][i] = (int)((uint64_t_C(1) << QMAT_SHIFT) /
132 (qscale * quant_matrix[j])); 132 (qscale * quant_matrix[j]));
133 } 133 }
134 } else if (dsp->fdct == fdct_ifast 134 } else if (dsp->fdct == fdct_ifast
135 #ifndef FAAN_POSTSCALE 135 #ifndef FAAN_POSTSCALE
136 || dsp->fdct == ff_faandct 136 || dsp->fdct == ff_faandct
140 const int j= dsp->idct_permutation[i]; 140 const int j= dsp->idct_permutation[i];
141 /* 16 <= qscale * quant_matrix[i] <= 7905 */ 141 /* 16 <= qscale * quant_matrix[i] <= 7905 */
142 /* 19952 <= aanscales[i] * qscale * quant_matrix[i] <= 249205026 */ 142 /* 19952 <= aanscales[i] * qscale * quant_matrix[i] <= 249205026 */
143 /* (1<<36)/19952 >= (1<<36)/(aanscales[i] * qscale * quant_matrix[i]) >= (1<<36)/249205026 */ 143 /* (1<<36)/19952 >= (1<<36)/(aanscales[i] * qscale * quant_matrix[i]) >= (1<<36)/249205026 */
144 /* 3444240 >= (1<<36)/(aanscales[i] * qscale * quant_matrix[i]) >= 275 */ 144 /* 3444240 >= (1<<36)/(aanscales[i] * qscale * quant_matrix[i]) >= 275 */
145 145
146 qmat[qscale][i] = (int)((uint64_t_C(1) << (QMAT_SHIFT + 14)) / 146 qmat[qscale][i] = (int)((uint64_t_C(1) << (QMAT_SHIFT + 14)) /
147 (aanscales[i] * qscale * quant_matrix[j])); 147 (aanscales[i] * qscale * quant_matrix[j]));
148 } 148 }
149 } else { 149 } else {
150 for(i=0;i<64;i++) { 150 for(i=0;i<64;i++) {
151 const int j= dsp->idct_permutation[i]; 151 const int j= dsp->idct_permutation[i];
160 160
161 if(qmat16[qscale][0][i]==0 || qmat16[qscale][0][i]==128*256) qmat16[qscale][0][i]=128*256-1; 161 if(qmat16[qscale][0][i]==0 || qmat16[qscale][0][i]==128*256) qmat16[qscale][0][i]=128*256-1;
162 qmat16[qscale][1][i]= ROUNDED_DIV(bias<<(16-QUANT_BIAS_SHIFT), qmat16[qscale][0][i]); 162 qmat16[qscale][1][i]= ROUNDED_DIV(bias<<(16-QUANT_BIAS_SHIFT), qmat16[qscale][0][i]);
163 } 163 }
164 } 164 }
165 165
166 for(i=intra; i<64; i++){ 166 for(i=intra; i<64; i++){
167 int64_t max= 8191; 167 int64_t max= 8191;
168 if (dsp->fdct == fdct_ifast 168 if (dsp->fdct == fdct_ifast
169 #ifndef FAAN_POSTSCALE 169 #ifndef FAAN_POSTSCALE
170 || dsp->fdct == ff_faandct 170 || dsp->fdct == ff_faandct
171 #endif 171 #endif
172 ) { 172 ) {
173 max= (8191LL*aanscales[i]) >> 14; 173 max= (8191LL*aanscales[i]) >> 14;
174 } 174 }
175 while(((max * qmat[qscale][i]) >> shift) > INT_MAX){ 175 while(((max * qmat[qscale][i]) >> shift) > INT_MAX){
176 shift++; 176 shift++;
177 } 177 }
178 } 178 }
179 } 179 }
180 if(shift){ 180 if(shift){
183 } 183 }
184 184
185 static inline void update_qscale(MpegEncContext *s){ 185 static inline void update_qscale(MpegEncContext *s){
186 s->qscale= (s->lambda*139 + FF_LAMBDA_SCALE*64) >> (FF_LAMBDA_SHIFT + 7); 186 s->qscale= (s->lambda*139 + FF_LAMBDA_SCALE*64) >> (FF_LAMBDA_SHIFT + 7);
187 s->qscale= clip(s->qscale, s->avctx->qmin, s->avctx->qmax); 187 s->qscale= clip(s->qscale, s->avctx->qmin, s->avctx->qmax);
188 188
189 s->lambda2= (s->lambda*s->lambda + FF_LAMBDA_SCALE/2) >> FF_LAMBDA_SHIFT; 189 s->lambda2= (s->lambda*s->lambda + FF_LAMBDA_SCALE/2) >> FF_LAMBDA_SHIFT;
190 } 190 }
191 #endif //CONFIG_ENCODERS 191 #endif //CONFIG_ENCODERS
192 192
193 void ff_init_scantable(uint8_t *permutation, ScanTable *st, const uint8_t *src_scantable){ 193 void ff_init_scantable(uint8_t *permutation, ScanTable *st, const uint8_t *src_scantable){
194 int i; 194 int i;
195 int end; 195 int end;
196 196
197 st->scantable= src_scantable; 197 st->scantable= src_scantable;
198 198
199 for(i=0; i<64; i++){ 199 for(i=0; i<64; i++){
200 int j; 200 int j;
201 j = src_scantable[i]; 201 j = src_scantable[i];
202 st->permutated[i] = permutation[j]; 202 st->permutated[i] = permutation[j];
203 #ifdef ARCH_POWERPC 203 #ifdef ARCH_POWERPC
204 st->inverse[j] = i; 204 st->inverse[j] = i;
205 #endif 205 #endif
206 } 206 }
207 207
208 end=-1; 208 end=-1;
209 for(i=0; i<64; i++){ 209 for(i=0; i<64; i++){
210 int j; 210 int j;
211 j = st->permutated[i]; 211 j = st->permutated[i];
212 if(j>end) end=j; 212 if(j>end) end=j;
240 240
241 #ifdef CONFIG_ENCODERS 241 #ifdef CONFIG_ENCODERS
242 s->dct_quantize= dct_quantize_c; 242 s->dct_quantize= dct_quantize_c;
243 s->denoise_dct= denoise_dct_c; 243 s->denoise_dct= denoise_dct_c;
244 #endif //CONFIG_ENCODERS 244 #endif //CONFIG_ENCODERS
245 245
246 #ifdef HAVE_MMX 246 #ifdef HAVE_MMX
247 MPV_common_init_mmx(s); 247 MPV_common_init_mmx(s);
248 #endif 248 #endif
249 #ifdef ARCH_ALPHA 249 #ifdef ARCH_ALPHA
250 MPV_common_init_axp(s); 250 MPV_common_init_axp(s);
270 } 270 }
271 271
272 #endif //CONFIG_ENCODERS 272 #endif //CONFIG_ENCODERS
273 273
274 /* load & permutate scantables 274 /* load & permutate scantables
275 note: only wmv uses different ones 275 note: only wmv uses different ones
276 */ 276 */
277 if(s->alternate_scan){ 277 if(s->alternate_scan){
278 ff_init_scantable(s->dsp.idct_permutation, &s->inter_scantable , ff_alternate_vertical_scan); 278 ff_init_scantable(s->dsp.idct_permutation, &s->inter_scantable , ff_alternate_vertical_scan);
279 ff_init_scantable(s->dsp.idct_permutation, &s->intra_scantable , ff_alternate_vertical_scan); 279 ff_init_scantable(s->dsp.idct_permutation, &s->intra_scantable , ff_alternate_vertical_scan);
280 }else{ 280 }else{
314 if(src->motion_subsample_log2 != dst->motion_subsample_log2) 314 if(src->motion_subsample_log2 != dst->motion_subsample_log2)
315 av_log(s->avctx, AV_LOG_ERROR, "AVFrame.motion_subsample_log2 doesn't match! (%d!=%d)\n", 315 av_log(s->avctx, AV_LOG_ERROR, "AVFrame.motion_subsample_log2 doesn't match! (%d!=%d)\n",
316 src->motion_subsample_log2, dst->motion_subsample_log2); 316 src->motion_subsample_log2, dst->motion_subsample_log2);
317 317
318 memcpy(dst->mb_type, src->mb_type, s->mb_stride * s->mb_height * sizeof(dst->mb_type[0])); 318 memcpy(dst->mb_type, src->mb_type, s->mb_stride * s->mb_height * sizeof(dst->mb_type[0]));
319 319
320 for(i=0; i<2; i++){ 320 for(i=0; i<2; i++){
321 int stride= ((16*s->mb_width )>>src->motion_subsample_log2) + 1; 321 int stride= ((16*s->mb_width )>>src->motion_subsample_log2) + 1;
322 int height= ((16*s->mb_height)>>src->motion_subsample_log2); 322 int height= ((16*s->mb_height)>>src->motion_subsample_log2);
323 323
324 if(src->motion_val[i] && src->motion_val[i] != dst->motion_val[i]){ 324 if(src->motion_val[i] && src->motion_val[i] != dst->motion_val[i]){
339 const int big_mb_num= s->mb_stride*(s->mb_height+1) + 1; //the +1 is needed so memset(,,stride*height) doesnt sig11 339 const int big_mb_num= s->mb_stride*(s->mb_height+1) + 1; //the +1 is needed so memset(,,stride*height) doesnt sig11
340 const int mb_array_size= s->mb_stride*s->mb_height; 340 const int mb_array_size= s->mb_stride*s->mb_height;
341 const int b8_array_size= s->b8_stride*s->mb_height*2; 341 const int b8_array_size= s->b8_stride*s->mb_height*2;
342 const int b4_array_size= s->b4_stride*s->mb_height*4; 342 const int b4_array_size= s->b4_stride*s->mb_height*4;
343 int i; 343 int i;
344 344
345 if(shared){ 345 if(shared){
346 assert(pic->data[0]); 346 assert(pic->data[0]);
347 assert(pic->type == 0 || pic->type == FF_BUFFER_TYPE_SHARED); 347 assert(pic->type == 0 || pic->type == FF_BUFFER_TYPE_SHARED);
348 pic->type= FF_BUFFER_TYPE_SHARED; 348 pic->type= FF_BUFFER_TYPE_SHARED;
349 }else{ 349 }else{
350 int r; 350 int r;
351 351
352 assert(!pic->data[0]); 352 assert(!pic->data[0]);
353 353
354 r= s->avctx->get_buffer(s->avctx, (AVFrame*)pic); 354 r= s->avctx->get_buffer(s->avctx, (AVFrame*)pic);
355 355
356 if(r<0 || !pic->age || !pic->type || !pic->data[0]){ 356 if(r<0 || !pic->age || !pic->type || !pic->data[0]){
357 av_log(s->avctx, AV_LOG_ERROR, "get_buffer() failed (%d %d %d %p)\n", r, pic->age, pic->type, pic->data[0]); 357 av_log(s->avctx, AV_LOG_ERROR, "get_buffer() failed (%d %d %d %p)\n", r, pic->age, pic->type, pic->data[0]);
358 return -1; 358 return -1;
359 } 359 }
360 360
369 } 369 }
370 370
371 s->linesize = pic->linesize[0]; 371 s->linesize = pic->linesize[0];
372 s->uvlinesize= pic->linesize[1]; 372 s->uvlinesize= pic->linesize[1];
373 } 373 }
374 374
375 if(pic->qscale_table==NULL){ 375 if(pic->qscale_table==NULL){
376 if (s->encoding) { 376 if (s->encoding) {
377 CHECKED_ALLOCZ(pic->mb_var , mb_array_size * sizeof(int16_t)) 377 CHECKED_ALLOCZ(pic->mb_var , mb_array_size * sizeof(int16_t))
378 CHECKED_ALLOCZ(pic->mc_mb_var, mb_array_size * sizeof(int16_t)) 378 CHECKED_ALLOCZ(pic->mc_mb_var, mb_array_size * sizeof(int16_t))
379 CHECKED_ALLOCZ(pic->mb_mean , mb_array_size * sizeof(int8_t)) 379 CHECKED_ALLOCZ(pic->mb_mean , mb_array_size * sizeof(int8_t))
380 } 380 }
381 381
408 //it might be nicer if the application would keep track of these but it would require a API change 408 //it might be nicer if the application would keep track of these but it would require a API change
409 memmove(s->prev_pict_types+1, s->prev_pict_types, PREV_PICT_TYPES_BUFFER_SIZE-1); 409 memmove(s->prev_pict_types+1, s->prev_pict_types, PREV_PICT_TYPES_BUFFER_SIZE-1);
410 s->prev_pict_types[0]= s->pict_type; 410 s->prev_pict_types[0]= s->pict_type;
411 if(pic->age < PREV_PICT_TYPES_BUFFER_SIZE && s->prev_pict_types[pic->age] == B_TYPE) 411 if(pic->age < PREV_PICT_TYPES_BUFFER_SIZE && s->prev_pict_types[pic->age] == B_TYPE)
412 pic->age= INT_MAX; // skipped MBs in b frames are quite rare in mpeg1/2 and its a bit tricky to skip them anyway 412 pic->age= INT_MAX; // skipped MBs in b frames are quite rare in mpeg1/2 and its a bit tricky to skip them anyway
413 413
414 return 0; 414 return 0;
415 fail: //for the CHECKED_ALLOCZ macro 415 fail: //for the CHECKED_ALLOCZ macro
416 return -1; 416 return -1;
417 } 417 }
418 418
437 pic->mb_type= NULL; 437 pic->mb_type= NULL;
438 for(i=0; i<2; i++){ 438 for(i=0; i<2; i++){
439 av_freep(&pic->motion_val_base[i]); 439 av_freep(&pic->motion_val_base[i]);
440 av_freep(&pic->ref_index[i]); 440 av_freep(&pic->ref_index[i]);
441 } 441 }
442 442
443 if(pic->type == FF_BUFFER_TYPE_SHARED){ 443 if(pic->type == FF_BUFFER_TYPE_SHARED){
444 for(i=0; i<4; i++){ 444 for(i=0; i<4; i++){
445 pic->base[i]= 445 pic->base[i]=
446 pic->data[i]= NULL; 446 pic->data[i]= NULL;
447 } 447 }
448 pic->type= 0; 448 pic->type= 0;
449 } 449 }
450 } 450 }
451 451
452 static int init_duplicate_context(MpegEncContext *s, MpegEncContext *base){ 452 static int init_duplicate_context(MpegEncContext *s, MpegEncContext *base){
453 int i; 453 int i;
454 454
455 // edge emu needs blocksize + filter length - 1 (=17x17 for halfpel / 21x21 for h264) 455 // edge emu needs blocksize + filter length - 1 (=17x17 for halfpel / 21x21 for h264)
456 CHECKED_ALLOCZ(s->allocated_edge_emu_buffer, (s->width+64)*2*17*2); //(width + edge + align)*interlaced*MBsize*tolerance 456 CHECKED_ALLOCZ(s->allocated_edge_emu_buffer, (s->width+64)*2*17*2); //(width + edge + align)*interlaced*MBsize*tolerance
457 s->edge_emu_buffer= s->allocated_edge_emu_buffer + (s->width+64)*2*17; 457 s->edge_emu_buffer= s->allocated_edge_emu_buffer + (s->width+64)*2*17;
458 458
459 //FIXME should be linesize instead of s->width*2 but that isnt known before get_buffer() 459 //FIXME should be linesize instead of s->width*2 but that isnt known before get_buffer()
460 CHECKED_ALLOCZ(s->me.scratchpad, (s->width+64)*4*16*2*sizeof(uint8_t)) 460 CHECKED_ALLOCZ(s->me.scratchpad, (s->width+64)*4*16*2*sizeof(uint8_t))
461 s->rd_scratchpad= s->me.scratchpad; 461 s->rd_scratchpad= s->me.scratchpad;
462 s->b_scratchpad= s->me.scratchpad; 462 s->b_scratchpad= s->me.scratchpad;
463 s->obmc_scratchpad= s->me.scratchpad + 16; 463 s->obmc_scratchpad= s->me.scratchpad + 16;
464 if (s->encoding) { 464 if (s->encoding) {
465 CHECKED_ALLOCZ(s->me.map , ME_MAP_SIZE*sizeof(uint32_t)) 465 CHECKED_ALLOCZ(s->me.map , ME_MAP_SIZE*sizeof(uint32_t))
466 CHECKED_ALLOCZ(s->me.score_map, ME_MAP_SIZE*sizeof(uint32_t)) 466 CHECKED_ALLOCZ(s->me.score_map, ME_MAP_SIZE*sizeof(uint32_t))
467 if(s->avctx->noise_reduction){ 467 if(s->avctx->noise_reduction){
468 CHECKED_ALLOCZ(s->dct_error_sum, 2 * 64 * sizeof(int)) 468 CHECKED_ALLOCZ(s->dct_error_sum, 2 * 64 * sizeof(int))
469 } 469 }
470 } 470 }
471 CHECKED_ALLOCZ(s->blocks, 64*12*2 * sizeof(DCTELEM)) 471 CHECKED_ALLOCZ(s->blocks, 64*12*2 * sizeof(DCTELEM))
472 s->block= s->blocks[0]; 472 s->block= s->blocks[0];
473 473
474 for(i=0;i<12;i++){ 474 for(i=0;i<12;i++){
475 s->pblocks[i] = (short *)(&s->block[i]); 475 s->pblocks[i] = (short *)(&s->block[i]);
482 static void free_duplicate_context(MpegEncContext *s){ 482 static void free_duplicate_context(MpegEncContext *s){
483 if(s==NULL) return; 483 if(s==NULL) return;
484 484
485 av_freep(&s->allocated_edge_emu_buffer); s->edge_emu_buffer= NULL; 485 av_freep(&s->allocated_edge_emu_buffer); s->edge_emu_buffer= NULL;
486 av_freep(&s->me.scratchpad); 486 av_freep(&s->me.scratchpad);
487 s->rd_scratchpad= 487 s->rd_scratchpad=
488 s->b_scratchpad= 488 s->b_scratchpad=
489 s->obmc_scratchpad= NULL; 489 s->obmc_scratchpad= NULL;
490 490
491 av_freep(&s->dct_error_sum); 491 av_freep(&s->dct_error_sum);
492 av_freep(&s->me.map); 492 av_freep(&s->me.map);
493 av_freep(&s->me.score_map); 493 av_freep(&s->me.score_map);
494 av_freep(&s->blocks); 494 av_freep(&s->blocks);
495 s->block= NULL; 495 s->block= NULL;
584 */ 584 */
585 585
586 #ifdef CONFIG_ENCODERS 586 #ifdef CONFIG_ENCODERS
587 static void MPV_encode_defaults(MpegEncContext *s){ 587 static void MPV_encode_defaults(MpegEncContext *s){
588 static int done=0; 588 static int done=0;
589 589
590 MPV_common_defaults(s); 590 MPV_common_defaults(s);
591 591
592 if(!done){ 592 if(!done){
593 int i; 593 int i;
594 done=1; 594 done=1;
595 595
596 default_mv_penalty= av_mallocz( sizeof(uint8_t)*(MAX_FCODE+1)*(2*MAX_MV+1) ); 596 default_mv_penalty= av_mallocz( sizeof(uint8_t)*(MAX_FCODE+1)*(2*MAX_MV+1) );
603 s->me.mv_penalty= default_mv_penalty; 603 s->me.mv_penalty= default_mv_penalty;
604 s->fcode_tab= default_fcode_tab; 604 s->fcode_tab= default_fcode_tab;
605 } 605 }
606 #endif //CONFIG_ENCODERS 606 #endif //CONFIG_ENCODERS
607 607
608 /** 608 /**
609 * init common structure for both encoder and decoder. 609 * init common structure for both encoder and decoder.
610 * this assumes that some variables like width/height are already set 610 * this assumes that some variables like width/height are already set
611 */ 611 */
612 int MPV_common_init(MpegEncContext *s) 612 int MPV_common_init(MpegEncContext *s)
613 { 613 {
642 /* set default edge pos, will be overriden in decode_header if needed */ 642 /* set default edge pos, will be overriden in decode_header if needed */
643 s->h_edge_pos= s->mb_width*16; 643 s->h_edge_pos= s->mb_width*16;
644 s->v_edge_pos= s->mb_height*16; 644 s->v_edge_pos= s->mb_height*16;
645 645
646 s->mb_num = s->mb_width * s->mb_height; 646 s->mb_num = s->mb_width * s->mb_height;
647 647
648 s->block_wrap[0]= 648 s->block_wrap[0]=
649 s->block_wrap[1]= 649 s->block_wrap[1]=
650 s->block_wrap[2]= 650 s->block_wrap[2]=
651 s->block_wrap[3]= s->b8_stride; 651 s->block_wrap[3]= s->b8_stride;
652 s->block_wrap[4]= 652 s->block_wrap[4]=
653 s->block_wrap[5]= s->mb_stride; 653 s->block_wrap[5]= s->mb_stride;
654 654
655 y_size = s->b8_stride * (2 * s->mb_height + 1); 655 y_size = s->b8_stride * (2 * s->mb_height + 1);
656 c_size = s->mb_stride * (s->mb_height + 1); 656 c_size = s->mb_stride * (s->mb_height + 1);
657 yc_size = y_size + 2 * c_size; 657 yc_size = y_size + 2 * c_size;
658 658
659 /* convert fourcc to upper case */ 659 /* convert fourcc to upper case */
660 s->avctx->codec_tag= toupper( s->avctx->codec_tag &0xFF) 660 s->avctx->codec_tag= toupper( s->avctx->codec_tag &0xFF)
661 + (toupper((s->avctx->codec_tag>>8 )&0xFF)<<8 ) 661 + (toupper((s->avctx->codec_tag>>8 )&0xFF)<<8 )
662 + (toupper((s->avctx->codec_tag>>16)&0xFF)<<16) 662 + (toupper((s->avctx->codec_tag>>16)&0xFF)<<16)
663 + (toupper((s->avctx->codec_tag>>24)&0xFF)<<24); 663 + (toupper((s->avctx->codec_tag>>24)&0xFF)<<24);
664 664
665 s->avctx->stream_codec_tag= toupper( s->avctx->stream_codec_tag &0xFF) 665 s->avctx->stream_codec_tag= toupper( s->avctx->stream_codec_tag &0xFF)
666 + (toupper((s->avctx->stream_codec_tag>>8 )&0xFF)<<8 ) 666 + (toupper((s->avctx->stream_codec_tag>>8 )&0xFF)<<8 )
667 + (toupper((s->avctx->stream_codec_tag>>16)&0xFF)<<16) 667 + (toupper((s->avctx->stream_codec_tag>>16)&0xFF)<<16)
668 + (toupper((s->avctx->stream_codec_tag>>24)&0xFF)<<24); 668 + (toupper((s->avctx->stream_codec_tag>>24)&0xFF)<<24);
669 669
670 s->avctx->coded_frame= (AVFrame*)&s->current_picture; 670 s->avctx->coded_frame= (AVFrame*)&s->current_picture;
671 671
672 CHECKED_ALLOCZ(s->mb_index2xy, (s->mb_num+1)*sizeof(int)) //error ressilience code looks cleaner with this 672 CHECKED_ALLOCZ(s->mb_index2xy, (s->mb_num+1)*sizeof(int)) //error ressilience code looks cleaner with this
674 for(x=0; x<s->mb_width; x++){ 674 for(x=0; x<s->mb_width; x++){
675 s->mb_index2xy[ x + y*s->mb_width ] = x + y*s->mb_stride; 675 s->mb_index2xy[ x + y*s->mb_width ] = x + y*s->mb_stride;
676 } 676 }
677 } 677 }
678 s->mb_index2xy[ s->mb_height*s->mb_width ] = (s->mb_height-1)*s->mb_stride + s->mb_width; //FIXME really needed? 678 s->mb_index2xy[ s->mb_height*s->mb_width ] = (s->mb_height-1)*s->mb_stride + s->mb_width; //FIXME really needed?
679 679
680 if (s->encoding) { 680 if (s->encoding) {
681 /* Allocate MV tables */ 681 /* Allocate MV tables */
682 CHECKED_ALLOCZ(s->p_mv_table_base , mv_table_size * 2 * sizeof(int16_t)) 682 CHECKED_ALLOCZ(s->p_mv_table_base , mv_table_size * 2 * sizeof(int16_t))
683 CHECKED_ALLOCZ(s->b_forw_mv_table_base , mv_table_size * 2 * sizeof(int16_t)) 683 CHECKED_ALLOCZ(s->b_forw_mv_table_base , mv_table_size * 2 * sizeof(int16_t))
684 CHECKED_ALLOCZ(s->b_back_mv_table_base , mv_table_size * 2 * sizeof(int16_t)) 684 CHECKED_ALLOCZ(s->b_back_mv_table_base , mv_table_size * 2 * sizeof(int16_t))
697 } 697 }
698 CHECKED_ALLOCZ(s->avctx->stats_out, 256); 698 CHECKED_ALLOCZ(s->avctx->stats_out, 256);
699 699
700 /* Allocate MB type table */ 700 /* Allocate MB type table */
701 CHECKED_ALLOCZ(s->mb_type , mb_array_size * sizeof(uint16_t)) //needed for encoding 701 CHECKED_ALLOCZ(s->mb_type , mb_array_size * sizeof(uint16_t)) //needed for encoding
702 702
703 CHECKED_ALLOCZ(s->lambda_table, mb_array_size * sizeof(int)) 703 CHECKED_ALLOCZ(s->lambda_table, mb_array_size * sizeof(int))
704 704
705 CHECKED_ALLOCZ(s->q_intra_matrix, 64*32 * sizeof(int)) 705 CHECKED_ALLOCZ(s->q_intra_matrix, 64*32 * sizeof(int))
706 CHECKED_ALLOCZ(s->q_inter_matrix, 64*32 * sizeof(int)) 706 CHECKED_ALLOCZ(s->q_inter_matrix, 64*32 * sizeof(int))
707 CHECKED_ALLOCZ(s->q_intra_matrix16, 64*32*2 * sizeof(uint16_t)) 707 CHECKED_ALLOCZ(s->q_intra_matrix16, 64*32*2 * sizeof(uint16_t))
708 CHECKED_ALLOCZ(s->q_inter_matrix16, 64*32*2 * sizeof(uint16_t)) 708 CHECKED_ALLOCZ(s->q_inter_matrix16, 64*32*2 * sizeof(uint16_t))
709 CHECKED_ALLOCZ(s->input_picture, MAX_PICTURE_COUNT * sizeof(Picture*)) 709 CHECKED_ALLOCZ(s->input_picture, MAX_PICTURE_COUNT * sizeof(Picture*))
710 CHECKED_ALLOCZ(s->reordered_input_picture, MAX_PICTURE_COUNT * sizeof(Picture*)) 710 CHECKED_ALLOCZ(s->reordered_input_picture, MAX_PICTURE_COUNT * sizeof(Picture*))
711 711
712 if(s->avctx->noise_reduction){ 712 if(s->avctx->noise_reduction){
713 CHECKED_ALLOCZ(s->dct_offset, 2 * 64 * sizeof(uint16_t)) 713 CHECKED_ALLOCZ(s->dct_offset, 2 * 64 * sizeof(uint16_t))
714 } 714 }
715 } 715 }
716 CHECKED_ALLOCZ(s->picture, MAX_PICTURE_COUNT * sizeof(Picture)) 716 CHECKED_ALLOCZ(s->picture, MAX_PICTURE_COUNT * sizeof(Picture))
717 717
718 CHECKED_ALLOCZ(s->error_status_table, mb_array_size*sizeof(uint8_t)) 718 CHECKED_ALLOCZ(s->error_status_table, mb_array_size*sizeof(uint8_t))
719 719
720 if(s->codec_id==CODEC_ID_MPEG4 || (s->flags & CODEC_FLAG_INTERLACED_ME)){ 720 if(s->codec_id==CODEC_ID_MPEG4 || (s->flags & CODEC_FLAG_INTERLACED_ME)){
721 /* interlaced direct mode decoding tables */ 721 /* interlaced direct mode decoding tables */
722 for(i=0; i<2; i++){ 722 for(i=0; i<2; i++){
723 int j, k; 723 int j, k;
724 for(j=0; j<2; j++){ 724 for(j=0; j<2; j++){
737 /* ac values */ 737 /* ac values */
738 CHECKED_ALLOCZ(s->ac_val_base, yc_size * sizeof(int16_t) * 16); 738 CHECKED_ALLOCZ(s->ac_val_base, yc_size * sizeof(int16_t) * 16);
739 s->ac_val[0] = s->ac_val_base + s->b8_stride + 1; 739 s->ac_val[0] = s->ac_val_base + s->b8_stride + 1;
740 s->ac_val[1] = s->ac_val_base + y_size + s->mb_stride + 1; 740 s->ac_val[1] = s->ac_val_base + y_size + s->mb_stride + 1;
741 s->ac_val[2] = s->ac_val[1] + c_size; 741 s->ac_val[2] = s->ac_val[1] + c_size;
742 742
743 /* cbp values */ 743 /* cbp values */
744 CHECKED_ALLOCZ(s->coded_block_base, y_size); 744 CHECKED_ALLOCZ(s->coded_block_base, y_size);
745 s->coded_block= s->coded_block_base + s->b8_stride + 1; 745 s->coded_block= s->coded_block_base + s->b8_stride + 1;
746 746
747 /* cbp, ac_pred, pred_dir */ 747 /* cbp, ac_pred, pred_dir */
748 CHECKED_ALLOCZ(s->cbp_table , mb_array_size * sizeof(uint8_t)) 748 CHECKED_ALLOCZ(s->cbp_table , mb_array_size * sizeof(uint8_t))
749 CHECKED_ALLOCZ(s->pred_dir_table, mb_array_size * sizeof(uint8_t)) 749 CHECKED_ALLOCZ(s->pred_dir_table, mb_array_size * sizeof(uint8_t))
750 } 750 }
751 751
752 if (s->h263_pred || s->h263_plus || !s->encoding) { 752 if (s->h263_pred || s->h263_plus || !s->encoding) {
753 /* dc values */ 753 /* dc values */
754 //MN: we need these for error resilience of intra-frames 754 //MN: we need these for error resilience of intra-frames
755 CHECKED_ALLOCZ(s->dc_val_base, yc_size * sizeof(int16_t)); 755 CHECKED_ALLOCZ(s->dc_val_base, yc_size * sizeof(int16_t));
756 s->dc_val[0] = s->dc_val_base + s->b8_stride + 1; 756 s->dc_val[0] = s->dc_val_base + s->b8_stride + 1;
761 } 761 }
762 762
763 /* which mb is a intra block */ 763 /* which mb is a intra block */
764 CHECKED_ALLOCZ(s->mbintra_table, mb_array_size); 764 CHECKED_ALLOCZ(s->mbintra_table, mb_array_size);
765 memset(s->mbintra_table, 1, mb_array_size); 765 memset(s->mbintra_table, 1, mb_array_size);
766 766
767 /* init macroblock skip table */ 767 /* init macroblock skip table */
768 CHECKED_ALLOCZ(s->mbskip_table, mb_array_size+2); 768 CHECKED_ALLOCZ(s->mbskip_table, mb_array_size+2);
769 //Note the +1 is for a quicker mpeg4 slice_end detection 769 //Note the +1 is for a quicker mpeg4 slice_end detection
770 CHECKED_ALLOCZ(s->prev_pict_types, PREV_PICT_TYPES_BUFFER_SIZE); 770 CHECKED_ALLOCZ(s->prev_pict_types, PREV_PICT_TYPES_BUFFER_SIZE);
771 771
772 s->parse_context.state= -1; 772 s->parse_context.state= -1;
773 if((s->avctx->debug&(FF_DEBUG_VIS_QP|FF_DEBUG_VIS_MB_TYPE)) || (s->avctx->debug_mv)){ 773 if((s->avctx->debug&(FF_DEBUG_VIS_QP|FF_DEBUG_VIS_MB_TYPE)) || (s->avctx->debug_mv)){
774 s->visualization_buffer[0] = av_malloc((s->mb_width*16 + 2*EDGE_WIDTH) * s->mb_height*16 + 2*EDGE_WIDTH); 774 s->visualization_buffer[0] = av_malloc((s->mb_width*16 + 2*EDGE_WIDTH) * s->mb_height*16 + 2*EDGE_WIDTH);
775 s->visualization_buffer[1] = av_malloc((s->mb_width*8 + EDGE_WIDTH) * s->mb_height*8 + EDGE_WIDTH); 775 s->visualization_buffer[1] = av_malloc((s->mb_width*8 + EDGE_WIDTH) * s->mb_height*8 + EDGE_WIDTH);
776 s->visualization_buffer[2] = av_malloc((s->mb_width*8 + EDGE_WIDTH) * s->mb_height*8 + EDGE_WIDTH); 776 s->visualization_buffer[2] = av_malloc((s->mb_width*8 + EDGE_WIDTH) * s->mb_height*8 + EDGE_WIDTH);
835 av_freep(&s->p_field_mv_table_base[i][j]); 835 av_freep(&s->p_field_mv_table_base[i][j]);
836 s->p_field_mv_table[i][j]=NULL; 836 s->p_field_mv_table[i][j]=NULL;
837 } 837 }
838 av_freep(&s->p_field_select_table[i]); 838 av_freep(&s->p_field_select_table[i]);
839 } 839 }
840 840
841 av_freep(&s->dc_val_base); 841 av_freep(&s->dc_val_base);
842 av_freep(&s->ac_val_base); 842 av_freep(&s->ac_val_base);
843 av_freep(&s->coded_block_base); 843 av_freep(&s->coded_block_base);
844 av_freep(&s->mbintra_table); 844 av_freep(&s->mbintra_table);
845 av_freep(&s->cbp_table); 845 av_freep(&s->cbp_table);
846 av_freep(&s->pred_dir_table); 846 av_freep(&s->pred_dir_table);
847 847
848 av_freep(&s->mbskip_table); 848 av_freep(&s->mbskip_table);
849 av_freep(&s->prev_pict_types); 849 av_freep(&s->prev_pict_types);
850 av_freep(&s->bitstream_buffer); 850 av_freep(&s->bitstream_buffer);
851 s->allocated_bitstream_buffer_size=0; 851 s->allocated_bitstream_buffer_size=0;
852 852
887 int MPV_encode_init(AVCodecContext *avctx) 887 int MPV_encode_init(AVCodecContext *avctx)
888 { 888 {
889 MpegEncContext *s = avctx->priv_data; 889 MpegEncContext *s = avctx->priv_data;
890 int i; 890 int i;
891 int chroma_h_shift, chroma_v_shift; 891 int chroma_h_shift, chroma_v_shift;
892 892
893 MPV_encode_defaults(s); 893 MPV_encode_defaults(s);
894 894
895 if(avctx->pix_fmt != PIX_FMT_YUVJ420P && avctx->pix_fmt != PIX_FMT_YUV420P){ 895 if(avctx->pix_fmt != PIX_FMT_YUVJ420P && avctx->pix_fmt != PIX_FMT_YUV420P){
896 av_log(avctx, AV_LOG_ERROR, "only YUV420 is supported\n"); 896 av_log(avctx, AV_LOG_ERROR, "only YUV420 is supported\n");
897 return -1; 897 return -1;
941 941
942 s->me_method = avctx->me_method; 942 s->me_method = avctx->me_method;
943 943
944 /* Fixed QSCALE */ 944 /* Fixed QSCALE */
945 s->fixed_qscale = !!(avctx->flags & CODEC_FLAG_QSCALE); 945 s->fixed_qscale = !!(avctx->flags & CODEC_FLAG_QSCALE);
946 946
947 s->adaptive_quant= ( s->avctx->lumi_masking 947 s->adaptive_quant= ( s->avctx->lumi_masking
948 || s->avctx->dark_masking 948 || s->avctx->dark_masking
949 || s->avctx->temporal_cplx_masking 949 || s->avctx->temporal_cplx_masking
950 || s->avctx->spatial_cplx_masking 950 || s->avctx->spatial_cplx_masking
951 || s->avctx->p_masking 951 || s->avctx->p_masking
952 || s->avctx->border_masking 952 || s->avctx->border_masking
953 || (s->flags&CODEC_FLAG_QP_RD)) 953 || (s->flags&CODEC_FLAG_QP_RD))
954 && !s->fixed_qscale; 954 && !s->fixed_qscale;
955 955
956 s->obmc= !!(s->flags & CODEC_FLAG_OBMC); 956 s->obmc= !!(s->flags & CODEC_FLAG_OBMC);
957 s->loop_filter= !!(s->flags & CODEC_FLAG_LOOP_FILTER); 957 s->loop_filter= !!(s->flags & CODEC_FLAG_LOOP_FILTER);
958 s->alternate_scan= !!(s->flags & CODEC_FLAG_ALT_SCAN); 958 s->alternate_scan= !!(s->flags & CODEC_FLAG_ALT_SCAN);
959 959
960 if(avctx->rc_max_rate && !avctx->rc_buffer_size){ 960 if(avctx->rc_max_rate && !avctx->rc_buffer_size){
961 av_log(avctx, AV_LOG_ERROR, "a vbv buffer size is needed, for encoding with a maximum bitrate\n"); 961 av_log(avctx, AV_LOG_ERROR, "a vbv buffer size is needed, for encoding with a maximum bitrate\n");
962 return -1; 962 return -1;
963 } 963 }
964 964
965 if(avctx->rc_min_rate && avctx->rc_max_rate != avctx->rc_min_rate){ 965 if(avctx->rc_min_rate && avctx->rc_max_rate != avctx->rc_min_rate){
966 av_log(avctx, AV_LOG_INFO, "Warning min_rate > 0 but min_rate != max_rate isn't recommended!\n"); 966 av_log(avctx, AV_LOG_INFO, "Warning min_rate > 0 but min_rate != max_rate isn't recommended!\n");
967 } 967 }
968 968
969 if(avctx->rc_min_rate && avctx->rc_min_rate > avctx->bit_rate){ 969 if(avctx->rc_min_rate && avctx->rc_min_rate > avctx->bit_rate){
970 av_log(avctx, AV_LOG_INFO, "bitrate below min bitrate\n"); 970 av_log(avctx, AV_LOG_INFO, "bitrate below min bitrate\n");
971 return -1; 971 return -1;
972 } 972 }
973 973
974 if(avctx->rc_max_rate && avctx->rc_max_rate < avctx->bit_rate){ 974 if(avctx->rc_max_rate && avctx->rc_max_rate < avctx->bit_rate){
975 av_log(avctx, AV_LOG_INFO, "bitrate above max bitrate\n"); 975 av_log(avctx, AV_LOG_INFO, "bitrate above max bitrate\n");
976 return -1; 976 return -1;
977 } 977 }
978 978
979 if( s->avctx->rc_max_rate && s->avctx->rc_min_rate == s->avctx->rc_max_rate 979 if( s->avctx->rc_max_rate && s->avctx->rc_min_rate == s->avctx->rc_max_rate
980 && (s->codec_id == CODEC_ID_MPEG1VIDEO || s->codec_id == CODEC_ID_MPEG2VIDEO) 980 && (s->codec_id == CODEC_ID_MPEG1VIDEO || s->codec_id == CODEC_ID_MPEG2VIDEO)
981 && 90000LL * (avctx->rc_buffer_size-1) > s->avctx->rc_max_rate*0xFFFFLL){ 981 && 90000LL * (avctx->rc_buffer_size-1) > s->avctx->rc_max_rate*0xFFFFLL){
982 982
983 av_log(avctx, AV_LOG_INFO, "Warning vbv_delay will be set to 0xFFFF (=VBR) as the specified vbv buffer is too large for the given bitrate!\n"); 983 av_log(avctx, AV_LOG_INFO, "Warning vbv_delay will be set to 0xFFFF (=VBR) as the specified vbv buffer is too large for the given bitrate!\n");
984 } 984 }
985 985
986 if((s->flags & CODEC_FLAG_4MV) && s->codec_id != CODEC_ID_MPEG4 986 if((s->flags & CODEC_FLAG_4MV) && s->codec_id != CODEC_ID_MPEG4
987 && s->codec_id != CODEC_ID_H263 && s->codec_id != CODEC_ID_H263P && s->codec_id != CODEC_ID_FLV1){ 987 && s->codec_id != CODEC_ID_H263 && s->codec_id != CODEC_ID_H263P && s->codec_id != CODEC_ID_FLV1){
988 av_log(avctx, AV_LOG_ERROR, "4MV not supported by codec\n"); 988 av_log(avctx, AV_LOG_ERROR, "4MV not supported by codec\n");
989 return -1; 989 return -1;
990 } 990 }
991 991
992 if(s->obmc && s->avctx->mb_decision != FF_MB_DECISION_SIMPLE){ 992 if(s->obmc && s->avctx->mb_decision != FF_MB_DECISION_SIMPLE){
993 av_log(avctx, AV_LOG_ERROR, "OBMC is only supported with simple mb decision\n"); 993 av_log(avctx, AV_LOG_ERROR, "OBMC is only supported with simple mb decision\n");
994 return -1; 994 return -1;
995 } 995 }
996 996
997 if(s->obmc && s->codec_id != CODEC_ID_H263 && s->codec_id != CODEC_ID_H263P){ 997 if(s->obmc && s->codec_id != CODEC_ID_H263 && s->codec_id != CODEC_ID_H263P){
998 av_log(avctx, AV_LOG_ERROR, "OBMC is only supported with H263(+)\n"); 998 av_log(avctx, AV_LOG_ERROR, "OBMC is only supported with H263(+)\n");
999 return -1; 999 return -1;
1000 } 1000 }
1001 1001
1002 if(s->quarter_sample && s->codec_id != CODEC_ID_MPEG4){ 1002 if(s->quarter_sample && s->codec_id != CODEC_ID_MPEG4){
1003 av_log(avctx, AV_LOG_ERROR, "qpel not supported by codec\n"); 1003 av_log(avctx, AV_LOG_ERROR, "qpel not supported by codec\n");
1004 return -1; 1004 return -1;
1005 } 1005 }
1006 1006
1007 if(s->data_partitioning && s->codec_id != CODEC_ID_MPEG4){ 1007 if(s->data_partitioning && s->codec_id != CODEC_ID_MPEG4){
1008 av_log(avctx, AV_LOG_ERROR, "data partitioning not supported by codec\n"); 1008 av_log(avctx, AV_LOG_ERROR, "data partitioning not supported by codec\n");
1009 return -1; 1009 return -1;
1010 } 1010 }
1011 1011
1012 if(s->max_b_frames && s->codec_id != CODEC_ID_MPEG4 && s->codec_id != CODEC_ID_MPEG1VIDEO && s->codec_id != CODEC_ID_MPEG2VIDEO){ 1012 if(s->max_b_frames && s->codec_id != CODEC_ID_MPEG4 && s->codec_id != CODEC_ID_MPEG1VIDEO && s->codec_id != CODEC_ID_MPEG2VIDEO){
1013 av_log(avctx, AV_LOG_ERROR, "b frames not supported by codec\n"); 1013 av_log(avctx, AV_LOG_ERROR, "b frames not supported by codec\n");
1014 return -1; 1014 return -1;
1015 } 1015 }
1016 1016
1017 if((s->flags & (CODEC_FLAG_INTERLACED_DCT|CODEC_FLAG_INTERLACED_ME|CODEC_FLAG_ALT_SCAN)) 1017 if((s->flags & (CODEC_FLAG_INTERLACED_DCT|CODEC_FLAG_INTERLACED_ME|CODEC_FLAG_ALT_SCAN))
1018 && s->codec_id != CODEC_ID_MPEG4 && s->codec_id != CODEC_ID_MPEG2VIDEO){ 1018 && s->codec_id != CODEC_ID_MPEG4 && s->codec_id != CODEC_ID_MPEG2VIDEO){
1019 av_log(avctx, AV_LOG_ERROR, "interlacing not supported by codec\n"); 1019 av_log(avctx, AV_LOG_ERROR, "interlacing not supported by codec\n");
1020 return -1; 1020 return -1;
1021 } 1021 }
1022 1022
1023 if(s->mpeg_quant && s->codec_id != CODEC_ID_MPEG4){ //FIXME mpeg2 uses that too 1023 if(s->mpeg_quant && s->codec_id != CODEC_ID_MPEG4){ //FIXME mpeg2 uses that too
1024 av_log(avctx, AV_LOG_ERROR, "mpeg2 style quantization not supported by codec\n"); 1024 av_log(avctx, AV_LOG_ERROR, "mpeg2 style quantization not supported by codec\n");
1025 return -1; 1025 return -1;
1026 } 1026 }
1027 1027
1028 if((s->flags & CODEC_FLAG_CBP_RD) && !(s->flags & CODEC_FLAG_TRELLIS_QUANT)){ 1028 if((s->flags & CODEC_FLAG_CBP_RD) && !(s->flags & CODEC_FLAG_TRELLIS_QUANT)){
1029 av_log(avctx, AV_LOG_ERROR, "CBP RD needs trellis quant\n"); 1029 av_log(avctx, AV_LOG_ERROR, "CBP RD needs trellis quant\n");
1030 return -1; 1030 return -1;
1031 } 1031 }
1032 1032
1033 if((s->flags & CODEC_FLAG_QP_RD) && s->avctx->mb_decision != FF_MB_DECISION_RD){ 1033 if((s->flags & CODEC_FLAG_QP_RD) && s->avctx->mb_decision != FF_MB_DECISION_RD){
1034 av_log(avctx, AV_LOG_ERROR, "QP RD needs mbd=2\n"); 1034 av_log(avctx, AV_LOG_ERROR, "QP RD needs mbd=2\n");
1035 return -1; 1035 return -1;
1036 } 1036 }
1037 1037
1038 if(s->avctx->scenechange_threshold < 1000000000 && (s->flags & CODEC_FLAG_CLOSED_GOP)){ 1038 if(s->avctx->scenechange_threshold < 1000000000 && (s->flags & CODEC_FLAG_CLOSED_GOP)){
1039 av_log(avctx, AV_LOG_ERROR, "closed gop with scene change detection arent supported yet\n"); 1039 av_log(avctx, AV_LOG_ERROR, "closed gop with scene change detection arent supported yet\n");
1040 return -1; 1040 return -1;
1041 } 1041 }
1042 1042
1043 if(s->avctx->thread_count > 1 && s->codec_id != CODEC_ID_MPEG4 1043 if(s->avctx->thread_count > 1 && s->codec_id != CODEC_ID_MPEG4
1044 && s->codec_id != CODEC_ID_MPEG1VIDEO && s->codec_id != CODEC_ID_MPEG2VIDEO 1044 && s->codec_id != CODEC_ID_MPEG1VIDEO && s->codec_id != CODEC_ID_MPEG2VIDEO
1045 && (s->codec_id != CODEC_ID_H263P || !(s->flags & CODEC_FLAG_H263P_SLICE_STRUCT))){ 1045 && (s->codec_id != CODEC_ID_H263P || !(s->flags & CODEC_FLAG_H263P_SLICE_STRUCT))){
1046 av_log(avctx, AV_LOG_ERROR, "multi threaded encoding not supported by codec\n"); 1046 av_log(avctx, AV_LOG_ERROR, "multi threaded encoding not supported by codec\n");
1047 return -1; 1047 return -1;
1048 } 1048 }
1049 1049
1050 if(s->avctx->thread_count > 1) 1050 if(s->avctx->thread_count > 1)
1051 s->rtp_mode= 1; 1051 s->rtp_mode= 1;
1052 1052
1053 if(!avctx->time_base.den || !avctx->time_base.num){ 1053 if(!avctx->time_base.den || !avctx->time_base.num){
1054 av_log(avctx, AV_LOG_ERROR, "framerate not set\n"); 1054 av_log(avctx, AV_LOG_ERROR, "framerate not set\n");
1055 return -1; 1055 return -1;
1056 } 1056 }
1057 1057
1058 i= (INT_MAX/2+128)>>8; 1058 i= (INT_MAX/2+128)>>8;
1059 if(avctx->me_threshold >= i){ 1059 if(avctx->me_threshold >= i){
1060 av_log(avctx, AV_LOG_ERROR, "me_threshold too large, max is %d\n", i - 1); 1060 av_log(avctx, AV_LOG_ERROR, "me_threshold too large, max is %d\n", i - 1);
1061 return -1; 1061 return -1;
1062 } 1062 }
1063 if(avctx->mb_threshold >= i){ 1063 if(avctx->mb_threshold >= i){
1064 av_log(avctx, AV_LOG_ERROR, "mb_threshold too large, max is %d\n", i - 1); 1064 av_log(avctx, AV_LOG_ERROR, "mb_threshold too large, max is %d\n", i - 1);
1065 return -1; 1065 return -1;
1066 } 1066 }
1067 1067
1068 if(avctx->b_frame_strategy && (avctx->flags&CODEC_FLAG_PASS2)){ 1068 if(avctx->b_frame_strategy && (avctx->flags&CODEC_FLAG_PASS2)){
1069 av_log(avctx, AV_LOG_ERROR, "b_frame_strategy must be 0 on the second pass"); 1069 av_log(avctx, AV_LOG_ERROR, "b_frame_strategy must be 0 on the second pass");
1070 return -1; 1070 return -1;
1071 } 1071 }
1072 1072
1075 av_log(avctx, AV_LOG_INFO, "removing common factors from framerate\n"); 1075 av_log(avctx, AV_LOG_INFO, "removing common factors from framerate\n");
1076 avctx->time_base.den /= i; 1076 avctx->time_base.den /= i;
1077 avctx->time_base.num /= i; 1077 avctx->time_base.num /= i;
1078 // return -1; 1078 // return -1;
1079 } 1079 }
1080 1080
1081 if(s->codec_id==CODEC_ID_MJPEG){ 1081 if(s->codec_id==CODEC_ID_MJPEG){
1082 s->intra_quant_bias= 1<<(QUANT_BIAS_SHIFT-1); //(a + x/2)/x 1082 s->intra_quant_bias= 1<<(QUANT_BIAS_SHIFT-1); //(a + x/2)/x
1083 s->inter_quant_bias= 0; 1083 s->inter_quant_bias= 0;
1084 }else if(s->mpeg_quant || s->codec_id==CODEC_ID_MPEG1VIDEO || s->codec_id==CODEC_ID_MPEG2VIDEO){ 1084 }else if(s->mpeg_quant || s->codec_id==CODEC_ID_MPEG1VIDEO || s->codec_id==CODEC_ID_MPEG2VIDEO){
1085 s->intra_quant_bias= 3<<(QUANT_BIAS_SHIFT-3); //(a + x*3/8)/x 1085 s->intra_quant_bias= 3<<(QUANT_BIAS_SHIFT-3); //(a + x*3/8)/x
1086 s->inter_quant_bias= 0; 1086 s->inter_quant_bias= 0;
1087 }else{ 1087 }else{
1088 s->intra_quant_bias=0; 1088 s->intra_quant_bias=0;
1089 s->inter_quant_bias=-(1<<(QUANT_BIAS_SHIFT-2)); //(a - x/4)/x 1089 s->inter_quant_bias=-(1<<(QUANT_BIAS_SHIFT-2)); //(a - x/4)/x
1090 } 1090 }
1091 1091
1092 if(avctx->intra_quant_bias != FF_DEFAULT_QUANT_BIAS) 1092 if(avctx->intra_quant_bias != FF_DEFAULT_QUANT_BIAS)
1093 s->intra_quant_bias= avctx->intra_quant_bias; 1093 s->intra_quant_bias= avctx->intra_quant_bias;
1094 if(avctx->inter_quant_bias != FF_DEFAULT_QUANT_BIAS) 1094 if(avctx->inter_quant_bias != FF_DEFAULT_QUANT_BIAS)
1095 s->inter_quant_bias= avctx->inter_quant_bias; 1095 s->inter_quant_bias= avctx->inter_quant_bias;
1096 1096
1097 avcodec_get_chroma_sub_sample(avctx->pix_fmt, &chroma_h_shift, &chroma_v_shift); 1097 avcodec_get_chroma_sub_sample(avctx->pix_fmt, &chroma_h_shift, &chroma_v_shift);
1098 1098
1099 if(avctx->codec_id == CODEC_ID_MPEG4 && s->avctx->time_base.den > (1<<16)-1){ 1099 if(avctx->codec_id == CODEC_ID_MPEG4 && s->avctx->time_base.den > (1<<16)-1){
1100 av_log(avctx, AV_LOG_ERROR, "timebase not supported by mpeg 4 standard\n"); 1100 av_log(avctx, AV_LOG_ERROR, "timebase not supported by mpeg 4 standard\n");
1101 return -1; 1101 return -1;
1102 } 1102 }
1103 s->time_increment_bits = av_log2(s->avctx->time_base.den - 1) + 1; 1103 s->time_increment_bits = av_log2(s->avctx->time_base.den - 1) + 1;
1104 1104
1105 switch(avctx->codec->id) { 1105 switch(avctx->codec->id) {
1106 case CODEC_ID_MPEG1VIDEO: 1106 case CODEC_ID_MPEG1VIDEO:
1120 s->intra_only = 1; /* force intra only for jpeg */ 1120 s->intra_only = 1; /* force intra only for jpeg */
1121 s->mjpeg_write_tables = 1; /* write all tables */ 1121 s->mjpeg_write_tables = 1; /* write all tables */
1122 s->mjpeg_data_only_frames = 0; /* write all the needed headers */ 1122 s->mjpeg_data_only_frames = 0; /* write all the needed headers */
1123 s->mjpeg_vsample[0] = 1<<chroma_v_shift; 1123 s->mjpeg_vsample[0] = 1<<chroma_v_shift;
1124 s->mjpeg_vsample[1] = 1; 1124 s->mjpeg_vsample[1] = 1;
1125 s->mjpeg_vsample[2] = 1; 1125 s->mjpeg_vsample[2] = 1;
1126 s->mjpeg_hsample[0] = 1<<chroma_h_shift; 1126 s->mjpeg_hsample[0] = 1<<chroma_h_shift;
1127 s->mjpeg_hsample[1] = 1; 1127 s->mjpeg_hsample[1] = 1;
1128 s->mjpeg_hsample[2] = 1; 1128 s->mjpeg_hsample[2] = 1;
1129 if (mjpeg_init(s) < 0) 1129 if (mjpeg_init(s) < 0)
1130 return -1; 1130 return -1;
1131 avctx->delay=0; 1131 avctx->delay=0;
1132 s->low_delay=1; 1132 s->low_delay=1;
1133 break; 1133 break;
1243 s->low_delay=1; 1243 s->low_delay=1;
1244 break; 1244 break;
1245 default: 1245 default:
1246 return -1; 1246 return -1;
1247 } 1247 }
1248 1248
1249 avctx->has_b_frames= !s->low_delay; 1249 avctx->has_b_frames= !s->low_delay;
1250 1250
1251 s->encoding = 1; 1251 s->encoding = 1;
1252 1252
1253 /* init */ 1253 /* init */
1254 if (MPV_common_init(s) < 0) 1254 if (MPV_common_init(s) < 0)
1255 return -1; 1255 return -1;
1256 1256
1257 if(s->modified_quant) 1257 if(s->modified_quant)
1258 s->chroma_qscale_table= ff_h263_chroma_qscale_table; 1258 s->chroma_qscale_table= ff_h263_chroma_qscale_table;
1259 s->progressive_frame= 1259 s->progressive_frame=
1260 s->progressive_sequence= !(avctx->flags & (CODEC_FLAG_INTERLACED_DCT|CODEC_FLAG_INTERLACED_ME)); 1260 s->progressive_sequence= !(avctx->flags & (CODEC_FLAG_INTERLACED_DCT|CODEC_FLAG_INTERLACED_ME));
1261 s->quant_precision=5; 1261 s->quant_precision=5;
1262 1262
1263 ff_set_cmp(&s->dsp, s->dsp.ildct_cmp, s->avctx->ildct_cmp); 1263 ff_set_cmp(&s->dsp, s->dsp.ildct_cmp, s->avctx->ildct_cmp);
1264 ff_set_cmp(&s->dsp, s->dsp.frame_skip_cmp, s->avctx->frame_skip_cmp); 1264 ff_set_cmp(&s->dsp, s->dsp.frame_skip_cmp, s->avctx->frame_skip_cmp);
1265 1265
1266 #ifdef CONFIG_H261_ENCODER 1266 #ifdef CONFIG_H261_ENCODER
1267 if (s->out_format == FMT_H261) 1267 if (s->out_format == FMT_H261)
1268 ff_h261_encode_init(s); 1268 ff_h261_encode_init(s);
1269 #endif 1269 #endif
1270 if (s->out_format == FMT_H263) 1270 if (s->out_format == FMT_H263)
1295 } 1295 }
1296 1296
1297 /* precompute matrix */ 1297 /* precompute matrix */
1298 /* for mjpeg, we do include qscale in the matrix */ 1298 /* for mjpeg, we do include qscale in the matrix */
1299 if (s->out_format != FMT_MJPEG) { 1299 if (s->out_format != FMT_MJPEG) {
1300 convert_matrix(&s->dsp, s->q_intra_matrix, s->q_intra_matrix16, 1300 convert_matrix(&s->dsp, s->q_intra_matrix, s->q_intra_matrix16,
1301 s->intra_matrix, s->intra_quant_bias, avctx->qmin, 31, 1); 1301 s->intra_matrix, s->intra_quant_bias, avctx->qmin, 31, 1);
1302 convert_matrix(&s->dsp, s->q_inter_matrix, s->q_inter_matrix16, 1302 convert_matrix(&s->dsp, s->q_inter_matrix, s->q_inter_matrix16,
1303 s->inter_matrix, s->inter_quant_bias, avctx->qmin, 31, 0); 1303 s->inter_matrix, s->inter_quant_bias, avctx->qmin, 31, 0);
1304 } 1304 }
1305 1305
1306 if(ff_rate_control_init(s) < 0) 1306 if(ff_rate_control_init(s) < 0)
1307 return -1; 1307 return -1;
1308 1308
1309 return 0; 1309 return 0;
1310 } 1310 }
1311 1311
1312 int MPV_encode_end(AVCodecContext *avctx) 1312 int MPV_encode_end(AVCodecContext *avctx)
1313 { 1313 {
1322 MPV_common_end(s); 1322 MPV_common_end(s);
1323 if (s->out_format == FMT_MJPEG) 1323 if (s->out_format == FMT_MJPEG)
1324 mjpeg_close(s); 1324 mjpeg_close(s);
1325 1325
1326 av_freep(&avctx->extradata); 1326 av_freep(&avctx->extradata);
1327 1327
1328 return 0; 1328 return 0;
1329 } 1329 }
1330 1330
1331 #endif //CONFIG_ENCODERS 1331 #endif //CONFIG_ENCODERS
1332 1332
1410 } 1410 }
1411 } 1411 }
1412 1412
1413 int ff_find_unused_picture(MpegEncContext *s, int shared){ 1413 int ff_find_unused_picture(MpegEncContext *s, int shared){
1414 int i; 1414 int i;
1415 1415
1416 if(shared){ 1416 if(shared){
1417 for(i=0; i<MAX_PICTURE_COUNT; i++){ 1417 for(i=0; i<MAX_PICTURE_COUNT; i++){
1418 if(s->picture[i].data[0]==NULL && s->picture[i].type==0) return i; 1418 if(s->picture[i].data[0]==NULL && s->picture[i].type==0) return i;
1419 } 1419 }
1420 }else{ 1420 }else{
1438 for(i=0; i<64; i++){ 1438 for(i=0; i<64; i++){
1439 s->dct_error_sum[intra][i] >>=1; 1439 s->dct_error_sum[intra][i] >>=1;
1440 } 1440 }
1441 s->dct_count[intra] >>= 1; 1441 s->dct_count[intra] >>= 1;
1442 } 1442 }
1443 1443
1444 for(i=0; i<64; i++){ 1444 for(i=0; i<64; i++){
1445 s->dct_offset[intra][i]= (s->avctx->noise_reduction * s->dct_count[intra] + s->dct_error_sum[intra][i]/2) / (s->dct_error_sum[intra][i]+1); 1445 s->dct_offset[intra][i]= (s->avctx->noise_reduction * s->dct_count[intra] + s->dct_error_sum[intra][i]/2) / (s->dct_error_sum[intra][i]+1);
1446 } 1446 }
1447 } 1447 }
1448 } 1448 }
1466 /* if(mpeg124/h263) */ 1466 /* if(mpeg124/h263) */
1467 if(!s->encoding){ 1467 if(!s->encoding){
1468 for(i=0; i<MAX_PICTURE_COUNT; i++){ 1468 for(i=0; i<MAX_PICTURE_COUNT; i++){
1469 if(s->picture[i].data[0] && &s->picture[i] != s->next_picture_ptr && s->picture[i].reference){ 1469 if(s->picture[i].data[0] && &s->picture[i] != s->next_picture_ptr && s->picture[i].reference){
1470 av_log(avctx, AV_LOG_ERROR, "releasing zombie picture\n"); 1470 av_log(avctx, AV_LOG_ERROR, "releasing zombie picture\n");
1471 avctx->release_buffer(avctx, (AVFrame*)&s->picture[i]); 1471 avctx->release_buffer(avctx, (AVFrame*)&s->picture[i]);
1472 } 1472 }
1473 } 1473 }
1474 } 1474 }
1475 } 1475 }
1476 alloc: 1476 alloc:
1491 1491
1492 pic->reference= (s->pict_type != B_TYPE || s->codec_id == CODEC_ID_H264) 1492 pic->reference= (s->pict_type != B_TYPE || s->codec_id == CODEC_ID_H264)
1493 && !s->dropable ? 3 : 0; 1493 && !s->dropable ? 3 : 0;
1494 1494
1495 pic->coded_picture_number= s->coded_picture_number++; 1495 pic->coded_picture_number= s->coded_picture_number++;
1496 1496
1497 if( alloc_picture(s, (Picture*)pic, 0) < 0) 1497 if( alloc_picture(s, (Picture*)pic, 0) < 0)
1498 return -1; 1498 return -1;
1499 1499
1500 s->current_picture_ptr= (Picture*)pic; 1500 s->current_picture_ptr= (Picture*)pic;
1501 s->current_picture_ptr->top_field_first= s->top_field_first; //FIXME use only the vars from current_pic 1501 s->current_picture_ptr->top_field_first= s->top_field_first; //FIXME use only the vars from current_pic
1502 s->current_picture_ptr->interlaced_frame= !s->progressive_frame && !s->progressive_sequence; 1502 s->current_picture_ptr->interlaced_frame= !s->progressive_frame && !s->progressive_sequence;
1503 } 1503 }
1504 1504
1505 s->current_picture_ptr->pict_type= s->pict_type; 1505 s->current_picture_ptr->pict_type= s->pict_type;
1506 // if(s->flags && CODEC_FLAG_QSCALE) 1506 // if(s->flags && CODEC_FLAG_QSCALE)
1507 // s->current_picture_ptr->quality= s->new_picture_ptr->quality; 1507 // s->current_picture_ptr->quality= s->new_picture_ptr->quality;
1508 s->current_picture_ptr->key_frame= s->pict_type == I_TYPE; 1508 s->current_picture_ptr->key_frame= s->pict_type == I_TYPE;
1509 1509
1510 copy_picture(&s->current_picture, s->current_picture_ptr); 1510 copy_picture(&s->current_picture, s->current_picture_ptr);
1511 1511
1512 if(s->out_format != FMT_H264 || s->codec_id == CODEC_ID_SVQ3){ 1512 if(s->out_format != FMT_H264 || s->codec_id == CODEC_ID_SVQ3){
1513 if (s->pict_type != B_TYPE) { 1513 if (s->pict_type != B_TYPE) {
1514 s->last_picture_ptr= s->next_picture_ptr; 1514 s->last_picture_ptr= s->next_picture_ptr;
1515 if(!s->dropable) 1515 if(!s->dropable)
1516 s->next_picture_ptr= s->current_picture_ptr; 1516 s->next_picture_ptr= s->current_picture_ptr;
1517 } 1517 }
1518 /* av_log(s->avctx, AV_LOG_DEBUG, "L%p N%p C%p L%p N%p C%p type:%d drop:%d\n", s->last_picture_ptr, s->next_picture_ptr,s->current_picture_ptr, 1518 /* av_log(s->avctx, AV_LOG_DEBUG, "L%p N%p C%p L%p N%p C%p type:%d drop:%d\n", s->last_picture_ptr, s->next_picture_ptr,s->current_picture_ptr,
1519 s->last_picture_ptr ? s->last_picture_ptr->data[0] : NULL, 1519 s->last_picture_ptr ? s->last_picture_ptr->data[0] : NULL,
1520 s->next_picture_ptr ? s->next_picture_ptr->data[0] : NULL, 1520 s->next_picture_ptr ? s->next_picture_ptr->data[0] : NULL,
1521 s->current_picture_ptr ? s->current_picture_ptr->data[0] : NULL, 1521 s->current_picture_ptr ? s->current_picture_ptr->data[0] : NULL,
1522 s->pict_type, s->dropable);*/ 1522 s->pict_type, s->dropable);*/
1523 1523
1524 if(s->last_picture_ptr) copy_picture(&s->last_picture, s->last_picture_ptr); 1524 if(s->last_picture_ptr) copy_picture(&s->last_picture, s->last_picture_ptr);
1525 if(s->next_picture_ptr) copy_picture(&s->next_picture, s->next_picture_ptr); 1525 if(s->next_picture_ptr) copy_picture(&s->next_picture, s->next_picture_ptr);
1526 1526
1527 if(s->pict_type != I_TYPE && (s->last_picture_ptr==NULL || s->last_picture_ptr->data[0]==NULL)){ 1527 if(s->pict_type != I_TYPE && (s->last_picture_ptr==NULL || s->last_picture_ptr->data[0]==NULL)){
1528 av_log(avctx, AV_LOG_ERROR, "warning: first frame is no keyframe\n"); 1528 av_log(avctx, AV_LOG_ERROR, "warning: first frame is no keyframe\n");
1529 assert(s->pict_type != B_TYPE); //these should have been dropped if we don't have a reference 1529 assert(s->pict_type != B_TYPE); //these should have been dropped if we don't have a reference
1530 goto alloc; 1530 goto alloc;
1531 } 1531 }
1535 if(s->picture_structure!=PICT_FRAME){ 1535 if(s->picture_structure!=PICT_FRAME){
1536 int i; 1536 int i;
1537 for(i=0; i<4; i++){ 1537 for(i=0; i<4; i++){
1538 if(s->picture_structure == PICT_BOTTOM_FIELD){ 1538 if(s->picture_structure == PICT_BOTTOM_FIELD){
1539 s->current_picture.data[i] += s->current_picture.linesize[i]; 1539 s->current_picture.data[i] += s->current_picture.linesize[i];
1540 } 1540 }
1541 s->current_picture.linesize[i] *= 2; 1541 s->current_picture.linesize[i] *= 2;
1542 s->last_picture.linesize[i] *=2; 1542 s->last_picture.linesize[i] *=2;
1543 s->next_picture.linesize[i] *=2; 1543 s->next_picture.linesize[i] *=2;
1544 } 1544 }
1545 } 1545 }
1546 } 1546 }
1547 1547
1548 s->hurry_up= s->avctx->hurry_up; 1548 s->hurry_up= s->avctx->hurry_up;
1549 s->error_resilience= avctx->error_resilience; 1549 s->error_resilience= avctx->error_resilience;
1550 1550
1551 /* set dequantizer, we can't do it during init as it might change for mpeg4 1551 /* set dequantizer, we can't do it during init as it might change for mpeg4
1552 and we can't do it in the header decode as init isnt called for mpeg4 there yet */ 1552 and we can't do it in the header decode as init isnt called for mpeg4 there yet */
1564 if(s->dct_error_sum){ 1564 if(s->dct_error_sum){
1565 assert(s->avctx->noise_reduction && s->encoding); 1565 assert(s->avctx->noise_reduction && s->encoding);
1566 1566
1567 update_noise_reduction(s); 1567 update_noise_reduction(s);
1568 } 1568 }
1569 1569
1570 #ifdef HAVE_XVMC 1570 #ifdef HAVE_XVMC
1571 if(s->avctx->xvmc_acceleration) 1571 if(s->avctx->xvmc_acceleration)
1572 return XVMC_field_start(s, avctx); 1572 return XVMC_field_start(s, avctx);
1573 #endif 1573 #endif
1574 return 0; 1574 return 0;
1589 draw_edges(s->current_picture.data[0], s->linesize , s->h_edge_pos , s->v_edge_pos , EDGE_WIDTH ); 1589 draw_edges(s->current_picture.data[0], s->linesize , s->h_edge_pos , s->v_edge_pos , EDGE_WIDTH );
1590 draw_edges(s->current_picture.data[1], s->uvlinesize, s->h_edge_pos>>1, s->v_edge_pos>>1, EDGE_WIDTH/2); 1590 draw_edges(s->current_picture.data[1], s->uvlinesize, s->h_edge_pos>>1, s->v_edge_pos>>1, EDGE_WIDTH/2);
1591 draw_edges(s->current_picture.data[2], s->uvlinesize, s->h_edge_pos>>1, s->v_edge_pos>>1, EDGE_WIDTH/2); 1591 draw_edges(s->current_picture.data[2], s->uvlinesize, s->h_edge_pos>>1, s->v_edge_pos>>1, EDGE_WIDTH/2);
1592 } 1592 }
1593 emms_c(); 1593 emms_c();
1594 1594
1595 s->last_pict_type = s->pict_type; 1595 s->last_pict_type = s->pict_type;
1596 if(s->pict_type!=B_TYPE){ 1596 if(s->pict_type!=B_TYPE){
1597 s->last_non_b_pict_type= s->pict_type; 1597 s->last_non_b_pict_type= s->pict_type;
1598 } 1598 }
1599 #if 0 1599 #if 0
1600 /* copy back current_picture variables */ 1600 /* copy back current_picture variables */
1601 for(i=0; i<MAX_PICTURE_COUNT; i++){ 1601 for(i=0; i<MAX_PICTURE_COUNT; i++){
1602 if(s->picture[i].data[0] == s->current_picture.data[0]){ 1602 if(s->picture[i].data[0] == s->current_picture.data[0]){
1603 s->picture[i]= s->current_picture; 1603 s->picture[i]= s->current_picture;
1604 break; 1604 break;
1605 } 1605 }
1606 } 1606 }
1607 assert(i<MAX_PICTURE_COUNT); 1607 assert(i<MAX_PICTURE_COUNT);
1608 #endif 1608 #endif
1609 1609
1610 if(s->encoding){ 1610 if(s->encoding){
1611 /* release non-reference frames */ 1611 /* release non-reference frames */
1612 for(i=0; i<MAX_PICTURE_COUNT; i++){ 1612 for(i=0; i<MAX_PICTURE_COUNT; i++){
1613 if(s->picture[i].data[0] && !s->picture[i].reference /*&& s->picture[i].type!=FF_BUFFER_TYPE_SHARED*/){ 1613 if(s->picture[i].data[0] && !s->picture[i].reference /*&& s->picture[i].type!=FF_BUFFER_TYPE_SHARED*/){
1631 * @param stride stride/linesize of the image 1631 * @param stride stride/linesize of the image
1632 * @param color color of the arrow 1632 * @param color color of the arrow
1633 */ 1633 */
1634 static void draw_line(uint8_t *buf, int sx, int sy, int ex, int ey, int w, int h, int stride, int color){ 1634 static void draw_line(uint8_t *buf, int sx, int sy, int ex, int ey, int w, int h, int stride, int color){
1635 int t, x, y, fr, f; 1635 int t, x, y, fr, f;
1636 1636
1637 sx= clip(sx, 0, w-1); 1637 sx= clip(sx, 0, w-1);
1638 sy= clip(sy, 0, h-1); 1638 sy= clip(sy, 0, h-1);
1639 ex= clip(ex, 0, w-1); 1639 ex= clip(ex, 0, w-1);
1640 ey= clip(ey, 0, h-1); 1640 ey= clip(ey, 0, h-1);
1641 1641
1642 buf[sy*stride + sx]+= color; 1642 buf[sy*stride + sx]+= color;
1643 1643
1644 if(ABS(ex - sx) > ABS(ey - sy)){ 1644 if(ABS(ex - sx) > ABS(ey - sy)){
1645 if(sx > ex){ 1645 if(sx > ex){
1646 t=sx; sx=ex; ex=t; 1646 t=sx; sx=ex; ex=t;
1647 t=sy; sy=ey; ey=t; 1647 t=sy; sy=ey; ey=t;
1648 } 1648 }
1678 * @param w width of the image 1678 * @param w width of the image
1679 * @param h height of the image 1679 * @param h height of the image
1680 * @param stride stride/linesize of the image 1680 * @param stride stride/linesize of the image
1681 * @param color color of the arrow 1681 * @param color color of the arrow
1682 */ 1682 */
1683 static void draw_arrow(uint8_t *buf, int sx, int sy, int ex, int ey, int w, int h, int stride, int color){ 1683 static void draw_arrow(uint8_t *buf, int sx, int sy, int ex, int ey, int w, int h, int stride, int color){
1684 int dx,dy; 1684 int dx,dy;
1685 1685
1686 sx= clip(sx, -100, w+100); 1686 sx= clip(sx, -100, w+100);
1687 sy= clip(sy, -100, h+100); 1687 sy= clip(sy, -100, h+100);
1688 ex= clip(ex, -100, w+100); 1688 ex= clip(ex, -100, w+100);
1689 ey= clip(ey, -100, h+100); 1689 ey= clip(ey, -100, h+100);
1690 1690
1691 dx= ex - sx; 1691 dx= ex - sx;
1692 dy= ey - sy; 1692 dy= ey - sy;
1693 1693
1694 if(dx*dx + dy*dy > 3*3){ 1694 if(dx*dx + dy*dy > 3*3){
1695 int rx= dx + dy; 1695 int rx= dx + dy;
1696 int ry= -dx + dy; 1696 int ry= -dx + dy;
1697 int length= ff_sqrt((rx*rx + ry*ry)<<8); 1697 int length= ff_sqrt((rx*rx + ry*ry)<<8);
1698 1698
1699 //FIXME subpixel accuracy 1699 //FIXME subpixel accuracy
1700 rx= ROUNDED_DIV(rx*3<<4, length); 1700 rx= ROUNDED_DIV(rx*3<<4, length);
1701 ry= ROUNDED_DIV(ry*3<<4, length); 1701 ry= ROUNDED_DIV(ry*3<<4, length);
1702 1702
1703 draw_line(buf, sx, sy, sx + rx, sy + ry, w, h, stride, color); 1703 draw_line(buf, sx, sy, sx + rx, sy + ry, w, h, stride, color);
1704 draw_line(buf, sx, sy, sx - ry, sy + rx, w, h, stride, color); 1704 draw_line(buf, sx, sy, sx - ry, sy + rx, w, h, stride, color);
1705 } 1705 }
1706 draw_line(buf, sx, sy, ex, ey, w, h, stride, color); 1706 draw_line(buf, sx, sy, ex, ey, w, h, stride, color);
1707 } 1707 }
1713 1713
1714 if(!pict || !pict->mb_type) return; 1714 if(!pict || !pict->mb_type) return;
1715 1715
1716 if(s->avctx->debug&(FF_DEBUG_SKIP | FF_DEBUG_QP | FF_DEBUG_MB_TYPE)){ 1716 if(s->avctx->debug&(FF_DEBUG_SKIP | FF_DEBUG_QP | FF_DEBUG_MB_TYPE)){
1717 int x,y; 1717 int x,y;
1718 1718
1719 av_log(s->avctx,AV_LOG_DEBUG,"New frame, type: "); 1719 av_log(s->avctx,AV_LOG_DEBUG,"New frame, type: ");
1720 switch (pict->pict_type) { 1720 switch (pict->pict_type) {
1721 case FF_I_TYPE: av_log(s->avctx,AV_LOG_DEBUG,"I\n"); break; 1721 case FF_I_TYPE: av_log(s->avctx,AV_LOG_DEBUG,"I\n"); break;
1722 case FF_P_TYPE: av_log(s->avctx,AV_LOG_DEBUG,"P\n"); break; 1722 case FF_P_TYPE: av_log(s->avctx,AV_LOG_DEBUG,"P\n"); break;
1723 case FF_B_TYPE: av_log(s->avctx,AV_LOG_DEBUG,"B\n"); break; 1723 case FF_B_TYPE: av_log(s->avctx,AV_LOG_DEBUG,"B\n"); break;
1724 case FF_S_TYPE: av_log(s->avctx,AV_LOG_DEBUG,"S\n"); break; 1724 case FF_S_TYPE: av_log(s->avctx,AV_LOG_DEBUG,"S\n"); break;
1725 case FF_SI_TYPE: av_log(s->avctx,AV_LOG_DEBUG,"SI\n"); break; 1725 case FF_SI_TYPE: av_log(s->avctx,AV_LOG_DEBUG,"SI\n"); break;
1726 case FF_SP_TYPE: av_log(s->avctx,AV_LOG_DEBUG,"SP\n"); break; 1726 case FF_SP_TYPE: av_log(s->avctx,AV_LOG_DEBUG,"SP\n"); break;
1727 } 1727 }
1728 for(y=0; y<s->mb_height; y++){ 1728 for(y=0; y<s->mb_height; y++){
1729 for(x=0; x<s->mb_width; x++){ 1729 for(x=0; x<s->mb_width; x++){
1730 if(s->avctx->debug&FF_DEBUG_SKIP){ 1730 if(s->avctx->debug&FF_DEBUG_SKIP){
1731 int count= s->mbskip_table[x + y*s->mb_stride]; 1731 int count= s->mbskip_table[x + y*s->mb_stride];
1762 av_log(s->avctx, AV_LOG_DEBUG, "<"); 1762 av_log(s->avctx, AV_LOG_DEBUG, "<");
1763 else{ 1763 else{
1764 assert(USES_LIST(mb_type, 0) && USES_LIST(mb_type, 1)); 1764 assert(USES_LIST(mb_type, 0) && USES_LIST(mb_type, 1));
1765 av_log(s->avctx, AV_LOG_DEBUG, "X"); 1765 av_log(s->avctx, AV_LOG_DEBUG, "X");
1766 } 1766 }
1767 1767
1768 //segmentation 1768 //segmentation
1769 if(IS_8X8(mb_type)) 1769 if(IS_8X8(mb_type))
1770 av_log(s->avctx, AV_LOG_DEBUG, "+"); 1770 av_log(s->avctx, AV_LOG_DEBUG, "+");
1771 else if(IS_16X8(mb_type)) 1771 else if(IS_16X8(mb_type))
1772 av_log(s->avctx, AV_LOG_DEBUG, "-"); 1772 av_log(s->avctx, AV_LOG_DEBUG, "-");
1774 av_log(s->avctx, AV_LOG_DEBUG, "|"); 1774 av_log(s->avctx, AV_LOG_DEBUG, "|");
1775 else if(IS_INTRA(mb_type) || IS_16X16(mb_type)) 1775 else if(IS_INTRA(mb_type) || IS_16X16(mb_type))
1776 av_log(s->avctx, AV_LOG_DEBUG, " "); 1776 av_log(s->avctx, AV_LOG_DEBUG, " ");
1777 else 1777 else
1778 av_log(s->avctx, AV_LOG_DEBUG, "?"); 1778 av_log(s->avctx, AV_LOG_DEBUG, "?");
1779 1779
1780 1780
1781 if(IS_INTERLACED(mb_type) && s->codec_id == CODEC_ID_H264) 1781 if(IS_INTERLACED(mb_type) && s->codec_id == CODEC_ID_H264)
1782 av_log(s->avctx, AV_LOG_DEBUG, "="); 1782 av_log(s->avctx, AV_LOG_DEBUG, "=");
1783 else 1783 else
1784 av_log(s->avctx, AV_LOG_DEBUG, " "); 1784 av_log(s->avctx, AV_LOG_DEBUG, " ");
1785 } 1785 }
1850 int sx=mb_x*16 + 8; 1850 int sx=mb_x*16 + 8;
1851 int sy=mb_y*16 + 4 + 8*i; 1851 int sy=mb_y*16 + 4 + 8*i;
1852 int xy= (mb_x*2 + (mb_y*2 + i)*mv_stride) << (mv_sample_log2-1); 1852 int xy= (mb_x*2 + (mb_y*2 + i)*mv_stride) << (mv_sample_log2-1);
1853 int mx=(pict->motion_val[direction][xy][0]>>shift); 1853 int mx=(pict->motion_val[direction][xy][0]>>shift);
1854 int my=(pict->motion_val[direction][xy][1]>>shift); 1854 int my=(pict->motion_val[direction][xy][1]>>shift);
1855 1855
1856 if(IS_INTERLACED(pict->mb_type[mb_index])) 1856 if(IS_INTERLACED(pict->mb_type[mb_index]))
1857 my*=2; 1857 my*=2;
1858 1858
1859 draw_arrow(ptr, sx, sy, mx+sx, my+sy, width, height, s->linesize, 100); 1859 draw_arrow(ptr, sx, sy, mx+sx, my+sy, width, height, s->linesize, 100);
1860 } 1860 }
1861 }else if(IS_8X16(pict->mb_type[mb_index])){ 1861 }else if(IS_8X16(pict->mb_type[mb_index])){
1862 int i; 1862 int i;
1863 for(i=0; i<2; i++){ 1863 for(i=0; i<2; i++){
1864 int sx=mb_x*16 + 4 + 8*i; 1864 int sx=mb_x*16 + 4 + 8*i;
1865 int sy=mb_y*16 + 8; 1865 int sy=mb_y*16 + 8;
1866 int xy= (mb_x*2 + i + mb_y*2*mv_stride) << (mv_sample_log2-1); 1866 int xy= (mb_x*2 + i + mb_y*2*mv_stride) << (mv_sample_log2-1);
1867 int mx=(pict->motion_val[direction][xy][0]>>shift); 1867 int mx=(pict->motion_val[direction][xy][0]>>shift);
1868 int my=(pict->motion_val[direction][xy][1]>>shift); 1868 int my=(pict->motion_val[direction][xy][1]>>shift);
1869 1869
1870 if(IS_INTERLACED(pict->mb_type[mb_index])) 1870 if(IS_INTERLACED(pict->mb_type[mb_index]))
1871 my*=2; 1871 my*=2;
1872 1872
1873 draw_arrow(ptr, sx, sy, mx+sx, my+sy, width, height, s->linesize, 100); 1873 draw_arrow(ptr, sx, sy, mx+sx, my+sy, width, height, s->linesize, 100);
1874 } 1874 }
1875 }else{ 1875 }else{
1876 int sx= mb_x*16 + 8; 1876 int sx= mb_x*16 + 8;
1877 int sy= mb_y*16 + 8; 1877 int sy= mb_y*16 + 8;
1878 int xy= (mb_x + mb_y*mv_stride) << mv_sample_log2; 1878 int xy= (mb_x + mb_y*mv_stride) << mv_sample_log2;
1879 int mx= (pict->motion_val[direction][xy][0]>>shift) + sx; 1879 int mx= (pict->motion_val[direction][xy][0]>>shift) + sx;
1880 int my= (pict->motion_val[direction][xy][1]>>shift) + sy; 1880 int my= (pict->motion_val[direction][xy][1]>>shift) + sy;
1881 draw_arrow(ptr, sx, sy, mx, my, width, height, s->linesize, 100); 1881 draw_arrow(ptr, sx, sy, mx, my, width, height, s->linesize, 100);
1882 } 1882 }
1883 } 1883 }
1884 } 1884 }
1885 if((s->avctx->debug&FF_DEBUG_VIS_QP) && pict->motion_val){ 1885 if((s->avctx->debug&FF_DEBUG_VIS_QP) && pict->motion_val){
1886 uint64_t c= (pict->qscale_table[mb_index]*128/31) * 0x0101010101010101ULL; 1886 uint64_t c= (pict->qscale_table[mb_index]*128/31) * 0x0101010101010101ULL;
1887 int y; 1887 int y;
1888 for(y=0; y<8; y++){ 1888 for(y=0; y<8; y++){
1896 int y; 1896 int y;
1897 #define COLOR(theta, r)\ 1897 #define COLOR(theta, r)\
1898 u= (int)(128 + r*cos(theta*3.141592/180));\ 1898 u= (int)(128 + r*cos(theta*3.141592/180));\
1899 v= (int)(128 + r*sin(theta*3.141592/180)); 1899 v= (int)(128 + r*sin(theta*3.141592/180));
1900 1900
1901 1901
1902 u=v=128; 1902 u=v=128;
1903 if(IS_PCM(mb_type)){ 1903 if(IS_PCM(mb_type)){
1904 COLOR(120,48) 1904 COLOR(120,48)
1905 }else if((IS_INTRA(mb_type) && IS_ACPRED(mb_type)) || IS_INTRA16x16(mb_type)){ 1905 }else if((IS_INTRA(mb_type) && IS_ACPRED(mb_type)) || IS_INTRA16x16(mb_type)){
1906 COLOR(30,48) 1906 COLOR(30,48)
1954 pict->data[0][sx + 4 + (sy + y)*pict->linesize[0]]^= 0x80; 1954 pict->data[0][sx + 4 + (sy + y)*pict->linesize[0]]^= 0x80;
1955 if(mv[0] != mv[dm*mv_stride] || mv[dm] != mv[dm*(mv_stride+1)]) 1955 if(mv[0] != mv[dm*mv_stride] || mv[dm] != mv[dm*(mv_stride+1)])
1956 *(uint64_t*)(pict->data[0] + sx + (sy + 4)*pict->linesize[0])^= 0x8080808080808080ULL; 1956 *(uint64_t*)(pict->data[0] + sx + (sy + 4)*pict->linesize[0])^= 0x8080808080808080ULL;
1957 } 1957 }
1958 } 1958 }
1959 1959
1960 if(IS_INTERLACED(mb_type) && s->codec_id == CODEC_ID_H264){ 1960 if(IS_INTERLACED(mb_type) && s->codec_id == CODEC_ID_H264){
1961 // hmm 1961 // hmm
1962 } 1962 }
1963 } 1963 }
1964 s->mbskip_table[mb_index]=0; 1964 s->mbskip_table[mb_index]=0;
1970 #ifdef CONFIG_ENCODERS 1970 #ifdef CONFIG_ENCODERS
1971 1971
1972 static int get_sae(uint8_t *src, int ref, int stride){ 1972 static int get_sae(uint8_t *src, int ref, int stride){
1973 int x,y; 1973 int x,y;
1974 int acc=0; 1974 int acc=0;
1975 1975
1976 for(y=0; y<16; y++){ 1976 for(y=0; y<16; y++){
1977 for(x=0; x<16; x++){ 1977 for(x=0; x<16; x++){
1978 acc+= ABS(src[x+y*stride] - ref); 1978 acc+= ABS(src[x+y*stride] - ref);
1979 } 1979 }
1980 } 1980 }
1981 1981
1982 return acc; 1982 return acc;
1983 } 1983 }
1984 1984
1985 static int get_intra_count(MpegEncContext *s, uint8_t *src, uint8_t *ref, int stride){ 1985 static int get_intra_count(MpegEncContext *s, uint8_t *src, uint8_t *ref, int stride){
1986 int x, y, w, h; 1986 int x, y, w, h;
1987 int acc=0; 1987 int acc=0;
1988 1988
1989 w= s->width &~15; 1989 w= s->width &~15;
1990 h= s->height&~15; 1990 h= s->height&~15;
1991 1991
1992 for(y=0; y<h; y+=16){ 1992 for(y=0; y<h; y+=16){
1993 for(x=0; x<w; x+=16){ 1993 for(x=0; x<w; x+=16){
1994 int offset= x + y*stride; 1994 int offset= x + y*stride;
1995 int sad = s->dsp.sad[0](NULL, src + offset, ref + offset, stride, 16); 1995 int sad = s->dsp.sad[0](NULL, src + offset, ref + offset, stride, 16);
1996 int mean= (s->dsp.pix_sum(src + offset, stride) + 128)>>8; 1996 int mean= (s->dsp.pix_sum(src + offset, stride) + 128)>>8;
1997 int sae = get_sae(src + offset, mean, stride); 1997 int sae = get_sae(src + offset, mean, stride);
1998 1998
1999 acc+= sae + 500 < sad; 1999 acc+= sae + 500 < sad;
2000 } 2000 }
2001 } 2001 }
2002 return acc; 2002 return acc;
2003 } 2003 }
2007 AVFrame *pic=NULL; 2007 AVFrame *pic=NULL;
2008 int64_t pts; 2008 int64_t pts;
2009 int i; 2009 int i;
2010 const int encoding_delay= s->max_b_frames; 2010 const int encoding_delay= s->max_b_frames;
2011 int direct=1; 2011 int direct=1;
2012 2012
2013 if(pic_arg){ 2013 if(pic_arg){
2014 pts= pic_arg->pts; 2014 pts= pic_arg->pts;
2015 pic_arg->display_picture_number= s->input_picture_number++; 2015 pic_arg->display_picture_number= s->input_picture_number++;
2016 2016
2017 if(pts != AV_NOPTS_VALUE){ 2017 if(pts != AV_NOPTS_VALUE){
2018 if(s->user_specified_pts != AV_NOPTS_VALUE){ 2018 if(s->user_specified_pts != AV_NOPTS_VALUE){
2019 int64_t time= pts; 2019 int64_t time= pts;
2020 int64_t last= s->user_specified_pts; 2020 int64_t last= s->user_specified_pts;
2021 2021
2022 if(time <= last){ 2022 if(time <= last){
2023 av_log(s->avctx, AV_LOG_ERROR, "Error, Invalid timestamp=%"PRId64", last=%"PRId64"\n", pts, s->user_specified_pts); 2023 av_log(s->avctx, AV_LOG_ERROR, "Error, Invalid timestamp=%"PRId64", last=%"PRId64"\n", pts, s->user_specified_pts);
2024 return -1; 2024 return -1;
2025 } 2025 }
2026 } 2026 }
2027 s->user_specified_pts= pts; 2027 s->user_specified_pts= pts;
2028 }else{ 2028 }else{
2029 if(s->user_specified_pts != AV_NOPTS_VALUE){ 2029 if(s->user_specified_pts != AV_NOPTS_VALUE){
2030 s->user_specified_pts= 2030 s->user_specified_pts=
2031 pts= s->user_specified_pts + 1; 2031 pts= s->user_specified_pts + 1;
2032 av_log(s->avctx, AV_LOG_INFO, "Warning: AVFrame.pts=? trying to guess (%"PRId64")\n", pts); 2032 av_log(s->avctx, AV_LOG_INFO, "Warning: AVFrame.pts=? trying to guess (%"PRId64")\n", pts);
2033 }else{ 2033 }else{
2034 pts= pic_arg->display_picture_number; 2034 pts= pic_arg->display_picture_number;
2035 } 2035 }
2039 if(pic_arg){ 2039 if(pic_arg){
2040 if(encoding_delay && !(s->flags&CODEC_FLAG_INPUT_PRESERVED)) direct=0; 2040 if(encoding_delay && !(s->flags&CODEC_FLAG_INPUT_PRESERVED)) direct=0;
2041 if(pic_arg->linesize[0] != s->linesize) direct=0; 2041 if(pic_arg->linesize[0] != s->linesize) direct=0;
2042 if(pic_arg->linesize[1] != s->uvlinesize) direct=0; 2042 if(pic_arg->linesize[1] != s->uvlinesize) direct=0;
2043 if(pic_arg->linesize[2] != s->uvlinesize) direct=0; 2043 if(pic_arg->linesize[2] != s->uvlinesize) direct=0;
2044 2044
2045 // av_log(AV_LOG_DEBUG, "%d %d %d %d\n",pic_arg->linesize[0], pic_arg->linesize[1], s->linesize, s->uvlinesize); 2045 // av_log(AV_LOG_DEBUG, "%d %d %d %d\n",pic_arg->linesize[0], pic_arg->linesize[1], s->linesize, s->uvlinesize);
2046 2046
2047 if(direct){ 2047 if(direct){
2048 i= ff_find_unused_picture(s, 1); 2048 i= ff_find_unused_picture(s, 1);
2049 2049
2050 pic= (AVFrame*)&s->picture[i]; 2050 pic= (AVFrame*)&s->picture[i];
2051 pic->reference= 3; 2051 pic->reference= 3;
2052 2052
2053 for(i=0; i<4; i++){ 2053 for(i=0; i<4; i++){
2054 pic->data[i]= pic_arg->data[i]; 2054 pic->data[i]= pic_arg->data[i];
2055 pic->linesize[i]= pic_arg->linesize[i]; 2055 pic->linesize[i]= pic_arg->linesize[i];
2056 } 2056 }
2057 alloc_picture(s, (Picture*)pic, 1); 2057 alloc_picture(s, (Picture*)pic, 1);
2062 pic= (AVFrame*)&s->picture[i]; 2062 pic= (AVFrame*)&s->picture[i];
2063 pic->reference= 3; 2063 pic->reference= 3;
2064 2064
2065 alloc_picture(s, (Picture*)pic, 0); 2065 alloc_picture(s, (Picture*)pic, 0);
2066 2066
2067 if( pic->data[0] + offset == pic_arg->data[0] 2067 if( pic->data[0] + offset == pic_arg->data[0]
2068 && pic->data[1] + offset == pic_arg->data[1] 2068 && pic->data[1] + offset == pic_arg->data[1]
2069 && pic->data[2] + offset == pic_arg->data[2]){ 2069 && pic->data[2] + offset == pic_arg->data[2]){
2070 // empty 2070 // empty
2071 }else{ 2071 }else{
2072 int h_chroma_shift, v_chroma_shift; 2072 int h_chroma_shift, v_chroma_shift;
2073 avcodec_get_chroma_sub_sample(s->avctx->pix_fmt, &h_chroma_shift, &v_chroma_shift); 2073 avcodec_get_chroma_sub_sample(s->avctx->pix_fmt, &h_chroma_shift, &v_chroma_shift);
2074 2074
2075 for(i=0; i<3; i++){ 2075 for(i=0; i<3; i++){
2076 int src_stride= pic_arg->linesize[i]; 2076 int src_stride= pic_arg->linesize[i];
2077 int dst_stride= i ? s->uvlinesize : s->linesize; 2077 int dst_stride= i ? s->uvlinesize : s->linesize;
2078 int h_shift= i ? h_chroma_shift : 0; 2078 int h_shift= i ? h_chroma_shift : 0;
2079 int v_shift= i ? v_chroma_shift : 0; 2079 int v_shift= i ? v_chroma_shift : 0;
2080 int w= s->width >>h_shift; 2080 int w= s->width >>h_shift;
2081 int h= s->height>>v_shift; 2081 int h= s->height>>v_shift;
2082 uint8_t *src= pic_arg->data[i]; 2082 uint8_t *src= pic_arg->data[i];
2083 uint8_t *dst= pic->data[i] + offset; 2083 uint8_t *dst= pic->data[i] + offset;
2084 2084
2085 if(src_stride==dst_stride) 2085 if(src_stride==dst_stride)
2086 memcpy(dst, src, src_stride*h); 2086 memcpy(dst, src, src_stride*h);
2087 else{ 2087 else{
2088 while(h--){ 2088 while(h--){
2089 memcpy(dst, src, w); 2089 memcpy(dst, src, w);
2095 } 2095 }
2096 } 2096 }
2097 copy_picture_attributes(s, pic, pic_arg); 2097 copy_picture_attributes(s, pic, pic_arg);
2098 pic->pts= pts; //we set this here to avoid modifiying pic_arg 2098 pic->pts= pts; //we set this here to avoid modifiying pic_arg
2099 } 2099 }
2100 2100
2101 /* shift buffer entries */ 2101 /* shift buffer entries */
2102 for(i=1; i<MAX_PICTURE_COUNT /*s->encoding_delay+1*/; i++) 2102 for(i=1; i<MAX_PICTURE_COUNT /*s->encoding_delay+1*/; i++)
2103 s->input_picture[i-1]= s->input_picture[i]; 2103 s->input_picture[i-1]= s->input_picture[i];
2104 2104
2105 s->input_picture[encoding_delay]= (Picture*)pic; 2105 s->input_picture[encoding_delay]= (Picture*)pic;
2106 2106
2107 return 0; 2107 return 0;
2108 } 2108 }
2109 2109
2117 const int bw= plane ? 1 : 2; 2117 const int bw= plane ? 1 : 2;
2118 for(y=0; y<s->mb_height*bw; y++){ 2118 for(y=0; y<s->mb_height*bw; y++){
2119 for(x=0; x<s->mb_width*bw; x++){ 2119 for(x=0; x<s->mb_width*bw; x++){
2120 int off= p->type == FF_BUFFER_TYPE_SHARED ? 0: 16; 2120 int off= p->type == FF_BUFFER_TYPE_SHARED ? 0: 16;
2121 int v= s->dsp.frame_skip_cmp[1](s, p->data[plane] + 8*(x + y*stride)+off, ref->data[plane] + 8*(x + y*stride), stride, 8); 2121 int v= s->dsp.frame_skip_cmp[1](s, p->data[plane] + 8*(x + y*stride)+off, ref->data[plane] + 8*(x + y*stride), stride, 8);
2122 2122
2123 switch(s->avctx->frame_skip_exp){ 2123 switch(s->avctx->frame_skip_exp){
2124 case 0: score= FFMAX(score, v); break; 2124 case 0: score= FFMAX(score, v); break;
2125 case 1: score+= ABS(v);break; 2125 case 1: score+= ABS(v);break;
2126 case 2: score+= v*v;break; 2126 case 2: score+= v*v;break;
2127 case 3: score64+= ABS(v*v*(int64_t)v);break; 2127 case 3: score64+= ABS(v*v*(int64_t)v);break;
2128 case 4: score64+= v*v*(int64_t)(v*v);break; 2128 case 4: score64+= v*v*(int64_t)(v*v);break;
2129 } 2129 }
2130 } 2130 }
2131 } 2131 }
2132 } 2132 }
2133 2133
2134 if(score) score64= score; 2134 if(score) score64= score;
2135 2135
2136 if(score64 < s->avctx->frame_skip_threshold) 2136 if(score64 < s->avctx->frame_skip_threshold)
2137 return 1; 2137 return 1;
2138 if(score64 < ((s->avctx->frame_skip_factor * (int64_t)s->lambda)>>8)) 2138 if(score64 < ((s->avctx->frame_skip_factor * (int64_t)s->lambda)>>8))
2158 2158
2159 if(s->avctx->frame_skip_threshold || s->avctx->frame_skip_factor){ 2159 if(s->avctx->frame_skip_threshold || s->avctx->frame_skip_factor){
2160 if(s->picture_in_gop_number < s->gop_size && skip_check(s, s->input_picture[0], s->next_picture_ptr)){ 2160 if(s->picture_in_gop_number < s->gop_size && skip_check(s, s->input_picture[0], s->next_picture_ptr)){
2161 //FIXME check that te gop check above is +-1 correct 2161 //FIXME check that te gop check above is +-1 correct
2162 //av_log(NULL, AV_LOG_DEBUG, "skip %p %Ld\n", s->input_picture[0]->data[0], s->input_picture[0]->pts); 2162 //av_log(NULL, AV_LOG_DEBUG, "skip %p %Ld\n", s->input_picture[0]->data[0], s->input_picture[0]->pts);
2163 2163
2164 if(s->input_picture[0]->type == FF_BUFFER_TYPE_SHARED){ 2164 if(s->input_picture[0]->type == FF_BUFFER_TYPE_SHARED){
2165 for(i=0; i<4; i++) 2165 for(i=0; i<4; i++)
2166 s->input_picture[0]->data[i]= NULL; 2166 s->input_picture[0]->data[i]= NULL;
2167 s->input_picture[0]->type= 0; 2167 s->input_picture[0]->type= 0;
2168 }else{ 2168 }else{
2169 assert( s->input_picture[0]->type==FF_BUFFER_TYPE_USER 2169 assert( s->input_picture[0]->type==FF_BUFFER_TYPE_USER
2170 || s->input_picture[0]->type==FF_BUFFER_TYPE_INTERNAL); 2170 || s->input_picture[0]->type==FF_BUFFER_TYPE_INTERNAL);
2171 2171
2172 s->avctx->release_buffer(s->avctx, (AVFrame*)s->input_picture[0]); 2172 s->avctx->release_buffer(s->avctx, (AVFrame*)s->input_picture[0]);
2173 } 2173 }
2174 2174
2175 emms_c(); 2175 emms_c();
2176 ff_vbv_update(s, 0); 2176 ff_vbv_update(s, 0);
2177 2177
2178 goto no_output_pic; 2178 goto no_output_pic;
2179 } 2179 }
2181 2181
2182 if(s->flags&CODEC_FLAG_PASS2){ 2182 if(s->flags&CODEC_FLAG_PASS2){
2183 for(i=0; i<s->max_b_frames+1; i++){ 2183 for(i=0; i<s->max_b_frames+1; i++){
2184 int pict_num= s->input_picture[0]->display_picture_number + i; 2184 int pict_num= s->input_picture[0]->display_picture_number + i;
2185 2185
2186 if(pict_num >= s->rc_context.num_entries) 2186 if(pict_num >= s->rc_context.num_entries)
2187 break; 2187 break;
2188 if(!s->input_picture[i]){ 2188 if(!s->input_picture[i]){
2189 s->rc_context.entry[pict_num-1].new_pict_type = P_TYPE; 2189 s->rc_context.entry[pict_num-1].new_pict_type = P_TYPE;
2190 break; 2190 break;
2191 } 2191 }
2192 2192
2193 s->input_picture[i]->pict_type= 2193 s->input_picture[i]->pict_type=
2194 s->rc_context.entry[pict_num].new_pict_type; 2194 s->rc_context.entry[pict_num].new_pict_type;
2195 } 2195 }
2196 } 2196 }
2197 2197
2198 if(s->avctx->b_frame_strategy==0){ 2198 if(s->avctx->b_frame_strategy==0){
2199 b_frames= s->max_b_frames; 2199 b_frames= s->max_b_frames;
2200 while(b_frames && !s->input_picture[b_frames]) b_frames--; 2200 while(b_frames && !s->input_picture[b_frames]) b_frames--;
2201 }else if(s->avctx->b_frame_strategy==1){ 2201 }else if(s->avctx->b_frame_strategy==1){
2202 for(i=1; i<s->max_b_frames+1; i++){ 2202 for(i=1; i<s->max_b_frames+1; i++){
2203 if(s->input_picture[i] && s->input_picture[i]->b_frame_score==0){ 2203 if(s->input_picture[i] && s->input_picture[i]->b_frame_score==0){
2204 s->input_picture[i]->b_frame_score= 2204 s->input_picture[i]->b_frame_score=
2205 get_intra_count(s, s->input_picture[i ]->data[0], 2205 get_intra_count(s, s->input_picture[i ]->data[0],
2206 s->input_picture[i-1]->data[0], s->linesize) + 1; 2206 s->input_picture[i-1]->data[0], s->linesize) + 1;
2207 } 2207 }
2208 } 2208 }
2209 for(i=0; i<s->max_b_frames+1; i++){ 2209 for(i=0; i<s->max_b_frames+1; i++){
2210 if(s->input_picture[i]==NULL || s->input_picture[i]->b_frame_score - 1 > s->mb_num/40) break; 2210 if(s->input_picture[i]==NULL || s->input_picture[i]->b_frame_score - 1 > s->mb_num/40) break;
2211 } 2211 }
2212 2212
2213 b_frames= FFMAX(0, i-1); 2213 b_frames= FFMAX(0, i-1);
2214 2214
2215 /* reset scores */ 2215 /* reset scores */
2216 for(i=0; i<b_frames+1; i++){ 2216 for(i=0; i<b_frames+1; i++){
2217 s->input_picture[i]->b_frame_score=0; 2217 s->input_picture[i]->b_frame_score=0;
2218 } 2218 }
2219 }else{ 2219 }else{
2242 if(s->flags & CODEC_FLAG_CLOSED_GOP) 2242 if(s->flags & CODEC_FLAG_CLOSED_GOP)
2243 b_frames=0; 2243 b_frames=0;
2244 s->input_picture[b_frames]->pict_type= I_TYPE; 2244 s->input_picture[b_frames]->pict_type= I_TYPE;
2245 } 2245 }
2246 } 2246 }
2247 2247
2248 if( (s->flags & CODEC_FLAG_CLOSED_GOP) 2248 if( (s->flags & CODEC_FLAG_CLOSED_GOP)
2249 && b_frames 2249 && b_frames
2250 && s->input_picture[b_frames]->pict_type== I_TYPE) 2250 && s->input_picture[b_frames]->pict_type== I_TYPE)
2251 b_frames--; 2251 b_frames--;
2252 2252
2267 2267
2268 copy_picture(&s->new_picture, s->reordered_input_picture[0]); 2268 copy_picture(&s->new_picture, s->reordered_input_picture[0]);
2269 2269
2270 if(s->reordered_input_picture[0]->type == FF_BUFFER_TYPE_SHARED){ 2270 if(s->reordered_input_picture[0]->type == FF_BUFFER_TYPE_SHARED){
2271 // input is a shared pix, so we can't modifiy it -> alloc a new one & ensure that the shared one is reuseable 2271 // input is a shared pix, so we can't modifiy it -> alloc a new one & ensure that the shared one is reuseable
2272 2272
2273 int i= ff_find_unused_picture(s, 0); 2273 int i= ff_find_unused_picture(s, 0);
2274 Picture *pic= &s->picture[i]; 2274 Picture *pic= &s->picture[i];
2275 2275
2276 /* mark us unused / free shared pic */ 2276 /* mark us unused / free shared pic */
2277 for(i=0; i<4; i++) 2277 for(i=0; i<4; i++)
2278 s->reordered_input_picture[0]->data[i]= NULL; 2278 s->reordered_input_picture[0]->data[i]= NULL;
2279 s->reordered_input_picture[0]->type= 0; 2279 s->reordered_input_picture[0]->type= 0;
2280 2280
2281 pic->reference = s->reordered_input_picture[0]->reference; 2281 pic->reference = s->reordered_input_picture[0]->reference;
2282 2282
2283 alloc_picture(s, pic, 0); 2283 alloc_picture(s, pic, 0);
2284 2284
2285 copy_picture_attributes(s, (AVFrame*)pic, (AVFrame*)s->reordered_input_picture[0]); 2285 copy_picture_attributes(s, (AVFrame*)pic, (AVFrame*)s->reordered_input_picture[0]);
2286 2286
2287 s->current_picture_ptr= pic; 2287 s->current_picture_ptr= pic;
2288 }else{ 2288 }else{
2289 // input is not a shared pix -> reuse buffer for current_pix 2289 // input is not a shared pix -> reuse buffer for current_pix
2290 2290
2291 assert( s->reordered_input_picture[0]->type==FF_BUFFER_TYPE_USER 2291 assert( s->reordered_input_picture[0]->type==FF_BUFFER_TYPE_USER
2292 || s->reordered_input_picture[0]->type==FF_BUFFER_TYPE_INTERNAL); 2292 || s->reordered_input_picture[0]->type==FF_BUFFER_TYPE_INTERNAL);
2293 2293
2294 s->current_picture_ptr= s->reordered_input_picture[0]; 2294 s->current_picture_ptr= s->reordered_input_picture[0];
2295 for(i=0; i<4; i++){ 2295 for(i=0; i<4; i++){
2296 s->new_picture.data[i]+=16; 2296 s->new_picture.data[i]+=16;
2297 } 2297 }
2298 } 2298 }
2299 copy_picture(&s->current_picture, s->current_picture_ptr); 2299 copy_picture(&s->current_picture, s->current_picture_ptr);
2300 2300
2301 s->picture_number= s->new_picture.display_picture_number; 2301 s->picture_number= s->new_picture.display_picture_number;
2302 //printf("dpn:%d\n", s->picture_number); 2302 //printf("dpn:%d\n", s->picture_number);
2303 }else{ 2303 }else{
2304 memset(&s->new_picture, 0, sizeof(Picture)); 2304 memset(&s->new_picture, 0, sizeof(Picture));
2305 } 2305 }
2314 2314
2315 if(avctx->pix_fmt != PIX_FMT_YUV420P && avctx->pix_fmt != PIX_FMT_YUVJ420P){ 2315 if(avctx->pix_fmt != PIX_FMT_YUV420P && avctx->pix_fmt != PIX_FMT_YUVJ420P){
2316 av_log(avctx, AV_LOG_ERROR, "this codec supports only YUV420P\n"); 2316 av_log(avctx, AV_LOG_ERROR, "this codec supports only YUV420P\n");
2317 return -1; 2317 return -1;
2318 } 2318 }
2319 2319
2320 for(i=0; i<avctx->thread_count; i++){ 2320 for(i=0; i<avctx->thread_count; i++){
2321 int start_y= s->thread_context[i]->start_mb_y; 2321 int start_y= s->thread_context[i]->start_mb_y;
2322 int end_y= s->thread_context[i]-> end_mb_y; 2322 int end_y= s->thread_context[i]-> end_mb_y;
2323 int h= s->mb_height; 2323 int h= s->mb_height;
2324 uint8_t *start= buf + (size_t)(((int64_t) buf_size)*start_y/h); 2324 uint8_t *start= buf + (size_t)(((int64_t) buf_size)*start_y/h);
2329 2329
2330 s->picture_in_gop_number++; 2330 s->picture_in_gop_number++;
2331 2331
2332 if(load_input_picture(s, pic_arg) < 0) 2332 if(load_input_picture(s, pic_arg) < 0)
2333 return -1; 2333 return -1;
2334 2334
2335 select_input_picture(s); 2335 select_input_picture(s);
2336 2336
2337 /* output? */ 2337 /* output? */
2338 if(s->new_picture.data[0]){ 2338 if(s->new_picture.data[0]){
2339 s->pict_type= s->new_picture.pict_type; 2339 s->pict_type= s->new_picture.pict_type;
2340 //emms_c(); 2340 //emms_c();
2341 //printf("qs:%f %f %d\n", s->new_picture.quality, s->current_picture.quality, s->qscale); 2341 //printf("qs:%f %f %d\n", s->new_picture.quality, s->current_picture.quality, s->qscale);
2342 MPV_frame_start(s, avctx); 2342 MPV_frame_start(s, avctx);
2343 2343
2344 encode_picture(s, s->picture_number); 2344 encode_picture(s, s->picture_number);
2345 2345
2346 avctx->real_pict_num = s->picture_number; 2346 avctx->real_pict_num = s->picture_number;
2347 avctx->header_bits = s->header_bits; 2347 avctx->header_bits = s->header_bits;
2348 avctx->mv_bits = s->mv_bits; 2348 avctx->mv_bits = s->mv_bits;
2349 avctx->misc_bits = s->misc_bits; 2349 avctx->misc_bits = s->misc_bits;
2350 avctx->i_tex_bits = s->i_tex_bits; 2350 avctx->i_tex_bits = s->i_tex_bits;
2355 2355
2356 MPV_frame_end(s); 2356 MPV_frame_end(s);
2357 2357
2358 if (s->out_format == FMT_MJPEG) 2358 if (s->out_format == FMT_MJPEG)
2359 mjpeg_picture_trailer(s); 2359 mjpeg_picture_trailer(s);
2360 2360
2361 if(s->flags&CODEC_FLAG_PASS1) 2361 if(s->flags&CODEC_FLAG_PASS1)
2362 ff_write_pass1_stats(s); 2362 ff_write_pass1_stats(s);
2363 2363
2364 for(i=0; i<4; i++){ 2364 for(i=0; i<4; i++){
2365 s->current_picture_ptr->error[i]= s->current_picture.error[i]; 2365 s->current_picture_ptr->error[i]= s->current_picture.error[i];
2398 } 2398 }
2399 flush_put_bits(&s->pb); 2399 flush_put_bits(&s->pb);
2400 s->frame_bits = put_bits_count(&s->pb); 2400 s->frame_bits = put_bits_count(&s->pb);
2401 } 2401 }
2402 2402
2403 /* update mpeg1/2 vbv_delay for CBR */ 2403 /* update mpeg1/2 vbv_delay for CBR */
2404 if(s->avctx->rc_max_rate && s->avctx->rc_min_rate == s->avctx->rc_max_rate && s->out_format == FMT_MPEG1 2404 if(s->avctx->rc_max_rate && s->avctx->rc_min_rate == s->avctx->rc_max_rate && s->out_format == FMT_MPEG1
2405 && 90000LL * (avctx->rc_buffer_size-1) <= s->avctx->rc_max_rate*0xFFFFLL){ 2405 && 90000LL * (avctx->rc_buffer_size-1) <= s->avctx->rc_max_rate*0xFFFFLL){
2406 int vbv_delay; 2406 int vbv_delay;
2407 2407
2408 assert(s->repeat_first_field==0); 2408 assert(s->repeat_first_field==0);
2409 2409
2410 vbv_delay= lrintf(90000 * s->rc_context.buffer_index / s->avctx->rc_max_rate); 2410 vbv_delay= lrintf(90000 * s->rc_context.buffer_index / s->avctx->rc_max_rate);
2411 assert(vbv_delay < 0xFFFF); 2411 assert(vbv_delay < 0xFFFF);
2412 2412
2413 s->vbv_delay_ptr[0] &= 0xF8; 2413 s->vbv_delay_ptr[0] &= 0xF8;
2414 s->vbv_delay_ptr[0] |= vbv_delay>>13; 2414 s->vbv_delay_ptr[0] |= vbv_delay>>13;
2421 }else{ 2421 }else{
2422 assert((pbBufPtr(&s->pb) == s->pb.buf)); 2422 assert((pbBufPtr(&s->pb) == s->pb.buf));
2423 s->frame_bits=0; 2423 s->frame_bits=0;
2424 } 2424 }
2425 assert((s->frame_bits&7)==0); 2425 assert((s->frame_bits&7)==0);
2426 2426
2427 return s->frame_bits/8; 2427 return s->frame_bits/8;
2428 } 2428 }
2429 2429
2430 #endif //CONFIG_ENCODERS 2430 #endif //CONFIG_ENCODERS
2431 2431
2451 if (src_y == s->height) 2451 if (src_y == s->height)
2452 motion_y =0; 2452 motion_y =0;
2453 2453
2454 linesize = s->linesize; 2454 linesize = s->linesize;
2455 uvlinesize = s->uvlinesize; 2455 uvlinesize = s->uvlinesize;
2456 2456
2457 ptr = ref_picture[0] + (src_y * linesize) + src_x; 2457 ptr = ref_picture[0] + (src_y * linesize) + src_x;
2458 2458
2459 if(s->flags&CODEC_FLAG_EMU_EDGE){ 2459 if(s->flags&CODEC_FLAG_EMU_EDGE){
2460 if( (unsigned)src_x >= s->h_edge_pos - 17 2460 if( (unsigned)src_x >= s->h_edge_pos - 17
2461 || (unsigned)src_y >= s->v_edge_pos - 17){ 2461 || (unsigned)src_y >= s->v_edge_pos - 17){
2462 ff_emulated_edge_mc(s->edge_emu_buffer, ptr, linesize, 17, 17, src_x, src_y, s->h_edge_pos, s->v_edge_pos); 2462 ff_emulated_edge_mc(s->edge_emu_buffer, ptr, linesize, 17, 17, src_x, src_y, s->h_edge_pos, s->v_edge_pos);
2463 ptr= s->edge_emu_buffer; 2463 ptr= s->edge_emu_buffer;
2464 } 2464 }
2465 } 2465 }
2466 2466
2467 if((motion_x|motion_y)&7){ 2467 if((motion_x|motion_y)&7){
2468 s->dsp.gmc1(dest_y , ptr , linesize, 16, motion_x&15, motion_y&15, 128 - s->no_rounding); 2468 s->dsp.gmc1(dest_y , ptr , linesize, 16, motion_x&15, motion_y&15, 128 - s->no_rounding);
2469 s->dsp.gmc1(dest_y+8, ptr+8, linesize, 16, motion_x&15, motion_y&15, 128 - s->no_rounding); 2469 s->dsp.gmc1(dest_y+8, ptr+8, linesize, 16, motion_x&15, motion_y&15, 128 - s->no_rounding);
2470 }else{ 2470 }else{
2471 int dxy; 2471 int dxy;
2472 2472
2473 dxy= ((motion_x>>3)&1) | ((motion_y>>2)&2); 2473 dxy= ((motion_x>>3)&1) | ((motion_y>>2)&2);
2474 if (s->no_rounding){ 2474 if (s->no_rounding){
2475 s->dsp.put_no_rnd_pixels_tab[0][dxy](dest_y, ptr, linesize, 16); 2475 s->dsp.put_no_rnd_pixels_tab[0][dxy](dest_y, ptr, linesize, 16);
2476 }else{ 2476 }else{
2477 s->dsp.put_pixels_tab [0][dxy](dest_y, ptr, linesize, 16); 2477 s->dsp.put_pixels_tab [0][dxy](dest_y, ptr, linesize, 16);
2478 } 2478 }
2479 } 2479 }
2480 2480
2481 if(s->flags&CODEC_FLAG_GRAY) return; 2481 if(s->flags&CODEC_FLAG_GRAY) return;
2482 2482
2483 motion_x= s->sprite_offset[1][0]; 2483 motion_x= s->sprite_offset[1][0];
2484 motion_y= s->sprite_offset[1][1]; 2484 motion_y= s->sprite_offset[1][1];
2485 src_x = s->mb_x * 8 + (motion_x >> (s->sprite_warping_accuracy+1)); 2485 src_x = s->mb_x * 8 + (motion_x >> (s->sprite_warping_accuracy+1));
2502 ptr= s->edge_emu_buffer; 2502 ptr= s->edge_emu_buffer;
2503 emu=1; 2503 emu=1;
2504 } 2504 }
2505 } 2505 }
2506 s->dsp.gmc1(dest_cb, ptr, uvlinesize, 8, motion_x&15, motion_y&15, 128 - s->no_rounding); 2506 s->dsp.gmc1(dest_cb, ptr, uvlinesize, 8, motion_x&15, motion_y&15, 128 - s->no_rounding);
2507 2507
2508 ptr = ref_picture[2] + offset; 2508 ptr = ref_picture[2] + offset;
2509 if(emu){ 2509 if(emu){
2510 ff_emulated_edge_mc(s->edge_emu_buffer, ptr, uvlinesize, 9, 9, src_x, src_y, s->h_edge_pos>>1, s->v_edge_pos>>1); 2510 ff_emulated_edge_mc(s->edge_emu_buffer, ptr, uvlinesize, 9, 9, src_x, src_y, s->h_edge_pos>>1, s->v_edge_pos>>1);
2511 ptr= s->edge_emu_buffer; 2511 ptr= s->edge_emu_buffer;
2512 } 2512 }
2513 s->dsp.gmc1(dest_cr, ptr, uvlinesize, 8, motion_x&15, motion_y&15, 128 - s->no_rounding); 2513 s->dsp.gmc1(dest_cr, ptr, uvlinesize, 8, motion_x&15, motion_y&15, 128 - s->no_rounding);
2514 2514
2515 return; 2515 return;
2516 } 2516 }
2517 2517
2518 static inline void gmc_motion(MpegEncContext *s, 2518 static inline void gmc_motion(MpegEncContext *s,
2519 uint8_t *dest_y, uint8_t *dest_cb, uint8_t *dest_cr, 2519 uint8_t *dest_y, uint8_t *dest_cb, uint8_t *dest_cr,
2531 2531
2532 ox= s->sprite_offset[0][0] + s->sprite_delta[0][0]*s->mb_x*16 + s->sprite_delta[0][1]*s->mb_y*16; 2532 ox= s->sprite_offset[0][0] + s->sprite_delta[0][0]*s->mb_x*16 + s->sprite_delta[0][1]*s->mb_y*16;
2533 oy= s->sprite_offset[0][1] + s->sprite_delta[1][0]*s->mb_x*16 + s->sprite_delta[1][1]*s->mb_y*16; 2533 oy= s->sprite_offset[0][1] + s->sprite_delta[1][0]*s->mb_x*16 + s->sprite_delta[1][1]*s->mb_y*16;
2534 2534
2535 s->dsp.gmc(dest_y, ptr, linesize, 16, 2535 s->dsp.gmc(dest_y, ptr, linesize, 16,
2536 ox, 2536 ox,
2537 oy, 2537 oy,
2538 s->sprite_delta[0][0], s->sprite_delta[0][1], 2538 s->sprite_delta[0][0], s->sprite_delta[0][1],
2539 s->sprite_delta[1][0], s->sprite_delta[1][1], 2539 s->sprite_delta[1][0], s->sprite_delta[1][1],
2540 a+1, (1<<(2*a+1)) - s->no_rounding, 2540 a+1, (1<<(2*a+1)) - s->no_rounding,
2541 s->h_edge_pos, s->v_edge_pos); 2541 s->h_edge_pos, s->v_edge_pos);
2542 s->dsp.gmc(dest_y+8, ptr, linesize, 16, 2542 s->dsp.gmc(dest_y+8, ptr, linesize, 16,
2543 ox + s->sprite_delta[0][0]*8, 2543 ox + s->sprite_delta[0][0]*8,
2544 oy + s->sprite_delta[1][0]*8, 2544 oy + s->sprite_delta[1][0]*8,
2545 s->sprite_delta[0][0], s->sprite_delta[0][1], 2545 s->sprite_delta[0][0], s->sprite_delta[0][1],
2546 s->sprite_delta[1][0], s->sprite_delta[1][1], 2546 s->sprite_delta[1][0], s->sprite_delta[1][1],
2547 a+1, (1<<(2*a+1)) - s->no_rounding, 2547 a+1, (1<<(2*a+1)) - s->no_rounding,
2548 s->h_edge_pos, s->v_edge_pos); 2548 s->h_edge_pos, s->v_edge_pos);
2549 2549
2550 if(s->flags&CODEC_FLAG_GRAY) return; 2550 if(s->flags&CODEC_FLAG_GRAY) return;
2551 2551
2552 ox= s->sprite_offset[1][0] + s->sprite_delta[0][0]*s->mb_x*8 + s->sprite_delta[0][1]*s->mb_y*8; 2552 ox= s->sprite_offset[1][0] + s->sprite_delta[0][0]*s->mb_x*8 + s->sprite_delta[0][1]*s->mb_y*8;
2553 oy= s->sprite_offset[1][1] + s->sprite_delta[1][0]*s->mb_x*8 + s->sprite_delta[1][1]*s->mb_y*8; 2553 oy= s->sprite_offset[1][1] + s->sprite_delta[1][0]*s->mb_x*8 + s->sprite_delta[1][1]*s->mb_y*8;
2554 2554
2555 ptr = ref_picture[1]; 2555 ptr = ref_picture[1];
2556 s->dsp.gmc(dest_cb, ptr, uvlinesize, 8, 2556 s->dsp.gmc(dest_cb, ptr, uvlinesize, 8,
2557 ox, 2557 ox,
2558 oy, 2558 oy,
2559 s->sprite_delta[0][0], s->sprite_delta[0][1], 2559 s->sprite_delta[0][0], s->sprite_delta[0][1],
2560 s->sprite_delta[1][0], s->sprite_delta[1][1], 2560 s->sprite_delta[1][0], s->sprite_delta[1][1],
2561 a+1, (1<<(2*a+1)) - s->no_rounding, 2561 a+1, (1<<(2*a+1)) - s->no_rounding,
2562 s->h_edge_pos>>1, s->v_edge_pos>>1); 2562 s->h_edge_pos>>1, s->v_edge_pos>>1);
2563 2563
2564 ptr = ref_picture[2]; 2564 ptr = ref_picture[2];
2565 s->dsp.gmc(dest_cr, ptr, uvlinesize, 8, 2565 s->dsp.gmc(dest_cr, ptr, uvlinesize, 8,
2566 ox, 2566 ox,
2567 oy, 2567 oy,
2568 s->sprite_delta[0][0], s->sprite_delta[0][1], 2568 s->sprite_delta[0][0], s->sprite_delta[0][1],
2569 s->sprite_delta[1][0], s->sprite_delta[1][1], 2569 s->sprite_delta[1][0], s->sprite_delta[1][1],
2570 a+1, (1<<(2*a+1)) - s->no_rounding, 2570 a+1, (1<<(2*a+1)) - s->no_rounding,
2571 s->h_edge_pos>>1, s->v_edge_pos>>1); 2571 s->h_edge_pos>>1, s->v_edge_pos>>1);
2572 } 2572 }
2573 2573
2574 /** 2574 /**
2581 * @param src_x x coordinate of the top left sample of the block in the source buffer 2581 * @param src_x x coordinate of the top left sample of the block in the source buffer
2582 * @param src_y y coordinate of the top left sample of the block in the source buffer 2582 * @param src_y y coordinate of the top left sample of the block in the source buffer
2583 * @param w width of the source buffer 2583 * @param w width of the source buffer
2584 * @param h height of the source buffer 2584 * @param h height of the source buffer
2585 */ 2585 */
2586 void ff_emulated_edge_mc(uint8_t *buf, uint8_t *src, int linesize, int block_w, int block_h, 2586 void ff_emulated_edge_mc(uint8_t *buf, uint8_t *src, int linesize, int block_w, int block_h,
2587 int src_x, int src_y, int w, int h){ 2587 int src_x, int src_y, int w, int h){
2588 int x, y; 2588 int x, y;
2589 int start_y, start_x, end_y, end_x; 2589 int start_y, start_x, end_y, end_x;
2590 2590
2591 if(src_y>= h){ 2591 if(src_y>= h){
2626 for(y=end_y; y<block_h; y++){ 2626 for(y=end_y; y<block_h; y++){
2627 for(x=start_x; x<end_x; x++){ 2627 for(x=start_x; x<end_x; x++){
2628 buf[x + y*linesize]= buf[x + (end_y-1)*linesize]; 2628 buf[x + y*linesize]= buf[x + (end_y-1)*linesize];
2629 } 2629 }
2630 } 2630 }
2631 2631
2632 for(y=0; y<block_h; y++){ 2632 for(y=0; y<block_h; y++){
2633 //left 2633 //left
2634 for(x=0; x<start_x; x++){ 2634 for(x=0; x<start_x; x++){
2635 buf[x + y*linesize]= buf[start_x + y*linesize]; 2635 buf[x + y*linesize]= buf[start_x + y*linesize];
2636 } 2636 }
2637 2637
2638 //right 2638 //right
2639 for(x=end_x; x<block_w; x++){ 2639 for(x=end_x; x<block_w; x++){
2640 buf[x + y*linesize]= buf[end_x - 1 + y*linesize]; 2640 buf[x + y*linesize]= buf[end_x - 1 + y*linesize];
2641 } 2641 }
2642 } 2642 }
2643 } 2643 }
2644 2644
2645 static inline int hpel_motion(MpegEncContext *s, 2645 static inline int hpel_motion(MpegEncContext *s,
2646 uint8_t *dest, uint8_t *src, 2646 uint8_t *dest, uint8_t *src,
2647 int field_based, int field_select, 2647 int field_based, int field_select,
2648 int src_x, int src_y, 2648 int src_x, int src_y,
2649 int width, int height, int stride, 2649 int width, int height, int stride,
2650 int h_edge_pos, int v_edge_pos, 2650 int h_edge_pos, int v_edge_pos,
2655 int emu=0; 2655 int emu=0;
2656 2656
2657 dxy = ((motion_y & 1) << 1) | (motion_x & 1); 2657 dxy = ((motion_y & 1) << 1) | (motion_x & 1);
2658 src_x += motion_x >> 1; 2658 src_x += motion_x >> 1;
2659 src_y += motion_y >> 1; 2659 src_y += motion_y >> 1;
2660 2660
2661 /* WARNING: do no forget half pels */ 2661 /* WARNING: do no forget half pels */
2662 src_x = clip(src_x, -16, width); //FIXME unneeded for emu? 2662 src_x = clip(src_x, -16, width); //FIXME unneeded for emu?
2663 if (src_x == width) 2663 if (src_x == width)
2664 dxy &= ~1; 2664 dxy &= ~1;
2665 src_y = clip(src_y, -16, height); 2665 src_y = clip(src_y, -16, height);
2680 src += s->linesize; 2680 src += s->linesize;
2681 pix_op[dxy](dest, src, stride, h); 2681 pix_op[dxy](dest, src, stride, h);
2682 return emu; 2682 return emu;
2683 } 2683 }
2684 2684
2685 static inline int hpel_motion_lowres(MpegEncContext *s, 2685 static inline int hpel_motion_lowres(MpegEncContext *s,
2686 uint8_t *dest, uint8_t *src, 2686 uint8_t *dest, uint8_t *src,
2687 int field_based, int field_select, 2687 int field_based, int field_select,
2688 int src_x, int src_y, 2688 int src_x, int src_y,
2689 int width, int height, int stride, 2689 int width, int height, int stride,
2690 int h_edge_pos, int v_edge_pos, 2690 int h_edge_pos, int v_edge_pos,
2703 2703
2704 sx= motion_x & s_mask; 2704 sx= motion_x & s_mask;
2705 sy= motion_y & s_mask; 2705 sy= motion_y & s_mask;
2706 src_x += motion_x >> (lowres+1); 2706 src_x += motion_x >> (lowres+1);
2707 src_y += motion_y >> (lowres+1); 2707 src_y += motion_y >> (lowres+1);
2708 2708
2709 src += src_y * stride + src_x; 2709 src += src_y * stride + src_x;
2710 2710
2711 if( (unsigned)src_x > h_edge_pos - (!!sx) - w 2711 if( (unsigned)src_x > h_edge_pos - (!!sx) - w
2712 || (unsigned)src_y >(v_edge_pos >> field_based) - (!!sy) - h){ 2712 || (unsigned)src_y >(v_edge_pos >> field_based) - (!!sy) - h){
2713 ff_emulated_edge_mc(s->edge_emu_buffer, src, s->linesize, w+1, (h+1)<<field_based, 2713 ff_emulated_edge_mc(s->edge_emu_buffer, src, s->linesize, w+1, (h+1)<<field_based,
2731 uint8_t **ref_picture, op_pixels_func (*pix_op)[4], 2731 uint8_t **ref_picture, op_pixels_func (*pix_op)[4],
2732 int motion_x, int motion_y, int h) 2732 int motion_x, int motion_y, int h)
2733 { 2733 {
2734 uint8_t *ptr_y, *ptr_cb, *ptr_cr; 2734 uint8_t *ptr_y, *ptr_cb, *ptr_cr;
2735 int dxy, uvdxy, mx, my, src_x, src_y, uvsrc_x, uvsrc_y, v_edge_pos, uvlinesize, linesize; 2735 int dxy, uvdxy, mx, my, src_x, src_y, uvsrc_x, uvsrc_y, v_edge_pos, uvlinesize, linesize;
2736 2736
2737 #if 0 2737 #if 0
2738 if(s->quarter_sample) 2738 if(s->quarter_sample)
2739 { 2739 {
2740 motion_x>>=1; 2740 motion_x>>=1;
2741 motion_y>>=1; 2741 motion_y>>=1;
2742 } 2742 }
2805 ff_emulated_edge_mc(s->edge_emu_buffer, ptr_y, s->linesize, 17, 17+field_based, 2805 ff_emulated_edge_mc(s->edge_emu_buffer, ptr_y, s->linesize, 17, 17+field_based,
2806 src_x, src_y<<field_based, s->h_edge_pos, s->v_edge_pos); 2806 src_x, src_y<<field_based, s->h_edge_pos, s->v_edge_pos);
2807 ptr_y = s->edge_emu_buffer; 2807 ptr_y = s->edge_emu_buffer;
2808 if(!(s->flags&CODEC_FLAG_GRAY)){ 2808 if(!(s->flags&CODEC_FLAG_GRAY)){
2809 uint8_t *uvbuf= s->edge_emu_buffer+18*s->linesize; 2809 uint8_t *uvbuf= s->edge_emu_buffer+18*s->linesize;
2810 ff_emulated_edge_mc(uvbuf , ptr_cb, s->uvlinesize, 9, 9+field_based, 2810 ff_emulated_edge_mc(uvbuf , ptr_cb, s->uvlinesize, 9, 9+field_based,
2811 uvsrc_x, uvsrc_y<<field_based, s->h_edge_pos>>1, s->v_edge_pos>>1); 2811 uvsrc_x, uvsrc_y<<field_based, s->h_edge_pos>>1, s->v_edge_pos>>1);
2812 ff_emulated_edge_mc(uvbuf+16, ptr_cr, s->uvlinesize, 9, 9+field_based, 2812 ff_emulated_edge_mc(uvbuf+16, ptr_cr, s->uvlinesize, 9, 9+field_based,
2813 uvsrc_x, uvsrc_y<<field_based, s->h_edge_pos>>1, s->v_edge_pos>>1); 2813 uvsrc_x, uvsrc_y<<field_based, s->h_edge_pos>>1, s->v_edge_pos>>1);
2814 ptr_cb= uvbuf; 2814 ptr_cb= uvbuf;
2815 ptr_cr= uvbuf+16; 2815 ptr_cr= uvbuf+16;
2816 } 2816 }
2817 } 2817 }
2827 ptr_cb+= s->uvlinesize; 2827 ptr_cb+= s->uvlinesize;
2828 ptr_cr+= s->uvlinesize; 2828 ptr_cr+= s->uvlinesize;
2829 } 2829 }
2830 2830
2831 pix_op[0][dxy](dest_y, ptr_y, linesize, h); 2831 pix_op[0][dxy](dest_y, ptr_y, linesize, h);
2832 2832
2833 if(!(s->flags&CODEC_FLAG_GRAY)){ 2833 if(!(s->flags&CODEC_FLAG_GRAY)){
2834 pix_op[s->chroma_x_shift][uvdxy](dest_cb, ptr_cb, uvlinesize, h >> s->chroma_y_shift); 2834 pix_op[s->chroma_x_shift][uvdxy](dest_cb, ptr_cb, uvlinesize, h >> s->chroma_y_shift);
2835 pix_op[s->chroma_x_shift][uvdxy](dest_cr, ptr_cr, uvlinesize, h >> s->chroma_y_shift); 2835 pix_op[s->chroma_x_shift][uvdxy](dest_cr, ptr_cr, uvlinesize, h >> s->chroma_y_shift);
2836 } 2836 }
2837 #if defined(CONFIG_H261_ENCODER) || defined(CONFIG_H261_DECODER) 2837 #if defined(CONFIG_H261_ENCODER) || defined(CONFIG_H261_DECODER)
2860 2860
2861 if(s->quarter_sample){ //FIXME obviously not perfect but qpel wont work in lowres anyway 2861 if(s->quarter_sample){ //FIXME obviously not perfect but qpel wont work in lowres anyway
2862 motion_x/=2; 2862 motion_x/=2;
2863 motion_y/=2; 2863 motion_y/=2;
2864 } 2864 }
2865 2865
2866 if(field_based){ 2866 if(field_based){
2867 motion_y += (bottom_field - field_select)*((1<<lowres)-1); 2867 motion_y += (bottom_field - field_select)*((1<<lowres)-1);
2868 } 2868 }
2869 2869
2870 sx= motion_x & s_mask; 2870 sx= motion_x & s_mask;
2871 sy= motion_y & s_mask; 2871 sy= motion_y & s_mask;
2872 src_x = s->mb_x*2*block_s + (motion_x >> (lowres+1)); 2872 src_x = s->mb_x*2*block_s + (motion_x >> (lowres+1));
2873 src_y =(s->mb_y*2*block_s>>field_based) + (motion_y >> (lowres+1)); 2873 src_y =(s->mb_y*2*block_s>>field_based) + (motion_y >> (lowres+1));
2874 2874
2875 if (s->out_format == FMT_H263) { 2875 if (s->out_format == FMT_H263) {
2876 uvsx = ((motion_x>>1) & s_mask) | (sx&1); 2876 uvsx = ((motion_x>>1) & s_mask) | (sx&1);
2877 uvsy = ((motion_y>>1) & s_mask) | (sy&1); 2877 uvsy = ((motion_y>>1) & s_mask) | (sy&1);
2878 uvsrc_x = src_x>>1; 2878 uvsrc_x = src_x>>1;
2879 uvsrc_y = src_y>>1; 2879 uvsrc_y = src_y>>1;
2902 ff_emulated_edge_mc(s->edge_emu_buffer, ptr_y, s->linesize, 17, 17+field_based, 2902 ff_emulated_edge_mc(s->edge_emu_buffer, ptr_y, s->linesize, 17, 17+field_based,
2903 src_x, src_y<<field_based, h_edge_pos, v_edge_pos); 2903 src_x, src_y<<field_based, h_edge_pos, v_edge_pos);
2904 ptr_y = s->edge_emu_buffer; 2904 ptr_y = s->edge_emu_buffer;
2905 if(!(s->flags&CODEC_FLAG_GRAY)){ 2905 if(!(s->flags&CODEC_FLAG_GRAY)){
2906 uint8_t *uvbuf= s->edge_emu_buffer+18*s->linesize; 2906 uint8_t *uvbuf= s->edge_emu_buffer+18*s->linesize;
2907 ff_emulated_edge_mc(uvbuf , ptr_cb, s->uvlinesize, 9, 9+field_based, 2907 ff_emulated_edge_mc(uvbuf , ptr_cb, s->uvlinesize, 9, 9+field_based,
2908 uvsrc_x, uvsrc_y<<field_based, h_edge_pos>>1, v_edge_pos>>1); 2908 uvsrc_x, uvsrc_y<<field_based, h_edge_pos>>1, v_edge_pos>>1);
2909 ff_emulated_edge_mc(uvbuf+16, ptr_cr, s->uvlinesize, 9, 9+field_based, 2909 ff_emulated_edge_mc(uvbuf+16, ptr_cr, s->uvlinesize, 9, 9+field_based,
2910 uvsrc_x, uvsrc_y<<field_based, h_edge_pos>>1, v_edge_pos>>1); 2910 uvsrc_x, uvsrc_y<<field_based, h_edge_pos>>1, v_edge_pos>>1);
2911 ptr_cb= uvbuf; 2911 ptr_cb= uvbuf;
2912 ptr_cr= uvbuf+16; 2912 ptr_cr= uvbuf+16;
2913 } 2913 }
2914 } 2914 }
2926 } 2926 }
2927 2927
2928 sx <<= 2 - lowres; 2928 sx <<= 2 - lowres;
2929 sy <<= 2 - lowres; 2929 sy <<= 2 - lowres;
2930 pix_op[lowres-1](dest_y, ptr_y, linesize, h, sx, sy); 2930 pix_op[lowres-1](dest_y, ptr_y, linesize, h, sx, sy);
2931 2931
2932 if(!(s->flags&CODEC_FLAG_GRAY)){ 2932 if(!(s->flags&CODEC_FLAG_GRAY)){
2933 uvsx <<= 2 - lowres; 2933 uvsx <<= 2 - lowres;
2934 uvsy <<= 2 - lowres; 2934 uvsy <<= 2 - lowres;
2935 pix_op[lowres](dest_cb, ptr_cb, uvlinesize, h >> s->chroma_y_shift, uvsx, uvsy); 2935 pix_op[lowres](dest_cb, ptr_cb, uvlinesize, h >> s->chroma_y_shift, uvsx, uvsy);
2936 pix_op[lowres](dest_cr, ptr_cr, uvlinesize, h >> s->chroma_y_shift, uvsx, uvsy); 2936 pix_op[lowres](dest_cr, ptr_cr, uvlinesize, h >> s->chroma_y_shift, uvsx, uvsy);
2951 #define OBMC_FILTER4(x, t, l, m, r, b)\ 2951 #define OBMC_FILTER4(x, t, l, m, r, b)\
2952 OBMC_FILTER(x , t, l, m, r, b);\ 2952 OBMC_FILTER(x , t, l, m, r, b);\
2953 OBMC_FILTER(x+1 , t, l, m, r, b);\ 2953 OBMC_FILTER(x+1 , t, l, m, r, b);\
2954 OBMC_FILTER(x +stride, t, l, m, r, b);\ 2954 OBMC_FILTER(x +stride, t, l, m, r, b);\
2955 OBMC_FILTER(x+1+stride, t, l, m, r, b); 2955 OBMC_FILTER(x+1+stride, t, l, m, r, b);
2956 2956
2957 x=0; 2957 x=0;
2958 OBMC_FILTER (x , 2, 2, 4, 0, 0); 2958 OBMC_FILTER (x , 2, 2, 4, 0, 0);
2959 OBMC_FILTER (x+1, 2, 1, 5, 0, 0); 2959 OBMC_FILTER (x+1, 2, 1, 5, 0, 0);
2960 OBMC_FILTER4(x+2, 2, 1, 5, 0, 0); 2960 OBMC_FILTER4(x+2, 2, 1, 5, 0, 0);
2961 OBMC_FILTER4(x+4, 2, 0, 5, 1, 0); 2961 OBMC_FILTER4(x+4, 2, 0, 5, 1, 0);
2998 int16_t mv[5][2]/* mid top left right bottom*/) 2998 int16_t mv[5][2]/* mid top left right bottom*/)
2999 #define MID 0 2999 #define MID 0
3000 { 3000 {
3001 int i; 3001 int i;
3002 uint8_t *ptr[5]; 3002 uint8_t *ptr[5];
3003 3003
3004 assert(s->quarter_sample==0); 3004 assert(s->quarter_sample==0);
3005 3005
3006 for(i=0; i<5; i++){ 3006 for(i=0; i<5; i++){
3007 if(i && mv[i][0]==mv[MID][0] && mv[i][1]==mv[MID][1]){ 3007 if(i && mv[i][0]==mv[MID][0] && mv[i][1]==mv[MID][1]){
3008 ptr[i]= ptr[MID]; 3008 ptr[i]= ptr[MID];
3009 }else{ 3009 }else{
3010 ptr[i]= s->obmc_scratchpad + 8*(i&1) + s->linesize*8*(i>>1); 3010 ptr[i]= s->obmc_scratchpad + 8*(i&1) + s->linesize*8*(i>>1);
3015 8, 8, pix_op, 3015 8, 8, pix_op,
3016 mv[i][0], mv[i][1]); 3016 mv[i][0], mv[i][1]);
3017 } 3017 }
3018 } 3018 }
3019 3019
3020 put_obmc(dest, ptr, s->linesize); 3020 put_obmc(dest, ptr, s->linesize);
3021 } 3021 }
3022 3022
3023 static inline void qpel_motion(MpegEncContext *s, 3023 static inline void qpel_motion(MpegEncContext *s,
3024 uint8_t *dest_y, uint8_t *dest_cb, uint8_t *dest_cr, 3024 uint8_t *dest_y, uint8_t *dest_cb, uint8_t *dest_cr,
3025 int field_based, int bottom_field, int field_select, 3025 int field_based, int bottom_field, int field_select,
3035 src_y = s->mb_y * (16 >> field_based) + (motion_y >> 2); 3035 src_y = s->mb_y * (16 >> field_based) + (motion_y >> 2);
3036 3036
3037 v_edge_pos = s->v_edge_pos >> field_based; 3037 v_edge_pos = s->v_edge_pos >> field_based;
3038 linesize = s->linesize << field_based; 3038 linesize = s->linesize << field_based;
3039 uvlinesize = s->uvlinesize << field_based; 3039 uvlinesize = s->uvlinesize << field_based;
3040 3040
3041 if(field_based){ 3041 if(field_based){
3042 mx= motion_x/2; 3042 mx= motion_x/2;
3043 my= motion_y>>1; 3043 my= motion_y>>1;
3044 }else if(s->workaround_bugs&FF_BUG_QPEL_CHROMA2){ 3044 }else if(s->workaround_bugs&FF_BUG_QPEL_CHROMA2){
3045 static const int rtab[8]= {0,0,1,1,0,0,0,1}; 3045 static const int rtab[8]= {0,0,1,1,0,0,0,1};
3064 3064
3065 ptr_y = ref_picture[0] + src_y * linesize + src_x; 3065 ptr_y = ref_picture[0] + src_y * linesize + src_x;
3066 ptr_cb = ref_picture[1] + uvsrc_y * uvlinesize + uvsrc_x; 3066 ptr_cb = ref_picture[1] + uvsrc_y * uvlinesize + uvsrc_x;
3067 ptr_cr = ref_picture[2] + uvsrc_y * uvlinesize + uvsrc_x; 3067 ptr_cr = ref_picture[2] + uvsrc_y * uvlinesize + uvsrc_x;
3068 3068
3069 if( (unsigned)src_x > s->h_edge_pos - (motion_x&3) - 16 3069 if( (unsigned)src_x > s->h_edge_pos - (motion_x&3) - 16
3070 || (unsigned)src_y > v_edge_pos - (motion_y&3) - h ){ 3070 || (unsigned)src_y > v_edge_pos - (motion_y&3) - h ){
3071 ff_emulated_edge_mc(s->edge_emu_buffer, ptr_y, s->linesize, 17, 17+field_based, 3071 ff_emulated_edge_mc(s->edge_emu_buffer, ptr_y, s->linesize, 17, 17+field_based,
3072 src_x, src_y<<field_based, s->h_edge_pos, s->v_edge_pos); 3072 src_x, src_y<<field_based, s->h_edge_pos, s->v_edge_pos);
3073 ptr_y= s->edge_emu_buffer; 3073 ptr_y= s->edge_emu_buffer;
3074 if(!(s->flags&CODEC_FLAG_GRAY)){ 3074 if(!(s->flags&CODEC_FLAG_GRAY)){
3075 uint8_t *uvbuf= s->edge_emu_buffer + 18*s->linesize; 3075 uint8_t *uvbuf= s->edge_emu_buffer + 18*s->linesize;
3076 ff_emulated_edge_mc(uvbuf, ptr_cb, s->uvlinesize, 9, 9 + field_based, 3076 ff_emulated_edge_mc(uvbuf, ptr_cb, s->uvlinesize, 9, 9 + field_based,
3077 uvsrc_x, uvsrc_y<<field_based, s->h_edge_pos>>1, s->v_edge_pos>>1); 3077 uvsrc_x, uvsrc_y<<field_based, s->h_edge_pos>>1, s->v_edge_pos>>1);
3078 ff_emulated_edge_mc(uvbuf + 16, ptr_cr, s->uvlinesize, 9, 9 + field_based, 3078 ff_emulated_edge_mc(uvbuf + 16, ptr_cr, s->uvlinesize, 9, 9 + field_based,
3079 uvsrc_x, uvsrc_y<<field_based, s->h_edge_pos>>1, s->v_edge_pos>>1); 3079 uvsrc_x, uvsrc_y<<field_based, s->h_edge_pos>>1, s->v_edge_pos>>1);
3080 ptr_cb= uvbuf; 3080 ptr_cb= uvbuf;
3081 ptr_cr= uvbuf + 16; 3081 ptr_cr= uvbuf + 16;
3082 } 3082 }
3083 } 3083 }
3124 uint8_t **ref_picture, 3124 uint8_t **ref_picture,
3125 op_pixels_func *pix_op, 3125 op_pixels_func *pix_op,
3126 int mx, int my){ 3126 int mx, int my){
3127 int dxy, emu=0, src_x, src_y, offset; 3127 int dxy, emu=0, src_x, src_y, offset;
3128 uint8_t *ptr; 3128 uint8_t *ptr;
3129 3129
3130 /* In case of 8X8, we construct a single chroma motion vector 3130 /* In case of 8X8, we construct a single chroma motion vector
3131 with a special rounding */ 3131 with a special rounding */
3132 mx= ff_h263_round_chroma(mx); 3132 mx= ff_h263_round_chroma(mx);
3133 my= ff_h263_round_chroma(my); 3133 my= ff_h263_round_chroma(my);
3134 3134
3135 dxy = ((my & 1) << 1) | (mx & 1); 3135 dxy = ((my & 1) << 1) | (mx & 1);
3136 mx >>= 1; 3136 mx >>= 1;
3137 my >>= 1; 3137 my >>= 1;
3138 3138
3139 src_x = s->mb_x * 8 + mx; 3139 src_x = s->mb_x * 8 + mx;
3142 if (src_x == s->width/2) 3142 if (src_x == s->width/2)
3143 dxy &= ~1; 3143 dxy &= ~1;
3144 src_y = clip(src_y, -8, s->height/2); 3144 src_y = clip(src_y, -8, s->height/2);
3145 if (src_y == s->height/2) 3145 if (src_y == s->height/2)
3146 dxy &= ~2; 3146 dxy &= ~2;
3147 3147
3148 offset = (src_y * (s->uvlinesize)) + src_x; 3148 offset = (src_y * (s->uvlinesize)) + src_x;
3149 ptr = ref_picture[1] + offset; 3149 ptr = ref_picture[1] + offset;
3150 if(s->flags&CODEC_FLAG_EMU_EDGE){ 3150 if(s->flags&CODEC_FLAG_EMU_EDGE){
3151 if( (unsigned)src_x > (s->h_edge_pos>>1) - (dxy &1) - 8 3151 if( (unsigned)src_x > (s->h_edge_pos>>1) - (dxy &1) - 8
3152 || (unsigned)src_y > (s->v_edge_pos>>1) - (dxy>>1) - 8){ 3152 || (unsigned)src_y > (s->v_edge_pos>>1) - (dxy>>1) - 8){
3175 const int s_mask= (2<<lowres)-1; 3175 const int s_mask= (2<<lowres)-1;
3176 const int h_edge_pos = s->h_edge_pos >> (lowres+1); 3176 const int h_edge_pos = s->h_edge_pos >> (lowres+1);
3177 const int v_edge_pos = s->v_edge_pos >> (lowres+1); 3177 const int v_edge_pos = s->v_edge_pos >> (lowres+1);
3178 int emu=0, src_x, src_y, offset, sx, sy; 3178 int emu=0, src_x, src_y, offset, sx, sy;
3179 uint8_t *ptr; 3179 uint8_t *ptr;
3180 3180
3181 if(s->quarter_sample){ 3181 if(s->quarter_sample){
3182 mx/=2; 3182 mx/=2;
3183 my/=2; 3183 my/=2;
3184 } 3184 }
3185 3185
3186 /* In case of 8X8, we construct a single chroma motion vector 3186 /* In case of 8X8, we construct a single chroma motion vector
3187 with a special rounding */ 3187 with a special rounding */
3188 mx= ff_h263_round_chroma(mx); 3188 mx= ff_h263_round_chroma(mx);
3189 my= ff_h263_round_chroma(my); 3189 my= ff_h263_round_chroma(my);
3190 3190
3191 sx= mx & s_mask; 3191 sx= mx & s_mask;
3192 sy= my & s_mask; 3192 sy= my & s_mask;
3193 src_x = s->mb_x*block_s + (mx >> (lowres+1)); 3193 src_x = s->mb_x*block_s + (mx >> (lowres+1));
3194 src_y = s->mb_y*block_s + (my >> (lowres+1)); 3194 src_y = s->mb_y*block_s + (my >> (lowres+1));
3195 3195
3196 offset = src_y * s->uvlinesize + src_x; 3196 offset = src_y * s->uvlinesize + src_x;
3197 ptr = ref_picture[1] + offset; 3197 ptr = ref_picture[1] + offset;
3198 if(s->flags&CODEC_FLAG_EMU_EDGE){ 3198 if(s->flags&CODEC_FLAG_EMU_EDGE){
3199 if( (unsigned)src_x > h_edge_pos - (!!sx) - block_s 3199 if( (unsigned)src_x > h_edge_pos - (!!sx) - block_s
3200 || (unsigned)src_y > v_edge_pos - (!!sy) - block_s){ 3200 || (unsigned)src_y > v_edge_pos - (!!sy) - block_s){
3201 ff_emulated_edge_mc(s->edge_emu_buffer, ptr, s->uvlinesize, 9, 9, src_x, src_y, h_edge_pos, v_edge_pos); 3201 ff_emulated_edge_mc(s->edge_emu_buffer, ptr, s->uvlinesize, 9, 9, src_x, src_y, h_edge_pos, v_edge_pos);
3202 ptr= s->edge_emu_buffer; 3202 ptr= s->edge_emu_buffer;
3203 emu=1; 3203 emu=1;
3204 } 3204 }
3205 } 3205 }
3206 sx <<= 2 - lowres; 3206 sx <<= 2 - lowres;
3207 sy <<= 2 - lowres; 3207 sy <<= 2 - lowres;
3208 pix_op[lowres](dest_cb, ptr, s->uvlinesize, block_s, sx, sy); 3208 pix_op[lowres](dest_cb, ptr, s->uvlinesize, block_s, sx, sy);
3209 3209
3210 ptr = ref_picture[2] + offset; 3210 ptr = ref_picture[2] + offset;
3211 if(emu){ 3211 if(emu){
3212 ff_emulated_edge_mc(s->edge_emu_buffer, ptr, s->uvlinesize, 9, 9, src_x, src_y, h_edge_pos, v_edge_pos); 3212 ff_emulated_edge_mc(s->edge_emu_buffer, ptr, s->uvlinesize, 9, 9, src_x, src_y, h_edge_pos, v_edge_pos);
3213 ptr= s->edge_emu_buffer; 3213 ptr= s->edge_emu_buffer;
3214 } 3214 }
3225 * @param ref_picture array[3] of pointers to the 3 planes of the reference picture 3225 * @param ref_picture array[3] of pointers to the 3 planes of the reference picture
3226 * @param pic_op halfpel motion compensation function (average or put normally) 3226 * @param pic_op halfpel motion compensation function (average or put normally)
3227 * @param pic_op qpel motion compensation function (average or put normally) 3227 * @param pic_op qpel motion compensation function (average or put normally)
3228 * the motion vectors are taken from s->mv and the MV type from s->mv_type 3228 * the motion vectors are taken from s->mv and the MV type from s->mv_type
3229 */ 3229 */
3230 static inline void MPV_motion(MpegEncContext *s, 3230 static inline void MPV_motion(MpegEncContext *s,
3231 uint8_t *dest_y, uint8_t *dest_cb, uint8_t *dest_cr, 3231 uint8_t *dest_y, uint8_t *dest_cb, uint8_t *dest_cr,
3232 int dir, uint8_t **ref_picture, 3232 int dir, uint8_t **ref_picture,
3233 op_pixels_func (*pix_op)[4], qpel_mc_func (*qpix_op)[16]) 3233 op_pixels_func (*pix_op)[4], qpel_mc_func (*qpix_op)[16])
3234 { 3234 {
3235 int dxy, mx, my, src_x, src_y, motion_x, motion_y; 3235 int dxy, mx, my, src_x, src_y, motion_x, motion_y;
3236 int mb_x, mb_y, i; 3236 int mb_x, mb_y, i;
3237 uint8_t *ptr, *dest; 3237 uint8_t *ptr, *dest;
3244 const int xy= s->mb_x + s->mb_y*s->mb_stride; 3244 const int xy= s->mb_x + s->mb_y*s->mb_stride;
3245 const int mot_stride= s->b8_stride; 3245 const int mot_stride= s->b8_stride;
3246 const int mot_xy= mb_x*2 + mb_y*2*mot_stride; 3246 const int mot_xy= mb_x*2 + mb_y*2*mot_stride;
3247 3247
3248 assert(!s->mb_skipped); 3248 assert(!s->mb_skipped);
3249 3249
3250 memcpy(mv_cache[1][1], s->current_picture.motion_val[0][mot_xy ], sizeof(int16_t)*4); 3250 memcpy(mv_cache[1][1], s->current_picture.motion_val[0][mot_xy ], sizeof(int16_t)*4);
3251 memcpy(mv_cache[2][1], s->current_picture.motion_val[0][mot_xy+mot_stride], sizeof(int16_t)*4); 3251 memcpy(mv_cache[2][1], s->current_picture.motion_val[0][mot_xy+mot_stride], sizeof(int16_t)*4);
3252 memcpy(mv_cache[3][1], s->current_picture.motion_val[0][mot_xy+mot_stride], sizeof(int16_t)*4); 3252 memcpy(mv_cache[3][1], s->current_picture.motion_val[0][mot_xy+mot_stride], sizeof(int16_t)*4);
3253 3253
3254 if(mb_y==0 || IS_INTRA(s->current_picture.mb_type[xy-s->mb_stride])){ 3254 if(mb_y==0 || IS_INTRA(s->current_picture.mb_type[xy-s->mb_stride])){
3270 *(int32_t*)mv_cache[2][3]= *(int32_t*)mv_cache[2][2]; 3270 *(int32_t*)mv_cache[2][3]= *(int32_t*)mv_cache[2][2];
3271 }else{ 3271 }else{
3272 *(int32_t*)mv_cache[1][3]= *(int32_t*)s->current_picture.motion_val[0][mot_xy+2]; 3272 *(int32_t*)mv_cache[1][3]= *(int32_t*)s->current_picture.motion_val[0][mot_xy+2];
3273 *(int32_t*)mv_cache[2][3]= *(int32_t*)s->current_picture.motion_val[0][mot_xy+2+mot_stride]; 3273 *(int32_t*)mv_cache[2][3]= *(int32_t*)s->current_picture.motion_val[0][mot_xy+2+mot_stride];
3274 } 3274 }
3275 3275
3276 mx = 0; 3276 mx = 0;
3277 my = 0; 3277 my = 0;
3278 for(i=0;i<4;i++) { 3278 for(i=0;i<4;i++) {
3279 const int x= (i&1)+1; 3279 const int x= (i&1)+1;
3280 const int y= (i>>1)+1; 3280 const int y= (i>>1)+1;
3297 if(!(s->flags&CODEC_FLAG_GRAY)) 3297 if(!(s->flags&CODEC_FLAG_GRAY))
3298 chroma_4mv_motion(s, dest_cb, dest_cr, ref_picture, pix_op[1], mx, my); 3298 chroma_4mv_motion(s, dest_cb, dest_cr, ref_picture, pix_op[1], mx, my);
3299 3299
3300 return; 3300 return;
3301 } 3301 }
3302 3302
3303 switch(s->mv_type) { 3303 switch(s->mv_type) {
3304 case MV_TYPE_16X16: 3304 case MV_TYPE_16X16:
3305 if(s->mcsel){ 3305 if(s->mcsel){
3306 if(s->real_sprite_warping_points==1){ 3306 if(s->real_sprite_warping_points==1){
3307 gmc1_motion(s, dest_y, dest_cb, dest_cr, 3307 gmc1_motion(s, dest_y, dest_cb, dest_cr,
3309 }else{ 3309 }else{
3310 gmc_motion(s, dest_y, dest_cb, dest_cr, 3310 gmc_motion(s, dest_y, dest_cb, dest_cr,
3311 ref_picture); 3311 ref_picture);
3312 } 3312 }
3313 }else if(s->quarter_sample){ 3313 }else if(s->quarter_sample){
3314 qpel_motion(s, dest_y, dest_cb, dest_cr, 3314 qpel_motion(s, dest_y, dest_cb, dest_cr,
3315 0, 0, 0, 3315 0, 0, 0,
3316 ref_picture, pix_op, qpix_op, 3316 ref_picture, pix_op, qpix_op,
3317 s->mv[dir][0][0], s->mv[dir][0][1], 16); 3317 s->mv[dir][0][0], s->mv[dir][0][1], 16);
3318 }else if(s->mspel){ 3318 }else if(s->mspel){
3319 ff_mspel_motion(s, dest_y, dest_cb, dest_cr, 3319 ff_mspel_motion(s, dest_y, dest_cb, dest_cr,
3320 ref_picture, pix_op, 3320 ref_picture, pix_op,
3321 s->mv[dir][0][0], s->mv[dir][0][1], 16); 3321 s->mv[dir][0][0], s->mv[dir][0][1], 16);
3322 }else 3322 }else
3323 { 3323 {
3324 mpeg_motion(s, dest_y, dest_cb, dest_cr, 3324 mpeg_motion(s, dest_y, dest_cb, dest_cr,
3325 0, 0, 0, 3325 0, 0, 0,
3326 ref_picture, pix_op, 3326 ref_picture, pix_op,
3327 s->mv[dir][0][0], s->mv[dir][0][1], 16); 3327 s->mv[dir][0][0], s->mv[dir][0][1], 16);
3328 } 3328 }
3329 break; 3329 break;
3330 case MV_TYPE_8X8: 3330 case MV_TYPE_8X8:
3331 mx = 0; 3331 mx = 0;
3332 my = 0; 3332 my = 0;
3333 if(s->quarter_sample){ 3333 if(s->quarter_sample){
3336 motion_y = s->mv[dir][i][1]; 3336 motion_y = s->mv[dir][i][1];
3337 3337
3338 dxy = ((motion_y & 3) << 2) | (motion_x & 3); 3338 dxy = ((motion_y & 3) << 2) | (motion_x & 3);
3339 src_x = mb_x * 16 + (motion_x >> 2) + (i & 1) * 8; 3339 src_x = mb_x * 16 + (motion_x >> 2) + (i & 1) * 8;
3340 src_y = mb_y * 16 + (motion_y >> 2) + (i >>1) * 8; 3340 src_y = mb_y * 16 + (motion_y >> 2) + (i >>1) * 8;
3341 3341
3342 /* WARNING: do no forget half pels */ 3342 /* WARNING: do no forget half pels */
3343 src_x = clip(src_x, -16, s->width); 3343 src_x = clip(src_x, -16, s->width);
3344 if (src_x == s->width) 3344 if (src_x == s->width)
3345 dxy &= ~3; 3345 dxy &= ~3;
3346 src_y = clip(src_y, -16, s->height); 3346 src_y = clip(src_y, -16, s->height);
3347 if (src_y == s->height) 3347 if (src_y == s->height)
3348 dxy &= ~12; 3348 dxy &= ~12;
3349 3349
3350 ptr = ref_picture[0] + (src_y * s->linesize) + (src_x); 3350 ptr = ref_picture[0] + (src_y * s->linesize) + (src_x);
3351 if(s->flags&CODEC_FLAG_EMU_EDGE){ 3351 if(s->flags&CODEC_FLAG_EMU_EDGE){
3352 if( (unsigned)src_x > s->h_edge_pos - (motion_x&3) - 8 3352 if( (unsigned)src_x > s->h_edge_pos - (motion_x&3) - 8
3353 || (unsigned)src_y > s->v_edge_pos - (motion_y&3) - 8 ){ 3353 || (unsigned)src_y > s->v_edge_pos - (motion_y&3) - 8 ){
3354 ff_emulated_edge_mc(s->edge_emu_buffer, ptr, s->linesize, 9, 9, src_x, src_y, s->h_edge_pos, s->v_edge_pos); 3354 ff_emulated_edge_mc(s->edge_emu_buffer, ptr, s->linesize, 9, 9, src_x, src_y, s->h_edge_pos, s->v_edge_pos);
3355 ptr= s->edge_emu_buffer; 3355 ptr= s->edge_emu_buffer;
3356 } 3356 }
3357 } 3357 }
3387 1, i, s->field_select[dir][i], 3387 1, i, s->field_select[dir][i],
3388 ref_picture, pix_op, qpix_op, 3388 ref_picture, pix_op, qpix_op,
3389 s->mv[dir][i][0], s->mv[dir][i][1], 8); 3389 s->mv[dir][i][0], s->mv[dir][i][1], 8);
3390 } 3390 }
3391 }else{ 3391 }else{
3392 /* top field */ 3392 /* top field */
3393 mpeg_motion(s, dest_y, dest_cb, dest_cr, 3393 mpeg_motion(s, dest_y, dest_cb, dest_cr,
3394 1, 0, s->field_select[dir][0], 3394 1, 0, s->field_select[dir][0],
3395 ref_picture, pix_op, 3395 ref_picture, pix_op,
3396 s->mv[dir][0][0], s->mv[dir][0][1], 8); 3396 s->mv[dir][0][0], s->mv[dir][0][1], 8);
3397 /* bottom field */ 3397 /* bottom field */
3401 s->mv[dir][1][0], s->mv[dir][1][1], 8); 3401 s->mv[dir][1][0], s->mv[dir][1][1], 8);
3402 } 3402 }
3403 } else { 3403 } else {
3404 if(s->picture_structure != s->field_select[dir][0] + 1 && s->pict_type != B_TYPE && !s->first_field){ 3404 if(s->picture_structure != s->field_select[dir][0] + 1 && s->pict_type != B_TYPE && !s->first_field){
3405 ref_picture= s->current_picture_ptr->data; 3405 ref_picture= s->current_picture_ptr->data;
3406 } 3406 }
3407 3407
3408 mpeg_motion(s, dest_y, dest_cb, dest_cr, 3408 mpeg_motion(s, dest_y, dest_cb, dest_cr,
3409 0, 0, s->field_select[dir][0], 3409 0, 0, s->field_select[dir][0],
3410 ref_picture, pix_op, 3410 ref_picture, pix_op,
3411 s->mv[dir][0][0], s->mv[dir][0][1], 16); 3411 s->mv[dir][0][0], s->mv[dir][0][1], 16);
3417 3417
3418 if(s->picture_structure == s->field_select[dir][i] + 1 || s->pict_type == B_TYPE || s->first_field){ 3418 if(s->picture_structure == s->field_select[dir][i] + 1 || s->pict_type == B_TYPE || s->first_field){
3419 ref2picture= ref_picture; 3419 ref2picture= ref_picture;
3420 }else{ 3420 }else{
3421 ref2picture= s->current_picture_ptr->data; 3421 ref2picture= s->current_picture_ptr->data;
3422 } 3422 }
3423 3423
3424 mpeg_motion(s, dest_y, dest_cb, dest_cr, 3424 mpeg_motion(s, dest_y, dest_cb, dest_cr,
3425 0, 0, s->field_select[dir][i], 3425 0, 0, s->field_select[dir][i],
3426 ref2picture, pix_op, 3426 ref2picture, pix_op,
3427 s->mv[dir][i][0], s->mv[dir][i][1] + 16*i, 8); 3427 s->mv[dir][i][0], s->mv[dir][i][1] + 16*i, 8);
3428 3428
3429 dest_y += 16*s->linesize; 3429 dest_y += 16*s->linesize;
3430 dest_cb+= (16>>s->chroma_y_shift)*s->uvlinesize; 3430 dest_cb+= (16>>s->chroma_y_shift)*s->uvlinesize;
3431 dest_cr+= (16>>s->chroma_y_shift)*s->uvlinesize; 3431 dest_cr+= (16>>s->chroma_y_shift)*s->uvlinesize;
3432 } 3432 }
3433 break; 3433 break;
3434 case MV_TYPE_DMV: 3434 case MV_TYPE_DMV:
3435 if(s->picture_structure == PICT_FRAME){ 3435 if(s->picture_structure == PICT_FRAME){
3436 for(i=0; i<2; i++){ 3436 for(i=0; i<2; i++){
3437 int j; 3437 int j;
3439 mpeg_motion(s, dest_y, dest_cb, dest_cr, 3439 mpeg_motion(s, dest_y, dest_cb, dest_cr,
3440 1, j, j^i, 3440 1, j, j^i,
3441 ref_picture, pix_op, 3441 ref_picture, pix_op,
3442 s->mv[dir][2*i + j][0], s->mv[dir][2*i + j][1], 8); 3442 s->mv[dir][2*i + j][0], s->mv[dir][2*i + j][1], 8);
3443 } 3443 }
3444 pix_op = s->dsp.avg_pixels_tab; 3444 pix_op = s->dsp.avg_pixels_tab;
3445 } 3445 }
3446 }else{ 3446 }else{
3447 for(i=0; i<2; i++){ 3447 for(i=0; i<2; i++){
3448 mpeg_motion(s, dest_y, dest_cb, dest_cr, 3448 mpeg_motion(s, dest_y, dest_cb, dest_cr,
3449 0, 0, s->picture_structure != i+1, 3449 0, 0, s->picture_structure != i+1,
3450 ref_picture, pix_op, 3450 ref_picture, pix_op,
3451 s->mv[dir][2*i][0],s->mv[dir][2*i][1],16); 3451 s->mv[dir][2*i][0],s->mv[dir][2*i][1],16);
3452 3452
3453 // after put we make avg of the same block 3453 // after put we make avg of the same block
3454 pix_op=s->dsp.avg_pixels_tab; 3454 pix_op=s->dsp.avg_pixels_tab;
3455 3455
3456 //opposite parity is always in the same frame if this is second field 3456 //opposite parity is always in the same frame if this is second field
3457 if(!s->first_field){ 3457 if(!s->first_field){
3458 ref_picture = s->current_picture_ptr->data; 3458 ref_picture = s->current_picture_ptr->data;
3459 } 3459 }
3460 } 3460 }
3461 } 3461 }
3462 break; 3462 break;
3463 default: assert(0); 3463 default: assert(0);
3473 * @param dir direction (0->forward, 1->backward) 3473 * @param dir direction (0->forward, 1->backward)
3474 * @param ref_picture array[3] of pointers to the 3 planes of the reference picture 3474 * @param ref_picture array[3] of pointers to the 3 planes of the reference picture
3475 * @param pic_op halfpel motion compensation function (average or put normally) 3475 * @param pic_op halfpel motion compensation function (average or put normally)
3476 * the motion vectors are taken from s->mv and the MV type from s->mv_type 3476 * the motion vectors are taken from s->mv and the MV type from s->mv_type
3477 */ 3477 */
3478 static inline void MPV_motion_lowres(MpegEncContext *s, 3478 static inline void MPV_motion_lowres(MpegEncContext *s,
3479 uint8_t *dest_y, uint8_t *dest_cb, uint8_t *dest_cr, 3479 uint8_t *dest_y, uint8_t *dest_cb, uint8_t *dest_cr,
3480 int dir, uint8_t **ref_picture, 3480 int dir, uint8_t **ref_picture,
3481 h264_chroma_mc_func *pix_op) 3481 h264_chroma_mc_func *pix_op)
3482 { 3482 {
3483 int mx, my; 3483 int mx, my;
3484 int mb_x, mb_y, i; 3484 int mb_x, mb_y, i;
3485 const int lowres= s->avctx->lowres; 3485 const int lowres= s->avctx->lowres;
3486 const int block_s= 8>>lowres; 3486 const int block_s= 8>>lowres;
3487 3487
3488 mb_x = s->mb_x; 3488 mb_x = s->mb_x;
3489 mb_y = s->mb_y; 3489 mb_y = s->mb_y;
3490 3490
3491 switch(s->mv_type) { 3491 switch(s->mv_type) {
3492 case MV_TYPE_16X16: 3492 case MV_TYPE_16X16:
3493 mpeg_motion_lowres(s, dest_y, dest_cb, dest_cr, 3493 mpeg_motion_lowres(s, dest_y, dest_cb, dest_cr,
3494 0, 0, 0, 3494 0, 0, 0,
3495 ref_picture, pix_op, 3495 ref_picture, pix_op,
3496 s->mv[dir][0][0], s->mv[dir][0][1], 2*block_s); 3496 s->mv[dir][0][0], s->mv[dir][0][1], 2*block_s);
3497 break; 3497 break;
3498 case MV_TYPE_8X8: 3498 case MV_TYPE_8X8:
3514 if(!(s->flags&CODEC_FLAG_GRAY)) 3514 if(!(s->flags&CODEC_FLAG_GRAY))
3515 chroma_4mv_motion_lowres(s, dest_cb, dest_cr, ref_picture, pix_op, mx, my); 3515 chroma_4mv_motion_lowres(s, dest_cb, dest_cr, ref_picture, pix_op, mx, my);
3516 break; 3516 break;
3517 case MV_TYPE_FIELD: 3517 case MV_TYPE_FIELD:
3518 if (s->picture_structure == PICT_FRAME) { 3518 if (s->picture_structure == PICT_FRAME) {
3519 /* top field */ 3519 /* top field */
3520 mpeg_motion_lowres(s, dest_y, dest_cb, dest_cr, 3520 mpeg_motion_lowres(s, dest_y, dest_cb, dest_cr,
3521 1, 0, s->field_select[dir][0], 3521 1, 0, s->field_select[dir][0],
3522 ref_picture, pix_op, 3522 ref_picture, pix_op,
3523 s->mv[dir][0][0], s->mv[dir][0][1], block_s); 3523 s->mv[dir][0][0], s->mv[dir][0][1], block_s);
3524 /* bottom field */ 3524 /* bottom field */
3527 ref_picture, pix_op, 3527 ref_picture, pix_op,
3528 s->mv[dir][1][0], s->mv[dir][1][1], block_s); 3528 s->mv[dir][1][0], s->mv[dir][1][1], block_s);
3529 } else { 3529 } else {
3530 if(s->picture_structure != s->field_select[dir][0] + 1 && s->pict_type != B_TYPE && !s->first_field){ 3530 if(s->picture_structure != s->field_select[dir][0] + 1 && s->pict_type != B_TYPE && !s->first_field){
3531 ref_picture= s->current_picture_ptr->data; 3531 ref_picture= s->current_picture_ptr->data;
3532 } 3532 }
3533 3533
3534 mpeg_motion_lowres(s, dest_y, dest_cb, dest_cr, 3534 mpeg_motion_lowres(s, dest_y, dest_cb, dest_cr,
3535 0, 0, s->field_select[dir][0], 3535 0, 0, s->field_select[dir][0],
3536 ref_picture, pix_op, 3536 ref_picture, pix_op,
3537 s->mv[dir][0][0], s->mv[dir][0][1], 2*block_s); 3537 s->mv[dir][0][0], s->mv[dir][0][1], 2*block_s);
3543 3543
3544 if(s->picture_structure == s->field_select[dir][i] + 1 || s->pict_type == B_TYPE || s->first_field){ 3544 if(s->picture_structure == s->field_select[dir][i] + 1 || s->pict_type == B_TYPE || s->first_field){
3545 ref2picture= ref_picture; 3545 ref2picture= ref_picture;
3546 }else{ 3546 }else{
3547 ref2picture= s->current_picture_ptr->data; 3547 ref2picture= s->current_picture_ptr->data;
3548 } 3548 }
3549 3549
3550 mpeg_motion_lowres(s, dest_y, dest_cb, dest_cr, 3550 mpeg_motion_lowres(s, dest_y, dest_cb, dest_cr,
3551 0, 0, s->field_select[dir][i], 3551 0, 0, s->field_select[dir][i],
3552 ref2picture, pix_op, 3552 ref2picture, pix_op,
3553 s->mv[dir][i][0], s->mv[dir][i][1] + 2*block_s*i, block_s); 3553 s->mv[dir][i][0], s->mv[dir][i][1] + 2*block_s*i, block_s);
3554 3554
3555 dest_y += 2*block_s*s->linesize; 3555 dest_y += 2*block_s*s->linesize;
3556 dest_cb+= (2*block_s>>s->chroma_y_shift)*s->uvlinesize; 3556 dest_cb+= (2*block_s>>s->chroma_y_shift)*s->uvlinesize;
3557 dest_cr+= (2*block_s>>s->chroma_y_shift)*s->uvlinesize; 3557 dest_cr+= (2*block_s>>s->chroma_y_shift)*s->uvlinesize;
3558 } 3558 }
3559 break; 3559 break;
3560 case MV_TYPE_DMV: 3560 case MV_TYPE_DMV:
3561 if(s->picture_structure == PICT_FRAME){ 3561 if(s->picture_structure == PICT_FRAME){
3562 for(i=0; i<2; i++){ 3562 for(i=0; i<2; i++){
3563 int j; 3563 int j;
3569 } 3569 }
3570 pix_op = s->dsp.avg_h264_chroma_pixels_tab; 3570 pix_op = s->dsp.avg_h264_chroma_pixels_tab;
3571 } 3571 }
3572 }else{ 3572 }else{
3573 for(i=0; i<2; i++){ 3573 for(i=0; i<2; i++){
3574 mpeg_motion_lowres(s, dest_y, dest_cb, dest_cr, 3574 mpeg_motion_lowres(s, dest_y, dest_cb, dest_cr,
3575 0, 0, s->picture_structure != i+1, 3575 0, 0, s->picture_structure != i+1,
3576 ref_picture, pix_op, 3576 ref_picture, pix_op,
3577 s->mv[dir][2*i][0],s->mv[dir][2*i][1],2*block_s); 3577 s->mv[dir][2*i][0],s->mv[dir][2*i][1],2*block_s);
3578 3578
3579 // after put we make avg of the same block 3579 // after put we make avg of the same block
3580 pix_op = s->dsp.avg_h264_chroma_pixels_tab; 3580 pix_op = s->dsp.avg_h264_chroma_pixels_tab;
3581 3581
3582 //opposite parity is always in the same frame if this is second field 3582 //opposite parity is always in the same frame if this is second field
3583 if(!s->first_field){ 3583 if(!s->first_field){
3584 ref_picture = s->current_picture_ptr->data; 3584 ref_picture = s->current_picture_ptr->data;
3585 } 3585 }
3586 } 3586 }
3587 } 3587 }
3588 break; 3588 break;
3589 default: assert(0); 3589 default: assert(0);
3590 } 3590 }
3591 } 3591 }
3592 3592
3593 /* put block[] to dest[] */ 3593 /* put block[] to dest[] */
3594 static inline void put_dct(MpegEncContext *s, 3594 static inline void put_dct(MpegEncContext *s,
3595 DCTELEM *block, int i, uint8_t *dest, int line_size, int qscale) 3595 DCTELEM *block, int i, uint8_t *dest, int line_size, int qscale)
3596 { 3596 {
3597 s->dct_unquantize_intra(s, block, i, qscale); 3597 s->dct_unquantize_intra(s, block, i, qscale);
3598 s->dsp.idct_put (dest, line_size, block); 3598 s->dsp.idct_put (dest, line_size, block);
3599 } 3599 }
3600 3600
3601 /* add block[] to dest[] */ 3601 /* add block[] to dest[] */
3602 static inline void add_dct(MpegEncContext *s, 3602 static inline void add_dct(MpegEncContext *s,
3603 DCTELEM *block, int i, uint8_t *dest, int line_size) 3603 DCTELEM *block, int i, uint8_t *dest, int line_size)
3604 { 3604 {
3605 if (s->block_last_index[i] >= 0) { 3605 if (s->block_last_index[i] >= 0) {
3606 s->dsp.idct_add (dest, line_size, block); 3606 s->dsp.idct_add (dest, line_size, block);
3607 } 3607 }
3608 } 3608 }
3609 3609
3610 static inline void add_dequant_dct(MpegEncContext *s, 3610 static inline void add_dequant_dct(MpegEncContext *s,
3611 DCTELEM *block, int i, uint8_t *dest, int line_size, int qscale) 3611 DCTELEM *block, int i, uint8_t *dest, int line_size, int qscale)
3612 { 3612 {
3613 if (s->block_last_index[i] >= 0) { 3613 if (s->block_last_index[i] >= 0) {
3614 s->dct_unquantize_inter(s, block, i, qscale); 3614 s->dct_unquantize_inter(s, block, i, qscale);
3615 3615
3622 */ 3622 */
3623 void ff_clean_intra_table_entries(MpegEncContext *s) 3623 void ff_clean_intra_table_entries(MpegEncContext *s)
3624 { 3624 {
3625 int wrap = s->b8_stride; 3625 int wrap = s->b8_stride;
3626 int xy = s->block_index[0]; 3626 int xy = s->block_index[0];
3627 3627
3628 s->dc_val[0][xy ] = 3628 s->dc_val[0][xy ] =
3629 s->dc_val[0][xy + 1 ] = 3629 s->dc_val[0][xy + 1 ] =
3630 s->dc_val[0][xy + wrap] = 3630 s->dc_val[0][xy + wrap] =
3631 s->dc_val[0][xy + 1 + wrap] = 1024; 3631 s->dc_val[0][xy + 1 + wrap] = 1024;
3632 /* ac pred */ 3632 /* ac pred */
3633 memset(s->ac_val[0][xy ], 0, 32 * sizeof(int16_t)); 3633 memset(s->ac_val[0][xy ], 0, 32 * sizeof(int16_t));
3634 memset(s->ac_val[0][xy + wrap], 0, 32 * sizeof(int16_t)); 3634 memset(s->ac_val[0][xy + wrap], 0, 32 * sizeof(int16_t));
3644 s->dc_val[1][xy] = 3644 s->dc_val[1][xy] =
3645 s->dc_val[2][xy] = 1024; 3645 s->dc_val[2][xy] = 1024;
3646 /* ac pred */ 3646 /* ac pred */
3647 memset(s->ac_val[1][xy], 0, 16 * sizeof(int16_t)); 3647 memset(s->ac_val[1][xy], 0, 16 * sizeof(int16_t));
3648 memset(s->ac_val[2][xy], 0, 16 * sizeof(int16_t)); 3648 memset(s->ac_val[2][xy], 0, 16 * sizeof(int16_t));
3649 3649
3650 s->mbintra_table[xy]= 0; 3650 s->mbintra_table[xy]= 0;
3651 } 3651 }
3652 3652
3653 /* generic function called after a macroblock has been parsed by the 3653 /* generic function called after a macroblock has been parsed by the
3654 decoder or after it has been encoded by the encoder. 3654 decoder or after it has been encoded by the encoder.
3718 assert(age); 3718 assert(age);
3719 3719
3720 if (s->mb_skipped) { 3720 if (s->mb_skipped) {
3721 s->mb_skipped= 0; 3721 s->mb_skipped= 0;
3722 assert(s->pict_type!=I_TYPE); 3722 assert(s->pict_type!=I_TYPE);
3723 3723
3724 (*mbskip_ptr) ++; /* indicate that this time we skipped it */ 3724 (*mbskip_ptr) ++; /* indicate that this time we skipped it */
3725 if(*mbskip_ptr >99) *mbskip_ptr= 99; 3725 if(*mbskip_ptr >99) *mbskip_ptr= 99;
3726 3726
3727 /* if previous was skipped too, then nothing to do ! */ 3727 /* if previous was skipped too, then nothing to do ! */
3728 if (*mbskip_ptr >= age && s->current_picture.reference){ 3728 if (*mbskip_ptr >= age && s->current_picture.reference){
3733 if(*mbskip_ptr >99) *mbskip_ptr= 99; 3733 if(*mbskip_ptr >99) *mbskip_ptr= 99;
3734 } else{ 3734 } else{
3735 *mbskip_ptr = 0; /* not skipped */ 3735 *mbskip_ptr = 0; /* not skipped */
3736 } 3736 }
3737 } 3737 }
3738 3738
3739 dct_linesize = linesize << s->interlaced_dct; 3739 dct_linesize = linesize << s->interlaced_dct;
3740 dct_offset =(s->interlaced_dct)? linesize : linesize*block_size; 3740 dct_offset =(s->interlaced_dct)? linesize : linesize*block_size;
3741 3741
3742 if(readable){ 3742 if(readable){
3743 dest_y= s->dest[0]; 3743 dest_y= s->dest[0];
3744 dest_cb= s->dest[1]; 3744 dest_cb= s->dest[1];
3745 dest_cr= s->dest[2]; 3745 dest_cr= s->dest[2];
3746 }else{ 3746 }else{
3762 } 3762 }
3763 if (s->mv_dir & MV_DIR_BACKWARD) { 3763 if (s->mv_dir & MV_DIR_BACKWARD) {
3764 MPV_motion_lowres(s, dest_y, dest_cb, dest_cr, 1, s->next_picture.data, op_pix); 3764 MPV_motion_lowres(s, dest_y, dest_cb, dest_cr, 1, s->next_picture.data, op_pix);
3765 } 3765 }
3766 }else{ 3766 }else{
3767 if ((!s->no_rounding) || s->pict_type==B_TYPE){ 3767 if ((!s->no_rounding) || s->pict_type==B_TYPE){
3768 op_pix = s->dsp.put_pixels_tab; 3768 op_pix = s->dsp.put_pixels_tab;
3769 op_qpix= s->dsp.put_qpel_pixels_tab; 3769 op_qpix= s->dsp.put_qpel_pixels_tab;
3770 }else{ 3770 }else{
3771 op_pix = s->dsp.put_no_rnd_pixels_tab; 3771 op_pix = s->dsp.put_no_rnd_pixels_tab;
3772 op_qpix= s->dsp.put_no_rnd_qpel_pixels_tab; 3772 op_qpix= s->dsp.put_no_rnd_qpel_pixels_tab;
3944 { 3944 {
3945 int i; 3945 int i;
3946 const int maxlevel= s->max_qcoeff; 3946 const int maxlevel= s->max_qcoeff;
3947 const int minlevel= s->min_qcoeff; 3947 const int minlevel= s->min_qcoeff;
3948 int overflow=0; 3948 int overflow=0;
3949 3949
3950 if(s->mb_intra){ 3950 if(s->mb_intra){
3951 i=1; //skip clipping of intra dc 3951 i=1; //skip clipping of intra dc
3952 }else 3952 }else
3953 i=0; 3953 i=0;
3954 3954
3955 for(;i<=last_index; i++){ 3955 for(;i<=last_index; i++){
3956 const int j= s->intra_scantable.permutated[i]; 3956 const int j= s->intra_scantable.permutated[i];
3957 int level = block[j]; 3957 int level = block[j];
3958 3958
3959 if (level>maxlevel){ 3959 if (level>maxlevel){
3960 level=maxlevel; 3960 level=maxlevel;
3961 overflow++; 3961 overflow++;
3962 }else if(level<minlevel){ 3962 }else if(level<minlevel){
3963 level=minlevel; 3963 level=minlevel;
3964 overflow++; 3964 overflow++;
3965 } 3965 }
3966 3966
3967 block[j]= level; 3967 block[j]= level;
3968 } 3968 }
3969 3969
3970 if(overflow && s->avctx->mb_decision == FF_MB_DECISION_SIMPLE) 3970 if(overflow && s->avctx->mb_decision == FF_MB_DECISION_SIMPLE)
3971 av_log(s->avctx, AV_LOG_INFO, "warning, clipping %d dct coefficients to %d..%d\n", overflow, minlevel, maxlevel); 3971 av_log(s->avctx, AV_LOG_INFO, "warning, clipping %d dct coefficients to %d..%d\n", overflow, minlevel, maxlevel);
3972 } 3972 }
3973 3973
3974 #endif //CONFIG_ENCODERS 3974 #endif //CONFIG_ENCODERS
3979 */ 3979 */
3980 void ff_draw_horiz_band(MpegEncContext *s, int y, int h){ 3980 void ff_draw_horiz_band(MpegEncContext *s, int y, int h){
3981 if (s->avctx->draw_horiz_band) { 3981 if (s->avctx->draw_horiz_band) {
3982 AVFrame *src; 3982 AVFrame *src;
3983 int offset[4]; 3983 int offset[4];
3984 3984
3985 if(s->picture_structure != PICT_FRAME){ 3985 if(s->picture_structure != PICT_FRAME){
3986 h <<= 1; 3986 h <<= 1;
3987 y <<= 1; 3987 y <<= 1;
3988 if(s->first_field && !(s->avctx->slice_flags&SLICE_FLAG_ALLOW_FIELD)) return; 3988 if(s->first_field && !(s->avctx->slice_flags&SLICE_FLAG_ALLOW_FIELD)) return;
3989 } 3989 }
3990 3990
3991 h= FFMIN(h, s->avctx->height - y); 3991 h= FFMIN(h, s->avctx->height - y);
3992 3992
3993 if(s->pict_type==B_TYPE || s->low_delay || (s->avctx->slice_flags&SLICE_FLAG_CODED_ORDER)) 3993 if(s->pict_type==B_TYPE || s->low_delay || (s->avctx->slice_flags&SLICE_FLAG_CODED_ORDER))
3994 src= (AVFrame*)s->current_picture_ptr; 3994 src= (AVFrame*)s->current_picture_ptr;
3995 else if(s->last_picture_ptr) 3995 else if(s->last_picture_ptr)
3996 src= (AVFrame*)s->last_picture_ptr; 3996 src= (AVFrame*)s->last_picture_ptr;
3997 else 3997 else
3998 return; 3998 return;
3999 3999
4000 if(s->pict_type==B_TYPE && s->picture_structure == PICT_FRAME && s->out_format != FMT_H264){ 4000 if(s->pict_type==B_TYPE && s->picture_structure == PICT_FRAME && s->out_format != FMT_H264){
4001 offset[0]= 4001 offset[0]=
4002 offset[1]= 4002 offset[1]=
4003 offset[2]= 4003 offset[2]=
4004 offset[3]= 0; 4004 offset[3]= 0;
4005 }else{ 4005 }else{
4006 offset[0]= y * s->linesize;; 4006 offset[0]= y * s->linesize;;
4007 offset[1]= 4007 offset[1]=
4008 offset[2]= (y >> s->chroma_y_shift) * s->uvlinesize; 4008 offset[2]= (y >> s->chroma_y_shift) * s->uvlinesize;
4009 offset[3]= 0; 4009 offset[3]= 0;
4010 } 4010 }
4011 4011
4012 emms_c(); 4012 emms_c();
4018 4018
4019 void ff_init_block_index(MpegEncContext *s){ //FIXME maybe rename 4019 void ff_init_block_index(MpegEncContext *s){ //FIXME maybe rename
4020 const int linesize= s->current_picture.linesize[0]; //not s->linesize as this would be wrong for field pics 4020 const int linesize= s->current_picture.linesize[0]; //not s->linesize as this would be wrong for field pics
4021 const int uvlinesize= s->current_picture.linesize[1]; 4021 const int uvlinesize= s->current_picture.linesize[1];
4022 const int mb_size= 4 - s->avctx->lowres; 4022 const int mb_size= 4 - s->avctx->lowres;
4023 4023
4024 s->block_index[0]= s->b8_stride*(s->mb_y*2 ) - 2 + s->mb_x*2; 4024 s->block_index[0]= s->b8_stride*(s->mb_y*2 ) - 2 + s->mb_x*2;
4025 s->block_index[1]= s->b8_stride*(s->mb_y*2 ) - 1 + s->mb_x*2; 4025 s->block_index[1]= s->b8_stride*(s->mb_y*2 ) - 1 + s->mb_x*2;
4026 s->block_index[2]= s->b8_stride*(s->mb_y*2 + 1) - 2 + s->mb_x*2; 4026 s->block_index[2]= s->b8_stride*(s->mb_y*2 + 1) - 2 + s->mb_x*2;
4027 s->block_index[3]= s->b8_stride*(s->mb_y*2 + 1) - 1 + s->mb_x*2; 4027 s->block_index[3]= s->b8_stride*(s->mb_y*2 + 1) - 1 + s->mb_x*2;
4028 s->block_index[4]= s->mb_stride*(s->mb_y + 1) + s->b8_stride*s->mb_height*2 + s->mb_x - 1; 4028 s->block_index[4]= s->mb_stride*(s->mb_y + 1) + s->b8_stride*s->mb_height*2 + s->mb_x - 1;
4075 int i; 4075 int i;
4076 int skip_dct[6]; 4076 int skip_dct[6];
4077 int dct_offset = s->linesize*8; //default for progressive frames 4077 int dct_offset = s->linesize*8; //default for progressive frames
4078 uint8_t *ptr_y, *ptr_cb, *ptr_cr; 4078 uint8_t *ptr_y, *ptr_cb, *ptr_cr;
4079 int wrap_y, wrap_c; 4079 int wrap_y, wrap_c;
4080 4080
4081 for(i=0; i<6; i++) skip_dct[i]=0; 4081 for(i=0; i<6; i++) skip_dct[i]=0;
4082 4082
4083 if(s->adaptive_quant){ 4083 if(s->adaptive_quant){
4084 const int last_qp= s->qscale; 4084 const int last_qp= s->qscale;
4085 const int mb_xy= mb_x + mb_y*s->mb_stride; 4085 const int mb_xy= mb_x + mb_y*s->mb_stride;
4086 4086
4087 s->lambda= s->lambda_table[mb_xy]; 4087 s->lambda= s->lambda_table[mb_xy];
4088 update_qscale(s); 4088 update_qscale(s);
4089 4089
4090 if(!(s->flags&CODEC_FLAG_QP_RD)){ 4090 if(!(s->flags&CODEC_FLAG_QP_RD)){
4091 s->dquant= s->qscale - last_qp; 4091 s->dquant= s->qscale - last_qp;
4092 4092
4093 if(s->out_format==FMT_H263){ 4093 if(s->out_format==FMT_H263){
4094 s->dquant= clip(s->dquant, -2, 2); //FIXME RD 4094 s->dquant= clip(s->dquant, -2, 2); //FIXME RD
4095 4095
4096 if(s->codec_id==CODEC_ID_MPEG4){ 4096 if(s->codec_id==CODEC_ID_MPEG4){
4097 if(!s->mb_intra){ 4097 if(!s->mb_intra){
4098 if(s->pict_type == B_TYPE){ 4098 if(s->pict_type == B_TYPE){
4099 if(s->dquant&1) 4099 if(s->dquant&1)
4100 s->dquant= (s->dquant/2)*2; 4100 s->dquant= (s->dquant/2)*2;
4101 if(s->mv_dir&MV_DIRECT) 4101 if(s->mv_dir&MV_DIRECT)
4102 s->dquant= 0; 4102 s->dquant= 0;
4103 } 4103 }
4104 if(s->mv_type==MV_TYPE_8X8) 4104 if(s->mv_type==MV_TYPE_8X8)
4130 if (s->mb_intra) { 4130 if (s->mb_intra) {
4131 if(s->flags&CODEC_FLAG_INTERLACED_DCT){ 4131 if(s->flags&CODEC_FLAG_INTERLACED_DCT){
4132 int progressive_score, interlaced_score; 4132 int progressive_score, interlaced_score;
4133 4133
4134 s->interlaced_dct=0; 4134 s->interlaced_dct=0;
4135 progressive_score= s->dsp.ildct_cmp[4](s, ptr_y , NULL, wrap_y, 8) 4135 progressive_score= s->dsp.ildct_cmp[4](s, ptr_y , NULL, wrap_y, 8)
4136 +s->dsp.ildct_cmp[4](s, ptr_y + wrap_y*8, NULL, wrap_y, 8) - 400; 4136 +s->dsp.ildct_cmp[4](s, ptr_y + wrap_y*8, NULL, wrap_y, 8) - 400;
4137 4137
4138 if(progressive_score > 0){ 4138 if(progressive_score > 0){
4139 interlaced_score = s->dsp.ildct_cmp[4](s, ptr_y , NULL, wrap_y*2, 8) 4139 interlaced_score = s->dsp.ildct_cmp[4](s, ptr_y , NULL, wrap_y*2, 8)
4140 +s->dsp.ildct_cmp[4](s, ptr_y + wrap_y , NULL, wrap_y*2, 8); 4140 +s->dsp.ildct_cmp[4](s, ptr_y + wrap_y , NULL, wrap_y*2, 8);
4141 if(progressive_score > interlaced_score){ 4141 if(progressive_score > interlaced_score){
4142 s->interlaced_dct=1; 4142 s->interlaced_dct=1;
4143 4143
4144 dct_offset= wrap_y; 4144 dct_offset= wrap_y;
4145 wrap_y<<=1; 4145 wrap_y<<=1;
4146 } 4146 }
4147 } 4147 }
4148 } 4148 }
4149 4149
4150 s->dsp.get_pixels(s->block[0], ptr_y , wrap_y); 4150 s->dsp.get_pixels(s->block[0], ptr_y , wrap_y);
4151 s->dsp.get_pixels(s->block[1], ptr_y + 8, wrap_y); 4151 s->dsp.get_pixels(s->block[1], ptr_y + 8, wrap_y);
4152 s->dsp.get_pixels(s->block[2], ptr_y + dct_offset , wrap_y); 4152 s->dsp.get_pixels(s->block[2], ptr_y + dct_offset , wrap_y);
4153 s->dsp.get_pixels(s->block[3], ptr_y + dct_offset + 8, wrap_y); 4153 s->dsp.get_pixels(s->block[3], ptr_y + dct_offset + 8, wrap_y);
4154 4154
4187 4187
4188 if(s->flags&CODEC_FLAG_INTERLACED_DCT){ 4188 if(s->flags&CODEC_FLAG_INTERLACED_DCT){
4189 int progressive_score, interlaced_score; 4189 int progressive_score, interlaced_score;
4190 4190
4191 s->interlaced_dct=0; 4191 s->interlaced_dct=0;
4192 progressive_score= s->dsp.ildct_cmp[0](s, dest_y , ptr_y , wrap_y, 8) 4192 progressive_score= s->dsp.ildct_cmp[0](s, dest_y , ptr_y , wrap_y, 8)
4193 +s->dsp.ildct_cmp[0](s, dest_y + wrap_y*8, ptr_y + wrap_y*8, wrap_y, 8) - 400; 4193 +s->dsp.ildct_cmp[0](s, dest_y + wrap_y*8, ptr_y + wrap_y*8, wrap_y, 8) - 400;
4194 4194
4195 if(s->avctx->ildct_cmp == FF_CMP_VSSE) progressive_score -= 400; 4195 if(s->avctx->ildct_cmp == FF_CMP_VSSE) progressive_score -= 400;
4196 4196
4197 if(progressive_score>0){ 4197 if(progressive_score>0){
4198 interlaced_score = s->dsp.ildct_cmp[0](s, dest_y , ptr_y , wrap_y*2, 8) 4198 interlaced_score = s->dsp.ildct_cmp[0](s, dest_y , ptr_y , wrap_y*2, 8)
4199 +s->dsp.ildct_cmp[0](s, dest_y + wrap_y , ptr_y + wrap_y , wrap_y*2, 8); 4199 +s->dsp.ildct_cmp[0](s, dest_y + wrap_y , ptr_y + wrap_y , wrap_y*2, 8);
4200 4200
4201 if(progressive_score > interlaced_score){ 4201 if(progressive_score > interlaced_score){
4202 s->interlaced_dct=1; 4202 s->interlaced_dct=1;
4203 4203
4204 dct_offset= wrap_y; 4204 dct_offset= wrap_y;
4205 wrap_y<<=1; 4205 wrap_y<<=1;
4206 } 4206 }
4207 } 4207 }
4208 } 4208 }
4209 4209
4210 s->dsp.diff_pixels(s->block[0], ptr_y , dest_y , wrap_y); 4210 s->dsp.diff_pixels(s->block[0], ptr_y , dest_y , wrap_y);
4211 s->dsp.diff_pixels(s->block[1], ptr_y + 8, dest_y + 8, wrap_y); 4211 s->dsp.diff_pixels(s->block[1], ptr_y + 8, dest_y + 8, wrap_y);
4212 s->dsp.diff_pixels(s->block[2], ptr_y + dct_offset , dest_y + dct_offset , wrap_y); 4212 s->dsp.diff_pixels(s->block[2], ptr_y + dct_offset , dest_y + dct_offset , wrap_y);
4213 s->dsp.diff_pixels(s->block[3], ptr_y + dct_offset + 8, dest_y + dct_offset + 8, wrap_y); 4213 s->dsp.diff_pixels(s->block[3], ptr_y + dct_offset + 8, dest_y + dct_offset + 8, wrap_y);
4214 4214
4215 if(s->flags&CODEC_FLAG_GRAY){ 4215 if(s->flags&CODEC_FLAG_GRAY){
4216 skip_dct[4]= 1; 4216 skip_dct[4]= 1;
4217 skip_dct[5]= 1; 4217 skip_dct[5]= 1;
4218 }else{ 4218 }else{
4219 s->dsp.diff_pixels(s->block[4], ptr_cb, dest_cb, wrap_c); 4219 s->dsp.diff_pixels(s->block[4], ptr_cb, dest_cb, wrap_c);
4220 s->dsp.diff_pixels(s->block[5], ptr_cr, dest_cr, wrap_c); 4220 s->dsp.diff_pixels(s->block[5], ptr_cr, dest_cr, wrap_c);
4221 } 4221 }
4222 /* pre quantization */ 4222 /* pre quantization */
4223 if(s->current_picture.mc_mb_var[s->mb_stride*mb_y+ mb_x]<2*s->qscale*s->qscale){ 4223 if(s->current_picture.mc_mb_var[s->mb_stride*mb_y+ mb_x]<2*s->qscale*s->qscale){
4224 //FIXME optimize 4224 //FIXME optimize
4225 if(s->dsp.sad[1](NULL, ptr_y , dest_y , wrap_y, 8) < 20*s->qscale) skip_dct[0]= 1; 4225 if(s->dsp.sad[1](NULL, ptr_y , dest_y , wrap_y, 8) < 20*s->qscale) skip_dct[0]= 1;
4226 if(s->dsp.sad[1](NULL, ptr_y + 8, dest_y + 8, wrap_y, 8) < 20*s->qscale) skip_dct[1]= 1; 4226 if(s->dsp.sad[1](NULL, ptr_y + 8, dest_y + 8, wrap_y, 8) < 20*s->qscale) skip_dct[1]= 1;
4227 if(s->dsp.sad[1](NULL, ptr_y +dct_offset , dest_y +dct_offset , wrap_y, 8) < 20*s->qscale) skip_dct[2]= 1; 4227 if(s->dsp.sad[1](NULL, ptr_y +dct_offset , dest_y +dct_offset , wrap_y, 8) < 20*s->qscale) skip_dct[2]= 1;
4238 if(!skip_dct[3]) get_vissual_weight(weight[3], ptr_y + dct_offset + 8, wrap_y); 4238 if(!skip_dct[3]) get_vissual_weight(weight[3], ptr_y + dct_offset + 8, wrap_y);
4239 if(!skip_dct[4]) get_vissual_weight(weight[4], ptr_cb , wrap_c); 4239 if(!skip_dct[4]) get_vissual_weight(weight[4], ptr_cb , wrap_c);
4240 if(!skip_dct[5]) get_vissual_weight(weight[5], ptr_cr , wrap_c); 4240 if(!skip_dct[5]) get_vissual_weight(weight[5], ptr_cr , wrap_c);
4241 memcpy(orig[0], s->block[0], sizeof(DCTELEM)*64*6); 4241 memcpy(orig[0], s->block[0], sizeof(DCTELEM)*64*6);
4242 } 4242 }
4243 4243
4244 /* DCT & quantize */ 4244 /* DCT & quantize */
4245 assert(s->out_format!=FMT_MJPEG || s->qscale==8); 4245 assert(s->out_format!=FMT_MJPEG || s->qscale==8);
4246 { 4246 {
4247 for(i=0;i<6;i++) { 4247 for(i=0;i<6;i++) {
4248 if(!skip_dct[i]){ 4248 if(!skip_dct[i]){
4260 if(!skip_dct[i]){ 4260 if(!skip_dct[i]){
4261 s->block_last_index[i] = dct_quantize_refine(s, s->block[i], weight[i], orig[i], i, s->qscale); 4261 s->block_last_index[i] = dct_quantize_refine(s, s->block[i], weight[i], orig[i], i, s->qscale);
4262 } 4262 }
4263 } 4263 }
4264 } 4264 }
4265 4265
4266 if(s->luma_elim_threshold && !s->mb_intra) 4266 if(s->luma_elim_threshold && !s->mb_intra)
4267 for(i=0; i<4; i++) 4267 for(i=0; i<4; i++)
4268 dct_single_coeff_elimination(s, i, s->luma_elim_threshold); 4268 dct_single_coeff_elimination(s, i, s->luma_elim_threshold);
4269 if(s->chroma_elim_threshold && !s->mb_intra) 4269 if(s->chroma_elim_threshold && !s->mb_intra)
4270 for(i=4; i<6; i++) 4270 for(i=4; i<6; i++)
4331 #endif //CONFIG_ENCODERS 4331 #endif //CONFIG_ENCODERS
4332 4332
4333 void ff_mpeg_flush(AVCodecContext *avctx){ 4333 void ff_mpeg_flush(AVCodecContext *avctx){
4334 int i; 4334 int i;
4335 MpegEncContext *s = avctx->priv_data; 4335 MpegEncContext *s = avctx->priv_data;
4336 4336
4337 if(s==NULL || s->picture==NULL) 4337 if(s==NULL || s->picture==NULL)
4338 return; 4338 return;
4339 4339
4340 for(i=0; i<MAX_PICTURE_COUNT; i++){ 4340 for(i=0; i<MAX_PICTURE_COUNT; i++){
4341 if(s->picture[i].data[0] && ( s->picture[i].type == FF_BUFFER_TYPE_INTERNAL 4341 if(s->picture[i].data[0] && ( s->picture[i].type == FF_BUFFER_TYPE_INTERNAL
4342 || s->picture[i].type == FF_BUFFER_TYPE_USER)) 4342 || s->picture[i].type == FF_BUFFER_TYPE_USER))
4343 avctx->release_buffer(avctx, (AVFrame*)&s->picture[i]); 4343 avctx->release_buffer(avctx, (AVFrame*)&s->picture[i]);
4344 } 4344 }
4345 s->current_picture_ptr = s->last_picture_ptr = s->next_picture_ptr = NULL; 4345 s->current_picture_ptr = s->last_picture_ptr = s->next_picture_ptr = NULL;
4346 4346
4347 s->mb_x= s->mb_y= 0; 4347 s->mb_x= s->mb_y= 0;
4348 4348
4349 s->parse_context.state= -1; 4349 s->parse_context.state= -1;
4350 s->parse_context.frame_start_found= 0; 4350 s->parse_context.frame_start_found= 0;
4351 s->parse_context.overread= 0; 4351 s->parse_context.overread= 0;
4352 s->parse_context.overread_index= 0; 4352 s->parse_context.overread_index= 0;
4353 s->parse_context.index= 0; 4353 s->parse_context.index= 0;
4362 int words= length>>4; 4362 int words= length>>4;
4363 int bits= length&15; 4363 int bits= length&15;
4364 int i; 4364 int i;
4365 4365
4366 if(length==0) return; 4366 if(length==0) return;
4367 4367
4368 if(words < 16){ 4368 if(words < 16){
4369 for(i=0; i<words; i++) put_bits(pb, 16, be2me_16(srcw[i])); 4369 for(i=0; i<words; i++) put_bits(pb, 16, be2me_16(srcw[i]));
4370 }else if(put_bits_count(pb)&7){ 4370 }else if(put_bits_count(pb)&7){
4371 for(i=0; i<words; i++) put_bits(pb, 16, be2me_16(srcw[i])); 4371 for(i=0; i<words; i++) put_bits(pb, 16, be2me_16(srcw[i]));
4372 }else{ 4372 }else{
4374 put_bits(pb, 8, src[i]); 4374 put_bits(pb, 8, src[i]);
4375 flush_put_bits(pb); 4375 flush_put_bits(pb);
4376 memcpy(pbBufPtr(pb), src+i, 2*words-i); 4376 memcpy(pbBufPtr(pb), src+i, 2*words-i);
4377 skip_put_bytes(pb, 2*words-i); 4377 skip_put_bytes(pb, 2*words-i);
4378 } 4378 }
4379 4379
4380 put_bits(pb, bits, be2me_16(srcw[words])>>(16-bits)); 4380 put_bits(pb, bits, be2me_16(srcw[words])>>(16-bits));
4381 } 4381 }
4382 4382
4383 static inline void copy_context_before_encode(MpegEncContext *d, MpegEncContext *s, int type){ 4383 static inline void copy_context_before_encode(MpegEncContext *d, MpegEncContext *s, int type){
4384 int i; 4384 int i;
4387 4387
4388 /* mpeg1 */ 4388 /* mpeg1 */
4389 d->mb_skip_run= s->mb_skip_run; 4389 d->mb_skip_run= s->mb_skip_run;
4390 for(i=0; i<3; i++) 4390 for(i=0; i<3; i++)
4391 d->last_dc[i]= s->last_dc[i]; 4391 d->last_dc[i]= s->last_dc[i];
4392 4392
4393 /* statistics */ 4393 /* statistics */
4394 d->mv_bits= s->mv_bits; 4394 d->mv_bits= s->mv_bits;
4395 d->i_tex_bits= s->i_tex_bits; 4395 d->i_tex_bits= s->i_tex_bits;
4396 d->p_tex_bits= s->p_tex_bits; 4396 d->p_tex_bits= s->p_tex_bits;
4397 d->i_count= s->i_count; 4397 d->i_count= s->i_count;
4407 } 4407 }
4408 4408
4409 static inline void copy_context_after_encode(MpegEncContext *d, MpegEncContext *s, int type){ 4409 static inline void copy_context_after_encode(MpegEncContext *d, MpegEncContext *s, int type){
4410 int i; 4410 int i;
4411 4411
4412 memcpy(d->mv, s->mv, 2*4*2*sizeof(int)); 4412 memcpy(d->mv, s->mv, 2*4*2*sizeof(int));
4413 memcpy(d->last_mv, s->last_mv, 2*2*2*sizeof(int)); //FIXME is memcpy faster then a loop? 4413 memcpy(d->last_mv, s->last_mv, 2*2*2*sizeof(int)); //FIXME is memcpy faster then a loop?
4414 4414
4415 /* mpeg1 */ 4415 /* mpeg1 */
4416 d->mb_skip_run= s->mb_skip_run; 4416 d->mb_skip_run= s->mb_skip_run;
4417 for(i=0; i<3; i++) 4417 for(i=0; i<3; i++)
4418 d->last_dc[i]= s->last_dc[i]; 4418 d->last_dc[i]= s->last_dc[i];
4419 4419
4420 /* statistics */ 4420 /* statistics */
4421 d->mv_bits= s->mv_bits; 4421 d->mv_bits= s->mv_bits;
4422 d->i_tex_bits= s->i_tex_bits; 4422 d->i_tex_bits= s->i_tex_bits;
4423 d->p_tex_bits= s->p_tex_bits; 4423 d->p_tex_bits= s->p_tex_bits;
4424 d->i_count= s->i_count; 4424 d->i_count= s->i_count;
4441 d->block_last_index[i]= s->block_last_index[i]; 4441 d->block_last_index[i]= s->block_last_index[i];
4442 d->interlaced_dct= s->interlaced_dct; 4442 d->interlaced_dct= s->interlaced_dct;
4443 d->qscale= s->qscale; 4443 d->qscale= s->qscale;
4444 } 4444 }
4445 4445
4446 static inline void encode_mb_hq(MpegEncContext *s, MpegEncContext *backup, MpegEncContext *best, int type, 4446 static inline void encode_mb_hq(MpegEncContext *s, MpegEncContext *backup, MpegEncContext *best, int type,
4447 PutBitContext pb[2], PutBitContext pb2[2], PutBitContext tex_pb[2], 4447 PutBitContext pb[2], PutBitContext pb2[2], PutBitContext tex_pb[2],
4448 int *dmin, int *next_block, int motion_x, int motion_y) 4448 int *dmin, int *next_block, int motion_x, int motion_y)
4449 { 4449 {
4450 int score; 4450 int score;
4451 uint8_t *dest_backup[3]; 4451 uint8_t *dest_backup[3];
4452 4452
4453 copy_context_before_encode(s, backup, type); 4453 copy_context_before_encode(s, backup, type);
4454 4454
4455 s->block= s->blocks[*next_block]; 4455 s->block= s->blocks[*next_block];
4456 s->pb= pb[*next_block]; 4456 s->pb= pb[*next_block];
4457 if(s->data_partitioning){ 4457 if(s->data_partitioning){
4458 s->pb2 = pb2 [*next_block]; 4458 s->pb2 = pb2 [*next_block];
4459 s->tex_pb= tex_pb[*next_block]; 4459 s->tex_pb= tex_pb[*next_block];
4460 } 4460 }
4461 4461
4462 if(*next_block){ 4462 if(*next_block){
4463 memcpy(dest_backup, s->dest, sizeof(s->dest)); 4463 memcpy(dest_backup, s->dest, sizeof(s->dest));
4464 s->dest[0] = s->rd_scratchpad; 4464 s->dest[0] = s->rd_scratchpad;
4465 s->dest[1] = s->rd_scratchpad + 16*s->linesize; 4465 s->dest[1] = s->rd_scratchpad + 16*s->linesize;
4466 s->dest[2] = s->rd_scratchpad + 16*s->linesize + 8; 4466 s->dest[2] = s->rd_scratchpad + 16*s->linesize + 8;
4467 assert(s->linesize >= 32); //FIXME 4467 assert(s->linesize >= 32); //FIXME
4468 } 4468 }
4469 4469
4470 encode_mb(s, motion_x, motion_y); 4470 encode_mb(s, motion_x, motion_y);
4471 4471
4472 score= put_bits_count(&s->pb); 4472 score= put_bits_count(&s->pb);
4473 if(s->data_partitioning){ 4473 if(s->data_partitioning){
4474 score+= put_bits_count(&s->pb2); 4474 score+= put_bits_count(&s->pb2);
4475 score+= put_bits_count(&s->tex_pb); 4475 score+= put_bits_count(&s->tex_pb);
4476 } 4476 }
4477 4477
4478 if(s->avctx->mb_decision == FF_MB_DECISION_RD){ 4478 if(s->avctx->mb_decision == FF_MB_DECISION_RD){
4479 MPV_decode_mb(s, s->block); 4479 MPV_decode_mb(s, s->block);
4480 4480
4481 score *= s->lambda2; 4481 score *= s->lambda2;
4482 score += sse_mb(s) << FF_LAMBDA_SHIFT; 4482 score += sse_mb(s) << FF_LAMBDA_SHIFT;
4483 } 4483 }
4484 4484
4485 if(*next_block){ 4485 if(*next_block){
4486 memcpy(s->dest, dest_backup, sizeof(s->dest)); 4486 memcpy(s->dest, dest_backup, sizeof(s->dest));
4487 } 4487 }
4488 4488
4489 if(score<*dmin){ 4489 if(score<*dmin){
4491 *next_block^=1; 4491 *next_block^=1;
4492 4492
4493 copy_context_after_encode(best, s, type); 4493 copy_context_after_encode(best, s, type);
4494 } 4494 }
4495 } 4495 }
4496 4496
4497 static int sse(MpegEncContext *s, uint8_t *src1, uint8_t *src2, int w, int h, int stride){ 4497 static int sse(MpegEncContext *s, uint8_t *src1, uint8_t *src2, int w, int h, int stride){
4498 uint32_t *sq = squareTbl + 256; 4498 uint32_t *sq = squareTbl + 256;
4499 int acc=0; 4499 int acc=0;
4500 int x,y; 4500 int x,y;
4501 4501
4502 if(w==16 && h==16) 4502 if(w==16 && h==16)
4503 return s->dsp.sse[0](NULL, src1, src2, stride, 16); 4503 return s->dsp.sse[0](NULL, src1, src2, stride, 16);
4504 else if(w==8 && h==8) 4504 else if(w==8 && h==8)
4505 return s->dsp.sse[1](NULL, src1, src2, stride, 8); 4505 return s->dsp.sse[1](NULL, src1, src2, stride, 8);
4506 4506
4507 for(y=0; y<h; y++){ 4507 for(y=0; y<h; y++){
4508 for(x=0; x<w; x++){ 4508 for(x=0; x<w; x++){
4509 acc+= sq[src1[x + y*stride] - src2[x + y*stride]]; 4509 acc+= sq[src1[x + y*stride] - src2[x + y*stride]];
4510 } 4510 }
4511 } 4511 }
4512 4512
4513 assert(acc>=0); 4513 assert(acc>=0);
4514 4514
4515 return acc; 4515 return acc;
4516 } 4516 }
4517 4517
4518 static int sse_mb(MpegEncContext *s){ 4518 static int sse_mb(MpegEncContext *s){
4519 int w= 16; 4519 int w= 16;
4539 } 4539 }
4540 4540
4541 static int pre_estimate_motion_thread(AVCodecContext *c, void *arg){ 4541 static int pre_estimate_motion_thread(AVCodecContext *c, void *arg){
4542 MpegEncContext *s= arg; 4542 MpegEncContext *s= arg;
4543 4543
4544 4544
4545 s->me.pre_pass=1; 4545 s->me.pre_pass=1;
4546 s->me.dia_size= s->avctx->pre_dia_size; 4546 s->me.dia_size= s->avctx->pre_dia_size;
4547 s->first_slice_line=1; 4547 s->first_slice_line=1;
4548 for(s->mb_y= s->end_mb_y-1; s->mb_y >= s->start_mb_y; s->mb_y--) { 4548 for(s->mb_y= s->end_mb_y-1; s->mb_y >= s->start_mb_y; s->mb_y--) {
4549 for(s->mb_x=s->mb_width-1; s->mb_x >=0 ;s->mb_x--) { 4549 for(s->mb_x=s->mb_width-1; s->mb_x >=0 ;s->mb_x--) {
4550 ff_pre_estimate_p_frame_motion(s, s->mb_x, s->mb_y); 4550 ff_pre_estimate_p_frame_motion(s, s->mb_x, s->mb_y);
4551 } 4551 }
4552 s->first_slice_line=0; 4552 s->first_slice_line=0;
4553 } 4553 }
4554 4554
4555 s->me.pre_pass=0; 4555 s->me.pre_pass=0;
4556 4556
4557 return 0; 4557 return 0;
4558 } 4558 }
4559 4559
4560 static int estimate_motion_thread(AVCodecContext *c, void *arg){ 4560 static int estimate_motion_thread(AVCodecContext *c, void *arg){
4561 MpegEncContext *s= arg; 4561 MpegEncContext *s= arg;
4568 for(s->mb_x=0; s->mb_x < s->mb_width; s->mb_x++) { 4568 for(s->mb_x=0; s->mb_x < s->mb_width; s->mb_x++) {
4569 s->block_index[0]+=2; 4569 s->block_index[0]+=2;
4570 s->block_index[1]+=2; 4570 s->block_index[1]+=2;
4571 s->block_index[2]+=2; 4571 s->block_index[2]+=2;
4572 s->block_index[3]+=2; 4572 s->block_index[3]+=2;
4573 4573
4574 /* compute motion vector & mb_type and store in context */ 4574 /* compute motion vector & mb_type and store in context */
4575 if(s->pict_type==B_TYPE) 4575 if(s->pict_type==B_TYPE)
4576 ff_estimate_b_frame_motion(s, s->mb_x, s->mb_y); 4576 ff_estimate_b_frame_motion(s, s->mb_x, s->mb_y);
4577 else 4577 else
4578 ff_estimate_p_frame_motion(s, s->mb_x, s->mb_y); 4578 ff_estimate_p_frame_motion(s, s->mb_x, s->mb_y);
4591 int xx = mb_x * 16; 4591 int xx = mb_x * 16;
4592 int yy = mb_y * 16; 4592 int yy = mb_y * 16;
4593 uint8_t *pix = s->new_picture.data[0] + (yy * s->linesize) + xx; 4593 uint8_t *pix = s->new_picture.data[0] + (yy * s->linesize) + xx;
4594 int varc; 4594 int varc;
4595 int sum = s->dsp.pix_sum(pix, s->linesize); 4595 int sum = s->dsp.pix_sum(pix, s->linesize);
4596 4596
4597 varc = (s->dsp.pix_norm1(pix, s->linesize) - (((unsigned)(sum*sum))>>8) + 500 + 128)>>8; 4597 varc = (s->dsp.pix_norm1(pix, s->linesize) - (((unsigned)(sum*sum))>>8) + 500 + 128)>>8;
4598 4598
4599 s->current_picture.mb_var [s->mb_stride * mb_y + mb_x] = varc; 4599 s->current_picture.mb_var [s->mb_stride * mb_y + mb_x] = varc;
4600 s->current_picture.mb_mean[s->mb_stride * mb_y + mb_x] = (sum+128)>>8; 4600 s->current_picture.mb_mean[s->mb_stride * mb_y + mb_x] = (sum+128)>>8;
4601 s->me.mb_var_sum_temp += varc; 4601 s->me.mb_var_sum_temp += varc;
4607 static void write_slice_end(MpegEncContext *s){ 4607 static void write_slice_end(MpegEncContext *s){
4608 if(s->codec_id==CODEC_ID_MPEG4){ 4608 if(s->codec_id==CODEC_ID_MPEG4){
4609 if(s->partitioned_frame){ 4609 if(s->partitioned_frame){
4610 ff_mpeg4_merge_partitions(s); 4610 ff_mpeg4_merge_partitions(s);
4611 } 4611 }
4612 4612
4613 ff_mpeg4_stuffing(&s->pb); 4613 ff_mpeg4_stuffing(&s->pb);
4614 }else if(s->out_format == FMT_MJPEG){ 4614 }else if(s->out_format == FMT_MJPEG){
4615 ff_mjpeg_stuffing(&s->pb); 4615 ff_mjpeg_stuffing(&s->pb);
4616 } 4616 }
4617 4617
4618 align_put_bits(&s->pb); 4618 align_put_bits(&s->pb);
4619 flush_put_bits(&s->pb); 4619 flush_put_bits(&s->pb);
4620 4620
4621 if((s->flags&CODEC_FLAG_PASS1) && !s->partitioned_frame) 4621 if((s->flags&CODEC_FLAG_PASS1) && !s->partitioned_frame)
4622 s->misc_bits+= get_bits_diff(s); 4622 s->misc_bits+= get_bits_diff(s);
4623 } 4623 }
4624 4624
4625 static int encode_thread(AVCodecContext *c, void *arg){ 4625 static int encode_thread(AVCodecContext *c, void *arg){
4651 4651
4652 for(i=0; i<3; i++){ 4652 for(i=0; i<3; i++){
4653 /* init last dc values */ 4653 /* init last dc values */
4654 /* note: quant matrix value (8) is implied here */ 4654 /* note: quant matrix value (8) is implied here */
4655 s->last_dc[i] = 128 << s->intra_dc_precision; 4655 s->last_dc[i] = 128 << s->intra_dc_precision;
4656 4656
4657 s->current_picture.error[i] = 0; 4657 s->current_picture.error[i] = 0;
4658 } 4658 }
4659 s->mb_skip_run = 0; 4659 s->mb_skip_run = 0;
4660 memset(s->last_mv, 0, sizeof(s->last_mv)); 4660 memset(s->last_mv, 0, sizeof(s->last_mv));
4661 4661
4662 s->last_mv_dir = 0; 4662 s->last_mv_dir = 0;
4663 4663
4664 switch(s->codec_id){ 4664 switch(s->codec_id){
4665 case CODEC_ID_H263: 4665 case CODEC_ID_H263:
4666 case CODEC_ID_H263P: 4666 case CODEC_ID_H263P:
4672 ff_mpeg4_init_partitions(s); 4672 ff_mpeg4_init_partitions(s);
4673 break; 4673 break;
4674 } 4674 }
4675 4675
4676 s->resync_mb_x=0; 4676 s->resync_mb_x=0;
4677 s->resync_mb_y=0; 4677 s->resync_mb_y=0;
4678 s->first_slice_line = 1; 4678 s->first_slice_line = 1;
4679 s->ptr_lastgob = s->pb.buf; 4679 s->ptr_lastgob = s->pb.buf;
4680 for(mb_y= s->start_mb_y; mb_y < s->end_mb_y; mb_y++) { 4680 for(mb_y= s->start_mb_y; mb_y < s->end_mb_y; mb_y++) {
4681 // printf("row %d at %X\n", s->mb_y, (int)s); 4681 // printf("row %d at %X\n", s->mb_y, (int)s);
4682 s->mb_x=0; 4682 s->mb_x=0;
4683 s->mb_y= mb_y; 4683 s->mb_y= mb_y;
4684 4684
4685 ff_set_qscale(s, s->qscale); 4685 ff_set_qscale(s, s->qscale);
4686 ff_init_block_index(s); 4686 ff_init_block_index(s);
4687 4687
4688 for(mb_x=0; mb_x < s->mb_width; mb_x++) { 4688 for(mb_x=0; mb_x < s->mb_width; mb_x++) {
4689 int xy= mb_y*s->mb_stride + mb_x; // removed const, H261 needs to adjust this 4689 int xy= mb_y*s->mb_stride + mb_x; // removed const, H261 needs to adjust this
4690 int mb_type= s->mb_type[xy]; 4690 int mb_type= s->mb_type[xy];
4691 // int d; 4691 // int d;
4692 int dmin= INT_MAX; 4692 int dmin= INT_MAX;
4717 #endif 4717 #endif
4718 4718
4719 /* write gob / video packet header */ 4719 /* write gob / video packet header */
4720 if(s->rtp_mode){ 4720 if(s->rtp_mode){
4721 int current_packet_size, is_gob_start; 4721 int current_packet_size, is_gob_start;
4722 4722
4723 current_packet_size= ((put_bits_count(&s->pb)+7)>>3) - (s->ptr_lastgob - s->pb.buf); 4723 current_packet_size= ((put_bits_count(&s->pb)+7)>>3) - (s->ptr_lastgob - s->pb.buf);
4724 4724
4725 is_gob_start= s->avctx->rtp_payload_size && current_packet_size >= s->avctx->rtp_payload_size && mb_y + mb_x>0; 4725 is_gob_start= s->avctx->rtp_payload_size && current_packet_size >= s->avctx->rtp_payload_size && mb_y + mb_x>0;
4726 4726
4727 if(s->start_mb_y == mb_y && mb_y > 0 && mb_x==0) is_gob_start=1; 4727 if(s->start_mb_y == mb_y && mb_y > 0 && mb_x==0) is_gob_start=1;
4728 4728
4729 switch(s->codec_id){ 4729 switch(s->codec_id){
4730 case CODEC_ID_H263: 4730 case CODEC_ID_H263:
4731 case CODEC_ID_H263P: 4731 case CODEC_ID_H263P:
4732 if(!s->h263_slice_structured) 4732 if(!s->h263_slice_structured)
4733 if(s->mb_x || s->mb_y%s->gob_index) is_gob_start=0; 4733 if(s->mb_x || s->mb_y%s->gob_index) is_gob_start=0;
4745 4745
4746 if(s->codec_id==CODEC_ID_MPEG4 && s->partitioned_frame){ 4746 if(s->codec_id==CODEC_ID_MPEG4 && s->partitioned_frame){
4747 ff_mpeg4_init_partitions(s); 4747 ff_mpeg4_init_partitions(s);
4748 } 4748 }
4749 } 4749 }
4750 4750
4751 assert((put_bits_count(&s->pb)&7) == 0); 4751 assert((put_bits_count(&s->pb)&7) == 0);
4752 current_packet_size= pbBufPtr(&s->pb) - s->ptr_lastgob; 4752 current_packet_size= pbBufPtr(&s->pb) - s->ptr_lastgob;
4753 4753
4754 if(s->avctx->error_rate && s->resync_mb_x + s->resync_mb_y > 0){ 4754 if(s->avctx->error_rate && s->resync_mb_x + s->resync_mb_y > 0){
4755 int r= put_bits_count(&s->pb)/8 + s->picture_number + 16 + s->mb_x + s->mb_y; 4755 int r= put_bits_count(&s->pb)/8 + s->picture_number + 16 + s->mb_x + s->mb_y;
4756 int d= 100 / s->avctx->error_rate; 4756 int d= 100 / s->avctx->error_rate;
4757 if(r % d == 0){ 4757 if(r % d == 0){
4758 current_packet_size=0; 4758 current_packet_size=0;
4765 4765
4766 if (s->avctx->rtp_callback){ 4766 if (s->avctx->rtp_callback){
4767 int number_mb = (mb_y - s->resync_mb_y)*s->mb_width + mb_x - s->resync_mb_x; 4767 int number_mb = (mb_y - s->resync_mb_y)*s->mb_width + mb_x - s->resync_mb_x;
4768 s->avctx->rtp_callback(s->avctx, s->ptr_lastgob, current_packet_size, number_mb); 4768 s->avctx->rtp_callback(s->avctx, s->ptr_lastgob, current_packet_size, number_mb);
4769 } 4769 }
4770 4770
4771 switch(s->codec_id){ 4771 switch(s->codec_id){
4772 case CODEC_ID_MPEG4: 4772 case CODEC_ID_MPEG4:
4773 ff_mpeg4_encode_video_packet_header(s); 4773 ff_mpeg4_encode_video_packet_header(s);
4774 ff_mpeg4_clean_buffers(s); 4774 ff_mpeg4_clean_buffers(s);
4775 break; 4775 break;
4778 ff_mpeg1_encode_slice_header(s); 4778 ff_mpeg1_encode_slice_header(s);
4779 ff_mpeg1_clean_buffers(s); 4779 ff_mpeg1_clean_buffers(s);
4780 break; 4780 break;
4781 case CODEC_ID_H263: 4781 case CODEC_ID_H263:
4782 case CODEC_ID_H263P: 4782 case CODEC_ID_H263P:
4783 h263_encode_gob_header(s, mb_y); 4783 h263_encode_gob_header(s, mb_y);
4784 break; 4784 break;
4785 } 4785 }
4786 4786
4787 if(s->flags&CODEC_FLAG_PASS1){ 4787 if(s->flags&CODEC_FLAG_PASS1){
4788 int bits= put_bits_count(&s->pb); 4788 int bits= put_bits_count(&s->pb);
4789 s->misc_bits+= bits - s->last_bits; 4789 s->misc_bits+= bits - s->last_bits;
4790 s->last_bits= bits; 4790 s->last_bits= bits;
4791 } 4791 }
4792 4792
4793 s->ptr_lastgob += current_packet_size; 4793 s->ptr_lastgob += current_packet_size;
4794 s->first_slice_line=1; 4794 s->first_slice_line=1;
4795 s->resync_mb_x=mb_x; 4795 s->resync_mb_x=mb_x;
4796 s->resync_mb_y=mb_y; 4796 s->resync_mb_y=mb_y;
4797 } 4797 }
4798 } 4798 }
4799 4799
4800 if( (s->resync_mb_x == s->mb_x) 4800 if( (s->resync_mb_x == s->mb_x)
4801 && s->resync_mb_y+1 == s->mb_y){ 4801 && s->resync_mb_y+1 == s->mb_y){
4802 s->first_slice_line=0; 4802 s->first_slice_line=0;
4803 } 4803 }
4804 4804
4805 s->mb_skipped=0; 4805 s->mb_skipped=0;
4806 s->dquant=0; //only for QP_RD 4806 s->dquant=0; //only for QP_RD
4807 4807
4822 s->mv_dir = MV_DIR_FORWARD; 4822 s->mv_dir = MV_DIR_FORWARD;
4823 s->mv_type = MV_TYPE_16X16; 4823 s->mv_type = MV_TYPE_16X16;
4824 s->mb_intra= 0; 4824 s->mb_intra= 0;
4825 s->mv[0][0][0] = s->p_mv_table[xy][0]; 4825 s->mv[0][0][0] = s->p_mv_table[xy][0];
4826 s->mv[0][0][1] = s->p_mv_table[xy][1]; 4826 s->mv[0][0][1] = s->p_mv_table[xy][1];
4827 encode_mb_hq(s, &backup_s, &best_s, CANDIDATE_MB_TYPE_INTER, pb, pb2, tex_pb, 4827 encode_mb_hq(s, &backup_s, &best_s, CANDIDATE_MB_TYPE_INTER, pb, pb2, tex_pb,
4828 &dmin, &next_block, s->mv[0][0][0], s->mv[0][0][1]); 4828 &dmin, &next_block, s->mv[0][0][0], s->mv[0][0][1]);
4829 } 4829 }
4830 if(mb_type&CANDIDATE_MB_TYPE_INTER_I){ 4830 if(mb_type&CANDIDATE_MB_TYPE_INTER_I){
4831 s->mv_dir = MV_DIR_FORWARD; 4831 s->mv_dir = MV_DIR_FORWARD;
4832 s->mv_type = MV_TYPE_FIELD; 4832 s->mv_type = MV_TYPE_FIELD;
4833 s->mb_intra= 0; 4833 s->mb_intra= 0;
4834 for(i=0; i<2; i++){ 4834 for(i=0; i<2; i++){
4835 j= s->field_select[0][i] = s->p_field_select_table[i][xy]; 4835 j= s->field_select[0][i] = s->p_field_select_table[i][xy];
4836 s->mv[0][i][0] = s->p_field_mv_table[i][j][xy][0]; 4836 s->mv[0][i][0] = s->p_field_mv_table[i][j][xy][0];
4837 s->mv[0][i][1] = s->p_field_mv_table[i][j][xy][1]; 4837 s->mv[0][i][1] = s->p_field_mv_table[i][j][xy][1];
4838 } 4838 }
4839 encode_mb_hq(s, &backup_s, &best_s, CANDIDATE_MB_TYPE_INTER_I, pb, pb2, tex_pb, 4839 encode_mb_hq(s, &backup_s, &best_s, CANDIDATE_MB_TYPE_INTER_I, pb, pb2, tex_pb,
4840 &dmin, &next_block, 0, 0); 4840 &dmin, &next_block, 0, 0);
4841 } 4841 }
4842 if(mb_type&CANDIDATE_MB_TYPE_SKIPPED){ 4842 if(mb_type&CANDIDATE_MB_TYPE_SKIPPED){
4843 s->mv_dir = MV_DIR_FORWARD; 4843 s->mv_dir = MV_DIR_FORWARD;
4844 s->mv_type = MV_TYPE_16X16; 4844 s->mv_type = MV_TYPE_16X16;
4845 s->mb_intra= 0; 4845 s->mb_intra= 0;
4846 s->mv[0][0][0] = 0; 4846 s->mv[0][0][0] = 0;
4847 s->mv[0][0][1] = 0; 4847 s->mv[0][0][1] = 0;
4848 encode_mb_hq(s, &backup_s, &best_s, CANDIDATE_MB_TYPE_SKIPPED, pb, pb2, tex_pb, 4848 encode_mb_hq(s, &backup_s, &best_s, CANDIDATE_MB_TYPE_SKIPPED, pb, pb2, tex_pb,
4849 &dmin, &next_block, s->mv[0][0][0], s->mv[0][0][1]); 4849 &dmin, &next_block, s->mv[0][0][0], s->mv[0][0][1]);
4850 } 4850 }
4851 if(mb_type&CANDIDATE_MB_TYPE_INTER4V){ 4851 if(mb_type&CANDIDATE_MB_TYPE_INTER4V){
4852 s->mv_dir = MV_DIR_FORWARD; 4852 s->mv_dir = MV_DIR_FORWARD;
4853 s->mv_type = MV_TYPE_8X8; 4853 s->mv_type = MV_TYPE_8X8;
4854 s->mb_intra= 0; 4854 s->mb_intra= 0;
4855 for(i=0; i<4; i++){ 4855 for(i=0; i<4; i++){
4856 s->mv[0][i][0] = s->current_picture.motion_val[0][s->block_index[i]][0]; 4856 s->mv[0][i][0] = s->current_picture.motion_val[0][s->block_index[i]][0];
4857 s->mv[0][i][1] = s->current_picture.motion_val[0][s->block_index[i]][1]; 4857 s->mv[0][i][1] = s->current_picture.motion_val[0][s->block_index[i]][1];
4858 } 4858 }
4859 encode_mb_hq(s, &backup_s, &best_s, CANDIDATE_MB_TYPE_INTER4V, pb, pb2, tex_pb, 4859 encode_mb_hq(s, &backup_s, &best_s, CANDIDATE_MB_TYPE_INTER4V, pb, pb2, tex_pb,
4860 &dmin, &next_block, 0, 0); 4860 &dmin, &next_block, 0, 0);
4861 } 4861 }
4862 if(mb_type&CANDIDATE_MB_TYPE_FORWARD){ 4862 if(mb_type&CANDIDATE_MB_TYPE_FORWARD){
4863 s->mv_dir = MV_DIR_FORWARD; 4863 s->mv_dir = MV_DIR_FORWARD;
4864 s->mv_type = MV_TYPE_16X16; 4864 s->mv_type = MV_TYPE_16X16;
4865 s->mb_intra= 0; 4865 s->mb_intra= 0;
4866 s->mv[0][0][0] = s->b_forw_mv_table[xy][0]; 4866 s->mv[0][0][0] = s->b_forw_mv_table[xy][0];
4867 s->mv[0][0][1] = s->b_forw_mv_table[xy][1]; 4867 s->mv[0][0][1] = s->b_forw_mv_table[xy][1];
4868 encode_mb_hq(s, &backup_s, &best_s, CANDIDATE_MB_TYPE_FORWARD, pb, pb2, tex_pb, 4868 encode_mb_hq(s, &backup_s, &best_s, CANDIDATE_MB_TYPE_FORWARD, pb, pb2, tex_pb,
4869 &dmin, &next_block, s->mv[0][0][0], s->mv[0][0][1]); 4869 &dmin, &next_block, s->mv[0][0][0], s->mv[0][0][1]);
4870 } 4870 }
4871 if(mb_type&CANDIDATE_MB_TYPE_BACKWARD){ 4871 if(mb_type&CANDIDATE_MB_TYPE_BACKWARD){
4872 s->mv_dir = MV_DIR_BACKWARD; 4872 s->mv_dir = MV_DIR_BACKWARD;
4873 s->mv_type = MV_TYPE_16X16; 4873 s->mv_type = MV_TYPE_16X16;
4874 s->mb_intra= 0; 4874 s->mb_intra= 0;
4875 s->mv[1][0][0] = s->b_back_mv_table[xy][0]; 4875 s->mv[1][0][0] = s->b_back_mv_table[xy][0];
4876 s->mv[1][0][1] = s->b_back_mv_table[xy][1]; 4876 s->mv[1][0][1] = s->b_back_mv_table[xy][1];
4877 encode_mb_hq(s, &backup_s, &best_s, CANDIDATE_MB_TYPE_BACKWARD, pb, pb2, tex_pb, 4877 encode_mb_hq(s, &backup_s, &best_s, CANDIDATE_MB_TYPE_BACKWARD, pb, pb2, tex_pb,
4878 &dmin, &next_block, s->mv[1][0][0], s->mv[1][0][1]); 4878 &dmin, &next_block, s->mv[1][0][0], s->mv[1][0][1]);
4879 } 4879 }
4880 if(mb_type&CANDIDATE_MB_TYPE_BIDIR){ 4880 if(mb_type&CANDIDATE_MB_TYPE_BIDIR){
4881 s->mv_dir = MV_DIR_FORWARD | MV_DIR_BACKWARD; 4881 s->mv_dir = MV_DIR_FORWARD | MV_DIR_BACKWARD;
4882 s->mv_type = MV_TYPE_16X16; 4882 s->mv_type = MV_TYPE_16X16;
4883 s->mb_intra= 0; 4883 s->mb_intra= 0;
4884 s->mv[0][0][0] = s->b_bidir_forw_mv_table[xy][0]; 4884 s->mv[0][0][0] = s->b_bidir_forw_mv_table[xy][0];
4885 s->mv[0][0][1] = s->b_bidir_forw_mv_table[xy][1]; 4885 s->mv[0][0][1] = s->b_bidir_forw_mv_table[xy][1];
4886 s->mv[1][0][0] = s->b_bidir_back_mv_table[xy][0]; 4886 s->mv[1][0][0] = s->b_bidir_back_mv_table[xy][0];
4887 s->mv[1][0][1] = s->b_bidir_back_mv_table[xy][1]; 4887 s->mv[1][0][1] = s->b_bidir_back_mv_table[xy][1];
4888 encode_mb_hq(s, &backup_s, &best_s, CANDIDATE_MB_TYPE_BIDIR, pb, pb2, tex_pb, 4888 encode_mb_hq(s, &backup_s, &best_s, CANDIDATE_MB_TYPE_BIDIR, pb, pb2, tex_pb,
4889 &dmin, &next_block, 0, 0); 4889 &dmin, &next_block, 0, 0);
4890 } 4890 }
4891 if(mb_type&CANDIDATE_MB_TYPE_DIRECT){ 4891 if(mb_type&CANDIDATE_MB_TYPE_DIRECT){
4892 int mx= s->b_direct_mv_table[xy][0]; 4892 int mx= s->b_direct_mv_table[xy][0];
4893 int my= s->b_direct_mv_table[xy][1]; 4893 int my= s->b_direct_mv_table[xy][1];
4894 4894
4895 s->mv_dir = MV_DIR_FORWARD | MV_DIR_BACKWARD | MV_DIRECT; 4895 s->mv_dir = MV_DIR_FORWARD | MV_DIR_BACKWARD | MV_DIRECT;
4896 s->mb_intra= 0; 4896 s->mb_intra= 0;
4897 ff_mpeg4_set_direct_mv(s, mx, my); 4897 ff_mpeg4_set_direct_mv(s, mx, my);
4898 encode_mb_hq(s, &backup_s, &best_s, CANDIDATE_MB_TYPE_DIRECT, pb, pb2, tex_pb, 4898 encode_mb_hq(s, &backup_s, &best_s, CANDIDATE_MB_TYPE_DIRECT, pb, pb2, tex_pb,
4899 &dmin, &next_block, mx, my); 4899 &dmin, &next_block, mx, my);
4900 } 4900 }
4901 if(mb_type&CANDIDATE_MB_TYPE_FORWARD_I){ 4901 if(mb_type&CANDIDATE_MB_TYPE_FORWARD_I){
4902 s->mv_dir = MV_DIR_FORWARD; 4902 s->mv_dir = MV_DIR_FORWARD;
4903 s->mv_type = MV_TYPE_FIELD; 4903 s->mv_type = MV_TYPE_FIELD;
4904 s->mb_intra= 0; 4904 s->mb_intra= 0;
4905 for(i=0; i<2; i++){ 4905 for(i=0; i<2; i++){
4906 j= s->field_select[0][i] = s->b_field_select_table[0][i][xy]; 4906 j= s->field_select[0][i] = s->b_field_select_table[0][i][xy];
4907 s->mv[0][i][0] = s->b_field_mv_table[0][i][j][xy][0]; 4907 s->mv[0][i][0] = s->b_field_mv_table[0][i][j][xy][0];
4908 s->mv[0][i][1] = s->b_field_mv_table[0][i][j][xy][1]; 4908 s->mv[0][i][1] = s->b_field_mv_table[0][i][j][xy][1];
4909 } 4909 }
4910 encode_mb_hq(s, &backup_s, &best_s, CANDIDATE_MB_TYPE_FORWARD_I, pb, pb2, tex_pb, 4910 encode_mb_hq(s, &backup_s, &best_s, CANDIDATE_MB_TYPE_FORWARD_I, pb, pb2, tex_pb,
4911 &dmin, &next_block, 0, 0); 4911 &dmin, &next_block, 0, 0);
4912 } 4912 }
4913 if(mb_type&CANDIDATE_MB_TYPE_BACKWARD_I){ 4913 if(mb_type&CANDIDATE_MB_TYPE_BACKWARD_I){
4914 s->mv_dir = MV_DIR_BACKWARD; 4914 s->mv_dir = MV_DIR_BACKWARD;
4915 s->mv_type = MV_TYPE_FIELD; 4915 s->mv_type = MV_TYPE_FIELD;
4916 s->mb_intra= 0; 4916 s->mb_intra= 0;
4917 for(i=0; i<2; i++){ 4917 for(i=0; i<2; i++){
4918 j= s->field_select[1][i] = s->b_field_select_table[1][i][xy]; 4918 j= s->field_select[1][i] = s->b_field_select_table[1][i][xy];
4919 s->mv[1][i][0] = s->b_field_mv_table[1][i][j][xy][0]; 4919 s->mv[1][i][0] = s->b_field_mv_table[1][i][j][xy][0];
4920 s->mv[1][i][1] = s->b_field_mv_table[1][i][j][xy][1]; 4920 s->mv[1][i][1] = s->b_field_mv_table[1][i][j][xy][1];
4921 } 4921 }
4922 encode_mb_hq(s, &backup_s, &best_s, CANDIDATE_MB_TYPE_BACKWARD_I, pb, pb2, tex_pb, 4922 encode_mb_hq(s, &backup_s, &best_s, CANDIDATE_MB_TYPE_BACKWARD_I, pb, pb2, tex_pb,
4923 &dmin, &next_block, 0, 0); 4923 &dmin, &next_block, 0, 0);
4924 } 4924 }
4925 if(mb_type&CANDIDATE_MB_TYPE_BIDIR_I){ 4925 if(mb_type&CANDIDATE_MB_TYPE_BIDIR_I){
4926 s->mv_dir = MV_DIR_FORWARD | MV_DIR_BACKWARD; 4926 s->mv_dir = MV_DIR_FORWARD | MV_DIR_BACKWARD;
4927 s->mv_type = MV_TYPE_FIELD; 4927 s->mv_type = MV_TYPE_FIELD;
4928 s->mb_intra= 0; 4928 s->mb_intra= 0;
4929 for(dir=0; dir<2; dir++){ 4929 for(dir=0; dir<2; dir++){
4930 for(i=0; i<2; i++){ 4930 for(i=0; i<2; i++){
4931 j= s->field_select[dir][i] = s->b_field_select_table[dir][i][xy]; 4931 j= s->field_select[dir][i] = s->b_field_select_table[dir][i][xy];
4932 s->mv[dir][i][0] = s->b_field_mv_table[dir][i][j][xy][0]; 4932 s->mv[dir][i][0] = s->b_field_mv_table[dir][i][j][xy][0];
4933 s->mv[dir][i][1] = s->b_field_mv_table[dir][i][j][xy][1]; 4933 s->mv[dir][i][1] = s->b_field_mv_table[dir][i][j][xy][1];
4934 } 4934 }
4935 } 4935 }
4936 encode_mb_hq(s, &backup_s, &best_s, CANDIDATE_MB_TYPE_BIDIR_I, pb, pb2, tex_pb, 4936 encode_mb_hq(s, &backup_s, &best_s, CANDIDATE_MB_TYPE_BIDIR_I, pb, pb2, tex_pb,
4937 &dmin, &next_block, 0, 0); 4937 &dmin, &next_block, 0, 0);
4938 } 4938 }
4939 if(mb_type&CANDIDATE_MB_TYPE_INTRA){ 4939 if(mb_type&CANDIDATE_MB_TYPE_INTRA){
4940 s->mv_dir = 0; 4940 s->mv_dir = 0;
4941 s->mv_type = MV_TYPE_16X16; 4941 s->mv_type = MV_TYPE_16X16;
4942 s->mb_intra= 1; 4942 s->mb_intra= 1;
4943 s->mv[0][0][0] = 0; 4943 s->mv[0][0][0] = 0;
4944 s->mv[0][0][1] = 0; 4944 s->mv[0][0][1] = 0;
4945 encode_mb_hq(s, &backup_s, &best_s, CANDIDATE_MB_TYPE_INTRA, pb, pb2, tex_pb, 4945 encode_mb_hq(s, &backup_s, &best_s, CANDIDATE_MB_TYPE_INTRA, pb, pb2, tex_pb,
4946 &dmin, &next_block, 0, 0); 4946 &dmin, &next_block, 0, 0);
4947 if(s->h263_pred || s->h263_aic){ 4947 if(s->h263_pred || s->h263_aic){
4948 if(best_s.mb_intra) 4948 if(best_s.mb_intra)
4949 s->mbintra_table[mb_x + mb_y*s->mb_stride]=1; 4949 s->mbintra_table[mb_x + mb_y*s->mb_stride]=1;
4950 else 4950 else
4956 if(best_s.mv_type==MV_TYPE_16X16 && !(best_s.mv_dir&MV_DIRECT)){ 4956 if(best_s.mv_type==MV_TYPE_16X16 && !(best_s.mv_dir&MV_DIRECT)){
4957 const int last_qp= backup_s.qscale; 4957 const int last_qp= backup_s.qscale;
4958 int dquant, dir, qp, dc[6]; 4958 int dquant, dir, qp, dc[6];
4959 DCTELEM ac[6][16]; 4959 DCTELEM ac[6][16];
4960 const int mvdir= (best_s.mv_dir&MV_DIR_BACKWARD) ? 1 : 0; 4960 const int mvdir= (best_s.mv_dir&MV_DIR_BACKWARD) ? 1 : 0;
4961 4961
4962 assert(backup_s.dquant == 0); 4962 assert(backup_s.dquant == 0);
4963 4963
4964 //FIXME intra 4964 //FIXME intra
4965 s->mv_dir= best_s.mv_dir; 4965 s->mv_dir= best_s.mv_dir;
4966 s->mv_type = MV_TYPE_16X16; 4966 s->mv_type = MV_TYPE_16X16;
4967 s->mb_intra= best_s.mb_intra; 4967 s->mb_intra= best_s.mb_intra;
4968 s->mv[0][0][0] = best_s.mv[0][0][0]; 4968 s->mv[0][0][0] = best_s.mv[0][0][0];
4969 s->mv[0][0][1] = best_s.mv[0][0][1]; 4969 s->mv[0][0][1] = best_s.mv[0][0][1];
4970 s->mv[1][0][0] = best_s.mv[1][0][0]; 4970 s->mv[1][0][0] = best_s.mv[1][0][0];
4971 s->mv[1][0][1] = best_s.mv[1][0][1]; 4971 s->mv[1][0][1] = best_s.mv[1][0][1];
4972 4972
4973 dir= s->pict_type == B_TYPE ? 2 : 1; 4973 dir= s->pict_type == B_TYPE ? 2 : 1;
4974 if(last_qp + dir > s->avctx->qmax) dir= -dir; 4974 if(last_qp + dir > s->avctx->qmax) dir= -dir;
4975 for(dquant= dir; dquant<=2 && dquant>=-2; dquant += dir){ 4975 for(dquant= dir; dquant<=2 && dquant>=-2; dquant += dir){
4976 qp= last_qp + dquant; 4976 qp= last_qp + dquant;
4977 if(qp < s->avctx->qmin || qp > s->avctx->qmax) 4977 if(qp < s->avctx->qmin || qp > s->avctx->qmax)
4982 dc[i]= s->dc_val[0][ s->block_index[i] ]; 4982 dc[i]= s->dc_val[0][ s->block_index[i] ];
4983 memcpy(ac[i], s->ac_val[0][s->block_index[i]], sizeof(DCTELEM)*16); 4983 memcpy(ac[i], s->ac_val[0][s->block_index[i]], sizeof(DCTELEM)*16);
4984 } 4984 }
4985 } 4985 }
4986 4986
4987 encode_mb_hq(s, &backup_s, &best_s, CANDIDATE_MB_TYPE_INTER /* wrong but unused */, pb, pb2, tex_pb, 4987 encode_mb_hq(s, &backup_s, &best_s, CANDIDATE_MB_TYPE_INTER /* wrong but unused */, pb, pb2, tex_pb,
4988 &dmin, &next_block, s->mv[mvdir][0][0], s->mv[mvdir][0][1]); 4988 &dmin, &next_block, s->mv[mvdir][0][0], s->mv[mvdir][0][1]);
4989 if(best_s.qscale != qp){ 4989 if(best_s.qscale != qp){
4990 if(s->mb_intra && s->dc_val[0]){ 4990 if(s->mb_intra && s->dc_val[0]){
4991 for(i=0; i<6; i++){ 4991 for(i=0; i<6; i++){
4992 s->dc_val[0][ s->block_index[i] ]= dc[i]; 4992 s->dc_val[0][ s->block_index[i] ]= dc[i];
5004 s->current_picture.qscale_table[xy]= qp; 5004 s->current_picture.qscale_table[xy]= qp;
5005 } 5005 }
5006 } 5006 }
5007 5007
5008 copy_context_after_encode(s, &best_s, -1); 5008 copy_context_after_encode(s, &best_s, -1);
5009 5009
5010 pb_bits_count= put_bits_count(&s->pb); 5010 pb_bits_count= put_bits_count(&s->pb);
5011 flush_put_bits(&s->pb); 5011 flush_put_bits(&s->pb);
5012 ff_copy_bits(&backup_s.pb, bit_buf[next_block^1], pb_bits_count); 5012 ff_copy_bits(&backup_s.pb, bit_buf[next_block^1], pb_bits_count);
5013 s->pb= backup_s.pb; 5013 s->pb= backup_s.pb;
5014 5014
5015 if(s->data_partitioning){ 5015 if(s->data_partitioning){
5016 pb2_bits_count= put_bits_count(&s->pb2); 5016 pb2_bits_count= put_bits_count(&s->pb2);
5017 flush_put_bits(&s->pb2); 5017 flush_put_bits(&s->pb2);
5018 ff_copy_bits(&backup_s.pb2, bit_buf2[next_block^1], pb2_bits_count); 5018 ff_copy_bits(&backup_s.pb2, bit_buf2[next_block^1], pb2_bits_count);
5019 s->pb2= backup_s.pb2; 5019 s->pb2= backup_s.pb2;
5020 5020
5021 tex_pb_bits_count= put_bits_count(&s->tex_pb); 5021 tex_pb_bits_count= put_bits_count(&s->tex_pb);
5022 flush_put_bits(&s->tex_pb); 5022 flush_put_bits(&s->tex_pb);
5023 ff_copy_bits(&backup_s.tex_pb, bit_buf_tex[next_block^1], tex_pb_bits_count); 5023 ff_copy_bits(&backup_s.tex_pb, bit_buf_tex[next_block^1], tex_pb_bits_count);
5024 s->tex_pb= backup_s.tex_pb; 5024 s->tex_pb= backup_s.tex_pb;
5025 } 5025 }
5026 s->last_bits= put_bits_count(&s->pb); 5026 s->last_bits= put_bits_count(&s->pb);
5027 5027
5028 if (s->out_format == FMT_H263 && s->pict_type!=B_TYPE) 5028 if (s->out_format == FMT_H263 && s->pict_type!=B_TYPE)
5029 ff_h263_update_motion_val(s); 5029 ff_h263_update_motion_val(s);
5030 5030
5031 if(next_block==0){ //FIXME 16 vs linesize16 5031 if(next_block==0){ //FIXME 16 vs linesize16
5032 s->dsp.put_pixels_tab[0][0](s->dest[0], s->rd_scratchpad , s->linesize ,16); 5032 s->dsp.put_pixels_tab[0][0](s->dest[0], s->rd_scratchpad , s->linesize ,16);
5033 s->dsp.put_pixels_tab[1][0](s->dest[1], s->rd_scratchpad + 16*s->linesize , s->uvlinesize, 8); 5033 s->dsp.put_pixels_tab[1][0](s->dest[1], s->rd_scratchpad + 16*s->linesize , s->uvlinesize, 8);
5034 s->dsp.put_pixels_tab[1][0](s->dest[2], s->rd_scratchpad + 16*s->linesize + 8, s->uvlinesize, 8); 5034 s->dsp.put_pixels_tab[1][0](s->dest[2], s->rd_scratchpad + 16*s->linesize + 8, s->uvlinesize, 8);
5035 } 5035 }
5038 MPV_decode_mb(s, s->block); 5038 MPV_decode_mb(s, s->block);
5039 } else { 5039 } else {
5040 int motion_x, motion_y; 5040 int motion_x, motion_y;
5041 s->mv_type=MV_TYPE_16X16; 5041 s->mv_type=MV_TYPE_16X16;
5042 // only one MB-Type possible 5042 // only one MB-Type possible
5043 5043
5044 switch(mb_type){ 5044 switch(mb_type){
5045 case CANDIDATE_MB_TYPE_INTRA: 5045 case CANDIDATE_MB_TYPE_INTRA:
5046 s->mv_dir = 0; 5046 s->mv_dir = 0;
5047 s->mb_intra= 1; 5047 s->mb_intra= 1;
5048 motion_x= s->mv[0][0][0] = 0; 5048 motion_x= s->mv[0][0][0] = 0;
5147 5147
5148 encode_mb(s, motion_x, motion_y); 5148 encode_mb(s, motion_x, motion_y);
5149 5149
5150 // RAL: Update last macroblock type 5150 // RAL: Update last macroblock type
5151 s->last_mv_dir = s->mv_dir; 5151 s->last_mv_dir = s->mv_dir;
5152 5152
5153 if (s->out_format == FMT_H263 && s->pict_type!=B_TYPE) 5153 if (s->out_format == FMT_H263 && s->pict_type!=B_TYPE)
5154 ff_h263_update_motion_val(s); 5154 ff_h263_update_motion_val(s);
5155 5155
5156 MPV_decode_mb(s, s->block); 5156 MPV_decode_mb(s, s->block);
5157 } 5157 }
5158 5158
5159 /* clean the MV table in IPS frames for direct mode in B frames */ 5159 /* clean the MV table in IPS frames for direct mode in B frames */
5160 if(s->mb_intra /* && I,P,S_TYPE */){ 5160 if(s->mb_intra /* && I,P,S_TYPE */){
5161 s->p_mv_table[xy][0]=0; 5161 s->p_mv_table[xy][0]=0;
5162 s->p_mv_table[xy][1]=0; 5162 s->p_mv_table[xy][1]=0;
5163 } 5163 }
5164 5164
5165 if(s->flags&CODEC_FLAG_PSNR){ 5165 if(s->flags&CODEC_FLAG_PSNR){
5166 int w= 16; 5166 int w= 16;
5167 int h= 16; 5167 int h= 16;
5168 5168
5169 if(s->mb_x*16 + 16 > s->width ) w= s->width - s->mb_x*16; 5169 if(s->mb_x*16 + 16 > s->width ) w= s->width - s->mb_x*16;
5191 if (s->msmpeg4_version && s->msmpeg4_version<4 && s->pict_type == I_TYPE) 5191 if (s->msmpeg4_version && s->msmpeg4_version<4 && s->pict_type == I_TYPE)
5192 msmpeg4_encode_ext_header(s); 5192 msmpeg4_encode_ext_header(s);
5193 5193
5194 write_slice_end(s); 5194 write_slice_end(s);
5195 5195
5196 /* Send the last GOB if RTP */ 5196 /* Send the last GOB if RTP */
5197 if (s->avctx->rtp_callback) { 5197 if (s->avctx->rtp_callback) {
5198 int number_mb = (mb_y - s->resync_mb_y)*s->mb_width - s->resync_mb_x; 5198 int number_mb = (mb_y - s->resync_mb_y)*s->mb_width - s->resync_mb_x;
5199 pdif = pbBufPtr(&s->pb) - s->ptr_lastgob; 5199 pdif = pbBufPtr(&s->pb) - s->ptr_lastgob;
5200 /* Call the RTP callback to send the last GOB */ 5200 /* Call the RTP callback to send the last GOB */
5201 emms_c(); 5201 emms_c();
5235 for(i=0; i<64; i++){ 5235 for(i=0; i<64; i++){
5236 MERGE(dct_error_sum[0][i]); 5236 MERGE(dct_error_sum[0][i]);
5237 MERGE(dct_error_sum[1][i]); 5237 MERGE(dct_error_sum[1][i]);
5238 } 5238 }
5239 } 5239 }
5240 5240
5241 assert(put_bits_count(&src->pb) % 8 ==0); 5241 assert(put_bits_count(&src->pb) % 8 ==0);
5242 assert(put_bits_count(&dst->pb) % 8 ==0); 5242 assert(put_bits_count(&dst->pb) % 8 ==0);
5243 ff_copy_bits(&dst->pb, src->pb.buf, put_bits_count(&src->pb)); 5243 ff_copy_bits(&dst->pb, src->pb.buf, put_bits_count(&src->pb));
5244 flush_put_bits(&dst->pb); 5244 flush_put_bits(&dst->pb);
5245 } 5245 }
5248 { 5248 {
5249 int i; 5249 int i;
5250 int bits; 5250 int bits;
5251 5251
5252 s->picture_number = picture_number; 5252 s->picture_number = picture_number;
5253 5253
5254 /* Reset the average MB variance */ 5254 /* Reset the average MB variance */
5255 s->me.mb_var_sum_temp = 5255 s->me.mb_var_sum_temp =
5256 s->me.mc_mb_var_sum_temp = 0; 5256 s->me.mc_mb_var_sum_temp = 0;
5257 5257
5258 /* we need to initialize some time vars before we can encode b-frames */ 5258 /* we need to initialize some time vars before we can encode b-frames */
5259 // RAL: Condition added for MPEG1VIDEO 5259 // RAL: Condition added for MPEG1VIDEO
5260 if (s->codec_id == CODEC_ID_MPEG1VIDEO || s->codec_id == CODEC_ID_MPEG2VIDEO || (s->h263_pred && !s->h263_msmpeg4)) 5260 if (s->codec_id == CODEC_ID_MPEG1VIDEO || s->codec_id == CODEC_ID_MPEG2VIDEO || (s->h263_pred && !s->h263_msmpeg4))
5261 ff_set_mpeg4_time(s, s->picture_number); //FIXME rename and use has_b_frames or similar 5261 ff_set_mpeg4_time(s, s->picture_number); //FIXME rename and use has_b_frames or similar
5262 5262
5263 s->me.scene_change_score=0; 5263 s->me.scene_change_score=0;
5264 5264
5265 // s->lambda= s->current_picture_ptr->quality; //FIXME qscale / ... stuff for ME ratedistoration 5265 // s->lambda= s->current_picture_ptr->quality; //FIXME qscale / ... stuff for ME ratedistoration
5266 5266
5267 if(s->pict_type==I_TYPE){ 5267 if(s->pict_type==I_TYPE){
5268 if(s->msmpeg4_version >= 3) s->no_rounding=1; 5268 if(s->msmpeg4_version >= 3) s->no_rounding=1;
5269 else s->no_rounding=0; 5269 else s->no_rounding=0;
5270 }else if(s->pict_type!=B_TYPE){ 5270 }else if(s->pict_type!=B_TYPE){
5271 if(s->flipflop_rounding || s->codec_id == CODEC_ID_H263P || s->codec_id == CODEC_ID_MPEG4) 5271 if(s->flipflop_rounding || s->codec_id == CODEC_ID_H263P || s->codec_id == CODEC_ID_MPEG4)
5272 s->no_rounding ^= 1; 5272 s->no_rounding ^= 1;
5273 } 5273 }
5274 5274
5275 s->mb_intra=0; //for the rate distortion & bit compare functions 5275 s->mb_intra=0; //for the rate distortion & bit compare functions
5276 for(i=1; i<s->avctx->thread_count; i++){ 5276 for(i=1; i<s->avctx->thread_count; i++){
5277 ff_update_duplicate_context(s->thread_context[i], s); 5277 ff_update_duplicate_context(s->thread_context[i], s);
5278 } 5278 }
5279 5279
5292 s->avctx->execute(s->avctx, estimate_motion_thread, (void**)&(s->thread_context[0]), NULL, s->avctx->thread_count); 5292 s->avctx->execute(s->avctx, estimate_motion_thread, (void**)&(s->thread_context[0]), NULL, s->avctx->thread_count);
5293 }else /* if(s->pict_type == I_TYPE) */{ 5293 }else /* if(s->pict_type == I_TYPE) */{
5294 /* I-Frame */ 5294 /* I-Frame */
5295 for(i=0; i<s->mb_stride*s->mb_height; i++) 5295 for(i=0; i<s->mb_stride*s->mb_height; i++)
5296 s->mb_type[i]= CANDIDATE_MB_TYPE_INTRA; 5296 s->mb_type[i]= CANDIDATE_MB_TYPE_INTRA;
5297 5297
5298 if(!s->fixed_qscale){ 5298 if(!s->fixed_qscale){
5299 /* finding spatial complexity for I-frame rate control */ 5299 /* finding spatial complexity for I-frame rate control */
5300 s->avctx->execute(s->avctx, mb_var_thread, (void**)&(s->thread_context[0]), NULL, s->avctx->thread_count); 5300 s->avctx->execute(s->avctx, mb_var_thread, (void**)&(s->thread_context[0]), NULL, s->avctx->thread_count);
5301 } 5301 }
5302 } 5302 }
5322 int a,b; 5322 int a,b;
5323 a= ff_get_best_fcode(s, s->p_field_mv_table[0][0], CANDIDATE_MB_TYPE_INTER_I); //FIXME field_select 5323 a= ff_get_best_fcode(s, s->p_field_mv_table[0][0], CANDIDATE_MB_TYPE_INTER_I); //FIXME field_select
5324 b= ff_get_best_fcode(s, s->p_field_mv_table[1][1], CANDIDATE_MB_TYPE_INTER_I); 5324 b= ff_get_best_fcode(s, s->p_field_mv_table[1][1], CANDIDATE_MB_TYPE_INTER_I);
5325 s->f_code= FFMAX(s->f_code, FFMAX(a,b)); 5325 s->f_code= FFMAX(s->f_code, FFMAX(a,b));
5326 } 5326 }
5327 5327
5328 ff_fix_long_p_mvs(s); 5328 ff_fix_long_p_mvs(s);
5329 ff_fix_long_mvs(s, NULL, 0, s->p_mv_table, s->f_code, CANDIDATE_MB_TYPE_INTER, 0); 5329 ff_fix_long_mvs(s, NULL, 0, s->p_mv_table, s->f_code, CANDIDATE_MB_TYPE_INTER, 0);
5330 if(s->flags & CODEC_FLAG_INTERLACED_ME){ 5330 if(s->flags & CODEC_FLAG_INTERLACED_ME){
5331 int j; 5331 int j;
5332 for(i=0; i<2; i++){ 5332 for(i=0; i<2; i++){
5333 for(j=0; j<2; j++) 5333 for(j=0; j<2; j++)
5334 ff_fix_long_mvs(s, s->p_field_select_table[i], j, 5334 ff_fix_long_mvs(s, s->p_field_select_table[i], j,
5335 s->p_field_mv_table[i][j], s->f_code, CANDIDATE_MB_TYPE_INTER_I, 0); 5335 s->p_field_mv_table[i][j], s->f_code, CANDIDATE_MB_TYPE_INTER_I, 0);
5336 } 5336 }
5337 } 5337 }
5338 } 5338 }
5339 5339
5355 if(s->flags & CODEC_FLAG_INTERLACED_ME){ 5355 if(s->flags & CODEC_FLAG_INTERLACED_ME){
5356 int dir, j; 5356 int dir, j;
5357 for(dir=0; dir<2; dir++){ 5357 for(dir=0; dir<2; dir++){
5358 for(i=0; i<2; i++){ 5358 for(i=0; i<2; i++){
5359 for(j=0; j<2; j++){ 5359 for(j=0; j<2; j++){
5360 int type= dir ? (CANDIDATE_MB_TYPE_BACKWARD_I|CANDIDATE_MB_TYPE_BIDIR_I) 5360 int type= dir ? (CANDIDATE_MB_TYPE_BACKWARD_I|CANDIDATE_MB_TYPE_BIDIR_I)
5361 : (CANDIDATE_MB_TYPE_FORWARD_I |CANDIDATE_MB_TYPE_BIDIR_I); 5361 : (CANDIDATE_MB_TYPE_FORWARD_I |CANDIDATE_MB_TYPE_BIDIR_I);
5362 ff_fix_long_mvs(s, s->b_field_select_table[dir][i], j, 5362 ff_fix_long_mvs(s, s->b_field_select_table[dir][i], j,
5363 s->b_field_mv_table[dir][i][j], dir ? s->b_code : s->f_code, type, 1); 5363 s->b_field_mv_table[dir][i][j], dir ? s->b_code : s->f_code, type, 1);
5364 } 5364 }
5365 } 5365 }
5366 } 5366 }
5367 } 5367 }
5368 } 5368 }
5369 } 5369 }
5370 5370
5371 if (!s->fixed_qscale) 5371 if (!s->fixed_qscale)
5372 s->current_picture.quality = ff_rate_estimate_qscale(s); //FIXME pic_ptr 5372 s->current_picture.quality = ff_rate_estimate_qscale(s); //FIXME pic_ptr
5373 5373
5374 if(s->adaptive_quant){ 5374 if(s->adaptive_quant){
5375 switch(s->codec_id){ 5375 switch(s->codec_id){
5376 case CODEC_ID_MPEG4: 5376 case CODEC_ID_MPEG4:
5387 //FIXME broken 5387 //FIXME broken
5388 }else 5388 }else
5389 s->lambda= s->current_picture.quality; 5389 s->lambda= s->current_picture.quality;
5390 //printf("%d %d\n", s->avctx->global_quality, s->current_picture.quality); 5390 //printf("%d %d\n", s->avctx->global_quality, s->current_picture.quality);
5391 update_qscale(s); 5391 update_qscale(s);
5392 5392
5393 if(s->qscale < 3 && s->max_qcoeff<=128 && s->pict_type==I_TYPE && !(s->flags & CODEC_FLAG_QSCALE)) 5393 if(s->qscale < 3 && s->max_qcoeff<=128 && s->pict_type==I_TYPE && !(s->flags & CODEC_FLAG_QSCALE))
5394 s->qscale= 3; //reduce clipping problems 5394 s->qscale= 3; //reduce clipping problems
5395 5395
5396 if (s->out_format == FMT_MJPEG) { 5396 if (s->out_format == FMT_MJPEG) {
5397 /* for mjpeg, we do include qscale in the matrix */ 5397 /* for mjpeg, we do include qscale in the matrix */
5398 s->intra_matrix[0] = ff_mpeg1_default_intra_matrix[0]; 5398 s->intra_matrix[0] = ff_mpeg1_default_intra_matrix[0];
5399 for(i=1;i<64;i++){ 5399 for(i=1;i<64;i++){
5400 int j= s->dsp.idct_permutation[i]; 5400 int j= s->dsp.idct_permutation[i];
5401 5401
5402 s->intra_matrix[j] = clip_uint8((ff_mpeg1_default_intra_matrix[i] * s->qscale) >> 3) & 0xFF; 5402 s->intra_matrix[j] = clip_uint8((ff_mpeg1_default_intra_matrix[i] * s->qscale) >> 3) & 0xFF;
5403 } 5403 }
5404 convert_matrix(&s->dsp, s->q_intra_matrix, s->q_intra_matrix16, 5404 convert_matrix(&s->dsp, s->q_intra_matrix, s->q_intra_matrix16,
5405 s->intra_matrix, s->intra_quant_bias, 8, 8, 1); 5405 s->intra_matrix, s->intra_quant_bias, 8, 8, 1);
5406 s->qscale= 8; 5406 s->qscale= 8;
5407 } 5407 }
5408 5408
5409 //FIXME var duplication 5409 //FIXME var duplication
5410 s->current_picture_ptr->key_frame= 5410 s->current_picture_ptr->key_frame=
5411 s->current_picture.key_frame= s->pict_type == I_TYPE; //FIXME pic_ptr 5411 s->current_picture.key_frame= s->pict_type == I_TYPE; //FIXME pic_ptr
5412 s->current_picture_ptr->pict_type= 5412 s->current_picture_ptr->pict_type=
5413 s->current_picture.pict_type= s->pict_type; 5413 s->current_picture.pict_type= s->pict_type;
5424 case FMT_H261: 5424 case FMT_H261:
5425 ff_h261_encode_picture_header(s, picture_number); 5425 ff_h261_encode_picture_header(s, picture_number);
5426 break; 5426 break;
5427 #endif 5427 #endif
5428 case FMT_H263: 5428 case FMT_H263:
5429 if (s->codec_id == CODEC_ID_WMV2) 5429 if (s->codec_id == CODEC_ID_WMV2)
5430 ff_wmv2_encode_picture_header(s, picture_number); 5430 ff_wmv2_encode_picture_header(s, picture_number);
5431 else if (s->h263_msmpeg4) 5431 else if (s->h263_msmpeg4)
5432 msmpeg4_encode_picture_header(s, picture_number); 5432 msmpeg4_encode_picture_header(s, picture_number);
5433 else if (s->h263_pred) 5433 else if (s->h263_pred)
5434 mpeg4_encode_picture_header(s, picture_number); 5434 mpeg4_encode_picture_header(s, picture_number);
5435 #ifdef CONFIG_RV10_ENCODER 5435 #ifdef CONFIG_RV10_ENCODER
5436 else if (s->codec_id == CODEC_ID_RV10) 5436 else if (s->codec_id == CODEC_ID_RV10)
5437 rv10_encode_picture_header(s, picture_number); 5437 rv10_encode_picture_header(s, picture_number);
5438 #endif 5438 #endif
5439 #ifdef CONFIG_RV20_ENCODER 5439 #ifdef CONFIG_RV20_ENCODER
5440 else if (s->codec_id == CODEC_ID_RV20) 5440 else if (s->codec_id == CODEC_ID_RV20)
5441 rv20_encode_picture_header(s, picture_number); 5441 rv20_encode_picture_header(s, picture_number);
5442 #endif 5442 #endif
5443 else if (s->codec_id == CODEC_ID_FLV1) 5443 else if (s->codec_id == CODEC_ID_FLV1)
5444 ff_flv_encode_picture_header(s, picture_number); 5444 ff_flv_encode_picture_header(s, picture_number);
5445 else 5445 else
5453 default: 5453 default:
5454 assert(0); 5454 assert(0);
5455 } 5455 }
5456 bits= put_bits_count(&s->pb); 5456 bits= put_bits_count(&s->pb);
5457 s->header_bits= bits - s->last_bits; 5457 s->header_bits= bits - s->last_bits;
5458 5458
5459 for(i=1; i<s->avctx->thread_count; i++){ 5459 for(i=1; i<s->avctx->thread_count; i++){
5460 update_duplicate_context_after_me(s->thread_context[i], s); 5460 update_duplicate_context_after_me(s->thread_context[i], s);
5461 } 5461 }
5462 s->avctx->execute(s->avctx, encode_thread, (void**)&(s->thread_context[0]), NULL, s->avctx->thread_count); 5462 s->avctx->execute(s->avctx, encode_thread, (void**)&(s->thread_context[0]), NULL, s->avctx->thread_count);
5463 for(i=1; i<s->avctx->thread_count; i++){ 5463 for(i=1; i<s->avctx->thread_count; i++){
5492 } 5492 }
5493 } 5493 }
5494 5494
5495 #ifdef CONFIG_ENCODERS 5495 #ifdef CONFIG_ENCODERS
5496 5496
5497 static int dct_quantize_trellis_c(MpegEncContext *s, 5497 static int dct_quantize_trellis_c(MpegEncContext *s,
5498 DCTELEM *block, int n, 5498 DCTELEM *block, int n,
5499 int qscale, int *overflow){ 5499 int qscale, int *overflow){
5500 const int *qmat; 5500 const int *qmat;
5501 const uint8_t *scantable= s->intra_scantable.scantable; 5501 const uint8_t *scantable= s->intra_scantable.scantable;
5502 const uint8_t *perm_scantable= s->intra_scantable.permutated; 5502 const uint8_t *perm_scantable= s->intra_scantable.permutated;
5517 int qmul, qadd, start_i, last_non_zero, i, dc; 5517 int qmul, qadd, start_i, last_non_zero, i, dc;
5518 const int esc_length= s->ac_esc_length; 5518 const int esc_length= s->ac_esc_length;
5519 uint8_t * length; 5519 uint8_t * length;
5520 uint8_t * last_length; 5520 uint8_t * last_length;
5521 const int lambda= s->lambda2 >> (FF_LAMBDA_SHIFT - 6); 5521 const int lambda= s->lambda2 >> (FF_LAMBDA_SHIFT - 6);
5522 5522
5523 s->dsp.fdct (block); 5523 s->dsp.fdct (block);
5524 5524
5525 if(s->dct_error_sum) 5525 if(s->dct_error_sum)
5526 s->denoise_dct(s, block); 5526 s->denoise_dct(s, block);
5527 qmul= qscale*16; 5527 qmul= qscale*16;
5528 qadd= ((qscale-1)|1)*8; 5528 qadd= ((qscale-1)|1)*8;
5529 5529
5538 } else{ 5538 } else{
5539 /* For AIC we skip quant/dequant of INTRADC */ 5539 /* For AIC we skip quant/dequant of INTRADC */
5540 q = 1 << 3; 5540 q = 1 << 3;
5541 qadd=0; 5541 qadd=0;
5542 } 5542 }
5543 5543
5544 /* note: block[0] is assumed to be positive */ 5544 /* note: block[0] is assumed to be positive */
5545 block[0] = (block[0] + (q >> 1)) / q; 5545 block[0] = (block[0] + (q >> 1)) / q;
5546 start_i = 1; 5546 start_i = 1;
5547 last_non_zero = 0; 5547 last_non_zero = 0;
5548 qmat = s->q_intra_matrix[qscale]; 5548 qmat = s->q_intra_matrix[qscale];
5596 }else{ 5596 }else{
5597 coeff[0][i]= (level>>31)|1; 5597 coeff[0][i]= (level>>31)|1;
5598 coeff_count[i]= 1; 5598 coeff_count[i]= 1;
5599 } 5599 }
5600 } 5600 }
5601 5601
5602 *overflow= s->max_qcoeff < max; //overflow might have happened 5602 *overflow= s->max_qcoeff < max; //overflow might have happened
5603 5603
5604 if(last_non_zero < start_i){ 5604 if(last_non_zero < start_i){
5605 memset(block + start_i, 0, (64-start_i)*sizeof(DCTELEM)); 5605 memset(block + start_i, 0, (64-start_i)*sizeof(DCTELEM));
5606 return last_non_zero; 5606 return last_non_zero;
5607 } 5607 }
5608 5608
5609 score_tab[start_i]= 0; 5609 score_tab[start_i]= 0;
5610 survivor[0]= start_i; 5610 survivor[0]= start_i;
5611 survivor_count= 1; 5611 survivor_count= 1;
5612 5612
5613 for(i=start_i; i<=last_non_zero; i++){ 5613 for(i=start_i; i<=last_non_zero; i++){
5614 int level_index, j; 5614 int level_index, j;
5615 const int dct_coeff= ABS(block[ scantable[i] ]); 5615 const int dct_coeff= ABS(block[ scantable[i] ]);
5616 const int zero_distoration= dct_coeff*dct_coeff; 5616 const int zero_distoration= dct_coeff*dct_coeff;
5617 int best_score=256*256*256*120; 5617 int best_score=256*256*256*120;
5618 for(level_index=0; level_index < coeff_count[i]; level_index++){ 5618 for(level_index=0; level_index < coeff_count[i]; level_index++){
5619 int distoration; 5619 int distoration;
5620 int level= coeff[level_index][i]; 5620 int level= coeff[level_index][i];
5621 const int alevel= ABS(level); 5621 const int alevel= ABS(level);
5622 int unquant_coeff; 5622 int unquant_coeff;
5623 5623
5624 assert(level); 5624 assert(level);
5625 5625
5626 if(s->out_format == FMT_H263){ 5626 if(s->out_format == FMT_H263){
5627 unquant_coeff= alevel*qmul + qadd; 5627 unquant_coeff= alevel*qmul + qadd;
5628 }else{ //MPEG1 5628 }else{ //MPEG1
5642 if((level&(~127)) == 0){ 5642 if((level&(~127)) == 0){
5643 for(j=survivor_count-1; j>=0; j--){ 5643 for(j=survivor_count-1; j>=0; j--){
5644 int run= i - survivor[j]; 5644 int run= i - survivor[j];
5645 int score= distoration + length[UNI_AC_ENC_INDEX(run, level)]*lambda; 5645 int score= distoration + length[UNI_AC_ENC_INDEX(run, level)]*lambda;
5646 score += score_tab[i-run]; 5646 score += score_tab[i-run];
5647 5647
5648 if(score < best_score){ 5648 if(score < best_score){
5649 best_score= score; 5649 best_score= score;
5650 run_tab[i+1]= run; 5650 run_tab[i+1]= run;
5651 level_tab[i+1]= level-64; 5651 level_tab[i+1]= level-64;
5652 } 5652 }
5668 }else{ 5668 }else{
5669 distoration += esc_length*lambda; 5669 distoration += esc_length*lambda;
5670 for(j=survivor_count-1; j>=0; j--){ 5670 for(j=survivor_count-1; j>=0; j--){
5671 int run= i - survivor[j]; 5671 int run= i - survivor[j];
5672 int score= distoration + score_tab[i-run]; 5672 int score= distoration + score_tab[i-run];
5673 5673
5674 if(score < best_score){ 5674 if(score < best_score){
5675 best_score= score; 5675 best_score= score;
5676 run_tab[i+1]= run; 5676 run_tab[i+1]= run;
5677 level_tab[i+1]= level-64; 5677 level_tab[i+1]= level-64;
5678 } 5678 }
5690 } 5690 }
5691 } 5691 }
5692 } 5692 }
5693 } 5693 }
5694 } 5694 }
5695 5695
5696 score_tab[i+1]= best_score; 5696 score_tab[i+1]= best_score;
5697 5697
5698 //Note: there is a vlc code in mpeg4 which is 1 bit shorter then another one with a shorter run and the same level 5698 //Note: there is a vlc code in mpeg4 which is 1 bit shorter then another one with a shorter run and the same level
5699 if(last_non_zero <= 27){ 5699 if(last_non_zero <= 27){
5700 for(; survivor_count; survivor_count--){ 5700 for(; survivor_count; survivor_count--){
5725 } 5725 }
5726 } 5726 }
5727 } 5727 }
5728 5728
5729 s->coded_score[n] = last_score; 5729 s->coded_score[n] = last_score;
5730 5730
5731 dc= ABS(block[0]); 5731 dc= ABS(block[0]);
5732 last_non_zero= last_i - 1; 5732 last_non_zero= last_i - 1;
5733 memset(block + start_i, 0, (64-start_i)*sizeof(DCTELEM)); 5733 memset(block + start_i, 0, (64-start_i)*sizeof(DCTELEM));
5734 5734
5735 if(last_non_zero < start_i) 5735 if(last_non_zero < start_i)
5736 return last_non_zero; 5736 return last_non_zero;
5737 5737
5738 if(last_non_zero == 0 && start_i == 0){ 5738 if(last_non_zero == 0 && start_i == 0){
5739 int best_level= 0; 5739 int best_level= 0;
5740 int best_score= dc * dc; 5740 int best_score= dc * dc;
5741 5741
5742 for(i=0; i<coeff_count[0]; i++){ 5742 for(i=0; i<coeff_count[0]; i++){
5743 int level= coeff[i][0]; 5743 int level= coeff[i][0];
5744 int alevel= ABS(level); 5744 int alevel= ABS(level);
5745 int unquant_coeff, score, distortion; 5745 int unquant_coeff, score, distortion;
5746 5746
5772 i= last_i; 5772 i= last_i;
5773 assert(last_level); 5773 assert(last_level);
5774 5774
5775 block[ perm_scantable[last_non_zero] ]= last_level; 5775 block[ perm_scantable[last_non_zero] ]= last_level;
5776 i -= last_run + 1; 5776 i -= last_run + 1;
5777 5777
5778 for(; i>start_i; i -= run_tab[i] + 1){ 5778 for(; i>start_i; i -= run_tab[i] + 1){
5779 block[ perm_scantable[i-1] ]= level_tab[i]; 5779 block[ perm_scantable[i-1] ]= level_tab[i];
5780 } 5780 }
5781 5781
5782 return last_non_zero; 5782 return last_non_zero;
5832 static int messed_sign=0; 5832 static int messed_sign=0;
5833 #endif 5833 #endif
5834 5834
5835 if(basis[0][0] == 0) 5835 if(basis[0][0] == 0)
5836 build_basis(s->dsp.idct_permutation); 5836 build_basis(s->dsp.idct_permutation);
5837 5837
5838 qmul= qscale*2; 5838 qmul= qscale*2;
5839 qadd= (qscale-1)|1; 5839 qadd= (qscale-1)|1;
5840 if (s->mb_intra) { 5840 if (s->mb_intra) {
5841 if (!s->h263_aic) { 5841 if (!s->h263_aic) {
5842 if (n < 4) 5842 if (n < 4)
5886 w= ABS(weight[i]) + qns*one; 5886 w= ABS(weight[i]) + qns*one;
5887 w= 15 + (48*qns*one + w/2)/w; // 16 .. 63 5887 w= 15 + (48*qns*one + w/2)/w; // 16 .. 63
5888 5888
5889 weight[i] = w; 5889 weight[i] = w;
5890 // w=weight[i] = (63*qns + (w/2)) / w; 5890 // w=weight[i] = (63*qns + (w/2)) / w;
5891 5891
5892 assert(w>0); 5892 assert(w>0);
5893 assert(w<(1<<6)); 5893 assert(w<(1<<6));
5894 sum += w*w; 5894 sum += w*w;
5895 } 5895 }
5896 lambda= sum*(uint64_t)s->lambda2 >> (FF_LAMBDA_SHIFT - 6 + 6 + 6 + 6); 5896 lambda= sum*(uint64_t)s->lambda2 >> (FF_LAMBDA_SHIFT - 6 + 6 + 6 + 6);
5901 rle_index=0; 5901 rle_index=0;
5902 for(i=start_i; i<=last_non_zero; i++){ 5902 for(i=start_i; i<=last_non_zero; i++){
5903 int j= perm_scantable[i]; 5903 int j= perm_scantable[i];
5904 const int level= block[j]; 5904 const int level= block[j];
5905 int coeff; 5905 int coeff;
5906 5906
5907 if(level){ 5907 if(level){
5908 if(level<0) coeff= qmul*level - qadd; 5908 if(level<0) coeff= qmul*level - qadd;
5909 else coeff= qmul*level + qadd; 5909 else coeff= qmul*level + qadd;
5910 run_tab[rle_index++]=run; 5910 run_tab[rle_index++]=run;
5911 run=0; 5911 run=0;
5937 #ifdef REFINE_STATS 5937 #ifdef REFINE_STATS
5938 {START_TIMER 5938 {START_TIMER
5939 #endif 5939 #endif
5940 for(i=0; i<64; i++){ 5940 for(i=0; i<64; i++){
5941 int w= weight[i]; 5941 int w= weight[i];
5942 5942
5943 d1[i] = (rem[i]*w*w + (1<<(RECON_SHIFT+12-1)))>>(RECON_SHIFT+12); 5943 d1[i] = (rem[i]*w*w + (1<<(RECON_SHIFT+12-1)))>>(RECON_SHIFT+12);
5944 } 5944 }
5945 #ifdef REFINE_STATS 5945 #ifdef REFINE_STATS
5946 STOP_TIMER("rem*w*w")} 5946 STOP_TIMER("rem*w*w")}
5947 {START_TIMER 5947 {START_TIMER
5955 if(start_i){ 5955 if(start_i){
5956 const int level= block[0]; 5956 const int level= block[0];
5957 int change, old_coeff; 5957 int change, old_coeff;
5958 5958
5959 assert(s->mb_intra); 5959 assert(s->mb_intra);
5960 5960
5961 old_coeff= q*level; 5961 old_coeff= q*level;
5962 5962
5963 for(change=-1; change<=1; change+=2){ 5963 for(change=-1; change<=1; change+=2){
5964 int new_level= level + change; 5964 int new_level= level + change;
5965 int score, new_coeff; 5965 int score, new_coeff;
5966 5966
5967 new_coeff= q*new_level; 5967 new_coeff= q*new_level;
5968 if(new_coeff >= 2048 || new_coeff < 0) 5968 if(new_coeff >= 2048 || new_coeff < 0)
5969 continue; 5969 continue;
5970 5970
5971 score= s->dsp.try_8x8basis(rem, weight, basis[0], new_coeff - old_coeff); 5971 score= s->dsp.try_8x8basis(rem, weight, basis[0], new_coeff - old_coeff);
5975 best_change= change; 5975 best_change= change;
5976 best_unquant_change= new_coeff - old_coeff; 5976 best_unquant_change= new_coeff - old_coeff;
5977 } 5977 }
5978 } 5978 }
5979 } 5979 }
5980 5980
5981 run=0; 5981 run=0;
5982 rle_index=0; 5982 rle_index=0;
5983 run2= run_tab[rle_index++]; 5983 run2= run_tab[rle_index++];
5984 prev_level=0; 5984 prev_level=0;
5985 prev_run=0; 5985 prev_run=0;
5999 }else{ 5999 }else{
6000 old_coeff=0; 6000 old_coeff=0;
6001 run2--; 6001 run2--;
6002 assert(run2>=0 || i >= last_non_zero ); 6002 assert(run2>=0 || i >= last_non_zero );
6003 } 6003 }
6004 6004
6005 for(change=-1; change<=1; change+=2){ 6005 for(change=-1; change<=1; change+=2){
6006 int new_level= level + change; 6006 int new_level= level + change;
6007 int score, new_coeff, unquant_change; 6007 int score, new_coeff, unquant_change;
6008 6008
6009 score=0; 6009 score=0;
6010 if(s->avctx->quantizer_noise_shaping < 2 && ABS(new_level) > ABS(level)) 6010 if(s->avctx->quantizer_noise_shaping < 2 && ABS(new_level) > ABS(level))
6011 continue; 6011 continue;
6012 6012
6013 if(new_level){ 6013 if(new_level){
6014 if(new_level<0) new_coeff= qmul*new_level - qadd; 6014 if(new_level<0) new_coeff= qmul*new_level - qadd;
6015 else new_coeff= qmul*new_level + qadd; 6015 else new_coeff= qmul*new_level + qadd;
6016 if(new_coeff >= 2048 || new_coeff <= -2048) 6016 if(new_coeff >= 2048 || new_coeff <= -2048)
6017 continue; 6017 continue;
6018 //FIXME check for overflow 6018 //FIXME check for overflow
6019 6019
6020 if(level){ 6020 if(level){
6021 if(level < 63 && level > -63){ 6021 if(level < 63 && level > -63){
6022 if(i < last_non_zero) 6022 if(i < last_non_zero)
6023 score += length[UNI_AC_ENC_INDEX(run, new_level+64)] 6023 score += length[UNI_AC_ENC_INDEX(run, new_level+64)]
6024 - length[UNI_AC_ENC_INDEX(run, level+64)]; 6024 - length[UNI_AC_ENC_INDEX(run, level+64)];
6026 score += last_length[UNI_AC_ENC_INDEX(run, new_level+64)] 6026 score += last_length[UNI_AC_ENC_INDEX(run, new_level+64)]
6027 - last_length[UNI_AC_ENC_INDEX(run, level+64)]; 6027 - last_length[UNI_AC_ENC_INDEX(run, level+64)];
6028 } 6028 }
6029 }else{ 6029 }else{
6030 assert(ABS(new_level)==1); 6030 assert(ABS(new_level)==1);
6031 6031
6032 if(analyze_gradient){ 6032 if(analyze_gradient){
6033 int g= d1[ scantable[i] ]; 6033 int g= d1[ scantable[i] ];
6034 if(g && (g^new_level) >= 0) 6034 if(g && (g^new_level) >= 0)
6035 continue; 6035 continue;
6036 } 6036 }
6037 6037
6038 if(i < last_non_zero){ 6038 if(i < last_non_zero){
6039 int next_i= i + run2 + 1; 6039 int next_i= i + run2 + 1;
6040 int next_level= block[ perm_scantable[next_i] ] + 64; 6040 int next_level= block[ perm_scantable[next_i] ] + 64;
6041 6041
6042 if(next_level&(~127)) 6042 if(next_level&(~127))
6043 next_level= 0; 6043 next_level= 0;
6044 6044
6045 if(next_i < last_non_zero) 6045 if(next_i < last_non_zero)
6046 score += length[UNI_AC_ENC_INDEX(run, 65)] 6046 score += length[UNI_AC_ENC_INDEX(run, 65)]
6063 assert(ABS(level)==1); 6063 assert(ABS(level)==1);
6064 6064
6065 if(i < last_non_zero){ 6065 if(i < last_non_zero){
6066 int next_i= i + run2 + 1; 6066 int next_i= i + run2 + 1;
6067 int next_level= block[ perm_scantable[next_i] ] + 64; 6067 int next_level= block[ perm_scantable[next_i] ] + 64;
6068 6068
6069 if(next_level&(~127)) 6069 if(next_level&(~127))
6070 next_level= 0; 6070 next_level= 0;
6071 6071
6072 if(next_i < last_non_zero) 6072 if(next_i < last_non_zero)
6073 score += length[UNI_AC_ENC_INDEX(run + run2 + 1, next_level)] 6073 score += length[UNI_AC_ENC_INDEX(run + run2 + 1, next_level)]
6083 score += last_length[UNI_AC_ENC_INDEX(prev_run, prev_level)] 6083 score += last_length[UNI_AC_ENC_INDEX(prev_run, prev_level)]
6084 - length[UNI_AC_ENC_INDEX(prev_run, prev_level)]; 6084 - length[UNI_AC_ENC_INDEX(prev_run, prev_level)];
6085 } 6085 }
6086 } 6086 }
6087 } 6087 }
6088 6088
6089 score *= lambda; 6089 score *= lambda;
6090 6090
6091 unquant_change= new_coeff - old_coeff; 6091 unquant_change= new_coeff - old_coeff;
6092 assert((score < 100*lambda && score > -100*lambda) || lambda==0); 6092 assert((score < 100*lambda && score > -100*lambda) || lambda==0);
6093 6093
6094 score+= s->dsp.try_8x8basis(rem, weight, basis[j], unquant_change); 6094 score+= s->dsp.try_8x8basis(rem, weight, basis[j], unquant_change);
6095 if(score<best_score){ 6095 if(score<best_score){
6096 best_score= score; 6096 best_score= score;
6097 best_coeff= i; 6097 best_coeff= i;
6098 best_change= change; 6098 best_change= change;
6113 STOP_TIMER("iterative step")} 6113 STOP_TIMER("iterative step")}
6114 #endif 6114 #endif
6115 6115
6116 if(best_change){ 6116 if(best_change){
6117 int j= perm_scantable[ best_coeff ]; 6117 int j= perm_scantable[ best_coeff ];
6118 6118
6119 block[j] += best_change; 6119 block[j] += best_change;
6120 6120
6121 if(best_coeff > last_non_zero){ 6121 if(best_coeff > last_non_zero){
6122 last_non_zero= best_coeff; 6122 last_non_zero= best_coeff;
6123 assert(block[j]); 6123 assert(block[j]);
6124 #ifdef REFINE_STATS 6124 #ifdef REFINE_STATS
6125 after_last++; 6125 after_last++;
6154 run=0; 6154 run=0;
6155 rle_index=0; 6155 rle_index=0;
6156 for(i=start_i; i<=last_non_zero; i++){ 6156 for(i=start_i; i<=last_non_zero; i++){
6157 int j= perm_scantable[i]; 6157 int j= perm_scantable[i];
6158 const int level= block[j]; 6158 const int level= block[j];
6159 6159
6160 if(level){ 6160 if(level){
6161 run_tab[rle_index++]=run; 6161 run_tab[rle_index++]=run;
6162 run=0; 6162 run=0;
6163 }else{ 6163 }else{
6164 run++; 6164 run++;
6165 } 6165 }
6166 } 6166 }
6167 6167
6168 s->dsp.add_8x8basis(rem, basis[j], best_unquant_change); 6168 s->dsp.add_8x8basis(rem, basis[j], best_unquant_change);
6169 }else{ 6169 }else{
6170 break; 6170 break;
6171 } 6171 }
6172 } 6172 }
6178 #endif 6178 #endif
6179 6179
6180 return last_non_zero; 6180 return last_non_zero;
6181 } 6181 }
6182 6182
6183 static int dct_quantize_c(MpegEncContext *s, 6183 static int dct_quantize_c(MpegEncContext *s,
6184 DCTELEM *block, int n, 6184 DCTELEM *block, int n,
6185 int qscale, int *overflow) 6185 int qscale, int *overflow)
6186 { 6186 {
6187 int i, j, level, last_non_zero, q, start_i; 6187 int i, j, level, last_non_zero, q, start_i;
6188 const int *qmat; 6188 const int *qmat;
6204 q = s->c_dc_scale; 6204 q = s->c_dc_scale;
6205 q = q << 3; 6205 q = q << 3;
6206 } else 6206 } else
6207 /* For AIC we skip quant/dequant of INTRADC */ 6207 /* For AIC we skip quant/dequant of INTRADC */
6208 q = 1 << 3; 6208 q = 1 << 3;
6209 6209
6210 /* note: block[0] is assumed to be positive */ 6210 /* note: block[0] is assumed to be positive */
6211 block[0] = (block[0] + (q >> 1)) / q; 6211 block[0] = (block[0] + (q >> 1)) / q;
6212 start_i = 1; 6212 start_i = 1;
6213 last_non_zero = 0; 6213 last_non_zero = 0;
6214 qmat = s->q_intra_matrix[qscale]; 6214 qmat = s->q_intra_matrix[qscale];
6250 }else{ 6250 }else{
6251 block[j]=0; 6251 block[j]=0;
6252 } 6252 }
6253 } 6253 }
6254 *overflow= s->max_qcoeff < max; //overflow might have happened 6254 *overflow= s->max_qcoeff < max; //overflow might have happened
6255 6255
6256 /* we need this permutation so that we correct the IDCT, we only permute the !=0 elements */ 6256 /* we need this permutation so that we correct the IDCT, we only permute the !=0 elements */
6257 if (s->dsp.idct_permutation_type != FF_NO_IDCT_PERM) 6257 if (s->dsp.idct_permutation_type != FF_NO_IDCT_PERM)
6258 ff_block_permute(block, s->dsp.idct_permutation, scantable, last_non_zero); 6258 ff_block_permute(block, s->dsp.idct_permutation, scantable, last_non_zero);
6259 6259
6260 return last_non_zero; 6260 return last_non_zero;
6261 } 6261 }
6262 6262
6263 #endif //CONFIG_ENCODERS 6263 #endif //CONFIG_ENCODERS
6264 6264
6265 static void dct_unquantize_mpeg1_intra_c(MpegEncContext *s, 6265 static void dct_unquantize_mpeg1_intra_c(MpegEncContext *s,
6266 DCTELEM *block, int n, int qscale) 6266 DCTELEM *block, int n, int qscale)
6267 { 6267 {
6268 int i, level, nCoeffs; 6268 int i, level, nCoeffs;
6269 const uint16_t *quant_matrix; 6269 const uint16_t *quant_matrix;
6270 6270
6271 nCoeffs= s->block_last_index[n]; 6271 nCoeffs= s->block_last_index[n];
6272 6272
6273 if (n < 4) 6273 if (n < 4)
6274 block[0] = block[0] * s->y_dc_scale; 6274 block[0] = block[0] * s->y_dc_scale;
6275 else 6275 else
6276 block[0] = block[0] * s->c_dc_scale; 6276 block[0] = block[0] * s->c_dc_scale;
6277 /* XXX: only mpeg1 */ 6277 /* XXX: only mpeg1 */
6278 quant_matrix = s->intra_matrix; 6278 quant_matrix = s->intra_matrix;
6292 block[j] = level; 6292 block[j] = level;
6293 } 6293 }
6294 } 6294 }
6295 } 6295 }
6296 6296
6297 static void dct_unquantize_mpeg1_inter_c(MpegEncContext *s, 6297 static void dct_unquantize_mpeg1_inter_c(MpegEncContext *s,
6298 DCTELEM *block, int n, int qscale) 6298 DCTELEM *block, int n, int qscale)
6299 { 6299 {
6300 int i, level, nCoeffs; 6300 int i, level, nCoeffs;
6301 const uint16_t *quant_matrix; 6301 const uint16_t *quant_matrix;
6302 6302
6303 nCoeffs= s->block_last_index[n]; 6303 nCoeffs= s->block_last_index[n];
6304 6304
6305 quant_matrix = s->inter_matrix; 6305 quant_matrix = s->inter_matrix;
6306 for(i=0; i<=nCoeffs; i++) { 6306 for(i=0; i<=nCoeffs; i++) {
6307 int j= s->intra_scantable.permutated[i]; 6307 int j= s->intra_scantable.permutated[i];
6308 level = block[j]; 6308 level = block[j];
6309 if (level) { 6309 if (level) {
6321 block[j] = level; 6321 block[j] = level;
6322 } 6322 }
6323 } 6323 }
6324 } 6324 }
6325 6325
6326 static void dct_unquantize_mpeg2_intra_c(MpegEncContext *s, 6326 static void dct_unquantize_mpeg2_intra_c(MpegEncContext *s,
6327 DCTELEM *block, int n, int qscale) 6327 DCTELEM *block, int n, int qscale)
6328 { 6328 {
6329 int i, level, nCoeffs; 6329 int i, level, nCoeffs;
6330 const uint16_t *quant_matrix; 6330 const uint16_t *quant_matrix;
6331 6331
6332 if(s->alternate_scan) nCoeffs= 63; 6332 if(s->alternate_scan) nCoeffs= 63;
6333 else nCoeffs= s->block_last_index[n]; 6333 else nCoeffs= s->block_last_index[n];
6334 6334
6335 if (n < 4) 6335 if (n < 4)
6336 block[0] = block[0] * s->y_dc_scale; 6336 block[0] = block[0] * s->y_dc_scale;
6337 else 6337 else
6338 block[0] = block[0] * s->c_dc_scale; 6338 block[0] = block[0] * s->c_dc_scale;
6339 quant_matrix = s->intra_matrix; 6339 quant_matrix = s->intra_matrix;
6340 for(i=1;i<=nCoeffs;i++) { 6340 for(i=1;i<=nCoeffs;i++) {
6351 block[j] = level; 6351 block[j] = level;
6352 } 6352 }
6353 } 6353 }
6354 } 6354 }
6355 6355
6356 static void dct_unquantize_mpeg2_inter_c(MpegEncContext *s, 6356 static void dct_unquantize_mpeg2_inter_c(MpegEncContext *s,
6357 DCTELEM *block, int n, int qscale) 6357 DCTELEM *block, int n, int qscale)
6358 { 6358 {
6359 int i, level, nCoeffs; 6359 int i, level, nCoeffs;
6360 const uint16_t *quant_matrix; 6360 const uint16_t *quant_matrix;
6361 int sum=-1; 6361 int sum=-1;
6362 6362
6363 if(s->alternate_scan) nCoeffs= 63; 6363 if(s->alternate_scan) nCoeffs= 63;
6364 else nCoeffs= s->block_last_index[n]; 6364 else nCoeffs= s->block_last_index[n];
6365 6365
6366 quant_matrix = s->inter_matrix; 6366 quant_matrix = s->inter_matrix;
6367 for(i=0; i<=nCoeffs; i++) { 6367 for(i=0; i<=nCoeffs; i++) {
6368 int j= s->intra_scantable.permutated[i]; 6368 int j= s->intra_scantable.permutated[i];
6369 level = block[j]; 6369 level = block[j];
6370 if (level) { 6370 if (level) {
6382 } 6382 }
6383 } 6383 }
6384 block[63]^=sum&1; 6384 block[63]^=sum&1;
6385 } 6385 }
6386 6386
6387 static void dct_unquantize_h263_intra_c(MpegEncContext *s, 6387 static void dct_unquantize_h263_intra_c(MpegEncContext *s,
6388 DCTELEM *block, int n, int qscale) 6388 DCTELEM *block, int n, int qscale)
6389 { 6389 {
6390 int i, level, qmul, qadd; 6390 int i, level, qmul, qadd;
6391 int nCoeffs; 6391 int nCoeffs;
6392 6392
6393 assert(s->block_last_index[n]>=0); 6393 assert(s->block_last_index[n]>=0);
6394 6394
6395 qmul = qscale << 1; 6395 qmul = qscale << 1;
6396 6396
6397 if (!s->h263_aic) { 6397 if (!s->h263_aic) {
6398 if (n < 4) 6398 if (n < 4)
6399 block[0] = block[0] * s->y_dc_scale; 6399 block[0] = block[0] * s->y_dc_scale;
6400 else 6400 else
6401 block[0] = block[0] * s->c_dc_scale; 6401 block[0] = block[0] * s->c_dc_scale;
6402 qadd = (qscale - 1) | 1; 6402 qadd = (qscale - 1) | 1;
6403 }else{ 6403 }else{
6419 block[i] = level; 6419 block[i] = level;
6420 } 6420 }
6421 } 6421 }
6422 } 6422 }
6423 6423
6424 static void dct_unquantize_h263_inter_c(MpegEncContext *s, 6424 static void dct_unquantize_h263_inter_c(MpegEncContext *s,
6425 DCTELEM *block, int n, int qscale) 6425 DCTELEM *block, int n, int qscale)
6426 { 6426 {
6427 int i, level, qmul, qadd; 6427 int i, level, qmul, qadd;
6428 int nCoeffs; 6428 int nCoeffs;
6429 6429
6430 assert(s->block_last_index[n]>=0); 6430 assert(s->block_last_index[n]>=0);
6431 6431
6432 qadd = (qscale - 1) | 1; 6432 qadd = (qscale - 1) | 1;
6433 qmul = qscale << 1; 6433 qmul = qscale << 1;
6434 6434
6435 nCoeffs= s->inter_scantable.raster_end[ s->block_last_index[n] ]; 6435 nCoeffs= s->inter_scantable.raster_end[ s->block_last_index[n] ];
6436 6436
6437 for(i=0; i<=nCoeffs; i++) { 6437 for(i=0; i<=nCoeffs; i++) {
6438 level = block[i]; 6438 level = block[i];
6439 if (level) { 6439 if (level) {