Merge commit 'cebdedca57d95834a8f8098c7b6a322a1163e26b'
[ffmpeg.git] / libavcodec / mpeg4videoenc.c
1 /*
2  * MPEG4 encoder.
3  * Copyright (c) 2000,2001 Fabrice Bellard
4  * Copyright (c) 2002-2010 Michael Niedermayer <michaelni@gmx.at>
5  *
6  * This file is part of FFmpeg.
7  *
8  * FFmpeg is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU Lesser General Public
10  * License as published by the Free Software Foundation; either
11  * version 2.1 of the License, or (at your option) any later version.
12  *
13  * FFmpeg is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16  * Lesser General Public License for more details.
17  *
18  * You should have received a copy of the GNU Lesser General Public
19  * License along with FFmpeg; if not, write to the Free Software
20  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21  */
22
23 #include "libavutil/attributes.h"
24 #include "libavutil/log.h"
25 #include "libavutil/opt.h"
26 #include "mpegvideo.h"
27 #include "h263.h"
28 #include "mpeg4video.h"
29
30 //The uni_DCtab_* tables below contain unified bits+length tables to encode DC
31 //differences in mpeg4. Unified in the sense that the specification specifies
32 //this encoding in several steps.
33 static uint8_t uni_DCtab_lum_len[512];
34 static uint8_t uni_DCtab_chrom_len[512];
35 static uint16_t uni_DCtab_lum_bits[512];
36 static uint16_t uni_DCtab_chrom_bits[512];
37
38 //unified encoding tables for run length encoding of coefficients
39 //unified in the sense that the specification specifies the encoding in several steps.
40 static uint32_t uni_mpeg4_intra_rl_bits[64*64*2*2];
41 static uint8_t  uni_mpeg4_intra_rl_len [64*64*2*2];
42 static uint32_t uni_mpeg4_inter_rl_bits[64*64*2*2];
43 static uint8_t  uni_mpeg4_inter_rl_len [64*64*2*2];
44 //#define UNI_MPEG4_ENC_INDEX(last,run,level) ((last)*128 + (run)*256 + (level))
45 //#define UNI_MPEG4_ENC_INDEX(last,run,level) ((last)*128*64 + (run) + (level)*64)
46 #define UNI_MPEG4_ENC_INDEX(last,run,level) ((last)*128*64 + (run)*128 + (level))
47
48 /* mpeg4
49 inter
50 max level: 24/6
51 max run: 53/63
52
53 intra
54 max level: 53/16
55 max run: 29/41
56 */
57
58
59 /**
60  * Return the number of bits that encoding the 8x8 block in block would need.
61  * @param[in]  block_last_index last index in scantable order that refers to a non zero element in block.
62  */
63 static inline int get_block_rate(MpegEncContext * s, int16_t block[64], int block_last_index, uint8_t scantable[64]){
64     int last=0;
65     int j;
66     int rate=0;
67
68     for(j=1; j<=block_last_index; j++){
69         const int index= scantable[j];
70         int level= block[index];
71         if(level){
72             level+= 64;
73             if((level&(~127)) == 0){
74                 if(j<block_last_index) rate+= s->intra_ac_vlc_length     [UNI_AC_ENC_INDEX(j-last-1, level)];
75                 else                   rate+= s->intra_ac_vlc_last_length[UNI_AC_ENC_INDEX(j-last-1, level)];
76             }else
77                 rate += s->ac_esc_length;
78
79             last= j;
80         }
81     }
82
83     return rate;
84 }
85
86
87 /**
88  * Restore the ac coefficients in block that have been changed by decide_ac_pred().
89  * This function also restores s->block_last_index.
90  * @param[in,out] block MB coefficients, these will be restored
91  * @param[in] dir ac prediction direction for each 8x8 block
92  * @param[out] st scantable for each 8x8 block
93  * @param[in] zigzag_last_index index referring to the last non zero coefficient in zigzag order
94  */
95 static inline void restore_ac_coeffs(MpegEncContext * s, int16_t block[6][64], const int dir[6], uint8_t *st[6], const int zigzag_last_index[6])
96 {
97     int i, n;
98     memcpy(s->block_last_index, zigzag_last_index, sizeof(int)*6);
99
100     for(n=0; n<6; n++){
101         int16_t *ac_val = s->ac_val[0][0] + s->block_index[n] * 16;
102
103         st[n]= s->intra_scantable.permutated;
104         if(dir[n]){
105             /* top prediction */
106             for(i=1; i<8; i++){
107                 block[n][s->dsp.idct_permutation[i   ]] = ac_val[i+8];
108             }
109         }else{
110             /* left prediction */
111             for(i=1; i<8; i++){
112                 block[n][s->dsp.idct_permutation[i<<3]]= ac_val[i  ];
113             }
114         }
115     }
116 }
117
118 /**
119  * Return the optimal value (0 or 1) for the ac_pred element for the given MB in mpeg4.
120  * This function will also update s->block_last_index and s->ac_val.
121  * @param[in,out] block MB coefficients, these will be updated if 1 is returned
122  * @param[in] dir ac prediction direction for each 8x8 block
123  * @param[out] st scantable for each 8x8 block
124  * @param[out] zigzag_last_index index referring to the last non zero coefficient in zigzag order
125  */
126 static inline int decide_ac_pred(MpegEncContext * s, int16_t block[6][64], const int dir[6], uint8_t *st[6], int zigzag_last_index[6])
127 {
128     int score= 0;
129     int i, n;
130     int8_t * const qscale_table = s->current_picture.qscale_table;
131
132     memcpy(zigzag_last_index, s->block_last_index, sizeof(int)*6);
133
134     for(n=0; n<6; n++){
135         int16_t *ac_val, *ac_val1;
136
137         score -= get_block_rate(s, block[n], s->block_last_index[n], s->intra_scantable.permutated);
138
139         ac_val = s->ac_val[0][0] + s->block_index[n] * 16;
140         ac_val1= ac_val;
141         if(dir[n]){
142             const int xy= s->mb_x + s->mb_y*s->mb_stride - s->mb_stride;
143             /* top prediction */
144             ac_val-= s->block_wrap[n]*16;
145             if(s->mb_y==0 || s->qscale == qscale_table[xy] || n==2 || n==3){
146                 /* same qscale */
147                 for(i=1; i<8; i++){
148                     const int level= block[n][s->dsp.idct_permutation[i   ]];
149                     block[n][s->dsp.idct_permutation[i   ]] = level - ac_val[i+8];
150                     ac_val1[i  ]=    block[n][s->dsp.idct_permutation[i<<3]];
151                     ac_val1[i+8]= level;
152                 }
153             }else{
154                 /* different qscale, we must rescale */
155                 for(i=1; i<8; i++){
156                     const int level= block[n][s->dsp.idct_permutation[i   ]];
157                     block[n][s->dsp.idct_permutation[i   ]] = level - ROUNDED_DIV(ac_val[i + 8]*qscale_table[xy], s->qscale);
158                     ac_val1[i  ]=    block[n][s->dsp.idct_permutation[i<<3]];
159                     ac_val1[i+8]= level;
160                 }
161             }
162             st[n]= s->intra_h_scantable.permutated;
163         }else{
164             const int xy= s->mb_x-1 + s->mb_y*s->mb_stride;
165             /* left prediction */
166             ac_val-= 16;
167             if(s->mb_x==0 || s->qscale == qscale_table[xy] || n==1 || n==3){
168                 /* same qscale */
169                 for(i=1; i<8; i++){
170                     const int level= block[n][s->dsp.idct_permutation[i<<3]];
171                     block[n][s->dsp.idct_permutation[i<<3]]= level - ac_val[i];
172                     ac_val1[i  ]= level;
173                     ac_val1[i+8]=    block[n][s->dsp.idct_permutation[i   ]];
174                 }
175             }else{
176                 /* different qscale, we must rescale */
177                 for(i=1; i<8; i++){
178                     const int level= block[n][s->dsp.idct_permutation[i<<3]];
179                     block[n][s->dsp.idct_permutation[i<<3]]= level - ROUNDED_DIV(ac_val[i]*qscale_table[xy], s->qscale);
180                     ac_val1[i  ]= level;
181                     ac_val1[i+8]=    block[n][s->dsp.idct_permutation[i   ]];
182                 }
183             }
184             st[n]= s->intra_v_scantable.permutated;
185         }
186
187         for(i=63; i>0; i--) //FIXME optimize
188             if(block[n][ st[n][i] ]) break;
189         s->block_last_index[n]= i;
190
191         score += get_block_rate(s, block[n], s->block_last_index[n], st[n]);
192     }
193
194     if(score < 0){
195         return 1;
196     }else{
197         restore_ac_coeffs(s, block, dir, st, zigzag_last_index);
198         return 0;
199     }
200 }
201
202 /**
203  * modify mb_type & qscale so that encoding is actually possible in mpeg4
204  */
205 void ff_clean_mpeg4_qscales(MpegEncContext *s){
206     int i;
207     int8_t * const qscale_table = s->current_picture.qscale_table;
208
209     ff_clean_h263_qscales(s);
210
211     if(s->pict_type== AV_PICTURE_TYPE_B){
212         int odd=0;
213         /* ok, come on, this isn't funny anymore, there's more code for handling this mpeg4 mess than for the actual adaptive quantization */
214
215         for(i=0; i<s->mb_num; i++){
216             int mb_xy= s->mb_index2xy[i];
217             odd += qscale_table[mb_xy]&1;
218         }
219
220         if(2*odd > s->mb_num) odd=1;
221         else                  odd=0;
222
223         for(i=0; i<s->mb_num; i++){
224             int mb_xy= s->mb_index2xy[i];
225             if((qscale_table[mb_xy]&1) != odd)
226                 qscale_table[mb_xy]++;
227             if(qscale_table[mb_xy] > 31)
228                 qscale_table[mb_xy]= 31;
229         }
230
231         for(i=1; i<s->mb_num; i++){
232             int mb_xy= s->mb_index2xy[i];
233             if(qscale_table[mb_xy] != qscale_table[s->mb_index2xy[i-1]] && (s->mb_type[mb_xy]&CANDIDATE_MB_TYPE_DIRECT)){
234                 s->mb_type[mb_xy]|= CANDIDATE_MB_TYPE_BIDIR;
235             }
236         }
237     }
238 }
239
240
241 /**
242  * Encode the dc value.
243  * @param n block index (0-3 are luma, 4-5 are chroma)
244  */
245 static inline void mpeg4_encode_dc(PutBitContext * s, int level, int n)
246 {
247 #if 1
248     /* DC will overflow if level is outside the [-255,255] range. */
249     level+=256;
250     if (n < 4) {
251         /* luminance */
252         put_bits(s, uni_DCtab_lum_len[level], uni_DCtab_lum_bits[level]);
253     } else {
254         /* chrominance */
255         put_bits(s, uni_DCtab_chrom_len[level], uni_DCtab_chrom_bits[level]);
256     }
257 #else
258     int size, v;
259     /* find number of bits */
260     size = 0;
261     v = abs(level);
262     while (v) {
263         v >>= 1;
264         size++;
265     }
266
267     if (n < 4) {
268         /* luminance */
269         put_bits(&s->pb, ff_mpeg4_DCtab_lum[size][1], ff_mpeg4_DCtab_lum[size][0]);
270     } else {
271         /* chrominance */
272         put_bits(&s->pb, ff_mpeg4_DCtab_chrom[size][1], ff_mpeg4_DCtab_chrom[size][0]);
273     }
274
275     /* encode remaining bits */
276     if (size > 0) {
277         if (level < 0)
278             level = (-level) ^ ((1 << size) - 1);
279         put_bits(&s->pb, size, level);
280         if (size > 8)
281             put_bits(&s->pb, 1, 1);
282     }
283 #endif
284 }
285
286 static inline int mpeg4_get_dc_length(int level, int n){
287     if (n < 4) {
288         return uni_DCtab_lum_len[level + 256];
289     } else {
290         return uni_DCtab_chrom_len[level + 256];
291     }
292 }
293
294 /**
295  * Encode an 8x8 block.
296  * @param n block index (0-3 are luma, 4-5 are chroma)
297  */
298 static inline void mpeg4_encode_block(MpegEncContext * s, int16_t * block, int n, int intra_dc,
299                                uint8_t *scan_table, PutBitContext *dc_pb, PutBitContext *ac_pb)
300 {
301     int i, last_non_zero;
302     uint32_t *bits_tab;
303     uint8_t *len_tab;
304     const int last_index = s->block_last_index[n];
305
306     if (s->mb_intra) { //Note gcc (3.2.1 at least) will optimize this away
307         /* mpeg4 based DC predictor */
308         mpeg4_encode_dc(dc_pb, intra_dc, n);
309         if(last_index<1) return;
310         i = 1;
311         bits_tab= uni_mpeg4_intra_rl_bits;
312         len_tab = uni_mpeg4_intra_rl_len;
313     } else {
314         if(last_index<0) return;
315         i = 0;
316         bits_tab= uni_mpeg4_inter_rl_bits;
317         len_tab = uni_mpeg4_inter_rl_len;
318     }
319
320     /* AC coefs */
321     last_non_zero = i - 1;
322     for (; i < last_index; i++) {
323         int level = block[ scan_table[i] ];
324         if (level) {
325             int run = i - last_non_zero - 1;
326             level+=64;
327             if((level&(~127)) == 0){
328                 const int index= UNI_MPEG4_ENC_INDEX(0, run, level);
329                 put_bits(ac_pb, len_tab[index], bits_tab[index]);
330             }else{ //ESC3
331                 put_bits(ac_pb, 7+2+1+6+1+12+1, (3<<23)+(3<<21)+(0<<20)+(run<<14)+(1<<13)+(((level-64)&0xfff)<<1)+1);
332             }
333             last_non_zero = i;
334         }
335     }
336     /*if(i<=last_index)*/{
337         int level = block[ scan_table[i] ];
338         int run = i - last_non_zero - 1;
339         level+=64;
340         if((level&(~127)) == 0){
341             const int index= UNI_MPEG4_ENC_INDEX(1, run, level);
342             put_bits(ac_pb, len_tab[index], bits_tab[index]);
343         }else{ //ESC3
344             put_bits(ac_pb, 7+2+1+6+1+12+1, (3<<23)+(3<<21)+(1<<20)+(run<<14)+(1<<13)+(((level-64)&0xfff)<<1)+1);
345         }
346     }
347 }
348
349 static int mpeg4_get_block_length(MpegEncContext * s, int16_t * block, int n, int intra_dc,
350                                uint8_t *scan_table)
351 {
352     int i, last_non_zero;
353     uint8_t *len_tab;
354     const int last_index = s->block_last_index[n];
355     int len=0;
356
357     if (s->mb_intra) { //Note gcc (3.2.1 at least) will optimize this away
358         /* mpeg4 based DC predictor */
359         len += mpeg4_get_dc_length(intra_dc, n);
360         if(last_index<1) return len;
361         i = 1;
362         len_tab = uni_mpeg4_intra_rl_len;
363     } else {
364         if(last_index<0) return 0;
365         i = 0;
366         len_tab = uni_mpeg4_inter_rl_len;
367     }
368
369     /* AC coefs */
370     last_non_zero = i - 1;
371     for (; i < last_index; i++) {
372         int level = block[ scan_table[i] ];
373         if (level) {
374             int run = i - last_non_zero - 1;
375             level+=64;
376             if((level&(~127)) == 0){
377                 const int index= UNI_MPEG4_ENC_INDEX(0, run, level);
378                 len += len_tab[index];
379             }else{ //ESC3
380                 len += 7+2+1+6+1+12+1;
381             }
382             last_non_zero = i;
383         }
384     }
385     /*if(i<=last_index)*/{
386         int level = block[ scan_table[i] ];
387         int run = i - last_non_zero - 1;
388         level+=64;
389         if((level&(~127)) == 0){
390             const int index= UNI_MPEG4_ENC_INDEX(1, run, level);
391             len += len_tab[index];
392         }else{ //ESC3
393             len += 7+2+1+6+1+12+1;
394         }
395     }
396
397     return len;
398 }
399
400 static inline void mpeg4_encode_blocks(MpegEncContext * s, int16_t block[6][64], int intra_dc[6],
401                                uint8_t **scan_table, PutBitContext *dc_pb, PutBitContext *ac_pb){
402     int i;
403
404     if(scan_table){
405         if(s->flags2 & CODEC_FLAG2_NO_OUTPUT){
406             for (i = 0; i < 6; i++) {
407                 skip_put_bits(&s->pb, mpeg4_get_block_length(s, block[i], i, intra_dc[i], scan_table[i]));
408             }
409         }else{
410             /* encode each block */
411             for (i = 0; i < 6; i++) {
412                 mpeg4_encode_block(s, block[i], i, intra_dc[i], scan_table[i], dc_pb, ac_pb);
413             }
414         }
415     }else{
416         if(s->flags2 & CODEC_FLAG2_NO_OUTPUT){
417             for (i = 0; i < 6; i++) {
418                 skip_put_bits(&s->pb, mpeg4_get_block_length(s, block[i], i, 0, s->intra_scantable.permutated));
419             }
420         }else{
421             /* encode each block */
422             for (i = 0; i < 6; i++) {
423                 mpeg4_encode_block(s, block[i], i, 0, s->intra_scantable.permutated, dc_pb, ac_pb);
424             }
425         }
426     }
427 }
428
429 static inline int get_b_cbp(MpegEncContext * s, int16_t block[6][64],
430                             int motion_x, int motion_y, int mb_type)
431 {
432     int cbp = 0, i;
433
434     if (s->mpv_flags & FF_MPV_FLAG_CBP_RD) {
435         int score = 0;
436         const int lambda = s->lambda2 >> (FF_LAMBDA_SHIFT - 6);
437
438         for (i = 0; i < 6; i++)
439             if (s->coded_score[i] < 0) {
440                 score += s->coded_score[i];
441                 cbp   |= 1 << (5 - i);
442             }
443
444         if (cbp) {
445             int zero_score = -6;
446             if ((motion_x | motion_y | s->dquant | mb_type) == 0)
447                 zero_score -= 4; //2*MV + mb_type + cbp bit
448
449             zero_score *= lambda;
450             if (zero_score <= score)
451                 cbp = 0;
452         }
453
454         for (i = 0; i < 6; i++) {
455             if (s->block_last_index[i] >= 0 && ((cbp >> (5 - i)) & 1) == 0) {
456                 s->block_last_index[i] = -1;
457                 s->dsp.clear_block(s->block[i]);
458             }
459         }
460     } else {
461         for (i = 0; i < 6; i++) {
462             if (s->block_last_index[i] >= 0)
463                 cbp |= 1 << (5 - i);
464         }
465     }
466     return cbp;
467 }
468
469 //FIXME this is duplicated to h263.c
470 static const int dquant_code[5]= {1,0,9,2,3};
471
472 void ff_mpeg4_encode_mb(MpegEncContext * s,
473                         int16_t block[6][64],
474                         int motion_x, int motion_y)
475 {
476     int cbpc, cbpy, pred_x, pred_y;
477     PutBitContext * const pb2    = s->data_partitioning                         ? &s->pb2    : &s->pb;
478     PutBitContext * const tex_pb = s->data_partitioning && s->pict_type!=AV_PICTURE_TYPE_B ? &s->tex_pb : &s->pb;
479     PutBitContext * const dc_pb  = s->data_partitioning && s->pict_type!=AV_PICTURE_TYPE_I ? &s->pb2    : &s->pb;
480     const int interleaved_stats= (s->flags&CODEC_FLAG_PASS1) && !s->data_partitioning ? 1 : 0;
481
482     if (!s->mb_intra) {
483         int i, cbp;
484
485         if(s->pict_type==AV_PICTURE_TYPE_B){
486             static const int mb_type_table[8]= {-1, 3, 2, 1,-1,-1,-1, 0}; /* convert from mv_dir to type */
487             int mb_type=  mb_type_table[s->mv_dir];
488
489             if(s->mb_x==0){
490                 for(i=0; i<2; i++){
491                     s->last_mv[i][0][0]=
492                     s->last_mv[i][0][1]=
493                     s->last_mv[i][1][0]=
494                     s->last_mv[i][1][1]= 0;
495                 }
496             }
497
498             av_assert2(s->dquant>=-2 && s->dquant<=2);
499             av_assert2((s->dquant&1)==0);
500             av_assert2(mb_type>=0);
501
502             /* nothing to do if this MB was skipped in the next P Frame */
503             if (s->next_picture.mbskip_table[s->mb_y * s->mb_stride + s->mb_x]) { //FIXME avoid DCT & ...
504                 s->skip_count++;
505                 s->mv[0][0][0]=
506                 s->mv[0][0][1]=
507                 s->mv[1][0][0]=
508                 s->mv[1][0][1]= 0;
509                 s->mv_dir= MV_DIR_FORWARD; //doesn't matter
510                 s->qscale -= s->dquant;
511 //                s->mb_skipped=1;
512
513                 return;
514             }
515
516             cbp= get_b_cbp(s, block, motion_x, motion_y, mb_type);
517
518             if ((cbp | motion_x | motion_y | mb_type) ==0) {
519                 /* direct MB with MV={0,0} */
520                 av_assert2(s->dquant==0);
521
522                 put_bits(&s->pb, 1, 1); /* mb not coded modb1=1 */
523
524                 if(interleaved_stats){
525                     s->misc_bits++;
526                     s->last_bits++;
527                 }
528                 s->skip_count++;
529                 return;
530             }
531
532             put_bits(&s->pb, 1, 0);     /* mb coded modb1=0 */
533             put_bits(&s->pb, 1, cbp ? 0 : 1); /* modb2 */ //FIXME merge
534             put_bits(&s->pb, mb_type+1, 1); // this table is so simple that we don't need it :)
535             if(cbp) put_bits(&s->pb, 6, cbp);
536
537             if(cbp && mb_type){
538                 if(s->dquant)
539                     put_bits(&s->pb, 2, (s->dquant>>2)+3);
540                 else
541                     put_bits(&s->pb, 1, 0);
542             }else
543                 s->qscale -= s->dquant;
544
545             if(!s->progressive_sequence){
546                 if(cbp)
547                     put_bits(&s->pb, 1, s->interlaced_dct);
548                 if(mb_type) // not direct mode
549                     put_bits(&s->pb, 1, s->mv_type == MV_TYPE_FIELD);
550             }
551
552             if(interleaved_stats){
553                 s->misc_bits+= get_bits_diff(s);
554             }
555
556             if(mb_type == 0){
557                 av_assert2(s->mv_dir & MV_DIRECT);
558                 ff_h263_encode_motion_vector(s, motion_x, motion_y, 1);
559                 s->b_count++;
560                 s->f_count++;
561             }else{
562                 av_assert2(mb_type > 0 && mb_type < 4);
563                 if(s->mv_type != MV_TYPE_FIELD){
564                     if(s->mv_dir & MV_DIR_FORWARD){
565                         ff_h263_encode_motion_vector(s, s->mv[0][0][0] - s->last_mv[0][0][0],
566                                                         s->mv[0][0][1] - s->last_mv[0][0][1], s->f_code);
567                         s->last_mv[0][0][0]= s->last_mv[0][1][0]= s->mv[0][0][0];
568                         s->last_mv[0][0][1]= s->last_mv[0][1][1]= s->mv[0][0][1];
569                         s->f_count++;
570                     }
571                     if(s->mv_dir & MV_DIR_BACKWARD){
572                         ff_h263_encode_motion_vector(s, s->mv[1][0][0] - s->last_mv[1][0][0],
573                                                         s->mv[1][0][1] - s->last_mv[1][0][1], s->b_code);
574                         s->last_mv[1][0][0]= s->last_mv[1][1][0]= s->mv[1][0][0];
575                         s->last_mv[1][0][1]= s->last_mv[1][1][1]= s->mv[1][0][1];
576                         s->b_count++;
577                     }
578                 }else{
579                     if(s->mv_dir & MV_DIR_FORWARD){
580                         put_bits(&s->pb, 1, s->field_select[0][0]);
581                         put_bits(&s->pb, 1, s->field_select[0][1]);
582                     }
583                     if(s->mv_dir & MV_DIR_BACKWARD){
584                         put_bits(&s->pb, 1, s->field_select[1][0]);
585                         put_bits(&s->pb, 1, s->field_select[1][1]);
586                     }
587                     if(s->mv_dir & MV_DIR_FORWARD){
588                         for(i=0; i<2; i++){
589                             ff_h263_encode_motion_vector(s, s->mv[0][i][0] - s->last_mv[0][i][0]  ,
590                                                             s->mv[0][i][1] - s->last_mv[0][i][1]/2, s->f_code);
591                             s->last_mv[0][i][0]= s->mv[0][i][0];
592                             s->last_mv[0][i][1]= s->mv[0][i][1]*2;
593                         }
594                         s->f_count++;
595                     }
596                     if(s->mv_dir & MV_DIR_BACKWARD){
597                         for(i=0; i<2; i++){
598                             ff_h263_encode_motion_vector(s, s->mv[1][i][0] - s->last_mv[1][i][0]  ,
599                                                             s->mv[1][i][1] - s->last_mv[1][i][1]/2, s->b_code);
600                             s->last_mv[1][i][0]= s->mv[1][i][0];
601                             s->last_mv[1][i][1]= s->mv[1][i][1]*2;
602                         }
603                         s->b_count++;
604                     }
605                 }
606             }
607
608             if(interleaved_stats){
609                 s->mv_bits+= get_bits_diff(s);
610             }
611
612             mpeg4_encode_blocks(s, block, NULL, NULL, NULL, &s->pb);
613
614             if(interleaved_stats){
615                 s->p_tex_bits+= get_bits_diff(s);
616             }
617
618         }else{ /* s->pict_type==AV_PICTURE_TYPE_B */
619             cbp= get_p_cbp(s, block, motion_x, motion_y);
620
621             if ((cbp | motion_x | motion_y | s->dquant) == 0 && s->mv_type==MV_TYPE_16X16) {
622                 /* check if the B frames can skip it too, as we must skip it if we skip here
623                    why didn't they just compress the skip-mb bits instead of reusing them ?! */
624                 if(s->max_b_frames>0){
625                     int i;
626                     int x,y, offset;
627                     uint8_t *p_pic;
628
629                     x= s->mb_x*16;
630                     y= s->mb_y*16;
631
632                     offset= x + y*s->linesize;
633                     p_pic = s->new_picture.f.data[0] + offset;
634
635                     s->mb_skipped=1;
636                     for(i=0; i<s->max_b_frames; i++){
637                         uint8_t *b_pic;
638                         int diff;
639                         Picture *pic= s->reordered_input_picture[i+1];
640
641                         if (pic == NULL || pic->f.pict_type != AV_PICTURE_TYPE_B)
642                             break;
643
644                         b_pic = pic->f.data[0] + offset;
645                         if (!pic->shared)
646                             b_pic+= INPLACE_OFFSET;
647
648                         if(x+16 > s->width || y+16 > s->height){
649                             int x1,y1;
650                             int xe= FFMIN(16, s->width - x);
651                             int ye= FFMIN(16, s->height- y);
652                             diff=0;
653                             for(y1=0; y1<ye; y1++){
654                                 for(x1=0; x1<xe; x1++){
655                                     diff+= FFABS(p_pic[x1+y1*s->linesize] - b_pic[x1+y1*s->linesize]);
656                                 }
657                             }
658                             diff= diff*256/(xe*ye);
659                         }else{
660                             diff= s->dsp.sad[0](NULL, p_pic, b_pic, s->linesize, 16);
661                         }
662                         if(diff>s->qscale*70){ //FIXME check that 70 is optimal
663                             s->mb_skipped=0;
664                             break;
665                         }
666                     }
667                 }else
668                     s->mb_skipped=1;
669
670                 if(s->mb_skipped==1){
671                     /* skip macroblock */
672                     put_bits(&s->pb, 1, 1);
673
674                     if(interleaved_stats){
675                         s->misc_bits++;
676                         s->last_bits++;
677                     }
678                     s->skip_count++;
679
680                     return;
681                 }
682             }
683
684             put_bits(&s->pb, 1, 0);     /* mb coded */
685             cbpc = cbp & 3;
686             cbpy = cbp >> 2;
687             cbpy ^= 0xf;
688             if(s->mv_type==MV_TYPE_16X16){
689                 if(s->dquant) cbpc+= 8;
690                 put_bits(&s->pb,
691                         ff_h263_inter_MCBPC_bits[cbpc],
692                         ff_h263_inter_MCBPC_code[cbpc]);
693
694                 put_bits(pb2, ff_h263_cbpy_tab[cbpy][1], ff_h263_cbpy_tab[cbpy][0]);
695                 if(s->dquant)
696                     put_bits(pb2, 2, dquant_code[s->dquant+2]);
697
698                 if(!s->progressive_sequence){
699                     if(cbp)
700                         put_bits(pb2, 1, s->interlaced_dct);
701                     put_bits(pb2, 1, 0);
702                 }
703
704                 if(interleaved_stats){
705                     s->misc_bits+= get_bits_diff(s);
706                 }
707
708                 /* motion vectors: 16x16 mode */
709                 ff_h263_pred_motion(s, 0, 0, &pred_x, &pred_y);
710
711                 ff_h263_encode_motion_vector(s, motion_x - pred_x,
712                                                 motion_y - pred_y, s->f_code);
713             }else if(s->mv_type==MV_TYPE_FIELD){
714                 if(s->dquant) cbpc+= 8;
715                 put_bits(&s->pb,
716                         ff_h263_inter_MCBPC_bits[cbpc],
717                         ff_h263_inter_MCBPC_code[cbpc]);
718
719                 put_bits(pb2, ff_h263_cbpy_tab[cbpy][1], ff_h263_cbpy_tab[cbpy][0]);
720                 if(s->dquant)
721                     put_bits(pb2, 2, dquant_code[s->dquant+2]);
722
723                 av_assert2(!s->progressive_sequence);
724                 if(cbp)
725                     put_bits(pb2, 1, s->interlaced_dct);
726                 put_bits(pb2, 1, 1);
727
728                 if(interleaved_stats){
729                     s->misc_bits+= get_bits_diff(s);
730                 }
731
732                 /* motion vectors: 16x8 interlaced mode */
733                 ff_h263_pred_motion(s, 0, 0, &pred_x, &pred_y);
734                 pred_y /=2;
735
736                 put_bits(&s->pb, 1, s->field_select[0][0]);
737                 put_bits(&s->pb, 1, s->field_select[0][1]);
738
739                 ff_h263_encode_motion_vector(s, s->mv[0][0][0] - pred_x,
740                                                 s->mv[0][0][1] - pred_y, s->f_code);
741                 ff_h263_encode_motion_vector(s, s->mv[0][1][0] - pred_x,
742                                                 s->mv[0][1][1] - pred_y, s->f_code);
743             }else{
744                 av_assert2(s->mv_type==MV_TYPE_8X8);
745                 put_bits(&s->pb,
746                         ff_h263_inter_MCBPC_bits[cbpc+16],
747                         ff_h263_inter_MCBPC_code[cbpc+16]);
748                 put_bits(pb2, ff_h263_cbpy_tab[cbpy][1], ff_h263_cbpy_tab[cbpy][0]);
749
750                 if(!s->progressive_sequence){
751                     if(cbp)
752                         put_bits(pb2, 1, s->interlaced_dct);
753                 }
754
755                 if(interleaved_stats){
756                     s->misc_bits+= get_bits_diff(s);
757                 }
758
759                 for(i=0; i<4; i++){
760                     /* motion vectors: 8x8 mode*/
761                     ff_h263_pred_motion(s, i, 0, &pred_x, &pred_y);
762
763                     ff_h263_encode_motion_vector(s, s->current_picture.motion_val[0][ s->block_index[i] ][0] - pred_x,
764                                                     s->current_picture.motion_val[0][ s->block_index[i] ][1] - pred_y, s->f_code);
765                 }
766             }
767
768             if(interleaved_stats){
769                 s->mv_bits+= get_bits_diff(s);
770             }
771
772             mpeg4_encode_blocks(s, block, NULL, NULL, NULL, tex_pb);
773
774             if(interleaved_stats){
775                 s->p_tex_bits+= get_bits_diff(s);
776             }
777             s->f_count++;
778         }
779     } else {
780         int cbp;
781         int dc_diff[6];   //dc values with the dc prediction subtracted
782         int dir[6];  //prediction direction
783         int zigzag_last_index[6];
784         uint8_t *scan_table[6];
785         int i;
786
787         for(i=0; i<6; i++){
788             dc_diff[i]= ff_mpeg4_pred_dc(s, i, block[i][0], &dir[i], 1);
789         }
790
791         if(s->flags & CODEC_FLAG_AC_PRED){
792             s->ac_pred= decide_ac_pred(s, block, dir, scan_table, zigzag_last_index);
793         }else{
794             for(i=0; i<6; i++)
795                 scan_table[i]= s->intra_scantable.permutated;
796         }
797
798         /* compute cbp */
799         cbp = 0;
800         for (i = 0; i < 6; i++) {
801             if (s->block_last_index[i] >= 1)
802                 cbp |= 1 << (5 - i);
803         }
804
805         cbpc = cbp & 3;
806         if (s->pict_type == AV_PICTURE_TYPE_I) {
807             if(s->dquant) cbpc+=4;
808             put_bits(&s->pb,
809                 ff_h263_intra_MCBPC_bits[cbpc],
810                 ff_h263_intra_MCBPC_code[cbpc]);
811         } else {
812             if(s->dquant) cbpc+=8;
813             put_bits(&s->pb, 1, 0);     /* mb coded */
814             put_bits(&s->pb,
815                 ff_h263_inter_MCBPC_bits[cbpc + 4],
816                 ff_h263_inter_MCBPC_code[cbpc + 4]);
817         }
818         put_bits(pb2, 1, s->ac_pred);
819         cbpy = cbp >> 2;
820         put_bits(pb2, ff_h263_cbpy_tab[cbpy][1], ff_h263_cbpy_tab[cbpy][0]);
821         if(s->dquant)
822             put_bits(dc_pb, 2, dquant_code[s->dquant+2]);
823
824         if(!s->progressive_sequence){
825             put_bits(dc_pb, 1, s->interlaced_dct);
826         }
827
828         if(interleaved_stats){
829             s->misc_bits+= get_bits_diff(s);
830         }
831
832         mpeg4_encode_blocks(s, block, dc_diff, scan_table, dc_pb, tex_pb);
833
834         if(interleaved_stats){
835             s->i_tex_bits+= get_bits_diff(s);
836         }
837         s->i_count++;
838
839         /* restore ac coeffs & last_index stuff if we messed them up with the prediction */
840         if(s->ac_pred)
841             restore_ac_coeffs(s, block, dir, scan_table, zigzag_last_index);
842     }
843 }
844
845 /**
846  * add mpeg4 stuffing bits (01...1)
847  */
848 void ff_mpeg4_stuffing(PutBitContext * pbc)
849 {
850     int length;
851     put_bits(pbc, 1, 0);
852     length= (-put_bits_count(pbc))&7;
853     if(length) put_bits(pbc, length, (1<<length)-1);
854 }
855
856 /* must be called before writing the header */
857 void ff_set_mpeg4_time(MpegEncContext * s){
858     if(s->pict_type==AV_PICTURE_TYPE_B){
859         ff_mpeg4_init_direct_mv(s);
860     }else{
861         s->last_time_base= s->time_base;
862         s->time_base= FFUDIV(s->time, s->avctx->time_base.den);
863     }
864 }
865
866 static void mpeg4_encode_gop_header(MpegEncContext * s){
867     int hours, minutes, seconds;
868     int64_t time;
869
870     put_bits(&s->pb, 16, 0);
871     put_bits(&s->pb, 16, GOP_STARTCODE);
872
873     time = s->current_picture_ptr->f.pts;
874     if(s->reordered_input_picture[1])
875         time = FFMIN(time, s->reordered_input_picture[1]->f.pts);
876     time= time*s->avctx->time_base.num;
877     s->last_time_base= FFUDIV(time, s->avctx->time_base.den);
878
879     seconds= FFUDIV(time, s->avctx->time_base.den);
880     minutes= FFUDIV(seconds, 60); seconds = FFUMOD(seconds, 60);
881     hours  = FFUDIV(minutes, 60); minutes = FFUMOD(minutes, 60);
882     hours  = FFUMOD(hours  , 24);
883
884     put_bits(&s->pb, 5, hours);
885     put_bits(&s->pb, 6, minutes);
886     put_bits(&s->pb, 1, 1);
887     put_bits(&s->pb, 6, seconds);
888
889     put_bits(&s->pb, 1, !!(s->flags&CODEC_FLAG_CLOSED_GOP));
890     put_bits(&s->pb, 1, 0); //broken link == NO
891
892     ff_mpeg4_stuffing(&s->pb);
893 }
894
895 static void mpeg4_encode_visual_object_header(MpegEncContext * s){
896     int profile_and_level_indication;
897     int vo_ver_id;
898
899     if(s->avctx->profile != FF_PROFILE_UNKNOWN){
900         profile_and_level_indication = s->avctx->profile << 4;
901     }else if(s->max_b_frames || s->quarter_sample){
902         profile_and_level_indication= 0xF0; // adv simple
903     }else{
904         profile_and_level_indication= 0x00; // simple
905     }
906
907     if(s->avctx->level != FF_LEVEL_UNKNOWN){
908         profile_and_level_indication |= s->avctx->level;
909     }else{
910         profile_and_level_indication |= 1; //level 1
911     }
912
913     if(profile_and_level_indication>>4 == 0xF){
914         vo_ver_id= 5;
915     }else{
916         vo_ver_id= 1;
917     }
918
919     //FIXME levels
920
921     put_bits(&s->pb, 16, 0);
922     put_bits(&s->pb, 16, VOS_STARTCODE);
923
924     put_bits(&s->pb, 8, profile_and_level_indication);
925
926     put_bits(&s->pb, 16, 0);
927     put_bits(&s->pb, 16, VISUAL_OBJ_STARTCODE);
928
929     put_bits(&s->pb, 1, 1);
930         put_bits(&s->pb, 4, vo_ver_id);
931         put_bits(&s->pb, 3, 1); //priority
932
933     put_bits(&s->pb, 4, 1); //visual obj type== video obj
934
935     put_bits(&s->pb, 1, 0); //video signal type == no clue //FIXME
936
937     ff_mpeg4_stuffing(&s->pb);
938 }
939
940 static void mpeg4_encode_vol_header(MpegEncContext * s, int vo_number, int vol_number)
941 {
942     int vo_ver_id;
943
944     if (!CONFIG_MPEG4_ENCODER)  return;
945
946     if(s->max_b_frames || s->quarter_sample){
947         vo_ver_id= 5;
948         s->vo_type= ADV_SIMPLE_VO_TYPE;
949     }else{
950         vo_ver_id= 1;
951         s->vo_type= SIMPLE_VO_TYPE;
952     }
953
954     put_bits(&s->pb, 16, 0);
955     put_bits(&s->pb, 16, 0x100 + vo_number);        /* video obj */
956     put_bits(&s->pb, 16, 0);
957     put_bits(&s->pb, 16, 0x120 + vol_number);       /* video obj layer */
958
959     put_bits(&s->pb, 1, 0);             /* random access vol */
960     put_bits(&s->pb, 8, s->vo_type);    /* video obj type indication */
961     if(s->workaround_bugs & FF_BUG_MS) {
962         put_bits(&s->pb, 1, 0);         /* is obj layer id= no */
963     } else {
964         put_bits(&s->pb, 1, 1);         /* is obj layer id= yes */
965         put_bits(&s->pb, 4, vo_ver_id); /* is obj layer ver id */
966         put_bits(&s->pb, 3, 1);         /* is obj layer priority */
967     }
968
969     s->aspect_ratio_info= ff_h263_aspect_to_info(s->avctx->sample_aspect_ratio);
970
971     put_bits(&s->pb, 4, s->aspect_ratio_info);/* aspect ratio info */
972     if (s->aspect_ratio_info == FF_ASPECT_EXTENDED){
973         av_reduce(&s->avctx->sample_aspect_ratio.num, &s->avctx->sample_aspect_ratio.den,
974                    s->avctx->sample_aspect_ratio.num,  s->avctx->sample_aspect_ratio.den, 255);
975         put_bits(&s->pb, 8, s->avctx->sample_aspect_ratio.num);
976         put_bits(&s->pb, 8, s->avctx->sample_aspect_ratio.den);
977     }
978
979     if(s->workaround_bugs & FF_BUG_MS) { //
980         put_bits(&s->pb, 1, 0);         /* vol control parameters= no @@@ */
981     } else {
982         put_bits(&s->pb, 1, 1);         /* vol control parameters= yes */
983         put_bits(&s->pb, 2, 1);         /* chroma format YUV 420/YV12 */
984         put_bits(&s->pb, 1, s->low_delay);
985         put_bits(&s->pb, 1, 0);         /* vbv parameters= no */
986     }
987
988     put_bits(&s->pb, 2, RECT_SHAPE);    /* vol shape= rectangle */
989     put_bits(&s->pb, 1, 1);             /* marker bit */
990
991     put_bits(&s->pb, 16, s->avctx->time_base.den);
992     if (s->time_increment_bits < 1)
993         s->time_increment_bits = 1;
994     put_bits(&s->pb, 1, 1);             /* marker bit */
995     put_bits(&s->pb, 1, 0);             /* fixed vop rate=no */
996     put_bits(&s->pb, 1, 1);             /* marker bit */
997     put_bits(&s->pb, 13, s->width);     /* vol width */
998     put_bits(&s->pb, 1, 1);             /* marker bit */
999     put_bits(&s->pb, 13, s->height);    /* vol height */
1000     put_bits(&s->pb, 1, 1);             /* marker bit */
1001     put_bits(&s->pb, 1, s->progressive_sequence ? 0 : 1);
1002     put_bits(&s->pb, 1, 1);             /* obmc disable */
1003     if (vo_ver_id == 1) {
1004         put_bits(&s->pb, 1, s->vol_sprite_usage);       /* sprite enable */
1005     }else{
1006         put_bits(&s->pb, 2, s->vol_sprite_usage);       /* sprite enable */
1007     }
1008
1009     put_bits(&s->pb, 1, 0);             /* not 8 bit == false */
1010     put_bits(&s->pb, 1, s->mpeg_quant); /* quant type= (0=h263 style)*/
1011
1012     if(s->mpeg_quant){
1013         ff_write_quant_matrix(&s->pb, s->avctx->intra_matrix);
1014         ff_write_quant_matrix(&s->pb, s->avctx->inter_matrix);
1015     }
1016
1017     if (vo_ver_id != 1)
1018         put_bits(&s->pb, 1, s->quarter_sample);
1019     put_bits(&s->pb, 1, 1);             /* complexity estimation disable */
1020     s->resync_marker= s->rtp_mode;
1021     put_bits(&s->pb, 1, s->resync_marker ? 0 : 1);/* resync marker disable */
1022     put_bits(&s->pb, 1, s->data_partitioning ? 1 : 0);
1023     if(s->data_partitioning){
1024         put_bits(&s->pb, 1, 0);         /* no rvlc */
1025     }
1026
1027     if (vo_ver_id != 1){
1028         put_bits(&s->pb, 1, 0);         /* newpred */
1029         put_bits(&s->pb, 1, 0);         /* reduced res vop */
1030     }
1031     put_bits(&s->pb, 1, 0);             /* scalability */
1032
1033     ff_mpeg4_stuffing(&s->pb);
1034
1035     /* user data */
1036     if(!(s->flags & CODEC_FLAG_BITEXACT)){
1037         put_bits(&s->pb, 16, 0);
1038         put_bits(&s->pb, 16, 0x1B2);    /* user_data */
1039         avpriv_put_string(&s->pb, LIBAVCODEC_IDENT, 0);
1040     }
1041 }
1042
1043 /* write mpeg4 VOP header */
1044 void ff_mpeg4_encode_picture_header(MpegEncContext * s, int picture_number)
1045 {
1046     int time_incr;
1047     int time_div, time_mod;
1048
1049     if(s->pict_type==AV_PICTURE_TYPE_I){
1050         if(!(s->flags&CODEC_FLAG_GLOBAL_HEADER)){
1051             if(s->strict_std_compliance < FF_COMPLIANCE_VERY_STRICT) //HACK, the reference sw is buggy
1052                 mpeg4_encode_visual_object_header(s);
1053             if(s->strict_std_compliance < FF_COMPLIANCE_VERY_STRICT || picture_number==0) //HACK, the reference sw is buggy
1054                 mpeg4_encode_vol_header(s, 0, 0);
1055         }
1056         if(!(s->workaround_bugs & FF_BUG_MS))
1057             mpeg4_encode_gop_header(s);
1058     }
1059
1060     s->partitioned_frame= s->data_partitioning && s->pict_type!=AV_PICTURE_TYPE_B;
1061
1062     put_bits(&s->pb, 16, 0);                /* vop header */
1063     put_bits(&s->pb, 16, VOP_STARTCODE);    /* vop header */
1064     put_bits(&s->pb, 2, s->pict_type - 1);  /* pict type: I = 0 , P = 1 */
1065
1066     time_div= FFUDIV(s->time, s->avctx->time_base.den);
1067     time_mod= FFUMOD(s->time, s->avctx->time_base.den);
1068     time_incr= time_div - s->last_time_base;
1069     av_assert0(time_incr >= 0);
1070     while(time_incr--)
1071         put_bits(&s->pb, 1, 1);
1072
1073     put_bits(&s->pb, 1, 0);
1074
1075     put_bits(&s->pb, 1, 1);                             /* marker */
1076     put_bits(&s->pb, s->time_increment_bits, time_mod); /* time increment */
1077     put_bits(&s->pb, 1, 1);                             /* marker */
1078     put_bits(&s->pb, 1, 1);                             /* vop coded */
1079     if (    s->pict_type == AV_PICTURE_TYPE_P
1080         || (s->pict_type == AV_PICTURE_TYPE_S && s->vol_sprite_usage==GMC_SPRITE)) {
1081         put_bits(&s->pb, 1, s->no_rounding);    /* rounding type */
1082     }
1083     put_bits(&s->pb, 3, 0);     /* intra dc VLC threshold */
1084     if(!s->progressive_sequence){
1085          put_bits(&s->pb, 1, s->current_picture_ptr->f.top_field_first);
1086          put_bits(&s->pb, 1, s->alternate_scan);
1087     }
1088     //FIXME sprite stuff
1089
1090     put_bits(&s->pb, 5, s->qscale);
1091
1092     if (s->pict_type != AV_PICTURE_TYPE_I)
1093         put_bits(&s->pb, 3, s->f_code); /* fcode_for */
1094     if (s->pict_type == AV_PICTURE_TYPE_B)
1095         put_bits(&s->pb, 3, s->b_code); /* fcode_back */
1096 }
1097
1098
1099 static av_cold void init_uni_dc_tab(void)
1100 {
1101     int level, uni_code, uni_len;
1102
1103     for(level=-256; level<256; level++){
1104         int size, v, l;
1105         /* find number of bits */
1106         size = 0;
1107         v = abs(level);
1108         while (v) {
1109             v >>= 1;
1110             size++;
1111         }
1112
1113         if (level < 0)
1114             l= (-level) ^ ((1 << size) - 1);
1115         else
1116             l= level;
1117
1118         /* luminance */
1119         uni_code= ff_mpeg4_DCtab_lum[size][0];
1120         uni_len = ff_mpeg4_DCtab_lum[size][1];
1121
1122         if (size > 0) {
1123             uni_code<<=size; uni_code|=l;
1124             uni_len+=size;
1125             if (size > 8){
1126                 uni_code<<=1; uni_code|=1;
1127                 uni_len++;
1128             }
1129         }
1130         uni_DCtab_lum_bits[level+256]= uni_code;
1131         uni_DCtab_lum_len [level+256]= uni_len;
1132
1133         /* chrominance */
1134         uni_code= ff_mpeg4_DCtab_chrom[size][0];
1135         uni_len = ff_mpeg4_DCtab_chrom[size][1];
1136
1137         if (size > 0) {
1138             uni_code<<=size; uni_code|=l;
1139             uni_len+=size;
1140             if (size > 8){
1141                 uni_code<<=1; uni_code|=1;
1142                 uni_len++;
1143             }
1144         }
1145         uni_DCtab_chrom_bits[level+256]= uni_code;
1146         uni_DCtab_chrom_len [level+256]= uni_len;
1147
1148     }
1149 }
1150
1151 static av_cold void init_uni_mpeg4_rl_tab(RLTable *rl, uint32_t *bits_tab,
1152                                           uint8_t *len_tab)
1153 {
1154     int slevel, run, last;
1155
1156     av_assert0(MAX_LEVEL >= 64);
1157     av_assert0(MAX_RUN   >= 63);
1158
1159     for(slevel=-64; slevel<64; slevel++){
1160         if(slevel==0) continue;
1161         for(run=0; run<64; run++){
1162             for(last=0; last<=1; last++){
1163                 const int index= UNI_MPEG4_ENC_INDEX(last, run, slevel+64);
1164                 int level= slevel < 0 ? -slevel : slevel;
1165                 int sign= slevel < 0 ? 1 : 0;
1166                 int bits, len, code;
1167                 int level1, run1;
1168
1169                 len_tab[index]= 100;
1170
1171                 /* ESC0 */
1172                 code= get_rl_index(rl, last, run, level);
1173                 bits= rl->table_vlc[code][0];
1174                 len=  rl->table_vlc[code][1];
1175                 bits=bits*2+sign; len++;
1176
1177                 if(code!=rl->n && len < len_tab[index]){
1178                     bits_tab[index]= bits;
1179                     len_tab [index]= len;
1180                 }
1181                 /* ESC1 */
1182                 bits= rl->table_vlc[rl->n][0];
1183                 len=  rl->table_vlc[rl->n][1];
1184                 bits=bits*2;    len++; //esc1
1185                 level1= level - rl->max_level[last][run];
1186                 if(level1>0){
1187                     code= get_rl_index(rl, last, run, level1);
1188                     bits<<= rl->table_vlc[code][1];
1189                     len  += rl->table_vlc[code][1];
1190                     bits += rl->table_vlc[code][0];
1191                     bits=bits*2+sign; len++;
1192
1193                     if(code!=rl->n && len < len_tab[index]){
1194                         bits_tab[index]= bits;
1195                         len_tab [index]= len;
1196                     }
1197                 }
1198                 /* ESC2 */
1199                 bits= rl->table_vlc[rl->n][0];
1200                 len=  rl->table_vlc[rl->n][1];
1201                 bits=bits*4+2;    len+=2; //esc2
1202                 run1 = run - rl->max_run[last][level] - 1;
1203                 if(run1>=0){
1204                     code= get_rl_index(rl, last, run1, level);
1205                     bits<<= rl->table_vlc[code][1];
1206                     len  += rl->table_vlc[code][1];
1207                     bits += rl->table_vlc[code][0];
1208                     bits=bits*2+sign; len++;
1209
1210                     if(code!=rl->n && len < len_tab[index]){
1211                         bits_tab[index]= bits;
1212                         len_tab [index]= len;
1213                     }
1214                 }
1215                 /* ESC3 */
1216                 bits= rl->table_vlc[rl->n][0];
1217                 len = rl->table_vlc[rl->n][1];
1218                 bits=bits*4+3;    len+=2; //esc3
1219                 bits=bits*2+last; len++;
1220                 bits=bits*64+run; len+=6;
1221                 bits=bits*2+1;    len++;  //marker
1222                 bits=bits*4096+(slevel&0xfff); len+=12;
1223                 bits=bits*2+1;    len++;  //marker
1224
1225                 if(len < len_tab[index]){
1226                     bits_tab[index]= bits;
1227                     len_tab [index]= len;
1228                 }
1229             }
1230         }
1231     }
1232 }
1233
1234 static av_cold int encode_init(AVCodecContext *avctx)
1235 {
1236     MpegEncContext *s = avctx->priv_data;
1237     int ret;
1238     static int done = 0;
1239
1240     if (avctx->width >= (1<<13) || avctx->height >= (1<<13)) {
1241         av_log(avctx, AV_LOG_ERROR, "dimensions too large for MPEG-4\n");
1242         return AVERROR(EINVAL);
1243     }
1244
1245     if((ret=ff_MPV_encode_init(avctx)) < 0)
1246         return ret;
1247
1248     if (!done) {
1249         done = 1;
1250
1251         init_uni_dc_tab();
1252
1253         ff_init_rl(&ff_mpeg4_rl_intra, ff_mpeg4_static_rl_table_store[0]);
1254
1255         init_uni_mpeg4_rl_tab(&ff_mpeg4_rl_intra, uni_mpeg4_intra_rl_bits, uni_mpeg4_intra_rl_len);
1256         init_uni_mpeg4_rl_tab(&ff_h263_rl_inter, uni_mpeg4_inter_rl_bits, uni_mpeg4_inter_rl_len);
1257     }
1258
1259     s->min_qcoeff= -2048;
1260     s->max_qcoeff=  2047;
1261     s->intra_ac_vlc_length     = uni_mpeg4_intra_rl_len;
1262     s->intra_ac_vlc_last_length= uni_mpeg4_intra_rl_len + 128*64;
1263     s->inter_ac_vlc_length     = uni_mpeg4_inter_rl_len;
1264     s->inter_ac_vlc_last_length= uni_mpeg4_inter_rl_len + 128*64;
1265     s->luma_dc_vlc_length= uni_DCtab_lum_len;
1266     s->ac_esc_length= 7+2+1+6+1+12+1;
1267     s->y_dc_scale_table= ff_mpeg4_y_dc_scale_table;
1268     s->c_dc_scale_table= ff_mpeg4_c_dc_scale_table;
1269
1270     if(s->flags & CODEC_FLAG_GLOBAL_HEADER){
1271
1272         s->avctx->extradata= av_malloc(1024);
1273         init_put_bits(&s->pb, s->avctx->extradata, 1024);
1274
1275         if(!(s->workaround_bugs & FF_BUG_MS))
1276             mpeg4_encode_visual_object_header(s);
1277         mpeg4_encode_vol_header(s, 0, 0);
1278
1279 //            ff_mpeg4_stuffing(&s->pb); ?
1280         flush_put_bits(&s->pb);
1281         s->avctx->extradata_size= (put_bits_count(&s->pb)+7)>>3;
1282     }
1283     return 0;
1284 }
1285
1286 void ff_mpeg4_init_partitions(MpegEncContext *s)
1287 {
1288     uint8_t *start= put_bits_ptr(&s->pb);
1289     uint8_t *end= s->pb.buf_end;
1290     int size= end - start;
1291     int pb_size = (((intptr_t)start + size/3)&(~3)) - (intptr_t)start;
1292     int tex_size= (size - 2*pb_size)&(~3);
1293
1294     set_put_bits_buffer_size(&s->pb, pb_size);
1295     init_put_bits(&s->tex_pb, start + pb_size           , tex_size);
1296     init_put_bits(&s->pb2   , start + pb_size + tex_size, pb_size);
1297 }
1298
1299 void ff_mpeg4_merge_partitions(MpegEncContext *s)
1300 {
1301     const int pb2_len   = put_bits_count(&s->pb2   );
1302     const int tex_pb_len= put_bits_count(&s->tex_pb);
1303     const int bits= put_bits_count(&s->pb);
1304
1305     if(s->pict_type==AV_PICTURE_TYPE_I){
1306         put_bits(&s->pb, 19, DC_MARKER);
1307         s->misc_bits+=19 + pb2_len + bits - s->last_bits;
1308         s->i_tex_bits+= tex_pb_len;
1309     }else{
1310         put_bits(&s->pb, 17, MOTION_MARKER);
1311         s->misc_bits+=17 + pb2_len;
1312         s->mv_bits+= bits - s->last_bits;
1313         s->p_tex_bits+= tex_pb_len;
1314     }
1315
1316     flush_put_bits(&s->pb2);
1317     flush_put_bits(&s->tex_pb);
1318
1319     set_put_bits_buffer_size(&s->pb, s->pb2.buf_end - s->pb.buf);
1320     avpriv_copy_bits(&s->pb, s->pb2.buf   , pb2_len);
1321     avpriv_copy_bits(&s->pb, s->tex_pb.buf, tex_pb_len);
1322     s->last_bits= put_bits_count(&s->pb);
1323 }
1324
1325
1326 void ff_mpeg4_encode_video_packet_header(MpegEncContext *s)
1327 {
1328     int mb_num_bits= av_log2(s->mb_num - 1) + 1;
1329
1330     put_bits(&s->pb, ff_mpeg4_get_video_packet_prefix_length(s), 0);
1331     put_bits(&s->pb, 1, 1);
1332
1333     put_bits(&s->pb, mb_num_bits, s->mb_x + s->mb_y*s->mb_width);
1334     put_bits(&s->pb, s->quant_precision, s->qscale);
1335     put_bits(&s->pb, 1, 0); /* no HEC */
1336 }
1337
1338 #define OFFSET(x) offsetof(MpegEncContext, x)
1339 #define VE AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_ENCODING_PARAM
1340 static const AVOption options[] = {
1341     { "data_partitioning",       "Use data partitioning.",      OFFSET(data_partitioning), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, 1, VE },
1342     { "alternate_scan",          "Enable alternate scantable.", OFFSET(alternate_scan),    AV_OPT_TYPE_INT, { .i64 = 0 }, 0, 1, VE },
1343     FF_MPV_COMMON_OPTS
1344     { NULL },
1345 };
1346
1347 static const AVClass mpeg4enc_class = {
1348     .class_name = "MPEG4 encoder",
1349     .item_name  = av_default_item_name,
1350     .option     = options,
1351     .version    = LIBAVUTIL_VERSION_INT,
1352 };
1353
1354 AVCodec ff_mpeg4_encoder = {
1355     .name           = "mpeg4",
1356     .type           = AVMEDIA_TYPE_VIDEO,
1357     .id             = AV_CODEC_ID_MPEG4,
1358     .priv_data_size = sizeof(MpegEncContext),
1359     .init           = encode_init,
1360     .encode2        = ff_MPV_encode_picture,
1361     .close          = ff_MPV_encode_end,
1362     .pix_fmts       = (const enum AVPixelFormat[]){ AV_PIX_FMT_YUV420P, AV_PIX_FMT_NONE },
1363     .capabilities   = CODEC_CAP_DELAY | CODEC_CAP_SLICE_THREADS,
1364     .long_name      = NULL_IF_CONFIG_SMALL("MPEG-4 part 2"),
1365     .priv_class     = &mpeg4enc_class,
1366 };