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

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

Modules

 AVRational
 Rational number calculation.
 

Macros

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

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

#define ff_ctz   ff_ctz_c

Definition at line 106 of file intmath.h.

Referenced by get_bit_length(), refill2(), and remove_wasted_bits().

#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

Definition at line 142 of file intmath.h.

Referenced by config_input(), read_high_coeffs(), and read_low_coeffs().

#define av_parity   __builtin_parity

Definition at line 158 of file intmath.h.

Referenced by decode_frame(), and validate_cc_data_pair().

Enumeration Type Documentation

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

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 115 of file intmath.h.

static av_always_inline av_const int ff_ctzll_c ( long long  v)
static

Definition at line 129 of file intmath.h.

static av_always_inline av_const unsigned ff_clz_c ( unsigned  x)
static

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.

Parameters
a,bOperands
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_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(), 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(), 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 alloc_out_frame(), asf_write_markers(), av_add_stable(), av_packet_rescale_ts(), av_rescale_delta(), avcodec_decode_audio4(), 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_write_packet(), dash_write_trailer(), decode(), decode_fctl_chunk(), decode_frame(), decode_syncpoint(), decode_video(), decoder_decode_frame(), display_frame(), do_streamcopy(), do_subtitle_out(), do_video_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_interleave_packet_per_dts(), ff_interleaved_peek(), ff_mediacodec_dec_decode(), ff_qsv_encode(), 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_frame(), find_and_decode_index(), framesync_inject_frame(), get_pkt_pts(), get_subtitle_pkt(), hls_read_packet(), http_prepare_data(), init(), init_convert_timestamp(), init_output_stream(), init_output_stream_encode(), interleave_compare_dts(), interleave_new_audio_packet(), main(), matroska_parse_frame(), matroska_parse_tracks(), mediacodec_wrap_hw_buffer(), mkv_write_block(), mkv_write_chapters(), mkv_write_packet(), mkv_write_track(), mov_create_chapter_track(), mov_read_seek(), mov_read_sidx(), mov_write_chpl_tag(), mov_write_single_packet(), movie_common_init(), mp3_parse_info_tag(), mp3_parse_vbr_tags(), mpeg4_decode_header(), mpegts_write_packet_internal(), mxf_parse_physical_source_package(), mxf_parse_structural_metadata(), mxf_read_seek(), ni_prepare_read(), nut_write_packet(), ogg_buffer_data(), ogg_compare_granule(), ogg_write_packet_internal(), omx_encode_frame(), parse_forced_key_frames(), print_report(), process_frame(), process_input(), process_input_packet(), pulse_write_packet(), push_frame(), push_samples(), qtkit_read_packet(), 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(), set_srce_frame_dest_pts(), source_request_frame(), sub2video_heartbeat(), sub2video_update(), submit_frame(), teletext_decode_frame(), ts_str(), update_initial_timestamps(), update_stream_timings(), v210_read_header(), 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

Rescale a 64-bit integer by 2 rational numbers with specified rounding.

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

See Also
av_rescale(), av_rescale_rnd(), av_rescale_q()

Definition at line 134 of file mathematics.c.

Referenced by av_rescale_delta(), av_rescale_q(), ff_interleave_add_packet(), filter_frame(), lavfi_read_packet(), main(), movie_push_frame(), parse_packet(), process_input(), rescale_interval(), rv10_write_header(), update_stream_timings(), write_packet(), and write_packet_header().

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 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)
Parameters
a,bOperands
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().

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

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