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