header_asf.cpp 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. #include <windows.h>
  2. #include "header_asf.h"
  3. #include <mmsystem.h>
  4. #include <stdio.h>
  5. #include <bfc/platform/types.h>
  6. #pragma pack(1)
  7. ///////////////////////
  8. // MS GUID definition
  9. ///////////////////////
  10. #ifndef GUID_DEFINED
  11. #define GUID_DEFINED
  12. // Size of GUID is 16 bytes!
  13. typedef struct {
  14. uint32_t Data1; // 4 bytes
  15. uint16_t Data2; // 2 bytes
  16. uint16_t Data3; // 2 bytes
  17. uint8_t Data4[8]; // 8 bytes
  18. } GUID_t;
  19. #endif
  20. ///////////////////////
  21. // ASF Object Header
  22. ///////////////////////
  23. typedef struct {
  24. uint8_t guid[16];
  25. uint64_t size;
  26. } ASF_obj_header_t;
  27. ////////////////
  28. // ASF Header
  29. ////////////////
  30. typedef struct {
  31. ASF_obj_header_t objh;
  32. uint32_t cno; // number of subchunks
  33. uint8_t v1; // unknown (0x01)
  34. uint8_t v2; // unknown (0x02)
  35. } ASF_header_t;
  36. /////////////////////
  37. // ASF File Header
  38. /////////////////////
  39. typedef struct {
  40. uint8_t client[16]; // Client GUID
  41. uint64_t file_size;
  42. uint64_t creat_time; //File creation time FILETIME 8
  43. uint64_t packets; //Number of packets UINT64 8
  44. uint64_t end_timestamp; //Timestamp of the end position UINT64 8
  45. uint64_t duration; //Duration of the playback UINT64 8
  46. uint32_t start_timestamp; //Timestamp of the start position UINT32 4
  47. uint32_t preroll; //Time to bufferize before playing UINT32 4
  48. uint32_t flags; //Unknown, maybe flags ( usually contains 2 ) UINT32 4
  49. uint32_t packetsize; //Size of packet, in bytes UINT32 4
  50. uint32_t packetsize2; //Size of packet ( confirm ) UINT32 4
  51. uint32_t frame_size; //Size of uncompressed video frame UINT32 4
  52. } ASF_file_header_t;
  53. ///////////////////////
  54. // ASF Stream Header
  55. ///////////////////////
  56. typedef struct {
  57. uint8_t type[16]; // Stream type (audio/video) GUID 16
  58. uint8_t concealment[16]; // Audio error concealment type GUID 16
  59. uint64_t unk1; // Unknown, maybe reserved ( usually contains 0 ) UINT64 8
  60. uint32_t type_size; //Total size of type-specific data UINT32 4
  61. uint32_t stream_size; //Size of stream-specific data UINT32 4
  62. uint16_t stream_no; //Stream number UINT16 2
  63. uint32_t unk2; //Unknown UINT32 4
  64. } ASF_stream_header_t;
  65. ///////////////////////////
  66. // ASF Content Description
  67. ///////////////////////////
  68. typedef struct {
  69. uint16_t title_size;
  70. uint16_t author_size;
  71. uint16_t copyright_size;
  72. uint16_t comment_size;
  73. uint16_t rating_size;
  74. } ASF_content_description_t;
  75. ////////////////////////
  76. // ASF Segment Header
  77. ////////////////////////
  78. typedef struct {
  79. uint8_t streamno;
  80. uint8_t seq;
  81. uint32_t x;
  82. uint8_t flag;
  83. } ASF_segmhdr_t;
  84. //////////////////////
  85. // ASF Stream Chunck
  86. //////////////////////
  87. typedef struct {
  88. uint16_t type;
  89. uint16_t size;
  90. uint32_t sequence_number;
  91. uint16_t unknown;
  92. uint16_t size_confirm;
  93. } ASF_stream_chunck_t;
  94. #pragma pack()
  95. // Definition of the differents type of ASF streaming
  96. typedef enum {
  97. ASF_Unknown_e,
  98. ASF_Live_e,
  99. ASF_Prerecorded_e,
  100. ASF_Redirector_e,
  101. ASF_PlainText_e
  102. } ASF_StreamType_e;
  103. #define ASF_LOAD_GUID_PREFIX(guid) (*(uint32_t *)(guid))
  104. #define ASF_GUID_PREFIX_audio_stream 0xF8699E40
  105. #define ASF_GUID_PREFIX_video_stream 0xBC19EFC0
  106. #define ASF_GUID_PREFIX_audio_conceal_none 0x49f1a440
  107. #define ASF_GUID_PREFIX_audio_conceal_interleave 0xbfc3cd50
  108. #define ASF_GUID_PREFIX_header 0x75B22630
  109. #define ASF_GUID_PREFIX_data_chunk 0x75b22636
  110. #define ASF_GUID_PREFIX_index_chunk 0x33000890
  111. #define ASF_GUID_PREFIX_stream_header 0xB7DC0791
  112. #define ASF_GUID_PREFIX_header_2_0 0xD6E229D1
  113. #define ASF_GUID_PREFIX_file_header 0x8CABDCA1
  114. #define ASF_GUID_PREFIX_content_desc 0x75b22633
  115. HeaderAsf::HeaderAsf()
  116. {
  117. }
  118. int HeaderAsf::getInfos(const wchar_t *filename, bool checkMetadata)
  119. {
  120. ASF_header_t asfh;
  121. DWORD bytesRead = 0;
  122. unsigned char asfhdrguid[16]={0x30,0x26,0xB2,0x75,0x8E,0x66,0xCF,0x11,0xA6,0xD9,0x00,0xAA,0x00,0x62,0xCE,0x6C};
  123. HANDLE fh=CreateFileW(filename, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, 0, NULL);
  124. if (fh==INVALID_HANDLE_VALUE)
  125. return 0;
  126. ReadFile(fh,&asfh, sizeof(ASF_header_t), &bytesRead, NULL);
  127. // check for ASF guid
  128. if(memcmp(asfhdrguid,asfh.objh.guid,16) || asfh.cno>256)
  129. {
  130. CloseHandle(fh);
  131. return 0;
  132. }
  133. for(int i=0;i<(int)asfh.cno;i++) {
  134. int pos = SetFilePointer(fh, 0, 0, FILE_CURRENT);
  135. ASF_obj_header_t objh;
  136. bytesRead = 0;
  137. ReadFile(fh, &objh, sizeof(objh), &bytesRead, NULL);
  138. switch(ASF_LOAD_GUID_PREFIX(objh.guid)) {
  139. case ASF_GUID_PREFIX_file_header:
  140. {
  141. ASF_file_header_t fileh;
  142. bytesRead = 0;
  143. ReadFile(fh, &fileh, sizeof(fileh), &bytesRead, NULL);
  144. length=(int)(fileh.duration/10000);
  145. }
  146. break;
  147. case ASF_GUID_PREFIX_stream_header:
  148. {
  149. ASF_stream_header_t streamh;
  150. bytesRead = 0;
  151. ReadFile(fh, &streamh, sizeof(streamh), &bytesRead, NULL);
  152. switch(ASF_LOAD_GUID_PREFIX(streamh.type))
  153. {
  154. case ASF_GUID_PREFIX_audio_stream:
  155. {
  156. WAVEFORMATEX wfe = {0};
  157. bytesRead = 0;
  158. ReadFile(fh, &wfe, sizeof(wfe), &bytesRead, NULL);
  159. audio_bps=wfe.wBitsPerSample;
  160. audio_srate=wfe.nSamplesPerSec;
  161. audio_nch=wfe.nChannels;
  162. has_audio=true;
  163. }
  164. break;
  165. case ASF_GUID_PREFIX_video_stream:
  166. {
  167. bytesRead = 0;
  168. ReadFile(fh, &video_w, sizeof(video_w), &bytesRead, NULL);
  169. bytesRead = 0;
  170. ReadFile(fh, &video_h, sizeof(video_h), &bytesRead, NULL);
  171. has_video=true;
  172. }
  173. break;
  174. }
  175. }
  176. break;
  177. }
  178. SetFilePointer(fh, pos+(int)objh.size, NULL, FILE_BEGIN);
  179. }
  180. CloseHandle(fh);
  181. return 1;
  182. }