AvcodecBindings to libavcodec. The interface is the one of spec/avcodec.md §4.
val version : Avutil.versionThe version of the libavcodec library loaded at run time. It is read once, when this module is loaded.
A codec FFmpeg registers: the static description of one encoder or decoder implementation, such as "libmp3lame" or "h264".
'media is the media kind (audio, video, subtitle or [`Data]) and 'mode is encode or decode. Both are phantom parameters, and every function that returns a codec gives the parameters that match it.
A codec holds no stream state. It is valid for the life of the process and is immutable, so any number of threads may use one. A decoder or an encoder is the working instance built from a codec, see Audio.create_decoder, Audio.create_encoder, Video.create_decoder and Video.create_encoder.
Equality between two codec values is not defined: compare their names.
The parameters of one encoded stream: its codec identifier and the properties of its content, such as the sample rate and the channel layout of an audio stream or the picture size of a video stream. Parameters say how a stream is encoded. They hold no codec state and decode nothing.
A value owns a private deep copy of the native parameters and shares nothing with the object it was read from. It is immutable and the garbage collector releases it. params and BitstreamFilter.init return parameters, and so do the libraries built on this one.
An opened decoder: the working state that turns the packets of one stream into frames, driven by decode and flush_decoder.
A decoder value owns its native codec context. The garbage collector releases it. There is no explicit close, and collection flushes nothing.
A decoder is in one of three states:
decode accepts packets;flush_decoder signalled the end of the stream and the decoder accepted it: decode raises Error `Eof;decode raises Error `Eof and flush_decoder returns at once.A drained decoder stays drained. Decoding again takes a new decoder.
Concurrent use. A decoder is memory-safe under any interleaving of calls from several threads or domains. Each native send or receive step takes the decoder exclusively. An operation that finds the decoder taken by another thread raises Error (`Failure "Object in use!") at once; it does not wait. After any error the decoder is valid and in one of the three states.
The interface builds audio and video decoders only: the types subtitle decoder and [`Data] decoder have no value.
An opened encoder: the working state that turns frames into the packets of one stream, driven by encode and flush_encoder.
Ownership, release, the three states and the behaviour under concurrent use are those of a decoder, with encode and flush_encoder in the place of decode and flush_decoder. A video encoder created with a hardware context holds a reference of its own to that context and keeps it alive.
The interface builds audio and video encoders only: the types subtitle encoder and [`Data] encoder have no value.
The 'mode parameter of a codec that encodes.
The 'mode parameter of a codec that decodes.
A profile of a codec, as FFmpeg lists it in the codec's descriptor. id is FFmpeg's number for the profile (an AV_PROFILE_* constant) and profile_name is its name, the empty string when FFmpeg gives none.
type descriptor = {media_type : Avutil.media_type;name : string;long_name : string option;properties : [ `Intra_only
| `Lossy
| `Lossless
| `Reorder
| `Fields
| `Enhancement
| `Bitmap_sub
| `Text_sub ]
list;mime_types : string list;profiles : profile list;}FFmpeg's description of a codec identifier. It describes the format, not one implementation of it: an encoder and a decoder of the same identifier share one descriptor.
media_type: the media kind of the format.name: the short name of the format, the empty string when FFmpeg gives none.long_name: a descriptive name, None when FFmpeg gives none.properties: the properties of the format (intra-only, lossy, lossless and so on), in ascending order of FFmpeg's bit values. A property the bindings have no constructor for is left out.mime_types: the MIME types associated with the format, in FFmpeg's order. Empty when FFmpeg lists none.profiles: the profiles FFmpeg lists for the format, in FFmpeg's order. Empty when it lists none.The integer value of FFmpeg's AV_CODEC_FLAG_QSCALE codec flag ("use fixed qscale"). It is read once, when this module is loaded.
The integer value of FFmpeg's AV_CODEC_FLAG_GLOBAL_HEADER codec flag ("place global headers in extradata"). It is read once, when this module is loaded.
params encoder is a fresh, independent snapshot of the encoder's parameters, as FFmpeg derives them from the opened codec context. It is what a container needs to declare the stream the encoder produces.
The call only reads the encoder.
val descriptor : 'media params -> descriptor optiondescriptor params is the descriptor of the codec identifier the parameters carry, None when FFmpeg has none for it. Every text of the result is a copy.
val time_base : 'media encoder -> Avutil.rationaltime_base encoder is the encoder's time base: the unit, in seconds, of the timestamps of the packets it produces.
val name : (_, _) codec -> stringname codec is the codec's name, the one Audio.find_encoder_by_name and the other lookups by name accept. It is the empty string when FFmpeg gives none.
type capability = [ | `Draw_horiz_band| `Dr1| `Delay| `Small_last_frame| `Experimental| `Channel_conf| `Frame_threads| `Slice_threads| `Param_change| `Other_threads| `Variable_frame_size| `Avoid_probing| `Hardware| `Hybrid| `Encoder_reordered_opaque| `Encoder_flush| `Encoder_recon_frame ]A capability of a codec implementation, FFmpeg's AV_CODEC_CAP_* flags.
val capabilities : ([< `Audio | `Video ], _) codec -> capability listcapabilities codec is the list of the capabilities the codec declares, in ascending order of FFmpeg's bit values. A capability the bindings have no constructor for is left out. It accepts encoders and decoders.
A way to set up hardware acceleration for a codec, FFmpeg's AV_CODEC_HW_CONFIG_METHOD_* flags.
type hw_config = {pixel_format : Avutil.Pixel_format.t;methods : hw_config_method list;device_type : Avutil.HwContext.device_type;}One hardware configuration a codec supports.
pixel_format: for a decoder, a hardware pixel format it may decode to when suitable hardware is available. For an encoder, a pixel format it may accept; `None then stands for every pixel format the codec supports.methods: the setup methods usable with this configuration, in ascending order of FFmpeg's bit values.device_type: the type of hardware device of the configuration. FFmpeg sets it for the `Hw_device_ctx and `Hw_frames_ctx methods; it is meaningless for the others.hw_configs codec is the list of the hardware configurations the codec declares, in FFmpeg's order. It is empty for a codec with none. A configuration whose pixel format or device type the bindings have no constructor for is left out.
The list tells which Video.hardware_context a video encoder can be given: a device context of type device_type when methods has `Hw_device_ctx, a frame context when it has `Hw_frames_ctx.
module Packet_side_data : sig ... endSide data of packets and of codec parameters: the kinds FFmpeg defines, raw entries and their typed content. A packet's side data applies to that packet; the side data of codec parameters applies to the whole stream.
val params_side_data : _ params -> Packet_side_data.raw listThe side data of the parameters, which applies to the whole stream, in order. An entry whose kind the bindings have no constructor for is left out.
val params_extradata : _ params -> stringThe codec's extradata (for instance an AudioSpecificConfig or an H.264 avcC record, or Annex-B parameter sets), byte for byte. The empty string when the parameters have none.
val params_profile : _ params -> int optionThe codec profile, an AV_PROFILE_* value; None when unknown.
val params_level : _ params -> int optionThe codec level; None when unknown.
val params_block_align : _ params -> intThe block alignment, in bytes; 0 when unset.
val params_bits_per_coded_sample : _ params -> intThe number of bits per coded sample; 0 when unset.
val params_with_side_data :
'media params ->
Packet_side_data.raw list ->
'media paramsparams_with_side_data params l is an independent copy of params whose side data is exactly l, in order. A kind that appears twice keeps its last occurrence. params is unchanged.
module Packet : sig ... endPackets: one unit of encoded data of a stream, with its timestamps, its flags and its side data. An encoder produces packets and a decoder consumes them; containers read and write them.
module Audio : sig ... endAudio codecs: lookup, supported configurations, decoders, encoders and the audio fields of codec parameters.
module Video : sig ... endVideo codecs: lookup, supported configurations, decoders, encoders and the video fields of codec parameters.
module Subtitle : sig ... endSubtitle codecs: lookup only. This module builds no subtitle decoder or encoder. Subtitles are decoded and encoded through containers, in the Av library.
module Unknown : sig ... endThe formats FFmpeg places in none of the audio, video and subtitle families. This module offers their identifiers only: no codec list, no lookup, no decoder and no encoder.
type id = [ | `None| `Mpeg1video| `Mpeg2video| `H261| `H263| `Rv10| `Rv20| `Mjpeg| `Mjpegb| `Ljpeg| `Sp5x| `Jpegls| `Mpeg4| `Rawvideo| `Msmpeg4v1| `Msmpeg4v2| `Msmpeg4v3| `Wmv1| `Wmv2| `H263p| `H263i| `Flv1| `Svq1| `Svq3| `Dvvideo| `Huffyuv| `Cyuv| `H264| `Indeo3| `Vp3| `Theora| `Asv1| `Asv2| `Ffv1| `_4xm| `Vcr1| `Cljr| `Mdec| `Roq| `Interplay_video| `Xan_wc3| `Xan_wc4| `Rpza| `Cinepak| `Ws_vqa| `Msrle| `Msvideo1| `Idcin| `_8bps| `Smc| `Flic| `Truemotion1| `Vmdvideo| `Mszh| `Zlib| `Qtrle| `Tscc| `Ulti| `Qdraw| `Vixl| `Qpeg| `Png| `Ppm| `Pbm| `Pgm| `Pgmyuv| `Pam| `Ffvhuff| `Rv30| `Rv40| `Vc1| `Wmv3| `Loco| `Wnv1| `Aasc| `Indeo2| `Fraps| `Truemotion2| `Bmp| `Cscd| `Mmvideo| `Zmbv| `Avs| `Smackvideo| `Nuv| `Kmvc| `Flashsv| `Cavs| `Jpeg2000| `Vmnc| `Vp5| `Vp6| `Vp6f| `Targa| `Dsicinvideo| `Tiertexseqvideo| `Tiff| `Gif| `Dxa| `Dnxhd| `Thp| `Sgi| `C93| `Bethsoftvid| `Ptx| `Txd| `Vp6a| `Amv| `Vb| `Pcx| `Sunrast| `Indeo4| `Indeo5| `Mimic| `Rl2| `Escape124| `Dirac| `Bfi| `Cmv| `Motionpixels| `Tgv| `Tgq| `Tqi| `Aura| `Aura2| `V210x| `Tmv| `V210| `Dpx| `Mad| `Frwu| `Flashsv2| `Cdgraphics| `R210| `Anm| `Binkvideo| `Iff_ilbm| `Kgv1| `Yop| `Vp8| `Pictor| `Ansi| `A64_multi| `A64_multi5| `R10k| `Mxpeg| `Lagarith| `Prores| `Jv| `Dfa| `Wmv3image| `Vc1image| `Utvideo| `Bmv_video| `Vble| `Dxtory| `Xwd| `Cdxl| `Xbm| `Zerocodec| `Mss1| `Msa1| `Tscc2| `Mts2| `Cllc| `Mss2| `Vp9| `Aic| `Escape130| `G2m| `Webp| `Hnm4_video| `Hevc| `Fic| `Alias_pix| `Brender_pix| `Paf_video| `Exr| `Vp7| `Sanm| `Sgirle| `Mvc1| `Mvc2| `Hqx| `Tdsc| `Hq_hqa| `Hap| `Dds| `Dxv| `Screenpresso| `Rscc| `Avs2| `Pgx| `Avs3| `Msp2| `Vvc| `Y41p| `Avrp| `_012v| `Avui| `Targa_y216| `Yuv4| `Avrn| `Cpia| `Xface| `Snow| `Smvjpeg| `Apng| `Daala| `Cfhd| `Truemotion2rt| `M101| `Magicyuv| `Sheervideo| `Ylc| `Psd| `Pixlet| `Speedhq| `Fmvc| `Scpr| `Clearvideo| `Xpm| `Av1| `Bitpacked| `Mscc| `Srgc| `Svg| `Gdv| `Fits| `Imm4| `Prosumer| `Mwsc| `Wcmv| `Rasc| `Hymt| `Arbc| `Agm| `Lscr| `Vp4| `Imm5| `Mvdv| `Mvha| `Cdtoons| `Mv30| `Notchlc| `Pfm| `Mobiclip| `Photocd| `Ipu| `Argo| `Cri| `Simbiosis_imx| `Sga_video| `Gem| `Vbn| `Jpegxl| `Qoi| `Phm| `Radiance_hdr| `Wbmp| `Media100| `Vqc| `Pdv| `Evc| `Rtv1| `Vmix| `Lead| `Dnxuc| `Rv60| `Jpegxl_anim| `Apv| `Prores_raw| `Jpegxs| `Webp_anim| `Pcm_s16le| `Pcm_s16be| `Pcm_u16le| `Pcm_u16be| `Pcm_s8| `Pcm_u8| `Pcm_mulaw| `Pcm_alaw| `Pcm_s32le| `Pcm_s32be| `Pcm_u32le| `Pcm_u32be| `Pcm_s24le| `Pcm_s24be| `Pcm_u24le| `Pcm_u24be| `Pcm_s24daud| `Pcm_zork| `Pcm_s16le_planar| `Pcm_dvd| `Pcm_f32be| `Pcm_f32le| `Pcm_f64be| `Pcm_f64le| `Pcm_bluray| `Pcm_lxf| `S302m| `Pcm_s8_planar| `Pcm_s24le_planar| `Pcm_s32le_planar| `Pcm_s16be_planar| `Pcm_s64le| `Pcm_s64be| `Pcm_f16le| `Pcm_f24le| `Pcm_vidc| `Pcm_sga| `Adpcm_ima_qt| `Adpcm_ima_wav| `Adpcm_ima_dk3| `Adpcm_ima_dk4| `Adpcm_ima_ws| `Adpcm_ima_smjpeg| `Adpcm_ms| `Adpcm_4xm| `Adpcm_xa| `Adpcm_adx| `Adpcm_ea| `Adpcm_g726| `Adpcm_ct| `Adpcm_swf| `Adpcm_yamaha| `Adpcm_sbpro_4| `Adpcm_sbpro_3| `Adpcm_sbpro_2| `Adpcm_thp| `Adpcm_ima_amv| `Adpcm_ea_r1| `Adpcm_ea_r3| `Adpcm_ea_r2| `Adpcm_ima_ea_sead| `Adpcm_ima_ea_eacs| `Adpcm_ea_xas| `Adpcm_ea_maxis_xa| `Adpcm_ima_iss| `Adpcm_g722| `Adpcm_ima_apc| `Adpcm_vima| `Adpcm_afc| `Adpcm_ima_oki| `Adpcm_dtk| `Adpcm_ima_rad| `Adpcm_g726le| `Adpcm_thp_le| `Adpcm_psx| `Adpcm_aica| `Adpcm_ima_dat4| `Adpcm_mtaf| `Adpcm_agm| `Adpcm_argo| `Adpcm_ima_ssi| `Adpcm_zork| `Adpcm_ima_apm| `Adpcm_ima_alp| `Adpcm_ima_mtf| `Adpcm_ima_cunning| `Adpcm_ima_moflex| `Adpcm_ima_acorn| `Adpcm_xmd| `Adpcm_ima_xbox| `Adpcm_sanyo| `Adpcm_ima_hvqm4| `Adpcm_ima_pda| `Adpcm_n64| `Adpcm_ima_hvqm2| `Adpcm_ima_magix| `Adpcm_psxc| `Adpcm_circus| `Adpcm_ima_escape| `Amr_wb| `Ra_144| `Ra_288| `Roq_dpcm| `Interplay_dpcm| `Xan_dpcm| `Sol_dpcm| `Sdx2_dpcm| `Gremlin_dpcm| `Derf_dpcm| `Wady_dpcm| `Cbd2_dpcm| `Mp2| `Mp3| `Aac| `Ac3| `Dts| `Vorbis| `Dvaudio| `Wmav1| `Wmav2| `Mace3| `Mace6| `Vmdaudio| `Flac| `Mp3adu| `Mp3on4| `Shorten| `Alac| `Westwood_snd1| `Gsm| `Qdm2| `Cook| `Truespeech| `Tta| `Smackaudio| `Qcelp| `Wavpack| `Dsicinaudio| `Imc| `Musepack7| `Mlp| `Gsm_ms| `Atrac3| `Ape| `Nellymoser| `Musepack8| `Speex| `Wmavoice| `Wmapro| `Wmalossless| `Atrac3p| `Eac3| `Sipr| `Mp1| `Twinvq| `Truehd| `Mp4als| `Atrac1| `Binkaudio_rdft| `Binkaudio_dct| `Aac_latm| `Qdmc| `Celt| `G723_1| `G729| `_8svx_exp| `_8svx_fib| `Bmv_audio| `Ralf| `Iac| `Ilbc| `Opus| `Comfort_noise| `Tak| `Metasound| `Paf_audio| `On2avc| `Dss_sp| `Codec2| `Ffwavesynth| `Sonic| `Sonic_ls| `Evrc| `Smv| `Dsd_lsbf| `Dsd_msbf| `Dsd_lsbf_planar| `Dsd_msbf_planar| `_4gv| `Interplay_acm| `Xma1| `Xma2| `Dst| `Atrac3al| `Atrac3pal| `Dolby_e| `Aptx| `Aptx_hd| `Sbc| `Atrac9| `Hcom| `Acelp_kelvin| `Mpegh_3d_audio| `Siren| `Hca| `Fastaudio| `Msnsiren| `Dfpwm| `Bonk| `Misc4| `Apac| `Ftr| `Wavarc| `Rka| `Ac4| `Osq| `Qoa| `Lc3| `G728| `Ahx| `Apple_apac| `Dvd_subtitle| `Dvb_subtitle| `Text| `Xsub| `Ssa| `Mov_text| `Hdmv_pgs_subtitle| `Dvb_teletext| `Srt| `Microdvd| `Eia_608| `Jacosub| `Sami| `Realtext| `Stl| `Subviewer1| `Subviewer| `Subrip| `Webvtt| `Mpl2| `Vplayer| `Pjs| `Ass| `Hdmv_text_subtitle| `Ttml| `Arib_caption| `Ivtv_vbi| `Ttf| `Scte_35| `Epg| `Bintext| `Xbin| `Idf| `Otf| `Smpte_klv| `Timed_id3| `Bin_data| `Smpte_2038| `Lcevc| `Smpte_436m_anc| `Probe| `Mpeg2ts| `Mpeg4systems| `Ffmetadata| `Wrapped_avframe| `Vnull| `Anull ]A codec identifier of any family: every member of FFmpeg's AVCodecID enumeration the bindings know.
val string_of_id : id -> stringstring_of_id id is FFmpeg's name of the format.
module BitstreamFilter : sig ... endBitstream filters: transformations applied to the packets of a stream, from packets to packets.
val decode :
'media decoder ->
('media Avutil.frame -> unit) ->
'media Packet.t ->
unitdecode decoder f packet gives packet to the decoder and calls f on every frame that becomes available, in order, each as soon as it is received. A packet may produce no frame, or several; producing none is not an error.
The steps are:
The decoder references the payload of packet, which is unchanged and stays usable. Each frame passed to f is a new value that owns what the decoder produced. f may keep it.
Each send and each receive releases the OCaml runtime lock: other OCaml threads run during the decoding work. f runs on the calling thread with the lock held, between two native steps. The decoder is free while f runs: f may call any operation on it. The call as a whole is not atomic with respect to other threads.
An exception raised by f propagates unchanged. The frames not yet delivered stay in the decoder and the next decode or flush_decoder delivers them first. When f raises in the first step the packet was not sent; in the third step the decoder accepted it.
val flush_decoder : 'media decoder -> ('media Avutil.frame -> unit) -> unitflush_decoder decoder f signals the end of the stream to the decoder and calls f on every frame it still holds, in order. It is the call that ends a stream: collecting a decoder delivers nothing.
What it does depends on the state of the decoder (see decoder):
f interrupted by an exception: it delivers the remaining frames.f on nothing.The lock, the thread f runs on, the exceptions of f and the ownership of the frames are as in decode.
val encode :
'media encoder ->
('media Packet.t -> unit) ->
'media Avutil.frame ->
unitencode encoder f frame gives frame to the encoder and calls f on every packet that becomes available, in order, each as soon as it is received. A frame may produce no packet, or several; producing none is not an error.
The steps are those of decode: deliver the packets the encoder has ready, send the frame, deliver again.
The encoder references the buffers of frame, which is unchanged and stays usable. An encoder created with a `Frame_context hardware context instead uploads the frame: it takes a frame from the pool of the context, transfers the pixel data to it, copies the properties of frame onto it, timestamps included, and encodes that frame. The pool frame is released after the send.
Each packet passed to f is a new value that owns what the encoder produced. Its timestamps are in the encoder's time base, see time_base.
Each send, upload included, and each receive releases the OCaml runtime lock. f runs on the calling thread with the lock held, between two native steps, with the encoder free: f may call any operation on it. An exception raised by f propagates unchanged, and the packets not yet delivered stay in the encoder: the next encode or flush_encoder delivers them first.
flush_encoder encoder f signals the end of the stream to the encoder and calls f on every packet it still holds, in order. It is the call that ends a stream: collecting an encoder flushes nothing.
The three states are handled as in flush_decoder: an open encoder is drained, a draining one delivers what remains, a drained one returns at once. A drained encoder accepts no more frame: encoding again takes a new encoder.
The lock, the thread f runs on, the exceptions of f and the ownership of the packets are as in encode.