4 * Copyright (C) 1997-2012 Adam Williams <broadcast at earthling dot net>
6 * This program 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.
11 * This program 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.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23 #include "bcsignals.h"
28 #include "interlacemodes.h"
30 #include "mwindow.inc"
32 #include "renderfarmfsserver.inc"
34 #include "mainerror.h"
45 FileList::FileList(Asset *asset,
47 const char *list_prefix,
48 const char *file_extension,
51 : FileBase(asset, file)
54 path_list.set_array_delete();
55 asset->video_data = 1;
56 this->list_prefix = list_prefix;
57 this->file_extension = file_extension;
58 this->frame_type = frame_type;
59 this->list_type = list_type;
60 table_lock = new Mutex("FileList::table_lock");
69 int FileList::reset_parameters_derived()
78 int FileList::open_file(int rd, int wr)
82 // skip header for write
85 int fd = open(asset->path, O_CREAT+O_TRUNC+O_RDWR, 0777);
89 // Frame files are created in write_frame and list index is created when
91 // Look for the starting number in the path but ignore the starting character
92 // and total digits since these are used by the header.
93 Render::get_starting_number(asset->path,
94 first_number, number_start, number_digits);
95 path_list.remove_all_objects();
96 writer = new FrameWriter(this,
97 asset->format == list_type ? file->cpus : 1);
100 eprintf(_("Error while opening \"%s\" for writing. \n%m\n"), asset->path);
105 // Determine type of file.
106 // Header isn't used for background rendering, in which case everything known
107 // by the file encoder is known by the decoder.
108 //printf("FileList::open_file %d %d\n", __LINE__, asset->use_header);
109 if(asset->use_header)
111 FILE *stream = fopen(asset->path, "rb");
112 //printf("FileList::open_file %d asset->path=%s\n", __LINE__, asset->path);
115 char string[BCTEXTLEN];
116 int len = strlen(list_prefix);
117 int ret = fread(string, 1, strlen(list_prefix), stream);
119 result = len == ret ? 0 : 1;
120 if( !result && !strncasecmp(string, list_prefix, len)) {
121 asset->format = list_type;
122 result = read_list_header();
124 result = read_frame_header(path_list.values[0]);
126 else if( !result && frame_type != FILE_UNKNOWN ) {
127 asset->format = frame_type;
128 result = read_frame_header(asset->path);
129 asset->video_length = -1;
134 asset->actual_width = asset->width;
135 asset->actual_height = asset->height;
136 int scale = asset->proxy_scale;
137 if( !scale ) scale = 1;
138 asset->width = asset->actual_width * scale;
139 asset->height = asset->actual_height * scale;
141 if( !asset->frame_rate )
142 asset->frame_rate = 10;
146 eprintf(_("Error while opening \"%s\" for reading. \n%m\n"), asset->path);
150 Render::get_starting_number(asset->path,
151 first_number, number_start, number_digits, 6);
156 file->current_frame = 0;
157 // Compressed data storage
164 int FileList::close_file()
166 // path_list.total, asset->format, list_type, wr);
167 if(asset->format == list_type && path_list.total)
169 if(file->wr && asset->use_header) write_list_header();
170 path_list.remove_all_objects();
172 if(data) delete data;
173 if(writer) delete writer;
174 if(temp) delete temp;
177 FileBase::close_file();
181 int FileList::write_list_header()
183 FILE *stream = fopen(asset->path, "w");
184 if( !stream ) return 1;
185 // Use sprintf instead of fprintf for VFS.
186 fprintf(stream, "%s\n", list_prefix);
187 fprintf(stream, "# First line is always %s\n", list_prefix);
188 fprintf(stream, "# Frame rate:\n");
189 fprintf(stream, "%f\n", asset->frame_rate);
190 fprintf(stream, "# Width:\n");
191 fprintf(stream, "%d\n", asset->width);
192 fprintf(stream, "# Height:\n");
193 fprintf(stream, "%d\n", asset->height);
194 fprintf(stream, "# List of image files follows\n");
196 char *cp = strrchr(asset->path, '/');
197 int dir_len = !cp ? 0 : cp - asset->path;
199 for(int i = 0; i < path_list.total; i++) {
200 const char *path = path_list.values[i];
201 // Fix path for VFS but leave leading slash
202 if( !strncmp(path, RENDERFARM_FS_PREFIX, strlen(RENDERFARM_FS_PREFIX)) )
203 path += strlen(RENDERFARM_FS_PREFIX);
204 // ./path for relative list access
205 else if( dir_len > 0 && !strncmp(path, asset->path, dir_len) ) {
206 fprintf(stream, "."); path += dir_len;
208 fprintf(stream, "%s\n", path);
214 int FileList::read_list_header()
216 char string[BCTEXTLEN];
218 FILE *stream = fopen(asset->path, "r");
219 if( !stream ) return 1;
220 // Get information about the frames
222 if( feof(stream) || !fgets(string, BCTEXTLEN, stream) ) return 1;
223 } while(string[0] == '#' || string[0] == ' ' || isalpha(string[0]));
225 // Don't want a user configured frame rate to get destroyed
226 if(asset->frame_rate == 0)
227 asset->frame_rate = atof(string);
230 if( feof(stream) || !fgets(string, BCTEXTLEN, stream) ) return 1;
231 } while(string[0] == '#' || string[0] == ' ');
232 if( (asset->width = atol(string)) <= 0 ) return 1;
235 if( feof(stream) || !fgets(string, BCTEXTLEN, stream) ) return 1;
236 } while(string[0] == '#' || string[0] == ' ');
237 if( (asset->height = atol(string)) <= 0 ) return 1;
239 asset->interlace_mode = ILACE_MODE_UNDETECTED;
241 asset->audio_data = 0;
242 asset->video_data = 1;
244 char prefix[BCTEXTLEN], *bp = prefix, *cp = strrchr(asset->path, '/');
245 for( int i=0, n=!cp ? 0 : cp-asset->path; i<n; ++i ) *bp++ = asset->path[i];
248 // Get all the paths, expand relative paths
250 while( !feof(stream) && fgets(string, BCTEXTLEN, stream) ) {
251 int len = strlen(string);
252 if( !len || string[0] == '#' || string[0] == ' ' ) continue;
253 if( string[len-1] == '\n' ) string[len-1] = 0;
254 char path[BCTEXTLEN], *pp = path, *ep = pp + sizeof(path)-1;
255 if( string[0] == '.' && string[1] == '/' && prefix[0] )
256 pp += snprintf(pp, ep-pp, "%s/", prefix);
257 snprintf(pp, ep-pp, "%s", string);
258 if( access(path, R_OK) && !missing++ )
259 eprintf(_("%s:no such file"), path);
260 path_list.append(cstrdup(path));
263 //for(int i = 0; i < path_list.total; i++) printf("%s\n", path_list.values[i]);
265 if( !(asset->video_length = path_list.total) )
266 eprintf(_("%s:\nlist empty"), asset->path);
268 eprintf(_("%s:\n%d files not found"), asset->path, missing);
272 int FileList::read_frame(VFrame *frame)
277 // printf("FileList::read_frame %d %d use_header=%d current_frame=%d total=%d\n",
280 // asset->use_header,
281 // file->current_frame,
284 if(file->current_frame < 0 ||
285 (asset->use_header && file->current_frame >= path_list.total &&
286 asset->format == list_type))
289 if(asset->format == list_type)
291 char string[BCTEXTLEN];
293 if(asset->use_header)
295 path = path_list.values[file->current_frame];
299 path = calculate_path(file->current_frame, string);
304 // Fix path for VFS. Not used anymore.
305 if(!strncmp(asset->path, RENDERFARM_FS_PREFIX, strlen(RENDERFARM_FS_PREFIX)))
306 sprintf(string, "%s%s", RENDERFARM_FS_PREFIX, path);
308 strcpy(string, path);
312 if(!use_path() || frame->get_color_model() == BC_COMPRESSED)
314 if(!(in = fopen(string, "rb"))) {
315 eprintf(_("Error while opening \"%s\" for reading. \n%m\n"), string);
320 stat(string, &ostat);
322 switch(frame->get_color_model())
325 frame->allocate_compressed_data(ostat.st_size);
326 frame->set_compressed_size(ostat.st_size);
327 (void)fread(frame->get_data(), ostat.st_size, 1, in);
330 data->allocate_compressed_data(ostat.st_size);
331 data->set_compressed_size(ostat.st_size);
332 (void)fread(data->get_data(), ostat.st_size, 1, in);
333 result = read_frame(frame, data);
342 //printf("FileList::read_frame %d %s\n", __LINE__, string);
343 result = read_frame(frame, string);
348 asset->single_frame = 1;
349 // Allocate and decompress single frame into new temporary
350 //printf("FileList::read_frame %d\n", frame->get_color_model());
351 if( !temp || temp->get_color_model() != frame->get_color_model() ) {
352 delete temp; temp = 0;
353 int aw = asset->actual_width, ah = asset->actual_height;
354 int color_model = frame->get_color_model();
355 switch( color_model ) {
359 aw = (aw+1) & ~1; ah = (ah+1) & ~1;
363 aw = (aw+3) & ~3; ah = (ah+3) & ~3;
366 if( !use_path() || color_model == BC_COMPRESSED ) {
367 FILE *fd = fopen(asset->path, "rb");
370 stat(asset->path, &ostat);
372 switch(frame->get_color_model()) {
374 frame->allocate_compressed_data(ostat.st_size);
375 frame->set_compressed_size(ostat.st_size);
376 (void)fread(frame->get_data(), ostat.st_size, 1, fd);
379 data->allocate_compressed_data(ostat.st_size);
380 data->set_compressed_size(ostat.st_size);
381 (void)fread(data->get_data(), ostat.st_size, 1, fd);
382 temp = new VFrame(aw, ah, color_model);
383 read_frame(temp, data);
390 eprintf(_("Error while opening \"%s\" for reading. \n%m\n"), asset->path);
395 temp = new VFrame(aw, ah, color_model);
396 read_frame(temp, asset->path);
400 if(!temp) return result;
402 //printf("FileList::read_frame frame=%d temp=%d\n",
403 // frame->get_color_model(), // temp->get_color_model());
404 frame->transfer_from(temp);
408 // printf("FileList::read_frame %d %d\n", __LINE__, result);
410 // if(frame->get_y())
411 // for(int i = 0; i < 100000; i++)
413 // frame->get_y()[i] = 0xff;
415 // if(frame->get_rows())
416 // for(int i = 0; i < 100000; i++)
418 // frame->get_rows()[0][i] = 0xff;
425 int FileList::write_frames(VFrame ***frames, int len)
429 //printf("FileList::write_frames 1\n");
430 if(frames[0][0]->get_color_model() == BC_COMPRESSED)
432 for(int i = 0; i < asset->layers && !return_value; i++)
434 for(int j = 0; j < len && !return_value; j++)
436 VFrame *frame = frames[i][j];
437 char *path = create_path(frame->get_number());
438 //printf("FileList::write_frames %d %jd\n", __LINE__, frame->get_number());
441 FILE *fd = fopen(path, "wb");
444 return_value = !fwrite(frames[i][j]->get_data(),
445 frames[i][j]->get_compressed_size(),
453 eprintf(_("Error while opening \"%s\" for writing. \n%m\n"), asset->path);
461 //printf("FileList::write_frames 2\n");
462 writer->write_frames(frames, len);
463 //printf("FileList::write_frames 100\n");
476 void FileList::add_return_value(int amount)
478 table_lock->lock("FileList::add_return_value");
479 return_value += amount;
480 table_lock->unlock();
483 char* FileList::calculate_path(int number, char *string)
485 // Synthesize filename.
486 // If a header is used, the filename number must be in a different location.
487 if(asset->use_header)
490 strcpy(string, asset->path);
491 for(k = strlen(string) - 1; k > 0 && string[k] != '.'; k--)
493 if(k <= 0) k = strlen(string);
495 sprintf(&string[k], "%06d%s",
500 // Without a header, the original filename can be altered.
502 Render::create_filename(string,
512 char* FileList::create_path(int number_override)
514 if(asset->format != list_type) return asset->path;
516 table_lock->lock("FileList::create_path");
521 char output[BCTEXTLEN];
522 if(file->current_frame >= path_list.total || !asset->use_header)
525 if(number_override < 0)
526 number = file->current_frame++;
529 number = number_override;
530 file->current_frame++;
533 if(!asset->use_header)
535 number += first_number;
538 calculate_path(number, output);
540 path = new char[strlen(output) + 1];
541 strcpy(path, output);
542 path_list.append(path);
546 // Overwrite an old path
547 path = path_list.values[file->current_frame];
551 table_lock->unlock();
556 FrameWriterUnit* FileList::new_writer_unit(FrameWriter *writer)
558 return new FrameWriterUnit(writer);
561 int64_t FileList::get_memory_usage()
564 if(data) result += data->get_compressed_allocated();
565 if(temp) result += temp->get_data_size();
566 // printf("FileList::get_memory_usage %d %p %s %jd\n",
569 // file->asset->path,
574 int FileList::get_units()
576 return !writer ? 0 : writer->get_total_clients();
579 FrameWriterUnit* FileList::get_unit(int number)
581 return !writer ? 0 : (FrameWriterUnit*)writer->get_client(number);
584 int FileList::use_path()
594 FrameWriterPackage::FrameWriterPackage()
598 FrameWriterPackage::~FrameWriterPackage()
612 FrameWriterUnit::FrameWriterUnit(FrameWriter *server)
615 // Don't use server here since subclasses call this with no server.
616 this->server = server;
620 FrameWriterUnit::~FrameWriterUnit()
625 void FrameWriterUnit::process_package(LoadPackage *package)
627 //printf("FrameWriterUnit::process_package 1\n");
628 FrameWriterPackage *ptr = (FrameWriterPackage*)package;
632 //printf("FrameWriterUnit::process_package 2 %s\n", ptr->path);
633 if(!(file = fopen(ptr->path, "wb")))
635 eprintf(_("Error while opening \"%s\" for writing. \n%m\n"), ptr->path);
638 //printf("FrameWriterUnit::process_package 3");
641 int result = server->file->write_frame(ptr->input, output, this);
643 //printf("FrameWriterUnit::process_package 4 %s %d\n", ptr->path, output->get_compressed_size());
644 if(!result) result = !fwrite(output->get_data(), output->get_compressed_size(), 1, file);
645 //TRACE("FrameWriterUnit::process_package 4");
647 //TRACE("FrameWriterUnit::process_package 5");
649 server->file->add_return_value(result);
650 //TRACE("FrameWriterUnit::process_package 6");
663 FrameWriter::FrameWriter(FileList *file, int cpus)
664 : LoadServer(cpus, 0)
670 FrameWriter::~FrameWriter()
674 void FrameWriter::init_packages()
676 for(int i = 0, layer = 0, number = 0;
677 i < get_total_packages();
680 FrameWriterPackage *package = (FrameWriterPackage*)get_package(i);
681 package->input = frames[layer][number];
682 package->path = file->create_path(package->input->get_number());
683 // printf("FrameWriter::init_packages 1 %p %d %s\n",
685 // package->input->get_number(),
696 void FrameWriter::write_frames(VFrame ***frames, int len)
698 this->frames = frames;
700 set_package_count(len * file->asset->layers);
705 LoadClient* FrameWriter::new_client()
707 return file->new_writer_unit(this);
710 LoadPackage* FrameWriter::new_package()
712 return new FrameWriterPackage;