1 /*
2 * MPEG4 encoder.
3 * Copyright (c) 2000,2001 Fabrice Bellard
4 * Copyright (c) 2002-2010 Michael Niedermayer <michaelni@gmx.at>
5 *
6 * This file is part of FFmpeg.
7 *
8 * FFmpeg is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; either
11 * version 2.1 of the License, or (at your option) any later version.
12 *
13 * FFmpeg is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
17 *
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with FFmpeg; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21 */
22
29
30 //The uni_DCtab_* tables below contain unified bits+length tables to encode DC
31 //differences in mpeg4. Unified in the sense that the specification specifies
32 //this encoding in several steps.
37
38 //unified encoding tables for run length encoding of coefficients
39 //unified in the sense that the specification specifies the encoding in several steps.
44 //#define UNI_MPEG4_ENC_INDEX(last,run,level) ((last)*128 + (run)*256 + (level))
45 //#define UNI_MPEG4_ENC_INDEX(last,run,level) ((last)*128*64 + (run) + (level)*64)
46 #define UNI_MPEG4_ENC_INDEX(last,run,level) ((last)*128*64 + (run)*128 + (level))
47
48 /* mpeg4
49 inter
50 max level: 24/6
51 max run: 53/63
52
53 intra
54 max level: 53/16
55 max run: 29/41
56 */
57
58
59 /**
60 * Return the number of bits that encoding the 8x8 block in block would need.
61 * @param[in] block_last_index last index in scantable order that refers to a non zero element in block.
62 */
64 int last=0;
65 int j;
66 int rate=0;
67
68 for(j=1; j<=block_last_index; j++){
69 const int index= scantable[j];
71 if(level){
72 level+= 64;
73 if((level&(~127)) == 0){
76 }else
78
79 last= j;
80 }
81 }
82
83 return rate;
84 }
85
86
87 /**
88 * Restore the ac coefficients in block that have been changed by decide_ac_pred().
89 * This function also restores s->block_last_index.
90 * @param[in,out] block MB coefficients, these will be restored
91 * @param[in] dir ac prediction direction for each 8x8 block
92 * @param[out] st scantable for each 8x8 block
93 * @param[in] zigzag_last_index index referring to the last non zero coefficient in zigzag order
94 */
96 {
99
100 for(n=0; n<6; n++){
102
104 if(dir[n]){
105 /* top prediction */
106 for(i=1; i<8; i++){
108 }
109 }else{
110 /* left prediction */
111 for(i=1; i<8; i++){
113 }
114 }
115 }
116 }
117
118 /**
119 * Return the optimal value (0 or 1) for the ac_pred element for the given MB in mpeg4.
120 * This function will also update s->block_last_index and s->ac_val.
121 * @param[in,out] block MB coefficients, these will be updated if 1 is returned
122 * @param[in] dir ac prediction direction for each 8x8 block
123 * @param[out] st scantable for each 8x8 block
124 * @param[out] zigzag_last_index index referring to the last non zero coefficient in zigzag order
125 */
127 {
128 int score= 0;
131
133
134 for(n=0; n<6; n++){
135 int16_t *ac_val, *ac_val1;
136
138
140 ac_val1= ac_val;
141 if(dir[n]){
143 /* top prediction */
145 if(s->
mb_y==0 || s->
qscale == qscale_table[xy] || n==2 || n==3){
146 /* same qscale */
147 for(i=1; i<8; i++){
152 }
153 }else{
154 /* different qscale, we must rescale */
155 for(i=1; i<8; i++){
160 }
161 }
163 }else{
165 /* left prediction */
166 ac_val-= 16;
167 if(s->
mb_x==0 || s->
qscale == qscale_table[xy] || n==1 || n==3){
168 /* same qscale */
169 for(i=1; i<8; i++){
174 }
175 }else{
176 /* different qscale, we must rescale */
177 for(i=1; i<8; i++){
182 }
183 }
185 }
186
187 for(i=63; i>0; i--) //FIXME optimize
188 if(block[n][ st[n][i] ]) break;
190
192 }
193
194 if(score < 0){
195 return 1;
196 }else{
198 return 0;
199 }
200 }
201
202 /**
203 * modify mb_type & qscale so that encoding is actually possible in mpeg4
204 */
206 int i;
208
210
212 int odd=0;
213 /* ok, come on, this isn't funny anymore, there's more code for handling this mpeg4 mess than for the actual adaptive quantization */
214
215 for(i=0; i<s->
mb_num; i++){
217 odd += qscale_table[mb_xy]&1;
218 }
219
220 if(2*odd > s->
mb_num) odd=1;
221 else odd=0;
222
223 for(i=0; i<s->
mb_num; i++){
225 if((qscale_table[mb_xy]&1) != odd)
226 qscale_table[mb_xy]++;
227 if(qscale_table[mb_xy] > 31)
228 qscale_table[mb_xy]= 31;
229 }
230
231 for(i=1; i<s->
mb_num; i++){
235 }
236 }
237 }
238 }
239
240
241 /**
242 * Encode the dc value.
243 * @param n block index (0-3 are luma, 4-5 are chroma)
244 */
246 {
247 #if 1
248 /* DC will overflow if level is outside the [-255,255] range. */
249 level+=256;
250 if (n < 4) {
251 /* luminance */
253 } else {
254 /* chrominance */
256 }
257 #else
259 /* find number of bits */
260 size = 0;
261 v = abs(level);
262 while (v) {
263 v >>= 1;
264 size++;
265 }
266
267 if (n < 4) {
268 /* luminance */
270 } else {
271 /* chrominance */
273 }
274
275 /* encode remaining bits */
276 if (size > 0) {
277 if (level < 0)
278 level = (-
level) ^ ((1 << size) - 1);
280 if (size > 8)
282 }
283 #endif
284 }
285
287 if (n < 4) {
289 } else {
291 }
292 }
293
294 /**
295 * Encode an 8x8 block.
296 * @param n block index (0-3 are luma, 4-5 are chroma)
297 */
300 {
301 int i, last_non_zero;
302 uint32_t *bits_tab;
305
306 if (s->
mb_intra) {
//Note gcc (3.2.1 at least) will optimize this away
307 /* mpeg4 based DC predictor */
309 if(last_index<1) return;
310 i = 1;
313 } else {
314 if(last_index<0) return;
315 i = 0;
318 }
319
320 /* AC coefs */
321 last_non_zero = i - 1;
322 for (; i < last_index; i++) {
323 int level = block[ scan_table[i] ];
324 if (level) {
325 int run = i - last_non_zero - 1;
326 level+=64;
327 if((level&(~127)) == 0){
329 put_bits(ac_pb, len_tab[index], bits_tab[index]);
330 }else{ //ESC3
331 put_bits(ac_pb, 7+2+1+6+1+12+1, (3<<23)+(3<<21)+(0<<20)+(run<<14)+(1<<13)+(((level-64)&0xfff)<<1)+1);
332 }
333 last_non_zero = i;
334 }
335 }
336 /*if(i<=last_index)*/{
337 int level = block[ scan_table[i] ];
338 int run = i - last_non_zero - 1;
339 level+=64;
340 if((level&(~127)) == 0){
342 put_bits(ac_pb, len_tab[index], bits_tab[index]);
343 }else{ //ESC3
344 put_bits(ac_pb, 7+2+1+6+1+12+1, (3<<23)+(3<<21)+(1<<20)+(run<<14)+(1<<13)+(((level-64)&0xfff)<<1)+1);
345 }
346 }
347 }
348
351 {
352 int i, last_non_zero;
356
357 if (s->
mb_intra) {
//Note gcc (3.2.1 at least) will optimize this away
358 /* mpeg4 based DC predictor */
360 if(last_index<1)
return len;
361 i = 1;
363 } else {
364 if(last_index<0) return 0;
365 i = 0;
367 }
368
369 /* AC coefs */
370 last_non_zero = i - 1;
371 for (; i < last_index; i++) {
372 int level = block[ scan_table[i] ];
373 if (level) {
374 int run = i - last_non_zero - 1;
375 level+=64;
376 if((level&(~127)) == 0){
378 len += len_tab[
index];
379 }else{ //ESC3
380 len += 7+2+1+6+1+12+1;
381 }
382 last_non_zero = i;
383 }
384 }
385 /*if(i<=last_index)*/{
386 int level = block[ scan_table[i] ];
387 int run = i - last_non_zero - 1;
388 level+=64;
389 if((level&(~127)) == 0){
391 len += len_tab[
index];
392 }else{ //ESC3
393 len += 7+2+1+6+1+12+1;
394 }
395 }
396
398 }
399
402 int i;
403
404 if(scan_table){
406 for (i = 0; i < 6; i++) {
408 }
409 }else{
410 /* encode each block */
411 for (i = 0; i < 6; i++) {
413 }
414 }
415 }else{
417 for (i = 0; i < 6; i++) {
419 }
420 }else{
421 /* encode each block */
422 for (i = 0; i < 6; i++) {
424 }
425 }
426 }
427 }
428
430 int motion_x, int motion_y, int mb_type)
431 {
432 int cbp = 0, i;
433
435 int score = 0;
437
438 for (i = 0; i < 6; i++)
441 cbp |= 1 << (5 - i);
442 }
443
444 if (cbp) {
445 int zero_score = -6;
446 if ((motion_x | motion_y | s->
dquant | mb_type) == 0)
447 zero_score -= 4; //2*MV + mb_type + cbp bit
448
449 zero_score *= lambda;
450 if (zero_score <= score)
451 cbp = 0;
452 }
453
454 for (i = 0; i < 6; i++) {
458 }
459 }
460 } else {
461 for (i = 0; i < 6; i++) {
463 cbp |= 1 << (5 - i);
464 }
465 }
466 return cbp;
467 }
468
469 //FIXME this is duplicated to h263.c
471
473 int16_t
block[6][64],
474 int motion_x, int motion_y)
475 {
476 int cbpc, cbpy, pred_x, pred_y;
481
483 int i, cbp;
484
486 static const int mb_type_table[8]= {-1, 3, 2, 1,-1,-1,-1, 0}; /* convert from mv_dir to type */
487 int mb_type= mb_type_table[s->
mv_dir];
488
490 for(i=0; i<2; i++){
495 }
496 }
497
501
502 /* nothing to do if this MB was skipped in the next P Frame */
511 // s->mb_skipped=1;
512
513 return;
514 }
515
516 cbp=
get_b_cbp(s, block, motion_x, motion_y, mb_type);
517
518 if ((cbp | motion_x | motion_y | mb_type) ==0) {
519 /* direct MB with MV={0,0} */
521
522 put_bits(&s->
pb, 1, 1);
/* mb not coded modb1=1 */
523
524 if(interleaved_stats){
527 }
529 return;
530 }
531
532 put_bits(&s->
pb, 1, 0);
/* mb coded modb1=0 */
533 put_bits(&s->
pb, 1, cbp ? 0 : 1);
/* modb2 */ //FIXME merge
534 put_bits(&s->
pb, mb_type+1, 1);
// this table is so simple that we don't need it :)
536
537 if(cbp && mb_type){
540 else
542 }else
544
546 if(cbp)
548 if(mb_type) // not direct mode
550 }
551
552 if(interleaved_stats){
554 }
555
556 if(mb_type == 0){
561 }else{
570 }
577 }
578 }else{
582 }
586 }
588 for(i=0; i<2; i++){
593 }
595 }
597 for(i=0; i<2; i++){
602 }
604 }
605 }
606 }
607
608 if(interleaved_stats){
610 }
611
613
614 if(interleaved_stats){
616 }
617
618 }else{ /* s->pict_type==AV_PICTURE_TYPE_B */
619 cbp=
get_p_cbp(s, block, motion_x, motion_y);
620
622 /* check if the B frames can skip it too, as we must skip it if we skip here
623 why didn't they just compress the skip-mb bits instead of reusing them ?! */
625 int i;
628
631
634
640
642 break;
643
647
649 int x1,y1;
652 diff=0;
653 for(y1=0; y1<ye; y1++){
654 for(x1=0; x1<xe; x1++){
656 }
657 }
658 diff= diff*256/(xe*ye);
659 }else{
661 }
662 if(diff>s->
qscale*70){
//FIXME check that 70 is optimal
664 break;
665 }
666 }
667 }else
669
671 /* skip macroblock */
673
674 if(interleaved_stats){
677 }
679
680 return;
681 }
682 }
683
685 cbpc = cbp & 3;
686 cbpy = cbp >> 2;
687 cbpy ^= 0xf;
693
697
699 if(cbp)
702 }
703
704 if(interleaved_stats){
706 }
707
708 /* motion vectors: 16x16 mode */
710
712 motion_y - pred_y, s->
f_code);
718
722
724 if(cbp)
727
728 if(interleaved_stats){
730 }
731
732 /* motion vectors: 16x8 interlaced mode */
734 pred_y /=2;
735
738
740 s->
mv[0][0][1] - pred_y, s->
f_code);
742 s->
mv[0][1][1] - pred_y, s->
f_code);
743 }else{
749
751 if(cbp)
753 }
754
755 if(interleaved_stats){
757 }
758
759 for(i=0; i<4; i++){
760 /* motion vectors: 8x8 mode*/
762
765 }
766 }
767
768 if(interleaved_stats){
770 }
771
773
774 if(interleaved_stats){
776 }
778 }
779 } else {
780 int cbp;
781 int dc_diff[6]; //dc values with the dc prediction subtracted
782 int dir[6]; //prediction direction
783 int zigzag_last_index[6];
785 int i;
786
787 for(i=0; i<6; i++){
789 }
790
793 }else{
794 for(i=0; i<6; i++)
796 }
797
798 /* compute cbp */
799 cbp = 0;
800 for (i = 0; i < 6; i++) {
802 cbp |= 1 << (5 - i);
803 }
804
805 cbpc = cbp & 3;
811 } else {
817 }
819 cbpy = cbp >> 2;
823
826 }
827
828 if(interleaved_stats){
830 }
831
833
834 if(interleaved_stats){
836 }
838
839 /* restore ac coeffs & last_index stuff if we messed them up with the prediction */
842 }
843 }
844
845 /**
846 * add mpeg4 stuffing bits (01...1)
847 */
849 {
853 if(length)
put_bits(pbc, length, (1<<length)-1);
854 }
855
856 /* must be called before writing the header */
860 }else{
863 }
864 }
865
867 int hours, minutes, seconds;
868 int64_t time;
869
872
878
880 minutes=
FFUDIV(seconds, 60); seconds =
FFUMOD(seconds, 60);
881 hours =
FFUDIV(minutes, 60); minutes =
FFUMOD(minutes, 60);
882 hours =
FFUMOD(hours , 24);
883
888
891
893 }
894
896 int profile_and_level_indication;
897 int vo_ver_id;
898
902 profile_and_level_indication= 0xF0; // adv simple
903 }else{
904 profile_and_level_indication= 0x00; // simple
905 }
906
908 profile_and_level_indication |= s->
avctx->
level;
909 }else{
910 profile_and_level_indication |= 1; //level 1
911 }
912
913 if(profile_and_level_indication>>4 == 0xF){
914 vo_ver_id= 5;
915 }else{
916 vo_ver_id= 1;
917 }
918
919 //FIXME levels
920
923
924 put_bits(&s->
pb, 8, profile_and_level_indication);
925
928
932
933 put_bits(&s->
pb, 4, 1);
//visual obj type== video obj
934
935 put_bits(&s->
pb, 1, 0);
//video signal type == no clue //FIXME
936
938 }
939
941 {
942 int vo_ver_id;
943
944 if (!CONFIG_MPEG4_ENCODER) return;
945
947 vo_ver_id= 5;
949 }else{
950 vo_ver_id= 1;
952 }
953
955 put_bits(&s->
pb, 16, 0x100 + vo_number);
/* video obj */
957 put_bits(&s->
pb, 16, 0x120 + vol_number);
/* video obj layer */
958
959 put_bits(&s->
pb, 1, 0);
/* random access vol */
962 put_bits(&s->
pb, 1, 0);
/* is obj layer id= no */
963 } else {
964 put_bits(&s->
pb, 1, 1);
/* is obj layer id= yes */
965 put_bits(&s->
pb, 4, vo_ver_id);
/* is obj layer ver id */
966 put_bits(&s->
pb, 3, 1);
/* is obj layer priority */
967 }
968
970
977 }
978
980 put_bits(&s->
pb, 1, 0);
/* vol control parameters= no @@@ */
981 } else {
982 put_bits(&s->
pb, 1, 1);
/* vol control parameters= yes */
983 put_bits(&s->
pb, 2, 1);
/* chroma format YUV 420/YV12 */
985 put_bits(&s->
pb, 1, 0);
/* vbv parameters= no */
986 }
987
990
995 put_bits(&s->
pb, 1, 0);
/* fixed vop rate=no */
1003 if (vo_ver_id == 1) {
1005 }else{
1007 }
1008
1009 put_bits(&s->
pb, 1, 0);
/* not 8 bit == false */
1011
1015 }
1016
1017 if (vo_ver_id != 1)
1019 put_bits(&s->
pb, 1, 1);
/* complexity estimation disable */
1025 }
1026
1027 if (vo_ver_id != 1){
1029 put_bits(&s->
pb, 1, 0);
/* reduced res vop */
1030 }
1032
1034
1035 /* user data */
1040 }
1041 }
1042
1043 /* write mpeg4 VOP header */
1045 {
1046 int time_incr;
1047 int time_div, time_mod;
1048
1055 }
1058 }
1059
1061
1065
1070 while(time_incr--)
1072
1074
1082 }
1083 put_bits(&s->
pb, 3, 0);
/* intra dc VLC threshold */
1087 }
1088 //FIXME sprite stuff
1089
1091
1096 }
1097
1098
1100 {
1101 int level, uni_code, uni_len;
1102
1103 for(level=-256; level<256; level++){
1105 /* find number of bits */
1106 size = 0;
1107 v = abs(level);
1108 while (v) {
1109 v >>= 1;
1110 size++;
1111 }
1112
1113 if (level < 0)
1114 l= (-
level) ^ ((1 << size) - 1);
1115 else
1117
1118 /* luminance */
1121
1122 if (size > 0) {
1123 uni_code<<=
size; uni_code|=l;
1125 if (size > 8){
1126 uni_code<<=1; uni_code|=1;
1127 uni_len++;
1128 }
1129 }
1132
1133 /* chrominance */
1136
1137 if (size > 0) {
1138 uni_code<<=
size; uni_code|=l;
1140 if (size > 8){
1141 uni_code<<=1; uni_code|=1;
1142 uni_len++;
1143 }
1144 }
1147
1148 }
1149 }
1150
1153 {
1154 int slevel,
run, last;
1155
1158
1159 for(slevel=-64; slevel<64; slevel++){
1160 if(slevel==0) continue;
1161 for(run=0; run<64; run++){
1162 for(last=0; last<=1; last++){
1164 int level= slevel < 0 ? -slevel : slevel;
1165 int sign= slevel < 0 ? 1 : 0;
1167 int level1, run1;
1168
1169 len_tab[
index]= 100;
1170
1171 /* ESC0 */
1175 bits=bits*2+sign; len++;
1176
1177 if(code!=rl->
n && len < len_tab[index]){
1180 }
1181 /* ESC1 */
1184 bits=bits*2; len++; //esc1
1186 if(level1>0){
1191 bits=bits*2+sign; len++;
1192
1193 if(code!=rl->
n && len < len_tab[index]){
1196 }
1197 }
1198 /* ESC2 */
1201 bits=bits*4+2; len+=2; //esc2
1203 if(run1>=0){
1208 bits=bits*2+sign; len++;
1209
1210 if(code!=rl->
n && len < len_tab[index]){
1213 }
1214 }
1215 /* ESC3 */
1218 bits=bits*4+3; len+=2; //esc3
1219 bits=bits*2+last; len++;
1220 bits=bits*64+
run; len+=6;
1221 bits=bits*2+1; len++; //marker
1222 bits=bits*4096+(slevel&0xfff); len+=12;
1223 bits=bits*2+1; len++; //marker
1224
1225 if(len < len_tab[index]){
1228 }
1229 }
1230 }
1231 }
1232 }
1233
1235 {
1238 static int done = 0;
1239
1240 if (avctx->
width >= (1<<13) || avctx->
height >= (1<<13)) {
1243 }
1244
1247
1248 if (!done) {
1249 done = 1;
1250
1252
1254
1257 }
1258
1269
1271
1274
1278
1279 // ff_mpeg4_stuffing(&s->pb); ?
1282 }
1283 return 0;
1284 }
1285
1287 {
1291 int pb_size = (((intptr_t)start + size/3)&(~3)) - (intptr_t)start;
1292 int tex_size= (size - 2*pb_size)&(~3);
1293
1297 }
1298
1300 {
1304
1309 }else{
1314 }
1315
1318
1323 }
1324
1325
1327 {
1329
1332
1336 }
1337
1338 #define OFFSET(x) offsetof(MpegEncContext, x)
1339 #define VE AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_ENCODING_PARAM
1341 {
"data_partitioning",
"Use data partitioning.",
OFFSET(data_partitioning),
AV_OPT_TYPE_INT, { .i64 = 0 }, 0, 1,
VE },
1342 {
"alternate_scan",
"Enable alternate scantable.",
OFFSET(alternate_scan),
AV_OPT_TYPE_INT, { .i64 = 0 }, 0, 1,
VE },
1344 { NULL },
1345 };
1346
1352 };
1353
1366 };