dts.pas 18 KB

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