gstutils: add gst_util_ceil_log2

Move ceil_log2 from nalutils.* to gstutils.*

Part-of: <https://gitlab.freedesktop.org/gstreamer/gstreamer/-/merge_requests/5888>
This commit is contained in:
Stéphane Cerveau 2024-01-05 14:32:00 +01:00 committed by GStreamer Marge Bot
parent a242b93f33
commit 97ebaa0bc1
8 changed files with 63 additions and 34 deletions

View file

@ -54222,6 +54222,20 @@ element or %NULL if nothing was found</doc>
</parameter>
</parameters>
</function>
<function name="util_ceil_log2" c:identifier="gst_util_ceil_log2" version="1.24">
<doc xml:space="preserve" filename="../subprojects/gstreamer/gst/gstutils.c">Return a max num of log2.</doc>
<source-position filename="../subprojects/gstreamer/gst/gstutils.h"/>
<return-value transfer-ownership="none">
<doc xml:space="preserve" filename="../subprojects/gstreamer/gst/gstutils.c">a computed #guint val.</doc>
<type name="guint" c:type="guint"/>
</return-value>
<parameters>
<parameter name="v" transfer-ownership="none">
<doc xml:space="preserve" filename="../subprojects/gstreamer/gst/gstutils.c">a #guint32 value.</doc>
<type name="guint32" c:type="guint32"/>
</parameter>
</parameters>
</function>
<function name="util_double_to_fraction" c:identifier="gst_util_double_to_fraction">
<doc xml:space="preserve" filename="../subprojects/gstreamer/gst/gstutils.c">Transforms a #gdouble to a fraction and simplifies
the result.</doc>

View file

@ -2432,7 +2432,8 @@ gst_h264_parser_parse_slice_hdr (GstH264NalParser * nalparser,
guint32 PicHeightInMapUnits = sps->pic_height_in_map_units_minus1 + 1;
guint32 PicSizeInMapUnits = PicWidthInMbs * PicHeightInMapUnits;
guint32 SliceGroupChangeRate = pps->slice_group_change_rate_minus1 + 1;
const guint n = ceil_log2 (PicSizeInMapUnits / SliceGroupChangeRate + 1);
const guint n =
gst_util_ceil_log2 (PicSizeInMapUnits / SliceGroupChangeRate + 1);
READ_UINT16 (&nr, slice->slice_group_change_cycle, n);
}

View file

@ -1411,7 +1411,7 @@ _h265_slice_bit_writer_ref_pic_list_modification (const GstH265SliceHdr *
guint i;
const GstH265RefPicListModification *rpl_mod =
&slice->ref_pic_list_modification;
const guint n = ceil_log2 (NumPocTotalCurr);
const guint n = gst_util_ceil_log2 (NumPocTotalCurr);
WRITE_BITS (bw, rpl_mod->ref_pic_list_modification_flag_l0, 1);
@ -1551,7 +1551,7 @@ _h265_bit_writer_slice_header (const GstH265SliceHdr * slice,
ceil ((gdouble) sps->pic_width_in_luma_samples / (gdouble) CtbSizeY);
PicSizeInCtbsY = PicWidthInCtbsY * PicHeightInCtbsY;
n = ceil_log2 (PicSizeInCtbsY);
n = gst_util_ceil_log2 (PicSizeInCtbsY);
if (slice->pps->dependent_slice_segments_enabled_flag)
WRITE_BITS (bw, slice->dependent_slice_segment_flag, 1);
@ -1584,7 +1584,7 @@ _h265_bit_writer_slice_header (const GstH265SliceHdr * slice,
slice->pps->sps, bw, &have_space))
goto error;
} else if (sps->num_short_term_ref_pic_sets > 1) {
const guint n = ceil_log2 (sps->num_short_term_ref_pic_sets);
const guint n = gst_util_ceil_log2 (sps->num_short_term_ref_pic_sets);
if (slice->short_term_ref_pic_set_idx >
sps->num_short_term_ref_pic_sets - 1)
@ -1605,7 +1605,8 @@ _h265_bit_writer_slice_header (const GstH265SliceHdr * slice,
for (i = 0; i < limit; i++) {
if (i < slice->num_long_term_sps) {
if (sps->num_long_term_ref_pics_sps > 1) {
const guint n = ceil_log2 (sps->num_long_term_ref_pics_sps);
const guint n =
gst_util_ceil_log2 (sps->num_long_term_ref_pics_sps);
WRITE_BITS (bw, slice->lt_idx_sps[i], n);
}
} else {

View file

@ -920,7 +920,7 @@ gst_h265_slice_parse_ref_pic_list_modification (GstH265SliceHdr * slice,
{
guint i;
GstH265RefPicListModification *rpl_mod = &slice->ref_pic_list_modification;
const guint n = ceil_log2 (NumPocTotalCurr);
const guint n = gst_util_ceil_log2 (NumPocTotalCurr);
READ_UINT8 (nr, rpl_mod->ref_pic_list_modification_flag_l0, 1);
@ -2797,7 +2797,7 @@ gst_h265_parser_parse_slice_hdr (GstH265Parser * parser,
pps->loop_filter_across_slices_enabled_flag;
if (!slice->first_slice_segment_in_pic_flag) {
const guint n = ceil_log2 (PicSizeInCtbsY);
const guint n = gst_util_ceil_log2 (PicSizeInCtbsY);
if (pps->dependent_slice_segments_enabled_flag)
READ_UINT8 (&nr, slice->dependent_slice_segment_flag, 1);
@ -2835,7 +2835,7 @@ gst_h265_parser_parse_slice_hdr (GstH265Parser * parser,
(nal_reader_get_pos (&nr) - pos) -
(8 * (nal_reader_get_epb_count (&nr) - epb_pos));
} else if (sps->num_short_term_ref_pic_sets > 1) {
const guint n = ceil_log2 (sps->num_short_term_ref_pic_sets);
const guint n = gst_util_ceil_log2 (sps->num_short_term_ref_pic_sets);
READ_UINT8 (&nr, slice->short_term_ref_pic_set_idx, n);
CHECK_ALLOWED_MAX (slice->short_term_ref_pic_set_idx,
sps->num_short_term_ref_pic_sets - 1);
@ -2855,7 +2855,8 @@ gst_h265_parser_parse_slice_hdr (GstH265Parser * parser,
for (i = 0; i < limit; i++) {
if (i < slice->num_long_term_sps) {
if (sps->num_long_term_ref_pics_sps > 1) {
const guint n = ceil_log2 (sps->num_long_term_ref_pics_sps);
const guint n =
gst_util_ceil_log2 (sps->num_long_term_ref_pics_sps);
READ_UINT8 (&nr, slice->lt_idx_sps[i], n);
}
} else {

View file

@ -38,29 +38,6 @@
#include "nalutils.h"
#include <string.h>
/* Compute Ceil(Log2(v)) */
/* Derived from branchless code for integer log2(v) from:
<http://graphics.stanford.edu/~seander/bithacks.html#IntegerLog> */
guint
ceil_log2 (guint32 v)
{
guint r, shift;
v--;
r = (v > 0xFFFF) << 4;
v >>= r;
shift = (v > 0xFF) << 3;
v >>= shift;
r |= shift;
shift = (v > 0xF) << 2;
v >>= shift;
r |= shift;
shift = (v > 0x3) << 1;
v >>= shift;
r |= shift;
r |= (v >> 1);
return r + 1;
}
/****** Nal parser ******/

View file

@ -40,8 +40,6 @@
#include <gst/base/gstbitwriter.h>
#include <string.h>
guint ceil_log2 (guint32 v);
typedef struct
{
const guint8 *data;

View file

@ -4473,6 +4473,40 @@ gst_log2 (GstClockTime in)
return count;
}
/**
* gst_util_ceil_log2:
* @v: a #guint32 value.
*
* Return a max num of log2.
*
* Returns: a computed #guint val.
*
* Since: 1.24
*/
guint
gst_util_ceil_log2 (guint32 v)
{
/* Compute Ceil(Log2(v)) */
/* Derived from branchless code for integer log2(v) from:
<http://graphics.stanford.edu/~seander/bithacks.html#IntegerLog> */
guint r, shift;
v--;
r = (v > 0xFFFF) << 4;
v >>= r;
shift = (v > 0xFF) << 3;
v >>= shift;
r |= shift;
shift = (v > 0xF) << 2;
v >>= shift;
r |= shift;
shift = (v > 0x3) << 1;
v >>= shift;
r |= shift;
r |= (v >> 1);
return r + 1;
}
/**
* gst_calculate_linear_regression: (skip)
* @xy: Pairs of (x,y) values

View file

@ -1240,6 +1240,9 @@ void gst_type_mark_as_plugin_api (GType type, GstPluginAPIFlags f
GST_API
gboolean gst_type_is_plugin_api (GType type, GstPluginAPIFlags *flags);
GST_API
guint gst_util_ceil_log2 (guint32 v);
G_END_DECLS
#endif /* __GST_UTILS_H__ */