Leaked source code of windows server 2003
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

4017 lines
129 KiB

  1. /*
  2. * @DEC_COPYRIGHT@
  3. */
  4. /*
  5. * HISTORY
  6. * $Log: sv_api.c,v $
  7. * Revision 1.1.8.11 1996/10/28 17:32:51 Hans_Graves
  8. * MME-01402. Changed SvGet/SetParamInt() to qwords to allow for timestamps.
  9. * [1996/10/28 17:09:33 Hans_Graves]
  10. *
  11. * Revision 1.1.8.10 1996/10/12 17:19:24 Hans_Graves
  12. * Added initialization of SV_PARAM_SKIPPEL and SV_PARAM_HALFPEL to MPEG encode.
  13. * [1996/10/12 17:16:28 Hans_Graves]
  14. *
  15. * Revision 1.1.8.9 1996/09/29 22:19:56 Hans_Graves
  16. * Added stride to ScYuv411ToRgb() calls.
  17. * [1996/09/29 21:34:40 Hans_Graves]
  18. *
  19. * Revision 1.1.8.8 1996/09/25 19:17:01 Hans_Graves
  20. * Fix up support for YUY2 under MPEG.
  21. * [1996/09/25 19:03:17 Hans_Graves]
  22. *
  23. * Revision 1.1.8.7 1996/09/18 23:51:11 Hans_Graves
  24. * Added JPEG 4:1:1 to 4:2:2 conversions. Added BI_YVU9SEP and BI_RGB 24 support in MPEG decode.
  25. * [1996/09/18 22:16:08 Hans_Graves]
  26. *
  27. * Revision 1.1.8.6 1996/07/30 20:25:50 Wei_Hsu
  28. * Add Logarithmetic search for motion estimation.
  29. * [1996/07/30 15:57:59 Wei_Hsu]
  30. *
  31. * Revision 1.1.8.5 1996/05/24 22:22:26 Hans_Graves
  32. * Add GetImageSize MPEG support for BI_DECYUVDIB
  33. * [1996/05/24 22:14:20 Hans_Graves]
  34. *
  35. * Revision 1.1.8.4 1996/05/08 16:24:32 Hans_Graves
  36. * Put BITSTREAM_SUPPORT around BSIn in SvDecompress
  37. * [1996/05/08 16:24:15 Hans_Graves]
  38. *
  39. * Revision 1.1.8.3 1996/05/07 21:24:05 Hans_Graves
  40. * Add missing break in switch statement in SvRegisterCallback
  41. * [1996/05/07 21:19:50 Hans_Graves]
  42. *
  43. * Revision 1.1.8.2 1996/05/07 19:56:46 Hans_Graves
  44. * Added HUFF_SUPPORT. Remove NT warnings.
  45. * [1996/05/07 17:27:18 Hans_Graves]
  46. *
  47. * Revision 1.1.6.12 1996/04/23 18:51:10 Karen_Dintino
  48. * Fix the memory alloc for the ref buffer for WIN32
  49. * [1996/04/23 18:49:23 Karen_Dintino]
  50. *
  51. * Revision 1.1.6.11 1996/04/17 23:44:35 Karen_Dintino
  52. * added initializations for H.261/WIN32
  53. * [1996/04/17 23:43:20 Karen_Dintino]
  54. *
  55. * Revision 1.1.6.10 1996/04/11 22:54:43 Karen_Dintino
  56. * added casting for in SetFrameRate
  57. * [1996/04/11 22:52:29 Karen_Dintino]
  58. *
  59. * Revision 1.1.6.9 1996/04/11 14:14:14 Hans_Graves
  60. * Fix NT warnings
  61. * [1996/04/11 14:09:53 Hans_Graves]
  62. *
  63. * Revision 1.1.6.8 1996/04/10 21:48:09 Hans_Graves
  64. * Added SvGet/SetBoolean() functions.
  65. * [1996/04/10 21:28:13 Hans_Graves]
  66. *
  67. * Revision 1.1.6.7 1996/04/09 20:50:44 Karen_Dintino
  68. * Adding WIN32 support
  69. * [1996/04/09 20:47:26 Karen_Dintino]
  70. *
  71. * Revision 1.1.6.6 1996/04/09 16:05:00 Hans_Graves
  72. * Add some abs() around height params. SvRegisterCallback() cleanup
  73. * [1996/04/09 15:39:31 Hans_Graves]
  74. *
  75. * Revision 1.1.6.5 1996/04/04 23:35:27 Hans_Graves
  76. * Removed BI_YU16SEP support from MPEG decomp.
  77. * [1996/04/04 23:12:02 Hans_Graves]
  78. *
  79. * Fixed up Multibuf size (MPEG) related stuff
  80. * [1996/04/04 23:08:55 Hans_Graves]
  81. *
  82. * Revision 1.1.6.4 1996/04/01 15:17:47 Bjorn_Engberg
  83. * Got rid of a compiler warning.
  84. * [1996/04/01 15:02:35 Bjorn_Engberg]
  85. *
  86. * Revision 1.1.6.3 1996/03/29 22:22:36 Hans_Graves
  87. * -Added JPEG_SUPPORT ifdefs.
  88. * -Changed JPEG related structures to fit naming conventions.
  89. * [1996/03/29 21:59:08 Hans_Graves]
  90. *
  91. * Revision 1.1.6.2 1996/03/16 20:13:51 Karen_Dintino
  92. * Adding NT port changes for H.261
  93. * [1996/03/16 19:48:31 Karen_Dintino]
  94. *
  95. * Revision 1.1.4.12 1996/02/26 18:42:32 Karen_Dintino
  96. * fix PTT 01106 server crash in ICCompress
  97. * [1996/02/26 18:41:33 Karen_Dintino]
  98. *
  99. * Revision 1.1.4.11 1996/02/22 17:35:19 Bjorn_Engberg
  100. * Added support for JPEG Mono to BI_BITFIELDS 16 decompression on NT.
  101. * [1996/02/22 17:34:53 Bjorn_Engberg]
  102. *
  103. * Revision 1.1.4.10 1996/02/08 13:48:44 Bjorn_Engberg
  104. * Get rid of int to float compiler warning.
  105. * [1996/02/08 13:48:20 Bjorn_Engberg]
  106. *
  107. * Revision 1.1.4.9 1996/02/07 23:24:08 Hans_Graves
  108. * MPEG Key frame stats initialization
  109. * [1996/02/07 23:14:41 Hans_Graves]
  110. *
  111. * Revision 1.1.4.8 1996/02/06 22:54:17 Hans_Graves
  112. * Added SvGet/SetParam functions
  113. * [1996/02/06 22:51:19 Hans_Graves]
  114. *
  115. * Revision 1.1.4.7 1996/01/08 20:19:33 Bjorn_Engberg
  116. * Got rid of more compiler warnings.
  117. * [1996/01/08 20:19:13 Bjorn_Engberg]
  118. *
  119. * Revision 1.1.4.6 1996/01/08 16:42:47 Hans_Graves
  120. * Added MPEG II decoding support
  121. * [1996/01/08 15:41:37 Hans_Graves]
  122. *
  123. * Revision 1.1.4.5 1996/01/02 18:32:14 Bjorn_Engberg
  124. * Got rid of compiler warnings: Added Casts, Removed unused variables.
  125. * [1996/01/02 17:26:21 Bjorn_Engberg]
  126. *
  127. * Revision 1.1.4.4 1995/12/28 18:40:06 Bjorn_Engberg
  128. * IsSupported() sometimes returned garbage and thus a false match.
  129. * SvDecompressQuery() and SvCompressQuery() were using the
  130. * wrong lookup tables.
  131. * [1995/12/28 18:39:30 Bjorn_Engberg]
  132. *
  133. * Revision 1.1.4.3 1995/12/08 20:01:30 Hans_Graves
  134. * Added SvSetRate() and SvSetFrameRate() to H.261 compression open
  135. * [1995/12/08 19:58:32 Hans_Graves]
  136. *
  137. * Revision 1.1.4.2 1995/12/07 19:32:17 Hans_Graves
  138. * Added MPEG I & II Encoding support
  139. * [1995/12/07 18:43:45 Hans_Graves]
  140. *
  141. * Revision 1.1.2.46 1995/11/30 20:17:06 Hans_Graves
  142. * Added BI_DECGRAYDIB handling for JPEG decompression, used with Mono JPEG
  143. * [1995/11/30 20:12:24 Hans_Graves]
  144. *
  145. * Revision 1.1.2.45 1995/11/29 17:53:26 Hans_Graves
  146. * Added JPEG_DIB 8-bit to BI_DECGRAYDIB as supported format
  147. * [1995/11/29 17:53:00 Hans_Graves]
  148. *
  149. * Revision 1.1.2.44 1995/11/28 22:47:33 Hans_Graves
  150. * Added BI_BITFIELDS as supported decompression format for H261 and MPEG
  151. * [1995/11/28 22:39:25 Hans_Graves]
  152. *
  153. * Revision 1.1.2.43 1995/11/17 21:31:28 Hans_Graves
  154. * Added checks on ImageSize in SvDecompress()
  155. * [1995/11/17 21:27:19 Hans_Graves]
  156. *
  157. * Query cleanup - Added lookup tables for supportted formats
  158. * [1995/11/17 20:53:54 Hans_Graves]
  159. *
  160. * Revision 1.1.2.42 1995/11/03 16:36:25 Paul_Gauthier
  161. * Reject requests to scale MPEG input during decompression
  162. * [1995/11/03 16:34:01 Paul_Gauthier]
  163. *
  164. * Revision 1.1.2.41 1995/10/25 18:19:22 Bjorn_Engberg
  165. * What was allocated with ScPaMalloc() must be freed with ScPaFree().
  166. * [1995/10/25 18:02:04 Bjorn_Engberg]
  167. *
  168. * Revision 1.1.2.40 1995/10/25 17:38:04 Hans_Graves
  169. * Removed some memory freeing calls on image memory in SvCloseCodec for H261 decoding. The app allocates the image buffer.
  170. * [1995/10/25 17:37:35 Hans_Graves]
  171. *
  172. * Revision 1.1.2.39 1995/10/13 16:57:19 Bjorn_Engberg
  173. * Added a cast to get rid of a compiler warning.
  174. * [1995/10/13 16:56:29 Bjorn_Engberg]
  175. *
  176. * Revision 1.1.2.38 1995/10/06 20:51:47 Farokh_Morshed
  177. * Enhance to handle BI_BITFIELDS for input to compression
  178. * [1995/10/06 20:49:10 Farokh_Morshed]
  179. *
  180. * Revision 1.1.2.37 1995/10/02 19:31:03 Bjorn_Engberg
  181. * Clarified what formats are supported and not.
  182. * [1995/10/02 18:51:49 Bjorn_Engberg]
  183. *
  184. * Revision 1.1.2.36 1995/09/28 20:40:09 Farokh_Morshed
  185. * Handle negative Height
  186. * [1995/09/28 20:39:45 Farokh_Morshed]
  187. *
  188. * Revision 1.1.2.35 1995/09/26 17:49:47 Paul_Gauthier
  189. * Fix H.261 output conversion to interleaved YUV
  190. * [1995/09/26 17:49:20 Paul_Gauthier]
  191. *
  192. * Revision 1.1.2.34 1995/09/26 15:58:44 Paul_Gauthier
  193. * Fix mono JPEG to interlaced 422 YUV conversion
  194. * [1995/09/26 15:58:16 Paul_Gauthier]
  195. *
  196. * Revision 1.1.2.33 1995/09/25 21:18:14 Paul_Gauthier
  197. * Added interleaved YUV output to decompression
  198. * [1995/09/25 21:17:42 Paul_Gauthier]
  199. *
  200. * Revision 1.1.2.32 1995/09/22 12:58:50 Bjorn_Engberg
  201. * More NT porting work; Added MPEG_SUPPORT and H261_SUPPORT.
  202. * [1995/09/22 12:26:14 Bjorn_Engberg]
  203. *
  204. * Revision 1.1.2.31 1995/09/20 19:35:02 Hans_Graves
  205. * Cleaned-up debugging statements
  206. * [1995/09/20 19:33:07 Hans_Graves]
  207. *
  208. * Revision 1.1.2.30 1995/09/20 18:22:53 Karen_Dintino
  209. * Free temp buffer after convert
  210. * [1995/09/20 18:22:37 Karen_Dintino]
  211. *
  212. * Revision 1.1.2.29 1995/09/20 17:39:22 Karen_Dintino
  213. * {** Merge Information **}
  214. * {** Command used: bsubmit **}
  215. * {** Ancestor revision: 1.1.2.27 **}
  216. * {** Merge revision: 1.1.2.28 **}
  217. * {** End **}
  218. * Add RGB support to JPEG
  219. * [1995/09/20 17:37:41 Karen_Dintino]
  220. *
  221. * Revision 1.1.2.28 1995/09/20 15:00:03 Bjorn_Engberg
  222. * Port to NT
  223. * [1995/09/20 14:43:15 Bjorn_Engberg]
  224. *
  225. * Revision 1.1.2.27 1995/09/13 19:42:44 Paul_Gauthier
  226. * Added TGA2 support (direct 422 interleaved output from JPEG codec
  227. * [1995/09/13 19:15:47 Paul_Gauthier]
  228. *
  229. * Revision 1.1.2.26 1995/09/11 18:53:45 Farokh_Morshed
  230. * {** Merge Information **}
  231. * {** Command used: bsubmit **}
  232. * {** Ancestor revision: 1.1.2.24 **}
  233. * {** Merge revision: 1.1.2.25 **}
  234. * {** End **}
  235. * Support BI_BITFIELDS format
  236. * [1995/09/11 18:52:08 Farokh_Morshed]
  237. *
  238. * Revision 1.1.2.25 1995/09/05 14:05:01 Paul_Gauthier
  239. * Add ICMODE_OLDQ flag on ICOpen for softjpeg to use old quant tables
  240. * [1995/08/31 20:58:06 Paul_Gauthier]
  241. *
  242. * Revision 1.1.2.24 1995/08/16 19:56:46 Hans_Graves
  243. * Fixed RELEASE_BUFFER callbacks for Images
  244. * [1995/08/16 19:54:40 Hans_Graves]
  245. *
  246. * Revision 1.1.2.23 1995/08/15 19:14:18 Karen_Dintino
  247. * pass H261 struct to inithuff & freehuff
  248. * [1995/08/15 19:10:58 Karen_Dintino]
  249. *
  250. * Revision 1.1.2.22 1995/08/14 19:40:36 Hans_Graves
  251. * Add CB_CODEC_DONE callback. Fixed Memory allocation and freeing under H261.
  252. * [1995/08/14 18:45:22 Hans_Graves]
  253. *
  254. * Revision 1.1.2.21 1995/08/04 17:22:49 Hans_Graves
  255. * Make END_SEQ callback happen after any H261 decompress error.
  256. * [1995/08/04 17:20:55 Hans_Graves]
  257. *
  258. * Revision 1.1.2.20 1995/08/04 16:32:46 Karen_Dintino
  259. * Free Huffman codes on end of Encode and Decode
  260. * [1995/08/04 16:25:59 Karen_Dintino]
  261. *
  262. * Revision 1.1.2.19 1995/07/31 21:11:14 Karen_Dintino
  263. * {** Merge Information **}
  264. * {** Command used: bsubmit **}
  265. * {** Ancestor revision: 1.1.2.17 **}
  266. * {** Merge revision: 1.1.2.18 **}
  267. * {** End **}
  268. * Add 411YUVSEP Support
  269. * [1995/07/31 20:41:28 Karen_Dintino]
  270. *
  271. * Revision 1.1.2.18 1995/07/31 20:19:58 Hans_Graves
  272. * Set Format parameter in Frame callbacks
  273. * [1995/07/31 20:16:06 Hans_Graves]
  274. *
  275. * Revision 1.1.2.17 1995/07/28 20:58:40 Hans_Graves
  276. * Added Queue debugging messages.
  277. * [1995/07/28 20:49:15 Hans_Graves]
  278. *
  279. * Revision 1.1.2.16 1995/07/28 17:36:10 Hans_Graves
  280. * Fixed up H261 Compression and Decompression.
  281. * [1995/07/28 17:26:17 Hans_Graves]
  282. *
  283. * Revision 1.1.2.15 1995/07/27 18:28:55 Hans_Graves
  284. * Fixed AddBuffer() so it works with H261.
  285. * [1995/07/27 18:24:37 Hans_Graves]
  286. *
  287. * Revision 1.1.2.14 1995/07/26 17:49:04 Hans_Graves
  288. * Fixed SvCompressBegin() JPEG initialization. Added ImageQ support.
  289. * [1995/07/26 17:41:24 Hans_Graves]
  290. *
  291. * Revision 1.1.2.13 1995/07/25 22:00:33 Hans_Graves
  292. * Fixed H261 image size logic in SvCompressQuery().
  293. * [1995/07/25 21:59:08 Hans_Graves]
  294. *
  295. * Revision 1.1.2.12 1995/07/21 17:41:20 Hans_Graves
  296. * Renamed Callback related stuff.
  297. * [1995/07/21 17:26:11 Hans_Graves]
  298. *
  299. * Revision 1.1.2.11 1995/07/18 17:26:56 Hans_Graves
  300. * Fixed QCIF width parameter checking.
  301. * [1995/07/18 17:24:55 Hans_Graves]
  302. *
  303. * Revision 1.1.2.10 1995/07/17 22:01:45 Hans_Graves
  304. * Removed defines for CIF_WIDTH, CIF_HEIGHT, etc.
  305. * [1995/07/17 21:48:51 Hans_Graves]
  306. *
  307. * Revision 1.1.2.9 1995/07/17 16:12:37 Hans_Graves
  308. * H261 Cleanup.
  309. * [1995/07/17 15:50:51 Hans_Graves]
  310. *
  311. * Revision 1.1.2.8 1995/07/12 19:48:30 Hans_Graves
  312. * Fixed up some H261 Queue/Callback bugs.
  313. * [1995/07/12 19:31:51 Hans_Graves]
  314. *
  315. * Revision 1.1.2.7 1995/07/11 22:12:00 Karen_Dintino
  316. * Add SvCompressQuery, SvDecompressQuery support
  317. * [1995/07/11 21:57:21 Karen_Dintino]
  318. *
  319. * Revision 1.1.2.6 1995/07/01 18:43:49 Karen_Dintino
  320. * Add support for H.261 Decompress
  321. * [1995/07/01 18:26:18 Karen_Dintino]
  322. *
  323. * Revision 1.1.2.5 1995/06/19 20:31:51 Karen_Dintino
  324. * Adding support for H.261 Codec
  325. * [1995/06/19 19:25:27 Karen_Dintino]
  326. *
  327. * Revision 1.1.2.4 1995/06/09 18:33:34 Hans_Graves
  328. * Added SvGetInputBitstream(). Changed SvDecompressBegin() to handle NULL Image formats.
  329. * [1995/06/09 16:35:29 Hans_Graves]
  330. *
  331. * Revision 1.1.2.3 1995/06/05 21:07:20 Hans_Graves
  332. * Fixed logic in SvRegisterCallback().
  333. * [1995/06/05 20:04:30 Hans_Graves]
  334. *
  335. * Revision 1.1.2.2 1995/05/31 18:12:42 Hans_Graves
  336. * Inclusion in new SLIB location.
  337. * [1995/05/31 17:18:53 Hans_Graves]
  338. *
  339. * Revision 1.1.2.10 1995/02/02 19:26:01 Paul_Gauthier
  340. * Fix to blank bottom strip & server crash for softjpeg compress
  341. * [1995/02/02 19:12:58 Paul_Gauthier]
  342. *
  343. * Revision 1.1.2.9 1995/01/20 21:45:57 Jim_Ludwig
  344. * Add support for 16 bit YUV
  345. * [1995/01/20 21:28:49 Jim_Ludwig]
  346. *
  347. * Revision 1.1.2.8 1994/12/12 15:38:54 Paul_Gauthier
  348. * Merge changes from other SLIB versions
  349. * [1994/12/12 15:34:21 Paul_Gauthier]
  350. *
  351. * Revision 1.1.2.7 1994/11/18 18:48:36 Paul_Gauthier
  352. * Cleanup & bug fixes
  353. * [1994/11/18 18:44:02 Paul_Gauthier]
  354. *
  355. * Revision 1.1.2.6 1994/10/28 19:56:30 Paul_Gauthier
  356. * Additional Clean Up
  357. * [1994/10/28 19:54:35 Paul_Gauthier]
  358. *
  359. * Revision 1.1.2.5 1994/10/17 19:03:28 Paul_Gauthier
  360. * Fixed reversed Quality scale
  361. * [1994/10/17 19:02:50 Paul_Gauthier]
  362. *
  363. * Revision 1.1.2.4 1994/10/13 20:34:27 Paul_Gauthier
  364. * MPEG cleanup
  365. * [1994/10/13 20:23:23 Paul_Gauthier]
  366. *
  367. * Revision 1.1.2.3 1994/10/10 21:45:50 Tom_Morris
  368. * Rename Status to not conflict with X11
  369. * [1994/10/10 21:44:16 Tom_Morris]
  370. *
  371. * Revision 1.1.2.2 1994/10/07 14:39:25 Paul_Gauthier
  372. * SLIB v3.0 incl. MPEG Decode
  373. * [1994/10/07 13:53:40 Paul_Gauthier]
  374. *
  375. * ******************************************************************
  376. * Changes from original brance merged into this file 11/30/94 PSG
  377. * ******************************************************************
  378. * Revision 1.1.2.10 1994/08/11 21:27:24 Leela_Obilichetti
  379. * Added in width and height checks into SvDecompressQuery and SvCompressQuery.
  380. * [1994/08/11 21:03:38 Leela_Obilichetti]
  381. *
  382. * Revision 1.1.2.9 1994/08/09 18:52:40 Ken_Chiquoine
  383. * set mode type in decode as well as encode
  384. * [1994/08/09 18:52:17 Ken_Chiquoine]
  385. *
  386. * Revision 1.1.2.8 1994/08/04 22:06:33 Leela_Obilichetti
  387. * oops, removed fprintf.
  388. * [1994/08/04 21:54:11 Leela_Obilichetti]
  389. *
  390. * Revision 1.1.2.7 1994/08/04 21:34:04 Leela_Obilichetti
  391. * v1 drop.
  392. * [1994/08/04 21:05:01 Leela_Obilichetti]
  393. *
  394. * Revision 1.1.2.6 1994/07/15 23:31:43 Leela_Obilichetti
  395. * added new stuff for compression - v4 of SLIB.
  396. * [1994/07/15 23:29:17 Leela_Obilichetti]
  397. *
  398. * Revision 1.1.2.5 1994/06/08 16:44:28 Leela_Obilichetti
  399. * fixes for YUV_to_RGB for OSF/1. Haven't tested on Microsoft.
  400. * [1994/06/08 16:42:46 Leela_Obilichetti]
  401. *
  402. * Revision 1.1.2.4 1994/06/03 21:11:14 Leela_Obilichetti
  403. * commment out the code to let in DECYUVDIB
  404. * free memory that is allocated for YUV
  405. * add in code to convert from DECSEPYUV to DECYUV -
  406. * shouldn't get there since se don't allow YUV anymore
  407. * [1994/06/03 21:03:42 Leela_Obilichetti]
  408. *
  409. * Revision 1.1.2.3 1994/05/11 21:02:17 Leela_Obilichetti
  410. * bug fix for NT
  411. * [1994/05/11 20:56:16 Leela_Obilichetti]
  412. *
  413. * Revision 1.1.2.2 1994/05/09 22:06:07 Leela_Obilichetti
  414. * V3 drop
  415. * [1994/05/09 21:51:30 Leela_Obilichetti]
  416. *
  417. * $EndLog$
  418. */
  419. /*
  420. **++
  421. ** FACILITY: Workstation Multimedia (WMM) v1.0
  422. **
  423. ** FILE NAME:
  424. ** MODULE NAME:
  425. **
  426. ** MODULE DESCRIPTION:
  427. **
  428. ** DESIGN OVERVIEW:
  429. **
  430. **--
  431. */
  432. /*****************************************************************************
  433. ** Copyright (c) Digital Equipment Corporation, 1994 **
  434. ** **
  435. ** All Rights Reserved. Unpublished rights reserved under the copyright **
  436. ** laws of the United States. **
  437. ** **
  438. ** The software contained on this media is proprietary to and embodies **
  439. ** the confidential technology of Digital Equipment Corporation. **
  440. ** Possession, use, duplication or dissemination of the software and **
  441. ** media is authorized only pursuant to a valid written license from **
  442. ** Digital Equipment Corporation. **
  443. ** **
  444. ** RESTRICTED RIGHTS LEGEND Use, duplication, or disclosure by the U.S. **
  445. ** Government is subject to restrictions as set forth in Subparagraph **
  446. ** (c)(1)(ii) of DFARS 252.227-7013, or in FAR 52.227-19, as applicable. **
  447. ******************************************************************************/
  448. /*-------------------------------------------------------------------------
  449. * Modification History: sv_api.c
  450. *
  451. * 08-Sep-94 PSG Added MPEG decode support
  452. * 29-Jul-94 VB Added restrictions for compression
  453. * 21-Jul-94 VB Rewrote/cleaned up sections of JPEG decompression
  454. * 13-Jul-94 PSG Added support for encode/decode of grayscale only
  455. * 07-Jul-94 PSG Converted to single Info structure (cmp/dcmp)
  456. * 14-Jun-94 VB Added JPEG compression support
  457. * 08-Jun-94 PSG Added support for BI_DECXIMAGEDIB (B,G,R,0)
  458. * 06-Jun-94 PSG Bring code up to SLIB v0.04 spec
  459. * 15-Apr-94 VB Added support for 24-bit,16-bit and 15-bit RGB output
  460. * 24-Feb-94 PSG Bring code up to SLIB v0.02 spec
  461. * 20-Jan-94 PSG added a number of new SLIB routines
  462. * 12-Jan-94 VB Created (from SLIB spec.)
  463. *
  464. * Author(s):
  465. * VB - Victor Bahl
  466. * PSG - Paul Gauthier
  467. --------------------------------------------------------------------------*/
  468. /*
  469. #define _SLIBDEBUG_
  470. */
  471. #include <stdio.h>
  472. #include <stdlib.h>
  473. #include <string.h>
  474. #include "SV.h"
  475. #include "sv_intrn.h"
  476. #ifdef JPEG_SUPPORT
  477. /*
  478. * More JPEG code needs to be moved to sv_jpeg_init file
  479. */
  480. #include "sv_jpeg_tables.h"
  481. #endif /* JPEG_SUPPORT */
  482. #include "sv_proto.h"
  483. #include "SC.h"
  484. #include "SC_conv.h"
  485. #include "SC_err.h"
  486. #ifdef WIN32
  487. #include <mmsystem.h>
  488. #endif
  489. #ifdef _SLIBDEBUG_
  490. #define _DEBUG_ 1 /* detailed debuging statements */
  491. #define _VERBOSE_ 1 /* show progress */
  492. #define _VERIFY_ 1 /* verify correct operation */
  493. #define _WARN_ 0 /* warnings about strange behavior */
  494. #endif
  495. static void sv_copy_bmh (
  496. BITMAPINFOHEADER *ImgFrom,
  497. BITMAPINFOHEADER *ImgTo);
  498. typedef struct SupportList_s {
  499. int InFormat; /* Input format */
  500. int InBits; /* Input number of bits */
  501. int OutFormat; /* Output format */
  502. int OutBits; /* Output number of bits */
  503. } SupportList_t;
  504. /*
  505. ** Input & Output Formats supported by SLIB Compression
  506. */
  507. static SupportList_t _SvCompressionSupport[] = {
  508. BI_DECYUVDIB, 16, JPEG_DIB, 8, /* YUV 4:2:2 Packed */
  509. BI_DECYUVDIB, 16, JPEG_DIB, 24, /* YUV 4:2:2 Packed */
  510. BI_DECYUVDIB, 16, MJPG_DIB, 24, /* YUV 4:2:2 Packed */
  511. BI_YUY2, 16, JPEG_DIB, 8, /* YUV 4:2:2 Packed */
  512. BI_YUY2, 16, JPEG_DIB, 24, /* YUV 4:2:2 Packed */
  513. BI_YUY2, 16, MJPG_DIB, 24, /* YUV 4:2:2 Packed */
  514. BI_S422, 16, JPEG_DIB, 8, /* YUV 4:2:2 Packed */
  515. BI_S422, 16, JPEG_DIB, 24, /* YUV 4:2:2 Packed */
  516. BI_S422, 16, MJPG_DIB, 24, /* YUV 4:2:2 Packed */
  517. BI_BITFIELDS, 32, JPEG_DIB, 8, /* BITFIELDS */
  518. BI_BITFIELDS, 32, JPEG_DIB, 24, /* BITFIELDS */
  519. BI_BITFIELDS, 32, MJPG_DIB, 24, /* BITFIELDS */
  520. BI_DECSEPRGBDIB, 32, JPEG_DIB, 8, /* RGB 32 Planar */
  521. BI_DECSEPRGBDIB, 32, JPEG_DIB, 24, /* RGB 32 Planar */
  522. BI_DECSEPRGBDIB, 32, MJPG_DIB, 24, /* RGB 32 Planar */
  523. #ifdef WIN32
  524. BI_RGB, 24, JPEG_DIB, 8, /* RGB 24 */
  525. BI_RGB, 24, JPEG_DIB, 24, /* RGB 24 */
  526. BI_RGB, 24, MJPG_DIB, 24, /* RGB 24 */
  527. BI_RGB, 32, JPEG_DIB, 8, /* RGB 32 */
  528. BI_RGB, 32, JPEG_DIB, 24, /* RGB 32 */
  529. BI_RGB, 32, MJPG_DIB, 24, /* RGB 32 */
  530. #endif /* WIN32 */
  531. #ifndef WIN32
  532. BI_DECGRAYDIB, 8, JPEG_DIB, 8, /* Gray 8 */
  533. BI_DECXIMAGEDIB, 24, JPEG_DIB, 8, /* XIMAGE 24 */
  534. BI_DECXIMAGEDIB, 24, JPEG_DIB, 24, /* XIMAGE 24 */
  535. BI_DECXIMAGEDIB, 24, MJPG_DIB, 24, /* XIMAGE 24 */
  536. #endif /* !WIN32 */
  537. #ifdef H261_SUPPORT
  538. BI_DECYUVDIB, 16, BI_DECH261DIB, 24, /* YUV 4:2:2 Packed */
  539. BI_YU12SEP, 24, BI_DECH261DIB, 24, /* YUV 4:1:1 Planar */
  540. BI_DECSEPYUV411DIB, 24, BI_DECH261DIB, 24, /* YUV 4:1:1 Planar */
  541. BI_YU16SEP, 24, BI_DECH261DIB, 24, /* YUV 4:2:2 Planar */
  542. BI_DECSEPYUVDIB, 24, BI_DECH261DIB, 24, /* YUV 4:2:2 Planar */
  543. #endif /* H261_SUPPORT */
  544. #ifdef H263_SUPPORT
  545. BI_DECYUVDIB, 16, BI_DECH263DIB, 24, /* YUV 4:2:2 Packed */
  546. BI_YU12SEP, 24, BI_DECH263DIB, 24, /* YUV 4:1:1 Planar */
  547. BI_DECSEPYUV411DIB, 24, BI_DECH263DIB, 24, /* YUV 4:1:1 Planar */
  548. BI_YU16SEP, 24, BI_DECH263DIB, 24, /* YUV 4:2:2 Planar */
  549. BI_DECSEPYUVDIB, 24, BI_DECH263DIB, 24, /* YUV 4:2:2 Planar */
  550. #endif /* H263_SUPPORT */
  551. #ifdef MPEG_SUPPORT
  552. BI_DECYUVDIB, 16, BI_DECMPEGDIB, 24, /* YUV 4:2:2 Packed */
  553. BI_YU12SEP, 24, BI_DECMPEGDIB, 24, /* YUV 4:1:1 Planar */
  554. BI_DECSEPYUV411DIB, 24, BI_DECMPEGDIB, 24, /* YUV 4:1:1 Planar */
  555. BI_YU16SEP, 24, BI_DECMPEGDIB, 24, /* YUV 4:2:2 Planar */
  556. BI_DECSEPYUVDIB, 24, BI_DECMPEGDIB, 24, /* YUV 4:2:2 Planar */
  557. BI_YVU9SEP, 24, BI_DECMPEGDIB, 24, /* YUV 16:1:1 Planar */
  558. BI_RGB, 24, BI_DECMPEGDIB, 24, /* RGB 24 */
  559. #endif /* MPEG_SUPPORT */
  560. #ifdef HUFF_SUPPORT
  561. BI_DECYUVDIB, 16, BI_DECHUFFDIB, 24, /* YUV 4:2:2 Packed */
  562. BI_YU12SEP, 24, BI_DECHUFFDIB, 24, /* YUV 4:1:1 Planar */
  563. BI_DECSEPYUV411DIB, 24, BI_DECHUFFDIB, 24, /* YUV 4:1:1 Planar */
  564. BI_YU16SEP, 24, BI_DECHUFFDIB, 24, /* YUV 4:2:2 Planar */
  565. BI_DECSEPYUVDIB, 24, BI_DECHUFFDIB, 24, /* YUV 4:2:2 Planar */
  566. #endif /* HUFF_SUPPORT */
  567. 0, 0, 0, 0
  568. };
  569. /*
  570. ** Input & Output Formats supported by SLIB Decompression
  571. */
  572. static SupportList_t _SvDecompressionSupport[] = {
  573. #ifdef JPEG_SUPPORT
  574. JPEG_DIB, 8, BI_DECSEPYUVDIB, 24, /* YUV 4:2:2 Planar */
  575. JPEG_DIB, 24, BI_DECSEPYUVDIB, 24, /* YUV 4:2:2 Planar */
  576. MJPG_DIB, 24, BI_DECSEPYUVDIB, 24, /* YUV 4:2:2 Planar */
  577. JPEG_DIB, 8, BI_YU16SEP, 24, /* YUV 4:2:2 Planar */
  578. JPEG_DIB, 24, BI_YU16SEP, 24, /* YUV 4:2:2 Planar */
  579. MJPG_DIB, 24, BI_YU16SEP, 24, /* YUV 4:2:2 Planar */
  580. JPEG_DIB, 8, BI_DECYUVDIB, 16, /* YUV 4:2:2 Packed */
  581. JPEG_DIB, 24, BI_DECYUVDIB, 16, /* YUV 4:2:2 Packed */
  582. MJPG_DIB, 24, BI_DECYUVDIB, 16, /* YUV 4:2:2 Packed */
  583. JPEG_DIB, 8, BI_YUY2, 16, /* YUV 4:2:2 Packed */
  584. JPEG_DIB, 24, BI_YUY2, 16, /* YUV 4:2:2 Packed */
  585. MJPG_DIB, 24, BI_YUY2, 16, /* YUV 4:2:2 Packed */
  586. JPEG_DIB, 8, BI_BITFIELDS, 32, /* BITFIELDS */
  587. JPEG_DIB, 24, BI_BITFIELDS, 32, /* BITFIELDS */
  588. MJPG_DIB, 24, BI_BITFIELDS, 32, /* BITFIELDS */
  589. JPEG_DIB, 8, BI_DECGRAYDIB, 8, /* Gray 8 */
  590. #endif /* JPEG_SUPPORT */
  591. #ifdef WIN32
  592. JPEG_DIB, 8, BI_RGB, 16, /* RGB 16 */
  593. JPEG_DIB, 24, BI_RGB, 16, /* RGB 16 */
  594. MJPG_DIB, 24, BI_RGB, 16, /* RGB 16 */
  595. JPEG_DIB, 8, BI_RGB, 24, /* RGB 24 */
  596. JPEG_DIB, 24, BI_RGB, 24, /* RGB 24 */
  597. MJPG_DIB, 24, BI_RGB, 24, /* RGB 24 */
  598. JPEG_DIB, 8, BI_RGB, 32, /* RGB 32 */
  599. JPEG_DIB, 24, BI_RGB, 32, /* RGB 32 */
  600. MJPG_DIB, 24, BI_RGB, 32, /* RGB 32 */
  601. JPEG_DIB, 8, BI_BITFIELDS, 16, /* BITFIELDS */
  602. #ifdef H261_SUPPORT
  603. BI_DECH261DIB, 24, BI_RGB, 16, /* RGB 16 */
  604. BI_DECH261DIB, 24, BI_RGB, 24, /* RGB 24 */
  605. BI_DECH261DIB, 24, BI_RGB, 32, /* RGB 32 */
  606. #endif /* H261_SUPPORT */
  607. #ifdef MPEG_SUPPORT
  608. BI_DECMPEGDIB, 24, BI_RGB, 16, /* RGB 16 */
  609. BI_DECMPEGDIB, 24, BI_RGB, 24, /* RGB 24 */
  610. BI_DECMPEGDIB, 24, BI_RGB, 32, /* RGB 32 */
  611. #endif /* MPEG_SUPPORT */
  612. #endif /* WIN32 */
  613. #ifndef WIN32
  614. JPEG_DIB, 8, BI_DECXIMAGEDIB, 24, /* XIMAGE 24 */
  615. JPEG_DIB, 24, BI_DECXIMAGEDIB, 24, /* XIMAGE 24 */
  616. MJPG_DIB, 24, BI_DECXIMAGEDIB, 24, /* XIMAGE 24 */
  617. #ifdef H261_SUPPORT
  618. BI_DECH261DIB, 24, BI_DECXIMAGEDIB, 24, /* XIMAGE 24 */
  619. #endif /* H261_SUPPORT */
  620. #ifdef MPEG_SUPPORT
  621. BI_DECMPEGDIB, 24, BI_DECXIMAGEDIB, 24, /* XIMAGE 24 */
  622. #endif /* MPEG_SUPPORT */
  623. #endif /* !WIN32 */
  624. #ifdef H261_SUPPORT
  625. BI_DECH261DIB, 24, BI_YU12SEP, 24, /* YUV 4:1:1 Planar */
  626. BI_DECH261DIB, 24, BI_DECSEPYUV411DIB, 24, /* YUV 4:1:1 Planar */
  627. BI_DECH261DIB, 24, BI_DECYUVDIB, 16, /* YUV 4:2:2 Packed */
  628. BI_DECH261DIB, 24, BI_BITFIELDS, 32, /* BITFIELDS */
  629. #endif /* H261_SUPPORT */
  630. #ifdef H263_SUPPORT
  631. BI_DECH263DIB, 24, BI_YU12SEP, 24, /* YUV 4:1:1 Planar */
  632. BI_DECH263DIB, 24, BI_DECSEPYUV411DIB, 24, /* YUV 4:1:1 Planar */
  633. BI_DECH263DIB, 24, BI_YUY2, 16, /* YUV 4:2:2 Packed */
  634. BI_DECH263DIB, 24, BI_DECYUVDIB, 16, /* YUV 4:2:2 Packed */
  635. #endif /* H263_SUPPORT */
  636. #ifdef MPEG_SUPPORT
  637. BI_DECMPEGDIB, 24, BI_YU12SEP, 24, /* YUV 4:1:1 Planar */
  638. BI_DECMPEGDIB, 24, BI_DECSEPYUV411DIB, 24, /* YUV 4:1:1 Planar */
  639. BI_DECMPEGDIB, 24, BI_DECYUVDIB, 16, /* YUV 4:2:2 Packed */
  640. BI_DECMPEGDIB, 24, BI_YUY2, 16, /* YUV 4:2:2 Packed */
  641. BI_DECMPEGDIB, 24, BI_YU16SEP, 24, /* YUV 4:2:2 Planar */
  642. BI_DECMPEGDIB, 24, BI_BITFIELDS, 32, /* BITFIELDS */
  643. #endif /* MPEG_SUPPORT */
  644. #ifdef HUFF_SUPPORT
  645. BI_DECHUFFDIB, 24, BI_YU12SEP, 24, /* YUV 4:1:1 Planar */
  646. BI_DECHUFFDIB, 24, BI_DECSEPYUV411DIB, 24, /* YUV 4:1:1 Planar */
  647. BI_DECHUFFDIB, 24, BI_DECYUVDIB, 16, /* YUV 4:2:2 Packed */
  648. #endif /* HUFF_SUPPORT */
  649. 0, 0, 0, 0
  650. };
  651. /*
  652. ** Name: IsSupported
  653. ** Desc: Lookup the a given input and output format to see if it
  654. ** exists in a SupportList.
  655. ** Note: If OutFormat==-1 and OutBits==-1 then only input format
  656. ** is checked for support.
  657. ** If InFormat==-1 and InBits==-1 then only output format
  658. ** is checked for support.
  659. ** Return: NULL Formats not supported.
  660. ** not NULL A pointer to the list entry.
  661. */
  662. static SupportList_t *IsSupported(SupportList_t *list,
  663. int InFormat, int InBits,
  664. int OutFormat, int OutBits)
  665. {
  666. if (OutFormat==-1 && OutBits==-1) /* Looking up only the Input format */
  667. {
  668. while (list->InFormat || list->InBits)
  669. if (list->InFormat == InFormat && list->InBits==InBits)
  670. return(list);
  671. else
  672. list++;
  673. return(NULL);
  674. }
  675. if (InFormat==-1 && InBits==-1) /* Looking up only the Output format */
  676. {
  677. while (list->InFormat || list->InBits)
  678. if (list->OutFormat == OutFormat && list->OutBits==OutBits)
  679. return(list);
  680. else
  681. list++;
  682. return(NULL);
  683. }
  684. /* Looking up both Input and Output */
  685. while (list->InFormat || list->InBits)
  686. if (list->InFormat == InFormat && list->InBits==InBits &&
  687. list->OutFormat == OutFormat && list->OutBits==OutBits)
  688. return(list);
  689. else
  690. list++;
  691. return(NULL);
  692. }
  693. /*
  694. ** Name: SvOpenCodec
  695. ** Purpose: Open the specified codec. Return stat code.
  696. **
  697. ** Args: CodecType = i.e. SV_JPEG_ENCODE, SV_JPEG_DECODE, etc.
  698. ** Svh = handle to software codec's Info structure.
  699. */
  700. SvStatus_t SvOpenCodec (SvCodecType_e CodecType, SvHandle_t *Svh)
  701. {
  702. SvCodecInfo_t *Info = NULL;
  703. _SlibDebug(_DEBUG_, printf("SvOpenCodec()\n") );
  704. /* check if CODEC is supported */
  705. switch (CodecType)
  706. {
  707. #ifdef JPEG_SUPPORT
  708. case SV_JPEG_DECODE:
  709. case SV_JPEG_ENCODE:
  710. break;
  711. #endif /* JPEG_SUPPORT */
  712. #ifdef H261_SUPPORT
  713. case SV_H261_ENCODE:
  714. case SV_H261_DECODE:
  715. break;
  716. #endif /* H261_SUPPORT */
  717. #ifdef H263_SUPPORT
  718. case SV_H263_ENCODE:
  719. case SV_H263_DECODE:
  720. break;
  721. #endif /* H263_SUPPORT */
  722. #ifdef MPEG_SUPPORT
  723. case SV_MPEG_ENCODE:
  724. case SV_MPEG_DECODE:
  725. case SV_MPEG2_ENCODE:
  726. case SV_MPEG2_DECODE:
  727. break;
  728. #endif /* MPEG_SUPPORT */
  729. #ifdef HUFF_SUPPORT
  730. case SV_HUFF_ENCODE:
  731. case SV_HUFF_DECODE:
  732. break;
  733. #endif /* HUFF_SUPPORT */
  734. default:
  735. return(SvErrorCodecType);
  736. }
  737. if (!Svh)
  738. return (SvErrorBadPointer);
  739. /*
  740. ** Allocate memory for the Codec Info structure:
  741. */
  742. if ((Info = (SvCodecInfo_t *) ScAlloc(sizeof(SvCodecInfo_t))) == NULL)
  743. return (SvErrorMemory);
  744. memset (Info, 0, sizeof(SvCodecInfo_t));
  745. Info->BSIn=NULL;
  746. Info->BSOut=NULL;
  747. Info->mode = CodecType;
  748. Info->started = FALSE;
  749. /*
  750. ** Allocate memory for Info structure and clear it
  751. */
  752. switch (CodecType)
  753. {
  754. #ifdef JPEG_SUPPORT
  755. case SV_JPEG_DECODE:
  756. if ((Info->jdcmp = (SvJpegDecompressInfo_t *)
  757. ScAlloc(sizeof(SvJpegDecompressInfo_t))) == NULL)
  758. return(SvErrorMemory);
  759. memset (Info->jdcmp, 0, sizeof(SvJpegDecompressInfo_t));
  760. break;
  761. case SV_JPEG_ENCODE:
  762. if ((Info->jcomp = (SvJpegCompressInfo_t *)
  763. ScAlloc(sizeof(SvJpegCompressInfo_t))) == NULL)
  764. return (SvErrorMemory);
  765. memset (Info->jcomp, 0, sizeof(SvJpegCompressInfo_t));
  766. break;
  767. #endif /* JPEG_SUPPORT */
  768. #ifdef MPEG_SUPPORT
  769. case SV_MPEG_DECODE:
  770. case SV_MPEG2_DECODE:
  771. if ((Info->mdcmp = (SvMpegDecompressInfo_t *)
  772. ScAlloc(sizeof(SvMpegDecompressInfo_t))) == NULL)
  773. return(SvErrorMemory);
  774. memset (Info->mdcmp, 0, sizeof(SvMpegDecompressInfo_t));
  775. Info->mdcmp->timecode0 = 0;
  776. Info->mdcmp->timecode_state = MPEG_TIMECODE_START;
  777. Info->mdcmp->timecodefps = 0.0F;
  778. Info->mdcmp->fps = 0.0F;
  779. Info->mdcmp->twostreams = 0;
  780. Info->mdcmp->verbose=FALSE;
  781. Info->mdcmp->quiet=TRUE;
  782. ScBufQueueCreate(&Info->BufQ);
  783. ScBufQueueCreate(&Info->ImageQ);
  784. break;
  785. case SV_MPEG_ENCODE:
  786. case SV_MPEG2_ENCODE:
  787. if ((Info->mcomp = (SvMpegCompressInfo_t *)
  788. ScAlloc(sizeof(SvMpegCompressInfo_t))) == NULL)
  789. return(SvErrorMemory);
  790. memset (Info->mcomp, 0, sizeof(SvMpegCompressInfo_t));
  791. Info->mcomp->quiet=1;
  792. SvSetParamInt((SvHandle_t)Info, SV_PARAM_QUALITY, 100);
  793. SvSetParamBoolean((SvHandle_t)Info, SV_PARAM_FASTENCODE, FALSE);
  794. SvSetParamBoolean((SvHandle_t)Info, SV_PARAM_FASTDECODE, FALSE);
  795. SvSetParamInt((SvHandle_t)Info, SV_PARAM_MOTIONALG, 0);
  796. SvSetParamInt((SvHandle_t)Info, SV_PARAM_ALGFLAGS,
  797. PARAM_ALGFLAG_HALFPEL);
  798. SvSetParamInt((SvHandle_t)Info, SV_PARAM_BITRATE, 1152000);
  799. SvSetParamFloat((SvHandle_t)Info, SV_PARAM_FPS, (float)25.0);
  800. SvSetParamInt((SvHandle_t)Info, SV_PARAM_KEYSPACING, 12);
  801. SvSetParamInt((SvHandle_t)Info, SV_PARAM_SUBKEYSPACING, 4);
  802. ScBufQueueCreate(&Info->BufQ);
  803. ScBufQueueCreate(&Info->ImageQ);
  804. break;
  805. #endif /* MPEG_SUPPORT */
  806. #ifdef H261_SUPPORT
  807. case SV_H261_DECODE:
  808. if ((Info->h261 = (SvH261Info_t *)
  809. ScAlloc(sizeof(SvH261Info_t))) == NULL)
  810. return(SvErrorMemory);
  811. memset (Info->h261, 0, sizeof(SvH261Info_t));
  812. Info->h261->inited=FALSE;
  813. SvSetParamInt((SvHandle_t)Info, SV_PARAM_QUALITY, 100);
  814. ScBufQueueCreate(&Info->BufQ);
  815. ScBufQueueCreate(&Info->ImageQ);
  816. break;
  817. case SV_H261_ENCODE:
  818. if ((Info->h261 = (SvH261Info_t *)
  819. ScAlloc(sizeof(SvH261Info_t))) == NULL)
  820. return(SvErrorMemory);
  821. memset (Info->h261, 0, sizeof(SvH261Info_t));
  822. Info->h261->inited=FALSE;
  823. SvSetParamInt((SvHandle_t)Info, SV_PARAM_QUALITY, 100);
  824. SvSetParamInt((SvHandle_t)Info, SV_PARAM_MOTIONALG, ME_BRUTE);
  825. SvSetParamInt((SvHandle_t)Info, SV_PARAM_MOTIONSEARCH, 5);
  826. SvSetParamInt((SvHandle_t)Info, SV_PARAM_MOTIONTHRESH, 600);
  827. SvSetParamInt((SvHandle_t)Info, SV_PARAM_ALGFLAGS, 0);
  828. SvSetParamInt((SvHandle_t)Info, SV_PARAM_BITRATE, 352000);
  829. SvSetParamInt((SvHandle_t)Info, SV_PARAM_QUANTI, 10); /* for VBR */
  830. SvSetParamInt((SvHandle_t)Info, SV_PARAM_QUANTP, 10);
  831. SvSetParamInt((SvHandle_t)Info, SV_PARAM_PACKETSIZE, 512);
  832. SvSetParamFloat((SvHandle_t)Info, SV_PARAM_FPS, (float)15.0);
  833. ScBufQueueCreate(&Info->BufQ);
  834. ScBufQueueCreate(&Info->ImageQ);
  835. break;
  836. #endif /* H261_SUPPORT */
  837. #ifdef H263_SUPPORT
  838. case SV_H263_DECODE:
  839. if ((Info->h263dcmp = (SvH263DecompressInfo_t *)
  840. ScAlloc(sizeof(SvH263DecompressInfo_t))) == NULL)
  841. return(SvErrorMemory);
  842. memset (Info->h263dcmp, 0, sizeof(SvH263DecompressInfo_t));
  843. Info->h263dcmp->inited=FALSE;
  844. ScBufQueueCreate(&Info->BufQ);
  845. ScBufQueueCreate(&Info->ImageQ);
  846. break;
  847. case SV_H263_ENCODE:
  848. if ((Info->h263comp = (SvH263CompressInfo_t *)
  849. ScAlloc(sizeof(SvH263CompressInfo_t))) == NULL)
  850. return(SvErrorMemory);
  851. memset (Info->h263comp, 0, sizeof(SvH263CompressInfo_t));
  852. Info->h263comp->inited=FALSE;
  853. SvSetParamInt((SvHandle_t)Info, SV_PARAM_MOTIONALG, 0);
  854. SvSetParamInt((SvHandle_t)Info, SV_PARAM_BITRATE, 0);
  855. SvSetParamFloat((SvHandle_t)Info, SV_PARAM_FPS, (float)30.0);
  856. SvSetParamInt((SvHandle_t)Info, SV_PARAM_ALGFLAGS, 0);
  857. SvSetParamInt((SvHandle_t)Info, SV_PARAM_PACKETSIZE, 512);
  858. SvSetParamInt((SvHandle_t)Info, SV_PARAM_QUANTI, 10);
  859. SvSetParamInt((SvHandle_t)Info, SV_PARAM_QUANTP, 10);
  860. SvSetParamInt((SvHandle_t)Info, SV_PARAM_QUALITY, 0);
  861. SvSetParamInt((SvHandle_t)Info, SV_PARAM_KEYSPACING, 120);
  862. ScBufQueueCreate(&Info->BufQ);
  863. ScBufQueueCreate(&Info->ImageQ);
  864. break;
  865. #endif /* H263_SUPPORT */
  866. #ifdef HUFF_SUPPORT
  867. case SV_HUFF_DECODE:
  868. case SV_HUFF_ENCODE:
  869. if ((Info->huff = (SvHuffInfo_t *)
  870. ScAlloc(sizeof(SvHuffInfo_t))) == NULL)
  871. return(SvErrorMemory);
  872. memset (Info->huff, 0, sizeof(SvHuffInfo_t));
  873. ScBufQueueCreate(&Info->BufQ);
  874. ScBufQueueCreate(&Info->ImageQ);
  875. break;
  876. #endif /* HUFF_SUPPORT */
  877. }
  878. *Svh = (SvHandle_t) Info; /* Return handle */
  879. _SlibDebug(_DEBUG_, printf("SvOpenCodec() returns Svh=%p\n", *Svh) );
  880. return(NoErrors);
  881. }
  882. /*
  883. ** Name: SvCloseCodec
  884. ** Purpose: Closes the specified codec. Free the Info structure
  885. **
  886. ** Args: Svh = handle to software codec's Info structure.
  887. **
  888. ** XXX - needs to change since now we have compression also, i.e.
  889. ** Svh should be handle to the CodecInfo structure. (VB)
  890. */
  891. SvStatus_t SvCloseCodec (SvHandle_t Svh)
  892. {
  893. SvCodecInfo_t *Info = (SvCodecInfo_t *)Svh;
  894. _SlibDebug(_DEBUG_, printf("SvCloseCodec()\n") );
  895. if (!Info)
  896. return(SvErrorCodecHandle);
  897. if (Info->BSIn)
  898. ScBSDestroy(Info->BSIn);
  899. if (Info->BSOut)
  900. ScBSDestroy(Info->BSOut);
  901. if (Info->BufQ);
  902. ScBufQueueDestroy(Info->BufQ);
  903. if (Info->ImageQ);
  904. ScBufQueueDestroy(Info->ImageQ);
  905. switch (Info->mode) /* free all associated codec memory */
  906. {
  907. #ifdef JPEG_SUPPORT
  908. case SV_JPEG_DECODE:
  909. {
  910. int i;
  911. for (i = 0; i < 4; i++) {
  912. if (Info->jdcmp->DcHt[i])
  913. ScPaFree(Info->jdcmp->DcHt[i]);
  914. if (Info->jdcmp->AcHt[i])
  915. ScPaFree(Info->jdcmp->AcHt[i]);
  916. }
  917. if (Info->jdcmp->compinfo)
  918. ScFree(Info->jdcmp->compinfo);
  919. if (Info->jdcmp) {
  920. if (Info->jdcmp->TempImage)
  921. ScPaFree(Info->jdcmp->TempImage);
  922. if (Info->jdcmp->_SvBlockPtr)
  923. ScFree(Info->jdcmp->_SvBlockPtr);
  924. ScFree(Info->jdcmp);
  925. }
  926. }
  927. break;
  928. case SV_JPEG_ENCODE:
  929. {
  930. int i;
  931. for (i = 0 ; i < Info->jcomp->NumComponents ; i++)
  932. if (Info->jcomp->Qt[i])
  933. ScPaFree(Info->jcomp->Qt[i]);
  934. for (i = 0; i < 4; i++) {
  935. if (Info->jcomp->DcHt[i])
  936. ScPaFree(Info->jcomp->DcHt[i]);
  937. if (Info->jcomp->AcHt[i])
  938. ScPaFree(Info->jcomp->AcHt[i]);
  939. }
  940. if (Info->jcomp->compinfo)
  941. ScFree(Info->jcomp->compinfo);
  942. if (Info->jcomp) {
  943. if (Info->jcomp->BlkBuffer)
  944. ScPaFree(Info->jcomp->BlkBuffer);
  945. if (Info->jcomp->BlkTable)
  946. ScPaFree(Info->jcomp->BlkTable);
  947. ScFree(Info->jcomp);
  948. }
  949. }
  950. break;
  951. #endif /* JPEG_SUPPORT */
  952. #ifdef H261_SUPPORT
  953. case SV_H261_ENCODE:
  954. if (Info->h261)
  955. {
  956. svH261CompressFree(Info);
  957. ScFree(Info->h261);
  958. }
  959. break;
  960. case SV_H261_DECODE:
  961. if (Info->h261)
  962. {
  963. svH261DecompressFree(Info);
  964. ScFree(Info->h261);
  965. }
  966. break;
  967. #endif /* H261_SUPPORT */
  968. #ifdef H263_SUPPORT
  969. case SV_H263_DECODE:
  970. if (Info->h263dcmp)
  971. {
  972. svH263FreeDecompressor(Info);
  973. ScFree(Info->h263dcmp);
  974. }
  975. break;
  976. case SV_H263_ENCODE:
  977. if (Info->h263comp)
  978. {
  979. svH263FreeCompressor(Info);
  980. ScFree(Info->h263comp);
  981. }
  982. break;
  983. #endif /* H263_SUPPORT */
  984. #ifdef MPEG_SUPPORT
  985. case SV_MPEG_DECODE:
  986. case SV_MPEG2_DECODE:
  987. if (Info->mdcmp) {
  988. sv_MpegFreeDecoder(Info);
  989. ScFree(Info->mdcmp);
  990. }
  991. break;
  992. case SV_MPEG_ENCODE:
  993. case SV_MPEG2_ENCODE:
  994. if (Info->mcomp) {
  995. ScFree(Info->mcomp);
  996. }
  997. break;
  998. #endif /* MPEG_SUPPORT */
  999. #ifdef HUFF_SUPPORT
  1000. case SV_HUFF_DECODE:
  1001. case SV_HUFF_ENCODE:
  1002. if (Info->huff) {
  1003. sv_HuffFreeDecoder(Info);
  1004. ScFree(Info->huff);
  1005. }
  1006. break;
  1007. break;
  1008. #endif /* HUFF_SUPPORT */
  1009. }
  1010. /*
  1011. ** Free Info structure
  1012. */
  1013. ScFree(Info);
  1014. return(NoErrors);
  1015. }
  1016. /*
  1017. ** Name: SvDecompressBegin
  1018. ** Purpose: Initialize the Decompression Codec. Call after SvOpenCodec &
  1019. ** before SvDecompress (SvDecompress will call SvDecompressBegin
  1020. ** on first call to codec after open if user doesn't call it)
  1021. **
  1022. ** Args: Svh = handle to software codec's Info structure.
  1023. ** ImgIn = format of input (uncompressed) image
  1024. ** ImgOut = format of output (compressed) image
  1025. */
  1026. SvStatus_t SvDecompressBegin (SvHandle_t Svh, BITMAPINFOHEADER *ImgIn,
  1027. BITMAPINFOHEADER *ImgOut)
  1028. {
  1029. int stat;
  1030. SvCodecInfo_t *Info = (SvCodecInfo_t *)Svh;
  1031. _SlibDebug(_DEBUG_, printf("SvDecompressBegin()\n") );
  1032. if (!Info)
  1033. return(SvErrorCodecHandle);
  1034. if (Info->started)
  1035. return(SvErrorNone);
  1036. /* if no Image header provided, use previous headers */
  1037. if (!ImgIn)
  1038. ImgIn = &Info->InputFormat;
  1039. if (!ImgOut)
  1040. ImgOut = &Info->OutputFormat;
  1041. stat=SvDecompressQuery (Svh, ImgIn, ImgOut);
  1042. RETURN_ON_ERROR(stat);
  1043. /*
  1044. ** Save input & output formats for SvDecompress
  1045. */
  1046. sv_copy_bmh(ImgIn, &Info->InputFormat);
  1047. sv_copy_bmh(ImgOut, &Info->OutputFormat);
  1048. Info->Width = Info->InputFormat.biWidth;
  1049. Info->Height = abs(Info->InputFormat.biHeight);
  1050. switch (Info->mode)
  1051. {
  1052. #ifdef JPEG_SUPPORT
  1053. case SV_JPEG_DECODE:
  1054. {
  1055. SvJpegDecompressInfo_t *DInfo;
  1056. /*
  1057. ** Load the default Huffman tablse
  1058. */
  1059. stat = sv_LoadDefaultHTable (Info);
  1060. RETURN_ON_ERROR (stat);
  1061. stat = sv_InitJpegDecoder (Info);
  1062. RETURN_ON_ERROR (stat);
  1063. /*
  1064. ** Video-specific information will be filled in during processing
  1065. ** of first frame
  1066. */
  1067. DInfo = Info->jdcmp;
  1068. DInfo->InfoFilled = 0;
  1069. DInfo->ReInit = 1;
  1070. DInfo->DecompressStarted = TRUE;
  1071. DInfo->TempImage = NULL;
  1072. break;
  1073. }
  1074. #endif
  1075. #ifdef MPEG_SUPPORT
  1076. case SV_MPEG_DECODE:
  1077. case SV_MPEG2_DECODE:
  1078. Info->mdcmp->DecompressStarted = TRUE;
  1079. /* the default data source is the buffer queue */
  1080. if (Info->BSIn)
  1081. ScBSReset(Info->BSIn);
  1082. else
  1083. stat=SvSetDataSource (Svh, SV_USE_BUFFER_QUEUE, 0, NULL, 0);
  1084. RETURN_ON_ERROR (stat);
  1085. stat = sv_MpegInitDecoder(Info);
  1086. RETURN_ON_ERROR (stat);
  1087. break;
  1088. #endif /* MPEG_SUPPORT */
  1089. #ifdef H261_SUPPORT
  1090. case SV_H261_DECODE:
  1091. stat = svH261Init(Info);
  1092. RETURN_ON_ERROR (stat);
  1093. break;
  1094. #endif /* H261_SUPPORT */
  1095. #ifdef H263_SUPPORT
  1096. case SV_H263_DECODE:
  1097. stat = svH263InitDecompressor(Info);
  1098. RETURN_ON_ERROR (stat);
  1099. break;
  1100. #endif /* H263_SUPPORT */
  1101. #ifdef HUFF_SUPPORT
  1102. case SV_HUFF_DECODE:
  1103. Info->huff->DecompressStarted = TRUE;
  1104. /* the default data source is the buffer queue */
  1105. if (Info->BSIn)
  1106. ScBSReset(Info->BSIn);
  1107. else
  1108. stat=SvSetDataSource (Svh, SV_USE_BUFFER_QUEUE, 0, NULL, 0);
  1109. RETURN_ON_ERROR (stat);
  1110. stat = sv_HuffInitDecoder(Info);
  1111. RETURN_ON_ERROR (stat);
  1112. break;
  1113. #endif /* HUFF_SUPPORT */
  1114. }
  1115. Info->started=TRUE;
  1116. return (NoErrors);
  1117. }
  1118. /*
  1119. ** Name: SvGetDecompressSize
  1120. ** Purpose: Return minimum data buffer size to receive decompressed data
  1121. ** for current settings on codec
  1122. **
  1123. ** Args: Svh = handle to software codec's Info structure.
  1124. ** MinSize = Returns minimum buffer size required
  1125. */
  1126. SvStatus_t SvGetDecompressSize (SvHandle_t Svh, int *MinSize)
  1127. {
  1128. int pixels,lines;
  1129. SvCodecInfo_t *Info = (SvCodecInfo_t *)Svh;
  1130. if (!Info)
  1131. return(SvErrorCodecHandle);
  1132. switch (Info->mode) /* check that decompressor was started */
  1133. {
  1134. #ifdef JPEG_SUPPORT
  1135. case SV_JPEG_DECODE:
  1136. if (!Info->jdcmp->DecompressStarted)
  1137. return(SvErrorDcmpNotStarted);
  1138. break;
  1139. #endif /* JPEG_SUPPORT */
  1140. #ifdef MPEG_SUPPORT
  1141. case SV_MPEG_DECODE:
  1142. case SV_MPEG2_DECODE:
  1143. if (!Info->mdcmp->DecompressStarted)
  1144. return(SvErrorDcmpNotStarted);
  1145. break;
  1146. #endif /* MPEG_SUPPORT */
  1147. default:
  1148. break;
  1149. }
  1150. if (!MinSize)
  1151. return(SvErrorBadPointer);
  1152. pixels = Info->OutputFormat.biWidth;
  1153. lines = Info->OutputFormat.biHeight;
  1154. if (lines < 0) lines = -lines;
  1155. _SlibDebug(_VERBOSE_,
  1156. printf("OutputFormat.biWidth=%d OutputFormat.biHeight=%d\n",
  1157. pixels, lines) );
  1158. switch (Info->mode)
  1159. {
  1160. #ifdef JPEG_SUPPORT
  1161. case SV_JPEG_DECODE:
  1162. /*
  1163. ** On output, accept: 8, 16 or 24 bit uncompressed RGB
  1164. ** or YUV formats, 32 bit uncompressed RGB
  1165. */
  1166. if (Info->OutputFormat.biBitCount == 8)
  1167. *MinSize = pixels * lines;
  1168. else if (Info->OutputFormat.biBitCount == 24) {
  1169. if (Info->OutputFormat.biCompression == BI_RGB)
  1170. *MinSize = 3 * pixels * lines;
  1171. else if (Info->OutputFormat.biCompression == BI_DECSEPRGBDIB)
  1172. *MinSize = 3 * pixels * lines;
  1173. else if (IsYUV422Packed(Info->OutputFormat.biCompression))
  1174. *MinSize = 2 * pixels * lines;
  1175. else if (Info->OutputFormat.biCompression == BI_DECXIMAGEDIB)
  1176. *MinSize = 4 * pixels * lines;
  1177. else if (IsYUV422Sep(Info->OutputFormat.biCompression))
  1178. *MinSize = 2 * pixels * lines;
  1179. else if (IsYUV411Sep(Info->OutputFormat.biCompression))
  1180. *MinSize = 2 * pixels * lines;
  1181. else
  1182. return(SvErrorUnrecognizedFormat);
  1183. }
  1184. else if (Info->OutputFormat.biBitCount == 16) {
  1185. if (IsYUV422Packed(Info->OutputFormat.biCompression))
  1186. *MinSize = 2 * pixels * lines;
  1187. else if (Info->OutputFormat.biCompression == BI_RGB)
  1188. *MinSize = 2 * pixels * lines;
  1189. }
  1190. else if (Info->OutputFormat.biBitCount == 32) {
  1191. if (Info->OutputFormat.biCompression == BI_RGB ||
  1192. Info->OutputFormat.biCompression == BI_BITFIELDS)
  1193. *MinSize = 4 * pixels * lines;
  1194. }
  1195. break;
  1196. #endif /* JPEG_SUPPORT */
  1197. #ifdef MPEG_SUPPORT
  1198. case SV_MPEG_DECODE:
  1199. case SV_MPEG2_DECODE:
  1200. /*
  1201. ** MPEG multibuffer size = 3 pictures*(1Y + 1/4 U + 1/4 V)*imagesize
  1202. */
  1203. if (IsYUV422Sep(SvGetParamInt(Svh, SV_PARAM_FINALFORMAT)) ||
  1204. IsYUV422Packed(SvGetParamInt(Svh, SV_PARAM_FINALFORMAT)))
  1205. *MinSize = 3 * pixels * lines * 2; /* 4:2:2 */
  1206. else
  1207. *MinSize = 3 * (pixels * lines * 3)/2; /* 4:1:1 */
  1208. break;
  1209. #endif /* MPEG_SUPPORT */
  1210. #ifdef H261_SUPPORT
  1211. case SV_H261_DECODE:
  1212. *MinSize = 3 * (pixels * lines * 3)/2; /* 4:1:1 */
  1213. break;
  1214. #endif /* H261_SUPPORT */
  1215. #ifdef H263_SUPPORT
  1216. case SV_H263_DECODE:
  1217. *MinSize = 3 * (pixels * lines * 3)/2; /* 4:1:1 */
  1218. break;
  1219. #endif /* H263_SUPPORT */
  1220. default:
  1221. return(SvErrorUnrecognizedFormat);
  1222. }
  1223. return(NoErrors);
  1224. }
  1225. /*
  1226. ** Name: SvDecompressQuery
  1227. ** Purpose: Determine if Codec can decompress desired format
  1228. **
  1229. ** Args: Svh = handle to software codec's Info structure.
  1230. ** ImgIn = Pointer to BITMAPINFOHEADER structure describing format
  1231. ** ImgOut = Pointer to BITMAPINFOHEADER structure describing format
  1232. */
  1233. SvStatus_t SvDecompressQuery (SvHandle_t Svh, BITMAPINFOHEADER *ImgIn,
  1234. BITMAPINFOHEADER *ImgOut)
  1235. {
  1236. SvCodecInfo_t *Info = (SvCodecInfo_t *)Svh;
  1237. if (!Info)
  1238. return(SvErrorCodecHandle);
  1239. if (!ImgIn && !ImgOut)
  1240. return(SvErrorBadPointer);
  1241. if (!IsSupported(_SvDecompressionSupport,
  1242. ImgIn ? ImgIn->biCompression : -1,
  1243. ImgIn ? ImgIn->biBitCount : -1,
  1244. ImgOut ? ImgOut->biCompression : -1,
  1245. ImgOut ? ImgOut->biBitCount : -1))
  1246. return(SvErrorUnrecognizedFormat);
  1247. if (ImgOut) /* Query output format */
  1248. {
  1249. /*
  1250. ** XXX - check to see if the # of output lines/# of output
  1251. ** pixels/line are a multiple of 8. If not can't decompress
  1252. ** Note: This is an artifical restriction since the JPEG
  1253. ** bitream will always be a multiple of 8x8, so we
  1254. ** should have no problems decoding, only on the
  1255. ** output will be have to add an extra copy operation
  1256. ** XXX - will address/remove this restriction in the
  1257. ** later release (VB)
  1258. */
  1259. if (ImgOut->biWidth <= 0 || ImgOut->biHeight == 0)
  1260. return(SvErrorBadImageSize);
  1261. switch (Info->mode)
  1262. {
  1263. #ifdef JPEG_SUPPORT
  1264. case SV_JPEG_DECODE: /* 8x8 restriction */
  1265. if ((ImgOut->biWidth%8) || (ImgOut->biHeight%8))
  1266. return(SvErrorBadImageSize);
  1267. break;
  1268. #endif /* JPEG_SUPPORT */
  1269. #ifdef MPEG_SUPPORT
  1270. case SV_MPEG_DECODE:
  1271. case SV_MPEG2_DECODE:
  1272. /* MPEG 16x16 - because of Software Renderer YUV limitation */
  1273. if ((ImgOut->biWidth%16) || (ImgOut->biHeight%16))
  1274. return(SvErrorBadImageSize);
  1275. /* Reject requests to scale during decompression - renderer's job */
  1276. if (ImgIn && ImgOut &&
  1277. (ImgIn->biWidth != ImgOut->biWidth) ||
  1278. (abs(ImgIn->biHeight) != abs(ImgOut->biHeight)))
  1279. return (SvErrorBadImageSize);
  1280. break;
  1281. #endif /* MPEG_SUPPORT */
  1282. #ifdef H261_SUPPORT
  1283. case SV_H261_DECODE:
  1284. /* H261 16x16 - because of Software Renderer YUV limitation */
  1285. if ((ImgOut->biWidth%16) || (ImgOut->biHeight%16))
  1286. return(SvErrorBadImageSize);
  1287. if ((ImgOut->biWidth!=CIF_WIDTH && ImgOut->biWidth!=QCIF_WIDTH) ||
  1288. (abs(ImgOut->biHeight)!=CIF_HEIGHT && abs(ImgOut->biHeight)!=QCIF_HEIGHT))
  1289. return (SvErrorBadImageSize);
  1290. /* Reject requests to scale during decompression - renderer's job */
  1291. if (ImgIn && ImgOut &&
  1292. (ImgIn->biWidth != ImgOut->biWidth) ||
  1293. (abs(ImgIn->biHeight) != abs(ImgOut->biHeight)))
  1294. return (SvErrorBadImageSize);
  1295. break;
  1296. #endif /* H261_SUPPORT */
  1297. default:
  1298. break;
  1299. }
  1300. if (ImgOut->biCompression == BI_BITFIELDS &&
  1301. ValidateBI_BITFIELDS(ImgOut) == InvalidBI_BITFIELDS)
  1302. return (SvErrorUnrecognizedFormat);
  1303. }
  1304. if (ImgIn) /* Query input format also */
  1305. {
  1306. if (ImgIn->biWidth <= 0 || ImgIn->biHeight == 0)
  1307. return(SvErrorBadImageSize);
  1308. }
  1309. return(NoErrors);
  1310. }
  1311. /*
  1312. ** Name: SvDecompress
  1313. ** Purpose: Decompress a frame CompData -> YUV or RGB
  1314. **
  1315. ** Args: Svh = handle to software codec's Info structure.
  1316. ** Data = For JPEG points to compressed data (INPUT)
  1317. ** For MPEG & H261, points to MultiBuf
  1318. ** MaxDataSize = Length of Data buffer
  1319. ** Image = buffer for decompressed data (OUTPUT)
  1320. ** MaxImageSize = Size of output image buffer
  1321. **
  1322. */
  1323. SvStatus_t SvDecompress(SvHandle_t Svh, u_char *Data, int MaxDataSize,
  1324. u_char *Image, int MaxImageSize)
  1325. {
  1326. int stat=NoErrors, UsedQ=FALSE, ImageSize;
  1327. SvCodecInfo_t *Info = (SvCodecInfo_t *)Svh;
  1328. u_char *YData=NULL, *CbData=NULL, *CrData=NULL;
  1329. int pixels, lines;
  1330. SvCallbackInfo_t CB;
  1331. u_char *ReturnImage=NULL;
  1332. _SlibDebug(_VERBOSE_, printf("SvDecompress() In\n") );
  1333. if (!Info)
  1334. return(SvErrorCodecHandle);
  1335. if (!Info->started)
  1336. return(SvErrorDcmpNotStarted);
  1337. if (!Data && !Info->BSIn)
  1338. return(SvErrorBadPointer);
  1339. /*
  1340. ** If no image buffer is supplied, see if the Image Queue
  1341. ** has any. If not do a callback to get a buffer.
  1342. */
  1343. if (Image == NULL && Info->ImageQ)
  1344. {
  1345. if (ScBufQueueGetNum(Info->ImageQ))
  1346. {
  1347. ScBufQueueGetHead(Info->ImageQ, &Image, &MaxImageSize);
  1348. ScBufQueueRemove(Info->ImageQ);
  1349. UsedQ = TRUE;
  1350. _SlibDebug(_VERBOSE_, printf("SvDecompress() Got Image %p from Q\n",
  1351. Image) );
  1352. }
  1353. else if (Info->CallbackFunction)
  1354. {
  1355. CB.Message = CB_END_BUFFERS;
  1356. CB.Data = NULL;
  1357. CB.DataSize = 0;
  1358. CB.DataUsed = 0;
  1359. CB.DataType = CB_DATA_IMAGE;
  1360. CB.UserData = Info->BSIn?Info->BSIn->UserData:NULL;
  1361. CB.Action = CB_ACTION_CONTINUE;
  1362. _SlibDebug(_VERBOSE_,
  1363. printf("SvDecompress() Calling callback for Image\n") );
  1364. (*(Info->CallbackFunction))(Svh, &CB, NULL);
  1365. if (CB.Action == CB_ACTION_END)
  1366. {
  1367. _SlibDebug(_DEBUG_,
  1368. printf("SvDecompress() CB.Action = CB_ACTION_END\n") );
  1369. return(SvErrorClientEnd);
  1370. }
  1371. else if (ScBufQueueGetNum(Info->ImageQ))
  1372. {
  1373. ScBufQueueGetHead(Info->ImageQ, &Image, &MaxImageSize);
  1374. ScBufQueueRemove(Info->ImageQ);
  1375. UsedQ = TRUE;
  1376. _SlibDebug(_VERBOSE_,
  1377. printf("SvDecompress() Got Image %p from Q\n", Image) );
  1378. }
  1379. else
  1380. return(SvErrorNoImageBuffer);
  1381. }
  1382. }
  1383. if (!Image)
  1384. return(SvErrorNoImageBuffer);
  1385. ImageSize=MaxImageSize;
  1386. pixels = Info->OutputFormat.biWidth;
  1387. lines = Info->OutputFormat.biHeight;
  1388. if (lines<0) lines=-lines;
  1389. /*
  1390. ** Decompress an image
  1391. */
  1392. switch (Info->mode)
  1393. {
  1394. #ifdef MPEG_SUPPORT
  1395. case SV_MPEG_DECODE:
  1396. case SV_MPEG2_DECODE:
  1397. {
  1398. SvMpegDecompressInfo_t *MDInfo;
  1399. _SlibDebug(_DEBUG_, printf("SvDecompress() SV_MPEG_DECODE\n") );
  1400. if (!(MDInfo = Info->mdcmp))
  1401. return(SvErrorBadPointer);
  1402. if (!MDInfo->DecompressStarted)
  1403. return(SvErrorDcmpNotStarted);
  1404. if (MaxDataSize < MDInfo->finalbufsize)
  1405. return(SvErrorSmallBuffer);
  1406. if (!Data)
  1407. return(SvErrorBadPointer);
  1408. stat = sv_MpegDecompressFrame(Info, Data, &ReturnImage);
  1409. RETURN_ON_ERROR(stat);
  1410. /*
  1411. ** Because the ReturnImage is a pointer into Data
  1412. ** we need to copy it (do a format conversion if necessary).
  1413. */
  1414. switch (Info->OutputFormat.biCompression)
  1415. {
  1416. case BI_YU12SEP:
  1417. /* native format is 4:1:1 planar, just copy */
  1418. ImageSize=(3 * lines * pixels)/2;
  1419. if (ImageSize > MaxImageSize)
  1420. return(SvErrorSmallBuffer);
  1421. memcpy(Image, ReturnImage, ImageSize);
  1422. break;
  1423. case BI_DECYUVDIB:
  1424. case BI_YUY2:
  1425. case BI_S422: /* 4:1:1 planar -> 4:2:2 interleaved */
  1426. ImageSize=(3 * lines * pixels)/2;
  1427. if (ImageSize > MaxImageSize)
  1428. return(SvErrorSmallBuffer);
  1429. ScSepYUVto422i(Image, Image+(lines*pixels),
  1430. Image+(lines*pixels*5)/4,
  1431. ReturnImage, pixels, lines);
  1432. break;
  1433. default: /* 4:1:1 planar -> RGB */
  1434. if (Info->OutputFormat.biCompression==BI_DECXIMAGEDIB)
  1435. ImageSize=lines*pixels *
  1436. (Info->OutputFormat.biBitCount==24 ? 4 : 1);
  1437. else
  1438. ImageSize=lines*pixels * (Info->OutputFormat.biBitCount/8);
  1439. if (ImageSize > MaxImageSize)
  1440. return(SvErrorSmallBuffer);
  1441. YData = ReturnImage;
  1442. CbData = YData + (pixels * lines);
  1443. CrData = CbData + (pixels * lines)/4;
  1444. ScYuv411ToRgb(&Info->OutputFormat, YData, CbData, CrData,
  1445. Image, pixels, lines, pixels);
  1446. break;
  1447. }
  1448. }
  1449. break;
  1450. #endif /* MPEG_SUPPORT */
  1451. #ifdef H261_SUPPORT
  1452. case SV_H261_DECODE:
  1453. {
  1454. SvH261Info_t *H261 = Info->h261;
  1455. _SlibDebug(_DEBUG_, printf("SvDecompress() SV_H261_DECODE\n") );
  1456. if (!H261)
  1457. return(SvErrorBadPointer);
  1458. if (!Data)
  1459. return(SvErrorBadPointer);
  1460. _SlibDebug(_DEBUG_, printf("sv_DecompressH261(Data=%p)\n",Data) );
  1461. stat=svH261Decompress(Info, Data, &ReturnImage);
  1462. if (stat==NoErrors)
  1463. {
  1464. /*
  1465. ** Because the ReturnImage is a pointer into Data
  1466. ** we need to copy it (do a format conversion if necessary).
  1467. */
  1468. switch (Info->OutputFormat.biCompression)
  1469. {
  1470. case BI_YU12SEP:
  1471. /* native format is 4:1:1 planar, just copy */
  1472. ImageSize=(3 * lines * pixels)/2;
  1473. if (ImageSize > MaxImageSize)
  1474. return(SvErrorSmallBuffer);
  1475. memcpy(Image, ReturnImage, ImageSize);
  1476. break;
  1477. case BI_DECYUVDIB:
  1478. case BI_YUY2:
  1479. case BI_S422:
  1480. /* 4:1:1 planar -> 4:2:2 interleaved */
  1481. ImageSize=(3 * lines * pixels)/2;
  1482. if (ImageSize > MaxImageSize)
  1483. return(SvErrorSmallBuffer);
  1484. ScSepYUVto422i(Image, Image+(lines*pixels),
  1485. Image+(lines*pixels*5)/4,
  1486. ReturnImage, pixels, lines);
  1487. break;
  1488. default:
  1489. if (Info->OutputFormat.biCompression==BI_DECXIMAGEDIB)
  1490. ImageSize=lines*pixels *
  1491. (Info->OutputFormat.biBitCount==24 ? 4 : 1);
  1492. else
  1493. ImageSize=lines*pixels * (Info->OutputFormat.biBitCount/8);
  1494. if (ImageSize > MaxImageSize)
  1495. return(SvErrorSmallBuffer);
  1496. YData = ReturnImage;
  1497. CbData = YData + (pixels * lines * sizeof(u_char));
  1498. CrData = CbData + ((pixels * lines * sizeof(u_char))/4);
  1499. ScYuv411ToRgb(&Info->OutputFormat, YData, CbData, CrData,
  1500. Image, pixels, lines, pixels);
  1501. break;
  1502. }
  1503. }
  1504. else
  1505. {
  1506. ImageSize=0;
  1507. if (Info->CallbackFunction)
  1508. {
  1509. CB.Message = CB_SEQ_END;
  1510. CB.Data = NULL;
  1511. CB.DataSize = 0;
  1512. CB.DataType = CB_DATA_NONE;
  1513. CB.UserData = Info->BSIn?Info->BSIn->UserData:NULL;
  1514. CB.Action = CB_ACTION_CONTINUE;
  1515. (*Info->CallbackFunction)(Svh, &CB, NULL);
  1516. _SlibDebug(_DEBUG_,
  1517. printf("H261 Callback: CB_SEQ_END Data = 0x%x Action = %d\n",
  1518. CB.Data, CB.Action) );
  1519. if (CB.Action == CB_ACTION_END)
  1520. return (ScErrorClientEnd);
  1521. }
  1522. }
  1523. }
  1524. break;
  1525. #endif /* H261_SUPPORT */
  1526. #ifdef H263_SUPPORT
  1527. case SV_H263_DECODE:
  1528. {
  1529. SvH263DecompressInfo_t *H263Info = Info->h263dcmp;
  1530. _SlibDebug(_DEBUG_, printf("SvDecompress() SV_H261_DECODE\n") );
  1531. if (!H263Info)
  1532. return(SvErrorBadPointer);
  1533. _SlibDebug(_DEBUG_, printf("svH263Decompress(Data=%p)\n",Data) );
  1534. stat=svH263Decompress(Info, &ReturnImage);
  1535. if (stat==NoErrors)
  1536. {
  1537. /*
  1538. ** Because the ReturnImage is a pointer into Data
  1539. ** we need to copy it (do a format conversion if necessary).
  1540. */
  1541. switch (Info->OutputFormat.biCompression)
  1542. {
  1543. case BI_YU12SEP:
  1544. /* native format is 4:1:1 planar, just copy */
  1545. ImageSize=(3 * lines * pixels)/2;
  1546. if (ImageSize > MaxImageSize)
  1547. return(SvErrorSmallBuffer);
  1548. memcpy(Image, ReturnImage, ImageSize);
  1549. break;
  1550. case BI_DECYUVDIB:
  1551. case BI_YUY2:
  1552. case BI_S422:
  1553. /* 4:1:1 planar -> 4:2:2 interleaved */
  1554. ImageSize=(3 * lines * pixels)/2;
  1555. if (ImageSize > MaxImageSize)
  1556. return(SvErrorSmallBuffer);
  1557. ScSepYUVto422i(Image, Image+(lines*pixels),
  1558. Image+(lines*pixels*5)/4,
  1559. ReturnImage, pixels, lines);
  1560. break;
  1561. default:
  1562. if (Info->OutputFormat.biCompression==BI_DECXIMAGEDIB)
  1563. ImageSize=lines*pixels *
  1564. (Info->OutputFormat.biBitCount==24 ? 4 : 1);
  1565. else
  1566. ImageSize=lines*pixels * (Info->OutputFormat.biBitCount/8);
  1567. if (ImageSize > MaxImageSize)
  1568. return(SvErrorSmallBuffer);
  1569. YData = ReturnImage;
  1570. CbData = YData + (pixels * lines * sizeof(u_char));
  1571. CrData = CbData + ((pixels * lines * sizeof(u_char))/4);
  1572. ScYuv411ToRgb(&Info->OutputFormat, YData, CbData, CrData,
  1573. Image, pixels, lines, pixels);
  1574. break;
  1575. }
  1576. }
  1577. else
  1578. {
  1579. ImageSize=0;
  1580. if (Info->CallbackFunction)
  1581. {
  1582. CB.Message = CB_SEQ_END;
  1583. CB.Data = NULL;
  1584. CB.DataSize = 0;
  1585. CB.DataType = CB_DATA_NONE;
  1586. CB.UserData = Info->BSIn?Info->BSIn->UserData:NULL;
  1587. CB.Action = CB_ACTION_CONTINUE;
  1588. (*Info->CallbackFunction)(Svh, &CB, NULL);
  1589. _SlibDebug(_DEBUG_,
  1590. printf("H263 Callback: CB_SEQ_END Data = 0x%x Action = %d\n",
  1591. CB.Data, CB.Action) );
  1592. if (CB.Action == CB_ACTION_END)
  1593. return (ScErrorClientEnd);
  1594. }
  1595. }
  1596. }
  1597. break;
  1598. #endif /* H263_SUPPORT */
  1599. #ifdef JPEG_SUPPORT
  1600. case SV_JPEG_DECODE:
  1601. {
  1602. SvJpegDecompressInfo_t *DInfo;
  1603. register int i;
  1604. JPEGINFOHEADER *jpegbm;
  1605. int maxMcu;
  1606. EXBMINFOHEADER * exbi;
  1607. _SlibDebug(_DEBUG_, printf("SvDecompress() SV_JPEG_DECODE\n") );
  1608. if (!(DInfo = Info->jdcmp))
  1609. return(SvErrorBadPointer);
  1610. exbi = (EXBMINFOHEADER *)&Info->InputFormat;
  1611. jpegbm = (JPEGINFOHEADER *)(
  1612. (unsigned long)exbi + exbi->biExtDataOffset);
  1613. /*
  1614. ** In case the application forgot to call SvDecompressBegin().
  1615. */
  1616. if (!DInfo->DecompressStarted)
  1617. return(SvErrorDcmpNotStarted);
  1618. /*
  1619. ** If desired output is not separate YUV components, we have to
  1620. ** convert from Sep. YUV to desired format. Create intermediate image.
  1621. */
  1622. _SlibDebug(_DEBUG_, printf ("JPEGBitsPerSample %d \n",
  1623. jpegbm->JPEGBitsPerSample) );
  1624. if (lines < 0) lines = -lines;
  1625. _SlibDebug(_DEBUG_,
  1626. printf ("JPEG_RGB : %d - ", JPEG_RGB);
  1627. if (jpegbm->JPEGColorSpaceID == JPEG_RGB)
  1628. printf ("Color Space is RGB \n");
  1629. else
  1630. printf ("Color Space is %d \n", jpegbm->JPEGColorSpaceID) );
  1631. if (!IsYUV422Sep(Info->OutputFormat.biCompression) &&
  1632. !IsYUV411Sep(Info->OutputFormat.biCompression) &&
  1633. Info->OutputFormat.biCompression != BI_DECGRAYDIB)
  1634. {
  1635. /*
  1636. ** should be done only once for each instance of the CODEC.
  1637. ** - Note: this forces us to check the size parameters (pixels &
  1638. ** lines) for each image to be decompressed. Should we
  1639. ** support sequences that do not have constant frame sizes?
  1640. */
  1641. if (!DInfo->TempImage) {
  1642. DInfo->TempImage = (u_char *)ScPaMalloc (3 * pixels * lines);
  1643. if (DInfo->TempImage == NULL)
  1644. return(SvErrorMemory);
  1645. }
  1646. YData = DInfo->TempImage;
  1647. CbData = YData + (pixels * lines * sizeof(u_char));
  1648. CrData = CbData + (pixels * lines * sizeof(u_char));
  1649. }
  1650. else {
  1651. /*
  1652. ** For YUV Planar formats, no need to translate.
  1653. ** Get pointers to individual components.
  1654. */
  1655. _SlibDebug(_DEBUG_, printf ("sv_GetYUVComponentPointers\n") );
  1656. stat = sv_GetYUVComponentPointers(Info->OutputFormat.biCompression,
  1657. pixels, lines, Image, MaxImageSize,
  1658. &YData, &CbData, &CrData);
  1659. RETURN_ON_ERROR (stat);
  1660. }
  1661. _SlibDebug(_DEBUG_, printf ("sv_ParseFrame\n") );
  1662. stat = sv_ParseFrame (Data, MaxDataSize, Info);
  1663. RETURN_ON_ERROR (stat);
  1664. /*
  1665. ** Fill Info structure with video-specific data on first frame
  1666. */
  1667. if (!DInfo->InfoFilled) {
  1668. _SlibDebug(_DEBUG_, printf ("sv_InitJpegDecoder\n") );
  1669. stat = sv_InitJpegDecoder (Info);
  1670. RETURN_ON_ERROR (stat);
  1671. DInfo->InfoFilled = 1;
  1672. /*
  1673. ** Error checking:
  1674. ** make the assumption that for MJPEG we need to check for
  1675. ** valid subsampling only once at the start of the seqence
  1676. */
  1677. _SlibDebug(_DEBUG_, printf ("sv_CheckChroma\n") );
  1678. stat = sv_CheckChroma(Info);
  1679. RETURN_ON_ERROR (stat);
  1680. }
  1681. /*
  1682. ** Decompress everything into MCU's
  1683. */
  1684. if (!DInfo->ReInit) /* Reset the JPEG compressor */
  1685. sv_ReInitJpegDecoder (Info);
  1686. maxMcu = DInfo->MCUsPerRow * DInfo->MCUrowsInScan;
  1687. if (DInfo->ReInit)
  1688. DInfo->ReInit = 0;
  1689. DInfo->CurrBlockNumber = 0;
  1690. /*
  1691. ** Create the BlockPtr array for the output buffers
  1692. */
  1693. if ((YData != DInfo->Old_YData) ||
  1694. (CbData != DInfo->Old_CbData) ||
  1695. (CrData != DInfo->Old_CrData))
  1696. {
  1697. DInfo->Old_YData =YData;
  1698. DInfo->Old_CbData=CbData;
  1699. DInfo->Old_CrData=CrData;
  1700. stat = sv_MakeDecoderBlkTable (Info);
  1701. RETURN_ON_ERROR (stat);
  1702. }
  1703. CB.Message = CB_PROCESSING;
  1704. for (i = 0; i < maxMcu; i++) {
  1705. #if 0
  1706. if ((Info->CallbackFunction) && ((i % MCU_CALLBACK_COUNT) == 0)) {
  1707. (*Info->CallbackFunction)(Svh, &CB, &PictureInfo);
  1708. if (CB.Action == CB_ACTION_END)
  1709. return(SvErrorClientEnd);
  1710. }
  1711. #endif
  1712. _SlibDebug(_DEBUG_, printf ("sv_DecodeJpeg\n") );
  1713. stat = sv_DecodeJpeg (Info);
  1714. RETURN_ON_ERROR (stat);
  1715. }
  1716. #if 0
  1717. /*
  1718. ** Check for multiple scans in the JPEG file
  1719. ** - we do not support multiple scans
  1720. */
  1721. if (sv_ParseScanHeader (Info) != SvErrorEOI)
  1722. _SlibDebug(_DEBUG_ || _WARN_ || _VERBOSE_,
  1723. printf(" *** Warning ***, Multiple Scans detected, unsupported\n") );
  1724. #endif
  1725. if (DInfo->compinfo[0].Vsf==2) /* 4:1:1->4:2:2 */
  1726. {
  1727. if (IsYUV422Packed(Info->OutputFormat.biCompression))
  1728. ScConvert411sTo422i_C(YData, CbData, CrData, Image,
  1729. pixels, lines);
  1730. else if IsYUV422Sep(Info->OutputFormat.biCompression)
  1731. ScConvert411sTo422s_C(YData, CbData, CrData, Image,
  1732. pixels, lines);
  1733. else if IsYUV411Sep(Info->OutputFormat.biCompression)
  1734. {
  1735. if (YData!=Image)
  1736. memcpy(Image, YData, pixels*lines);
  1737. memcpy(Image+pixels*lines, CbData, (pixels*lines)/4);
  1738. memcpy(Image+(pixels*lines*5)/4, CrData, (pixels*lines)/4);
  1739. }
  1740. else
  1741. {
  1742. ScConvert411sTo422s_C(YData, CbData, CrData, YData,
  1743. pixels, lines);
  1744. ScConvertSepYUVToOther(&Info->InputFormat, &Info->OutputFormat,
  1745. Image, YData, CbData, CrData);
  1746. }
  1747. }
  1748. else if (!IsYUV422Sep(Info->OutputFormat.biCompression) &&
  1749. !IsYUV411Sep(Info->OutputFormat.biCompression) &&
  1750. Info->OutputFormat.biCompression != BI_DECGRAYDIB)
  1751. ScConvertSepYUVToOther(&Info->InputFormat, &Info->OutputFormat,
  1752. Image, YData, CbData, CrData);
  1753. }
  1754. break; /* SV_JPEG_DECODE */
  1755. #endif /* JPEG_SUPPORT */
  1756. #ifdef HUFF_SUPPORT
  1757. case SV_HUFF_DECODE:
  1758. {
  1759. SvHuffInfo_t *HInfo;
  1760. _SlibDebug(_DEBUG_, printf("SvDecompress() SV_HUFF_DECODE\n") );
  1761. if (!(HInfo = Info->huff))
  1762. return(SvErrorBadPointer);
  1763. if (!HInfo->DecompressStarted)
  1764. return(SvErrorDcmpNotStarted);
  1765. stat = sv_HuffDecodeFrame(Info, Image);
  1766. RETURN_ON_ERROR(stat);
  1767. }
  1768. break;
  1769. #endif /* HUFF_SUPPORT */
  1770. default:
  1771. return(SvErrorCodecType);
  1772. }
  1773. Info->NumOperations++;
  1774. if (Info->CallbackFunction)
  1775. {
  1776. if (ImageSize>0)
  1777. {
  1778. CB.Message = CB_FRAME_READY;
  1779. CB.Data = Image;
  1780. CB.DataSize = MaxImageSize;
  1781. CB.DataUsed = ImageSize;
  1782. CB.DataType = CB_DATA_IMAGE;
  1783. CB.UserData = Info->BSIn?Info->BSIn->UserData:NULL;
  1784. CB.TimeStamp = 0;
  1785. CB.Flags = 0;
  1786. CB.Value = 0;
  1787. CB.Format = (void *)&Info->OutputFormat;
  1788. CB.Action = CB_ACTION_CONTINUE;
  1789. (*(Info->CallbackFunction))(Svh, &CB, NULL);
  1790. _SlibDebug(_DEBUG_,
  1791. printf("Decompress Callback: CB_FRAME_READY Addr=0x%x, Action=%d\n",
  1792. CB.Data, CB.Action) );
  1793. if (CB.Action == CB_ACTION_END)
  1794. return(SvErrorClientEnd);
  1795. }
  1796. /*
  1797. ** If an Image buffer was taken from the queue, do a callback
  1798. ** to let the client free or re-use the buffer.
  1799. */
  1800. if (UsedQ)
  1801. {
  1802. CB.Message = CB_RELEASE_BUFFER;
  1803. CB.Data = Image;
  1804. CB.DataSize = MaxImageSize;
  1805. CB.DataUsed = ImageSize;
  1806. CB.DataType = CB_DATA_IMAGE;
  1807. CB.UserData = Info->BSIn?Info->BSIn->UserData:NULL;
  1808. CB.Action = CB_ACTION_CONTINUE;
  1809. (*(Info->CallbackFunction))(Svh, &CB, NULL);
  1810. _SlibDebug(_DEBUG_,
  1811. printf("Decompress Callback: RELEASE_BUFFER Addr=0x%x, Action=%d\n",
  1812. CB.Data, CB.Action) );
  1813. if (CB.Action == CB_ACTION_END)
  1814. return(SvErrorClientEnd);
  1815. }
  1816. }
  1817. _SlibDebug(_DEBUG_, printf("SvDecompress() Out\n") );
  1818. return(stat);
  1819. }
  1820. /*
  1821. ** Name: SvDecompressEnd
  1822. ** Purpose: Terminate the Decompression Codec. Call after all calls to
  1823. ** SvDecompress are done.
  1824. **
  1825. ** Args: Svh = handle to software codec's Info structure.
  1826. */
  1827. SvStatus_t SvDecompressEnd (SvHandle_t Svh)
  1828. {
  1829. SvCodecInfo_t *Info = (SvCodecInfo_t *)Svh;
  1830. SvCallbackInfo_t CB;
  1831. _SlibDebug(_DEBUG_, printf("SvDecompressEnd()\n") );
  1832. if (!Info)
  1833. return(SvErrorCodecHandle);
  1834. if (!Info->started)
  1835. return(SvErrorDcmpNotStarted);
  1836. switch (Info->mode)
  1837. {
  1838. #ifdef JPEG_SUPPORT
  1839. case SV_JPEG_DECODE:
  1840. Info->jdcmp->DecompressStarted = FALSE;
  1841. break;
  1842. #endif /* JPEG_SUPPORT */
  1843. #ifdef MPEG_SUPPORT
  1844. case SV_MPEG_DECODE:
  1845. case SV_MPEG2_DECODE:
  1846. Info->mdcmp->DecompressStarted = FALSE;
  1847. Info->mdcmp->PicturePositioned = FALSE;
  1848. Info->mdcmp->lastI = -1;
  1849. Info->mdcmp->lastP = -1;
  1850. Info->mdcmp->N = 12;
  1851. Info->mdcmp->M = 3;
  1852. Info->mdcmp->framenum = 0;
  1853. break;
  1854. #endif /* MPEG_SUPPORT */
  1855. #ifdef H261_SUPPORT
  1856. case SV_H261_DECODE:
  1857. {
  1858. int status=svH261DecompressFree(Svh);
  1859. RETURN_ON_ERROR(status);
  1860. }
  1861. break;
  1862. #endif /* H261_SUPPORT */
  1863. #ifdef H263_SUPPORT
  1864. case SV_H263_DECODE:
  1865. {
  1866. int status=svH263FreeDecompressor(Info);
  1867. RETURN_ON_ERROR(status);
  1868. }
  1869. #endif /* H263_SUPPORT */
  1870. #ifdef HUFF_SUPPORT
  1871. case SV_HUFF_DECODE:
  1872. /*
  1873. {
  1874. int status=sv_HuffDecompressEnd(Svh);
  1875. RETURN_ON_ERROR(status);
  1876. }
  1877. */
  1878. break;
  1879. #endif /* HUFF_SUPPORT */
  1880. }
  1881. /* Release any Image Buffers in the queue */
  1882. if (Info->ImageQ)
  1883. {
  1884. int datasize;
  1885. _SlibDebug(_VERBOSE_, printf("Info->ImageQ exists\n") );
  1886. while (ScBufQueueGetNum(Info->ImageQ))
  1887. {
  1888. _SlibDebug(_VERBOSE_, printf("Removing from ImageQ\n") );
  1889. ScBufQueueGetHead(Info->ImageQ, &CB.Data, &datasize);
  1890. ScBufQueueRemove(Info->ImageQ);
  1891. if (Info->CallbackFunction && CB.Data)
  1892. {
  1893. CB.Message = CB_RELEASE_BUFFER;
  1894. CB.DataSize = datasize;
  1895. CB.DataUsed = 0;
  1896. CB.DataType = CB_DATA_IMAGE;
  1897. CB.UserData = Info->BSIn?Info->BSIn->UserData:NULL;
  1898. CB.Action = CB_ACTION_CONTINUE;
  1899. (*(Info->CallbackFunction))(Svh, &CB, NULL);
  1900. _SlibDebug(_DEBUG_,
  1901. printf("SvDecompressEnd: RELEASE_BUFFER. Data = 0x%x, Action = %d\n",
  1902. CB.Data, CB.Action) );
  1903. }
  1904. }
  1905. }
  1906. if (Info->BSIn)
  1907. ScBSFlush(Info->BSIn); /* flush out any remaining compressed buffers */
  1908. if (Info->CallbackFunction)
  1909. {
  1910. CB.Message = CB_CODEC_DONE;
  1911. CB.Data = NULL;
  1912. CB.DataSize = 0;
  1913. CB.DataUsed = 0;
  1914. CB.DataType = CB_DATA_NONE;
  1915. CB.UserData = Info->BSIn?Info->BSIn->UserData:NULL;
  1916. CB.TimeStamp = 0;
  1917. CB.Flags = 0;
  1918. CB.Value = 0;
  1919. CB.Format = NULL;
  1920. CB.Action = CB_ACTION_CONTINUE;
  1921. (*Info->CallbackFunction)(Svh, &CB, NULL);
  1922. _SlibDebug(_DEBUG_,
  1923. printf("SvDecompressEnd Callback: CB_CODEC_DONE Action = %d\n",
  1924. CB.Action) );
  1925. if (CB.Action == CB_ACTION_END)
  1926. return (ScErrorClientEnd);
  1927. }
  1928. Info->started=FALSE;
  1929. return(NoErrors);
  1930. }
  1931. /*
  1932. ** Name: SvSetDataSource
  1933. ** Purpose: Set the data source used by the MPEG or H261 bitstream parsing code
  1934. ** to either the Buffer Queue or File input. The default is
  1935. ** to use the Buffer Queue where data buffers are added by calling
  1936. ** SvAddBuffer. When using file IO, the data is read from a file
  1937. ** descriptor into a buffer supplied by the user.
  1938. **
  1939. ** Args: Svh = handle to software codec's Info structure.
  1940. ** Source = SV_USE_BUFFER_QUEUE or SV_USE_FILE
  1941. ** Fd = File descriptor to use if Source = SV_USE_FILE
  1942. ** Buf = Pointer to buffer to use if Source = SV_USE_FILE
  1943. ** BufSize= Size of buffer when Source = SV_USE_FILE
  1944. */
  1945. SvStatus_t SvSetDataSource (SvHandle_t Svh, int Source, int Fd,
  1946. void *Buffer_UserData, int BufSize)
  1947. {
  1948. SvCodecInfo_t *Info = (SvCodecInfo_t *)Svh;
  1949. int stat=NoErrors;
  1950. if (!Info)
  1951. return(SvErrorCodecHandle);
  1952. if (Info->mode!=SV_MPEG_DECODE && Info->mode!=SV_MPEG2_DECODE
  1953. && Info->mode!=SV_H261_DECODE && Info->mode!=SV_H263_DECODE
  1954. && Info->mode!=SV_HUFF_DECODE)
  1955. return(SvErrorCodecType);
  1956. if (Info->BSIn)
  1957. {
  1958. ScBSDestroy(Info->BSIn);
  1959. Info->BSIn=NULL;
  1960. }
  1961. switch (Source)
  1962. {
  1963. case SV_USE_BUFFER:
  1964. _SlibDebug(_DEBUG_, printf("SvSetDataSource(SV_USE_BUFFER)\n") );
  1965. stat=ScBSCreateFromBuffer(&Info->BSIn, Buffer_UserData, BufSize);
  1966. break;
  1967. case SV_USE_BUFFER_QUEUE:
  1968. _SlibDebug(_DEBUG_, printf("SvSetDataSource(SV_USE_BUFFER_QUEUE)\n") );
  1969. stat=ScBSCreateFromBufferQueue(&Info->BSIn, Svh,
  1970. CB_DATA_COMPRESSED,
  1971. Info->BufQ,
  1972. (int (*)(ScHandle_t, ScCallbackInfo_t *, void *))Info->CallbackFunction,
  1973. (void *)Buffer_UserData);
  1974. break;
  1975. case SV_USE_FILE:
  1976. _SlibDebug(_DEBUG_, printf("SvSetDataSource(SV_USE_FILE)\n") );
  1977. stat=ScBSCreateFromFile(&Info->BSIn, Fd, Buffer_UserData, BufSize);
  1978. break;
  1979. default:
  1980. stat=SvErrorBadArgument;
  1981. }
  1982. return(stat);
  1983. }
  1984. /*
  1985. ** Name: SvSetDataDestination
  1986. ** Purpose: Set the data destination used by the MPEG or H261 bitstream
  1987. ** writing code
  1988. ** to either the Buffer Queue or File input. The default is
  1989. ** to use the Buffer Queue where data buffers are added by calling
  1990. ** SvAddBuffer. When using file IO, the data is read from a file
  1991. ** descriptor into a buffer supplied by the user.
  1992. **
  1993. ** Args: Svh = handle to software codec's Info structure.
  1994. ** Source = SV_USE_BUFFER_QUEUE or SV_USE_FILE
  1995. ** Fd = File descriptor to use if Source = SV_USE_FILE
  1996. ** Buf = Pointer to buffer to use if Source = SV_USE_FILE
  1997. ** BufSize= Size of buffer when Source = SV_USE_FILE
  1998. */
  1999. SvStatus_t SvSetDataDestination(SvHandle_t Svh, int Dest, int Fd,
  2000. void *Buffer_UserData, int BufSize)
  2001. {
  2002. SvCodecInfo_t *Info = (SvCodecInfo_t *)Svh;
  2003. int stat=NoErrors;
  2004. if (!Info)
  2005. return(SvErrorCodecHandle);
  2006. if (Info->mode != SV_H261_ENCODE && Info->mode != SV_H263_ENCODE &&
  2007. Info->mode != SV_MPEG_ENCODE &&
  2008. Info->mode != SV_MPEG2_ENCODE && Info->mode != SV_HUFF_ENCODE)
  2009. return(SvErrorCodecType);
  2010. if (Info->BSOut)
  2011. {
  2012. ScBSDestroy(Info->BSOut);
  2013. Info->BSOut=NULL;
  2014. }
  2015. switch (Dest)
  2016. {
  2017. case SV_USE_BUFFER:
  2018. _SlibDebug(_DEBUG_, printf("SvSetDataDestination(SV_USE_BUFFER)\n") );
  2019. stat=ScBSCreateFromBuffer(&Info->BSOut, Buffer_UserData, BufSize);
  2020. break;
  2021. case SV_USE_BUFFER_QUEUE:
  2022. _SlibDebug(_DEBUG_,
  2023. printf("SvSetDataDestination(SV_USE_BUFFER_QUEUE)\n") );
  2024. stat=ScBSCreateFromBufferQueue(&Info->BSOut, Svh,
  2025. CB_DATA_COMPRESSED, Info->BufQ,
  2026. (int (*)(ScHandle_t, ScCallbackInfo_t *, void *))Info->CallbackFunction,
  2027. (void *)Buffer_UserData);
  2028. break;
  2029. case SV_USE_FILE:
  2030. _SlibDebug(_DEBUG_, printf("SvSetDataDestination(SV_USE_FILE)\n") );
  2031. stat=ScBSCreateFromFile(&Info->BSOut, Fd, Buffer_UserData, BufSize);
  2032. break;
  2033. default:
  2034. stat=SvErrorBadArgument;
  2035. }
  2036. return(stat);
  2037. }
  2038. /*
  2039. ** Name: SvGetDataSource
  2040. ** Purpose: Returns the current input bitstream being used by
  2041. ** the Codec.
  2042. ** Return: NULL if there no associated bitstream
  2043. ** (currently H.261 and MPEG use a bitstream)
  2044. */
  2045. ScBitstream_t *SvGetDataSource (SvHandle_t Svh)
  2046. {
  2047. SvCodecInfo_t *Info = (SvCodecInfo_t *)Svh;
  2048. if (!Info)
  2049. return(NULL);
  2050. return(Info->BSIn);
  2051. }
  2052. /*
  2053. ** Name: SvGetDataDestination
  2054. ** Purpose: Returns the current input bitstream being used by
  2055. ** the Codec.
  2056. ** Return: NULL if there no associated bitstream
  2057. ** (currently H.261 and MPEG use a bitstream)
  2058. */
  2059. ScBitstream_t *SvGetDataDestination(SvHandle_t Svh)
  2060. {
  2061. SvCodecInfo_t *Info = (SvCodecInfo_t *)Svh;
  2062. if (!Info)
  2063. return(NULL);
  2064. return(Info->BSOut);
  2065. }
  2066. /*
  2067. ** Name: SvGetInputBitstream
  2068. ** Purpose: Returns the current input bitstream being used by
  2069. ** the Codec.
  2070. ** Return: NULL if there no associated bitstream
  2071. ** (currently H.261 and MPEG use a bitstream)
  2072. */
  2073. ScBitstream_t *SvGetInputBitstream (SvHandle_t Svh)
  2074. {
  2075. return(SvGetDataSource(Svh));
  2076. }
  2077. /*
  2078. ** Name: SvFlush
  2079. ** Purpose: Flushes out current compressed buffers.
  2080. ** Return: status
  2081. */
  2082. SvStatus_t SvFlush(SvHandle_t Svh)
  2083. {
  2084. SvCodecInfo_t *Info = (SvCodecInfo_t *)Svh;
  2085. if (!Info)
  2086. return(SvErrorCodecHandle);
  2087. if (Info->BSIn)
  2088. ScBSFlush(Info->BSIn); /* flush out any remaining input compressed buffers */
  2089. if (Info->BSOut)
  2090. ScBSFlush(Info->BSOut); /* flush out any remaining output compressed buffers */
  2091. return(SvErrorNone);
  2092. }
  2093. /*
  2094. ** Name: SvRegisterCallback
  2095. ** Purpose: Specify the user-function that will be called during processing
  2096. ** to determine if the codec should abort the frame.
  2097. ** Args: Svh = handle to software codec's Info structure.
  2098. ** Callback = callback function to register
  2099. **
  2100. */
  2101. SvStatus_t SvRegisterCallback (SvHandle_t Svh,
  2102. int (*Callback)(SvHandle_t, SvCallbackInfo_t *, SvPictureInfo_t *),
  2103. void *UserData)
  2104. {
  2105. SvStatus_t stat=NoErrors;
  2106. SvCodecInfo_t *Info = (SvCodecInfo_t *)Svh;
  2107. _SlibDebug(_DEBUG_, printf("SvRegisterCallback()\n") );
  2108. if (!Info)
  2109. return(SvErrorCodecHandle);
  2110. if (!Callback)
  2111. return(SvErrorBadPointer);
  2112. switch (Info->mode)
  2113. {
  2114. #ifdef MPEG_SUPPORT
  2115. case SV_MPEG_DECODE:
  2116. case SV_MPEG2_DECODE:
  2117. Info->CallbackFunction = Callback;
  2118. if (Info->BSIn==NULL)
  2119. stat=SvSetDataSource(Svh, SV_USE_BUFFER_QUEUE, 0, UserData, 0);
  2120. break;
  2121. case SV_MPEG_ENCODE:
  2122. case SV_MPEG2_ENCODE:
  2123. Info->CallbackFunction = Callback;
  2124. if (Info->BSOut==NULL)
  2125. stat=SvSetDataDestination(Svh, SV_USE_BUFFER_QUEUE, 0, UserData, 0);
  2126. break;
  2127. #endif /* MPEG_SUPPORT */
  2128. #ifdef H261_SUPPORT
  2129. case SV_H261_DECODE:
  2130. Info->CallbackFunction = Callback;
  2131. if (Info->h261) /* copy callback to H261 structure */
  2132. Info->h261->CallbackFunction=Callback;
  2133. if (Info->BSIn==NULL)
  2134. stat=SvSetDataSource(Svh, SV_USE_BUFFER_QUEUE, 0, UserData, 0);
  2135. break;
  2136. case SV_H261_ENCODE:
  2137. Info->CallbackFunction = Callback;
  2138. if (Info->h261) /* copy callback to H261 structure */
  2139. Info->h261->CallbackFunction=Callback;
  2140. if (Info->BSOut==NULL)
  2141. stat=SvSetDataDestination(Svh, SV_USE_BUFFER_QUEUE, 0, UserData, 0);
  2142. break;
  2143. #endif /* H261_SUPPORT */
  2144. #ifdef H263_SUPPORT
  2145. case SV_H263_DECODE:
  2146. Info->CallbackFunction = Callback;
  2147. if (Info->BSIn==NULL)
  2148. stat=SvSetDataSource(Svh, SV_USE_BUFFER_QUEUE, 0, UserData, 0);
  2149. break;
  2150. case SV_H263_ENCODE:
  2151. Info->CallbackFunction = Callback;
  2152. if (Info->BSOut==NULL)
  2153. stat=SvSetDataDestination(Svh, SV_USE_BUFFER_QUEUE, 0, UserData, 0);
  2154. break;
  2155. #endif /* H263_SUPPORT */
  2156. #ifdef HUFF_SUPPORT
  2157. case SV_HUFF_DECODE:
  2158. Info->CallbackFunction = Callback;
  2159. if (Info->BSIn==NULL)
  2160. stat=SvSetDataSource(Svh, SV_USE_BUFFER_QUEUE, 0, UserData, 0);
  2161. break;
  2162. case SV_HUFF_ENCODE:
  2163. Info->CallbackFunction = Callback;
  2164. if (Info->BSOut==NULL)
  2165. stat=SvSetDataDestination(Svh, SV_USE_BUFFER_QUEUE, 0, UserData, 0);
  2166. break;
  2167. #endif /* HUFF_SUPPORT */
  2168. default:
  2169. return(SvErrorCodecType);
  2170. }
  2171. return(stat);
  2172. }
  2173. /*
  2174. ** Name: SvAddBuffer
  2175. ** Purpose: Add a buffer of MPEG bitstream data to the CODEC or add an image
  2176. ** buffer to be filled by the CODEC (in streaming mode)
  2177. **
  2178. ** Args: Svh = handle to software codec's Info structure.
  2179. ** BufferInfo = structure describing buffer's address, type & size
  2180. */
  2181. SvStatus_t SvAddBuffer (SvHandle_t Svh, SvCallbackInfo_t *BufferInfo)
  2182. {
  2183. SvCodecInfo_t *Info = (SvCodecInfo_t *)Svh;
  2184. ScQueue_t *Q=NULL;
  2185. _SlibDebug(_DEBUG_, printf("SvAddBuffer() length=%d\n",BufferInfo->DataSize));
  2186. if (!Info)
  2187. return(SvErrorCodecHandle);
  2188. if (BufferInfo->DataType != CB_DATA_COMPRESSED &&
  2189. BufferInfo->DataType != CB_DATA_IMAGE)
  2190. return(SvErrorBadArgument);
  2191. /*
  2192. ** Compressed data can only be added for MPEG and H261
  2193. */
  2194. if (BufferInfo->DataType == CB_DATA_COMPRESSED
  2195. #ifdef MPEG_SUPPORT
  2196. && Info->mode != SV_MPEG_DECODE
  2197. && Info->mode != SV_MPEG2_DECODE
  2198. && Info->mode != SV_MPEG_ENCODE
  2199. && Info->mode != SV_MPEG2_ENCODE
  2200. #endif /* MPEG_SUPPORT */
  2201. #ifdef H261_SUPPORT
  2202. && Info->mode != SV_H261_DECODE
  2203. && Info->mode != SV_H261_ENCODE
  2204. #endif /* H261_SUPPORT */
  2205. #ifdef H263_SUPPORT
  2206. && Info->mode != SV_H263_DECODE
  2207. && Info->mode != SV_H263_ENCODE
  2208. #endif /* H263_SUPPORT */
  2209. #ifdef HUFF_SUPPORT
  2210. && Info->mode != SV_HUFF_DECODE
  2211. && Info->mode != SV_HUFF_ENCODE
  2212. #endif /* HUFF_SUPPORT */
  2213. )
  2214. return(SvErrorCodecType);
  2215. if (!BufferInfo->Data || (BufferInfo->DataSize <= 0))
  2216. return(SvErrorBadArgument);
  2217. switch (BufferInfo->DataType)
  2218. {
  2219. case CB_DATA_COMPRESSED:
  2220. _SlibDebug(_DEBUG_, printf("SvAddBuffer() COMPRESSED\n") );
  2221. if (Info->BSOut && Info->BSOut->EOI)
  2222. ScBSReset(Info->BSOut);
  2223. if (Info->BSIn && Info->BSIn->EOI)
  2224. ScBSReset(Info->BSIn);
  2225. Q = Info->BufQ;
  2226. break;
  2227. case CB_DATA_IMAGE:
  2228. _SlibDebug(_DEBUG_, printf("SvAddBuffer() IMAGE\n") );
  2229. Q = Info->ImageQ;
  2230. break;
  2231. default:
  2232. return(SvErrorBadArgument);
  2233. }
  2234. if (Q)
  2235. ScBufQueueAdd(Q, BufferInfo->Data, BufferInfo->DataSize);
  2236. else
  2237. _SlibDebug(_DEBUG_, printf("ScBufQueueAdd() no Queue\n") );
  2238. return(NoErrors);
  2239. }
  2240. /*
  2241. ** Name: SvFindNextPicture
  2242. ** Purpose: Find the start of the next picture in a bitstream.
  2243. ** Return the picture type to the caller.
  2244. **
  2245. ** Args: Svh = handle to software codec's Info structure.
  2246. ** PictureInfo = Structure used to select what type of pictures to
  2247. ** search for and to return information about the
  2248. ** picture that is found
  2249. */
  2250. SvStatus_t SvFindNextPicture (SvHandle_t Svh, SvPictureInfo_t *PictureInfo)
  2251. {
  2252. SvCodecInfo_t *Info = (SvCodecInfo_t *)Svh;
  2253. _SlibDebug(_DEBUG_, printf("SvFindNextPicture()\n") );
  2254. if (!Info)
  2255. return(SvErrorCodecHandle);
  2256. switch (Info->mode)
  2257. {
  2258. #ifdef MPEG_SUPPORT
  2259. case SV_MPEG_DECODE:
  2260. case SV_MPEG2_DECODE:
  2261. if (!Info->mdcmp)
  2262. return(SvErrorBadPointer);
  2263. if (!Info->mdcmp->DecompressStarted)
  2264. return(SvErrorDcmpNotStarted);
  2265. {
  2266. SvStatus_t stat = sv_MpegFindNextPicture(Info, PictureInfo);
  2267. return(stat);
  2268. }
  2269. #endif /* MPEG_SUPPORT */
  2270. default:
  2271. return(SvErrorCodecType);
  2272. }
  2273. }
  2274. #ifdef MPEG_SUPPORT
  2275. /*
  2276. ** Name: SvDecompressMPEG
  2277. ** Purpose: Decompress the MPEG picture that starts at the current position
  2278. ** of the bitstream. If the bitstream is not properly positioned
  2279. ** then find the next picture.
  2280. **
  2281. ** Args: Svh = handle to software codec's Info structure.
  2282. ** MultiBuf = Specifies pointer to start of the Multibuffer, an area
  2283. ** large enough to hold 3 decompressed images: the
  2284. ** current image, the past reference image and the
  2285. ** future reference image.
  2286. ** MaxMultiSize = Size of the Multibuffer in bytes.
  2287. ** ImagePtr = Returns a pointer to the current image. This will be
  2288. ** somewhere within the Multibuffer.
  2289. */
  2290. SvStatus_t SvDecompressMPEG (SvHandle_t Svh, u_char *MultiBuf,
  2291. int MaxMultiSize, u_char **ImagePtr)
  2292. {
  2293. SvCodecInfo_t *Info = (SvCodecInfo_t *)Svh;
  2294. SvMpegDecompressInfo_t *MDInfo;
  2295. _SlibDebug(_DEBUG_, printf("SvDecompressMPEG()\n") );
  2296. if (!Info)
  2297. return(SvErrorCodecHandle);
  2298. if (!(MDInfo = Info->mdcmp))
  2299. return(SvErrorBadPointer);
  2300. if (!MDInfo->DecompressStarted)
  2301. return(SvErrorDcmpNotStarted);
  2302. return(sv_MpegDecompressFrame(Info, MultiBuf, ImagePtr));
  2303. }
  2304. #endif /* MPEG_SUPPORT */
  2305. #ifdef H261_SUPPORT
  2306. SvStatus_t SvDecompressH261 (SvHandle_t Svh, u_char *MultiBuf,
  2307. int MaxMultiSize, u_char **ImagePtr)
  2308. {
  2309. SvCodecInfo_t *Info = (SvCodecInfo_t *)Svh;
  2310. SvH261Info_t *H261;
  2311. ScCallbackInfo_t CB;
  2312. SvStatus_t status;
  2313. if (!Info)
  2314. return(SvErrorCodecHandle);
  2315. if (!(H261 = Info->h261))
  2316. return(SvErrorBadPointer);
  2317. if (Info->BSIn->EOI)
  2318. return(SvErrorEndBitstream);
  2319. status = svH261Decompress(Info, MultiBuf, ImagePtr);
  2320. if (status == SvErrorEndBitstream && Info->CallbackFunction)
  2321. {
  2322. CB.Message = CB_SEQ_END;
  2323. CB.Data = NULL;
  2324. CB.DataSize = 0;
  2325. CB.DataType = CB_DATA_NONE;
  2326. CB.UserData = Info->BSIn?Info->BSIn->UserData:NULL;
  2327. CB.Action = CB_ACTION_CONTINUE;
  2328. (*Info->CallbackFunction)(Svh, &CB, NULL);
  2329. _SlibDebug(_DEBUG_,
  2330. printf("H261 Callback: CB_SEQ_END Data = 0x%x Action = %d\n",
  2331. CB.Data, CB.Action) );
  2332. if (CB.Action == CB_ACTION_END)
  2333. return (ScErrorClientEnd);
  2334. }
  2335. else if (status==NoErrors)
  2336. {
  2337. *ImagePtr = H261->Y;
  2338. if (Info->CallbackFunction)
  2339. {
  2340. CB.Message = CB_FRAME_READY;
  2341. CB.Data = *ImagePtr;
  2342. CB.DataSize = H261->PICSIZE+(H261->PICSIZE/2);
  2343. CB.DataUsed = CB.DataSize;
  2344. CB.DataType = CB_DATA_IMAGE;
  2345. CB.UserData = Info->BSIn?Info->BSIn->UserData:NULL;
  2346. CB.TimeStamp = 0;
  2347. CB.Flags = 0;
  2348. CB.Value = 0;
  2349. CB.Format = (void *)&Info->OutputFormat;
  2350. CB.Action = CB_ACTION_CONTINUE;
  2351. (*Info->CallbackFunction)(Svh, &CB, NULL);
  2352. _SlibDebug(_DEBUG_,
  2353. printf("H261 Callback: CB_FRAME_READY Data = 0x%x, Action = %d\n",
  2354. CB.Data, CB.Action) );
  2355. if (CB.Action == CB_ACTION_END)
  2356. return (ScErrorClientEnd);
  2357. }
  2358. }
  2359. return (status);
  2360. }
  2361. #endif /* H261_SUPPORT */
  2362. #ifdef JPEG_SUPPORT
  2363. /*---------------------------------------------------------------------
  2364. SLIB routines to Query and return CODEC Tables to caller
  2365. *---------------------------------------------------------------------*/
  2366. /*
  2367. ** From JPEG Spec. :
  2368. ** Huffman tables are specified in terms of a 16-byte list (BITS) giving
  2369. ** the number of codes for each code length from 1 to 16. This is
  2370. ** followed by a list of 8-bit symbol values (HUFVAL), each of which is
  2371. ** assigned a Huffman code. The symbol values are placed in the list
  2372. ** in order of increasing code length. Code length greater than 16-bits
  2373. ** are not allowed.
  2374. */
  2375. /*
  2376. ** Name: SvSetDcmpHTables
  2377. ** Purpose:
  2378. **
  2379. ** Notes: Baseline process is the only supported mode:
  2380. ** - uses 2 AC tables and 2 DC Tables
  2381. **
  2382. */
  2383. SvStatus_t SvSetDcmpHTables (SvHandle_t Svh, SvHuffmanTables_t *Ht)
  2384. {
  2385. SvCodecInfo_t *Info = (SvCodecInfo_t *)Svh;
  2386. int i,stat,count;
  2387. SvHt_t **htblptr;
  2388. SvHTable_t *HTab;
  2389. register int j;
  2390. if (!Info)
  2391. return(SvErrorCodecHandle);
  2392. if (!Ht)
  2393. return(SvErrorBadPointer);
  2394. for (j = 0; j < 4; j++) {
  2395. switch(j) {
  2396. case 0: htblptr = &Info->jdcmp->DcHt[0];
  2397. HTab = &Ht->DcY;
  2398. break;
  2399. case 1: htblptr = &Info->jdcmp->AcHt[0];
  2400. HTab = &Ht->AcY;
  2401. break;
  2402. case 2: htblptr = &Info->jdcmp->DcHt[1];
  2403. HTab = &Ht->DcUV;
  2404. break;
  2405. case 3: htblptr = &Info->jdcmp->AcHt[1];
  2406. HTab = &Ht->AcUV;
  2407. break;
  2408. }
  2409. if (*htblptr == NULL)
  2410. *htblptr = (SvHt_t *) ScPaMalloc(sizeof(SvHt_t));
  2411. (*htblptr)->bits[0] = 0;
  2412. count = 0;
  2413. for (i = 1; i < BITS_LENGTH; i++) {
  2414. (*htblptr)->bits[i] = (u_char)HTab->bits[i-1];
  2415. count += (*htblptr)->bits[i];
  2416. }
  2417. if (count > 256)
  2418. return(SvErrorDHTTable);
  2419. /*
  2420. ** Load Huffman table:
  2421. */
  2422. for (i = 0; i < count; i++)
  2423. (*htblptr)->value[i] = (u_char)HTab->value[i];
  2424. }
  2425. stat = sv_LoadDefaultHTable (Info);
  2426. if (stat) return(stat);
  2427. return(NoErrors);
  2428. }
  2429. /*
  2430. ** Name: SvGetDcmpHTables
  2431. ** Purpose:
  2432. **
  2433. */
  2434. SvStatus_t SvGetDcmpHTables (SvHandle_t Svh, SvHuffmanTables_t *Ht)
  2435. {
  2436. SvCodecInfo_t *Info = (SvCodecInfo_t *)Svh;
  2437. int i,count;
  2438. SvHt_t **htblptr;
  2439. SvHTable_t *HTab;
  2440. register int j;
  2441. if (!Info)
  2442. return (SvErrorCodecHandle);
  2443. if (!Ht)
  2444. return(SvErrorBadPointer);
  2445. for (j = 0; j < 4; j++) {
  2446. switch(j) {
  2447. case 0: htblptr = &Info->jdcmp->DcHt[0];
  2448. HTab = &Ht->DcY;
  2449. break;
  2450. case 1: htblptr = &Info->jdcmp->AcHt[0];
  2451. HTab = &Ht->AcY;
  2452. break;
  2453. case 2: htblptr = &Info->jdcmp->DcHt[1];
  2454. HTab = &Ht->DcUV;
  2455. break;
  2456. case 3: htblptr = &Info->jdcmp->AcHt[1];
  2457. HTab = &Ht->AcUV;
  2458. break;
  2459. }
  2460. if (*htblptr == NULL)
  2461. return(SvErrorHuffUndefined);
  2462. count = 0;
  2463. for (i = 1; i < BITS_LENGTH; i++) {
  2464. HTab->bits[i-1] = (int)(*htblptr)->bits[i];
  2465. count += (*htblptr)->bits[i];
  2466. }
  2467. if (count > 256)
  2468. return(SvErrorDHTTable);
  2469. /*
  2470. ** Copy Huffman table:
  2471. */
  2472. for (i = 0; i < count; i++)
  2473. HTab->value[i] = (u_int)(*htblptr)->value[i];
  2474. }
  2475. return(NoErrors);
  2476. }
  2477. /*
  2478. ** Name: SvSetCompHTables
  2479. ** Purpose:
  2480. **
  2481. */
  2482. SvStatus_t SvSetCompHTables (SvHandle_t Svh, SvHuffmanTables_t *Ht)
  2483. {
  2484. return(SvErrorNotImplemented);
  2485. }
  2486. /*
  2487. ** Name: SvGetCompHTables
  2488. ** Purpose:
  2489. **
  2490. */
  2491. SvStatus_t SvGetCompHTables (SvHandle_t Svh, SvHuffmanTables_t *Ht)
  2492. {
  2493. SvCodecInfo_t *Info = (SvCodecInfo_t *)Svh;
  2494. SvHt_t **htblptr;
  2495. SvHTable_t *HTab;
  2496. register int i, j, count;
  2497. if (!Info)
  2498. return (SvErrorCodecHandle);
  2499. if (!Ht)
  2500. return (SvErrorBadPointer);
  2501. for (j = 0; j < 4; j++) {
  2502. switch(j) {
  2503. case 0: htblptr = &Info->jcomp->DcHt[0];
  2504. HTab = &Ht->DcY;
  2505. break;
  2506. case 1: htblptr = &Info->jcomp->AcHt[0];
  2507. HTab = &Ht->AcY;
  2508. break;
  2509. case 2: htblptr = &Info->jcomp->DcHt[1];
  2510. HTab = &Ht->DcUV;
  2511. break;
  2512. case 3: htblptr = &Info->jcomp->AcHt[1];
  2513. HTab = &Ht->AcUV;
  2514. break;
  2515. }
  2516. if (*htblptr == NULL)
  2517. return (SvErrorHuffUndefined);
  2518. /*
  2519. ** Copy the "bits" array (contains number of codes of each size)
  2520. */
  2521. count = 0;
  2522. for (i = 1; i < BITS_LENGTH; i++) {
  2523. HTab->bits[i-1] = (int)(*htblptr)->bits[i];
  2524. count += (*htblptr)->bits[i];
  2525. }
  2526. if (count > 256)
  2527. /*
  2528. ** total # of Huffman code words cannot exceed 256
  2529. */
  2530. return (SvErrorDHTTable);
  2531. /*
  2532. ** Copy the "value" array (contains values associated with above codes)
  2533. */
  2534. for (i = 0; i < count; i++)
  2535. HTab->value[i] = (u_int)(*htblptr)->value[i];
  2536. }
  2537. return(NoErrors);
  2538. }
  2539. /*
  2540. ** Name: SvSetDcmpQTables
  2541. ** Purpose:
  2542. **
  2543. */
  2544. SvStatus_t SvSetDcmpQTables (SvHandle_t Svh, SvQuantTables_t *Qt)
  2545. {
  2546. SvCodecInfo_t *Info = (SvCodecInfo_t *)Svh;
  2547. SvJpegDecompressInfo_t *DInfo;
  2548. if (!Info)
  2549. return(SvErrorCodecHandle);
  2550. DInfo = (SvJpegDecompressInfo_t *)Info->jdcmp;
  2551. if (!Qt)
  2552. return(SvErrorBadPointer);
  2553. if (DInfo->_SviquantTblPtrs[0] == NULL)
  2554. if ((DInfo->_SviquantTblPtrs[0] = (int *) ScAlloc(64*sizeof(int))) ==
  2555. (int *)NULL) return(SvErrorMemory);
  2556. if (DInfo->_SviquantTblPtrs[1] == NULL)
  2557. if ((DInfo->_SviquantTblPtrs[1] = (int *) ScAlloc(64*sizeof(int))) ==
  2558. (int *)NULL) return(SvErrorMemory);
  2559. bcopy (Qt->c1, DInfo->_SviquantTblPtrs[0], 64*sizeof(int));
  2560. bcopy (Qt->c2, DInfo->_SviquantTblPtrs[1], 64*sizeof(int));
  2561. bcopy (Qt->c3, DInfo->_SviquantTblPtrs[1], 64*sizeof(int));
  2562. return(NoErrors);
  2563. }
  2564. /*
  2565. ** Name: SvGetDcmpQTables
  2566. ** Purpose:
  2567. **
  2568. */
  2569. SvStatus_t SvGetDcmpQTables (SvHandle_t Svh, SvQuantTables_t *Qt)
  2570. {
  2571. SvCodecInfo_t *Info = (SvCodecInfo_t *)Svh;
  2572. SvJpegDecompressInfo_t *DInfo;
  2573. if (!Info)
  2574. return(SvErrorCodecHandle);
  2575. DInfo = (SvJpegDecompressInfo_t *)Info->jdcmp;
  2576. if (!Qt)
  2577. return(SvErrorBadPointer);
  2578. if (DInfo->_SviquantTblPtrs[0])
  2579. bcopy (DInfo->_SviquantTblPtrs[0], Qt->c1, 64*sizeof(int));
  2580. else
  2581. bzero (Qt->c1, 64*sizeof(int));
  2582. if (DInfo->_SviquantTblPtrs[1])
  2583. bcopy(DInfo->_SviquantTblPtrs[1], Qt->c2, 64*sizeof(int));
  2584. else
  2585. bzero(Qt->c2, 64*sizeof(int));
  2586. /*
  2587. ** XXX - when the structure is changed approprately remove the
  2588. ** above and do the following:
  2589. **
  2590. ** if ((!Qt->c1) || (!Qt->c2) || (!Qt->c3))
  2591. ** return (SvErrorBadPointer);
  2592. ** bcopy ((u_char *)DInfo->Qt, (u_char *)Qt, sizeof(SvQuantTables_t));
  2593. */
  2594. return(NoErrors);
  2595. }
  2596. /*
  2597. ** Name: SvSetCompQTables
  2598. ** Purpose: Allows user to set quantization tables directly
  2599. **
  2600. */
  2601. SvStatus_t SvSetCompQTables (SvHandle_t Svh, SvQuantTables_t *Qt)
  2602. {
  2603. SvCodecInfo_t *Info = (SvCodecInfo_t *)Svh;
  2604. if (!Info)
  2605. return (SvErrorCodecHandle);
  2606. if (!Info->jcomp->CompressStarted)
  2607. return (SvErrorCompNotStarted);
  2608. if (!Qt)
  2609. return (SvErrorBadPointer);
  2610. if ((!Qt->c1) || (!Qt->c2) || (!Qt->c3))
  2611. return (SvErrorBadPointer);
  2612. /*
  2613. ** Convert SvQuantTables_t structure to internal SvQt_t structure.
  2614. */
  2615. sv_ConvertQTable(Info, Qt);
  2616. return(NoErrors);
  2617. }
  2618. #endif /* JPEG_SUPPORT */
  2619. /*---------------------------------------------------------------------
  2620. SLIB Compression Routines
  2621. *---------------------------------------------------------------------*/
  2622. /*
  2623. ** Name: SvCompressBegin
  2624. ** Purpose: Initialize the Compression Codec. Call after SvOpenCodec &
  2625. ** before SvCompress (SvCompress will call SvCompressBegin
  2626. ** on first call to codec after open if user doesn't call it)
  2627. **
  2628. ** Args: Svh = handle to software codec's Info structure.
  2629. ** ImgIn = format of input (uncompressed) image
  2630. ** ImgOut = format of output (compressed) image
  2631. */
  2632. SvStatus_t SvCompressBegin (SvHandle_t Svh, BITMAPINFOHEADER *ImgIn,
  2633. BITMAPINFOHEADER *ImgOut)
  2634. {
  2635. int stat;
  2636. SvCodecInfo_t *Info = (SvCodecInfo_t *)Svh;
  2637. /*
  2638. ** Sanity checks:
  2639. */
  2640. if (!Info)
  2641. return (SvErrorCodecHandle);
  2642. if (!ImgIn || !ImgOut)
  2643. return (SvErrorBadPointer);
  2644. stat=SvCompressQuery (Svh, ImgIn, ImgOut);
  2645. RETURN_ON_ERROR(stat);
  2646. /*
  2647. ** Save input & output formats for SvDecompress
  2648. */
  2649. sv_copy_bmh(ImgIn, &Info->InputFormat);
  2650. sv_copy_bmh(ImgOut, &Info->OutputFormat);
  2651. Info->Width = Info->OutputFormat.biWidth;
  2652. Info->Height = abs(Info->OutputFormat.biHeight);
  2653. /*
  2654. ** Initialize - the encoder structure
  2655. ** Load - the default Huffman Tables
  2656. ** Make - the internal Block Table
  2657. */
  2658. switch (Info->mode)
  2659. {
  2660. #ifdef JPEG_SUPPORT
  2661. case SV_JPEG_ENCODE:
  2662. stat = sv_InitJpegEncoder (Info);
  2663. RETURN_ON_ERROR (stat);
  2664. /*
  2665. ** Set up the default quantization matrices:
  2666. */
  2667. stat = SvSetQuality (Svh, DEFAULT_Q_FACTOR);
  2668. Info->jcomp->CompressStarted = TRUE;
  2669. Info->jcomp->Quality = DEFAULT_Q_FACTOR;
  2670. RETURN_ON_ERROR (stat);
  2671. break;
  2672. #endif /* JPEG_SUPPORT */
  2673. #ifdef H261_SUPPORT
  2674. case SV_H261_ENCODE:
  2675. stat = svH261CompressInit(Info);
  2676. RETURN_ON_ERROR (stat);
  2677. break;
  2678. #endif /* H261_SUPPORT */
  2679. #ifdef H263_SUPPORT
  2680. case SV_H263_ENCODE:
  2681. stat = svH263InitCompressor(Info);
  2682. RETURN_ON_ERROR (stat);
  2683. break;
  2684. #endif /* MPEG_SUPPORT */
  2685. #ifdef MPEG_SUPPORT
  2686. case SV_MPEG_ENCODE:
  2687. case SV_MPEG2_ENCODE:
  2688. stat = sv_MpegInitEncoder (Info);
  2689. RETURN_ON_ERROR (stat);
  2690. sv_MpegEncoderBegin(Info);
  2691. break;
  2692. #endif /* MPEG_SUPPORT */
  2693. #ifdef HUFF_SUPPORT
  2694. case SV_HUFF_ENCODE:
  2695. stat = sv_HuffInitEncoder (Info);
  2696. RETURN_ON_ERROR (stat);
  2697. break;
  2698. #endif /* HUFF_SUPPORT */
  2699. default:
  2700. return(SvErrorCodecType);
  2701. }
  2702. return (NoErrors);
  2703. }
  2704. /*
  2705. ** Name: SvCompressEnd
  2706. ** Purpose: Terminate the Compression Codec. Call after all calls to
  2707. ** SvCompress are done.
  2708. **
  2709. ** Args: Svh = handle to software codec's Info structure.
  2710. */
  2711. SvStatus_t SvCompressEnd (SvHandle_t Svh)
  2712. {
  2713. SvCodecInfo_t *Info = (SvCodecInfo_t *)Svh;
  2714. SvCallbackInfo_t CB;
  2715. SvStatus_t status=NoErrors;
  2716. _SlibDebug(_VERBOSE_, printf("SvCompressEnd()\n") );
  2717. if (!Info)
  2718. return (SvErrorCodecHandle);
  2719. switch (Info->mode)
  2720. {
  2721. #ifdef H261_SUPPORT
  2722. case SV_H261_ENCODE:
  2723. status=svH261CompressFree(Svh);
  2724. RETURN_ON_ERROR(status)
  2725. break;
  2726. #endif /* H261_SUPPORT */
  2727. #ifdef H263_SUPPORT
  2728. case SV_H263_ENCODE:
  2729. status=svH263FreeCompressor(Svh);
  2730. RETURN_ON_ERROR(status)
  2731. break;
  2732. #endif /* H263_SUPPORT */
  2733. #ifdef MPEG_SUPPORT
  2734. case SV_MPEG_ENCODE:
  2735. case SV_MPEG2_ENCODE:
  2736. sv_MpegEncoderEnd(Info);
  2737. sv_MpegFreeEncoder(Info);
  2738. break;
  2739. #endif /* MPEG_SUPPORT */
  2740. #ifdef JPEG_SUPPORT
  2741. case SV_JPEG_ENCODE:
  2742. if (!Info->jcomp)
  2743. return (SvErrorMemory);
  2744. Info->jcomp->CompressStarted = FALSE;
  2745. break;
  2746. #endif /* JPEG_SUPPORT */
  2747. #ifdef HUFF_SUPPORT
  2748. case SV_HUFF_ENCODE:
  2749. sv_HuffFreeEncoder(Info);
  2750. break;
  2751. #endif /* HUFF_SUPPORT */
  2752. default:
  2753. break;
  2754. }
  2755. /* Release any Image Buffers in the queue */
  2756. if (Info->ImageQ)
  2757. {
  2758. int datasize;
  2759. while (ScBufQueueGetNum(Info->ImageQ))
  2760. {
  2761. ScBufQueueGetHead(Info->ImageQ, &CB.Data, &datasize);
  2762. ScBufQueueRemove(Info->ImageQ);
  2763. if (Info->CallbackFunction && CB.Data)
  2764. {
  2765. CB.Message = CB_RELEASE_BUFFER;
  2766. CB.DataSize = datasize;
  2767. CB.DataUsed = 0;
  2768. CB.DataType = CB_DATA_IMAGE;
  2769. CB.UserData = Info->BSOut?Info->BSOut->UserData:NULL;
  2770. CB.Action = CB_ACTION_CONTINUE;
  2771. (*(Info->CallbackFunction))(Svh, &CB, NULL);
  2772. _SlibDebug(_DEBUG_,
  2773. printf("SvCompressEnd: RELEASE_BUFFER. Data = 0x%X, Action = %d\n",
  2774. CB.Data, CB.Action) );
  2775. }
  2776. }
  2777. }
  2778. if (Info->BSOut)
  2779. ScBSFlush(Info->BSOut); /* flush out the last compressed data */
  2780. if (Info->CallbackFunction)
  2781. {
  2782. CB.Message = CB_CODEC_DONE;
  2783. CB.Data = NULL;
  2784. CB.DataSize = 0;
  2785. CB.DataUsed = 0;
  2786. CB.DataType = CB_DATA_NONE;
  2787. CB.UserData = Info->BSOut?Info->BSOut->UserData:NULL;
  2788. CB.TimeStamp = 0;
  2789. CB.Flags = 0;
  2790. CB.Value = 0;
  2791. CB.Format = NULL;
  2792. CB.Action = CB_ACTION_CONTINUE;
  2793. (*Info->CallbackFunction)(Svh, &CB, NULL);
  2794. _SlibDebug(_DEBUG_,
  2795. printf("SvCompressEnd Callback: CB_CODEC_DONE Action = %d\n",
  2796. CB.Action) );
  2797. if (CB.Action == CB_ACTION_END)
  2798. return (ScErrorClientEnd);
  2799. }
  2800. return (status);
  2801. }
  2802. /*
  2803. ** Name: SvCompress
  2804. ** Purpose:
  2805. **
  2806. */
  2807. SvStatus_t SvCompress(SvHandle_t Svh, u_char *CompData, int MaxCompLen,
  2808. u_char *Image, int ImageSize, int *CmpBytes)
  2809. {
  2810. SvCodecInfo_t *Info = (SvCodecInfo_t *)Svh;
  2811. SvCallbackInfo_t CB;
  2812. int stat=NoErrors, UsedQ=FALSE;
  2813. _SlibDebug(_DEBUG_, printf("SvCompress()\n") );
  2814. if (!Info)
  2815. return (SvErrorCodecHandle);
  2816. /*
  2817. ** If no image buffer is supplied, see if the Image Queue
  2818. ** has any. If not do a callback to get a buffer.
  2819. */
  2820. if (Image == NULL && Info->ImageQ)
  2821. {
  2822. if (ScBufQueueGetNum(Info->ImageQ))
  2823. {
  2824. ScBufQueueGetHead(Info->ImageQ, &Image, &ImageSize);
  2825. ScBufQueueRemove(Info->ImageQ);
  2826. UsedQ = TRUE;
  2827. }
  2828. else if (Info->CallbackFunction)
  2829. {
  2830. CB.Message = CB_END_BUFFERS;
  2831. CB.Data = NULL;
  2832. CB.DataSize = 0;
  2833. CB.DataUsed = 0;
  2834. CB.DataType = CB_DATA_IMAGE;
  2835. CB.UserData = Info->BSOut?Info->BSOut->UserData:NULL;
  2836. CB.Action = CB_ACTION_CONTINUE;
  2837. (*(Info->CallbackFunction))(Svh, &CB, NULL);
  2838. if (CB.Action == CB_ACTION_END)
  2839. {
  2840. _SlibDebug(_DEBUG_,
  2841. printf("SvDecompress() CB.Action = CB_ACTION_END\n") );
  2842. return(SvErrorClientEnd);
  2843. }
  2844. else if (ScBufQueueGetNum(Info->ImageQ))
  2845. {
  2846. ScBufQueueGetHead(Info->ImageQ, &Image, &ImageSize);
  2847. ScBufQueueRemove(Info->ImageQ);
  2848. UsedQ = TRUE;
  2849. }
  2850. else
  2851. return(SvErrorNoImageBuffer);
  2852. }
  2853. }
  2854. if (Image == NULL)
  2855. return(SvErrorNoImageBuffer);
  2856. switch (Info->mode)
  2857. {
  2858. #ifdef H261_SUPPORT
  2859. case SV_H261_ENCODE:
  2860. stat = svH261Compress(Svh, Image);
  2861. if (CmpBytes)
  2862. *CmpBytes = (int)(Info->h261->TotalBits/8);
  2863. break;
  2864. #endif /* H261_SUPPORT */
  2865. #ifdef H263_SUPPORT
  2866. case SV_H263_ENCODE:
  2867. stat = svH263Compress(Svh, Image);
  2868. break;
  2869. #endif /* H261_SUPPORT */
  2870. #ifdef MPEG_SUPPORT
  2871. case SV_MPEG_ENCODE:
  2872. case SV_MPEG2_ENCODE:
  2873. stat = sv_MpegEncodeFrame(Svh, Image);
  2874. break;
  2875. #endif /* MPEG_SUPPORT */
  2876. #ifdef JPEG_SUPPORT
  2877. case SV_JPEG_ENCODE:
  2878. {
  2879. SvJpegCompressInfo_t *CInfo;
  2880. u_char *CompBuffer;
  2881. register int i;
  2882. int RetBytes, InLen;
  2883. CInfo = Info->jcomp;
  2884. /*
  2885. ** In case the application forgot to call SvCompressBegin().
  2886. */
  2887. if (!CInfo->CompressStarted)
  2888. return (SvErrorCompNotStarted);
  2889. if ((u_int)Image%8)
  2890. return (SvErrorNotAligned);
  2891. CompBuffer = CompData;
  2892. /*
  2893. ** Start - add header information
  2894. ** - needed if we want to conform to the interchange format
  2895. */
  2896. stat = sv_AddJpegHeader (Svh, CompBuffer, MaxCompLen, &RetBytes);
  2897. RETURN_ON_ERROR (stat);
  2898. CompBuffer += RetBytes;
  2899. /*
  2900. ** Separate input image directly into 8x8 blocks.
  2901. ** level shift to go from signed to unsigned representation
  2902. ** - since we support baseline DCT process only (i.e 8-bit
  2903. ** precision) subtract raw data by 128
  2904. */
  2905. sv_JpegExtractBlocks (Info, Image);
  2906. for (i = 0; i < CInfo->NumComponents; i++)
  2907. CInfo->lastDcVal[i] = 0;
  2908. /*
  2909. ** JPEG business loop:
  2910. */
  2911. {
  2912. register int Cid, HQid, blkN, mcuN, mbn, DcVal;
  2913. float *FQt, *FThresh, *FThreshNeg;
  2914. float *RawData;
  2915. SvRLE_t rle;
  2916. const static long Mask = 0xffL;
  2917. float DCTData[64];
  2918. register float tmp,AcVal;
  2919. CB.Message = CB_PROCESSING;
  2920. /*
  2921. ** Processing within a frame is done on a MCU by MCU basis:
  2922. */
  2923. for (blkN = 0, mcuN = 0 ; mcuN < (int) CInfo->NumMCU; mcuN++)
  2924. {
  2925. /*
  2926. ** Callback user routine every now&then to see if we should abort
  2927. */
  2928. if ((Info->CallbackFunction) && ((i % MCU_CALLBACK_COUNT) == 0))
  2929. {
  2930. SvPictureInfo_t DummyPictInfo;
  2931. (*Info->CallbackFunction)(Svh, &CB, &DummyPictInfo);
  2932. if (CB.Action == CB_ACTION_END)
  2933. return(SvErrorClientEnd);
  2934. }
  2935. /*
  2936. ** Account for restart interval, emit restart marker if needed
  2937. */
  2938. if (CInfo->restartInterval)
  2939. {
  2940. if (CInfo->restartsToGo == 0)
  2941. EmitRestart (CInfo);
  2942. CInfo->restartsToGo--;
  2943. }
  2944. /*
  2945. ** Processing within an MCU is done on a block by block basis:
  2946. */
  2947. for (mbn = 0; mbn < (int) CInfo->BlocksInMCU; mbn++, blkN++)
  2948. {
  2949. /*
  2950. ** Figure out the component to which the current block belongs:
  2951. ** -Due to the way input data is processed by "sv_extract_blocks"
  2952. ** and under the assumption that the input is YCrCb,
  2953. ** Cid is 0,0,1,2 for each block in the MCU
  2954. */
  2955. switch (mbn) {
  2956. case 0:
  2957. case 1: Cid = 0; HQid = 0; break;
  2958. case 2: Cid = 1; HQid = 1; break;
  2959. case 3: Cid = 2; HQid = 1; break;
  2960. }
  2961. RawData = CInfo->BlkTable[blkN];
  2962. #ifndef _NO_DCT_
  2963. /*
  2964. ** Discrete Cosine Transform:
  2965. ** Perform the Forward DCT, take the input data from "RawData"
  2966. ** and place the computed coefficients in "DCTData":
  2967. */
  2968. ScFDCT8x8 (RawData, DCTData);
  2969. #ifndef _NO_QUANT_
  2970. /*
  2971. ** Quantization:
  2972. **
  2973. ** Identify the quantization tables:
  2974. */
  2975. FQt = (float *) (CInfo->Qt[HQid])->fval;
  2976. FThresh = (float *) (CInfo->Qt[HQid])->fthresh;
  2977. FThreshNeg = (float *) (CInfo->Qt[HQid])->fthresh_neg;
  2978. /*
  2979. ** Quantize the DC value first:
  2980. */
  2981. tmp = DCTData[0] *FQt[0];
  2982. if (tmp < 0)
  2983. DcVal = (int) (tmp - 0.5);
  2984. else
  2985. DcVal = (int) (tmp + 0.5);
  2986. /*
  2987. ** Go after (quantize) the AC coefficients now:
  2988. */
  2989. for (rle.numAC = 0, i = 1; i < 64; i++)
  2990. {
  2991. AcVal = DCTData[ZagIndex[i]];
  2992. if (AcVal > FThresh[i]) {
  2993. rle.ac[rle.numAC].index = i;
  2994. rle.ac[rle.numAC++].value = (int) (AcVal * FQt[i] + 0.5);
  2995. }
  2996. else if (AcVal < FThreshNeg[i]) {
  2997. rle.ac[rle.numAC].index = i;
  2998. rle.ac[rle.numAC++].value = (int) (AcVal * FQt[i] - 0.5);
  2999. }
  3000. }
  3001. /*
  3002. ** DPCM coding:
  3003. **
  3004. ** Difference encoding of the DC value,
  3005. */
  3006. rle.dc = DcVal - CInfo->lastDcVal[Cid];
  3007. CInfo->lastDcVal[Cid] = DcVal;
  3008. #ifndef _NO_HUFF_
  3009. /*
  3010. ** Entropy Coding:
  3011. **
  3012. ** Huffman encode the current block
  3013. */
  3014. sv_EncodeOneBlock (&rle, CInfo->DcHt[HQid], CInfo->AcHt[HQid]);
  3015. FlushBytes(&CompBuffer);
  3016. #endif /* _NO_HUFF_ */
  3017. #endif /* _NO_QUANT_ */
  3018. #endif /* _NO_DCT_ */
  3019. }
  3020. }
  3021. }
  3022. (void ) sv_HuffEncoderTerm (&CompBuffer);
  3023. Info->OutputFormat.biSize = CompBuffer - CompData;
  3024. InLen = MaxCompLen - Info->OutputFormat.biSize;
  3025. /*
  3026. ** JPEG End:
  3027. ** - add trailer information to the compressed bitstream,
  3028. ** - needed if we want to conform to the interchange format
  3029. */
  3030. stat = sv_AddJpegTrailer (Svh, CompBuffer, InLen, &RetBytes);
  3031. RETURN_ON_ERROR (stat);
  3032. CompBuffer += RetBytes;
  3033. Info->OutputFormat.biSize += RetBytes;
  3034. if (CmpBytes)
  3035. *CmpBytes = CompBuffer - CompData;
  3036. }
  3037. break;
  3038. #endif /* JPEG_SUPPORT */
  3039. #ifdef HUFF_SUPPORT
  3040. case SV_HUFF_ENCODE:
  3041. stat = sv_HuffEncodeFrame(Svh, Image);
  3042. break;
  3043. #endif /* HUFF_SUPPORT */
  3044. default:
  3045. return(SvErrorCodecType);
  3046. }
  3047. Info->NumOperations++;
  3048. /*
  3049. ** If an Image buffer was taken from the queue, do a callback
  3050. ** to let the client free or re-use the buffer.
  3051. */
  3052. if (Info->CallbackFunction && UsedQ)
  3053. {
  3054. CB.Message = CB_RELEASE_BUFFER;
  3055. CB.Data = Image;
  3056. CB.DataSize = ImageSize;
  3057. CB.DataUsed = ImageSize;
  3058. CB.DataType = CB_DATA_IMAGE;
  3059. CB.UserData = Info->BSOut?Info->BSOut->UserData:NULL;
  3060. CB.Action = CB_ACTION_CONTINUE;
  3061. (*(Info->CallbackFunction))(Svh, &CB, NULL);
  3062. _SlibDebug(_DEBUG_,
  3063. printf("Compress Callback: RELEASE_BUFFER Addr=0x%x, Action=%d\n",
  3064. CB.Data, CB.Action) );
  3065. if (CB.Action == CB_ACTION_END)
  3066. return(SvErrorClientEnd);
  3067. }
  3068. return (stat);
  3069. }
  3070. static SvStatus_t sv_ConvertRGBToSepComponent(u_char *Iimage,
  3071. BITMAPINFOHEADER * Bmh, u_char *comp1, u_char *comp2, u_char *comp3,
  3072. int pixels, int lines)
  3073. {
  3074. register i;
  3075. int bpp = Bmh->biBitCount;
  3076. u_int *Ip = (u_int *)Iimage;
  3077. u_short *Is = (u_short *)Iimage;
  3078. if (bpp == 24) {
  3079. if (Bmh->biCompression == BI_RGB) {
  3080. for (i = 0 ; i < pixels*lines ; i++) {
  3081. comp3[i] = *Iimage++; /* Blue */
  3082. comp2[i] = *Iimage++; /* Green */
  3083. comp1[i] = *Iimage++; /* Red */
  3084. }
  3085. }
  3086. else if (Bmh->biCompression == BI_DECXIMAGEDIB) {
  3087. /* RGBQUAD structures: (B,G,R,0) */
  3088. for (i = 0 ; i < pixels*lines ; i++) {
  3089. comp3[i] = *Iimage++; /* Blue */
  3090. comp2[i] = *Iimage++; /* Green */
  3091. comp1[i] = *Iimage++; /* Red */
  3092. Iimage++; /* Reserved */
  3093. }
  3094. }
  3095. }
  3096. else if (bpp == 32) { /* RGBQUAD structures: (B,G,R,0) */
  3097. for (i = 0 ; i < pixels*lines ; i++) {
  3098. comp3[i] = (Ip[i] >> 24) & 0xFF;
  3099. comp2[i] = (Ip[i] >> 16) & 0xFF;
  3100. comp1[i] = (Ip[i] >> 8) & 0xFF;
  3101. }
  3102. }
  3103. else if (bpp == 16) {
  3104. for (i = 0 ; i < pixels*lines ; i++) {
  3105. comp1[i] = (Is[i] >> 7) & 0xf8;
  3106. comp2[i] = (Is[i] >> 2) & 0xf8;
  3107. comp3[i] = (Is[i] << 3) & 0xf8;
  3108. }
  3109. }
  3110. return (NoErrors);
  3111. }
  3112. /*
  3113. ** Name: SvCompressQuery
  3114. ** Purpose: Determine if Codec can Compress desired format
  3115. **
  3116. ** Args: Svh = handle to software codec's Info structure.
  3117. ** ImgIn = Pointer to BITMAPINFOHEADER structure describing format
  3118. ** ImgOut = Pointer to BITMAPINFOHEADER structure describing format
  3119. */
  3120. SvStatus_t SvCompressQuery (SvHandle_t Svh, BITMAPINFOHEADER *ImgIn,
  3121. BITMAPINFOHEADER *ImgOut)
  3122. {
  3123. /*
  3124. ** We don't *really* need the Info structures, but we check for
  3125. ** NULL pointers to make sure the CODEC, whoes ability is being
  3126. ** queried, was at least opened.
  3127. */
  3128. SvCodecInfo_t *Info = (SvCodecInfo_t *)Svh;
  3129. if (!Info)
  3130. return(SvErrorCodecHandle);
  3131. if (!ImgIn && !ImgOut)
  3132. return(SvErrorBadPointer);
  3133. if (!IsSupported(_SvCompressionSupport,
  3134. ImgIn ? ImgIn->biCompression : -1,
  3135. ImgIn ? ImgIn->biBitCount : -1,
  3136. ImgOut ? ImgOut->biCompression : -1,
  3137. ImgOut ? ImgOut->biBitCount : -1))
  3138. return(SvErrorUnrecognizedFormat);
  3139. /*
  3140. ** For speed we impose a restriction that the image size should be
  3141. ** a multiple of 16x8. This insures that we would have at least one
  3142. ** MCU for a 4:2:2 image
  3143. **
  3144. ** NOTE: This is an artificial restriction from JPEG's perspective.
  3145. ** In the case when the dimesnsions are otherwise, we should
  3146. ** pixel replicate and/or line replicate before compressing.
  3147. */
  3148. if (ImgIn)
  3149. {
  3150. if (ImgIn->biWidth <= 0 || ImgIn->biHeight == 0)
  3151. return(SvErrorBadImageSize);
  3152. if ((ImgIn->biWidth%16) || (ImgIn->biHeight%8))
  3153. return (SvErrorNotImplemented);
  3154. }
  3155. if (ImgOut) /* Query Output also */
  3156. {
  3157. if (ImgOut->biWidth <= 0 || ImgOut->biHeight == 0)
  3158. return (SvErrorBadImageSize);
  3159. if (ImgOut->biCompression == BI_DECH261DIB)
  3160. {
  3161. if ((ImgOut->biWidth != CIF_WIDTH && ImgOut->biWidth != QCIF_WIDTH) ||
  3162. (abs(ImgOut->biHeight) != CIF_HEIGHT && abs(ImgOut->biHeight) != QCIF_HEIGHT))
  3163. return (SvErrorBadImageSize);
  3164. }
  3165. }
  3166. return(NoErrors);
  3167. }
  3168. /*
  3169. ** Name: SvGetCompressSize
  3170. ** Purpose:
  3171. **
  3172. */
  3173. SvStatus_t SvGetCompressSize (SvHandle_t Svh, int *MaxSize)
  3174. {
  3175. SvCodecInfo_t *Info = (SvCodecInfo_t *)Svh;
  3176. if (!Info)
  3177. return (SvErrorCodecHandle);
  3178. if (!MaxSize)
  3179. return (SvErrorBadPointer);
  3180. /*
  3181. ** We are being extra cautious here, it would reflect poorly on the JPEG
  3182. ** commitee is the compressed bitstream was so big
  3183. */
  3184. *MaxSize = 2 * Info->InputFormat.biWidth * abs(Info->InputFormat.biHeight);
  3185. return(NoErrors);
  3186. }
  3187. #ifdef JPEG_SUPPORT
  3188. /*
  3189. ** Name: SvGetQuality
  3190. ** Purpose:
  3191. **
  3192. */
  3193. SvStatus_t SvGetQuality (SvHandle_t Svh, int *Quality)
  3194. {
  3195. SvCodecInfo_t *Info = (SvCodecInfo_t *)Svh;
  3196. if (!Info)
  3197. return (SvErrorCodecHandle);
  3198. if (!Quality)
  3199. return (SvErrorBadPointer);
  3200. *Quality = Info->jcomp->Quality;
  3201. return (NoErrors);
  3202. }
  3203. #endif /* JPEG_SUPPORT */
  3204. #ifdef JPEG_SUPPORT
  3205. /*
  3206. ** Name: SvSetQuality
  3207. ** Purpose:
  3208. **
  3209. */
  3210. SvStatus_t SvSetQuality (SvHandle_t Svh, int Quality)
  3211. {
  3212. int stat,ConvertedQuality;
  3213. SvCodecInfo_t *Info = (SvCodecInfo_t *)Svh;
  3214. if (!Info)
  3215. return (SvErrorCodecHandle);
  3216. if ((Quality < 0) || (Quality > 10000))
  3217. return (SvErrorValue);
  3218. Info->jcomp->Quality = Quality;
  3219. ConvertedQuality = 10000 - Quality;
  3220. if (ConvertedQuality < MIN_QUAL)
  3221. ConvertedQuality = MIN_QUAL;
  3222. stat = sv_MakeQTables (ConvertedQuality, Info);
  3223. return (stat);
  3224. }
  3225. #endif /* JPEG_SUPPORT */
  3226. #ifdef JPEG_SUPPORT
  3227. /*
  3228. ** Name: SvGetCompQTables
  3229. ** Purpose:
  3230. **
  3231. */
  3232. SvStatus_t SvGetCompQTables (SvHandle_t Svh, SvQuantTables_t *Qt)
  3233. {
  3234. register int i;
  3235. SvCodecInfo_t *Info = (SvCodecInfo_t *)Svh;
  3236. if (!Info)
  3237. return (SvErrorCodecHandle);
  3238. if (!Info->jcomp->CompressStarted)
  3239. return (SvErrorCompNotStarted);
  3240. if (!Qt)
  3241. return (SvErrorBadPointer);
  3242. if ((!Qt->c1) || (!Qt->c2) || (!Qt->c3))
  3243. return (SvErrorBadPointer);
  3244. for (i = 0 ; i < 64 ; i++) {
  3245. register int zz = ZigZag[i];
  3246. Qt->c1[i] = (Info->jcomp->Qt[0])->ival[zz];
  3247. Qt->c2[i] = (Info->jcomp->Qt[1])->ival[zz];
  3248. Qt->c3[i] = (Info->jcomp->Qt[1])->ival[zz];
  3249. }
  3250. return(NoErrors);
  3251. }
  3252. #endif /* JPEG_SUPPORT */
  3253. /*
  3254. ** Name: SvGetCodecInfo
  3255. ** Purpose: Get info about the codec & the data
  3256. **
  3257. ** Args: Svh = handle to software codec's Info structure.
  3258. **
  3259. ** XXX - does not work for compression, this has to wait for the
  3260. ** decompressor to use SvCodecInfo_t struct for this to work
  3261. */
  3262. SvStatus_t SvGetInfo (SvHandle_t Svh, SV_INFO_t *lpinfo, BITMAPINFOHEADER *Bmh)
  3263. {
  3264. SvCodecInfo_t *Info = (SvCodecInfo_t *)Svh;
  3265. if (!Info)
  3266. return(SvErrorCodecHandle);
  3267. lpinfo->Version = SLIB_VERSION;
  3268. switch (Info->mode)
  3269. {
  3270. #ifdef JPEG_SUPPORT
  3271. case SV_JPEG_ENCODE:
  3272. lpinfo->CodecStarted = Info->jcomp->CompressStarted;
  3273. break;
  3274. case SV_JPEG_DECODE:
  3275. lpinfo->CodecStarted = Info->jdcmp->DecompressStarted;
  3276. break;
  3277. #endif /* JPEG_SUPPORT */
  3278. default:
  3279. lpinfo->CodecStarted = 0;
  3280. break;
  3281. }
  3282. lpinfo->NumOperations = Info->NumOperations;
  3283. *Bmh = Info->InputFormat;
  3284. return(NoErrors);
  3285. }
  3286. /*
  3287. ** Name: sv_GetComponentPointers
  3288. ** Purpose: Given a pointer to an image and its size,
  3289. ** return pointers to the individual image components
  3290. **
  3291. ** Args: pixels = number of pixels in a line.
  3292. ** lines = number of lines in image.
  3293. ** Image = Pointer to start of combined image data
  3294. ** MaxLen = Size of image data in bytes
  3295. ** comp1/2/3= pointers to pointers to individual components
  3296. */
  3297. static SvStatus_t sv_GetYUVComponentPointers(int biCompression,
  3298. int pixels, int lines, u_char *Image,
  3299. int MaxLen, u_char **comp1, u_char **comp2, u_char **comp3)
  3300. {
  3301. u_int sz1,sz2,sz3,maxlen;
  3302. sz1 = pixels * lines;
  3303. sz2 = sz3 = (IsYUV411Sep(biCompression)) ? (sz1 / 4) :
  3304. ((IsYUV1611Sep(biCompression)) ? (pixels * lines / 16)
  3305. : (sz1 / 2));
  3306. maxlen = (MaxLen > 0) ? (u_int) MaxLen : 0 ;
  3307. if (biCompression == BI_DECGRAYDIB) {
  3308. if (sz1 > maxlen)
  3309. return(SvErrorBadImageSize);
  3310. *comp1 = Image;
  3311. *comp2 = NULL;
  3312. *comp3 = NULL;
  3313. }
  3314. else {
  3315. if ((sz1+sz2+sz3) > maxlen)
  3316. return(SvErrorBadImageSize);
  3317. *comp1 = Image;
  3318. *comp2 = Image + sz1;
  3319. *comp3 = Image + sz1 + sz2;
  3320. }
  3321. return(SvErrorNone);
  3322. }
  3323. #ifdef JPEG_SUPPORT
  3324. /*
  3325. ** Name: sv_JpegExtractBlocks
  3326. ** Purpose:
  3327. **
  3328. ** Note: If we did our job right, memory for all global structures should
  3329. ** have been allocated by the upper layers, we do not waste time
  3330. ** checking for NULL pointers at this point
  3331. **
  3332. */
  3333. static SvStatus_t sv_JpegExtractBlocks (SvCodecInfo_t *Info, u_char *RawImage)
  3334. {
  3335. SvJpegCompressInfo_t *CInfo = (SvJpegCompressInfo_t *)Info->jcomp;
  3336. int size = Info->Width * Info->Height;
  3337. u_char *TempImage;
  3338. SvStatus_t stat;
  3339. if (IsYUV422Packed(Info->InputFormat.biCompression))
  3340. /*
  3341. ** This will extract chunks of 64 bytes (8x8 blocks) from the uncompressed
  3342. ** 4:2:2 interleaved input video frame and place them in three separate
  3343. ** linear arrays for later processing.
  3344. ** XXX - should also do level shifting in this routine
  3345. **
  3346. */
  3347. ScConvert422iTo422sf_C(RawImage, 16,
  3348. (float *)(CInfo->BlkBuffer),
  3349. (float *)(CInfo->BlkBuffer + size),
  3350. (float *)(CInfo->BlkBuffer + size + size/2),
  3351. Info->Width,
  3352. Info->Height);
  3353. else if (IsYUV422Sep(Info->InputFormat.biCompression))
  3354. /*
  3355. ** Same but RawImage is not interleaved. Three components are sequential.
  3356. */
  3357. ScConvertSep422ToBlockYUV (RawImage, 16,
  3358. (float *)(CInfo->BlkBuffer),
  3359. (float *)(CInfo->BlkBuffer + size),
  3360. (float *)(CInfo->BlkBuffer + size + size/2),
  3361. Info->Width,
  3362. Info->Height);
  3363. else if (Info->InputFormat.biCompression == BI_DECGRAYDIB)
  3364. /*
  3365. ** Grayscale: one component
  3366. */
  3367. ScConvertGrayToBlock (RawImage,
  3368. 8,
  3369. (float *)(CInfo->BlkBuffer),
  3370. Info->Width,
  3371. Info->Height);
  3372. if ((Info->InputFormat.biCompression == BI_RGB) ||
  3373. (Info->InputFormat.biCompression == BI_DECXIMAGEDIB) ||
  3374. (ValidateBI_BITFIELDS(&Info->InputFormat) != InvalidBI_BITFIELDS))
  3375. {
  3376. TempImage = (u_char *)ScPaMalloc (3 * Info->Width * Info->Height);
  3377. if (TempImage == NULL)
  3378. return(ScErrorMemory);
  3379. stat = ScRgbInterlToYuvInterl(
  3380. &Info->InputFormat,
  3381. (int)Info->Width,
  3382. (int)Info->Height,
  3383. RawImage,
  3384. (u_short *) TempImage);
  3385. RETURN_ON_ERROR (stat);
  3386. ScConvert422iTo422sf_C(
  3387. TempImage,
  3388. 16,
  3389. (float *)(CInfo->BlkBuffer),
  3390. (float *)(CInfo->BlkBuffer + size),
  3391. (float *)(CInfo->BlkBuffer + size + size/2),
  3392. Info->Width,
  3393. Info->Height);
  3394. ScPaFree(TempImage);
  3395. }
  3396. return (NoErrors);
  3397. }
  3398. #endif /* JPEG_SUPPORT */
  3399. #ifdef JPEG_SUPPORT
  3400. /*
  3401. ** Name: SvSetQuantMode
  3402. ** Purpose: Used only in the "Q Conversion" program "jpegconvert" to
  3403. ** set a flag in the comp & decomp info structures that causes
  3404. ** the quantization algorithm to use the new or old versions
  3405. ** of JPEG quantization.
  3406. **
  3407. */
  3408. SvStatus_t SvSetQuantMode (SvHandle_t Svh, int QuantMode)
  3409. {
  3410. SvCodecInfo_t *Info = (SvCodecInfo_t *)Svh;
  3411. if (!Info)
  3412. return (SvErrorCodecHandle);
  3413. if ((QuantMode != SV_JPEG_QUANT_OLD) && (QuantMode != SV_JPEG_QUANT_NEW))
  3414. return (SvErrorValue);
  3415. if (Info->jdcmp)
  3416. Info->jdcmp->QuantMode = QuantMode;
  3417. if (Info->jcomp)
  3418. Info->jcomp->QuantMode = QuantMode;
  3419. return (NoErrors);
  3420. }
  3421. #endif /* JPEG_SUPPORT */
  3422. /*
  3423. ** Name: SvSetParamBoolean()
  3424. ** Desc: Generic call used to set specific BOOLEAN (TRUE or FALSE) parameters
  3425. ** of the CODEC.
  3426. */
  3427. SvStatus_t SvSetParamBoolean(SvHandle_t Svh, SvParameter_t param,
  3428. ScBoolean_t value)
  3429. {
  3430. SvCodecInfo_t *Info = (SvCodecInfo_t *)Svh;
  3431. if (!Info)
  3432. return(SvErrorCodecHandle);
  3433. _SlibDebug(_VERBOSE_, printf("SvSetParamBoolean()\n") );
  3434. switch (Info->mode)
  3435. {
  3436. #ifdef MPEG_SUPPORT
  3437. case SV_MPEG_DECODE:
  3438. case SV_MPEG2_DECODE:
  3439. case SV_MPEG_ENCODE:
  3440. case SV_MPEG2_ENCODE:
  3441. sv_MpegSetParamBoolean(Svh, param, value);
  3442. break;
  3443. #endif /* MPEG_SUPPORT */
  3444. #ifdef H261_SUPPORT
  3445. case SV_H261_DECODE:
  3446. case SV_H261_ENCODE:
  3447. svH261SetParamBoolean(Svh, param, value);
  3448. break;
  3449. #endif /* H263_SUPPORT */
  3450. #ifdef H263_SUPPORT
  3451. case SV_H263_DECODE:
  3452. case SV_H263_ENCODE:
  3453. svH263SetParamBoolean(Svh, param, value);
  3454. break;
  3455. #endif /* H263_SUPPORT */
  3456. default:
  3457. return(SvErrorCodecType);
  3458. }
  3459. return(NoErrors);
  3460. }
  3461. /*
  3462. ** Name: SvSetParamInt()
  3463. ** Desc: Generic call used to set specific INTEGER (qword) parameters
  3464. ** of the CODEC.
  3465. */
  3466. SvStatus_t SvSetParamInt(SvHandle_t Svh, SvParameter_t param, qword value)
  3467. {
  3468. SvCodecInfo_t *Info = (SvCodecInfo_t *)Svh;
  3469. if (!Info)
  3470. return(SvErrorCodecHandle);
  3471. _SlibDebug(_VERBOSE_, printf("SvSetParamInt()\n") );
  3472. switch (Info->mode)
  3473. {
  3474. #ifdef MPEG_SUPPORT
  3475. case SV_MPEG_DECODE:
  3476. case SV_MPEG2_DECODE:
  3477. case SV_MPEG_ENCODE:
  3478. case SV_MPEG2_ENCODE:
  3479. sv_MpegSetParamInt(Svh, param, value);
  3480. break;
  3481. #endif /* MPEG_SUPPORT */
  3482. #ifdef H261_SUPPORT
  3483. case SV_H261_DECODE:
  3484. case SV_H261_ENCODE:
  3485. svH261SetParamInt(Svh, param, value);
  3486. break;
  3487. #endif /* H261_SUPPORT */
  3488. #ifdef H263_SUPPORT
  3489. case SV_H263_DECODE:
  3490. case SV_H263_ENCODE:
  3491. svH263SetParamInt(Svh, param, value);
  3492. break;
  3493. #endif /* H263_SUPPORT */
  3494. default:
  3495. return(SvErrorCodecType);
  3496. }
  3497. return(NoErrors);
  3498. }
  3499. /*
  3500. ** Name: SvSetParamFloat()
  3501. ** Desc: Generic call used to set specific FLOAT parameters of the CODEC.
  3502. */
  3503. SvStatus_t SvSetParamFloat(SvHandle_t Svh, SvParameter_t param, float value)
  3504. {
  3505. SvCodecInfo_t *Info = (SvCodecInfo_t *)Svh;
  3506. if (!Info)
  3507. return(SvErrorCodecHandle);
  3508. _SlibDebug(_VERBOSE_, printf("SvSetParamFloat()\n") );
  3509. switch (Info->mode)
  3510. {
  3511. #ifdef MPEG_SUPPORT
  3512. case SV_MPEG_DECODE:
  3513. case SV_MPEG2_DECODE:
  3514. case SV_MPEG_ENCODE:
  3515. case SV_MPEG2_ENCODE:
  3516. sv_MpegSetParamFloat(Svh, param, value);
  3517. break;
  3518. #endif /* MPEG_SUPPORT */
  3519. #ifdef H261_SUPPORT
  3520. case SV_H261_DECODE:
  3521. case SV_H261_ENCODE:
  3522. svH261SetParamFloat(Svh, param, value);
  3523. break;
  3524. #endif /* H261_SUPPORT */
  3525. #ifdef H263_SUPPORT
  3526. case SV_H263_DECODE:
  3527. case SV_H263_ENCODE:
  3528. svH263SetParamFloat(Svh, param, value);
  3529. break;
  3530. #endif /* H263_SUPPORT */
  3531. default:
  3532. return(SvErrorCodecType);
  3533. }
  3534. return(NoErrors);
  3535. }
  3536. /*
  3537. ** Name: SvGetParamBoolean()
  3538. ** Desc: Generic call used to get the setting of specific BOOLEAN (TRUE or FALSE)
  3539. ** parameters of the CODEC.
  3540. */
  3541. ScBoolean_t SvGetParamBoolean(SvHandle_t Svh, SvParameter_t param)
  3542. {
  3543. SvCodecInfo_t *Info = (SvCodecInfo_t *)Svh;
  3544. if (!Info)
  3545. return(FALSE);
  3546. switch (Info->mode)
  3547. {
  3548. #ifdef JPEG_SUPPORT
  3549. /* this code should be moved into JPEG codec: svJPEGGetParamBoolean() */
  3550. case SV_JPEG_DECODE:
  3551. case SV_JPEG_ENCODE:
  3552. switch (param)
  3553. {
  3554. case SV_PARAM_BITSTREAMING:
  3555. return(FALSE); /* this is a frame-based codecs */
  3556. }
  3557. break;
  3558. #endif /* JPEG_SUPPORT */
  3559. #ifdef MPEG_SUPPORT
  3560. case SV_MPEG_DECODE:
  3561. case SV_MPEG2_DECODE:
  3562. case SV_MPEG_ENCODE:
  3563. case SV_MPEG2_ENCODE:
  3564. return(sv_MpegGetParamBoolean(Svh, param));
  3565. break;
  3566. #endif /* MPEG_SUPPORT */
  3567. #ifdef H261_SUPPORT
  3568. /* this code should be moved into H261 codec: svH261GetParamBoolean() */
  3569. case SV_H261_DECODE:
  3570. case SV_H261_ENCODE:
  3571. return(svH261GetParamBoolean(Svh, param));
  3572. break;
  3573. #endif /* H261_SUPPORT */
  3574. #ifdef H263_SUPPORT
  3575. case SV_H263_DECODE:
  3576. case SV_H263_ENCODE:
  3577. return(svH263GetParamBoolean(Svh, param));
  3578. break;
  3579. #endif /* H263_SUPPORT */
  3580. }
  3581. return(FALSE);
  3582. }
  3583. /*
  3584. ** Name: SvGetParamInt()
  3585. ** Desc: Generic call used to get the setting of specific INTEGER (qword)
  3586. ** parameters of the CODEC.
  3587. */
  3588. qword SvGetParamInt(SvHandle_t Svh, SvParameter_t param)
  3589. {
  3590. SvCodecInfo_t *Info = (SvCodecInfo_t *)Svh;
  3591. if (!Info)
  3592. return(0);
  3593. switch (Info->mode)
  3594. {
  3595. #ifdef JPEG_SUPPORT
  3596. /* this code should be moved into JPEG codec: svJPEGGetParamInt() */
  3597. case SV_JPEG_DECODE:
  3598. case SV_JPEG_ENCODE:
  3599. switch (param)
  3600. {
  3601. case SV_PARAM_NATIVEFORMAT:
  3602. return(BI_YU16SEP);
  3603. }
  3604. break;
  3605. #endif /* JPEG_SUPPORT */
  3606. #ifdef H261_SUPPORT
  3607. /* this code should be moved into H261 codec: svH261GetParamInt() */
  3608. case SV_H261_DECODE:
  3609. case SV_H261_ENCODE:
  3610. return(svH261GetParamInt(Svh, param));
  3611. break;
  3612. #endif /* H261_SUPPORT */
  3613. #ifdef H263_SUPPORT
  3614. case SV_H263_DECODE:
  3615. case SV_H263_ENCODE:
  3616. return(svH263GetParamInt(Svh, param));
  3617. break;
  3618. #endif /* H263_SUPPORT */
  3619. #ifdef MPEG_SUPPORT
  3620. case SV_MPEG_DECODE:
  3621. case SV_MPEG2_DECODE:
  3622. case SV_MPEG_ENCODE:
  3623. case SV_MPEG2_ENCODE:
  3624. return(sv_MpegGetParamInt(Svh, param));
  3625. #endif /* MPEG_SUPPORT */
  3626. }
  3627. switch (param)
  3628. {
  3629. case SV_PARAM_FINALFORMAT:
  3630. return(Info->OutputFormat.biCompression);
  3631. }
  3632. return(0);
  3633. }
  3634. /*
  3635. ** Name: SvGetParamBoolean()
  3636. ** Desc: Generic call used to get the setting of specific FLOAT
  3637. ** parameters of the CODEC.
  3638. */
  3639. float SvGetParamFloat(SvHandle_t Svh, SvParameter_t param)
  3640. {
  3641. SvCodecInfo_t *Info = (SvCodecInfo_t *)Svh;
  3642. if (!Info)
  3643. return(0.0f);
  3644. switch (Info->mode)
  3645. {
  3646. #ifdef MPEG_SUPPORT
  3647. case SV_MPEG_DECODE:
  3648. case SV_MPEG2_DECODE:
  3649. case SV_MPEG_ENCODE:
  3650. case SV_MPEG2_ENCODE:
  3651. return(sv_MpegGetParamFloat(Svh, param));
  3652. #endif
  3653. #ifdef H261_SUPPORT
  3654. case SV_H261_DECODE:
  3655. case SV_H261_ENCODE:
  3656. return(svH261GetParamFloat(Svh, param));
  3657. #endif /* H261_SUPPORT */
  3658. #ifdef H263_SUPPORT
  3659. case SV_H263_DECODE:
  3660. case SV_H263_ENCODE:
  3661. return(svH263GetParamFloat(Svh, param));
  3662. #endif /* H263_SUPPORT */
  3663. }
  3664. return(0.0f);
  3665. }
  3666. /*
  3667. ** Name: sv_copy_bmh
  3668. ** Purpose: Copy a BITMAPINFOHEADER struct. For now, it only knows about the
  3669. ** extra DWORD masks at the end of BI_BITFIELDS bitmapinfoheaders.
  3670. ** Otherwise, it treats others (such as 8 bit rgb, or jpeg) the
  3671. ** same as a vanilla bitmapinfoheader.
  3672. */
  3673. static void sv_copy_bmh (
  3674. BITMAPINFOHEADER *ImgFrom,
  3675. BITMAPINFOHEADER *ImgTo)
  3676. {
  3677. *ImgTo = *ImgFrom;
  3678. if (ImgFrom->biCompression == BI_BITFIELDS)
  3679. bcopy(ImgFrom + 1, ImgTo + 1, 3*sizeof(DWORD));
  3680. }