code cleanup:
[blender.git] / source / blender / python / intern / bpy_app_ffmpeg.c
1 /*
2  * ***** BEGIN GPL LICENSE BLOCK *****
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU General Public License
6  * as published by the Free Software Foundation; either version 2
7  * of the License, or (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software Foundation,
16  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17  *
18  * Contributor(s): Sergey Sharybin
19  *
20  * ***** END GPL LICENSE BLOCK *****
21  */
22
23 /** \file blender/python/intern/bpy_app_ffmpeg.c
24  *  \ingroup pythonintern
25  */
26
27 #include <Python.h>
28 #include "BLI_utildefines.h"
29
30 #include "bpy_app_ffmpeg.h"
31
32 #ifdef WITH_FFMPEG
33 #include <libavcodec/avcodec.h>
34 #include <libavdevice/avdevice.h>
35 #include <libavformat/avformat.h>
36 #include <libavutil/avutil.h>
37 #include <libswscale/swscale.h>
38 #endif
39
40 static PyTypeObject BlenderAppFFmpegType;
41
42 #define DEF_FFMPEG_LIB_VERSION(lib) \
43         {(char *)(#lib "_version"), (char *)("The " #lib " version  as a tuple of 3 numbers")}, \
44         {(char *)(#lib "_version_string"), (char *)("The " #lib " version formatted as a string")}
45
46 static PyStructSequence_Field app_ffmpeg_info_fields[] = {
47         {(char *)"supported", (char *)("Boolean, True when Blender is built with FFmpeg support")},
48
49         DEF_FFMPEG_LIB_VERSION(avcodec),
50         DEF_FFMPEG_LIB_VERSION(avdevice),
51         DEF_FFMPEG_LIB_VERSION(avformat),
52         DEF_FFMPEG_LIB_VERSION(avutil),
53         DEF_FFMPEG_LIB_VERSION(swscale),
54         {NULL}
55 };
56
57 #undef DEF_FFMPEG_LIB_VERSION
58
59 static PyStructSequence_Desc app_ffmpeg_info_desc = {
60         (char *)"bpy.app.ffmpeg",     /* name */
61         (char *)"This module contains information about FFmpeg blender is linked against",    /* doc */
62         app_ffmpeg_info_fields,    /* fields */
63         (sizeof(app_ffmpeg_info_fields) / sizeof(PyStructSequence_Field)) - 1
64 };
65
66 static PyObject *make_ffmpeg_info(void)
67 {
68         PyObject *ffmpeg_info;
69         int pos = 0;
70
71 #ifdef WITH_FFMPEG
72         int curversion;
73 #endif
74
75         ffmpeg_info = PyStructSequence_New(&BlenderAppFFmpegType);
76         if (ffmpeg_info == NULL) {
77                 return NULL;
78         }
79
80 #if 0 // UNUSED
81 #define SetIntItem(flag) \
82         PyStructSequence_SET_ITEM(ffmpeg_info, pos++, PyLong_FromLong(flag))
83 #endif
84 #ifndef WITH_FFMPEG
85 #define SetStrItem(str) \
86         PyStructSequence_SET_ITEM(ffmpeg_info, pos++, PyUnicode_FromString(str))
87 #endif
88 #define SetObjItem(obj) \
89         PyStructSequence_SET_ITEM(ffmpeg_info, pos++, obj)
90
91 #ifdef WITH_FFMPEG
92 #  define FFMPEG_LIB_VERSION(lib)  { \
93                 curversion = lib ## _version(); \
94                 SetObjItem(Py_BuildValue("(iii)", \
95                                          curversion >> 16, (curversion >> 8) % 256, curversion % 256)); \
96                 SetObjItem(PyUnicode_FromFormat("%2d, %2d, %2d", \
97                                                 curversion >> 16, (curversion >> 8) % 256, curversion % 256)); \
98 } (void)0
99 #else
100 #  define FFMPEG_LIB_VERSION(lib)  { \
101                 SetStrItem("Unknown"); \
102                 SetStrItem("Unknown"); \
103 } (void)0
104 #endif
105
106 #ifdef WITH_FFMPEG
107         SetObjItem(PyBool_FromLong(1));
108 #else
109         SetObjItem(PyBool_FromLong(0));
110 #endif
111
112         FFMPEG_LIB_VERSION(avcodec);
113         FFMPEG_LIB_VERSION(avdevice);
114         FFMPEG_LIB_VERSION(avformat);
115         FFMPEG_LIB_VERSION(avutil);
116         FFMPEG_LIB_VERSION(swscale);
117
118 #undef FFMPEG_LIB_VERSION
119
120         if (PyErr_Occurred()) {
121                 Py_CLEAR(ffmpeg_info);
122                 return NULL;
123         }
124
125 // #undef SetIntItem
126 #undef SetStrItem
127 #undef SetObjItem
128
129         return ffmpeg_info;
130 }
131
132 PyObject *BPY_app_ffmpeg_struct(void)
133 {
134         PyObject *ret;
135
136         PyStructSequence_InitType(&BlenderAppFFmpegType, &app_ffmpeg_info_desc);
137
138         ret = make_ffmpeg_info();
139
140         /* prevent user from creating new instances */
141         BlenderAppFFmpegType.tp_init = NULL;
142         BlenderAppFFmpegType.tp_new = NULL;
143         BlenderAppFFmpegType.tp_hash = (hashfunc)_Py_HashPointer; /* without this we can't do set(sys.modules) [#29635] */
144
145         return ret;
146 }