]> git.sesse.net Git - x264/blob - encoder/set.c
212bdb013717f61f953377fa59a70ac639a32cd6
[x264] / encoder / set.c
1 /*****************************************************************************
2  * set: h264 encoder (SPS and PPS init and write)
3  *****************************************************************************
4  * Copyright (C) 2003 Laurent Aimar
5  * $Id: set.c,v 1.1 2004/06/03 19:27:08 fenrir Exp $
6  *
7  * Authors: Laurent Aimar <fenrir@via.ecp.fr>
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., 59 Temple Place - Suite 330, Boston, MA  02111, USA.
22  *****************************************************************************/
23
24 #ifdef HAVE_STDINT_H
25 #include <stdint.h>
26 #else
27 #include <inttypes.h>
28 #endif
29 #include <stdlib.h>
30 #include <stdio.h>
31 #include <string.h>
32 #include <stdarg.h>
33 #include <math.h>
34
35 #include "x264.h"
36 #include "common/common.h"
37 #include "common/macroblock.h"
38 #ifndef _MSC_VER
39 #include "config.h"
40 #endif
41
42 static const uint8_t *const x264_cqm_jvt[6] =
43 {
44     x264_cqm_jvt4i, x264_cqm_jvt4p,
45     x264_cqm_jvt4i, x264_cqm_jvt4p,
46     x264_cqm_jvt8i, x264_cqm_jvt8p
47 };
48
49 static void scaling_list_write( bs_t *s, x264_pps_t *pps, int idx )
50 {
51     const int len = idx<4 ? 16 : 64;
52     const int *zigzag = idx<4 ? x264_zigzag_scan4 : x264_zigzag_scan8;
53     const uint8_t *list = pps->scaling_list[idx];
54     const uint8_t *def_list = (idx==CQM_4IC) ? pps->scaling_list[CQM_4IY]
55                             : (idx==CQM_4PC) ? pps->scaling_list[CQM_4PY]
56                             : x264_cqm_jvt[idx];
57     int j;
58     if( memcmp( list, def_list, len ) )
59     {
60         bs_write( s, 1, 1 ); // scaling_list_present_flag
61         for( j = 0; j < len; j++ )
62             bs_write_se( s, list[zigzag[j]] - (j>0 ? list[zigzag[j-1]] : 8) ); // delta
63     }
64     else
65         bs_write( s, 1, 0 ); // scaling_list_present_flag
66 }
67
68 void x264_sps_init( x264_sps_t *sps, int i_id, x264_param_t *param )
69 {
70     sps->i_id = i_id;
71
72     sps->b_qpprime_y_zero_transform_bypass = !param->rc.b_cbr && param->rc.i_qp_constant == 0;
73     if( sps->b_qpprime_y_zero_transform_bypass )
74         sps->i_profile_idc  = PROFILE_HIGH444;
75     else if( param->analyse.b_transform_8x8 || param->i_cqm_preset != X264_CQM_FLAT )
76         sps->i_profile_idc  = PROFILE_HIGH;
77     else if( param->b_cabac || param->i_bframe > 0 )
78         sps->i_profile_idc  = PROFILE_MAIN;
79     else
80         sps->i_profile_idc  = PROFILE_BASELINE;
81     sps->i_level_idc = param->i_level_idc;
82
83     sps->b_constraint_set0  = 0;
84     sps->b_constraint_set1  = 0;
85     sps->b_constraint_set2  = 0;
86
87     sps->i_log2_max_frame_num = 4;  /* at least 4 */
88     while( (1 << sps->i_log2_max_frame_num) <= param->i_keyint_max )
89     {
90         sps->i_log2_max_frame_num++;
91     }
92     sps->i_log2_max_frame_num++;    /* just in case */
93
94     sps->i_poc_type = 0;
95     if( sps->i_poc_type == 0 )
96     {
97         sps->i_log2_max_poc_lsb = sps->i_log2_max_frame_num + 1;    /* max poc = 2*frame_num */
98     }
99     else if( sps->i_poc_type == 1 )
100     {
101         int i;
102
103         /* FIXME */
104         sps->b_delta_pic_order_always_zero = 1;
105         sps->i_offset_for_non_ref_pic = 0;
106         sps->i_offset_for_top_to_bottom_field = 0;
107         sps->i_num_ref_frames_in_poc_cycle = 0;
108
109         for( i = 0; i < sps->i_num_ref_frames_in_poc_cycle; i++ )
110         {
111             sps->i_offset_for_ref_frame[i] = 0;
112         }
113     }
114
115     sps->vui.i_num_reorder_frames = param->b_bframe_pyramid ? 2 : param->i_bframe ? 1 : 0;
116     sps->vui.i_max_dec_frame_buffering =
117     sps->i_num_ref_frames = X264_MIN(16, param->i_frame_reference + sps->vui.i_num_reorder_frames);
118
119     sps->b_gaps_in_frame_num_value_allowed = 0;
120     sps->i_mb_width = ( param->i_width + 15 ) / 16;
121     sps->i_mb_height= ( param->i_height + 15 )/ 16;
122     sps->b_frame_mbs_only = 1;
123     sps->b_mb_adaptive_frame_field = 0;
124     sps->b_direct8x8_inference = 0;
125     if( sps->b_frame_mbs_only == 0 ||
126         !(param->analyse.inter & X264_ANALYSE_PSUB8x8) )
127     {
128         sps->b_direct8x8_inference = 1;
129     }
130
131     if( param->i_width % 16 != 0 || param->i_height % 16 != 0 )
132     {
133         sps->b_crop = 1;
134         sps->crop.i_left    = 0;
135         sps->crop.i_right   = ( 16 - param->i_width % 16)/2;
136         sps->crop.i_top     = 0;
137         sps->crop.i_bottom  = ( 16 - param->i_height % 16)/2;
138     }
139     else
140     {
141         sps->b_crop = 0;
142         sps->crop.i_left    = 0;
143         sps->crop.i_right   = 0;
144         sps->crop.i_top     = 0;
145         sps->crop.i_bottom  = 0;
146     }
147
148     sps->b_vui = 0;
149     sps->vui.b_aspect_ratio_info_present = 0;
150
151     if( param->vui.i_sar_width > 0 && param->vui.i_sar_height > 0 )
152     {
153         sps->vui.b_aspect_ratio_info_present = 1;
154         sps->vui.i_sar_width = param->vui.i_sar_width;
155         sps->vui.i_sar_height= param->vui.i_sar_height;
156     }
157     sps->b_vui |= sps->vui.b_aspect_ratio_info_present;
158
159     if( param->i_fps_num > 0 && param->i_fps_den > 0)
160     {
161         sps->vui.b_timing_info_present = 1;
162         /* The standard is confusing me, but this seems to work best
163            with other encoders */
164         sps->vui.i_num_units_in_tick = param->i_fps_den;
165         sps->vui.i_time_scale = param->i_fps_num;
166         sps->vui.b_fixed_frame_rate = 1;
167     }
168     sps->b_vui |= sps->vui.b_timing_info_present;
169
170     sps->vui.b_bitstream_restriction = param->i_bframe > 0;
171     if( sps->vui.b_bitstream_restriction )
172     {
173         sps->vui.b_motion_vectors_over_pic_boundaries = 1;
174         sps->vui.i_max_bytes_per_pic_denom = 0;
175         sps->vui.i_max_bits_per_mb_denom = 0;
176         sps->vui.i_log2_max_mv_length_horizontal =
177         sps->vui.i_log2_max_mv_length_vertical = (int)(log(param->analyse.i_mv_range*4-1)/log(2)) + 1;
178     }
179     sps->b_vui |= sps->vui.b_bitstream_restriction;
180 }
181
182
183 void x264_sps_write( bs_t *s, x264_sps_t *sps )
184 {
185     bs_write( s, 8, sps->i_profile_idc );
186     bs_write( s, 1, sps->b_constraint_set0 );
187     bs_write( s, 1, sps->b_constraint_set1 );
188     bs_write( s, 1, sps->b_constraint_set2 );
189
190     bs_write( s, 5, 0 );    /* reserved */
191
192     bs_write( s, 8, sps->i_level_idc );
193
194     bs_write_ue( s, sps->i_id );
195
196     if( sps->i_profile_idc >= PROFILE_HIGH )
197     {
198         bs_write_ue( s, 1 ); // chroma_format_idc = 4:2:0
199         bs_write_ue( s, 0 ); // bit_depth_luma_minus8
200         bs_write_ue( s, 0 ); // bit_depth_chroma_minus8
201         bs_write( s, 1, sps->b_qpprime_y_zero_transform_bypass );
202         bs_write( s, 1, 0 ); // seq_scaling_matrix_present_flag
203     }
204
205     bs_write_ue( s, sps->i_log2_max_frame_num - 4 );
206     bs_write_ue( s, sps->i_poc_type );
207     if( sps->i_poc_type == 0 )
208     {
209         bs_write_ue( s, sps->i_log2_max_poc_lsb - 4 );
210     }
211     else if( sps->i_poc_type == 1 )
212     {
213         int i;
214
215         bs_write( s, 1, sps->b_delta_pic_order_always_zero );
216         bs_write_se( s, sps->i_offset_for_non_ref_pic );
217         bs_write_se( s, sps->i_offset_for_top_to_bottom_field );
218         bs_write_ue( s, sps->i_num_ref_frames_in_poc_cycle );
219
220         for( i = 0; i < sps->i_num_ref_frames_in_poc_cycle; i++ )
221         {
222             bs_write_se( s, sps->i_offset_for_ref_frame[i] );
223         }
224     }
225     bs_write_ue( s, sps->i_num_ref_frames );
226     bs_write( s, 1, sps->b_gaps_in_frame_num_value_allowed );
227     bs_write_ue( s, sps->i_mb_width - 1 );
228     bs_write_ue( s, sps->i_mb_height - 1);
229     bs_write( s, 1, sps->b_frame_mbs_only );
230     if( !sps->b_frame_mbs_only )
231     {
232         bs_write( s, 1, sps->b_mb_adaptive_frame_field );
233     }
234     bs_write( s, 1, sps->b_direct8x8_inference );
235
236     bs_write( s, 1, sps->b_crop );
237     if( sps->b_crop )
238     {
239         bs_write_ue( s, sps->crop.i_left );
240         bs_write_ue( s, sps->crop.i_right );
241         bs_write_ue( s, sps->crop.i_top );
242         bs_write_ue( s, sps->crop.i_bottom );
243     }
244
245     bs_write( s, 1, sps->b_vui );
246     if( sps->b_vui )
247     {
248         bs_write1( s, sps->vui.b_aspect_ratio_info_present );
249         if( sps->vui.b_aspect_ratio_info_present )
250         {
251             int i;
252             static const struct { int w, h; int sar; } sar[] =
253             {
254                 { 1,   1, 1 }, { 12, 11, 2 }, { 10, 11, 3 }, { 16, 11, 4 },
255                 { 40, 33, 5 }, { 24, 11, 6 }, { 20, 11, 7 }, { 32, 11, 8 },
256                 { 80, 33, 9 }, { 18, 11, 10}, { 15, 11, 11}, { 64, 33, 12},
257                 { 160,99, 13}, { 0, 0, -1 }
258             };
259             for( i = 0; sar[i].sar != -1; i++ )
260             {
261                 if( sar[i].w == sps->vui.i_sar_width &&
262                     sar[i].h == sps->vui.i_sar_height )
263                     break;
264             }
265             if( sar[i].sar != -1 )
266             {
267                 bs_write( s, 8, sar[i].sar );
268             }
269             else
270             {
271                 bs_write( s, 8, 255);   /* aspect_ratio_idc (extented) */
272                 bs_write( s, 16, sps->vui.i_sar_width );
273                 bs_write( s, 16, sps->vui.i_sar_height );
274             }
275         }
276
277         bs_write1( s, 0 );      /* overscan_info_present_flag */
278
279         bs_write1( s, 0 );      /* video_signal_type_present_flag */
280 #if 0
281         bs_write( s, 3, 5 );    /* unspecified video format */
282         bs_write1( s, 1 );      /* video full range flag */
283         bs_write1( s, 0 );      /* colour description present flag */
284 #endif
285         bs_write1( s, 0 );      /* chroma_loc_info_present_flag */
286
287         bs_write1( s, sps->vui.b_timing_info_present );
288         if( sps->vui.b_timing_info_present )
289         {
290             bs_write( s, 32, sps->vui.i_num_units_in_tick );
291             bs_write( s, 32, sps->vui.i_time_scale );
292             bs_write1( s, sps->vui.b_fixed_frame_rate );
293         }
294
295         bs_write1( s, 0 );      /* nal_hrd_parameters_present_flag */
296         bs_write1( s, 0 );      /* vcl_hrd_parameters_present_flag */
297         bs_write1( s, 0 );      /* pic_struct_present_flag */
298         bs_write1( s, sps->vui.b_bitstream_restriction );
299         if( sps->vui.b_bitstream_restriction )
300         {
301             bs_write1( s, sps->vui.b_motion_vectors_over_pic_boundaries );
302             bs_write_ue( s, sps->vui.i_max_bytes_per_pic_denom );
303             bs_write_ue( s, sps->vui.i_max_bits_per_mb_denom );
304             bs_write_ue( s, sps->vui.i_log2_max_mv_length_horizontal );
305             bs_write_ue( s, sps->vui.i_log2_max_mv_length_vertical );
306             bs_write_ue( s, sps->vui.i_num_reorder_frames );
307             bs_write_ue( s, sps->vui.i_max_dec_frame_buffering );
308         }
309     }
310
311     bs_rbsp_trailing( s );
312 }
313
314 void x264_pps_init( x264_pps_t *pps, int i_id, x264_param_t *param, x264_sps_t *sps )
315 {
316     int i, j;
317
318     pps->i_id = i_id;
319     pps->i_sps_id = sps->i_id;
320     pps->b_cabac = param->b_cabac;
321
322     pps->b_pic_order = 0;
323     pps->i_num_slice_groups = 1;
324
325 #if 0
326     if( pps->i_num_slice_groups > 1 )
327     {
328         pps->i_slice_group_map_type = 0;
329         if( pps->i_slice_group_map_type == 0 )
330         {
331             for( i = 0; i < pps->i_num_slice_groups; i++ )
332             {
333                 pps->i_run_length[i] = 1;
334             }
335         }
336         else if( pps->i_slice_group_map_type == 2 )
337         {
338             for( i = 0; i < pps->i_num_slice_groups; i++ )
339             {
340                 pps->i_top_left[i] = 0;
341                 pps->i_bottom_right[i] = 0;
342             }
343         }
344         else if( pps->i_slice_group_map_type >= 3 &&
345                  pps->i_slice_group_map_type <= 5 )
346         {
347             pps->b_slice_group_change_direction = 0;
348             pps->i_slice_group_change_rate = 0;
349         }
350         else if( pps->i_slice_group_map_type == 6 )
351         {
352             pps->i_pic_size_in_map_units = 1;
353             for( i = 0; i < pps->i_pic_size_in_map_units; i++ )
354             {
355                 pps->i_slice_group_id[i] = 0;
356             }
357         }
358     }
359 #endif
360     pps->i_num_ref_idx_l0_active = 1;
361     pps->i_num_ref_idx_l1_active = 1;
362
363     pps->b_weighted_pred = 0;
364     pps->b_weighted_bipred = param->analyse.b_weighted_bipred ? 2 : 0;
365
366     pps->i_pic_init_qp = 26;
367     pps->i_pic_init_qs = 26;
368
369     pps->i_chroma_qp_index_offset = param->analyse.i_chroma_qp_offset;
370     pps->b_deblocking_filter_control = 1;
371     pps->b_constrained_intra_pred = 0;
372     pps->b_redundant_pic_cnt = 0;
373
374     pps->b_transform_8x8_mode = param->analyse.b_transform_8x8 ? 1 : 0;
375
376     pps->i_cqm_preset = param->i_cqm_preset;
377     switch( pps->i_cqm_preset )
378     {
379     case X264_CQM_FLAT:
380         for( i = 0; i < 6; i++ )
381             pps->scaling_list[i] = x264_cqm_flat16;
382         break;
383     case X264_CQM_JVT:
384         for( i = 0; i < 6; i++ )
385             pps->scaling_list[i] = x264_cqm_jvt[i];
386         break;
387     case X264_CQM_CUSTOM:
388         pps->scaling_list[CQM_4IY] = param->cqm_4iy;
389         pps->scaling_list[CQM_4IC] = param->cqm_4ic;
390         pps->scaling_list[CQM_4PY] = param->cqm_4py;
391         pps->scaling_list[CQM_4PC] = param->cqm_4pc;
392         pps->scaling_list[CQM_8IY+4] = param->cqm_8iy;
393         pps->scaling_list[CQM_8PY+4] = param->cqm_8py;
394         for( i = 0; i < 6; i++ )
395             for( j = 0; j < (i<4?16:64); j++ )
396                 if( pps->scaling_list[i][j] == 0 )
397                     pps->scaling_list[i] = x264_cqm_jvt[i];
398         break;
399     }
400 }
401
402 void x264_pps_write( bs_t *s, x264_pps_t *pps )
403 {
404     bs_write_ue( s, pps->i_id );
405     bs_write_ue( s, pps->i_sps_id );
406
407     bs_write( s, 1, pps->b_cabac );
408     bs_write( s, 1, pps->b_pic_order );
409     bs_write_ue( s, pps->i_num_slice_groups - 1 );
410
411 #if 0
412     if( pps->i_num_slice_groups > 1 )
413     {
414         int i;
415
416         bs_write_ue( s, pps->i_slice_group_map_type );
417         if( pps->i_slice_group_map_type == 0 )
418         {
419             for( i = 0; i < pps->i_num_slice_groups; i++ )
420             {
421                 bs_write_ue( s, pps->i_run_length[i] - 1 );
422             }
423         }
424         else if( pps->i_slice_group_map_type == 2 )
425         {
426             for( i = 0; i < pps->i_num_slice_groups; i++ )
427             {
428                 bs_write_ue( s, pps->i_top_left[i] );
429                 bs_write_ue( s, pps->i_bottom_right[i] );
430             }
431         }
432         else if( pps->i_slice_group_map_type >= 3 &&
433                  pps->i_slice_group_map_type <= 5 )
434         {
435             bs_write( s, 1, pps->b_slice_group_change_direction );
436             bs_write_ue( s, pps->b_slice_group_change_direction - 1 );
437         }
438         else if( pps->i_slice_group_map_type == 6 )
439         {
440             bs_write_ue( s, pps->i_pic_size_in_map_units - 1 );
441             for( i = 0; i < pps->i_pic_size_in_map_units; i++ )
442             {
443                 /* FIXME */
444                 /* bs_write( s, ceil( log2( pps->i_pic_size_in_map_units +1 ) ),
445                  *              pps->i_slice_group_id[i] );
446                  */
447             }
448         }
449     }
450 #endif
451
452     bs_write_ue( s, pps->i_num_ref_idx_l0_active - 1 );
453     bs_write_ue( s, pps->i_num_ref_idx_l1_active - 1 );
454     bs_write( s, 1, pps->b_weighted_pred );
455     bs_write( s, 2, pps->b_weighted_bipred );
456
457     bs_write_se( s, pps->i_pic_init_qp - 26 );
458     bs_write_se( s, pps->i_pic_init_qs - 26 );
459     bs_write_se( s, pps->i_chroma_qp_index_offset );
460
461     bs_write( s, 1, pps->b_deblocking_filter_control );
462     bs_write( s, 1, pps->b_constrained_intra_pred );
463     bs_write( s, 1, pps->b_redundant_pic_cnt );
464
465     if( pps->b_transform_8x8_mode || pps->i_cqm_preset != X264_CQM_FLAT )
466     {
467         bs_write( s, 1, pps->b_transform_8x8_mode );
468         bs_write( s, 1, (pps->i_cqm_preset != X264_CQM_FLAT) );
469         if( pps->i_cqm_preset != X264_CQM_FLAT )
470         {
471             scaling_list_write( s, pps, CQM_4IY );
472             scaling_list_write( s, pps, CQM_4IC );
473             bs_write( s, 1, 0 ); // Cr = Cb
474             scaling_list_write( s, pps, CQM_4PY );
475             scaling_list_write( s, pps, CQM_4PC );
476             bs_write( s, 1, 0 ); // Cr = Cb
477             if( pps->b_transform_8x8_mode )
478             {
479                 scaling_list_write( s, pps, CQM_8IY+4 );
480                 scaling_list_write( s, pps, CQM_8PY+4 );
481             }
482         }
483         bs_write_se( s, pps->i_chroma_qp_index_offset );
484     }
485
486     bs_rbsp_trailing( s );
487 }
488
489 void x264_sei_version_write( bs_t *s )
490 {
491     int i;
492     // random ID number generated according to ISO-11578
493     const uint8_t uuid[16] = {
494         0xdc, 0x45, 0xe9, 0xbd, 0xe6, 0xd9, 0x48, 0xb7,
495         0x96, 0x2c, 0xd8, 0x20, 0xd9, 0x23, 0xee, 0xef
496     };
497     char version[256];
498     int length;
499     sprintf( version, "x264 - core %d%s - H.264/MPEG-4 AVC codec - Copyleft 2005 - http://www.videolan.org/x264.html",
500              X264_BUILD, X264_VERSION );
501     length = strlen(version)+1+16;
502
503     bs_write( s, 8, 0x5 ); // payload_type = user_data_unregistered
504     while( length > 255 )
505         bs_write( s, 8, 255 ), length -= 255;
506     bs_write( s, 8, length ); // payload_size
507
508     for( i = 0; i < 16; i++ )
509         bs_write( s, 8, uuid[i] );
510     for( i = 0; i < length-16; i++ )
511         bs_write( s, 8, version[i] );
512
513     bs_rbsp_trailing( s );
514 }