FFmpeg
Files | Data Structures | Functions
AVRational

Files

file  rational.h
 

Data Structures

struct  AVRational
 Rational number (pair of numerator and denominator). More...
 

Functions

static AVRational av_make_q (int num, int den)
 Create an AVRational. More...
 
static int av_cmp_q (AVRational a, AVRational b)
 Compare two rationals. More...
 
static double av_q2d (AVRational a)
 Convert an AVRational to a double. More...
 
int av_reduce (int *dst_num, int *dst_den, int64_t num, int64_t den, int64_t max)
 Reduce a fraction. More...
 
AVRational av_mul_q (AVRational b, AVRational c) av_const
 Multiply two rationals. More...
 
AVRational av_div_q (AVRational b, AVRational c) av_const
 Divide one rational by another. More...
 
AVRational av_add_q (AVRational b, AVRational c) av_const
 Add two rationals. More...
 
AVRational av_sub_q (AVRational b, AVRational c) av_const
 Subtract one rational from another. More...
 
static av_always_inline AVRational av_inv_q (AVRational q)
 Invert a rational. More...
 
AVRational av_d2q (double d, int max) av_const
 Convert a double precision floating point number to a rational. More...
 
int av_nearer_q (AVRational q, AVRational q1, AVRational q2)
 Find which of the two rationals is closer to another rational. More...
 
int av_find_nearest_q_idx (AVRational q, const AVRational *q_list)
 Find the value in a list of rationals nearest a given reference rational. More...
 
uint32_t av_q2intfloat (AVRational q)
 Convert an AVRational to a IEEE 32-bit float expressed in fixed-point format. More...
 
AVRational av_gcd_q (AVRational a, AVRational b, int max_den, AVRational def)
 Return the best rational so that a and b are multiple of it. More...
 

Detailed Description

Rational number calculation.

While rational numbers can be expressed as floating-point numbers, the conversion process is a lossy one, so are floating-point operations. On the other hand, the nature of FFmpeg demands highly accurate calculation of timestamps. This set of rational number utilities serves as a generic interface for manipulating rational numbers as pairs of numerators and denominators.

Many of the functions that operate on AVRational's have the suffix _q, in reference to the mathematical symbol "ℚ" (Q) which denotes the set of all rational numbers.

Function Documentation

◆ av_make_q()

static AVRational av_make_q ( int  num,
int  den 
)
inlinestatic

Create an AVRational.

Useful for compilers that do not support compound literals.

Note
The return value is not reduced.
See also
av_reduce()

Definition at line 71 of file rational.h.

Referenced by abs_sub_q(), activate(), ambisonics_config(), aribcaption_decode(), audio_frame(), av_csp_primaries_id_from_desc(), av_gcd_q(), av_q2den(), avf_read_packet(), calculate_display_rect(), config_comm_output(), config_input(), config_out_props(), config_output(), config_props(), config_video_output(), dnn_classify_post_proc(), dnn_detect_parse_yolo_output(), dnn_detect_post_proc_ssd(), dnn_detect_post_proc_tf(), encode_frame(), export_metadata(), ff_decklink_read_header(), ff_decklink_set_format(), ff_decklink_write_header(), ff_dovi_rpu_parse(), ff_framequeue_skip_samples(), ff_icc_profile_sanitize(), ff_vorbiscomment_write(), filter_frame(), fsync_config_props(), get_bmd_timecode(), get_cie_xy(), get_frame_timecode(), get_next_nb_samples(), get_next_track_with_minimum_timestamp(), get_packet_defaults(), gif_read_header(), h2645_sei_to_side_data(), imf_base_resource_init(), imf_cpl_init(), imf_read_packet(), imf_seek(), init(), libdav1d_receive_frame(), libjxl_receive_frame(), main(), mcc_read_header(), mix_presentation_obu(), moflex_read_sync(), mov_metadata_hmmt(), mov_read_amve(), mov_read_mdcv(), mov_read_sidx(), mov_read_smdm(), mxf_init(), mxf_parse_structural_metadata(), mxf_read_generic_descriptor(), open_track_file_resource(), open_virtual_track(), output_frame(), parameter_block_obu(), parse_chunk(), parse_manifest_representation(), plot_cqt(), populate_avctx_color_fields(), push_frame(), scalable_channel_layout_config(), try_push_frame(), video_frame(), decklink_input_callback::VideoInputFrameArrived(), viv_read_packet(), and viv_read_seek().

◆ av_cmp_q()

static int av_cmp_q ( AVRational  a,
AVRational  b 
)
inlinestatic

◆ av_q2d()

static double av_q2d ( AVRational  a)
inlinestatic

Convert an AVRational to a double.

Parameters
aAVRational to convert
Returns
a in floating-point form
See also
av_d2q()

Definition at line 104 of file rational.h.

Referenced by activate(), amf_save_hdr_metadata(), audio_thread(), av_dump_format(), av_guess_frame_rate(), av_ts_make_time_string2(), avformat_find_stream_info(), choose_out_timebase(), config_input(), config_output(), config_output_props(), config_props(), configure_video_device(), ddagrab_config_props(), ddagrab_request_frame(), decode_frame(), decode_syncpoint(), defined_frame_rate(), deshake_opencl_init(), do_blend(), draw_text(), dump_ambient_viewing_environment(), dump_ambient_viewing_environment_metadata(), dump_dovi_metadata(), dump_dynamic_hdr_plus(), dump_dynamic_hdr_vivid(), dump_mastering_display(), dump_mastering_display_metadata(), dump_stereo3d(), dump_stream_format(), dv_write_pack(), enc_stats_write(), encode_frame(), evaluate_timeline_at_frame(), fbdev_read_header(), fbdev_read_packet(), ff_decklink_set_format(), ff_determine_signal_peak(), ff_fill_rgb2xyz_table(), ff_fill_rgb2yuv_table(), ff_icc_profile_sanitize(), ff_inlink_process_commands(), ff_mpv_encode_init(), ff_mpv_encode_picture(), ff_rfps_add_frame(), ff_rfps_calculate(), fill_whitepoint_conv_table(), film_write_header(), filter_frame(), filter_slice(), flv_init(), flv_write_metadata_packet(), forced_kf_apply(), gdigrab_read_header(), gdigrab_read_packet(), geq_filter_frame(), get_best_frame(), get_fps(), get_resource_context_for_timestamp(), get_video_frame(), h2645_sei_to_side_data(), hdr_encode_frame(), imf_read_packet(), init(), init_muxer(), init_stream(), init_video(), ist_dts_update(), libjxl_populate_primaries(), libplacebo_activate(), libplacebo_config_output(), main(), match_framerate(), mkv_write_trailer(), mkv_write_video_color(), mov_read_clap(), mov_write_trak_tag(), mpegvideo_parse(), noise_init(), output_frame(), output_single_frame(), overlay_cuda_blend(), pkt_dump_internal(), put_videoinfoheader2(), qrencode_config_input(), qsv_hevc_set_encode_ctrl(), read_header(), read_seek(), read_thread(), request_frame(), rfc4175_parse_format(), seg_write_packet(), segment_end(), select_frame(), set_context_streams_from_tracks(), setts_init(), stream_has_enough_packets(), svc_encode_init(), thp_read_header(), tonemap(), tonemap_opencl_init(), tonemap_vaapi_save_metadata(), update_crops(), update_video_stats(), v4l2_read_header(), vaapi_encode_h265_init_picture_params(), vaapi_encode_mpeg2_init_sequence_params(), video_sync_process(), video_thread(), vk_enc_h265_update_pic_info(), vpp_set_frame_ext_params(), write_adaptation_set(), write_manifest(), and writer_print_time().

◆ av_reduce()

int av_reduce ( int *  dst_num,
int *  dst_den,
int64_t  num,
int64_t  den,
int64_t  max 
)

Reduce a fraction.

This is useful for framerate calculations.

Parameters
[out]dst_numDestination numerator
[out]dst_denDestination denominator
[in]numSource numerator
[in]denSource denominator
[in]maxMaximum allowed values for dst_num & dst_den
Returns
1 if the operation is exact, 0 otherwise

Definition at line 35 of file rational.c.

Referenced by aom_decode(), apng_write_header(), asf_read_header(), av1_metadata_update_sequence_header(), av_add_q(), av_d2q(), av_guess_sample_aspect_ratio(), av_mul_q(), av_parse_ratio(), avcodec_string(), avformat_find_stream_info(), avi_write_header(), avpriv_set_pts_info(), choose_out_timebase(), coherent_ts(), compute_dar(), compute_frame_duration(), config_input(), config_output(), cudacolorspace_filter_frame(), cudascale_filter_frame(), d3d12va_encode_hevc_init_sequence_params(), d3d12va_encode_init_rate_control(), dash_init(), decode_frame(), decode_sequence_header_adv(), dnxhd_decode_header(), dump_stream_format(), dxa_read_header(), export_stream_params(), ff_av1_framerate(), ff_canopus_parse_info_tag(), ff_hw_base_encode_init_params_h264(), ff_hw_base_encode_init_params_h265(), ff_mpv_encode_init(), ff_rfps_calculate(), ff_rm_read_mdpr_codecdata(), ff_yadif_config_output_common(), filter_frame(), flush_packet(), gif_read_header(), h264_metadata_update_sps(), h264_slice_header_init(), h265_metadata_update_sps(), h265_metadata_update_vps(), hevc_parse_slice_header(), imf_time_to_ts(), jp2_find_codestream(), jpeg_put_comments(), libdav1d_receive_frame(), libx265_encode_init(), mcc_read_header(), mkv_write_track_video(), mov_read_header(), mov_read_trak(), mov_write_pasp_tag(), mpeg2_metadata_update_fragment(), mpeg4_encode_vol_header(), mpeg_decode_postinit(), mxf_parse_dnxhd_frame(), mxf_parse_ffv1_frame(), mxf_parse_h264_frame(), mxf_parse_mpeg2_frame(), nppscale_filter_frame(), parse_nal_unit(), parse_nal_units(), parse_video_var(), pcx_encode_frame(), put_videoinfoheader2(), read_fps(), rebuild_filter_bank_with_compensation(), resample_init(), scale_frame(), scale_vt_filter_frame(), set_parser_ctx(), set_sar(), show_stream(), skeleton_header(), smacker_read_header(), svc_encode_init(), tmv_read_header(), update_context_with_frame_header(), vaapi_encode_init_rate_control(), vaapi_encode_mjpeg_init_picture_params(), vmd_read_header(), vt_pixbuf_set_par(), vtenc_create_encoder(), X264_init(), xvid_encode_frame(), yuv4_read_header(), and yuv4_write_header().

◆ av_mul_q()

AVRational av_mul_q ( AVRational  b,
AVRational  c 
) const

◆ av_div_q()

AVRational av_div_q ( AVRational  b,
AVRational  c 
) const

◆ av_add_q()

AVRational av_add_q ( AVRational  b,
AVRational  c 
) const

Add two rationals.

Parameters
bFirst rational
cSecond rational
Returns
b+c

Definition at line 93 of file rational.c.

Referenced by av_csp_primaries_id_from_desc(), av_sub_q(), ff_icc_profile_sanitize(), imf_read_packet(), main(), mov_read_clap(), of_stream_init(), open_track_file_resource(), streamcopy_init(), and video_frame().

◆ av_sub_q()

AVRational av_sub_q ( AVRational  b,
AVRational  c 
) const

Subtract one rational from another.

Parameters
bFirst rational
cSecond rational
Returns
b-c

Definition at line 101 of file rational.c.

Referenced by abs_sub_q(), config_props(), ff_icc_profile_sanitize(), imf_read_packet(), main(), mov_read_clap(), mov_write_clap_tag(), open_track_file_resource(), and open_track_resource_context().

◆ av_inv_q()

static av_always_inline AVRational av_inv_q ( AVRational  q)
static

Invert a rational.

Parameters
qvalue
Returns
1 / q
Examples
qsv_transcode.c, transcode.c, and vaapi_transcode.c.

Definition at line 159 of file rational.h.

Referenced by activate(), aom_encode(), avformat_find_stream_info(), avi_read_header(), build_frame_code(), choose_out_timebase(), config_input(), config_output(), config_props(), config_video(), config_video_output(), ddagrab_config_props(), dec_enc(), dshow_add_device(), dvvideo_decode_frame(), dynamic_set_parameter(), ff_guess_coded_bitrate(), ff_rfps_calculate(), fg_output_step(), film_write_header(), filter_frame(), gdigrab_read_header(), get_fpsQ(), guess_pkt_duration(), h264_parse(), hdr_decode_frame(), hdr_encode_frame(), ifilter_parameters_from_frame(), init(), init_src(), init_video_stream(), ist_dts_update(), libplacebo_activate(), libplacebo_config_output(), mf_encv_output_adjust(), mkv_write_track(), mov_create_timecode_track(), mxf_compute_sample_count(), mxf_handle_missing_index_segment(), mxf_init(), mxf_init_timecode(), mxf_parse_structural_metadata(), mxf_read_seek(), mxf_set_audio_pts(), mxf_set_current_edit_unit(), open_output_file(), process_work_frame(), program_opencl_config_output(), qrencodesrc_config_props(), query_frame(), r3d_read_rdvo(), r3d_seek(), read_header(), request_frame(), rm_write_header(), set_context_streams_from_tracks(), smush_read_header(), source_config_props(), streamcopy_init(), tb_unreliable(), testsrc_vulkan_config_props(), try_push_frame(), txd_read_header(), vaapi_encode_mpeg2_init_sequence_params(), video_duration_estimate(), video_frame_process(), decklink_input_callback::VideoInputFrameArrived(), viv_read_seek(), vivo_read_header(), vpx_encode(), and yuv4_read_header().

◆ av_d2q()

AVRational av_d2q ( double  d,
int  max 
) const

Convert a double precision floating point number to a rational.

In case of infinity, the returned value is expressed as {1, 0} or {-1, 0} depending on the sign.

In general rational numbers with |num| <= 1<<26 && |den| <= 1<<26 can be recovered exactly from their double representation. (no exceptions were found within 1B random ones)

Parameters
ddouble to convert
maxMaximum allowed numerator and denominator
Returns
d in AVRational form
See also
av_q2d()

Definition at line 106 of file rational.c.

Referenced by amf_parse_object(), audio_frame(), av_opt_is_set_to_default(), av_opt_set_defaults2(), av_parse_ratio(), config_output_props(), config_props(), dec_enc(), decode_frame(), double_to_rational(), dynamic_set_parameter(), ff_update_hdr_metadata(), flv_update_video_color_info(), fourxm_read_header(), get_aspect_ratio(), hdr_decode_frame(), libjxl_get_primaries(), log_default(), microdvd_read_header(), mov_read_tkhd(), mpeg_decode_postinit(), mv_read_header(), nuv_header(), pdv_read_header(), read_header(), sampling_init(), thp_read_header(), var_read_float(), vivo_read_header(), and XYZ_xy().

◆ av_nearer_q()

int av_nearer_q ( AVRational  q,
AVRational  q1,
AVRational  q2 
)

Find which of the two rationals is closer to another rational.

Parameters
qRational to be compared against
q1Rational to be tested
q2Rational to be tested
Returns
One of the following values:
  • 1 if q1 is nearer to q than q2
  • -1 if q2 is nearer to q than q1
  • 0 if they have the same distance

Definition at line 125 of file rational.c.

Referenced by av_find_nearest_q_idx().

◆ av_find_nearest_q_idx()

int av_find_nearest_q_idx ( AVRational  q,
const AVRational q_list 
)

Find the value in a list of rationals nearest a given reference rational.

Parameters
qReference rational
q_listArray of rationals terminated by {0, 0}
Returns
Index of the nearest value found in the array

Definition at line 140 of file rational.c.

Referenced by choose_out_timebase().

◆ av_q2intfloat()

uint32_t av_q2intfloat ( AVRational  q)

Convert an AVRational to a IEEE 32-bit float expressed in fixed-point format.

Parameters
qRational to be converted
Returns
Equivalent floating-point value, expressed as an unsigned 32-bit integer.
Note
The returned value is platform-indepedant.

Definition at line 150 of file rational.c.

Referenced by ircam_write_header(), and main().

◆ av_gcd_q()

AVRational av_gcd_q ( AVRational  a,
AVRational  b,
int  max_den,
AVRational  def 
)

Return the best rational so that a and b are multiple of it.

If the resulting denominator is larger than max_den, return def.

Definition at line 184 of file rational.c.

Referenced by config_output(), ff_framesync_configure(), and libplacebo_config_output().