1 /*****************************************************************************
3 *****************************************************************************
4 * Copyright (C) 2003 VideoLAN
5 * Copyright (C) 2003 Freebox S.A.
6 * Copyright (C) 2003 Antoine Missout
7 * Copyright (C) 2000-2003 Michel Lespinasse <walken@zoy.org>
8 * Copyright (C) 1999-2000 Aaron Holtzman <aholtzma@ess.engr.uvic.ca>
9 * $Id: transrate.c,v 1.3 2003/11/21 15:32:08 fenrir Exp $
11 * Authors: Christophe Massiot <massiot@via.ecp.fr>
12 * Laurent Aimar <fenrir@via.ecp.fr>
14 * Michel Lespinasse <walken@zoy.org>
15 * Aaron Holtzman <aholtzma@ess.engr.uvic.ca>
17 * This program is free software; you can redistribute it and/or modify
18 * it under the terms of the GNU General Public License as published by
19 * the Free Software Foundation; either version 2 of the License, or
20 * (at your option) any later version.
22 * This program is distributed in the hope that it will be useful,
23 * but WITHOUT ANY WARRANTY; without even the implied warranty of
24 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
25 * GNU General Public License for more details.
27 * You should have received a copy of the GNU General Public License
28 * along with this program; if not, write to the Free Software
29 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111, USA.
30 *****************************************************************************/
32 /*****************************************************************************
34 *****************************************************************************/
43 #include <vlc/input.h>
45 /*****************************************************************************
47 *****************************************************************************/
48 static int Open ( vlc_object_t * );
49 static void Close ( vlc_object_t * );
51 static sout_stream_id_t *Add ( sout_stream_t *, es_format_t * );
52 static int Del ( sout_stream_t *, sout_stream_id_t * );
53 static int Send( sout_stream_t *, sout_stream_id_t *, sout_buffer_t * );
55 static int transrate_video_process( sout_stream_t *, sout_stream_id_t *, sout_buffer_t *, sout_buffer_t ** );
57 /*****************************************************************************
59 *****************************************************************************/
61 set_description( _("Transrate stream") );
62 set_capability( "sout stream", 50 );
63 add_shortcut( "transrate" );
64 set_callbacks( Open, Close );
67 struct sout_stream_sys_t
73 mtime_t i_first_frame;
77 /*****************************************************************************
79 *****************************************************************************/
80 static int Open( vlc_object_t *p_this )
82 sout_stream_t *p_stream = (sout_stream_t*)p_this;
83 sout_stream_sys_t *p_sys;
86 p_sys = malloc( sizeof( sout_stream_sys_t ) );
87 p_sys->p_out = sout_stream_new( p_stream->p_sout, p_stream->psz_next );
89 p_sys->i_vbitrate = 0;
91 if( ( val = sout_cfg_find_value( p_stream->p_cfg, "vb" ) ) )
93 p_sys->i_vbitrate = atoi( val );
94 if( p_sys->i_vbitrate < 16000 )
96 p_sys->i_vbitrate *= 1000;
101 p_sys->i_vbitrate = 3000000;
103 p_sys->i_first_frame = 0;
105 msg_Dbg( p_stream, "codec video %dkb/s",
106 p_sys->i_vbitrate / 1024 );
110 msg_Err( p_stream, "cannot create chain" );
114 p_stream->pf_add = Add;
115 p_stream->pf_del = Del;
116 p_stream->pf_send = Send;
118 p_stream->p_sys = p_sys;
120 p_stream->p_sout->i_padding += 200;
125 /*****************************************************************************
127 *****************************************************************************/
128 static void Close( vlc_object_t * p_this )
130 sout_stream_t *p_stream = (sout_stream_t *)p_this;
131 sout_stream_sys_t *p_sys = p_stream->p_sys;
133 sout_stream_delete( p_sys->p_out );
139 /*****************************************************************************
141 *****************************************************************************/
159 uint32_t i_bit_in_cache;
160 uint32_t i_bit_out_cache;
173 unsigned int horizontal_size_value;
174 unsigned int vertical_size_value;
177 unsigned int picture_coding_type;
180 unsigned int f_code[2][2];
181 /* unsigned int intra_dc_precision; */
182 unsigned int picture_structure;
183 unsigned int frame_pred_frame_dct;
184 unsigned int concealment_motion_vectors;
185 unsigned int q_scale_type;
186 unsigned int intra_vlc_format;
187 /* unsigned int alternate_scan; */
190 // quantizer_scale_code
191 unsigned int quantizer_scale;
192 unsigned int new_quantizer_scale;
193 unsigned int last_coded_scale;
194 int h_offset, v_offset;
197 double quant_corr, fact_x;
201 struct sout_stream_id_t
204 vlc_bool_t b_transrate;
206 sout_buffer_t *p_current_buffer;
207 sout_buffer_t *p_next_gop;
208 mtime_t i_next_gop_duration;
209 size_t i_next_gop_size;
215 static sout_stream_id_t * Add( sout_stream_t *p_stream, es_format_t *p_fmt )
217 sout_stream_sys_t *p_sys = p_stream->p_sys;
218 sout_stream_id_t *id;
220 id = malloc( sizeof( sout_stream_id_t ) );
223 if( p_fmt->i_cat == VIDEO_ES
224 && p_fmt->i_codec == VLC_FOURCC('m', 'p', 'g', 'v') )
227 "creating video transrating for fcc=`%4.4s'",
228 (char*)&p_fmt->i_codec );
230 id->p_current_buffer = NULL;
231 id->p_next_gop = NULL;
232 id->i_next_gop_duration = 0;
233 id->i_next_gop_size = 0;
234 memset( &id->tr, 0, sizeof( transrate_t ) );
235 id->tr.bs.i_byte_in = id->tr.bs.i_byte_out = 0;
236 id->tr.quant_corr = 0.0;
239 /* open output stream */
240 id->id = p_sys->p_out->pf_add( p_sys->p_out, p_fmt );
241 id->b_transrate = VLC_TRUE;
245 msg_Dbg( p_stream, "not transrating a stream (fcc=`%4.4s')", (char*)&p_fmt->i_codec );
246 id->id = p_sys->p_out->pf_add( p_sys->p_out, p_fmt );
247 id->b_transrate = VLC_FALSE;
259 static int Del ( sout_stream_t *p_stream, sout_stream_id_t *id )
261 sout_stream_sys_t *p_sys = p_stream->p_sys;
265 p_sys->p_out->pf_del( p_sys->p_out, id->id );
272 static int Send( sout_stream_t *p_stream, sout_stream_id_t *id,
273 sout_buffer_t *p_buffer )
275 sout_stream_sys_t *p_sys = p_stream->p_sys;
277 if( id->b_transrate )
279 sout_buffer_t *p_buffer_out;
280 transrate_video_process( p_stream, id, p_buffer, &p_buffer_out );
284 return p_sys->p_out->pf_send( p_sys->p_out, id->id, p_buffer_out );
288 else if( id->id != NULL )
290 return p_sys->p_out->pf_send( p_sys->p_out, id->id, p_buffer );
294 sout_BufferDelete( p_stream->p_sout, p_buffer );
299 /****************************************************************************
300 * transrater, code from M2VRequantizer http://www.metakine.com/
301 ****************************************************************************/
304 // #define NDEBUG // turns off asserts
305 /* This is awful magic --Meuuh */
306 //#define REACT_DELAY (1024.0*128.0)
307 #define REACT_DELAY (1024.0*4.0)
312 // - the quantiser is increment by one step
314 // - non intra block:
315 // - in P_FRAME we keep the original quantiser but drop the last coefficient
316 // if there is more than one
317 // - in B_FRAME we multiply the quantiser by a factor
319 // - I_FRAME is recoded when we're 5.0 * REACT_DELAY late
320 // - P_FRAME is recoded when we're 2.5 * REACT_DELAY late
321 // - B_FRAME are always recoded
323 // if we're getting *very* late (60 * REACT_DELAY)
325 // - intra blocks quantiser is incremented two step
326 // - drop a few coefficients but always keep the first one
338 #ifdef HAVE_BUILTIN_EXPECT
339 #define likely(x) __builtin_expect ((x) != 0, 1)
340 #define unlikely(x) __builtin_expect ((x) != 0, 0)
342 #define likely(x) (x)
343 #define unlikely(x) (x)
346 #define BITS_IN_BUF (8)
348 #define LOG(msg) fprintf (stderr, msg)
349 #define LOGF(format, args...) fprintf (stderr, format, args)
351 static inline void bs_write( bs_transrate_t *s, unsigned int val, int n)
354 assert(!(val & (0xffffffffU << n)));
356 while (unlikely(n >= s->i_bit_out))
358 s->p_w[0] = (s->i_bit_out_cache << s->i_bit_out ) | (val >> (n - s->i_bit_out));
361 s->i_bit_out_cache = 0;
362 val &= ~(0xffffffffU << n);
363 s->i_bit_out = BITS_IN_BUF;
368 s->i_bit_out_cache = (s->i_bit_out_cache << n) | val;
372 assert(s->i_bit_out > 0);
373 assert(s->i_bit_out <= BITS_IN_BUF);
376 static inline void bs_refill( bs_transrate_t *s )
378 assert((s->p_r - s->p_c) >= 1);
379 s->i_bit_in_cache |= s->p_c[0] << (24 - s->i_bit_in);
384 static inline void bs_flush( bs_transrate_t *s, unsigned int n )
386 assert(s->i_bit_in >= n);
388 s->i_bit_in_cache <<= n;
391 assert( (!n) || ((n>0) && !(s->i_bit_in_cache & 0x1)) );
393 while (unlikely(s->i_bit_in < 24)) bs_refill( s );
396 static inline unsigned int bs_read( bs_transrate_t *s, unsigned int n)
398 unsigned int Val = ((unsigned int)s->i_bit_in_cache) >> (32 - n);
403 static inline unsigned int bs_copy( bs_transrate_t *s, unsigned int n)
405 unsigned int Val = bs_read( s, n);
410 static inline void bs_flush_read( bs_transrate_t *s )
412 int i = s->i_bit_in & 0x7;
415 assert(((unsigned int)bs->i_bit_in_cache) >> (32 - i) == 0);
416 s->i_bit_in_cache <<= i;
419 s->p_c += -1 * (s->i_bit_in >> 3);
422 static inline void bs_flush_write( bs_transrate_t *s )
424 if( s->i_bit_out != 8 ) bs_write(s, 0, s->i_bit_out);
427 /////---- begin ext mpeg code
429 const uint8_t non_linear_mquant_table[32] =
431 0, 1, 2, 3, 4, 5, 6, 7,
432 8,10,12,14,16,18,20,22,
433 24,28,32,36,40,44,48,52,
434 56,64,72,80,88,96,104,112
436 const uint8_t map_non_linear_mquant[113] =
438 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,
439 16,17,17,17,18,18,18,18,19,19,19,19,20,20,20,20,21,21,21,21,22,22,
440 22,22,23,23,23,23,24,24,24,24,24,24,24,25,25,25,25,25,25,25,26,26,
441 26,26,26,26,26,26,27,27,27,27,27,27,27,27,28,28,28,28,28,28,28,29,
442 29,29,29,29,29,29,29,29,29,30,30,30,30,30,30,30,31,31,31,31,31
445 static int scale_quant( unsigned int q_scale_type, double quant )
450 iquant = (int) floor(quant+0.5);
452 /* clip mquant to legal (linear) range */
453 if (iquant<1) iquant = 1;
454 if (iquant>112) iquant = 112;
456 iquant = non_linear_mquant_table[map_non_linear_mquant[iquant]];
460 /* clip mquant to legal (linear) range */
461 iquant = (int)floor(quant+0.5);
462 if (iquant<2) iquant = 2;
463 if (iquant>62) iquant = 62;
464 iquant = (iquant/2)*2; // Must be *even*
469 static int increment_quant( transrate_t *tr, int quant )
471 if( tr->q_scale_type)
473 assert(quant >= 1 && quant <= 112);
474 quant = map_non_linear_mquant[quant] + 1;
475 if( tr->quant_corr < -60.0f) quant++;
476 if( quant > 31) quant = 31;
477 quant = non_linear_mquant_table[quant];
481 assert(!(quant & 1));
483 if( tr->quant_corr < -60.0f) quant += 2;
484 if (quant > 62) quant = 62;
489 static inline int intmax( register int x, register int y )
491 return x < y ? y : x;
493 static inline int intmin( register int x, register int y )
495 return x < y ? x : y;
498 static int getNewQuant( transrate_t *tr, int curQuant)
500 bs_transrate_t *bs = &tr->bs;
502 double calc_quant, quant_to_use;
505 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;
506 calc_quant = curQuant * tr->fact_x;
507 quant_to_use = calc_quant - tr->quant_corr;
509 switch (tr->picture_coding_type )
513 mquant = increment_quant( tr, curQuant);
517 mquant = intmax(scale_quant( tr->q_scale_type, quant_to_use), increment_quant( tr, curQuant));
526 LOGF("type: %s orig_quant: %3i calc_quant: %7.1f quant_corr: %7.1f using_quant: %3i\n",
527 (picture_coding_type == I_TYPE ? "I_TYPE" : (picture_coding_type == P_TYPE ? "P_TYPE" : "B_TYPE")),
528 (int)curQuant, (float)calc_quant, (float)quant_corr, (int)mquant);
531 assert(mquant >= curQuant);
536 static inline int isNotEmpty(RunLevel *blk)
543 static void putAC( bs_transrate_t *bs, int run, int signed_level, int vlcformat)
546 const VLCtable *ptab = NULL;
548 level = (signed_level<0) ? -signed_level : signed_level; /* abs(signed_level) */
550 assert(!(run<0 || run>63 || level==0 || level>2047));
554 if (run<2 && level<41)
556 if (vlcformat) ptab = &dct_code_tab1a[run][level-1];
557 else ptab = &dct_code_tab1[run][level-1];
560 else if (run<32 && level<6)
562 if (vlcformat) ptab = &dct_code_tab2a[run-2][level-1];
563 else ptab = &dct_code_tab2[run-2][level-1];
567 if (len) /* a VLC code exists */
569 bs_write( bs, ptab->code, len);
570 bs_write( bs, signed_level<0, 1); /* sign */
574 bs_write( bs, 1l, 6); /* Escape */
575 bs_write( bs, run, 6); /* 6 bit code for run */
576 bs_write( bs, ((unsigned int)signed_level) & 0xFFF, 12);
581 static inline void putACfirst( bs_transrate_t *bs, int run, int val)
583 if (run==0 && (val==1 || val==-1)) bs_write( bs, 2|(val<0),2);
584 else putAC( bs, run,val,0);
587 static void putnonintrablk( bs_transrate_t *bs, RunLevel *blk)
591 putACfirst( bs, blk->run, blk->level);
596 putAC( bs, blk->run, blk->level, 0);
605 static const int non_linear_quantizer_scale [] =
607 0, 1, 2, 3, 4, 5, 6, 7,
608 8, 10, 12, 14, 16, 18, 20, 22,
609 24, 28, 32, 36, 40, 44, 48, 52,
610 56, 64, 72, 80, 88, 96, 104, 112
613 static inline int get_macroblock_modes( transrate_t *tr )
615 bs_transrate_t *bs = &tr->bs;
617 int macroblock_modes;
620 switch( tr->picture_coding_type)
624 tab = MB_I + UBITS (bs->i_bit_in_cache, 1);
625 bs_flush( bs, tab->len );
626 macroblock_modes = tab->modes;
628 if ((! ( tr->frame_pred_frame_dct)) && ( tr->picture_structure == FRAME_PICTURE))
630 macroblock_modes |= UBITS (bs->i_bit_in_cache, 1) * DCT_TYPE_INTERLACED;
634 return macroblock_modes;
638 tab = MB_P + UBITS (bs->i_bit_in_cache, 5);
639 bs_flush( bs, tab->len );
640 macroblock_modes = tab->modes;
642 if (tr->picture_structure != FRAME_PICTURE)
644 if (macroblock_modes & MACROBLOCK_MOTION_FORWARD)
646 macroblock_modes |= UBITS (bs->i_bit_in_cache, 2) * MOTION_TYPE_BASE;
649 return macroblock_modes;
651 else if (tr->frame_pred_frame_dct)
653 if (macroblock_modes & MACROBLOCK_MOTION_FORWARD)
654 macroblock_modes |= MC_FRAME;
655 return macroblock_modes;
659 if (macroblock_modes & MACROBLOCK_MOTION_FORWARD)
661 macroblock_modes |= UBITS (bs->i_bit_in_cache, 2) * MOTION_TYPE_BASE;
664 if (macroblock_modes & (MACROBLOCK_INTRA | MACROBLOCK_PATTERN))
666 macroblock_modes |= UBITS (bs->i_bit_in_cache, 1) * DCT_TYPE_INTERLACED;
669 return macroblock_modes;
674 tab = MB_B + UBITS (bs->i_bit_in_cache, 6);
675 bs_flush( bs, tab->len );
676 macroblock_modes = tab->modes;
678 if( tr->picture_structure != FRAME_PICTURE)
680 if (! (macroblock_modes & MACROBLOCK_INTRA))
682 macroblock_modes |= UBITS (bs->i_bit_in_cache, 2) * MOTION_TYPE_BASE;
685 return macroblock_modes;
687 else if (tr->frame_pred_frame_dct)
689 /* if (! (macroblock_modes & MACROBLOCK_INTRA)) */
690 macroblock_modes |= MC_FRAME;
691 return macroblock_modes;
695 if (macroblock_modes & MACROBLOCK_INTRA) goto intra;
696 macroblock_modes |= UBITS (bs->i_bit_in_cache, 2) * MOTION_TYPE_BASE;
698 if (macroblock_modes & (MACROBLOCK_INTRA | MACROBLOCK_PATTERN))
701 macroblock_modes |= UBITS (bs->i_bit_in_cache, 1) * DCT_TYPE_INTERLACED;
704 return macroblock_modes;
713 static inline int get_quantizer_scale( transrate_t *tr )
715 bs_transrate_t *bs = &tr->bs;
717 int quantizer_scale_code;
719 quantizer_scale_code = UBITS (bs->i_bit_in_cache, 5);
722 if( tr->q_scale_type )
723 return non_linear_quantizer_scale[quantizer_scale_code];
725 return quantizer_scale_code << 1;
728 static inline int get_motion_delta( bs_transrate_t *bs, const int f_code )
734 if (bs->i_bit_in_cache & 0x80000000)
739 else if (bs->i_bit_in_cache >= 0x0c000000)
742 tab = MV_4 + UBITS (bs->i_bit_in_cache, 4);
743 delta = (tab->delta << f_code) + 1;
744 bs_copy( bs, tab->len);
746 sign = SBITS (bs->i_bit_in_cache, 1);
749 if (f_code) delta += UBITS (bs->i_bit_in_cache, f_code);
750 bs_copy( bs, f_code);
752 return (delta ^ sign) - sign;
757 tab = MV_10 + UBITS (bs->i_bit_in_cache, 10);
758 delta = (tab->delta << f_code) + 1;
759 bs_copy( bs, tab->len);
761 sign = SBITS (bs->i_bit_in_cache, 1);
766 delta += UBITS (bs->i_bit_in_cache, f_code);
767 bs_copy( bs, f_code);
770 return (delta ^ sign) - sign;
775 static inline int get_dmv( bs_transrate_t *bs )
779 tab = DMV_2 + UBITS (bs->i_bit_in_cache, 2);
780 bs_copy( bs, tab->len);
784 static inline int get_coded_block_pattern( bs_transrate_t *bs )
788 if (bs->i_bit_in_cache >= 0x20000000)
790 tab = CBP_7 + (UBITS (bs->i_bit_in_cache, 7) - 16);
791 bs_flush( bs, tab->len );
796 tab = CBP_9 + UBITS (bs->i_bit_in_cache, 9);
797 bs_flush( bs, tab->len );
802 static inline int get_luma_dc_dct_diff( bs_transrate_t *bs )
808 if (bs->i_bit_in_cache < 0xf8000000)
810 tab = DC_lum_5 + UBITS (bs->i_bit_in_cache, 5);
814 bs_copy( bs, tab->len);
815 //dc_diff = UBITS (bs->i_bit_in_cache, size) - UBITS (SBITS (~bs->i_bit_in_cache, 1), size);
816 dc_diff = UBITS (bs->i_bit_in_cache, size);
817 if (!(dc_diff >> (size - 1))) dc_diff = (dc_diff + 1) - (1 << size);
829 tab = DC_long + (UBITS (bs->i_bit_in_cache, 9) - 0x1e0);
831 bs_copy( bs, tab->len);
832 //dc_diff = UBITS (bs->i_bit_in_cache, size) - UBITS (SBITS (~bs->i_bit_in_cache, 1), size);
833 dc_diff = UBITS (bs->i_bit_in_cache, size);
834 if (!(dc_diff >> (size - 1))) dc_diff = (dc_diff + 1) - (1 << size);
840 static inline int get_chroma_dc_dct_diff( bs_transrate_t *bs )
846 if (bs->i_bit_in_cache < 0xf8000000)
848 tab = DC_chrom_5 + UBITS (bs->i_bit_in_cache, 5);
852 bs_copy( bs, tab->len);
853 //dc_diff = UBITS (bs->i_bit_in_cache, size) - UBITS (SBITS (~bs->i_bit_in_cache, 1), size);
854 dc_diff = UBITS (bs->i_bit_in_cache, size);
855 if (!(dc_diff >> (size - 1))) dc_diff = (dc_diff + 1) - (1 << size);
866 tab = DC_long + (UBITS (bs->i_bit_in_cache, 10) - 0x3e0);
868 bs_copy( bs, tab->len + 1);
869 //dc_diff = UBITS (bs->i_bit_in_cache, size) - UBITS (SBITS (~bs->i_bit_in_cache, 1), size);
870 dc_diff = UBITS (bs->i_bit_in_cache, size);
871 if (!(dc_diff >> (size - 1))) dc_diff = (dc_diff + 1) - (1 << size);
877 static void get_intra_block_B14( bs_transrate_t *bs, const int i_qscale, const int i_qscale_new )
884 /* Basic sanity check --Meuuh */
890 tst = i_qscale_new/i_qscale + ((i_qscale_new%i_qscale) ? 1 : 0);
896 if (bs->i_bit_in_cache >= 0x28000000)
898 tab = DCT_B14AC_5 + (UBITS (bs->i_bit_in_cache, 5) - 5);
901 if (i >= 64) break; /* end of block */
904 bs_flush( bs, tab->len );
908 val = (val ^ SBITS (bs->i_bit_in_cache, 1)) - SBITS (bs->i_bit_in_cache, 1);
909 putAC( bs, i - li - 1, (val * i_qscale) / i_qscale_new, 0);
916 else if (bs->i_bit_in_cache >= 0x04000000)
918 tab = DCT_B14_8 + (UBITS (bs->i_bit_in_cache, 8) - 4);
921 if (i < 64) goto normal_code;
924 i += (UBITS (bs->i_bit_in_cache, 12) & 0x3F) - 64;
925 if (i >= 64) break; /* illegal, check needed to avoid buffer overflow */
928 val = SBITS (bs->i_bit_in_cache, 12);
931 putAC( bs, i - li - 1, (val * i_qscale) / i_qscale_new, 0);
939 else if (bs->i_bit_in_cache >= 0x02000000)
941 tab = DCT_B14_10 + (UBITS (bs->i_bit_in_cache, 10) - 8);
943 if (i < 64) goto normal_code;
945 else if (bs->i_bit_in_cache >= 0x00800000)
947 tab = DCT_13 + (UBITS (bs->i_bit_in_cache, 13) - 16);
949 if (i < 64) goto normal_code;
951 else if (bs->i_bit_in_cache >= 0x00200000)
953 tab = DCT_15 + (UBITS (bs->i_bit_in_cache, 15) - 16);
955 if (i < 64) goto normal_code;
959 tab = DCT_16 + UBITS (bs->i_bit_in_cache, 16);
962 if (i < 64) goto normal_code;
964 break; /* illegal, check needed to avoid buffer overflow */
967 bs_copy( bs, 2); /* end of block code */
970 static void get_intra_block_B15( bs_transrate_t *bs, const int i_qscale, int const i_qscale_new )
977 /* Basic sanity check --Meuuh */
983 tst = i_qscale_new/i_qscale + ((i_qscale_new%i_qscale) ? 1 : 0);
989 if (bs->i_bit_in_cache >= 0x04000000)
991 tab = DCT_B15_8 + (UBITS (bs->i_bit_in_cache, 8) - 4);
997 bs_flush( bs, tab->len );
1002 val = (val ^ SBITS (bs->i_bit_in_cache, 1)) - SBITS (bs->i_bit_in_cache, 1);
1003 putAC( bs, i - li - 1, (val * i_qscale) / i_qscale_new, 1);
1012 i += (UBITS (bs->i_bit_in_cache, 12) & 0x3F) - 64;
1014 if (i >= 64) break; /* illegal, check against buffer overflow */
1017 val = SBITS (bs->i_bit_in_cache, 12);
1018 if (abs(val) >= tst)
1020 putAC( bs, i - li - 1, (val * i_qscale) / i_qscale_new, 1);
1028 else if (bs->i_bit_in_cache >= 0x02000000)
1030 tab = DCT_B15_10 + (UBITS (bs->i_bit_in_cache, 10) - 8);
1032 if (i < 64) goto normal_code;
1034 else if (bs->i_bit_in_cache >= 0x00800000)
1036 tab = DCT_13 + (UBITS (bs->i_bit_in_cache, 13) - 16);
1038 if (i < 64) goto normal_code;
1040 else if (bs->i_bit_in_cache >= 0x00200000)
1042 tab = DCT_15 + (UBITS (bs->i_bit_in_cache, 15) - 16);
1044 if (i < 64) goto normal_code;
1048 tab = DCT_16 + UBITS (bs->i_bit_in_cache, 16);
1051 if (i < 64) goto normal_code;
1053 break; /* illegal, check needed to avoid buffer overflow */
1056 bs_copy( bs, 4); /* end of block code */
1060 static int get_non_intra_block_drop( transrate_t *tr, RunLevel *blk)
1062 bs_transrate_t *bs = &tr->bs;
1067 RunLevel *sblk = blk + 1;
1071 if (bs->i_bit_in_cache >= 0x28000000)
1073 tab = DCT_B14DC_5 + (UBITS (bs->i_bit_in_cache, 5) - 5);
1080 if (bs->i_bit_in_cache >= 0x28000000)
1082 tab = DCT_B14AC_5 + (UBITS (bs->i_bit_in_cache, 5) - 5);
1086 if (i >= 64) break; /* end of block */
1090 bs_flush( bs, tab->len );
1092 val = (val ^ SBITS (bs->i_bit_in_cache, 1)) - SBITS (bs->i_bit_in_cache, 1); /* if (bitstream_get (1)) val = -val; */
1095 blk->run = i - li - 1;
1105 if (bs->i_bit_in_cache >= 0x04000000)
1107 tab = DCT_B14_8 + (UBITS (bs->i_bit_in_cache, 8) - 4);
1110 if (i < 64) goto normal_code;
1114 i += (UBITS (bs->i_bit_in_cache, 12) & 0x3F) - 64;
1116 if (i >= 64) break; /* illegal, check needed to avoid buffer overflow */
1119 val = SBITS (bs->i_bit_in_cache, 12);
1122 blk->run = i - li - 1;
1129 else if (bs->i_bit_in_cache >= 0x02000000)
1131 tab = DCT_B14_10 + (UBITS (bs->i_bit_in_cache, 10) - 8);
1133 if (i < 64) goto normal_code;
1135 else if (bs->i_bit_in_cache >= 0x00800000)
1137 tab = DCT_13 + (UBITS (bs->i_bit_in_cache, 13) - 16);
1139 if (i < 64) goto normal_code;
1141 else if (bs->i_bit_in_cache >= 0x00200000)
1143 tab = DCT_15 + (UBITS (bs->i_bit_in_cache, 15) - 16);
1145 if (i < 64) goto normal_code;
1149 tab = DCT_16 + UBITS (bs->i_bit_in_cache, 16);
1152 if (i < 64) goto normal_code;
1154 break; /* illegal, check needed to avoid buffer overflow */
1156 bs_flush( bs, 2 ); /* dump end of block code */
1158 // remove last coeff
1162 // remove more coeffs if very late
1163 if ((tr->quant_corr < -60.0f) && (blk != sblk))
1166 if ((tr->quant_corr < -80.0f) && (blk != sblk))
1169 if ((tr->quant_corr < -100.0f) && (blk != sblk))
1172 if ((tr->quant_corr < -120.0f) && (blk != sblk))
1184 static int get_non_intra_block_rq( bs_transrate_t *bs, RunLevel *blk, const int i_qscale, const int i_qscale_new )
1191 /* Basic sanity check --Meuuh */
1197 tst = i_qscale_new/i_qscale + ((i_qscale_new%i_qscale) ? 1 : 0);
1201 if (bs->i_bit_in_cache >= 0x28000000)
1203 tab = DCT_B14DC_5 + (UBITS (bs->i_bit_in_cache, 5) - 5);
1210 if (bs->i_bit_in_cache >= 0x28000000)
1212 tab = DCT_B14AC_5 + (UBITS (bs->i_bit_in_cache, 5) - 5);
1217 break; /* end of block */
1221 bs_flush( bs, tab->len );
1225 val = (val ^ SBITS (bs->i_bit_in_cache, 1)) - SBITS (bs->i_bit_in_cache, 1);
1226 blk->level = (val * i_qscale) / i_qscale_new;
1227 blk->run = i - li - 1;
1232 //if ( ((val) && (tab->level < tst)) || ((!val) && (tab->level >= tst)) )
1233 // LOGF("level: %i val: %i tst : %i q: %i nq : %i\n", tab->level, val, tst, q, nq);
1240 if (bs->i_bit_in_cache >= 0x04000000)
1242 tab = DCT_B14_8 + (UBITS (bs->i_bit_in_cache, 8) - 4);
1245 if (i < 64) goto normal_code;
1249 i += (UBITS (bs->i_bit_in_cache, 12) & 0x3F) - 64;
1251 if (i >= 64) break; /* illegal, check needed to avoid buffer overflow */
1254 val = SBITS (bs->i_bit_in_cache, 12);
1255 if (abs(val) >= tst)
1257 blk->level = (val * i_qscale) / i_qscale_new;
1258 blk->run = i - li - 1;
1266 else if (bs->i_bit_in_cache >= 0x02000000)
1268 tab = DCT_B14_10 + (UBITS (bs->i_bit_in_cache, 10) - 8);
1270 if (i < 64) goto normal_code;
1272 else if (bs->i_bit_in_cache >= 0x00800000)
1274 tab = DCT_13 + (UBITS (bs->i_bit_in_cache, 13) - 16);
1276 if (i < 64) goto normal_code;
1278 else if (bs->i_bit_in_cache >= 0x00200000)
1280 tab = DCT_15 + (UBITS (bs->i_bit_in_cache, 15) - 16);
1282 if (i < 64) goto normal_code;
1286 tab = DCT_16 + UBITS (bs->i_bit_in_cache, 16);
1290 if (i < 64) goto normal_code;
1292 break; /* illegal, check needed to avoid buffer overflow */
1294 bs_flush( bs, 2 ); /* dump end of block code */
1301 static void motion_fr_frame( bs_transrate_t *bs, unsigned int f_code[2] )
1303 get_motion_delta( bs, f_code[0] );
1304 get_motion_delta( bs, f_code[1] );
1307 static void motion_fr_field( bs_transrate_t *bs, unsigned int f_code[2] )
1311 get_motion_delta( bs, f_code[0]);
1312 get_motion_delta( bs, f_code[1]);
1316 get_motion_delta( bs, f_code[0]);
1317 get_motion_delta( bs, f_code[1]);
1320 static void motion_fr_dmv( bs_transrate_t *bs, unsigned int f_code[2] )
1322 get_motion_delta( bs, f_code[0]);
1325 get_motion_delta( bs, f_code[1]);
1329 static void motion_fi_field( bs_transrate_t *bs, unsigned int f_code[2] )
1333 get_motion_delta( bs, f_code[0]);
1334 get_motion_delta( bs, f_code[1]);
1337 static void motion_fi_16x8( bs_transrate_t *bs, unsigned int f_code[2] )
1341 get_motion_delta( bs, f_code[0]);
1342 get_motion_delta( bs, f_code[1]);
1346 get_motion_delta( bs, f_code[0]);
1347 get_motion_delta( bs, f_code[1]);
1350 static void motion_fi_dmv( bs_transrate_t *bs, unsigned int f_code[2] )
1352 get_motion_delta( bs, f_code[0]);
1355 get_motion_delta( bs, f_code[1]);
1360 #define MOTION_CALL(routine,direction) \
1362 if ((direction) & MACROBLOCK_MOTION_FORWARD) \
1363 routine( bs, tr->f_code[0]); \
1364 if ((direction) & MACROBLOCK_MOTION_BACKWARD) \
1365 routine( bs, tr->f_code[1]); \
1368 #define NEXT_MACROBLOCK \
1370 tr->h_offset += 16; \
1371 if( tr->h_offset == tr->horizontal_size_value) \
1373 tr->v_offset += 16; \
1374 if (tr->v_offset > (tr->vertical_size_value - 16)) return; \
1379 static void putmbdata( transrate_t *tr, int macroblock_modes )
1381 bs_transrate_t *bs = &tr->bs;
1384 mbtypetab[tr->picture_coding_type-1][macroblock_modes&0x1F].code,
1385 mbtypetab[tr->picture_coding_type-1][macroblock_modes&0x1F].len);
1387 switch ( tr->picture_coding_type)
1390 if ((! (tr->frame_pred_frame_dct)) && (tr->picture_structure == FRAME_PICTURE))
1391 bs_write( bs, macroblock_modes & DCT_TYPE_INTERLACED ? 1 : 0, 1);
1395 if (tr->picture_structure != FRAME_PICTURE)
1397 if (macroblock_modes & MACROBLOCK_MOTION_FORWARD)
1398 bs_write( bs, (macroblock_modes & MOTION_TYPE_MASK) / MOTION_TYPE_BASE, 2);
1401 else if (tr->frame_pred_frame_dct) break;
1404 if (macroblock_modes & MACROBLOCK_MOTION_FORWARD)
1405 bs_write( bs, (macroblock_modes & MOTION_TYPE_MASK) / MOTION_TYPE_BASE, 2);
1406 if (macroblock_modes & (MACROBLOCK_INTRA | MACROBLOCK_PATTERN))
1407 bs_write( bs, macroblock_modes & DCT_TYPE_INTERLACED ? 1 : 0, 1);
1412 if (tr->picture_structure != FRAME_PICTURE)
1414 if (! (macroblock_modes & MACROBLOCK_INTRA))
1415 bs_write( bs, (macroblock_modes & MOTION_TYPE_MASK) / MOTION_TYPE_BASE, 2);
1418 else if (tr->frame_pred_frame_dct) break;
1421 if (macroblock_modes & MACROBLOCK_INTRA) goto intra;
1422 bs_write( bs, (macroblock_modes & MOTION_TYPE_MASK) / MOTION_TYPE_BASE, 2);
1423 if (macroblock_modes & (MACROBLOCK_INTRA | MACROBLOCK_PATTERN))
1426 bs_write( bs, macroblock_modes & DCT_TYPE_INTERLACED ? 1 : 0, 1);
1433 static inline void put_quantiser( transrate_t *tr )
1435 bs_transrate_t *bs = &tr->bs;
1437 bs_write( bs, tr->q_scale_type ? map_non_linear_mquant[tr->new_quantizer_scale] : tr->new_quantizer_scale >> 1, 5);
1438 tr->last_coded_scale = tr->new_quantizer_scale;
1441 static int slice_init( transrate_t *tr, int code)
1443 bs_transrate_t *bs = &tr->bs;
1447 tr->v_offset = (code - 1) * 16;
1449 tr->quantizer_scale = get_quantizer_scale( tr );
1450 if ( tr->picture_coding_type == P_TYPE)
1452 tr->new_quantizer_scale = tr->quantizer_scale;
1456 tr->new_quantizer_scale = getNewQuant(tr, tr->quantizer_scale);
1458 put_quantiser( tr );
1460 /*LOGF("************************\nstart of slice %i in %s picture. ori quant: %i new quant: %i\n", code,
1461 (picture_coding_type == I_TYPE ? "I_TYPE" : (picture_coding_type == P_TYPE ? "P_TYPE" : "B_TYPE")),
1462 quantizer_scale, new_quantizer_scale);*/
1464 /* ignore intra_slice and all the extra data */
1465 while (bs->i_bit_in_cache & 0x80000000)
1470 /* decode initial macroblock address increment */
1474 if (bs->i_bit_in_cache >= 0x08000000)
1476 mba = MBA_5 + (UBITS (bs->i_bit_in_cache, 6) - 2);
1479 else if (bs->i_bit_in_cache >= 0x01800000)
1481 mba = MBA_11 + (UBITS (bs->i_bit_in_cache, 12) - 24);
1484 else if( UBITS (bs->i_bit_in_cache, 12 ) == 8 )
1486 /* macroblock_escape */
1496 bs_copy( bs, mba->len + 1);
1497 tr->h_offset = (offset + mba->mba) << 4;
1499 while( tr->h_offset - (int)tr->horizontal_size_value >= 0)
1501 tr->h_offset -= tr->horizontal_size_value;
1505 if( tr->v_offset > tr->vertical_size_value - 16 )
1512 static void mpeg2_slice( transrate_t *tr, const int code )
1514 bs_transrate_t *bs = &tr->bs;
1516 if( slice_init( tr, code ) )
1523 int macroblock_modes;
1527 macroblock_modes = get_macroblock_modes( tr );
1528 if (macroblock_modes & MACROBLOCK_QUANT) tr->quantizer_scale = get_quantizer_scale( tr );
1530 //LOGF("blk %i : ", h_offset >> 4);
1532 if (macroblock_modes & MACROBLOCK_INTRA)
1534 //LOG("intra "); if (macroblock_modes & MACROBLOCK_QUANT) LOGF("got new quant: %i ", quantizer_scale);
1536 tr->new_quantizer_scale = increment_quant( tr, tr->quantizer_scale);
1537 if (tr->last_coded_scale == tr->new_quantizer_scale) macroblock_modes &= 0xFFFFFFEF; // remove MACROBLOCK_QUANT
1538 else macroblock_modes |= MACROBLOCK_QUANT; //add MACROBLOCK_QUANT
1539 putmbdata( tr, macroblock_modes);
1540 if (macroblock_modes & MACROBLOCK_QUANT) put_quantiser( tr );
1542 //if (macroblock_modes & MACROBLOCK_QUANT) LOGF("put new quant: %i ", new_quantizer_scale);
1544 if (tr->concealment_motion_vectors)
1546 if (tr->picture_structure != FRAME_PICTURE)
1548 bs_copy( bs, 1); /* remove field_select */
1550 /* like motion_frame, but parsing without actual motion compensation */
1551 get_motion_delta( bs, tr->f_code[0][0]);
1552 get_motion_delta( bs, tr->f_code[0][1]);
1554 bs_copy( bs, 1); /* remove marker_bit */
1557 if( tr->intra_vlc_format )
1560 get_luma_dc_dct_diff( bs ); get_intra_block_B15( bs, tr->quantizer_scale, tr->new_quantizer_scale );
1561 get_luma_dc_dct_diff( bs ); get_intra_block_B15( bs, tr->quantizer_scale, tr->new_quantizer_scale );
1562 get_luma_dc_dct_diff( bs ); get_intra_block_B15( bs, tr->quantizer_scale, tr->new_quantizer_scale );
1563 get_luma_dc_dct_diff( bs ); get_intra_block_B15( bs, tr->quantizer_scale, tr->new_quantizer_scale );
1565 get_chroma_dc_dct_diff( bs ); get_intra_block_B15( bs, tr->quantizer_scale, tr->new_quantizer_scale );
1566 get_chroma_dc_dct_diff( bs ); get_intra_block_B15( bs, tr->quantizer_scale, tr->new_quantizer_scale );
1571 get_luma_dc_dct_diff( bs ); get_intra_block_B14( bs, tr->quantizer_scale, tr->new_quantizer_scale );
1572 get_luma_dc_dct_diff( bs ); get_intra_block_B14( bs, tr->quantizer_scale, tr->new_quantizer_scale );
1573 get_luma_dc_dct_diff( bs ); get_intra_block_B14( bs, tr->quantizer_scale, tr->new_quantizer_scale );
1574 get_luma_dc_dct_diff( bs ); get_intra_block_B14( bs, tr->quantizer_scale, tr->new_quantizer_scale );
1576 get_chroma_dc_dct_diff( bs ); get_intra_block_B14( bs, tr->quantizer_scale, tr->new_quantizer_scale );
1577 get_chroma_dc_dct_diff( bs ); get_intra_block_B14( bs, tr->quantizer_scale, tr->new_quantizer_scale );
1582 RunLevel block[6][65]; // terminated by level = 0, so we need 64+1
1583 int new_coded_block_pattern = 0;
1585 // begin saving data
1587 uint8_t p_n_ow[32], *p_n_w,
1588 *p_o_ow = bs->p_ow, *p_o_w = bs->p_w;
1589 uint32_t i_n_bit_out, i_n_bit_out_cache,
1590 i_o_bit_out = bs->i_bit_out, i_o_bit_out_cache = bs->i_bit_out_cache;
1592 bs->i_bit_out_cache = 0; bs->i_bit_out = BITS_IN_BUF;
1593 bs->p_ow = bs->p_w = p_n_ow;
1595 if (tr->picture_structure == FRAME_PICTURE)
1596 switch (macroblock_modes & MOTION_TYPE_MASK)
1598 case MC_FRAME: MOTION_CALL (motion_fr_frame, macroblock_modes); break;
1599 case MC_FIELD: MOTION_CALL (motion_fr_field, macroblock_modes); break;
1600 case MC_DMV: MOTION_CALL (motion_fr_dmv, MACROBLOCK_MOTION_FORWARD); break;
1603 switch (macroblock_modes & MOTION_TYPE_MASK)
1605 case MC_FIELD: MOTION_CALL (motion_fi_field, macroblock_modes); break;
1606 case MC_16X8: MOTION_CALL (motion_fi_16x8, macroblock_modes); break;
1607 case MC_DMV: MOTION_CALL (motion_fi_dmv, MACROBLOCK_MOTION_FORWARD); break;
1610 assert(bs->p_w - bs->p_ow < 32);
1613 i_n_bit_out = bs->i_bit_out;
1614 i_n_bit_out_cache = bs->i_bit_out_cache;
1615 assert(bs->p_ow == p_n_ow);
1617 bs->i_bit_out = i_o_bit_out ;
1618 bs->i_bit_out_cache = i_o_bit_out_cache;
1623 if ( tr->picture_coding_type == P_TYPE) tr->new_quantizer_scale = tr->quantizer_scale;
1624 else tr->new_quantizer_scale = getNewQuant( tr, tr->quantizer_scale);
1626 //LOG("non intra "); if (macroblock_modes & MACROBLOCK_QUANT) LOGF("got new quant: %i ", quantizer_scale);
1628 if (macroblock_modes & MACROBLOCK_PATTERN)
1630 const int cbp = get_coded_block_pattern( bs );
1632 if( tr->picture_coding_type == P_TYPE )
1634 if( cbp&0x20 ) get_non_intra_block_drop( tr, block[0] );
1635 if( cbp&0x10 ) get_non_intra_block_drop( tr, block[1] );
1636 if( cbp&0x08 ) get_non_intra_block_drop( tr, block[2] );
1637 if( cbp&0x04 ) get_non_intra_block_drop( tr, block[3] );
1638 if( cbp&0x02 ) get_non_intra_block_drop( tr, block[4] );
1639 if( cbp&0x01 ) get_non_intra_block_drop( tr, block[5] );
1641 new_coded_block_pattern = cbp;
1647 get_non_intra_block_rq( bs, block[0], tr->quantizer_scale, tr->new_quantizer_scale );
1648 if( isNotEmpty( block[0] ) ) new_coded_block_pattern |= 0x20;
1652 get_non_intra_block_rq( bs, block[1], tr->quantizer_scale, tr->new_quantizer_scale );
1653 if( isNotEmpty( block[1] ) ) new_coded_block_pattern |= 0x10;
1657 get_non_intra_block_rq( bs, block[2], tr->quantizer_scale, tr->new_quantizer_scale );
1658 if( isNotEmpty( block[2] ) ) new_coded_block_pattern |= 0x08;
1662 get_non_intra_block_rq( bs, block[3], tr->quantizer_scale, tr->new_quantizer_scale );
1663 if( isNotEmpty( block[3] ) ) new_coded_block_pattern |= 0x04;
1667 get_non_intra_block_rq( bs, block[4], tr->quantizer_scale, tr->new_quantizer_scale );
1668 if( isNotEmpty( block[4] ) ) new_coded_block_pattern |= 0x02;
1672 get_non_intra_block_rq( bs, block[5], tr->quantizer_scale, tr->new_quantizer_scale );
1673 if( isNotEmpty( block[5] ) ) new_coded_block_pattern |= 0x01;
1675 if( !new_coded_block_pattern) macroblock_modes &= 0xFFFFFFED; // remove MACROBLOCK_PATTERN and MACROBLOCK_QUANT flag
1679 if (tr->last_coded_scale == tr->new_quantizer_scale) macroblock_modes &= 0xFFFFFFEF; // remove MACROBLOCK_QUANT
1680 else if (macroblock_modes & MACROBLOCK_PATTERN) macroblock_modes |= MACROBLOCK_QUANT; //add MACROBLOCK_QUANT
1681 assert( (macroblock_modes & MACROBLOCK_PATTERN) || !(macroblock_modes & MACROBLOCK_QUANT) );
1683 putmbdata( tr, macroblock_modes);
1684 if( macroblock_modes & MACROBLOCK_QUANT )
1686 put_quantiser( tr );
1689 // put saved motion data...
1690 for (batb = 0; batb < (p_n_w - p_n_ow); batb++)
1692 bs_write( bs, p_n_ow[batb], 8 );
1694 bs_write( bs, i_n_bit_out_cache, BITS_IN_BUF - i_n_bit_out);
1695 // end saved motion data...
1697 if (macroblock_modes & MACROBLOCK_PATTERN)
1700 bs_write( bs, cbptable[new_coded_block_pattern].code,cbptable[new_coded_block_pattern].len);
1702 if (new_coded_block_pattern & 0x20) putnonintrablk( bs, block[0]);
1703 if (new_coded_block_pattern & 0x10) putnonintrablk( bs, block[1]);
1704 if (new_coded_block_pattern & 0x08) putnonintrablk( bs, block[2]);
1705 if (new_coded_block_pattern & 0x04) putnonintrablk( bs, block[3]);
1706 if (new_coded_block_pattern & 0x02) putnonintrablk( bs, block[4]);
1707 if (new_coded_block_pattern & 0x01) putnonintrablk( bs, block[5]);
1711 //LOGF("\n\to: %i c: %i n: %i\n", quantizer_scale, last_coded_scale, new_quantizer_scale);
1718 if (bs->i_bit_in_cache >= 0x10000000)
1720 mba = MBA_5 + (UBITS (bs->i_bit_in_cache, 5) - 2);
1723 else if (bs->i_bit_in_cache >= 0x03000000)
1725 mba = MBA_11 + (UBITS (bs->i_bit_in_cache, 11) - 24);
1728 else if( UBITS (bs->i_bit_in_cache, 11 ) == 8 )
1730 /* macroblock_escape */
1740 bs_copy( bs, mba->len);
1741 mba_inc += mba->mba;
1750 /////---- end ext mpeg code
1752 static int do_next_start_code( transrate_t *tr )
1754 bs_transrate_t *bs = &tr->bs;
1761 *bs->p_w++ = *bs->p_c++;
1763 if (ID == 0x00) // pic header
1765 tr->picture_coding_type = (bs->p_c[1] >> 3) & 0x7;
1766 bs->p_c[1] |= 0x7; bs->p_c[2] = 0xFF; bs->p_c[3] |= 0xF8; // vbv_delay is now 0xFFFF
1768 memcpy(bs->p_w, bs->p_c, 4);
1772 else if (ID == 0xB3) // seq header
1774 tr->horizontal_size_value = (bs->p_c[0] << 4) | (bs->p_c[1] >> 4);
1775 tr->vertical_size_value = ((bs->p_c[1] & 0xF) << 8) | bs->p_c[2];
1776 if(!tr->horizontal_size_value || !tr->vertical_size_value )
1781 memcpy(bs->p_w, bs->p_c, 8 );
1785 else if (ID == 0xB5) // extension
1787 if ((bs->p_c[0] >> 4) == 0x8) // pic coding ext
1789 tr->f_code[0][0] = (bs->p_c[0] & 0xF) - 1;
1790 tr->f_code[0][1] = (bs->p_c[1] >> 4) - 1;
1791 tr->f_code[1][0] = (bs->p_c[1] & 0xF) - 1;
1792 tr->f_code[1][1] = (bs->p_c[2] >> 4) - 1;
1794 /* tr->intra_dc_precision = (bs->p_c[2] >> 2) & 0x3; */
1795 tr->picture_structure = bs->p_c[2] & 0x3;
1796 tr->frame_pred_frame_dct = (bs->p_c[3] >> 6) & 0x1;
1797 tr->concealment_motion_vectors = (bs->p_c[3] >> 5) & 0x1;
1798 tr->q_scale_type = (bs->p_c[3] >> 4) & 0x1;
1799 tr->intra_vlc_format = (bs->p_c[3] >> 3) & 0x1;
1800 /* tr->alternate_scan = (bs->p_c[3] >> 2) & 0x1; */
1803 memcpy(bs->p_w, bs->p_c, 5);
1809 *bs->p_w++ = *bs->p_c++;
1812 else if (ID == 0xB8) // gop header
1814 memcpy(bs->p_w, bs->p_c, 4);
1818 else if ((ID >= 0x01) && (ID <= 0xAF)) // slice
1820 uint8_t *outTemp = bs->p_w, *inTemp = bs->p_c;
1822 if( ( tr->picture_coding_type == B_TYPE && tr->quant_corr < 2.5f ) || // don't recompress if we're in advance!
1823 ( tr->picture_coding_type == P_TYPE && tr->quant_corr < -2.5f ) ||
1824 ( tr->picture_coding_type == I_TYPE && tr->quant_corr < -5.0f ) )
1826 if( !tr->horizontal_size_value || !tr->vertical_size_value )
1832 bs->i_bit_in_cache = 0; bs->i_bit_in = 0;
1833 bs->i_bit_out_cache = 0; bs->i_bit_out = BITS_IN_BUF;
1841 // begin bit level recoding
1842 mpeg2_slice(tr, ID);
1844 bs_flush_read( bs );
1845 bs_flush_write( bs );
1846 // end bit level recoding
1848 /* Basic sanity checks --Meuuh */
1849 if (bs->p_c > bs->p_r || bs->p_w > bs->p_rw)
1854 /*LOGF("type: %s code: %02i in : %6i out : %6i diff : %6i fact: %2.2f\n",
1855 (picture_coding_type == I_TYPE ? "I_TYPE" : (picture_coding_type == P_TYPE ? "P_TYPE" : "B_TYPE")),
1856 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));*/
1858 if (bs->p_w - outTemp > bs->p_c - inTemp) // yes that might happen, rarely
1860 /*LOGF("*** slice bigger than before !! (type: %s code: %i in : %i out : %i diff : %i)\n",
1861 (picture_coding_type == I_TYPE ? "I_TYPE" : (picture_coding_type == P_TYPE ? "P_TYPE" : "B_TYPE")),
1862 ID, bs->p_c - inTemp, bs->p_w - outTemp, (bs->p_w - outTemp) - (bs->p_c - inTemp));*/
1864 // in this case, we'll just use the original slice !
1865 memcpy(outTemp, inTemp, bs->p_c - inTemp);
1866 bs->p_w = outTemp + (bs->p_c - inTemp);
1868 // adjust bs->i_byte_out
1869 bs->i_byte_out -= (bs->p_w - outTemp) - (bs->p_c - inTemp);
1876 static void process_frame( sout_stream_t *p_stream,
1877 sout_stream_id_t *id, sout_buffer_t *in, sout_buffer_t **out )
1879 transrate_t *tr = &id->tr;
1880 bs_transrate_t *bs = &tr->bs;
1882 sout_buffer_t *p_out;
1884 /* The output buffer can't be bigger than the input buffer. */
1885 p_out = sout_BufferNew( p_stream->p_sout, in->i_size );
1887 p_out->i_length = in->i_length;
1888 p_out->i_dts = in->i_dts;
1889 p_out->i_pts = in->i_pts;
1891 sout_BufferChain( out, p_out );
1893 bs->p_rw = bs->p_ow = bs->p_w = p_out->p_buffer;
1894 bs->p_c = bs->p_r = in->p_buffer;
1895 bs->p_r += in->i_size + 4;
1896 bs->p_rw += in->i_size;
1897 *(in->p_buffer + in->i_size) = 0;
1898 *(in->p_buffer + in->i_size + 1) = 0;
1899 *(in->p_buffer + in->i_size + 2) = 1;
1900 *(in->p_buffer + in->i_size + 3) = 0;
1901 bs->i_byte_in += in->i_size;
1905 uint8_t *p_end = &in->p_buffer[in->i_size];
1907 /* Search next start code */
1910 if( bs->p_c < p_end - 3 && bs->p_c[0] == 0 && bs->p_c[1] == 0 && bs->p_c[2] == 1 )
1912 /* Next start code */
1915 else if( bs->p_c < p_end - 6 &&
1916 bs->p_c[0] == 0 && bs->p_c[1] == 0 && bs->p_c[2] == 0 &&
1917 bs->p_c[3] == 0 && bs->p_c[4] == 0 && bs->p_c[5] == 0 )
1919 /* remove stuffing (looking for 6 0x00 bytes) */
1925 *bs->p_w++ = *bs->p_c++;
1928 if( bs->p_c >= p_end)
1934 if( bs->p_c >= p_end )
1939 /* Copy the start code */
1940 memcpy(bs->p_w, bs->p_c, 3 );
1944 if (do_next_start_code( tr ) )
1950 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;
1953 bs->i_byte_out += bs->p_w - bs->p_ow;
1954 p_out->i_size = bs->p_w - bs->p_ow;
1957 static int transrate_video_process( sout_stream_t *p_stream,
1958 sout_stream_id_t *id, sout_buffer_t *in, sout_buffer_t **out )
1960 transrate_t *tr = &id->tr;
1961 bs_transrate_t *bs = &tr->bs;
1965 if ( in->i_flags & SOUT_BUFFER_FLAGS_GOP )
1967 while ( id->p_current_buffer != NULL )
1969 sout_buffer_t * p_next = id->p_current_buffer->p_next;
1970 if ( tr->fact_x == 1.0 )
1972 bs->i_byte_out += id->p_current_buffer->i_size;
1973 id->p_current_buffer->p_next = NULL;
1974 sout_BufferChain( out, id->p_current_buffer );
1978 process_frame( p_stream, id, id->p_current_buffer, out );
1979 sout_BufferDelete(p_stream->p_sout, id->p_current_buffer);
1981 id->p_current_buffer = p_next;
1984 if ( id->i_next_gop_duration )
1986 mtime_t i_bitrate = (mtime_t)id->i_next_gop_size * 8000
1987 / (id->i_next_gop_duration / 1000);
1988 static mtime_t i_old_bitrate = 0;
1989 static mtime_t i_old_duration = 0;
1992 msg_Dbg(p_stream, "bitrate = %lld -> %lld", i_old_bitrate,
1993 (mtime_t)bs->i_byte_out * 8000 / (i_old_duration / 1000));
1995 i_old_bitrate = i_bitrate;
1996 i_old_duration = id->i_next_gop_duration;
1997 if ( i_bitrate > p_stream->p_sys->i_vbitrate )
1999 tr->fact_x = (double)i_bitrate / p_stream->p_sys->i_vbitrate;
2005 msg_Dbg(p_stream, "new fact_x = %f", tr->fact_x);
2007 id->p_current_buffer = id->p_next_gop;
2008 id->p_next_gop = NULL;
2009 id->i_next_gop_duration = 0;
2010 id->i_next_gop_size = 0;
2016 /* Store the buffer for the next GOP. */
2017 sout_BufferChain( &id->p_next_gop, in );
2018 id->i_next_gop_duration += in->i_length;
2019 id->i_next_gop_size += in->i_size;
2021 if ( id->p_current_buffer != NULL )
2023 sout_buffer_t * p_next = id->p_current_buffer->p_next;
2024 if ( tr->fact_x == 1.0 )
2026 bs->i_byte_out += id->p_current_buffer->i_size;
2027 id->p_current_buffer->p_next = NULL;
2028 sout_BufferChain( out, id->p_current_buffer );
2032 process_frame( p_stream, id, id->p_current_buffer, out );
2033 sout_BufferDelete(p_stream->p_sout, id->p_current_buffer);
2035 id->p_current_buffer = p_next;