smpeg.pas 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435
  1. unit smpeg;
  2. {******************************************************************************}
  3. {
  4. $Id: smpeg.pas,v 1.7 2004/08/14 22:54:30 savage Exp $
  5. }
  6. { }
  7. { Borland Delphi SMPEG - SDL MPEG Player Library }
  8. { Conversion of the SMPEG - SDL MPEG Player Library }
  9. { }
  10. { Portions created by Sam Lantinga <[email protected]> are }
  11. { Copyright (C) 1997, 1998, 1999, 2000, 2001 Sam Lantinga }
  12. { 5635-34 Springhouse Dr. }
  13. { Pleasanton, CA 94588 (USA) }
  14. { }
  15. { All Rights Reserved. }
  16. { }
  17. { The original files are : smpeg.h }
  18. { }
  19. { The initial developer of this Pascal code was : }
  20. { Matthias Thoma <[email protected]> }
  21. { }
  22. { Portions created by Matthias Thoma are }
  23. { Copyright (C) 2000 - 2001 Matthias Thoma. }
  24. { }
  25. { }
  26. { Contributor(s) }
  27. { -------------- }
  28. { Tom Jones <[email protected]> His Project inspired this conversion }
  29. { Matthias Thoma <[email protected]> }
  30. { }
  31. { Obtained through: }
  32. { Joint Endeavour of Delphi Innovators ( Project JEDI ) }
  33. { }
  34. { You may retrieve the latest version of this file at the Project }
  35. { JEDI home page, located at http://delphi-jedi.org }
  36. { }
  37. { The contents of this file are used with permission, subject to }
  38. { the Mozilla Public License Version 1.1 (the "License"); you may }
  39. { not use this file except in compliance with the License. You may }
  40. { obtain a copy of the License at }
  41. { http://www.mozilla.org/MPL/MPL-1.1.html }
  42. { }
  43. { Software distributed under the License is distributed on an }
  44. { "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or }
  45. { implied. See the License for the specific language governing }
  46. { rights and limitations under the License. }
  47. { }
  48. { Description }
  49. { ----------- }
  50. { }
  51. { }
  52. { }
  53. { }
  54. { }
  55. { }
  56. { }
  57. { Requires }
  58. { -------- }
  59. { The SDL Runtime libraris on Win32 : SDL.dll on Linux : libSDL-1.2.so.0 }
  60. { They are available from... }
  61. { http://www.libsdl.org . }
  62. { }
  63. { Programming Notes }
  64. { ----------------- }
  65. { }
  66. { }
  67. { }
  68. { }
  69. { Revision History }
  70. { ---------------- }
  71. { May 08 2001 - MT : Initial conversion }
  72. { }
  73. { October 12 2001 - DA : Various changes as suggested by David Acklam }
  74. { }
  75. { April 03 2003 - DL : Added jedi-sdl.inc include file to support more }
  76. { Pascal compilers. Initial support is now included }
  77. { for GnuPascal, VirtualPascal, TMT and obviously }
  78. { continue support for Delphi Kylix and FreePascal. }
  79. { }
  80. { April 08 2003 - MK : Aka Mr Kroket - Added Better FPC support }
  81. { Fixed all invalid calls to DLL. }
  82. { Changed constant names to: }
  83. { const }
  84. { STATUS_SMPEG_ERROR = -1; }
  85. { STATUS_SMPEG_STOPPED = 0; }
  86. { STATUS_SMPEG_PLAYING = 1; }
  87. { because SMPEG_ERROR is a function (_SMPEG_error }
  88. { isn't correct), and cannot be two elements with the }
  89. { same name }
  90. { }
  91. { April 24 2003 - DL : under instruction from Alexey Barkovoy, I have added}
  92. { better TMT Pascal support and under instruction }
  93. { from Prof. Abimbola Olowofoyeku (The African Chief),}
  94. { I have added better Gnu Pascal support }
  95. { }
  96. { April 30 2003 - DL : under instruction from David Mears AKA }
  97. { Jason Siletto, I have added FPC Linux support. }
  98. { This was compiled with fpc 1.1, so remember to set }
  99. { include file path. ie. -Fi/usr/share/fpcsrc/rtl/* }
  100. { }
  101. {
  102. $Log: smpeg.pas,v $
  103. Revision 1.7 2004/08/14 22:54:30 savage
  104. Updated so that Library name defines are correctly defined for MacOS X.
  105. Revision 1.6 2004/05/10 14:10:04 savage
  106. Initial MacOS X support. Fixed defines for MACOS ( Classic ) and DARWIN ( MacOS X ).
  107. Revision 1.5 2004/04/13 09:32:08 savage
  108. Changed Shared object names back to just the .so extension to avoid conflicts on various Linux/Unix distros. Therefore developers will need to create Symbolic links to the actual Share Objects if necessary.
  109. Revision 1.4 2004/04/02 10:40:55 savage
  110. Changed Linux Shared Object name so they reflect the Symbolic Links that are created when installing the RPMs from the SDL site.
  111. Revision 1.3 2004/03/31 22:20:02 savage
  112. Windows unit not used in this file, so it was removed to keep the code tidy.
  113. Revision 1.2 2004/03/30 20:23:28 savage
  114. Tidied up use of UNIX compiler directive.
  115. Revision 1.1 2004/02/14 23:35:42 savage
  116. version 1 of sdl_image, sdl_mixer and smpeg.
  117. }
  118. {******************************************************************************}
  119. {$I jedi-sdl.inc}
  120. interface
  121. uses
  122. {$IFDEF __GPC__}
  123. gpc,
  124. {$ENDIF}
  125. sdl;
  126. const
  127. {$IFDEF windows}
  128. SmpegLibName = 'smpeg.dll';
  129. {$ENDIF}
  130. {$IFDEF UNIX}
  131. {$IFDEF DARWIN}
  132. SmpegLibName = 'libsmpeg.dylib';
  133. {$ELSE}
  134. SmpegLibName = 'libsmpeg.so';
  135. {$ENDIF}
  136. {$ENDIF}
  137. {$IFDEF MACOS}
  138. SmpegLibName = 'smpeg';
  139. {$ENDIF}
  140. //------------------------------------------------------------------------------
  141. // MPEGFilter.h
  142. //------------------------------------------------------------------------------
  143. { SMPEG filter info flags }
  144. const
  145. SMPEG_FILTER_INFO_MB_ERROR = 1;
  146. SMPEG_FILTER_INFO_PIXEL_ERROR = 2;
  147. { Filter info from SMPEG }
  148. type
  149. SMPEG_FilterInfo = record
  150. yuv_mb_square_error: PUint16;
  151. yuv_pixel_square_error: PUint16;
  152. end;
  153. TSMPEG_FilterInfo = SMPEG_FilterInfo;
  154. PSMPEG_FilterInfo = ^SMPEG_FilterInfo;
  155. { MPEG filter definition }
  156. PSMPEG_Filter = ^TSMPEG_Filter;
  157. { Callback functions for the filter }
  158. {$IFNDEF __GPC__}
  159. TSMPEG_FilterCallback = function( dest, source: PSDL_Overlay; region: PSDL_Rect; filter_info: PSMPEG_FilterInfo; data: Pointer ): Pointer; cdecl;
  160. {$ELSE}
  161. TSMPEG_FilterCallback = function( dest, source: PSDL_Overlay; region: PSDL_Rect; filter_info: PSMPEG_FilterInfo; data: Pointer ): Pointer;
  162. {$ENDIF}
  163. {$IFNDEF __GPC__}
  164. TSMPEG_FilterDestroy = function( Filter: PSMPEG_Filter ): Pointer; cdecl;
  165. {$ELSE}
  166. TSMPEG_FilterDestroy = function( Filter: PSMPEG_Filter ): Pointer;
  167. {$ENDIF}
  168. { The filter definition itself }
  169. TSMPEG_Filter = record
  170. flags: Uint32;
  171. data: Pointer;
  172. callback: TSMPEG_FilterCallback;
  173. destroy: TSMPEG_FilterDestroy;
  174. end;
  175. { The null filter (default). It simply copies the source rectangle to the video overlay. }
  176. function SMPEGfilter_null: PSMPEG_Filter;
  177. cdecl; external {$IFDEF __GPC__}name 'SMPEGfilter_null'{$ELSE} SmpegLibName{$ENDIF __GPC__};
  178. { The bilinear filter. A basic low-pass filter that will produce a smoother image. }
  179. function SMPEGfilter_bilinear: PSMPEG_Filter;
  180. cdecl; external {$IFDEF __GPC__}name 'SMPEGfilter_bilinear'{$ELSE} SmpegLibName{$ENDIF __GPC__};
  181. { The deblocking filter. It filters block borders and non-intra coded blocks to reduce blockiness }
  182. function SMPEGfilter_deblocking: PSMPEG_Filter;
  183. cdecl; external {$IFDEF __GPC__}name 'SMPEGfilter_deblocking'{$ELSE} SmpegLibName{$ENDIF __GPC__};
  184. //------------------------------------------------------------------------------
  185. // SMPEG.h
  186. //------------------------------------------------------------------------------
  187. const
  188. SMPEG_MAJOR_VERSION = 0;
  189. SMPEG_MINOR_VERSION = 4;
  190. SMPEG_PATCHLEVEL = 2;
  191. type
  192. SMPEG_version = record
  193. major: UInt8;
  194. minor: UInt8;
  195. patch: UInt8;
  196. end;
  197. TSMPEG_version = SMPEG_version;
  198. PSMPEG_version = ^TSMPEG_version;
  199. // This is the actual SMPEG object
  200. _SMPEG = record
  201. //obj: PMPEG;
  202. end;
  203. TSMPEG = _SMPEG;
  204. PSMPEG = ^_SMPEG;
  205. { Used to get information about the SMPEG object }
  206. __SMPEG_Info = record
  207. has_audio: Integer;
  208. has_video: Integer;
  209. width: Integer;
  210. height: Integer;
  211. current_frame: Integer;
  212. current_fps: double;
  213. audio_string: array[0..79] of char;
  214. audio_current_frame: Integer;
  215. current_offset: UInt32;
  216. total_size: UInt32;
  217. current_time: double;
  218. total_time: double;
  219. end;
  220. _SMPEG_Info = __SMPEG_Info;
  221. SMPEG_Info = _SMPEG_Info;
  222. TSMPEG_Info = _SMPEG_Info;
  223. PSMPEG_Info = ^_SMPEG_Info;
  224. { Possible MPEG status codes }
  225. const
  226. STATUS_SMPEG_ERROR = -1;
  227. STATUS_SMPEG_STOPPED = 0;
  228. STATUS_SMPEG_PLAYING = 1;
  229. type
  230. SMPEGstatus = Integer;
  231. TSMPEGstatus = Integer;
  232. PSMPEGstatus = ^Integer;
  233. { Matches the declaration of SDL_UpdateRect() }
  234. {$IFNDEF __GPC__}
  235. TSMPEG_DisplayCallback = function( dst: PSDL_Surface; x, y: Integer; w, h: Cardinal ): Pointer; cdecl;
  236. {$ELSE}
  237. TSMPEG_DisplayCallback = function( dst: PSDL_Surface; x, y: Integer; w, h: Cardinal ): Pointer;
  238. {$ENDIF}
  239. { Create a new SMPEG object from an MPEG file.
  240. On return, if 'info' is not NULL, it will be filled with information
  241. about the MPEG object.
  242. This function returns a new SMPEG object. Use SMPEG_error() to find out
  243. whether or not there was a problem building the MPEG stream.
  244. The sdl_audio parameter indicates if SMPEG should initialize the SDL audio
  245. subsystem. If not, you will have to use the SMPEG_playaudio() function below
  246. to extract the decoded data. }
  247. function SMPEG_new(const _file: PChar; info: PSMPEG_Info; sdl_audio: Integer): PSMPEG;
  248. cdecl; external {$IFDEF __GPC__}name 'SMPEG_new'{$ELSE} SmpegLibName{$ENDIF __GPC__};
  249. { The same as above for a file descriptor }
  250. function SMPEG_new_descr(_file: Integer; info: PSMPEG_Info; sdl_audio: Integer): PSMPEG;
  251. cdecl; external {$IFDEF __GPC__}name 'SMPEG_new_descr'{$ELSE} SmpegLibName{$ENDIF __GPC__};
  252. { The same as above but for a raw chunk of data. SMPEG makes a copy of the
  253. data, so the application is free to delete after a successful call to this
  254. function. }
  255. function SMPEG_new_data(data: Pointer; size: Integer; info: PSMPEG_Info; sdl_audio: Integer): PSMPEG;
  256. cdecl; external {$IFDEF __GPC__}name 'SMPEG_new_data'{$ELSE} SmpegLibName{$ENDIF __GPC__};
  257. { Get current information about an SMPEG object }
  258. procedure SMPEG_getinfo(mpeg: PSMPEG; info: PSMPEG_Info);
  259. cdecl; external {$IFDEF __GPC__}name 'SMPEG_getinfo'{$ELSE} SmpegLibName{$ENDIF __GPC__};
  260. //procedure SMPEG_getinfo(mpeg: PSMPEG; info: Pointer);
  261. //cdecl; external {$IFDEF __GPC__}name 'SMPEG_getinfo'{$ELSE} SmpegLibName{$ENDIF __GPC__};
  262. { Enable or disable audio playback in MPEG stream }
  263. procedure SMPEG_enableaudio(mpeg: PSMPEG; enable: Integer);
  264. cdecl; external {$IFDEF __GPC__}name 'SMPEG_enableaudio'{$ELSE} SmpegLibName{$ENDIF __GPC__};
  265. { Enable or disable video playback in MPEG stream }
  266. procedure SMPEG_enablevideo(mpeg: PSMPEG; enable: Integer);
  267. cdecl; external {$IFDEF __GPC__}name 'SMPEG_enablevideo'{$ELSE} SmpegLibName{$ENDIF __GPC__};
  268. { Delete an SMPEG object }
  269. procedure SMPEG_delete(mpeg: PSMPEG);
  270. cdecl; external {$IFDEF __GPC__}name 'SMPEG_delete'{$ELSE} SmpegLibName{$ENDIF __GPC__};
  271. { Get the current status of an SMPEG object }
  272. function SMPEG_status(mpeg: PSMPEG): TSMPEGstatus;
  273. cdecl; external {$IFDEF __GPC__}name 'SMPEG_status'{$ELSE} SmpegLibName{$ENDIF __GPC__};
  274. // status
  275. { Set the audio volume of an MPEG stream, in the range 0-100 }
  276. procedure SMPEG_setvolume(mpeg: PSMPEG; volume: Integer);
  277. cdecl; external {$IFDEF __GPC__}name 'SMPEG_setvolume'{$ELSE} SmpegLibName{$ENDIF __GPC__};
  278. { Set the destination surface for MPEG video playback
  279. 'surfLock' is a mutex used to synchronize access to 'dst', and can be NULL.
  280. 'callback' is a function called when an area of 'dst' needs to be updated.
  281. If 'callback' is NULL, the default function (SDL_UpdateRect) will be used. }
  282. procedure SMPEG_setdisplay(mpeg: PSMPEG; dst: PSDL_Surface; surfLock: PSDL_mutex; callback: TSMPEG_DisplayCallback);
  283. cdecl; external {$IFDEF __GPC__}name 'SMPEG_setdisplay'{$ELSE} SmpegLibName{$ENDIF __GPC__};
  284. { Set or clear looping play on an SMPEG object }
  285. procedure SMPEG_loop(mpeg: PSMPEG; _repeat: Integer);
  286. cdecl; external {$IFDEF __GPC__}name 'SMPEG_loop'{$ELSE} SmpegLibName{$ENDIF __GPC__};
  287. { Scale pixel display on an SMPEG object }
  288. procedure SMPEG_scaleXY(mpeg: PSMPEG; width, height: Integer);
  289. cdecl; external {$IFDEF __GPC__}name 'SMPEG_scaleXY'{$ELSE} SmpegLibName{$ENDIF __GPC__};
  290. procedure SMPEG_scale(mpeg: PSMPEG; scale: Integer);
  291. cdecl; external {$IFDEF __GPC__}name 'SMPEG_scale'{$ELSE} SmpegLibName{$ENDIF __GPC__};
  292. procedure SMPEG_Double(mpeg : PSMPEG; doubleit : Boolean );
  293. { Move the video display area within the destination surface }
  294. procedure SMPEG_move(mpeg: PSMPEG; x, y: Integer);
  295. cdecl; external {$IFDEF __GPC__}name 'SMPEG_move'{$ELSE} SmpegLibName{$ENDIF __GPC__};
  296. { Set the region of the video to be shown }
  297. procedure SMPEG_setdisplayregion(mpeg: PSMPEG; x, y, w, h: Integer);
  298. cdecl; external {$IFDEF __GPC__}name 'SMPEG_setdisplayregion'{$ELSE} SmpegLibName{$ENDIF __GPC__};
  299. { Play an SMPEG object }
  300. procedure SMPEG_play(mpeg: PSMPEG);
  301. cdecl; external {$IFDEF __GPC__}name 'SMPEG_play'{$ELSE} SmpegLibName{$ENDIF __GPC__};
  302. { Pause/Resume playback of an SMPEG object}
  303. procedure SMPEG_pause(mpeg: PSMPEG);
  304. cdecl; external {$IFDEF __GPC__}name 'SMPEG_pause'{$ELSE} SmpegLibName{$ENDIF __GPC__};
  305. { Stop playback of an SMPEG object }
  306. procedure SMPEG_stop(mpeg: PSMPEG);
  307. cdecl; external {$IFDEF __GPC__}name 'SMPEG_stop'{$ELSE} SmpegLibName{$ENDIF __GPC__};
  308. { Rewind the play position of an SMPEG object to the beginning of the MPEG }
  309. procedure SMPEG_rewind(mpeg: PSMPEG);
  310. cdecl; external {$IFDEF __GPC__}name 'SMPEG_rewind'{$ELSE} SmpegLibName{$ENDIF __GPC__};
  311. { Seek 'bytes' bytes in the MPEG stream }
  312. procedure SMPEG_seek(mpeg: PSMPEG; bytes: Integer);
  313. cdecl; external {$IFDEF __GPC__}name 'SMPEG_seek'{$ELSE} SmpegLibName{$ENDIF __GPC__};
  314. { Skip 'seconds' seconds in the MPEG stream }
  315. procedure SMPEG_skip(mpeg: PSMPEG; seconds: single);
  316. cdecl; external {$IFDEF __GPC__}name 'SMPEG_skip'{$ELSE} SmpegLibName{$ENDIF __GPC__};
  317. { Render a particular frame in the MPEG video
  318. API CHANGE: This function no longer takes a target surface and position.
  319. Use SMPEG_setdisplay() and SMPEG_move() to set this information. }
  320. procedure SMPEG_renderFrame(mpeg: PSMPEG; framenum: Integer);
  321. cdecl; external {$IFDEF __GPC__}name 'SMPEG_renderFrame'{$ELSE} SmpegLibName{$ENDIF __GPC__};
  322. { Render the last frame of an MPEG video }
  323. procedure SMPEG_renderFinal(mpeg: PSMPEG; dst: PSDL_Surface; x, y: Integer);
  324. cdecl; external {$IFDEF __GPC__}name 'SMPEG_renderFinal'{$ELSE} SmpegLibName{$ENDIF __GPC__};
  325. { Set video filter }
  326. function SMPEG_filter(mpeg: PSMPEG; filter: PSMPEG_Filter): PSMPEG_Filter;
  327. cdecl; external {$IFDEF __GPC__}name 'SMPEG_filter'{$ELSE} SmpegLibName{$ENDIF __GPC__};
  328. { Return NULL if there is no error in the MPEG stream, or an error message
  329. if there was a fatal error in the MPEG stream for the SMPEG object. }
  330. function SMPEG_error(mpeg: PSMPEG): PChar;
  331. cdecl; external {$IFDEF __GPC__}name 'SMPEG_error'{$ELSE} SmpegLibName{$ENDIF __GPC__};
  332. { Exported callback function for audio playback.
  333. The function takes a buffer and the amount of data to fill, and returns
  334. the amount of data in bytes that was actually written. This will be the
  335. amount requested unless the MPEG audio has finished.
  336. }
  337. function SMPEG_playAudio(mpeg: PSMPEG; stream: PUInt8; len: Integer): Integer;
  338. cdecl; external {$IFDEF __GPC__}name 'SMPEG_playAudio'{$ELSE} SmpegLibName{$ENDIF __GPC__};
  339. { Wrapper for SMPEG_playAudio() that can be passed to SDL and SDL_mixer }
  340. procedure SMPEG_playAudioSDL(mpeg: Pointer; stream: PUInt8; len: Integer);
  341. cdecl; external {$IFDEF __GPC__}name 'SMPEG_playAudioSDL'{$ELSE} SmpegLibName{$ENDIF __GPC__};
  342. { Get the best SDL audio spec for the audio stream }
  343. function SMPEG_wantedSpec(mpeg: PSMPEG; wanted: PSDL_AudioSpec): Integer;
  344. cdecl; external {$IFDEF __GPC__}name 'SMPEG_wantedSpec'{$ELSE} SmpegLibName{$ENDIF __GPC__};
  345. { Inform SMPEG of the actual SDL audio spec used for sound playback }
  346. procedure SMPEG_actualSpec(mpeg: PSMPEG; spec: PSDL_AudioSpec);
  347. cdecl; external {$IFDEF __GPC__}name 'SMPEG_actualSpec'{$ELSE} SmpegLibName{$ENDIF __GPC__};
  348. { This macro can be used to fill a version structure with the compile-time
  349. version of the SDL library. }
  350. procedure SMPEG_GETVERSION( var X : TSMPEG_version );
  351. implementation
  352. {$IFDEF __GPC__}
  353. {$L 'smpeg'} { link smpeg.dll.a or libsmpeg.so or libsmpeg.a }
  354. {$ENDIF}
  355. procedure SMPEG_double(mpeg : PSMPEG; doubleit : Boolean );
  356. begin
  357. if doubleit then
  358. SMPEG_scale( mpeg, 2 )
  359. else
  360. SMPEG_scale( mpeg, 1 );
  361. end;
  362. procedure SMPEG_GETVERSION( var X : TSMPEG_version );
  363. begin
  364. X.major := SMPEG_MAJOR_VERSION;
  365. X.minor := SMPEG_MINOR_VERSION;
  366. X.patch := SMPEG_PATCHLEVEL;
  367. end;
  368. end.