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 int width = asset->width, height = asset->height;
116 char string[BCTEXTLEN];
117 int len = strlen(list_prefix);
118 int ret = fread(string, 1, len, stream);
120 result = len == ret ? 0 : 1;
121 if( !result && !strncasecmp(string, list_prefix, len)) {
122 asset->format = list_type;
123 result = read_list_header();
125 result = read_frame_header(path_list.values[0]);
127 else if( !result && frame_type != FILE_UNKNOWN ) {
128 asset->format = frame_type;
129 result = read_frame_header(asset->path);
130 asset->video_length = -1;
135 asset->actual_width = asset->width;
136 asset->actual_height = asset->height;
137 int scale = asset->proxy_scale;
139 asset->width = asset->actual_width * scale;
140 asset->height = asset->actual_height * scale;
142 else { // can_scale_input
143 if( width ) asset->width = width;
144 if( height ) asset->height = height;
147 if( !asset->frame_rate )
148 asset->frame_rate = 10;
152 eprintf(_("Error while opening \"%s\" for reading. \n%m\n"), asset->path);
156 Render::get_starting_number(asset->path,
157 first_number, number_start, number_digits, 6);
162 file->current_frame = 0;
163 // Compressed data storage
170 int FileList::close_file()
172 // path_list.total, asset->format, list_type, wr);
173 if(asset->format == list_type && path_list.total)
175 if(file->wr && asset->use_header) write_list_header();
176 path_list.remove_all_objects();
178 if(data) delete data;
179 if(writer) delete writer;
180 if(temp) delete temp;
183 FileBase::close_file();
187 int FileList::write_list_header()
189 FILE *stream = fopen(asset->path, "w");
190 if( !stream ) return 1;
191 // Use sprintf instead of fprintf for VFS.
192 fprintf(stream, "%s\n", list_prefix);
193 fprintf(stream, "# First line is always %s\n", list_prefix);
194 fprintf(stream, "# Frame rate:\n");
195 fprintf(stream, "%f\n", asset->frame_rate);
196 fprintf(stream, "# Width:\n");
197 fprintf(stream, "%d\n", asset->width);
198 fprintf(stream, "# Height:\n");
199 fprintf(stream, "%d\n", asset->height);
200 fprintf(stream, "# List of image files follows\n");
202 char *cp = strrchr(asset->path, '/');
203 int dir_len = !cp ? 0 : cp - asset->path;
205 for(int i = 0; i < path_list.total; i++) {
206 const char *path = path_list.values[i];
207 // Fix path for VFS but leave leading slash
208 if( !strncmp(path, RENDERFARM_FS_PREFIX, strlen(RENDERFARM_FS_PREFIX)) )
209 path += strlen(RENDERFARM_FS_PREFIX);
210 // ./path for relative list access
211 else if( dir_len > 0 && !strncmp(path, asset->path, dir_len) ) {
212 fprintf(stream, "."); path += dir_len;
214 fprintf(stream, "%s\n", path);
220 int FileList::read_list_header()
222 char string[BCTEXTLEN];
224 FILE *stream = fopen(asset->path, "r");
225 if( !stream ) return 1;
226 // Get information about the frames
228 if( feof(stream) || !fgets(string, BCTEXTLEN, stream) ) return 1;
229 } while(string[0] == '#' || string[0] == ' ' || isalpha(string[0]));
231 // Don't want a user configured frame rate to get destroyed
232 if(asset->frame_rate == 0)
233 asset->frame_rate = atof(string);
236 if( feof(stream) || !fgets(string, BCTEXTLEN, stream) ) return 1;
237 } while(string[0] == '#' || string[0] == ' ');
238 if( (asset->width = atol(string)) <= 0 ) return 1;
241 if( feof(stream) || !fgets(string, BCTEXTLEN, stream) ) return 1;
242 } while(string[0] == '#' || string[0] == ' ');
243 if( (asset->height = atol(string)) <= 0 ) return 1;
245 asset->interlace_mode = ILACE_MODE_UNDETECTED;
247 asset->audio_data = 0;
248 asset->video_data = 1;
250 char prefix[BCTEXTLEN], *bp = prefix, *cp = strrchr(asset->path, '/');
251 for( int i=0, n=!cp ? 0 : cp-asset->path; i<n; ++i ) *bp++ = asset->path[i];
254 // Get all the paths, expand relative paths
256 while( !feof(stream) && fgets(string, BCTEXTLEN, stream) ) {
257 int len = strlen(string);
258 if( !len || string[0] == '#' || string[0] == ' ' ) continue;
259 if( string[len-1] == '\n' ) string[len-1] = 0;
260 char path[BCTEXTLEN], *pp = path, *ep = pp + sizeof(path)-1;
261 if( string[0] == '.' && string[1] == '/' && prefix[0] )
262 pp += snprintf(pp, ep-pp, "%s/", prefix);
263 snprintf(pp, ep-pp, "%s", string);
264 if( access(path, R_OK) && !missing++ )
265 eprintf(_("%s:no such file"), path);
266 path_list.append(cstrdup(path));
269 //for(int i = 0; i < path_list.total; i++) printf("%s\n", path_list.values[i]);
271 if( !(asset->video_length = path_list.total) )
272 eprintf(_("%s:\nlist empty"), asset->path);
274 eprintf(_("%s:\n%d files not found"), asset->path, missing);
278 int FileList::read_frame(VFrame *frame)
283 // printf("FileList::read_frame %d %d use_header=%d current_frame=%d total=%d\n",
286 // asset->use_header,
287 // file->current_frame,
290 if(file->current_frame < 0 ||
291 (asset->use_header && file->current_frame >= path_list.total &&
292 asset->format == list_type))
295 if(asset->format == list_type)
297 char string[BCTEXTLEN];
299 if(asset->use_header)
301 path = path_list.values[file->current_frame];
305 path = calculate_path(file->current_frame, string);
310 // Fix path for VFS. Not used anymore.
311 if(!strncmp(asset->path, RENDERFARM_FS_PREFIX, strlen(RENDERFARM_FS_PREFIX)))
312 sprintf(string, "%s%s", RENDERFARM_FS_PREFIX, path);
314 strcpy(string, path);
318 if(!use_path() || frame->get_color_model() == BC_COMPRESSED)
320 if(!(in = fopen(string, "rb"))) {
321 eprintf(_("Error while opening \"%s\" for reading. \n%m\n"), string);
326 stat(string, &ostat);
328 switch(frame->get_color_model())
331 frame->allocate_compressed_data(ostat.st_size);
332 frame->set_compressed_size(ostat.st_size);
333 (void)fread(frame->get_data(), ostat.st_size, 1, in);
336 data->allocate_compressed_data(ostat.st_size);
337 data->set_compressed_size(ostat.st_size);
338 (void)fread(data->get_data(), ostat.st_size, 1, in);
339 result = read_frame(frame, data);
348 //printf("FileList::read_frame %d %s\n", __LINE__, string);
349 result = read_frame(frame, string);
354 asset->single_frame = 1;
355 // Allocate and decompress single frame into new temporary
356 //printf("FileList::read_frame %d\n", frame->get_color_model());
357 if( !temp || temp->get_color_model() != frame->get_color_model() ) {
358 delete temp; temp = 0;
359 int aw = asset->actual_width, ah = asset->actual_height;
360 int color_model = frame->get_color_model();
361 switch( color_model ) {
365 aw = (aw+1) & ~1; ah = (ah+1) & ~1;
369 aw = (aw+3) & ~3; ah = (ah+3) & ~3;
372 if( !use_path() || color_model == BC_COMPRESSED ) {
373 FILE *fd = fopen(asset->path, "rb");
376 stat(asset->path, &ostat);
378 switch(frame->get_color_model()) {
380 frame->allocate_compressed_data(ostat.st_size);
381 frame->set_compressed_size(ostat.st_size);
382 (void)fread(frame->get_data(), ostat.st_size, 1, fd);
385 data->allocate_compressed_data(ostat.st_size);
386 data->set_compressed_size(ostat.st_size);
387 (void)fread(data->get_data(), ostat.st_size, 1, fd);
388 temp = new VFrame(aw, ah, color_model);
389 read_frame(temp, data);
396 eprintf(_("Error while opening \"%s\" for reading. \n%m\n"), asset->path);
401 temp = new VFrame(aw, ah, color_model);
402 read_frame(temp, asset->path);
406 if(!temp) return result;
408 //printf("FileList::read_frame frame=%d temp=%d\n",
409 // frame->get_color_model(), // temp->get_color_model());
410 frame->transfer_from(temp);
414 // printf("FileList::read_frame %d %d\n", __LINE__, result);
416 // if(frame->get_y())
417 // for(int i = 0; i < 100000; i++)
419 // frame->get_y()[i] = 0xff;
421 // if(frame->get_rows())
422 // for(int i = 0; i < 100000; i++)
424 // frame->get_rows()[0][i] = 0xff;
431 int FileList::write_frames(VFrame ***frames, int len)
435 //printf("FileList::write_frames 1\n");
436 if(frames[0][0]->get_color_model() == BC_COMPRESSED)
438 for(int i = 0; i < asset->layers && !return_value; i++)
440 for(int j = 0; j < len && !return_value; j++)
442 VFrame *frame = frames[i][j];
443 char *path = create_path(frame->get_number());
444 //printf("FileList::write_frames %d %jd\n", __LINE__, frame->get_number());
447 FILE *fd = fopen(path, "wb");
450 return_value = !fwrite(frames[i][j]->get_data(),
451 frames[i][j]->get_compressed_size(),
459 eprintf(_("Error while opening \"%s\" for writing. \n%m\n"), asset->path);
467 //printf("FileList::write_frames 2\n");
468 writer->write_frames(frames, len);
469 //printf("FileList::write_frames 100\n");
482 void FileList::add_return_value(int amount)
484 table_lock->lock("FileList::add_return_value");
485 return_value += amount;
486 table_lock->unlock();
489 char* FileList::calculate_path(int number, char *string)
491 // Synthesize filename.
492 // If a header is used, the filename number must be in a different location.
493 if(asset->use_header)
496 strcpy(string, asset->path);
497 for(k = strlen(string) - 1; k > 0 && string[k] != '.'; k--)
499 if(k <= 0) k = strlen(string);
501 sprintf(&string[k], "%06d%s",
506 // Without a header, the original filename can be altered.
508 Render::create_filename(string,
518 char* FileList::create_path(int number_override)
520 if(asset->format != list_type) return asset->path;
522 table_lock->lock("FileList::create_path");
527 char output[BCTEXTLEN];
528 if(file->current_frame >= path_list.total || !asset->use_header)
531 if(number_override < 0)
532 number = file->current_frame++;
535 number = number_override;
536 file->current_frame++;
539 if(!asset->use_header)
541 number += first_number;
544 calculate_path(number, output);
546 path = new char[strlen(output) + 1];
547 strcpy(path, output);
548 path_list.append(path);
552 // Overwrite an old path
553 path = path_list.values[file->current_frame];
557 table_lock->unlock();
562 FrameWriterUnit* FileList::new_writer_unit(FrameWriter *writer)
564 return new FrameWriterUnit(writer);
567 int64_t FileList::get_memory_usage()
570 if(data) result += data->get_compressed_allocated();
571 if(temp) result += temp->get_data_size();
572 // printf("FileList::get_memory_usage %d %p %s %jd\n",
575 // file->asset->path,
580 int FileList::get_units()
582 return !writer ? 0 : writer->get_total_clients();
585 FrameWriterUnit* FileList::get_unit(int number)
587 return !writer ? 0 : (FrameWriterUnit*)writer->get_client(number);
590 int FileList::use_path()
600 FrameWriterPackage::FrameWriterPackage()
604 FrameWriterPackage::~FrameWriterPackage()
618 FrameWriterUnit::FrameWriterUnit(FrameWriter *server)
621 // Don't use server here since subclasses call this with no server.
622 this->server = server;
626 FrameWriterUnit::~FrameWriterUnit()
631 void FrameWriterUnit::process_package(LoadPackage *package)
633 //printf("FrameWriterUnit::process_package 1\n");
634 FrameWriterPackage *ptr = (FrameWriterPackage*)package;
638 //printf("FrameWriterUnit::process_package 2 %s\n", ptr->path);
639 if(!(file = fopen(ptr->path, "wb")))
641 eprintf(_("Error while opening \"%s\" for writing. \n%m\n"), ptr->path);
644 //printf("FrameWriterUnit::process_package 3");
647 int result = server->file->write_frame(ptr->input, output, this);
649 //printf("FrameWriterUnit::process_package 4 %s %d\n", ptr->path, output->get_compressed_size());
650 if(!result) result = !fwrite(output->get_data(), output->get_compressed_size(), 1, file);
651 //TRACE("FrameWriterUnit::process_package 4");
653 //TRACE("FrameWriterUnit::process_package 5");
655 server->file->add_return_value(result);
656 //TRACE("FrameWriterUnit::process_package 6");
669 FrameWriter::FrameWriter(FileList *file, int cpus)
670 : LoadServer(cpus, 0)
676 FrameWriter::~FrameWriter()
680 void FrameWriter::init_packages()
682 for(int i = 0, layer = 0, number = 0;
683 i < get_total_packages();
686 FrameWriterPackage *package = (FrameWriterPackage*)get_package(i);
687 package->input = frames[layer][number];
688 package->path = file->create_path(package->input->get_number());
689 // printf("FrameWriter::init_packages 1 %p %d %s\n",
691 // package->input->get_number(),
702 void FrameWriter::write_frames(VFrame ***frames, int len)
704 this->frames = frames;
706 set_package_count(len * file->asset->layers);
711 LoadClient* FrameWriter::new_client()
713 return file->new_writer_unit(this);
716 LoadPackage* FrameWriter::new_package()
718 return new FrameWriterPackage;