Blame synfig-core/src/modules/mod_ffmpeg/trgt_ffmpeg.cpp

Carlos Lopez a09598
/* === S Y N F I G ========================================================= */
Carlos Lopez a09598
/*!	\file trgt_ffmpeg.cpp
Carlos Lopez a09598
**	\brief ppm Target Module
Carlos Lopez a09598
**
Carlos Lopez a09598
**	$Id$
Carlos Lopez a09598
**
Carlos Lopez a09598
**	\legal
Carlos Lopez a09598
**	Copyright (c) 2002-2005 Robert B. Quattlebaum Jr., Adrian Bentley
Carlos Lopez a09598
**	Copyright (c) 2007 Chris Moore
Diego Barrios Romero 6202fa
**	Copyright (c) 2010 Diego Barrios Romero
Carlos Lopez a09598
**
Carlos Lopez a09598
**	This package is free software; you can redistribute it and/or
Carlos Lopez a09598
**	modify it under the terms of the GNU General Public License as
Carlos Lopez a09598
**	published by the Free Software Foundation; either version 2 of
Carlos Lopez a09598
**	the License, or (at your option) any later version.
Carlos Lopez a09598
**
Carlos Lopez a09598
**	This package is distributed in the hope that it will be useful,
Carlos Lopez a09598
**	but WITHOUT ANY WARRANTY; without even the implied warranty of
Carlos Lopez a09598
**	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Carlos Lopez a09598
**	General Public License for more details.
Carlos Lopez a09598
**	\endlegal
Carlos Lopez a09598
**
Carlos Lopez a09598
** === N O T E S ===========================================================
Carlos Lopez a09598
**
Carlos Lopez a09598
** ========================================================================= */
Carlos Lopez a09598
Carlos Lopez a09598
/* === H E A D E R S ======================================================= */
Carlos Lopez a09598
Carlos Lopez a09598
#ifdef USING_PCH
Carlos Lopez a09598
#	include "pch.h"
Carlos Lopez a09598
#else
Carlos Lopez a09598
#ifdef HAVE_CONFIG_H
Carlos Lopez a09598
#	include <config.h></config.h>
Carlos Lopez a09598
#endif
Carlos Lopez a09598
bw 94d8a6
#include <synfig localization.h=""></synfig>
bw 94d8a6
#include <synfig general.h=""></synfig>
bw 94d8a6
Carlos Lopez a09598
#include <etl stringf=""></etl>
Carlos Lopez a09598
#include "trgt_ffmpeg.h"
c13708
#include <cstdio></cstdio>
Carlos Lopez a09598
#include <sys types.h=""></sys>
Carlos Lopez a09598
#if HAVE_SYS_WAIT_H
Carlos Lopez a09598
 #include <sys wait.h=""></sys>
Carlos Lopez a09598
#endif
Carlos Lopez a09598
#if HAVE_IO_H
Carlos Lopez a09598
 #include <io.h></io.h>
Carlos Lopez a09598
#endif
Carlos Lopez a09598
#if HAVE_PROCESS_H
Carlos Lopez a09598
 #include <process.h></process.h>
Carlos Lopez a09598
#endif
Carlos Lopez a09598
#if HAVE_FCNTL_H
Carlos Lopez a09598
 #include <fcntl.h></fcntl.h>
Carlos Lopez a09598
#endif
Carlos Lopez a09598
#include <unistd.h></unistd.h>
Carlos Lopez a09598
#include <algorithm></algorithm>
Carlos Lopez a09598
#include <functional></functional>
Carlos Lopez a09598
#include <etl clock=""></etl>
Carlos Lopez a09598
Carlos Lopez a09598
#endif
Carlos Lopez a09598
Carlos Lopez a09598
/* === M A C R O S ========================================================= */
Carlos Lopez a09598
Carlos Lopez a09598
using namespace synfig;
Carlos Lopez a09598
using namespace std;
Carlos Lopez a09598
using namespace etl;
Carlos Lopez a09598
Carlos Lopez a09598
#if defined(HAVE_FORK) && defined(HAVE_PIPE) && defined(HAVE_WAITPID)
Carlos Lopez a09598
 #define UNIX_PIPE_TO_PROCESSES
Carlos Lopez a09598
#else
Carlos Lopez a09598
 #define WIN32_PIPE_TO_PROCESSES
Carlos Lopez a09598
#endif
Carlos Lopez a09598
Carlos Lopez a09598
/* === G L O B A L S ======================================================= */
Carlos Lopez a09598
Carlos Lopez a09598
SYNFIG_TARGET_INIT(ffmpeg_trgt);
Carlos Lopez a09598
SYNFIG_TARGET_SET_NAME(ffmpeg_trgt,"ffmpeg");
Carlos Lopez a09598
SYNFIG_TARGET_SET_EXT(ffmpeg_trgt,"mpg");
Carlos Lopez a09598
SYNFIG_TARGET_SET_VERSION(ffmpeg_trgt,"0.1");
Carlos Lopez a09598
SYNFIG_TARGET_SET_CVS_ID(ffmpeg_trgt,"$Id$");
Carlos Lopez a09598
Carlos Lopez a09598
/* === M E T H O D S ======================================================= */
Carlos Lopez a09598
6320b8
ffmpeg_trgt::ffmpeg_trgt(const char *Filename, const synfig::TargetParam ¶ms):
6320b8
	pid(-1),
6320b8
	imagecount(0),
6320b8
	multi_image(false),
6320b8
	file(NULL),
6320b8
	filename(Filename),
6320b8
	buffer(NULL),
6320b8
	color_buffer(NULL),
6320b8
	bitrate()
Carlos Lopez a09598
{
dc451c
	set_alpha_mode(TARGET_ALPHA_MODE_FILL);
Diego Barrios Romero 6d0a3a
Diego Barrios Romero 6d0a3a
	// Set default video codec and bitrate if they weren't given.
Diego Barrios Romero 6d0a3a
	if (params.video_codec == "none")
Diego Barrios Romero 6d0a3a
		video_codec = "mpeg1video";
Diego Barrios Romero 6d0a3a
	else
Diego Barrios Romero 6d0a3a
		video_codec = params.video_codec;
Diego Barrios Romero 6d0a3a
Diego Barrios Romero 6d0a3a
	if (params.bitrate == -1)
Diego Barrios Romero 6d0a3a
		bitrate = 200;
Diego Barrios Romero 6d0a3a
	else
Diego Barrios Romero 6d0a3a
		bitrate = params.bitrate;
Carlos Lopez a09598
}
Carlos Lopez a09598
Carlos Lopez a09598
ffmpeg_trgt::~ffmpeg_trgt()
Carlos Lopez a09598
{
Carlos Lopez a09598
	if(file)
Carlos Lopez a09598
	{
Carlos Lopez a09598
		etl::yield();
Carlos Lopez a09598
		sleep(1);
Carlos Lopez a09598
#if defined(WIN32_PIPE_TO_PROCESSES)
Carlos Lopez a09598
		pclose(file);
Carlos Lopez a09598
#elif defined(UNIX_PIPE_TO_PROCESSES)
Carlos Lopez a09598
		fclose(file);
Carlos Lopez a09598
		int status;
Carlos Lopez a09598
		waitpid(pid,&status,0);
Carlos Lopez a09598
#endif
Carlos Lopez a09598
	}
Carlos Lopez a09598
	file=NULL;
Carlos Lopez a09598
	delete [] buffer;
Carlos Lopez a09598
	delete [] color_buffer;
Carlos Lopez a09598
}
Carlos Lopez a09598
Carlos Lopez a09598
bool
Carlos Lopez a09598
ffmpeg_trgt::set_rend_desc(RendDesc *given_desc)
Carlos Lopez a09598
{
Carlos Lopez a09598
	//given_desc->set_pixel_format(PF_RGB);
Carlos Lopez a09598
Carlos Lopez a09598
	// Make sure that the width and height
Carlos Lopez a09598
	// are multiples of 8
Carlos Lopez a09598
	given_desc->set_w((given_desc->get_w()+4)/8*8);
Carlos Lopez a09598
	given_desc->set_h((given_desc->get_h()+4)/8*8);
Carlos Lopez a09598
Carlos Lopez a09598
	/*
Carlos Lopez a09598
	// Valid framerates:
Carlos Lopez a09598
	// 23.976, 24, 25, 29.97, 30, 50 ,59.94, 60
Carlos Lopez a09598
	float fps=given_desc->get_frame_rate();
Carlos Lopez a09598
	if(fps <24.0)
Carlos Lopez a09598
		given_desc->set_frame_rate(23.976);
Carlos Lopez a09598
	if(fps>=24.0 && fps <25.0)
Carlos Lopez a09598
		given_desc->set_frame_rate(24);
Carlos Lopez a09598
	if(fps>=25.0 && fps <29.97)
Carlos Lopez a09598
		given_desc->set_frame_rate(25);
Carlos Lopez a09598
	if(fps>=29.97 && fps <30.0)
Carlos Lopez a09598
		given_desc->set_frame_rate(29.97);
Carlos Lopez a09598
	if(fps>=29.97 && fps <30.0)
Carlos Lopez a09598
		given_desc->set_frame_rate(29.97);
Carlos Lopez a09598
	if(fps>=30.0 && fps <50.0)
Carlos Lopez a09598
		given_desc->set_frame_rate(30.0);
Carlos Lopez a09598
	if(fps>=50.0 && fps <59.94)
Carlos Lopez a09598
		given_desc->set_frame_rate(50);
Carlos Lopez a09598
	if(fps>=59.94)
Carlos Lopez a09598
		given_desc->set_frame_rate(59.94);
Carlos Lopez a09598
    */
Carlos Lopez a09598
Carlos Lopez a09598
	desc=*given_desc;
Carlos Lopez a09598
Carlos Lopez a09598
	return true;
Carlos Lopez a09598
}
Carlos Lopez a09598
Carlos Lopez a09598
bool
76162e
ffmpeg_trgt::init(ProgressCallback *cb=NULL)
Carlos Lopez a09598
{
76162e
	String ffmpeg_binary_path;
76162e
	std::list< String > binary_choices;
76162e
	binary_choices.push_back("ffmpeg");
76162e
	binary_choices.push_back("avconv");
76162e
	std::list< String >::iterator iter;
76162e
	for(iter=binary_choices.begin();iter!=binary_choices.end();iter++)
76162e
	{
76162e
		String binary_path;
76162e
#if defined(WIN32_PIPE_TO_PROCESSES)
76162e
		binary_path = synfig::get_binary_path("");
76162e
		if (binary_path != "")
76162e
			binary_path = etl::dirname(binary_path)+ETL_DIRECTORY_SEPARATOR;
f9e38e
		binary_path += *iter+".exe";
16fc5a
		if( access( binary_path.c_str(), F_OK ) != -1 ) {
16fc5a
			binary_path = "\"" + binary_path + "\"";
16fc5a
			ffmpeg_binary_path = binary_path;
16fc5a
			break;
16fc5a
		}
76162e
#else
76162e
		binary_path = *iter;
16fc5a
		
16fc5a
		String command;
16fc5a
		String result;
16fc5a
		command = "which "+binary_path;
16fc5a
		FILE* pipe = popen(command.c_str(), "r");
16fc5a
		if (!pipe) {
16fc5a
			continue;
16fc5a
		}
16fc5a
		char buffer[128];
16fc5a
		while(!feof(pipe)) {
16fc5a
			if(fgets(buffer, 128, pipe) != NULL)
16fc5a
					result += buffer;
16fc5a
		}
16fc5a
		pclose(pipe);
c56e0b
		synfig::info(strprintf("\"%s\" --> %s", command.c_str(), result.c_str()));
c56e0b
		if (result.substr(0,1) == "/" || result.substr(0,2) == "./" ){
76162e
			ffmpeg_binary_path = binary_path;
76162e
			break;
76162e
		}
16fc5a
#endif
76162e
76162e
	}
76162e
	if (ffmpeg_binary_path == "")
76162e
	{
41c10d
		if (cb) cb->error(_("Error: No FFmpeg binary found.\n\nPlease install \"ffmpeg\" or \"avconv\" (libav-tools package)."));
76162e
		return false;
76162e
	}
76162e
	
Carlos Lopez a09598
	imagecount=desc.get_frame_start();
Carlos Lopez a09598
	if(desc.get_frame_end()-desc.get_frame_start()>0)
Carlos Lopez a09598
		multi_image=true;
Carlos Lopez fec44c
	// this should avoid conflicts with locale settings
Carlos Lopez fec44c
	synfig::ChangeLocale change_locale(LC_NUMERIC, "C");
1b44a8
	
1b44a8
	String video_codec_real;
1b44a8
	if (video_codec == "libx264-lossless")
1b44a8
		video_codec_real="libx264";
1b44a8
	else
1b44a8
		video_codec_real=video_codec;
1b44a8
	
1b44a8
	std::vector<string> vargs;</string>
76162e
	vargs.push_back(ffmpeg_binary_path);
1b44a8
	vargs.push_back("-f");
1b44a8
	vargs.push_back("image2pipe");
1b44a8
	vargs.push_back("-vcodec");
1b44a8
	vargs.push_back("ppm");
1b44a8
	vargs.push_back("-r");
1b44a8
	vargs.push_back(strprintf("%f", desc.get_frame_rate()));
1b44a8
	vargs.push_back("-i");
1b44a8
	vargs.push_back("pipe:");
7220ac
	vargs.push_back("-an");
1b44a8
	vargs.push_back("-metadata");
1b44a8
	vargs.push_back(strprintf("title=\"%s\"", get_canvas()->get_name().c_str()));
1b44a8
	vargs.push_back("-vcodec");
1b44a8
	vargs.push_back(video_codec_real);
1b44a8
	vargs.push_back("-b:v");
1b44a8
	vargs.push_back(strprintf("%ik", bitrate));
1b44a8
	if (video_codec == "libx264-lossless") {
1b44a8
		vargs.push_back("-tune");
1b44a8
		vargs.push_back("fastdecode");
1b44a8
		vargs.push_back("-pix_fmt");
1b44a8
		vargs.push_back("yuv420p");
1b44a8
		vargs.push_back("-qp");
1b44a8
		vargs.push_back("0");
1b44a8
	}
1b44a8
	vargs.push_back("-y");
1b44a8
	// We need "--" to separate filename from arguments (for the case when filename starts with "-")
41c10d
	if ( filename.substr(0,1) == "-" )
41c10d
		vargs.push_back("--"); 
360021
360021
#if defined(WIN32_PIPE_TO_PROCESSES)
360021
	vargs.push_back("\"" + filename + "\"");
360021
#else
1b44a8
	vargs.push_back(filename);
360021
#endif
d1f2d2
1b44a8
#if defined(WIN32_PIPE_TO_PROCESSES)
1b44a8
1b44a8
	String command;
1b44a8
	for( std::vector<std::string>::size_type i = 0; i != vargs.size(); i++ )</std::string>
1b44a8
	{
1b44a8
		command+=" "+vargs[i];
1b44a8
	}
1b44a8
	command+="\n";
Carlos Lopez a09598
d1f2d2
	// This covers the dumb cmd.exe behavior.
d1f2d2
	// See: http://eli.thegreenplace.net/2011/01/28/on-spaces-in-the-paths-of-programs-and-files-on-windows/
d1f2d2
	command = "\"" + command + "\"";
d1f2d2
Carlos Lopez a09598
	file=popen(command.c_str(),POPEN_BINARY_WRITE_TYPE);
Carlos Lopez a09598
Carlos Lopez a09598
#elif defined(UNIX_PIPE_TO_PROCESSES)
Carlos Lopez a09598
Carlos Lopez a09598
	int p[2];
Carlos Lopez a09598
Carlos Lopez a09598
	if (pipe(p)) {
efc1c1
		synfig::error(_("Unable to open pipe to ffmpeg (no pipe)"));
Carlos Lopez a09598
		return false;
Carlos Lopez a09598
	};
Carlos Lopez a09598
Carlos Lopez a09598
	pid = fork();
Carlos Lopez a09598
Carlos Lopez a09598
	if (pid == -1) {
efc1c1
		synfig::error(_("Unable to open pipe to ffmpeg (pid == -1)"));
Carlos Lopez a09598
		return false;
Carlos Lopez a09598
	}
Carlos Lopez a09598
Carlos Lopez a09598
	if (pid == 0){
Carlos Lopez a09598
		// Child process
Carlos Lopez a09598
		// Close pipeout, not needed
Carlos Lopez a09598
		close(p[1]);
Carlos Lopez a09598
		// Dup pipeout to stdin
Carlos Lopez a09598
		if( dup2( p[0], STDIN_FILENO ) == -1 ){
efc1c1
			synfig::error(_("Unable to open pipe to ffmpeg (dup2( p[0], STDIN_FILENO ) == -1)"));
Carlos Lopez a09598
			return false;
Carlos Lopez a09598
		}
Carlos Lopez a09598
		// Close the unneeded pipeout
Carlos Lopez a09598
		close(p[0]);
1b44a8
		
1b44a8
		char *args[vargs.size()+1];
1b44a8
		size_t idx = 0;
1b44a8
		for( std::vector<std::string>::size_type i = 0; i != vargs.size(); i++ )</std::string>
Diego Barrios Romero 88af81
		{
1b44a8
			//std::vector<char> writable(vargs[i].begin(), vargs[i].end());</char>
1b44a8
			//writable.push_back('\0');
1b44a8
			//args[idx++] = &writable[0];
1b44a8
			//args[idx++] = strdup(vargs[i].c_str());
1b44a8
			args[idx++] = &vargs[i][0];
1b44a8
			//synfig::info(&vargs[i][0]);
Diego Barrios Romero 88af81
		}
1b44a8
		args[idx++] = (char *)NULL;
1b44a8
		
76162e
		execvp(ffmpeg_binary_path.c_str(), args);
Diego Barrios Romero 6202fa
Carlos Lopez a09598
		// We should never reach here unless the exec failed
efc1c1
		synfig::error(_("Unable to open pipe to ffmpeg (exec failed)"));
Carlos Lopez a09598
		return false;
Carlos Lopez a09598
	} else {
Carlos Lopez a09598
		// Parent process
Carlos Lopez a09598
		// Close pipein, not needed
Carlos Lopez a09598
		close(p[0]);
Carlos Lopez a09598
		// Save pipeout to file handle, will write to it later
Carlos Lopez a09598
		file = fdopen(p[1], "wb");
Carlos Lopez a09598
	}
Carlos Lopez a09598
Carlos Lopez a09598
#else
Carlos Lopez a09598
	#error There are no known APIs for creating child processes
Carlos Lopez a09598
#endif
Carlos Lopez a09598
Carlos Lopez a09598
	// etl::yield();
Carlos Lopez a09598
Carlos Lopez a09598
	if(!file)
Carlos Lopez a09598
	{
efc1c1
		synfig::error(_("Unable to open pipe to ffmpeg (no file)"));
Carlos Lopez a09598
		return false;
Carlos Lopez a09598
	}
Carlos Lopez a09598
Carlos Lopez a09598
	return true;
Carlos Lopez a09598
}
Carlos Lopez a09598
Carlos Lopez a09598
void
Carlos Lopez a09598
ffmpeg_trgt::end_frame()
Carlos Lopez a09598
{
Carlos Lopez a09598
	//fprintf(file, " ");
Carlos Lopez a09598
	fflush(file);
Carlos Lopez a09598
	imagecount++;
Carlos Lopez a09598
}
Carlos Lopez a09598
Carlos Lopez a09598
bool
Carlos Lopez a09598
ffmpeg_trgt::start_frame(synfig::ProgressCallback */*callback*/)
Carlos Lopez a09598
{
Carlos Lopez a09598
	int w=desc.get_w(),h=desc.get_h();
Carlos Lopez a09598
Carlos Lopez a09598
	if(!file)
Carlos Lopez a09598
		return false;
Carlos Lopez a09598
Carlos Lopez a09598
	fprintf(file, "P6\n");
Carlos Lopez a09598
	fprintf(file, "%d %d\n", w, h);
Carlos Lopez a09598
	fprintf(file, "%d\n", 255);
Carlos Lopez a09598
Carlos Lopez a09598
	delete [] buffer;
Carlos Lopez a09598
	buffer=new unsigned char[3*w];
Carlos Lopez a09598
	delete [] color_buffer;
Carlos Lopez a09598
	color_buffer=new Color[w];
Carlos Lopez a09598
Carlos Lopez a09598
	return true;
Carlos Lopez a09598
}
Carlos Lopez a09598
Carlos Lopez a09598
Color *
Carlos Lopez a09598
ffmpeg_trgt::start_scanline(int /*scanline*/)
Carlos Lopez a09598
{
Carlos Lopez a09598
	return color_buffer;
Carlos Lopez a09598
}
Carlos Lopez a09598
Carlos Lopez a09598
bool
Carlos Lopez a09598
ffmpeg_trgt::end_scanline()
Carlos Lopez a09598
{
Carlos Lopez a09598
	if(!file)
Carlos Lopez a09598
		return false;
Carlos Lopez a09598
a4bbdd
	color_to_pixelformat(buffer, color_buffer, PF_RGB, 0, desc.get_w());
Carlos Lopez a09598
Carlos Lopez a09598
	if(!fwrite(buffer,1,desc.get_w()*3,file))
Carlos Lopez a09598
		return false;
Carlos Lopez a09598
Carlos Lopez a09598
	return true;
Carlos Lopez a09598
}