Blender V5.3
bpy_app_ffmpeg.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2023 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
8
9#include "BLI_utildefines.hh"
10#include <Python.h>
11
12#include "../generic/python_compat.hh" /* IWYU pragma: keep. */
13
14#include "bpy_app_ffmpeg.hh"
15
17
18namespace blender {
19
20#ifdef WITH_FFMPEG
21extern "C" {
22# include <libavcodec/avcodec.h>
23# include <libavdevice/avdevice.h>
24# include <libavformat/avformat.h>
25# include <libavutil/avutil.h>
26# include <libswscale/swscale.h>
27}
28#endif
29
30static PyTypeObject BlenderAppFFmpegType;
31
32#define DEF_FFMPEG_LIB_VERSION(lib) \
33 {(#lib "_version"), ("The " #lib " version as a tuple of 3 numbers")}, \
34 { \
35 (#lib "_version_string"), ("The " #lib " version formatted as a string") \
36 }
37
38static PyStructSequence_Field app_ffmpeg_info_fields[] = {
39 {"supported", "Boolean, True when Blender is built with FFmpeg support"},
40
42 DEF_FFMPEG_LIB_VERSION(avdevice),
43 DEF_FFMPEG_LIB_VERSION(avformat),
46 {nullptr},
47};
48
49#undef DEF_FFMPEG_LIB_VERSION
50
51static PyStructSequence_Desc app_ffmpeg_info_desc = {
52 /*name*/ "bpy.app.ffmpeg",
53 /*doc*/ "This module contains information about FFmpeg blender is linked against",
54 /*fields*/ app_ffmpeg_info_fields,
55 /*n_in_sequence*/ ARRAY_SIZE(app_ffmpeg_info_fields) - 1,
56};
57
58static PyObject *make_ffmpeg_info()
59{
60 PyObject *ffmpeg_info;
61 int pos = 0;
62
63#ifdef WITH_FFMPEG
64 int curversion;
65#endif
66
67 ffmpeg_info = PyStructSequence_New(&BlenderAppFFmpegType);
68 if (ffmpeg_info == nullptr) {
69 return nullptr;
70 }
71
72#if 0 /* UNUSED */
73# define SetIntItem(flag) PyStructSequence_SET_ITEM(ffmpeg_info, pos++, PyLong_FromLong(flag))
74#endif
75#ifndef WITH_FFMPEG
76# define SetStrItem(str) PyStructSequence_SET_ITEM(ffmpeg_info, pos++, PyUnicode_FromString(str))
77#endif
78#define SetObjItem(obj) PyStructSequence_SET_ITEM(ffmpeg_info, pos++, obj)
79
80#ifdef WITH_FFMPEG
81# define FFMPEG_LIB_VERSION(lib) \
82 { \
83 curversion = lib##_version(); \
84 SetObjItem( \
85 PyC_Tuple_Pack_I32({curversion >> 16, (curversion >> 8) % 256, curversion % 256})); \
86 SetObjItem(PyUnicode_FromFormat( \
87 "%2d, %2d, %2d", curversion >> 16, (curversion >> 8) % 256, curversion % 256)); \
88 } \
89 (void)0
90#else
91# define FFMPEG_LIB_VERSION(lib) \
92 { \
93 SetStrItem("Unknown"); \
94 SetStrItem("Unknown"); \
95 } \
96 (void)0
97#endif
98
99#ifdef WITH_FFMPEG
100 SetObjItem(PyBool_FromLong(1));
101#else
102 SetObjItem(PyBool_FromLong(0));
103#endif
104
105 FFMPEG_LIB_VERSION(avcodec);
106 FFMPEG_LIB_VERSION(avdevice);
107 FFMPEG_LIB_VERSION(avformat);
108 FFMPEG_LIB_VERSION(avutil);
109 FFMPEG_LIB_VERSION(swscale);
110
111#undef FFMPEG_LIB_VERSION
112
113 if (PyErr_Occurred()) [[unlikely]] {
114 Py_DECREF(ffmpeg_info);
115 return nullptr;
116 }
117
118// #undef SetIntItem
119#undef SetStrItem
120#undef SetObjItem
121
122 return ffmpeg_info;
123}
124
126{
127 PyObject *ret;
128
129 PyStructSequence_InitType(&BlenderAppFFmpegType, &app_ffmpeg_info_desc);
130
132
133 /* prevent user from creating new instances */
134 BlenderAppFFmpegType.tp_init = nullptr;
135 BlenderAppFFmpegType.tp_new = nullptr;
136 /* Without this we can't do `set(sys.modules)` #29635. */
137 BlenderAppFFmpegType.tp_hash = reinterpret_cast<hashfunc>(Py_HashPointer);
138
139 return ret;
140}
141
142} // namespace blender
#define SetObjItem(obj)
#define DEF_FFMPEG_LIB_VERSION(lib)
#define FFMPEG_LIB_VERSION(lib)
BMesh Iterators.
static PyStructSequence_Desc app_ffmpeg_info_desc
static PyStructSequence_Field app_ffmpeg_info_fields[]
constexpr size_t ARRAY_SIZE(const T(&arg)[N]) noexcept
static PyTypeObject BlenderAppFFmpegType
PyObject * BPY_app_ffmpeg_struct()
static PyObject * make_ffmpeg_info()
header-only compatibility defines.
#define Py_HashPointer