Advertisement
cr88192

BGBTech Image Codec 1 (Preliminary Spec 2)

Mar 2nd, 2013
212
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 12.91 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 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, 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. === Layer IDs ===
  29.  
  30. The layerid will be the primary key used for identifying image layers. The layers and layer IDs will be established in the base-frame, and as a result all layers are required to be present (but need not necessarily have any image data).
  31.  
  32. In subsequent frames, layers may be omitted, but the layer IDs for each layer are to reflect those established in the base image.
  33.  
  34. TagLayer IDs will indicate the position of the layer within the image starting at 1 (Though layer 1 will be special and exist for defining the canvas and for single-layer images). All tag-layer IDs are to be unique.
  35.  
  36. Component Layer IDs will be assigned sequentially (starting at 1). All component layer IDs are to be unique.
  37.  
  38.  
  39. === Markers ===
  40.  
  41. <0xFF:BYTE> <marker:BYTE> <size:WORD> <data:BYTE[size-2]>
  42.  
  43. <0xFF:BYTE> <marker:BYTE> <0x0000:WORD>
  44. <size:DWORD> <data:BYTE[size-6]>
  45.  
  46. <0xFF:BYTE> <marker:BYTE> <0x0001:WORD>
  47. <size:QWORD> <data:BYTE[size-10]>
  48.  
  49. Marker:
  50. Escape: 0x00-0x0F Levels 0-15
  51. EscapeChain: 0x10 Levels 16+
  52. Reserved: 0x11-0xBF
  53. ReservedJPEG: 0xC0-0xDF JPEG Markers, Not Used
  54. APP0-APP15: 0xE0-0xEF Application Markers
  55. FMT0-FMT13: 0xF0-0xFD Markers, Must Understand
  56. COM: 0xFE Comment Marker, Ignored
  57.  
  58. Data within a marker will be 0xFF escaped.
  59. This will mean any literal 0xFF bytes will be replaced with an escape marker (initially 0x00, but for nested-structures, escape-levels are used).
  60.  
  61. Data within an unknown APPn marker should be silently ignored by a decoder.
  62. Data within an unknown FMTn marker should result in the image being rejected.
  63.  
  64.  
  65. 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.
  66.  
  67.  
  68. === APP11 / FMT11 ===
  69.  
  70. <tag: ASCIIZ> <args:ASCIIZ[]>
  71.  
  72. Note that APP11 markers are presently limited to ASCII data.
  73.  
  74.  
  75. === APP12 / FMT12 ===
  76.  
  77. <tag: FOURCC> <data:BYTE[]>
  78.  
  79.  
  80. === APP13 / FMT13 ===
  81.  
  82. <tag: ASCIIZ> <data:BYTE[]>
  83.  
  84.  
  85. == BTIC1 Wrapper ==
  86.  
  87. FMT13: "BTIC1"
  88. FMT13: "BTIC1Z"
  89.  
  90. Will contain all data for the image.
  91.  
  92. 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.
  93.  
  94. The contents of this marker's data will be a collection of tag-layers and their corresponding component-layers and images.
  95.  
  96. Alternatively, a delta-frame may instead contain a collection of 'CLID' markers followed by their associated image data.
  97.  
  98. Note that layer definitions and 'CLID' markers may not appear within the same frame.
  99.  
  100.  
  101. == Layers ==
  102.  
  103. Two types of layers will exist, those being component layers and tag layers.
  104. Component layers will identify individual images, representing the various components in the texture.
  105.  
  106. Tag layers will be more like the traditional layers within a graphics program. These tag-layers may be used independently of each other.
  107.  
  108. The "TagLayer" and "CompLayer" markers will be the primary means of defining these layers, however frames may may use 'CLID' instead, which references layers defined in prior frames.
  109.  
  110.  
  111. === TagLayer ===
  112.  
  113. FMT13: "TagLayer"
  114. <name:BYTE[32]> Tag Layer Name
  115. <layerID:DWORD> LayerID of Layer
  116. <xorg:DWORD> X Origin of Layer
  117. <yorg:DWORD> Y Origin of Layer
  118. <xsize:DWORD> X Size of Layer
  119. <ysize:DWORD> Y Size of Layer
  120. <xcenter:DWORD> X Center of Layer
  121. <ycenter:DWORD> Y Center of Layer
  122. <flags:DWORD> Layer Flags
  123.  
  124. FMT12: "LEND"
  125. Marks the end of a given tag-layer.
  126.  
  127. The tag-layer is followed by zero or more component layers.
  128.  
  129.  
  130. 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 for the stored image (potentially padded up to a power-of-2). Usually, these should both be the same values, and also a power-of-2 size (This will be required for video-maps).
  131.  
  132. These sizes will be in pixels.
  133.  
  134. The origin will indicate the position of the layer image (relative to its center) within the canvas.
  135.  
  136. The center will indicate the center of a layer image (in pixels) relative to its lower-left corner.
  137.  
  138. 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.
  139.  
  140.  
  141. Note that tag-layer 1 is special:
  142. It will have an empty name;
  143. Its properties will define those of the canvas;
  144. If present, any component layers will be part of a special background layer, but may be absent for layered images.
  145.  
  146. In single layer images, this layer will be the sole layer.
  147.  
  148.  
  149. === CompLayer ===
  150.  
  151. FMT13: "CompLayer"
  152. <name:BYTE[8]> Component Layer Name
  153. <layerID:DWORD> LayerID of Image
  154. <flags:DWORD> Layer Flags
  155.  
  156. Denotes the start of a given Component Layer.
  157. This marker is directly followed by the relevant image data (terminated by a FMT12:"TEND" marker).
  158.  
  159. Layer Names:
  160. "RGBA": RGBA Base Layer
  161. "XYZD": XYZ Normals+Depth
  162. "SpRGBS": Specular RGB+Scale
  163. "LuRGBS": Luma RGB+Scale
  164.  
  165. Note that the specular exponent scale will represent a normalized value to be multiplied with the value given in the material definition.
  166.  
  167. Likewise for the Luma scale, which will indicate the brightness of the emitted light.
  168.  
  169.  
  170. == Texture Images ==
  171.  
  172. === BTIC1 Image ===
  173.  
  174. Stores a single or mipmap image.
  175.  
  176. Note that for mipmap images, the layers will be packed end-to-end.
  177.  
  178. FMT12: "CLID" (Component Layer ID)
  179. <layerID:DWORD> LayerID of Image
  180. <flags:DWORD> Image Flags
  181.  
  182. FMT12: "THDR" (Image Header)
  183. <width:DWORD> Image Width
  184. <height:DWORD> Image Height
  185. <imgtype:WORD> Image Type
  186. <mip_start:BYTE> MipMap Level Start
  187. <mip_end:BYTE> MipMap Level End
  188. <filtmode:BYTE> Filter Modes (Depends on ImageType)
  189. <clrtype:BYTE> Colorspace Type (Depends on ImageType)
  190. <pixtype:BYTE> Pixel Type (Depends on ImageType)
  191.  
  192. FMT12: "TDAT" (Image Data)
  193. <data:BYTE[]> Image Data
  194.  
  195. FMT12: "TEND" (End Of Image)
  196. Marks the end of a component-layer image.
  197.  
  198. Image Types:
  199. 0 RGBA (Raw RGBA)
  200. 1 RGB (Raw RGB)
  201. 2 -
  202. 3 BGRA
  203. 4 BGR
  204. 5 YUVA (Raw YUVA)
  205. 6 YUV (Raw YUV)
  206. 7 Y (Raw Luma)
  207. 8 YA (Raw Luma+Alpha)
  208. ...
  209. 16 BC1 / DXT1 (Opaque)
  210. 17 BC2 / DXT3
  211. 18 BC3 / DXT5
  212. 19 BC4 (Y)
  213. 20 BC5 (YA)
  214. 21 BC6
  215. 22 BC7
  216. 23 BC1F / DXT1F (Fast)
  217. 24 BC3F / DXT5F (Fast)
  218. 25 BC1A / DXT1A (DXT1 + Alpha)
  219. 26 DXT5_UVAY (UVAY)
  220.  
  221. Filter Modes:
  222. 0 None (RGB / YUV / DXTn)
  223. 1 Scanline Filtering (RGB / YUV)
  224. 2 Simple Block Filtering (RGB / YUV)
  225. 3 Block Pack (DXTn)
  226.  
  227. Clrtype:
  228. 0 RGB(A) (RGB / DXTn)
  229. 1 YCbCr (YUV / UVAY)
  230. 2 RCT (YUV / UVAY)
  231. 3 MJXR1 (YUV / UVAY)
  232.  
  233. RCT:
  234. Y=(R+2G+B)/4
  235. Y=G+(B+R-2*G)/4
  236. U=B-G
  237. V=R-G
  238.  
  239. G=Y-(U+V)/4
  240. B=G+U
  241. R=G+V
  242.  
  243. MJXR1:
  244. Y=G-(2*G-B-R)/4
  245. U=B-R
  246. V=(2*G-B-R)/2
  247.  
  248. G=Y+V/2
  249. R=G-V-U/2
  250. B=G-V+U/2
  251.  
  252.  
  253. === Scanline and Block Filtering ===
  254.  
  255. 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.
  256.  
  257. Pixel Type:
  258. 0 Default (Default / Undefined)
  259. 1 Byte (Raw Byte, RGBA/YUVA)
  260. 2 Short (Raw Signed 16-bit, RGBA/YUVA)
  261. 3 UShort (Raw Unsigned 16-bit, RGBA/YUVA)
  262. 4 ByteVL (Byte, VLI-Packed)
  263. 5 ShortVL (16-Bit Signed Short, VLI-Packed)
  264. 6 UShortVL (16-Bit Unsigned Short, VLI-Packed)
  265. 7 Float16VL (16-Bit Float, VLI-Packed)
  266.  
  267. This is specific to RGB(A) and YUV(A) modes, N/A for DXTn.
  268.  
  269. Note that float16 data will be treated as if it were unsigned-short data.
  270.  
  271.  
  272. Pixels:
  273. C A
  274. B x
  275.  
  276. Filters (Scanline or Block):
  277. 0 None (P=0)
  278. 1 Left (P=B)
  279. 2 Up (P=A)
  280. 3 Average (P=(A+B)/2)
  281. 4 Paeth (...)
  282. 5 Linear (P=A+B-C)
  283.  
  284. Block Only (Possible):
  285. 16 Hadamard
  286. 17 DCT
  287. 18 RDCT
  288.  
  289. DC Coefficients will use Paeth prediction for block filtering.
  290.  
  291. Paeth:
  292. P0=A+B-C
  293. Pick P value closest to P0.
  294.  
  295.  
  296. === DXTn ===
  297.  
  298. Filter for DXTn / BCn texture compression.
  299. DXTn packs bits starting from the LSB.
  300.  
  301. DXTn stores a 4x4 block of pixels using colors interpolated from 2 pixel values.
  302.  
  303. Pixel Block:
  304. A B C D
  305. E F G H
  306. I J K L
  307. M N O P
  308.  
  309. Color: 5:6:5 (LE WORD)
  310. Red: Bits 11-15
  311. Green: Bits 5-10
  312. Blue: Bits 0-4
  313.  
  314. DXT1 / DXT5 RGB
  315.  
  316. Color0: Color
  317. Color1: Color
  318. pixels: BYTE[4] (2 bpp)
  319. DCBA
  320. HGFE
  321. LKJI
  322. PONM
  323.  
  324. Pixel Bits:
  325. 0=Color0
  326. 1=Color1
  327. 2= 0.66*Color0 + 0.33*Color1 (DXT5)
  328. 0.5*Color0 + 0.5*Color1(DXT1)
  329. 3= 0.33*Color0 + 0.66*Color1 (DXT5)
  330. Transparent / Black (DXT1)
  331.  
  332. DXT5
  333.  
  334. Alpha0: BYTE
  335. Alpha1: BYTE
  336. alphas: BYTE[6] (3bpp)
  337. Color0: Color
  338. Color1: Color
  339. pixels: BYTE[4]
  340.  
  341.  
  342. DXT5 Alpha / BC4:
  343. Alpha0: BYTE
  344. Alpha1: BYTE
  345. alphas: BYTE[6] (3bpp for each pixel)
  346.  
  347. if(Alpha0<=Alpha1)
  348. {
  349. 0=Alpha0, 1=Alpha1;
  350. 2-5=interpolated alphas;
  351. 6=0, 7=255.
  352. }else
  353. {
  354. 0=Alpha0, 1=Alpha1;
  355. 2-7=interpolated alphas;
  356. }
  357.  
  358.  
  359. === DXT5 UVAY ===
  360.  
  361. DXT5 UVAY: YUVA Embedded in DXT5 Textures.
  362.  
  363. Colorspace:
  364. Y, U, V, A
  365.  
  366. "A" may encode either Alpha or a UV Scale factor.
  367.  
  368. Values between 0 and 127 encode Alpha (With Scale=1.0), and 128-255 encode Scale (With Alpha=1.0).
  369.  
  370. Note that A=128 means Scale=2.0, and A=255 means Scale=1.0/63.
  371.  
  372. 0-127: Alpha=2.0*B, Scale=2.0 or 1.0.
  373. 128-255: Alpha=1.0, Scale=2.0-(4.0*B-2.0) or 1.0-(2.0*B-1.0).
  374.  
  375. The use of a Scale allows more accurate reproduction of colors.
  376.  
  377. Assertion: For alpha-blending, reduced color precision is acceptable.
  378.  
  379. Scale Values:
  380. 128 -> 2.0 136 -> 1.875 144 -> 1.75 152 -> 1.625
  381. 160 -> 1.5 168 -> 1.375 176 -> 1.25 184 -> 1.125
  382. 192 -> 1.0 200 -> 0.875 208 -> 0.75 216 -> 0.625
  383. 224 -> 0.5 232 -> 0.376 240 -> 0.25 248 -> 0.125
  384.  
  385. 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.
  386.  
  387. Note that when stored, UV components will be both inverse-scaled and have a DC bias added.
  388.  
  389. IOW: 256, 0 with Scale=2.0 would be stored as 255,128 and 128,-128 as 192,64.
  390.  
  391. Whether scale is 1.0 or 2.0 will depend on the colorspace, with the 2.0 definition being used for colorspaces which would otherwise require 9 bits to store the UV values.
  392.  
  393.  
  394. === VLI Pack ===
  395.  
  396. Images will be packed in terms of variable-length quantaties.
  397.  
  398. Each block tag will be encoded in the form (byte):
  399. 0-127 Literal Value (0..127, -64..63).
  400. 128-191 X Literal Value (128..16383, -8192..8191).
  401. 192-223 XX Literal Value (16384..2097152, -1048576..1048575).
  402. 224-238 I LZ/RLE Run (2-16 items, Index)
  403. 239 LI LZ/RLE Run (Length, Index)
  404. 240 XXX 24-Bit Value
  405. 241 XXXX 32-Bit Value
  406. 242-246 Literal Blocks (2-6 Values)
  407. 247 L Literal Blocks (L Values)
  408. 248-255 Reserved
  409.  
  410. Values may be interpreted as signed or unsigned.
  411. Normal values will be signed, whereas indices and lengths will be unsigned.
  412.  
  413. Sign will be folded into the LSB following the pattern:
  414. 0, -1, 1, -2, 2, ...
  415.  
  416.  
  417. === Block Pack ===
  418.  
  419. DXTn packed images.
  420.  
  421. 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.
  422.  
  423.  
  424. Each block tag will be encoded in the form (byte):
  425. 0 <block:QWORD> Literal Block.
  426. 1-127 Single byte block index.
  427. 128-191 X Two byte block index (16384 blocks).
  428. 192-223 XX Three byte block index (2097152 blocks).
  429. 224-238 I LZ/RLE Run (2-16 blocks, Index)
  430. 239 LI LZ/RLE Run (Length, Index)
  431. 240 XXX 24-Bit Index
  432. 241 XXXX 32-Bit Index
  433. 242-246 Literal Blocks (2-6 Blocks)
  434. 247 L Literal Blocks (L Blocks)
  435. 248-255 Reserved
  436.  
  437. The block index will indicate how many blocks backwards to look for a matching block (1 will repeat the prior block).
  438.  
  439. Length/Index values will use the same organization as above, only limited to encoding numeric values.
  440.  
  441. 0-127 0-127.
  442. 128-191 X 128-16383.
  443. 192-223 XX 16384-2097151.
  444. 240 XXX 24-Bit Index (0-16777215)
  445. 241 XXXX 32-Bit Index (0-4294967295)
  446.  
  447. 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