Advertisement
cr88192

BGBTech Image Codec 1 (Preliminary Spec)

Feb 25th, 2013
236
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 10.12 KB | None | 0 0
  1. BGBTech Image Codec 1
  2.  
  3. Goal: Fast decoding for video maps.
  4. Should be able to directly encode/decode DXTn images.
  5.  
  6. Basic format derived from BTJ-NBCES.
  7.  
  8. (Most) Multibyte values will be big-endian.
  9.  
  10. Note that a valid decoder need not be able to accept all possible combinations of features, only the specific combination of features it expects (This format is more intended for task specific usage than as an interchange format).
  11.  
  12. Will reuse concepts and markers for tag-layers and component layers.
  13.  
  14.  
  15. == General ==
  16.  
  17. Images may have multiple layers.
  18.  
  19. The origin point for images will be in the lower-left corner of the image canvas (0,0), with +X as right and +Y as up.
  20.  
  21. All textures within a given tag-layer will currently be required to use the same colorspace.
  22.  
  23. For video, this may also mean that all subsequent frames may need to have the same image format as laid out in the initial frame.
  24.  
  25. For DXTn Block Pack and video, the prior frame will provide the initial contents of the sliding window for decoding the next frame.
  26.  
  27.  
  28. === Markers ===
  29.  
  30. <0xFF:BYTE> <marker:BYTE> <size:WORD> <data:BYTE[size-2]>
  31.  
  32. <0xFF:BYTE> <marker:BYTE> <0x0000:WORD>
  33. <size:DWORD> <data:BYTE[size-6]>
  34.  
  35. <0xFF:BYTE> <marker:BYTE> <0x0001:WORD>
  36. <size:QWORD> <data:BYTE[size-10]>
  37.  
  38. Marker:
  39. Escape: 0x00-0x0F Levels 0-15
  40. EscapeChain: 0x10 Levels 16+
  41. Reserved: 0x11-0xBF
  42. ReservedJPEG: 0xC0-0xDF JPEG Markers, Not Used
  43. APP0-APP15: 0xE0-0xEF Application Markers
  44. FMT0-FMT13: 0xF0-0xFD Markers, Must Understand
  45. COM: 0xFE Comment Marker, Ignored
  46.  
  47. Data within a marker will be 0xFF escaped.
  48. This will mean any literal 0xFF bytes will be replaced with an escape marker (initially 0x00, but for nested-structures, escape-levels are used).
  49.  
  50. Data within an unknown APPn marker should be silently ignored.
  51. Data within an unknown FMTn marker should result in the image being rejected.
  52.  
  53.  
  54. APP11/APP12/FMT12/FMT13 markers may be optionally split into multiple parts via a continuation markers, which will be a marker of matching type but with 0 for the FOURCC or tag.
  55.  
  56.  
  57. === APP11 / FMT11 ===
  58.  
  59. <tag: ASCIIZ> <args:ASCIIZ[]>
  60.  
  61. Note that APP11 markers are presently limited to ASCII data.
  62.  
  63.  
  64. === APP12 / FMT12 ===
  65.  
  66. <tag: FOURCC> <data:BYTE[]>
  67.  
  68.  
  69. === FMT13 ===
  70.  
  71. <tag: ASCIIZ> <data:BYTE[]>
  72.  
  73.  
  74. == BTIC1 Wrapper ==
  75.  
  76. FMT13: "BTIC1"
  77. FMT13: "BTIC1Z"
  78.  
  79. Will contain all data for the image.
  80.  
  81. The second (Z suffix) form, will Deflate encode the image data, and will use a Zlib header, with method=8 for Deflate, and 9 for Deflate64.
  82.  
  83.  
  84. === Layers ===
  85.  
  86. APP11: "CompLayer", LayerName
  87. Denotes the start of a given Component Layer.
  88. This marker is directly followed by the relevant image data.
  89.  
  90. Layer Names:
  91. "RGB": RGB Base Layer
  92. "RGBA": RGBA Base Layer
  93. "Alpha": Optional disjoint alpha image (RGB)
  94. "XYZ": XYZ Normals
  95. "XYZD": XYZ Normals+Depth
  96. "Depth": Depth image (Bump Map)
  97. "SpRGB": Specular RGB
  98. "SpRGBE": Specular RGB+Exponent
  99. "SpExp": Specular Exponent
  100. "LuRGB": Luma RGB
  101. "LuRGBE": Luma RGB+Exponent
  102. "LuExp": Luma Exponent
  103.  
  104. APP11: "TagLayer", LayerName
  105. Gives a named tag layer.
  106. This may be followed by 1 or component-layers.
  107.  
  108.  
  109. === BTIC1 Image ===
  110.  
  111. Stores a single or mipmap image.
  112.  
  113. Note that for mipmap images, the layers will be packed end-to-end.
  114.  
  115. FMT12: "LHDR" (Layer Header)
  116. <layerID:DWORD> LayerID of Image
  117. <xorg:DWORD> X Origin of Image
  118. <yoorg:DWORD> Y Origin of Image
  119. <xsize:DWORD> X Size of Image
  120. <ysize:DWORD> Y Size of Image
  121. <xcenter:DWORD> X Center of Image
  122. <ycenter:DWORD> Y Center of Image
  123. <flags:DWORD> Layer Flags
  124.  
  125. Note that while it may seem redundant to give the image size twice, the sizes will represent different sizes. The layer header size will represent the images' size relative to the canvas, whereas the image header will encode the physically-encoded size (potentially padded up to a power-of-2).
  126.  
  127. The origin will indicate the position of the layer image (relative to its center) within the canvas.
  128.  
  129. The center will indicate the center of a layer image (in pixels) relative to its lower-left corner.
  130.  
  131. LayerID gives a layer ID for each image. This is required to be unique for all layer-images within a compound image, and is required to match that of the same layer (same tag-layer and component) within the base-frame.
  132.  
  133.  
  134. FMT12: "THDR" (Image Header)
  135. <width:DWORD> Image Width
  136. <height:DWORD> Image Height
  137. <imgtype:WORD> Image Type
  138. <mip_start:BYTE> MipMap Level Start
  139. <mip_end:BYTE> MipMap Level End
  140. <filtmode:BYTE> Filter Modes (Depends on ImageType)
  141. <clrtype:BYTE> Colorspace Type (Depends on ImageType)
  142. <pixtype:BYTE> Pixel Type (Depends on ImageType)
  143.  
  144. FMT12: "TDAT" (Image Data)
  145. <data:BYTE[]> Image Data
  146.  
  147. Image Types:
  148. 0 RGBA (Raw RGBA)
  149. 1 RGB (Raw RGB)
  150. 2 -
  151. 3 BGRA
  152. 4 BGR
  153. 5 YUVA (Raw YUVA)
  154. 6 YUV (Raw YUV)
  155. 7 Y (Raw Luma)
  156. 8 YA (Raw Luma+Alpha)
  157. ...
  158. 16 BC1 / DXT1 (Opaque)
  159. 17 BC2 / DXT3
  160. 18 BC3 / DXT5
  161. 19 BC4
  162. 20 BC5
  163. 21 BC6
  164. 22 BC7
  165. 23 BC1F / DXT1F (Fast)
  166. 24 BC3F / DXT5F (Fast)
  167. 25 BC1A / DXT1A (DXT1 + Alpha)
  168. 26 DXT5_UVAY
  169.  
  170. Filter Modes:
  171. 0 None (RGB / YUV / DXTn)
  172. 1 Scanline Filtering (RGB / YUV)
  173. 2 Simple Block Filtering (RGB / YUV)
  174. 3 Block Pack (DXTn)
  175.  
  176. Clrtype:
  177. 0 RGB(A) (Normal)
  178. 1 YCbCr (YUV / UVAY)
  179. 2 RCT (YUV)
  180. 3 BLCT1 (YUV / UVAY)
  181. 4 BLCT2 (YUV / UVAY)
  182.  
  183. RCT:
  184. Y=(R+2G+B)/4
  185. Y=G+(B+R-2*G)/4;
  186. U=B-G
  187. V=R-G
  188.  
  189. G=Y-(U+V)/4
  190. B=G+U
  191. R=G+V
  192.  
  193. BLCT1:
  194. Y=(R+2G+B)/4
  195. Y=G+(B+R-2*G)/4
  196. U=(B-G)/2 + DC
  197. V=(R-G)/2 + DC
  198.  
  199. G=Y-(U+V-2DC)/2
  200. B=G+2*(U-DC)
  201. R=G+2*(V-DC)
  202.  
  203. BLCT2:
  204. Y=(R+2G+B)/4
  205. Y=G+(B+R-2*G)/4
  206. Y=(G+V)/2
  207. U=(B-R)/2 + DC
  208. V=(R+B)/2
  209.  
  210. R=V-(U-DC)
  211. G=2Y-V
  212. B=V+(U-DC)
  213.  
  214.  
  215. === Scanline and Block Filtering ===
  216.  
  217. Scanline filtering and block-filtering will store the filter bytes prior to the image data. Scanline filtering will apply the filter for a single scanline, whereas block-filtering will apply it to an 8x8 block of pixels.
  218.  
  219. Pixel Type:
  220. 0 Default (Default / Undefined)
  221. 1 Byte (Raw Byte, RGBA/YUVA)
  222. 2 Short (Raw Signed 16-bit, RGBA/YUVA)
  223. 3 UShort (Raw Unsigned 16-bit, RGBA/YUVA)
  224. 4 ByteVL (Byte, VLI-Packed)
  225. 5 ShortVL (16-Bit Signed Short, VLI-Packed)
  226. 6 UShortVL (16-Bit Unsigned Short, VLI-Packed)
  227. 7 Float16VL (16-Bit Float, VLI-Packed)
  228.  
  229. This is specific to RGB(A) and YUV(A) modes, N/A for DXTn.
  230.  
  231. Note that float16 data will be treated as if it were unsigned-short data.
  232.  
  233.  
  234. Pixels:
  235. C A
  236. B x
  237.  
  238. Filters (Scanline or Block):
  239. 0 None (P=0)
  240. 1 Left (P=B)
  241. 2 Up (P=A)
  242. 3 Average (P=(A+B)/2)
  243. 4 Paeth (...)
  244. 5 Linear (P=A+B-C)
  245.  
  246. Block Only (Possible):
  247. 16 Hadamard
  248. 17 DCT
  249. 18 RDCT
  250.  
  251. DC Coefficients will use Paeth prediction for block filtering.
  252.  
  253. Paeth:
  254. P0=A+B-C
  255. Pick P value closest to P0.
  256.  
  257.  
  258. === DXTn ===
  259.  
  260. Filter for DXTn / BCn texture compression.
  261. DXTn packs bits starting from the LSB.
  262.  
  263. DXTn stores a 4x4 block of pixels using colors interpolated from 2 pixel values.
  264.  
  265. Pixel Block:
  266. A B C D
  267. E F G H
  268. I J K L
  269. M N O P
  270.  
  271. Color: 5:6:5 (LE WORD)
  272. Red: Bits 11-15
  273. Green: Bits 5-10
  274. Blue: Bits 0-4
  275.  
  276. DXT1 / DXT5 RGB
  277.  
  278. Color0: Color
  279. Color1: Color
  280. pixels: BYTE[4] (2 bpp)
  281. DCBA
  282. HGFE
  283. LKJI
  284. PONM
  285.  
  286. Pixel Bits:
  287. 0=Color0
  288. 1=Color1
  289. 2= 0.66*Color0 + 0.33*Color1 (DXT5)
  290. 0.5*Color0 + 0.5*Color1(DXT1)
  291. 3= 0.33*Color0 + 0.66*Color1 (DXT5)
  292. Transparent / Black (DXT1)
  293.  
  294. DXT5
  295.  
  296. Alpha0: BYTE
  297. Alpha1: BYTE
  298. alphas: BYTE[6] (3bpp)
  299. Color0: Color
  300. Color1: Color
  301. pixels: BYTE[4]
  302.  
  303.  
  304. DXT5 Alpha / BC4:
  305. Alpha0: BYTE
  306. Alpha1: BYTE
  307. alphas: BYTE[6] (3bpp for each pixel)
  308.  
  309. if(Alpha0<=Alpha1)
  310. {
  311. 0=Alpha0, 1=Alpha1;
  312. 2-5=interpolated alphas;
  313. 6=0, 7=255.
  314. }else
  315. {
  316. 0=Alpha0, 1=Alpha1;
  317. 2-7=interpolated alphas;
  318. }
  319.  
  320.  
  321. === DXT5 UVAY ===
  322.  
  323. DXT5 UVAY: YUVA Embedded in DXT5 Textures.
  324.  
  325. Colorspace:
  326. Y, U, V, A
  327.  
  328. "A" may encode either Alpha or a UV Scale factor.
  329.  
  330. Values between 0 and 127 encode Alpha (With Scale=1.0), and 128-255 encode Scale (With Alpha=1.0).
  331.  
  332. Note that A=128 means Scale=1.0, and A=255 means Scale=1.0/127.
  333.  
  334. 0-127: Alpha=2.0*B, Scale=1.0.
  335. 128-255: Alpha=1.0, Scale=1.0-(2.0*B-1.0).
  336.  
  337. The use of a Scale allows more accurate reproduction of colors.
  338.  
  339. Assertion: For alpha-blending, reduced color precision is acceptable.
  340.  
  341.  
  342. Within the DXT5 image, components are encoded as in the order (U, V, A, Y), or essentially: R'=U, G'=V, B'=A, A'=Y.
  343.  
  344.  
  345. === VLI Pack ===
  346.  
  347. Images will be packed in terms of variable-length quantaties.
  348.  
  349. Each block tag will be encoded in the form (byte):
  350. 0-127 Literal Value (0..127, -64..63).
  351. 128-191 X Literal Value (128..16383, -8192..8191).
  352. 192-223 XX Literal Value (16384..2097152, -1048576..1048575).
  353. 224-238 I LZ/RLE Run (2-16 items, Index)
  354. 239 LI LZ/RLE Run (Length, Index)
  355. 240 XXX 24-Bit Value
  356. 241 XXXX 32-Bit Value
  357. 242-246 Literal Blocks (2-6 Values)
  358. 247 L Literal Blocks (L Values)
  359. 248-255 Reserved
  360.  
  361. Values may be interpreted as signed or unsigned.
  362. Normal values will be signed, whereas indices and lengths will be unsigned.
  363.  
  364. Sign will be folded into the LSB following the pattern:
  365. 0, -1, 1, -2, 2, ...
  366.  
  367.  
  368. === Block Pack ===
  369.  
  370. DXTn packed images.
  371.  
  372. Images will be packed in terms of 8-byte blocks. Formats using 16-byte blocks (such as DXT5) will store blocks instead as 2 planes.
  373.  
  374.  
  375. Each block tag will be encoded in the form (byte):
  376. 0 <block:QWORD> Literal Block.
  377. 1-127 Single byte block index.
  378. 128-191 X Two byte block index (16384 blocks).
  379. 192-223 XX Three byte block index (2097152 blocks).
  380. 224-238 I LZ/RLE Run (2-16 blocks, Index)
  381. 239 LI LZ/RLE Run (Length, Index)
  382. 240 XXX 24-Bit Index
  383. 241 XXXX 32-Bit Index
  384. 242-246 Literal Blocks (2-6 Blocks)
  385. 247 L Literal Blocks (L Blocks)
  386. 248-255 Reserved
  387.  
  388. The block index will indicate how many blocks backwards to look for a matching block (1 will repeat the prior block).
  389.  
  390. Length/Index values will use the same organization as above, only limited to encoding numeric values.
  391.  
  392. 0-127 0-127.
  393. 128-191 X 128-16383.
  394. 192-223 XX 16384-2097151.
  395. 240 XXX 24-Bit Index (0-16777215)
  396. 241 XXXX 32-Bit Index (0-4294967295)
  397.  
  398. Note that DXT5 images will be split into 2 block-planes, with the first encoding the alpha component, followed by the plane encoding the RGB components.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement