Debian Codegraph @

gst-libs/gst/codecparsers/gsth265bitwriter.c

raw symbols SCIP
1 /* GStreamer
2 * Copyright (C) 2021 Intel Corporation
3 * Author: He Junyan <[email protected]>
4 *
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Library General Public
7 * License as published by the Free Software Foundation; either
8 * version 2 of the License, or (at your option) any later version.
9 *
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Library General Public License for more details.
14 *
15 * You should have received a copy of the GNU Library General Public
16 * License along with this library; if not, write to the0
17 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
18 * Boston, MA 02110-1301, USA.
19 */
20
21 /**
22 * SECTION:gsth265bitwriter
23 * @title: GstH265BitWriter
24 * @short_description: Functions for writing bitstream headers
25 *
26 * Since: 1.28
27 */
28
29 #ifdef HAVE_CONFIG_H
30 #include "config.h"
31 #endif
32
33 #include "gsth265bitwriter.h"
34 #include "nalutils.h"
35 #include <gst/base/gstbitwriter.h>
36
37 #ifndef GST_DISABLE_GST_DEBUG
38 #define GST_CAT_DEFAULT gst_h265_debug_category_get()
39 static GstDebugCategory *
40 gst_h265_debug_category_get (void)
41 {
42 static gsize cat_gonce = 0;
43
44 if (g_once_init_enter (&cat_gonce)) {
45 GstDebugCategory *cat = NULL;
46
47 GST_DEBUG_CATEGORY_INIT (cat, "bitwriter_h265", 0,
48 "h265 bitwriter library");
49
50 g_once_init_leave (&cat_gonce, (gsize) cat);
51 }
52
53 return (GstDebugCategory *) cat_gonce;
54 }
55 #endif /* GST_DISABLE_GST_DEBUG */
56
57 /******************************** Utils ********************************/
58 #define SIGNED(val) (2 * ABS(val) - ((val) > 0))
59
60 /* Write an unsigned integer Exp-Golomb-coded syntax element. i.e. ue(v) */
61 static gboolean
62 _bs_write_ue (GstBitWriter * bs, guint32 value)
63 {
64 guint32 size_in_bits = 0;
65 guint32 tmp_value = ++value;
66
67 while (tmp_value) {
68 ++size_in_bits;
69 tmp_value >>= 1;
70 }
71 if (size_in_bits > 1
72 && !gst_bit_writer_put_bits_uint32 (bs, 0, size_in_bits - 1))
73 return FALSE;
74 if (!gst_bit_writer_put_bits_uint32 (bs, value, size_in_bits))
75 return FALSE;
76 return TRUE;
77 }
78
79 #define WRITE_BITS_UNCHECK(bw, val, nbits) \
80 (nbits <= 8 ? gst_bit_writer_put_bits_uint8 (bw, val, nbits) : \
81 (nbits <= 16 ? gst_bit_writer_put_bits_uint16 (bw, val, nbits) : \
82 (nbits <= 32 ? gst_bit_writer_put_bits_uint32 (bw, val, nbits) : \
83 FALSE)))
84
85 #define WRITE_BITS(bw, val, nbits) \
86 if (!WRITE_BITS_UNCHECK (bw, val, nbits)) { \
87 g_warning ("Unsupported bit size: %u", nbits); \
88 have_space = FALSE; \
89 goto error; \
90 }
91
92 #define WRITE_UE_UNCHECK(bw, val) _bs_write_ue (bw, val)
93
94 #ifdef WRITE_UE
95 #undef WRITE_UE
96 #endif
97 #define WRITE_UE(bw, val) \
98 if (!(have_space = WRITE_UE_UNCHECK (bw, val))) \
99 goto error; \
100
101 #define WRITE_UE_MAX(bw, val, max) \
102 if ((guint32) val > (max) || !(have_space = WRITE_UE_UNCHECK (bw, val))) \
103 goto error;
104
105 #define WRITE_SE(bw, val) WRITE_UE (bw, SIGNED (val))
106
107 #define WRITE_SE_RANGE(bw, val, min, max) \
108 if (val > max || val < min || \
109 !(have_space = WRITE_UE_UNCHECK (bw, SIGNED (val)))) \
110 goto error;
111
112 #define WRITE_BYTES_UNCHECK(bw, ptr, nbytes) \
113 gst_bit_writer_put_bytes(bw, ptr, nbytes)
114
115 #ifdef WRITE_BYTES
116 #undef WRITE_BYTES
117 #endif
118 #define WRITE_BYTES(bw, ptr, nbytes) \
119 if (!(have_space = WRITE_BYTES_UNCHECK (bw, ptr, nbytes))) \
120 goto error;
121
122 /***************************** End of Utils ****************************/
123
124 #define EXTENDED_SAR 255
125
126 /**** Default scaling_lists according to Table 7-5 and 7-6 *****/
127 /* Table 7-5 */
128 static const guint8 default_scaling_list0[16] = {
129 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16,
130 16, 16, 16, 16
131 };
132
133 /* Combined the values in Table 7-6 to make the calculation easier
134 * Default scaling list of 8x8 and 16x16 matrices for matrixId = 0, 1 and 2
135 * Default scaling list of 32x32 matrix for matrixId = 0
136 */
137 static const guint8 default_scaling_list1[64] = {
138 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 17, 16,
139 17, 16, 17, 18, 17, 18, 18, 17, 18, 21, 19, 20,
140 21, 20, 19, 21, 24, 22, 22, 24, 24, 22, 22, 24,
141 25, 25, 27, 30, 27, 25, 25, 29, 31, 35, 35, 31,
142 29, 36, 41, 44, 41, 36, 47, 54, 54, 47, 65, 70,
143 65, 88, 88, 115
144 };
145
146 /* Combined the values in Table 7-6 to make the calculation easier
147 * Default scaling list of 8x8 and 16x16 matrices for matrixId = 3, 4 and 5
148 * Default scaling list of 32x32 matrix for matrixId = 1
149 */
150 static const guint8 default_scaling_list2[64] = {
151 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 17, 17,
152 17, 17, 17, 18, 18, 18, 18, 18, 18, 20, 20, 20,
153 20, 20, 20, 20, 24, 24, 24, 24, 24, 24, 24, 24,
154 25, 25, 25, 25, 25, 25, 25, 28, 28, 28, 28, 28,
155 28, 33, 33, 33, 33, 33, 41, 41, 41, 41, 54, 54,
156 54, 71, 71, 91
157 };
158
159 static gboolean
160 _h265_bit_writer_profile_tier_level (const GstH265ProfileTierLevel * ptl,
161 guint8 maxNumSubLayersMinus1, GstBitWriter * bw, gboolean * space)
162 {
163 gboolean have_space = TRUE;
164 guint i, j;
165
166 GST_DEBUG ("writing profile_tier_level");
167
168 WRITE_BITS (bw, ptl->profile_space, 2);
169 WRITE_BITS (bw, ptl->tier_flag, 1);
170 WRITE_BITS (bw, ptl->profile_idc, 5);
171
172 for (j = 0; j < 32; j++)
173 WRITE_BITS (bw, ptl->profile_compatibility_flag[j], 1);
174
175 WRITE_BITS (bw, ptl->progressive_source_flag, 1);
176 WRITE_BITS (bw, ptl->interlaced_source_flag, 1);
177 WRITE_BITS (bw, ptl->non_packed_constraint_flag, 1);
178 WRITE_BITS (bw, ptl->frame_only_constraint_flag, 1);
179
180 if (ptl->profile_idc == 4 || ptl->profile_compatibility_flag[4] ||
181 ptl->profile_idc == 5 || ptl->profile_compatibility_flag[5] ||
182 ptl->profile_idc == 6 || ptl->profile_compatibility_flag[6] ||
183 ptl->profile_idc == 7 || ptl->profile_compatibility_flag[7] ||
184 ptl->profile_idc == 8 || ptl->profile_compatibility_flag[8] ||
185 ptl->profile_idc == 9 || ptl->profile_compatibility_flag[9] ||
186 ptl->profile_idc == 10 || ptl->profile_compatibility_flag[10] ||
187 ptl->profile_idc == 11 || ptl->profile_compatibility_flag[11]) {
188 WRITE_BITS (bw, ptl->max_12bit_constraint_flag, 1);
189 WRITE_BITS (bw, ptl->max_10bit_constraint_flag, 1);
190 WRITE_BITS (bw, ptl->max_8bit_constraint_flag, 1);
191 WRITE_BITS (bw, ptl->max_422chroma_constraint_flag, 1);
192 WRITE_BITS (bw, ptl->max_420chroma_constraint_flag, 1);
193 WRITE_BITS (bw, ptl->max_monochrome_constraint_flag, 1);
194 WRITE_BITS (bw, ptl->intra_constraint_flag, 1);
195 WRITE_BITS (bw, ptl->one_picture_only_constraint_flag, 1);
196 WRITE_BITS (bw, ptl->lower_bit_rate_constraint_flag, 1);
197
198 if (ptl->profile_idc == 5 || ptl->profile_compatibility_flag[5] ||
199 ptl->profile_idc == 9 || ptl->profile_compatibility_flag[9] ||
200 ptl->profile_idc == 10 || ptl->profile_compatibility_flag[10] ||
201 ptl->profile_idc == 11 || ptl->profile_compatibility_flag[11]) {
202 WRITE_BITS (bw, ptl->max_14bit_constraint_flag, 1);
203 /* general_reserved_zero_33bits */
204 WRITE_BITS (bw, 0, 32);
205 WRITE_BITS (bw, 0, 1);
206 } else {
207 /* general_reserved_zero_34bits */
208 WRITE_BITS (bw, 0, 32);
209 WRITE_BITS (bw, 0, 2);
210 }
211 } else if (ptl->profile_idc == 2 || ptl->profile_compatibility_flag[2]) {
212 /* general_reserved_zero_7bits */
213 WRITE_BITS (bw, 0, 7);
214 WRITE_BITS (bw, ptl->one_picture_only_constraint_flag, 1);
215 /* general_reserved_zero_35bits */
216 WRITE_BITS (bw, 0, 32);
217 WRITE_BITS (bw, 0, 3);
218 } else {
219 /* general_reserved_zero_43bits */
220 WRITE_BITS (bw, 0, 32);
221 WRITE_BITS (bw, 0, 11);
222 }
223
224 /* general_inbld_flag, just set to 0 */
225 WRITE_BITS (bw, 0, 1);
226
227 WRITE_BITS (bw, ptl->level_idc, 8);
228
229 for (j = 0; j < maxNumSubLayersMinus1; j++) {
230 if (ptl->sub_layer_profile_present_flag[j]) {
231 GST_WARNING ("sub layer profile does not supported now");
232 goto error;
233 }
234 WRITE_BITS (bw, ptl->sub_layer_profile_present_flag[j], 1);
235
236 if (ptl->sub_layer_level_present_flag[j]) {
237 GST_WARNING ("sub layer level does not supported now");
238 goto error;
239 }
240 WRITE_BITS (bw, ptl->sub_layer_level_present_flag[j], 1);
241 }
242
243 if (maxNumSubLayersMinus1 > 0) {
244 for (i = maxNumSubLayersMinus1; i < 8; i++)
245 /* reserved_zero_2bits */
246 WRITE_BITS (bw, 0, 2);
247 }
248
249 /* TODO: Add sub layers profiles. */
250
251 *space = TRUE;
252 return TRUE;
253
254 error:
255 GST_WARNING ("error to write profile_tier_level");
256
257 *space = have_space;
258 return FALSE;
259 }
260
261 static gboolean
262 _h265_bit_writer_sub_layer_hrd_parameters (const GstH265SubLayerHRDParams *
263 sub_hrd, guint8 CpbCnt, guint8 sub_pic_hrd_params_present_flag,
264 GstBitWriter * bw, gboolean * space)
265 {
266 gboolean have_space = TRUE;
267 guint i;
268
269 GST_DEBUG ("writing \"subLayer HRD Parameters\"");
270
271 for (i = 0; i <= CpbCnt; i++) {
272 WRITE_UE_MAX (bw, sub_hrd->bit_rate_value_minus1[i], G_MAXUINT32 - 1);
273 WRITE_UE_MAX (bw, sub_hrd->cpb_size_value_minus1[i], G_MAXUINT32 - 1);
274
275 if (sub_pic_hrd_params_present_flag) {
276 WRITE_UE_MAX (bw, sub_hrd->cpb_size_du_value_minus1[i], G_MAXUINT32 - 1);
277 WRITE_UE_MAX (bw, sub_hrd->bit_rate_du_value_minus1[i], G_MAXUINT32 - 1);
278 }
279
280 WRITE_BITS (bw, sub_hrd->cbr_flag[i], 1);
281 }
282
283 *space = TRUE;
284 return TRUE;
285
286 error:
287 GST_WARNING ("error to write \"subLayer HRD Parameters \"");
288
289 *space = have_space;
290 return FALSE;
291 }
292
293 static gboolean
294 _h265_bit_writer_hrd_parameters (const GstH265HRDParams * hrd,
295 guint8 commonInfPresentFlag, guint8 maxNumSubLayersMinus1,
296 GstBitWriter * bw, gboolean * space)
297 {
298 gboolean have_space = TRUE;
299 guint i;
300
301 GST_DEBUG ("writing \"HRD Parameters\"");
302
303 if (commonInfPresentFlag) {
304 WRITE_BITS (bw, hrd->nal_hrd_parameters_present_flag, 1);
305 WRITE_BITS (bw, hrd->vcl_hrd_parameters_present_flag, 1);
306
307 if (hrd->nal_hrd_parameters_present_flag
308 || hrd->vcl_hrd_parameters_present_flag) {
309 WRITE_BITS (bw, hrd->sub_pic_hrd_params_present_flag, 1);
310
311 if (hrd->sub_pic_hrd_params_present_flag) {
312 WRITE_BITS (bw, hrd->tick_divisor_minus2, 8);
313 WRITE_BITS (bw, hrd->du_cpb_removal_delay_increment_length_minus1, 5);
314 WRITE_BITS (bw, hrd->sub_pic_cpb_params_in_pic_timing_sei_flag, 1);
315 WRITE_BITS (bw, hrd->dpb_output_delay_du_length_minus1, 5);
316 }
317
318 WRITE_BITS (bw, hrd->bit_rate_scale, 4);
319 WRITE_BITS (bw, hrd->cpb_size_scale, 4);
320
321 if (hrd->sub_pic_hrd_params_present_flag)
322 WRITE_BITS (bw, hrd->cpb_size_du_scale, 4);
323
324 WRITE_BITS (bw, hrd->initial_cpb_removal_delay_length_minus1, 5);
325 WRITE_BITS (bw, hrd->au_cpb_removal_delay_length_minus1, 5);
326 WRITE_BITS (bw, hrd->dpb_output_delay_length_minus1, 5);
327 }
328 }
329
330 for (i = 0; i <= maxNumSubLayersMinus1; i++) {
331 WRITE_BITS (bw, hrd->fixed_pic_rate_general_flag[i], 1);
332
333 if (!hrd->fixed_pic_rate_general_flag[i]) {
334 WRITE_BITS (bw, hrd->fixed_pic_rate_within_cvs_flag[i], 1);
335 } else {
336 if (hrd->fixed_pic_rate_within_cvs_flag[i] == 0)
337 goto error;
338 }
339
340 if (hrd->fixed_pic_rate_within_cvs_flag[i]) {
341 WRITE_UE_MAX (bw, hrd->elemental_duration_in_tc_minus1[i], 2047);
342 } else {
343 WRITE_BITS (bw, hrd->low_delay_hrd_flag[i], 1);
344 }
345
346 if (!hrd->low_delay_hrd_flag[i])
347 WRITE_UE_MAX (bw, hrd->cpb_cnt_minus1[i], 31);
348
349 if (hrd->nal_hrd_parameters_present_flag)
350 if (!_h265_bit_writer_sub_layer_hrd_parameters
351 (&hrd->sublayer_hrd_params[i], hrd->cpb_cnt_minus1[i],
352 hrd->sub_pic_hrd_params_present_flag, bw, &have_space))
353 goto error;
354
355 /* TODO: need to separate nal and vcl from hrd_parameters. */
356 if (hrd->vcl_hrd_parameters_present_flag)
357 if (!_h265_bit_writer_sub_layer_hrd_parameters
358 (&hrd->sublayer_hrd_params[i], hrd->cpb_cnt_minus1[i],
359 hrd->sub_pic_hrd_params_present_flag, bw, &have_space))
360 goto error;
361 }
362
363 *space = TRUE;
364 return TRUE;
365
366 error:
367 GST_WARNING ("error to write \"HRD Parameters\"");
368
369 *space = have_space;
370 return FALSE;
371 }
372
373 static gboolean
374 _h265_bit_writer_vps (const GstH265VPS * vps, GstBitWriter * bw,
375 gboolean * space)
376 {
377 gboolean have_space = TRUE;
378 guint i, j;
379
380 GST_DEBUG ("writing VPS");
381
382 WRITE_BITS (bw, vps->id, 4);
383
384 WRITE_BITS (bw, vps->base_layer_internal_flag, 1);
385 WRITE_BITS (bw, vps->base_layer_available_flag, 1);
386
387 WRITE_BITS (bw, vps->max_layers_minus1, 6);
388 WRITE_BITS (bw, vps->max_sub_layers_minus1, 3);
389 WRITE_BITS (bw, vps->temporal_id_nesting_flag, 1);
390
391 /* reserved_0xffff_16bits */
392 WRITE_BITS (bw, 0xffff, 16);
393
394 if (!_h265_bit_writer_profile_tier_level (&vps->profile_tier_level,
395 vps->max_sub_layers_minus1, bw, &have_space))
396 goto error;
397
398 WRITE_BITS (bw, vps->sub_layer_ordering_info_present_flag, 1);
399
400 for (i = (vps->sub_layer_ordering_info_present_flag ? 0 :
401 vps->max_sub_layers_minus1); i <= vps->max_sub_layers_minus1; i++) {
402 WRITE_UE (bw, vps->max_dec_pic_buffering_minus1[i]);
403 WRITE_UE_MAX (bw, vps->max_num_reorder_pics[i],
404 vps->max_dec_pic_buffering_minus1[i]);
405 WRITE_UE_MAX (bw, vps->max_latency_increase_plus1[i], G_MAXUINT32 - 1);
406 }
407
408 /* max_layer_id should be <63, but only support 1 layer now. */
409 if (vps->max_layer_id > 1) {
410 GST_WARNING ("multi layers are not supported now");
411 goto error;
412 }
413
414 WRITE_BITS (bw, vps->max_layer_id, 6);
415
416 if (vps->num_layer_sets_minus1 >= 1) {
417 GST_WARNING ("layer set is not supported now");
418 goto error;
419 }
420 WRITE_UE_MAX (bw, vps->num_layer_sets_minus1, 1023);
421
422 /* TODO: support multi-layer. */
423 for (i = 1; i <= vps->num_layer_sets_minus1; i++) {
424 for (j = 0; j <= vps->max_layer_id; j++) {
425 /* layer_id_included_flag[i][j] */
426 WRITE_BITS (bw, 0, 1);
427 }
428 }
429
430 WRITE_BITS (bw, vps->timing_info_present_flag, 1);
431 if (vps->timing_info_present_flag) {
432 WRITE_BITS (bw, vps->num_units_in_tick, 32);
433 WRITE_BITS (bw, vps->time_scale, 32);
434 WRITE_BITS (bw, vps->poc_proportional_to_timing_flag, 1);
435
436 if (vps->poc_proportional_to_timing_flag)
437 WRITE_UE_MAX (bw, vps->num_ticks_poc_diff_one_minus1, G_MAXUINT32 - 1);
438
439 /* TODO: VPS can have multiple hrd parameters, and therefore hrd_params
440 * should be an array (like Garray). Just support 1 hdr parameter now.
441 */
442 if (vps->num_hrd_parameters > 1) {
443 GST_WARNING ("HRD parameters > 1 is not supported now");
444 goto error;
445 }
446 WRITE_UE_MAX (bw, vps->num_hrd_parameters, vps->num_layer_sets_minus1 + 1);
447
448 if (vps->num_hrd_parameters) {
449 WRITE_UE_MAX (bw, vps->hrd_layer_set_idx, vps->num_layer_sets_minus1);
450
451 if (!_h265_bit_writer_hrd_parameters (&vps->hrd_params,
452 vps->cprms_present_flag, vps->max_sub_layers_minus1,
453 bw, &have_space))
454 goto error;
455 }
456
457 }
458
459 if (vps->vps_extension) {
460 GST_WARNING ("vps extension is not supported now");
461 goto error;
462 }
463 WRITE_BITS (bw, 0, 1);
464
465 *space = TRUE;
466 return TRUE;
467
468 error:
469 GST_WARNING ("failed to write VPS");
470
471 *space = have_space;
472 return FALSE;
473 }
474
475 /**
476 * gst_h265_bit_writer_vps:
477 * @vps: the vps of #GstH265VPS to write
478 * @start_code: whether adding the nal start code
479 * @data: (out): the bit stream generated by the sps
480 * @size: (inout): the size in bytes of the input and output
481 *
482 * Generating the according h265 bit stream by providing the vps.
483 *
484 * Returns: a #GstH265BitWriterResult
485 *
486 * Since: 1.22
487 **/
488 GstH265BitWriterResult
489 gst_h265_bit_writer_vps (const GstH265VPS * vps, gboolean start_code,
490 guint8 * data, guint * size)
491 {
492 gboolean have_space = TRUE;
493 GstBitWriter bw;
494
495 g_return_val_if_fail (vps != NULL, GST_H265_BIT_WRITER_ERROR);
496 g_return_val_if_fail (data != NULL, GST_H265_BIT_WRITER_ERROR);
497 g_return_val_if_fail (size != NULL, GST_H265_BIT_WRITER_ERROR);
498 g_return_val_if_fail (*size > 0, GST_H265_BIT_WRITER_ERROR);
499
500 gst_bit_writer_init_with_data (&bw, data, *size, FALSE);
501
502 if (start_code)
503 WRITE_BITS (&bw, 0x00000001, 32);
504
505 /* NAL unit header */
506 /* forbidden_zero_bit */
507 WRITE_BITS (&bw, 0, 1);
508 /* nal_unit_type */
509 WRITE_BITS (&bw, GST_H265_NAL_VPS, 6);
510 /* nuh_layer_id, only support 0 now */
511 WRITE_BITS (&bw, 0, 6);
512 /* nuh_temporal_id_plus1, only support 1 now */
513 WRITE_BITS (&bw, 1, 3);
514
515 if (!_h265_bit_writer_vps (vps, &bw, &have_space))
516 goto error;
517
518 /* Add trailings. */
519 WRITE_BITS (&bw, 1, 1);
520 if (!gst_bit_writer_align_bytes (&bw, 0)) {
521 have_space = FALSE;
522 goto error;
523 }
524
525 *size = gst_bit_writer_get_size (&bw) / 8;
526 gst_bit_writer_reset (&bw);
527
528 return GST_H265_BIT_WRITER_OK;
529
530 error:
531 gst_bit_writer_reset (&bw);
532 *size = 0;
533
534 if (!have_space)
535 return GST_H265_BIT_WRITER_NO_MORE_SPACE;
536 return GST_H265_BIT_WRITER_INVALID_DATA;
537 }
538
539 static gboolean
540 _get_scaling_list_params (const GstH265ScalingList * dest_scaling_list,
541 guint8 sizeId, guint8 matrixId, const guint8 ** sl, guint8 * size,
542 gint16 * scaling_list_dc_coef_minus8)
543 {
544 switch (sizeId) {
545 case GST_H265_QUANT_MATIX_4X4:
546 *sl = dest_scaling_list->scaling_lists_4x4[matrixId];
547 if (size)
548 *size = 16;
549 break;
550 case GST_H265_QUANT_MATIX_8X8:
551 *sl = dest_scaling_list->scaling_lists_8x8[matrixId];
552 if (size)
553 *size = 64;
554 break;
555 case GST_H265_QUANT_MATIX_16X16:
556 *sl = dest_scaling_list->scaling_lists_16x16[matrixId];
557 if (size)
558 *size = 64;
559 if (scaling_list_dc_coef_minus8)
560 *scaling_list_dc_coef_minus8 =
561 dest_scaling_list->scaling_list_dc_coef_minus8_16x16[matrixId];
562 break;
563 case GST_H265_QUANT_MATIX_32X32:
564 *sl = dest_scaling_list->scaling_lists_32x32[matrixId];
565 if (size)
566 *size = 64;
567 if (scaling_list_dc_coef_minus8)
568 *scaling_list_dc_coef_minus8 =
569 dest_scaling_list->scaling_list_dc_coef_minus8_32x32[matrixId];
570 break;
571 default:
572 g_assert_not_reached ();
573 return FALSE;
574 }
575
576 return TRUE;
577 }
578
579 static const guint8 *
580 _get_default_scaling_lists (GstH265QuantMatrixSize sizeId, guint8 matrixId)
581 {
582 const guint8 *sl;
583
584 switch (sizeId) {
585 case GST_H265_QUANT_MATIX_4X4:
586 sl = default_scaling_list0;
587 break;
588
589 case GST_H265_QUANT_MATIX_8X8:
590 case GST_H265_QUANT_MATIX_16X16:
591 if (matrixId <= 2) {
592 sl = default_scaling_list1;
593 } else {
594 sl = default_scaling_list2;
595 }
596 break;
597
598 case GST_H265_QUANT_MATIX_32X32:
599 if (matrixId == 0) {
600 sl = default_scaling_list1;
601 } else {
602 sl = default_scaling_list2;
603 }
604 break;
605
606 default:
607 g_assert_not_reached ();
608 return NULL;
609 }
610
611 return sl;
612 }
613
614 static gboolean
615 _compare_scaling_list_matrix (GstH265QuantMatrixSize sizeId,
616 const guint8 * sl0, const guint8 * sl1,
617 gint16 dc_coef_minus8_0, gint16 dc_coef_minus8_1)
618 {
619 guint size = sizeId == GST_H265_QUANT_MATIX_4X4 ? 16 : 64;
620
621 if (memcmp (sl0, sl1, size * sizeof (guint8)))
622 return FALSE;
623
624 if (sizeId <= GST_H265_QUANT_MATIX_8X8)
625 return TRUE;
626
627 return dc_coef_minus8_0 == dc_coef_minus8_1;
628 }
629
630 static gboolean
631 _h265_bit_writer_scaling_lists (const GstH265ScalingList * src_scaling_list,
632 GstBitWriter * bw, gboolean * space)
633 {
634 gboolean have_space = TRUE;
635 GstH265QuantMatrixSize sizeId;
636 guint8 matrixId;
637 guint8 scaling_list_pred_mode_flag = 0;
638 guint8 size, i, j;
639
640 GST_DEBUG ("writing scaling lists");
641
642 for (sizeId = 0; sizeId <= GST_H265_QUANT_MATIX_32X32; sizeId++) {
643 for (matrixId = 0;
644 matrixId < ((sizeId == GST_H265_QUANT_MATIX_32X32) ? 2 : 6);
645 matrixId++) {
646 gint16 scaling_list_dc_coef_minus8 = 8;
647 const guint8 *sl;
648 const guint8 *default_sl;
649 guint8 nextCoef;
650 guint8 scaling_list_pred_matrix_id_delta;
651
652 if (!_get_scaling_list_params (src_scaling_list, sizeId, matrixId,
653 &sl, &size, &scaling_list_dc_coef_minus8))
654 goto error;
655
656 /* Check whether it is the default matrix. */
657 default_sl = _get_default_scaling_lists (sizeId, matrixId);
658 if (_compare_scaling_list_matrix (sizeId, sl, default_sl,
659 scaling_list_dc_coef_minus8, 8)) {
660 scaling_list_pred_mode_flag = 0;
661 WRITE_BITS (bw, scaling_list_pred_mode_flag, 1);
662 scaling_list_pred_matrix_id_delta = 0;
663 WRITE_UE_MAX (bw, scaling_list_pred_matrix_id_delta, matrixId);
664 continue;
665 }
666
667 /* If some previous matrix is the same, just ref it. */
668 scaling_list_pred_matrix_id_delta = 0;
669 for (j = 0; j < matrixId; j++) {
670 gboolean ret GST_UNUSED_ASSERT;
671 guint8 size2;
672 const guint8 *sl2;
673 gint16 scaling_list_dc_coef_minus8_2 = 8;
674
675 ret = _get_scaling_list_params (src_scaling_list, sizeId, j,
676 &sl2, &size2, &scaling_list_dc_coef_minus8_2);
677 g_assert (ret);
678 g_assert (size == size2);
679
680 if (_compare_scaling_list_matrix (sizeId, sl, sl2,
681 scaling_list_dc_coef_minus8, scaling_list_dc_coef_minus8_2)) {
682 scaling_list_pred_matrix_id_delta = matrixId - j;
683 break;
684 }
685 }
686
687 if (scaling_list_pred_matrix_id_delta > 0) {
688 scaling_list_pred_mode_flag = 0;
689 WRITE_BITS (bw, scaling_list_pred_mode_flag, 1);
690 WRITE_UE_MAX (bw, scaling_list_pred_matrix_id_delta, matrixId);
691 continue;
692 }
693
694 /* Just explicitly signal all matrix coef. */
695 scaling_list_pred_mode_flag = 1;
696 WRITE_BITS (bw, scaling_list_pred_mode_flag, 1);
697
698 nextCoef = 8;
699
700 if (sizeId > 1) {
701 WRITE_SE_RANGE (bw, scaling_list_dc_coef_minus8, -7, 247);
702 nextCoef = scaling_list_dc_coef_minus8 + 8;
703 }
704
705 for (i = 0; i < size; i++) {
706 gint coef_val = sl[i] - nextCoef;
707 nextCoef = sl[i];
708
709 if (coef_val > 127) {
710 coef_val = coef_val - 256;
711 }
712 if (coef_val < -128) {
713 coef_val = coef_val + 256;
714 }
715
716 WRITE_SE_RANGE (bw, coef_val, -128, 127);
717 }
718 }
719 }
720
721 *space = TRUE;
722 return TRUE;
723
724 error:
725 GST_WARNING ("error to write scaling lists");
726
727 *space = have_space;
728 return FALSE;
729 }
730
731 static gboolean
732 _h265_bit_writer_short_term_ref_pic_set (const GstH265ShortTermRefPicSet *
733 stRPS, guint8 stRpsIdx, const GstH265SPS * sps,
734 GstBitWriter * bw, gboolean * space)
735 {
736 gboolean have_space = TRUE;
737 gint32 prev;
738 gint i = 0;
739
740 GST_DEBUG ("writing \"ShortTermRefPicSetParameter\"");
741
742 if (stRPS->inter_ref_pic_set_prediction_flag) {
743 /* TODO */
744 GST_WARNING ("inter_ref_pic_set_prediction_flag mode not supported");
745 goto error;
746 }
747
748 if (stRpsIdx != 0)
749 WRITE_BITS (bw, stRPS->inter_ref_pic_set_prediction_flag, 1);
750
751 if (stRPS->NumNegativePics + stRPS->NumPositivePics != stRPS->NumDeltaPocs)
752 goto error;
753
754 /* 7-49 */
755 WRITE_UE_MAX (bw, stRPS->NumNegativePics,
756 sps->max_dec_pic_buffering_minus1[sps->max_sub_layers_minus1]);
757 /* 7-50 */
758 WRITE_UE_MAX (bw, stRPS->NumPositivePics,
759 (sps->max_dec_pic_buffering_minus1[sps->max_sub_layers_minus1] -
760 stRPS->NumNegativePics));
761
762 prev = 0;
763 for (i = 0; i < stRPS->NumNegativePics; i++) {
764 WRITE_UE_MAX (bw, prev - stRPS->DeltaPocS0[i] - 1, 32767);
765 prev = stRPS->DeltaPocS0[i];
766 /* 7-51 */
767 WRITE_BITS (bw, stRPS->UsedByCurrPicS0[i], 1);
768 }
769
770 prev = 0;
771 for (i = 0; i < stRPS->NumPositivePics; i++) {
772 WRITE_UE_MAX (bw, stRPS->DeltaPocS1[i] - prev - 1, 32767);
773 prev = stRPS->DeltaPocS1[i];
774 /* 7-52 */
775 WRITE_BITS (bw, stRPS->UsedByCurrPicS1[i], 1);
776 }
777
778 *space = TRUE;
779 return TRUE;
780
781 error:
782 GST_WARNING ("error to write \"ShortTermRefPicSet Parameters\"");
783
784 *space = have_space;
785 return FALSE;
786 }
787
788 static gboolean
789 _h265_bit_writer_vui_parameters (const GstH265SPS * sps,
790 GstBitWriter * bw, gboolean * space)
791 {
792 gboolean have_space = TRUE;
793 const GstH265VUIParams *vui = &sps->vui_params;
794
795 GST_DEBUG ("writing \"VUI Parameters\"");
796
797 WRITE_BITS (bw, vui->aspect_ratio_info_present_flag, 1);
798 if (vui->aspect_ratio_info_present_flag) {
799 WRITE_BITS (bw, vui->aspect_ratio_idc, 8);
800 if (vui->aspect_ratio_idc == EXTENDED_SAR) {
801 WRITE_BITS (bw, vui->sar_width, 16);
802 WRITE_BITS (bw, vui->sar_height, 16);
803 }
804 }
805
806 WRITE_BITS (bw, vui->overscan_info_present_flag, 1);
807 if (vui->overscan_info_present_flag)
808 WRITE_BITS (bw, vui->overscan_appropriate_flag, 1);
809
810 WRITE_BITS (bw, vui->video_signal_type_present_flag, 1);
811 if (vui->video_signal_type_present_flag) {
812 WRITE_BITS (bw, vui->video_format, 3);
813 WRITE_BITS (bw, vui->video_full_range_flag, 1);
814 WRITE_BITS (bw, vui->colour_description_present_flag, 1);
815 if (vui->colour_description_present_flag) {
816 WRITE_BITS (bw, vui->colour_primaries, 8);
817 WRITE_BITS (bw, vui->transfer_characteristics, 8);
818 WRITE_BITS (bw, vui->matrix_coefficients, 8);
819 }
820 }
821
822 WRITE_BITS (bw, vui->chroma_loc_info_present_flag, 1);
823 if (vui->chroma_loc_info_present_flag) {
824 WRITE_UE_MAX (bw, vui->chroma_sample_loc_type_top_field, 5);
825 WRITE_UE_MAX (bw, vui->chroma_sample_loc_type_bottom_field, 5);
826 }
827
828 WRITE_BITS (bw, vui->neutral_chroma_indication_flag, 1);
829 WRITE_BITS (bw, vui->field_seq_flag, 1);
830 WRITE_BITS (bw, vui->frame_field_info_present_flag, 1);
831
832 WRITE_BITS (bw, vui->default_display_window_flag, 1);
833 if (vui->default_display_window_flag) {
834 WRITE_UE (bw, vui->def_disp_win_left_offset);
835 WRITE_UE (bw, vui->def_disp_win_right_offset);
836 WRITE_UE (bw, vui->def_disp_win_top_offset);
837 WRITE_UE (bw, vui->def_disp_win_bottom_offset);
838 }
839
840 WRITE_BITS (bw, vui->timing_info_present_flag, 1);
841 if (vui->timing_info_present_flag) {
842 if (vui->num_units_in_tick == 0)
843 GST_WARNING ("num_units_in_tick = 0 (incompliant to H.265 E.2.1).");
844 WRITE_BITS (bw, vui->num_units_in_tick, 32);
845
846 if (vui->time_scale == 0)
847 GST_WARNING ("time_scale = 0 (incompliant to H.265 E.2.1).");
848 WRITE_BITS (bw, vui->time_scale, 32);
849
850 WRITE_BITS (bw, vui->poc_proportional_to_timing_flag, 1);
851 if (vui->poc_proportional_to_timing_flag)
852 WRITE_UE_MAX (bw, vui->num_ticks_poc_diff_one_minus1, G_MAXUINT32 - 1);
853
854 WRITE_BITS (bw, vui->hrd_parameters_present_flag, 1);
855 if (vui->hrd_parameters_present_flag)
856 if (!_h265_bit_writer_hrd_parameters (&vui->hrd_params, 1,
857 sps->max_sub_layers_minus1, bw, &have_space))
858 goto error;
859 }
860
861 WRITE_BITS (bw, vui->bitstream_restriction_flag, 1);
862 if (vui->bitstream_restriction_flag) {
863 WRITE_BITS (bw, vui->tiles_fixed_structure_flag, 1);
864 WRITE_BITS (bw, vui->motion_vectors_over_pic_boundaries_flag, 1);
865 WRITE_BITS (bw, vui->restricted_ref_pic_lists_flag, 1);
866 WRITE_UE_MAX (bw, vui->min_spatial_segmentation_idc, 4096);
867 WRITE_UE_MAX (bw, vui->max_bytes_per_pic_denom, 16);
868 WRITE_UE_MAX (bw, vui->max_bits_per_min_cu_denom, 16);
869 WRITE_UE_MAX (bw, vui->log2_max_mv_length_horizontal, 16);
870 WRITE_UE_MAX (bw, vui->log2_max_mv_length_vertical, 15);
871 }
872
873 *space = TRUE;
874 return TRUE;
875
876 error:
877 GST_WARNING ("error to write \"VUI Parameters\"");
878
879 *space = have_space;
880 return FALSE;
881 }
882
883 static gboolean
884 _h265_bit_writer_sps (const GstH265SPS * sps,
885 GstBitWriter * bw, gboolean * space)
886 {
887 gboolean have_space = TRUE;
888 guint i;
889
890 GST_DEBUG ("writing SPS");
891
892 WRITE_BITS (bw, sps->vps->id, 4);
893
894 WRITE_BITS (bw, sps->max_sub_layers_minus1, 3);
895 WRITE_BITS (bw, sps->temporal_id_nesting_flag, 1);
896
897 if (!_h265_bit_writer_profile_tier_level (&sps->profile_tier_level,
898 sps->max_sub_layers_minus1, bw, &have_space))
899 goto error;
900
901 WRITE_UE_MAX (bw, sps->id, GST_H265_MAX_SPS_COUNT - 1);
902
903 WRITE_UE_MAX (bw, sps->chroma_format_idc, 3);
904 if (sps->chroma_format_idc == 3)
905 WRITE_BITS (bw, sps->separate_colour_plane_flag, 1);
906
907 if (sps->pic_width_in_luma_samples < 1)
908 goto error;
909 WRITE_UE_MAX (bw, sps->pic_width_in_luma_samples, 16888);
910
911 if (sps->pic_height_in_luma_samples < 1)
912 goto error;
913 WRITE_UE_MAX (bw, sps->pic_height_in_luma_samples, 16888);
914
915 WRITE_BITS (bw, sps->conformance_window_flag, 1);
916 if (sps->conformance_window_flag) {
917 WRITE_UE (bw, sps->conf_win_left_offset);
918 WRITE_UE (bw, sps->conf_win_right_offset);
919 WRITE_UE (bw, sps->conf_win_top_offset);
920 WRITE_UE (bw, sps->conf_win_bottom_offset);
921 }
922
923 WRITE_UE_MAX (bw, sps->bit_depth_luma_minus8, 6);
924 WRITE_UE_MAX (bw, sps->bit_depth_chroma_minus8, 6);
925 WRITE_UE_MAX (bw, sps->log2_max_pic_order_cnt_lsb_minus4, 12);
926
927 WRITE_BITS (bw, sps->sub_layer_ordering_info_present_flag, 1);
928 for (i = (sps->sub_layer_ordering_info_present_flag ? 0 :
929 sps->max_sub_layers_minus1); i <= sps->max_sub_layers_minus1; i++) {
930 WRITE_UE_MAX (bw, sps->max_dec_pic_buffering_minus1[i], 16);
931 WRITE_UE_MAX (bw, sps->max_num_reorder_pics[i],
932 sps->max_dec_pic_buffering_minus1[i]);
933 WRITE_UE (bw, sps->max_latency_increase_plus1[i]);
934 }
935
936 /* The limits are calculted based on the profile_tier_level constraint
937 * in Annex-A: CtbLog2SizeY = 4 to 6 */
938 WRITE_UE_MAX (bw, sps->log2_min_luma_coding_block_size_minus3, 3);
939 WRITE_UE_MAX (bw, sps->log2_diff_max_min_luma_coding_block_size, 6);
940 WRITE_UE_MAX (bw, sps->log2_min_transform_block_size_minus2, 3);
941 WRITE_UE_MAX (bw, sps->log2_diff_max_min_transform_block_size, 3);
942 WRITE_UE_MAX (bw, sps->max_transform_hierarchy_depth_inter, 4);
943 WRITE_UE_MAX (bw, sps->max_transform_hierarchy_depth_intra, 4);
944
945 WRITE_BITS (bw, sps->scaling_list_enabled_flag, 1);
946 if (sps->scaling_list_enabled_flag) {
947 WRITE_BITS (bw, sps->scaling_list_data_present_flag, 1);
948
949 if (sps->scaling_list_data_present_flag)
950 if (!_h265_bit_writer_scaling_lists (&sps->scaling_list, bw, &have_space))
951 goto error;
952 }
953
954 WRITE_BITS (bw, sps->amp_enabled_flag, 1);
955 WRITE_BITS (bw, sps->sample_adaptive_offset_enabled_flag, 1);
956 WRITE_BITS (bw, sps->pcm_enabled_flag, 1);
957
958 if (sps->pcm_enabled_flag) {
959 WRITE_BITS (bw, sps->pcm_sample_bit_depth_luma_minus1, 4);
960 WRITE_BITS (bw, sps->pcm_sample_bit_depth_chroma_minus1, 4);
961 WRITE_UE_MAX (bw, sps->log2_min_pcm_luma_coding_block_size_minus3, 2);
962 WRITE_UE_MAX (bw, sps->log2_diff_max_min_pcm_luma_coding_block_size, 2);
963 WRITE_BITS (bw, sps->pcm_loop_filter_disabled_flag, 1);
964 }
965
966 WRITE_UE_MAX (bw, sps->num_short_term_ref_pic_sets, 64);
967 for (i = 0; i < sps->num_short_term_ref_pic_sets; i++) {
968 if (!_h265_bit_writer_short_term_ref_pic_set
969 (&sps->short_term_ref_pic_set[i], i, sps, bw, &have_space))
970 goto error;
971 }
972
973 WRITE_BITS (bw, sps->long_term_ref_pics_present_flag, 1);
974 if (sps->long_term_ref_pics_present_flag) {
975 WRITE_UE_MAX (bw, sps->num_long_term_ref_pics_sps, 32);
976 for (i = 0; i < sps->num_long_term_ref_pics_sps; i++) {
977 WRITE_BITS (bw, sps->lt_ref_pic_poc_lsb_sps[i],
978 sps->log2_max_pic_order_cnt_lsb_minus4 + 4);
979 WRITE_BITS (bw, sps->used_by_curr_pic_lt_sps_flag[i], 1);
980 }
981 }
982
983 WRITE_BITS (bw, sps->temporal_mvp_enabled_flag, 1);
984 WRITE_BITS (bw, sps->strong_intra_smoothing_enabled_flag, 1);
985 WRITE_BITS (bw, sps->vui_parameters_present_flag, 1);
986
987 if (sps->vui_parameters_present_flag) {
988 if (!_h265_bit_writer_vui_parameters (sps, bw, &have_space))
989 goto error;
990 }
991
992 WRITE_BITS (bw, sps->sps_extension_flag, 1);
993
994 if (sps->sps_extension_flag) {
995 WRITE_BITS (bw, sps->sps_range_extension_flag, 1);
996 WRITE_BITS (bw, sps->sps_multilayer_extension_flag, 1);
997 WRITE_BITS (bw, sps->sps_3d_extension_flag, 1);
998 WRITE_BITS (bw, sps->sps_scc_extension_flag, 1);
999 WRITE_BITS (bw, sps->sps_extension_4bits, 4);
1000 }
1001
1002 if (sps->sps_range_extension_flag) {
1003 WRITE_BITS (bw,
1004 sps->sps_extension_params.transform_skip_rotation_enabled_flag, 1);
1005 WRITE_BITS (bw,
1006 sps->sps_extension_params.transform_skip_context_enabled_flag, 1);
1007 WRITE_BITS (bw, sps->sps_extension_params.implicit_rdpcm_enabled_flag, 1);
1008 WRITE_BITS (bw, sps->sps_extension_params.explicit_rdpcm_enabled_flag, 1);
1009 WRITE_BITS (bw,
1010 sps->sps_extension_params.extended_precision_processing_flag, 1);
1011 WRITE_BITS (bw, sps->sps_extension_params.intra_smoothing_disabled_flag, 1);
1012 WRITE_BITS (bw,
1013 sps->sps_extension_params.high_precision_offsets_enabled_flag, 1);
1014 WRITE_BITS (bw,
1015 sps->sps_extension_params.persistent_rice_adaptation_enabled_flag, 1);
1016 WRITE_BITS (bw,
1017 sps->sps_extension_params.cabac_bypass_alignment_enabled_flag, 1);
1018 }
1019
1020 if (sps->sps_multilayer_extension_flag) {
1021 GST_WARNING ("do not support multilayer extension");
1022 goto error;
1023 }
1024 if (sps->sps_3d_extension_flag) {
1025 GST_WARNING ("do not support 3d extension");
1026 goto error;
1027 }
1028
1029 if (sps->sps_scc_extension_flag) {
1030 const GstH265SPSSccExtensionParams *scc_params =
1031 &sps->sps_scc_extension_params;
1032
1033 WRITE_BITS (bw, scc_params->sps_curr_pic_ref_enabled_flag, 1);
1034 WRITE_BITS (bw, scc_params->palette_mode_enabled_flag, 1);
1035 if (scc_params->palette_mode_enabled_flag) {
1036 WRITE_UE_MAX (bw, scc_params->palette_max_size, 64);
1037 WRITE_UE_MAX (bw, scc_params->delta_palette_max_predictor_size,
1038 128 - scc_params->palette_max_size);
1039
1040 WRITE_BITS (bw,
1041 scc_params->sps_palette_predictor_initializers_present_flag, 1);
1042 if (scc_params->sps_palette_predictor_initializers_present_flag) {
1043 guint comp;
1044 WRITE_UE_MAX (bw,
1045 scc_params->sps_num_palette_predictor_initializer_minus1,
1046 scc_params->palette_max_size +
1047 scc_params->delta_palette_max_predictor_size - 1);
1048
1049 for (comp = 0; comp < (sps->chroma_format_idc == 0 ? 1 : 3); comp++) {
1050 guint num_bits;
1051 guint num =
1052 scc_params->sps_num_palette_predictor_initializer_minus1 + 1;
1053
1054 num_bits = (comp == 0 ? sps->bit_depth_luma_minus8 + 8 :
1055 sps->bit_depth_chroma_minus8 + 8);
1056 for (i = 0; i < num; i++)
1057 WRITE_BITS (bw,
1058 scc_params->sps_palette_predictor_initializer[comp][i],
1059 num_bits);
1060 }
1061 }
1062 }
1063
1064 WRITE_BITS (bw, scc_params->motion_vector_resolution_control_idc, 2);
1065 WRITE_BITS (bw, scc_params->intra_boundary_filtering_disabled_flag, 1);
1066 }
1067
1068 *space = TRUE;
1069 return TRUE;
1070
1071 error:
1072 GST_WARNING ("failed to write SPS");
1073
1074 *space = have_space;
1075 return FALSE;
1076 }
1077
1078 /**
1079 * gst_h265_bit_writer_sps:
1080 * @sps: the sps of #GstH265SPS to write
1081 * @start_code: whether adding the nal start code
1082 * @data: (out): the bit stream generated by the sps
1083 * @size: (inout): the size in bytes of the input and output
1084 *
1085 * Generating the according h265 bit stream by providing the sps.
1086 *
1087 * Returns: a #GstH265BitWriterResult
1088 *
1089 * Since: 1.22
1090 **/
1091 GstH265BitWriterResult
1092 gst_h265_bit_writer_sps (const GstH265SPS * sps, gboolean start_code,
1093 guint8 * data, guint * size)
1094 {
1095 gboolean have_space = TRUE;
1096 GstBitWriter bw;
1097
1098 g_return_val_if_fail (sps != NULL, GST_H265_BIT_WRITER_ERROR);
1099 g_return_val_if_fail (sps->vps != NULL, GST_H265_BIT_WRITER_ERROR);
1100 g_return_val_if_fail (data != NULL, GST_H265_BIT_WRITER_ERROR);
1101 g_return_val_if_fail (size != NULL, GST_H265_BIT_WRITER_ERROR);
1102 g_return_val_if_fail (*size > 0, GST_H265_BIT_WRITER_ERROR);
1103
1104 gst_bit_writer_init_with_data (&bw, data, *size, FALSE);
1105
1106 if (start_code)
1107 WRITE_BITS (&bw, 0x00000001, 32);
1108
1109 /* NAL unit header */
1110 /* forbidden_zero_bit */
1111 WRITE_BITS (&bw, 0, 1);
1112 /* nal_unit_type */
1113 WRITE_BITS (&bw, GST_H265_NAL_SPS, 6);
1114 /* nuh_layer_id, only support 0 now */
1115 WRITE_BITS (&bw, 0, 6);
1116 /* nuh_temporal_id_plus1, only support 1 now */
1117 WRITE_BITS (&bw, 1, 3);
1118
1119 if (!_h265_bit_writer_sps (sps, &bw, &have_space))
1120 goto error;
1121
1122 /* Add trailings. */
1123 WRITE_BITS (&bw, 1, 1);
1124 if (!gst_bit_writer_align_bytes (&bw, 0)) {
1125 have_space = FALSE;
1126 goto error;
1127 }
1128
1129 *size = gst_bit_writer_get_size (&bw) / 8;
1130 gst_bit_writer_reset (&bw);
1131
1132 return GST_H265_BIT_WRITER_OK;
1133
1134 error:
1135 gst_bit_writer_reset (&bw);
1136 *size = 0;
1137
1138 if (!have_space)
1139 return GST_H265_BIT_WRITER_NO_MORE_SPACE;
1140 return GST_H265_BIT_WRITER_INVALID_DATA;
1141 }
1142
1143 static gboolean
1144 _h265_bit_writer_pps (const GstH265PPS * pps, GstBitWriter * bw,
1145 gboolean * space)
1146 {
1147 gboolean have_space = TRUE;
1148
1149 GST_DEBUG ("writing PPS");
1150
1151 WRITE_UE_MAX (bw, pps->id, GST_H265_MAX_PPS_COUNT - 1);
1152 WRITE_UE_MAX (bw, pps->sps->id, GST_H265_MAX_SPS_COUNT - 1);
1153
1154 WRITE_BITS (bw, pps->dependent_slice_segments_enabled_flag, 1);
1155 WRITE_BITS (bw, pps->output_flag_present_flag, 1);
1156 WRITE_BITS (bw, pps->num_extra_slice_header_bits, 3);
1157 WRITE_BITS (bw, pps->sign_data_hiding_enabled_flag, 1);
1158 WRITE_BITS (bw, pps->cabac_init_present_flag, 1);
1159
1160 WRITE_UE_MAX (bw, pps->num_ref_idx_l0_default_active_minus1, 14);
1161 WRITE_UE_MAX (bw, pps->num_ref_idx_l1_default_active_minus1, 14);
1162 WRITE_SE_RANGE (bw, pps->init_qp_minus26,
1163 -(26 + 6 * pps->sps->bit_depth_luma_minus8), 25);
1164
1165 WRITE_BITS (bw, pps->constrained_intra_pred_flag, 1);
1166 WRITE_BITS (bw, pps->transform_skip_enabled_flag, 1);
1167
1168 WRITE_BITS (bw, pps->cu_qp_delta_enabled_flag, 1);
1169 if (pps->cu_qp_delta_enabled_flag)
1170 WRITE_UE_MAX (bw, pps->diff_cu_qp_delta_depth,
1171 pps->sps->log2_diff_max_min_luma_coding_block_size);
1172
1173 WRITE_SE_RANGE (bw, pps->cb_qp_offset, -12, 12);
1174 WRITE_SE_RANGE (bw, pps->cr_qp_offset, -12, 12);
1175
1176 WRITE_BITS (bw, pps->slice_chroma_qp_offsets_present_flag, 1);
1177 WRITE_BITS (bw, pps->weighted_pred_flag, 1);
1178 WRITE_BITS (bw, pps->weighted_bipred_flag, 1);
1179 WRITE_BITS (bw, pps->transquant_bypass_enabled_flag, 1);
1180 WRITE_BITS (bw, pps->tiles_enabled_flag, 1);
1181 WRITE_BITS (bw, pps->entropy_coding_sync_enabled_flag, 1);
1182
1183 if (pps->tiles_enabled_flag) {
1184 if (pps->num_tile_columns_minus1 + 1 >
1185 G_N_ELEMENTS (pps->column_width_minus1)) {
1186 GST_WARNING ("Invalid \"num_tile_columns_minus1\" %d",
1187 pps->num_tile_columns_minus1);
1188 goto error;
1189 }
1190
1191 if (pps->num_tile_rows_minus1 + 1 > G_N_ELEMENTS (pps->row_height_minus1)) {
1192 GST_WARNING ("Invalid \"num_tile_rows_minus1\" %d",
1193 pps->num_tile_rows_minus1);
1194 goto error;
1195 }
1196
1197 WRITE_UE_MAX (bw, pps->num_tile_columns_minus1, pps->PicWidthInCtbsY - 1);
1198 WRITE_UE_MAX (bw, pps->num_tile_rows_minus1, pps->PicHeightInCtbsY - 1);
1199
1200 WRITE_BITS (bw, pps->uniform_spacing_flag, 1);
1201
1202 /* 6.5.1, 6-4, 6-5, 7.4.3.3.1 */
1203 if (!pps->uniform_spacing_flag) {
1204 guint i;
1205
1206 for (i = 0; i < pps->num_tile_columns_minus1; i++)
1207 WRITE_UE (bw, pps->column_width_minus1[i]);
1208
1209 for (i = 0; i < pps->num_tile_rows_minus1; i++)
1210 WRITE_UE (bw, pps->row_height_minus1[i]);
1211 }
1212 WRITE_BITS (bw, pps->loop_filter_across_tiles_enabled_flag, 1);
1213 }
1214
1215 WRITE_BITS (bw, pps->loop_filter_across_slices_enabled_flag, 1);
1216
1217 WRITE_BITS (bw, pps->deblocking_filter_control_present_flag, 1);
1218 if (pps->deblocking_filter_control_present_flag) {
1219 WRITE_BITS (bw, pps->deblocking_filter_override_enabled_flag, 1);
1220
1221 WRITE_BITS (bw, pps->deblocking_filter_disabled_flag, 1);
1222 if (!pps->deblocking_filter_disabled_flag) {
1223 WRITE_SE_RANGE (bw, pps->beta_offset_div2, -6, 6);
1224 WRITE_SE_RANGE (bw, pps->tc_offset_div2, -6, +6);
1225 }
1226 }
1227
1228 WRITE_BITS (bw, pps->scaling_list_data_present_flag, 1);
1229 if (pps->scaling_list_data_present_flag)
1230 if (!_h265_bit_writer_scaling_lists (&pps->scaling_list, bw, &have_space))
1231 goto error;
1232
1233 WRITE_BITS (bw, pps->lists_modification_present_flag, 1);
1234 WRITE_UE_MAX (bw, pps->log2_parallel_merge_level_minus2, 4);
1235
1236 /* TODO: slice_segment_header */
1237 if (pps->slice_segment_header_extension_present_flag) {
1238 GST_WARNING
1239 ("slice_segment_header_extension_present_flag is not supported");
1240 goto error;
1241 }
1242 WRITE_BITS (bw, pps->slice_segment_header_extension_present_flag, 1);
1243
1244 WRITE_BITS (bw, pps->pps_extension_flag, 1);
1245
1246 if (pps->pps_extension_flag) {
1247 WRITE_BITS (bw, pps->pps_range_extension_flag, 1);
1248 WRITE_BITS (bw, pps->pps_multilayer_extension_flag, 1);
1249 WRITE_BITS (bw, pps->pps_3d_extension_flag, 1);
1250 WRITE_BITS (bw, pps->pps_scc_extension_flag, 1);
1251 WRITE_BITS (bw, pps->pps_extension_4bits, 4);
1252 }
1253
1254 if (pps->pps_range_extension_flag) {
1255 guint i;
1256 guint32 MaxBitDepthY, MaxBitDepthC;
1257
1258 if (pps->transform_skip_enabled_flag)
1259 WRITE_UE (bw,
1260 pps->pps_extension_params.log2_max_transform_skip_block_size_minus2);
1261
1262 WRITE_BITS (bw,
1263 pps->pps_extension_params.cross_component_prediction_enabled_flag, 1);
1264 WRITE_BITS (bw,
1265 pps->pps_extension_params.chroma_qp_offset_list_enabled_flag, 1);
1266
1267 if (pps->pps_extension_params.chroma_qp_offset_list_enabled_flag) {
1268 WRITE_UE_MAX (bw,
1269 pps->pps_extension_params.diff_cu_chroma_qp_offset_depth,
1270 pps->sps->log2_diff_max_min_luma_coding_block_size);
1271
1272 WRITE_UE_MAX (bw,
1273 pps->pps_extension_params.chroma_qp_offset_list_len_minus1, 5);
1274 for (i = 0;
1275 i <= pps->pps_extension_params.chroma_qp_offset_list_len_minus1;
1276 i++) {
1277 WRITE_SE_RANGE (bw, pps->pps_extension_params.cb_qp_offset_list[i],
1278 -12, 12);
1279 WRITE_SE_RANGE (bw, pps->pps_extension_params.cr_qp_offset_list[i],
1280 -12, 12);
1281 }
1282 }
1283
1284 MaxBitDepthY = pps->sps->bit_depth_luma_minus8 > 2 ?
1285 pps->sps->bit_depth_luma_minus8 - 2 : 0;
1286 MaxBitDepthC = pps->sps->bit_depth_chroma_minus8 > 2 ?
1287 pps->sps->bit_depth_chroma_minus8 - 2 : 0;
1288 WRITE_UE_MAX (bw, pps->pps_extension_params.log2_sao_offset_scale_luma,
1289 MaxBitDepthY);
1290 WRITE_UE_MAX (bw, pps->pps_extension_params.log2_sao_offset_scale_chroma,
1291 MaxBitDepthC);
1292 }
1293
1294 if (pps->pps_multilayer_extension_flag) {
1295 GST_WARNING ("do not support multilayer extension");
1296 goto error;
1297 }
1298
1299 if (pps->pps_3d_extension_flag) {
1300 GST_WARNING ("do not support 3d extension");
1301 goto error;
1302 }
1303
1304 if (pps->pps_scc_extension_flag) {
1305 const GstH265PPSSccExtensionParams *pps_scc =
1306 &pps->pps_scc_extension_params;
1307
1308 WRITE_BITS (bw, pps_scc->pps_curr_pic_ref_enabled_flag, 1);
1309 WRITE_BITS (bw,
1310 pps_scc->residual_adaptive_colour_transform_enabled_flag, 1);
1311 if (pps_scc->residual_adaptive_colour_transform_enabled_flag) {
1312 WRITE_BITS (bw, pps_scc->pps_slice_act_qp_offsets_present_flag, 1);
1313 WRITE_SE_RANGE (bw, (gint8) pps_scc->pps_act_y_qp_offset_plus5, -7, 17);
1314 WRITE_SE_RANGE (bw, (gint8) pps_scc->pps_act_cb_qp_offset_plus5, -7, 17);
1315 WRITE_SE_RANGE (bw, (gint8) pps_scc->pps_act_cr_qp_offset_plus3, -9, 15);
1316 }
1317
1318 WRITE_BITS (bw,
1319 pps_scc->pps_palette_predictor_initializers_present_flag, 1);
1320 if (pps_scc->pps_palette_predictor_initializers_present_flag) {
1321 guint i;
1322
1323 WRITE_UE_MAX (bw,
1324 pps_scc->pps_num_palette_predictor_initializer,
1325 pps->sps->sps_scc_extension_params.palette_max_size +
1326 pps->sps->sps_scc_extension_params.delta_palette_max_predictor_size);
1327 if (pps_scc->pps_num_palette_predictor_initializer > 0) {
1328 guint comp;
1329
1330 WRITE_BITS (bw, pps_scc->monochrome_palette_flag, 1);
1331 /* It is a requirement of bitstream conformance that the value of
1332 luma_bit_depth_entry_minus8 shall be equal to the value of
1333 bit_depth_luma_minus8 */
1334 WRITE_UE_MAX (bw, pps_scc->luma_bit_depth_entry_minus8,
1335 pps->sps->bit_depth_luma_minus8);
1336 if (!pps_scc->monochrome_palette_flag) {
1337 /* It is a requirement of bitstream conformance that the value
1338 of chroma_bit_depth_entry_minus8 shall be equal to the value
1339 of bit_depth_chroma_minus8. */
1340 WRITE_UE_MAX (bw, pps_scc->chroma_bit_depth_entry_minus8,
1341 pps->sps->bit_depth_chroma_minus8);
1342 }
1343
1344 for (comp = 0; comp < (pps_scc->monochrome_palette_flag ? 1 : 3);
1345 comp++) {
1346 guint num_bits;
1347 guint num = pps_scc->pps_num_palette_predictor_initializer;
1348
1349 num_bits = (comp == 0 ?
1350 pps_scc->luma_bit_depth_entry_minus8 + 8 :
1351 pps_scc->chroma_bit_depth_entry_minus8 + 8);
1352 for (i = 0; i < num; i++)
1353 WRITE_BITS (bw,
1354 pps_scc->pps_palette_predictor_initializer[comp][i], num_bits);
1355 }
1356 }
1357 }
1358 }
1359
1360 return TRUE;
1361
1362 error:
1363 GST_WARNING ("failed to write PPS");
1364 return FALSE;
1365 }
1366
1367 /**
1368 * gst_h265_bit_writer_pps:
1369 * @pps: the pps of #GstH265PPS to write
1370 * @start_code: whether adding the nal start code
1371 * @data: (out): the bit stream generated by the pps
1372 * @size: (inout): the size in bytes of the input and output
1373 *
1374 * Generating the according h265 bit stream by providing the pps.
1375 *
1376 * Returns: a #GstH265BitWriterResult
1377 *
1378 * Since: 1.22
1379 **/
1380 GstH265BitWriterResult
1381 gst_h265_bit_writer_pps (const GstH265PPS * pps, gboolean start_code,
1382 guint8 * data, guint * size)
1383 {
1384 gboolean have_space = TRUE;
1385 GstBitWriter bw;
1386
1387 g_return_val_if_fail (pps != NULL, GST_H265_BIT_WRITER_ERROR);
1388 g_return_val_if_fail (pps->sps != NULL, GST_H265_BIT_WRITER_ERROR);
1389 g_return_val_if_fail (data != NULL, GST_H265_BIT_WRITER_ERROR);
1390 g_return_val_if_fail (size != NULL, GST_H265_BIT_WRITER_ERROR);
1391 g_return_val_if_fail (*size > 0, GST_H265_BIT_WRITER_ERROR);
1392
1393 gst_bit_writer_init_with_data (&bw, data, *size, FALSE);
1394
1395 if (start_code)
1396 WRITE_BITS (&bw, 0x00000001, 32);
1397
1398 /* NAL unit header */
1399 /* forbidden_zero_bit */
1400 WRITE_BITS (&bw, 0, 1);
1401 /* nal_unit_type */
1402 WRITE_BITS (&bw, GST_H265_NAL_PPS, 6);
1403 /* nuh_layer_id, only support 0 now */
1404 WRITE_BITS (&bw, 0, 6);
1405 /* nuh_temporal_id_plus1, only support 1 now */
1406 WRITE_BITS (&bw, 1, 3);
1407
1408 if (!_h265_bit_writer_pps (pps, &bw, &have_space))
1409 goto error;
1410
1411 /* Add trailings. */
1412 WRITE_BITS (&bw, 1, 1);
1413 if (!gst_bit_writer_align_bytes (&bw, 0)) {
1414 have_space = FALSE;
1415 goto error;
1416 }
1417
1418 *size = gst_bit_writer_get_size (&bw) / 8;
1419 gst_bit_writer_reset (&bw);
1420
1421 return GST_H265_BIT_WRITER_OK;
1422
1423 error:
1424 gst_bit_writer_reset (&bw);
1425 *size = 0;
1426
1427 if (!have_space)
1428 return GST_H265_BIT_WRITER_NO_MORE_SPACE;
1429 return GST_H265_BIT_WRITER_INVALID_DATA;
1430 }
1431
1432 static gboolean
1433 _h265_slice_bit_writer_ref_pic_list_modification (const GstH265SliceHdr *
1434 slice, gint NumPocTotalCurr, GstBitWriter * bw, gboolean * space)
1435 {
1436 gboolean have_space = TRUE;
1437 guint i;
1438 const GstH265RefPicListModification *rpl_mod =
1439 &slice->ref_pic_list_modification;
1440 const guint n = gst_util_ceil_log2 (NumPocTotalCurr);
1441
1442 WRITE_BITS (bw, rpl_mod->ref_pic_list_modification_flag_l0, 1);
1443
1444 if (rpl_mod->ref_pic_list_modification_flag_l0) {
1445 for (i = 0; i <= slice->num_ref_idx_l0_active_minus1; i++) {
1446 /* 7.4.7.2 list_entry_l0 */
1447 WRITE_BITS (bw, rpl_mod->list_entry_l0[i], n);
1448 }
1449 }
1450
1451 if (GST_H265_IS_B_SLICE (slice)) {
1452 WRITE_BITS (bw, rpl_mod->ref_pic_list_modification_flag_l1, 1);
1453
1454 if (rpl_mod->ref_pic_list_modification_flag_l1)
1455 for (i = 0; i <= slice->num_ref_idx_l1_active_minus1; i++) {
1456 WRITE_BITS (bw, rpl_mod->list_entry_l1[i], n);
1457 }
1458 }
1459
1460 *space = TRUE;
1461 return TRUE;
1462
1463 error:
1464 GST_WARNING ("error to write \"Reference picture list modifications\"");
1465
1466 *space = have_space;
1467 return FALSE;
1468 }
1469
1470 static gboolean
1471 _h265_slice_bit_writer_pred_weight_table (const GstH265SliceHdr * slice,
1472 GstBitWriter * bw, gboolean * space)
1473 {
1474 gboolean have_space = TRUE;
1475 const GstH265PredWeightTable *p;
1476 const GstH265PPS *pps = slice->pps;
1477 const GstH265SPS *sps = pps->sps;
1478 gint i, j;
1479
1480 GST_DEBUG ("writing \"Prediction weight table\"");
1481
1482 p = &slice->pred_weight_table;
1483
1484 WRITE_UE_MAX (bw, p->luma_log2_weight_denom, 7);
1485
1486 if (sps->chroma_format_idc != 0) {
1487 WRITE_SE_RANGE (bw, p->delta_chroma_log2_weight_denom,
1488 (0 - p->luma_log2_weight_denom), (7 - p->luma_log2_weight_denom));
1489 }
1490
1491 for (i = 0; i <= slice->num_ref_idx_l0_active_minus1; i++)
1492 WRITE_BITS (bw, p->luma_weight_l0_flag[i], 1);
1493
1494 if (sps->chroma_format_idc != 0)
1495 for (i = 0; i <= slice->num_ref_idx_l0_active_minus1; i++)
1496 WRITE_BITS (bw, p->chroma_weight_l0_flag[i], 1);
1497
1498 for (i = 0; i <= slice->num_ref_idx_l0_active_minus1; i++) {
1499 if (p->luma_weight_l0_flag[i]) {
1500 WRITE_SE_RANGE (bw, p->delta_luma_weight_l0[i], -128, 127);
1501 WRITE_SE_RANGE (bw, p->luma_offset_l0[i], -128, 127);
1502 }
1503 if (p->chroma_weight_l0_flag[i])
1504 for (j = 0; j < 2; j++) {
1505 WRITE_SE_RANGE (bw, p->delta_chroma_weight_l0[i][j], -128, 127);
1506 WRITE_SE_RANGE (bw, p->delta_chroma_offset_l0[i][j], -512, 511);
1507 }
1508 }
1509
1510 if (GST_H265_IS_B_SLICE (slice)) {
1511 for (i = 0; i <= slice->num_ref_idx_l1_active_minus1; i++)
1512 WRITE_BITS (bw, p->luma_weight_l1_flag[i], 1);
1513
1514 if (sps->chroma_format_idc != 0)
1515 for (i = 0; i <= slice->num_ref_idx_l1_active_minus1; i++)
1516 WRITE_BITS (bw, p->chroma_weight_l1_flag[i], 1);
1517
1518 for (i = 0; i <= slice->num_ref_idx_l1_active_minus1; i++) {
1519 if (p->luma_weight_l1_flag[i]) {
1520 WRITE_SE_RANGE (bw, p->delta_luma_weight_l1[i], -128, 127);
1521 WRITE_SE_RANGE (bw, p->luma_offset_l1[i], -128, 127);
1522 }
1523 if (p->chroma_weight_l1_flag[i])
1524 for (j = 0; j < 2; j++) {
1525 WRITE_SE_RANGE (bw, p->delta_chroma_weight_l1[i][j], -128, 127);
1526 WRITE_SE_RANGE (bw, p->delta_chroma_offset_l1[i][j], -512, 511);
1527 }
1528 }
1529 }
1530
1531 *space = TRUE;
1532 return TRUE;
1533
1534 error:
1535 GST_WARNING ("error to write \"Prediction weight table\"");
1536
1537 *space = have_space;
1538 return FALSE;
1539 }
1540
1541 static gboolean
1542 _h265_bit_writer_slice_header (const GstH265SliceHdr * slice,
1543 guint32 nal_type, GstBitWriter * bw, gboolean * space)
1544 {
1545 gboolean have_space = TRUE;
1546 guint i;
1547 const GstH265SPS *sps = slice->pps->sps;
1548 const GstH265PPSSccExtensionParams *pps_scc_extension_params =
1549 &slice->pps->pps_scc_extension_params;
1550 const GstH265PPSExtensionParams *pps_extension_params =
1551 &slice->pps->pps_extension_params;
1552
1553 GST_DEBUG ("writing slice header");
1554
1555 WRITE_BITS (bw, slice->first_slice_segment_in_pic_flag, 1);
1556
1557 if (GST_H265_IS_NAL_TYPE_IRAP (nal_type))
1558 WRITE_BITS (bw, slice->no_output_of_prior_pics_flag, 1);
1559
1560 WRITE_UE_MAX (bw, slice->pps->id, GST_H265_MAX_PPS_COUNT - 1);
1561
1562 if (!slice->first_slice_segment_in_pic_flag) {
1563 guint32 PicSizeInCtbsY;
1564 guint32 PicWidthInCtbsY;
1565 guint32 PicHeightInCtbsY;
1566 guint32 CtbSizeY, MinCbLog2SizeY, CtbLog2SizeY;
1567 guint n;
1568
1569 /* We can not directly use slice->pps->PicWidthInCtbsY/PicHeightInCtbsY,
1570 they are calculated value when parsing but may not have value here. */
1571 MinCbLog2SizeY = sps->log2_min_luma_coding_block_size_minus3 + 3;
1572 CtbLog2SizeY =
1573 MinCbLog2SizeY + sps->log2_diff_max_min_luma_coding_block_size;
1574 CtbSizeY = 1 << CtbLog2SizeY;
1575 PicHeightInCtbsY = div_ceil (sps->pic_height_in_luma_samples, CtbSizeY);
1576 PicWidthInCtbsY = div_ceil (sps->pic_width_in_luma_samples, CtbSizeY);
1577 PicSizeInCtbsY = PicWidthInCtbsY * PicHeightInCtbsY;
1578
1579 n = gst_util_ceil_log2 (PicSizeInCtbsY);
1580
1581 if (slice->pps->dependent_slice_segments_enabled_flag)
1582 WRITE_BITS (bw, slice->dependent_slice_segment_flag, 1);
1583 /* 7.4.7.1 slice_segment_address parsing */
1584 WRITE_BITS (bw, slice->segment_address, n);
1585 }
1586
1587 if (!slice->dependent_slice_segment_flag) {
1588 for (i = 0; i < slice->pps->num_extra_slice_header_bits; i++) {
1589 /* slice_reserved_flag */
1590 WRITE_BITS (bw, 0, 1);
1591 }
1592
1593 WRITE_UE_MAX (bw, slice->type, 63);
1594
1595 if (slice->pps->output_flag_present_flag)
1596 WRITE_BITS (bw, slice->pic_output_flag, 1);
1597
1598 if (sps->separate_colour_plane_flag == 1)
1599 WRITE_BITS (bw, slice->colour_plane_id, 2);
1600
1601 if (!GST_H265_IS_NAL_TYPE_IDR (nal_type)) {
1602 WRITE_BITS (bw, slice->pic_order_cnt_lsb,
1603 (sps->log2_max_pic_order_cnt_lsb_minus4 + 4));
1604
1605 WRITE_BITS (bw, slice->short_term_ref_pic_set_sps_flag, 1);
1606 if (!slice->short_term_ref_pic_set_sps_flag) {
1607 if (!_h265_bit_writer_short_term_ref_pic_set
1608 (&slice->short_term_ref_pic_sets, sps->num_short_term_ref_pic_sets,
1609 slice->pps->sps, bw, &have_space))
1610 goto error;
1611 } else if (sps->num_short_term_ref_pic_sets > 1) {
1612 const guint n = gst_util_ceil_log2 (sps->num_short_term_ref_pic_sets);
1613
1614 if (slice->short_term_ref_pic_set_idx >
1615 sps->num_short_term_ref_pic_sets - 1)
1616 goto error;
1617
1618 /* 7.4.7.1 short_term_ref_pic_set_idx */
1619 WRITE_BITS (bw, slice->short_term_ref_pic_set_idx, n);
1620 }
1621
1622 if (sps->long_term_ref_pics_present_flag) {
1623 guint32 limit;
1624
1625 if (sps->num_long_term_ref_pics_sps > 0)
1626 WRITE_UE_MAX (bw, slice->num_long_term_sps,
1627 sps->num_long_term_ref_pics_sps);
1628
1629 WRITE_UE_MAX (bw, slice->num_long_term_pics, 16);
1630 limit = slice->num_long_term_sps + slice->num_long_term_pics;
1631 for (i = 0; i < limit; i++) {
1632 if (i < slice->num_long_term_sps) {
1633 if (sps->num_long_term_ref_pics_sps > 1) {
1634 /* 7.4.7.1 lt_idx_sps */
1635 const guint n =
1636 gst_util_ceil_log2 (sps->num_long_term_ref_pics_sps);
1637 WRITE_BITS (bw, slice->lt_idx_sps[i], n);
1638 }
1639 } else {
1640 WRITE_BITS (bw, slice->poc_lsb_lt[i],
1641 (sps->log2_max_pic_order_cnt_lsb_minus4 + 4));
1642 WRITE_BITS (bw, slice->used_by_curr_pic_lt_flag[i], 1);
1643 }
1644
1645 WRITE_BITS (bw, slice->delta_poc_msb_present_flag[i], 1);
1646 if (slice->delta_poc_msb_present_flag[i])
1647 WRITE_UE (bw, slice->delta_poc_msb_cycle_lt[i]);
1648 }
1649 }
1650
1651 if (sps->temporal_mvp_enabled_flag)
1652 WRITE_BITS (bw, slice->temporal_mvp_enabled_flag, 1);
1653 }
1654
1655 if (sps->sample_adaptive_offset_enabled_flag) {
1656 gboolean ChromaArrayType =
1657 sps->separate_colour_plane_flag == 0 ? sps->chroma_format_idc : 0;
1658
1659 WRITE_BITS (bw, slice->sao_luma_flag, 1);
1660 if (ChromaArrayType)
1661 WRITE_BITS (bw, slice->sao_chroma_flag, 1);
1662 }
1663
1664 if (GST_H265_IS_B_SLICE (slice) || GST_H265_IS_P_SLICE (slice)) {
1665 WRITE_BITS (bw, slice->num_ref_idx_active_override_flag, 1);
1666
1667 if (slice->num_ref_idx_active_override_flag) {
1668 WRITE_UE_MAX (bw, slice->num_ref_idx_l0_active_minus1, 14);
1669 if (GST_H265_IS_B_SLICE (slice))
1670 WRITE_UE_MAX (bw, slice->num_ref_idx_l1_active_minus1, 14);
1671 }
1672
1673 if (slice->pps->lists_modification_present_flag
1674 && slice->NumPocTotalCurr > 1) {
1675 if (!_h265_slice_bit_writer_ref_pic_list_modification (slice,
1676 slice->NumPocTotalCurr, bw, &have_space))
1677 goto error;
1678 }
1679
1680 if (GST_H265_IS_B_SLICE (slice))
1681 WRITE_BITS (bw, slice->mvd_l1_zero_flag, 1);
1682
1683 if (slice->pps->cabac_init_present_flag)
1684 WRITE_BITS (bw, slice->cabac_init_flag, 1);
1685
1686 if (slice->temporal_mvp_enabled_flag) {
1687 if (GST_H265_IS_B_SLICE (slice))
1688 WRITE_BITS (bw, slice->collocated_from_l0_flag, 1);
1689
1690 if ((slice->collocated_from_l0_flag
1691 && slice->num_ref_idx_l0_active_minus1 > 0)
1692 || (!slice->collocated_from_l0_flag
1693 && slice->num_ref_idx_l1_active_minus1 > 0)) {
1694 if ((GST_H265_IS_P_SLICE (slice))
1695 || ((GST_H265_IS_B_SLICE (slice))
1696 && (slice->collocated_from_l0_flag))) {
1697 WRITE_UE_MAX (bw, slice->collocated_ref_idx,
1698 slice->num_ref_idx_l0_active_minus1);
1699 } else if ((GST_H265_IS_B_SLICE (slice))
1700 && (!slice->collocated_from_l0_flag)) {
1701 WRITE_UE_MAX (bw, slice->collocated_ref_idx,
1702 slice->num_ref_idx_l1_active_minus1);
1703 }
1704 }
1705 }
1706
1707 if ((slice->pps->weighted_pred_flag && GST_H265_IS_P_SLICE (slice)) ||
1708 (slice->pps->weighted_bipred_flag && GST_H265_IS_B_SLICE (slice)))
1709 if (!_h265_slice_bit_writer_pred_weight_table (slice, bw, &have_space))
1710 goto error;
1711
1712 WRITE_UE_MAX (bw, slice->five_minus_max_num_merge_cand, 4);
1713
1714 if (sps->sps_scc_extension_params.motion_vector_resolution_control_idc
1715 == 2)
1716 WRITE_BITS (bw, slice->use_integer_mv_flag, 1);
1717 }
1718
1719 WRITE_SE_RANGE (bw, slice->qp_delta, -87, 77);
1720 if (slice->pps->slice_chroma_qp_offsets_present_flag) {
1721 WRITE_SE_RANGE (bw, slice->cb_qp_offset, -12, 12);
1722 WRITE_SE_RANGE (bw, slice->cr_qp_offset, -12, 12);
1723 }
1724
1725 if (pps_scc_extension_params->pps_slice_act_qp_offsets_present_flag) {
1726 WRITE_SE_RANGE (bw, slice->slice_act_y_qp_offset, -12, 12);
1727 WRITE_SE_RANGE (bw, slice->slice_act_cb_qp_offset, -12, 12);
1728 WRITE_SE_RANGE (bw, slice->slice_act_cr_qp_offset, -12, 12);
1729 }
1730
1731 if (pps_extension_params->chroma_qp_offset_list_enabled_flag)
1732 WRITE_BITS (bw, slice->cu_chroma_qp_offset_enabled_flag, 1);
1733
1734 if (slice->pps->deblocking_filter_override_enabled_flag)
1735 WRITE_BITS (bw, slice->deblocking_filter_override_flag, 1);
1736
1737 if (slice->deblocking_filter_override_flag) {
1738 WRITE_BITS (bw, slice->deblocking_filter_disabled_flag, 1);
1739
1740 if (!slice->deblocking_filter_disabled_flag) {
1741 WRITE_SE_RANGE (bw, slice->beta_offset_div2, -6, 6);
1742 WRITE_SE_RANGE (bw, slice->tc_offset_div2, -6, 6);
1743 }
1744 }
1745
1746 if (slice->pps->loop_filter_across_slices_enabled_flag &&
1747 (slice->sao_luma_flag || slice->sao_chroma_flag ||
1748 !slice->deblocking_filter_disabled_flag))
1749 WRITE_BITS (bw, slice->loop_filter_across_slices_enabled_flag, 1);
1750 }
1751
1752 if (slice->pps->tiles_enabled_flag
1753 || slice->pps->entropy_coding_sync_enabled_flag) {
1754 guint32 offset_max;
1755
1756 if (!slice->pps->tiles_enabled_flag
1757 && slice->pps->entropy_coding_sync_enabled_flag) {
1758 offset_max = slice->pps->PicHeightInCtbsY - 1;
1759 } else if (slice->pps->tiles_enabled_flag
1760 && !slice->pps->entropy_coding_sync_enabled_flag) {
1761 offset_max =
1762 (slice->pps->num_tile_columns_minus1 +
1763 1) * (slice->pps->num_tile_rows_minus1 + 1) - 1;
1764 } else {
1765 offset_max =
1766 (slice->pps->num_tile_columns_minus1 +
1767 1) * slice->pps->PicHeightInCtbsY - 1;
1768 }
1769
1770 WRITE_UE_MAX (bw, slice->num_entry_point_offsets, offset_max);
1771 if (slice->num_entry_point_offsets > 0) {
1772 WRITE_UE_MAX (bw, slice->offset_len_minus1, 31);
1773 for (i = 0; i < slice->num_entry_point_offsets; i++)
1774 WRITE_BITS (bw, slice->entry_point_offset_minus1[i],
1775 (slice->offset_len_minus1 + 1));
1776 }
1777 }
1778
1779 /* TODO */
1780 if (slice->pps->slice_segment_header_extension_present_flag) {
1781 GST_WARNING
1782 ("slice_segment_header_extension_present_flag is not supported");
1783 goto error;
1784 }
1785
1786 *space = TRUE;
1787 return TRUE;
1788
1789 error:
1790 GST_WARNING ("error to write slice header");
1791
1792 *space = have_space;
1793 return FALSE;
1794 }
1795
1796 /**
1797 * gst_h265_bit_writer_slice_hdr:
1798 * @slice: the slice header of #GstH265SliceHdr to write
1799 * @start_code: whether adding the nal start code
1800 * @nal_type: the slice's nal type of #GstH265NalUnitType
1801 * @data: (out): the bit stream generated by the slice header
1802 * @size: (inout): the size in bytes of the input and output
1803 *
1804 * Generating the according h265 bit stream by providing the slice header.
1805 *
1806 * Returns: a #GstH265BitWriterResult
1807 *
1808 * Since: 1.22
1809 **/
1810 GstH265BitWriterResult
1811 gst_h265_bit_writer_slice_hdr (const GstH265SliceHdr * slice,
1812 gboolean start_code, guint32 nal_type, guint8 * data, guint * size)
1813 {
1814 gboolean have_space = TRUE;
1815 GstBitWriter bw;
1816
1817 g_return_val_if_fail (slice != NULL, GST_H265_BIT_WRITER_ERROR);
1818 g_return_val_if_fail (slice->pps != NULL, GST_H265_BIT_WRITER_ERROR);
1819 g_return_val_if_fail (slice->pps->sps != NULL, GST_H265_BIT_WRITER_ERROR);
1820 g_return_val_if_fail (data != NULL, GST_H265_BIT_WRITER_ERROR);
1821 g_return_val_if_fail (size != NULL, GST_H265_BIT_WRITER_ERROR);
1822 g_return_val_if_fail (*size > 0, GST_H265_BIT_WRITER_ERROR);
1823 g_return_val_if_fail (nal_type <= GST_H265_NAL_SLICE_CRA_NUT,
1824 GST_H265_BIT_WRITER_ERROR);
1825
1826 gst_bit_writer_init_with_data (&bw, data, *size, FALSE);
1827
1828 if (start_code)
1829 WRITE_BITS (&bw, 0x00000001, 32);
1830
1831 /* NAL unit header */
1832 /* forbidden_zero_bit */
1833 WRITE_BITS (&bw, 0, 1);
1834 /* nal_unit_type */
1835 WRITE_BITS (&bw, nal_type, 6);
1836 /* nuh_layer_id, only support 0 now */
1837 WRITE_BITS (&bw, 0, 6);
1838 /* nuh_temporal_id_plus1, only support 1 now */
1839 WRITE_BITS (&bw, 1, 3);
1840
1841 if (!_h265_bit_writer_slice_header (slice, nal_type, &bw, &have_space))
1842 goto error;
1843
1844 /* Add trailings. */
1845 WRITE_BITS (&bw, 1, 1);
1846 if (!gst_bit_writer_align_bytes (&bw, 0)) {
1847 have_space = FALSE;
1848 goto error;
1849 }
1850
1851 *size = gst_bit_writer_get_size (&bw) / 8;
1852 gst_bit_writer_reset (&bw);
1853 return GST_H265_BIT_WRITER_OK;
1854
1855 error:
1856 gst_bit_writer_reset (&bw);
1857 *size = 0;
1858
1859 if (!have_space)
1860 return GST_H265_BIT_WRITER_NO_MORE_SPACE;
1861 return GST_H265_BIT_WRITER_INVALID_DATA;
1862 }
1863
1864
1865 static gboolean
1866 _h265_bit_writer_sei_registered_user_data (const GstH265RegisteredUserData *
1867 rud, GstBitWriter * bw, gboolean * space)
1868 {
1869 gboolean have_space = TRUE;
1870
1871 GST_DEBUG ("Writing \"Registered user data\"");
1872
1873 WRITE_BITS (bw, rud->country_code, 8);
1874 if (rud->country_code == 0xff)
1875 WRITE_BITS (bw, rud->country_code_extension, 8);
1876
1877 WRITE_BYTES (bw, rud->data, rud->size);
1878
1879 *space = TRUE;
1880 return TRUE;
1881
1882 error:
1883 GST_DEBUG ("Failed to write \"Registered user data\"");
1884
1885 *space = have_space;
1886 return FALSE;
1887 }
1888
1889 static gboolean
1890 _h265_bit_writer_sei_time_code (const GstH265TimeCode * tc,
1891 GstBitWriter * bw, gboolean * space)
1892 {
1893 gboolean have_space = TRUE;
1894 gint i;
1895
1896 GST_DEBUG ("Wrtiting \"Time code\"");
1897
1898 WRITE_BITS (bw, tc->num_clock_ts, 2);
1899
1900 for (i = 0; i < tc->num_clock_ts; i++) {
1901 WRITE_BITS (bw, tc->clock_timestamp_flag[i], 1);
1902 if (tc->clock_timestamp_flag[i]) {
1903 WRITE_BITS (bw, tc->units_field_based_flag[i], 1);
1904 WRITE_BITS (bw, tc->counting_type[i], 5);
1905 WRITE_BITS (bw, tc->full_timestamp_flag[i], 1);
1906 WRITE_BITS (bw, tc->discontinuity_flag[i], 1);
1907 WRITE_BITS (bw, tc->cnt_dropped_flag[i], 1);
1908 WRITE_BITS (bw, tc->n_frames[i], 9);
1909
1910 if (tc->full_timestamp_flag[i]) {
1911 WRITE_BITS (bw, tc->seconds_value[i], 6);
1912 WRITE_BITS (bw, tc->minutes_value[i], 6);
1913 WRITE_BITS (bw, tc->hours_value[i], 5);
1914 } else {
1915 WRITE_BITS (bw, tc->seconds_flag[i], 1);
1916 if (tc->seconds_flag[i]) {
1917 WRITE_BITS (bw, tc->seconds_value[i], 6);
1918 WRITE_BITS (bw, tc->minutes_flag[i], 1);
1919 if (tc->minutes_flag[i]) {
1920 WRITE_BITS (bw, tc->minutes_value[i], 6);
1921 WRITE_BITS (bw, tc->hours_flag[i], 1);
1922 if (tc->hours_flag[i]) {
1923 WRITE_BITS (bw, tc->hours_value[i], 5);
1924 }
1925 }
1926 }
1927 }
1928 }
1929
1930 WRITE_BITS (bw, tc->time_offset_length[i], 5);
1931
1932 if (tc->time_offset_length[i] > 0)
1933 WRITE_BITS (bw, tc->time_offset_value[i], tc->time_offset_length[i]);
1934 }
1935
1936 *space = TRUE;
1937 return TRUE;
1938
1939 error:
1940 GST_WARNING ("Failed to write \"Time code\"");
1941
1942 *space = have_space;
1943 return FALSE;
1944 }
1945
1946 static gboolean
1947 _h265_bit_writer_sei_mastering_display_colour_volume (const
1948 GstH265MasteringDisplayColourVolume * mdcv, GstBitWriter * bw,
1949 gboolean * space)
1950 {
1951 gboolean have_space = TRUE;
1952 gint i;
1953
1954 GST_DEBUG ("Wrtiting \"Mastering display colour volume\"");
1955
1956 for (i = 0; i < 3; i++) {
1957 WRITE_BITS (bw, mdcv->display_primaries_x[i], 16);
1958 WRITE_BITS (bw, mdcv->display_primaries_y[i], 16);
1959 }
1960
1961 WRITE_BITS (bw, mdcv->white_point_x, 16);
1962 WRITE_BITS (bw, mdcv->white_point_y, 16);
1963 WRITE_BITS (bw, mdcv->max_display_mastering_luminance, 32);
1964 WRITE_BITS (bw, mdcv->min_display_mastering_luminance, 32);
1965
1966 *space = TRUE;
1967 return TRUE;
1968
1969 error:
1970 GST_WARNING ("Failed to write \"Mastering display colour volume\"");
1971
1972 *space = have_space;
1973 return FALSE;
1974 }
1975
1976 static gboolean
1977 _h265_bit_writer_sei_content_light_level_info (const
1978 GstH265ContentLightLevel * cll, GstBitWriter * bw, gboolean * space)
1979 {
1980 gboolean have_space = TRUE;
1981
1982 GST_DEBUG ("Writing \"Content light level\"");
1983
1984 WRITE_BITS (bw, cll->max_content_light_level, 16);
1985 WRITE_BITS (bw, cll->max_pic_average_light_level, 16);
1986
1987 *space = TRUE;
1988 return TRUE;
1989
1990 error:
1991 GST_WARNING ("Failed to write \"Content light level\"");
1992
1993 *space = have_space;
1994 return FALSE;
1995 }
1996
1997 static gboolean
1998 _h265_bit_writer_sei_message (const GstH265SEIMessage * msg,
1999 GstBitWriter * bw, gboolean * space)
2000 {
2001 gboolean have_space = TRUE;
2002
2003 GST_DEBUG ("writing SEI message");
2004
2005 switch (msg->payloadType) {
2006 case GST_H265_SEI_REGISTERED_USER_DATA:
2007 if (!_h265_bit_writer_sei_registered_user_data
2008 (&msg->payload.registered_user_data, bw, &have_space))
2009 goto error;
2010 break;
2011 case GST_H265_SEI_TIME_CODE:
2012 if (!_h265_bit_writer_sei_time_code
2013 (&msg->payload.time_code, bw, &have_space))
2014 goto error;
2015 break;
2016 case GST_H265_SEI_MASTERING_DISPLAY_COLOUR_VOLUME:
2017 if (!_h265_bit_writer_sei_mastering_display_colour_volume
2018 (&msg->payload.mastering_display_colour_volume, bw, &have_space))
2019 goto error;
2020 break;
2021 case GST_H265_SEI_CONTENT_LIGHT_LEVEL:
2022 if (!_h265_bit_writer_sei_content_light_level_info
2023 (&msg->payload.content_light_level, bw, &have_space))
2024 goto error;
2025 break;
2026 default:
2027 break;
2028 }
2029
2030 /* Add trailings. */
2031 WRITE_BITS (bw, 1, 1);
2032 gst_bit_writer_align_bytes_unchecked (bw, 0);
2033
2034 *space = TRUE;
2035
2036 return TRUE;
2037
2038 error:
2039 GST_WARNING ("error to write SEI message");
2040
2041 *space = have_space;
2042 return FALSE;
2043 }
2044
2045 /**
2046 * gst_h265_bit_writer_sei:
2047 * @sei_messages: An array of #GstH265SEIMessage to write
2048 * @start_code: whether adding the nal start code
2049 * @data: (out): the bit stream generated by the sei messages
2050 * @size: (inout): the size in bytes of the input and output
2051 *
2052 * Generating the according h265 bit stream by providing sei messages.
2053 *
2054 * Returns: a #GstH265BitWriterResult
2055 *
2056 * Since: 1.22
2057 **/
2058 GstH265BitWriterResult
2059 gst_h265_bit_writer_sei (GArray * sei_messages,
2060 GstH265NalUnitType nal_type, gboolean start_code, guint8 * data,
2061 guint * size)
2062 {
2063 gboolean have_space = TRUE;
2064 GstBitWriter bw;
2065 GstH265SEIMessage *sei;
2066 gboolean have_written_data = FALSE;
2067 guint i;
2068
2069 g_return_val_if_fail (sei_messages != NULL, GST_H265_BIT_WRITER_ERROR);
2070 g_return_val_if_fail (nal_type == GST_H265_NAL_PREFIX_SEI
2071 || nal_type == GST_H265_NAL_SUFFIX_SEI, GST_H265_BIT_WRITER_ERROR);
2072 g_return_val_if_fail (data != NULL, GST_H265_BIT_WRITER_ERROR);
2073 g_return_val_if_fail (size != NULL, GST_H265_BIT_WRITER_ERROR);
2074 g_return_val_if_fail (*size > 0, GST_H265_BIT_WRITER_ERROR);
2075
2076 if (nal_type == GST_H265_NAL_PREFIX_SEI) {
2077 GST_WARNING ("prefix sei is not supported");
2078 return GST_H265_BIT_WRITER_ERROR;
2079 }
2080
2081 gst_bit_writer_init_with_data (&bw, data, *size, FALSE);
2082
2083 if (start_code)
2084 WRITE_BITS (&bw, 0x00000001, 32);
2085
2086 /* NAL unit header */
2087 /* forbidden_zero_bit */
2088 WRITE_BITS (&bw, 0, 1);
2089 /* nal_unit_type */
2090 WRITE_BITS (&bw, nal_type, 6);
2091 /* nuh_layer_id, only support 0 now */
2092 WRITE_BITS (&bw, 0, 6);
2093 /* nuh_temporal_id_plus1, only support 1 now */
2094 WRITE_BITS (&bw, 1, 3);
2095
2096 for (i = 0; i < sei_messages->len; i++) {
2097 guint32 payload_size_data;
2098 guint32 payload_type_data;
2099
2100 gst_bit_writer_init (&bw);
2101
2102 sei = &g_array_index (sei_messages, GstH265SEIMessage, i);
2103 if (!_h265_bit_writer_sei_message (sei, &bw, &have_space))
2104 goto error;
2105
2106 if (gst_bit_writer_get_size (&bw) == 0) {
2107 GST_FIXME ("Unsupported SEI type %d", sei->payloadType);
2108 continue;
2109 }
2110
2111 have_written_data = TRUE;
2112
2113 g_assert (gst_bit_writer_get_size (&bw) % 8 == 0);
2114 payload_size_data = (gst_bit_writer_get_size (&bw) + 7) / 8;
2115 payload_type_data = sei->payloadType;
2116
2117 /* write payload type bytes */
2118 while (payload_type_data >= 0xff) {
2119 WRITE_BITS (&bw, 0xff, 8);
2120 payload_type_data -= -0xff;
2121 }
2122 WRITE_BITS (&bw, payload_type_data, 8);
2123
2124 /* write payload size bytes */
2125 while (payload_size_data >= 0xff) {
2126 WRITE_BITS (&bw, 0xff, 8);
2127 payload_size_data -= -0xff;
2128 }
2129 WRITE_BITS (&bw, payload_size_data, 8);
2130
2131 if (gst_bit_writer_get_size (&bw) / 8)
2132 WRITE_BYTES (&bw, gst_bit_writer_get_data (&bw),
2133 gst_bit_writer_get_size (&bw) / 8);
2134
2135 gst_bit_writer_reset (&bw);
2136 }
2137
2138 if (!have_written_data) {
2139 GST_WARNING ("No written sei data");
2140 goto error;
2141 }
2142
2143 /* Add trailings. */
2144 WRITE_BITS (&bw, 1, 1);
2145 if (!gst_bit_writer_align_bytes (&bw, 0)) {
2146 have_space = FALSE;
2147 goto error;
2148 }
2149
2150 *size = gst_bit_writer_get_size (&bw) / 8;
2151 gst_bit_writer_reset (&bw);
2152 return GST_H265_BIT_WRITER_OK;
2153
2154 error:
2155 gst_bit_writer_reset (&bw);
2156 *size = 0;
2157
2158 if (!have_space)
2159 return GST_H265_BIT_WRITER_NO_MORE_SPACE;
2160 return GST_H265_BIT_WRITER_INVALID_DATA;
2161 }
2162
2163 /**
2164 * gst_h265_bit_writer_aud:
2165 * @pic_type: indicate the possible slice types list just
2166 * as the H265 spec Table 7-2 defines
2167 * @start_code: whether adding the nal start code
2168 * @data: (out): the bit stream generated by the aud
2169 * @size: (inout): the size in bytes of the input and output
2170 *
2171 * Generating the according h265 bit stream of an aud.
2172 *
2173 * Returns: a #GstH265BitWriterResult
2174 *
2175 * Since: 1.22
2176 **/
2177 GstH265BitWriterResult
2178 gst_h265_bit_writer_aud (guint8 pic_type, gboolean start_code,
2179 guint8 * data, guint * size)
2180 {
2181 gboolean have_space = TRUE;
2182 GstBitWriter bw;
2183
2184 g_return_val_if_fail (pic_type <= 2, GST_H265_BIT_WRITER_ERROR);
2185 g_return_val_if_fail (data != NULL, GST_H265_BIT_WRITER_ERROR);
2186 g_return_val_if_fail (size != NULL, GST_H265_BIT_WRITER_ERROR);
2187 g_return_val_if_fail (*size > 0, GST_H265_BIT_WRITER_ERROR);
2188
2189 gst_bit_writer_init_with_data (&bw, data, *size, FALSE);
2190
2191 if (start_code)
2192 WRITE_BITS (&bw, 0x00000001, 32);
2193
2194 /* NAL unit header */
2195 /* forbidden_zero_bit */
2196 WRITE_BITS (&bw, 0, 1);
2197 /* nal_unit_type */
2198 WRITE_BITS (&bw, GST_H265_NAL_AUD, 6);
2199 /* nuh_layer_id, only support 0 now */
2200 WRITE_BITS (&bw, 0, 6);
2201 /* nuh_temporal_id_plus1, only support 1 now */
2202 WRITE_BITS (&bw, 1, 3);
2203
2204 WRITE_BITS (&bw, pic_type, 3);
2205
2206 /* Add trailings. */
2207 WRITE_BITS (&bw, 1, 1);
2208 if (!gst_bit_writer_align_bytes (&bw, 0)) {
2209 have_space = FALSE;
2210 goto error;
2211 }
2212
2213 *size = gst_bit_writer_get_size (&bw) / 8;
2214 gst_bit_writer_reset (&bw);
2215
2216 return GST_H265_BIT_WRITER_OK;
2217
2218 error:
2219 gst_bit_writer_reset (&bw);
2220 *size = 0;
2221
2222 if (!have_space)
2223 return GST_H265_BIT_WRITER_NO_MORE_SPACE;
2224 return GST_H265_BIT_WRITER_INVALID_DATA;
2225 }
2226
2227 /**
2228 * gst_h265_bit_writer_convert_to_nal:
2229 * @nal_prefix_size: the size in bytes for the prefix of a nal, may
2230 * be 2, 3 or 4
2231 * @packetized: whether to write the bit stream in packetized format,
2232 * which does not have the start code but has a @nal_prefix_size bytes'
2233 * size prepending to the real nal data
2234 * @has_startcode: whether the input already has a start code
2235 * @add_trailings: whether to add rbsp trailing bits to make the output
2236 * aligned to byte
2237 * @raw_data: the input bit stream
2238 * @raw_size: the size in bits of the input bit stream
2239 * @nal_data: (out): the output bit stream converted to a real nal
2240 * @nal_size: (inout): the size in bytes of the output
2241 *
2242 * Converting a bit stream into a real nal packet. If the bit stream already
2243 * has a start code, it will be replaced by the new one specified by the
2244 * @nal_prefix_size and @packetized. It is assured that the output aligns to
2245 * the byte and the all the emulations are inserted.
2246 *
2247 * Returns: a #GstH265BitWriterResult
2248 *
2249 * Since: 1.22
2250 **/
2251 GstH265BitWriterResult
2252 gst_h265_bit_writer_convert_to_nal (guint nal_prefix_size,
2253 gboolean packetized, gboolean has_startcode, gboolean add_trailings,
2254 const guint8 * raw_data, gsize raw_size, guint8 * nal_data,
2255 guint * nal_size)
2256 {
2257 NalWriter nw;
2258 guint8 *data;
2259 guint32 size = 0;
2260 gboolean need_more_space = FALSE;
2261
2262 g_return_val_if_fail (
2263 (packetized && nal_prefix_size > 1 && nal_prefix_size < 5) ||
2264 (!packetized && (nal_prefix_size == 3 || nal_prefix_size == 4)),
2265 GST_H265_BIT_WRITER_ERROR);
2266 g_return_val_if_fail (raw_data != NULL, GST_H265_BIT_WRITER_ERROR);
2267 g_return_val_if_fail (raw_size > 0, GST_H265_BIT_WRITER_ERROR);
2268 g_return_val_if_fail (raw_size / 8 <= G_MAXUINT, GST_H265_BIT_WRITER_ERROR);
2269 g_return_val_if_fail (nal_data != NULL, GST_H265_BIT_WRITER_ERROR);
2270 g_return_val_if_fail (nal_size != NULL, GST_H265_BIT_WRITER_ERROR);
2271 g_return_val_if_fail (*nal_size > 0, GST_H265_BIT_WRITER_ERROR);
2272
2273 if (has_startcode) {
2274 /* Skip the start code, the NalWriter will add it automatically. */
2275 if (raw_size >= 4 && raw_data[0] == 0
2276 && raw_data[1] == 0 && raw_data[2] == 0 && raw_data[3] == 0x01) {
2277 raw_data += 4;
2278 raw_size -= 4 * 8;
2279 } else if (raw_size >= 3 && raw_data[0] == 0 && raw_data[1] == 0
2280 && raw_data[2] == 0x01) {
2281 raw_data += 3;
2282 raw_size -= 3 * 8;
2283 } else {
2284 /* Fail to find the start code. */
2285 g_return_val_if_reached (GST_H265_BIT_WRITER_ERROR);
2286 }
2287 }
2288
2289 /* If no RBSP trailing needed, it must align to byte. We assume
2290 that the rbsp trailing bits are already added. */
2291 if (!add_trailings)
2292 g_return_val_if_fail (raw_size % 8 == 0, GST_H265_BIT_WRITER_ERROR);
2293
2294 nal_writer_init (&nw, nal_prefix_size, packetized);
2295
2296 if (!nal_writer_put_bytes (&nw, raw_data, (guint) (raw_size / 8)))
2297 goto error;
2298
2299 if (raw_size % 8) {
2300 guint8 data = *(raw_data + raw_size / 8);
2301
2302 if (!nal_writer_put_bits_uint8 (&nw,
2303 data >> (8 - raw_size % 8), raw_size % 8))
2304 goto error;
2305 }
2306
2307 if (add_trailings) {
2308 if (!nal_writer_do_rbsp_trailing_bits (&nw))
2309 goto error;
2310 }
2311
2312 data = nal_writer_reset_and_get_data (&nw, &size);
2313 if (!data)
2314 goto error;
2315
2316 if (size > *nal_size) {
2317 need_more_space = TRUE;
2318 g_free (data);
2319 goto error;
2320 }
2321
2322 memcpy (nal_data, data, size);
2323 *nal_size = size;
2324 g_free (data);
2325 nal_writer_reset (&nw);
2326 return GST_H265_BIT_WRITER_OK;
2327
2328 error:
2329 nal_writer_reset (&nw);
2330 *nal_size = 0;
2331
2332 GST_WARNING ("Failed to convert nal data");
2333
2334 if (need_more_space)
2335 return GST_H265_BIT_WRITER_NO_MORE_SPACE;
2336 return GST_H265_BIT_WRITER_INVALID_DATA;
2337 }