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 {
76 }
77 }
78
81 {
84 int run_diff= intra ? 0 : 1;
85
89 int level1, run1;
90
92 if (level1 < 1)
93 goto esc2;
96 esc2:
99 goto esc3;
101 if (run1 < 0)
102 goto esc3;
105 esc3:
106 /* third escape */
108 } else {
109 /* second escape */
111 }
112 } else {
113 /* first escape */
115 }
116 } else {
118 }
120 }
121
123 {
124 static uint16_t mv_index_tables[2][4096];
127
131 for (int last = 0; last < 2; last++) {
133 }
134 }
135 }
136 }
137 }
138
140 {
142
144 if (
s->msmpeg4_version >= 4) {
145 s->min_qcoeff = -255;
147 }
148
149 /* init various encoding tables */
151 }
152
154 {
157 int best = 0, best_size = INT_MAX;
158 int chroma_best = 0, best_chroma_size = INT_MAX;
159
162 int chroma_size=0;
164
167 chroma_size++;
168 }
172 int last;
173 const int last_size=
size + chroma_size;
174 for(last=0; last<2; last++){
178
182 }else{
186 }
187 }
188 if(last_size ==
size+chroma_size)
break;
189 }
190 }
194 }
195 if(chroma_size<best_chroma_size){
196 best_chroma_size= chroma_size;
198 }
199 }
200
202
204
205 s->rl_table_index = best;
206 s->rl_chroma_table_index= chroma_best;
207
208 if(
s->pict_type !=
s->last_non_b_pict_type){
209 s->rl_table_index= 2;
211 s->rl_chroma_table_index= 1;
212 else
213 s->rl_chroma_table_index= 2;
214 }
215
216 }
217
218 /* write MSMPEG4 compatible frame header */
220 {
222
224
227
229 if(
s->msmpeg4_version<=2){
230 s->rl_table_index = 2;
231 s->rl_chroma_table_index = 2;
232 }
233
234 s->dc_table_index = 1;
235 s->mv_table_index = 1;
/* only if P-frame */
236 s->use_skip_mb_code = 1;
/* only if P-frame */
237 s->per_mb_rl_table = 0;
238 if(
s->msmpeg4_version==4)
240 ff_dlog(
s,
"%d %"PRId64
" %d %d %d\n",
s->pict_type,
s->bit_rate,
241 s->inter_intra_pred,
s->width,
s->height);
242
244 s->slice_height=
s->mb_height/1;
245 put_bits(&
s->pb, 5, 0x16 +
s->mb_height/
s->slice_height);
246
247 if(
s->msmpeg4_version==4){
251 }
252
253 if(
s->msmpeg4_version>2){
254 if(!
s->per_mb_rl_table){
257 }
258
260 }
261 } else {
263
266
267 if(
s->msmpeg4_version>2){
268 if(!
s->per_mb_rl_table)
270
272
274 }
275 }
276
277 s->esc3_level_length= 0;
278 s->esc3_run_length= 0;
279 }
280
282 {
283 unsigned fps;
284
285 if (
s->avctx->framerate.num > 0 &&
s->avctx->framerate.den > 0)
286 fps =
s->avctx->framerate.num /
s->avctx->framerate.den;
287 else {
289 fps =
s->avctx->time_base.den /
s->avctx->time_base.num
290 #if FF_API_TICKS_PER_FRAME
291 /
FFMAX(
s->avctx->ticks_per_frame, 1)
292 #endif
293 ;
295 }
296
298
300
301 if (
s->msmpeg4_version >= 3)
303 else
305 }
306
308 int mx, int my)
309 {
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 !!! */
316 if (mx <= -64)
317 mx += 64;
318 else if (mx >= 64)
319 mx -= 64;
320 if (my <= -64)
321 my += 64;
322 else if (my >= 64)
323 my -= 64;
324
325 mx += 32;
326 my += 32;
328
329 code =
mv->table_mv_index[(mx << 6) | my];
334 /* escape : code literally */
337 }
338 }
339
342 if (
s->slice_height && (
s->mb_y %
s->slice_height) == 0) {
343 if(
s->msmpeg4_version < 4){
345 }
346 s->first_slice_line = 1;
347 } else {
348 s->first_slice_line = 0;
349 }
350 }
351 }
352
354 {
356
358 /* zero vector */
361 } else {
362 bit_size =
s->f_code - 1;
363 range = 1 << bit_size;
368
370 sign = 0;
371 } else {
373 sign = 1;
374 }
378
380 if (bit_size > 0) {
382 }
383 }
384 }
385
387 int16_t
block[6][64],
388 int motion_x, int motion_y)
389 {
390 int cbp, coded_cbp,
i;
391 int pred_x, pred_y;
392 uint8_t *coded_block;
393
395
397 /* compute cbp */
398 cbp = 0;
399 for (
i = 0;
i < 6;
i++) {
400 if (
s->block_last_index[
i] >= 0)
402 }
403 if (
s->use_skip_mb_code && (cbp | motion_x | motion_y) == 0) {
404 /* skip macroblock */
409
410 return;
411 }
412 if (
s->use_skip_mb_code)
414
415 if(
s->msmpeg4_version<=2){
419 if((cbp&3) != 3) coded_cbp= cbp ^ 0x3C;
420 else coded_cbp= cbp;
421
425
427
431 }else{
435
437
438 /* motion vector */
441 motion_y - pred_y);
442 }
443
445
446 for (
i = 0;
i < 6;
i++) {
448 }
450 } else {
451 /* compute cbp */
452 cbp = 0;
453 coded_cbp = 0;
454 for (
i = 0;
i < 6;
i++) {
456 val = (
s->block_last_index[
i] >= 1);
457 cbp |=
val << (5 -
i);
459 /* predict value for close blocks only for luma */
463 }
464 coded_cbp |=
val << (5 -
i);
465 }
466
467 if(
s->msmpeg4_version<=2){
471 } else {
472 if (
s->use_skip_mb_code)
477 }
478 put_bits(&
s->pb, 1, 0);
/* no AC prediction yet */
482 }else{
486 } else {
487 if (
s->use_skip_mb_code)
492 }
493 put_bits(&
s->pb, 1, 0);
/* no AC prediction yet */
494 if(
s->inter_intra_pred){
497 }
498 }
500
501 for (
i = 0;
i < 6;
i++) {
503 }
506 }
507 }
508
510 {
513
514 int16_t *dc_val;
516
517 /* update predictor */
518 if (n < 4) {
519 *dc_val =
level *
s->y_dc_scale;
520 } else {
521 *dc_val =
level *
s->c_dc_scale;
522 }
523
524 /* do the prediction */
526
527 if(
s->msmpeg4_version<=2){
528 if (n < 4) {
532 }else{
536 }
537 }else{
538 sign = 0;
541 sign = 1;
542 }
546
549
552
555 }
556 }
557 }
558
559 /* Encoding of a block; very similar to MPEG-4 except for a different
560 * escape coding (same as H.263) and more VLC tables. */
562 {
565 int last_non_zero, sign, slevel;
566 int code, run_diff, dc_pred_dir;
568 const uint8_t *scantable;
569
573 if (n < 4) {
575 } else {
577 }
578 run_diff =
s->msmpeg4_version>=4;
579 scantable=
s->intra_scantable.permutated;
580 } else {
583 if(
s->msmpeg4_version<=2)
584 run_diff = 0;
585 else
586 run_diff = 1;
587 scantable=
s->inter_scantable.permutated;
588 }
589
590 /* recalculate block_last_index for M$ wmv1 */
591 if (
s->msmpeg4_version >= 4 &&
s->block_last_index[n] > 0) {
592 for(last_index=63; last_index>=0; last_index--){
593 if(
block[scantable[last_index]])
break;
594 }
595 s->block_last_index[n]= last_index;
596 }else
597 last_index =
s->block_last_index[n];
598 /* AC coefs */
599 last_non_zero =
i - 1;
600 for (;
i <= last_index;
i++) {
604 run =
i - last_non_zero - 1;
605 last = (
i == last_index);
606 sign = 0;
609 sign = 1;
611 }
612
615 }
616
617 ms->
ac_stats[
s->mb_intra][n > 3][40][63][0]++;
//esc3 like
618
622 int level1, run1;
623
625 if (level1 < 1)
626 goto esc2;
629 esc2:
632 goto esc3;
634 if (run1 < 0)
635 goto esc3;
637 if (
s->msmpeg4_version == 4 &&
code == rl->
n)
638 goto esc3;
641 esc3:
642 /* third escape */
645 if(
s->msmpeg4_version>=4){
646 if(
s->esc3_level_length==0){
647 s->esc3_level_length=8;
648 s->esc3_run_length= 6;
649 //ESCLVLSZ + ESCRUNSZ
652 else
654 }
658 }else{
661 }
662 } else {
663 /* second escape */
667 }
668 } else {
669 /* first escape */
673 }
674 } else {
676 }
678 }
679 }
680 }
681
683 .
p.
name =
"msmpeg4v2",
695 };
696
710 };
711
725 };