FFmpeg  4.0
Macros | Functions | Variables
utils.c File Reference

utils. More...

#include "config.h"
#include "libavutil/attributes.h"
#include "libavutil/avassert.h"
#include "libavutil/avstring.h"
#include "libavutil/bprint.h"
#include "libavutil/channel_layout.h"
#include "libavutil/crc.h"
#include "libavutil/frame.h"
#include "libavutil/hwcontext.h"
#include "libavutil/internal.h"
#include "libavutil/mathematics.h"
#include "libavutil/mem_internal.h"
#include "libavutil/pixdesc.h"
#include "libavutil/imgutils.h"
#include "libavutil/samplefmt.h"
#include "libavutil/dict.h"
#include "libavutil/thread.h"
#include "avcodec.h"
#include "decode.h"
#include "hwaccel.h"
#include "libavutil/opt.h"
#include "mpegvideo.h"
#include "thread.h"
#include "frame_thread_encoder.h"
#include "internal.h"
#include "raw.h"
#include "bytestream.h"
#include "version.h"
#include <stdlib.h>
#include <stdarg.h>
#include <stdatomic.h>
#include <limits.h>
#include <float.h>
#include "libavutil/ffversion.h"

Go to the source code of this file.

Macros

#define TAG_PRINT(x)
 
#define LICENSE_PREFIX   "libavcodec license: "
 

Functions

void av_fast_padded_malloc (void *ptr, unsigned int *size, size_t min_size)
 Same behaviour av_fast_malloc but the buffer has additional AV_INPUT_BUFFER_PADDING_SIZE at the end which will always be 0. More...
 
void av_fast_padded_mallocz (void *ptr, unsigned int *size, size_t min_size)
 Same behaviour av_fast_padded_malloc except that buffer will always be 0-initialized after call. More...
 
int av_codec_is_encoder (const AVCodec *codec)
 
int av_codec_is_decoder (const AVCodec *codec)
 
int ff_set_dimensions (AVCodecContext *s, int width, int height)
 Check that the provided frame dimensions are valid and set them on the codec context. More...
 
int ff_set_sar (AVCodecContext *avctx, AVRational sar)
 Check that the provided sample aspect ratio is valid and set it on the codec context. More...
 
int ff_side_data_update_matrix_encoding (AVFrame *frame, enum AVMatrixEncoding matrix_encoding)
 Add or update AV_FRAME_DATA_MATRIXENCODING side data. More...
 
void avcodec_align_dimensions2 (AVCodecContext *s, int *width, int *height, int linesize_align[AV_NUM_DATA_POINTERS])
 Modify width and height values so that they will result in a memory buffer that is acceptable for the codec if you also ensure that all line sizes are a multiple of the respective linesize_align[i]. More...
 
void avcodec_align_dimensions (AVCodecContext *s, int *width, int *height)
 Modify width and height values so that they will result in a memory buffer that is acceptable for the codec if you do not use any horizontal padding. More...
 
int avcodec_enum_to_chroma_pos (int *xpos, int *ypos, enum AVChromaLocation pos)
 Converts AVChromaLocation to swscale x/y chroma position. More...
 
enum AVChromaLocation avcodec_chroma_pos_to_enum (int xpos, int ypos)
 Converts swscale x/y chroma position to AVChromaLocation. More...
 
int avcodec_fill_audio_frame (AVFrame *frame, int nb_channels, enum AVSampleFormat sample_fmt, const uint8_t *buf, int buf_size, int align)
 Fill AVFrame audio data and linesize pointers. More...
 
void ff_color_frame (AVFrame *frame, const int c[4])
 
int avcodec_default_execute (AVCodecContext *c, int(*func)(AVCodecContext *c2, void *arg2), void *arg, int *ret, int count, int size)
 
int avcodec_default_execute2 (AVCodecContext *c, int(*func)(AVCodecContext *c2, void *arg2, int jobnr, int threadnr), void *arg, int *ret, int count)
 
enum AVPixelFormat avpriv_find_pix_fmt (const PixelFormatTag *tags, unsigned int fourcc)
 
unsigned av_codec_get_codec_properties (const AVCodecContext *codec)
 
int av_codec_get_max_lowres (const AVCodec *codec)
 
int avpriv_codec_get_cap_skip_frame_fill_param (const AVCodec *codec)
 
static int64_t get_bit_rate (AVCodecContext *ctx)
 
static void ff_lock_avcodec (AVCodecContext *log_ctx, const AVCodec *codec)
 
static void ff_unlock_avcodec (const AVCodec *codec)
 
int attribute_align_arg ff_codec_open2_recursive (AVCodecContext *avctx, const AVCodec *codec, AVDictionary **options)
 Call avcodec_open2 recursively by decrementing counter, unlocking mutex, calling the function and then restoring again. More...
 
int attribute_align_arg avcodec_open2 (AVCodecContext *avctx, const AVCodec *codec, AVDictionary **options)
 Initialize the AVCodecContext to use the given AVCodec. More...
 
void avsubtitle_free (AVSubtitle *sub)
 Free all allocated data in the given subtitle struct. More...
 
av_cold int avcodec_close (AVCodecContext *avctx)
 Close a given AVCodecContext and free all the data associated with it (but not the AVCodecContext itself). More...
 
const char * avcodec_get_name (enum AVCodecID id)
 Get the name of a codec. More...
 
size_t av_get_codec_tag_string (char *buf, size_t buf_size, unsigned int codec_tag)
 Put a string representing the codec tag codec_tag in buf. More...
 
void avcodec_string (char *buf, int buf_size, AVCodecContext *enc, int encode)
 
const char * av_get_profile_name (const AVCodec *codec, int profile)
 Return a name for the specified profile, if available. More...
 
const char * avcodec_profile_name (enum AVCodecID codec_id, int profile)
 Return a name for the specified profile, if available. More...
 
unsigned avcodec_version (void)
 Return the LIBAVCODEC_VERSION_INT constant. More...
 
const char * avcodec_configuration (void)
 Return the libavcodec build-time configuration. More...
 
const char * avcodec_license (void)
 Return the libavcodec license. More...
 
int av_get_exact_bits_per_sample (enum AVCodecID codec_id)
 Return codec bits per sample. More...
 
enum AVCodecID av_get_pcm_codec (enum AVSampleFormat fmt, int be)
 Return the PCM codec associated with a sample format. More...
 
int av_get_bits_per_sample (enum AVCodecID codec_id)
 Return codec bits per sample. More...
 
static int get_audio_frame_duration (enum AVCodecID id, int sr, int ch, int ba, uint32_t tag, int bits_per_coded_sample, int64_t bitrate, uint8_t *extradata, int frame_size, int frame_bytes)
 
int av_get_audio_frame_duration (AVCodecContext *avctx, int frame_bytes)
 Return audio frame duration. More...
 
int av_get_audio_frame_duration2 (AVCodecParameters *par, int frame_bytes)
 This function is the same as av_get_audio_frame_duration(), except it works with AVCodecParameters instead of an AVCodecContext. More...
 
int ff_thread_init (AVCodecContext *s)
 
unsigned int av_xiphlacing (unsigned char *s, unsigned int v)
 Encode extradata length to a buffer. More...
 
int ff_match_2uint16 (const uint16_t(*tab)[2], int size, int a, int b)
 Return the index into tab at which {a,b} match elements {[0],[1]} of tab. More...
 
const AVCodecHWConfigavcodec_get_hw_config (const AVCodec *codec, int index)
 Retrieve supported hardware configurations for a codec. More...
 
AVHWAccelav_hwaccel_next (const AVHWAccel *hwaccel)
 If hwaccel is NULL, returns the first registered hardware accelerator, if hwaccel is non-NULL, returns the next registered hardware accelerator after hwaccel, or NULL if hwaccel is the last one. More...
 
void av_register_hwaccel (AVHWAccel *hwaccel)
 Register the hardware accelerator hwaccel. More...
 
int av_lockmgr_register (int(*cb)(void **mutex, enum AVLockOp op))
 Register a user provided lock manager supporting the operations specified by AVLockOp. More...
 
unsigned int avpriv_toupper4 (unsigned int x)
 
int ff_thread_ref_frame (ThreadFrame *dst, ThreadFrame *src)
 
enum AVPixelFormat ff_thread_get_format (AVCodecContext *avctx, const enum AVPixelFormat *fmt)
 Wrapper around get_format() for frame-multithreaded codecs. More...
 
int ff_thread_get_buffer (AVCodecContext *avctx, ThreadFrame *f, int flags)
 Wrapper around get_buffer() for frame-multithreaded codecs. More...
 
void ff_thread_release_buffer (AVCodecContext *avctx, ThreadFrame *f)
 Wrapper around release_buffer() frame-for multithreaded codecs. More...
 
void ff_thread_finish_setup (AVCodecContext *avctx)
 If the codec defines update_thread_context(), call this when they are ready for the next thread to start decoding the next frame. More...
 
void ff_thread_report_progress (ThreadFrame *f, int progress, int field)
 Notify later decoding threads when part of their reference picture is ready. More...
 
void ff_thread_await_progress (ThreadFrame *f, int progress, int field)
 Wait for earlier decoding threads to finish reference pictures. More...
 
int ff_thread_can_start_frame (AVCodecContext *avctx)
 
int ff_alloc_entries (AVCodecContext *avctx, int count)
 
void ff_reset_entries (AVCodecContext *avctx)
 
void ff_thread_await_progress2 (AVCodecContext *avctx, int field, int thread, int shift)
 
void ff_thread_report_progress2 (AVCodecContext *avctx, int field, int thread, int n)
 
int avcodec_is_open (AVCodecContext *s)
 
int avpriv_bprint_to_extradata (AVCodecContext *avctx, struct AVBPrint *buf)
 Finalize buf into extradata and set its size appropriately. More...
 
const uint8_tavpriv_find_start_code (const uint8_t *av_restrict p, const uint8_t *end, uint32_t *av_restrict state)
 
AVCPBPropertiesav_cpb_properties_alloc (size_t *size)
 Allocate a CPB properties structure and initialize its fields to default values. More...
 
AVCPBPropertiesff_add_cpb_side_data (AVCodecContext *avctx)
 Add a CPB properties side data to an encoding context. More...
 
static void codec_parameters_reset (AVCodecParameters *par)
 
AVCodecParametersavcodec_parameters_alloc (void)
 Allocate a new AVCodecParameters and set its fields to default values (unknown/invalid/0). More...
 
void avcodec_parameters_free (AVCodecParameters **ppar)
 Free an AVCodecParameters instance and everything associated with it and write NULL to the supplied pointer. More...
 
int avcodec_parameters_copy (AVCodecParameters *dst, const AVCodecParameters *src)
 Copy the contents of src to dst. More...
 
int avcodec_parameters_from_context (AVCodecParameters *par, const AVCodecContext *codec)
 Fill the parameters struct based on the values from the supplied codec context. More...
 
int avcodec_parameters_to_context (AVCodecContext *codec, const AVCodecParameters *par)
 Fill the codec context based on the values from the supplied codec parameters. More...
 
int ff_alloc_a53_sei (const AVFrame *frame, size_t prefix_len, void **data, size_t *sei_size)
 Check AVFrame for A53 side data and allocate and fill SEI message with A53 info. More...
 
int64_t ff_guess_coded_bitrate (AVCodecContext *avctx)
 Get an estimated video bitrate based on frame size, frame rate and coded bits per pixel. More...
 

Variables

const char av_codec_ffversion [] = "FFmpeg version " FFMPEG_VERSION
 
static AVMutex codec_mutex = AV_MUTEX_INITIALIZER
 

Detailed Description

utils.

Definition in file utils.c.

Macro Definition Documentation

◆ TAG_PRINT

#define TAG_PRINT (   x)
Value:
(((x) >= '0' && (x) <= '9') || \
((x) >= 'a' && (x) <= 'z') || ((x) >= 'A' && (x) <= 'Z') || \
((x) == '.' || (x) == ' ' || (x) == '-' || (x) == '_'))

Referenced by av_get_codec_tag_string().

◆ LICENSE_PREFIX

#define LICENSE_PREFIX   "libavcodec license: "

Referenced by avcodec_license().

Function Documentation

◆ ff_set_dimensions()

int ff_set_dimensions ( AVCodecContext s,
int  width,
int  height 
)

Check that the provided frame dimensions are valid and set them on the codec context.

Definition at line 104 of file utils.c.

Referenced by alloc_buffers(), aom_decode(), apply_param_change(), avcodec_open2(), avs_decode_init(), bmp_decode_frame(), cdxl_decode_frame(), cfhd_decode(), clv_decode_init(), cmv_process_header(), decode_frame(), decode_frame_headers(), decode_header(), decode_idat_chunk(), decode_init(), decode_seq_header(), dirac_decode_data_unit(), dnxhd_decode_frame(), dvdsub_parse_extradata(), dvvideo_decode_frame(), execute_code(), ff_exp2fi(), ff_h263_decode_frame(), ff_ivi_decode_frame(), ff_mjpeg_decode_sof(), ff_pnm_decode_header(), ff_rv34_decode_frame(), ff_vc1_decode_entry_point(), ff_vc1_decode_sequence_header(), ff_vp56_decode_frame(), ffmal_update_format(), fits_decode_frame(), flashsv_decode_frame(), g2m_decode_frame(), get_siz(), gif_decode_frame(), h261_decode_frame(), init_image(), libopenjpeg_decode_frame(), librsvg_decode_frame(), magy_decode_frame(), mediacodec_dec_parse_format(), mimic_decode_frame(), mpeg4_decode_header(), mpeg_decode_postinit(), mpegvideo_extract_headers(), mvc_decode_init(), parse_presentation_segment(), pcx_decode_frame(), pix_decode_frame(), pixlet_decode_frame(), process_frame_obj(), ptx_decode_frame(), read_huffman_tree(), rv20_decode_picture_header(), seqvideo_decode_init(), smvjpeg_decode_frame(), sunrast_decode_frame(), svc_decode_frame(), svq1_decode_frame(), tdsc_parse_tdsf(), tgq_decode_frame(), tgv_decode_frame(), tqi_decode_frame(), truemotion1_decode_header(), truemotion2rt_decode_header(), txd_decode_frame(), update_dimensions(), update_size(), v4l2_handle_event(), vp56_size_changed(), vp5_parse_header(), vp6_parse_header(), vp8_lossless_decode_frame(), vpx_decode(), xbm_decode_frame(), and xpm_decode_frame().

◆ ff_set_sar()

int ff_set_sar ( AVCodecContext avctx,
AVRational  sar 
)

◆ ff_side_data_update_matrix_encoding()

int ff_side_data_update_matrix_encoding ( AVFrame frame,
enum AVMatrixEncoding  matrix_encoding 
)

Add or update AV_FRAME_DATA_MATRIXENCODING side data.

Definition at line 134 of file utils.c.

Referenced by ac3_decode_frame(), ff_dca_core_filter_frame(), ff_dca_lbr_filter_frame(), ff_dca_xll_filter_frame(), ff_exp2fi(), and output_data().

◆ ff_color_frame()

void ff_color_frame ( AVFrame frame,
const int  c[4] 
)

Definition at line 408 of file utils.c.

Referenced by h264_frame_start().

◆ avcodec_default_execute2()

int avcodec_default_execute2 ( AVCodecContext c,
int(*)(AVCodecContext *c2, void *arg2, int jobnr, int threadnr)  func,
void arg,
int ret,
int  count 
)

Definition at line 444 of file utils.c.

◆ avpriv_find_pix_fmt()

enum AVPixelFormat avpriv_find_pix_fmt ( const PixelFormatTag tags,
unsigned int  fourcc 
)

◆ avpriv_codec_get_cap_skip_frame_fill_param()

int avpriv_codec_get_cap_skip_frame_fill_param ( const AVCodec codec)

Definition at line 486 of file utils.c.

Referenced by ff_exp2fi(), try_decode_frame(), and try_decode_video_frame().

◆ get_bit_rate()

static int64_t get_bit_rate ( AVCodecContext ctx)
static

Definition at line 490 of file utils.c.

Referenced by avcodec_open2(), and avcodec_string().

◆ ff_lock_avcodec()

static void ff_lock_avcodec ( AVCodecContext log_ctx,
const AVCodec codec 
)
static

Definition at line 514 of file utils.c.

Referenced by avcodec_open2(), and ff_codec_open2_recursive().

◆ ff_unlock_avcodec()

static void ff_unlock_avcodec ( const AVCodec codec)
static

Definition at line 520 of file utils.c.

Referenced by avcodec_open2(), and ff_codec_open2_recursive().

◆ ff_codec_open2_recursive()

int attribute_align_arg ff_codec_open2_recursive ( AVCodecContext avctx,
const AVCodec codec,
AVDictionary **  options 
)

Call avcodec_open2 recursively by decrementing counter, unlocking mutex, calling the function and then restoring again.

Assumes the mutex is already locked

Definition at line 526 of file utils.c.

Referenced by ff_exp2fi(), init(), smvjpeg_decode_init(), and tdsc_init().

◆ get_audio_frame_duration()

static int get_audio_frame_duration ( enum AVCodecID  id,
int  sr,
int  ch,
int  ba,
uint32_t  tag,
int  bits_per_coded_sample,
int64_t  bitrate,
uint8_t extradata,
int  frame_size,
int  frame_bytes 
)
static

Definition at line 1513 of file utils.c.

Referenced by av_get_audio_frame_duration(), and av_get_audio_frame_duration2().

◆ ff_thread_init()

int ff_thread_init ( AVCodecContext s)

Definition at line 1719 of file utils.c.

Referenced by avcodec_open2().

◆ ff_match_2uint16()

int ff_match_2uint16 ( const uint16_t(*)  tab[2],
int  size,
int  a,
int  b 
)

Return the index into tab at which {a,b} match elements {[0],[1]} of tab.

If there is no such matching pair then size is returned.

Definition at line 1740 of file utils.c.

Referenced by ff_h263_encode_picture_header(), ff_mpv_encode_init(), and svq1_write_header().

◆ avpriv_toupper4()

unsigned int avpriv_toupper4 ( unsigned int  x)

◆ ff_thread_ref_frame()

int ff_thread_ref_frame ( ThreadFrame dst,
ThreadFrame src 
)

◆ ff_thread_get_format()

enum AVPixelFormat ff_thread_get_format ( AVCodecContext avctx,
const enum AVPixelFormat fmt 
)

Wrapper around get_format() for frame-multithreaded codecs.

Call this function instead of avctx->get_format(). Cannot be called after the codec has called ff_thread_finish_setup().

Parameters
avctxThe current context.
fmtThe list of available formats.

Definition at line 1808 of file utils.c.

◆ ff_thread_get_buffer()

int ff_thread_get_buffer ( AVCodecContext avctx,
ThreadFrame f,
int  flags 
)

Wrapper around get_buffer() for frame-multithreaded codecs.

Call this function instead of ff_get_buffer(f). Cannot be called after the codec has called ff_thread_finish_setup().

Parameters
avctxThe current context.
fThe frame to write into.

Definition at line 1813 of file utils.c.

◆ ff_thread_release_buffer()

void ff_thread_release_buffer ( AVCodecContext avctx,
ThreadFrame f 
)

Wrapper around release_buffer() frame-for multithreaded codecs.

Call this function instead of avctx->release_buffer(f). The AVFrame will be copied and the actual release_buffer() call will be performed later. The contents of data pointed to by the AVFrame should not be changed until ff_thread_get_buffer() is called on it.

Parameters
avctxThe current context.
fThe picture being released.

Definition at line 1819 of file utils.c.

Referenced by ff_thread_ref_frame().

◆ ff_thread_finish_setup()

void ff_thread_finish_setup ( AVCodecContext avctx)

If the codec defines update_thread_context(), call this when they are ready for the next thread to start decoding the next frame.

After calling it, do not change any variables read by the update_thread_context() method, or call ff_thread_get_buffer().

Parameters
avctxThe context.

Definition at line 1825 of file utils.c.

◆ ff_thread_report_progress()

void ff_thread_report_progress ( ThreadFrame f,
int  progress,
int  field 
)

Notify later decoding threads when part of their reference picture is ready.

Call this when some part of the picture is finished decoding. Later calls with lower values of progress have no effect.

Parameters
fThe picture being decoded.
progressValue, in arbitrary units, of how much of the picture has decoded.
fieldThe field being decoded, for field-picture codecs. 0 for top field or frame pictures, 1 for bottom field.

Definition at line 1829 of file utils.c.

◆ ff_thread_await_progress()

void ff_thread_await_progress ( ThreadFrame f,
int  progress,
int  field 
)

Wait for earlier decoding threads to finish reference pictures.

Call this before accessing some part of a picture, with a given value for progress, and it will return after the responsible decoding thread calls ff_thread_report_progress() with the same or higher value for progress.

Parameters
fThe picture being referenced.
progressValue, in arbitrary units, to wait for.
fieldThe field being referenced, for field-picture codecs. 0 for top field or frame pictures, 1 for bottom field.

Definition at line 1833 of file utils.c.

◆ ff_thread_can_start_frame()

int ff_thread_can_start_frame ( AVCodecContext avctx)

Definition at line 1837 of file utils.c.

Referenced by ff_exp2fi(), ff_mpv_frame_start(), and h264_frame_start().

◆ ff_alloc_entries()

int ff_alloc_entries ( AVCodecContext avctx,
int  count 
)

Definition at line 1842 of file utils.c.

◆ ff_reset_entries()

void ff_reset_entries ( AVCodecContext avctx)

Definition at line 1847 of file utils.c.

◆ ff_thread_await_progress2()

void ff_thread_await_progress2 ( AVCodecContext avctx,
int  field,
int  thread,
int  shift 
)

Definition at line 1851 of file utils.c.

◆ ff_thread_report_progress2()

void ff_thread_report_progress2 ( AVCodecContext avctx,
int  field,
int  thread,
int  n 
)

Definition at line 1855 of file utils.c.

◆ avpriv_bprint_to_extradata()

int avpriv_bprint_to_extradata ( AVCodecContext avctx,
struct AVBPrint *  buf 
)

Finalize buf into extradata and set its size appropriately.

Definition at line 1866 of file utils.c.

Referenced by dvdsub_init(), and ff_exp2fi().

◆ avpriv_find_start_code()

const uint8_t* avpriv_find_start_code ( const uint8_t *av_restrict  p,
const uint8_t end,
uint32_t *av_restrict  state 
)

Definition at line 1889 of file utils.c.

◆ ff_add_cpb_side_data()

AVCPBProperties* ff_add_cpb_side_data ( AVCodecContext avctx)

Add a CPB properties side data to an encoding context.

Definition at line 1936 of file utils.c.

Referenced by aom_init(), ff_exp2fi(), ff_mpv_encode_init(), nvenc_setup_encoder(), qsv_retrieve_enc_params(), svc_encode_init(), vpx_init(), and X264_init().

◆ codec_parameters_reset()

static void codec_parameters_reset ( AVCodecParameters par)
static

◆ ff_alloc_a53_sei()

int ff_alloc_a53_sei ( const AVFrame frame,
size_t  prefix_len,
void **  data,
size_t *  sei_size 
)

Check AVFrame for A53 side data and allocate and fill SEI message with A53 info.

Parameters
frameRaw frame to get A53 side data from
prefix_lenNumber of bytes to allocate before SEI message
dataPointer to a variable to store allocated memory Upon return the variable will hold NULL on error or if frame has no A53 info. Otherwise it will point to prefix_len uninitialized bytes followed by *sei_size SEI message
sei_sizePointer to a variable to store generated SEI message length
Returns
Zero on success, negative error code on failure

'GA94' is standard in North America for ATSC, but hard coding this style may not be the right thing to do – other formats do exist. This information is not available in the side_data so we are going with this right now.

Definition at line 2135 of file utils.c.

Referenced by ff_exp2fi(), qsv_h264_set_encode_ctrl(), vtenc_send_frame(), and X264_frame().

◆ ff_guess_coded_bitrate()

int64_t ff_guess_coded_bitrate ( AVCodecContext avctx)

Get an estimated video bitrate based on frame size, frame rate and coded bits per pixel.

Definition at line 2178 of file utils.c.

Referenced by encode_init(), ff_exp2fi(), raw_encode_init(), v308_encode_init(), v408_encode_init(), v410_encode_init(), and y41p_encode_init().

Variable Documentation

◆ av_codec_ffversion

const char av_codec_ffversion[] = "FFmpeg version " FFMPEG_VERSION

Definition at line 66 of file utils.c.

◆ codec_mutex

AVMutex codec_mutex = AV_MUTEX_INITIALIZER
static

Definition at line 68 of file utils.c.

Referenced by ff_lock_avcodec(), and ff_unlock_avcodec().