]> git.sesse.net Git - ffmpeg/blob - libavcodec/mpegvideo.c
49a99816782d9a210efac52bd0a63db8438cd24f
[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 #include <stdlib.h>
20 #include <stdio.h>
21 #include <math.h>
22 #include <string.h>
23 #include "avcodec.h"
24 #include "dsputil.h"
25 #include "mpegvideo.h"
26
27 /* Stuff below is useful only for mplayer project */
28 #ifdef HAVE_CONFIG_H
29 #include "../config.h"
30 #endif
31
32 #ifdef USE_FASTMEMCPY
33 #include "fastmemcpy.h"
34 #endif
35
36 static void encode_picture(MpegEncContext *s, int picture_number);
37 static void rate_control_init(MpegEncContext *s);
38 static int rate_estimate_qscale(MpegEncContext *s);
39 static void dct_unquantize_mpeg1_c(MpegEncContext *s, 
40                                    DCTELEM *block, int n, int qscale);
41 static void dct_unquantize_h263_c(MpegEncContext *s, 
42                                   DCTELEM *block, int n, int qscale);
43 static int dct_quantize(MpegEncContext *s, DCTELEM *block, int n, int qscale);
44 static int dct_quantize_mmx(MpegEncContext *s, 
45                             DCTELEM *block, int n,
46                             int qscale);
47 #define EDGE_WIDTH 16
48
49 /* enable all paranoid tests for rounding, overflows, etc... */
50 //#define PARANOID
51
52 //#define DEBUG
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 /* default motion estimation */
74 int motion_estimation_method = ME_LOG;
75
76 /* XXX: should use variable shift ? */
77 #define QMAT_SHIFT_MMX 19
78 #define QMAT_SHIFT 25
79
80 static void convert_matrix(int *qmat, const UINT16 *quant_matrix, int qscale)
81 {
82     int i;
83
84     if (av_fdct == jpeg_fdct_ifast) {
85         for(i=0;i<64;i++) {
86             /* 16 <= qscale * quant_matrix[i] <= 7905 */
87             /* 19952 <= aanscales[i] * qscale * quant_matrix[i] <= 249205026 */
88             
89             qmat[i] = (int)((1ULL << (QMAT_SHIFT + 11)) / (aanscales[i] * qscale * quant_matrix[i]));
90         }
91     } else {
92         for(i=0;i<64;i++) {
93             /* We can safely suppose that 16 <= quant_matrix[i] <= 255
94                So 16 <= qscale * quant_matrix[i] <= 7905
95                so (1 << QMAT_SHIFT) / 16 >= qmat[i] >= (1 << QMAT_SHIFT) / 7905
96             */
97             qmat[i] = (1 << QMAT_SHIFT_MMX) / (qscale * quant_matrix[i]);
98         }
99     }
100 }
101
102 /* init common structure for both encoder and decoder */
103 int MPV_common_init(MpegEncContext *s)
104 {
105     int c_size, i;
106     UINT8 *pict;
107
108     if (s->out_format == FMT_H263) 
109         s->dct_unquantize = dct_unquantize_h263_c;
110     else
111         s->dct_unquantize = dct_unquantize_mpeg1_c;
112         
113 #ifdef HAVE_MMX
114     MPV_common_init_mmx(s);
115 #endif
116     s->mb_width = (s->width + 15) / 16;
117     s->mb_height = (s->height + 15) / 16;
118     s->linesize = s->mb_width * 16 + 2 * EDGE_WIDTH;
119
120     for(i=0;i<3;i++) {
121         int w, h, shift, pict_start;
122
123         w = s->linesize;
124         h = s->mb_height * 16 + 2 * EDGE_WIDTH;
125         shift = (i == 0) ? 0 : 1;
126         c_size = (w >> shift) * (h >> shift);
127         pict_start = (w >> shift) * (EDGE_WIDTH >> shift) + (EDGE_WIDTH >> shift);
128
129         pict = av_mallocz(c_size);
130         if (pict == NULL)
131             goto fail;
132         s->last_picture_base[i] = pict;
133         s->last_picture[i] = pict + pict_start;
134     
135         pict = av_mallocz(c_size);
136         if (pict == NULL)
137             goto fail;
138         s->next_picture_base[i] = pict;
139         s->next_picture[i] = pict + pict_start;
140
141         if (s->has_b_frames) {
142             pict = av_mallocz(c_size);
143             if (pict == NULL) 
144                 goto fail;
145             s->aux_picture_base[i] = pict;
146             s->aux_picture[i] = pict + pict_start;
147         }
148     }
149
150     if (s->out_format == FMT_H263) {
151         int size;
152         /* MV prediction */
153         size = (2 * s->mb_width + 2) * (2 * s->mb_height + 2);
154         s->motion_val = malloc(size * 2 * sizeof(INT16));
155         if (s->motion_val == NULL)
156             goto fail;
157         memset(s->motion_val, 0, size * 2 * sizeof(INT16));
158     }
159
160     if (s->h263_pred) {
161         int y_size, c_size, i, size;
162         
163         /* dc values */
164
165         y_size = (2 * s->mb_width + 2) * (2 * s->mb_height + 2);
166         c_size = (s->mb_width + 2) * (s->mb_height + 2);
167         size = y_size + 2 * c_size;
168         s->dc_val[0] = malloc(size * sizeof(INT16));
169         if (s->dc_val[0] == NULL)
170             goto fail;
171         s->dc_val[1] = s->dc_val[0] + y_size;
172         s->dc_val[2] = s->dc_val[1] + c_size;
173         for(i=0;i<size;i++)
174             s->dc_val[0][i] = 1024;
175
176         /* ac values */
177         s->ac_val[0] = av_mallocz(size * sizeof(INT16) * 16);
178         if (s->ac_val[0] == NULL)
179             goto fail;
180         s->ac_val[1] = s->ac_val[0] + y_size;
181         s->ac_val[2] = s->ac_val[1] + c_size;
182         
183         /* cbp values */
184         s->coded_block = av_mallocz(y_size);
185         if (!s->coded_block)
186             goto fail;
187     }
188     /* default structure is frame */
189     s->picture_structure = PICT_FRAME;
190
191     /* init default q matrix (only for mpeg and mjpeg) */
192     for(i=0;i<64;i++) {
193         s->intra_matrix[i] = default_intra_matrix[i];
194         s->chroma_intra_matrix[i] = default_intra_matrix[i];
195         s->non_intra_matrix[i] = default_non_intra_matrix[i];
196         s->chroma_non_intra_matrix[i] = default_non_intra_matrix[i];
197     }
198     /* init macroblock skip table */
199     if (!s->encoding) {
200         s->mbskip_table = av_mallocz(s->mb_width * s->mb_height);
201         if (!s->mbskip_table)
202             goto fail;
203     }
204
205     s->context_initialized = 1;
206     return 0;
207  fail:
208     if (s->motion_val)
209         free(s->motion_val);
210     if (s->dc_val[0])
211         free(s->dc_val[0]);
212     if (s->ac_val[0])
213         free(s->ac_val[0]);
214     if (s->coded_block)
215         free(s->coded_block);
216     if (s->mbskip_table)
217         free(s->mbskip_table);
218     for(i=0;i<3;i++) {
219         if (s->last_picture_base[i])
220             free(s->last_picture_base[i]);
221         if (s->next_picture_base[i])
222             free(s->next_picture_base[i]);
223         if (s->aux_picture_base[i])
224             free(s->aux_picture_base[i]);
225     }
226     return -1;
227 }
228
229 /* init common structure for both encoder and decoder */
230 void MPV_common_end(MpegEncContext *s)
231 {
232     int i;
233
234     if (s->motion_val)
235         free(s->motion_val);
236     if (s->h263_pred) {
237         free(s->dc_val[0]);
238         free(s->ac_val[0]);
239         free(s->coded_block);
240     }
241     if (s->mbskip_table)
242         free(s->mbskip_table);
243     for(i=0;i<3;i++) {
244         free(s->last_picture_base[i]);
245         free(s->next_picture_base[i]);
246         if (s->has_b_frames)
247             free(s->aux_picture_base[i]);
248     }
249     s->context_initialized = 0;
250 }
251
252 /* init video encoder */
253 int MPV_encode_init(AVCodecContext *avctx)
254 {
255     MpegEncContext *s = avctx->priv_data;
256
257     s->bit_rate = avctx->bit_rate;
258     s->frame_rate = avctx->frame_rate;
259     s->width = avctx->width;
260     s->height = avctx->height;
261     s->gop_size = avctx->gop_size;
262     if (s->gop_size <= 1) {
263         s->intra_only = 1;
264         s->gop_size = 12;
265     } else {
266         s->intra_only = 0;
267     }
268     s->full_search = motion_estimation_method;
269
270     s->fixed_qscale = (avctx->flags & CODEC_FLAG_QSCALE);
271
272     switch(avctx->codec->id) {
273     case CODEC_ID_MPEG1VIDEO:
274         s->out_format = FMT_MPEG1;
275         break;
276     case CODEC_ID_MJPEG:
277         s->out_format = FMT_MJPEG;
278         s->intra_only = 1; /* force intra only for jpeg */
279         if (mjpeg_init(s) < 0)
280             return -1;
281         break;
282     case CODEC_ID_H263:
283         if (h263_get_picture_format(s->width, s->height) == 7)
284             return -1;
285         s->out_format = FMT_H263;
286         break;
287     case CODEC_ID_H263P:
288         s->out_format = FMT_H263;
289         s->h263_plus = 1;
290         /* XXX: not unrectricted mv yet */
291         break;
292     case CODEC_ID_RV10:
293         s->out_format = FMT_H263;
294         s->h263_rv10 = 1;
295         break;
296     case CODEC_ID_OPENDIVX:
297         s->out_format = FMT_H263;
298         s->h263_pred = 1;
299         s->unrestricted_mv = 1;
300         break;
301     case CODEC_ID_MSMPEG4:
302         s->out_format = FMT_H263;
303         s->h263_msmpeg4 = 1;
304         s->h263_pred = 1;
305         s->unrestricted_mv = 1;
306         break;
307     default:
308         return -1;
309     }
310
311     if (s->out_format == FMT_H263)
312         h263_encode_init_vlc(s);
313
314     s->encoding = 1;
315
316     /* init */
317     if (MPV_common_init(s) < 0)
318         return -1;
319     
320     /* rate control init */
321     rate_control_init(s);
322
323     s->picture_number = 0;
324     s->fake_picture_number = 0;
325     /* motion detector init */
326     s->f_code = 1;
327
328     return 0;
329 }
330
331 int MPV_encode_end(AVCodecContext *avctx)
332 {
333     MpegEncContext *s = avctx->priv_data;
334
335 #ifdef STATS
336     print_stats();
337 #endif
338     MPV_common_end(s);
339     if (s->out_format == FMT_MJPEG)
340         mjpeg_close(s);
341     return 0;
342 }
343
344 /* draw the edges of width 'w' of an image of size width, height */
345 static void draw_edges(UINT8 *buf, int wrap, int width, int height, int w)
346 {
347     UINT8 *ptr, *last_line;
348     int i;
349
350     last_line = buf + (height - 1) * wrap;
351     for(i=0;i<w;i++) {
352         /* top and bottom */
353         memcpy(buf - (i + 1) * wrap, buf, width);
354         memcpy(last_line + (i + 1) * wrap, last_line, width);
355     }
356     /* left and right */
357     ptr = buf;
358     for(i=0;i<height;i++) {
359         memset(ptr - w, ptr[0], w);
360         memset(ptr + width, ptr[width-1], w);
361         ptr += wrap;
362     }
363     /* corners */
364     for(i=0;i<w;i++) {
365         memset(buf - (i + 1) * wrap - w, buf[0], w); /* top left */
366         memset(buf - (i + 1) * wrap + width, buf[width-1], w); /* top right */
367         memset(last_line + (i + 1) * wrap - w, last_line[0], w); /* top left */
368         memset(last_line + (i + 1) * wrap + width, last_line[width-1], w); /* top right */
369     }
370 }
371
372 /* generic function for encode/decode called before a frame is coded/decoded */
373 void MPV_frame_start(MpegEncContext *s)
374 {
375     int i;
376     UINT8 *tmp;
377
378     if (s->pict_type == B_TYPE) {
379         for(i=0;i<3;i++) {
380             s->current_picture[i] = s->aux_picture[i];
381         }
382     } else {
383         for(i=0;i<3;i++) {
384             /* swap next and last */
385             tmp = s->last_picture[i];
386             s->last_picture[i] = s->next_picture[i];
387             s->next_picture[i] = tmp;
388             s->current_picture[i] = tmp;
389         }
390     }
391 }
392
393 /* generic function for encode/decode called after a frame has been coded/decoded */
394 void MPV_frame_end(MpegEncContext *s)
395 {
396     /* draw edge for correct motion prediction if outside */
397     if (s->pict_type != B_TYPE) {
398         draw_edges(s->current_picture[0], s->linesize, s->width, s->height, EDGE_WIDTH);
399         draw_edges(s->current_picture[1], s->linesize/2, s->width/2, s->height/2, EDGE_WIDTH/2);
400         draw_edges(s->current_picture[2], s->linesize/2, s->width/2, s->height/2, EDGE_WIDTH/2);
401     }
402 }
403
404 int MPV_encode_picture(AVCodecContext *avctx,
405                        unsigned char *buf, int buf_size, void *data)
406 {
407     MpegEncContext *s = avctx->priv_data;
408     AVPicture *pict = data;
409     int i, j;
410
411     if (s->fixed_qscale) 
412         s->qscale = avctx->quality;
413
414     init_put_bits(&s->pb, buf, buf_size, NULL, NULL);
415
416     if (!s->intra_only) {
417         /* first picture of GOP is intra */
418         if ((s->picture_number % s->gop_size) == 0)
419             s->pict_type = I_TYPE;
420         else
421             s->pict_type = P_TYPE;
422     } else {
423         s->pict_type = I_TYPE;
424     }
425     avctx->key_frame = (s->pict_type == I_TYPE);
426     
427     MPV_frame_start(s);
428
429     for(i=0;i<3;i++) {
430         UINT8 *src = pict->data[i];
431         UINT8 *dest = s->current_picture[i];
432         int src_wrap = pict->linesize[i];
433         int dest_wrap = s->linesize;
434         int w = s->width;
435         int h = s->height;
436
437         if (i >= 1) {
438             dest_wrap >>= 1;
439             w >>= 1;
440             h >>= 1;
441         }
442
443         for(j=0;j<h;j++) {
444             memcpy(dest, src, w);
445             dest += dest_wrap;
446             src += src_wrap;
447         }
448         s->new_picture[i] = s->current_picture[i];
449     }
450
451     encode_picture(s, s->picture_number);
452     
453     MPV_frame_end(s);
454     s->picture_number++;
455
456     if (s->out_format == FMT_MJPEG)
457         mjpeg_picture_trailer(s);
458
459     flush_put_bits(&s->pb);
460     s->total_bits += (s->pb.buf_ptr - s->pb.buf) * 8;
461     avctx->quality = s->qscale;
462     return s->pb.buf_ptr - s->pb.buf;
463 }
464
465 static inline int clip(int a, int amin, int amax)
466 {
467     if (a < amin)
468         return amin;
469     else if (a > amax)
470         return amax;
471     else
472         return a;
473 }
474
475 /* apply one mpeg motion vector to the three components */
476 static inline void mpeg_motion(MpegEncContext *s,
477                                UINT8 *dest_y, UINT8 *dest_cb, UINT8 *dest_cr,
478                                int dest_offset,
479                                UINT8 **ref_picture, int src_offset,
480                                int field_based, op_pixels_func *pix_op,
481                                int motion_x, int motion_y, int h)
482 {
483     UINT8 *ptr;
484     int dxy, offset, mx, my, src_x, src_y, height, linesize;
485     
486     dxy = ((motion_y & 1) << 1) | (motion_x & 1);
487     src_x = s->mb_x * 16 + (motion_x >> 1);
488     src_y = s->mb_y * (16 >> field_based) + (motion_y >> 1);
489                 
490     /* WARNING: do no forget half pels */
491     height = s->height >> field_based;
492     src_x = clip(src_x, -16, s->width);
493     if (src_x == s->width)
494         dxy &= ~1;
495     src_y = clip(src_y, -16, height);
496     if (src_y == height)
497         dxy &= ~2;
498     linesize = s->linesize << field_based;
499     ptr = ref_picture[0] + (src_y * linesize) + (src_x) + src_offset;
500     dest_y += dest_offset;
501     pix_op[dxy](dest_y, ptr, linesize, h);
502     pix_op[dxy](dest_y + 8, ptr + 8, linesize, h);
503
504     if (s->out_format == FMT_H263) {
505         dxy = 0;
506         if ((motion_x & 3) != 0)
507             dxy |= 1;
508         if ((motion_y & 3) != 0)
509             dxy |= 2;
510         mx = motion_x >> 2;
511         my = motion_y >> 2;
512     } else {
513         mx = motion_x / 2;
514         my = motion_y / 2;
515         dxy = ((my & 1) << 1) | (mx & 1);
516         mx >>= 1;
517         my >>= 1;
518     }
519     
520     src_x = s->mb_x * 8 + mx;
521     src_y = s->mb_y * (8 >> field_based) + my;
522     src_x = clip(src_x, -8, s->width >> 1);
523     if (src_x == (s->width >> 1))
524         dxy &= ~1;
525     src_y = clip(src_y, -8, height >> 1);
526     if (src_y == (height >> 1))
527         dxy &= ~2;
528
529     offset = (src_y * (linesize >> 1)) + src_x + (src_offset >> 1);
530     ptr = ref_picture[1] + offset;
531     pix_op[dxy](dest_cb + (dest_offset >> 1), ptr, linesize >> 1, h >> 1);
532     ptr = ref_picture[2] + offset;
533     pix_op[dxy](dest_cr + (dest_offset >> 1), ptr, linesize >> 1, h >> 1);
534 }
535
536 static inline void MPV_motion(MpegEncContext *s, 
537                               UINT8 *dest_y, UINT8 *dest_cb, UINT8 *dest_cr,
538                               int dir, UINT8 **ref_picture, 
539                               op_pixels_func *pix_op)
540 {
541     int dxy, offset, mx, my, src_x, src_y, motion_x, motion_y;
542     int mb_x, mb_y, i;
543     UINT8 *ptr, *dest;
544
545     mb_x = s->mb_x;
546     mb_y = s->mb_y;
547
548     switch(s->mv_type) {
549     case MV_TYPE_16X16:
550         mpeg_motion(s, dest_y, dest_cb, dest_cr, 0,
551                     ref_picture, 0,
552                     0, pix_op,
553                     s->mv[dir][0][0], s->mv[dir][0][1], 16);
554         break;
555     case MV_TYPE_8X8:
556         for(i=0;i<4;i++) {
557             motion_x = s->mv[dir][i][0];
558             motion_y = s->mv[dir][i][1];
559
560             dxy = ((motion_y & 1) << 1) | (motion_x & 1);
561             src_x = mb_x * 16 + (motion_x >> 1) + (i & 1) * 8;
562             src_y = mb_y * 16 + (motion_y >> 1) + ((i >> 1) & 1) * 8;
563                     
564             /* WARNING: do no forget half pels */
565             src_x = clip(src_x, -16, s->width);
566             if (src_x == s->width)
567                 dxy &= ~1;
568             src_y = clip(src_y, -16, s->height);
569             if (src_y == s->height)
570                 dxy &= ~2;
571                     
572             ptr = ref_picture[0] + (src_y * s->linesize) + (src_x);
573             dest = dest_y + ((i & 1) * 8) + (i >> 1) * 8 * s->linesize;
574             pix_op[dxy](dest, ptr, s->linesize, 8);
575         }
576         /* In case of 8X8, we construct a single chroma motion vector
577            with a special rounding */
578         mx = 0;
579         my = 0;
580         for(i=0;i<4;i++) {
581             mx += s->mv[dir][i][0];
582             my += s->mv[dir][i][1];
583         }
584         if (mx >= 0)
585             mx = (h263_chroma_roundtab[mx & 0xf] + ((mx >> 3) & ~1));
586         else {
587             mx = -mx;
588             mx = -(h263_chroma_roundtab[mx & 0xf] + ((mx >> 3) & ~1));
589         }
590         if (my >= 0)
591             my = (h263_chroma_roundtab[my & 0xf] + ((my >> 3) & ~1));
592         else {
593             my = -my;
594             my = -(h263_chroma_roundtab[my & 0xf] + ((my >> 3) & ~1));
595         }
596         dxy = ((my & 1) << 1) | (mx & 1);
597         mx >>= 1;
598         my >>= 1;
599
600         src_x = mb_x * 8 + mx;
601         src_y = mb_y * 8 + my;
602         src_x = clip(src_x, -8, s->width/2);
603         if (src_x == s->width/2)
604             dxy &= ~1;
605         src_y = clip(src_y, -8, s->height/2);
606         if (src_y == s->height/2)
607             dxy &= ~2;
608         
609         offset = (src_y * (s->linesize >> 1)) + src_x;
610         ptr = ref_picture[1] + offset;
611         pix_op[dxy](dest_cb, ptr, s->linesize >> 1, 8);
612         ptr = ref_picture[2] + offset;
613         pix_op[dxy](dest_cr, ptr, s->linesize >> 1, 8);
614         break;
615     case MV_TYPE_FIELD:
616         if (s->picture_structure == PICT_FRAME) {
617             /* top field */
618             mpeg_motion(s, dest_y, dest_cb, dest_cr, 0,
619                         ref_picture, s->field_select[dir][0] ? s->linesize : 0,
620                         1, pix_op,
621                         s->mv[dir][0][0], s->mv[dir][0][1], 8);
622             /* bottom field */
623             mpeg_motion(s, dest_y, dest_cb, dest_cr, s->linesize,
624                         ref_picture, s->field_select[dir][1] ? s->linesize : 0,
625                         1, pix_op,
626                         s->mv[dir][1][0], s->mv[dir][1][1], 8);
627         } else {
628             
629
630         }
631         break;
632     }
633 }
634
635
636 /* put block[] to dest[] */
637 static inline void put_dct(MpegEncContext *s, 
638                            DCTELEM *block, int i, UINT8 *dest, int line_size)
639 {
640     if (!s->mpeg2)
641         s->dct_unquantize(s, block, i, s->qscale);
642     j_rev_dct (block);
643     put_pixels_clamped(block, dest, line_size);
644 }
645
646 /* add block[] to dest[] */
647 static inline void add_dct(MpegEncContext *s, 
648                            DCTELEM *block, int i, UINT8 *dest, int line_size)
649 {
650     if (s->block_last_index[i] >= 0) {
651         if (!s->mpeg2)
652             s->dct_unquantize(s, block, i, s->qscale);
653         j_rev_dct (block);
654         add_pixels_clamped(block, dest, line_size);
655     }
656 }
657
658 /* generic function called after a macroblock has been parsed by the
659    decoder or after it has been encoded by the encoder.
660
661    Important variables used:
662    s->mb_intra : true if intra macroblock
663    s->mv_dir   : motion vector direction
664    s->mv_type  : motion vector type
665    s->mv       : motion vector
666    s->interlaced_dct : true if interlaced dct used (mpeg2)
667  */
668 void MPV_decode_mb(MpegEncContext *s, DCTELEM block[6][64])
669 {
670     int mb_x, mb_y, motion_x, motion_y;
671     int dct_linesize, dct_offset;
672     op_pixels_func *op_pix;
673
674     mb_x = s->mb_x;
675     mb_y = s->mb_y;
676
677     /* update DC predictors for P macroblocks */
678     if (!s->mb_intra) {
679         if (s->h263_pred) {
680             int wrap, x, y, v;
681             wrap = 2 * s->mb_width + 2;
682             v = 1024;
683             x = 2 * mb_x + 1;
684             y = 2 * mb_y + 1;
685             s->dc_val[0][(x) + (y) * wrap] = v;
686             s->dc_val[0][(x + 1) + (y) * wrap] = v;
687             s->dc_val[0][(x) + (y + 1) * wrap] = v;
688             s->dc_val[0][(x + 1) + (y + 1) * wrap] = v;
689             /* ac pred */
690             memset(s->ac_val[0][(x) + (y) * wrap], 0, 16 * sizeof(INT16));
691             memset(s->ac_val[0][(x + 1) + (y) * wrap], 0, 16 * sizeof(INT16));
692             memset(s->ac_val[0][(x) + (y + 1) * wrap], 0, 16 * sizeof(INT16));
693             memset(s->ac_val[0][(x + 1) + (y + 1) * wrap], 0, 16 * sizeof(INT16));
694             if (s->h263_msmpeg4) {
695                 s->coded_block[(x) + (y) * wrap] = 0;
696                 s->coded_block[(x + 1) + (y) * wrap] = 0;
697                 s->coded_block[(x) + (y + 1) * wrap] = 0;
698                 s->coded_block[(x + 1) + (y + 1) * wrap] = 0;
699             }
700             /* chroma */
701             wrap = s->mb_width + 2;
702             x = mb_x + 1;
703             y = mb_y + 1;
704             s->dc_val[1][(x) + (y) * wrap] = v;
705             s->dc_val[2][(x) + (y) * wrap] = v;
706             /* ac pred */
707             memset(s->ac_val[1][(x) + (y) * wrap], 0, 16 * sizeof(INT16));
708             memset(s->ac_val[2][(x) + (y) * wrap], 0, 16 * sizeof(INT16));
709         } else {
710             s->last_dc[0] = 128 << s->intra_dc_precision;
711             s->last_dc[1] = 128 << s->intra_dc_precision;
712             s->last_dc[2] = 128 << s->intra_dc_precision;
713         }
714     }
715     
716     /* update motion predictor */
717     if (s->out_format == FMT_H263) {
718         int x, y, wrap;
719         
720         x = 2 * mb_x + 1;
721         y = 2 * mb_y + 1;
722         wrap = 2 * s->mb_width + 2;
723         if (s->mb_intra) {
724             motion_x = 0;
725             motion_y = 0;
726             goto motion_init;
727         } else if (s->mv_type == MV_TYPE_16X16) {
728             motion_x = s->mv[0][0][0];
729             motion_y = s->mv[0][0][1];
730         motion_init:
731             /* no update if 8X8 because it has been done during parsing */
732             s->motion_val[(x) + (y) * wrap][0] = motion_x;
733             s->motion_val[(x) + (y) * wrap][1] = motion_y;
734             s->motion_val[(x + 1) + (y) * wrap][0] = motion_x;
735             s->motion_val[(x + 1) + (y) * wrap][1] = motion_y;
736             s->motion_val[(x) + (y + 1) * wrap][0] = motion_x;
737             s->motion_val[(x) + (y + 1) * wrap][1] = motion_y;
738             s->motion_val[(x + 1) + (y + 1) * wrap][0] = motion_x;
739             s->motion_val[(x + 1) + (y + 1) * wrap][1] = motion_y;
740         }
741     }
742     
743     if (!s->intra_only) {
744         UINT8 *dest_y, *dest_cb, *dest_cr;
745         UINT8 *mbskip_ptr;
746
747         /* avoid copy if macroblock skipped in last frame too */
748         if (!s->encoding && s->pict_type != B_TYPE) {
749             mbskip_ptr = &s->mbskip_table[s->mb_y * s->mb_width + s->mb_x];
750             if (s->mb_skiped) {
751                 s->mb_skiped = 0;
752                 /* if previous was skipped too, then nothing to do ! */
753                 if (*mbskip_ptr != 0) 
754                     goto the_end;
755                 *mbskip_ptr = 1; /* indicate that this time we skiped it */
756             } else {
757                 *mbskip_ptr = 0; /* not skipped */
758             }
759         }
760
761         dest_y = s->current_picture[0] + (mb_y * 16 * s->linesize) + mb_x * 16;
762         dest_cb = s->current_picture[1] + (mb_y * 8 * (s->linesize >> 1)) + mb_x * 8;
763         dest_cr = s->current_picture[2] + (mb_y * 8 * (s->linesize >> 1)) + mb_x * 8;
764
765         if (s->interlaced_dct) {
766             dct_linesize = s->linesize * 2;
767             dct_offset = s->linesize;
768         } else {
769             dct_linesize = s->linesize;
770             dct_offset = s->linesize * 8;
771         }
772
773         if (!s->mb_intra) {
774             /* motion handling */
775             if (!s->no_rounding) 
776                 op_pix = put_pixels_tab;
777             else
778                 op_pix = put_no_rnd_pixels_tab;
779
780             if (s->mv_dir & MV_DIR_FORWARD) {
781                 MPV_motion(s, dest_y, dest_cb, dest_cr, 0, s->last_picture, op_pix);
782                 if (!s->no_rounding) 
783                     op_pix = avg_pixels_tab;
784                 else
785                     op_pix = avg_no_rnd_pixels_tab;
786             }
787             if (s->mv_dir & MV_DIR_BACKWARD) {
788                 MPV_motion(s, dest_y, dest_cb, dest_cr, 1, s->next_picture, op_pix);
789             }
790
791             /* add dct residue */
792             add_dct(s, block[0], 0, dest_y, dct_linesize);
793             add_dct(s, block[1], 1, dest_y + 8, dct_linesize);
794             add_dct(s, block[2], 2, dest_y + dct_offset, dct_linesize);
795             add_dct(s, block[3], 3, dest_y + dct_offset + 8, dct_linesize);
796
797             add_dct(s, block[4], 4, dest_cb, dct_linesize >> 1);
798             add_dct(s, block[5], 5, dest_cr, dct_linesize >> 1);
799         } else {
800             /* dct only in intra block */
801             put_dct(s, block[0], 0, dest_y, dct_linesize);
802             put_dct(s, block[1], 1, dest_y + 8, dct_linesize);
803             put_dct(s, block[2], 2, dest_y + dct_offset, dct_linesize);
804             put_dct(s, block[3], 3, dest_y + dct_offset + 8, dct_linesize);
805
806             put_dct(s, block[4], 4, dest_cb, dct_linesize >> 1);
807             put_dct(s, block[5], 5, dest_cr, dct_linesize >> 1);
808         }
809     }
810  the_end:
811     emms_c();
812 }
813
814 static void encode_picture(MpegEncContext *s, int picture_number)
815 {
816     int mb_x, mb_y, wrap;
817     UINT8 *ptr;
818     int i, motion_x, motion_y;
819
820     s->picture_number = picture_number;
821     if (!s->fixed_qscale) 
822         s->qscale = rate_estimate_qscale(s);
823
824     /* precompute matrix */
825     if (s->out_format == FMT_MJPEG) {
826         /* for mjpeg, we do include qscale in the matrix */
827         s->intra_matrix[0] = default_intra_matrix[0];
828         for(i=1;i<64;i++)
829             s->intra_matrix[i] = (default_intra_matrix[i] * s->qscale) >> 3;
830         convert_matrix(s->q_intra_matrix, s->intra_matrix, 8);
831     } else {
832         convert_matrix(s->q_intra_matrix, s->intra_matrix, s->qscale);
833         convert_matrix(s->q_non_intra_matrix, s->non_intra_matrix, s->qscale);
834     }
835
836     switch(s->out_format) {
837     case FMT_MJPEG:
838         mjpeg_picture_header(s);
839         break;
840     case FMT_H263:
841         if (s->h263_msmpeg4) 
842             msmpeg4_encode_picture_header(s, picture_number);
843         else if (s->h263_pred)
844             mpeg4_encode_picture_header(s, picture_number);
845         else if (s->h263_rv10) 
846             rv10_encode_picture_header(s, picture_number);
847         else
848             h263_encode_picture_header(s, picture_number);
849         break;
850     case FMT_MPEG1:
851         mpeg1_encode_picture_header(s, picture_number);
852         break;
853     }
854         
855     /* init last dc values */
856     /* note: quant matrix value (8) is implied here */
857     s->last_dc[0] = 128;
858     s->last_dc[1] = 128;
859     s->last_dc[2] = 128;
860     s->mb_incr = 1;
861     s->last_mv[0][0][0] = 0;
862     s->last_mv[0][0][1] = 0;
863     s->mv_type = MV_TYPE_16X16;
864     s->mv_dir = MV_DIR_FORWARD;
865
866     for(mb_y=0; mb_y < s->mb_height; mb_y++) {
867         for(mb_x=0; mb_x < s->mb_width; mb_x++) {
868
869             s->mb_x = mb_x;
870             s->mb_y = mb_y;
871
872             /* compute motion vector and macro block type (intra or non intra) */
873             motion_x = 0;
874             motion_y = 0;
875             if (s->pict_type == P_TYPE) {
876                 s->mb_intra = estimate_motion(s, mb_x, mb_y,
877                                               &motion_x,
878                                               &motion_y);
879             } else {
880                 s->mb_intra = 1;
881             }
882
883             /* get the pixels */
884             wrap = s->linesize;
885             ptr = s->new_picture[0] + (mb_y * 16 * wrap) + mb_x * 16;
886             get_pixels(s->block[0], ptr, wrap);
887             get_pixels(s->block[1], ptr + 8, wrap);
888             get_pixels(s->block[2], ptr + 8 * wrap, wrap);
889             get_pixels(s->block[3], ptr + 8 * wrap + 8, wrap);
890             wrap = s->linesize >> 1;
891             ptr = s->new_picture[1] + (mb_y * 8 * wrap) + mb_x * 8;
892             get_pixels(s->block[4], ptr, wrap);
893
894             wrap = s->linesize >> 1;
895             ptr = s->new_picture[2] + (mb_y * 8 * wrap) + mb_x * 8;
896             get_pixels(s->block[5], ptr, wrap);
897
898             /* subtract previous frame if non intra */
899             if (!s->mb_intra) {
900                 int dxy, offset, mx, my;
901
902                 dxy = ((motion_y & 1) << 1) | (motion_x & 1);
903                 ptr = s->last_picture[0] + 
904                     ((mb_y * 16 + (motion_y >> 1)) * s->linesize) + 
905                     (mb_x * 16 + (motion_x >> 1));
906
907                 sub_pixels_2(s->block[0], ptr, s->linesize, dxy);
908                 sub_pixels_2(s->block[1], ptr + 8, s->linesize, dxy);
909                 sub_pixels_2(s->block[2], ptr + s->linesize * 8, s->linesize, dxy);
910                 sub_pixels_2(s->block[3], ptr + 8 + s->linesize * 8, s->linesize ,dxy);
911
912                 if (s->out_format == FMT_H263) {
913                     /* special rounding for h263 */
914                     dxy = 0;
915                     if ((motion_x & 3) != 0)
916                         dxy |= 1;
917                     if ((motion_y & 3) != 0)
918                         dxy |= 2;
919                     mx = motion_x >> 2;
920                     my = motion_y >> 2;
921                 } else {
922                     mx = motion_x / 2;
923                     my = motion_y / 2;
924                     dxy = ((my & 1) << 1) | (mx & 1);
925                     mx >>= 1;
926                     my >>= 1;
927                 }
928                 offset = ((mb_y * 8 + my) * (s->linesize >> 1)) + (mb_x * 8 + mx);
929                 ptr = s->last_picture[1] + offset;
930                 sub_pixels_2(s->block[4], ptr, s->linesize >> 1, dxy);
931                 ptr = s->last_picture[2] + offset;
932                 sub_pixels_2(s->block[5], ptr, s->linesize >> 1, dxy);
933             }
934             emms_c();
935
936             /* DCT & quantize */
937             if (s->h263_msmpeg4) {
938                 msmpeg4_dc_scale(s);
939             } else if (s->h263_pred) {
940                 h263_dc_scale(s);
941             } else {
942                 /* default quantization values */
943                 s->y_dc_scale = 8;
944                 s->c_dc_scale = 8;
945             }
946
947             for(i=0;i<6;i++) {
948                 int last_index;
949                 if (av_fdct == jpeg_fdct_ifast)
950                     last_index = dct_quantize(s, s->block[i], i, s->qscale);
951                 else
952                     last_index = dct_quantize_mmx(s, s->block[i], i, s->qscale);
953                 s->block_last_index[i] = last_index;
954             }
955
956             /* huffman encode */
957             switch(s->out_format) {
958             case FMT_MPEG1:
959                 mpeg1_encode_mb(s, s->block, motion_x, motion_y);
960                 break;
961             case FMT_H263:
962                 if (s->h263_msmpeg4)
963                     msmpeg4_encode_mb(s, s->block, motion_x, motion_y);
964                 else
965                     h263_encode_mb(s, s->block, motion_x, motion_y);
966                 break;
967             case FMT_MJPEG:
968                 mjpeg_encode_mb(s, s->block);
969                 break;
970             }
971
972             /* decompress blocks so that we keep the state of the decoder */
973             s->mv[0][0][0] = motion_x;
974             s->mv[0][0][1] = motion_y;
975
976             MPV_decode_mb(s, s->block);
977         }
978     }
979 }
980
981 static int dct_quantize(MpegEncContext *s, 
982                         DCTELEM *block, int n,
983                         int qscale)
984 {
985     int i, j, level, last_non_zero, q;
986     const int *qmat;
987
988     av_fdct (block);
989
990     if (s->mb_intra) {
991         if (n < 4)
992             q = s->y_dc_scale;
993         else
994             q = s->c_dc_scale;
995         q = q << 3;
996         
997         /* note: block[0] is assumed to be positive */
998         block[0] = (block[0] + (q >> 1)) / q;
999         i = 1;
1000         last_non_zero = 0;
1001         if (s->out_format == FMT_H263) {
1002             qmat = s->q_non_intra_matrix;
1003         } else {
1004             qmat = s->q_intra_matrix;
1005         }
1006     } else {
1007         i = 0;
1008         last_non_zero = -1;
1009         qmat = s->q_non_intra_matrix;
1010     }
1011
1012     for(;i<64;i++) {
1013         j = zigzag_direct[i];
1014         level = block[j];
1015         level = level * qmat[j];
1016 #ifdef PARANOID
1017         {
1018             static int count = 0;
1019             int level1, level2, qmat1;
1020             double val;
1021             if (qmat == s->q_non_intra_matrix) {
1022                 qmat1 = default_non_intra_matrix[j] * s->qscale;
1023             } else {
1024                 qmat1 = default_intra_matrix[j] * s->qscale;
1025             }
1026             if (av_fdct != jpeg_fdct_ifast)
1027                 val = ((double)block[j] * 8.0) / (double)qmat1;
1028             else
1029                 val = ((double)block[j] * 8.0 * 2048.0) / 
1030                     ((double)qmat1 * aanscales[j]);
1031             level1 = (int)val;
1032             level2 = level / (1 << (QMAT_SHIFT - 3));
1033             if (level1 != level2) {
1034                 fprintf(stderr, "%d: quant error qlevel=%d wanted=%d level=%d qmat1=%d qmat=%d wantedf=%0.6f\n", 
1035                         count, level2, level1, block[j], qmat1, qmat[j],
1036                         val);
1037                 count++;
1038             }
1039
1040         }
1041 #endif
1042         /* XXX: slight error for the low range. Test should be equivalent to
1043            (level <= -(1 << (QMAT_SHIFT - 3)) || level >= (1 <<
1044            (QMAT_SHIFT - 3)))
1045         */
1046         if (((level << (31 - (QMAT_SHIFT - 3))) >> (31 - (QMAT_SHIFT - 3))) != 
1047             level) {
1048             level = level / (1 << (QMAT_SHIFT - 3));
1049             /* XXX: currently, this code is not optimal. the range should be:
1050                mpeg1: -255..255
1051                mpeg2: -2048..2047
1052                h263:  -128..127
1053                mpeg4: -2048..2047
1054             */
1055             if (level > 127)
1056                 level = 127;
1057             else if (level < -128)
1058                 level = -128;
1059             block[j] = level;
1060             last_non_zero = i;
1061         } else {
1062             block[j] = 0;
1063         }
1064     }
1065     return last_non_zero;
1066 }
1067
1068 static int dct_quantize_mmx(MpegEncContext *s, 
1069                             DCTELEM *block, int n,
1070                             int qscale)
1071 {
1072     int i, j, level, last_non_zero, q;
1073     const int *qmat;
1074
1075     av_fdct (block);
1076
1077     if (s->mb_intra) {
1078         if (n < 4)
1079             q = s->y_dc_scale;
1080         else
1081             q = s->c_dc_scale;
1082         
1083         /* note: block[0] is assumed to be positive */
1084         block[0] = (block[0] + (q >> 1)) / q;
1085         i = 1;
1086         last_non_zero = 0;
1087         if (s->out_format == FMT_H263) {
1088             qmat = s->q_non_intra_matrix;
1089         } else {
1090             qmat = s->q_intra_matrix;
1091         }
1092     } else {
1093         i = 0;
1094         last_non_zero = -1;
1095         qmat = s->q_non_intra_matrix;
1096     }
1097
1098     for(;i<64;i++) {
1099         j = zigzag_direct[i];
1100         level = block[j];
1101         level = level * qmat[j];
1102         /* XXX: slight error for the low range. Test should be equivalent to
1103            (level <= -(1 << (QMAT_SHIFT_MMX - 3)) || level >= (1 <<
1104            (QMAT_SHIFT_MMX - 3)))
1105         */
1106         if (((level << (31 - (QMAT_SHIFT_MMX - 3))) >> (31 - (QMAT_SHIFT_MMX - 3))) != 
1107             level) {
1108             level = level / (1 << (QMAT_SHIFT_MMX - 3));
1109             /* XXX: currently, this code is not optimal. the range should be:
1110                mpeg1: -255..255
1111                mpeg2: -2048..2047
1112                h263:  -128..127
1113                mpeg4: -2048..2047
1114             */
1115             if (level > 127)
1116                 level = 127;
1117             else if (level < -128)
1118                 level = -128;
1119             block[j] = level;
1120             last_non_zero = i;
1121         } else {
1122             block[j] = 0;
1123         }
1124     }
1125     return last_non_zero;
1126 }
1127
1128 static void dct_unquantize_mpeg1_c(MpegEncContext *s, 
1129                                    DCTELEM *block, int n, int qscale)
1130 {
1131     int i, level;
1132     const UINT16 *quant_matrix;
1133
1134     if (s->mb_intra) {
1135         if (n < 4) 
1136             block[0] = block[0] * s->y_dc_scale;
1137         else
1138             block[0] = block[0] * s->c_dc_scale;
1139         /* XXX: only mpeg1 */
1140         quant_matrix = s->intra_matrix;
1141         for(i=1;i<64;i++) {
1142             level = block[i];
1143             if (level) {
1144                 if (level < 0) {
1145                     level = -level;
1146                     level = (int)(level * qscale * quant_matrix[i]) >> 3;
1147                     level = (level - 1) | 1;
1148                     level = -level;
1149                 } else {
1150                     level = (int)(level * qscale * quant_matrix[i]) >> 3;
1151                     level = (level - 1) | 1;
1152                 }
1153 #ifdef PARANOID
1154                 if (level < -2048 || level > 2047)
1155                     fprintf(stderr, "unquant error %d %d\n", i, level);
1156 #endif
1157                 block[i] = level;
1158             }
1159         }
1160     } else {
1161         i = 0;
1162         quant_matrix = s->non_intra_matrix;
1163         for(;i<64;i++) {
1164             level = block[i];
1165             if (level) {
1166                 if (level < 0) {
1167                     level = -level;
1168                     level = (((level << 1) + 1) * qscale *
1169                              ((int) (quant_matrix[i]))) >> 4;
1170                     level = (level - 1) | 1;
1171                     level = -level;
1172                 } else {
1173                     level = (((level << 1) + 1) * qscale *
1174                              ((int) (quant_matrix[i]))) >> 4;
1175                     level = (level - 1) | 1;
1176                 }
1177 #ifdef PARANOID
1178                 if (level < -2048 || level > 2047)
1179                     fprintf(stderr, "unquant error %d %d\n", i, level);
1180 #endif
1181                 block[i] = level;
1182             }
1183         }
1184     }
1185 }
1186
1187 static void dct_unquantize_h263_c(MpegEncContext *s, 
1188                                   DCTELEM *block, int n, int qscale)
1189 {
1190     int i, level, qmul, qadd;
1191
1192     if (s->mb_intra) {
1193         if (n < 4) 
1194             block[0] = block[0] * s->y_dc_scale;
1195         else
1196             block[0] = block[0] * s->c_dc_scale;
1197         i = 1;
1198     } else {
1199         i = 0;
1200     }
1201
1202     qmul = s->qscale << 1;
1203     qadd = (s->qscale - 1) | 1;
1204
1205     for(;i<64;i++) {
1206         level = block[i];
1207         if (level) {
1208             if (level < 0) {
1209                 level = level * qmul - qadd;
1210             } else {
1211                 level = level * qmul + qadd;
1212             }
1213 #ifdef PARANOID
1214                 if (level < -2048 || level > 2047)
1215                     fprintf(stderr, "unquant error %d %d\n", i, level);
1216 #endif
1217             block[i] = level;
1218         }
1219     }
1220 }
1221
1222 /* rate control */
1223
1224 /* an I frame is I_FRAME_SIZE_RATIO bigger than a P frame */
1225 #define I_FRAME_SIZE_RATIO 3.0
1226 #define QSCALE_K           20
1227
1228 static void rate_control_init(MpegEncContext *s)
1229 {
1230     s->wanted_bits = 0;
1231
1232     if (s->intra_only) {
1233         s->I_frame_bits = ((INT64)s->bit_rate * FRAME_RATE_BASE) / s->frame_rate;
1234         s->P_frame_bits = s->I_frame_bits;
1235     } else {
1236         s->P_frame_bits = (int) ((float)(s->gop_size * s->bit_rate) / 
1237                                  (float)((float)s->frame_rate / FRAME_RATE_BASE * (I_FRAME_SIZE_RATIO + s->gop_size - 1)));
1238         s->I_frame_bits = (int)(s->P_frame_bits * I_FRAME_SIZE_RATIO);
1239     }
1240     
1241 #if defined(DEBUG)
1242     printf("I_frame_size=%d P_frame_size=%d\n",
1243            s->I_frame_bits, s->P_frame_bits);
1244 #endif
1245 }
1246
1247
1248 /*
1249  * This heuristic is rather poor, but at least we do not have to
1250  * change the qscale at every macroblock.
1251  */
1252 static int rate_estimate_qscale(MpegEncContext *s)
1253 {
1254     long long total_bits = s->total_bits;
1255     float q;
1256     int qscale, diff, qmin;
1257
1258     if (s->pict_type == I_TYPE) {
1259         s->wanted_bits += s->I_frame_bits;
1260     } else {
1261         s->wanted_bits += s->P_frame_bits;
1262     }
1263     diff = s->wanted_bits - total_bits;
1264     q = 31.0 - (float)diff / (QSCALE_K * s->mb_height * s->mb_width);
1265     /* adjust for I frame */
1266     if (s->pict_type == I_TYPE && !s->intra_only) {
1267         q /= I_FRAME_SIZE_RATIO;
1268     }
1269
1270     /* using a too small Q scale leeds to problems in mpeg1 and h263
1271        because AC coefficients are clamped to 255 or 127 */
1272     qmin = 3;
1273     if (q < qmin)
1274         q = qmin;
1275     else if (q > 31)
1276         q = 31;
1277     qscale = (int)(q + 0.5);
1278 #if defined(DEBUG)
1279     printf("%d: total=%Ld br=%0.1f diff=%d qest=%0.1f\n", 
1280            s->picture_number, 
1281            total_bits, 
1282            (float)s->frame_rate / FRAME_RATE_BASE * 
1283            total_bits / s->picture_number, 
1284            diff, q);
1285 #endif
1286     return qscale;
1287 }
1288
1289 AVCodec mpeg1video_encoder = {
1290     "mpeg1video",
1291     CODEC_TYPE_VIDEO,
1292     CODEC_ID_MPEG1VIDEO,
1293     sizeof(MpegEncContext),
1294     MPV_encode_init,
1295     MPV_encode_picture,
1296     MPV_encode_end,
1297 };
1298
1299 AVCodec h263_encoder = {
1300     "h263",
1301     CODEC_TYPE_VIDEO,
1302     CODEC_ID_H263,
1303     sizeof(MpegEncContext),
1304     MPV_encode_init,
1305     MPV_encode_picture,
1306     MPV_encode_end,
1307 };
1308
1309 AVCodec h263p_encoder = {
1310     "h263p",
1311     CODEC_TYPE_VIDEO,
1312     CODEC_ID_H263P,
1313     sizeof(MpegEncContext),
1314     MPV_encode_init,
1315     MPV_encode_picture,
1316     MPV_encode_end,
1317 };
1318
1319 AVCodec rv10_encoder = {
1320     "rv10",
1321     CODEC_TYPE_VIDEO,
1322     CODEC_ID_RV10,
1323     sizeof(MpegEncContext),
1324     MPV_encode_init,
1325     MPV_encode_picture,
1326     MPV_encode_end,
1327 };
1328
1329 AVCodec mjpeg_encoder = {
1330     "mjpeg",
1331     CODEC_TYPE_VIDEO,
1332     CODEC_ID_MJPEG,
1333     sizeof(MpegEncContext),
1334     MPV_encode_init,
1335     MPV_encode_picture,
1336     MPV_encode_end,
1337 };
1338
1339 AVCodec opendivx_encoder = {
1340     "opendivx",
1341     CODEC_TYPE_VIDEO,
1342     CODEC_ID_OPENDIVX,
1343     sizeof(MpegEncContext),
1344     MPV_encode_init,
1345     MPV_encode_picture,
1346     MPV_encode_end,
1347 };
1348
1349 AVCodec msmpeg4_encoder = {
1350     "msmpeg4",
1351     CODEC_TYPE_VIDEO,
1352     CODEC_ID_MSMPEG4,
1353     sizeof(MpegEncContext),
1354     MPV_encode_init,
1355     MPV_encode_picture,
1356     MPV_encode_end,
1357 };