Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "libavcodec/avcodec.h"
- #include "libavformat/avformat.h"
- AVFormatContext *ctx;
- AVCodecContext *avctx;
- int videoStreamIndex;
- static int loadFile(const char *file) {
- int res;
- AVCodec *codec;
- ctx = NULL;
- res = avformat_open_input(&ctx, file, NULL, NULL);
- if (res != 0)
- return res;
- res = avformat_find_stream_info(ctx, NULL);
- if (res < 0)
- return res;
- res = av_find_best_stream(ctx, AVMEDIA_TYPE_VIDEO, -1, -1, &codec, 0);
- if (res < 0)
- return res;
- videoStreamIndex = res;
- avctx = avcodec_alloc_context3(codec);
- avcodec_copy_context(avctx, ctx->streams[videoStreamIndex]->codec);
- res = avcodec_open2(avctx, codec, NULL);
- if (res < 0) {
- avcodec_free_context(&avctx);
- return res;
- }
- return 0;
- }
- static int loadNextFrame(AVFrame *frame) {
- AVPacket pkt;
- int got, res;
- while (!av_read_frame(ctx, &pkt)) {
- if (pkt.stream_index != videoStreamIndex) {
- av_free_packet(&pkt);
- continue;
- }
- res = avcodec_decode_video2(avctx, frame, &got, &pkt);
- av_free_packet(&pkt);
- if (res < 0) {
- return res;
- } else if (got > 0) {
- return 0;
- }
- }
- for (;;) {
- pkt.data = NULL;
- pkt.size = 0;
- res = avcodec_decode_video2(avctx, frame, &got, &pkt);
- if (res < 0) {
- return res;
- } else if (got > 0) {
- return 0;
- } else {
- break;
- }
- }
- return AVERROR_EOF;
- }
- static void closeFile() {
- if (ctx) {
- if (avctx) {
- avcodec_close(avctx);
- avcodec_free_context(&avctx);
- }
- avformat_close_input(&ctx);
- }
- }
- #if 1
- int main(int argc, char *argv[]) {
- int res;
- AVFrame *inFrame = av_frame_alloc();
- AVPicture out;
- if (argc != 2) {
- printf("Usage: %s <file>\n", argv[0]);
- return -1;
- }
- av_register_all();
- res = loadFile(argv[1]);
- if (res < 0)
- return res;
- avpicture_alloc(&out, avctx->pix_fmt, avctx->width, avctx->height);
- while (1) {
- res = loadNextFrame(inFrame);
- if (res < 0)
- return res;
- res = avpicture_deinterlace(&out, (const AVPicture *) inFrame,
- inFrame->format, inFrame->width, inFrame->height);
- if (res < 0)
- return res;
- // do stuff
- printf("Filtered one frame\n");
- }
- av_frame_free(&inFrame);
- avpicture_free(&out);
- closeFile();
- return 0;
- }
- #else
- #include <assert.h>
- #include "libavfilter/avfilter.h"
- #include "libavfilter/buffersrc.h"
- #include "libavfilter/buffersink.h"
- AVFilterContext *buffersink_ctx;
- AVFilterContext *buffersrc_ctx;
- AVFilterGraph *filter_graph;
- AVFrame *frame;
- int last_width = -1, last_height = -1;
- enum AVPixelFormat last_pixfmt = AV_PIX_FMT_NONE;
- static void delete_filter_graph() {
- if (filter_graph) {
- av_frame_free(& frame);
- avfilter_graph_free(&filter_graph);
- }
- }
- static int init_filter_graph(enum AVPixelFormat pixfmt, int width, int height) {
- AVFilterInOut *inputs = NULL, *outputs = NULL;
- char args[512];
- int res;
- delete_filter_graph();
- filter_graph = avfilter_graph_alloc();
- snprintf(args, sizeof(args),
- "buffer=video_size=%dx%d:pix_fmt=%d:time_base=1/1:pixel_aspect=0/1[in];"
- "[in]yadif[out];"
- "[out]buffersink",
- width, height, pixfmt);
- res = avfilter_graph_parse2(filter_graph, args, &inputs, &outputs);
- if (res < 0)
- return res;
- assert(inputs == NULL && outputs == NULL);
- res = avfilter_graph_config(filter_graph, NULL);
- if (res < 0)
- return res;
- buffersrc_ctx = avfilter_graph_get_filter(filter_graph, "Parsed_buffer_0");
- buffersink_ctx = avfilter_graph_get_filter(filter_graph, "Parsed_buffersink_2");
- assert(buffersrc_ctx != NULL);
- assert(buffersink_ctx != NULL);
- frame = av_frame_alloc();
- last_width = width;
- last_height = height;
- last_pixfmt = pixfmt;
- return 0;
- }
- static int process_filter_graph(AVPicture *dst, const AVPicture *src,
- enum AVPixelFormat pixfmt, int width, int height) {
- int res;
- if (!filter_graph || width != last_width ||
- height != last_height || pixfmt != last_pixfmt) {
- res = init_filter_graph(pixfmt, width, height);
- if (res < 0)
- return res;
- }
- memcpy(frame->data, src->data, sizeof(src->data));
- memcpy(frame->linesize, src->linesize, sizeof(src->linesize));
- frame->width = width;
- frame->height = height;
- frame->format = pixfmt;
- res = av_buffersrc_add_frame(buffersrc_ctx, frame);
- if (res < 0)
- return res;
- res = av_buffersink_get_frame(buffersink_ctx, frame);
- if (res < 0)
- return res;
- av_picture_copy(dst, (const AVPicture *) frame, pixfmt, width, height);
- av_frame_unref(frame);
- return 0;
- }
- int main(int argc, char *argv[]) {
- int res;
- AVFrame *inFrame = av_frame_alloc();
- AVPicture out;
- if (argc != 2) {
- printf("Usage: %s <file>\n", argv[0]);
- return -1;
- }
- av_register_all();
- avfilter_register_all();
- res = loadFile(argv[1]);
- if (res < 0)
- return res;
- avpicture_alloc(&out, avctx->pix_fmt, avctx->width, avctx->height);
- while (1) {
- res = loadNextFrame(inFrame);
- if (res < 0)
- return res;
- res = process_filter_graph(&out, (const AVPicture *) inFrame,
- inFrame->format, inFrame->width, inFrame->height);
- if (res < 0)
- continue;
- // do stuff
- printf("Filtered one frame\n");
- }
- delete_filter_graph();
- av_frame_free(&inFrame);
- avpicture_free(&out);
- closeFile();
- return 0;
- }
- #endif
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement