microsoft / DirectXTK

The DirectX Tool Kit (aka DirectXTK) is a collection of helper classes for writing DirectX 11.x code in C++

Home Page:https://walbourn.github.io/directxtk/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

[Question] XMA2WAVEFORMATEX packing

flibitijibibo opened this issue · comments

Hey there!

I'm working on something that uses the XMA2 format, and when looking up the data types I noticed that the struct for XMA2WAVEFORMATEX is tightly packed in this project:

#pragma pack(push,1)
struct XMA2WAVEFORMATEX
{
WAVEFORMATEX wfx;
// Meaning of the WAVEFORMATEX fields here:
// wFormatTag; // Audio format type; always WAVE_FORMAT_XMA2
// nChannels; // Channel count of the decoded audio
// nSamplesPerSec; // Sample rate of the decoded audio
// nAvgBytesPerSec; // Used internally by the XMA encoder
// nBlockAlign; // Decoded sample size; channels * wBitsPerSample / 8
// wBitsPerSample; // Bits per decoded mono sample; always 16 for XMA
// cbSize; // Size in bytes of the rest of this structure (34)
WORD NumStreams; // Number of audio streams (1 or 2 channels each)
DWORD ChannelMask; // Spatial positions of the channels in this file,
// stored as SPEAKER_xxx values (see audiodefs.h)
DWORD SamplesEncoded; // Total number of PCM samples per channel the file decodes to
DWORD BytesPerBlock; // XMA block size (but the last one may be shorter)
DWORD PlayBegin; // First valid sample in the decoded audio
DWORD PlayLength; // Length of the valid part of the decoded audio
DWORD LoopBegin; // Beginning of the loop region in decoded sample terms
DWORD LoopLength; // Length of the loop region in decoded sample terms
BYTE LoopCount; // Number of loop repetitions; 255 = infinite
BYTE EncoderVersion; // Version of XMA encoder that generated the file
WORD BlockCount; // XMA blocks in file (and entries in its seek table)
};
#pragma pack(pop)

This makes sense to me at least, considering every WAVEFORMATEX has always been packed (as far as I know, anyway) but when looking at the original xma2defs.h type, there's no sign of packing anywhere:

https://github.com/hrydgard/minidx9/blob/master/Include/xma2defs.h

Are we missing a pragma somewhere? Or has the packing changed since the last DXSDK released?

I may have made a mistake in xwbtool. Let me check

So the key thing is that the structure is 52 bytes. pack(1), pack(4), pack(8), pack(16) all produce the same layout for x86 and x64, so really the pack statements in xwbtool are misleading.

Updated code in this commit

Excellent, thanks for the help!