Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- extern "C" {
- #include <libavcodec/avcodec.h>
- #include <libavformat/avformat.h>
- #include <libavfilter/avfiltergraph.h>
- #include <libavfilter/buffersink.h>
- #include <libavfilter/buffersrc.h>
- #include <libavutil/opt.h>
- #include <libavutil/pixdesc.h>
- #include <libavdevice/avdevice.h>
- #include <libavutil/time.h>
- };
- #include <iostream>
- #ifdef __cplusplus
- static const std::string av_make_error_string(int errnum) {
- char errbuf[AV_ERROR_MAX_STRING_SIZE];
- av_strerror(errnum, errbuf, AV_ERROR_MAX_STRING_SIZE);
- return (std::string)errbuf;
- }
- #undef av_err2str
- #define av_err2str(errnum) av_make_error_string(errnum).c_str()
- #endif // __cplusplus
- #define INPUT_SOURCE "video=Integrated Webcam"
- #define OUTPUT_SOURCE "udp://127.0.0.1:8888"
- #define INPUT_FORMAT av_find_input_format("dshow")
- int videoindex = 0;
- AVFormatContext *inputFormatContext = avformat_alloc_context();
- AVFormatContext *outputFormatContext = avformat_alloc_context();
- //AVInputFormat *inputFormat = av_find_input_format("dshow");
- typedef struct FilteringContext {
- AVFilterContext *buffersink_ctx;
- AVFilterContext *buffersrc_ctx;
- AVFilterGraph *filter_graph;
- } FilteringContext;
- static FilteringContext *filter_ctx;
- static int open_input_file(const char *filename)
- {
- int ret;
- unsigned int i;
- if ((ret = avformat_open_input(&inputFormatContext, INPUT_SOURCE, INPUT_FORMAT, NULL)) < 0) {
- av_log(NULL, AV_LOG_ERROR, "Cannot open input file\n");
- return ret;
- }
- if ((ret = avformat_find_stream_info(inputFormatContext, NULL)) < 0) {
- av_log(NULL, AV_LOG_ERROR, "Cannot find stream information\n");
- return ret;
- }
- for (i = 0; i < inputFormatContext->nb_streams; i++) {
- AVStream *stream;
- AVCodecContext *codecContext;
- stream = inputFormatContext->streams[i];
- codecContext = stream->codec;
- /* Reencode video & audio and remux subtitles etc. */
- if (codecContext->codec_type == AVMEDIA_TYPE_VIDEO
- || codecContext->codec_type == AVMEDIA_TYPE_AUDIO) {
- /* Open decoder */
- ret = avcodec_open2(codecContext,
- avcodec_find_decoder(codecContext->codec_id), NULL);
- if (ret < 0) {
- av_log(NULL, AV_LOG_ERROR, "Failed to open decoder for stream #%u\n", i);
- return ret;
- }
- }
- if (inputFormatContext->streams[i]->codec->codec_type == AVMEDIA_TYPE_VIDEO
- || inputFormatContext->streams[i]->codec->codec_type == AVMEDIA_TYPE_AUDIO) {
- videoindex = i;
- break;
- }
- }
- av_dump_format(inputFormatContext, 0, INPUT_SOURCE, 0);
- return 0;
- }
- static int open_output_file(const char *filename) {
- AVCodecContext *decoderContext, *encoderContext;
- AVCodec *encoder;
- int ret;
- unsigned int i;
- avformat_alloc_output_context2(&outputFormatContext, NULL, "mpegts", OUTPUT_SOURCE);
- if (!outputFormatContext) {
- av_log(NULL, AV_LOG_ERROR, "Could not create output context\n");
- return AVERROR_UNKNOWN;
- }
- for (i = 0; i < inputFormatContext->nb_streams; i++) {
- AVStream *streamIn = inputFormatContext->streams[i];
- AVStream *streamOut = avformat_new_stream(outputFormatContext, streamIn->codec->codec);
- if (!streamOut) {
- av_log(NULL, AV_LOG_ERROR, "Failed allocating output stream\n");
- return AVERROR_UNKNOWN;
- }
- ret = avcodec_copy_context(streamOut->codec, streamIn->codec);
- if (ret < 0) {
- av_log(NULL, AV_LOG_ERROR, "Failed to copy context from input to output stream codec context\n");
- return AVERROR_UNKNOWN;
- }
- streamOut->codec->codec_tag = 0;
- if (outputFormatContext->oformat->flags & AVFMT_GLOBALHEADER)
- streamOut->codec->flags |= CODEC_FLAG_GLOBAL_HEADER;
- }
- av_dump_format(outputFormatContext, 0, OUTPUT_SOURCE, 1);
- if (!(outputFormatContext->oformat->flags & AVFMT_NOFILE)) {
- ret = avio_open(&outputFormatContext->pb, OUTPUT_SOURCE, AVIO_FLAG_WRITE);
- if (ret < 0) {
- av_log(NULL, AV_LOG_ERROR, "Could not open output file '%s'", filename);
- return ret;
- }
- }
- return 0;
- }
- int main(int argc, char **argv)
- {
- av_register_all();
- avformat_network_init();
- avdevice_register_all();
- avcodec_register_all();
- avfilter_register_all();
- int ret, start_time, frame_index = 0;
- AVPacket packet;// = { .data = NULL,.size = 0 };
- packet.data = NULL;
- packet.size = 0;
- AVFrame *frame = NULL;
- enum AVMediaType type;
- unsigned int stream_index;
- unsigned int i;
- int got_frame;
- int(*dec_func)(AVCodecContext *, AVFrame *, int *, const AVPacket *);
- open_input_file(INPUT_SOURCE);
- open_output_file(OUTPUT_SOURCE);
- if ((ret = avformat_write_header(outputFormatContext, NULL)) < 0) {
- av_log(NULL, AV_LOG_DEBUG, "Error occurred when opening output URL\n");
- return ret;
- }
- start_time = av_gettime();
- /* read all packets */
- while (1) {
- AVStream *streamIn, *streamOut;
- if ((ret = av_read_frame(inputFormatContext, &packet)) < 0)
- break;
- if (packet.pts == AV_NOPTS_VALUE) {
- AVRational time_base_aux = inputFormatContext->streams[videoindex]->time_base;
- // Duration between 2 frames(us)
- int64_t calc_duration = (double)AV_TIME_BASE / av_q2d(inputFormatContext->streams[videoindex]->r_frame_rate);
- //Parameters
- packet.pts = (double)(frame_index*calc_duration) / (double)(av_q2d(time_base_aux)*AV_TIME_BASE);
- packet.dts = packet.pts;
- packet.duration = (double)calc_duration / (double)(av_q2d(time_base_aux)*AV_TIME_BASE);
- }
- if (packet.stream_index == videoindex) {
- AVRational time_base = inputFormatContext->streams[videoindex]->time_base;
- AVRational time_base_q = { 1,AV_TIME_BASE };
- int64_t pts_time = av_rescale_q(packet.dts, time_base, time_base_q);
- int64_t now_time = av_gettime() - start_time;
- if (pts_time > now_time)
- av_usleep(pts_time - now_time);
- }
- streamIn = inputFormatContext->streams[packet.stream_index];
- streamOut = outputFormatContext->streams[packet.stream_index];
- /* copy packet */
- //Convert PTS/DTS
- packet.pts = av_rescale_q_rnd(packet.pts, streamIn->time_base, streamOut->time_base, (AVRounding)(AV_ROUND_NEAR_INF | AV_ROUND_PASS_MINMAX));
- packet.dts = av_rescale_q_rnd(packet.dts, streamIn->time_base, streamOut->time_base, (AVRounding)(AV_ROUND_NEAR_INF | AV_ROUND_PASS_MINMAX));
- packet.duration = av_rescale_q(packet.duration, streamIn->time_base, streamOut->time_base);
- packet.pos = -1;
- //Print to Screen
- if (packet.stream_index == videoindex) {
- //printf("Send %8d video frames to output URL\n", frame_index);
- frame_index++;
- }
- //ret = av_write_frame(ofmt_ctx, &pkt);
- ret = av_interleaved_write_frame(outputFormatContext, &packet);
- if (ret < 0) {
- printf("Error muxing packet\n");
- break;
- }
- }
- av_write_trailer(outputFormatContext);
- end:
- av_packet_unref(&packet);
- av_frame_free(&frame);
- for (i = 0; i < inputFormatContext->nb_streams; i++) {
- avcodec_close(inputFormatContext->streams[i]->codec);
- if (outputFormatContext && outputFormatContext->nb_streams > i && outputFormatContext->streams[i] && outputFormatContext->streams[i]->codec)
- avcodec_close(outputFormatContext->streams[i]->codec);
- if (filter_ctx && filter_ctx[i].filter_graph)
- avfilter_graph_free(&filter_ctx[i].filter_graph);
- }
- av_free(filter_ctx);
- avformat_close_input(&inputFormatContext);
- if (outputFormatContext && !(outputFormatContext->oformat->flags & AVFMT_NOFILE))
- avio_closep(&outputFormatContext->pb);
- avformat_free_context(outputFormatContext);
- if (ret < 0)
- av_log(NULL, AV_LOG_ERROR, "Error occurred: %s\n", av_err2str(ret));
- return ret ? 1 : 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement