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
48
50
51 /* build the table which associate a (x,y) motion vector to a vlc */
53 {
55
56 tab->table_mv_index = table_mv_index;
57
58 /* mark all entries as not used */
61
63 x =
tab->table_mvx[
i];
64 y =
tab->table_mvy[
i];
65 tab->table_mv_index[(x << 6) | y] =
i;
66 }
67 }
68
70 {
71 if (n == 0) {
73 } else {
75 }
76 }
77
80 {
83 int run_diff= intra ? 0 : 1;
84
88 int level1, run1;
89
91 if (level1 < 1)
92 goto esc2;
95 esc2:
98 goto esc3;
100 if (run1 < 0)
101 goto esc3;
104 esc3:
105 /* third escape */
107 } else {
108 /* second escape */
110 }
111 } else {
112 /* first escape */
114 }
115 } else {
117 }
119 }
120
122 {
123 static uint16_t mv_index_tables[2][4096];
126
130 for (int last = 0; last < 2; last++) {
132 }
133 }
134 }
135 }
136 }
137
139 {
141
143 if (
s->msmpeg4_version >= MSMP4_WMV1) {
144 s->min_qcoeff = -255;
146 }
147
148 /* init various encoding tables */
150 }
151
153 {
156 int best = 0, best_size = INT_MAX;
157 int chroma_best = 0, best_chroma_size = INT_MAX;
158
161 int chroma_size=0;
163
166 chroma_size++;
167 }
171 int last;
172 const int last_size=
size + chroma_size;
173 for(last=0; last<2; last++){
177
181 }else{
185 }
186 }
187 if(last_size ==
size+chroma_size)
break;
188 }
189 }
193 }
194 if(chroma_size<best_chroma_size){
195 best_chroma_size= chroma_size;
197 }
198 }
199
201
203
204 s->rl_table_index = best;
205 s->rl_chroma_table_index= chroma_best;
206
207 if(
s->pict_type !=
s->last_non_b_pict_type){
208 s->rl_table_index= 2;
210 s->rl_chroma_table_index= 1;
211 else
212 s->rl_chroma_table_index= 2;
213 }
214
215 }
216
217 /* write MSMPEG4 compatible frame header */
219 {
221
223
226
228 if (
s->msmpeg4_version <= MSMP4_V2) {
229 s->rl_table_index = 2;
230 s->rl_chroma_table_index = 2;
231 }
232
233 s->dc_table_index = 1;
234 s->mv_table_index = 1;
/* only if P-frame */
235 s->use_skip_mb_code = 1;
/* only if P-frame */
236 s->per_mb_rl_table = 0;
237 if (
s->msmpeg4_version == MSMP4_WMV1)
239 ff_dlog(
s,
"%d %"PRId64
" %d %d %d\n",
s->pict_type,
s->bit_rate,
240 s->inter_intra_pred,
s->width,
s->height);
241
243 s->slice_height=
s->mb_height/1;
244 put_bits(&
s->pb, 5, 0x16 +
s->mb_height/
s->slice_height);
245
246 if (
s->msmpeg4_version == MSMP4_WMV1) {
250 }
251
252 if (
s->msmpeg4_version > MSMP4_V2) {
253 if(!
s->per_mb_rl_table){
256 }
257
259 }
260 } else {
262
263 if (
s->msmpeg4_version == MSMP4_WMV1 &&
s->bit_rate >
MBAC_BITRATE)
265
266 if (
s->msmpeg4_version > MSMP4_V2) {
267 if(!
s->per_mb_rl_table)
269
271
273 }
274 }
275
276 s->esc3_level_length= 0;
277 s->esc3_run_length= 0;
278 }
279
281 {
282 unsigned fps;
283
284 if (
s->avctx->framerate.num > 0 &&
s->avctx->framerate.den > 0)
285 fps =
s->avctx->framerate.num /
s->avctx->framerate.den;
286 else {
288 fps =
s->avctx->time_base.den /
s->avctx->time_base.num
289 #if FF_API_TICKS_PER_FRAME
290 /
FFMAX(
s->avctx->ticks_per_frame, 1)
291 #endif
292 ;
294 }
295
297
299
300 if (
s->msmpeg4_version >= MSMP4_V3)
302 else
304 }
305
308 {
311
312 /* modulo encoding */
313 /* WARNING : you cannot reach all the MVs even with the modulo
314 encoding. This is a somewhat strange compromise they took !!! */
323
327
333 /* escape : code literally */
336 }
337 }
338
341 if (
s->slice_height && (
s->mb_y %
s->slice_height) == 0) {
342 if (
s->msmpeg4_version < MSMP4_WMV1) {
344 }
345 s->first_slice_line = 1;
346 } else {
347 s->first_slice_line = 0;
348 }
349 }
350 }
351
353 {
355
357 /* zero vector */
360 } else {
361 bit_size =
s->f_code - 1;
362 range = 1 << bit_size;
367
369 sign = 0;
370 } else {
372 sign = 1;
373 }
377
379 if (bit_size > 0) {
381 }
382 }
383 }
384
386 int16_t
block[6][64],
387 int motion_x, int motion_y)
388 {
389 int cbp, coded_cbp,
i;
390 int pred_x, pred_y;
391
393
395 /* compute cbp */
396 cbp = 0;
397 for (
i = 0;
i < 6;
i++) {
398 if (
s->block_last_index[
i] >= 0)
400 }
401 if (
s->use_skip_mb_code && (cbp | motion_x | motion_y) == 0) {
402 /* skip macroblock */
406
407 return;
408 }
409 if (
s->use_skip_mb_code)
411
412 if (
s->msmpeg4_version <= MSMP4_V2) {
416 if((cbp&3) != 3) coded_cbp= cbp ^ 0x3C;
417 else coded_cbp= cbp;
418
422
424
428 }else{
432
434
435 /* motion vector */
438 motion_y - pred_y);
439 }
440
442
443 for (
i = 0;
i < 6;
i++) {
445 }
447 } else {
448 /* compute cbp */
449 cbp = 0;
450 for (
int i = 0;
i < 6;
i++) {
451 int val = (
s->block_last_index[
i] >= 1);
452 cbp |=
val << (5 -
i);
453 }
454 if (
s->msmpeg4_version <= MSMP4_V2) {
458 } else {
459 if (
s->use_skip_mb_code)
464 }
465 put_bits(&
s->pb, 1, 0);
/* no AC prediction yet */
469 }else{
471 /* compute coded_cbp; the 0x3 corresponds to chroma cbp;
472 * luma coded_cbp are set in the loop below */
473 coded_cbp = cbp & 0x3;
474 for (
int i = 0;
i < 4;
i++) {
475 uint8_t *coded_block;
477 int val = (
s->block_last_index[
i] >= 1);
480 coded_cbp |=
val << (5 -
i);
481 }
482
485 } else {
486 if (
s->use_skip_mb_code)
491 }
492 put_bits(&
s->pb, 1, 0);
/* no AC prediction yet */
493 if(
s->inter_intra_pred){
496 }
497 }
499
500 for (
i = 0;
i < 6;
i++) {
502 }
505 }
506 }
507
509 {
512
513 int16_t *dc_val;
515
516 /* update predictor */
517 if (n < 4) {
518 *dc_val =
level *
s->y_dc_scale;
519 } else {
520 *dc_val =
level *
s->c_dc_scale;
521 }
522
523 /* do the prediction */
525
526 if (
s->msmpeg4_version <= MSMP4_V2) {
527 if (n < 4) {
531 }else{
535 }
536 }else{
537 sign = 0;
540 sign = 1;
541 }
545
548
551
554 }
555 }
556 }
557
558 /* Encoding of a block; very similar to MPEG-4 except for a different
559 * escape coding (same as H.263) and more VLC tables. */
561 {
564 int last_non_zero, sign, slevel;
565 int code, run_diff, dc_pred_dir;
567 const uint8_t *scantable;
568
572 if (n < 4) {
574 } else {
576 }
577 run_diff =
s->msmpeg4_version >= MSMP4_WMV1;
578 scantable=
s->intra_scantable.permutated;
579 } else {
582 run_diff =
s->msmpeg4_version > MSMP4_V2;
583 scantable=
s->inter_scantable.permutated;
584 }
585
586 /* recalculate block_last_index for M$ wmv1 */
587 if (
s->msmpeg4_version >= MSMP4_WMV1 &&
s->block_last_index[n] > 0) {
588 for(last_index=63; last_index>=0; last_index--){
589 if(
block[scantable[last_index]])
break;
590 }
591 s->block_last_index[n]= last_index;
592 }else
593 last_index =
s->block_last_index[n];
594 /* AC coefs */
595 last_non_zero =
i - 1;
596 for (;
i <= last_index;
i++) {
600 run =
i - last_non_zero - 1;
601 last = (
i == last_index);
602 sign = 0;
605 sign = 1;
607 }
608
611 }
612
613 ms->
ac_stats[
s->mb_intra][n > 3][40][63][0]++;
//esc3 like
614
618 int level1, run1;
619
621 if (level1 < 1)
622 goto esc2;
625 esc2:
628 goto esc3;
630 if (run1 < 0)
631 goto esc3;
633 if (
s->msmpeg4_version == MSMP4_WMV1 &&
code == rl->
n)
634 goto esc3;
637 esc3:
638 /* third escape */
641 if (
s->msmpeg4_version >= MSMP4_WMV1) {
642 if(
s->esc3_level_length==0){
643 s->esc3_level_length=8;
644 s->esc3_run_length= 6;
645 //ESCLVLSZ + ESCRUNSZ
648 else
650 }
654 }else{
657 }
658 } else {
659 /* second escape */
663 }
664 } else {
665 /* first escape */
669 }
670 } else {
672 }
674 }
675 }
676 }
677
679 .
p.
name =
"msmpeg4v2",
692 };
693
708 };
709
724 };