WIP loading bmesh in trunk, some conversion functions for this purpose.
[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;
73
74 #ifdef WITH_FFMPEG
75         int curversion;
76 #endif
77
78         ffmpeg_info = PyStructSequence_New(&BlenderAppFFmpegType);
79         if (ffmpeg_info == NULL) {
80                 return NULL;
81         }
82
83 #define SetIntItem(flag) \
84         PyStructSequence_SET_ITEM(ffmpeg_info, pos++, PyLong_FromLong(flag))
85 #define SetStrItem(str) \
86         PyStructSequence_SET_ITEM(ffmpeg_info, pos++, PyUnicode_FromString(str))
87 #define SetObjItem(obj) \
88         PyStructSequence_SET_ITEM(ffmpeg_info, pos++, obj)
89
90 #ifdef WITH_FFMPEG
91         #define FFMPEG_LIB_VERSION(lib) \
92                 curversion = lib ## _version(); \
93                 SetObjItem(Py_BuildValue("(iii)", \
94                                 curversion >> 16, (curversion >> 8) % 256, curversion % 256)); \
95                 SetObjItem(PyUnicode_FromFormat("%2d, %2d, %2d", \
96                                 curversion >> 16, (curversion >> 8) % 256, curversion % 256));
97 #else
98         #define FFMPEG_LIB_VERSION(lib) \
99                 SetStrItem("Unknown"); \
100                 SetStrItem("Unknown");
101 #endif
102
103 #ifdef WITH_FFMPEG
104         SetObjItem(PyBool_FromLong(1));
105 #else
106         SetObjItem(PyBool_FromLong(0));
107 #endif
108
109         FFMPEG_LIB_VERSION(avcodec);
110         FFMPEG_LIB_VERSION(avdevice);
111         FFMPEG_LIB_VERSION(avformat);
112         FFMPEG_LIB_VERSION(avutil);
113         FFMPEG_LIB_VERSION(swscale);
114
115 #undef FFMPEG_LIB_VERSION
116
117         if (PyErr_Occurred()) {
118                 Py_CLEAR(ffmpeg_info);
119                 return NULL;
120         }
121
122 #undef SetIntItem
123 #undef SetStrItem
124 #undef SetObjItem
125
126         return ffmpeg_info;
127 }
128
129 PyObject *BPY_app_ffmpeg_struct(void)
130 {
131         PyObject *ret;
132
133         PyStructSequence_InitType(&BlenderAppFFmpegType, &app_ffmpeg_info_desc);
134
135         ret = make_ffmpeg_info();
136
137         /* prevent user from creating new instances */
138         BlenderAppFFmpegType.tp_init = NULL;
139         BlenderAppFFmpegType.tp_new = NULL;
140         BlenderAppFFmpegType.tp_hash = (hashfunc)_Py_HashPointer; /* without this we can't do set(sys.modules) [#29635] */
141
142         return ret;
143 }