WebMDemuxer.cpp 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  1. /*
  2. MIT License
  3. Copyright (c) 2016 Błażej Szczygieł
  4. Permission is hereby granted, free of charge, to any person obtaining a copy
  5. of this software and associated documentation files (the "Software"), to deal
  6. in the Software without restriction, including without limitation the rights
  7. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. copies of the Software, and to permit persons to whom the Software is
  9. furnished to do so, subject to the following conditions:
  10. The above copyright notice and this permission notice shall be included in all
  11. copies or substantial portions of the Software.
  12. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  13. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  14. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  15. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  16. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  17. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  18. SOFTWARE.
  19. */
  20. #include "WebMDemuxer.hpp"
  21. #include "mkvparser/mkvparser.h"
  22. #include <assert.h>
  23. #include <stdlib.h>
  24. #include <string.h>
  25. WebMFrame::WebMFrame() :
  26. bufferSize(0), bufferCapacity(0),
  27. buffer(NULL),
  28. time(0),
  29. key(false)
  30. {}
  31. WebMFrame::~WebMFrame()
  32. {
  33. free(buffer);
  34. }
  35. /**/
  36. WebMDemuxer::WebMDemuxer(mkvparser::IMkvReader *reader, int videoTrack, int audioTrack) :
  37. m_reader(reader),
  38. m_segment(NULL),
  39. m_cluster(NULL), m_block(NULL), m_blockEntry(NULL),
  40. m_blockFrameIndex(0),
  41. m_videoTrack(NULL), m_vCodec(NO_VIDEO),
  42. m_audioTrack(NULL), m_aCodec(NO_AUDIO),
  43. m_isOpen(false),
  44. m_eos(false)
  45. {
  46. long long pos = 0;
  47. if (mkvparser::EBMLHeader().Parse(m_reader, pos))
  48. return;
  49. if (mkvparser::Segment::CreateInstance(m_reader, pos, m_segment))
  50. return;
  51. if (m_segment->Load() < 0)
  52. return;
  53. const mkvparser::Tracks *tracks = m_segment->GetTracks();
  54. const unsigned long tracksCount = tracks->GetTracksCount();
  55. int currVideoTrack = -1, currAudioTrack = -1;
  56. for (unsigned long i = 0; i < tracksCount; ++i)
  57. {
  58. const mkvparser::Track *track = tracks->GetTrackByIndex(i);
  59. if (const char *codecId = track->GetCodecId())
  60. {
  61. if ((!m_videoTrack || currVideoTrack != videoTrack) && track->GetType() == mkvparser::Track::kVideo)
  62. {
  63. if (!strcmp(codecId, "V_VP8"))
  64. m_vCodec = VIDEO_VP8;
  65. else if (!strcmp(codecId, "V_VP9"))
  66. m_vCodec = VIDEO_VP9;
  67. if (m_vCodec != NO_VIDEO)
  68. m_videoTrack = static_cast<const mkvparser::VideoTrack *>(track);
  69. ++currVideoTrack;
  70. }
  71. if ((!m_audioTrack || currAudioTrack != audioTrack) && track->GetType() == mkvparser::Track::kAudio)
  72. {
  73. if (!strcmp(codecId, "A_VORBIS"))
  74. m_aCodec = AUDIO_VORBIS;
  75. else if (!strcmp(codecId, "A_OPUS"))
  76. m_aCodec = AUDIO_OPUS;
  77. if (m_aCodec != NO_AUDIO)
  78. m_audioTrack = static_cast<const mkvparser::AudioTrack *>(track);
  79. ++currAudioTrack;
  80. }
  81. }
  82. }
  83. if (!m_videoTrack && !m_audioTrack)
  84. return;
  85. m_isOpen = true;
  86. }
  87. WebMDemuxer::~WebMDemuxer()
  88. {
  89. delete m_segment;
  90. delete m_reader;
  91. }
  92. double WebMDemuxer::getLength() const
  93. {
  94. return m_segment->GetDuration() / 1e9;
  95. }
  96. WebMDemuxer::VIDEO_CODEC WebMDemuxer::getVideoCodec() const
  97. {
  98. return m_vCodec;
  99. }
  100. int WebMDemuxer::getWidth() const
  101. {
  102. return m_videoTrack->GetWidth();
  103. }
  104. int WebMDemuxer::getHeight() const
  105. {
  106. return m_videoTrack->GetHeight();
  107. }
  108. WebMDemuxer::AUDIO_CODEC WebMDemuxer::getAudioCodec() const
  109. {
  110. return m_aCodec;
  111. }
  112. const unsigned char *WebMDemuxer::getAudioExtradata(size_t &size) const
  113. {
  114. return m_audioTrack->GetCodecPrivate(size);
  115. }
  116. double WebMDemuxer::getSampleRate() const
  117. {
  118. return m_audioTrack->GetSamplingRate();
  119. }
  120. int WebMDemuxer::getChannels() const
  121. {
  122. return m_audioTrack->GetChannels();
  123. }
  124. int WebMDemuxer::getAudioDepth() const
  125. {
  126. return m_audioTrack->GetBitDepth();
  127. }
  128. bool WebMDemuxer::readFrame(WebMFrame *videoFrame, WebMFrame *audioFrame)
  129. {
  130. const long videoTrackNumber = (videoFrame && m_videoTrack) ? m_videoTrack->GetNumber() : 0;
  131. const long audioTrackNumber = (audioFrame && m_audioTrack) ? m_audioTrack->GetNumber() : 0;
  132. bool blockEntryEOS = false;
  133. if (videoFrame)
  134. videoFrame->bufferSize = 0;
  135. if (audioFrame)
  136. audioFrame->bufferSize = 0;
  137. if (videoTrackNumber == 0 && audioTrackNumber == 0)
  138. return false;
  139. if (m_eos)
  140. return false;
  141. if (!m_cluster)
  142. m_cluster = m_segment->GetFirst();
  143. do
  144. {
  145. bool getNewBlock = false;
  146. long status = 0;
  147. if (!m_blockEntry && !blockEntryEOS)
  148. {
  149. status = m_cluster->GetFirst(m_blockEntry);
  150. getNewBlock = true;
  151. }
  152. else if (blockEntryEOS || m_blockEntry->EOS())
  153. {
  154. m_cluster = m_segment->GetNext(m_cluster);
  155. if (!m_cluster || m_cluster->EOS())
  156. {
  157. m_eos = true;
  158. return false;
  159. }
  160. status = m_cluster->GetFirst(m_blockEntry);
  161. blockEntryEOS = false;
  162. getNewBlock = true;
  163. }
  164. else if (!m_block || m_blockFrameIndex == m_block->GetFrameCount() || notSupportedTrackNumber(videoTrackNumber, audioTrackNumber))
  165. {
  166. status = m_cluster->GetNext(m_blockEntry, m_blockEntry);
  167. if (!m_blockEntry || m_blockEntry->EOS())
  168. {
  169. blockEntryEOS = true;
  170. continue;
  171. }
  172. getNewBlock = true;
  173. }
  174. if (status || !m_blockEntry)
  175. return false;
  176. if (getNewBlock)
  177. {
  178. m_block = m_blockEntry->GetBlock();
  179. m_blockFrameIndex = 0;
  180. }
  181. } while (blockEntryEOS || notSupportedTrackNumber(videoTrackNumber, audioTrackNumber));
  182. WebMFrame *frame = NULL;
  183. const long trackNumber = m_block->GetTrackNumber();
  184. if (trackNumber == videoTrackNumber)
  185. frame = videoFrame;
  186. else if (trackNumber == audioTrackNumber)
  187. frame = audioFrame;
  188. else
  189. {
  190. //Should not be possible
  191. assert(trackNumber == videoTrackNumber || trackNumber == audioTrackNumber);
  192. return false;
  193. }
  194. const mkvparser::Block::Frame &blockFrame = m_block->GetFrame(m_blockFrameIndex++);
  195. if (blockFrame.len > frame->bufferCapacity)
  196. {
  197. unsigned char *newBuff = (unsigned char *)realloc(frame->buffer, frame->bufferCapacity = blockFrame.len);
  198. if (newBuff)
  199. frame->buffer = newBuff;
  200. else // Out of memory
  201. return false;
  202. }
  203. frame->bufferSize = blockFrame.len;
  204. frame->time = m_block->GetTime(m_cluster) / 1e9;
  205. frame->key = m_block->IsKey();
  206. return !blockFrame.Read(m_reader, frame->buffer);
  207. }
  208. inline bool WebMDemuxer::notSupportedTrackNumber(long videoTrackNumber, long audioTrackNumber) const
  209. {
  210. const long trackNumber = m_block->GetTrackNumber();
  211. return (trackNumber != videoTrackNumber && trackNumber != audioTrackNumber);
  212. }