diff --git a/CHANGELOG.rst b/CHANGELOG.rst index 48fe78d4d..2814b76ab 100644 --- a/CHANGELOG.rst +++ b/CHANGELOG.rst @@ -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`. diff --git a/av/_core.pxd b/av/_core.pxd index 43d1e716d..2d9e95209 100644 --- a/av/_core.pxd +++ b/av/_core.pxd @@ -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() diff --git a/av/packet.py b/av/packet.py index 8be300661..3fb7d4f4c 100644 --- a/av/packet.py +++ b/av/packet.py @@ -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 @@ -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 diff --git a/av/packet.pyi b/av/packet.pyi index 805a3cf97..1f005bf27 100644 --- a/av/packet.pyi +++ b/av/packet.pyi @@ -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 @@ -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 diff --git a/av/video/codeccontext.py b/av/video/codeccontext.py index 3dca9619b..2e39a7feb 100644 --- a/av/video/codeccontext.py +++ b/av/video/codeccontext.py @@ -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): diff --git a/include/avcodec.pxd b/include/avcodec.pxd index 2255738f6..0a37cb91c 100644 --- a/include/avcodec.pxd +++ b/include/avcodec.pxd @@ -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, @@ -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 @@ -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 @@ -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 @@ -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 @@ -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) @@ -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 @@ -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 @@ -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 @@ -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 @@ -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 ) @@ -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 @@ -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 @@ -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) diff --git a/include/avdevice.pxd b/include/avdevice.pxd index 9631337fa..18a4dfed4 100644 --- a/include/avdevice.pxd +++ b/include/avdevice.pxd @@ -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: diff --git a/include/avfilter.pxd b/include/avfilter.pxd index afb953fdd..2905a8c88 100644 --- a/include/avfilter.pxd +++ b/include/avfilter.pxd @@ -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 @@ -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 @@ -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 @@ -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, diff --git a/include/avformat.pxd b/include/avformat.pxd index 54103f723..9a18e5a9b 100644 --- a/include/avformat.pxd +++ b/include/avformat.pxd @@ -2,9 +2,9 @@ from libc.stdint cimport int64_t, uint64_t cdef extern from "libavformat/avformat.h" nogil: - cdef int avformat_version() - cdef char* avformat_configuration() - cdef char* avformat_license() + cdef unsigned int avformat_version() + cdef const char* avformat_configuration() + cdef const char* avformat_license() cdef int AV_TIME_BASE cdef int AVSEEK_FLAG_BACKWARD @@ -91,8 +91,6 @@ cdef extern from "libavformat/avformat.h" nogil: int flags const AVClass *priv_class - int avformat_query_codec(const AVOutputFormat *oformat, AVCodecID codec_id, int std_compliance) - # AVInputFormat.flags and AVOutputFormat.flags cdef enum: AVFMT_NOFILE @@ -134,7 +132,7 @@ cdef extern from "libavformat/avformat.h" nogil: AVMediaType type, int wanted_stream_nb, int related_stream, - AVCodec **decoder_ret, + const AVCodec **decoder_ret, int flags ) @@ -142,6 +140,7 @@ cdef extern from "libavformat/avformat.h" nogil: # http://ffmpeg.org/doxygen/trunk/structAVFormatContext.html cdef struct AVFormatContext: + const AVClass *av_class unsigned int nb_streams AVStream **streams unsigned int nb_chapters @@ -151,7 +150,6 @@ cdef extern from "libavformat/avformat.h" nogil: AVIOContext *pb AVIOInterruptCB interrupt_callback AVDictionary *metadata - char filename int64_t start_time int64_t duration int64_t bit_rate @@ -171,23 +169,23 @@ cdef extern from "libavformat/avformat.h" nogil: cdef AVFormatContext* avformat_alloc_context() cdef int avformat_open_input( AVFormatContext **ctx, - char *filename, + const char *filename, const AVInputFormat *format, AVDictionary **options ) - cdef int avformat_close_input(AVFormatContext **ctx) + cdef void avformat_close_input(AVFormatContext **ctx) cdef int avformat_write_header(AVFormatContext *ctx, AVDictionary **options) cdef int av_write_trailer(AVFormatContext *ctx) cdef int av_interleaved_write_frame(AVFormatContext *ctx, AVPacket *pkt) cdef int av_write_frame(AVFormatContext *ctx, AVPacket *pkt) - cdef int avio_open(AVIOContext **s, char *url, int flags) + cdef int avio_open(AVIOContext **s, const char *url, int flags) cdef int64_t avio_size(AVIOContext *s) cdef const AVOutputFormat* av_guess_format( - char *short_name, char *filename, char *mime_type + const char *short_name, const char *filename, const char *mime_type ) cdef int avformat_query_codec( - AVOutputFormat *ofmt, AVCodecID codec_id, int std_compliance + const AVOutputFormat *ofmt, AVCodecID codec_id, int std_compliance ) cdef void avio_flush(AVIOContext *s) cdef int avio_close(AVIOContext *s) @@ -197,12 +195,12 @@ cdef extern from "libavformat/avformat.h" nogil: cdef int avformat_alloc_output_context2( AVFormatContext **ctx, const AVOutputFormat *oformat, - char *format_name, - char *filename + const char *format_name, + const char *filename ) - cdef int avformat_free_context(AVFormatContext *ctx) - cdef AVClass* avformat_get_class() - cdef void av_dump_format(AVFormatContext *ctx, int index, char *url, int is_output) + cdef void avformat_free_context(AVFormatContext *ctx) + cdef const AVClass* avformat_get_class() + cdef void av_dump_format(AVFormatContext *ctx, int index, const char *url, int is_output) cdef int av_read_frame(AVFormatContext *ctx, AVPacket *packet) cdef int av_seek_frame( AVFormatContext *ctx, int stream_index, int64_t timestamp, int flags @@ -237,6 +235,6 @@ cdef extern from "libavformat/avformat.h" nogil: AVINDEX_KEYFRAME AVINDEX_DISCARD_FRAME - cdef AVIndexEntry *avformat_index_get_entry(AVStream *st, int idx) - cdef int avformat_index_get_entries_count(AVStream *st) + cdef const AVIndexEntry *avformat_index_get_entry(AVStream *st, int idx) + cdef int avformat_index_get_entries_count(const AVStream *st) cdef int av_index_search_timestamp(AVStream *st, int64_t timestamp, int flags) diff --git a/include/avutil.pxd b/include/avutil.pxd index 615b6979d..2cf76c230 100644 --- a/include/avutil.pxd +++ b/include/avutil.pxd @@ -8,15 +8,15 @@ cdef extern from "libavutil/audio_fifo.h" nogil: cdef AVAudioFifo* av_audio_fifo_alloc( AVSampleFormat sample_fmt, int channels, int nb_samples ) - cdef int av_audio_fifo_write(AVAudioFifo *af, void **data, int nb_samples) - cdef int av_audio_fifo_read(AVAudioFifo *af, void **data, int nb_samples) + cdef int av_audio_fifo_write(AVAudioFifo *af, void *const *data, int nb_samples) + cdef int av_audio_fifo_read(AVAudioFifo *af, void *const *data, int nb_samples) cdef int av_audio_fifo_size(AVAudioFifo *af) cdef extern from "libavutil/avutil.h" nogil: cdef const char* av_version_info() - cdef int avutil_version() - cdef char* avutil_configuration() - cdef char* avutil_license() + cdef unsigned int avutil_version() + cdef const char* avutil_configuration() + cdef const char* avutil_license() int FF_QP2LAMBDA @@ -113,18 +113,14 @@ cdef extern from "libavutil/avutil.h" nogil: cdef extern from "libavutil/buffer.h" nogil: AVBufferRef *av_buffer_create(uint8_t *data, size_t size, void (*free)(void *opaque, uint8_t *data), void *opaque, int flags) - AVBufferRef* av_buffer_ref(AVBufferRef *buf) + AVBufferRef* av_buffer_ref(const AVBufferRef *buf) void av_buffer_unref(AVBufferRef **buf) cdef struct AVBuffer: - uint8_t *data - int size - void (*free)(void *opaque, uint8_t *data) - void *opaque - int flags + pass cdef struct AVBufferRef: AVBuffer *buffer uint8_t *data - int size + size_t size cdef extern from "libavutil/dict.h" nogil: # See: http://ffmpeg.org/doxygen/trunk/structAVDictionary.html @@ -137,13 +133,16 @@ cdef extern from "libavutil/dict.h" nogil: cdef int AV_DICT_IGNORE_SUFFIX cdef void av_dict_free(AVDictionary **) cdef AVDictionaryEntry* av_dict_get( - AVDictionary *dict, char *key, AVDictionaryEntry *prev, int flags + const AVDictionary *dict, + const char *key, + const AVDictionaryEntry *prev, + int flags ) cdef int av_dict_set( AVDictionary **pm, const char *key, const char *value, int flags ) - cdef int av_dict_count(AVDictionary *m) - cdef int av_dict_copy(AVDictionary **dst, AVDictionary *src, int flags) + cdef int av_dict_count(const AVDictionary *m) + cdef int av_dict_copy(AVDictionary **dst, const AVDictionary *src, int flags) cdef extern from "libavutil/display.h" nogil: cdef double av_display_rotation_get(const int32_t matrix[9]) @@ -189,9 +188,12 @@ cdef extern from "libavutil/frame.h" nogil: cdef int av_frame_get_buffer(AVFrame *frame, int align) cdef int av_frame_make_writable(AVFrame *frame) cdef int av_frame_copy_props(AVFrame *dst, const AVFrame *src) - cdef AVFrameSideData* av_frame_get_side_data(AVFrame *frame, AVFrameSideDataType type) + cdef AVFrameSideData* av_frame_get_side_data(const AVFrame *frame, AVFrameSideDataType type) cdef extern from "libavutil/hwcontext.h" nogil: + cdef struct AVHWDeviceContext: + pass + enum AVHWDeviceType: AV_HWDEVICE_TYPE_NONE AV_HWDEVICE_TYPE_VDPAU @@ -208,15 +210,15 @@ cdef extern from "libavutil/hwcontext.h" nogil: AV_HWDEVICE_TYPE_D3D12VA ctypedef struct AVHWFramesContext: - const void *av_class + const AVClass *av_class AVBufferRef *device_ref - void *device_ctx + AVHWDeviceContext *device_ctx void *hwctx + int initial_pool_size AVPixelFormat format AVPixelFormat sw_format int width int height - int initial_pool_size cdef int av_hwdevice_ctx_create(AVBufferRef **device_ctx, AVHWDeviceType type, const char *device, AVDictionary *opts, int flags) cdef AVHWDeviceType av_hwdevice_find_type_by_name(const char *name) @@ -304,7 +306,7 @@ cdef extern from "libavutil/opt.h" nogil: AV_OPT_TYPE_CHLAYOUT AV_OPT_TYPE_BOOL - cdef struct AVOption_default_val: + cdef union AVOption_default_val: int64_t i64 double dbl const char *str @@ -334,11 +336,11 @@ cdef extern from "libavutil/opt.h" nogil: cdef extern from "libavutil/pixdesc.h" nogil: # See: http://ffmpeg.org/doxygen/trunk/structAVComponentDescriptor.html cdef struct AVComponentDescriptor: - unsigned int plane - unsigned int step - unsigned int offset - unsigned int shift - unsigned int depth + int plane + int step + int offset + int shift + int depth cdef enum AVPixFmtFlags: AV_PIX_FMT_FLAG_BE @@ -354,13 +356,13 @@ cdef extern from "libavutil/pixdesc.h" nogil: uint8_t nb_components uint8_t log2_chroma_w uint8_t log2_chroma_h - uint8_t flags + uint64_t flags AVComponentDescriptor comp[4] cdef const AVPixFmtDescriptor* av_pix_fmt_desc_get(AVPixelFormat pix_fmt) cdef const AVPixFmtDescriptor* av_pix_fmt_desc_next(const AVPixFmtDescriptor *prev) - cdef char * av_get_pix_fmt_name(AVPixelFormat pix_fmt) - cdef AVPixelFormat av_get_pix_fmt(char* name) + cdef const char *av_get_pix_fmt_name(AVPixelFormat pix_fmt) + cdef AVPixelFormat av_get_pix_fmt(const char *name) int av_get_bits_per_pixel(const AVPixFmtDescriptor *pixdesc) int av_get_padded_bits_per_pixel(const AVPixFmtDescriptor *pixdesc) @@ -379,8 +381,8 @@ cdef extern from "libavutil/samplefmt.h" nogil: AV_SAMPLE_FMT_FLT AV_SAMPLE_FMT_DBL - cdef AVSampleFormat av_get_sample_fmt(char* name) - cdef char *av_get_sample_fmt_name(AVSampleFormat sample_fmt) + cdef AVSampleFormat av_get_sample_fmt(const char *name) + cdef const char *av_get_sample_fmt_name(AVSampleFormat sample_fmt) cdef int av_get_bytes_per_sample(AVSampleFormat sample_fmt) cdef int av_sample_fmt_is_planar(AVSampleFormat sample_fmt) cdef AVSampleFormat av_get_packed_sample_fmt(AVSampleFormat sample_fmt) @@ -401,7 +403,7 @@ cdef extern from "libavutil/video_enc_params.h" nogil: AV_VIDEO_ENC_PARAMS_MPEG2 cdef struct AVVideoEncParams: - uint32_t nb_blocks + unsigned int nb_blocks size_t blocks_offset size_t block_size AVVideoEncParamsType type @@ -409,10 +411,10 @@ cdef extern from "libavutil/video_enc_params.h" nogil: int32_t delta_qp[4][2] cdef struct AVVideoBlockParams: - int32_t src_x - int32_t src_y - int32_t w - int32_t h + int src_x + int src_y + int w + int h int32_t delta_qp cdef extern from "stdarg.h" nogil: diff --git a/tests/test_packet.py b/tests/test_packet.py index c4503caad..e266bbe3c 100644 --- a/tests/test_packet.py +++ b/tests/test_packet.py @@ -6,6 +6,7 @@ from unittest import SkipTest import numpy +import pytest import av @@ -63,6 +64,45 @@ def test_data_packet_bytes(self): class TestProperties: + def test_rescale_ts(self) -> None: + packet = av.Packet() + packet.time_base = fractions.Fraction(1, 1000) + packet.pts = 1000 + packet.dts = 900 + packet.duration = 40 + + packet.rescale_ts(av.AVRational(1, 100)) + + assert packet.time_base == fractions.Fraction(1, 100) + assert packet.pts == 100 + assert packet.dts == 90 + assert packet.duration == 4 + + def test_rescale_ts_without_source_time_base(self) -> None: + packet = av.Packet() + packet.pts = 1000 + packet.dts = 900 + packet.duration = 40 + + packet.rescale_ts(av.AVRational(1, 100)) + + assert packet.time_base == fractions.Fraction(1, 100) + assert packet.pts == 1000 + assert packet.dts == 900 + assert packet.duration == 40 + + def test_rescale_ts_rejects_zero_time_base(self) -> None: + packet = av.Packet() + + with pytest.raises(ValueError, match="Cannot rebase to zero time"): + packet.rescale_ts(av.AVRational(0, 1)) + + def test_rescale_ts_requires_avrational(self) -> None: + packet = av.Packet() + + with pytest.raises(TypeError, match="time_base must be an AVRational"): + packet.rescale_ts(fractions.Fraction(1, 100)) # type: ignore[arg-type] + def test_is_keyframe(self) -> None: with av.open(fate_suite("h264/interlaced_crop.mp4")) as container: stream = container.streams.video[0]