· 6 years ago · Aug 14, 2019, 04:52 PM
1unit xvid;
2
3interface
4uses
5 windows, SysUtils, math, forms, Contnrs;
6
7// Bitstream Version
8const
9 XVID_BS_VERSION = 34;
10
11 //****************************************************************************
12 // error codes
13 //****************************************************************************
14
15 // all functions return values <0 indicate error
16const
17 XVID_ERR_FAIL = -1; // general fault
18 XVID_ERR_MEMORY = -2; // memory allocation error
19 XVID_ERR_FORMAT = -3; // file format error
20 XVID_ERR_VERSION = -4; // structure version not supported
21 XVID_ERR_END = -5; // encoder only; end of stream reached
22
23 //****************************************************************************
24 // xvid_image_t
25 //****************************************************************************
26
27 // colorspace values
28const
29 XVID_CSP_PLANAR = (1 shl 0); // 4:2:0 planar (==I420, except for pointers/strides)
30 XVID_CSP_USER = XVID_CSP_PLANAR;
31 XVID_CSP_I420 = (1 shl 1); // 4:2:0 planar
32 XVID_CSP_YV12 = (1 shl 2); // 4:2:0 planar
33 XVID_CSP_YUY2 = (1 shl 3); // 4:2:2 packed
34 XVID_CSP_UYVY = (1 shl 4); // 4:2:2 packed
35 XVID_CSP_YVYU = (1 shl 5); // 4:2:2 packed
36 XVID_CSP_BGRA = (1 shl 6); // 32-bit bgra packed
37 XVID_CSP_ABGR = (1 shl 7); // 32-bit abgr packed
38 XVID_CSP_RGBA = (1 shl 8); // 32-bit rgba packed
39 XVID_CSP_ARGB = (1 shl 15); // 32-bit argb packed
40 XVID_CSP_BGR = (1 shl 9); // 24-bit bgr packed
41 XVID_CSP_RGB555 = (1 shl 10); // 16-bit rgb555 packed
42 XVID_CSP_RGB565 = (1 shl 11); // 16-bit rgb565 packed
43 XVID_CSP_SLICE = (1 shl 12); // decoder only: 4:2:0 planar, per slice rendering
44 XVID_CSP_INTERNAL = (1 shl 13); // decoder only: 4:2:0 planar, returns ptrs to internal buffers
45 XVID_CSP_NULL = (1 shl 14); // decoder only: dont output anything
46 XVID_CSP_VFLIP = (1 shl 31); // vertical flip mask
47
48 // xvid_image_t
49type
50 xvid_image_t = packed record
51 csp: Integer; // [in] colorspace; or with XVID_CSP_VFLIP to perform vertical flip
52 plane: array[0..3] of Pointer; // [in] image plane ptrs
53 stride: array[0..3] of Integer; // [in] image stride; "bytes per row"
54 end;
55
56 // video-object-sequence profiles
57const
58 XVID_PROFILE_S_L0 = $08; // simple
59 XVID_PROFILE_S_L1 = $01;
60 XVID_PROFILE_S_L2 = $02;
61 XVID_PROFILE_S_L3 = $03;
62 XVID_PROFILE_ARTS_L1 = $91; // advanced realtime simple
63 XVID_PROFILE_ARTS_L2 = $92;
64 XVID_PROFILE_ARTS_L3 = $93;
65 XVID_PROFILE_ARTS_L4 = $94;
66 XVID_PROFILE_AS_L0 = $F0; // advanced simple
67 XVID_PROFILE_AS_L1 = $F1;
68 XVID_PROFILE_AS_L2 = $F2;
69 XVID_PROFILE_AS_L3 = $F3;
70 XVID_PROFILE_AS_L4 = $F4;
71
72 // aspect ratios
73const
74 XVID_PAR_11_VGA = 1; // 1:1 vga (square), default if supplied PAR is not a valid value
75 XVID_PAR_43_PAL = 2; // 4:3 pal (12:11 625-line)
76 XVID_PAR_43_NTSC = 3; // 4:3 ntsc (10:11 525-line)
77 XVID_PAR_169_PAL = 4; // 16:9 pal (16:11 625-line)
78 XVID_PAR_169_NTSC = 5; // 16:9 ntsc (40:33 525-line)
79 XVID_PAR_EXT = 15; // extended par; use par_width, par_height
80
81 // frame type flags
82const
83 XVID_TYPE_VOL = -1; // decoder only: vol was decoded
84 XVID_TYPE_NOTHING = 0; // decoder only (encoder stats): nothing was decoded/encoded
85 XVID_TYPE_AUTO = 0; // encoder: automatically determine coding type
86 XVID_TYPE_IVOP = 1; // intra frame
87 XVID_TYPE_PVOP = 2; // predicted frame
88 XVID_TYPE_BVOP = 3; // bidirectionally encoded
89 XVID_TYPE_SVOP = 4; // predicted+sprite frame
90
91 //****************************************************************************
92 // xvid_global()
93 //****************************************************************************
94
95 // cpu_flags definitions (make sure to sync this with cpuid.asm for ia32)
96const
97 XVID_CPU_FORCE = (1 shl 31); // force passed cpu flags
98 XVID_CPU_ASM = (1 shl 7); // native assembly
99 // ARCH_IS_IA32
100 XVID_CPU_MMX = (1 shl 0); // mmx : pentiumMMX,k6
101 XVID_CPU_MMXEXT = (1 shl 1); // mmx-ext : pentium2, athlon
102 XVID_CPU_SSE = (1 shl 2); // sse : pentium3, athlonXP
103 XVID_CPU_SSE2 = (1 shl 3); // sse2 : pentium4, athlon64
104 XVID_CPU_3DNOW = (1 shl 4); // 3dnow : k6-2
105 XVID_CPU_3DNOWEXT = (1 shl 5); // 3dnow-ext : athlon
106 XVID_CPU_TSC = (1 shl 6); // tsc : Pentium
107 // ARCH_IS_PPC
108 XVID_CPU_ALTIVEC = (1 shl 0); // altivec
109
110 XVID_DEBUG_ERROR = (1 shl 0);
111 XVID_DEBUG_STARTCODE = (1 shl 1);
112 XVID_DEBUG_HEADER = (1 shl 2);
113 XVID_DEBUG_TIMECODE = (1 shl 3);
114 XVID_DEBUG_MB = (1 shl 4);
115 XVID_DEBUG_COEFF = (1 shl 5);
116 XVID_DEBUG_MV = (1 shl 6);
117 XVID_DEBUG_RC = (1 shl 7);
118 XVID_DEBUG_DEBUG = (1 shl 31);
119
120 // XVID_GBL_INIT param1
121type
122 xvid_gbl_init_t = packed record
123 version: Integer;
124 cpu_flags: Word; // [in:opt] zero = autodetect cpu; XVID_CPU_FORCE or {cpu features} = force cpu features
125 debug: Integer; // [in:opt] debug level
126 end;
127
128 // XVID_GBL_INFO param1
129 xvid_gbl_info_t = packed record
130 version: Integer;
131 actual_version: Integer; // [out] returns the actual xvidcore version
132 build: Pchar; // [out] if !null, points to description of this xvid core build
133 cpu_flags: Word; // [out] detected cpu features
134 num_threads: Integer; // [out] detected number of cpus/threads
135 end;
136
137 // XVID_GBL_CONVERT param1
138 xvid_gbl_convert_t = packed record
139 version: Integer;
140 input: XVID_IMAGE_T; // [in] input image and colorspace
141 output: XVID_IMAGE_T; // [in] output image and colorspace
142 width: Integer; // [in] width
143 height: Integer; // [in] height
144 interlacing: Integer; // [in] interlacing
145 end;
146
147const
148 XVID_GBL_INIT = 0; // initialize xvidcore; must be called before using xvid_decore, or xvid_encore)
149 XVID_GBL_INFO = 1; // return some info about xvidcore, and the host computer
150 XVID_GBL_CONVERT = 2; // colorspace conversion utility
151
152//type
153 //xvid_global = function(handle:Pointer;opt:Integer;param1:Pointer;param2: Pointer): Integer; cdecl;
154
155 //****************************************************************************
156 // xvid_decore()
157 //****************************************************************************
158const
159 XVID_DEC_CREATE = 0; // create decore instance; return 0 on success
160 XVID_DEC_DESTROY = 1; // destroy decore instance: return 0 on success
161 XVID_DEC_DECODE = 2; // decode a frame: returns number of bytes consumed >= 0
162
163{type
164 xvid_decore = function(handle: Pointer;
165 opt: Integer;
166 param1: Pointer;
167 param2: Pointer): Integer; cdecl;
168}
169 // XVID_DEC_CREATE param 1
170 //-image width and height may be specified here when the dimensions are
171 //known in advance.
172
173type
174 xvid_dec_create_t = packed record
175 version: Integer;
176 width: Integer; // [in:opt] image width
177 height: Integer; // [in:opt] image width
178 handle: Pointer; // [out] decore context handle
179 end;
180
181 // XVID_DEC_DECODE param1
182 // general flags
183const
184 XVID_LOWDELAY = (1 shl 0); // lowdelay mode
185 XVID_DISCONTINUITY = (1 shl 1); // indicates break in stream
186 XVID_DEBLOCKY = (1 shl 2); // perform luma deblocking
187 XVID_DEBLOCKUV = (1 shl 3); // perform chroma deblocking
188 XVID_FILMEFFECT = (1 shl 4); // adds film grain
189
190type
191 xvid_dec_frame_t = packed record
192 version: Integer;
193 general: Integer; // [in:opt] general flags
194 bitstream: Pointer; // [in] bitstream (read from)
195 length: Integer; // [in] bitstream length
196 output: XVID_IMAGE_T; // [in] output image (written to)
197 end;
198
199 // XVID_DEC_DECODE param2 :: optional
200 vop_t = packed record
201 version: Integer;
202 _type: Integer; // [out] output data type
203 // type>0 {XVID_TYPE_IVOP,XVID_TYPE_PVOP,XVID_TYPE_BVOP,XVID_TYPE_SVOP}
204 general: Integer; // [out] flags
205 time_base: Integer; // [out] time base
206 time_increment: Integer; // [out] time increment
207
208 // XXX: external deblocking stuff
209 qscale: PInteger; // [out] pointer to quantizer table
210 qscale_stride: Integer; // [out] quantizer scale stride
211 end;
212
213 vol_t = record // XVID_TYPE_VOL
214 general: Integer; // [out] flags
215 width: Integer; // [out] width
216 height: Integer; // [out] height
217 par: Integer; // [out] pixel aspect ratio (refer to XVID_PAR_xxx above)
218 par_width: Integer; // [out] aspect ratio width [1..255]
219 par_height: Integer; // [out] aspect ratio height [1..255]
220 end;
221
222 xvid_dec_stats_t = packed record
223 version: integer;
224 frametype: integer; // [out] output data type */
225 vop: vop_t;
226 vol: vol_t;
227 end;
228const
229 XVID_ZONE_QUANT = (1 shl 0);
230 XVID_ZONE_WEIGHT = (1 shl 1);
231
232type
233 Pxvid_enc_zone_t = ^xvid_enc_zone_t;
234 xvid_enc_zone_t = packed record
235 frame: Integer;
236 mode: Integer;
237 increment: Integer;
238 base: Integer;
239 end;
240
241 //----------------------------------------------------------------------------
242 // xvid_enc_stats_t structure
243 //
244 // Used in:
245 // - xvid_plg_data_t structure
246 // - optional parameter in xvid_encore() function
247 //
248 // .coding_type = XVID_TYPE_NOTHING if the stats are not given
249 //----------------------------------------------------------------------------
250
251type
252 xvid_enc_stats_t = packed record
253 version: Integer; // encoding parameters
254 frame_type: Integer; // [out] coding type
255 quant: Integer; // [out] frame quantizer
256 vol_flags: Integer; // [out] vol flags (see above)
257 vop_flags: Integer; // [out] vop flags (see above)
258
259 // bitrate
260 length: Integer; // [out] frame length
261 hlength: Integer; // [out] header length (bytes)
262 kblks: Integer; // [out] number of blocks compressed as Intra
263 mblks: Integer; // [out] number of blocks compressed as Inter
264 ublks: Integer; // [out] number of blocks marked as not_coded
265
266 sse_y: Integer; // [out] Y plane's sse
267 sse_u: Integer; // [out] U plane's sse
268 sse_v: Integer; // [out] V plane's sse
269 end;
270
271 //****************************************************************************
272 {-xvid plugin system -- internals }
273
274 {-xvidcore will call XVID_PLG_INFO and XVID_PLG_CREATE during XVID_ENC_CREATE }
275 {-before encoding each frame xvidcore will call XVID_PLG_BEFORE }
276 {-after encoding each frame xvidcore will call XVID_PLG_AFTER }
277 {-xvidcore will call XVID_PLG_DESTROY during XVID_ENC_DESTROY }
278 //****************************************************************************
279
280const
281 XVID_PLG_CREATE = (1 shl 0);
282 XVID_PLG_DESTROY = (1 shl 1);
283 XVID_PLG_INFO = (1 shl 2);
284 XVID_PLG_BEFORE = (1 shl 3);
285 XVID_PLG_FRAME = (1 shl 4);
286 XVID_PLG_AFTER = (1 shl 5);
287
288 // xvid_plg_info_t.flags*/ }
289 XVID_REQORIGINAL = (1 shl 0); // plugin requires a copy of the original (uncompressed) image
290 XVID_REQPSNR = (1 shl 1); // plugin requires psnr between the uncompressed and compressed image
291 XVID_REQDQUANTS = (1 shl 2); // plugin requires access to the dquant table
292
293type
294 xvid_plg_info_t = packed record
295 version: Integer;
296 flags: Integer; // [in:opt] plugin flags
297 end;
298
299 xvid_plg_create_t = packed record
300 version: Integer;
301 num_zones: Integer; // [out]
302 zones: PXVID_ENC_ZONE_T; // [out]
303
304 width: Integer; // [out]
305 height: Integer; // [out]
306 mb_width: Integer; // [out]
307 mb_height: Integer; // [out]
308 fincr: Integer; // [out]
309 fbase: Integer; // [out]
310
311 param: Pointer; // [out]
312 end;
313
314 xvid_plg_destroy_t = packed record
315 version: Integer;
316 num_frames: Integer; // [out] total frame encoded
317 end;
318
319 xvid_plg_data_t = packed record
320 version: Integer;
321 zone: PXVID_ENC_ZONE_T; // [out] current zone
322
323 width: Integer; // [out]
324 height: Integer; // [out]
325 mb_width: Integer; // [out]
326 mb_height: Integer; // [out]
327 fincr: Integer; // [out]
328 fbase: Integer; // [out]
329
330 min_quant: array[0..3 - 1] of Integer; // [out]
331 max_quant: array[0..3 - 1] of Integer; // [out]
332
333 reference: XVID_IMAGE_T; // [out] -> [out]
334 current: XVID_IMAGE_T; // [out] -> [in,out]
335 original: XVID_IMAGE_T; // [out] after: points the original (uncompressed) copy of the current frame
336 frame_num: Integer; // [out] frame number
337
338 _type: Integer; // [in,out]
339 quant: Integer; // [in,out]
340
341 dquant: PInteger; // [in,out] pointer to diff quantizer table
342 dquant_stride: Integer; // [in,out] diff quantizer stride
343
344 vop_flags: Integer; // [in,out]
345 vol_flags: Integer; // [in,out]
346 motion_flags: Integer; // [in,out]
347
348 // Deprecated, use the stats field instead. }
349 // Will disapear before 1.0 }
350 length: Integer; // [out] after: length of encoded frame
351 kblks: Integer; // [out] number of blocks compressed as Intra
352 mblks: Integer; // [out] number of blocks compressed as Inter
353 ublks: Integer; // [out] number of blocks marked not_coded
354 sse_y: Integer; // [out] Y plane's sse
355 sse_u: Integer; // [out] U plane's sse
356 sse_v: Integer; // [out] V plane's sse
357 // End of duplicated data, kept only for binary compatibility*/ }
358
359 bquant_ratio: Integer; // [in]
360 bquant_offset: Integer; // [in]
361
362 stats: XVID_ENC_STATS_T; // [out] frame statistics
363 end;
364
365 //****************************************************************************
366 {-xvid plugin system -- external }
367
368 {-the application passes xvid an array of "xvid_plugin_t" at XVID_ENC_CREATE. the array }
369 {-indicates the plugin function pointer and plugin-specific data. }
370 {-xvidcore handles the rest. example: }
371
372 {-xvid_enc_create_t create; }
373 {-xvid_enc_plugin_t plugins[2]; }
374
375 {-plugins[0].func = xvid_psnr_func; }
376 {-plugins[0].param = NULL; }
377 {-plugins[1].func = xvid_cbr_func; }
378 {-plugins[1].param = and cbr_data; }
379
380 {-create.num_plugins = 2; }
381 {-create.plugins = plugins; }
382
383 //****************************************************************************
384
385type
386 XVID_PLUGIN_FUNC = function(HANDLE: pointer; OPT: Integer; PARAM1, PARAM2: Pointer): Integer;
387
388 Pxvid_enc_plugin_t = ^xvid_enc_plugin_t;
389 xvid_enc_plugin_t = packed record
390 func: XVID_PLUGIN_FUNC;
391 param: Pointer;
392 end;
393
394 { single-pass rate control }
395 { two-pass rate control: first pass }
396 { two-pass rate control: second pass }
397
398 { lumimasking }
399
400 { write psnr values to stdout }
401 { dump before and after yuvpgms }
402
403 { single pass rate control }
404 { CBR and Constant quantizer modes }
405type
406 xvid_plugin_single_t = packed record
407 version: Integer;
408 bitrate: Integer; // [in] bits per second
409 reaction_delay_factor: Integer; // [in]
410 averaging_period: Integer; // [in]
411 buffer: Integer; // [in]
412 end;
413
414 xvid_plugin_2pass1_t = packed record
415 version: Integer;
416 filename: PChar;
417 end;
418
419const
420 XVID_PAYBACK_BIAS = 0; // payback with bias
421 XVID_PAYBACK_PROP = 1; // payback proportionally
422
423type
424 xvid_plugin_2pass2_t = packed record
425 version: Integer;
426 bitrate: Integer; // [in] bits per second
427 filename: PChar; // [in] first pass stats filename
428
429 keyframe_boost: Integer; // [in] keyframe boost percentage: [0..100]
430 curve_compression_high: Integer; // [in] percentage of compression performed on the high part of the curve (above average) }
431 curve_compression_low: Integer; // [in] percentage of compression performed on the low part of the curve (below average) }
432 overflow_control_strength: Integer; // [in] Payback delay expressed in number of frames }
433 max_overflow_improvement: Integer; // [in] percentage of allowed range for a frame that gets bigger because of overflow bonus }
434 max_overflow_degradation: Integer; // [in] percentage of allowed range for a frame that gets smaller because of overflow penalty }
435
436 kfreduction: Integer; // [in] maximum bitrate reduction applied to an iframe under the kfthreshold distance limit }
437 kfthreshold: Integer; // [in] if an iframe is closer to the next iframe than this distance, a quantity of bits }
438 // is substracted from its bit allocation. The reduction is computed as multiples of
439 // kfreduction/kthreshold. It reaches kfreduction when the distance == kfthreshold,
440 // 0 for 1<distance<kfthreshold }
441
442 container_frame_overhead: Integer; // [in] How many bytes the controller has to compensate per frame due to container format overhead }
443 end;
444
445 //****************************************************************************
446 // ENCODER API
447 //****************************************************************************
448
449 //----------------------------------------------------------------------------
450 // Encoder operations
451 //----------------------------------------------------------------------------
452const
453 XVID_ENC_CREATE = 0; // create encoder instance; returns 0 on success
454 XVID_ENC_DESTROY = 1; // destroy encoder instance; returns 0 on success
455 XVID_ENC_ENCODE = 2; //* encode a frame: returns number of ouput bytes
456 // * 0 means this frame should not be written (ie. encoder lag)
457//----------------------------------------------------------------------------
458// Encoder entry point
459//----------------------------------------------------------------------------
460{type
461
462 xvid_encore = function(handle: Pointer;
463 opt: Integer;
464 param1: Pointer;
465 param2: Pointer): Integer; cdecl;
466}
467 // Quick API reference
468 //
469 // XVID_ENC_CREATE operation
470 // - handle: ignored
471 // - opt: XVID_ENC_CREATE
472 // - param1: address of a xvid_enc_create_t structure
473 // - param2: ignored
474 //
475 // XVID_ENC_ENCODE operation
476 // - handle: an instance returned by a CREATE op
477 // - opt: XVID_ENC_ENCODE
478 // - param1: address of a xvid_enc_frame_t structure
479 // - param2: address of a xvid_enc_stats_t structure (optional)
480 // its return value is asynchronous to what is written to the buffer
481 // depending on the delay introduced by bvop use. It's display
482 // ordered.
483 //
484 // XVID_ENC_DESTROY operation
485 // - handle: an instance returned by a CREATE op
486 // - opt: XVID_ENC_DESTROY
487 // - param1: ignored
488 // - param2: ignored
489
490 //----------------------------------------------------------------------------
491 // "Global" flags
492 //
493 // These flags are used for xvid_enc_create_t->global field during instance
494 // creation (operation XVID_ENC_CREATE)
495 //----------------------------------------------------------------------------
496
497const
498 XVID_GLOBAL_PACKED = (1 shl 0); // packed bitstream
499 XVID_GLOBAL_CLOSED_GOP = (1 shl 1); // closed_gop: was DX50BVOP dx50 bvop compatibility
500 XVID_GLOBAL_EXTRASTATS_ENABLE = (1 shl 2);
501{$IFDEF 0}
502 XVID_GLOBAL_VOL_AT_IVOP = (1 shl 3); // write vol at every ivop: WIN32/divx compatibility
503 XVID_GLOBAL_FORCE_VOL = (1 shl 4); // when vol-based parameters are changed, insert an ivop NOT recommended
504{$ENDIF}
505
506 //----------------------------------------------------------------------------
507 // "VOL" flags
508 //
509 // These flags are used for xvid_enc_frame_t->vol_flags field during frame
510 // encoding (operation XVID_ENC_ENCODE)
511 //----------------------------------------------------------------------------
512
513const
514 XVID_VOL_MPEGQUANT = (1 shl 0); // enable MPEG type quantization
515 XVID_VOL_EXTRASTATS = (1 shl 1); // enable plane sse stats
516 XVID_VOL_QUARTERPEL = (1 shl 2); // enable quarterpel: frames will encoded as quarterpel
517 XVID_VOL_GMC = (1 shl 3); // enable GMC; frames will be checked for gmc suitability
518 XVID_VOL_REDUCED_ENABLE = (1 shl 4); // enable reduced resolution vops: frames will be checked for rrv suitability
519 XVID_VOL_INTERLACING = (1 shl 5); // enable interlaced encoding
520
521 //----------------------------------------------------------------------------
522 // "VOP" flags
523 //
524 // These flags are used for xvid_enc_frame_t->vop_flags field during frame
525 // encoding (operation XVID_ENC_ENCODE)
526 //----------------------------------------------------------------------------
527
528 // Always valid
529const
530 XVID_VOP_DEBUG = (1 shl 0); // print debug messages in frames
531 XVID_VOP_HALFPEL = (1 shl 1); // use halfpel interpolation
532 XVID_VOP_INTER4V = (1 shl 2); // use 4 motion vectors per MB
533 XVID_VOP_TRELLISQUANT = (1 shl 3); // use trellis based R-D 'optimal' quantization
534 XVID_VOP_CHROMAOPT = (1 shl 4); // enable chroma optimization pre-filter
535 XVID_VOP_CARTOON = (1 shl 5); // use 'cartoon mode'
536 XVID_VOP_GREYSCALE = (1 shl 6); // enable greyscale only mode (even for color input material chroma is ignored)
537 XVID_VOP_HQACPRED = (1 shl 7); // high quality ac prediction
538 XVID_VOP_MODEDECISION_RD = (1 shl 8); // enable DCT-ME and use it for mode decision
539 XVID_VOP_FAST_MODEDECISION_RD = (1 shl 12); // use simplified R-D mode decision
540
541 // Only valid for vol_flags or =XVID_VOL_INTERLACING
542const
543 XVID_VOP_TOPFIELDFIRST = (1 shl 9); // set top-field-first flag
544 XVID_VOP_ALTERNATESCAN = (1 shl 10); // set alternate vertical scan flag
545
546 // only valid for vol_flags or =XVID_VOL_REDUCED_ENABLED
547 XVID_VOP_REDUCED = (1 shl 11); // reduced resolution vop
548
549 //----------------------------------------------------------------------------
550 // "Motion" flags
551 //
552 // These flags are used for xvid_enc_frame_t->motion field during frame
553 // encoding (operation XVID_ENC_ENCODE)
554 //----------------------------------------------------------------------------
555
556 // Motion Estimation Search Patterns
557const
558 XVID_ME_ADVANCEDDIAMOND16 = (1 shl 0); // use advdiamonds instead of diamonds as search pattern
559 XVID_ME_ADVANCEDDIAMOND8 = (1 shl 1); // use advdiamond for XVID_ME_EXTSEARCH8
560 XVID_ME_USESQUARES16 = (1 shl 2); // use squares instead of diamonds as search pattern
561 XVID_ME_USESQUARES8 = (1 shl 3); // use square for XVID_ME_EXTSEARCH8
562
563 // SAD operator based flags
564 XVID_ME_HALFPELREFINE16 = (1 shl 4);
565 XVID_ME_HALFPELREFINE8 = (1 shl 6);
566 XVID_ME_QUARTERPELREFINE16 = (1 shl 7);
567 XVID_ME_QUARTERPELREFINE8 = (1 shl 8);
568 XVID_ME_GME_REFINE = (1 shl 9);
569 XVID_ME_EXTSEARCH16 = (1 shl 10); // extend PMV by more searches
570 XVID_ME_EXTSEARCH8 = (1 shl 11); // use diamond/square for extended 8x8 search
571 XVID_ME_CHROMA_PVOP = (1 shl 12); // also use chroma for P_VOP/S_VOP ME
572 XVID_ME_CHROMA_BVOP = (1 shl 13); // also use chroma for B_VOP ME
573 XVID_ME_FASTREFINE16 = (1 shl 25); // use low-complexity refinement functions
574 XVID_ME_FASTREFINE8 = (1 shl 29); // low-complexity 8x8 sub-block refinement
575
576 // Rate Distortion based flags
577 // Valid when XVID_VOP_MODEDECISION_RD is enabled
578 XVID_ME_HALFPELREFINE16_RD = (1 shl 14); // perform RD-based halfpel refinement
579 XVID_ME_HALFPELREFINE8_RD = (1 shl 15); // perform RD-based halfpel refinement for 8x8 mode
580 XVID_ME_QUARTERPELREFINE16_RD = (1 shl 16); // perform RD-based qpel refinement
581 XVID_ME_QUARTERPELREFINE8_RD = (1 shl 17); // perform RD-based qpel refinement for 8x8 mode
582 XVID_ME_EXTSEARCH_RD = (1 shl 18); // perform RD-based search using square pattern enable XVID_ME_EXTSEARCH8 to do this in 8x8 search as well
583 XVID_ME_CHECKPREDICTION_RD = (1 shl 19); // always check vector equal to prediction
584
585 // Other
586 XVID_ME_DETECT_STATIC_MOTION = (1 shl 24); // speed-up ME by detecting stationary scenes
587 XVID_ME_SKIP_DELTASEARCH = (1 shl 26); // speed-up by skipping b-frame delta search
588 XVID_ME_FAST_MODEINTERPOLATE = (1 shl 27); // speed-up by partly skipping interpolate mode
589 XVID_ME_BFRAME_EARLYSTOP = (1 shl 28); // speed-up by early exiting b-search
590
591 // Unused
592 XVID_ME_UNRESTRICTED16 = (1 shl 20); // unrestricted ME, not implemented
593 XVID_ME_OVERLAPPING16 = (1 shl 21); // overlapping ME, not implemented
594 XVID_ME_UNRESTRICTED8 = (1 shl 22); // unrestricted ME, not implemented
595 XVID_ME_OVERLAPPING8 = (1 shl 23); // overlapping ME, not implemented
596
597 //----------------------------------------------------------------------------
598 // xvid_enc_create_t structure definition
599 //
600 // This structure is passed as param1 during an instance creation (operation
601 // XVID_ENC_CREATE)
602 //----------------------------------------------------------------------------
603
604type
605 xvid_enc_create_t = packed record
606 version: Integer;
607 profile: Integer; // [in] profile@level; refer to XVID_PROFILE_xxx
608 width: Integer; // [in] frame dimensions; width, pixel units
609 height: Integer; // [in] frame dimensions; height, pixel units
610
611 num_zones: Integer; // [in:opt] number of bitrate zones
612 zones: PXVID_ENC_ZONE_T; // ^^ zone array
613
614 num_plugins: Integer; // [in:opt] number of plugins
615 plugins: PXVID_ENC_PLUGIN_T; // ^^ plugin array
616
617 num_threads: Integer; // [in:opt] number of threads
618 max_bframes: Integer; // [in:opt] max sequential bframes (0=disable bframes)
619
620 global: Integer; // [in:opt] global flags; controls encoding behavior
621 // --- vol-based stuff; included here for convenience
622 fincr: Integer; // [in:opt] framerate increment; set to zero for variable framerate
623 fbase: Integer; // [in] framerate base frame_duration = fincr/fbase seconds
624 // ----------------------------------------------
625 // --- vop-based; included here for convenience
626 max_key_interval: Integer; // [in:opt] the maximum interval between key frames
627
628 frame_drop_ratio: Integer; // [in:opt] frame dropping: 0=drop none... 100=drop all
629
630 bquant_ratio: Integer; // [in:opt] bframe quantizer multipier/offeset; used to decide bframes quant when bquant==-1
631 bquant_offset: Integer; // bquant = (avg(past_ref_quant,future_ref_quant)*bquant_ratio + bquant_offset) / 100
632
633 min_quant: array[0..3 - 1] of Integer; // [in:opt]
634 max_quant: array[0..3 - 1] of Integer; // [in:opt]
635 // ----------------------------------------------
636
637 handle: Pointer; // [out] encoder instance handle
638 end;
639
640 //----------------------------------------------------------------------------
641 // xvid_enc_frame_t structure definition
642 //
643 // This structure is passed as param1 during a frame encoding (operation
644 // XVID_ENC_ENCODE)
645 //----------------------------------------------------------------------------
646
647 // out value for the frame structure->type field
648 // unlike stats output in param2, this field is not asynchronous and tells
649 // the client app, if the frame written into the stream buffer is an ivop
650 // usually used for indexing purpose in the container
651const
652 XVID_KEYFRAME = (1 shl 1);
653
654 // The structure
655type
656 xvid_enc_frame_t = packed record
657 version: Integer;
658 // VOL related stuff
659 // unless XVID_FORCEVOL is set, the encoder will not react to any changes
660 // here until the next VOL (keyframe).
661
662 vol_flags: Integer; // [in] vol flags
663 quant_intra_matrix: PByte; // [in:opt] custom intra qmatrix
664 quant_inter_matrix: PByte; // [in:opt] custom inter qmatrix
665
666 par: Integer; // [in:opt] pixel aspect ratio (refer to XVID_PAR_xxx above)
667 par_width: Integer; // [in:opt] aspect ratio width
668 par_height: Integer; // [in:opt] aspect ratio height
669
670 // Other fields that can change on a frame base
671 fincr: Integer; // [in:opt] framerate increment, for variable framerate only
672 vop_flags: Integer; // [in] (general)vop-based flags
673 motion: Integer; // [in] ME options
674
675 input: XVID_IMAGE_T; // [in] input image (read from)
676
677 coding_type: Integer; // [in:opt] coding type
678 quant: Integer; // [in] frame quantizer; if <=0, automatic (ratecontrol)
679 bframe_threshold: Integer;
680 bitstream: Pointer; // [in:opt] bitstream ptr (written to)
681 length: Integer; // [in:opt] bitstream length (bytes)
682
683 out_flags: Integer; // [out] bitstream output flags
684 end;
685
686const
687 XviDCoreDll = 'xvidcore.dll';
688
689type
690
691 TxvidInitException = class(Exception)
692 end;
693
694 TxvidEncodeException = class(Exception)
695 end;
696
697 TxvidDecodeException = class(Exception)
698 end;
699
700 TxvidEncoder = class;
701 TxvidDecoder = class;
702
703 TxvidCore = class(TObject)
704 private
705 _xvidInited: boolean;
706 _xvidHandle: THandle;
707 //_globalProc: xvid_global;
708 //_encodeProc: xvid_encore;
709 //_decodeProc: xvid_decore;
710
711 _encparam: xvid_enc_create_t;
712 _decparam: xvid_dec_create_t;
713
714 _encoders: TObjectList;
715 _decoders: TObjectList;
716 public
717 XVID_VERSION: integer;
718 XVID_API: Integer;
719 constructor create();
720 destructor Destroy(); override;
721// procedure init();
722 //±àÂ뺯Êý
723 function encode(handle: Pointer; opt: Integer; param1: Pointer;
724 param2: Pointer): integer;
725 //½âÂ뺯Êý
726 function decode(handle: Pointer; opt: Integer; param1: Pointer;
727 param2: Pointer): Integer;
728
729 function CreateEncoder: TxvidEncoder;
730 procedure RemoveEncoder(encoder: TxvidEncoder);
731
732 function CreateDecoder: TxvidDecoder;
733 procedure RemoveDecoder(decoder: TxvidDecoder);
734
735 end;
736
737 TxvidEncoder = class(TObject)
738 private
739 _xvidcore: Txvidcore;
740 _encodeHandle: Pointer;
741 _encparam: xvid_enc_create_t;
742 _outbuf: array[0..352 * 288 * 3 - 1] of byte; // *
743 _tmpBuf: array[0..352 * 288 * 3 - 1] of byte; // *
744 public
745 constructor create(xvidcore: TxvidCore);
746 destructor Destroy(); override;
747
748 procedure init(width: Integer; height: Integer; rate: Integer);
749 procedure reset(width: Integer; height: Integer; rate: Integer);
750 function encode(opt: Integer; param1,
751 param2: Pointer): integer;
752 end;
753
754 TxvidDecoder = class(TObject)
755 private
756 _decodeHandle: Pointer;
757 _xvidcore: Txvidcore;
758 _decparam: xvid_dec_create_t;
759 _decBuf: array[0..352 * 288 * 3 - 1] of byte; // -
760 _tmpBuf: array[0..352 * 288 * 3 - 1] of byte; // -
761 public
762 constructor create(xvidcore: TxvidCore);
763 destructor Destroy(); override;
764
765 procedure init(width: Integer; height: Integer; rate: Integer);
766 end;
767
768function XVID_MAKE_VERSION(a, b, c: Integer): Integer;
769function XVID_VERSION_MAJOR(a: Integer): Byte;
770function XVID_VERSION_MINOR(a: Integer): Byte;
771function XVID_VERSION_PATCH(a: Integer): Byte;
772
773function XVID_MAKE_API(a, b: Integer): Integer;
774function XVID_API_MAJOR(a: Integer): Integer;
775function XVID_API_MINOR(a: Integer): Integer;
776
777function xvid_global(hnd: pointer; opt: integer; param1: pointer; param2: pointer): integer; cdecl; external XviDCoreDll;
778function xvid_decore(hnd: pointer; opt: integer; param1: pointer; param2: pointer): integer; cdecl; external XviDCoreDll;
779function xvid_encore(hnd: pointer; opt: integer; param1: pointer; param2: pointer): integer; cdecl; external XviDCoreDll;
780
781implementation
782
783
784
785{ TxvidCore }
786
787function TxvidCore.CreateDecoder: TxvidDecoder;
788begin
789
790end;
791
792function TxvidCore.CreateEncoder: TxvidEncoder;
793begin
794
795end;
796
797constructor TxvidCore.create;
798begin
799 _encoders := TObjectList.Create();
800 _decoders := TObjectList.Create();
801
802
803 XVID_VERSION := XVID_MAKE_VERSION(1, 0, 0);
804 XVID_API := XVID_MAKE_API(4, 0);
805 //_encodeProc := nil;
806 //_DecodeProc := nil;
807 // _xvidHandle := 0;
808end;
809
810function TxvidCore.decode(handle: Pointer; opt: Integer; param1,
811 param2: Pointer): Integer;
812begin
813 //if assigned(_decodeProc) then
814 // result := _decodeProc(handle, opt, param1, param2)
815 // else
816 // raise Exception.Create('endcodeproc not exists');
817end;
818
819procedure TxvidCore.RemoveDecoder(decoder: TxvidDecoder);
820begin
821 _decoders.Remove(decoder);
822end;
823
824procedure TxvidCore.RemoveEncoder(encoder: TxvidEncoder);
825begin
826 _encoders.Remove(encoder);
827end;
828
829destructor TxvidCore.destroy;
830begin
831 if _xvidHandle > 0 then
832 FreeLibrary(_xvidHandle);
833 _encoders.Free;
834 _decoders.Free;
835 inherited;
836end;
837
838function TxvidCore.encode(handle: Pointer; opt: Integer; param1,
839 param2: Pointer): integer;
840begin
841 // if assigned(_encodeProc) then
842 // result := _encodeProc(handle, opt, param1, param2)
843 // else
844 // raise Exception.Create('endcodeproc not exists');
845
846end;
847{
848procedure TxvidCore.init;
849var
850 xinit: xvid_gbl_init_t;
851
852begin
853 if _xvidInited then
854 exit;
855
856 if _xvidHandle <= 0 then
857 _xvidHandle := LoadLibrary(pchar(ExtractFilePath(Application.ExeName) + XviDCoreDll));
858
859 if _xvidHandle <= 0 then
860 raise TXviDInitException.Create('δ·¢ÏÖÊÓÆµ±à½âÂëÆ÷');
861
862 _globalProc := GetProcAddress(_xvidHandle, 'xvid_global');
863 _encodeProc := GetProcAddress(_xvidHandle, 'xvid_encore');
864 _decodeProc := GetProcAddress(_xvidHandle, 'xvid_decore');
865
866 xinit.version := XVID_VERSION;
867 xinit.cpu_flags := 0;
868 xinit.debug := 0;
869 try
870 _xvidInited := _globalProc(nil, 0, @xinit, nil) = 0;
871 except
872 TXviDInitException.Create('±à½âÂëÆ÷³õʼ»¯Ê§°Ü');
873 _xvidInited := false;
874 end;
875end;
876}
877{ TxvidEncoder }
878
879constructor TxvidEncoder.create(xvidcore: TxvidCore);
880begin
881 _encodeHandle := nil;
882 _xvidCore := xvidcore;
883
884end;
885
886procedure TxvidEncoder.init(width, height, rate: Integer);
887begin
888 FillMemory(@_encparam, sizeof(_encparam), 0);
889 _encparam.version := _xvidcore.XVID_VERSION;
890 _encparam.profile := XVID_PROFILE_ARTS_L1;
891 _encparam.width := width;
892 _encparam.height := height;
893 _encparam.max_key_interval := 300;
894 if _xvidcore.encode(nil, XVID_ENC_CREATE, @_encparam, nil) <> 0 then
895 raise TxvidEncodeException.Create('±àÂëÆ÷³õʼ»¯Ê§°Ü');
896 _encodeHandle := _encparam.handle;
897
898end;
899
900procedure TxvidEncoder.reset(width, height, rate: Integer);
901begin
902
903 _xvidcore.encode(_encodeHandle, XVID_ENC_DESTROY, nil, nil);
904 FillMemory(@_encparam, sizeof(_encparam), 0);
905 _encparam.version := _xvidcore.XVID_VERSION;
906 _encparam.profile := XVID_PROFILE_ARTS_L1;
907 _encparam.width := width;
908 _encparam.height := height;
909 _encparam.max_key_interval := 300;
910
911 if _xvidcore.encode(nil, XVID_ENC_CREATE, @_encparam, nil) <> 0 then
912 raise TxvidEncodeException.Create('±àÂëÆ÷³õʼ»¯Ê§°Ü');
913 _encodeHandle := _encparam.handle;
914
915end;
916
917destructor TxvidEncoder.Destroy;
918begin
919 if _encodeHandle <> nil then
920 _xvidCore.decode(_encodeHandle, XVID_ENC_DESTROY, nil, nil);
921
922 _xvidcore.RemoveEncoder(self);
923 inherited;
924end;
925
926function TxvidEncoder.encode(opt: Integer; param1,
927 param2: Pointer): integer;
928var
929 FRAME: xvid_enc_frame_t;
930 Eimg_t: xvid_image_t;
931begin
932 {
933 FillMemory(@Eimg_t, Sizeof(Eimg_t), 0);
934 Eimg_t.csp := XVID_CSP_BGR or XVID_CSP_VFLIP;
935 Eimg_t.plane[0] := lpData;
936 Eimg_t.stride[0] := 352 * 3;
937
938 FillMemory(@FRAME, sizeof(FRAME), 0);
939 FRAME.version := _xvidCore.XVID_VERSION;
940 FRAME.vol_flags := XVID_VOL_QUARTERPEL;
941 FRAME.quant_intra_matrix := nil;
942 FRAME.quant_inter_matrix := nil;
943 FRAME.vop_flags := XVID_VOP_HALFPEL or XVID_VOP_CHROMAOPT or XVID_VOP_TRELLISQUANT; //or XVID_VOP_DEBUG;
944 FRAME.motion := XVID_ME_QUARTERPELREFINE16 or XVID_ME_CHROMA_BVOP;
945 FRAME.input := Eimg_t;
946 FRAME._type := XVID_TYPE_AUTO;
947 FRAME.bitstream := @_outbuf[0];
948
949 if VPos < 3 then
950 FRAME._type := 1;
951
952 FRAME.quant := 3;
953
954 FRAME.length := 0;
955
956 FRAME.length := _xviDCore.encode(_encodehandle, XVID_ENC_ENCODE, @FRAME, nil);
957 }
958 result := _xviDCore.encode(_encodehandle, opt, param1, param2);
959
960end;
961
962{ TxvidDecoder }
963
964constructor TxvidDecoder.create(xvidcore: TxvidCore);
965begin
966 _decodeHandle := nil;
967 _xvidCore := xvidcore;
968end;
969
970procedure TxvidDecoder.init(width, height, rate: Integer);
971begin
972 FillMemory(@_decparam, sizeof(_decparam), 0);
973 _decparam.version := _xvidcore.XVID_VERSION;
974
975 if _xvidcore.decode(nil, XVID_DEC_CREATE, @_decparam, nil) <> 0 then
976 begin
977 _decodeHandle := nil;
978 raise TxvidDecodeException.Create('±àÂëÆ÷³õʼ»¯Ê§°Ü');
979 end;
980 _decodehandle := _decparam.handle;
981
982end;
983
984
985
986
987destructor TxvidDecoder.Destroy;
988begin
989 if _decodeHandle <> nil then
990 _xvidCore.decode(_decodeHandle, XVID_DEC_DESTROY, nil, nil);
991 _xvidCore.RemoveDecoder(self);
992 inherited;
993end;
994
995{public function}
996
997function XVID_MAKE_VERSION(a, b, c: Integer): Integer;
998begin
999 Result := ((a and $FF) shl 16) or ((b and $FF) shl 8) or (c and $FF);
1000end;
1001
1002function XVID_VERSION_MAJOR(a: Integer): Byte;
1003begin
1004 result := (a shr 16) and $FF;
1005end;
1006
1007function XVID_VERSION_MINOR(a: Integer): Byte;
1008begin
1009 result := (a shr 8) and $FF;
1010end;
1011
1012function XVID_VERSION_PATCH(a: Integer): Byte;
1013begin
1014 result := (a shr 0) and $FF;
1015end;
1016
1017function XVID_MAKE_API(a, b: Integer): Integer;
1018begin
1019 result := ((a and $FF) shl 16) or ((b and $FF) shl 0);
1020end;
1021
1022function XVID_API_MAJOR(a: Integer): Integer;
1023begin
1024 result := (a shr 16) and $FF;
1025end;
1026
1027function XVID_API_MINOR(a: Integer): Integer;
1028begin
1029 result := (a shr 0) and $FF;
1030end;
1031
1032end.