2 Libavcodec integration for Darkplaces by Timofeyev Pavel
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.
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.
13 See the GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to:
18 Free Software Foundation, Inc.
19 59 Temple Place - Suite 330
20 Boston, MA 02111-1307, USA
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.
28 #define LIBAVW_SCALER_BILINEAR 0
29 #define LIBAVW_SCALER_BICUBIC 1
30 #define LIBAVW_SCALER_X 2
31 #define LIBAVW_SCALER_POINT 3
32 #define LIBAVW_SCALER_AREA 4
33 #define LIBAVW_SCALER_BICUBLIN 5
34 #define LIBAVW_SCALER_GAUSS 6
35 #define LIBAVW_SCALER_SINC 7
36 #define LIBAVW_SCALER_LANCZOS 8
37 #define LIBAVW_SCALER_SPLINE 9
39 #define LIBAVW_PIXEL_FORMAT_BGR 0
40 #define LIBAVW_PIXEL_FORMAT_BGRA 1
42 #define LIBAVW_PRINT_WARNING 1
43 #define LIBAVW_PRINT_ERROR 2
44 #define LIBAVW_PRINT_FATAL 3
45 #define LIBAVW_PRINT_PANIC 4
46 // exported callback functions:
47 typedef void avwCallbackPrint(int, const char *);
48 typedef int avwCallbackIoRead(void *, unsigned char *, int);
49 typedef fs_offset_t avwCallbackIoSeek(void *, fs_offset_t, int);
50 typedef fs_offset_t avwCallbackIoSeekSize(void *);
51 // exported functions:
52 int (*qLibAvW_Init)(avwCallbackPrint *printfunction); // init library, returns error code
53 const char *(*qLibAvW_ErrorString)(int errorcode); // get string for error code
54 const char *(*qLibAvW_AvcVersion)(void); // get a string containing libavcodec version wrapper was built for
55 float (*qLibAvW_Version)(void); // get wrapper version
56 int (*qLibAvW_CreateStream)(void **stream); // create stream, returns error code
57 void (*qLibAvW_RemoveStream)(void *stream); // flush and remove stream
58 int (*qLibAvW_StreamGetVideoWidth)(void *stream); // get video parameters of stream
59 int (*qLibAvW_StreamGetVideoHeight)(void *stream);
60 double (*qLibAvW_StreamGetFramerate)(void *stream);
61 int (*qLibAvW_StreamGetError)(void *stream); // get last function errorcode from stream
62 // simple API to play video
63 int (*qLibAvW_PlayVideo)(void *stream, void *file, avwCallbackIoRead *IoRead, avwCallbackIoSeek *IoSeek, avwCallbackIoSeekSize *IoSeekSize);
64 int (*qLibAvW_PlaySeekNextFrame)(void *stream);
65 int (*qLibAvW_PlayGetFrameImage)(void *stream, int pixel_format, void *imagedata, int imagewidth, int imageheight, int scaler);
67 static dllfunction_t libavwfuncs[] =
69 {"LibAvW_Init", (void **) &qLibAvW_Init },
70 {"LibAvW_ErrorString", (void **) &qLibAvW_ErrorString },
71 {"LibAvW_AvcVersion", (void **) &qLibAvW_AvcVersion },
72 {"LibAvW_Version", (void **) &qLibAvW_Version },
73 {"LibAvW_CreateStream", (void **) &qLibAvW_CreateStream },
74 {"LibAvW_RemoveStream", (void **) &qLibAvW_RemoveStream },
75 {"LibAvW_StreamGetVideoWidth", (void **) &qLibAvW_StreamGetVideoWidth },
76 {"LibAvW_StreamGetVideoHeight",(void **) &qLibAvW_StreamGetVideoHeight },
77 {"LibAvW_StreamGetFramerate", (void **) &qLibAvW_StreamGetFramerate },
78 {"LibAvW_StreamGetError", (void **) &qLibAvW_StreamGetError },
79 {"LibAvW_PlayVideo", (void **) &qLibAvW_PlayVideo },
80 {"LibAvW_PlaySeekNextFrame", (void **) &qLibAvW_PlaySeekNextFrame },
81 {"LibAvW_PlayGetFrameImage", (void **) &qLibAvW_PlayGetFrameImage },
85 const char* dllnames_libavw[] =
98 static dllhandle_t libavw_dll = NULL;
101 typedef struct libavwstream_s
104 double info_framerate;
105 unsigned int info_imagewidth;
106 unsigned int info_imageheight;
107 double info_aspectratio;
110 // channel the sound file is being played on
117 cvar_t cl_video_libavw_minwidth = {CVAR_SAVE, "cl_video_libavw_minwidth", "0", "if videos width is lesser than minimal, thay will be upscaled"};
118 cvar_t cl_video_libavw_minheight = {CVAR_SAVE, "cl_video_libavw_minheight", "0", "if videos height is lesser than minimal, thay will be upscaled"};
119 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."};
122 const char* libavw_extensions[] =
141 =================================================================
144 a features that is not supported yet and likely to be done
145 - streaming audio from videofiles
146 - streaming subtitles
148 =================================================================
151 unsigned int libavw_getwidth(void *stream);
152 unsigned int libavw_getheight(void *stream);
153 double libavw_getframerate(void *stream);
154 double libavw_getaspectratio(void *stream);
155 void libavw_close(void *stream);
157 static int libavw_decodeframe(void *stream, void *imagedata, unsigned int Rmask, unsigned int Gmask, unsigned int Bmask, unsigned int bytesperpixel, int imagebytesperrow)
159 int pixel_format = LIBAVW_PIXEL_FORMAT_BGR;
162 libavwstream_t *s = (libavwstream_t *)stream;
168 s->sndchan = S_StartSound(-1, 0, s->sfx, vec3_origin, 1.0f, 0);
173 if (!qLibAvW_PlaySeekNextFrame(s->stream))
175 // got error or file end
176 errorcode = qLibAvW_StreamGetError(s->stream);
178 Con_Printf("LibAvW: %s\n", qLibAvW_ErrorString(errorcode));
182 // decode into bgr texture
183 if (bytesperpixel == 4)
184 pixel_format = LIBAVW_PIXEL_FORMAT_BGRA;
185 else if (bytesperpixel == 3)
186 pixel_format = LIBAVW_PIXEL_FORMAT_BGR;
189 Con_Printf("LibAvW: cannot determine pixel format for bpp %i\n", bytesperpixel);
192 if (!qLibAvW_PlayGetFrameImage(s->stream, pixel_format, imagedata, s->info_imagewidth, s->info_imageheight, min(9, max(0, cl_video_libavw_scaler.integer))))
193 Con_Printf("LibAvW: %s\n", qLibAvW_ErrorString(qLibAvW_StreamGetError(s->stream)));
198 unsigned int libavw_getwidth(void *stream)
200 return ((libavwstream_t *)stream)->info_imagewidth;
203 unsigned int libavw_getheight(void *stream)
205 return ((libavwstream_t *)stream)->info_imageheight;
208 double libavw_getframerate(void *stream)
210 return ((libavwstream_t *)stream)->info_framerate;
213 double libavw_getaspectratio(void *stream)
215 return ((libavwstream_t *)stream)->info_aspectratio;
219 void libavw_close(void *stream)
221 libavwstream_t *s = (libavwstream_t *)stream;
224 qLibAvW_RemoveStream(s->stream);
230 S_StopChannel(s->sndchan, true, true);
235 static int LibAvW_FS_Read(void *opaque, unsigned char *buf, int buf_size)
237 return FS_Read((qfile_t *)opaque, buf, buf_size);
239 static fs_offset_t LibAvW_FS_Seek(void *opaque, fs_offset_t pos, int whence)
241 return (fs_offset_t)FS_Seek((qfile_t *)opaque, pos, whence);
243 static fs_offset_t LibAvW_FS_SeekSize(void *opaque)
245 return (fs_offset_t)FS_FileSize((qfile_t *)opaque);
248 // open as DP video stream
249 static void *LibAvW_OpenVideo(clvideo_t *video, char *filename, const char **errorstring)
252 char filebase[MAX_OSPATH], check[MAX_OSPATH];
262 s = (libavwstream_t *)Z_Malloc(sizeof(libavwstream_t));
265 *errorstring = "unable to allocate memory for stream info structure";
268 memset(s, 0, sizeof(libavwstream_t));
272 s->file = FS_OpenVirtualFile(filename, true);
275 FS_StripExtension(filename, filebase, sizeof(filebase));
276 // we tried .dpv, try another extensions
277 for (i = 0; libavw_extensions[i] != NULL; i++)
279 dpsnprintf(check, sizeof(check), "%s.%s", filebase, libavw_extensions[i]);
280 s->file = FS_OpenVirtualFile(check, true);
286 *errorstring = "unable to open videofile";
293 // allocate libavw stream
294 if ((errorcode = qLibAvW_CreateStream(&s->stream)))
296 *errorstring = qLibAvW_ErrorString(errorcode);
302 // open video for playing
303 if (!qLibAvW_PlayVideo(s->stream, s->file, &LibAvW_FS_Read, &LibAvW_FS_Seek, &LibAvW_FS_SeekSize))
305 *errorstring = qLibAvW_ErrorString(qLibAvW_StreamGetError(s->stream));
311 // all right, start codec
312 s->info_imagewidth = qLibAvW_StreamGetVideoWidth(s->stream);
313 s->info_imageheight = qLibAvW_StreamGetVideoHeight(s->stream);
314 s->info_framerate = qLibAvW_StreamGetFramerate(s->stream);
315 s->info_aspectratio = (double)s->info_imagewidth / (double)s->info_imageheight;
316 video->close = libavw_close;
317 video->getwidth = libavw_getwidth;
318 video->getheight = libavw_getheight;
319 video->getframerate = libavw_getframerate;
320 video->decodeframe = libavw_decodeframe;
321 video->getaspectratio = libavw_getaspectratio;
323 // apply min-width, min-height, keep aspect rate
324 if (cl_video_libavw_minwidth.integer > 0)
325 s->info_imagewidth = max(s->info_imagewidth, (unsigned int)cl_video_libavw_minwidth.integer);
326 if (cl_video_libavw_minheight.integer > 0)
327 s->info_imageheight = max(s->info_imageheight, (unsigned int)cl_video_libavw_minheight.integer);
329 // provide sound in separate .wav
330 len = strlen(filename) + 10;
331 wavename = (char *)Z_Malloc(len);
334 FS_StripExtension(filename, wavename, len-1);
335 strlcat(wavename, ".wav", len);
336 s->sfx = S_PrecacheSound(wavename, false, false);
343 static void libavw_message(int level, const char *message)
345 if (level == LIBAVW_PRINT_WARNING)
346 Con_Printf("LibAvcodec warning: %s\n", message);
347 else if (level == LIBAVW_PRINT_ERROR)
348 Con_Printf("LibAvcodec error: %s\n", message);
349 else if (level == LIBAVW_PRINT_FATAL)
350 Con_Printf("LibAvcodec fatal error: %s\n", message);
352 Con_Printf("LibAvcodec panic: %s\n", message);
355 static qboolean LibAvW_OpenLibrary(void)
359 // COMMANDLINEOPTION: Video: -nolibavw disables libavcodec wrapper support
360 if (COM_CheckParm("-nolibavw"))
364 Sys_LoadLibrary(dllnames_libavw, &libavw_dll, libavwfuncs);
368 // initialize libav wrapper
369 if ((errorcode = qLibAvW_Init(&libavw_message)))
371 Con_Printf("LibAvW failed to initialize: %s\n", qLibAvW_ErrorString(errorcode));
372 Sys_UnloadLibrary(&libavw_dll);
375 Cvar_RegisterVariable(&cl_video_libavw_minwidth);
376 Cvar_RegisterVariable(&cl_video_libavw_minheight);
377 Cvar_RegisterVariable(&cl_video_libavw_scaler);
382 static void LibAvW_CloseLibrary(void)
384 Sys_UnloadLibrary(&libavw_dll);