nsv_vp8_decoder.cpp 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. #include "api.h"
  2. #include "nsv_vp8_decoder.h"
  3. #include "../nsv/nsvlib.h"
  4. #include <new>
  5. IVideoDecoder *NSVFactory::CreateVideoDecoder(int w, int h, double framerate, unsigned int fmt, int *flip)
  6. {
  7. if (fmt == NSV_MAKETYPE('V','P','8','0'))
  8. {
  9. *flip=1;
  10. void *mem = WASABI_API_MEMMGR->sysMalloc(sizeof(VP8_Decoder));
  11. VP8_Decoder *dec = new (mem) VP8_Decoder(w,h);
  12. return dec;
  13. }
  14. return NULL;
  15. }
  16. #define CBCLASS NSVFactory
  17. START_DISPATCH;
  18. CB(SVC_NSVFACTORY_CREATEVIDEODECODER, CreateVideoDecoder)
  19. END_DISPATCH;
  20. #undef CBCLASS
  21. VP8_Decoder::VP8_Decoder(int w, int h)
  22. {
  23. vpx_codec_dec_init(&decoder, &vpx_codec_vp8_dx_algo, NULL, 0);
  24. }
  25. VP8_Decoder::~VP8_Decoder()
  26. {
  27. vpx_codec_destroy(&decoder);
  28. }
  29. int VP8_Decoder::decode(int need_kf,
  30. void *in, int in_len,
  31. void **out, // out is set to a pointer to data
  32. unsigned int *out_type, // 'Y','V','1','2' is currently defined
  33. int *is_kf)
  34. {
  35. unsigned char *data=(unsigned char *)in;
  36. if (in_len)
  37. {
  38. vpx_codec_decode(&decoder, (const uint8_t *)in, in_len, 0, 0);
  39. vpx_codec_stream_info_t stream_info;
  40. stream_info.sz = sizeof(stream_info);
  41. if (vpx_codec_get_stream_info(&decoder, &stream_info) == VPX_CODEC_OK)
  42. {
  43. *is_kf = stream_info.is_kf;
  44. if (need_kf && !stream_info.is_kf)
  45. return 0;
  46. }
  47. }
  48. *out_type=NSV_MAKETYPE('Y','V','1','2');
  49. vpx_codec_iter_t frame_iterator = 0;
  50. vpx_image_t *image = vpx_codec_get_frame(&decoder, &frame_iterator);
  51. if (image)
  52. {
  53. planes.y.baseAddr = image->planes[0];
  54. planes.y.rowBytes = image->stride[0];
  55. planes.u.baseAddr = image->planes[1];
  56. planes.u.rowBytes = image->stride[1];
  57. planes.v.baseAddr = image->planes[2];
  58. planes.v.rowBytes = image->stride[2];
  59. *out = &planes;
  60. return 0;
  61. }
  62. return 0;
  63. }