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"
44 FileList::FileList(Asset *asset,
46 const char *list_prefix,
47 const char *file_extension,
50 : FileBase(asset, file)
53 path_list.set_array_delete();
54 asset->video_data = 1;
55 this->list_prefix = list_prefix;
56 this->file_extension = file_extension;
57 this->frame_type = frame_type;
58 this->list_type = list_type;
59 table_lock = new Mutex("FileList::table_lock");
68 int FileList::reset_parameters_derived()
77 int FileList::open_file(int rd, int wr)
81 // skip header for write
84 // Frame files are created in write_frame and list index is created when
86 // Look for the starting number in the path but ignore the starting character
87 // and total digits since these are used by the header.
88 Render::get_starting_number(asset->path,
92 path_list.remove_all_objects();
93 writer = new FrameWriter(this,
94 asset->format == list_type ? file->cpus : 1);
99 // Determine type of file.
100 // Header isn't used for background rendering, in which case everything known
101 // by the file encoder is known by the decoder.
102 //printf("FileList::open_file %d %d\n", __LINE__, asset->use_header);
103 if(asset->use_header)
105 FILE *stream = fopen(asset->path, "rb");
106 //printf("FileList::open_file %d asset->path=%s\n", __LINE__, asset->path);
109 char string[BCTEXTLEN];
110 (void)fread(string, strlen(list_prefix), 1, stream);
113 if(!strncasecmp(string, list_prefix, strlen(list_prefix)))
116 //printf("FileList::open_file %d\n", __LINE__);
117 asset->format = list_type;
119 // Open index here or get frame size from file.
120 result = read_list_header();
121 //printf("FileList::open_file %d %s\n", __LINE__, path_list.values[0]);
122 if(!result) result = read_frame_header(path_list.values[0]);
126 //printf("FileList::open_file 2\n", asset->use_header);
127 //printf("FileList::open_file %d\n", __LINE__);
128 asset->format = frame_type;
129 result = read_frame_header(asset->path);
131 if(!asset->frame_rate)
132 asset->frame_rate = 1;
133 asset->video_length = -1;
139 Render::get_starting_number(asset->path,
147 file->current_frame = 0;
148 // Compressed data storage
155 int FileList::close_file()
157 // path_list.total, asset->format, list_type, wr);
158 if(asset->format == list_type && path_list.total)
160 if(file->wr && asset->use_header) write_list_header();
161 path_list.remove_all_objects();
163 if(data) delete data;
164 if(writer) delete writer;
165 if(temp) delete temp;
168 FileBase::close_file();
172 int FileList::write_list_header()
174 FILE *stream = fopen(asset->path, "w");
175 // Use sprintf instead of fprintf for VFS.
176 char string[BCTEXTLEN];
177 sprintf(string, "%s\n", list_prefix);
178 fwrite(string, strlen(string), 1, stream);
179 sprintf(string, "# First line is always %s\n", list_prefix);
180 fwrite(string, strlen(string), 1, stream);
181 sprintf(string, "# Frame rate:\n");
182 fwrite(string, strlen(string), 1, stream);
183 sprintf(string, "%f\n", asset->frame_rate);
184 fwrite(string, strlen(string), 1, stream);
185 sprintf(string, "# Width:\n");
186 fwrite(string, strlen(string), 1, stream);
187 sprintf(string, "%d\n", asset->width);
188 fwrite(string, strlen(string), 1, stream);
189 sprintf(string, "# Height:\n");
190 fwrite(string, strlen(string), 1, stream);
191 sprintf(string, "%d\n", asset->height);
192 fwrite(string, strlen(string), 1, stream);
193 sprintf(string, "# List of image files follows\n");
194 fwrite(string, strlen(string), 1, stream);
196 for(int i = 0; i < path_list.total; i++)
198 // Fix path for VFS but leave leading slash
199 if(!strncmp(path_list.values[i], RENDERFARM_FS_PREFIX, strlen(RENDERFARM_FS_PREFIX)))
200 sprintf(string, "%s\n", path_list.values[i] + strlen(RENDERFARM_FS_PREFIX));
202 sprintf(string, "%s\n", path_list.values[i]);
203 fwrite(string, strlen(string), 1, stream);
209 int FileList::read_list_header()
211 char string[BCTEXTLEN];
213 FILE *stream = fopen(asset->path, "r");
214 if( !stream ) return 1;
215 // Get information about the frames
217 if( feof(stream) || !fgets(string, BCTEXTLEN, stream) ) return 1;
218 } while(string[0] == '#' || string[0] == ' ' || isalpha(string[0]));
220 // Don't want a user configured frame rate to get destroyed
221 if(asset->frame_rate == 0)
222 asset->frame_rate = atof(string);
225 if( feof(stream) || !fgets(string, BCTEXTLEN, stream) ) return 1;
226 } while(string[0] == '#' || string[0] == ' ');
227 if( (asset->width = atol(string)) <= 0 ) return 1;
230 if( feof(stream) || !fgets(string, BCTEXTLEN, stream) ) return 1;
231 } while(string[0] == '#' || string[0] == ' ');
232 if( (asset->height = atol(string)) <= 0 ) return 1;
234 asset->interlace_mode = ILACE_MODE_UNDETECTED;
236 asset->audio_data = 0;
237 asset->video_data = 1;
241 while(!feof(stream) && fgets(string, BCTEXTLEN, stream) ) {
242 int len = strlen(string);
243 if( !len || string[0] == '#' || string[0] == ' ') continue;
245 if( access(string,R_OK) && !missing++ )
246 eprintf(_("%s:no such file"), string);
247 path_list.append(cstrdup(string));
250 //for(int i = 0; i < path_list.total; i++) printf("%s\n", path_list.values[i]);
252 if( !(asset->video_length = path_list.total) )
253 eprintf(_("%s:\nlist empty"), asset->path);
255 eprintf(_("%s:\n%d files not found"), asset->path, missing);
259 int FileList::read_frame(VFrame *frame)
264 // printf("FileList::read_frame %d %d use_header=%d current_frame=%d total=%d\n",
267 // asset->use_header,
268 // file->current_frame,
271 if(file->current_frame < 0 ||
272 (asset->use_header && file->current_frame >= path_list.total &&
273 asset->format == list_type))
276 if(asset->format == list_type)
278 char string[BCTEXTLEN];
280 if(asset->use_header)
282 path = path_list.values[file->current_frame];
286 path = calculate_path(file->current_frame, string);
292 if(!strncmp(asset->path, RENDERFARM_FS_PREFIX, strlen(RENDERFARM_FS_PREFIX)))
293 sprintf(string, "%s%s", RENDERFARM_FS_PREFIX, path);
295 strcpy(string, path);
299 if(!use_path() || frame->get_color_model() == BC_COMPRESSED)
301 if(!(in = fopen(string, "rb"))) {
302 eprintf(_("Error while opening \"%s\" for reading. \n%m\n"), string);
307 stat(string, &ostat);
309 switch(frame->get_color_model())
312 frame->allocate_compressed_data(ostat.st_size);
313 frame->set_compressed_size(ostat.st_size);
314 (void)fread(frame->get_data(), ostat.st_size, 1, in);
317 data->allocate_compressed_data(ostat.st_size);
318 data->set_compressed_size(ostat.st_size);
319 (void)fread(data->get_data(), ostat.st_size, 1, in);
320 result = read_frame(frame, data);
329 //printf("FileList::read_frame %d %s\n", __LINE__, string);
330 result = read_frame(frame, string);
335 asset->single_frame = 1;
336 // Allocate and decompress single frame into new temporary
337 //printf("FileList::read_frame %d\n", frame->get_color_model());
338 if(!temp || temp->get_color_model() != frame->get_color_model())
340 if(temp) delete temp;
344 if(!use_path() || frame->get_color_model() == BC_COMPRESSED)
346 FILE *fd = fopen(asset->path, "rb");
350 stat(asset->path, &ostat);
352 switch(frame->get_color_model())
355 frame->allocate_compressed_data(ostat.st_size);
356 frame->set_compressed_size(ostat.st_size);
357 (void)fread(frame->get_data(), ostat.st_size, 1, fd);
360 data->allocate_compressed_data(ostat.st_size);
361 data->set_compressed_size(ostat.st_size);
362 (void)fread(data->get_data(), ostat.st_size, 1, fd);
367 frame->get_color_model(),
369 read_frame(temp, data);
377 eprintf(_("Error while opening \"%s\" for reading. \n%m\n"), asset->path);
387 frame->get_color_model(),
389 read_frame(temp, asset->path);
393 if(!temp) return result;
395 // printf("FileList::read_frame frame=%d temp=%d\n",
396 // frame->get_color_model(),
397 // temp->get_color_model());
398 if(frame->get_color_model() == temp->get_color_model())
400 frame->copy_from(temp);
405 BC_CModels::transfer(frame->get_rows(), /* Leave NULL if non existent */
407 frame->get_y(), /* Leave NULL if non existent */
410 temp->get_y(), /* Leave NULL if non existent */
413 0, /* Dimensions to capture from input frame */
417 0, /* Dimensions to project on output frame */
421 temp->get_color_model(),
422 frame->get_color_model(),
423 0, /* When transfering BC_RGBA8888 to non-alpha this is the background color in 0xRRGGBB hex */
424 temp->get_w(), /* For planar use the luma rowspan */
430 // printf("FileList::read_frame %d %d\n", __LINE__, result);
432 // if(frame->get_y())
433 // for(int i = 0; i < 100000; i++)
435 // frame->get_y()[i] = 0xff;
437 // if(frame->get_rows())
438 // for(int i = 0; i < 100000; i++)
440 // frame->get_rows()[0][i] = 0xff;
447 int FileList::write_frames(VFrame ***frames, int len)
451 //printf("FileList::write_frames 1\n");
452 if(frames[0][0]->get_color_model() == BC_COMPRESSED)
454 for(int i = 0; i < asset->layers && !return_value; i++)
456 for(int j = 0; j < len && !return_value; j++)
458 VFrame *frame = frames[i][j];
459 char *path = create_path(frame->get_number());
460 //printf("FileList::write_frames %d %jd\n", __LINE__, frame->get_number());
463 FILE *fd = fopen(path, "wb");
466 return_value = !fwrite(frames[i][j]->get_data(),
467 frames[i][j]->get_compressed_size(),
475 eprintf(_("Error while opening \"%s\" for writing. \n%m\n"), asset->path);
483 //printf("FileList::write_frames 2\n");
484 writer->write_frames(frames, len);
485 //printf("FileList::write_frames 100\n");
498 void FileList::add_return_value(int amount)
500 table_lock->lock("FileList::add_return_value");
501 return_value += amount;
502 table_lock->unlock();
505 char* FileList::calculate_path(int number, char *string)
507 // Synthesize filename.
508 // If a header is used, the filename number must be in a different location.
509 if(asset->use_header)
512 strcpy(string, asset->path);
513 for(k = strlen(string) - 1; k > 0 && string[k] != '.'; k--)
515 if(k <= 0) k = strlen(string);
517 sprintf(&string[k], "%06d%s",
522 // Without a header, the original filename can be altered.
524 Render::create_filename(string,
534 char* FileList::create_path(int number_override)
536 if(asset->format != list_type) return asset->path;
538 table_lock->lock("FileList::create_path");
543 char output[BCTEXTLEN];
544 if(file->current_frame >= path_list.total || !asset->use_header)
547 if(number_override < 0)
548 number = file->current_frame++;
551 number = number_override;
552 file->current_frame++;
555 if(!asset->use_header)
557 number += first_number;
560 calculate_path(number, output);
562 path = new char[strlen(output) + 1];
563 strcpy(path, output);
564 path_list.append(path);
568 // Overwrite an old path
569 path = path_list.values[file->current_frame];
573 table_lock->unlock();
578 FrameWriterUnit* FileList::new_writer_unit(FrameWriter *writer)
580 return new FrameWriterUnit(writer);
583 int64_t FileList::get_memory_usage()
586 if(data) result += data->get_compressed_allocated();
587 if(temp) result += temp->get_data_size();
588 // printf("FileList::get_memory_usage %d %p %s %jd\n",
591 // file->asset->path,
596 int FileList::get_units()
598 return !writer ? 0 : writer->get_total_clients();
601 FrameWriterUnit* FileList::get_unit(int number)
603 return !writer ? 0 : (FrameWriterUnit*)writer->get_client(number);
606 int FileList::use_path()
616 FrameWriterPackage::FrameWriterPackage()
620 FrameWriterPackage::~FrameWriterPackage()
634 FrameWriterUnit::FrameWriterUnit(FrameWriter *server)
637 // Don't use server here since subclasses call this with no server.
638 this->server = server;
642 FrameWriterUnit::~FrameWriterUnit()
647 void FrameWriterUnit::process_package(LoadPackage *package)
649 //printf("FrameWriterUnit::process_package 1\n");
650 FrameWriterPackage *ptr = (FrameWriterPackage*)package;
654 //printf("FrameWriterUnit::process_package 2 %s\n", ptr->path);
655 if(!(file = fopen(ptr->path, "wb")))
657 eprintf(_("Error while opening \"%s\" for writing. \n%m\n"), ptr->path);
660 //printf("FrameWriterUnit::process_package 3");
663 int result = server->file->write_frame(ptr->input, output, this);
665 //printf("FrameWriterUnit::process_package 4 %s %d\n", ptr->path, output->get_compressed_size());
666 if(!result) result = !fwrite(output->get_data(), output->get_compressed_size(), 1, file);
667 //TRACE("FrameWriterUnit::process_package 4");
669 //TRACE("FrameWriterUnit::process_package 5");
671 server->file->add_return_value(result);
672 //TRACE("FrameWriterUnit::process_package 6");
685 FrameWriter::FrameWriter(FileList *file, int cpus)
686 : LoadServer(cpus, 0)
692 FrameWriter::~FrameWriter()
696 void FrameWriter::init_packages()
698 for(int i = 0, layer = 0, number = 0;
699 i < get_total_packages();
702 FrameWriterPackage *package = (FrameWriterPackage*)get_package(i);
703 package->input = frames[layer][number];
704 package->path = file->create_path(package->input->get_number());
705 // printf("FrameWriter::init_packages 1 %p %d %s\n",
707 // package->input->get_number(),
718 void FrameWriter::write_frames(VFrame ***frames, int len)
720 this->frames = frames;
722 set_package_count(len * file->asset->layers);
727 LoadClient* FrameWriter::new_client()
729 return file->new_writer_unit(this);
732 LoadPackage* FrameWriter::new_package()
734 return new FrameWriterPackage;