1 /*
2 * HEVC Parameter Set encoding
3 *
4 * This file is part of FFmpeg.
5 *
6 * FFmpeg is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
10 *
11 * FFmpeg is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with FFmpeg; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19 */
20
21 #include "golomb.h"
22 #include "hevc_ps.h"
23 #include "put_bits.h"
24
write_ptl_layer(PutBitContext * pb,PTLCommon * ptl)25 static void write_ptl_layer(PutBitContext *pb, PTLCommon *ptl)
26 {
27 int i;
28
29 put_bits(pb, 2, ptl->profile_space);
30 put_bits(pb, 1, ptl->tier_flag);
31 put_bits(pb, 5, ptl->profile_idc);
32 for (i = 0; i < 32; i++)
33 put_bits(pb, 1, ptl->profile_compatibility_flag[i]);
34 put_bits(pb, 1, ptl->progressive_source_flag);
35 put_bits(pb, 1, ptl->interlaced_source_flag);
36 put_bits(pb, 1, ptl->non_packed_constraint_flag);
37 put_bits(pb, 1, ptl->frame_only_constraint_flag);
38 put_bits32(pb, 0); // reserved
39 put_bits(pb, 12, 0); // reserved
40 }
41
write_ptl(PutBitContext * pb,PTL * ptl,int max_num_sub_layers)42 static void write_ptl(PutBitContext *pb, PTL *ptl, int max_num_sub_layers)
43 {
44 int i;
45
46 write_ptl_layer(pb, &ptl->general_ptl);
47 put_bits(pb, 8, ptl->general_ptl.level_idc);
48
49 for (i = 0; i < max_num_sub_layers - 1; i++) {
50 put_bits(pb, 1, ptl->sub_layer_profile_present_flag[i]);
51 put_bits(pb, 1, ptl->sub_layer_level_present_flag[i]);
52 }
53
54 if (max_num_sub_layers > 1)
55 for (i = max_num_sub_layers - 1; i < 8; i++)
56 put_bits(pb, 2, 0); // reserved
57
58 for (i = 0; i < max_num_sub_layers - 1; i++) {
59 if (ptl->sub_layer_profile_present_flag[i])
60 write_ptl_layer(pb, &ptl->sub_layer_ptl[i]);
61 if (ptl->sub_layer_level_present_flag[i])
62 put_bits(pb, 8, ptl->sub_layer_ptl[i].level_idc);
63 }
64 }
65
ff_hevc_encode_nal_vps(HEVCVPS * vps,unsigned int id,uint8_t * buf,int buf_size)66 int ff_hevc_encode_nal_vps(HEVCVPS *vps, unsigned int id,
67 uint8_t *buf, int buf_size)
68 {
69 PutBitContext pb;
70 int i, data_size;
71
72 init_put_bits(&pb, buf, buf_size);
73 put_bits(&pb, 4, id);
74 put_bits(&pb, 2, 3); // reserved
75 put_bits(&pb, 6, vps->vps_max_layers - 1);
76 put_bits(&pb, 3, vps->vps_max_sub_layers - 1);
77 put_bits(&pb, 1, vps->vps_temporal_id_nesting_flag);
78 put_bits(&pb, 16, 0xffff); // reserved
79
80 write_ptl(&pb, &vps->ptl, vps->vps_max_sub_layers);
81
82 put_bits(&pb, 1, vps->vps_sub_layer_ordering_info_present_flag);
83 for (i = vps->vps_sub_layer_ordering_info_present_flag ? 0 : vps->vps_max_layers - 1;
84 i < vps->vps_max_sub_layers; i++) {
85 set_ue_golomb(&pb, vps->vps_max_dec_pic_buffering[i] - 1);
86 set_ue_golomb(&pb, vps->vps_num_reorder_pics[i]);
87 set_ue_golomb(&pb, vps->vps_max_latency_increase[i] + 1);
88 }
89
90 put_bits(&pb, 6, vps->vps_max_layer_id);
91 set_ue_golomb(&pb, vps->vps_num_layer_sets - 1);
92
93 if (vps->vps_num_layer_sets > 1) {
94 avpriv_report_missing_feature(NULL, "Writing layer_id_included_flag");
95 return AVERROR_PATCHWELCOME;
96 }
97
98 put_bits(&pb, 1, vps->vps_timing_info_present_flag);
99 if (vps->vps_timing_info_present_flag) {
100 put_bits32(&pb, vps->vps_num_units_in_tick);
101 put_bits32(&pb, vps->vps_time_scale);
102 put_bits(&pb, 1, vps->vps_poc_proportional_to_timing_flag);
103 if (vps->vps_poc_proportional_to_timing_flag)
104 set_ue_golomb(&pb, vps->vps_num_ticks_poc_diff_one - 1);
105
106 set_ue_golomb(&pb, vps->vps_num_hrd_parameters);
107 if (vps->vps_num_hrd_parameters) {
108 avpriv_report_missing_feature(NULL, "Writing HRD parameters");
109 return AVERROR_PATCHWELCOME;
110 }
111 }
112
113 put_bits(&pb, 1, 0); // extension flag
114
115 put_bits(&pb, 1, 1); // stop bit
116 flush_put_bits(&pb);
117
118 data_size = put_bits_count(&pb) / 8;
119
120 return data_size;
121 }
122