FFmpeg
Modules | Macros | Enumerations | Functions
Mathematics

Modules

 Colorspace Utility
 
 AVRational
 

Macros

#define ff_ctz   ff_ctz_c
 
#define ff_ctzll   ff_ctzll_c
 
#define ff_clz   ff_clz_c
 

Enumerations

enum  AVRounding {
  AV_ROUND_ZERO = 0, AV_ROUND_INF = 1, AV_ROUND_DOWN = 2, AV_ROUND_UP = 3,
  AV_ROUND_NEAR_INF = 5, AV_ROUND_PASS_MINMAX = 8192
}
 Rounding methods. More...
 

Functions

static av_always_inline av_const int ff_ctz_c (int v)
 Trailing zero bit count. More...
 
static av_always_inline av_const int ff_ctzll_c (long long v)
 
static av_always_inline av_const unsigned ff_clz_c (unsigned x)
 
int64_t av_const av_gcd (int64_t a, int64_t b)
 Compute the greatest common divisor of two integer operands. More...
 
int64_t av_rescale (int64_t a, int64_t b, int64_t c) av_const
 Rescale a 64-bit integer with rounding to nearest. More...
 
int64_t av_rescale_rnd (int64_t a, int64_t b, int64_t c, enum AVRounding rnd) av_const
 Rescale a 64-bit integer with specified rounding. More...
 
int64_t av_rescale_q (int64_t a, AVRational bq, AVRational cq) av_const
 Rescale a 64-bit integer by 2 rational numbers. More...
 
int64_t av_rescale_q_rnd (int64_t a, AVRational bq, AVRational cq, enum AVRounding rnd) av_const
 Rescale a 64-bit integer by 2 rational numbers with specified rounding. More...
 
int av_compare_ts (int64_t ts_a, AVRational tb_a, int64_t ts_b, AVRational tb_b)
 Compare two timestamps each in its own time base. More...
 
int64_t av_compare_mod (uint64_t a, uint64_t b, uint64_t mod)
 Compare the remainders of two integer operands divided by a common divisor. More...
 
int64_t av_rescale_delta (AVRational in_tb, int64_t in_ts, AVRational fs_tb, int duration, int64_t *last, AVRational out_tb)
 Rescale a timestamp while preserving known durations. More...
 
int64_t av_add_stable (AVRational ts_tb, int64_t ts, AVRational inc_tb, int64_t inc)
 Add a value to a timestamp. More...
 

Detailed Description

Mathematical utilities for working with timestamp and time base.

Macro Definition Documentation

◆ ff_ctz

#define ff_ctz   ff_ctz_c

Definition at line 107 of file intmath.h.

◆ ff_ctzll

#define ff_ctzll   ff_ctzll_c

Definition at line 127 of file intmath.h.

◆ ff_clz

#define ff_clz   ff_clz_c

Definition at line 143 of file intmath.h.

Enumeration Type Documentation

◆ AVRounding

enum AVRounding

Rounding methods.

Enumerator
AV_ROUND_ZERO 

Round toward zero.

AV_ROUND_INF 

Round away from zero.

AV_ROUND_DOWN 

Round toward -infinity.

AV_ROUND_UP 

Round toward +infinity.

AV_ROUND_NEAR_INF 

Round to nearest and halfway cases away from zero.

AV_ROUND_PASS_MINMAX 

Flag telling rescaling functions to pass INT64_MIN/MAX through unchanged, avoiding special cases for AV_NOPTS_VALUE.

Unlike other values of the enumeration AVRounding, this value is a bitmask that must be used in conjunction with another value of the enumeration through a bitwise OR, in order to set behavior for normal cases.

// Rescaling 3:
// Calculating 3 * 1 / 2
// 3 / 2 is rounded up to 2
// => 2
// Rescaling AV_NOPTS_VALUE:
// AV_NOPTS_VALUE == INT64_MIN
// AV_NOPTS_VALUE is passed through
// => AV_NOPTS_VALUE

Definition at line 79 of file mathematics.h.

Function Documentation

◆ ff_ctz_c()

static av_always_inline av_const int ff_ctz_c ( int  v)
static

Trailing zero bit count.

Parameters
vinput value. If v is 0, the result is undefined.
Returns
the number of trailing 0-bits

Definition at line 116 of file intmath.h.

◆ ff_ctzll_c()

static av_always_inline av_const int ff_ctzll_c ( long long  v)
static

Definition at line 130 of file intmath.h.

◆ ff_clz_c()

static av_always_inline av_const unsigned ff_clz_c ( unsigned  x)
static

Definition at line 144 of file intmath.h.

◆ av_gcd()

int64_t av_const av_gcd ( int64_t  a,
int64_t  b 
)

Compute the greatest common divisor of two integer operands.

Parameters
aOperand
bOperand
Returns
GCD of a and b up to sign; if a >= 0 and b >= 0, return value is >= 0; if a == 0 and b == 0, returns 0.

Definition at line 37 of file mathematics.c.

Referenced by av_gcd_q(), av_reduce(), avcodec_string(), config_output(), decode_main_header(), eval_expr(), ff_h263_decode_picture_header(), ff_mpv_encode_init(), ff_parse_specific_params(), ff_put_wav_header(), ff_rfps_add_frame(), ff_tx_decompose_length(), ff_tx_gen_compound_mapping(), main(), mpsub_read_header(), rtp_send_samples(), and xvid_correct_framerate().

◆ av_rescale()

int64_t av_rescale ( int64_t  a,
int64_t  b,
int64_t  c 
) const

Rescale a 64-bit integer with rounding to nearest.

The operation is mathematically equivalent to a * b / c, but writing that directly can overflow.

This function is equivalent to av_rescale_rnd() with AV_ROUND_NEAR_INF.

See also
av_rescale_rnd(), av_rescale_q(), av_rescale_q_rnd()

Definition at line 129 of file mathematics.c.

Referenced by aconfig_props(), activate(), amv_init(), asf_build_simple_index(), asf_read_simple_index(), audio_read_packet(), av_dict_set_fxp(), av_dump_format(), av_q2intfloat(), avi_read_header(), avpriv_update_cur_dts(), calculate_bitrate(), calculate_display_rect(), check_stream_max_drift(), compute_dar(), compute_display_area(), compute_pkt_fields(), compute_texture_rect(), config_comm_output(), config_filter(), config_input(), config_output(), config_props(), create_stream(), dash_read_packet(), dc1394_read_common(), dca_parse(), dxa_read_header(), enable_pcr_generation_for_stream(), estimate_timings_from_bit_rate(), ff_decklink_read_header(), ff_gen_search(), ff_interleave_add_packet(), ff_pcm_read_seek(), ff_rtp_check_and_send_back_rr(), ff_scale_adjust_dimensions(), ff_vorbiscomment_write(), filter(), filter_frame(), finalize_packet(), flv_write_packet(), generate_intervals(), get_aiff_header(), get_delay(), get_edit_list_entry(), get_lin_h(), get_lin_h2(), get_pcr(), get_pts(), get_pts_range(), get_vcd_padding_size(), h264_parse(), ifile_get_packet(), imf_seek(), interpolate(), libaribb24_handle_regions(), mcc_read_header(), mkv_write_info(), mkv_write_track_video(), mov_build_index(), mov_find_next_sample(), mov_flush_fragment(), mov_read_header(), mov_read_sidx(), mov_read_timecode_track(), mov_write_moov_tag(), mov_write_tapt_tag(), mov_write_tkhd_tag(), mov_write_tmcd_tag(), mp3_parse_info_tag(), mp3_parse_vbr_tags(), mp3_seek(), mp3_update_xing(), mpeg_mux_write_packet(), mpegts_init(), mpegts_write_packet_internal(), mpegts_write_pes(), mxf_read_seek(), nsv_parse_NSVs_header(), opengl_compute_display_area(), opus_header(), output_frame(), output_packet(), pad_opencl_config_output(), postProcess_TMPL(), process_subtitle(), process_work_frame(), push_frame(), r3d_read_reda(), read_header(), read_packet(), read_xing_toc(), request_frame(), rescale_mastering_chroma(), rescale_mastering_luma(), rescale_mdcv(), rm_read_header(), sbg_read_header(), sds_read_header(), seek_frame_internal(), select_pcr_streams(), sga_audio_packet(), sga_read_header(), ts_to_samples(), ttml_get_extent(), ttml_get_origin(), update_wrap_reference(), vc2_encode_frame(), video_frame(), vivo_read_header(), vqf_read_seek(), xv_write_header(), and xwma_read_header().

◆ av_rescale_rnd()

int64_t av_rescale_rnd ( int64_t  a,
int64_t  b,
int64_t  c,
enum AVRounding  rnd 
) const

◆ av_rescale_q()

int64_t av_rescale_q ( int64_t  a,
AVRational  bq,
AVRational  cq 
) const

Rescale a 64-bit integer by 2 rational numbers.

The operation is mathematically equivalent to a * bq / cq.

This function is equivalent to av_rescale_q_rnd() with AV_ROUND_NEAR_INF.

See also
av_rescale(), av_rescale_rnd(), av_rescale_q_rnd()

Definition at line 142 of file mathematics.c.

Referenced by activate(), adjust_frame_pts_to_encoder_tb(), alloc_out_frame(), amv_write_trailer(), asf_write_markers(), audio_frame(), av_add_stable(), av_packet_rescale_ts(), av_rescale_delta(), avcodec_decode_subtitle2(), avf_read_packet(), avformat_find_stream_info(), avformat_seek_file(), avi_read_packet(), avi_read_seek(), avi_write_header(), avisynth_read_packet_audio(), avisynth_read_seek(), caca_write_header(), check_stream_max_drift(), compand_delay(), compand_drain(), compare_ts_with_wrapdetect(), compute_chapters_end(), concat_read_packet(), config_input(), config_output(), copy_chapters(), correct_input_start_times(), create_stream(), cuvid_decode_packet(), cuvid_output_frame(), dash_flush(), dash_write_packet(), dash_write_trailer(), ddagrab_config_props(), dec_enc(), decode(), decode_fctl_chunk(), decode_flush(), decode_frame(), decode_simple_internal(), decode_syncpoint(), decode_video(), decoder_decode_frame(), display_frame(), do_audio_out(), do_morpho(), do_streamcopy(), do_subtitle_out(), draw_spatial(), ff_af_queue_add(), ff_configure_buffers_for_index(), ff_framequeue_skip_samples(), ff_framesync_dualinput_get(), ff_get_muxer_ts_offset(), ff_interleave_packet_per_dts(), ff_mediacodec_dec_send(), ff_qsv_encode(), ff_qsvvpp_filter_frame(), ff_read_packet(), ff_rescale_interval(), ff_samples_to_time_base(), ff_update_link_current_pts(), ff_vorbiscomment_write(), fifo_thread_attempt_recovery(), fill_all_stream_timings(), fill_timing_for_id3_timestamped_stream(), filter_callback(), filter_frame(), find_and_decode_index(), framesync_inject_frame(), get_frag_time(), get_next_nb_samples(), get_pkt_pts(), get_subtitle_pkt(), guess_pkt_duration(), guess_status_pts(), handle_avoid_negative_ts(), hls_read_packet(), imf_read_packet(), init(), init_convert_timestamp(), init_output_stream(), init_output_stream_streamcopy(), interleave_compare_dts(), ism_flush(), kmsgrab_read_header(), libaribb24_decode(), main(), mediacodec_receive(), mediacodec_send(), mediacodec_wrap_hw_buffer(), mediacodec_wrap_sw_buffer(), mf_from_mf_time(), mf_to_mf_time(), mkv_write_block(), mkv_write_chapters(), mkv_write_packet(), mkv_write_track(), mov_create_chapter_track(), mov_create_timecode_track(), mov_get_skip_samples(), mov_preroll_write_stbl_atoms(), mov_read_dops(), mov_read_seek(), mov_read_sidx(), mov_write_chpl_tag(), mov_write_prft_tag(), mov_write_single_packet(), movie_common_init(), mp3_parse_info_tag(), mp3_parse_vbr_tags(), mpeg4_decode_header(), mpegts_write_packet_internal(), mxf_compute_sample_count(), mxf_edit_unit_absolute_offset(), mxf_parse_physical_source_package(), mxf_parse_structural_metadata(), mxf_read_seek(), mxf_set_audio_pts(), mxf_set_current_edit_unit(), next_duration(), ni_prepare_read(), nut_write_packet(), of_output_packet(), ogg_buffer_data(), ogg_compare_granule(), ogg_write_packet_internal(), omx_encode_frame(), output_frame(), overflow_heartbeat(), parse_forced_key_frames(), plot_freqs(), plot_spectrum_column(), process_frame(), process_input_packet(), process_work_frame(), pulse_write_packet(), push_frame(), push_samples(), query_frame(), r3d_read_rdvo(), r3d_seek(), rawvideo_read_header(), read_header(), read_interval_packets(), reap_filters(), request_frame(), rescale_pts(), rl2_read_seek(), rtcp_send_sr(), rtp_mpegts_write_packet(), rtp_parse_one_packet(), rtp_set_prft(), rtsp_read_play(), rtsp_read_seek(), save_subtitle_set(), sbg_read_seek2(), seek_chapter(), seek_subtitle(), seek_to_start(), seg_write_packet(), select_frame(), send_silence(), ser_read_header(), source_request_frame(), sq_set_tb(), sub2video_heartbeat(), sub2video_update(), submit_frame(), teletext_decode_frame(), trigger_fix_sub_duration_heartbeat(), try_push_frame(), ts_discontinuity_detect(), ts_discontinuity_process(), ts_fixup(), ts_str(), update(), update_initial_timestamps(), update_pts(), update_stream_timings(), v4l2_get_pts(), v4l2_set_pts(), video_frame(), viv_read_packet(), viv_read_seek(), webm_chunk_write_packet(), write_audio_frame(), write_chapter(), write_manifest(), write_packet(), and xfade_opencl_config_output().

◆ av_rescale_q_rnd()

int64_t av_rescale_q_rnd ( int64_t  a,
AVRational  bq,
AVRational  cq,
enum AVRounding  rnd 
) const

◆ av_compare_ts()

int av_compare_ts ( int64_t  ts_a,
AVRational  tb_a,
int64_t  ts_b,
AVRational  tb_b 
)

Compare two timestamps each in its own time base.

Returns
One of the following values:
  • -1 if ts_a is before ts_b
  • 1 if ts_a is after ts_b
  • 0 if they represent the same position
Warning
The result of the function is undefined if one of the timestamps is outside the int64_t range when represented in the other's timebase.

Definition at line 147 of file mathematics.c.

Referenced by chapter_start_cmp(), check_recording_time(), dash_write_packet(), ff_rtp_send_aac(), ff_rtp_send_amr(), ff_rtp_send_xiph(), filter_frame(), finish_stream(), forced_kf_apply(), get_audio_frame(), get_video_frame(), handle_avoid_negative_ts(), hds_write_packet(), hls_write_packet(), ifile_duration_update(), interleave_compare_dts(), ism_write_packet(), main(), mux_frames(), nut_write_packet(), overflow_heartbeat(), packet_after_outpoint(), queue_head_update(), receive_for_stream(), rtp_send_ilbc(), seek_chapter(), seg_write_packet(), and stream_update_ts().

◆ av_compare_mod()

int64_t av_compare_mod ( uint64_t  a,
uint64_t  b,
uint64_t  mod 
)

Compare the remainders of two integer operands divided by a common divisor.

In other words, compare the least significant log2(mod) bits of integers a and b.

av_compare_mod(0x11, 0x02, 0x10) < 0 // since 0x11 % 0x10 (0x1) < 0x02 % 0x10 (0x2)
av_compare_mod(0x11, 0x02, 0x20) > 0 // since 0x11 % 0x20 (0x11) > 0x02 % 0x20 (0x02)
Parameters
aOperand
bOperand
modDivisor; must be a power of 2
Returns
  • a negative value if a % mod < b % mod
  • a positive value if a % mod > b % mod
  • zero if a % mod == b % mod

Definition at line 160 of file mathematics.c.

Referenced by av_read_frame(), and compare_ts_with_wrapdetect().

◆ av_rescale_delta()

int64_t av_rescale_delta ( AVRational  in_tb,
int64_t  in_ts,
AVRational  fs_tb,
int  duration,
int64_t *  last,
AVRational  out_tb 
)

Rescale a timestamp while preserving known durations.

This function is designed to be called per audio packet to scale the input timestamp to a different time base. Compared to a simple av_rescale_q() call, this function is robust against possible inconsistent frame durations.

The last parameter is a state variable that must be preserved for all subsequent calls for the same stream. For the first call, *last should be initialized to AV_NOPTS_VALUE.

Parameters
[in]in_tbInput time base
[in]in_tsInput timestamp
[in]fs_tbDuration time base; typically this is finer-grained (greater) than in_tb and out_tb
[in]durationDuration till the next call to this function (i.e. duration of the current packet/frame)
[in,out]lastPointer to a timestamp expressed in terms of fs_tb, acting as a state variable
[in]out_tbOutput timebase
Returns
Timestamp expressed in terms of out_tb
Note
In the context of this function, "duration" is in term of samples, not seconds.

Definition at line 168 of file mathematics.c.

Referenced by decode_audio(), and do_streamcopy().

◆ av_add_stable()

int64_t av_add_stable ( AVRational  ts_tb,
int64_t  ts,
AVRational  inc_tb,
int64_t  inc 
)

Add a value to a timestamp.

This function guarantees that when the same value is repeatly added that no accumulation of rounding errors occurs.

Parameters
[in]tsInput timestamp
[in]ts_tbInput timestamp time base
[in]incValue to be added
[in]inc_tbTime base of inc

Definition at line 191 of file mathematics.c.

Referenced by compute_pkt_fields(), and main().

AV_ROUND_UP
@ AV_ROUND_UP
Round toward +infinity.
Definition: mathematics.h:83
av_rescale_rnd
int64_t av_rescale_rnd(int64_t a, int64_t b, int64_t c, enum AVRounding rnd)
Rescale a 64-bit integer with specified rounding.
Definition: mathematics.c:58
AV_NOPTS_VALUE
#define AV_NOPTS_VALUE
Undefined timestamp value.
Definition: avutil.h:248
av_compare_mod
int64_t av_compare_mod(uint64_t a, uint64_t b, uint64_t mod)
Compare the remainders of two integer operands divided by a common divisor.
Definition: mathematics.c:160
AV_ROUND_PASS_MINMAX
@ AV_ROUND_PASS_MINMAX
Flag telling rescaling functions to pass INT64_MIN/MAX through unchanged, avoiding special cases for ...
Definition: mathematics.h:108