1 /*
2 * MSMPEG4 encoder backend
3 * Copyright (c) 2001 Fabrice Bellard
4 * Copyright (c) 2002-2004 Michael Niedermayer <michaelni@gmx.at>
5 *
6 * msmpeg4v1 & v2 stuff by Michael Niedermayer <michaelni@gmx.at>
7 *
8 * This file is part of FFmpeg.
9 *
10 * FFmpeg is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU Lesser General Public
12 * License as published by the Free Software Foundation; either
13 * version 2.1 of the License, or (at your option) any later version.
14 *
15 * FFmpeg is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 * Lesser General Public License for more details.
19 *
20 * You should have received a copy of the GNU Lesser General Public
21 * License along with FFmpeg; if not, write to the Free Software
22 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
23 */
24
25 /**
26 * @file
27 * MSMPEG4 encoder backend
28 */
29
30 #include <stdint.h>
31 #include <string.h>
32
33 #define NO_SLICE_THREADING_HERE
34
50
52
53 // The lowest 8 bits of each entry are length, the other bits are the code.
54 // The index of the (mx, my) entry is (mx * 64) + my.
56
57 /* build the table which associate a (x,y) motion vector to a vlc */
59 uint32_t mv_vector_table[4096],
60 unsigned escape_code, int escape_length)
61 {
62 for (
int i = 0;
i < 4096;
i++) {
63 // Initialize to the table to "escaped". This code is equivalent to
64 // the following double loop (with x and y ranging over 0..63):
65 // tab[x * 64 + y] = (esc_code << 20) | (x << 14) | (y << 8) | esc_length
66 mv_vector_table[
i] = (escape_code << 20) | (
i << 8) | escape_length;
67 }
68
70 int sym = mv_table[
i];
71 int len = mv_table_lens[
i];
72 int x = sym >> 8;
73 int y = sym & 0xFF;
74 // We ignore the escape value here and restore it after the loop.
75 mv_vector_table[(x << 6) | y] = (
code >> (24 -
len)) |
len;
77 }
78 mv_vector_table[0] = (escape_code << 20) | escape_length;
79 }
80
82 {
83 if (n == 0) {
85 } else {
87 }
88 }
89
92 {
95 int run_diff= intra ? 0 : 1;
96
100 int level1, run1;
101
103 if (level1 < 1)
104 goto esc2;
107 esc2:
110 goto esc3;
112 if (run1 < 0)
113 goto esc3;
116 esc3:
117 /* third escape */
119 } else {
120 /* second escape */
122 }
123 } else {
124 /* first escape */
126 }
127 } else {
129 }
131 }
132
134 {
139
143 for (int last = 0; last < 2; last++) {
145 }
146 }
147 }
148 }
149 }
150
152 {
155 int best = 0, best_size = INT_MAX;
156 int chroma_best = 0, best_chroma_size = INT_MAX;
157
160 int chroma_size=0;
162
165 chroma_size++;
166 }
170 int last;
171 const int last_size=
size + chroma_size;
172 for(last=0; last<2; last++){
176
180 }else{
184 }
185 }
186 if(last_size ==
size+chroma_size)
break;
187 }
188 }
192 }
193 if(chroma_size<best_chroma_size){
194 best_chroma_size= chroma_size;
196 }
197 }
198
200
202
205
210 else
212 }
213
214 }
215
216 /* write MSMPEG4 compatible frame header */
218 {
221
223
225
227
229 if (
s->c.msmpeg4_version <= MSMP4_V2) {
232 }
233
238 if (
s->c.msmpeg4_version == MSMP4_WMV1)
239 s->c.inter_intra_pred =
s->c.width *
s->c.height < 320*240 &&
243 s->c.inter_intra_pred,
s->c.width,
s->c.height);
244
246 s->slice_height =
s->c.mb_height/1;
247 put_bits(&
s->pb, 5, 0x16 +
s->c.mb_height /
s->slice_height);
248
249 if (
s->c.msmpeg4_version == MSMP4_WMV1) {
253 }
254
255 if (
s->c.msmpeg4_version > MSMP4_V2) {
259 }
260
262 }
263 } else {
265
268
269 if (
s->c.msmpeg4_version > MSMP4_V2) {
272
274
276 }
277 }
278
279 s->esc3_level_length = 0;
281
282 return 0;
283 }
284
286 {
288 unsigned fps;
289
290 if (
s->c.avctx->framerate.num > 0 &&
s->c.avctx->framerate.den > 0)
291 fps =
s->c.avctx->framerate.num /
s->c.avctx->framerate.den;
292 else {
293 fps =
s->c.avctx->time_base.den /
s->c.avctx->time_base.num;
294 }
295
297
299
300 if (
s->c.msmpeg4_version >= MSMP4_V3)
302 else
304 }
305
308 {
312
313 /* modulo encoding */
314 /* WARNING : you cannot reach all the MVs even with the modulo
315 encoding. This is a somewhat strange compromise they took !!! */
324
327
328 code = mv_vector_table[(
mx << 6) |
my];
330 }
331
333 {
334 if (
s->c.mb_x == 0) {
335 if (
s->slice_height && (
s->c.mb_y %
s->slice_height) == 0) {
336 if (
s->c.msmpeg4_version < MSMP4_WMV1)
338 s->c.first_slice_line = 1;
339 } else {
340 s->c.first_slice_line = 0;
341 }
342 }
343 }
344
346 {
348
350 /* zero vector; corresponds to ff_mvtab[0] */
352 } else {
353 bit_size =
s->f_code - 1;
354 range = 1 << bit_size;
359
361 sign = 0;
362 } else {
364 sign = 1;
365 }
369
371 if (bit_size > 0) {
373 }
374 }
375 }
376
379 int motion_x, int motion_y)
380 {
382 int cbp, coded_cbp,
i;
383 int pred_x, pred_y;
384
386
387 if (!
s->c.mb_intra) {
388 /* compute cbp */
389 cbp = 0;
390 for (
i = 0;
i < 6;
i++) {
391 if (
s->c.block_last_index[
i] >= 0)
393 }
395 /* skip macroblock */
399
400 return;
401 }
404
405 if (
s->c.msmpeg4_version <= MSMP4_V2) {
409 if((cbp&3) != 3) coded_cbp= cbp ^ 0x3C;
410 else coded_cbp= cbp;
411
415
417
421 }else{
425
427
428 /* motion vector */
431 motion_y - pred_y);
432 }
433
435
436 for (
i = 0;
i < 6;
i++) {
438 }
440 } else {
441 /* compute cbp */
442 cbp = 0;
443 for (
int i = 0;
i < 6;
i++) {
444 int val = (
s->c.block_last_index[
i] >= 1);
445 cbp |=
val << (5 -
i);
446 }
447 if (
s->c.msmpeg4_version <= MSMP4_V2) {
451 } else {
457 }
458 put_bits(&
s->pb, 1, 0);
/* no AC prediction yet */
462 }else{
464 /* compute coded_cbp; the 0x3 corresponds to chroma cbp;
465 * luma coded_cbp are set in the loop below */
466 coded_cbp = cbp & 0x3;
467 for (
int i = 0;
i < 4;
i++) {
468 uint8_t *coded_block;
470 int val = (
s->c.block_last_index[
i] >= 1);
473 coded_cbp |=
val << (5 -
i);
474 }
475
478 } else {
484 }
485 put_bits(&
s->pb, 1, 0);
/* no AC prediction yet */
486 if (
s->c.inter_intra_pred) {
487 s->c.h263_aic_dir = 0;
490 }
491 }
493
494 for (
i = 0;
i < 6;
i++) {
496 }
499 }
500 }
501
503 {
507
508 int16_t *dc_val;
510
511 /* update predictor */
512 if (n < 4) {
513 *dc_val =
level *
s->c.y_dc_scale;
514 } else {
515 *dc_val =
level *
s->c.c_dc_scale;
516 }
517
518 /* do the prediction */
520
521 if (
s->c.msmpeg4_version <= MSMP4_V2) {
522 if (n < 4) {
526 }else{
530 }
531 }else{
532 sign = 0;
535 sign = 1;
536 }
540
543
546
549 }
550 }
551 }
552
553 /* Encoding of a block; very similar to MPEG-4 except for a different
554 * escape coding (same as H.263) and more VLC tables. */
556 {
559 int last_non_zero, sign, slevel;
560 int code, run_diff, dc_pred_dir;
562 const uint8_t *scantable;
563
567 if (n < 4) {
569 } else {
571 }
572 run_diff =
s->c.msmpeg4_version >= MSMP4_WMV1;
573 scantable =
s->c.intra_scantable.permutated;
574 } else {
577 run_diff =
s->c.msmpeg4_version > MSMP4_V2;
578 scantable =
s->c.inter_scantable.permutated;
579 }
580
581 /* recalculate block_last_index for M$ wmv1 */
582 if (
s->c.msmpeg4_version >= MSMP4_WMV1 &&
s->c.block_last_index[n] > 0) {
583 for(last_index=63; last_index>=0; last_index--){
584 if(
block[scantable[last_index]])
break;
585 }
586 s->c.block_last_index[n] = last_index;
587 }else
588 last_index =
s->c.block_last_index[n];
589 /* AC coefs */
590 last_non_zero =
i - 1;
591 for (;
i <= last_index;
i++) {
595 run =
i - last_non_zero - 1;
596 last = (
i == last_index);
597 sign = 0;
600 sign = 1;
602 }
603
606 }
607
608 ms->
ac_stats[
s->c.mb_intra][n > 3][40][63][0]++;
//esc3 like
609
613 int level1, run1;
614
616 if (level1 < 1)
617 goto esc2;
620 esc2:
623 goto esc3;
625 if (run1 < 0)
626 goto esc3;
628 if (
s->c.msmpeg4_version == MSMP4_WMV1 &&
code == rl->
n)
629 goto esc3;
632 esc3:
633 /* third escape */
636 if (
s->c.msmpeg4_version >= MSMP4_WMV1) {
637 if (
s->esc3_level_length == 0) {
638 s->esc3_level_length = 8;
640 //ESCLVLSZ + ESCRUNSZ
643 else
645 }
649 }else{
652 }
653 } else {
654 /* second escape */
658 }
659 } else {
660 /* first escape */
664 }
665 } else {
667 }
669 }
670 }
671 }
672
674 {
677
679
680 if (
s->c.msmpeg4_version <= MSMP4_WMV1) {
683 }
684
685 if (
s->c.msmpeg4_version >= MSMP4_WMV1) {
686 s->min_qcoeff = -255;
688 }
689
690 /* init various encoding tables */
692 }
693
695 .
p.
name =
"msmpeg4v2",
708 };
709
724 };
725
740 };