Revert "Use make_shared<>."
[dcpomatic.git] / src / lib / video_filter_graph.cc
1 /*
2     Copyright (C) 2012-2015 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 #include "video_filter_graph.h"
22 #include "image.h"
23 #include "compose.hpp"
24 extern "C" {
25 #include <libavfilter/buffersrc.h>
26 #include <libavfilter/buffersink.h>
27 }
28
29 #include "i18n.h"
30
31 using std::list;
32 using std::pair;
33 using std::vector;
34 using std::string;
35 using std::make_pair;
36 using boost::shared_ptr;
37
38 VideoFilterGraph::VideoFilterGraph (dcp::Size s, AVPixelFormat p)
39         : _size (s)
40         , _pixel_format (p)
41 {
42
43 }
44
45 /** Take an AVFrame and process it using our configured filters, returning a
46  *  set of Images.  Caller handles memory management of the input frame.
47  */
48 list<pair<shared_ptr<Image>, int64_t> >
49 VideoFilterGraph::process (AVFrame* frame)
50 {
51         list<pair<shared_ptr<Image>, int64_t> > images;
52
53         if (_copy) {
54                 images.push_back (make_pair (shared_ptr<Image> (new Image (frame)), av_frame_get_best_effort_timestamp (frame)));
55         } else {
56                 int r = av_buffersrc_write_frame (_buffer_src_context, frame);
57                 if (r < 0) {
58                         throw DecodeError (String::compose (N_("could not push buffer into filter chain (%1)."), r));
59                 }
60
61                 while (true) {
62                         if (av_buffersink_get_frame (_buffer_sink_context, _frame) < 0) {
63                                 break;
64                         }
65
66                         images.push_back (make_pair (shared_ptr<Image> (new Image (_frame)), av_frame_get_best_effort_timestamp (_frame)));
67                         av_frame_unref (_frame);
68                 }
69         }
70
71         return images;
72 }
73
74 /** @param s Image size.
75  *  @param p Pixel format.
76  *  @return true if this chain can process images with `s' and `p', otherwise false.
77  */
78 bool
79 VideoFilterGraph::can_process (dcp::Size s, AVPixelFormat p) const
80 {
81         return (_size == s && _pixel_format == p);
82 }
83
84 string
85 VideoFilterGraph::src_parameters () const
86 {
87         SafeStringStream a;
88
89         a << "video_size=" << _size.width << "x" << _size.height << ":"
90           << "pix_fmt=" << _pixel_format << ":"
91           << "time_base=1/1:"
92           << "pixel_aspect=1/1";
93
94         return a.str ();
95 }
96
97 void *
98 VideoFilterGraph::sink_parameters () const
99 {
100         AVBufferSinkParams* sink_params = av_buffersink_params_alloc ();
101         AVPixelFormat* pixel_fmts = new AVPixelFormat[2];
102         pixel_fmts = new AVPixelFormat[2];
103         pixel_fmts[0] = _pixel_format;
104         pixel_fmts[1] = AV_PIX_FMT_NONE;
105         sink_params->pixel_fmts = pixel_fmts;
106         return sink_params;
107 }
108
109 string
110 VideoFilterGraph::src_name () const
111 {
112         return "buffer";
113 }
114
115 string
116 VideoFilterGraph::sink_name () const
117 {
118         return "buffersink";
119 }
120