1
0

main.cpp 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <windows.h> // evil, i know
  4. #include <bfc/platform/types.h>
  5. #include "avi_header.h"
  6. #include "file_avi_reader.h"
  7. #include "read.h"
  8. #include "Info.h"
  9. using namespace nsavi;
  10. void printf_riff_chunk(const riff_chunk *chunk, int indent)
  11. {
  12. char cc[5];
  13. memcpy(cc, &chunk->id, 4);
  14. cc[4]=0;
  15. if (chunk->type)
  16. {
  17. char type[5];
  18. memcpy(type, &chunk->type, 4);
  19. type[4]=0;
  20. printf("%*sID: %4s%*sSIZE: %10u TYPE: %s\r\n", indent, "", cc,8-indent, "",chunk->size, type);
  21. }
  22. else
  23. printf("%*sID: %4s%*sSIZE: %10u\r\n", indent, "", cc, 8-indent, "", chunk->size);
  24. }
  25. uint32_t ParseLIST(nsavi::avi_reader *reader, const riff_chunk *parse_chunk, int indent);
  26. uint32_t ParseHDRL(nsavi::avi_reader *reader, uint32_t chunk_size, int indent)
  27. {
  28. uint32_t total_bytes_read=0;
  29. uint32_t bytes_read=0;
  30. riff_chunk chunk;
  31. while ((chunk_size - total_bytes_read) >= 8
  32. && total_bytes_read < chunk_size // seems redundant, but since the above is unsigned math, we won't get negative values
  33. && (read_riff_chunk(reader, &chunk, &bytes_read) == READ_OK))
  34. {
  35. total_bytes_read += bytes_read;
  36. printf_riff_chunk(&chunk, indent);
  37. if (chunk.id == 'TSIL')
  38. {
  39. bytes_read = ParseLIST(reader, &chunk, indent+1);
  40. if (!bytes_read)
  41. return 0;
  42. total_bytes_read += bytes_read;
  43. }
  44. else if (chunk.id == 'hiva')
  45. {
  46. nsavi::AVIH *header = (nsavi::AVIH *)malloc(chunk.size + sizeof(uint32_t));
  47. if (header)
  48. {
  49. reader->Read(((uint8_t *)header) + sizeof(uint32_t), chunk.size, &bytes_read);
  50. if (bytes_read != chunk.size)
  51. return 0;
  52. total_bytes_read+=bytes_read;
  53. header->size_bytes = chunk.size;
  54. }
  55. else
  56. return 0;
  57. }
  58. else
  59. {
  60. if (skip_chunk(reader, &chunk, &bytes_read) != READ_OK)
  61. return 0;
  62. total_bytes_read += bytes_read;
  63. }
  64. }
  65. return total_bytes_read;
  66. }
  67. uint32_t ParseSTRL(nsavi::avi_reader *reader, uint32_t chunk_size, int indent)
  68. {
  69. uint32_t total_bytes_read=0;
  70. uint32_t bytes_read=0;
  71. riff_chunk chunk;
  72. while ((chunk_size - total_bytes_read) >= 8
  73. && total_bytes_read < chunk_size // seems redundant, but since the above is unsigned math, we won't get negative values
  74. && (read_riff_chunk(reader, &chunk, &bytes_read) == READ_OK))
  75. {
  76. total_bytes_read += bytes_read;
  77. printf_riff_chunk(&chunk, indent);
  78. if (chunk.id == 'TSIL')
  79. {
  80. bytes_read = ParseLIST(reader, &chunk, indent+1);
  81. if (!bytes_read)
  82. return 0;
  83. total_bytes_read += bytes_read;
  84. }
  85. else if (chunk.id == 'hrts')
  86. {
  87. nsavi::STRH *header = (nsavi::STRH *)malloc(chunk.size + sizeof(uint32_t));
  88. if (header)
  89. {
  90. reader->Read(((uint8_t *)header) + sizeof(uint32_t), chunk.size, &bytes_read);
  91. if (bytes_read != chunk.size)
  92. return 0;
  93. total_bytes_read+=bytes_read;
  94. header->size_bytes = chunk.size;
  95. }
  96. else
  97. return 0;
  98. }
  99. else if (chunk.id == 'frts')
  100. {
  101. nsavi::STRF *header = (nsavi::STRF *)malloc(chunk.size + sizeof(uint32_t));
  102. if (header)
  103. {
  104. reader->Read(((uint8_t *)header) + sizeof(uint32_t), chunk.size, &bytes_read);
  105. if (bytes_read != chunk.size)
  106. return 0;
  107. total_bytes_read+=bytes_read;
  108. header->size_bytes = chunk.size;
  109. }
  110. else
  111. return 0;
  112. }
  113. else if (chunk.id == 'xdni')
  114. {
  115. nsavi::INDX *index = (nsavi::INDX *)malloc(chunk.size + sizeof(uint32_t));
  116. if (index)
  117. {
  118. reader->Read(&index->entry_size, chunk.size, &bytes_read);
  119. if (bytes_read != chunk.size)
  120. return 0;
  121. total_bytes_read+=bytes_read;
  122. index->size_bytes = chunk.size;
  123. }
  124. else
  125. return 0;
  126. }
  127. else
  128. {
  129. if (skip_chunk(reader, &chunk, &bytes_read) != READ_OK)
  130. return 0;
  131. total_bytes_read += bytes_read;
  132. }
  133. }
  134. return total_bytes_read;
  135. }
  136. uint32_t ParseGeneric(nsavi::avi_reader *reader, uint32_t chunk_size, int indent)
  137. {
  138. uint32_t total_bytes_read=0;
  139. uint32_t bytes_read=0;
  140. riff_chunk chunk;
  141. while ((chunk_size - total_bytes_read) >= 8
  142. && total_bytes_read < chunk_size // seems redundant, but since the above is unsigned math, we won't get negative values
  143. && (read_riff_chunk(reader, &chunk, &bytes_read) == READ_OK))
  144. {
  145. total_bytes_read += bytes_read;
  146. printf_riff_chunk(&chunk, indent);
  147. if (chunk.id == 'TSIL')
  148. {
  149. bytes_read = ParseLIST(reader, &chunk, indent+1);
  150. if (!bytes_read)
  151. return 0;
  152. total_bytes_read += bytes_read;
  153. }
  154. else
  155. {
  156. if (skip_chunk(reader, &chunk, &bytes_read) != READ_OK)
  157. return 0;
  158. total_bytes_read += bytes_read;
  159. }
  160. }
  161. return total_bytes_read;
  162. }
  163. uint32_t ParseLIST(nsavi::avi_reader *reader, const riff_chunk *parse_chunk, int indent)
  164. {
  165. uint32_t total_bytes_read=0;
  166. uint32_t bytes_read=0;
  167. if (parse_chunk->type == 'lrdh')
  168. ParseHDRL(reader, parse_chunk->size, indent);
  169. else if (parse_chunk->type == 'lrts')
  170. ParseSTRL(reader, parse_chunk->size, indent);
  171. else if (parse_chunk->type == 'lmdo')
  172. ParseSTRL(reader, parse_chunk->size, indent);
  173. else if (parse_chunk->type == 'OFNI')
  174. {
  175. Info *info = new Info;
  176. info->Read(reader, parse_chunk->size);
  177. info = info;
  178. }
  179. //else if (parse_chunk->type == 'ivom')
  180. //ParseGeneric(reader, parse_chunk->size, indent);
  181. //else if (parse_chunk->type == ' cer')
  182. //ParseGeneric(reader, parse_chunk->size, indent);
  183. else
  184. reader->Skip(parse_chunk->size);
  185. if (parse_chunk->size & 1)
  186. reader->Skip(1);
  187. return parse_chunk->size;
  188. }
  189. uint32_t ParseRIFF(nsavi::avi_reader *reader, uint32_t chunk_size, int indent)
  190. {
  191. uint32_t total_bytes_read=0;
  192. uint32_t bytes_read=0;
  193. riff_chunk chunk;
  194. while ((chunk_size - total_bytes_read) >= 8
  195. && total_bytes_read < chunk_size // seems redundant, but since the above is unsigned math, we won't get negative values
  196. && (read_riff_chunk(reader, &chunk, &bytes_read) == READ_OK))
  197. {
  198. total_bytes_read += bytes_read;
  199. printf_riff_chunk(&chunk, indent);
  200. if (chunk.id == 'TSIL')
  201. {
  202. bytes_read = ParseLIST(reader, &chunk, indent+1);
  203. if (!bytes_read)
  204. return 0;
  205. total_bytes_read += bytes_read;
  206. }
  207. else if (chunk.id == '1xdi')
  208. {
  209. nsavi::IDX1 *index = (nsavi::IDX1 *)malloc(chunk.size + sizeof(uint32_t));
  210. if (index)
  211. {
  212. reader->Read(((uint8_t *)index) + sizeof(uint32_t), chunk.size, &bytes_read);
  213. if (bytes_read != chunk.size)
  214. return 0;
  215. total_bytes_read+=bytes_read;
  216. index->index_count = chunk.size / sizeof(IDX1_INDEX);
  217. }
  218. }
  219. else
  220. {
  221. if (skip_chunk(reader, &chunk, &bytes_read) != READ_OK)
  222. return 0;
  223. total_bytes_read += bytes_read;
  224. }
  225. }
  226. if (chunk_size & 1)
  227. reader->Skip(1);
  228. return total_bytes_read;
  229. }
  230. int main()
  231. {
  232. AVIReaderFILE reader(L"//o2d2/ftp/usr/nullsoft/test media/20bit/Track 1.wav");
  233. riff_chunk chunk;
  234. while (read_riff_chunk(&reader, &chunk) == READ_OK)
  235. {
  236. printf_riff_chunk(&chunk, 0);
  237. if (chunk.id == 'FFIR')
  238. ParseRIFF(&reader, chunk.size, 1);
  239. else
  240. skip_chunk(&reader, &chunk);
  241. }
  242. }