Statistics
| Branch: | Revision:

ffmpeg / libavcodec / mpegvideo.c @ badaf88e

History | View | Annotate | Download (44.5 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
#include <stdlib.h>
20
#include <stdio.h>
21
#include <math.h>
22
#include <string.h>
23
#include "avcodec.h"
24
#include "dsputil.h"
25
#include "mpegvideo.h"
26

    
27
#ifdef USE_FASTMEMCPY
28
#include "fastmemcpy.h"
29
#endif
30

    
31
static void encode_picture(MpegEncContext *s, int picture_number);
32
static void rate_control_init(MpegEncContext *s);
33
static int rate_estimate_qscale(MpegEncContext *s);
34
static void dct_unquantize_mpeg1_c(MpegEncContext *s, 
35
                                   DCTELEM *block, int n, int qscale);
36
static void dct_unquantize_h263_c(MpegEncContext *s, 
37
                                  DCTELEM *block, int n, int qscale);
38
static int dct_quantize(MpegEncContext *s, DCTELEM *block, int n, int qscale);
39
static int dct_quantize_mmx(MpegEncContext *s, 
40
                            DCTELEM *block, int n,
41
                            int qscale);
42
#define EDGE_WIDTH 16
43

    
44
/* enable all paranoid tests for rounding, overflows, etc... */
45
//#define PARANOID
46

    
47
//#define DEBUG
48

    
49
/* for jpeg fast DCT */
50
#define CONST_BITS 14
51

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

    
64
static UINT8 h263_chroma_roundtab[16] = {
65
    0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2,
66
};
67

    
68
/* default motion estimation */
69
int motion_estimation_method = ME_LOG;
70

    
71
extern UINT8 zigzag_end[64];
72

    
73
/* XXX: should use variable shift ? */
74
#define QMAT_SHIFT_MMX 19
75
#define QMAT_SHIFT 25
76

    
77
static void convert_matrix(int *qmat, const UINT16 *quant_matrix, int qscale)
78
{
79
    int i;
80

    
81
    if (av_fdct == jpeg_fdct_ifast) {
82
        for(i=0;i<64;i++) {
83
            /* 16 <= qscale * quant_matrix[i] <= 7905 */
84
            /* 19952 <= aanscales[i] * qscale * quant_matrix[i] <= 249205026 */
85
            
86
            qmat[i] = (int)((UINT64_C(1) << (QMAT_SHIFT + 11)) / 
87
                            (aanscales[i] * qscale * quant_matrix[i]));
88
        }
89
    } else {
90
        for(i=0;i<64;i++) {
91
            /* We can safely suppose that 16 <= quant_matrix[i] <= 255
92
               So 16 <= qscale * quant_matrix[i] <= 7905
93
               so (1 << QMAT_SHIFT) / 16 >= qmat[i] >= (1 << QMAT_SHIFT) / 7905
94
            */
95
            qmat[i] = (1 << QMAT_SHIFT_MMX) / (qscale * quant_matrix[i]);
96
        }
97
    }
98
}
99

    
100
/* init common structure for both encoder and decoder */
101
int MPV_common_init(MpegEncContext *s)
102
{
103
    int c_size, i;
104
    UINT8 *pict;
105

    
106
    if (s->out_format == FMT_H263) 
107
        s->dct_unquantize = dct_unquantize_h263_c;
108
    else
109
        s->dct_unquantize = dct_unquantize_mpeg1_c;
110
        
111
#ifdef HAVE_MMX
112
    MPV_common_init_mmx(s);
113
#endif
114
    s->mb_width = (s->width + 15) / 16;
115
    s->mb_height = (s->height + 15) / 16;
116
    s->linesize = s->mb_width * 16 + 2 * EDGE_WIDTH;
117

    
118
    for(i=0;i<3;i++) {
119
        int w, h, shift, pict_start;
120

    
121
        w = s->linesize;
122
        h = s->mb_height * 16 + 2 * EDGE_WIDTH;
123
        shift = (i == 0) ? 0 : 1;
124
        c_size = (w >> shift) * (h >> shift);
125
        pict_start = (w >> shift) * (EDGE_WIDTH >> shift) + (EDGE_WIDTH >> shift);
126

    
127
        pict = av_mallocz(c_size);
128
        if (pict == NULL)
129
            goto fail;
130
        s->last_picture_base[i] = pict;
131
        s->last_picture[i] = pict + pict_start;
132
    
133
        pict = av_mallocz(c_size);
134
        if (pict == NULL)
135
            goto fail;
136
        s->next_picture_base[i] = pict;
137
        s->next_picture[i] = pict + pict_start;
138

    
139
        if (s->has_b_frames) {
140
            pict = av_mallocz(c_size);
141
            if (pict == NULL) 
142
                goto fail;
143
            s->aux_picture_base[i] = pict;
144
            s->aux_picture[i] = pict + pict_start;
145
        }
146
    }
147

    
148
    if (s->out_format == FMT_H263) {
149
        int size;
150
        /* MV prediction */
151
        size = (2 * s->mb_width + 2) * (2 * s->mb_height + 2);
152
        s->motion_val = malloc(size * 2 * sizeof(INT16));
153
        if (s->motion_val == NULL)
154
            goto fail;
155
        memset(s->motion_val, 0, size * 2 * sizeof(INT16));
156
    }
157

    
158
    if (s->h263_pred) {
159
        int y_size, c_size, i, size;
160
        
161
        /* dc values */
162

    
163
        y_size = (2 * s->mb_width + 2) * (2 * s->mb_height + 2);
164
        c_size = (s->mb_width + 2) * (s->mb_height + 2);
165
        size = y_size + 2 * c_size;
166
        s->dc_val[0] = malloc(size * sizeof(INT16));
167
        if (s->dc_val[0] == NULL)
168
            goto fail;
169
        s->dc_val[1] = s->dc_val[0] + y_size;
170
        s->dc_val[2] = s->dc_val[1] + c_size;
171
        for(i=0;i<size;i++)
172
            s->dc_val[0][i] = 1024;
173

    
174
        /* ac values */
175
        s->ac_val[0] = av_mallocz(size * sizeof(INT16) * 16);
176
        if (s->ac_val[0] == NULL)
177
            goto fail;
178
        s->ac_val[1] = s->ac_val[0] + y_size;
179
        s->ac_val[2] = s->ac_val[1] + c_size;
180
        
181
        /* cbp values */
182
        s->coded_block = av_mallocz(y_size);
183
        if (!s->coded_block)
184
            goto fail;
185

    
186
        /* which mb is a intra block */
187
        s->mbintra_table = av_mallocz(s->mb_width * s->mb_height);
188
        if (!s->mbintra_table)
189
            goto fail;
190
        memset(s->mbintra_table, 1, s->mb_width * s->mb_height);
191
    }
192
    /* default structure is frame */
193
    s->picture_structure = PICT_FRAME;
194

    
195
    /* init macroblock skip table */
196
    if (!s->encoding) {
197
        s->mbskip_table = av_mallocz(s->mb_width * s->mb_height);
198
        if (!s->mbskip_table)
199
            goto fail;
200
    }
201

    
202
    s->context_initialized = 1;
203
    return 0;
204
 fail:
205
    if (s->motion_val)
206
        free(s->motion_val);
207
    if (s->dc_val[0])
208
        free(s->dc_val[0]);
209
    if (s->ac_val[0])
210
        free(s->ac_val[0]);
211
    if (s->coded_block)
212
        free(s->coded_block);
213
    if (s->mbintra_table)
214
        free(s->mbintra_table);
215
    if (s->mbskip_table)
216
        free(s->mbskip_table);
217
    for(i=0;i<3;i++) {
218
        if (s->last_picture_base[i])
219
            free(s->last_picture_base[i]);
220
        if (s->next_picture_base[i])
221
            free(s->next_picture_base[i]);
222
        if (s->aux_picture_base[i])
223
            free(s->aux_picture_base[i]);
224
    }
225
    return -1;
226
}
227

    
228
/* init common structure for both encoder and decoder */
229
void MPV_common_end(MpegEncContext *s)
230
{
231
    int i;
232

    
233
    if (s->motion_val)
234
        free(s->motion_val);
235
    if (s->h263_pred) {
236
        free(s->dc_val[0]);
237
        free(s->ac_val[0]);
238
        free(s->coded_block);
239
        free(s->mbintra_table);
240
    }
241
    if (s->mbskip_table)
242
        free(s->mbskip_table);
243
    for(i=0;i<3;i++) {
244
        free(s->last_picture_base[i]);
245
        free(s->next_picture_base[i]);
246
        if (s->has_b_frames)
247
            free(s->aux_picture_base[i]);
248
    }
249
    s->context_initialized = 0;
250
}
251

    
252
/* init video encoder */
253
int MPV_encode_init(AVCodecContext *avctx)
254
{
255
    MpegEncContext *s = avctx->priv_data;
256
    int i;
257

    
258
    s->bit_rate = avctx->bit_rate;
259
    s->frame_rate = avctx->frame_rate;
260
    s->width = avctx->width;
261
    s->height = avctx->height;
262
    s->gop_size = avctx->gop_size;
263
    s->rtp_mode = avctx->rtp_mode;
264
    s->rtp_payload_size = avctx->rtp_payload_size;
265
    s->avctx = avctx;
266
    
267
    if (s->gop_size <= 1) {
268
        s->intra_only = 1;
269
        s->gop_size = 12;
270
    } else {
271
        s->intra_only = 0;
272
    }
273
    s->full_search = motion_estimation_method;
274

    
275
    s->fixed_qscale = (avctx->flags & CODEC_FLAG_QSCALE);
276

    
277
    switch(avctx->codec->id) {
278
    case CODEC_ID_MPEG1VIDEO:
279
        s->out_format = FMT_MPEG1;
280
        break;
281
    case CODEC_ID_MJPEG:
282
        s->out_format = FMT_MJPEG;
283
        s->intra_only = 1; /* force intra only for jpeg */
284
        if (mjpeg_init(s) < 0)
285
            return -1;
286
        break;
287
    case CODEC_ID_H263:
288
        if (h263_get_picture_format(s->width, s->height) == 7){
289
            printf("Input picture size isn't suitable for h263 codec! try h263+\n");
290
            return -1;
291
        }
292
        s->out_format = FMT_H263;
293
        break;
294
    case CODEC_ID_H263P:
295
        s->out_format = FMT_H263;
296
        s->rtp_mode = 1;
297
        s->rtp_payload_size = 1200; 
298
        s->h263_plus = 1;
299
        s->unrestricted_mv = 1;
300
        
301
        /* These are just to be sure */
302
        s->umvplus = 0;
303
        s->umvplus_dec = 0;
304
        break;
305
    case CODEC_ID_RV10:
306
        s->out_format = FMT_H263;
307
        s->h263_rv10 = 1;
308
        break;
309
    case CODEC_ID_MPEG4:
310
        s->out_format = FMT_H263;
311
        s->h263_pred = 1;
312
        s->unrestricted_mv = 1;
313
        break;
314
    case CODEC_ID_MSMPEG4:
315
        s->out_format = FMT_H263;
316
        s->h263_msmpeg4 = 1;
317
        s->h263_pred = 1;
318
        s->unrestricted_mv = 1;
319
        break;
320
    default:
321
        return -1;
322
    }
323

    
324
    if (s->out_format == FMT_H263)
325
        h263_encode_init_vlc(s);
326

    
327
    s->encoding = 1;
328

    
329
    /* init */
330
    if (MPV_common_init(s) < 0)
331
        return -1;
332
    
333
    /* init default q matrix */
334
    for(i=0;i<64;i++) {
335
        s->intra_matrix[i] = default_intra_matrix[i];
336
        s->non_intra_matrix[i] = default_non_intra_matrix[i];
337
    }
338

    
339
    /* rate control init */
340
    rate_control_init(s);
341

    
342
    s->picture_number = 0;
343
    s->fake_picture_number = 0;
344
    /* motion detector init */
345
    s->f_code = 1;
346

    
347
    return 0;
348
}
349

    
350
int MPV_encode_end(AVCodecContext *avctx)
351
{
352
    MpegEncContext *s = avctx->priv_data;
353

    
354
#ifdef STATS
355
    print_stats();
356
#endif
357
    MPV_common_end(s);
358
    if (s->out_format == FMT_MJPEG)
359
        mjpeg_close(s);
360
    return 0;
361
}
362

    
363
/* draw the edges of width 'w' of an image of size width, height */
364
static void draw_edges(UINT8 *buf, int wrap, int width, int height, int w)
365
{
366
    UINT8 *ptr, *last_line;
367
    int i;
368

    
369
    last_line = buf + (height - 1) * wrap;
370
    for(i=0;i<w;i++) {
371
        /* top and bottom */
372
        memcpy(buf - (i + 1) * wrap, buf, width);
373
        memcpy(last_line + (i + 1) * wrap, last_line, width);
374
    }
375
    /* left and right */
376
    ptr = buf;
377
    for(i=0;i<height;i++) {
378
        memset(ptr - w, ptr[0], w);
379
        memset(ptr + width, ptr[width-1], w);
380
        ptr += wrap;
381
    }
382
    /* corners */
383
    for(i=0;i<w;i++) {
384
        memset(buf - (i + 1) * wrap - w, buf[0], w); /* top left */
385
        memset(buf - (i + 1) * wrap + width, buf[width-1], w); /* top right */
386
        memset(last_line + (i + 1) * wrap - w, last_line[0], w); /* top left */
387
        memset(last_line + (i + 1) * wrap + width, last_line[width-1], w); /* top right */
388
    }
389
}
390

    
391
/* generic function for encode/decode called before a frame is coded/decoded */
392
void MPV_frame_start(MpegEncContext *s)
393
{
394
    int i;
395
    UINT8 *tmp;
396

    
397
    s->mb_skiped = 0;
398
    if (s->pict_type == B_TYPE) {
399
        for(i=0;i<3;i++) {
400
            s->current_picture[i] = s->aux_picture[i];
401
        }
402
    } else {
403
        for(i=0;i<3;i++) {
404
            /* swap next and last */
405
            tmp = s->last_picture[i];
406
            s->last_picture[i] = s->next_picture[i];
407
            s->next_picture[i] = tmp;
408
            s->current_picture[i] = tmp;
409
        }
410
    }
411
}
412

    
413
/* generic function for encode/decode called after a frame has been coded/decoded */
414
void MPV_frame_end(MpegEncContext *s)
415
{
416
    /* draw edge for correct motion prediction if outside */
417
    if (s->pict_type != B_TYPE) {
418
      if(s->avctx==NULL || s->avctx->codec->id!=CODEC_ID_MPEG4){
419
        draw_edges(s->current_picture[0], s->linesize, s->mb_width*16, s->mb_height*16, EDGE_WIDTH);
420
        draw_edges(s->current_picture[1], s->linesize/2, s->mb_width*8, s->mb_height*8, EDGE_WIDTH/2);
421
        draw_edges(s->current_picture[2], s->linesize/2, s->mb_width*8, s->mb_height*8, EDGE_WIDTH/2);
422
      }else{
423
        /* OpenDivx, but i dunno how to distinguish it from mpeg4 */
424
        draw_edges(s->current_picture[0], s->linesize, s->width, s->height, EDGE_WIDTH);
425
        draw_edges(s->current_picture[1], s->linesize/2, s->width/2, s->height/2, EDGE_WIDTH/2);
426
        draw_edges(s->current_picture[2], s->linesize/2, s->width/2, s->height/2, EDGE_WIDTH/2);
427
      }
428
    }
429
}
430

    
431
int MPV_encode_picture(AVCodecContext *avctx,
432
                       unsigned char *buf, int buf_size, void *data)
433
{
434
    MpegEncContext *s = avctx->priv_data;
435
    AVPicture *pict = data;
436
    int i, j;
437

    
438
    if (s->fixed_qscale) 
439
        s->qscale = avctx->quality;
440

    
441
    init_put_bits(&s->pb, buf, buf_size, NULL, NULL);
442

    
443
    if (!s->intra_only) {
444
        /* first picture of GOP is intra */
445
        if ((s->picture_number % s->gop_size) == 0)
446
            s->pict_type = I_TYPE;
447
        else
448
            s->pict_type = P_TYPE;
449
    } else {
450
        s->pict_type = I_TYPE;
451
    }
452
    avctx->key_frame = (s->pict_type == I_TYPE);
453
    
454
    MPV_frame_start(s);
455

    
456
    for(i=0;i<3;i++) {
457
        UINT8 *src = pict->data[i];
458
        UINT8 *dest = s->current_picture[i];
459
        int src_wrap = pict->linesize[i];
460
        int dest_wrap = s->linesize;
461
        int w = s->width;
462
        int h = s->height;
463

    
464
        if (i >= 1) {
465
            dest_wrap >>= 1;
466
            w >>= 1;
467
            h >>= 1;
468
        }
469

    
470
        for(j=0;j<h;j++) {
471
            memcpy(dest, src, w);
472
            dest += dest_wrap;
473
            src += src_wrap;
474
        }
475
        s->new_picture[i] = s->current_picture[i];
476
    }
477

    
478
    encode_picture(s, s->picture_number);
479
    
480
    MPV_frame_end(s);
481
    s->picture_number++;
482

    
483
    if (s->out_format == FMT_MJPEG)
484
        mjpeg_picture_trailer(s);
485

    
486
    flush_put_bits(&s->pb);
487
    s->total_bits += (s->pb.buf_ptr - s->pb.buf) * 8;
488
    avctx->quality = s->qscale;
489
    return s->pb.buf_ptr - s->pb.buf;
490
}
491

    
492
static inline int clip(int a, int amin, int amax)
493
{
494
    if (a < amin)
495
        return amin;
496
    else if (a > amax)
497
        return amax;
498
    else
499
        return a;
500
}
501

    
502
/* apply one mpeg motion vector to the three components */
503
static inline void mpeg_motion(MpegEncContext *s,
504
                               UINT8 *dest_y, UINT8 *dest_cb, UINT8 *dest_cr,
505
                               int dest_offset,
506
                               UINT8 **ref_picture, int src_offset,
507
                               int field_based, op_pixels_func *pix_op,
508
                               int motion_x, int motion_y, int h)
509
{
510
    UINT8 *ptr;
511
    int dxy, offset, mx, my, src_x, src_y, height, linesize;
512
    
513
    dxy = ((motion_y & 1) << 1) | (motion_x & 1);
514
    src_x = s->mb_x * 16 + (motion_x >> 1);
515
    src_y = s->mb_y * (16 >> field_based) + (motion_y >> 1);
516
                
517
    /* WARNING: do no forget half pels */
518
    height = s->height >> field_based;
519
    src_x = clip(src_x, -16, s->width);
520
    if (src_x == s->width)
521
        dxy &= ~1;
522
    src_y = clip(src_y, -16, height);
523
    if (src_y == height)
524
        dxy &= ~2;
525
    linesize = s->linesize << field_based;
526
    ptr = ref_picture[0] + (src_y * linesize) + (src_x) + src_offset;
527
    dest_y += dest_offset;
528
    pix_op[dxy](dest_y, ptr, linesize, h);
529
    pix_op[dxy](dest_y + 8, ptr + 8, linesize, h);
530

    
531
    if (s->out_format == FMT_H263) {
532
        dxy = 0;
533
        if ((motion_x & 3) != 0)
534
            dxy |= 1;
535
        if ((motion_y & 3) != 0)
536
            dxy |= 2;
537
        mx = motion_x >> 2;
538
        my = motion_y >> 2;
539
    } else {
540
        mx = motion_x / 2;
541
        my = motion_y / 2;
542
        dxy = ((my & 1) << 1) | (mx & 1);
543
        mx >>= 1;
544
        my >>= 1;
545
    }
546
    
547
    src_x = s->mb_x * 8 + mx;
548
    src_y = s->mb_y * (8 >> field_based) + my;
549
    src_x = clip(src_x, -8, s->width >> 1);
550
    if (src_x == (s->width >> 1))
551
        dxy &= ~1;
552
    src_y = clip(src_y, -8, height >> 1);
553
    if (src_y == (height >> 1))
554
        dxy &= ~2;
555

    
556
    offset = (src_y * (linesize >> 1)) + src_x + (src_offset >> 1);
557
    ptr = ref_picture[1] + offset;
558
    pix_op[dxy](dest_cb + (dest_offset >> 1), ptr, linesize >> 1, h >> 1);
559
    ptr = ref_picture[2] + offset;
560
    pix_op[dxy](dest_cr + (dest_offset >> 1), ptr, linesize >> 1, h >> 1);
561
}
562

    
563
static inline void MPV_motion(MpegEncContext *s, 
564
                              UINT8 *dest_y, UINT8 *dest_cb, UINT8 *dest_cr,
565
                              int dir, UINT8 **ref_picture, 
566
                              op_pixels_func *pix_op)
567
{
568
    int dxy, offset, mx, my, src_x, src_y, motion_x, motion_y;
569
    int mb_x, mb_y, i;
570
    UINT8 *ptr, *dest;
571

    
572
    mb_x = s->mb_x;
573
    mb_y = s->mb_y;
574

    
575
    switch(s->mv_type) {
576
    case MV_TYPE_16X16:
577
        mpeg_motion(s, dest_y, dest_cb, dest_cr, 0,
578
                    ref_picture, 0,
579
                    0, pix_op,
580
                    s->mv[dir][0][0], s->mv[dir][0][1], 16);
581
        break;
582
    case MV_TYPE_8X8:
583
        for(i=0;i<4;i++) {
584
            motion_x = s->mv[dir][i][0];
585
            motion_y = s->mv[dir][i][1];
586

    
587
            dxy = ((motion_y & 1) << 1) | (motion_x & 1);
588
            src_x = mb_x * 16 + (motion_x >> 1) + (i & 1) * 8;
589
            src_y = mb_y * 16 + (motion_y >> 1) + ((i >> 1) & 1) * 8;
590
                    
591
            /* WARNING: do no forget half pels */
592
            src_x = clip(src_x, -16, s->width);
593
            if (src_x == s->width)
594
                dxy &= ~1;
595
            src_y = clip(src_y, -16, s->height);
596
            if (src_y == s->height)
597
                dxy &= ~2;
598
                    
599
            ptr = ref_picture[0] + (src_y * s->linesize) + (src_x);
600
            dest = dest_y + ((i & 1) * 8) + (i >> 1) * 8 * s->linesize;
601
            pix_op[dxy](dest, ptr, s->linesize, 8);
602
        }
603
        /* In case of 8X8, we construct a single chroma motion vector
604
           with a special rounding */
605
        mx = 0;
606
        my = 0;
607
        for(i=0;i<4;i++) {
608
            mx += s->mv[dir][i][0];
609
            my += s->mv[dir][i][1];
610
        }
611
        if (mx >= 0)
612
            mx = (h263_chroma_roundtab[mx & 0xf] + ((mx >> 3) & ~1));
613
        else {
614
            mx = -mx;
615
            mx = -(h263_chroma_roundtab[mx & 0xf] + ((mx >> 3) & ~1));
616
        }
617
        if (my >= 0)
618
            my = (h263_chroma_roundtab[my & 0xf] + ((my >> 3) & ~1));
619
        else {
620
            my = -my;
621
            my = -(h263_chroma_roundtab[my & 0xf] + ((my >> 3) & ~1));
622
        }
623
        dxy = ((my & 1) << 1) | (mx & 1);
624
        mx >>= 1;
625
        my >>= 1;
626

    
627
        src_x = mb_x * 8 + mx;
628
        src_y = mb_y * 8 + my;
629
        src_x = clip(src_x, -8, s->width/2);
630
        if (src_x == s->width/2)
631
            dxy &= ~1;
632
        src_y = clip(src_y, -8, s->height/2);
633
        if (src_y == s->height/2)
634
            dxy &= ~2;
635
        
636
        offset = (src_y * (s->linesize >> 1)) + src_x;
637
        ptr = ref_picture[1] + offset;
638
        pix_op[dxy](dest_cb, ptr, s->linesize >> 1, 8);
639
        ptr = ref_picture[2] + offset;
640
        pix_op[dxy](dest_cr, ptr, s->linesize >> 1, 8);
641
        break;
642
    case MV_TYPE_FIELD:
643
        if (s->picture_structure == PICT_FRAME) {
644
            /* top field */
645
            mpeg_motion(s, dest_y, dest_cb, dest_cr, 0,
646
                        ref_picture, s->field_select[dir][0] ? s->linesize : 0,
647
                        1, pix_op,
648
                        s->mv[dir][0][0], s->mv[dir][0][1], 8);
649
            /* bottom field */
650
            mpeg_motion(s, dest_y, dest_cb, dest_cr, s->linesize,
651
                        ref_picture, s->field_select[dir][1] ? s->linesize : 0,
652
                        1, pix_op,
653
                        s->mv[dir][1][0], s->mv[dir][1][1], 8);
654
        } else {
655
            
656

    
657
        }
658
        break;
659
    }
660
}
661

    
662

    
663
/* put block[] to dest[] */
664
static inline void put_dct(MpegEncContext *s, 
665
                           DCTELEM *block, int i, UINT8 *dest, int line_size)
666
{
667
    if (!s->mpeg2)
668
        s->dct_unquantize(s, block, i, s->qscale);
669
    ff_idct (block);
670
    put_pixels_clamped(block, dest, line_size);
671
}
672

    
673
/* add block[] to dest[] */
674
static inline void add_dct(MpegEncContext *s, 
675
                           DCTELEM *block, int i, UINT8 *dest, int line_size)
676
{
677
    if (s->block_last_index[i] >= 0) {
678
        if (!s->mpeg2)
679
            if(s->encoding || s->avctx==NULL || s->avctx->codec->id!=CODEC_ID_MSMPEG4)
680
                s->dct_unquantize(s, block, i, s->qscale);
681
        ff_idct (block);
682
        add_pixels_clamped(block, dest, line_size);
683
    }
684
}
685

    
686
/* generic function called after a macroblock has been parsed by the
687
   decoder or after it has been encoded by the encoder.
688

689
   Important variables used:
690
   s->mb_intra : true if intra macroblock
691
   s->mv_dir   : motion vector direction
692
   s->mv_type  : motion vector type
693
   s->mv       : motion vector
694
   s->interlaced_dct : true if interlaced dct used (mpeg2)
695
 */
696
void MPV_decode_mb(MpegEncContext *s, DCTELEM block[6][64])
697
{
698
    int mb_x, mb_y, motion_x, motion_y;
699
    int dct_linesize, dct_offset;
700
    op_pixels_func *op_pix;
701

    
702
    mb_x = s->mb_x;
703
    mb_y = s->mb_y;
704

    
705
#ifdef FF_POSTPROCESS
706
    quant_store[mb_y][mb_x]=s->qscale;
707
    //printf("[%02d][%02d] %d\n",mb_x,mb_y,s->qscale);
708
#endif
709

    
710
    /* update DC predictors for P macroblocks */
711
    if (!s->mb_intra) {
712
        if (s->h263_pred) {
713
          if(s->mbintra_table[mb_x + mb_y*s->mb_width])
714
          {
715
            int wrap, x, y, v;
716
            s->mbintra_table[mb_x + mb_y*s->mb_width]=0;
717
  
718
            wrap = 2 * s->mb_width + 2;
719
            v = 1024;
720
            x = 2 * mb_x + 1;
721
            y = 2 * mb_y + 1;
722
            
723
            s->dc_val[0][(x) + (y) * wrap] = v;
724
            s->dc_val[0][(x + 1) + (y) * wrap] = v;
725
            s->dc_val[0][(x) + (y + 1) * wrap] = v;
726
            s->dc_val[0][(x + 1) + (y + 1) * wrap] = v;
727
            /* ac pred */
728
            memset(s->ac_val[0][(x) + (y) * wrap], 0, 16 * sizeof(INT16));
729
            memset(s->ac_val[0][(x + 1) + (y) * wrap], 0, 16 * sizeof(INT16));
730
            memset(s->ac_val[0][(x) + (y + 1) * wrap], 0, 16 * sizeof(INT16));
731
            memset(s->ac_val[0][(x + 1) + (y + 1) * wrap], 0, 16 * sizeof(INT16));
732
            if (s->h263_msmpeg4) {
733
                s->coded_block[(x) + (y) * wrap] = 0;
734
                s->coded_block[(x + 1) + (y) * wrap] = 0;
735
                s->coded_block[(x) + (y + 1) * wrap] = 0;
736
                s->coded_block[(x + 1) + (y + 1) * wrap] = 0;
737
            }
738
            /* chroma */
739
            wrap = s->mb_width + 2;
740
            x = mb_x + 1;
741
            y = mb_y + 1;
742
            s->dc_val[1][(x) + (y) * wrap] = v;
743
            s->dc_val[2][(x) + (y) * wrap] = v;
744
            /* ac pred */
745
            memset(s->ac_val[1][(x) + (y) * wrap], 0, 16 * sizeof(INT16));
746
            memset(s->ac_val[2][(x) + (y) * wrap], 0, 16 * sizeof(INT16));
747
          }
748
        } else {
749
            s->last_dc[0] = 128 << s->intra_dc_precision;
750
            s->last_dc[1] = 128 << s->intra_dc_precision;
751
            s->last_dc[2] = 128 << s->intra_dc_precision;
752
        }
753
    }
754
    else if (s->h263_pred)
755
        s->mbintra_table[mb_x + mb_y*s->mb_width]=1;
756

    
757
    /* update motion predictor */
758
    if (s->out_format == FMT_H263) {
759
        int x, y, wrap;
760
        
761
        x = 2 * mb_x + 1;
762
        y = 2 * mb_y + 1;
763
        wrap = 2 * s->mb_width + 2;
764
        if (s->mb_intra) {
765
            motion_x = 0;
766
            motion_y = 0;
767
            goto motion_init;
768
        } else if (s->mv_type == MV_TYPE_16X16) {
769
            motion_x = s->mv[0][0][0];
770
            motion_y = s->mv[0][0][1];
771
        motion_init:
772
            /* no update if 8X8 because it has been done during parsing */
773
            s->motion_val[(x) + (y) * wrap][0] = motion_x;
774
            s->motion_val[(x) + (y) * wrap][1] = motion_y;
775
            s->motion_val[(x + 1) + (y) * wrap][0] = motion_x;
776
            s->motion_val[(x + 1) + (y) * wrap][1] = motion_y;
777
            s->motion_val[(x) + (y + 1) * wrap][0] = motion_x;
778
            s->motion_val[(x) + (y + 1) * wrap][1] = motion_y;
779
            s->motion_val[(x + 1) + (y + 1) * wrap][0] = motion_x;
780
            s->motion_val[(x + 1) + (y + 1) * wrap][1] = motion_y;
781
        }
782
    }
783
    
784
    if (!s->intra_only) {
785
        UINT8 *dest_y, *dest_cb, *dest_cr;
786
        UINT8 *mbskip_ptr;
787

    
788
        /* avoid copy if macroblock skipped in last frame too */
789
        if (!s->encoding && s->pict_type != B_TYPE) {
790
            mbskip_ptr = &s->mbskip_table[s->mb_y * s->mb_width + s->mb_x];
791
            if (s->mb_skiped) {
792
                s->mb_skiped = 0;
793
                /* if previous was skipped too, then nothing to do ! */
794
                if (*mbskip_ptr != 0) 
795
                    goto the_end;
796
                *mbskip_ptr = 1; /* indicate that this time we skiped it */
797
            } else {
798
                *mbskip_ptr = 0; /* not skipped */
799
            }
800
        }
801

    
802
        dest_y = s->current_picture[0] + (mb_y * 16 * s->linesize) + mb_x * 16;
803
        dest_cb = s->current_picture[1] + (mb_y * 8 * (s->linesize >> 1)) + mb_x * 8;
804
        dest_cr = s->current_picture[2] + (mb_y * 8 * (s->linesize >> 1)) + mb_x * 8;
805

    
806
        if (s->interlaced_dct) {
807
            dct_linesize = s->linesize * 2;
808
            dct_offset = s->linesize;
809
        } else {
810
            dct_linesize = s->linesize;
811
            dct_offset = s->linesize * 8;
812
        }
813

    
814
        if (!s->mb_intra) {
815
            /* motion handling */
816
            if (!s->no_rounding) 
817
                op_pix = put_pixels_tab;
818
            else
819
                op_pix = put_no_rnd_pixels_tab;
820

    
821
            if (s->mv_dir & MV_DIR_FORWARD) {
822
                MPV_motion(s, dest_y, dest_cb, dest_cr, 0, s->last_picture, op_pix);
823
                if (!s->no_rounding) 
824
                    op_pix = avg_pixels_tab;
825
                else
826
                    op_pix = avg_no_rnd_pixels_tab;
827
            }
828
            if (s->mv_dir & MV_DIR_BACKWARD) {
829
                MPV_motion(s, dest_y, dest_cb, dest_cr, 1, s->next_picture, op_pix);
830
            }
831

    
832
            /* add dct residue */
833
            add_dct(s, block[0], 0, dest_y, dct_linesize);
834
            add_dct(s, block[1], 1, dest_y + 8, dct_linesize);
835
            add_dct(s, block[2], 2, dest_y + dct_offset, dct_linesize);
836
            add_dct(s, block[3], 3, dest_y + dct_offset + 8, dct_linesize);
837

    
838
            add_dct(s, block[4], 4, dest_cb, s->linesize >> 1);
839
            add_dct(s, block[5], 5, dest_cr, s->linesize >> 1);
840
        } else {
841
            /* dct only in intra block */
842
            put_dct(s, block[0], 0, dest_y, dct_linesize);
843
            put_dct(s, block[1], 1, dest_y + 8, dct_linesize);
844
            put_dct(s, block[2], 2, dest_y + dct_offset, dct_linesize);
845
            put_dct(s, block[3], 3, dest_y + dct_offset + 8, dct_linesize);
846

    
847
            put_dct(s, block[4], 4, dest_cb, s->linesize >> 1);
848
            put_dct(s, block[5], 5, dest_cr, s->linesize >> 1);
849
        }
850
    }
851
 the_end:
852
    emms_c();
853
}
854

    
855
static void encode_picture(MpegEncContext *s, int picture_number)
856
{
857
    int mb_x, mb_y, wrap, last_gob;
858
    UINT8 *ptr;
859
    int i, motion_x, motion_y;
860

    
861
    s->picture_number = picture_number;
862
    if (!s->fixed_qscale) 
863
        s->qscale = rate_estimate_qscale(s);
864

    
865
    /* precompute matrix */
866
    if (s->out_format == FMT_MJPEG) {
867
        /* for mjpeg, we do include qscale in the matrix */
868
        s->intra_matrix[0] = default_intra_matrix[0];
869
        for(i=1;i<64;i++)
870
            s->intra_matrix[i] = (default_intra_matrix[i] * s->qscale) >> 3;
871
        convert_matrix(s->q_intra_matrix, s->intra_matrix, 8);
872
    } else {
873
        convert_matrix(s->q_intra_matrix, s->intra_matrix, s->qscale);
874
        convert_matrix(s->q_non_intra_matrix, s->non_intra_matrix, s->qscale);
875
    }
876

    
877
    switch(s->out_format) {
878
    case FMT_MJPEG:
879
        mjpeg_picture_header(s);
880
        break;
881
    case FMT_H263:
882
        if (s->h263_msmpeg4) 
883
            msmpeg4_encode_picture_header(s, picture_number);
884
        else if (s->h263_pred)
885
            mpeg4_encode_picture_header(s, picture_number);
886
        else if (s->h263_rv10) 
887
            rv10_encode_picture_header(s, picture_number);
888
        else
889
            h263_encode_picture_header(s, picture_number);
890
        break;
891
    case FMT_MPEG1:
892
        mpeg1_encode_picture_header(s, picture_number);
893
        break;
894
    }
895
        
896
    /* init last dc values */
897
    /* note: quant matrix value (8) is implied here */
898
    s->last_dc[0] = 128;
899
    s->last_dc[1] = 128;
900
    s->last_dc[2] = 128;
901
    s->mb_incr = 1;
902
    s->last_mv[0][0][0] = 0;
903
    s->last_mv[0][0][1] = 0;
904
    s->mv_type = MV_TYPE_16X16;
905
    s->mv_dir = MV_DIR_FORWARD;
906

    
907
    /* Get the GOB height based on picture height */
908
    if (s->out_format == FMT_H263 && s->h263_plus) {
909
        if (s->height <= 400)
910
            s->gob_index = 1;
911
        else if (s->height <= 800)
912
            s->gob_index = 2;
913
        else
914
            s->gob_index = 4;
915
    }
916
        
917
    for(mb_y=0; mb_y < s->mb_height; mb_y++) {
918
        /* Put GOB header based on RTP MTU */
919
        if (!mb_y) {
920
            s->ptr_lastgob = s->pb.buf_ptr;
921
            s->ptr_last_mb_line = s->pb.buf_ptr;
922
        } else if (s->out_format == FMT_H263 && s->h263_plus) {
923
            last_gob = h263_encode_gob_header(s, mb_y);
924
            if (last_gob) {
925
                //fprintf(stderr,"\nLast GOB size: %d", last_gob);
926
                s->first_gob_line = 1;
927
            } else
928
                s->first_gob_line = 0;
929
        }
930
        for(mb_x=0; mb_x < s->mb_width; mb_x++) {
931

    
932
            s->mb_x = mb_x;
933
            s->mb_y = mb_y;
934

    
935
            /* compute motion vector and macro block type (intra or non intra) */
936
            motion_x = 0;
937
            motion_y = 0;
938
            if (s->pict_type == P_TYPE) {
939
                s->mb_intra = estimate_motion(s, mb_x, mb_y,
940
                                              &motion_x,
941
                                              &motion_y);
942
            } else {
943
                s->mb_intra = 1;
944
            }
945

    
946
            /* get the pixels */
947
            wrap = s->linesize;
948
            ptr = s->new_picture[0] + (mb_y * 16 * wrap) + mb_x * 16;
949
            get_pixels(s->block[0], ptr, wrap);
950
            get_pixels(s->block[1], ptr + 8, wrap);
951
            get_pixels(s->block[2], ptr + 8 * wrap, wrap);
952
            get_pixels(s->block[3], ptr + 8 * wrap + 8, wrap);
953
            wrap = s->linesize >> 1;
954
            ptr = s->new_picture[1] + (mb_y * 8 * wrap) + mb_x * 8;
955
            get_pixels(s->block[4], ptr, wrap);
956

    
957
            wrap = s->linesize >> 1;
958
            ptr = s->new_picture[2] + (mb_y * 8 * wrap) + mb_x * 8;
959
            get_pixels(s->block[5], ptr, wrap);
960

    
961
            /* subtract previous frame if non intra */
962
            if (!s->mb_intra) {
963
                int dxy, offset, mx, my;
964

    
965
                dxy = ((motion_y & 1) << 1) | (motion_x & 1);
966
                ptr = s->last_picture[0] + 
967
                    ((mb_y * 16 + (motion_y >> 1)) * s->linesize) + 
968
                    (mb_x * 16 + (motion_x >> 1));
969

    
970
                sub_pixels_2(s->block[0], ptr, s->linesize, dxy);
971
                sub_pixels_2(s->block[1], ptr + 8, s->linesize, dxy);
972
                sub_pixels_2(s->block[2], ptr + s->linesize * 8, s->linesize, dxy);
973
                sub_pixels_2(s->block[3], ptr + 8 + s->linesize * 8, s->linesize ,dxy);
974

    
975
                if (s->out_format == FMT_H263) {
976
                    /* special rounding for h263 */
977
                    dxy = 0;
978
                    if ((motion_x & 3) != 0)
979
                        dxy |= 1;
980
                    if ((motion_y & 3) != 0)
981
                        dxy |= 2;
982
                    mx = motion_x >> 2;
983
                    my = motion_y >> 2;
984
                } else {
985
                    mx = motion_x / 2;
986
                    my = motion_y / 2;
987
                    dxy = ((my & 1) << 1) | (mx & 1);
988
                    mx >>= 1;
989
                    my >>= 1;
990
                }
991
                offset = ((mb_y * 8 + my) * (s->linesize >> 1)) + (mb_x * 8 + mx);
992
                ptr = s->last_picture[1] + offset;
993
                sub_pixels_2(s->block[4], ptr, s->linesize >> 1, dxy);
994
                ptr = s->last_picture[2] + offset;
995
                sub_pixels_2(s->block[5], ptr, s->linesize >> 1, dxy);
996
            }
997
            emms_c();
998

    
999
            /* DCT & quantize */
1000
            if (s->h263_msmpeg4) {
1001
                msmpeg4_dc_scale(s);
1002
            } else if (s->h263_pred) {
1003
                h263_dc_scale(s);
1004
            } else {
1005
                /* default quantization values */
1006
                s->y_dc_scale = 8;
1007
                s->c_dc_scale = 8;
1008
            }
1009

    
1010
            for(i=0;i<6;i++) {
1011
                int last_index;
1012
                if (av_fdct == jpeg_fdct_ifast)
1013
                    last_index = dct_quantize(s, s->block[i], i, s->qscale);
1014
                else
1015
                    last_index = dct_quantize_mmx(s, s->block[i], i, s->qscale);
1016
                s->block_last_index[i] = last_index;
1017
            }
1018

    
1019
            /* huffman encode */
1020
            switch(s->out_format) {
1021
            case FMT_MPEG1:
1022
                mpeg1_encode_mb(s, s->block, motion_x, motion_y);
1023
                break;
1024
            case FMT_H263:
1025
                if (s->h263_msmpeg4)
1026
                    msmpeg4_encode_mb(s, s->block, motion_x, motion_y);
1027
                else
1028
                    h263_encode_mb(s, s->block, motion_x, motion_y);
1029
                break;
1030
            case FMT_MJPEG:
1031
                mjpeg_encode_mb(s, s->block);
1032
                break;
1033
            }
1034

    
1035
            /* decompress blocks so that we keep the state of the decoder */
1036
            s->mv[0][0][0] = motion_x;
1037
            s->mv[0][0][1] = motion_y;
1038

    
1039
            MPV_decode_mb(s, s->block);
1040
        }
1041
        /* Obtain average MB line size for RTP */
1042
        if (!mb_y)
1043
            s->mb_line_avgsize = s->pb.buf_ptr - s->ptr_last_mb_line;
1044
        else    
1045
            s->mb_line_avgsize = (s->mb_line_avgsize + s->pb.buf_ptr - s->ptr_last_mb_line) >> 1;
1046
        //fprintf(stderr, "\nMB line: %d\tSize: %u\tAvg. Size: %u", s->mb_y, 
1047
        //                    (s->pb.buf_ptr - s->ptr_last_mb_line), s->mb_line_avgsize);
1048
        s->ptr_last_mb_line = s->pb.buf_ptr;
1049
    }
1050
    //if (s->gob_number)
1051
    //    fprintf(stderr,"\nNumber of GOB: %d", s->gob_number);
1052
}
1053

    
1054
static int dct_quantize(MpegEncContext *s, 
1055
                        DCTELEM *block, int n,
1056
                        int qscale)
1057
{
1058
    int i, j, level, last_non_zero, q;
1059
    const int *qmat;
1060

    
1061
    av_fdct (block);
1062

    
1063
    /* we need this permutation so that we correct the IDCT
1064
       permutation. will be moved into DCT code */
1065
    block_permute(block);
1066

    
1067
    if (s->mb_intra) {
1068
        if (n < 4)
1069
            q = s->y_dc_scale;
1070
        else
1071
            q = s->c_dc_scale;
1072
        q = q << 3;
1073
        
1074
        /* note: block[0] is assumed to be positive */
1075
        block[0] = (block[0] + (q >> 1)) / q;
1076
        i = 1;
1077
        last_non_zero = 0;
1078
        if (s->out_format == FMT_H263) {
1079
            qmat = s->q_non_intra_matrix;
1080
        } else {
1081
            qmat = s->q_intra_matrix;
1082
        }
1083
    } else {
1084
        i = 0;
1085
        last_non_zero = -1;
1086
        qmat = s->q_non_intra_matrix;
1087
    }
1088

    
1089
    for(;i<64;i++) {
1090
        j = zigzag_direct[i];
1091
        level = block[j];
1092
        level = level * qmat[j];
1093
#ifdef PARANOID
1094
        {
1095
            static int count = 0;
1096
            int level1, level2, qmat1;
1097
            double val;
1098
            if (qmat == s->q_non_intra_matrix) {
1099
                qmat1 = default_non_intra_matrix[j] * s->qscale;
1100
            } else {
1101
                qmat1 = default_intra_matrix[j] * s->qscale;
1102
            }
1103
            if (av_fdct != jpeg_fdct_ifast)
1104
                val = ((double)block[j] * 8.0) / (double)qmat1;
1105
            else
1106
                val = ((double)block[j] * 8.0 * 2048.0) / 
1107
                    ((double)qmat1 * aanscales[j]);
1108
            level1 = (int)val;
1109
            level2 = level / (1 << (QMAT_SHIFT - 3));
1110
            if (level1 != level2) {
1111
                fprintf(stderr, "%d: quant error qlevel=%d wanted=%d level=%d qmat1=%d qmat=%d wantedf=%0.6f\n", 
1112
                        count, level2, level1, block[j], qmat1, qmat[j],
1113
                        val);
1114
                count++;
1115
            }
1116

    
1117
        }
1118
#endif
1119
        /* XXX: slight error for the low range. Test should be equivalent to
1120
           (level <= -(1 << (QMAT_SHIFT - 3)) || level >= (1 <<
1121
           (QMAT_SHIFT - 3)))
1122
        */
1123
        if (((level << (31 - (QMAT_SHIFT - 3))) >> (31 - (QMAT_SHIFT - 3))) != 
1124
            level) {
1125
            level = level / (1 << (QMAT_SHIFT - 3));
1126
            /* XXX: currently, this code is not optimal. the range should be:
1127
               mpeg1: -255..255
1128
               mpeg2: -2048..2047
1129
               h263:  -128..127
1130
               mpeg4: -2048..2047
1131
            */
1132
            if (level > 127)
1133
                level = 127;
1134
            else if (level < -128)
1135
                level = -128;
1136
            block[j] = level;
1137
            last_non_zero = i;
1138
        } else {
1139
            block[j] = 0;
1140
        }
1141
    }
1142
    return last_non_zero;
1143
}
1144

    
1145
static int dct_quantize_mmx(MpegEncContext *s, 
1146
                            DCTELEM *block, int n,
1147
                            int qscale)
1148
{
1149
    int i, j, level, last_non_zero, q;
1150
    const int *qmat;
1151

    
1152
    av_fdct (block);
1153
    
1154
    /* we need this permutation so that we correct the IDCT
1155
       permutation. will be moved into DCT code */
1156
    block_permute(block);
1157

    
1158
    if (s->mb_intra) {
1159
        if (n < 4)
1160
            q = s->y_dc_scale;
1161
        else
1162
            q = s->c_dc_scale;
1163
        
1164
        /* note: block[0] is assumed to be positive */
1165
        block[0] = (block[0] + (q >> 1)) / q;
1166
        i = 1;
1167
        last_non_zero = 0;
1168
        if (s->out_format == FMT_H263) {
1169
            qmat = s->q_non_intra_matrix;
1170
        } else {
1171
            qmat = s->q_intra_matrix;
1172
        }
1173
    } else {
1174
        i = 0;
1175
        last_non_zero = -1;
1176
        qmat = s->q_non_intra_matrix;
1177
    }
1178

    
1179
    for(;i<64;i++) {
1180
        j = zigzag_direct[i];
1181
        level = block[j];
1182
        level = level * qmat[j];
1183
        /* XXX: slight error for the low range. Test should be equivalent to
1184
           (level <= -(1 << (QMAT_SHIFT_MMX - 3)) || level >= (1 <<
1185
           (QMAT_SHIFT_MMX - 3)))
1186
        */
1187
        if (((level << (31 - (QMAT_SHIFT_MMX - 3))) >> (31 - (QMAT_SHIFT_MMX - 3))) != 
1188
            level) {
1189
            level = level / (1 << (QMAT_SHIFT_MMX - 3));
1190
            /* XXX: currently, this code is not optimal. the range should be:
1191
               mpeg1: -255..255
1192
               mpeg2: -2048..2047
1193
               h263:  -128..127
1194
               mpeg4: -2048..2047
1195
            */
1196
            if (level > 127)
1197
                level = 127;
1198
            else if (level < -128)
1199
                level = -128;
1200
            block[j] = level;
1201
            last_non_zero = i;
1202
        } else {
1203
            block[j] = 0;
1204
        }
1205
    }
1206
    return last_non_zero;
1207
}
1208

    
1209
static void dct_unquantize_mpeg1_c(MpegEncContext *s, 
1210
                                   DCTELEM *block, int n, int qscale)
1211
{
1212
    int i, level, nCoeffs;
1213
    const UINT16 *quant_matrix;
1214

    
1215
    if(s->alternate_scan) nCoeffs= 64;
1216
    else nCoeffs= s->block_last_index[n]+1;
1217
    
1218
    if (s->mb_intra) {
1219
        if (n < 4) 
1220
            block[0] = block[0] * s->y_dc_scale;
1221
        else
1222
            block[0] = block[0] * s->c_dc_scale;
1223
        /* XXX: only mpeg1 */
1224
        quant_matrix = s->intra_matrix;
1225
        for(i=1;i<nCoeffs;i++) {
1226
            int j= zigzag_direct[i];
1227
            level = block[j];
1228
            if (level) {
1229
                if (level < 0) {
1230
                    level = -level;
1231
                    level = (int)(level * qscale * quant_matrix[j]) >> 3;
1232
                    level = (level - 1) | 1;
1233
                    level = -level;
1234
                } else {
1235
                    level = (int)(level * qscale * quant_matrix[j]) >> 3;
1236
                    level = (level - 1) | 1;
1237
                }
1238
#ifdef PARANOID
1239
                if (level < -2048 || level > 2047)
1240
                    fprintf(stderr, "unquant error %d %d\n", i, level);
1241
#endif
1242
                block[j] = level;
1243
            }
1244
        }
1245
    } else {
1246
        i = 0;
1247
        quant_matrix = s->non_intra_matrix;
1248
        for(i=1;i<nCoeffs;i++) {
1249
            int j= zigzag_direct[i];
1250
            level = block[j];
1251
            if (level) {
1252
                if (level < 0) {
1253
                    level = -level;
1254
                    level = (((level << 1) + 1) * qscale *
1255
                             ((int) (quant_matrix[j]))) >> 4;
1256
                    level = (level - 1) | 1;
1257
                    level = -level;
1258
                } else {
1259
                    level = (((level << 1) + 1) * qscale *
1260
                             ((int) (quant_matrix[j]))) >> 4;
1261
                    level = (level - 1) | 1;
1262
                }
1263
#ifdef PARANOID
1264
                if (level < -2048 || level > 2047)
1265
                    fprintf(stderr, "unquant error %d %d\n", i, level);
1266
#endif
1267
                block[j] = level;
1268
            }
1269
        }
1270
    }
1271
}
1272

    
1273
static void dct_unquantize_h263_c(MpegEncContext *s, 
1274
                                  DCTELEM *block, int n, int qscale)
1275
{
1276
    int i, level, qmul, qadd;
1277
    int nCoeffs;
1278

    
1279
    if (s->mb_intra) {
1280
        if (n < 4) 
1281
            block[0] = block[0] * s->y_dc_scale;
1282
        else
1283
            block[0] = block[0] * s->c_dc_scale;
1284
        i = 1;
1285
        nCoeffs= 64; //does not allways use zigzag table 
1286
    } else {
1287
        i = 0;
1288
        nCoeffs= zigzag_end[ s->block_last_index[n] ];
1289
    }
1290

    
1291
    qmul = s->qscale << 1;
1292
    qadd = (s->qscale - 1) | 1;
1293

    
1294
    for(;i<nCoeffs;i++) {
1295
        level = block[i];
1296
        if (level) {
1297
            if (level < 0) {
1298
                level = level * qmul - qadd;
1299
            } else {
1300
                level = level * qmul + qadd;
1301
            }
1302
#ifdef PARANOID
1303
                if (level < -2048 || level > 2047)
1304
                    fprintf(stderr, "unquant error %d %d\n", i, level);
1305
#endif
1306
            block[i] = level;
1307
        }
1308
    }
1309
}
1310

    
1311
/* rate control */
1312

    
1313
/* an I frame is I_FRAME_SIZE_RATIO bigger than a P frame */
1314
#define I_FRAME_SIZE_RATIO 3.0
1315
#define QSCALE_K           20
1316

    
1317
static void rate_control_init(MpegEncContext *s)
1318
{
1319
    s->wanted_bits = 0;
1320

    
1321
    if (s->intra_only) {
1322
        s->I_frame_bits = ((INT64)s->bit_rate * FRAME_RATE_BASE) / s->frame_rate;
1323
        s->P_frame_bits = s->I_frame_bits;
1324
    } else {
1325
        s->P_frame_bits = (int) ((float)(s->gop_size * s->bit_rate) / 
1326
                                 (float)((float)s->frame_rate / FRAME_RATE_BASE * (I_FRAME_SIZE_RATIO + s->gop_size - 1)));
1327
        s->I_frame_bits = (int)(s->P_frame_bits * I_FRAME_SIZE_RATIO);
1328
    }
1329
    
1330
#if defined(DEBUG)
1331
    printf("I_frame_size=%d P_frame_size=%d\n",
1332
           s->I_frame_bits, s->P_frame_bits);
1333
#endif
1334
}
1335

    
1336

    
1337
/*
1338
 * This heuristic is rather poor, but at least we do not have to
1339
 * change the qscale at every macroblock.
1340
 */
1341
static int rate_estimate_qscale(MpegEncContext *s)
1342
{
1343
    INT64 diff, total_bits = s->total_bits;
1344
    float q;
1345
    int qscale, qmin;
1346

    
1347
    if (s->pict_type == I_TYPE) {
1348
        s->wanted_bits += s->I_frame_bits;
1349
    } else {
1350
        s->wanted_bits += s->P_frame_bits;
1351
    }
1352
    diff = s->wanted_bits - total_bits;
1353
    q = 31.0 - (float)diff / (QSCALE_K * s->mb_height * s->mb_width);
1354
    /* adjust for I frame */
1355
    if (s->pict_type == I_TYPE && !s->intra_only) {
1356
        q /= I_FRAME_SIZE_RATIO;
1357
    }
1358

    
1359
    /* using a too small Q scale leeds to problems in mpeg1 and h263
1360
       because AC coefficients are clamped to 255 or 127 */
1361
    qmin = 3;
1362
    if (q < qmin)
1363
        q = qmin;
1364
    else if (q > 31)
1365
        q = 31;
1366
    qscale = (int)(q + 0.5);
1367
#if defined(DEBUG)
1368
    printf("%d: total=%0.0f br=%0.1f diff=%d qest=%0.1f\n", 
1369
           s->picture_number, 
1370
           (double)total_bits, 
1371
           (float)s->frame_rate / FRAME_RATE_BASE * 
1372
           total_bits / s->picture_number, 
1373
           diff, q);
1374
#endif
1375
    return qscale;
1376
}
1377

    
1378
AVCodec mpeg1video_encoder = {
1379
    "mpeg1video",
1380
    CODEC_TYPE_VIDEO,
1381
    CODEC_ID_MPEG1VIDEO,
1382
    sizeof(MpegEncContext),
1383
    MPV_encode_init,
1384
    MPV_encode_picture,
1385
    MPV_encode_end,
1386
};
1387

    
1388
AVCodec h263_encoder = {
1389
    "h263",
1390
    CODEC_TYPE_VIDEO,
1391
    CODEC_ID_H263,
1392
    sizeof(MpegEncContext),
1393
    MPV_encode_init,
1394
    MPV_encode_picture,
1395
    MPV_encode_end,
1396
};
1397

    
1398
AVCodec h263p_encoder = {
1399
    "h263p",
1400
    CODEC_TYPE_VIDEO,
1401
    CODEC_ID_H263P,
1402
    sizeof(MpegEncContext),
1403
    MPV_encode_init,
1404
    MPV_encode_picture,
1405
    MPV_encode_end,
1406
};
1407

    
1408
AVCodec rv10_encoder = {
1409
    "rv10",
1410
    CODEC_TYPE_VIDEO,
1411
    CODEC_ID_RV10,
1412
    sizeof(MpegEncContext),
1413
    MPV_encode_init,
1414
    MPV_encode_picture,
1415
    MPV_encode_end,
1416
};
1417

    
1418
AVCodec mjpeg_encoder = {
1419
    "mjpeg",
1420
    CODEC_TYPE_VIDEO,
1421
    CODEC_ID_MJPEG,
1422
    sizeof(MpegEncContext),
1423
    MPV_encode_init,
1424
    MPV_encode_picture,
1425
    MPV_encode_end,
1426
};
1427

    
1428
AVCodec mpeg4_encoder = {
1429
    "mpeg4",
1430
    CODEC_TYPE_VIDEO,
1431
    CODEC_ID_MPEG4,
1432
    sizeof(MpegEncContext),
1433
    MPV_encode_init,
1434
    MPV_encode_picture,
1435
    MPV_encode_end,
1436
};
1437

    
1438
AVCodec msmpeg4_encoder = {
1439
    "msmpeg4",
1440
    CODEC_TYPE_VIDEO,
1441
    CODEC_ID_MSMPEG4,
1442
    sizeof(MpegEncContext),
1443
    MPV_encode_init,
1444
    MPV_encode_picture,
1445
    MPV_encode_end,
1446
};