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 *****************************************************************************/
42 #include <vlc_common.h>
44 #include <vlc_codec.h>
46 #include "transrate.h"
48 /****************************************************************************
49 * transrater, code from M2VRequantizer http://www.metakine.com/
50 ****************************************************************************/
61 /////---- begin ext mpeg code
67 static const int non_linear_quantizer_scale [] =
69 0, 1, 2, 3, 4, 5, 6, 7,
70 8, 10, 12, 14, 16, 18, 20, 22,
71 24, 28, 32, 36, 40, 44, 48, 52,
72 56, 64, 72, 80, 88, 96, 104, 112
75 static inline int get_macroblock_modes( transrate_t *tr )
77 bs_transrate_t *bs = &tr->bs;
82 switch( tr->picture_coding_type )
86 tab = MB_I + UBITS (bs->i_bit_in_cache, 1);
87 bs_flush( bs, tab->len );
88 macroblock_modes = tab->modes;
90 if ((!(tr->frame_pred_frame_dct)) && (tr->picture_structure == FRAME_PICTURE))
92 macroblock_modes |= UBITS (bs->i_bit_in_cache, 1) * DCT_TYPE_INTERLACED;
96 return macroblock_modes;
100 tab = MB_P + UBITS (bs->i_bit_in_cache, 5);
101 bs_flush( bs, tab->len );
102 macroblock_modes = tab->modes;
104 if (tr->picture_structure != FRAME_PICTURE)
106 if (macroblock_modes & MACROBLOCK_MOTION_FORWARD)
108 macroblock_modes |= UBITS (bs->i_bit_in_cache, 2) * MOTION_TYPE_BASE;
111 return macroblock_modes;
113 else if (tr->frame_pred_frame_dct)
115 if (macroblock_modes & MACROBLOCK_MOTION_FORWARD)
116 macroblock_modes |= MC_FRAME;
117 return macroblock_modes;
121 if (macroblock_modes & MACROBLOCK_MOTION_FORWARD)
123 macroblock_modes |= UBITS (bs->i_bit_in_cache, 2) * MOTION_TYPE_BASE;
126 if (macroblock_modes & (MACROBLOCK_INTRA | MACROBLOCK_PATTERN))
128 macroblock_modes |= UBITS (bs->i_bit_in_cache, 1) * DCT_TYPE_INTERLACED;
131 return macroblock_modes;
136 tab = MB_B + UBITS (bs->i_bit_in_cache, 6);
137 bs_flush( bs, tab->len );
138 macroblock_modes = tab->modes;
140 if( tr->picture_structure != FRAME_PICTURE)
142 if (! (macroblock_modes & MACROBLOCK_INTRA))
144 macroblock_modes |= UBITS (bs->i_bit_in_cache, 2) * MOTION_TYPE_BASE;
147 return macroblock_modes;
149 else if (tr->frame_pred_frame_dct)
151 /* if (! (macroblock_modes & MACROBLOCK_INTRA)) */
152 macroblock_modes |= MC_FRAME;
153 return macroblock_modes;
157 if (macroblock_modes & MACROBLOCK_INTRA) goto intra;
158 macroblock_modes |= UBITS (bs->i_bit_in_cache, 2) * MOTION_TYPE_BASE;
160 if (macroblock_modes & (MACROBLOCK_INTRA | MACROBLOCK_PATTERN))
163 macroblock_modes |= UBITS (bs->i_bit_in_cache, 1) * DCT_TYPE_INTERLACED;
166 return macroblock_modes;
175 static inline int get_quantizer_scale( transrate_t *tr )
177 bs_transrate_t *bs = &tr->bs;
179 int quantizer_scale_code;
181 quantizer_scale_code = UBITS (bs->i_bit_in_cache, 5);
184 if( tr->q_scale_type )
185 return non_linear_quantizer_scale[quantizer_scale_code];
187 return quantizer_scale_code << 1;
190 static inline int get_motion_delta( bs_transrate_t *bs, const int f_code )
196 if (bs->i_bit_in_cache & 0x80000000)
201 else if (bs->i_bit_in_cache >= 0x0c000000)
204 tab = MV_4 + UBITS (bs->i_bit_in_cache, 4);
205 delta = (tab->delta << f_code) + 1;
206 bs_copy( bs, tab->len);
208 sign = SBITS (bs->i_bit_in_cache, 1);
213 delta += UBITS (bs->i_bit_in_cache, f_code);
214 bs_copy( bs, f_code);
217 return (delta ^ sign) - sign;
222 tab = MV_10 + UBITS (bs->i_bit_in_cache, 10);
223 delta = (tab->delta << f_code) + 1;
224 bs_copy( bs, tab->len);
226 sign = SBITS (bs->i_bit_in_cache, 1);
231 delta += UBITS (bs->i_bit_in_cache, f_code);
232 bs_copy( bs, f_code);
235 return (delta ^ sign) - sign;
240 static inline int get_dmv( bs_transrate_t *bs )
244 tab = DMV_2 + UBITS (bs->i_bit_in_cache, 2);
245 bs_copy( bs, tab->len);
249 static inline int get_coded_block_pattern( bs_transrate_t *bs )
253 if (bs->i_bit_in_cache >= 0x20000000)
255 tab = CBP_7 + (UBITS (bs->i_bit_in_cache, 7) - 16);
256 bs_flush( bs, tab->len );
261 tab = CBP_9 + UBITS (bs->i_bit_in_cache, 9);
262 bs_flush( bs, tab->len );
267 static inline int get_luma_dc_dct_diff( bs_transrate_t *bs, uint32_t *bits, uint8_t *len )
273 if (bs->i_bit_in_cache < 0xf8000000)
275 tab = DC_lum_5 + UBITS (bs->i_bit_in_cache, 5);
279 *bits = bs_read( bs, tab->len );
281 //dc_diff = UBITS (bs->i_bit_in_cache, size) - UBITS (SBITS (~bs->i_bit_in_cache, 1), size);
282 dc_diff = UBITS (bs->i_bit_in_cache, size);
283 if (!(dc_diff >> (size - 1))) dc_diff = (dc_diff + 1) - (1 << size);
285 *bits |= bs_read( bs, size );
291 *bits = bs_read( bs, 3 );
298 tab = DC_long + (UBITS (bs->i_bit_in_cache, 9) - 0x1e0);
300 *bits = bs_read( bs, tab->len );
302 //dc_diff = UBITS (bs->i_bit_in_cache, size) - UBITS (SBITS (~bs->i_bit_in_cache, 1), size);
303 dc_diff = UBITS (bs->i_bit_in_cache, size);
304 if (!(dc_diff >> (size - 1))) dc_diff = (dc_diff + 1) - (1 << size);
306 *bits |= bs_read( bs, size );
312 static inline int get_chroma_dc_dct_diff( bs_transrate_t *bs, uint32_t *bits, uint8_t *len )
318 if (bs->i_bit_in_cache < 0xf8000000)
320 tab = DC_chrom_5 + UBITS (bs->i_bit_in_cache, 5);
324 *bits = bs_read( bs, tab->len );
326 //dc_diff = UBITS (bs->i_bit_in_cache, size) - UBITS (SBITS (~bs->i_bit_in_cache, 1), size);
327 dc_diff = UBITS (bs->i_bit_in_cache, size);
328 if (!(dc_diff >> (size - 1))) dc_diff = (dc_diff + 1) - (1 << size);
330 *bits |= bs_read( bs, size );
336 *bits = bs_read( bs, 2 );
343 tab = DC_long + (UBITS (bs->i_bit_in_cache, 10) - 0x3e0);
345 *bits = bs_read( bs, tab->len + 1 );
347 //dc_diff = UBITS (bs->i_bit_in_cache, size) - UBITS (SBITS (~bs->i_bit_in_cache, 1), size);
348 dc_diff = UBITS (bs->i_bit_in_cache, size);
349 if (!(dc_diff >> (size - 1))) dc_diff = (dc_diff + 1) - (1 << size);
351 *bits |= bs_read( bs, size );
357 static void motion_fr_frame( bs_transrate_t *bs, unsigned int f_code[2] )
359 get_motion_delta( bs, f_code[0] );
360 get_motion_delta( bs, f_code[1] );
363 static void motion_fr_field( bs_transrate_t *bs, unsigned int f_code[2] )
367 get_motion_delta( bs, f_code[0]);
368 get_motion_delta( bs, f_code[1]);
372 get_motion_delta( bs, f_code[0]);
373 get_motion_delta( bs, f_code[1]);
376 static void motion_fr_dmv( bs_transrate_t *bs, unsigned int f_code[2] )
378 get_motion_delta( bs, f_code[0]);
381 get_motion_delta( bs, f_code[1]);
385 static void motion_fi_field( bs_transrate_t *bs, unsigned int f_code[2] )
389 get_motion_delta( bs, f_code[0]);
390 get_motion_delta( bs, f_code[1]);
393 static void motion_fi_16x8( bs_transrate_t *bs, unsigned int f_code[2] )
397 get_motion_delta( bs, f_code[0]);
398 get_motion_delta( bs, f_code[1]);
402 get_motion_delta( bs, f_code[0]);
403 get_motion_delta( bs, f_code[1]);
406 static void motion_fi_dmv( bs_transrate_t *bs, unsigned int f_code[2] )
408 get_motion_delta( bs, f_code[0]);
411 get_motion_delta( bs, f_code[1]);
416 #define MOTION_CALL(routine,direction) \
418 if ((direction) & MACROBLOCK_MOTION_FORWARD) \
419 routine( bs, tr->f_code[0]); \
420 if ((direction) & MACROBLOCK_MOTION_BACKWARD) \
421 routine( bs, tr->f_code[1]); \
424 #define NEXT_MACROBLOCK \
426 tr->h_offset += 16; \
427 if( tr->h_offset == tr->horizontal_size_value) \
429 tr->v_offset += 16; \
430 if (tr->v_offset > (tr->vertical_size_value - 16)) return; \
435 static void putmbdata( transrate_t *tr, int macroblock_modes )
437 bs_transrate_t *bs = &tr->bs;
440 mbtypetab[tr->picture_coding_type-1][macroblock_modes&0x1F].code,
441 mbtypetab[tr->picture_coding_type-1][macroblock_modes&0x1F].len);
443 switch ( tr->picture_coding_type )
446 if ((! (tr->frame_pred_frame_dct)) && (tr->picture_structure == FRAME_PICTURE))
447 bs_write( bs, macroblock_modes & DCT_TYPE_INTERLACED ? 1 : 0, 1);
451 if (tr->picture_structure != FRAME_PICTURE)
453 if (macroblock_modes & MACROBLOCK_MOTION_FORWARD)
454 bs_write( bs, (macroblock_modes & MOTION_TYPE_MASK) / MOTION_TYPE_BASE, 2);
457 else if (tr->frame_pred_frame_dct) break;
460 if (macroblock_modes & MACROBLOCK_MOTION_FORWARD)
461 bs_write( bs, (macroblock_modes & MOTION_TYPE_MASK) / MOTION_TYPE_BASE, 2);
462 if (macroblock_modes & (MACROBLOCK_INTRA | MACROBLOCK_PATTERN))
463 bs_write( bs, macroblock_modes & DCT_TYPE_INTERLACED ? 1 : 0, 1);
468 if (tr->picture_structure != FRAME_PICTURE)
470 if (! (macroblock_modes & MACROBLOCK_INTRA))
471 bs_write( bs, (macroblock_modes & MOTION_TYPE_MASK) / MOTION_TYPE_BASE, 2);
474 else if (tr->frame_pred_frame_dct) break;
477 if (macroblock_modes & MACROBLOCK_INTRA) goto intra;
478 bs_write( bs, (macroblock_modes & MOTION_TYPE_MASK) / MOTION_TYPE_BASE, 2);
479 if (macroblock_modes & (MACROBLOCK_INTRA | MACROBLOCK_PATTERN))
482 bs_write( bs, macroblock_modes & DCT_TYPE_INTERLACED ? 1 : 0, 1);
489 static const uint8_t map_non_linear_mquant[113] =
491 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,
492 16,17,17,17,18,18,18,18,19,19,19,19,20,20,20,20,21,21,21,21,22,22,
493 22,22,23,23,23,23,24,24,24,24,24,24,24,25,25,25,25,25,25,25,26,26,
494 26,26,26,26,26,26,27,27,27,27,27,27,27,27,28,28,28,28,28,28,28,29,
495 29,29,29,29,29,29,29,29,29,30,30,30,30,30,30,30,31,31,31,31,31
497 static inline void put_quantiser( transrate_t *tr )
499 bs_transrate_t *bs = &tr->bs;
501 bs_write( bs, tr->q_scale_type ? map_non_linear_mquant[tr->new_quantizer_scale] : tr->new_quantizer_scale >> 1, 5 );
502 tr->last_coded_scale = tr->new_quantizer_scale;
505 /* generate variable length code for macroblock_address_increment (6.3.16) */
506 static inline void putaddrinc( transrate_t *tr, int addrinc )
508 bs_transrate_t *bs = &tr->bs;
510 while ( addrinc >= 33 )
512 bs_write( bs, 0x08, 11 ); /* macroblock_escape */
516 bs_write( bs, addrinctab[addrinc].code, addrinctab[addrinc].len );
519 static int slice_init( transrate_t *tr, int code )
521 bs_transrate_t *bs = &tr->bs;
525 tr->v_offset = (code - 1) * 16;
527 tr->quantizer_scale = get_quantizer_scale( tr );
528 if ( tr->new_quantizer_scale < tr->quantizer_scale )
529 tr->new_quantizer_scale = scale_quant( tr, tr->qrate );
531 /*LOGF("************************\nstart of slice %i in %s picture. ori quant: %i new quant: %i\n", code,
532 (picture_coding_type == I_TYPE ? "I_TYPE" : (picture_coding_type == P_TYPE ? "P_TYPE" : "B_TYPE")),
533 quantizer_scale, new_quantizer_scale);*/
535 /* ignore intra_slice and all the extra data */
536 while (bs->i_bit_in_cache & 0x80000000)
541 /* decode initial macroblock address increment */
545 if (bs->i_bit_in_cache >= 0x08000000)
547 mba = MBA_5 + (UBITS (bs->i_bit_in_cache, 6) - 2);
550 else if (bs->i_bit_in_cache >= 0x01800000)
552 mba = MBA_11 + (UBITS (bs->i_bit_in_cache, 12) - 24);
555 else if( UBITS (bs->i_bit_in_cache, 12 ) == 8 )
557 /* macroblock_escape */
567 bs_flush(bs, mba->len + 1);
568 tr->h_offset = (offset + mba->mba) << 4;
570 while( tr->h_offset - (int)tr->horizontal_size_value >= 0)
572 tr->h_offset -= tr->horizontal_size_value;
576 if( tr->v_offset > tr->vertical_size_value - 16 )
580 return (offset + mba->mba);
583 static void mpeg2_slice( transrate_t *tr, const int code )
585 bs_transrate_t *bs = &tr->bs;
587 int first_in_slice = 1;
589 if( (mba_inc = slice_init( tr, code )) < 0 )
597 int macroblock_modes;
601 while (unlikely(bs->i_bit_in < 24)) bs_refill( bs );
603 macroblock_modes = get_macroblock_modes( tr );
604 if (macroblock_modes & MACROBLOCK_QUANT)
605 tr->quantizer_scale = get_quantizer_scale( tr );
606 if (tr->new_quantizer_scale < tr->quantizer_scale)
607 tr->new_quantizer_scale = scale_quant( tr, tr->qrate );
609 //LOGF("blk %i : ", h_offset >> 4);
611 if (macroblock_modes & MACROBLOCK_INTRA)
613 RunLevel block[6][65]; // terminated by level = 0, so we need 64+1
614 RunLevel new_block[6][65]; // terminated by level = 0, so we need 64+1
620 uint8_t p_n_ow[32], *p_n_w,
621 *p_o_ow = bs->p_ow, *p_o_w = bs->p_w;
622 uint32_t i_n_bit_out, i_n_bit_out_cache,
623 i_o_bit_out = bs->i_bit_out, i_o_bit_out_cache = bs->i_bit_out_cache;
625 bs->i_bit_out_cache = 0; bs->i_bit_out = BITS_IN_BUF;
626 bs->p_ow = bs->p_w = p_n_ow;
628 //LOG("intra "); if (macroblock_modes & MACROBLOCK_QUANT) LOGF("got new quant: %i ", quantizer_scale);
630 if (tr->concealment_motion_vectors)
632 if (tr->picture_structure != FRAME_PICTURE)
634 bs_copy(bs, 1); /* remove field_select */
636 /* like motion_frame, but parsing without actual motion compensation */
637 get_motion_delta(bs, tr->f_code[0][0]);
638 get_motion_delta(bs, tr->f_code[0][1]);
640 bs_copy(bs, 1); /* remove marker_bit */
643 assert(bs->p_w - bs->p_ow < 32);
646 i_n_bit_out = bs->i_bit_out;
647 i_n_bit_out_cache = bs->i_bit_out_cache;
648 assert(bs->p_ow == p_n_ow);
650 bs->i_bit_out = i_o_bit_out ;
651 bs->i_bit_out_cache = i_o_bit_out_cache;
656 if( tr->intra_vlc_format )
659 for ( i = 0; i < 4; i++ )
661 get_luma_dc_dct_diff( bs, dc + i, dc_len + i );
662 get_intra_block_B15( tr, block[i] );
663 if (tr->b_error) return;
668 get_chroma_dc_dct_diff( bs, dc + i, dc_len + i );
669 get_intra_block_B15( tr, block[i] );
670 if (tr->b_error) return;
676 for ( i = 0; i < 4; i++ )
678 get_luma_dc_dct_diff( bs, dc + i, dc_len + i );
679 get_intra_block_B14( tr, block[i] );
680 if (tr->b_error) return;
685 get_chroma_dc_dct_diff( bs, dc + i, dc_len + i );
686 get_intra_block_B14( tr, block[i] );
687 if (tr->b_error) return;
691 transrate_mb( tr, block, new_block, 0x3f, 1 );
693 if (tr->last_coded_scale == tr->new_quantizer_scale)
694 macroblock_modes &= ~MACROBLOCK_QUANT;
696 if ( first_in_slice )
699 bs_write( bs, 0, 1 );
700 macroblock_modes &= ~MACROBLOCK_QUANT;
702 putaddrinc( tr, mba_inc );
704 putmbdata( tr, macroblock_modes );
705 if( macroblock_modes & MACROBLOCK_QUANT )
710 // put saved motion data...
711 for (batb = 0; batb < (p_n_w - p_n_ow); batb++)
713 bs_write( bs, p_n_ow[batb], 8 );
715 bs_write( bs, i_n_bit_out_cache, BITS_IN_BUF - i_n_bit_out );
716 // end saved motion data...
718 for ( i = 0; i < 6; i++ )
720 bs_write( bs, *(dc + i), *(dc_len + i) );
721 putintrablk( bs, new_block[i], tr->intra_vlc_format );
727 RunLevel block[6][65]; // terminated by level = 0, so we need 64+1
728 RunLevel new_block[6][65]; // terminated by level = 0, so we need 64+1
729 int new_coded_block_pattern = 0;
734 uint8_t p_n_ow[32], *p_n_w,
735 *p_o_ow = bs->p_ow, *p_o_w = bs->p_w;
736 uint32_t i_n_bit_out, i_n_bit_out_cache,
737 i_o_bit_out = bs->i_bit_out, i_o_bit_out_cache = bs->i_bit_out_cache;
739 bs->i_bit_out_cache = 0; bs->i_bit_out = BITS_IN_BUF;
740 bs->p_ow = bs->p_w = p_n_ow;
742 if (tr->picture_structure == FRAME_PICTURE)
743 switch (macroblock_modes & MOTION_TYPE_MASK)
745 case MC_FRAME: MOTION_CALL (motion_fr_frame, macroblock_modes); break;
746 case MC_FIELD: MOTION_CALL (motion_fr_field, macroblock_modes); break;
747 case MC_DMV: MOTION_CALL (motion_fr_dmv, MACROBLOCK_MOTION_FORWARD); break;
750 switch (macroblock_modes & MOTION_TYPE_MASK)
752 case MC_FIELD: MOTION_CALL (motion_fi_field, macroblock_modes); break;
753 case MC_16X8: MOTION_CALL (motion_fi_16x8, macroblock_modes); break;
754 case MC_DMV: MOTION_CALL (motion_fi_dmv, MACROBLOCK_MOTION_FORWARD); break;
757 //LOG("non intra "); if (macroblock_modes & MACROBLOCK_QUANT) LOGF("got new quant: %i ", quantizer_scale);
759 if (macroblock_modes & MACROBLOCK_PATTERN)
763 cbp = get_coded_block_pattern( bs );
765 for ( i = 0; i < 6; i++ )
767 if ( cbp & (1 << (5 - i)) )
769 get_non_intra_block( tr, block[i] );
770 if (tr->b_error) return;
773 last_in_slice = !UBITS( bs->i_bit_in_cache, 11 );
775 new_coded_block_pattern = transrate_mb( tr, block, new_block,
778 if ( !new_coded_block_pattern &&
780 & (MACROBLOCK_MOTION_FORWARD
781 | MACROBLOCK_MOTION_BACKWARD))
782 && (first_in_slice || last_in_slice) )
784 /* First mb in slice, just code a 0-mv mb.
785 * This is wrong for last in slice, but it only shows
786 * a few artefacts. */
787 macroblock_modes |= MACROBLOCK_MOTION_FORWARD;
788 if (tr->picture_structure == FRAME_PICTURE)
790 macroblock_modes |= MC_FRAME;
791 bs_write( bs, 0x3, 2 ); /* motion vectors */
795 macroblock_modes |= MC_FIELD;
797 (tr->picture_structure == BOTTOM_FIELD ? 1 : 0),
798 1); /* motion field select */
799 bs_write( bs, 0x3, 2 ); /* motion vectors */
803 if ( !new_coded_block_pattern )
805 macroblock_modes &= ~MACROBLOCK_PATTERN;
806 macroblock_modes &= ~MACROBLOCK_QUANT;
810 if ( tr->last_coded_scale == tr->new_quantizer_scale )
812 macroblock_modes &= ~MACROBLOCK_QUANT;
816 macroblock_modes |= MACROBLOCK_QUANT;
821 assert(bs->p_w - bs->p_ow < 32);
824 i_n_bit_out = bs->i_bit_out;
825 i_n_bit_out_cache = bs->i_bit_out_cache;
826 assert(bs->p_ow == p_n_ow);
828 bs->i_bit_out = i_o_bit_out ;
829 bs->i_bit_out_cache = i_o_bit_out_cache;
834 if ( macroblock_modes &
835 (MACROBLOCK_MOTION_FORWARD | MACROBLOCK_MOTION_BACKWARD
836 | MACROBLOCK_PATTERN) )
838 if ( first_in_slice )
841 bs_write( bs, 0, 1 );
842 macroblock_modes &= ~MACROBLOCK_QUANT;
844 putaddrinc( tr, mba_inc );
846 putmbdata( tr, macroblock_modes );
847 if ( macroblock_modes & MACROBLOCK_QUANT )
852 // put saved motion data...
853 for (batb = 0; batb < (p_n_w - p_n_ow); batb++)
855 bs_write( bs, p_n_ow[batb], 8 );
857 bs_write( bs, i_n_bit_out_cache, BITS_IN_BUF - i_n_bit_out);
858 // end saved motion data...
860 if (macroblock_modes & MACROBLOCK_PATTERN)
863 bs_write( bs, cbptable[new_coded_block_pattern].code,
864 cbptable[new_coded_block_pattern].len );
866 for ( i = 0; i < 6; i++ )
868 if ( new_coded_block_pattern & (1 << (5 - i)) )
870 putnonintrablk( bs, new_block[i] );
877 /* skipped macroblock */
882 if (bs->p_c > bs->p_r || bs->p_w > bs->p_rw)
887 //LOGF("\n\to: %i c: %i n: %i\n", quantizer_scale, last_coded_scale, new_quantizer_scale);
895 if ( bs->i_bit_in_cache >= 0x10000000 )
897 mba = MBA_5 + (UBITS (bs->i_bit_in_cache, 5) - 2);
900 else if ( bs->i_bit_in_cache >= 0x03000000 )
902 mba = MBA_11 + (UBITS (bs->i_bit_in_cache, 11) - 24);
905 else if ( UBITS( bs->i_bit_in_cache, 11 ) == 8 )
907 /* macroblock_escape */
918 bs_flush(bs, mba->len);
920 mba_local += mba->mba;
929 static const uint8_t mpeg2_scan_norm[64] ATTR_ALIGN(16) = {
930 /* Zig-Zag scan pattern */
931 0, 1, 8, 16, 9, 2, 3, 10, 17, 24, 32, 25, 18, 11, 4, 5,
932 12, 19, 26, 33, 40, 48, 41, 34, 27, 20, 13, 6, 7, 14, 21, 28,
933 35, 42, 49, 56, 57, 50, 43, 36, 29, 22, 15, 23, 30, 37, 44, 51,
934 58, 59, 52, 45, 38, 31, 39, 46, 53, 60, 61, 54, 47, 55, 62, 63
937 static const uint8_t mpeg2_scan_alt[64] ATTR_ALIGN(16) = {
938 /* Alternate scan pattern */
939 0, 8, 16, 24, 1, 9, 2, 10, 17, 25, 32, 40, 48, 56, 57, 49,
940 41, 33, 26, 18, 3, 11, 4, 12, 19, 27, 34, 42, 50, 58, 35, 43,
941 51, 59, 20, 28, 5, 13, 6, 14, 21, 29, 36, 44, 52, 60, 37, 45,
942 53, 61, 22, 30, 7, 15, 23, 31, 38, 46, 54, 62, 39, 47, 55, 63
945 static const int16_t default_intra_matrix[64] = {
946 8, 16, 19, 22, 26, 27, 29, 34,
947 16, 16, 22, 24, 27, 29, 34, 37,
948 19, 22, 26, 27, 29, 34, 34, 38,
949 22, 22, 26, 27, 29, 34, 37, 40,
950 22, 26, 27, 29, 32, 35, 40, 48,
951 26, 27, 29, 32, 35, 40, 48, 58,
952 26, 27, 29, 34, 38, 46, 56, 69,
953 27, 29, 35, 38, 46, 56, 69, 83
956 static int mpeg2_header_sequence( transrate_t * tr )
958 bs_transrate_t *bs = &tr->bs;
959 int has_intra = 0, has_non_intra = 0;
962 i = (bs->p_c[0] << 16) | (bs->p_c[1] << 8) | bs->p_c[2];
963 tr->horizontal_size_value = i >> 12;
964 tr->vertical_size_value = i & 0xfff;
965 tr->horizontal_size_value = (tr->horizontal_size_value + 15) & ~15;
966 tr->vertical_size_value = (tr->vertical_size_value + 15) & ~15;
967 if ( !tr->horizontal_size_value || !tr->vertical_size_value )
972 if ( tr->mpeg4_matrix )
977 for (i = 0; i < 64; i++)
978 tr->intra_quantizer_matrix[mpeg2_scan_norm[i]] =
979 (bs->p_c[i+7] << 7) | (bs->p_c[i+8] >> 1);
983 for (i = 0; i < 64; i++)
984 tr->intra_quantizer_matrix[mpeg2_scan_norm[i]] =
985 default_intra_matrix[i];
988 if (bs->p_c[7+64] & 1)
991 for (i = 0; i < 64; i++)
992 tr->non_intra_quantizer_matrix[mpeg2_scan_norm[i]] =
997 for (i = 0; i < 64; i++)
998 tr->non_intra_quantizer_matrix[i] = 16;
1002 /* Write quantization matrices */
1003 memcpy( bs->p_w, bs->p_c, 8 );
1006 if ( tr->mpeg4_matrix )
1008 memset( &bs->p_w[8], 0, 128 );
1011 for (i = 0; i < 64; i++)
1013 bs->p_w[i+7] |= mpeg4_default_intra_matrix[mpeg2_scan_norm[i]] >> 7;
1014 bs->p_w[i+8] |= mpeg4_default_intra_matrix[mpeg2_scan_norm[i]] << 1;
1018 for (i = 0; i < 64; i++)
1020 bs->p_w[i+8+64] |= mpeg4_default_intra_matrix[mpeg2_scan_norm[i]];
1023 bs->p_c += (has_intra + has_non_intra) * 64;
1030 tr->scan = mpeg2_scan_norm;
1035 /////---- end ext mpeg code
1037 static int do_next_start_code( transrate_t *tr )
1039 bs_transrate_t *bs = &tr->bs;
1046 *bs->p_w++ = *bs->p_c++;
1048 if (ID == 0x00) // pic header
1050 tr->picture_coding_type = (bs->p_c[1] >> 3) & 0x7;
1051 bs->p_c[1] |= 0x7; bs->p_c[2] = 0xFF; bs->p_c[3] |= 0xF8; // vbv_delay is now 0xFFFF
1053 memcpy(bs->p_w, bs->p_c, 4);
1057 else if (ID == 0xB3) // seq header
1059 mpeg2_header_sequence(tr);
1061 else if (ID == 0xB5) // extension
1063 if ((bs->p_c[0] >> 4) == 0x8) // pic coding ext
1065 tr->f_code[0][0] = (bs->p_c[0] & 0xF) - 1;
1066 tr->f_code[0][1] = (bs->p_c[1] >> 4) - 1;
1067 tr->f_code[1][0] = (bs->p_c[1] & 0xF) - 1;
1068 tr->f_code[1][1] = (bs->p_c[2] >> 4) - 1;
1070 /* tr->intra_dc_precision = (bs->p_c[2] >> 2) & 0x3; */
1071 tr->picture_structure = bs->p_c[2] & 0x3;
1072 tr->frame_pred_frame_dct = (bs->p_c[3] >> 6) & 0x1;
1073 tr->concealment_motion_vectors = (bs->p_c[3] >> 5) & 0x1;
1074 tr->q_scale_type = (bs->p_c[3] >> 4) & 0x1;
1075 tr->intra_vlc_format = (bs->p_c[3] >> 3) & 0x1;
1076 if ( (bs->p_c[3] >> 2) & 0x1 )
1077 tr->scan = mpeg2_scan_alt;
1079 memcpy(bs->p_w, bs->p_c, 5);
1085 *bs->p_w++ = *bs->p_c++;
1088 else if (ID == 0xB8) // gop header
1090 memcpy(bs->p_w, bs->p_c, 4);
1094 else if ((ID >= 0x01) && (ID <= 0xAF)) // slice
1096 uint8_t *outTemp = bs->p_w, *inTemp = bs->p_c;
1098 if( tr->qrate != 1.0 )
1100 if( !tr->horizontal_size_value || !tr->vertical_size_value )
1106 bs->i_bit_in_cache = 0; bs->i_bit_in = 0;
1107 bs->i_bit_out_cache = 0; bs->i_bit_out = BITS_IN_BUF;
1115 // begin bit level recoding
1116 mpeg2_slice(tr, ID);
1117 if (tr->b_error) return -1;
1119 bs_flush_read( bs );
1120 bs_flush_write( bs );
1121 // end bit level recoding
1123 /* Basic sanity checks --Meuuh */
1124 if (bs->p_c > bs->p_r || bs->p_w > bs->p_rw)
1129 /*LOGF("type: %s code: %02i in : %6i out : %6i diff : %6i fact: %2.2f\n",
1130 (picture_coding_type == I_TYPE ? "I_TYPE" : (picture_coding_type == P_TYPE ? "P_TYPE" : "B_TYPE")),
1131 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));*/
1133 if (bs->p_w - outTemp > bs->p_c - inTemp) // yes that might happen, rarely
1136 /*LOGF("*** slice bigger than before !! (type: %s code: %i in : %i out : %i diff : %i)\n",
1137 (picture_coding_type == I_TYPE ? "I_TYPE" : (picture_coding_type == P_TYPE ? "P_TYPE" : "B_TYPE")),
1138 ID, bs->p_c - inTemp, bs->p_w - outTemp, (bs->p_w - outTemp) - (bs->p_c - inTemp));*/
1140 if ( !tr->mpeg4_matrix )
1142 // in this case, we'll just use the original slice !
1143 memcpy(outTemp, inTemp, bs->p_c - inTemp);
1144 bs->p_w = outTemp + (bs->p_c - inTemp);
1146 // adjust bs->i_byte_out
1147 bs->i_byte_out -= (bs->p_w - outTemp) - (bs->p_c - inTemp);
1151 fprintf(stderr, "bad choice for mpeg4-matrix...\n");
1159 int process_frame( sout_stream_t *p_stream, sout_stream_id_t *id,
1160 block_t *in, block_t **out, int i_handicap )
1162 transrate_t *tr = &id->tr;
1163 bs_transrate_t *bs = &tr->bs;
1167 double f_drift, f_fact;
1170 p_out = block_New( p_stream, in->i_buffer * 3 );
1172 p_out->i_length = in->i_length;
1173 p_out->i_dts = in->i_dts;
1174 p_out->i_pts = in->i_pts;
1175 p_out->i_flags = in->i_flags;
1177 bs->p_rw = bs->p_ow = bs->p_w = p_out->p_buffer;
1178 bs->p_c = bs->p_r = in->p_buffer;
1179 bs->p_r += in->i_buffer + 4;
1180 bs->p_rw += in->i_buffer * 2;
1181 *(in->p_buffer + in->i_buffer) = 0;
1182 *(in->p_buffer + in->i_buffer + 1) = 0;
1183 *(in->p_buffer + in->i_buffer + 2) = 1;
1184 *(in->p_buffer + in->i_buffer + 3) = 0;
1186 /* Calculate how late we are */
1187 bs->i_byte_in = in->i_buffer;
1190 i_drift = tr->i_current_output + tr->i_remaining_input
1191 - tr->i_wanted_output;
1192 f_drift = (double)i_drift / tr->i_wanted_output;
1193 f_fact = (double)(tr->i_wanted_output - tr->i_current_output)
1194 / tr->i_remaining_input;
1196 if ( in->i_flags & BLOCK_FLAG_TYPE_I )
1198 /* This is the last picture of the GOP ; only transrate if we're
1200 if ( 0 && f_drift > 0.085 )
1202 tr->i_minimum_error = (f_drift - 0.085) * 50.0 * 50.0;
1203 tr->i_admissible_error = (f_drift - 0.085) * 50.0 * 75.0;
1204 tr->qrate = 1.0 + (f_drift - 0.085) * 50.0;
1205 msg_Warn( p_stream, "transrating I %d/%d",
1206 tr->i_minimum_error, tr->i_admissible_error );
1210 tr->i_minimum_error = 0;
1211 tr->i_admissible_error = 0;
1215 else if ( in->i_flags & BLOCK_FLAG_TYPE_P )
1219 tr->i_minimum_error = (0.8 - f_fact) * 3000.0 + i_handicap;
1220 tr->i_admissible_error = (0.8 - f_fact) * 3500.0 + i_handicap;
1221 tr->qrate = 1.0 + (0.8 - f_fact) * 70.0;
1225 tr->i_minimum_error = 0;
1226 tr->i_admissible_error = 0;
1234 tr->i_minimum_error = (1.2 - f_fact) * 1750.0 + i_handicap;
1235 tr->i_admissible_error = (1.2 - f_fact) * 2250.0 + i_handicap;
1236 tr->qrate = 1.0 + (1.2 - f_fact) * 45.0;
1240 tr->i_minimum_error = 0;
1241 tr->i_admissible_error = 0;
1246 tr->new_quantizer_scale = 0;
1251 uint8_t *p_end = &in->p_buffer[in->i_buffer];
1253 /* Search next start code */
1256 if( bs->p_c < p_end - 3 && bs->p_c[0] == 0 && bs->p_c[1] == 0 && bs->p_c[2] == 1 )
1258 /* Next start code */
1261 else if( bs->p_c < p_end - 6 &&
1262 bs->p_c[0] == 0 && bs->p_c[1] == 0 && bs->p_c[2] == 0 &&
1263 bs->p_c[3] == 0 && bs->p_c[4] == 0 && bs->p_c[5] == 0 )
1265 /* remove stuffing (looking for 6 0x00 bytes) */
1271 *bs->p_w++ = *bs->p_c++;
1274 if( bs->p_c >= p_end )
1280 if( bs->p_c >= p_end )
1285 /* Copy the start code */
1286 memcpy( bs->p_w, bs->p_c, 3 );
1290 if ( do_next_start_code( tr ) )
1293 msg_Err( p_stream, "error in do_next_start_code()" );
1294 block_Release( p_out );
1295 tr->i_remaining_input -= in->i_buffer;
1296 tr->i_current_output += in->i_buffer;
1301 bs->i_byte_out += bs->p_w - bs->p_ow;
1302 p_out->i_buffer = bs->p_w - bs->p_ow;
1305 if ( in->i_flags & BLOCK_FLAG_TYPE_P && f_fact < 0.8 )
1307 double f_ratio = (in->i_buffer - p_out->i_buffer) / in->i_buffer;
1308 if ( f_ratio < (0.8 - f_fact) * 0.1 && i_handicap < 200 )
1310 block_Release( p_out );
1311 return process_frame( p_stream, id, in, out, i_handicap + 50 );
1315 if ( in->i_flags & BLOCK_FLAG_TYPE_B && f_fact < 1.1 )
1317 double f_ratio = (double)(in->i_buffer - p_out->i_buffer)
1319 if ( f_ratio < (1.1 - f_fact) * 0.1 && i_handicap < 400 )
1321 #ifdef DEBUG_TRANSRATER
1322 msg_Dbg( p_stream, "%d: %d -> %d big (f: %f d: %f)",
1323 tr->picture_coding_type, in->i_buffer, p_out->i_buffer,
1326 block_Release( p_out );
1327 return process_frame( p_stream, id, in, out, i_handicap + 100 );
1335 for ( toto = 0; toto < p_out->i_buffer; toto++ )
1336 if (in->p_buffer[toto] != p_out->p_buffer[toto])
1337 msg_Dbg(p_stream, "toto %d %x %x", toto, in->p_buffer[toto], p_out->p_buffer[toto]);
1341 block_ChainAppend( out, p_out );
1342 tr->i_remaining_input -= in->i_buffer;
1343 tr->i_current_output += p_out->i_buffer;
1345 #ifdef DEBUG_TRANSRATER
1346 msg_Dbg( p_stream, "%d: %d -> %d (%d/%d)",
1347 tr->picture_coding_type, in->i_buffer, p_out->i_buffer,
1348 tr->i_minimum_error, tr->i_admissible_error );