annotate src/Modules/Output/Graphics/Devices/GraphicsOutputDeviceMovie.cc @ 142:f03d4455b262

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