1 /*****************************************************************************
2 * frame.c: MPEG2 video transrating module
3 *****************************************************************************
4 * Copyright (C) 2003-2004 the VideoLAN team
5 * Copyright (C) 2003 Antoine Missout <antoine.missout@metakine.com>
6 * Copyright (C) 2000-2003 Michel Lespinasse <walken@zoy.org>
7 * Copyright (C) 1999-2000 Aaron Holtzman <aholtzma@ess.engr.uvic.ca>
10 * Authors: Christophe Massiot <massiot@via.ecp.fr>
11 * Laurent Aimar <fenrir@via.ecp.fr>
12 * Antoine Missout <antoine.missout@metakine.com>
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., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
29 *****************************************************************************/
31 /*****************************************************************************
33 *****************************************************************************/
40 #include <vlc_codec.h>
42 #include "transrate.h"
44 /****************************************************************************
45 * transrater, code from M2VRequantizer http://www.metakine.com/
46 ****************************************************************************/
57 /////---- begin ext mpeg code
63 static const int non_linear_quantizer_scale [] =
65 0, 1, 2, 3, 4, 5, 6, 7,
66 8, 10, 12, 14, 16, 18, 20, 22,
67 24, 28, 32, 36, 40, 44, 48, 52,
68 56, 64, 72, 80, 88, 96, 104, 112
71 static inline int get_macroblock_modes( transrate_t *tr )
73 bs_transrate_t *bs = &tr->bs;
78 switch( tr->picture_coding_type )
82 tab = MB_I + UBITS (bs->i_bit_in_cache, 1);
83 bs_flush( bs, tab->len );
84 macroblock_modes = tab->modes;
86 if ((!(tr->frame_pred_frame_dct)) && (tr->picture_structure == FRAME_PICTURE))
88 macroblock_modes |= UBITS (bs->i_bit_in_cache, 1) * DCT_TYPE_INTERLACED;
92 return macroblock_modes;
96 tab = MB_P + UBITS (bs->i_bit_in_cache, 5);
97 bs_flush( bs, tab->len );
98 macroblock_modes = tab->modes;
100 if (tr->picture_structure != FRAME_PICTURE)
102 if (macroblock_modes & MACROBLOCK_MOTION_FORWARD)
104 macroblock_modes |= UBITS (bs->i_bit_in_cache, 2) * MOTION_TYPE_BASE;
107 return macroblock_modes;
109 else if (tr->frame_pred_frame_dct)
111 if (macroblock_modes & MACROBLOCK_MOTION_FORWARD)
112 macroblock_modes |= MC_FRAME;
113 return macroblock_modes;
117 if (macroblock_modes & MACROBLOCK_MOTION_FORWARD)
119 macroblock_modes |= UBITS (bs->i_bit_in_cache, 2) * MOTION_TYPE_BASE;
122 if (macroblock_modes & (MACROBLOCK_INTRA | MACROBLOCK_PATTERN))
124 macroblock_modes |= UBITS (bs->i_bit_in_cache, 1) * DCT_TYPE_INTERLACED;
127 return macroblock_modes;
132 tab = MB_B + UBITS (bs->i_bit_in_cache, 6);
133 bs_flush( bs, tab->len );
134 macroblock_modes = tab->modes;
136 if( tr->picture_structure != FRAME_PICTURE)
138 if (! (macroblock_modes & MACROBLOCK_INTRA))
140 macroblock_modes |= UBITS (bs->i_bit_in_cache, 2) * MOTION_TYPE_BASE;
143 return macroblock_modes;
145 else if (tr->frame_pred_frame_dct)
147 /* if (! (macroblock_modes & MACROBLOCK_INTRA)) */
148 macroblock_modes |= MC_FRAME;
149 return macroblock_modes;
153 if (macroblock_modes & MACROBLOCK_INTRA) goto intra;
154 macroblock_modes |= UBITS (bs->i_bit_in_cache, 2) * MOTION_TYPE_BASE;
156 if (macroblock_modes & (MACROBLOCK_INTRA | MACROBLOCK_PATTERN))
159 macroblock_modes |= UBITS (bs->i_bit_in_cache, 1) * DCT_TYPE_INTERLACED;
162 return macroblock_modes;
171 static inline int get_quantizer_scale( transrate_t *tr )
173 bs_transrate_t *bs = &tr->bs;
175 int quantizer_scale_code;
177 quantizer_scale_code = UBITS (bs->i_bit_in_cache, 5);
180 if( tr->q_scale_type )
181 return non_linear_quantizer_scale[quantizer_scale_code];
183 return quantizer_scale_code << 1;
186 static inline int get_motion_delta( bs_transrate_t *bs, const int f_code )
192 if (bs->i_bit_in_cache & 0x80000000)
197 else if (bs->i_bit_in_cache >= 0x0c000000)
200 tab = MV_4 + UBITS (bs->i_bit_in_cache, 4);
201 delta = (tab->delta << f_code) + 1;
202 bs_copy( bs, tab->len);
204 sign = SBITS (bs->i_bit_in_cache, 1);
209 delta += UBITS (bs->i_bit_in_cache, f_code);
210 bs_copy( bs, f_code);
213 return (delta ^ sign) - sign;
218 tab = MV_10 + UBITS (bs->i_bit_in_cache, 10);
219 delta = (tab->delta << f_code) + 1;
220 bs_copy( bs, tab->len);
222 sign = SBITS (bs->i_bit_in_cache, 1);
227 delta += UBITS (bs->i_bit_in_cache, f_code);
228 bs_copy( bs, f_code);
231 return (delta ^ sign) - sign;
236 static inline int get_dmv( bs_transrate_t *bs )
240 tab = DMV_2 + UBITS (bs->i_bit_in_cache, 2);
241 bs_copy( bs, tab->len);
245 static inline int get_coded_block_pattern( bs_transrate_t *bs )
249 if (bs->i_bit_in_cache >= 0x20000000)
251 tab = CBP_7 + (UBITS (bs->i_bit_in_cache, 7) - 16);
252 bs_flush( bs, tab->len );
257 tab = CBP_9 + UBITS (bs->i_bit_in_cache, 9);
258 bs_flush( bs, tab->len );
263 static inline int get_luma_dc_dct_diff( bs_transrate_t *bs, uint32_t *bits, uint8_t *len )
269 if (bs->i_bit_in_cache < 0xf8000000)
271 tab = DC_lum_5 + UBITS (bs->i_bit_in_cache, 5);
275 *bits = bs_read( bs, tab->len );
277 //dc_diff = UBITS (bs->i_bit_in_cache, size) - UBITS (SBITS (~bs->i_bit_in_cache, 1), size);
278 dc_diff = UBITS (bs->i_bit_in_cache, size);
279 if (!(dc_diff >> (size - 1))) dc_diff = (dc_diff + 1) - (1 << size);
281 *bits |= bs_read( bs, size );
287 *bits = bs_read( bs, 3 );
294 tab = DC_long + (UBITS (bs->i_bit_in_cache, 9) - 0x1e0);
296 *bits = bs_read( bs, tab->len );
298 //dc_diff = UBITS (bs->i_bit_in_cache, size) - UBITS (SBITS (~bs->i_bit_in_cache, 1), size);
299 dc_diff = UBITS (bs->i_bit_in_cache, size);
300 if (!(dc_diff >> (size - 1))) dc_diff = (dc_diff + 1) - (1 << size);
302 *bits |= bs_read( bs, size );
308 static inline int get_chroma_dc_dct_diff( bs_transrate_t *bs, uint32_t *bits, uint8_t *len )
314 if (bs->i_bit_in_cache < 0xf8000000)
316 tab = DC_chrom_5 + UBITS (bs->i_bit_in_cache, 5);
320 *bits = bs_read( bs, tab->len );
322 //dc_diff = UBITS (bs->i_bit_in_cache, size) - UBITS (SBITS (~bs->i_bit_in_cache, 1), size);
323 dc_diff = UBITS (bs->i_bit_in_cache, size);
324 if (!(dc_diff >> (size - 1))) dc_diff = (dc_diff + 1) - (1 << size);
326 *bits |= bs_read( bs, size );
332 *bits = bs_read( bs, 2 );
339 tab = DC_long + (UBITS (bs->i_bit_in_cache, 10) - 0x3e0);
341 *bits = bs_read( bs, tab->len + 1 );
343 //dc_diff = UBITS (bs->i_bit_in_cache, size) - UBITS (SBITS (~bs->i_bit_in_cache, 1), size);
344 dc_diff = UBITS (bs->i_bit_in_cache, size);
345 if (!(dc_diff >> (size - 1))) dc_diff = (dc_diff + 1) - (1 << size);
347 *bits |= bs_read( bs, size );
353 static void motion_fr_frame( bs_transrate_t *bs, unsigned int f_code[2] )
355 get_motion_delta( bs, f_code[0] );
356 get_motion_delta( bs, f_code[1] );
359 static void motion_fr_field( bs_transrate_t *bs, unsigned int f_code[2] )
363 get_motion_delta( bs, f_code[0]);
364 get_motion_delta( bs, f_code[1]);
368 get_motion_delta( bs, f_code[0]);
369 get_motion_delta( bs, f_code[1]);
372 static void motion_fr_dmv( bs_transrate_t *bs, unsigned int f_code[2] )
374 get_motion_delta( bs, f_code[0]);
377 get_motion_delta( bs, f_code[1]);
381 static void motion_fi_field( bs_transrate_t *bs, unsigned int f_code[2] )
385 get_motion_delta( bs, f_code[0]);
386 get_motion_delta( bs, f_code[1]);
389 static void motion_fi_16x8( bs_transrate_t *bs, unsigned int f_code[2] )
393 get_motion_delta( bs, f_code[0]);
394 get_motion_delta( bs, f_code[1]);
398 get_motion_delta( bs, f_code[0]);
399 get_motion_delta( bs, f_code[1]);
402 static void motion_fi_dmv( bs_transrate_t *bs, unsigned int f_code[2] )
404 get_motion_delta( bs, f_code[0]);
407 get_motion_delta( bs, f_code[1]);
412 #define MOTION_CALL(routine,direction) \
414 if ((direction) & MACROBLOCK_MOTION_FORWARD) \
415 routine( bs, tr->f_code[0]); \
416 if ((direction) & MACROBLOCK_MOTION_BACKWARD) \
417 routine( bs, tr->f_code[1]); \
420 #define NEXT_MACROBLOCK \
422 tr->h_offset += 16; \
423 if( tr->h_offset == tr->horizontal_size_value) \
425 tr->v_offset += 16; \
426 if (tr->v_offset > (tr->vertical_size_value - 16)) return; \
431 static void putmbdata( transrate_t *tr, int macroblock_modes )
433 bs_transrate_t *bs = &tr->bs;
436 mbtypetab[tr->picture_coding_type-1][macroblock_modes&0x1F].code,
437 mbtypetab[tr->picture_coding_type-1][macroblock_modes&0x1F].len);
439 switch ( tr->picture_coding_type )
442 if ((! (tr->frame_pred_frame_dct)) && (tr->picture_structure == FRAME_PICTURE))
443 bs_write( bs, macroblock_modes & DCT_TYPE_INTERLACED ? 1 : 0, 1);
447 if (tr->picture_structure != FRAME_PICTURE)
449 if (macroblock_modes & MACROBLOCK_MOTION_FORWARD)
450 bs_write( bs, (macroblock_modes & MOTION_TYPE_MASK) / MOTION_TYPE_BASE, 2);
453 else if (tr->frame_pred_frame_dct) break;
456 if (macroblock_modes & MACROBLOCK_MOTION_FORWARD)
457 bs_write( bs, (macroblock_modes & MOTION_TYPE_MASK) / MOTION_TYPE_BASE, 2);
458 if (macroblock_modes & (MACROBLOCK_INTRA | MACROBLOCK_PATTERN))
459 bs_write( bs, macroblock_modes & DCT_TYPE_INTERLACED ? 1 : 0, 1);
464 if (tr->picture_structure != FRAME_PICTURE)
466 if (! (macroblock_modes & MACROBLOCK_INTRA))
467 bs_write( bs, (macroblock_modes & MOTION_TYPE_MASK) / MOTION_TYPE_BASE, 2);
470 else if (tr->frame_pred_frame_dct) break;
473 if (macroblock_modes & MACROBLOCK_INTRA) goto intra;
474 bs_write( bs, (macroblock_modes & MOTION_TYPE_MASK) / MOTION_TYPE_BASE, 2);
475 if (macroblock_modes & (MACROBLOCK_INTRA | MACROBLOCK_PATTERN))
478 bs_write( bs, macroblock_modes & DCT_TYPE_INTERLACED ? 1 : 0, 1);
485 static const uint8_t map_non_linear_mquant[113] =
487 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,
488 16,17,17,17,18,18,18,18,19,19,19,19,20,20,20,20,21,21,21,21,22,22,
489 22,22,23,23,23,23,24,24,24,24,24,24,24,25,25,25,25,25,25,25,26,26,
490 26,26,26,26,26,26,27,27,27,27,27,27,27,27,28,28,28,28,28,28,28,29,
491 29,29,29,29,29,29,29,29,29,30,30,30,30,30,30,30,31,31,31,31,31
493 static inline void put_quantiser( transrate_t *tr )
495 bs_transrate_t *bs = &tr->bs;
497 bs_write( bs, tr->q_scale_type ? map_non_linear_mquant[tr->new_quantizer_scale] : tr->new_quantizer_scale >> 1, 5 );
498 tr->last_coded_scale = tr->new_quantizer_scale;
501 /* generate variable length code for macroblock_address_increment (6.3.16) */
502 static inline void putaddrinc( transrate_t *tr, int addrinc )
504 bs_transrate_t *bs = &tr->bs;
506 while ( addrinc >= 33 )
508 bs_write( bs, 0x08, 11 ); /* macroblock_escape */
512 bs_write( bs, addrinctab[addrinc].code, addrinctab[addrinc].len );
515 static int slice_init( transrate_t *tr, int code )
517 bs_transrate_t *bs = &tr->bs;
521 tr->v_offset = (code - 1) * 16;
523 tr->quantizer_scale = get_quantizer_scale( tr );
524 if ( tr->new_quantizer_scale < tr->quantizer_scale )
525 tr->new_quantizer_scale = scale_quant( tr, tr->qrate );
527 /*LOGF("************************\nstart of slice %i in %s picture. ori quant: %i new quant: %i\n", code,
528 (picture_coding_type == I_TYPE ? "I_TYPE" : (picture_coding_type == P_TYPE ? "P_TYPE" : "B_TYPE")),
529 quantizer_scale, new_quantizer_scale);*/
531 /* ignore intra_slice and all the extra data */
532 while (bs->i_bit_in_cache & 0x80000000)
537 /* decode initial macroblock address increment */
541 if (bs->i_bit_in_cache >= 0x08000000)
543 mba = MBA_5 + (UBITS (bs->i_bit_in_cache, 6) - 2);
546 else if (bs->i_bit_in_cache >= 0x01800000)
548 mba = MBA_11 + (UBITS (bs->i_bit_in_cache, 12) - 24);
551 else if( UBITS (bs->i_bit_in_cache, 12 ) == 8 )
553 /* macroblock_escape */
563 bs_flush(bs, mba->len + 1);
564 tr->h_offset = (offset + mba->mba) << 4;
566 while( tr->h_offset - (int)tr->horizontal_size_value >= 0)
568 tr->h_offset -= tr->horizontal_size_value;
572 if( tr->v_offset > tr->vertical_size_value - 16 )
576 return (offset + mba->mba);
579 static void mpeg2_slice( transrate_t *tr, const int code )
581 bs_transrate_t *bs = &tr->bs;
583 int first_in_slice = 1;
585 if( (mba_inc = slice_init( tr, code )) < 0 )
593 int macroblock_modes;
597 while (unlikely(bs->i_bit_in < 24)) bs_refill( bs );
599 macroblock_modes = get_macroblock_modes( tr );
600 if (macroblock_modes & MACROBLOCK_QUANT)
601 tr->quantizer_scale = get_quantizer_scale( tr );
602 if (tr->new_quantizer_scale < tr->quantizer_scale)
603 tr->new_quantizer_scale = scale_quant( tr, tr->qrate );
605 //LOGF("blk %i : ", h_offset >> 4);
607 if (macroblock_modes & MACROBLOCK_INTRA)
609 RunLevel block[6][65]; // terminated by level = 0, so we need 64+1
610 RunLevel new_block[6][65]; // terminated by level = 0, so we need 64+1
616 uint8_t p_n_ow[32], *p_n_w,
617 *p_o_ow = bs->p_ow, *p_o_w = bs->p_w;
618 uint32_t i_n_bit_out, i_n_bit_out_cache,
619 i_o_bit_out = bs->i_bit_out, i_o_bit_out_cache = bs->i_bit_out_cache;
621 bs->i_bit_out_cache = 0; bs->i_bit_out = BITS_IN_BUF;
622 bs->p_ow = bs->p_w = p_n_ow;
624 //LOG("intra "); if (macroblock_modes & MACROBLOCK_QUANT) LOGF("got new quant: %i ", quantizer_scale);
626 if (tr->concealment_motion_vectors)
628 if (tr->picture_structure != FRAME_PICTURE)
630 bs_copy(bs, 1); /* remove field_select */
632 /* like motion_frame, but parsing without actual motion compensation */
633 get_motion_delta(bs, tr->f_code[0][0]);
634 get_motion_delta(bs, tr->f_code[0][1]);
636 bs_copy(bs, 1); /* remove marker_bit */
639 assert(bs->p_w - bs->p_ow < 32);
642 i_n_bit_out = bs->i_bit_out;
643 i_n_bit_out_cache = bs->i_bit_out_cache;
644 assert(bs->p_ow == p_n_ow);
646 bs->i_bit_out = i_o_bit_out ;
647 bs->i_bit_out_cache = i_o_bit_out_cache;
652 if( tr->intra_vlc_format )
655 for ( i = 0; i < 4; i++ )
657 get_luma_dc_dct_diff( bs, dc + i, dc_len + i );
658 get_intra_block_B15( tr, block[i] );
659 if (tr->b_error) return;
664 get_chroma_dc_dct_diff( bs, dc + i, dc_len + i );
665 get_intra_block_B15( tr, block[i] );
666 if (tr->b_error) return;
672 for ( i = 0; i < 4; i++ )
674 get_luma_dc_dct_diff( bs, dc + i, dc_len + i );
675 get_intra_block_B14( tr, block[i] );
676 if (tr->b_error) return;
681 get_chroma_dc_dct_diff( bs, dc + i, dc_len + i );
682 get_intra_block_B14( tr, block[i] );
683 if (tr->b_error) return;
687 transrate_mb( tr, block, new_block, 0x3f, 1 );
689 if (tr->last_coded_scale == tr->new_quantizer_scale)
690 macroblock_modes &= ~MACROBLOCK_QUANT;
692 if ( first_in_slice )
695 bs_write( bs, 0, 1 );
696 macroblock_modes &= ~MACROBLOCK_QUANT;
698 putaddrinc( tr, mba_inc );
700 putmbdata( tr, macroblock_modes );
701 if( macroblock_modes & MACROBLOCK_QUANT )
706 // put saved motion data...
707 for (batb = 0; batb < (p_n_w - p_n_ow); batb++)
709 bs_write( bs, p_n_ow[batb], 8 );
711 bs_write( bs, i_n_bit_out_cache, BITS_IN_BUF - i_n_bit_out );
712 // end saved motion data...
714 for ( i = 0; i < 6; i++ )
716 bs_write( bs, *(dc + i), *(dc_len + i) );
717 putintrablk( bs, new_block[i], tr->intra_vlc_format );
723 RunLevel block[6][65]; // terminated by level = 0, so we need 64+1
724 RunLevel new_block[6][65]; // terminated by level = 0, so we need 64+1
725 int new_coded_block_pattern = 0;
730 uint8_t p_n_ow[32], *p_n_w,
731 *p_o_ow = bs->p_ow, *p_o_w = bs->p_w;
732 uint32_t i_n_bit_out, i_n_bit_out_cache,
733 i_o_bit_out = bs->i_bit_out, i_o_bit_out_cache = bs->i_bit_out_cache;
735 bs->i_bit_out_cache = 0; bs->i_bit_out = BITS_IN_BUF;
736 bs->p_ow = bs->p_w = p_n_ow;
738 if (tr->picture_structure == FRAME_PICTURE)
739 switch (macroblock_modes & MOTION_TYPE_MASK)
741 case MC_FRAME: MOTION_CALL (motion_fr_frame, macroblock_modes); break;
742 case MC_FIELD: MOTION_CALL (motion_fr_field, macroblock_modes); break;
743 case MC_DMV: MOTION_CALL (motion_fr_dmv, MACROBLOCK_MOTION_FORWARD); break;
746 switch (macroblock_modes & MOTION_TYPE_MASK)
748 case MC_FIELD: MOTION_CALL (motion_fi_field, macroblock_modes); break;
749 case MC_16X8: MOTION_CALL (motion_fi_16x8, macroblock_modes); break;
750 case MC_DMV: MOTION_CALL (motion_fi_dmv, MACROBLOCK_MOTION_FORWARD); break;
753 //LOG("non intra "); if (macroblock_modes & MACROBLOCK_QUANT) LOGF("got new quant: %i ", quantizer_scale);
755 if (macroblock_modes & MACROBLOCK_PATTERN)
759 cbp = get_coded_block_pattern( bs );
761 for ( i = 0; i < 6; i++ )
763 if ( cbp & (1 << (5 - i)) )
765 get_non_intra_block( tr, block[i] );
766 if (tr->b_error) return;
769 last_in_slice = !UBITS( bs->i_bit_in_cache, 11 );
771 new_coded_block_pattern = transrate_mb( tr, block, new_block,
774 if ( !new_coded_block_pattern &&
776 & (MACROBLOCK_MOTION_FORWARD
777 | MACROBLOCK_MOTION_BACKWARD))
778 && (first_in_slice || last_in_slice) )
780 /* First mb in slice, just code a 0-mv mb.
781 * This is wrong for last in slice, but it only shows
782 * a few artefacts. */
783 macroblock_modes |= MACROBLOCK_MOTION_FORWARD;
784 if (tr->picture_structure == FRAME_PICTURE)
786 macroblock_modes |= MC_FRAME;
787 bs_write( bs, 0x3, 2 ); /* motion vectors */
791 macroblock_modes |= MC_FIELD;
793 (tr->picture_structure == BOTTOM_FIELD ? 1 : 0),
794 1); /* motion field select */
795 bs_write( bs, 0x3, 2 ); /* motion vectors */
799 if ( !new_coded_block_pattern )
801 macroblock_modes &= ~MACROBLOCK_PATTERN;
802 macroblock_modes &= ~MACROBLOCK_QUANT;
806 if ( tr->last_coded_scale == tr->new_quantizer_scale )
808 macroblock_modes &= ~MACROBLOCK_QUANT;
812 macroblock_modes |= MACROBLOCK_QUANT;
817 assert(bs->p_w - bs->p_ow < 32);
820 i_n_bit_out = bs->i_bit_out;
821 i_n_bit_out_cache = bs->i_bit_out_cache;
822 assert(bs->p_ow == p_n_ow);
824 bs->i_bit_out = i_o_bit_out ;
825 bs->i_bit_out_cache = i_o_bit_out_cache;
830 if ( macroblock_modes &
831 (MACROBLOCK_MOTION_FORWARD | MACROBLOCK_MOTION_BACKWARD
832 | MACROBLOCK_PATTERN) )
834 if ( first_in_slice )
837 bs_write( bs, 0, 1 );
838 macroblock_modes &= ~MACROBLOCK_QUANT;
840 putaddrinc( tr, mba_inc );
842 putmbdata( tr, macroblock_modes );
843 if ( macroblock_modes & MACROBLOCK_QUANT )
848 // put saved motion data...
849 for (batb = 0; batb < (p_n_w - p_n_ow); batb++)
851 bs_write( bs, p_n_ow[batb], 8 );
853 bs_write( bs, i_n_bit_out_cache, BITS_IN_BUF - i_n_bit_out);
854 // end saved motion data...
856 if (macroblock_modes & MACROBLOCK_PATTERN)
859 bs_write( bs, cbptable[new_coded_block_pattern].code,
860 cbptable[new_coded_block_pattern].len );
862 for ( i = 0; i < 6; i++ )
864 if ( new_coded_block_pattern & (1 << (5 - i)) )
866 putnonintrablk( bs, new_block[i] );
873 /* skipped macroblock */
878 if (bs->p_c > bs->p_r || bs->p_w > bs->p_rw)
883 //LOGF("\n\to: %i c: %i n: %i\n", quantizer_scale, last_coded_scale, new_quantizer_scale);
891 if ( bs->i_bit_in_cache >= 0x10000000 )
893 mba = MBA_5 + (UBITS (bs->i_bit_in_cache, 5) - 2);
896 else if ( bs->i_bit_in_cache >= 0x03000000 )
898 mba = MBA_11 + (UBITS (bs->i_bit_in_cache, 11) - 24);
901 else if ( UBITS( bs->i_bit_in_cache, 11 ) == 8 )
903 /* macroblock_escape */
914 bs_flush(bs, mba->len);
916 mba_local += mba->mba;
925 static const uint8_t mpeg2_scan_norm[64] ATTR_ALIGN(16) = {
926 /* Zig-Zag scan pattern */
927 0, 1, 8, 16, 9, 2, 3, 10, 17, 24, 32, 25, 18, 11, 4, 5,
928 12, 19, 26, 33, 40, 48, 41, 34, 27, 20, 13, 6, 7, 14, 21, 28,
929 35, 42, 49, 56, 57, 50, 43, 36, 29, 22, 15, 23, 30, 37, 44, 51,
930 58, 59, 52, 45, 38, 31, 39, 46, 53, 60, 61, 54, 47, 55, 62, 63
933 static const uint8_t mpeg2_scan_alt[64] ATTR_ALIGN(16) = {
934 /* Alternate scan pattern */
935 0, 8, 16, 24, 1, 9, 2, 10, 17, 25, 32, 40, 48, 56, 57, 49,
936 41, 33, 26, 18, 3, 11, 4, 12, 19, 27, 34, 42, 50, 58, 35, 43,
937 51, 59, 20, 28, 5, 13, 6, 14, 21, 29, 36, 44, 52, 60, 37, 45,
938 53, 61, 22, 30, 7, 15, 23, 31, 38, 46, 54, 62, 39, 47, 55, 63
941 static const int16_t default_intra_matrix[64] = {
942 8, 16, 19, 22, 26, 27, 29, 34,
943 16, 16, 22, 24, 27, 29, 34, 37,
944 19, 22, 26, 27, 29, 34, 34, 38,
945 22, 22, 26, 27, 29, 34, 37, 40,
946 22, 26, 27, 29, 32, 35, 40, 48,
947 26, 27, 29, 32, 35, 40, 48, 58,
948 26, 27, 29, 34, 38, 46, 56, 69,
949 27, 29, 35, 38, 46, 56, 69, 83
952 static int mpeg2_header_sequence( transrate_t * tr )
954 bs_transrate_t *bs = &tr->bs;
955 int has_intra = 0, has_non_intra = 0;
958 i = (bs->p_c[0] << 16) | (bs->p_c[1] << 8) | bs->p_c[2];
959 tr->horizontal_size_value = i >> 12;
960 tr->vertical_size_value = i & 0xfff;
961 tr->horizontal_size_value = (tr->horizontal_size_value + 15) & ~15;
962 tr->vertical_size_value = (tr->vertical_size_value + 15) & ~15;
963 if ( !tr->horizontal_size_value || !tr->vertical_size_value )
968 if ( tr->mpeg4_matrix )
973 for (i = 0; i < 64; i++)
974 tr->intra_quantizer_matrix[mpeg2_scan_norm[i]] =
975 (bs->p_c[i+7] << 7) | (bs->p_c[i+8] >> 1);
979 for (i = 0; i < 64; i++)
980 tr->intra_quantizer_matrix[mpeg2_scan_norm[i]] =
981 default_intra_matrix[i];
984 if (bs->p_c[7+64] & 1)
987 for (i = 0; i < 64; i++)
988 tr->non_intra_quantizer_matrix[mpeg2_scan_norm[i]] =
993 for (i = 0; i < 64; i++)
994 tr->non_intra_quantizer_matrix[i] = 16;
998 /* Write quantization matrices */
999 memcpy( bs->p_w, bs->p_c, 8 );
1002 if ( tr->mpeg4_matrix )
1004 memset( &bs->p_w[8], 0, 128 );
1007 for (i = 0; i < 64; i++)
1009 bs->p_w[i+7] |= mpeg4_default_intra_matrix[mpeg2_scan_norm[i]] >> 7;
1010 bs->p_w[i+8] |= mpeg4_default_intra_matrix[mpeg2_scan_norm[i]] << 1;
1014 for (i = 0; i < 64; i++)
1016 bs->p_w[i+8+64] |= mpeg4_default_intra_matrix[mpeg2_scan_norm[i]];
1019 bs->p_c += (has_intra + has_non_intra) * 64;
1026 tr->scan = mpeg2_scan_norm;
1031 /////---- end ext mpeg code
1033 static int do_next_start_code( transrate_t *tr )
1035 bs_transrate_t *bs = &tr->bs;
1042 *bs->p_w++ = *bs->p_c++;
1044 if (ID == 0x00) // pic header
1046 tr->picture_coding_type = (bs->p_c[1] >> 3) & 0x7;
1047 bs->p_c[1] |= 0x7; bs->p_c[2] = 0xFF; bs->p_c[3] |= 0xF8; // vbv_delay is now 0xFFFF
1049 memcpy(bs->p_w, bs->p_c, 4);
1053 else if (ID == 0xB3) // seq header
1055 mpeg2_header_sequence(tr);
1057 else if (ID == 0xB5) // extension
1059 if ((bs->p_c[0] >> 4) == 0x8) // pic coding ext
1061 tr->f_code[0][0] = (bs->p_c[0] & 0xF) - 1;
1062 tr->f_code[0][1] = (bs->p_c[1] >> 4) - 1;
1063 tr->f_code[1][0] = (bs->p_c[1] & 0xF) - 1;
1064 tr->f_code[1][1] = (bs->p_c[2] >> 4) - 1;
1066 /* tr->intra_dc_precision = (bs->p_c[2] >> 2) & 0x3; */
1067 tr->picture_structure = bs->p_c[2] & 0x3;
1068 tr->frame_pred_frame_dct = (bs->p_c[3] >> 6) & 0x1;
1069 tr->concealment_motion_vectors = (bs->p_c[3] >> 5) & 0x1;
1070 tr->q_scale_type = (bs->p_c[3] >> 4) & 0x1;
1071 tr->intra_vlc_format = (bs->p_c[3] >> 3) & 0x1;
1072 if ( (bs->p_c[3] >> 2) & 0x1 )
1073 tr->scan = mpeg2_scan_alt;
1075 memcpy(bs->p_w, bs->p_c, 5);
1081 *bs->p_w++ = *bs->p_c++;
1084 else if (ID == 0xB8) // gop header
1086 memcpy(bs->p_w, bs->p_c, 4);
1090 else if ((ID >= 0x01) && (ID <= 0xAF)) // slice
1092 uint8_t *outTemp = bs->p_w, *inTemp = bs->p_c;
1094 if( tr->qrate != 1.0 )
1096 if( !tr->horizontal_size_value || !tr->vertical_size_value )
1102 bs->i_bit_in_cache = 0; bs->i_bit_in = 0;
1103 bs->i_bit_out_cache = 0; bs->i_bit_out = BITS_IN_BUF;
1111 // begin bit level recoding
1112 mpeg2_slice(tr, ID);
1113 if (tr->b_error) return -1;
1115 bs_flush_read( bs );
1116 bs_flush_write( bs );
1117 // end bit level recoding
1119 /* Basic sanity checks --Meuuh */
1120 if (bs->p_c > bs->p_r || bs->p_w > bs->p_rw)
1125 /*LOGF("type: %s code: %02i in : %6i out : %6i diff : %6i fact: %2.2f\n",
1126 (picture_coding_type == I_TYPE ? "I_TYPE" : (picture_coding_type == P_TYPE ? "P_TYPE" : "B_TYPE")),
1127 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));*/
1129 if (bs->p_w - outTemp > bs->p_c - inTemp) // yes that might happen, rarely
1132 /*LOGF("*** slice bigger than before !! (type: %s code: %i in : %i out : %i diff : %i)\n",
1133 (picture_coding_type == I_TYPE ? "I_TYPE" : (picture_coding_type == P_TYPE ? "P_TYPE" : "B_TYPE")),
1134 ID, bs->p_c - inTemp, bs->p_w - outTemp, (bs->p_w - outTemp) - (bs->p_c - inTemp));*/
1136 if ( !tr->mpeg4_matrix )
1138 // in this case, we'll just use the original slice !
1139 memcpy(outTemp, inTemp, bs->p_c - inTemp);
1140 bs->p_w = outTemp + (bs->p_c - inTemp);
1142 // adjust bs->i_byte_out
1143 bs->i_byte_out -= (bs->p_w - outTemp) - (bs->p_c - inTemp);
1147 fprintf(stderr, "bad choice for mpeg4-matrix...\n");
1155 int process_frame( sout_stream_t *p_stream, sout_stream_id_t *id,
1156 block_t *in, block_t **out, int i_handicap )
1158 transrate_t *tr = &id->tr;
1159 bs_transrate_t *bs = &tr->bs;
1163 double f_drift, f_fact;
1166 p_out = block_New( p_stream, in->i_buffer * 3 );
1168 p_out->i_length = in->i_length;
1169 p_out->i_dts = in->i_dts;
1170 p_out->i_pts = in->i_pts;
1171 p_out->i_flags = in->i_flags;
1173 bs->p_rw = bs->p_ow = bs->p_w = p_out->p_buffer;
1174 bs->p_c = bs->p_r = in->p_buffer;
1175 bs->p_r += in->i_buffer + 4;
1176 bs->p_rw += in->i_buffer * 2;
1177 *(in->p_buffer + in->i_buffer) = 0;
1178 *(in->p_buffer + in->i_buffer + 1) = 0;
1179 *(in->p_buffer + in->i_buffer + 2) = 1;
1180 *(in->p_buffer + in->i_buffer + 3) = 0;
1182 /* Calculate how late we are */
1183 bs->i_byte_in = in->i_buffer;
1186 i_drift = tr->i_current_output + tr->i_remaining_input
1187 - tr->i_wanted_output;
1188 f_drift = (double)i_drift / tr->i_wanted_output;
1189 f_fact = (double)(tr->i_wanted_output - tr->i_current_output)
1190 / tr->i_remaining_input;
1192 if ( in->i_flags & BLOCK_FLAG_TYPE_I )
1194 /* This is the last picture of the GOP ; only transrate if we're
1196 if ( 0 && f_drift > 0.085 )
1198 tr->i_minimum_error = (f_drift - 0.085) * 50.0 * 50.0;
1199 tr->i_admissible_error = (f_drift - 0.085) * 50.0 * 75.0;
1200 tr->qrate = 1.0 + (f_drift - 0.085) * 50.0;
1201 msg_Warn( p_stream, "transrating I %d/%d",
1202 tr->i_minimum_error, tr->i_admissible_error );
1206 tr->i_minimum_error = 0;
1207 tr->i_admissible_error = 0;
1211 else if ( in->i_flags & BLOCK_FLAG_TYPE_P )
1215 tr->i_minimum_error = (0.8 - f_fact) * 3000.0 + i_handicap;
1216 tr->i_admissible_error = (0.8 - f_fact) * 3500.0 + i_handicap;
1217 tr->qrate = 1.0 + (0.8 - f_fact) * 70.0;
1221 tr->i_minimum_error = 0;
1222 tr->i_admissible_error = 0;
1230 tr->i_minimum_error = (1.2 - f_fact) * 1750.0 + i_handicap;
1231 tr->i_admissible_error = (1.2 - f_fact) * 2250.0 + i_handicap;
1232 tr->qrate = 1.0 + (1.2 - f_fact) * 45.0;
1236 tr->i_minimum_error = 0;
1237 tr->i_admissible_error = 0;
1242 tr->new_quantizer_scale = 0;
1247 uint8_t *p_end = &in->p_buffer[in->i_buffer];
1249 /* Search next start code */
1252 if( bs->p_c < p_end - 3 && bs->p_c[0] == 0 && bs->p_c[1] == 0 && bs->p_c[2] == 1 )
1254 /* Next start code */
1257 else if( bs->p_c < p_end - 6 &&
1258 bs->p_c[0] == 0 && bs->p_c[1] == 0 && bs->p_c[2] == 0 &&
1259 bs->p_c[3] == 0 && bs->p_c[4] == 0 && bs->p_c[5] == 0 )
1261 /* remove stuffing (looking for 6 0x00 bytes) */
1267 *bs->p_w++ = *bs->p_c++;
1270 if( bs->p_c >= p_end )
1276 if( bs->p_c >= p_end )
1281 /* Copy the start code */
1282 memcpy( bs->p_w, bs->p_c, 3 );
1286 if ( do_next_start_code( tr ) )
1289 msg_Err( p_stream, "error in do_next_start_code()" );
1290 block_Release( p_out );
1291 tr->i_remaining_input -= in->i_buffer;
1292 tr->i_current_output += in->i_buffer;
1297 bs->i_byte_out += bs->p_w - bs->p_ow;
1298 p_out->i_buffer = bs->p_w - bs->p_ow;
1301 if ( in->i_flags & BLOCK_FLAG_TYPE_P && f_fact < 0.8 )
1303 double f_ratio = (in->i_buffer - p_out->i_buffer) / in->i_buffer;
1304 if ( f_ratio < (0.8 - f_fact) * 0.1 && i_handicap < 200 )
1306 block_Release( p_out );
1307 return process_frame( p_stream, id, in, out, i_handicap + 50 );
1311 if ( in->i_flags & BLOCK_FLAG_TYPE_B && f_fact < 1.1 )
1313 double f_ratio = (double)(in->i_buffer - p_out->i_buffer)
1315 if ( f_ratio < (1.1 - f_fact) * 0.1 && i_handicap < 400 )
1317 #ifdef DEBUG_TRANSRATER
1318 msg_Dbg( p_stream, "%d: %d -> %d big (f: %f d: %f)",
1319 tr->picture_coding_type, in->i_buffer, p_out->i_buffer,
1322 block_Release( p_out );
1323 return process_frame( p_stream, id, in, out, i_handicap + 100 );
1331 for ( toto = 0; toto < p_out->i_buffer; toto++ )
1332 if (in->p_buffer[toto] != p_out->p_buffer[toto])
1333 msg_Dbg(p_stream, "toto %d %x %x", toto, in->p_buffer[toto], p_out->p_buffer[toto]);
1337 block_ChainAppend( out, p_out );
1338 tr->i_remaining_input -= in->i_buffer;
1339 tr->i_current_output += p_out->i_buffer;
1341 #ifdef DEBUG_TRANSRATER
1342 msg_Dbg( p_stream, "%d: %d -> %d (%d/%d)",
1343 tr->picture_coding_type, in->i_buffer, p_out->i_buffer,
1344 tr->i_minimum_error, tr->i_admissible_error );