svn merge ^/trunk/blender -r42920:42927
[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 #include "BLI_callbacks.h"
30
31 #include "RNA_types.h"
32 #include "RNA_access.h"
33 #include "bpy_rna.h"
34
35 #ifdef WITH_FFMPEG
36 #include <libavcodec/avcodec.h>
37 #include <libavdevice/avdevice.h>
38 #include <libavformat/avformat.h>
39 #include <libavutil/avutil.h>
40 #include <libswscale/swscale.h>
41 #endif
42
43 static PyTypeObject BlenderAppFFmpegType;
44
45 #define DEF_FFMPEG_LIB_VERSION(lib) \
46         {(char *)(#lib "_version"), (char *)("The " #lib " version  as a tuple of 3 numbers")}, \
47         {(char *)(#lib "_version_string"), (char *)("The " #lib " version formatted as a string")},
48
49 static PyStructSequence_Field app_ffmpeg_info_fields[] = {
50         {(char *)"supported", (char *)("Boolean, True when Blender is built with FFmpeg support")},
51
52         DEF_FFMPEG_LIB_VERSION(avcodec)
53         DEF_FFMPEG_LIB_VERSION(avdevice)
54         DEF_FFMPEG_LIB_VERSION(avformat)
55         DEF_FFMPEG_LIB_VERSION(avutil)
56         DEF_FFMPEG_LIB_VERSION(swscale)
57         {NULL}
58 };
59
60 #undef DEF_FFMPEG_LIB_VERSION
61
62 static PyStructSequence_Desc app_ffmpeg_info_desc = {
63         (char *)"bpy.app.ffmpeg",     /* name */
64         (char *)"This module contains information about FFmpeg blender is linked against",    /* doc */
65         app_ffmpeg_info_fields,    /* fields */
66         (sizeof(app_ffmpeg_info_fields) / sizeof(PyStructSequence_Field)) - 1
67 };
68
69 static PyObject *make_ffmpeg_info(void)
70 {
71         PyObject *ffmpeg_info;
72         int pos = 0, curversion;
73
74         ffmpeg_info = PyStructSequence_New(&BlenderAppFFmpegType);
75         if (ffmpeg_info == NULL) {
76                 return NULL;
77         }
78
79 #define SetIntItem(flag) \
80         PyStructSequence_SET_ITEM(ffmpeg_info, pos++, PyLong_FromLong(flag))
81 #define SetStrItem(str) \
82         PyStructSequence_SET_ITEM(ffmpeg_info, pos++, PyUnicode_FromString(str))
83 #define SetObjItem(obj) \
84         PyStructSequence_SET_ITEM(ffmpeg_info, pos++, obj)
85
86 #ifdef WITH_FFMPEG
87         #define FFMPEG_LIB_VERSION(lib) \
88                 curversion = lib ## _version(); \
89                 SetObjItem(Py_BuildValue("(iii)", \
90                                 curversion >> 16, (curversion >> 8) % 256, curversion % 256)); \
91                 SetObjItem(PyUnicode_FromFormat("%2d, %2d, %2d", \
92                                 curversion >> 16, (curversion >> 8) % 256, curversion % 256));
93 #else
94         #define FFMPEG_LIB_VERSION(lib) \
95                 SetStrItem("Unknown"); \
96                 SetStrItem("Unknown");
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()) {
114                 Py_CLEAR(ffmpeg_info);
115                 return NULL;
116         }
117
118 #undef SetIntItem
119 #undef SetStrItem
120 #undef SetObjItem
121
122         return ffmpeg_info;
123 }
124
125 PyObject *BPY_app_ffmpeg_struct(void)
126 {
127         PyObject *ret;
128
129         PyStructSequence_InitType(&BlenderAppFFmpegType, &app_ffmpeg_info_desc);
130
131         ret = make_ffmpeg_info();
132
133         /* prevent user from creating new instances */
134         BlenderAppFFmpegType.tp_init = NULL;
135         BlenderAppFFmpegType.tp_new = NULL;
136         BlenderAppFFmpegType.tp_hash = (hashfunc)_Py_HashPointer; /* without this we can't do set(sys.modules) [#29635] */
137
138         return ret;
139 }