]> git.sesse.net Git - ffmpeg/blob - libavcodec/mpegvideo.c
divx 5.01 support
[ffmpeg] / libavcodec / mpegvideo.c
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 & b-frame 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 dct_unquantize_mpeg1_c(MpegEncContext *s, 
35                                    DCTELEM *block, int n, int qscale);
36 static void dct_unquantize_mpeg2_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
54 /* for jpeg fast DCT */
55 #define CONST_BITS 14
56
57 static const unsigned short aanscales[64] = {
58     /* precomputed values scaled up by 14 bits */
59     16384, 22725, 21407, 19266, 16384, 12873,  8867,  4520,
60     22725, 31521, 29692, 26722, 22725, 17855, 12299,  6270,
61     21407, 29692, 27969, 25172, 21407, 16819, 11585,  5906,
62     19266, 26722, 25172, 22654, 19266, 15137, 10426,  5315,
63     16384, 22725, 21407, 19266, 16384, 12873,  8867,  4520,
64     12873, 17855, 16819, 15137, 12873, 10114,  6967,  3552,
65     8867, 12299, 11585, 10426,  8867,  6967,  4799,  2446,
66     4520,  6270,  5906,  5315,  4520,  3552,  2446,  1247
67 };
68
69 static UINT8 h263_chroma_roundtab[16] = {
70     0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2,
71 };
72
73 static UINT16 default_mv_penalty[MAX_FCODE+1][MAX_MV*2+1];
74 static UINT8 default_fcode_tab[MAX_MV*2+1];
75
76 extern UINT8 zigzag_end[64];
77
78 /* default motion estimation */
79 int motion_estimation_method = ME_EPZS;
80
81 static void convert_matrix(int *qmat, UINT16 *qmat16, const UINT16 *quant_matrix, int qscale)
82 {
83     int i;
84
85     if (av_fdct == jpeg_fdct_ifast) {
86         for(i=0;i<64;i++) {
87             /* 16 <= qscale * quant_matrix[i] <= 7905 */
88             /* 19952         <= aanscales[i] * qscale * quant_matrix[i]           <= 249205026 */
89             /* (1<<36)/19952 >= (1<<36)/(aanscales[i] * qscale * quant_matrix[i]) >= (1<<36)/249205026 */
90             /* 3444240       >= (1<<36)/(aanscales[i] * qscale * quant_matrix[i]) >= 275 */
91             
92             qmat[block_permute_op(i)] = (int)((UINT64_C(1) << (QMAT_SHIFT + 11)) / 
93                             (aanscales[i] * qscale * quant_matrix[block_permute_op(i)]));
94         }
95     } else {
96         for(i=0;i<64;i++) {
97             /* We can safely suppose that 16 <= quant_matrix[i] <= 255
98                So 16           <= qscale * quant_matrix[i]             <= 7905
99                so (1<<19) / 16 >= (1<<19) / (qscale * quant_matrix[i]) >= (1<<19) / 7905
100                so 32768        >= (1<<19) / (qscale * quant_matrix[i]) >= 67
101             */
102             qmat[i]   = (1 << QMAT_SHIFT_MMX) / (qscale * quant_matrix[i]);
103             qmat16[i] = (1 << QMAT_SHIFT_MMX) / (qscale * quant_matrix[block_permute_op(i)]);
104         }
105     }
106 }
107
108 /* init common structure for both encoder and decoder */
109 int MPV_common_init(MpegEncContext *s)
110 {
111     int c_size, i;
112     UINT8 *pict;
113
114     s->dct_unquantize_h263 = dct_unquantize_h263_c;
115     s->dct_unquantize_mpeg1 = dct_unquantize_mpeg1_c;
116     s->dct_unquantize_mpeg2 = dct_unquantize_mpeg2_c;
117         
118 #ifdef HAVE_MMX
119     MPV_common_init_mmx(s);
120 #endif
121     //setup default unquantizers (mpeg4 might change it later)
122     if(s->out_format == FMT_H263)
123         s->dct_unquantize = s->dct_unquantize_h263;
124     else
125         s->dct_unquantize = s->dct_unquantize_mpeg1;
126     
127     s->mb_width = (s->width + 15) / 16;
128     s->mb_height = (s->height + 15) / 16;
129     s->mb_num = s->mb_width * s->mb_height;
130     s->linesize = s->mb_width * 16 + 2 * EDGE_WIDTH;
131
132     for(i=0;i<3;i++) {
133         int w, h, shift, pict_start;
134
135         w = s->linesize;
136         h = s->mb_height * 16 + 2 * EDGE_WIDTH;
137         shift = (i == 0) ? 0 : 1;
138         c_size = (w >> shift) * (h >> shift);
139         pict_start = (w >> shift) * (EDGE_WIDTH >> shift) + (EDGE_WIDTH >> shift);
140
141         pict = av_mallocz(c_size);
142         if (pict == NULL)
143             goto fail;
144         s->last_picture_base[i] = pict;
145         s->last_picture[i] = pict + pict_start;
146     
147         pict = av_mallocz(c_size);
148         if (pict == NULL)
149             goto fail;
150         s->next_picture_base[i] = pict;
151         s->next_picture[i] = pict + pict_start;
152         
153         if (s->has_b_frames) {
154             pict = av_mallocz(c_size);
155             if (pict == NULL) 
156                 goto fail;
157             s->aux_picture_base[i] = pict;
158             s->aux_picture[i] = pict + pict_start;
159         }
160     }
161     
162     if (s->encoding) {
163         int j;
164         int mv_table_size= (s->mb_width+2)*(s->mb_height+2);
165
166         /* Allocate MB type table */
167         s->mb_type = av_mallocz(s->mb_num * sizeof(char));
168         if (s->mb_type == NULL) {
169             perror("malloc");
170             goto fail;
171         }
172         
173         s->mb_var = av_mallocz(s->mb_num * sizeof(INT16));
174         if (s->mb_var == NULL) {
175             perror("malloc");
176             goto fail;
177         }
178
179         /* Allocate MV tables */
180         s->p_mv_table = av_mallocz(mv_table_size * 2 * sizeof(INT16));
181         if (s->p_mv_table == NULL) {
182             perror("malloc");
183             goto fail;
184         }
185         s->last_p_mv_table = av_mallocz(mv_table_size * 2 * sizeof(INT16));
186         if (s->last_p_mv_table == NULL) {
187             perror("malloc");
188             goto fail;
189         }
190         s->b_forw_mv_table = av_mallocz(mv_table_size * 2 * sizeof(INT16));
191         if (s->b_forw_mv_table == NULL) {
192             perror("malloc");
193             goto fail;
194         }
195         s->b_back_mv_table = av_mallocz(mv_table_size * 2 * sizeof(INT16));
196         if (s->b_back_mv_table == NULL) {
197             perror("malloc");
198             goto fail;
199         }
200         s->b_bidir_forw_mv_table = av_mallocz(mv_table_size * 2 * sizeof(INT16));
201         if (s->b_bidir_forw_mv_table == NULL) {
202             perror("malloc");
203             goto fail;
204         }
205         s->b_bidir_back_mv_table = av_mallocz(mv_table_size * 2 * sizeof(INT16));
206         if (s->b_bidir_back_mv_table == NULL) {
207             perror("malloc");
208             goto fail;
209         }
210         s->b_direct_forw_mv_table = av_mallocz(mv_table_size * 2 * sizeof(INT16));
211         if (s->b_direct_forw_mv_table == NULL) {
212             perror("malloc");
213             goto fail;
214         }
215         s->b_direct_back_mv_table = av_mallocz(mv_table_size * 2 * sizeof(INT16));
216         if (s->b_direct_back_mv_table == NULL) {
217             perror("malloc");
218             goto fail;
219         }
220         s->b_direct_mv_table = av_mallocz(mv_table_size * 2 * sizeof(INT16));
221         if (s->b_direct_mv_table == NULL) {
222             perror("malloc");
223             goto fail;
224         }
225
226         s->me_scratchpad = av_mallocz( s->linesize*16*3*sizeof(uint8_t));
227         if (s->me_scratchpad == NULL) {
228             perror("malloc");
229             goto fail;
230         }
231
232         if(s->max_b_frames){
233             for(j=0; j<REORDER_BUFFER_SIZE; j++){
234                 int i;
235                 for(i=0;i<3;i++) {
236                     int w, h, shift;
237
238                     w = s->linesize;
239                     h = s->mb_height * 16;
240                     shift = (i == 0) ? 0 : 1;
241                     c_size = (w >> shift) * (h >> shift);
242
243                     pict = av_mallocz(c_size);
244                     if (pict == NULL)
245                         goto fail;
246                     s->picture_buffer[j][i] = pict;
247                 }
248             }
249         }
250     }
251     
252     if (s->out_format == FMT_H263 || s->encoding) {
253         int size;
254         /* MV prediction */
255         size = (2 * s->mb_width + 2) * (2 * s->mb_height + 2);
256         s->motion_val = malloc(size * 2 * sizeof(INT16));
257         if (s->motion_val == NULL)
258             goto fail;
259         memset(s->motion_val, 0, size * 2 * sizeof(INT16));
260     }
261
262     if (s->h263_pred || s->h263_plus) {
263         int y_size, c_size, i, size;
264         
265         /* dc values */
266
267         y_size = (2 * s->mb_width + 2) * (2 * s->mb_height + 2);
268         c_size = (s->mb_width + 2) * (s->mb_height + 2);
269         size = y_size + 2 * c_size;
270         s->dc_val[0] = malloc(size * sizeof(INT16));
271         if (s->dc_val[0] == NULL)
272             goto fail;
273         s->dc_val[1] = s->dc_val[0] + y_size;
274         s->dc_val[2] = s->dc_val[1] + c_size;
275         for(i=0;i<size;i++)
276             s->dc_val[0][i] = 1024;
277
278         /* ac values */
279         s->ac_val[0] = av_mallocz(size * sizeof(INT16) * 16);
280         if (s->ac_val[0] == NULL)
281             goto fail;
282         s->ac_val[1] = s->ac_val[0] + y_size;
283         s->ac_val[2] = s->ac_val[1] + c_size;
284         
285         /* cbp values */
286         s->coded_block = av_mallocz(y_size);
287         if (!s->coded_block)
288             goto fail;
289
290         /* which mb is a intra block */
291         s->mbintra_table = av_mallocz(s->mb_num);
292         if (!s->mbintra_table)
293             goto fail;
294         memset(s->mbintra_table, 1, s->mb_num);
295         
296         /* divx501 bitstream reorder buffer */
297         s->bitstream_buffer= av_mallocz(BITSTREAM_BUFFER_SIZE);
298         if (!s->bitstream_buffer)
299             goto fail;
300     }
301     /* default structure is frame */
302     s->picture_structure = PICT_FRAME;
303
304     /* init macroblock skip table */
305     s->mbskip_table = av_mallocz(s->mb_num);
306     if (!s->mbskip_table)
307         goto fail;
308     
309     s->block= s->blocks[0];
310
311     s->context_initialized = 1;
312     return 0;
313  fail:
314     MPV_common_end(s);
315     return -1;
316 }
317
318 #define CHECK_FREE(p)\
319 {\
320     if(p) free(p);\
321     p= NULL;\
322 }
323
324 /* init common structure for both encoder and decoder */
325 void MPV_common_end(MpegEncContext *s)
326 {
327     int i;
328
329     CHECK_FREE(s->mb_type);
330     CHECK_FREE(s->mb_var);
331     CHECK_FREE(s->p_mv_table);
332     CHECK_FREE(s->last_p_mv_table);
333     CHECK_FREE(s->b_forw_mv_table);
334     CHECK_FREE(s->b_back_mv_table);
335     CHECK_FREE(s->b_bidir_forw_mv_table);
336     CHECK_FREE(s->b_bidir_back_mv_table);
337     CHECK_FREE(s->b_direct_forw_mv_table);
338     CHECK_FREE(s->b_direct_back_mv_table);
339     CHECK_FREE(s->b_direct_mv_table);
340     CHECK_FREE(s->motion_val);
341     CHECK_FREE(s->dc_val[0]);
342     CHECK_FREE(s->ac_val[0]);
343     CHECK_FREE(s->coded_block);
344     CHECK_FREE(s->mbintra_table);
345     CHECK_FREE(s->me_scratchpad);
346
347     CHECK_FREE(s->mbskip_table);
348     CHECK_FREE(s->bitstream_buffer);
349     for(i=0;i<3;i++) {
350         int j;
351         CHECK_FREE(s->last_picture_base[i]);
352         CHECK_FREE(s->next_picture_base[i]);
353         CHECK_FREE(s->aux_picture_base[i]);
354         for(j=0; j<REORDER_BUFFER_SIZE; j++){
355             CHECK_FREE(s->picture_buffer[j][i]);
356         }
357     }
358     s->context_initialized = 0;
359 }
360
361 /* init video encoder */
362 int MPV_encode_init(AVCodecContext *avctx)
363 {
364     MpegEncContext *s = avctx->priv_data;
365     int i;
366
367     avctx->pix_fmt = PIX_FMT_YUV420P;
368
369     s->bit_rate = avctx->bit_rate;
370     s->bit_rate_tolerance = avctx->bit_rate_tolerance;
371     s->frame_rate = avctx->frame_rate;
372     s->width = avctx->width;
373     s->height = avctx->height;
374     s->gop_size = avctx->gop_size;
375     s->rtp_mode = avctx->rtp_mode;
376     s->rtp_payload_size = avctx->rtp_payload_size;
377     if (avctx->rtp_callback)
378         s->rtp_callback = avctx->rtp_callback;
379     s->qmin= avctx->qmin;
380     s->qmax= avctx->qmax;
381     s->max_qdiff= avctx->max_qdiff;
382     s->qcompress= avctx->qcompress;
383     s->qblur= avctx->qblur;
384     s->b_quant_factor= avctx->b_quant_factor;
385     s->avctx = avctx;
386     s->aspect_ratio_info= avctx->aspect_ratio_info;
387     s->flags= avctx->flags;
388     s->max_b_frames= avctx->max_b_frames;
389     s->rc_strategy= avctx->rc_strategy;
390     s->b_frame_strategy= avctx->b_frame_strategy;
391     
392     if (s->gop_size <= 1) {
393         s->intra_only = 1;
394         s->gop_size = 12;
395     } else {
396         s->intra_only = 0;
397     }
398     
399     /* ME algorithm */
400     if (avctx->me_method == 0)
401         /* For compatibility */
402         s->me_method = motion_estimation_method;
403     else
404         s->me_method = avctx->me_method;
405         
406     /* Fixed QSCALE */
407     s->fixed_qscale = (avctx->flags & CODEC_FLAG_QSCALE);
408     
409     switch(avctx->codec->id) {
410     case CODEC_ID_MPEG1VIDEO:
411         s->out_format = FMT_MPEG1;
412         break;
413     case CODEC_ID_MJPEG:
414         s->out_format = FMT_MJPEG;
415         s->intra_only = 1; /* force intra only for jpeg */
416         s->mjpeg_write_tables = 1; /* write all tables */
417         s->mjpeg_vsample[0] = 2; /* set up default sampling factors */
418         s->mjpeg_vsample[1] = 1; /* the only currently supported values */
419         s->mjpeg_vsample[2] = 1; 
420         s->mjpeg_hsample[0] = 2; 
421         s->mjpeg_hsample[1] = 1; 
422         s->mjpeg_hsample[2] = 1; 
423         if (mjpeg_init(s) < 0)
424             return -1;
425         break;
426     case CODEC_ID_H263:
427         if (h263_get_picture_format(s->width, s->height) == 7) {
428             printf("Input picture size isn't suitable for h263 codec! try h263+\n");
429             return -1;
430         }
431         s->out_format = FMT_H263;
432         break;
433     case CODEC_ID_H263P:
434         s->out_format = FMT_H263;
435         s->rtp_mode = 1;
436         s->rtp_payload_size = 1200; 
437         s->h263_plus = 1;
438         s->unrestricted_mv = 1;
439         
440         /* These are just to be sure */
441         s->umvplus = 0;
442         s->umvplus_dec = 0;
443         break;
444     case CODEC_ID_RV10:
445         s->out_format = FMT_H263;
446         s->h263_rv10 = 1;
447         break;
448     case CODEC_ID_MPEG4:
449         s->out_format = FMT_H263;
450         s->h263_pred = 1;
451         s->unrestricted_mv = 1;
452         s->has_b_frames= s->max_b_frames ? 1 : 0;
453         break;
454     case CODEC_ID_MSMPEG4V1:
455         s->out_format = FMT_H263;
456         s->h263_msmpeg4 = 1;
457         s->h263_pred = 1;
458         s->unrestricted_mv = 1;
459         s->msmpeg4_version= 1;
460         break;
461     case CODEC_ID_MSMPEG4V2:
462         s->out_format = FMT_H263;
463         s->h263_msmpeg4 = 1;
464         s->h263_pred = 1;
465         s->unrestricted_mv = 1;
466         s->msmpeg4_version= 2;
467         break;
468     case CODEC_ID_MSMPEG4V3:
469         s->out_format = FMT_H263;
470         s->h263_msmpeg4 = 1;
471         s->h263_pred = 1;
472         s->unrestricted_mv = 1;
473         s->msmpeg4_version= 3;
474         break;
475     default:
476         return -1;
477     }
478     
479     if((s->flags&CODEC_FLAG_4MV) && !(s->flags&CODEC_FLAG_HQ)){
480         printf("4MV is currently only supported in HQ mode\n");
481         return -1;
482     }
483
484     { /* set up some save defaults, some codecs might override them later */
485         static int done=0;
486         if(!done){
487             int i;
488             done=1;
489             memset(default_mv_penalty, 0, sizeof(UINT16)*(MAX_FCODE+1)*(2*MAX_MV+1));
490             memset(default_fcode_tab , 0, sizeof(UINT8)*(2*MAX_MV+1));
491
492             for(i=-16; i<16; i++){
493                 default_fcode_tab[i + MAX_MV]= 1;
494             }
495         }
496     }
497     s->mv_penalty= default_mv_penalty;
498     s->fcode_tab= default_fcode_tab;
499
500     if (s->out_format == FMT_H263)
501         h263_encode_init(s);
502     else if (s->out_format == FMT_MPEG1)
503         mpeg1_encode_init(s);
504
505     /* dont use mv_penalty table for crap MV as it would be confused */
506     if (s->me_method < ME_EPZS) s->mv_penalty = default_mv_penalty;
507
508     s->encoding = 1;
509
510     /* init */
511     if (MPV_common_init(s) < 0)
512         return -1;
513     
514     /* init default q matrix */
515     for(i=0;i<64;i++) {
516         s->intra_matrix[i] = default_intra_matrix[i];
517         s->non_intra_matrix[i] = default_non_intra_matrix[i];
518     }
519
520     if(ff_rate_control_init(s) < 0)
521         return -1;
522
523     s->picture_number = 0;
524     s->picture_in_gop_number = 0;
525     s->fake_picture_number = 0;
526     /* motion detector init */
527     s->f_code = 1;
528     s->b_code = 1;
529
530     return 0;
531 }
532
533 int MPV_encode_end(AVCodecContext *avctx)
534 {
535     MpegEncContext *s = avctx->priv_data;
536
537 #ifdef STATS
538     print_stats();
539 #endif
540
541     ff_rate_control_uninit(s);
542
543     MPV_common_end(s);
544     if (s->out_format == FMT_MJPEG)
545         mjpeg_close(s);
546       
547     return 0;
548 }
549
550 /* draw the edges of width 'w' of an image of size width, height */
551 static void draw_edges_c(UINT8 *buf, int wrap, int width, int height, int w)
552 {
553     UINT8 *ptr, *last_line;
554     int i;
555
556     last_line = buf + (height - 1) * wrap;
557     for(i=0;i<w;i++) {
558         /* top and bottom */
559         memcpy(buf - (i + 1) * wrap, buf, width);
560         memcpy(last_line + (i + 1) * wrap, last_line, width);
561     }
562     /* left and right */
563     ptr = buf;
564     for(i=0;i<height;i++) {
565         memset(ptr - w, ptr[0], w);
566         memset(ptr + width, ptr[width-1], w);
567         ptr += wrap;
568     }
569     /* corners */
570     for(i=0;i<w;i++) {
571         memset(buf - (i + 1) * wrap - w, buf[0], w); /* top left */
572         memset(buf - (i + 1) * wrap + width, buf[width-1], w); /* top right */
573         memset(last_line + (i + 1) * wrap - w, last_line[0], w); /* top left */
574         memset(last_line + (i + 1) * wrap + width, last_line[width-1], w); /* top right */
575     }
576 }
577
578 /* generic function for encode/decode called before a frame is coded/decoded */
579 void MPV_frame_start(MpegEncContext *s)
580 {
581     int i;
582     UINT8 *tmp;
583
584     s->mb_skiped = 0;
585     if (s->pict_type == B_TYPE) {
586         for(i=0;i<3;i++) {
587             s->current_picture[i] = s->aux_picture[i];
588         }
589     } else {
590         for(i=0;i<3;i++) {
591             /* swap next and last */
592             tmp = s->last_picture[i];
593             s->last_picture[i] = s->next_picture[i];
594             s->next_picture[i] = tmp;
595             s->current_picture[i] = tmp;
596         }
597     }
598 }
599
600 /* generic function for encode/decode called after a frame has been coded/decoded */
601 void MPV_frame_end(MpegEncContext *s)
602 {
603     /* draw edge for correct motion prediction if outside */
604     if (s->pict_type != B_TYPE && !s->intra_only) {
605       if(s->avctx==NULL || s->avctx->codec->id!=CODEC_ID_MPEG4 || s->divx_version==500){
606         draw_edges(s->current_picture[0], s->linesize, s->mb_width*16, s->mb_height*16, EDGE_WIDTH);
607         draw_edges(s->current_picture[1], s->linesize/2, s->mb_width*8, s->mb_height*8, EDGE_WIDTH/2);
608         draw_edges(s->current_picture[2], s->linesize/2, s->mb_width*8, s->mb_height*8, EDGE_WIDTH/2);
609       }else{
610         /* mpeg4? / opendivx / xvid */
611         draw_edges(s->current_picture[0], s->linesize, s->width, s->height, EDGE_WIDTH);
612         draw_edges(s->current_picture[1], s->linesize/2, s->width/2, s->height/2, EDGE_WIDTH/2);
613         draw_edges(s->current_picture[2], s->linesize/2, s->width/2, s->height/2, EDGE_WIDTH/2);
614       }
615     }
616     emms_c();
617     
618     if(s->pict_type!=B_TYPE){
619         s->last_non_b_pict_type= s->pict_type;
620         s->last_non_b_qscale= s->qscale;
621         s->last_non_b_mc_mb_var= s->mc_mb_var;
622     }
623 }
624
625 /* reorder input for encoding */
626 void reorder_input(MpegEncContext *s, AVPicture *pict)
627 {
628     int i, j, index;
629             
630     if(s->max_b_frames > FF_MAX_B_FRAMES) s->max_b_frames= FF_MAX_B_FRAMES;
631
632 //        delay= s->max_b_frames+1; (or 0 if no b frames cuz decoder diff)
633
634     for(j=0; j<REORDER_BUFFER_SIZE-1; j++){
635         s->coded_order[j]= s->coded_order[j+1];
636     }
637     s->coded_order[j].picture[0]= s->coded_order[j].picture[1]= s->coded_order[j].picture[2]= NULL; //catch uninitalized buffers
638
639     switch(s->input_pict_type){
640     default: 
641     case I_TYPE:
642     case S_TYPE:
643     case P_TYPE:
644         index= s->max_b_frames - s->b_frames_since_non_b;
645         s->b_frames_since_non_b=0;
646         break;            
647     case B_TYPE:
648         index= s->max_b_frames + 1;
649         s->b_frames_since_non_b++;
650         break;          
651     }
652 //printf("index:%d type:%d strides: %d %d\n", index, s->input_pict_type, pict->linesize[0], s->linesize);
653     if(   (index==0 || (s->flags&CODEC_FLAG_INPUT_PRESERVED))
654        && pict->linesize[0] == s->linesize
655        && pict->linesize[1] == s->linesize>>1
656        && pict->linesize[2] == s->linesize>>1){
657 //printf("ptr\n");
658         for(i=0; i<3; i++){
659             s->coded_order[index].picture[i]= pict->data[i];
660         }
661     }else{
662 //printf("copy\n");
663         for(i=0; i<3; i++){
664             uint8_t *src = pict->data[i];
665             uint8_t *dest;
666             int src_wrap = pict->linesize[i];
667             int dest_wrap = s->linesize;
668             int w = s->width;
669             int h = s->height;
670
671             if(index==0) dest= s->last_picture[i]+16; //is current_picture indeed but the switch hapens after reordering
672             else         dest= s->picture_buffer[s->picture_buffer_index][i];
673
674             if (i >= 1) {
675                 dest_wrap >>= 1;
676                 w >>= 1;
677                 h >>= 1;
678             }
679
680             s->coded_order[index].picture[i]= dest;
681             for(j=0;j<h;j++) {
682                 memcpy(dest, src, w);
683                 dest += dest_wrap;
684                 src += src_wrap;
685             }
686         }
687         if(index!=0){
688             s->picture_buffer_index++;
689             if(s->picture_buffer_index >= REORDER_BUFFER_SIZE-1) s->picture_buffer_index=0;
690         }
691     }
692     s->coded_order[index].pict_type = s->input_pict_type;
693     s->coded_order[index].qscale    = s->input_qscale;
694     s->coded_order[index].force_type= s->force_input_type;
695     s->coded_order[index].picture_in_gop_number= s->input_picture_in_gop_number;
696     s->coded_order[index].picture_number= s->input_picture_number;
697
698     for(i=0; i<3; i++){
699         s->new_picture[i]= s->coded_order[0].picture[i];
700     }
701 }
702
703 int MPV_encode_picture(AVCodecContext *avctx,
704                        unsigned char *buf, int buf_size, void *data)
705 {
706     MpegEncContext *s = avctx->priv_data;
707     AVPicture *pict = data;
708
709     s->input_qscale = avctx->quality;
710
711     init_put_bits(&s->pb, buf, buf_size, NULL, NULL);
712
713     if(avctx->flags&CODEC_FLAG_TYPE){
714         s->input_pict_type=
715         s->force_input_type= avctx->key_frame ? I_TYPE : P_TYPE;
716     }else if(s->flags&CODEC_FLAG_PASS2){
717         s->input_pict_type=
718         s->force_input_type= s->rc_context.entry[s->input_picture_number].new_pict_type;
719     }else{
720         s->force_input_type=0;
721         if (!s->intra_only) {
722             /* first picture of GOP is intra */
723             if (s->input_picture_in_gop_number % s->gop_size==0){
724                 s->input_pict_type = I_TYPE;
725             }else if(s->max_b_frames==0){
726                 s->input_pict_type = P_TYPE;
727             }else{
728                 if(s->b_frames_since_non_b < s->max_b_frames) //FIXME more IQ
729                     s->input_pict_type = B_TYPE;
730                 else
731                     s->input_pict_type = P_TYPE;
732             }
733         } else {
734             s->input_pict_type = I_TYPE;
735         }
736     }
737
738     if(s->input_pict_type==I_TYPE)
739         s->input_picture_in_gop_number=0;
740     
741     reorder_input(s, pict);
742     
743     /* output? */
744     if(s->coded_order[0].picture[0]){
745
746         s->pict_type= s->coded_order[0].pict_type;
747         if (s->fixed_qscale) /* the ratecontrol needs the last qscale so we dont touch it for CBR */
748             s->qscale= s->coded_order[0].qscale;
749         s->force_type= s->coded_order[0].force_type;
750         s->picture_in_gop_number= s->coded_order[0].picture_in_gop_number;
751         s->picture_number= s->coded_order[0].picture_number;
752
753         MPV_frame_start(s);
754
755         encode_picture(s, s->picture_number);
756         avctx->key_frame = (s->pict_type == I_TYPE);
757         avctx->header_bits = s->header_bits;
758         avctx->mv_bits     = s->mv_bits;
759         avctx->misc_bits   = s->misc_bits;
760         avctx->i_tex_bits  = s->i_tex_bits;
761         avctx->p_tex_bits  = s->p_tex_bits;
762         avctx->i_count     = s->i_count;
763         avctx->p_count     = s->p_count;
764         avctx->skip_count  = s->skip_count;
765
766         MPV_frame_end(s);
767
768         if (s->out_format == FMT_MJPEG)
769             mjpeg_picture_trailer(s);
770
771         avctx->quality = s->qscale;
772         
773         if(s->flags&CODEC_FLAG_PASS1)
774             ff_write_pass1_stats(s);
775     }
776
777     s->input_picture_number++;
778     s->input_picture_in_gop_number++;
779
780     flush_put_bits(&s->pb);
781     s->frame_bits  = (pbBufPtr(&s->pb) - s->pb.buf) * 8;
782     if(s->pict_type==B_TYPE) s->pb_frame_bits+= s->frame_bits;
783     else                     s->pb_frame_bits= s->frame_bits;
784
785     s->total_bits += s->frame_bits;
786     avctx->frame_bits  = s->frame_bits;
787 //printf("fcode: %d, type: %d, head: %d, mv: %d, misc: %d, frame: %d, itex: %d, ptex: %d\n", 
788 //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);
789
790     if (avctx->get_psnr) {
791         /* At this point pict->data should have the original frame   */
792         /* an s->current_picture should have the coded/decoded frame */
793         get_psnr(pict->data, s->current_picture,
794                  pict->linesize, s->linesize, avctx);
795 //        printf("%f\n", avctx->psnr_y);
796     }
797     return pbBufPtr(&s->pb) - s->pb.buf;
798 }
799
800 static inline void gmc1_motion(MpegEncContext *s,
801                                UINT8 *dest_y, UINT8 *dest_cb, UINT8 *dest_cr,
802                                int dest_offset,
803                                UINT8 **ref_picture, int src_offset,
804                                int h)
805 {
806     UINT8 *ptr;
807     int offset, src_x, src_y, linesize;
808     int motion_x, motion_y;
809
810     if(s->real_sprite_warping_points>1) printf("more than 1 warp point isnt supported\n");
811     motion_x= s->sprite_offset[0][0];
812     motion_y= s->sprite_offset[0][1];
813     src_x = s->mb_x * 16 + (motion_x >> (s->sprite_warping_accuracy+1));
814     src_y = s->mb_y * 16 + (motion_y >> (s->sprite_warping_accuracy+1));
815     motion_x<<=(3-s->sprite_warping_accuracy);
816     motion_y<<=(3-s->sprite_warping_accuracy);
817     src_x = clip(src_x, -16, s->width);
818     if (src_x == s->width)
819         motion_x =0;
820     src_y = clip(src_y, -16, s->height);
821     if (src_y == s->height)
822         motion_y =0;
823     
824     linesize = s->linesize;
825     ptr = ref_picture[0] + (src_y * linesize) + src_x + src_offset;
826
827     dest_y+=dest_offset;
828     gmc1(dest_y  , ptr  , linesize, h, motion_x&15, motion_y&15, s->no_rounding);
829     gmc1(dest_y+8, ptr+8, linesize, h, motion_x&15, motion_y&15, s->no_rounding);
830
831     motion_x= s->sprite_offset[1][0];
832     motion_y= s->sprite_offset[1][1];
833     src_x = s->mb_x * 8 + (motion_x >> (s->sprite_warping_accuracy+1));
834     src_y = s->mb_y * 8 + (motion_y >> (s->sprite_warping_accuracy+1));
835     motion_x<<=(3-s->sprite_warping_accuracy);
836     motion_y<<=(3-s->sprite_warping_accuracy);
837     src_x = clip(src_x, -8, s->width>>1);
838     if (src_x == s->width>>1)
839         motion_x =0;
840     src_y = clip(src_y, -8, s->height>>1);
841     if (src_y == s->height>>1)
842         motion_y =0;
843
844     offset = (src_y * linesize>>1) + src_x + (src_offset>>1);
845     ptr = ref_picture[1] + offset;
846     gmc1(dest_cb + (dest_offset>>1), ptr, linesize>>1, h>>1, motion_x&15, motion_y&15, s->no_rounding);
847     ptr = ref_picture[2] + offset;
848     gmc1(dest_cr + (dest_offset>>1), ptr, linesize>>1, h>>1, motion_x&15, motion_y&15, s->no_rounding);
849     
850     return;
851 }
852
853 /* apply one mpeg motion vector to the three components */
854 static inline void mpeg_motion(MpegEncContext *s,
855                                UINT8 *dest_y, UINT8 *dest_cb, UINT8 *dest_cr,
856                                int dest_offset,
857                                UINT8 **ref_picture, int src_offset,
858                                int field_based, op_pixels_func *pix_op,
859                                int motion_x, int motion_y, int h)
860 {
861     UINT8 *ptr;
862     int dxy, offset, mx, my, src_x, src_y, height, linesize;
863 if(s->quarter_sample)
864 {
865     motion_x>>=1;
866     motion_y>>=1;
867 }
868
869     dxy = ((motion_y & 1) << 1) | (motion_x & 1);
870     src_x = s->mb_x * 16 + (motion_x >> 1);
871     src_y = s->mb_y * (16 >> field_based) + (motion_y >> 1);
872                 
873     /* WARNING: do no forget half pels */
874     height = s->height >> field_based;
875     src_x = clip(src_x, -16, s->width);
876     if (src_x == s->width)
877         dxy &= ~1;
878     src_y = clip(src_y, -16, height);
879     if (src_y == height)
880         dxy &= ~2;
881     linesize = s->linesize << field_based;
882     ptr = ref_picture[0] + (src_y * linesize) + (src_x) + src_offset;
883     dest_y += dest_offset;
884     pix_op[dxy](dest_y, ptr, linesize, h);
885     pix_op[dxy](dest_y + 8, ptr + 8, linesize, h);
886
887     if (s->out_format == FMT_H263) {
888         dxy = 0;
889         if ((motion_x & 3) != 0)
890             dxy |= 1;
891         if ((motion_y & 3) != 0)
892             dxy |= 2;
893         mx = motion_x >> 2;
894         my = motion_y >> 2;
895     } else {
896         mx = motion_x / 2;
897         my = motion_y / 2;
898         dxy = ((my & 1) << 1) | (mx & 1);
899         mx >>= 1;
900         my >>= 1;
901     }
902     
903     src_x = s->mb_x * 8 + mx;
904     src_y = s->mb_y * (8 >> field_based) + my;
905     src_x = clip(src_x, -8, s->width >> 1);
906     if (src_x == (s->width >> 1))
907         dxy &= ~1;
908     src_y = clip(src_y, -8, height >> 1);
909     if (src_y == (height >> 1))
910         dxy &= ~2;
911
912     offset = (src_y * (linesize >> 1)) + src_x + (src_offset >> 1);
913     ptr = ref_picture[1] + offset;
914     pix_op[dxy](dest_cb + (dest_offset >> 1), ptr, linesize >> 1, h >> 1);
915     ptr = ref_picture[2] + offset;
916     pix_op[dxy](dest_cr + (dest_offset >> 1), ptr, linesize >> 1, h >> 1);
917 }
918
919 static inline void qpel_motion(MpegEncContext *s,
920                                UINT8 *dest_y, UINT8 *dest_cb, UINT8 *dest_cr,
921                                int dest_offset,
922                                UINT8 **ref_picture, int src_offset,
923                                int field_based, op_pixels_func *pix_op,
924                                qpel_mc_func *qpix_op,
925                                int motion_x, int motion_y, int h)
926 {
927     UINT8 *ptr;
928     int dxy, offset, mx, my, src_x, src_y, height, linesize;
929
930     dxy = ((motion_y & 3) << 2) | (motion_x & 3);
931     src_x = s->mb_x * 16 + (motion_x >> 2);
932     src_y = s->mb_y * (16 >> field_based) + (motion_y >> 2);
933
934     height = s->height >> field_based;
935     src_x = clip(src_x, -16, s->width);
936     if (src_x == s->width)
937         dxy &= ~3;
938     src_y = clip(src_y, -16, height);
939     if (src_y == height)
940         dxy &= ~12;
941     linesize = s->linesize << field_based;
942     ptr = ref_picture[0] + (src_y * linesize) + src_x + src_offset;
943     dest_y += dest_offset;
944 //printf("%d %d %d\n", src_x, src_y, dxy);
945     qpix_op[dxy](dest_y                 , ptr                 , linesize, linesize, motion_x&3, motion_y&3);
946     qpix_op[dxy](dest_y              + 8, ptr              + 8, linesize, linesize, motion_x&3, motion_y&3);
947     qpix_op[dxy](dest_y + linesize*8    , ptr + linesize*8    , linesize, linesize, motion_x&3, motion_y&3);
948     qpix_op[dxy](dest_y + linesize*8 + 8, ptr + linesize*8 + 8, linesize, linesize, motion_x&3, motion_y&3);
949     
950     mx= (motion_x>>1) | (motion_x&1);
951     my= (motion_y>>1) | (motion_y&1);
952
953     dxy = 0;
954     if ((mx & 3) != 0)
955         dxy |= 1;
956     if ((my & 3) != 0)
957         dxy |= 2;
958     mx = mx >> 2;
959     my = my >> 2;
960     
961     src_x = s->mb_x * 8 + mx;
962     src_y = s->mb_y * (8 >> field_based) + my;
963     src_x = clip(src_x, -8, s->width >> 1);
964     if (src_x == (s->width >> 1))
965         dxy &= ~1;
966     src_y = clip(src_y, -8, height >> 1);
967     if (src_y == (height >> 1))
968         dxy &= ~2;
969
970     offset = (src_y * (linesize >> 1)) + src_x + (src_offset >> 1);
971     ptr = ref_picture[1] + offset;
972     pix_op[dxy](dest_cb + (dest_offset >> 1), ptr, linesize >> 1, h >> 1);
973     ptr = ref_picture[2] + offset;
974     pix_op[dxy](dest_cr + (dest_offset >> 1), ptr, linesize >> 1, h >> 1);
975 }
976
977
978 static inline void MPV_motion(MpegEncContext *s, 
979                               UINT8 *dest_y, UINT8 *dest_cb, UINT8 *dest_cr,
980                               int dir, UINT8 **ref_picture, 
981                               op_pixels_func *pix_op, qpel_mc_func *qpix_op)
982 {
983     int dxy, offset, mx, my, src_x, src_y, motion_x, motion_y;
984     int mb_x, mb_y, i;
985     UINT8 *ptr, *dest;
986
987     mb_x = s->mb_x;
988     mb_y = s->mb_y;
989
990     switch(s->mv_type) {
991     case MV_TYPE_16X16:
992         if(s->mcsel){
993 #if 0
994             mpeg_motion(s, dest_y, dest_cb, dest_cr, 0,
995                         ref_picture, 0,
996                         0, pix_op,
997                         s->sprite_offset[0][0]>>3,
998                         s->sprite_offset[0][1]>>3,
999                         16);
1000 #else
1001             gmc1_motion(s, dest_y, dest_cb, dest_cr, 0,
1002                         ref_picture, 0,
1003                         16);
1004 #endif
1005         }else if(s->quarter_sample && dir==0){ //FIXME
1006             qpel_motion(s, dest_y, dest_cb, dest_cr, 0,
1007                         ref_picture, 0,
1008                         0, pix_op, qpix_op,
1009                         s->mv[dir][0][0], s->mv[dir][0][1], 16);
1010         }else{
1011             mpeg_motion(s, dest_y, dest_cb, dest_cr, 0,
1012                         ref_picture, 0,
1013                         0, pix_op,
1014                         s->mv[dir][0][0], s->mv[dir][0][1], 16);
1015         }           
1016         break;
1017     case MV_TYPE_8X8:
1018         for(i=0;i<4;i++) {
1019             motion_x = s->mv[dir][i][0];
1020             motion_y = s->mv[dir][i][1];
1021
1022             dxy = ((motion_y & 1) << 1) | (motion_x & 1);
1023             src_x = mb_x * 16 + (motion_x >> 1) + (i & 1) * 8;
1024             src_y = mb_y * 16 + (motion_y >> 1) + (i >>1) * 8;
1025                     
1026             /* WARNING: do no forget half pels */
1027             src_x = clip(src_x, -16, s->width);
1028             if (src_x == s->width)
1029                 dxy &= ~1;
1030             src_y = clip(src_y, -16, s->height);
1031             if (src_y == s->height)
1032                 dxy &= ~2;
1033                     
1034             ptr = ref_picture[0] + (src_y * s->linesize) + (src_x);
1035             dest = dest_y + ((i & 1) * 8) + (i >> 1) * 8 * s->linesize;
1036             pix_op[dxy](dest, ptr, s->linesize, 8);
1037         }
1038         /* In case of 8X8, we construct a single chroma motion vector
1039            with a special rounding */
1040         mx = 0;
1041         my = 0;
1042         for(i=0;i<4;i++) {
1043             mx += s->mv[dir][i][0];
1044             my += s->mv[dir][i][1];
1045         }
1046         if (mx >= 0)
1047             mx = (h263_chroma_roundtab[mx & 0xf] + ((mx >> 3) & ~1));
1048         else {
1049             mx = -mx;
1050             mx = -(h263_chroma_roundtab[mx & 0xf] + ((mx >> 3) & ~1));
1051         }
1052         if (my >= 0)
1053             my = (h263_chroma_roundtab[my & 0xf] + ((my >> 3) & ~1));
1054         else {
1055             my = -my;
1056             my = -(h263_chroma_roundtab[my & 0xf] + ((my >> 3) & ~1));
1057         }
1058         dxy = ((my & 1) << 1) | (mx & 1);
1059         mx >>= 1;
1060         my >>= 1;
1061
1062         src_x = mb_x * 8 + mx;
1063         src_y = mb_y * 8 + my;
1064         src_x = clip(src_x, -8, s->width/2);
1065         if (src_x == s->width/2)
1066             dxy &= ~1;
1067         src_y = clip(src_y, -8, s->height/2);
1068         if (src_y == s->height/2)
1069             dxy &= ~2;
1070         
1071         offset = (src_y * (s->linesize >> 1)) + src_x;
1072         ptr = ref_picture[1] + offset;
1073         pix_op[dxy](dest_cb, ptr, s->linesize >> 1, 8);
1074         ptr = ref_picture[2] + offset;
1075         pix_op[dxy](dest_cr, ptr, s->linesize >> 1, 8);
1076         break;
1077     case MV_TYPE_FIELD:
1078         if (s->picture_structure == PICT_FRAME) {
1079             /* top field */
1080             mpeg_motion(s, dest_y, dest_cb, dest_cr, 0,
1081                         ref_picture, s->field_select[dir][0] ? s->linesize : 0,
1082                         1, pix_op,
1083                         s->mv[dir][0][0], s->mv[dir][0][1], 8);
1084             /* bottom field */
1085             mpeg_motion(s, dest_y, dest_cb, dest_cr, s->linesize,
1086                         ref_picture, s->field_select[dir][1] ? s->linesize : 0,
1087                         1, pix_op,
1088                         s->mv[dir][1][0], s->mv[dir][1][1], 8);
1089         } else {
1090             
1091
1092         }
1093         break;
1094     }
1095 }
1096
1097
1098 /* put block[] to dest[] */
1099 static inline void put_dct(MpegEncContext *s, 
1100                            DCTELEM *block, int i, UINT8 *dest, int line_size)
1101 {
1102     if (!s->mpeg2)
1103         s->dct_unquantize(s, block, i, s->qscale);
1104     ff_idct (block);
1105     put_pixels_clamped(block, dest, line_size);
1106 }
1107
1108 /* add block[] to dest[] */
1109 static inline void add_dct(MpegEncContext *s, 
1110                            DCTELEM *block, int i, UINT8 *dest, int line_size)
1111 {
1112     if (s->block_last_index[i] >= 0) {
1113         if (!s->mpeg2)
1114             if(s->encoding || (!s->h263_msmpeg4))
1115                 s->dct_unquantize(s, block, i, s->qscale);
1116
1117         ff_idct (block);
1118         add_pixels_clamped(block, dest, line_size);
1119     }
1120 }
1121
1122 /* generic function called after a macroblock has been parsed by the
1123    decoder or after it has been encoded by the encoder.
1124
1125    Important variables used:
1126    s->mb_intra : true if intra macroblock
1127    s->mv_dir   : motion vector direction
1128    s->mv_type  : motion vector type
1129    s->mv       : motion vector
1130    s->interlaced_dct : true if interlaced dct used (mpeg2)
1131  */
1132 void MPV_decode_mb(MpegEncContext *s, DCTELEM block[6][64])
1133 {
1134     int mb_x, mb_y;
1135     int dct_linesize, dct_offset;
1136     op_pixels_func *op_pix;
1137     qpel_mc_func *op_qpix;
1138
1139     mb_x = s->mb_x;
1140     mb_y = s->mb_y;
1141
1142 #ifdef FF_POSTPROCESS
1143     quant_store[mb_y][mb_x]=s->qscale;
1144     //printf("[%02d][%02d] %d\n",mb_x,mb_y,s->qscale);
1145 #endif
1146
1147     /* update DC predictors for P macroblocks */
1148     if (!s->mb_intra) {
1149         if (s->h263_pred || s->h263_aic) {
1150           if(s->mbintra_table[mb_x + mb_y*s->mb_width])
1151           {
1152             int wrap, xy, v;
1153             s->mbintra_table[mb_x + mb_y*s->mb_width]=0;
1154             wrap = 2 * s->mb_width + 2;
1155             xy = 2 * mb_x + 1 +  (2 * mb_y + 1) * wrap;
1156             v = 1024;
1157             
1158             s->dc_val[0][xy] = v;
1159             s->dc_val[0][xy + 1] = v;
1160             s->dc_val[0][xy + wrap] = v;
1161             s->dc_val[0][xy + 1 + wrap] = v;
1162             /* ac pred */
1163             memset(s->ac_val[0][xy], 0, 16 * sizeof(INT16));
1164             memset(s->ac_val[0][xy + 1], 0, 16 * sizeof(INT16));
1165             memset(s->ac_val[0][xy + wrap], 0, 16 * sizeof(INT16));
1166             memset(s->ac_val[0][xy + 1 + wrap], 0, 16 * sizeof(INT16));
1167             if (s->h263_msmpeg4) {
1168                 s->coded_block[xy] = 0;
1169                 s->coded_block[xy + 1] = 0;
1170                 s->coded_block[xy + wrap] = 0;
1171                 s->coded_block[xy + 1 + wrap] = 0;
1172             }
1173             /* chroma */
1174             wrap = s->mb_width + 2;
1175             xy = mb_x + 1 + (mb_y + 1) * wrap;
1176             s->dc_val[1][xy] = v;
1177             s->dc_val[2][xy] = v;
1178             /* ac pred */
1179             memset(s->ac_val[1][xy], 0, 16 * sizeof(INT16));
1180             memset(s->ac_val[2][xy], 0, 16 * sizeof(INT16));
1181           }
1182         } else {
1183             s->last_dc[0] = 128 << s->intra_dc_precision;
1184             s->last_dc[1] = 128 << s->intra_dc_precision;
1185             s->last_dc[2] = 128 << s->intra_dc_precision;
1186         }
1187     }
1188     else if (s->h263_pred || s->h263_aic)
1189         s->mbintra_table[mb_x + mb_y*s->mb_width]=1;
1190
1191     /* update motion predictor, not for B-frames as they need the motion_val from the last P/S-Frame */
1192     if (s->out_format == FMT_H263) { //FIXME move into h263.c if possible, format specific stuff shouldnt be here
1193       if(s->pict_type!=B_TYPE){
1194         int xy, wrap, motion_x, motion_y;
1195         
1196         wrap = 2 * s->mb_width + 2;
1197         xy = 2 * mb_x + 1 + (2 * mb_y + 1) * wrap;
1198         if (s->mb_intra) {
1199             motion_x = 0;
1200             motion_y = 0;
1201             goto motion_init;
1202         } else if (s->mv_type == MV_TYPE_16X16) {
1203             motion_x = s->mv[0][0][0];
1204             motion_y = s->mv[0][0][1];
1205         motion_init:
1206             /* no update if 8X8 because it has been done during parsing */
1207             s->motion_val[xy][0] = motion_x;
1208             s->motion_val[xy][1] = motion_y;
1209             s->motion_val[xy + 1][0] = motion_x;
1210             s->motion_val[xy + 1][1] = motion_y;
1211             s->motion_val[xy + wrap][0] = motion_x;
1212             s->motion_val[xy + wrap][1] = motion_y;
1213             s->motion_val[xy + 1 + wrap][0] = motion_x;
1214             s->motion_val[xy + 1 + wrap][1] = motion_y;
1215         }
1216       }
1217     }
1218     
1219     if (!(s->encoding && (s->intra_only || s->pict_type==B_TYPE))) {
1220         UINT8 *dest_y, *dest_cb, *dest_cr;
1221         UINT8 *mbskip_ptr;
1222
1223         /* avoid copy if macroblock skipped in last frame too 
1224            dont touch it for B-frames as they need the skip info from the next p-frame */
1225         if (s->pict_type != B_TYPE) {
1226             mbskip_ptr = &s->mbskip_table[s->mb_y * s->mb_width + s->mb_x];
1227             if (s->mb_skiped) {
1228                 s->mb_skiped = 0;
1229                 /* if previous was skipped too, then nothing to do ! 
1230                    skip only during decoding as we might trash the buffers during encoding a bit */
1231                 if (*mbskip_ptr != 0 && !s->encoding) 
1232                     goto the_end;
1233                 *mbskip_ptr = 1; /* indicate that this time we skiped it */
1234             } else {
1235                 *mbskip_ptr = 0; /* not skipped */
1236             }
1237         }
1238
1239         dest_y = s->current_picture[0] + (mb_y * 16 * s->linesize) + mb_x * 16;
1240         dest_cb = s->current_picture[1] + (mb_y * 8 * (s->linesize >> 1)) + mb_x * 8;
1241         dest_cr = s->current_picture[2] + (mb_y * 8 * (s->linesize >> 1)) + mb_x * 8;
1242
1243         if (s->interlaced_dct) {
1244             dct_linesize = s->linesize * 2;
1245             dct_offset = s->linesize;
1246         } else {
1247             dct_linesize = s->linesize;
1248             dct_offset = s->linesize * 8;
1249         }
1250
1251         if (!s->mb_intra) {
1252             /* motion handling */
1253             if((s->flags&CODEC_FLAG_HQ) || (!s->encoding)){
1254                 if ((!s->no_rounding) || s->pict_type==B_TYPE){                
1255                     op_pix = put_pixels_tab;
1256                     op_qpix= qpel_mc_rnd_tab;
1257                 }else{
1258                     op_pix = put_no_rnd_pixels_tab;
1259                     op_qpix= qpel_mc_no_rnd_tab;
1260                 }
1261
1262                 if (s->mv_dir & MV_DIR_FORWARD) {
1263                     MPV_motion(s, dest_y, dest_cb, dest_cr, 0, s->last_picture, op_pix, op_qpix);
1264                     if ((!s->no_rounding) || s->pict_type==B_TYPE)
1265                         op_pix = avg_pixels_tab;
1266                     else
1267                         op_pix = avg_no_rnd_pixels_tab;
1268                 }
1269                 if (s->mv_dir & MV_DIR_BACKWARD) {
1270                     MPV_motion(s, dest_y, dest_cb, dest_cr, 1, s->next_picture, op_pix, op_qpix);
1271                 }
1272             }
1273
1274             /* add dct residue */
1275             add_dct(s, block[0], 0, dest_y, dct_linesize);
1276             add_dct(s, block[1], 1, dest_y + 8, dct_linesize);
1277             add_dct(s, block[2], 2, dest_y + dct_offset, dct_linesize);
1278             add_dct(s, block[3], 3, dest_y + dct_offset + 8, dct_linesize);
1279
1280             add_dct(s, block[4], 4, dest_cb, s->linesize >> 1);
1281             add_dct(s, block[5], 5, dest_cr, s->linesize >> 1);
1282         } else {
1283             /* dct only in intra block */
1284             put_dct(s, block[0], 0, dest_y, dct_linesize);
1285             put_dct(s, block[1], 1, dest_y + 8, dct_linesize);
1286             put_dct(s, block[2], 2, dest_y + dct_offset, dct_linesize);
1287             put_dct(s, block[3], 3, dest_y + dct_offset + 8, dct_linesize);
1288
1289             put_dct(s, block[4], 4, dest_cb, s->linesize >> 1);
1290             put_dct(s, block[5], 5, dest_cr, s->linesize >> 1);
1291         }
1292     }
1293  the_end:
1294     emms_c(); //FIXME remove
1295 }
1296
1297
1298 static void encode_mb(MpegEncContext *s, int motion_x, int motion_y)
1299 {
1300     const int mb_x= s->mb_x;
1301     const int mb_y= s->mb_y;
1302     int i;
1303 #if 0
1304         if (s->interlaced_dct) {
1305             dct_linesize = s->linesize * 2;
1306             dct_offset = s->linesize;
1307         } else {
1308             dct_linesize = s->linesize;
1309             dct_offset = s->linesize * 8;
1310         }
1311 #endif
1312
1313     if (s->mb_intra) {
1314         UINT8 *ptr;
1315         int wrap;
1316
1317         wrap = s->linesize;
1318         ptr = s->new_picture[0] + (mb_y * 16 * wrap) + mb_x * 16;
1319         get_pixels(s->block[0], ptr               , wrap);
1320         get_pixels(s->block[1], ptr            + 8, wrap);
1321         get_pixels(s->block[2], ptr + 8 * wrap    , wrap);
1322         get_pixels(s->block[3], ptr + 8 * wrap + 8, wrap);
1323
1324         wrap >>=1;
1325         ptr = s->new_picture[1] + (mb_y * 8 * wrap) + mb_x * 8;
1326         get_pixels(s->block[4], ptr, wrap);
1327
1328         ptr = s->new_picture[2] + (mb_y * 8 * wrap) + mb_x * 8;
1329         get_pixels(s->block[5], ptr, wrap);
1330     }else{
1331         op_pixels_func *op_pix;
1332         qpel_mc_func *op_qpix;
1333         UINT8 *dest_y, *dest_cb, *dest_cr;
1334         UINT8 *ptr;
1335         int wrap;
1336
1337         dest_y  = s->current_picture[0] + (mb_y * 16 * s->linesize       ) + mb_x * 16;
1338         dest_cb = s->current_picture[1] + (mb_y * 8  * (s->linesize >> 1)) + mb_x * 8;
1339         dest_cr = s->current_picture[2] + (mb_y * 8  * (s->linesize >> 1)) + mb_x * 8;
1340
1341         if ((!s->no_rounding) || s->pict_type==B_TYPE){
1342             op_pix = put_pixels_tab;
1343             op_qpix= qpel_mc_rnd_tab;
1344         }else{
1345             op_pix = put_no_rnd_pixels_tab;
1346             op_qpix= qpel_mc_no_rnd_tab;
1347         }
1348
1349         if (s->mv_dir & MV_DIR_FORWARD) {
1350             MPV_motion(s, dest_y, dest_cb, dest_cr, 0, s->last_picture, op_pix, op_qpix);
1351            if ((!s->no_rounding) || s->pict_type==B_TYPE)
1352                 op_pix = avg_pixels_tab;
1353             else
1354                 op_pix = avg_no_rnd_pixels_tab;
1355         }
1356         if (s->mv_dir & MV_DIR_BACKWARD) {
1357             MPV_motion(s, dest_y, dest_cb, dest_cr, 1, s->next_picture, op_pix, op_qpix);
1358         }
1359         wrap = s->linesize;
1360         ptr = s->new_picture[0] + (mb_y * 16 * wrap) + mb_x * 16;
1361         diff_pixels(s->block[0], ptr               , dest_y               , wrap);
1362         diff_pixels(s->block[1], ptr            + 8, dest_y            + 8, wrap);
1363         diff_pixels(s->block[2], ptr + 8 * wrap    , dest_y + 8 * wrap    , wrap);
1364         diff_pixels(s->block[3], ptr + 8 * wrap + 8, dest_y + 8 * wrap + 8, wrap);
1365
1366         wrap >>=1;
1367         ptr = s->new_picture[1] + (mb_y * 8 * wrap) + mb_x * 8;
1368         diff_pixels(s->block[4], ptr, dest_cb, wrap);
1369
1370         ptr = s->new_picture[2] + (mb_y * 8 * wrap) + mb_x * 8;
1371         diff_pixels(s->block[5], ptr, dest_cr, wrap);
1372     }
1373             
1374 #if 0
1375             {
1376                 float adap_parm;
1377                 
1378                 adap_parm = ((s->avg_mb_var << 1) + s->mb_var[s->mb_width*mb_y+mb_x] + 1.0) /
1379                             ((s->mb_var[s->mb_width*mb_y+mb_x] << 1) + s->avg_mb_var + 1.0);
1380             
1381                 printf("\ntype=%c qscale=%2d adap=%0.2f dquant=%4.2f var=%4d avgvar=%4d", 
1382                         (s->mb_type[s->mb_width*mb_y+mb_x] > 0) ? 'I' : 'P', 
1383                         s->qscale, adap_parm, s->qscale*adap_parm,
1384                         s->mb_var[s->mb_width*mb_y+mb_x], s->avg_mb_var);
1385             }
1386 #endif
1387     /* DCT & quantize */
1388     if (s->h263_msmpeg4) {
1389         msmpeg4_dc_scale(s);
1390     } else if (s->h263_pred) {
1391         h263_dc_scale(s);
1392     } else {
1393         /* default quantization values */
1394         s->y_dc_scale = 8;
1395         s->c_dc_scale = 8;
1396     }
1397     for(i=0;i<6;i++) {
1398         s->block_last_index[i] = dct_quantize(s, s->block[i], i, s->qscale);
1399     }
1400
1401     /* huffman encode */
1402     switch(s->out_format) {
1403     case FMT_MPEG1:
1404         mpeg1_encode_mb(s, s->block, motion_x, motion_y);
1405         break;
1406     case FMT_H263:
1407         if (s->h263_msmpeg4)
1408             msmpeg4_encode_mb(s, s->block, motion_x, motion_y);
1409         else if(s->h263_pred)
1410             mpeg4_encode_mb(s, s->block, motion_x, motion_y);
1411         else
1412             h263_encode_mb(s, s->block, motion_x, motion_y);
1413         break;
1414     case FMT_MJPEG:
1415         mjpeg_encode_mb(s, s->block);
1416         break;
1417     }
1418 }
1419
1420 static void copy_bits(PutBitContext *pb, UINT8 *src, int length)
1421 {
1422 #if 1
1423     int bytes= length>>4;
1424     int bits= length&15;
1425     int i;
1426
1427     for(i=0; i<bytes; i++) put_bits(pb, 16, be2me_16(((uint16_t*)src)[i]));
1428     put_bits(pb, bits, be2me_16(((uint16_t*)src)[i])>>(16-bits));
1429 #else
1430     int bytes= length>>3;
1431     int bits= length&7;
1432     int i;
1433
1434     for(i=0; i<bytes; i++) put_bits(pb, 8, src[i]);
1435     put_bits(pb, bits, src[i]>>(8-bits));
1436 #endif
1437 }
1438
1439 static void copy_context_before_encode(MpegEncContext *d, MpegEncContext *s, int type){
1440     int i;
1441
1442     memcpy(d->last_mv, s->last_mv, 2*2*2*sizeof(int)); //FIXME is memcpy faster then a loop?
1443
1444     /* mpeg1 */
1445     d->mb_incr= s->mb_incr;
1446     for(i=0; i<3; i++)
1447         d->last_dc[i]= s->last_dc[i];
1448     
1449     /* statistics */
1450     d->mv_bits= s->mv_bits;
1451     d->i_tex_bits= s->i_tex_bits;
1452     d->p_tex_bits= s->p_tex_bits;
1453     d->i_count= s->i_count;
1454     d->p_count= s->p_count;
1455     d->skip_count= s->skip_count;
1456     d->misc_bits= s->misc_bits;
1457     d->last_bits= 0;
1458
1459     d->mb_skiped= s->mb_skiped;
1460 }
1461
1462 static void copy_context_after_encode(MpegEncContext *d, MpegEncContext *s, int type){
1463     int i;
1464
1465     memcpy(d->mv, s->mv, 2*4*2*sizeof(int)); 
1466     memcpy(d->last_mv, s->last_mv, 2*2*2*sizeof(int)); //FIXME is memcpy faster then a loop?
1467     
1468     /* mpeg1 */
1469     d->mb_incr= s->mb_incr;
1470     for(i=0; i<3; i++)
1471         d->last_dc[i]= s->last_dc[i];
1472     
1473     /* statistics */
1474     d->mv_bits= s->mv_bits;
1475     d->i_tex_bits= s->i_tex_bits;
1476     d->p_tex_bits= s->p_tex_bits;
1477     d->i_count= s->i_count;
1478     d->p_count= s->p_count;
1479     d->skip_count= s->skip_count;
1480     d->misc_bits= s->misc_bits;
1481
1482     d->mb_intra= s->mb_intra;
1483     d->mb_skiped= s->mb_skiped;
1484     d->mv_type= s->mv_type;
1485     d->mv_dir= s->mv_dir;
1486     d->pb= s->pb;
1487     d->block= s->block;
1488     for(i=0; i<6; i++)
1489         d->block_last_index[i]= s->block_last_index[i];
1490 }
1491
1492
1493 static void encode_picture(MpegEncContext *s, int picture_number)
1494 {
1495     int mb_x, mb_y, last_gob, pdif = 0;
1496     int i;
1497     int bits;
1498     MpegEncContext best_s, backup_s;
1499     UINT8 bit_buf[7][3000]; //FIXME check that this is ALLWAYS large enogh for a MB
1500
1501     s->picture_number = picture_number;
1502
1503     s->block_wrap[0]=
1504     s->block_wrap[1]=
1505     s->block_wrap[2]=
1506     s->block_wrap[3]= s->mb_width*2 + 2;
1507     s->block_wrap[4]=
1508     s->block_wrap[5]= s->mb_width + 2;
1509     
1510     /* Reset the average MB variance */
1511     s->avg_mb_var = 0;
1512     s->mc_mb_var = 0;
1513
1514     /* we need to initialize some time vars before we can encode b-frames */
1515     if (s->h263_pred && !s->h263_msmpeg4)
1516         ff_set_mpeg4_time(s, s->picture_number); 
1517
1518     /* Estimate motion for every MB */
1519     if(s->pict_type != I_TYPE){
1520 //        int16_t (*tmp)[2]= s->p_mv_table;
1521 //        s->p_mv_table= s->last_mv_table;
1522 //        s->last_mv_table= s->mv_table;
1523     
1524         for(mb_y=0; mb_y < s->mb_height; mb_y++) {
1525             s->block_index[0]= s->block_wrap[0]*(mb_y*2 + 1) - 1;
1526             s->block_index[1]= s->block_wrap[0]*(mb_y*2 + 1);
1527             s->block_index[2]= s->block_wrap[0]*(mb_y*2 + 2) - 1;
1528             s->block_index[3]= s->block_wrap[0]*(mb_y*2 + 2);
1529             for(mb_x=0; mb_x < s->mb_width; mb_x++) {
1530                 s->mb_x = mb_x;
1531                 s->mb_y = mb_y;
1532                 s->block_index[0]+=2;
1533                 s->block_index[1]+=2;
1534                 s->block_index[2]+=2;
1535                 s->block_index[3]+=2;
1536
1537                 /* compute motion vector & mb_type and store in context */
1538                 if(s->pict_type==B_TYPE)
1539                     ff_estimate_b_frame_motion(s, mb_x, mb_y);
1540                 else
1541                     ff_estimate_p_frame_motion(s, mb_x, mb_y);
1542 //                s->mb_type[mb_y*s->mb_width + mb_x]=MB_TYPE_INTER;
1543             }
1544         }
1545         emms_c();
1546     }else if(s->pict_type == I_TYPE){
1547         /* I-Frame */
1548         //FIXME do we need to zero them?
1549         memset(s->motion_val[0], 0, sizeof(INT16)*(s->mb_width*2 + 2)*(s->mb_height*2 + 2)*2);
1550         memset(s->p_mv_table   , 0, sizeof(INT16)*(s->mb_width+2)*(s->mb_height+2)*2);
1551         memset(s->mb_type      , MB_TYPE_INTRA, sizeof(UINT8)*s->mb_width*s->mb_height);
1552     }
1553
1554     if(s->avg_mb_var < s->mc_mb_var && s->pict_type == P_TYPE){ //FIXME subtract MV bits
1555         s->pict_type= I_TYPE;
1556         memset(s->mb_type   , MB_TYPE_INTRA, sizeof(UINT8)*s->mb_width*s->mb_height);
1557         if(s->max_b_frames==0){
1558             s->input_pict_type= I_TYPE;
1559             s->input_picture_in_gop_number=0;
1560         }
1561 //printf("Scene change detected, encoding as I Frame\n");
1562     }
1563     
1564     if(s->pict_type==P_TYPE || s->pict_type==S_TYPE) 
1565         s->f_code= ff_get_best_fcode(s, s->p_mv_table, MB_TYPE_INTER);
1566         ff_fix_long_p_mvs(s);
1567     if(s->pict_type==B_TYPE){
1568         s->f_code= ff_get_best_fcode(s, s->b_forw_mv_table, MB_TYPE_FORWARD);
1569         s->b_code= ff_get_best_fcode(s, s->b_back_mv_table, MB_TYPE_BACKWARD);
1570
1571         ff_fix_long_b_mvs(s, s->b_forw_mv_table, s->f_code, MB_TYPE_FORWARD);
1572         ff_fix_long_b_mvs(s, s->b_back_mv_table, s->b_code, MB_TYPE_BACKWARD);
1573         ff_fix_long_b_mvs(s, s->b_bidir_forw_mv_table, s->f_code, MB_TYPE_BIDIR);
1574         ff_fix_long_b_mvs(s, s->b_bidir_back_mv_table, s->b_code, MB_TYPE_BIDIR);
1575     }
1576     
1577 //printf("f_code %d ///\n", s->f_code);
1578
1579 //    printf("%d %d\n", s->avg_mb_var, s->mc_mb_var);
1580
1581     if(s->flags&CODEC_FLAG_PASS2)
1582         s->qscale = ff_rate_estimate_qscale_pass2(s);
1583     else if (!s->fixed_qscale) 
1584         s->qscale = ff_rate_estimate_qscale(s);
1585
1586
1587     /* precompute matrix */
1588     if (s->out_format == FMT_MJPEG) {
1589         /* for mjpeg, we do include qscale in the matrix */
1590         s->intra_matrix[0] = default_intra_matrix[0];
1591         for(i=1;i<64;i++)
1592             s->intra_matrix[i] = (default_intra_matrix[i] * s->qscale) >> 3;
1593         convert_matrix(s->q_intra_matrix, s->q_intra_matrix16, s->intra_matrix, 8);
1594     } else {
1595         convert_matrix(s->q_intra_matrix, s->q_intra_matrix16, s->intra_matrix, s->qscale);
1596         convert_matrix(s->q_non_intra_matrix, s->q_non_intra_matrix16, s->non_intra_matrix, s->qscale);
1597     }
1598
1599     s->last_bits= get_bit_count(&s->pb);
1600     switch(s->out_format) {
1601     case FMT_MJPEG:
1602         mjpeg_picture_header(s);
1603         break;
1604     case FMT_H263:
1605         if (s->h263_msmpeg4) 
1606             msmpeg4_encode_picture_header(s, picture_number);
1607         else if (s->h263_pred)
1608             mpeg4_encode_picture_header(s, picture_number);
1609         else if (s->h263_rv10) 
1610             rv10_encode_picture_header(s, picture_number);
1611         else
1612             h263_encode_picture_header(s, picture_number);
1613         break;
1614     case FMT_MPEG1:
1615         mpeg1_encode_picture_header(s, picture_number);
1616         break;
1617     }
1618     bits= get_bit_count(&s->pb);
1619     s->header_bits= bits - s->last_bits;
1620     s->last_bits= bits;
1621     s->mv_bits=0;
1622     s->misc_bits=0;
1623     s->i_tex_bits=0;
1624     s->p_tex_bits=0;
1625     s->i_count=0;
1626     s->p_count=0;
1627     s->skip_count=0;
1628
1629     /* init last dc values */
1630     /* note: quant matrix value (8) is implied here */
1631     s->last_dc[0] = 128;
1632     s->last_dc[1] = 128;
1633     s->last_dc[2] = 128;
1634     s->mb_incr = 1;
1635     s->last_mv[0][0][0] = 0;
1636     s->last_mv[0][0][1] = 0;
1637
1638     /* Get the GOB height based on picture height */
1639     if (s->out_format == FMT_H263 && !s->h263_pred && !s->h263_msmpeg4) {
1640         if (s->height <= 400)
1641             s->gob_index = 1;
1642         else if (s->height <= 800)
1643             s->gob_index = 2;
1644         else
1645             s->gob_index = 4;
1646     }
1647         
1648     s->avg_mb_var = s->avg_mb_var / s->mb_num;        
1649     
1650     for(mb_y=0; mb_y < s->mb_height; mb_y++) {
1651         /* Put GOB header based on RTP MTU */
1652         /* TODO: Put all this stuff in a separate generic function */
1653         if (s->rtp_mode) {
1654             if (!mb_y) {
1655                 s->ptr_lastgob = s->pb.buf;
1656                 s->ptr_last_mb_line = s->pb.buf;
1657             } else if (s->out_format == FMT_H263 && !s->h263_pred && !s->h263_msmpeg4 && !(mb_y % s->gob_index)) {
1658                 last_gob = h263_encode_gob_header(s, mb_y);
1659                 if (last_gob) {
1660                     s->first_gob_line = 1;
1661                 }
1662             }
1663         }
1664         
1665         s->block_index[0]= s->block_wrap[0]*(mb_y*2 + 1) - 1;
1666         s->block_index[1]= s->block_wrap[0]*(mb_y*2 + 1);
1667         s->block_index[2]= s->block_wrap[0]*(mb_y*2 + 2) - 1;
1668         s->block_index[3]= s->block_wrap[0]*(mb_y*2 + 2);
1669         s->block_index[4]= s->block_wrap[4]*(mb_y + 1)                    + s->block_wrap[0]*(s->mb_height*2 + 2);
1670         s->block_index[5]= s->block_wrap[4]*(mb_y + 1 + s->mb_height + 2) + s->block_wrap[0]*(s->mb_height*2 + 2);
1671         for(mb_x=0; mb_x < s->mb_width; mb_x++) {
1672             const int mb_type= s->mb_type[mb_y * s->mb_width + mb_x];
1673             const int xy= (mb_y+1) * (s->mb_width+2) + mb_x + 1;
1674             PutBitContext pb;
1675             int d;
1676             int dmin=10000000;
1677             int best=0;
1678
1679             s->mb_x = mb_x;
1680             s->mb_y = mb_y;
1681             s->block_index[0]+=2;
1682             s->block_index[1]+=2;
1683             s->block_index[2]+=2;
1684             s->block_index[3]+=2;
1685             s->block_index[4]++;
1686             s->block_index[5]++;
1687             if(mb_type & (mb_type-1)){ // more than 1 MB type possible
1688                 int next_block=0;
1689                 pb= s->pb;
1690
1691                 copy_context_before_encode(&backup_s, s, -1);
1692
1693                 if(mb_type&MB_TYPE_INTER){
1694                     s->mv_dir = MV_DIR_FORWARD;
1695                     s->mv_type = MV_TYPE_16X16;
1696                     s->mb_intra= 0;
1697                     s->mv[0][0][0] = s->p_mv_table[xy][0];
1698                     s->mv[0][0][1] = s->p_mv_table[xy][1];
1699                     init_put_bits(&s->pb, bit_buf[1], 3000, NULL, NULL);
1700                     s->block= s->blocks[next_block];
1701                     s->last_bits= 0; //done in copy_context_before_encode but we skip that here
1702
1703                     encode_mb(s, s->mv[0][0][0], s->mv[0][0][1]);
1704                     d= get_bit_count(&s->pb);
1705                     if(d<dmin){
1706                         flush_put_bits(&s->pb);
1707                         dmin=d;
1708                         copy_context_after_encode(&best_s, s, MB_TYPE_INTER);
1709                         best=1;
1710                         next_block^=1;
1711                     }
1712                 }
1713                 if(mb_type&MB_TYPE_INTER4V){                 
1714                     copy_context_before_encode(s, &backup_s, MB_TYPE_INTER4V);
1715                     s->mv_dir = MV_DIR_FORWARD;
1716                     s->mv_type = MV_TYPE_8X8;
1717                     s->mb_intra= 0;
1718                     for(i=0; i<4; i++){
1719                         s->mv[0][i][0] = s->motion_val[s->block_index[i]][0];
1720                         s->mv[0][i][1] = s->motion_val[s->block_index[i]][1];
1721                     }
1722                     init_put_bits(&s->pb, bit_buf[2], 3000, NULL, NULL);
1723                     s->block= s->blocks[next_block];
1724
1725                     encode_mb(s, 0, 0);
1726                     d= get_bit_count(&s->pb);
1727                     if(d<dmin){
1728                         flush_put_bits(&s->pb);
1729                         dmin=d;
1730                         copy_context_after_encode(&best_s, s, MB_TYPE_INTER4V);
1731                         best=2;
1732                         next_block^=1;
1733                     }
1734                 }
1735                 if(mb_type&MB_TYPE_FORWARD){
1736                     copy_context_before_encode(s, &backup_s, MB_TYPE_FORWARD);
1737                     s->mv_dir = MV_DIR_FORWARD;
1738                     s->mv_type = MV_TYPE_16X16;
1739                     s->mb_intra= 0;
1740                     s->mv[0][0][0] = s->b_forw_mv_table[xy][0];
1741                     s->mv[0][0][1] = s->b_forw_mv_table[xy][1];
1742                     init_put_bits(&s->pb, bit_buf[3], 3000, NULL, NULL);
1743                     s->block= s->blocks[next_block];
1744
1745                     encode_mb(s, s->mv[0][0][0], s->mv[0][0][1]);
1746                     d= get_bit_count(&s->pb);
1747                     if(d<dmin){
1748                         flush_put_bits(&s->pb);
1749                         dmin=d;
1750                         copy_context_after_encode(&best_s, s, MB_TYPE_FORWARD);
1751                         best=3;
1752                         next_block^=1;
1753                     }
1754                 }
1755                 if(mb_type&MB_TYPE_BACKWARD){
1756                     copy_context_before_encode(s, &backup_s, MB_TYPE_BACKWARD);
1757                     s->mv_dir = MV_DIR_BACKWARD;
1758                     s->mv_type = MV_TYPE_16X16;
1759                     s->mb_intra= 0;
1760                     s->mv[1][0][0] = s->b_back_mv_table[xy][0];
1761                     s->mv[1][0][1] = s->b_back_mv_table[xy][1];
1762                     init_put_bits(&s->pb, bit_buf[4], 3000, NULL, NULL);
1763                     s->block= s->blocks[next_block];
1764
1765                     encode_mb(s, s->mv[1][0][0], s->mv[1][0][1]);
1766                     d= get_bit_count(&s->pb);
1767                     if(d<dmin){
1768                         flush_put_bits(&s->pb);
1769                         dmin=d;
1770                         copy_context_after_encode(&best_s, s, MB_TYPE_BACKWARD);
1771                         best=4;
1772                         next_block^=1;
1773                     }
1774                 }
1775                 if(mb_type&MB_TYPE_BIDIR){
1776                     copy_context_before_encode(s, &backup_s, MB_TYPE_BIDIR);
1777                     s->mv_dir = MV_DIR_FORWARD | MV_DIR_BACKWARD;
1778                     s->mv_type = MV_TYPE_16X16;
1779                     s->mb_intra= 0;
1780                     s->mv[0][0][0] = s->b_bidir_forw_mv_table[xy][0];
1781                     s->mv[0][0][1] = s->b_bidir_forw_mv_table[xy][1];
1782                     s->mv[1][0][0] = s->b_bidir_back_mv_table[xy][0];
1783                     s->mv[1][0][1] = s->b_bidir_back_mv_table[xy][1];
1784                     init_put_bits(&s->pb, bit_buf[5], 3000, NULL, NULL);
1785                     s->block= s->blocks[next_block];
1786
1787                     encode_mb(s, 0, 0);
1788                     d= get_bit_count(&s->pb);
1789                     if(d<dmin){
1790                         flush_put_bits(&s->pb);
1791                         dmin=d;
1792                         copy_context_after_encode(&best_s, s, MB_TYPE_BIDIR);
1793                         best=5;
1794                         next_block^=1;
1795                     }
1796                 }
1797                 if(mb_type&MB_TYPE_DIRECT){
1798                     copy_context_before_encode(s, &backup_s, MB_TYPE_DIRECT);
1799                     s->mv_dir = MV_DIR_FORWARD | MV_DIR_BACKWARD | MV_DIRECT;
1800                     s->mv_type = MV_TYPE_16X16; //FIXME
1801                     s->mb_intra= 0;
1802                     s->mv[0][0][0] = s->b_direct_forw_mv_table[xy][0];
1803                     s->mv[0][0][1] = s->b_direct_forw_mv_table[xy][1];
1804                     s->mv[1][0][0] = s->b_direct_back_mv_table[xy][0];
1805                     s->mv[1][0][1] = s->b_direct_back_mv_table[xy][1];
1806                     init_put_bits(&s->pb, bit_buf[6], 3000, NULL, NULL);
1807                     s->block= s->blocks[next_block];
1808
1809                     encode_mb(s, s->b_direct_mv_table[xy][0], s->b_direct_mv_table[xy][1]);
1810                     d= get_bit_count(&s->pb);
1811                     if(d<dmin){
1812                         flush_put_bits(&s->pb);
1813                         dmin=d;
1814                         copy_context_after_encode(&best_s, s, MB_TYPE_DIRECT);
1815                         best=6;
1816                         next_block^=1;
1817                     }
1818                 }
1819                 if(mb_type&MB_TYPE_INTRA){
1820                     copy_context_before_encode(s, &backup_s, MB_TYPE_INTRA);
1821                     s->mv_dir = MV_DIR_FORWARD;
1822                     s->mv_type = MV_TYPE_16X16;
1823                     s->mb_intra= 1;
1824                     s->mv[0][0][0] = 0;
1825                     s->mv[0][0][1] = 0;
1826                     init_put_bits(&s->pb, bit_buf[0], 3000, NULL, NULL);
1827                     s->block= s->blocks[next_block];
1828                    
1829                     encode_mb(s, 0, 0);
1830                     d= get_bit_count(&s->pb);
1831                     if(d<dmin){
1832                         flush_put_bits(&s->pb);
1833                         dmin=d;
1834                         copy_context_after_encode(&best_s, s, MB_TYPE_INTRA);
1835                         best=0;
1836                         next_block^=1;
1837                     }
1838                     /* force cleaning of ac/dc pred stuff if needed ... */
1839                     if(s->h263_pred || s->h263_aic)
1840                         s->mbintra_table[mb_x + mb_y*s->mb_width]=1;
1841                 }
1842                 copy_context_after_encode(s, &best_s, -1);
1843                 copy_bits(&pb, bit_buf[best], dmin);
1844                 s->pb= pb;
1845                 s->last_bits= get_bit_count(&s->pb);
1846             } else {
1847                 int motion_x, motion_y;
1848                 s->mv_type=MV_TYPE_16X16;
1849                 // only one MB-Type possible
1850                 switch(mb_type){
1851                 case MB_TYPE_INTRA:
1852                     s->mv_dir = MV_DIR_FORWARD;
1853                     s->mb_intra= 1;
1854                     motion_x= s->mv[0][0][0] = 0;
1855                     motion_y= s->mv[0][0][1] = 0;
1856                     break;
1857                 case MB_TYPE_INTER:
1858                     s->mv_dir = MV_DIR_FORWARD;
1859                     s->mb_intra= 0;
1860                     motion_x= s->mv[0][0][0] = s->p_mv_table[xy][0];
1861                     motion_y= s->mv[0][0][1] = s->p_mv_table[xy][1];
1862                     break;
1863                 case MB_TYPE_DIRECT:
1864                     s->mv_dir = MV_DIR_FORWARD | MV_DIR_BACKWARD | MV_DIRECT;
1865                     s->mb_intra= 0;
1866                     motion_x=s->b_direct_mv_table[xy][0];
1867                     motion_y=s->b_direct_mv_table[xy][1];
1868                     s->mv[0][0][0] = s->b_direct_forw_mv_table[xy][0];
1869                     s->mv[0][0][1] = s->b_direct_forw_mv_table[xy][1];
1870                     s->mv[1][0][0] = s->b_direct_back_mv_table[xy][0];
1871                     s->mv[1][0][1] = s->b_direct_back_mv_table[xy][1];
1872                     break;
1873                 case MB_TYPE_BIDIR:
1874                     s->mv_dir = MV_DIR_FORWARD | MV_DIR_BACKWARD;
1875                     s->mb_intra= 0;
1876                     motion_x=0;
1877                     motion_y=0;
1878                     s->mv[0][0][0] = s->b_bidir_forw_mv_table[xy][0];
1879                     s->mv[0][0][1] = s->b_bidir_forw_mv_table[xy][1];
1880                     s->mv[1][0][0] = s->b_bidir_back_mv_table[xy][0];
1881                     s->mv[1][0][1] = s->b_bidir_back_mv_table[xy][1];
1882                     break;
1883                 case MB_TYPE_BACKWARD:
1884                     s->mv_dir = MV_DIR_BACKWARD;
1885                     s->mb_intra= 0;
1886                     motion_x= s->mv[1][0][0] = s->b_back_mv_table[xy][0];
1887                     motion_y= s->mv[1][0][1] = s->b_back_mv_table[xy][1];
1888                     break;
1889                 case MB_TYPE_FORWARD:
1890                     s->mv_dir = MV_DIR_FORWARD;
1891                     s->mb_intra= 0;
1892                     motion_x= s->mv[0][0][0] = s->b_forw_mv_table[xy][0];
1893                     motion_y= s->mv[0][0][1] = s->b_forw_mv_table[xy][1];
1894 //                    printf(" %d %d ", motion_x, motion_y);
1895                     break;
1896                 default:
1897                     motion_x=motion_y=0; //gcc warning fix
1898                     printf("illegal MB type\n");
1899                 }
1900                 encode_mb(s, motion_x, motion_y);
1901             }
1902             /* clean the MV table in IPS frames for direct mode in B frames */
1903             if(s->mb_intra /* && I,P,S_TYPE */){
1904                 s->p_mv_table[xy][0]=0;
1905                 s->p_mv_table[xy][1]=0;
1906             }
1907
1908             MPV_decode_mb(s, s->block);
1909         }
1910
1911
1912         /* Obtain average GOB size for RTP */
1913         if (s->rtp_mode) {
1914             if (!mb_y)
1915                 s->mb_line_avgsize = pbBufPtr(&s->pb) - s->ptr_last_mb_line;
1916             else if (!(mb_y % s->gob_index)) {    
1917                 s->mb_line_avgsize = (s->mb_line_avgsize + pbBufPtr(&s->pb) - s->ptr_last_mb_line) >> 1;
1918                 s->ptr_last_mb_line = pbBufPtr(&s->pb);
1919             }
1920             //fprintf(stderr, "\nMB line: %d\tSize: %u\tAvg. Size: %u", s->mb_y, 
1921             //                    (s->pb.buf_ptr - s->ptr_last_mb_line), s->mb_line_avgsize);
1922             s->first_gob_line = 0;
1923         }
1924     }
1925     emms_c();
1926
1927     if (s->h263_msmpeg4 && s->msmpeg4_version<4 && s->pict_type == I_TYPE)
1928         msmpeg4_encode_ext_header(s);
1929
1930     //if (s->gob_number)
1931     //    fprintf(stderr,"\nNumber of GOB: %d", s->gob_number);
1932     
1933     /* Send the last GOB if RTP */    
1934     if (s->rtp_mode) {
1935         flush_put_bits(&s->pb);
1936         pdif = pbBufPtr(&s->pb) - s->ptr_lastgob;
1937         /* Call the RTP callback to send the last GOB */
1938         if (s->rtp_callback)
1939             s->rtp_callback(s->ptr_lastgob, pdif, s->gob_number);
1940         s->ptr_lastgob = pbBufPtr(&s->pb);
1941         //fprintf(stderr,"\nGOB: %2d size: %d (last)", s->gob_number, pdif);
1942     }
1943 }
1944
1945 static int dct_quantize_c(MpegEncContext *s, 
1946                         DCTELEM *block, int n,
1947                         int qscale)
1948 {
1949     int i, j, level, last_non_zero, q;
1950     const int *qmat;
1951     int minLevel, maxLevel;
1952
1953     if(s->avctx!=NULL && s->avctx->codec->id==CODEC_ID_MPEG4){
1954         /* mpeg4 */
1955         minLevel= -2048;
1956         maxLevel= 2047;
1957     }else if(s->out_format==FMT_MPEG1){
1958         /* mpeg1 */
1959         minLevel= -255;
1960         maxLevel= 255;
1961     }else if(s->out_format==FMT_MJPEG){
1962         /* (m)jpeg */
1963         minLevel= -1023;
1964         maxLevel= 1023;
1965     }else{
1966         /* h263 / msmpeg4 */
1967         minLevel= -128;
1968         maxLevel= 127;
1969     }
1970
1971     av_fdct (block);
1972
1973     /* we need this permutation so that we correct the IDCT
1974        permutation. will be moved into DCT code */
1975     block_permute(block);
1976
1977     if (s->mb_intra) {
1978         if (n < 4)
1979             q = s->y_dc_scale;
1980         else
1981             q = s->c_dc_scale;
1982         q = q << 3;
1983         
1984         /* note: block[0] is assumed to be positive */
1985         block[0] = (block[0] + (q >> 1)) / q;
1986         i = 1;
1987         last_non_zero = 0;
1988         if (s->out_format == FMT_H263) {
1989             qmat = s->q_non_intra_matrix;
1990         } else {
1991             qmat = s->q_intra_matrix;
1992         }
1993     } else {
1994         i = 0;
1995         last_non_zero = -1;
1996         qmat = s->q_non_intra_matrix;
1997     }
1998
1999     for(;i<64;i++) {
2000         j = zigzag_direct[i];
2001         level = block[j];
2002         level = level * qmat[j];
2003 #ifdef PARANOID
2004         {
2005             static int count = 0;
2006             int level1, level2, qmat1;
2007             double val;
2008             if (qmat == s->q_non_intra_matrix) {
2009                 qmat1 = default_non_intra_matrix[j] * s->qscale;
2010             } else {
2011                 qmat1 = default_intra_matrix[j] * s->qscale;
2012             }
2013             if (av_fdct != jpeg_fdct_ifast)
2014                 val = ((double)block[j] * 8.0) / (double)qmat1;
2015             else
2016                 val = ((double)block[j] * 8.0 * 2048.0) / 
2017                     ((double)qmat1 * aanscales[j]);
2018             level1 = (int)val;
2019             level2 = level / (1 << (QMAT_SHIFT - 3));
2020             if (level1 != level2) {
2021                 fprintf(stderr, "%d: quant error qlevel=%d wanted=%d level=%d qmat1=%d qmat=%d wantedf=%0.6f\n", 
2022                         count, level2, level1, block[j], qmat1, qmat[j],
2023                         val);
2024                 count++;
2025             }
2026
2027         }
2028 #endif
2029         /* XXX: slight error for the low range. Test should be equivalent to
2030            (level <= -(1 << (QMAT_SHIFT - 3)) || level >= (1 <<
2031            (QMAT_SHIFT - 3)))
2032         */
2033         if (((level << (31 - (QMAT_SHIFT - 3))) >> (31 - (QMAT_SHIFT - 3))) != 
2034             level) {
2035             level = level / (1 << (QMAT_SHIFT - 3));
2036             /* XXX: currently, this code is not optimal. the range should be:
2037                mpeg1: -255..255
2038                mpeg2: -2048..2047
2039                h263:  -128..127
2040                mpeg4: -2048..2047
2041             */
2042             if (level > maxLevel)
2043                 level = maxLevel;
2044             else if (level < minLevel)
2045                 level = minLevel;
2046
2047             block[j] = level;
2048             last_non_zero = i;
2049         } else {
2050             block[j] = 0;
2051         }
2052     }
2053     return last_non_zero;
2054 }
2055
2056 static void dct_unquantize_mpeg1_c(MpegEncContext *s, 
2057                                    DCTELEM *block, int n, int qscale)
2058 {
2059     int i, level, nCoeffs;
2060     const UINT16 *quant_matrix;
2061
2062     if(s->alternate_scan) nCoeffs= 64;
2063     else nCoeffs= s->block_last_index[n]+1;
2064     
2065     if (s->mb_intra) {
2066         if (n < 4) 
2067             block[0] = block[0] * s->y_dc_scale;
2068         else
2069             block[0] = block[0] * s->c_dc_scale;
2070         /* XXX: only mpeg1 */
2071         quant_matrix = s->intra_matrix;
2072         for(i=1;i<nCoeffs;i++) {
2073             int j= zigzag_direct[i];
2074             level = block[j];
2075             if (level) {
2076                 if (level < 0) {
2077                     level = -level;
2078                     level = (int)(level * qscale * quant_matrix[j]) >> 3;
2079                     level = (level - 1) | 1;
2080                     level = -level;
2081                 } else {
2082                     level = (int)(level * qscale * quant_matrix[j]) >> 3;
2083                     level = (level - 1) | 1;
2084                 }
2085 #ifdef PARANOID
2086                 if (level < -2048 || level > 2047)
2087                     fprintf(stderr, "unquant error %d %d\n", i, level);
2088 #endif
2089                 block[j] = level;
2090             }
2091         }
2092     } else {
2093         i = 0;
2094         quant_matrix = s->non_intra_matrix;
2095         for(;i<nCoeffs;i++) {
2096             int j= zigzag_direct[i];
2097             level = block[j];
2098             if (level) {
2099                 if (level < 0) {
2100                     level = -level;
2101                     level = (((level << 1) + 1) * qscale *
2102                              ((int) (quant_matrix[j]))) >> 4;
2103                     level = (level - 1) | 1;
2104                     level = -level;
2105                 } else {
2106                     level = (((level << 1) + 1) * qscale *
2107                              ((int) (quant_matrix[j]))) >> 4;
2108                     level = (level - 1) | 1;
2109                 }
2110 #ifdef PARANOID
2111                 if (level < -2048 || level > 2047)
2112                     fprintf(stderr, "unquant error %d %d\n", i, level);
2113 #endif
2114                 block[j] = level;
2115             }
2116         }
2117     }
2118 }
2119
2120 static void dct_unquantize_mpeg2_c(MpegEncContext *s, 
2121                                    DCTELEM *block, int n, int qscale)
2122 {
2123     int i, level, nCoeffs;
2124     const UINT16 *quant_matrix;
2125
2126     if(s->alternate_scan) nCoeffs= 64;
2127     else nCoeffs= s->block_last_index[n]+1;
2128     
2129     if (s->mb_intra) {
2130         if (n < 4) 
2131             block[0] = block[0] * s->y_dc_scale;
2132         else
2133             block[0] = block[0] * s->c_dc_scale;
2134         quant_matrix = s->intra_matrix;
2135         for(i=1;i<nCoeffs;i++) {
2136             int j= zigzag_direct[i];
2137             level = block[j];
2138             if (level) {
2139                 if (level < 0) {
2140                     level = -level;
2141                     level = (int)(level * qscale * quant_matrix[j]) >> 3;
2142                     level = -level;
2143                 } else {
2144                     level = (int)(level * qscale * quant_matrix[j]) >> 3;
2145                 }
2146 #ifdef PARANOID
2147                 if (level < -2048 || level > 2047)
2148                     fprintf(stderr, "unquant error %d %d\n", i, level);
2149 #endif
2150                 block[j] = level;
2151             }
2152         }
2153     } else {
2154         int sum=-1;
2155         i = 0;
2156         quant_matrix = s->non_intra_matrix;
2157         for(;i<nCoeffs;i++) {
2158             int j= zigzag_direct[i];
2159             level = block[j];
2160             if (level) {
2161                 if (level < 0) {
2162                     level = -level;
2163                     level = (((level << 1) + 1) * qscale *
2164                              ((int) (quant_matrix[j]))) >> 4;
2165                     level = -level;
2166                 } else {
2167                     level = (((level << 1) + 1) * qscale *
2168                              ((int) (quant_matrix[j]))) >> 4;
2169                 }
2170 #ifdef PARANOID
2171                 if (level < -2048 || level > 2047)
2172                     fprintf(stderr, "unquant error %d %d\n", i, level);
2173 #endif
2174                 block[j] = level;
2175                 sum+=level;
2176             }
2177         }
2178         block[63]^=sum&1;
2179     }
2180 }
2181
2182
2183 static void dct_unquantize_h263_c(MpegEncContext *s, 
2184                                   DCTELEM *block, int n, int qscale)
2185 {
2186     int i, level, qmul, qadd;
2187     int nCoeffs;
2188     
2189     if (s->mb_intra) {
2190         if (!s->h263_aic) {
2191             if (n < 4) 
2192                 block[0] = block[0] * s->y_dc_scale;
2193             else
2194                 block[0] = block[0] * s->c_dc_scale;
2195         }
2196         i = 1;
2197         nCoeffs= 64; //does not allways use zigzag table 
2198     } else {
2199         i = 0;
2200         nCoeffs= zigzag_end[ s->block_last_index[n] ];
2201     }
2202
2203     qmul = s->qscale << 1;
2204     if (s->h263_aic && s->mb_intra)
2205         qadd = 0;
2206     else
2207         qadd = (s->qscale - 1) | 1;
2208
2209     for(;i<nCoeffs;i++) {
2210         level = block[i];
2211         if (level) {
2212             if (level < 0) {
2213                 level = level * qmul - qadd;
2214             } else {
2215                 level = level * qmul + qadd;
2216             }
2217 #ifdef PARANOID
2218                 if (level < -2048 || level > 2047)
2219                     fprintf(stderr, "unquant error %d %d\n", i, level);
2220 #endif
2221             block[i] = level;
2222         }
2223     }
2224 }
2225
2226 AVCodec mpeg1video_encoder = {
2227     "mpeg1video",
2228     CODEC_TYPE_VIDEO,
2229     CODEC_ID_MPEG1VIDEO,
2230     sizeof(MpegEncContext),
2231     MPV_encode_init,
2232     MPV_encode_picture,
2233     MPV_encode_end,
2234 };
2235
2236 AVCodec h263_encoder = {
2237     "h263",
2238     CODEC_TYPE_VIDEO,
2239     CODEC_ID_H263,
2240     sizeof(MpegEncContext),
2241     MPV_encode_init,
2242     MPV_encode_picture,
2243     MPV_encode_end,
2244 };
2245
2246 AVCodec h263p_encoder = {
2247     "h263p",
2248     CODEC_TYPE_VIDEO,
2249     CODEC_ID_H263P,
2250     sizeof(MpegEncContext),
2251     MPV_encode_init,
2252     MPV_encode_picture,
2253     MPV_encode_end,
2254 };
2255
2256 AVCodec rv10_encoder = {
2257     "rv10",
2258     CODEC_TYPE_VIDEO,
2259     CODEC_ID_RV10,
2260     sizeof(MpegEncContext),
2261     MPV_encode_init,
2262     MPV_encode_picture,
2263     MPV_encode_end,
2264 };
2265
2266 AVCodec mjpeg_encoder = {
2267     "mjpeg",
2268     CODEC_TYPE_VIDEO,
2269     CODEC_ID_MJPEG,
2270     sizeof(MpegEncContext),
2271     MPV_encode_init,
2272     MPV_encode_picture,
2273     MPV_encode_end,
2274 };
2275
2276 AVCodec mpeg4_encoder = {
2277     "mpeg4",
2278     CODEC_TYPE_VIDEO,
2279     CODEC_ID_MPEG4,
2280     sizeof(MpegEncContext),
2281     MPV_encode_init,
2282     MPV_encode_picture,
2283     MPV_encode_end,
2284 };
2285
2286 AVCodec msmpeg4v1_encoder = {
2287     "msmpeg4v1",
2288     CODEC_TYPE_VIDEO,
2289     CODEC_ID_MSMPEG4V1,
2290     sizeof(MpegEncContext),
2291     MPV_encode_init,
2292     MPV_encode_picture,
2293     MPV_encode_end,
2294 };
2295
2296 AVCodec msmpeg4v2_encoder = {
2297     "msmpeg4v2",
2298     CODEC_TYPE_VIDEO,
2299     CODEC_ID_MSMPEG4V2,
2300     sizeof(MpegEncContext),
2301     MPV_encode_init,
2302     MPV_encode_picture,
2303     MPV_encode_end,
2304 };
2305
2306 AVCodec msmpeg4v3_encoder = {
2307     "msmpeg4",
2308     CODEC_TYPE_VIDEO,
2309     CODEC_ID_MSMPEG4V3,
2310     sizeof(MpegEncContext),
2311     MPV_encode_init,
2312     MPV_encode_picture,
2313     MPV_encode_end,
2314 };