Remove some FFmpeg-related warnings by directly accessing AVFrame.
[dcpomatic.git] / src / lib / video_filter_graph.cc
1 /*
2     Copyright (C) 2012-2021 Carl Hetherington <cth@carlh.net>
3
4     This file is part of DCP-o-matic.
5
6     DCP-o-matic is free software; you can redistribute it and/or modify
7     it under the terms of the GNU General Public License as published by
8     the Free Software Foundation; either version 2 of the License, or
9     (at your option) any later version.
10
11     DCP-o-matic is distributed in the hope that it will be useful,
12     but WITHOUT ANY WARRANTY; without even the implied warranty of
13     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14     GNU General Public License for more details.
15
16     You should have received a copy of the GNU General Public License
17     along with DCP-o-matic.  If not, see <http://www.gnu.org/licenses/>.
18
19 */
20
21
22 #include "compose.hpp"
23 #include "image.h"
24 #include "video_filter_graph.h"
25 #include "warnings.h"
26 extern "C" {
27 #include <libavfilter/buffersrc.h>
28 #include <libavfilter/buffersink.h>
29 }
30
31 #include "i18n.h"
32
33
34 using std::list;
35 using std::make_pair;
36 using std::make_shared;
37 using std::pair;
38 using std::shared_ptr;
39 using std::string;
40 using std::vector;
41
42
43 VideoFilterGraph::VideoFilterGraph (dcp::Size s, AVPixelFormat p, dcp::Fraction r)
44         : _size (s)
45         , _pixel_format (p)
46         , _frame_rate (r)
47 {
48
49 }
50
51
52 /** Take an AVFrame and process it using our configured filters, returning a
53  *  set of Images.  Caller handles memory management of the input frame.
54  */
55 list<pair<shared_ptr<Image>, int64_t>>
56 VideoFilterGraph::process (AVFrame* frame)
57 {
58         list<pair<shared_ptr<Image>, int64_t>> images;
59
60         if (_copy) {
61                 images.push_back (make_pair(make_shared<Image>(frame), frame->best_effort_timestamp));
62         } else {
63                 int r = av_buffersrc_write_frame (_buffer_src_context, frame);
64                 if (r < 0) {
65                         throw DecodeError (String::compose(N_("could not push buffer into filter chain (%1)."), r));
66                 }
67
68                 while (true) {
69                         if (av_buffersink_get_frame (_buffer_sink_context, _frame) < 0) {
70                                 break;
71                         }
72
73                         images.push_back (make_pair(make_shared<Image>(_frame), _frame->best_effort_timestamp));
74                         av_frame_unref (_frame);
75                 }
76         }
77
78         return images;
79 }
80
81
82 /** @param s Image size.
83  *  @param p Pixel format.
84  *  @return true if this chain can process images with `s' and `p', otherwise false.
85  */
86 bool
87 VideoFilterGraph::can_process (dcp::Size s, AVPixelFormat p) const
88 {
89         return (_size == s && _pixel_format == p);
90 }
91
92
93 string
94 VideoFilterGraph::src_parameters () const
95 {
96         char buffer[256];
97         snprintf (
98                 buffer, sizeof(buffer),
99                 "video_size=%dx%d:pix_fmt=%d:frame_rate=%d/%d:time_base=1/1:pixel_aspect=1/1",
100                 _size.width, _size.height,
101                 _pixel_format,
102                 _frame_rate.numerator, _frame_rate.denominator
103                 );
104         return buffer;
105 }
106
107
108 void *
109 VideoFilterGraph::sink_parameters () const
110 {
111         auto sink_params = av_buffersink_params_alloc ();
112         auto pixel_fmts = new AVPixelFormat[2];
113         pixel_fmts[0] = _pixel_format;
114         pixel_fmts[1] = AV_PIX_FMT_NONE;
115         sink_params->pixel_fmts = pixel_fmts;
116         return sink_params;
117 }
118
119
120 string
121 VideoFilterGraph::src_name () const
122 {
123         return "buffer";
124 }
125
126
127 string
128 VideoFilterGraph::sink_name () const
129 {
130         return "buffersink";
131 }