Statistics
| Branch: | Revision:

chunker-player / chunker_streamer / chunker_filtering.c @ 51d67a10

History | View | Annotate | Download (4.27 KB)

1
/*
2
 * Copyright (c) 2010 Nicolas George
3
 * Copyright (c) 2011 Stefano Sabatini
4
 * Copyright (c) 2011 Csaba Kiraly
5
 *
6
 * Permission is hereby granted, free of charge, to any person obtaining a copy
7
 * of this software and associated documentation files (the "Software"), to deal
8
 * in the Software without restriction, including without limitation the rights
9
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10
 * copies of the Software, and to permit persons to whom the Software is
11
 * furnished to do so, subject to the following conditions:
12
 *
13
 * The above copyright notice and this permission notice shall be included in
14
 * all copies or substantial portions of the Software.
15
 *
16
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19
 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22
 * THE SOFTWARE.
23
 */
24

    
25
/**
26
 * @file
27
 * decoding and filtering, based on ffmpeg's avfiler example
28
 */
29

    
30
#ifdef USE_AVFILTER
31

    
32
#include <libavcodec/avcodec.h>
33
#include <libavformat/avformat.h>
34
#include <libavfilter/avfiltergraph.h>
35
#include <libavfilter/vsink_buffer.h>
36
#include <libavfilter/vsrc_buffer.h>
37

    
38
#include <libavfilter/avcodec.h>        //might not be needed in newer versions of ffmpeg
39

    
40
#include "chunker_filtering.h"
41

    
42
AVFilterContext *buffersink_ctx;
43
AVFilterContext *buffersrc_ctx;
44
AVFilterGraph *filter_graph;
45

    
46
int init_filters(const char *filters_descr, const AVCodecContext *dec_ctx)
47
{
48
    char args[512];
49
    int ret;
50
    AVFilter *buffersrc  = avfilter_get_by_name("buffer");
51
    AVFilter *buffersink = avfilter_get_by_name("buffersink");
52
    AVFilterInOut *outputs = avfilter_inout_alloc();
53
    AVFilterInOut *inputs  = avfilter_inout_alloc();
54
    enum PixelFormat pix_fmts[] = { PIX_FMT_YUV420P, PIX_FMT_GRAY8, PIX_FMT_NONE };
55
    filter_graph = avfilter_graph_alloc();
56

    
57
    /* buffer video source: the decoded frames from the decoder will be inserted here. */
58
    snprintf(args, sizeof(args), "%d:%d:%d:%d:%d:%d:%d",
59
             dec_ctx->width, dec_ctx->height, dec_ctx->pix_fmt,
60
             dec_ctx->time_base.num, dec_ctx->time_base.den,
61
             dec_ctx->sample_aspect_ratio.num, dec_ctx->sample_aspect_ratio.den);
62
    ret = avfilter_graph_create_filter(&buffersrc_ctx, buffersrc, "in",
63
                                       args, NULL, filter_graph);
64
    if (ret < 0) {
65
        av_log(NULL, AV_LOG_ERROR, "Cannot create buffer source\n");
66
        return ret;
67
    }
68

    
69
    /* buffer video sink: to terminate the filter chain. */
70
    ret = avfilter_graph_create_filter(&buffersink_ctx, buffersink, "out",
71
                                       NULL, pix_fmts, filter_graph);
72
    if (ret < 0) {
73
        av_log(NULL, AV_LOG_ERROR, "Cannot create buffer sink\n");
74
        return ret;
75
    }
76

    
77
    /* Endpoints for the filter graph. */
78
    outputs->name       = av_strdup("in");
79
    outputs->filter_ctx = buffersrc_ctx;
80
    outputs->pad_idx    = 0;
81
    outputs->next       = NULL;
82

    
83
    inputs->name       = av_strdup("out");
84
    inputs->filter_ctx = buffersink_ctx;
85
    inputs->pad_idx    = 0;
86
    inputs->next       = NULL;
87

    
88
    if ((ret = avfilter_graph_parse(filter_graph, filters_descr,
89
                                    &inputs, &outputs, NULL)) < 0)
90
        return ret;
91

    
92
    if ((ret = avfilter_graph_config(filter_graph, NULL)) < 0)
93
        return ret;
94
}
95

    
96
int filter(AVFrame* src, AVFrame *dst)
97
{
98
    int ret;
99
    AVFilterBufferRef *picref;
100

    
101
    /* push the decoded frame into the filtergraph. 3rd parameter might not be needed with new ffmpeg */
102
    av_vsrc_buffer_add_frame(buffersrc_ctx, src, 0);
103

    
104
    /* pull filtered pictures from the filtergraph */
105
    ret = avfilter_poll_frame(buffersink_ctx->inputs[0]);
106
    if (ret) {
107
        av_vsink_buffer_get_video_buffer_ref(buffersink_ctx, &picref, 0);
108
        if (picref) {
109
            avfilter_fill_frame_from_video_buffer_ref(dst, picref);
110
            dst->pts = picref->pts;
111
            avfilter_unref_buffer(picref);
112
        }
113
    }
114

    
115
    return ret;
116
}
117

    
118
void close_filters(void)
119
{
120
    avfilter_graph_free(&filter_graph);
121
}
122

    
123
#endif