Module Avcodec.Packet

Packets: 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.

Concurrent use. A packet has no guard. Any number of threads may read one at the same time, including giving it to decoders, bitstream filters and containers. The setters and Packet.add_side_data change the packet: the thread that calls one must be the only user of the packet at that moment, and the bindings do not detect a violation.

type 'media t

A packet. The value owns the packet structure: its timestamps, flags, stream index and side data. The payload is a reference-counted buffer that other packets and FFmpeg may also reference; OCaml never changes it.

The garbage collector releases a packet, and is told the size of its payload. No operation of the bindings empties or consumes a packet passed to it: the packet stays usable afterwards.

'media is a phantom parameter. No operation relies on it for memory safety: a packet is opaque bytes to every consumer.

type flag = [
  1. | `Keyframe
  2. | `Corrupt
  3. | `Discard
  4. | `Trusted
  5. | `Disposable
]

Packet flags:

  • `Keyframe: the packet contains a keyframe;
  • `Corrupt: the packet content is corrupted;
  • `Discard: the packet is needed to keep the decoder state valid, and its decoded output is to be dropped;
  • `Trusted: the packet comes from a trusted source;
  • `Disposable: the packet holds frames the decoder may discard, that is non-reference frames.
type replaygain = Packet_side_data.replaygain = {
  1. track_gain : int;
  2. track_peak : int;
  3. album_gain : int;
  4. album_peak : int;
}

ReplayGain information, FFmpeg's AVReplayGain. track_gain and album_gain are in microbels: divide by 100000 to get decibels. FFmpeg's "unknown" gain is INT32_MIN, -2147483648. track_peak and album_peak are peak amplitudes where 100000 is full scale, 0 when unknown.

type side_data = Packet_side_data.t

The side data the bindings translate.

val raw_side_data : 'media t -> Packet_side_data.raw list

Every side-data entry of the packet, in the packet's order. An entry whose kind the bindings have no constructor for is left out.

val add_raw_side_data : 'media t -> Packet_side_data.raw -> unit

add_raw_side_data packet entry attaches a copy of entry. A packet holds one entry per kind: an entry of the same kind is replaced. On failure the packet is unchanged.

val remove_side_data : 'media t -> Packet_side_data.kind -> unit

remove_side_data packet kind removes the entry of that kind.

val add_side_data : 'media t -> side_data -> unit

add_side_data packet data attaches a copy of data to the packet.

A packet holds one entry per kind of side data: adding a kind the packet already carries replaces that entry. On failure the packet is unchanged.

For `Replaygain, each gain must fit a signed 32-bit integer and each peak an unsigned 32-bit integer. For the two metadata kinds, each key and value ends at its first NUL byte, and a later pair replaces an earlier pair of the same key.

  • raises Avutil.Error

    with `Failure _ when a gain does not fit a signed 32-bit integer or a peak an unsigned one.

val side_data : 'media t -> side_data list

side_data packet is the packet's side data of the kinds of side_data, in the packet's order: the entries of raw_side_data that Packet_side_data.decode translates. The pairs of a metadata entry are in the order of the entry.

On a packet with no side data, side_data p after add_side_data p d is [d].

val dup : 'media t -> 'media t

dup packet is a new packet with a copy of the properties and of the side data of packet. Both packets reference the same payload: no payload byte is copied. Changing one packet through its setters leaves the other unchanged.

  • raises Avutil.Error

    with the mapped FFmpeg code when FFmpeg fails.

val get_flags : 'media t -> flag list

get_flags packet is the list of the flags set on the packet, in the order of flag.

val set_flags : 'media t -> flag list -> unit

set_flags packet flags replaces the packet's flags with flags. The empty list clears them all, and every bit FFmpeg defines outside flag is cleared too.

val get_size : 'media t -> int

get_size packet is the size of the payload, in bytes.

val get_stream_index : 'media t -> int

get_stream_index packet is the index of the stream the packet belongs to in its container.

val set_stream_index : 'media t -> int -> unit

set_stream_index packet index sets the packet's stream index.

  • raises Avutil.Error

    with `Failure _ when index does not fit a C int.

val get_pts : 'media t -> Stdlib.Int64.t option

get_pts packet is the presentation timestamp, in units of the time base of the packet's producer (the encoder's time base for an encoded packet). None when the packet has none. No time-base conversion happens here.

val set_pts : 'media t -> Stdlib.Int64.t option -> unit

set_pts packet pts sets the presentation timestamp. None removes it.

val get_dts : 'media t -> Stdlib.Int64.t option

get_dts packet is the decoding timestamp, in the same unit as the presentation timestamp. None when the packet has none.

val set_dts : 'media t -> Stdlib.Int64.t option -> unit

set_dts packet dts sets the decoding timestamp. None removes it.

val get_duration : 'media t -> Stdlib.Int64.t option

get_duration packet is the duration of the packet, in the same unit as its timestamps. None when it is unknown, which FFmpeg stores as 0.

val set_duration : 'media t -> Stdlib.Int64.t option -> unit

set_duration packet duration sets the duration. None marks it unknown. Some 0L is FFmpeg's "unknown" too and reads back as None.

val get_position : 'media t -> Stdlib.Int64.t option

get_position packet is the byte position of the packet in its stream. None when it is unknown, which FFmpeg stores as -1.

val set_position : 'media t -> Stdlib.Int64.t option -> unit

set_position packet position sets the byte position. None marks it unknown. Some (-1L) is FFmpeg's "unknown" too and reads back as None.

val to_bytes : 'media t -> bytes

to_bytes packet is a fresh copy of the payload.

val content : 'media t -> string

content packet is a fresh copy of the payload.

val create : string -> 'media t

create content is a new packet whose payload is a copy of content. It has no timestamp, no duration, no position, stream index 0, no flag and no side data. The caller chooses 'media.

  • raises Avutil.Error

    with `Failure _ when content is longer than a C int can count.