SCM make_ffmpeg_input(SCM scm_file_name, SCM scm_debug) { SCM retval; struct ffmpeg_t *self; scm_dynwind_begin(0); const char *file_name = scm_to_locale_string(scm_file_name); scm_dynwind_free(file_name); self = (struct ffmpeg_t *)scm_gc_calloc(sizeof(struct ffmpeg_t), "ffmpeg"); self->video_stream_idx = -1; self->audio_stream_idx = -1; SCM_NEWSMOB(retval, ffmpeg_tag, self); int err; err = avformat_open_input(&self->fmt_ctx, file_name, NULL, NULL); if (err < 0) { ffmpeg_destroy(retval); scm_misc_error("make-ffmpeg-input", "Error opening file '~a': ~a", scm_list_2(scm_file_name, get_error_text(err))); }; err = avformat_find_stream_info(self->fmt_ctx, NULL); if (err < 0) { ffmpeg_destroy(retval); scm_misc_error("make-ffmpeg-input", "No stream information in file '~a': ~a", scm_list_2(scm_file_name, get_error_text(err))); }; // TODO: only open desired streams // Open video stream self->video_stream_idx = av_find_best_stream(self->fmt_ctx, AVMEDIA_TYPE_VIDEO, -1, -1, NULL, 0); if (self->video_stream_idx >= 0) self->video_codec_ctx = open_decoder(retval, scm_file_name, video_stream(self), "video"); // Open audio stream self->audio_stream_idx = av_find_best_stream(self->fmt_ctx, AVMEDIA_TYPE_AUDIO, -1, -1, NULL, 0); if (self->audio_stream_idx >= 0) self->audio_codec_ctx = open_decoder(retval, scm_file_name, audio_stream(self), "audio"); // Print debug information if (scm_is_true(scm_debug)) av_dump_format(self->fmt_ctx, 0, file_name, 0); // Allocate input frames self->video_target_frame = allocate_frame(retval); self->audio_target_frame = allocate_frame(retval); // Initialise data packet av_init_packet(&self->pkt); self->pkt.data = NULL; self->pkt.size = 0; scm_dynwind_end(); return retval; }
void calcDenseFlow(string file_name, int bound, int type, int step, vector<vector<uchar> >& output_x, vector<vector<uchar> >& output_y, vector<vector<uchar> >& output_img){ VideoCapture video_stream(file_name); CHECK(video_stream.isOpened())<<"Cannot open video stream \"" <<file_name <<"\" for optical flow extraction."; Mat capture_frame, capture_image, prev_image, capture_gray, prev_gray; Mat flow, flow_split[2]; bool initialized = false; for(int iter = 0;; iter++){ video_stream >> capture_frame; if (capture_frame.empty()) break; // read frames until end //build mats for the first frame if (!initialized){ initializeMats(capture_frame, capture_image, capture_gray, prev_image, prev_gray); capture_frame.copyTo(prev_image); cvtColor(prev_image, prev_gray, CV_BGR2GRAY); initialized = true; // LOG(INFO)<<"Initialized"; }else if(iter % step == 0){ capture_frame.copyTo(capture_image); cvtColor(capture_image, capture_gray, CV_BGR2GRAY); calcOpticalFlowFarneback(prev_gray, capture_gray, flow, 0.702, 5, 10, 2, 7, 1.5, cv::OPTFLOW_FARNEBACK_GAUSSIAN ); vector<uchar> str_x, str_y, str_img; split(flow, flow_split); encodeFlowMap(flow_split[0], flow_split[0], str_x, str_y, bound); imencode(".jpg", capture_image, str_img); output_x.push_back(str_x); output_y.push_back(str_y); output_img.push_back(str_img); // LOG(INFO)<<iter; std::swap(prev_gray, capture_gray); std::swap(prev_image, capture_image); } } }
static int encode_video(struct ffmpeg_t *self, AVFrame *video_frame) { AVCodecContext *codec = video_codec_ctx(self); // Initialise data packet AVPacket pkt = { 0 }; av_init_packet(&pkt); // Encode the video frame int got_packet; int err = avcodec_encode_video2(codec, &pkt, video_frame, &got_packet); if (err < 0) scm_misc_error("encode-video", "Error encoding video frame: ~a", scm_list_1(get_error_text(err))); // Write any new video packets if (got_packet) write_frame(self, &pkt, codec, video_stream(self), self->video_stream_idx); return got_packet; }
static SCM list_timestamped_video(struct ffmpeg_t *self, AVFrame *frame) { return scm_list_2(scm_from_locale_symbol("video"), scm_product(scm_from_int(frame_timestamp(frame)), time_base(video_stream(self)))); }
SCM ffmpeg_aspect_ratio(SCM scm_self) { AVRational aspect_ratio = video_stream(get_self(scm_self))->sample_aspect_ratio; return rational(aspect_ratio.num, aspect_ratio.den); }
SCM ffmpeg_frame_rate(SCM scm_self) { AVRational avg_frame_rate = video_stream(get_self(scm_self))->avg_frame_rate; return rational(avg_frame_rate.num, avg_frame_rate.den); }