| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
#ifdef HAVE_CONFIG_H |
| 30 |
# include "config.h" |
| 31 |
#endif |
| 32 |
|
| 33 |
#include "gsth264bitwriter.h" |
| 34 |
#include "nalutils.h" |
| 35 |
#include <gst/base/gstbitwriter.h> |
| 36 |
|
| 37 |
#ifndef GST_DISABLE_GST_DEBUG |
| 38 |
#define GST_CAT_DEFAULT gst_h264_debug_category_get() |
| 39 |
static GstDebugCategory * |
| 40 |
gst_h264_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_h264", 0, |
| 48 |
"h264 bitwriter library"); |
| 49 |
|
| 50 |
g_once_init_leave (&cat_gonce, (gsize) cat); |
| 51 |
} |
| 52 |
|
| 53 |
return (GstDebugCategory *) cat_gonce; |
| 54 |
} |
| 55 |
#endif |
| 56 |
|
| 57 |
|
| 58 |
#define SIGNED(val) (2 * ABS(val) - ((val) > 0)) |
| 59 |
|
| 60 |
|
| 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 |
|
| 123 |
|
| 124 |
|
| 125 |
static const guint8 default_4x4_intra[16] = { |
| 126 |
6, 13, 13, 20, 20, 20, 28, 28, 28, 28, 32, 32, |
| 127 |
32, 37, 37, 42 |
| 128 |
}; |
| 129 |
|
| 130 |
static const guint8 default_4x4_inter[16] = { |
| 131 |
10, 14, 14, 20, 20, 20, 24, 24, 24, 24, 27, 27, |
| 132 |
27, 30, 30, 34 |
| 133 |
}; |
| 134 |
|
| 135 |
static const guint8 default_8x8_intra[64] = { |
| 136 |
6, 10, 10, 13, 11, 13, 16, 16, 16, 16, 18, 18, |
| 137 |
18, 18, 18, 23, 23, 23, 23, 23, 23, 25, 25, 25, 25, 25, 25, 25, 27, 27, 27, |
| 138 |
27, 27, 27, 27, 27, 29, 29, 29, 29, 29, 29, 29, 31, 31, 31, 31, 31, 31, 33, |
| 139 |
33, 33, 33, 33, 36, 36, 36, 36, 38, 38, 38, 40, 40, 42 |
| 140 |
}; |
| 141 |
|
| 142 |
static const guint8 default_8x8_inter[64] = { |
| 143 |
9, 13, 13, 15, 13, 15, 17, 17, 17, 17, 19, 19, |
| 144 |
19, 19, 19, 21, 21, 21, 21, 21, 21, 22, 22, 22, 22, 22, 22, 22, 24, 24, 24, |
| 145 |
24, 24, 24, 24, 24, 25, 25, 25, 25, 25, 25, 25, 27, 27, 27, 27, 27, 27, 28, |
| 146 |
28, 28, 28, 28, 30, 30, 30, 30, 32, 32, 32, 33, 33, 35 |
| 147 |
}; |
| 148 |
|
| 149 |
static gboolean |
| 150 |
_h264_bit_writer_scaling_list (GstBitWriter * bw, gboolean * space, |
| 151 |
const guint8 scaling_lists_4x4[6][16], |
| 152 |
const guint8 scaling_lists_8x8[6][64], const guint8 fallback_4x4_inter[16], |
| 153 |
const guint8 fallback_4x4_intra[16], const guint8 fallback_8x8_inter[64], |
| 154 |
const guint8 fallback_8x8_intra[64], guint8 n_lists) |
| 155 |
{ |
| 156 |
gboolean have_space = TRUE; |
| 157 |
guint i, j; |
| 158 |
|
| 159 |
const guint8 *default_lists[12] = { |
| 160 |
fallback_4x4_intra, fallback_4x4_intra, fallback_4x4_intra, |
| 161 |
fallback_4x4_inter, fallback_4x4_inter, fallback_4x4_inter, |
| 162 |
fallback_8x8_intra, fallback_8x8_inter, |
| 163 |
fallback_8x8_intra, fallback_8x8_inter, |
| 164 |
fallback_8x8_intra, fallback_8x8_inter |
| 165 |
}; |
| 166 |
|
| 167 |
GST_DEBUG ("writing scaling lists"); |
| 168 |
|
| 169 |
for (i = 0; i < 12; i++) { |
| 170 |
if (i < n_lists) { |
| 171 |
guint8 scaling_list_present_flag = FALSE; |
| 172 |
const guint8 *scaling_list; |
| 173 |
guint size; |
| 174 |
|
| 175 |
if (i < 6) { |
| 176 |
scaling_list = scaling_lists_4x4[i]; |
| 177 |
size = 16; |
| 178 |
} else { |
| 179 |
scaling_list = scaling_lists_8x8[i - 6]; |
| 180 |
size = 64; |
| 181 |
} |
| 182 |
|
| 183 |
switch (i) { |
| 184 |
case 0: |
| 185 |
case 3: |
| 186 |
case 6: |
| 187 |
case 7: |
| 188 |
if (memcmp (scaling_list, default_lists[i], size)) |
| 189 |
scaling_list_present_flag = TRUE; |
| 190 |
break; |
| 191 |
case 1: |
| 192 |
case 2: |
| 193 |
case 4: |
| 194 |
case 5: |
| 195 |
if (memcmp (scaling_list, scaling_lists_4x4[i - 1], size)) |
| 196 |
scaling_list_present_flag = TRUE; |
| 197 |
break; |
| 198 |
case 8: |
| 199 |
case 9: |
| 200 |
case 10: |
| 201 |
case 11: |
| 202 |
if (memcmp (scaling_list, scaling_lists_8x8[i - 6 - 2], size)) |
| 203 |
scaling_list_present_flag = TRUE; |
| 204 |
break; |
| 205 |
default: |
| 206 |
break; |
| 207 |
} |
| 208 |
|
| 209 |
WRITE_BITS (bw, scaling_list_present_flag, 1); |
| 210 |
if (scaling_list_present_flag) { |
| 211 |
guint8 last_scale, next_scale; |
| 212 |
gint8 delta_scale; |
| 213 |
|
| 214 |
last_scale = next_scale = 8; |
| 215 |
|
| 216 |
for (j = 0; j < size; j++) { |
| 217 |
if (next_scale != 0) { |
| 218 |
delta_scale = (gint8) (scaling_list[j] - last_scale); |
| 219 |
|
| 220 |
WRITE_SE (bw, delta_scale); |
| 221 |
|
| 222 |
next_scale = scaling_list[j]; |
| 223 |
} |
| 224 |
last_scale = (next_scale == 0) ? last_scale : next_scale; |
| 225 |
} |
| 226 |
} |
| 227 |
} |
| 228 |
} |
| 229 |
|
| 230 |
*space = TRUE; |
| 231 |
return TRUE; |
| 232 |
|
| 233 |
error: |
| 234 |
GST_WARNING ("error to write scaling lists"); |
| 235 |
|
| 236 |
*space = have_space; |
| 237 |
return FALSE; |
| 238 |
} |
| 239 |
|
| 240 |
static gboolean |
| 241 |
_h264_bit_writer_hrd_parameters (const GstH264HRDParams * hrd, |
| 242 |
GstBitWriter * bw, gboolean * space) |
| 243 |
{ |
| 244 |
gboolean have_space = TRUE; |
| 245 |
guint sched_sel_idx; |
| 246 |
|
| 247 |
GST_DEBUG ("writing \"HRD Parameters\""); |
| 248 |
|
| 249 |
WRITE_UE_MAX (bw, hrd->cpb_cnt_minus1, 31); |
| 250 |
WRITE_BITS (bw, hrd->bit_rate_scale, 4); |
| 251 |
WRITE_BITS (bw, hrd->cpb_size_scale, 4); |
| 252 |
|
| 253 |
for (sched_sel_idx = 0; sched_sel_idx <= hrd->cpb_cnt_minus1; sched_sel_idx++) { |
| 254 |
WRITE_UE (bw, hrd->bit_rate_value_minus1[sched_sel_idx]); |
| 255 |
WRITE_UE (bw, hrd->cpb_size_value_minus1[sched_sel_idx]); |
| 256 |
WRITE_BITS (bw, hrd->cbr_flag[sched_sel_idx], 1); |
| 257 |
} |
| 258 |
|
| 259 |
WRITE_BITS (bw, hrd->initial_cpb_removal_delay_length_minus1, 5); |
| 260 |
WRITE_BITS (bw, hrd->cpb_removal_delay_length_minus1, 5); |
| 261 |
WRITE_BITS (bw, hrd->dpb_output_delay_length_minus1, 5); |
| 262 |
WRITE_BITS (bw, hrd->time_offset_length, 5); |
| 263 |
|
| 264 |
*space = TRUE; |
| 265 |
return TRUE; |
| 266 |
|
| 267 |
error: |
| 268 |
GST_WARNING ("error to write \"HRD Parameters\""); |
| 269 |
|
| 270 |
*space = have_space; |
| 271 |
return FALSE; |
| 272 |
} |
| 273 |
|
| 274 |
#define EXTENDED_SAR 255 |
| 275 |
|
| 276 |
static gboolean |
| 277 |
_h264_bit_writer_vui_parameters (const GstH264SPS * sps, GstBitWriter * bw, |
| 278 |
gboolean * space) |
| 279 |
{ |
| 280 |
gboolean have_space = TRUE; |
| 281 |
const GstH264VUIParams *vui = &sps->vui_parameters; |
| 282 |
|
| 283 |
GST_DEBUG ("writing \"VUI Parameters\""); |
| 284 |
|
| 285 |
WRITE_BITS (bw, vui->aspect_ratio_info_present_flag, 1); |
| 286 |
if (vui->aspect_ratio_info_present_flag) { |
| 287 |
WRITE_BITS (bw, vui->aspect_ratio_idc, 8); |
| 288 |
if (vui->aspect_ratio_idc == EXTENDED_SAR) { |
| 289 |
WRITE_BITS (bw, vui->sar_width, 16); |
| 290 |
WRITE_BITS (bw, vui->sar_height, 16); |
| 291 |
} |
| 292 |
} |
| 293 |
|
| 294 |
WRITE_BITS (bw, vui->overscan_info_present_flag, 1); |
| 295 |
if (vui->overscan_info_present_flag) |
| 296 |
WRITE_BITS (bw, vui->overscan_appropriate_flag, 1); |
| 297 |
|
| 298 |
WRITE_BITS (bw, vui->video_signal_type_present_flag, 1); |
| 299 |
if (vui->video_signal_type_present_flag) { |
| 300 |
WRITE_BITS (bw, vui->video_format, 3); |
| 301 |
WRITE_BITS (bw, vui->video_full_range_flag, 1); |
| 302 |
WRITE_BITS (bw, vui->colour_description_present_flag, 1); |
| 303 |
if (vui->colour_description_present_flag) { |
| 304 |
WRITE_BITS (bw, vui->colour_primaries, 8); |
| 305 |
WRITE_BITS (bw, vui->transfer_characteristics, 8); |
| 306 |
WRITE_BITS (bw, vui->matrix_coefficients, 8); |
| 307 |
} |
| 308 |
} |
| 309 |
|
| 310 |
WRITE_BITS (bw, vui->chroma_loc_info_present_flag, 1); |
| 311 |
if (vui->chroma_loc_info_present_flag) { |
| 312 |
WRITE_UE_MAX (bw, vui->chroma_sample_loc_type_top_field, 5); |
| 313 |
WRITE_UE_MAX (bw, vui->chroma_sample_loc_type_bottom_field, 5); |
| 314 |
} |
| 315 |
|
| 316 |
WRITE_BITS (bw, vui->timing_info_present_flag, 1); |
| 317 |
if (vui->timing_info_present_flag) { |
| 318 |
WRITE_BITS (bw, vui->num_units_in_tick, 32); |
| 319 |
if (vui->num_units_in_tick == 0) |
| 320 |
GST_WARNING ("num_units_in_tick = 0 write to stream " |
| 321 |
"(incompliant to H.264 E.2.1)."); |
| 322 |
|
| 323 |
WRITE_BITS (bw, vui->time_scale, 32); |
| 324 |
if (vui->time_scale == 0) |
| 325 |
GST_WARNING ("time_scale = 0 write to stream " |
| 326 |
"(incompliant to H.264 E.2.1)."); |
| 327 |
|
| 328 |
WRITE_BITS (bw, vui->fixed_frame_rate_flag, 1); |
| 329 |
} |
| 330 |
|
| 331 |
WRITE_BITS (bw, vui->nal_hrd_parameters_present_flag, 1); |
| 332 |
if (vui->nal_hrd_parameters_present_flag) { |
| 333 |
if (!_h264_bit_writer_hrd_parameters (&vui->nal_hrd_parameters, bw, |
| 334 |
&have_space)) |
| 335 |
goto error; |
| 336 |
} |
| 337 |
|
| 338 |
WRITE_BITS (bw, vui->vcl_hrd_parameters_present_flag, 1); |
| 339 |
if (vui->vcl_hrd_parameters_present_flag) { |
| 340 |
if (!_h264_bit_writer_hrd_parameters (&vui->vcl_hrd_parameters, bw, |
| 341 |
&have_space)) |
| 342 |
goto error; |
| 343 |
} |
| 344 |
|
| 345 |
if (vui->nal_hrd_parameters_present_flag || |
| 346 |
vui->vcl_hrd_parameters_present_flag) |
| 347 |
WRITE_BITS (bw, vui->low_delay_hrd_flag, 1); |
| 348 |
|
| 349 |
WRITE_BITS (bw, vui->pic_struct_present_flag, 1); |
| 350 |
WRITE_BITS (bw, vui->bitstream_restriction_flag, 1); |
| 351 |
if (vui->bitstream_restriction_flag) { |
| 352 |
WRITE_BITS (bw, vui->motion_vectors_over_pic_boundaries_flag, 1); |
| 353 |
WRITE_UE (bw, vui->max_bytes_per_pic_denom); |
| 354 |
WRITE_UE_MAX (bw, vui->max_bits_per_mb_denom, 16); |
| 355 |
WRITE_UE_MAX (bw, vui->log2_max_mv_length_horizontal, 16); |
| 356 |
WRITE_UE_MAX (bw, vui->log2_max_mv_length_vertical, 16); |
| 357 |
WRITE_UE (bw, vui->num_reorder_frames); |
| 358 |
WRITE_UE (bw, vui->max_dec_frame_buffering); |
| 359 |
} |
| 360 |
|
| 361 |
*space = TRUE; |
| 362 |
return TRUE; |
| 363 |
|
| 364 |
error: |
| 365 |
GST_WARNING ("error to write \"VUI Parameters\""); |
| 366 |
|
| 367 |
*space = have_space; |
| 368 |
return FALSE; |
| 369 |
} |
| 370 |
|
| 371 |
static gboolean |
| 372 |
_h264_bit_writer_sps (const GstH264SPS * sps, GstBitWriter * bw, |
| 373 |
gboolean * space) |
| 374 |
{ |
| 375 |
gboolean have_space = TRUE; |
| 376 |
|
| 377 |
GST_DEBUG ("writing SPS"); |
| 378 |
|
| 379 |
WRITE_BITS (bw, sps->profile_idc, 8); |
| 380 |
WRITE_BITS (bw, sps->constraint_set0_flag, 1); |
| 381 |
WRITE_BITS (bw, sps->constraint_set1_flag, 1); |
| 382 |
WRITE_BITS (bw, sps->constraint_set2_flag, 1); |
| 383 |
WRITE_BITS (bw, sps->constraint_set3_flag, 1); |
| 384 |
WRITE_BITS (bw, sps->constraint_set4_flag, 1); |
| 385 |
WRITE_BITS (bw, sps->constraint_set5_flag, 1); |
| 386 |
|
| 387 |
WRITE_BITS (bw, 0, 2); |
| 388 |
|
| 389 |
WRITE_BITS (bw, sps->level_idc, 8); |
| 390 |
|
| 391 |
WRITE_UE_MAX (bw, sps->id, GST_H264_MAX_SPS_COUNT - 1); |
| 392 |
|
| 393 |
if (sps->profile_idc == 100 || sps->profile_idc == 110 || |
| 394 |
sps->profile_idc == 122 || sps->profile_idc == 244 || |
| 395 |
sps->profile_idc == 44 || sps->profile_idc == 83 || |
| 396 |
sps->profile_idc == 86 || sps->profile_idc == 118 || |
| 397 |
sps->profile_idc == 128 || sps->profile_idc == 138 || |
| 398 |
sps->profile_idc == 139 || sps->profile_idc == 134 || |
| 399 |
sps->profile_idc == 135) { |
| 400 |
WRITE_UE_MAX (bw, sps->chroma_format_idc, 3); |
| 401 |
if (sps->chroma_format_idc == 3) |
| 402 |
WRITE_BITS (bw, sps->separate_colour_plane_flag, 1); |
| 403 |
|
| 404 |
WRITE_UE_MAX (bw, sps->bit_depth_luma_minus8, 6); |
| 405 |
WRITE_UE_MAX (bw, sps->bit_depth_chroma_minus8, 6); |
| 406 |
WRITE_BITS (bw, sps->qpprime_y_zero_transform_bypass_flag, 1); |
| 407 |
|
| 408 |
WRITE_BITS (bw, sps->scaling_matrix_present_flag, 1); |
| 409 |
if (sps->scaling_matrix_present_flag) { |
| 410 |
guint8 n_lists; |
| 411 |
|
| 412 |
n_lists = (sps->chroma_format_idc != 3) ? 8 : 12; |
| 413 |
if (!_h264_bit_writer_scaling_list (bw, &have_space, |
| 414 |
sps->scaling_lists_4x4, sps->scaling_lists_8x8, |
| 415 |
default_4x4_inter, default_4x4_intra, |
| 416 |
default_8x8_inter, default_8x8_intra, n_lists)) |
| 417 |
goto error; |
| 418 |
} |
| 419 |
} |
| 420 |
|
| 421 |
WRITE_UE_MAX (bw, sps->log2_max_frame_num_minus4, 12); |
| 422 |
|
| 423 |
WRITE_UE_MAX (bw, sps->pic_order_cnt_type, 2); |
| 424 |
if (sps->pic_order_cnt_type == 0) { |
| 425 |
WRITE_UE_MAX (bw, sps->log2_max_pic_order_cnt_lsb_minus4, 12); |
| 426 |
} else if (sps->pic_order_cnt_type == 1) { |
| 427 |
guint i; |
| 428 |
|
| 429 |
WRITE_BITS (bw, sps->delta_pic_order_always_zero_flag, 1); |
| 430 |
WRITE_SE (bw, sps->offset_for_non_ref_pic); |
| 431 |
WRITE_SE (bw, sps->offset_for_top_to_bottom_field); |
| 432 |
WRITE_UE_MAX (bw, sps->num_ref_frames_in_pic_order_cnt_cycle, 255); |
| 433 |
|
| 434 |
for (i = 0; i < sps->num_ref_frames_in_pic_order_cnt_cycle; i++) |
| 435 |
WRITE_SE (bw, sps->offset_for_ref_frame[i]); |
| 436 |
} |
| 437 |
|
| 438 |
WRITE_UE (bw, sps->num_ref_frames); |
| 439 |
WRITE_BITS (bw, sps->gaps_in_frame_num_value_allowed_flag, 1); |
| 440 |
WRITE_UE (bw, sps->pic_width_in_mbs_minus1); |
| 441 |
WRITE_UE (bw, sps->pic_height_in_map_units_minus1); |
| 442 |
WRITE_BITS (bw, sps->frame_mbs_only_flag, 1); |
| 443 |
|
| 444 |
if (!sps->frame_mbs_only_flag) |
| 445 |
WRITE_BITS (bw, sps->mb_adaptive_frame_field_flag, 1); |
| 446 |
|
| 447 |
WRITE_BITS (bw, sps->direct_8x8_inference_flag, 1); |
| 448 |
WRITE_BITS (bw, sps->frame_cropping_flag, 1); |
| 449 |
if (sps->frame_cropping_flag) { |
| 450 |
WRITE_UE (bw, sps->frame_crop_left_offset); |
| 451 |
WRITE_UE (bw, sps->frame_crop_right_offset); |
| 452 |
WRITE_UE (bw, sps->frame_crop_top_offset); |
| 453 |
WRITE_UE (bw, sps->frame_crop_bottom_offset); |
| 454 |
} |
| 455 |
|
| 456 |
WRITE_BITS (bw, sps->vui_parameters_present_flag, 1); |
| 457 |
if (sps->vui_parameters_present_flag) |
| 458 |
if (!_h264_bit_writer_vui_parameters (sps, bw, &have_space)) |
| 459 |
goto error; |
| 460 |
|
| 461 |
*space = TRUE; |
| 462 |
return TRUE; |
| 463 |
|
| 464 |
error: |
| 465 |
GST_WARNING ("error to write sps"); |
| 466 |
|
| 467 |
*space = have_space; |
| 468 |
return FALSE; |
| 469 |
} |
| 470 |
|
| 471 |
|
| 472 |
|
| 473 |
|
| 474 |
|
| 475 |
|
| 476 |
|
| 477 |
|
| 478 |
|
| 479 |
|
| 480 |
|
| 481 |
|
| 482 |
|
| 483 |
|
| 484 |
GstH264BitWriterResult |
| 485 |
gst_h264_bit_writer_sps (const GstH264SPS * sps, gboolean start_code, |
| 486 |
guint8 * data, guint * size) |
| 487 |
{ |
| 488 |
gboolean have_space = TRUE; |
| 489 |
GstBitWriter bw; |
| 490 |
|
| 491 |
g_return_val_if_fail (sps != NULL, GST_H264_BIT_WRITER_ERROR); |
| 492 |
g_return_val_if_fail (data != NULL, GST_H264_BIT_WRITER_ERROR); |
| 493 |
g_return_val_if_fail (size != NULL, GST_H264_BIT_WRITER_ERROR); |
| 494 |
g_return_val_if_fail (*size > 0, GST_H264_BIT_WRITER_ERROR); |
| 495 |
|
| 496 |
gst_bit_writer_init_with_data (&bw, data, *size, FALSE); |
| 497 |
|
| 498 |
if (start_code) |
| 499 |
WRITE_BITS (&bw, 0x00000001, 32); |
| 500 |
|
| 501 |
|
| 502 |
|
| 503 |
WRITE_BITS (&bw, 0, 1); |
| 504 |
|
| 505 |
WRITE_BITS (&bw, 1, 2); |
| 506 |
|
| 507 |
WRITE_BITS (&bw, GST_H264_NAL_SPS, 5); |
| 508 |
|
| 509 |
if (!_h264_bit_writer_sps (sps, &bw, &have_space)) |
| 510 |
goto error; |
| 511 |
|
| 512 |
|
| 513 |
WRITE_BITS (&bw, 1, 1); |
| 514 |
if (!gst_bit_writer_align_bytes (&bw, 0)) { |
| 515 |
have_space = FALSE; |
| 516 |
goto error; |
| 517 |
} |
| 518 |
|
| 519 |
*size = (gst_bit_writer_get_size (&bw)) / 8; |
| 520 |
gst_bit_writer_reset (&bw); |
| 521 |
|
| 522 |
return GST_H264_BIT_WRITER_OK; |
| 523 |
|
| 524 |
error: |
| 525 |
gst_bit_writer_reset (&bw); |
| 526 |
*size = 0; |
| 527 |
|
| 528 |
return have_space ? GST_H264_BIT_WRITER_INVALID_DATA : |
| 529 |
GST_H264_BIT_WRITER_NO_MORE_SPACE; |
| 530 |
} |
| 531 |
|
| 532 |
static gboolean |
| 533 |
_h264_bit_writer_pps (const GstH264PPS * pps, GstBitWriter * bw, |
| 534 |
gboolean * space) |
| 535 |
{ |
| 536 |
gboolean have_space = TRUE; |
| 537 |
gint qp_bd_offset; |
| 538 |
|
| 539 |
GST_DEBUG ("writing PPS"); |
| 540 |
|
| 541 |
qp_bd_offset = 6 * (pps->sequence->bit_depth_luma_minus8 + |
| 542 |
pps->sequence->separate_colour_plane_flag); |
| 543 |
|
| 544 |
WRITE_UE_MAX (bw, pps->id, GST_H264_MAX_PPS_COUNT - 1); |
| 545 |
WRITE_UE_MAX (bw, pps->sequence->id, GST_H264_MAX_SPS_COUNT - 1); |
| 546 |
|
| 547 |
WRITE_BITS (bw, pps->entropy_coding_mode_flag, 1); |
| 548 |
WRITE_BITS (bw, pps->pic_order_present_flag, 1); |
| 549 |
|
| 550 |
WRITE_UE_MAX (bw, pps->num_slice_groups_minus1, 7); |
| 551 |
if (pps->num_slice_groups_minus1 > 0) { |
| 552 |
WRITE_UE_MAX (bw, pps->slice_group_map_type, 6); |
| 553 |
|
| 554 |
if (pps->slice_group_map_type == 0) { |
| 555 |
gint i; |
| 556 |
|
| 557 |
for (i = 0; i <= pps->num_slice_groups_minus1; i++) |
| 558 |
WRITE_UE (bw, pps->run_length_minus1[i]); |
| 559 |
} else if (pps->slice_group_map_type == 2) { |
| 560 |
gint i; |
| 561 |
|
| 562 |
for (i = 0; i < pps->num_slice_groups_minus1; i++) { |
| 563 |
WRITE_UE (bw, pps->top_left[i]); |
| 564 |
WRITE_UE (bw, pps->bottom_right[i]); |
| 565 |
} |
| 566 |
} else if (pps->slice_group_map_type >= 3 && pps->slice_group_map_type <= 5) { |
| 567 |
WRITE_BITS (bw, pps->slice_group_change_direction_flag, 1); |
| 568 |
WRITE_UE (bw, pps->slice_group_change_rate_minus1); |
| 569 |
} else if (pps->slice_group_map_type == 6) { |
| 570 |
gint bits; |
| 571 |
gint i; |
| 572 |
|
| 573 |
WRITE_UE (bw, pps->pic_size_in_map_units_minus1); |
| 574 |
bits = g_bit_storage (pps->num_slice_groups_minus1); |
| 575 |
|
| 576 |
g_assert (pps->slice_group_id); |
| 577 |
for (i = 0; i <= pps->pic_size_in_map_units_minus1; i++) |
| 578 |
WRITE_BITS (bw, pps->slice_group_id[i], bits); |
| 579 |
} |
| 580 |
} |
| 581 |
|
| 582 |
WRITE_UE_MAX (bw, pps->num_ref_idx_l0_active_minus1, 31); |
| 583 |
WRITE_UE_MAX (bw, pps->num_ref_idx_l1_active_minus1, 31); |
| 584 |
WRITE_BITS (bw, pps->weighted_pred_flag, 1); |
| 585 |
WRITE_BITS (bw, pps->weighted_bipred_idc, 2); |
| 586 |
WRITE_SE_RANGE (bw, pps->pic_init_qp_minus26, -(26 + qp_bd_offset), 25); |
| 587 |
WRITE_SE_RANGE (bw, pps->pic_init_qs_minus26, -26, 25); |
| 588 |
WRITE_SE_RANGE (bw, pps->chroma_qp_index_offset, -12, 12); |
| 589 |
|
| 590 |
WRITE_BITS (bw, pps->deblocking_filter_control_present_flag, 1); |
| 591 |
WRITE_BITS (bw, pps->constrained_intra_pred_flag, 1); |
| 592 |
WRITE_BITS (bw, pps->redundant_pic_cnt_present_flag, 1); |
| 593 |
|
| 594 |
|
| 595 |
|
| 596 |
|
| 597 |
|
| 598 |
|
| 599 |
if (pps->sequence->profile_idc == 66 || |
| 600 |
pps->sequence->profile_idc == 77 || pps->sequence->profile_idc == 88) |
| 601 |
return TRUE; |
| 602 |
|
| 603 |
WRITE_BITS (bw, pps->transform_8x8_mode_flag, 1); |
| 604 |
|
| 605 |
WRITE_BITS (bw, pps->pic_scaling_matrix_present_flag, 1); |
| 606 |
|
| 607 |
if (pps->pic_scaling_matrix_present_flag) { |
| 608 |
guint8 n_lists; |
| 609 |
|
| 610 |
n_lists = 6 + ((pps->sequence->chroma_format_idc != 3) ? 2 : 6) * |
| 611 |
pps->transform_8x8_mode_flag; |
| 612 |
|
| 613 |
if (pps->sequence->scaling_matrix_present_flag) { |
| 614 |
if (!_h264_bit_writer_scaling_list (bw, &have_space, |
| 615 |
pps->scaling_lists_4x4, pps->scaling_lists_8x8, |
| 616 |
pps->sequence->scaling_lists_4x4[3], |
| 617 |
pps->sequence->scaling_lists_4x4[0], |
| 618 |
pps->sequence->scaling_lists_8x8[3], |
| 619 |
pps->sequence->scaling_lists_8x8[0], n_lists)) |
| 620 |
goto error; |
| 621 |
} else { |
| 622 |
if (!_h264_bit_writer_scaling_list (bw, &have_space, |
| 623 |
pps->scaling_lists_4x4, pps->scaling_lists_8x8, |
| 624 |
default_4x4_inter, default_4x4_intra, |
| 625 |
default_8x8_inter, default_8x8_intra, n_lists)) |
| 626 |
goto error; |
| 627 |
} |
| 628 |
} |
| 629 |
|
| 630 |
WRITE_SE_RANGE (bw, pps->second_chroma_qp_index_offset, -12, 12); |
| 631 |
|
| 632 |
*space = TRUE; |
| 633 |
return TRUE; |
| 634 |
|
| 635 |
error: |
| 636 |
GST_WARNING ("error to write pps"); |
| 637 |
|
| 638 |
*space = have_space; |
| 639 |
return FALSE; |
| 640 |
} |
| 641 |
|
| 642 |
|
| 643 |
|
| 644 |
|
| 645 |
|
| 646 |
|
| 647 |
|
| 648 |
|
| 649 |
|
| 650 |
|
| 651 |
|
| 652 |
|
| 653 |
|
| 654 |
|
| 655 |
GstH264BitWriterResult |
| 656 |
gst_h264_bit_writer_pps (const GstH264PPS * pps, gboolean start_code, |
| 657 |
guint8 * data, guint * size) |
| 658 |
{ |
| 659 |
gboolean have_space = TRUE; |
| 660 |
GstBitWriter bw; |
| 661 |
|
| 662 |
g_return_val_if_fail (pps != NULL, GST_H264_BIT_WRITER_ERROR); |
| 663 |
g_return_val_if_fail (pps->sequence != NULL, GST_H264_BIT_WRITER_ERROR); |
| 664 |
g_return_val_if_fail (data != NULL, GST_H264_BIT_WRITER_ERROR); |
| 665 |
g_return_val_if_fail (size != NULL, GST_H264_BIT_WRITER_ERROR); |
| 666 |
g_return_val_if_fail (*size > 0, GST_H264_BIT_WRITER_ERROR); |
| 667 |
|
| 668 |
gst_bit_writer_init_with_data (&bw, data, *size, FALSE); |
| 669 |
|
| 670 |
if (start_code) |
| 671 |
WRITE_BITS (&bw, 0x00000001, 32); |
| 672 |
|
| 673 |
|
| 674 |
|
| 675 |
WRITE_BITS (&bw, 0, 1); |
| 676 |
|
| 677 |
WRITE_BITS (&bw, 1, 2); |
| 678 |
|
| 679 |
WRITE_BITS (&bw, GST_H264_NAL_PPS, 5); |
| 680 |
|
| 681 |
if (!_h264_bit_writer_pps (pps, &bw, &have_space)) |
| 682 |
goto error; |
| 683 |
|
| 684 |
|
| 685 |
WRITE_BITS (&bw, 1, 1); |
| 686 |
if (!gst_bit_writer_align_bytes (&bw, 0)) { |
| 687 |
have_space = FALSE; |
| 688 |
goto error; |
| 689 |
} |
| 690 |
|
| 691 |
*size = (gst_bit_writer_get_size (&bw)) / 8; |
| 692 |
gst_bit_writer_reset (&bw); |
| 693 |
return GST_H264_BIT_WRITER_OK; |
| 694 |
|
| 695 |
error: |
| 696 |
gst_bit_writer_reset (&bw); |
| 697 |
*size = 0; |
| 698 |
|
| 699 |
return have_space ? GST_H264_BIT_WRITER_INVALID_DATA : |
| 700 |
GST_H264_BIT_WRITER_NO_MORE_SPACE; |
| 701 |
} |
| 702 |
|
| 703 |
static gboolean |
| 704 |
_h264_slice_bit_writer_ref_pic_list_modification_1 (const GstH264SliceHdr * |
| 705 |
slice, guint list, gboolean is_mvc, GstBitWriter * bw, gboolean * space) |
| 706 |
{ |
| 707 |
gboolean have_space = TRUE; |
| 708 |
const GstH264RefPicListModification *entries; |
| 709 |
guint8 ref_pic_list_modification_flag = 0; |
| 710 |
guint i; |
| 711 |
|
| 712 |
if (list == 0) { |
| 713 |
entries = slice->ref_pic_list_modification_l0; |
| 714 |
ref_pic_list_modification_flag = slice->ref_pic_list_modification_flag_l0; |
| 715 |
} else { |
| 716 |
entries = slice->ref_pic_list_modification_l1; |
| 717 |
ref_pic_list_modification_flag = slice->ref_pic_list_modification_flag_l1; |
| 718 |
} |
| 719 |
|
| 720 |
WRITE_BITS (bw, ref_pic_list_modification_flag, 1); |
| 721 |
if (ref_pic_list_modification_flag) { |
| 722 |
i = 0; |
| 723 |
do { |
| 724 |
g_assert (i < 32); |
| 725 |
|
| 726 |
WRITE_UE (bw, entries[i].modification_of_pic_nums_idc); |
| 727 |
if (entries[i].modification_of_pic_nums_idc == 0 || |
| 728 |
entries[i].modification_of_pic_nums_idc == 1) { |
| 729 |
WRITE_UE_MAX (bw, entries[i].value.abs_diff_pic_num_minus1, |
| 730 |
slice->max_pic_num - 1); |
| 731 |
} else if (entries[i].modification_of_pic_nums_idc == 2) { |
| 732 |
WRITE_UE (bw, entries[i].value.long_term_pic_num); |
| 733 |
} else if (is_mvc && (entries[i].modification_of_pic_nums_idc == 4 || |
| 734 |
entries[i].modification_of_pic_nums_idc == 5)) { |
| 735 |
WRITE_UE (bw, entries[i].value.abs_diff_view_idx_minus1); |
| 736 |
} |
| 737 |
} while (entries[i++].modification_of_pic_nums_idc != 3); |
| 738 |
} |
| 739 |
|
| 740 |
*space = TRUE; |
| 741 |
return TRUE; |
| 742 |
|
| 743 |
error: |
| 744 |
GST_WARNING ("error to write \"Reference picture list %u modification\"", |
| 745 |
list); |
| 746 |
|
| 747 |
*space = have_space; |
| 748 |
return FALSE; |
| 749 |
} |
| 750 |
|
| 751 |
static gboolean |
| 752 |
_h264_slice_bit_writer_ref_pic_list_modification (const GstH264SliceHdr * |
| 753 |
slice, gboolean is_mvc, GstBitWriter * bw, gboolean * space) |
| 754 |
{ |
| 755 |
if (!GST_H264_IS_I_SLICE (slice) && !GST_H264_IS_SI_SLICE (slice)) { |
| 756 |
if (!_h264_slice_bit_writer_ref_pic_list_modification_1 (slice, 0, |
| 757 |
is_mvc, bw, space)) |
| 758 |
return FALSE; |
| 759 |
} |
| 760 |
|
| 761 |
if (GST_H264_IS_B_SLICE (slice)) { |
| 762 |
if (!_h264_slice_bit_writer_ref_pic_list_modification_1 (slice, 1, |
| 763 |
is_mvc, bw, space)) |
| 764 |
return FALSE; |
| 765 |
} |
| 766 |
|
| 767 |
*space = TRUE; |
| 768 |
return TRUE; |
| 769 |
} |
| 770 |
|
| 771 |
static gboolean |
| 772 |
_h264_slice_bit_writer_pred_weight_table (const GstH264SliceHdr * slice, |
| 773 |
guint8 chroma_array_type, GstBitWriter * bw, gboolean * space) |
| 774 |
{ |
| 775 |
gboolean have_space = TRUE; |
| 776 |
const GstH264PredWeightTable *p; |
| 777 |
gint i; |
| 778 |
gint16 default_luma_weight, default_chroma_weight; |
| 779 |
|
| 780 |
GST_DEBUG ("writing \"Prediction weight table\""); |
| 781 |
|
| 782 |
p = &slice->pred_weight_table; |
| 783 |
default_luma_weight = 1 << p->luma_log2_weight_denom; |
| 784 |
default_chroma_weight = 1 << p->chroma_log2_weight_denom; |
| 785 |
|
| 786 |
WRITE_UE_MAX (bw, p->luma_log2_weight_denom, 7); |
| 787 |
|
| 788 |
if (chroma_array_type != 0) |
| 789 |
WRITE_UE_MAX (bw, p->chroma_log2_weight_denom, 7); |
| 790 |
|
| 791 |
for (i = 0; i <= slice->num_ref_idx_l0_active_minus1; i++) { |
| 792 |
guint8 luma_weight_l0_flag = 0; |
| 793 |
|
| 794 |
if (p->luma_weight_l0[i] != default_luma_weight || |
| 795 |
p->luma_offset_l0[i] != 0) |
| 796 |
luma_weight_l0_flag = 1; |
| 797 |
|
| 798 |
WRITE_BITS (bw, luma_weight_l0_flag, 1); |
| 799 |
if (luma_weight_l0_flag) { |
| 800 |
WRITE_SE_RANGE (bw, p->luma_weight_l0[i], -128, 127); |
| 801 |
WRITE_SE_RANGE (bw, p->luma_offset_l0[i], -128, 127); |
| 802 |
} |
| 803 |
if (chroma_array_type != 0) { |
| 804 |
guint8 chroma_weight_l0_flag = 0; |
| 805 |
gint j; |
| 806 |
|
| 807 |
for (j = 0; j < 2; j++) { |
| 808 |
if (p->chroma_weight_l0[i][j] != default_chroma_weight || |
| 809 |
p->chroma_offset_l0[i][j] != 0) |
| 810 |
chroma_weight_l0_flag = 1; |
| 811 |
} |
| 812 |
|
| 813 |
WRITE_BITS (bw, chroma_weight_l0_flag, 1); |
| 814 |
if (chroma_weight_l0_flag) { |
| 815 |
for (j = 0; j < 2; j++) { |
| 816 |
WRITE_SE_RANGE (bw, p->chroma_weight_l0[i][j], -128, 127); |
| 817 |
WRITE_SE_RANGE (bw, p->chroma_offset_l0[i][j], -128, 127); |
| 818 |
} |
| 819 |
} |
| 820 |
} |
| 821 |
} |
| 822 |
|
| 823 |
if (GST_H264_IS_B_SLICE (slice)) { |
| 824 |
for (i = 0; i <= slice->num_ref_idx_l1_active_minus1; i++) { |
| 825 |
guint8 luma_weight_l1_flag = 0; |
| 826 |
|
| 827 |
if (p->luma_weight_l1[i] != default_luma_weight || |
| 828 |
p->luma_offset_l1[i] != 0) |
| 829 |
luma_weight_l1_flag = 1; |
| 830 |
|
| 831 |
WRITE_BITS (bw, luma_weight_l1_flag, 1); |
| 832 |
if (luma_weight_l1_flag) { |
| 833 |
WRITE_SE_RANGE (bw, p->luma_weight_l1[i], -128, 127); |
| 834 |
WRITE_SE_RANGE (bw, p->luma_offset_l1[i], -128, 127); |
| 835 |
} |
| 836 |
if (chroma_array_type != 0) { |
| 837 |
guint8 chroma_weight_l1_flag = 0; |
| 838 |
gint j; |
| 839 |
|
| 840 |
for (j = 0; j < 2; j++) { |
| 841 |
if (p->chroma_weight_l1[i][j] != default_chroma_weight || |
| 842 |
p->chroma_offset_l1[i][j] != 0) |
| 843 |
chroma_weight_l1_flag = 1; |
| 844 |
} |
| 845 |
|
| 846 |
WRITE_BITS (bw, chroma_weight_l1_flag, 1); |
| 847 |
if (chroma_weight_l1_flag) { |
| 848 |
for (j = 0; j < 2; j++) { |
| 849 |
WRITE_SE_RANGE (bw, p->chroma_weight_l1[i][j], -128, 127); |
| 850 |
WRITE_SE_RANGE (bw, p->chroma_offset_l1[i][j], -128, 127); |
| 851 |
} |
| 852 |
} |
| 853 |
} |
| 854 |
} |
| 855 |
} |
| 856 |
|
| 857 |
*space = TRUE; |
| 858 |
return TRUE; |
| 859 |
|
| 860 |
error: |
| 861 |
GST_WARNING ("error to write \"Prediction weight table\""); |
| 862 |
|
| 863 |
*space = have_space; |
| 864 |
return FALSE; |
| 865 |
} |
| 866 |
|
| 867 |
static gboolean |
| 868 |
_h264_bit_writer_slice_dec_ref_pic_marking (const GstH264SliceHdr * slice, |
| 869 |
guint32 nal_type, GstBitWriter * bw, gboolean * space) |
| 870 |
{ |
| 871 |
gboolean have_space = TRUE; |
| 872 |
|
| 873 |
GST_DEBUG ("writing \"Dec Ref Pic Marking\""); |
| 874 |
|
| 875 |
if (nal_type == GST_H264_NAL_SLICE_IDR) { |
| 876 |
WRITE_BITS (bw, slice->dec_ref_pic_marking.no_output_of_prior_pics_flag, 1); |
| 877 |
WRITE_BITS (bw, slice->dec_ref_pic_marking.long_term_reference_flag, 1); |
| 878 |
} else { |
| 879 |
WRITE_BITS (bw, |
| 880 |
slice->dec_ref_pic_marking.adaptive_ref_pic_marking_mode_flag, 1); |
| 881 |
|
| 882 |
if (slice->dec_ref_pic_marking.adaptive_ref_pic_marking_mode_flag) { |
| 883 |
const GstH264RefPicMarking *refpicmarking; |
| 884 |
guint i; |
| 885 |
|
| 886 |
for (i = 0; i < slice->dec_ref_pic_marking.n_ref_pic_marking; i++) { |
| 887 |
refpicmarking = &slice->dec_ref_pic_marking.ref_pic_marking[i]; |
| 888 |
|
| 889 |
WRITE_UE_MAX (bw, |
| 890 |
refpicmarking->memory_management_control_operation, 6); |
| 891 |
|
| 892 |
if (refpicmarking->memory_management_control_operation == 0) |
| 893 |
break; |
| 894 |
|
| 895 |
if (refpicmarking->memory_management_control_operation == 1 |
| 896 |
|| refpicmarking->memory_management_control_operation == 3) |
| 897 |
WRITE_UE (bw, refpicmarking->difference_of_pic_nums_minus1); |
| 898 |
|
| 899 |
if (refpicmarking->memory_management_control_operation == 2) |
| 900 |
WRITE_UE (bw, refpicmarking->long_term_pic_num); |
| 901 |
|
| 902 |
if (refpicmarking->memory_management_control_operation == 3 |
| 903 |
|| refpicmarking->memory_management_control_operation == 6) |
| 904 |
WRITE_UE (bw, refpicmarking->long_term_frame_idx); |
| 905 |
|
| 906 |
if (refpicmarking->memory_management_control_operation == 4) |
| 907 |
WRITE_UE (bw, refpicmarking->max_long_term_frame_idx_plus1); |
| 908 |
} |
| 909 |
} |
| 910 |
} |
| 911 |
|
| 912 |
*space = TRUE; |
| 913 |
return TRUE; |
| 914 |
|
| 915 |
error: |
| 916 |
GST_WARNING ("error to write \"Dec Ref Pic Marking\""); |
| 917 |
|
| 918 |
*space = have_space; |
| 919 |
return FALSE; |
| 920 |
} |
| 921 |
|
| 922 |
static gboolean |
| 923 |
_h264_bit_writer_slice_hdr (const GstH264SliceHdr * slice, guint32 nal_type, |
| 924 |
guint32 ext_type, gboolean is_ref, GstBitWriter * bw, gboolean * space) |
| 925 |
{ |
| 926 |
gboolean have_space = TRUE; |
| 927 |
|
| 928 |
GST_DEBUG ("writing slice header"); |
| 929 |
|
| 930 |
WRITE_UE (bw, slice->first_mb_in_slice); |
| 931 |
WRITE_UE (bw, slice->type); |
| 932 |
|
| 933 |
WRITE_UE_MAX (bw, slice->pps->id, GST_H264_MAX_PPS_COUNT - 1); |
| 934 |
|
| 935 |
if (slice->pps->sequence->separate_colour_plane_flag) |
| 936 |
WRITE_BITS (bw, slice->colour_plane_id, 2); |
| 937 |
|
| 938 |
WRITE_BITS (bw, slice->frame_num, |
| 939 |
slice->pps->sequence->log2_max_frame_num_minus4 + 4); |
| 940 |
|
| 941 |
if (!slice->pps->sequence->frame_mbs_only_flag) { |
| 942 |
WRITE_BITS (bw, slice->field_pic_flag, 1); |
| 943 |
if (slice->field_pic_flag) |
| 944 |
WRITE_BITS (bw, slice->bottom_field_flag, 1); |
| 945 |
} |
| 946 |
|
| 947 |
if (nal_type == GST_H264_NAL_SLICE_IDR) |
| 948 |
WRITE_UE_MAX (bw, slice->idr_pic_id, G_MAXUINT16); |
| 949 |
|
| 950 |
if (slice->pps->sequence->pic_order_cnt_type == 0) { |
| 951 |
WRITE_BITS (bw, slice->pic_order_cnt_lsb, |
| 952 |
slice->pps->sequence->log2_max_pic_order_cnt_lsb_minus4 + 4); |
| 953 |
|
| 954 |
if (slice->pps->pic_order_present_flag && !slice->field_pic_flag) |
| 955 |
WRITE_SE (bw, slice->delta_pic_order_cnt_bottom); |
| 956 |
} |
| 957 |
|
| 958 |
if (slice->pps->sequence->pic_order_cnt_type == 1 |
| 959 |
&& !slice->pps->sequence->delta_pic_order_always_zero_flag) { |
| 960 |
WRITE_SE (bw, slice->delta_pic_order_cnt[0]); |
| 961 |
if (slice->pps->pic_order_present_flag && !slice->field_pic_flag) |
| 962 |
WRITE_SE (bw, slice->delta_pic_order_cnt[1]); |
| 963 |
} |
| 964 |
|
| 965 |
if (slice->pps->redundant_pic_cnt_present_flag) |
| 966 |
WRITE_UE_MAX (bw, slice->redundant_pic_cnt, G_MAXINT8); |
| 967 |
|
| 968 |
if (GST_H264_IS_B_SLICE (slice)) |
| 969 |
WRITE_BITS (bw, slice->direct_spatial_mv_pred_flag, 1); |
| 970 |
|
| 971 |
if (GST_H264_IS_P_SLICE (slice) || GST_H264_IS_SP_SLICE (slice) || |
| 972 |
GST_H264_IS_B_SLICE (slice)) { |
| 973 |
WRITE_BITS (bw, slice->num_ref_idx_active_override_flag, 1); |
| 974 |
if (slice->num_ref_idx_active_override_flag) { |
| 975 |
WRITE_UE_MAX (bw, slice->num_ref_idx_l0_active_minus1, 31); |
| 976 |
|
| 977 |
if (GST_H264_IS_B_SLICE (slice)) |
| 978 |
WRITE_UE_MAX (bw, slice->num_ref_idx_l1_active_minus1, 31); |
| 979 |
} |
| 980 |
} |
| 981 |
|
| 982 |
if (!_h264_slice_bit_writer_ref_pic_list_modification (slice, |
| 983 |
ext_type == GST_H264_NAL_EXTENSION_MVC, bw, &have_space)) |
| 984 |
goto error; |
| 985 |
|
| 986 |
if ((slice->pps->weighted_pred_flag && (GST_H264_IS_P_SLICE (slice) |
| 987 |
|| GST_H264_IS_SP_SLICE (slice))) |
| 988 |
|| (slice->pps->weighted_bipred_idc == 1 && GST_H264_IS_B_SLICE (slice))) { |
| 989 |
if (!_h264_slice_bit_writer_pred_weight_table (slice, |
| 990 |
slice->pps->sequence->chroma_array_type, bw, &have_space)) |
| 991 |
goto error; |
| 992 |
} |
| 993 |
|
| 994 |
if (is_ref) { |
| 995 |
if (!_h264_bit_writer_slice_dec_ref_pic_marking (slice, nal_type, bw, |
| 996 |
&have_space)) |
| 997 |
goto error; |
| 998 |
} |
| 999 |
|
| 1000 |
if (slice->pps->entropy_coding_mode_flag && !GST_H264_IS_I_SLICE (slice) && |
| 1001 |
!GST_H264_IS_SI_SLICE (slice)) |
| 1002 |
WRITE_UE_MAX (bw, slice->cabac_init_idc, 2); |
| 1003 |
|
| 1004 |
WRITE_SE_RANGE (bw, slice->slice_qp_delta, -87, 77); |
| 1005 |
|
| 1006 |
if (GST_H264_IS_SP_SLICE (slice) || GST_H264_IS_SI_SLICE (slice)) { |
| 1007 |
if (GST_H264_IS_SP_SLICE (slice)) |
| 1008 |
WRITE_BITS (bw, slice->sp_for_switch_flag, 1); |
| 1009 |
|
| 1010 |
WRITE_SE_RANGE (bw, slice->slice_qs_delta, -51, 51); |
| 1011 |
} |
| 1012 |
|
| 1013 |
if (slice->pps->deblocking_filter_control_present_flag) { |
| 1014 |
WRITE_UE_MAX (bw, slice->disable_deblocking_filter_idc, 2); |
| 1015 |
if (slice->disable_deblocking_filter_idc != 1) { |
| 1016 |
WRITE_SE_RANGE (bw, slice->slice_alpha_c0_offset_div2, -6, 6); |
| 1017 |
WRITE_SE_RANGE (bw, slice->slice_beta_offset_div2, -6, 6); |
| 1018 |
} |
| 1019 |
} |
| 1020 |
|
| 1021 |
*space = TRUE; |
| 1022 |
return TRUE; |
| 1023 |
|
| 1024 |
error: |
| 1025 |
GST_WARNING ("error to write slice header"); |
| 1026 |
|
| 1027 |
*space = have_space; |
| 1028 |
return FALSE; |
| 1029 |
} |
| 1030 |
|
| 1031 |
|
| 1032 |
|
| 1033 |
|
| 1034 |
|
| 1035 |
|
| 1036 |
|
| 1037 |
|
| 1038 |
|
| 1039 |
|
| 1040 |
|
| 1041 |
|
| 1042 |
|
| 1043 |
|
| 1044 |
|
| 1045 |
|
| 1046 |
|
| 1047 |
GstH264BitWriterResult |
| 1048 |
gst_h264_bit_writer_slice_hdr (const GstH264SliceHdr * slice, |
| 1049 |
gboolean start_code, GstH264NalUnitType nal_type, gboolean is_ref, |
| 1050 |
guint8 * data, guint * size, guint * trail_bits_num) |
| 1051 |
{ |
| 1052 |
gboolean have_space = TRUE; |
| 1053 |
GstBitWriter bw; |
| 1054 |
|
| 1055 |
g_return_val_if_fail (slice != NULL, GST_H264_BIT_WRITER_ERROR); |
| 1056 |
g_return_val_if_fail (slice->pps != NULL, GST_H264_BIT_WRITER_ERROR); |
| 1057 |
g_return_val_if_fail (slice->pps->sequence != NULL, |
| 1058 |
GST_H264_BIT_WRITER_ERROR); |
| 1059 |
g_return_val_if_fail (nal_type >= GST_H264_NAL_SLICE |
| 1060 |
&& nal_type <= GST_H264_NAL_SLICE_IDR, GST_H264_BIT_WRITER_ERROR); |
| 1061 |
g_return_val_if_fail (data != NULL, GST_H264_BIT_WRITER_ERROR); |
| 1062 |
g_return_val_if_fail (size != NULL, GST_H264_BIT_WRITER_ERROR); |
| 1063 |
g_return_val_if_fail (*size > 0, GST_H264_BIT_WRITER_ERROR); |
| 1064 |
g_return_val_if_fail (trail_bits_num != NULL, GST_H264_BIT_WRITER_ERROR); |
| 1065 |
|
| 1066 |
if (nal_type == GST_H264_NAL_SLICE_IDR) |
| 1067 |
g_return_val_if_fail (is_ref, GST_H264_BIT_WRITER_ERROR); |
| 1068 |
|
| 1069 |
gst_bit_writer_init_with_data (&bw, data, *size, FALSE); |
| 1070 |
|
| 1071 |
if (start_code) |
| 1072 |
WRITE_BITS (&bw, 0x00000001, 32); |
| 1073 |
|
| 1074 |
|
| 1075 |
|
| 1076 |
WRITE_BITS (&bw, 0, 1); |
| 1077 |
|
| 1078 |
WRITE_BITS (&bw, is_ref, 2); |
| 1079 |
|
| 1080 |
WRITE_BITS (&bw, nal_type, 5); |
| 1081 |
|
| 1082 |
if (!_h264_bit_writer_slice_hdr (slice, nal_type, |
| 1083 |
GST_H264_NAL_EXTENSION_NONE, is_ref, &bw, &have_space)) |
| 1084 |
goto error; |
| 1085 |
|
| 1086 |
|
| 1087 |
|
| 1088 |
*size = gst_bit_writer_get_size (&bw) / 8; |
| 1089 |
*trail_bits_num = gst_bit_writer_get_size (&bw) % 8; |
| 1090 |
gst_bit_writer_reset (&bw); |
| 1091 |
return GST_H264_BIT_WRITER_OK; |
| 1092 |
|
| 1093 |
error: |
| 1094 |
gst_bit_writer_reset (&bw); |
| 1095 |
*size = 0; |
| 1096 |
|
| 1097 |
return have_space ? GST_H264_BIT_WRITER_INVALID_DATA : |
| 1098 |
GST_H264_BIT_WRITER_NO_MORE_SPACE; |
| 1099 |
} |
| 1100 |
|
| 1101 |
static gboolean |
| 1102 |
_h264_bit_writer_sei_registered_user_data (const GstH264RegisteredUserData * |
| 1103 |
rud, GstBitWriter * bw, gboolean * space) |
| 1104 |
{ |
| 1105 |
gboolean have_space = TRUE; |
| 1106 |
|
| 1107 |
GST_DEBUG ("Writing \"Registered user data\""); |
| 1108 |
|
| 1109 |
WRITE_BITS (bw, rud->country_code, 8); |
| 1110 |
if (rud->country_code == 0xff) |
| 1111 |
WRITE_BITS (bw, rud->country_code_extension, 8); |
| 1112 |
|
| 1113 |
WRITE_BYTES (bw, rud->data, rud->size); |
| 1114 |
|
| 1115 |
*space = TRUE; |
| 1116 |
return TRUE; |
| 1117 |
|
| 1118 |
error:GST_WARNING ("Failed to write \"Registered user data\""); |
| 1119 |
|
| 1120 |
*space = have_space; |
| 1121 |
return FALSE; |
| 1122 |
} |
| 1123 |
|
| 1124 |
static gboolean |
| 1125 |
_h264_bit_writer_sei_frame_packing (const GstH264FramePacking * |
| 1126 |
frame_packing, GstBitWriter * bw, gboolean * space) |
| 1127 |
{ |
| 1128 |
gboolean have_space = TRUE; |
| 1129 |
|
| 1130 |
GST_DEBUG ("Writing \"Frame packing\""); |
| 1131 |
|
| 1132 |
WRITE_UE (bw, frame_packing->frame_packing_id); |
| 1133 |
WRITE_BITS (bw, frame_packing->frame_packing_cancel_flag, 1); |
| 1134 |
|
| 1135 |
if (!frame_packing->frame_packing_cancel_flag) { |
| 1136 |
WRITE_BITS (bw, frame_packing->frame_packing_type, 7); |
| 1137 |
WRITE_BITS (bw, frame_packing->quincunx_sampling_flag, 1); |
| 1138 |
WRITE_BITS (bw, frame_packing->content_interpretation_type, 6); |
| 1139 |
WRITE_BITS (bw, frame_packing->spatial_flipping_flag, 1); |
| 1140 |
WRITE_BITS (bw, frame_packing->frame0_flipped_flag, 1); |
| 1141 |
WRITE_BITS (bw, frame_packing->field_views_flag, 1); |
| 1142 |
WRITE_BITS (bw, frame_packing->current_frame_is_frame0_flag, 1); |
| 1143 |
WRITE_BITS (bw, frame_packing->frame0_self_contained_flag, 1); |
| 1144 |
WRITE_BITS (bw, frame_packing->frame1_self_contained_flag, 1); |
| 1145 |
|
| 1146 |
if (!frame_packing->quincunx_sampling_flag && |
| 1147 |
frame_packing->frame_packing_type != |
| 1148 |
GST_H264_FRAME_PACKING_TEMPORAL_INTERLEAVING) { |
| 1149 |
WRITE_BITS (bw, frame_packing->frame0_grid_position_x, 4); |
| 1150 |
WRITE_BITS (bw, frame_packing->frame0_grid_position_y, 4); |
| 1151 |
WRITE_BITS (bw, frame_packing->frame1_grid_position_x, 4); |
| 1152 |
WRITE_BITS (bw, frame_packing->frame1_grid_position_y, 4); |
| 1153 |
} |
| 1154 |
|
| 1155 |
|
| 1156 |
WRITE_BITS (bw, 0, 8); |
| 1157 |
WRITE_UE (bw, frame_packing->frame_packing_repetition_period); |
| 1158 |
} |
| 1159 |
|
| 1160 |
|
| 1161 |
WRITE_BITS (bw, 0, 1); |
| 1162 |
|
| 1163 |
*space = TRUE; |
| 1164 |
return TRUE; |
| 1165 |
|
| 1166 |
error: |
| 1167 |
GST_WARNING ("Failed to write \"Frame packing\""); |
| 1168 |
|
| 1169 |
*space = have_space; |
| 1170 |
return FALSE; |
| 1171 |
} |
| 1172 |
|
| 1173 |
static gboolean |
| 1174 |
_h264_bit_writer_sei_mastering_display_colour_volume (const |
| 1175 |
GstH264MasteringDisplayColourVolume * mdcv, GstBitWriter * bw, |
| 1176 |
gboolean * space) |
| 1177 |
{ |
| 1178 |
gboolean have_space = TRUE; |
| 1179 |
gint i; |
| 1180 |
|
| 1181 |
GST_DEBUG ("Wrtiting \"Mastering display colour volume\""); |
| 1182 |
|
| 1183 |
for (i = 0; i < 3; i++) { |
| 1184 |
WRITE_BITS (bw, mdcv->display_primaries_x[i], 16); |
| 1185 |
WRITE_BITS (bw, mdcv->display_primaries_y[i], 16); |
| 1186 |
} |
| 1187 |
|
| 1188 |
WRITE_BITS (bw, mdcv->white_point_x, 16); |
| 1189 |
WRITE_BITS (bw, mdcv->white_point_y, 16); |
| 1190 |
WRITE_BITS (bw, mdcv->max_display_mastering_luminance, 32); |
| 1191 |
WRITE_BITS (bw, mdcv->min_display_mastering_luminance, 32); |
| 1192 |
|
| 1193 |
*space = TRUE; |
| 1194 |
return TRUE; |
| 1195 |
|
| 1196 |
error: |
| 1197 |
GST_WARNING ("Failed to write \"Mastering display colour volume\""); |
| 1198 |
|
| 1199 |
*space = have_space; |
| 1200 |
return FALSE; |
| 1201 |
} |
| 1202 |
|
| 1203 |
static gboolean |
| 1204 |
_h264_bit_writer_sei_content_light_level_info (const |
| 1205 |
GstH264ContentLightLevel * cll, GstBitWriter * bw, gboolean * space) |
| 1206 |
{ |
| 1207 |
gboolean have_space = TRUE; |
| 1208 |
|
| 1209 |
GST_DEBUG ("Writing \"Content light level\""); |
| 1210 |
|
| 1211 |
WRITE_BITS (bw, cll->max_content_light_level, 16); |
| 1212 |
WRITE_BITS (bw, cll->max_pic_average_light_level, 16); |
| 1213 |
|
| 1214 |
*space = TRUE; |
| 1215 |
return TRUE; |
| 1216 |
|
| 1217 |
error: |
| 1218 |
GST_WARNING ("Failed to write \"Content light level\""); |
| 1219 |
|
| 1220 |
*space = have_space; |
| 1221 |
return FALSE; |
| 1222 |
} |
| 1223 |
|
| 1224 |
static gboolean |
| 1225 |
_h264_bit_writer_sei_pic_timing (const GstH264PicTiming * tim, |
| 1226 |
GstBitWriter * bw, gboolean * space) |
| 1227 |
{ |
| 1228 |
gboolean have_space = TRUE; |
| 1229 |
|
| 1230 |
GST_DEBUG ("Writing \"Picture timing\""); |
| 1231 |
|
| 1232 |
if (tim->CpbDpbDelaysPresentFlag) { |
| 1233 |
WRITE_BITS (bw, tim->cpb_removal_delay, |
| 1234 |
tim->cpb_removal_delay_length_minus1 + 1); |
| 1235 |
WRITE_BITS (bw, tim->dpb_output_delay, |
| 1236 |
tim->dpb_output_delay_length_minus1 + 1); |
| 1237 |
} |
| 1238 |
|
| 1239 |
if (tim->pic_struct_present_flag) { |
| 1240 |
const guint8 num_clock_ts_table[9] = { |
| 1241 |
1, 1, 1, 2, 2, 3, 3, 2, 3 |
| 1242 |
}; |
| 1243 |
guint8 num_clock_num_ts; |
| 1244 |
guint i; |
| 1245 |
|
| 1246 |
WRITE_BITS (bw, tim->pic_struct, 4); |
| 1247 |
|
| 1248 |
num_clock_num_ts = num_clock_ts_table[tim->pic_struct]; |
| 1249 |
for (i = 0; i < num_clock_num_ts; i++) { |
| 1250 |
WRITE_BITS (bw, tim->clock_timestamp_flag[i], 1); |
| 1251 |
if (tim->clock_timestamp_flag[i]) { |
| 1252 |
const GstH264ClockTimestamp *timestamp = &tim->clock_timestamp[i]; |
| 1253 |
|
| 1254 |
WRITE_BITS (bw, timestamp->ct_type, 2); |
| 1255 |
WRITE_BITS (bw, timestamp->nuit_field_based_flag, 1); |
| 1256 |
WRITE_BITS (bw, timestamp->counting_type, 5); |
| 1257 |
WRITE_BITS (bw, timestamp->full_timestamp_flag, 1); |
| 1258 |
WRITE_BITS (bw, timestamp->discontinuity_flag, 1); |
| 1259 |
WRITE_BITS (bw, timestamp->cnt_dropped_flag, 1); |
| 1260 |
WRITE_BITS (bw, timestamp->n_frames, 8); |
| 1261 |
|
| 1262 |
if (timestamp->full_timestamp_flag) { |
| 1263 |
if (!timestamp->seconds_flag || !timestamp->minutes_flag |
| 1264 |
|| !timestamp->hours_flag) |
| 1265 |
goto error; |
| 1266 |
|
| 1267 |
WRITE_BITS (bw, timestamp->seconds_value, 6); |
| 1268 |
WRITE_BITS (bw, timestamp->minutes_value, 6); |
| 1269 |
WRITE_BITS (bw, timestamp->hours_value, 5); |
| 1270 |
} else { |
| 1271 |
WRITE_BITS (bw, timestamp->seconds_flag, 1); |
| 1272 |
if (timestamp->seconds_flag) { |
| 1273 |
WRITE_BITS (bw, timestamp->seconds_value, 6); |
| 1274 |
WRITE_BITS (bw, timestamp->minutes_flag, 1); |
| 1275 |
if (timestamp->minutes_flag) { |
| 1276 |
WRITE_BITS (bw, timestamp->minutes_value, 6); |
| 1277 |
WRITE_BITS (bw, timestamp->hours_flag, 1); |
| 1278 |
if (timestamp->hours_flag) |
| 1279 |
WRITE_BITS (bw, timestamp->hours_value, 5); |
| 1280 |
} |
| 1281 |
} |
| 1282 |
} |
| 1283 |
|
| 1284 |
if (tim->time_offset_length > 0) { |
| 1285 |
WRITE_BITS (bw, timestamp->time_offset, tim->time_offset_length); |
| 1286 |
} |
| 1287 |
} |
| 1288 |
} |
| 1289 |
} |
| 1290 |
|
| 1291 |
*space = TRUE; |
| 1292 |
return TRUE; |
| 1293 |
|
| 1294 |
error: |
| 1295 |
GST_WARNING ("Failed to write \"Picture timing\""); |
| 1296 |
|
| 1297 |
*space = have_space; |
| 1298 |
return FALSE; |
| 1299 |
} |
| 1300 |
|
| 1301 |
static gboolean |
| 1302 |
_h264_bit_writer_sei_buffering_period (const GstH264BufferingPeriod * per, |
| 1303 |
GstBitWriter * bw, gboolean * space) |
| 1304 |
{ |
| 1305 |
gboolean have_space = TRUE; |
| 1306 |
|
| 1307 |
GST_DEBUG ("Writing \"Buffering period\""); |
| 1308 |
|
| 1309 |
if (!per->sps) |
| 1310 |
goto error; |
| 1311 |
|
| 1312 |
WRITE_UE_MAX (bw, per->sps->id, GST_H264_MAX_SPS_COUNT - 1); |
| 1313 |
|
| 1314 |
if (per->sps->vui_parameters_present_flag) { |
| 1315 |
GstH264VUIParams *vui = &per->sps->vui_parameters; |
| 1316 |
|
| 1317 |
if (vui->nal_hrd_parameters_present_flag) { |
| 1318 |
GstH264HRDParams *hrd = &vui->nal_hrd_parameters; |
| 1319 |
const guint8 nbits = hrd->initial_cpb_removal_delay_length_minus1 + 1; |
| 1320 |
guint8 sched_sel_idx; |
| 1321 |
|
| 1322 |
for (sched_sel_idx = 0; sched_sel_idx <= hrd->cpb_cnt_minus1; |
| 1323 |
sched_sel_idx++) { |
| 1324 |
WRITE_BITS (bw, per->nal_initial_cpb_removal_delay[sched_sel_idx], |
| 1325 |
nbits); |
| 1326 |
WRITE_BITS (bw, |
| 1327 |
per->nal_initial_cpb_removal_delay_offset[sched_sel_idx], nbits); |
| 1328 |
} |
| 1329 |
} |
| 1330 |
|
| 1331 |
if (vui->vcl_hrd_parameters_present_flag) { |
| 1332 |
GstH264HRDParams *hrd = &vui->vcl_hrd_parameters; |
| 1333 |
const guint8 nbits = hrd->initial_cpb_removal_delay_length_minus1 + 1; |
| 1334 |
guint8 sched_sel_idx; |
| 1335 |
|
| 1336 |
for (sched_sel_idx = 0; sched_sel_idx <= hrd->cpb_cnt_minus1; |
| 1337 |
sched_sel_idx++) { |
| 1338 |
WRITE_BITS (bw, per->vcl_initial_cpb_removal_delay[sched_sel_idx], |
| 1339 |
nbits); |
| 1340 |
WRITE_BITS (bw, |
| 1341 |
per->vcl_initial_cpb_removal_delay_offset[sched_sel_idx], nbits); |
| 1342 |
} |
| 1343 |
} |
| 1344 |
} |
| 1345 |
|
| 1346 |
*space = TRUE; |
| 1347 |
return TRUE; |
| 1348 |
|
| 1349 |
error: |
| 1350 |
GST_WARNING ("Failed to write \"Buffering period\""); |
| 1351 |
|
| 1352 |
*space = have_space; |
| 1353 |
return FALSE; |
| 1354 |
} |
| 1355 |
|
| 1356 |
static gboolean |
| 1357 |
_h264_bit_writer_sei_message (const GstH264SEIMessage * msg, |
| 1358 |
GstBitWriter * bw, gboolean * space) |
| 1359 |
{ |
| 1360 |
gboolean have_space = TRUE; |
| 1361 |
|
| 1362 |
GST_DEBUG ("writing SEI message"); |
| 1363 |
|
| 1364 |
switch (msg->payloadType) { |
| 1365 |
case GST_H264_SEI_REGISTERED_USER_DATA: |
| 1366 |
if (!_h264_bit_writer_sei_registered_user_data |
| 1367 |
(&msg->payload.registered_user_data, bw, &have_space)) |
| 1368 |
goto error; |
| 1369 |
break; |
| 1370 |
case GST_H264_SEI_FRAME_PACKING: |
| 1371 |
if (!_h264_bit_writer_sei_frame_packing |
| 1372 |
(&msg->payload.frame_packing, bw, &have_space)) |
| 1373 |
goto error; |
| 1374 |
break; |
| 1375 |
case GST_H264_SEI_MASTERING_DISPLAY_COLOUR_VOLUME: |
| 1376 |
if (!_h264_bit_writer_sei_mastering_display_colour_volume |
| 1377 |
(&msg->payload.mastering_display_colour_volume, bw, &have_space)) |
| 1378 |
goto error; |
| 1379 |
break; |
| 1380 |
case GST_H264_SEI_CONTENT_LIGHT_LEVEL: |
| 1381 |
if (!_h264_bit_writer_sei_content_light_level_info |
| 1382 |
(&msg->payload.content_light_level, bw, &have_space)) |
| 1383 |
goto error; |
| 1384 |
break; |
| 1385 |
case GST_H264_SEI_PIC_TIMING: |
| 1386 |
if (!_h264_bit_writer_sei_pic_timing (&msg->payload.pic_timing, bw, |
| 1387 |
&have_space)) |
| 1388 |
goto error; |
| 1389 |
break; |
| 1390 |
case GST_H264_SEI_BUF_PERIOD: |
| 1391 |
if (!_h264_bit_writer_sei_buffering_period |
| 1392 |
(&msg->payload.buffering_period, bw, &have_space)) |
| 1393 |
goto error; |
| 1394 |
break; |
| 1395 |
default: |
| 1396 |
break; |
| 1397 |
} |
| 1398 |
|
| 1399 |
|
| 1400 |
WRITE_BITS (bw, 1, 1); |
| 1401 |
gst_bit_writer_align_bytes_unchecked (bw, 0); |
| 1402 |
|
| 1403 |
*space = TRUE; |
| 1404 |
|
| 1405 |
return TRUE; |
| 1406 |
|
| 1407 |
error: |
| 1408 |
GST_WARNING ("error to write SEI message"); |
| 1409 |
|
| 1410 |
*space = have_space; |
| 1411 |
return FALSE; |
| 1412 |
} |
| 1413 |
|
| 1414 |
|
| 1415 |
|
| 1416 |
|
| 1417 |
|
| 1418 |
|
| 1419 |
|
| 1420 |
|
| 1421 |
|
| 1422 |
|
| 1423 |
|
| 1424 |
|
| 1425 |
|
| 1426 |
|
| 1427 |
GstH264BitWriterResult |
| 1428 |
gst_h264_bit_writer_sei (GArray * sei_messages, gboolean start_code, |
| 1429 |
guint8 * data, guint * size) |
| 1430 |
{ |
| 1431 |
gboolean have_space = TRUE; |
| 1432 |
GstBitWriter bw; |
| 1433 |
GstBitWriter bw_msg; |
| 1434 |
GstH264SEIMessage *sei; |
| 1435 |
gboolean have_written_data = FALSE; |
| 1436 |
guint i; |
| 1437 |
|
| 1438 |
g_return_val_if_fail (sei_messages != NULL, GST_H264_BIT_WRITER_ERROR); |
| 1439 |
g_return_val_if_fail (data != NULL, GST_H264_BIT_WRITER_ERROR); |
| 1440 |
g_return_val_if_fail (size != NULL, GST_H264_BIT_WRITER_ERROR); |
| 1441 |
g_return_val_if_fail (*size > 0, GST_H264_BIT_WRITER_ERROR); |
| 1442 |
|
| 1443 |
gst_bit_writer_init_with_data (&bw, data, *size, FALSE); |
| 1444 |
|
| 1445 |
if (start_code) |
| 1446 |
WRITE_BITS (&bw, 0x00000001, 32); |
| 1447 |
|
| 1448 |
|
| 1449 |
|
| 1450 |
WRITE_BITS (&bw, 0, 1); |
| 1451 |
|
| 1452 |
WRITE_BITS (&bw, 0, 2); |
| 1453 |
|
| 1454 |
WRITE_BITS (&bw, GST_H264_NAL_SEI, 5); |
| 1455 |
|
| 1456 |
for (i = 0; i < sei_messages->len; i++) { |
| 1457 |
guint32 payload_size_data; |
| 1458 |
guint32 payload_type_data; |
| 1459 |
guint32 sz; |
| 1460 |
|
| 1461 |
gst_bit_writer_init (&bw_msg); |
| 1462 |
|
| 1463 |
sei = &g_array_index (sei_messages, GstH264SEIMessage, i); |
| 1464 |
if (!_h264_bit_writer_sei_message (sei, &bw_msg, &have_space)) |
| 1465 |
goto error; |
| 1466 |
|
| 1467 |
if (gst_bit_writer_get_size (&bw_msg) == 0) { |
| 1468 |
GST_FIXME ("Unsupported SEI type %d", sei->payloadType); |
| 1469 |
continue; |
| 1470 |
} |
| 1471 |
|
| 1472 |
have_written_data = TRUE; |
| 1473 |
|
| 1474 |
g_assert (gst_bit_writer_get_size (&bw_msg) % 8 == 0); |
| 1475 |
payload_size_data = gst_bit_writer_get_size (&bw_msg) / 8; |
| 1476 |
payload_type_data = sei->payloadType; |
| 1477 |
|
| 1478 |
|
| 1479 |
while (payload_type_data >= 0xff) { |
| 1480 |
WRITE_BITS (&bw, 0xff, 8); |
| 1481 |
payload_type_data -= 0xff; |
| 1482 |
} |
| 1483 |
WRITE_BITS (&bw, payload_type_data, 8); |
| 1484 |
|
| 1485 |
|
| 1486 |
sz = payload_size_data; |
| 1487 |
while (sz >= 0xff) { |
| 1488 |
WRITE_BITS (&bw, 0xff, 8); |
| 1489 |
sz -= 0xff; |
| 1490 |
} |
| 1491 |
WRITE_BITS (&bw, sz, 8); |
| 1492 |
|
| 1493 |
if (payload_size_data > 0) |
| 1494 |
WRITE_BYTES (&bw, gst_bit_writer_get_data (&bw_msg), payload_size_data); |
| 1495 |
|
| 1496 |
gst_bit_writer_reset (&bw_msg); |
| 1497 |
} |
| 1498 |
|
| 1499 |
if (!have_written_data) { |
| 1500 |
GST_WARNING ("No written sei data"); |
| 1501 |
goto error; |
| 1502 |
} |
| 1503 |
|
| 1504 |
|
| 1505 |
WRITE_BITS (&bw, 1, 1); |
| 1506 |
if (!gst_bit_writer_align_bytes (&bw, 0)) { |
| 1507 |
have_space = FALSE; |
| 1508 |
goto error; |
| 1509 |
} |
| 1510 |
|
| 1511 |
*size = (gst_bit_writer_get_size (&bw)) / 8; |
| 1512 |
gst_bit_writer_reset (&bw); |
| 1513 |
return GST_H264_BIT_WRITER_OK; |
| 1514 |
|
| 1515 |
error: |
| 1516 |
gst_bit_writer_reset (&bw); |
| 1517 |
*size = 0; |
| 1518 |
|
| 1519 |
return have_space ? GST_H264_BIT_WRITER_INVALID_DATA : |
| 1520 |
GST_H264_BIT_WRITER_NO_MORE_SPACE; |
| 1521 |
} |
| 1522 |
|
| 1523 |
|
| 1524 |
|
| 1525 |
|
| 1526 |
|
| 1527 |
|
| 1528 |
|
| 1529 |
|
| 1530 |
|
| 1531 |
|
| 1532 |
|
| 1533 |
|
| 1534 |
|
| 1535 |
|
| 1536 |
|
| 1537 |
GstH264BitWriterResult |
| 1538 |
gst_h264_bit_writer_aud (guint8 primary_pic_type, gboolean start_code, |
| 1539 |
guint8 * data, guint * size) |
| 1540 |
{ |
| 1541 |
gboolean have_space = TRUE; |
| 1542 |
GstBitWriter bw; |
| 1543 |
|
| 1544 |
g_return_val_if_fail (primary_pic_type <= 7, GST_H264_BIT_WRITER_ERROR); |
| 1545 |
g_return_val_if_fail (data != NULL, GST_H264_BIT_WRITER_ERROR); |
| 1546 |
g_return_val_if_fail (size != NULL, GST_H264_BIT_WRITER_ERROR); |
| 1547 |
g_return_val_if_fail (*size > 0, GST_H264_BIT_WRITER_ERROR); |
| 1548 |
|
| 1549 |
gst_bit_writer_init_with_data (&bw, data, *size, FALSE); |
| 1550 |
|
| 1551 |
if (start_code) |
| 1552 |
WRITE_BITS (&bw, 0x00000001, 32); |
| 1553 |
|
| 1554 |
|
| 1555 |
|
| 1556 |
WRITE_BITS (&bw, 0, 1); |
| 1557 |
|
| 1558 |
WRITE_BITS (&bw, 0, 2); |
| 1559 |
|
| 1560 |
WRITE_BITS (&bw, GST_H264_NAL_AU_DELIMITER, 5); |
| 1561 |
|
| 1562 |
WRITE_BITS (&bw, primary_pic_type, 3); |
| 1563 |
|
| 1564 |
|
| 1565 |
WRITE_BITS (&bw, 1, 1); |
| 1566 |
if (!gst_bit_writer_align_bytes (&bw, 0)) { |
| 1567 |
have_space = FALSE; |
| 1568 |
goto error; |
| 1569 |
} |
| 1570 |
|
| 1571 |
*size = (gst_bit_writer_get_size (&bw)) / 8; |
| 1572 |
gst_bit_writer_reset (&bw); |
| 1573 |
|
| 1574 |
return GST_H264_BIT_WRITER_OK; |
| 1575 |
|
| 1576 |
error: |
| 1577 |
gst_bit_writer_reset (&bw); |
| 1578 |
*size = 0; |
| 1579 |
|
| 1580 |
return have_space ? GST_H264_BIT_WRITER_INVALID_DATA : |
| 1581 |
GST_H264_BIT_WRITER_NO_MORE_SPACE; |
| 1582 |
} |
| 1583 |
|
| 1584 |
|
| 1585 |
|
| 1586 |
|
| 1587 |
|
| 1588 |
|
| 1589 |
|
| 1590 |
|
| 1591 |
|
| 1592 |
|
| 1593 |
|
| 1594 |
|
| 1595 |
|
| 1596 |
|
| 1597 |
|
| 1598 |
|
| 1599 |
|
| 1600 |
|
| 1601 |
|
| 1602 |
|
| 1603 |
|
| 1604 |
|
| 1605 |
|
| 1606 |
|
| 1607 |
|
| 1608 |
GstH264BitWriterResult |
| 1609 |
gst_h264_bit_writer_convert_to_nal (guint nal_prefix_size, gboolean packetized, |
| 1610 |
gboolean has_startcode, gboolean add_trailings, const guint8 * raw_data, |
| 1611 |
gsize raw_size, guint8 * nal_data, guint * nal_size) |
| 1612 |
{ |
| 1613 |
NalWriter nw; |
| 1614 |
guint8 *data; |
| 1615 |
guint32 size = 0; |
| 1616 |
gboolean need_more_space = FALSE; |
| 1617 |
|
| 1618 |
g_return_val_if_fail ( |
| 1619 |
(packetized && nal_prefix_size > 1 && nal_prefix_size < 5) || |
| 1620 |
(!packetized && (nal_prefix_size == 3 || nal_prefix_size == 4)), |
| 1621 |
GST_H264_BIT_WRITER_ERROR); |
| 1622 |
g_return_val_if_fail (raw_data != NULL, GST_H264_BIT_WRITER_ERROR); |
| 1623 |
g_return_val_if_fail (raw_size > 0, GST_H264_BIT_WRITER_ERROR); |
| 1624 |
g_return_val_if_fail (raw_size / 8 <= G_MAXUINT, GST_H264_BIT_WRITER_ERROR); |
| 1625 |
g_return_val_if_fail (nal_data != NULL, GST_H264_BIT_WRITER_ERROR); |
| 1626 |
g_return_val_if_fail (nal_size != NULL, GST_H264_BIT_WRITER_ERROR); |
| 1627 |
g_return_val_if_fail (*nal_size > 0, GST_H264_BIT_WRITER_ERROR); |
| 1628 |
|
| 1629 |
if (has_startcode) { |
| 1630 |
|
| 1631 |
if (raw_size >= 4 && raw_data[0] == 0 |
| 1632 |
&& raw_data[1] == 0 && raw_data[2] == 0 && raw_data[3] == 0x01) { |
| 1633 |
raw_data += 4; |
| 1634 |
raw_size -= 4 * 8; |
| 1635 |
} else if (raw_size >= 3 && raw_data[0] == 0 && raw_data[1] == 0 |
| 1636 |
&& raw_data[2] == 0x01) { |
| 1637 |
raw_data += 3; |
| 1638 |
raw_size -= 3 * 8; |
| 1639 |
} else { |
| 1640 |
|
| 1641 |
g_return_val_if_reached (GST_H264_BIT_WRITER_ERROR); |
| 1642 |
} |
| 1643 |
} |
| 1644 |
|
| 1645 |
|
| 1646 |
|
| 1647 |
if (!add_trailings) |
| 1648 |
g_return_val_if_fail (raw_size % 8 == 0, GST_H264_BIT_WRITER_ERROR); |
| 1649 |
|
| 1650 |
nal_writer_init (&nw, nal_prefix_size, packetized); |
| 1651 |
|
| 1652 |
if (!nal_writer_put_bytes (&nw, raw_data, (guint) (raw_size / 8))) |
| 1653 |
goto error; |
| 1654 |
|
| 1655 |
if (raw_size % 8) { |
| 1656 |
guint8 data = *(raw_data + raw_size / 8); |
| 1657 |
|
| 1658 |
if (!nal_writer_put_bits_uint8 (&nw, |
| 1659 |
data >> (8 - raw_size % 8), raw_size % 8)) |
| 1660 |
goto error; |
| 1661 |
} |
| 1662 |
|
| 1663 |
if (add_trailings) { |
| 1664 |
if (!nal_writer_do_rbsp_trailing_bits (&nw)) |
| 1665 |
goto error; |
| 1666 |
} |
| 1667 |
|
| 1668 |
data = nal_writer_reset_and_get_data (&nw, &size); |
| 1669 |
if (!data) |
| 1670 |
goto error; |
| 1671 |
|
| 1672 |
if (size > *nal_size) { |
| 1673 |
need_more_space = TRUE; |
| 1674 |
g_free (data); |
| 1675 |
goto error; |
| 1676 |
} |
| 1677 |
|
| 1678 |
memcpy (nal_data, data, size); |
| 1679 |
*nal_size = size; |
| 1680 |
g_free (data); |
| 1681 |
nal_writer_reset (&nw); |
| 1682 |
return GST_H264_BIT_WRITER_OK; |
| 1683 |
|
| 1684 |
error: |
| 1685 |
nal_writer_reset (&nw); |
| 1686 |
*nal_size = 0; |
| 1687 |
|
| 1688 |
GST_WARNING ("Failed to convert nal data"); |
| 1689 |
|
| 1690 |
return need_more_space ? GST_H264_BIT_WRITER_INVALID_DATA : |
| 1691 |
GST_H264_BIT_WRITER_NO_MORE_SPACE; |
| 1692 |
} |
| 1693 |
|
| 1694 |
|
| 1695 |
|
| 1696 |
|
| 1697 |
|
| 1698 |
|
| 1699 |
|
| 1700 |
|
| 1701 |
|
| 1702 |
|
| 1703 |
|
| 1704 |
|
| 1705 |
|
| 1706 |
|
| 1707 |
GstH264BitWriterResult |
| 1708 |
gst_h264_bit_writer_filler (gboolean start_code, guint num, guint8 * data, |
| 1709 |
guint * size) |
| 1710 |
{ |
| 1711 |
gboolean have_space = TRUE; |
| 1712 |
GstBitWriter bw; |
| 1713 |
guint i; |
| 1714 |
|
| 1715 |
g_return_val_if_fail (data != NULL, GST_H264_BIT_WRITER_ERROR); |
| 1716 |
g_return_val_if_fail (size != NULL, GST_H264_BIT_WRITER_ERROR); |
| 1717 |
g_return_val_if_fail (*size > 0, GST_H264_BIT_WRITER_ERROR); |
| 1718 |
|
| 1719 |
gst_bit_writer_init_with_data (&bw, data, *size, FALSE); |
| 1720 |
|
| 1721 |
if (start_code) |
| 1722 |
WRITE_BITS (&bw, 0x00000001, 32); |
| 1723 |
|
| 1724 |
|
| 1725 |
|
| 1726 |
WRITE_BITS (&bw, 0, 1); |
| 1727 |
|
| 1728 |
WRITE_BITS (&bw, 0, 2); |
| 1729 |
|
| 1730 |
WRITE_BITS (&bw, GST_H264_NAL_FILLER_DATA, 5); |
| 1731 |
|
| 1732 |
for (i = 0; i < num; i++) |
| 1733 |
WRITE_BITS (&bw, 0xff, 8); |
| 1734 |
|
| 1735 |
|
| 1736 |
WRITE_BITS (&bw, 1, 1); |
| 1737 |
if (!gst_bit_writer_align_bytes (&bw, 0)) { |
| 1738 |
have_space = FALSE; |
| 1739 |
goto error; |
| 1740 |
} |
| 1741 |
|
| 1742 |
*size = (gst_bit_writer_get_size (&bw)) / 8; |
| 1743 |
gst_bit_writer_reset (&bw); |
| 1744 |
|
| 1745 |
return GST_H264_BIT_WRITER_OK; |
| 1746 |
|
| 1747 |
error: |
| 1748 |
gst_bit_writer_reset (&bw); |
| 1749 |
*size = 0; |
| 1750 |
|
| 1751 |
return have_space ? GST_H264_BIT_WRITER_INVALID_DATA : |
| 1752 |
GST_H264_BIT_WRITER_NO_MORE_SPACE; |
| 1753 |
} |