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