1
0

nsv_h264_decoder.cpp 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. #include "nsv_h264_decoder.h"
  2. #include "../nsv/nsvlib.h"
  3. #include "../nsv/dec_if.h"
  4. #include <assert.h>
  5. #include <Mferror.h>
  6. H264_Decoder::H264_Decoder()
  7. {
  8. vidbufdec=0;
  9. last_pic = 0;
  10. decoder.Open();
  11. }
  12. H264_Decoder::~H264_Decoder()
  13. {
  14. for (size_t i=0;i<buffered_frames.size();i++) {
  15. nullsoft_h264_frame_data frame_data = buffered_frames[i];
  16. decoder.FreeFrame((YV12_PLANES *)frame_data.data, frame_data.decoder_data);
  17. }
  18. decoder.FreeFrame(vidbufdec, last_pic);
  19. }
  20. int H264_Decoder::decode(int need_kf,
  21. void *_in, int _in_len,
  22. void **out, // out is set to a pointer to data
  23. unsigned int *out_type, // 'Y','V','1','2' is currently defined
  24. int *is_kf)
  25. {
  26. *out_type=NSV_MAKETYPE('Y','V','1','2');
  27. if (last_pic)
  28. {
  29. decoder.FreeFrame(vidbufdec, last_pic);
  30. vidbufdec=0;
  31. last_pic=0;
  32. }
  33. if (_in_len) {
  34. for (;;) {
  35. HRESULT hr = decoder.FeedRaw(_in, _in_len, 0);
  36. if (hr == MF_E_NOTACCEPTING) {
  37. nullsoft_h264_frame_data frame_data;
  38. if (FAILED(decoder.GetFrame((YV12_PLANES **)&frame_data.data, &frame_data.decoder_data, &frame_data.local_timestamp))) {
  39. continue;
  40. }
  41. buffered_frames.push_back(frame_data);
  42. } else if (FAILED(hr)) {
  43. return -1;
  44. } else {
  45. break;
  46. }
  47. }
  48. } else {
  49. decoder.Drain();
  50. }
  51. if (SUCCEEDED(decoder.GetFrame(&vidbufdec, &last_pic, 0))) {
  52. *out = vidbufdec;
  53. *is_kf = 1;
  54. } else {
  55. *out = 0;
  56. }
  57. return 0;
  58. }
  59. void H264_Decoder::flush()
  60. {
  61. for ( size_t i = 0; i < buffered_frames.size(); i++ )
  62. {
  63. nullsoft_h264_frame_data frame_data = buffered_frames[ i ];
  64. decoder.FreeFrame( (YV12_PLANES *)frame_data.data, frame_data.decoder_data );
  65. }
  66. decoder.Flush();
  67. }