Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.rst
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ v18.1.0 (Unreleased)

Features:

- Add ``Packet.rescale_ts()`` to rescale packet PTS, DTS, and duration to a new ``AVRational`` time base by :gh-user:`WyattBlue`.
- Support reusing the thread's current CUDA context via a ``current_ctx`` flag on ``CudaContext`` and ``VideoFrame.from_dlpack``, for interop with libraries like PyTorch that initialize CUDA first by :gh-user:`Yozer` (:pr:`2339`).
- ``VideoFrame.from_dlpack`` no longer requires restating ``primary_ctx``/``current_ctx`` when passing an explicit ``cuda_context``; the flags are only validated when explicitly given by :gh-user:`WyattBlue`.

Expand Down
12 changes: 6 additions & 6 deletions av/_core.pxd
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
cdef extern from "libswscale/swscale.h" nogil:
cdef int swscale_version()
cdef char* swscale_configuration()
cdef char* swscale_license()
cdef unsigned int swscale_version()
cdef const char* swscale_configuration()
cdef const char* swscale_license()

cdef extern from "libswresample/swresample.h" nogil:
cdef int swresample_version()
cdef char* swresample_configuration()
cdef char* swresample_license()
cdef unsigned int swresample_version()
cdef const char* swresample_configuration()
cdef const char* swresample_license()
21 changes: 21 additions & 0 deletions av/packet.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
from cython.cimports.libc.stdint import uint8_t
from cython.cimports.libc.string import memcpy

from av.rational import AVRational

# Check https://github.com/FFmpeg/FFmpeg/blob/master/libavcodec/packet.h#L41
# for new additions in the future ffmpeg releases
# Note: the order must follow that of the AVPacketSideDataType enum def
Expand Down Expand Up @@ -289,6 +291,25 @@ def _rebase_time(self, dst: lib.AVRational):
lib.av_packet_rescale_ts(self.ptr, self.ptr.time_base, dst)
self.ptr.time_base = dst

def rescale_ts(self, time_base):
"""Rescale the packet timestamps to a new time base.

This rescales :attr:`pts`, :attr:`dts`, and :attr:`duration`, then updates
:attr:`time_base`. If the current time base is unset, the timestamp values
are unchanged and the new time base is assigned.

:meth:`~av.container.OutputContainer.mux` already performs this operation
automatically when necessary.

Wraps :ffmpeg:`av_packet_rescale_ts`.
"""
if not isinstance(time_base, AVRational):
raise TypeError("time_base must be an AVRational")

dst: lib.AVRational
to_avrational(time_base, cython.address(dst))
self._rebase_time(dst)

def decode(self):
"""
Send the packet's data to the decoder and return a list of
Expand Down
2 changes: 2 additions & 0 deletions av/packet.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ from typing import Generic, Literal, TypeVar, overload

from av.audio.frame import AudioFrame
from av.audio.stream import AudioStream
from av.rational import AVRational
from av.stream import Stream
from av.subtitles.stream import SubtitleStream
from av.subtitles.subtitle import AssSubtitle, BitmapSubtitle
Expand Down Expand Up @@ -92,6 +93,7 @@ class Packet(Buffer, Generic[StreamT]):
is_disposable: bool

def __init__(self: Packet[Stream], input: int | bytes | None = None) -> None: ...
def rescale_ts(self, time_base: AVRational) -> None: ...

# Overloads that return the same type as the stream's decode method
@overload
Expand Down
2 changes: 1 addition & 1 deletion av/video/codeccontext.py
Original file line number Diff line number Diff line change
Expand Up @@ -445,7 +445,7 @@ def field_order(self):

@field_order.setter
def field_order(self, value: cython.int):
self.ptr.field_order = value
self.ptr.field_order = cython.cast(lib.AVFieldOrder, value)

@property
def max_b_frames(self):
Expand Down
78 changes: 40 additions & 38 deletions include/avcodec.pxd
Original file line number Diff line number Diff line change
Expand Up @@ -9,19 +9,19 @@ cdef extern from "libavutil/channel_layout.h" nogil:
ctypedef struct AVChannelLayout:
int nb_channels

int av_channel_layout_default(AVChannelLayout *ch_layout, int nb_channels)
void av_channel_layout_default(AVChannelLayout *ch_layout, int nb_channels)
int av_channel_layout_from_string(AVChannelLayout *channel_layout, const char *str)
int av_channel_layout_describe(const AVChannelLayout *channel_layout, char *buf, size_t buf_size)
int av_channel_name(char *buf, size_t buf_size, AVChannel channel_id)
int av_channel_description(char *buf, size_t buf_size, AVChannel channel_id)
int av_channel_layout_compare(AVChannelLayout *chl, AVChannelLayout *chl1)
AVChannel av_channel_layout_channel_from_index(AVChannelLayout *channel_layout, unsigned int idx)
int av_channel_layout_compare(const AVChannelLayout *chl, const AVChannelLayout *chl1)
AVChannel av_channel_layout_channel_from_index(const AVChannelLayout *channel_layout, unsigned int idx)
void av_channel_layout_uninit(AVChannelLayout *channel_layout)

cdef extern from "libavcodec/avcodec.h" nogil:
cdef int avcodec_version()
cdef char* avcodec_configuration()
cdef char* avcodec_license()
cdef unsigned int avcodec_version()
cdef const char* avcodec_configuration()
cdef const char* avcodec_license()

AVPixelFormat avcodec_find_best_pix_fmt_of_list(
const AVPixelFormat *pix_fmt_list,
Expand Down Expand Up @@ -71,7 +71,6 @@ cdef extern from "libavcodec/avcodec.h" nogil:
AV_CODEC_FLAG_4MV
AV_CODEC_FLAG_OUTPUT_CORRUPT
AV_CODEC_FLAG_QPEL
AV_CODEC_FLAG_DROPCHANGED
AV_CODEC_FLAG_RECON_FRAME
AV_CODEC_FLAG_COPY_OPAQUE
AV_CODEC_FLAG_FRAME_DURATION
Expand Down Expand Up @@ -168,15 +167,15 @@ cdef extern from "libavcodec/avcodec.h" nogil:
AVDISCARD_ALL

cdef struct AVCodec:
char *name
char *long_name
const char *name
const char *long_name
AVMediaType type
AVCodecID id
int capabilities
AVClass *priv_class
const AVClass *priv_class

cdef int av_codec_is_encoder(AVCodec*)
cdef int av_codec_is_decoder(AVCodec*)
cdef int av_codec_is_encoder(const AVCodec*)
cdef int av_codec_is_decoder(const AVCodec*)

cdef enum AVCodecConfig:
AV_CODEC_CONFIG_PIX_FORMAT
Expand All @@ -195,18 +194,18 @@ cdef extern from "libavcodec/avcodec.h" nogil:

cdef struct AVProfile:
int profile
char *name
const char *name

cdef struct AVCodecDescriptor:
AVCodecID id
AVMediaType type
char *name
char *long_name
const char *name
const char *long_name
int props
char **mime_types
AVProfile *profiles
const char *const *mime_types
const AVProfile *profiles

AVCodecDescriptor* avcodec_descriptor_get(AVCodecID)
const AVCodecDescriptor* avcodec_descriptor_get(AVCodecID)

cdef enum:
AV_CODEC_HW_CONFIG_METHOD_HW_DEVICE_CTX
Expand All @@ -222,11 +221,14 @@ cdef extern from "libavcodec/avcodec.h" nogil:
cdef struct AVHWAccel:
pass

cdef enum AVFieldOrder:
pass

cdef struct AVCodecContext:
AVClass *av_class
const AVClass *av_class

AVMediaType codec_type
AVCodec *codec
const AVCodec *codec
AVCodecID codec_id
unsigned int codec_tag

Expand All @@ -252,7 +254,7 @@ cdef extern from "libavcodec/avcodec.h" nogil:
AVColorTransferCharacteristic color_trc
AVColorSpace colorspace
AVColorRange color_range
int field_order
AVFieldOrder field_order

int has_b_frames
AVPixelFormat (*get_format)(AVCodecContext *s, const AVPixelFormat *fmt)
Expand All @@ -273,7 +275,7 @@ cdef extern from "libavcodec/avcodec.h" nogil:
int64_t rc_max_rate
int64_t rc_min_rate

AVHWAccel *hwaccel
const AVHWAccel *hwaccel
AVBufferRef *hw_device_ctx
AVBufferRef *hw_frames_ctx

Expand All @@ -290,15 +292,15 @@ cdef extern from "libavcodec/avcodec.h" nogil:

cdef AVCodecContext* avcodec_alloc_context3(const AVCodec *codec)
cdef void avcodec_free_context(AVCodecContext **ctx)
cdef AVClass* avcodec_get_class()
cdef const AVClass* avcodec_get_class()
cdef const AVCodec* avcodec_find_decoder(AVCodecID id)
cdef const AVCodec* avcodec_find_encoder(AVCodecID id)
cdef const AVCodec* avcodec_find_decoder_by_name(char *name)
cdef const AVCodec* avcodec_find_encoder_by_name(char *name)
cdef const AVCodec* avcodec_find_decoder_by_name(const char *name)
cdef const AVCodec* avcodec_find_encoder_by_name(const char *name)
cdef const AVCodec* av_codec_iterate(void **opaque)
cdef const AVCodecDescriptor* avcodec_descriptor_get(AVCodecID id)
cdef const AVCodecDescriptor* avcodec_descriptor_get_by_name(char *name)
cdef char* avcodec_get_name(AVCodecID id)
cdef const AVCodecDescriptor* avcodec_descriptor_get_by_name(const char *name)
cdef const char* avcodec_get_name(AVCodecID id)
cdef int avcodec_open2(AVCodecContext *ctx, const AVCodec *codec, AVDictionary **options)
cdef enum AVPacketSideDataType:
AV_PKT_DATA_NEW_EXTRADATA
Expand Down Expand Up @@ -346,8 +348,8 @@ cdef extern from "libavcodec/avcodec.h" nogil:

# See: http://ffmpeg.org/doxygen/trunk/structAVFrame.html
cdef struct AVFrame:
uint8_t *data[4]
int linesize[4]
uint8_t *data[8]
int linesize[8]
uint8_t **extended_data
int width
int height
Expand Down Expand Up @@ -378,7 +380,7 @@ cdef extern from "libavcodec/avcodec.h" nogil:
int64_t duration

cdef struct AVPacket:
void *buf
AVBufferRef *buf
int64_t pts
int64_t dts
uint8_t *data
Expand All @@ -397,7 +399,7 @@ cdef extern from "libavcodec/avcodec.h" nogil:
AVFrame *frame,
int nb_channels,
AVSampleFormat sample_fmt,
uint8_t *buf,
const uint8_t *buf,
int buf_size,
int align
)
Expand Down Expand Up @@ -437,20 +439,20 @@ cdef extern from "libavcodec/avcodec.h" nogil:
int64_t pts

cdef int avcodec_decode_subtitle2(
AVCodecContext *ctx, AVSubtitle *sub, int *done, AVPacket *pkt,
AVCodecContext *ctx, AVSubtitle *sub, int *done, const AVPacket *pkt,
)
cdef int avcodec_encode_subtitle(
AVCodecContext *avctx, uint8_t *buf, int buf_size, AVSubtitle *sub
AVCodecContext *avctx, uint8_t *buf, int buf_size, const AVSubtitle *sub
)
cdef void avsubtitle_free(AVSubtitle*)
cdef void avcodec_flush_buffers(AVCodecContext *ctx)
cdef int avcodec_send_packet(AVCodecContext *avctx, AVPacket *packet)
cdef int avcodec_send_packet(AVCodecContext *avctx, const AVPacket *packet)
cdef int avcodec_receive_frame(AVCodecContext *avctx, AVFrame *frame)
cdef int avcodec_send_frame(AVCodecContext *avctx, AVFrame *frame)
cdef int avcodec_send_frame(AVCodecContext *avctx, const AVFrame *frame)
cdef int avcodec_receive_packet(AVCodecContext *avctx, AVPacket *avpkt)

cdef struct AVCodecParser:
int codec_ids[5]
int codec_ids[7]

cdef struct AVCodecParserContext:
int64_t pts
Expand Down Expand Up @@ -497,7 +499,7 @@ cdef extern from "libavcodec/avcodec.h" nogil:
cdef extern from "libavcodec/bsf.h" nogil:
cdef struct AVBitStreamFilter:
const char *name
AVCodecID *codec_ids
const AVCodecID *codec_ids

cdef struct AVCodecParameters:
pass
Expand All @@ -510,7 +512,7 @@ cdef extern from "libavcodec/bsf.h" nogil:
cdef int av_bsf_list_parse_str(const char *str, AVBSFContext **bsf)
cdef int av_bsf_init(AVBSFContext *ctx)
cdef void av_bsf_free(AVBSFContext **ctx)
cdef AVBitStreamFilter* av_bsf_iterate(void **opaque)
cdef const AVBitStreamFilter* av_bsf_iterate(void **opaque)
cdef int av_bsf_send_packet(AVBSFContext *ctx, AVPacket *pkt)
cdef int av_bsf_receive_packet(AVBSFContext *ctx, AVPacket *pkt)
cdef void av_bsf_flush(AVBSFContext *ctx)
Expand Down
6 changes: 3 additions & 3 deletions include/avdevice.pxd
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
cdef extern from "libavdevice/avdevice.h" nogil:
cdef int avdevice_version()
cdef char* avdevice_configuration()
cdef char* avdevice_license()
cdef unsigned int avdevice_version()
cdef const char* avdevice_configuration()
cdef const char* avdevice_license()
void avdevice_register_all()

cdef struct AVDeviceInfo:
Expand Down
22 changes: 11 additions & 11 deletions include/avfilter.pxd
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
cdef extern from "libavfilter/avfilter.h" nogil:
cdef int avfilter_version()
cdef char* avfilter_configuration()
cdef char* avfilter_license()
cdef unsigned int avfilter_version()
cdef const char* avfilter_configuration()
cdef const char* avfilter_license()

cdef struct AVFilterPad:
pass
Expand All @@ -19,12 +19,12 @@ cdef extern from "libavfilter/avfilter.h" nogil:
const AVClass *priv_class
int flags

cdef AVFilter* avfilter_get_by_name(const char *name)
cdef const AVFilter* avfilter_get_by_name(const char *name)
cdef const AVFilter* av_filter_iterate(void **opaque)

cdef struct AVFilterContext:
AVClass *av_class
AVFilter *filter
const AVClass *av_class
const AVFilter *filter

char *name

Expand All @@ -39,24 +39,24 @@ cdef extern from "libavfilter/avfilter.h" nogil:
cdef int avfilter_init_str(AVFilterContext *ctx, const char *args)
cdef int avfilter_init_dict(AVFilterContext *ctx, AVDictionary **options)
cdef void avfilter_free(AVFilterContext*)
cdef AVClass* avfilter_get_class()
cdef const AVClass* avfilter_get_class()

cdef struct AVFilterLink:
AVFilterContext *src
AVFilterPad *srcpad
AVFilterContext *dst
AVFilterPad *dstpad
AVMediaType Type
AVMediaType type
int w
int h
AVRational sample_aspect_ratio
uint64_t channel_layout
AVChannelLayout ch_layout
int sample_rate
int format
AVRational time_base

cdef struct AVFilterGraph:
int nb_filters
unsigned int nb_filters
AVFilterContext **filters
int nb_threads

Expand All @@ -75,7 +75,7 @@ cdef extern from "libavfilter/avfilter.h" nogil:
)
cdef int avfilter_graph_create_filter(
AVFilterContext **filt_ctx,
AVFilter *filt,
const AVFilter *filt,
const char *name,
const char *args,
void *opaque,
Expand Down
Loading
Loading