VPXDecoder.cpp 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  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 "VPXDecoder.hpp"
  21. #include <vpx/vpx_decoder.h>
  22. #include <vpx/vp8dx.h>
  23. #include <stdlib.h>
  24. #include <string.h>
  25. VPXDecoder::VPXDecoder(const WebMDemuxer &demuxer, unsigned threads) :
  26. m_ctx(NULL),
  27. m_iter(NULL),
  28. m_delay(0),
  29. m_last_space(VPX_CS_UNKNOWN)
  30. {
  31. if (threads > 8)
  32. threads = 8;
  33. else if (threads < 1)
  34. threads = 1;
  35. const vpx_codec_dec_cfg_t codecCfg = {
  36. threads,
  37. 0,
  38. 0
  39. };
  40. vpx_codec_iface_t *codecIface = NULL;
  41. switch (demuxer.getVideoCodec())
  42. {
  43. case WebMDemuxer::VIDEO_VP8:
  44. codecIface = vpx_codec_vp8_dx();
  45. break;
  46. case WebMDemuxer::VIDEO_VP9:
  47. codecIface = vpx_codec_vp9_dx();
  48. m_delay = threads - 1;
  49. break;
  50. default:
  51. return;
  52. }
  53. m_ctx = new vpx_codec_ctx_t;
  54. if (vpx_codec_dec_init(m_ctx, codecIface, &codecCfg, m_delay > 0 ? VPX_CODEC_USE_FRAME_THREADING : 0))
  55. {
  56. delete m_ctx;
  57. m_ctx = NULL;
  58. }
  59. }
  60. VPXDecoder::~VPXDecoder()
  61. {
  62. if (m_ctx)
  63. {
  64. vpx_codec_destroy(m_ctx);
  65. delete m_ctx;
  66. }
  67. }
  68. bool VPXDecoder::decode(const WebMFrame &frame)
  69. {
  70. m_iter = NULL;
  71. return !vpx_codec_decode(m_ctx, frame.buffer, frame.bufferSize, NULL, 0);
  72. }
  73. VPXDecoder::IMAGE_ERROR VPXDecoder::getImage(Image &image)
  74. {
  75. IMAGE_ERROR err = NO_FRAME;
  76. if (vpx_image_t *img = vpx_codec_get_frame(m_ctx, &m_iter))
  77. {
  78. // It seems to be a common problem that UNKNOWN comes up a lot, yet FFMPEG is somehow getting accurate colour-space information.
  79. // After checking FFMPEG code, *they're* getting colour-space information, so I'm assuming something like this is going on.
  80. // It appears to work, at least.
  81. if (img->cs != VPX_CS_UNKNOWN)
  82. m_last_space = img->cs;
  83. if ((img->fmt & VPX_IMG_FMT_PLANAR) && !(img->fmt & (VPX_IMG_FMT_HAS_ALPHA | VPX_IMG_FMT_HIGHBITDEPTH)))
  84. {
  85. if (img->stride[0] && img->stride[1] && img->stride[2])
  86. {
  87. const int uPlane = !!(img->fmt & VPX_IMG_FMT_UV_FLIP) + 1;
  88. const int vPlane = !(img->fmt & VPX_IMG_FMT_UV_FLIP) + 1;
  89. image.w = img->d_w;
  90. image.h = img->d_h;
  91. image.cs = m_last_space;
  92. image.chromaShiftW = img->x_chroma_shift;
  93. image.chromaShiftH = img->y_chroma_shift;
  94. image.planes[0] = img->planes[0];
  95. image.planes[1] = img->planes[uPlane];
  96. image.planes[2] = img->planes[vPlane];
  97. image.linesize[0] = img->stride[0];
  98. image.linesize[1] = img->stride[uPlane];
  99. image.linesize[2] = img->stride[vPlane];
  100. err = NO_ERROR;
  101. }
  102. }
  103. else
  104. {
  105. err = UNSUPPORTED_FRAME;
  106. }
  107. }
  108. return err;
  109. }
  110. /**/
  111. // -- GODOT begin --
  112. #if 0
  113. // -- GODOT end --
  114. static inline int ceilRshift(int val, int shift)
  115. {
  116. return (val + (1 << shift) - 1) >> shift;
  117. }
  118. int VPXDecoder::Image::getWidth(int plane) const
  119. {
  120. if (!plane)
  121. return w;
  122. return ceilRshift(w, chromaShiftW);
  123. }
  124. int VPXDecoder::Image::getHeight(int plane) const
  125. {
  126. if (!plane)
  127. return h;
  128. return ceilRshift(h, chromaShiftH);
  129. }
  130. // -- GODOT begin --
  131. #endif
  132. // -- GODOT end --