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.1 2004/03/03 11:20:52 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 /*****************************************************************************
48 *****************************************************************************/
49 static int Open ( vlc_object_t * );
50 static void Close ( vlc_object_t * );
52 static sout_stream_id_t *Add ( sout_stream_t *, es_format_t * );
53 static int Del ( sout_stream_t *, sout_stream_id_t * );
54 static int Send( sout_stream_t *, sout_stream_id_t *, sout_buffer_t * );
56 static int transrate_video_process( sout_stream_t *, sout_stream_id_t *, sout_buffer_t *, sout_buffer_t ** );
59 /****************************************************************************
60 * transrater, code from M2VRequantizer http://www.metakine.com/
61 ****************************************************************************/
63 /* This is awful magic --Meuuh */
64 //#define REACT_DELAY (1024.0*128.0)
65 #define REACT_DELAY (256.0)
71 #define QUANT_P_INC (0.1)
78 // - the quantiser is increment by one step
81 // - in P_FRAME we keep the original quantiser but drop the last coefficient
82 // if there is more than one
83 // - in B_FRAME we multiply the quantiser by a factor
85 // - I_FRAME is recoded when we're 5.0 * REACT_DELAY late
86 // - P_FRAME is recoded when we're 2.5 * REACT_DELAY late
87 // - B_FRAME are always recoded
89 // if we're getting *very* late (60 * REACT_DELAY)
91 // - intra blocks quantiser is incremented two step
92 // - drop a few coefficients but always keep the first one
104 #ifdef HAVE_BUILTIN_EXPECT
105 #define likely(x) __builtin_expect ((x) != 0, 1)
106 #define unlikely(x) __builtin_expect ((x) != 0, 0)
108 #define likely(x) (x)
109 #define unlikely(x) (x)
112 #define BITS_IN_BUF (8)
114 #define LOG(msg) fprintf (stderr, msg)
115 #define LOGF(format, args...) fprintf (stderr, format, args)
117 static inline void bs_write( bs_transrate_t *s, unsigned int val, int n)
120 assert(!(val & (0xffffffffU << n)));
122 while (unlikely(n >= s->i_bit_out))
124 s->p_w[0] = (s->i_bit_out_cache << s->i_bit_out ) | (val >> (n - s->i_bit_out));
127 s->i_bit_out_cache = 0;
128 val &= ~(0xffffffffU << n);
129 s->i_bit_out = BITS_IN_BUF;
134 s->i_bit_out_cache = (s->i_bit_out_cache << n) | val;
138 assert(s->i_bit_out > 0);
139 assert(s->i_bit_out <= BITS_IN_BUF);
142 static inline void bs_refill( bs_transrate_t *s )
144 assert((s->p_r - s->p_c) >= 1);
145 s->i_bit_in_cache |= s->p_c[0] << (24 - s->i_bit_in);
150 static inline void bs_flush( bs_transrate_t *s, unsigned int n )
152 assert(s->i_bit_in >= n);
154 s->i_bit_in_cache <<= n;
157 assert( (!n) || ((n>0) && !(s->i_bit_in_cache & 0x1)) );
159 while (unlikely(s->i_bit_in < 24)) bs_refill( s );
162 static inline unsigned int bs_read( bs_transrate_t *s, unsigned int n)
164 unsigned int Val = ((unsigned int)s->i_bit_in_cache) >> (32 - n);
169 static inline unsigned int bs_copy( bs_transrate_t *s, unsigned int n)
171 unsigned int Val = bs_read( s, n);
176 static inline void bs_flush_read( bs_transrate_t *s )
178 int i = s->i_bit_in & 0x7;
181 assert(((unsigned int)bs->i_bit_in_cache) >> (32 - i) == 0);
182 s->i_bit_in_cache <<= i;
185 s->p_c += -1 * (s->i_bit_in >> 3);
188 static inline void bs_flush_write( bs_transrate_t *s )
190 if( s->i_bit_out != 8 ) bs_write(s, 0, s->i_bit_out);
193 /////---- begin ext mpeg code
195 const uint8_t non_linear_mquant_table[32] =
197 0, 1, 2, 3, 4, 5, 6, 7,
198 8,10,12,14,16,18,20,22,
199 24,28,32,36,40,44,48,52,
200 56,64,72,80,88,96,104,112
202 const uint8_t map_non_linear_mquant[113] =
204 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,
205 16,17,17,17,18,18,18,18,19,19,19,19,20,20,20,20,21,21,21,21,22,22,
206 22,22,23,23,23,23,24,24,24,24,24,24,24,25,25,25,25,25,25,25,26,26,
207 26,26,26,26,26,26,27,27,27,27,27,27,27,27,28,28,28,28,28,28,28,29,
208 29,29,29,29,29,29,29,29,29,30,30,30,30,30,30,30,31,31,31,31,31
211 static int scale_quant( unsigned int q_scale_type, double quant )
216 iquant = (int) floor(quant+0.5);
218 /* clip mquant to legal (linear) range */
219 if (iquant<1) iquant = 1;
220 if (iquant>112) iquant = 112;
222 iquant = non_linear_mquant_table[map_non_linear_mquant[iquant]];
226 /* clip mquant to legal (linear) range */
227 iquant = (int)floor(quant+0.5);
228 if (iquant<2) iquant = 2;
229 if (iquant>62) iquant = 62;
230 iquant = (iquant/2)*2; // Must be *even*
235 static int increment_quant( transrate_t *tr, int quant )
237 if( tr->q_scale_type )
239 assert(quant >= 1 && quant <= 112 );
240 quant = map_non_linear_mquant[quant] + 1;
241 if( tr->picture_coding_type == P_TYPE )
242 quant += tr->level_p;
243 if( quant > 31) quant = 31;
244 quant = non_linear_mquant_table[quant];
248 assert(!(quant & 1));
250 if( tr->picture_coding_type == P_TYPE )
251 quant += 2 * tr->level_p;
252 if (quant > 62) quant = 62;
257 static inline int intmax( register int x, register int y )
259 return x < y ? y : x;
261 static inline int intmin( register int x, register int y )
263 return x < y ? x : y;
266 static int getNewQuant( transrate_t *tr, int curQuant)
268 bs_transrate_t *bs = &tr->bs;
270 double calc_quant, quant_to_use;
273 switch ( tr->picture_coding_type )
277 mquant = increment_quant( tr, curQuant );
281 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;
282 calc_quant = curQuant * tr->current_fact_x;
283 quant_to_use = calc_quant - tr->quant_corr;
285 mquant = intmax(scale_quant( tr->q_scale_type, quant_to_use), increment_quant( tr, curQuant) );
294 LOGF("type: %s orig_quant: %3i calc_quant: %7.1f quant_corr: %7.1f using_quant: %3i\n",
295 (picture_coding_type == I_TYPE ? "I_TYPE" : (picture_coding_type == P_TYPE ? "P_TYPE" : "B_TYPE")),
296 (int)curQuant, (float)calc_quant, (float)quant_corr, (int)mquant);
299 assert(mquant >= curQuant);
304 static inline int isNotEmpty(RunLevel *blk)
311 static void putAC( bs_transrate_t *bs, int run, int signed_level, int vlcformat)
314 const VLCtable *ptab = NULL;
316 level = (signed_level<0) ? -signed_level : signed_level; /* abs(signed_level) */
318 assert(!(run<0 || run>63 || level==0 || level>2047));
322 if (run<2 && level<41)
324 if (vlcformat) ptab = &dct_code_tab1a[run][level-1];
325 else ptab = &dct_code_tab1[run][level-1];
328 else if (run<32 && level<6)
330 if (vlcformat) ptab = &dct_code_tab2a[run-2][level-1];
331 else ptab = &dct_code_tab2[run-2][level-1];
335 if (len) /* a VLC code exists */
337 bs_write( bs, ptab->code, len);
338 bs_write( bs, signed_level<0, 1); /* sign */
342 bs_write( bs, 1l, 6); /* Escape */
343 bs_write( bs, run, 6); /* 6 bit code for run */
344 bs_write( bs, ((unsigned int)signed_level) & 0xFFF, 12);
349 static inline void putACfirst( bs_transrate_t *bs, int run, int val)
351 if (run==0 && (val==1 || val==-1)) bs_write( bs, 2|(val<0),2);
352 else putAC( bs, run,val,0);
355 static void putnonintrablk( bs_transrate_t *bs, RunLevel *blk)
359 putACfirst( bs, blk->run, blk->level);
364 putAC( bs, blk->run, blk->level, 0);
373 static const int non_linear_quantizer_scale [] =
375 0, 1, 2, 3, 4, 5, 6, 7,
376 8, 10, 12, 14, 16, 18, 20, 22,
377 24, 28, 32, 36, 40, 44, 48, 52,
378 56, 64, 72, 80, 88, 96, 104, 112
381 static inline int get_macroblock_modes( transrate_t *tr )
383 bs_transrate_t *bs = &tr->bs;
385 int macroblock_modes;
388 switch( tr->picture_coding_type)
392 tab = MB_I + UBITS (bs->i_bit_in_cache, 1);
393 bs_flush( bs, tab->len );
394 macroblock_modes = tab->modes;
396 if ((! ( tr->frame_pred_frame_dct)) && ( tr->picture_structure == FRAME_PICTURE))
398 macroblock_modes |= UBITS (bs->i_bit_in_cache, 1) * DCT_TYPE_INTERLACED;
402 return macroblock_modes;
406 tab = MB_P + UBITS (bs->i_bit_in_cache, 5);
407 bs_flush( bs, tab->len );
408 macroblock_modes = tab->modes;
410 if (tr->picture_structure != FRAME_PICTURE)
412 if (macroblock_modes & MACROBLOCK_MOTION_FORWARD)
414 macroblock_modes |= UBITS (bs->i_bit_in_cache, 2) * MOTION_TYPE_BASE;
417 return macroblock_modes;
419 else if (tr->frame_pred_frame_dct)
421 if (macroblock_modes & MACROBLOCK_MOTION_FORWARD)
422 macroblock_modes |= MC_FRAME;
423 return macroblock_modes;
427 if (macroblock_modes & MACROBLOCK_MOTION_FORWARD)
429 macroblock_modes |= UBITS (bs->i_bit_in_cache, 2) * MOTION_TYPE_BASE;
432 if (macroblock_modes & (MACROBLOCK_INTRA | MACROBLOCK_PATTERN))
434 macroblock_modes |= UBITS (bs->i_bit_in_cache, 1) * DCT_TYPE_INTERLACED;
437 return macroblock_modes;
442 tab = MB_B + UBITS (bs->i_bit_in_cache, 6);
443 bs_flush( bs, tab->len );
444 macroblock_modes = tab->modes;
446 if( tr->picture_structure != FRAME_PICTURE)
448 if (! (macroblock_modes & MACROBLOCK_INTRA))
450 macroblock_modes |= UBITS (bs->i_bit_in_cache, 2) * MOTION_TYPE_BASE;
453 return macroblock_modes;
455 else if (tr->frame_pred_frame_dct)
457 /* if (! (macroblock_modes & MACROBLOCK_INTRA)) */
458 macroblock_modes |= MC_FRAME;
459 return macroblock_modes;
463 if (macroblock_modes & MACROBLOCK_INTRA) goto intra;
464 macroblock_modes |= UBITS (bs->i_bit_in_cache, 2) * MOTION_TYPE_BASE;
466 if (macroblock_modes & (MACROBLOCK_INTRA | MACROBLOCK_PATTERN))
469 macroblock_modes |= UBITS (bs->i_bit_in_cache, 1) * DCT_TYPE_INTERLACED;
472 return macroblock_modes;
481 static inline int get_quantizer_scale( transrate_t *tr )
483 bs_transrate_t *bs = &tr->bs;
485 int quantizer_scale_code;
487 quantizer_scale_code = UBITS (bs->i_bit_in_cache, 5);
490 if( tr->q_scale_type )
491 return non_linear_quantizer_scale[quantizer_scale_code];
493 return quantizer_scale_code << 1;
496 static inline int get_motion_delta( bs_transrate_t *bs, const int f_code )
502 if (bs->i_bit_in_cache & 0x80000000)
507 else if (bs->i_bit_in_cache >= 0x0c000000)
510 tab = MV_4 + UBITS (bs->i_bit_in_cache, 4);
511 delta = (tab->delta << f_code) + 1;
512 bs_copy( bs, tab->len);
514 sign = SBITS (bs->i_bit_in_cache, 1);
517 if (f_code) delta += UBITS (bs->i_bit_in_cache, f_code);
518 bs_copy( bs, f_code);
520 return (delta ^ sign) - sign;
525 tab = MV_10 + UBITS (bs->i_bit_in_cache, 10);
526 delta = (tab->delta << f_code) + 1;
527 bs_copy( bs, tab->len);
529 sign = SBITS (bs->i_bit_in_cache, 1);
534 delta += UBITS (bs->i_bit_in_cache, f_code);
535 bs_copy( bs, f_code);
538 return (delta ^ sign) - sign;
543 static inline int get_dmv( bs_transrate_t *bs )
547 tab = DMV_2 + UBITS (bs->i_bit_in_cache, 2);
548 bs_copy( bs, tab->len);
552 static inline int get_coded_block_pattern( bs_transrate_t *bs )
556 if (bs->i_bit_in_cache >= 0x20000000)
558 tab = CBP_7 + (UBITS (bs->i_bit_in_cache, 7) - 16);
559 bs_flush( bs, tab->len );
564 tab = CBP_9 + UBITS (bs->i_bit_in_cache, 9);
565 bs_flush( bs, tab->len );
570 static inline int get_luma_dc_dct_diff( bs_transrate_t *bs )
576 if (bs->i_bit_in_cache < 0xf8000000)
578 tab = DC_lum_5 + UBITS (bs->i_bit_in_cache, 5);
582 bs_copy( bs, tab->len);
583 //dc_diff = UBITS (bs->i_bit_in_cache, size) - UBITS (SBITS (~bs->i_bit_in_cache, 1), size);
584 dc_diff = UBITS (bs->i_bit_in_cache, size);
585 if (!(dc_diff >> (size - 1))) dc_diff = (dc_diff + 1) - (1 << size);
597 tab = DC_long + (UBITS (bs->i_bit_in_cache, 9) - 0x1e0);
599 bs_copy( bs, tab->len);
600 //dc_diff = UBITS (bs->i_bit_in_cache, size) - UBITS (SBITS (~bs->i_bit_in_cache, 1), size);
601 dc_diff = UBITS (bs->i_bit_in_cache, size);
602 if (!(dc_diff >> (size - 1))) dc_diff = (dc_diff + 1) - (1 << size);
608 static inline int get_chroma_dc_dct_diff( bs_transrate_t *bs )
614 if (bs->i_bit_in_cache < 0xf8000000)
616 tab = DC_chrom_5 + UBITS (bs->i_bit_in_cache, 5);
620 bs_copy( bs, tab->len);
621 //dc_diff = UBITS (bs->i_bit_in_cache, size) - UBITS (SBITS (~bs->i_bit_in_cache, 1), size);
622 dc_diff = UBITS (bs->i_bit_in_cache, size);
623 if (!(dc_diff >> (size - 1))) dc_diff = (dc_diff + 1) - (1 << size);
634 tab = DC_long + (UBITS (bs->i_bit_in_cache, 10) - 0x3e0);
636 bs_copy( bs, tab->len + 1);
637 //dc_diff = UBITS (bs->i_bit_in_cache, size) - UBITS (SBITS (~bs->i_bit_in_cache, 1), size);
638 dc_diff = UBITS (bs->i_bit_in_cache, size);
639 if (!(dc_diff >> (size - 1))) dc_diff = (dc_diff + 1) - (1 << size);
645 static void get_intra_block_B14( bs_transrate_t *bs, const int i_qscale, const int i_qscale_new )
652 /* Basic sanity check --Meuuh */
658 tst = i_qscale_new/i_qscale + ((i_qscale_new%i_qscale) ? 1 : 0);
664 if (bs->i_bit_in_cache >= 0x28000000)
666 tab = DCT_B14AC_5 + (UBITS (bs->i_bit_in_cache, 5) - 5);
669 if (i >= 64) break; /* end of block */
672 bs_flush( bs, tab->len );
676 val = (val ^ SBITS (bs->i_bit_in_cache, 1)) - SBITS (bs->i_bit_in_cache, 1);
677 putAC( bs, i - li - 1, (val * i_qscale) / i_qscale_new, 0);
684 else if (bs->i_bit_in_cache >= 0x04000000)
686 tab = DCT_B14_8 + (UBITS (bs->i_bit_in_cache, 8) - 4);
689 if (i < 64) goto normal_code;
692 i += (UBITS (bs->i_bit_in_cache, 12) & 0x3F) - 64;
693 if (i >= 64) break; /* illegal, check needed to avoid buffer overflow */
696 val = SBITS (bs->i_bit_in_cache, 12);
699 putAC( bs, i - li - 1, (val * i_qscale) / i_qscale_new, 0);
707 else if (bs->i_bit_in_cache >= 0x02000000)
709 tab = DCT_B14_10 + (UBITS (bs->i_bit_in_cache, 10) - 8);
711 if (i < 64 ) goto normal_code;
713 else if (bs->i_bit_in_cache >= 0x00800000)
715 tab = DCT_13 + (UBITS (bs->i_bit_in_cache, 13) - 16);
717 if (i < 64 ) goto normal_code;
719 else if (bs->i_bit_in_cache >= 0x00200000)
721 tab = DCT_15 + (UBITS (bs->i_bit_in_cache, 15) - 16);
723 if (i < 64 ) goto normal_code;
727 tab = DCT_16 + UBITS (bs->i_bit_in_cache, 16);
730 if (i < 64 ) goto normal_code;
732 break; /* illegal, check needed to avoid buffer overflow */
735 bs_copy( bs, 2); /* end of block code */
738 static void get_intra_block_B15( bs_transrate_t *bs, const int i_qscale, int const i_qscale_new )
745 /* Basic sanity check --Meuuh */
751 tst = i_qscale_new/i_qscale + ((i_qscale_new%i_qscale) ? 1 : 0);
757 if (bs->i_bit_in_cache >= 0x04000000)
759 tab = DCT_B15_8 + (UBITS (bs->i_bit_in_cache, 8) - 4);
765 bs_flush( bs, tab->len );
770 val = (val ^ SBITS (bs->i_bit_in_cache, 1)) - SBITS (bs->i_bit_in_cache, 1);
771 putAC( bs, i - li - 1, (val * i_qscale) / i_qscale_new, 1);
780 i += (UBITS (bs->i_bit_in_cache, 12) & 0x3F) - 64;
782 if (i >= 64) break; /* illegal, check against buffer overflow */
785 val = SBITS (bs->i_bit_in_cache, 12);
788 putAC( bs, i - li - 1, (val * i_qscale) / i_qscale_new, 1);
796 else if (bs->i_bit_in_cache >= 0x02000000)
798 tab = DCT_B15_10 + (UBITS (bs->i_bit_in_cache, 10) - 8);
800 if (i < 64) goto normal_code;
802 else if (bs->i_bit_in_cache >= 0x00800000)
804 tab = DCT_13 + (UBITS (bs->i_bit_in_cache, 13) - 16);
806 if (i < 64) goto normal_code;
808 else if (bs->i_bit_in_cache >= 0x00200000)
810 tab = DCT_15 + (UBITS (bs->i_bit_in_cache, 15) - 16);
812 if (i < 64) goto normal_code;
816 tab = DCT_16 + UBITS (bs->i_bit_in_cache, 16);
819 if (i < 64) goto normal_code;
821 break; /* illegal, check needed to avoid buffer overflow */
824 bs_copy( bs, 4); /* end of block code */
828 static int get_non_intra_block_drop( transrate_t *tr, RunLevel *blk)
830 bs_transrate_t *bs = &tr->bs;
835 RunLevel *sblk = blk + 1;
839 if (bs->i_bit_in_cache >= 0x28000000)
841 tab = DCT_B14DC_5 + (UBITS (bs->i_bit_in_cache, 5) - 5);
848 if (bs->i_bit_in_cache >= 0x28000000)
850 tab = DCT_B14AC_5 + (UBITS (bs->i_bit_in_cache, 5) - 5);
854 if (i >= 64) break; /* end of block */
858 bs_flush( bs, tab->len );
860 val = (val ^ SBITS (bs->i_bit_in_cache, 1)) - SBITS (bs->i_bit_in_cache, 1); /* if (bitstream_get (1)) val = -val; */
863 blk->run = i - li - 1;
873 if (bs->i_bit_in_cache >= 0x04000000)
875 tab = DCT_B14_8 + (UBITS (bs->i_bit_in_cache, 8) - 4);
878 if (i < 64) goto normal_code;
882 i += (UBITS (bs->i_bit_in_cache, 12) & 0x3F) - 64;
884 if (i >= 64) break; /* illegal, check needed to avoid buffer overflow */
887 val = SBITS (bs->i_bit_in_cache, 12);
890 blk->run = i - li - 1;
897 else if (bs->i_bit_in_cache >= 0x02000000)
899 tab = DCT_B14_10 + (UBITS (bs->i_bit_in_cache, 10) - 8);
901 if (i < 64) goto normal_code;
903 else if (bs->i_bit_in_cache >= 0x00800000)
905 tab = DCT_13 + (UBITS (bs->i_bit_in_cache, 13) - 16);
907 if (i < 64) goto normal_code;
909 else if (bs->i_bit_in_cache >= 0x00200000)
911 tab = DCT_15 + (UBITS (bs->i_bit_in_cache, 15) - 16);
913 if (i < 64) goto normal_code;
917 tab = DCT_16 + UBITS (bs->i_bit_in_cache, 16);
920 if (i < 64) goto normal_code;
922 break; /* illegal, check needed to avoid buffer overflow */
924 bs_flush( bs, 2 ); /* dump end of block code */
932 // remove more coeffs if very late
933 if (tr->level_p >= 4 && (blk != sblk))
936 if (tr->level_p >= 5 && (blk != sblk))
939 if (tr->level_p >= 6 && (blk != sblk))
942 if (tr->level_p >= 7 && (blk != sblk))
953 static int get_non_intra_block_rq( bs_transrate_t *bs, RunLevel *blk, const int i_qscale, const int i_qscale_new )
960 /* Basic sanity check --Meuuh */
966 tst = i_qscale_new/i_qscale + ((i_qscale_new%i_qscale) ? 1 : 0);
970 if (bs->i_bit_in_cache >= 0x28000000)
972 tab = DCT_B14DC_5 + (UBITS (bs->i_bit_in_cache, 5) - 5);
979 if (bs->i_bit_in_cache >= 0x28000000)
981 tab = DCT_B14AC_5 + (UBITS (bs->i_bit_in_cache, 5) - 5);
986 break; /* end of block */
990 bs_flush( bs, tab->len );
994 val = (val ^ SBITS (bs->i_bit_in_cache, 1)) - SBITS (bs->i_bit_in_cache, 1);
995 blk->level = (val * i_qscale) / i_qscale_new;
996 blk->run = i - li - 1;
1001 //if ( ((val) && (tab->level < tst)) || ((!val) && (tab->level >= tst)) )
1002 // LOGF("level: %i val: %i tst : %i q: %i nq : %i\n", tab->level, val, tst, q, nq);
1009 if (bs->i_bit_in_cache >= 0x04000000)
1011 tab = DCT_B14_8 + (UBITS (bs->i_bit_in_cache, 8) - 4);
1014 if (i < 64) goto normal_code;
1018 i += (UBITS (bs->i_bit_in_cache, 12) & 0x3F) - 64;
1020 if (i >= 64) break; /* illegal, check needed to avoid buffer overflow */
1023 val = SBITS (bs->i_bit_in_cache, 12);
1024 if (abs(val) >= tst)
1026 blk->level = (val * i_qscale) / i_qscale_new;
1027 blk->run = i - li - 1;
1035 else if (bs->i_bit_in_cache >= 0x02000000)
1037 tab = DCT_B14_10 + (UBITS (bs->i_bit_in_cache, 10) - 8);
1039 if (i < 64) goto normal_code;
1041 else if (bs->i_bit_in_cache >= 0x00800000)
1043 tab = DCT_13 + (UBITS (bs->i_bit_in_cache, 13) - 16);
1045 if (i < 64) goto normal_code;
1047 else if (bs->i_bit_in_cache >= 0x00200000)
1049 tab = DCT_15 + (UBITS (bs->i_bit_in_cache, 15) - 16);
1051 if (i < 64) goto normal_code;
1055 tab = DCT_16 + UBITS (bs->i_bit_in_cache, 16);
1059 if (i < 64) goto normal_code;
1061 break; /* illegal, check needed to avoid buffer overflow */
1063 bs_flush( bs, 2 ); /* dump end of block code */
1070 static void motion_fr_frame( bs_transrate_t *bs, unsigned int f_code[2] )
1072 get_motion_delta( bs, f_code[0] );
1073 get_motion_delta( bs, f_code[1] );
1076 static void motion_fr_field( bs_transrate_t *bs, unsigned int f_code[2] )
1080 get_motion_delta( bs, f_code[0]);
1081 get_motion_delta( bs, f_code[1]);
1085 get_motion_delta( bs, f_code[0]);
1086 get_motion_delta( bs, f_code[1]);
1089 static void motion_fr_dmv( bs_transrate_t *bs, unsigned int f_code[2] )
1091 get_motion_delta( bs, f_code[0]);
1094 get_motion_delta( bs, f_code[1]);
1098 static void motion_fi_field( bs_transrate_t *bs, unsigned int f_code[2] )
1102 get_motion_delta( bs, f_code[0]);
1103 get_motion_delta( bs, f_code[1]);
1106 static void motion_fi_16x8( bs_transrate_t *bs, unsigned int f_code[2] )
1110 get_motion_delta( bs, f_code[0]);
1111 get_motion_delta( bs, f_code[1]);
1115 get_motion_delta( bs, f_code[0]);
1116 get_motion_delta( bs, f_code[1]);
1119 static void motion_fi_dmv( bs_transrate_t *bs, unsigned int f_code[2] )
1121 get_motion_delta( bs, f_code[0]);
1124 get_motion_delta( bs, f_code[1]);
1129 #define MOTION_CALL(routine,direction) \
1131 if ((direction) & MACROBLOCK_MOTION_FORWARD) \
1132 routine( bs, tr->f_code[0]); \
1133 if ((direction) & MACROBLOCK_MOTION_BACKWARD) \
1134 routine( bs, tr->f_code[1]); \
1137 #define NEXT_MACROBLOCK \
1139 tr->h_offset += 16; \
1140 if( tr->h_offset == tr->horizontal_size_value) \
1142 tr->v_offset += 16; \
1143 if (tr->v_offset > (tr->vertical_size_value - 16)) return; \
1148 static void putmbdata( transrate_t *tr, int macroblock_modes )
1150 bs_transrate_t *bs = &tr->bs;
1153 mbtypetab[tr->picture_coding_type-1][macroblock_modes&0x1F].code,
1154 mbtypetab[tr->picture_coding_type-1][macroblock_modes&0x1F].len);
1156 switch ( tr->picture_coding_type)
1159 if ((! (tr->frame_pred_frame_dct)) && (tr->picture_structure == FRAME_PICTURE))
1160 bs_write( bs, macroblock_modes & DCT_TYPE_INTERLACED ? 1 : 0, 1);
1164 if (tr->picture_structure != FRAME_PICTURE)
1166 if (macroblock_modes & MACROBLOCK_MOTION_FORWARD)
1167 bs_write( bs, (macroblock_modes & MOTION_TYPE_MASK) / MOTION_TYPE_BASE, 2);
1170 else if (tr->frame_pred_frame_dct) break;
1173 if (macroblock_modes & MACROBLOCK_MOTION_FORWARD)
1174 bs_write( bs, (macroblock_modes & MOTION_TYPE_MASK) / MOTION_TYPE_BASE, 2);
1175 if (macroblock_modes & (MACROBLOCK_INTRA | MACROBLOCK_PATTERN))
1176 bs_write( bs, macroblock_modes & DCT_TYPE_INTERLACED ? 1 : 0, 1);
1181 if (tr->picture_structure != FRAME_PICTURE)
1183 if (! (macroblock_modes & MACROBLOCK_INTRA))
1184 bs_write( bs, (macroblock_modes & MOTION_TYPE_MASK) / MOTION_TYPE_BASE, 2);
1187 else if (tr->frame_pred_frame_dct) break;
1190 if (macroblock_modes & MACROBLOCK_INTRA) goto intra;
1191 bs_write( bs, (macroblock_modes & MOTION_TYPE_MASK) / MOTION_TYPE_BASE, 2);
1192 if (macroblock_modes & (MACROBLOCK_INTRA | MACROBLOCK_PATTERN))
1195 bs_write( bs, macroblock_modes & DCT_TYPE_INTERLACED ? 1 : 0, 1);
1202 static inline void put_quantiser( transrate_t *tr )
1204 bs_transrate_t *bs = &tr->bs;
1206 bs_write( bs, tr->q_scale_type ? map_non_linear_mquant[tr->new_quantizer_scale] : tr->new_quantizer_scale >> 1, 5);
1207 tr->last_coded_scale = tr->new_quantizer_scale;
1210 static int slice_init( transrate_t *tr, int code)
1212 bs_transrate_t *bs = &tr->bs;
1216 tr->v_offset = (code - 1) * 16;
1218 tr->quantizer_scale = get_quantizer_scale( tr );
1219 if ( tr->picture_coding_type == P_TYPE)
1221 tr->new_quantizer_scale = tr->quantizer_scale;
1225 tr->new_quantizer_scale = getNewQuant(tr, tr->quantizer_scale);
1227 put_quantiser( tr );
1229 /*LOGF("************************\nstart of slice %i in %s picture. ori quant: %i new quant: %i\n", code,
1230 (picture_coding_type == I_TYPE ? "I_TYPE" : (picture_coding_type == P_TYPE ? "P_TYPE" : "B_TYPE")),
1231 quantizer_scale, new_quantizer_scale);*/
1233 /* ignore intra_slice and all the extra data */
1234 while (bs->i_bit_in_cache & 0x80000000)
1239 /* decode initial macroblock address increment */
1243 if (bs->i_bit_in_cache >= 0x08000000)
1245 mba = MBA_5 + (UBITS (bs->i_bit_in_cache, 6) - 2);
1248 else if (bs->i_bit_in_cache >= 0x01800000)
1250 mba = MBA_11 + (UBITS (bs->i_bit_in_cache, 12) - 24);
1253 else if( UBITS (bs->i_bit_in_cache, 12 ) == 8 )
1255 /* macroblock_escape */
1265 bs_copy( bs, mba->len + 1);
1266 tr->h_offset = (offset + mba->mba) << 4;
1268 while( tr->h_offset - (int)tr->horizontal_size_value >= 0)
1270 tr->h_offset -= tr->horizontal_size_value;
1274 if( tr->v_offset > tr->vertical_size_value - 16 )
1281 static void mpeg2_slice( transrate_t *tr, const int code )
1283 bs_transrate_t *bs = &tr->bs;
1285 if( slice_init( tr, code ) )
1292 int macroblock_modes;
1296 macroblock_modes = get_macroblock_modes( tr );
1297 if (macroblock_modes & MACROBLOCK_QUANT) tr->quantizer_scale = get_quantizer_scale( tr );
1299 //LOGF("blk %i : ", h_offset >> 4);
1301 if (macroblock_modes & MACROBLOCK_INTRA)
1303 //LOG("intra "); if (macroblock_modes & MACROBLOCK_QUANT) LOGF("got new quant: %i ", quantizer_scale);
1305 tr->new_quantizer_scale = increment_quant( tr, tr->quantizer_scale);
1306 if (tr->last_coded_scale == tr->new_quantizer_scale) macroblock_modes &= 0xFFFFFFEF; // remove MACROBLOCK_QUANT
1307 else macroblock_modes |= MACROBLOCK_QUANT; //add MACROBLOCK_QUANT
1308 putmbdata( tr, macroblock_modes);
1309 if (macroblock_modes & MACROBLOCK_QUANT) put_quantiser( tr );
1311 //if (macroblock_modes & MACROBLOCK_QUANT) LOGF("put new quant: %i ", new_quantizer_scale);
1313 if (tr->concealment_motion_vectors)
1315 if (tr->picture_structure != FRAME_PICTURE)
1317 bs_copy( bs, 1); /* remove field_select */
1319 /* like motion_frame, but parsing without actual motion compensation */
1320 get_motion_delta( bs, tr->f_code[0][0]);
1321 get_motion_delta( bs, tr->f_code[0][1]);
1323 bs_copy( bs, 1); /* remove marker_bit */
1326 if( tr->intra_vlc_format )
1329 get_luma_dc_dct_diff( bs ); get_intra_block_B15( bs, tr->quantizer_scale, tr->new_quantizer_scale );
1330 get_luma_dc_dct_diff( bs ); get_intra_block_B15( bs, tr->quantizer_scale, tr->new_quantizer_scale );
1331 get_luma_dc_dct_diff( bs ); get_intra_block_B15( bs, tr->quantizer_scale, tr->new_quantizer_scale );
1332 get_luma_dc_dct_diff( bs ); get_intra_block_B15( bs, tr->quantizer_scale, tr->new_quantizer_scale );
1334 get_chroma_dc_dct_diff( bs ); get_intra_block_B15( bs, tr->quantizer_scale, tr->new_quantizer_scale );
1335 get_chroma_dc_dct_diff( bs ); get_intra_block_B15( bs, tr->quantizer_scale, tr->new_quantizer_scale );
1340 get_luma_dc_dct_diff( bs ); get_intra_block_B14( bs, tr->quantizer_scale, tr->new_quantizer_scale );
1341 get_luma_dc_dct_diff( bs ); get_intra_block_B14( bs, tr->quantizer_scale, tr->new_quantizer_scale );
1342 get_luma_dc_dct_diff( bs ); get_intra_block_B14( bs, tr->quantizer_scale, tr->new_quantizer_scale );
1343 get_luma_dc_dct_diff( bs ); get_intra_block_B14( bs, tr->quantizer_scale, tr->new_quantizer_scale );
1345 get_chroma_dc_dct_diff( bs ); get_intra_block_B14( bs, tr->quantizer_scale, tr->new_quantizer_scale );
1346 get_chroma_dc_dct_diff( bs ); get_intra_block_B14( bs, tr->quantizer_scale, tr->new_quantizer_scale );
1351 RunLevel block[6][65]; // terminated by level = 0, so we need 64+1
1352 int new_coded_block_pattern = 0;
1354 // begin saving data
1356 uint8_t p_n_ow[32], *p_n_w,
1357 *p_o_ow = bs->p_ow, *p_o_w = bs->p_w;
1358 uint32_t i_n_bit_out, i_n_bit_out_cache,
1359 i_o_bit_out = bs->i_bit_out, i_o_bit_out_cache = bs->i_bit_out_cache;
1361 bs->i_bit_out_cache = 0; bs->i_bit_out = BITS_IN_BUF;
1362 bs->p_ow = bs->p_w = p_n_ow;
1364 if (tr->picture_structure == FRAME_PICTURE)
1365 switch (macroblock_modes & MOTION_TYPE_MASK)
1367 case MC_FRAME: MOTION_CALL (motion_fr_frame, macroblock_modes); break;
1368 case MC_FIELD: MOTION_CALL (motion_fr_field, macroblock_modes); break;
1369 case MC_DMV: MOTION_CALL (motion_fr_dmv, MACROBLOCK_MOTION_FORWARD); break;
1372 switch (macroblock_modes & MOTION_TYPE_MASK)
1374 case MC_FIELD: MOTION_CALL (motion_fi_field, macroblock_modes); break;
1375 case MC_16X8: MOTION_CALL (motion_fi_16x8, macroblock_modes); break;
1376 case MC_DMV: MOTION_CALL (motion_fi_dmv, MACROBLOCK_MOTION_FORWARD); break;
1379 assert(bs->p_w - bs->p_ow < 32);
1382 i_n_bit_out = bs->i_bit_out;
1383 i_n_bit_out_cache = bs->i_bit_out_cache;
1384 assert(bs->p_ow == p_n_ow);
1386 bs->i_bit_out = i_o_bit_out ;
1387 bs->i_bit_out_cache = i_o_bit_out_cache;
1392 if ( tr->picture_coding_type == P_TYPE) tr->new_quantizer_scale = tr->quantizer_scale;
1393 else tr->new_quantizer_scale = getNewQuant( tr, tr->quantizer_scale);
1395 //LOG("non intra "); if (macroblock_modes & MACROBLOCK_QUANT) LOGF("got new quant: %i ", quantizer_scale);
1397 if (macroblock_modes & MACROBLOCK_PATTERN)
1399 const int cbp = get_coded_block_pattern( bs );
1401 if( tr->picture_coding_type == P_TYPE )
1403 if( cbp&0x20 ) get_non_intra_block_drop( tr, block[0] );
1404 if( cbp&0x10 ) get_non_intra_block_drop( tr, block[1] );
1405 if( cbp&0x08 ) get_non_intra_block_drop( tr, block[2] );
1406 if( cbp&0x04 ) get_non_intra_block_drop( tr, block[3] );
1407 if( cbp&0x02 ) get_non_intra_block_drop( tr, block[4] );
1408 if( cbp&0x01 ) get_non_intra_block_drop( tr, block[5] );
1410 new_coded_block_pattern = cbp;
1416 get_non_intra_block_rq( bs, block[0], tr->quantizer_scale, tr->new_quantizer_scale );
1417 if( isNotEmpty( block[0] ) ) new_coded_block_pattern |= 0x20;
1421 get_non_intra_block_rq( bs, block[1], tr->quantizer_scale, tr->new_quantizer_scale );
1422 if( isNotEmpty( block[1] ) ) new_coded_block_pattern |= 0x10;
1426 get_non_intra_block_rq( bs, block[2], tr->quantizer_scale, tr->new_quantizer_scale );
1427 if( isNotEmpty( block[2] ) ) new_coded_block_pattern |= 0x08;
1431 get_non_intra_block_rq( bs, block[3], tr->quantizer_scale, tr->new_quantizer_scale );
1432 if( isNotEmpty( block[3] ) ) new_coded_block_pattern |= 0x04;
1436 get_non_intra_block_rq( bs, block[4], tr->quantizer_scale, tr->new_quantizer_scale );
1437 if( isNotEmpty( block[4] ) ) new_coded_block_pattern |= 0x02;
1441 get_non_intra_block_rq( bs, block[5], tr->quantizer_scale, tr->new_quantizer_scale );
1442 if( isNotEmpty( block[5] ) ) new_coded_block_pattern |= 0x01;
1444 if( !new_coded_block_pattern) macroblock_modes &= 0xFFFFFFED; // remove MACROBLOCK_PATTERN and MACROBLOCK_QUANT flag
1448 if (tr->last_coded_scale == tr->new_quantizer_scale) macroblock_modes &= 0xFFFFFFEF; // remove MACROBLOCK_QUANT
1449 else if (macroblock_modes & MACROBLOCK_PATTERN) macroblock_modes |= MACROBLOCK_QUANT; //add MACROBLOCK_QUANT
1450 assert( (macroblock_modes & MACROBLOCK_PATTERN) || !(macroblock_modes & MACROBLOCK_QUANT) );
1452 putmbdata( tr, macroblock_modes);
1453 if( macroblock_modes & MACROBLOCK_QUANT )
1455 put_quantiser( tr );
1458 // put saved motion data...
1459 for (batb = 0; batb < (p_n_w - p_n_ow); batb++)
1461 bs_write( bs, p_n_ow[batb], 8 );
1463 bs_write( bs, i_n_bit_out_cache, BITS_IN_BUF - i_n_bit_out);
1464 // end saved motion data...
1466 if (macroblock_modes & MACROBLOCK_PATTERN)
1469 bs_write( bs, cbptable[new_coded_block_pattern].code,cbptable[new_coded_block_pattern].len);
1471 if (new_coded_block_pattern & 0x20) putnonintrablk( bs, block[0]);
1472 if (new_coded_block_pattern & 0x10) putnonintrablk( bs, block[1]);
1473 if (new_coded_block_pattern & 0x08) putnonintrablk( bs, block[2]);
1474 if (new_coded_block_pattern & 0x04) putnonintrablk( bs, block[3]);
1475 if (new_coded_block_pattern & 0x02) putnonintrablk( bs, block[4]);
1476 if (new_coded_block_pattern & 0x01) putnonintrablk( bs, block[5]);
1480 //LOGF("\n\to: %i c: %i n: %i\n", quantizer_scale, last_coded_scale, new_quantizer_scale);
1487 if (bs->i_bit_in_cache >= 0x10000000)
1489 mba = MBA_5 + (UBITS (bs->i_bit_in_cache, 5) - 2);
1492 else if (bs->i_bit_in_cache >= 0x03000000)
1494 mba = MBA_11 + (UBITS (bs->i_bit_in_cache, 11) - 24);
1497 else if( UBITS (bs->i_bit_in_cache, 11 ) == 8 )
1499 /* macroblock_escape */
1509 bs_copy( bs, mba->len);
1510 mba_inc += mba->mba;
1519 /////---- end ext mpeg code
1521 static int do_next_start_code( transrate_t *tr )
1523 bs_transrate_t *bs = &tr->bs;
1530 *bs->p_w++ = *bs->p_c++;
1532 if (ID == 0x00) // pic header
1534 tr->picture_coding_type = (bs->p_c[1] >> 3) & 0x7;
1535 bs->p_c[1] |= 0x7; bs->p_c[2] = 0xFF; bs->p_c[3] |= 0xF8; // vbv_delay is now 0xFFFF
1537 memcpy(bs->p_w, bs->p_c, 4);
1541 else if (ID == 0xB3) // seq header
1543 tr->horizontal_size_value = (bs->p_c[0] << 4) | (bs->p_c[1] >> 4);
1544 tr->vertical_size_value = ((bs->p_c[1] & 0xF) << 8) | bs->p_c[2];
1545 if(!tr->horizontal_size_value || !tr->vertical_size_value )
1550 memcpy(bs->p_w, bs->p_c, 8 );
1554 else if (ID == 0xB5) // extension
1556 if ((bs->p_c[0] >> 4) == 0x8) // pic coding ext
1558 tr->f_code[0][0] = (bs->p_c[0] & 0xF) - 1;
1559 tr->f_code[0][1] = (bs->p_c[1] >> 4) - 1;
1560 tr->f_code[1][0] = (bs->p_c[1] & 0xF) - 1;
1561 tr->f_code[1][1] = (bs->p_c[2] >> 4) - 1;
1563 /* tr->intra_dc_precision = (bs->p_c[2] >> 2) & 0x3; */
1564 tr->picture_structure = bs->p_c[2] & 0x3;
1565 tr->frame_pred_frame_dct = (bs->p_c[3] >> 6) & 0x1;
1566 tr->concealment_motion_vectors = (bs->p_c[3] >> 5) & 0x1;
1567 tr->q_scale_type = (bs->p_c[3] >> 4) & 0x1;
1568 tr->intra_vlc_format = (bs->p_c[3] >> 3) & 0x1;
1569 /* tr->alternate_scan = (bs->p_c[3] >> 2) & 0x1; */
1572 memcpy(bs->p_w, bs->p_c, 5);
1578 *bs->p_w++ = *bs->p_c++;
1581 else if (ID == 0xB8) // gop header
1583 memcpy(bs->p_w, bs->p_c, 4);
1587 else if ((ID >= 0x01) && (ID <= 0xAF)) // slice
1589 uint8_t *outTemp = bs->p_w, *inTemp = bs->p_c;
1592 if( ( tr->picture_coding_type == B_TYPE && tr->quant_corr < 2.5f ) || // don't recompress if we're in advance!
1593 ( tr->picture_coding_type == P_TYPE && tr->quant_corr < -2.5f ) ||
1594 ( tr->picture_coding_type == I_TYPE && tr->quant_corr < -5.0f ) )
1596 if( ( tr->picture_coding_type == B_TYPE ) ||
1597 ( tr->picture_coding_type == P_TYPE && tr->level_p ) ||
1598 ( tr->picture_coding_type == I_TYPE && tr->level_i ) )
1601 if( !tr->horizontal_size_value || !tr->vertical_size_value )
1607 bs->i_bit_in_cache = 0; bs->i_bit_in = 0;
1608 bs->i_bit_out_cache = 0; bs->i_bit_out = BITS_IN_BUF;
1616 // begin bit level recoding
1617 mpeg2_slice(tr, ID);
1619 bs_flush_read( bs );
1620 bs_flush_write( bs );
1621 // end bit level recoding
1623 /* Basic sanity checks --Meuuh */
1624 if (bs->p_c > bs->p_r || bs->p_w > bs->p_rw)
1629 /*LOGF("type: %s code: %02i in : %6i out : %6i diff : %6i fact: %2.2f\n",
1630 (picture_coding_type == I_TYPE ? "I_TYPE" : (picture_coding_type == P_TYPE ? "P_TYPE" : "B_TYPE")),
1631 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));*/
1633 if (bs->p_w - outTemp > bs->p_c - inTemp) // yes that might happen, rarely
1635 /*LOGF("*** slice bigger than before !! (type: %s code: %i in : %i out : %i diff : %i)\n",
1636 (picture_coding_type == I_TYPE ? "I_TYPE" : (picture_coding_type == P_TYPE ? "P_TYPE" : "B_TYPE")),
1637 ID, bs->p_c - inTemp, bs->p_w - outTemp, (bs->p_w - outTemp) - (bs->p_c - inTemp));*/
1639 // in this case, we'll just use the original slice !
1640 memcpy(outTemp, inTemp, bs->p_c - inTemp);
1641 bs->p_w = outTemp + (bs->p_c - inTemp);
1643 // adjust bs->i_byte_out
1644 bs->i_byte_out -= (bs->p_w - outTemp) - (bs->p_c - inTemp);
1651 void E_(process_frame)( sout_stream_t *p_stream,
1652 sout_stream_id_t *id, sout_buffer_t *in, sout_buffer_t **out )
1654 transrate_t *tr = &id->tr;
1655 bs_transrate_t *bs = &tr->bs;
1657 sout_buffer_t *p_out;
1659 double next_fact_x = 1.0;
1661 /* The output buffer can't be bigger than the input buffer. */
1662 p_out = sout_BufferNew( p_stream->p_sout, in->i_size );
1664 p_out->i_length = in->i_length;
1665 p_out->i_dts = in->i_dts;
1666 p_out->i_pts = in->i_pts;
1667 p_out->i_flags = in->i_flags;
1669 sout_BufferChain( out, p_out );
1671 bs->p_rw = bs->p_ow = bs->p_w = p_out->p_buffer;
1672 bs->p_c = bs->p_r = in->p_buffer;
1673 bs->p_r += in->i_size + 4;
1674 bs->p_rw += in->i_size;
1675 *(in->p_buffer + in->i_size) = 0;
1676 *(in->p_buffer + in->i_size + 1) = 0;
1677 *(in->p_buffer + in->i_size + 2) = 1;
1678 *(in->p_buffer + in->i_size + 3) = 0;
1680 /* Calculate how late we are */
1681 tr->quant_corr = 0.0 + B_HANDICAP;
1684 bs->i_byte_in = in->i_size;
1687 if (tr->i_current_gop_size - in->i_size > 100)
1689 if (tr->i_wanted_gop_size == in->i_size)
1693 else if ( tr->i_wanted_gop_size < in->i_size )
1695 /* We're really late */
1700 next_fact_x = ((double)(tr->i_current_gop_size - in->i_size)) /
1701 (tr->i_wanted_gop_size - in->i_size);
1704 if (next_fact_x > QUANT_I)
1708 if (next_fact_x > QUANT_P)
1710 tr->level_p = 1 + (next_fact_x - QUANT_P) / (QUANT_P_INC);
1713 if ( tr->i_wanted_gop_size < 0 )
1715 /* We're really late */
1716 tr->current_fact_x = 3.0;
1720 tr->current_fact_x = ((double)(tr->i_current_gop_size) /
1721 (tr->i_wanted_gop_size));
1726 uint8_t *p_end = &in->p_buffer[in->i_size];
1728 /* Search next start code */
1731 if( bs->p_c < p_end - 3 && bs->p_c[0] == 0 && bs->p_c[1] == 0 && bs->p_c[2] == 1 )
1733 /* Next start code */
1736 else if( bs->p_c < p_end - 6 &&
1737 bs->p_c[0] == 0 && bs->p_c[1] == 0 && bs->p_c[2] == 0 &&
1738 bs->p_c[3] == 0 && bs->p_c[4] == 0 && bs->p_c[5] == 0 )
1740 /* remove stuffing (looking for 6 0x00 bytes) */
1746 *bs->p_w++ = *bs->p_c++;
1749 if( bs->p_c >= p_end)
1755 if( bs->p_c >= p_end )
1760 /* Copy the start code */
1761 memcpy(bs->p_w, bs->p_c, 3 );
1765 if (do_next_start_code( tr ) )
1771 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;
1774 bs->i_byte_out += bs->p_w - bs->p_ow;
1775 p_out->i_size = bs->p_w - bs->p_ow;
1776 tr->i_current_gop_size -= in->i_size;
1777 tr->i_wanted_gop_size -= p_out->i_size;
1778 tr->i_new_gop_size += bs->i_byte_out;
1781 msg_Dbg( p_stream, "%d: %d -> %d (r: %f, n:%f, corr:%f)",
1782 tr->picture_coding_type, in->i_size, p_out->i_size,
1783 (float)in->i_size / p_out->i_size,
1784 next_fact_x, tr->quant_corr);