Statistics
| Branch: | Revision:

ffmpeg / libavcodec / mpegvideo.c @ ff3dfbae

History | View | Annotate | Download (68.9 KB)

1
/*
2
 * The simplest mpeg encoder (well, it was the simplest!)
3
 * Copyright (c) 2000,2001 Gerard Lantau.
4
 *
5
 * This program is free software; you can redistribute it and/or modify
6
 * it under the terms of the GNU General Public License as published by
7
 * the Free Software Foundation; either version 2 of the License, or
8
 * (at your option) any later version.
9
 *
10
 * This program is distributed in the hope that it will be useful,
11
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13
 * GNU General Public License for more details.
14
 *
15
 * You should have received a copy of the GNU General Public License
16
 * along with this program; if not, write to the Free Software
17
 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18
 *
19
 * 4MV & hq encoding stuff by Michael Niedermayer <michaelni@gmx.at>
20
 */
21
#include <stdlib.h>
22
#include <stdio.h>
23
#include <math.h>
24
#include <string.h>
25
#include "avcodec.h"
26
#include "dsputil.h"
27
#include "mpegvideo.h"
28

    
29
#ifdef USE_FASTMEMCPY
30
#include "fastmemcpy.h"
31
#endif
32

    
33
static void encode_picture(MpegEncContext *s, int picture_number);
34
static void rate_control_init(MpegEncContext *s);
35
static int rate_estimate_qscale(MpegEncContext *s);
36
static void dct_unquantize_mpeg1_c(MpegEncContext *s, 
37
                                   DCTELEM *block, int n, int qscale);
38
static void dct_unquantize_h263_c(MpegEncContext *s, 
39
                                  DCTELEM *block, int n, int qscale);
40
static void draw_edges_c(UINT8 *buf, int wrap, int width, int height, int w);
41
static int dct_quantize_c(MpegEncContext *s, DCTELEM *block, int n, int qscale);
42

    
43
int (*dct_quantize)(MpegEncContext *s, DCTELEM *block, int n, int qscale)= dct_quantize_c;
44
void (*draw_edges)(UINT8 *buf, int wrap, int width, int height, int w)= draw_edges_c;
45

    
46
#define EDGE_WIDTH 16
47

    
48
/* enable all paranoid tests for rounding, overflows, etc... */
49
//#define PARANOID
50

    
51
//#define DEBUG
52

    
53
/* for jpeg fast DCT */
54
#define CONST_BITS 14
55

    
56
static const unsigned short aanscales[64] = {
57
    /* precomputed values scaled up by 14 bits */
58
    16384, 22725, 21407, 19266, 16384, 12873,  8867,  4520,
59
    22725, 31521, 29692, 26722, 22725, 17855, 12299,  6270,
60
    21407, 29692, 27969, 25172, 21407, 16819, 11585,  5906,
61
    19266, 26722, 25172, 22654, 19266, 15137, 10426,  5315,
62
    16384, 22725, 21407, 19266, 16384, 12873,  8867,  4520,
63
    12873, 17855, 16819, 15137, 12873, 10114,  6967,  3552,
64
    8867, 12299, 11585, 10426,  8867,  6967,  4799,  2446,
65
    4520,  6270,  5906,  5315,  4520,  3552,  2446,  1247
66
};
67

    
68
static UINT8 h263_chroma_roundtab[16] = {
69
    0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2,
70
};
71

    
72
static UINT16 default_mv_penalty[MAX_FCODE+1][MAX_MV*2+1];
73
static UINT8 default_fcode_tab[MAX_MV*2+1];
74

    
75
/* default motion estimation */
76
int motion_estimation_method = ME_LOG;
77

    
78
extern UINT8 zigzag_end[64];
79

    
80
static void convert_matrix(int *qmat, UINT16 *qmat16, const UINT16 *quant_matrix, int qscale)
81
{
82
    int i;
83

    
84
    if (av_fdct == jpeg_fdct_ifast) {
85
        for(i=0;i<64;i++) {
86
            /* 16 <= qscale * quant_matrix[i] <= 7905 */
87
            /* 19952         <= aanscales[i] * qscale * quant_matrix[i]           <= 249205026 */
88
            /* (1<<36)/19952 >= (1<<36)/(aanscales[i] * qscale * quant_matrix[i]) >= (1<<36)/249205026 */
89
            /* 3444240       >= (1<<36)/(aanscales[i] * qscale * quant_matrix[i]) >= 275 */
90
            
91
            qmat[block_permute_op(i)] = (int)((UINT64_C(1) << (QMAT_SHIFT + 11)) / 
92
                            (aanscales[i] * qscale * quant_matrix[block_permute_op(i)]));
93
        }
94
    } else {
95
        for(i=0;i<64;i++) {
96
            /* We can safely suppose that 16 <= quant_matrix[i] <= 255
97
               So 16           <= qscale * quant_matrix[i]             <= 7905
98
               so (1<<19) / 16 >= (1<<19) / (qscale * quant_matrix[i]) >= (1<<19) / 7905
99
               so 32768        >= (1<<19) / (qscale * quant_matrix[i]) >= 67
100
            */
101
            qmat[i]   = (1 << QMAT_SHIFT_MMX) / (qscale * quant_matrix[i]);
102
            qmat16[i] = (1 << QMAT_SHIFT_MMX) / (qscale * quant_matrix[block_permute_op(i)]);
103
        }
104
    }
105
}
106

    
107
/* init common structure for both encoder and decoder */
108
int MPV_common_init(MpegEncContext *s)
109
{
110
    int c_size, i;
111
    UINT8 *pict;
112

    
113
    if (s->out_format == FMT_H263) 
114
        s->dct_unquantize = dct_unquantize_h263_c;
115
    else
116
        s->dct_unquantize = dct_unquantize_mpeg1_c;
117
        
118
#ifdef HAVE_MMX
119
    MPV_common_init_mmx(s);
120
#endif
121
    s->mb_width = (s->width + 15) / 16;
122
    s->mb_height = (s->height + 15) / 16;
123
    s->mb_num = s->mb_width * s->mb_height;
124
    s->linesize = s->mb_width * 16 + 2 * EDGE_WIDTH;
125

    
126
    for(i=0;i<3;i++) {
127
        int w, h, shift, pict_start;
128

    
129
        w = s->linesize;
130
        h = s->mb_height * 16 + 2 * EDGE_WIDTH;
131
        shift = (i == 0) ? 0 : 1;
132
        c_size = (w >> shift) * (h >> shift);
133
        pict_start = (w >> shift) * (EDGE_WIDTH >> shift) + (EDGE_WIDTH >> shift);
134

    
135
        pict = av_mallocz(c_size);
136
        if (pict == NULL)
137
            goto fail;
138
        s->last_picture_base[i] = pict;
139
        s->last_picture[i] = pict + pict_start;
140
    
141
        pict = av_mallocz(c_size);
142
        if (pict == NULL)
143
            goto fail;
144
        s->next_picture_base[i] = pict;
145
        s->next_picture[i] = pict + pict_start;
146

    
147
        if (s->has_b_frames) {
148
            pict = av_mallocz(c_size);
149
            if (pict == NULL) 
150
                goto fail;
151
            s->aux_picture_base[i] = pict;
152
            s->aux_picture[i] = pict + pict_start;
153
        }
154
    }
155
    
156
    if (s->encoding) {
157
        /* Allocate MB type table */
158
        s->mb_type = av_mallocz(s->mb_num * sizeof(char));
159
        if (s->mb_type == NULL) {
160
            perror("malloc");
161
            goto fail;
162
        }
163
        
164
        s->mb_var = av_mallocz(s->mb_num * sizeof(INT16));
165
        if (s->mb_var == NULL) {
166
            perror("malloc");
167
            goto fail;
168
        }
169
        /* Allocate MV table */
170
        /* By now we just have one MV per MB */
171
        s->mv_table[0] = av_mallocz(s->mb_num * sizeof(INT16));
172
        s->mv_table[1] = av_mallocz(s->mb_num * sizeof(INT16));
173
        if (s->mv_table[1] == NULL || s->mv_table[0] == NULL) {
174
            perror("malloc");
175
            goto fail;
176
        }
177
    }
178
    
179
    if (s->out_format == FMT_H263 || s->encoding) {
180
        int size;
181
        /* MV prediction */
182
        size = (2 * s->mb_width + 2) * (2 * s->mb_height + 2);
183
        s->motion_val = malloc(size * 2 * sizeof(INT16));
184
        if (s->motion_val == NULL)
185
            goto fail;
186
        memset(s->motion_val, 0, size * 2 * sizeof(INT16));
187
    }
188

    
189
    if (s->h263_pred || s->h263_plus) {
190
        int y_size, c_size, i, size;
191
        
192
        /* dc values */
193

    
194
        y_size = (2 * s->mb_width + 2) * (2 * s->mb_height + 2);
195
        c_size = (s->mb_width + 2) * (s->mb_height + 2);
196
        size = y_size + 2 * c_size;
197
        s->dc_val[0] = malloc(size * sizeof(INT16));
198
        if (s->dc_val[0] == NULL)
199
            goto fail;
200
        s->dc_val[1] = s->dc_val[0] + y_size;
201
        s->dc_val[2] = s->dc_val[1] + c_size;
202
        for(i=0;i<size;i++)
203
            s->dc_val[0][i] = 1024;
204

    
205
        /* ac values */
206
        s->ac_val[0] = av_mallocz(size * sizeof(INT16) * 16);
207
        if (s->ac_val[0] == NULL)
208
            goto fail;
209
        s->ac_val[1] = s->ac_val[0] + y_size;
210
        s->ac_val[2] = s->ac_val[1] + c_size;
211
        
212
        /* cbp values */
213
        s->coded_block = av_mallocz(y_size);
214
        if (!s->coded_block)
215
            goto fail;
216

    
217
        /* which mb is a intra block */
218
        s->mbintra_table = av_mallocz(s->mb_num);
219
        if (!s->mbintra_table)
220
            goto fail;
221
        memset(s->mbintra_table, 1, s->mb_num);
222
    }
223
    /* default structure is frame */
224
    s->picture_structure = PICT_FRAME;
225

    
226
    /* init macroblock skip table */
227
    if (!s->encoding) {
228
        s->mbskip_table = av_mallocz(s->mb_num);
229
        if (!s->mbskip_table)
230
            goto fail;
231
    }
232
    
233
    s->block= s->intra_block;
234

    
235
    s->context_initialized = 1;
236
    return 0;
237
 fail:
238
    MPV_common_end(s);
239
    return -1;
240
}
241

    
242
/* init common structure for both encoder and decoder */
243
void MPV_common_end(MpegEncContext *s)
244
{
245
    int i;
246

    
247
    if (s->mb_type)
248
        free(s->mb_type);
249
    if (s->mb_var)
250
        free(s->mb_var);
251
    if (s->mv_table[0])
252
        free(s->mv_table[0]);
253
    if (s->mv_table[1])
254
        free(s->mv_table[1]);
255
    if (s->motion_val)
256
        free(s->motion_val);
257
    if (s->dc_val[0])
258
        free(s->dc_val[0]);
259
    if (s->ac_val[0])
260
        free(s->ac_val[0]);
261
    if (s->coded_block)
262
        free(s->coded_block);
263
    if (s->mbintra_table)
264
        free(s->mbintra_table);
265

    
266
    if (s->mbskip_table)
267
        free(s->mbskip_table);
268
    for(i=0;i<3;i++) {
269
        if (s->last_picture_base[i])
270
            free(s->last_picture_base[i]);
271
        if (s->next_picture_base[i])
272
            free(s->next_picture_base[i]);
273
        if (s->has_b_frames)
274
            free(s->aux_picture_base[i]);
275
    }
276
    s->context_initialized = 0;
277
}
278

    
279
/* init video encoder */
280
int MPV_encode_init(AVCodecContext *avctx)
281
{
282
    MpegEncContext *s = avctx->priv_data;
283
    int i;
284

    
285
    s->bit_rate = avctx->bit_rate;
286
    s->bit_rate_tolerance = avctx->bit_rate_tolerance;
287
    s->frame_rate = avctx->frame_rate;
288
    s->width = avctx->width;
289
    s->height = avctx->height;
290
    s->gop_size = avctx->gop_size;
291
    s->rtp_mode = avctx->rtp_mode;
292
    s->rtp_payload_size = avctx->rtp_payload_size;
293
    if (avctx->rtp_callback)
294
        s->rtp_callback = avctx->rtp_callback;
295
    s->qmin= avctx->qmin;
296
    s->qmax= avctx->qmax;
297
    s->max_qdiff= avctx->max_qdiff;
298
    s->qcompress= avctx->qcompress;
299
    s->qblur= avctx->qblur;
300
    s->avctx = avctx;
301
    s->aspect_ratio_info= avctx->aspect_ratio_info;
302
    s->flags= avctx->flags;
303
    
304
    if (s->gop_size <= 1) {
305
        s->intra_only = 1;
306
        s->gop_size = 12;
307
    } else {
308
        s->intra_only = 0;
309
    }
310
    s->full_search = motion_estimation_method;
311

    
312
    s->fixed_qscale = (avctx->flags & CODEC_FLAG_QSCALE);
313
    
314
    switch(avctx->codec->id) {
315
    case CODEC_ID_MPEG1VIDEO:
316
        s->out_format = FMT_MPEG1;
317
        break;
318
    case CODEC_ID_MJPEG:
319
        s->out_format = FMT_MJPEG;
320
        s->intra_only = 1; /* force intra only for jpeg */
321
        s->mjpeg_write_tables = 1; /* write all tables */
322
        s->mjpeg_vsample[0] = 2; /* set up default sampling factors */
323
        s->mjpeg_vsample[1] = 1; /* the only currently supported values */
324
        s->mjpeg_vsample[2] = 1; 
325
        s->mjpeg_hsample[0] = 2; 
326
        s->mjpeg_hsample[1] = 1; 
327
        s->mjpeg_hsample[2] = 1; 
328
        if (mjpeg_init(s) < 0)
329
            return -1;
330
        break;
331
    case CODEC_ID_H263:
332
        if (h263_get_picture_format(s->width, s->height) == 7) {
333
            printf("Input picture size isn't suitable for h263 codec! try h263+\n");
334
            return -1;
335
        }
336
        s->out_format = FMT_H263;
337
        break;
338
    case CODEC_ID_H263P:
339
        s->out_format = FMT_H263;
340
        s->rtp_mode = 1;
341
        s->rtp_payload_size = 1200; 
342
        s->h263_plus = 1;
343
        s->unrestricted_mv = 1;
344
        
345
        /* These are just to be sure */
346
        s->umvplus = 0;
347
        s->umvplus_dec = 0;
348
        break;
349
    case CODEC_ID_RV10:
350
        s->out_format = FMT_H263;
351
        s->h263_rv10 = 1;
352
        break;
353
    case CODEC_ID_MPEG4:
354
        s->out_format = FMT_H263;
355
        s->h263_pred = 1;
356
        s->unrestricted_mv = 1;
357
        break;
358
    case CODEC_ID_MSMPEG4:
359
        s->out_format = FMT_H263;
360
        s->h263_msmpeg4 = 1;
361
        s->h263_pred = 1;
362
        s->unrestricted_mv = 1;
363
        break;
364
    default:
365
        return -1;
366
    }
367
    
368
    if((s->flags&CODEC_FLAG_4MV) && !(s->flags&CODEC_FLAG_HQ)){
369
        printf("4MV is currently only supported in HQ mode\n");
370
        return -1;
371
    }
372

    
373
    { /* set up some save defaults, some codecs might override them later */
374
        static int done=0;
375
        if(!done){
376
            int i;
377
            done=1;
378
            memset(default_mv_penalty, 0, sizeof(UINT16)*(MAX_FCODE+1)*(2*MAX_MV+1));
379
            memset(default_fcode_tab , 0, sizeof(UINT8)*(2*MAX_MV+1));
380

    
381
            for(i=-16; i<16; i++){
382
                default_fcode_tab[i + MAX_MV]= 1;
383
            }
384
        }
385
    }
386
    s->mv_penalty= default_mv_penalty;
387
    s->fcode_tab= default_fcode_tab;
388

    
389
    if (s->out_format == FMT_H263)
390
        h263_encode_init(s);
391
    else if (s->out_format == FMT_MPEG1)
392
        mpeg1_encode_init(s);
393

    
394
    /* dont use mv_penalty table for crap MV as it would be confused */
395
    if(s->full_search<4) s->mv_penalty= default_mv_penalty;
396

    
397
    s->encoding = 1;
398

    
399
    /* init */
400
    if (MPV_common_init(s) < 0)
401
        return -1;
402
    
403
    /* init default q matrix */
404
    for(i=0;i<64;i++) {
405
        s->intra_matrix[i] = default_intra_matrix[i];
406
        s->non_intra_matrix[i] = default_non_intra_matrix[i];
407
    }
408

    
409
    /* rate control init */
410
    rate_control_init(s);
411

    
412
    s->picture_number = 0;
413
    s->picture_in_gop_number = 0;
414
    s->fake_picture_number = 0;
415
    /* motion detector init */
416
    s->f_code = 1;
417

    
418
    return 0;
419
}
420

    
421
int MPV_encode_end(AVCodecContext *avctx)
422
{
423
    MpegEncContext *s = avctx->priv_data;
424

    
425
#ifdef STATS
426
    print_stats();
427
#endif
428
    MPV_common_end(s);
429
    if (s->out_format == FMT_MJPEG)
430
        mjpeg_close(s);
431
      
432
    return 0;
433
}
434

    
435
/* draw the edges of width 'w' of an image of size width, height */
436
static void draw_edges_c(UINT8 *buf, int wrap, int width, int height, int w)
437
{
438
    UINT8 *ptr, *last_line;
439
    int i;
440

    
441
    last_line = buf + (height - 1) * wrap;
442
    for(i=0;i<w;i++) {
443
        /* top and bottom */
444
        memcpy(buf - (i + 1) * wrap, buf, width);
445
        memcpy(last_line + (i + 1) * wrap, last_line, width);
446
    }
447
    /* left and right */
448
    ptr = buf;
449
    for(i=0;i<height;i++) {
450
        memset(ptr - w, ptr[0], w);
451
        memset(ptr + width, ptr[width-1], w);
452
        ptr += wrap;
453
    }
454
    /* corners */
455
    for(i=0;i<w;i++) {
456
        memset(buf - (i + 1) * wrap - w, buf[0], w); /* top left */
457
        memset(buf - (i + 1) * wrap + width, buf[width-1], w); /* top right */
458
        memset(last_line + (i + 1) * wrap - w, last_line[0], w); /* top left */
459
        memset(last_line + (i + 1) * wrap + width, last_line[width-1], w); /* top right */
460
    }
461
}
462

    
463
/* generic function for encode/decode called before a frame is coded/decoded */
464
void MPV_frame_start(MpegEncContext *s)
465
{
466
    int i;
467
    UINT8 *tmp;
468

    
469
    s->mb_skiped = 0;
470
    if (s->pict_type == B_TYPE) {
471
        for(i=0;i<3;i++) {
472
            s->current_picture[i] = s->aux_picture[i];
473
        }
474
    } else {
475
        s->last_non_b_pict_type= s->pict_type;
476
        for(i=0;i<3;i++) {
477
            /* swap next and last */
478
            tmp = s->last_picture[i];
479
            s->last_picture[i] = s->next_picture[i];
480
            s->next_picture[i] = tmp;
481
            s->current_picture[i] = tmp;
482
        }
483
    }
484
}
485

    
486
/* generic function for encode/decode called after a frame has been coded/decoded */
487
void MPV_frame_end(MpegEncContext *s)
488
{
489
    /* draw edge for correct motion prediction if outside */
490
    if (s->pict_type != B_TYPE && !s->intra_only) {
491
      if(s->avctx==NULL || s->avctx->codec->id!=CODEC_ID_MPEG4 || s->divx_version==500){
492
        draw_edges(s->current_picture[0], s->linesize, s->mb_width*16, s->mb_height*16, EDGE_WIDTH);
493
        draw_edges(s->current_picture[1], s->linesize/2, s->mb_width*8, s->mb_height*8, EDGE_WIDTH/2);
494
        draw_edges(s->current_picture[2], s->linesize/2, s->mb_width*8, s->mb_height*8, EDGE_WIDTH/2);
495
      }else{
496
        /* mpeg4? / opendivx / xvid */
497
        draw_edges(s->current_picture[0], s->linesize, s->width, s->height, EDGE_WIDTH);
498
        draw_edges(s->current_picture[1], s->linesize/2, s->width/2, s->height/2, EDGE_WIDTH/2);
499
        draw_edges(s->current_picture[2], s->linesize/2, s->width/2, s->height/2, EDGE_WIDTH/2);
500
      }
501
    }
502
    emms_c();
503
}
504

    
505
int MPV_encode_picture(AVCodecContext *avctx,
506
                       unsigned char *buf, int buf_size, void *data)
507
{
508
    MpegEncContext *s = avctx->priv_data;
509
    AVPicture *pict = data;
510
    int i, j;
511

    
512
    if (s->fixed_qscale) 
513
        s->qscale = avctx->quality;
514

    
515
    init_put_bits(&s->pb, buf, buf_size, NULL, NULL);
516

    
517
    s->force_type= (avctx->flags&CODEC_FLAG_TYPE) ?
518
        (avctx->key_frame ? I_TYPE : P_TYPE) : 0;
519
    if (!s->intra_only) {
520
        /* first picture of GOP is intra */
521
        if (s->picture_in_gop_number % s->gop_size==0 || s->force_type==I_TYPE){
522
            s->picture_in_gop_number=0;
523
            s->pict_type = I_TYPE;
524
        }else
525
            s->pict_type = P_TYPE;
526
    } else {
527
        s->pict_type = I_TYPE;
528
    }
529
    
530
    MPV_frame_start(s);
531
    
532
    for(i=0;i<3;i++) {
533
        UINT8 *src = pict->data[i];
534
        UINT8 *dest = s->current_picture[i];
535
        int src_wrap = pict->linesize[i];
536
        int dest_wrap = s->linesize;
537
        int w = s->width;
538
        int h = s->height;
539

    
540
        if (i >= 1) {
541
            dest_wrap >>= 1;
542
            w >>= 1;
543
            h >>= 1;
544
        }
545

    
546
        if(dest_wrap==src_wrap){
547
            s->new_picture[i] = pict->data[i];
548
        } else {
549
            for(j=0;j<h;j++) {
550
                memcpy(dest, src, w);
551
                dest += dest_wrap;
552
                src += src_wrap;
553
            }
554
            s->new_picture[i] = s->current_picture[i];
555
            }
556
    }
557

    
558
    encode_picture(s, s->picture_number);
559
    avctx->key_frame = (s->pict_type == I_TYPE);
560
    avctx->header_bits = s->header_bits;
561
    avctx->mv_bits     = s->mv_bits;
562
    avctx->misc_bits   = s->misc_bits;
563
    avctx->i_tex_bits  = s->i_tex_bits;
564
    avctx->p_tex_bits  = s->p_tex_bits;
565
    avctx->i_count     = s->i_count;
566
    avctx->p_count     = s->p_count;
567
    avctx->skip_count  = s->skip_count;
568

    
569
    MPV_frame_end(s);
570
    s->picture_number++;
571
    s->picture_in_gop_number++;
572

    
573
    if (s->out_format == FMT_MJPEG)
574
        mjpeg_picture_trailer(s);
575

    
576
    flush_put_bits(&s->pb);
577
    s->last_frame_bits= s->frame_bits;
578
    s->frame_bits  = (pbBufPtr(&s->pb) - s->pb.buf) * 8;
579
    s->total_bits += s->frame_bits;
580
    avctx->frame_bits  = s->frame_bits;
581
//printf("fcode: %d, type: %d, head: %d, mv: %d, misc: %d, frame: %d, itex: %d, ptex: %d\n", 
582
//s->f_code, avctx->key_frame, s->header_bits, s->mv_bits, s->misc_bits, s->frame_bits, s->i_tex_bits, s->p_tex_bits);
583

    
584
    avctx->quality = s->qscale;
585
    if (avctx->get_psnr) {
586
        /* At this point pict->data should have the original frame   */
587
        /* an s->current_picture should have the coded/decoded frame */
588
        get_psnr(pict->data, s->current_picture,
589
                 pict->linesize, s->linesize, avctx);
590
    }
591
    return pbBufPtr(&s->pb) - s->pb.buf;
592
}
593

    
594
static inline int clip(int a, int amin, int amax)
595
{
596
    if (a < amin)
597
        return amin;
598
    else if (a > amax)
599
        return amax;
600
    else
601
        return a;
602
}
603

    
604
static inline void gmc1_motion(MpegEncContext *s,
605
                               UINT8 *dest_y, UINT8 *dest_cb, UINT8 *dest_cr,
606
                               int dest_offset,
607
                               UINT8 **ref_picture, int src_offset,
608
                               int h)
609
{
610
    UINT8 *ptr;
611
    int dxy, offset, mx, my, src_x, src_y, height, linesize;
612
    int motion_x, motion_y;
613

    
614
    if(s->real_sprite_warping_points>1) printf("more than 1 warp point isnt supported\n");
615
    motion_x= s->sprite_offset[0][0];
616
    motion_y= s->sprite_offset[0][1];
617
    src_x = s->mb_x * 16 + (motion_x >> (s->sprite_warping_accuracy+1));
618
    src_y = s->mb_y * 16 + (motion_y >> (s->sprite_warping_accuracy+1));
619
    motion_x<<=(3-s->sprite_warping_accuracy);
620
    motion_y<<=(3-s->sprite_warping_accuracy);
621
    src_x = clip(src_x, -16, s->width);
622
    if (src_x == s->width)
623
        motion_x =0;
624
    src_y = clip(src_y, -16, s->height);
625
    if (src_y == s->height)
626
        motion_y =0;
627
    
628
    linesize = s->linesize;
629
    ptr = ref_picture[0] + (src_y * linesize) + src_x + src_offset;
630

    
631
    dest_y+=dest_offset;
632
    gmc1(dest_y  , ptr  , linesize, h, motion_x&15, motion_y&15, s->no_rounding);
633
    gmc1(dest_y+8, ptr+8, linesize, h, motion_x&15, motion_y&15, s->no_rounding);
634

    
635
    motion_x= s->sprite_offset[1][0];
636
    motion_y= s->sprite_offset[1][1];
637
    src_x = s->mb_x * 8 + (motion_x >> (s->sprite_warping_accuracy+1));
638
    src_y = s->mb_y * 8 + (motion_y >> (s->sprite_warping_accuracy+1));
639
    motion_x<<=(3-s->sprite_warping_accuracy);
640
    motion_y<<=(3-s->sprite_warping_accuracy);
641
    src_x = clip(src_x, -8, s->width>>1);
642
    if (src_x == s->width>>1)
643
        motion_x =0;
644
    src_y = clip(src_y, -8, s->height>>1);
645
    if (src_y == s->height>>1)
646
        motion_y =0;
647

    
648
    offset = (src_y * linesize>>1) + src_x + (src_offset>>1);
649
    ptr = ref_picture[1] + offset;
650
    gmc1(dest_cb + (dest_offset>>1), ptr, linesize>>1, h>>1, motion_x&15, motion_y&15, s->no_rounding);
651
    ptr = ref_picture[2] + offset;
652
    gmc1(dest_cr + (dest_offset>>1), ptr, linesize>>1, h>>1, motion_x&15, motion_y&15, s->no_rounding);
653
    
654
    return;
655
}
656

    
657
/* apply one mpeg motion vector to the three components */
658
static inline void mpeg_motion(MpegEncContext *s,
659
                               UINT8 *dest_y, UINT8 *dest_cb, UINT8 *dest_cr,
660
                               int dest_offset,
661
                               UINT8 **ref_picture, int src_offset,
662
                               int field_based, op_pixels_func *pix_op,
663
                               int motion_x, int motion_y, int h)
664
{
665
    UINT8 *ptr;
666
    int dxy, offset, mx, my, src_x, src_y, height, linesize;
667
if(s->quarter_sample)
668
{
669
    motion_x>>=1;
670
    motion_y>>=1;
671
}
672
    dxy = ((motion_y & 1) << 1) | (motion_x & 1);
673
    src_x = s->mb_x * 16 + (motion_x >> 1);
674
    src_y = s->mb_y * (16 >> field_based) + (motion_y >> 1);
675
                
676
    /* WARNING: do no forget half pels */
677
    height = s->height >> field_based;
678
    src_x = clip(src_x, -16, s->width);
679
    if (src_x == s->width)
680
        dxy &= ~1;
681
    src_y = clip(src_y, -16, height);
682
    if (src_y == height)
683
        dxy &= ~2;
684
    linesize = s->linesize << field_based;
685
    ptr = ref_picture[0] + (src_y * linesize) + (src_x) + src_offset;
686
    dest_y += dest_offset;
687
    pix_op[dxy](dest_y, ptr, linesize, h);
688
    pix_op[dxy](dest_y + 8, ptr + 8, linesize, h);
689

    
690
    if (s->out_format == FMT_H263) {
691
        dxy = 0;
692
        if ((motion_x & 3) != 0)
693
            dxy |= 1;
694
        if ((motion_y & 3) != 0)
695
            dxy |= 2;
696
        mx = motion_x >> 2;
697
        my = motion_y >> 2;
698
    } else {
699
        mx = motion_x / 2;
700
        my = motion_y / 2;
701
        dxy = ((my & 1) << 1) | (mx & 1);
702
        mx >>= 1;
703
        my >>= 1;
704
    }
705
    
706
    src_x = s->mb_x * 8 + mx;
707
    src_y = s->mb_y * (8 >> field_based) + my;
708
    src_x = clip(src_x, -8, s->width >> 1);
709
    if (src_x == (s->width >> 1))
710
        dxy &= ~1;
711
    src_y = clip(src_y, -8, height >> 1);
712
    if (src_y == (height >> 1))
713
        dxy &= ~2;
714

    
715
    offset = (src_y * (linesize >> 1)) + src_x + (src_offset >> 1);
716
    ptr = ref_picture[1] + offset;
717
    pix_op[dxy](dest_cb + (dest_offset >> 1), ptr, linesize >> 1, h >> 1);
718
    ptr = ref_picture[2] + offset;
719
    pix_op[dxy](dest_cr + (dest_offset >> 1), ptr, linesize >> 1, h >> 1);
720
}
721

    
722
static inline void qpel_motion(MpegEncContext *s,
723
                               UINT8 *dest_y, UINT8 *dest_cb, UINT8 *dest_cr,
724
                               int dest_offset,
725
                               UINT8 **ref_picture, int src_offset,
726
                               int field_based, op_pixels_func *pix_op,
727
                               qpel_mc_func *qpix_op,
728
                               int motion_x, int motion_y, int h)
729
{
730
    UINT8 *ptr;
731
    int dxy, offset, mx, my, src_x, src_y, height, linesize;
732

    
733
    dxy = ((motion_y & 3) << 2) | (motion_x & 3);
734
    src_x = s->mb_x * 16 + (motion_x >> 2);
735
    src_y = s->mb_y * (16 >> field_based) + (motion_y >> 2);
736

    
737
    height = s->height >> field_based;
738
    src_x = clip(src_x, -16, s->width);
739
    if (src_x == s->width)
740
        dxy &= ~3;
741
    src_y = clip(src_y, -16, height);
742
    if (src_y == height)
743
        dxy &= ~12;
744
    linesize = s->linesize << field_based;
745
    ptr = ref_picture[0] + (src_y * linesize) + src_x + src_offset;
746
    dest_y += dest_offset;
747
//printf("%d %d %d\n", src_x, src_y, dxy);
748
    qpix_op[dxy](dest_y                 , ptr                 , linesize, linesize, motion_x&3, motion_y&3);
749
    qpix_op[dxy](dest_y              + 8, ptr              + 8, linesize, linesize, motion_x&3, motion_y&3);
750
    qpix_op[dxy](dest_y + linesize*8    , ptr + linesize*8    , linesize, linesize, motion_x&3, motion_y&3);
751
    qpix_op[dxy](dest_y + linesize*8 + 8, ptr + linesize*8 + 8, linesize, linesize, motion_x&3, motion_y&3);
752
    
753
    mx= (motion_x>>1) | (motion_x&1);
754
    my= (motion_y>>1) | (motion_y&1);
755

    
756
    dxy = 0;
757
    if ((mx & 3) != 0)
758
        dxy |= 1;
759
    if ((my & 3) != 0)
760
        dxy |= 2;
761
    mx = mx >> 2;
762
    my = my >> 2;
763
    
764
    src_x = s->mb_x * 8 + mx;
765
    src_y = s->mb_y * (8 >> field_based) + my;
766
    src_x = clip(src_x, -8, s->width >> 1);
767
    if (src_x == (s->width >> 1))
768
        dxy &= ~1;
769
    src_y = clip(src_y, -8, height >> 1);
770
    if (src_y == (height >> 1))
771
        dxy &= ~2;
772

    
773
    offset = (src_y * (linesize >> 1)) + src_x + (src_offset >> 1);
774
    ptr = ref_picture[1] + offset;
775
    pix_op[dxy](dest_cb + (dest_offset >> 1), ptr, linesize >> 1, h >> 1);
776
    ptr = ref_picture[2] + offset;
777
    pix_op[dxy](dest_cr + (dest_offset >> 1), ptr, linesize >> 1, h >> 1);
778
}
779

    
780

    
781
static inline void MPV_motion(MpegEncContext *s, 
782
                              UINT8 *dest_y, UINT8 *dest_cb, UINT8 *dest_cr,
783
                              int dir, UINT8 **ref_picture, 
784
                              op_pixels_func *pix_op, qpel_mc_func *qpix_op)
785
{
786
    int dxy, offset, mx, my, src_x, src_y, motion_x, motion_y;
787
    int mb_x, mb_y, i;
788
    UINT8 *ptr, *dest;
789

    
790
    mb_x = s->mb_x;
791
    mb_y = s->mb_y;
792

    
793
    switch(s->mv_type) {
794
    case MV_TYPE_16X16:
795
        if(s->mcsel){
796
#if 0
797
            mpeg_motion(s, dest_y, dest_cb, dest_cr, 0,
798
                        ref_picture, 0,
799
                        0, pix_op,
800
                        s->sprite_offset[0][0]>>3,
801
                        s->sprite_offset[0][1]>>3,
802
                        16);
803
#else
804
            gmc1_motion(s, dest_y, dest_cb, dest_cr, 0,
805
                        ref_picture, 0,
806
                        16);
807
#endif
808
        }else if(s->quarter_sample && dir==0){ //FIXME
809
            qpel_motion(s, dest_y, dest_cb, dest_cr, 0,
810
                        ref_picture, 0,
811
                        0, pix_op, qpix_op,
812
                        s->mv[dir][0][0], s->mv[dir][0][1], 16);
813
        }else{
814
            mpeg_motion(s, dest_y, dest_cb, dest_cr, 0,
815
                        ref_picture, 0,
816
                        0, pix_op,
817
                        s->mv[dir][0][0], s->mv[dir][0][1], 16);
818
        }           
819
        break;
820
    case MV_TYPE_8X8:
821
        for(i=0;i<4;i++) {
822
            motion_x = s->mv[dir][i][0];
823
            motion_y = s->mv[dir][i][1];
824

    
825
            dxy = ((motion_y & 1) << 1) | (motion_x & 1);
826
            src_x = mb_x * 16 + (motion_x >> 1) + (i & 1) * 8;
827
            src_y = mb_y * 16 + (motion_y >> 1) + (i >>1) * 8;
828
                    
829
            /* WARNING: do no forget half pels */
830
            src_x = clip(src_x, -16, s->width);
831
            if (src_x == s->width)
832
                dxy &= ~1;
833
            src_y = clip(src_y, -16, s->height);
834
            if (src_y == s->height)
835
                dxy &= ~2;
836
                    
837
            ptr = ref_picture[0] + (src_y * s->linesize) + (src_x);
838
            dest = dest_y + ((i & 1) * 8) + (i >> 1) * 8 * s->linesize;
839
            pix_op[dxy](dest, ptr, s->linesize, 8);
840
        }
841
        /* In case of 8X8, we construct a single chroma motion vector
842
           with a special rounding */
843
        mx = 0;
844
        my = 0;
845
        for(i=0;i<4;i++) {
846
            mx += s->mv[dir][i][0];
847
            my += s->mv[dir][i][1];
848
        }
849
        if (mx >= 0)
850
            mx = (h263_chroma_roundtab[mx & 0xf] + ((mx >> 3) & ~1));
851
        else {
852
            mx = -mx;
853
            mx = -(h263_chroma_roundtab[mx & 0xf] + ((mx >> 3) & ~1));
854
        }
855
        if (my >= 0)
856
            my = (h263_chroma_roundtab[my & 0xf] + ((my >> 3) & ~1));
857
        else {
858
            my = -my;
859
            my = -(h263_chroma_roundtab[my & 0xf] + ((my >> 3) & ~1));
860
        }
861
        dxy = ((my & 1) << 1) | (mx & 1);
862
        mx >>= 1;
863
        my >>= 1;
864

    
865
        src_x = mb_x * 8 + mx;
866
        src_y = mb_y * 8 + my;
867
        src_x = clip(src_x, -8, s->width/2);
868
        if (src_x == s->width/2)
869
            dxy &= ~1;
870
        src_y = clip(src_y, -8, s->height/2);
871
        if (src_y == s->height/2)
872
            dxy &= ~2;
873
        
874
        offset = (src_y * (s->linesize >> 1)) + src_x;
875
        ptr = ref_picture[1] + offset;
876
        pix_op[dxy](dest_cb, ptr, s->linesize >> 1, 8);
877
        ptr = ref_picture[2] + offset;
878
        pix_op[dxy](dest_cr, ptr, s->linesize >> 1, 8);
879
        break;
880
    case MV_TYPE_FIELD:
881
        if (s->picture_structure == PICT_FRAME) {
882
            /* top field */
883
            mpeg_motion(s, dest_y, dest_cb, dest_cr, 0,
884
                        ref_picture, s->field_select[dir][0] ? s->linesize : 0,
885
                        1, pix_op,
886
                        s->mv[dir][0][0], s->mv[dir][0][1], 8);
887
            /* bottom field */
888
            mpeg_motion(s, dest_y, dest_cb, dest_cr, s->linesize,
889
                        ref_picture, s->field_select[dir][1] ? s->linesize : 0,
890
                        1, pix_op,
891
                        s->mv[dir][1][0], s->mv[dir][1][1], 8);
892
        } else {
893
            
894

    
895
        }
896
        break;
897
    }
898
}
899

    
900

    
901
/* put block[] to dest[] */
902
static inline void put_dct(MpegEncContext *s, 
903
                           DCTELEM *block, int i, UINT8 *dest, int line_size)
904
{
905
    if (!s->mpeg2)
906
        s->dct_unquantize(s, block, i, s->qscale);
907
    ff_idct (block);
908
    put_pixels_clamped(block, dest, line_size);
909
}
910

    
911
/* add block[] to dest[] */
912
static inline void add_dct(MpegEncContext *s, 
913
                           DCTELEM *block, int i, UINT8 *dest, int line_size)
914
{
915
    if (s->block_last_index[i] >= 0) {
916
        if (!s->mpeg2)
917
            if(s->encoding || (!s->h263_msmpeg4))
918
                s->dct_unquantize(s, block, i, s->qscale);
919
        ff_idct (block);
920
        add_pixels_clamped(block, dest, line_size);
921
    }
922
}
923

    
924
/* generic function called after a macroblock has been parsed by the
925
   decoder or after it has been encoded by the encoder.
926

927
   Important variables used:
928
   s->mb_intra : true if intra macroblock
929
   s->mv_dir   : motion vector direction
930
   s->mv_type  : motion vector type
931
   s->mv       : motion vector
932
   s->interlaced_dct : true if interlaced dct used (mpeg2)
933
 */
934
void MPV_decode_mb(MpegEncContext *s, DCTELEM block[6][64])
935
{
936
    int mb_x, mb_y;
937
    int dct_linesize, dct_offset;
938
    op_pixels_func *op_pix;
939
    qpel_mc_func *op_qpix;
940

    
941
    mb_x = s->mb_x;
942
    mb_y = s->mb_y;
943

    
944
#ifdef FF_POSTPROCESS
945
    quant_store[mb_y][mb_x]=s->qscale;
946
    //printf("[%02d][%02d] %d\n",mb_x,mb_y,s->qscale);
947
#endif
948

    
949
    /* update DC predictors for P macroblocks */
950
    if (!s->mb_intra) {
951
        if (s->h263_pred || s->h263_aic) {
952
          if(s->mbintra_table[mb_x + mb_y*s->mb_width])
953
          {
954
            int wrap, xy, v;
955
            s->mbintra_table[mb_x + mb_y*s->mb_width]=0;
956
            wrap = 2 * s->mb_width + 2;
957
            xy = 2 * mb_x + 1 +  (2 * mb_y + 1) * wrap;
958
            v = 1024;
959
            
960
            s->dc_val[0][xy] = v;
961
            s->dc_val[0][xy + 1] = v;
962
            s->dc_val[0][xy + wrap] = v;
963
            s->dc_val[0][xy + 1 + wrap] = v;
964
            /* ac pred */
965
            memset(s->ac_val[0][xy], 0, 16 * sizeof(INT16));
966
            memset(s->ac_val[0][xy + 1], 0, 16 * sizeof(INT16));
967
            memset(s->ac_val[0][xy + wrap], 0, 16 * sizeof(INT16));
968
            memset(s->ac_val[0][xy + 1 + wrap], 0, 16 * sizeof(INT16));
969
            if (s->h263_msmpeg4) {
970
                s->coded_block[xy] = 0;
971
                s->coded_block[xy + 1] = 0;
972
                s->coded_block[xy + wrap] = 0;
973
                s->coded_block[xy + 1 + wrap] = 0;
974
            }
975
            /* chroma */
976
            wrap = s->mb_width + 2;
977
            xy = mb_x + 1 + (mb_y + 1) * wrap;
978
            s->dc_val[1][xy] = v;
979
            s->dc_val[2][xy] = v;
980
            /* ac pred */
981
            memset(s->ac_val[1][xy], 0, 16 * sizeof(INT16));
982
            memset(s->ac_val[2][xy], 0, 16 * sizeof(INT16));
983
          }
984
        } else {
985
            s->last_dc[0] = 128 << s->intra_dc_precision;
986
            s->last_dc[1] = 128 << s->intra_dc_precision;
987
            s->last_dc[2] = 128 << s->intra_dc_precision;
988
        }
989
    }
990
    else if (s->h263_pred || s->h263_aic)
991
        s->mbintra_table[mb_x + mb_y*s->mb_width]=1;
992

    
993
    /* update motion predictor, not for B-frames as they need the motion_val from the last P/S-Frame */
994
    if (s->out_format == FMT_H263) {
995
      if(s->pict_type!=B_TYPE){
996
        int xy, wrap, motion_x, motion_y;
997
        
998
        wrap = 2 * s->mb_width + 2;
999
        xy = 2 * mb_x + 1 + (2 * mb_y + 1) * wrap;
1000
        if (s->mb_intra) {
1001
            motion_x = 0;
1002
            motion_y = 0;
1003
            goto motion_init;
1004
        } else if (s->mv_type == MV_TYPE_16X16) {
1005
            motion_x = s->mv[0][0][0];
1006
            motion_y = s->mv[0][0][1];
1007
        motion_init:
1008
            /* no update if 8X8 because it has been done during parsing */
1009
            s->motion_val[xy][0] = motion_x;
1010
            s->motion_val[xy][1] = motion_y;
1011
            s->motion_val[xy + 1][0] = motion_x;
1012
            s->motion_val[xy + 1][1] = motion_y;
1013
            s->motion_val[xy + wrap][0] = motion_x;
1014
            s->motion_val[xy + wrap][1] = motion_y;
1015
            s->motion_val[xy + 1 + wrap][0] = motion_x;
1016
            s->motion_val[xy + 1 + wrap][1] = motion_y;
1017
        }
1018
      }
1019
    }
1020
    
1021
    if (!s->intra_only) {
1022
        UINT8 *dest_y, *dest_cb, *dest_cr;
1023
        UINT8 *mbskip_ptr;
1024

    
1025
        /* avoid copy if macroblock skipped in last frame too */
1026
        if (!s->encoding && s->pict_type != B_TYPE) {
1027
            mbskip_ptr = &s->mbskip_table[s->mb_y * s->mb_width + s->mb_x];
1028
            if (s->mb_skiped) {
1029
                s->mb_skiped = 0;
1030
                /* if previous was skipped too, then nothing to do ! */
1031
                if (*mbskip_ptr != 0) 
1032
                    goto the_end;
1033
                *mbskip_ptr = 1; /* indicate that this time we skiped it */
1034
            } else {
1035
                *mbskip_ptr = 0; /* not skipped */
1036
            }
1037
        }
1038

    
1039
        dest_y = s->current_picture[0] + (mb_y * 16 * s->linesize) + mb_x * 16;
1040
        dest_cb = s->current_picture[1] + (mb_y * 8 * (s->linesize >> 1)) + mb_x * 8;
1041
        dest_cr = s->current_picture[2] + (mb_y * 8 * (s->linesize >> 1)) + mb_x * 8;
1042

    
1043
        if (s->interlaced_dct) {
1044
            dct_linesize = s->linesize * 2;
1045
            dct_offset = s->linesize;
1046
        } else {
1047
            dct_linesize = s->linesize;
1048
            dct_offset = s->linesize * 8;
1049
        }
1050

    
1051
        if (!s->mb_intra) {
1052
            /* motion handling */
1053
            if (!s->no_rounding){
1054
                op_pix = put_pixels_tab;
1055
                op_qpix= qpel_mc_rnd_tab;
1056
            }else{
1057
                op_pix = put_no_rnd_pixels_tab;
1058
                op_qpix= qpel_mc_no_rnd_tab;
1059
            }
1060

    
1061
            if (s->mv_dir & MV_DIR_FORWARD) {
1062
                MPV_motion(s, dest_y, dest_cb, dest_cr, 0, s->last_picture, op_pix, op_qpix);
1063
                if (!s->no_rounding) 
1064
                    op_pix = avg_pixels_tab;
1065
                else
1066
                    op_pix = avg_no_rnd_pixels_tab;
1067
            }
1068
            if (s->mv_dir & MV_DIR_BACKWARD) {
1069
                MPV_motion(s, dest_y, dest_cb, dest_cr, 1, s->next_picture, op_pix, op_qpix);
1070
            }
1071

    
1072
            /* add dct residue */
1073
            add_dct(s, block[0], 0, dest_y, dct_linesize);
1074
            add_dct(s, block[1], 1, dest_y + 8, dct_linesize);
1075
            add_dct(s, block[2], 2, dest_y + dct_offset, dct_linesize);
1076
            add_dct(s, block[3], 3, dest_y + dct_offset + 8, dct_linesize);
1077

    
1078
            add_dct(s, block[4], 4, dest_cb, s->linesize >> 1);
1079
            add_dct(s, block[5], 5, dest_cr, s->linesize >> 1);
1080
        } else {
1081
            /* dct only in intra block */
1082
            put_dct(s, block[0], 0, dest_y, dct_linesize);
1083
            put_dct(s, block[1], 1, dest_y + 8, dct_linesize);
1084
            put_dct(s, block[2], 2, dest_y + dct_offset, dct_linesize);
1085
            put_dct(s, block[3], 3, dest_y + dct_offset + 8, dct_linesize);
1086

    
1087
            put_dct(s, block[4], 4, dest_cb, s->linesize >> 1);
1088
            put_dct(s, block[5], 5, dest_cr, s->linesize >> 1);
1089
        }
1090
    }
1091
 the_end:
1092
    emms_c(); //FIXME remove
1093
}
1094

    
1095
static void encode_mb(MpegEncContext *s)
1096
{
1097
    int wrap;
1098
    const int mb_x= s->mb_x;
1099
    const int mb_y= s->mb_y;
1100
    UINT8 *ptr;
1101
    const int motion_x= s->mv[0][0][0];
1102
    const int motion_y= s->mv[0][0][1];
1103
    int i;
1104

    
1105
    /* get the pixels */
1106
    wrap = s->linesize;
1107
    ptr = s->new_picture[0] + (mb_y * 16 * wrap) + mb_x * 16;
1108
    get_pixels(s->block[0], ptr, wrap);
1109
    get_pixels(s->block[1], ptr + 8, wrap);
1110
    get_pixels(s->block[2], ptr + 8 * wrap, wrap);
1111
    get_pixels(s->block[3], ptr + 8 * wrap + 8, wrap);
1112
    wrap = s->linesize >> 1;
1113
    ptr = s->new_picture[1] + (mb_y * 8 * wrap) + mb_x * 8;
1114
    get_pixels(s->block[4], ptr, wrap);
1115

    
1116
    wrap = s->linesize >> 1;
1117
    ptr = s->new_picture[2] + (mb_y * 8 * wrap) + mb_x * 8;
1118
    get_pixels(s->block[5], ptr, wrap);
1119

    
1120
    /* subtract previous frame if non intra */
1121
    if (!s->mb_intra) {
1122
        int dxy, offset, mx, my;
1123
        
1124
        if(s->mv_type==MV_TYPE_16X16){
1125
            dxy = ((motion_y & 1) << 1) | (motion_x & 1);
1126
            ptr = s->last_picture[0] + 
1127
                ((mb_y * 16 + (motion_y >> 1)) * s->linesize) + 
1128
                (mb_x * 16 + (motion_x >> 1));
1129

    
1130
            sub_pixels_2(s->block[0], ptr, s->linesize, dxy);
1131
            sub_pixels_2(s->block[1], ptr + 8, s->linesize, dxy);
1132
            sub_pixels_2(s->block[2], ptr + s->linesize * 8, s->linesize, dxy);
1133
            sub_pixels_2(s->block[3], ptr + 8 + s->linesize * 8, s->linesize ,dxy);
1134

    
1135
            if (s->out_format == FMT_H263) {
1136
                /* special rounding for h263 */
1137
                dxy = 0;
1138
                if ((motion_x & 3) != 0)
1139
                    dxy |= 1;
1140
                if ((motion_y & 3) != 0)
1141
                    dxy |= 2;
1142
                mx = motion_x >> 2;
1143
                my = motion_y >> 2;
1144
            } else {
1145
                mx = motion_x / 2;
1146
                my = motion_y / 2;
1147
                dxy = ((my & 1) << 1) | (mx & 1);
1148
                mx >>= 1;
1149
                my >>= 1;
1150
            }
1151
            offset = ((mb_y * 8 + my) * (s->linesize >> 1)) + (mb_x * 8 + mx);
1152
            ptr = s->last_picture[1] + offset;
1153
            sub_pixels_2(s->block[4], ptr, s->linesize >> 1, dxy);
1154
            ptr = s->last_picture[2] + offset;
1155
            sub_pixels_2(s->block[5], ptr, s->linesize >> 1, dxy);
1156
        }else{
1157
            int src_x, src_y;
1158

    
1159
            for(i=0;i<4;i++) {
1160
                int motion_x = s->mv[0][i][0];
1161
                int motion_y = s->mv[0][i][1];
1162

    
1163
                dxy = ((motion_y & 1) << 1) | (motion_x & 1);
1164
                src_x = mb_x * 16 + (motion_x >> 1) + (i & 1) * 8;
1165
                src_y = mb_y * 16 + (motion_y >> 1) + (i >>1) * 8;
1166
                        
1167
                ptr = s->last_picture[0] + (src_y * s->linesize) + (src_x);
1168
                sub_pixels_2(s->block[i], ptr, s->linesize, dxy);
1169
            }
1170
            /* In case of 8X8, we construct a single chroma motion vector
1171
               with a special rounding */
1172
            mx = 0;
1173
            my = 0;
1174
            for(i=0;i<4;i++) {
1175
                mx += s->mv[0][i][0];
1176
                my += s->mv[0][i][1];
1177
            }
1178
            if (mx >= 0)
1179
                mx = (h263_chroma_roundtab[mx & 0xf] + ((mx >> 3) & ~1));
1180
            else {
1181
                mx = -mx;
1182
                mx = -(h263_chroma_roundtab[mx & 0xf] + ((mx >> 3) & ~1));
1183
            }
1184
            if (my >= 0)
1185
                my = (h263_chroma_roundtab[my & 0xf] + ((my >> 3) & ~1));
1186
            else {
1187
                my = -my;
1188
                my = -(h263_chroma_roundtab[my & 0xf] + ((my >> 3) & ~1));
1189
            }
1190
            dxy = ((my & 1) << 1) | (mx & 1);
1191
            mx >>= 1;
1192
            my >>= 1;
1193

    
1194
            src_x = mb_x * 8 + mx;
1195
            src_y = mb_y * 8 + my;
1196
            src_x = clip(src_x, -8, s->width/2);
1197
            if (src_x == s->width/2)
1198
                dxy &= ~1;
1199
            src_y = clip(src_y, -8, s->height/2);
1200
            if (src_y == s->height/2)
1201
                dxy &= ~2;
1202
            
1203
            offset = (src_y * (s->linesize >> 1)) + src_x;
1204
            ptr = s->last_picture[1] + offset;
1205
            sub_pixels_2(s->block[4], ptr, s->linesize >> 1, dxy);
1206
            ptr = s->last_picture[2] + offset;
1207
            sub_pixels_2(s->block[5], ptr, s->linesize >> 1, dxy);
1208
        }
1209
    }
1210
            
1211
#if 0
1212
            {
1213
                float adap_parm;
1214
                
1215
                adap_parm = ((s->avg_mb_var << 1) + s->mb_var[s->mb_width*mb_y+mb_x] + 1.0) /
1216
                            ((s->mb_var[s->mb_width*mb_y+mb_x] << 1) + s->avg_mb_var + 1.0);
1217
            
1218
                printf("\ntype=%c qscale=%2d adap=%0.2f dquant=%4.2f var=%4d avgvar=%4d", 
1219
                        (s->mb_type[s->mb_width*mb_y+mb_x] > 0) ? 'I' : 'P', 
1220
                        s->qscale, adap_parm, s->qscale*adap_parm,
1221
                        s->mb_var[s->mb_width*mb_y+mb_x], s->avg_mb_var);
1222
            }
1223
#endif
1224
    /* DCT & quantize */
1225
    if (s->h263_msmpeg4) {
1226
        msmpeg4_dc_scale(s);
1227
    } else if (s->h263_pred) {
1228
        h263_dc_scale(s);
1229
    } else {
1230
        /* default quantization values */
1231
        s->y_dc_scale = 8;
1232
        s->c_dc_scale = 8;
1233
    }
1234
    for(i=0;i<6;i++) {
1235
        s->block_last_index[i] = dct_quantize(s, s->block[i], i, s->qscale);
1236
    }
1237

    
1238
    /* huffman encode */
1239
    switch(s->out_format) {
1240
    case FMT_MPEG1:
1241
        mpeg1_encode_mb(s, s->block, motion_x, motion_y);
1242
        break;
1243
    case FMT_H263:
1244
        if (s->h263_msmpeg4)
1245
            msmpeg4_encode_mb(s, s->block, motion_x, motion_y);
1246
        else if(s->h263_pred)
1247
            mpeg4_encode_mb(s, s->block, motion_x, motion_y);
1248
        else
1249
            h263_encode_mb(s, s->block, motion_x, motion_y);
1250
        break;
1251
    case FMT_MJPEG:
1252
        mjpeg_encode_mb(s, s->block);
1253
        break;
1254
    }
1255
}
1256

    
1257
static void copy_bits(PutBitContext *pb, UINT8 *src, int length)
1258
{
1259
    int bytes= length>>3;
1260
    int bits= length&7;
1261
    int i;
1262

    
1263
    for(i=0; i<bytes; i++) put_bits(pb, 8, src[i]);
1264
    put_bits(pb, bits, src[i]>>(8-bits));
1265
}
1266

    
1267
static void encode_picture(MpegEncContext *s, int picture_number)
1268
{
1269
    int mb_x, mb_y, last_gob, pdif = 0;
1270
    int i;
1271
    int bits;
1272
    MpegEncContext best_s;
1273
    UINT8 bit_buf[4][3000]; //FIXME check that this is ALLWAYS large enogh for a MB
1274

    
1275
    s->picture_number = picture_number;
1276

    
1277
    s->block_wrap[0]=
1278
    s->block_wrap[1]=
1279
    s->block_wrap[2]=
1280
    s->block_wrap[3]= s->mb_width*2 + 2;
1281
    s->block_wrap[4]=
1282
    s->block_wrap[5]= s->mb_width + 2;
1283
    
1284
    s->last_mc_mb_var = s->mc_mb_var;
1285
    /* Reset the average MB variance */
1286
    s->avg_mb_var = 0;
1287
    s->mc_mb_var = 0;
1288
    /* Estimate motion for every MB */
1289
    if(s->pict_type == P_TYPE){
1290
        for(mb_y=0; mb_y < s->mb_height; mb_y++) {
1291
            s->block_index[0]= s->block_wrap[0]*(mb_y*2 + 1) - 1;
1292
            s->block_index[1]= s->block_wrap[0]*(mb_y*2 + 1);
1293
            s->block_index[2]= s->block_wrap[0]*(mb_y*2 + 2) - 1;
1294
            s->block_index[3]= s->block_wrap[0]*(mb_y*2 + 2);
1295
            for(mb_x=0; mb_x < s->mb_width; mb_x++) {
1296
                s->mb_x = mb_x;
1297
                s->mb_y = mb_y;
1298
                s->block_index[0]+=2;
1299
                s->block_index[1]+=2;
1300
                s->block_index[2]+=2;
1301
                s->block_index[3]+=2;
1302

    
1303
                /* compute motion vector & mb_type and store in context */
1304
                estimate_motion(s, mb_x, mb_y);
1305
//                s->mb_type[mb_y*s->mb_width + mb_x]=MB_TYPE_INTER;
1306
            }
1307
        }
1308
        emms_c();
1309
    }else{
1310
        /* I-Frame */
1311
        //FIXME do we need to zero them?
1312
        memset(s->motion_val[0], 0, sizeof(INT16)*(s->mb_width*2 + 2)*(s->mb_height*2 + 2)*2);
1313
        memset(s->mv_table[0]  , 0, sizeof(INT16)*s->mb_width*s->mb_height);
1314
        memset(s->mv_table[1]  , 0, sizeof(INT16)*s->mb_width*s->mb_height);
1315
        memset(s->mb_type      , MB_TYPE_INTRA, sizeof(UINT8)*s->mb_width*s->mb_height);
1316
    }
1317

    
1318
    if(s->avg_mb_var < s->mc_mb_var && s->pict_type != B_TYPE && (!s->force_type)){ //FIXME subtract MV bits
1319
        s->pict_type= I_TYPE;
1320
        s->picture_in_gop_number=0;
1321
        memset(s->mb_type   , MB_TYPE_INTRA, sizeof(UINT8)*s->mb_width*s->mb_height);
1322
//printf("Scene change detected, encoding as I Frame\n");
1323
    }
1324

    
1325
    /* find best f_code for ME which do unlimited searches */
1326
    if(s->pict_type==P_TYPE && s->full_search>3){
1327
        int mv_num[8];
1328
        int i;
1329
        int loose=0;
1330
        UINT8 * fcode_tab= s->fcode_tab;
1331

    
1332
        for(i=0; i<8; i++) mv_num[i]=0;
1333

    
1334
        for(i=0; i<s->mb_num; i++){
1335
            if(s->mb_type[i] & MB_TYPE_INTER){
1336
                mv_num[ fcode_tab[s->mv_table[0][i] + MAX_MV] ]++;
1337
                mv_num[ fcode_tab[s->mv_table[1][i] + MAX_MV] ]++;
1338
//printf("%d %d %d\n", s->mv_table[0][i], fcode_tab[s->mv_table[0][i] + MAX_MV], i);
1339
            }
1340
//else printf("I");
1341
        }
1342

    
1343
        for(i=MAX_FCODE; i>1; i--){
1344
            loose+= mv_num[i];
1345
            if(loose > 10) break; //FIXME this is pretty ineffective
1346
        }
1347
        s->f_code= i;
1348
/*        for(i=0; i<=MAX_FCODE; i++){
1349
            printf("%d ", mv_num[i]);
1350
        }
1351
        printf("\n");*/
1352
    }else{
1353
        s->f_code= 1;
1354
    }
1355

    
1356
//printf("f_code %d ///\n", s->f_code);
1357
    /* convert MBs with too long MVs to I-Blocks */
1358
    if(s->pict_type==P_TYPE){
1359
        int i, x, y;
1360
        const int f_code= s->f_code;
1361
        UINT8 * fcode_tab= s->fcode_tab;
1362
//FIXME try to clip instead of intra izing ;)
1363
        /* clip / convert to intra 16x16 type MVs */
1364
        for(i=0; i<s->mb_num; i++){
1365
            if(s->mb_type[i]&MB_TYPE_INTER){
1366
                if(   fcode_tab[s->mv_table[0][i] + MAX_MV] > f_code
1367
                   || fcode_tab[s->mv_table[0][i] + MAX_MV] == 0
1368
                   || fcode_tab[s->mv_table[1][i] + MAX_MV] > f_code
1369
                   || fcode_tab[s->mv_table[1][i] + MAX_MV] == 0 ){
1370
                    s->mb_type[i] &= ~MB_TYPE_INTER;
1371
                    s->mb_type[i] |= MB_TYPE_INTRA;
1372
                    s->mv_table[0][i] = 0;
1373
                    s->mv_table[1][i] = 0;
1374
                }
1375
            }
1376
        }
1377

    
1378
        if(s->flags&CODEC_FLAG_4MV){
1379
            int wrap= 2+ s->mb_width*2;
1380

    
1381
            /* clip / convert to intra 8x8 type MVs */
1382
            for(y=0; y<s->mb_height; y++){
1383
                int xy= (y*2 + 1)*wrap + 1;
1384
                i= y*s->mb_width;
1385

    
1386
                for(x=0; x<s->mb_width; x++){
1387
                    if(s->mb_type[i]&MB_TYPE_INTER4V){
1388
                        int block;
1389
                        for(block=0; block<4; block++){
1390
                            int off= (block& 1) + (block>>1)*wrap;
1391
                            int mx= s->motion_val[ xy + off ][0];
1392
                            int my= s->motion_val[ xy + off ][1];
1393

    
1394
                            if(   fcode_tab[mx + MAX_MV] > f_code
1395
                               || fcode_tab[mx + MAX_MV] == 0
1396
                               || fcode_tab[my + MAX_MV] > f_code
1397
                               || fcode_tab[my + MAX_MV] == 0 ){
1398
                                s->mb_type[i] &= ~MB_TYPE_INTER4V;
1399
                                s->mb_type[i] |= MB_TYPE_INTRA;
1400
                            }
1401
                        }
1402
                        xy+=2;
1403
                        i++;
1404
                    }
1405
                }
1406
            }
1407
        }
1408
    }
1409

    
1410
//    printf("%d %d\n", s->avg_mb_var, s->mc_mb_var);
1411

    
1412
    if (!s->fixed_qscale) 
1413
        s->qscale = rate_estimate_qscale(s);
1414

    
1415
    /* precompute matrix */
1416
    if (s->out_format == FMT_MJPEG) {
1417
        /* for mjpeg, we do include qscale in the matrix */
1418
        s->intra_matrix[0] = default_intra_matrix[0];
1419
        for(i=1;i<64;i++)
1420
            s->intra_matrix[i] = (default_intra_matrix[i] * s->qscale) >> 3;
1421
        convert_matrix(s->q_intra_matrix, s->q_intra_matrix16, s->intra_matrix, 8);
1422
    } else {
1423
        convert_matrix(s->q_intra_matrix, s->q_intra_matrix16, s->intra_matrix, s->qscale);
1424
        convert_matrix(s->q_non_intra_matrix, s->q_non_intra_matrix16, s->non_intra_matrix, s->qscale);
1425
    }
1426

    
1427
    s->last_bits= get_bit_count(&s->pb);
1428
    switch(s->out_format) {
1429
    case FMT_MJPEG:
1430
        mjpeg_picture_header(s);
1431
        break;
1432
    case FMT_H263:
1433
        if (s->h263_msmpeg4) 
1434
            msmpeg4_encode_picture_header(s, picture_number);
1435
        else if (s->h263_pred)
1436
            mpeg4_encode_picture_header(s, picture_number);
1437
        else if (s->h263_rv10) 
1438
            rv10_encode_picture_header(s, picture_number);
1439
        else
1440
            h263_encode_picture_header(s, picture_number);
1441
        break;
1442
    case FMT_MPEG1:
1443
        mpeg1_encode_picture_header(s, picture_number);
1444
        break;
1445
    }
1446
    bits= get_bit_count(&s->pb);
1447
    s->header_bits= bits - s->last_bits;
1448
    s->last_bits= bits;
1449
    s->mv_bits=0;
1450
    s->misc_bits=0;
1451
    s->i_tex_bits=0;
1452
    s->p_tex_bits=0;
1453
    s->i_count=0;
1454
    s->p_count=0;
1455
    s->skip_count=0;
1456

    
1457
    /* init last dc values */
1458
    /* note: quant matrix value (8) is implied here */
1459
    s->last_dc[0] = 128;
1460
    s->last_dc[1] = 128;
1461
    s->last_dc[2] = 128;
1462
    s->mb_incr = 1;
1463
    s->last_mv[0][0][0] = 0;
1464
    s->last_mv[0][0][1] = 0;
1465

    
1466
    /* Get the GOB height based on picture height */
1467
    if (s->out_format == FMT_H263 && !s->h263_pred && !s->h263_msmpeg4) {
1468
        if (s->height <= 400)
1469
            s->gob_index = 1;
1470
        else if (s->height <= 800)
1471
            s->gob_index = 2;
1472
        else
1473
            s->gob_index = 4;
1474
    }
1475
        
1476
    s->avg_mb_var = s->avg_mb_var / s->mb_num;        
1477
    
1478
    for(mb_y=0; mb_y < s->mb_height; mb_y++) {
1479
        /* Put GOB header based on RTP MTU */
1480
        /* TODO: Put all this stuff in a separate generic function */
1481
        if (s->rtp_mode) {
1482
            if (!mb_y) {
1483
                s->ptr_lastgob = s->pb.buf;
1484
                s->ptr_last_mb_line = s->pb.buf;
1485
            } else if (s->out_format == FMT_H263 && !s->h263_pred && !s->h263_msmpeg4 && !(mb_y % s->gob_index)) {
1486
                last_gob = h263_encode_gob_header(s, mb_y);
1487
                if (last_gob) {
1488
                    s->first_gob_line = 1;
1489
                }
1490
            }
1491
        }
1492
        
1493
        s->block_index[0]= s->block_wrap[0]*(mb_y*2 + 1) - 1;
1494
        s->block_index[1]= s->block_wrap[0]*(mb_y*2 + 1);
1495
        s->block_index[2]= s->block_wrap[0]*(mb_y*2 + 2) - 1;
1496
        s->block_index[3]= s->block_wrap[0]*(mb_y*2 + 2);
1497
        s->block_index[4]= s->block_wrap[4]*(mb_y + 1)                    + s->block_wrap[0]*(s->mb_height*2 + 2);
1498
        s->block_index[5]= s->block_wrap[4]*(mb_y + 1 + s->mb_height + 2) + s->block_wrap[0]*(s->mb_height*2 + 2);
1499
        for(mb_x=0; mb_x < s->mb_width; mb_x++) {
1500
            const int mb_type= s->mb_type[mb_y * s->mb_width + mb_x];
1501
            PutBitContext pb;
1502
            int d;
1503
            int dmin=10000000;
1504
            int best=0;
1505

    
1506
            s->mb_x = mb_x;
1507
            s->mb_y = mb_y;
1508
            s->block_index[0]+=2;
1509
            s->block_index[1]+=2;
1510
            s->block_index[2]+=2;
1511
            s->block_index[3]+=2;
1512
            s->block_index[4]++;
1513
            s->block_index[5]++;
1514

    
1515
            s->mv_dir = MV_DIR_FORWARD;
1516
            if(mb_type & (mb_type-1)){ // more than 1 MB type possible
1517
                pb= s->pb;
1518
                if(mb_type&MB_TYPE_INTER){
1519
                    s->mv_type = MV_TYPE_16X16;
1520
                    s->mb_intra= 0;
1521
                    s->mv[0][0][0] = s->mv_table[0][mb_y * s->mb_width + mb_x];
1522
                    s->mv[0][0][1] = s->mv_table[1][mb_y * s->mb_width + mb_x];
1523
                    init_put_bits(&s->pb, bit_buf[1], 3000, NULL, NULL);
1524
                    s->block= s->inter_block;
1525

    
1526
                    encode_mb(s);
1527
                    d= get_bit_count(&s->pb);
1528
                    if(d<dmin){
1529
                        flush_put_bits(&s->pb);
1530
                        dmin=d;
1531
                        best_s.mv[0][0][0]= s->mv[0][0][0];
1532
                        best_s.mv[0][0][1]= s->mv[0][0][1];
1533
                        best_s.mb_intra= 0;
1534
                        best_s.mv_type = MV_TYPE_16X16;
1535
                        best_s.pb=s->pb;
1536
                        best_s.block= s->block;
1537
                        best=1;
1538
                        for(i=0; i<6; i++)
1539
                            best_s.block_last_index[i]= s->block_last_index[i];
1540
                    }
1541
                }
1542
                if(mb_type&MB_TYPE_INTER4V){
1543
                    s->mv_type = MV_TYPE_8X8;
1544
                    s->mb_intra= 0;
1545
                    for(i=0; i<4; i++){
1546
                        s->mv[0][i][0] = s->motion_val[s->block_index[i]][0];
1547
                        s->mv[0][i][1] = s->motion_val[s->block_index[i]][1];
1548
                    }
1549
                    init_put_bits(&s->pb, bit_buf[2], 3000, NULL, NULL);
1550
                    s->block= s->inter4v_block;
1551

    
1552
                    encode_mb(s);
1553
                    d= get_bit_count(&s->pb);
1554
                    if(d<dmin){
1555
                        flush_put_bits(&s->pb);
1556
                        dmin=d;
1557
                        for(i=0; i<4; i++){
1558
                            best_s.mv[0][i][0] = s->mv[0][i][0];
1559
                            best_s.mv[0][i][1] = s->mv[0][i][1];
1560
                        }
1561
                        best_s.mb_intra= 0;
1562
                        best_s.mv_type = MV_TYPE_8X8;
1563
                        best_s.pb=s->pb;
1564
                        best_s.block= s->block;
1565
                        best=2;
1566
                        for(i=0; i<6; i++)
1567
                            best_s.block_last_index[i]= s->block_last_index[i];
1568
                    }
1569
                }
1570
                if(mb_type&MB_TYPE_INTRA){
1571
                    s->mv_type = MV_TYPE_16X16;
1572
                    s->mb_intra= 1;
1573
                    s->mv[0][0][0] = 0;
1574
                    s->mv[0][0][1] = 0;
1575
                    init_put_bits(&s->pb, bit_buf[0], 3000, NULL, NULL);
1576
                    s->block= s->intra_block;
1577
                   
1578
                    encode_mb(s);
1579
                    d= get_bit_count(&s->pb);
1580
                    if(d<dmin){
1581
                        flush_put_bits(&s->pb);
1582
                        dmin=d;
1583
                        best_s.mv[0][0][0]= 0;
1584
                        best_s.mv[0][0][1]= 0;
1585
                        best_s.mb_intra= 1;
1586
                        best_s.mv_type = MV_TYPE_16X16;
1587
                        best_s.pb=s->pb;
1588
                        best_s.block= s->block;
1589
                        for(i=0; i<6; i++)
1590
                            best_s.block_last_index[i]= s->block_last_index[i];
1591
                        best=0;
1592
                    }
1593
                    /* force cleaning of ac/dc if needed ... */
1594
                    s->mbintra_table[mb_x + mb_y*s->mb_width]=1;
1595
                }
1596
                for(i=0; i<4; i++){
1597
                   s->mv[0][i][0] =  best_s.mv[0][i][0];
1598
                   s->mv[0][i][1] =  best_s.mv[0][i][1];
1599
                }
1600
                s->mb_intra= best_s.mb_intra;
1601
                s->mv_type= best_s.mv_type;
1602
                for(i=0; i<6; i++)
1603
                   s->block_last_index[i]= best_s.block_last_index[i];
1604
                copy_bits(&pb, bit_buf[best], dmin);
1605
                s->block= best_s.block;
1606
                s->pb= pb;
1607
            } else {
1608
                // only one MB-Type possible
1609
                if(mb_type&MB_TYPE_INTRA){
1610
                    s->mb_intra= 1;
1611
                    s->mv[0][0][0] = 0;
1612
                    s->mv[0][0][1] = 0;
1613
                }else{
1614
                    s->mb_intra= 0;
1615
                    s->mv[0][0][0] = s->mv_table[0][mb_y * s->mb_width + mb_x];
1616
                    s->mv[0][0][1] = s->mv_table[1][mb_y * s->mb_width + mb_x];
1617
                }
1618
                encode_mb(s);
1619
            }
1620

    
1621
            MPV_decode_mb(s, s->block);
1622
        }
1623

    
1624

    
1625
        /* Obtain average GOB size for RTP */
1626
        if (s->rtp_mode) {
1627
            if (!mb_y)
1628
                s->mb_line_avgsize = pbBufPtr(&s->pb) - s->ptr_last_mb_line;
1629
            else if (!(mb_y % s->gob_index)) {    
1630
                s->mb_line_avgsize = (s->mb_line_avgsize + pbBufPtr(&s->pb) - s->ptr_last_mb_line) >> 1;
1631
                s->ptr_last_mb_line = pbBufPtr(&s->pb);
1632
            }
1633
            //fprintf(stderr, "\nMB line: %d\tSize: %u\tAvg. Size: %u", s->mb_y, 
1634
            //                    (s->pb.buf_ptr - s->ptr_last_mb_line), s->mb_line_avgsize);
1635
            s->first_gob_line = 0;
1636
        }
1637
    }
1638
    emms_c();
1639

    
1640
    if (s->h263_msmpeg4 && s->pict_type == I_TYPE)
1641
        msmpeg4_encode_ext_header(s);
1642

    
1643
    //if (s->gob_number)
1644
    //    fprintf(stderr,"\nNumber of GOB: %d", s->gob_number);
1645
    
1646
    /* Send the last GOB if RTP */    
1647
    if (s->rtp_mode) {
1648
        flush_put_bits(&s->pb);
1649
        pdif = pbBufPtr(&s->pb) - s->ptr_lastgob;
1650
        /* Call the RTP callback to send the last GOB */
1651
        if (s->rtp_callback)
1652
            s->rtp_callback(s->ptr_lastgob, pdif, s->gob_number);
1653
        s->ptr_lastgob = pbBufPtr(&s->pb);
1654
        //fprintf(stderr,"\nGOB: %2d size: %d (last)", s->gob_number, pdif);
1655
    }
1656
}
1657

    
1658
static int dct_quantize_c(MpegEncContext *s, 
1659
                        DCTELEM *block, int n,
1660
                        int qscale)
1661
{
1662
    int i, j, level, last_non_zero, q;
1663
    const int *qmat;
1664
    int minLevel, maxLevel;
1665

    
1666
    if(s->avctx!=NULL && s->avctx->codec->id==CODEC_ID_MPEG4){
1667
        /* mpeg4 */
1668
        minLevel= -2048;
1669
        maxLevel= 2047;
1670
    }else if(s->out_format==FMT_MPEG1){
1671
        /* mpeg1 */
1672
        minLevel= -255;
1673
        maxLevel= 255;
1674
    }else if(s->out_format==FMT_MJPEG){
1675
        /* (m)jpeg */
1676
        minLevel= -1023;
1677
        maxLevel= 1023;
1678
    }else{
1679
        /* h263 / msmpeg4 */
1680
        minLevel= -128;
1681
        maxLevel= 127;
1682
    }
1683

    
1684
    av_fdct (block);
1685

    
1686
    /* we need this permutation so that we correct the IDCT
1687
       permutation. will be moved into DCT code */
1688
    block_permute(block);
1689

    
1690
    if (s->mb_intra) {
1691
        if (n < 4)
1692
            q = s->y_dc_scale;
1693
        else
1694
            q = s->c_dc_scale;
1695
        q = q << 3;
1696
        
1697
        /* note: block[0] is assumed to be positive */
1698
        block[0] = (block[0] + (q >> 1)) / q;
1699
        i = 1;
1700
        last_non_zero = 0;
1701
        if (s->out_format == FMT_H263) {
1702
            qmat = s->q_non_intra_matrix;
1703
        } else {
1704
            qmat = s->q_intra_matrix;
1705
        }
1706
    } else {
1707
        i = 0;
1708
        last_non_zero = -1;
1709
        qmat = s->q_non_intra_matrix;
1710
    }
1711

    
1712
    for(;i<64;i++) {
1713
        j = zigzag_direct[i];
1714
        level = block[j];
1715
        level = level * qmat[j];
1716
#ifdef PARANOID
1717
        {
1718
            static int count = 0;
1719
            int level1, level2, qmat1;
1720
            double val;
1721
            if (qmat == s->q_non_intra_matrix) {
1722
                qmat1 = default_non_intra_matrix[j] * s->qscale;
1723
            } else {
1724
                qmat1 = default_intra_matrix[j] * s->qscale;
1725
            }
1726
            if (av_fdct != jpeg_fdct_ifast)
1727
                val = ((double)block[j] * 8.0) / (double)qmat1;
1728
            else
1729
                val = ((double)block[j] * 8.0 * 2048.0) / 
1730
                    ((double)qmat1 * aanscales[j]);
1731
            level1 = (int)val;
1732
            level2 = level / (1 << (QMAT_SHIFT - 3));
1733
            if (level1 != level2) {
1734
                fprintf(stderr, "%d: quant error qlevel=%d wanted=%d level=%d qmat1=%d qmat=%d wantedf=%0.6f\n", 
1735
                        count, level2, level1, block[j], qmat1, qmat[j],
1736
                        val);
1737
                count++;
1738
            }
1739

    
1740
        }
1741
#endif
1742
        /* XXX: slight error for the low range. Test should be equivalent to
1743
           (level <= -(1 << (QMAT_SHIFT - 3)) || level >= (1 <<
1744
           (QMAT_SHIFT - 3)))
1745
        */
1746
        if (((level << (31 - (QMAT_SHIFT - 3))) >> (31 - (QMAT_SHIFT - 3))) != 
1747
            level) {
1748
            level = level / (1 << (QMAT_SHIFT - 3));
1749
            /* XXX: currently, this code is not optimal. the range should be:
1750
               mpeg1: -255..255
1751
               mpeg2: -2048..2047
1752
               h263:  -128..127
1753
               mpeg4: -2048..2047
1754
            */
1755
            if (level > maxLevel)
1756
                level = maxLevel;
1757
            else if (level < minLevel)
1758
                level = minLevel;
1759

    
1760
            block[j] = level;
1761
            last_non_zero = i;
1762
        } else {
1763
            block[j] = 0;
1764
        }
1765
    }
1766
    return last_non_zero;
1767
}
1768

    
1769
static void dct_unquantize_mpeg1_c(MpegEncContext *s, 
1770
                                   DCTELEM *block, int n, int qscale)
1771
{
1772
    int i, level, nCoeffs;
1773
    const UINT16 *quant_matrix;
1774

    
1775
    if(s->alternate_scan) nCoeffs= 64;
1776
    else nCoeffs= s->block_last_index[n]+1;
1777
    
1778
    if (s->mb_intra) {
1779
        if (n < 4) 
1780
            block[0] = block[0] * s->y_dc_scale;
1781
        else
1782
            block[0] = block[0] * s->c_dc_scale;
1783
        /* XXX: only mpeg1 */
1784
        quant_matrix = s->intra_matrix;
1785
        for(i=1;i<nCoeffs;i++) {
1786
            int j= zigzag_direct[i];
1787
            level = block[j];
1788
            if (level) {
1789
                if (level < 0) {
1790
                    level = -level;
1791
                    level = (int)(level * qscale * quant_matrix[j]) >> 3;
1792
                    level = (level - 1) | 1;
1793
                    level = -level;
1794
                } else {
1795
                    level = (int)(level * qscale * quant_matrix[j]) >> 3;
1796
                    level = (level - 1) | 1;
1797
                }
1798
#ifdef PARANOID
1799
                if (level < -2048 || level > 2047)
1800
                    fprintf(stderr, "unquant error %d %d\n", i, level);
1801
#endif
1802
                block[j] = level;
1803
            }
1804
        }
1805
    } else {
1806
        i = 0;
1807
        quant_matrix = s->non_intra_matrix;
1808
        for(;i<nCoeffs;i++) {
1809
            int j= zigzag_direct[i];
1810
            level = block[j];
1811
            if (level) {
1812
                if (level < 0) {
1813
                    level = -level;
1814
                    level = (((level << 1) + 1) * qscale *
1815
                             ((int) (quant_matrix[j]))) >> 4;
1816
                    level = (level - 1) | 1;
1817
                    level = -level;
1818
                } else {
1819
                    level = (((level << 1) + 1) * qscale *
1820
                             ((int) (quant_matrix[j]))) >> 4;
1821
                    level = (level - 1) | 1;
1822
                }
1823
#ifdef PARANOID
1824
                if (level < -2048 || level > 2047)
1825
                    fprintf(stderr, "unquant error %d %d\n", i, level);
1826
#endif
1827
                block[j] = level;
1828
            }
1829
        }
1830
    }
1831
}
1832

    
1833
static void dct_unquantize_h263_c(MpegEncContext *s, 
1834
                                  DCTELEM *block, int n, int qscale)
1835
{
1836
    int i, level, qmul, qadd;
1837
    int nCoeffs;
1838
    
1839
    if (s->mb_intra) {
1840
        if (!s->h263_aic) {
1841
            if (n < 4) 
1842
                block[0] = block[0] * s->y_dc_scale;
1843
            else
1844
                block[0] = block[0] * s->c_dc_scale;
1845
        }
1846
        i = 1;
1847
        nCoeffs= 64; //does not allways use zigzag table 
1848
    } else {
1849
        i = 0;
1850
        nCoeffs= zigzag_end[ s->block_last_index[n] ];
1851
    }
1852

    
1853
    qmul = s->qscale << 1;
1854
    if (s->h263_aic && s->mb_intra)
1855
        qadd = 0;
1856
    else
1857
        qadd = (s->qscale - 1) | 1;
1858

    
1859
    for(;i<nCoeffs;i++) {
1860
        level = block[i];
1861
        if (level) {
1862
            if (level < 0) {
1863
                level = level * qmul - qadd;
1864
            } else {
1865
                level = level * qmul + qadd;
1866
            }
1867
#ifdef PARANOID
1868
                if (level < -2048 || level > 2047)
1869
                    fprintf(stderr, "unquant error %d %d\n", i, level);
1870
#endif
1871
            block[i] = level;
1872
        }
1873
    }
1874
}
1875

    
1876
/* rate control */
1877

    
1878
/* an I frame is I_FRAME_SIZE_RATIO bigger than a P frame */
1879
#define I_FRAME_SIZE_RATIO 3.0
1880
#define QSCALE_K           20
1881

    
1882
static void rate_control_init(MpegEncContext *s)
1883
{
1884
#if 1
1885
    emms_c();
1886

    
1887
    //initial values, they dont really matter as they will be totally different within a few frames
1888
    s->i_pred.coeff= s->p_pred.coeff= 7.0;
1889
    s->i_pred.count= s->p_pred.count= 1.0;
1890
    
1891
    s->i_pred.decay= s->p_pred.decay= 0.4;
1892
    
1893
    // use more bits at the beginning, otherwise high motion at the begin will look like shit
1894
    s->qsum=100;
1895
    s->qcount=100;
1896

    
1897
    s->short_term_qsum=0.001;
1898
    s->short_term_qcount=0.001;
1899
#else
1900
    s->wanted_bits = 0;
1901

    
1902
    if (s->intra_only) {
1903
        s->I_frame_bits = ((INT64)s->bit_rate * FRAME_RATE_BASE) / s->frame_rate;
1904
        s->P_frame_bits = s->I_frame_bits;
1905
    } else {
1906
        s->P_frame_bits = (int) ((float)(s->gop_size * s->bit_rate) / 
1907
                                 (float)((float)s->frame_rate / FRAME_RATE_BASE * (I_FRAME_SIZE_RATIO + s->gop_size - 1)));
1908
        s->I_frame_bits = (int)(s->P_frame_bits * I_FRAME_SIZE_RATIO);
1909
    }
1910

    
1911
#if defined(DEBUG)
1912
    printf("I_frame_size=%d P_frame_size=%d\n",
1913
           s->I_frame_bits, s->P_frame_bits);
1914
#endif
1915
#endif
1916
}
1917

    
1918
static double predict(Predictor *p, double q, double var)
1919
{
1920
    return p->coeff*var / (q*p->count);
1921
}
1922

    
1923
static void update_predictor(Predictor *p, double q, double var, double size)
1924
{
1925
    double new_coeff= size*q / (var + 1);
1926
    if(var<1000) return;
1927
/*{
1928
int pred= predict(p, q, var);
1929
int error= abs(pred-size);
1930
static double sum=0;
1931
static int count=0;
1932
if(count>5) sum+=error;
1933
count++;
1934
if(256*256*256*64%count==0){
1935
    printf("%d %f %f\n", count, sum/count, p->coeff);
1936
}
1937
}*/
1938
    p->count*= p->decay;
1939
    p->coeff*= p->decay;
1940
    p->count++;
1941
    p->coeff+= new_coeff;
1942
}
1943

    
1944
static int rate_estimate_qscale(MpegEncContext *s)
1945
{
1946
#if 1
1947
    int qmin= s->qmin;
1948
    int qmax= s->qmax;
1949
    int rate_q=5;
1950
    float q;
1951
    int qscale;
1952
    float br_compensation;
1953
    double diff;
1954
    double short_term_q;
1955
    double long_term_q;
1956
    int last_qscale= s->qscale;
1957
    double fps;
1958
    INT64 wanted_bits;
1959
    emms_c();
1960

    
1961
    fps= (double)s->frame_rate / FRAME_RATE_BASE;
1962
    wanted_bits= s->bit_rate*(double)s->picture_number/fps;
1963

    
1964
    
1965
    if(s->picture_number>2){
1966
        /* update predictors */
1967
        if(s->last_pict_type == I_TYPE){
1968
        //FIXME
1969
        }else{ //P Frame
1970
//printf("%d %d %d %f\n", s->qscale, s->last_mc_mb_var, s->frame_bits, s->p_pred.coeff);
1971
            update_predictor(&s->p_pred, s->qscale, s->last_mc_mb_var, s->frame_bits);
1972
        }
1973
    }
1974

    
1975
    if(s->pict_type == I_TYPE){
1976
        //FIXME
1977
        rate_q= s->qsum/s->qcount;
1978
    }else{ //P Frame
1979
        int i;
1980
        int diff, best_diff=1000000000;
1981
        for(i=1; i<=31; i++){
1982
            diff= predict(&s->p_pred, i, s->mc_mb_var) - (double)s->bit_rate/fps;
1983
            if(diff<0) diff= -diff;
1984
            if(diff<best_diff){
1985
                best_diff= diff;
1986
                rate_q= i;
1987
            }
1988
        }
1989
    }
1990

    
1991
    s->short_term_qsum*=s->qblur;
1992
    s->short_term_qcount*=s->qblur;
1993

    
1994
    s->short_term_qsum+= rate_q;
1995
    s->short_term_qcount++;
1996
    short_term_q= s->short_term_qsum/s->short_term_qcount;
1997
    
1998
    long_term_q= s->qsum/s->qcount*(s->total_bits+1)/(wanted_bits+1); //+1 to avoid nan & 0
1999

    
2000
//    q= (long_term_q - short_term_q)*s->qcompress + short_term_q;
2001
    q= 1/((1/long_term_q - 1/short_term_q)*s->qcompress + 1/short_term_q);
2002

    
2003
    diff= s->total_bits - wanted_bits;
2004
    br_compensation= (s->bit_rate_tolerance - diff)/s->bit_rate_tolerance;
2005
    if(br_compensation<=0.0) br_compensation=0.001;
2006
    q/=br_compensation;
2007

    
2008
    qscale= (int)(q + 0.5);
2009
    if     (qscale<qmin) qscale=qmin;
2010
    else if(qscale>qmax) qscale=qmax;
2011
    
2012
    if     (qscale<last_qscale-s->max_qdiff) qscale=last_qscale-s->max_qdiff;
2013
    else if(qscale>last_qscale+s->max_qdiff) qscale=last_qscale+s->max_qdiff;
2014

    
2015
    s->qsum+= qscale;
2016
    s->qcount++;
2017

    
2018
    s->last_pict_type= s->pict_type;
2019
//printf("q:%d diff:%d comp:%f rate_q:%d st_q:%f fvar:%d last_size:%d\n", qscale, (int)diff, br_compensation, 
2020
//       rate_q, short_term_q, s->mc_mb_var, s->frame_bits);
2021
//printf("%d %d\n", s->bit_rate, (int)fps);
2022
    return qscale;
2023
#else
2024
    INT64 diff, total_bits = s->total_bits;
2025
    float q;
2026
    int qscale;
2027
    if (s->pict_type == I_TYPE) {
2028
        s->wanted_bits += s->I_frame_bits;
2029
    } else {
2030
        s->wanted_bits += s->P_frame_bits;
2031
    }
2032
    diff = s->wanted_bits - total_bits;
2033
    q = 31.0 - (float)diff / (QSCALE_K * s->mb_height * s->mb_width);
2034
    /* adjust for I frame */
2035
    if (s->pict_type == I_TYPE && !s->intra_only) {
2036
        q /= I_FRAME_SIZE_RATIO;
2037
    }
2038

    
2039
    /* using a too small Q scale leeds to problems in mpeg1 and h263
2040
       because AC coefficients are clamped to 255 or 127 */
2041
    qmin = 3;
2042
    if (q < qmin)
2043
        q = qmin;
2044
    else if (q > 31)
2045
        q = 31;
2046
    qscale = (int)(q + 0.5);
2047
#if defined(DEBUG)
2048
    printf("\n%d: total=%0.0f wanted=%0.0f br=%0.1f diff=%d qest=%2.1f\n", 
2049
           s->picture_number, 
2050
           (double)total_bits, 
2051
           (double)s->wanted_bits,
2052
           (float)s->frame_rate / FRAME_RATE_BASE * 
2053
           total_bits / s->picture_number, 
2054
           (int)diff, q);
2055
#endif
2056
    return qscale;
2057
#endif
2058
}
2059

    
2060
AVCodec mpeg1video_encoder = {
2061
    "mpeg1video",
2062
    CODEC_TYPE_VIDEO,
2063
    CODEC_ID_MPEG1VIDEO,
2064
    sizeof(MpegEncContext),
2065
    MPV_encode_init,
2066
    MPV_encode_picture,
2067
    MPV_encode_end,
2068
};
2069

    
2070
AVCodec h263_encoder = {
2071
    "h263",
2072
    CODEC_TYPE_VIDEO,
2073
    CODEC_ID_H263,
2074
    sizeof(MpegEncContext),
2075
    MPV_encode_init,
2076
    MPV_encode_picture,
2077
    MPV_encode_end,
2078
};
2079

    
2080
AVCodec h263p_encoder = {
2081
    "h263p",
2082
    CODEC_TYPE_VIDEO,
2083
    CODEC_ID_H263P,
2084
    sizeof(MpegEncContext),
2085
    MPV_encode_init,
2086
    MPV_encode_picture,
2087
    MPV_encode_end,
2088
};
2089

    
2090
AVCodec rv10_encoder = {
2091
    "rv10",
2092
    CODEC_TYPE_VIDEO,
2093
    CODEC_ID_RV10,
2094
    sizeof(MpegEncContext),
2095
    MPV_encode_init,
2096
    MPV_encode_picture,
2097
    MPV_encode_end,
2098
};
2099

    
2100
AVCodec mjpeg_encoder = {
2101
    "mjpeg",
2102
    CODEC_TYPE_VIDEO,
2103
    CODEC_ID_MJPEG,
2104
    sizeof(MpegEncContext),
2105
    MPV_encode_init,
2106
    MPV_encode_picture,
2107
    MPV_encode_end,
2108
};
2109

    
2110
AVCodec mpeg4_encoder = {
2111
    "mpeg4",
2112
    CODEC_TYPE_VIDEO,
2113
    CODEC_ID_MPEG4,
2114
    sizeof(MpegEncContext),
2115
    MPV_encode_init,
2116
    MPV_encode_picture,
2117
    MPV_encode_end,
2118
};
2119

    
2120
AVCodec msmpeg4_encoder = {
2121
    "msmpeg4",
2122
    CODEC_TYPE_VIDEO,
2123
    CODEC_ID_MSMPEG4,
2124
    sizeof(MpegEncContext),
2125
    MPV_encode_init,
2126
    MPV_encode_picture,
2127
    MPV_encode_end,
2128
};