1 /*****************************************************************************
2  * set: header writing
3  *****************************************************************************
4  * Copyright (C) 2003-2021 x264 project
5  *
6  * Authors: Laurent Aimar <fenrir@via.ecp.fr>
7  *          Loren Merritt <lorenm@u.washington.edu>
8  *
9  * This program is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program; if not, write to the Free Software
21  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02111, USA.
22  *
23  * This program is also available under a commercial proprietary license.
24  * For more information, contact us at licensing@x264.com.
25  *****************************************************************************/
26 
27 #include "common/common.h"
28 #include "set.h"
29 
30 #define bs_write_ue bs_write_ue_big
31 
32 // Indexed by pic_struct values
33 static const uint8_t num_clock_ts[10] = { 0, 1, 1, 1, 2, 2, 3, 3, 2, 3 };
34 static const uint8_t avcintra_uuid[] = {0xF7, 0x49, 0x3E, 0xB3, 0xD4, 0x00, 0x47, 0x96, 0x86, 0x86, 0xC9, 0x70, 0x7B, 0x64, 0x37, 0x2A};
35 
transpose(uint8_t * buf,int w)36 static void transpose( uint8_t *buf, int w )
37 {
38     for( int i = 0; i < w; i++ )
39         for( int j = 0; j < i; j++ )
40             XCHG( uint8_t, buf[w*i+j], buf[w*j+i] );
41 }
42 
scaling_list_write(bs_t * s,x264_sps_t * sps,int idx)43 static void scaling_list_write( bs_t *s, x264_sps_t *sps, int idx )
44 {
45     const int len = idx<4 ? 16 : 64;
46     const uint8_t *zigzag = idx<4 ? x264_zigzag_scan4[0] : x264_zigzag_scan8[0];
47     const uint8_t *list = sps->scaling_list[idx];
48     const uint8_t *def_list = (idx==CQM_4IC) ? sps->scaling_list[CQM_4IY]
49                             : (idx==CQM_4PC) ? sps->scaling_list[CQM_4PY]
50                             : (idx==CQM_8IC+4) ? sps->scaling_list[CQM_8IY+4]
51                             : (idx==CQM_8PC+4) ? sps->scaling_list[CQM_8PY+4]
52                             : x264_cqm_jvt[idx];
53     if( !memcmp( list, def_list, len ) )
54         bs_write1( s, 0 );   // scaling_list_present_flag
55     else if( !memcmp( list, x264_cqm_jvt[idx], len ) )
56     {
57         bs_write1( s, 1 );   // scaling_list_present_flag
58         bs_write_se( s, -8 ); // use jvt list
59     }
60     else
61     {
62         int run;
63         bs_write1( s, 1 );   // scaling_list_present_flag
64 
65         // try run-length compression of trailing values
66         for( run = len; run > 1; run-- )
67             if( list[zigzag[run-1]] != list[zigzag[run-2]] )
68                 break;
69         if( run < len && len - run < bs_size_se( (int8_t)-list[zigzag[run]] ) )
70             run = len;
71 
72         for( int j = 0; j < run; j++ )
73             bs_write_se( s, (int8_t)(list[zigzag[j]] - (j>0 ? list[zigzag[j-1]] : 8)) ); // delta
74 
75         if( run < len )
76             bs_write_se( s, (int8_t)-list[zigzag[run]] );
77     }
78 }
79 
x264_sei_write(bs_t * s,uint8_t * payload,int payload_size,int payload_type)80 void x264_sei_write( bs_t *s, uint8_t *payload, int payload_size, int payload_type )
81 {
82     int i;
83 
84     bs_realign( s );
85 
86     for( i = 0; i <= payload_type-255; i += 255 )
87         bs_write( s, 8, 255 );
88     bs_write( s, 8, payload_type-i );
89 
90     for( i = 0; i <= payload_size-255; i += 255 )
91         bs_write( s, 8, 255 );
92     bs_write( s, 8, payload_size-i );
93 
94     for( i = 0; i < payload_size; i++ )
95         bs_write( s, 8, payload[i] );
96 
97     bs_rbsp_trailing( s );
98     bs_flush( s );
99 }
100 
x264_sps_init(x264_sps_t * sps,int i_id,x264_param_t * param)101 void x264_sps_init( x264_sps_t *sps, int i_id, x264_param_t *param )
102 {
103     int csp = param->i_csp & X264_CSP_MASK;
104 
105     sps->i_id = i_id;
106     sps->i_mb_width = ( param->i_width + 15 ) / 16;
107     sps->i_mb_height= ( param->i_height + 15 ) / 16;
108     sps->b_frame_mbs_only = !(param->b_interlaced || param->b_fake_interlaced);
109     if( !sps->b_frame_mbs_only )
110         sps->i_mb_height = ( sps->i_mb_height + 1 ) & ~1;
111     sps->i_chroma_format_idc = csp >= X264_CSP_I444 ? CHROMA_444 :
112                                csp >= X264_CSP_I422 ? CHROMA_422 :
113                                csp >= X264_CSP_I420 ? CHROMA_420 : CHROMA_400;
114 
115     sps->b_qpprime_y_zero_transform_bypass = param->rc.i_rc_method == X264_RC_CQP && param->rc.i_qp_constant == 0;
116     if( sps->b_qpprime_y_zero_transform_bypass || sps->i_chroma_format_idc == CHROMA_444 )
117         sps->i_profile_idc  = PROFILE_HIGH444_PREDICTIVE;
118     else if( sps->i_chroma_format_idc == CHROMA_422 )
119         sps->i_profile_idc  = PROFILE_HIGH422;
120     else if( BIT_DEPTH > 8 )
121         sps->i_profile_idc  = PROFILE_HIGH10;
122     else if( param->analyse.b_transform_8x8 || param->i_cqm_preset != X264_CQM_FLAT || sps->i_chroma_format_idc == CHROMA_400 )
123         sps->i_profile_idc  = PROFILE_HIGH;
124     else if( param->b_cabac || param->i_bframe > 0 || param->b_interlaced || param->b_fake_interlaced || param->analyse.i_weighted_pred > 0 )
125         sps->i_profile_idc  = PROFILE_MAIN;
126     else
127         sps->i_profile_idc  = PROFILE_BASELINE;
128 
129     sps->b_constraint_set0  = sps->i_profile_idc == PROFILE_BASELINE;
130     /* x264 doesn't support the features that are in Baseline and not in Main,
131      * namely arbitrary_slice_order and slice_groups. */
132     sps->b_constraint_set1  = sps->i_profile_idc <= PROFILE_MAIN;
133     /* Never set constraint_set2, it is not necessary and not used in real world. */
134     sps->b_constraint_set2  = 0;
135     sps->b_constraint_set3  = 0;
136 
137     sps->i_level_idc = param->i_level_idc;
138     if( param->i_level_idc == 9 && ( sps->i_profile_idc == PROFILE_BASELINE || sps->i_profile_idc == PROFILE_MAIN ) )
139     {
140         sps->b_constraint_set3 = 1; /* level 1b with Baseline or Main profile is signalled via constraint_set3 */
141         sps->i_level_idc      = 11;
142     }
143     /* Intra profiles */
144     if( param->i_keyint_max == 1 && sps->i_profile_idc >= PROFILE_HIGH )
145         sps->b_constraint_set3 = 1;
146 
147     sps->vui.i_num_reorder_frames = param->i_bframe_pyramid ? 2 : param->i_bframe ? 1 : 0;
148     /* extra slot with pyramid so that we don't have to override the
149      * order of forgetting old pictures */
150     sps->vui.i_max_dec_frame_buffering =
151     sps->i_num_ref_frames = X264_MIN(X264_REF_MAX, X264_MAX4(param->i_frame_reference, 1 + sps->vui.i_num_reorder_frames,
152                             param->i_bframe_pyramid ? 4 : 1, param->i_dpb_size));
153     sps->i_num_ref_frames -= param->i_bframe_pyramid == X264_B_PYRAMID_STRICT;
154     if( param->i_keyint_max == 1 )
155     {
156         sps->i_num_ref_frames = 0;
157         sps->vui.i_max_dec_frame_buffering = 0;
158     }
159 
160     /* number of refs + current frame */
161     int max_frame_num = sps->vui.i_max_dec_frame_buffering * (!!param->i_bframe_pyramid+1) + 1;
162     /* Intra refresh cannot write a recovery time greater than max frame num-1 */
163     if( param->b_intra_refresh )
164     {
165         int time_to_recovery = X264_MIN( sps->i_mb_width - 1, param->i_keyint_max ) + param->i_bframe - 1;
166         max_frame_num = X264_MAX( max_frame_num, time_to_recovery+1 );
167     }
168 
169     sps->i_log2_max_frame_num = 4;
170     while( (1 << sps->i_log2_max_frame_num) <= max_frame_num )
171         sps->i_log2_max_frame_num++;
172 
173     sps->i_poc_type = param->i_bframe || param->b_interlaced || param->i_avcintra_class ? 0 : 2;
174     if( sps->i_poc_type == 0 )
175     {
176         int max_delta_poc = (param->i_bframe + 2) * (!!param->i_bframe_pyramid + 1) * 2;
177         sps->i_log2_max_poc_lsb = 4;
178         while( (1 << sps->i_log2_max_poc_lsb) <= max_delta_poc * 2 )
179             sps->i_log2_max_poc_lsb++;
180     }
181 
182     sps->b_vui = 1;
183 
184     sps->b_gaps_in_frame_num_value_allowed = 0;
185     sps->b_mb_adaptive_frame_field = param->b_interlaced;
186     sps->b_direct8x8_inference = 1;
187 
188     x264_sps_init_reconfigurable( sps, param );
189 
190     sps->vui.b_overscan_info_present = param->vui.i_overscan > 0 && param->vui.i_overscan <= 2;
191     if( sps->vui.b_overscan_info_present )
192         sps->vui.b_overscan_info = ( param->vui.i_overscan == 2 ? 1 : 0 );
193 
194     sps->vui.b_signal_type_present = 0;
195     sps->vui.i_vidformat = ( param->vui.i_vidformat >= 0 && param->vui.i_vidformat <= 5 ? param->vui.i_vidformat : 5 );
196     sps->vui.b_fullrange = ( param->vui.b_fullrange >= 0 && param->vui.b_fullrange <= 1 ? param->vui.b_fullrange :
197                            ( csp >= X264_CSP_BGR ? 1 : 0 ) );
198     sps->vui.b_color_description_present = 0;
199 
200     sps->vui.i_colorprim = ( param->vui.i_colorprim >= 0 && param->vui.i_colorprim <= 12 ? param->vui.i_colorprim : 2 );
201     sps->vui.i_transfer  = ( param->vui.i_transfer  >= 0 && param->vui.i_transfer  <= 18 ? param->vui.i_transfer  : 2 );
202     sps->vui.i_colmatrix = ( param->vui.i_colmatrix >= 0 && param->vui.i_colmatrix <= 14 ? param->vui.i_colmatrix :
203                            ( csp >= X264_CSP_BGR ? 0 : 2 ) );
204     if( sps->vui.i_colorprim != 2 || sps->vui.i_transfer != 2 || sps->vui.i_colmatrix != 2 )
205         sps->vui.b_color_description_present = 1;
206 
207     if( sps->vui.i_vidformat != 5 || sps->vui.b_fullrange || sps->vui.b_color_description_present )
208         sps->vui.b_signal_type_present = 1;
209 
210     /* FIXME: not sufficient for interlaced video */
211     sps->vui.b_chroma_loc_info_present = param->vui.i_chroma_loc > 0 && param->vui.i_chroma_loc <= 5 &&
212                                          sps->i_chroma_format_idc == CHROMA_420;
213     if( sps->vui.b_chroma_loc_info_present )
214     {
215         sps->vui.i_chroma_loc_top = param->vui.i_chroma_loc;
216         sps->vui.i_chroma_loc_bottom = param->vui.i_chroma_loc;
217     }
218 
219     sps->vui.b_timing_info_present = param->i_timebase_num > 0 && param->i_timebase_den > 0;
220 
221     if( sps->vui.b_timing_info_present )
222     {
223         sps->vui.i_num_units_in_tick = param->i_timebase_num;
224         sps->vui.i_time_scale = param->i_timebase_den * 2;
225         sps->vui.b_fixed_frame_rate = !param->b_vfr_input;
226     }
227 
228     sps->vui.b_vcl_hrd_parameters_present = 0; // we don't support VCL HRD
229     sps->vui.b_nal_hrd_parameters_present = !!param->i_nal_hrd;
230     sps->vui.b_pic_struct_present = param->b_pic_struct;
231 
232     // NOTE: HRD related parts of the SPS are initialised in x264_ratecontrol_init_reconfigurable
233 
234     sps->vui.b_bitstream_restriction = !(sps->b_constraint_set3 && sps->i_profile_idc >= PROFILE_HIGH);
235     if( sps->vui.b_bitstream_restriction )
236     {
237         sps->vui.b_motion_vectors_over_pic_boundaries = 1;
238         sps->vui.i_max_bytes_per_pic_denom = 0;
239         sps->vui.i_max_bits_per_mb_denom = 0;
240         sps->vui.i_log2_max_mv_length_horizontal =
241         sps->vui.i_log2_max_mv_length_vertical = (int)log2f( X264_MAX( 1, param->analyse.i_mv_range*4-1 ) ) + 1;
242     }
243 
244     sps->b_avcintra = !!param->i_avcintra_class;
245     sps->i_cqm_preset = param->i_cqm_preset;
246 }
247 
x264_sps_init_reconfigurable(x264_sps_t * sps,x264_param_t * param)248 void x264_sps_init_reconfigurable( x264_sps_t *sps, x264_param_t *param )
249 {
250     sps->crop.i_left   = param->crop_rect.i_left;
251     sps->crop.i_top    = param->crop_rect.i_top;
252     sps->crop.i_right  = param->crop_rect.i_right + sps->i_mb_width*16 - param->i_width;
253     sps->crop.i_bottom = param->crop_rect.i_bottom + sps->i_mb_height*16 - param->i_height;
254     sps->b_crop = sps->crop.i_left  || sps->crop.i_top ||
255                   sps->crop.i_right || sps->crop.i_bottom;
256 
257     sps->vui.b_aspect_ratio_info_present = 0;
258     if( param->vui.i_sar_width > 0 && param->vui.i_sar_height > 0 )
259     {
260         sps->vui.b_aspect_ratio_info_present = 1;
261         sps->vui.i_sar_width = param->vui.i_sar_width;
262         sps->vui.i_sar_height= param->vui.i_sar_height;
263     }
264 }
265 
x264_sps_init_scaling_list(x264_sps_t * sps,x264_param_t * param)266 void x264_sps_init_scaling_list( x264_sps_t *sps, x264_param_t *param )
267 {
268     switch( sps->i_cqm_preset )
269     {
270     case X264_CQM_FLAT:
271         for( int i = 0; i < 8; i++ )
272             sps->scaling_list[i] = x264_cqm_flat16;
273         break;
274     case X264_CQM_JVT:
275         for( int i = 0; i < 8; i++ )
276             sps->scaling_list[i] = x264_cqm_jvt[i];
277         break;
278     case X264_CQM_CUSTOM:
279         /* match the transposed DCT & zigzag */
280         transpose( param->cqm_4iy, 4 );
281         transpose( param->cqm_4py, 4 );
282         transpose( param->cqm_4ic, 4 );
283         transpose( param->cqm_4pc, 4 );
284         transpose( param->cqm_8iy, 8 );
285         transpose( param->cqm_8py, 8 );
286         transpose( param->cqm_8ic, 8 );
287         transpose( param->cqm_8pc, 8 );
288         sps->scaling_list[CQM_4IY] = param->cqm_4iy;
289         sps->scaling_list[CQM_4PY] = param->cqm_4py;
290         sps->scaling_list[CQM_4IC] = param->cqm_4ic;
291         sps->scaling_list[CQM_4PC] = param->cqm_4pc;
292         sps->scaling_list[CQM_8IY+4] = param->cqm_8iy;
293         sps->scaling_list[CQM_8PY+4] = param->cqm_8py;
294         sps->scaling_list[CQM_8IC+4] = param->cqm_8ic;
295         sps->scaling_list[CQM_8PC+4] = param->cqm_8pc;
296         for( int i = 0; i < 8; i++ )
297             for( int j = 0; j < (i < 4 ? 16 : 64); j++ )
298                 if( sps->scaling_list[i][j] == 0 )
299                     sps->scaling_list[i] = x264_cqm_jvt[i];
300         break;
301     }
302 }
303 
x264_sps_write(bs_t * s,x264_sps_t * sps)304 void x264_sps_write( bs_t *s, x264_sps_t *sps )
305 {
306     bs_realign( s );
307     bs_write( s, 8, sps->i_profile_idc );
308     bs_write1( s, sps->b_constraint_set0 );
309     bs_write1( s, sps->b_constraint_set1 );
310     bs_write1( s, sps->b_constraint_set2 );
311     bs_write1( s, sps->b_constraint_set3 );
312 
313     bs_write( s, 4, 0 );    /* reserved */
314 
315     bs_write( s, 8, sps->i_level_idc );
316 
317     bs_write_ue( s, sps->i_id );
318 
319     if( sps->i_profile_idc >= PROFILE_HIGH )
320     {
321         bs_write_ue( s, sps->i_chroma_format_idc );
322         if( sps->i_chroma_format_idc == CHROMA_444 )
323             bs_write1( s, 0 ); // separate_colour_plane_flag
324         bs_write_ue( s, BIT_DEPTH-8 ); // bit_depth_luma_minus8
325         bs_write_ue( s, BIT_DEPTH-8 ); // bit_depth_chroma_minus8
326         bs_write1( s, sps->b_qpprime_y_zero_transform_bypass );
327         /* Exactly match the AVC-Intra bitstream */
328         bs_write1( s, sps->b_avcintra ); // seq_scaling_matrix_present_flag
329         if( sps->b_avcintra )
330         {
331             scaling_list_write( s, sps, CQM_4IY );
332             scaling_list_write( s, sps, CQM_4IC );
333             scaling_list_write( s, sps, CQM_4IC );
334             bs_write1( s, 0 ); // no inter
335             bs_write1( s, 0 ); // no inter
336             bs_write1( s, 0 ); // no inter
337             scaling_list_write( s, sps, CQM_8IY+4 );
338             bs_write1( s, 0 ); // no inter
339             if( sps->i_chroma_format_idc == CHROMA_444 )
340             {
341                 scaling_list_write( s, sps, CQM_8IC+4 );
342                 bs_write1( s, 0 ); // no inter
343                 scaling_list_write( s, sps, CQM_8IC+4 );
344                 bs_write1( s, 0 ); // no inter
345             }
346         }
347     }
348 
349     bs_write_ue( s, sps->i_log2_max_frame_num - 4 );
350     bs_write_ue( s, sps->i_poc_type );
351     if( sps->i_poc_type == 0 )
352         bs_write_ue( s, sps->i_log2_max_poc_lsb - 4 );
353     bs_write_ue( s, sps->i_num_ref_frames );
354     bs_write1( s, sps->b_gaps_in_frame_num_value_allowed );
355     bs_write_ue( s, sps->i_mb_width - 1 );
356     bs_write_ue( s, (sps->i_mb_height >> !sps->b_frame_mbs_only) - 1);
357     bs_write1( s, sps->b_frame_mbs_only );
358     if( !sps->b_frame_mbs_only )
359         bs_write1( s, sps->b_mb_adaptive_frame_field );
360     bs_write1( s, sps->b_direct8x8_inference );
361 
362     bs_write1( s, sps->b_crop );
363     if( sps->b_crop )
364     {
365         int h_shift = sps->i_chroma_format_idc == CHROMA_420 || sps->i_chroma_format_idc == CHROMA_422;
366         int v_shift = (sps->i_chroma_format_idc == CHROMA_420) + !sps->b_frame_mbs_only;
367         bs_write_ue( s, sps->crop.i_left   >> h_shift );
368         bs_write_ue( s, sps->crop.i_right  >> h_shift );
369         bs_write_ue( s, sps->crop.i_top    >> v_shift );
370         bs_write_ue( s, sps->crop.i_bottom >> v_shift );
371     }
372 
373     bs_write1( s, sps->b_vui );
374     if( sps->b_vui )
375     {
376         bs_write1( s, sps->vui.b_aspect_ratio_info_present );
377         if( sps->vui.b_aspect_ratio_info_present )
378         {
379             int i;
380             static const struct { uint8_t w, h, sar; } sar[] =
381             {
382                 // aspect_ratio_idc = 0 -> unspecified
383                 {  1,  1, 1 }, { 12, 11, 2 }, { 10, 11, 3 }, { 16, 11, 4 },
384                 { 40, 33, 5 }, { 24, 11, 6 }, { 20, 11, 7 }, { 32, 11, 8 },
385                 { 80, 33, 9 }, { 18, 11, 10}, { 15, 11, 11}, { 64, 33, 12},
386                 {160, 99, 13}, {  4,  3, 14}, {  3,  2, 15}, {  2,  1, 16},
387                 // aspect_ratio_idc = [17..254] -> reserved
388                 { 0, 0, 255 }
389             };
390             for( i = 0; sar[i].sar != 255; i++ )
391             {
392                 if( sar[i].w == sps->vui.i_sar_width &&
393                     sar[i].h == sps->vui.i_sar_height )
394                     break;
395             }
396             bs_write( s, 8, sar[i].sar );
397             if( sar[i].sar == 255 ) /* aspect_ratio_idc (extended) */
398             {
399                 bs_write( s, 16, sps->vui.i_sar_width );
400                 bs_write( s, 16, sps->vui.i_sar_height );
401             }
402         }
403 
404         bs_write1( s, sps->vui.b_overscan_info_present );
405         if( sps->vui.b_overscan_info_present )
406             bs_write1( s, sps->vui.b_overscan_info );
407 
408         bs_write1( s, sps->vui.b_signal_type_present );
409         if( sps->vui.b_signal_type_present )
410         {
411             bs_write( s, 3, sps->vui.i_vidformat );
412             bs_write1( s, sps->vui.b_fullrange );
413             bs_write1( s, sps->vui.b_color_description_present );
414             if( sps->vui.b_color_description_present )
415             {
416                 bs_write( s, 8, sps->vui.i_colorprim );
417                 bs_write( s, 8, sps->vui.i_transfer );
418                 bs_write( s, 8, sps->vui.i_colmatrix );
419             }
420         }
421 
422         bs_write1( s, sps->vui.b_chroma_loc_info_present );
423         if( sps->vui.b_chroma_loc_info_present )
424         {
425             bs_write_ue( s, sps->vui.i_chroma_loc_top );
426             bs_write_ue( s, sps->vui.i_chroma_loc_bottom );
427         }
428 
429         bs_write1( s, sps->vui.b_timing_info_present );
430         if( sps->vui.b_timing_info_present )
431         {
432             bs_write32( s, sps->vui.i_num_units_in_tick );
433             bs_write32( s, sps->vui.i_time_scale );
434             bs_write1( s, sps->vui.b_fixed_frame_rate );
435         }
436 
437         bs_write1( s, sps->vui.b_nal_hrd_parameters_present );
438         if( sps->vui.b_nal_hrd_parameters_present )
439         {
440             bs_write_ue( s, sps->vui.hrd.i_cpb_cnt - 1 );
441             bs_write( s, 4, sps->vui.hrd.i_bit_rate_scale );
442             bs_write( s, 4, sps->vui.hrd.i_cpb_size_scale );
443 
444             bs_write_ue( s, sps->vui.hrd.i_bit_rate_value - 1 );
445             bs_write_ue( s, sps->vui.hrd.i_cpb_size_value - 1 );
446 
447             bs_write1( s, sps->vui.hrd.b_cbr_hrd );
448 
449             bs_write( s, 5, sps->vui.hrd.i_initial_cpb_removal_delay_length - 1 );
450             bs_write( s, 5, sps->vui.hrd.i_cpb_removal_delay_length - 1 );
451             bs_write( s, 5, sps->vui.hrd.i_dpb_output_delay_length - 1 );
452             bs_write( s, 5, sps->vui.hrd.i_time_offset_length );
453         }
454 
455         bs_write1( s, sps->vui.b_vcl_hrd_parameters_present );
456 
457         if( sps->vui.b_nal_hrd_parameters_present || sps->vui.b_vcl_hrd_parameters_present )
458             bs_write1( s, 0 );   /* low_delay_hrd_flag */
459 
460         bs_write1( s, sps->vui.b_pic_struct_present );
461         bs_write1( s, sps->vui.b_bitstream_restriction );
462         if( sps->vui.b_bitstream_restriction )
463         {
464             bs_write1( s, sps->vui.b_motion_vectors_over_pic_boundaries );
465             bs_write_ue( s, sps->vui.i_max_bytes_per_pic_denom );
466             bs_write_ue( s, sps->vui.i_max_bits_per_mb_denom );
467             bs_write_ue( s, sps->vui.i_log2_max_mv_length_horizontal );
468             bs_write_ue( s, sps->vui.i_log2_max_mv_length_vertical );
469             bs_write_ue( s, sps->vui.i_num_reorder_frames );
470             bs_write_ue( s, sps->vui.i_max_dec_frame_buffering );
471         }
472     }
473 
474     bs_rbsp_trailing( s );
475     bs_flush( s );
476 }
477 
x264_pps_init(x264_pps_t * pps,int i_id,x264_param_t * param,x264_sps_t * sps)478 void x264_pps_init( x264_pps_t *pps, int i_id, x264_param_t *param, x264_sps_t *sps )
479 {
480     pps->i_id = i_id;
481     pps->i_sps_id = sps->i_id;
482     pps->b_cabac = param->b_cabac;
483 
484     pps->b_pic_order = !param->i_avcintra_class && param->b_interlaced;
485     pps->i_num_slice_groups = 1;
486 
487     pps->i_num_ref_idx_l0_default_active = param->i_frame_reference;
488     pps->i_num_ref_idx_l1_default_active = 1;
489 
490     pps->b_weighted_pred = param->analyse.i_weighted_pred > 0;
491     pps->b_weighted_bipred = param->analyse.b_weighted_bipred ? 2 : 0;
492 
493     pps->i_pic_init_qp = param->rc.i_rc_method == X264_RC_ABR || param->b_stitchable ? 26 + QP_BD_OFFSET : SPEC_QP( param->rc.i_qp_constant );
494     pps->i_pic_init_qs = 26 + QP_BD_OFFSET;
495 
496     pps->i_chroma_qp_index_offset = param->analyse.i_chroma_qp_offset;
497     pps->b_deblocking_filter_control = 1;
498     pps->b_constrained_intra_pred = param->b_constrained_intra;
499     pps->b_redundant_pic_cnt = 0;
500 
501     pps->b_transform_8x8_mode = param->analyse.b_transform_8x8 ? 1 : 0;
502 }
503 
x264_pps_write(bs_t * s,x264_sps_t * sps,x264_pps_t * pps)504 void x264_pps_write( bs_t *s, x264_sps_t *sps, x264_pps_t *pps )
505 {
506     bs_realign( s );
507     bs_write_ue( s, pps->i_id );
508     bs_write_ue( s, pps->i_sps_id );
509 
510     bs_write1( s, pps->b_cabac );
511     bs_write1( s, pps->b_pic_order );
512     bs_write_ue( s, pps->i_num_slice_groups - 1 );
513 
514     bs_write_ue( s, pps->i_num_ref_idx_l0_default_active - 1 );
515     bs_write_ue( s, pps->i_num_ref_idx_l1_default_active - 1 );
516     bs_write1( s, pps->b_weighted_pred );
517     bs_write( s, 2, pps->b_weighted_bipred );
518 
519     bs_write_se( s, pps->i_pic_init_qp - 26 - QP_BD_OFFSET );
520     bs_write_se( s, pps->i_pic_init_qs - 26 - QP_BD_OFFSET );
521     bs_write_se( s, pps->i_chroma_qp_index_offset );
522 
523     bs_write1( s, pps->b_deblocking_filter_control );
524     bs_write1( s, pps->b_constrained_intra_pred );
525     bs_write1( s, pps->b_redundant_pic_cnt );
526 
527     int b_scaling_list = !sps->b_avcintra && sps->i_cqm_preset != X264_CQM_FLAT;
528     if( pps->b_transform_8x8_mode || b_scaling_list )
529     {
530         bs_write1( s, pps->b_transform_8x8_mode );
531         bs_write1( s, b_scaling_list );
532         if( b_scaling_list )
533         {
534             scaling_list_write( s, sps, CQM_4IY );
535             scaling_list_write( s, sps, CQM_4IC );
536             bs_write1( s, 0 ); // Cr = Cb
537             scaling_list_write( s, sps, CQM_4PY );
538             scaling_list_write( s, sps, CQM_4PC );
539             bs_write1( s, 0 ); // Cr = Cb
540             if( pps->b_transform_8x8_mode )
541             {
542                 scaling_list_write( s, sps, CQM_8IY+4 );
543                 scaling_list_write( s, sps, CQM_8PY+4 );
544                 if( sps->i_chroma_format_idc == CHROMA_444 )
545                 {
546                     scaling_list_write( s, sps, CQM_8IC+4 );
547                     scaling_list_write( s, sps, CQM_8PC+4 );
548                     bs_write1( s, 0 ); // Cr = Cb
549                     bs_write1( s, 0 ); // Cr = Cb
550                 }
551             }
552         }
553         bs_write_se( s, pps->i_chroma_qp_index_offset );
554     }
555 
556     bs_rbsp_trailing( s );
557     bs_flush( s );
558 }
559 
x264_sei_recovery_point_write(x264_t * h,bs_t * s,int recovery_frame_cnt)560 void x264_sei_recovery_point_write( x264_t *h, bs_t *s, int recovery_frame_cnt )
561 {
562     bs_t q;
563     ALIGNED_4( uint8_t tmp_buf[100] );
564     M32( tmp_buf ) = 0; // shut up gcc
565     bs_init( &q, tmp_buf, 100 );
566 
567     bs_realign( &q );
568 
569     bs_write_ue( &q, recovery_frame_cnt ); // recovery_frame_cnt
570     bs_write1( &q, 1 );   //exact_match_flag 1
571     bs_write1( &q, 0 );   //broken_link_flag 0
572     bs_write( &q, 2, 0 ); //changing_slice_group 0
573 
574     bs_align_10( &q );
575 
576     x264_sei_write( s, tmp_buf, bs_pos( &q ) / 8, SEI_RECOVERY_POINT );
577 }
578 
x264_sei_version_write(x264_t * h,bs_t * s)579 int x264_sei_version_write( x264_t *h, bs_t *s )
580 {
581     // random ID number generated according to ISO-11578
582     static const uint8_t uuid[16] =
583     {
584         0xdc, 0x45, 0xe9, 0xbd, 0xe6, 0xd9, 0x48, 0xb7,
585         0x96, 0x2c, 0xd8, 0x20, 0xd9, 0x23, 0xee, 0xef
586     };
587     char *opts = x264_param2string( &h->param, 0 );
588     char *payload;
589     int length;
590 
591     if( !opts )
592         return -1;
593     CHECKED_MALLOC( payload, 200 + strlen( opts ) );
594 
595     memcpy( payload, uuid, 16 );
596     sprintf( payload+16, "x264 - core %d%s - H.264/MPEG-4 AVC codec - "
597              "Copy%s 2003-2021 - http://www.videolan.org/x264.html - options: %s",
598              X264_BUILD, X264_VERSION, HAVE_GPL?"left":"right", opts );
599     length = strlen(payload)+1;
600 
601     x264_sei_write( s, (uint8_t *)payload, length, SEI_USER_DATA_UNREGISTERED );
602 
603     x264_free( opts );
604     x264_free( payload );
605     return 0;
606 fail:
607     x264_free( opts );
608     return -1;
609 }
610 
x264_sei_buffering_period_write(x264_t * h,bs_t * s)611 void x264_sei_buffering_period_write( x264_t *h, bs_t *s )
612 {
613     x264_sps_t *sps = h->sps;
614     bs_t q;
615     ALIGNED_4( uint8_t tmp_buf[100] );
616     M32( tmp_buf ) = 0; // shut up gcc
617     bs_init( &q, tmp_buf, 100 );
618 
619     bs_realign( &q );
620     bs_write_ue( &q, sps->i_id );
621 
622     if( sps->vui.b_nal_hrd_parameters_present )
623     {
624         bs_write( &q, sps->vui.hrd.i_initial_cpb_removal_delay_length, h->initial_cpb_removal_delay );
625         bs_write( &q, sps->vui.hrd.i_initial_cpb_removal_delay_length, h->initial_cpb_removal_delay_offset );
626     }
627 
628     bs_align_10( &q );
629 
630     x264_sei_write( s, tmp_buf, bs_pos( &q ) / 8, SEI_BUFFERING_PERIOD );
631 }
632 
x264_sei_pic_timing_write(x264_t * h,bs_t * s)633 void x264_sei_pic_timing_write( x264_t *h, bs_t *s )
634 {
635     x264_sps_t *sps = h->sps;
636     bs_t q;
637     ALIGNED_4( uint8_t tmp_buf[100] );
638     M32( tmp_buf ) = 0; // shut up gcc
639     bs_init( &q, tmp_buf, 100 );
640 
641     bs_realign( &q );
642 
643     if( sps->vui.b_nal_hrd_parameters_present || sps->vui.b_vcl_hrd_parameters_present )
644     {
645         bs_write( &q, sps->vui.hrd.i_cpb_removal_delay_length, h->fenc->i_cpb_delay - h->i_cpb_delay_pir_offset );
646         bs_write( &q, sps->vui.hrd.i_dpb_output_delay_length, h->fenc->i_dpb_output_delay );
647     }
648 
649     if( sps->vui.b_pic_struct_present )
650     {
651         bs_write( &q, 4, h->fenc->i_pic_struct-1 ); // We use index 0 for "Auto"
652 
653         // These clock timestamps are not standardised so we don't set them
654         // They could be time of origin, capture or alternative ideal display
655         for( int i = 0; i < num_clock_ts[h->fenc->i_pic_struct]; i++ )
656             bs_write1( &q, 0 ); // clock_timestamp_flag
657     }
658 
659     bs_align_10( &q );
660 
661     x264_sei_write( s, tmp_buf, bs_pos( &q ) / 8, SEI_PIC_TIMING );
662 }
663 
x264_sei_frame_packing_write(x264_t * h,bs_t * s)664 void x264_sei_frame_packing_write( x264_t *h, bs_t *s )
665 {
666     int quincunx_sampling_flag = h->param.i_frame_packing == 0;
667     bs_t q;
668     ALIGNED_4( uint8_t tmp_buf[100] );
669     M32( tmp_buf ) = 0; // shut up gcc
670     bs_init( &q, tmp_buf, 100 );
671 
672     bs_realign( &q );
673 
674     bs_write_ue( &q, 0 );                         // frame_packing_arrangement_id
675     bs_write1( &q, 0 );                           // frame_packing_arrangement_cancel_flag
676     bs_write ( &q, 7, h->param.i_frame_packing ); // frame_packing_arrangement_type
677     bs_write1( &q, quincunx_sampling_flag );      // quincunx_sampling_flag
678 
679     // 0: views are unrelated, 1: left view is on the left, 2: left view is on the right
680     bs_write ( &q, 6, h->param.i_frame_packing != 6 ); // content_interpretation_type
681 
682     bs_write1( &q, 0 );                           // spatial_flipping_flag
683     bs_write1( &q, 0 );                           // frame0_flipped_flag
684     bs_write1( &q, 0 );                           // field_views_flag
685     bs_write1( &q, h->param.i_frame_packing == 5 && !(h->fenc->i_frame&1) ); // current_frame_is_frame0_flag
686     bs_write1( &q, 0 );                           // frame0_self_contained_flag
687     bs_write1( &q, 0 );                           // frame1_self_contained_flag
688     if( quincunx_sampling_flag == 0 && h->param.i_frame_packing != 5 )
689     {
690         bs_write( &q, 4, 0 );                     // frame0_grid_position_x
691         bs_write( &q, 4, 0 );                     // frame0_grid_position_y
692         bs_write( &q, 4, 0 );                     // frame1_grid_position_x
693         bs_write( &q, 4, 0 );                     // frame1_grid_position_y
694     }
695     bs_write( &q, 8, 0 );                         // frame_packing_arrangement_reserved_byte
696     // "frame_packing_arrangement_repetition_period equal to 1 specifies that the frame packing arrangement SEI message persists in output"
697     // for (i_frame_packing == 5) this will undermine current_frame_is_frame0_flag which must alternate every view sequence
698     bs_write_ue( &q, h->param.i_frame_packing != 5 ); // frame_packing_arrangement_repetition_period
699     bs_write1( &q, 0 );                           // frame_packing_arrangement_extension_flag
700 
701     bs_align_10( &q );
702 
703     x264_sei_write( s, tmp_buf, bs_pos( &q ) / 8, SEI_FRAME_PACKING );
704 }
705 
x264_sei_mastering_display_write(x264_t * h,bs_t * s)706 void x264_sei_mastering_display_write( x264_t *h, bs_t *s )
707 {
708     bs_t q;
709     ALIGNED_4( uint8_t tmp_buf[100] );
710     M32( tmp_buf ) = 0; // shut up gcc
711     bs_init( &q, tmp_buf, 100 );
712 
713     bs_realign( &q );
714 
715     bs_write( &q, 16, h->param.mastering_display.i_green_x );
716     bs_write( &q, 16, h->param.mastering_display.i_green_y );
717     bs_write( &q, 16, h->param.mastering_display.i_blue_x );
718     bs_write( &q, 16, h->param.mastering_display.i_blue_y );
719     bs_write( &q, 16, h->param.mastering_display.i_red_x );
720     bs_write( &q, 16, h->param.mastering_display.i_red_y );
721     bs_write( &q, 16, h->param.mastering_display.i_white_x );
722     bs_write( &q, 16, h->param.mastering_display.i_white_y );
723     bs_write32( &q, h->param.mastering_display.i_display_max );
724     bs_write32( &q, h->param.mastering_display.i_display_min );
725 
726     bs_align_10( &q );
727 
728     x264_sei_write( s, tmp_buf, bs_pos( &q ) / 8, SEI_MASTERING_DISPLAY );
729 }
730 
x264_sei_content_light_level_write(x264_t * h,bs_t * s)731 void x264_sei_content_light_level_write( x264_t *h, bs_t *s )
732 {
733     bs_t q;
734     ALIGNED_4( uint8_t tmp_buf[100] );
735     M32( tmp_buf ) = 0; // shut up gcc
736     bs_init( &q, tmp_buf, 100 );
737 
738     bs_realign( &q );
739 
740     bs_write( &q, 16, h->param.content_light_level.i_max_cll );
741     bs_write( &q, 16, h->param.content_light_level.i_max_fall );
742 
743     bs_align_10( &q );
744 
745     x264_sei_write( s, tmp_buf, bs_pos( &q ) / 8, SEI_CONTENT_LIGHT_LEVEL );
746 }
747 
x264_sei_alternative_transfer_write(x264_t * h,bs_t * s)748 void x264_sei_alternative_transfer_write( x264_t *h, bs_t *s )
749 {
750     bs_t q;
751     ALIGNED_4( uint8_t tmp_buf[100] );
752     M32( tmp_buf ) = 0; // shut up gcc
753     bs_init( &q, tmp_buf, 100 );
754 
755     bs_realign( &q );
756 
757     bs_write ( &q, 8, h->param.i_alternative_transfer ); // preferred_transfer_characteristics
758 
759     bs_align_10( &q );
760 
761     x264_sei_write( s, tmp_buf, bs_pos( &q ) / 8, SEI_ALTERNATIVE_TRANSFER );
762 }
763 
x264_filler_write(x264_t * h,bs_t * s,int filler)764 void x264_filler_write( x264_t *h, bs_t *s, int filler )
765 {
766     bs_realign( s );
767 
768     for( int i = 0; i < filler; i++ )
769         bs_write( s, 8, 0xff );
770 
771     bs_rbsp_trailing( s );
772     bs_flush( s );
773 }
774 
x264_sei_dec_ref_pic_marking_write(x264_t * h,bs_t * s)775 void x264_sei_dec_ref_pic_marking_write( x264_t *h, bs_t *s )
776 {
777     x264_slice_header_t *sh = &h->sh_backup;
778     bs_t q;
779     ALIGNED_4( uint8_t tmp_buf[100] );
780     M32( tmp_buf ) = 0; // shut up gcc
781     bs_init( &q, tmp_buf, 100 );
782 
783     bs_realign( &q );
784 
785     /* We currently only use this for repeating B-refs, as required by Blu-ray. */
786     bs_write1( &q, 0 );                 //original_idr_flag
787     bs_write_ue( &q, sh->i_frame_num ); //original_frame_num
788     if( !h->sps->b_frame_mbs_only )
789         bs_write1( &q, 0 );             //original_field_pic_flag
790 
791     bs_write1( &q, sh->i_mmco_command_count > 0 );
792     if( sh->i_mmco_command_count > 0 )
793     {
794         for( int i = 0; i < sh->i_mmco_command_count; i++ )
795         {
796             bs_write_ue( &q, 1 );
797             bs_write_ue( &q, sh->mmco[i].i_difference_of_pic_nums - 1 );
798         }
799         bs_write_ue( &q, 0 );
800     }
801 
802     bs_align_10( &q );
803 
804     x264_sei_write( s, tmp_buf, bs_pos( &q ) / 8, SEI_DEC_REF_PIC_MARKING );
805 }
806 
x264_sei_avcintra_umid_write(x264_t * h,bs_t * s)807 int x264_sei_avcintra_umid_write( x264_t *h, bs_t *s )
808 {
809     uint8_t data[512];
810     const char *msg = "UMID";
811     const int len = 497;
812 
813     memset( data, 0xff, len );
814     memcpy( data, avcintra_uuid, sizeof(avcintra_uuid) );
815     memcpy( data+16, msg, strlen(msg) );
816 
817     data[20] = 0x13;
818     /* These bytes appear to be some sort of frame/seconds counter in certain applications,
819      * but others jump around, so leave them as zero for now */
820     data[22] = data[23] = data[25] = data[26] = 0;
821     data[28] = 0x14;
822     data[30] = data[31] = data[33] = data[34] = 0;
823     data[36] = 0x60;
824     data[41] = 0x22; /* Believed to be some sort of end of basic UMID identifier */
825     data[60] = 0x62;
826     data[62] = data[63] = data[65] = data[66] = 0;
827     data[68] = 0x63;
828     data[70] = data[71] = data[73] = data[74] = 0;
829 
830     x264_sei_write( &h->out.bs, data, len, SEI_USER_DATA_UNREGISTERED );
831 
832     return 0;
833 }
834 
x264_sei_avcintra_vanc_write(x264_t * h,bs_t * s,int len)835 int x264_sei_avcintra_vanc_write( x264_t *h, bs_t *s, int len )
836 {
837     uint8_t data[6000];
838     const char *msg = "VANC";
839     if( len < 0 || (unsigned)len > sizeof(data) )
840     {
841         x264_log( h, X264_LOG_ERROR, "AVC-Intra SEI is too large (%d)\n", len );
842         return -1;
843     }
844 
845     memset( data, 0xff, len );
846     memcpy( data, avcintra_uuid, sizeof(avcintra_uuid) );
847     memcpy( data+16, msg, strlen(msg) );
848 
849     x264_sei_write( &h->out.bs, data, len, SEI_USER_DATA_UNREGISTERED );
850 
851     return 0;
852 }
853 
854 #define ERROR(...)\
855 {\
856     if( verbose )\
857         x264_log( h, X264_LOG_WARNING, __VA_ARGS__ );\
858     ret = 1;\
859 }
860 
x264_validate_levels(x264_t * h,int verbose)861 int x264_validate_levels( x264_t *h, int verbose )
862 {
863     int ret = 0;
864     int mbs = h->sps->i_mb_width * h->sps->i_mb_height;
865     int dpb = mbs * h->sps->vui.i_max_dec_frame_buffering;
866     int cbp_factor = h->sps->i_profile_idc>=PROFILE_HIGH422 ? 16 :
867                      h->sps->i_profile_idc==PROFILE_HIGH10 ? 12 :
868                      h->sps->i_profile_idc==PROFILE_HIGH ? 5 : 4;
869 
870     const x264_level_t *l = x264_levels;
871     while( l->level_idc != 0 && l->level_idc != h->param.i_level_idc )
872         l++;
873 
874     if( l->frame_size < mbs
875         || l->frame_size*8 < h->sps->i_mb_width * h->sps->i_mb_width
876         || l->frame_size*8 < h->sps->i_mb_height * h->sps->i_mb_height )
877         ERROR( "frame MB size (%dx%d) > level limit (%d)\n",
878                h->sps->i_mb_width, h->sps->i_mb_height, l->frame_size );
879     if( dpb > l->dpb )
880         ERROR( "DPB size (%d frames, %d mbs) > level limit (%d frames, %d mbs)\n",
881                 h->sps->vui.i_max_dec_frame_buffering, dpb, l->dpb / mbs, l->dpb );
882 
883 #define CHECK( name, limit, val ) \
884     if( (val) > (limit) ) \
885         ERROR( name " (%"PRId64") > level limit (%d)\n", (int64_t)(val), (limit) );
886 
887     CHECK( "VBV bitrate", (l->bitrate * cbp_factor) / 4, h->param.rc.i_vbv_max_bitrate );
888     CHECK( "VBV buffer", (l->cpb * cbp_factor) / 4, h->param.rc.i_vbv_buffer_size );
889     CHECK( "MV range", l->mv_range, h->param.analyse.i_mv_range );
890     CHECK( "interlaced", !l->frame_only, h->param.b_interlaced );
891     CHECK( "fake interlaced", !l->frame_only, h->param.b_fake_interlaced );
892 
893     if( h->param.i_fps_den > 0 )
894         CHECK( "MB rate", l->mbps, (int64_t)mbs * h->param.i_fps_num / h->param.i_fps_den );
895 
896     /* TODO check the rest of the limits */
897     return ret;
898 }
899