static inline void bs_write( bs_t *s, int i_count, uint32_t i_bits )
{
- if( s->p >= s->p_end - 4 )
- return;
while( i_count > 0 )
{
if( i_count < 32 )
static inline void bs_write1( bs_t *s, uint32_t i_bit )
{
- if( s->p < s->p_end )
+ *s->p <<= 1;
+ *s->p |= i_bit;
+ s->i_left--;
+ if( s->i_left == 0 )
{
- *s->p <<= 1;
- *s->p |= i_bit;
- s->i_left--;
- if( s->i_left == 0 )
- {
- s->p++;
- s->i_left = 8;
- }
+ s->p++;
+ s->i_left = 8;
}
}
{
int carry = out >> 8;
int bytes_outstanding = cb->i_bytes_outstanding;
- if( cb->p + bytes_outstanding + 1 >= cb->p_end )
- return;
// this can't modify before the beginning of the stream because
// that would correspond to a probability > 1.
// it will write before the beginning of the stream, which is ok
cb->i_queue = 8;
x264_cabac_putbyte( cb );
- if( cb->p + cb->i_bytes_outstanding + 1 >= cb->p_end )
- return; //FIXME throw an error instead of silently truncating the frame
-
while( cb->i_bytes_outstanding > 0 )
{
*(cb->p++) = 0xff;
}
}
+/* If we are within a reasonable distance of the end of the memory allocated for the bitstream, */
+/* reallocate, adding an arbitrary amount of space (100 kilobytes). */
+static void x264_bitstream_check_buffer( x264_t *h )
+{
+ if( ( h->param.b_cabac && (h->cabac.p_end - h->cabac.p < 2500) )
+ || ( h->out.bs.p_end - h->out.bs.p < 2500 ) )
+ {
+ uint8_t *bs_bak = h->out.p_bitstream;
+ intptr_t delta;
+ int i;
+
+ h->out.i_bitstream += 100000;
+ h->out.p_bitstream = x264_realloc( h->out.p_bitstream, h->out.i_bitstream );
+ delta = h->out.p_bitstream - bs_bak;
+
+ h->out.bs.p_start += delta;
+ h->out.bs.p += delta;
+ h->out.bs.p_end = h->out.p_bitstream + h->out.i_bitstream;
+
+ h->cabac.p_start += delta;
+ h->cabac.p += delta;
+ h->cabac.p_end = h->out.p_bitstream + h->out.i_bitstream;
+
+ for( i = 0; i <= h->out.i_nal; i++ )
+ h->out.nal[i].p_payload += delta;
+ }
+}
+
/****************************************************************************
*
****************************************************************************
/* encode this macroblock -> be careful it can change the mb type to P_SKIP if needed */
x264_macroblock_encode( h );
+ x264_bitstream_check_buffer( h );
+
if( h->param.b_cabac )
{
if( mb_xy > h->sh.i_first_mb && !(h->sh.b_mbaff && (i_mb_y&1)) )