Blender V5.3
ffmpeg_compat.h
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2011 Peter Schlaile
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
13
14#pragma once
15
16#include <libavcodec/avcodec.h>
17#include <libavformat/avformat.h>
18#include <libavutil/cpu.h>
19#include <libavutil/display.h>
20#include <libswscale/swscale.h>
21
22/* Check if our FFMPEG is new enough, avoids user complaints.
23 * Minimum supported version is currently 3.2.0 which mean the following library versions:
24 * `libavutil` > 55.30
25 * `libavcodec` > 57.60
26 * `libavformat` > 57.50
27 *
28 * We only check for one of these as they are usually updated in tandem.
29 */
30#if (LIBAVFORMAT_VERSION_MAJOR < 57) || \
31 ((LIBAVFORMAT_VERSION_MAJOR == 57) && (LIBAVFORMAT_VERSION_MINOR <= 50))
32# error "FFmpeg 3.2.0 or newer is needed, Upgrade your FFmpeg or disable it"
33#endif
34/* end sanity check */
35
36/* visual studio 2012 does not define inline for C */
37#ifdef _MSC_VER
38# define FFMPEG_INLINE static __inline
39#else
40# define FFMPEG_INLINE static inline
41#endif
42
43#if LIBAVUTIL_VERSION_INT < AV_VERSION_INT(58, 29, 100)
44/* In FFMPEG 6.1 usage of the "key_frame" variable from "AVFrame" has been deprecated.
45 * used the new method to query for the "AV_FRAME_FLAG_KEY" flag instead.
46 */
47# define FFMPEG_OLD_KEY_FRAME_QUERY_METHOD
48#endif
49
50#if (LIBAVFORMAT_VERSION_MAJOR < 59)
51/* For versions older than FFMPEG 5.0, use the old channel layout variables.
52 * We intend to only keep this workaround for around two releases (3.5, 3.6).
53 * If it sticks around any longer, then we should consider refactoring this.
54 */
55# define FFMPEG_USE_OLD_CHANNEL_VARS
56#endif
57
58/* Threaded sws_scale_frame was added in FFMPEG 5.0 (`swscale` version 6.1). */
59#if (LIBSWSCALE_VERSION_INT >= AV_VERSION_INT(6, 1, 100))
60# define FFMPEG_SWSCALE_THREADING
61#endif
62
63/* AV_CODEC_CAP_AUTO_THREADS was renamed to AV_CODEC_CAP_OTHER_THREADS with
64 * upstream commit
65 * `github.com/FFmpeg/FFmpeg/commit/7d09579190def3ef7562399489e628f3b65714ce`
66 * (`lavc` 58.132.100) and removed with commit
67 * `github.com/FFmpeg/FFmpeg/commit/10c9a0874cb361336237557391d306d26d43f137`
68 * for FFMPEG 6.0.
69 */
70#if LIBAVCODEC_VERSION_INT < AV_VERSION_INT(58, 132, 100)
71# define AV_CODEC_CAP_OTHER_THREADS AV_CODEC_CAP_AUTO_THREADS
72#endif
73
74#if (LIBAVFORMAT_VERSION_MAJOR < 58) || \
75 ((LIBAVFORMAT_VERSION_MAJOR == 58) && (LIBAVFORMAT_VERSION_MINOR < 76))
76# define FFMPEG_USE_DURATION_WORKAROUND 1
77
78/* Before FFMPEG 4.4, package duration calculation used deprecated variables to calculate the
79 * packet duration. Use the function from commit
80 * `github.com/FFmpeg/FFmpeg/commit/1c0885334dda9ee8652e60c586fa2e3674056586`
81 * to calculate the correct frame-rate for FFMPEG < 4.4.
82 */
83
85void my_guess_pkt_duration(AVFormatContext *s, AVStream *st, AVPacket *pkt)
86{
87 if (pkt->duration < 0 && st->codecpar->codec_type != AVMEDIA_TYPE_SUBTITLE) {
88 av_log(s,
89 AV_LOG_WARNING,
90 "Packet with invalid duration %" PRId64 " in stream %d\n",
91 pkt->duration,
92 pkt->stream_index);
93 pkt->duration = 0;
94 }
95
96 if (pkt->duration) {
97 return;
98 }
99
100 switch (st->codecpar->codec_type) {
101 case AVMEDIA_TYPE_VIDEO:
102 if (st->avg_frame_rate.num > 0 && st->avg_frame_rate.den > 0) {
103 pkt->duration = av_rescale_q(1, av_inv_q(st->avg_frame_rate), st->time_base);
104 }
105 else if (st->time_base.num * 1000LL > st->time_base.den) {
106 pkt->duration = 1;
107 }
108 break;
109 case AVMEDIA_TYPE_AUDIO: {
110 int frame_size = av_get_audio_frame_duration2(st->codecpar, pkt->size);
111 if (frame_size && st->codecpar->sample_rate) {
112 pkt->duration = av_rescale_q(
113 frame_size, (AVRational){1, st->codecpar->sample_rate}, st->time_base);
114 }
115 break;
116 }
117 default:
118 break;
119 }
120}
121#endif
122
125{
126 /* Some videos do not have any pts values, use dts instead in those cases if
127 * possible. Usually when this happens dts can act as pts because as all frames
128 * should then be presented in their decoded in order. IE pts == dts. */
129 if (pts == AV_NOPTS_VALUE) {
130 return dts;
131 }
132 return pts;
133}
134
137{
138 return timestamp_from_pts_or_dts(picture->pts, picture->pkt_dts);
139}
140
141/* Duration of the frame, in the same units as pts. 0 if unknown. */
144{
145#if LIBAVUTIL_VERSION_INT < AV_VERSION_INT(57, 30, 100)
146 return picture->pkt_duration;
147#else
148 return picture->duration;
149#endif
150}
151
153{
154 /* NOTE: even if av_frame_get_buffer suggests to pass 0 for alignment,
155 * as of FFMPEG 6.1/7.0 it does not use correct alignment for AVX512
156 * CPU (frame.c get_video_buffer ends up always using 32 alignment,
157 * whereas it should have used 64). Reported upstream:
158 * https://trac.ffmpeg.org/ticket/11116 and the fix on their code
159 * side is to use 64 byte alignment as soon as AVX512 is compiled
160 * in (even if CPU might not support it). So play safe and
161 * use at least 64 byte alignment here too. Currently larger than
162 * 64 alignment would not happen anywhere, but keep on querying
163 * av_cpu_max_align just in case some future platform might. */
164 size_t align = av_cpu_max_align();
165 if (align < 64) {
166 align = 64;
167 }
168 return align;
169}
170
171FFMPEG_INLINE void ffmpeg_copy_display_matrix(const AVStream *src, AVStream *dst)
172{
173#if LIBAVCODEC_VERSION_INT >= AV_VERSION_INT(60, 29, 100)
174 const AVPacketSideData *src_matrix = av_packet_side_data_get(src->codecpar->coded_side_data,
175 src->codecpar->nb_coded_side_data,
176 AV_PKT_DATA_DISPLAYMATRIX);
177 if (src_matrix != nullptr) {
178 uint8_t *dst_matrix = (uint8_t *)av_memdup(src_matrix->data, src_matrix->size);
179 av_packet_side_data_add(&dst->codecpar->coded_side_data,
180 &dst->codecpar->nb_coded_side_data,
181 AV_PKT_DATA_DISPLAYMATRIX,
182 dst_matrix,
183 src_matrix->size,
184 0);
185 }
186#endif
187}
188
189FFMPEG_INLINE int ffmpeg_get_video_rotation(const AVStream *stream)
190{
191#if LIBAVCODEC_VERSION_INT >= AV_VERSION_INT(60, 29, 100)
192 const AVPacketSideData *src_matrix = av_packet_side_data_get(
193 stream->codecpar->coded_side_data,
194 stream->codecpar->nb_coded_side_data,
195 AV_PKT_DATA_DISPLAYMATRIX);
196 if (src_matrix != nullptr) {
197 /* ffmpeg reports rotation in [-180..+180] range; our image rotation
198 * uses different direction and [0..360] range. */
199 double theta = -av_display_rotation_get((const int32_t *)src_matrix->data);
200 if (theta < 0.0) {
201 theta += 360.0;
202 }
203 return int(theta);
204 }
205#endif
206 return 0;
207}
208
209#if LIBAVCODEC_VERSION_INT >= AV_VERSION_INT(61, 13, 100)
210FFMPEG_INLINE const enum AVPixelFormat *ffmpeg_get_pix_fmts(struct AVCodecContext *context,
211 const AVCodec *codec)
212{
213 const enum AVPixelFormat *pix_fmts = nullptr;
214 avcodec_get_supported_config(
215 context, codec, AV_CODEC_CONFIG_PIX_FORMAT, 0, (const void **)&pix_fmts, nullptr);
216 return pix_fmts;
217}
218
219FFMPEG_INLINE const enum AVSampleFormat *ffmpeg_get_sample_fmts(struct AVCodecContext *context,
220 const AVCodec *codec)
221{
222 const enum AVSampleFormat *sample_fmts = nullptr;
223 avcodec_get_supported_config(
224 context, codec, AV_CODEC_CONFIG_SAMPLE_FORMAT, 0, (const void **)&sample_fmts, nullptr);
225 return sample_fmts;
226}
227
228FFMPEG_INLINE const int *ffmpeg_get_sample_rates(struct AVCodecContext *context,
229 const AVCodec *codec)
230{
231 const int *sample_rates = nullptr;
232 avcodec_get_supported_config(
233 context, codec, AV_CODEC_CONFIG_SAMPLE_RATE, 0, (const void **)&sample_rates, nullptr);
234 return sample_rates;
235}
236#else
237FFMPEG_INLINE const enum AVPixelFormat *ffmpeg_get_pix_fmts(struct AVCodecContext * /*context*/,
238 const AVCodec *codec)
239{
240 return codec->pix_fmts;
241}
242
243FFMPEG_INLINE const enum AVSampleFormat *ffmpeg_get_sample_fmts(
244 struct AVCodecContext * /*context*/, const AVCodec *codec)
245{
246 return codec->sample_fmts;
247}
248
249FFMPEG_INLINE const int *ffmpeg_get_sample_rates(struct AVCodecContext * /*context*/,
250 const AVCodec *codec)
251{
252 return codec->supported_samplerates;
253}
254#endif
long long int int64_t
FFMPEG_INLINE int64_t av_get_frame_duration_in_pts_units(const AVFrame *picture)
FFMPEG_INLINE size_t ffmpeg_get_buffer_alignment()
FFMPEG_INLINE int64_t timestamp_from_pts_or_dts(int64_t pts, int64_t dts)
FFMPEG_INLINE enum AVSampleFormat * ffmpeg_get_sample_fmts(struct AVCodecContext *context, const AVCodec *codec)
FFMPEG_INLINE int64_t av_get_pts_from_frame(AVFrame *picture)
FFMPEG_INLINE const int * ffmpeg_get_sample_rates(struct AVCodecContext *context, const AVCodec *codec)
FFMPEG_INLINE enum AVPixelFormat * ffmpeg_get_pix_fmts(struct AVCodecContext *context, const AVCodec *codec)
FFMPEG_INLINE int ffmpeg_get_video_rotation(const AVStream *stream)
FFMPEG_INLINE void my_guess_pkt_duration(AVFormatContext *s, AVStream *st, AVPacket *pkt)
#define FFMPEG_INLINE
FFMPEG_INLINE void ffmpeg_copy_display_matrix(const AVStream *src, AVStream *dst)
#define PRId64
Definition inttypes.h:78