1 /*****************************************************************************
2 * frame.c: MPEG2 video transrating module
3 *****************************************************************************
4 * Copyright (C) 2003 VideoLAN
5 * Copyright (C) 2003 Antoine Missout
6 * Copyright (C) 2000-2003 Michel Lespinasse <walken@zoy.org>
7 * Copyright (C) 1999-2000 Aaron Holtzman <aholtzma@ess.engr.uvic.ca>
8 * $Id: frame.c,v 1.2 2004/03/03 11:39:06 massiot Exp $
10 * Authors: Christophe Massiot <massiot@via.ecp.fr>
11 * Laurent Aimar <fenrir@via.ecp.fr>
13 * Michel Lespinasse <walken@zoy.org>
14 * Aaron Holtzman <aholtzma@ess.engr.uvic.ca>
16 * This program is free software; you can redistribute it and/or modify
17 * it under the terms of the GNU General Public License as published by
18 * the Free Software Foundation; either version 2 of the License, or
19 * (at your option) any later version.
21 * This program is distributed in the hope that it will be useful,
22 * but WITHOUT ANY WARRANTY; without even the implied warranty of
23 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
24 * GNU General Public License for more details.
26 * You should have received a copy of the GNU General Public License
27 * along with this program; if not, write to the Free Software
28 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111, USA.
29 *****************************************************************************/
31 /*****************************************************************************
33 *****************************************************************************/
42 #include <vlc/input.h>
44 #include "transrate.h"
46 /****************************************************************************
47 * transrater, code from M2VRequantizer http://www.metakine.com/
48 ****************************************************************************/
50 /* This is awful magic --Meuuh */
51 //#define REACT_DELAY (1024.0*128.0)
52 #define REACT_DELAY (256.0)
58 #define QUANT_P_INC (0.1)
65 // - the quantiser is increment by one step
68 // - in P_FRAME we keep the original quantiser but drop the last coefficient
69 // if there is more than one
70 // - in B_FRAME we multiply the quantiser by a factor
72 // - I_FRAME is recoded when we're 5.0 * REACT_DELAY late
73 // - P_FRAME is recoded when we're 2.5 * REACT_DELAY late
74 // - B_FRAME are always recoded
76 // if we're getting *very* late (60 * REACT_DELAY)
78 // - intra blocks quantiser is incremented two step
79 // - drop a few coefficients but always keep the first one
91 #ifdef HAVE_BUILTIN_EXPECT
92 #define likely(x) __builtin_expect ((x) != 0, 1)
93 #define unlikely(x) __builtin_expect ((x) != 0, 0)
96 #define unlikely(x) (x)
99 #define BITS_IN_BUF (8)
101 #define LOG(msg) fprintf (stderr, msg)
102 #define LOGF(format, args...) fprintf (stderr, format, args)
104 static inline void bs_write( bs_transrate_t *s, unsigned int val, int n)
107 assert(!(val & (0xffffffffU << n)));
109 while (unlikely(n >= s->i_bit_out))
111 s->p_w[0] = (s->i_bit_out_cache << s->i_bit_out ) | (val >> (n - s->i_bit_out));
114 s->i_bit_out_cache = 0;
115 val &= ~(0xffffffffU << n);
116 s->i_bit_out = BITS_IN_BUF;
121 s->i_bit_out_cache = (s->i_bit_out_cache << n) | val;
125 assert(s->i_bit_out > 0);
126 assert(s->i_bit_out <= BITS_IN_BUF);
129 static inline void bs_refill( bs_transrate_t *s )
131 assert((s->p_r - s->p_c) >= 1);
132 s->i_bit_in_cache |= s->p_c[0] << (24 - s->i_bit_in);
137 static inline void bs_flush( bs_transrate_t *s, unsigned int n )
139 assert(s->i_bit_in >= n);
141 s->i_bit_in_cache <<= n;
144 assert( (!n) || ((n>0) && !(s->i_bit_in_cache & 0x1)) );
146 while (unlikely(s->i_bit_in < 24)) bs_refill( s );
149 static inline unsigned int bs_read( bs_transrate_t *s, unsigned int n)
151 unsigned int Val = ((unsigned int)s->i_bit_in_cache) >> (32 - n);
156 static inline unsigned int bs_copy( bs_transrate_t *s, unsigned int n)
158 unsigned int Val = bs_read( s, n);
163 static inline void bs_flush_read( bs_transrate_t *s )
165 int i = s->i_bit_in & 0x7;
168 assert(((unsigned int)bs->i_bit_in_cache) >> (32 - i) == 0);
169 s->i_bit_in_cache <<= i;
172 s->p_c += -1 * (s->i_bit_in >> 3);
175 static inline void bs_flush_write( bs_transrate_t *s )
177 if( s->i_bit_out != 8 ) bs_write(s, 0, s->i_bit_out);
180 /////---- begin ext mpeg code
182 const uint8_t non_linear_mquant_table[32] =
184 0, 1, 2, 3, 4, 5, 6, 7,
185 8,10,12,14,16,18,20,22,
186 24,28,32,36,40,44,48,52,
187 56,64,72,80,88,96,104,112
189 const uint8_t map_non_linear_mquant[113] =
191 0,1,2,3,4,5,6,7,8,8,9,9,10,10,11,11,12,12,13,13,14,14,15,15,16,16,
192 16,17,17,17,18,18,18,18,19,19,19,19,20,20,20,20,21,21,21,21,22,22,
193 22,22,23,23,23,23,24,24,24,24,24,24,24,25,25,25,25,25,25,25,26,26,
194 26,26,26,26,26,26,27,27,27,27,27,27,27,27,28,28,28,28,28,28,28,29,
195 29,29,29,29,29,29,29,29,29,30,30,30,30,30,30,30,31,31,31,31,31
198 static int scale_quant( unsigned int q_scale_type, double quant )
203 iquant = (int) floor(quant+0.5);
205 /* clip mquant to legal (linear) range */
206 if (iquant<1) iquant = 1;
207 if (iquant>112) iquant = 112;
209 iquant = non_linear_mquant_table[map_non_linear_mquant[iquant]];
213 /* clip mquant to legal (linear) range */
214 iquant = (int)floor(quant+0.5);
215 if (iquant<2) iquant = 2;
216 if (iquant>62) iquant = 62;
217 iquant = (iquant/2)*2; // Must be *even*
222 static int increment_quant( transrate_t *tr, int quant )
224 if( tr->q_scale_type )
226 assert(quant >= 1 && quant <= 112 );
227 quant = map_non_linear_mquant[quant] + 1;
228 if( tr->picture_coding_type == P_TYPE )
229 quant += tr->level_p;
230 if( quant > 31) quant = 31;
231 quant = non_linear_mquant_table[quant];
235 assert(!(quant & 1));
237 if( tr->picture_coding_type == P_TYPE )
238 quant += 2 * tr->level_p;
239 if (quant > 62) quant = 62;
244 static inline int intmax( register int x, register int y )
246 return x < y ? y : x;
248 static inline int intmin( register int x, register int y )
250 return x < y ? x : y;
253 static int getNewQuant( transrate_t *tr, int curQuant)
255 bs_transrate_t *bs = &tr->bs;
257 double calc_quant, quant_to_use;
260 switch ( tr->picture_coding_type )
264 mquant = increment_quant( tr, curQuant );
268 tr->quant_corr = (((bs->i_byte_in - (bs->p_r - 4 - bs->p_c)) / tr->fact_x) - (bs->i_byte_out + (bs->p_w - bs->p_ow))) / REACT_DELAY + B_HANDICAP;
269 calc_quant = curQuant * tr->current_fact_x;
270 quant_to_use = calc_quant - tr->quant_corr;
272 mquant = intmax(scale_quant( tr->q_scale_type, quant_to_use), increment_quant( tr, curQuant) );
281 LOGF("type: %s orig_quant: %3i calc_quant: %7.1f quant_corr: %7.1f using_quant: %3i\n",
282 (picture_coding_type == I_TYPE ? "I_TYPE" : (picture_coding_type == P_TYPE ? "P_TYPE" : "B_TYPE")),
283 (int)curQuant, (float)calc_quant, (float)quant_corr, (int)mquant);
286 assert(mquant >= curQuant);
291 static inline int isNotEmpty(RunLevel *blk)
298 static void putAC( bs_transrate_t *bs, int run, int signed_level, int vlcformat)
301 const VLCtable *ptab = NULL;
303 level = (signed_level<0) ? -signed_level : signed_level; /* abs(signed_level) */
305 assert(!(run<0 || run>63 || level==0 || level>2047));
309 if (run<2 && level<41)
311 if (vlcformat) ptab = &dct_code_tab1a[run][level-1];
312 else ptab = &dct_code_tab1[run][level-1];
315 else if (run<32 && level<6)
317 if (vlcformat) ptab = &dct_code_tab2a[run-2][level-1];
318 else ptab = &dct_code_tab2[run-2][level-1];
322 if (len) /* a VLC code exists */
324 bs_write( bs, ptab->code, len);
325 bs_write( bs, signed_level<0, 1); /* sign */
329 bs_write( bs, 1l, 6); /* Escape */
330 bs_write( bs, run, 6); /* 6 bit code for run */
331 bs_write( bs, ((unsigned int)signed_level) & 0xFFF, 12);
336 static inline void putACfirst( bs_transrate_t *bs, int run, int val)
338 if (run==0 && (val==1 || val==-1)) bs_write( bs, 2|(val<0),2);
339 else putAC( bs, run,val,0);
342 static void putnonintrablk( bs_transrate_t *bs, RunLevel *blk)
346 putACfirst( bs, blk->run, blk->level);
351 putAC( bs, blk->run, blk->level, 0);
360 static const int non_linear_quantizer_scale [] =
362 0, 1, 2, 3, 4, 5, 6, 7,
363 8, 10, 12, 14, 16, 18, 20, 22,
364 24, 28, 32, 36, 40, 44, 48, 52,
365 56, 64, 72, 80, 88, 96, 104, 112
368 static inline int get_macroblock_modes( transrate_t *tr )
370 bs_transrate_t *bs = &tr->bs;
372 int macroblock_modes;
375 switch( tr->picture_coding_type)
379 tab = MB_I + UBITS (bs->i_bit_in_cache, 1);
380 bs_flush( bs, tab->len );
381 macroblock_modes = tab->modes;
383 if ((! ( tr->frame_pred_frame_dct)) && ( tr->picture_structure == FRAME_PICTURE))
385 macroblock_modes |= UBITS (bs->i_bit_in_cache, 1) * DCT_TYPE_INTERLACED;
389 return macroblock_modes;
393 tab = MB_P + UBITS (bs->i_bit_in_cache, 5);
394 bs_flush( bs, tab->len );
395 macroblock_modes = tab->modes;
397 if (tr->picture_structure != FRAME_PICTURE)
399 if (macroblock_modes & MACROBLOCK_MOTION_FORWARD)
401 macroblock_modes |= UBITS (bs->i_bit_in_cache, 2) * MOTION_TYPE_BASE;
404 return macroblock_modes;
406 else if (tr->frame_pred_frame_dct)
408 if (macroblock_modes & MACROBLOCK_MOTION_FORWARD)
409 macroblock_modes |= MC_FRAME;
410 return macroblock_modes;
414 if (macroblock_modes & MACROBLOCK_MOTION_FORWARD)
416 macroblock_modes |= UBITS (bs->i_bit_in_cache, 2) * MOTION_TYPE_BASE;
419 if (macroblock_modes & (MACROBLOCK_INTRA | MACROBLOCK_PATTERN))
421 macroblock_modes |= UBITS (bs->i_bit_in_cache, 1) * DCT_TYPE_INTERLACED;
424 return macroblock_modes;
429 tab = MB_B + UBITS (bs->i_bit_in_cache, 6);
430 bs_flush( bs, tab->len );
431 macroblock_modes = tab->modes;
433 if( tr->picture_structure != FRAME_PICTURE)
435 if (! (macroblock_modes & MACROBLOCK_INTRA))
437 macroblock_modes |= UBITS (bs->i_bit_in_cache, 2) * MOTION_TYPE_BASE;
440 return macroblock_modes;
442 else if (tr->frame_pred_frame_dct)
444 /* if (! (macroblock_modes & MACROBLOCK_INTRA)) */
445 macroblock_modes |= MC_FRAME;
446 return macroblock_modes;
450 if (macroblock_modes & MACROBLOCK_INTRA) goto intra;
451 macroblock_modes |= UBITS (bs->i_bit_in_cache, 2) * MOTION_TYPE_BASE;
453 if (macroblock_modes & (MACROBLOCK_INTRA | MACROBLOCK_PATTERN))
456 macroblock_modes |= UBITS (bs->i_bit_in_cache, 1) * DCT_TYPE_INTERLACED;
459 return macroblock_modes;
468 static inline int get_quantizer_scale( transrate_t *tr )
470 bs_transrate_t *bs = &tr->bs;
472 int quantizer_scale_code;
474 quantizer_scale_code = UBITS (bs->i_bit_in_cache, 5);
477 if( tr->q_scale_type )
478 return non_linear_quantizer_scale[quantizer_scale_code];
480 return quantizer_scale_code << 1;
483 static inline int get_motion_delta( bs_transrate_t *bs, const int f_code )
489 if (bs->i_bit_in_cache & 0x80000000)
494 else if (bs->i_bit_in_cache >= 0x0c000000)
497 tab = MV_4 + UBITS (bs->i_bit_in_cache, 4);
498 delta = (tab->delta << f_code) + 1;
499 bs_copy( bs, tab->len);
501 sign = SBITS (bs->i_bit_in_cache, 1);
504 if (f_code) delta += UBITS (bs->i_bit_in_cache, f_code);
505 bs_copy( bs, f_code);
507 return (delta ^ sign) - sign;
512 tab = MV_10 + UBITS (bs->i_bit_in_cache, 10);
513 delta = (tab->delta << f_code) + 1;
514 bs_copy( bs, tab->len);
516 sign = SBITS (bs->i_bit_in_cache, 1);
521 delta += UBITS (bs->i_bit_in_cache, f_code);
522 bs_copy( bs, f_code);
525 return (delta ^ sign) - sign;
530 static inline int get_dmv( bs_transrate_t *bs )
534 tab = DMV_2 + UBITS (bs->i_bit_in_cache, 2);
535 bs_copy( bs, tab->len);
539 static inline int get_coded_block_pattern( bs_transrate_t *bs )
543 if (bs->i_bit_in_cache >= 0x20000000)
545 tab = CBP_7 + (UBITS (bs->i_bit_in_cache, 7) - 16);
546 bs_flush( bs, tab->len );
551 tab = CBP_9 + UBITS (bs->i_bit_in_cache, 9);
552 bs_flush( bs, tab->len );
557 static inline int get_luma_dc_dct_diff( bs_transrate_t *bs )
563 if (bs->i_bit_in_cache < 0xf8000000)
565 tab = DC_lum_5 + UBITS (bs->i_bit_in_cache, 5);
569 bs_copy( bs, tab->len);
570 //dc_diff = UBITS (bs->i_bit_in_cache, size) - UBITS (SBITS (~bs->i_bit_in_cache, 1), size);
571 dc_diff = UBITS (bs->i_bit_in_cache, size);
572 if (!(dc_diff >> (size - 1))) dc_diff = (dc_diff + 1) - (1 << size);
584 tab = DC_long + (UBITS (bs->i_bit_in_cache, 9) - 0x1e0);
586 bs_copy( bs, tab->len);
587 //dc_diff = UBITS (bs->i_bit_in_cache, size) - UBITS (SBITS (~bs->i_bit_in_cache, 1), size);
588 dc_diff = UBITS (bs->i_bit_in_cache, size);
589 if (!(dc_diff >> (size - 1))) dc_diff = (dc_diff + 1) - (1 << size);
595 static inline int get_chroma_dc_dct_diff( bs_transrate_t *bs )
601 if (bs->i_bit_in_cache < 0xf8000000)
603 tab = DC_chrom_5 + UBITS (bs->i_bit_in_cache, 5);
607 bs_copy( bs, tab->len);
608 //dc_diff = UBITS (bs->i_bit_in_cache, size) - UBITS (SBITS (~bs->i_bit_in_cache, 1), size);
609 dc_diff = UBITS (bs->i_bit_in_cache, size);
610 if (!(dc_diff >> (size - 1))) dc_diff = (dc_diff + 1) - (1 << size);
621 tab = DC_long + (UBITS (bs->i_bit_in_cache, 10) - 0x3e0);
623 bs_copy( bs, tab->len + 1);
624 //dc_diff = UBITS (bs->i_bit_in_cache, size) - UBITS (SBITS (~bs->i_bit_in_cache, 1), size);
625 dc_diff = UBITS (bs->i_bit_in_cache, size);
626 if (!(dc_diff >> (size - 1))) dc_diff = (dc_diff + 1) - (1 << size);
632 static void get_intra_block_B14( bs_transrate_t *bs, const int i_qscale, const int i_qscale_new )
639 /* Basic sanity check --Meuuh */
645 tst = i_qscale_new/i_qscale + ((i_qscale_new%i_qscale) ? 1 : 0);
651 if (bs->i_bit_in_cache >= 0x28000000)
653 tab = DCT_B14AC_5 + (UBITS (bs->i_bit_in_cache, 5) - 5);
656 if (i >= 64) break; /* end of block */
659 bs_flush( bs, tab->len );
663 val = (val ^ SBITS (bs->i_bit_in_cache, 1)) - SBITS (bs->i_bit_in_cache, 1);
664 putAC( bs, i - li - 1, (val * i_qscale) / i_qscale_new, 0);
671 else if (bs->i_bit_in_cache >= 0x04000000)
673 tab = DCT_B14_8 + (UBITS (bs->i_bit_in_cache, 8) - 4);
676 if (i < 64) goto normal_code;
679 i += (UBITS (bs->i_bit_in_cache, 12) & 0x3F) - 64;
680 if (i >= 64) break; /* illegal, check needed to avoid buffer overflow */
683 val = SBITS (bs->i_bit_in_cache, 12);
686 putAC( bs, i - li - 1, (val * i_qscale) / i_qscale_new, 0);
694 else if (bs->i_bit_in_cache >= 0x02000000)
696 tab = DCT_B14_10 + (UBITS (bs->i_bit_in_cache, 10) - 8);
698 if (i < 64 ) goto normal_code;
700 else if (bs->i_bit_in_cache >= 0x00800000)
702 tab = DCT_13 + (UBITS (bs->i_bit_in_cache, 13) - 16);
704 if (i < 64 ) goto normal_code;
706 else if (bs->i_bit_in_cache >= 0x00200000)
708 tab = DCT_15 + (UBITS (bs->i_bit_in_cache, 15) - 16);
710 if (i < 64 ) goto normal_code;
714 tab = DCT_16 + UBITS (bs->i_bit_in_cache, 16);
717 if (i < 64 ) goto normal_code;
719 break; /* illegal, check needed to avoid buffer overflow */
722 bs_copy( bs, 2); /* end of block code */
725 static void get_intra_block_B15( bs_transrate_t *bs, const int i_qscale, int const i_qscale_new )
732 /* Basic sanity check --Meuuh */
738 tst = i_qscale_new/i_qscale + ((i_qscale_new%i_qscale) ? 1 : 0);
744 if (bs->i_bit_in_cache >= 0x04000000)
746 tab = DCT_B15_8 + (UBITS (bs->i_bit_in_cache, 8) - 4);
752 bs_flush( bs, tab->len );
757 val = (val ^ SBITS (bs->i_bit_in_cache, 1)) - SBITS (bs->i_bit_in_cache, 1);
758 putAC( bs, i - li - 1, (val * i_qscale) / i_qscale_new, 1);
767 i += (UBITS (bs->i_bit_in_cache, 12) & 0x3F) - 64;
769 if (i >= 64) break; /* illegal, check against buffer overflow */
772 val = SBITS (bs->i_bit_in_cache, 12);
775 putAC( bs, i - li - 1, (val * i_qscale) / i_qscale_new, 1);
783 else if (bs->i_bit_in_cache >= 0x02000000)
785 tab = DCT_B15_10 + (UBITS (bs->i_bit_in_cache, 10) - 8);
787 if (i < 64) goto normal_code;
789 else if (bs->i_bit_in_cache >= 0x00800000)
791 tab = DCT_13 + (UBITS (bs->i_bit_in_cache, 13) - 16);
793 if (i < 64) goto normal_code;
795 else if (bs->i_bit_in_cache >= 0x00200000)
797 tab = DCT_15 + (UBITS (bs->i_bit_in_cache, 15) - 16);
799 if (i < 64) goto normal_code;
803 tab = DCT_16 + UBITS (bs->i_bit_in_cache, 16);
806 if (i < 64) goto normal_code;
808 break; /* illegal, check needed to avoid buffer overflow */
811 bs_copy( bs, 4); /* end of block code */
815 static int get_non_intra_block_drop( transrate_t *tr, RunLevel *blk)
817 bs_transrate_t *bs = &tr->bs;
822 RunLevel *sblk = blk + 1;
826 if (bs->i_bit_in_cache >= 0x28000000)
828 tab = DCT_B14DC_5 + (UBITS (bs->i_bit_in_cache, 5) - 5);
835 if (bs->i_bit_in_cache >= 0x28000000)
837 tab = DCT_B14AC_5 + (UBITS (bs->i_bit_in_cache, 5) - 5);
841 if (i >= 64) break; /* end of block */
845 bs_flush( bs, tab->len );
847 val = (val ^ SBITS (bs->i_bit_in_cache, 1)) - SBITS (bs->i_bit_in_cache, 1); /* if (bitstream_get (1)) val = -val; */
850 blk->run = i - li - 1;
860 if (bs->i_bit_in_cache >= 0x04000000)
862 tab = DCT_B14_8 + (UBITS (bs->i_bit_in_cache, 8) - 4);
865 if (i < 64) goto normal_code;
869 i += (UBITS (bs->i_bit_in_cache, 12) & 0x3F) - 64;
871 if (i >= 64) break; /* illegal, check needed to avoid buffer overflow */
874 val = SBITS (bs->i_bit_in_cache, 12);
877 blk->run = i - li - 1;
884 else if (bs->i_bit_in_cache >= 0x02000000)
886 tab = DCT_B14_10 + (UBITS (bs->i_bit_in_cache, 10) - 8);
888 if (i < 64) goto normal_code;
890 else if (bs->i_bit_in_cache >= 0x00800000)
892 tab = DCT_13 + (UBITS (bs->i_bit_in_cache, 13) - 16);
894 if (i < 64) goto normal_code;
896 else if (bs->i_bit_in_cache >= 0x00200000)
898 tab = DCT_15 + (UBITS (bs->i_bit_in_cache, 15) - 16);
900 if (i < 64) goto normal_code;
904 tab = DCT_16 + UBITS (bs->i_bit_in_cache, 16);
907 if (i < 64) goto normal_code;
909 break; /* illegal, check needed to avoid buffer overflow */
911 bs_flush( bs, 2 ); /* dump end of block code */
919 // remove more coeffs if very late
920 if (tr->level_p >= 4 && (blk != sblk))
923 if (tr->level_p >= 5 && (blk != sblk))
926 if (tr->level_p >= 6 && (blk != sblk))
929 if (tr->level_p >= 7 && (blk != sblk))
940 static int get_non_intra_block_rq( bs_transrate_t *bs, RunLevel *blk, const int i_qscale, const int i_qscale_new )
947 /* Basic sanity check --Meuuh */
953 tst = i_qscale_new/i_qscale + ((i_qscale_new%i_qscale) ? 1 : 0);
957 if (bs->i_bit_in_cache >= 0x28000000)
959 tab = DCT_B14DC_5 + (UBITS (bs->i_bit_in_cache, 5) - 5);
966 if (bs->i_bit_in_cache >= 0x28000000)
968 tab = DCT_B14AC_5 + (UBITS (bs->i_bit_in_cache, 5) - 5);
973 break; /* end of block */
977 bs_flush( bs, tab->len );
981 val = (val ^ SBITS (bs->i_bit_in_cache, 1)) - SBITS (bs->i_bit_in_cache, 1);
982 blk->level = (val * i_qscale) / i_qscale_new;
983 blk->run = i - li - 1;
988 //if ( ((val) && (tab->level < tst)) || ((!val) && (tab->level >= tst)) )
989 // LOGF("level: %i val: %i tst : %i q: %i nq : %i\n", tab->level, val, tst, q, nq);
996 if (bs->i_bit_in_cache >= 0x04000000)
998 tab = DCT_B14_8 + (UBITS (bs->i_bit_in_cache, 8) - 4);
1001 if (i < 64) goto normal_code;
1005 i += (UBITS (bs->i_bit_in_cache, 12) & 0x3F) - 64;
1007 if (i >= 64) break; /* illegal, check needed to avoid buffer overflow */
1010 val = SBITS (bs->i_bit_in_cache, 12);
1011 if (abs(val) >= tst)
1013 blk->level = (val * i_qscale) / i_qscale_new;
1014 blk->run = i - li - 1;
1022 else if (bs->i_bit_in_cache >= 0x02000000)
1024 tab = DCT_B14_10 + (UBITS (bs->i_bit_in_cache, 10) - 8);
1026 if (i < 64) goto normal_code;
1028 else if (bs->i_bit_in_cache >= 0x00800000)
1030 tab = DCT_13 + (UBITS (bs->i_bit_in_cache, 13) - 16);
1032 if (i < 64) goto normal_code;
1034 else if (bs->i_bit_in_cache >= 0x00200000)
1036 tab = DCT_15 + (UBITS (bs->i_bit_in_cache, 15) - 16);
1038 if (i < 64) goto normal_code;
1042 tab = DCT_16 + UBITS (bs->i_bit_in_cache, 16);
1046 if (i < 64) goto normal_code;
1048 break; /* illegal, check needed to avoid buffer overflow */
1050 bs_flush( bs, 2 ); /* dump end of block code */
1057 static void motion_fr_frame( bs_transrate_t *bs, unsigned int f_code[2] )
1059 get_motion_delta( bs, f_code[0] );
1060 get_motion_delta( bs, f_code[1] );
1063 static void motion_fr_field( bs_transrate_t *bs, unsigned int f_code[2] )
1067 get_motion_delta( bs, f_code[0]);
1068 get_motion_delta( bs, f_code[1]);
1072 get_motion_delta( bs, f_code[0]);
1073 get_motion_delta( bs, f_code[1]);
1076 static void motion_fr_dmv( bs_transrate_t *bs, unsigned int f_code[2] )
1078 get_motion_delta( bs, f_code[0]);
1081 get_motion_delta( bs, f_code[1]);
1085 static void motion_fi_field( bs_transrate_t *bs, unsigned int f_code[2] )
1089 get_motion_delta( bs, f_code[0]);
1090 get_motion_delta( bs, f_code[1]);
1093 static void motion_fi_16x8( bs_transrate_t *bs, unsigned int f_code[2] )
1097 get_motion_delta( bs, f_code[0]);
1098 get_motion_delta( bs, f_code[1]);
1102 get_motion_delta( bs, f_code[0]);
1103 get_motion_delta( bs, f_code[1]);
1106 static void motion_fi_dmv( bs_transrate_t *bs, unsigned int f_code[2] )
1108 get_motion_delta( bs, f_code[0]);
1111 get_motion_delta( bs, f_code[1]);
1116 #define MOTION_CALL(routine,direction) \
1118 if ((direction) & MACROBLOCK_MOTION_FORWARD) \
1119 routine( bs, tr->f_code[0]); \
1120 if ((direction) & MACROBLOCK_MOTION_BACKWARD) \
1121 routine( bs, tr->f_code[1]); \
1124 #define NEXT_MACROBLOCK \
1126 tr->h_offset += 16; \
1127 if( tr->h_offset == tr->horizontal_size_value) \
1129 tr->v_offset += 16; \
1130 if (tr->v_offset > (tr->vertical_size_value - 16)) return; \
1135 static void putmbdata( transrate_t *tr, int macroblock_modes )
1137 bs_transrate_t *bs = &tr->bs;
1140 mbtypetab[tr->picture_coding_type-1][macroblock_modes&0x1F].code,
1141 mbtypetab[tr->picture_coding_type-1][macroblock_modes&0x1F].len);
1143 switch ( tr->picture_coding_type)
1146 if ((! (tr->frame_pred_frame_dct)) && (tr->picture_structure == FRAME_PICTURE))
1147 bs_write( bs, macroblock_modes & DCT_TYPE_INTERLACED ? 1 : 0, 1);
1151 if (tr->picture_structure != FRAME_PICTURE)
1153 if (macroblock_modes & MACROBLOCK_MOTION_FORWARD)
1154 bs_write( bs, (macroblock_modes & MOTION_TYPE_MASK) / MOTION_TYPE_BASE, 2);
1157 else if (tr->frame_pred_frame_dct) break;
1160 if (macroblock_modes & MACROBLOCK_MOTION_FORWARD)
1161 bs_write( bs, (macroblock_modes & MOTION_TYPE_MASK) / MOTION_TYPE_BASE, 2);
1162 if (macroblock_modes & (MACROBLOCK_INTRA | MACROBLOCK_PATTERN))
1163 bs_write( bs, macroblock_modes & DCT_TYPE_INTERLACED ? 1 : 0, 1);
1168 if (tr->picture_structure != FRAME_PICTURE)
1170 if (! (macroblock_modes & MACROBLOCK_INTRA))
1171 bs_write( bs, (macroblock_modes & MOTION_TYPE_MASK) / MOTION_TYPE_BASE, 2);
1174 else if (tr->frame_pred_frame_dct) break;
1177 if (macroblock_modes & MACROBLOCK_INTRA) goto intra;
1178 bs_write( bs, (macroblock_modes & MOTION_TYPE_MASK) / MOTION_TYPE_BASE, 2);
1179 if (macroblock_modes & (MACROBLOCK_INTRA | MACROBLOCK_PATTERN))
1182 bs_write( bs, macroblock_modes & DCT_TYPE_INTERLACED ? 1 : 0, 1);
1189 static inline void put_quantiser( transrate_t *tr )
1191 bs_transrate_t *bs = &tr->bs;
1193 bs_write( bs, tr->q_scale_type ? map_non_linear_mquant[tr->new_quantizer_scale] : tr->new_quantizer_scale >> 1, 5);
1194 tr->last_coded_scale = tr->new_quantizer_scale;
1197 static int slice_init( transrate_t *tr, int code)
1199 bs_transrate_t *bs = &tr->bs;
1203 tr->v_offset = (code - 1) * 16;
1205 tr->quantizer_scale = get_quantizer_scale( tr );
1206 if ( tr->picture_coding_type == P_TYPE)
1208 tr->new_quantizer_scale = tr->quantizer_scale;
1212 tr->new_quantizer_scale = getNewQuant(tr, tr->quantizer_scale);
1214 put_quantiser( tr );
1216 /*LOGF("************************\nstart of slice %i in %s picture. ori quant: %i new quant: %i\n", code,
1217 (picture_coding_type == I_TYPE ? "I_TYPE" : (picture_coding_type == P_TYPE ? "P_TYPE" : "B_TYPE")),
1218 quantizer_scale, new_quantizer_scale);*/
1220 /* ignore intra_slice and all the extra data */
1221 while (bs->i_bit_in_cache & 0x80000000)
1226 /* decode initial macroblock address increment */
1230 if (bs->i_bit_in_cache >= 0x08000000)
1232 mba = MBA_5 + (UBITS (bs->i_bit_in_cache, 6) - 2);
1235 else if (bs->i_bit_in_cache >= 0x01800000)
1237 mba = MBA_11 + (UBITS (bs->i_bit_in_cache, 12) - 24);
1240 else if( UBITS (bs->i_bit_in_cache, 12 ) == 8 )
1242 /* macroblock_escape */
1252 bs_copy( bs, mba->len + 1);
1253 tr->h_offset = (offset + mba->mba) << 4;
1255 while( tr->h_offset - (int)tr->horizontal_size_value >= 0)
1257 tr->h_offset -= tr->horizontal_size_value;
1261 if( tr->v_offset > tr->vertical_size_value - 16 )
1268 static void mpeg2_slice( transrate_t *tr, const int code )
1270 bs_transrate_t *bs = &tr->bs;
1272 if( slice_init( tr, code ) )
1279 int macroblock_modes;
1283 macroblock_modes = get_macroblock_modes( tr );
1284 if (macroblock_modes & MACROBLOCK_QUANT) tr->quantizer_scale = get_quantizer_scale( tr );
1286 //LOGF("blk %i : ", h_offset >> 4);
1288 if (macroblock_modes & MACROBLOCK_INTRA)
1290 //LOG("intra "); if (macroblock_modes & MACROBLOCK_QUANT) LOGF("got new quant: %i ", quantizer_scale);
1292 tr->new_quantizer_scale = increment_quant( tr, tr->quantizer_scale);
1293 if (tr->last_coded_scale == tr->new_quantizer_scale) macroblock_modes &= 0xFFFFFFEF; // remove MACROBLOCK_QUANT
1294 else macroblock_modes |= MACROBLOCK_QUANT; //add MACROBLOCK_QUANT
1295 putmbdata( tr, macroblock_modes);
1296 if (macroblock_modes & MACROBLOCK_QUANT) put_quantiser( tr );
1298 //if (macroblock_modes & MACROBLOCK_QUANT) LOGF("put new quant: %i ", new_quantizer_scale);
1300 if (tr->concealment_motion_vectors)
1302 if (tr->picture_structure != FRAME_PICTURE)
1304 bs_copy( bs, 1); /* remove field_select */
1306 /* like motion_frame, but parsing without actual motion compensation */
1307 get_motion_delta( bs, tr->f_code[0][0]);
1308 get_motion_delta( bs, tr->f_code[0][1]);
1310 bs_copy( bs, 1); /* remove marker_bit */
1313 if( tr->intra_vlc_format )
1316 get_luma_dc_dct_diff( bs ); get_intra_block_B15( bs, tr->quantizer_scale, tr->new_quantizer_scale );
1317 get_luma_dc_dct_diff( bs ); get_intra_block_B15( bs, tr->quantizer_scale, tr->new_quantizer_scale );
1318 get_luma_dc_dct_diff( bs ); get_intra_block_B15( bs, tr->quantizer_scale, tr->new_quantizer_scale );
1319 get_luma_dc_dct_diff( bs ); get_intra_block_B15( bs, tr->quantizer_scale, tr->new_quantizer_scale );
1321 get_chroma_dc_dct_diff( bs ); get_intra_block_B15( bs, tr->quantizer_scale, tr->new_quantizer_scale );
1322 get_chroma_dc_dct_diff( bs ); get_intra_block_B15( bs, tr->quantizer_scale, tr->new_quantizer_scale );
1327 get_luma_dc_dct_diff( bs ); get_intra_block_B14( bs, tr->quantizer_scale, tr->new_quantizer_scale );
1328 get_luma_dc_dct_diff( bs ); get_intra_block_B14( bs, tr->quantizer_scale, tr->new_quantizer_scale );
1329 get_luma_dc_dct_diff( bs ); get_intra_block_B14( bs, tr->quantizer_scale, tr->new_quantizer_scale );
1330 get_luma_dc_dct_diff( bs ); get_intra_block_B14( bs, tr->quantizer_scale, tr->new_quantizer_scale );
1332 get_chroma_dc_dct_diff( bs ); get_intra_block_B14( bs, tr->quantizer_scale, tr->new_quantizer_scale );
1333 get_chroma_dc_dct_diff( bs ); get_intra_block_B14( bs, tr->quantizer_scale, tr->new_quantizer_scale );
1338 RunLevel block[6][65]; // terminated by level = 0, so we need 64+1
1339 int new_coded_block_pattern = 0;
1341 // begin saving data
1343 uint8_t p_n_ow[32], *p_n_w,
1344 *p_o_ow = bs->p_ow, *p_o_w = bs->p_w;
1345 uint32_t i_n_bit_out, i_n_bit_out_cache,
1346 i_o_bit_out = bs->i_bit_out, i_o_bit_out_cache = bs->i_bit_out_cache;
1348 bs->i_bit_out_cache = 0; bs->i_bit_out = BITS_IN_BUF;
1349 bs->p_ow = bs->p_w = p_n_ow;
1351 if (tr->picture_structure == FRAME_PICTURE)
1352 switch (macroblock_modes & MOTION_TYPE_MASK)
1354 case MC_FRAME: MOTION_CALL (motion_fr_frame, macroblock_modes); break;
1355 case MC_FIELD: MOTION_CALL (motion_fr_field, macroblock_modes); break;
1356 case MC_DMV: MOTION_CALL (motion_fr_dmv, MACROBLOCK_MOTION_FORWARD); break;
1359 switch (macroblock_modes & MOTION_TYPE_MASK)
1361 case MC_FIELD: MOTION_CALL (motion_fi_field, macroblock_modes); break;
1362 case MC_16X8: MOTION_CALL (motion_fi_16x8, macroblock_modes); break;
1363 case MC_DMV: MOTION_CALL (motion_fi_dmv, MACROBLOCK_MOTION_FORWARD); break;
1366 assert(bs->p_w - bs->p_ow < 32);
1369 i_n_bit_out = bs->i_bit_out;
1370 i_n_bit_out_cache = bs->i_bit_out_cache;
1371 assert(bs->p_ow == p_n_ow);
1373 bs->i_bit_out = i_o_bit_out ;
1374 bs->i_bit_out_cache = i_o_bit_out_cache;
1379 if ( tr->picture_coding_type == P_TYPE) tr->new_quantizer_scale = tr->quantizer_scale;
1380 else tr->new_quantizer_scale = getNewQuant( tr, tr->quantizer_scale);
1382 //LOG("non intra "); if (macroblock_modes & MACROBLOCK_QUANT) LOGF("got new quant: %i ", quantizer_scale);
1384 if (macroblock_modes & MACROBLOCK_PATTERN)
1386 const int cbp = get_coded_block_pattern( bs );
1388 if( tr->picture_coding_type == P_TYPE )
1390 if( cbp&0x20 ) get_non_intra_block_drop( tr, block[0] );
1391 if( cbp&0x10 ) get_non_intra_block_drop( tr, block[1] );
1392 if( cbp&0x08 ) get_non_intra_block_drop( tr, block[2] );
1393 if( cbp&0x04 ) get_non_intra_block_drop( tr, block[3] );
1394 if( cbp&0x02 ) get_non_intra_block_drop( tr, block[4] );
1395 if( cbp&0x01 ) get_non_intra_block_drop( tr, block[5] );
1397 new_coded_block_pattern = cbp;
1403 get_non_intra_block_rq( bs, block[0], tr->quantizer_scale, tr->new_quantizer_scale );
1404 if( isNotEmpty( block[0] ) ) new_coded_block_pattern |= 0x20;
1408 get_non_intra_block_rq( bs, block[1], tr->quantizer_scale, tr->new_quantizer_scale );
1409 if( isNotEmpty( block[1] ) ) new_coded_block_pattern |= 0x10;
1413 get_non_intra_block_rq( bs, block[2], tr->quantizer_scale, tr->new_quantizer_scale );
1414 if( isNotEmpty( block[2] ) ) new_coded_block_pattern |= 0x08;
1418 get_non_intra_block_rq( bs, block[3], tr->quantizer_scale, tr->new_quantizer_scale );
1419 if( isNotEmpty( block[3] ) ) new_coded_block_pattern |= 0x04;
1423 get_non_intra_block_rq( bs, block[4], tr->quantizer_scale, tr->new_quantizer_scale );
1424 if( isNotEmpty( block[4] ) ) new_coded_block_pattern |= 0x02;
1428 get_non_intra_block_rq( bs, block[5], tr->quantizer_scale, tr->new_quantizer_scale );
1429 if( isNotEmpty( block[5] ) ) new_coded_block_pattern |= 0x01;
1431 if( !new_coded_block_pattern) macroblock_modes &= 0xFFFFFFED; // remove MACROBLOCK_PATTERN and MACROBLOCK_QUANT flag
1435 if (tr->last_coded_scale == tr->new_quantizer_scale) macroblock_modes &= 0xFFFFFFEF; // remove MACROBLOCK_QUANT
1436 else if (macroblock_modes & MACROBLOCK_PATTERN) macroblock_modes |= MACROBLOCK_QUANT; //add MACROBLOCK_QUANT
1437 assert( (macroblock_modes & MACROBLOCK_PATTERN) || !(macroblock_modes & MACROBLOCK_QUANT) );
1439 putmbdata( tr, macroblock_modes);
1440 if( macroblock_modes & MACROBLOCK_QUANT )
1442 put_quantiser( tr );
1445 // put saved motion data...
1446 for (batb = 0; batb < (p_n_w - p_n_ow); batb++)
1448 bs_write( bs, p_n_ow[batb], 8 );
1450 bs_write( bs, i_n_bit_out_cache, BITS_IN_BUF - i_n_bit_out);
1451 // end saved motion data...
1453 if (macroblock_modes & MACROBLOCK_PATTERN)
1456 bs_write( bs, cbptable[new_coded_block_pattern].code,cbptable[new_coded_block_pattern].len);
1458 if (new_coded_block_pattern & 0x20) putnonintrablk( bs, block[0]);
1459 if (new_coded_block_pattern & 0x10) putnonintrablk( bs, block[1]);
1460 if (new_coded_block_pattern & 0x08) putnonintrablk( bs, block[2]);
1461 if (new_coded_block_pattern & 0x04) putnonintrablk( bs, block[3]);
1462 if (new_coded_block_pattern & 0x02) putnonintrablk( bs, block[4]);
1463 if (new_coded_block_pattern & 0x01) putnonintrablk( bs, block[5]);
1467 //LOGF("\n\to: %i c: %i n: %i\n", quantizer_scale, last_coded_scale, new_quantizer_scale);
1474 if (bs->i_bit_in_cache >= 0x10000000)
1476 mba = MBA_5 + (UBITS (bs->i_bit_in_cache, 5) - 2);
1479 else if (bs->i_bit_in_cache >= 0x03000000)
1481 mba = MBA_11 + (UBITS (bs->i_bit_in_cache, 11) - 24);
1484 else if( UBITS (bs->i_bit_in_cache, 11 ) == 8 )
1486 /* macroblock_escape */
1496 bs_copy( bs, mba->len);
1497 mba_inc += mba->mba;
1506 /////---- end ext mpeg code
1508 static int do_next_start_code( transrate_t *tr )
1510 bs_transrate_t *bs = &tr->bs;
1517 *bs->p_w++ = *bs->p_c++;
1519 if (ID == 0x00) // pic header
1521 tr->picture_coding_type = (bs->p_c[1] >> 3) & 0x7;
1522 bs->p_c[1] |= 0x7; bs->p_c[2] = 0xFF; bs->p_c[3] |= 0xF8; // vbv_delay is now 0xFFFF
1524 memcpy(bs->p_w, bs->p_c, 4);
1528 else if (ID == 0xB3) // seq header
1530 tr->horizontal_size_value = (bs->p_c[0] << 4) | (bs->p_c[1] >> 4);
1531 tr->vertical_size_value = ((bs->p_c[1] & 0xF) << 8) | bs->p_c[2];
1532 if(!tr->horizontal_size_value || !tr->vertical_size_value )
1537 memcpy(bs->p_w, bs->p_c, 8 );
1541 else if (ID == 0xB5) // extension
1543 if ((bs->p_c[0] >> 4) == 0x8) // pic coding ext
1545 tr->f_code[0][0] = (bs->p_c[0] & 0xF) - 1;
1546 tr->f_code[0][1] = (bs->p_c[1] >> 4) - 1;
1547 tr->f_code[1][0] = (bs->p_c[1] & 0xF) - 1;
1548 tr->f_code[1][1] = (bs->p_c[2] >> 4) - 1;
1550 /* tr->intra_dc_precision = (bs->p_c[2] >> 2) & 0x3; */
1551 tr->picture_structure = bs->p_c[2] & 0x3;
1552 tr->frame_pred_frame_dct = (bs->p_c[3] >> 6) & 0x1;
1553 tr->concealment_motion_vectors = (bs->p_c[3] >> 5) & 0x1;
1554 tr->q_scale_type = (bs->p_c[3] >> 4) & 0x1;
1555 tr->intra_vlc_format = (bs->p_c[3] >> 3) & 0x1;
1556 /* tr->alternate_scan = (bs->p_c[3] >> 2) & 0x1; */
1559 memcpy(bs->p_w, bs->p_c, 5);
1565 *bs->p_w++ = *bs->p_c++;
1568 else if (ID == 0xB8) // gop header
1570 memcpy(bs->p_w, bs->p_c, 4);
1574 else if ((ID >= 0x01) && (ID <= 0xAF)) // slice
1576 uint8_t *outTemp = bs->p_w, *inTemp = bs->p_c;
1579 if( ( tr->picture_coding_type == B_TYPE && tr->quant_corr < 2.5f ) || // don't recompress if we're in advance!
1580 ( tr->picture_coding_type == P_TYPE && tr->quant_corr < -2.5f ) ||
1581 ( tr->picture_coding_type == I_TYPE && tr->quant_corr < -5.0f ) )
1583 if( ( tr->picture_coding_type == B_TYPE ) ||
1584 ( tr->picture_coding_type == P_TYPE && tr->level_p ) ||
1585 ( tr->picture_coding_type == I_TYPE && tr->level_i ) )
1588 if( !tr->horizontal_size_value || !tr->vertical_size_value )
1594 bs->i_bit_in_cache = 0; bs->i_bit_in = 0;
1595 bs->i_bit_out_cache = 0; bs->i_bit_out = BITS_IN_BUF;
1603 // begin bit level recoding
1604 mpeg2_slice(tr, ID);
1606 bs_flush_read( bs );
1607 bs_flush_write( bs );
1608 // end bit level recoding
1610 /* Basic sanity checks --Meuuh */
1611 if (bs->p_c > bs->p_r || bs->p_w > bs->p_rw)
1616 /*LOGF("type: %s code: %02i in : %6i out : %6i diff : %6i fact: %2.2f\n",
1617 (picture_coding_type == I_TYPE ? "I_TYPE" : (picture_coding_type == P_TYPE ? "P_TYPE" : "B_TYPE")),
1618 ID, bs->p_c - inTemp, bs->p_w - outTemp, (bs->p_w - outTemp) - (bs->p_c - inTemp), (float)(bs->p_c - inTemp) / (float)(bs->p_w - outTemp));*/
1620 if (bs->p_w - outTemp > bs->p_c - inTemp) // yes that might happen, rarely
1622 /*LOGF("*** slice bigger than before !! (type: %s code: %i in : %i out : %i diff : %i)\n",
1623 (picture_coding_type == I_TYPE ? "I_TYPE" : (picture_coding_type == P_TYPE ? "P_TYPE" : "B_TYPE")),
1624 ID, bs->p_c - inTemp, bs->p_w - outTemp, (bs->p_w - outTemp) - (bs->p_c - inTemp));*/
1626 // in this case, we'll just use the original slice !
1627 memcpy(outTemp, inTemp, bs->p_c - inTemp);
1628 bs->p_w = outTemp + (bs->p_c - inTemp);
1630 // adjust bs->i_byte_out
1631 bs->i_byte_out -= (bs->p_w - outTemp) - (bs->p_c - inTemp);
1638 void E_(process_frame)( sout_stream_t *p_stream,
1639 sout_stream_id_t *id, sout_buffer_t *in, sout_buffer_t **out )
1641 transrate_t *tr = &id->tr;
1642 bs_transrate_t *bs = &tr->bs;
1644 sout_buffer_t *p_out;
1646 double next_fact_x = 1.0;
1648 /* The output buffer can't be bigger than the input buffer. */
1649 p_out = sout_BufferNew( p_stream->p_sout, in->i_size );
1651 p_out->i_length = in->i_length;
1652 p_out->i_dts = in->i_dts;
1653 p_out->i_pts = in->i_pts;
1654 p_out->i_flags = in->i_flags;
1656 sout_BufferChain( out, p_out );
1658 bs->p_rw = bs->p_ow = bs->p_w = p_out->p_buffer;
1659 bs->p_c = bs->p_r = in->p_buffer;
1660 bs->p_r += in->i_size + 4;
1661 bs->p_rw += in->i_size;
1662 *(in->p_buffer + in->i_size) = 0;
1663 *(in->p_buffer + in->i_size + 1) = 0;
1664 *(in->p_buffer + in->i_size + 2) = 1;
1665 *(in->p_buffer + in->i_size + 3) = 0;
1667 /* Calculate how late we are */
1668 tr->quant_corr = 0.0 + B_HANDICAP;
1671 bs->i_byte_in = in->i_size;
1674 if (tr->i_current_gop_size - in->i_size > 100)
1676 if (tr->i_wanted_gop_size == in->i_size)
1680 else if ( tr->i_wanted_gop_size < in->i_size )
1682 /* We're really late */
1687 next_fact_x = ((double)(tr->i_current_gop_size - in->i_size)) /
1688 (tr->i_wanted_gop_size - in->i_size);
1691 if (next_fact_x > QUANT_I)
1695 if (next_fact_x > QUANT_P)
1697 tr->level_p = 1 + (next_fact_x - QUANT_P) / (QUANT_P_INC);
1700 if ( tr->i_wanted_gop_size < 0 )
1702 /* We're really late */
1703 tr->current_fact_x = 3.0;
1707 tr->current_fact_x = ((double)(tr->i_current_gop_size) /
1708 (tr->i_wanted_gop_size));
1713 uint8_t *p_end = &in->p_buffer[in->i_size];
1715 /* Search next start code */
1718 if( bs->p_c < p_end - 3 && bs->p_c[0] == 0 && bs->p_c[1] == 0 && bs->p_c[2] == 1 )
1720 /* Next start code */
1723 else if( bs->p_c < p_end - 6 &&
1724 bs->p_c[0] == 0 && bs->p_c[1] == 0 && bs->p_c[2] == 0 &&
1725 bs->p_c[3] == 0 && bs->p_c[4] == 0 && bs->p_c[5] == 0 )
1727 /* remove stuffing (looking for 6 0x00 bytes) */
1733 *bs->p_w++ = *bs->p_c++;
1736 if( bs->p_c >= p_end)
1742 if( bs->p_c >= p_end )
1747 /* Copy the start code */
1748 memcpy(bs->p_w, bs->p_c, 3 );
1752 if (do_next_start_code( tr ) )
1758 tr->quant_corr = (((bs->i_byte_in - (bs->p_r - 4 - bs->p_c)) / tr->fact_x) - (bs->i_byte_out + (bs->p_w - bs->p_ow))) / REACT_DELAY + B_HANDICAP;
1761 bs->i_byte_out += bs->p_w - bs->p_ow;
1762 p_out->i_size = bs->p_w - bs->p_ow;
1763 tr->i_current_gop_size -= in->i_size;
1764 tr->i_wanted_gop_size -= p_out->i_size;
1765 tr->i_new_gop_size += bs->i_byte_out;
1768 msg_Dbg( p_stream, "%d: %d -> %d (r: %f, n:%f, corr:%f)",
1769 tr->picture_coding_type, in->i_size, p_out->i_size,
1770 (float)in->i_size / p_out->i_size,
1771 next_fact_x, tr->quant_corr);