| 1 |
#ifndef AVIFILE_ASFFMT_H |
| 2 |
#define AVIFILE_ASFFMT_H |
| 3 |
|
| 4 |
#include "avm_stl.h" |
| 5 |
#include "formats.h" // GUID, WAVEFORMATEX |
| 6 |
#include "utils.h" |
| 7 |
#include "asf_guids.h" |
| 8 |
|
| 9 |
AVM_BEGIN_NAMESPACE; |
| 10 |
|
| 11 |
// Data is stored in ASF packets in a relatively complicated manner |
| 12 |
// ( see details in http://divx.euro.ru/asf-1.0.htm ). |
| 13 |
// The following objects represent that data in convenient form. |
| 14 |
|
| 15 |
struct AsfPacketFragment |
| 16 |
{ |
| 17 |
uint32_t object_start_time; |
| 18 |
uint32_t fragment_offset; ///< offset of fragment within object |
| 19 |
///< ( needed for reconstruction of fragmented packets ) |
| 20 |
uint32_t object_length; ///< length of object that this fragment beints to |
| 21 |
uint32_t data_length; ///< length of this fragment |
| 22 |
uint32_t seq_num; ///< sequential id for fragmented packet |
| 23 |
const uint8_t* pointer; ///< pointer to data inside packet |
| 24 |
///< first byte of fragment N in packet p |
| 25 |
///< is p[p.fragments[N].pointer] |
| 26 |
uint8_t keyframe :1; ///< is this a keyframe |
| 27 |
uint8_t stream_id :7; ///< stream id - valid 1 - 127 |
| 28 |
}; |
| 29 |
|
| 30 |
|
| 31 |
struct AsfPacket: avm::vector<uint8_t> |
| 32 |
{ |
| 33 |
size_t length; ///< of header itself |
| 34 |
uint8_t length_flags; ///< see spec masks: |
| 35 |
///< 0x01 Multiple payloads present |
| 36 |
///< 0x06 Sequence_type |
| 37 |
///< 0x18 Padding length |
| 38 |
///< 0x60 Packet length |
| 39 |
///< 0x80 Error Correction Present |
| 40 |
uint8_t property_flags; ///< masks: |
| 41 |
///< 0x03 Replicated data length type |
| 42 |
///< 0x0c Offset into media object type |
| 43 |
///< 0x30 Media object number length type |
| 44 |
///< 0xc0 Stream number length type |
| 45 |
|
| 46 |
uint8_t segsizetype; |
| 47 |
uint8_t segments; ///< number of segments |
| 48 |
uint32_t send_time; ///< timestamp in milliseconds |
| 49 |
uint16_t packet_length; ///< of data following header |
| 50 |
uint16_t duration; ///< in milliseconds |
| 51 |
|
| 52 |
avm::vector<AsfPacketFragment> fragments; |
| 53 |
|
| 54 |
AsfPacket(size_t psize = 0); |
| 55 |
int init(uint_t timeshift = 0); // packet could be time shifted |
| 56 |
void addRef() { refcount++; } |
| 57 |
void release() { if (!--refcount) delete this; } |
| 58 |
|
| 59 |
protected: |
| 60 |
int segment(uint8_t* memory, size_t offset, uint_t ts); |
| 61 |
int refcount; |
| 62 |
}; |
| 63 |
|
| 64 |
struct AVMPACKED ASFMainHeader |
| 65 |
{ |
| 66 |
GUID guid; ///< generated by client computer |
| 67 |
uint64_t file_size; ///< in bytes |
| 68 |
///< invalid if broadcasting |
| 69 |
uint64_t create_time; ///< time of creation, in 100-nanosecond units since 1.1.1601 |
| 70 |
///< invalid if broadcasting |
| 71 |
uint64_t pkts_count; ///< how many packets are there in the file |
| 72 |
///< invalid if broadcasting |
| 73 |
uint64_t play_time; ///< play time, in 100-nanosecond units |
| 74 |
///< invalid if broadcasting |
| 75 |
uint64_t send_time; ///< time to send file, in 100-nanosecond units |
| 76 |
///< invalid if broadcasting (could be ignored) |
| 77 |
uint32_t preroll; ///< timestamp of the first packet, in milliseconds |
| 78 |
///< if nonzero - substract from time |
| 79 |
uint32_t ignore; ///< preroll is 64bit - but let's just ignore it |
| 80 |
int32_t flags; ///< 0x01 - broadcast |
| 81 |
///< 0x02 - seekable |
| 82 |
///< rest is reserved should be 0 |
| 83 |
uint32_t min_pktsize; ///< size of a data packet |
| 84 |
///< invalid if broadcasting |
| 85 |
uint32_t max_pktsize; ///< shall be the same as for min_pktsize |
| 86 |
///< invalid if broadcasting |
| 87 |
uint32_t max_bitrate; ///< bandwith of stream in bps |
| 88 |
///< should be the sum of bitrates of the |
| 89 |
///< individual media streams |
| 90 |
}; |
| 91 |
|
| 92 |
static inline ASFMainHeader* avm_get_leASFMainHeader(void* asfmainheader) |
| 93 |
{ |
| 94 |
ASFMainHeader* h = (ASFMainHeader*)asfmainheader; |
| 95 |
avm_get_leGUID(&h->guid); |
| 96 |
h->file_size = avm_get_le64(&h->file_size); |
| 97 |
h->create_time = avm_get_le64(&h->create_time); |
| 98 |
h->pkts_count = avm_get_le64(&h->pkts_count); |
| 99 |
h->play_time = avm_get_le64(&h->play_time); |
| 100 |
h->send_time = avm_get_le64(&h->send_time); |
| 101 |
h->preroll = avm_get_le32(&h->preroll); |
| 102 |
h->flags = avm_get_le32(&h->flags); |
| 103 |
h->min_pktsize = avm_get_le32(&h->min_pktsize); |
| 104 |
h->max_pktsize = avm_get_le32(&h->max_pktsize); |
| 105 |
h->max_bitrate = avm_get_le32(&h->max_bitrate); |
| 106 |
|
| 107 |
return h; |
| 108 |
} |
| 109 |
|
| 110 |
void avm_print_ASFMainHeader(const ASFMainHeader*); |
| 111 |
|
| 112 |
union ASFStreamHeader |
| 113 |
{ |
| 114 |
char buf[1024]; |
| 115 |
struct AVMPACKED |
| 116 |
{ |
| 117 |
GUID stream_guid; ///< type of media stream |
| 118 |
GUID error_guid; ///< data error correction used |
| 119 |
uint64_t time_offset; ///< presentation time (in 100-nanosecond unit) |
| 120 |
uint32_t stream_size; ///< size of type-specific data |
| 121 |
uint32_t error_size; ///< size of error correct data |
| 122 |
uint16_t stream; ///< number ( 1, 2 ... ) |
| 123 |
uint32_t reserved; ///< usually the same in both streams: |
| 124 |
///< Eyes on me.asf: 0x62dffd4 |
| 125 |
///< Alsou - Solo.asf: 0x10 |
| 126 |
///< Baby one more time.asf: 0x10 |
| 127 |
///< Cure_LastDayOfSummer.wma: 0x818f900c |
| 128 |
///< Windows Movie Maker Sample File.wmv: 0x3f |
| 129 |
///< KellssyV_500VBR.wmv: 0x0 |
| 130 |
union |
| 131 |
{ |
| 132 |
struct AVMPACKED |
| 133 |
{ |
| 134 |
int32_t width; ///< width of encoded image |
| 135 |
int32_t height; ///< height of encoded image |
| 136 |
int8_t flags; ///< shall be set to 2 |
| 137 |
uint16_t data_size; |
| 138 |
BITMAPINFOHEADER bih; |
| 139 |
} vid; |
| 140 |
struct AVMPACKED |
| 141 |
{ |
| 142 |
WAVEFORMATEX wfx; |
| 143 |
// audio scramble data follows |
| 144 |
} aud; |
| 145 |
}; |
| 146 |
} hdr; |
| 147 |
}; |
| 148 |
|
| 149 |
static inline ASFStreamHeader* avm_get_leASFStreamHeader(void* asfstreamheader) |
| 150 |
{ |
| 151 |
ASFStreamHeader* h = (ASFStreamHeader*)asfstreamheader; |
| 152 |
|
| 153 |
avm_get_leGUID(&h->hdr.stream_guid); |
| 154 |
avm_get_leGUID(&h->hdr.error_guid); |
| 155 |
h->hdr.time_offset = avm_get_le64(&h->hdr.time_offset); |
| 156 |
h->hdr.stream_size = avm_get_le32(&h->hdr.stream_size); |
| 157 |
h->hdr.error_size = avm_get_le32(&h->hdr.error_size); |
| 158 |
h->hdr.stream = avm_get_le16(&h->hdr.stream); |
| 159 |
h->hdr.reserved = avm_get_le32(&h->hdr.reserved); |
| 160 |
|
| 161 |
if (!guid_is_guidid(&h->hdr.stream_guid, GUID_ASF_AUDIO_MEDIA)) |
| 162 |
{ |
| 163 |
h->hdr.vid.width = avm_get_le32(&h->hdr.vid.width); |
| 164 |
h->hdr.vid.height = avm_get_le32(&h->hdr.vid.height); |
| 165 |
h->hdr.vid.data_size = avm_get_le16(&h->hdr.vid.data_size); |
| 166 |
avm_get_leBITMAPINFOHEADER(&h->hdr.vid.bih); |
| 167 |
} |
| 168 |
else |
| 169 |
avm_get_leWAVEFORMATEX(&h->hdr.aud); |
| 170 |
|
| 171 |
return h; |
| 172 |
} |
| 173 |
|
| 174 |
static inline char* avm_convert_asfstring(void* str, size_t size) |
| 175 |
{ |
| 176 |
if (str) |
| 177 |
{ |
| 178 |
char* s = (char*) str; |
| 179 |
uint_t i,j; |
| 180 |
for (i = 0, j = 0; i < size && s[i] != '\0'; i += 2, j++) |
| 181 |
s[j] = s[i]; |
| 182 |
s[j] = '\0'; |
| 183 |
} |
| 184 |
return (char*)str; |
| 185 |
} |
| 186 |
|
| 187 |
struct AVMPACKED ASFSpreadAudio |
| 188 |
{ |
| 189 |
uint8_t span; ///< total number of audio blocks in each scramble group |
| 190 |
uint16_t packet_length; ///< byte size of each scrambling chunk |
| 191 |
uint16_t chunk_length; ///< usually = nBlockAlign Chunks Per Data Unit |
| 192 |
uint16_t data_length; ///< usually = nBlockAlign Chunk Distance |
| 193 |
uint8_t silence_data; |
| 194 |
}; |
| 195 |
|
| 196 |
static inline void avm_get_leASFSpreadAudio(ASFSpreadAudio* h) |
| 197 |
{ |
| 198 |
h->packet_length = avm_get_le16(&h->packet_length); |
| 199 |
h->chunk_length = avm_get_le16(&h->chunk_length); |
| 200 |
h->data_length = avm_get_le16(&h->data_length); |
| 201 |
} |
| 202 |
|
| 203 |
AVM_END_NAMESPACE; |
| 204 |
|
| 205 |
#endif // AVIFILE_ASFFMT_H |