Mercurial > libavcodec.hg
comparison mpegvideo.c @ 5204:1c3baf4323bb libavcodec
Description: split mpegvideo.c
author | benoit |
---|---|
date | Tue, 03 Jul 2007 09:55:56 +0000 |
parents | 830576859f71 |
children | 413c5e2eff68 |
comparison
equal
deleted
inserted
replaced
5203:d71791d72286 | 5204:1c3baf4323bb |
---|---|
28 */ | 28 */ |
29 | 29 |
30 #include "avcodec.h" | 30 #include "avcodec.h" |
31 #include "dsputil.h" | 31 #include "dsputil.h" |
32 #include "mpegvideo.h" | 32 #include "mpegvideo.h" |
33 #include "mpegvideo_common.h" | |
33 #include "mjpegenc.h" | 34 #include "mjpegenc.h" |
34 #include "msmpeg4.h" | 35 #include "msmpeg4.h" |
35 #include "faandct.h" | 36 #include "faandct.h" |
36 #include <limits.h> | 37 #include <limits.h> |
37 | 38 |
38 //#undef NDEBUG | 39 //#undef NDEBUG |
39 //#include <assert.h> | 40 //#include <assert.h> |
40 | 41 |
41 #ifdef CONFIG_ENCODERS | |
42 static int encode_picture(MpegEncContext *s, int picture_number); | |
43 #endif //CONFIG_ENCODERS | |
44 static void dct_unquantize_mpeg1_intra_c(MpegEncContext *s, | 42 static void dct_unquantize_mpeg1_intra_c(MpegEncContext *s, |
45 DCTELEM *block, int n, int qscale); | 43 DCTELEM *block, int n, int qscale); |
46 static void dct_unquantize_mpeg1_inter_c(MpegEncContext *s, | 44 static void dct_unquantize_mpeg1_inter_c(MpegEncContext *s, |
47 DCTELEM *block, int n, int qscale); | 45 DCTELEM *block, int n, int qscale); |
48 static void dct_unquantize_mpeg2_intra_c(MpegEncContext *s, | 46 static void dct_unquantize_mpeg2_intra_c(MpegEncContext *s, |
54 static void dct_unquantize_h263_intra_c(MpegEncContext *s, | 52 static void dct_unquantize_h263_intra_c(MpegEncContext *s, |
55 DCTELEM *block, int n, int qscale); | 53 DCTELEM *block, int n, int qscale); |
56 static void dct_unquantize_h263_inter_c(MpegEncContext *s, | 54 static void dct_unquantize_h263_inter_c(MpegEncContext *s, |
57 DCTELEM *block, int n, int qscale); | 55 DCTELEM *block, int n, int qscale); |
58 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); |
59 #ifdef CONFIG_ENCODERS | |
60 static int dct_quantize_c(MpegEncContext *s, DCTELEM *block, int n, int qscale, int *overflow); | |
61 static int dct_quantize_trellis_c(MpegEncContext *s, DCTELEM *block, int n, int qscale, int *overflow); | |
62 static int dct_quantize_refine(MpegEncContext *s, DCTELEM *block, int16_t *weight, DCTELEM *orig, int n, int qscale); | |
63 static int sse_mb(MpegEncContext *s); | |
64 static void denoise_dct_c(MpegEncContext *s, DCTELEM *block); | |
65 #endif //CONFIG_ENCODERS | |
66 | 57 |
67 #ifdef HAVE_XVMC | 58 #ifdef HAVE_XVMC |
68 extern int XVMC_field_start(MpegEncContext*s, AVCodecContext *avctx); | 59 extern int XVMC_field_start(MpegEncContext*s, AVCodecContext *avctx); |
69 extern void XVMC_field_end(MpegEncContext *s); | 60 extern void XVMC_field_end(MpegEncContext *s); |
70 extern void XVMC_decode_mb(MpegEncContext *s); | 61 extern void XVMC_decode_mb(MpegEncContext *s); |
77 //#define PARANOID | 68 //#define PARANOID |
78 | 69 |
79 //#define DEBUG | 70 //#define DEBUG |
80 | 71 |
81 | 72 |
82 #ifdef CONFIG_ENCODERS | |
83 static const uint16_t aanscales[64] = { | |
84 /* precomputed values scaled up by 14 bits */ | |
85 16384, 22725, 21407, 19266, 16384, 12873, 8867, 4520, | |
86 22725, 31521, 29692, 26722, 22725, 17855, 12299, 6270, | |
87 21407, 29692, 27969, 25172, 21407, 16819, 11585, 5906, | |
88 19266, 26722, 25172, 22654, 19266, 15137, 10426, 5315, | |
89 16384, 22725, 21407, 19266, 16384, 12873, 8867, 4520, | |
90 12873, 17855, 16819, 15137, 12873, 10114, 6967, 3552, | |
91 8867 , 12299, 11585, 10426, 8867, 6967, 4799, 2446, | |
92 4520 , 6270, 5906, 5315, 4520, 3552, 2446, 1247 | |
93 }; | |
94 #endif //CONFIG_ENCODERS | |
95 | |
96 static const uint8_t h263_chroma_roundtab[16] = { | 73 static const uint8_t h263_chroma_roundtab[16] = { |
97 // 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | 74 // 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
98 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2, | 75 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2, |
99 }; | 76 }; |
100 | 77 |
101 static const uint8_t ff_default_chroma_qscale_table[32]={ | 78 static const uint8_t ff_default_chroma_qscale_table[32]={ |
102 // 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 | 79 // 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
103 0, 1, 2, 3, 4, 5, 6, 7, 8, 9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31 | 80 0, 1, 2, 3, 4, 5, 6, 7, 8, 9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31 |
104 }; | 81 }; |
105 | |
106 #ifdef CONFIG_ENCODERS | |
107 static uint8_t default_mv_penalty[MAX_FCODE+1][MAX_MV*2+1]; | |
108 static uint8_t default_fcode_tab[MAX_MV*2+1]; | |
109 | |
110 enum PixelFormat ff_yuv420p_list[2]= {PIX_FMT_YUV420P, -1}; | |
111 | |
112 static void convert_matrix(DSPContext *dsp, int (*qmat)[64], uint16_t (*qmat16)[2][64], | |
113 const uint16_t *quant_matrix, int bias, int qmin, int qmax, int intra) | |
114 { | |
115 int qscale; | |
116 int shift=0; | |
117 | |
118 for(qscale=qmin; qscale<=qmax; qscale++){ | |
119 int i; | |
120 if (dsp->fdct == ff_jpeg_fdct_islow | |
121 #ifdef FAAN_POSTSCALE | |
122 || dsp->fdct == ff_faandct | |
123 #endif | |
124 ) { | |
125 for(i=0;i<64;i++) { | |
126 const int j= dsp->idct_permutation[i]; | |
127 /* 16 <= qscale * quant_matrix[i] <= 7905 */ | |
128 /* 19952 <= aanscales[i] * qscale * quant_matrix[i] <= 249205026 */ | |
129 /* (1<<36)/19952 >= (1<<36)/(aanscales[i] * qscale * quant_matrix[i]) >= (1<<36)/249205026 */ | |
130 /* 3444240 >= (1<<36)/(aanscales[i] * qscale * quant_matrix[i]) >= 275 */ | |
131 | |
132 qmat[qscale][i] = (int)((UINT64_C(1) << QMAT_SHIFT) / | |
133 (qscale * quant_matrix[j])); | |
134 } | |
135 } else if (dsp->fdct == fdct_ifast | |
136 #ifndef FAAN_POSTSCALE | |
137 || dsp->fdct == ff_faandct | |
138 #endif | |
139 ) { | |
140 for(i=0;i<64;i++) { | |
141 const int j= dsp->idct_permutation[i]; | |
142 /* 16 <= qscale * quant_matrix[i] <= 7905 */ | |
143 /* 19952 <= aanscales[i] * qscale * quant_matrix[i] <= 249205026 */ | |
144 /* (1<<36)/19952 >= (1<<36)/(aanscales[i] * qscale * quant_matrix[i]) >= (1<<36)/249205026 */ | |
145 /* 3444240 >= (1<<36)/(aanscales[i] * qscale * quant_matrix[i]) >= 275 */ | |
146 | |
147 qmat[qscale][i] = (int)((UINT64_C(1) << (QMAT_SHIFT + 14)) / | |
148 (aanscales[i] * qscale * quant_matrix[j])); | |
149 } | |
150 } else { | |
151 for(i=0;i<64;i++) { | |
152 const int j= dsp->idct_permutation[i]; | |
153 /* We can safely suppose that 16 <= quant_matrix[i] <= 255 | |
154 So 16 <= qscale * quant_matrix[i] <= 7905 | |
155 so (1<<19) / 16 >= (1<<19) / (qscale * quant_matrix[i]) >= (1<<19) / 7905 | |
156 so 32768 >= (1<<19) / (qscale * quant_matrix[i]) >= 67 | |
157 */ | |
158 qmat[qscale][i] = (int)((UINT64_C(1) << QMAT_SHIFT) / (qscale * quant_matrix[j])); | |
159 // qmat [qscale][i] = (1 << QMAT_SHIFT_MMX) / (qscale * quant_matrix[i]); | |
160 qmat16[qscale][0][i] = (1 << QMAT_SHIFT_MMX) / (qscale * quant_matrix[j]); | |
161 | |
162 if(qmat16[qscale][0][i]==0 || qmat16[qscale][0][i]==128*256) qmat16[qscale][0][i]=128*256-1; | |
163 qmat16[qscale][1][i]= ROUNDED_DIV(bias<<(16-QUANT_BIAS_SHIFT), qmat16[qscale][0][i]); | |
164 } | |
165 } | |
166 | |
167 for(i=intra; i<64; i++){ | |
168 int64_t max= 8191; | |
169 if (dsp->fdct == fdct_ifast | |
170 #ifndef FAAN_POSTSCALE | |
171 || dsp->fdct == ff_faandct | |
172 #endif | |
173 ) { | |
174 max= (8191LL*aanscales[i]) >> 14; | |
175 } | |
176 while(((max * qmat[qscale][i]) >> shift) > INT_MAX){ | |
177 shift++; | |
178 } | |
179 } | |
180 } | |
181 if(shift){ | |
182 av_log(NULL, AV_LOG_INFO, "Warning, QMAT_SHIFT is larger than %d, overflows possible\n", QMAT_SHIFT - shift); | |
183 } | |
184 } | |
185 | |
186 static inline void update_qscale(MpegEncContext *s){ | |
187 s->qscale= (s->lambda*139 + FF_LAMBDA_SCALE*64) >> (FF_LAMBDA_SHIFT + 7); | |
188 s->qscale= av_clip(s->qscale, s->avctx->qmin, s->avctx->qmax); | |
189 | |
190 s->lambda2= (s->lambda*s->lambda + FF_LAMBDA_SCALE/2) >> FF_LAMBDA_SHIFT; | |
191 } | |
192 #endif //CONFIG_ENCODERS | |
193 | 82 |
194 void ff_init_scantable(uint8_t *permutation, ScanTable *st, const uint8_t *src_scantable){ | 83 void ff_init_scantable(uint8_t *permutation, ScanTable *st, const uint8_t *src_scantable){ |
195 int i; | 84 int i; |
196 int end; | 85 int end; |
197 | 86 |
212 j = st->permutated[i]; | 101 j = st->permutated[i]; |
213 if(j>end) end=j; | 102 if(j>end) end=j; |
214 st->raster_end[i]= end; | 103 st->raster_end[i]= end; |
215 } | 104 } |
216 } | 105 } |
217 | |
218 #ifdef CONFIG_ENCODERS | |
219 void ff_write_quant_matrix(PutBitContext *pb, uint16_t *matrix){ | |
220 int i; | |
221 | |
222 if(matrix){ | |
223 put_bits(pb, 1, 1); | |
224 for(i=0;i<64;i++) { | |
225 put_bits(pb, 8, matrix[ ff_zigzag_direct[i] ]); | |
226 } | |
227 }else | |
228 put_bits(pb, 1, 0); | |
229 } | |
230 #endif //CONFIG_ENCODERS | |
231 | 106 |
232 const uint8_t *ff_find_start_code(const uint8_t * restrict p, const uint8_t *end, uint32_t * restrict state){ | 107 const uint8_t *ff_find_start_code(const uint8_t * restrict p, const uint8_t *end, uint32_t * restrict state){ |
233 int i; | 108 int i; |
234 | 109 |
235 assert(p<=end); | 110 assert(p<=end); |
314 ff_init_scantable(s->dsp.idct_permutation, &s->intra_v_scantable, ff_alternate_vertical_scan); | 189 ff_init_scantable(s->dsp.idct_permutation, &s->intra_v_scantable, ff_alternate_vertical_scan); |
315 | 190 |
316 return 0; | 191 return 0; |
317 } | 192 } |
318 | 193 |
319 static void copy_picture(Picture *dst, Picture *src){ | 194 void copy_picture(Picture *dst, Picture *src){ |
320 *dst = *src; | 195 *dst = *src; |
321 dst->type= FF_BUFFER_TYPE_COPY; | 196 dst->type= FF_BUFFER_TYPE_COPY; |
322 } | 197 } |
323 | |
324 #ifdef CONFIG_ENCODERS | |
325 static void copy_picture_attributes(MpegEncContext *s, AVFrame *dst, AVFrame *src){ | |
326 int i; | |
327 | |
328 dst->pict_type = src->pict_type; | |
329 dst->quality = src->quality; | |
330 dst->coded_picture_number = src->coded_picture_number; | |
331 dst->display_picture_number = src->display_picture_number; | |
332 // dst->reference = src->reference; | |
333 dst->pts = src->pts; | |
334 dst->interlaced_frame = src->interlaced_frame; | |
335 dst->top_field_first = src->top_field_first; | |
336 | |
337 if(s->avctx->me_threshold){ | |
338 if(!src->motion_val[0]) | |
339 av_log(s->avctx, AV_LOG_ERROR, "AVFrame.motion_val not set!\n"); | |
340 if(!src->mb_type) | |
341 av_log(s->avctx, AV_LOG_ERROR, "AVFrame.mb_type not set!\n"); | |
342 if(!src->ref_index[0]) | |
343 av_log(s->avctx, AV_LOG_ERROR, "AVFrame.ref_index not set!\n"); | |
344 if(src->motion_subsample_log2 != dst->motion_subsample_log2) | |
345 av_log(s->avctx, AV_LOG_ERROR, "AVFrame.motion_subsample_log2 doesn't match! (%d!=%d)\n", | |
346 src->motion_subsample_log2, dst->motion_subsample_log2); | |
347 | |
348 memcpy(dst->mb_type, src->mb_type, s->mb_stride * s->mb_height * sizeof(dst->mb_type[0])); | |
349 | |
350 for(i=0; i<2; i++){ | |
351 int stride= ((16*s->mb_width )>>src->motion_subsample_log2) + 1; | |
352 int height= ((16*s->mb_height)>>src->motion_subsample_log2); | |
353 | |
354 if(src->motion_val[i] && src->motion_val[i] != dst->motion_val[i]){ | |
355 memcpy(dst->motion_val[i], src->motion_val[i], 2*stride*height*sizeof(int16_t)); | |
356 } | |
357 if(src->ref_index[i] && src->ref_index[i] != dst->ref_index[i]){ | |
358 memcpy(dst->ref_index[i], src->ref_index[i], s->b8_stride*2*s->mb_height*sizeof(int8_t)); | |
359 } | |
360 } | |
361 } | |
362 } | |
363 #endif | |
364 | 198 |
365 /** | 199 /** |
366 * allocates a Picture | 200 * allocates a Picture |
367 * The pixels are allocated/set by calling get_buffer() if shared=0 | 201 * The pixels are allocated/set by calling get_buffer() if shared=0 |
368 */ | 202 */ |
369 static int alloc_picture(MpegEncContext *s, Picture *pic, int shared){ | 203 int alloc_picture(MpegEncContext *s, Picture *pic, int shared){ |
370 const int big_mb_num= s->mb_stride*(s->mb_height+1) + 1; //the +1 is needed so memset(,,stride*height) does not sig11 | 204 const int big_mb_num= s->mb_stride*(s->mb_height+1) + 1; //the +1 is needed so memset(,,stride*height) does not sig11 |
371 const int mb_array_size= s->mb_stride*s->mb_height; | 205 const int mb_array_size= s->mb_stride*s->mb_height; |
372 const int b8_array_size= s->b8_stride*s->mb_height*2; | 206 const int b8_array_size= s->b8_stride*s->mb_height*2; |
373 const int b4_array_size= s->b4_stride*s->mb_height*4; | 207 const int b4_array_size= s->b4_stride*s->mb_height*4; |
374 int i; | 208 int i; |
561 dst->pblocks[i] = (short *)(&dst->block[i]); | 395 dst->pblocks[i] = (short *)(&dst->block[i]); |
562 } | 396 } |
563 //STOP_TIMER("update_duplicate_context") //about 10k cycles / 0.01 sec for 1000frames on 1ghz with 2 threads | 397 //STOP_TIMER("update_duplicate_context") //about 10k cycles / 0.01 sec for 1000frames on 1ghz with 2 threads |
564 } | 398 } |
565 | 399 |
566 #ifdef CONFIG_ENCODERS | |
567 static void update_duplicate_context_after_me(MpegEncContext *dst, MpegEncContext *src){ | |
568 #define COPY(a) dst->a= src->a | |
569 COPY(pict_type); | |
570 COPY(current_picture); | |
571 COPY(f_code); | |
572 COPY(b_code); | |
573 COPY(qscale); | |
574 COPY(lambda); | |
575 COPY(lambda2); | |
576 COPY(picture_in_gop_number); | |
577 COPY(gop_picture_number); | |
578 COPY(frame_pred_frame_dct); //FIXME don't set in encode_header | |
579 COPY(progressive_frame); //FIXME don't set in encode_header | |
580 COPY(partitioned_frame); //FIXME don't set in encode_header | |
581 #undef COPY | |
582 } | |
583 #endif | |
584 | |
585 /** | 400 /** |
586 * sets the given MpegEncContext to common defaults (same for encoding and decoding). | 401 * sets the given MpegEncContext to common defaults (same for encoding and decoding). |
587 * the changed fields will not depend upon the prior state of the MpegEncContext. | 402 * the changed fields will not depend upon the prior state of the MpegEncContext. |
588 */ | 403 */ |
589 static void MPV_common_defaults(MpegEncContext *s){ | 404 void MPV_common_defaults(MpegEncContext *s){ |
590 s->y_dc_scale_table= | 405 s->y_dc_scale_table= |
591 s->c_dc_scale_table= ff_mpeg1_dc_scale_table; | 406 s->c_dc_scale_table= ff_mpeg1_dc_scale_table; |
592 s->chroma_qscale_table= ff_default_chroma_qscale_table; | 407 s->chroma_qscale_table= ff_default_chroma_qscale_table; |
593 s->progressive_frame= 1; | 408 s->progressive_frame= 1; |
594 s->progressive_sequence= 1; | 409 s->progressive_sequence= 1; |
609 * the changed fields will not depend upon the prior state of the MpegEncContext. | 424 * the changed fields will not depend upon the prior state of the MpegEncContext. |
610 */ | 425 */ |
611 void MPV_decode_defaults(MpegEncContext *s){ | 426 void MPV_decode_defaults(MpegEncContext *s){ |
612 MPV_common_defaults(s); | 427 MPV_common_defaults(s); |
613 } | 428 } |
614 | |
615 /** | |
616 * sets the given MpegEncContext to defaults for encoding. | |
617 * the changed fields will not depend upon the prior state of the MpegEncContext. | |
618 */ | |
619 | |
620 #ifdef CONFIG_ENCODERS | |
621 static void MPV_encode_defaults(MpegEncContext *s){ | |
622 int i; | |
623 MPV_common_defaults(s); | |
624 | |
625 for(i=-16; i<16; i++){ | |
626 default_fcode_tab[i + MAX_MV]= 1; | |
627 } | |
628 s->me.mv_penalty= default_mv_penalty; | |
629 s->fcode_tab= default_fcode_tab; | |
630 } | |
631 #endif //CONFIG_ENCODERS | |
632 | 429 |
633 /** | 430 /** |
634 * init common structure for both encoder and decoder. | 431 * init common structure for both encoder and decoder. |
635 * this assumes that some variables like width/height are already set | 432 * this assumes that some variables like width/height are already set |
636 */ | 433 */ |
904 for(i=0; i<3; i++) | 701 for(i=0; i<3; i++) |
905 av_freep(&s->visualization_buffer[i]); | 702 av_freep(&s->visualization_buffer[i]); |
906 | 703 |
907 avcodec_default_free_buffers(s->avctx); | 704 avcodec_default_free_buffers(s->avctx); |
908 } | 705 } |
909 | |
910 #ifdef CONFIG_ENCODERS | |
911 | |
912 /* init video encoder */ | |
913 int MPV_encode_init(AVCodecContext *avctx) | |
914 { | |
915 MpegEncContext *s = avctx->priv_data; | |
916 int i; | |
917 int chroma_h_shift, chroma_v_shift; | |
918 | |
919 MPV_encode_defaults(s); | |
920 | |
921 switch (avctx->codec_id) { | |
922 case CODEC_ID_MPEG2VIDEO: | |
923 if(avctx->pix_fmt != PIX_FMT_YUV420P && avctx->pix_fmt != PIX_FMT_YUV422P){ | |
924 av_log(avctx, AV_LOG_ERROR, "only YUV420 and YUV422 are supported\n"); | |
925 return -1; | |
926 } | |
927 break; | |
928 case CODEC_ID_LJPEG: | |
929 case CODEC_ID_MJPEG: | |
930 if(avctx->pix_fmt != PIX_FMT_YUVJ420P && avctx->pix_fmt != PIX_FMT_YUVJ422P && | |
931 ((avctx->pix_fmt != PIX_FMT_YUV420P && avctx->pix_fmt != PIX_FMT_YUV422P) || avctx->strict_std_compliance>FF_COMPLIANCE_INOFFICIAL)){ | |
932 av_log(avctx, AV_LOG_ERROR, "colorspace not supported in jpeg\n"); | |
933 return -1; | |
934 } | |
935 break; | |
936 default: | |
937 if(avctx->pix_fmt != PIX_FMT_YUV420P){ | |
938 av_log(avctx, AV_LOG_ERROR, "only YUV420 is supported\n"); | |
939 return -1; | |
940 } | |
941 } | |
942 | |
943 switch (avctx->pix_fmt) { | |
944 case PIX_FMT_YUVJ422P: | |
945 case PIX_FMT_YUV422P: | |
946 s->chroma_format = CHROMA_422; | |
947 break; | |
948 case PIX_FMT_YUVJ420P: | |
949 case PIX_FMT_YUV420P: | |
950 default: | |
951 s->chroma_format = CHROMA_420; | |
952 break; | |
953 } | |
954 | |
955 s->bit_rate = avctx->bit_rate; | |
956 s->width = avctx->width; | |
957 s->height = avctx->height; | |
958 if(avctx->gop_size > 600 && avctx->strict_std_compliance>FF_COMPLIANCE_EXPERIMENTAL){ | |
959 av_log(avctx, AV_LOG_ERROR, "Warning keyframe interval too large! reducing it ...\n"); | |
960 avctx->gop_size=600; | |
961 } | |
962 s->gop_size = avctx->gop_size; | |
963 s->avctx = avctx; | |
964 s->flags= avctx->flags; | |
965 s->flags2= avctx->flags2; | |
966 s->max_b_frames= avctx->max_b_frames; | |
967 s->codec_id= avctx->codec->id; | |
968 s->luma_elim_threshold = avctx->luma_elim_threshold; | |
969 s->chroma_elim_threshold= avctx->chroma_elim_threshold; | |
970 s->strict_std_compliance= avctx->strict_std_compliance; | |
971 s->data_partitioning= avctx->flags & CODEC_FLAG_PART; | |
972 s->quarter_sample= (avctx->flags & CODEC_FLAG_QPEL)!=0; | |
973 s->mpeg_quant= avctx->mpeg_quant; | |
974 s->rtp_mode= !!avctx->rtp_payload_size; | |
975 s->intra_dc_precision= avctx->intra_dc_precision; | |
976 s->user_specified_pts = AV_NOPTS_VALUE; | |
977 | |
978 if (s->gop_size <= 1) { | |
979 s->intra_only = 1; | |
980 s->gop_size = 12; | |
981 } else { | |
982 s->intra_only = 0; | |
983 } | |
984 | |
985 s->me_method = avctx->me_method; | |
986 | |
987 /* Fixed QSCALE */ | |
988 s->fixed_qscale = !!(avctx->flags & CODEC_FLAG_QSCALE); | |
989 | |
990 s->adaptive_quant= ( s->avctx->lumi_masking | |
991 || s->avctx->dark_masking | |
992 || s->avctx->temporal_cplx_masking | |
993 || s->avctx->spatial_cplx_masking | |
994 || s->avctx->p_masking | |
995 || s->avctx->border_masking | |
996 || (s->flags&CODEC_FLAG_QP_RD)) | |
997 && !s->fixed_qscale; | |
998 | |
999 s->obmc= !!(s->flags & CODEC_FLAG_OBMC); | |
1000 s->loop_filter= !!(s->flags & CODEC_FLAG_LOOP_FILTER); | |
1001 s->alternate_scan= !!(s->flags & CODEC_FLAG_ALT_SCAN); | |
1002 s->intra_vlc_format= !!(s->flags2 & CODEC_FLAG2_INTRA_VLC); | |
1003 s->q_scale_type= !!(s->flags2 & CODEC_FLAG2_NON_LINEAR_QUANT); | |
1004 | |
1005 if(avctx->rc_max_rate && !avctx->rc_buffer_size){ | |
1006 av_log(avctx, AV_LOG_ERROR, "a vbv buffer size is needed, for encoding with a maximum bitrate\n"); | |
1007 return -1; | |
1008 } | |
1009 | |
1010 if(avctx->rc_min_rate && avctx->rc_max_rate != avctx->rc_min_rate){ | |
1011 av_log(avctx, AV_LOG_INFO, "Warning min_rate > 0 but min_rate != max_rate isn't recommended!\n"); | |
1012 } | |
1013 | |
1014 if(avctx->rc_min_rate && avctx->rc_min_rate > avctx->bit_rate){ | |
1015 av_log(avctx, AV_LOG_ERROR, "bitrate below min bitrate\n"); | |
1016 return -1; | |
1017 } | |
1018 | |
1019 if(avctx->rc_max_rate && avctx->rc_max_rate < avctx->bit_rate){ | |
1020 av_log(avctx, AV_LOG_INFO, "bitrate above max bitrate\n"); | |
1021 return -1; | |
1022 } | |
1023 | |
1024 if(avctx->rc_buffer_size && avctx->bit_rate*av_q2d(avctx->time_base) > avctx->rc_buffer_size){ | |
1025 av_log(avctx, AV_LOG_ERROR, "VBV buffer too small for bitrate\n"); | |
1026 return -1; | |
1027 } | |
1028 | |
1029 if(avctx->bit_rate*av_q2d(avctx->time_base) > avctx->bit_rate_tolerance){ | |
1030 av_log(avctx, AV_LOG_ERROR, "bitrate tolerance too small for bitrate\n"); | |
1031 return -1; | |
1032 } | |
1033 | |
1034 if( s->avctx->rc_max_rate && s->avctx->rc_min_rate == s->avctx->rc_max_rate | |
1035 && (s->codec_id == CODEC_ID_MPEG1VIDEO || s->codec_id == CODEC_ID_MPEG2VIDEO) | |
1036 && 90000LL * (avctx->rc_buffer_size-1) > s->avctx->rc_max_rate*0xFFFFLL){ | |
1037 | |
1038 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"); | |
1039 } | |
1040 | |
1041 if((s->flags & CODEC_FLAG_4MV) && s->codec_id != CODEC_ID_MPEG4 | |
1042 && s->codec_id != CODEC_ID_H263 && s->codec_id != CODEC_ID_H263P && s->codec_id != CODEC_ID_FLV1){ | |
1043 av_log(avctx, AV_LOG_ERROR, "4MV not supported by codec\n"); | |
1044 return -1; | |
1045 } | |
1046 | |
1047 if(s->obmc && s->avctx->mb_decision != FF_MB_DECISION_SIMPLE){ | |
1048 av_log(avctx, AV_LOG_ERROR, "OBMC is only supported with simple mb decision\n"); | |
1049 return -1; | |
1050 } | |
1051 | |
1052 if(s->obmc && s->codec_id != CODEC_ID_H263 && s->codec_id != CODEC_ID_H263P){ | |
1053 av_log(avctx, AV_LOG_ERROR, "OBMC is only supported with H263(+)\n"); | |
1054 return -1; | |
1055 } | |
1056 | |
1057 if(s->quarter_sample && s->codec_id != CODEC_ID_MPEG4){ | |
1058 av_log(avctx, AV_LOG_ERROR, "qpel not supported by codec\n"); | |
1059 return -1; | |
1060 } | |
1061 | |
1062 if(s->data_partitioning && s->codec_id != CODEC_ID_MPEG4){ | |
1063 av_log(avctx, AV_LOG_ERROR, "data partitioning not supported by codec\n"); | |
1064 return -1; | |
1065 } | |
1066 | |
1067 if(s->max_b_frames && s->codec_id != CODEC_ID_MPEG4 && s->codec_id != CODEC_ID_MPEG1VIDEO && s->codec_id != CODEC_ID_MPEG2VIDEO){ | |
1068 av_log(avctx, AV_LOG_ERROR, "b frames not supported by codec\n"); | |
1069 return -1; | |
1070 } | |
1071 | |
1072 if((s->flags & (CODEC_FLAG_INTERLACED_DCT|CODEC_FLAG_INTERLACED_ME|CODEC_FLAG_ALT_SCAN)) | |
1073 && s->codec_id != CODEC_ID_MPEG4 && s->codec_id != CODEC_ID_MPEG2VIDEO){ | |
1074 av_log(avctx, AV_LOG_ERROR, "interlacing not supported by codec\n"); | |
1075 return -1; | |
1076 } | |
1077 | |
1078 if(s->mpeg_quant && s->codec_id != CODEC_ID_MPEG4){ //FIXME mpeg2 uses that too | |
1079 av_log(avctx, AV_LOG_ERROR, "mpeg2 style quantization not supported by codec\n"); | |
1080 return -1; | |
1081 } | |
1082 | |
1083 if((s->flags & CODEC_FLAG_CBP_RD) && !(s->flags & CODEC_FLAG_TRELLIS_QUANT)){ | |
1084 av_log(avctx, AV_LOG_ERROR, "CBP RD needs trellis quant\n"); | |
1085 return -1; | |
1086 } | |
1087 | |
1088 if((s->flags & CODEC_FLAG_QP_RD) && s->avctx->mb_decision != FF_MB_DECISION_RD){ | |
1089 av_log(avctx, AV_LOG_ERROR, "QP RD needs mbd=2\n"); | |
1090 return -1; | |
1091 } | |
1092 | |
1093 if(s->avctx->scenechange_threshold < 1000000000 && (s->flags & CODEC_FLAG_CLOSED_GOP)){ | |
1094 av_log(avctx, AV_LOG_ERROR, "closed gop with scene change detection arent supported yet, set threshold to 1000000000\n"); | |
1095 return -1; | |
1096 } | |
1097 | |
1098 if((s->flags2 & CODEC_FLAG2_INTRA_VLC) && s->codec_id != CODEC_ID_MPEG2VIDEO){ | |
1099 av_log(avctx, AV_LOG_ERROR, "intra vlc table not supported by codec\n"); | |
1100 return -1; | |
1101 } | |
1102 | |
1103 if(s->flags & CODEC_FLAG_LOW_DELAY){ | |
1104 if (s->codec_id != CODEC_ID_MPEG2VIDEO && s->codec_id != CODEC_ID_MPEG1VIDEO){ | |
1105 av_log(avctx, AV_LOG_ERROR, "low delay forcing is only available for mpeg1/2\n"); | |
1106 return -1; | |
1107 } | |
1108 if (s->max_b_frames != 0){ | |
1109 av_log(avctx, AV_LOG_ERROR, "b frames cannot be used with low delay\n"); | |
1110 return -1; | |
1111 } | |
1112 } | |
1113 | |
1114 if(s->q_scale_type == 1){ | |
1115 if(s->codec_id != CODEC_ID_MPEG2VIDEO){ | |
1116 av_log(avctx, AV_LOG_ERROR, "non linear quant is only available for mpeg2\n"); | |
1117 return -1; | |
1118 } | |
1119 if(avctx->qmax > 12){ | |
1120 av_log(avctx, AV_LOG_ERROR, "non linear quant only supports qmax <= 12 currently\n"); | |
1121 return -1; | |
1122 } | |
1123 } | |
1124 | |
1125 if(s->avctx->thread_count > 1 && s->codec_id != CODEC_ID_MPEG4 | |
1126 && s->codec_id != CODEC_ID_MPEG1VIDEO && s->codec_id != CODEC_ID_MPEG2VIDEO | |
1127 && (s->codec_id != CODEC_ID_H263P || !(s->flags & CODEC_FLAG_H263P_SLICE_STRUCT))){ | |
1128 av_log(avctx, AV_LOG_ERROR, "multi threaded encoding not supported by codec\n"); | |
1129 return -1; | |
1130 } | |
1131 | |
1132 if(s->avctx->thread_count > 1) | |
1133 s->rtp_mode= 1; | |
1134 | |
1135 if(!avctx->time_base.den || !avctx->time_base.num){ | |
1136 av_log(avctx, AV_LOG_ERROR, "framerate not set\n"); | |
1137 return -1; | |
1138 } | |
1139 | |
1140 i= (INT_MAX/2+128)>>8; | |
1141 if(avctx->me_threshold >= i){ | |
1142 av_log(avctx, AV_LOG_ERROR, "me_threshold too large, max is %d\n", i - 1); | |
1143 return -1; | |
1144 } | |
1145 if(avctx->mb_threshold >= i){ | |
1146 av_log(avctx, AV_LOG_ERROR, "mb_threshold too large, max is %d\n", i - 1); | |
1147 return -1; | |
1148 } | |
1149 | |
1150 if(avctx->b_frame_strategy && (avctx->flags&CODEC_FLAG_PASS2)){ | |
1151 av_log(avctx, AV_LOG_INFO, "notice: b_frame_strategy only affects the first pass\n"); | |
1152 avctx->b_frame_strategy = 0; | |
1153 } | |
1154 | |
1155 i= ff_gcd(avctx->time_base.den, avctx->time_base.num); | |
1156 if(i > 1){ | |
1157 av_log(avctx, AV_LOG_INFO, "removing common factors from framerate\n"); | |
1158 avctx->time_base.den /= i; | |
1159 avctx->time_base.num /= i; | |
1160 // return -1; | |
1161 } | |
1162 | |
1163 if(s->codec_id==CODEC_ID_MJPEG){ | |
1164 s->intra_quant_bias= 1<<(QUANT_BIAS_SHIFT-1); //(a + x/2)/x | |
1165 s->inter_quant_bias= 0; | |
1166 }else if(s->mpeg_quant || s->codec_id==CODEC_ID_MPEG1VIDEO || s->codec_id==CODEC_ID_MPEG2VIDEO){ | |
1167 s->intra_quant_bias= 3<<(QUANT_BIAS_SHIFT-3); //(a + x*3/8)/x | |
1168 s->inter_quant_bias= 0; | |
1169 }else{ | |
1170 s->intra_quant_bias=0; | |
1171 s->inter_quant_bias=-(1<<(QUANT_BIAS_SHIFT-2)); //(a - x/4)/x | |
1172 } | |
1173 | |
1174 if(avctx->intra_quant_bias != FF_DEFAULT_QUANT_BIAS) | |
1175 s->intra_quant_bias= avctx->intra_quant_bias; | |
1176 if(avctx->inter_quant_bias != FF_DEFAULT_QUANT_BIAS) | |
1177 s->inter_quant_bias= avctx->inter_quant_bias; | |
1178 | |
1179 avcodec_get_chroma_sub_sample(avctx->pix_fmt, &chroma_h_shift, &chroma_v_shift); | |
1180 | |
1181 if(avctx->codec_id == CODEC_ID_MPEG4 && s->avctx->time_base.den > (1<<16)-1){ | |
1182 av_log(avctx, AV_LOG_ERROR, "timebase not supported by mpeg 4 standard\n"); | |
1183 return -1; | |
1184 } | |
1185 s->time_increment_bits = av_log2(s->avctx->time_base.den - 1) + 1; | |
1186 | |
1187 switch(avctx->codec->id) { | |
1188 case CODEC_ID_MPEG1VIDEO: | |
1189 s->out_format = FMT_MPEG1; | |
1190 s->low_delay= !!(s->flags & CODEC_FLAG_LOW_DELAY); | |
1191 avctx->delay= s->low_delay ? 0 : (s->max_b_frames + 1); | |
1192 break; | |
1193 case CODEC_ID_MPEG2VIDEO: | |
1194 s->out_format = FMT_MPEG1; | |
1195 s->low_delay= !!(s->flags & CODEC_FLAG_LOW_DELAY); | |
1196 avctx->delay= s->low_delay ? 0 : (s->max_b_frames + 1); | |
1197 s->rtp_mode= 1; | |
1198 break; | |
1199 case CODEC_ID_LJPEG: | |
1200 case CODEC_ID_MJPEG: | |
1201 s->out_format = FMT_MJPEG; | |
1202 s->intra_only = 1; /* force intra only for jpeg */ | |
1203 s->mjpeg_vsample[0] = 2; | |
1204 s->mjpeg_vsample[1] = 2>>chroma_v_shift; | |
1205 s->mjpeg_vsample[2] = 2>>chroma_v_shift; | |
1206 s->mjpeg_hsample[0] = 2; | |
1207 s->mjpeg_hsample[1] = 2>>chroma_h_shift; | |
1208 s->mjpeg_hsample[2] = 2>>chroma_h_shift; | |
1209 if (!(ENABLE_MJPEG_ENCODER || ENABLE_LJPEG_ENCODER) | |
1210 || ff_mjpeg_encode_init(s) < 0) | |
1211 return -1; | |
1212 avctx->delay=0; | |
1213 s->low_delay=1; | |
1214 break; | |
1215 case CODEC_ID_H261: | |
1216 if (!ENABLE_H261_ENCODER) return -1; | |
1217 if (ff_h261_get_picture_format(s->width, s->height) < 0) { | |
1218 av_log(avctx, AV_LOG_ERROR, "The specified picture size of %dx%d is not valid for the H.261 codec.\nValid sizes are 176x144, 352x288\n", s->width, s->height); | |
1219 return -1; | |
1220 } | |
1221 s->out_format = FMT_H261; | |
1222 avctx->delay=0; | |
1223 s->low_delay=1; | |
1224 break; | |
1225 case CODEC_ID_H263: | |
1226 if (h263_get_picture_format(s->width, s->height) == 7) { | |
1227 av_log(avctx, AV_LOG_INFO, "The specified picture size of %dx%d is not valid for the H.263 codec.\nValid sizes are 128x96, 176x144, 352x288, 704x576, and 1408x1152. Try H.263+.\n", s->width, s->height); | |
1228 return -1; | |
1229 } | |
1230 s->out_format = FMT_H263; | |
1231 s->obmc= (avctx->flags & CODEC_FLAG_OBMC) ? 1:0; | |
1232 avctx->delay=0; | |
1233 s->low_delay=1; | |
1234 break; | |
1235 case CODEC_ID_H263P: | |
1236 s->out_format = FMT_H263; | |
1237 s->h263_plus = 1; | |
1238 /* Fx */ | |
1239 s->umvplus = (avctx->flags & CODEC_FLAG_H263P_UMV) ? 1:0; | |
1240 s->h263_aic= (avctx->flags & CODEC_FLAG_AC_PRED) ? 1:0; | |
1241 s->modified_quant= s->h263_aic; | |
1242 s->alt_inter_vlc= (avctx->flags & CODEC_FLAG_H263P_AIV) ? 1:0; | |
1243 s->obmc= (avctx->flags & CODEC_FLAG_OBMC) ? 1:0; | |
1244 s->loop_filter= (avctx->flags & CODEC_FLAG_LOOP_FILTER) ? 1:0; | |
1245 s->unrestricted_mv= s->obmc || s->loop_filter || s->umvplus; | |
1246 s->h263_slice_structured= (s->flags & CODEC_FLAG_H263P_SLICE_STRUCT) ? 1:0; | |
1247 | |
1248 /* /Fx */ | |
1249 /* These are just to be sure */ | |
1250 avctx->delay=0; | |
1251 s->low_delay=1; | |
1252 break; | |
1253 case CODEC_ID_FLV1: | |
1254 s->out_format = FMT_H263; | |
1255 s->h263_flv = 2; /* format = 1; 11-bit codes */ | |
1256 s->unrestricted_mv = 1; | |
1257 s->rtp_mode=0; /* don't allow GOB */ | |
1258 avctx->delay=0; | |
1259 s->low_delay=1; | |
1260 break; | |
1261 case CODEC_ID_RV10: | |
1262 s->out_format = FMT_H263; | |
1263 avctx->delay=0; | |
1264 s->low_delay=1; | |
1265 break; | |
1266 case CODEC_ID_RV20: | |
1267 s->out_format = FMT_H263; | |
1268 avctx->delay=0; | |
1269 s->low_delay=1; | |
1270 s->modified_quant=1; | |
1271 s->h263_aic=1; | |
1272 s->h263_plus=1; | |
1273 s->loop_filter=1; | |
1274 s->unrestricted_mv= s->obmc || s->loop_filter || s->umvplus; | |
1275 break; | |
1276 case CODEC_ID_MPEG4: | |
1277 s->out_format = FMT_H263; | |
1278 s->h263_pred = 1; | |
1279 s->unrestricted_mv = 1; | |
1280 s->low_delay= s->max_b_frames ? 0 : 1; | |
1281 avctx->delay= s->low_delay ? 0 : (s->max_b_frames + 1); | |
1282 break; | |
1283 case CODEC_ID_MSMPEG4V1: | |
1284 s->out_format = FMT_H263; | |
1285 s->h263_msmpeg4 = 1; | |
1286 s->h263_pred = 1; | |
1287 s->unrestricted_mv = 1; | |
1288 s->msmpeg4_version= 1; | |
1289 avctx->delay=0; | |
1290 s->low_delay=1; | |
1291 break; | |
1292 case CODEC_ID_MSMPEG4V2: | |
1293 s->out_format = FMT_H263; | |
1294 s->h263_msmpeg4 = 1; | |
1295 s->h263_pred = 1; | |
1296 s->unrestricted_mv = 1; | |
1297 s->msmpeg4_version= 2; | |
1298 avctx->delay=0; | |
1299 s->low_delay=1; | |
1300 break; | |
1301 case CODEC_ID_MSMPEG4V3: | |
1302 s->out_format = FMT_H263; | |
1303 s->h263_msmpeg4 = 1; | |
1304 s->h263_pred = 1; | |
1305 s->unrestricted_mv = 1; | |
1306 s->msmpeg4_version= 3; | |
1307 s->flipflop_rounding=1; | |
1308 avctx->delay=0; | |
1309 s->low_delay=1; | |
1310 break; | |
1311 case CODEC_ID_WMV1: | |
1312 s->out_format = FMT_H263; | |
1313 s->h263_msmpeg4 = 1; | |
1314 s->h263_pred = 1; | |
1315 s->unrestricted_mv = 1; | |
1316 s->msmpeg4_version= 4; | |
1317 s->flipflop_rounding=1; | |
1318 avctx->delay=0; | |
1319 s->low_delay=1; | |
1320 break; | |
1321 case CODEC_ID_WMV2: | |
1322 s->out_format = FMT_H263; | |
1323 s->h263_msmpeg4 = 1; | |
1324 s->h263_pred = 1; | |
1325 s->unrestricted_mv = 1; | |
1326 s->msmpeg4_version= 5; | |
1327 s->flipflop_rounding=1; | |
1328 avctx->delay=0; | |
1329 s->low_delay=1; | |
1330 break; | |
1331 default: | |
1332 return -1; | |
1333 } | |
1334 | |
1335 avctx->has_b_frames= !s->low_delay; | |
1336 | |
1337 s->encoding = 1; | |
1338 | |
1339 /* init */ | |
1340 if (MPV_common_init(s) < 0) | |
1341 return -1; | |
1342 | |
1343 if(s->modified_quant) | |
1344 s->chroma_qscale_table= ff_h263_chroma_qscale_table; | |
1345 s->progressive_frame= | |
1346 s->progressive_sequence= !(avctx->flags & (CODEC_FLAG_INTERLACED_DCT|CODEC_FLAG_INTERLACED_ME|CODEC_FLAG_ALT_SCAN)); | |
1347 s->quant_precision=5; | |
1348 | |
1349 ff_set_cmp(&s->dsp, s->dsp.ildct_cmp, s->avctx->ildct_cmp); | |
1350 ff_set_cmp(&s->dsp, s->dsp.frame_skip_cmp, s->avctx->frame_skip_cmp); | |
1351 | |
1352 if (ENABLE_H261_ENCODER && s->out_format == FMT_H261) | |
1353 ff_h261_encode_init(s); | |
1354 if (s->out_format == FMT_H263) | |
1355 h263_encode_init(s); | |
1356 if (ENABLE_MSMPEG4_ENCODER && s->msmpeg4_version) | |
1357 ff_msmpeg4_encode_init(s); | |
1358 if (s->out_format == FMT_MPEG1) | |
1359 ff_mpeg1_encode_init(s); | |
1360 | |
1361 /* init q matrix */ | |
1362 for(i=0;i<64;i++) { | |
1363 int j= s->dsp.idct_permutation[i]; | |
1364 if(s->codec_id==CODEC_ID_MPEG4 && s->mpeg_quant){ | |
1365 s->intra_matrix[j] = ff_mpeg4_default_intra_matrix[i]; | |
1366 s->inter_matrix[j] = ff_mpeg4_default_non_intra_matrix[i]; | |
1367 }else if(s->out_format == FMT_H263 || s->out_format == FMT_H261){ | |
1368 s->intra_matrix[j] = | |
1369 s->inter_matrix[j] = ff_mpeg1_default_non_intra_matrix[i]; | |
1370 }else | |
1371 { /* mpeg1/2 */ | |
1372 s->intra_matrix[j] = ff_mpeg1_default_intra_matrix[i]; | |
1373 s->inter_matrix[j] = ff_mpeg1_default_non_intra_matrix[i]; | |
1374 } | |
1375 if(s->avctx->intra_matrix) | |
1376 s->intra_matrix[j] = s->avctx->intra_matrix[i]; | |
1377 if(s->avctx->inter_matrix) | |
1378 s->inter_matrix[j] = s->avctx->inter_matrix[i]; | |
1379 } | |
1380 | |
1381 /* precompute matrix */ | |
1382 /* for mjpeg, we do include qscale in the matrix */ | |
1383 if (s->out_format != FMT_MJPEG) { | |
1384 convert_matrix(&s->dsp, s->q_intra_matrix, s->q_intra_matrix16, | |
1385 s->intra_matrix, s->intra_quant_bias, avctx->qmin, 31, 1); | |
1386 convert_matrix(&s->dsp, s->q_inter_matrix, s->q_inter_matrix16, | |
1387 s->inter_matrix, s->inter_quant_bias, avctx->qmin, 31, 0); | |
1388 } | |
1389 | |
1390 if(ff_rate_control_init(s) < 0) | |
1391 return -1; | |
1392 | |
1393 return 0; | |
1394 } | |
1395 | |
1396 int MPV_encode_end(AVCodecContext *avctx) | |
1397 { | |
1398 MpegEncContext *s = avctx->priv_data; | |
1399 | |
1400 ff_rate_control_uninit(s); | |
1401 | |
1402 MPV_common_end(s); | |
1403 if ((ENABLE_MJPEG_ENCODER || ENABLE_LJPEG_ENCODER) && s->out_format == FMT_MJPEG) | |
1404 ff_mjpeg_encode_close(s); | |
1405 | |
1406 av_freep(&avctx->extradata); | |
1407 | |
1408 return 0; | |
1409 } | |
1410 | |
1411 #endif //CONFIG_ENCODERS | |
1412 | 706 |
1413 void init_rl(RLTable *rl, uint8_t static_store[2][2*MAX_RUN + MAX_LEVEL + 3]) | 707 void init_rl(RLTable *rl, uint8_t static_store[2][2*MAX_RUN + MAX_LEVEL + 3]) |
1414 { | 708 { |
1415 int8_t max_level[MAX_RUN+1], max_run[MAX_LEVEL+1]; | 709 int8_t max_level[MAX_RUN+1], max_run[MAX_LEVEL+1]; |
1416 uint8_t index_run[MAX_RUN+1]; | 710 uint8_t index_run[MAX_RUN+1]; |
2046 } | 1340 } |
2047 } | 1341 } |
2048 } | 1342 } |
2049 } | 1343 } |
2050 | 1344 |
2051 #ifdef CONFIG_ENCODERS | |
2052 | |
2053 static int get_sae(uint8_t *src, int ref, int stride){ | |
2054 int x,y; | |
2055 int acc=0; | |
2056 | |
2057 for(y=0; y<16; y++){ | |
2058 for(x=0; x<16; x++){ | |
2059 acc+= FFABS(src[x+y*stride] - ref); | |
2060 } | |
2061 } | |
2062 | |
2063 return acc; | |
2064 } | |
2065 | |
2066 static int get_intra_count(MpegEncContext *s, uint8_t *src, uint8_t *ref, int stride){ | |
2067 int x, y, w, h; | |
2068 int acc=0; | |
2069 | |
2070 w= s->width &~15; | |
2071 h= s->height&~15; | |
2072 | |
2073 for(y=0; y<h; y+=16){ | |
2074 for(x=0; x<w; x+=16){ | |
2075 int offset= x + y*stride; | |
2076 int sad = s->dsp.sad[0](NULL, src + offset, ref + offset, stride, 16); | |
2077 int mean= (s->dsp.pix_sum(src + offset, stride) + 128)>>8; | |
2078 int sae = get_sae(src + offset, mean, stride); | |
2079 | |
2080 acc+= sae + 500 < sad; | |
2081 } | |
2082 } | |
2083 return acc; | |
2084 } | |
2085 | |
2086 | |
2087 static int load_input_picture(MpegEncContext *s, AVFrame *pic_arg){ | |
2088 AVFrame *pic=NULL; | |
2089 int64_t pts; | |
2090 int i; | |
2091 const int encoding_delay= s->max_b_frames; | |
2092 int direct=1; | |
2093 | |
2094 if(pic_arg){ | |
2095 pts= pic_arg->pts; | |
2096 pic_arg->display_picture_number= s->input_picture_number++; | |
2097 | |
2098 if(pts != AV_NOPTS_VALUE){ | |
2099 if(s->user_specified_pts != AV_NOPTS_VALUE){ | |
2100 int64_t time= pts; | |
2101 int64_t last= s->user_specified_pts; | |
2102 | |
2103 if(time <= last){ | |
2104 av_log(s->avctx, AV_LOG_ERROR, "Error, Invalid timestamp=%"PRId64", last=%"PRId64"\n", pts, s->user_specified_pts); | |
2105 return -1; | |
2106 } | |
2107 } | |
2108 s->user_specified_pts= pts; | |
2109 }else{ | |
2110 if(s->user_specified_pts != AV_NOPTS_VALUE){ | |
2111 s->user_specified_pts= | |
2112 pts= s->user_specified_pts + 1; | |
2113 av_log(s->avctx, AV_LOG_INFO, "Warning: AVFrame.pts=? trying to guess (%"PRId64")\n", pts); | |
2114 }else{ | |
2115 pts= pic_arg->display_picture_number; | |
2116 } | |
2117 } | |
2118 } | |
2119 | |
2120 if(pic_arg){ | |
2121 if(encoding_delay && !(s->flags&CODEC_FLAG_INPUT_PRESERVED)) direct=0; | |
2122 if(pic_arg->linesize[0] != s->linesize) direct=0; | |
2123 if(pic_arg->linesize[1] != s->uvlinesize) direct=0; | |
2124 if(pic_arg->linesize[2] != s->uvlinesize) direct=0; | |
2125 | |
2126 // av_log(AV_LOG_DEBUG, "%d %d %d %d\n",pic_arg->linesize[0], pic_arg->linesize[1], s->linesize, s->uvlinesize); | |
2127 | |
2128 if(direct){ | |
2129 i= ff_find_unused_picture(s, 1); | |
2130 | |
2131 pic= (AVFrame*)&s->picture[i]; | |
2132 pic->reference= 3; | |
2133 | |
2134 for(i=0; i<4; i++){ | |
2135 pic->data[i]= pic_arg->data[i]; | |
2136 pic->linesize[i]= pic_arg->linesize[i]; | |
2137 } | |
2138 alloc_picture(s, (Picture*)pic, 1); | |
2139 }else{ | |
2140 i= ff_find_unused_picture(s, 0); | |
2141 | |
2142 pic= (AVFrame*)&s->picture[i]; | |
2143 pic->reference= 3; | |
2144 | |
2145 alloc_picture(s, (Picture*)pic, 0); | |
2146 | |
2147 if( pic->data[0] + INPLACE_OFFSET == pic_arg->data[0] | |
2148 && pic->data[1] + INPLACE_OFFSET == pic_arg->data[1] | |
2149 && pic->data[2] + INPLACE_OFFSET == pic_arg->data[2]){ | |
2150 // empty | |
2151 }else{ | |
2152 int h_chroma_shift, v_chroma_shift; | |
2153 avcodec_get_chroma_sub_sample(s->avctx->pix_fmt, &h_chroma_shift, &v_chroma_shift); | |
2154 | |
2155 for(i=0; i<3; i++){ | |
2156 int src_stride= pic_arg->linesize[i]; | |
2157 int dst_stride= i ? s->uvlinesize : s->linesize; | |
2158 int h_shift= i ? h_chroma_shift : 0; | |
2159 int v_shift= i ? v_chroma_shift : 0; | |
2160 int w= s->width >>h_shift; | |
2161 int h= s->height>>v_shift; | |
2162 uint8_t *src= pic_arg->data[i]; | |
2163 uint8_t *dst= pic->data[i]; | |
2164 | |
2165 if(!s->avctx->rc_buffer_size) | |
2166 dst +=INPLACE_OFFSET; | |
2167 | |
2168 if(src_stride==dst_stride) | |
2169 memcpy(dst, src, src_stride*h); | |
2170 else{ | |
2171 while(h--){ | |
2172 memcpy(dst, src, w); | |
2173 dst += dst_stride; | |
2174 src += src_stride; | |
2175 } | |
2176 } | |
2177 } | |
2178 } | |
2179 } | |
2180 copy_picture_attributes(s, pic, pic_arg); | |
2181 pic->pts= pts; //we set this here to avoid modifiying pic_arg | |
2182 } | |
2183 | |
2184 /* shift buffer entries */ | |
2185 for(i=1; i<MAX_PICTURE_COUNT /*s->encoding_delay+1*/; i++) | |
2186 s->input_picture[i-1]= s->input_picture[i]; | |
2187 | |
2188 s->input_picture[encoding_delay]= (Picture*)pic; | |
2189 | |
2190 return 0; | |
2191 } | |
2192 | |
2193 static int skip_check(MpegEncContext *s, Picture *p, Picture *ref){ | |
2194 int x, y, plane; | |
2195 int score=0; | |
2196 int64_t score64=0; | |
2197 | |
2198 for(plane=0; plane<3; plane++){ | |
2199 const int stride= p->linesize[plane]; | |
2200 const int bw= plane ? 1 : 2; | |
2201 for(y=0; y<s->mb_height*bw; y++){ | |
2202 for(x=0; x<s->mb_width*bw; x++){ | |
2203 int off= p->type == FF_BUFFER_TYPE_SHARED ? 0: 16; | |
2204 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); | |
2205 | |
2206 switch(s->avctx->frame_skip_exp){ | |
2207 case 0: score= FFMAX(score, v); break; | |
2208 case 1: score+= FFABS(v);break; | |
2209 case 2: score+= v*v;break; | |
2210 case 3: score64+= FFABS(v*v*(int64_t)v);break; | |
2211 case 4: score64+= v*v*(int64_t)(v*v);break; | |
2212 } | |
2213 } | |
2214 } | |
2215 } | |
2216 | |
2217 if(score) score64= score; | |
2218 | |
2219 if(score64 < s->avctx->frame_skip_threshold) | |
2220 return 1; | |
2221 if(score64 < ((s->avctx->frame_skip_factor * (int64_t)s->lambda)>>8)) | |
2222 return 1; | |
2223 return 0; | |
2224 } | |
2225 | |
2226 static int estimate_best_b_count(MpegEncContext *s){ | |
2227 AVCodec *codec= avcodec_find_encoder(s->avctx->codec_id); | |
2228 AVCodecContext *c= avcodec_alloc_context(); | |
2229 AVFrame input[FF_MAX_B_FRAMES+2]; | |
2230 const int scale= s->avctx->brd_scale; | |
2231 int i, j, out_size, p_lambda, b_lambda, lambda2; | |
2232 int outbuf_size= s->width * s->height; //FIXME | |
2233 uint8_t *outbuf= av_malloc(outbuf_size); | |
2234 int64_t best_rd= INT64_MAX; | |
2235 int best_b_count= -1; | |
2236 | |
2237 assert(scale>=0 && scale <=3); | |
2238 | |
2239 // emms_c(); | |
2240 p_lambda= s->last_lambda_for[P_TYPE]; //s->next_picture_ptr->quality; | |
2241 b_lambda= s->last_lambda_for[B_TYPE]; //p_lambda *FFABS(s->avctx->b_quant_factor) + s->avctx->b_quant_offset; | |
2242 if(!b_lambda) b_lambda= p_lambda; //FIXME we should do this somewhere else | |
2243 lambda2= (b_lambda*b_lambda + (1<<FF_LAMBDA_SHIFT)/2 ) >> FF_LAMBDA_SHIFT; | |
2244 | |
2245 c->width = s->width >> scale; | |
2246 c->height= s->height>> scale; | |
2247 c->flags= CODEC_FLAG_QSCALE | CODEC_FLAG_PSNR | CODEC_FLAG_INPUT_PRESERVED /*| CODEC_FLAG_EMU_EDGE*/; | |
2248 c->flags|= s->avctx->flags & CODEC_FLAG_QPEL; | |
2249 c->mb_decision= s->avctx->mb_decision; | |
2250 c->me_cmp= s->avctx->me_cmp; | |
2251 c->mb_cmp= s->avctx->mb_cmp; | |
2252 c->me_sub_cmp= s->avctx->me_sub_cmp; | |
2253 c->pix_fmt = PIX_FMT_YUV420P; | |
2254 c->time_base= s->avctx->time_base; | |
2255 c->max_b_frames= s->max_b_frames; | |
2256 | |
2257 if (avcodec_open(c, codec) < 0) | |
2258 return -1; | |
2259 | |
2260 for(i=0; i<s->max_b_frames+2; i++){ | |
2261 int ysize= c->width*c->height; | |
2262 int csize= (c->width/2)*(c->height/2); | |
2263 Picture pre_input, *pre_input_ptr= i ? s->input_picture[i-1] : s->next_picture_ptr; | |
2264 | |
2265 avcodec_get_frame_defaults(&input[i]); | |
2266 input[i].data[0]= av_malloc(ysize + 2*csize); | |
2267 input[i].data[1]= input[i].data[0] + ysize; | |
2268 input[i].data[2]= input[i].data[1] + csize; | |
2269 input[i].linesize[0]= c->width; | |
2270 input[i].linesize[1]= | |
2271 input[i].linesize[2]= c->width/2; | |
2272 | |
2273 if(pre_input_ptr && (!i || s->input_picture[i-1])) { | |
2274 pre_input= *pre_input_ptr; | |
2275 | |
2276 if(pre_input.type != FF_BUFFER_TYPE_SHARED && i) { | |
2277 pre_input.data[0]+=INPLACE_OFFSET; | |
2278 pre_input.data[1]+=INPLACE_OFFSET; | |
2279 pre_input.data[2]+=INPLACE_OFFSET; | |
2280 } | |
2281 | |
2282 s->dsp.shrink[scale](input[i].data[0], input[i].linesize[0], pre_input.data[0], pre_input.linesize[0], c->width, c->height); | |
2283 s->dsp.shrink[scale](input[i].data[1], input[i].linesize[1], pre_input.data[1], pre_input.linesize[1], c->width>>1, c->height>>1); | |
2284 s->dsp.shrink[scale](input[i].data[2], input[i].linesize[2], pre_input.data[2], pre_input.linesize[2], c->width>>1, c->height>>1); | |
2285 } | |
2286 } | |
2287 | |
2288 for(j=0; j<s->max_b_frames+1; j++){ | |
2289 int64_t rd=0; | |
2290 | |
2291 if(!s->input_picture[j]) | |
2292 break; | |
2293 | |
2294 c->error[0]= c->error[1]= c->error[2]= 0; | |
2295 | |
2296 input[0].pict_type= I_TYPE; | |
2297 input[0].quality= 1 * FF_QP2LAMBDA; | |
2298 out_size = avcodec_encode_video(c, outbuf, outbuf_size, &input[0]); | |
2299 // rd += (out_size * lambda2) >> FF_LAMBDA_SHIFT; | |
2300 | |
2301 for(i=0; i<s->max_b_frames+1; i++){ | |
2302 int is_p= i % (j+1) == j || i==s->max_b_frames; | |
2303 | |
2304 input[i+1].pict_type= is_p ? P_TYPE : B_TYPE; | |
2305 input[i+1].quality= is_p ? p_lambda : b_lambda; | |
2306 out_size = avcodec_encode_video(c, outbuf, outbuf_size, &input[i+1]); | |
2307 rd += (out_size * lambda2) >> (FF_LAMBDA_SHIFT - 3); | |
2308 } | |
2309 | |
2310 /* get the delayed frames */ | |
2311 while(out_size){ | |
2312 out_size = avcodec_encode_video(c, outbuf, outbuf_size, NULL); | |
2313 rd += (out_size * lambda2) >> (FF_LAMBDA_SHIFT - 3); | |
2314 } | |
2315 | |
2316 rd += c->error[0] + c->error[1] + c->error[2]; | |
2317 | |
2318 if(rd < best_rd){ | |
2319 best_rd= rd; | |
2320 best_b_count= j; | |
2321 } | |
2322 } | |
2323 | |
2324 av_freep(&outbuf); | |
2325 avcodec_close(c); | |
2326 av_freep(&c); | |
2327 | |
2328 for(i=0; i<s->max_b_frames+2; i++){ | |
2329 av_freep(&input[i].data[0]); | |
2330 } | |
2331 | |
2332 return best_b_count; | |
2333 } | |
2334 | |
2335 static void select_input_picture(MpegEncContext *s){ | |
2336 int i; | |
2337 | |
2338 for(i=1; i<MAX_PICTURE_COUNT; i++) | |
2339 s->reordered_input_picture[i-1]= s->reordered_input_picture[i]; | |
2340 s->reordered_input_picture[MAX_PICTURE_COUNT-1]= NULL; | |
2341 | |
2342 /* set next picture type & ordering */ | |
2343 if(s->reordered_input_picture[0]==NULL && s->input_picture[0]){ | |
2344 if(/*s->picture_in_gop_number >= s->gop_size ||*/ s->next_picture_ptr==NULL || s->intra_only){ | |
2345 s->reordered_input_picture[0]= s->input_picture[0]; | |
2346 s->reordered_input_picture[0]->pict_type= I_TYPE; | |
2347 s->reordered_input_picture[0]->coded_picture_number= s->coded_picture_number++; | |
2348 }else{ | |
2349 int b_frames; | |
2350 | |
2351 if(s->avctx->frame_skip_threshold || s->avctx->frame_skip_factor){ | |
2352 if(s->picture_in_gop_number < s->gop_size && skip_check(s, s->input_picture[0], s->next_picture_ptr)){ | |
2353 //FIXME check that te gop check above is +-1 correct | |
2354 //av_log(NULL, AV_LOG_DEBUG, "skip %p %"PRId64"\n", s->input_picture[0]->data[0], s->input_picture[0]->pts); | |
2355 | |
2356 if(s->input_picture[0]->type == FF_BUFFER_TYPE_SHARED){ | |
2357 for(i=0; i<4; i++) | |
2358 s->input_picture[0]->data[i]= NULL; | |
2359 s->input_picture[0]->type= 0; | |
2360 }else{ | |
2361 assert( s->input_picture[0]->type==FF_BUFFER_TYPE_USER | |
2362 || s->input_picture[0]->type==FF_BUFFER_TYPE_INTERNAL); | |
2363 | |
2364 s->avctx->release_buffer(s->avctx, (AVFrame*)s->input_picture[0]); | |
2365 } | |
2366 | |
2367 emms_c(); | |
2368 ff_vbv_update(s, 0); | |
2369 | |
2370 goto no_output_pic; | |
2371 } | |
2372 } | |
2373 | |
2374 if(s->flags&CODEC_FLAG_PASS2){ | |
2375 for(i=0; i<s->max_b_frames+1; i++){ | |
2376 int pict_num= s->input_picture[0]->display_picture_number + i; | |
2377 | |
2378 if(pict_num >= s->rc_context.num_entries) | |
2379 break; | |
2380 if(!s->input_picture[i]){ | |
2381 s->rc_context.entry[pict_num-1].new_pict_type = P_TYPE; | |
2382 break; | |
2383 } | |
2384 | |
2385 s->input_picture[i]->pict_type= | |
2386 s->rc_context.entry[pict_num].new_pict_type; | |
2387 } | |
2388 } | |
2389 | |
2390 if(s->avctx->b_frame_strategy==0){ | |
2391 b_frames= s->max_b_frames; | |
2392 while(b_frames && !s->input_picture[b_frames]) b_frames--; | |
2393 }else if(s->avctx->b_frame_strategy==1){ | |
2394 for(i=1; i<s->max_b_frames+1; i++){ | |
2395 if(s->input_picture[i] && s->input_picture[i]->b_frame_score==0){ | |
2396 s->input_picture[i]->b_frame_score= | |
2397 get_intra_count(s, s->input_picture[i ]->data[0], | |
2398 s->input_picture[i-1]->data[0], s->linesize) + 1; | |
2399 } | |
2400 } | |
2401 for(i=0; i<s->max_b_frames+1; i++){ | |
2402 if(s->input_picture[i]==NULL || s->input_picture[i]->b_frame_score - 1 > s->mb_num/s->avctx->b_sensitivity) break; | |
2403 } | |
2404 | |
2405 b_frames= FFMAX(0, i-1); | |
2406 | |
2407 /* reset scores */ | |
2408 for(i=0; i<b_frames+1; i++){ | |
2409 s->input_picture[i]->b_frame_score=0; | |
2410 } | |
2411 }else if(s->avctx->b_frame_strategy==2){ | |
2412 b_frames= estimate_best_b_count(s); | |
2413 }else{ | |
2414 av_log(s->avctx, AV_LOG_ERROR, "illegal b frame strategy\n"); | |
2415 b_frames=0; | |
2416 } | |
2417 | |
2418 emms_c(); | |
2419 //static int b_count=0; | |
2420 //b_count+= b_frames; | |
2421 //av_log(s->avctx, AV_LOG_DEBUG, "b_frames: %d\n", b_count); | |
2422 | |
2423 for(i= b_frames - 1; i>=0; i--){ | |
2424 int type= s->input_picture[i]->pict_type; | |
2425 if(type && type != B_TYPE) | |
2426 b_frames= i; | |
2427 } | |
2428 if(s->input_picture[b_frames]->pict_type == B_TYPE && b_frames == s->max_b_frames){ | |
2429 av_log(s->avctx, AV_LOG_ERROR, "warning, too many b frames in a row\n"); | |
2430 } | |
2431 | |
2432 if(s->picture_in_gop_number + b_frames >= s->gop_size){ | |
2433 if((s->flags2 & CODEC_FLAG2_STRICT_GOP) && s->gop_size > s->picture_in_gop_number){ | |
2434 b_frames= s->gop_size - s->picture_in_gop_number - 1; | |
2435 }else{ | |
2436 if(s->flags & CODEC_FLAG_CLOSED_GOP) | |
2437 b_frames=0; | |
2438 s->input_picture[b_frames]->pict_type= I_TYPE; | |
2439 } | |
2440 } | |
2441 | |
2442 if( (s->flags & CODEC_FLAG_CLOSED_GOP) | |
2443 && b_frames | |
2444 && s->input_picture[b_frames]->pict_type== I_TYPE) | |
2445 b_frames--; | |
2446 | |
2447 s->reordered_input_picture[0]= s->input_picture[b_frames]; | |
2448 if(s->reordered_input_picture[0]->pict_type != I_TYPE) | |
2449 s->reordered_input_picture[0]->pict_type= P_TYPE; | |
2450 s->reordered_input_picture[0]->coded_picture_number= s->coded_picture_number++; | |
2451 for(i=0; i<b_frames; i++){ | |
2452 s->reordered_input_picture[i+1]= s->input_picture[i]; | |
2453 s->reordered_input_picture[i+1]->pict_type= B_TYPE; | |
2454 s->reordered_input_picture[i+1]->coded_picture_number= s->coded_picture_number++; | |
2455 } | |
2456 } | |
2457 } | |
2458 no_output_pic: | |
2459 if(s->reordered_input_picture[0]){ | |
2460 s->reordered_input_picture[0]->reference= s->reordered_input_picture[0]->pict_type!=B_TYPE ? 3 : 0; | |
2461 | |
2462 copy_picture(&s->new_picture, s->reordered_input_picture[0]); | |
2463 | |
2464 if(s->reordered_input_picture[0]->type == FF_BUFFER_TYPE_SHARED || s->avctx->rc_buffer_size){ | |
2465 // input is a shared pix, so we can't modifiy it -> alloc a new one & ensure that the shared one is reuseable | |
2466 | |
2467 int i= ff_find_unused_picture(s, 0); | |
2468 Picture *pic= &s->picture[i]; | |
2469 | |
2470 pic->reference = s->reordered_input_picture[0]->reference; | |
2471 alloc_picture(s, pic, 0); | |
2472 | |
2473 /* mark us unused / free shared pic */ | |
2474 if(s->reordered_input_picture[0]->type == FF_BUFFER_TYPE_INTERNAL) | |
2475 s->avctx->release_buffer(s->avctx, (AVFrame*)s->reordered_input_picture[0]); | |
2476 for(i=0; i<4; i++) | |
2477 s->reordered_input_picture[0]->data[i]= NULL; | |
2478 s->reordered_input_picture[0]->type= 0; | |
2479 | |
2480 copy_picture_attributes(s, (AVFrame*)pic, (AVFrame*)s->reordered_input_picture[0]); | |
2481 | |
2482 s->current_picture_ptr= pic; | |
2483 }else{ | |
2484 // input is not a shared pix -> reuse buffer for current_pix | |
2485 | |
2486 assert( s->reordered_input_picture[0]->type==FF_BUFFER_TYPE_USER | |
2487 || s->reordered_input_picture[0]->type==FF_BUFFER_TYPE_INTERNAL); | |
2488 | |
2489 s->current_picture_ptr= s->reordered_input_picture[0]; | |
2490 for(i=0; i<4; i++){ | |
2491 s->new_picture.data[i]+= INPLACE_OFFSET; | |
2492 } | |
2493 } | |
2494 copy_picture(&s->current_picture, s->current_picture_ptr); | |
2495 | |
2496 s->picture_number= s->new_picture.display_picture_number; | |
2497 //printf("dpn:%d\n", s->picture_number); | |
2498 }else{ | |
2499 memset(&s->new_picture, 0, sizeof(Picture)); | |
2500 } | |
2501 } | |
2502 | |
2503 int MPV_encode_picture(AVCodecContext *avctx, | |
2504 unsigned char *buf, int buf_size, void *data) | |
2505 { | |
2506 MpegEncContext *s = avctx->priv_data; | |
2507 AVFrame *pic_arg = data; | |
2508 int i, stuffing_count; | |
2509 | |
2510 for(i=0; i<avctx->thread_count; i++){ | |
2511 int start_y= s->thread_context[i]->start_mb_y; | |
2512 int end_y= s->thread_context[i]-> end_mb_y; | |
2513 int h= s->mb_height; | |
2514 uint8_t *start= buf + (size_t)(((int64_t) buf_size)*start_y/h); | |
2515 uint8_t *end = buf + (size_t)(((int64_t) buf_size)* end_y/h); | |
2516 | |
2517 init_put_bits(&s->thread_context[i]->pb, start, end - start); | |
2518 } | |
2519 | |
2520 s->picture_in_gop_number++; | |
2521 | |
2522 if(load_input_picture(s, pic_arg) < 0) | |
2523 return -1; | |
2524 | |
2525 select_input_picture(s); | |
2526 | |
2527 /* output? */ | |
2528 if(s->new_picture.data[0]){ | |
2529 s->pict_type= s->new_picture.pict_type; | |
2530 //emms_c(); | |
2531 //printf("qs:%f %f %d\n", s->new_picture.quality, s->current_picture.quality, s->qscale); | |
2532 MPV_frame_start(s, avctx); | |
2533 vbv_retry: | |
2534 if (encode_picture(s, s->picture_number) < 0) | |
2535 return -1; | |
2536 | |
2537 avctx->real_pict_num = s->picture_number; | |
2538 avctx->header_bits = s->header_bits; | |
2539 avctx->mv_bits = s->mv_bits; | |
2540 avctx->misc_bits = s->misc_bits; | |
2541 avctx->i_tex_bits = s->i_tex_bits; | |
2542 avctx->p_tex_bits = s->p_tex_bits; | |
2543 avctx->i_count = s->i_count; | |
2544 avctx->p_count = s->mb_num - s->i_count - s->skip_count; //FIXME f/b_count in avctx | |
2545 avctx->skip_count = s->skip_count; | |
2546 | |
2547 MPV_frame_end(s); | |
2548 | |
2549 if (ENABLE_MJPEG_ENCODER && s->out_format == FMT_MJPEG) | |
2550 ff_mjpeg_encode_picture_trailer(s); | |
2551 | |
2552 if(avctx->rc_buffer_size){ | |
2553 RateControlContext *rcc= &s->rc_context; | |
2554 int max_size= rcc->buffer_index/3; | |
2555 | |
2556 if(put_bits_count(&s->pb) > max_size && s->lambda < s->avctx->lmax){ | |
2557 s->next_lambda= FFMAX(s->lambda+1, s->lambda*(s->qscale+1) / s->qscale); | |
2558 if(s->adaptive_quant){ | |
2559 int i; | |
2560 for(i=0; i<s->mb_height*s->mb_stride; i++) | |
2561 s->lambda_table[i]= FFMAX(s->lambda_table[i]+1, s->lambda_table[i]*(s->qscale+1) / s->qscale); | |
2562 } | |
2563 s->mb_skipped = 0; //done in MPV_frame_start() | |
2564 if(s->pict_type==P_TYPE){ //done in encode_picture() so we must undo it | |
2565 if(s->flipflop_rounding || s->codec_id == CODEC_ID_H263P || s->codec_id == CODEC_ID_MPEG4) | |
2566 s->no_rounding ^= 1; | |
2567 } | |
2568 if(s->pict_type!=B_TYPE){ | |
2569 s->time_base= s->last_time_base; | |
2570 s->last_non_b_time= s->time - s->pp_time; | |
2571 } | |
2572 // av_log(NULL, AV_LOG_ERROR, "R:%d ", s->next_lambda); | |
2573 for(i=0; i<avctx->thread_count; i++){ | |
2574 PutBitContext *pb= &s->thread_context[i]->pb; | |
2575 init_put_bits(pb, pb->buf, pb->buf_end - pb->buf); | |
2576 } | |
2577 goto vbv_retry; | |
2578 } | |
2579 | |
2580 assert(s->avctx->rc_max_rate); | |
2581 } | |
2582 | |
2583 if(s->flags&CODEC_FLAG_PASS1) | |
2584 ff_write_pass1_stats(s); | |
2585 | |
2586 for(i=0; i<4; i++){ | |
2587 s->current_picture_ptr->error[i]= s->current_picture.error[i]; | |
2588 avctx->error[i] += s->current_picture_ptr->error[i]; | |
2589 } | |
2590 | |
2591 if(s->flags&CODEC_FLAG_PASS1) | |
2592 assert(avctx->header_bits + avctx->mv_bits + avctx->misc_bits + avctx->i_tex_bits + avctx->p_tex_bits == put_bits_count(&s->pb)); | |
2593 flush_put_bits(&s->pb); | |
2594 s->frame_bits = put_bits_count(&s->pb); | |
2595 | |
2596 stuffing_count= ff_vbv_update(s, s->frame_bits); | |
2597 if(stuffing_count){ | |
2598 if(s->pb.buf_end - s->pb.buf - (put_bits_count(&s->pb)>>3) < stuffing_count + 50){ | |
2599 av_log(s->avctx, AV_LOG_ERROR, "stuffing too large\n"); | |
2600 return -1; | |
2601 } | |
2602 | |
2603 switch(s->codec_id){ | |
2604 case CODEC_ID_MPEG1VIDEO: | |
2605 case CODEC_ID_MPEG2VIDEO: | |
2606 while(stuffing_count--){ | |
2607 put_bits(&s->pb, 8, 0); | |
2608 } | |
2609 break; | |
2610 case CODEC_ID_MPEG4: | |
2611 put_bits(&s->pb, 16, 0); | |
2612 put_bits(&s->pb, 16, 0x1C3); | |
2613 stuffing_count -= 4; | |
2614 while(stuffing_count--){ | |
2615 put_bits(&s->pb, 8, 0xFF); | |
2616 } | |
2617 break; | |
2618 default: | |
2619 av_log(s->avctx, AV_LOG_ERROR, "vbv buffer overflow\n"); | |
2620 } | |
2621 flush_put_bits(&s->pb); | |
2622 s->frame_bits = put_bits_count(&s->pb); | |
2623 } | |
2624 | |
2625 /* update mpeg1/2 vbv_delay for CBR */ | |
2626 if(s->avctx->rc_max_rate && s->avctx->rc_min_rate == s->avctx->rc_max_rate && s->out_format == FMT_MPEG1 | |
2627 && 90000LL * (avctx->rc_buffer_size-1) <= s->avctx->rc_max_rate*0xFFFFLL){ | |
2628 int vbv_delay; | |
2629 | |
2630 assert(s->repeat_first_field==0); | |
2631 | |
2632 vbv_delay= lrintf(90000 * s->rc_context.buffer_index / s->avctx->rc_max_rate); | |
2633 assert(vbv_delay < 0xFFFF); | |
2634 | |
2635 s->vbv_delay_ptr[0] &= 0xF8; | |
2636 s->vbv_delay_ptr[0] |= vbv_delay>>13; | |
2637 s->vbv_delay_ptr[1] = vbv_delay>>5; | |
2638 s->vbv_delay_ptr[2] &= 0x07; | |
2639 s->vbv_delay_ptr[2] |= vbv_delay<<3; | |
2640 } | |
2641 s->total_bits += s->frame_bits; | |
2642 avctx->frame_bits = s->frame_bits; | |
2643 }else{ | |
2644 assert((pbBufPtr(&s->pb) == s->pb.buf)); | |
2645 s->frame_bits=0; | |
2646 } | |
2647 assert((s->frame_bits&7)==0); | |
2648 | |
2649 return s->frame_bits/8; | |
2650 } | |
2651 | |
2652 #endif //CONFIG_ENCODERS | |
2653 | |
2654 static inline void gmc1_motion(MpegEncContext *s, | |
2655 uint8_t *dest_y, uint8_t *dest_cb, uint8_t *dest_cr, | |
2656 uint8_t **ref_picture) | |
2657 { | |
2658 uint8_t *ptr; | |
2659 int offset, src_x, src_y, linesize, uvlinesize; | |
2660 int motion_x, motion_y; | |
2661 int emu=0; | |
2662 | |
2663 motion_x= s->sprite_offset[0][0]; | |
2664 motion_y= s->sprite_offset[0][1]; | |
2665 src_x = s->mb_x * 16 + (motion_x >> (s->sprite_warping_accuracy+1)); | |
2666 src_y = s->mb_y * 16 + (motion_y >> (s->sprite_warping_accuracy+1)); | |
2667 motion_x<<=(3-s->sprite_warping_accuracy); | |
2668 motion_y<<=(3-s->sprite_warping_accuracy); | |
2669 src_x = av_clip(src_x, -16, s->width); | |
2670 if (src_x == s->width) | |
2671 motion_x =0; | |
2672 src_y = av_clip(src_y, -16, s->height); | |
2673 if (src_y == s->height) | |
2674 motion_y =0; | |
2675 | |
2676 linesize = s->linesize; | |
2677 uvlinesize = s->uvlinesize; | |
2678 | |
2679 ptr = ref_picture[0] + (src_y * linesize) + src_x; | |
2680 | |
2681 if(s->flags&CODEC_FLAG_EMU_EDGE){ | |
2682 if( (unsigned)src_x >= s->h_edge_pos - 17 | |
2683 || (unsigned)src_y >= s->v_edge_pos - 17){ | |
2684 ff_emulated_edge_mc(s->edge_emu_buffer, ptr, linesize, 17, 17, src_x, src_y, s->h_edge_pos, s->v_edge_pos); | |
2685 ptr= s->edge_emu_buffer; | |
2686 } | |
2687 } | |
2688 | |
2689 if((motion_x|motion_y)&7){ | |
2690 s->dsp.gmc1(dest_y , ptr , linesize, 16, motion_x&15, motion_y&15, 128 - s->no_rounding); | |
2691 s->dsp.gmc1(dest_y+8, ptr+8, linesize, 16, motion_x&15, motion_y&15, 128 - s->no_rounding); | |
2692 }else{ | |
2693 int dxy; | |
2694 | |
2695 dxy= ((motion_x>>3)&1) | ((motion_y>>2)&2); | |
2696 if (s->no_rounding){ | |
2697 s->dsp.put_no_rnd_pixels_tab[0][dxy](dest_y, ptr, linesize, 16); | |
2698 }else{ | |
2699 s->dsp.put_pixels_tab [0][dxy](dest_y, ptr, linesize, 16); | |
2700 } | |
2701 } | |
2702 | |
2703 if(s->flags&CODEC_FLAG_GRAY) return; | |
2704 | |
2705 motion_x= s->sprite_offset[1][0]; | |
2706 motion_y= s->sprite_offset[1][1]; | |
2707 src_x = s->mb_x * 8 + (motion_x >> (s->sprite_warping_accuracy+1)); | |
2708 src_y = s->mb_y * 8 + (motion_y >> (s->sprite_warping_accuracy+1)); | |
2709 motion_x<<=(3-s->sprite_warping_accuracy); | |
2710 motion_y<<=(3-s->sprite_warping_accuracy); | |
2711 src_x = av_clip(src_x, -8, s->width>>1); | |
2712 if (src_x == s->width>>1) | |
2713 motion_x =0; | |
2714 src_y = av_clip(src_y, -8, s->height>>1); | |
2715 if (src_y == s->height>>1) | |
2716 motion_y =0; | |
2717 | |
2718 offset = (src_y * uvlinesize) + src_x; | |
2719 ptr = ref_picture[1] + offset; | |
2720 if(s->flags&CODEC_FLAG_EMU_EDGE){ | |
2721 if( (unsigned)src_x >= (s->h_edge_pos>>1) - 9 | |
2722 || (unsigned)src_y >= (s->v_edge_pos>>1) - 9){ | |
2723 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); | |
2724 ptr= s->edge_emu_buffer; | |
2725 emu=1; | |
2726 } | |
2727 } | |
2728 s->dsp.gmc1(dest_cb, ptr, uvlinesize, 8, motion_x&15, motion_y&15, 128 - s->no_rounding); | |
2729 | |
2730 ptr = ref_picture[2] + offset; | |
2731 if(emu){ | |
2732 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); | |
2733 ptr= s->edge_emu_buffer; | |
2734 } | |
2735 s->dsp.gmc1(dest_cr, ptr, uvlinesize, 8, motion_x&15, motion_y&15, 128 - s->no_rounding); | |
2736 | |
2737 return; | |
2738 } | |
2739 | |
2740 static inline void gmc_motion(MpegEncContext *s, | |
2741 uint8_t *dest_y, uint8_t *dest_cb, uint8_t *dest_cr, | |
2742 uint8_t **ref_picture) | |
2743 { | |
2744 uint8_t *ptr; | |
2745 int linesize, uvlinesize; | |
2746 const int a= s->sprite_warping_accuracy; | |
2747 int ox, oy; | |
2748 | |
2749 linesize = s->linesize; | |
2750 uvlinesize = s->uvlinesize; | |
2751 | |
2752 ptr = ref_picture[0]; | |
2753 | |
2754 ox= s->sprite_offset[0][0] + s->sprite_delta[0][0]*s->mb_x*16 + s->sprite_delta[0][1]*s->mb_y*16; | |
2755 oy= s->sprite_offset[0][1] + s->sprite_delta[1][0]*s->mb_x*16 + s->sprite_delta[1][1]*s->mb_y*16; | |
2756 | |
2757 s->dsp.gmc(dest_y, ptr, linesize, 16, | |
2758 ox, | |
2759 oy, | |
2760 s->sprite_delta[0][0], s->sprite_delta[0][1], | |
2761 s->sprite_delta[1][0], s->sprite_delta[1][1], | |
2762 a+1, (1<<(2*a+1)) - s->no_rounding, | |
2763 s->h_edge_pos, s->v_edge_pos); | |
2764 s->dsp.gmc(dest_y+8, ptr, linesize, 16, | |
2765 ox + s->sprite_delta[0][0]*8, | |
2766 oy + s->sprite_delta[1][0]*8, | |
2767 s->sprite_delta[0][0], s->sprite_delta[0][1], | |
2768 s->sprite_delta[1][0], s->sprite_delta[1][1], | |
2769 a+1, (1<<(2*a+1)) - s->no_rounding, | |
2770 s->h_edge_pos, s->v_edge_pos); | |
2771 | |
2772 if(s->flags&CODEC_FLAG_GRAY) return; | |
2773 | |
2774 ox= s->sprite_offset[1][0] + s->sprite_delta[0][0]*s->mb_x*8 + s->sprite_delta[0][1]*s->mb_y*8; | |
2775 oy= s->sprite_offset[1][1] + s->sprite_delta[1][0]*s->mb_x*8 + s->sprite_delta[1][1]*s->mb_y*8; | |
2776 | |
2777 ptr = ref_picture[1]; | |
2778 s->dsp.gmc(dest_cb, ptr, uvlinesize, 8, | |
2779 ox, | |
2780 oy, | |
2781 s->sprite_delta[0][0], s->sprite_delta[0][1], | |
2782 s->sprite_delta[1][0], s->sprite_delta[1][1], | |
2783 a+1, (1<<(2*a+1)) - s->no_rounding, | |
2784 s->h_edge_pos>>1, s->v_edge_pos>>1); | |
2785 | |
2786 ptr = ref_picture[2]; | |
2787 s->dsp.gmc(dest_cr, ptr, uvlinesize, 8, | |
2788 ox, | |
2789 oy, | |
2790 s->sprite_delta[0][0], s->sprite_delta[0][1], | |
2791 s->sprite_delta[1][0], s->sprite_delta[1][1], | |
2792 a+1, (1<<(2*a+1)) - s->no_rounding, | |
2793 s->h_edge_pos>>1, s->v_edge_pos>>1); | |
2794 } | |
2795 | |
2796 /** | 1345 /** |
2797 * Copies a rectangular area of samples to a temporary buffer and replicates the boarder samples. | 1346 * Copies a rectangular area of samples to a temporary buffer and replicates the boarder samples. |
2798 * @param buf destination buffer | 1347 * @param buf destination buffer |
2799 * @param src source buffer | 1348 * @param src source buffer |
2800 * @param linesize number of bytes between 2 vertically adjacent samples in both the source and destination buffers | 1349 * @param linesize number of bytes between 2 vertically adjacent samples in both the source and destination buffers |
2862 buf[x + y*linesize]= buf[end_x - 1 + y*linesize]; | 1411 buf[x + y*linesize]= buf[end_x - 1 + y*linesize]; |
2863 } | 1412 } |
2864 } | 1413 } |
2865 } | 1414 } |
2866 | 1415 |
2867 static inline int hpel_motion(MpegEncContext *s, | |
2868 uint8_t *dest, uint8_t *src, | |
2869 int field_based, int field_select, | |
2870 int src_x, int src_y, | |
2871 int width, int height, int stride, | |
2872 int h_edge_pos, int v_edge_pos, | |
2873 int w, int h, op_pixels_func *pix_op, | |
2874 int motion_x, int motion_y) | |
2875 { | |
2876 int dxy; | |
2877 int emu=0; | |
2878 | |
2879 dxy = ((motion_y & 1) << 1) | (motion_x & 1); | |
2880 src_x += motion_x >> 1; | |
2881 src_y += motion_y >> 1; | |
2882 | |
2883 /* WARNING: do no forget half pels */ | |
2884 src_x = av_clip(src_x, -16, width); //FIXME unneeded for emu? | |
2885 if (src_x == width) | |
2886 dxy &= ~1; | |
2887 src_y = av_clip(src_y, -16, height); | |
2888 if (src_y == height) | |
2889 dxy &= ~2; | |
2890 src += src_y * stride + src_x; | |
2891 | |
2892 if(s->unrestricted_mv && (s->flags&CODEC_FLAG_EMU_EDGE)){ | |
2893 if( (unsigned)src_x > h_edge_pos - (motion_x&1) - w | |
2894 || (unsigned)src_y > v_edge_pos - (motion_y&1) - h){ | |
2895 ff_emulated_edge_mc(s->edge_emu_buffer, src, s->linesize, w+1, (h+1)<<field_based, | |
2896 src_x, src_y<<field_based, h_edge_pos, s->v_edge_pos); | |
2897 src= s->edge_emu_buffer; | |
2898 emu=1; | |
2899 } | |
2900 } | |
2901 if(field_select) | |
2902 src += s->linesize; | |
2903 pix_op[dxy](dest, src, stride, h); | |
2904 return emu; | |
2905 } | |
2906 | |
2907 static inline int hpel_motion_lowres(MpegEncContext *s, | 1416 static inline int hpel_motion_lowres(MpegEncContext *s, |
2908 uint8_t *dest, uint8_t *src, | 1417 uint8_t *dest, uint8_t *src, |
2909 int field_based, int field_select, | 1418 int field_based, int field_select, |
2910 int src_x, int src_y, | 1419 int src_x, int src_y, |
2911 int width, int height, int stride, | 1420 int width, int height, int stride, |
2942 sy <<= 2 - lowres; | 1451 sy <<= 2 - lowres; |
2943 if(field_select) | 1452 if(field_select) |
2944 src += s->linesize; | 1453 src += s->linesize; |
2945 pix_op[lowres](dest, src, stride, h, sx, sy); | 1454 pix_op[lowres](dest, src, stride, h, sx, sy); |
2946 return emu; | 1455 return emu; |
2947 } | |
2948 | |
2949 /* apply one mpeg motion vector to the three components */ | |
2950 static av_always_inline void mpeg_motion(MpegEncContext *s, | |
2951 uint8_t *dest_y, uint8_t *dest_cb, uint8_t *dest_cr, | |
2952 int field_based, int bottom_field, int field_select, | |
2953 uint8_t **ref_picture, op_pixels_func (*pix_op)[4], | |
2954 int motion_x, int motion_y, int h) | |
2955 { | |
2956 uint8_t *ptr_y, *ptr_cb, *ptr_cr; | |
2957 int dxy, uvdxy, mx, my, src_x, src_y, uvsrc_x, uvsrc_y, v_edge_pos, uvlinesize, linesize; | |
2958 | |
2959 #if 0 | |
2960 if(s->quarter_sample) | |
2961 { | |
2962 motion_x>>=1; | |
2963 motion_y>>=1; | |
2964 } | |
2965 #endif | |
2966 | |
2967 v_edge_pos = s->v_edge_pos >> field_based; | |
2968 linesize = s->current_picture.linesize[0] << field_based; | |
2969 uvlinesize = s->current_picture.linesize[1] << field_based; | |
2970 | |
2971 dxy = ((motion_y & 1) << 1) | (motion_x & 1); | |
2972 src_x = s->mb_x* 16 + (motion_x >> 1); | |
2973 src_y =(s->mb_y<<(4-field_based)) + (motion_y >> 1); | |
2974 | |
2975 if (s->out_format == FMT_H263) { | |
2976 if((s->workaround_bugs & FF_BUG_HPEL_CHROMA) && field_based){ | |
2977 mx = (motion_x>>1)|(motion_x&1); | |
2978 my = motion_y >>1; | |
2979 uvdxy = ((my & 1) << 1) | (mx & 1); | |
2980 uvsrc_x = s->mb_x* 8 + (mx >> 1); | |
2981 uvsrc_y = (s->mb_y<<(3-field_based)) + (my >> 1); | |
2982 }else{ | |
2983 uvdxy = dxy | (motion_y & 2) | ((motion_x & 2) >> 1); | |
2984 uvsrc_x = src_x>>1; | |
2985 uvsrc_y = src_y>>1; | |
2986 } | |
2987 }else if(s->out_format == FMT_H261){//even chroma mv's are full pel in H261 | |
2988 mx = motion_x / 4; | |
2989 my = motion_y / 4; | |
2990 uvdxy = 0; | |
2991 uvsrc_x = s->mb_x*8 + mx; | |
2992 uvsrc_y = s->mb_y*8 + my; | |
2993 } else { | |
2994 if(s->chroma_y_shift){ | |
2995 mx = motion_x / 2; | |
2996 my = motion_y / 2; | |
2997 uvdxy = ((my & 1) << 1) | (mx & 1); | |
2998 uvsrc_x = s->mb_x* 8 + (mx >> 1); | |
2999 uvsrc_y = (s->mb_y<<(3-field_based)) + (my >> 1); | |
3000 } else { | |
3001 if(s->chroma_x_shift){ | |
3002 //Chroma422 | |
3003 mx = motion_x / 2; | |
3004 uvdxy = ((motion_y & 1) << 1) | (mx & 1); | |
3005 uvsrc_x = s->mb_x* 8 + (mx >> 1); | |
3006 uvsrc_y = src_y; | |
3007 } else { | |
3008 //Chroma444 | |
3009 uvdxy = dxy; | |
3010 uvsrc_x = src_x; | |
3011 uvsrc_y = src_y; | |
3012 } | |
3013 } | |
3014 } | |
3015 | |
3016 ptr_y = ref_picture[0] + src_y * linesize + src_x; | |
3017 ptr_cb = ref_picture[1] + uvsrc_y * uvlinesize + uvsrc_x; | |
3018 ptr_cr = ref_picture[2] + uvsrc_y * uvlinesize + uvsrc_x; | |
3019 | |
3020 if( (unsigned)src_x > s->h_edge_pos - (motion_x&1) - 16 | |
3021 || (unsigned)src_y > v_edge_pos - (motion_y&1) - h){ | |
3022 if(s->codec_id == CODEC_ID_MPEG2VIDEO || | |
3023 s->codec_id == CODEC_ID_MPEG1VIDEO){ | |
3024 av_log(s->avctx,AV_LOG_DEBUG,"MPEG motion vector out of boundary\n"); | |
3025 return ; | |
3026 } | |
3027 ff_emulated_edge_mc(s->edge_emu_buffer, ptr_y, s->linesize, 17, 17+field_based, | |
3028 src_x, src_y<<field_based, s->h_edge_pos, s->v_edge_pos); | |
3029 ptr_y = s->edge_emu_buffer; | |
3030 if(!(s->flags&CODEC_FLAG_GRAY)){ | |
3031 uint8_t *uvbuf= s->edge_emu_buffer+18*s->linesize; | |
3032 ff_emulated_edge_mc(uvbuf , ptr_cb, s->uvlinesize, 9, 9+field_based, | |
3033 uvsrc_x, uvsrc_y<<field_based, s->h_edge_pos>>1, s->v_edge_pos>>1); | |
3034 ff_emulated_edge_mc(uvbuf+16, ptr_cr, s->uvlinesize, 9, 9+field_based, | |
3035 uvsrc_x, uvsrc_y<<field_based, s->h_edge_pos>>1, s->v_edge_pos>>1); | |
3036 ptr_cb= uvbuf; | |
3037 ptr_cr= uvbuf+16; | |
3038 } | |
3039 } | |
3040 | |
3041 if(bottom_field){ //FIXME use this for field pix too instead of the obnoxious hack which changes picture.data | |
3042 dest_y += s->linesize; | |
3043 dest_cb+= s->uvlinesize; | |
3044 dest_cr+= s->uvlinesize; | |
3045 } | |
3046 | |
3047 if(field_select){ | |
3048 ptr_y += s->linesize; | |
3049 ptr_cb+= s->uvlinesize; | |
3050 ptr_cr+= s->uvlinesize; | |
3051 } | |
3052 | |
3053 pix_op[0][dxy](dest_y, ptr_y, linesize, h); | |
3054 | |
3055 if(!(s->flags&CODEC_FLAG_GRAY)){ | |
3056 pix_op[s->chroma_x_shift][uvdxy](dest_cb, ptr_cb, uvlinesize, h >> s->chroma_y_shift); | |
3057 pix_op[s->chroma_x_shift][uvdxy](dest_cr, ptr_cr, uvlinesize, h >> s->chroma_y_shift); | |
3058 } | |
3059 if((ENABLE_H261_ENCODER || ENABLE_H261_DECODER) && s->out_format == FMT_H261){ | |
3060 ff_h261_loop_filter(s); | |
3061 } | |
3062 } | 1456 } |
3063 | 1457 |
3064 /* apply one mpeg motion vector to the three components */ | 1458 /* apply one mpeg motion vector to the three components */ |
3065 static av_always_inline void mpeg_motion_lowres(MpegEncContext *s, | 1459 static av_always_inline void mpeg_motion_lowres(MpegEncContext *s, |
3066 uint8_t *dest_y, uint8_t *dest_cb, uint8_t *dest_cr, | 1460 uint8_t *dest_y, uint8_t *dest_cb, uint8_t *dest_cr, |
3156 pix_op[lowres](dest_cr, ptr_cr, uvlinesize, h >> s->chroma_y_shift, uvsx, uvsy); | 1550 pix_op[lowres](dest_cr, ptr_cr, uvlinesize, h >> s->chroma_y_shift, uvsx, uvsy); |
3157 } | 1551 } |
3158 //FIXME h261 lowres loop filter | 1552 //FIXME h261 lowres loop filter |
3159 } | 1553 } |
3160 | 1554 |
3161 //FIXME move to dsputil, avg variant, 16x16 version | |
3162 static inline void put_obmc(uint8_t *dst, uint8_t *src[5], int stride){ | |
3163 int x; | |
3164 uint8_t * const top = src[1]; | |
3165 uint8_t * const left = src[2]; | |
3166 uint8_t * const mid = src[0]; | |
3167 uint8_t * const right = src[3]; | |
3168 uint8_t * const bottom= src[4]; | |
3169 #define OBMC_FILTER(x, t, l, m, r, b)\ | |
3170 dst[x]= (t*top[x] + l*left[x] + m*mid[x] + r*right[x] + b*bottom[x] + 4)>>3 | |
3171 #define OBMC_FILTER4(x, t, l, m, r, b)\ | |
3172 OBMC_FILTER(x , t, l, m, r, b);\ | |
3173 OBMC_FILTER(x+1 , t, l, m, r, b);\ | |
3174 OBMC_FILTER(x +stride, t, l, m, r, b);\ | |
3175 OBMC_FILTER(x+1+stride, t, l, m, r, b); | |
3176 | |
3177 x=0; | |
3178 OBMC_FILTER (x , 2, 2, 4, 0, 0); | |
3179 OBMC_FILTER (x+1, 2, 1, 5, 0, 0); | |
3180 OBMC_FILTER4(x+2, 2, 1, 5, 0, 0); | |
3181 OBMC_FILTER4(x+4, 2, 0, 5, 1, 0); | |
3182 OBMC_FILTER (x+6, 2, 0, 5, 1, 0); | |
3183 OBMC_FILTER (x+7, 2, 0, 4, 2, 0); | |
3184 x+= stride; | |
3185 OBMC_FILTER (x , 1, 2, 5, 0, 0); | |
3186 OBMC_FILTER (x+1, 1, 2, 5, 0, 0); | |
3187 OBMC_FILTER (x+6, 1, 0, 5, 2, 0); | |
3188 OBMC_FILTER (x+7, 1, 0, 5, 2, 0); | |
3189 x+= stride; | |
3190 OBMC_FILTER4(x , 1, 2, 5, 0, 0); | |
3191 OBMC_FILTER4(x+2, 1, 1, 6, 0, 0); | |
3192 OBMC_FILTER4(x+4, 1, 0, 6, 1, 0); | |
3193 OBMC_FILTER4(x+6, 1, 0, 5, 2, 0); | |
3194 x+= 2*stride; | |
3195 OBMC_FILTER4(x , 0, 2, 5, 0, 1); | |
3196 OBMC_FILTER4(x+2, 0, 1, 6, 0, 1); | |
3197 OBMC_FILTER4(x+4, 0, 0, 6, 1, 1); | |
3198 OBMC_FILTER4(x+6, 0, 0, 5, 2, 1); | |
3199 x+= 2*stride; | |
3200 OBMC_FILTER (x , 0, 2, 5, 0, 1); | |
3201 OBMC_FILTER (x+1, 0, 2, 5, 0, 1); | |
3202 OBMC_FILTER4(x+2, 0, 1, 5, 0, 2); | |
3203 OBMC_FILTER4(x+4, 0, 0, 5, 1, 2); | |
3204 OBMC_FILTER (x+6, 0, 0, 5, 2, 1); | |
3205 OBMC_FILTER (x+7, 0, 0, 5, 2, 1); | |
3206 x+= stride; | |
3207 OBMC_FILTER (x , 0, 2, 4, 0, 2); | |
3208 OBMC_FILTER (x+1, 0, 1, 5, 0, 2); | |
3209 OBMC_FILTER (x+6, 0, 0, 5, 1, 2); | |
3210 OBMC_FILTER (x+7, 0, 0, 4, 2, 2); | |
3211 } | |
3212 | |
3213 /* obmc for 1 8x8 luma block */ | |
3214 static inline void obmc_motion(MpegEncContext *s, | |
3215 uint8_t *dest, uint8_t *src, | |
3216 int src_x, int src_y, | |
3217 op_pixels_func *pix_op, | |
3218 int16_t mv[5][2]/* mid top left right bottom*/) | |
3219 #define MID 0 | |
3220 { | |
3221 int i; | |
3222 uint8_t *ptr[5]; | |
3223 | |
3224 assert(s->quarter_sample==0); | |
3225 | |
3226 for(i=0; i<5; i++){ | |
3227 if(i && mv[i][0]==mv[MID][0] && mv[i][1]==mv[MID][1]){ | |
3228 ptr[i]= ptr[MID]; | |
3229 }else{ | |
3230 ptr[i]= s->obmc_scratchpad + 8*(i&1) + s->linesize*8*(i>>1); | |
3231 hpel_motion(s, ptr[i], src, 0, 0, | |
3232 src_x, src_y, | |
3233 s->width, s->height, s->linesize, | |
3234 s->h_edge_pos, s->v_edge_pos, | |
3235 8, 8, pix_op, | |
3236 mv[i][0], mv[i][1]); | |
3237 } | |
3238 } | |
3239 | |
3240 put_obmc(dest, ptr, s->linesize); | |
3241 } | |
3242 | |
3243 static inline void qpel_motion(MpegEncContext *s, | |
3244 uint8_t *dest_y, uint8_t *dest_cb, uint8_t *dest_cr, | |
3245 int field_based, int bottom_field, int field_select, | |
3246 uint8_t **ref_picture, op_pixels_func (*pix_op)[4], | |
3247 qpel_mc_func (*qpix_op)[16], | |
3248 int motion_x, int motion_y, int h) | |
3249 { | |
3250 uint8_t *ptr_y, *ptr_cb, *ptr_cr; | |
3251 int dxy, uvdxy, mx, my, src_x, src_y, uvsrc_x, uvsrc_y, v_edge_pos, linesize, uvlinesize; | |
3252 | |
3253 dxy = ((motion_y & 3) << 2) | (motion_x & 3); | |
3254 src_x = s->mb_x * 16 + (motion_x >> 2); | |
3255 src_y = s->mb_y * (16 >> field_based) + (motion_y >> 2); | |
3256 | |
3257 v_edge_pos = s->v_edge_pos >> field_based; | |
3258 linesize = s->linesize << field_based; | |
3259 uvlinesize = s->uvlinesize << field_based; | |
3260 | |
3261 if(field_based){ | |
3262 mx= motion_x/2; | |
3263 my= motion_y>>1; | |
3264 }else if(s->workaround_bugs&FF_BUG_QPEL_CHROMA2){ | |
3265 static const int rtab[8]= {0,0,1,1,0,0,0,1}; | |
3266 mx= (motion_x>>1) + rtab[motion_x&7]; | |
3267 my= (motion_y>>1) + rtab[motion_y&7]; | |
3268 }else if(s->workaround_bugs&FF_BUG_QPEL_CHROMA){ | |
3269 mx= (motion_x>>1)|(motion_x&1); | |
3270 my= (motion_y>>1)|(motion_y&1); | |
3271 }else{ | |
3272 mx= motion_x/2; | |
3273 my= motion_y/2; | |
3274 } | |
3275 mx= (mx>>1)|(mx&1); | |
3276 my= (my>>1)|(my&1); | |
3277 | |
3278 uvdxy= (mx&1) | ((my&1)<<1); | |
3279 mx>>=1; | |
3280 my>>=1; | |
3281 | |
3282 uvsrc_x = s->mb_x * 8 + mx; | |
3283 uvsrc_y = s->mb_y * (8 >> field_based) + my; | |
3284 | |
3285 ptr_y = ref_picture[0] + src_y * linesize + src_x; | |
3286 ptr_cb = ref_picture[1] + uvsrc_y * uvlinesize + uvsrc_x; | |
3287 ptr_cr = ref_picture[2] + uvsrc_y * uvlinesize + uvsrc_x; | |
3288 | |
3289 if( (unsigned)src_x > s->h_edge_pos - (motion_x&3) - 16 | |
3290 || (unsigned)src_y > v_edge_pos - (motion_y&3) - h ){ | |
3291 ff_emulated_edge_mc(s->edge_emu_buffer, ptr_y, s->linesize, 17, 17+field_based, | |
3292 src_x, src_y<<field_based, s->h_edge_pos, s->v_edge_pos); | |
3293 ptr_y= s->edge_emu_buffer; | |
3294 if(!(s->flags&CODEC_FLAG_GRAY)){ | |
3295 uint8_t *uvbuf= s->edge_emu_buffer + 18*s->linesize; | |
3296 ff_emulated_edge_mc(uvbuf, ptr_cb, s->uvlinesize, 9, 9 + field_based, | |
3297 uvsrc_x, uvsrc_y<<field_based, s->h_edge_pos>>1, s->v_edge_pos>>1); | |
3298 ff_emulated_edge_mc(uvbuf + 16, ptr_cr, s->uvlinesize, 9, 9 + field_based, | |
3299 uvsrc_x, uvsrc_y<<field_based, s->h_edge_pos>>1, s->v_edge_pos>>1); | |
3300 ptr_cb= uvbuf; | |
3301 ptr_cr= uvbuf + 16; | |
3302 } | |
3303 } | |
3304 | |
3305 if(!field_based) | |
3306 qpix_op[0][dxy](dest_y, ptr_y, linesize); | |
3307 else{ | |
3308 if(bottom_field){ | |
3309 dest_y += s->linesize; | |
3310 dest_cb+= s->uvlinesize; | |
3311 dest_cr+= s->uvlinesize; | |
3312 } | |
3313 | |
3314 if(field_select){ | |
3315 ptr_y += s->linesize; | |
3316 ptr_cb += s->uvlinesize; | |
3317 ptr_cr += s->uvlinesize; | |
3318 } | |
3319 //damn interlaced mode | |
3320 //FIXME boundary mirroring is not exactly correct here | |
3321 qpix_op[1][dxy](dest_y , ptr_y , linesize); | |
3322 qpix_op[1][dxy](dest_y+8, ptr_y+8, linesize); | |
3323 } | |
3324 if(!(s->flags&CODEC_FLAG_GRAY)){ | |
3325 pix_op[1][uvdxy](dest_cr, ptr_cr, uvlinesize, h >> 1); | |
3326 pix_op[1][uvdxy](dest_cb, ptr_cb, uvlinesize, h >> 1); | |
3327 } | |
3328 } | |
3329 | |
3330 inline int ff_h263_round_chroma(int x){ | 1555 inline int ff_h263_round_chroma(int x){ |
3331 if (x >= 0) | 1556 if (x >= 0) |
3332 return (h263_chroma_roundtab[x & 0xf] + ((x >> 3) & ~1)); | 1557 return (h263_chroma_roundtab[x & 0xf] + ((x >> 3) & ~1)); |
3333 else { | 1558 else { |
3334 x = -x; | 1559 x = -x; |
3335 return -(h263_chroma_roundtab[x & 0xf] + ((x >> 3) & ~1)); | 1560 return -(h263_chroma_roundtab[x & 0xf] + ((x >> 3) & ~1)); |
3336 } | 1561 } |
3337 } | |
3338 | |
3339 /** | |
3340 * h263 chorma 4mv motion compensation. | |
3341 */ | |
3342 static inline void chroma_4mv_motion(MpegEncContext *s, | |
3343 uint8_t *dest_cb, uint8_t *dest_cr, | |
3344 uint8_t **ref_picture, | |
3345 op_pixels_func *pix_op, | |
3346 int mx, int my){ | |
3347 int dxy, emu=0, src_x, src_y, offset; | |
3348 uint8_t *ptr; | |
3349 | |
3350 /* In case of 8X8, we construct a single chroma motion vector | |
3351 with a special rounding */ | |
3352 mx= ff_h263_round_chroma(mx); | |
3353 my= ff_h263_round_chroma(my); | |
3354 | |
3355 dxy = ((my & 1) << 1) | (mx & 1); | |
3356 mx >>= 1; | |
3357 my >>= 1; | |
3358 | |
3359 src_x = s->mb_x * 8 + mx; | |
3360 src_y = s->mb_y * 8 + my; | |
3361 src_x = av_clip(src_x, -8, s->width/2); | |
3362 if (src_x == s->width/2) | |
3363 dxy &= ~1; | |
3364 src_y = av_clip(src_y, -8, s->height/2); | |
3365 if (src_y == s->height/2) | |
3366 dxy &= ~2; | |
3367 | |
3368 offset = (src_y * (s->uvlinesize)) + src_x; | |
3369 ptr = ref_picture[1] + offset; | |
3370 if(s->flags&CODEC_FLAG_EMU_EDGE){ | |
3371 if( (unsigned)src_x > (s->h_edge_pos>>1) - (dxy &1) - 8 | |
3372 || (unsigned)src_y > (s->v_edge_pos>>1) - (dxy>>1) - 8){ | |
3373 ff_emulated_edge_mc(s->edge_emu_buffer, ptr, s->uvlinesize, 9, 9, src_x, src_y, s->h_edge_pos>>1, s->v_edge_pos>>1); | |
3374 ptr= s->edge_emu_buffer; | |
3375 emu=1; | |
3376 } | |
3377 } | |
3378 pix_op[dxy](dest_cb, ptr, s->uvlinesize, 8); | |
3379 | |
3380 ptr = ref_picture[2] + offset; | |
3381 if(emu){ | |
3382 ff_emulated_edge_mc(s->edge_emu_buffer, ptr, s->uvlinesize, 9, 9, src_x, src_y, s->h_edge_pos>>1, s->v_edge_pos>>1); | |
3383 ptr= s->edge_emu_buffer; | |
3384 } | |
3385 pix_op[dxy](dest_cr, ptr, s->uvlinesize, 8); | |
3386 } | 1562 } |
3387 | 1563 |
3388 static inline void chroma_4mv_motion_lowres(MpegEncContext *s, | 1564 static inline void chroma_4mv_motion_lowres(MpegEncContext *s, |
3389 uint8_t *dest_cb, uint8_t *dest_cr, | 1565 uint8_t *dest_cb, uint8_t *dest_cr, |
3390 uint8_t **ref_picture, | 1566 uint8_t **ref_picture, |
3431 if(emu){ | 1607 if(emu){ |
3432 ff_emulated_edge_mc(s->edge_emu_buffer, ptr, s->uvlinesize, 9, 9, src_x, src_y, h_edge_pos, v_edge_pos); | 1608 ff_emulated_edge_mc(s->edge_emu_buffer, ptr, s->uvlinesize, 9, 9, src_x, src_y, h_edge_pos, v_edge_pos); |
3433 ptr= s->edge_emu_buffer; | 1609 ptr= s->edge_emu_buffer; |
3434 } | 1610 } |
3435 pix_op[lowres](dest_cr, ptr, s->uvlinesize, block_s, sx, sy); | 1611 pix_op[lowres](dest_cr, ptr, s->uvlinesize, block_s, sx, sy); |
3436 } | |
3437 | |
3438 static inline void prefetch_motion(MpegEncContext *s, uint8_t **pix, int dir){ | |
3439 /* fetch pixels for estimated mv 4 macroblocks ahead | |
3440 * optimized for 64byte cache lines */ | |
3441 const int shift = s->quarter_sample ? 2 : 1; | |
3442 const int mx= (s->mv[dir][0][0]>>shift) + 16*s->mb_x + 8; | |
3443 const int my= (s->mv[dir][0][1]>>shift) + 16*s->mb_y; | |
3444 int off= mx + (my + (s->mb_x&3)*4)*s->linesize + 64; | |
3445 s->dsp.prefetch(pix[0]+off, s->linesize, 4); | |
3446 off= (mx>>1) + ((my>>1) + (s->mb_x&7))*s->uvlinesize + 64; | |
3447 s->dsp.prefetch(pix[1]+off, pix[2]-pix[1], 2); | |
3448 } | |
3449 | |
3450 /** | |
3451 * motion compensation of a single macroblock | |
3452 * @param s context | |
3453 * @param dest_y luma destination pointer | |
3454 * @param dest_cb chroma cb/u destination pointer | |
3455 * @param dest_cr chroma cr/v destination pointer | |
3456 * @param dir direction (0->forward, 1->backward) | |
3457 * @param ref_picture array[3] of pointers to the 3 planes of the reference picture | |
3458 * @param pic_op halfpel motion compensation function (average or put normally) | |
3459 * @param pic_op qpel motion compensation function (average or put normally) | |
3460 * the motion vectors are taken from s->mv and the MV type from s->mv_type | |
3461 */ | |
3462 static inline void MPV_motion(MpegEncContext *s, | |
3463 uint8_t *dest_y, uint8_t *dest_cb, uint8_t *dest_cr, | |
3464 int dir, uint8_t **ref_picture, | |
3465 op_pixels_func (*pix_op)[4], qpel_mc_func (*qpix_op)[16]) | |
3466 { | |
3467 int dxy, mx, my, src_x, src_y, motion_x, motion_y; | |
3468 int mb_x, mb_y, i; | |
3469 uint8_t *ptr, *dest; | |
3470 | |
3471 mb_x = s->mb_x; | |
3472 mb_y = s->mb_y; | |
3473 | |
3474 prefetch_motion(s, ref_picture, dir); | |
3475 | |
3476 if(s->obmc && s->pict_type != B_TYPE){ | |
3477 int16_t mv_cache[4][4][2]; | |
3478 const int xy= s->mb_x + s->mb_y*s->mb_stride; | |
3479 const int mot_stride= s->b8_stride; | |
3480 const int mot_xy= mb_x*2 + mb_y*2*mot_stride; | |
3481 | |
3482 assert(!s->mb_skipped); | |
3483 | |
3484 memcpy(mv_cache[1][1], s->current_picture.motion_val[0][mot_xy ], sizeof(int16_t)*4); | |
3485 memcpy(mv_cache[2][1], s->current_picture.motion_val[0][mot_xy+mot_stride], sizeof(int16_t)*4); | |
3486 memcpy(mv_cache[3][1], s->current_picture.motion_val[0][mot_xy+mot_stride], sizeof(int16_t)*4); | |
3487 | |
3488 if(mb_y==0 || IS_INTRA(s->current_picture.mb_type[xy-s->mb_stride])){ | |
3489 memcpy(mv_cache[0][1], mv_cache[1][1], sizeof(int16_t)*4); | |
3490 }else{ | |
3491 memcpy(mv_cache[0][1], s->current_picture.motion_val[0][mot_xy-mot_stride], sizeof(int16_t)*4); | |
3492 } | |
3493 | |
3494 if(mb_x==0 || IS_INTRA(s->current_picture.mb_type[xy-1])){ | |
3495 *(int32_t*)mv_cache[1][0]= *(int32_t*)mv_cache[1][1]; | |
3496 *(int32_t*)mv_cache[2][0]= *(int32_t*)mv_cache[2][1]; | |
3497 }else{ | |
3498 *(int32_t*)mv_cache[1][0]= *(int32_t*)s->current_picture.motion_val[0][mot_xy-1]; | |
3499 *(int32_t*)mv_cache[2][0]= *(int32_t*)s->current_picture.motion_val[0][mot_xy-1+mot_stride]; | |
3500 } | |
3501 | |
3502 if(mb_x+1>=s->mb_width || IS_INTRA(s->current_picture.mb_type[xy+1])){ | |
3503 *(int32_t*)mv_cache[1][3]= *(int32_t*)mv_cache[1][2]; | |
3504 *(int32_t*)mv_cache[2][3]= *(int32_t*)mv_cache[2][2]; | |
3505 }else{ | |
3506 *(int32_t*)mv_cache[1][3]= *(int32_t*)s->current_picture.motion_val[0][mot_xy+2]; | |
3507 *(int32_t*)mv_cache[2][3]= *(int32_t*)s->current_picture.motion_val[0][mot_xy+2+mot_stride]; | |
3508 } | |
3509 | |
3510 mx = 0; | |
3511 my = 0; | |
3512 for(i=0;i<4;i++) { | |
3513 const int x= (i&1)+1; | |
3514 const int y= (i>>1)+1; | |
3515 int16_t mv[5][2]= { | |
3516 {mv_cache[y][x ][0], mv_cache[y][x ][1]}, | |
3517 {mv_cache[y-1][x][0], mv_cache[y-1][x][1]}, | |
3518 {mv_cache[y][x-1][0], mv_cache[y][x-1][1]}, | |
3519 {mv_cache[y][x+1][0], mv_cache[y][x+1][1]}, | |
3520 {mv_cache[y+1][x][0], mv_cache[y+1][x][1]}}; | |
3521 //FIXME cleanup | |
3522 obmc_motion(s, dest_y + ((i & 1) * 8) + (i >> 1) * 8 * s->linesize, | |
3523 ref_picture[0], | |
3524 mb_x * 16 + (i & 1) * 8, mb_y * 16 + (i >>1) * 8, | |
3525 pix_op[1], | |
3526 mv); | |
3527 | |
3528 mx += mv[0][0]; | |
3529 my += mv[0][1]; | |
3530 } | |
3531 if(!(s->flags&CODEC_FLAG_GRAY)) | |
3532 chroma_4mv_motion(s, dest_cb, dest_cr, ref_picture, pix_op[1], mx, my); | |
3533 | |
3534 return; | |
3535 } | |
3536 | |
3537 switch(s->mv_type) { | |
3538 case MV_TYPE_16X16: | |
3539 if(s->mcsel){ | |
3540 if(s->real_sprite_warping_points==1){ | |
3541 gmc1_motion(s, dest_y, dest_cb, dest_cr, | |
3542 ref_picture); | |
3543 }else{ | |
3544 gmc_motion(s, dest_y, dest_cb, dest_cr, | |
3545 ref_picture); | |
3546 } | |
3547 }else if(s->quarter_sample){ | |
3548 qpel_motion(s, dest_y, dest_cb, dest_cr, | |
3549 0, 0, 0, | |
3550 ref_picture, pix_op, qpix_op, | |
3551 s->mv[dir][0][0], s->mv[dir][0][1], 16); | |
3552 }else if(ENABLE_WMV2 && s->mspel){ | |
3553 ff_mspel_motion(s, dest_y, dest_cb, dest_cr, | |
3554 ref_picture, pix_op, | |
3555 s->mv[dir][0][0], s->mv[dir][0][1], 16); | |
3556 }else | |
3557 { | |
3558 mpeg_motion(s, dest_y, dest_cb, dest_cr, | |
3559 0, 0, 0, | |
3560 ref_picture, pix_op, | |
3561 s->mv[dir][0][0], s->mv[dir][0][1], 16); | |
3562 } | |
3563 break; | |
3564 case MV_TYPE_8X8: | |
3565 mx = 0; | |
3566 my = 0; | |
3567 if(s->quarter_sample){ | |
3568 for(i=0;i<4;i++) { | |
3569 motion_x = s->mv[dir][i][0]; | |
3570 motion_y = s->mv[dir][i][1]; | |
3571 | |
3572 dxy = ((motion_y & 3) << 2) | (motion_x & 3); | |
3573 src_x = mb_x * 16 + (motion_x >> 2) + (i & 1) * 8; | |
3574 src_y = mb_y * 16 + (motion_y >> 2) + (i >>1) * 8; | |
3575 | |
3576 /* WARNING: do no forget half pels */ | |
3577 src_x = av_clip(src_x, -16, s->width); | |
3578 if (src_x == s->width) | |
3579 dxy &= ~3; | |
3580 src_y = av_clip(src_y, -16, s->height); | |
3581 if (src_y == s->height) | |
3582 dxy &= ~12; | |
3583 | |
3584 ptr = ref_picture[0] + (src_y * s->linesize) + (src_x); | |
3585 if(s->flags&CODEC_FLAG_EMU_EDGE){ | |
3586 if( (unsigned)src_x > s->h_edge_pos - (motion_x&3) - 8 | |
3587 || (unsigned)src_y > s->v_edge_pos - (motion_y&3) - 8 ){ | |
3588 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); | |
3589 ptr= s->edge_emu_buffer; | |
3590 } | |
3591 } | |
3592 dest = dest_y + ((i & 1) * 8) + (i >> 1) * 8 * s->linesize; | |
3593 qpix_op[1][dxy](dest, ptr, s->linesize); | |
3594 | |
3595 mx += s->mv[dir][i][0]/2; | |
3596 my += s->mv[dir][i][1]/2; | |
3597 } | |
3598 }else{ | |
3599 for(i=0;i<4;i++) { | |
3600 hpel_motion(s, dest_y + ((i & 1) * 8) + (i >> 1) * 8 * s->linesize, | |
3601 ref_picture[0], 0, 0, | |
3602 mb_x * 16 + (i & 1) * 8, mb_y * 16 + (i >>1) * 8, | |
3603 s->width, s->height, s->linesize, | |
3604 s->h_edge_pos, s->v_edge_pos, | |
3605 8, 8, pix_op[1], | |
3606 s->mv[dir][i][0], s->mv[dir][i][1]); | |
3607 | |
3608 mx += s->mv[dir][i][0]; | |
3609 my += s->mv[dir][i][1]; | |
3610 } | |
3611 } | |
3612 | |
3613 if(!(s->flags&CODEC_FLAG_GRAY)) | |
3614 chroma_4mv_motion(s, dest_cb, dest_cr, ref_picture, pix_op[1], mx, my); | |
3615 break; | |
3616 case MV_TYPE_FIELD: | |
3617 if (s->picture_structure == PICT_FRAME) { | |
3618 if(s->quarter_sample){ | |
3619 for(i=0; i<2; i++){ | |
3620 qpel_motion(s, dest_y, dest_cb, dest_cr, | |
3621 1, i, s->field_select[dir][i], | |
3622 ref_picture, pix_op, qpix_op, | |
3623 s->mv[dir][i][0], s->mv[dir][i][1], 8); | |
3624 } | |
3625 }else{ | |
3626 /* top field */ | |
3627 mpeg_motion(s, dest_y, dest_cb, dest_cr, | |
3628 1, 0, s->field_select[dir][0], | |
3629 ref_picture, pix_op, | |
3630 s->mv[dir][0][0], s->mv[dir][0][1], 8); | |
3631 /* bottom field */ | |
3632 mpeg_motion(s, dest_y, dest_cb, dest_cr, | |
3633 1, 1, s->field_select[dir][1], | |
3634 ref_picture, pix_op, | |
3635 s->mv[dir][1][0], s->mv[dir][1][1], 8); | |
3636 } | |
3637 } else { | |
3638 if(s->picture_structure != s->field_select[dir][0] + 1 && s->pict_type != B_TYPE && !s->first_field){ | |
3639 ref_picture= s->current_picture_ptr->data; | |
3640 } | |
3641 | |
3642 mpeg_motion(s, dest_y, dest_cb, dest_cr, | |
3643 0, 0, s->field_select[dir][0], | |
3644 ref_picture, pix_op, | |
3645 s->mv[dir][0][0], s->mv[dir][0][1], 16); | |
3646 } | |
3647 break; | |
3648 case MV_TYPE_16X8: | |
3649 for(i=0; i<2; i++){ | |
3650 uint8_t ** ref2picture; | |
3651 | |
3652 if(s->picture_structure == s->field_select[dir][i] + 1 || s->pict_type == B_TYPE || s->first_field){ | |
3653 ref2picture= ref_picture; | |
3654 }else{ | |
3655 ref2picture= s->current_picture_ptr->data; | |
3656 } | |
3657 | |
3658 mpeg_motion(s, dest_y, dest_cb, dest_cr, | |
3659 0, 0, s->field_select[dir][i], | |
3660 ref2picture, pix_op, | |
3661 s->mv[dir][i][0], s->mv[dir][i][1] + 16*i, 8); | |
3662 | |
3663 dest_y += 16*s->linesize; | |
3664 dest_cb+= (16>>s->chroma_y_shift)*s->uvlinesize; | |
3665 dest_cr+= (16>>s->chroma_y_shift)*s->uvlinesize; | |
3666 } | |
3667 break; | |
3668 case MV_TYPE_DMV: | |
3669 if(s->picture_structure == PICT_FRAME){ | |
3670 for(i=0; i<2; i++){ | |
3671 int j; | |
3672 for(j=0; j<2; j++){ | |
3673 mpeg_motion(s, dest_y, dest_cb, dest_cr, | |
3674 1, j, j^i, | |
3675 ref_picture, pix_op, | |
3676 s->mv[dir][2*i + j][0], s->mv[dir][2*i + j][1], 8); | |
3677 } | |
3678 pix_op = s->dsp.avg_pixels_tab; | |
3679 } | |
3680 }else{ | |
3681 for(i=0; i<2; i++){ | |
3682 mpeg_motion(s, dest_y, dest_cb, dest_cr, | |
3683 0, 0, s->picture_structure != i+1, | |
3684 ref_picture, pix_op, | |
3685 s->mv[dir][2*i][0],s->mv[dir][2*i][1],16); | |
3686 | |
3687 // after put we make avg of the same block | |
3688 pix_op=s->dsp.avg_pixels_tab; | |
3689 | |
3690 //opposite parity is always in the same frame if this is second field | |
3691 if(!s->first_field){ | |
3692 ref_picture = s->current_picture_ptr->data; | |
3693 } | |
3694 } | |
3695 } | |
3696 break; | |
3697 default: assert(0); | |
3698 } | |
3699 } | 1612 } |
3700 | 1613 |
3701 /** | 1614 /** |
3702 * motion compensation of a single macroblock | 1615 * motion compensation of a single macroblock |
3703 * @param s context | 1616 * @param s context |
4138 void MPV_decode_mb(MpegEncContext *s, DCTELEM block[12][64]){ | 2051 void MPV_decode_mb(MpegEncContext *s, DCTELEM block[12][64]){ |
4139 if(s->avctx->lowres) MPV_decode_mb_internal(s, block, 1); | 2052 if(s->avctx->lowres) MPV_decode_mb_internal(s, block, 1); |
4140 else MPV_decode_mb_internal(s, block, 0); | 2053 else MPV_decode_mb_internal(s, block, 0); |
4141 } | 2054 } |
4142 | 2055 |
4143 #ifdef CONFIG_ENCODERS | |
4144 | |
4145 static inline void dct_single_coeff_elimination(MpegEncContext *s, int n, int threshold) | |
4146 { | |
4147 static const char tab[64]= | |
4148 {3,2,2,1,1,1,1,1, | |
4149 1,1,1,1,1,1,1,1, | |
4150 1,1,1,1,1,1,1,1, | |
4151 0,0,0,0,0,0,0,0, | |
4152 0,0,0,0,0,0,0,0, | |
4153 0,0,0,0,0,0,0,0, | |
4154 0,0,0,0,0,0,0,0, | |
4155 0,0,0,0,0,0,0,0}; | |
4156 int score=0; | |
4157 int run=0; | |
4158 int i; | |
4159 DCTELEM *block= s->block[n]; | |
4160 const int last_index= s->block_last_index[n]; | |
4161 int skip_dc; | |
4162 | |
4163 if(threshold<0){ | |
4164 skip_dc=0; | |
4165 threshold= -threshold; | |
4166 }else | |
4167 skip_dc=1; | |
4168 | |
4169 /* are all which we could set to zero are allready zero? */ | |
4170 if(last_index<=skip_dc - 1) return; | |
4171 | |
4172 for(i=0; i<=last_index; i++){ | |
4173 const int j = s->intra_scantable.permutated[i]; | |
4174 const int level = FFABS(block[j]); | |
4175 if(level==1){ | |
4176 if(skip_dc && i==0) continue; | |
4177 score+= tab[run]; | |
4178 run=0; | |
4179 }else if(level>1){ | |
4180 return; | |
4181 }else{ | |
4182 run++; | |
4183 } | |
4184 } | |
4185 if(score >= threshold) return; | |
4186 for(i=skip_dc; i<=last_index; i++){ | |
4187 const int j = s->intra_scantable.permutated[i]; | |
4188 block[j]=0; | |
4189 } | |
4190 if(block[0]) s->block_last_index[n]= 0; | |
4191 else s->block_last_index[n]= -1; | |
4192 } | |
4193 | |
4194 static inline void clip_coeffs(MpegEncContext *s, DCTELEM *block, int last_index) | |
4195 { | |
4196 int i; | |
4197 const int maxlevel= s->max_qcoeff; | |
4198 const int minlevel= s->min_qcoeff; | |
4199 int overflow=0; | |
4200 | |
4201 if(s->mb_intra){ | |
4202 i=1; //skip clipping of intra dc | |
4203 }else | |
4204 i=0; | |
4205 | |
4206 for(;i<=last_index; i++){ | |
4207 const int j= s->intra_scantable.permutated[i]; | |
4208 int level = block[j]; | |
4209 | |
4210 if (level>maxlevel){ | |
4211 level=maxlevel; | |
4212 overflow++; | |
4213 }else if(level<minlevel){ | |
4214 level=minlevel; | |
4215 overflow++; | |
4216 } | |
4217 | |
4218 block[j]= level; | |
4219 } | |
4220 | |
4221 if(overflow && s->avctx->mb_decision == FF_MB_DECISION_SIMPLE) | |
4222 av_log(s->avctx, AV_LOG_INFO, "warning, clipping %d dct coefficients to %d..%d\n", overflow, minlevel, maxlevel); | |
4223 } | |
4224 | |
4225 #endif //CONFIG_ENCODERS | |
4226 | |
4227 /** | 2056 /** |
4228 * | 2057 * |
4229 * @param h is the normal height, this will be reduced automatically if needed for the last row | 2058 * @param h is the normal height, this will be reduced automatically if needed for the last row |
4230 */ | 2059 */ |
4231 void ff_draw_horiz_band(MpegEncContext *s, int y, int h){ | 2060 void ff_draw_horiz_band(MpegEncContext *s, int y, int h){ |
4290 s->dest[1] += s->mb_y * uvlinesize << (mb_size - s->chroma_y_shift); | 2119 s->dest[1] += s->mb_y * uvlinesize << (mb_size - s->chroma_y_shift); |
4291 s->dest[2] += s->mb_y * uvlinesize << (mb_size - s->chroma_y_shift); | 2120 s->dest[2] += s->mb_y * uvlinesize << (mb_size - s->chroma_y_shift); |
4292 } | 2121 } |
4293 } | 2122 } |
4294 | 2123 |
4295 #ifdef CONFIG_ENCODERS | |
4296 | |
4297 static void get_vissual_weight(int16_t *weight, uint8_t *ptr, int stride){ | |
4298 int x, y; | |
4299 //FIXME optimize | |
4300 for(y=0; y<8; y++){ | |
4301 for(x=0; x<8; x++){ | |
4302 int x2, y2; | |
4303 int sum=0; | |
4304 int sqr=0; | |
4305 int count=0; | |
4306 | |
4307 for(y2= FFMAX(y-1, 0); y2 < FFMIN(8, y+2); y2++){ | |
4308 for(x2= FFMAX(x-1, 0); x2 < FFMIN(8, x+2); x2++){ | |
4309 int v= ptr[x2 + y2*stride]; | |
4310 sum += v; | |
4311 sqr += v*v; | |
4312 count++; | |
4313 } | |
4314 } | |
4315 weight[x + 8*y]= (36*ff_sqrt(count*sqr - sum*sum)) / count; | |
4316 } | |
4317 } | |
4318 } | |
4319 | |
4320 static av_always_inline void encode_mb_internal(MpegEncContext *s, int motion_x, int motion_y, int mb_block_height, int mb_block_count) | |
4321 { | |
4322 int16_t weight[8][64]; | |
4323 DCTELEM orig[8][64]; | |
4324 const int mb_x= s->mb_x; | |
4325 const int mb_y= s->mb_y; | |
4326 int i; | |
4327 int skip_dct[8]; | |
4328 int dct_offset = s->linesize*8; //default for progressive frames | |
4329 uint8_t *ptr_y, *ptr_cb, *ptr_cr; | |
4330 int wrap_y, wrap_c; | |
4331 | |
4332 for(i=0; i<mb_block_count; i++) skip_dct[i]=s->skipdct; | |
4333 | |
4334 if(s->adaptive_quant){ | |
4335 const int last_qp= s->qscale; | |
4336 const int mb_xy= mb_x + mb_y*s->mb_stride; | |
4337 | |
4338 s->lambda= s->lambda_table[mb_xy]; | |
4339 update_qscale(s); | |
4340 | |
4341 if(!(s->flags&CODEC_FLAG_QP_RD)){ | |
4342 s->qscale= s->current_picture_ptr->qscale_table[mb_xy]; | |
4343 s->dquant= s->qscale - last_qp; | |
4344 | |
4345 if(s->out_format==FMT_H263){ | |
4346 s->dquant= av_clip(s->dquant, -2, 2); | |
4347 | |
4348 if(s->codec_id==CODEC_ID_MPEG4){ | |
4349 if(!s->mb_intra){ | |
4350 if(s->pict_type == B_TYPE){ | |
4351 if(s->dquant&1 || s->mv_dir&MV_DIRECT) | |
4352 s->dquant= 0; | |
4353 } | |
4354 if(s->mv_type==MV_TYPE_8X8) | |
4355 s->dquant=0; | |
4356 } | |
4357 } | |
4358 } | |
4359 } | |
4360 ff_set_qscale(s, last_qp + s->dquant); | |
4361 }else if(s->flags&CODEC_FLAG_QP_RD) | |
4362 ff_set_qscale(s, s->qscale + s->dquant); | |
4363 | |
4364 wrap_y = s->linesize; | |
4365 wrap_c = s->uvlinesize; | |
4366 ptr_y = s->new_picture.data[0] + (mb_y * 16 * wrap_y) + mb_x * 16; | |
4367 ptr_cb = s->new_picture.data[1] + (mb_y * mb_block_height * wrap_c) + mb_x * 8; | |
4368 ptr_cr = s->new_picture.data[2] + (mb_y * mb_block_height * wrap_c) + mb_x * 8; | |
4369 | |
4370 if(mb_x*16+16 > s->width || mb_y*16+16 > s->height){ | |
4371 uint8_t *ebuf= s->edge_emu_buffer + 32; | |
4372 ff_emulated_edge_mc(ebuf , ptr_y , wrap_y,16,16,mb_x*16,mb_y*16, s->width , s->height); | |
4373 ptr_y= ebuf; | |
4374 ff_emulated_edge_mc(ebuf+18*wrap_y , ptr_cb, wrap_c, 8, mb_block_height, mb_x*8, mb_y*8, s->width>>1, s->height>>1); | |
4375 ptr_cb= ebuf+18*wrap_y; | |
4376 ff_emulated_edge_mc(ebuf+18*wrap_y+8, ptr_cr, wrap_c, 8, mb_block_height, mb_x*8, mb_y*8, s->width>>1, s->height>>1); | |
4377 ptr_cr= ebuf+18*wrap_y+8; | |
4378 } | |
4379 | |
4380 if (s->mb_intra) { | |
4381 if(s->flags&CODEC_FLAG_INTERLACED_DCT){ | |
4382 int progressive_score, interlaced_score; | |
4383 | |
4384 s->interlaced_dct=0; | |
4385 progressive_score= s->dsp.ildct_cmp[4](s, ptr_y , NULL, wrap_y, 8) | |
4386 +s->dsp.ildct_cmp[4](s, ptr_y + wrap_y*8, NULL, wrap_y, 8) - 400; | |
4387 | |
4388 if(progressive_score > 0){ | |
4389 interlaced_score = s->dsp.ildct_cmp[4](s, ptr_y , NULL, wrap_y*2, 8) | |
4390 +s->dsp.ildct_cmp[4](s, ptr_y + wrap_y , NULL, wrap_y*2, 8); | |
4391 if(progressive_score > interlaced_score){ | |
4392 s->interlaced_dct=1; | |
4393 | |
4394 dct_offset= wrap_y; | |
4395 wrap_y<<=1; | |
4396 if (s->chroma_format == CHROMA_422) | |
4397 wrap_c<<=1; | |
4398 } | |
4399 } | |
4400 } | |
4401 | |
4402 s->dsp.get_pixels(s->block[0], ptr_y , wrap_y); | |
4403 s->dsp.get_pixels(s->block[1], ptr_y + 8, wrap_y); | |
4404 s->dsp.get_pixels(s->block[2], ptr_y + dct_offset , wrap_y); | |
4405 s->dsp.get_pixels(s->block[3], ptr_y + dct_offset + 8, wrap_y); | |
4406 | |
4407 if(s->flags&CODEC_FLAG_GRAY){ | |
4408 skip_dct[4]= 1; | |
4409 skip_dct[5]= 1; | |
4410 }else{ | |
4411 s->dsp.get_pixels(s->block[4], ptr_cb, wrap_c); | |
4412 s->dsp.get_pixels(s->block[5], ptr_cr, wrap_c); | |
4413 if(!s->chroma_y_shift){ /* 422 */ | |
4414 s->dsp.get_pixels(s->block[6], ptr_cb + (dct_offset>>1), wrap_c); | |
4415 s->dsp.get_pixels(s->block[7], ptr_cr + (dct_offset>>1), wrap_c); | |
4416 } | |
4417 } | |
4418 }else{ | |
4419 op_pixels_func (*op_pix)[4]; | |
4420 qpel_mc_func (*op_qpix)[16]; | |
4421 uint8_t *dest_y, *dest_cb, *dest_cr; | |
4422 | |
4423 dest_y = s->dest[0]; | |
4424 dest_cb = s->dest[1]; | |
4425 dest_cr = s->dest[2]; | |
4426 | |
4427 if ((!s->no_rounding) || s->pict_type==B_TYPE){ | |
4428 op_pix = s->dsp.put_pixels_tab; | |
4429 op_qpix= s->dsp.put_qpel_pixels_tab; | |
4430 }else{ | |
4431 op_pix = s->dsp.put_no_rnd_pixels_tab; | |
4432 op_qpix= s->dsp.put_no_rnd_qpel_pixels_tab; | |
4433 } | |
4434 | |
4435 if (s->mv_dir & MV_DIR_FORWARD) { | |
4436 MPV_motion(s, dest_y, dest_cb, dest_cr, 0, s->last_picture.data, op_pix, op_qpix); | |
4437 op_pix = s->dsp.avg_pixels_tab; | |
4438 op_qpix= s->dsp.avg_qpel_pixels_tab; | |
4439 } | |
4440 if (s->mv_dir & MV_DIR_BACKWARD) { | |
4441 MPV_motion(s, dest_y, dest_cb, dest_cr, 1, s->next_picture.data, op_pix, op_qpix); | |
4442 } | |
4443 | |
4444 if(s->flags&CODEC_FLAG_INTERLACED_DCT){ | |
4445 int progressive_score, interlaced_score; | |
4446 | |
4447 s->interlaced_dct=0; | |
4448 progressive_score= s->dsp.ildct_cmp[0](s, dest_y , ptr_y , wrap_y, 8) | |
4449 +s->dsp.ildct_cmp[0](s, dest_y + wrap_y*8, ptr_y + wrap_y*8, wrap_y, 8) - 400; | |
4450 | |
4451 if(s->avctx->ildct_cmp == FF_CMP_VSSE) progressive_score -= 400; | |
4452 | |
4453 if(progressive_score>0){ | |
4454 interlaced_score = s->dsp.ildct_cmp[0](s, dest_y , ptr_y , wrap_y*2, 8) | |
4455 +s->dsp.ildct_cmp[0](s, dest_y + wrap_y , ptr_y + wrap_y , wrap_y*2, 8); | |
4456 | |
4457 if(progressive_score > interlaced_score){ | |
4458 s->interlaced_dct=1; | |
4459 | |
4460 dct_offset= wrap_y; | |
4461 wrap_y<<=1; | |
4462 if (s->chroma_format == CHROMA_422) | |
4463 wrap_c<<=1; | |
4464 } | |
4465 } | |
4466 } | |
4467 | |
4468 s->dsp.diff_pixels(s->block[0], ptr_y , dest_y , wrap_y); | |
4469 s->dsp.diff_pixels(s->block[1], ptr_y + 8, dest_y + 8, wrap_y); | |
4470 s->dsp.diff_pixels(s->block[2], ptr_y + dct_offset , dest_y + dct_offset , wrap_y); | |
4471 s->dsp.diff_pixels(s->block[3], ptr_y + dct_offset + 8, dest_y + dct_offset + 8, wrap_y); | |
4472 | |
4473 if(s->flags&CODEC_FLAG_GRAY){ | |
4474 skip_dct[4]= 1; | |
4475 skip_dct[5]= 1; | |
4476 }else{ | |
4477 s->dsp.diff_pixels(s->block[4], ptr_cb, dest_cb, wrap_c); | |
4478 s->dsp.diff_pixels(s->block[5], ptr_cr, dest_cr, wrap_c); | |
4479 if(!s->chroma_y_shift){ /* 422 */ | |
4480 s->dsp.diff_pixels(s->block[6], ptr_cb + (dct_offset>>1), dest_cb + (dct_offset>>1), wrap_c); | |
4481 s->dsp.diff_pixels(s->block[7], ptr_cr + (dct_offset>>1), dest_cr + (dct_offset>>1), wrap_c); | |
4482 } | |
4483 } | |
4484 /* pre quantization */ | |
4485 if(s->current_picture.mc_mb_var[s->mb_stride*mb_y+ mb_x]<2*s->qscale*s->qscale){ | |
4486 //FIXME optimize | |
4487 if(s->dsp.sad[1](NULL, ptr_y , dest_y , wrap_y, 8) < 20*s->qscale) skip_dct[0]= 1; | |
4488 if(s->dsp.sad[1](NULL, ptr_y + 8, dest_y + 8, wrap_y, 8) < 20*s->qscale) skip_dct[1]= 1; | |
4489 if(s->dsp.sad[1](NULL, ptr_y +dct_offset , dest_y +dct_offset , wrap_y, 8) < 20*s->qscale) skip_dct[2]= 1; | |
4490 if(s->dsp.sad[1](NULL, ptr_y +dct_offset+ 8, dest_y +dct_offset+ 8, wrap_y, 8) < 20*s->qscale) skip_dct[3]= 1; | |
4491 if(s->dsp.sad[1](NULL, ptr_cb , dest_cb , wrap_c, 8) < 20*s->qscale) skip_dct[4]= 1; | |
4492 if(s->dsp.sad[1](NULL, ptr_cr , dest_cr , wrap_c, 8) < 20*s->qscale) skip_dct[5]= 1; | |
4493 if(!s->chroma_y_shift){ /* 422 */ | |
4494 if(s->dsp.sad[1](NULL, ptr_cb +(dct_offset>>1), dest_cb +(dct_offset>>1), wrap_c, 8) < 20*s->qscale) skip_dct[6]= 1; | |
4495 if(s->dsp.sad[1](NULL, ptr_cr +(dct_offset>>1), dest_cr +(dct_offset>>1), wrap_c, 8) < 20*s->qscale) skip_dct[7]= 1; | |
4496 } | |
4497 } | |
4498 } | |
4499 | |
4500 if(s->avctx->quantizer_noise_shaping){ | |
4501 if(!skip_dct[0]) get_vissual_weight(weight[0], ptr_y , wrap_y); | |
4502 if(!skip_dct[1]) get_vissual_weight(weight[1], ptr_y + 8, wrap_y); | |
4503 if(!skip_dct[2]) get_vissual_weight(weight[2], ptr_y + dct_offset , wrap_y); | |
4504 if(!skip_dct[3]) get_vissual_weight(weight[3], ptr_y + dct_offset + 8, wrap_y); | |
4505 if(!skip_dct[4]) get_vissual_weight(weight[4], ptr_cb , wrap_c); | |
4506 if(!skip_dct[5]) get_vissual_weight(weight[5], ptr_cr , wrap_c); | |
4507 if(!s->chroma_y_shift){ /* 422 */ | |
4508 if(!skip_dct[6]) get_vissual_weight(weight[6], ptr_cb + (dct_offset>>1), wrap_c); | |
4509 if(!skip_dct[7]) get_vissual_weight(weight[7], ptr_cr + (dct_offset>>1), wrap_c); | |
4510 } | |
4511 memcpy(orig[0], s->block[0], sizeof(DCTELEM)*64*mb_block_count); | |
4512 } | |
4513 | |
4514 /* DCT & quantize */ | |
4515 assert(s->out_format!=FMT_MJPEG || s->qscale==8); | |
4516 { | |
4517 for(i=0;i<mb_block_count;i++) { | |
4518 if(!skip_dct[i]){ | |
4519 int overflow; | |
4520 s->block_last_index[i] = s->dct_quantize(s, s->block[i], i, s->qscale, &overflow); | |
4521 // FIXME we could decide to change to quantizer instead of clipping | |
4522 // JS: I don't think that would be a good idea it could lower quality instead | |
4523 // of improve it. Just INTRADC clipping deserves changes in quantizer | |
4524 if (overflow) clip_coeffs(s, s->block[i], s->block_last_index[i]); | |
4525 }else | |
4526 s->block_last_index[i]= -1; | |
4527 } | |
4528 if(s->avctx->quantizer_noise_shaping){ | |
4529 for(i=0;i<mb_block_count;i++) { | |
4530 if(!skip_dct[i]){ | |
4531 s->block_last_index[i] = dct_quantize_refine(s, s->block[i], weight[i], orig[i], i, s->qscale); | |
4532 } | |
4533 } | |
4534 } | |
4535 | |
4536 if(s->luma_elim_threshold && !s->mb_intra) | |
4537 for(i=0; i<4; i++) | |
4538 dct_single_coeff_elimination(s, i, s->luma_elim_threshold); | |
4539 if(s->chroma_elim_threshold && !s->mb_intra) | |
4540 for(i=4; i<mb_block_count; i++) | |
4541 dct_single_coeff_elimination(s, i, s->chroma_elim_threshold); | |
4542 | |
4543 if(s->flags & CODEC_FLAG_CBP_RD){ | |
4544 for(i=0;i<mb_block_count;i++) { | |
4545 if(s->block_last_index[i] == -1) | |
4546 s->coded_score[i]= INT_MAX/256; | |
4547 } | |
4548 } | |
4549 } | |
4550 | |
4551 if((s->flags&CODEC_FLAG_GRAY) && s->mb_intra){ | |
4552 s->block_last_index[4]= | |
4553 s->block_last_index[5]= 0; | |
4554 s->block[4][0]= | |
4555 s->block[5][0]= (1024 + s->c_dc_scale/2)/ s->c_dc_scale; | |
4556 } | |
4557 | |
4558 //non c quantize code returns incorrect block_last_index FIXME | |
4559 if(s->alternate_scan && s->dct_quantize != dct_quantize_c){ | |
4560 for(i=0; i<mb_block_count; i++){ | |
4561 int j; | |
4562 if(s->block_last_index[i]>0){ | |
4563 for(j=63; j>0; j--){ | |
4564 if(s->block[i][ s->intra_scantable.permutated[j] ]) break; | |
4565 } | |
4566 s->block_last_index[i]= j; | |
4567 } | |
4568 } | |
4569 } | |
4570 | |
4571 /* huffman encode */ | |
4572 switch(s->codec_id){ //FIXME funct ptr could be slightly faster | |
4573 case CODEC_ID_MPEG1VIDEO: | |
4574 case CODEC_ID_MPEG2VIDEO: | |
4575 mpeg1_encode_mb(s, s->block, motion_x, motion_y); break; | |
4576 case CODEC_ID_MPEG4: | |
4577 mpeg4_encode_mb(s, s->block, motion_x, motion_y); break; | |
4578 case CODEC_ID_MSMPEG4V2: | |
4579 case CODEC_ID_MSMPEG4V3: | |
4580 case CODEC_ID_WMV1: | |
4581 if (ENABLE_MSMPEG4_ENCODER) | |
4582 msmpeg4_encode_mb(s, s->block, motion_x, motion_y); | |
4583 break; | |
4584 case CODEC_ID_WMV2: | |
4585 if (ENABLE_WMV2_ENCODER) | |
4586 ff_wmv2_encode_mb(s, s->block, motion_x, motion_y); | |
4587 break; | |
4588 case CODEC_ID_H261: | |
4589 if (ENABLE_H261_ENCODER) | |
4590 ff_h261_encode_mb(s, s->block, motion_x, motion_y); | |
4591 break; | |
4592 case CODEC_ID_H263: | |
4593 case CODEC_ID_H263P: | |
4594 case CODEC_ID_FLV1: | |
4595 case CODEC_ID_RV10: | |
4596 case CODEC_ID_RV20: | |
4597 h263_encode_mb(s, s->block, motion_x, motion_y); break; | |
4598 case CODEC_ID_MJPEG: | |
4599 if (ENABLE_MJPEG_ENCODER) | |
4600 ff_mjpeg_encode_mb(s, s->block); | |
4601 break; | |
4602 default: | |
4603 assert(0); | |
4604 } | |
4605 } | |
4606 | |
4607 static av_always_inline void encode_mb(MpegEncContext *s, int motion_x, int motion_y) | |
4608 { | |
4609 if (s->chroma_format == CHROMA_420) encode_mb_internal(s, motion_x, motion_y, 8, 6); | |
4610 else encode_mb_internal(s, motion_x, motion_y, 16, 8); | |
4611 } | |
4612 | |
4613 #endif //CONFIG_ENCODERS | |
4614 | |
4615 void ff_mpeg_flush(AVCodecContext *avctx){ | 2124 void ff_mpeg_flush(AVCodecContext *avctx){ |
4616 int i; | 2125 int i; |
4617 MpegEncContext *s = avctx->priv_data; | 2126 MpegEncContext *s = avctx->priv_data; |
4618 | 2127 |
4619 if(s==NULL || s->picture==NULL) | 2128 if(s==NULL || s->picture==NULL) |
4635 s->parse_context.index= 0; | 2144 s->parse_context.index= 0; |
4636 s->parse_context.last_index= 0; | 2145 s->parse_context.last_index= 0; |
4637 s->bitstream_buffer_size=0; | 2146 s->bitstream_buffer_size=0; |
4638 s->pp_time=0; | 2147 s->pp_time=0; |
4639 } | 2148 } |
4640 | |
4641 #ifdef CONFIG_ENCODERS | |
4642 void ff_copy_bits(PutBitContext *pb, uint8_t *src, int length) | |
4643 { | |
4644 const uint16_t *srcw= (uint16_t*)src; | |
4645 int words= length>>4; | |
4646 int bits= length&15; | |
4647 int i; | |
4648 | |
4649 if(length==0) return; | |
4650 | |
4651 if(words < 16){ | |
4652 for(i=0; i<words; i++) put_bits(pb, 16, be2me_16(srcw[i])); | |
4653 }else if(put_bits_count(pb)&7){ | |
4654 for(i=0; i<words; i++) put_bits(pb, 16, be2me_16(srcw[i])); | |
4655 }else{ | |
4656 for(i=0; put_bits_count(pb)&31; i++) | |
4657 put_bits(pb, 8, src[i]); | |
4658 flush_put_bits(pb); | |
4659 memcpy(pbBufPtr(pb), src+i, 2*words-i); | |
4660 skip_put_bytes(pb, 2*words-i); | |
4661 } | |
4662 | |
4663 put_bits(pb, bits, be2me_16(srcw[words])>>(16-bits)); | |
4664 } | |
4665 | |
4666 static inline void copy_context_before_encode(MpegEncContext *d, MpegEncContext *s, int type){ | |
4667 int i; | |
4668 | |
4669 memcpy(d->last_mv, s->last_mv, 2*2*2*sizeof(int)); //FIXME is memcpy faster then a loop? | |
4670 | |
4671 /* mpeg1 */ | |
4672 d->mb_skip_run= s->mb_skip_run; | |
4673 for(i=0; i<3; i++) | |
4674 d->last_dc[i]= s->last_dc[i]; | |
4675 | |
4676 /* statistics */ | |
4677 d->mv_bits= s->mv_bits; | |
4678 d->i_tex_bits= s->i_tex_bits; | |
4679 d->p_tex_bits= s->p_tex_bits; | |
4680 d->i_count= s->i_count; | |
4681 d->f_count= s->f_count; | |
4682 d->b_count= s->b_count; | |
4683 d->skip_count= s->skip_count; | |
4684 d->misc_bits= s->misc_bits; | |
4685 d->last_bits= 0; | |
4686 | |
4687 d->mb_skipped= 0; | |
4688 d->qscale= s->qscale; | |
4689 d->dquant= s->dquant; | |
4690 } | |
4691 | |
4692 static inline void copy_context_after_encode(MpegEncContext *d, MpegEncContext *s, int type){ | |
4693 int i; | |
4694 | |
4695 memcpy(d->mv, s->mv, 2*4*2*sizeof(int)); | |
4696 memcpy(d->last_mv, s->last_mv, 2*2*2*sizeof(int)); //FIXME is memcpy faster then a loop? | |
4697 | |
4698 /* mpeg1 */ | |
4699 d->mb_skip_run= s->mb_skip_run; | |
4700 for(i=0; i<3; i++) | |
4701 d->last_dc[i]= s->last_dc[i]; | |
4702 | |
4703 /* statistics */ | |
4704 d->mv_bits= s->mv_bits; | |
4705 d->i_tex_bits= s->i_tex_bits; | |
4706 d->p_tex_bits= s->p_tex_bits; | |
4707 d->i_count= s->i_count; | |
4708 d->f_count= s->f_count; | |
4709 d->b_count= s->b_count; | |
4710 d->skip_count= s->skip_count; | |
4711 d->misc_bits= s->misc_bits; | |
4712 | |
4713 d->mb_intra= s->mb_intra; | |
4714 d->mb_skipped= s->mb_skipped; | |
4715 d->mv_type= s->mv_type; | |
4716 d->mv_dir= s->mv_dir; | |
4717 d->pb= s->pb; | |
4718 if(s->data_partitioning){ | |
4719 d->pb2= s->pb2; | |
4720 d->tex_pb= s->tex_pb; | |
4721 } | |
4722 d->block= s->block; | |
4723 for(i=0; i<8; i++) | |
4724 d->block_last_index[i]= s->block_last_index[i]; | |
4725 d->interlaced_dct= s->interlaced_dct; | |
4726 d->qscale= s->qscale; | |
4727 } | |
4728 | |
4729 static inline void encode_mb_hq(MpegEncContext *s, MpegEncContext *backup, MpegEncContext *best, int type, | |
4730 PutBitContext pb[2], PutBitContext pb2[2], PutBitContext tex_pb[2], | |
4731 int *dmin, int *next_block, int motion_x, int motion_y) | |
4732 { | |
4733 int score; | |
4734 uint8_t *dest_backup[3]; | |
4735 | |
4736 copy_context_before_encode(s, backup, type); | |
4737 | |
4738 s->block= s->blocks[*next_block]; | |
4739 s->pb= pb[*next_block]; | |
4740 if(s->data_partitioning){ | |
4741 s->pb2 = pb2 [*next_block]; | |
4742 s->tex_pb= tex_pb[*next_block]; | |
4743 } | |
4744 | |
4745 if(*next_block){ | |
4746 memcpy(dest_backup, s->dest, sizeof(s->dest)); | |
4747 s->dest[0] = s->rd_scratchpad; | |
4748 s->dest[1] = s->rd_scratchpad + 16*s->linesize; | |
4749 s->dest[2] = s->rd_scratchpad + 16*s->linesize + 8; | |
4750 assert(s->linesize >= 32); //FIXME | |
4751 } | |
4752 | |
4753 encode_mb(s, motion_x, motion_y); | |
4754 | |
4755 score= put_bits_count(&s->pb); | |
4756 if(s->data_partitioning){ | |
4757 score+= put_bits_count(&s->pb2); | |
4758 score+= put_bits_count(&s->tex_pb); | |
4759 } | |
4760 | |
4761 if(s->avctx->mb_decision == FF_MB_DECISION_RD){ | |
4762 MPV_decode_mb(s, s->block); | |
4763 | |
4764 score *= s->lambda2; | |
4765 score += sse_mb(s) << FF_LAMBDA_SHIFT; | |
4766 } | |
4767 | |
4768 if(*next_block){ | |
4769 memcpy(s->dest, dest_backup, sizeof(s->dest)); | |
4770 } | |
4771 | |
4772 if(score<*dmin){ | |
4773 *dmin= score; | |
4774 *next_block^=1; | |
4775 | |
4776 copy_context_after_encode(best, s, type); | |
4777 } | |
4778 } | |
4779 | |
4780 static int sse(MpegEncContext *s, uint8_t *src1, uint8_t *src2, int w, int h, int stride){ | |
4781 uint32_t *sq = ff_squareTbl + 256; | |
4782 int acc=0; | |
4783 int x,y; | |
4784 | |
4785 if(w==16 && h==16) | |
4786 return s->dsp.sse[0](NULL, src1, src2, stride, 16); | |
4787 else if(w==8 && h==8) | |
4788 return s->dsp.sse[1](NULL, src1, src2, stride, 8); | |
4789 | |
4790 for(y=0; y<h; y++){ | |
4791 for(x=0; x<w; x++){ | |
4792 acc+= sq[src1[x + y*stride] - src2[x + y*stride]]; | |
4793 } | |
4794 } | |
4795 | |
4796 assert(acc>=0); | |
4797 | |
4798 return acc; | |
4799 } | |
4800 | |
4801 static int sse_mb(MpegEncContext *s){ | |
4802 int w= 16; | |
4803 int h= 16; | |
4804 | |
4805 if(s->mb_x*16 + 16 > s->width ) w= s->width - s->mb_x*16; | |
4806 if(s->mb_y*16 + 16 > s->height) h= s->height- s->mb_y*16; | |
4807 | |
4808 if(w==16 && h==16) | |
4809 if(s->avctx->mb_cmp == FF_CMP_NSSE){ | |
4810 return s->dsp.nsse[0](s, s->new_picture.data[0] + s->mb_x*16 + s->mb_y*s->linesize*16, s->dest[0], s->linesize, 16) | |
4811 +s->dsp.nsse[1](s, s->new_picture.data[1] + s->mb_x*8 + s->mb_y*s->uvlinesize*8,s->dest[1], s->uvlinesize, 8) | |
4812 +s->dsp.nsse[1](s, s->new_picture.data[2] + s->mb_x*8 + s->mb_y*s->uvlinesize*8,s->dest[2], s->uvlinesize, 8); | |
4813 }else{ | |
4814 return s->dsp.sse[0](NULL, s->new_picture.data[0] + s->mb_x*16 + s->mb_y*s->linesize*16, s->dest[0], s->linesize, 16) | |
4815 +s->dsp.sse[1](NULL, s->new_picture.data[1] + s->mb_x*8 + s->mb_y*s->uvlinesize*8,s->dest[1], s->uvlinesize, 8) | |
4816 +s->dsp.sse[1](NULL, s->new_picture.data[2] + s->mb_x*8 + s->mb_y*s->uvlinesize*8,s->dest[2], s->uvlinesize, 8); | |
4817 } | |
4818 else | |
4819 return sse(s, s->new_picture.data[0] + s->mb_x*16 + s->mb_y*s->linesize*16, s->dest[0], w, h, s->linesize) | |
4820 +sse(s, s->new_picture.data[1] + s->mb_x*8 + s->mb_y*s->uvlinesize*8,s->dest[1], w>>1, h>>1, s->uvlinesize) | |
4821 +sse(s, s->new_picture.data[2] + s->mb_x*8 + s->mb_y*s->uvlinesize*8,s->dest[2], w>>1, h>>1, s->uvlinesize); | |
4822 } | |
4823 | |
4824 static int pre_estimate_motion_thread(AVCodecContext *c, void *arg){ | |
4825 MpegEncContext *s= arg; | |
4826 | |
4827 | |
4828 s->me.pre_pass=1; | |
4829 s->me.dia_size= s->avctx->pre_dia_size; | |
4830 s->first_slice_line=1; | |
4831 for(s->mb_y= s->end_mb_y-1; s->mb_y >= s->start_mb_y; s->mb_y--) { | |
4832 for(s->mb_x=s->mb_width-1; s->mb_x >=0 ;s->mb_x--) { | |
4833 ff_pre_estimate_p_frame_motion(s, s->mb_x, s->mb_y); | |
4834 } | |
4835 s->first_slice_line=0; | |
4836 } | |
4837 | |
4838 s->me.pre_pass=0; | |
4839 | |
4840 return 0; | |
4841 } | |
4842 | |
4843 static int estimate_motion_thread(AVCodecContext *c, void *arg){ | |
4844 MpegEncContext *s= arg; | |
4845 | |
4846 ff_check_alignment(); | |
4847 | |
4848 s->me.dia_size= s->avctx->dia_size; | |
4849 s->first_slice_line=1; | |
4850 for(s->mb_y= s->start_mb_y; s->mb_y < s->end_mb_y; s->mb_y++) { | |
4851 s->mb_x=0; //for block init below | |
4852 ff_init_block_index(s); | |
4853 for(s->mb_x=0; s->mb_x < s->mb_width; s->mb_x++) { | |
4854 s->block_index[0]+=2; | |
4855 s->block_index[1]+=2; | |
4856 s->block_index[2]+=2; | |
4857 s->block_index[3]+=2; | |
4858 | |
4859 /* compute motion vector & mb_type and store in context */ | |
4860 if(s->pict_type==B_TYPE) | |
4861 ff_estimate_b_frame_motion(s, s->mb_x, s->mb_y); | |
4862 else | |
4863 ff_estimate_p_frame_motion(s, s->mb_x, s->mb_y); | |
4864 } | |
4865 s->first_slice_line=0; | |
4866 } | |
4867 return 0; | |
4868 } | |
4869 | |
4870 static int mb_var_thread(AVCodecContext *c, void *arg){ | |
4871 MpegEncContext *s= arg; | |
4872 int mb_x, mb_y; | |
4873 | |
4874 ff_check_alignment(); | |
4875 | |
4876 for(mb_y=s->start_mb_y; mb_y < s->end_mb_y; mb_y++) { | |
4877 for(mb_x=0; mb_x < s->mb_width; mb_x++) { | |
4878 int xx = mb_x * 16; | |
4879 int yy = mb_y * 16; | |
4880 uint8_t *pix = s->new_picture.data[0] + (yy * s->linesize) + xx; | |
4881 int varc; | |
4882 int sum = s->dsp.pix_sum(pix, s->linesize); | |
4883 | |
4884 varc = (s->dsp.pix_norm1(pix, s->linesize) - (((unsigned)(sum*sum))>>8) + 500 + 128)>>8; | |
4885 | |
4886 s->current_picture.mb_var [s->mb_stride * mb_y + mb_x] = varc; | |
4887 s->current_picture.mb_mean[s->mb_stride * mb_y + mb_x] = (sum+128)>>8; | |
4888 s->me.mb_var_sum_temp += varc; | |
4889 } | |
4890 } | |
4891 return 0; | |
4892 } | |
4893 | |
4894 static void write_slice_end(MpegEncContext *s){ | |
4895 if(s->codec_id==CODEC_ID_MPEG4){ | |
4896 if(s->partitioned_frame){ | |
4897 ff_mpeg4_merge_partitions(s); | |
4898 } | |
4899 | |
4900 ff_mpeg4_stuffing(&s->pb); | |
4901 }else if(ENABLE_MJPEG_ENCODER && s->out_format == FMT_MJPEG){ | |
4902 ff_mjpeg_encode_stuffing(&s->pb); | |
4903 } | |
4904 | |
4905 align_put_bits(&s->pb); | |
4906 flush_put_bits(&s->pb); | |
4907 | |
4908 if((s->flags&CODEC_FLAG_PASS1) && !s->partitioned_frame) | |
4909 s->misc_bits+= get_bits_diff(s); | |
4910 } | |
4911 | |
4912 static int encode_thread(AVCodecContext *c, void *arg){ | |
4913 MpegEncContext *s= arg; | |
4914 int mb_x, mb_y, pdif = 0; | |
4915 int i, j; | |
4916 MpegEncContext best_s, backup_s; | |
4917 uint8_t bit_buf[2][MAX_MB_BYTES]; | |
4918 uint8_t bit_buf2[2][MAX_MB_BYTES]; | |
4919 uint8_t bit_buf_tex[2][MAX_MB_BYTES]; | |
4920 PutBitContext pb[2], pb2[2], tex_pb[2]; | |
4921 //printf("%d->%d\n", s->resync_mb_y, s->end_mb_y); | |
4922 | |
4923 ff_check_alignment(); | |
4924 | |
4925 for(i=0; i<2; i++){ | |
4926 init_put_bits(&pb [i], bit_buf [i], MAX_MB_BYTES); | |
4927 init_put_bits(&pb2 [i], bit_buf2 [i], MAX_MB_BYTES); | |
4928 init_put_bits(&tex_pb[i], bit_buf_tex[i], MAX_MB_BYTES); | |
4929 } | |
4930 | |
4931 s->last_bits= put_bits_count(&s->pb); | |
4932 s->mv_bits=0; | |
4933 s->misc_bits=0; | |
4934 s->i_tex_bits=0; | |
4935 s->p_tex_bits=0; | |
4936 s->i_count=0; | |
4937 s->f_count=0; | |
4938 s->b_count=0; | |
4939 s->skip_count=0; | |
4940 | |
4941 for(i=0; i<3; i++){ | |
4942 /* init last dc values */ | |
4943 /* note: quant matrix value (8) is implied here */ | |
4944 s->last_dc[i] = 128 << s->intra_dc_precision; | |
4945 | |
4946 s->current_picture.error[i] = 0; | |
4947 } | |
4948 s->mb_skip_run = 0; | |
4949 memset(s->last_mv, 0, sizeof(s->last_mv)); | |
4950 | |
4951 s->last_mv_dir = 0; | |
4952 | |
4953 switch(s->codec_id){ | |
4954 case CODEC_ID_H263: | |
4955 case CODEC_ID_H263P: | |
4956 case CODEC_ID_FLV1: | |
4957 s->gob_index = ff_h263_get_gob_height(s); | |
4958 break; | |
4959 case CODEC_ID_MPEG4: | |
4960 if(s->partitioned_frame) | |
4961 ff_mpeg4_init_partitions(s); | |
4962 break; | |
4963 } | |
4964 | |
4965 s->resync_mb_x=0; | |
4966 s->resync_mb_y=0; | |
4967 s->first_slice_line = 1; | |
4968 s->ptr_lastgob = s->pb.buf; | |
4969 for(mb_y= s->start_mb_y; mb_y < s->end_mb_y; mb_y++) { | |
4970 // printf("row %d at %X\n", s->mb_y, (int)s); | |
4971 s->mb_x=0; | |
4972 s->mb_y= mb_y; | |
4973 | |
4974 ff_set_qscale(s, s->qscale); | |
4975 ff_init_block_index(s); | |
4976 | |
4977 for(mb_x=0; mb_x < s->mb_width; mb_x++) { | |
4978 int xy= mb_y*s->mb_stride + mb_x; // removed const, H261 needs to adjust this | |
4979 int mb_type= s->mb_type[xy]; | |
4980 // int d; | |
4981 int dmin= INT_MAX; | |
4982 int dir; | |
4983 | |
4984 if(s->pb.buf_end - s->pb.buf - (put_bits_count(&s->pb)>>3) < MAX_MB_BYTES){ | |
4985 av_log(s->avctx, AV_LOG_ERROR, "encoded frame too large\n"); | |
4986 return -1; | |
4987 } | |
4988 if(s->data_partitioning){ | |
4989 if( s->pb2 .buf_end - s->pb2 .buf - (put_bits_count(&s-> pb2)>>3) < MAX_MB_BYTES | |
4990 || s->tex_pb.buf_end - s->tex_pb.buf - (put_bits_count(&s->tex_pb )>>3) < MAX_MB_BYTES){ | |
4991 av_log(s->avctx, AV_LOG_ERROR, "encoded frame too large\n"); | |
4992 return -1; | |
4993 } | |
4994 } | |
4995 | |
4996 s->mb_x = mb_x; | |
4997 s->mb_y = mb_y; // moved into loop, can get changed by H.261 | |
4998 ff_update_block_index(s); | |
4999 | |
5000 if(ENABLE_H261_ENCODER && s->codec_id == CODEC_ID_H261){ | |
5001 ff_h261_reorder_mb_index(s); | |
5002 xy= s->mb_y*s->mb_stride + s->mb_x; | |
5003 mb_type= s->mb_type[xy]; | |
5004 } | |
5005 | |
5006 /* write gob / video packet header */ | |
5007 if(s->rtp_mode){ | |
5008 int current_packet_size, is_gob_start; | |
5009 | |
5010 current_packet_size= ((put_bits_count(&s->pb)+7)>>3) - (s->ptr_lastgob - s->pb.buf); | |
5011 | |
5012 is_gob_start= s->avctx->rtp_payload_size && current_packet_size >= s->avctx->rtp_payload_size && mb_y + mb_x>0; | |
5013 | |
5014 if(s->start_mb_y == mb_y && mb_y > 0 && mb_x==0) is_gob_start=1; | |
5015 | |
5016 switch(s->codec_id){ | |
5017 case CODEC_ID_H263: | |
5018 case CODEC_ID_H263P: | |
5019 if(!s->h263_slice_structured) | |
5020 if(s->mb_x || s->mb_y%s->gob_index) is_gob_start=0; | |
5021 break; | |
5022 case CODEC_ID_MPEG2VIDEO: | |
5023 if(s->mb_x==0 && s->mb_y!=0) is_gob_start=1; | |
5024 case CODEC_ID_MPEG1VIDEO: | |
5025 if(s->mb_skip_run) is_gob_start=0; | |
5026 break; | |
5027 } | |
5028 | |
5029 if(is_gob_start){ | |
5030 if(s->start_mb_y != mb_y || mb_x!=0){ | |
5031 write_slice_end(s); | |
5032 | |
5033 if(s->codec_id==CODEC_ID_MPEG4 && s->partitioned_frame){ | |
5034 ff_mpeg4_init_partitions(s); | |
5035 } | |
5036 } | |
5037 | |
5038 assert((put_bits_count(&s->pb)&7) == 0); | |
5039 current_packet_size= pbBufPtr(&s->pb) - s->ptr_lastgob; | |
5040 | |
5041 if(s->avctx->error_rate && s->resync_mb_x + s->resync_mb_y > 0){ | |
5042 int r= put_bits_count(&s->pb)/8 + s->picture_number + 16 + s->mb_x + s->mb_y; | |
5043 int d= 100 / s->avctx->error_rate; | |
5044 if(r % d == 0){ | |
5045 current_packet_size=0; | |
5046 #ifndef ALT_BITSTREAM_WRITER | |
5047 s->pb.buf_ptr= s->ptr_lastgob; | |
5048 #endif | |
5049 assert(pbBufPtr(&s->pb) == s->ptr_lastgob); | |
5050 } | |
5051 } | |
5052 | |
5053 if (s->avctx->rtp_callback){ | |
5054 int number_mb = (mb_y - s->resync_mb_y)*s->mb_width + mb_x - s->resync_mb_x; | |
5055 s->avctx->rtp_callback(s->avctx, s->ptr_lastgob, current_packet_size, number_mb); | |
5056 } | |
5057 | |
5058 switch(s->codec_id){ | |
5059 case CODEC_ID_MPEG4: | |
5060 ff_mpeg4_encode_video_packet_header(s); | |
5061 ff_mpeg4_clean_buffers(s); | |
5062 break; | |
5063 case CODEC_ID_MPEG1VIDEO: | |
5064 case CODEC_ID_MPEG2VIDEO: | |
5065 ff_mpeg1_encode_slice_header(s); | |
5066 ff_mpeg1_clean_buffers(s); | |
5067 break; | |
5068 case CODEC_ID_H263: | |
5069 case CODEC_ID_H263P: | |
5070 h263_encode_gob_header(s, mb_y); | |
5071 break; | |
5072 } | |
5073 | |
5074 if(s->flags&CODEC_FLAG_PASS1){ | |
5075 int bits= put_bits_count(&s->pb); | |
5076 s->misc_bits+= bits - s->last_bits; | |
5077 s->last_bits= bits; | |
5078 } | |
5079 | |
5080 s->ptr_lastgob += current_packet_size; | |
5081 s->first_slice_line=1; | |
5082 s->resync_mb_x=mb_x; | |
5083 s->resync_mb_y=mb_y; | |
5084 } | |
5085 } | |
5086 | |
5087 if( (s->resync_mb_x == s->mb_x) | |
5088 && s->resync_mb_y+1 == s->mb_y){ | |
5089 s->first_slice_line=0; | |
5090 } | |
5091 | |
5092 s->mb_skipped=0; | |
5093 s->dquant=0; //only for QP_RD | |
5094 | |
5095 if(mb_type & (mb_type-1) || (s->flags & CODEC_FLAG_QP_RD)){ // more than 1 MB type possible or CODEC_FLAG_QP_RD | |
5096 int next_block=0; | |
5097 int pb_bits_count, pb2_bits_count, tex_pb_bits_count; | |
5098 | |
5099 copy_context_before_encode(&backup_s, s, -1); | |
5100 backup_s.pb= s->pb; | |
5101 best_s.data_partitioning= s->data_partitioning; | |
5102 best_s.partitioned_frame= s->partitioned_frame; | |
5103 if(s->data_partitioning){ | |
5104 backup_s.pb2= s->pb2; | |
5105 backup_s.tex_pb= s->tex_pb; | |
5106 } | |
5107 | |
5108 if(mb_type&CANDIDATE_MB_TYPE_INTER){ | |
5109 s->mv_dir = MV_DIR_FORWARD; | |
5110 s->mv_type = MV_TYPE_16X16; | |
5111 s->mb_intra= 0; | |
5112 s->mv[0][0][0] = s->p_mv_table[xy][0]; | |
5113 s->mv[0][0][1] = s->p_mv_table[xy][1]; | |
5114 encode_mb_hq(s, &backup_s, &best_s, CANDIDATE_MB_TYPE_INTER, pb, pb2, tex_pb, | |
5115 &dmin, &next_block, s->mv[0][0][0], s->mv[0][0][1]); | |
5116 } | |
5117 if(mb_type&CANDIDATE_MB_TYPE_INTER_I){ | |
5118 s->mv_dir = MV_DIR_FORWARD; | |
5119 s->mv_type = MV_TYPE_FIELD; | |
5120 s->mb_intra= 0; | |
5121 for(i=0; i<2; i++){ | |
5122 j= s->field_select[0][i] = s->p_field_select_table[i][xy]; | |
5123 s->mv[0][i][0] = s->p_field_mv_table[i][j][xy][0]; | |
5124 s->mv[0][i][1] = s->p_field_mv_table[i][j][xy][1]; | |
5125 } | |
5126 encode_mb_hq(s, &backup_s, &best_s, CANDIDATE_MB_TYPE_INTER_I, pb, pb2, tex_pb, | |
5127 &dmin, &next_block, 0, 0); | |
5128 } | |
5129 if(mb_type&CANDIDATE_MB_TYPE_SKIPPED){ | |
5130 s->mv_dir = MV_DIR_FORWARD; | |
5131 s->mv_type = MV_TYPE_16X16; | |
5132 s->mb_intra= 0; | |
5133 s->mv[0][0][0] = 0; | |
5134 s->mv[0][0][1] = 0; | |
5135 encode_mb_hq(s, &backup_s, &best_s, CANDIDATE_MB_TYPE_SKIPPED, pb, pb2, tex_pb, | |
5136 &dmin, &next_block, s->mv[0][0][0], s->mv[0][0][1]); | |
5137 } | |
5138 if(mb_type&CANDIDATE_MB_TYPE_INTER4V){ | |
5139 s->mv_dir = MV_DIR_FORWARD; | |
5140 s->mv_type = MV_TYPE_8X8; | |
5141 s->mb_intra= 0; | |
5142 for(i=0; i<4; i++){ | |
5143 s->mv[0][i][0] = s->current_picture.motion_val[0][s->block_index[i]][0]; | |
5144 s->mv[0][i][1] = s->current_picture.motion_val[0][s->block_index[i]][1]; | |
5145 } | |
5146 encode_mb_hq(s, &backup_s, &best_s, CANDIDATE_MB_TYPE_INTER4V, pb, pb2, tex_pb, | |
5147 &dmin, &next_block, 0, 0); | |
5148 } | |
5149 if(mb_type&CANDIDATE_MB_TYPE_FORWARD){ | |
5150 s->mv_dir = MV_DIR_FORWARD; | |
5151 s->mv_type = MV_TYPE_16X16; | |
5152 s->mb_intra= 0; | |
5153 s->mv[0][0][0] = s->b_forw_mv_table[xy][0]; | |
5154 s->mv[0][0][1] = s->b_forw_mv_table[xy][1]; | |
5155 encode_mb_hq(s, &backup_s, &best_s, CANDIDATE_MB_TYPE_FORWARD, pb, pb2, tex_pb, | |
5156 &dmin, &next_block, s->mv[0][0][0], s->mv[0][0][1]); | |
5157 } | |
5158 if(mb_type&CANDIDATE_MB_TYPE_BACKWARD){ | |
5159 s->mv_dir = MV_DIR_BACKWARD; | |
5160 s->mv_type = MV_TYPE_16X16; | |
5161 s->mb_intra= 0; | |
5162 s->mv[1][0][0] = s->b_back_mv_table[xy][0]; | |
5163 s->mv[1][0][1] = s->b_back_mv_table[xy][1]; | |
5164 encode_mb_hq(s, &backup_s, &best_s, CANDIDATE_MB_TYPE_BACKWARD, pb, pb2, tex_pb, | |
5165 &dmin, &next_block, s->mv[1][0][0], s->mv[1][0][1]); | |
5166 } | |
5167 if(mb_type&CANDIDATE_MB_TYPE_BIDIR){ | |
5168 s->mv_dir = MV_DIR_FORWARD | MV_DIR_BACKWARD; | |
5169 s->mv_type = MV_TYPE_16X16; | |
5170 s->mb_intra= 0; | |
5171 s->mv[0][0][0] = s->b_bidir_forw_mv_table[xy][0]; | |
5172 s->mv[0][0][1] = s->b_bidir_forw_mv_table[xy][1]; | |
5173 s->mv[1][0][0] = s->b_bidir_back_mv_table[xy][0]; | |
5174 s->mv[1][0][1] = s->b_bidir_back_mv_table[xy][1]; | |
5175 encode_mb_hq(s, &backup_s, &best_s, CANDIDATE_MB_TYPE_BIDIR, pb, pb2, tex_pb, | |
5176 &dmin, &next_block, 0, 0); | |
5177 } | |
5178 if(mb_type&CANDIDATE_MB_TYPE_FORWARD_I){ | |
5179 s->mv_dir = MV_DIR_FORWARD; | |
5180 s->mv_type = MV_TYPE_FIELD; | |
5181 s->mb_intra= 0; | |
5182 for(i=0; i<2; i++){ | |
5183 j= s->field_select[0][i] = s->b_field_select_table[0][i][xy]; | |
5184 s->mv[0][i][0] = s->b_field_mv_table[0][i][j][xy][0]; | |
5185 s->mv[0][i][1] = s->b_field_mv_table[0][i][j][xy][1]; | |
5186 } | |
5187 encode_mb_hq(s, &backup_s, &best_s, CANDIDATE_MB_TYPE_FORWARD_I, pb, pb2, tex_pb, | |
5188 &dmin, &next_block, 0, 0); | |
5189 } | |
5190 if(mb_type&CANDIDATE_MB_TYPE_BACKWARD_I){ | |
5191 s->mv_dir = MV_DIR_BACKWARD; | |
5192 s->mv_type = MV_TYPE_FIELD; | |
5193 s->mb_intra= 0; | |
5194 for(i=0; i<2; i++){ | |
5195 j= s->field_select[1][i] = s->b_field_select_table[1][i][xy]; | |
5196 s->mv[1][i][0] = s->b_field_mv_table[1][i][j][xy][0]; | |
5197 s->mv[1][i][1] = s->b_field_mv_table[1][i][j][xy][1]; | |
5198 } | |
5199 encode_mb_hq(s, &backup_s, &best_s, CANDIDATE_MB_TYPE_BACKWARD_I, pb, pb2, tex_pb, | |
5200 &dmin, &next_block, 0, 0); | |
5201 } | |
5202 if(mb_type&CANDIDATE_MB_TYPE_BIDIR_I){ | |
5203 s->mv_dir = MV_DIR_FORWARD | MV_DIR_BACKWARD; | |
5204 s->mv_type = MV_TYPE_FIELD; | |
5205 s->mb_intra= 0; | |
5206 for(dir=0; dir<2; dir++){ | |
5207 for(i=0; i<2; i++){ | |
5208 j= s->field_select[dir][i] = s->b_field_select_table[dir][i][xy]; | |
5209 s->mv[dir][i][0] = s->b_field_mv_table[dir][i][j][xy][0]; | |
5210 s->mv[dir][i][1] = s->b_field_mv_table[dir][i][j][xy][1]; | |
5211 } | |
5212 } | |
5213 encode_mb_hq(s, &backup_s, &best_s, CANDIDATE_MB_TYPE_BIDIR_I, pb, pb2, tex_pb, | |
5214 &dmin, &next_block, 0, 0); | |
5215 } | |
5216 if(mb_type&CANDIDATE_MB_TYPE_INTRA){ | |
5217 s->mv_dir = 0; | |
5218 s->mv_type = MV_TYPE_16X16; | |
5219 s->mb_intra= 1; | |
5220 s->mv[0][0][0] = 0; | |
5221 s->mv[0][0][1] = 0; | |
5222 encode_mb_hq(s, &backup_s, &best_s, CANDIDATE_MB_TYPE_INTRA, pb, pb2, tex_pb, | |
5223 &dmin, &next_block, 0, 0); | |
5224 if(s->h263_pred || s->h263_aic){ | |
5225 if(best_s.mb_intra) | |
5226 s->mbintra_table[mb_x + mb_y*s->mb_stride]=1; | |
5227 else | |
5228 ff_clean_intra_table_entries(s); //old mode? | |
5229 } | |
5230 } | |
5231 | |
5232 if((s->flags & CODEC_FLAG_QP_RD) && dmin < INT_MAX){ | |
5233 if(best_s.mv_type==MV_TYPE_16X16){ //FIXME move 4mv after QPRD | |
5234 const int last_qp= backup_s.qscale; | |
5235 int qpi, qp, dc[6]; | |
5236 DCTELEM ac[6][16]; | |
5237 const int mvdir= (best_s.mv_dir&MV_DIR_BACKWARD) ? 1 : 0; | |
5238 static const int dquant_tab[4]={-1,1,-2,2}; | |
5239 | |
5240 assert(backup_s.dquant == 0); | |
5241 | |
5242 //FIXME intra | |
5243 s->mv_dir= best_s.mv_dir; | |
5244 s->mv_type = MV_TYPE_16X16; | |
5245 s->mb_intra= best_s.mb_intra; | |
5246 s->mv[0][0][0] = best_s.mv[0][0][0]; | |
5247 s->mv[0][0][1] = best_s.mv[0][0][1]; | |
5248 s->mv[1][0][0] = best_s.mv[1][0][0]; | |
5249 s->mv[1][0][1] = best_s.mv[1][0][1]; | |
5250 | |
5251 qpi = s->pict_type == B_TYPE ? 2 : 0; | |
5252 for(; qpi<4; qpi++){ | |
5253 int dquant= dquant_tab[qpi]; | |
5254 qp= last_qp + dquant; | |
5255 if(qp < s->avctx->qmin || qp > s->avctx->qmax) | |
5256 continue; | |
5257 backup_s.dquant= dquant; | |
5258 if(s->mb_intra && s->dc_val[0]){ | |
5259 for(i=0; i<6; i++){ | |
5260 dc[i]= s->dc_val[0][ s->block_index[i] ]; | |
5261 memcpy(ac[i], s->ac_val[0][s->block_index[i]], sizeof(DCTELEM)*16); | |
5262 } | |
5263 } | |
5264 | |
5265 encode_mb_hq(s, &backup_s, &best_s, CANDIDATE_MB_TYPE_INTER /* wrong but unused */, pb, pb2, tex_pb, | |
5266 &dmin, &next_block, s->mv[mvdir][0][0], s->mv[mvdir][0][1]); | |
5267 if(best_s.qscale != qp){ | |
5268 if(s->mb_intra && s->dc_val[0]){ | |
5269 for(i=0; i<6; i++){ | |
5270 s->dc_val[0][ s->block_index[i] ]= dc[i]; | |
5271 memcpy(s->ac_val[0][s->block_index[i]], ac[i], sizeof(DCTELEM)*16); | |
5272 } | |
5273 } | |
5274 } | |
5275 } | |
5276 } | |
5277 } | |
5278 if(mb_type&CANDIDATE_MB_TYPE_DIRECT){ | |
5279 int mx= s->b_direct_mv_table[xy][0]; | |
5280 int my= s->b_direct_mv_table[xy][1]; | |
5281 | |
5282 backup_s.dquant = 0; | |
5283 s->mv_dir = MV_DIR_FORWARD | MV_DIR_BACKWARD | MV_DIRECT; | |
5284 s->mb_intra= 0; | |
5285 ff_mpeg4_set_direct_mv(s, mx, my); | |
5286 encode_mb_hq(s, &backup_s, &best_s, CANDIDATE_MB_TYPE_DIRECT, pb, pb2, tex_pb, | |
5287 &dmin, &next_block, mx, my); | |
5288 } | |
5289 if(mb_type&CANDIDATE_MB_TYPE_DIRECT0){ | |
5290 backup_s.dquant = 0; | |
5291 s->mv_dir = MV_DIR_FORWARD | MV_DIR_BACKWARD | MV_DIRECT; | |
5292 s->mb_intra= 0; | |
5293 ff_mpeg4_set_direct_mv(s, 0, 0); | |
5294 encode_mb_hq(s, &backup_s, &best_s, CANDIDATE_MB_TYPE_DIRECT, pb, pb2, tex_pb, | |
5295 &dmin, &next_block, 0, 0); | |
5296 } | |
5297 if(!best_s.mb_intra && s->flags2&CODEC_FLAG2_SKIP_RD){ | |
5298 int coded=0; | |
5299 for(i=0; i<6; i++) | |
5300 coded |= s->block_last_index[i]; | |
5301 if(coded){ | |
5302 int mx,my; | |
5303 memcpy(s->mv, best_s.mv, sizeof(s->mv)); | |
5304 if(best_s.mv_dir & MV_DIRECT){ | |
5305 mx=my=0; //FIXME find the one we actually used | |
5306 ff_mpeg4_set_direct_mv(s, mx, my); | |
5307 }else if(best_s.mv_dir&MV_DIR_BACKWARD){ | |
5308 mx= s->mv[1][0][0]; | |
5309 my= s->mv[1][0][1]; | |
5310 }else{ | |
5311 mx= s->mv[0][0][0]; | |
5312 my= s->mv[0][0][1]; | |
5313 } | |
5314 | |
5315 s->mv_dir= best_s.mv_dir; | |
5316 s->mv_type = best_s.mv_type; | |
5317 s->mb_intra= 0; | |
5318 /* s->mv[0][0][0] = best_s.mv[0][0][0]; | |
5319 s->mv[0][0][1] = best_s.mv[0][0][1]; | |
5320 s->mv[1][0][0] = best_s.mv[1][0][0]; | |
5321 s->mv[1][0][1] = best_s.mv[1][0][1];*/ | |
5322 backup_s.dquant= 0; | |
5323 s->skipdct=1; | |
5324 encode_mb_hq(s, &backup_s, &best_s, CANDIDATE_MB_TYPE_INTER /* wrong but unused */, pb, pb2, tex_pb, | |
5325 &dmin, &next_block, mx, my); | |
5326 s->skipdct=0; | |
5327 } | |
5328 } | |
5329 | |
5330 s->current_picture.qscale_table[xy]= best_s.qscale; | |
5331 | |
5332 copy_context_after_encode(s, &best_s, -1); | |
5333 | |
5334 pb_bits_count= put_bits_count(&s->pb); | |
5335 flush_put_bits(&s->pb); | |
5336 ff_copy_bits(&backup_s.pb, bit_buf[next_block^1], pb_bits_count); | |
5337 s->pb= backup_s.pb; | |
5338 | |
5339 if(s->data_partitioning){ | |
5340 pb2_bits_count= put_bits_count(&s->pb2); | |
5341 flush_put_bits(&s->pb2); | |
5342 ff_copy_bits(&backup_s.pb2, bit_buf2[next_block^1], pb2_bits_count); | |
5343 s->pb2= backup_s.pb2; | |
5344 | |
5345 tex_pb_bits_count= put_bits_count(&s->tex_pb); | |
5346 flush_put_bits(&s->tex_pb); | |
5347 ff_copy_bits(&backup_s.tex_pb, bit_buf_tex[next_block^1], tex_pb_bits_count); | |
5348 s->tex_pb= backup_s.tex_pb; | |
5349 } | |
5350 s->last_bits= put_bits_count(&s->pb); | |
5351 | |
5352 if (s->out_format == FMT_H263 && s->pict_type!=B_TYPE) | |
5353 ff_h263_update_motion_val(s); | |
5354 | |
5355 if(next_block==0){ //FIXME 16 vs linesize16 | |
5356 s->dsp.put_pixels_tab[0][0](s->dest[0], s->rd_scratchpad , s->linesize ,16); | |
5357 s->dsp.put_pixels_tab[1][0](s->dest[1], s->rd_scratchpad + 16*s->linesize , s->uvlinesize, 8); | |
5358 s->dsp.put_pixels_tab[1][0](s->dest[2], s->rd_scratchpad + 16*s->linesize + 8, s->uvlinesize, 8); | |
5359 } | |
5360 | |
5361 if(s->avctx->mb_decision == FF_MB_DECISION_BITS) | |
5362 MPV_decode_mb(s, s->block); | |
5363 } else { | |
5364 int motion_x = 0, motion_y = 0; | |
5365 s->mv_type=MV_TYPE_16X16; | |
5366 // only one MB-Type possible | |
5367 | |
5368 switch(mb_type){ | |
5369 case CANDIDATE_MB_TYPE_INTRA: | |
5370 s->mv_dir = 0; | |
5371 s->mb_intra= 1; | |
5372 motion_x= s->mv[0][0][0] = 0; | |
5373 motion_y= s->mv[0][0][1] = 0; | |
5374 break; | |
5375 case CANDIDATE_MB_TYPE_INTER: | |
5376 s->mv_dir = MV_DIR_FORWARD; | |
5377 s->mb_intra= 0; | |
5378 motion_x= s->mv[0][0][0] = s->p_mv_table[xy][0]; | |
5379 motion_y= s->mv[0][0][1] = s->p_mv_table[xy][1]; | |
5380 break; | |
5381 case CANDIDATE_MB_TYPE_INTER_I: | |
5382 s->mv_dir = MV_DIR_FORWARD; | |
5383 s->mv_type = MV_TYPE_FIELD; | |
5384 s->mb_intra= 0; | |
5385 for(i=0; i<2; i++){ | |
5386 j= s->field_select[0][i] = s->p_field_select_table[i][xy]; | |
5387 s->mv[0][i][0] = s->p_field_mv_table[i][j][xy][0]; | |
5388 s->mv[0][i][1] = s->p_field_mv_table[i][j][xy][1]; | |
5389 } | |
5390 break; | |
5391 case CANDIDATE_MB_TYPE_INTER4V: | |
5392 s->mv_dir = MV_DIR_FORWARD; | |
5393 s->mv_type = MV_TYPE_8X8; | |
5394 s->mb_intra= 0; | |
5395 for(i=0; i<4; i++){ | |
5396 s->mv[0][i][0] = s->current_picture.motion_val[0][s->block_index[i]][0]; | |
5397 s->mv[0][i][1] = s->current_picture.motion_val[0][s->block_index[i]][1]; | |
5398 } | |
5399 break; | |
5400 case CANDIDATE_MB_TYPE_DIRECT: | |
5401 s->mv_dir = MV_DIR_FORWARD | MV_DIR_BACKWARD | MV_DIRECT; | |
5402 s->mb_intra= 0; | |
5403 motion_x=s->b_direct_mv_table[xy][0]; | |
5404 motion_y=s->b_direct_mv_table[xy][1]; | |
5405 ff_mpeg4_set_direct_mv(s, motion_x, motion_y); | |
5406 break; | |
5407 case CANDIDATE_MB_TYPE_DIRECT0: | |
5408 s->mv_dir = MV_DIR_FORWARD | MV_DIR_BACKWARD | MV_DIRECT; | |
5409 s->mb_intra= 0; | |
5410 ff_mpeg4_set_direct_mv(s, 0, 0); | |
5411 break; | |
5412 case CANDIDATE_MB_TYPE_BIDIR: | |
5413 s->mv_dir = MV_DIR_FORWARD | MV_DIR_BACKWARD; | |
5414 s->mb_intra= 0; | |
5415 s->mv[0][0][0] = s->b_bidir_forw_mv_table[xy][0]; | |
5416 s->mv[0][0][1] = s->b_bidir_forw_mv_table[xy][1]; | |
5417 s->mv[1][0][0] = s->b_bidir_back_mv_table[xy][0]; | |
5418 s->mv[1][0][1] = s->b_bidir_back_mv_table[xy][1]; | |
5419 break; | |
5420 case CANDIDATE_MB_TYPE_BACKWARD: | |
5421 s->mv_dir = MV_DIR_BACKWARD; | |
5422 s->mb_intra= 0; | |
5423 motion_x= s->mv[1][0][0] = s->b_back_mv_table[xy][0]; | |
5424 motion_y= s->mv[1][0][1] = s->b_back_mv_table[xy][1]; | |
5425 break; | |
5426 case CANDIDATE_MB_TYPE_FORWARD: | |
5427 s->mv_dir = MV_DIR_FORWARD; | |
5428 s->mb_intra= 0; | |
5429 motion_x= s->mv[0][0][0] = s->b_forw_mv_table[xy][0]; | |
5430 motion_y= s->mv[0][0][1] = s->b_forw_mv_table[xy][1]; | |
5431 // printf(" %d %d ", motion_x, motion_y); | |
5432 break; | |
5433 case CANDIDATE_MB_TYPE_FORWARD_I: | |
5434 s->mv_dir = MV_DIR_FORWARD; | |
5435 s->mv_type = MV_TYPE_FIELD; | |
5436 s->mb_intra= 0; | |
5437 for(i=0; i<2; i++){ | |
5438 j= s->field_select[0][i] = s->b_field_select_table[0][i][xy]; | |
5439 s->mv[0][i][0] = s->b_field_mv_table[0][i][j][xy][0]; | |
5440 s->mv[0][i][1] = s->b_field_mv_table[0][i][j][xy][1]; | |
5441 } | |
5442 break; | |
5443 case CANDIDATE_MB_TYPE_BACKWARD_I: | |
5444 s->mv_dir = MV_DIR_BACKWARD; | |
5445 s->mv_type = MV_TYPE_FIELD; | |
5446 s->mb_intra= 0; | |
5447 for(i=0; i<2; i++){ | |
5448 j= s->field_select[1][i] = s->b_field_select_table[1][i][xy]; | |
5449 s->mv[1][i][0] = s->b_field_mv_table[1][i][j][xy][0]; | |
5450 s->mv[1][i][1] = s->b_field_mv_table[1][i][j][xy][1]; | |
5451 } | |
5452 break; | |
5453 case CANDIDATE_MB_TYPE_BIDIR_I: | |
5454 s->mv_dir = MV_DIR_FORWARD | MV_DIR_BACKWARD; | |
5455 s->mv_type = MV_TYPE_FIELD; | |
5456 s->mb_intra= 0; | |
5457 for(dir=0; dir<2; dir++){ | |
5458 for(i=0; i<2; i++){ | |
5459 j= s->field_select[dir][i] = s->b_field_select_table[dir][i][xy]; | |
5460 s->mv[dir][i][0] = s->b_field_mv_table[dir][i][j][xy][0]; | |
5461 s->mv[dir][i][1] = s->b_field_mv_table[dir][i][j][xy][1]; | |
5462 } | |
5463 } | |
5464 break; | |
5465 default: | |
5466 av_log(s->avctx, AV_LOG_ERROR, "illegal MB type\n"); | |
5467 } | |
5468 | |
5469 encode_mb(s, motion_x, motion_y); | |
5470 | |
5471 // RAL: Update last macroblock type | |
5472 s->last_mv_dir = s->mv_dir; | |
5473 | |
5474 if (s->out_format == FMT_H263 && s->pict_type!=B_TYPE) | |
5475 ff_h263_update_motion_val(s); | |
5476 | |
5477 MPV_decode_mb(s, s->block); | |
5478 } | |
5479 | |
5480 /* clean the MV table in IPS frames for direct mode in B frames */ | |
5481 if(s->mb_intra /* && I,P,S_TYPE */){ | |
5482 s->p_mv_table[xy][0]=0; | |
5483 s->p_mv_table[xy][1]=0; | |
5484 } | |
5485 | |
5486 if(s->flags&CODEC_FLAG_PSNR){ | |
5487 int w= 16; | |
5488 int h= 16; | |
5489 | |
5490 if(s->mb_x*16 + 16 > s->width ) w= s->width - s->mb_x*16; | |
5491 if(s->mb_y*16 + 16 > s->height) h= s->height- s->mb_y*16; | |
5492 | |
5493 s->current_picture.error[0] += sse( | |
5494 s, s->new_picture.data[0] + s->mb_x*16 + s->mb_y*s->linesize*16, | |
5495 s->dest[0], w, h, s->linesize); | |
5496 s->current_picture.error[1] += sse( | |
5497 s, s->new_picture.data[1] + s->mb_x*8 + s->mb_y*s->uvlinesize*8, | |
5498 s->dest[1], w>>1, h>>1, s->uvlinesize); | |
5499 s->current_picture.error[2] += sse( | |
5500 s, s->new_picture .data[2] + s->mb_x*8 + s->mb_y*s->uvlinesize*8, | |
5501 s->dest[2], w>>1, h>>1, s->uvlinesize); | |
5502 } | |
5503 if(s->loop_filter){ | |
5504 if(s->out_format == FMT_H263) | |
5505 ff_h263_loop_filter(s); | |
5506 } | |
5507 //printf("MB %d %d bits\n", s->mb_x+s->mb_y*s->mb_stride, put_bits_count(&s->pb)); | |
5508 } | |
5509 } | |
5510 | |
5511 //not beautiful here but we must write it before flushing so it has to be here | |
5512 if (ENABLE_MSMPEG4_ENCODER && s->msmpeg4_version && s->msmpeg4_version<4 && s->pict_type == I_TYPE) | |
5513 msmpeg4_encode_ext_header(s); | |
5514 | |
5515 write_slice_end(s); | |
5516 | |
5517 /* Send the last GOB if RTP */ | |
5518 if (s->avctx->rtp_callback) { | |
5519 int number_mb = (mb_y - s->resync_mb_y)*s->mb_width - s->resync_mb_x; | |
5520 pdif = pbBufPtr(&s->pb) - s->ptr_lastgob; | |
5521 /* Call the RTP callback to send the last GOB */ | |
5522 emms_c(); | |
5523 s->avctx->rtp_callback(s->avctx, s->ptr_lastgob, pdif, number_mb); | |
5524 } | |
5525 | |
5526 return 0; | |
5527 } | |
5528 | |
5529 #define MERGE(field) dst->field += src->field; src->field=0 | |
5530 static void merge_context_after_me(MpegEncContext *dst, MpegEncContext *src){ | |
5531 MERGE(me.scene_change_score); | |
5532 MERGE(me.mc_mb_var_sum_temp); | |
5533 MERGE(me.mb_var_sum_temp); | |
5534 } | |
5535 | |
5536 static void merge_context_after_encode(MpegEncContext *dst, MpegEncContext *src){ | |
5537 int i; | |
5538 | |
5539 MERGE(dct_count[0]); //note, the other dct vars are not part of the context | |
5540 MERGE(dct_count[1]); | |
5541 MERGE(mv_bits); | |
5542 MERGE(i_tex_bits); | |
5543 MERGE(p_tex_bits); | |
5544 MERGE(i_count); | |
5545 MERGE(f_count); | |
5546 MERGE(b_count); | |
5547 MERGE(skip_count); | |
5548 MERGE(misc_bits); | |
5549 MERGE(error_count); | |
5550 MERGE(padding_bug_score); | |
5551 MERGE(current_picture.error[0]); | |
5552 MERGE(current_picture.error[1]); | |
5553 MERGE(current_picture.error[2]); | |
5554 | |
5555 if(dst->avctx->noise_reduction){ | |
5556 for(i=0; i<64; i++){ | |
5557 MERGE(dct_error_sum[0][i]); | |
5558 MERGE(dct_error_sum[1][i]); | |
5559 } | |
5560 } | |
5561 | |
5562 assert(put_bits_count(&src->pb) % 8 ==0); | |
5563 assert(put_bits_count(&dst->pb) % 8 ==0); | |
5564 ff_copy_bits(&dst->pb, src->pb.buf, put_bits_count(&src->pb)); | |
5565 flush_put_bits(&dst->pb); | |
5566 } | |
5567 | |
5568 static int estimate_qp(MpegEncContext *s, int dry_run){ | |
5569 if (s->next_lambda){ | |
5570 s->current_picture_ptr->quality= | |
5571 s->current_picture.quality = s->next_lambda; | |
5572 if(!dry_run) s->next_lambda= 0; | |
5573 } else if (!s->fixed_qscale) { | |
5574 s->current_picture_ptr->quality= | |
5575 s->current_picture.quality = ff_rate_estimate_qscale(s, dry_run); | |
5576 if (s->current_picture.quality < 0) | |
5577 return -1; | |
5578 } | |
5579 | |
5580 if(s->adaptive_quant){ | |
5581 switch(s->codec_id){ | |
5582 case CODEC_ID_MPEG4: | |
5583 ff_clean_mpeg4_qscales(s); | |
5584 break; | |
5585 case CODEC_ID_H263: | |
5586 case CODEC_ID_H263P: | |
5587 case CODEC_ID_FLV1: | |
5588 ff_clean_h263_qscales(s); | |
5589 break; | |
5590 } | |
5591 | |
5592 s->lambda= s->lambda_table[0]; | |
5593 //FIXME broken | |
5594 }else | |
5595 s->lambda= s->current_picture.quality; | |
5596 //printf("%d %d\n", s->avctx->global_quality, s->current_picture.quality); | |
5597 update_qscale(s); | |
5598 return 0; | |
5599 } | |
5600 | |
5601 static int encode_picture(MpegEncContext *s, int picture_number) | |
5602 { | |
5603 int i; | |
5604 int bits; | |
5605 | |
5606 s->picture_number = picture_number; | |
5607 | |
5608 /* Reset the average MB variance */ | |
5609 s->me.mb_var_sum_temp = | |
5610 s->me.mc_mb_var_sum_temp = 0; | |
5611 | |
5612 /* we need to initialize some time vars before we can encode b-frames */ | |
5613 // RAL: Condition added for MPEG1VIDEO | |
5614 if (s->codec_id == CODEC_ID_MPEG1VIDEO || s->codec_id == CODEC_ID_MPEG2VIDEO || (s->h263_pred && !s->h263_msmpeg4)) | |
5615 ff_set_mpeg4_time(s, s->picture_number); //FIXME rename and use has_b_frames or similar | |
5616 | |
5617 s->me.scene_change_score=0; | |
5618 | |
5619 // s->lambda= s->current_picture_ptr->quality; //FIXME qscale / ... stuff for ME ratedistoration | |
5620 | |
5621 if(s->pict_type==I_TYPE){ | |
5622 if(s->msmpeg4_version >= 3) s->no_rounding=1; | |
5623 else s->no_rounding=0; | |
5624 }else if(s->pict_type!=B_TYPE){ | |
5625 if(s->flipflop_rounding || s->codec_id == CODEC_ID_H263P || s->codec_id == CODEC_ID_MPEG4) | |
5626 s->no_rounding ^= 1; | |
5627 } | |
5628 | |
5629 if(s->flags & CODEC_FLAG_PASS2){ | |
5630 if (estimate_qp(s,1) < 0) | |
5631 return -1; | |
5632 ff_get_2pass_fcode(s); | |
5633 }else if(!(s->flags & CODEC_FLAG_QSCALE)){ | |
5634 if(s->pict_type==B_TYPE) | |
5635 s->lambda= s->last_lambda_for[s->pict_type]; | |
5636 else | |
5637 s->lambda= s->last_lambda_for[s->last_non_b_pict_type]; | |
5638 update_qscale(s); | |
5639 } | |
5640 | |
5641 s->mb_intra=0; //for the rate distortion & bit compare functions | |
5642 for(i=1; i<s->avctx->thread_count; i++){ | |
5643 ff_update_duplicate_context(s->thread_context[i], s); | |
5644 } | |
5645 | |
5646 ff_init_me(s); | |
5647 | |
5648 /* Estimate motion for every MB */ | |
5649 if(s->pict_type != I_TYPE){ | |
5650 s->lambda = (s->lambda * s->avctx->me_penalty_compensation + 128)>>8; | |
5651 s->lambda2= (s->lambda2* (int64_t)s->avctx->me_penalty_compensation + 128)>>8; | |
5652 if(s->pict_type != B_TYPE && s->avctx->me_threshold==0){ | |
5653 if((s->avctx->pre_me && s->last_non_b_pict_type==I_TYPE) || s->avctx->pre_me==2){ | |
5654 s->avctx->execute(s->avctx, pre_estimate_motion_thread, (void**)&(s->thread_context[0]), NULL, s->avctx->thread_count); | |
5655 } | |
5656 } | |
5657 | |
5658 s->avctx->execute(s->avctx, estimate_motion_thread, (void**)&(s->thread_context[0]), NULL, s->avctx->thread_count); | |
5659 }else /* if(s->pict_type == I_TYPE) */{ | |
5660 /* I-Frame */ | |
5661 for(i=0; i<s->mb_stride*s->mb_height; i++) | |
5662 s->mb_type[i]= CANDIDATE_MB_TYPE_INTRA; | |
5663 | |
5664 if(!s->fixed_qscale){ | |
5665 /* finding spatial complexity for I-frame rate control */ | |
5666 s->avctx->execute(s->avctx, mb_var_thread, (void**)&(s->thread_context[0]), NULL, s->avctx->thread_count); | |
5667 } | |
5668 } | |
5669 for(i=1; i<s->avctx->thread_count; i++){ | |
5670 merge_context_after_me(s, s->thread_context[i]); | |
5671 } | |
5672 s->current_picture.mc_mb_var_sum= s->current_picture_ptr->mc_mb_var_sum= s->me.mc_mb_var_sum_temp; | |
5673 s->current_picture. mb_var_sum= s->current_picture_ptr-> mb_var_sum= s->me. mb_var_sum_temp; | |
5674 emms_c(); | |
5675 | |
5676 if(s->me.scene_change_score > s->avctx->scenechange_threshold && s->pict_type == P_TYPE){ | |
5677 s->pict_type= I_TYPE; | |
5678 for(i=0; i<s->mb_stride*s->mb_height; i++) | |
5679 s->mb_type[i]= CANDIDATE_MB_TYPE_INTRA; | |
5680 //printf("Scene change detected, encoding as I Frame %d %d\n", s->current_picture.mb_var_sum, s->current_picture.mc_mb_var_sum); | |
5681 } | |
5682 | |
5683 if(!s->umvplus){ | |
5684 if(s->pict_type==P_TYPE || s->pict_type==S_TYPE) { | |
5685 s->f_code= ff_get_best_fcode(s, s->p_mv_table, CANDIDATE_MB_TYPE_INTER); | |
5686 | |
5687 if(s->flags & CODEC_FLAG_INTERLACED_ME){ | |
5688 int a,b; | |
5689 a= ff_get_best_fcode(s, s->p_field_mv_table[0][0], CANDIDATE_MB_TYPE_INTER_I); //FIXME field_select | |
5690 b= ff_get_best_fcode(s, s->p_field_mv_table[1][1], CANDIDATE_MB_TYPE_INTER_I); | |
5691 s->f_code= FFMAX(s->f_code, FFMAX(a,b)); | |
5692 } | |
5693 | |
5694 ff_fix_long_p_mvs(s); | |
5695 ff_fix_long_mvs(s, NULL, 0, s->p_mv_table, s->f_code, CANDIDATE_MB_TYPE_INTER, 0); | |
5696 if(s->flags & CODEC_FLAG_INTERLACED_ME){ | |
5697 int j; | |
5698 for(i=0; i<2; i++){ | |
5699 for(j=0; j<2; j++) | |
5700 ff_fix_long_mvs(s, s->p_field_select_table[i], j, | |
5701 s->p_field_mv_table[i][j], s->f_code, CANDIDATE_MB_TYPE_INTER_I, 0); | |
5702 } | |
5703 } | |
5704 } | |
5705 | |
5706 if(s->pict_type==B_TYPE){ | |
5707 int a, b; | |
5708 | |
5709 a = ff_get_best_fcode(s, s->b_forw_mv_table, CANDIDATE_MB_TYPE_FORWARD); | |
5710 b = ff_get_best_fcode(s, s->b_bidir_forw_mv_table, CANDIDATE_MB_TYPE_BIDIR); | |
5711 s->f_code = FFMAX(a, b); | |
5712 | |
5713 a = ff_get_best_fcode(s, s->b_back_mv_table, CANDIDATE_MB_TYPE_BACKWARD); | |
5714 b = ff_get_best_fcode(s, s->b_bidir_back_mv_table, CANDIDATE_MB_TYPE_BIDIR); | |
5715 s->b_code = FFMAX(a, b); | |
5716 | |
5717 ff_fix_long_mvs(s, NULL, 0, s->b_forw_mv_table, s->f_code, CANDIDATE_MB_TYPE_FORWARD, 1); | |
5718 ff_fix_long_mvs(s, NULL, 0, s->b_back_mv_table, s->b_code, CANDIDATE_MB_TYPE_BACKWARD, 1); | |
5719 ff_fix_long_mvs(s, NULL, 0, s->b_bidir_forw_mv_table, s->f_code, CANDIDATE_MB_TYPE_BIDIR, 1); | |
5720 ff_fix_long_mvs(s, NULL, 0, s->b_bidir_back_mv_table, s->b_code, CANDIDATE_MB_TYPE_BIDIR, 1); | |
5721 if(s->flags & CODEC_FLAG_INTERLACED_ME){ | |
5722 int dir, j; | |
5723 for(dir=0; dir<2; dir++){ | |
5724 for(i=0; i<2; i++){ | |
5725 for(j=0; j<2; j++){ | |
5726 int type= dir ? (CANDIDATE_MB_TYPE_BACKWARD_I|CANDIDATE_MB_TYPE_BIDIR_I) | |
5727 : (CANDIDATE_MB_TYPE_FORWARD_I |CANDIDATE_MB_TYPE_BIDIR_I); | |
5728 ff_fix_long_mvs(s, s->b_field_select_table[dir][i], j, | |
5729 s->b_field_mv_table[dir][i][j], dir ? s->b_code : s->f_code, type, 1); | |
5730 } | |
5731 } | |
5732 } | |
5733 } | |
5734 } | |
5735 } | |
5736 | |
5737 if (estimate_qp(s, 0) < 0) | |
5738 return -1; | |
5739 | |
5740 if(s->qscale < 3 && s->max_qcoeff<=128 && s->pict_type==I_TYPE && !(s->flags & CODEC_FLAG_QSCALE)) | |
5741 s->qscale= 3; //reduce clipping problems | |
5742 | |
5743 if (s->out_format == FMT_MJPEG) { | |
5744 /* for mjpeg, we do include qscale in the matrix */ | |
5745 s->intra_matrix[0] = ff_mpeg1_default_intra_matrix[0]; | |
5746 for(i=1;i<64;i++){ | |
5747 int j= s->dsp.idct_permutation[i]; | |
5748 | |
5749 s->intra_matrix[j] = av_clip_uint8((ff_mpeg1_default_intra_matrix[i] * s->qscale) >> 3); | |
5750 } | |
5751 convert_matrix(&s->dsp, s->q_intra_matrix, s->q_intra_matrix16, | |
5752 s->intra_matrix, s->intra_quant_bias, 8, 8, 1); | |
5753 s->qscale= 8; | |
5754 } | |
5755 | |
5756 //FIXME var duplication | |
5757 s->current_picture_ptr->key_frame= | |
5758 s->current_picture.key_frame= s->pict_type == I_TYPE; //FIXME pic_ptr | |
5759 s->current_picture_ptr->pict_type= | |
5760 s->current_picture.pict_type= s->pict_type; | |
5761 | |
5762 if(s->current_picture.key_frame) | |
5763 s->picture_in_gop_number=0; | |
5764 | |
5765 s->last_bits= put_bits_count(&s->pb); | |
5766 switch(s->out_format) { | |
5767 case FMT_MJPEG: | |
5768 if (ENABLE_MJPEG_ENCODER) | |
5769 ff_mjpeg_encode_picture_header(s); | |
5770 break; | |
5771 case FMT_H261: | |
5772 if (ENABLE_H261_ENCODER) | |
5773 ff_h261_encode_picture_header(s, picture_number); | |
5774 break; | |
5775 case FMT_H263: | |
5776 if (ENABLE_WMV2_ENCODER && s->codec_id == CODEC_ID_WMV2) | |
5777 ff_wmv2_encode_picture_header(s, picture_number); | |
5778 else if (ENABLE_MSMPEG4_ENCODER && s->h263_msmpeg4) | |
5779 msmpeg4_encode_picture_header(s, picture_number); | |
5780 else if (s->h263_pred) | |
5781 mpeg4_encode_picture_header(s, picture_number); | |
5782 else if (ENABLE_RV10_ENCODER && s->codec_id == CODEC_ID_RV10) | |
5783 rv10_encode_picture_header(s, picture_number); | |
5784 else if (ENABLE_RV20_ENCODER && s->codec_id == CODEC_ID_RV20) | |
5785 rv20_encode_picture_header(s, picture_number); | |
5786 else if (s->codec_id == CODEC_ID_FLV1) | |
5787 ff_flv_encode_picture_header(s, picture_number); | |
5788 else | |
5789 h263_encode_picture_header(s, picture_number); | |
5790 break; | |
5791 case FMT_MPEG1: | |
5792 mpeg1_encode_picture_header(s, picture_number); | |
5793 break; | |
5794 case FMT_H264: | |
5795 break; | |
5796 default: | |
5797 assert(0); | |
5798 } | |
5799 bits= put_bits_count(&s->pb); | |
5800 s->header_bits= bits - s->last_bits; | |
5801 | |
5802 for(i=1; i<s->avctx->thread_count; i++){ | |
5803 update_duplicate_context_after_me(s->thread_context[i], s); | |
5804 } | |
5805 s->avctx->execute(s->avctx, encode_thread, (void**)&(s->thread_context[0]), NULL, s->avctx->thread_count); | |
5806 for(i=1; i<s->avctx->thread_count; i++){ | |
5807 merge_context_after_encode(s, s->thread_context[i]); | |
5808 } | |
5809 emms_c(); | |
5810 return 0; | |
5811 } | |
5812 | |
5813 static void denoise_dct_c(MpegEncContext *s, DCTELEM *block){ | |
5814 const int intra= s->mb_intra; | |
5815 int i; | |
5816 | |
5817 s->dct_count[intra]++; | |
5818 | |
5819 for(i=0; i<64; i++){ | |
5820 int level= block[i]; | |
5821 | |
5822 if(level){ | |
5823 if(level>0){ | |
5824 s->dct_error_sum[intra][i] += level; | |
5825 level -= s->dct_offset[intra][i]; | |
5826 if(level<0) level=0; | |
5827 }else{ | |
5828 s->dct_error_sum[intra][i] -= level; | |
5829 level += s->dct_offset[intra][i]; | |
5830 if(level>0) level=0; | |
5831 } | |
5832 block[i]= level; | |
5833 } | |
5834 } | |
5835 } | |
5836 | |
5837 static int dct_quantize_trellis_c(MpegEncContext *s, | |
5838 DCTELEM *block, int n, | |
5839 int qscale, int *overflow){ | |
5840 const int *qmat; | |
5841 const uint8_t *scantable= s->intra_scantable.scantable; | |
5842 const uint8_t *perm_scantable= s->intra_scantable.permutated; | |
5843 int max=0; | |
5844 unsigned int threshold1, threshold2; | |
5845 int bias=0; | |
5846 int run_tab[65]; | |
5847 int level_tab[65]; | |
5848 int score_tab[65]; | |
5849 int survivor[65]; | |
5850 int survivor_count; | |
5851 int last_run=0; | |
5852 int last_level=0; | |
5853 int last_score= 0; | |
5854 int last_i; | |
5855 int coeff[2][64]; | |
5856 int coeff_count[64]; | |
5857 int qmul, qadd, start_i, last_non_zero, i, dc; | |
5858 const int esc_length= s->ac_esc_length; | |
5859 uint8_t * length; | |
5860 uint8_t * last_length; | |
5861 const int lambda= s->lambda2 >> (FF_LAMBDA_SHIFT - 6); | |
5862 | |
5863 s->dsp.fdct (block); | |
5864 | |
5865 if(s->dct_error_sum) | |
5866 s->denoise_dct(s, block); | |
5867 qmul= qscale*16; | |
5868 qadd= ((qscale-1)|1)*8; | |
5869 | |
5870 if (s->mb_intra) { | |
5871 int q; | |
5872 if (!s->h263_aic) { | |
5873 if (n < 4) | |
5874 q = s->y_dc_scale; | |
5875 else | |
5876 q = s->c_dc_scale; | |
5877 q = q << 3; | |
5878 } else{ | |
5879 /* For AIC we skip quant/dequant of INTRADC */ | |
5880 q = 1 << 3; | |
5881 qadd=0; | |
5882 } | |
5883 | |
5884 /* note: block[0] is assumed to be positive */ | |
5885 block[0] = (block[0] + (q >> 1)) / q; | |
5886 start_i = 1; | |
5887 last_non_zero = 0; | |
5888 qmat = s->q_intra_matrix[qscale]; | |
5889 if(s->mpeg_quant || s->out_format == FMT_MPEG1) | |
5890 bias= 1<<(QMAT_SHIFT-1); | |
5891 length = s->intra_ac_vlc_length; | |
5892 last_length= s->intra_ac_vlc_last_length; | |
5893 } else { | |
5894 start_i = 0; | |
5895 last_non_zero = -1; | |
5896 qmat = s->q_inter_matrix[qscale]; | |
5897 length = s->inter_ac_vlc_length; | |
5898 last_length= s->inter_ac_vlc_last_length; | |
5899 } | |
5900 last_i= start_i; | |
5901 | |
5902 threshold1= (1<<QMAT_SHIFT) - bias - 1; | |
5903 threshold2= (threshold1<<1); | |
5904 | |
5905 for(i=63; i>=start_i; i--) { | |
5906 const int j = scantable[i]; | |
5907 int level = block[j] * qmat[j]; | |
5908 | |
5909 if(((unsigned)(level+threshold1))>threshold2){ | |
5910 last_non_zero = i; | |
5911 break; | |
5912 } | |
5913 } | |
5914 | |
5915 for(i=start_i; i<=last_non_zero; i++) { | |
5916 const int j = scantable[i]; | |
5917 int level = block[j] * qmat[j]; | |
5918 | |
5919 // if( bias+level >= (1<<(QMAT_SHIFT - 3)) | |
5920 // || bias-level >= (1<<(QMAT_SHIFT - 3))){ | |
5921 if(((unsigned)(level+threshold1))>threshold2){ | |
5922 if(level>0){ | |
5923 level= (bias + level)>>QMAT_SHIFT; | |
5924 coeff[0][i]= level; | |
5925 coeff[1][i]= level-1; | |
5926 // coeff[2][k]= level-2; | |
5927 }else{ | |
5928 level= (bias - level)>>QMAT_SHIFT; | |
5929 coeff[0][i]= -level; | |
5930 coeff[1][i]= -level+1; | |
5931 // coeff[2][k]= -level+2; | |
5932 } | |
5933 coeff_count[i]= FFMIN(level, 2); | |
5934 assert(coeff_count[i]); | |
5935 max |=level; | |
5936 }else{ | |
5937 coeff[0][i]= (level>>31)|1; | |
5938 coeff_count[i]= 1; | |
5939 } | |
5940 } | |
5941 | |
5942 *overflow= s->max_qcoeff < max; //overflow might have happened | |
5943 | |
5944 if(last_non_zero < start_i){ | |
5945 memset(block + start_i, 0, (64-start_i)*sizeof(DCTELEM)); | |
5946 return last_non_zero; | |
5947 } | |
5948 | |
5949 score_tab[start_i]= 0; | |
5950 survivor[0]= start_i; | |
5951 survivor_count= 1; | |
5952 | |
5953 for(i=start_i; i<=last_non_zero; i++){ | |
5954 int level_index, j; | |
5955 const int dct_coeff= FFABS(block[ scantable[i] ]); | |
5956 const int zero_distoration= dct_coeff*dct_coeff; | |
5957 int best_score=256*256*256*120; | |
5958 for(level_index=0; level_index < coeff_count[i]; level_index++){ | |
5959 int distoration; | |
5960 int level= coeff[level_index][i]; | |
5961 const int alevel= FFABS(level); | |
5962 int unquant_coeff; | |
5963 | |
5964 assert(level); | |
5965 | |
5966 if(s->out_format == FMT_H263){ | |
5967 unquant_coeff= alevel*qmul + qadd; | |
5968 }else{ //MPEG1 | |
5969 j= s->dsp.idct_permutation[ scantable[i] ]; //FIXME optimize | |
5970 if(s->mb_intra){ | |
5971 unquant_coeff = (int)( alevel * qscale * s->intra_matrix[j]) >> 3; | |
5972 unquant_coeff = (unquant_coeff - 1) | 1; | |
5973 }else{ | |
5974 unquant_coeff = ((( alevel << 1) + 1) * qscale * ((int) s->inter_matrix[j])) >> 4; | |
5975 unquant_coeff = (unquant_coeff - 1) | 1; | |
5976 } | |
5977 unquant_coeff<<= 3; | |
5978 } | |
5979 | |
5980 distoration= (unquant_coeff - dct_coeff) * (unquant_coeff - dct_coeff) - zero_distoration; | |
5981 level+=64; | |
5982 if((level&(~127)) == 0){ | |
5983 for(j=survivor_count-1; j>=0; j--){ | |
5984 int run= i - survivor[j]; | |
5985 int score= distoration + length[UNI_AC_ENC_INDEX(run, level)]*lambda; | |
5986 score += score_tab[i-run]; | |
5987 | |
5988 if(score < best_score){ | |
5989 best_score= score; | |
5990 run_tab[i+1]= run; | |
5991 level_tab[i+1]= level-64; | |
5992 } | |
5993 } | |
5994 | |
5995 if(s->out_format == FMT_H263){ | |
5996 for(j=survivor_count-1; j>=0; j--){ | |
5997 int run= i - survivor[j]; | |
5998 int score= distoration + last_length[UNI_AC_ENC_INDEX(run, level)]*lambda; | |
5999 score += score_tab[i-run]; | |
6000 if(score < last_score){ | |
6001 last_score= score; | |
6002 last_run= run; | |
6003 last_level= level-64; | |
6004 last_i= i+1; | |
6005 } | |
6006 } | |
6007 } | |
6008 }else{ | |
6009 distoration += esc_length*lambda; | |
6010 for(j=survivor_count-1; j>=0; j--){ | |
6011 int run= i - survivor[j]; | |
6012 int score= distoration + score_tab[i-run]; | |
6013 | |
6014 if(score < best_score){ | |
6015 best_score= score; | |
6016 run_tab[i+1]= run; | |
6017 level_tab[i+1]= level-64; | |
6018 } | |
6019 } | |
6020 | |
6021 if(s->out_format == FMT_H263){ | |
6022 for(j=survivor_count-1; j>=0; j--){ | |
6023 int run= i - survivor[j]; | |
6024 int score= distoration + score_tab[i-run]; | |
6025 if(score < last_score){ | |
6026 last_score= score; | |
6027 last_run= run; | |
6028 last_level= level-64; | |
6029 last_i= i+1; | |
6030 } | |
6031 } | |
6032 } | |
6033 } | |
6034 } | |
6035 | |
6036 score_tab[i+1]= best_score; | |
6037 | |
6038 //Note: there is a vlc code in mpeg4 which is 1 bit shorter then another one with a shorter run and the same level | |
6039 if(last_non_zero <= 27){ | |
6040 for(; survivor_count; survivor_count--){ | |
6041 if(score_tab[ survivor[survivor_count-1] ] <= best_score) | |
6042 break; | |
6043 } | |
6044 }else{ | |
6045 for(; survivor_count; survivor_count--){ | |
6046 if(score_tab[ survivor[survivor_count-1] ] <= best_score + lambda) | |
6047 break; | |
6048 } | |
6049 } | |
6050 | |
6051 survivor[ survivor_count++ ]= i+1; | |
6052 } | |
6053 | |
6054 if(s->out_format != FMT_H263){ | |
6055 last_score= 256*256*256*120; | |
6056 for(i= survivor[0]; i<=last_non_zero + 1; i++){ | |
6057 int score= score_tab[i]; | |
6058 if(i) score += lambda*2; //FIXME exacter? | |
6059 | |
6060 if(score < last_score){ | |
6061 last_score= score; | |
6062 last_i= i; | |
6063 last_level= level_tab[i]; | |
6064 last_run= run_tab[i]; | |
6065 } | |
6066 } | |
6067 } | |
6068 | |
6069 s->coded_score[n] = last_score; | |
6070 | |
6071 dc= FFABS(block[0]); | |
6072 last_non_zero= last_i - 1; | |
6073 memset(block + start_i, 0, (64-start_i)*sizeof(DCTELEM)); | |
6074 | |
6075 if(last_non_zero < start_i) | |
6076 return last_non_zero; | |
6077 | |
6078 if(last_non_zero == 0 && start_i == 0){ | |
6079 int best_level= 0; | |
6080 int best_score= dc * dc; | |
6081 | |
6082 for(i=0; i<coeff_count[0]; i++){ | |
6083 int level= coeff[i][0]; | |
6084 int alevel= FFABS(level); | |
6085 int unquant_coeff, score, distortion; | |
6086 | |
6087 if(s->out_format == FMT_H263){ | |
6088 unquant_coeff= (alevel*qmul + qadd)>>3; | |
6089 }else{ //MPEG1 | |
6090 unquant_coeff = ((( alevel << 1) + 1) * qscale * ((int) s->inter_matrix[0])) >> 4; | |
6091 unquant_coeff = (unquant_coeff - 1) | 1; | |
6092 } | |
6093 unquant_coeff = (unquant_coeff + 4) >> 3; | |
6094 unquant_coeff<<= 3 + 3; | |
6095 | |
6096 distortion= (unquant_coeff - dc) * (unquant_coeff - dc); | |
6097 level+=64; | |
6098 if((level&(~127)) == 0) score= distortion + last_length[UNI_AC_ENC_INDEX(0, level)]*lambda; | |
6099 else score= distortion + esc_length*lambda; | |
6100 | |
6101 if(score < best_score){ | |
6102 best_score= score; | |
6103 best_level= level - 64; | |
6104 } | |
6105 } | |
6106 block[0]= best_level; | |
6107 s->coded_score[n] = best_score - dc*dc; | |
6108 if(best_level == 0) return -1; | |
6109 else return last_non_zero; | |
6110 } | |
6111 | |
6112 i= last_i; | |
6113 assert(last_level); | |
6114 | |
6115 block[ perm_scantable[last_non_zero] ]= last_level; | |
6116 i -= last_run + 1; | |
6117 | |
6118 for(; i>start_i; i -= run_tab[i] + 1){ | |
6119 block[ perm_scantable[i-1] ]= level_tab[i]; | |
6120 } | |
6121 | |
6122 return last_non_zero; | |
6123 } | |
6124 | |
6125 //#define REFINE_STATS 1 | |
6126 static int16_t basis[64][64]; | |
6127 | |
6128 static void build_basis(uint8_t *perm){ | |
6129 int i, j, x, y; | |
6130 emms_c(); | |
6131 for(i=0; i<8; i++){ | |
6132 for(j=0; j<8; j++){ | |
6133 for(y=0; y<8; y++){ | |
6134 for(x=0; x<8; x++){ | |
6135 double s= 0.25*(1<<BASIS_SHIFT); | |
6136 int index= 8*i + j; | |
6137 int perm_index= perm[index]; | |
6138 if(i==0) s*= sqrt(0.5); | |
6139 if(j==0) s*= sqrt(0.5); | |
6140 basis[perm_index][8*x + y]= lrintf(s * cos((M_PI/8.0)*i*(x+0.5)) * cos((M_PI/8.0)*j*(y+0.5))); | |
6141 } | |
6142 } | |
6143 } | |
6144 } | |
6145 } | |
6146 | |
6147 static int dct_quantize_refine(MpegEncContext *s, //FIXME breaks denoise? | |
6148 DCTELEM *block, int16_t *weight, DCTELEM *orig, | |
6149 int n, int qscale){ | |
6150 int16_t rem[64]; | |
6151 DECLARE_ALIGNED_16(DCTELEM, d1[64]); | |
6152 const int *qmat; | |
6153 const uint8_t *scantable= s->intra_scantable.scantable; | |
6154 const uint8_t *perm_scantable= s->intra_scantable.permutated; | |
6155 // unsigned int threshold1, threshold2; | |
6156 // int bias=0; | |
6157 int run_tab[65]; | |
6158 int prev_run=0; | |
6159 int prev_level=0; | |
6160 int qmul, qadd, start_i, last_non_zero, i, dc; | |
6161 uint8_t * length; | |
6162 uint8_t * last_length; | |
6163 int lambda; | |
6164 int rle_index, run, q = 1, sum; //q is only used when s->mb_intra is true | |
6165 #ifdef REFINE_STATS | |
6166 static int count=0; | |
6167 static int after_last=0; | |
6168 static int to_zero=0; | |
6169 static int from_zero=0; | |
6170 static int raise=0; | |
6171 static int lower=0; | |
6172 static int messed_sign=0; | |
6173 #endif | |
6174 | |
6175 if(basis[0][0] == 0) | |
6176 build_basis(s->dsp.idct_permutation); | |
6177 | |
6178 qmul= qscale*2; | |
6179 qadd= (qscale-1)|1; | |
6180 if (s->mb_intra) { | |
6181 if (!s->h263_aic) { | |
6182 if (n < 4) | |
6183 q = s->y_dc_scale; | |
6184 else | |
6185 q = s->c_dc_scale; | |
6186 } else{ | |
6187 /* For AIC we skip quant/dequant of INTRADC */ | |
6188 q = 1; | |
6189 qadd=0; | |
6190 } | |
6191 q <<= RECON_SHIFT-3; | |
6192 /* note: block[0] is assumed to be positive */ | |
6193 dc= block[0]*q; | |
6194 // block[0] = (block[0] + (q >> 1)) / q; | |
6195 start_i = 1; | |
6196 qmat = s->q_intra_matrix[qscale]; | |
6197 // if(s->mpeg_quant || s->out_format == FMT_MPEG1) | |
6198 // bias= 1<<(QMAT_SHIFT-1); | |
6199 length = s->intra_ac_vlc_length; | |
6200 last_length= s->intra_ac_vlc_last_length; | |
6201 } else { | |
6202 dc= 0; | |
6203 start_i = 0; | |
6204 qmat = s->q_inter_matrix[qscale]; | |
6205 length = s->inter_ac_vlc_length; | |
6206 last_length= s->inter_ac_vlc_last_length; | |
6207 } | |
6208 last_non_zero = s->block_last_index[n]; | |
6209 | |
6210 #ifdef REFINE_STATS | |
6211 {START_TIMER | |
6212 #endif | |
6213 dc += (1<<(RECON_SHIFT-1)); | |
6214 for(i=0; i<64; i++){ | |
6215 rem[i]= dc - (orig[i]<<RECON_SHIFT); //FIXME use orig dirrectly instead of copying to rem[] | |
6216 } | |
6217 #ifdef REFINE_STATS | |
6218 STOP_TIMER("memset rem[]")} | |
6219 #endif | |
6220 sum=0; | |
6221 for(i=0; i<64; i++){ | |
6222 int one= 36; | |
6223 int qns=4; | |
6224 int w; | |
6225 | |
6226 w= FFABS(weight[i]) + qns*one; | |
6227 w= 15 + (48*qns*one + w/2)/w; // 16 .. 63 | |
6228 | |
6229 weight[i] = w; | |
6230 // w=weight[i] = (63*qns + (w/2)) / w; | |
6231 | |
6232 assert(w>0); | |
6233 assert(w<(1<<6)); | |
6234 sum += w*w; | |
6235 } | |
6236 lambda= sum*(uint64_t)s->lambda2 >> (FF_LAMBDA_SHIFT - 6 + 6 + 6 + 6); | |
6237 #ifdef REFINE_STATS | |
6238 {START_TIMER | |
6239 #endif | |
6240 run=0; | |
6241 rle_index=0; | |
6242 for(i=start_i; i<=last_non_zero; i++){ | |
6243 int j= perm_scantable[i]; | |
6244 const int level= block[j]; | |
6245 int coeff; | |
6246 | |
6247 if(level){ | |
6248 if(level<0) coeff= qmul*level - qadd; | |
6249 else coeff= qmul*level + qadd; | |
6250 run_tab[rle_index++]=run; | |
6251 run=0; | |
6252 | |
6253 s->dsp.add_8x8basis(rem, basis[j], coeff); | |
6254 }else{ | |
6255 run++; | |
6256 } | |
6257 } | |
6258 #ifdef REFINE_STATS | |
6259 if(last_non_zero>0){ | |
6260 STOP_TIMER("init rem[]") | |
6261 } | |
6262 } | |
6263 | |
6264 {START_TIMER | |
6265 #endif | |
6266 for(;;){ | |
6267 int best_score=s->dsp.try_8x8basis(rem, weight, basis[0], 0); | |
6268 int best_coeff=0; | |
6269 int best_change=0; | |
6270 int run2, best_unquant_change=0, analyze_gradient; | |
6271 #ifdef REFINE_STATS | |
6272 {START_TIMER | |
6273 #endif | |
6274 analyze_gradient = last_non_zero > 2 || s->avctx->quantizer_noise_shaping >= 3; | |
6275 | |
6276 if(analyze_gradient){ | |
6277 #ifdef REFINE_STATS | |
6278 {START_TIMER | |
6279 #endif | |
6280 for(i=0; i<64; i++){ | |
6281 int w= weight[i]; | |
6282 | |
6283 d1[i] = (rem[i]*w*w + (1<<(RECON_SHIFT+12-1)))>>(RECON_SHIFT+12); | |
6284 } | |
6285 #ifdef REFINE_STATS | |
6286 STOP_TIMER("rem*w*w")} | |
6287 {START_TIMER | |
6288 #endif | |
6289 s->dsp.fdct(d1); | |
6290 #ifdef REFINE_STATS | |
6291 STOP_TIMER("dct")} | |
6292 #endif | |
6293 } | |
6294 | |
6295 if(start_i){ | |
6296 const int level= block[0]; | |
6297 int change, old_coeff; | |
6298 | |
6299 assert(s->mb_intra); | |
6300 | |
6301 old_coeff= q*level; | |
6302 | |
6303 for(change=-1; change<=1; change+=2){ | |
6304 int new_level= level + change; | |
6305 int score, new_coeff; | |
6306 | |
6307 new_coeff= q*new_level; | |
6308 if(new_coeff >= 2048 || new_coeff < 0) | |
6309 continue; | |
6310 | |
6311 score= s->dsp.try_8x8basis(rem, weight, basis[0], new_coeff - old_coeff); | |
6312 if(score<best_score){ | |
6313 best_score= score; | |
6314 best_coeff= 0; | |
6315 best_change= change; | |
6316 best_unquant_change= new_coeff - old_coeff; | |
6317 } | |
6318 } | |
6319 } | |
6320 | |
6321 run=0; | |
6322 rle_index=0; | |
6323 run2= run_tab[rle_index++]; | |
6324 prev_level=0; | |
6325 prev_run=0; | |
6326 | |
6327 for(i=start_i; i<64; i++){ | |
6328 int j= perm_scantable[i]; | |
6329 const int level= block[j]; | |
6330 int change, old_coeff; | |
6331 | |
6332 if(s->avctx->quantizer_noise_shaping < 3 && i > last_non_zero + 1) | |
6333 break; | |
6334 | |
6335 if(level){ | |
6336 if(level<0) old_coeff= qmul*level - qadd; | |
6337 else old_coeff= qmul*level + qadd; | |
6338 run2= run_tab[rle_index++]; //FIXME ! maybe after last | |
6339 }else{ | |
6340 old_coeff=0; | |
6341 run2--; | |
6342 assert(run2>=0 || i >= last_non_zero ); | |
6343 } | |
6344 | |
6345 for(change=-1; change<=1; change+=2){ | |
6346 int new_level= level + change; | |
6347 int score, new_coeff, unquant_change; | |
6348 | |
6349 score=0; | |
6350 if(s->avctx->quantizer_noise_shaping < 2 && FFABS(new_level) > FFABS(level)) | |
6351 continue; | |
6352 | |
6353 if(new_level){ | |
6354 if(new_level<0) new_coeff= qmul*new_level - qadd; | |
6355 else new_coeff= qmul*new_level + qadd; | |
6356 if(new_coeff >= 2048 || new_coeff <= -2048) | |
6357 continue; | |
6358 //FIXME check for overflow | |
6359 | |
6360 if(level){ | |
6361 if(level < 63 && level > -63){ | |
6362 if(i < last_non_zero) | |
6363 score += length[UNI_AC_ENC_INDEX(run, new_level+64)] | |
6364 - length[UNI_AC_ENC_INDEX(run, level+64)]; | |
6365 else | |
6366 score += last_length[UNI_AC_ENC_INDEX(run, new_level+64)] | |
6367 - last_length[UNI_AC_ENC_INDEX(run, level+64)]; | |
6368 } | |
6369 }else{ | |
6370 assert(FFABS(new_level)==1); | |
6371 | |
6372 if(analyze_gradient){ | |
6373 int g= d1[ scantable[i] ]; | |
6374 if(g && (g^new_level) >= 0) | |
6375 continue; | |
6376 } | |
6377 | |
6378 if(i < last_non_zero){ | |
6379 int next_i= i + run2 + 1; | |
6380 int next_level= block[ perm_scantable[next_i] ] + 64; | |
6381 | |
6382 if(next_level&(~127)) | |
6383 next_level= 0; | |
6384 | |
6385 if(next_i < last_non_zero) | |
6386 score += length[UNI_AC_ENC_INDEX(run, 65)] | |
6387 + length[UNI_AC_ENC_INDEX(run2, next_level)] | |
6388 - length[UNI_AC_ENC_INDEX(run + run2 + 1, next_level)]; | |
6389 else | |
6390 score += length[UNI_AC_ENC_INDEX(run, 65)] | |
6391 + last_length[UNI_AC_ENC_INDEX(run2, next_level)] | |
6392 - last_length[UNI_AC_ENC_INDEX(run + run2 + 1, next_level)]; | |
6393 }else{ | |
6394 score += last_length[UNI_AC_ENC_INDEX(run, 65)]; | |
6395 if(prev_level){ | |
6396 score += length[UNI_AC_ENC_INDEX(prev_run, prev_level)] | |
6397 - last_length[UNI_AC_ENC_INDEX(prev_run, prev_level)]; | |
6398 } | |
6399 } | |
6400 } | |
6401 }else{ | |
6402 new_coeff=0; | |
6403 assert(FFABS(level)==1); | |
6404 | |
6405 if(i < last_non_zero){ | |
6406 int next_i= i + run2 + 1; | |
6407 int next_level= block[ perm_scantable[next_i] ] + 64; | |
6408 | |
6409 if(next_level&(~127)) | |
6410 next_level= 0; | |
6411 | |
6412 if(next_i < last_non_zero) | |
6413 score += length[UNI_AC_ENC_INDEX(run + run2 + 1, next_level)] | |
6414 - length[UNI_AC_ENC_INDEX(run2, next_level)] | |
6415 - length[UNI_AC_ENC_INDEX(run, 65)]; | |
6416 else | |
6417 score += last_length[UNI_AC_ENC_INDEX(run + run2 + 1, next_level)] | |
6418 - last_length[UNI_AC_ENC_INDEX(run2, next_level)] | |
6419 - length[UNI_AC_ENC_INDEX(run, 65)]; | |
6420 }else{ | |
6421 score += -last_length[UNI_AC_ENC_INDEX(run, 65)]; | |
6422 if(prev_level){ | |
6423 score += last_length[UNI_AC_ENC_INDEX(prev_run, prev_level)] | |
6424 - length[UNI_AC_ENC_INDEX(prev_run, prev_level)]; | |
6425 } | |
6426 } | |
6427 } | |
6428 | |
6429 score *= lambda; | |
6430 | |
6431 unquant_change= new_coeff - old_coeff; | |
6432 assert((score < 100*lambda && score > -100*lambda) || lambda==0); | |
6433 | |
6434 score+= s->dsp.try_8x8basis(rem, weight, basis[j], unquant_change); | |
6435 if(score<best_score){ | |
6436 best_score= score; | |
6437 best_coeff= i; | |
6438 best_change= change; | |
6439 best_unquant_change= unquant_change; | |
6440 } | |
6441 } | |
6442 if(level){ | |
6443 prev_level= level + 64; | |
6444 if(prev_level&(~127)) | |
6445 prev_level= 0; | |
6446 prev_run= run; | |
6447 run=0; | |
6448 }else{ | |
6449 run++; | |
6450 } | |
6451 } | |
6452 #ifdef REFINE_STATS | |
6453 STOP_TIMER("iterative step")} | |
6454 #endif | |
6455 | |
6456 if(best_change){ | |
6457 int j= perm_scantable[ best_coeff ]; | |
6458 | |
6459 block[j] += best_change; | |
6460 | |
6461 if(best_coeff > last_non_zero){ | |
6462 last_non_zero= best_coeff; | |
6463 assert(block[j]); | |
6464 #ifdef REFINE_STATS | |
6465 after_last++; | |
6466 #endif | |
6467 }else{ | |
6468 #ifdef REFINE_STATS | |
6469 if(block[j]){ | |
6470 if(block[j] - best_change){ | |
6471 if(FFABS(block[j]) > FFABS(block[j] - best_change)){ | |
6472 raise++; | |
6473 }else{ | |
6474 lower++; | |
6475 } | |
6476 }else{ | |
6477 from_zero++; | |
6478 } | |
6479 }else{ | |
6480 to_zero++; | |
6481 } | |
6482 #endif | |
6483 for(; last_non_zero>=start_i; last_non_zero--){ | |
6484 if(block[perm_scantable[last_non_zero]]) | |
6485 break; | |
6486 } | |
6487 } | |
6488 #ifdef REFINE_STATS | |
6489 count++; | |
6490 if(256*256*256*64 % count == 0){ | |
6491 printf("after_last:%d to_zero:%d from_zero:%d raise:%d lower:%d sign:%d xyp:%d/%d/%d\n", after_last, to_zero, from_zero, raise, lower, messed_sign, s->mb_x, s->mb_y, s->picture_number); | |
6492 } | |
6493 #endif | |
6494 run=0; | |
6495 rle_index=0; | |
6496 for(i=start_i; i<=last_non_zero; i++){ | |
6497 int j= perm_scantable[i]; | |
6498 const int level= block[j]; | |
6499 | |
6500 if(level){ | |
6501 run_tab[rle_index++]=run; | |
6502 run=0; | |
6503 }else{ | |
6504 run++; | |
6505 } | |
6506 } | |
6507 | |
6508 s->dsp.add_8x8basis(rem, basis[j], best_unquant_change); | |
6509 }else{ | |
6510 break; | |
6511 } | |
6512 } | |
6513 #ifdef REFINE_STATS | |
6514 if(last_non_zero>0){ | |
6515 STOP_TIMER("iterative search") | |
6516 } | |
6517 } | |
6518 #endif | |
6519 | |
6520 return last_non_zero; | |
6521 } | |
6522 | |
6523 static int dct_quantize_c(MpegEncContext *s, | |
6524 DCTELEM *block, int n, | |
6525 int qscale, int *overflow) | |
6526 { | |
6527 int i, j, level, last_non_zero, q, start_i; | |
6528 const int *qmat; | |
6529 const uint8_t *scantable= s->intra_scantable.scantable; | |
6530 int bias; | |
6531 int max=0; | |
6532 unsigned int threshold1, threshold2; | |
6533 | |
6534 s->dsp.fdct (block); | |
6535 | |
6536 if(s->dct_error_sum) | |
6537 s->denoise_dct(s, block); | |
6538 | |
6539 if (s->mb_intra) { | |
6540 if (!s->h263_aic) { | |
6541 if (n < 4) | |
6542 q = s->y_dc_scale; | |
6543 else | |
6544 q = s->c_dc_scale; | |
6545 q = q << 3; | |
6546 } else | |
6547 /* For AIC we skip quant/dequant of INTRADC */ | |
6548 q = 1 << 3; | |
6549 | |
6550 /* note: block[0] is assumed to be positive */ | |
6551 block[0] = (block[0] + (q >> 1)) / q; | |
6552 start_i = 1; | |
6553 last_non_zero = 0; | |
6554 qmat = s->q_intra_matrix[qscale]; | |
6555 bias= s->intra_quant_bias<<(QMAT_SHIFT - QUANT_BIAS_SHIFT); | |
6556 } else { | |
6557 start_i = 0; | |
6558 last_non_zero = -1; | |
6559 qmat = s->q_inter_matrix[qscale]; | |
6560 bias= s->inter_quant_bias<<(QMAT_SHIFT - QUANT_BIAS_SHIFT); | |
6561 } | |
6562 threshold1= (1<<QMAT_SHIFT) - bias - 1; | |
6563 threshold2= (threshold1<<1); | |
6564 for(i=63;i>=start_i;i--) { | |
6565 j = scantable[i]; | |
6566 level = block[j] * qmat[j]; | |
6567 | |
6568 if(((unsigned)(level+threshold1))>threshold2){ | |
6569 last_non_zero = i; | |
6570 break; | |
6571 }else{ | |
6572 block[j]=0; | |
6573 } | |
6574 } | |
6575 for(i=start_i; i<=last_non_zero; i++) { | |
6576 j = scantable[i]; | |
6577 level = block[j] * qmat[j]; | |
6578 | |
6579 // if( bias+level >= (1<<QMAT_SHIFT) | |
6580 // || bias-level >= (1<<QMAT_SHIFT)){ | |
6581 if(((unsigned)(level+threshold1))>threshold2){ | |
6582 if(level>0){ | |
6583 level= (bias + level)>>QMAT_SHIFT; | |
6584 block[j]= level; | |
6585 }else{ | |
6586 level= (bias - level)>>QMAT_SHIFT; | |
6587 block[j]= -level; | |
6588 } | |
6589 max |=level; | |
6590 }else{ | |
6591 block[j]=0; | |
6592 } | |
6593 } | |
6594 *overflow= s->max_qcoeff < max; //overflow might have happened | |
6595 | |
6596 /* we need this permutation so that we correct the IDCT, we only permute the !=0 elements */ | |
6597 if (s->dsp.idct_permutation_type != FF_NO_IDCT_PERM) | |
6598 ff_block_permute(block, s->dsp.idct_permutation, scantable, last_non_zero); | |
6599 | |
6600 return last_non_zero; | |
6601 } | |
6602 | |
6603 #endif //CONFIG_ENCODERS | |
6604 | 2149 |
6605 static void dct_unquantize_mpeg1_intra_c(MpegEncContext *s, | 2150 static void dct_unquantize_mpeg1_intra_c(MpegEncContext *s, |
6606 DCTELEM *block, int n, int qscale) | 2151 DCTELEM *block, int n, int qscale) |
6607 { | 2152 { |
6608 int i, level, nCoeffs; | 2153 int i, level, nCoeffs; |
6818 block[i] = level; | 2363 block[i] = level; |
6819 } | 2364 } |
6820 } | 2365 } |
6821 } | 2366 } |
6822 | 2367 |
6823 #ifdef CONFIG_ENCODERS | |
6824 AVCodec h263_encoder = { | |
6825 "h263", | |
6826 CODEC_TYPE_VIDEO, | |
6827 CODEC_ID_H263, | |
6828 sizeof(MpegEncContext), | |
6829 MPV_encode_init, | |
6830 MPV_encode_picture, | |
6831 MPV_encode_end, | |
6832 .pix_fmts= (enum PixelFormat[]){PIX_FMT_YUV420P, -1}, | |
6833 }; | |
6834 | |
6835 AVCodec h263p_encoder = { | |
6836 "h263p", | |
6837 CODEC_TYPE_VIDEO, | |
6838 CODEC_ID_H263P, | |
6839 sizeof(MpegEncContext), | |
6840 MPV_encode_init, | |
6841 MPV_encode_picture, | |
6842 MPV_encode_end, | |
6843 .pix_fmts= (enum PixelFormat[]){PIX_FMT_YUV420P, -1}, | |
6844 }; | |
6845 | |
6846 AVCodec flv_encoder = { | |
6847 "flv", | |
6848 CODEC_TYPE_VIDEO, | |
6849 CODEC_ID_FLV1, | |
6850 sizeof(MpegEncContext), | |
6851 MPV_encode_init, | |
6852 MPV_encode_picture, | |
6853 MPV_encode_end, | |
6854 .pix_fmts= (enum PixelFormat[]){PIX_FMT_YUV420P, -1}, | |
6855 }; | |
6856 | |
6857 AVCodec rv10_encoder = { | |
6858 "rv10", | |
6859 CODEC_TYPE_VIDEO, | |
6860 CODEC_ID_RV10, | |
6861 sizeof(MpegEncContext), | |
6862 MPV_encode_init, | |
6863 MPV_encode_picture, | |
6864 MPV_encode_end, | |
6865 .pix_fmts= (enum PixelFormat[]){PIX_FMT_YUV420P, -1}, | |
6866 }; | |
6867 | |
6868 AVCodec rv20_encoder = { | |
6869 "rv20", | |
6870 CODEC_TYPE_VIDEO, | |
6871 CODEC_ID_RV20, | |
6872 sizeof(MpegEncContext), | |
6873 MPV_encode_init, | |
6874 MPV_encode_picture, | |
6875 MPV_encode_end, | |
6876 .pix_fmts= (enum PixelFormat[]){PIX_FMT_YUV420P, -1}, | |
6877 }; | |
6878 | |
6879 AVCodec mpeg4_encoder = { | |
6880 "mpeg4", | |
6881 CODEC_TYPE_VIDEO, | |
6882 CODEC_ID_MPEG4, | |
6883 sizeof(MpegEncContext), | |
6884 MPV_encode_init, | |
6885 MPV_encode_picture, | |
6886 MPV_encode_end, | |
6887 .pix_fmts= (enum PixelFormat[]){PIX_FMT_YUV420P, -1}, | |
6888 .capabilities= CODEC_CAP_DELAY, | |
6889 }; | |
6890 | |
6891 AVCodec msmpeg4v1_encoder = { | |
6892 "msmpeg4v1", | |
6893 CODEC_TYPE_VIDEO, | |
6894 CODEC_ID_MSMPEG4V1, | |
6895 sizeof(MpegEncContext), | |
6896 MPV_encode_init, | |
6897 MPV_encode_picture, | |
6898 MPV_encode_end, | |
6899 .pix_fmts= (enum PixelFormat[]){PIX_FMT_YUV420P, -1}, | |
6900 }; | |
6901 | |
6902 AVCodec msmpeg4v2_encoder = { | |
6903 "msmpeg4v2", | |
6904 CODEC_TYPE_VIDEO, | |
6905 CODEC_ID_MSMPEG4V2, | |
6906 sizeof(MpegEncContext), | |
6907 MPV_encode_init, | |
6908 MPV_encode_picture, | |
6909 MPV_encode_end, | |
6910 .pix_fmts= (enum PixelFormat[]){PIX_FMT_YUV420P, -1}, | |
6911 }; | |
6912 | |
6913 AVCodec msmpeg4v3_encoder = { | |
6914 "msmpeg4", | |
6915 CODEC_TYPE_VIDEO, | |
6916 CODEC_ID_MSMPEG4V3, | |
6917 sizeof(MpegEncContext), | |
6918 MPV_encode_init, | |
6919 MPV_encode_picture, | |
6920 MPV_encode_end, | |
6921 .pix_fmts= (enum PixelFormat[]){PIX_FMT_YUV420P, -1}, | |
6922 }; | |
6923 | |
6924 AVCodec wmv1_encoder = { | |
6925 "wmv1", | |
6926 CODEC_TYPE_VIDEO, | |
6927 CODEC_ID_WMV1, | |
6928 sizeof(MpegEncContext), | |
6929 MPV_encode_init, | |
6930 MPV_encode_picture, | |
6931 MPV_encode_end, | |
6932 .pix_fmts= (enum PixelFormat[]){PIX_FMT_YUV420P, -1}, | |
6933 }; | |
6934 | |
6935 #endif //CONFIG_ENCODERS |