FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavcodec/msmpeg4enc.c
Date: 2025-06-23 20:06:14
Exec Total Coverage
Lines: 369 386 95.6%
Functions: 14 14 100.0%
Branches: 181 214 84.6%

Line Branch Exec Source
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
35 #include "libavutil/attributes.h"
36 #include "libavutil/avutil.h"
37 #include "libavutil/thread.h"
38 #include "codec_internal.h"
39 #include "mpegvideo.h"
40 #include "mpegvideoenc.h"
41 #include "h263.h"
42 #include "h263data.h"
43 #include "mpeg4video.h"
44 #include "msmpeg4.h"
45 #include "msmpeg4data.h"
46 #include "msmpeg4_vc1_data.h"
47 #include "msmpeg4enc.h"
48 #include "put_bits.h"
49 #include "rl.h"
50
51 static uint8_t rl_length[NB_RL_TABLES][MAX_LEVEL+1][MAX_RUN+1][2];
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.
55 static uint32_t mv_vector_tables[2][4096];
56
57 /* build the table which associate a (x,y) motion vector to a vlc */
58 34 static av_cold void init_mv_table(const uint16_t mv_table[], const uint8_t mv_table_lens[],
59 uint32_t mv_vector_table[4096],
60 unsigned escape_code, int escape_length)
61 {
62
2/2
✓ Branch 0 taken 139264 times.
✓ Branch 1 taken 34 times.
139298 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 139264 mv_vector_table[i] = (escape_code << 20) | (i << 8) | escape_length;
67 }
68
69
2/2
✓ Branch 0 taken 37400 times.
✓ Branch 1 taken 34 times.
37434 for (uint32_t i = 0, code = 0; i < MSMPEG4_MV_TABLES_NB_ELEMS; i++) {
70 37400 int sym = mv_table[i];
71 37400 int len = mv_table_lens[i];
72 37400 int x = sym >> 8;
73 37400 int y = sym & 0xFF;
74 // We ignore the escape value here and restore it after the loop.
75 37400 mv_vector_table[(x << 6) | y] = (code >> (24 - len)) | len;
76 37400 code += 1U << (32 - len);
77 }
78 34 mv_vector_table[0] = (escape_code << 20) | escape_length;
79 34 }
80
81 1048 void ff_msmpeg4_code012(PutBitContext *pb, int n)
82 {
83
2/2
✓ Branch 0 taken 587 times.
✓ Branch 1 taken 461 times.
1048 if (n == 0) {
84 587 put_bits(pb, 1, 0);
85 } else {
86
2/2
✓ Branch 0 taken 367 times.
✓ Branch 1 taken 94 times.
461 put_bits(pb, 2, 2 | (n >= 2));
87 }
88 1048 }
89
90 848640 static int get_size_of_code(const RLTable *rl, int last, int run,
91 int level, int intra)
92 {
93 848640 int size=0;
94 int code;
95 848640 int run_diff= intra ? 0 : 1;
96
97 848640 code = get_rl_index(rl, last, run, level);
98 848640 size+= rl->table_vlc[code][1];
99
2/2
✓ Branch 0 taken 834411 times.
✓ Branch 1 taken 14229 times.
848640 if (code == rl->n) {
100 int level1, run1;
101
102 834411 level1 = level - rl->max_level[last][run];
103
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 834411 times.
834411 if (level1 < 1)
104 goto esc2;
105 834411 code = get_rl_index(rl, last, run, level1);
106
2/2
✓ Branch 0 taken 820182 times.
✓ Branch 1 taken 14229 times.
834411 if (code == rl->n) {
107 820182 esc2:
108 820182 size++;
109
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 820182 times.
820182 if (level > MAX_LEVEL)
110 goto esc3;
111 820182 run1 = run - rl->max_run[last][level] - run_diff;
112
2/2
✓ Branch 0 taken 8092 times.
✓ Branch 1 taken 812090 times.
820182 if (run1 < 0)
113 8092 goto esc3;
114 812090 code = get_rl_index(rl, last, run1, level);
115
2/2
✓ Branch 0 taken 805188 times.
✓ Branch 1 taken 6902 times.
812090 if (code == rl->n) {
116 805188 esc3:
117 /* third escape */
118 813280 size+=1+1+6+8;
119 } else {
120 /* second escape */
121 6902 size+= 1+1+ rl->table_vlc[code][1];
122 }
123 } else {
124 /* first escape */
125 14229 size+= 1+1+ rl->table_vlc[code][1];
126 }
127 } else {
128 14229 size++;
129 }
130 848640 return size;
131 }
132
133 17 static av_cold void msmpeg4_encode_init_static(void)
134 {
135 17 init_mv_table(ff_msmp4_mv_table0, ff_msmp4_mv_table0_lens,
136 mv_vector_tables[0], 0x0000, 8 + 12);
137 17 init_mv_table(ff_msmp4_mv_table1, ff_msmp4_mv_table1_lens,
138 mv_vector_tables[1], 0x000b, 4 + 12);
139
140
2/2
✓ Branch 0 taken 102 times.
✓ Branch 1 taken 17 times.
119 for (int i = 0; i < NB_RL_TABLES; i++) {
141
2/2
✓ Branch 0 taken 6528 times.
✓ Branch 1 taken 102 times.
6630 for (int level = 1; level <= MAX_LEVEL; level++) {
142
2/2
✓ Branch 0 taken 424320 times.
✓ Branch 1 taken 6528 times.
430848 for (int run = 0; run <= MAX_RUN; run++) {
143
2/2
✓ Branch 0 taken 848640 times.
✓ Branch 1 taken 424320 times.
1272960 for (int last = 0; last < 2; last++) {
144 848640 rl_length[i][level][run][last] = get_size_of_code(&ff_rl_table[i], last, run, level, 0);
145 }
146 }
147 }
148 }
149 17 }
150
151 625 static void find_best_tables(MSMPEG4EncContext *ms)
152 {
153 625 MPVEncContext *const s = &ms->m.s;
154 int i;
155 625 int best = 0, best_size = INT_MAX;
156 625 int chroma_best = 0, best_chroma_size = INT_MAX;
157
158
2/2
✓ Branch 0 taken 1875 times.
✓ Branch 1 taken 625 times.
2500 for(i=0; i<3; i++){
159 int level;
160 1875 int chroma_size=0;
161 1875 int size=0;
162
163
2/2
✓ Branch 0 taken 1250 times.
✓ Branch 1 taken 625 times.
1875 if(i>0){// ;)
164 1250 size++;
165 1250 chroma_size++;
166 }
167
2/2
✓ Branch 0 taken 121875 times.
✓ Branch 1 taken 1875 times.
123750 for(level=0; level<=MAX_LEVEL; level++){
168 int run;
169
1/2
✓ Branch 0 taken 255678 times.
✗ Branch 1 not taken.
255678 for(run=0; run<=MAX_RUN; run++){
170 int last;
171 255678 const int last_size= size + chroma_size;
172
2/2
✓ Branch 0 taken 511356 times.
✓ Branch 1 taken 255678 times.
767034 for(last=0; last<2; last++){
173 511356 int inter_count = ms->ac_stats[0][0][level][run][last] + ms->ac_stats[0][1][level][run][last];
174 511356 int intra_luma_count = ms->ac_stats[1][0][level][run][last];
175 511356 int intra_chroma_count= ms->ac_stats[1][1][level][run][last];
176
177
2/2
✓ Branch 0 taken 31794 times.
✓ Branch 1 taken 479562 times.
511356 if (s->c.pict_type == AV_PICTURE_TYPE_I) {
178 31794 size += intra_luma_count *rl_length[i ][level][run][last];
179 31794 chroma_size+= intra_chroma_count*rl_length[i+3][level][run][last];
180 }else{
181 479562 size+= intra_luma_count *rl_length[i ][level][run][last]
182 479562 +intra_chroma_count*rl_length[i+3][level][run][last]
183 479562 +inter_count *rl_length[i+3][level][run][last];
184 }
185 }
186
2/2
✓ Branch 0 taken 121875 times.
✓ Branch 1 taken 133803 times.
255678 if(last_size == size+chroma_size) break;
187 }
188 }
189
2/2
✓ Branch 0 taken 1435 times.
✓ Branch 1 taken 440 times.
1875 if(size<best_size){
190 1435 best_size= size;
191 1435 best= i;
192 }
193
2/2
✓ Branch 0 taken 667 times.
✓ Branch 1 taken 1208 times.
1875 if(chroma_size<best_chroma_size){
194 667 best_chroma_size= chroma_size;
195 667 chroma_best= i;
196 }
197 }
198
199
2/2
✓ Branch 0 taken 562 times.
✓ Branch 1 taken 63 times.
625 if (s->c.pict_type == AV_PICTURE_TYPE_P) chroma_best = best;
200
201 625 memset(ms->ac_stats, 0, sizeof(ms->ac_stats));
202
203 625 ms->rl_table_index = best;
204 625 ms->rl_chroma_table_index = chroma_best;
205
206
2/2
✓ Branch 0 taken 125 times.
✓ Branch 1 taken 500 times.
625 if (s->c.pict_type != ms->m.last_non_b_pict_type) {
207 125 ms->rl_table_index= 2;
208
2/2
✓ Branch 0 taken 63 times.
✓ Branch 1 taken 62 times.
125 if (s->c.pict_type == AV_PICTURE_TYPE_I)
209 63 ms->rl_chroma_table_index = 1;
210 else
211 62 ms->rl_chroma_table_index = 2;
212 }
213
214 625 }
215
216 /* write MSMPEG4 compatible frame header */
217 625 static int msmpeg4_encode_picture_header(MPVMainEncContext *const m)
218 {
219 625 MSMPEG4EncContext *const ms = (MSMPEG4EncContext*)m;
220 625 MPVEncContext *const s = &m->s;
221
222 625 find_best_tables(ms);
223
224 625 put_bits_assume_flushed(&s->pb);
225
226 625 put_bits(&s->pb, 2, s->c.pict_type - 1);
227
228 625 put_bits(&s->pb, 5, s->c.qscale);
229
2/2
✓ Branch 0 taken 200 times.
✓ Branch 1 taken 425 times.
625 if (s->c.msmpeg4_version <= MSMP4_V2) {
230 200 ms->rl_table_index = 2;
231 200 ms->rl_chroma_table_index = 2;
232 }
233
234 625 ms->dc_table_index = 1;
235 625 ms->mv_table_index = 1; /* only if P-frame */
236 625 ms->use_skip_mb_code = 1; /* only if P-frame */
237 625 ms->per_mb_rl_table = 0;
238
2/2
✓ Branch 0 taken 200 times.
✓ Branch 1 taken 425 times.
625 if (s->c.msmpeg4_version == MSMP4_WMV1)
239 200 s->c.inter_intra_pred = s->c.width * s->c.height < 320*240 &&
240
3/4
✓ Branch 0 taken 50 times.
✓ Branch 1 taken 150 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 50 times.
200 m->bit_rate <= II_BITRATE &&
241 s->c.pict_type == AV_PICTURE_TYPE_P;
242 ff_dlog(s->c.avctx, "%d %"PRId64" %d %d %d\n", s->c.pict_type, m->bit_rate,
243 s->c.inter_intra_pred, s->c.width, s->c.height);
244
245
2/2
✓ Branch 0 taken 63 times.
✓ Branch 1 taken 562 times.
625 if (s->c.pict_type == AV_PICTURE_TYPE_I) {
246 63 s->c.slice_height = s->c.mb_height/1;
247 63 put_bits(&s->pb, 5, 0x16 + s->c.mb_height/s->c.slice_height);
248
249
2/2
✓ Branch 0 taken 20 times.
✓ Branch 1 taken 43 times.
63 if (s->c.msmpeg4_version == MSMP4_WMV1) {
250 20 ff_msmpeg4_encode_ext_header(s);
251
1/2
✓ Branch 0 taken 20 times.
✗ Branch 1 not taken.
20 if (m->bit_rate > MBAC_BITRATE)
252 20 put_bits(&s->pb, 1, ms->per_mb_rl_table);
253 }
254
255
2/2
✓ Branch 0 taken 43 times.
✓ Branch 1 taken 20 times.
63 if (s->c.msmpeg4_version > MSMP4_V2) {
256
1/2
✓ Branch 0 taken 43 times.
✗ Branch 1 not taken.
43 if (!ms->per_mb_rl_table){
257 43 ff_msmpeg4_code012(&s->pb, ms->rl_chroma_table_index);
258 43 ff_msmpeg4_code012(&s->pb, ms->rl_table_index);
259 }
260
261 43 put_bits(&s->pb, 1, ms->dc_table_index);
262 }
263 } else {
264 562 put_bits(&s->pb, 1, ms->use_skip_mb_code);
265
266
3/4
✓ Branch 0 taken 180 times.
✓ Branch 1 taken 382 times.
✓ Branch 2 taken 180 times.
✗ Branch 3 not taken.
562 if (s->c.msmpeg4_version == MSMP4_WMV1 && m->bit_rate > MBAC_BITRATE)
267 180 put_bits(&s->pb, 1, ms->per_mb_rl_table);
268
269
2/2
✓ Branch 0 taken 382 times.
✓ Branch 1 taken 180 times.
562 if (s->c.msmpeg4_version > MSMP4_V2) {
270
1/2
✓ Branch 0 taken 382 times.
✗ Branch 1 not taken.
382 if (!ms->per_mb_rl_table)
271 382 ff_msmpeg4_code012(&s->pb, ms->rl_table_index);
272
273 382 put_bits(&s->pb, 1, ms->dc_table_index);
274
275 382 put_bits(&s->pb, 1, ms->mv_table_index);
276 }
277 }
278
279 625 s->esc3_level_length = 0;
280 625 ms->esc3_run_length = 0;
281
282 625 return 0;
283 }
284
285 63 void ff_msmpeg4_encode_ext_header(MPVEncContext *const s)
286 {
287 63 const MPVMainEncContext *const m = slice_to_mainenc(s);
288 unsigned fps;
289
290
2/4
✓ Branch 0 taken 63 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 63 times.
✗ Branch 3 not taken.
63 if (s->c.avctx->framerate.num > 0 && s->c.avctx->framerate.den > 0)
291 63 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
296 63 put_bits(&s->pb, 5, FFMIN(fps, 31)); //yes 29.97 -> 29
297
298
1/2
✓ Branch 0 taken 63 times.
✗ Branch 1 not taken.
63 put_bits(&s->pb, 11, FFMIN(m->bit_rate / 1024, 2047));
299
300
2/2
✓ Branch 0 taken 43 times.
✓ Branch 1 taken 20 times.
63 if (s->c.msmpeg4_version >= MSMP4_V3)
301 43 put_bits(&s->pb, 1, s->c.flipflop_rounding);
302 else
303
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 20 times.
20 av_assert0(!s->c.flipflop_rounding);
304 63 }
305
306 161363 void ff_msmpeg4_encode_motion(MSMPEG4EncContext *const ms,
307 int mx, int my)
308 {
309 161363 MPVEncContext *const s = &ms->m.s;
310 161363 const uint32_t *const mv_vector_table = mv_vector_tables[ms->mv_table_index];
311 uint32_t code;
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
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 161363 times.
161363 if (mx <= -64)
317 mx += 64;
318
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 161363 times.
161363 else if (mx >= 64)
319 mx -= 64;
320
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 161363 times.
161363 if (my <= -64)
321 my += 64;
322
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 161363 times.
161363 else if (my >= 64)
323 my -= 64;
324
325 161363 mx += 32;
326 161363 my += 32;
327
328 161363 code = mv_vector_table[(mx << 6) | my];
329 161363 put_bits(&s->pb, code & 0xff, code >> 8);
330 161363 }
331
332 249300 void ff_msmpeg4_handle_slices(MPVEncContext *const s)
333 {
334
2/2
✓ Branch 0 taken 11850 times.
✓ Branch 1 taken 237450 times.
249300 if (s->c.mb_x == 0) {
335
3/4
✓ Branch 0 taken 11850 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 825 times.
✓ Branch 3 taken 11025 times.
11850 if (s->c.slice_height && (s->c.mb_y % s->c.slice_height) == 0) {
336
2/2
✓ Branch 0 taken 425 times.
✓ Branch 1 taken 400 times.
825 if (s->c.msmpeg4_version < MSMP4_WMV1)
337 425 ff_mpeg4_clean_buffers(&s->c);
338 825 s->c.first_slice_line = 1;
339 } else {
340 11025 s->c.first_slice_line = 0;
341 }
342 }
343 249300 }
344
345 102006 static void msmpeg4v2_encode_motion(MPVEncContext *const s, int val)
346 {
347 int range, bit_size, sign, code, bits;
348
349
2/2
✓ Branch 0 taken 52036 times.
✓ Branch 1 taken 49970 times.
102006 if (val == 0) {
350 /* zero vector; corresponds to ff_mvtab[0] */
351 52036 put_bits(&s->pb, 1, 0x1);
352 } else {
353 49970 bit_size = s->f_code - 1;
354 49970 range = 1 << bit_size;
355
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 49970 times.
49970 if (val <= -64)
356 val += 64;
357
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 49970 times.
49970 else if (val >= 64)
358 val -= 64;
359
360
2/2
✓ Branch 0 taken 31351 times.
✓ Branch 1 taken 18619 times.
49970 if (val >= 0) {
361 31351 sign = 0;
362 } else {
363 18619 val = -val;
364 18619 sign = 1;
365 }
366 49970 val--;
367 49970 code = (val >> bit_size) + 1;
368 49970 bits = val & (range - 1);
369
370 49970 put_bits(&s->pb, ff_mvtab[code][1] + 1, (ff_mvtab[code][0] << 1) | sign);
371
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 49970 times.
49970 if (bit_size > 0) {
372 put_bits(&s->pb, bit_size, bits);
373 }
374 }
375 102006 }
376
377 189450 static void msmpeg4_encode_mb(MPVEncContext *const s,
378 int16_t block[][64],
379 int motion_x, int motion_y)
380 {
381 189450 MSMPEG4EncContext *const ms = mpv_to_msmpeg4(s);
382 int cbp, coded_cbp, i;
383 int pred_x, pred_y;
384
385 189450 ff_msmpeg4_handle_slices(s);
386
387
2/2
✓ Branch 0 taken 162914 times.
✓ Branch 1 taken 26536 times.
189450 if (!s->c.mb_intra) {
388 /* compute cbp */
389 162914 cbp = 0;
390
2/2
✓ Branch 0 taken 977484 times.
✓ Branch 1 taken 162914 times.
1140398 for (i = 0; i < 6; i++) {
391
2/2
✓ Branch 0 taken 354169 times.
✓ Branch 1 taken 623315 times.
977484 if (s->c.block_last_index[i] >= 0)
392 354169 cbp |= 1 << (5 - i);
393 }
394
3/4
✓ Branch 0 taken 162914 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 2259 times.
✓ Branch 3 taken 160655 times.
162914 if (ms->use_skip_mb_code && (cbp | motion_x | motion_y) == 0) {
395 /* skip macroblock */
396 2259 put_bits(&s->pb, 1, 1);
397 2259 s->last_bits++;
398 2259 s->misc_bits++;
399
400 2259 return;
401 }
402
1/2
✓ Branch 0 taken 160655 times.
✗ Branch 1 not taken.
160655 if (ms->use_skip_mb_code)
403 160655 put_bits(&s->pb, 1, 0); /* mb coded */
404
405
2/2
✓ Branch 0 taken 51003 times.
✓ Branch 1 taken 109652 times.
160655 if (s->c.msmpeg4_version <= MSMP4_V2) {
406 51003 put_bits(&s->pb,
407 51003 ff_v2_mb_type[cbp&3][1],
408 51003 ff_v2_mb_type[cbp&3][0]);
409
2/2
✓ Branch 0 taken 40593 times.
✓ Branch 1 taken 10410 times.
51003 if((cbp&3) != 3) coded_cbp= cbp ^ 0x3C;
410 10410 else coded_cbp= cbp;
411
412 51003 put_bits(&s->pb,
413 51003 ff_h263_cbpy_tab[coded_cbp>>2][1],
414 51003 ff_h263_cbpy_tab[coded_cbp>>2][0]);
415
416 51003 s->misc_bits += get_bits_diff(s);
417
418 51003 ff_h263_pred_motion(&s->c, 0, 0, &pred_x, &pred_y);
419 51003 msmpeg4v2_encode_motion(s, motion_x - pred_x);
420 51003 msmpeg4v2_encode_motion(s, motion_y - pred_y);
421 }else{
422 109652 put_bits(&s->pb,
423 109652 ff_table_mb_non_intra[cbp + 64][1],
424 109652 ff_table_mb_non_intra[cbp + 64][0]);
425
426 109652 s->misc_bits += get_bits_diff(s);
427
428 /* motion vector */
429 109652 ff_h263_pred_motion(&s->c, 0, 0, &pred_x, &pred_y);
430 109652 ff_msmpeg4_encode_motion(ms, motion_x - pred_x,
431 motion_y - pred_y);
432 }
433
434 160655 s->mv_bits += get_bits_diff(s);
435
436
2/2
✓ Branch 0 taken 963930 times.
✓ Branch 1 taken 160655 times.
1124585 for (i = 0; i < 6; i++) {
437 963930 ff_msmpeg4_encode_block(s, block[i], i);
438 }
439 160655 s->p_tex_bits += get_bits_diff(s);
440 } else {
441 /* compute cbp */
442 26536 cbp = 0;
443
2/2
✓ Branch 0 taken 159216 times.
✓ Branch 1 taken 26536 times.
185752 for (int i = 0; i < 6; i++) {
444 159216 int val = (s->c.block_last_index[i] >= 1);
445 159216 cbp |= val << (5 - i);
446 }
447
2/2
✓ Branch 0 taken 8142 times.
✓ Branch 1 taken 18394 times.
26536 if (s->c.msmpeg4_version <= MSMP4_V2) {
448
2/2
✓ Branch 0 taken 5985 times.
✓ Branch 1 taken 2157 times.
8142 if (s->c.pict_type == AV_PICTURE_TYPE_I) {
449 5985 put_bits(&s->pb,
450 5985 ff_v2_intra_cbpc[cbp&3][1], ff_v2_intra_cbpc[cbp&3][0]);
451 } else {
452
1/2
✓ Branch 0 taken 2157 times.
✗ Branch 1 not taken.
2157 if (ms->use_skip_mb_code)
453 2157 put_bits(&s->pb, 1, 0); /* mb coded */
454 2157 put_bits(&s->pb,
455 2157 ff_v2_mb_type[(cbp&3) + 4][1],
456 2157 ff_v2_mb_type[(cbp&3) + 4][0]);
457 }
458 8142 put_bits(&s->pb, 1, 0); /* no AC prediction yet */
459 8142 put_bits(&s->pb,
460 8142 ff_h263_cbpy_tab[cbp>>2][1],
461 8142 ff_h263_cbpy_tab[cbp>>2][0]);
462 }else{
463
2/2
✓ Branch 0 taken 13158 times.
✓ Branch 1 taken 5236 times.
18394 if (s->c.pict_type == AV_PICTURE_TYPE_I) {
464 /* compute coded_cbp; the 0x3 corresponds to chroma cbp;
465 * luma coded_cbp are set in the loop below */
466 13158 coded_cbp = cbp & 0x3;
467
2/2
✓ Branch 0 taken 52632 times.
✓ Branch 1 taken 13158 times.
65790 for (int i = 0; i < 4; i++) {
468 uint8_t *coded_block;
469 52632 int pred = ff_msmpeg4_coded_block_pred(&s->c, i, &coded_block);
470 52632 int val = (s->c.block_last_index[i] >= 1);
471 52632 *coded_block = val;
472 52632 val ^= pred;
473 52632 coded_cbp |= val << (5 - i);
474 }
475
476 13158 put_bits(&s->pb,
477 13158 ff_msmp4_mb_i_table[coded_cbp][1], ff_msmp4_mb_i_table[coded_cbp][0]);
478 } else {
479
1/2
✓ Branch 0 taken 5236 times.
✗ Branch 1 not taken.
5236 if (ms->use_skip_mb_code)
480 5236 put_bits(&s->pb, 1, 0); /* mb coded */
481 5236 put_bits(&s->pb,
482 5236 ff_table_mb_non_intra[cbp][1],
483 5236 ff_table_mb_non_intra[cbp][0]);
484 }
485 18394 put_bits(&s->pb, 1, 0); /* no AC prediction yet */
486
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 18394 times.
18394 if (s->c.inter_intra_pred) {
487 s->c.h263_aic_dir = 0;
488 put_bits(&s->pb, ff_table_inter_intra[s->c.h263_aic_dir][1],
489 ff_table_inter_intra[s->c.h263_aic_dir][0]);
490 }
491 }
492 26536 s->misc_bits += get_bits_diff(s);
493
494
2/2
✓ Branch 0 taken 159216 times.
✓ Branch 1 taken 26536 times.
185752 for (i = 0; i < 6; i++) {
495 159216 ff_msmpeg4_encode_block(s, block[i], i);
496 }
497 26536 s->i_tex_bits += get_bits_diff(s);
498 26536 s->i_count++;
499 }
500 }
501
502 208050 static void msmpeg4_encode_dc(MSMPEG4EncContext *const ms, int level, int n, int *dir_ptr)
503 {
504 208050 MPVEncContext *const s = &ms->m.s;
505 int sign, code;
506 int pred;
507
508 int16_t *dc_val;
509 208050 pred = ff_msmpeg4_pred_dc(&s->c, n, &dc_val, dir_ptr);
510
511 /* update predictor */
512
2/2
✓ Branch 0 taken 138700 times.
✓ Branch 1 taken 69350 times.
208050 if (n < 4) {
513 138700 *dc_val = level * s->c.y_dc_scale;
514 } else {
515 69350 *dc_val = level * s->c.c_dc_scale;
516 }
517
518 /* do the prediction */
519 208050 level -= pred;
520
521
2/2
✓ Branch 0 taken 48852 times.
✓ Branch 1 taken 159198 times.
208050 if (s->c.msmpeg4_version <= MSMP4_V2) {
522
2/2
✓ Branch 0 taken 32568 times.
✓ Branch 1 taken 16284 times.
48852 if (n < 4) {
523 32568 put_bits(&s->pb,
524 32568 ff_v2_dc_lum_table[level + 256][1],
525 32568 ff_v2_dc_lum_table[level + 256][0]);
526 }else{
527 16284 put_bits(&s->pb,
528 16284 ff_v2_dc_chroma_table[level + 256][1],
529 16284 ff_v2_dc_chroma_table[level + 256][0]);
530 }
531 }else{
532 159198 sign = 0;
533
2/2
✓ Branch 0 taken 69483 times.
✓ Branch 1 taken 89715 times.
159198 if (level < 0) {
534 69483 level = -level;
535 69483 sign = 1;
536 }
537 159198 code = level;
538
2/2
✓ Branch 0 taken 46 times.
✓ Branch 1 taken 159152 times.
159198 if (code > DC_MAX)
539 46 code = DC_MAX;
540
541 159198 put_bits(&s->pb, ff_msmp4_dc_tables[ms->dc_table_index][n >= 4][code][1],
542 159198 ff_msmp4_dc_tables[ms->dc_table_index][n >= 4][code][0]);
543
544
2/2
✓ Branch 0 taken 46 times.
✓ Branch 1 taken 159152 times.
159198 if (code == DC_MAX)
545 46 put_bits(&s->pb, 8, level);
546
547
2/2
✓ Branch 0 taken 144586 times.
✓ Branch 1 taken 14612 times.
159198 if (level != 0) {
548 144586 put_bits(&s->pb, 1, sign);
549 }
550 }
551 208050 }
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. */
555 1482246 void ff_msmpeg4_encode_block(MPVEncContext *const s, int16_t * block, int n)
556 {
557 1482246 MSMPEG4EncContext *const ms = (MSMPEG4EncContext*)s;
558 int level, run, last, i, j, last_index;
559 int last_non_zero, sign, slevel;
560 int code, run_diff, dc_pred_dir;
561 const RLTable *rl;
562 const uint8_t *scantable;
563
564
2/2
✓ Branch 0 taken 208050 times.
✓ Branch 1 taken 1274196 times.
1482246 if (s->c.mb_intra) {
565 208050 msmpeg4_encode_dc(ms, block[0], n, &dc_pred_dir);
566 208050 i = 1;
567
2/2
✓ Branch 0 taken 138700 times.
✓ Branch 1 taken 69350 times.
208050 if (n < 4) {
568 138700 rl = &ff_rl_table[ms->rl_table_index];
569 } else {
570 69350 rl = &ff_rl_table[3 + ms->rl_chroma_table_index];
571 }
572 208050 run_diff = s->c.msmpeg4_version >= MSMP4_WMV1;
573 208050 scantable = s->c.intra_scantable.permutated;
574 } else {
575 1274196 i = 0;
576 1274196 rl = &ff_rl_table[3 + ms->rl_table_index];
577 1274196 run_diff = s->c.msmpeg4_version > MSMP4_V2;
578 1274196 scantable = s->c.inter_scantable.permutated;
579 }
580
581 /* recalculate block_last_index for M$ wmv1 */
582
4/4
✓ Branch 0 taken 713544 times.
✓ Branch 1 taken 768702 times.
✓ Branch 2 taken 277524 times.
✓ Branch 3 taken 436020 times.
1482246 if (s->c.msmpeg4_version >= MSMP4_WMV1 && s->c.block_last_index[n] > 0) {
583
1/2
✓ Branch 0 taken 9923648 times.
✗ Branch 1 not taken.
9923648 for(last_index=63; last_index>=0; last_index--){
584
2/2
✓ Branch 0 taken 277524 times.
✓ Branch 1 taken 9646124 times.
9923648 if(block[scantable[last_index]]) break;
585 }
586 277524 s->c.block_last_index[n] = last_index;
587 }else
588 1204722 last_index = s->c.block_last_index[n];
589 /* AC coefs */
590 1482246 last_non_zero = i - 1;
591
2/2
✓ Branch 0 taken 17431821 times.
✓ Branch 1 taken 1482246 times.
18914067 for (; i <= last_index; i++) {
592 17431821 j = scantable[i];
593 17431821 level = block[j];
594
2/2
✓ Branch 0 taken 4396756 times.
✓ Branch 1 taken 13035065 times.
17431821 if (level) {
595 4396756 run = i - last_non_zero - 1;
596 4396756 last = (i == last_index);
597 4396756 sign = 0;
598 4396756 slevel = level;
599
2/2
✓ Branch 0 taken 2195832 times.
✓ Branch 1 taken 2200924 times.
4396756 if (level < 0) {
600 2195832 sign = 1;
601 2195832 level = -level;
602 }
603
604
2/4
✓ Branch 0 taken 4396756 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 4396756 times.
✗ Branch 3 not taken.
4396756 if(level<=MAX_LEVEL && run<=MAX_RUN){
605 4396756 ms->ac_stats[s->c.mb_intra][n>3][level][run][last]++;
606 }
607
608 4396756 ms->ac_stats[s->c.mb_intra][n > 3][40][63][0]++; //esc3 like
609
610 4396756 code = get_rl_index(rl, last, run, level);
611 4396756 put_bits(&s->pb, rl->table_vlc[code][1], rl->table_vlc[code][0]);
612
2/2
✓ Branch 0 taken 136660 times.
✓ Branch 1 taken 4260096 times.
4396756 if (code == rl->n) {
613 int level1, run1;
614
615 136660 level1 = level - rl->max_level[last][run];
616
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 136660 times.
136660 if (level1 < 1)
617 goto esc2;
618 136660 code = get_rl_index(rl, last, run, level1);
619
2/2
✓ Branch 0 taken 74016 times.
✓ Branch 1 taken 62644 times.
136660 if (code == rl->n) {
620 74016 esc2:
621 74016 put_bits(&s->pb, 1, 0);
622
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 74016 times.
74016 if (level > MAX_LEVEL)
623 goto esc3;
624 74016 run1 = run - rl->max_run[last][level] - run_diff;
625
2/2
✓ Branch 0 taken 403 times.
✓ Branch 1 taken 73613 times.
74016 if (run1 < 0)
626 403 goto esc3;
627 73613 code = get_rl_index(rl, last, run1+1, level);
628
4/4
✓ Branch 0 taken 19303 times.
✓ Branch 1 taken 54310 times.
✓ Branch 2 taken 5317 times.
✓ Branch 3 taken 13986 times.
73613 if (s->c.msmpeg4_version == MSMP4_WMV1 && code == rl->n)
629 5317 goto esc3;
630 68296 code = get_rl_index(rl, last, run1, level);
631
2/2
✓ Branch 0 taken 13568 times.
✓ Branch 1 taken 54728 times.
68296 if (code == rl->n) {
632 13568 esc3:
633 /* third escape */
634 19288 put_bits(&s->pb, 1, 0);
635 19288 put_bits(&s->pb, 1, last);
636
2/2
✓ Branch 0 taken 6445 times.
✓ Branch 1 taken 12843 times.
19288 if (s->c.msmpeg4_version >= MSMP4_WMV1) {
637
2/2
✓ Branch 0 taken 280 times.
✓ Branch 1 taken 6165 times.
6445 if (s->esc3_level_length == 0) {
638 280 s->esc3_level_length = 8;
639 280 ms->esc3_run_length = 6;
640 //ESCLVLSZ + ESCRUNSZ
641
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 280 times.
280 if (s->c.qscale < 8)
642 put_bits(&s->pb, 6, 3);
643 else
644 280 put_bits(&s->pb, 8, 3);
645 }
646 6445 put_bits(&s->pb, ms->esc3_run_length, run);
647 6445 put_bits(&s->pb, 1, sign);
648 6445 put_bits(&s->pb, s->esc3_level_length, level);
649 }else{
650 12843 put_bits(&s->pb, 6, run);
651 12843 put_sbits(&s->pb, 8, slevel);
652 }
653 } else {
654 /* second escape */
655 54728 put_bits(&s->pb, 1, 1);
656 54728 put_bits(&s->pb, rl->table_vlc[code][1], rl->table_vlc[code][0]);
657 54728 put_bits(&s->pb, 1, sign);
658 }
659 } else {
660 /* first escape */
661 62644 put_bits(&s->pb, 1, 1);
662 62644 put_bits(&s->pb, rl->table_vlc[code][1], rl->table_vlc[code][0]);
663 62644 put_bits(&s->pb, 1, sign);
664 }
665 } else {
666 4260096 put_bits(&s->pb, 1, sign);
667 }
668 4396756 last_non_zero = i;
669 }
670 }
671 1482246 }
672
673 17 av_cold void ff_msmpeg4_encode_init(MPVMainEncContext *const m)
674 {
675 17 MPVEncContext *const s = &m->s;
676 static AVOnce init_static_once = AV_ONCE_INIT;
677
678 17 ff_msmpeg4_common_init(&s->c);
679
680
2/2
✓ Branch 0 taken 13 times.
✓ Branch 1 taken 4 times.
17 if (s->c.msmpeg4_version <= MSMP4_WMV1) {
681 13 m->encode_picture_header = msmpeg4_encode_picture_header;
682 13 s->encode_mb = msmpeg4_encode_mb;
683 }
684
685
2/2
✓ Branch 0 taken 8 times.
✓ Branch 1 taken 9 times.
17 if (s->c.msmpeg4_version >= MSMP4_WMV1) {
686 8 s->min_qcoeff = -255;
687 8 s->max_qcoeff = 255;
688 }
689
690 /* init various encoding tables */
691 17 ff_thread_once(&init_static_once, msmpeg4_encode_init_static);
692 17 }
693
694 const FFCodec ff_msmpeg4v2_encoder = {
695 .p.name = "msmpeg4v2",
696 CODEC_LONG_NAME("MPEG-4 part 2 Microsoft variant version 2"),
697 .p.type = AVMEDIA_TYPE_VIDEO,
698 .p.id = AV_CODEC_ID_MSMPEG4V2,
699 CODEC_PIXFMTS(AV_PIX_FMT_YUV420P),
700 .color_ranges = AVCOL_RANGE_MPEG,
701 .p.priv_class = &ff_mpv_enc_class,
702 .p.capabilities = AV_CODEC_CAP_DR1 | AV_CODEC_CAP_ENCODER_REORDERED_OPAQUE,
703 .caps_internal = FF_CODEC_CAP_INIT_CLEANUP,
704 .priv_data_size = sizeof(MSMPEG4EncContext),
705 .init = ff_mpv_encode_init,
706 FF_CODEC_ENCODE_CB(ff_mpv_encode_picture),
707 .close = ff_mpv_encode_end,
708 };
709
710 const FFCodec ff_msmpeg4v3_encoder = {
711 .p.name = "msmpeg4",
712 CODEC_LONG_NAME("MPEG-4 part 2 Microsoft variant version 3"),
713 .p.type = AVMEDIA_TYPE_VIDEO,
714 .p.id = AV_CODEC_ID_MSMPEG4V3,
715 CODEC_PIXFMTS(AV_PIX_FMT_YUV420P),
716 .color_ranges = AVCOL_RANGE_MPEG,
717 .p.priv_class = &ff_mpv_enc_class,
718 .p.capabilities = AV_CODEC_CAP_DR1 | AV_CODEC_CAP_ENCODER_REORDERED_OPAQUE,
719 .caps_internal = FF_CODEC_CAP_INIT_CLEANUP,
720 .priv_data_size = sizeof(MSMPEG4EncContext),
721 .init = ff_mpv_encode_init,
722 FF_CODEC_ENCODE_CB(ff_mpv_encode_picture),
723 .close = ff_mpv_encode_end,
724 };
725
726 const FFCodec ff_wmv1_encoder = {
727 .p.name = "wmv1",
728 CODEC_LONG_NAME("Windows Media Video 7"),
729 .p.type = AVMEDIA_TYPE_VIDEO,
730 .p.id = AV_CODEC_ID_WMV1,
731 CODEC_PIXFMTS(AV_PIX_FMT_YUV420P),
732 .color_ranges = AVCOL_RANGE_MPEG,
733 .p.priv_class = &ff_mpv_enc_class,
734 .p.capabilities = AV_CODEC_CAP_DR1 | AV_CODEC_CAP_ENCODER_REORDERED_OPAQUE,
735 .caps_internal = FF_CODEC_CAP_INIT_CLEANUP,
736 .priv_data_size = sizeof(MSMPEG4EncContext),
737 .init = ff_mpv_encode_init,
738 FF_CODEC_ENCODE_CB(ff_mpv_encode_picture),
739 .close = ff_mpv_encode_end,
740 };
741