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