annotate trunk/src/Modules/Output/Graphics/Devices/GraphicsOutputDeviceMovie.cc @ 414:03954c09e5d0

- Cairo dependency
author tomwalters
date Sun, 24 Oct 2010 23:53:36 +0000
parents a908972d234e
children b6d5c0cc1849
rev   line source
tomwalters@397 1 // Copyright 2006, Willem van Engen
tomwalters@397 2 //
tomwalters@397 3 // AIM-C: A C++ implementation of the Auditory Image Model
tomwalters@397 4 // http://www.acousticscale.org/AIMC
tomwalters@397 5 //
tomwalters@397 6 // Licensed under the Apache License, Version 2.0 (the "License");
tomwalters@397 7 // you may not use this file except in compliance with the License.
tomwalters@397 8 // You may obtain a copy of the License at
tomwalters@397 9 //
tomwalters@397 10 // http://www.apache.org/licenses/LICENSE-2.0
tomwalters@397 11 //
tomwalters@397 12 // Unless required by applicable law or agreed to in writing, software
tomwalters@397 13 // distributed under the License is distributed on an "AS IS" BASIS,
tomwalters@397 14 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
tomwalters@397 15 // See the License for the specific language governing permissions and
tomwalters@397 16 // limitations under the License.
tomwalters@397 17
tomwalters@397 18 /*!
tomwalters@397 19 * \file
tomwalters@397 20 * \brief Output device for output to a movie
tomwalters@397 21 *
tomwalters@397 22 * \author Willem van Engen <cnbh@willem.engen.nl>
tomwalters@397 23 * \date created 2006/10/16
tomwalters@397 24 * \version \$Id: GraphicsOutputDeviceMovie.cpp 633 2008-09-11 04:20:16Z tom $
tomwalters@397 25 */
tomwalters@397 26
tomwalters@397 27 /*! \todo
tomwalters@397 28 * A recent experiment showed that the video was about an audioframe
tomwalters@397 29 * (something like 30 ms) behind the audio in rdct.wav. It seems odd
tomwalters@397 30 * to me, since I already output a frame at the beginning to compensate
tomwalters@397 31 * for the missed buffer.
tomwalters@397 32 * A solution that would solve this and be a broader improvement, is to
tomwalters@397 33 * include the source's time when Fire()ing. The outputdevice can then
tomwalters@397 34 * do audio/video synchronization. In the case of the movie, the very
tomwalters@397 35 * first gGrab() looks at the time and emits as much empty output frames
tomwalters@397 36 * as needed until the correct signal time is reached.
tomwalters@397 37 */
tomwalters@397 38 #include "Support/Common.h"
tomwalters@397 39
tomwalters@397 40 #ifdef _WINDOWS
tomwalters@398 41 # include <direct.h> // for _mkdir&_rmdir
tomwalters@397 42 #else
tomwalters@398 43 # include <sys/types.h>
tomwalters@398 44 # include <dirent.h> // for opendir&friends
tomwalters@397 45 #endif
tomwalters@397 46 #include <stdio.h>
tomwalters@397 47 #include <string.h>
tomwalters@397 48
tom@400 49 #include "Modules/Output/Graphics/Devices/GraphicsOutputDeviceMovie.h"
tomwalters@397 50
tom@400 51 namespace aimc {
tomwalters@397 52
tomwalters@411 53 GraphicsOutputDeviceMovie::GraphicsOutputDeviceMovie(Parameters *parameters)
tomwalters@411 54 : GraphicsOutputDeviceCairo(parameters) {
tomwalters@411 55 sound_filename_.clear();
tomwalters@411 56 movie_filename_.clear();
tomwalters@397 57 }
tomwalters@397 58
tomwalters@411 59 bool GraphicsOutputDeviceMovie::Initialize(Parameters *global_parameters) {
tomwalters@411 60 global_parameters_ = global_parameters;
tomwalters@411 61 sound_filename_ = global_parameters->GetString("input_filename");
tomwalters@411 62 string file_suffix = parameters_->DefaultString("filename_suffix", ".mov");
tomwalters@411 63 movie_filename_ = global_parameters->GetString("output_filename_base")
tomwalters@411 64 + file_suffix;
tomwalters@411 65
tomwalters@398 66 FILE *f;
tomwalters@397 67
tomwalters@398 68 // Check sound file exists
tomwalters@411 69 if ((f = fopen(sound_filename_.c_str(), "r")) == NULL) {
tom@400 70 LOG_ERROR(_T("Couldn't open sound file '%s' for movie creation."),
tomwalters@411 71 sound_filename_.c_str());
tomwalters@411 72 sound_filename_.clear();
tomwalters@398 73 return false;
tomwalters@398 74 }
tomwalters@398 75 fclose(f);
tomwalters@397 76
tomwalters@398 77 // Check movie output file can be made
tomwalters@411 78 if ((f = fopen(movie_filename_.c_str(), "w")) == NULL) {
tom@400 79 LOG_ERROR(_T("Couldn't open movie file '%s' to write to."),
tomwalters@411 80 movie_filename_.c_str());
tomwalters@411 81 movie_filename_.clear();
tomwalters@398 82 return false;
tomwalters@398 83 }
tomwalters@398 84 fclose(f);
tomwalters@397 85
tomwalters@398 86 // Get a temporary image output directory
tomwalters@398 87 //! \warning Not really safe ... but windows has no mkdtemp()
tomwalters@398 88 //! \todo Make build system check for mkdtemp() to use it when available. See TODO.txt.
tomwalters@397 89 #ifdef _WINDOWS
tomwalters@411 90 char *temp_dir = NULL;
tomwalters@411 91 if ((temp_dir = _tempnam(NULL, AIM_NAME))
tomwalters@411 92 && _mkdir(temp_dir) >= 0) {
tomwalters@411 93 directory_ = temp_dir;
tomwalters@411 94 directory_ += "\\"; // Make sure to end with trailing slash
tomwalters@411 95 } else {
tom@400 96 LOG_ERROR(_T("Couldn't create a temporary directory for movie output."));
tomwalters@411 97 if (temp_dir) {
tomwalters@411 98 free(temp_dir);
tomwalters@411 99 }
tomwalters@398 100 return false;
tomwalters@398 101 }
tomwalters@411 102 if (temp_dir) {
tomwalters@411 103 free(temp_dir);
tomwalters@397 104 }
tomwalters@411 105 #else
tomwalters@411 106 char temp_dir[PATH_MAX];
tomwalters@411 107 strcpy(temp_dir, "/tmp/"AIM_NAME"-movie.XXXXXX");
tomwalters@411 108 if (mkdtemp(temp_dir)) {
tomwalters@411 109 directory_ = temp_dir;
tomwalters@411 110 directory_ += "/"; // Make sure to end with trailing slash
tomwalters@411 111 } else {
tomwalters@411 112 LOG_ERROR(_T("Couldn't create a temporary directory for movie output."));
tomwalters@398 113 return false;
tomwalters@397 114 }
tomwalters@411 115 #endif
tomwalters@411 116
tomwalters@411 117 // We want png for movie conversion
tomwalters@411 118 parameters_->SetString("output.img.format", "png");
tomwalters@411 119 if ( !GraphicsOutputDeviceCairo::Initialize(directory_) ) {
tomwalters@411 120 return false;
tomwalters@411 121 }
tomwalters@398 122 return true;
tomwalters@397 123 }
tomwalters@397 124
tomwalters@397 125 void GraphicsOutputDeviceMovie::Start() {
tomwalters@398 126 GraphicsOutputDeviceCairo::Start();
tomwalters@411 127 // Output a couple of frames to get audio/video in sync, put params in there.
tomwalters@411 128 gGrab();
tomwalters@411 129 PlotParameterScreen();
tomwalters@411 130 gRelease();
tomwalters@398 131 gGrab();
tomwalters@398 132 PlotParameterScreen();
tomwalters@398 133 gRelease();
tomwalters@397 134 }
tomwalters@397 135
tomwalters@397 136 void GraphicsOutputDeviceMovie::Stop() {
tomwalters@398 137 GraphicsOutputDeviceCairo::Stop();
tomwalters@398 138 CloseFile();
tomwalters@397 139
tomwalters@397 140 #ifdef __WX__
tomwalters@398 141 // GUI only: popup dialog
tomwalters@397 142 #else
tomwalters@398 143 printf("Generating movie ... \n");
tomwalters@397 144 #endif
tomwalters@411 145 AIM_ASSERT(parameters_);
tomwalters@411 146 // Convert images and sound file to a movie.
tomwalters@411 147 //! \warning Movie files are overwritten without warning.
tomwalters@398 148 char sffmpegPath[1024];
tomwalters@411 149 if (!parameters_->IsSet("output.ffmpeg_path")) {
tomwalters@397 150 strcpy(sffmpegPath,"ffmpeg");
tomwalters@397 151 } else {
tomwalters@411 152 strcpy(sffmpegPath, parameters_->GetString("output.ffmpeg_path"));
tomwalters@397 153 }
tomwalters@397 154 char sCodecOptions[1024];
tomwalters@411 155 if (!parameters_->IsSet("output.ffmpeg_codec_options")) {
tomwalters@397 156 strcpy(sCodecOptions,"");
tomwalters@397 157 } else {
tomwalters@411 158 strcpy(sCodecOptions, parameters_->GetString("output.ffmpeg_codec_options"));
tomwalters@397 159 }
tomwalters@411 160 float frame_rate = global_parameters_->DefaultFloat("frame_rate", -1.0);
tomwalters@398 161 char sCmdLine[1024]; //!\todo check that snprintf does not want a larger buffer
tomwalters@398 162 snprintf(sCmdLine, sizeof(sCmdLine)/sizeof(sCmdLine[0]),
tomwalters@398 163 "%s -r %.2f -y -i \"%s\" -i \"%s%%06d.png\" "
tomwalters@398 164 "-sameq -r %.2f -ar 44100 -acodec pcm_s16le %s \"%s\"",
tomwalters@411 165 sffmpegPath, frame_rate, sound_filename_.c_str(), directory_.c_str(),
tomwalters@411 166 frame_rate, sCodecOptions, movie_filename_.c_str());
tomwalters@397 167 printf(sCmdLine);
tomwalters@397 168 printf("\n");
tomwalters@398 169 if (system(sCmdLine)) {
tom@400 170 LOG_ERROR(_T("Couldn't create movie output."));
tomwalters@398 171 }
tomwalters@397 172
tomwalters@397 173 #ifdef __WX__
tomwalters@398 174 // GUI only: close dialog again
tomwalters@397 175 #endif
tomwalters@398 176 // Remove files in temporary directory and the dir itself
tomwalters@398 177 //! \todo make portable function, possibly decided on by build system
tomwalters@397 178 #ifdef _WINDOWS
tomwalters@398 179 HANDLE hList;
tomwalters@398 180 WIN32_FIND_DATA FileData;
tomwalters@398 181 snprintf(sCmdLine, sizeof(sCmdLine)/sizeof(sCmdLine[0]), "%s/*.*", m_sDir);
tomwalters@398 182 if ((hList = FindFirstFile(sCmdLine, &FileData)) == INVALID_HANDLE_VALUE) {
tom@400 183 LOG_ERROR(_T("Couldn't remove files from temporary directory."));
tomwalters@398 184 return;
tomwalters@398 185 }
tomwalters@398 186 bool bRMfinished = false;
tomwalters@398 187 while (!bRMfinished) {
tomwalters@398 188 snprintf(sCmdLine,
tomwalters@397 189 sizeof(sCmdLine)/sizeof(sCmdLine[0]),
tomwalters@398 190 "%s%s",
tomwalters@397 191 m_sDir,
tomwalters@397 192 FileData.cFileName);
tomwalters@398 193 remove(sCmdLine);
tomwalters@398 194 if (!FindNextFile(hList, &FileData) && GetLastError() == ERROR_NO_MORE_FILES) {
tomwalters@398 195 bRMfinished = true;
tomwalters@397 196 }
tomwalters@398 197 }
tomwalters@398 198 FindClose(hList);
tomwalters@398 199 _rmdir(m_sDir);
tomwalters@397 200 #else
tomwalters@398 201 DIR *dir;
tomwalters@398 202 struct dirent *dirent;
tomwalters@411 203 if (!(dir = opendir(directory_.c_str()))) {
tom@400 204 LOG_ERROR(_T("Couldn't remove files in temporary directory."));
tomwalters@398 205 return;
tomwalters@398 206 }
tomwalters@402 207 while ((dirent = readdir(dir))) {
tomwalters@398 208 snprintf(sCmdLine,
tomwalters@397 209 sizeof(sCmdLine)/sizeof(sCmdLine[0]),
tomwalters@398 210 "%s%s",
tomwalters@411 211 directory_.c_str(),
tomwalters@397 212 dirent->d_name);
tomwalters@398 213 unlink(sCmdLine);
tomwalters@398 214 }
tomwalters@398 215 closedir(dir);
tomwalters@411 216 rmdir(directory_.c_str());
tomwalters@397 217 #endif
tomwalters@397 218 }
tomwalters@397 219
tomwalters@397 220 void GraphicsOutputDeviceMovie::PlotParameterScreen() {
tomwalters@411 221 AIM_ASSERT(parameters_);
tomwalters@398 222 char sStr[50];
tomwalters@398 223 int lineno = 1;
tomwalters@397 224
tomwalters@411 225 float fMarL = parameters_->GetFloat(_S("graph.margin.left"));
tomwalters@411 226 float fMarT = parameters_->GetFloat(_S("graph.margin.top"));
tomwalters@398 227 float fTextHeight = 1.0f / 50.0f * 1.2; // change this when fontsizing is there!
tomwalters@397 228
tomwalters@398 229 gText2f(fMarL, 1-(fMarT+fTextHeight*lineno++),
tomwalters@397 230 _S("AIM-C"));
tomwalters@398 231 gText2f(fMarL,
tomwalters@397 232 1-(fMarT+fTextHeight*lineno++),
tomwalters@397 233 _S("(c) 2006-2010, Thomas Walters, Willem van Engen"));
tomwalters@398 234 gText2f(fMarL,
tomwalters@397 235 1-(fMarT+fTextHeight*lineno++),
tomwalters@397 236 _S("http://aimc.acousticscale.org/"));
tomwalters@398 237 lineno++;
tomwalters@397 238
tomwalters@398 239 static const char *pPlotParams[] = {
tomwalters@398 240 _S("input.buffersize"),
tomwalters@398 241 _S("input.samplerate"),
tomwalters@398 242 _S("bmm.freqstart"),
tomwalters@398 243 _S("bmm.freqend"),
tomwalters@398 244 _S("bmm.numchannels"),
tomwalters@398 245 _S("preset.name"),
tomwalters@398 246 _S("preset.title"),
tomwalters@398 247 NULL
tomwalters@398 248 };
tomwalters@398 249 for (int i = 0; pPlotParams[i]; i++) {
tomwalters@398 250 snprintf(sStr,
tomwalters@397 251 sizeof(sStr)/sizeof(sStr[0]), _S("%s=%s"),
tomwalters@398 252 pPlotParams[i],
tomwalters@411 253 parameters_->GetString(pPlotParams[i]));
tomwalters@398 254 gText2f(fMarL,
tomwalters@397 255 1-(fMarT+fTextHeight*lineno++),
tomwalters@397 256 sStr);
tomwalters@398 257 }
tomwalters@397 258 }
tom@400 259 } // namespace aimc