NutFile.cpp
上传用户:xjjlds
上传日期:2015-12-05
资源大小:22823k
文件大小:4k
源码类别:

多媒体编程

开发平台:

Visual C++

  1. #include "StdAfx.h"
  2. #include "NutFile.h"
  3. CNutFile::CNutFile(IAsyncReader* pAsyncReader, HRESULT& hr)
  4. : CBaseSplitterFile(pAsyncReader, hr)
  5. {
  6. if(FAILED(hr)) return;
  7. hr = Init();
  8. }
  9. HRESULT CNutFile::Init()
  10. {
  11. Seek(0);
  12. if(BitRead(64) != NUTM)
  13. return E_FAIL;
  14. m_streams.RemoveAll();
  15. Seek(0);
  16. while(GetPos() < GetLength())
  17. {
  18. frame_header fh;
  19. fh.checksum_flag = 1;
  20. UINT64 id = 0;
  21. if(BitRead(1, true) == 0
  22. || (id = BitRead(64)) == NUTK)
  23. break;
  24. packet_header ph;
  25. Read(ph);
  26. if(id == NUTM)
  27. {
  28. Read(m_mh);
  29. }
  30. else if(id == NUTS)
  31. {
  32. CAutoPtr<stream_header> sh(new stream_header());
  33. Read(*sh);
  34. if(sh->stream_class == SC_VIDEO) Read(sh->vsh);
  35. else if(sh->stream_class == SC_AUDIO) Read(sh->ash);
  36. // else if(sh->stream_class == SC_SUBTITLE) ; // nothing to do
  37. m_streams.AddTail(sh);
  38. }
  39. else if(id == NUTX)
  40. {
  41. index_header ih;
  42. Read(ih);
  43. }
  44. else if(id == NUTI)
  45. {
  46. info_header ih;
  47. Read(ih);
  48. }
  49. else if(id == 0) // frame
  50. {
  51. ASSERT(0);
  52. break;
  53. }
  54. if(fh.checksum_flag)
  55. {
  56. Seek(ph.pos + ph.fptr - 4);
  57. ph.checksum = (UINT32)BitRead(32);
  58. }
  59. Seek(ph.pos + ph.fptr);
  60. }
  61. Seek(0);
  62. return m_streams.GetCount() ? S_OK : E_FAIL;
  63. }
  64. void CNutFile::Read(vint& vi)
  65. {
  66. vi = 0;
  67. bool more;
  68. do {more = !!BitRead(1); vi = (vi << 7) | BitRead(7);}
  69. while(more);
  70. }
  71. void CNutFile::Read(svint& svi)
  72. {
  73. vint vi;
  74. Read(vi);
  75. vi++;
  76. if(vi&1) svi = -((svint)vi>>1);
  77. else svi = ((svint)vi>>1);
  78. }
  79. void CNutFile::Read(binary& b)
  80. {
  81. vint len;
  82. Read(len);
  83. b.SetSize((INT_PTR)len);
  84. for(BYTE* buff = b.GetData(); len-- > 0; *buff++ = (BYTE)BitRead(8));
  85. }
  86. void CNutFile::Read(packet_header& ph)
  87. {
  88. ph.pos = GetPos();
  89. Read(ph.fptr);
  90. Read(ph.bptr);
  91. }
  92. void CNutFile::Read(main_header& mh)
  93. {
  94. Read(mh.version);
  95. Read(mh.stream_count);
  96. }
  97. void CNutFile::Read(stream_header& sh)
  98. {
  99. Read(sh.stream_id);
  100. Read(sh.stream_class);
  101.     Read(sh.fourcc);
  102.     Read(sh.average_bitrate);
  103.     Read(sh.language_code);
  104.     Read(sh.time_base_nom);
  105.     Read(sh.time_base_denom);
  106.     Read(sh.msb_timestamp_shift);
  107.     Read(sh.shuffle_type);
  108.     sh.fixed_fps = BitRead(1);
  109.     sh.index_flag = BitRead(1);
  110.     sh.reserved = BitRead(6);
  111. while(1)
  112. {
  113. CAutoPtr<codec_specific> p(new codec_specific());
  114. Read(p->type);
  115. if(p->type == 0) break;
  116. Read(p->data);
  117. sh.cs.AddTail(p);
  118. }
  119. }
  120. void CNutFile::Read(video_stream_header& vsh)
  121. {
  122. Read(vsh.width);
  123. Read(vsh.height);
  124. Read(vsh.sample_width);
  125. Read(vsh.sample_height);
  126. Read(vsh.colorspace_type);
  127. }
  128. void CNutFile::Read(audio_stream_header& ash)
  129. {
  130. Read(ash.samplerate_mul);
  131. Read(ash.channel_count);
  132. }
  133. void CNutFile::Read(index_header& ih)
  134. {
  135. Read(ih.stream_id);
  136. vint len;
  137. Read(len);
  138. ih.ie.SetSize((INT_PTR)len);
  139. for(index_entry* p = ih.ie.GetData(); len-- > 0;)
  140. {
  141. Read(p->timestamp);
  142. Read(p->position);
  143. }
  144. }
  145. void CNutFile::Read(info_header& ih)
  146. {
  147. // TODO
  148. /*
  149.         for(;;){
  150.                 id                              v
  151.                 if(id==0) break
  152.                 name= info_table[id][0]
  153.                 type= info_table[id][1]
  154.                 if(type==NULL)
  155.                         type                    b
  156.                 if(name==NULL)
  157.                         name                    b
  158.                 if(type=="v")
  159.                         value                   v
  160.                 else if(type=="s")
  161.                         value                   s
  162.                 else
  163.                         value                   b
  164.         }
  165. */
  166. }