cleaner & more flexible edge bug workaround
[libav.git] / libavcodec / mpegvideo.c
CommitLineData
de6d9b64
FB
1/*
2 * The simplest mpeg encoder (well, it was the simplest!)
ff4ec49e 3 * Copyright (c) 2000,2001 Fabrice Bellard.
de6d9b64 4 *
ff4ec49e
FB
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.
de6d9b64 9 *
ff4ec49e 10 * This library is distributed in the hope that it will be useful,
de6d9b64 11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
ff4ec49e
FB
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
de6d9b64 14 *
ff4ec49e
FB
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
cf8039b2 18 *
9dbf1ddd 19 * 4MV & hq & b-frame encoding stuff by Michael Niedermayer <michaelni@gmx.at>
de6d9b64 20 */
202ef8b8 21
983e3246
MN
22/**
23 * @file mpegvideo.c
24 * The simplest mpeg encoder (well, it was the simplest!).
25 */
26
202ef8b8 27#include <ctype.h>
f943e138 28#include <limits.h>
de6d9b64
FB
29#include "avcodec.h"
30#include "dsputil.h"
31#include "mpegvideo.h"
32
54329dd5
NK
33#ifdef USE_FASTMEMCPY
34#include "fastmemcpy.h"
35#endif
36
2ad1516a
MN
37//#undef NDEBUG
38//#include <assert.h>
39
7604246d 40#ifdef CONFIG_ENCODERS
21af69f7 41static void encode_picture(MpegEncContext *s, int picture_number);
7604246d 42#endif //CONFIG_ENCODERS
21af69f7
FB
43static void dct_unquantize_mpeg1_c(MpegEncContext *s,
44 DCTELEM *block, int n, int qscale);
9dbf1ddd
MN
45static void dct_unquantize_mpeg2_c(MpegEncContext *s,
46 DCTELEM *block, int n, int qscale);
21af69f7
FB
47static void dct_unquantize_h263_c(MpegEncContext *s,
48 DCTELEM *block, int n, int qscale);
0c1a9eda 49static void draw_edges_c(uint8_t *buf, int wrap, int width, int height, int w);
7604246d 50#ifdef CONFIG_ENCODERS
d7e9533a 51static int dct_quantize_c(MpegEncContext *s, DCTELEM *block, int n, int qscale, int *overflow);
477ab036 52static int dct_quantize_trellis_c(MpegEncContext *s, DCTELEM *block, int n, int qscale, int *overflow);
7604246d 53#endif //CONFIG_ENCODERS
3d9fccbf 54
0c1a9eda 55void (*draw_edges)(uint8_t *buf, int wrap, int width, int height, int w)= draw_edges_c;
3d9fccbf 56
de6d9b64
FB
57
58/* enable all paranoid tests for rounding, overflows, etc... */
59//#define PARANOID
60
61//#define DEBUG
62
101bea5f 63
de6d9b64
FB
64/* for jpeg fast DCT */
65#define CONST_BITS 14
66
eb4b3dd3 67static const uint16_t aanscales[64] = {
de6d9b64
FB
68 /* precomputed values scaled up by 14 bits */
69 16384, 22725, 21407, 19266, 16384, 12873, 8867, 4520,
70 22725, 31521, 29692, 26722, 22725, 17855, 12299, 6270,
71 21407, 29692, 27969, 25172, 21407, 16819, 11585, 5906,
72 19266, 26722, 25172, 22654, 19266, 15137, 10426, 5315,
73 16384, 22725, 21407, 19266, 16384, 12873, 8867, 4520,
74 12873, 17855, 16819, 15137, 12873, 10114, 6967, 3552,
1457ab52
MN
75 8867 , 12299, 11585, 10426, 8867, 6967, 4799, 2446,
76 4520 , 6270, 5906, 5315, 4520, 3552, 2446, 1247
de6d9b64
FB
77};
78
eb4b3dd3 79static const uint8_t h263_chroma_roundtab[16] = {
67725183 80// 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
de6d9b64
FB
81 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2,
82};
83
7604246d 84#ifdef CONFIG_ENCODERS
0c1a9eda
ZK
85static uint16_t (*default_mv_penalty)[MAX_MV*2+1]=NULL;
86static uint8_t default_fcode_tab[MAX_MV*2+1];
45870f57 87
a33c7159
MN
88enum PixelFormat ff_yuv420p_list[2]= {PIX_FMT_YUV420P, -1};
89
28db7fce 90static void convert_matrix(MpegEncContext *s, int (*qmat)[64], uint16_t (*qmat16)[64], uint16_t (*qmat16_bias)[64],
0c1a9eda 91 const uint16_t *quant_matrix, int bias, int qmin, int qmax)
de6d9b64 92{
d7e9533a
MN
93 int qscale;
94
cc7ac888 95 for(qscale=qmin; qscale<=qmax; qscale++){
d7e9533a 96 int i;
b0368839 97 if (s->dsp.fdct == ff_jpeg_fdct_islow) {
28db7fce 98 for(i=0;i<64;i++) {
b0368839 99 const int j= s->dsp.idct_permutation[i];
28db7fce
MN
100 /* 16 <= qscale * quant_matrix[i] <= 7905 */
101 /* 19952 <= aanscales[i] * qscale * quant_matrix[i] <= 249205026 */
102 /* (1<<36)/19952 >= (1<<36)/(aanscales[i] * qscale * quant_matrix[i]) >= (1<<36)/249205026 */
103 /* 3444240 >= (1<<36)/(aanscales[i] * qscale * quant_matrix[i]) >= 275 */
104
0c1a9eda 105 qmat[qscale][i] = (int)((uint64_t_C(1) << QMAT_SHIFT) /
28db7fce
MN
106 (qscale * quant_matrix[j]));
107 }
b0368839 108 } else if (s->dsp.fdct == fdct_ifast) {
d7e9533a 109 for(i=0;i<64;i++) {
b0368839 110 const int j= s->dsp.idct_permutation[i];
d7e9533a
MN
111 /* 16 <= qscale * quant_matrix[i] <= 7905 */
112 /* 19952 <= aanscales[i] * qscale * quant_matrix[i] <= 249205026 */
113 /* (1<<36)/19952 >= (1<<36)/(aanscales[i] * qscale * quant_matrix[i]) >= (1<<36)/249205026 */
114 /* 3444240 >= (1<<36)/(aanscales[i] * qscale * quant_matrix[i]) >= 275 */
115
0c1a9eda 116 qmat[qscale][i] = (int)((uint64_t_C(1) << (QMAT_SHIFT + 14)) /
d7e9533a
MN
117 (aanscales[i] * qscale * quant_matrix[j]));
118 }
119 } else {
120 for(i=0;i<64;i++) {
b0368839 121 const int j= s->dsp.idct_permutation[i];
d7e9533a
MN
122 /* We can safely suppose that 16 <= quant_matrix[i] <= 255
123 So 16 <= qscale * quant_matrix[i] <= 7905
124 so (1<<19) / 16 >= (1<<19) / (qscale * quant_matrix[i]) >= (1<<19) / 7905
125 so 32768 >= (1<<19) / (qscale * quant_matrix[i]) >= 67
126 */
0c1a9eda 127 qmat[qscale][i] = (int)((uint64_t_C(1) << QMAT_SHIFT) / (qscale * quant_matrix[j]));
477ab036 128// qmat [qscale][i] = (1 << QMAT_SHIFT_MMX) / (qscale * quant_matrix[i]);
2ad1516a 129 qmat16[qscale][i] = (1 << QMAT_SHIFT_MMX) / (qscale * quant_matrix[j]);
d7e9533a
MN
130
131 if(qmat16[qscale][i]==0 || qmat16[qscale][i]==128*256) qmat16[qscale][i]=128*256-1;
d7e9533a
MN
132 qmat16_bias[qscale][i]= ROUNDED_DIV(bias<<(16-QUANT_BIAS_SHIFT), qmat16[qscale][i]);
133 }
de6d9b64
FB
134 }
135 }
136}
7604246d
WH
137#endif //CONFIG_ENCODERS
138
7f2fe444
MN
139// move into common.c perhaps
140#define CHECKED_ALLOCZ(p, size)\
141{\
142 p= av_mallocz(size);\
143 if(p==NULL){\
144 perror("malloc");\
145 goto fail;\
146 }\
147}
4d2858de 148
0c1a9eda 149void ff_init_scantable(MpegEncContext *s, ScanTable *st, const uint8_t *src_scantable){
2ad1516a
MN
150 int i;
151 int end;
7801d21d
MN
152
153 st->scantable= src_scantable;
2ad1516a
MN
154
155 for(i=0; i<64; i++){
156 int j;
157 j = src_scantable[i];
b0368839 158 st->permutated[i] = s->dsp.idct_permutation[j];
05c4072b
MN
159#ifdef ARCH_POWERPC
160 st->inverse[j] = i;
161#endif
2ad1516a
MN
162 }
163
164 end=-1;
165 for(i=0; i<64; i++){
166 int j;
167 j = st->permutated[i];
168 if(j>end) end=j;
169 st->raster_end[i]= end;
170 }
171}
172
defdfc9a
AB
173/* init common dct for both encoder and decoder */
174int DCT_common_init(MpegEncContext *s)
de6d9b64 175{
3bf43d42 176 s->dct_unquantize_h263 = dct_unquantize_h263_c;
9dbf1ddd
MN
177 s->dct_unquantize_mpeg1 = dct_unquantize_mpeg1_c;
178 s->dct_unquantize_mpeg2 = dct_unquantize_mpeg2_c;
b0368839 179
7604246d 180#ifdef CONFIG_ENCODERS
28db7fce 181 s->dct_quantize= dct_quantize_c;
b0368839 182#endif
21af69f7
FB
183
184#ifdef HAVE_MMX
185 MPV_common_init_mmx(s);
a9b3f630 186#endif
e0580f8c
FH
187#ifdef ARCH_ALPHA
188 MPV_common_init_axp(s);
189#endif
c7e07931
MO
190#ifdef HAVE_MLIB
191 MPV_common_init_mlib(s);
192#endif
5917d17c
LS
193#ifdef HAVE_MMI
194 MPV_common_init_mmi(s);
195#endif
676e200c 196#ifdef ARCH_ARMV4L
83f238cb 197 MPV_common_init_armv4l(s);
676e200c 198#endif
05c4072b
MN
199#ifdef ARCH_POWERPC
200 MPV_common_init_ppc(s);
201#endif
676e200c 202
7604246d 203#ifdef CONFIG_ENCODERS
3a87ac94
MN
204 s->fast_dct_quantize= s->dct_quantize;
205
477ab036
MN
206 if(s->flags&CODEC_FLAG_TRELLIS_QUANT){
207 s->dct_quantize= dct_quantize_trellis_c; //move before MPV_common_init_*
208 }
209
7604246d
WH
210#endif //CONFIG_ENCODERS
211
2ad1516a
MN
212 /* load & permutate scantables
213 note: only wmv uses differnt ones
214 */
215 ff_init_scantable(s, &s->inter_scantable , ff_zigzag_direct);
216 ff_init_scantable(s, &s->intra_scantable , ff_zigzag_direct);
217 ff_init_scantable(s, &s->intra_h_scantable, ff_alternate_horizontal_scan);
218 ff_init_scantable(s, &s->intra_v_scantable, ff_alternate_vertical_scan);
d930ef19 219
dfb476cb
MN
220 s->picture_structure= PICT_FRAME;
221
defdfc9a
AB
222 return 0;
223}
224
1e491e29 225/**
4e00e76b
MN
226 * allocates a Picture
227 * The pixels are allocated/set by calling get_buffer() if shared=0
1e491e29 228 */
4e00e76b
MN
229static int alloc_picture(MpegEncContext *s, Picture *pic, int shared){
230
231 if(shared){
232 assert(pic->data[0]);
233 assert(pic->type == 0 || pic->type == FF_BUFFER_TYPE_SHARED);
234 pic->type= FF_BUFFER_TYPE_SHARED;
235 }else{
236 int r;
237
238 assert(!pic->data[0]);
239
492cd3a9 240 r= s->avctx->get_buffer(s->avctx, (AVFrame*)pic);
4e00e76b
MN
241
242 if(r<0 || !pic->age || !pic->type || !pic->data[0]){
ef9f7306 243 fprintf(stderr, "get_buffer() failed (%d %d %d %p)\n", r, pic->age, pic->type, pic->data[0]);
4e00e76b
MN
244 return -1;
245 }
246
247 if(s->linesize && (s->linesize != pic->linesize[0] || s->uvlinesize != pic->linesize[1])){
248 fprintf(stderr, "get_buffer() failed (stride changed)\n");
249 return -1;
250 }
251
252 if(pic->linesize[1] != pic->linesize[2]){
253 fprintf(stderr, "get_buffer() failed (uv stride missmatch)\n");
254 return -1;
255 }
256
257 s->linesize = pic->linesize[0];
258 s->uvlinesize= pic->linesize[1];
1e491e29 259 }
4e00e76b
MN
260
261 if(pic->qscale_table==NULL){
262 if (s->encoding) {
0c1a9eda
ZK
263 CHECKED_ALLOCZ(pic->mb_var , s->mb_num * sizeof(int16_t))
264 CHECKED_ALLOCZ(pic->mc_mb_var, s->mb_num * sizeof(int16_t))
265 CHECKED_ALLOCZ(pic->mb_mean , s->mb_num * sizeof(int8_t))
67725183 266 CHECKED_ALLOCZ(pic->mb_cmp_score, s->mb_num * sizeof(int32_t))
4e00e76b 267 }
1e491e29 268
0c1a9eda
ZK
269 CHECKED_ALLOCZ(pic->mbskip_table , s->mb_num * sizeof(uint8_t)+1) //the +1 is for the slice end check
270 CHECKED_ALLOCZ(pic->qscale_table , s->mb_num * sizeof(uint8_t))
4e00e76b
MN
271 pic->qstride= s->mb_width;
272 }
1e491e29 273
f943e138
MN
274 //it might be nicer if the application would keep track of these but it would require a API change
275 memmove(s->prev_pict_types+1, s->prev_pict_types, PREV_PICT_TYPES_BUFFER_SIZE-1);
276 s->prev_pict_types[0]= s->pict_type;
277 if(pic->age < PREV_PICT_TYPES_BUFFER_SIZE && s->prev_pict_types[pic->age] == B_TYPE)
278 pic->age= INT_MAX; // skiped MBs in b frames are quite rare in mpeg1/2 and its a bit tricky to skip them anyway
279
1e491e29
MN
280 return 0;
281fail: //for the CHECKED_ALLOCZ macro
282 return -1;
283}
284
4e00e76b
MN
285/**
286 * deallocates a picture
287 */
1e491e29
MN
288static void free_picture(MpegEncContext *s, Picture *pic){
289 int i;
4e00e76b
MN
290
291 if(pic->data[0] && pic->type!=FF_BUFFER_TYPE_SHARED){
492cd3a9 292 s->avctx->release_buffer(s->avctx, (AVFrame*)pic);
4e00e76b
MN
293 }
294
1e491e29
MN
295 av_freep(&pic->mb_var);
296 av_freep(&pic->mc_mb_var);
297 av_freep(&pic->mb_mean);
67725183 298 av_freep(&pic->mb_cmp_score);
1e491e29
MN
299 av_freep(&pic->mbskip_table);
300 av_freep(&pic->qscale_table);
301
4e00e76b 302 if(pic->type == FF_BUFFER_TYPE_INTERNAL){
1e491e29
MN
303 for(i=0; i<4; i++){
304 av_freep(&pic->base[i]);
305 pic->data[i]= NULL;
306 }
307 av_freep(&pic->opaque);
4e00e76b
MN
308 pic->type= 0;
309 }else if(pic->type == FF_BUFFER_TYPE_SHARED){
310 for(i=0; i<4; i++){
311 pic->base[i]=
312 pic->data[i]= NULL;
313 }
314 pic->type= 0;
1e491e29
MN
315 }
316}
317
defdfc9a
AB
318/* init common structure for both encoder and decoder */
319int MPV_common_init(MpegEncContext *s)
320{
8b32880c 321 int y_size, c_size, yc_size, i;
defdfc9a 322
b0368839 323 dsputil_init(&s->dsp, s->avctx);
defdfc9a 324 DCT_common_init(s);
eb4b3dd3 325
9fee1e23 326 s->flags= s->avctx->flags;
defdfc9a 327
1e491e29 328 s->mb_width = (s->width + 15) / 16;
de6d9b64 329 s->mb_height = (s->height + 15) / 16;
eb4b3dd3 330
b5a093b3
MN
331 /* set default edge pos, will be overriden in decode_header if needed */
332 s->h_edge_pos= s->mb_width*16;
333 s->v_edge_pos= s->mb_height*16;
eb4b3dd3
ZK
334
335 s->mb_num = s->mb_width * s->mb_height;
336
337 y_size = (2 * s->mb_width + 2) * (2 * s->mb_height + 2);
338 c_size = (s->mb_width + 2) * (s->mb_height + 2);
339 yc_size = y_size + 2 * c_size;
340
202ef8b8 341 /* convert fourcc to upper case */
7004ffb3
MN
342 s->avctx->codec_tag= toupper( s->avctx->codec_tag &0xFF)
343 + (toupper((s->avctx->codec_tag>>8 )&0xFF)<<8 )
344 + (toupper((s->avctx->codec_tag>>16)&0xFF)<<16)
345 + (toupper((s->avctx->codec_tag>>24)&0xFF)<<24);
b5a093b3 346
f7b47594
MN
347 CHECKED_ALLOCZ(s->allocated_edge_emu_buffer, (s->width+64)*2*17*2); //(width + edge + align)*interlaced*MBsize*tolerance
348 s->edge_emu_buffer= s->allocated_edge_emu_buffer + (s->width+64)*2*17;
1e491e29 349
492cd3a9 350 s->avctx->coded_frame= (AVFrame*)&s->current_picture;
1e491e29 351
37fbfd0a 352 if (s->encoding) {
9dbcbd92 353 int mv_table_size= (s->mb_width+2)*(s->mb_height+2);
9dbcbd92
MN
354
355 /* Allocate MV tables */
0c1a9eda
ZK
356 CHECKED_ALLOCZ(s->p_mv_table , mv_table_size * 2 * sizeof(int16_t))
357 CHECKED_ALLOCZ(s->b_forw_mv_table , mv_table_size * 2 * sizeof(int16_t))
358 CHECKED_ALLOCZ(s->b_back_mv_table , mv_table_size * 2 * sizeof(int16_t))
359 CHECKED_ALLOCZ(s->b_bidir_forw_mv_table , mv_table_size * 2 * sizeof(int16_t))
360 CHECKED_ALLOCZ(s->b_bidir_back_mv_table , mv_table_size * 2 * sizeof(int16_t))
361 CHECKED_ALLOCZ(s->b_direct_mv_table , mv_table_size * 2 * sizeof(int16_t))
7f2fe444 362
1e491e29 363 //FIXME should be linesize instead of s->width*2 but that isnt known before get_buffer()
1457ab52 364 CHECKED_ALLOCZ(s->me.scratchpad, s->width*2*16*3*sizeof(uint8_t))
7f2fe444 365
1457ab52
MN
366 CHECKED_ALLOCZ(s->me.map , ME_MAP_SIZE*sizeof(uint32_t))
367 CHECKED_ALLOCZ(s->me.score_map, ME_MAP_SIZE*sizeof(uint32_t))
91029be7 368
7f2fe444
MN
369 if(s->codec_id==CODEC_ID_MPEG4){
370 CHECKED_ALLOCZ(s->tex_pb_buffer, PB_BUFFER_SIZE);
371 CHECKED_ALLOCZ( s->pb2_buffer, PB_BUFFER_SIZE);
372 }
3aa102be 373
6b460aa3
MN
374 if(s->msmpeg4_version){
375 CHECKED_ALLOCZ(s->ac_stats, 2*2*(MAX_LEVEL+1)*(MAX_RUN+1)*2*sizeof(int));
376 }
3aa102be 377 CHECKED_ALLOCZ(s->avctx->stats_out, 256);
37fbfd0a 378 }
4d2858de 379
0c1a9eda 380 CHECKED_ALLOCZ(s->error_status_table, s->mb_num*sizeof(uint8_t))
37fbfd0a 381
174489bd 382 if (s->out_format == FMT_H263 || s->encoding) {
de6d9b64 383 int size;
7f2fe444 384 /* Allocate MB type table */
0c1a9eda 385 CHECKED_ALLOCZ(s->mb_type , s->mb_num * sizeof(uint8_t))
7f2fe444 386
de6d9b64
FB
387 /* MV prediction */
388 size = (2 * s->mb_width + 2) * (2 * s->mb_height + 2);
0c1a9eda 389 CHECKED_ALLOCZ(s->motion_val, size * 2 * sizeof(int16_t));
bea669e5
MN
390 }
391
392 if(s->codec_id==CODEC_ID_MPEG4){
4d2858de 393 /* interlaced direct mode decoding tables */
0c1a9eda
ZK
394 CHECKED_ALLOCZ(s->field_mv_table, s->mb_num*2*2 * sizeof(int16_t))
395 CHECKED_ALLOCZ(s->field_select_table, s->mb_num*2* sizeof(int8_t))
de6d9b64 396 }
4d2858de
MN
397 /* 4mv b frame decoding table */
398 //note this is needed for h263 without b frames too (segfault on damaged streams otherwise)
0c1a9eda 399 CHECKED_ALLOCZ(s->co_located_type_table, s->mb_num * sizeof(uint8_t))
6e2d5f1a 400 if (s->out_format == FMT_H263) {
de6d9b64 401 /* ac values */
0c1a9eda 402 CHECKED_ALLOCZ(s->ac_val[0], yc_size * sizeof(int16_t) * 16);
de6d9b64
FB
403 s->ac_val[1] = s->ac_val[0] + y_size;
404 s->ac_val[2] = s->ac_val[1] + c_size;
405
406 /* cbp values */
7f2fe444 407 CHECKED_ALLOCZ(s->coded_block, y_size);
eec1c6b9
MN
408
409 /* divx501 bitstream reorder buffer */
7f2fe444 410 CHECKED_ALLOCZ(s->bitstream_buffer, BITSTREAM_BUFFER_SIZE);
f01a1894 411
7f2fe444 412 /* cbp, ac_pred, pred_dir */
0c1a9eda
ZK
413 CHECKED_ALLOCZ(s->cbp_table , s->mb_num * sizeof(uint8_t))
414 CHECKED_ALLOCZ(s->pred_dir_table, s->mb_num * sizeof(uint8_t))
5b3438c6 415 }
8b32880c
MN
416
417 if (s->h263_pred || s->h263_plus || !s->encoding) {
418 /* dc values */
419 //MN: we need these for error resilience of intra-frames
0c1a9eda 420 CHECKED_ALLOCZ(s->dc_val[0], yc_size * sizeof(int16_t));
8b32880c
MN
421 s->dc_val[1] = s->dc_val[0] + y_size;
422 s->dc_val[2] = s->dc_val[1] + c_size;
423 for(i=0;i<yc_size;i++)
424 s->dc_val[0][i] = 1024;
425 }
426
7806197d
MN
427 /* which mb is a intra block */
428 CHECKED_ALLOCZ(s->mbintra_table, s->mb_num);
429 memset(s->mbintra_table, 1, s->mb_num);
430
de6d9b64
FB
431 /* default structure is frame */
432 s->picture_structure = PICT_FRAME;
93a21abd 433
3bb4e23a 434 /* init macroblock skip table */
4d2858de
MN
435 CHECKED_ALLOCZ(s->mbskip_table, s->mb_num+1);
436 //Note the +1 is for a quicker mpeg4 slice_end detection
f943e138 437 CHECKED_ALLOCZ(s->prev_pict_types, PREV_PICT_TYPES_BUFFER_SIZE);
ba6802de 438
91029be7 439 s->block= s->blocks[0];
3bb4e23a 440
d7425f59
MN
441 s->parse_context.state= -1;
442
de6d9b64
FB
443 s->context_initialized = 1;
444 return 0;
445 fail:
8257bf05 446 MPV_common_end(s);
de6d9b64
FB
447 return -1;
448}
449
7f2fe444
MN
450
451//extern int sads;
452
de6d9b64
FB
453/* init common structure for both encoder and decoder */
454void MPV_common_end(MpegEncContext *s)
455{
456 int i;
457
6000abfa 458 av_freep(&s->mb_type);
6000abfa 459 av_freep(&s->p_mv_table);
6000abfa
FB
460 av_freep(&s->b_forw_mv_table);
461 av_freep(&s->b_back_mv_table);
462 av_freep(&s->b_bidir_forw_mv_table);
463 av_freep(&s->b_bidir_back_mv_table);
6000abfa
FB
464 av_freep(&s->b_direct_mv_table);
465 av_freep(&s->motion_val);
466 av_freep(&s->dc_val[0]);
467 av_freep(&s->ac_val[0]);
468 av_freep(&s->coded_block);
469 av_freep(&s->mbintra_table);
7f2fe444
MN
470 av_freep(&s->cbp_table);
471 av_freep(&s->pred_dir_table);
1457ab52
MN
472 av_freep(&s->me.scratchpad);
473 av_freep(&s->me.map);
474 av_freep(&s->me.score_map);
7f2fe444 475
6000abfa 476 av_freep(&s->mbskip_table);
f943e138 477 av_freep(&s->prev_pict_types);
6000abfa 478 av_freep(&s->bitstream_buffer);
7f2fe444
MN
479 av_freep(&s->tex_pb_buffer);
480 av_freep(&s->pb2_buffer);
f7b47594 481 av_freep(&s->allocated_edge_emu_buffer); s->edge_emu_buffer= NULL;
bea669e5
MN
482 av_freep(&s->co_located_type_table);
483 av_freep(&s->field_mv_table);
484 av_freep(&s->field_select_table);
3aa102be 485 av_freep(&s->avctx->stats_out);
6b460aa3 486 av_freep(&s->ac_stats);
4d2858de 487 av_freep(&s->error_status_table);
1e491e29
MN
488
489 for(i=0; i<MAX_PICTURE_COUNT; i++){
490 free_picture(s, &s->picture[i]);
de6d9b64
FB
491 }
492 s->context_initialized = 0;
493}
494
7604246d
WH
495#ifdef CONFIG_ENCODERS
496
de6d9b64
FB
497/* init video encoder */
498int MPV_encode_init(AVCodecContext *avctx)
499{
500 MpegEncContext *s = avctx->priv_data;
519c2b6d 501 int i;
de6d9b64 502
bc657ac3
ZK
503 avctx->pix_fmt = PIX_FMT_YUV420P;
504
de6d9b64 505 s->bit_rate = avctx->bit_rate;
9cdd6a24 506 s->bit_rate_tolerance = avctx->bit_rate_tolerance;
de6d9b64
FB
507 s->width = avctx->width;
508 s->height = avctx->height;
7f2fe444 509 if(avctx->gop_size > 600){
333547be 510 fprintf(stderr, "Warning keyframe interval too large! reducing it ...\n");
7f2fe444
MN
511 avctx->gop_size=600;
512 }
de6d9b64 513 s->gop_size = avctx->gop_size;
644d98a4
J
514 s->rtp_mode = avctx->rtp_mode;
515 s->rtp_payload_size = avctx->rtp_payload_size;
81401c1f
J
516 if (avctx->rtp_callback)
517 s->rtp_callback = avctx->rtp_callback;
9cdd6a24
MN
518 s->qmin= avctx->qmin;
519 s->qmax= avctx->qmax;
520 s->max_qdiff= avctx->max_qdiff;
521 s->qcompress= avctx->qcompress;
522 s->qblur= avctx->qblur;
477c35a9 523 s->avctx = avctx;
ba6802de 524 s->flags= avctx->flags;
9dbcbd92 525 s->max_b_frames= avctx->max_b_frames;
8b4c7dbc 526 s->b_frame_strategy= avctx->b_frame_strategy;
d7e9533a 527 s->codec_id= avctx->codec->id;
7f2fe444
MN
528 s->luma_elim_threshold = avctx->luma_elim_threshold;
529 s->chroma_elim_threshold= avctx->chroma_elim_threshold;
530 s->strict_std_compliance= avctx->strict_std_compliance;
531 s->data_partitioning= avctx->flags & CODEC_FLAG_PART;
1457ab52 532 s->quarter_sample= (avctx->flags & CODEC_FLAG_QPEL)!=0;
87f8cab4 533 s->mpeg_quant= avctx->mpeg_quant;
d7e9533a 534
de6d9b64
FB
535 if (s->gop_size <= 1) {
536 s->intra_only = 1;
537 s->gop_size = 12;
538 } else {
539 s->intra_only = 0;
540 }
5e746b99 541
1457ab52 542 s->me_method = avctx->me_method;
5e746b99 543
e4986da9 544 /* Fixed QSCALE */
de6d9b64 545 s->fixed_qscale = (avctx->flags & CODEC_FLAG_QSCALE);
37fbfd0a 546
c5d309f2 547 s->adaptive_quant= ( s->avctx->lumi_masking
5e746b99 548 || s->avctx->dark_masking
c5d309f2
MN
549 || s->avctx->temporal_cplx_masking
550 || s->avctx->spatial_cplx_masking
551 || s->avctx->p_masking)
552 && !s->fixed_qscale;
fcb48651
MN
553
554 s->progressive_sequence= !(avctx->flags & CODEC_FLAG_INTERLACED_DCT);
5e746b99 555
de6d9b64
FB
556 switch(avctx->codec->id) {
557 case CODEC_ID_MPEG1VIDEO:
558 s->out_format = FMT_MPEG1;
14bea432
MN
559 s->low_delay= 0; //s->max_b_frames ? 0 : 1;
560 avctx->delay= s->low_delay ? 0 : (s->max_b_frames + 1);
de6d9b64
FB
561 break;
562 case CODEC_ID_MJPEG:
563 s->out_format = FMT_MJPEG;
564 s->intra_only = 1; /* force intra only for jpeg */
37fbfd0a 565 s->mjpeg_write_tables = 1; /* write all tables */
a69b930c 566 s->mjpeg_data_only_frames = 0; /* write all the needed headers */
37fbfd0a
J
567 s->mjpeg_vsample[0] = 2; /* set up default sampling factors */
568 s->mjpeg_vsample[1] = 1; /* the only currently supported values */
569 s->mjpeg_vsample[2] = 1;
a69b930c 570 s->mjpeg_hsample[0] = 2;
37fbfd0a
J
571 s->mjpeg_hsample[1] = 1;
572 s->mjpeg_hsample[2] = 1;
de6d9b64
FB
573 if (mjpeg_init(s) < 0)
574 return -1;
1ff662cc 575 avctx->delay=0;
4e00e76b 576 s->low_delay=1;
de6d9b64 577 break;
1d0d55da 578#ifdef CONFIG_RISKY
de6d9b64 579 case CODEC_ID_H263:
37fbfd0a
J
580 if (h263_get_picture_format(s->width, s->height) == 7) {
581 printf("Input picture size isn't suitable for h263 codec! try h263+\n");
de6d9b64 582 return -1;
37fbfd0a 583 }
de6d9b64 584 s->out_format = FMT_H263;
1ff662cc 585 avctx->delay=0;
4e00e76b 586 s->low_delay=1;
de6d9b64
FB
587 break;
588 case CODEC_ID_H263P:
589 s->out_format = FMT_H263;
590 s->h263_plus = 1;
21e59552
MN
591 /* Fx */
592 s->unrestricted_mv=(avctx->flags & CODEC_FLAG_H263P_UMV) ? 1:0;
593 s->h263_aic= (avctx->flags & CODEC_FLAG_H263P_AIC) ? 1:0;
594 /* /Fx */
544286b3 595 /* These are just to be sure */
e51d6d27 596 s->umvplus = 1;
1ff662cc 597 avctx->delay=0;
4e00e76b 598 s->low_delay=1;
de6d9b64
FB
599 break;
600 case CODEC_ID_RV10:
601 s->out_format = FMT_H263;
602 s->h263_rv10 = 1;
1ff662cc 603 avctx->delay=0;
4e00e76b 604 s->low_delay=1;
de6d9b64 605 break;
58f26ba9 606 case CODEC_ID_MPEG4:
de6d9b64
FB
607 s->out_format = FMT_H263;
608 s->h263_pred = 1;
609 s->unrestricted_mv = 1;
4e00e76b 610 s->low_delay= s->max_b_frames ? 0 : 1;
4d2858de 611 avctx->delay= s->low_delay ? 0 : (s->max_b_frames + 1);
de6d9b64 612 break;
84afee34 613 case CODEC_ID_MSMPEG4V1:
de6d9b64
FB
614 s->out_format = FMT_H263;
615 s->h263_msmpeg4 = 1;
616 s->h263_pred = 1;
617 s->unrestricted_mv = 1;
84afee34 618 s->msmpeg4_version= 1;
1ff662cc 619 avctx->delay=0;
4e00e76b 620 s->low_delay=1;
84afee34
MN
621 break;
622 case CODEC_ID_MSMPEG4V2:
623 s->out_format = FMT_H263;
624 s->h263_msmpeg4 = 1;
625 s->h263_pred = 1;
626 s->unrestricted_mv = 1;
627 s->msmpeg4_version= 2;
1ff662cc 628 avctx->delay=0;
4e00e76b 629 s->low_delay=1;
84afee34
MN
630 break;
631 case CODEC_ID_MSMPEG4V3:
632 s->out_format = FMT_H263;
633 s->h263_msmpeg4 = 1;
634 s->h263_pred = 1;
635 s->unrestricted_mv = 1;
636 s->msmpeg4_version= 3;
1ff662cc 637 avctx->delay=0;
4e00e76b 638 s->low_delay=1;
de6d9b64 639 break;
f5957f3f
MN
640 case CODEC_ID_WMV1:
641 s->out_format = FMT_H263;
642 s->h263_msmpeg4 = 1;
643 s->h263_pred = 1;
644 s->unrestricted_mv = 1;
645 s->msmpeg4_version= 4;
646 avctx->delay=0;
4e00e76b 647 s->low_delay=1;
f5957f3f
MN
648 break;
649 case CODEC_ID_WMV2:
650 s->out_format = FMT_H263;
651 s->h263_msmpeg4 = 1;
652 s->h263_pred = 1;
653 s->unrestricted_mv = 1;
654 s->msmpeg4_version= 5;
655 avctx->delay=0;
4e00e76b 656 s->low_delay=1;
f5957f3f 657 break;
1d0d55da 658#endif
de6d9b64
FB
659 default:
660 return -1;
661 }
cf8039b2 662
45870f57
MN
663 { /* set up some save defaults, some codecs might override them later */
664 static int done=0;
665 if(!done){
666 int i;
667 done=1;
bbed9259 668
0c1a9eda
ZK
669 default_mv_penalty= av_mallocz( sizeof(uint16_t)*(MAX_FCODE+1)*(2*MAX_MV+1) );
670 memset(default_mv_penalty, 0, sizeof(uint16_t)*(MAX_FCODE+1)*(2*MAX_MV+1));
671 memset(default_fcode_tab , 0, sizeof(uint8_t)*(2*MAX_MV+1));
45870f57
MN
672
673 for(i=-16; i<16; i++){
674 default_fcode_tab[i + MAX_MV]= 1;
675 }
676 }
677 }
1457ab52 678 s->me.mv_penalty= default_mv_penalty;
45870f57 679 s->fcode_tab= default_fcode_tab;
a406617f
MN
680 s->y_dc_scale_table=
681 s->c_dc_scale_table= ff_mpeg1_dc_scale_table;
682
9d2a0355 683 /* dont use mv_penalty table for crap MV as it would be confused */
1457ab52
MN
684 //FIXME remove after fixing / removing old ME
685 if (s->me_method < ME_EPZS) s->me.mv_penalty = default_mv_penalty;
9d2a0355 686
3bb4e23a
FB
687 s->encoding = 1;
688
de6d9b64
FB
689 /* init */
690 if (MPV_common_init(s) < 0)
691 return -1;
692
1457ab52
MN
693 ff_init_me(s);
694
bbed9259 695#ifdef CONFIG_ENCODERS
1d0d55da 696#ifdef CONFIG_RISKY
2ad1516a
MN
697 if (s->out_format == FMT_H263)
698 h263_encode_init(s);
2ad1516a
MN
699 if(s->msmpeg4_version)
700 ff_msmpeg4_encode_init(s);
bbed9259 701#endif
1d0d55da
MN
702 if (s->out_format == FMT_MPEG1)
703 ff_mpeg1_encode_init(s);
704#endif
2ad1516a 705
519c2b6d
FB
706 /* init default q matrix */
707 for(i=0;i<64;i++) {
b0368839 708 int j= s->dsp.idct_permutation[i];
1d0d55da 709#ifdef CONFIG_RISKY
87f8cab4 710 if(s->codec_id==CODEC_ID_MPEG4 && s->mpeg_quant){
2ad1516a
MN
711 s->intra_matrix[j] = ff_mpeg4_default_intra_matrix[i];
712 s->inter_matrix[j] = ff_mpeg4_default_non_intra_matrix[i];
87f8cab4 713 }else if(s->out_format == FMT_H263){
2ad1516a
MN
714 s->intra_matrix[j] =
715 s->inter_matrix[j] = ff_mpeg1_default_non_intra_matrix[i];
1d0d55da
MN
716 }else
717#endif
718 { /* mpeg1 */
2ad1516a
MN
719 s->intra_matrix[j] = ff_mpeg1_default_intra_matrix[i];
720 s->inter_matrix[j] = ff_mpeg1_default_non_intra_matrix[i];
87f8cab4 721 }
d7e9533a
MN
722 }
723
724 /* precompute matrix */
ef5b1b5a 725 /* for mjpeg, we do include qscale in the matrix */
d7e9533a 726 if (s->out_format != FMT_MJPEG) {
28db7fce 727 convert_matrix(s, s->q_intra_matrix, s->q_intra_matrix16, s->q_intra_matrix16_bias,
cc7ac888 728 s->intra_matrix, s->intra_quant_bias, 1, 31);
28db7fce 729 convert_matrix(s, s->q_inter_matrix, s->q_inter_matrix16, s->q_inter_matrix16_bias,
cc7ac888 730 s->inter_matrix, s->inter_quant_bias, 1, 31);
519c2b6d
FB
731 }
732
8b4c7dbc
MN
733 if(ff_rate_control_init(s) < 0)
734 return -1;
de6d9b64
FB
735
736 s->picture_number = 0;
45870f57 737 s->picture_in_gop_number = 0;
de6d9b64
FB
738 s->fake_picture_number = 0;
739 /* motion detector init */
740 s->f_code = 1;
9dbcbd92 741 s->b_code = 1;
de6d9b64
FB
742
743 return 0;
744}
745
746int MPV_encode_end(AVCodecContext *avctx)
747{
748 MpegEncContext *s = avctx->priv_data;
749
750#ifdef STATS
751 print_stats();
752#endif
8b4c7dbc
MN
753
754 ff_rate_control_uninit(s);
755
de6d9b64
FB
756 MPV_common_end(s);
757 if (s->out_format == FMT_MJPEG)
758 mjpeg_close(s);
37fbfd0a 759
de6d9b64
FB
760 return 0;
761}
762
7604246d
WH
763#endif //CONFIG_ENCODERS
764
1d0d55da
MN
765void init_rl(RLTable *rl)
766{
0c1a9eda
ZK
767 int8_t max_level[MAX_RUN+1], max_run[MAX_LEVEL+1];
768 uint8_t index_run[MAX_RUN+1];
1d0d55da
MN
769 int last, run, level, start, end, i;
770
771 /* compute max_level[], max_run[] and index_run[] */
772 for(last=0;last<2;last++) {
773 if (last == 0) {
774 start = 0;
775 end = rl->last;
776 } else {
777 start = rl->last;
778 end = rl->n;
779 }
780
781 memset(max_level, 0, MAX_RUN + 1);
782 memset(max_run, 0, MAX_LEVEL + 1);
783 memset(index_run, rl->n, MAX_RUN + 1);
784 for(i=start;i<end;i++) {
785 run = rl->table_run[i];
786 level = rl->table_level[i];
787 if (index_run[run] == rl->n)
788 index_run[run] = i;
789 if (level > max_level[run])
790 max_level[run] = level;
791 if (run > max_run[level])
792 max_run[level] = run;
793 }
794 rl->max_level[last] = av_malloc(MAX_RUN + 1);
795 memcpy(rl->max_level[last], max_level, MAX_RUN + 1);
796 rl->max_run[last] = av_malloc(MAX_LEVEL + 1);
797 memcpy(rl->max_run[last], max_run, MAX_LEVEL + 1);
798 rl->index_run[last] = av_malloc(MAX_RUN + 1);
799 memcpy(rl->index_run[last], index_run, MAX_RUN + 1);
800 }
801}
802
de6d9b64 803/* draw the edges of width 'w' of an image of size width, height */
fd7db0fd 804//FIXME check that this is ok for mpeg4 interlaced
0c1a9eda 805static void draw_edges_c(uint8_t *buf, int wrap, int width, int height, int w)
de6d9b64 806{
0c1a9eda 807 uint8_t *ptr, *last_line;
de6d9b64
FB
808 int i;
809
810 last_line = buf + (height - 1) * wrap;
811 for(i=0;i<w;i++) {
812 /* top and bottom */
813 memcpy(buf - (i + 1) * wrap, buf, width);
814 memcpy(last_line + (i + 1) * wrap, last_line, width);
815 }
816 /* left and right */
817 ptr = buf;
818 for(i=0;i<height;i++) {
819 memset(ptr - w, ptr[0], w);
820 memset(ptr + width, ptr[width-1], w);
821 ptr += wrap;
822 }
823 /* corners */
824 for(i=0;i<w;i++) {
825 memset(buf - (i + 1) * wrap - w, buf[0], w); /* top left */
826 memset(buf - (i + 1) * wrap + width, buf[width-1], w); /* top right */
827 memset(last_line + (i + 1) * wrap - w, last_line[0], w); /* top left */
828 memset(last_line + (i + 1) * wrap + width, last_line[width-1], w); /* top right */
829 }
830}
831
4e00e76b
MN
832static int find_unused_picture(MpegEncContext *s, int shared){
833 int i;
834
835 if(shared){
836 for(i=0; i<MAX_PICTURE_COUNT; i++){
837 if(s->picture[i].data[0]==NULL && s->picture[i].type==0) break;
838 }
839 }else{
840 for(i=0; i<MAX_PICTURE_COUNT; i++){
841 if(s->picture[i].data[0]==NULL && s->picture[i].type!=0) break;
842 }
843 for(i=0; i<MAX_PICTURE_COUNT; i++){
844 if(s->picture[i].data[0]==NULL) break;
845 }
846 }
847
848 assert(i<MAX_PICTURE_COUNT);
849 return i;
850}
851
de6d9b64 852/* generic function for encode/decode called before a frame is coded/decoded */
d6db1c9c 853int MPV_frame_start(MpegEncContext *s, AVCodecContext *avctx)
de6d9b64 854{
4e00e76b 855 int i;
492cd3a9 856 AVFrame *pic;
de6d9b64 857
425dddb7 858 s->mb_skiped = 0;
1e491e29
MN
859
860 /* mark&release old frames */
861 if (s->pict_type != B_TYPE && s->last_picture.data[0]) {
1e491e29 862 for(i=0; i<MAX_PICTURE_COUNT; i++){
4e00e76b 863//printf("%8X %d %d %X %X\n", s->picture[i].data[0], s->picture[i].type, i, s->next_picture.data[0], s->last_picture.data[0]);
1e491e29
MN
864 if(s->picture[i].data[0] == s->last_picture.data[0]){
865// s->picture[i].reference=0;
492cd3a9 866 avctx->release_buffer(avctx, (AVFrame*)&s->picture[i]);
1e491e29
MN
867 break;
868 }
869 }
870 assert(i<MAX_PICTURE_COUNT);
871
872 /* release forgotten pictures */
873 /* if(mpeg124/h263) */
874 if(!s->encoding){
875 for(i=0; i<MAX_PICTURE_COUNT; i++){
876 if(s->picture[i].data[0] && s->picture[i].data[0] != s->next_picture.data[0] && s->picture[i].reference){
877 fprintf(stderr, "releasing zombie picture\n");
492cd3a9 878 avctx->release_buffer(avctx, (AVFrame*)&s->picture[i]);
1e491e29
MN
879 }
880 }
d6db1c9c 881 }
93a21abd 882 }
aa388dba
MN
883alloc:
884 if(!s->encoding){
4e00e76b 885 i= find_unused_picture(s, 0);
1e491e29 886
492cd3a9 887 pic= (AVFrame*)&s->picture[i];
1e491e29
MN
888 pic->reference= s->pict_type != B_TYPE;
889 pic->coded_picture_number= s->current_picture.coded_picture_number+1;
890
4e00e76b 891 alloc_picture(s, (Picture*)pic, 0);
93a21abd 892
1e491e29
MN
893 s->current_picture= s->picture[i];
894 }
b7adc711 895
1e491e29
MN
896 if (s->pict_type != B_TYPE) {
897 s->last_picture= s->next_picture;
898 s->next_picture= s->current_picture;
de6d9b64 899 }
aa388dba
MN
900
901 if(s->pict_type != I_TYPE && s->last_picture.data[0]==NULL){
902 fprintf(stderr, "warning: first frame is no keyframe\n");
903 assert(s->pict_type != B_TYPE); //these should have been dropped if we dont have a reference
904 goto alloc;
905 }
1e491e29 906
aa388dba
MN
907 s->hurry_up= s->avctx->hurry_up;
908 s->error_resilience= avctx->error_resilience;
909
d930ef19
MN
910 /* set dequantizer, we cant do it during init as it might change for mpeg4
911 and we cant do it in the header decode as init isnt called for mpeg4 there yet */
912 if(s->out_format == FMT_H263){
913 if(s->mpeg_quant)
914 s->dct_unquantize = s->dct_unquantize_mpeg2;
915 else
916 s->dct_unquantize = s->dct_unquantize_h263;
917 }else
918 s->dct_unquantize = s->dct_unquantize_mpeg1;
d6db1c9c
MN
919
920 return 0;
de6d9b64 921}
21af69f7 922
de6d9b64
FB
923/* generic function for encode/decode called after a frame has been coded/decoded */
924void MPV_frame_end(MpegEncContext *s)
925{
1e491e29 926 int i;
de6d9b64 927 /* draw edge for correct motion prediction if outside */
1e491e29
MN
928 if(s->codec_id!=CODEC_ID_SVQ1){
929 if (s->pict_type != B_TYPE && !s->intra_only && !(s->flags&CODEC_FLAG_EMU_EDGE)) {
930 draw_edges(s->current_picture.data[0], s->linesize , s->h_edge_pos , s->v_edge_pos , EDGE_WIDTH );
931 draw_edges(s->current_picture.data[1], s->uvlinesize, s->h_edge_pos>>1, s->v_edge_pos>>1, EDGE_WIDTH/2);
932 draw_edges(s->current_picture.data[2], s->uvlinesize, s->h_edge_pos>>1, s->v_edge_pos>>1, EDGE_WIDTH/2);
933 }
de6d9b64 934 }
5975626d 935 emms_c();
8b4c7dbc 936
3aa102be 937 s->last_pict_type = s->pict_type;
8b4c7dbc
MN
938 if(s->pict_type!=B_TYPE){
939 s->last_non_b_pict_type= s->pict_type;
8b4c7dbc 940 }
1e491e29
MN
941
942 s->current_picture.quality= s->qscale; //FIXME get average of qscale_table
943 s->current_picture.pict_type= s->pict_type;
944 s->current_picture.key_frame= s->pict_type == I_TYPE;
945
946 /* copy back current_picture variables */
947 for(i=0; i<MAX_PICTURE_COUNT; i++){
948 if(s->picture[i].data[0] == s->current_picture.data[0]){
949 s->picture[i]= s->current_picture;
950 break;
951 }
952 }
953 assert(i<MAX_PICTURE_COUNT);
954
955 /* release non refernce frames */
956 for(i=0; i<MAX_PICTURE_COUNT; i++){
4e00e76b 957 if(s->picture[i].data[0] && !s->picture[i].reference /*&& s->picture[i].type!=FF_BUFFER_TYPE_SHARED*/)
492cd3a9 958 s->avctx->release_buffer(s->avctx, (AVFrame*)&s->picture[i]);
1e491e29 959 }
1457ab52
MN
960 if(s->avctx->debug&FF_DEBUG_SKIP){
961 int x,y;
962 for(y=0; y<s->mb_height; y++){
963 for(x=0; x<s->mb_width; x++){
964 int count= s->mbskip_table[x + y*s->mb_width];
965 if(count>9) count=9;
966 printf(" %1d", count);
967 }
968 printf("\n");
969 }
970 printf("pict type: %d\n", s->pict_type);
971 }
de6d9b64
FB
972}
973
7604246d
WH
974#ifdef CONFIG_ENCODERS
975
0d1e9246
MN
976static int get_sae(uint8_t *src, int ref, int stride){
977 int x,y;
978 int acc=0;
979
980 for(y=0; y<16; y++){
981 for(x=0; x<16; x++){
982 acc+= ABS(src[x+y*stride] - ref);
983 }
984 }
985
986 return acc;
987}
988
989static int get_intra_count(MpegEncContext *s, uint8_t *src, uint8_t *ref, int stride){
990 int x, y, w, h;
991 int acc=0;
992
993 w= s->width &~15;
994 h= s->height&~15;
995
996 for(y=0; y<h; y+=16){
997 for(x=0; x<w; x+=16){
998 int offset= x + y*stride;
999 int sad = s->dsp.pix_abs16x16(src + offset, ref + offset, stride);
1000 int mean= (s->dsp.pix_sum(src + offset, stride) + 128)>>8;
1001 int sae = get_sae(src + offset, mean, stride);
1002
1003 acc+= sae + 500 < sad;
1004 }
1005 }
1006 return acc;
1007}
1008
4e00e76b 1009
492cd3a9
MN
1010static int load_input_picture(MpegEncContext *s, AVFrame *pic_arg){
1011 AVFrame *pic;
4e00e76b 1012 int i;
1e491e29 1013 const int encoding_delay= s->max_b_frames;
4e00e76b 1014 int direct=1;
1e491e29 1015
4e00e76b
MN
1016 if(encoding_delay && !(s->flags&CODEC_FLAG_INPUT_PRESERVED)) direct=0;
1017 if(pic_arg->linesize[0] != s->linesize) direct=0;
1018 if(pic_arg->linesize[1] != s->uvlinesize) direct=0;
1019 if(pic_arg->linesize[2] != s->uvlinesize) direct=0;
1020
1021// printf("%d %d %d %d\n",pic_arg->linesize[0], pic_arg->linesize[1], s->linesize, s->uvlinesize);
1e491e29 1022
4e00e76b
MN
1023 if(direct){
1024 i= find_unused_picture(s, 1);
1e491e29 1025
492cd3a9 1026 pic= (AVFrame*)&s->picture[i];
4e00e76b 1027 pic->reference= 1;
1e491e29 1028
4e00e76b
MN
1029 for(i=0; i<4; i++){
1030 pic->data[i]= pic_arg->data[i];
1031 pic->linesize[i]= pic_arg->linesize[i];
1032 }
1033 alloc_picture(s, (Picture*)pic, 1);
1034 }else{
1035 i= find_unused_picture(s, 0);
1e491e29 1036
492cd3a9 1037 pic= (AVFrame*)&s->picture[i];
4e00e76b 1038 pic->reference= 1;
1e491e29 1039
4e00e76b
MN
1040 alloc_picture(s, (Picture*)pic, 0);
1041
1042 if( pic->data[0] == pic_arg->data[0]
1043 && pic->data[1] == pic_arg->data[1]
1044 && pic->data[2] == pic_arg->data[2]){
1e491e29 1045 // empty
4e00e76b
MN
1046 }else{
1047 int h_chroma_shift, v_chroma_shift;
1e491e29 1048
4e00e76b 1049 avcodec_get_chroma_sub_sample(s->avctx->pix_fmt, &h_chroma_shift, &v_chroma_shift);
1e491e29 1050
4e00e76b
MN
1051 for(i=0; i<3; i++){
1052 int src_stride= pic_arg->linesize[i];
1053 int dst_stride= i ? s->uvlinesize : s->linesize;
1054 int h_shift= i ? h_chroma_shift : 0;
1055 int v_shift= i ? v_chroma_shift : 0;
1056 int w= s->width >>h_shift;
1057 int h= s->height>>v_shift;
1058 uint8_t *src= pic_arg->data[i];
1059 uint8_t *dst= pic->data[i];
1e491e29 1060
4e00e76b
MN
1061 if(src_stride==dst_stride)
1062 memcpy(dst, src, src_stride*h);
1063 else{
1064 while(h--){
1065 memcpy(dst, src, w);
1066 dst += dst_stride;
1067 src += src_stride;
1068 }
1e491e29 1069 }
9dbcbd92 1070 }
1e491e29
MN
1071 }
1072 }
4e00e76b
MN
1073 pic->quality= pic_arg->quality;
1074 pic->pict_type= pic_arg->pict_type;
369fe599 1075 pic->pts = pic_arg->pts;
4e00e76b
MN
1076
1077 if(s->input_picture[encoding_delay])
1078 pic->display_picture_number= s->input_picture[encoding_delay]->display_picture_number + 1;
1079
1080 /* shift buffer entries */
1081 for(i=1; i<MAX_PICTURE_COUNT /*s->encoding_delay+1*/; i++)
1082 s->input_picture[i-1]= s->input_picture[i];
1083
1084 s->input_picture[encoding_delay]= (Picture*)pic;
1e491e29
MN
1085
1086 return 0;
1087}
9dbcbd92 1088
1e491e29
MN
1089static void select_input_picture(MpegEncContext *s){
1090 int i;
1091 const int encoding_delay= s->max_b_frames;
1092 int coded_pic_num=0;
1093
1094 if(s->reordered_input_picture[0])
1095 coded_pic_num= s->reordered_input_picture[0]->coded_picture_number + 1;
4e00e76b 1096
1e491e29
MN
1097 for(i=1; i<MAX_PICTURE_COUNT; i++)
1098 s->reordered_input_picture[i-1]= s->reordered_input_picture[i];
1099 s->reordered_input_picture[MAX_PICTURE_COUNT-1]= NULL;
1100
1101 /* set next picture types & ordering */
1102 if(s->reordered_input_picture[0]==NULL && s->input_picture[0]){
0d1e9246
MN
1103 if(/*s->picture_in_gop_number >= s->gop_size ||*/ s->next_picture.data[0]==NULL || s->intra_only){
1104 s->reordered_input_picture[0]= s->input_picture[0];
1105 s->reordered_input_picture[0]->pict_type= I_TYPE;
1106 s->reordered_input_picture[0]->coded_picture_number= coded_pic_num;
1107 }else{
1108 int b_frames;
1109
1110 if(s->flags&CODEC_FLAG_PASS2){
1111 for(i=0; i<s->max_b_frames+1; i++){
1112 int pict_num= s->input_picture[0]->display_picture_number + i;
1113 int pict_type= s->rc_context.entry[pict_num].new_pict_type;
1114 s->input_picture[i]->pict_type= pict_type;
1115
1116 if(i + 1 >= s->rc_context.num_entries) break;
1117 }
1118 }
4e00e76b 1119
0d1e9246
MN
1120 if(s->input_picture[0]->pict_type){
1121 /* user selected pict_type */
140cb663
MN
1122 for(b_frames=0; b_frames<s->max_b_frames+1; b_frames++){
1123 if(s->input_picture[b_frames]->pict_type!=B_TYPE) break;
1124 }
0d1e9246 1125
140cb663
MN
1126 if(b_frames > s->max_b_frames){
1127 fprintf(stderr, "warning, too many bframes in a row\n");
1128 b_frames = s->max_b_frames;
140cb663 1129 }
0d1e9246
MN
1130 }else if(s->b_frame_strategy==0){
1131 b_frames= s->max_b_frames;
1132 }else if(s->b_frame_strategy==1){
1133 for(i=1; i<s->max_b_frames+1; i++){
1134 if(s->input_picture[i]->b_frame_score==0){
1135 s->input_picture[i]->b_frame_score=
4e00e76b
MN
1136 get_intra_count(s, s->input_picture[i ]->data[0],
1137 s->input_picture[i-1]->data[0], s->linesize) + 1;
0d1e9246
MN
1138 }
1139 }
1140 for(i=0; i<s->max_b_frames; i++){
1141 if(s->input_picture[i]->b_frame_score - 1 > s->mb_num/40) break;
1142 }
1143
1144 b_frames= FFMAX(0, i-1);
140cb663 1145
0d1e9246
MN
1146 /* reset scores */
1147 for(i=0; i<b_frames+1; i++){
1148 s->input_picture[i]->b_frame_score=0;
1149 }
1150 }else{
1151 fprintf(stderr, "illegal b frame strategy\n");
1152 b_frames=0;
140cb663 1153 }
0d1e9246
MN
1154
1155 emms_c();
1156//static int b_count=0;
1157//b_count+= b_frames;
1158//printf("b_frames: %d\n", b_count);
1159
1160 s->reordered_input_picture[0]= s->input_picture[b_frames];
1161 if( s->picture_in_gop_number + b_frames >= s->gop_size
1162 || s->reordered_input_picture[0]->pict_type== I_TYPE)
1e491e29 1163 s->reordered_input_picture[0]->pict_type= I_TYPE;
0d1e9246
MN
1164 else
1165 s->reordered_input_picture[0]->pict_type= P_TYPE;
1166 s->reordered_input_picture[0]->coded_picture_number= coded_pic_num;
1167 for(i=0; i<b_frames; i++){
1168 coded_pic_num++;
1169 s->reordered_input_picture[i+1]= s->input_picture[i];
1170 s->reordered_input_picture[i+1]->pict_type= B_TYPE;
1171 s->reordered_input_picture[i+1]->coded_picture_number= coded_pic_num;
9dbcbd92
MN
1172 }
1173 }
9dbcbd92 1174 }
1e491e29
MN
1175
1176 if(s->reordered_input_picture[0]){
4e00e76b
MN
1177 s->reordered_input_picture[0]->reference= s->reordered_input_picture[0]->pict_type!=B_TYPE;
1178
1179 if(s->reordered_input_picture[0]->type == FF_BUFFER_TYPE_SHARED){
1180 int i= find_unused_picture(s, 0);
1181 Picture *pic= &s->picture[i];
1182
1183 s->new_picture= *s->reordered_input_picture[0];
1184
1185 /* mark us unused / free shared pic */
1186 for(i=0; i<4; i++)
1187 s->reordered_input_picture[0]->data[i]= NULL;
1188 s->reordered_input_picture[0]->type= 0;
1189
1190 pic->pict_type = s->reordered_input_picture[0]->pict_type;
1191 pic->quality = s->reordered_input_picture[0]->quality;
1192 pic->coded_picture_number = s->reordered_input_picture[0]->coded_picture_number;
1193 pic->reference = s->reordered_input_picture[0]->reference;
1194
1195 alloc_picture(s, pic, 0);
1196
1197 s->current_picture= *pic;
1198 }else{
1199 assert( s->reordered_input_picture[0]->type==FF_BUFFER_TYPE_USER
1200 || s->reordered_input_picture[0]->type==FF_BUFFER_TYPE_INTERNAL);
1201
1202 s->new_picture= *s->reordered_input_picture[0];
1203
1204 for(i=0; i<4; i++){
1205 s->reordered_input_picture[0]->data[i]-=16; //FIXME dirty
1206 }
1207 s->current_picture= *s->reordered_input_picture[0];
1e491e29 1208 }
1e491e29
MN
1209
1210 s->picture_number= s->new_picture.display_picture_number;
1211//printf("dpn:%d\n", s->picture_number);
1212 }else{
1213 memset(&s->new_picture, 0, sizeof(Picture));
9dbcbd92
MN
1214 }
1215}
1216
de6d9b64
FB
1217int MPV_encode_picture(AVCodecContext *avctx,
1218 unsigned char *buf, int buf_size, void *data)
1219{
1220 MpegEncContext *s = avctx->priv_data;
492cd3a9 1221 AVFrame *pic_arg = data;
140cb663 1222 int i;
de6d9b64
FB
1223
1224 init_put_bits(&s->pb, buf, buf_size, NULL, NULL);
1225
1e491e29 1226 s->picture_in_gop_number++;
de6d9b64 1227
1e491e29 1228 load_input_picture(s, pic_arg);
8b4c7dbc 1229
1e491e29 1230 select_input_picture(s);
9dbcbd92
MN
1231
1232 /* output? */
1e491e29 1233 if(s->new_picture.data[0]){
9dbcbd92 1234
1e491e29
MN
1235 s->pict_type= s->new_picture.pict_type;
1236 if (s->fixed_qscale){ /* the ratecontrol needs the last qscale so we dont touch it for CBR */
1237 s->qscale= (int)(s->new_picture.quality+0.5);
1238 assert(s->qscale);
1239 }
1240//emms_c();
1241//printf("qs:%f %f %d\n", s->new_picture.quality, s->current_picture.quality, s->qscale);
93a21abd 1242 MPV_frame_start(s, avctx);
9dbcbd92
MN
1243
1244 encode_picture(s, s->picture_number);
208d3ddf 1245
9cd3766f 1246 avctx->real_pict_num = s->picture_number;
9dbcbd92
MN
1247 avctx->header_bits = s->header_bits;
1248 avctx->mv_bits = s->mv_bits;
1249 avctx->misc_bits = s->misc_bits;
1250 avctx->i_tex_bits = s->i_tex_bits;
1251 avctx->p_tex_bits = s->p_tex_bits;
1252 avctx->i_count = s->i_count;
66370d3f 1253 avctx->p_count = s->mb_num - s->i_count - s->skip_count; //FIXME f/b_count in avctx
9dbcbd92
MN
1254 avctx->skip_count = s->skip_count;
1255
1256 MPV_frame_end(s);
1257
1258 if (s->out_format == FMT_MJPEG)
1259 mjpeg_picture_trailer(s);
8b4c7dbc
MN
1260
1261 if(s->flags&CODEC_FLAG_PASS1)
1262 ff_write_pass1_stats(s);
de6d9b64
FB
1263 }
1264
9dbcbd92 1265 s->input_picture_number++;
de6d9b64
FB
1266
1267 flush_put_bits(&s->pb);
9cdd6a24 1268 s->frame_bits = (pbBufPtr(&s->pb) - s->pb.buf) * 8;
3aa102be 1269
9cdd6a24 1270 s->total_bits += s->frame_bits;
098eefe1 1271 avctx->frame_bits = s->frame_bits;
1e491e29 1272
140cb663
MN
1273 for(i=0; i<4; i++){
1274 avctx->error[i] += s->current_picture.error[i];
1275 }
1276
17592475 1277 return pbBufPtr(&s->pb) - s->pb.buf;
de6d9b64
FB
1278}
1279
7604246d
WH
1280#endif //CONFIG_ENCODERS
1281
44eb4951 1282static inline void gmc1_motion(MpegEncContext *s,
0c1a9eda 1283 uint8_t *dest_y, uint8_t *dest_cb, uint8_t *dest_cr,
44eb4951 1284 int dest_offset,
0c1a9eda 1285 uint8_t **ref_picture, int src_offset)
44eb4951 1286{
0c1a9eda 1287 uint8_t *ptr;
0fd90455 1288 int offset, src_x, src_y, linesize, uvlinesize;
44eb4951 1289 int motion_x, motion_y;
54993774 1290 int emu=0;
44eb4951 1291
44eb4951
MN
1292 motion_x= s->sprite_offset[0][0];
1293 motion_y= s->sprite_offset[0][1];
1294 src_x = s->mb_x * 16 + (motion_x >> (s->sprite_warping_accuracy+1));
1295 src_y = s->mb_y * 16 + (motion_y >> (s->sprite_warping_accuracy+1));
1296 motion_x<<=(3-s->sprite_warping_accuracy);
1297 motion_y<<=(3-s->sprite_warping_accuracy);
1298 src_x = clip(src_x, -16, s->width);
1299 if (src_x == s->width)
1300 motion_x =0;
1301 src_y = clip(src_y, -16, s->height);
1302 if (src_y == s->height)
1303 motion_y =0;
073b013d 1304
44eb4951 1305 linesize = s->linesize;
0fd90455 1306 uvlinesize = s->uvlinesize;
073b013d 1307
44eb4951
MN
1308 ptr = ref_picture[0] + (src_y * linesize) + src_x + src_offset;
1309
1310 dest_y+=dest_offset;
54993774 1311 if(s->flags&CODEC_FLAG_EMU_EDGE){
741235eb
MN
1312 if(src_x<0 || src_y<0 || src_x + 17 >= s->h_edge_pos
1313 || src_y + 17 >= s->v_edge_pos){
1457ab52 1314 ff_emulated_edge_mc(s, ptr, linesize, 17, 17, src_x, src_y, s->h_edge_pos, s->v_edge_pos);
54993774 1315 ptr= s->edge_emu_buffer;
54993774
MN
1316 }
1317 }
073b013d
MN
1318
1319 if((motion_x|motion_y)&7){
eb4b3dd3
ZK
1320 s->dsp.gmc1(dest_y , ptr , linesize, 16, motion_x&15, motion_y&15, 128 - s->no_rounding);
1321 s->dsp.gmc1(dest_y+8, ptr+8, linesize, 16, motion_x&15, motion_y&15, 128 - s->no_rounding);
073b013d
MN
1322 }else{
1323 int dxy;
1324
1325 dxy= ((motion_x>>3)&1) | ((motion_y>>2)&2);
1326 if (s->no_rounding){
eb4b3dd3 1327 s->dsp.put_no_rnd_pixels_tab[0][dxy](dest_y, ptr, linesize, 16);
073b013d 1328 }else{
eb4b3dd3
ZK
1329 s->dsp.put_pixels_tab [0][dxy](dest_y, ptr, linesize, 16);
1330 }
073b013d
MN
1331 }
1332
1333 if(s->flags&CODEC_FLAG_GRAY) return;
44eb4951
MN
1334
1335 motion_x= s->sprite_offset[1][0];
1336 motion_y= s->sprite_offset[1][1];
1337 src_x = s->mb_x * 8 + (motion_x >> (s->sprite_warping_accuracy+1));
1338 src_y = s->mb_y * 8 + (motion_y >> (s->sprite_warping_accuracy+1));
1339 motion_x<<=(3-s->sprite_warping_accuracy);
1340 motion_y<<=(3-s->sprite_warping_accuracy);
1341 src_x = clip(src_x, -8, s->width>>1);
1342 if (src_x == s->width>>1)
1343 motion_x =0;
1344 src_y = clip(src_y, -8, s->height>>1);
1345 if (src_y == s->height>>1)
1346 motion_y =0;
1347
0fd90455 1348 offset = (src_y * uvlinesize) + src_x + (src_offset>>1);
44eb4951 1349 ptr = ref_picture[1] + offset;
741235eb
MN
1350 if(s->flags&CODEC_FLAG_EMU_EDGE){
1351 if(src_x<0 || src_y<0 || src_x + 9 >= s->h_edge_pos>>1
1352 || src_y + 9 >= s->v_edge_pos>>1){
1353 ff_emulated_edge_mc(s, ptr, uvlinesize, 9, 9, src_x, src_y, s->h_edge_pos>>1, s->v_edge_pos>>1);
1354 ptr= s->edge_emu_buffer;
1355 emu=1;
1356 }
54993774 1357 }
eb4b3dd3 1358 s->dsp.gmc1(dest_cb + (dest_offset>>1), ptr, uvlinesize, 8, motion_x&15, motion_y&15, 128 - s->no_rounding);
54993774 1359
44eb4951 1360 ptr = ref_picture[2] + offset;
54993774 1361 if(emu){
1457ab52 1362 ff_emulated_edge_mc(s, ptr, uvlinesize, 9, 9, src_x, src_y, s->h_edge_pos>>1, s->v_edge_pos>>1);
54993774
MN
1363 ptr= s->edge_emu_buffer;
1364 }
eb4b3dd3 1365 s->dsp.gmc1(dest_cr + (dest_offset>>1), ptr, uvlinesize, 8, motion_x&15, motion_y&15, 128 - s->no_rounding);
44eb4951
MN
1366
1367 return;
1368}
1369
073b013d 1370static inline void gmc_motion(MpegEncContext *s,
0c1a9eda 1371 uint8_t *dest_y, uint8_t *dest_cb, uint8_t *dest_cr,
073b013d 1372 int dest_offset,
0c1a9eda 1373 uint8_t **ref_picture, int src_offset)
073b013d 1374{
0c1a9eda 1375 uint8_t *ptr;
073b013d
MN
1376 int linesize, uvlinesize;
1377 const int a= s->sprite_warping_accuracy;
1378 int ox, oy;
1379
1380 linesize = s->linesize;
1381 uvlinesize = s->uvlinesize;
1382
1383 ptr = ref_picture[0] + src_offset;
1384
1385 dest_y+=dest_offset;
1386
1387 ox= s->sprite_offset[0][0] + s->sprite_delta[0][0]*s->mb_x*16 + s->sprite_delta[0][1]*s->mb_y*16;
1388 oy= s->sprite_offset[0][1] + s->sprite_delta[1][0]*s->mb_x*16 + s->sprite_delta[1][1]*s->mb_y*16;
1389
eb4b3dd3 1390 s->dsp.gmc(dest_y, ptr, linesize, 16,
073b013d
MN
1391 ox,
1392 oy,
1393 s->sprite_delta[0][0], s->sprite_delta[0][1],
1394 s->sprite_delta[1][0], s->sprite_delta[1][1],
1395 a+1, (1<<(2*a+1)) - s->no_rounding,
1396 s->h_edge_pos, s->v_edge_pos);
eb4b3dd3 1397 s->dsp.gmc(dest_y+8, ptr, linesize, 16,
073b013d
MN
1398 ox + s->sprite_delta[0][0]*8,
1399 oy + s->sprite_delta[1][0]*8,
1400 s->sprite_delta[0][0], s->sprite_delta[0][1],
1401 s->sprite_delta[1][0], s->sprite_delta[1][1],
1402 a+1, (1<<(2*a+1)) - s->no_rounding,
1403 s->h_edge_pos, s->v_edge_pos);
1404
1405 if(s->flags&CODEC_FLAG_GRAY) return;
1406
1407
1408 dest_cb+=dest_offset>>1;
1409 dest_cr+=dest_offset>>1;
1410
1411 ox= s->sprite_offset[1][0] + s->sprite_delta[0][0]*s->mb_x*8 + s->sprite_delta[0][1]*s->mb_y*8;
1412 oy= s->sprite_offset[1][1] + s->sprite_delta[1][0]*s->mb_x*8 + s->sprite_delta[1][1]*s->mb_y*8;
1413
1414 ptr = ref_picture[1] + (src_offset>>1);
eb4b3dd3 1415 s->dsp.gmc(dest_cb, ptr, uvlinesize, 8,
073b013d
MN
1416 ox,
1417 oy,
1418 s->sprite_delta[0][0], s->sprite_delta[0][1],
1419 s->sprite_delta[1][0], s->sprite_delta[1][1],
1420 a+1, (1<<(2*a+1)) - s->no_rounding,
1421 s->h_edge_pos>>1, s->v_edge_pos>>1);
1422
1423 ptr = ref_picture[2] + (src_offset>>1);
eb4b3dd3 1424 s->dsp.gmc(dest_cr, ptr, uvlinesize, 8,
073b013d
MN
1425 ox,
1426 oy,
1427 s->sprite_delta[0][0], s->sprite_delta[0][1],
1428 s->sprite_delta[1][0], s->sprite_delta[1][1],
1429 a+1, (1<<(2*a+1)) - s->no_rounding,
1430 s->h_edge_pos>>1, s->v_edge_pos>>1);
1431}
1432
1433
0c1a9eda 1434void ff_emulated_edge_mc(MpegEncContext *s, uint8_t *src, int linesize, int block_w, int block_h,
93a21abd
MN
1435 int src_x, int src_y, int w, int h){
1436 int x, y;
1437 int start_y, start_x, end_y, end_x;
0c1a9eda 1438 uint8_t *buf= s->edge_emu_buffer;
eb4b3dd3 1439
93a21abd
MN
1440 if(src_y>= h){
1441 src+= (h-1-src_y)*linesize;
1442 src_y=h-1;
225f9c44
MN
1443 }else if(src_y<=-block_h){
1444 src+= (1-block_h-src_y)*linesize;
1445 src_y=1-block_h;
93a21abd
MN
1446 }
1447 if(src_x>= w){
1448 src+= (w-1-src_x);
1449 src_x=w-1;
225f9c44
MN
1450 }else if(src_x<=-block_w){
1451 src+= (1-block_w-src_x);
1452 src_x=1-block_w;
93a21abd
MN
1453 }
1454
b8a78f41
MN
1455 start_y= FFMAX(0, -src_y);
1456 start_x= FFMAX(0, -src_x);
1457 end_y= FFMIN(block_h, h-src_y);
1458 end_x= FFMIN(block_w, w-src_x);
54993774 1459
93a21abd
MN
1460 // copy existing part
1461 for(y=start_y; y<end_y; y++){
1462 for(x=start_x; x<end_x; x++){
1463 buf[x + y*linesize]= src[x + y*linesize];
1464 }
1465 }
1466
1467 //top
1468 for(y=0; y<start_y; y++){
1469 for(x=start_x; x<end_x; x++){
1470 buf[x + y*linesize]= buf[x + start_y*linesize];
1471 }
1472 }
1473
1474 //bottom
1475 for(y=end_y; y<block_h; y++){
1476 for(x=start_x; x<end_x; x++){
1477 buf[x + y*linesize]= buf[x + (end_y-1)*linesize];
1478 }
1479 }
1480
1481 for(y=0; y<block_h; y++){
1482 //left
1483 for(x=0; x<start_x; x++){
1484 buf[x + y*linesize]= buf[start_x + y*linesize];
1485 }
1486
1487 //right
1488 for(x=end_x; x<block_w; x++){
1489 buf[x + y*linesize]= buf[end_x - 1 + y*linesize];
1490 }
1491 }
1492}
1493
1494
de6d9b64
FB
1495/* apply one mpeg motion vector to the three components */
1496static inline void mpeg_motion(MpegEncContext *s,
0c1a9eda 1497 uint8_t *dest_y, uint8_t *dest_cb, uint8_t *dest_cr,
de6d9b64 1498 int dest_offset,
0c1a9eda 1499 uint8_t **ref_picture, int src_offset,
b3184779 1500 int field_based, op_pixels_func (*pix_op)[4],
de6d9b64
FB
1501 int motion_x, int motion_y, int h)
1502{
0c1a9eda 1503 uint8_t *ptr;
b5a093b3 1504 int dxy, offset, mx, my, src_x, src_y, height, v_edge_pos, linesize, uvlinesize;
93a21abd 1505 int emu=0;
b3184779 1506#if 0
44eb4951
MN
1507if(s->quarter_sample)
1508{
1509 motion_x>>=1;
1510 motion_y>>=1;
1511}
b3184779 1512#endif
de6d9b64
FB
1513 dxy = ((motion_y & 1) << 1) | (motion_x & 1);
1514 src_x = s->mb_x * 16 + (motion_x >> 1);
1515 src_y = s->mb_y * (16 >> field_based) + (motion_y >> 1);
1516
1517 /* WARNING: do no forget half pels */
1518 height = s->height >> field_based;
b5a093b3 1519 v_edge_pos = s->v_edge_pos >> field_based;
de6d9b64
FB
1520 src_x = clip(src_x, -16, s->width);
1521 if (src_x == s->width)
1522 dxy &= ~1;
1523 src_y = clip(src_y, -16, height);
1524 if (src_y == height)
1525 dxy &= ~2;
4c9ede2e
MN
1526 linesize = s->linesize << field_based;
1527 uvlinesize = s->uvlinesize << field_based;
de6d9b64
FB
1528 ptr = ref_picture[0] + (src_y * linesize) + (src_x) + src_offset;
1529 dest_y += dest_offset;
93a21abd
MN
1530
1531 if(s->flags&CODEC_FLAG_EMU_EDGE){
b5a093b3
MN
1532 if(src_x<0 || src_y<0 || src_x + (motion_x&1) + 16 > s->h_edge_pos
1533 || src_y + (motion_y&1) + h > v_edge_pos){
1457ab52 1534 ff_emulated_edge_mc(s, ptr - src_offset, s->linesize, 17, 17+field_based,
4a3d7fbc
MN
1535 src_x, src_y<<field_based, s->h_edge_pos, s->v_edge_pos);
1536 ptr= s->edge_emu_buffer + src_offset;
93a21abd
MN
1537 emu=1;
1538 }
1539 }
b3184779 1540 pix_op[0][dxy](dest_y, ptr, linesize, h);
de6d9b64 1541
b50eef3a
MN
1542 if(s->flags&CODEC_FLAG_GRAY) return;
1543
de6d9b64
FB
1544 if (s->out_format == FMT_H263) {
1545 dxy = 0;
1546 if ((motion_x & 3) != 0)
1547 dxy |= 1;
1548 if ((motion_y & 3) != 0)
1549 dxy |= 2;
1550 mx = motion_x >> 2;
1551 my = motion_y >> 2;
1552 } else {
1553 mx = motion_x / 2;
1554 my = motion_y / 2;
1555 dxy = ((my & 1) << 1) | (mx & 1);
1556 mx >>= 1;
1557 my >>= 1;
1558 }
1559
1560 src_x = s->mb_x * 8 + mx;
1561 src_y = s->mb_y * (8 >> field_based) + my;
1562 src_x = clip(src_x, -8, s->width >> 1);
1563 if (src_x == (s->width >> 1))
1564 dxy &= ~1;
1565 src_y = clip(src_y, -8, height >> 1);
1566 if (src_y == (height >> 1))
1567 dxy &= ~2;
4c9ede2e 1568 offset = (src_y * uvlinesize) + src_x + (src_offset >> 1);
de6d9b64 1569 ptr = ref_picture[1] + offset;
93a21abd 1570 if(emu){
1457ab52 1571 ff_emulated_edge_mc(s, ptr - (src_offset >> 1), s->uvlinesize, 9, 9+field_based,
4a3d7fbc
MN
1572 src_x, src_y<<field_based, s->h_edge_pos>>1, s->v_edge_pos>>1);
1573 ptr= s->edge_emu_buffer + (src_offset >> 1);
93a21abd 1574 }
b3184779 1575 pix_op[1][dxy](dest_cb + (dest_offset >> 1), ptr, uvlinesize, h >> 1);
93a21abd 1576
de6d9b64 1577 ptr = ref_picture[2] + offset;
93a21abd 1578 if(emu){
1457ab52 1579 ff_emulated_edge_mc(s, ptr - (src_offset >> 1), s->uvlinesize, 9, 9+field_based,
4a3d7fbc
MN
1580 src_x, src_y<<field_based, s->h_edge_pos>>1, s->v_edge_pos>>1);
1581 ptr= s->edge_emu_buffer + (src_offset >> 1);
93a21abd 1582 }
b3184779 1583 pix_op[1][dxy](dest_cr + (dest_offset >> 1), ptr, uvlinesize, h >> 1);
de6d9b64
FB
1584}
1585
44eb4951 1586static inline void qpel_motion(MpegEncContext *s,
0c1a9eda 1587 uint8_t *dest_y, uint8_t *dest_cb, uint8_t *dest_cr,
44eb4951 1588 int dest_offset,
0c1a9eda 1589 uint8_t **ref_picture, int src_offset,
b3184779
MN
1590 int field_based, op_pixels_func (*pix_op)[4],
1591 qpel_mc_func (*qpix_op)[16],
44eb4951
MN
1592 int motion_x, int motion_y, int h)
1593{
0c1a9eda 1594 uint8_t *ptr;
590a6358 1595 int dxy, offset, mx, my, src_x, src_y, height, v_edge_pos, linesize, uvlinesize;
225f9c44 1596 int emu=0;
44eb4951
MN
1597
1598 dxy = ((motion_y & 3) << 2) | (motion_x & 3);
1599 src_x = s->mb_x * 16 + (motion_x >> 2);
1600 src_y = s->mb_y * (16 >> field_based) + (motion_y >> 2);
1601
1602 height = s->height >> field_based;
b5a093b3 1603 v_edge_pos = s->v_edge_pos >> field_based;
44eb4951
MN
1604 src_x = clip(src_x, -16, s->width);
1605 if (src_x == s->width)
1606 dxy &= ~3;
1607 src_y = clip(src_y, -16, height);
1608 if (src_y == height)
1609 dxy &= ~12;
1610 linesize = s->linesize << field_based;
590a6358 1611 uvlinesize = s->uvlinesize << field_based;
44eb4951
MN
1612 ptr = ref_picture[0] + (src_y * linesize) + src_x + src_offset;
1613 dest_y += dest_offset;
1614//printf("%d %d %d\n", src_x, src_y, dxy);
225f9c44
MN
1615
1616 if(s->flags&CODEC_FLAG_EMU_EDGE){
b5a093b3
MN
1617 if(src_x<0 || src_y<0 || src_x + (motion_x&3) + 16 > s->h_edge_pos
1618 || src_y + (motion_y&3) + h > v_edge_pos){
1457ab52 1619 ff_emulated_edge_mc(s, ptr - src_offset, s->linesize, 17, 17+field_based,
4a3d7fbc
MN
1620 src_x, src_y<<field_based, s->h_edge_pos, s->v_edge_pos);
1621 ptr= s->edge_emu_buffer + src_offset;
225f9c44
MN
1622 emu=1;
1623 }
1624 }
590a6358
MN
1625 if(!field_based)
1626 qpix_op[0][dxy](dest_y, ptr, linesize);
1627 else{
1628 //damn interlaced mode
1629 //FIXME boundary mirroring is not exactly correct here
1630 qpix_op[1][dxy](dest_y , ptr , linesize);
1631 qpix_op[1][dxy](dest_y+8, ptr+8, linesize);
1632 }
b3184779 1633
b50eef3a
MN
1634 if(s->flags&CODEC_FLAG_GRAY) return;
1635
590a6358
MN
1636 if(field_based){
1637 mx= motion_x/2;
1638 my= motion_y>>1;
36df8805
MN
1639 }else if(s->workaround_bugs&FF_BUG_QPEL_CHROMA2){
1640 static const int rtab[8]= {0,0,1,1,0,0,0,1};
1641 mx= (motion_x>>1) + rtab[motion_x&7];
1642 my= (motion_y>>1) + rtab[motion_y&7];
03e93d35 1643 }else if(s->workaround_bugs&FF_BUG_QPEL_CHROMA){
590a6358
MN
1644 mx= (motion_x>>1)|(motion_x&1);
1645 my= (motion_y>>1)|(motion_y&1);
1646 }else{
1647 mx= motion_x/2;
1648 my= motion_y/2;
1649 }
1650 mx= (mx>>1)|(mx&1);
1651 my= (my>>1)|(my&1);
36df8805 1652
590a6358
MN
1653 dxy= (mx&1) | ((my&1)<<1);
1654 mx>>=1;
1655 my>>=1;
44eb4951 1656
44eb4951
MN
1657 src_x = s->mb_x * 8 + mx;
1658 src_y = s->mb_y * (8 >> field_based) + my;
1659 src_x = clip(src_x, -8, s->width >> 1);
1660 if (src_x == (s->width >> 1))
1661 dxy &= ~1;
1662 src_y = clip(src_y, -8, height >> 1);
1663 if (src_y == (height >> 1))
1664 dxy &= ~2;
1665
590a6358 1666 offset = (src_y * uvlinesize) + src_x + (src_offset >> 1);
44eb4951 1667 ptr = ref_picture[1] + offset;
225f9c44 1668 if(emu){
1457ab52 1669 ff_emulated_edge_mc(s, ptr - (src_offset >> 1), s->uvlinesize, 9, 9 + field_based,
4a3d7fbc
MN
1670 src_x, src_y<<field_based, s->h_edge_pos>>1, s->v_edge_pos>>1);
1671 ptr= s->edge_emu_buffer + (src_offset >> 1);
225f9c44 1672 }
590a6358 1673 pix_op[1][dxy](dest_cb + (dest_offset >> 1), ptr, uvlinesize, h >> 1);
225f9c44 1674
44eb4951 1675 ptr = ref_picture[2] + offset;
225f9c44 1676 if(emu){
1457ab52 1677 ff_emulated_edge_mc(s, ptr - (src_offset >> 1), s->uvlinesize, 9, 9 + field_based,
4a3d7fbc
MN
1678 src_x, src_y<<field_based, s->h_edge_pos>>1, s->v_edge_pos>>1);
1679 ptr= s->edge_emu_buffer + (src_offset >> 1);
225f9c44 1680 }
590a6358 1681 pix_op[1][dxy](dest_cr + (dest_offset >> 1), ptr, uvlinesize, h >> 1);
44eb4951
MN
1682}
1683
67725183
MN
1684inline int ff_h263_round_chroma(int x){
1685 if (x >= 0)
1686 return (h263_chroma_roundtab[x & 0xf] + ((x >> 3) & ~1));
1687 else {
1688 x = -x;
1689 return -(h263_chroma_roundtab[x & 0xf] + ((x >> 3) & ~1));
1690 }
1691}
44eb4951 1692
de6d9b64 1693static inline void MPV_motion(MpegEncContext *s,
0c1a9eda
ZK
1694 uint8_t *dest_y, uint8_t *dest_cb, uint8_t *dest_cr,
1695 int dir, uint8_t **ref_picture,
b3184779 1696 op_pixels_func (*pix_op)[4], qpel_mc_func (*qpix_op)[16])
de6d9b64
FB
1697{
1698 int dxy, offset, mx, my, src_x, src_y, motion_x, motion_y;
1699 int mb_x, mb_y, i;
0c1a9eda 1700 uint8_t *ptr, *dest;
225f9c44 1701 int emu=0;
de6d9b64
FB
1702
1703 mb_x = s->mb_x;
1704 mb_y = s->mb_y;
1705
1706 switch(s->mv_type) {
1707 case MV_TYPE_16X16:
1d0d55da 1708#ifdef CONFIG_RISKY
44eb4951 1709 if(s->mcsel){
073b013d
MN
1710 if(s->real_sprite_warping_points==1){
1711 gmc1_motion(s, dest_y, dest_cb, dest_cr, 0,
1712 ref_picture, 0);
1713 }else{
1714 gmc_motion(s, dest_y, dest_cb, dest_cr, 0,
1715 ref_picture, 0);
1716 }
b3184779 1717 }else if(s->quarter_sample){
44eb4951
MN
1718 qpel_motion(s, dest_y, dest_cb, dest_cr, 0,
1719 ref_picture, 0,
1720 0, pix_op, qpix_op,
1721 s->mv[dir][0][0], s->mv[dir][0][1], 16);
1457ab52
MN
1722 }else if(s->mspel){
1723 ff_mspel_motion(s, dest_y, dest_cb, dest_cr,
1724 ref_picture, pix_op,
1725 s->mv[dir][0][0], s->mv[dir][0][1], 16);
1d0d55da
MN
1726 }else
1727#endif
1728 {
44eb4951
MN
1729 mpeg_motion(s, dest_y, dest_cb, dest_cr, 0,
1730 ref_picture, 0,
1731 0, pix_op,
1732 s->mv[dir][0][0], s->mv[dir][0][1], 16);
1733 }
de6d9b64
FB
1734 break;
1735 case MV_TYPE_8X8:
1e7bfebe
MN
1736 mx = 0;
1737 my = 0;
1738 if(s->quarter_sample){
1739 for(i=0;i<4;i++) {
1740 motion_x = s->mv[dir][i][0];
1741 motion_y = s->mv[dir][i][1];
1742
1743 dxy = ((motion_y & 3) << 2) | (motion_x & 3);
1744 src_x = mb_x * 16 + (motion_x >> 2) + (i & 1) * 8;
1745 src_y = mb_y * 16 + (motion_y >> 2) + (i >>1) * 8;
1746
1747 /* WARNING: do no forget half pels */
1748 src_x = clip(src_x, -16, s->width);
1749 if (src_x == s->width)
1750 dxy &= ~3;
1751 src_y = clip(src_y, -16, s->height);
1752 if (src_y == s->height)
1753 dxy &= ~12;
1754
1755 ptr = ref_picture[0] + (src_y * s->linesize) + (src_x);
1756 if(s->flags&CODEC_FLAG_EMU_EDGE){
1757 if(src_x<0 || src_y<0 || src_x + (motion_x&3) + 8 > s->h_edge_pos
1758 || src_y + (motion_y&3) + 8 > s->v_edge_pos){
1457ab52 1759 ff_emulated_edge_mc(s, ptr, s->linesize, 9, 9, src_x, src_y, s->h_edge_pos, s->v_edge_pos);
1e7bfebe
MN
1760 ptr= s->edge_emu_buffer;
1761 }
1762 }
1763 dest = dest_y + ((i & 1) * 8) + (i >> 1) * 8 * s->linesize;
1764 qpix_op[1][dxy](dest, ptr, s->linesize);
1765
1766 mx += s->mv[dir][i][0]/2;
1767 my += s->mv[dir][i][1]/2;
1768 }
1769 }else{
1770 for(i=0;i<4;i++) {
1771 motion_x = s->mv[dir][i][0];
1772 motion_y = s->mv[dir][i][1];
de6d9b64 1773
1e7bfebe
MN
1774 dxy = ((motion_y & 1) << 1) | (motion_x & 1);
1775 src_x = mb_x * 16 + (motion_x >> 1) + (i & 1) * 8;
1776 src_y = mb_y * 16 + (motion_y >> 1) + (i >>1) * 8;
de6d9b64 1777
1e7bfebe
MN
1778 /* WARNING: do no forget half pels */
1779 src_x = clip(src_x, -16, s->width);
1780 if (src_x == s->width)
1781 dxy &= ~1;
1782 src_y = clip(src_y, -16, s->height);
1783 if (src_y == s->height)
1784 dxy &= ~2;
de6d9b64 1785
1e7bfebe
MN
1786 ptr = ref_picture[0] + (src_y * s->linesize) + (src_x);
1787 if(s->flags&CODEC_FLAG_EMU_EDGE){
1788 if(src_x<0 || src_y<0 || src_x + (motion_x&1) + 8 > s->h_edge_pos
1789 || src_y + (motion_y&1) + 8 > s->v_edge_pos){
1457ab52 1790 ff_emulated_edge_mc(s, ptr, s->linesize, 9, 9, src_x, src_y, s->h_edge_pos, s->v_edge_pos);
1e7bfebe
MN
1791 ptr= s->edge_emu_buffer;
1792 }
225f9c44 1793 }
1e7bfebe
MN
1794 dest = dest_y + ((i & 1) * 8) + (i >> 1) * 8 * s->linesize;
1795 pix_op[1][dxy](dest, ptr, s->linesize, 8);
1796
1797 mx += s->mv[dir][i][0];
1798 my += s->mv[dir][i][1];
225f9c44 1799 }
de6d9b64 1800 }
1e7bfebe 1801
b50eef3a 1802 if(s->flags&CODEC_FLAG_GRAY) break;
de6d9b64
FB
1803 /* In case of 8X8, we construct a single chroma motion vector
1804 with a special rounding */
67725183
MN
1805 mx= ff_h263_round_chroma(mx);
1806 my= ff_h263_round_chroma(my);
de6d9b64
FB
1807 dxy = ((my & 1) << 1) | (mx & 1);
1808 mx >>= 1;
1809 my >>= 1;
1810
1811 src_x = mb_x * 8 + mx;
1812 src_y = mb_y * 8 + my;
1813 src_x = clip(src_x, -8, s->width/2);
1814 if (src_x == s->width/2)
1815 dxy &= ~1;
1816 src_y = clip(src_y, -8, s->height/2);
1817 if (src_y == s->height/2)
1818 dxy &= ~2;
1819
0fd90455 1820 offset = (src_y * (s->uvlinesize)) + src_x;
de6d9b64 1821 ptr = ref_picture[1] + offset;
225f9c44 1822 if(s->flags&CODEC_FLAG_EMU_EDGE){
b5a093b3
MN
1823 if(src_x<0 || src_y<0 || src_x + (dxy &1) + 8 > s->h_edge_pos>>1
1824 || src_y + (dxy>>1) + 8 > s->v_edge_pos>>1){
1457ab52 1825 ff_emulated_edge_mc(s, ptr, s->uvlinesize, 9, 9, src_x, src_y, s->h_edge_pos>>1, s->v_edge_pos>>1);
225f9c44
MN
1826 ptr= s->edge_emu_buffer;
1827 emu=1;
1828 }
1829 }
b3184779 1830 pix_op[1][dxy](dest_cb, ptr, s->uvlinesize, 8);
225f9c44 1831
de6d9b64 1832 ptr = ref_picture[2] + offset;
225f9c44 1833 if(emu){
1457ab52 1834 ff_emulated_edge_mc(s, ptr, s->uvlinesize, 9, 9, src_x, src_y, s->h_edge_pos>>1, s->v_edge_pos>>1);
225f9c44
MN
1835 ptr= s->edge_emu_buffer;
1836 }
b3184779 1837 pix_op[1][dxy](dest_cr, ptr, s->uvlinesize, 8);
de6d9b64
FB
1838 break;
1839 case MV_TYPE_FIELD:
1840 if (s->picture_structure == PICT_FRAME) {
590a6358
MN
1841 if(s->quarter_sample){
1842 /* top field */
1843 qpel_motion(s, dest_y, dest_cb, dest_cr, 0,
1844 ref_picture, s->field_select[dir][0] ? s->linesize : 0,
1845 1, pix_op, qpix_op,
1846 s->mv[dir][0][0], s->mv[dir][0][1], 8);
1847 /* bottom field */
1848 qpel_motion(s, dest_y, dest_cb, dest_cr, s->linesize,
1849 ref_picture, s->field_select[dir][1] ? s->linesize : 0,
1850 1, pix_op, qpix_op,
1851 s->mv[dir][1][0], s->mv[dir][1][1], 8);
1852 }else{
1853 /* top field */
1854 mpeg_motion(s, dest_y, dest_cb, dest_cr, 0,
1855 ref_picture, s->field_select[dir][0] ? s->linesize : 0,
1856 1, pix_op,
1857 s->mv[dir][0][0], s->mv[dir][0][1], 8);
1858 /* bottom field */
1859 mpeg_motion(s, dest_y, dest_cb, dest_cr, s->linesize,
1860 ref_picture, s->field_select[dir][1] ? s->linesize : 0,
1861 1, pix_op,
1862 s->mv[dir][1][0], s->mv[dir][1][1], 8);
1863 }
de6d9b64 1864 } else {
dfb476cb
MN
1865 int offset;
1866 if(s->picture_structure == s->field_select[dir][0] + 1 || s->pict_type == B_TYPE || s->first_field){
1867 offset= s->field_select[dir][0] ? s->linesize/2 : 0;
1868 }else{
1869 ref_picture= s->current_picture.data;
1870 offset= s->field_select[dir][0] ? s->linesize/2 : -s->linesize/2;
1871 }
de6d9b64 1872
dfb476cb
MN
1873 mpeg_motion(s, dest_y, dest_cb, dest_cr, 0,
1874 ref_picture, offset,
1875 0, pix_op,
1876 s->mv[dir][0][0], s->mv[dir][0][1], 16);
de6d9b64
FB
1877 }
1878 break;
1879 }
1880}
1881
1882
1883/* put block[] to dest[] */
1884static inline void put_dct(MpegEncContext *s,
0c1a9eda 1885 DCTELEM *block, int i, uint8_t *dest, int line_size)
de6d9b64 1886{
a0201736 1887 s->dct_unquantize(s, block, i, s->qscale);
b0368839 1888 s->dsp.idct_put (dest, line_size, block);
de6d9b64
FB
1889}
1890
1891/* add block[] to dest[] */
1892static inline void add_dct(MpegEncContext *s,
0c1a9eda 1893 DCTELEM *block, int i, uint8_t *dest, int line_size)
de6d9b64 1894{
0f440e02 1895 if (s->block_last_index[i] >= 0) {
b0368839 1896 s->dsp.idct_add (dest, line_size, block);
0f440e02
MN
1897 }
1898}
2417652e 1899
0f440e02 1900static inline void add_dequant_dct(MpegEncContext *s,
0c1a9eda 1901 DCTELEM *block, int i, uint8_t *dest, int line_size)
0f440e02 1902{
de6d9b64 1903 if (s->block_last_index[i] >= 0) {
0f440e02 1904 s->dct_unquantize(s, block, i, s->qscale);
9dbcbd92 1905
b0368839 1906 s->dsp.idct_add (dest, line_size, block);
de6d9b64
FB
1907 }
1908}
1909
7f2fe444
MN
1910/**
1911 * cleans dc, ac, coded_block for the current non intra MB
1912 */
1913void ff_clean_intra_table_entries(MpegEncContext *s)
1914{
1915 int wrap = s->block_wrap[0];
1916 int xy = s->block_index[0];
1917
1918 s->dc_val[0][xy ] =
1919 s->dc_val[0][xy + 1 ] =
1920 s->dc_val[0][xy + wrap] =
1921 s->dc_val[0][xy + 1 + wrap] = 1024;
1922 /* ac pred */
0c1a9eda
ZK
1923 memset(s->ac_val[0][xy ], 0, 32 * sizeof(int16_t));
1924 memset(s->ac_val[0][xy + wrap], 0, 32 * sizeof(int16_t));
7f2fe444
MN
1925 if (s->msmpeg4_version>=3) {
1926 s->coded_block[xy ] =
1927 s->coded_block[xy + 1 ] =
1928 s->coded_block[xy + wrap] =
1929 s->coded_block[xy + 1 + wrap] = 0;
1930 }
1931 /* chroma */
1932 wrap = s->block_wrap[4];
1933 xy = s->mb_x + 1 + (s->mb_y + 1) * wrap;
1934 s->dc_val[1][xy] =
1935 s->dc_val[2][xy] = 1024;
1936 /* ac pred */
0c1a9eda
ZK
1937 memset(s->ac_val[1][xy], 0, 16 * sizeof(int16_t));
1938 memset(s->ac_val[2][xy], 0, 16 * sizeof(int16_t));
7f2fe444
MN
1939
1940 s->mbintra_table[s->mb_x + s->mb_y*s->mb_width]= 0;
1941}
1942
de6d9b64
FB
1943/* generic function called after a macroblock has been parsed by the
1944 decoder or after it has been encoded by the encoder.
1945
1946 Important variables used:
1947 s->mb_intra : true if intra macroblock
1948 s->mv_dir : motion vector direction
1949 s->mv_type : motion vector type
1950 s->mv : motion vector
1951 s->interlaced_dct : true if interlaced dct used (mpeg2)
1952 */
1953void MPV_decode_mb(MpegEncContext *s, DCTELEM block[6][64])
1954{
8257bf05 1955 int mb_x, mb_y;
0f440e02 1956 const int mb_xy = s->mb_y * s->mb_width + s->mb_x;
de6d9b64
FB
1957
1958 mb_x = s->mb_x;
1959 mb_y = s->mb_y;
1960
1e491e29 1961 s->current_picture.qscale_table[mb_xy]= s->qscale;
79e7b305 1962
de6d9b64
FB
1963 /* update DC predictors for P macroblocks */
1964 if (!s->mb_intra) {
9e15ad28 1965 if (s->h263_pred || s->h263_aic) {
0f440e02 1966 if(s->mbintra_table[mb_xy])
7f2fe444 1967 ff_clean_intra_table_entries(s);
de6d9b64 1968 } else {
7f2fe444
MN
1969 s->last_dc[0] =
1970 s->last_dc[1] =
de6d9b64
FB
1971 s->last_dc[2] = 128 << s->intra_dc_precision;
1972 }
1973 }
9e15ad28 1974 else if (s->h263_pred || s->h263_aic)
0f440e02 1975 s->mbintra_table[mb_xy]=1;
bff6ecaa 1976
c5b1c10a 1977 /* update motion predictor, not for B-frames as they need the motion_val from the last P/S-Frame */
0f440e02 1978 if (s->out_format == FMT_H263 && s->pict_type!=B_TYPE) { //FIXME move into h263.c if possible, format specific stuff shouldnt be here
1457ab52 1979 //FIXME a lot of thet is only needed for !low_delay
0f440e02
MN
1980 const int wrap = s->block_wrap[0];
1981 const int xy = s->block_index[0];
bea669e5 1982 const int mb_index= s->mb_x + s->mb_y*s->mb_width;
fd7db0fd 1983 if(s->mv_type == MV_TYPE_8X8){
bea669e5 1984 s->co_located_type_table[mb_index]= CO_LOCATED_TYPE_4MV;
fd7db0fd
MN
1985 } else {
1986 int motion_x, motion_y;
1987 if (s->mb_intra) {
1988 motion_x = 0;
1989 motion_y = 0;
bea669e5
MN
1990 if(s->co_located_type_table)
1991 s->co_located_type_table[mb_index]= 0;
fd7db0fd
MN
1992 } else if (s->mv_type == MV_TYPE_16X16) {
1993 motion_x = s->mv[0][0][0];
1994 motion_y = s->mv[0][0][1];
bea669e5
MN
1995 if(s->co_located_type_table)
1996 s->co_located_type_table[mb_index]= 0;
fd7db0fd 1997 } else /*if (s->mv_type == MV_TYPE_FIELD)*/ {
bea669e5 1998 int i;
fd7db0fd
MN
1999 motion_x = s->mv[0][0][0] + s->mv[0][1][0];
2000 motion_y = s->mv[0][0][1] + s->mv[0][1][1];
2001 motion_x = (motion_x>>1) | (motion_x&1);
bea669e5
MN
2002 for(i=0; i<2; i++){
2003 s->field_mv_table[mb_index][i][0]= s->mv[0][i][0];
2004 s->field_mv_table[mb_index][i][1]= s->mv[0][i][1];
2005 s->field_select_table[mb_index][i]= s->field_select[0][i];
2006 }
2007 s->co_located_type_table[mb_index]= CO_LOCATED_TYPE_FIELDMV;
fd7db0fd 2008 }
de6d9b64 2009 /* no update if 8X8 because it has been done during parsing */
8257bf05
ZK
2010 s->motion_val[xy][0] = motion_x;
2011 s->motion_val[xy][1] = motion_y;
2012 s->motion_val[xy + 1][0] = motion_x;
2013 s->motion_val[xy + 1][1] = motion_y;
2014 s->motion_val[xy + wrap][0] = motion_x;
2015 s->motion_val[xy + wrap][1] = motion_y;
2016 s->motion_val[xy + 1 + wrap][0] = motion_x;
2017 s->motion_val[xy + 1 + wrap][1] = motion_y;
de6d9b64
FB
2018 }
2019 }
2020
140cb663 2021 if ((s->flags&CODEC_FLAG_PSNR) || !(s->encoding && (s->intra_only || s->pict_type==B_TYPE))) { //FIXME precalc
0c1a9eda 2022 uint8_t *dest_y, *dest_cb, *dest_cr;
0f440e02 2023 int dct_linesize, dct_offset;
b3184779
MN
2024 op_pixels_func (*op_pix)[4];
2025 qpel_mc_func (*op_qpix)[16];
3bb4e23a 2026
1e491e29 2027 /* avoid copy if macroblock skipped in last frame too */
9dbcbd92 2028 if (s->pict_type != B_TYPE) {
1e491e29
MN
2029 s->current_picture.mbskip_table[mb_xy]= s->mb_skiped;
2030 }
2031
2032 /* skip only during decoding as we might trash the buffers during encoding a bit */
2033 if(!s->encoding){
0c1a9eda 2034 uint8_t *mbskip_ptr = &s->mbskip_table[mb_xy];
1e491e29 2035 const int age= s->current_picture.age;
0fd90455 2036
1e491e29
MN
2037 assert(age);
2038
2039 if (s->mb_skiped) {
2040 s->mb_skiped= 0;
2041 assert(s->pict_type!=I_TYPE);
2042
0fd90455
MN
2043 (*mbskip_ptr) ++; /* indicate that this time we skiped it */
2044 if(*mbskip_ptr >99) *mbskip_ptr= 99;
2045
1e491e29 2046 /* if previous was skipped too, then nothing to do ! */
f943e138
MN
2047 if (*mbskip_ptr >= age && s->current_picture.reference){
2048 return;
1e491e29 2049 }
f943e138
MN
2050 } else if(!s->current_picture.reference){
2051 (*mbskip_ptr) ++; /* increase counter so the age can be compared cleanly */
2052 if(*mbskip_ptr >99) *mbskip_ptr= 99;
2053 } else{
3bb4e23a
FB
2054 *mbskip_ptr = 0; /* not skipped */
2055 }
1e491e29
MN
2056 }else
2057 s->mb_skiped= 0;
de6d9b64 2058
640950c7 2059 if(s->pict_type==B_TYPE && s->avctx->draw_horiz_band && s->picture_structure==PICT_FRAME){ //FIXME precalc
1e491e29
MN
2060 dest_y = s->current_picture.data[0] + mb_x * 16;
2061 dest_cb = s->current_picture.data[1] + mb_x * 8;
2062 dest_cr = s->current_picture.data[2] + mb_x * 8;
3994623d 2063 }else{
1e491e29
MN
2064 dest_y = s->current_picture.data[0] + (mb_y * 16* s->linesize ) + mb_x * 16;
2065 dest_cb = s->current_picture.data[1] + (mb_y * 8 * s->uvlinesize) + mb_x * 8;
2066 dest_cr = s->current_picture.data[2] + (mb_y * 8 * s->uvlinesize) + mb_x * 8;
3994623d 2067 }
de6d9b64
FB
2068
2069 if (s->interlaced_dct) {
2070 dct_linesize = s->linesize * 2;
2071 dct_offset = s->linesize;
2072 } else {
2073 dct_linesize = s->linesize;
2074 dct_offset = s->linesize * 8;
2075 }
2076
2077 if (!s->mb_intra) {
2078 /* motion handling */
7f2fe444 2079 /* decoding or more than one mb_type (MC was allready done otherwise) */
0f440e02 2080 if((!s->encoding) || (s->mb_type[mb_xy]&(s->mb_type[mb_xy]-1))){
91029be7 2081 if ((!s->no_rounding) || s->pict_type==B_TYPE){
eb4b3dd3
ZK
2082 op_pix = s->dsp.put_pixels_tab;
2083 op_qpix= s->dsp.put_qpel_pixels_tab;
9dbcbd92 2084 }else{
eb4b3dd3
ZK
2085 op_pix = s->dsp.put_no_rnd_pixels_tab;
2086 op_qpix= s->dsp.put_no_rnd_qpel_pixels_tab;
9dbcbd92 2087 }
de6d9b64 2088
9dbcbd92 2089 if (s->mv_dir & MV_DIR_FORWARD) {
1e491e29 2090 MPV_motion(s, dest_y, dest_cb, dest_cr, 0, s->last_picture.data, op_pix, op_qpix);
eb4b3dd3
ZK
2091 op_pix = s->dsp.avg_pixels_tab;
2092 op_qpix= s->dsp.avg_qpel_pixels_tab;
9dbcbd92
MN
2093 }
2094 if (s->mv_dir & MV_DIR_BACKWARD) {
1e491e29 2095 MPV_motion(s, dest_y, dest_cb, dest_cr, 1, s->next_picture.data, op_pix, op_qpix);
9dbcbd92 2096 }
de6d9b64
FB
2097 }
2098
0f440e02 2099 /* skip dequant / idct if we are really late ;) */
6bb925f4 2100 if(s->hurry_up>1) return;
0f440e02 2101
de6d9b64 2102 /* add dct residue */
a0201736
MN
2103 if(s->encoding || !( s->mpeg2 || s->h263_msmpeg4 || s->codec_id==CODEC_ID_MPEG1VIDEO
2104 || (s->codec_id==CODEC_ID_MPEG4 && !s->mpeg_quant))){
0f440e02
MN
2105 add_dequant_dct(s, block[0], 0, dest_y, dct_linesize);
2106 add_dequant_dct(s, block[1], 1, dest_y + 8, dct_linesize);
2107 add_dequant_dct(s, block[2], 2, dest_y + dct_offset, dct_linesize);
2108 add_dequant_dct(s, block[3], 3, dest_y + dct_offset + 8, dct_linesize);
2109
b50eef3a 2110 if(!(s->flags&CODEC_FLAG_GRAY)){
0fd90455
MN
2111 add_dequant_dct(s, block[4], 4, dest_cb, s->uvlinesize);
2112 add_dequant_dct(s, block[5], 5, dest_cr, s->uvlinesize);
b50eef3a 2113 }
1457ab52 2114 } else if(s->codec_id != CODEC_ID_WMV2){
0f440e02
MN
2115 add_dct(s, block[0], 0, dest_y, dct_linesize);
2116 add_dct(s, block[1], 1, dest_y + 8, dct_linesize);
2117 add_dct(s, block[2], 2, dest_y + dct_offset, dct_linesize);
2118 add_dct(s, block[3], 3, dest_y + dct_offset + 8, dct_linesize);
de6d9b64 2119
b50eef3a 2120 if(!(s->flags&CODEC_FLAG_GRAY)){
0fd90455
MN
2121 add_dct(s, block[4], 4, dest_cb, s->uvlinesize);
2122 add_dct(s, block[5], 5, dest_cr, s->uvlinesize);
b50eef3a 2123 }
1d0d55da
MN
2124 }
2125#ifdef CONFIG_RISKY
2126 else{
1457ab52 2127 ff_wmv2_add_mb(s, block, dest_y, dest_cb, dest_cr);
0f440e02 2128 }
1d0d55da 2129#endif
de6d9b64
FB
2130 } else {
2131 /* dct only in intra block */
a0201736
MN
2132 if(s->encoding || !(s->mpeg2 || s->codec_id==CODEC_ID_MPEG1VIDEO)){
2133 put_dct(s, block[0], 0, dest_y, dct_linesize);
2134 put_dct(s, block[1], 1, dest_y + 8, dct_linesize);
2135 put_dct(s, block[2], 2, dest_y + dct_offset, dct_linesize);
2136 put_dct(s, block[3], 3, dest_y + dct_offset + 8, dct_linesize);
2137
2138 if(!(s->flags&CODEC_FLAG_GRAY)){
2139 put_dct(s, block[4], 4, dest_cb, s->uvlinesize);
2140 put_dct(s, block[5], 5, dest_cr, s->uvlinesize);
2141 }
2142 }else{
b0368839
MN
2143 s->dsp.idct_put(dest_y , dct_linesize, block[0]);
2144 s->dsp.idct_put(dest_y + 8, dct_linesize, block[1]);
2145 s->dsp.idct_put(dest_y + dct_offset , dct_linesize, block[2]);
2146 s->dsp.idct_put(dest_y + dct_offset + 8, dct_linesize, block[3]);
a0201736
MN
2147
2148 if(!(s->flags&CODEC_FLAG_GRAY)){
b0368839
MN
2149 s->dsp.idct_put(dest_cb, s->uvlinesize, block[4]);
2150 s->dsp.idct_put(dest_cr, s->uvlinesize, block[5]);
a0201736 2151 }
b50eef3a 2152 }
de6d9b64
FB
2153 }
2154 }
2155}
2156
7604246d
WH
2157#ifdef CONFIG_ENCODERS
2158
c9178d86 2159static inline void dct_single_coeff_elimination(MpegEncContext *s, int n, int threshold)
7f2fe444
MN
2160{
2161 static const char tab[64]=
2162 {3,2,2,1,1,1,1,1,
2163 1,1,1,1,1,1,1,1,
2164 1,1,1,1,1,1,1,1,
2165 0,0,0,0,0,0,0,0,
2166 0,0,0,0,0,0,0,0,
2167 0,0,0,0,0,0,0,0,
2168 0,0,0,0,0,0,0,0,
2169 0,0,0,0,0,0,0,0};
2170 int score=0;
2171 int run=0;
2172 int i;
2173 DCTELEM *block= s->block[n];
2174 const int last_index= s->block_last_index[n];
c9178d86 2175 int skip_dc;
7f2fe444 2176
f5866a03
MN
2177 if(threshold<0){
2178 skip_dc=0;
2179 threshold= -threshold;
c9178d86
MN
2180 }else
2181 skip_dc=1;
f5866a03 2182
7f2fe444
MN
2183 /* are all which we could set to zero are allready zero? */
2184 if(last_index<=skip_dc - 1) return;
2185
2186 for(i=0; i<=last_index; i++){
2ad1516a 2187 const int j = s->intra_scantable.permutated[i];
7f2fe444
MN
2188 const int level = ABS(block[j]);
2189 if(level==1){
2190 if(skip_dc && i==0) continue;
2191 score+= tab[run];
2192 run=0;
2193 }else if(level>1){
2194 return;
2195 }else{
2196 run++;
2197 }
2198 }
2199 if(score >= threshold) return;
2200 for(i=skip_dc; i<=last_index; i++){
2ad1516a 2201 const int j = s->intra_scantable.permutated[i];
7f2fe444
MN
2202 block[j]=0;
2203 }
2204 if(block[0]) s->block_last_index[n]= 0;
2205 else s->block_last_index[n]= -1;
2206}
2207
d7e9533a
MN
2208static inline void clip_coeffs(MpegEncContext *s, DCTELEM *block, int last_index)
2209{
2210 int i;
2211 const int maxlevel= s->max_qcoeff;
2212 const int minlevel= s->min_qcoeff;
2ad1516a
MN
2213
2214 if(s->mb_intra){
2215 i=1; //skip clipping of intra dc
2216 }else
2217 i=0;
2218
2219 for(;i<=last_index; i++){
2220 const int j= s->intra_scantable.permutated[i];
d7e9533a
MN
2221 int level = block[j];
2222
2223 if (level>maxlevel) level=maxlevel;
2224 else if(level<minlevel) level=minlevel;
fcb48651 2225
3c5ce6dc 2226 block[j]= level;
fcb48651 2227 }
fcb48651
MN
2228}
2229
fcb48651 2230#if 0
0c1a9eda 2231static int pix_vcmp16x8(uint8_t *s, int stride){ //FIXME move to dsputil & optimize
fcb48651
MN
2232 int score=0;
2233 int x,y;
2234
2235 for(y=0; y<7; y++){
2236 for(x=0; x<16; x+=4){
2237 score+= ABS(s[x ] - s[x +stride]) + ABS(s[x+1] - s[x+1+stride])
2238 +ABS(s[x+2] - s[x+2+stride]) + ABS(s[x+3] - s[x+3+stride]);
2239 }
2240 s+= stride;
2241 }
2242
2243 return score;
2244}
2245
0c1a9eda 2246static int pix_diff_vcmp16x8(uint8_t *s1, uint8_t*s2, int stride){ //FIXME move to dsputil & optimize
fcb48651
MN
2247 int score=0;
2248 int x,y;
2249
2250 for(y=0; y<7; y++){
2251 for(x=0; x<16; x++){
2252 score+= ABS(s1[x ] - s2[x ] - s1[x +stride] + s2[x +stride]);
2253 }
2254 s1+= stride;
2255 s2+= stride;
2256 }
2257
2258 return score;
2259}
2260#else
2261#define SQ(a) ((a)*(a))
2262
0c1a9eda 2263static int pix_vcmp16x8(uint8_t *s, int stride){ //FIXME move to dsputil & optimize
fcb48651
MN
2264 int score=0;
2265 int x,y;
2266
2267 for(y=0; y<7; y++){
2268 for(x=0; x<16; x+=4){
2269 score+= SQ(s[x ] - s[x +stride]) + SQ(s[x+1] - s[x+1+stride])
2270 +SQ(s[x+2] - s[x+2+stride]) + SQ(s[x+3] - s[x+3+stride]);
2271 }
2272 s+= stride;
2273 }
2274
2275 return score;
2276}
2277
0c1a9eda 2278static int pix_diff_vcmp16x8(uint8_t *s1, uint8_t*s2, int stride){ //FIXME move to dsputil & optimize
fcb48651
MN
2279 int score=0;
2280 int x,y;
2281
2282 for(y=0; y<7; y++){
2283 for(x=0; x<16; x++){
2284 score+= SQ(s1[x ] - s2[x ] - s1[x +stride] + s2[x +stride]);
2285 }
2286 s1+= stride;
2287 s2+= stride;
2288 }
2289
2290 return score;
2291}
2292
2293#endif
4d2858de 2294
7604246d
WH
2295#endif //CONFIG_ENCODERS
2296
640950c7
MN
2297/**
2298 *
2299 * @param h is the normal height, this will be reduced automatically if needed for the last row
2300 */
2301void ff_draw_horiz_band(MpegEncContext *s, int y, int h){
4d2858de 2302 if ( s->avctx->draw_horiz_band
4e00e76b 2303 && (s->last_picture.data[0] || s->low_delay) ) {
0c1a9eda 2304 uint8_t *src_ptr[3];
640950c7
MN
2305 int offset;
2306 h= FFMIN(h, s->height - y);
4d2858de 2307
640950c7 2308 if(s->pict_type==B_TYPE && s->picture_structure == PICT_FRAME)
4d2858de
MN
2309 offset = 0;
2310 else
2311 offset = y * s->linesize;
2312
4e00e76b 2313 if(s->pict_type==B_TYPE || s->low_delay){
1e491e29
MN
2314 src_ptr[0] = s->current_picture.data[0] + offset;
2315 src_ptr[1] = s->current_picture.data[1] + (offset >> 2);
2316 src_ptr[2] = s->current_picture.data[2] + (offset >> 2);
4d2858de 2317 } else {
1e491e29
MN
2318 src_ptr[0] = s->last_picture.data[0] + offset;
2319 src_ptr[1] = s->last_picture.data[1] + (offset >> 2);
2320 src_ptr[2] = s->last_picture.data[2] + (offset >> 2);
4d2858de 2321 }
0aa7875a
MN
2322 emms_c();
2323
4d2858de
MN
2324 s->avctx->draw_horiz_band(s->avctx, src_ptr, s->linesize,
2325 y, s->width, h);
2326 }
2327}
2328
7604246d
WH
2329#ifdef CONFIG_ENCODERS
2330
9dbcbd92 2331static void encode_mb(MpegEncContext *s, int motion_x, int motion_y)
de6d9b64 2332{
ba6802de
MN
2333 const int mb_x= s->mb_x;
2334 const int mb_y= s->mb_y;
ba6802de 2335 int i;
7f2fe444 2336 int skip_dct[6];
fcb48651
MN
2337 int dct_offset = s->linesize*8; //default for progressive frames
2338
7f2fe444 2339 for(i=0; i<6; i++) skip_dct[i]=0;
c5d309f2
MN
2340
2341 if(s->adaptive_quant){
1e491e29 2342 s->dquant= s->current_picture.qscale_table[mb_x + mb_y*s->mb_width] - s->qscale;
fcb48651
MN
2343
2344 if(s->out_format==FMT_H263){
c5d309f2
MN
2345 if (s->dquant> 2) s->dquant= 2;
2346 else if(s->dquant<-2) s->dquant=-2;
fcb48651
MN
2347 }
2348
2349 if(s->codec_id==CODEC_ID_MPEG4){
c5d309f2
MN
2350 if(!s->mb_intra){
2351 assert(s->dquant==0 || s->mv_type!=MV_TYPE_8X8);
2352
2353 if(s->mv_dir&MV_DIRECT)
2354 s->dquant=0;
2355 }
2356 }
2357 s->qscale+= s->dquant;
2358 s->y_dc_scale= s->y_dc_scale_table[ s->qscale ];
2359 s->c_dc_scale= s->c_dc_scale_table[ s->qscale ];
2360 }
ba6802de 2361
9dbcbd92 2362 if (s->mb_intra) {
0c1a9eda 2363 uint8_t *ptr;
fcb48651 2364 int wrap_y;
79c0c785 2365 int emu=0;
9dbcbd92 2366
fcb48651 2367 wrap_y = s->linesize;
1e491e29 2368 ptr = s->new_picture.data[0] + (mb_y * 16 * wrap_y) + mb_x * 16;
fcb48651 2369
79c0c785 2370 if(mb_x*16+16 > s->width || mb_y*16+16 > s->height){
1457ab52 2371 ff_emulated_edge_mc(s, ptr, wrap_y, 16, 16, mb_x*16, mb_y*16, s->width, s->height);
79c0c785
MN
2372 ptr= s->edge_emu_buffer;
2373 emu=1;
2374 }
fcb48651
MN
2375
2376 if(s->flags&CODEC_FLAG_INTERLACED_DCT){
2377 int progressive_score, interlaced_score;
2378
2379 progressive_score= pix_vcmp16x8(ptr, wrap_y ) + pix_vcmp16x8(ptr + wrap_y*8, wrap_y );
2380 interlaced_score = pix_vcmp16x8(ptr, wrap_y*2) + pix_vcmp16x8(ptr + wrap_y , wrap_y*2);
2381
2382 if(progressive_score > interlaced_score + 100){
2383 s->interlaced_dct=1;
2384
2385 dct_offset= wrap_y;
2386 wrap_y<<=1;
2387 }else
2388 s->interlaced_dct=0;
2389 }
2390
eb4b3dd3
ZK
2391 s->dsp.get_pixels(s->block[0], ptr , wrap_y);
2392 s->dsp.get_pixels(s->block[1], ptr + 8, wrap_y);
2393 s->dsp.get_pixels(s->block[2], ptr + dct_offset , wrap_y);
2394 s->dsp.get_pixels(s->block[3], ptr + dct_offset + 8, wrap_y);
9dbcbd92 2395
6282185e
MN
2396 if(s->flags&CODEC_FLAG_GRAY){
2397 skip_dct[4]= 1;
2398 skip_dct[5]= 1;
2399 }else{
fcb48651 2400 int wrap_c = s->uvlinesize;
1e491e29 2401 ptr = s->new_picture.data[1] + (mb_y * 8 * wrap_c) + mb_x * 8;
79c0c785 2402 if(emu){
1457ab52 2403 ff_emulated_edge_mc(s, ptr, wrap_c, 8, 8, mb_x*8, mb_y*8, s->width>>1, s->height>>1);
79c0c785
MN
2404 ptr= s->edge_emu_buffer;
2405 }
eb4b3dd3 2406 s->dsp.get_pixels(s->block[4], ptr, wrap_c);
9dbcbd92 2407
1e491e29 2408 ptr = s->new_picture.data[2] + (mb_y * 8 * wrap_c) + mb_x * 8;
79c0c785 2409 if(emu){
1457ab52 2410 ff_emulated_edge_mc(s, ptr, wrap_c, 8, 8, mb_x*8, mb_y*8, s->width>>1, s->height>>1);
79c0c785
MN
2411 ptr= s->edge_emu_buffer;
2412 }
eb4b3dd3 2413 s->dsp.get_pixels(s->block[5], ptr, wrap_c);
6282185e 2414 }
9dbcbd92 2415 }else{
b3184779
MN
2416 op_pixels_func (*op_pix)[4];
2417 qpel_mc_func (*op_qpix)[16];
0c1a9eda
ZK
2418 uint8_t *dest_y, *dest_cb, *dest_cr;
2419 uint8_t *ptr_y, *ptr_cb, *ptr_cr;
7f2fe444 2420 int wrap_y, wrap_c;
79c0c785 2421 int emu=0;
9dbcbd92 2422
1e491e29
MN
2423 dest_y = s->current_picture.data[0] + (mb_y * 16 * s->linesize ) + mb_x * 16;
2424 dest_cb = s->current_picture.data[1] + (mb_y * 8 * (s->uvlinesize)) + mb_x * 8;
2425 dest_cr = s->current_picture.data[2] + (mb_y * 8 * (s->uvlinesize)) + mb_x * 8;
7f2fe444 2426 wrap_y = s->linesize;
fcb48651 2427 wrap_c = s->uvlinesize;
1e491e29
MN
2428 ptr_y = s->new_picture.data[0] + (mb_y * 16 * wrap_y) + mb_x * 16;
2429 ptr_cb = s->new_picture.data[1] + (mb_y * 8 * wrap_c) + mb_x * 8;
2430 ptr_cr = s->new_picture.data[2] + (mb_y * 8 * wrap_c) + mb_x * 8;
9dbcbd92 2431
91029be7 2432 if ((!s->no_rounding) || s->pict_type==B_TYPE){
eb4b3dd3
ZK
2433 op_pix = s->dsp.put_pixels_tab;
2434 op_qpix= s->dsp.put_qpel_pixels_tab;
cf8039b2 2435 }else{
eb4b3dd3
ZK
2436 op_pix = s->dsp.put_no_rnd_pixels_tab;
2437 op_qpix= s->dsp.put_no_rnd_qpel_pixels_tab;
9dbcbd92
MN
2438 }
2439
2440 if (s->mv_dir & MV_DIR_FORWARD) {
1e491e29 2441 MPV_motion(s, dest_y, dest_cb, dest_cr, 0, s->last_picture.data, op_pix, op_qpix);
eb4b3dd3
ZK
2442 op_pix = s->dsp.avg_pixels_tab;
2443 op_qpix= s->dsp.avg_qpel_pixels_tab;
9dbcbd92
MN
2444 }
2445 if (s->mv_dir & MV_DIR_BACKWARD) {
1e491e29 2446 MPV_motion(s, dest_y, dest_cb, dest_cr, 1, s->next_picture.data, op_pix, op_qpix);
ba6802de 2447 }
9dbcbd92 2448
79c0c785 2449 if(mb_x*16+16 > s->width || mb_y*16+16 > s->height){
1457ab52 2450 ff_emulated_edge_mc(s, ptr_y, wrap_y, 16, 16, mb_x*16, mb_y*16, s->width, s->height);
79c0c785
MN
2451 ptr_y= s->edge_emu_buffer;
2452 emu=1;
2453 }
fcb48651
MN
2454
2455 if(s->flags&CODEC_FLAG_INTERLACED_DCT){
2456 int progressive_score, interlaced_score;
2457
2458 progressive_score= pix_diff_vcmp16x8(ptr_y , dest_y , wrap_y )
2459 + pix_diff_vcmp16x8(ptr_y + wrap_y*8, dest_y + wrap_y*8, wrap_y );
2460 interlaced_score = pix_diff_vcmp16x8(ptr_y , dest_y , wrap_y*2)
2461 + pix_diff_vcmp16x8(ptr_y + wrap_y , dest_y + wrap_y , wrap_y*2);
2462
2463 if(progressive_score > interlaced_score + 600){
2464 s->interlaced_dct=1;
2465
2466 dct_offset= wrap_y;
2467 wrap_y<<=1;
2468 }else
2469 s->interlaced_dct=0;
2470 }
2471
eb4b3dd3
ZK
2472 s->dsp.diff_pixels(s->block[0], ptr_y , dest_y , wrap_y);
2473 s->dsp.diff_pixels(s->block[1], ptr_y + 8, dest_y + 8, wrap_y);
2474 s->dsp.diff_pixels(s->block[2], ptr_y + dct_offset , dest_y + dct_offset , wrap_y);
2475 s->dsp.diff_pixels(s->block[3], ptr_y + dct_offset + 8, dest_y + dct_offset + 8, wrap_y);
6282185e
MN
2476
2477 if(s->flags&CODEC_FLAG_GRAY){
2478 skip_dct[4]= 1;
2479 skip_dct[5]= 1;
2480 }else{
79c0c785 2481 if(emu){
1457ab52 2482 ff_emulated_edge_mc(s, ptr_cb, wrap_c, 8, 8, mb_x*8, mb_y*8, s->width>>1, s->height>>1);
79c0c785
MN
2483 ptr_cb= s->edge_emu_buffer;
2484 }
eb4b3dd3 2485 s->dsp.diff_pixels(s->block[4], ptr_cb, dest_cb, wrap_c);
79c0c785 2486 if(emu){
1457ab52 2487 ff_emulated_edge_mc(s, ptr_cr, wrap_c, 8, 8, mb_x*8, mb_y*8, s->width>>1, s->height>>1);
79c0c785
MN
2488 ptr_cr= s->edge_emu_buffer;
2489 }
eb4b3dd3 2490 s->dsp.diff_pixels(s->block[5], ptr_cr, dest_cr, wrap_c);
6282185e 2491 }
7f2fe444 2492 /* pre quantization */
1e491e29 2493 if(s->current_picture.mc_mb_var[s->mb_width*mb_y+ mb_x]<2*s->qscale*s->qscale){
fcb48651 2494 //FIXME optimize
eb4b3dd3
ZK
2495 if(s->dsp.pix_abs8x8(ptr_y , dest_y , wrap_y) < 20*s->qscale) skip_dct[0]= 1;
2496 if(s->dsp.pix_abs8x8(ptr_y + 8, dest_y + 8, wrap_y) < 20*s->qscale) skip_dct[1]= 1;
2497 if(s->dsp.pix_abs8x8(ptr_y +dct_offset , dest_y +dct_offset , wrap_y) < 20*s->qscale) skip_dct[2]= 1;
2498 if(s->dsp.pix_abs8x8(ptr_y +dct_offset+ 8, dest_y +dct_offset+ 8, wrap_y) < 20*s->qscale) skip_dct[3]= 1;
2571e3b6
MN
2499 if(s->dsp.pix_abs8x8(ptr_cb , dest_cb , wrap_c) < 20*s->qscale) skip_dct[4]= 1;
2500 if(s->dsp.pix_abs8x8(ptr_cr , dest_cr , wrap_c) < 20*s->qscale) skip_dct[5]= 1;
7f2fe444
MN
2501#if 0
2502{
2503 static int stat[7];
2504 int num=0;
2505 for(i=0; i<6; i++)
2506 if(skip_dct[i]) num++;
2507 stat[num]++;
2508
2509 if(s->mb_x==0 && s->mb_y==0){
2510 for(i=0; i<7; i++){
2511 printf("%6d %1d\n", stat[i], i);
2512 }
2513 }
2514}
2515#endif
2516 }
9dbcbd92 2517
ba6802de
MN
2518 }
2519
2520#if 0
2521 {
2522 float adap_parm;
2523
2524 adap_parm = ((s->avg_mb_var << 1) + s->mb_var[s->mb_width*mb_y+mb_x] + 1.0) /
2525 ((s->mb_var[s->mb_width*mb_y+mb_x] << 1) + s->avg_mb_var + 1.0);
2526
2527 printf("\ntype=%c qscale=%2d adap=%0.2f dquant=%4.2f var=%4d avgvar=%4d",
2528 (s->mb_type[s->mb_width*mb_y+mb_x] > 0) ? 'I' : 'P',
2529 s->qscale, adap_parm, s->qscale*adap_parm,
2530 s->mb_var[s->mb_width*mb_y+mb_x], s->avg_mb_var);
2531 }
2532#endif
2533 /* DCT & quantize */
d7e9533a
MN
2534 if(s->out_format==FMT_MJPEG){
2535 for(i=0;i<6;i++) {
2536 int overflow;
28db7fce 2537 s->block_last_index[i] = s->dct_quantize(s, s->block[i], i, 8, &overflow);
ef5b1b5a 2538 if (overflow) clip_coeffs(s, s->block[i], s->block_last_index[i]);
d7e9533a
MN
2539 }
2540 }else{
2541 for(i=0;i<6;i++) {
7f2fe444
MN
2542 if(!skip_dct[i]){
2543 int overflow;
28db7fce 2544 s->block_last_index[i] = s->dct_quantize(s, s->block[i], i, s->qscale, &overflow);
d7e9533a 2545 // FIXME we could decide to change to quantizer instead of clipping
ef5b1b5a
J
2546 // JS: I don't think that would be a good idea it could lower quality instead
2547 // of improve it. Just INTRADC clipping deserves changes in quantizer
7f2fe444
MN
2548 if (overflow) clip_coeffs(s, s->block[i], s->block_last_index[i]);
2549 }else
2550 s->block_last_index[i]= -1;
d7e9533a 2551 }
7f2fe444
MN
2552 if(s->luma_elim_threshold && !s->mb_intra)
2553 for(i=0; i<4; i++)
c9178d86 2554 dct_single_coeff_elimination(s, i, s->luma_elim_threshold);
7f2fe444
MN
2555 if(s->chroma_elim_threshold && !s->mb_intra)
2556 for(i=4; i<6; i++)
c9178d86 2557 dct_single_coeff_elimination(s, i, s->chroma_elim_threshold);
ba6802de
MN
2558 }
2559
6282185e
MN
2560 if((s->flags&CODEC_FLAG_GRAY) && s->mb_intra){
2561 s->block_last_index[4]=
2562 s->block_last_index[5]= 0;
2563 s->block[4][0]=
1d0eab1d 2564 s->block[5][0]= (1024 + s->c_dc_scale/2)/ s->c_dc_scale;
6282185e
MN
2565 }
2566
ba6802de 2567 /* huffman encode */
1457ab52
MN
2568 switch(s->codec_id){ //FIXME funct ptr could be slightly faster
2569 case CODEC_ID_MPEG1VIDEO:
2570 mpeg1_encode_mb(s, s->block, motion_x, motion_y); break;
1d0d55da 2571#ifdef CONFIG_RISKY
1457ab52
MN
2572 case CODEC_ID_MPEG4:
2573 mpeg4_encode_mb(s, s->block, motion_x, motion_y); break;
2574 case CODEC_ID_MSMPEG4V2:
2575 case CODEC_ID_MSMPEG4V3:
2576 case CODEC_ID_WMV1:
2577 msmpeg4_encode_mb(s, s->block, motion_x, motion_y); break;
2578 case CODEC_ID_WMV2:
2579 ff_wmv2_encode_mb(s, s->block, motion_x, motion_y); break;
1457ab52
MN
2580 case CODEC_ID_H263:
2581 case CODEC_ID_H263P:
2582 case CODEC_ID_RV10:
2583 h263_encode_mb(s, s->block, motion_x, motion_y); break;
1d0d55da
MN
2584#endif
2585 case CODEC_ID_MJPEG:
2586 mjpeg_encode_mb(s, s->block); break;
1457ab52
MN
2587 default:
2588 assert(0);
ba6802de
MN
2589 }
2590}
2591
7604246d
WH
2592#endif //CONFIG_ENCODERS
2593
b7a22d84
MN
2594/**
2595 * combines the (truncated) bitstream to a complete frame
2596 * @returns -1 if no complete frame could be created
2597 */
2598int ff_combine_frame( MpegEncContext *s, int next, uint8_t **buf, int *buf_size){
2599 ParseContext *pc= &s->parse_context;
2600
2601 pc->last_index= pc->index;
2602
2603 if(next==-1){
2604 pc->buffer= av_fast_realloc(pc->buffer, &pc->buffer_size, (*buf_size) + pc->index + FF_INPUT_BUFFER_PADDING_SIZE);
2605
2606 memcpy(&pc->buffer[pc->index], *buf, *buf_size);
2607 pc->index += *buf_size;
2608 return -1;
2609 }
2610
2611 if(pc->index){
2612 pc->buffer= av_fast_realloc(pc->buffer, &pc->buffer_size, next + pc->index + FF_INPUT_BUFFER_PADDING_SIZE);
2613
2614 memcpy(&pc->buffer[pc->index], *buf, next + FF_INPUT_BUFFER_PADDING_SIZE );
2615 pc->index = 0;
2616 *buf= pc->buffer;
2617 *buf_size= pc->last_index + next;
2618 }
2619
2620 return 0;
2621}
2622
7604246d 2623#ifdef CONFIG_ENCODERS
0c1a9eda 2624void ff_copy_bits(PutBitContext *pb, uint8_t *src, int length)
ba6802de 2625{
1f0cd30f
MN
2626 int bytes= length>>4;
2627 int bits= length&15;
2628 int i;
2629
7f2fe444
MN
2630 if(length==0) return;
2631
1f0cd30f
MN
2632 for(i=0; i<bytes; i++) put_bits(pb, 16, be2me_16(((uint16_t*)src)[i]));
2633 put_bits(pb, bits, be2me_16(((uint16_t*)src)[i])>>(16-bits));
ba6802de
MN
2634}
2635
7f2fe444 2636static inline void copy_context_before_encode(MpegEncContext *d, MpegEncContext *s, int type){
1f0cd30f
MN
2637 int i;
2638
2639 memcpy(d->last_mv, s->last_mv, 2*2*2*sizeof(int)); //FIXME is memcpy faster then a loop?
2640
2641 /* mpeg1 */
2642 d->mb_incr= s->mb_incr;
2643 for(i=0; i<3; i++)
2644 d->last_dc[i]= s->last_dc[i];
2645
2646 /* statistics */
2647 d->mv_bits= s->mv_bits;
2648 d->i_tex_bits= s->i_tex_bits;
2649 d->p_tex_bits= s->p_tex_bits;
2650 d->i_count= s->i_count;
66370d3f
MN
2651 d->f_count= s->f_count;
2652 d->b_count= s->b_count;
1f0cd30f
MN
2653 d->skip_count= s->skip_count;
2654 d->misc_bits= s->misc_bits;
8b4c7dbc 2655 d->last_bits= 0;
91029be7
MN
2656
2657 d->mb_skiped= s->mb_skiped;
5f3ed165 2658 d->qscale= s->qscale;
1f0cd30f
MN
2659}
2660
7f2fe444 2661static inline void copy_context_after_encode(MpegEncContext *d, MpegEncContext *s, int type){
1f0cd30f
MN
2662 int i;
2663
2664 memcpy(d->mv, s->mv, 2*4*2*sizeof(int));
2665 memcpy(d->last_mv, s->last_mv, 2*2*2*sizeof(int)); //FIXME is memcpy faster then a loop?
2666
2667 /* mpeg1 */
2668 d->mb_incr= s->mb_incr;
2669 for(i=0; i<3; i++)
2670 d->last_dc[i]= s->last_dc[i];
2671
2672 /* statistics */
2673 d->mv_bits= s->mv_bits;
2674 d->i_tex_bits= s->i_tex_bits;
2675 d->p_tex_bits= s->p_tex_bits;
2676 d->i_count= s->i_count;
66370d3f
MN
2677 d->f_count= s->f_count;
2678 d->b_count= s->b_count;
1f0cd30f
MN
2679 d->skip_count= s->skip_count;
2680 d->misc_bits= s->misc_bits;
1f0cd30f
MN
2681
2682 d->mb_intra= s->mb_intra;
91029be7 2683 d->mb_skiped= s->mb_skiped;
1f0cd30f
MN
2684 d->mv_type= s->mv_type;
2685 d->mv_dir= s->mv_dir;
2686 d->pb= s->pb;
7f2fe444
MN
2687 if(s->data_partitioning){
2688 d->pb2= s->pb2;
2689 d->tex_pb= s->tex_pb;
2690 }
1f0cd30f
MN
2691 d->block= s->block;
2692 for(i=0; i<6; i++)
2693 d->block_last_index[i]= s->block_last_index[i];
fb8a9e2f 2694 d->interlaced_dct= s->interlaced_dct;
5f3ed165 2695 d->qscale= s->qscale;
1f0cd30f
MN
2696}
2697
7f2fe444
MN
2698static inline void encode_mb_hq(MpegEncContext *s, MpegEncContext *backup, MpegEncContext *best, int type,
2699 PutBitContext pb[2], PutBitContext pb2[2], PutBitContext tex_pb[2],
2700 int *dmin, int *next_block, int motion_x, int motion_y)
2701{
2702 int bits_count;
2703
2704 copy_context_before_encode(s, backup, type);
2705
2706 s->block= s->blocks[*next_block];
2707 s->pb= pb[*next_block];
2708 if(s->data_partitioning){
2709 s->pb2 = pb2 [*next_block];
2710 s->tex_pb= tex_pb[*next_block];
2711 }
2712
2713 encode_mb(s, motion_x, motion_y);
2714
2715 bits_count= get_bit_count(&s->pb);
2716 if(s->data_partitioning){
2717 bits_count+= get_bit_count(&s->pb2);
2718 bits_count+= get_bit_count(&s->tex_pb);
2719 }
2720
2721 if(bits_count<*dmin){
2722 *dmin= bits_count;
2723 *next_block^=1;
2724
2725 copy_context_after_encode(best, s, type);
2726 }
2727}
140cb663
MN
2728
2729static inline int sse(MpegEncContext *s, uint8_t *src1, uint8_t *src2, int w, int h, int stride){
2730 uint32_t *sq = squareTbl + 256;
2731 int acc=0;
2732 int x,y;
2733
2734 if(w==16 && h==16)
1457ab52
MN
2735 return s->dsp.sse[0](NULL, src1, src2, stride);
2736 else if(w==8 && h==8)
2737 return s->dsp.sse[1](NULL, src1, src2, stride);
140cb663
MN
2738
2739 for(y=0; y<h; y++){
2740 for(x=0; x<w; x++){
2741 acc+= sq[src1[x + y*stride] - src2[x + y*stride]];
2742 }
2743 }
1457ab52
MN
2744
2745 assert(acc>=0);
2746
140cb663
MN
2747 return acc;
2748}
1f0cd30f 2749
ba6802de
MN
2750static void encode_picture(MpegEncContext *s, int picture_number)
2751{
945eeee1 2752 int mb_x, mb_y, pdif = 0;
ba6802de 2753 int i;
098eefe1 2754 int bits;
1f0cd30f 2755 MpegEncContext best_s, backup_s;
0c1a9eda
ZK
2756 uint8_t bit_buf[2][3000];
2757 uint8_t bit_buf2[2][3000];
2758 uint8_t bit_buf_tex[2][3000];
7f2fe444
MN
2759 PutBitContext pb[2], pb2[2], tex_pb[2];
2760
2761 for(i=0; i<2; i++){
2762 init_put_bits(&pb [i], bit_buf [i], 3000, NULL, NULL);
2763 init_put_bits(&pb2 [i], bit_buf2 [i], 3000, NULL, NULL);
2764 init_put_bits(&tex_pb[i], bit_buf_tex[i], 3000, NULL, NULL);
2765 }
de6d9b64
FB
2766
2767 s->picture_number = picture_number;
9cdd6a24 2768
ba6802de
MN
2769 s->block_wrap[0]=
2770 s->block_wrap[1]=
2771 s->block_wrap[2]=
2772 s->block_wrap[3]= s->mb_width*2 + 2;
2773 s->block_wrap[4]=
2774 s->block_wrap[5]= s->mb_width + 2;
2775
9cdd6a24 2776 /* Reset the average MB variance */
1e491e29
MN
2777 s->current_picture.mb_var_sum = 0;
2778 s->current_picture.mc_mb_var_sum = 0;
91029be7 2779
1d0d55da 2780#ifdef CONFIG_RISKY
91029be7 2781 /* we need to initialize some time vars before we can encode b-frames */
63b15e55
MN
2782 // RAL: Condition added for MPEG1VIDEO
2783 if (s->codec_id == CODEC_ID_MPEG1VIDEO || (s->h263_pred && !s->h263_msmpeg4))
91029be7 2784 ff_set_mpeg4_time(s, s->picture_number);
1d0d55da
MN
2785#endif
2786
1fb4890b 2787 s->scene_change_score=0;
c5d309f2
MN
2788
2789 s->qscale= (int)(s->frame_qscale + 0.5); //FIXME qscale / ... stuff for ME ratedistoration
1457ab52 2790
e51d6d27
MN
2791 if(s->pict_type==I_TYPE){
2792 if(s->msmpeg4_version) s->no_rounding=1;
2793 else s->no_rounding=0;
2794 }else if(s->pict_type!=B_TYPE){
2795 if(s->flipflop_rounding || s->codec_id == CODEC_ID_H263P || s->codec_id == CODEC_ID_MPEG4)
1457ab52
MN
2796 s->no_rounding ^= 1;
2797 }
e51d6d27 2798
9cdd6a24 2799 /* Estimate motion for every MB */
67725183 2800 s->mb_intra=0; //for the rate distoration & bit compare functions
9dbcbd92 2801 if(s->pict_type != I_TYPE){
f5fb6b34
MN
2802 if(s->pict_type != B_TYPE){
2803 if((s->avctx->pre_me && s->last_non_b_pict_type==I_TYPE) || s->avctx->pre_me==2){
f931ff7b 2804 s->me.pre_pass=1;
826f429a 2805 s->me.dia_size= s->avctx->pre_dia_size;
f931ff7b 2806
f5fb6b34
MN
2807 for(mb_y=s->mb_height-1; mb_y >=0 ; mb_y--) {
2808 for(mb_x=s->mb_width-1; mb_x >=0 ; mb_x--) {
2809 s->mb_x = mb_x;
2810 s->mb_y = mb_y;
2811 ff_pre_estimate_p_frame_motion(s, mb_x, mb_y);
2812 }
2813 }
f931ff7b 2814 s->me.pre_pass=0;
f5fb6b34
MN
2815 }
2816 }
2817
826f429a 2818 s->me.dia_size= s->avctx->dia_size;
ba6802de
MN
2819 for(mb_y=0; mb_y < s->mb_height; mb_y++) {
2820 s->block_index[0]= s->block_wrap[0]*(mb_y*2 + 1) - 1;
2821 s->block_index[1]= s->block_wrap[0]*(mb_y*2 + 1);
2822 s->block_index[2]= s->block_wrap[0]*(mb_y*2 + 2) - 1;
2823 s->block_index[3]= s->block_wrap[0]*(mb_y*2 + 2);
2824 for(mb_x=0; mb_x < s->mb_width; mb_x++) {
2825 s->mb_x = mb_x;
2826 s->mb_y = mb_y;
2827 s->block_index[0]+=2;
2828 s->block_index[1]+=2;
2829 s->block_index[2]+=2;
2830 s->block_index[3]+=2;
826f429a 2831
ba6802de 2832 /* compute motion vector & mb_type and store in context */
9dbcbd92
MN
2833 if(s->pict_type==B_TYPE)
2834 ff_estimate_b_frame_motion(s, mb_x, mb_y);
2835 else
2836 ff_estimate_p_frame_motion(s, mb_x, mb_y);
9cdd6a24 2837 }
9cdd6a24 2838 }
7f2fe444 2839 }else /* if(s->pict_type == I_TYPE) */{
ba6802de
MN
2840 /* I-Frame */
2841 //FIXME do we need to zero them?
0c1a9eda
ZK
2842 memset(s->motion_val[0], 0, sizeof(int16_t)*(s->mb_width*2 + 2)*(s->mb_height*2 + 2)*2);
2843 memset(s->p_mv_table , 0, sizeof(int16_t)*(s->mb_width+2)*(s->mb_height+2)*2);
2844 memset(s->mb_type , MB_TYPE_INTRA, sizeof(uint8_t)*s->mb_width*s->mb_height);
3aa102be
MN
2845
2846 if(!s->fixed_qscale){
2847 /* finding spatial complexity for I-frame rate control */
2848 for(mb_y=0; mb_y < s->mb_height; mb_y++) {
2849 for(mb_x=0; mb_x < s->mb_width; mb_x++) {
2850 int xx = mb_x * 16;
2851 int yy = mb_y * 16;
1e491e29 2852 uint8_t *pix = s->new_picture.data[0] + (yy * s->linesize) + xx;
3aa102be 2853 int varc;
eb4b3dd3 2854 int sum = s->dsp.pix_sum(pix, s->linesize);
3aa102be 2855
eb4b3dd3 2856 varc = (s->dsp.pix_norm1(pix, s->linesize) - (((unsigned)(sum*sum))>>8) + 500 + 128)>>8;
3aa102be 2857
1e491e29
MN
2858 s->current_picture.mb_var [s->mb_width * mb_y + mb_x] = varc;
2859 s->current_picture.mb_mean[s->mb_width * mb_y + mb_x] = (sum+128)>>8;
2860 s->current_picture.mb_var_sum += varc;
3aa102be
MN
2861 }
2862 }
2863 }
9cdd6a24 2864 }
6bb925f4
MN
2865 emms_c();
2866
1fb4890b 2867 if(s->scene_change_score > 0 && s->pict_type == P_TYPE){
bd31a775 2868 s->pict_type= I_TYPE;
0c1a9eda 2869 memset(s->mb_type , MB_TYPE_INTRA, sizeof(uint8_t)*s->mb_width*s->mb_height);
1e491e29 2870//printf("Scene change detected, encoding as I Frame %d %d\n", s->current_picture.mb_var_sum, s->current_picture.mc_mb_var_sum);
bd31a775 2871 }
1e491e29 2872
e51d6d27 2873 if(!s->umvplus){
ebbcdc9a
MN
2874 if(s->pict_type==P_TYPE || s->pict_type==S_TYPE) {
2875 s->f_code= ff_get_best_fcode(s, s->p_mv_table, MB_TYPE_INTER);
63b15e55 2876
ebbcdc9a
MN
2877 ff_fix_long_p_mvs(s);
2878 }
63b15e55 2879
ebbcdc9a
MN
2880 if(s->pict_type==B_TYPE){
2881 int a, b;
63b15e55 2882
ebbcdc9a
MN
2883 a = ff_get_best_fcode(s, s->b_forw_mv_table, MB_TYPE_FORWARD);
2884 b = ff_get_best_fcode(s, s->b_bidir_forw_mv_table, MB_TYPE_BIDIR);
2885 s->f_code = FFMAX(a, b);
63b15e55 2886
ebbcdc9a
MN
2887 a = ff_get_best_fcode(s, s->b_back_mv_table, MB_TYPE_BACKWARD);
2888 b = ff_get_best_fcode(s, s->b_bidir_back_mv_table, MB_TYPE_BIDIR);
2889 s->b_code = FFMAX(a, b);
91029be7 2890
ebbcdc9a
MN
2891 ff_fix_long_b_mvs(s, s->b_forw_mv_table, s->f_code, MB_TYPE_FORWARD);
2892 ff_fix_long_b_mvs(s, s->b_back_mv_table, s->b_code, MB_TYPE_BACKWARD);
2893 ff_fix_long_b_mvs(s, s->b_bidir_forw_mv_table, s->f_code, MB_TYPE_BIDIR);
2894 ff_fix_long_b_mvs(s, s->b_bidir_back_mv_table, s->b_code, MB_TYPE_BIDIR);
2895 }
45870f57 2896 }
9dbcbd92 2897
c5d309f2 2898 if (s->fixed_qscale)
1e491e29 2899 s->frame_qscale = s->current_picture.quality;
c5d309f2
MN
2900 else
2901 s->frame_qscale = ff_rate_estimate_qscale(s);
8b4c7dbc 2902
9e4c6948 2903 if(s->adaptive_quant){
1d0d55da 2904#ifdef CONFIG_RISKY
9e4c6948
MN
2905 switch(s->codec_id){
2906 case CODEC_ID_MPEG4:
2907 ff_clean_mpeg4_qscales(s);
2908 break;
2909 case CODEC_ID_H263:
2910 case CODEC_ID_H263P:
2911 ff_clean_h263_qscales(s);
2912 break;
2913 }
1d0d55da 2914#endif
9e4c6948 2915
1e491e29 2916 s->qscale= s->current_picture.qscale_table[0];
9e4c6948 2917 }else
c5d309f2
MN
2918 s->qscale= (int)(s->frame_qscale + 0.5);
2919
de6d9b64
FB
2920 if (s->out_format == FMT_MJPEG) {
2921 /* for mjpeg, we do include qscale in the matrix */
adc09b2e 2922 s->intra_matrix[0] = ff_mpeg1_default_intra_matrix[0];
2ad1516a 2923 for(i=1;i<64;i++){
b0368839 2924 int j= s->dsp.idct_permutation[i];
2ad1516a
MN
2925
2926 s->intra_matrix[j] = CLAMP_TO_8BIT((ff_mpeg1_default_intra_matrix[i] * s->qscale) >> 3);
2927 }
28db7fce 2928 convert_matrix(s, s->q_intra_matrix, s->q_intra_matrix16,
cc7ac888 2929 s->q_intra_matrix16_bias, s->intra_matrix, s->intra_quant_bias, 8, 8);
de6d9b64 2930 }
1e491e29
MN
2931
2932 //FIXME var duplication
2933 s->current_picture.key_frame= s->pict_type == I_TYPE;
2934 s->current_picture.pict_type= s->pict_type;
2935
2936 if(s->current_picture.key_frame)
2937 s->picture_in_gop_number=0;
de6d9b64 2938
098eefe1 2939 s->last_bits= get_bit_count(&s->pb);
de6d9b64
FB
2940 switch(s->out_format) {
2941 case FMT_MJPEG:
2942 mjpeg_picture_header(s);
2943 break;
1d0d55da 2944#ifdef CONFIG_RISKY
de6d9b64 2945 case FMT_H263:
1457ab52
MN
2946 if (s->codec_id == CODEC_ID_WMV2)
2947 ff_wmv2_encode_picture_header(s, picture_number);
2948 else if (s->h263_msmpeg4)
de6d9b64
FB
2949 msmpeg4_encode_picture_header(s, picture_number);
2950 else if (s->h263_pred)
2951 mpeg4_encode_picture_header(s, picture_number);
2952 else if (s->h263_rv10)
2953 rv10_encode_picture_header(s, picture_number);
2954 else
2955 h263_encode_picture_header(s, picture_number);
2956 break;
1d0d55da 2957#endif
de6d9b64
FB
2958 case FMT_MPEG1:
2959 mpeg1_encode_picture_header(s, picture_number);
2960 break;
2961 }
098eefe1
MN
2962 bits= get_bit_count(&s->pb);
2963 s->header_bits= bits - s->last_bits;
2964 s->last_bits= bits;
2965 s->mv_bits=0;
2966 s->misc_bits=0;
2967 s->i_tex_bits=0;
2968 s->p_tex_bits=0;
2969 s->i_count=0;
66370d3f
MN
2970 s->f_count=0;
2971 s->b_count=0;
098eefe1
MN
2972 s->skip_count=0;
2973
140cb663
MN
2974 for(i=0; i<3; i++){
2975 /* init last dc values */
2976 /* note: quant matrix value (8) is implied here */
2977 s->last_dc[i] = 128;
2978
2979 s->current_picture.error[i] = 0;
2980 }
de6d9b64
FB
2981 s->mb_incr = 1;
2982 s->last_mv[0][0][0] = 0;
2983 s->last_mv[0][0][1] = 0;
63b15e55
MN
2984 s->last_mv[1][0][0] = 0;
2985 s->last_mv[1][0][1] = 0;
2986
2987 s->last_mv_dir = 0;
de6d9b64 2988
1d0d55da 2989#ifdef CONFIG_RISKY
945eeee1 2990 if (s->codec_id==CODEC_ID_H263 || s->codec_id==CODEC_ID_H263P)
4d2858de 2991 s->gob_index = ff_h263_get_gob_height(s);
7f2fe444 2992
4d2858de 2993 if(s->codec_id==CODEC_ID_MPEG4 && s->partitioned_frame)
7f2fe444 2994 ff_mpeg4_init_partitions(s);
1d0d55da 2995#endif
7f2fe444
MN
2996
2997 s->resync_mb_x=0;
2998 s->resync_mb_y=0;
4d2858de 2999 s->first_slice_line = 1;
945eeee1
MN
3000 s->ptr_lastgob = s->pb.buf;
3001 s->ptr_last_mb_line = s->pb.buf;
e03c341e 3002 for(mb_y=0; mb_y < s->mb_height; mb_y++) {
f5957f3f
MN
3003 s->y_dc_scale= s->y_dc_scale_table[ s->qscale ];
3004 s->c_dc_scale= s->c_dc_scale_table[ s->qscale ];
e03c341e 3005
4278e7a6
MN
3006 s->block_index[0]= s->block_wrap[0]*(mb_y*2 + 1) - 1;
3007 s->block_index[1]= s->block_wrap[0]*(mb_y*2 + 1);
3008 s->block_index[2]= s->block_wrap[0]*(mb_y*2 + 2) - 1;
3009 s->block_index[3]= s->block_wrap[0]*(mb_y*2 + 2);
3010 s->block_index[4]= s->block_wrap[4]*(mb_y + 1) + s->block_wrap[0]*(s->mb_height*2 + 2);
3011 s->block_index[5]= s->block_wrap[4]*(mb_y + 1 + s->mb_height + 2) + s->block_wrap[0]*(s->mb_height*2 + 2);
37fbfd0a 3012 for(mb_x=0; mb_x < s->mb_width; mb_x++) {
67725183 3013 int mb_type= s->mb_type[mb_y * s->mb_width + mb_x];
91029be7 3014 const int xy= (mb_y+1) * (s->mb_width+2) + mb_x + 1;
7f2fe444 3015// int d;
ba6802de 3016 int dmin=10000000;
37fbfd0a
J
3017
3018 s->mb_x = mb_x;
3019 s->mb_y = mb_y;
4278e7a6
MN
3020 s->block_index[0]+=2;
3021 s->block_index[1]+=2;
3022 s->block_index[2]+=2;
3023 s->block_index[3]+=2;
3024 s->block_index[4]++;
3025 s->block_index[5]++;
945eeee1
MN
3026
3027 /* write gob / video packet header */
1d0d55da 3028#ifdef CONFIG_RISKY
945eeee1
MN
3029 if(s->rtp_mode){
3030 int current_packet_size, is_gob_start;
3031
3032 current_packet_size= pbBufPtr(&s->pb) - s->ptr_lastgob;
3033 is_gob_start=0;
3034
3035 if(s->codec_id==CODEC_ID_MPEG4){
3036 if(current_packet_size + s->mb_line_avgsize/s->mb_width >= s->rtp_payload_size
3037 && s->mb_y + s->mb_x>0){
3038
4d2858de 3039 if(s->partitioned_frame){
7f2fe444
MN
3040 ff_mpeg4_merge_partitions(s);
3041 ff_mpeg4_init_partitions(s);
3042 }
3043 ff_mpeg4_encode_video_packet_header(s);
3044
3045 if(s->flags&CODEC_FLAG_PASS1){
3046 int bits= get_bit_count(&s->pb);
3047 s->misc_bits+= bits - s->last_bits;
3048 s->last_bits= bits;
3049 }
3050 ff_mpeg4_clean_buffers(s);
945eeee1 3051 is_gob_start=1;
7f2fe444 3052 }
945eeee1
MN
3053 }else{
3054 if(current_packet_size + s->mb_line_avgsize*s->gob_index >= s->rtp_payload_size
3055 && s->mb_x==0 && s->mb_y>0 && s->mb_y%s->gob_index==0){
3056
3057 h263_encode_gob_header(s, mb_y);
3058 is_gob_start=1;
3059 }
3060 }
3061
3062 if(is_gob_start){
7f2fe444
MN
3063 s->ptr_lastgob = pbBufPtr(&s->pb);
3064 s->first_slice_line=1;
3065 s->resync_mb_x=mb_x;
3066 s->resync_mb_y=mb_y;
3067 }
4d2858de 3068 }
1d0d55da 3069#endif
7f2fe444 3070
4d2858de
MN
3071 if( (s->resync_mb_x == s->mb_x)
3072 && s->resync_mb_y+1 == s->mb_y){
3073 s->first_slice_line=0;
7f2fe444
MN
3074 }
3075
ba6802de 3076 if(mb_type & (mb_type-1)){ // more than 1 MB type possible
91029be7 3077 int next_block=0;
7f2fe444 3078 int pb_bits_count, pb2_bits_count, tex_pb_bits_count;
1f0cd30f
MN
3079
3080 copy_context_before_encode(&backup_s, s, -1);
7f2fe444
MN
3081 backup_s.pb= s->pb;
3082 best_s.data_partitioning= s->data_partitioning;
4d2858de 3083 best_s.partitioned_frame= s->partitioned_frame;
7f2fe444
MN
3084 if(s->data_partitioning){
3085 backup_s.pb2= s->pb2;
3086 backup_s.tex_pb= s->tex_pb;
3087 }
1f0cd30f 3088
ba6802de 3089 if(mb_type&MB_TYPE_INTER){
91029be7 3090 s->mv_dir = MV_DIR_FORWARD;
cf8039b2 3091 s->mv_type = MV_TYPE_16X16;
ba6802de 3092 s->mb_intra= 0;
9dbcbd92
MN
3093 s->mv[0][0][0] = s->p_mv_table[xy][0];
3094 s->mv[0][0][1] = s->p_mv_table[xy][1];
7f2fe444
MN
3095 encode_mb_hq(s, &backup_s, &best_s, MB_TYPE_INTER, pb, pb2, tex_pb,
3096 &dmin, &next_block, s->mv[0][0][0], s->mv[0][0][1]);
de6d9b64 3097 }
1f0cd30f 3098 if(mb_type&MB_TYPE_INTER4V){
91029be7 3099 s->mv_dir = MV_DIR_FORWARD;
cf8039b2
MN
3100 s->mv_type = MV_TYPE_8X8;
3101 s->mb_intra= 0;
3102 for(i=0; i<4; i++){
3103 s->mv[0][i][0] = s->motion_val[s->block_index[i]][0];
3104 s->mv[0][i][1] = s->motion_val[s->block_index[i]][1];
3105 }
7f2fe444
MN
3106 encode_mb_hq(s, &backup_s, &best_s, MB_TYPE_INTER4V, pb, pb2, tex_pb,
3107 &dmin, &next_block, 0, 0);
91029be7
MN
3108 }
3109 if(mb_type&MB_TYPE_FORWARD){
91029be7
MN
3110 s->mv_dir = MV_DIR_FORWARD;
3111 s->mv_type = MV_TYPE_16X16;
3112 s->mb_intra= 0;
3113 s->mv[0][0][0] = s->b_forw_mv_table[xy][0];
3114 s->mv[0][0][1] = s->b_forw_mv_table[xy][1];
7f2fe444
MN
3115 encode_mb_hq(s, &backup_s, &best_s, MB_TYPE_FORWARD, pb, pb2, tex_pb,
3116 &dmin, &next_block, s->mv[0][0][0], s->mv[0][0][1]);
91029be7
MN
3117 }
3118 if(mb_type&MB_TYPE_BACKWARD){
91029be7
MN
3119 s->mv_dir = MV_DIR_BACKWARD;
3120 s->mv_type = MV_TYPE_16X16;
3121 s->mb_intra= 0;
3122 s->mv[1][0][0] = s->b_back_mv_table[xy][0];
3123 s->mv[1][0][1] = s->b_back_mv_table[xy][1];
7f2fe444
MN
3124 encode_mb_hq(s, &backup_s, &best_s, MB_TYPE_BACKWARD, pb, pb2, tex_pb,
3125 &dmin, &next_block, s->mv[1][0][0], s->mv[1][0][1]);
91029be7
MN
3126 }
3127 if(mb_type&MB_TYPE_BIDIR){
91029be7
MN
3128 s->mv_dir = MV_DIR_FORWARD | MV_DIR_BACKWARD;
3129 s->mv_type = MV_TYPE_16X16;
3130 s->mb_intra= 0;
3131 s->mv[0][0][0] = s->b_bidir_forw_mv_table[xy][0];
3132 s->mv[0][0][1] = s->b_bidir_forw_mv_table[xy][1];
3133 s->mv[1][0][0] = s->b_bidir_back_mv_table[xy][0];
3134 s->mv[1][0][1] = s->b_bidir_back_mv_table[xy][1];
7f2fe444
MN
3135 encode_mb_hq(s, &backup_s, &best_s, MB_TYPE_BIDIR, pb, pb2, tex_pb,
3136 &dmin, &next_block, 0, 0);
91029be7
MN
3137 }
3138 if(mb_type&MB_TYPE_DIRECT){
1457ab52
MN
3139 int mx= s->b_direct_mv_table[xy][0];
3140 int my= s->b_direct_mv_table[xy][1];
3141
91029be7 3142 s->mv_dir = MV_DIR_FORWARD | MV_DIR_BACKWARD | MV_DIRECT;
91029be7 3143 s->mb_intra= 0;
1d0d55da 3144#ifdef CONFIG_RISKY
1457ab52 3145 ff_mpeg4_set_direct_mv(s, mx, my);
1d0d55da 3146#endif
7f2fe444 3147 encode_mb_hq(s, &backup_s, &best_s, MB_TYPE_DIRECT, pb, pb2, tex_pb,
1457ab52 3148 &dmin, &next_block, mx, my);
cf8039b2 3149 }
ba6802de 3150 if(mb_type&MB_TYPE_INTRA){
63b15e55 3151 s->mv_dir = 0;
cf8039b2 3152 s->mv_type = MV_TYPE_16X16;
ba6802de
MN
3153 s->mb_intra= 1;
3154 s->mv[0][0][0] = 0;
3155 s->mv[0][0][1] = 0;
7f2fe444
MN
3156 encode_mb_hq(s, &backup_s, &best_s, MB_TYPE_INTRA, pb, pb2, tex_pb,
3157 &dmin, &next_block, 0, 0);
1f0cd30f
MN
3158 /* force cleaning of ac/dc pred stuff if needed ... */
3159 if(s->h263_pred || s->h263_aic)
3160 s->mbintra_table[mb_x + mb_y*s->mb_width]=1;
cf8039b2 3161 }
1f0cd30f 3162 copy_context_after_encode(s, &best_s, -1);
7f2fe444
MN
3163
3164 pb_bits_count= get_bit_count(&s->pb);
3165 flush_put_bits(&s->pb);
3166 ff_copy_bits(&backup_s.pb, bit_buf[next_block^1], pb_bits_count);
3167 s->pb= backup_s.pb;
3168
3169 if(s->data_partitioning){
3170 pb2_bits_count= get_bit_count(&s->pb2);
3171 flush_put_bits(&s->pb2);
3172 ff_copy_bits(&backup_s.pb2, bit_buf2[next_block^1], pb2_bits_count);
3173 s->pb2= backup_s.pb2;
3174
3175 tex_pb_bits_count= get_bit_count(&s->tex_pb);
3176 flush_put_bits(&s->tex_pb);
3177 ff_copy_bits(&backup_s.tex_pb, bit_buf_tex[next_block^1], tex_pb_bits_count);
3178 s->tex_pb= backup_s.tex_pb;
3179 }
8b4c7dbc 3180 s->last_bits= get_bit_count(&s->pb);
de6d9b64 3181 } else {
9dbcbd92 3182 int motion_x, motion_y;
67725183
MN
3183 int intra_score;