1 /**
2  * MltPushConsumer.cpp - MLT Wrapper
3  * Copyright (C) 2004-2015 Meltytech, LLC
4  * Author: Charles Yates <charles.yates@gmail.com>
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or (at your option) any later version.
10  *
11  * This library 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 GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
19  */
20 
21 #include "MltPushConsumer.h"
22 #include "MltFilter.h"
23 using namespace Mlt;
24 
25 namespace Mlt
26 {
27 	class PushPrivate
28 	{
29 		public:
PushPrivate()30 			PushPrivate( )
31 			{
32 			}
33 	};
34 }
35 
filter_destructor(void * arg)36 static void filter_destructor( void *arg )
37 {
38 	Filter *filter = ( Filter * )arg;
39 	delete filter;
40 }
41 
PushConsumer(Profile & profile,const char * id,const char * service)42 PushConsumer::PushConsumer( Profile& profile, const char *id , const char *service ) :
43 	Consumer( profile, id, service ),
44 	m_private( new PushPrivate( ) )
45 {
46 	if ( is_valid( ) )
47 	{
48 		// Set up push mode (known as put mode in mlt)
49 		set( "real_time", 0 );
50 		set( "put_mode", 1 );
51 		set( "terminate_on_pause", 0 );
52 		set( "buffer", 0 );
53 
54 		// We might need resize and rescale filters so we'll create them now
55 		// NB: Try to use the best rescaler available here
56 		Filter *resize = new Filter( profile, "resize" );
57 		Filter *rescale = new Filter( profile, "mcrescale" );
58 		if ( !rescale->is_valid( ) )
59 		{
60 			delete rescale;
61 			rescale = new Filter( profile, "gtkrescale" );
62 		}
63 		if ( !rescale->is_valid( ) )
64 		{
65 			delete rescale;
66 			rescale = new Filter( profile, "rescale" );
67 		}
68 
69 		Filter *convert = new Filter( profile, "avcolour_space" );
70 
71 		set( "filter_convert", convert, 0, filter_destructor );
72 		set( "filter_resize", resize, 0, filter_destructor );
73 		set( "filter_rescale", rescale, 0, filter_destructor );
74 	}
75 }
76 
~PushConsumer()77 PushConsumer::~PushConsumer( )
78 {
79 }
80 
set_render(int width,int height,double aspect_ratio)81 void PushConsumer::set_render( int width, int height, double aspect_ratio )
82 {
83 	set( "render_width", width );
84 	set( "render_height", height );
85 	set( "render_aspect_ratio", aspect_ratio );
86 }
87 
connect(Service &)88 int PushConsumer::connect( Service &/*service*/ )
89 {
90 	return -1;
91 }
92 
push(Frame * frame)93 int PushConsumer::push( Frame *frame )
94 {
95 	frame->inc_ref( );
96 
97 	// Here we have the option to process the frame at a render resolution (this will
98 	// typically be PAL or NTSC) prior to scaling according to the consumers profile
99 	// This is done to optimise quality, esp. with regard to compositing positions
100 	if ( get_int( "render_width" ) )
101 	{
102 		// Process the projects render resolution first
103 		mlt_image_format format = mlt_image_yuv422;
104 		int w = get_int( "render_width" );
105 		int h = get_int( "render_height" );
106 		frame->set( "consumer_aspect_ratio", get_double( "render_aspect_ratio" ) );
107 		frame->set( "consumer_deinterlace", get_int( "deinterlace" ) );
108 		frame->set( "deinterlace_method", get_int( "deinterlace_method" ) );
109 		frame->set( "rescale.interp", get( "rescale" ) );
110 
111 		// Render the frame
112 		frame->get_image( format, w, h );
113 
114 		// Now set up the post image scaling
115 		Filter *convert = ( Filter * )get_data( "filter_convert" );
116 		mlt_filter_process( convert->get_filter( ), frame->get_frame( ) );
117 		Filter *rescale = ( Filter * )get_data( "filter_rescale" );
118 		mlt_filter_process( rescale->get_filter( ), frame->get_frame( ) );
119 		Filter *resize = ( Filter * )get_data( "filter_resize" );
120 		mlt_filter_process( resize->get_filter( ), frame->get_frame( ) );
121 	}
122 
123 	return mlt_consumer_put_frame( ( mlt_consumer )get_service( ), frame->get_frame( ) );
124 }
125 
push(Frame & frame)126 int PushConsumer::push( Frame &frame )
127 {
128 	return push( &frame );
129 }
130 
drain()131 int PushConsumer::drain( )
132 {
133 	return 0;
134 }
135 
136 // Convenience function - generates a frame with an image of a given size
construct(int size)137 Frame *PushConsumer::construct( int size )
138 {
139 	mlt_frame f = mlt_frame_init( get_service() );
140 	Frame *frame = new Frame( f );
141 	uint8_t *buffer = ( uint8_t * )mlt_pool_alloc( size );
142 	frame->set( "image", buffer, size, mlt_pool_release );
143 	mlt_frame_close( f );
144 	return frame;
145 }
146 
147