Skip to content

Commit

Permalink
Reorganize API compatibility defines
Browse files Browse the repository at this point in the history
h4tr3d committed Jan 13, 2025

Verified

This commit was signed with the committer’s verified signature.
snyk-bot Snyk bot
1 parent 251d97e commit 8cc2ba9
Showing 6 changed files with 26 additions and 36 deletions.
9 changes: 8 additions & 1 deletion src/avutils.h
Original file line number Diff line number Diff line change
@@ -45,14 +45,21 @@ extern "C" {
# define FF_ENABLE_DEPRECATION_WARNINGS
#endif

// sizeof(AVPacket) is no part of the public ABI, packet must be allocated in heap
#define API_AVCODEC_NEW_INIT_PACKET (LIBAVCODEC_VERSION_MAJOR >= 58)
// some fields in the AVCodec structure deprecard and replaced by the call of avcodec_get_supported_config()
#define API_AVCODEC_GET_SUPPORTED_CONFIG (LIBAVCODEC_VERSION_INT >= AV_VERSION_INT(61, 13, 100))
// av_stream_get_codec_timebase() deprecard now without replacement
#define API_AVFORMAT_AV_STREAM_GET_CODEC_TIMEBASE (LIBAVFORMAT_VERSION_INT < AV_VERSION_INT(61, 5, 101))

// Allow to use spece-ship operator, whem possible
#if __has_include(<compare>)
# include <compare>
# if defined(__cpp_lib_three_way_comparison) && __cpp_lib_three_way_comparison >= 201907
# define AVCPP_USE_SPACESHIP_OPERATOR 1
# endif
#endif


//
// Functions
//
12 changes: 6 additions & 6 deletions src/codec.cpp
Original file line number Diff line number Diff line change
@@ -68,7 +68,7 @@ std::deque<Rational> Codec::supportedFramerates() const
return {};
deque<Rational> frameRates;
const AVRational *frameRatesRaw = nullptr;
#if USE_AVCODEC_GET_SUPPORTED_CONFIG
#if API_AVCODEC_GET_SUPPORTED_CONFIG
avcodec_get_supported_config(nullptr, m_raw, AV_CODEC_CONFIG_FRAME_RATE, 0, reinterpret_cast<const void**>(&frameRatesRaw), nullptr);
#else
frameRatesRaw = m_raw->supported_framerates;
@@ -91,7 +91,7 @@ std::deque<PixelFormat> Codec::supportedPixelFormats() const
deque<PixelFormat> pixFmts;
const enum AVPixelFormat *pixFmtsRaw = nullptr;

#if USE_AVCODEC_GET_SUPPORTED_CONFIG
#if API_AVCODEC_GET_SUPPORTED_CONFIG
avcodec_get_supported_config(nullptr, m_raw, AV_CODEC_CONFIG_PIX_FORMAT, 0, reinterpret_cast<const void**>(&pixFmtsRaw), nullptr);
#else
pixFmtsRaw = m_raw->pix_fmts;
@@ -115,7 +115,7 @@ std::deque<int> Codec::supportedSamplerates() const
deque<int> sampleRates;
const int *sampleRatesRaw = nullptr;

#if USE_AVCODEC_GET_SUPPORTED_CONFIG
#if API_AVCODEC_GET_SUPPORTED_CONFIG
avcodec_get_supported_config(nullptr, m_raw, AV_CODEC_CONFIG_SAMPLE_RATE, 0, reinterpret_cast<const void**>(&sampleRatesRaw), nullptr);
#else
sampleRatesRaw = m_raw->supported_samplerates;
@@ -139,7 +139,7 @@ std::deque<SampleFormat> Codec::supportedSampleFormats() const
deque<SampleFormat> sampleFmts;
const enum AVSampleFormat *sampleFmtsRaw = nullptr;

#if USE_AVCODEC_GET_SUPPORTED_CONFIG
#if API_AVCODEC_GET_SUPPORTED_CONFIG
avcodec_get_supported_config(nullptr, m_raw, AV_CODEC_CONFIG_SAMPLE_FORMAT, 0, reinterpret_cast<const void**>(&sampleFmtsRaw), nullptr);
#else
sampleFmtsRaw = m_raw->sample_fmts;
@@ -165,7 +165,7 @@ std::deque<uint64_t> Codec::supportedChannelLayouts() const
#if API_NEW_CHANNEL_LAYOUT
const AVChannelLayout *channelLayoutsRaw = nullptr;

#if USE_AVCODEC_GET_SUPPORTED_CONFIG
#if API_AVCODEC_GET_SUPPORTED_CONFIG
avcodec_get_supported_config(nullptr, m_raw, AV_CODEC_CONFIG_CHANNEL_LAYOUT, 0, reinterpret_cast<const void**>(&channelLayoutsRaw), nullptr);
#else
channelLayoutsRaw = m_raw->ch_layouts;
@@ -198,7 +198,7 @@ std::deque<ChannelLayoutView> Codec::supportedChannelLayouts2() const
deque<ChannelLayoutView> channelLayouts;
const AVChannelLayout *channelLayoutsRaw = nullptr;

#if USE_AVCODEC_GET_SUPPORTED_CONFIG
#if API_AVCODEC_GET_SUPPORTED_CONFIG
avcodec_get_supported_config(nullptr, m_raw, AV_CODEC_CONFIG_CHANNEL_LAYOUT, 0, reinterpret_cast<const void**>(&channelLayoutsRaw), nullptr);
#else
channelLayoutsRaw = m_raw->ch_layouts;
6 changes: 3 additions & 3 deletions src/codeccontext.cpp
Original file line number Diff line number Diff line change
@@ -173,7 +173,7 @@ const int *get_supported_samplerates(const struct AVCodec *codec)
{
const int *sampleRates = nullptr;

#if USE_AVCODEC_GET_SUPPORTED_CONFIG
#if API_AVCODEC_GET_SUPPORTED_CONFIG
avcodec_get_supported_config(nullptr, codec,
AV_CODEC_CONFIG_SAMPLE_RATE, 0,
reinterpret_cast<const void**>(&sampleRates), nullptr);
@@ -403,7 +403,7 @@ void CodecContext2::setCodec(const Codec &codec, bool resetDefaults, Direction d
const enum AVPixelFormat *pixFmts = nullptr;
const enum AVSampleFormat *sampleFmts = nullptr;

#if USE_AVCODEC_GET_SUPPORTED_CONFIG
#if API_AVCODEC_GET_SUPPORTED_CONFIG
avcodec_get_supported_config(nullptr, codec.raw(),
AV_CODEC_CONFIG_PIX_FORMAT, 0,
reinterpret_cast<const void**>(&pixFmts), nullptr);
@@ -1049,7 +1049,7 @@ CodecContext2::encodeCommon(Packet &outPacket,
outPacket.setStreamIndex(inFrame.streamIndex());
} else if (m_stream.isValid()) {
#if USE_CODECPAR
#if USE_AV_STREAM_GET_CODEC_TIMEBASE
#if API_AVFORMAT_AV_STREAM_GET_CODEC_TIMEBASE
outPacket.setTimeBase(av_stream_get_codec_timebase(m_stream.raw()));
#else
// TBD: additional checking are needed
16 changes: 0 additions & 16 deletions src/ffmpeg.h
Original file line number Diff line number Diff line change
@@ -62,12 +62,6 @@ inline AVMediaType avfilter_pad_get_type(AVFilterPad *pads, int pad_idx)
#define avpacket_unref(p) av_packet_unref(p)
#endif

#define NO_INIT_PACKET (LIBAVCODEC_VERSION_MAJOR >= 60)
#define DEPRECATED_INIT_PACKET (LIBAVCODEC_VERSION_MAJOR >= 58)

#define USE_AVCODEC_GET_SUPPORTED_CONFIG (LIBAVCODEC_VERSION_INT >= AV_VERSION_INT(61, 13, 100))
#define USE_AV_STREAM_GET_CODEC_TIMEBASE (LIBAVFORMAT_VERSION_INT < AV_VERSION_INT(61, 5, 101))

template<typename T>
struct FFWrapperPtr
{
@@ -183,13 +177,3 @@ struct PixSampleFmtWrapper
protected:
T m_fmt = NoneValue;
};

// Extended attributes
#if AV_GCC_VERSION_AT_LEAST(3,1)
# define attribute_deprecated2(x) __attribute__((deprecated(x)))
#elif defined(_MSC_VER)
# define attribute_deprecated2(x) __declspec(deprecated(x))
#else
# define attribute_deprecated2(x)
#endif

11 changes: 5 additions & 6 deletions src/packet.cpp
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
#include "packet.h"
#include "avutils.h"

using namespace std;

namespace av {

Packet::Packet()
{
#if DEPRECATED_INIT_PACKET
#if API_AVCODEC_NEW_INIT_PACKET
m_raw = av_packet_alloc();
#else
av_init_packet(raw());
@@ -100,7 +99,7 @@ Packet::Packet(uint8_t *data, size_t size, Packet::wrap_data_static, OptionalErr

Packet::~Packet()
{
#if DEPRECATED_INIT_PACKET
#if API_AVCODEC_NEW_INIT_PACKET
av_packet_free(&m_raw);
#else
avpacket_unref(&m_raw);
@@ -121,7 +120,7 @@ void Packet::initFromAVPacket(const AVPacket *src, bool deepCopy, OptionalErrorC
avpacket_unref(raw());

if (deepCopy) {
#if DEPRECATED_INIT_PACKET
#if API_AVCODEC_NEW_INIT_PACKET
if (!m_raw)
m_raw = av_packet_alloc();

@@ -349,7 +348,7 @@ int Packet::refCount() const
return 0;
}

#if DEPRECATED_INIT_PACKET
#if API_AVCODEC_NEW_INIT_PACKET
AVPacket *Packet::makeRef(OptionalErrorCode ec) const
{
clear_if(ec);
@@ -416,7 +415,7 @@ Packet &Packet::operator=(Packet &&rhs)
return *this;
}

#if DEPRECATED_INIT_PACKET
#if API_AVCODEC_NEW_INIT_PACKET
Packet &Packet::operator=(const AVPacket *rhs)
{
if (rhs == raw())
8 changes: 4 additions & 4 deletions src/packet.h
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
#pragma once

#include <iostream>
#include <vector>

#include "ffmpeg.h"
#include "avutils.h"
#include "rational.h"
#include "stream.h"
#include "averror.h"
@@ -16,7 +16,7 @@ extern "C" {
namespace av {

class Packet :
#if DEPRECATED_INIT_PACKET
#if API_AVCODEC_NEW_INIT_PACKET
public FFWrapperPtr<AVPacket>
#else
public FFWrapperRef<AVPacket>
@@ -98,7 +98,7 @@ class Packet :
bool isReferenced() const;
int refCount() const;

#if DEPRECATED_INIT_PACKET
#if API_AVCODEC_NEW_INIT_PACKET
AVPacket* makeRef(OptionalErrorCode ec) const;
#else
AVPacket makeRef(OptionalErrorCode ec = throws()) const;
@@ -109,7 +109,7 @@ class Packet :
Packet &operator=(const Packet &rhs);
Packet &operator=(Packet &&rhs);

#if DEPRECATED_INIT_PACKET
#if API_AVCODEC_NEW_INIT_PACKET
Packet &operator=(const AVPacket *rhs);
#else
Packet &operator=(const AVPacket &rhs);

0 comments on commit 8cc2ba9

Please sign in to comment.