Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- extern "C"
- JNIEXPORT void JNICALL makeJniName(VideoProcessingThread_runNative)(JNIEnv* env, jobject thiz, jstring videopath) {
- using namespace cloture::util::common;
- const char* utf_vpath = env->GetStringUTFChars(videopath, nullptr);
- size_t vpathlen = env->GetStringUTFLength(videopath);
- CSApplication &app = *getCSAPP();
- app.updateEnv(env);
- app.displayToast("Running native video databending.");
- if(!didRegisterCodecs)
- {
- av_register_all();
- didRegisterCodecs = true;
- }
- AVFormatContext *fmtContext = nullptr;
- //AVInputFormat* iFormat = findFormat("mp4");
- if(avformat_open_input(&fmtContext, utf_vpath, /*iFormat*/nullptr, nullptr)) {
- app.displayToast("Failed to open video file.");
- return;
- }
- if(avformat_find_stream_info(fmtContext, nullptr) < 0) {
- app.displayToast("Failed to find stream information for video.");
- return;
- }
- int firstVideoStream = findFirstVideoStream(fmtContext);
- if( firstVideoStream == -1) {
- app.displayToast("Couldn't find any video streams.");
- return;
- }
- AVCodecContext* codecContext = fmtContext->streams[firstVideoStream]->codec;
- AVCodec* codec = avcodec_find_decoder(codecContext->codec_id);
- if(!codec) {
- app.displayToast("Unsupported codec.");
- return;
- }
- AVDictionary* options;
- if(avcodec_open2(codecContext, codec, &options) < 0) {
- app.displayToast("Couldn't open codec!");
- return;
- }
- AVFrame* frame = av_frame_alloc();
- AVFrame* frameRGB = av_frame_alloc();
- if(!frameRGB || !frame) {
- app.displayToast("Couldn't allocate frames!");
- return;
- }
- size_t numBytes = avpicture_get_size(AV_PIX_FMT_RGB24, codecContext->width, codecContext->height);
- uint8* buffer = (uint8*)av_malloc(numBytes);
- SwsContext *swsContext = sws_getContext(codecContext->width,
- codecContext->height,
- codecContext->pix_fmt,
- codecContext->width,
- codecContext->height,
- AV_PIX_FMT_RGB24,
- SWS_BILINEAR, nullptr, nullptr, nullptr);
- avpicture_fill((AVPicture*)frameRGB, buffer, AV_PIX_FMT_RGB24, codecContext->width, codecContext->height);
- int i = 0;
- AVPacket packet;
- int frameFinished;
- while(av_read_frame(fmtContext, &packet) >= 0) {
- if(packet.stream_index == firstVideoStream) {
- avcodec_decode_video2(codecContext, frame, &frameFinished, &packet);
- if(frameFinished) {
- sws_scale(swsContext, (uint8 const * const *)frame->data, frame->linesize, 0,
- codecContext->height, frameRGB->data, frameRGB->linesize);
- }
- if(++i <= 5)
- SaveFrame(frameRGB, codecContext->width, codecContext->height, i);
- }
- av_free_packet(&packet);
- }
- av_free(buffer);
- av_free(frameRGB);
- av_free(frame);
- avcodec_close(codecContext);
- avformat_close_input(&fmtContext);
- env->ReleaseStringUTFChars(videopath, utf_vpath);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement