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

Modules

 Frame parsing
 

Macros

#define AV_INPUT_BUFFER_PADDING_SIZE   32
 Required number of additionally allocated bytes at the end of the input bitstream for decoding. More...
 

Enumerations

enum  AVDiscard {
  AVDISCARD_NONE =-16, AVDISCARD_DEFAULT = 0, AVDISCARD_NONREF = 8, AVDISCARD_BIDIR = 16,
  AVDISCARD_NONINTRA = 24, AVDISCARD_NONKEY = 32, AVDISCARD_ALL = 48
}
 

Functions

AVCodecavcodec_find_decoder (enum AVCodecID id)
 Find a registered decoder with a matching codec ID. More...
 
AVCodecavcodec_find_decoder_by_name (const char *name)
 Find a registered decoder with the specified name. More...
 
int avcodec_default_get_buffer2 (AVCodecContext *s, AVFrame *frame, int flags)
 The default callback for AVCodecContext.get_buffer2(). More...
 
attribute_deprecated unsigned avcodec_get_edge_width (void)
 Return the amount of padding in pixels which the get_buffer callback must provide around the edge of the image for codecs which do not have the CODEC_FLAG_EMU_EDGE flag. 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...
 
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...
 
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...
 
attribute_deprecated int avcodec_decode_audio4 (AVCodecContext *avctx, AVFrame *frame, int *got_frame_ptr, const AVPacket *avpkt)
 Decode the audio frame of size avpkt->size from avpkt->data into frame. More...
 
attribute_deprecated int avcodec_decode_video2 (AVCodecContext *avctx, AVFrame *picture, int *got_picture_ptr, const AVPacket *avpkt)
 Decode the video frame of size avpkt->size from avpkt->data into picture. More...
 
int avcodec_decode_subtitle2 (AVCodecContext *avctx, AVSubtitle *sub, int *got_sub_ptr, AVPacket *avpkt)
 Decode a subtitle message. More...
 
int avcodec_send_packet (AVCodecContext *avctx, const AVPacket *avpkt)
 Supply raw packet data as input to a decoder. More...
 
int avcodec_receive_frame (AVCodecContext *avctx, AVFrame *frame)
 Return decoded output data from a decoder. More...
 
int avcodec_send_frame (AVCodecContext *avctx, const AVFrame *frame)
 Supply a raw video or audio frame to the encoder. More...
 
int avcodec_receive_packet (AVCodecContext *avctx, AVPacket *avpkt)
 Read encoded data from the encoder. More...
 

Detailed Description

Macro Definition Documentation

#define AV_INPUT_BUFFER_PADDING_SIZE   32

Required number of additionally allocated bytes at the end of the input bitstream for decoding.

This is mainly needed because some optimized bitstream readers read 32 or 64 bit at once and could read over the end.
Note: If the first 23 bits of the additional bytes are not 0, then damaged MPEG bitstreams could cause overread and segfault.

Examples:
decode_audio.c, decode_video.c, and qsvdec.c.

Definition at line 769 of file avcodec.h.

Referenced by a64multi_encode_init(), aac_adtstoasc_filter(), aac_decode_frame(), aac_encode_init(), aac_sync(), ac3_sync(), adpcm_decode_frame(), adpcm_encode_init(), alac_encode_init(), alloc_buffers(), allocate_buffers(), ape_tag_read_field(), apng_read_header(), apng_write_header(), append_extradata(), apply_color_indexing_transform(), asf_parse_packet(), asf_read_stream_properties(), atrac3_decode_init(), av_bitstream_filter_filter(), av_fast_padded_malloc(), av_fast_padded_mallocz(), av_grow_packet(), av_packet_from_data(), av_packet_merge_side_data(), av_packet_new_side_data(), av_packet_split_side_data(), av_parser_change(), av_parser_parse2(), av_shrink_packet(), avcodec_copy_context(), avcodec_decode_subtitle2(), avcodec_encode_audio2(), avcodec_encode_video2(), avcodec_parameters_copy(), avcodec_parameters_from_context(), avcodec_parameters_to_context(), avformat_find_stream_info(), avi_read_header(), avio_close_dyn_buf(), avui_encode_init(), cdxl_decode_frame(), codec_reinit(), cook_decode_init(), copy_to_pbr(), create_new_buffer(), daala_header(), dca_parse_params(), decode_extradata_ps_mp4(), decode_frame(), decode_init(), decode_plane(), decode_plane10(), dshow_add_device(), dts_probe(), dv_decode_video_segment(), encode_frame(), encode_init(), extract_extradata_h2645(), extract_extradata_mpeg12(), extract_extradata_mpeg4(), extract_extradata_vc1(), extract_header(), ff_alloc_extradata(), ff_alloc_packet2(), ff_avc_write_annexb_extradata(), ff_combine_frame(), ff_flac_parse_picture(), ff_h2645_extract_rbsp(), ff_h264_parse_sprop_parameter_sets(), ff_id3v2_parse_apic(), ff_jpeg2000_init_component(), ff_mjpeg_find_marker(), ff_mov_read_stsd_entries(), ff_mpeg_update_thread_context(), ff_mpv_encode_picture(), ff_parse_mpeg2_descriptor(), ff_vaapi_encode_init(), ffat_init_encoder(), ffm2_read_header(), ffm_read_header(), fixup_vorbis_headers(), flac_read_header(), flic_decode_frame_8BPP(), flush_packet(), flv_queue_extradata(), flv_write_packet(), g2m_init_buffers(), generate_fake_vps(), h264_mp4toannexb_init(), handle_packets(), hevc_extradata_to_annexb(), hevc_parse_sdp_line(), id3_has_changed_values(), iff_read_header(), imc_decode_frame(), init_bsf(), init_fps(), jacosub_read_header(), jpg_decode_data(), latm_decode_audio_specific_config(), libkvazaar_init(), libopus_encode_init(), libvorbis_encode_init(), libx265_encode_init(), main(), matroska_parse_tracks(), mov_read_atom_into_extradata(), mov_read_ddts(), mov_read_stsd(), mov_read_stsz(), mov_read_wave(), mov_realloc_extradata(), mov_rewrite_dvd_sub_extradata(), mov_text_encode_init(), mov_write_dvc1_structs(), mov_write_single_packet(), mp3_header_decompress(), mpc8_parse_seektable(), mpegts_push_data(), mxg_update_cache(), nal_unit_extract_rbsp(), new_pes_packet(), nvenc_setup_extradata(), ogg_new_buf(), ogg_new_stream(), ogg_read_page(), ogg_save(), ogm_header(), omx_encode_frame(), omx_encode_init(), opus_encode_init(), packet_alloc(), parse_fmtp_config(), parse_packed_headers(), parse_video_info(), parse_vtrk(), pcx_decode_frame(), qsv_retrieve_enc_params(), qt_rtp_parse_packet(), rdt_parse_b64buf(), read_apic(), read_header(), read_kuki_chunk(), read_packet(), read_sl_header(), recode_subtitle(), rm_read_audio_stream_info(), sao_edge_filter(), sao_filter_CTB(), set_extradata(), shn_probe(), shorten_decode_frame(), spdif_write_packet(), svc_encode_init(), svq3_decode_slice_header(), tak_read_header(), theora_header(), tiff_unpack_fax(), utvideo_encode_init(), vc1_decode_frame(), vc1_decode_init(), vfw_read_header(), wma_decode_superframe(), wmv2_encode_init(), wrapped_avframe_encode(), write_extradata(), wv_get_value_float(), and X264_init().

Enumeration Type Documentation

enum AVDiscard
Enumerator
AVDISCARD_NONE 

discard nothing

AVDISCARD_DEFAULT 

discard useless packets like 0 size packets in avi

AVDISCARD_NONREF 

discard all non reference

AVDISCARD_BIDIR 

discard all bidirectional frames

AVDISCARD_NONINTRA 

discard all non intra frames

AVDISCARD_NONKEY 

discard all frames except keyframes

AVDISCARD_ALL 

discard all

Definition at line 813 of file avcodec.h.

Function Documentation

AVCodec* avcodec_find_decoder ( enum AVCodecID  id)
AVCodec* avcodec_find_decoder_by_name ( const char *  name)

Find a registered decoder with the specified name.

Parameters
namename of the requested decoder
Returns
A decoder if one was found, NULL otherwise.
Examples:
qsvdec.c.

Definition at line 3173 of file utils.c.

Referenced by find_codec_or_die(), find_probe_decoder(), main(), show_help_codec(), stream_component_open(), and v4l2_read_header().

int avcodec_default_get_buffer2 ( AVCodecContext s,
AVFrame frame,
int  flags 
)

The default callback for AVCodecContext.get_buffer2().

It is made public so it can be called by custom get_buffer2() implementations for decoders without AV_CODEC_CAP_DR1 set.

Definition at line 728 of file utils.c.

Referenced by alloc_frame_buffer(), ff_thread_release_buffer(), get_buffer(), init_context_defaults(), submit_packet(), and thread_get_buffer_internal().

attribute_deprecated unsigned avcodec_get_edge_width ( void  )

Return the amount of padding in pixels which the get_buffer callback must provide around the edge of the image for codecs which do not have the CODEC_FLAG_EMU_EDGE flag.

Returns
Required padding in pixels.
Deprecated:
CODEC_FLAG_EMU_EDGE is deprecated, so this function is no longer needed

Definition at line 194 of file utils.c.

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.

May only be used if a codec with AV_CODEC_CAP_DR1 has been opened.

Definition at line 437 of file utils.c.

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].

May only be used if a codec with AV_CODEC_CAP_DR1 has been opened.

Definition at line 260 of file utils.c.

Referenced by avcodec_align_dimensions(), and update_frame_pool().

int avcodec_enum_to_chroma_pos ( int xpos,
int ypos,
enum AVChromaLocation  pos 
)

Converts AVChromaLocation to swscale x/y chroma position.

The positions represent the chroma (0,0) position in a coordinates system with luma (0,0) representing the origin and luma(1,1) representing 256,256

Parameters
xposhorizontal chroma sample position
yposvertical chroma sample position

Definition at line 452 of file utils.c.

Referenced by avcodec_chroma_pos_to_enum(), and mkv_write_video_color().

enum AVChromaLocation avcodec_chroma_pos_to_enum ( int  xpos,
int  ypos 
)

Converts swscale x/y chroma position to AVChromaLocation.

The positions represent the chroma (0,0) position in a coordinates system with luma (0,0) representing the origin and luma(1,1) representing 256,256

Parameters
xposhorizontal chroma sample position
yposvertical chroma sample position

Definition at line 464 of file utils.c.

Referenced by mkv_parse_video_color().

attribute_deprecated int avcodec_decode_audio4 ( AVCodecContext avctx,
AVFrame frame,
int got_frame_ptr,
const AVPacket avpkt 
)

Decode the audio frame of size avpkt->size from avpkt->data into frame.

Some decoders may support multiple frames in a single AVPacket. Such decoders would then just decode the first frame and the return value would be less than the packet size. In this case, avcodec_decode_audio4 has to be called again with an AVPacket containing the remaining data in order to decode the second frame, etc... Even if no frames are returned, the packet needs to be fed to the decoder with remaining data until it is completely consumed or an error occurs.

Some decoders (those marked with AV_CODEC_CAP_DELAY) have a delay between input and output. This means that for some packets they will not immediately produce decoded output and need to be flushed at the end of decoding to get all the decoded data. Flushing is done by calling this function with packets with avpkt->data set to NULL and avpkt->size set to 0 until it stops returning samples. It is safe to flush even those decoders that are not marked with AV_CODEC_CAP_DELAY, then no samples will be returned.

Warning
The input buffer, avpkt->data must be AV_INPUT_BUFFER_PADDING_SIZE larger than the actual read bytes because some optimized bitstream readers read 32 or 64 bits at once and could read over the end.
Note
The AVCodecContext MUST have been opened with avcodec_open2() before packets may be fed to the decoder.
Parameters
avctxthe codec context
[out]frameThe AVFrame in which to store decoded audio samples. The decoder will allocate a buffer for the decoded frame by calling the AVCodecContext.get_buffer2() callback. When AVCodecContext.refcounted_frames is set to 1, the frame is reference counted and the returned reference belongs to the caller. The caller must release the frame using av_frame_unref() when the frame is no longer needed. The caller may safely write to the frame if av_frame_is_writable() returns 1. When AVCodecContext.refcounted_frames is set to 0, the returned reference belongs to the decoder and is valid only until the next call to this function or until closing or flushing the decoder. The caller may not write to it.
[out]got_frame_ptrZero if no frame could be decoded, otherwise it is non-zero. Note that this field being set to zero does not mean that an error has occurred. For decoders with AV_CODEC_CAP_DELAY set, no given decode call is guaranteed to produce a frame.
[in]avpktThe input AVPacket containing the input buffer. At least avpkt->data and avpkt->size should be set. Some decoders might also require additional fields to be set.
Returns
A negative error code is returned if an error occurred during decoding, otherwise the number of bytes consumed from the input AVPacket is returned.
Deprecated:
Use avcodec_send_packet() and avcodec_receive_frame().
Examples:
decode_audio.c, demuxing_decoding.c, transcode_aac.c, and transcoding.c.

Definition at line 2336 of file utils.c.

Referenced by decode_audio_frame(), decode_packet(), decoder_decode_frame(), do_decode(), main(), movie_push_frame(), process_frame(), run_test(), and wrap().

attribute_deprecated int avcodec_decode_video2 ( AVCodecContext avctx,
AVFrame picture,
int got_picture_ptr,
const AVPacket avpkt 
)

Decode the video frame of size avpkt->size from avpkt->data into picture.

Some decoders may support multiple frames in a single AVPacket, such decoders would then just decode the first frame.

Warning
The input buffer must be AV_INPUT_BUFFER_PADDING_SIZE larger than the actual read bytes because some optimized bitstream readers read 32 or 64 bits at once and could read over the end.
The end of the input buffer buf should be set to 0 to ensure that no overreading happens for damaged MPEG streams.
Note
Codecs which have the AV_CODEC_CAP_DELAY capability set have a delay between input and output, these need to be fed with avpkt->data=NULL, avpkt->size=0 at the end to return the remaining frames.
The AVCodecContext MUST have been opened with avcodec_open2() before packets may be fed to the decoder.
Parameters
avctxthe codec context
[out]pictureThe AVFrame in which the decoded video frame will be stored. Use av_frame_alloc() to get an AVFrame. The codec will allocate memory for the actual bitmap by calling the AVCodecContext.get_buffer2() callback. When AVCodecContext.refcounted_frames is set to 1, the frame is reference counted and the returned reference belongs to the caller. The caller must release the frame using av_frame_unref() when the frame is no longer needed. The caller may safely write to the frame if av_frame_is_writable() returns 1. When AVCodecContext.refcounted_frames is set to 0, the returned reference belongs to the decoder and is valid only until the next call to this function or until closing or flushing the decoder. The caller may not write to it.
[in]avpktThe input AVPacket containing the input buffer. You can create such packet with av_init_packet() and by then setting data and size, some decoders might in addition need other fields like flags&AV_PKT_FLAG_KEY. All decoders are designed to use the least fields possible.
[in,out]got_picture_ptrZero if no frame could be decompressed, otherwise, it is nonzero.
Returns
On error a negative value is returned, otherwise the number of bytes used or zero if no frame could be decompressed.
Deprecated:
Use avcodec_send_packet() and avcodec_receive_frame().
Examples:
decode_video.c, demuxing_decoding.c, qsvdec.c, and transcoding.c.

Definition at line 2227 of file utils.c.

Referenced by compute_crc_of_packets(), decode_frame(), decode_packet(), decode_write_frame(), decoder_decode_frame(), do_decode(), ff_load_image(), main(), movie_push_frame(), process_frame(), smvjpeg_decode_frame(), try_decode_video_frame(), video_decode(), video_decode_example(), and wrap().

int avcodec_decode_subtitle2 ( AVCodecContext avctx,
AVSubtitle sub,
int got_sub_ptr,
AVPacket avpkt 
)

Decode a subtitle message.

Return a negative value on error, otherwise return the number of bytes used. If no subtitle could be decompressed, got_sub_ptr is zero. Otherwise, the subtitle is stored in *sub. Note that AV_CODEC_CAP_DR1 is not available for subtitle codecs. This is for simplicity, because the performance difference is expect to be negligible and reusing a get_buffer written for video codecs would probably perform badly due to a potentially very different allocation pattern.

Some decoders (those marked with CODEC_CAP_DELAY) have a delay between input and output. This means that for some packets they will not immediately produce decoded output and need to be flushed at the end of decoding to get all the decoded data. Flushing is done by calling this function with packets with avpkt->data set to NULL and avpkt->size set to 0 until it stops returning subtitles. It is safe to flush even those decoders that are not marked with CODEC_CAP_DELAY, then no subtitles will be returned.

Note
The AVCodecContext MUST have been opened with avcodec_open2() before packets may be fed to the decoder.
Parameters
avctxthe codec context
[out]subThe Preallocated AVSubtitle in which the decoded subtitle will be stored, must be freed with avsubtitle_free if *got_sub_ptr is set.
[in,out]got_sub_ptrZero if no subtitle could be decompressed, otherwise, it is nonzero.
[in]avpktThe input AVPacket containing the input buffer.

Definition at line 2669 of file utils.c.

Referenced by decoder_decode_frame(), process_frame(), transcode_subtitles(), try_decode_frame(), and wrap().

int avcodec_send_packet ( AVCodecContext avctx,
const AVPacket avpkt 
)

Supply raw packet data as input to a decoder.

Internally, this call will copy relevant AVCodecContext fields, which can influence decoding per-packet, and apply them when the packet is actually decoded. (For example AVCodecContext.skip_frame, which might direct the decoder to drop the frame contained by the packet sent with this function.)

Warning
The input buffer, avpkt->data must be AV_INPUT_BUFFER_PADDING_SIZE larger than the actual read bytes because some optimized bitstream readers read 32 or 64 bits at once and could read over the end.
Do not mix this API with the legacy API (like avcodec_decode_video2()) on the same AVCodecContext. It will return unexpected results now or in future libavcodec versions.
Note
The AVCodecContext MUST have been opened with avcodec_open2() before packets may be fed to the decoder.
Parameters
avctxcodec context
[in]avpktThe input AVPacket. Usually, this will be a single video frame, or several complete audio frames. Ownership of the packet remains with the caller, and the decoder will not write to the packet. The decoder may create a reference to the packet data (or copy it if the packet is not reference-counted). Unlike with older APIs, the packet is always fully consumed, and if it contains multiple frames (e.g. some audio codecs), will require you to call avcodec_receive_frame() multiple times afterwards before you can send a new packet. It can be NULL (or an AVPacket with data set to NULL and size set to 0); in this case, it is considered a flush packet, which signals the end of the stream. Sending the first flush packet will return success. Subsequent ones are unnecessary and will return AVERROR_EOF. If the decoder still has frames buffered, it will return them after sending a flush packet.
Returns
0 on success, otherwise negative error code: AVERROR(EAGAIN): input is not accepted in the current state - user must read output with avcodec_receive_frame() (once all output is read, the packet should be resent, and the call will not fail with EAGAIN). AVERROR_EOF: the decoder has been flushed, and no new packets can be sent to it (also returned if more than 1 flush packet is sent) AVERROR(EINVAL): codec not opened, it is an encoder, or requires flush AVERROR(ENOMEM): failed to add packet to internal queue, or similar other errors: legitimate decoding errors
Examples:
filtering_audio.c, and filtering_video.c.

Definition at line 2865 of file utils.c.

Referenced by decode(), main(), tdsc_decode_jpeg_tile(), try_decode_frame(), and wrap().

int avcodec_receive_frame ( AVCodecContext avctx,
AVFrame frame 
)

Return decoded output data from a decoder.

Parameters
avctxcodec context
frameThis will be set to a reference-counted video or audio frame (depending on the decoder type) allocated by the decoder. Note that the function will always call av_frame_unref(frame) before doing anything else.
Returns
0: success, a frame was returned AVERROR(EAGAIN): output is not available in this state - user must try to send new input AVERROR_EOF: the decoder has been fully flushed, and there will be no more output frames AVERROR(EINVAL): codec not opened, or it is an encoder other negative values: legitimate decoding errors
Examples:
filtering_audio.c, and filtering_video.c.

Definition at line 2920 of file utils.c.

Referenced by decode(), main(), tdsc_decode_jpeg_tile(), try_decode_frame(), and wrap().

int avcodec_send_frame ( AVCodecContext avctx,
const AVFrame frame 
)

Supply a raw video or audio frame to the encoder.

Use avcodec_receive_packet() to retrieve buffered output packets.

Parameters
avctxcodec context
[in]frameAVFrame containing the raw audio or video frame to be encoded. Ownership of the frame remains with the caller, and the encoder will not write to the frame. The encoder may create a reference to the frame data (or copy it if the frame is not reference-counted). It can be NULL, in which case it is considered a flush packet. This signals the end of the stream. If the encoder still has packets buffered, it will return them after this call. Once flushing mode has been entered, additional flush packets are ignored, and sending frames will return AVERROR_EOF.

For audio: If AV_CODEC_CAP_VARIABLE_FRAME_SIZE is set, then each frame can have any number of samples. If it is not set, frame->nb_samples must be equal to avctx->frame_size for all frames except the last. The final frame may be smaller than avctx->frame_size.

Returns
0 on success, otherwise negative error code: AVERROR(EAGAIN): input is not accepted in the current state - user must read output with avcodec_receive_packet() (once all output is read, the packet should be resent, and the call will not fail with EAGAIN). AVERROR_EOF: the encoder has been flushed, and no new frames can be sent to it AVERROR(EINVAL): codec not opened, refcounted_frames not set, it is a decoder, or requires flush AVERROR(ENOMEM): failed to add packet to internal queue, or similar other errors: legitimate decoding errors

Definition at line 3001 of file utils.c.

Referenced by do_audio_out(), do_video_out(), encode_frame(), flush_encoders(), and wrap().

int avcodec_receive_packet ( AVCodecContext avctx,
AVPacket avpkt 
)

Read encoded data from the encoder.

Parameters
avctxcodec context
avpktThis will be set to a reference-counted packet allocated by the encoder. Note that the function will always call av_frame_unref(frame) before doing anything else.
Returns
0 on success, otherwise negative error code: AVERROR(EAGAIN): output is not available in the current state - user must try to send input AVERROR_EOF: the encoder has been fully flushed, and there will be no more output packets AVERROR(EINVAL): codec not opened, or it is an encoder other errors: legitimate decoding errors

Definition at line 3031 of file utils.c.

Referenced by do_audio_out(), do_video_out(), encode_frame(), flush_encoders(), and wrap().