2 Libavcodec integration for Darkplaces by Timofeyev Pavel
\r
4 This program is free software; you can redistribute it and/or
\r
5 modify it under the terms of the GNU General Public License
\r
6 as published by the Free Software Foundation; either version 2
\r
7 of the License, or (at your option) any later version.
\r
9 This program is distributed in the hope that it will be useful,
\r
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
\r
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
\r
13 See the GNU General Public License for more details.
\r
15 You should have received a copy of the GNU General Public License
\r
16 along with this program; if not, write to:
\r
18 Free Software Foundation, Inc.
\r
19 59 Temple Place - Suite 330
\r
20 Boston, MA 02111-1307, USA
\r
32 #define LIBAVW_SCALER_BILINEAR 0
\r
33 #define LIBAVW_SCALER_BICUBIC 1
\r
34 #define LIBAVW_SCALER_X 2
\r
35 #define LIBAVW_SCALER_POINT 3
\r
36 #define LIBAVW_SCALER_AREA 4
\r
37 #define LIBAVW_SCALER_BICUBLIN 5
\r
38 #define LIBAVW_SCALER_GAUSS 6
\r
39 #define LIBAVW_SCALER_SINC 7
\r
40 #define LIBAVW_SCALER_LANCZOS 8
\r
41 #define LIBAVW_SCALER_SPLINE 9
\r
43 #define LIBAVW_PIXEL_FORMAT_BGR 0
\r
44 #define LIBAVW_PIXEL_FORMAT_BGRA 1
\r
46 #define LIBAVW_PRINT_WARNING 1
\r
47 #define LIBAVW_PRINT_ERROR 2
\r
48 #define LIBAVW_PRINT_FATAL 3
\r
49 #define LIBAVW_PRINT_PANIC 4
\r
50 // exported callback functions:
\r
51 typedef void avwCallbackPrint(int, const char *);
\r
52 typedef int avwCallbackIoRead(void *, uint8_t *, int);
\r
53 typedef int64_t avwCallbackIoSeek(void *, int64_t, int);
\r
54 typedef int64_t avwCallbackIoSeekSize(void *);
\r
55 // exported functions:
\r
56 int (*qLibAvW_Init)(avwCallbackPrint *printfunction); // init library, returns error code
\r
57 const char *(*qLibAvW_ErrorString)(int errorcode); // get string for error code
\r
58 const char *(*qLibAvW_AvcVersion)(void); // get a string containing libavcodec version wrapper was built for
\r
59 float (*qLibAvW_Version)(void); // get wrapper version
\r
60 int (*qLibAvW_CreateStream)(void **stream); // create stream, returns error code
\r
61 void (*qLibAvW_RemoveStream)(void *stream); // flush and remove stream
\r
62 int (*qLibAvW_StreamGetVideoWidth)(void *stream); // get video parameters of stream
\r
63 int (*qLibAvW_StreamGetVideoHeight)(void *stream);
\r
64 double (*qLibAvW_StreamGetFramerate)(void *stream);
\r
65 int (*qLibAvW_StreamGetError)(void *stream); // get last function errorcode from stream
\r
66 // simple API to play video
\r
67 int (*qLibAvW_PlayVideo)(void *stream, void *file, avwCallbackIoRead *IoRead, avwCallbackIoSeek *IoSeek, avwCallbackIoSeekSize *IoSeekSize);
\r
68 int (*qLibAvW_PlaySeekNextFrame)(void *stream);
\r
69 int (*qLibAvW_PlayGetFrameImage)(void *stream, int pixel_format, void *imagedata, int imagewidth, int imageheight, int scaler);
\r
71 static dllfunction_t libavwfuncs[] =
\r
73 {"LibAvW_Init", (void **) &qLibAvW_Init },
\r
74 {"LibAvW_ErrorString", (void **) &qLibAvW_ErrorString },
\r
75 {"LibAvW_AvcVersion", (void **) &qLibAvW_AvcVersion },
\r
76 {"LibAvW_Version", (void **) &qLibAvW_Version },
\r
77 {"LibAvW_CreateStream", (void **) &qLibAvW_CreateStream },
\r
78 {"LibAvW_RemoveStream", (void **) &qLibAvW_RemoveStream },
\r
79 {"LibAvW_StreamGetVideoWidth", (void **) &qLibAvW_StreamGetVideoWidth },
\r
80 {"LibAvW_StreamGetVideoHeight",(void **) &qLibAvW_StreamGetVideoHeight },
\r
81 {"LibAvW_StreamGetFramerate", (void **) &qLibAvW_StreamGetFramerate },
\r
82 {"LibAvW_StreamGetError", (void **) &qLibAvW_StreamGetError },
\r
83 {"LibAvW_PlayVideo", (void **) &qLibAvW_PlayVideo },
\r
84 {"LibAvW_PlaySeekNextFrame", (void **) &qLibAvW_PlaySeekNextFrame },
\r
85 {"LibAvW_PlayGetFrameImage", (void **) &qLibAvW_PlayGetFrameImage },
\r
89 const char* dllnames_libavw[] =
\r
93 #elif defined(MACOSX)
\r
102 static dllhandle_t libavw_dll = NULL;
\r
105 typedef struct libavwstream_s
\r
108 double info_framerate;
\r
109 unsigned int info_imagewidth;
\r
110 unsigned int info_imageheight;
\r
111 double info_aspectratio;
\r
114 // channel the sound file is being played on
\r
121 cvar_t cl_video_libavw_minwidth = {CVAR_SAVE, "cl_video_libavw_minwidth", "0", "if videos width is lesser than minimal, thay will be upscaled"};
\r
122 cvar_t cl_video_libavw_minheight = {CVAR_SAVE, "cl_video_libavw_minheight", "0", "if videos height is lesser than minimal, thay will be upscaled"};
\r
123 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."};
\r
125 // video extensions
\r
126 const char* libavw_extensions[] =
\r
145 =================================================================
\r
148 a features that is not supported yet and likely to be done
\r
149 - streaming audio from videofiles
\r
150 - streaming subtitles
\r
152 =================================================================
\r
155 unsigned int libavw_getwidth(void *stream);
\r
156 unsigned int libavw_getheight(void *stream);
\r
157 double libavw_getframerate(void *stream);
\r
158 double libavw_getaspectratio(void *stream);
\r
159 void libavw_close(void *stream);
\r
161 int libavw_decodeframe(void *stream, void *imagedata, unsigned int Rmask, unsigned int Gmask, unsigned int Bmask, unsigned int bytesperpixel, int imagebytesperrow)
\r
163 int pixel_format = LIBAVW_PIXEL_FORMAT_BGR;
\r
166 libavwstream_t *s = (libavwstream_t *)stream;
\r
169 if (!s->sndstarted)
\r
171 if (s->sfx != NULL)
\r
172 s->sndchan = S_StartSound(-1, 0, s->sfx, vec3_origin, 1.0f, 0);
\r
177 if (!qLibAvW_PlaySeekNextFrame(s->stream))
\r
179 // got error or file end
\r
180 errorcode = qLibAvW_StreamGetError(s->stream);
\r
182 Con_Printf("LibAvW: %s\n", qLibAvW_ErrorString(errorcode));
\r
186 // decode into bgr texture
\r
187 if (bytesperpixel == 4)
\r
188 pixel_format = LIBAVW_PIXEL_FORMAT_BGRA;
\r
189 else if (bytesperpixel == 3)
\r
190 pixel_format = LIBAVW_PIXEL_FORMAT_BGR;
\r
193 Con_Printf("LibAvW: cannot determine pixel format for bpp %i\n", bytesperpixel);
\r
196 if (!qLibAvW_PlayGetFrameImage(s->stream, pixel_format, imagedata, s->info_imagewidth, s->info_imageheight, min(9, max(0, cl_video_libavw_scaler.integer))))
\r
197 Con_Printf("LibAvW: %s\n", qLibAvW_ErrorString(qLibAvW_StreamGetError(s->stream)));
\r
202 unsigned int libavw_getwidth(void *stream)
\r
204 return ((libavwstream_t *)stream)->info_imagewidth;
\r
207 unsigned int libavw_getheight(void *stream)
\r
209 return ((libavwstream_t *)stream)->info_imageheight;
\r
212 double libavw_getframerate(void *stream)
\r
214 return ((libavwstream_t *)stream)->info_framerate;
\r
217 double libavw_getaspectratio(void *stream)
\r
219 return ((libavwstream_t *)stream)->info_aspectratio;
\r
223 void libavw_close(void *stream)
\r
225 libavwstream_t *s = (libavwstream_t *)stream;
\r
228 qLibAvW_RemoveStream(s->stream);
\r
233 if (s->sndchan >= 0)
\r
234 S_StopChannel(s->sndchan, true, true);
\r
239 int LibAvW_FS_Read(void *opaque, uint8_t *buf, int buf_size)
\r
241 return FS_Read((qfile_t *)opaque, buf, buf_size);
\r
243 int64_t LibAvW_FS_Seek(void *opaque, int64_t pos, int whence)
\r
245 return (int64_t)FS_Seek((qfile_t *)opaque, pos, whence);
\r
247 int64_t LibAvW_FS_SeekSize(void *opaque)
\r
249 return (int64_t)FS_FileSize((qfile_t *)opaque);
\r
252 // open as DP video stream
\r
253 void *LibAvW_OpenVideo(clvideo_t *video, char *filename, const char **errorstring)
\r
256 char filebase[MAX_OSPATH], check[MAX_OSPATH];
\r
266 s = (libavwstream_t *)Z_Malloc(sizeof(libavwstream_t));
\r
268 memset(s, 0, sizeof(libavwstream_t));
\r
271 *errorstring = "unable to allocate memory for stream info structure";
\r
276 s->file = FS_OpenVirtualFile(filename, true);
\r
279 FS_StripExtension(filename, filebase, sizeof(filebase));
\r
280 // we tried .dpv, try another extensions
\r
281 for (i = 0; libavw_extensions[i] != NULL; i++)
\r
283 dpsnprintf(check, sizeof(check), "%s.%s", filebase, libavw_extensions[i]);
\r
284 s->file = FS_OpenVirtualFile(check, true);
\r
290 *errorstring = "unable to open videofile";
\r
297 // allocate libavw stream
\r
298 if ((errorcode = qLibAvW_CreateStream(&s->stream)))
\r
300 *errorstring = qLibAvW_ErrorString(errorcode);
\r
306 // open video for playing
\r
307 if (!qLibAvW_PlayVideo(s->stream, s->file, &LibAvW_FS_Read, &LibAvW_FS_Seek, &LibAvW_FS_SeekSize))
\r
309 *errorstring = qLibAvW_ErrorString(qLibAvW_StreamGetError(s->stream));
\r
315 // all right, start codec
\r
316 s->info_imagewidth = qLibAvW_StreamGetVideoWidth(s->stream);
\r
317 s->info_imageheight = qLibAvW_StreamGetVideoHeight(s->stream);
\r
318 s->info_framerate = qLibAvW_StreamGetFramerate(s->stream);
\r
319 s->info_aspectratio = (double)s->info_imagewidth / (double)s->info_imageheight;
\r
320 video->close = libavw_close;
\r
321 video->getwidth = libavw_getwidth;
\r
322 video->getheight = libavw_getheight;
\r
323 video->getframerate = libavw_getframerate;
\r
324 video->decodeframe = libavw_decodeframe;
\r
325 video->getaspectratio = libavw_getaspectratio;
\r
327 // apply min-width, min-height, keep aspect rate
\r
328 if (cl_video_libavw_minwidth.integer > 0)
\r
329 s->info_imagewidth = max(s->info_imagewidth, (unsigned int)cl_video_libavw_minwidth.integer);
\r
330 if (cl_video_libavw_minheight.integer > 0)
\r
331 s->info_imageheight = max(s->info_imageheight, (unsigned int)cl_video_libavw_minheight.integer);
\r
333 // provide sound in separate .wav
\r
334 len = strlen(filename) + 10;
\r
335 wavename = (char *)Z_Malloc(len);
\r
338 FS_StripExtension(filename, wavename, len-1);
\r
339 strlcat(wavename, ".wav", len);
\r
340 s->sfx = S_PrecacheSound(wavename, false, false);
\r
347 void libavw_message(int level, const char *message)
\r
349 if (level == LIBAVW_PRINT_WARNING)
\r
350 Con_Printf("LibAvcodec warning: %s\n", message);
\r
351 else if (level == LIBAVW_PRINT_ERROR)
\r
352 Con_Printf("LibAvcodec error: %s\n", message);
\r
353 else if (level == LIBAVW_PRINT_FATAL)
\r
354 Con_Printf("LibAvcodec fatal error: %s\n", message);
\r
356 Con_Printf("LibAvcodec panic: %s\n", message);
\r
359 qboolean LibAvW_OpenLibrary(void)
\r
363 // COMMANDLINEOPTION: Video: -nolibavw disables libavcodec wrapper support
\r
364 if (COM_CheckParm("-nolibavw"))
\r
368 Sys_LoadLibrary(dllnames_libavw, &libavw_dll, libavwfuncs);
\r
372 // initialize libav wrapper
\r
373 if ((errorcode = qLibAvW_Init(&libavw_message)))
\r
375 Con_Printf("LibAvW failed to initialize: %s\n", qLibAvW_ErrorString(errorcode));
\r
376 Sys_UnloadLibrary(&libavw_dll);
\r
379 Cvar_RegisterVariable(&cl_video_libavw_minwidth);
\r
380 Cvar_RegisterVariable(&cl_video_libavw_minheight);
\r
381 Cvar_RegisterVariable(&cl_video_libavw_scaler);
\r
386 void LibAvW_CloseLibrary(void)
\r
388 Sys_UnloadLibrary(&libavw_dll);
\r