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