dts.pas 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529
  1. {
  2. Translation of the DTS headers for FreePascal
  3. Copyright (C) 2006 by Ivo Steinmann
  4. }
  5. (*
  6. * dts.h
  7. * Copyright (C) 2004 Gildas Bazin <[email protected]>
  8. *
  9. * This file is part of dtsdec, a free DTS Coherent Acoustics stream decoder.
  10. * See http://www.videolan.org/dtsdec.html for updates.
  11. *
  12. * dtsdec is free software; you can redistribute it and/or modify
  13. * it under the terms of the GNU General Public License as published by
  14. * the Free Software Foundation; either version 2 of the License, or
  15. * (at your option) any later version.
  16. *
  17. * dtsdec is distributed in the hope that it will be useful,
  18. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  19. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  20. * GNU General Public License for more details.
  21. *
  22. * You should have received a copy of the GNU General Public License
  23. * along with this program; if not, write to the Free Software
  24. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  25. *)
  26. {
  27. Using the libdts API
  28. --------------------
  29. libdts provides a low-level interface to decoding audio frames encoded
  30. using DTS Coherent Acoustics. libdts provides downmixing and
  31. dynamic range compression for the following output configurations:
  32. DTS_CHANNEL : Dual mono. Two independant mono channels.
  33. DTS_CHANNEL1 : First of the two mono channels above.
  34. DTS_CHANNEL2 : Second of the two mono channels above.
  35. DTS_MONO : Mono.
  36. DTS_STEREO : Stereo.
  37. DTS_DOLBY : Dolby surround compatible stereo.
  38. DTS_3F : 3 front channels (left, center, right)
  39. DTS_2F1R : 2 front, 1 rear surround channel (L, R, S)
  40. DTS_3F1R : 3 front, 1 rear surround channel (L, C, R, S)
  41. DTS_2F2R : 2 front, 2 rear surround channels (L, R, LS, RS)
  42. DTS_3F2R : 3 front, 2 rear surround channels (L, C, R, LS, RS)
  43. DTS_LFE : Low frequency effects channel. Normally used to connect a
  44. subwoofer. Can be combined with any of the above channels.
  45. For example: DTS_3F2R | DTS_LFE -> 3 front, 2 rear, 1 LFE (5.1)
  46. Initialization
  47. --------------
  48. dts_state_t * dts_init (uint32_t mm_accel);
  49. Initializes the DTS library. Takes as a parameter the acceptable
  50. optimizations which may be used, such as MMX. These are found in the
  51. included header file 'mm_accel', along with an autodetection function
  52. (mm_accel()). Currently, there is no accelleration implemented.
  53. The return value is a pointer to a dts state object.
  54. Probing the bitstream
  55. ---------------------
  56. int dts_syncinfo (uint8_t * buf, int * flags,
  57. int * sample_rate, int * bit_rate, int * frame_length);
  58. The DTS bitstream is composed of several dts frames concatenated one
  59. after each other. A dts frame is the smallest independantly decodable
  60. unit in the stream.
  61. buf must contain at least 14 bytes from the input stream. If these look
  62. like the start of a valid dts frame, dts_syncinfo() returns the size
  63. of the coded frame in bytes, and fills flags, sample_rate, bit_rate and
  64. frame_length with the information encoded in the stream. The returned size
  65. is guaranteed to be an even number between 96 and 16384 for the 16 bits
  66. version of the bitstream and 109 and 18726 for the 14 bits version.
  67. sample_rate will be the sampling frequency in Hz, bit_rate is for the
  68. compressed stream and is in bits per second, and flags is a description of
  69. the coded channels: the DTS_LFE bit is set if there is an LFE channel coded
  70. in this stream, and by masking flags with DTS_CHANNEL_MASK you will get a
  71. value that describes the full-bandwidth channels, as one of the
  72. DTS_CHANNEL...DTS_3F2R flags.
  73. If this can not possibly be a valid frame, then the function returns
  74. 0. You should then try to re-synchronize with the dts stream - one way
  75. to try this would be to advance buf by one byte until its contents
  76. looks like a valid frame, but there might be better
  77. application-specific ways to synchronize.
  78. You need to call this function for each frame, for several
  79. reasons: this function detects errors that the other functions will
  80. not double-check, consecutive frames might have different lengths, and
  81. it helps you re-sync with the stream if you get de-synchronized. It will as
  82. well detect the kind of bitstream it is dealing with (big/little endian,
  83. 16/14 bits mode)
  84. Starting to decode a frame
  85. --------------------------
  86. int dts_frame (dts_state_t * state, uint8_t * buf, int * flags,
  87. sample_t * level, sample_t bias);
  88. This starts the work of decoding the DTS frame (to be completed using
  89. dts_block()). buf should point to the beginning of the complete frame
  90. of the full size returned by dts_syncinfo().
  91. You should pass in the flags the speaker configuration that you
  92. support, and libdts will return the speaker configuration it will use
  93. for its output, based on what is coded in the stream and what you
  94. asked for. For example, if the stream contains 2+2 channels
  95. (dts_syncinfo() returned DTS_2F2R in the flags), and you have 3+1
  96. speakers (you passed DTS_3F1R), then libdts will choose do downmix to
  97. 2+1 speakers, since there is no center channel to send to your center
  98. speaker. So in that case the left and right channels will be
  99. essentially unmodified by the downmix, and the two surround channels
  100. will be added together and sent to your surround speaker. libdts will
  101. return DTS_2F1R to indicate this.
  102. The good news is that when you downmix to stereo you dont have to
  103. worry about this, you will ALWAYS get a stereo output no matter what
  104. was coded in the stream. For more complex output configurations you
  105. will have to handle the case where libdts couldnt give you what you
  106. wanted because some of the channels were not encoded in the stream
  107. though.
  108. Level, bias, and DTS_ADJUST_LEVEL:
  109. Before downmixing, samples are floating point values with a range of
  110. [-1,1]. Most types of downmixing will combine channels together, which
  111. will potentially result in a larger range for the output
  112. samples. libdts provides two methods of controlling the range of the
  113. output, either before or after the downmix stage.
  114. If you do not set DTS_ADJUST_LEVEL, libdts will multiply the samples
  115. by your level value, so that they fit in the [-level,level]
  116. range. Then it will apply the standardized downmix equations,
  117. potentially making the samples go out of that interval again. The
  118. level parameter is not modified.
  119. Setting the DTS_ADJUST_LEVEL flag will instruct libdts to treat your
  120. level value as the intended range interval after downmixing. It will
  121. then figure out what level to use before the downmix (what you should
  122. have passed if you hadnt used the DTS_ADJUST_LEVEL flag), and
  123. overwrite the level value you gave it with that new level value.
  124. The bias represents a value which should be added to the result
  125. regardless:
  126. output_sample = (input_sample * level) + bias;
  127. For example, a bias of 384 and a level of 1 tells liba52 you want
  128. samples between 383 and 385 instead of -1 and 1. This is what the
  129. sample program dtsdec does, as it makes it faster to convert the
  130. samples to integer format, using a trick based on the IEEE
  131. floating-point format.
  132. This function also initialises the state for that frame, which will be
  133. reused next when decoding blocks.
  134. Dynamic range compression
  135. -------------------------
  136. void dts_dynrng (dts_state_t * state,
  137. sample_t (* call) (sample_t, void *), void * data);
  138. This function is purely optional. If you dont call it, libdts will
  139. provide the default behaviour, which is to apply the full dynamic
  140. range compression as specified in the DTS stream. This basically
  141. makes the loud sounds softer, and the soft sounds louder, so you can
  142. more easily listen to the stream in a noisy environment without
  143. disturbing anyone.
  144. If you do call this function and set a NULL callback, this will
  145. totally disable the dynamic range compression and provide a playback
  146. more adapted to a movie theater or a listening room.
  147. If you call this function and specify a callback function, this
  148. callback might be called up to once for each block, with two
  149. arguments: the compression factor 'c' recommended by the bitstream,
  150. and the private data pointer you specified in dts_dynrng(). The
  151. callback will then return the amount of compression to actually use -
  152. typically pow(c,x) where x is somewhere between 0 and 1. More
  153. elaborate compression functions might want to use a different value
  154. for 'x' depending wether c>1 or c<1 - or even something more complex
  155. if this is what you want.
  156. Finding the number of blocks
  157. ----------------------------
  158. int dts_blocks_num (dts_state_t * state);
  159. Every DTS frame is composed of a variable number of blocks. Calling
  160. dts_blocks_num() after dts_frame() will give you the number of blocks in the
  161. current frame.
  162. Decoding blocks
  163. ---------------
  164. int dts_block (dts_state_t * state);
  165. Every DTS frame is composed of a variable number of blocks, each with an
  166. output of 256 samples for each channel. The dts_block() function decodes
  167. the next block in the frame, and should be called dts_blocks_num() times to
  168. decode all of the audio in the frame.
  169. Getting the decoded audio samples
  170. ---------------------------------
  171. sample_t * dts_samples (dts_state_t * state);
  172. After each call to dts_block(), you should extract the audio data from the
  173. internal samples buffer.
  174. This function returns a pointer to an internal buffer which will contain 256
  175. samples for the first channel, followed by 256 samples for the second
  176. channel, etc... the channel order is center, left, right, left
  177. surround, right surround, LFE. If one of the channels is not present in the
  178. libdts output, as indicated by the flags returned by dts_frame(), then
  179. this channel is skipped and the following channels are shifted so
  180. libdts does not leave an empty space between channels.
  181. Pseudocode example
  182. ------------------
  183. dts_state_t * state = dts_init (mm_accel());
  184. loop on input bytes:
  185. if at least 14 bytes in the buffer:
  186. bytes_to_get = dts_syncinfo (...)
  187. if bytes_to_get == 0:
  188. goto loop to keep looking for sync point
  189. else
  190. get rest of bytes
  191. dts_frame (state, buf, ...)
  192. [dts_dynrng (state, ...); this is only optional]
  193. for i = 1 ... dts_blocks_num():
  194. dts_block (state)
  195. dts_samples (state)
  196. convert samples to integer and queue to soundcard
  197. }
  198. unit dts;
  199. {$mode objfpc}
  200. {$MINENUMSIZE 4}
  201. interface
  202. uses
  203. ctypes;
  204. {$UNDEF LIBA52_DOUBLE}
  205. {$IFDEF WINDOWS}
  206. {$DEFINE DYNLINK}
  207. {$ENDIF}
  208. {$IFDEF DYNLINK}
  209. const
  210. {$IF Defined(WINDOWS)}
  211. dtslib = 'dts.dll';
  212. {$ELSEIF Defined(UNIX)}
  213. dtslib = 'libdts.so';
  214. {$ELSE}
  215. {$MESSAGE ERROR 'DYNLINK not supported'}
  216. {$IFEND}
  217. {$ELSE}
  218. {$LINKLIB dts}
  219. {$ENDIF}
  220. (* x86 accelerations *)
  221. const
  222. MM_ACCEL_X86_MMX = $80000000;
  223. MM_ACCEL_X86_3DNOW = $40000000;
  224. MM_ACCEL_X86_MMXEXT = $20000000;
  225. function mm_accel: cuint32; cdecl; external {$IFDEF DYNLINK}dtslib{$ENDIF};
  226. type
  227. pdts_sample_t = ^dts_sample_t;
  228. {$IF Defined(LIBDTS_FIXED)}
  229. dts_sample_t = cint32;
  230. {$ELSEIF Defined(LIBDTS_DOUBLE)}
  231. dts_sample_t = cdouble;
  232. {$ELSE}
  233. dts_sample_t = cfloat;
  234. {$IFEND}
  235. pdts_level_t = ^dts_level_t;
  236. dts_level_t = dts_sample_t;
  237. pdts_state_t = ^dts_state_t;
  238. dts_state_t = record
  239. end;
  240. dts_dynrng_call = function(s: dts_sample_t; data: pointer): dts_level_t; cdecl;
  241. const
  242. DTS_MONO = 0;
  243. DTS_CHANNEL = 1;
  244. DTS_STEREO = 2;
  245. DTS_STEREO_SUMDIFF = 3;
  246. DTS_STEREO_TOTAL = 4;
  247. DTS_3F = 5;
  248. DTS_2F1R = 6;
  249. DTS_3F1R = 7;
  250. DTS_2F2R = 8;
  251. DTS_3F2R = 9;
  252. DTS_4F2R = 10;
  253. DTS_DOLBY = 101; (* FIXME *)
  254. DTS_CHANNEL_MAX = DTS_3F2R; (* We don't handle anything above that *)
  255. DTS_CHANNEL_BITS = 6;
  256. DTS_CHANNEL_MASK = $3F;
  257. DTS_LFE = $80;
  258. DTS_ADJUST_LEVEL = $100;
  259. function dts_init(mm_accel: cuint32): pdts_state_t; cdecl; external {$IFDEF DYNLINK}dtslib{$ENDIF};
  260. function dts_syncinfo(state: pdts_state_t; buf: pcuint8; var flags: cint; var sample_rate: cint; var bit_rate: cint; var frame_length: cint): cint; cdecl; external {$IFDEF DYNLINK}dtslib{$ENDIF};
  261. function dts_frame(state: pdts_state_t; buf: pcuint8; var flags: cint; var level: dts_level_t; bias: dts_sample_t): cint; cdecl; external {$IFDEF DYNLINK}dtslib{$ENDIF};
  262. procedure dts_dynrng(state: pdts_state_t; call: dts_dynrng_call; data: pointer); cdecl; external {$IFDEF DYNLINK}dtslib{$ENDIF};
  263. function dts_blocks_num(state: pdts_state_t): cint; cdecl; external {$IFDEF DYNLINK}dtslib{$ENDIF};
  264. function dts_block(state: pdts_state_t): cint; cdecl; external {$IFDEF DYNLINK}dtslib{$ENDIF};
  265. function dts_samples(state: pdts_state_t): pdts_sample_t; cdecl; external {$IFDEF DYNLINK}dtslib{$ENDIF};
  266. procedure dts_free(state: pdts_state_t); cdecl; external {$IFDEF DYNLINK}dtslib{$ENDIF};
  267. {
  268. Developer of the A52 helpers for FreePascal
  269. Copyright (C) 2006 by Ivo Steinmann
  270. }
  271. type
  272. dts_read_func = function(user: pointer; ptr: pointer; size: cuint): cuint; cdecl;
  273. dts_seek_func = function(user: pointer; offset: clong; whence: cint): clong; cdecl;
  274. dts_close_func = function(user: pointer): cint; cdecl;
  275. dts_tell_func = function(user: pointer): clong; cdecl;
  276. pdts_decoder = ^dts_decoder;
  277. dts_decoder = record
  278. inbuf : array[0..24576-1] of cuint8;
  279. inbuf_ptr : pcuint8;
  280. frame_size : cint;
  281. state : pdts_state_t;
  282. fsamples : pdts_sample_t; // internal samples buffer of dts (returned by dts_samples)
  283. samples : array[0..1,0..6*256-1] of cint16;
  284. samplecnt : cint;
  285. sampleofs : cint;
  286. user : pointer;
  287. read : dts_read_func;
  288. seek : dts_seek_func;
  289. close : dts_close_func;
  290. tell : dts_tell_func;
  291. // Userinfo
  292. flags : cint;
  293. channels : cint;
  294. sample_rate : cint;
  295. bit_rate : cint;
  296. end;
  297. function dts_decoder_init(mm_accel: cuint32; user: pointer; read: dts_read_func; seek: dts_seek_func; close: dts_close_func; tell: dts_tell_func): pdts_decoder;
  298. function dts_decoder_read(decoder: pdts_decoder; buffer: pointer; length: cint): cint;
  299. procedure dts_decoder_free(decoder: pdts_decoder);
  300. implementation
  301. function dts_decoder_init(mm_accel: cuint32; user: pointer; read: dts_read_func; seek: dts_seek_func; close: dts_close_func; tell: dts_tell_func): pdts_decoder;
  302. begin
  303. GetMem(Result, Sizeof(dts_decoder));
  304. FillChar(Result^, Sizeof(dts_decoder), 0);
  305. Result^.state := dts_init(mm_accel);
  306. Result^.fsamples := dts_samples(Result^.state);
  307. Result^.inbuf_ptr := @Result^.inbuf[0];
  308. Result^.user := user;
  309. Result^.read := read;
  310. Result^.seek := seek;
  311. Result^.close := close;
  312. Result^.tell := tell;
  313. end;
  314. procedure dts_decoder_free(decoder: pdts_decoder);
  315. begin
  316. if not Assigned(decoder) then
  317. Exit;
  318. dts_free(decoder^.state);
  319. decoder^.close(decoder^.user);
  320. FreeMem(decoder);
  321. end;
  322. function dts_decoder_read(decoder: pdts_decoder; buffer: pointer; length: cint): cint;
  323. const
  324. HEADER_SIZE = 14;
  325. // ac3_channels: array[0..7] of cint = (2,1,2,3,3,4,4,5);
  326. var
  327. num, ofs: cint;
  328. flags, len, i, j: cint;
  329. sample_rate, bit_rate: cint;
  330. level: dts_sample_t;
  331. begin
  332. // check blocksize here!
  333. ofs := 0;
  334. num := length;
  335. while num > 0 do
  336. begin
  337. if decoder^.samplecnt = 0 then
  338. begin
  339. len := ptrint(decoder^.inbuf_ptr) - ptrint(@decoder^.inbuf);
  340. if (len < HEADER_SIZE) or (len < decoder^.frame_size) then
  341. begin
  342. (* inbuf too small : enlarge *)
  343. len := Sizeof(dts_decoder.inbuf) - len;
  344. len := decoder^.read(decoder^.user, decoder^.inbuf_ptr, len);
  345. if len <= 0 then
  346. Exit(ofs);
  347. Inc(decoder^.inbuf_ptr, len);
  348. end;
  349. if decoder^.frame_size = 0 then
  350. begin
  351. (* no header seen : find one. We need at least 7 bytes to parse it *)
  352. len := dts_syncinfo(decoder^.state, @decoder^.inbuf[0], decoder^.flags, sample_rate, bit_rate, i{dummy});
  353. if len = 0 then
  354. begin
  355. (* no sync found : move by one byte (inefficient, but simple!) *)
  356. Move(decoder^.inbuf[1], decoder^.inbuf[0], ptrint(decoder^.inbuf_ptr) - ptrint(@decoder^.inbuf) - 1);
  357. Dec(decoder^.inbuf_ptr, 1);
  358. end else begin
  359. decoder^.frame_size := len;
  360. (* update codec info *)
  361. decoder^.sample_rate := sample_rate;
  362. decoder^.bit_rate := bit_rate;
  363. {decoder^.channels := ac3_channels[decoder^.flags and $7];
  364. if decoder^.flags and A52_LFE <> 0 then
  365. Inc(decoder^.channels);}
  366. {WriteLn(' frame_size : ', decoder^.frame_size);
  367. WriteLn(' sample_rate : ', sample_rate);
  368. WriteLn(' bit_rate : ', bit_rate);
  369. WriteLn(' channels : ', decoder^.channels);}
  370. end;
  371. Continue;
  372. end;
  373. (* decode the frame *)
  374. flags := DTS_STEREO or DTS_ADJUST_LEVEL;//decoder^.flags;
  375. level := 0;//High(Smallint)-30;
  376. (* FIXME dts_frame dont care on level parameters, so I set it to zero and multiply with High(Smallint) later *)
  377. if dts_frame(decoder^.state, @decoder^.inbuf[0], flags, level, 0) <> 0 then
  378. begin
  379. decoder^.inbuf_ptr := @decoder^.inbuf[0];
  380. decoder^.frame_size := 0;
  381. Continue;
  382. end;
  383. len := dts_blocks_num(decoder^.state);
  384. for i := 0 to len - 1 do
  385. begin
  386. if dts_block(decoder^.state) <> 0 then
  387. begin
  388. decoder^.inbuf_ptr := @decoder^.inbuf[0];
  389. decoder^.frame_size := 0;
  390. Exit(-1);
  391. end;
  392. for j := 0 to 255 do
  393. begin
  394. decoder^.samples[0, i*256+j] := Round(High(Smallint)*decoder^.fsamples[j + 000]);
  395. decoder^.samples[1, i*256+j] := Round(High(Smallint)*decoder^.fsamples[j + 256]);
  396. end;
  397. end;
  398. (* skip decoded frame *)
  399. Move(decoder^.inbuf[decoder^.frame_size], decoder^.inbuf[0], ptrint(decoder^.inbuf_ptr) - ptrint(@decoder^.inbuf) - decoder^.frame_size);
  400. Dec(decoder^.inbuf_ptr, decoder^.frame_size);
  401. decoder^.frame_size := 0;
  402. decoder^.sampleofs := 0;
  403. decoder^.samplecnt := len*256;
  404. end;
  405. len := num div 4;
  406. if len > decoder^.samplecnt then
  407. len := decoder^.samplecnt;
  408. for i := 0 to len - 1 do
  409. begin
  410. pcint16(ptrint(buffer) + ofs + 0)^ := decoder^.samples[0][decoder^.sampleofs];
  411. pcint16(ptrint(buffer) + ofs + 2)^ := decoder^.samples[1][decoder^.sampleofs];
  412. Inc(decoder^.sampleofs);
  413. Dec(decoder^.samplecnt);
  414. ofs := ofs + 4;
  415. num := num - 4;
  416. end;
  417. end;
  418. Result := ofs;
  419. end;
  420. end.