Advertisement
Guest User

Untitled

a guest
Aug 9th, 2015
336
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 5.98 KB | None | 0 0
  1. #include "libavcodec/avcodec.h"
  2. #include "libavformat/avformat.h"
  3.  
  4. AVFormatContext *ctx;
  5. AVCodecContext *avctx;
  6. int videoStreamIndex;
  7.  
  8. static int loadFile(const char *file) {
  9.     int res;
  10.     AVCodec *codec;
  11.  
  12.     ctx = NULL;
  13.     res = avformat_open_input(&ctx, file, NULL, NULL);
  14.     if (res != 0)
  15.         return res;
  16.     res = avformat_find_stream_info(ctx, NULL);
  17.     if (res < 0)
  18.         return res;
  19.     res = av_find_best_stream(ctx, AVMEDIA_TYPE_VIDEO, -1, -1, &codec, 0);
  20.     if (res < 0)
  21.         return res;
  22.  
  23.     videoStreamIndex = res;
  24.     avctx = avcodec_alloc_context3(codec);
  25.     avcodec_copy_context(avctx, ctx->streams[videoStreamIndex]->codec);
  26.     res = avcodec_open2(avctx, codec, NULL);
  27.     if (res < 0) {
  28.         avcodec_free_context(&avctx);
  29.         return res;
  30.     }
  31.  
  32.     return 0;
  33. }
  34.  
  35. static int loadNextFrame(AVFrame *frame) {
  36.     AVPacket pkt;
  37.     int got, res;
  38.  
  39.     while (!av_read_frame(ctx, &pkt)) {
  40.         if (pkt.stream_index != videoStreamIndex) {
  41.             av_free_packet(&pkt);
  42.             continue;
  43.         }
  44.  
  45.         res = avcodec_decode_video2(avctx, frame, &got, &pkt);
  46.         av_free_packet(&pkt);
  47.         if (res < 0) {
  48.             return res;
  49.         } else if (got > 0) {
  50.             return 0;
  51.         }
  52.     }
  53.  
  54.     for (;;) {
  55.         pkt.data = NULL;
  56.         pkt.size = 0;
  57.         res = avcodec_decode_video2(avctx, frame, &got, &pkt);
  58.         if (res < 0) {
  59.             return res;
  60.         } else if (got > 0) {
  61.             return 0;
  62.         } else {
  63.             break;
  64.         }
  65.     }
  66.  
  67.     return AVERROR_EOF;
  68. }
  69.  
  70. static void closeFile() {
  71.     if (ctx) {
  72.         if (avctx) {
  73.             avcodec_close(avctx);
  74.             avcodec_free_context(&avctx);
  75.         }
  76.         avformat_close_input(&ctx);
  77.     }
  78. }
  79.  
  80. #if 1
  81. int main(int argc, char *argv[]) {
  82.     int res;
  83.     AVFrame *inFrame = av_frame_alloc();
  84.     AVPicture out;
  85.  
  86.     if (argc != 2) {
  87.         printf("Usage: %s <file>\n", argv[0]);
  88.         return -1;
  89.     }
  90.     av_register_all();
  91.     res = loadFile(argv[1]);
  92.     if (res < 0)
  93.         return res;
  94.     avpicture_alloc(&out, avctx->pix_fmt, avctx->width, avctx->height);
  95.  
  96.     while (1) {
  97.         res = loadNextFrame(inFrame);
  98.         if (res < 0)
  99.             return res;
  100.  
  101.         res = avpicture_deinterlace(&out, (const AVPicture *) inFrame,
  102.                                     inFrame->format, inFrame->width, inFrame->height);
  103.         if (res < 0)
  104.             return res;
  105.  
  106.         // do stuff
  107.         printf("Filtered one frame\n");
  108.     }
  109.  
  110.     av_frame_free(&inFrame);
  111.     avpicture_free(&out);
  112.     closeFile();
  113.  
  114.     return 0;
  115. }
  116. #else
  117. #include <assert.h>
  118. #include "libavfilter/avfilter.h"
  119. #include "libavfilter/buffersrc.h"
  120. #include "libavfilter/buffersink.h"
  121.  
  122. AVFilterContext *buffersink_ctx;
  123. AVFilterContext *buffersrc_ctx;
  124. AVFilterGraph *filter_graph;
  125. AVFrame *frame;
  126. int last_width = -1, last_height = -1;
  127. enum AVPixelFormat last_pixfmt = AV_PIX_FMT_NONE;
  128.  
  129. static void delete_filter_graph() {
  130.     if (filter_graph) {
  131.         av_frame_free(& frame);
  132.         avfilter_graph_free(&filter_graph);
  133.     }
  134. }
  135.  
  136. static int init_filter_graph(enum AVPixelFormat pixfmt, int width, int height) {
  137.     AVFilterInOut *inputs = NULL, *outputs = NULL;
  138.     char args[512];
  139.     int res;
  140.  
  141.     delete_filter_graph();
  142.     filter_graph = avfilter_graph_alloc();
  143.     snprintf(args, sizeof(args),
  144.              "buffer=video_size=%dx%d:pix_fmt=%d:time_base=1/1:pixel_aspect=0/1[in];"
  145.              "[in]yadif[out];"
  146.              "[out]buffersink",
  147.              width, height, pixfmt);
  148.     res = avfilter_graph_parse2(filter_graph, args, &inputs, &outputs);
  149.     if (res < 0)
  150.         return res;
  151.     assert(inputs == NULL && outputs == NULL);
  152.     res = avfilter_graph_config(filter_graph, NULL);
  153.     if (res < 0)
  154.         return res;
  155.  
  156.     buffersrc_ctx = avfilter_graph_get_filter(filter_graph, "Parsed_buffer_0");
  157.     buffersink_ctx = avfilter_graph_get_filter(filter_graph, "Parsed_buffersink_2");
  158.     assert(buffersrc_ctx != NULL);
  159.     assert(buffersink_ctx != NULL);
  160.     frame = av_frame_alloc();
  161.     last_width = width;
  162.     last_height = height;
  163.     last_pixfmt = pixfmt;
  164.  
  165.     return 0;
  166. }
  167.  
  168. static int process_filter_graph(AVPicture *dst, const AVPicture *src,
  169.                                 enum AVPixelFormat pixfmt, int width, int height) {
  170.     int res;
  171.  
  172.     if (!filter_graph || width != last_width ||
  173.         height != last_height || pixfmt != last_pixfmt) {
  174.         res = init_filter_graph(pixfmt, width, height);
  175.         if (res < 0)
  176.             return res;
  177.     }
  178.  
  179.     memcpy(frame->data, src->data, sizeof(src->data));
  180.     memcpy(frame->linesize, src->linesize, sizeof(src->linesize));
  181.     frame->width = width;
  182.     frame->height = height;
  183.     frame->format = pixfmt;
  184.     res = av_buffersrc_add_frame(buffersrc_ctx, frame);
  185.     if (res < 0)
  186.         return res;
  187.     res = av_buffersink_get_frame(buffersink_ctx, frame);
  188.     if (res < 0)
  189.         return res;
  190.     av_picture_copy(dst, (const AVPicture *) frame, pixfmt, width, height);
  191.     av_frame_unref(frame);
  192.  
  193.     return 0;
  194. }
  195.  
  196. int main(int argc, char *argv[]) {
  197.     int res;
  198.     AVFrame *inFrame = av_frame_alloc();
  199.     AVPicture out;
  200.  
  201.     if (argc != 2) {
  202.         printf("Usage: %s <file>\n", argv[0]);
  203.         return -1;
  204.     }
  205.     av_register_all();
  206.     avfilter_register_all();
  207.     res = loadFile(argv[1]);
  208.     if (res < 0)
  209.         return res;
  210.     avpicture_alloc(&out, avctx->pix_fmt, avctx->width, avctx->height);
  211.  
  212.     while (1) {
  213.         res = loadNextFrame(inFrame);
  214.         if (res < 0)
  215.             return res;
  216.  
  217.         res = process_filter_graph(&out, (const AVPicture *) inFrame,
  218.                                    inFrame->format, inFrame->width, inFrame->height);
  219.         if (res < 0)
  220.             continue;
  221.  
  222.         // do stuff
  223.         printf("Filtered one frame\n");
  224.     }
  225.     delete_filter_graph();
  226.     av_frame_free(&inFrame);
  227.     avpicture_free(&out);
  228.     closeFile();
  229.  
  230.     return 0;
  231. }
  232. #endif
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement