blob: 144468b24f16df8650be3dd91207ab26067e16a6
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/mem.h" |
36 | #include "mpegvideo.h" |
37 | #include "h263.h" |
38 | #include "internal.h" |
39 | #include "mpeg4video.h" |
40 | #include "msmpeg4.h" |
41 | #include "msmpeg4data.h" |
42 | #include "put_bits.h" |
43 | #include "rl.h" |
44 | #include "vc1data.h" |
45 | |
46 | static uint8_t rl_length[NB_RL_TABLES][MAX_LEVEL+1][MAX_RUN+1][2]; |
47 | |
48 | /* build the table which associate a (x,y) motion vector to a vlc */ |
49 | static av_cold int init_mv_table(MVTable *tab) |
50 | { |
51 | int i, x, y; |
52 | |
53 | tab->table_mv_index = av_malloc(sizeof(uint16_t) * 4096); |
54 | if (!tab->table_mv_index) |
55 | return AVERROR(ENOMEM); |
56 | |
57 | /* mark all entries as not used */ |
58 | for(i=0;i<4096;i++) |
59 | tab->table_mv_index[i] = tab->n; |
60 | |
61 | for(i=0;i<tab->n;i++) { |
62 | x = tab->table_mvx[i]; |
63 | y = tab->table_mvy[i]; |
64 | tab->table_mv_index[(x << 6) | y] = i; |
65 | } |
66 | |
67 | return 0; |
68 | } |
69 | |
70 | void ff_msmpeg4_code012(PutBitContext *pb, int n) |
71 | { |
72 | if (n == 0) { |
73 | put_bits(pb, 1, 0); |
74 | } else { |
75 | put_bits(pb, 1, 1); |
76 | put_bits(pb, 1, (n >= 2)); |
77 | } |
78 | } |
79 | |
80 | static int get_size_of_code(MpegEncContext * s, RLTable *rl, int last, int run, int level, int intra){ |
81 | int size=0; |
82 | int code; |
83 | int run_diff= intra ? 0 : 1; |
84 | |
85 | code = get_rl_index(rl, last, run, level); |
86 | size+= rl->table_vlc[code][1]; |
87 | if (code == rl->n) { |
88 | int level1, run1; |
89 | |
90 | level1 = level - rl->max_level[last][run]; |
91 | if (level1 < 1) |
92 | goto esc2; |
93 | code = get_rl_index(rl, last, run, level1); |
94 | if (code == rl->n) { |
95 | esc2: |
96 | size++; |
97 | if (level > MAX_LEVEL) |
98 | goto esc3; |
99 | run1 = run - rl->max_run[last][level] - run_diff; |
100 | if (run1 < 0) |
101 | goto esc3; |
102 | code = get_rl_index(rl, last, run1, level); |
103 | if (code == rl->n) { |
104 | esc3: |
105 | /* third escape */ |
106 | size+=1+1+6+8; |
107 | } else { |
108 | /* second escape */ |
109 | size+= 1+1+ rl->table_vlc[code][1]; |
110 | } |
111 | } else { |
112 | /* first escape */ |
113 | size+= 1+1+ rl->table_vlc[code][1]; |
114 | } |
115 | } else { |
116 | size++; |
117 | } |
118 | return size; |
119 | } |
120 | |
121 | av_cold int ff_msmpeg4_encode_init(MpegEncContext *s) |
122 | { |
123 | static int init_done=0; |
124 | int i, ret; |
125 | |
126 | ff_msmpeg4_common_init(s); |
127 | if(s->msmpeg4_version>=4){ |
128 | s->min_qcoeff= -255; |
129 | s->max_qcoeff= 255; |
130 | } |
131 | |
132 | if (!init_done) { |
133 | /* init various encoding tables */ |
134 | init_done = 1; |
135 | if ((ret = init_mv_table(&ff_mv_tables[0])) < 0) |
136 | return ret; |
137 | if ((ret = init_mv_table(&ff_mv_tables[1])) < 0) |
138 | return ret; |
139 | for(i=0;i<NB_RL_TABLES;i++) |
140 | ff_rl_init(&ff_rl_table[i], ff_static_rl_table_store[i]); |
141 | |
142 | for(i=0; i<NB_RL_TABLES; i++){ |
143 | int level; |
144 | for (level = 1; level <= MAX_LEVEL; level++) { |
145 | int run; |
146 | for(run=0; run<=MAX_RUN; run++){ |
147 | int last; |
148 | for(last=0; last<2; last++){ |
149 | rl_length[i][level][run][last]= get_size_of_code(s, &ff_rl_table[ i], last, run, level, 0); |
150 | } |
151 | } |
152 | } |
153 | } |
154 | } |
155 | |
156 | return 0; |
157 | } |
158 | |
159 | static void find_best_tables(MpegEncContext * s) |
160 | { |
161 | int i; |
162 | int best = 0, best_size = INT_MAX; |
163 | int chroma_best = 0, best_chroma_size = INT_MAX; |
164 | |
165 | for(i=0; i<3; i++){ |
166 | int level; |
167 | int chroma_size=0; |
168 | int size=0; |
169 | |
170 | if(i>0){// ;) |
171 | size++; |
172 | chroma_size++; |
173 | } |
174 | for(level=0; level<=MAX_LEVEL; level++){ |
175 | int run; |
176 | for(run=0; run<=MAX_RUN; run++){ |
177 | int last; |
178 | const int last_size= size + chroma_size; |
179 | for(last=0; last<2; last++){ |
180 | int inter_count = s->ac_stats[0][0][level][run][last] + s->ac_stats[0][1][level][run][last]; |
181 | int intra_luma_count = s->ac_stats[1][0][level][run][last]; |
182 | int intra_chroma_count= s->ac_stats[1][1][level][run][last]; |
183 | |
184 | if(s->pict_type==AV_PICTURE_TYPE_I){ |
185 | size += intra_luma_count *rl_length[i ][level][run][last]; |
186 | chroma_size+= intra_chroma_count*rl_length[i+3][level][run][last]; |
187 | }else{ |
188 | size+= intra_luma_count *rl_length[i ][level][run][last] |
189 | +intra_chroma_count*rl_length[i+3][level][run][last] |
190 | +inter_count *rl_length[i+3][level][run][last]; |
191 | } |
192 | } |
193 | if(last_size == size+chroma_size) break; |
194 | } |
195 | } |
196 | if(size<best_size){ |
197 | best_size= size; |
198 | best= i; |
199 | } |
200 | if(chroma_size<best_chroma_size){ |
201 | best_chroma_size= chroma_size; |
202 | chroma_best= i; |
203 | } |
204 | } |
205 | |
206 | if(s->pict_type==AV_PICTURE_TYPE_P) chroma_best= best; |
207 | |
208 | memset(s->ac_stats, 0, sizeof(int)*(MAX_LEVEL+1)*(MAX_RUN+1)*2*2*2); |
209 | |
210 | s->rl_table_index = best; |
211 | s->rl_chroma_table_index= chroma_best; |
212 | |
213 | if(s->pict_type != s->last_non_b_pict_type){ |
214 | s->rl_table_index= 2; |
215 | if(s->pict_type==AV_PICTURE_TYPE_I) |
216 | s->rl_chroma_table_index= 1; |
217 | else |
218 | s->rl_chroma_table_index= 2; |
219 | } |
220 | |
221 | } |
222 | |
223 | /* write MSMPEG4 compatible frame header */ |
224 | void ff_msmpeg4_encode_picture_header(MpegEncContext * s, int picture_number) |
225 | { |
226 | find_best_tables(s); |
227 | |
228 | avpriv_align_put_bits(&s->pb); |
229 | put_bits(&s->pb, 2, s->pict_type - 1); |
230 | |
231 | put_bits(&s->pb, 5, s->qscale); |
232 | if(s->msmpeg4_version<=2){ |
233 | s->rl_table_index = 2; |
234 | s->rl_chroma_table_index = 2; |
235 | } |
236 | |
237 | s->dc_table_index = 1; |
238 | s->mv_table_index = 1; /* only if P-frame */ |
239 | s->use_skip_mb_code = 1; /* only if P-frame */ |
240 | s->per_mb_rl_table = 0; |
241 | if(s->msmpeg4_version==4) |
242 | s->inter_intra_pred= (s->width*s->height < 320*240 && s->bit_rate<=II_BITRATE && s->pict_type==AV_PICTURE_TYPE_P); |
243 | ff_dlog(s, "%d %"PRId64" %d %d %d\n", s->pict_type, s->bit_rate, |
244 | s->inter_intra_pred, s->width, s->height); |
245 | |
246 | if (s->pict_type == AV_PICTURE_TYPE_I) { |
247 | s->slice_height= s->mb_height/1; |
248 | put_bits(&s->pb, 5, 0x16 + s->mb_height/s->slice_height); |
249 | |
250 | if(s->msmpeg4_version==4){ |
251 | ff_msmpeg4_encode_ext_header(s); |
252 | if(s->bit_rate>MBAC_BITRATE) |
253 | put_bits(&s->pb, 1, s->per_mb_rl_table); |
254 | } |
255 | |
256 | if(s->msmpeg4_version>2){ |
257 | if(!s->per_mb_rl_table){ |
258 | ff_msmpeg4_code012(&s->pb, s->rl_chroma_table_index); |
259 | ff_msmpeg4_code012(&s->pb, s->rl_table_index); |
260 | } |
261 | |
262 | put_bits(&s->pb, 1, s->dc_table_index); |
263 | } |
264 | } else { |
265 | put_bits(&s->pb, 1, s->use_skip_mb_code); |
266 | |
267 | if(s->msmpeg4_version==4 && s->bit_rate>MBAC_BITRATE) |
268 | put_bits(&s->pb, 1, s->per_mb_rl_table); |
269 | |
270 | if(s->msmpeg4_version>2){ |
271 | if(!s->per_mb_rl_table) |
272 | ff_msmpeg4_code012(&s->pb, s->rl_table_index); |
273 | |
274 | put_bits(&s->pb, 1, s->dc_table_index); |
275 | |
276 | put_bits(&s->pb, 1, s->mv_table_index); |
277 | } |
278 | } |
279 | |
280 | s->esc3_level_length= 0; |
281 | s->esc3_run_length= 0; |
282 | } |
283 | |
284 | void ff_msmpeg4_encode_ext_header(MpegEncContext * s) |
285 | { |
286 | unsigned fps = s->avctx->time_base.den / s->avctx->time_base.num / FFMAX(s->avctx->ticks_per_frame, 1); |
287 | put_bits(&s->pb, 5, FFMIN(fps, 31)); //yes 29.97 -> 29 |
288 | |
289 | put_bits(&s->pb, 11, FFMIN(s->bit_rate/1024, 2047)); |
290 | |
291 | if(s->msmpeg4_version>=3) |
292 | put_bits(&s->pb, 1, s->flipflop_rounding); |
293 | else |
294 | av_assert0(s->flipflop_rounding==0); |
295 | } |
296 | |
297 | void ff_msmpeg4_encode_motion(MpegEncContext * s, |
298 | int mx, int my) |
299 | { |
300 | int code; |
301 | MVTable *mv; |
302 | |
303 | /* modulo encoding */ |
304 | /* WARNING : you cannot reach all the MVs even with the modulo |
305 | encoding. This is a somewhat strange compromise they took !!! */ |
306 | if (mx <= -64) |
307 | mx += 64; |
308 | else if (mx >= 64) |
309 | mx -= 64; |
310 | if (my <= -64) |
311 | my += 64; |
312 | else if (my >= 64) |
313 | my -= 64; |
314 | |
315 | mx += 32; |
316 | my += 32; |
317 | mv = &ff_mv_tables[s->mv_table_index]; |
318 | |
319 | code = mv->table_mv_index[(mx << 6) | my]; |
320 | put_bits(&s->pb, |
321 | mv->table_mv_bits[code], |
322 | mv->table_mv_code[code]); |
323 | if (code == mv->n) { |
324 | /* escape : code literally */ |
325 | put_bits(&s->pb, 6, mx); |
326 | put_bits(&s->pb, 6, my); |
327 | } |
328 | } |
329 | |
330 | void ff_msmpeg4_handle_slices(MpegEncContext *s){ |
331 | if (s->mb_x == 0) { |
332 | if (s->slice_height && (s->mb_y % s->slice_height) == 0) { |
333 | if(s->msmpeg4_version < 4){ |
334 | ff_mpeg4_clean_buffers(s); |
335 | } |
336 | s->first_slice_line = 1; |
337 | } else { |
338 | s->first_slice_line = 0; |
339 | } |
340 | } |
341 | } |
342 | |
343 | static void msmpeg4v2_encode_motion(MpegEncContext * s, int val) |
344 | { |
345 | int range, bit_size, sign, code, bits; |
346 | |
347 | if (val == 0) { |
348 | /* zero vector */ |
349 | code = 0; |
350 | put_bits(&s->pb, ff_mvtab[code][1], ff_mvtab[code][0]); |
351 | } else { |
352 | bit_size = s->f_code - 1; |
353 | range = 1 << bit_size; |
354 | if (val <= -64) |
355 | val += 64; |
356 | else if (val >= 64) |
357 | val -= 64; |
358 | |
359 | if (val >= 0) { |
360 | sign = 0; |
361 | } else { |
362 | val = -val; |
363 | sign = 1; |
364 | } |
365 | val--; |
366 | code = (val >> bit_size) + 1; |
367 | bits = val & (range - 1); |
368 | |
369 | put_bits(&s->pb, ff_mvtab[code][1] + 1, (ff_mvtab[code][0] << 1) | sign); |
370 | if (bit_size > 0) { |
371 | put_bits(&s->pb, bit_size, bits); |
372 | } |
373 | } |
374 | } |
375 | |
376 | void ff_msmpeg4_encode_mb(MpegEncContext * s, |
377 | int16_t block[6][64], |
378 | int motion_x, int motion_y) |
379 | { |
380 | int cbp, coded_cbp, i; |
381 | int pred_x, pred_y; |
382 | uint8_t *coded_block; |
383 | |
384 | ff_msmpeg4_handle_slices(s); |
385 | |
386 | if (!s->mb_intra) { |
387 | /* compute cbp */ |
388 | cbp = 0; |
389 | for (i = 0; i < 6; i++) { |
390 | if (s->block_last_index[i] >= 0) |
391 | cbp |= 1 << (5 - i); |
392 | } |
393 | if (s->use_skip_mb_code && (cbp | motion_x | motion_y) == 0) { |
394 | /* skip macroblock */ |
395 | put_bits(&s->pb, 1, 1); |
396 | s->last_bits++; |
397 | s->misc_bits++; |
398 | s->skip_count++; |
399 | |
400 | return; |
401 | } |
402 | if (s->use_skip_mb_code) |
403 | put_bits(&s->pb, 1, 0); /* mb coded */ |
404 | |
405 | if(s->msmpeg4_version<=2){ |
406 | put_bits(&s->pb, |
407 | ff_v2_mb_type[cbp&3][1], |
408 | ff_v2_mb_type[cbp&3][0]); |
409 | if((cbp&3) != 3) coded_cbp= cbp ^ 0x3C; |
410 | else coded_cbp= cbp; |
411 | |
412 | put_bits(&s->pb, |
413 | ff_h263_cbpy_tab[coded_cbp>>2][1], |
414 | ff_h263_cbpy_tab[coded_cbp>>2][0]); |
415 | |
416 | s->misc_bits += get_bits_diff(s); |
417 | |
418 | ff_h263_pred_motion(s, 0, 0, &pred_x, &pred_y); |
419 | msmpeg4v2_encode_motion(s, motion_x - pred_x); |
420 | msmpeg4v2_encode_motion(s, motion_y - pred_y); |
421 | }else{ |
422 | put_bits(&s->pb, |
423 | ff_table_mb_non_intra[cbp + 64][1], |
424 | ff_table_mb_non_intra[cbp + 64][0]); |
425 | |
426 | s->misc_bits += get_bits_diff(s); |
427 | |
428 | /* motion vector */ |
429 | ff_h263_pred_motion(s, 0, 0, &pred_x, &pred_y); |
430 | ff_msmpeg4_encode_motion(s, motion_x - pred_x, |
431 | motion_y - pred_y); |
432 | } |
433 | |
434 | s->mv_bits += get_bits_diff(s); |
435 | |
436 | for (i = 0; i < 6; i++) { |
437 | ff_msmpeg4_encode_block(s, block[i], i); |
438 | } |
439 | s->p_tex_bits += get_bits_diff(s); |
440 | } else { |
441 | /* compute cbp */ |
442 | cbp = 0; |
443 | coded_cbp = 0; |
444 | for (i = 0; i < 6; i++) { |
445 | int val, pred; |
446 | val = (s->block_last_index[i] >= 1); |
447 | cbp |= val << (5 - i); |
448 | if (i < 4) { |
449 | /* predict value for close blocks only for luma */ |
450 | pred = ff_msmpeg4_coded_block_pred(s, i, &coded_block); |
451 | *coded_block = val; |
452 | val = val ^ pred; |
453 | } |
454 | coded_cbp |= val << (5 - i); |
455 | } |
456 | |
457 | if(s->msmpeg4_version<=2){ |
458 | if (s->pict_type == AV_PICTURE_TYPE_I) { |
459 | put_bits(&s->pb, |
460 | ff_v2_intra_cbpc[cbp&3][1], ff_v2_intra_cbpc[cbp&3][0]); |
461 | } else { |
462 | if (s->use_skip_mb_code) |
463 | put_bits(&s->pb, 1, 0); /* mb coded */ |
464 | put_bits(&s->pb, |
465 | ff_v2_mb_type[(cbp&3) + 4][1], |
466 | ff_v2_mb_type[(cbp&3) + 4][0]); |
467 | } |
468 | put_bits(&s->pb, 1, 0); /* no AC prediction yet */ |
469 | put_bits(&s->pb, |
470 | ff_h263_cbpy_tab[cbp>>2][1], |
471 | ff_h263_cbpy_tab[cbp>>2][0]); |
472 | }else{ |
473 | if (s->pict_type == AV_PICTURE_TYPE_I) { |
474 | put_bits(&s->pb, |
475 | ff_msmp4_mb_i_table[coded_cbp][1], ff_msmp4_mb_i_table[coded_cbp][0]); |
476 | } else { |
477 | if (s->use_skip_mb_code) |
478 | put_bits(&s->pb, 1, 0); /* mb coded */ |
479 | put_bits(&s->pb, |
480 | ff_table_mb_non_intra[cbp][1], |
481 | ff_table_mb_non_intra[cbp][0]); |
482 | } |
483 | put_bits(&s->pb, 1, 0); /* no AC prediction yet */ |
484 | if(s->inter_intra_pred){ |
485 | s->h263_aic_dir=0; |
486 | put_bits(&s->pb, ff_table_inter_intra[s->h263_aic_dir][1], ff_table_inter_intra[s->h263_aic_dir][0]); |
487 | } |
488 | } |
489 | s->misc_bits += get_bits_diff(s); |
490 | |
491 | for (i = 0; i < 6; i++) { |
492 | ff_msmpeg4_encode_block(s, block[i], i); |
493 | } |
494 | s->i_tex_bits += get_bits_diff(s); |
495 | s->i_count++; |
496 | } |
497 | } |
498 | |
499 | static void msmpeg4_encode_dc(MpegEncContext * s, int level, int n, int *dir_ptr) |
500 | { |
501 | int sign, code; |
502 | int pred, av_uninit(extquant); |
503 | int extrabits = 0; |
504 | |
505 | int16_t *dc_val; |
506 | pred = ff_msmpeg4_pred_dc(s, n, &dc_val, dir_ptr); |
507 | |
508 | /* update predictor */ |
509 | if (n < 4) { |
510 | *dc_val = level * s->y_dc_scale; |
511 | } else { |
512 | *dc_val = level * s->c_dc_scale; |
513 | } |
514 | |
515 | /* do the prediction */ |
516 | level -= pred; |
517 | |
518 | if(s->msmpeg4_version<=2){ |
519 | if (n < 4) { |
520 | put_bits(&s->pb, |
521 | ff_v2_dc_lum_table[level + 256][1], |
522 | ff_v2_dc_lum_table[level + 256][0]); |
523 | }else{ |
524 | put_bits(&s->pb, |
525 | ff_v2_dc_chroma_table[level + 256][1], |
526 | ff_v2_dc_chroma_table[level + 256][0]); |
527 | } |
528 | }else{ |
529 | sign = 0; |
530 | if (level < 0) { |
531 | level = -level; |
532 | sign = 1; |
533 | } |
534 | code = level; |
535 | if (code > DC_MAX) |
536 | code = DC_MAX; |
537 | else if( s->msmpeg4_version>=6 ) { |
538 | if( s->qscale == 1 ) { |
539 | extquant = (level + 3) & 0x3; |
540 | code = ((level+3)>>2); |
541 | } else if( s->qscale == 2 ) { |
542 | extquant = (level + 1) & 0x1; |
543 | code = ((level+1)>>1); |
544 | } |
545 | } |
546 | |
547 | 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 | if (n < 4) { |
555 | put_bits(&s->pb, ff_table1_dc_lum[code][1], ff_table1_dc_lum[code][0]); |
556 | } else { |
557 | put_bits(&s->pb, ff_table1_dc_chroma[code][1], ff_table1_dc_chroma[code][0]); |
558 | } |
559 | } |
560 | |
561 | if(s->msmpeg4_version>=6 && s->qscale<=2) |
562 | extrabits = 3 - s->qscale; |
563 | |
564 | if (code == DC_MAX) |
565 | put_bits(&s->pb, 8 + extrabits, level); |
566 | else if(extrabits > 0)//== VC1 && s->qscale<=2 |
567 | put_bits(&s->pb, extrabits, extquant); |
568 | |
569 | if (level != 0) { |
570 | put_bits(&s->pb, 1, sign); |
571 | } |
572 | } |
573 | } |
574 | |
575 | /* Encoding of a block; very similar to MPEG-4 except for a different |
576 | * escape coding (same as H.263) and more VLC tables. */ |
577 | void ff_msmpeg4_encode_block(MpegEncContext * s, int16_t * block, int n) |
578 | { |
579 | int level, run, last, i, j, last_index; |
580 | int last_non_zero, sign, slevel; |
581 | int code, run_diff, dc_pred_dir; |
582 | const RLTable *rl; |
583 | const uint8_t *scantable; |
584 | |
585 | if (s->mb_intra) { |
586 | msmpeg4_encode_dc(s, block[0], n, &dc_pred_dir); |
587 | i = 1; |
588 | if (n < 4) { |
589 | rl = &ff_rl_table[s->rl_table_index]; |
590 | } else { |
591 | rl = &ff_rl_table[3 + s->rl_chroma_table_index]; |
592 | } |
593 | run_diff = s->msmpeg4_version>=4; |
594 | scantable= s->intra_scantable.permutated; |
595 | } else { |
596 | i = 0; |
597 | rl = &ff_rl_table[3 + s->rl_table_index]; |
598 | if(s->msmpeg4_version<=2) |
599 | run_diff = 0; |
600 | else |
601 | run_diff = 1; |
602 | scantable= s->inter_scantable.permutated; |
603 | } |
604 | |
605 | /* recalculate block_last_index for M$ wmv1 */ |
606 | if(s->msmpeg4_version>=4 && s->msmpeg4_version<6 && s->block_last_index[n]>0){ |
607 | for(last_index=63; last_index>=0; last_index--){ |
608 | if(block[scantable[last_index]]) break; |
609 | } |
610 | s->block_last_index[n]= last_index; |
611 | }else |
612 | last_index = s->block_last_index[n]; |
613 | /* AC coefs */ |
614 | last_non_zero = i - 1; |
615 | for (; i <= last_index; i++) { |
616 | j = scantable[i]; |
617 | level = block[j]; |
618 | if (level) { |
619 | run = i - last_non_zero - 1; |
620 | last = (i == last_index); |
621 | sign = 0; |
622 | slevel = level; |
623 | if (level < 0) { |
624 | sign = 1; |
625 | level = -level; |
626 | } |
627 | |
628 | if(level<=MAX_LEVEL && run<=MAX_RUN){ |
629 | s->ac_stats[s->mb_intra][n>3][level][run][last]++; |
630 | } |
631 | |
632 | s->ac_stats[s->mb_intra][n > 3][40][63][0]++; //esc3 like |
633 | |
634 | code = get_rl_index(rl, last, run, level); |
635 | put_bits(&s->pb, rl->table_vlc[code][1], rl->table_vlc[code][0]); |
636 | if (code == rl->n) { |
637 | int level1, run1; |
638 | |
639 | level1 = level - rl->max_level[last][run]; |
640 | if (level1 < 1) |
641 | goto esc2; |
642 | code = get_rl_index(rl, last, run, level1); |
643 | if (code == rl->n) { |
644 | esc2: |
645 | put_bits(&s->pb, 1, 0); |
646 | if (level > MAX_LEVEL) |
647 | goto esc3; |
648 | run1 = run - rl->max_run[last][level] - run_diff; |
649 | if (run1 < 0) |
650 | goto esc3; |
651 | code = get_rl_index(rl, last, run1+1, level); |
652 | if (s->msmpeg4_version == 4 && code == rl->n) |
653 | goto esc3; |
654 | code = get_rl_index(rl, last, run1, level); |
655 | if (code == rl->n) { |
656 | esc3: |
657 | /* third escape */ |
658 | put_bits(&s->pb, 1, 0); |
659 | put_bits(&s->pb, 1, last); |
660 | if(s->msmpeg4_version>=4){ |
661 | if(s->esc3_level_length==0){ |
662 | s->esc3_level_length=8; |
663 | s->esc3_run_length= 6; |
664 | //ESCLVLSZ + ESCRUNSZ |
665 | if(s->qscale<8) |
666 | put_bits(&s->pb, 6 + (s->msmpeg4_version>=6), 3); |
667 | else |
668 | put_bits(&s->pb, 8, 3); |
669 | } |
670 | put_bits(&s->pb, s->esc3_run_length, run); |
671 | put_bits(&s->pb, 1, sign); |
672 | put_bits(&s->pb, s->esc3_level_length, level); |
673 | }else{ |
674 | put_bits(&s->pb, 6, run); |
675 | put_sbits(&s->pb, 8, slevel); |
676 | } |
677 | } else { |
678 | /* second escape */ |
679 | put_bits(&s->pb, 1, 1); |
680 | put_bits(&s->pb, rl->table_vlc[code][1], rl->table_vlc[code][0]); |
681 | put_bits(&s->pb, 1, sign); |
682 | } |
683 | } else { |
684 | /* first escape */ |
685 | put_bits(&s->pb, 1, 1); |
686 | put_bits(&s->pb, rl->table_vlc[code][1], rl->table_vlc[code][0]); |
687 | put_bits(&s->pb, 1, sign); |
688 | } |
689 | } else { |
690 | put_bits(&s->pb, 1, sign); |
691 | } |
692 | last_non_zero = i; |
693 | } |
694 | } |
695 | } |
696 |