/src/x264/encoder/encoder.c
Line | Count | Source |
1 | | /***************************************************************************** |
2 | | * encoder.c: top-level encoder functions |
3 | | ***************************************************************************** |
4 | | * Copyright (C) 2003-2025 x264 project |
5 | | * |
6 | | * Authors: Laurent Aimar <fenrir@via.ecp.fr> |
7 | | * Loren Merritt <lorenm@u.washington.edu> |
8 | | * Fiona Glaser <fiona@x264.com> |
9 | | * |
10 | | * This program is free software; you can redistribute it and/or modify |
11 | | * it under the terms of the GNU General Public License as published by |
12 | | * the Free Software Foundation; either version 2 of the License, or |
13 | | * (at your option) any later version. |
14 | | * |
15 | | * This program is distributed in the hope that it will be useful, |
16 | | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
17 | | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
18 | | * GNU General Public License for more details. |
19 | | * |
20 | | * You should have received a copy of the GNU General Public License |
21 | | * along with this program; if not, write to the Free Software |
22 | | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02111, USA. |
23 | | * |
24 | | * This program is also available under a commercial proprietary license. |
25 | | * For more information, contact us at licensing@x264.com. |
26 | | *****************************************************************************/ |
27 | | |
28 | | #include "common/common.h" |
29 | | |
30 | | #include "set.h" |
31 | | #include "analyse.h" |
32 | | #include "ratecontrol.h" |
33 | | #include "macroblock.h" |
34 | | #include "me.h" |
35 | | #if HAVE_INTEL_DISPATCHER |
36 | | #include "extras/intel_dispatcher.h" |
37 | | #endif |
38 | | |
39 | | //#define DEBUG_MB_TYPE |
40 | | |
41 | 0 | #define bs_write_ue bs_write_ue_big |
42 | | |
43 | | // forward declaration needed for template usage |
44 | | void x264_nal_encode( x264_t *h, uint8_t *dst, x264_nal_t *nal ); |
45 | | void x264_macroblock_cache_load_progressive( x264_t *h, int i_mb_x, int i_mb_y ); |
46 | | |
47 | | static int encoder_frame_end( x264_t *h, x264_t *thread_current, |
48 | | x264_nal_t **pp_nal, int *pi_nal, |
49 | | x264_picture_t *pic_out ); |
50 | | |
51 | | /**************************************************************************** |
52 | | * |
53 | | ******************************* x264 libs ********************************** |
54 | | * |
55 | | ****************************************************************************/ |
56 | | static double calc_psnr( double sqe, double size ) |
57 | 0 | { |
58 | 0 | double mse = sqe / (PIXEL_MAX*PIXEL_MAX * size); |
59 | 0 | if( mse <= 0.0000000001 ) /* Max 100dB */ |
60 | 0 | return 100; |
61 | | |
62 | 0 | return -10.0 * log10( mse ); |
63 | 0 | } |
64 | | |
65 | | static double calc_ssim_db( double ssim ) |
66 | 0 | { |
67 | 0 | double inv_ssim = 1 - ssim; |
68 | 0 | if( inv_ssim <= 0.0000000001 ) /* Max 100dB */ |
69 | 0 | return 100; |
70 | | |
71 | 0 | return -10.0 * log10( inv_ssim ); |
72 | 0 | } |
73 | | |
74 | | static int threadpool_wait_all( x264_t *h ) |
75 | 0 | { |
76 | 0 | for( int i = 0; i < h->param.i_threads; i++ ) |
77 | 0 | if( h->thread[i]->b_thread_active ) |
78 | 0 | { |
79 | 0 | h->thread[i]->b_thread_active = 0; |
80 | 0 | if( (intptr_t)x264_threadpool_wait( h->threadpool, h->thread[i] ) < 0 ) |
81 | 0 | return -1; |
82 | 0 | } |
83 | 0 | return 0; |
84 | 0 | } |
85 | | |
86 | | static void frame_dump( x264_t *h ) |
87 | 0 | { |
88 | 0 | FILE *f = x264_fopen( h->param.psz_dump_yuv, "r+b" ); |
89 | 0 | if( !f ) |
90 | 0 | return; |
91 | | |
92 | | /* Wait for the threads to finish deblocking */ |
93 | 0 | if( h->param.b_sliced_threads ) |
94 | 0 | threadpool_wait_all( h ); |
95 | | |
96 | | /* Write the frame in display order */ |
97 | 0 | int frame_size = FRAME_SIZE( h->param.i_height * h->param.i_width * SIZEOF_PIXEL ); |
98 | 0 | if( !fseek( f, (int64_t)h->fdec->i_frame * frame_size, SEEK_SET ) ) |
99 | 0 | { |
100 | 0 | for( int p = 0; p < (CHROMA444 ? 3 : 1); p++ ) |
101 | 0 | for( int y = 0; y < h->param.i_height; y++ ) |
102 | 0 | fwrite( &h->fdec->plane[p][y*h->fdec->i_stride[p]], SIZEOF_PIXEL, h->param.i_width, f ); |
103 | 0 | if( CHROMA_FORMAT == CHROMA_420 || CHROMA_FORMAT == CHROMA_422 ) |
104 | 0 | { |
105 | 0 | int cw = h->param.i_width>>1; |
106 | 0 | int ch = h->param.i_height>>CHROMA_V_SHIFT; |
107 | 0 | pixel *planeu = x264_malloc( 2 * (cw*ch*SIZEOF_PIXEL + 32) ); |
108 | 0 | if( planeu ) |
109 | 0 | { |
110 | 0 | pixel *planev = planeu + cw*ch + 32/SIZEOF_PIXEL; |
111 | 0 | h->mc.plane_copy_deinterleave( planeu, cw, planev, cw, h->fdec->plane[1], h->fdec->i_stride[1], cw, ch ); |
112 | 0 | fwrite( planeu, 1, cw*ch*SIZEOF_PIXEL, f ); |
113 | 0 | fwrite( planev, 1, cw*ch*SIZEOF_PIXEL, f ); |
114 | 0 | x264_free( planeu ); |
115 | 0 | } |
116 | 0 | } |
117 | 0 | } |
118 | 0 | fclose( f ); |
119 | 0 | } |
120 | | |
121 | | /* Fill "default" values */ |
122 | | static void slice_header_init( x264_t *h, x264_slice_header_t *sh, |
123 | | x264_sps_t *sps, x264_pps_t *pps, |
124 | | int i_idr_pic_id, int i_frame, int i_qp ) |
125 | 0 | { |
126 | 0 | x264_param_t *param = &h->param; |
127 | | |
128 | | /* First we fill all fields */ |
129 | 0 | sh->sps = sps; |
130 | 0 | sh->pps = pps; |
131 | |
|
132 | 0 | sh->i_first_mb = 0; |
133 | 0 | sh->i_last_mb = h->mb.i_mb_count - 1; |
134 | 0 | sh->i_pps_id = pps->i_id; |
135 | |
|
136 | 0 | sh->i_frame_num = i_frame; |
137 | |
|
138 | 0 | sh->b_mbaff = PARAM_INTERLACED; |
139 | 0 | sh->b_field_pic = 0; /* no field support for now */ |
140 | 0 | sh->b_bottom_field = 0; /* not yet used */ |
141 | |
|
142 | 0 | sh->i_idr_pic_id = i_idr_pic_id; |
143 | | |
144 | | /* poc stuff, fixed later */ |
145 | 0 | sh->i_poc = 0; |
146 | 0 | sh->i_delta_poc_bottom = 0; |
147 | 0 | sh->i_delta_poc[0] = 0; |
148 | 0 | sh->i_delta_poc[1] = 0; |
149 | |
|
150 | 0 | sh->i_redundant_pic_cnt = 0; |
151 | |
|
152 | 0 | h->mb.b_direct_auto_write = h->param.analyse.i_direct_mv_pred == X264_DIRECT_PRED_AUTO |
153 | 0 | && h->param.i_bframe |
154 | 0 | && ( h->param.rc.b_stat_write || !h->param.rc.b_stat_read ); |
155 | |
|
156 | 0 | if( !h->mb.b_direct_auto_read && sh->i_type == SLICE_TYPE_B ) |
157 | 0 | { |
158 | 0 | if( h->fref[1][0]->i_poc_l0ref0 == h->fref[0][0]->i_poc ) |
159 | 0 | { |
160 | 0 | if( h->mb.b_direct_auto_write ) |
161 | 0 | sh->b_direct_spatial_mv_pred = ( h->stat.i_direct_score[1] > h->stat.i_direct_score[0] ); |
162 | 0 | else |
163 | 0 | sh->b_direct_spatial_mv_pred = ( param->analyse.i_direct_mv_pred == X264_DIRECT_PRED_SPATIAL ); |
164 | 0 | } |
165 | 0 | else |
166 | 0 | { |
167 | 0 | h->mb.b_direct_auto_write = 0; |
168 | 0 | sh->b_direct_spatial_mv_pred = 1; |
169 | 0 | } |
170 | 0 | } |
171 | | /* else b_direct_spatial_mv_pred was read from the 2pass statsfile */ |
172 | |
|
173 | 0 | sh->b_num_ref_idx_override = 0; |
174 | 0 | sh->i_num_ref_idx_l0_active = 1; |
175 | 0 | sh->i_num_ref_idx_l1_active = 1; |
176 | |
|
177 | 0 | sh->b_ref_pic_list_reordering[0] = h->b_ref_reorder[0]; |
178 | 0 | sh->b_ref_pic_list_reordering[1] = h->b_ref_reorder[1]; |
179 | | |
180 | | /* If the ref list isn't in the default order, construct reordering header */ |
181 | 0 | for( int list = 0; list < 2; list++ ) |
182 | 0 | { |
183 | 0 | if( sh->b_ref_pic_list_reordering[list] ) |
184 | 0 | { |
185 | 0 | int pred_frame_num = i_frame; |
186 | 0 | for( int i = 0; i < h->i_ref[list]; i++ ) |
187 | 0 | { |
188 | 0 | int diff = h->fref[list][i]->i_frame_num - pred_frame_num; |
189 | 0 | sh->ref_pic_list_order[list][i].idc = ( diff > 0 ); |
190 | 0 | sh->ref_pic_list_order[list][i].arg = (abs(diff) - 1) & ((1 << sps->i_log2_max_frame_num) - 1); |
191 | 0 | pred_frame_num = h->fref[list][i]->i_frame_num; |
192 | 0 | } |
193 | 0 | } |
194 | 0 | } |
195 | |
|
196 | 0 | sh->i_cabac_init_idc = param->i_cabac_init_idc; |
197 | |
|
198 | 0 | sh->i_qp = SPEC_QP(i_qp); |
199 | 0 | sh->i_qp_delta = sh->i_qp - pps->i_pic_init_qp; |
200 | 0 | sh->b_sp_for_swidth = 0; |
201 | 0 | sh->i_qs_delta = 0; |
202 | |
|
203 | 0 | int deblock_thresh = i_qp + 2 * X264_MIN(param->i_deblocking_filter_alphac0, param->i_deblocking_filter_beta); |
204 | | /* If effective qp <= 15, deblocking would have no effect anyway */ |
205 | 0 | if( param->b_deblocking_filter && (h->mb.b_variable_qp || 15 < deblock_thresh ) ) |
206 | 0 | sh->i_disable_deblocking_filter_idc = param->b_sliced_threads ? 2 : 0; |
207 | 0 | else |
208 | 0 | sh->i_disable_deblocking_filter_idc = 1; |
209 | 0 | sh->i_alpha_c0_offset = param->i_deblocking_filter_alphac0 * 2; |
210 | 0 | sh->i_beta_offset = param->i_deblocking_filter_beta * 2; |
211 | 0 | } |
212 | | |
213 | | static void slice_header_write( bs_t *s, x264_slice_header_t *sh, int i_nal_ref_idc ) |
214 | 0 | { |
215 | 0 | if( sh->b_mbaff ) |
216 | 0 | { |
217 | 0 | int first_x = sh->i_first_mb % sh->sps->i_mb_width; |
218 | 0 | int first_y = sh->i_first_mb / sh->sps->i_mb_width; |
219 | 0 | assert( (first_y&1) == 0 ); |
220 | 0 | bs_write_ue( s, (2*first_x + sh->sps->i_mb_width*(first_y&~1) + (first_y&1)) >> 1 ); |
221 | 0 | } |
222 | 0 | else |
223 | 0 | bs_write_ue( s, sh->i_first_mb ); |
224 | |
|
225 | 0 | bs_write_ue( s, sh->i_type + 5 ); /* same type things */ |
226 | 0 | bs_write_ue( s, sh->i_pps_id ); |
227 | 0 | bs_write( s, sh->sps->i_log2_max_frame_num, sh->i_frame_num & ((1<<sh->sps->i_log2_max_frame_num)-1) ); |
228 | |
|
229 | 0 | if( !sh->sps->b_frame_mbs_only ) |
230 | 0 | { |
231 | 0 | bs_write1( s, sh->b_field_pic ); |
232 | 0 | if( sh->b_field_pic ) |
233 | 0 | bs_write1( s, sh->b_bottom_field ); |
234 | 0 | } |
235 | |
|
236 | 0 | if( sh->i_idr_pic_id >= 0 ) /* NAL IDR */ |
237 | 0 | bs_write_ue( s, sh->i_idr_pic_id ); |
238 | |
|
239 | 0 | if( sh->sps->i_poc_type == 0 ) |
240 | 0 | { |
241 | 0 | bs_write( s, sh->sps->i_log2_max_poc_lsb, sh->i_poc & ((1<<sh->sps->i_log2_max_poc_lsb)-1) ); |
242 | 0 | if( sh->pps->b_pic_order && !sh->b_field_pic ) |
243 | 0 | bs_write_se( s, sh->i_delta_poc_bottom ); |
244 | 0 | } |
245 | |
|
246 | 0 | if( sh->pps->b_redundant_pic_cnt ) |
247 | 0 | bs_write_ue( s, sh->i_redundant_pic_cnt ); |
248 | |
|
249 | 0 | if( sh->i_type == SLICE_TYPE_B ) |
250 | 0 | bs_write1( s, sh->b_direct_spatial_mv_pred ); |
251 | |
|
252 | 0 | if( sh->i_type == SLICE_TYPE_P || sh->i_type == SLICE_TYPE_B ) |
253 | 0 | { |
254 | 0 | bs_write1( s, sh->b_num_ref_idx_override ); |
255 | 0 | if( sh->b_num_ref_idx_override ) |
256 | 0 | { |
257 | 0 | bs_write_ue( s, sh->i_num_ref_idx_l0_active - 1 ); |
258 | 0 | if( sh->i_type == SLICE_TYPE_B ) |
259 | 0 | bs_write_ue( s, sh->i_num_ref_idx_l1_active - 1 ); |
260 | 0 | } |
261 | 0 | } |
262 | | |
263 | | /* ref pic list reordering */ |
264 | 0 | if( sh->i_type != SLICE_TYPE_I ) |
265 | 0 | { |
266 | 0 | bs_write1( s, sh->b_ref_pic_list_reordering[0] ); |
267 | 0 | if( sh->b_ref_pic_list_reordering[0] ) |
268 | 0 | { |
269 | 0 | for( int i = 0; i < sh->i_num_ref_idx_l0_active; i++ ) |
270 | 0 | { |
271 | 0 | bs_write_ue( s, sh->ref_pic_list_order[0][i].idc ); |
272 | 0 | bs_write_ue( s, sh->ref_pic_list_order[0][i].arg ); |
273 | 0 | } |
274 | 0 | bs_write_ue( s, 3 ); |
275 | 0 | } |
276 | 0 | } |
277 | 0 | if( sh->i_type == SLICE_TYPE_B ) |
278 | 0 | { |
279 | 0 | bs_write1( s, sh->b_ref_pic_list_reordering[1] ); |
280 | 0 | if( sh->b_ref_pic_list_reordering[1] ) |
281 | 0 | { |
282 | 0 | for( int i = 0; i < sh->i_num_ref_idx_l1_active; i++ ) |
283 | 0 | { |
284 | 0 | bs_write_ue( s, sh->ref_pic_list_order[1][i].idc ); |
285 | 0 | bs_write_ue( s, sh->ref_pic_list_order[1][i].arg ); |
286 | 0 | } |
287 | 0 | bs_write_ue( s, 3 ); |
288 | 0 | } |
289 | 0 | } |
290 | |
|
291 | 0 | sh->b_weighted_pred = 0; |
292 | 0 | if( sh->pps->b_weighted_pred && sh->i_type == SLICE_TYPE_P ) |
293 | 0 | { |
294 | 0 | sh->b_weighted_pred = sh->weight[0][0].weightfn || sh->weight[0][1].weightfn || sh->weight[0][2].weightfn; |
295 | | /* pred_weight_table() */ |
296 | 0 | bs_write_ue( s, sh->weight[0][0].i_denom ); /* luma_log2_weight_denom */ |
297 | 0 | if( sh->sps->i_chroma_format_idc ) |
298 | 0 | bs_write_ue( s, sh->weight[0][1].i_denom ); /* chroma_log2_weight_denom */ |
299 | 0 | for( int i = 0; i < sh->i_num_ref_idx_l0_active; i++ ) |
300 | 0 | { |
301 | 0 | int luma_weight_l0_flag = !!sh->weight[i][0].weightfn; |
302 | 0 | bs_write1( s, luma_weight_l0_flag ); |
303 | 0 | if( luma_weight_l0_flag ) |
304 | 0 | { |
305 | 0 | bs_write_se( s, sh->weight[i][0].i_scale ); |
306 | 0 | bs_write_se( s, sh->weight[i][0].i_offset ); |
307 | 0 | } |
308 | 0 | if( sh->sps->i_chroma_format_idc ) |
309 | 0 | { |
310 | 0 | int chroma_weight_l0_flag = sh->weight[i][1].weightfn || sh->weight[i][2].weightfn; |
311 | 0 | bs_write1( s, chroma_weight_l0_flag ); |
312 | 0 | if( chroma_weight_l0_flag ) |
313 | 0 | { |
314 | 0 | for( int j = 1; j < 3; j++ ) |
315 | 0 | { |
316 | 0 | bs_write_se( s, sh->weight[i][j].i_scale ); |
317 | 0 | bs_write_se( s, sh->weight[i][j].i_offset ); |
318 | 0 | } |
319 | 0 | } |
320 | 0 | } |
321 | 0 | } |
322 | 0 | } |
323 | 0 | else if( sh->pps->b_weighted_bipred == 1 && sh->i_type == SLICE_TYPE_B ) |
324 | 0 | { |
325 | | /* TODO */ |
326 | 0 | } |
327 | |
|
328 | 0 | if( i_nal_ref_idc != 0 ) |
329 | 0 | { |
330 | 0 | if( sh->i_idr_pic_id >= 0 ) |
331 | 0 | { |
332 | 0 | bs_write1( s, 0 ); /* no output of prior pics flag */ |
333 | 0 | bs_write1( s, 0 ); /* long term reference flag */ |
334 | 0 | } |
335 | 0 | else |
336 | 0 | { |
337 | 0 | bs_write1( s, sh->i_mmco_command_count > 0 ); /* adaptive_ref_pic_marking_mode_flag */ |
338 | 0 | if( sh->i_mmco_command_count > 0 ) |
339 | 0 | { |
340 | 0 | for( int i = 0; i < sh->i_mmco_command_count; i++ ) |
341 | 0 | { |
342 | 0 | bs_write_ue( s, 1 ); /* mark short term ref as unused */ |
343 | 0 | bs_write_ue( s, sh->mmco[i].i_difference_of_pic_nums - 1 ); |
344 | 0 | } |
345 | 0 | bs_write_ue( s, 0 ); /* end command list */ |
346 | 0 | } |
347 | 0 | } |
348 | 0 | } |
349 | |
|
350 | 0 | if( sh->pps->b_cabac && sh->i_type != SLICE_TYPE_I ) |
351 | 0 | bs_write_ue( s, sh->i_cabac_init_idc ); |
352 | |
|
353 | 0 | bs_write_se( s, sh->i_qp_delta ); /* slice qp delta */ |
354 | |
|
355 | 0 | if( sh->pps->b_deblocking_filter_control ) |
356 | 0 | { |
357 | 0 | bs_write_ue( s, sh->i_disable_deblocking_filter_idc ); |
358 | 0 | if( sh->i_disable_deblocking_filter_idc != 1 ) |
359 | 0 | { |
360 | 0 | bs_write_se( s, sh->i_alpha_c0_offset >> 1 ); |
361 | 0 | bs_write_se( s, sh->i_beta_offset >> 1 ); |
362 | 0 | } |
363 | 0 | } |
364 | 0 | } |
365 | | |
366 | | /* If we are within a reasonable distance of the end of the memory allocated for the bitstream, */ |
367 | | /* reallocate, adding an arbitrary amount of space. */ |
368 | | static int bitstream_check_buffer_internal( x264_t *h, int size, int b_cabac, int i_nal ) |
369 | 0 | { |
370 | 0 | if( (b_cabac && (h->cabac.p_end - h->cabac.p < size)) || |
371 | 0 | (h->out.bs.p_end - h->out.bs.p < size) ) |
372 | 0 | { |
373 | 0 | if( size > INT_MAX - h->out.i_bitstream ) |
374 | 0 | return -1; |
375 | 0 | int buf_size = h->out.i_bitstream + size; |
376 | 0 | uint8_t *buf = x264_malloc( buf_size ); |
377 | 0 | if( !buf ) |
378 | 0 | return -1; |
379 | 0 | int aligned_size = h->out.i_bitstream & ~15; |
380 | 0 | h->mc.memcpy_aligned( buf, h->out.p_bitstream, aligned_size ); |
381 | 0 | memcpy( buf + aligned_size, h->out.p_bitstream + aligned_size, h->out.i_bitstream - aligned_size ); |
382 | |
|
383 | 0 | intptr_t delta = buf - h->out.p_bitstream; |
384 | |
|
385 | 0 | h->out.bs.p_start += delta; |
386 | 0 | h->out.bs.p += delta; |
387 | 0 | h->out.bs.p_end = buf + buf_size; |
388 | |
|
389 | 0 | h->cabac.p_start += delta; |
390 | 0 | h->cabac.p += delta; |
391 | 0 | h->cabac.p_end = buf + buf_size; |
392 | |
|
393 | 0 | for( int i = 0; i <= i_nal; i++ ) |
394 | 0 | h->out.nal[i].p_payload += delta; |
395 | |
|
396 | 0 | x264_free( h->out.p_bitstream ); |
397 | 0 | h->out.p_bitstream = buf; |
398 | 0 | h->out.i_bitstream = buf_size; |
399 | 0 | } |
400 | 0 | return 0; |
401 | 0 | } |
402 | | |
403 | | static int bitstream_check_buffer( x264_t *h ) |
404 | 0 | { |
405 | 0 | int max_row_size = (2500 << SLICE_MBAFF) * h->mb.i_mb_width; |
406 | 0 | return bitstream_check_buffer_internal( h, max_row_size, h->param.b_cabac, h->out.i_nal ); |
407 | 0 | } |
408 | | |
409 | | static int bitstream_check_buffer_filler( x264_t *h, int filler ) |
410 | 0 | { |
411 | 0 | filler += 32; // add padding for safety |
412 | 0 | return bitstream_check_buffer_internal( h, filler, 0, -1 ); |
413 | 0 | } |
414 | | |
415 | | /**************************************************************************** |
416 | | * |
417 | | **************************************************************************** |
418 | | ****************************** External API********************************* |
419 | | **************************************************************************** |
420 | | * |
421 | | ****************************************************************************/ |
422 | | |
423 | | static int validate_parameters( x264_t *h, int b_open ) |
424 | 0 | { |
425 | 0 | if( !h->param.pf_log ) |
426 | 0 | { |
427 | 0 | x264_log_internal( X264_LOG_ERROR, "pf_log not set! did you forget to call x264_param_default?\n" ); |
428 | 0 | return -1; |
429 | 0 | } |
430 | | |
431 | | #if HAVE_MMX |
432 | | if( b_open ) |
433 | | { |
434 | | uint32_t cpuflags = x264_cpu_detect(); |
435 | | int fail = 0; |
436 | | #ifdef __SSE__ |
437 | | if( !(cpuflags & X264_CPU_SSE) ) |
438 | | { |
439 | | x264_log( h, X264_LOG_ERROR, "your cpu does not support SSE1, but x264 was compiled with asm\n"); |
440 | | fail = 1; |
441 | | } |
442 | | #else |
443 | | if( !(cpuflags & X264_CPU_MMX2) ) |
444 | | { |
445 | | x264_log( h, X264_LOG_ERROR, "your cpu does not support MMXEXT, but x264 was compiled with asm\n"); |
446 | | fail = 1; |
447 | | } |
448 | | #endif |
449 | | if( fail ) |
450 | | { |
451 | | x264_log( h, X264_LOG_ERROR, "to run x264, recompile without asm (configure --disable-asm)\n"); |
452 | | return -1; |
453 | | } |
454 | | } |
455 | | #endif |
456 | | |
457 | 0 | #if HAVE_INTERLACED |
458 | 0 | h->param.b_interlaced = !!PARAM_INTERLACED; |
459 | | #else |
460 | | if( h->param.b_interlaced ) |
461 | | { |
462 | | x264_log( h, X264_LOG_ERROR, "not compiled with interlaced support\n" ); |
463 | | return -1; |
464 | | } |
465 | | #endif |
466 | |
|
467 | 0 | #define MAX_RESOLUTION 16384 |
468 | 0 | if( h->param.i_width <= 0 || h->param.i_height <= 0 || |
469 | 0 | h->param.i_width > MAX_RESOLUTION || h->param.i_height > MAX_RESOLUTION ) |
470 | 0 | { |
471 | 0 | x264_log( h, X264_LOG_ERROR, "invalid width x height (%dx%d)\n", |
472 | 0 | h->param.i_width, h->param.i_height ); |
473 | 0 | return -1; |
474 | 0 | } |
475 | | |
476 | 0 | int i_csp = h->param.i_csp & X264_CSP_MASK; |
477 | | #if X264_CHROMA_FORMAT |
478 | | if( CHROMA_FORMAT != CHROMA_400 && i_csp == X264_CSP_I400 ) |
479 | | { |
480 | | x264_log( h, X264_LOG_ERROR, "not compiled with 4:0:0 support\n" ); |
481 | | return -1; |
482 | | } |
483 | | else if( CHROMA_FORMAT != CHROMA_420 && i_csp >= X264_CSP_I420 && i_csp < X264_CSP_I422 ) |
484 | | { |
485 | | x264_log( h, X264_LOG_ERROR, "not compiled with 4:2:0 support\n" ); |
486 | | return -1; |
487 | | } |
488 | | else if( CHROMA_FORMAT != CHROMA_422 && i_csp >= X264_CSP_I422 && i_csp < X264_CSP_I444 ) |
489 | | { |
490 | | x264_log( h, X264_LOG_ERROR, "not compiled with 4:2:2 support\n" ); |
491 | | return -1; |
492 | | } |
493 | | else if( CHROMA_FORMAT != CHROMA_444 && i_csp >= X264_CSP_I444 && i_csp <= X264_CSP_RGB ) |
494 | | { |
495 | | x264_log( h, X264_LOG_ERROR, "not compiled with 4:4:4 support\n" ); |
496 | | return -1; |
497 | | } |
498 | | #endif |
499 | 0 | if( i_csp <= X264_CSP_NONE || i_csp >= X264_CSP_MAX ) |
500 | 0 | { |
501 | 0 | x264_log( h, X264_LOG_ERROR, "invalid CSP (only I400/I420/YV12/NV12/NV21/I422/YV16/NV16/YUYV/UYVY/" |
502 | 0 | "I444/YV24/BGR/BGRA/RGB supported)\n" ); |
503 | 0 | return -1; |
504 | 0 | } |
505 | | |
506 | 0 | int w_mod = 1; |
507 | 0 | int h_mod = 1 << (PARAM_INTERLACED || h->param.b_fake_interlaced); |
508 | 0 | if( i_csp == X264_CSP_I400 ) |
509 | 0 | { |
510 | 0 | h->param.analyse.i_chroma_qp_offset = 0; |
511 | 0 | h->param.analyse.b_chroma_me = 0; |
512 | 0 | h->param.vui.i_colmatrix = 2; /* undefined */ |
513 | 0 | } |
514 | 0 | else if( i_csp < X264_CSP_I444 ) |
515 | 0 | { |
516 | 0 | w_mod = 2; |
517 | 0 | if( i_csp < X264_CSP_I422 ) |
518 | 0 | h_mod *= 2; |
519 | 0 | } |
520 | |
|
521 | 0 | if( h->param.i_width % w_mod ) |
522 | 0 | { |
523 | 0 | x264_log( h, X264_LOG_ERROR, "width not divisible by %d (%dx%d)\n", |
524 | 0 | w_mod, h->param.i_width, h->param.i_height ); |
525 | 0 | return -1; |
526 | 0 | } |
527 | 0 | if( h->param.i_height % h_mod ) |
528 | 0 | { |
529 | 0 | x264_log( h, X264_LOG_ERROR, "height not divisible by %d (%dx%d)\n", |
530 | 0 | h_mod, h->param.i_width, h->param.i_height ); |
531 | 0 | return -1; |
532 | 0 | } |
533 | | |
534 | 0 | if( h->param.crop_rect.i_left < 0 || h->param.crop_rect.i_left >= h->param.i_width || |
535 | 0 | h->param.crop_rect.i_right < 0 || h->param.crop_rect.i_right >= h->param.i_width || |
536 | 0 | h->param.crop_rect.i_top < 0 || h->param.crop_rect.i_top >= h->param.i_height || |
537 | 0 | h->param.crop_rect.i_bottom < 0 || h->param.crop_rect.i_bottom >= h->param.i_height || |
538 | 0 | h->param.crop_rect.i_left + h->param.crop_rect.i_right >= h->param.i_width || |
539 | 0 | h->param.crop_rect.i_top + h->param.crop_rect.i_bottom >= h->param.i_height ) |
540 | 0 | { |
541 | 0 | x264_log( h, X264_LOG_ERROR, "invalid crop-rect %d,%d,%d,%d\n", h->param.crop_rect.i_left, |
542 | 0 | h->param.crop_rect.i_top, h->param.crop_rect.i_right, h->param.crop_rect.i_bottom ); |
543 | 0 | return -1; |
544 | 0 | } |
545 | 0 | if( h->param.crop_rect.i_left % w_mod || h->param.crop_rect.i_right % w_mod || |
546 | 0 | h->param.crop_rect.i_top % h_mod || h->param.crop_rect.i_bottom % h_mod ) |
547 | 0 | { |
548 | 0 | x264_log( h, X264_LOG_ERROR, "crop-rect %d,%d,%d,%d not divisible by %dx%d\n", h->param.crop_rect.i_left, |
549 | 0 | h->param.crop_rect.i_top, h->param.crop_rect.i_right, h->param.crop_rect.i_bottom, w_mod, h_mod ); |
550 | 0 | return -1; |
551 | 0 | } |
552 | | |
553 | 0 | if( h->param.vui.i_sar_width <= 0 || h->param.vui.i_sar_height <= 0 ) |
554 | 0 | { |
555 | 0 | h->param.vui.i_sar_width = 0; |
556 | 0 | h->param.vui.i_sar_height = 0; |
557 | 0 | } |
558 | |
|
559 | 0 | if( h->param.i_threads == X264_THREADS_AUTO ) |
560 | 0 | { |
561 | 0 | h->param.i_threads = x264_cpu_num_processors() * (h->param.b_sliced_threads?2:3)/2; |
562 | | /* Avoid too many threads as they don't improve performance and |
563 | | * complicate VBV. Capped at an arbitrary 2 rows per thread. */ |
564 | 0 | int max_threads = X264_MAX( 1, (h->param.i_height+15)/16 / 2 ); |
565 | 0 | h->param.i_threads = X264_MIN( h->param.i_threads, max_threads ); |
566 | 0 | } |
567 | 0 | int max_sliced_threads = X264_MAX( 1, (h->param.i_height+15)/16 / 4 ); |
568 | 0 | if( h->param.i_threads > 1 ) |
569 | 0 | { |
570 | | #if !HAVE_THREAD |
571 | | x264_log( h, X264_LOG_WARNING, "not compiled with thread support!\n"); |
572 | | h->param.i_threads = 1; |
573 | | #endif |
574 | | /* Avoid absurdly small thread slices as they can reduce performance |
575 | | * and VBV compliance. Capped at an arbitrary 4 rows per thread. */ |
576 | 0 | if( h->param.b_sliced_threads ) |
577 | 0 | h->param.i_threads = X264_MIN( h->param.i_threads, max_sliced_threads ); |
578 | 0 | } |
579 | 0 | h->param.i_threads = x264_clip3( h->param.i_threads, 1, X264_THREAD_MAX ); |
580 | 0 | if( h->param.i_threads == 1 ) |
581 | 0 | { |
582 | 0 | h->param.b_sliced_threads = 0; |
583 | 0 | h->param.i_lookahead_threads = 1; |
584 | 0 | } |
585 | 0 | h->i_thread_frames = h->param.b_sliced_threads ? 1 : h->param.i_threads; |
586 | 0 | if( h->i_thread_frames > 1 ) |
587 | 0 | h->param.nalu_process = NULL; |
588 | |
|
589 | 0 | if( h->param.b_opencl ) |
590 | 0 | { |
591 | 0 | #if !HAVE_OPENCL |
592 | 0 | x264_log( h, X264_LOG_WARNING, "OpenCL: not compiled with OpenCL support, disabling\n" ); |
593 | 0 | h->param.b_opencl = 0; |
594 | | #elif BIT_DEPTH > 8 |
595 | | x264_log( h, X264_LOG_WARNING, "OpenCL lookahead does not support high bit depth, disabling opencl\n" ); |
596 | | h->param.b_opencl = 0; |
597 | | #else |
598 | | if( h->param.i_width < 32 || h->param.i_height < 32 ) |
599 | | { |
600 | | x264_log( h, X264_LOG_WARNING, "OpenCL: frame size is too small, disabling opencl\n" ); |
601 | | h->param.b_opencl = 0; |
602 | | } |
603 | | #endif |
604 | 0 | if( h->param.opencl_device_id && h->param.i_opencl_device ) |
605 | 0 | { |
606 | 0 | x264_log( h, X264_LOG_WARNING, "OpenCL: device id and device skip count configured; dropping skip\n" ); |
607 | 0 | h->param.i_opencl_device = 0; |
608 | 0 | } |
609 | 0 | } |
610 | |
|
611 | 0 | h->param.i_keyint_max = x264_clip3( h->param.i_keyint_max, 1, X264_KEYINT_MAX_INFINITE ); |
612 | 0 | if( h->param.i_keyint_max == 1 ) |
613 | 0 | { |
614 | 0 | h->param.b_intra_refresh = 0; |
615 | 0 | h->param.analyse.i_weighted_pred = 0; |
616 | 0 | h->param.i_frame_reference = 1; |
617 | 0 | h->param.i_dpb_size = 1; |
618 | 0 | } |
619 | |
|
620 | 0 | if( h->param.i_frame_packing < -1 || h->param.i_frame_packing > 7 ) |
621 | 0 | { |
622 | 0 | x264_log( h, X264_LOG_WARNING, "ignoring unknown frame packing value\n" ); |
623 | 0 | h->param.i_frame_packing = -1; |
624 | 0 | } |
625 | 0 | if( h->param.i_frame_packing == 7 && |
626 | 0 | ((h->param.i_width - h->param.crop_rect.i_left - h->param.crop_rect.i_right) % 3 || |
627 | 0 | (h->param.i_height - h->param.crop_rect.i_top - h->param.crop_rect.i_bottom) % 3) ) |
628 | 0 | { |
629 | 0 | x264_log( h, X264_LOG_ERROR, "cropped resolution %dx%d not compatible with tile format frame packing\n", |
630 | 0 | h->param.i_width - h->param.crop_rect.i_left - h->param.crop_rect.i_right, |
631 | 0 | h->param.i_height - h->param.crop_rect.i_top - h->param.crop_rect.i_bottom ); |
632 | 0 | return -1; |
633 | 0 | } |
634 | | |
635 | 0 | if( h->param.mastering_display.b_mastering_display ) |
636 | 0 | { |
637 | 0 | if( h->param.mastering_display.i_green_x > UINT16_MAX || h->param.mastering_display.i_green_x < 0 || |
638 | 0 | h->param.mastering_display.i_green_y > UINT16_MAX || h->param.mastering_display.i_green_y < 0 || |
639 | 0 | h->param.mastering_display.i_blue_x > UINT16_MAX || h->param.mastering_display.i_blue_x < 0 || |
640 | 0 | h->param.mastering_display.i_blue_y > UINT16_MAX || h->param.mastering_display.i_blue_y < 0 || |
641 | 0 | h->param.mastering_display.i_red_x > UINT16_MAX || h->param.mastering_display.i_red_x < 0 || |
642 | 0 | h->param.mastering_display.i_red_y > UINT16_MAX || h->param.mastering_display.i_red_y < 0 || |
643 | 0 | h->param.mastering_display.i_white_x > UINT16_MAX || h->param.mastering_display.i_white_x < 0 || |
644 | 0 | h->param.mastering_display.i_white_y > UINT16_MAX || h->param.mastering_display.i_white_y < 0 ) |
645 | 0 | { |
646 | 0 | x264_log( h, X264_LOG_ERROR, "mastering display xy coordinates out of range [0,%u]\n", UINT16_MAX ); |
647 | 0 | return -1; |
648 | 0 | } |
649 | 0 | if( h->param.mastering_display.i_display_max > UINT32_MAX || h->param.mastering_display.i_display_max < 0 || |
650 | 0 | h->param.mastering_display.i_display_min > UINT32_MAX || h->param.mastering_display.i_display_min < 0 ) |
651 | 0 | { |
652 | 0 | x264_log( h, X264_LOG_ERROR, "mastering display brightness out of range [0,%u]\n", UINT32_MAX ); |
653 | 0 | return -1; |
654 | 0 | } |
655 | 0 | if( h->param.mastering_display.i_display_min == 50000 && h->param.mastering_display.i_display_max == 50000 ) |
656 | 0 | { |
657 | 0 | x264_log( h, X264_LOG_ERROR, "mastering display min and max brightness cannot both be 50000\n" ); |
658 | 0 | return -1; |
659 | 0 | } |
660 | 0 | } |
661 | | |
662 | 0 | if( h->param.content_light_level.b_cll && |
663 | 0 | (h->param.content_light_level.i_max_cll > UINT16_MAX || h->param.content_light_level.i_max_cll < 0 || |
664 | 0 | h->param.content_light_level.i_max_fall > UINT16_MAX || h->param.content_light_level.i_max_fall < 0) ) |
665 | 0 | { |
666 | 0 | x264_log( h, X264_LOG_ERROR, "content light levels out of range [0,%u]\n", UINT16_MAX ); |
667 | 0 | return -1; |
668 | 0 | } |
669 | | |
670 | | /* Detect default ffmpeg settings and terminate with an error. */ |
671 | 0 | if( b_open ) |
672 | 0 | { |
673 | 0 | int score = 0; |
674 | 0 | score += h->param.analyse.i_me_range == 0; |
675 | 0 | score += h->param.rc.i_qp_step == 3; |
676 | 0 | score += h->param.i_keyint_max == 12; |
677 | 0 | score += h->param.rc.i_qp_min == 2; |
678 | 0 | score += h->param.rc.i_qp_max == 31; |
679 | 0 | score += h->param.rc.f_qcompress == 0.5; |
680 | 0 | score += fabs(h->param.rc.f_ip_factor - 1.25) < 0.01; |
681 | 0 | score += fabs(h->param.rc.f_pb_factor - 1.25) < 0.01; |
682 | 0 | score += h->param.analyse.inter == 0 && h->param.analyse.i_subpel_refine == 8; |
683 | 0 | if( score >= 5 ) |
684 | 0 | { |
685 | 0 | x264_log( h, X264_LOG_ERROR, "broken ffmpeg default settings detected\n" ); |
686 | 0 | x264_log( h, X264_LOG_ERROR, "use an encoding preset (e.g. -vpre medium)\n" ); |
687 | 0 | x264_log( h, X264_LOG_ERROR, "preset usage: -vpre <speed> -vpre <profile>\n" ); |
688 | 0 | x264_log( h, X264_LOG_ERROR, "speed presets are listed in x264 --help\n" ); |
689 | 0 | x264_log( h, X264_LOG_ERROR, "profile is optional; x264 defaults to high\n" ); |
690 | 0 | return -1; |
691 | 0 | } |
692 | 0 | } |
693 | | |
694 | 0 | if( h->param.rc.i_rc_method < 0 || h->param.rc.i_rc_method > 2 ) |
695 | 0 | { |
696 | 0 | x264_log( h, X264_LOG_ERROR, "no ratecontrol method specified\n" ); |
697 | 0 | return -1; |
698 | 0 | } |
699 | | |
700 | 0 | if( PARAM_INTERLACED ) |
701 | 0 | h->param.b_pic_struct = 1; |
702 | |
|
703 | 0 | if( h->param.i_avcintra_class ) |
704 | 0 | { |
705 | 0 | if( BIT_DEPTH != 10 ) |
706 | 0 | { |
707 | 0 | x264_log( h, X264_LOG_ERROR, "%2d-bit AVC-Intra is not widely compatible\n", BIT_DEPTH ); |
708 | 0 | x264_log( h, X264_LOG_ERROR, "10-bit x264 is required to encode AVC-Intra\n" ); |
709 | 0 | return -1; |
710 | 0 | } |
711 | | |
712 | 0 | int type = h->param.i_avcintra_class == 480 ? 4 : |
713 | 0 | h->param.i_avcintra_class == 300 ? 3 : |
714 | 0 | h->param.i_avcintra_class == 200 ? 2 : |
715 | 0 | h->param.i_avcintra_class == 100 ? 1 : |
716 | 0 | h->param.i_avcintra_class == 50 ? 0 : -1; |
717 | 0 | if( type < 0 ) |
718 | 0 | { |
719 | 0 | x264_log( h, X264_LOG_ERROR, "Invalid AVC-Intra class\n" ); |
720 | 0 | return -1; |
721 | 0 | } |
722 | 0 | else if( type > 2 && h->param.i_avcintra_flavor != X264_AVCINTRA_FLAVOR_SONY ) |
723 | 0 | { |
724 | 0 | x264_log( h, X264_LOG_ERROR, "AVC-Intra %d only supported by Sony XAVC flavor\n", h->param.i_avcintra_class ); |
725 | 0 | return -1; |
726 | 0 | } |
727 | | |
728 | | /* [50/100/200/300/480][res][fps] */ |
729 | 0 | static const struct |
730 | 0 | { |
731 | 0 | uint16_t fps_num; |
732 | 0 | uint16_t fps_den; |
733 | 0 | uint8_t interlaced; |
734 | 0 | uint16_t frame_size; |
735 | 0 | const uint8_t *cqm_4iy; |
736 | 0 | const uint8_t *cqm_4ic; |
737 | 0 | const uint8_t *cqm_8iy; |
738 | 0 | } avcintra_lut[5][2][7] = |
739 | 0 | { |
740 | 0 | {{{ 60000, 1001, 0, 912, x264_cqm_jvt4i, x264_cqm_avci50_4ic, x264_cqm_avci50_p_8iy }, |
741 | 0 | { 50, 1, 0, 1100, x264_cqm_jvt4i, x264_cqm_avci50_4ic, x264_cqm_avci50_p_8iy }, |
742 | 0 | { 30000, 1001, 0, 912, x264_cqm_jvt4i, x264_cqm_avci50_4ic, x264_cqm_avci50_p_8iy }, |
743 | 0 | { 25, 1, 0, 1100, x264_cqm_jvt4i, x264_cqm_avci50_4ic, x264_cqm_avci50_p_8iy }, |
744 | 0 | { 24000, 1001, 0, 912, x264_cqm_jvt4i, x264_cqm_avci50_4ic, x264_cqm_avci50_p_8iy }}, |
745 | 0 | {{ 30000, 1001, 1, 1820, x264_cqm_jvt4i, x264_cqm_avci50_4ic, x264_cqm_avci50_1080i_8iy }, |
746 | 0 | { 25, 1, 1, 2196, x264_cqm_jvt4i, x264_cqm_avci50_4ic, x264_cqm_avci50_1080i_8iy }, |
747 | 0 | { 60000, 1001, 0, 1820, x264_cqm_jvt4i, x264_cqm_avci50_4ic, x264_cqm_avci50_p_8iy }, |
748 | 0 | { 30000, 1001, 0, 1820, x264_cqm_jvt4i, x264_cqm_avci50_4ic, x264_cqm_avci50_p_8iy }, |
749 | 0 | { 50, 1, 0, 2196, x264_cqm_jvt4i, x264_cqm_avci50_4ic, x264_cqm_avci50_p_8iy }, |
750 | 0 | { 25, 1, 0, 2196, x264_cqm_jvt4i, x264_cqm_avci50_4ic, x264_cqm_avci50_p_8iy }, |
751 | 0 | { 24000, 1001, 0, 1820, x264_cqm_jvt4i, x264_cqm_avci50_4ic, x264_cqm_avci50_p_8iy }}}, |
752 | 0 | {{{ 60000, 1001, 0, 1848, x264_cqm_jvt4i, x264_cqm_avci100_720p_4ic, x264_cqm_avci100_720p_8iy }, |
753 | 0 | { 50, 1, 0, 2224, x264_cqm_jvt4i, x264_cqm_avci100_720p_4ic, x264_cqm_avci100_720p_8iy }, |
754 | 0 | { 30000, 1001, 0, 1848, x264_cqm_jvt4i, x264_cqm_avci100_720p_4ic, x264_cqm_avci100_720p_8iy }, |
755 | 0 | { 25, 1, 0, 2224, x264_cqm_jvt4i, x264_cqm_avci100_720p_4ic, x264_cqm_avci100_720p_8iy }, |
756 | 0 | { 24000, 1001, 0, 1848, x264_cqm_jvt4i, x264_cqm_avci100_720p_4ic, x264_cqm_avci100_720p_8iy }}, |
757 | 0 | {{ 30000, 1001, 1, 3692, x264_cqm_jvt4i, x264_cqm_avci100_1080_4ic, x264_cqm_avci100_1080i_8iy }, |
758 | 0 | { 25, 1, 1, 4444, x264_cqm_jvt4i, x264_cqm_avci100_1080_4ic, x264_cqm_avci100_1080i_8iy }, |
759 | 0 | { 60000, 1001, 0, 3692, x264_cqm_jvt4i, x264_cqm_avci100_1080_4ic, x264_cqm_avci100_1080p_8iy }, |
760 | 0 | { 30000, 1001, 0, 3692, x264_cqm_jvt4i, x264_cqm_avci100_1080_4ic, x264_cqm_avci100_1080p_8iy }, |
761 | 0 | { 50, 1, 0, 4444, x264_cqm_jvt4i, x264_cqm_avci100_1080_4ic, x264_cqm_avci100_1080p_8iy }, |
762 | 0 | { 25, 1, 0, 4444, x264_cqm_jvt4i, x264_cqm_avci100_1080_4ic, x264_cqm_avci100_1080p_8iy }, |
763 | 0 | { 24000, 1001, 0, 3692, x264_cqm_jvt4i, x264_cqm_avci100_1080_4ic, x264_cqm_avci100_1080p_8iy }}}, |
764 | 0 | {{{ 60000, 1001, 0, 3724, x264_cqm_jvt4i, x264_cqm_avci100_720p_4ic, x264_cqm_avci100_720p_8iy }, |
765 | 0 | { 50, 1, 0, 4472, x264_cqm_jvt4i, x264_cqm_avci100_720p_4ic, x264_cqm_avci100_720p_8iy }}, |
766 | 0 | {{ 30000, 1001, 1, 7444, x264_cqm_jvt4i, x264_cqm_avci100_1080_4ic, x264_cqm_avci100_1080i_8iy }, |
767 | 0 | { 25, 1, 1, 8940, x264_cqm_jvt4i, x264_cqm_avci100_1080_4ic, x264_cqm_avci100_1080i_8iy }, |
768 | 0 | { 60000, 1001, 0, 7444, x264_cqm_jvt4i, x264_cqm_avci100_1080_4ic, x264_cqm_avci100_1080p_8iy }, |
769 | 0 | { 30000, 1001, 0, 7444, x264_cqm_jvt4i, x264_cqm_avci100_1080_4ic, x264_cqm_avci100_1080p_8iy }, |
770 | 0 | { 50, 1, 0, 8940, x264_cqm_jvt4i, x264_cqm_avci100_1080_4ic, x264_cqm_avci100_1080p_8iy }, |
771 | 0 | { 25, 1, 0, 8940, x264_cqm_jvt4i, x264_cqm_avci100_1080_4ic, x264_cqm_avci100_1080p_8iy }, |
772 | 0 | { 24000, 1001, 0, 7444, x264_cqm_jvt4i, x264_cqm_avci100_1080_4ic, x264_cqm_avci100_1080p_8iy }}}, |
773 | 0 | {{{ 60000, 1001, 0, 9844, x264_cqm_avci300_2160p_4iy, x264_cqm_avci300_2160p_4ic, x264_cqm_avci300_2160p_8iy }, |
774 | 0 | { 50, 1, 0, 9844, x264_cqm_avci300_2160p_4iy, x264_cqm_avci300_2160p_4ic, x264_cqm_avci300_2160p_8iy }, |
775 | 0 | { 30000, 1001, 0, 9844, x264_cqm_avci300_2160p_4iy, x264_cqm_avci300_2160p_4ic, x264_cqm_avci300_2160p_8iy }, |
776 | 0 | { 25, 1, 0, 9844, x264_cqm_avci300_2160p_4iy, x264_cqm_avci300_2160p_4ic, x264_cqm_avci300_2160p_8iy }, |
777 | 0 | { 24000, 1001, 0, 9844, x264_cqm_avci300_2160p_4iy, x264_cqm_avci300_2160p_4ic, x264_cqm_avci300_2160p_8iy }}}, |
778 | 0 | {{{ 60000, 1001, 0, 15700, x264_cqm_avci300_2160p_4iy, x264_cqm_avci300_2160p_4ic, x264_cqm_avci300_2160p_8iy }, |
779 | 0 | { 50, 1, 0, 15700, x264_cqm_avci300_2160p_4iy, x264_cqm_avci300_2160p_4ic, x264_cqm_avci300_2160p_8iy }, |
780 | 0 | { 30000, 1001, 0, 15700, x264_cqm_avci300_2160p_4iy, x264_cqm_avci300_2160p_4ic, x264_cqm_avci300_2160p_8iy }, |
781 | 0 | { 25, 1, 0, 15700, x264_cqm_avci300_2160p_4iy, x264_cqm_avci300_2160p_4ic, x264_cqm_avci300_2160p_8iy }, |
782 | 0 | { 24000, 1001, 0, 15700, x264_cqm_avci300_2160p_4iy, x264_cqm_avci300_2160p_4ic, x264_cqm_avci300_2160p_8iy }}} |
783 | 0 | }; |
784 | |
|
785 | 0 | int res = -1; |
786 | 0 | if( i_csp >= X264_CSP_I420 && i_csp < X264_CSP_I422 && !type ) |
787 | 0 | { |
788 | 0 | if( h->param.i_width == 1440 && h->param.i_height == 1080 ) res = 1; |
789 | 0 | else if( h->param.i_width == 960 && h->param.i_height == 720 ) res = 0; |
790 | 0 | } |
791 | 0 | else if( i_csp >= X264_CSP_I422 && i_csp < X264_CSP_I444 && type ) |
792 | 0 | { |
793 | 0 | if( type < 3 ) |
794 | 0 | { |
795 | 0 | if( h->param.i_width == 1920 && h->param.i_height == 1080 ) res = 1; |
796 | 0 | else if( h->param.i_width == 2048 && h->param.i_height == 1080 ) res = 1; |
797 | 0 | else if( h->param.i_width == 1280 && h->param.i_height == 720 ) res = 0; |
798 | 0 | } |
799 | 0 | else |
800 | 0 | { |
801 | 0 | if( h->param.i_width == 3840 && h->param.i_height == 2160 ) res = 0; |
802 | 0 | else if( h->param.i_width == 4096 && h->param.i_height == 2160 ) res = 0; |
803 | 0 | } |
804 | 0 | } |
805 | 0 | else |
806 | 0 | { |
807 | 0 | x264_log( h, X264_LOG_ERROR, "Invalid colorspace for AVC-Intra %d\n", h->param.i_avcintra_class ); |
808 | 0 | return -1; |
809 | 0 | } |
810 | | |
811 | 0 | if( res < 0 ) |
812 | 0 | { |
813 | 0 | x264_log( h, X264_LOG_ERROR, "Resolution %dx%d invalid for AVC-Intra %d\n", |
814 | 0 | h->param.i_width, h->param.i_height, h->param.i_avcintra_class ); |
815 | 0 | return -1; |
816 | 0 | } |
817 | | |
818 | 0 | if( h->param.nalu_process ) |
819 | 0 | { |
820 | 0 | x264_log( h, X264_LOG_ERROR, "nalu_process is not supported in AVC-Intra mode\n" ); |
821 | 0 | return -1; |
822 | 0 | } |
823 | | |
824 | 0 | if( !h->param.b_repeat_headers ) |
825 | 0 | { |
826 | 0 | x264_log( h, X264_LOG_ERROR, "Separate headers not supported in AVC-Intra mode\n" ); |
827 | 0 | return -1; |
828 | 0 | } |
829 | | |
830 | 0 | int i; |
831 | 0 | uint32_t fps_num = h->param.i_fps_num, fps_den = h->param.i_fps_den; |
832 | 0 | x264_reduce_fraction( &fps_num, &fps_den ); |
833 | 0 | for( i = 0; i < 7; i++ ) |
834 | 0 | { |
835 | 0 | if( avcintra_lut[type][res][i].fps_num == fps_num && |
836 | 0 | avcintra_lut[type][res][i].fps_den == fps_den && |
837 | 0 | avcintra_lut[type][res][i].interlaced == PARAM_INTERLACED ) |
838 | 0 | { |
839 | 0 | break; |
840 | 0 | } |
841 | 0 | } |
842 | 0 | if( i == 7 ) |
843 | 0 | { |
844 | 0 | x264_log( h, X264_LOG_ERROR, "FPS %d/%d%c not compatible with AVC-Intra %d\n", |
845 | 0 | h->param.i_fps_num, h->param.i_fps_den, PARAM_INTERLACED ? 'i' : 'p', h->param.i_avcintra_class ); |
846 | 0 | return -1; |
847 | 0 | } |
848 | | |
849 | 0 | h->param.i_keyint_max = 1; |
850 | 0 | h->param.b_intra_refresh = 0; |
851 | 0 | h->param.analyse.i_weighted_pred = 0; |
852 | 0 | h->param.i_frame_reference = 1; |
853 | 0 | h->param.i_dpb_size = 1; |
854 | |
|
855 | 0 | h->param.b_bluray_compat = 0; |
856 | 0 | h->param.b_vfr_input = 0; |
857 | 0 | h->param.b_aud = 1; |
858 | 0 | h->param.vui.i_chroma_loc = 0; |
859 | 0 | h->param.i_nal_hrd = X264_NAL_HRD_NONE; |
860 | 0 | h->param.b_deblocking_filter = 0; |
861 | 0 | h->param.b_stitchable = 1; |
862 | 0 | h->param.b_pic_struct = 0; |
863 | 0 | h->param.analyse.b_transform_8x8 = 1; |
864 | 0 | h->param.analyse.intra = X264_ANALYSE_I8x8; |
865 | 0 | h->param.analyse.i_chroma_qp_offset = type > 2 ? -4 : res && type ? 3 : 4; |
866 | 0 | h->param.b_cabac = !type; |
867 | 0 | h->param.rc.i_vbv_buffer_size = avcintra_lut[type][res][i].frame_size; |
868 | 0 | h->param.rc.i_vbv_max_bitrate = |
869 | 0 | h->param.rc.i_bitrate = h->param.rc.i_vbv_buffer_size * fps_num / fps_den; |
870 | 0 | h->param.rc.i_rc_method = X264_RC_ABR; |
871 | 0 | h->param.rc.f_vbv_buffer_init = 1.0; |
872 | 0 | h->param.rc.b_filler = 1; |
873 | 0 | h->param.i_cqm_preset = X264_CQM_CUSTOM; |
874 | 0 | memcpy( h->param.cqm_4iy, avcintra_lut[type][res][i].cqm_4iy, sizeof(h->param.cqm_4iy) ); |
875 | 0 | memcpy( h->param.cqm_4ic, avcintra_lut[type][res][i].cqm_4ic, sizeof(h->param.cqm_4ic) ); |
876 | 0 | memcpy( h->param.cqm_8iy, avcintra_lut[type][res][i].cqm_8iy, sizeof(h->param.cqm_8iy) ); |
877 | | |
878 | | /* Sony XAVC flavor much more simple */ |
879 | 0 | if( h->param.i_avcintra_flavor == X264_AVCINTRA_FLAVOR_SONY ) |
880 | 0 | { |
881 | 0 | h->param.i_slice_count = 8; |
882 | 0 | if( h->param.b_sliced_threads ) |
883 | 0 | h->param.i_threads = h->param.i_slice_count; |
884 | | /* Sony XAVC unlike AVC-Intra doesn't seem to have a QP floor */ |
885 | 0 | } |
886 | 0 | else |
887 | 0 | { |
888 | | /* Need exactly 10 slices of equal MB count... why? $deity knows... */ |
889 | 0 | h->param.i_slice_max_mbs = ((h->param.i_width + 15) / 16) * ((h->param.i_height + 15) / 16) / 10; |
890 | 0 | h->param.i_slice_max_size = 0; |
891 | | /* The slice structure only allows a maximum of 2 threads for 1080i/p |
892 | | * and 1 or 5 threads for 720p */ |
893 | 0 | if( h->param.b_sliced_threads ) |
894 | 0 | { |
895 | 0 | if( res ) |
896 | 0 | h->param.i_threads = X264_MIN( 2, h->param.i_threads ); |
897 | 0 | else |
898 | 0 | { |
899 | 0 | h->param.i_threads = X264_MIN( 5, h->param.i_threads ); |
900 | 0 | if( h->param.i_threads < 5 ) |
901 | 0 | h->param.i_threads = 1; |
902 | 0 | } |
903 | 0 | } |
904 | | |
905 | | /* Official encoder doesn't appear to go under 13 |
906 | | * and Avid cannot handle negative QPs */ |
907 | 0 | h->param.rc.i_qp_min = X264_MAX( h->param.rc.i_qp_min, QP_BD_OFFSET + 1 ); |
908 | 0 | } |
909 | |
|
910 | 0 | if( type ) |
911 | 0 | h->param.vui.i_sar_width = h->param.vui.i_sar_height = 1; |
912 | 0 | else |
913 | 0 | { |
914 | 0 | h->param.vui.i_sar_width = 4; |
915 | 0 | h->param.vui.i_sar_height = 3; |
916 | 0 | } |
917 | 0 | } |
918 | | |
919 | 0 | h->param.rc.f_rf_constant = x264_clip3f( h->param.rc.f_rf_constant, -QP_BD_OFFSET, 51 ); |
920 | 0 | h->param.rc.f_rf_constant_max = x264_clip3f( h->param.rc.f_rf_constant_max, -QP_BD_OFFSET, 51 ); |
921 | 0 | h->param.rc.i_qp_constant = x264_clip3( h->param.rc.i_qp_constant, -1, QP_MAX ); |
922 | 0 | h->param.analyse.i_subpel_refine = x264_clip3( h->param.analyse.i_subpel_refine, 0, 11 ); |
923 | 0 | h->param.rc.f_ip_factor = x264_clip3f( h->param.rc.f_ip_factor, 0.01, 10.0 ); |
924 | 0 | h->param.rc.f_pb_factor = x264_clip3f( h->param.rc.f_pb_factor, 0.01, 10.0 ); |
925 | 0 | if( h->param.rc.i_rc_method == X264_RC_CRF ) |
926 | 0 | { |
927 | 0 | h->param.rc.i_qp_constant = h->param.rc.f_rf_constant + QP_BD_OFFSET; |
928 | 0 | h->param.rc.i_bitrate = 0; |
929 | 0 | } |
930 | 0 | if( b_open && (h->param.rc.i_rc_method == X264_RC_CQP || h->param.rc.i_rc_method == X264_RC_CRF) |
931 | 0 | && h->param.rc.i_qp_constant == 0 ) |
932 | 0 | { |
933 | 0 | h->mb.b_lossless = 1; |
934 | 0 | h->param.i_cqm_preset = X264_CQM_FLAT; |
935 | 0 | h->param.psz_cqm_file = NULL; |
936 | 0 | h->param.rc.i_rc_method = X264_RC_CQP; |
937 | 0 | h->param.rc.f_ip_factor = 1; |
938 | 0 | h->param.rc.f_pb_factor = 1; |
939 | 0 | h->param.analyse.b_psnr = 0; |
940 | 0 | h->param.analyse.b_ssim = 0; |
941 | 0 | h->param.analyse.i_chroma_qp_offset = 0; |
942 | 0 | h->param.analyse.i_trellis = 0; |
943 | 0 | h->param.analyse.b_fast_pskip = 0; |
944 | 0 | h->param.analyse.i_noise_reduction = 0; |
945 | 0 | h->param.analyse.b_psy = 0; |
946 | 0 | h->param.i_bframe = 0; |
947 | | /* 8x8dct is not useful without RD in CAVLC lossless */ |
948 | 0 | if( !h->param.b_cabac && h->param.analyse.i_subpel_refine < 6 ) |
949 | 0 | h->param.analyse.b_transform_8x8 = 0; |
950 | 0 | } |
951 | 0 | if( h->param.rc.i_rc_method == X264_RC_CQP ) |
952 | 0 | { |
953 | 0 | float qp_p = h->param.rc.i_qp_constant; |
954 | 0 | float qp_i = qp_p - 6*log2f( h->param.rc.f_ip_factor ); |
955 | 0 | float qp_b = qp_p + 6*log2f( h->param.rc.f_pb_factor ); |
956 | 0 | if( qp_p < 0 ) |
957 | 0 | { |
958 | 0 | x264_log( h, X264_LOG_ERROR, "qp not specified\n" ); |
959 | 0 | return -1; |
960 | 0 | } |
961 | | |
962 | 0 | h->param.rc.i_qp_min = x264_clip3( (int)(X264_MIN3( qp_p, qp_i, qp_b )), 0, QP_MAX ); |
963 | 0 | h->param.rc.i_qp_max = x264_clip3( (int)(X264_MAX3( qp_p, qp_i, qp_b ) + .999), 0, QP_MAX ); |
964 | 0 | h->param.rc.i_aq_mode = 0; |
965 | 0 | h->param.rc.b_mb_tree = 0; |
966 | 0 | h->param.rc.i_bitrate = 0; |
967 | 0 | } |
968 | 0 | h->param.rc.i_qp_max = x264_clip3( h->param.rc.i_qp_max, 0, QP_MAX ); |
969 | 0 | h->param.rc.i_qp_min = x264_clip3( h->param.rc.i_qp_min, 0, h->param.rc.i_qp_max ); |
970 | 0 | h->param.rc.i_qp_step = x264_clip3( h->param.rc.i_qp_step, 2, QP_MAX ); |
971 | 0 | h->param.rc.i_bitrate = x264_clip3( h->param.rc.i_bitrate, 0, 2000000 ); |
972 | 0 | if( h->param.rc.i_rc_method == X264_RC_ABR && !h->param.rc.i_bitrate ) |
973 | 0 | { |
974 | 0 | x264_log( h, X264_LOG_ERROR, "bitrate not specified\n" ); |
975 | 0 | return -1; |
976 | 0 | } |
977 | 0 | h->param.rc.i_vbv_buffer_size = x264_clip3( h->param.rc.i_vbv_buffer_size, 0, 2000000 ); |
978 | 0 | h->param.rc.i_vbv_max_bitrate = x264_clip3( h->param.rc.i_vbv_max_bitrate, 0, 2000000 ); |
979 | 0 | h->param.rc.f_vbv_buffer_init = x264_clip3f( h->param.rc.f_vbv_buffer_init, 0, 2000000 ); |
980 | 0 | if( h->param.rc.i_vbv_buffer_size ) |
981 | 0 | { |
982 | 0 | if( h->param.rc.i_rc_method == X264_RC_CQP ) |
983 | 0 | { |
984 | 0 | x264_log( h, X264_LOG_WARNING, "VBV is incompatible with constant QP, ignored.\n" ); |
985 | 0 | h->param.rc.i_vbv_max_bitrate = 0; |
986 | 0 | h->param.rc.i_vbv_buffer_size = 0; |
987 | 0 | } |
988 | 0 | else if( h->param.rc.i_vbv_max_bitrate == 0 ) |
989 | 0 | { |
990 | 0 | if( h->param.rc.i_rc_method == X264_RC_ABR ) |
991 | 0 | { |
992 | 0 | x264_log( h, X264_LOG_WARNING, "VBV maxrate unspecified, assuming CBR\n" ); |
993 | 0 | h->param.rc.i_vbv_max_bitrate = h->param.rc.i_bitrate; |
994 | 0 | } |
995 | 0 | else |
996 | 0 | { |
997 | 0 | x264_log( h, X264_LOG_WARNING, "VBV bufsize set but maxrate unspecified, ignored\n" ); |
998 | 0 | h->param.rc.i_vbv_buffer_size = 0; |
999 | 0 | } |
1000 | 0 | } |
1001 | 0 | else if( h->param.rc.i_vbv_max_bitrate < h->param.rc.i_bitrate && |
1002 | 0 | h->param.rc.i_rc_method == X264_RC_ABR ) |
1003 | 0 | { |
1004 | 0 | x264_log( h, X264_LOG_WARNING, "max bitrate less than average bitrate, assuming CBR\n" ); |
1005 | 0 | h->param.rc.i_bitrate = h->param.rc.i_vbv_max_bitrate; |
1006 | 0 | } |
1007 | 0 | } |
1008 | 0 | else if( h->param.rc.i_vbv_max_bitrate ) |
1009 | 0 | { |
1010 | 0 | x264_log( h, X264_LOG_WARNING, "VBV maxrate specified, but no bufsize, ignored\n" ); |
1011 | 0 | h->param.rc.i_vbv_max_bitrate = 0; |
1012 | 0 | } |
1013 | |
|
1014 | 0 | h->param.i_slice_max_size = X264_MAX( h->param.i_slice_max_size, 0 ); |
1015 | 0 | h->param.i_slice_max_mbs = X264_MAX( h->param.i_slice_max_mbs, 0 ); |
1016 | 0 | h->param.i_slice_min_mbs = X264_MAX( h->param.i_slice_min_mbs, 0 ); |
1017 | 0 | if( h->param.i_slice_max_mbs ) |
1018 | 0 | h->param.i_slice_min_mbs = X264_MIN( h->param.i_slice_min_mbs, h->param.i_slice_max_mbs/2 ); |
1019 | 0 | else if( !h->param.i_slice_max_size ) |
1020 | 0 | h->param.i_slice_min_mbs = 0; |
1021 | 0 | if( PARAM_INTERLACED && h->param.i_slice_min_mbs ) |
1022 | 0 | { |
1023 | 0 | x264_log( h, X264_LOG_WARNING, "interlace + slice-min-mbs is not implemented\n" ); |
1024 | 0 | h->param.i_slice_min_mbs = 0; |
1025 | 0 | } |
1026 | 0 | int mb_width = (h->param.i_width+15)/16; |
1027 | 0 | if( h->param.i_slice_min_mbs > mb_width ) |
1028 | 0 | { |
1029 | 0 | x264_log( h, X264_LOG_WARNING, "slice-min-mbs > row mb size (%d) not implemented\n", mb_width ); |
1030 | 0 | h->param.i_slice_min_mbs = mb_width; |
1031 | 0 | } |
1032 | |
|
1033 | 0 | int max_slices = (h->param.i_height+((16<<PARAM_INTERLACED)-1))/(16<<PARAM_INTERLACED); |
1034 | 0 | if( h->param.b_sliced_threads ) |
1035 | 0 | h->param.i_slice_count = x264_clip3( h->param.i_threads, 0, max_slices ); |
1036 | 0 | else |
1037 | 0 | { |
1038 | 0 | h->param.i_slice_count = x264_clip3( h->param.i_slice_count, 0, max_slices ); |
1039 | 0 | if( h->param.i_slice_max_mbs || h->param.i_slice_max_size ) |
1040 | 0 | h->param.i_slice_count = 0; |
1041 | 0 | } |
1042 | 0 | if( h->param.i_slice_count_max > 0 ) |
1043 | 0 | h->param.i_slice_count_max = X264_MAX( h->param.i_slice_count, h->param.i_slice_count_max ); |
1044 | |
|
1045 | 0 | if( h->param.b_bluray_compat ) |
1046 | 0 | { |
1047 | 0 | h->param.i_bframe_pyramid = X264_MIN( X264_B_PYRAMID_STRICT, h->param.i_bframe_pyramid ); |
1048 | 0 | h->param.i_bframe = X264_MIN( h->param.i_bframe, 3 ); |
1049 | 0 | h->param.b_aud = 1; |
1050 | 0 | h->param.i_nal_hrd = X264_MAX( h->param.i_nal_hrd, X264_NAL_HRD_VBR ); |
1051 | 0 | h->param.i_slice_max_size = 0; |
1052 | 0 | h->param.i_slice_max_mbs = 0; |
1053 | 0 | h->param.b_intra_refresh = 0; |
1054 | 0 | h->param.i_frame_reference = X264_MIN( h->param.i_frame_reference, 6 ); |
1055 | 0 | h->param.i_dpb_size = X264_MIN( h->param.i_dpb_size, 6 ); |
1056 | | /* Don't use I-frames, because Blu-ray treats them the same as IDR. */ |
1057 | 0 | h->param.i_keyint_min = 1; |
1058 | | /* Due to the proliferation of broken players that don't handle dupes properly. */ |
1059 | 0 | h->param.analyse.i_weighted_pred = X264_MIN( h->param.analyse.i_weighted_pred, X264_WEIGHTP_SIMPLE ); |
1060 | 0 | if( h->param.b_fake_interlaced ) |
1061 | 0 | h->param.b_pic_struct = 1; |
1062 | 0 | } |
1063 | |
|
1064 | 0 | h->param.i_frame_reference = x264_clip3( h->param.i_frame_reference, 1, X264_REF_MAX ); |
1065 | 0 | h->param.i_dpb_size = x264_clip3( h->param.i_dpb_size, 1, X264_REF_MAX ); |
1066 | 0 | if( h->param.i_scenecut_threshold < 0 ) |
1067 | 0 | h->param.i_scenecut_threshold = 0; |
1068 | 0 | h->param.analyse.i_direct_mv_pred = x264_clip3( h->param.analyse.i_direct_mv_pred, X264_DIRECT_PRED_NONE, X264_DIRECT_PRED_AUTO ); |
1069 | 0 | if( !h->param.analyse.i_subpel_refine && h->param.analyse.i_direct_mv_pred > X264_DIRECT_PRED_SPATIAL ) |
1070 | 0 | { |
1071 | 0 | x264_log( h, X264_LOG_WARNING, "subme=0 + direct=temporal is not supported\n" ); |
1072 | 0 | h->param.analyse.i_direct_mv_pred = X264_DIRECT_PRED_SPATIAL; |
1073 | 0 | } |
1074 | 0 | h->param.i_bframe = x264_clip3( h->param.i_bframe, 0, X264_MIN( X264_BFRAME_MAX, h->param.i_keyint_max-1 ) ); |
1075 | 0 | h->param.i_bframe_bias = x264_clip3( h->param.i_bframe_bias, -90, 100 ); |
1076 | 0 | if( h->param.i_bframe <= 1 ) |
1077 | 0 | h->param.i_bframe_pyramid = X264_B_PYRAMID_NONE; |
1078 | 0 | h->param.i_bframe_pyramid = x264_clip3( h->param.i_bframe_pyramid, X264_B_PYRAMID_NONE, X264_B_PYRAMID_NORMAL ); |
1079 | 0 | h->param.i_bframe_adaptive = x264_clip3( h->param.i_bframe_adaptive, X264_B_ADAPT_NONE, X264_B_ADAPT_TRELLIS ); |
1080 | 0 | if( !h->param.i_bframe ) |
1081 | 0 | { |
1082 | 0 | h->param.i_bframe_adaptive = X264_B_ADAPT_NONE; |
1083 | 0 | h->param.analyse.i_direct_mv_pred = 0; |
1084 | 0 | h->param.analyse.b_weighted_bipred = 0; |
1085 | 0 | h->param.b_open_gop = 0; |
1086 | 0 | } |
1087 | 0 | if( h->param.b_intra_refresh && h->param.i_bframe_pyramid == X264_B_PYRAMID_NORMAL ) |
1088 | 0 | { |
1089 | 0 | x264_log( h, X264_LOG_WARNING, "b-pyramid normal + intra-refresh is not supported\n" ); |
1090 | 0 | h->param.i_bframe_pyramid = X264_B_PYRAMID_STRICT; |
1091 | 0 | } |
1092 | 0 | if( h->param.b_intra_refresh && (h->param.i_frame_reference > 1 || h->param.i_dpb_size > 1) ) |
1093 | 0 | { |
1094 | 0 | x264_log( h, X264_LOG_WARNING, "ref > 1 + intra-refresh is not supported\n" ); |
1095 | 0 | h->param.i_frame_reference = 1; |
1096 | 0 | h->param.i_dpb_size = 1; |
1097 | 0 | } |
1098 | 0 | if( h->param.b_intra_refresh && h->param.b_open_gop ) |
1099 | 0 | { |
1100 | 0 | x264_log( h, X264_LOG_WARNING, "intra-refresh is not compatible with open-gop\n" ); |
1101 | 0 | h->param.b_open_gop = 0; |
1102 | 0 | } |
1103 | 0 | if( !h->param.i_fps_num || !h->param.i_fps_den ) |
1104 | 0 | { |
1105 | 0 | h->param.i_fps_num = 25; |
1106 | 0 | h->param.i_fps_den = 1; |
1107 | 0 | } |
1108 | 0 | float fps = (float)h->param.i_fps_num / h->param.i_fps_den; |
1109 | 0 | if( h->param.i_keyint_min == X264_KEYINT_MIN_AUTO ) |
1110 | 0 | h->param.i_keyint_min = X264_MIN( h->param.i_keyint_max / 10, (int)fps ); |
1111 | 0 | h->param.i_keyint_min = x264_clip3( h->param.i_keyint_min, 1, h->param.i_keyint_max/2+1 ); |
1112 | 0 | h->param.rc.i_lookahead = x264_clip3( h->param.rc.i_lookahead, 0, X264_LOOKAHEAD_MAX ); |
1113 | 0 | { |
1114 | 0 | int maxrate = X264_MAX( h->param.rc.i_vbv_max_bitrate, h->param.rc.i_bitrate ); |
1115 | 0 | float bufsize = maxrate ? (float)h->param.rc.i_vbv_buffer_size / maxrate : 0; |
1116 | 0 | h->param.rc.i_lookahead = X264_MIN( h->param.rc.i_lookahead, X264_MAX( h->param.i_keyint_max, bufsize*fps ) ); |
1117 | 0 | } |
1118 | |
|
1119 | 0 | if( !h->param.i_timebase_num || !h->param.i_timebase_den || !(h->param.b_vfr_input || h->param.b_pulldown) ) |
1120 | 0 | { |
1121 | 0 | h->param.i_timebase_num = h->param.i_fps_den; |
1122 | 0 | h->param.i_timebase_den = h->param.i_fps_num; |
1123 | 0 | } |
1124 | |
|
1125 | 0 | h->param.rc.f_qcompress = x264_clip3f( h->param.rc.f_qcompress, 0.0, 1.0 ); |
1126 | 0 | if( h->param.i_keyint_max == 1 || h->param.rc.f_qcompress == 1 ) |
1127 | 0 | h->param.rc.b_mb_tree = 0; |
1128 | 0 | if( (!h->param.b_intra_refresh && h->param.i_keyint_max != X264_KEYINT_MAX_INFINITE) && |
1129 | 0 | !h->param.rc.i_lookahead && h->param.rc.b_mb_tree ) |
1130 | 0 | { |
1131 | 0 | x264_log( h, X264_LOG_WARNING, "lookaheadless mb-tree requires intra refresh or infinite keyint\n" ); |
1132 | 0 | h->param.rc.b_mb_tree = 0; |
1133 | 0 | } |
1134 | 0 | if( b_open && h->param.rc.b_stat_read ) |
1135 | 0 | h->param.rc.i_lookahead = 0; |
1136 | 0 | #if HAVE_THREAD |
1137 | 0 | if( h->param.i_sync_lookahead < 0 ) |
1138 | 0 | h->param.i_sync_lookahead = h->param.i_bframe + 1; |
1139 | 0 | h->param.i_sync_lookahead = X264_MIN( h->param.i_sync_lookahead, X264_LOOKAHEAD_MAX ); |
1140 | 0 | if( h->param.rc.b_stat_read || h->i_thread_frames == 1 ) |
1141 | 0 | h->param.i_sync_lookahead = 0; |
1142 | | #else |
1143 | | h->param.i_sync_lookahead = 0; |
1144 | | #endif |
1145 | |
|
1146 | 0 | h->param.i_deblocking_filter_alphac0 = x264_clip3( h->param.i_deblocking_filter_alphac0, -6, 6 ); |
1147 | 0 | h->param.i_deblocking_filter_beta = x264_clip3( h->param.i_deblocking_filter_beta, -6, 6 ); |
1148 | 0 | h->param.analyse.i_luma_deadzone[0] = x264_clip3( h->param.analyse.i_luma_deadzone[0], 0, 32 ); |
1149 | 0 | h->param.analyse.i_luma_deadzone[1] = x264_clip3( h->param.analyse.i_luma_deadzone[1], 0, 32 ); |
1150 | |
|
1151 | 0 | h->param.i_cabac_init_idc = x264_clip3( h->param.i_cabac_init_idc, 0, 2 ); |
1152 | |
|
1153 | 0 | if( h->param.i_cqm_preset < X264_CQM_FLAT || h->param.i_cqm_preset > X264_CQM_CUSTOM ) |
1154 | 0 | h->param.i_cqm_preset = X264_CQM_FLAT; |
1155 | |
|
1156 | 0 | if( h->param.analyse.i_me_method < X264_ME_DIA || |
1157 | 0 | h->param.analyse.i_me_method > X264_ME_TESA ) |
1158 | 0 | h->param.analyse.i_me_method = X264_ME_HEX; |
1159 | 0 | h->param.analyse.i_me_range = x264_clip3( h->param.analyse.i_me_range, 4, 1024 ); |
1160 | 0 | if( h->param.analyse.i_me_range > 16 && h->param.analyse.i_me_method <= X264_ME_HEX ) |
1161 | 0 | h->param.analyse.i_me_range = 16; |
1162 | 0 | if( h->param.analyse.i_me_method == X264_ME_TESA && |
1163 | 0 | (h->mb.b_lossless || h->param.analyse.i_subpel_refine <= 1) ) |
1164 | 0 | h->param.analyse.i_me_method = X264_ME_ESA; |
1165 | 0 | h->param.analyse.b_mixed_references = h->param.analyse.b_mixed_references && h->param.i_frame_reference > 1; |
1166 | 0 | h->param.analyse.inter &= X264_ANALYSE_PSUB16x16|X264_ANALYSE_PSUB8x8|X264_ANALYSE_BSUB16x16| |
1167 | 0 | X264_ANALYSE_I4x4|X264_ANALYSE_I8x8; |
1168 | 0 | h->param.analyse.intra &= X264_ANALYSE_I4x4|X264_ANALYSE_I8x8; |
1169 | 0 | if( !(h->param.analyse.inter & X264_ANALYSE_PSUB16x16) ) |
1170 | 0 | h->param.analyse.inter &= ~X264_ANALYSE_PSUB8x8; |
1171 | 0 | if( !h->param.analyse.b_transform_8x8 ) |
1172 | 0 | { |
1173 | 0 | h->param.analyse.inter &= ~X264_ANALYSE_I8x8; |
1174 | 0 | h->param.analyse.intra &= ~X264_ANALYSE_I8x8; |
1175 | 0 | } |
1176 | 0 | h->param.analyse.i_trellis = x264_clip3( h->param.analyse.i_trellis, 0, 2 ); |
1177 | 0 | h->param.rc.i_aq_mode = x264_clip3( h->param.rc.i_aq_mode, 0, 3 ); |
1178 | 0 | h->param.rc.f_aq_strength = x264_clip3f( h->param.rc.f_aq_strength, 0, 3 ); |
1179 | 0 | if( h->param.rc.f_aq_strength == 0 ) |
1180 | 0 | h->param.rc.i_aq_mode = 0; |
1181 | |
|
1182 | 0 | if( h->param.i_log_level < X264_LOG_INFO ) |
1183 | 0 | { |
1184 | 0 | h->param.analyse.b_psnr = 0; |
1185 | 0 | h->param.analyse.b_ssim = 0; |
1186 | 0 | } |
1187 | | /* Warn users trying to measure PSNR/SSIM with psy opts on. */ |
1188 | 0 | if( b_open && (h->param.analyse.b_psnr || h->param.analyse.b_ssim) ) |
1189 | 0 | { |
1190 | 0 | char *s = NULL; |
1191 | |
|
1192 | 0 | if( h->param.analyse.b_psy ) |
1193 | 0 | { |
1194 | 0 | s = h->param.analyse.b_psnr ? "psnr" : "ssim"; |
1195 | 0 | x264_log( h, X264_LOG_WARNING, "--%s used with psy on: results will be invalid!\n", s ); |
1196 | 0 | } |
1197 | 0 | else if( !h->param.rc.i_aq_mode && h->param.analyse.b_ssim ) |
1198 | 0 | { |
1199 | 0 | x264_log( h, X264_LOG_WARNING, "--ssim used with AQ off: results will be invalid!\n" ); |
1200 | 0 | s = "ssim"; |
1201 | 0 | } |
1202 | 0 | else if( h->param.rc.i_aq_mode && h->param.analyse.b_psnr ) |
1203 | 0 | { |
1204 | 0 | x264_log( h, X264_LOG_WARNING, "--psnr used with AQ on: results will be invalid!\n" ); |
1205 | 0 | s = "psnr"; |
1206 | 0 | } |
1207 | 0 | if( s ) |
1208 | 0 | x264_log( h, X264_LOG_WARNING, "--tune %s should be used if attempting to benchmark %s!\n", s, s ); |
1209 | 0 | } |
1210 | |
|
1211 | 0 | if( !h->param.analyse.b_psy ) |
1212 | 0 | { |
1213 | 0 | h->param.analyse.f_psy_rd = 0; |
1214 | 0 | h->param.analyse.f_psy_trellis = 0; |
1215 | 0 | } |
1216 | 0 | h->param.analyse.f_psy_rd = x264_clip3f( h->param.analyse.f_psy_rd, 0, 10 ); |
1217 | 0 | h->param.analyse.f_psy_trellis = x264_clip3f( h->param.analyse.f_psy_trellis, 0, 10 ); |
1218 | 0 | h->mb.i_psy_rd = h->param.analyse.i_subpel_refine >= 6 ? FIX8( h->param.analyse.f_psy_rd ) : 0; |
1219 | 0 | h->mb.i_psy_trellis = h->param.analyse.i_trellis ? FIX8( h->param.analyse.f_psy_trellis / 4 ) : 0; |
1220 | 0 | h->param.analyse.i_chroma_qp_offset = x264_clip3(h->param.analyse.i_chroma_qp_offset, -32, 32); |
1221 | | /* In 4:4:4 mode, chroma gets twice as much resolution, so we can halve its quality. */ |
1222 | 0 | if( b_open && i_csp >= X264_CSP_I444 && i_csp < X264_CSP_BGR && h->param.analyse.b_psy ) |
1223 | 0 | h->param.analyse.i_chroma_qp_offset += 6; |
1224 | | /* Psy RDO increases overall quantizers to improve the quality of luma--this indirectly hurts chroma quality */ |
1225 | | /* so we lower the chroma QP offset to compensate */ |
1226 | 0 | if( b_open && h->mb.i_psy_rd && !h->param.i_avcintra_class ) |
1227 | 0 | h->param.analyse.i_chroma_qp_offset -= h->param.analyse.f_psy_rd < 0.25 ? 1 : 2; |
1228 | | /* Psy trellis has a similar effect. */ |
1229 | 0 | if( b_open && h->mb.i_psy_trellis && !h->param.i_avcintra_class ) |
1230 | 0 | h->param.analyse.i_chroma_qp_offset -= h->param.analyse.f_psy_trellis < 0.25 ? 1 : 2; |
1231 | 0 | h->param.analyse.i_chroma_qp_offset = x264_clip3(h->param.analyse.i_chroma_qp_offset, -12, 12); |
1232 | | /* MB-tree requires AQ to be on, even if the strength is zero. */ |
1233 | 0 | if( !h->param.rc.i_aq_mode && h->param.rc.b_mb_tree ) |
1234 | 0 | { |
1235 | 0 | h->param.rc.i_aq_mode = 1; |
1236 | 0 | h->param.rc.f_aq_strength = 0; |
1237 | 0 | } |
1238 | 0 | h->param.analyse.i_noise_reduction = x264_clip3( h->param.analyse.i_noise_reduction, 0, 1<<16 ); |
1239 | 0 | if( h->param.analyse.i_subpel_refine >= 10 && (h->param.analyse.i_trellis != 2 || !h->param.rc.i_aq_mode) ) |
1240 | 0 | h->param.analyse.i_subpel_refine = 9; |
1241 | |
|
1242 | 0 | if( b_open ) |
1243 | 0 | { |
1244 | 0 | const x264_level_t *l = x264_levels; |
1245 | 0 | if( h->param.i_level_idc < 0 ) |
1246 | 0 | { |
1247 | 0 | int maxrate_bak = h->param.rc.i_vbv_max_bitrate; |
1248 | 0 | if( h->param.rc.i_rc_method == X264_RC_ABR && h->param.rc.i_vbv_buffer_size <= 0 ) |
1249 | 0 | h->param.rc.i_vbv_max_bitrate = h->param.rc.i_bitrate * 2; |
1250 | 0 | x264_sps_init( h->sps, h->param.i_sps_id, &h->param ); |
1251 | 0 | do h->param.i_level_idc = l->level_idc; |
1252 | 0 | while( l[1].level_idc && x264_validate_levels( h, 0 ) && l++ ); |
1253 | 0 | h->param.rc.i_vbv_max_bitrate = maxrate_bak; |
1254 | 0 | } |
1255 | 0 | else |
1256 | 0 | { |
1257 | 0 | while( l->level_idc && l->level_idc != h->param.i_level_idc ) |
1258 | 0 | l++; |
1259 | 0 | if( l->level_idc == 0 ) |
1260 | 0 | { |
1261 | 0 | x264_log( h, X264_LOG_ERROR, "invalid level_idc: %d\n", h->param.i_level_idc ); |
1262 | 0 | return -1; |
1263 | 0 | } |
1264 | 0 | } |
1265 | 0 | if( h->param.analyse.i_mv_range <= 0 ) |
1266 | 0 | h->param.analyse.i_mv_range = l->mv_range >> PARAM_INTERLACED; |
1267 | 0 | else |
1268 | 0 | h->param.analyse.i_mv_range = x264_clip3(h->param.analyse.i_mv_range, 32, 8192 >> PARAM_INTERLACED); |
1269 | 0 | } |
1270 | | |
1271 | 0 | h->param.analyse.i_weighted_pred = x264_clip3( h->param.analyse.i_weighted_pred, X264_WEIGHTP_NONE, X264_WEIGHTP_SMART ); |
1272 | |
|
1273 | 0 | if( h->param.i_lookahead_threads == X264_THREADS_AUTO ) |
1274 | 0 | { |
1275 | 0 | if( h->param.b_sliced_threads ) |
1276 | 0 | h->param.i_lookahead_threads = h->param.i_threads; |
1277 | 0 | else |
1278 | 0 | { |
1279 | | /* If we're using much slower lookahead settings than encoding settings, it helps a lot to use |
1280 | | * more lookahead threads. This typically happens in the first pass of a two-pass encode, so |
1281 | | * try to guess at this sort of case. |
1282 | | * |
1283 | | * Tuned by a little bit of real encoding with the various presets. */ |
1284 | 0 | int badapt = h->param.i_bframe_adaptive == X264_B_ADAPT_TRELLIS; |
1285 | 0 | int subme = X264_MIN( h->param.analyse.i_subpel_refine / 3, 3 ) + (h->param.analyse.i_subpel_refine > 1); |
1286 | 0 | int bframes = X264_MIN( (h->param.i_bframe - 1) / 3, 3 ); |
1287 | | |
1288 | | /* [b-adapt 0/1 vs 2][quantized subme][quantized bframes] */ |
1289 | 0 | static const uint8_t lookahead_thread_div[2][5][4] = |
1290 | 0 | {{{6,6,6,6}, {3,3,3,3}, {4,4,4,4}, {6,6,6,6}, {12,12,12,12}}, |
1291 | 0 | {{3,2,1,1}, {2,1,1,1}, {4,3,2,1}, {6,4,3,2}, {12, 9, 6, 4}}}; |
1292 | |
|
1293 | 0 | h->param.i_lookahead_threads = h->param.i_threads / lookahead_thread_div[badapt][subme][bframes]; |
1294 | | /* Since too many lookahead threads significantly degrades lookahead accuracy, limit auto |
1295 | | * lookahead threads to about 8 macroblock rows high each at worst. This number is chosen |
1296 | | * pretty much arbitrarily. */ |
1297 | 0 | h->param.i_lookahead_threads = X264_MIN( h->param.i_lookahead_threads, h->param.i_height / 128 ); |
1298 | 0 | } |
1299 | 0 | } |
1300 | 0 | h->param.i_lookahead_threads = x264_clip3( h->param.i_lookahead_threads, 1, X264_MIN( max_sliced_threads, X264_LOOKAHEAD_THREAD_MAX ) ); |
1301 | |
|
1302 | 0 | if( PARAM_INTERLACED ) |
1303 | 0 | { |
1304 | 0 | if( h->param.analyse.i_me_method >= X264_ME_ESA ) |
1305 | 0 | { |
1306 | 0 | x264_log( h, X264_LOG_WARNING, "interlace + me=esa is not implemented\n" ); |
1307 | 0 | h->param.analyse.i_me_method = X264_ME_UMH; |
1308 | 0 | } |
1309 | 0 | if( h->param.analyse.i_weighted_pred > 0 ) |
1310 | 0 | { |
1311 | 0 | x264_log( h, X264_LOG_WARNING, "interlace + weightp is not implemented\n" ); |
1312 | 0 | h->param.analyse.i_weighted_pred = X264_WEIGHTP_NONE; |
1313 | 0 | } |
1314 | 0 | } |
1315 | |
|
1316 | 0 | if( !h->param.analyse.i_weighted_pred && h->param.rc.b_mb_tree && h->param.analyse.b_psy ) |
1317 | 0 | h->param.analyse.i_weighted_pred = X264_WEIGHTP_FAKE; |
1318 | |
|
1319 | 0 | if( h->i_thread_frames > 1 ) |
1320 | 0 | { |
1321 | 0 | int r = h->param.analyse.i_mv_range_thread; |
1322 | 0 | int r2; |
1323 | 0 | if( r <= 0 ) |
1324 | 0 | { |
1325 | | // half of the available space is reserved and divided evenly among the threads, |
1326 | | // the rest is allocated to whichever thread is far enough ahead to use it. |
1327 | | // reserving more space increases quality for some videos, but costs more time |
1328 | | // in thread synchronization. |
1329 | 0 | int max_range = (h->param.i_height + X264_THREAD_HEIGHT) / h->i_thread_frames - X264_THREAD_HEIGHT; |
1330 | 0 | r = max_range / 2; |
1331 | 0 | } |
1332 | 0 | r = X264_MAX( r, h->param.analyse.i_me_range ); |
1333 | 0 | r = X264_MIN( r, h->param.analyse.i_mv_range ); |
1334 | | // round up to use the whole mb row |
1335 | 0 | r2 = (r & ~15) + ((-X264_THREAD_HEIGHT) & 15); |
1336 | 0 | if( r2 < r ) |
1337 | 0 | r2 += 16; |
1338 | 0 | x264_log( h, X264_LOG_DEBUG, "using mv_range_thread = %d\n", r2 ); |
1339 | 0 | h->param.analyse.i_mv_range_thread = r2; |
1340 | 0 | } |
1341 | |
|
1342 | 0 | if( h->param.rc.f_rate_tolerance < 0 ) |
1343 | 0 | h->param.rc.f_rate_tolerance = 0; |
1344 | 0 | if( h->param.rc.f_qblur < 0 ) |
1345 | 0 | h->param.rc.f_qblur = 0; |
1346 | 0 | if( h->param.rc.f_complexity_blur < 0 ) |
1347 | 0 | h->param.rc.f_complexity_blur = 0; |
1348 | |
|
1349 | 0 | h->param.i_sps_id &= 31; |
1350 | |
|
1351 | 0 | h->param.i_nal_hrd = x264_clip3( h->param.i_nal_hrd, X264_NAL_HRD_NONE, X264_NAL_HRD_CBR ); |
1352 | |
|
1353 | 0 | if( h->param.i_nal_hrd && !h->param.rc.i_vbv_buffer_size ) |
1354 | 0 | { |
1355 | 0 | x264_log( h, X264_LOG_WARNING, "NAL HRD parameters require VBV parameters\n" ); |
1356 | 0 | h->param.i_nal_hrd = X264_NAL_HRD_NONE; |
1357 | 0 | } |
1358 | |
|
1359 | 0 | if( h->param.i_nal_hrd == X264_NAL_HRD_CBR && |
1360 | 0 | (h->param.rc.i_bitrate != h->param.rc.i_vbv_max_bitrate || !h->param.rc.i_vbv_max_bitrate) ) |
1361 | 0 | { |
1362 | 0 | x264_log( h, X264_LOG_WARNING, "CBR HRD requires constant bitrate\n" ); |
1363 | 0 | h->param.i_nal_hrd = X264_NAL_HRD_VBR; |
1364 | 0 | } |
1365 | |
|
1366 | 0 | if( h->param.i_nal_hrd == X264_NAL_HRD_CBR ) |
1367 | 0 | h->param.rc.b_filler = 1; |
1368 | | |
1369 | | /* ensure the booleans are 0 or 1 so they can be used in math */ |
1370 | 0 | #define BOOLIFY(x) h->param.x = !!h->param.x |
1371 | 0 | BOOLIFY( b_cabac ); |
1372 | 0 | BOOLIFY( b_constrained_intra ); |
1373 | 0 | BOOLIFY( b_deblocking_filter ); |
1374 | 0 | BOOLIFY( b_deterministic ); |
1375 | 0 | BOOLIFY( b_sliced_threads ); |
1376 | 0 | BOOLIFY( b_interlaced ); |
1377 | 0 | BOOLIFY( b_intra_refresh ); |
1378 | 0 | BOOLIFY( b_aud ); |
1379 | 0 | BOOLIFY( b_repeat_headers ); |
1380 | 0 | BOOLIFY( b_annexb ); |
1381 | 0 | BOOLIFY( b_vfr_input ); |
1382 | 0 | BOOLIFY( b_pulldown ); |
1383 | 0 | BOOLIFY( b_tff ); |
1384 | 0 | BOOLIFY( b_pic_struct ); |
1385 | 0 | BOOLIFY( b_fake_interlaced ); |
1386 | 0 | BOOLIFY( b_open_gop ); |
1387 | 0 | BOOLIFY( b_bluray_compat ); |
1388 | 0 | BOOLIFY( b_stitchable ); |
1389 | 0 | BOOLIFY( b_full_recon ); |
1390 | 0 | BOOLIFY( b_opencl ); |
1391 | 0 | BOOLIFY( analyse.b_transform_8x8 ); |
1392 | 0 | BOOLIFY( analyse.b_weighted_bipred ); |
1393 | 0 | BOOLIFY( analyse.b_chroma_me ); |
1394 | 0 | BOOLIFY( analyse.b_mixed_references ); |
1395 | 0 | BOOLIFY( analyse.b_fast_pskip ); |
1396 | 0 | BOOLIFY( analyse.b_dct_decimate ); |
1397 | 0 | BOOLIFY( analyse.b_psy ); |
1398 | 0 | BOOLIFY( analyse.b_psnr ); |
1399 | 0 | BOOLIFY( analyse.b_ssim ); |
1400 | 0 | BOOLIFY( rc.b_stat_write ); |
1401 | 0 | BOOLIFY( rc.b_stat_read ); |
1402 | 0 | BOOLIFY( rc.b_mb_tree ); |
1403 | 0 | BOOLIFY( rc.b_filler ); |
1404 | 0 | #undef BOOLIFY |
1405 | |
|
1406 | 0 | return 0; |
1407 | 0 | } |
1408 | | |
1409 | | static void mbcmp_init( x264_t *h ) |
1410 | 0 | { |
1411 | 0 | int satd = !h->mb.b_lossless && h->param.analyse.i_subpel_refine > 1; |
1412 | 0 | memcpy( h->pixf.mbcmp, satd ? h->pixf.satd : h->pixf.sad_aligned, sizeof(h->pixf.mbcmp) ); |
1413 | 0 | memcpy( h->pixf.mbcmp_unaligned, satd ? h->pixf.satd : h->pixf.sad, sizeof(h->pixf.mbcmp_unaligned) ); |
1414 | 0 | h->pixf.intra_mbcmp_x3_16x16 = satd ? h->pixf.intra_satd_x3_16x16 : h->pixf.intra_sad_x3_16x16; |
1415 | 0 | h->pixf.intra_mbcmp_x3_8x16c = satd ? h->pixf.intra_satd_x3_8x16c : h->pixf.intra_sad_x3_8x16c; |
1416 | 0 | h->pixf.intra_mbcmp_x3_8x8c = satd ? h->pixf.intra_satd_x3_8x8c : h->pixf.intra_sad_x3_8x8c; |
1417 | 0 | h->pixf.intra_mbcmp_x3_8x8 = satd ? h->pixf.intra_sa8d_x3_8x8 : h->pixf.intra_sad_x3_8x8; |
1418 | 0 | h->pixf.intra_mbcmp_x3_4x4 = satd ? h->pixf.intra_satd_x3_4x4 : h->pixf.intra_sad_x3_4x4; |
1419 | 0 | h->pixf.intra_mbcmp_x9_4x4 = h->param.b_cpu_independent || h->mb.b_lossless ? NULL |
1420 | 0 | : satd ? h->pixf.intra_satd_x9_4x4 : h->pixf.intra_sad_x9_4x4; |
1421 | 0 | h->pixf.intra_mbcmp_x9_8x8 = h->param.b_cpu_independent || h->mb.b_lossless ? NULL |
1422 | 0 | : satd ? h->pixf.intra_sa8d_x9_8x8 : h->pixf.intra_sad_x9_8x8; |
1423 | 0 | satd &= h->param.analyse.i_me_method == X264_ME_TESA; |
1424 | 0 | memcpy( h->pixf.fpelcmp, satd ? h->pixf.satd : h->pixf.sad, sizeof(h->pixf.fpelcmp) ); |
1425 | 0 | memcpy( h->pixf.fpelcmp_x3, satd ? h->pixf.satd_x3 : h->pixf.sad_x3, sizeof(h->pixf.fpelcmp_x3) ); |
1426 | 0 | memcpy( h->pixf.fpelcmp_x4, satd ? h->pixf.satd_x4 : h->pixf.sad_x4, sizeof(h->pixf.fpelcmp_x4) ); |
1427 | 0 | } |
1428 | | |
1429 | | static void chroma_dsp_init( x264_t *h ) |
1430 | 0 | { |
1431 | 0 | memcpy( h->luma2chroma_pixel, x264_luma2chroma_pixel[CHROMA_FORMAT], sizeof(h->luma2chroma_pixel) ); |
1432 | |
|
1433 | 0 | switch( CHROMA_FORMAT ) |
1434 | 0 | { |
1435 | 0 | case CHROMA_400: |
1436 | 0 | h->mc.prefetch_fenc = h->mc.prefetch_fenc_400; |
1437 | 0 | break; |
1438 | 0 | case CHROMA_420: |
1439 | 0 | memcpy( h->predict_chroma, h->predict_8x8c, sizeof(h->predict_chroma) ); |
1440 | 0 | h->mc.prefetch_fenc = h->mc.prefetch_fenc_420; |
1441 | 0 | h->loopf.deblock_chroma[0] = h->loopf.deblock_h_chroma_420; |
1442 | 0 | h->loopf.deblock_chroma_intra[0] = h->loopf.deblock_h_chroma_420_intra; |
1443 | 0 | h->loopf.deblock_chroma_mbaff = h->loopf.deblock_chroma_420_mbaff; |
1444 | 0 | h->loopf.deblock_chroma_intra_mbaff = h->loopf.deblock_chroma_420_intra_mbaff; |
1445 | 0 | h->pixf.intra_mbcmp_x3_chroma = h->pixf.intra_mbcmp_x3_8x8c; |
1446 | 0 | h->quantf.coeff_last[DCT_CHROMA_DC] = h->quantf.coeff_last4; |
1447 | 0 | h->quantf.coeff_level_run[DCT_CHROMA_DC] = h->quantf.coeff_level_run4; |
1448 | 0 | break; |
1449 | 0 | case CHROMA_422: |
1450 | 0 | memcpy( h->predict_chroma, h->predict_8x16c, sizeof(h->predict_chroma) ); |
1451 | 0 | h->mc.prefetch_fenc = h->mc.prefetch_fenc_422; |
1452 | 0 | h->loopf.deblock_chroma[0] = h->loopf.deblock_h_chroma_422; |
1453 | 0 | h->loopf.deblock_chroma_intra[0] = h->loopf.deblock_h_chroma_422_intra; |
1454 | 0 | h->loopf.deblock_chroma_mbaff = h->loopf.deblock_chroma_422_mbaff; |
1455 | 0 | h->loopf.deblock_chroma_intra_mbaff = h->loopf.deblock_chroma_422_intra_mbaff; |
1456 | 0 | h->pixf.intra_mbcmp_x3_chroma = h->pixf.intra_mbcmp_x3_8x16c; |
1457 | 0 | h->quantf.coeff_last[DCT_CHROMA_DC] = h->quantf.coeff_last8; |
1458 | 0 | h->quantf.coeff_level_run[DCT_CHROMA_DC] = h->quantf.coeff_level_run8; |
1459 | 0 | break; |
1460 | 0 | case CHROMA_444: |
1461 | 0 | h->mc.prefetch_fenc = h->mc.prefetch_fenc_422; /* FIXME: doesn't cover V plane */ |
1462 | 0 | h->loopf.deblock_chroma_mbaff = h->loopf.deblock_luma_mbaff; |
1463 | 0 | h->loopf.deblock_chroma_intra_mbaff = h->loopf.deblock_luma_intra_mbaff; |
1464 | 0 | break; |
1465 | 0 | } |
1466 | 0 | } |
1467 | | |
1468 | | static void set_aspect_ratio( x264_t *h, x264_param_t *param, int initial ) |
1469 | 0 | { |
1470 | | /* VUI */ |
1471 | 0 | if( param->vui.i_sar_width > 0 && param->vui.i_sar_height > 0 ) |
1472 | 0 | { |
1473 | 0 | uint32_t i_w = param->vui.i_sar_width; |
1474 | 0 | uint32_t i_h = param->vui.i_sar_height; |
1475 | 0 | uint32_t old_w = h->param.vui.i_sar_width; |
1476 | 0 | uint32_t old_h = h->param.vui.i_sar_height; |
1477 | |
|
1478 | 0 | x264_reduce_fraction( &i_w, &i_h ); |
1479 | |
|
1480 | 0 | while( i_w > 65535 || i_h > 65535 ) |
1481 | 0 | { |
1482 | 0 | i_w /= 2; |
1483 | 0 | i_h /= 2; |
1484 | 0 | } |
1485 | |
|
1486 | 0 | x264_reduce_fraction( &i_w, &i_h ); |
1487 | |
|
1488 | 0 | if( i_w != old_w || i_h != old_h || initial ) |
1489 | 0 | { |
1490 | 0 | h->param.vui.i_sar_width = 0; |
1491 | 0 | h->param.vui.i_sar_height = 0; |
1492 | 0 | if( i_w == 0 || i_h == 0 ) |
1493 | 0 | x264_log( h, X264_LOG_WARNING, "cannot create valid sample aspect ratio\n" ); |
1494 | 0 | else |
1495 | 0 | { |
1496 | 0 | x264_log( h, initial?X264_LOG_INFO:X264_LOG_DEBUG, "using SAR=%d/%d\n", i_w, i_h ); |
1497 | 0 | h->param.vui.i_sar_width = i_w; |
1498 | 0 | h->param.vui.i_sar_height = i_h; |
1499 | 0 | } |
1500 | 0 | } |
1501 | 0 | } |
1502 | 0 | } |
1503 | | |
1504 | | /**************************************************************************** |
1505 | | * x264_encoder_open: |
1506 | | ****************************************************************************/ |
1507 | | x264_t *x264_encoder_open( x264_param_t *param, void *api ) |
1508 | 0 | { |
1509 | 0 | x264_t *h; |
1510 | 0 | char buf[1000], *p; |
1511 | 0 | int i_slicetype_length; |
1512 | |
|
1513 | 0 | CHECKED_MALLOCZERO( h, sizeof(x264_t) ); |
1514 | | |
1515 | | /* Create a copy of param */ |
1516 | 0 | memcpy( &h->param, param, sizeof(x264_param_t) ); |
1517 | 0 | h->param.opaque = NULL; |
1518 | 0 | h->param.param_free = NULL; |
1519 | |
|
1520 | 0 | if( h->param.psz_cqm_file ) |
1521 | 0 | CHECKED_PARAM_STRDUP( h->param.psz_cqm_file, &h->param, h->param.psz_cqm_file ); |
1522 | 0 | if( h->param.psz_dump_yuv ) |
1523 | 0 | CHECKED_PARAM_STRDUP( h->param.psz_dump_yuv, &h->param, h->param.psz_dump_yuv ); |
1524 | 0 | if( h->param.rc.psz_stat_out ) |
1525 | 0 | CHECKED_PARAM_STRDUP( h->param.rc.psz_stat_out, &h->param, h->param.rc.psz_stat_out ); |
1526 | 0 | if( h->param.rc.psz_stat_in ) |
1527 | 0 | CHECKED_PARAM_STRDUP( h->param.rc.psz_stat_in, &h->param, h->param.rc.psz_stat_in ); |
1528 | 0 | if( h->param.rc.psz_zones ) |
1529 | 0 | CHECKED_PARAM_STRDUP( h->param.rc.psz_zones, &h->param, h->param.rc.psz_zones ); |
1530 | 0 | if( h->param.psz_clbin_file ) |
1531 | 0 | CHECKED_PARAM_STRDUP( h->param.psz_clbin_file, &h->param, h->param.psz_clbin_file ); |
1532 | | |
1533 | 0 | if( param->param_free ) |
1534 | 0 | { |
1535 | 0 | x264_param_cleanup( param ); |
1536 | 0 | param->param_free( param ); |
1537 | 0 | } |
1538 | | |
1539 | | /* Save pointer to bit depth independent interface */ |
1540 | 0 | h->api = api; |
1541 | |
|
1542 | | #if HAVE_INTEL_DISPATCHER |
1543 | | x264_intel_dispatcher_override(); |
1544 | | #endif |
1545 | |
|
1546 | 0 | if( x264_threading_init() ) |
1547 | 0 | { |
1548 | 0 | x264_log( h, X264_LOG_ERROR, "unable to initialize threading\n" ); |
1549 | 0 | goto fail; |
1550 | 0 | } |
1551 | | |
1552 | 0 | if( validate_parameters( h, 1 ) < 0 ) |
1553 | 0 | goto fail; |
1554 | | |
1555 | 0 | if( h->param.psz_cqm_file ) |
1556 | 0 | if( x264_cqm_parse_file( h, h->param.psz_cqm_file ) < 0 ) |
1557 | 0 | goto fail; |
1558 | | |
1559 | 0 | x264_reduce_fraction( &h->param.i_fps_num, &h->param.i_fps_den ); |
1560 | 0 | x264_reduce_fraction( &h->param.i_timebase_num, &h->param.i_timebase_den ); |
1561 | | |
1562 | | /* Init x264_t */ |
1563 | 0 | h->i_frame = -1; |
1564 | 0 | h->i_frame_num = 0; |
1565 | |
|
1566 | 0 | if( h->param.i_avcintra_class ) |
1567 | 0 | h->i_idr_pic_id = h->param.i_avcintra_class > 200 ? 4 : 5; |
1568 | 0 | else |
1569 | 0 | h->i_idr_pic_id = 0; |
1570 | |
|
1571 | 0 | if( (uint64_t)h->param.i_timebase_den * 2 > UINT32_MAX ) |
1572 | 0 | { |
1573 | 0 | x264_log( h, X264_LOG_ERROR, "Effective timebase denominator %u exceeds H.264 maximum\n", h->param.i_timebase_den ); |
1574 | 0 | goto fail; |
1575 | 0 | } |
1576 | | |
1577 | 0 | set_aspect_ratio( h, &h->param, 1 ); |
1578 | |
|
1579 | 0 | x264_sps_init( h->sps, h->param.i_sps_id, &h->param ); |
1580 | 0 | x264_sps_init_scaling_list( h->sps, &h->param ); |
1581 | 0 | x264_pps_init( h->pps, h->param.i_sps_id, &h->param, h->sps ); |
1582 | |
|
1583 | 0 | x264_validate_levels( h, 1 ); |
1584 | |
|
1585 | 0 | h->chroma_qp_table = i_chroma_qp_table + 12 + h->pps->i_chroma_qp_index_offset; |
1586 | |
|
1587 | 0 | if( x264_cqm_init( h ) < 0 ) |
1588 | 0 | goto fail; |
1589 | | |
1590 | 0 | h->mb.i_mb_width = h->sps->i_mb_width; |
1591 | 0 | h->mb.i_mb_height = h->sps->i_mb_height; |
1592 | 0 | h->mb.i_mb_count = h->mb.i_mb_width * h->mb.i_mb_height; |
1593 | |
|
1594 | 0 | h->mb.chroma_h_shift = CHROMA_FORMAT == CHROMA_420 || CHROMA_FORMAT == CHROMA_422; |
1595 | 0 | h->mb.chroma_v_shift = CHROMA_FORMAT == CHROMA_420; |
1596 | | |
1597 | | /* Adaptive MBAFF and subme 0 are not supported as we require halving motion |
1598 | | * vectors during prediction, resulting in hpel mvs. |
1599 | | * The chosen solution is to make MBAFF non-adaptive in this case. */ |
1600 | 0 | h->mb.b_adaptive_mbaff = PARAM_INTERLACED && h->param.analyse.i_subpel_refine; |
1601 | | |
1602 | | /* Init frames. */ |
1603 | 0 | if( h->param.i_bframe_adaptive == X264_B_ADAPT_TRELLIS && !h->param.rc.b_stat_read ) |
1604 | 0 | h->frames.i_delay = X264_MAX(h->param.i_bframe,3)*4; |
1605 | 0 | else |
1606 | 0 | h->frames.i_delay = h->param.i_bframe; |
1607 | 0 | if( h->param.rc.b_mb_tree || h->param.rc.i_vbv_buffer_size ) |
1608 | 0 | h->frames.i_delay = X264_MAX( h->frames.i_delay, h->param.rc.i_lookahead ); |
1609 | 0 | i_slicetype_length = h->frames.i_delay; |
1610 | 0 | h->frames.i_delay += h->i_thread_frames - 1; |
1611 | 0 | h->frames.i_delay += h->param.i_sync_lookahead; |
1612 | 0 | h->frames.i_delay += h->param.b_vfr_input; |
1613 | 0 | h->frames.i_bframe_delay = h->param.i_bframe ? (h->param.i_bframe_pyramid ? 2 : 1) : 0; |
1614 | |
|
1615 | 0 | h->frames.i_max_ref0 = h->param.i_frame_reference; |
1616 | 0 | h->frames.i_max_ref1 = X264_MIN( h->sps->vui.i_num_reorder_frames, h->param.i_frame_reference ); |
1617 | 0 | h->frames.i_max_dpb = h->sps->vui.i_max_dec_frame_buffering; |
1618 | 0 | h->frames.b_have_lowres = !h->param.rc.b_stat_read |
1619 | 0 | && ( h->param.rc.i_rc_method == X264_RC_ABR |
1620 | 0 | || h->param.rc.i_rc_method == X264_RC_CRF |
1621 | 0 | || h->param.i_bframe_adaptive |
1622 | 0 | || h->param.i_scenecut_threshold |
1623 | 0 | || h->param.rc.b_mb_tree |
1624 | 0 | || h->param.analyse.i_weighted_pred ); |
1625 | 0 | h->frames.b_have_lowres |= h->param.rc.b_stat_read && h->param.rc.i_vbv_buffer_size > 0; |
1626 | 0 | h->frames.b_have_sub8x8_esa = !!(h->param.analyse.inter & X264_ANALYSE_PSUB8x8); |
1627 | |
|
1628 | 0 | h->frames.i_last_idr = |
1629 | 0 | h->frames.i_last_keyframe = - h->param.i_keyint_max; |
1630 | 0 | h->frames.i_input = 0; |
1631 | 0 | h->frames.i_largest_pts = h->frames.i_second_largest_pts = -1; |
1632 | 0 | h->frames.i_poc_last_open_gop = -1; |
1633 | |
|
1634 | 0 | CHECKED_MALLOCZERO( h->cost_table, sizeof(*h->cost_table) ); |
1635 | 0 | CHECKED_MALLOCZERO( h->frames.unused[0], (h->frames.i_delay + 3) * sizeof(x264_frame_t *) ); |
1636 | | /* Allocate room for max refs plus a few extra just in case. */ |
1637 | 0 | CHECKED_MALLOCZERO( h->frames.unused[1], (h->i_thread_frames + X264_REF_MAX + 4) * sizeof(x264_frame_t *) ); |
1638 | 0 | CHECKED_MALLOCZERO( h->frames.current, (h->param.i_sync_lookahead + h->param.i_bframe |
1639 | 0 | + h->i_thread_frames + 3) * sizeof(x264_frame_t *) ); |
1640 | 0 | if( h->param.analyse.i_weighted_pred > 0 ) |
1641 | 0 | CHECKED_MALLOCZERO( h->frames.blank_unused, h->i_thread_frames * 4 * sizeof(x264_frame_t *) ); |
1642 | 0 | h->i_ref[0] = h->i_ref[1] = 0; |
1643 | 0 | h->i_cpb_delay = h->i_coded_fields = h->i_disp_fields = 0; |
1644 | 0 | h->i_prev_duration = ((uint64_t)h->param.i_fps_den * h->sps->vui.i_time_scale) / ((uint64_t)h->param.i_fps_num * h->sps->vui.i_num_units_in_tick); |
1645 | 0 | h->i_disp_fields_last_frame = -1; |
1646 | 0 | x264_rdo_init(); |
1647 | | |
1648 | | /* init CPU functions */ |
1649 | | #if (ARCH_X86 || ARCH_X86_64) && HIGH_BIT_DEPTH |
1650 | | /* FIXME: Only 8-bit has been optimized for AVX-512 so far. The few AVX-512 functions |
1651 | | * enabled in high bit-depth are insignificant and just causes potential issues with |
1652 | | * unnecessary thermal throttling and whatnot, so keep it disabled for now. */ |
1653 | 0 | h->param.cpu &= ~X264_CPU_AVX512; |
1654 | | #endif |
1655 | 0 | x264_predict_16x16_init( h->param.cpu, h->predict_16x16 ); |
1656 | 0 | x264_predict_8x8c_init( h->param.cpu, h->predict_8x8c ); |
1657 | 0 | x264_predict_8x16c_init( h->param.cpu, h->predict_8x16c ); |
1658 | 0 | x264_predict_8x8_init( h->param.cpu, h->predict_8x8, &h->predict_8x8_filter ); |
1659 | 0 | x264_predict_4x4_init( h->param.cpu, h->predict_4x4 ); |
1660 | 0 | x264_pixel_init( h->param.cpu, &h->pixf ); |
1661 | 0 | x264_dct_init( h->param.cpu, &h->dctf ); |
1662 | 0 | x264_zigzag_init( h->param.cpu, &h->zigzagf_progressive, &h->zigzagf_interlaced ); |
1663 | 0 | memcpy( &h->zigzagf, PARAM_INTERLACED ? &h->zigzagf_interlaced : &h->zigzagf_progressive, sizeof(h->zigzagf) ); |
1664 | 0 | x264_mc_init( h->param.cpu, &h->mc, h->param.b_cpu_independent ); |
1665 | 0 | x264_quant_init( h, h->param.cpu, &h->quantf ); |
1666 | 0 | x264_deblock_init( h->param.cpu, &h->loopf, PARAM_INTERLACED ); |
1667 | 0 | x264_bitstream_init( h->param.cpu, &h->bsf ); |
1668 | 0 | if( h->param.b_cabac ) |
1669 | 0 | x264_cabac_init( h ); |
1670 | 0 | else |
1671 | 0 | x264_cavlc_init( h ); |
1672 | |
|
1673 | 0 | mbcmp_init( h ); |
1674 | 0 | chroma_dsp_init( h ); |
1675 | |
|
1676 | 0 | p = buf + sprintf( buf, "using cpu capabilities:" ); |
1677 | 0 | for( int i = 0; x264_cpu_names[i].flags; i++ ) |
1678 | 0 | { |
1679 | 0 | if( !strcmp(x264_cpu_names[i].name, "SSE") |
1680 | 0 | && h->param.cpu & (X264_CPU_SSE2) ) |
1681 | 0 | continue; |
1682 | 0 | if( !strcmp(x264_cpu_names[i].name, "SSE2") |
1683 | 0 | && h->param.cpu & (X264_CPU_SSE2_IS_FAST|X264_CPU_SSE2_IS_SLOW) ) |
1684 | 0 | continue; |
1685 | 0 | if( !strcmp(x264_cpu_names[i].name, "SSE3") |
1686 | 0 | && (h->param.cpu & X264_CPU_SSSE3 || !(h->param.cpu & X264_CPU_CACHELINE_64)) ) |
1687 | 0 | continue; |
1688 | 0 | if( !strcmp(x264_cpu_names[i].name, "SSE4.1") |
1689 | 0 | && (h->param.cpu & X264_CPU_SSE42) ) |
1690 | 0 | continue; |
1691 | 0 | if( !strcmp(x264_cpu_names[i].name, "LZCNT") |
1692 | 0 | && (h->param.cpu & X264_CPU_BMI1) ) |
1693 | 0 | continue; |
1694 | 0 | if( !strcmp(x264_cpu_names[i].name, "BMI1") |
1695 | 0 | && (h->param.cpu & X264_CPU_BMI2) ) |
1696 | 0 | continue; |
1697 | 0 | if( !strcmp(x264_cpu_names[i].name, "FMA4") |
1698 | 0 | && (h->param.cpu & X264_CPU_FMA3) ) |
1699 | 0 | continue; |
1700 | 0 | if( (h->param.cpu & x264_cpu_names[i].flags) == x264_cpu_names[i].flags |
1701 | 0 | && (!i || x264_cpu_names[i].flags != x264_cpu_names[i-1].flags) ) |
1702 | 0 | p += sprintf( p, " %s", x264_cpu_names[i].name ); |
1703 | 0 | } |
1704 | 0 | if( !h->param.cpu ) |
1705 | 0 | p += sprintf( p, " none!" ); |
1706 | 0 | x264_log( h, X264_LOG_INFO, "%s\n", buf ); |
1707 | |
|
1708 | 0 | if( x264_analyse_init_costs( h ) ) |
1709 | 0 | goto fail; |
1710 | | |
1711 | | /* Must be volatile or else GCC will optimize it out. */ |
1712 | 0 | volatile int temp = 392; |
1713 | 0 | if( x264_clz( temp ) != 23 ) |
1714 | 0 | { |
1715 | 0 | x264_log( h, X264_LOG_ERROR, "CLZ test failed: x264 has been miscompiled!\n" ); |
1716 | 0 | #if ARCH_X86 || ARCH_X86_64 |
1717 | 0 | x264_log( h, X264_LOG_ERROR, "Are you attempting to run an SSE4a/LZCNT-targeted build on a CPU that\n" ); |
1718 | 0 | x264_log( h, X264_LOG_ERROR, "doesn't support it?\n" ); |
1719 | 0 | #endif |
1720 | 0 | goto fail; |
1721 | 0 | } |
1722 | | |
1723 | 0 | h->out.i_nal = 0; |
1724 | 0 | h->out.i_bitstream = x264_clip3f( |
1725 | 0 | h->param.i_width * h->param.i_height * 4 |
1726 | 0 | * ( h->param.rc.i_rc_method == X264_RC_ABR |
1727 | 0 | ? pow( 0.95, h->param.rc.i_qp_min ) |
1728 | 0 | : pow( 0.95, h->param.rc.i_qp_constant ) * X264_MAX( 1, h->param.rc.f_ip_factor ) ), |
1729 | 0 | 1000000, INT_MAX/3 |
1730 | 0 | ); |
1731 | |
|
1732 | 0 | h->nal_buffer_size = h->out.i_bitstream * 3/2 + 4 + 64; /* +4 for startcode, +64 for nal_escape assembly padding */ |
1733 | 0 | CHECKED_MALLOC( h->nal_buffer, h->nal_buffer_size ); |
1734 | | |
1735 | 0 | CHECKED_MALLOC( h->reconfig_h, sizeof(x264_t) ); |
1736 | | |
1737 | 0 | if( h->param.i_threads > 1 && |
1738 | 0 | x264_threadpool_init( &h->threadpool, h->param.i_threads ) ) |
1739 | 0 | goto fail; |
1740 | 0 | if( h->param.i_lookahead_threads > 1 && |
1741 | 0 | x264_threadpool_init( &h->lookaheadpool, h->param.i_lookahead_threads ) ) |
1742 | 0 | goto fail; |
1743 | | |
1744 | | #if HAVE_OPENCL |
1745 | | if( h->param.b_opencl ) |
1746 | | { |
1747 | | h->opencl.ocl = x264_opencl_load_library(); |
1748 | | if( !h->opencl.ocl ) |
1749 | | { |
1750 | | x264_log( h, X264_LOG_WARNING, "failed to load OpenCL\n" ); |
1751 | | h->param.b_opencl = 0; |
1752 | | } |
1753 | | } |
1754 | | #endif |
1755 | | |
1756 | 0 | h->thread[0] = h; |
1757 | 0 | for( int i = 1; i < h->param.i_threads + !!h->param.i_sync_lookahead; i++ ) |
1758 | 0 | CHECKED_MALLOC( h->thread[i], sizeof(x264_t) ); |
1759 | 0 | if( h->param.i_lookahead_threads > 1 ) |
1760 | 0 | for( int i = 0; i < h->param.i_lookahead_threads; i++ ) |
1761 | 0 | { |
1762 | 0 | CHECKED_MALLOC( h->lookahead_thread[i], sizeof(x264_t) ); |
1763 | 0 | *h->lookahead_thread[i] = *h; |
1764 | 0 | } |
1765 | 0 | *h->reconfig_h = *h; |
1766 | |
|
1767 | 0 | for( int i = 0; i < h->param.i_threads; i++ ) |
1768 | 0 | { |
1769 | 0 | int init_nal_count = h->param.i_slice_count + 3; |
1770 | 0 | int allocate_threadlocal_data = !h->param.b_sliced_threads || !i; |
1771 | 0 | if( i > 0 ) |
1772 | 0 | *h->thread[i] = *h; |
1773 | |
|
1774 | 0 | if( x264_pthread_mutex_init( &h->thread[i]->mutex, NULL ) ) |
1775 | 0 | goto fail; |
1776 | 0 | if( x264_pthread_cond_init( &h->thread[i]->cv, NULL ) ) |
1777 | 0 | goto fail; |
1778 | | |
1779 | 0 | if( allocate_threadlocal_data ) |
1780 | 0 | { |
1781 | 0 | h->thread[i]->fdec = x264_frame_pop_unused( h, 1 ); |
1782 | 0 | if( !h->thread[i]->fdec ) |
1783 | 0 | goto fail; |
1784 | 0 | } |
1785 | 0 | else |
1786 | 0 | h->thread[i]->fdec = h->thread[0]->fdec; |
1787 | | |
1788 | 0 | CHECKED_MALLOC( h->thread[i]->out.p_bitstream, h->out.i_bitstream ); |
1789 | | /* Start each thread with room for init_nal_count NAL units; it'll realloc later if needed. */ |
1790 | 0 | CHECKED_MALLOC( h->thread[i]->out.nal, init_nal_count*sizeof(x264_nal_t) ); |
1791 | 0 | h->thread[i]->out.i_nals_allocated = init_nal_count; |
1792 | |
|
1793 | 0 | if( allocate_threadlocal_data && x264_macroblock_cache_allocate( h->thread[i] ) < 0 ) |
1794 | 0 | goto fail; |
1795 | 0 | } |
1796 | | |
1797 | | #if HAVE_OPENCL |
1798 | | if( h->param.b_opencl && x264_opencl_lookahead_init( h ) < 0 ) |
1799 | | h->param.b_opencl = 0; |
1800 | | #endif |
1801 | | |
1802 | 0 | if( x264_lookahead_init( h, i_slicetype_length ) ) |
1803 | 0 | goto fail; |
1804 | | |
1805 | 0 | for( int i = 0; i < h->param.i_threads; i++ ) |
1806 | 0 | if( x264_macroblock_thread_allocate( h->thread[i], 0 ) < 0 ) |
1807 | 0 | goto fail; |
1808 | | |
1809 | 0 | if( x264_ratecontrol_new( h ) < 0 ) |
1810 | 0 | goto fail; |
1811 | | |
1812 | 0 | if( h->param.i_nal_hrd ) |
1813 | 0 | { |
1814 | 0 | x264_log( h, X264_LOG_DEBUG, "HRD bitrate: %i bits/sec\n", h->sps->vui.hrd.i_bit_rate_unscaled ); |
1815 | 0 | x264_log( h, X264_LOG_DEBUG, "CPB size: %i bits\n", h->sps->vui.hrd.i_cpb_size_unscaled ); |
1816 | 0 | } |
1817 | |
|
1818 | 0 | if( h->param.psz_dump_yuv ) |
1819 | 0 | { |
1820 | | /* create or truncate the reconstructed video file */ |
1821 | 0 | FILE *f = x264_fopen( h->param.psz_dump_yuv, "w" ); |
1822 | 0 | if( !f ) |
1823 | 0 | { |
1824 | 0 | x264_log( h, X264_LOG_ERROR, "dump_yuv: can't write to %s\n", h->param.psz_dump_yuv ); |
1825 | 0 | goto fail; |
1826 | 0 | } |
1827 | 0 | else if( !x264_is_regular_file( f ) ) |
1828 | 0 | { |
1829 | 0 | x264_log( h, X264_LOG_ERROR, "dump_yuv: incompatible with non-regular file %s\n", h->param.psz_dump_yuv ); |
1830 | 0 | fclose( f ); |
1831 | 0 | goto fail; |
1832 | 0 | } |
1833 | 0 | fclose( f ); |
1834 | 0 | } |
1835 | | |
1836 | 0 | const char *profile = h->sps->i_profile_idc == PROFILE_BASELINE ? "Constrained Baseline" : |
1837 | 0 | h->sps->i_profile_idc == PROFILE_MAIN ? "Main" : |
1838 | 0 | h->sps->i_profile_idc == PROFILE_HIGH ? "High" : |
1839 | 0 | h->sps->i_profile_idc == PROFILE_HIGH10 ? |
1840 | 0 | (h->sps->b_constraint_set3 ? "High 10 Intra" : "High 10") : |
1841 | 0 | h->sps->i_profile_idc == PROFILE_HIGH422 ? |
1842 | 0 | (h->sps->b_constraint_set3 ? "High 4:2:2 Intra" : "High 4:2:2") : |
1843 | 0 | h->sps->b_constraint_set3 ? "High 4:4:4 Intra" : "High 4:4:4 Predictive"; |
1844 | 0 | char level[16]; |
1845 | 0 | if( h->sps->i_level_idc == 9 || ( h->sps->i_level_idc == 11 && h->sps->b_constraint_set3 && |
1846 | 0 | (h->sps->i_profile_idc == PROFILE_BASELINE || h->sps->i_profile_idc == PROFILE_MAIN) ) ) |
1847 | 0 | strcpy( level, "1b" ); |
1848 | 0 | else |
1849 | 0 | snprintf( level, sizeof(level), "%d.%d", h->sps->i_level_idc / 10, h->sps->i_level_idc % 10 ); |
1850 | |
|
1851 | 0 | static const char * const subsampling[4] = { "4:0:0", "4:2:0", "4:2:2", "4:4:4" }; |
1852 | 0 | x264_log( h, X264_LOG_INFO, "profile %s, level %s, %s, %d-bit\n", |
1853 | 0 | profile, level, subsampling[CHROMA_FORMAT], BIT_DEPTH ); |
1854 | |
|
1855 | 0 | return h; |
1856 | 0 | fail: |
1857 | 0 | x264_free( h ); |
1858 | 0 | return NULL; |
1859 | 0 | } Unexecuted instantiation: x264_8_encoder_open Unexecuted instantiation: x264_10_encoder_open |
1860 | | |
1861 | | /****************************************************************************/ |
1862 | | static int encoder_try_reconfig( x264_t *h, x264_param_t *param, int *rc_reconfig ) |
1863 | 0 | { |
1864 | 0 | *rc_reconfig = 0; |
1865 | 0 | set_aspect_ratio( h, param, 0 ); |
1866 | 0 | #define COPY(var) h->param.var = param->var |
1867 | 0 | COPY( i_frame_reference ); // but never uses more refs than initially specified |
1868 | 0 | COPY( i_bframe_bias ); |
1869 | 0 | if( h->param.i_scenecut_threshold ) |
1870 | 0 | COPY( i_scenecut_threshold ); // can't turn it on or off, only vary the threshold |
1871 | 0 | COPY( b_deblocking_filter ); |
1872 | 0 | COPY( i_deblocking_filter_alphac0 ); |
1873 | 0 | COPY( i_deblocking_filter_beta ); |
1874 | 0 | COPY( i_frame_packing ); |
1875 | 0 | COPY( mastering_display ); |
1876 | 0 | COPY( content_light_level ); |
1877 | 0 | COPY( i_alternative_transfer ); |
1878 | 0 | COPY( analyse.inter ); |
1879 | 0 | COPY( analyse.intra ); |
1880 | 0 | COPY( analyse.i_direct_mv_pred ); |
1881 | | /* Scratch buffer prevents me_range from being increased for esa/tesa */ |
1882 | 0 | if( h->param.analyse.i_me_method < X264_ME_ESA || param->analyse.i_me_range < h->param.analyse.i_me_range ) |
1883 | 0 | COPY( analyse.i_me_range ); |
1884 | 0 | COPY( analyse.i_noise_reduction ); |
1885 | | /* We can't switch out of subme=0 during encoding. */ |
1886 | 0 | if( h->param.analyse.i_subpel_refine ) |
1887 | 0 | COPY( analyse.i_subpel_refine ); |
1888 | 0 | COPY( analyse.i_trellis ); |
1889 | 0 | COPY( analyse.b_chroma_me ); |
1890 | 0 | COPY( analyse.b_dct_decimate ); |
1891 | 0 | COPY( analyse.b_fast_pskip ); |
1892 | 0 | COPY( analyse.b_mixed_references ); |
1893 | 0 | COPY( analyse.f_psy_rd ); |
1894 | 0 | COPY( analyse.f_psy_trellis ); |
1895 | 0 | COPY( crop_rect ); |
1896 | | // can only twiddle these if they were enabled to begin with: |
1897 | 0 | if( h->param.analyse.i_me_method >= X264_ME_ESA || param->analyse.i_me_method < X264_ME_ESA ) |
1898 | 0 | COPY( analyse.i_me_method ); |
1899 | 0 | if( h->param.analyse.i_me_method >= X264_ME_ESA && !h->frames.b_have_sub8x8_esa ) |
1900 | 0 | h->param.analyse.inter &= ~X264_ANALYSE_PSUB8x8; |
1901 | 0 | if( h->pps->b_transform_8x8_mode ) |
1902 | 0 | COPY( analyse.b_transform_8x8 ); |
1903 | 0 | if( h->frames.i_max_ref1 > 1 ) |
1904 | 0 | COPY( i_bframe_pyramid ); |
1905 | 0 | COPY( i_slice_max_size ); |
1906 | 0 | COPY( i_slice_max_mbs ); |
1907 | 0 | COPY( i_slice_min_mbs ); |
1908 | 0 | COPY( i_slice_count ); |
1909 | 0 | COPY( i_slice_count_max ); |
1910 | 0 | COPY( b_tff ); |
1911 | | |
1912 | | /* VBV can't be turned on if it wasn't on to begin with */ |
1913 | 0 | if( h->param.rc.i_vbv_max_bitrate > 0 && h->param.rc.i_vbv_buffer_size > 0 && |
1914 | 0 | param->rc.i_vbv_max_bitrate > 0 && param->rc.i_vbv_buffer_size > 0 ) |
1915 | 0 | { |
1916 | 0 | *rc_reconfig |= h->param.rc.i_vbv_max_bitrate != param->rc.i_vbv_max_bitrate; |
1917 | 0 | *rc_reconfig |= h->param.rc.i_vbv_buffer_size != param->rc.i_vbv_buffer_size; |
1918 | 0 | *rc_reconfig |= h->param.rc.i_bitrate != param->rc.i_bitrate; |
1919 | 0 | COPY( rc.i_vbv_max_bitrate ); |
1920 | 0 | COPY( rc.i_vbv_buffer_size ); |
1921 | 0 | COPY( rc.i_bitrate ); |
1922 | 0 | } |
1923 | 0 | *rc_reconfig |= h->param.rc.f_rf_constant != param->rc.f_rf_constant; |
1924 | 0 | *rc_reconfig |= h->param.rc.f_rf_constant_max != param->rc.f_rf_constant_max; |
1925 | 0 | COPY( rc.f_rf_constant ); |
1926 | 0 | COPY( rc.f_rf_constant_max ); |
1927 | 0 | #undef COPY |
1928 | |
|
1929 | 0 | return validate_parameters( h, 0 ); |
1930 | 0 | } |
1931 | | |
1932 | | int x264_encoder_reconfig_apply( x264_t *h, x264_param_t *param ) |
1933 | 0 | { |
1934 | 0 | int rc_reconfig; |
1935 | 0 | int ret = encoder_try_reconfig( h, param, &rc_reconfig ); |
1936 | |
|
1937 | 0 | mbcmp_init( h ); |
1938 | 0 | if( !ret ) |
1939 | 0 | x264_sps_init_reconfigurable( h->sps, &h->param ); |
1940 | | |
1941 | | /* Supported reconfiguration options (1-pass only): |
1942 | | * vbv-maxrate |
1943 | | * vbv-bufsize |
1944 | | * crf |
1945 | | * bitrate (CBR only) */ |
1946 | 0 | if( !ret && rc_reconfig ) |
1947 | 0 | x264_ratecontrol_init_reconfigurable( h, 0 ); |
1948 | |
|
1949 | 0 | return ret; |
1950 | 0 | } Unexecuted instantiation: x264_8_encoder_reconfig_apply Unexecuted instantiation: x264_10_encoder_reconfig_apply |
1951 | | |
1952 | | /**************************************************************************** |
1953 | | * x264_encoder_reconfig: |
1954 | | ****************************************************************************/ |
1955 | | int x264_encoder_reconfig( x264_t *h, x264_param_t *param ) |
1956 | 0 | { |
1957 | 0 | h = h->thread[h->thread[0]->i_thread_phase]; |
1958 | 0 | x264_param_t param_save = h->reconfig_h->param; |
1959 | 0 | h->reconfig_h->param = h->param; |
1960 | |
|
1961 | 0 | int rc_reconfig; |
1962 | 0 | int ret = encoder_try_reconfig( h->reconfig_h, param, &rc_reconfig ); |
1963 | 0 | if( !ret ) |
1964 | 0 | h->reconfig = 1; |
1965 | 0 | else |
1966 | 0 | h->reconfig_h->param = param_save; |
1967 | |
|
1968 | 0 | return ret; |
1969 | 0 | } Unexecuted instantiation: x264_8_encoder_reconfig Unexecuted instantiation: x264_10_encoder_reconfig |
1970 | | |
1971 | | /**************************************************************************** |
1972 | | * x264_encoder_parameters: |
1973 | | ****************************************************************************/ |
1974 | | void x264_encoder_parameters( x264_t *h, x264_param_t *param ) |
1975 | 0 | { |
1976 | 0 | memcpy( param, &h->thread[h->i_thread_phase]->param, sizeof(x264_param_t) ); |
1977 | 0 | param->opaque = NULL; |
1978 | 0 | } Unexecuted instantiation: x264_8_encoder_parameters Unexecuted instantiation: x264_10_encoder_parameters |
1979 | | |
1980 | | /* internal usage */ |
1981 | | static void nal_start( x264_t *h, int i_type, int i_ref_idc ) |
1982 | 0 | { |
1983 | 0 | x264_nal_t *nal = &h->out.nal[h->out.i_nal]; |
1984 | |
|
1985 | 0 | nal->i_ref_idc = i_ref_idc; |
1986 | 0 | nal->i_type = i_type; |
1987 | 0 | nal->b_long_startcode = 1; |
1988 | |
|
1989 | 0 | nal->i_payload= 0; |
1990 | 0 | nal->p_payload= &h->out.p_bitstream[bs_pos( &h->out.bs ) / 8]; |
1991 | 0 | nal->i_padding= 0; |
1992 | 0 | } |
1993 | | |
1994 | | /* if number of allocated nals is not enough, re-allocate a larger one. */ |
1995 | | static int nal_check_buffer( x264_t *h ) |
1996 | 0 | { |
1997 | 0 | if( h->out.i_nal >= h->out.i_nals_allocated ) |
1998 | 0 | { |
1999 | 0 | x264_nal_t *new_out = x264_malloc( sizeof(x264_nal_t) * (h->out.i_nals_allocated*2) ); |
2000 | 0 | if( !new_out ) |
2001 | 0 | return -1; |
2002 | 0 | memcpy( new_out, h->out.nal, sizeof(x264_nal_t) * (h->out.i_nals_allocated) ); |
2003 | 0 | x264_free( h->out.nal ); |
2004 | 0 | h->out.nal = new_out; |
2005 | 0 | h->out.i_nals_allocated *= 2; |
2006 | 0 | } |
2007 | 0 | return 0; |
2008 | 0 | } |
2009 | | |
2010 | | static int nal_end( x264_t *h ) |
2011 | 0 | { |
2012 | 0 | x264_nal_t *nal = &h->out.nal[h->out.i_nal]; |
2013 | 0 | uint8_t *end = &h->out.p_bitstream[bs_pos( &h->out.bs ) / 8]; |
2014 | 0 | nal->i_payload = end - nal->p_payload; |
2015 | | /* Assembly implementation of nal_escape reads past the end of the input. |
2016 | | * While undefined padding wouldn't actually affect the output, it makes valgrind unhappy. */ |
2017 | 0 | memset( end, 0xff, 64 ); |
2018 | 0 | if( h->param.nalu_process ) |
2019 | 0 | h->param.nalu_process( (x264_t *)h->api, nal, h->fenc->opaque ); |
2020 | 0 | h->out.i_nal++; |
2021 | |
|
2022 | 0 | return nal_check_buffer( h ); |
2023 | 0 | } |
2024 | | |
2025 | | static int check_encapsulated_buffer( x264_t *h, x264_t *h0, int start, |
2026 | | int64_t previous_nal_size, int64_t necessary_size ) |
2027 | 0 | { |
2028 | 0 | if( h0->nal_buffer_size < necessary_size ) |
2029 | 0 | { |
2030 | 0 | necessary_size *= 2; |
2031 | 0 | if( necessary_size > INT_MAX ) |
2032 | 0 | return -1; |
2033 | 0 | uint8_t *buf = x264_malloc( necessary_size ); |
2034 | 0 | if( !buf ) |
2035 | 0 | return -1; |
2036 | 0 | if( previous_nal_size ) |
2037 | 0 | memcpy( buf, h0->nal_buffer, previous_nal_size ); |
2038 | |
|
2039 | 0 | intptr_t delta = buf - h0->nal_buffer; |
2040 | 0 | for( int i = 0; i < start; i++ ) |
2041 | 0 | h->out.nal[i].p_payload += delta; |
2042 | |
|
2043 | 0 | x264_free( h0->nal_buffer ); |
2044 | 0 | h0->nal_buffer = buf; |
2045 | 0 | h0->nal_buffer_size = necessary_size; |
2046 | 0 | } |
2047 | | |
2048 | 0 | return 0; |
2049 | 0 | } |
2050 | | |
2051 | | static int encoder_encapsulate_nals( x264_t *h, int start ) |
2052 | 0 | { |
2053 | 0 | x264_t *h0 = h->thread[0]; |
2054 | 0 | int64_t nal_size = 0, previous_nal_size = 0; |
2055 | |
|
2056 | 0 | if( h->param.nalu_process ) |
2057 | 0 | { |
2058 | 0 | for( int i = start; i < h->out.i_nal; i++ ) |
2059 | 0 | nal_size += h->out.nal[i].i_payload; |
2060 | 0 | if( nal_size > INT_MAX ) |
2061 | 0 | return -1; |
2062 | 0 | return nal_size; |
2063 | 0 | } |
2064 | | |
2065 | 0 | for( int i = 0; i < start; i++ ) |
2066 | 0 | previous_nal_size += h->out.nal[i].i_payload; |
2067 | |
|
2068 | 0 | for( int i = start; i < h->out.i_nal; i++ ) |
2069 | 0 | nal_size += h->out.nal[i].i_payload; |
2070 | | |
2071 | | /* Worst-case NAL unit escaping: reallocate the buffer if it's too small. */ |
2072 | 0 | int64_t necessary_size = previous_nal_size + nal_size * 3/2 + h->out.i_nal * 4 + 4 + 64; |
2073 | 0 | for( int i = start; i < h->out.i_nal; i++ ) |
2074 | 0 | necessary_size += h->out.nal[i].i_padding; |
2075 | 0 | if( check_encapsulated_buffer( h, h0, start, previous_nal_size, necessary_size ) ) |
2076 | 0 | return -1; |
2077 | | |
2078 | 0 | uint8_t *nal_buffer = h0->nal_buffer + previous_nal_size; |
2079 | |
|
2080 | 0 | for( int i = start; i < h->out.i_nal; i++ ) |
2081 | 0 | { |
2082 | 0 | h->out.nal[i].b_long_startcode = !i || h->out.nal[i].i_type == NAL_SPS || h->out.nal[i].i_type == NAL_PPS || |
2083 | 0 | h->param.i_avcintra_class; |
2084 | 0 | x264_nal_encode( h, nal_buffer, &h->out.nal[i] ); |
2085 | 0 | nal_buffer += h->out.nal[i].i_payload; |
2086 | 0 | } |
2087 | |
|
2088 | 0 | x264_emms(); |
2089 | |
|
2090 | 0 | return nal_buffer - (h0->nal_buffer + previous_nal_size); |
2091 | 0 | } |
2092 | | |
2093 | | /**************************************************************************** |
2094 | | * x264_encoder_headers: |
2095 | | ****************************************************************************/ |
2096 | | int x264_encoder_headers( x264_t *h, x264_nal_t **pp_nal, int *pi_nal ) |
2097 | 0 | { |
2098 | 0 | int frame_size = 0; |
2099 | | /* init bitstream context */ |
2100 | 0 | h->out.i_nal = 0; |
2101 | 0 | bs_init( &h->out.bs, h->out.p_bitstream, h->out.i_bitstream ); |
2102 | | |
2103 | | /* Write SEI, SPS and PPS. */ |
2104 | | |
2105 | | /* generate sequence parameters */ |
2106 | 0 | nal_start( h, NAL_SPS, NAL_PRIORITY_HIGHEST ); |
2107 | 0 | x264_sps_write( &h->out.bs, h->sps ); |
2108 | 0 | if( nal_end( h ) ) |
2109 | 0 | return -1; |
2110 | | |
2111 | | /* generate picture parameters */ |
2112 | 0 | nal_start( h, NAL_PPS, NAL_PRIORITY_HIGHEST ); |
2113 | 0 | x264_pps_write( &h->out.bs, h->sps, h->pps ); |
2114 | 0 | if( nal_end( h ) ) |
2115 | 0 | return -1; |
2116 | | |
2117 | | /* identify ourselves */ |
2118 | 0 | nal_start( h, NAL_SEI, NAL_PRIORITY_DISPOSABLE ); |
2119 | 0 | if( x264_sei_version_write( h, &h->out.bs ) ) |
2120 | 0 | return -1; |
2121 | 0 | if( nal_end( h ) ) |
2122 | 0 | return -1; |
2123 | | |
2124 | 0 | frame_size = encoder_encapsulate_nals( h, 0 ); |
2125 | 0 | if( frame_size < 0 ) |
2126 | 0 | return -1; |
2127 | | |
2128 | | /* now set output*/ |
2129 | 0 | *pi_nal = h->out.i_nal; |
2130 | 0 | *pp_nal = &h->out.nal[0]; |
2131 | 0 | h->out.i_nal = 0; |
2132 | |
|
2133 | 0 | return frame_size; |
2134 | 0 | } Unexecuted instantiation: x264_8_encoder_headers Unexecuted instantiation: x264_10_encoder_headers |
2135 | | |
2136 | | /* Check to see whether we have chosen a reference list ordering different |
2137 | | * from the standard's default. */ |
2138 | | static inline void reference_check_reorder( x264_t *h ) |
2139 | 0 | { |
2140 | | /* The reorder check doesn't check for missing frames, so just |
2141 | | * force a reorder if one of the reference list is corrupt. */ |
2142 | 0 | for( int i = 0; h->frames.reference[i]; i++ ) |
2143 | 0 | if( h->frames.reference[i]->b_corrupt ) |
2144 | 0 | { |
2145 | 0 | h->b_ref_reorder[0] = 1; |
2146 | 0 | return; |
2147 | 0 | } |
2148 | 0 | for( int list = 0; list <= (h->sh.i_type == SLICE_TYPE_B); list++ ) |
2149 | 0 | for( int i = 0; i < h->i_ref[list] - 1; i++ ) |
2150 | 0 | { |
2151 | 0 | int framenum_diff = h->fref[list][i+1]->i_frame_num - h->fref[list][i]->i_frame_num; |
2152 | 0 | int poc_diff = h->fref[list][i+1]->i_poc - h->fref[list][i]->i_poc; |
2153 | | /* P and B-frames use different default orders. */ |
2154 | 0 | if( h->sh.i_type == SLICE_TYPE_P ? framenum_diff > 0 : list == 1 ? poc_diff < 0 : poc_diff > 0 ) |
2155 | 0 | { |
2156 | 0 | h->b_ref_reorder[list] = 1; |
2157 | 0 | return; |
2158 | 0 | } |
2159 | 0 | } |
2160 | 0 | } |
2161 | | |
2162 | | /* return -1 on failure, else return the index of the new reference frame */ |
2163 | | static int weighted_reference_duplicate( x264_t *h, int i_ref, const x264_weight_t *w ) |
2164 | 0 | { |
2165 | 0 | int i = h->i_ref[0]; |
2166 | 0 | int j = 1; |
2167 | 0 | x264_frame_t *newframe; |
2168 | 0 | if( i <= 1 ) /* empty list, definitely can't duplicate frame */ |
2169 | 0 | return -1; |
2170 | | |
2171 | | //Duplication is only used in X264_WEIGHTP_SMART |
2172 | 0 | if( h->param.analyse.i_weighted_pred != X264_WEIGHTP_SMART ) |
2173 | 0 | return -1; |
2174 | | |
2175 | | /* Duplication is a hack to compensate for crappy rounding in motion compensation. |
2176 | | * With high bit depth, it's not worth doing, so turn it off except in the case of |
2177 | | * unweighted dupes. */ |
2178 | 0 | if( BIT_DEPTH > 8 && w != x264_weight_none ) |
2179 | 0 | return -1; |
2180 | | |
2181 | 0 | newframe = x264_frame_pop_blank_unused( h ); |
2182 | 0 | if( !newframe ) |
2183 | 0 | return -1; |
2184 | | |
2185 | | //FIXME: probably don't need to copy everything |
2186 | 0 | *newframe = *h->fref[0][i_ref]; |
2187 | 0 | newframe->i_reference_count = 1; |
2188 | 0 | newframe->orig = h->fref[0][i_ref]; |
2189 | 0 | newframe->b_duplicate = 1; |
2190 | 0 | memcpy( h->fenc->weight[j], w, sizeof(h->fenc->weight[i]) ); |
2191 | | |
2192 | | /* shift the frames to make space for the dupe. */ |
2193 | 0 | h->b_ref_reorder[0] = 1; |
2194 | 0 | if( h->i_ref[0] < X264_REF_MAX ) |
2195 | 0 | ++h->i_ref[0]; |
2196 | 0 | h->fref[0][X264_REF_MAX-1] = NULL; |
2197 | 0 | x264_frame_unshift( &h->fref[0][j], newframe ); |
2198 | |
|
2199 | 0 | return j; |
2200 | 0 | } |
2201 | | |
2202 | | static void weighted_pred_init( x264_t *h ) |
2203 | 0 | { |
2204 | | /* for now no analysis and set all weights to nothing */ |
2205 | 0 | for( int i_ref = 0; i_ref < h->i_ref[0]; i_ref++ ) |
2206 | 0 | h->fenc->weighted[i_ref] = h->fref[0][i_ref]->filtered[0][0]; |
2207 | | |
2208 | | // FIXME: This only supports weighting of one reference frame |
2209 | | // and duplicates of that frame. |
2210 | 0 | h->fenc->i_lines_weighted = 0; |
2211 | |
|
2212 | 0 | for( int i_ref = 0; i_ref < (h->i_ref[0] << SLICE_MBAFF); i_ref++ ) |
2213 | 0 | for( int i = 0; i < 3; i++ ) |
2214 | 0 | h->sh.weight[i_ref][i].weightfn = NULL; |
2215 | | |
2216 | |
|
2217 | 0 | if( h->sh.i_type != SLICE_TYPE_P || h->param.analyse.i_weighted_pred <= 0 ) |
2218 | 0 | return; |
2219 | | |
2220 | 0 | int i_padv = PADV << PARAM_INTERLACED; |
2221 | 0 | int denom = -1; |
2222 | 0 | int weightplane[2] = { 0, 0 }; |
2223 | 0 | int buffer_next = 0; |
2224 | 0 | for( int i = 0; i < 3; i++ ) |
2225 | 0 | { |
2226 | 0 | for( int j = 0; j < h->i_ref[0]; j++ ) |
2227 | 0 | { |
2228 | 0 | if( h->fenc->weight[j][i].weightfn ) |
2229 | 0 | { |
2230 | 0 | h->sh.weight[j][i] = h->fenc->weight[j][i]; |
2231 | | // if weight is useless, don't write it to stream |
2232 | 0 | if( h->sh.weight[j][i].i_scale == 1<<h->sh.weight[j][i].i_denom && h->sh.weight[j][i].i_offset == 0 ) |
2233 | 0 | h->sh.weight[j][i].weightfn = NULL; |
2234 | 0 | else |
2235 | 0 | { |
2236 | 0 | if( !weightplane[!!i] ) |
2237 | 0 | { |
2238 | 0 | weightplane[!!i] = 1; |
2239 | 0 | h->sh.weight[0][!!i].i_denom = denom = h->sh.weight[j][i].i_denom; |
2240 | 0 | assert( x264_clip3( denom, 0, 7 ) == denom ); |
2241 | 0 | } |
2242 | |
|
2243 | 0 | assert( h->sh.weight[j][i].i_denom == denom ); |
2244 | 0 | if( !i ) |
2245 | 0 | { |
2246 | 0 | h->fenc->weighted[j] = h->mb.p_weight_buf[buffer_next++] + h->fenc->i_stride[0] * i_padv + PADH_ALIGN; |
2247 | | //scale full resolution frame |
2248 | 0 | if( h->param.i_threads == 1 ) |
2249 | 0 | { |
2250 | 0 | pixel *src = h->fref[0][j]->filtered[0][0] - h->fref[0][j]->i_stride[0]*i_padv - PADH_ALIGN; |
2251 | 0 | pixel *dst = h->fenc->weighted[j] - h->fenc->i_stride[0]*i_padv - PADH_ALIGN; |
2252 | 0 | int stride = h->fenc->i_stride[0]; |
2253 | 0 | int width = h->fenc->i_width[0] + PADH2; |
2254 | 0 | int height = h->fenc->i_lines[0] + i_padv*2; |
2255 | 0 | x264_weight_scale_plane( h, dst, stride, src, stride, width, height, &h->sh.weight[j][0] ); |
2256 | 0 | h->fenc->i_lines_weighted = height; |
2257 | 0 | } |
2258 | 0 | } |
2259 | 0 | } |
2260 | 0 | } |
2261 | 0 | } |
2262 | 0 | } |
2263 | |
|
2264 | 0 | if( weightplane[1] ) |
2265 | 0 | for( int i = 0; i < h->i_ref[0]; i++ ) |
2266 | 0 | { |
2267 | 0 | if( h->sh.weight[i][1].weightfn && !h->sh.weight[i][2].weightfn ) |
2268 | 0 | { |
2269 | 0 | h->sh.weight[i][2].i_scale = 1 << h->sh.weight[0][1].i_denom; |
2270 | 0 | h->sh.weight[i][2].i_offset = 0; |
2271 | 0 | } |
2272 | 0 | else if( h->sh.weight[i][2].weightfn && !h->sh.weight[i][1].weightfn ) |
2273 | 0 | { |
2274 | 0 | h->sh.weight[i][1].i_scale = 1 << h->sh.weight[0][1].i_denom; |
2275 | 0 | h->sh.weight[i][1].i_offset = 0; |
2276 | 0 | } |
2277 | 0 | } |
2278 | |
|
2279 | 0 | if( !weightplane[0] ) |
2280 | 0 | h->sh.weight[0][0].i_denom = 0; |
2281 | 0 | if( !weightplane[1] ) |
2282 | 0 | h->sh.weight[0][1].i_denom = 0; |
2283 | 0 | h->sh.weight[0][2].i_denom = h->sh.weight[0][1].i_denom; |
2284 | 0 | } |
2285 | | |
2286 | | static inline int reference_distance( x264_t *h, x264_frame_t *frame ) |
2287 | 0 | { |
2288 | 0 | if( h->param.i_frame_packing == 5 ) |
2289 | 0 | return abs((h->fenc->i_frame&~1) - (frame->i_frame&~1)) + |
2290 | 0 | ((h->fenc->i_frame&1) != (frame->i_frame&1)); |
2291 | 0 | else |
2292 | 0 | return abs(h->fenc->i_frame - frame->i_frame); |
2293 | 0 | } |
2294 | | |
2295 | | static inline void reference_build_list( x264_t *h, int i_poc ) |
2296 | 0 | { |
2297 | 0 | int b_ok; |
2298 | | |
2299 | | /* build ref list 0/1 */ |
2300 | 0 | h->mb.pic.i_fref[0] = h->i_ref[0] = 0; |
2301 | 0 | h->mb.pic.i_fref[1] = h->i_ref[1] = 0; |
2302 | 0 | if( h->sh.i_type == SLICE_TYPE_I ) |
2303 | 0 | return; |
2304 | | |
2305 | 0 | for( int i = 0; h->frames.reference[i]; i++ ) |
2306 | 0 | { |
2307 | 0 | if( h->frames.reference[i]->b_corrupt ) |
2308 | 0 | continue; |
2309 | 0 | if( h->frames.reference[i]->i_poc < i_poc ) |
2310 | 0 | h->fref[0][h->i_ref[0]++] = h->frames.reference[i]; |
2311 | 0 | else if( h->frames.reference[i]->i_poc > i_poc ) |
2312 | 0 | h->fref[1][h->i_ref[1]++] = h->frames.reference[i]; |
2313 | 0 | } |
2314 | |
|
2315 | 0 | if( h->sh.i_mmco_remove_from_end ) |
2316 | 0 | { |
2317 | | /* Order ref0 for MMCO remove */ |
2318 | 0 | do |
2319 | 0 | { |
2320 | 0 | b_ok = 1; |
2321 | 0 | for( int i = 0; i < h->i_ref[0] - 1; i++ ) |
2322 | 0 | { |
2323 | 0 | if( h->fref[0][i]->i_frame < h->fref[0][i+1]->i_frame ) |
2324 | 0 | { |
2325 | 0 | XCHG( x264_frame_t*, h->fref[0][i], h->fref[0][i+1] ); |
2326 | 0 | b_ok = 0; |
2327 | 0 | break; |
2328 | 0 | } |
2329 | 0 | } |
2330 | 0 | } while( !b_ok ); |
2331 | |
|
2332 | 0 | for( int i = h->i_ref[0]-1; i >= h->i_ref[0] - h->sh.i_mmco_remove_from_end; i-- ) |
2333 | 0 | { |
2334 | 0 | int diff = h->i_frame_num - h->fref[0][i]->i_frame_num; |
2335 | 0 | h->sh.mmco[h->sh.i_mmco_command_count].i_poc = h->fref[0][i]->i_poc; |
2336 | 0 | h->sh.mmco[h->sh.i_mmco_command_count++].i_difference_of_pic_nums = diff; |
2337 | 0 | } |
2338 | 0 | } |
2339 | | |
2340 | | /* Order reference lists by distance from the current frame. */ |
2341 | 0 | for( int list = 0; list < 2; list++ ) |
2342 | 0 | { |
2343 | 0 | h->fref_nearest[list] = h->fref[list][0]; |
2344 | 0 | do |
2345 | 0 | { |
2346 | 0 | b_ok = 1; |
2347 | 0 | for( int i = 0; i < h->i_ref[list] - 1; i++ ) |
2348 | 0 | { |
2349 | 0 | if( list ? h->fref[list][i+1]->i_poc < h->fref_nearest[list]->i_poc |
2350 | 0 | : h->fref[list][i+1]->i_poc > h->fref_nearest[list]->i_poc ) |
2351 | 0 | h->fref_nearest[list] = h->fref[list][i+1]; |
2352 | 0 | if( reference_distance( h, h->fref[list][i] ) > reference_distance( h, h->fref[list][i+1] ) ) |
2353 | 0 | { |
2354 | 0 | XCHG( x264_frame_t*, h->fref[list][i], h->fref[list][i+1] ); |
2355 | 0 | b_ok = 0; |
2356 | 0 | break; |
2357 | 0 | } |
2358 | 0 | } |
2359 | 0 | } while( !b_ok ); |
2360 | 0 | } |
2361 | |
|
2362 | 0 | reference_check_reorder( h ); |
2363 | |
|
2364 | 0 | h->i_ref[1] = X264_MIN( h->i_ref[1], h->frames.i_max_ref1 ); |
2365 | 0 | h->i_ref[0] = X264_MIN( h->i_ref[0], h->frames.i_max_ref0 ); |
2366 | 0 | h->i_ref[0] = X264_MIN( h->i_ref[0], h->param.i_frame_reference ); // if reconfig() has lowered the limit |
2367 | | |
2368 | | /* For Blu-ray compliance, don't reference frames outside of the minigop. */ |
2369 | 0 | if( IS_X264_TYPE_B( h->fenc->i_type ) && h->param.b_bluray_compat ) |
2370 | 0 | h->i_ref[0] = X264_MIN( h->i_ref[0], IS_X264_TYPE_B( h->fref[0][0]->i_type ) + 1 ); |
2371 | | |
2372 | | /* add duplicates */ |
2373 | 0 | if( h->fenc->i_type == X264_TYPE_P ) |
2374 | 0 | { |
2375 | 0 | int idx = -1; |
2376 | 0 | if( h->param.analyse.i_weighted_pred >= X264_WEIGHTP_SIMPLE ) |
2377 | 0 | { |
2378 | 0 | x264_weight_t w[3]; |
2379 | 0 | w[1].weightfn = w[2].weightfn = NULL; |
2380 | 0 | if( h->param.rc.b_stat_read ) |
2381 | 0 | x264_ratecontrol_set_weights( h, h->fenc ); |
2382 | |
|
2383 | 0 | if( !h->fenc->weight[0][0].weightfn ) |
2384 | 0 | { |
2385 | 0 | h->fenc->weight[0][0].i_denom = 0; |
2386 | 0 | SET_WEIGHT( w[0], 1, 1, 0, -1 ); |
2387 | 0 | idx = weighted_reference_duplicate( h, 0, w ); |
2388 | 0 | } |
2389 | 0 | else |
2390 | 0 | { |
2391 | 0 | if( h->fenc->weight[0][0].i_scale == 1<<h->fenc->weight[0][0].i_denom ) |
2392 | 0 | { |
2393 | 0 | SET_WEIGHT( h->fenc->weight[0][0], 1, 1, 0, h->fenc->weight[0][0].i_offset ); |
2394 | 0 | } |
2395 | 0 | weighted_reference_duplicate( h, 0, x264_weight_none ); |
2396 | 0 | if( h->fenc->weight[0][0].i_offset > -128 ) |
2397 | 0 | { |
2398 | 0 | w[0] = h->fenc->weight[0][0]; |
2399 | 0 | w[0].i_offset--; |
2400 | 0 | h->mc.weight_cache( h, &w[0] ); |
2401 | 0 | idx = weighted_reference_duplicate( h, 0, w ); |
2402 | 0 | } |
2403 | 0 | } |
2404 | 0 | } |
2405 | 0 | h->mb.ref_blind_dupe = idx; |
2406 | 0 | } |
2407 | |
|
2408 | 0 | assert( h->i_ref[0] + h->i_ref[1] <= X264_REF_MAX ); |
2409 | 0 | h->mb.pic.i_fref[0] = h->i_ref[0]; |
2410 | 0 | h->mb.pic.i_fref[1] = h->i_ref[1]; |
2411 | 0 | } |
2412 | | |
2413 | | static void fdec_filter_row( x264_t *h, int mb_y, int pass ) |
2414 | 0 | { |
2415 | | /* mb_y is the mb to be encoded next, not the mb to be filtered here */ |
2416 | 0 | int b_hpel = h->fdec->b_kept_as_ref; |
2417 | 0 | int b_deblock = h->sh.i_disable_deblocking_filter_idc != 1; |
2418 | 0 | int b_end = mb_y == h->i_threadslice_end; |
2419 | 0 | int b_measure_quality = 1; |
2420 | 0 | int min_y = mb_y - (1 << SLICE_MBAFF); |
2421 | 0 | int b_start = min_y == h->i_threadslice_start; |
2422 | | /* Even in interlaced mode, deblocking never modifies more than 4 pixels |
2423 | | * above each MB, as bS=4 doesn't happen for the top of interlaced mbpairs. */ |
2424 | 0 | int minpix_y = min_y*16 - 4 * !b_start; |
2425 | 0 | int maxpix_y = mb_y*16 - 4 * !b_end; |
2426 | 0 | b_deblock &= b_hpel || h->param.b_full_recon || h->param.psz_dump_yuv; |
2427 | 0 | if( h->param.b_sliced_threads ) |
2428 | 0 | { |
2429 | 0 | switch( pass ) |
2430 | 0 | { |
2431 | | /* During encode: only do deblock if asked for */ |
2432 | 0 | default: |
2433 | 0 | case 0: |
2434 | 0 | b_deblock &= h->param.b_full_recon; |
2435 | 0 | b_hpel = 0; |
2436 | 0 | break; |
2437 | | /* During post-encode pass: do deblock if not done yet, do hpel for all |
2438 | | * rows except those between slices. */ |
2439 | 0 | case 1: |
2440 | 0 | b_deblock &= !h->param.b_full_recon; |
2441 | 0 | b_hpel &= !(b_start && min_y > 0); |
2442 | 0 | b_measure_quality = 0; |
2443 | 0 | break; |
2444 | | /* Final pass: do the rows between slices in sequence. */ |
2445 | 0 | case 2: |
2446 | 0 | b_deblock = 0; |
2447 | 0 | b_measure_quality = 0; |
2448 | 0 | break; |
2449 | 0 | } |
2450 | 0 | } |
2451 | 0 | if( mb_y & SLICE_MBAFF ) |
2452 | 0 | return; |
2453 | 0 | if( min_y < h->i_threadslice_start ) |
2454 | 0 | return; |
2455 | | |
2456 | 0 | if( b_deblock ) |
2457 | 0 | for( int y = min_y; y < mb_y; y += (1 << SLICE_MBAFF) ) |
2458 | 0 | x264_frame_deblock_row( h, y ); |
2459 | | |
2460 | | /* FIXME: Prediction requires different borders for interlaced/progressive mc, |
2461 | | * but the actual image data is equivalent. For now, maintain this |
2462 | | * consistency by copying deblocked pixels between planes. */ |
2463 | 0 | if( PARAM_INTERLACED && (!h->param.b_sliced_threads || pass == 1) ) |
2464 | 0 | for( int p = 0; p < h->fdec->i_plane; p++ ) |
2465 | 0 | for( int i = minpix_y>>(CHROMA_V_SHIFT && p); i < maxpix_y>>(CHROMA_V_SHIFT && p); i++ ) |
2466 | 0 | memcpy( h->fdec->plane_fld[p] + i*h->fdec->i_stride[p], |
2467 | 0 | h->fdec->plane[p] + i*h->fdec->i_stride[p], |
2468 | 0 | h->mb.i_mb_width*16*SIZEOF_PIXEL ); |
2469 | |
|
2470 | 0 | if( h->fdec->b_kept_as_ref && (!h->param.b_sliced_threads || pass == 1) ) |
2471 | 0 | x264_frame_expand_border( h, h->fdec, min_y ); |
2472 | 0 | if( b_hpel ) |
2473 | 0 | { |
2474 | 0 | int end = mb_y == h->mb.i_mb_height; |
2475 | | /* Can't do hpel until the previous slice is done encoding. */ |
2476 | 0 | if( h->param.analyse.i_subpel_refine ) |
2477 | 0 | { |
2478 | 0 | x264_frame_filter( h, h->fdec, min_y, end ); |
2479 | 0 | x264_frame_expand_border_filtered( h, h->fdec, min_y, end ); |
2480 | 0 | } |
2481 | 0 | } |
2482 | |
|
2483 | 0 | if( SLICE_MBAFF && pass == 0 ) |
2484 | 0 | for( int i = 0; i < 3; i++ ) |
2485 | 0 | { |
2486 | 0 | XCHG( pixel *, h->intra_border_backup[0][i], h->intra_border_backup[3][i] ); |
2487 | 0 | XCHG( pixel *, h->intra_border_backup[1][i], h->intra_border_backup[4][i] ); |
2488 | 0 | } |
2489 | |
|
2490 | 0 | if( h->i_thread_frames > 1 && h->fdec->b_kept_as_ref ) |
2491 | 0 | x264_frame_cond_broadcast( h->fdec, mb_y*16 + (b_end ? 10000 : -(X264_THREAD_HEIGHT << SLICE_MBAFF)) ); |
2492 | |
|
2493 | 0 | if( b_measure_quality ) |
2494 | 0 | { |
2495 | 0 | maxpix_y = X264_MIN( maxpix_y, h->param.i_height ); |
2496 | 0 | if( h->param.analyse.b_psnr ) |
2497 | 0 | { |
2498 | 0 | for( int p = 0; p < (CHROMA444 ? 3 : 1); p++ ) |
2499 | 0 | h->stat.frame.i_ssd[p] += x264_pixel_ssd_wxh( &h->pixf, |
2500 | 0 | h->fdec->plane[p] + minpix_y * h->fdec->i_stride[p], h->fdec->i_stride[p], |
2501 | 0 | h->fenc->plane[p] + minpix_y * h->fenc->i_stride[p], h->fenc->i_stride[p], |
2502 | 0 | h->param.i_width, maxpix_y-minpix_y ); |
2503 | 0 | if( !CHROMA444 ) |
2504 | 0 | { |
2505 | 0 | uint64_t ssd_u, ssd_v; |
2506 | 0 | int v_shift = CHROMA_V_SHIFT; |
2507 | 0 | x264_pixel_ssd_nv12( &h->pixf, |
2508 | 0 | h->fdec->plane[1] + (minpix_y>>v_shift) * h->fdec->i_stride[1], h->fdec->i_stride[1], |
2509 | 0 | h->fenc->plane[1] + (minpix_y>>v_shift) * h->fenc->i_stride[1], h->fenc->i_stride[1], |
2510 | 0 | h->param.i_width>>1, (maxpix_y-minpix_y)>>v_shift, &ssd_u, &ssd_v ); |
2511 | 0 | h->stat.frame.i_ssd[1] += ssd_u; |
2512 | 0 | h->stat.frame.i_ssd[2] += ssd_v; |
2513 | 0 | } |
2514 | 0 | } |
2515 | |
|
2516 | 0 | if( h->param.analyse.b_ssim ) |
2517 | 0 | { |
2518 | 0 | int ssim_cnt; |
2519 | 0 | x264_emms(); |
2520 | | /* offset by 2 pixels to avoid alignment of ssim blocks with dct blocks, |
2521 | | * and overlap by 4 */ |
2522 | 0 | minpix_y += b_start ? 2 : -6; |
2523 | 0 | h->stat.frame.f_ssim += |
2524 | 0 | x264_pixel_ssim_wxh( &h->pixf, |
2525 | 0 | h->fdec->plane[0] + 2+minpix_y*h->fdec->i_stride[0], h->fdec->i_stride[0], |
2526 | 0 | h->fenc->plane[0] + 2+minpix_y*h->fenc->i_stride[0], h->fenc->i_stride[0], |
2527 | 0 | h->param.i_width-2, maxpix_y-minpix_y, h->scratch_buffer, &ssim_cnt ); |
2528 | 0 | h->stat.frame.i_ssim_cnt += ssim_cnt; |
2529 | 0 | } |
2530 | 0 | } |
2531 | 0 | } |
2532 | | |
2533 | | static inline int reference_update( x264_t *h ) |
2534 | 0 | { |
2535 | 0 | if( !h->fdec->b_kept_as_ref ) |
2536 | 0 | { |
2537 | 0 | if( h->i_thread_frames > 1 ) |
2538 | 0 | { |
2539 | 0 | x264_frame_push_unused( h, h->fdec ); |
2540 | 0 | h->fdec = x264_frame_pop_unused( h, 1 ); |
2541 | 0 | if( !h->fdec ) |
2542 | 0 | return -1; |
2543 | 0 | } |
2544 | 0 | return 0; |
2545 | 0 | } |
2546 | | |
2547 | | /* apply mmco from previous frame. */ |
2548 | 0 | for( int i = 0; i < h->sh.i_mmco_command_count; i++ ) |
2549 | 0 | for( int j = 0; h->frames.reference[j]; j++ ) |
2550 | 0 | if( h->frames.reference[j]->i_poc == h->sh.mmco[i].i_poc ) |
2551 | 0 | x264_frame_push_unused( h, x264_frame_shift( &h->frames.reference[j] ) ); |
2552 | | |
2553 | | /* move frame in the buffer */ |
2554 | 0 | x264_frame_push( h->frames.reference, h->fdec ); |
2555 | 0 | if( h->frames.reference[h->sps->i_num_ref_frames] ) |
2556 | 0 | x264_frame_push_unused( h, x264_frame_shift( h->frames.reference ) ); |
2557 | 0 | h->fdec = x264_frame_pop_unused( h, 1 ); |
2558 | 0 | if( !h->fdec ) |
2559 | 0 | return -1; |
2560 | 0 | return 0; |
2561 | 0 | } |
2562 | | |
2563 | | static inline void reference_reset( x264_t *h ) |
2564 | 0 | { |
2565 | 0 | while( h->frames.reference[0] ) |
2566 | 0 | x264_frame_push_unused( h, x264_frame_pop( h->frames.reference ) ); |
2567 | 0 | h->fdec->i_poc = |
2568 | 0 | h->fenc->i_poc = 0; |
2569 | 0 | } |
2570 | | |
2571 | | static inline void reference_hierarchy_reset( x264_t *h ) |
2572 | 0 | { |
2573 | 0 | int ref; |
2574 | 0 | int b_hasdelayframe = 0; |
2575 | | |
2576 | | /* look for delay frames -- chain must only contain frames that are disposable */ |
2577 | 0 | for( int i = 0; h->frames.current[i] && IS_DISPOSABLE( h->frames.current[i]->i_type ); i++ ) |
2578 | 0 | b_hasdelayframe |= h->frames.current[i]->i_coded |
2579 | 0 | != h->frames.current[i]->i_frame + h->sps->vui.i_num_reorder_frames; |
2580 | | |
2581 | | /* This function must handle b-pyramid and clear frames for open-gop */ |
2582 | 0 | if( h->param.i_bframe_pyramid != X264_B_PYRAMID_STRICT && !b_hasdelayframe && h->frames.i_poc_last_open_gop == -1 ) |
2583 | 0 | return; |
2584 | | |
2585 | | /* Remove last BREF. There will never be old BREFs in the |
2586 | | * dpb during a BREF decode when pyramid == STRICT */ |
2587 | 0 | for( ref = 0; h->frames.reference[ref]; ref++ ) |
2588 | 0 | { |
2589 | 0 | if( ( h->param.i_bframe_pyramid == X264_B_PYRAMID_STRICT |
2590 | 0 | && h->frames.reference[ref]->i_type == X264_TYPE_BREF ) |
2591 | 0 | || ( h->frames.reference[ref]->i_poc < h->frames.i_poc_last_open_gop |
2592 | 0 | && h->sh.i_type != SLICE_TYPE_B ) ) |
2593 | 0 | { |
2594 | 0 | int diff = h->i_frame_num - h->frames.reference[ref]->i_frame_num; |
2595 | 0 | h->sh.mmco[h->sh.i_mmco_command_count].i_difference_of_pic_nums = diff; |
2596 | 0 | h->sh.mmco[h->sh.i_mmco_command_count++].i_poc = h->frames.reference[ref]->i_poc; |
2597 | 0 | x264_frame_push_unused( h, x264_frame_shift( &h->frames.reference[ref] ) ); |
2598 | 0 | h->b_ref_reorder[0] = 1; |
2599 | 0 | ref--; |
2600 | 0 | } |
2601 | 0 | } |
2602 | | |
2603 | | /* Prepare room in the dpb for the delayed display time of the later b-frame's */ |
2604 | 0 | if( h->param.i_bframe_pyramid ) |
2605 | 0 | h->sh.i_mmco_remove_from_end = X264_MAX( ref + 2 - h->frames.i_max_dpb, 0 ); |
2606 | 0 | } |
2607 | | |
2608 | | static inline void slice_init( x264_t *h, int i_nal_type, int i_global_qp ) |
2609 | 0 | { |
2610 | | /* ------------------------ Create slice header ----------------------- */ |
2611 | 0 | if( i_nal_type == NAL_SLICE_IDR ) |
2612 | 0 | { |
2613 | 0 | slice_header_init( h, &h->sh, h->sps, h->pps, h->i_idr_pic_id, h->i_frame_num, i_global_qp ); |
2614 | | |
2615 | | /* alternate id */ |
2616 | 0 | if( h->param.i_avcintra_class ) |
2617 | 0 | { |
2618 | 0 | switch( h->i_idr_pic_id ) |
2619 | 0 | { |
2620 | 0 | case 5: |
2621 | 0 | h->i_idr_pic_id = 3; |
2622 | 0 | break; |
2623 | 0 | case 3: |
2624 | 0 | h->i_idr_pic_id = 4; |
2625 | 0 | break; |
2626 | 0 | case 4: |
2627 | 0 | default: |
2628 | 0 | h->i_idr_pic_id = 5; |
2629 | 0 | break; |
2630 | 0 | } |
2631 | 0 | } |
2632 | 0 | else |
2633 | 0 | h->i_idr_pic_id ^= 1; |
2634 | 0 | } |
2635 | 0 | else |
2636 | 0 | { |
2637 | 0 | slice_header_init( h, &h->sh, h->sps, h->pps, -1, h->i_frame_num, i_global_qp ); |
2638 | |
|
2639 | 0 | h->sh.i_num_ref_idx_l0_active = h->i_ref[0] <= 0 ? 1 : h->i_ref[0]; |
2640 | 0 | h->sh.i_num_ref_idx_l1_active = h->i_ref[1] <= 0 ? 1 : h->i_ref[1]; |
2641 | 0 | if( h->sh.i_num_ref_idx_l0_active != h->pps->i_num_ref_idx_l0_default_active || |
2642 | 0 | (h->sh.i_type == SLICE_TYPE_B && h->sh.i_num_ref_idx_l1_active != h->pps->i_num_ref_idx_l1_default_active) ) |
2643 | 0 | { |
2644 | 0 | h->sh.b_num_ref_idx_override = 1; |
2645 | 0 | } |
2646 | 0 | } |
2647 | | |
2648 | 0 | if( h->fenc->i_type == X264_TYPE_BREF && h->param.b_bluray_compat && h->sh.i_mmco_command_count ) |
2649 | 0 | { |
2650 | 0 | h->b_sh_backup = 1; |
2651 | 0 | h->sh_backup = h->sh; |
2652 | 0 | } |
2653 | |
|
2654 | 0 | h->fdec->i_frame_num = h->sh.i_frame_num; |
2655 | |
|
2656 | 0 | if( h->sps->i_poc_type == 0 ) |
2657 | 0 | { |
2658 | 0 | h->sh.i_poc = h->fdec->i_poc; |
2659 | 0 | if( PARAM_INTERLACED ) |
2660 | 0 | { |
2661 | 0 | h->sh.i_delta_poc_bottom = h->param.b_tff ? 1 : -1; |
2662 | 0 | h->sh.i_poc += h->sh.i_delta_poc_bottom == -1; |
2663 | 0 | } |
2664 | 0 | else |
2665 | 0 | h->sh.i_delta_poc_bottom = 0; |
2666 | 0 | h->fdec->i_delta_poc[0] = h->sh.i_delta_poc_bottom == -1; |
2667 | 0 | h->fdec->i_delta_poc[1] = h->sh.i_delta_poc_bottom == 1; |
2668 | 0 | } |
2669 | 0 | else |
2670 | 0 | { |
2671 | | /* Nothing to do ? */ |
2672 | 0 | } |
2673 | |
|
2674 | 0 | x264_macroblock_slice_init( h ); |
2675 | 0 | } |
2676 | | |
2677 | | typedef struct |
2678 | | { |
2679 | | int skip; |
2680 | | uint8_t cabac_prevbyte; |
2681 | | bs_t bs; |
2682 | | x264_cabac_t cabac; |
2683 | | x264_frame_stat_t stat; |
2684 | | int last_qp; |
2685 | | int last_dqp; |
2686 | | int field_decoding_flag; |
2687 | | } x264_bs_bak_t; |
2688 | | |
2689 | | static ALWAYS_INLINE void bitstream_backup( x264_t *h, x264_bs_bak_t *bak, int i_skip, int full ) |
2690 | 0 | { |
2691 | 0 | if( full ) |
2692 | 0 | { |
2693 | 0 | bak->stat = h->stat.frame; |
2694 | 0 | bak->last_qp = h->mb.i_last_qp; |
2695 | 0 | bak->last_dqp = h->mb.i_last_dqp; |
2696 | 0 | bak->field_decoding_flag = h->mb.field_decoding_flag; |
2697 | 0 | } |
2698 | 0 | else |
2699 | 0 | { |
2700 | 0 | bak->stat.i_mv_bits = h->stat.frame.i_mv_bits; |
2701 | 0 | bak->stat.i_tex_bits = h->stat.frame.i_tex_bits; |
2702 | 0 | } |
2703 | | /* In the per-MB backup, we don't need the contexts because flushing the CABAC |
2704 | | * encoder has no context dependency and in this case, a slice is ended (and |
2705 | | * thus the content of all contexts are thrown away). */ |
2706 | 0 | if( h->param.b_cabac ) |
2707 | 0 | { |
2708 | 0 | if( full ) |
2709 | 0 | memcpy( &bak->cabac, &h->cabac, sizeof(x264_cabac_t) ); |
2710 | 0 | else |
2711 | 0 | memcpy( &bak->cabac, &h->cabac, offsetof(x264_cabac_t, f8_bits_encoded) ); |
2712 | | /* x264's CABAC writer modifies the previous byte during carry, so it has to be |
2713 | | * backed up. */ |
2714 | 0 | bak->cabac_prevbyte = h->cabac.p[-1]; |
2715 | 0 | } |
2716 | 0 | else |
2717 | 0 | { |
2718 | 0 | bak->bs = h->out.bs; |
2719 | 0 | bak->skip = i_skip; |
2720 | 0 | } |
2721 | 0 | } |
2722 | | |
2723 | | static ALWAYS_INLINE void bitstream_restore( x264_t *h, x264_bs_bak_t *bak, int *skip, int full ) |
2724 | 0 | { |
2725 | 0 | if( full ) |
2726 | 0 | { |
2727 | 0 | h->stat.frame = bak->stat; |
2728 | 0 | h->mb.i_last_qp = bak->last_qp; |
2729 | 0 | h->mb.i_last_dqp = bak->last_dqp; |
2730 | 0 | h->mb.field_decoding_flag = bak->field_decoding_flag; |
2731 | 0 | } |
2732 | 0 | else |
2733 | 0 | { |
2734 | 0 | h->stat.frame.i_mv_bits = bak->stat.i_mv_bits; |
2735 | 0 | h->stat.frame.i_tex_bits = bak->stat.i_tex_bits; |
2736 | 0 | } |
2737 | 0 | if( h->param.b_cabac ) |
2738 | 0 | { |
2739 | 0 | if( full ) |
2740 | 0 | memcpy( &h->cabac, &bak->cabac, sizeof(x264_cabac_t) ); |
2741 | 0 | else |
2742 | 0 | memcpy( &h->cabac, &bak->cabac, offsetof(x264_cabac_t, f8_bits_encoded) ); |
2743 | 0 | h->cabac.p[-1] = bak->cabac_prevbyte; |
2744 | 0 | } |
2745 | 0 | else |
2746 | 0 | { |
2747 | 0 | h->out.bs = bak->bs; |
2748 | 0 | *skip = bak->skip; |
2749 | 0 | } |
2750 | 0 | } |
2751 | | |
2752 | | static intptr_t slice_write( x264_t *h ) |
2753 | 0 | { |
2754 | 0 | int i_skip; |
2755 | 0 | int mb_xy, i_mb_x, i_mb_y; |
2756 | | /* NALUs other than the first use a 3-byte startcode. |
2757 | | * Add one extra byte for the rbsp, and one more for the final CABAC putbyte. |
2758 | | * Then add an extra 5 bytes just in case, to account for random NAL escapes and |
2759 | | * other inaccuracies. */ |
2760 | 0 | int overhead_guess = (NALU_OVERHEAD - (h->param.b_annexb && h->out.i_nal)) + 1 + h->param.b_cabac + 5; |
2761 | 0 | int slice_max_size = h->param.i_slice_max_size > 0 ? (h->param.i_slice_max_size-overhead_guess)*8 : 0; |
2762 | 0 | int back_up_bitstream_cavlc = !h->param.b_cabac && h->sps->i_profile_idc < PROFILE_HIGH; |
2763 | 0 | int back_up_bitstream = slice_max_size || back_up_bitstream_cavlc; |
2764 | 0 | int starting_bits = bs_pos(&h->out.bs); |
2765 | 0 | int b_deblock = h->sh.i_disable_deblocking_filter_idc != 1; |
2766 | 0 | int b_hpel = h->fdec->b_kept_as_ref; |
2767 | 0 | int orig_last_mb = h->sh.i_last_mb; |
2768 | 0 | int thread_last_mb = h->i_threadslice_end * h->mb.i_mb_width - 1; |
2769 | 0 | uint8_t *last_emu_check; |
2770 | 0 | #define BS_BAK_SLICE_MAX_SIZE 0 |
2771 | 0 | #define BS_BAK_CAVLC_OVERFLOW 1 |
2772 | 0 | #define BS_BAK_SLICE_MIN_MBS 2 |
2773 | 0 | #define BS_BAK_ROW_VBV 3 |
2774 | 0 | x264_bs_bak_t bs_bak[4]; |
2775 | 0 | b_deblock &= b_hpel || h->param.b_full_recon || h->param.psz_dump_yuv; |
2776 | 0 | bs_realign( &h->out.bs ); |
2777 | | |
2778 | | /* Slice */ |
2779 | 0 | nal_start( h, h->i_nal_type, h->i_nal_ref_idc ); |
2780 | 0 | h->out.nal[h->out.i_nal].i_first_mb = h->sh.i_first_mb; |
2781 | | |
2782 | | /* Slice header */ |
2783 | 0 | x264_macroblock_thread_init( h ); |
2784 | | |
2785 | | /* Set the QP equal to the first QP in the slice for more accurate CABAC initialization. */ |
2786 | 0 | h->mb.i_mb_xy = h->sh.i_first_mb; |
2787 | 0 | h->sh.i_qp = x264_ratecontrol_mb_qp( h ); |
2788 | 0 | h->sh.i_qp = SPEC_QP( h->sh.i_qp ); |
2789 | 0 | h->sh.i_qp_delta = h->sh.i_qp - h->pps->i_pic_init_qp; |
2790 | |
|
2791 | 0 | slice_header_write( &h->out.bs, &h->sh, h->i_nal_ref_idc ); |
2792 | 0 | if( h->param.b_cabac ) |
2793 | 0 | { |
2794 | | /* alignment needed */ |
2795 | 0 | bs_align_1( &h->out.bs ); |
2796 | | |
2797 | | /* init cabac */ |
2798 | 0 | x264_cabac_context_init( h, &h->cabac, h->sh.i_type, x264_clip3( h->sh.i_qp-QP_BD_OFFSET, 0, 51 ), h->sh.i_cabac_init_idc ); |
2799 | 0 | x264_cabac_encode_init ( &h->cabac, h->out.bs.p, h->out.bs.p_end ); |
2800 | 0 | last_emu_check = h->cabac.p; |
2801 | 0 | } |
2802 | 0 | else |
2803 | 0 | last_emu_check = h->out.bs.p; |
2804 | 0 | h->mb.i_last_qp = h->sh.i_qp; |
2805 | 0 | h->mb.i_last_dqp = 0; |
2806 | 0 | h->mb.field_decoding_flag = 0; |
2807 | |
|
2808 | 0 | i_mb_y = h->sh.i_first_mb / h->mb.i_mb_width; |
2809 | 0 | i_mb_x = h->sh.i_first_mb % h->mb.i_mb_width; |
2810 | 0 | i_skip = 0; |
2811 | |
|
2812 | 0 | while( 1 ) |
2813 | 0 | { |
2814 | 0 | mb_xy = i_mb_x + i_mb_y * h->mb.i_mb_width; |
2815 | 0 | int mb_spos = bs_pos(&h->out.bs) + x264_cabac_pos(&h->cabac); |
2816 | |
|
2817 | 0 | if( i_mb_x == 0 ) |
2818 | 0 | { |
2819 | 0 | if( bitstream_check_buffer( h ) ) |
2820 | 0 | return -1; |
2821 | 0 | if( !(i_mb_y & SLICE_MBAFF) && h->param.rc.i_vbv_buffer_size ) |
2822 | 0 | bitstream_backup( h, &bs_bak[BS_BAK_ROW_VBV], i_skip, 1 ); |
2823 | 0 | if( !h->mb.b_reencode_mb ) |
2824 | 0 | fdec_filter_row( h, i_mb_y, 0 ); |
2825 | 0 | } |
2826 | | |
2827 | 0 | if( back_up_bitstream ) |
2828 | 0 | { |
2829 | 0 | if( back_up_bitstream_cavlc ) |
2830 | 0 | bitstream_backup( h, &bs_bak[BS_BAK_CAVLC_OVERFLOW], i_skip, 0 ); |
2831 | 0 | if( slice_max_size && !(i_mb_y & SLICE_MBAFF) ) |
2832 | 0 | { |
2833 | 0 | bitstream_backup( h, &bs_bak[BS_BAK_SLICE_MAX_SIZE], i_skip, 0 ); |
2834 | 0 | if( (thread_last_mb+1-mb_xy) == h->param.i_slice_min_mbs ) |
2835 | 0 | bitstream_backup( h, &bs_bak[BS_BAK_SLICE_MIN_MBS], i_skip, 0 ); |
2836 | 0 | } |
2837 | 0 | } |
2838 | |
|
2839 | 0 | if( PARAM_INTERLACED ) |
2840 | 0 | { |
2841 | 0 | if( h->mb.b_adaptive_mbaff ) |
2842 | 0 | { |
2843 | 0 | if( !(i_mb_y&1) ) |
2844 | 0 | { |
2845 | | /* FIXME: VSAD is fast but fairly poor at choosing the best interlace type. */ |
2846 | 0 | h->mb.b_interlaced = x264_field_vsad( h, i_mb_x, i_mb_y ); |
2847 | 0 | memcpy( &h->zigzagf, MB_INTERLACED ? &h->zigzagf_interlaced : &h->zigzagf_progressive, sizeof(h->zigzagf) ); |
2848 | 0 | if( !MB_INTERLACED && (i_mb_y+2) == h->mb.i_mb_height ) |
2849 | 0 | x264_expand_border_mbpair( h, i_mb_x, i_mb_y ); |
2850 | 0 | } |
2851 | 0 | } |
2852 | 0 | h->mb.field[mb_xy] = MB_INTERLACED; |
2853 | 0 | } |
2854 | | |
2855 | | /* load cache */ |
2856 | 0 | if( SLICE_MBAFF ) |
2857 | 0 | x264_macroblock_cache_load_interlaced( h, i_mb_x, i_mb_y ); |
2858 | 0 | else |
2859 | 0 | x264_macroblock_cache_load_progressive( h, i_mb_x, i_mb_y ); |
2860 | |
|
2861 | 0 | x264_macroblock_analyse( h ); |
2862 | | |
2863 | | /* encode this macroblock -> be careful it can change the mb type to P_SKIP if needed */ |
2864 | 0 | reencode: |
2865 | 0 | x264_macroblock_encode( h ); |
2866 | |
|
2867 | 0 | if( h->param.b_cabac ) |
2868 | 0 | { |
2869 | 0 | if( mb_xy > h->sh.i_first_mb && !(SLICE_MBAFF && (i_mb_y&1)) ) |
2870 | 0 | x264_cabac_encode_terminal( &h->cabac ); |
2871 | |
|
2872 | 0 | if( IS_SKIP( h->mb.i_type ) ) |
2873 | 0 | x264_cabac_mb_skip( h, 1 ); |
2874 | 0 | else |
2875 | 0 | { |
2876 | 0 | if( h->sh.i_type != SLICE_TYPE_I ) |
2877 | 0 | x264_cabac_mb_skip( h, 0 ); |
2878 | 0 | x264_macroblock_write_cabac( h, &h->cabac ); |
2879 | 0 | } |
2880 | 0 | } |
2881 | 0 | else |
2882 | 0 | { |
2883 | 0 | if( IS_SKIP( h->mb.i_type ) ) |
2884 | 0 | i_skip++; |
2885 | 0 | else |
2886 | 0 | { |
2887 | 0 | if( h->sh.i_type != SLICE_TYPE_I ) |
2888 | 0 | { |
2889 | 0 | bs_write_ue( &h->out.bs, i_skip ); /* skip run */ |
2890 | 0 | i_skip = 0; |
2891 | 0 | } |
2892 | 0 | x264_macroblock_write_cavlc( h ); |
2893 | | /* If there was a CAVLC level code overflow, try again at a higher QP. */ |
2894 | 0 | if( h->mb.b_overflow ) |
2895 | 0 | { |
2896 | 0 | h->mb.i_chroma_qp = h->chroma_qp_table[++h->mb.i_qp]; |
2897 | 0 | h->mb.i_skip_intra = 0; |
2898 | 0 | h->mb.b_skip_mc = 0; |
2899 | 0 | h->mb.b_overflow = 0; |
2900 | 0 | bitstream_restore( h, &bs_bak[BS_BAK_CAVLC_OVERFLOW], &i_skip, 0 ); |
2901 | 0 | goto reencode; |
2902 | 0 | } |
2903 | 0 | } |
2904 | 0 | } |
2905 | | |
2906 | 0 | int total_bits = bs_pos(&h->out.bs) + x264_cabac_pos(&h->cabac); |
2907 | 0 | int mb_size = total_bits - mb_spos; |
2908 | |
|
2909 | 0 | if( slice_max_size && (!SLICE_MBAFF || (i_mb_y&1)) ) |
2910 | 0 | { |
2911 | | /* Count the skip run, just in case. */ |
2912 | 0 | if( !h->param.b_cabac ) |
2913 | 0 | total_bits += bs_size_ue_big( i_skip ); |
2914 | | /* Check for escape bytes. */ |
2915 | 0 | uint8_t *end = h->param.b_cabac ? h->cabac.p : h->out.bs.p; |
2916 | 0 | for( ; last_emu_check < end - 2; last_emu_check++ ) |
2917 | 0 | if( last_emu_check[0] == 0 && last_emu_check[1] == 0 && last_emu_check[2] <= 3 ) |
2918 | 0 | { |
2919 | 0 | slice_max_size -= 8; |
2920 | 0 | last_emu_check++; |
2921 | 0 | } |
2922 | | /* We'll just re-encode this last macroblock if we go over the max slice size. */ |
2923 | 0 | if( total_bits - starting_bits > slice_max_size && !h->mb.b_reencode_mb ) |
2924 | 0 | { |
2925 | 0 | if( !x264_frame_new_slice( h, h->fdec ) ) |
2926 | 0 | { |
2927 | | /* Handle the most obnoxious slice-min-mbs edge case: we need to end the slice |
2928 | | * because it's gone over the maximum size, but doing so would violate slice-min-mbs. |
2929 | | * If possible, roll back to the last checkpoint and try again. |
2930 | | * We could try raising QP, but that would break in the case where a slice spans multiple |
2931 | | * rows, which the re-encoding infrastructure can't currently handle. */ |
2932 | 0 | if( mb_xy <= thread_last_mb && (thread_last_mb+1-mb_xy) < h->param.i_slice_min_mbs ) |
2933 | 0 | { |
2934 | 0 | if( thread_last_mb-h->param.i_slice_min_mbs < h->sh.i_first_mb+h->param.i_slice_min_mbs ) |
2935 | 0 | { |
2936 | 0 | x264_log( h, X264_LOG_WARNING, "slice-max-size violated (frame %d, cause: slice-min-mbs)\n", h->i_frame ); |
2937 | 0 | slice_max_size = 0; |
2938 | 0 | goto cont; |
2939 | 0 | } |
2940 | 0 | bitstream_restore( h, &bs_bak[BS_BAK_SLICE_MIN_MBS], &i_skip, 0 ); |
2941 | 0 | h->mb.b_reencode_mb = 1; |
2942 | 0 | h->sh.i_last_mb = thread_last_mb-h->param.i_slice_min_mbs; |
2943 | 0 | break; |
2944 | 0 | } |
2945 | 0 | if( mb_xy-SLICE_MBAFF*h->mb.i_mb_stride != h->sh.i_first_mb ) |
2946 | 0 | { |
2947 | 0 | bitstream_restore( h, &bs_bak[BS_BAK_SLICE_MAX_SIZE], &i_skip, 0 ); |
2948 | 0 | h->mb.b_reencode_mb = 1; |
2949 | 0 | if( SLICE_MBAFF ) |
2950 | 0 | { |
2951 | | // set to bottom of previous mbpair |
2952 | 0 | if( i_mb_x ) |
2953 | 0 | h->sh.i_last_mb = mb_xy-1+h->mb.i_mb_stride*(!(i_mb_y&1)); |
2954 | 0 | else |
2955 | 0 | h->sh.i_last_mb = (i_mb_y-2+!(i_mb_y&1))*h->mb.i_mb_stride + h->mb.i_mb_width - 1; |
2956 | 0 | } |
2957 | 0 | else |
2958 | 0 | h->sh.i_last_mb = mb_xy-1; |
2959 | 0 | break; |
2960 | 0 | } |
2961 | 0 | else |
2962 | 0 | h->sh.i_last_mb = mb_xy; |
2963 | 0 | } |
2964 | 0 | else |
2965 | 0 | slice_max_size = 0; |
2966 | 0 | } |
2967 | 0 | } |
2968 | 0 | cont: |
2969 | 0 | h->mb.b_reencode_mb = 0; |
2970 | | |
2971 | | /* save cache */ |
2972 | 0 | x264_macroblock_cache_save( h ); |
2973 | |
|
2974 | 0 | if( x264_ratecontrol_mb( h, mb_size ) < 0 ) |
2975 | 0 | { |
2976 | 0 | bitstream_restore( h, &bs_bak[BS_BAK_ROW_VBV], &i_skip, 1 ); |
2977 | 0 | h->mb.b_reencode_mb = 1; |
2978 | 0 | i_mb_x = 0; |
2979 | 0 | i_mb_y = i_mb_y - SLICE_MBAFF; |
2980 | 0 | h->mb.i_mb_prev_xy = i_mb_y * h->mb.i_mb_stride - 1; |
2981 | 0 | h->sh.i_last_mb = orig_last_mb; |
2982 | 0 | continue; |
2983 | 0 | } |
2984 | | |
2985 | | /* accumulate mb stats */ |
2986 | 0 | h->stat.frame.i_mb_count[h->mb.i_type]++; |
2987 | |
|
2988 | 0 | int b_intra = IS_INTRA( h->mb.i_type ); |
2989 | 0 | int b_skip = IS_SKIP( h->mb.i_type ); |
2990 | 0 | if( h->param.i_log_level >= X264_LOG_INFO || h->param.rc.b_stat_write ) |
2991 | 0 | { |
2992 | 0 | if( !b_intra && !b_skip && !IS_DIRECT( h->mb.i_type ) ) |
2993 | 0 | { |
2994 | 0 | if( h->mb.i_partition != D_8x8 ) |
2995 | 0 | h->stat.frame.i_mb_partition[h->mb.i_partition] += 4; |
2996 | 0 | else |
2997 | 0 | for( int i = 0; i < 4; i++ ) |
2998 | 0 | h->stat.frame.i_mb_partition[h->mb.i_sub_partition[i]] ++; |
2999 | 0 | if( h->param.i_frame_reference > 1 ) |
3000 | 0 | for( int i_list = 0; i_list <= (h->sh.i_type == SLICE_TYPE_B); i_list++ ) |
3001 | 0 | for( int i = 0; i < 4; i++ ) |
3002 | 0 | { |
3003 | 0 | int i_ref = h->mb.cache.ref[i_list][ x264_scan8[4*i] ]; |
3004 | 0 | if( i_ref >= 0 ) |
3005 | 0 | h->stat.frame.i_mb_count_ref[i_list][i_ref] ++; |
3006 | 0 | } |
3007 | 0 | } |
3008 | 0 | } |
3009 | |
|
3010 | 0 | if( h->param.i_log_level >= X264_LOG_INFO ) |
3011 | 0 | { |
3012 | 0 | if( h->mb.i_cbp_luma | h->mb.i_cbp_chroma ) |
3013 | 0 | { |
3014 | 0 | if( CHROMA444 ) |
3015 | 0 | { |
3016 | 0 | for( int i = 0; i < 4; i++ ) |
3017 | 0 | if( h->mb.i_cbp_luma & (1 << i) ) |
3018 | 0 | for( int p = 0; p < 3; p++ ) |
3019 | 0 | { |
3020 | 0 | int s8 = i*4+p*16; |
3021 | 0 | int nnz8x8 = M16( &h->mb.cache.non_zero_count[x264_scan8[s8]+0] ) |
3022 | 0 | | M16( &h->mb.cache.non_zero_count[x264_scan8[s8]+8] ); |
3023 | 0 | h->stat.frame.i_mb_cbp[!b_intra + p*2] += !!nnz8x8; |
3024 | 0 | } |
3025 | 0 | } |
3026 | 0 | else |
3027 | 0 | { |
3028 | 0 | int cbpsum = (h->mb.i_cbp_luma&1) + ((h->mb.i_cbp_luma>>1)&1) |
3029 | 0 | + ((h->mb.i_cbp_luma>>2)&1) + (h->mb.i_cbp_luma>>3); |
3030 | 0 | h->stat.frame.i_mb_cbp[!b_intra + 0] += cbpsum; |
3031 | 0 | h->stat.frame.i_mb_cbp[!b_intra + 2] += !!h->mb.i_cbp_chroma; |
3032 | 0 | h->stat.frame.i_mb_cbp[!b_intra + 4] += h->mb.i_cbp_chroma >> 1; |
3033 | 0 | } |
3034 | 0 | } |
3035 | 0 | if( h->mb.i_cbp_luma && !b_intra ) |
3036 | 0 | { |
3037 | 0 | h->stat.frame.i_mb_count_8x8dct[0] ++; |
3038 | 0 | h->stat.frame.i_mb_count_8x8dct[1] += h->mb.b_transform_8x8; |
3039 | 0 | } |
3040 | 0 | if( b_intra && h->mb.i_type != I_PCM ) |
3041 | 0 | { |
3042 | 0 | if( h->mb.i_type == I_16x16 ) |
3043 | 0 | h->stat.frame.i_mb_pred_mode[0][h->mb.i_intra16x16_pred_mode]++; |
3044 | 0 | else if( h->mb.i_type == I_8x8 ) |
3045 | 0 | for( int i = 0; i < 16; i += 4 ) |
3046 | 0 | h->stat.frame.i_mb_pred_mode[1][h->mb.cache.intra4x4_pred_mode[x264_scan8[i]]]++; |
3047 | 0 | else //if( h->mb.i_type == I_4x4 ) |
3048 | 0 | for( int i = 0; i < 16; i++ ) |
3049 | 0 | h->stat.frame.i_mb_pred_mode[2][h->mb.cache.intra4x4_pred_mode[x264_scan8[i]]]++; |
3050 | 0 | h->stat.frame.i_mb_pred_mode[3][x264_mb_chroma_pred_mode_fix[h->mb.i_chroma_pred_mode]]++; |
3051 | 0 | } |
3052 | 0 | h->stat.frame.i_mb_field[b_intra?0:b_skip?2:1] += MB_INTERLACED; |
3053 | 0 | } |
3054 | | |
3055 | | /* calculate deblock strength values (actual deblocking is done per-row along with hpel) */ |
3056 | 0 | if( b_deblock ) |
3057 | 0 | x264_macroblock_deblock_strength( h ); |
3058 | |
|
3059 | 0 | if( mb_xy == h->sh.i_last_mb ) |
3060 | 0 | break; |
3061 | | |
3062 | 0 | if( SLICE_MBAFF ) |
3063 | 0 | { |
3064 | 0 | i_mb_x += i_mb_y & 1; |
3065 | 0 | i_mb_y ^= i_mb_x < h->mb.i_mb_width; |
3066 | 0 | } |
3067 | 0 | else |
3068 | 0 | i_mb_x++; |
3069 | 0 | if( i_mb_x == h->mb.i_mb_width ) |
3070 | 0 | { |
3071 | 0 | i_mb_y++; |
3072 | 0 | i_mb_x = 0; |
3073 | 0 | } |
3074 | 0 | } |
3075 | 0 | if( h->sh.i_last_mb < h->sh.i_first_mb ) |
3076 | 0 | return 0; |
3077 | | |
3078 | 0 | h->out.nal[h->out.i_nal].i_last_mb = h->sh.i_last_mb; |
3079 | |
|
3080 | 0 | if( h->param.b_cabac ) |
3081 | 0 | { |
3082 | 0 | x264_cabac_encode_flush( h, &h->cabac ); |
3083 | 0 | h->out.bs.p = h->cabac.p; |
3084 | 0 | } |
3085 | 0 | else |
3086 | 0 | { |
3087 | 0 | if( i_skip > 0 ) |
3088 | 0 | bs_write_ue( &h->out.bs, i_skip ); /* last skip run */ |
3089 | | /* rbsp_slice_trailing_bits */ |
3090 | 0 | bs_rbsp_trailing( &h->out.bs ); |
3091 | 0 | bs_flush( &h->out.bs ); |
3092 | 0 | } |
3093 | 0 | if( nal_end( h ) ) |
3094 | 0 | return -1; |
3095 | | |
3096 | 0 | if( h->sh.i_last_mb == (h->i_threadslice_end * h->mb.i_mb_width - 1) ) |
3097 | 0 | { |
3098 | 0 | h->stat.frame.i_misc_bits = bs_pos( &h->out.bs ) |
3099 | 0 | + (h->out.i_nal*NALU_OVERHEAD * 8) |
3100 | 0 | - h->stat.frame.i_tex_bits |
3101 | 0 | - h->stat.frame.i_mv_bits; |
3102 | 0 | fdec_filter_row( h, h->i_threadslice_end, 0 ); |
3103 | |
|
3104 | 0 | if( h->param.b_sliced_threads ) |
3105 | 0 | { |
3106 | | /* Tell the main thread we're done. */ |
3107 | 0 | x264_threadslice_cond_broadcast( h, 1 ); |
3108 | | /* Do hpel now */ |
3109 | 0 | for( int mb_y = h->i_threadslice_start; mb_y <= h->i_threadslice_end; mb_y++ ) |
3110 | 0 | fdec_filter_row( h, mb_y, 1 ); |
3111 | 0 | x264_threadslice_cond_broadcast( h, 2 ); |
3112 | | /* Do the first row of hpel, now that the previous slice is done */ |
3113 | 0 | if( h->i_thread_idx > 0 ) |
3114 | 0 | { |
3115 | 0 | x264_threadslice_cond_wait( h->thread[h->i_thread_idx-1], 2 ); |
3116 | 0 | fdec_filter_row( h, h->i_threadslice_start + (1 << SLICE_MBAFF), 2 ); |
3117 | 0 | } |
3118 | 0 | } |
3119 | | |
3120 | | /* Free mb info after the last thread's done using it */ |
3121 | 0 | if( h->fdec->mb_info_free && (!h->param.b_sliced_threads || h->i_thread_idx == (h->param.i_threads-1)) ) |
3122 | 0 | { |
3123 | 0 | h->fdec->mb_info_free( h->fdec->mb_info ); |
3124 | 0 | h->fdec->mb_info = NULL; |
3125 | 0 | h->fdec->mb_info_free = NULL; |
3126 | 0 | } |
3127 | 0 | } |
3128 | |
|
3129 | 0 | return 0; |
3130 | 0 | } |
3131 | | |
3132 | | static void thread_sync_context( x264_t *dst, x264_t *src ) |
3133 | 0 | { |
3134 | 0 | if( dst == src ) |
3135 | 0 | return; |
3136 | | |
3137 | | // reference counting |
3138 | 0 | for( x264_frame_t **f = src->frames.reference; *f; f++ ) |
3139 | 0 | (*f)->i_reference_count++; |
3140 | 0 | for( x264_frame_t **f = dst->frames.reference; *f; f++ ) |
3141 | 0 | x264_frame_push_unused( src, *f ); |
3142 | 0 | src->fdec->i_reference_count++; |
3143 | 0 | x264_frame_push_unused( src, dst->fdec ); |
3144 | | |
3145 | | // copy everything except the per-thread pointers and the constants. |
3146 | 0 | memcpy( &dst->i_frame, &src->i_frame, offsetof(x264_t, mb.base) - offsetof(x264_t, i_frame) ); |
3147 | 0 | dst->param = src->param; |
3148 | 0 | dst->stat = src->stat; |
3149 | 0 | dst->pixf = src->pixf; |
3150 | 0 | dst->reconfig = src->reconfig; |
3151 | 0 | } |
3152 | | |
3153 | | static void thread_sync_stat( x264_t *dst, x264_t *src ) |
3154 | 0 | { |
3155 | 0 | if( dst != src ) |
3156 | 0 | memcpy( &dst->stat, &src->stat, offsetof(x264_t, stat.frame) - offsetof(x264_t, stat) ); |
3157 | 0 | } |
3158 | | |
3159 | | static void *slices_write( x264_t *h ) |
3160 | 0 | { |
3161 | 0 | int i_slice_num = 0; |
3162 | 0 | int last_thread_mb = h->sh.i_last_mb; |
3163 | 0 | int round_bias = h->param.i_avcintra_class ? 0 : h->param.i_slice_count/2; |
3164 | | |
3165 | | /* init stats */ |
3166 | 0 | memset( &h->stat.frame, 0, sizeof(h->stat.frame) ); |
3167 | 0 | h->mb.b_reencode_mb = 0; |
3168 | 0 | while( h->sh.i_first_mb + SLICE_MBAFF*h->mb.i_mb_stride <= last_thread_mb ) |
3169 | 0 | { |
3170 | 0 | h->sh.i_last_mb = last_thread_mb; |
3171 | 0 | if( !i_slice_num || !x264_frame_new_slice( h, h->fdec ) ) |
3172 | 0 | { |
3173 | 0 | if( h->param.i_slice_max_mbs ) |
3174 | 0 | { |
3175 | 0 | if( SLICE_MBAFF ) |
3176 | 0 | { |
3177 | | // convert first to mbaff form, add slice-max-mbs, then convert back to normal form |
3178 | 0 | int last_mbaff = 2*(h->sh.i_first_mb % h->mb.i_mb_width) |
3179 | 0 | + h->mb.i_mb_width*(h->sh.i_first_mb / h->mb.i_mb_width) |
3180 | 0 | + h->param.i_slice_max_mbs - 1; |
3181 | 0 | int last_x = (last_mbaff % (2*h->mb.i_mb_width))/2; |
3182 | 0 | int last_y = (last_mbaff / (2*h->mb.i_mb_width))*2 + 1; |
3183 | 0 | h->sh.i_last_mb = last_x + h->mb.i_mb_stride*last_y; |
3184 | 0 | } |
3185 | 0 | else |
3186 | 0 | { |
3187 | 0 | h->sh.i_last_mb = h->sh.i_first_mb + h->param.i_slice_max_mbs - 1; |
3188 | 0 | if( h->sh.i_last_mb < last_thread_mb && last_thread_mb - h->sh.i_last_mb < h->param.i_slice_min_mbs ) |
3189 | 0 | h->sh.i_last_mb = last_thread_mb - h->param.i_slice_min_mbs; |
3190 | 0 | } |
3191 | 0 | i_slice_num++; |
3192 | 0 | } |
3193 | 0 | else if( h->param.i_slice_count && !h->param.b_sliced_threads ) |
3194 | 0 | { |
3195 | 0 | int height = h->mb.i_mb_height >> PARAM_INTERLACED; |
3196 | 0 | int width = h->mb.i_mb_width << PARAM_INTERLACED; |
3197 | 0 | i_slice_num++; |
3198 | 0 | h->sh.i_last_mb = (height * i_slice_num + round_bias) / h->param.i_slice_count * width - 1; |
3199 | 0 | } |
3200 | 0 | } |
3201 | 0 | h->sh.i_last_mb = X264_MIN( h->sh.i_last_mb, last_thread_mb ); |
3202 | 0 | if( slice_write( h ) ) |
3203 | 0 | goto fail; |
3204 | 0 | h->sh.i_first_mb = h->sh.i_last_mb + 1; |
3205 | | // if i_first_mb is not the last mb in a row then go to the next mb in MBAFF order |
3206 | 0 | if( SLICE_MBAFF && h->sh.i_first_mb % h->mb.i_mb_width ) |
3207 | 0 | h->sh.i_first_mb -= h->mb.i_mb_stride; |
3208 | 0 | } |
3209 | | |
3210 | 0 | return (void *)0; |
3211 | | |
3212 | 0 | fail: |
3213 | | /* Tell other threads we're done, so they wouldn't wait for it */ |
3214 | 0 | if( h->param.b_sliced_threads ) |
3215 | 0 | x264_threadslice_cond_broadcast( h, 2 ); |
3216 | 0 | return (void *)-1; |
3217 | 0 | } |
3218 | | |
3219 | | static int threaded_slices_write( x264_t *h ) |
3220 | 0 | { |
3221 | 0 | int round_bias = h->param.i_avcintra_class ? 0 : h->param.i_slice_count/2; |
3222 | | |
3223 | | /* set first/last mb and sync contexts */ |
3224 | 0 | for( int i = 0; i < h->param.i_threads; i++ ) |
3225 | 0 | { |
3226 | 0 | x264_t *t = h->thread[i]; |
3227 | 0 | if( i ) |
3228 | 0 | { |
3229 | 0 | t->param = h->param; |
3230 | 0 | memcpy( &t->i_frame, &h->i_frame, offsetof(x264_t, rc) - offsetof(x264_t, i_frame) ); |
3231 | 0 | } |
3232 | 0 | int height = h->mb.i_mb_height >> PARAM_INTERLACED; |
3233 | 0 | t->i_threadslice_start = ((height * i + round_bias) / h->param.i_threads) << PARAM_INTERLACED; |
3234 | 0 | t->i_threadslice_end = ((height * (i+1) + round_bias) / h->param.i_threads) << PARAM_INTERLACED; |
3235 | 0 | t->sh.i_first_mb = t->i_threadslice_start * h->mb.i_mb_width; |
3236 | 0 | t->sh.i_last_mb = t->i_threadslice_end * h->mb.i_mb_width - 1; |
3237 | 0 | } |
3238 | |
|
3239 | 0 | x264_analyse_weight_frame( h, h->mb.i_mb_height*16 + 16 ); |
3240 | |
|
3241 | 0 | x264_threads_distribute_ratecontrol( h ); |
3242 | | |
3243 | | /* setup */ |
3244 | 0 | for( int i = 0; i < h->param.i_threads; i++ ) |
3245 | 0 | { |
3246 | 0 | h->thread[i]->i_thread_idx = i; |
3247 | 0 | h->thread[i]->b_thread_active = 1; |
3248 | 0 | x264_threadslice_cond_broadcast( h->thread[i], 0 ); |
3249 | 0 | } |
3250 | | /* dispatch */ |
3251 | 0 | for( int i = 0; i < h->param.i_threads; i++ ) |
3252 | 0 | x264_threadpool_run( h->threadpool, (void*)slices_write, h->thread[i] ); |
3253 | | /* wait */ |
3254 | 0 | for( int i = 0; i < h->param.i_threads; i++ ) |
3255 | 0 | x264_threadslice_cond_wait( h->thread[i], 1 ); |
3256 | |
|
3257 | 0 | x264_threads_merge_ratecontrol( h ); |
3258 | |
|
3259 | 0 | for( int i = 1; i < h->param.i_threads; i++ ) |
3260 | 0 | { |
3261 | 0 | x264_t *t = h->thread[i]; |
3262 | 0 | for( int j = 0; j < t->out.i_nal; j++ ) |
3263 | 0 | { |
3264 | 0 | h->out.nal[h->out.i_nal] = t->out.nal[j]; |
3265 | 0 | h->out.i_nal++; |
3266 | 0 | nal_check_buffer( h ); |
3267 | 0 | } |
3268 | | /* All entries in stat.frame are ints except for ssd/ssim. */ |
3269 | 0 | for( size_t j = 0; j < (offsetof(x264_t,stat.frame.i_ssd) - offsetof(x264_t,stat.frame.i_mv_bits)) / sizeof(int); j++ ) |
3270 | 0 | ((int*)&h->stat.frame)[j] += ((int*)&t->stat.frame)[j]; |
3271 | 0 | for( int j = 0; j < 3; j++ ) |
3272 | 0 | h->stat.frame.i_ssd[j] += t->stat.frame.i_ssd[j]; |
3273 | 0 | h->stat.frame.f_ssim += t->stat.frame.f_ssim; |
3274 | 0 | h->stat.frame.i_ssim_cnt += t->stat.frame.i_ssim_cnt; |
3275 | 0 | } |
3276 | |
|
3277 | 0 | return 0; |
3278 | 0 | } |
3279 | | |
3280 | | void x264_encoder_intra_refresh( x264_t *h ) |
3281 | 0 | { |
3282 | 0 | h = h->thread[h->i_thread_phase]; |
3283 | 0 | h->b_queued_intra_refresh = 1; |
3284 | 0 | } Unexecuted instantiation: x264_8_encoder_intra_refresh Unexecuted instantiation: x264_10_encoder_intra_refresh |
3285 | | |
3286 | | int x264_encoder_invalidate_reference( x264_t *h, int64_t pts ) |
3287 | 0 | { |
3288 | 0 | if( h->param.i_bframe ) |
3289 | 0 | { |
3290 | 0 | x264_log( h, X264_LOG_ERROR, "x264_encoder_invalidate_reference is not supported with B-frames enabled\n" ); |
3291 | 0 | return -1; |
3292 | 0 | } |
3293 | 0 | if( h->param.b_intra_refresh ) |
3294 | 0 | { |
3295 | 0 | x264_log( h, X264_LOG_ERROR, "x264_encoder_invalidate_reference is not supported with intra refresh enabled\n" ); |
3296 | 0 | return -1; |
3297 | 0 | } |
3298 | 0 | h = h->thread[h->i_thread_phase]; |
3299 | 0 | if( pts >= h->i_last_idr_pts ) |
3300 | 0 | { |
3301 | 0 | for( int i = 0; h->frames.reference[i]; i++ ) |
3302 | 0 | if( pts <= h->frames.reference[i]->i_pts ) |
3303 | 0 | h->frames.reference[i]->b_corrupt = 1; |
3304 | 0 | if( pts <= h->fdec->i_pts ) |
3305 | 0 | h->fdec->b_corrupt = 1; |
3306 | 0 | } |
3307 | 0 | return 0; |
3308 | 0 | } Unexecuted instantiation: x264_8_encoder_invalidate_reference Unexecuted instantiation: x264_10_encoder_invalidate_reference |
3309 | | |
3310 | | /**************************************************************************** |
3311 | | * x264_encoder_encode: |
3312 | | * XXX: i_poc : is the poc of the current given picture |
3313 | | * i_frame : is the number of the frame being coded |
3314 | | * ex: type frame poc |
3315 | | * I 0 2*0 |
3316 | | * P 1 2*3 |
3317 | | * B 2 2*1 |
3318 | | * B 3 2*2 |
3319 | | * P 4 2*6 |
3320 | | * B 5 2*4 |
3321 | | * B 6 2*5 |
3322 | | ****************************************************************************/ |
3323 | | int x264_encoder_encode( x264_t *h, |
3324 | | x264_nal_t **pp_nal, int *pi_nal, |
3325 | | x264_picture_t *pic_in, |
3326 | | x264_picture_t *pic_out ) |
3327 | 0 | { |
3328 | 0 | x264_t *thread_current, *thread_prev, *thread_oldest; |
3329 | 0 | int i_nal_type, i_nal_ref_idc, i_global_qp; |
3330 | 0 | int overhead = NALU_OVERHEAD; |
3331 | |
|
3332 | | #if HAVE_OPENCL |
3333 | | if( h->opencl.b_fatal_error ) |
3334 | | return -1; |
3335 | | #endif |
3336 | |
|
3337 | 0 | if( h->i_thread_frames > 1 ) |
3338 | 0 | { |
3339 | 0 | thread_prev = h->thread[ h->i_thread_phase ]; |
3340 | 0 | h->i_thread_phase = (h->i_thread_phase + 1) % h->i_thread_frames; |
3341 | 0 | thread_current = h->thread[ h->i_thread_phase ]; |
3342 | 0 | thread_oldest = h->thread[ (h->i_thread_phase + 1) % h->i_thread_frames ]; |
3343 | 0 | thread_sync_context( thread_current, thread_prev ); |
3344 | 0 | x264_thread_sync_ratecontrol( thread_current, thread_prev, thread_oldest ); |
3345 | 0 | h = thread_current; |
3346 | 0 | } |
3347 | 0 | else |
3348 | 0 | { |
3349 | 0 | thread_current = |
3350 | 0 | thread_oldest = h; |
3351 | 0 | } |
3352 | 0 | h->i_cpb_delay_pir_offset = h->i_cpb_delay_pir_offset_next; |
3353 | | |
3354 | | /* no data out */ |
3355 | 0 | *pi_nal = 0; |
3356 | 0 | *pp_nal = NULL; |
3357 | | |
3358 | | /* ------------------- Setup new frame from picture -------------------- */ |
3359 | 0 | if( pic_in != NULL ) |
3360 | 0 | { |
3361 | 0 | if( h->lookahead->b_exit_thread ) |
3362 | 0 | { |
3363 | 0 | x264_log( h, X264_LOG_ERROR, "lookahead thread is already stopped\n" ); |
3364 | 0 | return -1; |
3365 | 0 | } |
3366 | | |
3367 | | /* 1: Copy the picture to a frame and move it to a buffer */ |
3368 | 0 | x264_frame_t *fenc = x264_frame_pop_unused( h, 0 ); |
3369 | 0 | if( !fenc ) |
3370 | 0 | return -1; |
3371 | | |
3372 | 0 | if( x264_frame_copy_picture( h, fenc, pic_in ) < 0 ) |
3373 | 0 | return -1; |
3374 | | |
3375 | 0 | if( h->param.i_width != 16 * h->mb.i_mb_width || |
3376 | 0 | h->param.i_height != 16 * h->mb.i_mb_height ) |
3377 | 0 | x264_frame_expand_border_mod16( h, fenc ); |
3378 | |
|
3379 | 0 | fenc->i_frame = h->frames.i_input++; |
3380 | |
|
3381 | 0 | if( fenc->i_frame == 0 ) |
3382 | 0 | h->frames.i_first_pts = fenc->i_pts; |
3383 | 0 | if( h->frames.i_bframe_delay && fenc->i_frame == h->frames.i_bframe_delay ) |
3384 | 0 | h->frames.i_bframe_delay_time = fenc->i_pts - h->frames.i_first_pts; |
3385 | |
|
3386 | 0 | if( h->param.b_vfr_input && fenc->i_pts <= h->frames.i_largest_pts ) |
3387 | 0 | x264_log( h, X264_LOG_WARNING, "non-strictly-monotonic PTS\n" ); |
3388 | |
|
3389 | 0 | h->frames.i_second_largest_pts = h->frames.i_largest_pts; |
3390 | 0 | h->frames.i_largest_pts = fenc->i_pts; |
3391 | |
|
3392 | 0 | if( (fenc->i_pic_struct < PIC_STRUCT_AUTO) || (fenc->i_pic_struct > PIC_STRUCT_TRIPLE) ) |
3393 | 0 | fenc->i_pic_struct = PIC_STRUCT_AUTO; |
3394 | |
|
3395 | 0 | if( fenc->i_pic_struct == PIC_STRUCT_AUTO ) |
3396 | 0 | { |
3397 | 0 | #if HAVE_INTERLACED |
3398 | 0 | int b_interlaced = fenc->param ? fenc->param->b_interlaced : h->param.b_interlaced; |
3399 | | #else |
3400 | | int b_interlaced = 0; |
3401 | | #endif |
3402 | 0 | if( b_interlaced ) |
3403 | 0 | { |
3404 | 0 | int b_tff = fenc->param ? fenc->param->b_tff : h->param.b_tff; |
3405 | 0 | fenc->i_pic_struct = b_tff ? PIC_STRUCT_TOP_BOTTOM : PIC_STRUCT_BOTTOM_TOP; |
3406 | 0 | } |
3407 | 0 | else |
3408 | 0 | fenc->i_pic_struct = PIC_STRUCT_PROGRESSIVE; |
3409 | 0 | } |
3410 | |
|
3411 | 0 | if( h->param.rc.b_mb_tree && h->param.rc.b_stat_read ) |
3412 | 0 | { |
3413 | 0 | if( x264_macroblock_tree_read( h, fenc, pic_in->prop.quant_offsets ) ) |
3414 | 0 | return -1; |
3415 | 0 | } |
3416 | 0 | else |
3417 | 0 | x264_adaptive_quant_frame( h, fenc, pic_in->prop.quant_offsets ); |
3418 | | |
3419 | 0 | if( pic_in->prop.quant_offsets_free ) |
3420 | 0 | pic_in->prop.quant_offsets_free( pic_in->prop.quant_offsets ); |
3421 | |
|
3422 | 0 | if( h->frames.b_have_lowres ) |
3423 | 0 | x264_frame_init_lowres( h, fenc ); |
3424 | | |
3425 | | /* 2: Place the frame into the queue for its slice type decision */ |
3426 | 0 | x264_lookahead_put_frame( h, fenc ); |
3427 | |
|
3428 | 0 | if( h->frames.i_input <= h->frames.i_delay + 1 - h->i_thread_frames ) |
3429 | 0 | { |
3430 | | /* Nothing yet to encode, waiting for filling of buffers */ |
3431 | 0 | pic_out->i_type = X264_TYPE_AUTO; |
3432 | 0 | return 0; |
3433 | 0 | } |
3434 | 0 | } |
3435 | 0 | else |
3436 | 0 | { |
3437 | | /* signal kills for lookahead thread */ |
3438 | 0 | x264_pthread_mutex_lock( &h->lookahead->ifbuf.mutex ); |
3439 | 0 | h->lookahead->b_exit_thread = 1; |
3440 | 0 | x264_pthread_cond_broadcast( &h->lookahead->ifbuf.cv_fill ); |
3441 | 0 | x264_pthread_mutex_unlock( &h->lookahead->ifbuf.mutex ); |
3442 | 0 | } |
3443 | | |
3444 | 0 | h->i_frame++; |
3445 | | /* 3: The picture is analyzed in the lookahead */ |
3446 | 0 | if( !h->frames.current[0] ) |
3447 | 0 | x264_lookahead_get_frames( h ); |
3448 | |
|
3449 | 0 | if( !h->frames.current[0] && x264_lookahead_is_empty( h ) ) |
3450 | 0 | return encoder_frame_end( thread_oldest, thread_current, pp_nal, pi_nal, pic_out ); |
3451 | | |
3452 | | /* ------------------- Get frame to be encoded ------------------------- */ |
3453 | | /* 4: get picture to encode */ |
3454 | 0 | h->fenc = x264_frame_shift( h->frames.current ); |
3455 | | |
3456 | | /* If applicable, wait for previous frame reconstruction to finish */ |
3457 | 0 | if( h->param.b_sliced_threads ) |
3458 | 0 | if( threadpool_wait_all( h ) < 0 ) |
3459 | 0 | return -1; |
3460 | | |
3461 | 0 | if( h->i_frame == 0 ) |
3462 | 0 | h->i_reordered_pts_delay = h->fenc->i_reordered_pts; |
3463 | 0 | if( h->reconfig ) |
3464 | 0 | { |
3465 | 0 | x264_encoder_reconfig_apply( h, &h->reconfig_h->param ); |
3466 | 0 | h->reconfig = 0; |
3467 | 0 | } |
3468 | 0 | if( h->fenc->param ) |
3469 | 0 | { |
3470 | 0 | x264_encoder_reconfig_apply( h, h->fenc->param ); |
3471 | 0 | if( h->fenc->param->param_free ) |
3472 | 0 | { |
3473 | 0 | x264_param_cleanup( h->fenc->param ); |
3474 | 0 | h->fenc->param->param_free( h->fenc->param ); |
3475 | 0 | h->fenc->param = NULL; |
3476 | 0 | } |
3477 | 0 | } |
3478 | 0 | x264_ratecontrol_zone_init( h ); |
3479 | | |
3480 | | // ok to call this before encoding any frames, since the initial values of fdec have b_kept_as_ref=0 |
3481 | 0 | if( reference_update( h ) ) |
3482 | 0 | return -1; |
3483 | 0 | h->fdec->i_lines_completed = -1; |
3484 | |
|
3485 | 0 | if( !IS_X264_TYPE_I( h->fenc->i_type ) ) |
3486 | 0 | { |
3487 | 0 | int valid_refs_left = 0; |
3488 | 0 | for( int i = 0; h->frames.reference[i]; i++ ) |
3489 | 0 | if( !h->frames.reference[i]->b_corrupt ) |
3490 | 0 | valid_refs_left++; |
3491 | | /* No valid reference frames left: force an IDR. */ |
3492 | 0 | if( !valid_refs_left ) |
3493 | 0 | { |
3494 | 0 | h->fenc->b_keyframe = 1; |
3495 | 0 | h->fenc->i_type = X264_TYPE_IDR; |
3496 | 0 | } |
3497 | 0 | } |
3498 | |
|
3499 | 0 | if( h->fenc->b_keyframe ) |
3500 | 0 | { |
3501 | 0 | h->frames.i_last_keyframe = h->fenc->i_frame; |
3502 | 0 | if( h->fenc->i_type == X264_TYPE_IDR ) |
3503 | 0 | { |
3504 | 0 | h->i_frame_num = 0; |
3505 | 0 | h->frames.i_last_idr = h->fenc->i_frame; |
3506 | 0 | } |
3507 | 0 | } |
3508 | 0 | h->sh.i_mmco_command_count = |
3509 | 0 | h->sh.i_mmco_remove_from_end = 0; |
3510 | 0 | h->b_ref_reorder[0] = |
3511 | 0 | h->b_ref_reorder[1] = 0; |
3512 | 0 | h->fdec->i_poc = |
3513 | 0 | h->fenc->i_poc = 2 * ( h->fenc->i_frame - X264_MAX( h->frames.i_last_idr, 0 ) ); |
3514 | | |
3515 | | /* ------------------- Setup frame context ----------------------------- */ |
3516 | | /* 5: Init data dependent of frame type */ |
3517 | 0 | if( h->fenc->i_type == X264_TYPE_IDR ) |
3518 | 0 | { |
3519 | | /* reset ref pictures */ |
3520 | 0 | i_nal_type = NAL_SLICE_IDR; |
3521 | 0 | i_nal_ref_idc = NAL_PRIORITY_HIGHEST; |
3522 | 0 | h->sh.i_type = SLICE_TYPE_I; |
3523 | 0 | reference_reset( h ); |
3524 | 0 | h->frames.i_poc_last_open_gop = -1; |
3525 | 0 | } |
3526 | 0 | else if( h->fenc->i_type == X264_TYPE_I ) |
3527 | 0 | { |
3528 | 0 | i_nal_type = NAL_SLICE; |
3529 | 0 | i_nal_ref_idc = NAL_PRIORITY_HIGH; /* Not completely true but for now it is (as all I/P are kept as ref)*/ |
3530 | 0 | h->sh.i_type = SLICE_TYPE_I; |
3531 | 0 | reference_hierarchy_reset( h ); |
3532 | 0 | if( h->param.b_open_gop ) |
3533 | 0 | h->frames.i_poc_last_open_gop = h->fenc->b_keyframe ? h->fenc->i_poc : -1; |
3534 | 0 | } |
3535 | 0 | else if( h->fenc->i_type == X264_TYPE_P ) |
3536 | 0 | { |
3537 | 0 | i_nal_type = NAL_SLICE; |
3538 | 0 | i_nal_ref_idc = NAL_PRIORITY_HIGH; /* Not completely true but for now it is (as all I/P are kept as ref)*/ |
3539 | 0 | h->sh.i_type = SLICE_TYPE_P; |
3540 | 0 | reference_hierarchy_reset( h ); |
3541 | 0 | h->frames.i_poc_last_open_gop = -1; |
3542 | 0 | } |
3543 | 0 | else if( h->fenc->i_type == X264_TYPE_BREF ) |
3544 | 0 | { |
3545 | 0 | i_nal_type = NAL_SLICE; |
3546 | 0 | i_nal_ref_idc = h->param.i_bframe_pyramid == X264_B_PYRAMID_STRICT ? NAL_PRIORITY_LOW : NAL_PRIORITY_HIGH; |
3547 | 0 | h->sh.i_type = SLICE_TYPE_B; |
3548 | 0 | reference_hierarchy_reset( h ); |
3549 | 0 | } |
3550 | 0 | else /* B frame */ |
3551 | 0 | { |
3552 | 0 | i_nal_type = NAL_SLICE; |
3553 | 0 | i_nal_ref_idc = NAL_PRIORITY_DISPOSABLE; |
3554 | 0 | h->sh.i_type = SLICE_TYPE_B; |
3555 | 0 | } |
3556 | |
|
3557 | 0 | h->fdec->i_type = h->fenc->i_type; |
3558 | 0 | h->fdec->i_frame = h->fenc->i_frame; |
3559 | 0 | h->fenc->b_kept_as_ref = |
3560 | 0 | h->fdec->b_kept_as_ref = i_nal_ref_idc != NAL_PRIORITY_DISPOSABLE && h->param.i_keyint_max > 1; |
3561 | |
|
3562 | 0 | h->fdec->mb_info = h->fenc->mb_info; |
3563 | 0 | h->fdec->mb_info_free = h->fenc->mb_info_free; |
3564 | 0 | h->fenc->mb_info = NULL; |
3565 | 0 | h->fenc->mb_info_free = NULL; |
3566 | |
|
3567 | 0 | h->fdec->i_pts = h->fenc->i_pts; |
3568 | 0 | if( h->frames.i_bframe_delay ) |
3569 | 0 | { |
3570 | 0 | int64_t *prev_reordered_pts = thread_current->frames.i_prev_reordered_pts; |
3571 | 0 | h->fdec->i_dts = h->i_frame > h->frames.i_bframe_delay |
3572 | 0 | ? prev_reordered_pts[ (h->i_frame - h->frames.i_bframe_delay) % h->frames.i_bframe_delay ] |
3573 | 0 | : h->fenc->i_reordered_pts - h->frames.i_bframe_delay_time; |
3574 | 0 | prev_reordered_pts[ h->i_frame % h->frames.i_bframe_delay ] = h->fenc->i_reordered_pts; |
3575 | 0 | } |
3576 | 0 | else |
3577 | 0 | h->fdec->i_dts = h->fenc->i_reordered_pts; |
3578 | 0 | if( h->fenc->i_type == X264_TYPE_IDR ) |
3579 | 0 | h->i_last_idr_pts = h->fdec->i_pts; |
3580 | | |
3581 | | /* ------------------- Init ----------------------------- */ |
3582 | | /* build ref list 0/1 */ |
3583 | 0 | reference_build_list( h, h->fdec->i_poc ); |
3584 | | |
3585 | | /* ---------------------- Write the bitstream -------------------------- */ |
3586 | | /* Init bitstream context */ |
3587 | 0 | if( h->param.b_sliced_threads ) |
3588 | 0 | { |
3589 | 0 | for( int i = 0; i < h->param.i_threads; i++ ) |
3590 | 0 | { |
3591 | 0 | bs_init( &h->thread[i]->out.bs, h->thread[i]->out.p_bitstream, h->thread[i]->out.i_bitstream ); |
3592 | 0 | h->thread[i]->out.i_nal = 0; |
3593 | 0 | } |
3594 | 0 | } |
3595 | 0 | else |
3596 | 0 | { |
3597 | 0 | bs_init( &h->out.bs, h->out.p_bitstream, h->out.i_bitstream ); |
3598 | 0 | h->out.i_nal = 0; |
3599 | 0 | } |
3600 | |
|
3601 | 0 | if( h->param.b_aud ) |
3602 | 0 | { |
3603 | 0 | int pic_type; |
3604 | |
|
3605 | 0 | if( h->sh.i_type == SLICE_TYPE_I ) |
3606 | 0 | pic_type = 0; |
3607 | 0 | else if( h->sh.i_type == SLICE_TYPE_P ) |
3608 | 0 | pic_type = 1; |
3609 | 0 | else if( h->sh.i_type == SLICE_TYPE_B ) |
3610 | 0 | pic_type = 2; |
3611 | 0 | else |
3612 | 0 | pic_type = 7; |
3613 | |
|
3614 | 0 | nal_start( h, NAL_AUD, NAL_PRIORITY_DISPOSABLE ); |
3615 | 0 | bs_write( &h->out.bs, 3, pic_type ); |
3616 | 0 | bs_rbsp_trailing( &h->out.bs ); |
3617 | 0 | bs_flush( &h->out.bs ); |
3618 | 0 | if( nal_end( h ) ) |
3619 | 0 | return -1; |
3620 | 0 | overhead += h->out.nal[h->out.i_nal-1].i_payload + NALU_OVERHEAD; |
3621 | 0 | } |
3622 | | |
3623 | 0 | h->i_nal_type = i_nal_type; |
3624 | 0 | h->i_nal_ref_idc = i_nal_ref_idc; |
3625 | |
|
3626 | 0 | if( h->param.b_intra_refresh ) |
3627 | 0 | { |
3628 | 0 | if( IS_X264_TYPE_I( h->fenc->i_type ) ) |
3629 | 0 | { |
3630 | 0 | h->fdec->i_frames_since_pir = 0; |
3631 | 0 | h->b_queued_intra_refresh = 0; |
3632 | | /* PIR is currently only supported with ref == 1, so any intra frame effectively refreshes |
3633 | | * the whole frame and counts as an intra refresh. */ |
3634 | 0 | h->fdec->f_pir_position = h->mb.i_mb_width; |
3635 | 0 | } |
3636 | 0 | else if( h->fenc->i_type == X264_TYPE_P ) |
3637 | 0 | { |
3638 | 0 | int pocdiff = (h->fdec->i_poc - h->fref[0][0]->i_poc)/2; |
3639 | 0 | float increment = X264_MAX( ((float)h->mb.i_mb_width-1) / h->param.i_keyint_max, 1 ); |
3640 | 0 | h->fdec->f_pir_position = h->fref[0][0]->f_pir_position; |
3641 | 0 | h->fdec->i_frames_since_pir = h->fref[0][0]->i_frames_since_pir + pocdiff; |
3642 | 0 | if( h->fdec->i_frames_since_pir >= h->param.i_keyint_max || |
3643 | 0 | (h->b_queued_intra_refresh && h->fdec->f_pir_position + 0.5 >= h->mb.i_mb_width) ) |
3644 | 0 | { |
3645 | 0 | h->fdec->f_pir_position = 0; |
3646 | 0 | h->fdec->i_frames_since_pir = 0; |
3647 | 0 | h->b_queued_intra_refresh = 0; |
3648 | 0 | h->fenc->b_keyframe = 1; |
3649 | 0 | } |
3650 | 0 | h->fdec->i_pir_start_col = h->fdec->f_pir_position+0.5; |
3651 | 0 | h->fdec->f_pir_position += increment * pocdiff; |
3652 | 0 | h->fdec->i_pir_end_col = h->fdec->f_pir_position+0.5; |
3653 | | /* If our intra refresh has reached the right side of the frame, we're done. */ |
3654 | 0 | if( h->fdec->i_pir_end_col >= h->mb.i_mb_width - 1 ) |
3655 | 0 | { |
3656 | 0 | h->fdec->f_pir_position = h->mb.i_mb_width; |
3657 | 0 | h->fdec->i_pir_end_col = h->mb.i_mb_width - 1; |
3658 | 0 | } |
3659 | 0 | } |
3660 | 0 | } |
3661 | |
|
3662 | 0 | if( h->fenc->b_keyframe ) |
3663 | 0 | { |
3664 | | /* Write SPS and PPS */ |
3665 | 0 | if( h->param.b_repeat_headers ) |
3666 | 0 | { |
3667 | | /* generate sequence parameters */ |
3668 | 0 | nal_start( h, NAL_SPS, NAL_PRIORITY_HIGHEST ); |
3669 | 0 | x264_sps_write( &h->out.bs, h->sps ); |
3670 | 0 | if( nal_end( h ) ) |
3671 | 0 | return -1; |
3672 | | /* Pad AUD/SPS to 256 bytes like Panasonic */ |
3673 | 0 | if( h->param.i_avcintra_class ) |
3674 | 0 | h->out.nal[h->out.i_nal-1].i_padding = 256 - bs_pos( &h->out.bs ) / 8 - 2*NALU_OVERHEAD; |
3675 | 0 | overhead += h->out.nal[h->out.i_nal-1].i_payload + h->out.nal[h->out.i_nal-1].i_padding + NALU_OVERHEAD; |
3676 | | |
3677 | | /* generate picture parameters */ |
3678 | 0 | nal_start( h, NAL_PPS, NAL_PRIORITY_HIGHEST ); |
3679 | 0 | x264_pps_write( &h->out.bs, h->sps, h->pps ); |
3680 | 0 | if( nal_end( h ) ) |
3681 | 0 | return -1; |
3682 | 0 | if( h->param.i_avcintra_class ) |
3683 | 0 | { |
3684 | 0 | int total_len = 256; |
3685 | | /* Sony XAVC uses an oversized PPS instead of SEI padding */ |
3686 | 0 | if( h->param.i_avcintra_flavor == X264_AVCINTRA_FLAVOR_SONY ) |
3687 | 0 | total_len += h->param.i_height >= 1080 ? 18*512 : 10*512; |
3688 | 0 | h->out.nal[h->out.i_nal-1].i_padding = total_len - h->out.nal[h->out.i_nal-1].i_payload - NALU_OVERHEAD; |
3689 | 0 | } |
3690 | 0 | overhead += h->out.nal[h->out.i_nal-1].i_payload + h->out.nal[h->out.i_nal-1].i_padding + NALU_OVERHEAD; |
3691 | 0 | } |
3692 | | |
3693 | | /* when frame threading is used, buffering period sei is written in encoder_frame_end */ |
3694 | 0 | if( h->i_thread_frames == 1 && h->sps->vui.b_nal_hrd_parameters_present ) |
3695 | 0 | { |
3696 | 0 | x264_hrd_fullness( h ); |
3697 | 0 | nal_start( h, NAL_SEI, NAL_PRIORITY_DISPOSABLE ); |
3698 | 0 | x264_sei_buffering_period_write( h, &h->out.bs ); |
3699 | 0 | if( nal_end( h ) ) |
3700 | 0 | return -1; |
3701 | 0 | overhead += h->out.nal[h->out.i_nal-1].i_payload + SEI_OVERHEAD; |
3702 | 0 | } |
3703 | 0 | } |
3704 | | |
3705 | | /* write extra sei */ |
3706 | 0 | for( int i = 0; i < h->fenc->extra_sei.num_payloads; i++ ) |
3707 | 0 | { |
3708 | 0 | nal_start( h, NAL_SEI, NAL_PRIORITY_DISPOSABLE ); |
3709 | 0 | x264_sei_write( &h->out.bs, h->fenc->extra_sei.payloads[i].payload, h->fenc->extra_sei.payloads[i].payload_size, |
3710 | 0 | h->fenc->extra_sei.payloads[i].payload_type ); |
3711 | 0 | if( nal_end( h ) ) |
3712 | 0 | return -1; |
3713 | 0 | overhead += h->out.nal[h->out.i_nal-1].i_payload + SEI_OVERHEAD; |
3714 | 0 | if( h->fenc->extra_sei.sei_free ) |
3715 | 0 | { |
3716 | 0 | h->fenc->extra_sei.sei_free( h->fenc->extra_sei.payloads[i].payload ); |
3717 | 0 | h->fenc->extra_sei.payloads[i].payload = NULL; |
3718 | 0 | } |
3719 | 0 | } |
3720 | | |
3721 | 0 | if( h->fenc->extra_sei.sei_free ) |
3722 | 0 | { |
3723 | 0 | h->fenc->extra_sei.sei_free( h->fenc->extra_sei.payloads ); |
3724 | 0 | h->fenc->extra_sei.payloads = NULL; |
3725 | 0 | h->fenc->extra_sei.sei_free = NULL; |
3726 | 0 | } |
3727 | |
|
3728 | 0 | if( h->fenc->b_keyframe ) |
3729 | 0 | { |
3730 | | /* Avid's decoder strictly wants two SEIs for AVC-Intra so we can't insert the x264 SEI */ |
3731 | 0 | if( h->param.b_repeat_headers && h->fenc->i_frame == 0 && !h->param.i_avcintra_class ) |
3732 | 0 | { |
3733 | | /* identify ourself */ |
3734 | 0 | nal_start( h, NAL_SEI, NAL_PRIORITY_DISPOSABLE ); |
3735 | 0 | if( x264_sei_version_write( h, &h->out.bs ) ) |
3736 | 0 | return -1; |
3737 | 0 | if( nal_end( h ) ) |
3738 | 0 | return -1; |
3739 | 0 | overhead += h->out.nal[h->out.i_nal-1].i_payload + SEI_OVERHEAD; |
3740 | 0 | } |
3741 | | |
3742 | 0 | if( h->fenc->i_type != X264_TYPE_IDR ) |
3743 | 0 | { |
3744 | 0 | int time_to_recovery = h->param.b_open_gop ? 0 : X264_MIN( h->mb.i_mb_width - 1, h->param.i_keyint_max ) + h->param.i_bframe - 1; |
3745 | 0 | nal_start( h, NAL_SEI, NAL_PRIORITY_DISPOSABLE ); |
3746 | 0 | x264_sei_recovery_point_write( h, &h->out.bs, time_to_recovery ); |
3747 | 0 | if( nal_end( h ) ) |
3748 | 0 | return -1; |
3749 | 0 | overhead += h->out.nal[h->out.i_nal-1].i_payload + SEI_OVERHEAD; |
3750 | 0 | } |
3751 | | |
3752 | 0 | if( h->param.mastering_display.b_mastering_display ) |
3753 | 0 | { |
3754 | 0 | nal_start( h, NAL_SEI, NAL_PRIORITY_DISPOSABLE ); |
3755 | 0 | x264_sei_mastering_display_write( h, &h->out.bs ); |
3756 | 0 | if( nal_end( h ) ) |
3757 | 0 | return -1; |
3758 | 0 | overhead += h->out.nal[h->out.i_nal-1].i_payload + SEI_OVERHEAD; |
3759 | 0 | } |
3760 | | |
3761 | 0 | if( h->param.content_light_level.b_cll ) |
3762 | 0 | { |
3763 | 0 | nal_start( h, NAL_SEI, NAL_PRIORITY_DISPOSABLE ); |
3764 | 0 | x264_sei_content_light_level_write( h, &h->out.bs ); |
3765 | 0 | if( nal_end( h ) ) |
3766 | 0 | return -1; |
3767 | 0 | overhead += h->out.nal[h->out.i_nal-1].i_payload + SEI_OVERHEAD; |
3768 | 0 | } |
3769 | | |
3770 | 0 | if( h->param.i_alternative_transfer != 2 ) |
3771 | 0 | { |
3772 | 0 | nal_start( h, NAL_SEI, NAL_PRIORITY_DISPOSABLE ); |
3773 | 0 | x264_sei_alternative_transfer_write( h, &h->out.bs ); |
3774 | 0 | if( nal_end( h ) ) |
3775 | 0 | return -1; |
3776 | 0 | overhead += h->out.nal[h->out.i_nal-1].i_payload + SEI_OVERHEAD; |
3777 | 0 | } |
3778 | 0 | } |
3779 | | |
3780 | 0 | if( h->param.i_frame_packing >= 0 && (h->fenc->b_keyframe || h->param.i_frame_packing == 5) ) |
3781 | 0 | { |
3782 | 0 | nal_start( h, NAL_SEI, NAL_PRIORITY_DISPOSABLE ); |
3783 | 0 | x264_sei_frame_packing_write( h, &h->out.bs ); |
3784 | 0 | if( nal_end( h ) ) |
3785 | 0 | return -1; |
3786 | 0 | overhead += h->out.nal[h->out.i_nal-1].i_payload + SEI_OVERHEAD; |
3787 | 0 | } |
3788 | | |
3789 | | /* generate sei pic timing */ |
3790 | 0 | if( h->sps->vui.b_pic_struct_present || h->sps->vui.b_nal_hrd_parameters_present ) |
3791 | 0 | { |
3792 | 0 | nal_start( h, NAL_SEI, NAL_PRIORITY_DISPOSABLE ); |
3793 | 0 | x264_sei_pic_timing_write( h, &h->out.bs ); |
3794 | 0 | if( nal_end( h ) ) |
3795 | 0 | return -1; |
3796 | 0 | overhead += h->out.nal[h->out.i_nal-1].i_payload + SEI_OVERHEAD; |
3797 | 0 | } |
3798 | | |
3799 | | /* As required by Blu-ray. */ |
3800 | 0 | if( !IS_X264_TYPE_B( h->fenc->i_type ) && h->b_sh_backup ) |
3801 | 0 | { |
3802 | 0 | h->b_sh_backup = 0; |
3803 | 0 | nal_start( h, NAL_SEI, NAL_PRIORITY_DISPOSABLE ); |
3804 | 0 | x264_sei_dec_ref_pic_marking_write( h, &h->out.bs ); |
3805 | 0 | if( nal_end( h ) ) |
3806 | 0 | return -1; |
3807 | 0 | overhead += h->out.nal[h->out.i_nal-1].i_payload + SEI_OVERHEAD; |
3808 | 0 | } |
3809 | | |
3810 | 0 | if( h->fenc->b_keyframe && h->param.b_intra_refresh ) |
3811 | 0 | h->i_cpb_delay_pir_offset_next = h->fenc->i_cpb_delay; |
3812 | | |
3813 | | /* Filler space: 10 or 18 SEIs' worth of space, depending on resolution */ |
3814 | 0 | if( h->param.i_avcintra_class && h->param.i_avcintra_flavor != X264_AVCINTRA_FLAVOR_SONY ) |
3815 | 0 | { |
3816 | | /* Write an empty filler NAL to mimic the AUD in the P2 format*/ |
3817 | 0 | nal_start( h, NAL_FILLER, NAL_PRIORITY_DISPOSABLE ); |
3818 | 0 | x264_filler_write( h, &h->out.bs, 0 ); |
3819 | 0 | if( nal_end( h ) ) |
3820 | 0 | return -1; |
3821 | 0 | overhead += h->out.nal[h->out.i_nal-1].i_payload + NALU_OVERHEAD; |
3822 | | |
3823 | | /* All lengths are magic lengths that decoders expect to see */ |
3824 | | /* "UMID" SEI */ |
3825 | 0 | nal_start( h, NAL_SEI, NAL_PRIORITY_DISPOSABLE ); |
3826 | 0 | if( x264_sei_avcintra_umid_write( h, &h->out.bs ) < 0 ) |
3827 | 0 | return -1; |
3828 | 0 | if( nal_end( h ) ) |
3829 | 0 | return -1; |
3830 | 0 | overhead += h->out.nal[h->out.i_nal-1].i_payload + SEI_OVERHEAD; |
3831 | |
|
3832 | 0 | int unpadded_len; |
3833 | 0 | int total_len; |
3834 | 0 | if( h->param.i_height == 1080 ) |
3835 | 0 | { |
3836 | 0 | unpadded_len = 5780; |
3837 | 0 | total_len = 17*512; |
3838 | 0 | } |
3839 | 0 | else |
3840 | 0 | { |
3841 | 0 | unpadded_len = 2900; |
3842 | 0 | total_len = 9*512; |
3843 | 0 | } |
3844 | | /* "VANC" SEI */ |
3845 | 0 | nal_start( h, NAL_SEI, NAL_PRIORITY_DISPOSABLE ); |
3846 | 0 | if( x264_sei_avcintra_vanc_write( h, &h->out.bs, unpadded_len ) < 0 ) |
3847 | 0 | return -1; |
3848 | 0 | if( nal_end( h ) ) |
3849 | 0 | return -1; |
3850 | | |
3851 | 0 | h->out.nal[h->out.i_nal-1].i_padding = total_len - h->out.nal[h->out.i_nal-1].i_payload - SEI_OVERHEAD; |
3852 | 0 | overhead += h->out.nal[h->out.i_nal-1].i_payload + h->out.nal[h->out.i_nal-1].i_padding + SEI_OVERHEAD; |
3853 | 0 | } |
3854 | | |
3855 | | /* Init the rate control */ |
3856 | | /* FIXME: Include slice header bit cost. */ |
3857 | 0 | x264_ratecontrol_start( h, h->fenc->i_qpplus1, overhead*8 ); |
3858 | 0 | i_global_qp = x264_ratecontrol_qp( h ); |
3859 | |
|
3860 | 0 | pic_out->i_qpplus1 = |
3861 | 0 | h->fdec->i_qpplus1 = i_global_qp + 1; |
3862 | |
|
3863 | 0 | if( h->param.rc.b_stat_read && h->sh.i_type != SLICE_TYPE_I ) |
3864 | 0 | { |
3865 | 0 | x264_reference_build_list_optimal( h ); |
3866 | 0 | reference_check_reorder( h ); |
3867 | 0 | } |
3868 | |
|
3869 | 0 | if( h->i_ref[0] ) |
3870 | 0 | h->fdec->i_poc_l0ref0 = h->fref[0][0]->i_poc; |
3871 | | |
3872 | | /* ------------------------ Create slice header ----------------------- */ |
3873 | 0 | slice_init( h, i_nal_type, i_global_qp ); |
3874 | | |
3875 | | /*------------------------- Weights -------------------------------------*/ |
3876 | 0 | if( h->sh.i_type == SLICE_TYPE_B ) |
3877 | 0 | x264_macroblock_bipred_init( h ); |
3878 | |
|
3879 | 0 | weighted_pred_init( h ); |
3880 | |
|
3881 | 0 | if( i_nal_ref_idc != NAL_PRIORITY_DISPOSABLE ) |
3882 | 0 | h->i_frame_num++; |
3883 | | |
3884 | | /* Write frame */ |
3885 | 0 | h->i_threadslice_start = 0; |
3886 | 0 | h->i_threadslice_end = h->mb.i_mb_height; |
3887 | 0 | if( h->i_thread_frames > 1 ) |
3888 | 0 | { |
3889 | 0 | x264_threadpool_run( h->threadpool, (void*)slices_write, h ); |
3890 | 0 | h->b_thread_active = 1; |
3891 | 0 | } |
3892 | 0 | else if( h->param.b_sliced_threads ) |
3893 | 0 | { |
3894 | 0 | if( threaded_slices_write( h ) ) |
3895 | 0 | return -1; |
3896 | 0 | } |
3897 | 0 | else |
3898 | 0 | if( (intptr_t)slices_write( h ) ) |
3899 | 0 | return -1; |
3900 | | |
3901 | 0 | return encoder_frame_end( thread_oldest, thread_current, pp_nal, pi_nal, pic_out ); |
3902 | 0 | } Unexecuted instantiation: x264_8_encoder_encode Unexecuted instantiation: x264_10_encoder_encode |
3903 | | |
3904 | | static int encoder_frame_end( x264_t *h, x264_t *thread_current, |
3905 | | x264_nal_t **pp_nal, int *pi_nal, |
3906 | | x264_picture_t *pic_out ) |
3907 | 0 | { |
3908 | 0 | char psz_message[80]; |
3909 | |
|
3910 | 0 | if( !h->param.b_sliced_threads && h->b_thread_active ) |
3911 | 0 | { |
3912 | 0 | h->b_thread_active = 0; |
3913 | 0 | if( (intptr_t)x264_threadpool_wait( h->threadpool, h ) ) |
3914 | 0 | return -1; |
3915 | 0 | } |
3916 | 0 | if( !h->out.i_nal ) |
3917 | 0 | { |
3918 | 0 | pic_out->i_type = X264_TYPE_AUTO; |
3919 | 0 | return 0; |
3920 | 0 | } |
3921 | | |
3922 | 0 | x264_emms(); |
3923 | | |
3924 | | /* generate buffering period sei and insert it into place */ |
3925 | 0 | if( h->i_thread_frames > 1 && h->fenc->b_keyframe && h->sps->vui.b_nal_hrd_parameters_present ) |
3926 | 0 | { |
3927 | 0 | x264_hrd_fullness( h ); |
3928 | 0 | nal_start( h, NAL_SEI, NAL_PRIORITY_DISPOSABLE ); |
3929 | 0 | x264_sei_buffering_period_write( h, &h->out.bs ); |
3930 | 0 | if( nal_end( h ) ) |
3931 | 0 | return -1; |
3932 | | /* buffering period sei must follow AUD, SPS and PPS and precede all other SEIs */ |
3933 | 0 | int idx = 0; |
3934 | 0 | while( h->out.nal[idx].i_type == NAL_AUD || |
3935 | 0 | h->out.nal[idx].i_type == NAL_SPS || |
3936 | 0 | h->out.nal[idx].i_type == NAL_PPS ) |
3937 | 0 | idx++; |
3938 | 0 | x264_nal_t nal_tmp = h->out.nal[h->out.i_nal-1]; |
3939 | 0 | memmove( &h->out.nal[idx+1], &h->out.nal[idx], (h->out.i_nal-idx-1)*sizeof(x264_nal_t) ); |
3940 | 0 | h->out.nal[idx] = nal_tmp; |
3941 | 0 | } |
3942 | | |
3943 | 0 | int frame_size = encoder_encapsulate_nals( h, 0 ); |
3944 | 0 | if( frame_size < 0 ) |
3945 | 0 | return -1; |
3946 | | |
3947 | | /* Set output picture properties */ |
3948 | 0 | pic_out->i_type = h->fenc->i_type; |
3949 | |
|
3950 | 0 | pic_out->b_keyframe = h->fenc->b_keyframe; |
3951 | 0 | pic_out->i_pic_struct = h->fenc->i_pic_struct; |
3952 | |
|
3953 | 0 | pic_out->i_pts = h->fdec->i_pts; |
3954 | 0 | pic_out->i_dts = h->fdec->i_dts; |
3955 | |
|
3956 | 0 | if( pic_out->i_pts < pic_out->i_dts ) |
3957 | 0 | x264_log( h, X264_LOG_WARNING, "invalid DTS: PTS is less than DTS\n" ); |
3958 | |
|
3959 | 0 | pic_out->opaque = h->fenc->opaque; |
3960 | |
|
3961 | 0 | pic_out->img.i_csp = h->fdec->i_csp; |
3962 | | #if HIGH_BIT_DEPTH |
3963 | | pic_out->img.i_csp |= X264_CSP_HIGH_DEPTH; |
3964 | | #endif |
3965 | 0 | pic_out->img.i_plane = h->fdec->i_plane; |
3966 | 0 | for( int i = 0; i < pic_out->img.i_plane; i++ ) |
3967 | 0 | { |
3968 | 0 | pic_out->img.i_stride[i] = h->fdec->i_stride[i] * SIZEOF_PIXEL; |
3969 | 0 | pic_out->img.plane[i] = (uint8_t*)h->fdec->plane[i]; |
3970 | 0 | } |
3971 | |
|
3972 | 0 | x264_frame_push_unused( thread_current, h->fenc ); |
3973 | | |
3974 | | /* ---------------------- Update encoder state ------------------------- */ |
3975 | | |
3976 | | /* update rc */ |
3977 | 0 | int filler = 0; |
3978 | 0 | if( x264_ratecontrol_end( h, frame_size * 8, &filler ) < 0 ) |
3979 | 0 | return -1; |
3980 | | |
3981 | 0 | pic_out->hrd_timing = h->fenc->hrd_timing; |
3982 | 0 | pic_out->prop.f_crf_avg = h->fdec->f_crf_avg; |
3983 | | |
3984 | | /* Filler in AVC-Intra mode is written as zero bytes to the last slice |
3985 | | * We don't know the size of the last slice until encapsulation so we add filler to the encapsulated NAL */ |
3986 | 0 | if( h->param.i_avcintra_class ) |
3987 | 0 | { |
3988 | 0 | if( check_encapsulated_buffer( h, h->thread[0], h->out.i_nal, frame_size, (int64_t)frame_size + filler ) < 0 ) |
3989 | 0 | return -1; |
3990 | | |
3991 | 0 | x264_nal_t *nal = &h->out.nal[h->out.i_nal-1]; |
3992 | 0 | memset( nal->p_payload + nal->i_payload, 0, filler ); |
3993 | 0 | nal->i_payload += filler; |
3994 | 0 | nal->i_padding = filler; |
3995 | 0 | frame_size += filler; |
3996 | | |
3997 | | /* Fix up the size header for mp4/etc */ |
3998 | 0 | if( !h->param.b_annexb ) |
3999 | 0 | { |
4000 | | /* Size doesn't include the size of the header we're writing now. */ |
4001 | 0 | uint8_t *nal_data = nal->p_payload; |
4002 | 0 | int chunk_size = nal->i_payload - 4; |
4003 | 0 | nal_data[0] = chunk_size >> 24; |
4004 | 0 | nal_data[1] = chunk_size >> 16; |
4005 | 0 | nal_data[2] = chunk_size >> 8; |
4006 | 0 | nal_data[3] = chunk_size >> 0; |
4007 | 0 | } |
4008 | 0 | } |
4009 | 0 | else |
4010 | 0 | { |
4011 | 0 | while( filler > 0 ) |
4012 | 0 | { |
4013 | 0 | int f, overhead = FILLER_OVERHEAD - h->param.b_annexb; |
4014 | 0 | if( h->param.i_slice_max_size && filler > h->param.i_slice_max_size ) |
4015 | 0 | { |
4016 | 0 | int next_size = filler - h->param.i_slice_max_size; |
4017 | 0 | int overflow = X264_MAX( overhead - next_size, 0 ); |
4018 | 0 | f = h->param.i_slice_max_size - overhead - overflow; |
4019 | 0 | } |
4020 | 0 | else |
4021 | 0 | f = X264_MAX( 0, filler - overhead ); |
4022 | |
|
4023 | 0 | if( bitstream_check_buffer_filler( h, f ) ) |
4024 | 0 | return -1; |
4025 | 0 | nal_start( h, NAL_FILLER, NAL_PRIORITY_DISPOSABLE ); |
4026 | 0 | x264_filler_write( h, &h->out.bs, f ); |
4027 | 0 | if( nal_end( h ) ) |
4028 | 0 | return -1; |
4029 | 0 | int total_size = encoder_encapsulate_nals( h, h->out.i_nal-1 ); |
4030 | 0 | if( total_size < 0 ) |
4031 | 0 | return -1; |
4032 | 0 | frame_size += total_size; |
4033 | 0 | filler -= total_size; |
4034 | 0 | } |
4035 | 0 | } |
4036 | | |
4037 | | /* End bitstream, set output */ |
4038 | 0 | *pi_nal = h->out.i_nal; |
4039 | 0 | *pp_nal = h->out.nal; |
4040 | |
|
4041 | 0 | h->out.i_nal = 0; |
4042 | |
|
4043 | 0 | x264_noise_reduction_update( h ); |
4044 | | |
4045 | | /* ---------------------- Compute/Print statistics --------------------- */ |
4046 | 0 | thread_sync_stat( h, h->thread[0] ); |
4047 | | |
4048 | | /* Slice stat */ |
4049 | 0 | h->stat.i_frame_count[h->sh.i_type]++; |
4050 | 0 | h->stat.i_frame_size[h->sh.i_type] += frame_size; |
4051 | 0 | h->stat.f_frame_qp[h->sh.i_type] += h->fdec->f_qp_avg_aq; |
4052 | |
|
4053 | 0 | for( int i = 0; i < X264_MBTYPE_MAX; i++ ) |
4054 | 0 | h->stat.i_mb_count[h->sh.i_type][i] += h->stat.frame.i_mb_count[i]; |
4055 | 0 | for( int i = 0; i < 2; i++ ) |
4056 | 0 | h->stat.i_mb_count_8x8dct[i] += h->stat.frame.i_mb_count_8x8dct[i]; |
4057 | 0 | for( int i = 0; i < 6; i++ ) |
4058 | 0 | h->stat.i_mb_cbp[i] += h->stat.frame.i_mb_cbp[i]; |
4059 | 0 | for( int i = 0; i < 4; i++ ) |
4060 | 0 | for( int j = 0; j < 13; j++ ) |
4061 | 0 | h->stat.i_mb_pred_mode[i][j] += h->stat.frame.i_mb_pred_mode[i][j]; |
4062 | 0 | if( h->sh.i_type != SLICE_TYPE_I ) |
4063 | 0 | { |
4064 | 0 | for( int i = 0; i < X264_PARTTYPE_MAX; i++ ) |
4065 | 0 | h->stat.i_mb_partition[h->sh.i_type][i] += h->stat.frame.i_mb_partition[i]; |
4066 | 0 | for( int i_list = 0; i_list < 2; i_list++ ) |
4067 | 0 | for( int i = 0; i < X264_REF_MAX*2; i++ ) |
4068 | 0 | h->stat.i_mb_count_ref[h->sh.i_type][i_list][i] += h->stat.frame.i_mb_count_ref[i_list][i]; |
4069 | 0 | } |
4070 | 0 | for( int i = 0; i < 3; i++ ) |
4071 | 0 | h->stat.i_mb_field[i] += h->stat.frame.i_mb_field[i]; |
4072 | 0 | if( h->sh.i_type == SLICE_TYPE_P && h->param.analyse.i_weighted_pred >= X264_WEIGHTP_SIMPLE ) |
4073 | 0 | { |
4074 | 0 | h->stat.i_wpred[0] += !!h->sh.weight[0][0].weightfn; |
4075 | 0 | h->stat.i_wpred[1] += !!h->sh.weight[0][1].weightfn || !!h->sh.weight[0][2].weightfn; |
4076 | 0 | } |
4077 | 0 | if( h->sh.i_type == SLICE_TYPE_B ) |
4078 | 0 | { |
4079 | 0 | h->stat.i_direct_frames[ h->sh.b_direct_spatial_mv_pred ] ++; |
4080 | 0 | if( h->mb.b_direct_auto_write ) |
4081 | 0 | { |
4082 | | //FIXME somewhat arbitrary time constants |
4083 | 0 | if( h->stat.i_direct_score[0] + h->stat.i_direct_score[1] > h->mb.i_mb_count ) |
4084 | 0 | for( int i = 0; i < 2; i++ ) |
4085 | 0 | h->stat.i_direct_score[i] = h->stat.i_direct_score[i] * 9/10; |
4086 | 0 | for( int i = 0; i < 2; i++ ) |
4087 | 0 | h->stat.i_direct_score[i] += h->stat.frame.i_direct_score[i]; |
4088 | 0 | } |
4089 | 0 | } |
4090 | 0 | else |
4091 | 0 | h->stat.i_consecutive_bframes[h->fenc->i_bframes]++; |
4092 | |
|
4093 | 0 | psz_message[0] = '\0'; |
4094 | 0 | double dur = h->fenc->f_duration; |
4095 | 0 | h->stat.f_frame_duration[h->sh.i_type] += dur; |
4096 | 0 | if( h->param.analyse.b_psnr ) |
4097 | 0 | { |
4098 | 0 | int64_t ssd[3] = |
4099 | 0 | { |
4100 | 0 | h->stat.frame.i_ssd[0], |
4101 | 0 | h->stat.frame.i_ssd[1], |
4102 | 0 | h->stat.frame.i_ssd[2], |
4103 | 0 | }; |
4104 | 0 | int luma_size = h->param.i_width * h->param.i_height; |
4105 | 0 | int chroma_size = CHROMA_SIZE( luma_size ); |
4106 | 0 | pic_out->prop.f_psnr[0] = calc_psnr( ssd[0], luma_size ); |
4107 | 0 | pic_out->prop.f_psnr[1] = calc_psnr( ssd[1], chroma_size ); |
4108 | 0 | pic_out->prop.f_psnr[2] = calc_psnr( ssd[2], chroma_size ); |
4109 | 0 | pic_out->prop.f_psnr_avg = calc_psnr( ssd[0] + ssd[1] + ssd[2], luma_size + chroma_size*2 ); |
4110 | |
|
4111 | 0 | h->stat.f_ssd_global[h->sh.i_type] += dur * (ssd[0] + ssd[1] + ssd[2]); |
4112 | 0 | h->stat.f_psnr_average[h->sh.i_type] += dur * pic_out->prop.f_psnr_avg; |
4113 | 0 | h->stat.f_psnr_mean_y[h->sh.i_type] += dur * pic_out->prop.f_psnr[0]; |
4114 | 0 | h->stat.f_psnr_mean_u[h->sh.i_type] += dur * pic_out->prop.f_psnr[1]; |
4115 | 0 | h->stat.f_psnr_mean_v[h->sh.i_type] += dur * pic_out->prop.f_psnr[2]; |
4116 | |
|
4117 | 0 | snprintf( psz_message, 80, " PSNR Y:%5.2f U:%5.2f V:%5.2f", pic_out->prop.f_psnr[0], |
4118 | 0 | pic_out->prop.f_psnr[1], |
4119 | 0 | pic_out->prop.f_psnr[2] ); |
4120 | 0 | } |
4121 | |
|
4122 | 0 | if( h->param.analyse.b_ssim ) |
4123 | 0 | { |
4124 | 0 | pic_out->prop.f_ssim = h->stat.frame.f_ssim / h->stat.frame.i_ssim_cnt; |
4125 | 0 | h->stat.f_ssim_mean_y[h->sh.i_type] += pic_out->prop.f_ssim * dur; |
4126 | 0 | int msg_len = strlen(psz_message); |
4127 | 0 | snprintf( psz_message + msg_len, 80 - msg_len, " SSIM Y:%.5f", pic_out->prop.f_ssim ); |
4128 | 0 | } |
4129 | 0 | psz_message[79] = '\0'; |
4130 | |
|
4131 | 0 | x264_log( h, X264_LOG_DEBUG, |
4132 | 0 | "frame=%4d QP=%.2f NAL=%d Slice:%c Poc:%-3d I:%-4d P:%-4d SKIP:%-4d size=%d bytes%s\n", |
4133 | 0 | h->i_frame, |
4134 | 0 | h->fdec->f_qp_avg_aq, |
4135 | 0 | h->i_nal_ref_idc, |
4136 | 0 | h->sh.i_type == SLICE_TYPE_I ? 'I' : (h->sh.i_type == SLICE_TYPE_P ? 'P' : 'B' ), |
4137 | 0 | h->fdec->i_poc, |
4138 | 0 | h->stat.frame.i_mb_count_i, |
4139 | 0 | h->stat.frame.i_mb_count_p, |
4140 | 0 | h->stat.frame.i_mb_count_skip, |
4141 | 0 | frame_size, |
4142 | 0 | psz_message ); |
4143 | | |
4144 | | // keep stats all in one place |
4145 | 0 | thread_sync_stat( h->thread[0], h ); |
4146 | | // for the use of the next frame |
4147 | 0 | thread_sync_stat( thread_current, h ); |
4148 | |
|
4149 | | #ifdef DEBUG_MB_TYPE |
4150 | | { |
4151 | | static const char mb_chars[] = { 'i', 'i', 'I', 'C', 'P', '8', 'S', |
4152 | | 'D', '<', 'X', 'B', 'X', '>', 'B', 'B', 'B', 'B', '8', 'S' }; |
4153 | | for( int mb_xy = 0; mb_xy < h->mb.i_mb_width * h->mb.i_mb_height; mb_xy++ ) |
4154 | | { |
4155 | | if( h->mb.type[mb_xy] < X264_MBTYPE_MAX && h->mb.type[mb_xy] >= 0 ) |
4156 | | fprintf( stderr, "%c ", mb_chars[ h->mb.type[mb_xy] ] ); |
4157 | | else |
4158 | | fprintf( stderr, "? " ); |
4159 | | |
4160 | | if( (mb_xy+1) % h->mb.i_mb_width == 0 ) |
4161 | | fprintf( stderr, "\n" ); |
4162 | | } |
4163 | | } |
4164 | | #endif |
4165 | | |
4166 | | /* Remove duplicates, must be done near the end as breaks h->fref0 array |
4167 | | * by freeing some of its pointers. */ |
4168 | 0 | for( int i = 0; i < h->i_ref[0]; i++ ) |
4169 | 0 | if( h->fref[0][i] && h->fref[0][i]->b_duplicate ) |
4170 | 0 | { |
4171 | 0 | x264_frame_push_blank_unused( h, h->fref[0][i] ); |
4172 | 0 | h->fref[0][i] = 0; |
4173 | 0 | } |
4174 | |
|
4175 | 0 | if( h->param.psz_dump_yuv ) |
4176 | 0 | frame_dump( h ); |
4177 | 0 | x264_emms(); |
4178 | |
|
4179 | 0 | return frame_size; |
4180 | 0 | } |
4181 | | |
4182 | | static void print_intra( int64_t *i_mb_count, double i_count, int b_print_pcm, char *intra ) |
4183 | 0 | { |
4184 | 0 | intra += sprintf( intra, "I16..4%s: %4.1f%% %4.1f%% %4.1f%%", |
4185 | 0 | b_print_pcm ? "..PCM" : "", |
4186 | 0 | i_mb_count[I_16x16]/ i_count, |
4187 | 0 | i_mb_count[I_8x8] / i_count, |
4188 | 0 | i_mb_count[I_4x4] / i_count ); |
4189 | 0 | if( b_print_pcm ) |
4190 | 0 | sprintf( intra, " %4.1f%%", i_mb_count[I_PCM] / i_count ); |
4191 | 0 | } |
4192 | | |
4193 | | /**************************************************************************** |
4194 | | * x264_encoder_close: |
4195 | | ****************************************************************************/ |
4196 | | void x264_encoder_close ( x264_t *h ) |
4197 | 0 | { |
4198 | 0 | int64_t i_yuv_size = FRAME_SIZE( h->param.i_width * h->param.i_height ); |
4199 | 0 | int64_t i_mb_count_size[2][7] = {{0}}; |
4200 | 0 | char buf[200]; |
4201 | 0 | int b_print_pcm = h->stat.i_mb_count[SLICE_TYPE_I][I_PCM] |
4202 | 0 | || h->stat.i_mb_count[SLICE_TYPE_P][I_PCM] |
4203 | 0 | || h->stat.i_mb_count[SLICE_TYPE_B][I_PCM]; |
4204 | |
|
4205 | 0 | x264_lookahead_delete( h ); |
4206 | |
|
4207 | | #if HAVE_OPENCL |
4208 | | x264_opencl_lookahead_delete( h ); |
4209 | | x264_opencl_function_t *ocl = h->opencl.ocl; |
4210 | | #endif |
4211 | |
|
4212 | 0 | if( h->param.b_sliced_threads ) |
4213 | 0 | threadpool_wait_all( h ); |
4214 | 0 | if( h->param.i_threads > 1 ) |
4215 | 0 | x264_threadpool_delete( h->threadpool ); |
4216 | 0 | if( h->param.i_lookahead_threads > 1 ) |
4217 | 0 | x264_threadpool_delete( h->lookaheadpool ); |
4218 | 0 | if( h->i_thread_frames > 1 ) |
4219 | 0 | { |
4220 | 0 | for( int i = 0; i < h->i_thread_frames; i++ ) |
4221 | 0 | if( h->thread[i]->b_thread_active ) |
4222 | 0 | { |
4223 | 0 | assert( h->thread[i]->fenc->i_reference_count == 1 ); |
4224 | 0 | x264_frame_delete( h->thread[i]->fenc ); |
4225 | 0 | } |
4226 | |
|
4227 | 0 | x264_t *thread_prev = h->thread[h->i_thread_phase]; |
4228 | 0 | x264_thread_sync_ratecontrol( h, thread_prev, h ); |
4229 | 0 | x264_thread_sync_ratecontrol( thread_prev, thread_prev, h ); |
4230 | 0 | h->i_frame = thread_prev->i_frame + 1 - h->i_thread_frames; |
4231 | 0 | } |
4232 | 0 | h->i_frame++; |
4233 | | |
4234 | | /* Slices used and PSNR */ |
4235 | 0 | for( int i = 0; i < 3; i++ ) |
4236 | 0 | { |
4237 | 0 | static const uint8_t slice_order[] = { SLICE_TYPE_I, SLICE_TYPE_P, SLICE_TYPE_B }; |
4238 | 0 | int i_slice = slice_order[i]; |
4239 | |
|
4240 | 0 | if( h->stat.i_frame_count[i_slice] > 0 ) |
4241 | 0 | { |
4242 | 0 | int i_count = h->stat.i_frame_count[i_slice]; |
4243 | 0 | double dur = h->stat.f_frame_duration[i_slice]; |
4244 | 0 | if( h->param.analyse.b_psnr ) |
4245 | 0 | { |
4246 | 0 | x264_log( h, X264_LOG_INFO, |
4247 | 0 | "frame %c:%-5d Avg QP:%5.2f size:%6.0f PSNR Mean Y:%5.2f U:%5.2f V:%5.2f Avg:%5.2f Global:%5.2f\n", |
4248 | 0 | slice_type_to_char[i_slice], |
4249 | 0 | i_count, |
4250 | 0 | h->stat.f_frame_qp[i_slice] / i_count, |
4251 | 0 | (double)h->stat.i_frame_size[i_slice] / i_count, |
4252 | 0 | h->stat.f_psnr_mean_y[i_slice] / dur, h->stat.f_psnr_mean_u[i_slice] / dur, h->stat.f_psnr_mean_v[i_slice] / dur, |
4253 | 0 | h->stat.f_psnr_average[i_slice] / dur, |
4254 | 0 | calc_psnr( h->stat.f_ssd_global[i_slice], dur * i_yuv_size ) ); |
4255 | 0 | } |
4256 | 0 | else |
4257 | 0 | { |
4258 | 0 | x264_log( h, X264_LOG_INFO, |
4259 | 0 | "frame %c:%-5d Avg QP:%5.2f size:%6.0f\n", |
4260 | 0 | slice_type_to_char[i_slice], |
4261 | 0 | i_count, |
4262 | 0 | h->stat.f_frame_qp[i_slice] / i_count, |
4263 | 0 | (double)h->stat.i_frame_size[i_slice] / i_count ); |
4264 | 0 | } |
4265 | 0 | } |
4266 | 0 | } |
4267 | 0 | if( h->param.i_bframe && h->stat.i_frame_count[SLICE_TYPE_B] ) |
4268 | 0 | { |
4269 | 0 | char *p = buf; |
4270 | 0 | int den = 0; |
4271 | | // weight by number of frames (including the I/P-frames) that are in a sequence of N B-frames |
4272 | 0 | for( int i = 0; i <= h->param.i_bframe; i++ ) |
4273 | 0 | den += (i+1) * h->stat.i_consecutive_bframes[i]; |
4274 | 0 | for( int i = 0; i <= h->param.i_bframe; i++ ) |
4275 | 0 | p += sprintf( p, " %4.1f%%", 100. * (i+1) * h->stat.i_consecutive_bframes[i] / den ); |
4276 | 0 | x264_log( h, X264_LOG_INFO, "consecutive B-frames:%s\n", buf ); |
4277 | 0 | } |
4278 | |
|
4279 | 0 | for( int i_type = 0; i_type < 2; i_type++ ) |
4280 | 0 | for( int i = 0; i < X264_PARTTYPE_MAX; i++ ) |
4281 | 0 | { |
4282 | 0 | if( i == D_DIRECT_8x8 ) continue; /* direct is counted as its own type */ |
4283 | 0 | i_mb_count_size[i_type][x264_mb_partition_pixel_table[i]] += h->stat.i_mb_partition[i_type][i]; |
4284 | 0 | } |
4285 | | |
4286 | | /* MB types used */ |
4287 | 0 | if( h->stat.i_frame_count[SLICE_TYPE_I] > 0 ) |
4288 | 0 | { |
4289 | 0 | int64_t *i_mb_count = h->stat.i_mb_count[SLICE_TYPE_I]; |
4290 | 0 | double i_count = (double)h->stat.i_frame_count[SLICE_TYPE_I] * h->mb.i_mb_count / 100.0; |
4291 | 0 | print_intra( i_mb_count, i_count, b_print_pcm, buf ); |
4292 | 0 | x264_log( h, X264_LOG_INFO, "mb I %s\n", buf ); |
4293 | 0 | } |
4294 | 0 | if( h->stat.i_frame_count[SLICE_TYPE_P] > 0 ) |
4295 | 0 | { |
4296 | 0 | int64_t *i_mb_count = h->stat.i_mb_count[SLICE_TYPE_P]; |
4297 | 0 | double i_count = (double)h->stat.i_frame_count[SLICE_TYPE_P] * h->mb.i_mb_count / 100.0; |
4298 | 0 | int64_t *i_mb_size = i_mb_count_size[SLICE_TYPE_P]; |
4299 | 0 | print_intra( i_mb_count, i_count, b_print_pcm, buf ); |
4300 | 0 | x264_log( h, X264_LOG_INFO, |
4301 | 0 | "mb P %s P16..4: %4.1f%% %4.1f%% %4.1f%% %4.1f%% %4.1f%% skip:%4.1f%%\n", |
4302 | 0 | buf, |
4303 | 0 | i_mb_size[PIXEL_16x16] / (i_count*4), |
4304 | 0 | (i_mb_size[PIXEL_16x8] + i_mb_size[PIXEL_8x16]) / (i_count*4), |
4305 | 0 | i_mb_size[PIXEL_8x8] / (i_count*4), |
4306 | 0 | (i_mb_size[PIXEL_8x4] + i_mb_size[PIXEL_4x8]) / (i_count*4), |
4307 | 0 | i_mb_size[PIXEL_4x4] / (i_count*4), |
4308 | 0 | i_mb_count[P_SKIP] / i_count ); |
4309 | 0 | } |
4310 | 0 | if( h->stat.i_frame_count[SLICE_TYPE_B] > 0 ) |
4311 | 0 | { |
4312 | 0 | int64_t *i_mb_count = h->stat.i_mb_count[SLICE_TYPE_B]; |
4313 | 0 | double i_count = (double)h->stat.i_frame_count[SLICE_TYPE_B] * h->mb.i_mb_count / 100.0; |
4314 | 0 | double i_mb_list_count; |
4315 | 0 | int64_t *i_mb_size = i_mb_count_size[SLICE_TYPE_B]; |
4316 | 0 | int64_t list_count[3] = {0}; /* 0 == L0, 1 == L1, 2 == BI */ |
4317 | 0 | print_intra( i_mb_count, i_count, b_print_pcm, buf ); |
4318 | 0 | for( int i = 0; i < X264_PARTTYPE_MAX; i++ ) |
4319 | 0 | for( int j = 0; j < 2; j++ ) |
4320 | 0 | { |
4321 | 0 | int l0 = x264_mb_type_list_table[i][0][j]; |
4322 | 0 | int l1 = x264_mb_type_list_table[i][1][j]; |
4323 | 0 | if( l0 || l1 ) |
4324 | 0 | list_count[l1+l0*l1] += h->stat.i_mb_count[SLICE_TYPE_B][i] * 2; |
4325 | 0 | } |
4326 | 0 | list_count[0] += h->stat.i_mb_partition[SLICE_TYPE_B][D_L0_8x8]; |
4327 | 0 | list_count[1] += h->stat.i_mb_partition[SLICE_TYPE_B][D_L1_8x8]; |
4328 | 0 | list_count[2] += h->stat.i_mb_partition[SLICE_TYPE_B][D_BI_8x8]; |
4329 | 0 | i_mb_count[B_DIRECT] += (h->stat.i_mb_partition[SLICE_TYPE_B][D_DIRECT_8x8]+2)/4; |
4330 | 0 | i_mb_list_count = (list_count[0] + list_count[1] + list_count[2]) / 100.0; |
4331 | 0 | sprintf( buf + strlen(buf), " B16..8: %4.1f%% %4.1f%% %4.1f%% direct:%4.1f%% skip:%4.1f%%", |
4332 | 0 | i_mb_size[PIXEL_16x16] / (i_count*4), |
4333 | 0 | (i_mb_size[PIXEL_16x8] + i_mb_size[PIXEL_8x16]) / (i_count*4), |
4334 | 0 | i_mb_size[PIXEL_8x8] / (i_count*4), |
4335 | 0 | i_mb_count[B_DIRECT] / i_count, |
4336 | 0 | i_mb_count[B_SKIP] / i_count ); |
4337 | 0 | if( i_mb_list_count != 0 ) |
4338 | 0 | sprintf( buf + strlen(buf), " L0:%4.1f%% L1:%4.1f%% BI:%4.1f%%", |
4339 | 0 | list_count[0] / i_mb_list_count, |
4340 | 0 | list_count[1] / i_mb_list_count, |
4341 | 0 | list_count[2] / i_mb_list_count ); |
4342 | 0 | x264_log( h, X264_LOG_INFO, "mb B %s\n", buf ); |
4343 | 0 | } |
4344 | |
|
4345 | 0 | x264_ratecontrol_summary( h ); |
4346 | |
|
4347 | 0 | if( h->stat.i_frame_count[SLICE_TYPE_I] + h->stat.i_frame_count[SLICE_TYPE_P] + h->stat.i_frame_count[SLICE_TYPE_B] > 0 ) |
4348 | 0 | { |
4349 | 0 | #define SUM3(p) (p[SLICE_TYPE_I] + p[SLICE_TYPE_P] + p[SLICE_TYPE_B]) |
4350 | 0 | #define SUM3b(p,o) (p[SLICE_TYPE_I][o] + p[SLICE_TYPE_P][o] + p[SLICE_TYPE_B][o]) |
4351 | 0 | int64_t i_i8x8 = SUM3b( h->stat.i_mb_count, I_8x8 ); |
4352 | 0 | int64_t i_intra = i_i8x8 + SUM3b( h->stat.i_mb_count, I_4x4 ) |
4353 | 0 | + SUM3b( h->stat.i_mb_count, I_16x16 ); |
4354 | 0 | int64_t i_all_intra = i_intra + SUM3b( h->stat.i_mb_count, I_PCM ); |
4355 | 0 | int64_t i_skip = SUM3b( h->stat.i_mb_count, P_SKIP ) |
4356 | 0 | + SUM3b( h->stat.i_mb_count, B_SKIP ); |
4357 | 0 | const int i_count = h->stat.i_frame_count[SLICE_TYPE_I] + |
4358 | 0 | h->stat.i_frame_count[SLICE_TYPE_P] + |
4359 | 0 | h->stat.i_frame_count[SLICE_TYPE_B]; |
4360 | 0 | int64_t i_mb_count = (int64_t)i_count * h->mb.i_mb_count; |
4361 | 0 | int64_t i_inter = i_mb_count - i_skip - i_all_intra; |
4362 | 0 | const double duration = h->stat.f_frame_duration[SLICE_TYPE_I] + |
4363 | 0 | h->stat.f_frame_duration[SLICE_TYPE_P] + |
4364 | 0 | h->stat.f_frame_duration[SLICE_TYPE_B]; |
4365 | 0 | float f_bitrate = SUM3(h->stat.i_frame_size) / duration / 125; |
4366 | |
|
4367 | 0 | if( PARAM_INTERLACED ) |
4368 | 0 | { |
4369 | 0 | char *fieldstats = buf; |
4370 | 0 | fieldstats[0] = 0; |
4371 | 0 | if( i_inter ) |
4372 | 0 | fieldstats += sprintf( fieldstats, " inter:%.1f%%", h->stat.i_mb_field[1] * 100.0 / i_inter ); |
4373 | 0 | if( i_skip ) |
4374 | 0 | fieldstats += sprintf( fieldstats, " skip:%.1f%%", h->stat.i_mb_field[2] * 100.0 / i_skip ); |
4375 | 0 | x264_log( h, X264_LOG_INFO, "field mbs: intra: %.1f%%%s\n", |
4376 | 0 | h->stat.i_mb_field[0] * 100.0 / i_all_intra, buf ); |
4377 | 0 | } |
4378 | |
|
4379 | 0 | if( h->pps->b_transform_8x8_mode ) |
4380 | 0 | { |
4381 | 0 | buf[0] = 0; |
4382 | 0 | if( h->stat.i_mb_count_8x8dct[0] ) |
4383 | 0 | sprintf( buf, " inter:%.1f%%", 100. * h->stat.i_mb_count_8x8dct[1] / h->stat.i_mb_count_8x8dct[0] ); |
4384 | 0 | x264_log( h, X264_LOG_INFO, "8x8 transform intra:%.1f%%%s\n", 100. * i_i8x8 / X264_MAX( i_intra, 1 ), buf ); |
4385 | 0 | } |
4386 | |
|
4387 | 0 | if( (h->param.analyse.i_direct_mv_pred == X264_DIRECT_PRED_AUTO || |
4388 | 0 | (h->stat.i_direct_frames[0] && h->stat.i_direct_frames[1])) |
4389 | 0 | && h->stat.i_frame_count[SLICE_TYPE_B] ) |
4390 | 0 | { |
4391 | 0 | x264_log( h, X264_LOG_INFO, "direct mvs spatial:%.1f%% temporal:%.1f%%\n", |
4392 | 0 | h->stat.i_direct_frames[1] * 100. / h->stat.i_frame_count[SLICE_TYPE_B], |
4393 | 0 | h->stat.i_direct_frames[0] * 100. / h->stat.i_frame_count[SLICE_TYPE_B] ); |
4394 | 0 | } |
4395 | |
|
4396 | 0 | buf[0] = 0; |
4397 | 0 | if( CHROMA_FORMAT ) |
4398 | 0 | { |
4399 | 0 | int csize = CHROMA444 ? 4 : 1; |
4400 | 0 | if( i_mb_count != i_all_intra ) |
4401 | 0 | sprintf( buf, " inter: %.1f%% %.1f%% %.1f%%", |
4402 | 0 | h->stat.i_mb_cbp[1] * 100.0 / ((i_mb_count - i_all_intra)*4), |
4403 | 0 | h->stat.i_mb_cbp[3] * 100.0 / ((i_mb_count - i_all_intra)*csize), |
4404 | 0 | h->stat.i_mb_cbp[5] * 100.0 / ((i_mb_count - i_all_intra)*csize) ); |
4405 | 0 | x264_log( h, X264_LOG_INFO, "coded y,%s,%s intra: %.1f%% %.1f%% %.1f%%%s\n", |
4406 | 0 | CHROMA444?"u":"uvDC", CHROMA444?"v":"uvAC", |
4407 | 0 | h->stat.i_mb_cbp[0] * 100.0 / (i_all_intra*4), |
4408 | 0 | h->stat.i_mb_cbp[2] * 100.0 / (i_all_intra*csize), |
4409 | 0 | h->stat.i_mb_cbp[4] * 100.0 / (i_all_intra*csize), buf ); |
4410 | 0 | } |
4411 | 0 | else |
4412 | 0 | { |
4413 | 0 | if( i_mb_count != i_all_intra ) |
4414 | 0 | sprintf( buf, " inter: %.1f%%", h->stat.i_mb_cbp[1] * 100.0 / ((i_mb_count - i_all_intra)*4) ); |
4415 | 0 | x264_log( h, X264_LOG_INFO, "coded y intra: %.1f%%%s\n", |
4416 | 0 | h->stat.i_mb_cbp[0] * 100.0 / (i_all_intra*4), buf ); |
4417 | 0 | } |
4418 | |
|
4419 | 0 | int64_t fixed_pred_modes[4][9] = {{0}}; |
4420 | 0 | int64_t sum_pred_modes[4] = {0}; |
4421 | 0 | for( int i = 0; i <= I_PRED_16x16_DC_128; i++ ) |
4422 | 0 | { |
4423 | 0 | fixed_pred_modes[0][x264_mb_pred_mode16x16_fix[i]] += h->stat.i_mb_pred_mode[0][i]; |
4424 | 0 | sum_pred_modes[0] += h->stat.i_mb_pred_mode[0][i]; |
4425 | 0 | } |
4426 | 0 | if( sum_pred_modes[0] ) |
4427 | 0 | x264_log( h, X264_LOG_INFO, "i16 v,h,dc,p: %2.0f%% %2.0f%% %2.0f%% %2.0f%%\n", |
4428 | 0 | fixed_pred_modes[0][0] * 100.0 / sum_pred_modes[0], |
4429 | 0 | fixed_pred_modes[0][1] * 100.0 / sum_pred_modes[0], |
4430 | 0 | fixed_pred_modes[0][2] * 100.0 / sum_pred_modes[0], |
4431 | 0 | fixed_pred_modes[0][3] * 100.0 / sum_pred_modes[0] ); |
4432 | 0 | for( int i = 1; i <= 2; i++ ) |
4433 | 0 | { |
4434 | 0 | for( int j = 0; j <= I_PRED_8x8_DC_128; j++ ) |
4435 | 0 | { |
4436 | 0 | fixed_pred_modes[i][x264_mb_pred_mode4x4_fix(j)] += h->stat.i_mb_pred_mode[i][j]; |
4437 | 0 | sum_pred_modes[i] += h->stat.i_mb_pred_mode[i][j]; |
4438 | 0 | } |
4439 | 0 | if( sum_pred_modes[i] ) |
4440 | 0 | x264_log( h, X264_LOG_INFO, "i%d v,h,dc,ddl,ddr,vr,hd,vl,hu: %2.0f%% %2.0f%% %2.0f%% %2.0f%% %2.0f%% %2.0f%% %2.0f%% %2.0f%% %2.0f%%\n", (3-i)*4, |
4441 | 0 | fixed_pred_modes[i][0] * 100.0 / sum_pred_modes[i], |
4442 | 0 | fixed_pred_modes[i][1] * 100.0 / sum_pred_modes[i], |
4443 | 0 | fixed_pred_modes[i][2] * 100.0 / sum_pred_modes[i], |
4444 | 0 | fixed_pred_modes[i][3] * 100.0 / sum_pred_modes[i], |
4445 | 0 | fixed_pred_modes[i][4] * 100.0 / sum_pred_modes[i], |
4446 | 0 | fixed_pred_modes[i][5] * 100.0 / sum_pred_modes[i], |
4447 | 0 | fixed_pred_modes[i][6] * 100.0 / sum_pred_modes[i], |
4448 | 0 | fixed_pred_modes[i][7] * 100.0 / sum_pred_modes[i], |
4449 | 0 | fixed_pred_modes[i][8] * 100.0 / sum_pred_modes[i] ); |
4450 | 0 | } |
4451 | 0 | for( int i = 0; i <= I_PRED_CHROMA_DC_128; i++ ) |
4452 | 0 | { |
4453 | 0 | fixed_pred_modes[3][x264_mb_chroma_pred_mode_fix[i]] += h->stat.i_mb_pred_mode[3][i]; |
4454 | 0 | sum_pred_modes[3] += h->stat.i_mb_pred_mode[3][i]; |
4455 | 0 | } |
4456 | 0 | if( sum_pred_modes[3] && !CHROMA444 ) |
4457 | 0 | x264_log( h, X264_LOG_INFO, "i8c dc,h,v,p: %2.0f%% %2.0f%% %2.0f%% %2.0f%%\n", |
4458 | 0 | fixed_pred_modes[3][0] * 100.0 / sum_pred_modes[3], |
4459 | 0 | fixed_pred_modes[3][1] * 100.0 / sum_pred_modes[3], |
4460 | 0 | fixed_pred_modes[3][2] * 100.0 / sum_pred_modes[3], |
4461 | 0 | fixed_pred_modes[3][3] * 100.0 / sum_pred_modes[3] ); |
4462 | |
|
4463 | 0 | if( h->param.analyse.i_weighted_pred >= X264_WEIGHTP_SIMPLE && h->stat.i_frame_count[SLICE_TYPE_P] > 0 ) |
4464 | 0 | { |
4465 | 0 | buf[0] = 0; |
4466 | 0 | if( CHROMA_FORMAT ) |
4467 | 0 | sprintf( buf, " UV:%.1f%%", h->stat.i_wpred[1] * 100.0 / h->stat.i_frame_count[SLICE_TYPE_P] ); |
4468 | 0 | x264_log( h, X264_LOG_INFO, "Weighted P-Frames: Y:%.1f%%%s\n", |
4469 | 0 | h->stat.i_wpred[0] * 100.0 / h->stat.i_frame_count[SLICE_TYPE_P], buf ); |
4470 | 0 | } |
4471 | |
|
4472 | 0 | for( int i_list = 0; i_list < 2; i_list++ ) |
4473 | 0 | for( int i_slice = 0; i_slice < 2; i_slice++ ) |
4474 | 0 | { |
4475 | 0 | char *p = buf; |
4476 | 0 | int64_t i_den = 0; |
4477 | 0 | int i_max = 0; |
4478 | 0 | for( int i = 0; i < X264_REF_MAX*2; i++ ) |
4479 | 0 | if( h->stat.i_mb_count_ref[i_slice][i_list][i] ) |
4480 | 0 | { |
4481 | 0 | i_den += h->stat.i_mb_count_ref[i_slice][i_list][i]; |
4482 | 0 | i_max = i; |
4483 | 0 | } |
4484 | 0 | if( i_max == 0 ) |
4485 | 0 | continue; |
4486 | 0 | for( int i = 0; i <= i_max; i++ ) |
4487 | 0 | p += sprintf( p, " %4.1f%%", 100. * h->stat.i_mb_count_ref[i_slice][i_list][i] / i_den ); |
4488 | 0 | x264_log( h, X264_LOG_INFO, "ref %c L%d:%s\n", "PB"[i_slice], i_list, buf ); |
4489 | 0 | } |
4490 | |
|
4491 | 0 | if( h->param.analyse.b_ssim ) |
4492 | 0 | { |
4493 | 0 | float ssim = SUM3( h->stat.f_ssim_mean_y ) / duration; |
4494 | 0 | x264_log( h, X264_LOG_INFO, "SSIM Mean Y:%.7f (%6.3fdb)\n", ssim, calc_ssim_db( ssim ) ); |
4495 | 0 | } |
4496 | 0 | if( h->param.analyse.b_psnr ) |
4497 | 0 | { |
4498 | 0 | x264_log( h, X264_LOG_INFO, |
4499 | 0 | "PSNR Mean Y:%6.3f U:%6.3f V:%6.3f Avg:%6.3f Global:%6.3f kb/s:%.2f\n", |
4500 | 0 | SUM3( h->stat.f_psnr_mean_y ) / duration, |
4501 | 0 | SUM3( h->stat.f_psnr_mean_u ) / duration, |
4502 | 0 | SUM3( h->stat.f_psnr_mean_v ) / duration, |
4503 | 0 | SUM3( h->stat.f_psnr_average ) / duration, |
4504 | 0 | calc_psnr( SUM3( h->stat.f_ssd_global ), duration * i_yuv_size ), |
4505 | 0 | f_bitrate ); |
4506 | 0 | } |
4507 | 0 | else |
4508 | 0 | x264_log( h, X264_LOG_INFO, "kb/s:%.2f\n", f_bitrate ); |
4509 | 0 | } |
4510 | | |
4511 | | /* rc */ |
4512 | 0 | x264_ratecontrol_delete( h ); |
4513 | | |
4514 | | /* param */ |
4515 | 0 | x264_param_cleanup( &h->param ); |
4516 | |
|
4517 | 0 | x264_cqm_delete( h ); |
4518 | 0 | x264_free( h->nal_buffer ); |
4519 | 0 | x264_free( h->reconfig_h ); |
4520 | 0 | x264_analyse_free_costs( h ); |
4521 | 0 | x264_free( h->cost_table ); |
4522 | |
|
4523 | 0 | if( h->i_thread_frames > 1 ) |
4524 | 0 | h = h->thread[h->i_thread_phase]; |
4525 | | |
4526 | | /* frames */ |
4527 | 0 | x264_frame_delete_list( h->frames.unused[0] ); |
4528 | 0 | x264_frame_delete_list( h->frames.unused[1] ); |
4529 | 0 | x264_frame_delete_list( h->frames.current ); |
4530 | 0 | x264_frame_delete_list( h->frames.blank_unused ); |
4531 | |
|
4532 | 0 | h = h->thread[0]; |
4533 | |
|
4534 | 0 | for( int i = 0; i < h->i_thread_frames; i++ ) |
4535 | 0 | if( h->thread[i]->b_thread_active ) |
4536 | 0 | for( int j = 0; j < h->thread[i]->i_ref[0]; j++ ) |
4537 | 0 | if( h->thread[i]->fref[0][j] && h->thread[i]->fref[0][j]->b_duplicate ) |
4538 | 0 | x264_frame_delete( h->thread[i]->fref[0][j] ); |
4539 | |
|
4540 | 0 | if( h->param.i_lookahead_threads > 1 ) |
4541 | 0 | for( int i = 0; i < h->param.i_lookahead_threads; i++ ) |
4542 | 0 | x264_free( h->lookahead_thread[i] ); |
4543 | |
|
4544 | 0 | for( int i = h->param.i_threads - 1; i >= 0; i-- ) |
4545 | 0 | { |
4546 | 0 | x264_frame_t **frame; |
4547 | |
|
4548 | 0 | if( !h->param.b_sliced_threads || i == 0 ) |
4549 | 0 | { |
4550 | 0 | for( frame = h->thread[i]->frames.reference; *frame; frame++ ) |
4551 | 0 | { |
4552 | 0 | assert( (*frame)->i_reference_count > 0 ); |
4553 | 0 | (*frame)->i_reference_count--; |
4554 | 0 | if( (*frame)->i_reference_count == 0 ) |
4555 | 0 | x264_frame_delete( *frame ); |
4556 | 0 | } |
4557 | 0 | frame = &h->thread[i]->fdec; |
4558 | 0 | if( *frame ) |
4559 | 0 | { |
4560 | 0 | assert( (*frame)->i_reference_count > 0 ); |
4561 | 0 | (*frame)->i_reference_count--; |
4562 | 0 | if( (*frame)->i_reference_count == 0 ) |
4563 | 0 | x264_frame_delete( *frame ); |
4564 | 0 | } |
4565 | 0 | x264_macroblock_cache_free( h->thread[i] ); |
4566 | 0 | } |
4567 | 0 | x264_macroblock_thread_free( h->thread[i], 0 ); |
4568 | 0 | x264_free( h->thread[i]->out.p_bitstream ); |
4569 | 0 | x264_free( h->thread[i]->out.nal ); |
4570 | 0 | x264_pthread_mutex_destroy( &h->thread[i]->mutex ); |
4571 | 0 | x264_pthread_cond_destroy( &h->thread[i]->cv ); |
4572 | 0 | x264_free( h->thread[i] ); |
4573 | 0 | } |
4574 | | #if HAVE_OPENCL |
4575 | | x264_opencl_close_library( ocl ); |
4576 | | #endif |
4577 | 0 | } Unexecuted instantiation: x264_8_encoder_close Unexecuted instantiation: x264_10_encoder_close |
4578 | | |
4579 | | int x264_encoder_delayed_frames( x264_t *h ) |
4580 | 0 | { |
4581 | 0 | int delayed_frames = 0; |
4582 | 0 | if( h->i_thread_frames > 1 ) |
4583 | 0 | { |
4584 | 0 | for( int i = 0; i < h->i_thread_frames; i++ ) |
4585 | 0 | delayed_frames += h->thread[i]->b_thread_active; |
4586 | 0 | h = h->thread[h->i_thread_phase]; |
4587 | 0 | } |
4588 | 0 | for( int i = 0; h->frames.current[i]; i++ ) |
4589 | 0 | delayed_frames++; |
4590 | 0 | x264_pthread_mutex_lock( &h->lookahead->ofbuf.mutex ); |
4591 | 0 | x264_pthread_mutex_lock( &h->lookahead->ifbuf.mutex ); |
4592 | 0 | x264_pthread_mutex_lock( &h->lookahead->next.mutex ); |
4593 | 0 | delayed_frames += h->lookahead->ifbuf.i_size + h->lookahead->next.i_size + h->lookahead->ofbuf.i_size; |
4594 | 0 | x264_pthread_mutex_unlock( &h->lookahead->next.mutex ); |
4595 | 0 | x264_pthread_mutex_unlock( &h->lookahead->ifbuf.mutex ); |
4596 | 0 | x264_pthread_mutex_unlock( &h->lookahead->ofbuf.mutex ); |
4597 | 0 | return delayed_frames; |
4598 | 0 | } Unexecuted instantiation: x264_8_encoder_delayed_frames Unexecuted instantiation: x264_10_encoder_delayed_frames |
4599 | | |
4600 | | int x264_encoder_maximum_delayed_frames( x264_t *h ) |
4601 | 0 | { |
4602 | 0 | return h->frames.i_delay; |
4603 | 0 | } Unexecuted instantiation: x264_8_encoder_maximum_delayed_frames Unexecuted instantiation: x264_10_encoder_maximum_delayed_frames |