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
45
47
48 /* build the table which associate a (x,y) motion vector to a vlc */
50 {
51 int i, x, y;
52
56
57 /* mark all entries as not used */
58 for(i=0;i<4096;i++)
60
61 for(i=0;i<tab->
n;i++) {
65 }
66
67 return 0;
68 }
69
71 {
72 if (n == 0) {
74 } else {
77 }
78 }
79
82 int code;
83 int run_diff= intra ? 0 : 1;
84
88 int level1, run1;
89
91 if (level1 < 1)
92 goto esc2;
95 esc2:
96 size++;
98 goto esc3;
100 if (run1 < 0)
101 goto esc3;
104 esc3:
105 /* third escape */
106 size+=1+1+6+8;
107 } else {
108 /* second escape */
110 }
111 } else {
112 /* first escape */
114 }
115 } else {
116 size++;
117 }
119 }
120
122 {
123 static int init_done=0;
124 int i, ret;
125
130 }
131
132 if (!init_done) {
133 /* init various encoding tables */
134 init_done = 1;
136 return ret;
138 return ret;
141
144 for (level = 1; level <=
MAX_LEVEL; level++) {
146 for(run=0; run<=
MAX_RUN; run++){
147 int last;
148 for(last=0; last<2; last++){
150 }
151 }
152 }
153 }
154 }
155
156 return 0;
157 }
158
160 {
161 int i;
162 int best = 0, best_size = INT_MAX;
163 int chroma_best = 0, best_chroma_size = INT_MAX;
164
165 for(i=0; i<3; i++){
167 int chroma_size=0;
169
170 if(i>0){// ;)
171 size++;
172 chroma_size++;
173 }
176 for(run=0; run<=
MAX_RUN; run++){
177 int last;
178 const int last_size= size + chroma_size;
179 for(last=0; last<2; last++){
183
186 chroma_size+= intra_chroma_count*rl_length[i+3][
level][
run][last];
187 }else{
191 }
192 }
193 if(last_size == size+chroma_size) break;
194 }
195 }
196 if(size<best_size){
198 best= i;
199 }
200 if(chroma_size<best_chroma_size){
201 best_chroma_size= chroma_size;
202 chroma_best= i;
203 }
204 }
205
207
209
212
217 else
219 }
220
221 }
222
223 /* write MSMPEG4 compatible frame header */
225 {
227
230
235 }
236
245
249
254 }
255
260 }
261
263 }
264 } else {
266
269
273
275
277 }
278 }
279
282 }
283
285 {
288
290
293 else
295 }
296
298 int mx, int my)
299 {
300 int code;
302
303 /* modulo encoding */
304 /* WARNING : you cannot reach all the MVs even with the modulo
305 encoding. This is a somewhat strange compromise they took !!! */
306 if (mx <= -64)
307 mx += 64;
308 else if (mx >= 64)
309 mx -= 64;
310 if (my <= -64)
311 my += 64;
312 else if (my >= 64)
313 my -= 64;
314
315 mx += 32;
316 my += 32;
318
324 /* escape : code literally */
327 }
328 }
329
335 }
337 } else {
339 }
340 }
341 }
342
344 {
345 int range, bit_size, sign, code, bits;
346
347 if (val == 0) {
348 /* zero vector */
349 code = 0;
351 } else {
353 range = 1 << bit_size;
354 if (val <= -64)
355 val += 64;
356 else if (val >= 64)
357 val -= 64;
358
359 if (val >= 0) {
360 sign = 0;
361 } else {
363 sign = 1;
364 }
365 val--;
366 code = (val >> bit_size) + 1;
367 bits = val & (range - 1);
368
370 if (bit_size > 0) {
372 }
373 }
374 }
375
377 int16_t
block[6][64],
378 int motion_x, int motion_y)
379 {
380 int cbp, coded_cbp, i;
381 int pred_x, pred_y;
383
385
387 /* compute cbp */
388 cbp = 0;
389 for (i = 0; i < 6; i++) {
391 cbp |= 1 << (5 - i);
392 }
394 /* skip macroblock */
399
400 return;
401 }
404
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 coded_cbp = 0;
444 for (i = 0; i < 6; i++) {
447 cbp |= val << (5 - i);
448 if (i < 4) {
449 /* predict value for close blocks only for luma */
453 }
454 coded_cbp |= val << (5 - i);
455 }
456
461 } else {
467 }
468 put_bits(&s->
pb, 1, 0);
/* no AC prediction yet */
472 }else{
476 } else {
482 }
483 put_bits(&s->
pb, 1, 0);
/* no AC prediction yet */
487 }
488 }
490
491 for (i = 0; i < 6; i++) {
493 }
496 }
497 }
498
500 {
501 int sign, code;
503 int extrabits = 0;
504
505 int16_t *dc_val;
507
508 /* update predictor */
509 if (n < 4) {
511 } else {
513 }
514
515 /* do the prediction */
517
519 if (n < 4) {
523 }else{
527 }
528 }else{
529 sign = 0;
530 if (level < 0) {
532 sign = 1;
533 }
539 extquant = (level + 3) & 0x3;
540 code = ((level+3)>>2);
541 }
else if( s->
qscale == 2 ) {
542 extquant = (level + 1) & 0x1;
543 code = ((level+1)>>1);
544 }
545 }
546
548 if (n < 4) {
550 } else {
552 }
553 } else {
554 if (n < 4) {
556 } else {
558 }
559 }
560
562 extrabits = 3 - s->
qscale;
563
566 else if(extrabits > 0)//== VC1 && s->qscale<=2
568
569 if (level != 0) {
571 }
572 }
573 }
574
575 /* Encoding of a block; very similar to MPEG-4 except for a different
576 * escape coding (same as H.263) and more VLC tables. */
578 {
579 int level,
run, last, i, j, last_index;
580 int last_non_zero, sign, slevel;
581 int code, run_diff, dc_pred_dir;
584
587 i = 1;
588 if (n < 4) {
590 } else {
592 }
595 } else {
596 i = 0;
599 run_diff = 0;
600 else
601 run_diff = 1;
603 }
604
605 /* recalculate block_last_index for M$ wmv1 */
607 for(last_index=63; last_index>=0; last_index--){
608 if(block[scantable[last_index]]) break;
609 }
611 }else
613 /* AC coefs */
614 last_non_zero = i - 1;
615 for (; i <= last_index; i++) {
616 j = scantable[i];
617 level = block[j];
618 if (level) {
619 run = i - last_non_zero - 1;
620 last = (i == last_index);
621 sign = 0;
623 if (level < 0) {
624 sign = 1;
626 }
627
630 }
631
633
637 int level1, run1;
638
640 if (level1 < 1)
641 goto esc2;
644 esc2:
647 goto esc3;
649 if (run1 < 0)
650 goto esc3;
653 goto esc3;
656 esc3:
657 /* third escape */
664 //ESCLVLSZ + ESCRUNSZ
667 else
669 }
673 }else{
676 }
677 } else {
678 /* second escape */
682 }
683 } else {
684 /* first escape */
688 }
689 } else {
691 }
692 last_non_zero = i;
693 }
694 }
695 }
const char const char void * val
uint32_t ff_v2_dc_lum_table[512][2]
const uint32_t ff_table_mb_non_intra[128][2]
static void put_sbits(PutBitContext *pb, int n, int32_t value)
static void put_bits(Jpeg2000EncoderContext *s, int val, int n)
put n times val bit
const uint32_t ff_table0_dc_lum[120][2]
Memory handling functions.
uint32_t ff_v2_dc_chroma_table[512][2]
int msmpeg4_version
0=not msmpeg4, 1=mp41, 2=mp42, 3=mp43/divx3 4=wmv1/7 5=wmv2/8
void avpriv_align_put_bits(PutBitContext *s)
Pad the bitstream with zeros up to the next byte boundary.
Convenience header that includes libavutil's core.
int min_qcoeff
minimum encodable coefficient
static uint8_t rl_length[NB_RL_TABLES][MAX_LEVEL+1][MAX_RUN+1][2]
av_cold void ff_msmpeg4_common_init(MpegEncContext *s)
int16_t * ff_h263_pred_motion(MpegEncContext *s, int block, int dir, int *px, int *py)
Macro definitions for various function/variable attributes.
AVRational time_base
This is the fundamental unit of time (in seconds) in terms of which frame timestamps are represented...
void ff_mpeg4_clean_buffers(MpegEncContext *s)
void ff_msmpeg4_encode_picture_header(MpegEncContext *s, int picture_number)
#define av_assert0(cond)
assert() equivalent, that is always enabled.
int8_t * max_run[2]
encoding & decoding
static av_cold int init_mv_table(MVTable *tab)
const uint8_t ff_v2_intra_cbpc[4][2]
uint8_t ff_static_rl_table_store[NB_RL_TABLES][2][2 *MAX_RUN+MAX_LEVEL+3]
int misc_bits
cbp, mb_type
int8_t * max_level[2]
encoding & decoding
const uint32_t ff_table0_dc_chroma[120][2]
int mb_height
number of MBs horizontally & vertically
int max_qcoeff
maximum encodable coefficient
static void msmpeg4_encode_dc(MpegEncContext *s, int level, int n, int *dir_ptr)
static void find_best_tables(MpegEncContext *s)
static int get_bits_diff(MpegEncContext *s)
int last_non_b_pict_type
used for MPEG-4 gmc B-frames & ratecontrol
const uint16_t ff_msmp4_mb_i_table[64][2]
void ff_msmpeg4_encode_mb(MpegEncContext *s, int16_t block[6][64], int motion_x, int motion_y)
int rl_chroma_table_index
int n
number of entries of table_vlc minus 1
const uint8_t ff_v2_mb_type[8][2]
int ff_msmpeg4_pred_dc(MpegEncContext *s, int n, int16_t **dc_val_ptr, int *dir_ptr)
const uint8_t * table_mvx
const uint16_t(* table_vlc)[2]
const uint8_t * table_mv_bits
int(* ac_stats)[2][MAX_LEVEL+1][MAX_RUN+1][2]
[mb_intra][isChroma][level][run][last]
av_cold int ff_msmpeg4_encode_init(MpegEncContext *s)
int block_last_index[12]
last non zero coefficient in block
int ticks_per_frame
For some codecs, the time base is closer to the field rate than the frame rate.
const uint32_t ff_table1_dc_chroma[120][2]
uint16_t * table_mv_index
static const float pred[4]
static const int8_t mv[256][2]
int first_slice_line
used in MPEG-4 too to handle resync markers
void ff_msmpeg4_encode_block(MpegEncContext *s, int16_t *block, int n)
ScanTable intra_scantable
int height
picture size. must be a multiple of 16
void ff_msmpeg4_encode_ext_header(MpegEncContext *s)
const uint16_t * table_mv_code
av_cold int ff_rl_init(RLTable *rl, uint8_t static_store[2][2 *MAX_RUN+MAX_LEVEL+3])
const uint32_t ff_table1_dc_lum[120][2]
static void msmpeg4v2_encode_motion(MpegEncContext *s, int val)
int slice_height
in macroblocks
int f_code
forward MV resolution
const uint8_t ff_h263_cbpy_tab[16][2]
void ff_msmpeg4_code012(PutBitContext *pb, int n)
int pict_type
AV_PICTURE_TYPE_I, AV_PICTURE_TYPE_P, AV_PICTURE_TYPE_B, ...
RLTable ff_rl_table[NB_RL_TABLES]
struct AVCodecContext * avctx
PutBitContext pb
bit output
void ff_msmpeg4_handle_slices(MpegEncContext *s)
void ff_msmpeg4_encode_motion(MpegEncContext *s, int mx, int my)
common internal api header.
static int get_rl_index(const RLTable *rl, int last, int run, int level)
int last_bits
temp var used for calculating the above vars
int64_t bit_rate
wanted bit rate
static const struct twinvq_data tab
const uint8_t * table_mvy
ScanTable inter_scantable
if inter == intra then intra should be used to reduce the cache usage
int ff_msmpeg4_coded_block_pred(MpegEncContext *s, int n, uint8_t **coded_block_ptr)
static int get_size_of_code(MpegEncContext *s, RLTable *rl, int last, int run, int level, int intra)
int h263_aic_dir
AIC direction: 0 = left, 1 = top.
const uint8_t ff_table_inter_intra[4][2]
const uint8_t ff_mvtab[33][2]