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