Advertisement
Guest User

Untitled

a guest
Jan 14th, 2017
144
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.23 KB | None | 0 0
  1.  
  2. extern "C"
  3. JNIEXPORT void JNICALL makeJniName(VideoProcessingThread_runNative)(JNIEnv* env, jobject thiz, jstring videopath) {
  4. using namespace cloture::util::common;
  5.  
  6. const char* utf_vpath = env->GetStringUTFChars(videopath, nullptr);
  7. size_t vpathlen = env->GetStringUTFLength(videopath);
  8. CSApplication &app = *getCSAPP();
  9. app.updateEnv(env);
  10. app.displayToast("Running native video databending.");
  11. if(!didRegisterCodecs)
  12. {
  13. av_register_all();
  14. didRegisterCodecs = true;
  15. }
  16.  
  17. AVFormatContext *fmtContext = nullptr;
  18. //AVInputFormat* iFormat = findFormat("mp4");
  19.  
  20. if(avformat_open_input(&fmtContext, utf_vpath, /*iFormat*/nullptr, nullptr)) {
  21. app.displayToast("Failed to open video file.");
  22. return;
  23. }
  24.  
  25. if(avformat_find_stream_info(fmtContext, nullptr) < 0) {
  26. app.displayToast("Failed to find stream information for video.");
  27. return;
  28. }
  29.  
  30. int firstVideoStream = findFirstVideoStream(fmtContext);
  31. if( firstVideoStream == -1) {
  32. app.displayToast("Couldn't find any video streams.");
  33. return;
  34. }
  35. AVCodecContext* codecContext = fmtContext->streams[firstVideoStream]->codec;
  36. AVCodec* codec = avcodec_find_decoder(codecContext->codec_id);
  37. if(!codec) {
  38. app.displayToast("Unsupported codec.");
  39. return;
  40. }
  41. AVDictionary* options;
  42. if(avcodec_open2(codecContext, codec, &options) < 0) {
  43. app.displayToast("Couldn't open codec!");
  44. return;
  45. }
  46. AVFrame* frame = av_frame_alloc();
  47. AVFrame* frameRGB = av_frame_alloc();
  48. if(!frameRGB || !frame) {
  49. app.displayToast("Couldn't allocate frames!");
  50. return;
  51. }
  52. size_t numBytes = avpicture_get_size(AV_PIX_FMT_RGB24, codecContext->width, codecContext->height);
  53. uint8* buffer = (uint8*)av_malloc(numBytes);
  54. SwsContext *swsContext = sws_getContext(codecContext->width,
  55. codecContext->height,
  56. codecContext->pix_fmt,
  57. codecContext->width,
  58. codecContext->height,
  59. AV_PIX_FMT_RGB24,
  60. SWS_BILINEAR, nullptr, nullptr, nullptr);
  61.  
  62. avpicture_fill((AVPicture*)frameRGB, buffer, AV_PIX_FMT_RGB24, codecContext->width, codecContext->height);
  63.  
  64. int i = 0;
  65. AVPacket packet;
  66. int frameFinished;
  67.  
  68. while(av_read_frame(fmtContext, &packet) >= 0) {
  69. if(packet.stream_index == firstVideoStream) {
  70. avcodec_decode_video2(codecContext, frame, &frameFinished, &packet);
  71. if(frameFinished) {
  72. sws_scale(swsContext, (uint8 const * const *)frame->data, frame->linesize, 0,
  73. codecContext->height, frameRGB->data, frameRGB->linesize);
  74. }
  75. if(++i <= 5)
  76. SaveFrame(frameRGB, codecContext->width, codecContext->height, i);
  77. }
  78. av_free_packet(&packet);
  79. }
  80.  
  81. av_free(buffer);
  82. av_free(frameRGB);
  83. av_free(frame);
  84. avcodec_close(codecContext);
  85. avformat_close_input(&fmtContext);
  86. env->ReleaseStringUTFChars(videopath, utf_vpath);
  87. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement