Debian Codegraph @

cl_video_libavw.c

raw symbols ◀ ▶ SCIP
1 Debian patch touch this file
1 /*
2 Libavcodec integration for Darkplaces by Timofeyev Pavel
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.
12
13 See the GNU General Public License for more details.
14
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to:
17
18 Free Software Foundation, Inc.
19 59 Temple Place - Suite 330
20 Boston, MA 02111-1307, USA
21
22 */
23
24 // LordHavoc: for some reason this is being #include'd rather than treated as its own file...
25 // LordHavoc: adapted to not require stdint.h as this is not available on MSVC++, using unsigned char instead of uint8_t and fs_offset_t instead of int64_t.
26
•27 #ifdef DISABLE_AVW
28
29 static qboolean LibAvW_OpenLibrary(void)
30 {
31 return false;
32 }
33
34 static void *LibAvW_OpenVideo(clvideo_t *video, char *filename, const char **errorstring)
35 {
36 return NULL;
37 }
38
39
40 static void LibAvW_CloseLibrary(void)
41 {
42 }
43
44 #else /* !DISABLE_AVW */
45
46 // scaler type
47 #define LIBAVW_SCALER_BILINEAR 0
48 #define LIBAVW_SCALER_BICUBIC 1
49 #define LIBAVW_SCALER_X 2
50 #define LIBAVW_SCALER_POINT 3
51 #define LIBAVW_SCALER_AREA 4
52 #define LIBAVW_SCALER_BICUBLIN 5
53 #define LIBAVW_SCALER_GAUSS 6
54 #define LIBAVW_SCALER_SINC 7
55 #define LIBAVW_SCALER_LANCZOS 8
56 #define LIBAVW_SCALER_SPLINE 9
57 // output format
58 #define LIBAVW_PIXEL_FORMAT_BGR 0
59 #define LIBAVW_PIXEL_FORMAT_BGRA 1
60 // print levels
61 #define LIBAVW_PRINT_WARNING 1
62 #define LIBAVW_PRINT_ERROR 2
63 #define LIBAVW_PRINT_FATAL 3
64 #define LIBAVW_PRINT_PANIC 4
65 // exported callback functions:
66 typedef void avwCallbackPrint(int, const char *);
67 typedef int avwCallbackIoRead(void *, unsigned char *, int);
68 typedef fs_offset_t avwCallbackIoSeek(void *, fs_offset_t, int);
69 typedef fs_offset_t avwCallbackIoSeekSize(void *);
70 // exported functions:
71 int (*qLibAvW_Init)(avwCallbackPrint *printfunction); // init library, returns error code
72 const char *(*qLibAvW_ErrorString)(int errorcode); // get string for error code
73 const char *(*qLibAvW_AvcVersion)(void); // get a string containing libavcodec version wrapper was built for
74 float (*qLibAvW_Version)(void); // get wrapper version
75 int (*qLibAvW_CreateStream)(void **stream); // create stream, returns error code
76 void (*qLibAvW_RemoveStream)(void *stream); // flush and remove stream
77 int (*qLibAvW_StreamGetVideoWidth)(void *stream); // get video parameters of stream
78 int (*qLibAvW_StreamGetVideoHeight)(void *stream);
79 double (*qLibAvW_StreamGetFramerate)(void *stream);
80 int (*qLibAvW_StreamGetError)(void *stream); // get last function errorcode from stream
81 // simple API to play video
82 int (*qLibAvW_PlayVideo)(void *stream, void *file, avwCallbackIoRead *IoRead, avwCallbackIoSeek *IoSeek, avwCallbackIoSeekSize *IoSeekSize);
83 int (*qLibAvW_PlaySeekNextFrame)(void *stream);
84 int (*qLibAvW_PlayGetFrameImage)(void *stream, int pixel_format, void *imagedata, int imagewidth, int imageheight, int scaler);
85
86 static dllfunction_t libavwfuncs[] =
87 {
88 {"LibAvW_Init", (void **) &qLibAvW_Init },
89 {"LibAvW_ErrorString", (void **) &qLibAvW_ErrorString },
90 {"LibAvW_AvcVersion", (void **) &qLibAvW_AvcVersion },
91 {"LibAvW_Version", (void **) &qLibAvW_Version },
92 {"LibAvW_CreateStream", (void **) &qLibAvW_CreateStream },
93 {"LibAvW_RemoveStream", (void **) &qLibAvW_RemoveStream },
94 {"LibAvW_StreamGetVideoWidth", (void **) &qLibAvW_StreamGetVideoWidth },
95 {"LibAvW_StreamGetVideoHeight",(void **) &qLibAvW_StreamGetVideoHeight },
96 {"LibAvW_StreamGetFramerate", (void **) &qLibAvW_StreamGetFramerate },
97 {"LibAvW_StreamGetError", (void **) &qLibAvW_StreamGetError },
98 {"LibAvW_PlayVideo", (void **) &qLibAvW_PlayVideo },
99 {"LibAvW_PlaySeekNextFrame", (void **) &qLibAvW_PlaySeekNextFrame },
100 {"LibAvW_PlayGetFrameImage", (void **) &qLibAvW_PlayGetFrameImage },
101 {NULL, NULL}
102 };
103
104 const char* dllnames_libavw[] =
105 {
106 #if defined(WIN32)
107 "libavw.dll",
108 #elif defined(MACOSX)
109 "libavw.dylib",
110 #else
111 "libavw.so.1",
112 "libavw.so",
113 #endif
114 NULL
115 };
116
117 static dllhandle_t libavw_dll = NULL;
118
119 // DP videostream
120 typedef struct libavwstream_s
121 {
122 qfile_t *file;
123 double info_framerate;
124 unsigned int info_imagewidth;
125 unsigned int info_imageheight;
126 double info_aspectratio;
127 void *stream;
128
129 // channel the sound file is being played on
130 sfx_t *sfx;
131 int sndchan;
132 int sndstarted;
133 }
134 libavwstream_t;
135
136 cvar_t cl_video_libavw_minwidth = {CVAR_SAVE, "cl_video_libavw_minwidth", "0", "if videos width is lesser than minimal, thay will be upscaled"};
137 cvar_t cl_video_libavw_minheight = {CVAR_SAVE, "cl_video_libavw_minheight", "0", "if videos height is lesser than minimal, thay will be upscaled"};
138 cvar_t cl_video_libavw_scaler = {CVAR_SAVE, "cl_video_libavw_scaler", "1", "selects a scaler for libavcode played videos. Scalers are: 0 - bilinear, 1 - bicubic, 2 - x, 3 - point, 4 - area, 5 - bicublin, 6 - gauss, 7 - sinc, 8 - lanczos, 9 - spline."};
139
140 // video extensions
141 const char* libavw_extensions[] =
142 {
143 "ogv",
144 "avi",
145 "mpg",
146 "mp4",
147 "mkv",
148 "webm",
149 "bik",
150 "roq",
151 "flv",
152 "wmv",
153 "mpeg",
154 "mjpeg",
155 "mpeg4",
156 NULL
157 };
158
159 /*
160 =================================================================
161
162 Video decoding
163 a features that is not supported yet and likely to be done
164 - streaming audio from videofiles
165 - streaming subtitles
166
167 =================================================================
168 */
169
170 unsigned int libavw_getwidth(void *stream);
171 unsigned int libavw_getheight(void *stream);
172 double libavw_getframerate(void *stream);
173 double libavw_getaspectratio(void *stream);
174 void libavw_close(void *stream);
175
176 static int libavw_decodeframe(void *stream, void *imagedata, unsigned int Rmask, unsigned int Gmask, unsigned int Bmask, unsigned int bytesperpixel, int imagebytesperrow)
177 {
178 int pixel_format = LIBAVW_PIXEL_FORMAT_BGR;
179 int errorcode;
180
181 libavwstream_t *s = (libavwstream_t *)stream;
182
183 // start sound
184 if (!s->sndstarted)
185 {
186 if (s->sfx != NULL)
187 s->sndchan = S_StartSound(-1, 0, s->sfx, vec3_origin, 1.0f, 0);
188 s->sndstarted = 1;
189 }
190
191 // read frame
192 if (!qLibAvW_PlaySeekNextFrame(s->stream))
193 {
194 // got error or file end
195 errorcode = qLibAvW_StreamGetError(s->stream);
196 if (errorcode)
197 Con_Printf("LibAvW: %s\n", qLibAvW_ErrorString(errorcode));
198 return 1;
199 }
200
201 // decode into bgr texture
202 if (bytesperpixel == 4)
203 pixel_format = LIBAVW_PIXEL_FORMAT_BGRA;
204 else if (bytesperpixel == 3)
205 pixel_format = LIBAVW_PIXEL_FORMAT_BGR;
206 else
207 {
208 Con_Printf("LibAvW: cannot determine pixel format for bpp %i\n", bytesperpixel);
209 return 1;
210 }
211 if (!qLibAvW_PlayGetFrameImage(s->stream, pixel_format, imagedata, s->info_imagewidth, s->info_imageheight, min(9, max(0, cl_video_libavw_scaler.integer))))
212 Con_Printf("LibAvW: %s\n", qLibAvW_ErrorString(qLibAvW_StreamGetError(s->stream)));
213 return 0;
214 }
215
216 // get stream info
217 unsigned int libavw_getwidth(void *stream)
218 {
219 return ((libavwstream_t *)stream)->info_imagewidth;
220 }
221
222 unsigned int libavw_getheight(void *stream)
223 {
224 return ((libavwstream_t *)stream)->info_imageheight;
225 }
226
227 double libavw_getframerate(void *stream)
228 {
229 return ((libavwstream_t *)stream)->info_framerate;
230 }
231
232 double libavw_getaspectratio(void *stream)
233 {
234 return ((libavwstream_t *)stream)->info_aspectratio;
235 }
236
237 // close stream
238 void libavw_close(void *stream)
239 {
240 libavwstream_t *s = (libavwstream_t *)stream;
241
242 if (s->stream)
243 qLibAvW_RemoveStream(s->stream);
244 s->stream = NULL;
245 if (s->file)
246 FS_Close(s->file);
247 s->file = NULL;
248 if (s->sndchan >= 0)
249 S_StopChannel(s->sndchan, true, true);
250 s->sndchan = -1;
251 }
252
253 // IO wrapper
254 static int LibAvW_FS_Read(void *opaque, unsigned char *buf, int buf_size)
255 {
256 return FS_Read((qfile_t *)opaque, buf, buf_size);
257 }
258 static fs_offset_t LibAvW_FS_Seek(void *opaque, fs_offset_t pos, int whence)
259 {
260 return (fs_offset_t)FS_Seek((qfile_t *)opaque, pos, whence);
261 }
262 static fs_offset_t LibAvW_FS_SeekSize(void *opaque)
263 {
264 return (fs_offset_t)FS_FileSize((qfile_t *)opaque);
265 }
266
267 // open as DP video stream
268 static void *LibAvW_OpenVideo(clvideo_t *video, char *filename, const char **errorstring)
269 {
270 libavwstream_t *s;
271 char filebase[MAX_OSPATH], check[MAX_OSPATH];
272 unsigned int i;
273 int errorcode;
274 char *wavename;
275 size_t len;
276
277 if (!libavw_dll)
278 return NULL;
279
280 // allocate stream
281 s = (libavwstream_t *)Z_Malloc(sizeof(libavwstream_t));
282 if (s == NULL)
283 {
284 *errorstring = "unable to allocate memory for stream info structure";
285 return NULL;
286 }
287 memset(s, 0, sizeof(libavwstream_t));
288 s->sndchan = -1;
289
290 // open file
291 s->file = FS_OpenVirtualFile(filename, true);
292 if (!s->file)
293 {
294 FS_StripExtension(filename, filebase, sizeof(filebase));
295 // we tried .dpv, try another extensions
296 for (i = 0; libavw_extensions[i] != NULL; i++)
297 {
298 dpsnprintf(check, sizeof(check), "%s.%s", filebase, libavw_extensions[i]);
299 s->file = FS_OpenVirtualFile(check, true);
300 if (s->file)
301 break;
302 }
303 if (!s->file)
304 {
305 *errorstring = "unable to open videofile";
306 libavw_close(s);
307 Z_Free(s);
308 return NULL;
309 }
310 }
311
312 // allocate libavw stream
313 if ((errorcode = qLibAvW_CreateStream(&s->stream)))
314 {
315 *errorstring = qLibAvW_ErrorString(errorcode);
316 libavw_close(s);
317 Z_Free(s);
318 return NULL;
319 }
320
321 // open video for playing
322 if (!qLibAvW_PlayVideo(s->stream, s->file, &LibAvW_FS_Read, &LibAvW_FS_Seek, &LibAvW_FS_SeekSize))
323 {
324 *errorstring = qLibAvW_ErrorString(qLibAvW_StreamGetError(s->stream));
325 libavw_close(s);
326 Z_Free(s);
327 return NULL;
328 }
329
330 // all right, start codec
331 s->info_imagewidth = qLibAvW_StreamGetVideoWidth(s->stream);
332 s->info_imageheight = qLibAvW_StreamGetVideoHeight(s->stream);
333 s->info_framerate = qLibAvW_StreamGetFramerate(s->stream);
334 s->info_aspectratio = (double)s->info_imagewidth / (double)s->info_imageheight;
335 video->close = libavw_close;
336 video->getwidth = libavw_getwidth;
337 video->getheight = libavw_getheight;
338 video->getframerate = libavw_getframerate;
339 video->decodeframe = libavw_decodeframe;
340 video->getaspectratio = libavw_getaspectratio;
341
342 // apply min-width, min-height, keep aspect rate
343 if (cl_video_libavw_minwidth.integer > 0)
344 s->info_imagewidth = max(s->info_imagewidth, (unsigned int)cl_video_libavw_minwidth.integer);
345 if (cl_video_libavw_minheight.integer > 0)
346 s->info_imageheight = max(s->info_imageheight, (unsigned int)cl_video_libavw_minheight.integer);
347
348 // provide sound in separate .wav
349 len = strlen(filename) + 10;
350 wavename = (char *)Z_Malloc(len);
351 if (wavename)
352 {
353 FS_StripExtension(filename, wavename, len-1);
354 strlcat(wavename, ".wav", len);
355 s->sfx = S_PrecacheSound(wavename, false, false);
356 s->sndchan = -1;
357 Z_Free(wavename);
358 }
359 return s;
360 }
361
362 static void libavw_message(int level, const char *message)
363 {
364 if (level == LIBAVW_PRINT_WARNING)
365 Con_Printf("LibAvcodec warning: %s\n", message);
366 else if (level == LIBAVW_PRINT_ERROR)
367 Con_Printf("LibAvcodec error: %s\n", message);
368 else if (level == LIBAVW_PRINT_FATAL)
369 Con_Printf("LibAvcodec fatal error: %s\n", message);
370 else
371 Con_Printf("LibAvcodec panic: %s\n", message);
372 }
373
374 static qboolean LibAvW_OpenLibrary(void)
375 {
376 int errorcode;
377
378 // COMMANDLINEOPTION: Video: -nolibavw disables libavcodec wrapper support
379 if (COM_CheckParm("-nolibavw"))
380 return false;
381
382 // load DLL's
383 Sys_LoadLibrary(dllnames_libavw, &libavw_dll, libavwfuncs);
384 if (!libavw_dll)
385 return false;
386
387 // initialize libav wrapper
388 if ((errorcode = qLibAvW_Init(&libavw_message)))
389 {
390 Con_Printf("LibAvW failed to initialize: %s\n", qLibAvW_ErrorString(errorcode));
391 Sys_UnloadLibrary(&libavw_dll);
392 }
393
394 Cvar_RegisterVariable(&cl_video_libavw_minwidth);
395 Cvar_RegisterVariable(&cl_video_libavw_minheight);
396 Cvar_RegisterVariable(&cl_video_libavw_scaler);
397
398 return true;
399 }
400
401 static void LibAvW_CloseLibrary(void)
402 {
403 Sys_UnloadLibrary(&libavw_dll);
404 }
405
•406 #endif /* !DISABLE_AVW */