6 #include "interlacemodes.h"
10 #include "renderfarmfsserver.inc"
21 FileList::FileList(Asset *asset,
27 : FileBase(asset, file)
30 asset->video_data = 1;
31 this->list_prefix = list_prefix;
32 this->file_extension = file_extension;
33 this->frame_type = frame_type;
34 this->list_type = list_type;
35 table_lock = new Mutex("FileList::table_lock");
44 int FileList::reset_parameters_derived()
52 int FileList::open_file(int rd, int wr)
58 // skip header for write
61 // Frame files are created in write_frame and list index is created when
63 // Look for the starting number in the path but ignore the starting character
64 // and total digits since these are used by the header.
65 Render::get_starting_number(asset->path,
69 path_list.remove_all_objects();
70 writer = new FrameWriter(this,
71 asset->format == list_type ? file->cpus : 1);
76 // Determine type of file.
77 // Header isn't used for background rendering, in which case everything known
78 // by the file encoder is known by the decoder.
79 //printf("FileList::open_file 1 %d\n", asset->use_header);
82 FILE *stream = fopen(asset->path, "rb");
85 char string[BCTEXTLEN];
86 fread(string, strlen(list_prefix), 1, stream);
89 if(!strncasecmp(string, list_prefix, strlen(list_prefix)))
92 asset->format = list_type;
94 // Open index here or get frame size from file.
95 result = read_list_header();
96 if(!result) result = read_frame_header(path_list.values[0]);
100 //printf("FileList::open_file 2\n", asset->use_header);
101 asset->format = frame_type;
102 result = read_frame_header(asset->path);
104 if(!asset->frame_rate)
105 asset->frame_rate = 1;
106 asset->video_length = -1;
112 Render::get_starting_number(asset->path,
120 file->current_frame = 0;
121 // Compressed data storage
128 int FileList::close_file()
130 // path_list.total, asset->format, list_type, wr);
131 if(asset->format == list_type && path_list.total)
133 if(wr && asset->use_header) write_list_header();
134 path_list.remove_all_objects();
136 if(data) delete data;
137 if(writer) delete writer;
138 if(temp) delete temp;
141 FileBase::close_file();
145 int FileList::write_list_header()
147 FILE *stream = fopen(asset->path, "w");
148 // Use sprintf instead of fprintf for VFS.
149 char string[BCTEXTLEN];
150 sprintf(string, "%s\n", list_prefix);
151 fwrite(string, strlen(string), 1, stream);
152 sprintf(string, "# First line is always %s\n", list_prefix);
153 fwrite(string, strlen(string), 1, stream);
154 sprintf(string, "# Frame rate:\n");
155 fwrite(string, strlen(string), 1, stream);
156 sprintf(string, "%f\n", asset->frame_rate);
157 fwrite(string, strlen(string), 1, stream);
158 sprintf(string, "# Width:\n");
159 fwrite(string, strlen(string), 1, stream);
160 sprintf(string, "%d\n", asset->width);
161 fwrite(string, strlen(string), 1, stream);
162 sprintf(string, "# Height:\n");
163 fwrite(string, strlen(string), 1, stream);
164 sprintf(string, "%d\n", asset->height);
165 fwrite(string, strlen(string), 1, stream);
166 sprintf(string, "# List of image files follows\n");
167 fwrite(string, strlen(string), 1, stream);
169 for(int i = 0; i < path_list.total; i++)
171 // Fix path for VFS but leave leading slash
172 if(!strncmp(path_list.values[i], RENDERFARM_FS_PREFIX, strlen(RENDERFARM_FS_PREFIX)))
173 sprintf(string, "%s\n", path_list.values[i] + strlen(RENDERFARM_FS_PREFIX));
175 sprintf(string, "%s\n", path_list.values[i]);
176 fwrite(string, strlen(string), 1, stream);
182 int FileList::read_list_header()
184 char string[BCTEXTLEN], *new_entry;
186 FILE *stream = fopen(asset->path, "r");
191 // Get information about the frames
194 fgets(string, BCTEXTLEN, stream);
195 }while(!feof(stream) && (string[0] == '#' || string[0] == ' ' || isalpha(string[0])));
197 // Don't want a user configured frame rate to get destroyed
198 if(asset->frame_rate == 0)
199 asset->frame_rate = atof(string);
203 fgets(string, BCTEXTLEN, stream);
204 }while(!feof(stream) && (string[0] == '#' || string[0] == ' '));
205 asset->width = atol(string);
209 fgets(string, BCTEXTLEN, stream);
210 }while(!feof(stream) && (string[0] == '#' || string[0] == ' '));
211 asset->height = atol(string);
213 asset->interlace_mode = BC_ILACE_MODE_UNDETECTED; // May be good to store the info in the list?
215 asset->audio_data = 0;
216 asset->video_data = 1;
221 fgets(string, BCTEXTLEN, stream);
222 if(strlen(string) && string[0] != '#' && string[0] != ' ' && !feof(stream))
224 string[strlen(string) - 1] = 0;
225 path_list.append(new_entry = new char[strlen(string) + 1]);
226 strcpy(new_entry, string);
230 //for(int i = 0; i < path_list.total; i++) printf("%s\n", path_list.values[i]);
232 asset->video_length = path_list.total;
240 int FileList::read_frame(VFrame *frame)
243 if(file->current_frame < 0 ||
244 (asset->use_header && file->current_frame >= path_list.total &&
245 asset->format == list_type))
248 if(asset->format == list_type)
250 char string[BCTEXTLEN];
252 if(asset->use_header)
254 path = path_list.values[file->current_frame];
258 path = calculate_path(file->current_frame, string);
264 if(!strncmp(asset->path, RENDERFARM_FS_PREFIX, strlen(RENDERFARM_FS_PREFIX)))
265 sprintf(string, "%s%s", RENDERFARM_FS_PREFIX, path);
267 strcpy(string, path);
269 if(!(in = fopen(string, "rb")))
271 fprintf(stderr, "FileList::read_frame %s: %s\n", string, strerror(errno));
276 stat(string, &ostat);
278 switch(frame->get_color_model())
281 frame->allocate_compressed_data(ostat.st_size);
282 frame->set_compressed_size(ostat.st_size);
283 fread(frame->get_data(), ostat.st_size, 1, in);
286 data->allocate_compressed_data(ostat.st_size);
287 data->set_compressed_size(ostat.st_size);
288 fread(data->get_data(), ostat.st_size, 1, in);
289 result = read_frame(frame, data);
299 // Allocate and decompress once into temporary
300 //printf("FileList::read_frame %d\n", frame->get_color_model());
301 if(!temp || temp->get_color_model() != frame->get_color_model())
303 if(temp) delete temp;
306 FILE *fd = fopen(asset->path, "rb");
310 stat(asset->path, &ostat);
312 switch(frame->get_color_model())
315 frame->allocate_compressed_data(ostat.st_size);
316 frame->set_compressed_size(ostat.st_size);
317 fread(frame->get_data(), ostat.st_size, 1, fd);
320 data->allocate_compressed_data(ostat.st_size);
321 data->set_compressed_size(ostat.st_size);
322 fread(data->get_data(), ostat.st_size, 1, fd);
326 frame->get_color_model());
327 read_frame(temp, data);
335 fprintf(stderr, "FileList::read_frame %s: %s\n", asset->path, strerror(errno));
340 if(!temp) return result;
342 // printf("FileList::read_frame frame=%d temp=%d\n",
343 // frame->get_color_model(),
344 // temp->get_color_model());
345 if(frame->get_color_model() == temp->get_color_model())
347 frame->copy_from(temp);
352 cmodel_transfer(frame->get_rows(), /* Leave NULL if non existent */
354 frame->get_y(), /* Leave NULL if non existent */
357 temp->get_y(), /* Leave NULL if non existent */
360 0, /* Dimensions to capture from input frame */
364 0, /* Dimensions to project on output frame */
368 temp->get_color_model(),
369 frame->get_color_model(),
370 0, /* When transfering BC_RGBA8888 to non-alpha this is the background color in 0xRRGGBB hex */
371 temp->get_w(), /* For planar use the luma rowspan */
377 //printf("FileList::read_frame 5 %d\n", result);
383 int FileList::write_frames(VFrame ***frames, int len)
387 //printf("FileList::write_frames 1\n");
388 if(frames[0][0]->get_color_model() == BC_COMPRESSED)
390 for(int i = 0; i < asset->layers && !return_value; i++)
392 for(int j = 0; j < len && !return_value; j++)
394 VFrame *frame = frames[i][j];
395 char *path = create_path(frame->get_number());
397 FILE *fd = fopen(path, "wb");
400 return_value = !fwrite(frames[i][j]->get_data(),
401 frames[i][j]->get_compressed_size(),
409 printf("FileList::write_frames %s: %s\n", path, strerror(errno));
417 //printf("FileList::write_frames 2\n");
418 writer->write_frames(frames, len);
419 //printf("FileList::write_frames 100\n");
432 void FileList::add_return_value(int amount)
434 table_lock->lock("FileList::add_return_value");
435 return_value += amount;
436 table_lock->unlock();
439 char* FileList::calculate_path(int number, char *string)
441 // Synthesize filename.
442 // If a header is used, the filename number must be in a different location.
443 if(asset->use_header)
446 strcpy(string, asset->path);
447 for(k = strlen(string) - 1; k > 0 && string[k] != '.'; k--)
449 if(k <= 0) k = strlen(string);
451 sprintf(&string[k], "%06d%s",
456 // Without a header, the original filename can be altered.
458 Render::create_filename(string,
468 char* FileList::create_path(int number_override)
470 if(asset->format != list_type) return asset->path;
472 table_lock->lock("FileList::create_path");
477 char output[BCTEXTLEN];
478 if(file->current_frame >= path_list.total || !asset->use_header)
481 if(number_override < 0)
482 number = file->current_frame++;
485 number = number_override;
486 file->current_frame++;
489 if(!asset->use_header)
491 number += first_number;
494 calculate_path(number, output);
496 path = new char[strlen(output) + 1];
497 strcpy(path, output);
498 path_list.append(path);
502 // Overwrite an old path
503 path = path_list.values[file->current_frame];
507 table_lock->unlock();
512 FrameWriterUnit* FileList::new_writer_unit(FrameWriter *writer)
514 return new FrameWriterUnit(writer);
517 int64_t FileList::get_memory_usage()
520 if(data) result += data->get_compressed_allocated();
521 if(temp) result += temp->get_data_size();
525 int FileList::get_units()
527 if(writer) return writer->get_total_clients();
531 FrameWriterUnit* FileList::get_unit(int number)
533 if(writer) return (FrameWriterUnit*)writer->get_client(number);
542 FrameWriterPackage::FrameWriterPackage()
546 FrameWriterPackage::~FrameWriterPackage()
560 FrameWriterUnit::FrameWriterUnit(FrameWriter *server)
563 // Don't use server here since subclasses call this with no server.
564 this->server = server;
568 FrameWriterUnit::~FrameWriterUnit()
573 void FrameWriterUnit::process_package(LoadPackage *package)
575 //printf("FrameWriterUnit::process_package 1\n");
576 FrameWriterPackage *ptr = (FrameWriterPackage*)package;
580 //printf("FrameWriterUnit::process_package 2 %s\n", ptr->path);
581 if(!(file = fopen(ptr->path, "wb")))
583 printf("FrameWriterUnit::process_package %s: %s\n",
588 //printf("FrameWriterUnit::process_package 3");
591 int result = server->file->write_frame(ptr->input, output, this);
593 //printf("FrameWriterUnit::process_package 4 %s %d\n", ptr->path, output->get_compressed_size());
594 if(!result) result = !fwrite(output->get_data(), output->get_compressed_size(), 1, file);
595 //TRACE("FrameWriterUnit::process_package 4");
597 //TRACE("FrameWriterUnit::process_package 5");
599 server->file->add_return_value(result);
600 //TRACE("FrameWriterUnit::process_package 6");
613 FrameWriter::FrameWriter(FileList *file, int cpus)
614 : LoadServer(cpus, 0)
620 FrameWriter::~FrameWriter()
624 void FrameWriter::init_packages()
626 for(int i = 0, layer = 0, number = 0;
627 i < get_total_packages();
630 FrameWriterPackage *package = (FrameWriterPackage*)get_package(i);
631 package->input = frames[layer][number];
632 package->path = file->create_path(package->input->get_number());
633 // printf("FrameWriter::init_packages 1 %p %d %s\n",
635 // package->input->get_number(),
646 void FrameWriter::write_frames(VFrame ***frames, int len)
648 this->frames = frames;
650 set_package_count(len * file->asset->layers);
655 LoadClient* FrameWriter::new_client()
657 return file->new_writer_unit(this);
660 LoadPackage* FrameWriter::new_package()
662 return new FrameWriterPackage;