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 (void)fread(string, strlen(list_prefix), 1, stream);
119 if(!strncasecmp(string, list_prefix, strlen(list_prefix)))
122 //printf("FileList::open_file %d\n", __LINE__);
123 asset->format = list_type;
125 // Open index here or get frame size from file.
126 result = read_list_header();
127 //printf("FileList::open_file %d %s\n", __LINE__, path_list.values[0]);
128 if(!result) result = read_frame_header(path_list.values[0]);
132 //printf("FileList::open_file 2\n", asset->use_header);
133 //printf("FileList::open_file %d\n", __LINE__);
134 asset->format = frame_type;
135 result = read_frame_header(asset->path);
137 if(!asset->frame_rate)
138 asset->frame_rate = 1;
139 asset->video_length = -1;
144 eprintf(_("Error while opening \"%s\" for reading. \n%m\n"), asset->path);
148 Render::get_starting_number(asset->path,
149 first_number, number_start, number_digits, 6);
154 file->current_frame = 0;
155 // Compressed data storage
162 int FileList::close_file()
164 // path_list.total, asset->format, list_type, wr);
165 if(asset->format == list_type && path_list.total)
167 if(file->wr && asset->use_header) write_list_header();
168 path_list.remove_all_objects();
170 if(data) delete data;
171 if(writer) delete writer;
172 if(temp) delete temp;
175 FileBase::close_file();
179 int FileList::write_list_header()
181 FILE *stream = fopen(asset->path, "w");
182 if( !stream ) return 1;
183 // Use sprintf instead of fprintf for VFS.
184 fprintf(stream, "%s\n", list_prefix);
185 fprintf(stream, "# First line is always %s\n", list_prefix);
186 fprintf(stream, "# Frame rate:\n");
187 fprintf(stream, "%f\n", asset->frame_rate);
188 fprintf(stream, "# Width:\n");
189 fprintf(stream, "%d\n", asset->width);
190 fprintf(stream, "# Height:\n");
191 fprintf(stream, "%d\n", asset->height);
192 fprintf(stream, "# List of image files follows\n");
194 char *cp = strrchr(asset->path, '/');
195 int dir_len = !cp ? 0 : cp - asset->path;
197 for(int i = 0; i < path_list.total; i++) {
198 const char *path = path_list.values[i];
199 // Fix path for VFS but leave leading slash
200 if( !strncmp(path, RENDERFARM_FS_PREFIX, strlen(RENDERFARM_FS_PREFIX)) )
201 path += strlen(RENDERFARM_FS_PREFIX);
202 // ./path for relative list access
203 else if( dir_len > 0 && !strncmp(path, asset->path, dir_len) ) {
204 fprintf(stream, "."); path += dir_len;
206 fprintf(stream, "%s\n", path);
212 int FileList::read_list_header()
214 char string[BCTEXTLEN];
216 FILE *stream = fopen(asset->path, "r");
217 if( !stream ) return 1;
218 // Get information about the frames
220 if( feof(stream) || !fgets(string, BCTEXTLEN, stream) ) return 1;
221 } while(string[0] == '#' || string[0] == ' ' || isalpha(string[0]));
223 // Don't want a user configured frame rate to get destroyed
224 if(asset->frame_rate == 0)
225 asset->frame_rate = atof(string);
228 if( feof(stream) || !fgets(string, BCTEXTLEN, stream) ) return 1;
229 } while(string[0] == '#' || string[0] == ' ');
230 if( (asset->width = atol(string)) <= 0 ) return 1;
233 if( feof(stream) || !fgets(string, BCTEXTLEN, stream) ) return 1;
234 } while(string[0] == '#' || string[0] == ' ');
235 if( (asset->height = atol(string)) <= 0 ) return 1;
237 asset->interlace_mode = ILACE_MODE_UNDETECTED;
239 asset->audio_data = 0;
240 asset->video_data = 1;
242 char prefix[BCTEXTLEN], *bp = prefix, *cp = strrchr(asset->path, '/');
243 for( int i=0, n=!cp ? 0 : cp-asset->path; i<n; ++i ) *bp++ = asset->path[i];
246 // Get all the paths, expand relative paths
248 while( !feof(stream) && fgets(string, BCTEXTLEN, stream) ) {
249 int len = strlen(string);
250 if( !len || string[0] == '#' || string[0] == ' ' ) continue;
251 if( string[len-1] == '\n' ) string[len-1] = 0;
252 char path[BCTEXTLEN], *pp = path, *ep = pp + sizeof(path)-1;
253 if( string[0] == '.' && string[1] == '/' && prefix[0] )
254 pp += snprintf(pp, ep-pp, "%s/", prefix);
255 snprintf(pp, ep-pp, "%s", string);
256 if( access(path, R_OK) && !missing++ )
257 eprintf(_("%s:no such file"), path);
258 path_list.append(cstrdup(path));
261 //for(int i = 0; i < path_list.total; i++) printf("%s\n", path_list.values[i]);
263 if( !(asset->video_length = path_list.total) )
264 eprintf(_("%s:\nlist empty"), asset->path);
266 eprintf(_("%s:\n%d files not found"), asset->path, missing);
270 int FileList::read_frame(VFrame *frame)
275 // printf("FileList::read_frame %d %d use_header=%d current_frame=%d total=%d\n",
278 // asset->use_header,
279 // file->current_frame,
282 if(file->current_frame < 0 ||
283 (asset->use_header && file->current_frame >= path_list.total &&
284 asset->format == list_type))
287 if(asset->format == list_type)
289 char string[BCTEXTLEN];
291 if(asset->use_header)
293 path = path_list.values[file->current_frame];
297 path = calculate_path(file->current_frame, string);
302 // Fix path for VFS. Not used anymore.
303 if(!strncmp(asset->path, RENDERFARM_FS_PREFIX, strlen(RENDERFARM_FS_PREFIX)))
304 sprintf(string, "%s%s", RENDERFARM_FS_PREFIX, path);
306 strcpy(string, path);
310 if(!use_path() || frame->get_color_model() == BC_COMPRESSED)
312 if(!(in = fopen(string, "rb"))) {
313 eprintf(_("Error while opening \"%s\" for reading. \n%m\n"), string);
318 stat(string, &ostat);
320 switch(frame->get_color_model())
323 frame->allocate_compressed_data(ostat.st_size);
324 frame->set_compressed_size(ostat.st_size);
325 (void)fread(frame->get_data(), ostat.st_size, 1, in);
328 data->allocate_compressed_data(ostat.st_size);
329 data->set_compressed_size(ostat.st_size);
330 (void)fread(data->get_data(), ostat.st_size, 1, in);
331 result = read_frame(frame, data);
340 //printf("FileList::read_frame %d %s\n", __LINE__, string);
341 result = read_frame(frame, string);
346 asset->single_frame = 1;
347 // Allocate and decompress single frame into new temporary
348 //printf("FileList::read_frame %d\n", frame->get_color_model());
349 if(!temp || temp->get_color_model() != frame->get_color_model())
351 if(temp) delete temp;
355 if(!use_path() || frame->get_color_model() == BC_COMPRESSED)
357 FILE *fd = fopen(asset->path, "rb");
361 stat(asset->path, &ostat);
363 switch(frame->get_color_model())
366 frame->allocate_compressed_data(ostat.st_size);
367 frame->set_compressed_size(ostat.st_size);
368 (void)fread(frame->get_data(), ostat.st_size, 1, fd);
371 data->allocate_compressed_data(ostat.st_size);
372 data->set_compressed_size(ostat.st_size);
373 (void)fread(data->get_data(), ostat.st_size, 1, fd);
374 temp = new VFrame(asset->width, asset->height,
375 frame->get_color_model(), 0);
376 read_frame(temp, data);
384 eprintf(_("Error while opening \"%s\" for reading. \n%m\n"), asset->path);
390 temp = new VFrame(asset->width, asset->height, frame->get_color_model(), 0);
391 read_frame(temp, asset->path);
395 if(!temp) return result;
397 //printf("FileList::read_frame frame=%d temp=%d\n",
398 // frame->get_color_model(), // temp->get_color_model());
399 frame->transfer_from(temp);
403 // printf("FileList::read_frame %d %d\n", __LINE__, result);
405 // if(frame->get_y())
406 // for(int i = 0; i < 100000; i++)
408 // frame->get_y()[i] = 0xff;
410 // if(frame->get_rows())
411 // for(int i = 0; i < 100000; i++)
413 // frame->get_rows()[0][i] = 0xff;
420 int FileList::write_frames(VFrame ***frames, int len)
424 //printf("FileList::write_frames 1\n");
425 if(frames[0][0]->get_color_model() == BC_COMPRESSED)
427 for(int i = 0; i < asset->layers && !return_value; i++)
429 for(int j = 0; j < len && !return_value; j++)
431 VFrame *frame = frames[i][j];
432 char *path = create_path(frame->get_number());
433 //printf("FileList::write_frames %d %jd\n", __LINE__, frame->get_number());
436 FILE *fd = fopen(path, "wb");
439 return_value = !fwrite(frames[i][j]->get_data(),
440 frames[i][j]->get_compressed_size(),
448 eprintf(_("Error while opening \"%s\" for writing. \n%m\n"), asset->path);
456 //printf("FileList::write_frames 2\n");
457 writer->write_frames(frames, len);
458 //printf("FileList::write_frames 100\n");
471 void FileList::add_return_value(int amount)
473 table_lock->lock("FileList::add_return_value");
474 return_value += amount;
475 table_lock->unlock();
478 char* FileList::calculate_path(int number, char *string)
480 // Synthesize filename.
481 // If a header is used, the filename number must be in a different location.
482 if(asset->use_header)
485 strcpy(string, asset->path);
486 for(k = strlen(string) - 1; k > 0 && string[k] != '.'; k--)
488 if(k <= 0) k = strlen(string);
490 sprintf(&string[k], "%06d%s",
495 // Without a header, the original filename can be altered.
497 Render::create_filename(string,
507 char* FileList::create_path(int number_override)
509 if(asset->format != list_type) return asset->path;
511 table_lock->lock("FileList::create_path");
516 char output[BCTEXTLEN];
517 if(file->current_frame >= path_list.total || !asset->use_header)
520 if(number_override < 0)
521 number = file->current_frame++;
524 number = number_override;
525 file->current_frame++;
528 if(!asset->use_header)
530 number += first_number;
533 calculate_path(number, output);
535 path = new char[strlen(output) + 1];
536 strcpy(path, output);
537 path_list.append(path);
541 // Overwrite an old path
542 path = path_list.values[file->current_frame];
546 table_lock->unlock();
551 FrameWriterUnit* FileList::new_writer_unit(FrameWriter *writer)
553 return new FrameWriterUnit(writer);
556 int64_t FileList::get_memory_usage()
559 if(data) result += data->get_compressed_allocated();
560 if(temp) result += temp->get_data_size();
561 // printf("FileList::get_memory_usage %d %p %s %jd\n",
564 // file->asset->path,
569 int FileList::get_units()
571 return !writer ? 0 : writer->get_total_clients();
574 FrameWriterUnit* FileList::get_unit(int number)
576 return !writer ? 0 : (FrameWriterUnit*)writer->get_client(number);
579 int FileList::use_path()
589 FrameWriterPackage::FrameWriterPackage()
593 FrameWriterPackage::~FrameWriterPackage()
607 FrameWriterUnit::FrameWriterUnit(FrameWriter *server)
610 // Don't use server here since subclasses call this with no server.
611 this->server = server;
615 FrameWriterUnit::~FrameWriterUnit()
620 void FrameWriterUnit::process_package(LoadPackage *package)
622 //printf("FrameWriterUnit::process_package 1\n");
623 FrameWriterPackage *ptr = (FrameWriterPackage*)package;
627 //printf("FrameWriterUnit::process_package 2 %s\n", ptr->path);
628 if(!(file = fopen(ptr->path, "wb")))
630 eprintf(_("Error while opening \"%s\" for writing. \n%m\n"), ptr->path);
633 //printf("FrameWriterUnit::process_package 3");
636 int result = server->file->write_frame(ptr->input, output, this);
638 //printf("FrameWriterUnit::process_package 4 %s %d\n", ptr->path, output->get_compressed_size());
639 if(!result) result = !fwrite(output->get_data(), output->get_compressed_size(), 1, file);
640 //TRACE("FrameWriterUnit::process_package 4");
642 //TRACE("FrameWriterUnit::process_package 5");
644 server->file->add_return_value(result);
645 //TRACE("FrameWriterUnit::process_package 6");
658 FrameWriter::FrameWriter(FileList *file, int cpus)
659 : LoadServer(cpus, 0)
665 FrameWriter::~FrameWriter()
669 void FrameWriter::init_packages()
671 for(int i = 0, layer = 0, number = 0;
672 i < get_total_packages();
675 FrameWriterPackage *package = (FrameWriterPackage*)get_package(i);
676 package->input = frames[layer][number];
677 package->path = file->create_path(package->input->get_number());
678 // printf("FrameWriter::init_packages 1 %p %d %s\n",
680 // package->input->get_number(),
691 void FrameWriter::write_frames(VFrame ***frames, int len)
693 this->frames = frames;
695 set_package_count(len * file->asset->layers);
700 LoadClient* FrameWriter::new_client()
702 return file->new_writer_unit(this);
705 LoadPackage* FrameWriter::new_package()
707 return new FrameWriterPackage;