All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
Modules | Macros | Enumerations | Functions

Mathematical utilities for working with timestamp and time base. More...


 Rational number calculation.


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


enum  AVRounding {
 Rounding methods. More...


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

#define ff_ctz   ff_ctz_c
#define ff_ctzll   ff_ctzll_c

Definition at line 126 of file intmath.h.

Referenced by av_gcd(), and ffat_get_channel_id().

#define ff_clz   ff_clz_c
#define av_parity   __builtin_parity

Definition at line 158 of file intmath.h.

Referenced by check_vanc_parity_checksum(), decode_frame(), and validate_cc_data_pair().

Enumeration Type Documentation

enum AVRounding

Rounding methods.


Round toward zero.


Round away from zero.


Round toward -infinity.


Round toward +infinity.


Round to nearest and halfway cases away from zero.


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 is passed through

Definition at line 79 of file mathematics.h.

Function Documentation

static av_always_inline av_const int ff_ctz_c ( int  v)

Trailing zero bit count.

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

Definition at line 115 of file intmath.h.

static av_always_inline av_const int ff_ctzll_c ( long long  v)

Definition at line 129 of file intmath.h.

static av_always_inline av_const unsigned ff_clz_c ( unsigned  x)

Definition at line 143 of file intmath.h.

int64_t av_const av_gcd ( int64_t  a,
int64_t  b 

Compute the greatest common divisor of two integer operands.

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_reduce(), avcodec_string(), config_output(), decode_main_header(), eval_expr(), ff_framesync_configure(), ff_h263_decode_picture_header(), ff_mpv_encode_init(), ff_parse_specific_params(), ff_put_wav_header(), ff_rfps_add_frame(), find_frame_rate_index(), main(), rtp_send_samples(), and xvid_correct_framerate().

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 asf_build_simple_index(), asf_read_simple_index(), audio_read_packet(), av_dict_set_fxp(), av_dump_format(), av_q2intfloat(), avi_read_header(), calculate_bitrate(), check_stream_max_drift(), compute_display_area(), compute_pkt_fields(), compute_texture_rect(), config_input(), config_output(), config_props(), dash_read_packet(), dc1394_read_common(), dca_parse(), 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_eval_dimensions(), ff_update_cur_dts(), filter(), filter_frame(), finalize_packet(), flv_write_packet(), generate_intervals(), get_delay(), get_edit_list_entry(), get_input_packet(), get_lin_h(), get_lin_h2(), get_pcr(), get_vcd_padding_size(), interpolate(), matroska_parse_tracks(), mkv_write_header(), mov_build_index(), mov_find_next_sample(), mov_flush_fragment(), mov_read_mvhd(), mov_read_sidx(), mov_write_moov_tag(), mov_write_tapt_tag(), mov_write_tkhd_tag(), mov_write_tmcd_tag(), 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_packet(), postProcess_TMPL(), process_work_frame(), push_frame(), qsvscale_config_props(), r3d_read_reda(), read_header(), read_packet(), read_xing_toc(), request_frame(), rm_read_header(), sbg_read_header(), seek_frame_internal(), transcode_subtitles(), ts_to_samples(), update_wrap_reference(), vc2_encode_frame(), vivo_read_header(), vqf_read_seek(), and xv_write_header().

int64_t av_rescale_rnd ( int64_t  a,
int64_t  b,
int64_t  c,
enum AVRounding  rnd 
) const
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(), alloc_out_frame(), asf_write_markers(), 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(), buffer_offset(), caca_write_header(), check_stream_max_drift(), choose_output(), close_output_stream(), compand_delay(), compand_drain(), compare_ts_with_wrapdetect(), compute_chapters_end(), concat_read_packet(), config_input(), convert_sub_to_old_ass_form(), copy_chapters(), cuvid_output_frame(), dash_flush(), dash_write_packet(), dash_write_trailer(), decode(), decode_fctl_chunk(), decode_frame(), decode_simple_internal(), decode_syncpoint(), decode_video(), decoder_decode_frame(), display_frame(), do_streamcopy(), do_subtitle_out(), dv_extract_video_info(), dv_read_header(), ff_af_queue_add(), ff_configure_buffers_for_index(), ff_dv_offset_reset(), ff_framequeue_skip_samples(), ff_framesync_dualinput_get(), ff_interleave_packet_per_dts(), ff_interleaved_peek(), ff_mediacodec_dec_send(), ff_qsv_encode(), ff_qsvvpp_filter_frame(), ff_read_packet(), ff_samples_to_time_base(), ff_update_link_current_pts(), ff_write_chained(), fifo_thread_attempt_recovery(), fill_all_stream_timings(), fill_timing_for_id3_timestamped_stream(), filter_callback(), filter_frame(), find_and_decode_index(), fir_frame(), flush(), framesync_inject_frame(), get_pkt_pts(), get_subtitle_pkt(), guess_status_pts(), hls_read_packet(), init(), init_convert_timestamp(), init_output_stream(), init_output_stream_encode(), interleave_compare_dts(), interleave_new_audio_packet(), kmsgrab_read_header(), main(), matroska_parse_frame(), matroska_parse_tracks(), mediacodec_wrap_hw_buffer(), mediacodec_wrap_sw_buffer(), mkv_write_block(), mkv_write_chapters(), mkv_write_packet(), mkv_write_track(), mov_create_chapter_track(), 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(), ndi_set_audio_packet(), ndi_set_video_packet(), ndi_write_audio_packet(), ndi_write_video_packet(), ni_prepare_read(), nut_write_packet(), ogg_buffer_data(), ogg_compare_granule(), ogg_write_packet_internal(), omx_encode_frame(), parse_forced_key_frames(), plot_spectrum_column(), print_report(), process_frame(), process_input(), process_input_packet(), process_work_frame(), pulse_write_packet(), push_frame(), push_samples(), r3d_read_rdvo(), r3d_seek(), rawvideo_read_header(), read_header(), read_interval_packets(), reap_filters(), request_frame(), rescale_interval(), rl2_read_seek(), rtcp_send_sr(), rtp_mpegts_write_packet(), rtp_parse_one_packet(), 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(), sub2video_heartbeat(), sub2video_update(), submit_frame(), teletext_decode_frame(), ts_str(), update(), update_initial_timestamps(), update_stream_timings(), v210_read_header(), v4l2_get_pts(), v4l2_set_pts(), wait_frame(), webm_chunk_write_packet(), write_audio_frame(), write_chapter(), write_manifest(), and write_packet().

int64_t av_rescale_q_rnd ( int64_t  a,
AVRational  bq,
AVRational  cq,
enum AVRounding  rnd 
) const
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.

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
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 check_recording_time(), dash_write_packet(), duration_max(), ff_rtp_send_aac(), ff_rtp_send_amr(), ff_rtp_send_xiph(), filter_frame(), get_audio_frame(), get_video_frame(), hds_write_packet(), hls_write_packet(), interleave_compare_dts(), ism_write_packet(), main(), mpegts_write_packet_internal(), mux_frames(), nut_write_packet(), packet_after_outpoint(), rtp_send_ilbc(), seek_chapter(), and seg_write_packet().

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)
modDivisor; must be a power of 2
  • 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().

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.

[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
Timestamp expressed in terms of out_tb
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().

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.

[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().