Advertisement
Guest User

Untitled

a guest
Apr 25th, 2013
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.70 KB | None | 0 0
  1. std::unique_ptr<PixelBuffer> FrameGrabber::GetNextFrame()
  2. {
  3. uint32 width = impl->pCodecCtx->width;
  4. uint32 height = impl->pCodecCtx->height;
  5. AVFrame* pFrame = avcodec_alloc_frame();
  6. AVFrame* pRGBFrame = avcodec_alloc_frame();
  7.  
  8. int numBytes = avpicture_get_size(PIX_FMT_RGB24, width, height);
  9. uint8_t* buffer = (uint8_t*)av_malloc(numBytes * sizeof(uint8_t));
  10. int byteSize = avpicture_fill((AVPicture*)pRGBFrame, buffer, PIX_FMT_RGB24, width, height);
  11.  
  12. int frameFinished;
  13. AVPacket packet;
  14.  
  15. auto sws = sws_getContext(width, height, PIX_FMT_YUV420P, width, height, PIX_FMT_RGB24, SWS_BICUBIC, 0, 0, 0);
  16.  
  17. while(av_read_frame(impl->pFormatCtx, &packet) >= 0)
  18. {
  19. if(packet.stream_index == impl->videoStream)
  20. {
  21. avcodec_decode_video2(impl->pCodecCtx, pFrame, &frameFinished, &packet);
  22.  
  23. if(frameFinished)
  24. {
  25. if(!SanityCheck(pFrame))
  26. throw std::exception("Bad frame");
  27.  
  28. glm::ivec2 size(width, height);
  29. int err = sws_scale(sws, pFrame->data, pFrame->linesize, 0, height, pRGBFrame->data, pRGBFrame->linesize);
  30.  
  31. if(!SanityCheck(pRGBFrame))
  32. throw std::exception("Bad frame");
  33.  
  34. uint8* data = new uint8[byteSize];
  35.  
  36. memcpy(data, pRGBFrame->data[0], byteSize);
  37.  
  38. auto pixels = std::unique_ptr<PixelBuffer>(new PixelBuffer(size, std::unique_ptr<uint8>(data)));
  39. sws_freeContext(sws);
  40.  
  41. avcodec_free_frame(&pFrame);
  42. avcodec_free_frame(&pRGBFrame);
  43. av_free_packet(&packet);
  44. av_free(buffer);
  45. return pixels;
  46. }
  47. }
  48. }
  49.  
  50. av_free(buffer);
  51. sws_freeContext(sws);
  52. avcodec_free_frame(&pFrame);
  53. avcodec_free_frame(&pRGBFrame);
  54. av_free_packet(&packet);
  55.  
  56. return nullptr;
  57. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement