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 int width = asset->width;
136 int height = asset->height;
137 result = read_frame_header(asset->path);
138 asset->actual_width = asset->width;
139 if( width ) asset->width = width;
140 asset->actual_height = asset->height;
141 if( height ) asset->height = height;
143 if( !asset->frame_rate )
144 asset->frame_rate = 1;
145 asset->video_length = -1;
150 eprintf(_("Error while opening \"%s\" for reading. \n%m\n"), asset->path);
154 Render::get_starting_number(asset->path,
155 first_number, number_start, number_digits, 6);
160 file->current_frame = 0;
161 // Compressed data storage
168 int FileList::close_file()
170 // path_list.total, asset->format, list_type, wr);
171 if(asset->format == list_type && path_list.total)
173 if(file->wr && asset->use_header) write_list_header();
174 path_list.remove_all_objects();
176 if(data) delete data;
177 if(writer) delete writer;
178 if(temp) delete temp;
181 FileBase::close_file();
185 int FileList::write_list_header()
187 FILE *stream = fopen(asset->path, "w");
188 if( !stream ) return 1;
189 // Use sprintf instead of fprintf for VFS.
190 fprintf(stream, "%s\n", list_prefix);
191 fprintf(stream, "# First line is always %s\n", list_prefix);
192 fprintf(stream, "# Frame rate:\n");
193 fprintf(stream, "%f\n", asset->frame_rate);
194 fprintf(stream, "# Width:\n");
195 fprintf(stream, "%d\n", asset->width);
196 fprintf(stream, "# Height:\n");
197 fprintf(stream, "%d\n", asset->height);
198 fprintf(stream, "# List of image files follows\n");
200 char *cp = strrchr(asset->path, '/');
201 int dir_len = !cp ? 0 : cp - asset->path;
203 for(int i = 0; i < path_list.total; i++) {
204 const char *path = path_list.values[i];
205 // Fix path for VFS but leave leading slash
206 if( !strncmp(path, RENDERFARM_FS_PREFIX, strlen(RENDERFARM_FS_PREFIX)) )
207 path += strlen(RENDERFARM_FS_PREFIX);
208 // ./path for relative list access
209 else if( dir_len > 0 && !strncmp(path, asset->path, dir_len) ) {
210 fprintf(stream, "."); path += dir_len;
212 fprintf(stream, "%s\n", path);
218 int FileList::read_list_header()
220 char string[BCTEXTLEN];
222 FILE *stream = fopen(asset->path, "r");
223 if( !stream ) return 1;
224 // Get information about the frames
226 if( feof(stream) || !fgets(string, BCTEXTLEN, stream) ) return 1;
227 } while(string[0] == '#' || string[0] == ' ' || isalpha(string[0]));
229 // Don't want a user configured frame rate to get destroyed
230 if(asset->frame_rate == 0)
231 asset->frame_rate = atof(string);
234 if( feof(stream) || !fgets(string, BCTEXTLEN, stream) ) return 1;
235 } while(string[0] == '#' || string[0] == ' ');
236 if( (asset->width = atol(string)) <= 0 ) return 1;
239 if( feof(stream) || !fgets(string, BCTEXTLEN, stream) ) return 1;
240 } while(string[0] == '#' || string[0] == ' ');
241 if( (asset->height = atol(string)) <= 0 ) return 1;
243 asset->interlace_mode = ILACE_MODE_UNDETECTED;
245 asset->audio_data = 0;
246 asset->video_data = 1;
248 char prefix[BCTEXTLEN], *bp = prefix, *cp = strrchr(asset->path, '/');
249 for( int i=0, n=!cp ? 0 : cp-asset->path; i<n; ++i ) *bp++ = asset->path[i];
252 // Get all the paths, expand relative paths
254 while( !feof(stream) && fgets(string, BCTEXTLEN, stream) ) {
255 int len = strlen(string);
256 if( !len || string[0] == '#' || string[0] == ' ' ) continue;
257 if( string[len-1] == '\n' ) string[len-1] = 0;
258 char path[BCTEXTLEN], *pp = path, *ep = pp + sizeof(path)-1;
259 if( string[0] == '.' && string[1] == '/' && prefix[0] )
260 pp += snprintf(pp, ep-pp, "%s/", prefix);
261 snprintf(pp, ep-pp, "%s", string);
262 if( access(path, R_OK) && !missing++ )
263 eprintf(_("%s:no such file"), path);
264 path_list.append(cstrdup(path));
267 //for(int i = 0; i < path_list.total; i++) printf("%s\n", path_list.values[i]);
269 if( !(asset->video_length = path_list.total) )
270 eprintf(_("%s:\nlist empty"), asset->path);
272 eprintf(_("%s:\n%d files not found"), asset->path, missing);
276 int FileList::read_frame(VFrame *frame)
281 // printf("FileList::read_frame %d %d use_header=%d current_frame=%d total=%d\n",
284 // asset->use_header,
285 // file->current_frame,
288 if(file->current_frame < 0 ||
289 (asset->use_header && file->current_frame >= path_list.total &&
290 asset->format == list_type))
293 if(asset->format == list_type)
295 char string[BCTEXTLEN];
297 if(asset->use_header)
299 path = path_list.values[file->current_frame];
303 path = calculate_path(file->current_frame, string);
308 // Fix path for VFS. Not used anymore.
309 if(!strncmp(asset->path, RENDERFARM_FS_PREFIX, strlen(RENDERFARM_FS_PREFIX)))
310 sprintf(string, "%s%s", RENDERFARM_FS_PREFIX, path);
312 strcpy(string, path);
316 if(!use_path() || frame->get_color_model() == BC_COMPRESSED)
318 if(!(in = fopen(string, "rb"))) {
319 eprintf(_("Error while opening \"%s\" for reading. \n%m\n"), string);
324 stat(string, &ostat);
326 switch(frame->get_color_model())
329 frame->allocate_compressed_data(ostat.st_size);
330 frame->set_compressed_size(ostat.st_size);
331 (void)fread(frame->get_data(), ostat.st_size, 1, in);
334 data->allocate_compressed_data(ostat.st_size);
335 data->set_compressed_size(ostat.st_size);
336 (void)fread(data->get_data(), ostat.st_size, 1, in);
337 result = read_frame(frame, data);
346 //printf("FileList::read_frame %d %s\n", __LINE__, string);
347 result = read_frame(frame, string);
352 asset->single_frame = 1;
353 // Allocate and decompress single frame into new temporary
354 //printf("FileList::read_frame %d\n", frame->get_color_model());
355 if(!temp || temp->get_color_model() != frame->get_color_model())
357 if(temp) delete temp;
361 if(!use_path() || frame->get_color_model() == BC_COMPRESSED)
363 FILE *fd = fopen(asset->path, "rb");
367 stat(asset->path, &ostat);
369 switch(frame->get_color_model())
372 frame->allocate_compressed_data(ostat.st_size);
373 frame->set_compressed_size(ostat.st_size);
374 (void)fread(frame->get_data(), ostat.st_size, 1, fd);
377 data->allocate_compressed_data(ostat.st_size);
378 data->set_compressed_size(ostat.st_size);
379 (void)fread(data->get_data(), ostat.st_size, 1, fd);
380 temp = new VFrame(asset->actual_width, asset->actual_height,
381 frame->get_color_model(), 0);
382 read_frame(temp, data);
390 eprintf(_("Error while opening \"%s\" for reading. \n%m\n"), asset->path);
396 temp = new VFrame(asset->actual_width, asset->actual_height,
397 frame->get_color_model(), 0);
398 read_frame(temp, asset->path);
402 if(!temp) return result;
404 //printf("FileList::read_frame frame=%d temp=%d\n",
405 // frame->get_color_model(), // temp->get_color_model());
406 frame->transfer_from(temp);
410 // printf("FileList::read_frame %d %d\n", __LINE__, result);
412 // if(frame->get_y())
413 // for(int i = 0; i < 100000; i++)
415 // frame->get_y()[i] = 0xff;
417 // if(frame->get_rows())
418 // for(int i = 0; i < 100000; i++)
420 // frame->get_rows()[0][i] = 0xff;
427 int FileList::write_frames(VFrame ***frames, int len)
431 //printf("FileList::write_frames 1\n");
432 if(frames[0][0]->get_color_model() == BC_COMPRESSED)
434 for(int i = 0; i < asset->layers && !return_value; i++)
436 for(int j = 0; j < len && !return_value; j++)
438 VFrame *frame = frames[i][j];
439 char *path = create_path(frame->get_number());
440 //printf("FileList::write_frames %d %jd\n", __LINE__, frame->get_number());
443 FILE *fd = fopen(path, "wb");
446 return_value = !fwrite(frames[i][j]->get_data(),
447 frames[i][j]->get_compressed_size(),
455 eprintf(_("Error while opening \"%s\" for writing. \n%m\n"), asset->path);
463 //printf("FileList::write_frames 2\n");
464 writer->write_frames(frames, len);
465 //printf("FileList::write_frames 100\n");
478 void FileList::add_return_value(int amount)
480 table_lock->lock("FileList::add_return_value");
481 return_value += amount;
482 table_lock->unlock();
485 char* FileList::calculate_path(int number, char *string)
487 // Synthesize filename.
488 // If a header is used, the filename number must be in a different location.
489 if(asset->use_header)
492 strcpy(string, asset->path);
493 for(k = strlen(string) - 1; k > 0 && string[k] != '.'; k--)
495 if(k <= 0) k = strlen(string);
497 sprintf(&string[k], "%06d%s",
502 // Without a header, the original filename can be altered.
504 Render::create_filename(string,
514 char* FileList::create_path(int number_override)
516 if(asset->format != list_type) return asset->path;
518 table_lock->lock("FileList::create_path");
523 char output[BCTEXTLEN];
524 if(file->current_frame >= path_list.total || !asset->use_header)
527 if(number_override < 0)
528 number = file->current_frame++;
531 number = number_override;
532 file->current_frame++;
535 if(!asset->use_header)
537 number += first_number;
540 calculate_path(number, output);
542 path = new char[strlen(output) + 1];
543 strcpy(path, output);
544 path_list.append(path);
548 // Overwrite an old path
549 path = path_list.values[file->current_frame];
553 table_lock->unlock();
558 FrameWriterUnit* FileList::new_writer_unit(FrameWriter *writer)
560 return new FrameWriterUnit(writer);
563 int64_t FileList::get_memory_usage()
566 if(data) result += data->get_compressed_allocated();
567 if(temp) result += temp->get_data_size();
568 // printf("FileList::get_memory_usage %d %p %s %jd\n",
571 // file->asset->path,
576 int FileList::get_units()
578 return !writer ? 0 : writer->get_total_clients();
581 FrameWriterUnit* FileList::get_unit(int number)
583 return !writer ? 0 : (FrameWriterUnit*)writer->get_client(number);
586 int FileList::use_path()
596 FrameWriterPackage::FrameWriterPackage()
600 FrameWriterPackage::~FrameWriterPackage()
614 FrameWriterUnit::FrameWriterUnit(FrameWriter *server)
617 // Don't use server here since subclasses call this with no server.
618 this->server = server;
622 FrameWriterUnit::~FrameWriterUnit()
627 void FrameWriterUnit::process_package(LoadPackage *package)
629 //printf("FrameWriterUnit::process_package 1\n");
630 FrameWriterPackage *ptr = (FrameWriterPackage*)package;
634 //printf("FrameWriterUnit::process_package 2 %s\n", ptr->path);
635 if(!(file = fopen(ptr->path, "wb")))
637 eprintf(_("Error while opening \"%s\" for writing. \n%m\n"), ptr->path);
640 //printf("FrameWriterUnit::process_package 3");
643 int result = server->file->write_frame(ptr->input, output, this);
645 //printf("FrameWriterUnit::process_package 4 %s %d\n", ptr->path, output->get_compressed_size());
646 if(!result) result = !fwrite(output->get_data(), output->get_compressed_size(), 1, file);
647 //TRACE("FrameWriterUnit::process_package 4");
649 //TRACE("FrameWriterUnit::process_package 5");
651 server->file->add_return_value(result);
652 //TRACE("FrameWriterUnit::process_package 6");
665 FrameWriter::FrameWriter(FileList *file, int cpus)
666 : LoadServer(cpus, 0)
672 FrameWriter::~FrameWriter()
676 void FrameWriter::init_packages()
678 for(int i = 0, layer = 0, number = 0;
679 i < get_total_packages();
682 FrameWriterPackage *package = (FrameWriterPackage*)get_package(i);
683 package->input = frames[layer][number];
684 package->path = file->create_path(package->input->get_number());
685 // printf("FrameWriter::init_packages 1 %p %d %s\n",
687 // package->input->get_number(),
698 void FrameWriter::write_frames(VFrame ***frames, int len)
700 this->frames = frames;
702 set_package_count(len * file->asset->layers);
707 LoadClient* FrameWriter::new_client()
709 return file->new_writer_unit(this);
712 LoadPackage* FrameWriter::new_package()
714 return new FrameWriterPackage;