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