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_codec.h>
44 #include "transrate.h"
46 /****************************************************************************
47 * transrater, code from M2VRequantizer http://www.metakine.com/
48 ****************************************************************************/
59 /////---- begin ext mpeg code
65 static const int non_linear_quantizer_scale [] =
67 0, 1, 2, 3, 4, 5, 6, 7,
68 8, 10, 12, 14, 16, 18, 20, 22,
69 24, 28, 32, 36, 40, 44, 48, 52,
70 56, 64, 72, 80, 88, 96, 104, 112
73 static inline int get_macroblock_modes( transrate_t *tr )
75 bs_transrate_t *bs = &tr->bs;
80 switch( tr->picture_coding_type )
84 tab = MB_I + UBITS (bs->i_bit_in_cache, 1);
85 bs_flush( bs, tab->len );
86 macroblock_modes = tab->modes;
88 if ((!(tr->frame_pred_frame_dct)) && (tr->picture_structure == FRAME_PICTURE))
90 macroblock_modes |= UBITS (bs->i_bit_in_cache, 1) * DCT_TYPE_INTERLACED;
94 return macroblock_modes;
98 tab = MB_P + UBITS (bs->i_bit_in_cache, 5);
99 bs_flush( bs, tab->len );
100 macroblock_modes = tab->modes;
102 if (tr->picture_structure != FRAME_PICTURE)
104 if (macroblock_modes & MACROBLOCK_MOTION_FORWARD)
106 macroblock_modes |= UBITS (bs->i_bit_in_cache, 2) * MOTION_TYPE_BASE;
109 return macroblock_modes;
111 else if (tr->frame_pred_frame_dct)
113 if (macroblock_modes & MACROBLOCK_MOTION_FORWARD)
114 macroblock_modes |= MC_FRAME;
115 return macroblock_modes;
119 if (macroblock_modes & MACROBLOCK_MOTION_FORWARD)
121 macroblock_modes |= UBITS (bs->i_bit_in_cache, 2) * MOTION_TYPE_BASE;
124 if (macroblock_modes & (MACROBLOCK_INTRA | MACROBLOCK_PATTERN))
126 macroblock_modes |= UBITS (bs->i_bit_in_cache, 1) * DCT_TYPE_INTERLACED;
129 return macroblock_modes;
134 tab = MB_B + UBITS (bs->i_bit_in_cache, 6);
135 bs_flush( bs, tab->len );
136 macroblock_modes = tab->modes;
138 if( tr->picture_structure != FRAME_PICTURE)
140 if (! (macroblock_modes & MACROBLOCK_INTRA))
142 macroblock_modes |= UBITS (bs->i_bit_in_cache, 2) * MOTION_TYPE_BASE;
145 return macroblock_modes;
147 else if (tr->frame_pred_frame_dct)
149 /* if (! (macroblock_modes & MACROBLOCK_INTRA)) */
150 macroblock_modes |= MC_FRAME;
151 return macroblock_modes;
155 if (macroblock_modes & MACROBLOCK_INTRA) goto intra;
156 macroblock_modes |= UBITS (bs->i_bit_in_cache, 2) * MOTION_TYPE_BASE;
158 if (macroblock_modes & (MACROBLOCK_INTRA | MACROBLOCK_PATTERN))
161 macroblock_modes |= UBITS (bs->i_bit_in_cache, 1) * DCT_TYPE_INTERLACED;
164 return macroblock_modes;
173 static inline int get_quantizer_scale( transrate_t *tr )
175 bs_transrate_t *bs = &tr->bs;
177 int quantizer_scale_code;
179 quantizer_scale_code = UBITS (bs->i_bit_in_cache, 5);
182 if( tr->q_scale_type )
183 return non_linear_quantizer_scale[quantizer_scale_code];
185 return quantizer_scale_code << 1;
188 static inline int get_motion_delta( bs_transrate_t *bs, const int f_code )
194 if (bs->i_bit_in_cache & 0x80000000)
199 else if (bs->i_bit_in_cache >= 0x0c000000)
202 tab = MV_4 + UBITS (bs->i_bit_in_cache, 4);
203 delta = (tab->delta << f_code) + 1;
204 bs_copy( bs, tab->len);
206 sign = SBITS (bs->i_bit_in_cache, 1);
211 delta += UBITS (bs->i_bit_in_cache, f_code);
212 bs_copy( bs, f_code);
215 return (delta ^ sign) - sign;
220 tab = MV_10 + UBITS (bs->i_bit_in_cache, 10);
221 delta = (tab->delta << f_code) + 1;
222 bs_copy( bs, tab->len);
224 sign = SBITS (bs->i_bit_in_cache, 1);
229 delta += UBITS (bs->i_bit_in_cache, f_code);
230 bs_copy( bs, f_code);
233 return (delta ^ sign) - sign;
238 static inline int get_dmv( bs_transrate_t *bs )
242 tab = DMV_2 + UBITS (bs->i_bit_in_cache, 2);
243 bs_copy( bs, tab->len);
247 static inline int get_coded_block_pattern( bs_transrate_t *bs )
251 if (bs->i_bit_in_cache >= 0x20000000)
253 tab = CBP_7 + (UBITS (bs->i_bit_in_cache, 7) - 16);
254 bs_flush( bs, tab->len );
259 tab = CBP_9 + UBITS (bs->i_bit_in_cache, 9);
260 bs_flush( bs, tab->len );
265 static inline int get_luma_dc_dct_diff( bs_transrate_t *bs, uint32_t *bits, uint8_t *len )
271 if (bs->i_bit_in_cache < 0xf8000000)
273 tab = DC_lum_5 + UBITS (bs->i_bit_in_cache, 5);
277 *bits = bs_read( bs, tab->len );
279 //dc_diff = UBITS (bs->i_bit_in_cache, size) - UBITS (SBITS (~bs->i_bit_in_cache, 1), size);
280 dc_diff = UBITS (bs->i_bit_in_cache, size);
281 if (!(dc_diff >> (size - 1))) dc_diff = (dc_diff + 1) - (1 << size);
283 *bits |= bs_read( bs, size );
289 *bits = bs_read( bs, 3 );
296 tab = DC_long + (UBITS (bs->i_bit_in_cache, 9) - 0x1e0);
298 *bits = bs_read( bs, tab->len );
300 //dc_diff = UBITS (bs->i_bit_in_cache, size) - UBITS (SBITS (~bs->i_bit_in_cache, 1), size);
301 dc_diff = UBITS (bs->i_bit_in_cache, size);
302 if (!(dc_diff >> (size - 1))) dc_diff = (dc_diff + 1) - (1 << size);
304 *bits |= bs_read( bs, size );
310 static inline int get_chroma_dc_dct_diff( bs_transrate_t *bs, uint32_t *bits, uint8_t *len )
316 if (bs->i_bit_in_cache < 0xf8000000)
318 tab = DC_chrom_5 + UBITS (bs->i_bit_in_cache, 5);
322 *bits = bs_read( bs, tab->len );
324 //dc_diff = UBITS (bs->i_bit_in_cache, size) - UBITS (SBITS (~bs->i_bit_in_cache, 1), size);
325 dc_diff = UBITS (bs->i_bit_in_cache, size);
326 if (!(dc_diff >> (size - 1))) dc_diff = (dc_diff + 1) - (1 << size);
328 *bits |= bs_read( bs, size );
334 *bits = bs_read( bs, 2 );
341 tab = DC_long + (UBITS (bs->i_bit_in_cache, 10) - 0x3e0);
343 *bits = bs_read( bs, tab->len + 1 );
345 //dc_diff = UBITS (bs->i_bit_in_cache, size) - UBITS (SBITS (~bs->i_bit_in_cache, 1), size);
346 dc_diff = UBITS (bs->i_bit_in_cache, size);
347 if (!(dc_diff >> (size - 1))) dc_diff = (dc_diff + 1) - (1 << size);
349 *bits |= bs_read( bs, size );
355 static void motion_fr_frame( bs_transrate_t *bs, unsigned int f_code[2] )
357 get_motion_delta( bs, f_code[0] );
358 get_motion_delta( bs, f_code[1] );
361 static void motion_fr_field( bs_transrate_t *bs, unsigned int f_code[2] )
365 get_motion_delta( bs, f_code[0]);
366 get_motion_delta( bs, f_code[1]);
370 get_motion_delta( bs, f_code[0]);
371 get_motion_delta( bs, f_code[1]);
374 static void motion_fr_dmv( bs_transrate_t *bs, unsigned int f_code[2] )
376 get_motion_delta( bs, f_code[0]);
379 get_motion_delta( bs, f_code[1]);
383 static void motion_fi_field( bs_transrate_t *bs, unsigned int f_code[2] )
387 get_motion_delta( bs, f_code[0]);
388 get_motion_delta( bs, f_code[1]);
391 static void motion_fi_16x8( bs_transrate_t *bs, unsigned int f_code[2] )
395 get_motion_delta( bs, f_code[0]);
396 get_motion_delta( bs, f_code[1]);
400 get_motion_delta( bs, f_code[0]);
401 get_motion_delta( bs, f_code[1]);
404 static void motion_fi_dmv( bs_transrate_t *bs, unsigned int f_code[2] )
406 get_motion_delta( bs, f_code[0]);
409 get_motion_delta( bs, f_code[1]);
414 #define MOTION_CALL(routine,direction) \
416 if ((direction) & MACROBLOCK_MOTION_FORWARD) \
417 routine( bs, tr->f_code[0]); \
418 if ((direction) & MACROBLOCK_MOTION_BACKWARD) \
419 routine( bs, tr->f_code[1]); \
422 #define NEXT_MACROBLOCK \
424 tr->h_offset += 16; \
425 if( tr->h_offset == tr->horizontal_size_value) \
427 tr->v_offset += 16; \
428 if (tr->v_offset > (tr->vertical_size_value - 16)) return; \
433 static void putmbdata( transrate_t *tr, int macroblock_modes )
435 bs_transrate_t *bs = &tr->bs;
438 mbtypetab[tr->picture_coding_type-1][macroblock_modes&0x1F].code,
439 mbtypetab[tr->picture_coding_type-1][macroblock_modes&0x1F].len);
441 switch ( tr->picture_coding_type )
444 if ((! (tr->frame_pred_frame_dct)) && (tr->picture_structure == FRAME_PICTURE))
445 bs_write( bs, macroblock_modes & DCT_TYPE_INTERLACED ? 1 : 0, 1);
449 if (tr->picture_structure != FRAME_PICTURE)
451 if (macroblock_modes & MACROBLOCK_MOTION_FORWARD)
452 bs_write( bs, (macroblock_modes & MOTION_TYPE_MASK) / MOTION_TYPE_BASE, 2);
455 else if (tr->frame_pred_frame_dct) break;
458 if (macroblock_modes & MACROBLOCK_MOTION_FORWARD)
459 bs_write( bs, (macroblock_modes & MOTION_TYPE_MASK) / MOTION_TYPE_BASE, 2);
460 if (macroblock_modes & (MACROBLOCK_INTRA | MACROBLOCK_PATTERN))
461 bs_write( bs, macroblock_modes & DCT_TYPE_INTERLACED ? 1 : 0, 1);
466 if (tr->picture_structure != FRAME_PICTURE)
468 if (! (macroblock_modes & MACROBLOCK_INTRA))
469 bs_write( bs, (macroblock_modes & MOTION_TYPE_MASK) / MOTION_TYPE_BASE, 2);
472 else if (tr->frame_pred_frame_dct) break;
475 if (macroblock_modes & MACROBLOCK_INTRA) goto intra;
476 bs_write( bs, (macroblock_modes & MOTION_TYPE_MASK) / MOTION_TYPE_BASE, 2);
477 if (macroblock_modes & (MACROBLOCK_INTRA | MACROBLOCK_PATTERN))
480 bs_write( bs, macroblock_modes & DCT_TYPE_INTERLACED ? 1 : 0, 1);
487 static const uint8_t map_non_linear_mquant[113] =
489 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,
490 16,17,17,17,18,18,18,18,19,19,19,19,20,20,20,20,21,21,21,21,22,22,
491 22,22,23,23,23,23,24,24,24,24,24,24,24,25,25,25,25,25,25,25,26,26,
492 26,26,26,26,26,26,27,27,27,27,27,27,27,27,28,28,28,28,28,28,28,29,
493 29,29,29,29,29,29,29,29,29,30,30,30,30,30,30,30,31,31,31,31,31
495 static inline void put_quantiser( transrate_t *tr )
497 bs_transrate_t *bs = &tr->bs;
499 bs_write( bs, tr->q_scale_type ? map_non_linear_mquant[tr->new_quantizer_scale] : tr->new_quantizer_scale >> 1, 5 );
500 tr->last_coded_scale = tr->new_quantizer_scale;
503 /* generate variable length code for macroblock_address_increment (6.3.16) */
504 static inline void putaddrinc( transrate_t *tr, int addrinc )
506 bs_transrate_t *bs = &tr->bs;
508 while ( addrinc >= 33 )
510 bs_write( bs, 0x08, 11 ); /* macroblock_escape */
514 bs_write( bs, addrinctab[addrinc].code, addrinctab[addrinc].len );
517 static int slice_init( transrate_t *tr, int code )
519 bs_transrate_t *bs = &tr->bs;
523 tr->v_offset = (code - 1) * 16;
525 tr->quantizer_scale = get_quantizer_scale( tr );
526 if ( tr->new_quantizer_scale < tr->quantizer_scale )
527 tr->new_quantizer_scale = scale_quant( tr, tr->qrate );
529 /*LOGF("************************\nstart of slice %i in %s picture. ori quant: %i new quant: %i\n", code,
530 (picture_coding_type == I_TYPE ? "I_TYPE" : (picture_coding_type == P_TYPE ? "P_TYPE" : "B_TYPE")),
531 quantizer_scale, new_quantizer_scale);*/
533 /* ignore intra_slice and all the extra data */
534 while (bs->i_bit_in_cache & 0x80000000)
539 /* decode initial macroblock address increment */
543 if (bs->i_bit_in_cache >= 0x08000000)
545 mba = MBA_5 + (UBITS (bs->i_bit_in_cache, 6) - 2);
548 else if (bs->i_bit_in_cache >= 0x01800000)
550 mba = MBA_11 + (UBITS (bs->i_bit_in_cache, 12) - 24);
553 else if( UBITS (bs->i_bit_in_cache, 12 ) == 8 )
555 /* macroblock_escape */
565 bs_flush(bs, mba->len + 1);
566 tr->h_offset = (offset + mba->mba) << 4;
568 while( tr->h_offset - (int)tr->horizontal_size_value >= 0)
570 tr->h_offset -= tr->horizontal_size_value;
574 if( tr->v_offset > tr->vertical_size_value - 16 )
578 return (offset + mba->mba);
581 static void mpeg2_slice( transrate_t *tr, const int code )
583 bs_transrate_t *bs = &tr->bs;
585 int first_in_slice = 1;
587 if( (mba_inc = slice_init( tr, code )) < 0 )
595 int macroblock_modes;
599 while (unlikely(bs->i_bit_in < 24)) bs_refill( bs );
601 macroblock_modes = get_macroblock_modes( tr );
602 if (macroblock_modes & MACROBLOCK_QUANT)
603 tr->quantizer_scale = get_quantizer_scale( tr );
604 if (tr->new_quantizer_scale < tr->quantizer_scale)
605 tr->new_quantizer_scale = scale_quant( tr, tr->qrate );
607 //LOGF("blk %i : ", h_offset >> 4);
609 if (macroblock_modes & MACROBLOCK_INTRA)
611 RunLevel block[6][65]; // terminated by level = 0, so we need 64+1
612 RunLevel new_block[6][65]; // terminated by level = 0, so we need 64+1
618 uint8_t p_n_ow[32], *p_n_w,
619 *p_o_ow = bs->p_ow, *p_o_w = bs->p_w;
620 uint32_t i_n_bit_out, i_n_bit_out_cache,
621 i_o_bit_out = bs->i_bit_out, i_o_bit_out_cache = bs->i_bit_out_cache;
623 bs->i_bit_out_cache = 0; bs->i_bit_out = BITS_IN_BUF;
624 bs->p_ow = bs->p_w = p_n_ow;
626 //LOG("intra "); if (macroblock_modes & MACROBLOCK_QUANT) LOGF("got new quant: %i ", quantizer_scale);
628 if (tr->concealment_motion_vectors)
630 if (tr->picture_structure != FRAME_PICTURE)
632 bs_copy(bs, 1); /* remove field_select */
634 /* like motion_frame, but parsing without actual motion compensation */
635 get_motion_delta(bs, tr->f_code[0][0]);
636 get_motion_delta(bs, tr->f_code[0][1]);
638 bs_copy(bs, 1); /* remove marker_bit */
641 assert(bs->p_w - bs->p_ow < 32);
644 i_n_bit_out = bs->i_bit_out;
645 i_n_bit_out_cache = bs->i_bit_out_cache;
646 assert(bs->p_ow == p_n_ow);
648 bs->i_bit_out = i_o_bit_out ;
649 bs->i_bit_out_cache = i_o_bit_out_cache;
654 if( tr->intra_vlc_format )
657 for ( i = 0; i < 4; i++ )
659 get_luma_dc_dct_diff( bs, dc + i, dc_len + i );
660 get_intra_block_B15( tr, block[i] );
661 if (tr->b_error) return;
666 get_chroma_dc_dct_diff( bs, dc + i, dc_len + i );
667 get_intra_block_B15( tr, block[i] );
668 if (tr->b_error) return;
674 for ( i = 0; i < 4; i++ )
676 get_luma_dc_dct_diff( bs, dc + i, dc_len + i );
677 get_intra_block_B14( tr, block[i] );
678 if (tr->b_error) return;
683 get_chroma_dc_dct_diff( bs, dc + i, dc_len + i );
684 get_intra_block_B14( tr, block[i] );
685 if (tr->b_error) return;
689 transrate_mb( tr, block, new_block, 0x3f, 1 );
691 if (tr->last_coded_scale == tr->new_quantizer_scale)
692 macroblock_modes &= ~MACROBLOCK_QUANT;
694 if ( first_in_slice )
697 bs_write( bs, 0, 1 );
698 macroblock_modes &= ~MACROBLOCK_QUANT;
700 putaddrinc( tr, mba_inc );
702 putmbdata( tr, macroblock_modes );
703 if( macroblock_modes & MACROBLOCK_QUANT )
708 // put saved motion data...
709 for (batb = 0; batb < (p_n_w - p_n_ow); batb++)
711 bs_write( bs, p_n_ow[batb], 8 );
713 bs_write( bs, i_n_bit_out_cache, BITS_IN_BUF - i_n_bit_out );
714 // end saved motion data...
716 for ( i = 0; i < 6; i++ )
718 bs_write( bs, *(dc + i), *(dc_len + i) );
719 putintrablk( bs, new_block[i], tr->intra_vlc_format );
725 RunLevel block[6][65]; // terminated by level = 0, so we need 64+1
726 RunLevel new_block[6][65]; // terminated by level = 0, so we need 64+1
727 int new_coded_block_pattern = 0;
732 uint8_t p_n_ow[32], *p_n_w,
733 *p_o_ow = bs->p_ow, *p_o_w = bs->p_w;
734 uint32_t i_n_bit_out, i_n_bit_out_cache,
735 i_o_bit_out = bs->i_bit_out, i_o_bit_out_cache = bs->i_bit_out_cache;
737 bs->i_bit_out_cache = 0; bs->i_bit_out = BITS_IN_BUF;
738 bs->p_ow = bs->p_w = p_n_ow;
740 if (tr->picture_structure == FRAME_PICTURE)
741 switch (macroblock_modes & MOTION_TYPE_MASK)
743 case MC_FRAME: MOTION_CALL (motion_fr_frame, macroblock_modes); break;
744 case MC_FIELD: MOTION_CALL (motion_fr_field, macroblock_modes); break;
745 case MC_DMV: MOTION_CALL (motion_fr_dmv, MACROBLOCK_MOTION_FORWARD); break;
748 switch (macroblock_modes & MOTION_TYPE_MASK)
750 case MC_FIELD: MOTION_CALL (motion_fi_field, macroblock_modes); break;
751 case MC_16X8: MOTION_CALL (motion_fi_16x8, macroblock_modes); break;
752 case MC_DMV: MOTION_CALL (motion_fi_dmv, MACROBLOCK_MOTION_FORWARD); break;
755 //LOG("non intra "); if (macroblock_modes & MACROBLOCK_QUANT) LOGF("got new quant: %i ", quantizer_scale);
757 if (macroblock_modes & MACROBLOCK_PATTERN)
761 cbp = get_coded_block_pattern( bs );
763 for ( i = 0; i < 6; i++ )
765 if ( cbp & (1 << (5 - i)) )
767 get_non_intra_block( tr, block[i] );
768 if (tr->b_error) return;
771 last_in_slice = !UBITS( bs->i_bit_in_cache, 11 );
773 new_coded_block_pattern = transrate_mb( tr, block, new_block,
776 if ( !new_coded_block_pattern &&
778 & (MACROBLOCK_MOTION_FORWARD
779 | MACROBLOCK_MOTION_BACKWARD))
780 && (first_in_slice || last_in_slice) )
782 /* First mb in slice, just code a 0-mv mb.
783 * This is wrong for last in slice, but it only shows
784 * a few artefacts. */
785 macroblock_modes |= MACROBLOCK_MOTION_FORWARD;
786 if (tr->picture_structure == FRAME_PICTURE)
788 macroblock_modes |= MC_FRAME;
789 bs_write( bs, 0x3, 2 ); /* motion vectors */
793 macroblock_modes |= MC_FIELD;
795 (tr->picture_structure == BOTTOM_FIELD ? 1 : 0),
796 1); /* motion field select */
797 bs_write( bs, 0x3, 2 ); /* motion vectors */
801 if ( !new_coded_block_pattern )
803 macroblock_modes &= ~MACROBLOCK_PATTERN;
804 macroblock_modes &= ~MACROBLOCK_QUANT;
808 if ( tr->last_coded_scale == tr->new_quantizer_scale )
810 macroblock_modes &= ~MACROBLOCK_QUANT;
814 macroblock_modes |= MACROBLOCK_QUANT;
819 assert(bs->p_w - bs->p_ow < 32);
822 i_n_bit_out = bs->i_bit_out;
823 i_n_bit_out_cache = bs->i_bit_out_cache;
824 assert(bs->p_ow == p_n_ow);
826 bs->i_bit_out = i_o_bit_out ;
827 bs->i_bit_out_cache = i_o_bit_out_cache;
832 if ( macroblock_modes &
833 (MACROBLOCK_MOTION_FORWARD | MACROBLOCK_MOTION_BACKWARD
834 | MACROBLOCK_PATTERN) )
836 if ( first_in_slice )
839 bs_write( bs, 0, 1 );
840 macroblock_modes &= ~MACROBLOCK_QUANT;
842 putaddrinc( tr, mba_inc );
844 putmbdata( tr, macroblock_modes );
845 if ( macroblock_modes & MACROBLOCK_QUANT )
850 // put saved motion data...
851 for (batb = 0; batb < (p_n_w - p_n_ow); batb++)
853 bs_write( bs, p_n_ow[batb], 8 );
855 bs_write( bs, i_n_bit_out_cache, BITS_IN_BUF - i_n_bit_out);
856 // end saved motion data...
858 if (macroblock_modes & MACROBLOCK_PATTERN)
861 bs_write( bs, cbptable[new_coded_block_pattern].code,
862 cbptable[new_coded_block_pattern].len );
864 for ( i = 0; i < 6; i++ )
866 if ( new_coded_block_pattern & (1 << (5 - i)) )
868 putnonintrablk( bs, new_block[i] );
875 /* skipped macroblock */
880 if (bs->p_c > bs->p_r || bs->p_w > bs->p_rw)
885 //LOGF("\n\to: %i c: %i n: %i\n", quantizer_scale, last_coded_scale, new_quantizer_scale);
893 if ( bs->i_bit_in_cache >= 0x10000000 )
895 mba = MBA_5 + (UBITS (bs->i_bit_in_cache, 5) - 2);
898 else if ( bs->i_bit_in_cache >= 0x03000000 )
900 mba = MBA_11 + (UBITS (bs->i_bit_in_cache, 11) - 24);
903 else if ( UBITS( bs->i_bit_in_cache, 11 ) == 8 )
905 /* macroblock_escape */
916 bs_flush(bs, mba->len);
918 mba_local += mba->mba;
927 static const uint8_t mpeg2_scan_norm[64] ATTR_ALIGN(16) = {
928 /* Zig-Zag scan pattern */
929 0, 1, 8, 16, 9, 2, 3, 10, 17, 24, 32, 25, 18, 11, 4, 5,
930 12, 19, 26, 33, 40, 48, 41, 34, 27, 20, 13, 6, 7, 14, 21, 28,
931 35, 42, 49, 56, 57, 50, 43, 36, 29, 22, 15, 23, 30, 37, 44, 51,
932 58, 59, 52, 45, 38, 31, 39, 46, 53, 60, 61, 54, 47, 55, 62, 63
935 static const uint8_t mpeg2_scan_alt[64] ATTR_ALIGN(16) = {
936 /* Alternate scan pattern */
937 0, 8, 16, 24, 1, 9, 2, 10, 17, 25, 32, 40, 48, 56, 57, 49,
938 41, 33, 26, 18, 3, 11, 4, 12, 19, 27, 34, 42, 50, 58, 35, 43,
939 51, 59, 20, 28, 5, 13, 6, 14, 21, 29, 36, 44, 52, 60, 37, 45,
940 53, 61, 22, 30, 7, 15, 23, 31, 38, 46, 54, 62, 39, 47, 55, 63
943 static const int16_t default_intra_matrix[64] = {
944 8, 16, 19, 22, 26, 27, 29, 34,
945 16, 16, 22, 24, 27, 29, 34, 37,
946 19, 22, 26, 27, 29, 34, 34, 38,
947 22, 22, 26, 27, 29, 34, 37, 40,
948 22, 26, 27, 29, 32, 35, 40, 48,
949 26, 27, 29, 32, 35, 40, 48, 58,
950 26, 27, 29, 34, 38, 46, 56, 69,
951 27, 29, 35, 38, 46, 56, 69, 83
954 static int mpeg2_header_sequence( transrate_t * tr )
956 bs_transrate_t *bs = &tr->bs;
957 int has_intra = 0, has_non_intra = 0;
960 i = (bs->p_c[0] << 16) | (bs->p_c[1] << 8) | bs->p_c[2];
961 tr->horizontal_size_value = i >> 12;
962 tr->vertical_size_value = i & 0xfff;
963 tr->horizontal_size_value = (tr->horizontal_size_value + 15) & ~15;
964 tr->vertical_size_value = (tr->vertical_size_value + 15) & ~15;
965 if ( !tr->horizontal_size_value || !tr->vertical_size_value )
970 if ( tr->mpeg4_matrix )
975 for (i = 0; i < 64; i++)
976 tr->intra_quantizer_matrix[mpeg2_scan_norm[i]] =
977 (bs->p_c[i+7] << 7) | (bs->p_c[i+8] >> 1);
981 for (i = 0; i < 64; i++)
982 tr->intra_quantizer_matrix[mpeg2_scan_norm[i]] =
983 default_intra_matrix[i];
986 if (bs->p_c[7+64] & 1)
989 for (i = 0; i < 64; i++)
990 tr->non_intra_quantizer_matrix[mpeg2_scan_norm[i]] =
995 for (i = 0; i < 64; i++)
996 tr->non_intra_quantizer_matrix[i] = 16;
1000 /* Write quantization matrices */
1001 memcpy( bs->p_w, bs->p_c, 8 );
1004 if ( tr->mpeg4_matrix )
1006 memset( &bs->p_w[8], 0, 128 );
1009 for (i = 0; i < 64; i++)
1011 bs->p_w[i+7] |= mpeg4_default_intra_matrix[mpeg2_scan_norm[i]] >> 7;
1012 bs->p_w[i+8] |= mpeg4_default_intra_matrix[mpeg2_scan_norm[i]] << 1;
1016 for (i = 0; i < 64; i++)
1018 bs->p_w[i+8+64] |= mpeg4_default_intra_matrix[mpeg2_scan_norm[i]];
1021 bs->p_c += (has_intra + has_non_intra) * 64;
1028 tr->scan = mpeg2_scan_norm;
1033 /////---- end ext mpeg code
1035 static int do_next_start_code( transrate_t *tr )
1037 bs_transrate_t *bs = &tr->bs;
1044 *bs->p_w++ = *bs->p_c++;
1046 if (ID == 0x00) // pic header
1048 tr->picture_coding_type = (bs->p_c[1] >> 3) & 0x7;
1049 bs->p_c[1] |= 0x7; bs->p_c[2] = 0xFF; bs->p_c[3] |= 0xF8; // vbv_delay is now 0xFFFF
1051 memcpy(bs->p_w, bs->p_c, 4);
1055 else if (ID == 0xB3) // seq header
1057 mpeg2_header_sequence(tr);
1059 else if (ID == 0xB5) // extension
1061 if ((bs->p_c[0] >> 4) == 0x8) // pic coding ext
1063 tr->f_code[0][0] = (bs->p_c[0] & 0xF) - 1;
1064 tr->f_code[0][1] = (bs->p_c[1] >> 4) - 1;
1065 tr->f_code[1][0] = (bs->p_c[1] & 0xF) - 1;
1066 tr->f_code[1][1] = (bs->p_c[2] >> 4) - 1;
1068 /* tr->intra_dc_precision = (bs->p_c[2] >> 2) & 0x3; */
1069 tr->picture_structure = bs->p_c[2] & 0x3;
1070 tr->frame_pred_frame_dct = (bs->p_c[3] >> 6) & 0x1;
1071 tr->concealment_motion_vectors = (bs->p_c[3] >> 5) & 0x1;
1072 tr->q_scale_type = (bs->p_c[3] >> 4) & 0x1;
1073 tr->intra_vlc_format = (bs->p_c[3] >> 3) & 0x1;
1074 if ( (bs->p_c[3] >> 2) & 0x1 )
1075 tr->scan = mpeg2_scan_alt;
1077 memcpy(bs->p_w, bs->p_c, 5);
1083 *bs->p_w++ = *bs->p_c++;
1086 else if (ID == 0xB8) // gop header
1088 memcpy(bs->p_w, bs->p_c, 4);
1092 else if ((ID >= 0x01) && (ID <= 0xAF)) // slice
1094 uint8_t *outTemp = bs->p_w, *inTemp = bs->p_c;
1096 if( tr->qrate != 1.0 )
1098 if( !tr->horizontal_size_value || !tr->vertical_size_value )
1104 bs->i_bit_in_cache = 0; bs->i_bit_in = 0;
1105 bs->i_bit_out_cache = 0; bs->i_bit_out = BITS_IN_BUF;
1113 // begin bit level recoding
1114 mpeg2_slice(tr, ID);
1115 if (tr->b_error) return -1;
1117 bs_flush_read( bs );
1118 bs_flush_write( bs );
1119 // end bit level recoding
1121 /* Basic sanity checks --Meuuh */
1122 if (bs->p_c > bs->p_r || bs->p_w > bs->p_rw)
1127 /*LOGF("type: %s code: %02i in : %6i out : %6i diff : %6i fact: %2.2f\n",
1128 (picture_coding_type == I_TYPE ? "I_TYPE" : (picture_coding_type == P_TYPE ? "P_TYPE" : "B_TYPE")),
1129 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));*/
1131 if (bs->p_w - outTemp > bs->p_c - inTemp) // yes that might happen, rarely
1134 /*LOGF("*** slice bigger than before !! (type: %s code: %i in : %i out : %i diff : %i)\n",
1135 (picture_coding_type == I_TYPE ? "I_TYPE" : (picture_coding_type == P_TYPE ? "P_TYPE" : "B_TYPE")),
1136 ID, bs->p_c - inTemp, bs->p_w - outTemp, (bs->p_w - outTemp) - (bs->p_c - inTemp));*/
1138 if ( !tr->mpeg4_matrix )
1140 // in this case, we'll just use the original slice !
1141 memcpy(outTemp, inTemp, bs->p_c - inTemp);
1142 bs->p_w = outTemp + (bs->p_c - inTemp);
1144 // adjust bs->i_byte_out
1145 bs->i_byte_out -= (bs->p_w - outTemp) - (bs->p_c - inTemp);
1149 fprintf(stderr, "bad choice for mpeg4-matrix...\n");
1157 int process_frame( sout_stream_t *p_stream, sout_stream_id_t *id,
1158 block_t *in, block_t **out, int i_handicap )
1160 transrate_t *tr = &id->tr;
1161 bs_transrate_t *bs = &tr->bs;
1165 double f_drift, f_fact;
1168 p_out = block_New( p_stream, in->i_buffer * 3 );
1170 p_out->i_length = in->i_length;
1171 p_out->i_dts = in->i_dts;
1172 p_out->i_pts = in->i_pts;
1173 p_out->i_flags = in->i_flags;
1175 bs->p_rw = bs->p_ow = bs->p_w = p_out->p_buffer;
1176 bs->p_c = bs->p_r = in->p_buffer;
1177 bs->p_r += in->i_buffer + 4;
1178 bs->p_rw += in->i_buffer * 2;
1179 *(in->p_buffer + in->i_buffer) = 0;
1180 *(in->p_buffer + in->i_buffer + 1) = 0;
1181 *(in->p_buffer + in->i_buffer + 2) = 1;
1182 *(in->p_buffer + in->i_buffer + 3) = 0;
1184 /* Calculate how late we are */
1185 bs->i_byte_in = in->i_buffer;
1188 i_drift = tr->i_current_output + tr->i_remaining_input
1189 - tr->i_wanted_output;
1190 f_drift = (double)i_drift / tr->i_wanted_output;
1191 f_fact = (double)(tr->i_wanted_output - tr->i_current_output)
1192 / tr->i_remaining_input;
1194 if ( in->i_flags & BLOCK_FLAG_TYPE_I )
1196 /* This is the last picture of the GOP ; only transrate if we're
1198 if ( 0 && f_drift > 0.085 )
1200 tr->i_minimum_error = (f_drift - 0.085) * 50.0 * 50.0;
1201 tr->i_admissible_error = (f_drift - 0.085) * 50.0 * 75.0;
1202 tr->qrate = 1.0 + (f_drift - 0.085) * 50.0;
1203 msg_Warn( p_stream, "transrating I %d/%d",
1204 tr->i_minimum_error, tr->i_admissible_error );
1208 tr->i_minimum_error = 0;
1209 tr->i_admissible_error = 0;
1213 else if ( in->i_flags & BLOCK_FLAG_TYPE_P )
1217 tr->i_minimum_error = (0.8 - f_fact) * 3000.0 + i_handicap;
1218 tr->i_admissible_error = (0.8 - f_fact) * 3500.0 + i_handicap;
1219 tr->qrate = 1.0 + (0.8 - f_fact) * 70.0;
1223 tr->i_minimum_error = 0;
1224 tr->i_admissible_error = 0;
1232 tr->i_minimum_error = (1.2 - f_fact) * 1750.0 + i_handicap;
1233 tr->i_admissible_error = (1.2 - f_fact) * 2250.0 + i_handicap;
1234 tr->qrate = 1.0 + (1.2 - f_fact) * 45.0;
1238 tr->i_minimum_error = 0;
1239 tr->i_admissible_error = 0;
1244 tr->new_quantizer_scale = 0;
1249 uint8_t *p_end = &in->p_buffer[in->i_buffer];
1251 /* Search next start code */
1254 if( bs->p_c < p_end - 3 && bs->p_c[0] == 0 && bs->p_c[1] == 0 && bs->p_c[2] == 1 )
1256 /* Next start code */
1259 else if( bs->p_c < p_end - 6 &&
1260 bs->p_c[0] == 0 && bs->p_c[1] == 0 && bs->p_c[2] == 0 &&
1261 bs->p_c[3] == 0 && bs->p_c[4] == 0 && bs->p_c[5] == 0 )
1263 /* remove stuffing (looking for 6 0x00 bytes) */
1269 *bs->p_w++ = *bs->p_c++;
1272 if( bs->p_c >= p_end )
1278 if( bs->p_c >= p_end )
1283 /* Copy the start code */
1284 memcpy( bs->p_w, bs->p_c, 3 );
1288 if ( do_next_start_code( tr ) )
1291 msg_Err( p_stream, "error in do_next_start_code()" );
1292 block_Release( p_out );
1293 tr->i_remaining_input -= in->i_buffer;
1294 tr->i_current_output += in->i_buffer;
1299 bs->i_byte_out += bs->p_w - bs->p_ow;
1300 p_out->i_buffer = bs->p_w - bs->p_ow;
1303 if ( in->i_flags & BLOCK_FLAG_TYPE_P && f_fact < 0.8 )
1305 double f_ratio = (in->i_buffer - p_out->i_buffer) / in->i_buffer;
1306 if ( f_ratio < (0.8 - f_fact) * 0.1 && i_handicap < 200 )
1308 block_Release( p_out );
1309 return process_frame( p_stream, id, in, out, i_handicap + 50 );
1313 if ( in->i_flags & BLOCK_FLAG_TYPE_B && f_fact < 1.1 )
1315 double f_ratio = (double)(in->i_buffer - p_out->i_buffer)
1317 if ( f_ratio < (1.1 - f_fact) * 0.1 && i_handicap < 400 )
1319 #ifdef DEBUG_TRANSRATER
1320 msg_Dbg( p_stream, "%d: %d -> %d big (f: %f d: %f)",
1321 tr->picture_coding_type, in->i_buffer, p_out->i_buffer,
1324 block_Release( p_out );
1325 return process_frame( p_stream, id, in, out, i_handicap + 100 );
1333 for ( toto = 0; toto < p_out->i_buffer; toto++ )
1334 if (in->p_buffer[toto] != p_out->p_buffer[toto])
1335 msg_Dbg(p_stream, "toto %d %x %x", toto, in->p_buffer[toto], p_out->p_buffer[toto]);
1339 block_ChainAppend( out, p_out );
1340 tr->i_remaining_input -= in->i_buffer;
1341 tr->i_current_output += p_out->i_buffer;
1343 #ifdef DEBUG_TRANSRATER
1344 msg_Dbg( p_stream, "%d: %d -> %d (%d/%d)",
1345 tr->picture_coding_type, in->i_buffer, p_out->i_buffer,
1346 tr->i_minimum_error, tr->i_admissible_error );