AVCodecContext
public final class AVCodecContext
Undocumented
-
Creates an
AVCodecContext
and set its fields to default values.Declaration
Swift
public init(codec: AVCodec? = nil)
Parameters
codec
codec
-
The codec’s media type.
Declaration
Swift
public var mediaType: AVMediaType { get }
-
Undocumented
Declaration
Swift
public var codec: AVCodec? { get set }
-
The codec’s id.
Declaration
Swift
public var codecId: AVCodecID { get set }
-
fourcc (LSB first, so
ABCD
-> (‘D’<<24) + (‘C’<<16) + (‘B’<<8) + ‘A’).This is used to work around some encoder bugs. A demuxer should set this to what is stored in the field used to identify the codec. If there are multiple such fields in a container then the demuxer should choose the one which maximizes the information about the used codec. If the codec tag field in a container is larger than 32 bits then the demuxer should remap the longer ID to 32 bits with a table or other structure. Alternatively a new extra_codec_tag + size could be added but for this a clear advantage must be demonstrated first.
- encoding: Set by user, if not then the default based on
codecId
will be used. - decoding: Set by user, will be converted to uppercase by libavcodec during init.
Declaration
Swift
public var codecTag: UInt32 { get set }
- encoding: Set by user, if not then the default based on
-
Private data of the user, can be used to carry app specific stuff.
- encoding: Set by user.
- decoding: Set by user.
Declaration
Swift
public var opaque: UnsafeMutableRawPointer? { get set }
-
The average bitrate.
- encoding: Set by user, unused for constant quantizer encoding.
- decoding: Set by user, may be overwritten by libavcodec if this info is available in the stream.
Declaration
Swift
public var bitRate: Int64 { get set }
-
Number of bits the bitstream is allowed to diverge from the reference. The reference can be CBR (for CBR pass1) or VBR (for pass2).
- encoding: Set by user, unused for constant quantizer encoding.
- decoding: Unused.
Declaration
Swift
public var bitRateTolerance: Int { get set }
-
- encoding: Set by user.
- decoding: Set by user.
Declaration
Swift
public var flags: Flag { get set }
-
- encoding: Set by user.
- decoding: Set by user.
Declaration
Swift
public var flags2: Flag2 { get set }
-
Some codecs need / can use extradata like Huffman tables.
- MJPEG: Huffman tables
- rv10: additional flags
- MPEG-4: global headers (they can be in the bitstream or here)
The allocated memory should be
AVConstant.inputBufferPaddingSize
bytes larger thanextradataSize
to avoid problems if it is read with the bitstream reader. The bytewise contents of extradata must not depend on the architecture or CPU endianness. Must be allocated with theAVIO.malloc(size:)
family of functions.- encoding: Set/allocated/freed by libavcodec.
- decoding: Set/allocated/freed by user.
Declaration
Swift
public var extradata: UnsafeMutablePointer<UInt8>? { get set }
-
The size of the extradata content in bytes.
Declaration
Swift
public var extradataSize: Int { get set }
-
This is the fundamental unit of time (in seconds) in terms of which frame timestamps are represented. For fixed-fps content, timebase should be 1/framerate and timestamp increments should be identically 1. This often, but not always is the inverse of the frame rate or field rate for video. 1/timebase is not the average frame rate if the frame rate is not constant.
Like containers, elementary streams also can store timestamps, 1/timebase is the unit in which these timestamps are specified. As example of such codec timebase see ISO/IEC 14496-2:2001(E) vop_time_increment_resolution and fixed_vop_rate (fixed_vop_rate == 0 implies that it is different from the framerate)
- encoding: Must be set by user.
- decoding: The use of this field for decoding is deprecated. Use framerate instead.
Declaration
Swift
public var timebase: AVRational { get set }
-
Frame counter.
- encoding: Total number of frames passed to the encoder so far.
- decoding: Total number of frames returned from the decoder so far.
Declaration
Swift
public var frameNumber: Int { get }
-
A reference to the
AVHWFramesContext
describing the input (for encoding) or output (decoding) frames. The reference is set by the caller and afterwards owned (and freed) by libavcodec - it should never be read by the caller after being set.- encoding: For hardware encoders configured to use a hwaccel pixel
format, this field should be set by the caller to a reference
to the
AVHWFramesContext
describing input frames.AVHWFramesContext.pixelFormat
must be equal toAVCodecContext.pixelFormat
.
This field should be set before
openCodec(_:options:)
is called.- decoding: This field should be set by the caller from the
getFormat
callback. The previous reference (if any) will always be unreffed by libavcodec before thegetFormat
call.
If the default
get_buffer2()
is used with a hwaccel pixel format, then thisAVHWFramesContext
will be used for allocating the frame buffers.Declaration
Swift
public var hwFramesContext: AVHWFramesContext? { get set }
- encoding: For hardware encoders configured to use a hwaccel pixel
format, this field should be set by the caller to a reference
to the
-
A reference to the
AVHWDeviceContext
describing the device which will be used by a hardware encoder/decoder. The reference is set by the caller and afterwards owned (and freed) by libavcodec.This should be used if either the codec device does not require hardware frames or any that are used are to be allocated internally by libavcodec. If the user wishes to supply any of the frames used as encoder input or decoder output then
hwFramesContext
should be used instead. WhenhwFramesContext
is set ingetFormat
for a decoder, this field will be ignored while decoding the associated stream segment, but may again be used on a following one after anothergetFormat
call.For both encoders and decoders this field should be set before
openCodec(_:options:)
is called and must not be written to thereafter.Note that some decoders may require this field to be set initially in order to support
hwFramesContext
at all - in that case, all frames contexts used must be created on the same device.Declaration
Swift
public var hwDeviceContext: AVHWDeviceContext? { get set }
-
A Boolean value indicating whether the codec is open.
Declaration
Swift
public var isOpen: Bool { get }
-
Fill the codec context based on the values from the supplied codec parameters.
Declaration
Swift
public func setParameters(_ params: AVCodecParameters)
Parameters
params
codec parameters
-
Initialize the
AVCodecContext
.Throws
AVErrorDeclaration
Swift
public func openCodec(_ codec: AVCodec? = nil, options: [String: String]? = nil) throws
Parameters
codec
The codec to open this context for. If a non-NULL codec has been previously passed to
init(codec:)
or for this context, then this parameter MUST be eithernil
or equal to the previously passed codec.options
A dictionary filled with
AVCodecContext
and codec-private options. -
Supply raw packet data as input to a decoder.
Throws
AVError.tryAgain
if input is not accepted in the current state - user must read output withreceiveFrame
. (once all output is read, the packet should be resent, and the call will not fail withAVError.tryAgain
).AVError.eof
if 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.invalidArgument
if codec not opened, it is an encoder, or requires flushAVError.outOfMemory
if failed to add packet to internal queue, or similar.- legitimate decoding errors
Declaration
Swift
public func sendPacket(_ packet: AVPacket?) throws
Parameters
packet
The input
AVPacket
. Usually, this will be a single video frame, or several complete audio frames. It can benil
(or anAVPacket
with data set tonil
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 throwAVError.eof
. If the decoder still has frames buffered, it will return them after sending a flush packet. -
Return decoded output data from a decoder.
Throws
AVError.tryAgain
if output is not available in this state - user must try to send new input.AVError.eof
if the decoder has been fully flushed, and there will be no more output frames.AVError.invalidArgument
if codec not opened, or it is an encoder.- legitimate decoding errors
Declaration
Swift
public func receiveFrame(_ frame: AVFrame) throws
Parameters
frame
This will be set to a reference-counted video or audio frame (depending on the decoder type) allocated by the decoder.
-
Supply a raw video or audio frame to the encoder.
Throws
AVError.tryAgain
if input is not accepted in the current state - user must read output withreceivePacket
. (once all output is read, the packet should be resent, and the call will not fail withAVError.tryAgain
).AVError.eof
if the encoder has been flushed, and no new frames can be sent to it.AVError.invalidArgument
if codec not opened, refcounted_frames not set, it is a decoder, or requires flush.AVError.outOfMemory
if failed to add packet to internal queue, or similar.- legitimate decoding errors
Declaration
Swift
public func sendFrame(_ frame: AVFrame?) throws
Parameters
frame
AVFrame
containing the raw audio or video frame to be encoded. It can benil
, 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 returnAVError.eof
. -
Read encoded data from the encoder.
Throws
AVError.tryAgain
if output is not available in the current state - user must try to send input.AVError.eof
if the encoder has been fully flushed, and there will be no more output packets.AVError.invalidArgument
if codec not opened, or it is an encoder.- legitimate decoding errors
Declaration
Swift
public func receivePacket(_ packet: AVPacket) throws
Parameters
packet
This will be set to a reference-counted packet allocated by the encoder.
-
Reset the internal decoder state / flush internal buffers. Should be called e.g. when seeking or when switching to a different stream.
Note
when refcounted frames are not used (i.e. avctx->refcounted_frames is 0), this invalidates the frames previously returned from the decoder. When refcounted frames are used, the decoder just releases any references it might keep internally, but the caller’s reference remains valid.Declaration
Swift
public func flush()
-
Declaration
Swift
public struct Flag : OptionSet
-
Declaration
Swift
public struct Flag2 : OptionSet
-
The width of the picture.
- encoding: Must be set by user.
- decoding: May be set by the user before opening the decoder if known e.g. from the container. Some decoders will require the dimensions to be set by the caller. During decoding, the decoder may overwrite those values as required while parsing the data.
Declaration
Swift
public var width: Int { get set }
-
The height of the picture.
- encoding: Must be set by user.
- decoding: May be set by the user before opening the decoder if known e.g. from the container. Some decoders will require the dimensions to be set by the caller. During decoding, the decoder may overwrite those values as required while parsing the data.
Declaration
Swift
public var height: Int { get set }
-
Bitstream width, may be different from
width
e.g. when the decoded frame is cropped before being output or lowres is enabled.- encoding: Unused.
- decoding: May be set by the user before opening the decoder if known e.g. from the container. During decoding, the decoder may overwrite those values as required while parsing the data.
Declaration
Swift
public var codedWidth: Int { get set }
-
Bitstream height, may be different from
height
e.g. when the decoded frame is cropped before being output or lowres is enabled.- encoding: Unused.
- decoding: May be set by the user before opening the decoder if known e.g. from the container. During decoding, the decoder may overwrite those values as required while parsing the data.
Declaration
Swift
public var codedHeight: Int { get set }
-
The number of pictures in a group of pictures, or 0 for intra_only.
- encoding: Set by user.
- decoding: Unused.
Declaration
Swift
public var gopSize: Int { get set }
-
The pixel format of the picture.
- encoding: Set by user.
- decoding: Set by user if known, overridden by codec while parsing the data.
Declaration
Swift
public var pixelFormat: AVPixelFormat { get set }
-
The callback used to negotiate the pixel format.
Note
The callback may be called again immediately if initialization for the selected (hardware-accelerated) pixel format failed.
Warning
Behavior is undefined if the callback returns a value not in the fmt list of formats.
encoding: Unused.
decoding: Set by user, if not set the native format will be chosen.
Declaration
Swift
public var getFormat: AVGetFormatHandler? { get set }
-
Maximum number of B-frames between non-B-frames.
Note
The output will be delayed by max_b_frames+1 relative to the input.
encoding: Set by user.
decoding: Unused.
Declaration
Swift
public var maxBFrames: Int { get set }
-
Macroblock decision mode.
- encoding: Set by user.
- decoding: Unused.
Declaration
Swift
public var mbDecision: Int { get set }
-
Sample aspect ratio (0/0 if unknown).
That is the width of a pixel divided by the height of the pixel. Numerator and denominator must be relatively prime and smaller than 256 for some video standards.
- encoding: Set by user.
- decoding: Set by codec.
Declaration
Swift
public var sampleAspectRatio: AVRational { get set }
-
low resolution decoding, 1->½ size, 2->¼ size
- encoding: Unused.
- decoding: Set by user.
Declaration
Swift
public var lowres: Int { get }
-
The framerate of the video.
- encoding: May be used to signal the framerate of CFR content to an encoder.
- decoding: For codecs that store a framerate value in the compressed bitstream, the decoder may export it here. 0/1 when unknown.
Declaration
Swift
public var framerate: AVRational { get set }
-
Samples per second.
Declaration
Swift
public var sampleRate: Int { get set }
-
Number of audio channels.
Declaration
Swift
public var channelCount: Int { get set }
-
Audio sample format.
- encoding: Set by user.
- decoding: Set by libavcodec.
Declaration
Swift
public var sampleFormat: AVSampleFormat { get set }
-
Number of samples per channel in an audio frame.
Declaration
Swift
public var frameSize: Int { get set }
-
Audio channel layout.
- encoding: Set by user.
- decoding: Set by user, may be overwritten by codec.
Declaration
Swift
public var channelLayout: AVChannelLayout { get set }
-
Undocumented
Declaration
Swift
public static let `class`: AVClass
-
Undocumented
Declaration
Swift
public func withUnsafeClassObjectPointer<T>(_ body: (UnsafeMutableRawPointer) throws -> T) rethrows -> T
-
Undocumented
Declaration
Swift
public func withUnsafeObjectPointer<T>(_ body: (UnsafeMutableRawPointer) throws -> T) rethrows -> T