/****************************************************************************
* x264_nal_encode:
****************************************************************************/
-int x264_nal_encode( uint8_t *dst, int b_annexb, x264_nal_t *nal )
+int x264_nal_encode( uint8_t *dst, x264_nal_t *nal, int b_annexb, int b_long_startcode )
{
uint8_t *src = nal->p_payload;
uint8_t *end = nal->p_payload + nal->i_payload;
uint8_t *orig_dst = dst;
int i_count = 0, size;
- /* long nal start code (we always use long ones) */
if( b_annexb )
{
- *dst++ = 0x00;
+ if( b_long_startcode )
+ *dst++ = 0x00;
*dst++ = 0x00;
*dst++ = 0x00;
*dst++ = 0x01;
* the encoding options */
char *x264_param2string( x264_param_t *p, int b_res );
-int x264_nal_encode( uint8_t *dst, int b_annexb, x264_nal_t *nal );
+int x264_nal_encode( uint8_t *dst, x264_nal_t *nal, int b_annexb, int b_long_startcode );
/* log */
void x264_log( x264_t *h, int i_level, const char *psz_fmt, ... );
}
uint8_t *nal_buffer = h->nal_buffer;
+ int long_startcode = 1;
for( i = 0; i < h->out.i_nal; i++ )
{
- int size = x264_nal_encode( nal_buffer, h->param.b_annexb, &h->out.nal[i] );
+ int size = x264_nal_encode( nal_buffer, &h->out.nal[i], h->param.b_annexb, long_startcode );
+ /* Don't use long startcodes for any slice beyond the first. */
+ if( h->out.nal[i].i_type >= NAL_SLICE && h->out.nal[i].i_type <= NAL_SLICE_IDR )
+ long_startcode = 0;
h->out.nal[i].i_payload = size;
h->out.nal[i].p_payload = nal_buffer;
nal_buffer += size;
bs_t bs_bak;
x264_cabac_t cabac_bak;
uint8_t cabac_prevbyte_bak = 0; /* Shut up GCC. */
- /* Assume no more than 3 bytes of NALU escaping. */
- int slice_max_size = h->param.i_slice_max_size > 0 ? (h->param.i_slice_max_size-3-NALU_OVERHEAD)*8 : INT_MAX;
+ /* Assume no more than 3 bytes of NALU escaping.
+ * Slices other than the first use a 3-byte startcode. */
+ int overhead_guess = (NALU_OVERHEAD - (h->param.b_annexb && h->sh.i_first_mb)) + 3;
+ int slice_max_size = h->param.i_slice_max_size > 0 ? (h->param.i_slice_max_size-overhead_guess)*8 : INT_MAX;
int starting_bits = bs_pos(&h->out.bs);
bs_realign( &h->out.bs );