/*****************************************************************************
* encoder.c: top-level encoder functions
*****************************************************************************
- * Copyright (C) 2003-2015 x264 project
+ * Copyright (C) 2003-2016 x264 project
*
* Authors: Laurent Aimar <fenrir@via.ecp.fr>
* Loren Merritt <lorenm@u.washington.edu>
/* Write the frame in display order */
int frame_size = FRAME_SIZE( h->param.i_height * h->param.i_width * sizeof(pixel) );
- fseek( f, (uint64_t)h->fdec->i_frame * frame_size, SEEK_SET );
- for( int p = 0; p < (CHROMA444 ? 3 : 1); p++ )
- for( int y = 0; y < h->param.i_height; y++ )
- fwrite( &h->fdec->plane[p][y*h->fdec->i_stride[p]], sizeof(pixel), h->param.i_width, f );
- if( !CHROMA444 )
- {
- int cw = h->param.i_width>>1;
- int ch = h->param.i_height>>CHROMA_V_SHIFT;
- pixel *planeu = x264_malloc( (cw*ch*2+32)*sizeof(pixel) );
- if( planeu )
+ if( !fseek( f, (int64_t)h->fdec->i_frame * frame_size, SEEK_SET ) )
+ {
+ for( int p = 0; p < (CHROMA444 ? 3 : 1); p++ )
+ for( int y = 0; y < h->param.i_height; y++ )
+ fwrite( &h->fdec->plane[p][y*h->fdec->i_stride[p]], sizeof(pixel), h->param.i_width, f );
+ if( !CHROMA444 )
{
- pixel *planev = planeu + cw*ch + 16;
- h->mc.plane_copy_deinterleave( planeu, cw, planev, cw, h->fdec->plane[1], h->fdec->i_stride[1], cw, ch );
- fwrite( planeu, 1, cw*ch*sizeof(pixel), f );
- fwrite( planev, 1, cw*ch*sizeof(pixel), f );
- x264_free( planeu );
+ int cw = h->param.i_width>>1;
+ int ch = h->param.i_height>>CHROMA_V_SHIFT;
+ pixel *planeu = x264_malloc( (cw*ch*2+32)*sizeof(pixel) );
+ if( planeu )
+ {
+ pixel *planev = planeu + cw*ch + 16;
+ h->mc.plane_copy_deinterleave( planeu, cw, planev, cw, h->fdec->plane[1], h->fdec->i_stride[1], cw, ch );
+ fwrite( planeu, 1, cw*ch*sizeof(pixel), f );
+ fwrite( planev, 1, cw*ch*sizeof(pixel), f );
+ x264_free( planeu );
+ }
}
}
fclose( f );
h->param.i_fps_num = 25;
h->param.i_fps_den = 1;
}
- float fps = (float) h->param.i_fps_num / h->param.i_fps_den;
+ float fps = (float)h->param.i_fps_num / h->param.i_fps_den;
if( h->param.i_keyint_min == X264_KEYINT_MIN_AUTO )
- h->param.i_keyint_min = X264_MIN( h->param.i_keyint_max / 10, fps );
+ h->param.i_keyint_min = X264_MIN( h->param.i_keyint_max / 10, (int)fps );
h->param.i_keyint_min = x264_clip3( h->param.i_keyint_min, 1, h->param.i_keyint_max/2+1 );
h->param.rc.i_lookahead = x264_clip3( h->param.rc.i_lookahead, 0, X264_LOOKAHEAD_MAX );
{
{
x264_t *h;
char buf[1000], *p;
- int qp, i_slicetype_length;
+ int i_slicetype_length;
CHECKED_MALLOCZERO( h, sizeof(x264_t) );
p += sprintf( p, " none!" );
x264_log( h, X264_LOG_INFO, "%s\n", buf );
- float *logs = x264_analyse_prepare_costs( h );
- if( !logs )
- goto fail;
- for( qp = X264_MIN( h->param.rc.i_qp_min, QP_MAX_SPEC ); qp <= h->param.rc.i_qp_max; qp++ )
- if( x264_analyse_init_costs( h, logs, qp ) )
- goto fail;
- if( x264_analyse_init_costs( h, logs, X264_LOOKAHEAD_QP ) )
+ if( x264_analyse_init_costs( h ) )
goto fail;
- x264_free( logs );
static const uint16_t cost_mv_correct[7] = { 24, 47, 95, 189, 379, 757, 1515 };
/* Checks for known miscompilation issues. */
for( int i = start; i < h->out.i_nal; i++ )
{
- int old_payload_len = h->out.nal[i].i_payload;
h->out.nal[i].b_long_startcode = !i || h->out.nal[i].i_type == NAL_SPS || h->out.nal[i].i_type == NAL_PPS ||
h->param.i_avcintra_class;
x264_nal_encode( h, nal_buffer, &h->out.nal[i] );
nal_buffer += h->out.nal[i].i_payload;
- if( h->param.i_avcintra_class )
- {
- h->out.nal[i].i_padding -= h->out.nal[i].i_payload - (old_payload_len + NALU_OVERHEAD);
- if( h->out.nal[i].i_padding > 0 )
- {
- memset( nal_buffer, 0, h->out.nal[i].i_padding );
- nal_buffer += h->out.nal[i].i_padding;
- h->out.nal[i].i_payload += h->out.nal[i].i_padding;
- }
- h->out.nal[i].i_padding = X264_MAX( h->out.nal[i].i_padding, 0 );
- }
}
x264_emms();
static void x264_thread_sync_stat( x264_t *dst, x264_t *src )
{
- if( dst == src )
- return;
- memcpy( &dst->stat.i_frame_count, &src->stat.i_frame_count, sizeof(dst->stat) - sizeof(dst->stat.frame) );
+ if( dst != src )
+ memcpy( &dst->stat, &src->stat, offsetof(x264_t, stat.frame) - offsetof(x264_t, stat) );
}
static void *x264_slices_write( x264_t *h )
if( x264_threadpool_wait_all( h ) < 0 )
return -1;
- if( h->i_frame == h->i_thread_frames - 1 )
+ if( h->i_frame == 0 )
h->i_reordered_pts_delay = h->fenc->i_reordered_pts;
if( h->reconfig )
{
* We don't know the size of the last slice until encapsulation so we add filler to the encapsulated NAL */
if( h->param.i_avcintra_class )
{
- x264_t *h0 = h->thread[0];
- int ret = x264_check_encapsulated_buffer( h, h0, h->out.i_nal, frame_size, frame_size + filler );
- if( ret < 0 )
+ if( x264_check_encapsulated_buffer( h, h->thread[0], h->out.i_nal, frame_size, frame_size + filler ) < 0 )
return -1;
- memset( h->out.nal[0].p_payload + frame_size, 0, filler );
- h->out.nal[h->out.i_nal-1].i_payload += filler;
- h->out.nal[h->out.i_nal-1].i_padding = filler;
+
+ x264_nal_t *nal = &h->out.nal[h->out.i_nal-1];
+ memset( nal->p_payload + nal->i_payload, 0, filler );
+ nal->i_payload += filler;
+ nal->i_padding = filler;
frame_size += filler;
+
+ /* Fix up the size header for mp4/etc */
+ if( !h->param.b_annexb )
+ {
+ /* Size doesn't include the size of the header we're writing now. */
+ uint8_t *nal_data = nal->p_payload;
+ int chunk_size = nal->i_payload - 4;
+ nal_data[0] = chunk_size >> 24;
+ nal_data[1] = chunk_size >> 16;
+ nal_data[2] = chunk_size >> 8;
+ nal_data[3] = chunk_size >> 0;
+ }
}
else
{
while( filler > 0 )
{
- int f, overhead;
- overhead = (FILLER_OVERHEAD - h->param.b_annexb);
+ int f, overhead = FILLER_OVERHEAD - h->param.b_annexb;
if( h->param.i_slice_max_size && filler > h->param.i_slice_max_size )
{
int next_size = filler - h->param.i_slice_max_size;
{
pic_out->prop.f_ssim = h->stat.frame.f_ssim / h->stat.frame.i_ssim_cnt;
h->stat.f_ssim_mean_y[h->sh.i_type] += pic_out->prop.f_ssim * dur;
- snprintf( psz_message + strlen(psz_message), 80 - strlen(psz_message),
- " SSIM Y:%.5f", pic_out->prop.f_ssim );
+ int msg_len = strlen(psz_message);
+ snprintf( psz_message + msg_len, 80 - msg_len, " SSIM Y:%.5f", pic_out->prop.f_ssim );
}
psz_message[79] = '\0';
x264_log( h, X264_LOG_DEBUG,
- "frame=%4d QP=%.2f NAL=%d Slice:%c Poc:%-3d I:%-4d P:%-4d SKIP:%-4d size=%d bytes%s\n",
+ "frame=%4d QP=%.2f NAL=%d Slice:%c Poc:%-3d I:%-4d P:%-4d SKIP:%-4d size=%d bytes%s\n",
h->i_frame,
h->fdec->f_qp_avg_aq,
h->i_nal_ref_idc,