interlaced motion estimation
[libav.git] / libavcodec / mpegvideo.c
1 /*
2 * The simplest mpeg encoder (well, it was the simplest!)
3 * Copyright (c) 2000,2001 Fabrice Bellard.
4 *
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2 of the License, or (at your option) any later version.
9 *
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
14 *
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this library; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 *
19 * 4MV & hq & b-frame encoding stuff by Michael Niedermayer <michaelni@gmx.at>
20 */
21
22 /**
23 * @file mpegvideo.c
24 * The simplest mpeg encoder (well, it was the simplest!).
25 */
26
27 #include <limits.h>
28 #include "avcodec.h"
29 #include "dsputil.h"
30 #include "mpegvideo.h"
31 #include "faandct.h"
32
33 #ifdef USE_FASTMEMCPY
34 #include "fastmemcpy.h"
35 #endif
36
37 //#undef NDEBUG
38 //#include <assert.h>
39
40 #ifdef CONFIG_ENCODERS
41 static void encode_picture(MpegEncContext *s, int picture_number);
42 #endif //CONFIG_ENCODERS
43 static void dct_unquantize_mpeg1_intra_c(MpegEncContext *s,
44 DCTELEM *block, int n, int qscale);
45 static void dct_unquantize_mpeg1_inter_c(MpegEncContext *s,
46 DCTELEM *block, int n, int qscale);
47 static void dct_unquantize_mpeg2_intra_c(MpegEncContext *s,
48 DCTELEM *block, int n, int qscale);
49 static void dct_unquantize_mpeg2_inter_c(MpegEncContext *s,
50 DCTELEM *block, int n, int qscale);
51 static void dct_unquantize_h263_intra_c(MpegEncContext *s,
52 DCTELEM *block, int n, int qscale);
53 static void dct_unquantize_h263_inter_c(MpegEncContext *s,
54 DCTELEM *block, int n, int qscale);
55 static void draw_edges_c(uint8_t *buf, int wrap, int width, int height, int w);
56 #ifdef CONFIG_ENCODERS
57 static int dct_quantize_c(MpegEncContext *s, DCTELEM *block, int n, int qscale, int *overflow);
58 static int dct_quantize_trellis_c(MpegEncContext *s, DCTELEM *block, int n, int qscale, int *overflow);
59 static int sse_mb(MpegEncContext *s);
60 #endif //CONFIG_ENCODERS
61
62 #ifdef HAVE_XVMC
63 extern int XVMC_field_start(MpegEncContext*s, AVCodecContext *avctx);
64 extern void XVMC_field_end(MpegEncContext *s);
65 extern void XVMC_decode_mb(MpegEncContext *s);
66 #endif
67
68 void (*draw_edges)(uint8_t *buf, int wrap, int width, int height, int w)= draw_edges_c;
69
70
71 /* enable all paranoid tests for rounding, overflows, etc... */
72 //#define PARANOID
73
74 //#define DEBUG
75
76
77 /* for jpeg fast DCT */
78 #define CONST_BITS 14
79
80 static const uint16_t aanscales[64] = {
81 /* precomputed values scaled up by 14 bits */
82 16384, 22725, 21407, 19266, 16384, 12873, 8867, 4520,
83 22725, 31521, 29692, 26722, 22725, 17855, 12299, 6270,
84 21407, 29692, 27969, 25172, 21407, 16819, 11585, 5906,
85 19266, 26722, 25172, 22654, 19266, 15137, 10426, 5315,
86 16384, 22725, 21407, 19266, 16384, 12873, 8867, 4520,
87 12873, 17855, 16819, 15137, 12873, 10114, 6967, 3552,
88 8867 , 12299, 11585, 10426, 8867, 6967, 4799, 2446,
89 4520 , 6270, 5906, 5315, 4520, 3552, 2446, 1247
90 };
91
92 static const uint8_t h263_chroma_roundtab[16] = {
93 // 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
94 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2,
95 };
96
97 static const uint8_t ff_default_chroma_qscale_table[32]={
98 // 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
99 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
100 };
101
102 #ifdef CONFIG_ENCODERS
103 static uint8_t (*default_mv_penalty)[MAX_MV*2+1]=NULL;
104 static uint8_t default_fcode_tab[MAX_MV*2+1];
105
106 enum PixelFormat ff_yuv420p_list[2]= {PIX_FMT_YUV420P, -1};
107
108 static void convert_matrix(DSPContext *dsp, int (*qmat)[64], uint16_t (*qmat16)[2][64],
109 const uint16_t *quant_matrix, int bias, int qmin, int qmax)
110 {
111 int qscale;
112
113 for(qscale=qmin; qscale<=qmax; qscale++){
114 int i;
115 if (dsp->fdct == ff_jpeg_fdct_islow
116 #ifdef FAAN_POSTSCALE
117 || dsp->fdct == ff_faandct
118 #endif
119 ) {
120 for(i=0;i<64;i++) {
121 const int j= dsp->idct_permutation[i];
122 /* 16 <= qscale * quant_matrix[i] <= 7905 */
123 /* 19952 <= aanscales[i] * qscale * quant_matrix[i] <= 249205026 */
124 /* (1<<36)/19952 >= (1<<36)/(aanscales[i] * qscale * quant_matrix[i]) >= (1<<36)/249205026 */
125 /* 3444240 >= (1<<36)/(aanscales[i] * qscale * quant_matrix[i]) >= 275 */
126
127 qmat[qscale][i] = (int)((uint64_t_C(1) << QMAT_SHIFT) /
128 (qscale * quant_matrix[j]));
129 }
130 } else if (dsp->fdct == fdct_ifast
131 #ifndef FAAN_POSTSCALE
132 || dsp->fdct == ff_faandct
133 #endif
134 ) {
135 for(i=0;i<64;i++) {
136 const int j= dsp->idct_permutation[i];
137 /* 16 <= qscale * quant_matrix[i] <= 7905 */
138 /* 19952 <= aanscales[i] * qscale * quant_matrix[i] <= 249205026 */
139 /* (1<<36)/19952 >= (1<<36)/(aanscales[i] * qscale * quant_matrix[i]) >= (1<<36)/249205026 */
140 /* 3444240 >= (1<<36)/(aanscales[i] * qscale * quant_matrix[i]) >= 275 */
141
142 qmat[qscale][i] = (int)((uint64_t_C(1) << (QMAT_SHIFT + 14)) /
143 (aanscales[i] * qscale * quant_matrix[j]));
144 }
145 } else {
146 for(i=0;i<64;i++) {
147 const int j= dsp->idct_permutation[i];
148 /* We can safely suppose that 16 <= quant_matrix[i] <= 255
149 So 16 <= qscale * quant_matrix[i] <= 7905
150 so (1<<19) / 16 >= (1<<19) / (qscale * quant_matrix[i]) >= (1<<19) / 7905
151 so 32768 >= (1<<19) / (qscale * quant_matrix[i]) >= 67
152 */
153 qmat[qscale][i] = (int)((uint64_t_C(1) << QMAT_SHIFT) / (qscale * quant_matrix[j]));
154 // qmat [qscale][i] = (1 << QMAT_SHIFT_MMX) / (qscale * quant_matrix[i]);
155 qmat16[qscale][0][i] = (1 << QMAT_SHIFT_MMX) / (qscale * quant_matrix[j]);
156
157 if(qmat16[qscale][0][i]==0 || qmat16[qscale][0][i]==128*256) qmat16[qscale][0][i]=128*256-1;
158 qmat16[qscale][1][i]= ROUNDED_DIV(bias<<(16-QUANT_BIAS_SHIFT), qmat16[qscale][0][i]);
159 }
160 }
161 }
162 }
163
164 static inline void update_qscale(MpegEncContext *s){
165 s->qscale= (s->lambda*139 + FF_LAMBDA_SCALE*64) >> (FF_LAMBDA_SHIFT + 7);
166 s->qscale= clip(s->qscale, s->avctx->qmin, s->avctx->qmax);
167
168 s->lambda2= (s->lambda*s->lambda + FF_LAMBDA_SCALE/2) >> FF_LAMBDA_SHIFT;
169 }
170 #endif //CONFIG_ENCODERS
171
172 void ff_init_scantable(uint8_t *permutation, ScanTable *st, const uint8_t *src_scantable){
173 int i;
174 int end;
175
176 st->scantable= src_scantable;
177
178 for(i=0; i<64; i++){
179 int j;
180 j = src_scantable[i];
181 st->permutated[i] = permutation[j];
182 #ifdef ARCH_POWERPC
183 st->inverse[j] = i;
184 #endif
185 }
186
187 end=-1;
188 for(i=0; i<64; i++){
189 int j;
190 j = st->permutated[i];
191 if(j>end) end=j;
192 st->raster_end[i]= end;
193 }
194 }
195
196 #ifdef CONFIG_ENCODERS
197 void ff_write_quant_matrix(PutBitContext *pb, int16_t *matrix){
198 int i;
199
200 if(matrix){
201 put_bits(pb, 1, 1);
202 for(i=0;i<64;i++) {
203 put_bits(pb, 8, matrix[ ff_zigzag_direct[i] ]);
204 }
205 }else
206 put_bits(pb, 1, 0);
207 }
208 #endif //CONFIG_ENCODERS
209
210 /* init common dct for both encoder and decoder */
211 int DCT_common_init(MpegEncContext *s)
212 {
213 s->dct_unquantize_h263_intra = dct_unquantize_h263_intra_c;
214 s->dct_unquantize_h263_inter = dct_unquantize_h263_inter_c;
215 s->dct_unquantize_mpeg1_intra = dct_unquantize_mpeg1_intra_c;
216 s->dct_unquantize_mpeg1_inter = dct_unquantize_mpeg1_inter_c;
217 s->dct_unquantize_mpeg2_intra = dct_unquantize_mpeg2_intra_c;
218 s->dct_unquantize_mpeg2_inter = dct_unquantize_mpeg2_inter_c;
219
220 #ifdef CONFIG_ENCODERS
221 s->dct_quantize= dct_quantize_c;
222 #endif
223
224 #ifdef HAVE_MMX
225 MPV_common_init_mmx(s);
226 #endif
227 #ifdef ARCH_ALPHA
228 MPV_common_init_axp(s);
229 #endif
230 #ifdef HAVE_MLIB
231 MPV_common_init_mlib(s);
232 #endif
233 #ifdef HAVE_MMI
234 MPV_common_init_mmi(s);
235 #endif
236 #ifdef ARCH_ARMV4L
237 MPV_common_init_armv4l(s);
238 #endif
239 #ifdef ARCH_POWERPC
240 MPV_common_init_ppc(s);
241 #endif
242
243 #ifdef CONFIG_ENCODERS
244 s->fast_dct_quantize= s->dct_quantize;
245
246 if(s->flags&CODEC_FLAG_TRELLIS_QUANT){
247 s->dct_quantize= dct_quantize_trellis_c; //move before MPV_common_init_*
248 }
249
250 #endif //CONFIG_ENCODERS
251
252 /* load & permutate scantables
253 note: only wmv uses differnt ones
254 */
255 if(s->alternate_scan){
256 ff_init_scantable(s->dsp.idct_permutation, &s->inter_scantable , ff_alternate_vertical_scan);
257 ff_init_scantable(s->dsp.idct_permutation, &s->intra_scantable , ff_alternate_vertical_scan);
258 }else{
259 ff_init_scantable(s->dsp.idct_permutation, &s->inter_scantable , ff_zigzag_direct);
260 ff_init_scantable(s->dsp.idct_permutation, &s->intra_scantable , ff_zigzag_direct);
261 }
262 ff_init_scantable(s->dsp.idct_permutation, &s->intra_h_scantable, ff_alternate_horizontal_scan);
263 ff_init_scantable(s->dsp.idct_permutation, &s->intra_v_scantable, ff_alternate_vertical_scan);
264
265 s->picture_structure= PICT_FRAME;
266
267 return 0;
268 }
269
270 static void copy_picture(Picture *dst, Picture *src){
271 *dst = *src;
272 dst->type= FF_BUFFER_TYPE_COPY;
273 }
274
275 static void copy_picture_attributes(AVFrame *dst, AVFrame *src){
276 dst->pict_type = src->pict_type;
277 dst->quality = src->quality;
278 dst->coded_picture_number = src->coded_picture_number;
279 dst->display_picture_number = src->display_picture_number;
280 // dst->reference = src->reference;
281 dst->pts = src->pts;
282 dst->interlaced_frame = src->interlaced_frame;
283 dst->top_field_first = src->top_field_first;
284 }
285
286 /**
287 * allocates a Picture
288 * The pixels are allocated/set by calling get_buffer() if shared=0
289 */
290 static int alloc_picture(MpegEncContext *s, Picture *pic, int shared){
291 const int big_mb_num= s->mb_stride*(s->mb_height+1) + 1; //the +1 is needed so memset(,,stride*height) doesnt sig11
292 const int mb_array_size= s->mb_stride*s->mb_height;
293 const int b8_array_size= s->b8_stride*s->mb_height*2;
294 const int b4_array_size= s->b4_stride*s->mb_height*4;
295 int i;
296
297 if(shared){
298 assert(pic->data[0]);
299 assert(pic->type == 0 || pic->type == FF_BUFFER_TYPE_SHARED);
300 pic->type= FF_BUFFER_TYPE_SHARED;
301 }else{
302 int r;
303
304 assert(!pic->data[0]);
305
306 r= s->avctx->get_buffer(s->avctx, (AVFrame*)pic);
307
308 if(r<0 || !pic->age || !pic->type || !pic->data[0]){
309 av_log(s->avctx, AV_LOG_ERROR, "get_buffer() failed (%d %d %d %p)\n", r, pic->age, pic->type, pic->data[0]);
310 return -1;
311 }
312
313 if(s->linesize && (s->linesize != pic->linesize[0] || s->uvlinesize != pic->linesize[1])){
314 av_log(s->avctx, AV_LOG_ERROR, "get_buffer() failed (stride changed)\n");
315 return -1;
316 }
317
318 if(pic->linesize[1] != pic->linesize[2]){
319 av_log(s->avctx, AV_LOG_ERROR, "get_buffer() failed (uv stride missmatch)\n");
320 return -1;
321 }
322
323 s->linesize = pic->linesize[0];
324 s->uvlinesize= pic->linesize[1];
325 }
326
327 if(pic->qscale_table==NULL){
328 if (s->encoding) {
329 CHECKED_ALLOCZ(pic->mb_var , mb_array_size * sizeof(int16_t))
330 CHECKED_ALLOCZ(pic->mc_mb_var, mb_array_size * sizeof(int16_t))
331 CHECKED_ALLOCZ(pic->mb_mean , mb_array_size * sizeof(int8_t))
332 }
333
334 CHECKED_ALLOCZ(pic->mbskip_table , mb_array_size * sizeof(uint8_t)+2) //the +2 is for the slice end check
335 CHECKED_ALLOCZ(pic->qscale_table , mb_array_size * sizeof(uint8_t))
336 CHECKED_ALLOCZ(pic->mb_type_base , big_mb_num * sizeof(int))
337 pic->mb_type= pic->mb_type_base + s->mb_stride+1;
338 if(s->out_format == FMT_H264){
339 for(i=0; i<2; i++){
340 CHECKED_ALLOCZ(pic->motion_val_base[i], 2 * (b4_array_size+1) * sizeof(uint16_t))
341 pic->motion_val[i]= pic->motion_val_base[i]+1;
342 CHECKED_ALLOCZ(pic->ref_index[i] , b8_array_size * sizeof(uint8_t))
343 }
344 pic->motion_subsample_log2= 2;
345 }else if(s->out_format == FMT_H263 || s->encoding || (s->avctx->debug&FF_DEBUG_MV) || (s->avctx->debug_mv)){
346 for(i=0; i<2; i++){
347 CHECKED_ALLOCZ(pic->motion_val_base[i], 2 * (b8_array_size+1) * sizeof(uint16_t)*2) //FIXME
348 pic->motion_val[i]= pic->motion_val_base[i]+1;
349 }
350 pic->motion_subsample_log2= 3;
351 }
352 pic->qstride= s->mb_stride;
353 CHECKED_ALLOCZ(pic->pan_scan , 1 * sizeof(AVPanScan))
354 }
355
356 //it might be nicer if the application would keep track of these but it would require a API change
357 memmove(s->prev_pict_types+1, s->prev_pict_types, PREV_PICT_TYPES_BUFFER_SIZE-1);
358 s->prev_pict_types[0]= s->pict_type;
359 if(pic->age < PREV_PICT_TYPES_BUFFER_SIZE && s->prev_pict_types[pic->age] == B_TYPE)
360 pic->age= INT_MAX; // skiped MBs in b frames are quite rare in mpeg1/2 and its a bit tricky to skip them anyway
361
362 return 0;
363 fail: //for the CHECKED_ALLOCZ macro
364 return -1;
365 }
366
367 /**
368 * deallocates a picture
369 */
370 static void free_picture(MpegEncContext *s, Picture *pic){
371 int i;
372
373 if(pic->data[0] && pic->type!=FF_BUFFER_TYPE_SHARED){
374 s->avctx->release_buffer(s->avctx, (AVFrame*)pic);
375 }
376
377 av_freep(&pic->mb_var);
378 av_freep(&pic->mc_mb_var);
379 av_freep(&pic->mb_mean);
380 av_freep(&pic->mbskip_table);
381 av_freep(&pic->qscale_table);
382 av_freep(&pic->mb_type_base);
383 av_freep(&pic->pan_scan);
384 pic->mb_type= NULL;
385 for(i=0; i<2; i++){
386 av_freep(&pic->motion_val_base[i]);
387 av_freep(&pic->ref_index[i]);
388 }
389
390 if(pic->type == FF_BUFFER_TYPE_SHARED){
391 for(i=0; i<4; i++){
392 pic->base[i]=
393 pic->data[i]= NULL;
394 }
395 pic->type= 0;
396 }
397 }
398
399 /* init common structure for both encoder and decoder */
400 int MPV_common_init(MpegEncContext *s)
401 {
402 int y_size, c_size, yc_size, i, mb_array_size, mv_table_size, x, y;
403
404 dsputil_init(&s->dsp, s->avctx);
405 DCT_common_init(s);
406
407 s->flags= s->avctx->flags;
408
409 s->mb_width = (s->width + 15) / 16;
410 s->mb_height = (s->height + 15) / 16;
411 s->mb_stride = s->mb_width + 1;
412 s->b8_stride = s->mb_width*2 + 1;
413 s->b4_stride = s->mb_width*4 + 1;
414 mb_array_size= s->mb_height * s->mb_stride;
415 mv_table_size= (s->mb_height+2) * s->mb_stride + 1;
416
417 /* set default edge pos, will be overriden in decode_header if needed */
418 s->h_edge_pos= s->mb_width*16;
419 s->v_edge_pos= s->mb_height*16;
420
421 s->mb_num = s->mb_width * s->mb_height;
422
423 s->block_wrap[0]=
424 s->block_wrap[1]=
425 s->block_wrap[2]=
426 s->block_wrap[3]= s->mb_width*2 + 2;
427 s->block_wrap[4]=
428 s->block_wrap[5]= s->mb_width + 2;
429
430 s->y_dc_scale_table=
431 s->c_dc_scale_table= ff_mpeg1_dc_scale_table;
432 s->chroma_qscale_table= ff_default_chroma_qscale_table;
433 if (!s->encoding)
434 s->progressive_sequence= 1;
435 s->progressive_frame= 1;
436 s->coded_picture_number = 0;
437
438 y_size = (2 * s->mb_width + 2) * (2 * s->mb_height + 2);
439 c_size = (s->mb_width + 2) * (s->mb_height + 2);
440 yc_size = y_size + 2 * c_size;
441
442 /* convert fourcc to upper case */
443 s->avctx->codec_tag= toupper( s->avctx->codec_tag &0xFF)
444 + (toupper((s->avctx->codec_tag>>8 )&0xFF)<<8 )
445 + (toupper((s->avctx->codec_tag>>16)&0xFF)<<16)
446 + (toupper((s->avctx->codec_tag>>24)&0xFF)<<24);
447
448 s->avctx->stream_codec_tag= toupper( s->avctx->stream_codec_tag &0xFF)
449 + (toupper((s->avctx->stream_codec_tag>>8 )&0xFF)<<8 )
450 + (toupper((s->avctx->stream_codec_tag>>16)&0xFF)<<16)
451 + (toupper((s->avctx->stream_codec_tag>>24)&0xFF)<<24);
452
453 CHECKED_ALLOCZ(s->allocated_edge_emu_buffer, (s->width+64)*2*17*2); //(width + edge + align)*interlaced*MBsize*tolerance
454 s->edge_emu_buffer= s->allocated_edge_emu_buffer + (s->width+64)*2*17;
455
456 s->avctx->coded_frame= (AVFrame*)&s->current_picture;
457
458 CHECKED_ALLOCZ(s->mb_index2xy, (s->mb_num+1)*sizeof(int)) //error ressilience code looks cleaner with this
459 for(y=0; y<s->mb_height; y++){
460 for(x=0; x<s->mb_width; x++){
461 s->mb_index2xy[ x + y*s->mb_width ] = x + y*s->mb_stride;
462 }
463 }
464 s->mb_index2xy[ s->mb_height*s->mb_width ] = (s->mb_height-1)*s->mb_stride + s->mb_width; //FIXME really needed?
465
466 if (s->encoding) {
467 /* Allocate MV tables */
468 CHECKED_ALLOCZ(s->p_mv_table_base , mv_table_size * 2 * sizeof(int16_t))
469 CHECKED_ALLOCZ(s->b_forw_mv_table_base , mv_table_size * 2 * sizeof(int16_t))
470 CHECKED_ALLOCZ(s->b_back_mv_table_base , mv_table_size * 2 * sizeof(int16_t))
471 CHECKED_ALLOCZ(s->b_bidir_forw_mv_table_base , mv_table_size * 2 * sizeof(int16_t))
472 CHECKED_ALLOCZ(s->b_bidir_back_mv_table_base , mv_table_size * 2 * sizeof(int16_t))
473 CHECKED_ALLOCZ(s->b_direct_mv_table_base , mv_table_size * 2 * sizeof(int16_t))
474 s->p_mv_table = s->p_mv_table_base + s->mb_stride + 1;
475 s->b_forw_mv_table = s->b_forw_mv_table_base + s->mb_stride + 1;
476 s->b_back_mv_table = s->b_back_mv_table_base + s->mb_stride + 1;
477 s->b_bidir_forw_mv_table= s->b_bidir_forw_mv_table_base + s->mb_stride + 1;
478 s->b_bidir_back_mv_table= s->b_bidir_back_mv_table_base + s->mb_stride + 1;
479 s->b_direct_mv_table = s->b_direct_mv_table_base + s->mb_stride + 1;
480
481 //FIXME should be linesize instead of s->width*2 but that isnt known before get_buffer()
482 CHECKED_ALLOCZ(s->me.scratchpad, s->width*2*16*3*sizeof(uint8_t))
483
484 CHECKED_ALLOCZ(s->me.map , ME_MAP_SIZE*sizeof(uint32_t))
485 CHECKED_ALLOCZ(s->me.score_map, ME_MAP_SIZE*sizeof(uint32_t))
486
487 if(s->codec_id==CODEC_ID_MPEG4){
488 CHECKED_ALLOCZ(s->tex_pb_buffer, PB_BUFFER_SIZE);
489 CHECKED_ALLOCZ( s->pb2_buffer, PB_BUFFER_SIZE);
490 }
491
492 if(s->msmpeg4_version){
493 CHECKED_ALLOCZ(s->ac_stats, 2*2*(MAX_LEVEL+1)*(MAX_RUN+1)*2*sizeof(int));
494 }
495 CHECKED_ALLOCZ(s->avctx->stats_out, 256);
496
497 /* Allocate MB type table */
498 CHECKED_ALLOCZ(s->mb_type , mb_array_size * sizeof(uint16_t)) //needed for encoding
499
500 CHECKED_ALLOCZ(s->lambda_table, mb_array_size * sizeof(int))
501
502 CHECKED_ALLOCZ(s->q_intra_matrix, 64*32 * sizeof(int))
503 CHECKED_ALLOCZ(s->q_inter_matrix, 64*32 * sizeof(int))
504 CHECKED_ALLOCZ(s->q_intra_matrix16, 64*32*2 * sizeof(uint16_t))
505 CHECKED_ALLOCZ(s->q_inter_matrix16, 64*32*2 * sizeof(uint16_t))
506 CHECKED_ALLOCZ(s->input_picture, MAX_PICTURE_COUNT * sizeof(Picture*))
507 CHECKED_ALLOCZ(s->reordered_input_picture, MAX_PICTURE_COUNT * sizeof(Picture*))
508
509 if(s->avctx->noise_reduction){
510 CHECKED_ALLOCZ(s->dct_error_sum, 2 * 64 * sizeof(int))
511 CHECKED_ALLOCZ(s->dct_offset, 2 * 64 * sizeof(uint16_t))
512 }
513 }
514 CHECKED_ALLOCZ(s->blocks, 64*6*2 * sizeof(DCTELEM))
515
516 CHECKED_ALLOCZ(s->picture, MAX_PICTURE_COUNT * sizeof(Picture))
517
518 CHECKED_ALLOCZ(s->error_status_table, mb_array_size*sizeof(uint8_t))
519
520 if(s->codec_id==CODEC_ID_MPEG4 || (s->flags & CODEC_FLAG_INTERLACED_ME)){
521 /* interlaced direct mode decoding tables */
522 for(i=0; i<2; i++){
523 int j, k;
524 for(j=0; j<2; j++){
525 for(k=0; k<2; k++){
526 CHECKED_ALLOCZ(s->b_field_mv_table_base[i][j][k] , mv_table_size * 2 * sizeof(int16_t))
527 s->b_field_mv_table[i][j][k] = s->b_field_mv_table_base[i][j][k] + s->mb_stride + 1;
528 }
529 CHECKED_ALLOCZ(s->b_field_select_table[i][j] , mb_array_size * 2 * sizeof(uint8_t))
530 CHECKED_ALLOCZ(s->p_field_mv_table_base[i][j] , mv_table_size * 2 * sizeof(int16_t))
531 s->p_field_mv_table[i][j] = s->p_field_mv_table_base[i][j] + s->mb_stride + 1;
532 }
533 CHECKED_ALLOCZ(s->p_field_select_table[i] , mb_array_size * 2 * sizeof(uint8_t))
534 }
535 }
536 if (s->out_format == FMT_H263) {
537 /* ac values */
538 CHECKED_ALLOCZ(s->ac_val[0], yc_size * sizeof(int16_t) * 16);
539 s->ac_val[1] = s->ac_val[0] + y_size;
540 s->ac_val[2] = s->ac_val[1] + c_size;
541
542 /* cbp values */
543 CHECKED_ALLOCZ(s->coded_block, y_size);
544
545 /* divx501 bitstream reorder buffer */
546 CHECKED_ALLOCZ(s->bitstream_buffer, BITSTREAM_BUFFER_SIZE);
547
548 /* cbp, ac_pred, pred_dir */
549 CHECKED_ALLOCZ(s->cbp_table , mb_array_size * sizeof(uint8_t))
550 CHECKED_ALLOCZ(s->pred_dir_table, mb_array_size * sizeof(uint8_t))
551 }
552
553 if (s->h263_pred || s->h263_plus || !s->encoding) {
554 /* dc values */
555 //MN: we need these for error resilience of intra-frames
556 CHECKED_ALLOCZ(s->dc_val[0], yc_size * sizeof(int16_t));
557 s->dc_val[1] = s->dc_val[0] + y_size;
558 s->dc_val[2] = s->dc_val[1] + c_size;
559 for(i=0;i<yc_size;i++)
560 s->dc_val[0][i] = 1024;
561 }
562
563 /* which mb is a intra block */
564 CHECKED_ALLOCZ(s->mbintra_table, mb_array_size);
565 memset(s->mbintra_table, 1, mb_array_size);
566
567 /* default structure is frame */
568 s->picture_structure = PICT_FRAME;
569
570 /* init macroblock skip table */
571 CHECKED_ALLOCZ(s->mbskip_table, mb_array_size+2);
572 //Note the +1 is for a quicker mpeg4 slice_end detection
573 CHECKED_ALLOCZ(s->prev_pict_types, PREV_PICT_TYPES_BUFFER_SIZE);
574
575 s->block= s->blocks[0];
576
577 for(i=0;i<12;i++){
578 s->pblocks[i] = (short *)(&s->block[i]);
579 }
580
581 s->parse_context.state= -1;
582 if((s->avctx->debug&(FF_DEBUG_VIS_QP|FF_DEBUG_VIS_MB_TYPE)) || (s->avctx->debug_mv)){
583 s->visualization_buffer[0] = av_malloc((s->mb_width*16 + 2*EDGE_WIDTH) * s->mb_height*16 + 2*EDGE_WIDTH);
584 s->visualization_buffer[1] = av_malloc((s->mb_width*8 + EDGE_WIDTH) * s->mb_height*8 + EDGE_WIDTH);
585 s->visualization_buffer[2] = av_malloc((s->mb_width*8 + EDGE_WIDTH) * s->mb_height*8 + EDGE_WIDTH);
586 }
587
588 s->context_initialized = 1;
589 return 0;
590 fail:
591 MPV_common_end(s);
592 return -1;
593 }
594
595
596 //extern int sads;
597
598 /* init common structure for both encoder and decoder */
599 void MPV_common_end(MpegEncContext *s)
600 {
601 int i, j, k;
602
603 av_freep(&s->parse_context.buffer);
604 s->parse_context.buffer_size=0;
605
606 av_freep(&s->mb_type);
607 av_freep(&s->p_mv_table_base);
608 av_freep(&s->b_forw_mv_table_base);
609 av_freep(&s->b_back_mv_table_base);
610 av_freep(&s->b_bidir_forw_mv_table_base);
611 av_freep(&s->b_bidir_back_mv_table_base);
612 av_freep(&s->b_direct_mv_table_base);
613 s->p_mv_table= NULL;
614 s->b_forw_mv_table= NULL;
615 s->b_back_mv_table= NULL;
616 s->b_bidir_forw_mv_table= NULL;
617 s->b_bidir_back_mv_table= NULL;
618 s->b_direct_mv_table= NULL;
619 for(i=0; i<2; i++){
620 for(j=0; j<2; j++){
621 for(k=0; k<2; k++){
622 av_freep(&s->b_field_mv_table_base[i][j][k]);
623 s->b_field_mv_table[i][j][k]=NULL;
624 }
625 av_freep(&s->b_field_select_table[i][j]);
626 av_freep(&s->p_field_mv_table_base[i][j]);
627 s->p_field_mv_table[i][j]=NULL;
628 }
629 av_freep(&s->p_field_select_table[i]);
630 }
631
632 av_freep(&s->dc_val[0]);
633 av_freep(&s->ac_val[0]);
634 av_freep(&s->coded_block);
635 av_freep(&s->mbintra_table);
636 av_freep(&s->cbp_table);
637 av_freep(&s->pred_dir_table);
638 av_freep(&s->me.scratchpad);
639 av_freep(&s->me.map);
640 av_freep(&s->me.score_map);
641
642 av_freep(&s->mbskip_table);
643 av_freep(&s->prev_pict_types);
644 av_freep(&s->bitstream_buffer);
645 av_freep(&s->tex_pb_buffer);
646 av_freep(&s->pb2_buffer);
647 av_freep(&s->allocated_edge_emu_buffer); s->edge_emu_buffer= NULL;
648 av_freep(&s->avctx->stats_out);
649 av_freep(&s->ac_stats);
650 av_freep(&s->error_status_table);
651 av_freep(&s->mb_index2xy);
652 av_freep(&s->lambda_table);
653 av_freep(&s->q_intra_matrix);
654 av_freep(&s->q_inter_matrix);
655 av_freep(&s->q_intra_matrix16);
656 av_freep(&s->q_inter_matrix16);
657 av_freep(&s->blocks);
658 av_freep(&s->input_picture);
659 av_freep(&s->reordered_input_picture);
660 av_freep(&s->dct_error_sum);
661 av_freep(&s->dct_offset);
662
663 if(s->picture){
664 for(i=0; i<MAX_PICTURE_COUNT; i++){
665 free_picture(s, &s->picture[i]);
666 }
667 }
668 av_freep(&s->picture);
669 avcodec_default_free_buffers(s->avctx);
670 s->context_initialized = 0;
671 s->last_picture_ptr=
672 s->next_picture_ptr=
673 s->current_picture_ptr= NULL;
674 for(i=0; i<3; i++)
675 if (s->visualization_buffer[i])
676 av_free(s->visualization_buffer[i]);
677 }
678
679 #ifdef CONFIG_ENCODERS
680
681 /* init video encoder */
682 int MPV_encode_init(AVCodecContext *avctx)
683 {
684 MpegEncContext *s = avctx->priv_data;
685 int i, dummy;
686 int chroma_h_shift, chroma_v_shift;
687
688 avctx->pix_fmt = PIX_FMT_YUV420P; // FIXME
689
690 s->bit_rate = avctx->bit_rate;
691 s->width = avctx->width;
692 s->height = avctx->height;
693 if(avctx->gop_size > 600){
694 av_log(avctx, AV_LOG_ERROR, "Warning keyframe interval too large! reducing it ...\n");
695 avctx->gop_size=600;
696 }
697 s->gop_size = avctx->gop_size;
698 s->avctx = avctx;
699 s->flags= avctx->flags;
700 s->max_b_frames= avctx->max_b_frames;
701 s->codec_id= avctx->codec->id;
702 s->luma_elim_threshold = avctx->luma_elim_threshold;
703 s->chroma_elim_threshold= avctx->chroma_elim_threshold;
704 s->strict_std_compliance= avctx->strict_std_compliance;
705 s->data_partitioning= avctx->flags & CODEC_FLAG_PART;
706 s->quarter_sample= (avctx->flags & CODEC_FLAG_QPEL)!=0;
707 s->mpeg_quant= avctx->mpeg_quant;
708 s->rtp_mode= !!avctx->rtp_payload_size;
709
710 if (s->gop_size <= 1) {
711 s->intra_only = 1;
712 s->gop_size = 12;
713 } else {
714 s->intra_only = 0;
715 }
716
717 s->me_method = avctx->me_method;
718
719 /* Fixed QSCALE */
720 s->fixed_qscale = !!(avctx->flags & CODEC_FLAG_QSCALE);
721
722 s->adaptive_quant= ( s->avctx->lumi_masking
723 || s->avctx->dark_masking
724 || s->avctx->temporal_cplx_masking
725 || s->avctx->spatial_cplx_masking
726 || s->avctx->p_masking
727 || (s->flags&CODEC_FLAG_QP_RD))
728 && !s->fixed_qscale;
729
730 s->obmc= !!(s->flags & CODEC_FLAG_OBMC);
731 s->loop_filter= !!(s->flags & CODEC_FLAG_LOOP_FILTER);
732 s->alternate_scan= !!(s->flags & CODEC_FLAG_ALT_SCAN);
733
734 if((s->flags & CODEC_FLAG_4MV) && s->codec_id != CODEC_ID_MPEG4
735 && s->codec_id != CODEC_ID_H263 && s->codec_id != CODEC_ID_H263P){
736 av_log(avctx, AV_LOG_ERROR, "4MV not supported by codec\n");
737 return -1;
738 }
739
740 if(s->obmc && s->avctx->mb_decision != FF_MB_DECISION_SIMPLE){
741 av_log(avctx, AV_LOG_ERROR, "OBMC is only supported with simple mb decission\n");
742 return -1;
743 }
744
745 if(s->obmc && s->codec_id != CODEC_ID_H263 && s->codec_id != CODEC_ID_H263P){
746 av_log(avctx, AV_LOG_ERROR, "OBMC is only supported with H263(+)\n");
747 return -1;
748 }
749
750 if(s->quarter_sample && s->codec_id != CODEC_ID_MPEG4){
751 av_log(avctx, AV_LOG_ERROR, "qpel not supported by codec\n");
752 return -1;
753 }
754
755 if(s->data_partitioning && s->codec_id != CODEC_ID_MPEG4){
756 av_log(avctx, AV_LOG_ERROR, "data partitioning not supported by codec\n");
757 return -1;
758 }
759
760 if(s->max_b_frames && s->codec_id != CODEC_ID_MPEG4 && s->codec_id != CODEC_ID_MPEG1VIDEO && s->codec_id != CODEC_ID_MPEG2VIDEO){
761 av_log(avctx, AV_LOG_ERROR, "b frames not supported by codec\n");
762 return -1;
763 }
764
765 if(s->mpeg_quant && s->codec_id != CODEC_ID_MPEG4){ //FIXME mpeg2 uses that too
766 av_log(avctx, AV_LOG_ERROR, "mpeg2 style quantization not supporetd by codec\n");
767 return -1;
768 }
769
770 if((s->flags & CODEC_FLAG_CBP_RD) && !(s->flags & CODEC_FLAG_TRELLIS_QUANT)){
771 av_log(avctx, AV_LOG_ERROR, "CBP RD needs trellis quant\n");
772 return -1;
773 }
774
775 if((s->flags & CODEC_FLAG_QP_RD) && s->avctx->mb_decision != FF_MB_DECISION_RD){
776 av_log(avctx, AV_LOG_ERROR, "QP RD needs mbd=2\n");
777 return -1;
778 }
779
780 if(s->codec_id==CODEC_ID_MJPEG){
781 s->intra_quant_bias= 1<<(QUANT_BIAS_SHIFT-1); //(a + x/2)/x
782 s->inter_quant_bias= 0;
783 }else if(s->mpeg_quant || s->codec_id==CODEC_ID_MPEG1VIDEO || s->codec_id==CODEC_ID_MPEG2VIDEO){
784 s->intra_quant_bias= 3<<(QUANT_BIAS_SHIFT-3); //(a + x*3/8)/x
785 s->inter_quant_bias= 0;
786 }else{
787 s->intra_quant_bias=0;
788 s->inter_quant_bias=-(1<<(QUANT_BIAS_SHIFT-2)); //(a - x/4)/x
789 }
790
791 if(avctx->intra_quant_bias != FF_DEFAULT_QUANT_BIAS)
792 s->intra_quant_bias= avctx->intra_quant_bias;
793 if(avctx->inter_quant_bias != FF_DEFAULT_QUANT_BIAS)
794 s->inter_quant_bias= avctx->inter_quant_bias;
795
796 avcodec_get_chroma_sub_sample(avctx->pix_fmt, &chroma_h_shift, &chroma_v_shift);
797
798 av_reduce(&s->time_increment_resolution, &dummy, s->avctx->frame_rate, s->avctx->frame_rate_base, (1<<16)-1);
799 s->time_increment_bits = av_log2(s->time_increment_resolution - 1) + 1;
800
801 switch(avctx->codec->id) {
802 case CODEC_ID_MPEG1VIDEO:
803 s->out_format = FMT_MPEG1;
804 s->low_delay= 0; //s->max_b_frames ? 0 : 1;
805 avctx->delay= s->low_delay ? 0 : (s->max_b_frames + 1);
806 break;
807 case CODEC_ID_MPEG2VIDEO:
808 s->out_format = FMT_MPEG1;
809 s->low_delay= 0; //s->max_b_frames ? 0 : 1;
810 avctx->delay= s->low_delay ? 0 : (s->max_b_frames + 1);
811 s->rtp_mode= 1;
812 break;
813 case CODEC_ID_LJPEG:
814 case CODEC_ID_MJPEG:
815 s->out_format = FMT_MJPEG;
816 s->intra_only = 1; /* force intra only for jpeg */
817 s->mjpeg_write_tables = 1; /* write all tables */
818 s->mjpeg_data_only_frames = 0; /* write all the needed headers */
819 s->mjpeg_vsample[0] = 1<<chroma_v_shift;
820 s->mjpeg_vsample[1] = 1;
821 s->mjpeg_vsample[2] = 1;
822 s->mjpeg_hsample[0] = 1<<chroma_h_shift;
823 s->mjpeg_hsample[1] = 1;
824 s->mjpeg_hsample[2] = 1;
825 if (mjpeg_init(s) < 0)
826 return -1;
827 avctx->delay=0;
828 s->low_delay=1;
829 break;
830 #ifdef CONFIG_RISKY
831 case CODEC_ID_H263:
832 if (h263_get_picture_format(s->width, s->height) == 7) {
833 av_log(avctx, AV_LOG_INFO, "Input picture size isn't suitable for h263 codec! try h263+\n");
834 return -1;
835 }
836 s->out_format = FMT_H263;
837 s->obmc= (avctx->flags & CODEC_FLAG_OBMC) ? 1:0;
838 avctx->delay=0;
839 s->low_delay=1;
840 break;
841 case CODEC_ID_H263P:
842 s->out_format = FMT_H263;
843 s->h263_plus = 1;
844 /* Fx */
845 s->umvplus = (avctx->flags & CODEC_FLAG_H263P_UMV) ? 1:0;
846 s->h263_aic= (avctx->flags & CODEC_FLAG_H263P_AIC) ? 1:0;
847 s->modified_quant= s->h263_aic;
848 s->alt_inter_vlc= (avctx->flags & CODEC_FLAG_H263P_AIV) ? 1:0;
849 s->obmc= (avctx->flags & CODEC_FLAG_OBMC) ? 1:0;
850 s->loop_filter= (avctx->flags & CODEC_FLAG_LOOP_FILTER) ? 1:0;
851 s->unrestricted_mv= s->obmc || s->loop_filter || s->umvplus;
852 s->h263_slice_structured= (s->flags & CODEC_FLAG_H263P_SLICE_STRUCT) ? 1:0;
853
854 /* /Fx */
855 /* These are just to be sure */
856 avctx->delay=0;
857 s->low_delay=1;
858 break;
859 case CODEC_ID_FLV1:
860 s->out_format = FMT_H263;
861 s->h263_flv = 2; /* format = 1; 11-bit codes */
862 s->unrestricted_mv = 1;
863 s->rtp_mode=0; /* don't allow GOB */
864 avctx->delay=0;
865 s->low_delay=1;
866 break;
867 case CODEC_ID_RV10:
868 s->out_format = FMT_H263;
869 avctx->delay=0;
870 s->low_delay=1;
871 break;
872 case CODEC_ID_MPEG4:
873 s->out_format = FMT_H263;
874 s->h263_pred = 1;
875 s->unrestricted_mv = 1;
876 s->low_delay= s->max_b_frames ? 0 : 1;
877 avctx->delay= s->low_delay ? 0 : (s->max_b_frames + 1);
878 break;
879 case CODEC_ID_MSMPEG4V1:
880 s->out_format = FMT_H263;
881 s->h263_msmpeg4 = 1;
882 s->h263_pred = 1;
883 s->unrestricted_mv = 1;
884 s->msmpeg4_version= 1;
885 avctx->delay=0;
886 s->low_delay=1;
887 break;
888 case CODEC_ID_MSMPEG4V2:
889 s->out_format = FMT_H263;
890 s->h263_msmpeg4 = 1;
891 s->h263_pred = 1;
892 s->unrestricted_mv = 1;
893 s->msmpeg4_version= 2;
894 avctx->delay=0;
895 s->low_delay=1;
896 break;
897 case CODEC_ID_MSMPEG4V3:
898 s->out_format = FMT_H263;
899 s->h263_msmpeg4 = 1;
900 s->h263_pred = 1;
901 s->unrestricted_mv = 1;
902 s->msmpeg4_version= 3;
903 s->flipflop_rounding=1;
904 avctx->delay=0;
905 s->low_delay=1;
906 break;
907 case CODEC_ID_WMV1:
908 s->out_format = FMT_H263;
909 s->h263_msmpeg4 = 1;
910 s->h263_pred = 1;
911 s->unrestricted_mv = 1;
912 s->msmpeg4_version= 4;
913 s->flipflop_rounding=1;
914 avctx->delay=0;
915 s->low_delay=1;
916 break;
917 case CODEC_ID_WMV2:
918 s->out_format = FMT_H263;
919 s->h263_msmpeg4 = 1;
920 s->h263_pred = 1;
921 s->unrestricted_mv = 1;
922 s->msmpeg4_version= 5;
923 s->flipflop_rounding=1;
924 avctx->delay=0;
925 s->low_delay=1;
926 break;
927 #endif
928 default:
929 return -1;
930 }
931
932 { /* set up some save defaults, some codecs might override them later */
933 static int done=0;
934 if(!done){
935 int i;
936 done=1;
937
938 default_mv_penalty= av_mallocz( sizeof(uint8_t)*(MAX_FCODE+1)*(2*MAX_MV+1) );
939 memset(default_mv_penalty, 0, sizeof(uint8_t)*(MAX_FCODE+1)*(2*MAX_MV+1));
940 memset(default_fcode_tab , 0, sizeof(uint8_t)*(2*MAX_MV+1));
941
942 for(i=-16; i<16; i++){
943 default_fcode_tab[i + MAX_MV]= 1;
944 }
945 }
946 }
947 s->me.mv_penalty= default_mv_penalty;
948 s->fcode_tab= default_fcode_tab;
949
950 /* dont use mv_penalty table for crap MV as it would be confused */
951 //FIXME remove after fixing / removing old ME
952 if (s->me_method < ME_EPZS) s->me.mv_penalty = default_mv_penalty;
953
954 s->encoding = 1;
955
956 /* init */
957 if (MPV_common_init(s) < 0)
958 return -1;
959
960 if(s->modified_quant)
961 s->chroma_qscale_table= ff_h263_chroma_qscale_table;
962 s->progressive_frame=
963 s->progressive_sequence= !(avctx->flags & (CODEC_FLAG_INTERLACED_DCT|CODEC_FLAG_INTERLACED_ME));
964
965 ff_init_me(s);
966
967 #ifdef CONFIG_ENCODERS
968 #ifdef CONFIG_RISKY
969 if (s->out_format == FMT_H263)
970 h263_encode_init(s);
971 if(s->msmpeg4_version)
972 ff_msmpeg4_encode_init(s);
973 #endif
974 if (s->out_format == FMT_MPEG1)
975 ff_mpeg1_encode_init(s);
976 #endif
977
978 /* init default q matrix */
979 for(i=0;i<64;i++) {
980 int j= s->dsp.idct_permutation[i];
981 #ifdef CONFIG_RISKY
982 if(s->codec_id==CODEC_ID_MPEG4 && s->mpeg_quant){
983 s->intra_matrix[j] = ff_mpeg4_default_intra_matrix[i];
984 s->inter_matrix[j] = ff_mpeg4_default_non_intra_matrix[i];
985 }else if(s->out_format == FMT_H263){
986 s->intra_matrix[j] =
987 s->inter_matrix[j] = ff_mpeg1_default_non_intra_matrix[i];
988 }else
989 #endif
990 { /* mpeg1/2 */
991 s->intra_matrix[j] = ff_mpeg1_default_intra_matrix[i];
992 s->inter_matrix[j] = ff_mpeg1_default_non_intra_matrix[i];
993 }
994 if(s->avctx->intra_matrix)
995 s->intra_matrix[j] = s->avctx->intra_matrix[i];
996 if(s->avctx->inter_matrix)
997 s->inter_matrix[j] = s->avctx->inter_matrix[i];
998 }
999
1000 /* precompute matrix */
1001 /* for mjpeg, we do include qscale in the matrix */
1002 if (s->out_format != FMT_MJPEG) {
1003 convert_matrix(&s->dsp, s->q_intra_matrix, s->q_intra_matrix16,
1004 s->intra_matrix, s->intra_quant_bias, 1, 31);
1005 convert_matrix(&s->dsp, s->q_inter_matrix, s->q_inter_matrix16,
1006 s->inter_matrix, s->inter_quant_bias, 1, 31);
1007 }
1008
1009 if(ff_rate_control_init(s) < 0)
1010 return -1;
1011
1012 s->picture_number = 0;
1013 s->input_picture_number = 0;
1014 s->picture_in_gop_number = 0;
1015 s->fake_picture_number = 0;
1016 /* motion detector init */
1017 s->f_code = 1;
1018 s->b_code = 1;
1019
1020 return 0;
1021 }
1022
1023 int MPV_encode_end(AVCodecContext *avctx)
1024 {
1025 MpegEncContext *s = avctx->priv_data;
1026
1027 #ifdef STATS
1028 print_stats();
1029 #endif
1030
1031 ff_rate_control_uninit(s);
1032
1033 MPV_common_end(s);
1034 if (s->out_format == FMT_MJPEG)
1035 mjpeg_close(s);
1036
1037 av_freep(&avctx->extradata);
1038
1039 return 0;
1040 }
1041
1042 #endif //CONFIG_ENCODERS
1043
1044 void init_rl(RLTable *rl)
1045 {
1046 int8_t max_level[MAX_RUN+1], max_run[MAX_LEVEL+1];
1047 uint8_t index_run[MAX_RUN+1];
1048 int last, run, level, start, end, i;
1049
1050 /* compute max_level[], max_run[] and index_run[] */
1051 for(last=0;last<2;last++) {
1052 if (last == 0) {
1053 start = 0;
1054 end = rl->last;
1055 } else {
1056 start = rl->last;
1057 end = rl->n;
1058 }
1059
1060 memset(max_level, 0, MAX_RUN + 1);
1061 memset(max_run, 0, MAX_LEVEL + 1);
1062 memset(index_run, rl->n, MAX_RUN + 1);
1063 for(i=start;i<end;i++) {
1064 run = rl->table_run[i];
1065 level = rl->table_level[i];
1066 if (index_run[run] == rl->n)
1067 index_run[run] = i;
1068 if (level > max_level[run])
1069 max_level[run] = level;
1070 if (run > max_run[level])
1071 max_run[level] = run;
1072 }
1073 rl->max_level[last] = av_malloc(MAX_RUN + 1);
1074 memcpy(rl->max_level[last], max_level, MAX_RUN + 1);
1075 rl->max_run[last] = av_malloc(MAX_LEVEL + 1);
1076 memcpy(rl->max_run[last], max_run, MAX_LEVEL + 1);
1077 rl->index_run[last] = av_malloc(MAX_RUN + 1);
1078 memcpy(rl->index_run[last], index_run, MAX_RUN + 1);
1079 }
1080 }
1081
1082 /* draw the edges of width 'w' of an image of size width, height */
1083 //FIXME check that this is ok for mpeg4 interlaced
1084 static void draw_edges_c(uint8_t *buf, int wrap, int width, int height, int w)
1085 {
1086 uint8_t *ptr, *last_line;
1087 int i;
1088
1089 last_line = buf + (height - 1) * wrap;
1090 for(i=0;i<w;i++) {
1091 /* top and bottom */
1092 memcpy(buf - (i + 1) * wrap, buf, width);
1093 memcpy(last_line + (i + 1) * wrap, last_line, width);
1094 }
1095 /* left and right */
1096 ptr = buf;
1097 for(i=0;i<height;i++) {
1098 memset(ptr - w, ptr[0], w);
1099 memset(ptr + width, ptr[width-1], w);
1100 ptr += wrap;
1101 }
1102 /* corners */
1103 for(i=0;i<w;i++) {
1104 memset(buf - (i + 1) * wrap - w, buf[0], w); /* top left */
1105 memset(buf - (i + 1) * wrap + width, buf[width-1], w); /* top right */
1106 memset(last_line + (i + 1) * wrap - w, last_line[0], w); /* top left */
1107 memset(last_line + (i + 1) * wrap + width, last_line[width-1], w); /* top right */
1108 }
1109 }
1110
1111 int ff_find_unused_picture(MpegEncContext *s, int shared){
1112 int i;
1113
1114 if(shared){
1115 for(i=0; i<MAX_PICTURE_COUNT; i++){
1116 if(s->picture[i].data[0]==NULL && s->picture[i].type==0) return i;
1117 }
1118 }else{
1119 for(i=0; i<MAX_PICTURE_COUNT; i++){
1120 if(s->picture[i].data[0]==NULL && s->picture[i].type!=0) return i; //FIXME
1121 }
1122 for(i=0; i<MAX_PICTURE_COUNT; i++){
1123 if(s->picture[i].data[0]==NULL) return i;
1124 }
1125 }
1126
1127 assert(0);
1128 return -1;
1129 }
1130
1131 static void update_noise_reduction(MpegEncContext *s){
1132 int intra, i;
1133
1134 for(intra=0; intra<2; intra++){
1135 if(s->dct_count[intra] > (1<<16)){
1136 for(i=0; i<64; i++){
1137 s->dct_error_sum[intra][i] >>=1;
1138 }
1139 s->dct_count[intra] >>= 1;
1140 }
1141
1142 for(i=0; i<64; i++){
1143 s->dct_offset[intra][i]= (s->avctx->noise_reduction * s->dct_count[intra] + s->dct_error_sum[intra][i]/2) / (s->dct_error_sum[intra][i]+1);
1144 }
1145 }
1146 }
1147
1148 /**
1149 * generic function for encode/decode called after coding/decoding the header and before a frame is coded/decoded
1150 */
1151 int MPV_frame_start(MpegEncContext *s, AVCodecContext *avctx)
1152 {
1153 int i;
1154 AVFrame *pic;
1155 s->mb_skiped = 0;
1156
1157 assert(s->last_picture_ptr==NULL || s->out_format != FMT_H264 || s->codec_id == CODEC_ID_SVQ3);
1158
1159 /* mark&release old frames */
1160 if (s->pict_type != B_TYPE && s->last_picture_ptr && s->last_picture_ptr->data[0]) {
1161 avctx->release_buffer(avctx, (AVFrame*)s->last_picture_ptr);
1162
1163 /* release forgotten pictures */
1164 /* if(mpeg124/h263) */
1165 if(!s->encoding){
1166 for(i=0; i<MAX_PICTURE_COUNT; i++){
1167 if(s->picture[i].data[0] && &s->picture[i] != s->next_picture_ptr && s->picture[i].reference){
1168 av_log(avctx, AV_LOG_ERROR, "releasing zombie picture\n");
1169 avctx->release_buffer(avctx, (AVFrame*)&s->picture[i]);
1170 }
1171 }
1172 }
1173 }
1174 alloc:
1175 if(!s->encoding){
1176 /* release non refernce frames */
1177 for(i=0; i<MAX_PICTURE_COUNT; i++){
1178 if(s->picture[i].data[0] && !s->picture[i].reference /*&& s->picture[i].type!=FF_BUFFER_TYPE_SHARED*/){
1179 s->avctx->release_buffer(s->avctx, (AVFrame*)&s->picture[i]);
1180 }
1181 }
1182
1183 if(s->current_picture_ptr && s->current_picture_ptr->data[0]==NULL)
1184 pic= (AVFrame*)s->current_picture_ptr; //we allready have a unused image (maybe it was set before reading the header)
1185 else{
1186 i= ff_find_unused_picture(s, 0);
1187 pic= (AVFrame*)&s->picture[i];
1188 }
1189
1190 pic->reference= s->pict_type != B_TYPE ? 3 : 0;
1191
1192 pic->coded_picture_number= s->coded_picture_number++;
1193
1194 if( alloc_picture(s, (Picture*)pic, 0) < 0)
1195 return -1;
1196
1197 s->current_picture_ptr= (Picture*)pic;
1198 s->current_picture_ptr->top_field_first= s->top_field_first; //FIXME use only the vars from current_pic
1199 s->current_picture_ptr->interlaced_frame= !s->progressive_frame && !s->progressive_sequence;
1200 }
1201
1202 s->current_picture_ptr->pict_type= s->pict_type;
1203 // if(s->flags && CODEC_FLAG_QSCALE)
1204 // s->current_picture_ptr->quality= s->new_picture_ptr->quality;
1205 s->current_picture_ptr->key_frame= s->pict_type == I_TYPE;
1206
1207 copy_picture(&s->current_picture, s->current_picture_ptr);
1208
1209 if(s->out_format != FMT_H264 || s->codec_id == CODEC_ID_SVQ3){
1210 if (s->pict_type != B_TYPE) {
1211 s->last_picture_ptr= s->next_picture_ptr;
1212 s->next_picture_ptr= s->current_picture_ptr;
1213 }
1214
1215 if(s->last_picture_ptr) copy_picture(&s->last_picture, s->last_picture_ptr);
1216 if(s->next_picture_ptr) copy_picture(&s->next_picture, s->next_picture_ptr);
1217
1218 if(s->pict_type != I_TYPE && (s->last_picture_ptr==NULL || s->last_picture_ptr->data[0]==NULL)){
1219 av_log(avctx, AV_LOG_ERROR, "warning: first frame is no keyframe\n");
1220 assert(s->pict_type != B_TYPE); //these should have been dropped if we dont have a reference
1221 goto alloc;
1222 }
1223
1224 assert(s->pict_type == I_TYPE || (s->last_picture_ptr && s->last_picture_ptr->data[0]));
1225
1226 if(s->picture_structure!=PICT_FRAME){
1227 int i;
1228 for(i=0; i<4; i++){
1229 if(s->picture_structure == PICT_BOTTOM_FIELD){
1230 s->current_picture.data[i] += s->current_picture.linesize[i];
1231 }
1232 s->current_picture.linesize[i] *= 2;
1233 s->last_picture.linesize[i] *=2;
1234 s->next_picture.linesize[i] *=2;
1235 }
1236 }
1237 }
1238
1239 s->hurry_up= s->avctx->hurry_up;
1240 s->error_resilience= avctx->error_resilience;
1241
1242 /* set dequantizer, we cant do it during init as it might change for mpeg4
1243 and we cant do it in the header decode as init isnt called for mpeg4 there yet */
1244 if(s->mpeg_quant || s->codec_id == CODEC_ID_MPEG2VIDEO){
1245 s->dct_unquantize_intra = s->dct_unquantize_mpeg2_intra;
1246 s->dct_unquantize_inter = s->dct_unquantize_mpeg2_inter;
1247 }else if(s->out_format == FMT_H263){
1248 s->dct_unquantize_intra = s->dct_unquantize_h263_intra;
1249 s->dct_unquantize_inter = s->dct_unquantize_h263_inter;
1250 }else{
1251 s->dct_unquantize_intra = s->dct_unquantize_mpeg1_intra;
1252 s->dct_unquantize_inter = s->dct_unquantize_mpeg1_inter;
1253 }
1254
1255 if(s->dct_error_sum){
1256 assert(s->avctx->noise_reduction && s->encoding);
1257
1258 update_noise_reduction(s);
1259 }
1260
1261 #ifdef HAVE_XVMC
1262 if(s->avctx->xvmc_acceleration)
1263 return XVMC_field_start(s, avctx);
1264 #endif
1265 return 0;
1266 }
1267
1268 /* generic function for encode/decode called after a frame has been coded/decoded */
1269 void MPV_frame_end(MpegEncContext *s)
1270 {
1271 int i;
1272 /* draw edge for correct motion prediction if outside */
1273 #ifdef HAVE_XVMC
1274 //just to make sure that all data is rendered.
1275 if(s->avctx->xvmc_acceleration){
1276 XVMC_field_end(s);
1277 }else
1278 #endif
1279 if(s->unrestricted_mv && s->pict_type != B_TYPE && !s->intra_only && !(s->flags&CODEC_FLAG_EMU_EDGE)) {
1280 draw_edges(s->current_picture.data[0], s->linesize , s->h_edge_pos , s->v_edge_pos , EDGE_WIDTH );
1281 draw_edges(s->current_picture.data[1], s->uvlinesize, s->h_edge_pos>>1, s->v_edge_pos>>1, EDGE_WIDTH/2);
1282 draw_edges(s->current_picture.data[2], s->uvlinesize, s->h_edge_pos>>1, s->v_edge_pos>>1, EDGE_WIDTH/2);
1283 }
1284 emms_c();
1285
1286 s->last_pict_type = s->pict_type;
1287 if(s->pict_type!=B_TYPE){
1288 s->last_non_b_pict_type= s->pict_type;
1289 }
1290 #if 0
1291 /* copy back current_picture variables */
1292 for(i=0; i<MAX_PICTURE_COUNT; i++){
1293 if(s->picture[i].data[0] == s->current_picture.data[0]){
1294 s->picture[i]= s->current_picture;
1295 break;
1296 }
1297 }
1298 assert(i<MAX_PICTURE_COUNT);
1299 #endif
1300
1301 if(s->encoding){
1302 /* release non refernce frames */
1303 for(i=0; i<MAX_PICTURE_COUNT; i++){
1304 if(s->picture[i].data[0] && !s->picture[i].reference /*&& s->picture[i].type!=FF_BUFFER_TYPE_SHARED*/){
1305 s->avctx->release_buffer(s->avctx, (AVFrame*)&s->picture[i]);
1306 }
1307 }
1308 }
1309 // clear copies, to avoid confusion
1310 #if 0
1311 memset(&s->last_picture, 0, sizeof(Picture));
1312 memset(&s->next_picture, 0, sizeof(Picture));
1313 memset(&s->current_picture, 0, sizeof(Picture));
1314 #endif
1315 }
1316
1317 /**
1318 * draws an line from (ex, ey) -> (sx, sy).
1319 * @param w width of the image
1320 * @param h height of the image
1321 * @param stride stride/linesize of the image
1322 * @param color color of the arrow
1323 */
1324 static void draw_line(uint8_t *buf, int sx, int sy, int ex, int ey, int w, int h, int stride, int color){
1325 int t, x, y, f;
1326
1327 sx= clip(sx, 0, w-1);
1328 sy= clip(sy, 0, h-1);
1329 ex= clip(ex, 0, w-1);
1330 ey= clip(ey, 0, h-1);
1331
1332 buf[sy*stride + sx]+= color;
1333
1334 if(ABS(ex - sx) > ABS(ey - sy)){
1335 if(sx > ex){
1336 t=sx; sx=ex; ex=t;
1337 t=sy; sy=ey; ey=t;
1338 }
1339 buf+= sx + sy*stride;
1340 ex-= sx;
1341 f= ((ey-sy)<<16)/ex;
1342 for(x= 0; x <= ex; x++){
1343 y= ((x*f) + (1<<15))>>16;
1344 buf[y*stride + x]+= color;
1345 }
1346 }else{
1347 if(sy > ey){
1348 t=sx; sx=ex; ex=t;
1349 t=sy; sy=ey; ey=t;
1350 }
1351 buf+= sx + sy*stride;
1352 ey-= sy;
1353 if(ey) f= ((ex-sx)<<16)/ey;
1354 else f= 0;
1355 for(y= 0; y <= ey; y++){
1356 x= ((y*f) + (1<<15))>>16;
1357 buf[y*stride + x]+= color;
1358 }
1359 }
1360 }
1361
1362 /**
1363 * draws an arrow from (ex, ey) -> (sx, sy).
1364 * @param w width of the image
1365 * @param h height of the image
1366 * @param stride stride/linesize of the image
1367 * @param color color of the arrow
1368 */
1369 static void draw_arrow(uint8_t *buf, int sx, int sy, int ex, int ey, int w, int h, int stride, int color){
1370 int dx,dy;
1371
1372 sx= clip(sx, -100, w+100);
1373 sy= clip(sy, -100, h+100);
1374 ex= clip(ex, -100, w+100);
1375 ey= clip(ey, -100, h+100);
1376
1377 dx= ex - sx;
1378 dy= ey - sy;
1379
1380 if(dx*dx + dy*dy > 3*3){
1381 int rx= dx + dy;
1382 int ry= -dx + dy;
1383 int length= ff_sqrt((rx*rx + ry*ry)<<8);
1384
1385 //FIXME subpixel accuracy
1386 rx= ROUNDED_DIV(rx*3<<4, length);
1387 ry= ROUNDED_DIV(ry*3<<4, length);
1388
1389 draw_line(buf, sx, sy, sx + rx, sy + ry, w, h, stride, color);
1390 draw_line(buf, sx, sy, sx - ry, sy + rx, w, h, stride, color);
1391 }
1392 draw_line(buf, sx, sy, ex, ey, w, h, stride, color);
1393 }
1394
1395 /**
1396 * prints debuging info for the given picture.
1397 */
1398 void ff_print_debug_info(MpegEncContext *s, AVFrame *pict){
1399
1400 if(!pict || !pict->mb_type) return;
1401
1402 if(s->avctx->debug&(FF_DEBUG_SKIP | FF_DEBUG_QP | FF_DEBUG_MB_TYPE)){
1403 int x,y;
1404
1405 av_log(s->avctx,AV_LOG_DEBUG,"New frame, type: ");
1406 switch (pict->pict_type) {
1407 case FF_I_TYPE: av_log(s->avctx,AV_LOG_DEBUG,"I\n"); break;
1408 case FF_P_TYPE: av_log(s->avctx,AV_LOG_DEBUG,"P\n"); break;
1409 case FF_B_TYPE: av_log(s->avctx,AV_LOG_DEBUG,"B\n"); break;
1410 case FF_S_TYPE: av_log(s->avctx,AV_LOG_DEBUG,"S\n"); break;
1411 case FF_SI_TYPE: av_log(s->avctx,AV_LOG_DEBUG,"SI\n"); break;
1412 case FF_SP_TYPE: av_log(s->avctx,AV_LOG_DEBUG,"SP\n"); break;
1413 }
1414 for(y=0; y<s->mb_height; y++){
1415 for(x=0; x<s->mb_width; x++){
1416 if(s->avctx->debug&FF_DEBUG_SKIP){
1417 int count= s->mbskip_table[x + y*s->mb_stride];
1418 if(count>9) count=9;
1419 av_log(s->avctx, AV_LOG_DEBUG, "%1d", count);
1420 }
1421 if(s->avctx->debug&FF_DEBUG_QP){
1422 av_log(s->avctx, AV_LOG_DEBUG, "%2d", pict->qscale_table[x + y*s->mb_stride]);
1423 }
1424 if(s->avctx->debug&FF_DEBUG_MB_TYPE){
1425 int mb_type= pict->mb_type[x + y*s->mb_stride];
1426 //Type & MV direction
1427 if(IS_PCM(mb_type))
1428 av_log(s->avctx, AV_LOG_DEBUG, "P");
1429 else if(IS_INTRA(mb_type) && IS_ACPRED(mb_type))
1430 av_log(s->avctx, AV_LOG_DEBUG, "A");
1431 else if(IS_INTRA4x4(mb_type))
1432 av_log(s->avctx, AV_LOG_DEBUG, "i");
1433 else if(IS_INTRA16x16(mb_type))
1434 av_log(s->avctx, AV_LOG_DEBUG, "I");
1435 else if(IS_DIRECT(mb_type) && IS_SKIP(mb_type))
1436 av_log(s->avctx, AV_LOG_DEBUG, "d");
1437 else if(IS_DIRECT(mb_type))
1438 av_log(s->avctx, AV_LOG_DEBUG, "D");
1439 else if(IS_GMC(mb_type) && IS_SKIP(mb_type))
1440 av_log(s->avctx, AV_LOG_DEBUG, "g");
1441 else if(IS_GMC(mb_type))
1442 av_log(s->avctx, AV_LOG_DEBUG, "G");
1443 else if(IS_SKIP(mb_type))
1444 av_log(s->avctx, AV_LOG_DEBUG, "S");
1445 else if(!USES_LIST(mb_type, 1))
1446 av_log(s->avctx, AV_LOG_DEBUG, ">");
1447 else if(!USES_LIST(mb_type, 0))
1448 av_log(s->avctx, AV_LOG_DEBUG, "<");
1449 else{
1450 assert(USES_LIST(mb_type, 0) && USES_LIST(mb_type, 1));
1451 av_log(s->avctx, AV_LOG_DEBUG, "X");
1452 }
1453
1454 //segmentation
1455 if(IS_8X8(mb_type))
1456 av_log(s->avctx, AV_LOG_DEBUG, "+");
1457 else if(IS_16X8(mb_type))
1458 av_log(s->avctx, AV_LOG_DEBUG, "-");
1459 else if(IS_8X16(mb_type))
1460 av_log(s->avctx, AV_LOG_DEBUG, "¦");
1461 else if(IS_INTRA(mb_type) || IS_16X16(mb_type))
1462 av_log(s->avctx, AV_LOG_DEBUG, " ");
1463 else
1464 av_log(s->avctx, AV_LOG_DEBUG, "?");
1465
1466
1467 if(IS_INTERLACED(mb_type) && s->codec_id == CODEC_ID_H264)
1468 av_log(s->avctx, AV_LOG_DEBUG, "=");
1469 else
1470 av_log(s->avctx, AV_LOG_DEBUG, " ");
1471 }
1472 // av_log(s->avctx, AV_LOG_DEBUG, " ");
1473 }
1474 av_log(s->avctx, AV_LOG_DEBUG, "\n");
1475 }
1476 }
1477
1478 if((s->avctx->debug&(FF_DEBUG_VIS_QP|FF_DEBUG_VIS_MB_TYPE)) || (s->avctx->debug_mv)){
1479 const int shift= 1 + s->quarter_sample;
1480 int mb_y;
1481 uint8_t *ptr;
1482 int i;
1483 s->low_delay=0; //needed to see the vectors without trashing the buffers
1484
1485 for(i=0; i<3; i++){
1486 memcpy(s->visualization_buffer[i], pict->data[i], (i==0) ? pict->linesize[i]*s->height:pict->linesize[i]*s->height/2);
1487 pict->data[i]= s->visualization_buffer[i];
1488 }
1489 pict->type= FF_BUFFER_TYPE_COPY;
1490 ptr= pict->data[0];
1491
1492 for(mb_y=0; mb_y<s->mb_height; mb_y++){
1493 int mb_x;
1494 for(mb_x=0; mb_x<s->mb_width; mb_x++){
1495 const int mb_index= mb_x + mb_y*s->mb_stride;
1496 if((s->avctx->debug_mv) && pict->motion_val){
1497 int type;
1498 for(type=0; type<3; type++){
1499 int direction;
1500 switch (type) {
1501 case 0: if ((!(s->avctx->debug_mv&FF_DEBUG_VIS_MV_P_FOR)) || (pict->pict_type!=FF_P_TYPE))
1502 continue;
1503 direction = 0;
1504 break;
1505 case 1: if ((!(s->avctx->debug_mv&FF_DEBUG_VIS_MV_B_FOR)) || (pict->pict_type!=FF_B_TYPE))
1506 continue;
1507 direction = 0;
1508 break;
1509 case 2: if ((!(s->avctx->debug_mv&FF_DEBUG_VIS_MV_B_BACK)) || (pict->pict_type!=FF_B_TYPE))
1510 continue;
1511 direction = 1;
1512 break;
1513 }
1514 if(IS_8X8(pict->mb_type[mb_index])){
1515 int i;
1516 for(i=0; i<4; i++){
1517 int sx= mb_x*16 + 4 + 8*(i&1);
1518 int sy= mb_y*16 + 4 + 8*(i>>1);
1519 int xy= 1 + mb_x*2 + (i&1) + (mb_y*2 + 1 + (i>>1))*(s->mb_width*2 + 2);
1520 int mx= (pict->motion_val[direction][xy][0]>>shift) + sx;
1521 int my= (pict->motion_val[direction][xy][1]>>shift) + sy;
1522 draw_arrow(ptr, sx, sy, mx, my, s->width, s->height, s->linesize, 100);
1523 }
1524 }else if(IS_16X8(pict->mb_type[mb_index])){
1525 int i;
1526 for(i=0; i<2; i++){
1527 int sx=mb_x*16 + 8;
1528 int sy=mb_y*16 + 4 + 8*i;
1529 int xy=1 + mb_x*2 + (mb_y*2 + 1 + i)*(s->mb_width*2 + 2);
1530 int mx=(pict->motion_val[direction][xy][0]>>shift) + sx;
1531 int my=(pict->motion_val[direction][xy][1]>>shift) + sy;
1532 draw_arrow(ptr, sx, sy, mx, my, s->width, s->height, s->linesize, 100);
1533 }
1534 }else{
1535 int sx= mb_x*16 + 8;
1536 int sy= mb_y*16 + 8;
1537 int xy= 1 + mb_x*2 + (mb_y*2 + 1)*(s->mb_width*2 + 2);
1538 int mx= (pict->motion_val[direction][xy][0]>>shift) + sx;
1539 int my= (pict->motion_val[direction][xy][1]>>shift) + sy;
1540 draw_arrow(ptr, sx, sy, mx, my, s->width, s->height, s->linesize, 100);
1541 }
1542 }
1543 }
1544 if((s->avctx->debug&FF_DEBUG_VIS_QP) && pict->motion_val){
1545 uint64_t c= (pict->qscale_table[mb_index]*128/31) * 0x0101010101010101ULL;
1546 int y;
1547 for(y=0; y<8; y++){
1548 *(uint64_t*)(pict->data[1] + 8*mb_x + (8*mb_y + y)*pict->linesize[1])= c;
1549 *(uint64_t*)(pict->data[2] + 8*mb_x + (8*mb_y + y)*pict->linesize[2])= c;
1550 }
1551 }
1552 if((s->avctx->debug&FF_DEBUG_VIS_MB_TYPE) && pict->motion_val){
1553 int mb_type= pict->mb_type[mb_index];
1554 uint64_t u,v;
1555 int y;
1556 #define COLOR(theta, r)\
1557 u= (int)(128 + r*cos(theta*3.141592/180));\
1558 v= (int)(128 + r*sin(theta*3.141592/180));
1559
1560
1561 u=v=128;
1562 if(IS_PCM(mb_type)){
1563 COLOR(120,48)
1564 }else if((IS_INTRA(mb_type) && IS_ACPRED(mb_type)) || IS_INTRA16x16(mb_type)){
1565 COLOR(30,48)
1566 }else if(IS_INTRA4x4(mb_type)){
1567 COLOR(90,48)
1568 }else if(IS_DIRECT(mb_type) && IS_SKIP(mb_type)){
1569 // COLOR(120,48)
1570 }else if(IS_DIRECT(mb_type)){
1571 COLOR(150,48)
1572 }else if(IS_GMC(mb_type) && IS_SKIP(mb_type)){
1573 COLOR(170,48)
1574 }else if(IS_GMC(mb_type)){
1575 COLOR(190,48)
1576 }else if(IS_SKIP(mb_type)){
1577 // COLOR(180,48)
1578 }else if(!USES_LIST(mb_type, 1)){
1579 COLOR(240,48)
1580 }else if(!USES_LIST(mb_type, 0)){
1581 COLOR(0,48)
1582 }else{
1583 assert(USES_LIST(mb_type, 0) && USES_LIST(mb_type, 1));
1584 COLOR(300,48)
1585 }
1586
1587 u*= 0x0101010101010101ULL;
1588 v*= 0x0101010101010101ULL;
1589 for(y=0; y<8; y++){
1590 *(uint64_t*)(pict->data[1] + 8*mb_x + (8*mb_y + y)*pict->linesize[1])= u;
1591 *(uint64_t*)(pict->data[2] + 8*mb_x + (8*mb_y + y)*pict->linesize[2])= v;
1592 }
1593
1594 //segmentation
1595 if(IS_8X8(mb_type) || IS_16X8(mb_type)){
1596 *(uint64_t*)(pict->data[0] + 16*mb_x + 0 + (16*mb_y + 8)*pict->linesize[0])^= 0x8080808080808080ULL;
1597 *(uint64_t*)(pict->data[0] + 16*mb_x + 8 + (16*mb_y + 8)*pict->linesize[0])^= 0x8080808080808080ULL;
1598 }
1599 if(IS_8X8(mb_type) || IS_8X16(mb_type)){
1600 for(y=0; y<16; y++)
1601 pict->data[0][16*mb_x + 8 + (16*mb_y + y)*pict->linesize[0]]^= 0x80;
1602 }
1603
1604 if(IS_INTERLACED(mb_type) && s->codec_id == CODEC_ID_H264){
1605 // hmm
1606 }
1607 }
1608 s->mbskip_table[mb_index]=0;
1609 }
1610 }
1611 }
1612 }
1613
1614 #ifdef CONFIG_ENCODERS
1615
1616 static int get_sae(uint8_t *src, int ref, int stride){
1617 int x,y;
1618 int acc=0;
1619
1620 for(y=0; y<16; y++){
1621 for(x=0; x<16; x++){
1622 acc+= ABS(src[x+y*stride] - ref);
1623 }
1624 }
1625
1626 return acc;
1627 }
1628
1629 static int get_intra_count(MpegEncContext *s, uint8_t *src, uint8_t *ref, int stride){
1630 int x, y, w, h;
1631 int acc=0;
1632
1633 w= s->width &~15;
1634 h= s->height&~15;
1635
1636 for(y=0; y<h; y+=16){
1637 for(x=0; x<w; x+=16){
1638 int offset= x + y*stride;
1639 int sad = s->dsp.sad[0](NULL, src + offset, ref + offset, stride, 16);
1640 int mean= (s->dsp.pix_sum(src + offset, stride) + 128)>>8;
1641 int sae = get_sae(src + offset, mean, stride);
1642
1643 acc+= sae + 500 < sad;
1644 }
1645 }
1646 return acc;
1647 }
1648
1649
1650 static int load_input_picture(MpegEncContext *s, AVFrame *pic_arg){
1651 AVFrame *pic=NULL;
1652 int i;
1653 const int encoding_delay= s->max_b_frames;
1654 int direct=1;
1655
1656 if(pic_arg){
1657 if(encoding_delay && !(s->flags&CODEC_FLAG_INPUT_PRESERVED)) direct=0;
1658 if(pic_arg->linesize[0] != s->linesize) direct=0;
1659 if(pic_arg->linesize[1] != s->uvlinesize) direct=0;
1660 if(pic_arg->linesize[2] != s->uvlinesize) direct=0;
1661
1662 // av_log(AV_LOG_DEBUG, "%d %d %d %d\n",pic_arg->linesize[0], pic_arg->linesize[1], s->linesize, s->uvlinesize);
1663
1664 if(direct){
1665 i= ff_find_unused_picture(s, 1);
1666
1667 pic= (AVFrame*)&s->picture[i];
1668 pic->reference= 3;
1669
1670 for(i=0; i<4; i++){
1671 pic->data[i]= pic_arg->data[i];
1672 pic->linesize[i]= pic_arg->linesize[i];
1673 }
1674 alloc_picture(s, (Picture*)pic, 1);
1675 }else{
1676 int offset= 16;
1677 i= ff_find_unused_picture(s, 0);
1678
1679 pic= (AVFrame*)&s->picture[i];
1680 pic->reference= 3;
1681
1682 alloc_picture(s, (Picture*)pic, 0);
1683
1684 if( pic->data[0] + offset == pic_arg->data[0]
1685 && pic->data[1] + offset == pic_arg->data[1]
1686 && pic->data[2] + offset == pic_arg->data[2]){
1687 // empty
1688 }else{
1689 int h_chroma_shift, v_chroma_shift;
1690 avcodec_get_chroma_sub_sample(s->avctx->pix_fmt, &h_chroma_shift, &v_chroma_shift);
1691
1692 for(i=0; i<3; i++){
1693 int src_stride= pic_arg->linesize[i];
1694 int dst_stride= i ? s->uvlinesize : s->linesize;
1695 int h_shift= i ? h_chroma_shift : 0;
1696 int v_shift= i ? v_chroma_shift : 0;
1697 int w= s->width >>h_shift;
1698 int h= s->height>>v_shift;
1699 uint8_t *src= pic_arg->data[i];
1700 uint8_t *dst= pic->data[i] + offset;
1701
1702 if(src_stride==dst_stride)
1703 memcpy(dst, src, src_stride*h);
1704 else{
1705 while(h--){
1706 memcpy(dst, src, w);
1707 dst += dst_stride;
1708 src += src_stride;
1709 }
1710 }
1711 }
1712 }
1713 }
1714 copy_picture_attributes(pic, pic_arg);
1715
1716 pic->display_picture_number= s->input_picture_number++;
1717 }
1718
1719 /* shift buffer entries */
1720 for(i=1; i<MAX_PICTURE_COUNT /*s->encoding_delay+1*/; i++)
1721 s->input_picture[i-1]= s->input_picture[i];
1722
1723 s->input_picture[encoding_delay]= (Picture*)pic;
1724
1725 return 0;
1726 }
1727
1728 static void select_input_picture(MpegEncContext *s){
1729 int i;
1730
1731 for(i=1; i<MAX_PICTURE_COUNT; i++)
1732 s->reordered_input_picture[i-1]= s->reordered_input_picture[i];
1733 s->reordered_input_picture[MAX_PICTURE_COUNT-1]= NULL;
1734
1735 /* set next picture types & ordering */
1736 if(s->reordered_input_picture[0]==NULL && s->input_picture[0]){
1737 if(/*s->picture_in_gop_number >= s->gop_size ||*/ s->next_picture_ptr==NULL || s->intra_only){
1738 s->reordered_input_picture[0]= s->input_picture[0];
1739 s->reordered_input_picture[0]->pict_type= I_TYPE;
1740 s->reordered_input_picture[0]->coded_picture_number= s->coded_picture_number++;
1741 }else{
1742 int b_frames;
1743
1744 if(s->flags&CODEC_FLAG_PASS2){
1745 for(i=0; i<s->max_b_frames+1; i++){
1746 int pict_num= s->input_picture[0]->display_picture_number + i;
1747 int pict_type= s->rc_context.entry[pict_num].new_pict_type;
1748 s->input_picture[i]->pict_type= pict_type;
1749
1750 if(i + 1 >= s->rc_context.num_entries) break;
1751 }
1752 }
1753
1754 if(s->input_picture[0]->pict_type){
1755 /* user selected pict_type */
1756 for(b_frames=0; b_frames<s->max_b_frames+1; b_frames++){
1757 if(s->input_picture[b_frames]->pict_type!=B_TYPE) break;
1758 }
1759
1760 if(b_frames > s->max_b_frames){
1761 av_log(s->avctx, AV_LOG_ERROR, "warning, too many bframes in a row\n");
1762 b_frames = s->max_b_frames;
1763 }
1764 }else if(s->avctx->b_frame_strategy==0){
1765 b_frames= s->max_b_frames;
1766 while(b_frames && !s->input_picture[b_frames]) b_frames--;
1767 }else if(s->avctx->b_frame_strategy==1){
1768 for(i=1; i<s->max_b_frames+1; i++){
1769 if(s->input_picture[i] && s->input_picture[i]->b_frame_score==0){
1770 s->input_picture[i]->b_frame_score=
1771 get_intra_count(s, s->input_picture[i ]->data[0],
1772 s->input_picture[i-1]->data[0], s->linesize) + 1;
1773 }
1774 }
1775 for(i=0; i<s->max_b_frames; i++){
1776 if(s->input_picture[i]==NULL || s->input_picture[i]->b_frame_score - 1 > s->mb_num/40) break;
1777 }
1778
1779 b_frames= FFMAX(0, i-1);
1780
1781 /* reset scores */
1782 for(i=0; i<b_frames+1; i++){
1783 s->input_picture[i]->b_frame_score=0;
1784 }
1785 }else{
1786 av_log(s->avctx, AV_LOG_ERROR, "illegal b frame strategy\n");
1787 b_frames=0;
1788 }
1789
1790 emms_c();
1791 //static int b_count=0;
1792 //b_count+= b_frames;
1793 //av_log(s->avctx, AV_LOG_DEBUG, "b_frames: %d\n", b_count);
1794
1795 s->reordered_input_picture[0]= s->input_picture[b_frames];
1796 if( s->picture_in_gop_number + b_frames >= s->gop_size
1797 || s->reordered_input_picture[0]->pict_type== I_TYPE)
1798 s->reordered_input_picture[0]->pict_type= I_TYPE;
1799 else
1800 s->reordered_input_picture[0]->pict_type= P_TYPE;
1801 s->reordered_input_picture[0]->coded_picture_number= s->coded_picture_number++;
1802 for(i=0; i<b_frames; i++){
1803 s->reordered_input_picture[i+1]= s->input_picture[i];
1804 s->reordered_input_picture[i+1]->pict_type= B_TYPE;
1805 s->reordered_input_picture[i+1]->coded_picture_number= s->coded_picture_number++;
1806 }
1807 }
1808 }
1809
1810 if(s->reordered_input_picture[0]){
1811 s->reordered_input_picture[0]->reference= s->reordered_input_picture[0]->pict_type!=B_TYPE ? 3 : 0;
1812
1813 copy_picture(&s->new_picture, s->reordered_input_picture[0]);
1814
1815 if(s->reordered_input_picture[0]->type == FF_BUFFER_TYPE_SHARED){
1816 // input is a shared pix, so we cant modifiy it -> alloc a new one & ensure that the shared one is reuseable
1817
1818 int i= ff_find_unused_picture(s, 0);
1819 Picture *pic= &s->picture[i];
1820
1821 /* mark us unused / free shared pic */
1822 for(i=0; i<4; i++)
1823 s->reordered_input_picture[0]->data[i]= NULL;
1824 s->reordered_input_picture[0]->type= 0;
1825
1826 copy_picture_attributes((AVFrame*)pic, (AVFrame*)s->reordered_input_picture[0]);
1827 pic->reference = s->reordered_input_picture[0]->reference;
1828
1829 alloc_picture(s, pic, 0);
1830
1831 s->current_picture_ptr= pic;
1832 }else{
1833 // input is not a shared pix -> reuse buffer for current_pix
1834
1835 assert( s->reordered_input_picture[0]->type==FF_BUFFER_TYPE_USER
1836 || s->reordered_input_picture[0]->type==FF_BUFFER_TYPE_INTERNAL);
1837
1838 s->current_picture_ptr= s->reordered_input_picture[0];
1839 for(i=0; i<4; i++){
1840 s->new_picture.data[i]+=16;
1841 }
1842 }
1843 copy_picture(&s->current_picture, s->current_picture_ptr);
1844
1845 s->picture_number= s->new_picture.display_picture_number;
1846 //printf("dpn:%d\n", s->picture_number);
1847 }else{
1848 memset(&s->new_picture, 0, sizeof(Picture));
1849 }
1850 }
1851
1852 int MPV_encode_picture(AVCodecContext *avctx,
1853 unsigned char *buf, int buf_size, void *data)
1854 {
1855 MpegEncContext *s = avctx->priv_data;
1856 AVFrame *pic_arg = data;
1857 int i, stuffing_count;
1858
1859 if(avctx->pix_fmt != PIX_FMT_YUV420P){
1860 av_log(avctx, AV_LOG_ERROR, "this codec supports only YUV420P\n");
1861 return -1;
1862 }
1863
1864 init_put_bits(&s->pb, buf, buf_size);
1865
1866 s->picture_in_gop_number++;
1867
1868 load_input_picture(s, pic_arg);
1869
1870 select_input_picture(s);
1871
1872 /* output? */
1873 if(s->new_picture.data[0]){
1874 s->pict_type= s->new_picture.pict_type;
1875 //emms_c();
1876 //printf("qs:%f %f %d\n", s->new_picture.quality, s->current_picture.quality, s->qscale);
1877 MPV_frame_start(s, avctx);
1878
1879 encode_picture(s, s->picture_number);
1880
1881 avctx->real_pict_num = s->picture_number;
1882 avctx->header_bits = s->header_bits;
1883 avctx->mv_bits = s->mv_bits;
1884 avctx->misc_bits = s->misc_bits;
1885 avctx->i_tex_bits = s->i_tex_bits;
1886 avctx->p_tex_bits = s->p_tex_bits;
1887 avctx->i_count = s->i_count;
1888 avctx->p_count = s->mb_num - s->i_count - s->skip_count; //FIXME f/b_count in avctx
1889 avctx->skip_count = s->skip_count;
1890
1891 MPV_frame_end(s);
1892
1893 if (s->out_format == FMT_MJPEG)
1894 mjpeg_picture_trailer(s);
1895
1896 if(s->flags&CODEC_FLAG_PASS1)
1897 ff_write_pass1_stats(s);
1898
1899 for(i=0; i<4; i++){
1900 avctx->error[i] += s->current_picture_ptr->error[i];
1901 }
1902 }
1903
1904 flush_put_bits(&s->pb);
1905 s->frame_bits = (pbBufPtr(&s->pb) - s->pb.buf) * 8;
1906
1907 stuffing_count= ff_vbv_update(s, s->frame_bits);
1908 if(stuffing_count){
1909 switch(s->codec_id){
1910 case CODEC_ID_MPEG1VIDEO:
1911 case CODEC_ID_MPEG2VIDEO:
1912 while(stuffing_count--){
1913 put_bits(&s->pb, 8, 0);
1914 }
1915 break;
1916 case CODEC_ID_MPEG4:
1917 put_bits(&s->pb, 16, 0);
1918 put_bits(&s->pb, 16, 0x1C3);
1919 stuffing_count -= 4;
1920 while(stuffing_count--){
1921 put_bits(&s->pb, 8, 0xFF);
1922 }
1923 break;
1924 default:
1925 av_log(s->avctx, AV_LOG_ERROR, "vbv buffer overflow\n");
1926 }
1927 flush_put_bits(&s->pb);
1928 s->frame_bits = (pbBufPtr(&s->pb) - s->pb.buf) * 8;
1929 }
1930
1931 /* update mpeg1/2 vbv_delay for CBR */
1932 if(s->avctx->rc_max_rate && s->avctx->rc_min_rate == s->avctx->rc_max_rate){
1933 int vbv_delay;
1934
1935 assert(s->repeat_first_field==0);
1936
1937 vbv_delay= lrintf(90000 * s->rc_context.buffer_index / s->avctx->rc_max_rate);
1938 assert(vbv_delay < 0xFFFF);
1939
1940 s->vbv_delay_ptr[0] &= 0xF8;
1941 s->vbv_delay_ptr[0] |= vbv_delay>>13;
1942 s->vbv_delay_ptr[1] = vbv_delay>>5;
1943 s->vbv_delay_ptr[2] &= 0x07;
1944 s->vbv_delay_ptr[2] |= vbv_delay<<3;
1945 }
1946
1947 s->total_bits += s->frame_bits;
1948 avctx->frame_bits = s->frame_bits;
1949
1950 return s->frame_bits/8;
1951 }
1952
1953 #endif //CONFIG_ENCODERS
1954
1955 static inline void gmc1_motion(MpegEncContext *s,
1956 uint8_t *dest_y, uint8_t *dest_cb, uint8_t *dest_cr,
1957 int dest_offset,
1958 uint8_t **ref_picture, int src_offset)
1959 {
1960 uint8_t *ptr;
1961 int offset, src_x, src_y, linesize, uvlinesize;
1962 int motion_x, motion_y;
1963 int emu=0;
1964
1965 motion_x= s->sprite_offset[0][0];
1966 motion_y= s->sprite_offset[0][1];
1967 src_x = s->mb_x * 16 + (motion_x >> (s->sprite_warping_accuracy+1));
1968 src_y = s->mb_y * 16 + (motion_y >> (s->sprite_warping_accuracy+1));
1969 motion_x<<=(3-s->sprite_warping_accuracy);
1970 motion_y<<=(3-s->sprite_warping_accuracy);
1971 src_x = clip(src_x, -16, s->width);
1972 if (src_x == s->width)
1973 motion_x =0;
1974 src_y = clip(src_y, -16, s->height);
1975 if (src_y == s->height)
1976 motion_y =0;
1977
1978 linesize = s->linesize;
1979 uvlinesize = s->uvlinesize;
1980
1981 ptr = ref_picture[0] + (src_y * linesize) + src_x + src_offset;
1982
1983 dest_y+=dest_offset;
1984 if(s->flags&CODEC_FLAG_EMU_EDGE){
1985 if( (unsigned)src_x >= s->h_edge_pos - 17
1986 || (unsigned)src_y >= s->v_edge_pos - 17){
1987 ff_emulated_edge_mc(s->edge_emu_buffer, ptr, linesize, 17, 17, src_x, src_y, s->h_edge_pos, s->v_edge_pos);
1988 ptr= s->edge_emu_buffer;
1989 }
1990 }
1991
1992 if((motion_x|motion_y)&7){
1993 s->dsp.gmc1(dest_y , ptr , linesize, 16, motion_x&15, motion_y&15, 128 - s->no_rounding);
1994 s->dsp.gmc1(dest_y+8, ptr+8, linesize, 16, motion_x&15, motion_y&15, 128 - s->no_rounding);
1995 }else{
1996 int dxy;
1997
1998 dxy= ((motion_x>>3)&1) | ((motion_y>>2)&2);
1999 if (s->no_rounding){
2000 s->dsp.put_no_rnd_pixels_tab[0][dxy](dest_y, ptr, linesize, 16);
2001 }else{
2002 s->dsp.put_pixels_tab [0][dxy](dest_y, ptr, linesize, 16);
2003 }
2004 }
2005
2006 if(s->flags&CODEC_FLAG_GRAY) return;
2007
2008 motion_x= s->sprite_offset[1][0];
2009 motion_y= s->sprite_offset[1][1];
2010 src_x = s->mb_x * 8 + (motion_x >> (s->sprite_warping_accuracy+1));
2011 src_y = s->mb_y * 8 + (motion_y >> (s->sprite_warping_accuracy+1));
2012 motion_x<<=(3-s->sprite_warping_accuracy);
2013 motion_y<<=(3-s->sprite_warping_accuracy);
2014 src_x = clip(src_x, -8, s->width>>1);
2015 if (src_x == s->width>>1)
2016 motion_x =0;
2017 src_y = clip(src_y, -8, s->height>>1);
2018 if (src_y == s->height>>1)
2019 motion_y =0;
2020
2021 offset = (src_y * uvlinesize) + src_x + (src_offset>>1);
2022 ptr = ref_picture[1] + offset;
2023 if(s->flags&CODEC_FLAG_EMU_EDGE){
2024 if( (unsigned)src_x >= (s->h_edge_pos>>1) - 9
2025 || (unsigned)src_y >= (s->v_edge_pos>>1) - 9){
2026 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);
2027 ptr= s->edge_emu_buffer;
2028 emu=1;
2029 }
2030 }
2031 s->dsp.gmc1(dest_cb + (dest_offset>>1), ptr, uvlinesize, 8, motion_x&15, motion_y&15, 128 - s->no_rounding);
2032
2033 ptr = ref_picture[2] + offset;
2034 if(emu){
2035 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);
2036 ptr= s->edge_emu_buffer;
2037 }
2038 s->dsp.gmc1(dest_cr + (dest_offset>>1), ptr, uvlinesize, 8, motion_x&15, motion_y&15, 128 - s->no_rounding);
2039
2040 return;
2041 }
2042
2043 static inline void gmc_motion(MpegEncContext *s,
2044 uint8_t *dest_y, uint8_t *dest_cb, uint8_t *dest_cr,
2045 int dest_offset,
2046 uint8_t **ref_picture, int src_offset)
2047 {
2048 uint8_t *ptr;
2049 int linesize, uvlinesize;
2050 const int a= s->sprite_warping_accuracy;
2051 int ox, oy;
2052
2053 linesize = s->linesize;
2054 uvlinesize = s->uvlinesize;
2055
2056 ptr = ref_picture[0] + src_offset;
2057
2058 dest_y+=dest_offset;
2059
2060 ox= s->sprite_offset[0][0] + s->sprite_delta[0][0]*s->mb_x*16 + s->sprite_delta[0][1]*s->mb_y*16;
2061 oy= s->sprite_offset[0][1] + s->sprite_delta[1][0]*s->mb_x*16 + s->sprite_delta[1][1]*s->mb_y*16;
2062
2063 s->dsp.gmc(dest_y, ptr, linesize, 16,
2064 ox,
2065 oy,
2066 s->sprite_delta[0][0], s->sprite_delta[0][1],
2067 s->sprite_delta[1][0], s->sprite_delta[1][1],
2068 a+1, (1<<(2*a+1)) - s->no_rounding,
2069 s->h_edge_pos, s->v_edge_pos);
2070 s->dsp.gmc(dest_y+8, ptr, linesize, 16,
2071 ox + s->sprite_delta[0][0]*8,
2072 oy + s->sprite_delta[1][0]*8,
2073 s->sprite_delta[0][0], s->sprite_delta[0][1],
2074 s->sprite_delta[1][0], s->sprite_delta[1][1],
2075 a+1, (1<<(2*a+1)) - s->no_rounding,
2076 s->h_edge_pos, s->v_edge_pos);
2077
2078 if(s->flags&CODEC_FLAG_GRAY) return;
2079
2080
2081 dest_cb+=dest_offset>>1;
2082 dest_cr+=dest_offset>>1;
2083
2084 ox= s->sprite_offset[1][0] + s->sprite_delta[0][0]*s->mb_x*8 + s->sprite_delta[0][1]*s->mb_y*8;
2085 oy= s->sprite_offset[1][1] + s->sprite_delta[1][0]*s->mb_x*8 + s->sprite_delta[1][1]*s->mb_y*8;
2086
2087 ptr = ref_picture[1] + (src_offset>>1);
2088 s->dsp.gmc(dest_cb, ptr, uvlinesize, 8,
2089 ox,
2090 oy,
2091 s->sprite_delta[0][0], s->sprite_delta[0][1],
2092 s->sprite_delta[1][0], s->sprite_delta[1][1],
2093 a+1, (1<<(2*a+1)) - s->no_rounding,
2094 s->h_edge_pos>>1, s->v_edge_pos>>1);
2095
2096 ptr = ref_picture[2] + (src_offset>>1);
2097 s->dsp.gmc(dest_cr, ptr, uvlinesize, 8,
2098 ox,
2099 oy,
2100 s->sprite_delta[0][0], s->sprite_delta[0][1],
2101 s->sprite_delta[1][0], s->sprite_delta[1][1],
2102 a+1, (1<<(2*a+1)) - s->no_rounding,
2103 s->h_edge_pos>>1, s->v_edge_pos>>1);
2104 }
2105
2106 /**
2107 * Copies a rectangular area of samples to a temporary buffer and replicates the boarder samples.
2108 * @param buf destination buffer
2109 * @param src source buffer
2110 * @param linesize number of bytes between 2 vertically adjacent samples in both the source and destination buffers
2111 * @param block_w width of block
2112 * @param block_h height of block
2113 * @param src_x x coordinate of the top left sample of the block in the source buffer
2114 * @param src_y y coordinate of the top left sample of the block in the source buffer
2115 * @param w width of the source buffer
2116 * @param h height of the source buffer
2117 */
2118 void ff_emulated_edge_mc(uint8_t *buf, uint8_t *src, int linesize, int block_w, int block_h,
2119 int src_x, int src_y, int w, int h){
2120 int x, y;
2121 int start_y, start_x, end_y, end_x;
2122
2123 if(src_y>= h){
2124 src+= (h-1-src_y)*linesize;
2125 src_y=h-1;
2126 }else if(src_y<=-block_h){
2127 src+= (1-block_h-src_y)*linesize;
2128 src_y=1-block_h;
2129 }
2130 if(src_x>= w){
2131 src+= (w-1-src_x);
2132 src_x=w-1;
2133 }else if(src_x<=-block_w){
2134 src+= (1-block_w-src_x);
2135 src_x=1-block_w;
2136 }
2137
2138 start_y= FFMAX(0, -src_y);
2139 start_x= FFMAX(0, -src_x);
2140 end_y= FFMIN(block_h, h-src_y);
2141 end_x= FFMIN(block_w, w-src_x);
2142
2143 // copy existing part
2144 for(y=start_y; y<end_y; y++){
2145 for(x=start_x; x<end_x; x++){
2146 buf[x + y*linesize]= src[x + y*linesize];
2147 }
2148 }
2149
2150 //top
2151 for(y=0; y<start_y; y++){
2152 for(x=start_x; x<end_x; x++){
2153 buf[x + y*linesize]= buf[x + start_y*linesize];
2154 }
2155 }
2156
2157 //bottom
2158 for(y=end_y; y<block_h; y++){
2159 for(x=start_x; x<end_x; x++){
2160 buf[x + y*linesize]= buf[x + (end_y-1)*linesize];
2161 }
2162 }
2163
2164 for(y=0; y<block_h; y++){
2165 //left
2166 for(x=0; x<start_x; x++){
2167 buf[x + y*linesize]= buf[start_x + y*linesize];
2168 }
2169
2170 //right
2171 for(x=end_x; x<block_w; x++){
2172 buf[x + y*linesize]= buf[end_x - 1 + y*linesize];
2173 }
2174 }
2175 }
2176
2177 static inline int hpel_motion(MpegEncContext *s,
2178 uint8_t *dest, uint8_t *src,
2179 int src_x, int src_y,
2180 int width, int height, int stride,
2181 int h_edge_pos, int v_edge_pos,
2182 int w, int h, op_pixels_func *pix_op,
2183 int motion_x, int motion_y)
2184 {
2185 int dxy;
2186 int emu=0;
2187
2188 dxy = ((motion_y & 1) << 1) | (motion_x & 1);
2189 src_x += motion_x >> 1;
2190 src_y += motion_y >> 1;
2191
2192 /* WARNING: do no forget half pels */
2193 src_x = clip(src_x, -16, width); //FIXME unneeded for emu?
2194 if (src_x == width)
2195 dxy &= ~1;
2196 src_y = clip(src_y, -16, height);
2197 if (src_y == height)
2198 dxy &= ~2;
2199 src += src_y * stride + src_x;
2200
2201 if(s->unrestricted_mv && (s->flags&CODEC_FLAG_EMU_EDGE)){
2202 if( (unsigned)src_x > h_edge_pos - (motion_x&1) - w
2203 || (unsigned)src_y > v_edge_pos - (motion_y&1) - h){
2204 ff_emulated_edge_mc(s->edge_emu_buffer, src, stride, w+1, h+1,
2205 src_x, src_y, h_edge_pos, v_edge_pos);
2206 src= s->edge_emu_buffer;
2207 emu=1;
2208 }
2209 }
2210 pix_op[dxy](dest, src, stride, h);
2211 return emu;
2212 }
2213
2214 /* apply one mpeg motion vector to the three components */
2215 static inline void mpeg_motion(MpegEncContext *s,
2216 uint8_t *dest_y, uint8_t *dest_cb, uint8_t *dest_cr,
2217 int dest_offset,
2218 uint8_t **ref_picture, int src_offset,
2219 int field_based, op_pixels_func (*pix_op)[4],
2220 int motion_x, int motion_y, int h)
2221 {
2222 uint8_t *ptr;
2223 int dxy, offset, mx, my, src_x, src_y, height, v_edge_pos, uvlinesize;
2224 int emu=0;
2225 #if 0
2226 if(s->quarter_sample)
2227 {
2228 motion_x>>=1;
2229 motion_y>>=1;
2230 }
2231 #endif
2232
2233 height = s->height >> field_based;
2234 v_edge_pos = s->v_edge_pos >> field_based;
2235 uvlinesize = s->current_picture.linesize[1] << field_based;
2236
2237 emu= hpel_motion(s,
2238 dest_y + dest_offset, ref_picture[0] + src_offset,
2239 s->mb_x * 16, s->mb_y * (16 >> field_based),
2240 s->width, height, s->current_picture.linesize[0] << field_based,
2241 s->h_edge_pos, v_edge_pos,
2242 16, h, pix_op[0],
2243 motion_x, motion_y);
2244
2245
2246 if(s->flags&CODEC_FLAG_GRAY) return;
2247
2248 if (s->out_format == FMT_H263) {
2249 dxy = 0;
2250 if ((motion_x & 3) != 0)
2251 dxy |= 1;
2252 if ((motion_y & 3) != 0)
2253 dxy |= 2;
2254 mx = motion_x >> 2;
2255 my = motion_y >> 2;
2256 } else {
2257 mx = motion_x / 2;
2258 my = motion_y / 2;
2259 dxy = ((my & 1) << 1) | (mx & 1);
2260 mx >>= 1;
2261 my >>= 1;
2262 }
2263
2264 src_x = s->mb_x * 8 + mx;
2265 src_y = s->mb_y * (8 >> field_based) + my;
2266 src_x = clip(src_x, -8, s->width >> 1);
2267 if (src_x == (s->width >> 1))
2268 dxy &= ~1;
2269 src_y = clip(src_y, -8, height >> 1);
2270 if (src_y == (height >> 1))
2271 dxy &= ~2;
2272 offset = (src_y * uvlinesize) + src_x + (src_offset >> 1);
2273 ptr = ref_picture[1] + offset;
2274 if(emu){
2275 ff_emulated_edge_mc(s->edge_emu_buffer, ptr - (src_offset >> 1), s->uvlinesize, 9, 9+field_based,
2276 src_x, src_y<<field_based, s->h_edge_pos>>1, s->v_edge_pos>>1);
2277 ptr= s->edge_emu_buffer + (src_offset >> 1);
2278 }
2279 pix_op[1][dxy](dest_cb + (dest_offset >> 1), ptr, uvlinesize, h >> 1);
2280
2281 ptr = ref_picture[2] + offset;
2282 if(emu){
2283 ff_emulated_edge_mc(s->edge_emu_buffer, ptr - (src_offset >> 1), s->uvlinesize, 9, 9+field_based,
2284 src_x, src_y<<field_based, s->h_edge_pos>>1, s->v_edge_pos>>1);
2285 ptr= s->edge_emu_buffer + (src_offset >> 1);
2286 }
2287 pix_op[1][dxy](dest_cr + (dest_offset >> 1), ptr, uvlinesize, h >> 1);
2288 }
2289 //FIXME move to dsputil, avg variant, 16x16 version
2290 static inline void put_obmc(uint8_t *dst, uint8_t *src[5], int stride){
2291 int x;
2292 uint8_t * const top = src[1];
2293 uint8_t * const left = src[2];
2294 uint8_t * const mid = src[0];
2295 uint8_t * const right = src[3];
2296 uint8_t * const bottom= src[4];
2297 #define OBMC_FILTER(x, t, l, m, r, b)\
2298 dst[x]= (t*top[x] + l*left[x] + m*mid[x] + r*right[x] + b*bottom[x] + 4)>>3
2299 #define OBMC_FILTER4(x, t, l, m, r, b)\
2300 OBMC_FILTER(x , t, l, m, r, b);\
2301 OBMC_FILTER(x+1 , t, l, m, r, b);\
2302 OBMC_FILTER(x +stride, t, l, m, r, b);\
2303 OBMC_FILTER(x+1+stride, t, l, m, r, b);
2304
2305 x=0;
2306 OBMC_FILTER (x , 2, 2, 4, 0, 0);
2307 OBMC_FILTER (x+1, 2, 1, 5, 0, 0);
2308 OBMC_FILTER4(x+2, 2, 1, 5, 0, 0);
2309 OBMC_FILTER4(x+4, 2, 0, 5, 1, 0);
2310 OBMC_FILTER (x+6, 2, 0, 5, 1, 0);
2311 OBMC_FILTER (x+7, 2, 0, 4, 2, 0);
2312 x+= stride;
2313 OBMC_FILTER (x , 1, 2, 5, 0, 0);
2314 OBMC_FILTER (x+1, 1, 2, 5, 0, 0);
2315 OBMC_FILTER (x+6, 1, 0, 5, 2, 0);
2316 OBMC_FILTER (x+7, 1, 0, 5, 2, 0);
2317 x+= stride;
2318 OBMC_FILTER4(x , 1, 2, 5, 0, 0);
2319 OBMC_FILTER4(x+2, 1, 1, 6, 0, 0);
2320 OBMC_FILTER4(x+4, 1, 0, 6, 1, 0);
2321 OBMC_FILTER4(x+6, 1, 0, 5, 2, 0);
2322 x+= 2*stride;
2323 OBMC_FILTER4(x , 0, 2, 5, 0, 1);
2324 OBMC_FILTER4(x+2, 0, 1, 6, 0, 1);
2325 OBMC_FILTER4(x+4, 0, 0, 6, 1, 1);
2326 OBMC_FILTER4(x+6, 0, 0, 5, 2, 1);
2327 x+= 2*stride;
2328 OBMC_FILTER (x , 0, 2, 5, 0, 1);
2329 OBMC_FILTER (x+1, 0, 2, 5, 0, 1);
2330 OBMC_FILTER4(x+2, 0, 1, 5, 0, 2);
2331 OBMC_FILTER4(x+4, 0, 0, 5, 1, 2);
2332 OBMC_FILTER (x+6, 0, 0, 5, 2, 1);
2333 OBMC_FILTER (x+7, 0, 0, 5, 2, 1);
2334 x+= stride;
2335 OBMC_FILTER (x , 0, 2, 4, 0, 2);
2336 OBMC_FILTER (x+1, 0, 1, 5, 0, 2);
2337 OBMC_FILTER (x+6, 0, 0, 5, 1, 2);
2338 OBMC_FILTER (x+7, 0, 0, 4, 2, 2);
2339 }
2340
2341 /* obmc for 1 8x8 luma block */
2342 static inline void obmc_motion(MpegEncContext *s,
2343 uint8_t *dest, uint8_t *src,
2344 int src_x, int src_y,
2345 op_pixels_func *pix_op,
2346 int16_t mv[5][2]/* mid top left right bottom*/)
2347 #define MID 0
2348 {
2349 int i;
2350 uint8_t *ptr[5];
2351
2352 assert(s->quarter_sample==0);
2353
2354 for(i=0; i<5; i++){
2355 if(i && mv[i][0]==mv[MID][0] && mv[i][1]==mv[MID][1]){
2356 ptr[i]= ptr[MID];
2357 }else{
2358 ptr[i]= s->edge_emu_buffer + 16 + 8*(i&1) + s->linesize*8*(i>>1);
2359 hpel_motion(s, ptr[i], src,
2360 src_x, src_y,
2361 s->width, s->height, s->linesize,
2362 s->h_edge_pos, s->v_edge_pos,
2363 8, 8, pix_op,
2364 mv[i][0], mv[i][1]);
2365 }
2366 }
2367
2368 put_obmc(dest, ptr, s->linesize);
2369 }
2370
2371 static inline void qpel_motion(MpegEncContext *s,
2372 uint8_t *dest_y, uint8_t *dest_cb, uint8_t *dest_cr,
2373 int dest_offset,
2374 uint8_t **ref_picture, int src_offset,
2375 int field_based, op_pixels_func (*pix_op)[4],
2376 qpel_mc_func (*qpix_op)[16],
2377 int motion_x, int motion_y, int h)
2378 {
2379 uint8_t *ptr;
2380 int dxy, offset, mx, my, src_x, src_y, height, v_edge_pos, linesize, uvlinesize;
2381 int emu=0;
2382
2383 dxy = ((motion_y & 3) << 2) | (motion_x & 3);
2384 src_x = s->mb_x * 16 + (motion_x >> 2);
2385 src_y = s->mb_y * (16 >> field_based) + (motion_y >> 2);
2386
2387 height = s->height >> field_based;
2388 v_edge_pos = s->v_edge_pos >> field_based;
2389 src_x = clip(src_x, -16, s->width);
2390 if (src_x == s->width)
2391 dxy &= ~3;
2392 src_y = clip(src_y, -16, height);
2393 if (src_y == height)
2394 dxy &= ~12;
2395 linesize = s->linesize << field_based;
2396 uvlinesize = s->uvlinesize << field_based;
2397 ptr = ref_picture[0] + (src_y * linesize) + src_x + src_offset;
2398 dest_y += dest_offset;
2399 //printf("%d %d %d\n", src_x, src_y, dxy);
2400
2401 if(s->flags&CODEC_FLAG_EMU_EDGE){
2402 if( (unsigned)src_x > s->h_edge_pos - (motion_x&3) - 16
2403 || (unsigned)src_y > v_edge_pos - (motion_y&3) - h ){
2404 ff_emulated_edge_mc(s->edge_emu_buffer, ptr - src_offset, s->linesize, 17, 17+field_based,
2405 src_x, src_y<<field_based, s->h_edge_pos, s->v_edge_pos);
2406 ptr= s->edge_emu_buffer + src_offset;
2407 emu=1;
2408 }
2409 }
2410 if(!field_based)
2411 qpix_op[0][dxy](dest_y, ptr, linesize);
2412 else{
2413 //damn interlaced mode
2414 //FIXME boundary mirroring is not exactly correct here
2415 qpix_op[1][dxy](dest_y , ptr , linesize);
2416 qpix_op[1][dxy](dest_y+8, ptr+8, linesize);
2417 }
2418
2419 if(s->flags&CODEC_FLAG_GRAY) return;
2420
2421 if(field_based){
2422 mx= motion_x/2;
2423 my= motion_y>>1;
2424 }else if(s->workaround_bugs&FF_BUG_QPEL_CHROMA2){
2425 static const int rtab[8]= {0,0,1,1,0,0,0,1};
2426 mx= (motion_x>>1) + rtab[motion_x&7];
2427 my= (motion_y>>1) + rtab[motion_y&7];
2428 }else if(s->workaround_bugs&FF_BUG_QPEL_CHROMA){
2429 mx= (motion_x>>1)|(motion_x&1);
2430 my= (motion_y>>1)|(motion_y&1);
2431 }else{
2432 mx= motion_x/2;
2433 my= motion_y/2;
2434 }
2435 mx= (mx>>1)|(mx&1);
2436 my= (my>>1)|(my&1);
2437
2438 dxy= (mx&1) | ((my&1)<<1);
2439 mx>>=1;
2440 my>>=1;
2441
2442 src_x = s->mb_x * 8 + mx;
2443 src_y = s->mb_y * (8 >> field_based) + my;
2444 src_x = clip(src_x, -8, s->width >> 1);
2445 if (src_x == (s->width >> 1))
2446 dxy &= ~1;
2447 src_y = clip(src_y, -8, height >> 1);
2448 if (src_y == (height >> 1))
2449 dxy &= ~2;
2450
2451 offset = (src_y * uvlinesize) + src_x + (src_offset >> 1);
2452 ptr = ref_picture[1] + offset;
2453 if(emu){
2454 ff_emulated_edge_mc(s->edge_emu_buffer, ptr - (src_offset >> 1), s->uvlinesize, 9, 9 + field_based,
2455 src_x, src_y<<field_based, s->h_edge_pos>>1, s->v_edge_pos>>1);
2456 ptr= s->edge_emu_buffer + (src_offset >> 1);
2457 }
2458 pix_op[1][dxy](dest_cb + (dest_offset >> 1), ptr, uvlinesize, h >> 1);
2459
2460 ptr = ref_picture[2] + offset;
2461 if(emu){
2462 ff_emulated_edge_mc(s->edge_emu_buffer, ptr - (src_offset >> 1), s->uvlinesize, 9, 9 + field_based,
2463 src_x, src_y<<field_based, s->h_edge_pos>>1, s->v_edge_pos>>1);
2464 ptr= s->edge_emu_buffer + (src_offset >> 1);
2465 }
2466 pix_op[1][dxy](dest_cr + (dest_offset >> 1), ptr, uvlinesize, h >> 1);
2467 }
2468
2469 inline int ff_h263_round_chroma(int x){
2470 if (x >= 0)
2471 return (h263_chroma_roundtab[x & 0xf] + ((x >> 3) & ~1));
2472 else {
2473 x = -x;
2474 return -(h263_chroma_roundtab[x & 0xf] + ((x >> 3) & ~1));
2475 }
2476 }
2477
2478 /**
2479 * h263 chorma 4mv motion compensation.
2480 */
2481 static inline void chroma_4mv_motion(MpegEncContext *s,
2482 uint8_t *dest_cb, uint8_t *dest_cr,
2483 uint8_t **ref_picture,
2484 op_pixels_func *pix_op,
2485 int mx, int my){
2486 int dxy, emu=0, src_x, src_y, offset;
2487 uint8_t *ptr;
2488
2489 /* In case of 8X8, we construct a single chroma motion vector
2490 with a special rounding */
2491 mx= ff_h263_round_chroma(mx);
2492 my= ff_h263_round_chroma(my);
2493
2494 dxy = ((my & 1) << 1) | (mx & 1);
2495 mx >>= 1;
2496 my >>= 1;
2497
2498 src_x = s->mb_x * 8 + mx;
2499 src_y = s->mb_y * 8 + my;
2500 src_x = clip(src_x, -8, s->width/2);
2501 if (src_x == s->width/2)
2502 dxy &= ~1;
2503 src_y = clip(src_y, -8, s->height/2);
2504 if (src_y == s->height/2)
2505 dxy &= ~2;
2506
2507 offset = (src_y * (s->uvlinesize)) + src_x;
2508 ptr = ref_picture[1] + offset;
2509 if(s->flags&CODEC_FLAG_EMU_EDGE){
2510 if( (unsigned)src_x > (s->h_edge_pos>>1) - (dxy &1) - 8
2511 || (unsigned)src_y > (s->v_edge_pos>>1) - (dxy>>1) - 8){
2512 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);
2513 ptr= s->edge_emu_buffer;
2514 emu=1;
2515 }
2516 }
2517 pix_op[dxy](dest_cb, ptr, s->uvlinesize, 8);
2518
2519 ptr = ref_picture[2] + offset;
2520 if(emu){
2521 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);
2522 ptr= s->edge_emu_buffer;
2523 }
2524 pix_op[dxy](dest_cr, ptr, s->uvlinesize, 8);
2525 }
2526
2527 /**
2528 * motion compesation of a single macroblock
2529 * @param s context
2530 * @param dest_y luma destination pointer
2531 * @param dest_cb chroma cb/u destination pointer
2532 * @param dest_cr chroma cr/v destination pointer
2533 * @param dir direction (0->forward, 1->backward)
2534 * @param ref_picture array[3] of pointers to the 3 planes of the reference picture
2535 * @param pic_op halfpel motion compensation function (average or put normally)
2536 * @param pic_op qpel motion compensation function (average or put normally)
2537 * the motion vectors are taken from s->mv and the MV type from s->mv_type
2538 */
2539 static inline void MPV_motion(MpegEncContext *s,
2540 uint8_t *dest_y, uint8_t *dest_cb, uint8_t *dest_cr,
2541 int dir, uint8_t **ref_picture,
2542 op_pixels_func (*pix_op)[4], qpel_mc_func (*qpix_op)[16])
2543 {
2544 int dxy, mx, my, src_x, src_y, motion_x, motion_y;
2545 int mb_x, mb_y, i;
2546 uint8_t *ptr, *dest;
2547
2548 mb_x = s->mb_x;
2549 mb_y = s->mb_y;
2550
2551 if(s->obmc && s->pict_type != B_TYPE){
2552 int16_t mv_cache[4][4][2];
2553 const int xy= s->mb_x + s->mb_y*s->mb_stride;
2554 const int mot_stride= s->mb_width*2 + 2;
2555 const int mot_xy= 1 + mb_x*2 + (mb_y*2 + 1)*mot_stride;
2556
2557 assert(!s->mb_skiped);
2558
2559 memcpy(mv_cache[1][1], s->current_picture.motion_val[0][mot_xy ], sizeof(int16_t)*4);
2560 memcpy(mv_cache[2][1], s->current_picture.motion_val[0][mot_xy+mot_stride], sizeof(int16_t)*4);
2561 memcpy(mv_cache[3][1], s->current_picture.motion_val[0][mot_xy+mot_stride], sizeof(int16_t)*4);
2562
2563 if(mb_y==0 || IS_INTRA(s->current_picture.mb_type[xy-s->mb_stride])){
2564 memcpy(mv_cache[0][1], mv_cache[1][1], sizeof(int16_t)*4);
2565 }else{
2566 memcpy(mv_cache[0][1], s->current_picture.motion_val[0][mot_xy-mot_stride], sizeof(int16_t)*4);
2567 }
2568
2569 if(mb_x==0 || IS_INTRA(s->current_picture.mb_type[xy-1])){
2570 *(int32_t*)mv_cache[1][0]= *(int32_t*)mv_cache[1][1];
2571 *(int32_t*)mv_cache[2][0]= *(int32_t*)mv_cache[2][1];
2572 }else{
2573 *(int32_t*)mv_cache[1][0]= *(int32_t*)s->current_picture.motion_val[0][mot_xy-1];
2574 *(int32_t*)mv_cache[2][0]= *(int32_t*)s->current_picture.motion_val[0][mot_xy-1+mot_stride];
2575 }
2576
2577 if(mb_x+1>=s->mb_width || IS_INTRA(s->current_picture.mb_type[xy+1])){
2578 *(int32_t*)mv_cache[1][3]= *(int32_t*)mv_cache[1][2];
2579 *(int32_t*)mv_cache[2][3]= *(int32_t*)mv_cache[2][2];
2580 }else{
2581 *(int32_t*)mv_cache[1][3]= *(int32_t*)s->current_picture.motion_val[0][mot_xy+2];
2582 *(int32_t*)mv_cache[2][3]= *(int32_t*)s->current_picture.motion_val[0][mot_xy+2+mot_stride];
2583 }
2584
2585 mx = 0;
2586 my = 0;
2587 for(i=0;i<4;i++) {
2588 const int x= (i&1)+1;
2589 const int y= (i>>1)+1;
2590 int16_t mv[5][2]= {
2591 {mv_cache[y][x ][0], mv_cache[y][x ][1]},
2592 {mv_cache[y-1][x][0], mv_cache[y-1][x][1]},
2593 {mv_cache[y][x-1][0], mv_cache[y][x-1][1]},
2594 {mv_cache[y][x+1][0], mv_cache[y][x+1][1]},
2595 {mv_cache[y+1][x][0], mv_cache[y+1][x][1]}};
2596 //FIXME cleanup
2597 obmc_motion(s, dest_y + ((i & 1) * 8) + (i >> 1) * 8 * s->linesize,
2598 ref_picture[0],
2599 mb_x * 16 + (i & 1) * 8, mb_y * 16 + (i >>1) * 8,
2600 pix_op[1],
2601 mv);
2602
2603 mx += mv[0][0];
2604 my += mv[0][1];
2605 }
2606 if(!(s->flags&CODEC_FLAG_GRAY))
2607 chroma_4mv_motion(s, dest_cb, dest_cr, ref_picture, pix_op[1], mx, my);
2608
2609 return;
2610 }
2611
2612 switch(s->mv_type) {
2613 case MV_TYPE_16X16:
2614 #ifdef CONFIG_RISKY
2615 if(s->mcsel){
2616 if(s->real_sprite_warping_points==1){
2617 gmc1_motion(s, dest_y, dest_cb, dest_cr, 0,
2618 ref_picture, 0);
2619 }else{
2620 gmc_motion(s, dest_y, dest_cb, dest_cr, 0,
2621 ref_picture, 0);
2622 }
2623 }else if(s->quarter_sample){
2624 qpel_motion(s, dest_y, dest_cb, dest_cr, 0,
2625 ref_picture, 0,
2626 0, pix_op, qpix_op,
2627 s->mv[dir][0][0], s->mv[dir][0][1], 16);
2628 }else if(s->mspel){
2629 ff_mspel_motion(s, dest_y, dest_cb, dest_cr,
2630 ref_picture, pix_op,
2631 s->mv[dir][0][0], s->mv[dir][0][1], 16);
2632 }else
2633 #endif
2634 {
2635 mpeg_motion(s, dest_y, dest_cb, dest_cr, 0,
2636 ref_picture, 0,
2637 0, pix_op,
2638 s->mv[dir][0][0], s->mv[dir][0][1], 16);
2639 }
2640 break;
2641 case MV_TYPE_8X8:
2642 mx = 0;
2643 my = 0;
2644 if(s->quarter_sample){
2645 for(i=0;i<4;i++) {
2646 motion_x = s->mv[dir][i][0];
2647 motion_y = s->mv[dir][i][1];
2648
2649 dxy = ((motion_y & 3) << 2) | (motion_x & 3);
2650 src_x = mb_x * 16 + (motion_x >> 2) + (i & 1) * 8;
2651 src_y = mb_y * 16 + (motion_y >> 2) + (i >>1) * 8;
2652
2653 /* WARNING: do no forget half pels */
2654 src_x = clip(src_x, -16, s->width);
2655 if (src_x == s->width)
2656 dxy &= ~3;
2657 src_y = clip(src_y, -16, s->height);
2658 if (src_y == s->height)
2659 dxy &= ~12;
2660
2661 ptr = ref_picture[0] + (src_y * s->linesize) + (src_x);
2662 if(s->flags&CODEC_FLAG_EMU_EDGE){
2663 if( (unsigned)src_x > s->h_edge_pos - (motion_x&3) - 8
2664 || (unsigned)src_y > s->v_edge_pos - (motion_y&3) - 8 ){
2665 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);
2666 ptr= s->edge_emu_buffer;
2667 }
2668 }
2669 dest = dest_y + ((i & 1) * 8) + (i >> 1) * 8 * s->linesize;
2670 qpix_op[1][dxy](dest, ptr, s->linesize);
2671
2672 mx += s->mv[dir][i][0]/2;
2673 my += s->mv[dir][i][1]/2;
2674 }
2675 }else{
2676 for(i=0;i<4;i++) {
2677 hpel_motion(s, dest_y + ((i & 1) * 8) + (i >> 1) * 8 * s->linesize,
2678 ref_picture[0],
2679 mb_x * 16 + (i & 1) * 8, mb_y * 16 + (i >>1) * 8,
2680 s->width, s->height, s->linesize,
2681 s->h_edge_pos, s->v_edge_pos,
2682 8, 8, pix_op[1],
2683 s->mv[dir][i][0], s->mv[dir][i][1]);
2684
2685 mx += s->mv[dir][i][0];
2686 my += s->mv[dir][i][1];
2687 }
2688 }
2689
2690 if(!(s->flags&CODEC_FLAG_GRAY))
2691 chroma_4mv_motion(s, dest_cb, dest_cr, ref_picture, pix_op[1], mx, my);
2692 break;
2693 case MV_TYPE_FIELD:
2694 if (s->picture_structure == PICT_FRAME) {
2695 if(s->quarter_sample){
2696 /* top field */
2697 qpel_motion(s, dest_y, dest_cb, dest_cr, 0,
2698 ref_picture, s->field_select[dir][0] ? s->linesize : 0,
2699 1, pix_op, qpix_op,
2700 s->mv[dir][0][0], s->mv[dir][0][1], 8);
2701 /* bottom field */
2702 qpel_motion(s, dest_y, dest_cb, dest_cr, s->linesize,
2703 ref_picture, s->field_select[dir][1] ? s->linesize : 0,
2704 1, pix_op, qpix_op,
2705 s->mv[dir][1][0], s->mv[dir][1][1], 8);
2706 }else{
2707 /* top field */
2708 mpeg_motion(s, dest_y, dest_cb, dest_cr, 0,
2709 ref_picture, s->field_select[dir][0] ? s->linesize : 0,
2710 1, pix_op,
2711 s->mv[dir][0][0], s->mv[dir][0][1], 8);
2712 /* bottom field */
2713 mpeg_motion(s, dest_y, dest_cb, dest_cr, s->linesize,
2714 ref_picture, s->field_select[dir][1] ? s->linesize : 0,
2715 1, pix_op,
2716 s->mv[dir][1][0], s->mv[dir][1][1], 8);
2717 }
2718 } else {
2719 int offset;
2720 if(s->picture_structure == s->field_select[dir][0] + 1 || s->pict_type == B_TYPE || s->first_field){
2721 offset= s->field_select[dir][0] ? s->linesize : 0;
2722 }else{
2723 ref_picture= s->current_picture.data;
2724 offset= s->field_select[dir][0] ? s->linesize : -s->linesize;
2725 }
2726
2727 mpeg_motion(s, dest_y, dest_cb, dest_cr, 0,
2728 ref_picture, offset,
2729 0, pix_op,
2730 s->mv[dir][0][0], s->mv[dir][0][1], 16);
2731 }
2732 break;
2733 case MV_TYPE_16X8:{
2734 int offset;
2735 uint8_t ** ref2picture;
2736
2737 if(s->picture_structure == s->field_select[dir][0] + 1 || s->pict_type == B_TYPE || s->first_field){
2738 ref2picture= ref_picture;
2739 offset= s->field_select[dir][0] ? s->linesize : 0;
2740 }else{
2741 ref2picture= s->current_picture.data;
2742 offset= s->field_select[dir][0] ? s->linesize : -s->linesize;
2743 }
2744
2745 mpeg_motion(s, dest_y, dest_cb, dest_cr, 0,
2746 ref2picture, offset,
2747 0, pix_op,
2748 s->mv[dir][0][0], s->mv[dir][0][1], 8);
2749
2750
2751 if(s->picture_structure == s->field_select[dir][1] + 1 || s->pict_type == B_TYPE || s->first_field){