fix mask vframe setup, add unshared vframe constructor
[goodguy/history.git] / cinelerra-5.1 / cinelerra / filelist.C
1
2 /*
3  * CINELERRA
4  * Copyright (C) 1997-2012 Adam Williams <broadcast at earthling dot net>
5  *
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.
10  *
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.
15  *
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
19  *
20  */
21
22 #include "asset.h"
23 #include "bcsignals.h"
24 #include "cstrdup.h"
25 #include "file.h"
26 #include "filelist.h"
27 #include "guicast.h"
28 #include "interlacemodes.h"
29 #include "mutex.h"
30 #include "mwindow.inc"
31 #include "render.h"
32 #include "renderfarmfsserver.inc"
33 #include "vframe.h"
34 #include "mainerror.h"
35
36 #include <ctype.h>
37 #include <errno.h>
38 #include <fcntl.h>
39 #include <stdlib.h>
40 #include <string.h>
41 #include <sys/stat.h>
42 #include <unistd.h>
43
44
45 FileList::FileList(Asset *asset,
46         File *file,
47         const char *list_prefix,
48         const char *file_extension,
49         int frame_type,
50         int list_type)
51  : FileBase(asset, file)
52 {
53         reset_parameters();
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");
61 }
62
63 FileList::~FileList()
64 {
65         close_file();
66         delete table_lock;
67 }
68
69 int FileList::reset_parameters_derived()
70 {
71         data = 0;
72         writer = 0;
73         temp = 0;
74         first_number = 0;
75         return 0;
76 }
77
78 int FileList::open_file(int rd, int wr)
79 {
80         int result = 1;
81
82 // skip header for write
83         if(file->wr)
84         {
85                 int fd = open(asset->path, O_CREAT+O_TRUNC+O_RDWR, 0777);
86                 if( fd >= 0 ) {
87                         close(fd);
88                         result = 0;
89 // Frame files are created in write_frame and list index is created when
90 // file is closed.
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);
98                 }
99                 else
100                         eprintf(_("Error while opening \"%s\" for writing. \n%m\n"), asset->path);
101         }
102         else
103         if(file->rd)
104         {
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)
110                 {
111                         FILE *stream = fopen(asset->path, "rb");
112 //printf("FileList::open_file %d asset->path=%s\n", __LINE__, asset->path);
113                         if(stream)
114                         {
115                                 char string[BCTEXTLEN];
116                                 (void)fread(string, strlen(list_prefix), 1, stream);
117                                 fclose(stream);
118
119                                 if(!strncasecmp(string, list_prefix, strlen(list_prefix)))
120                                 {
121
122 //printf("FileList::open_file %d\n", __LINE__);
123                                         asset->format = list_type;
124
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]);
129                                 }
130                                 else
131                                 {
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);
136                                         asset->layers = 1;
137                                         if(!asset->frame_rate)
138                                                 asset->frame_rate = 1;
139                                         asset->video_length = -1;
140                                 }
141                                 result = 0;
142                         }
143                         else
144                                 eprintf(_("Error while opening \"%s\" for reading. \n%m\n"), asset->path);
145                 }
146                 else
147                 {
148                         Render::get_starting_number(asset->path,
149                                 first_number, number_start, number_digits, 6);
150                         result = 0;
151                 }
152         }
153
154         file->current_frame = 0;
155 // Compressed data storage
156         data = new VFrame;
157
158         return result;
159 }
160
161
162 int FileList::close_file()
163 {
164 //      path_list.total, asset->format, list_type, wr);
165         if(asset->format == list_type && path_list.total)
166         {
167                 if(file->wr && asset->use_header) write_list_header();
168                 path_list.remove_all_objects();
169         }
170         if(data) delete data;
171         if(writer) delete writer;
172         if(temp) delete temp;
173         reset_parameters();
174
175         FileBase::close_file();
176         return 0;
177 }
178
179 int FileList::write_list_header()
180 {
181         FILE *stream = fopen(asset->path, "w");
182         if( !stream ) return 1;
183 // Use sprintf instead of fprintf for VFS.
184         char string[BCTEXTLEN];
185         sprintf(string, "%s\n", list_prefix);
186         fwrite(string, strlen(string), 1, stream);
187         sprintf(string, "# First line is always %s\n", list_prefix);
188         fwrite(string, strlen(string), 1, stream);
189         sprintf(string, "# Frame rate:\n");
190         fwrite(string, strlen(string), 1, stream);
191         sprintf(string, "%f\n", asset->frame_rate);
192         fwrite(string, strlen(string), 1, stream);
193         sprintf(string, "# Width:\n");
194         fwrite(string, strlen(string), 1, stream);
195         sprintf(string, "%d\n", asset->width);
196         fwrite(string, strlen(string), 1, stream);
197         sprintf(string, "# Height:\n");
198         fwrite(string, strlen(string), 1, stream);
199         sprintf(string, "%d\n", asset->height);
200         fwrite(string, strlen(string), 1, stream);
201         sprintf(string, "# List of image files follows\n");
202         fwrite(string, strlen(string), 1, stream);
203
204         for(int i = 0; i < path_list.total; i++)
205         {
206 // Fix path for VFS but leave leading slash
207                 if(!strncmp(path_list.values[i], RENDERFARM_FS_PREFIX, strlen(RENDERFARM_FS_PREFIX)))
208                         sprintf(string, "%s\n", path_list.values[i] + strlen(RENDERFARM_FS_PREFIX));
209                 else
210                         sprintf(string, "%s\n", path_list.values[i]);
211                 fwrite(string, strlen(string), 1, stream);
212         }
213         fclose(stream);
214         return 0;
215 }
216
217 int FileList::read_list_header()
218 {
219         char string[BCTEXTLEN];
220
221         FILE *stream = fopen(asset->path, "r");
222         if( !stream ) return 1;
223 // Get information about the frames
224         do {
225                 if( feof(stream) || !fgets(string, BCTEXTLEN, stream) ) return 1;
226         } while(string[0] == '#' || string[0] == ' ' || isalpha(string[0]));
227
228 // Don't want a user configured frame rate to get destroyed
229         if(asset->frame_rate == 0)
230                 asset->frame_rate = atof(string);
231
232         do {
233                 if( feof(stream) || !fgets(string, BCTEXTLEN, stream) ) return 1;
234         } while(string[0] == '#' || string[0] == ' ');
235         if( (asset->width = atol(string)) <= 0 ) return 1;
236
237         do {
238                 if( feof(stream) || !fgets(string, BCTEXTLEN, stream) ) return 1;
239         } while(string[0] == '#' || string[0] == ' ');
240         if( (asset->height = atol(string)) <= 0 ) return 1;
241
242         asset->interlace_mode = ILACE_MODE_UNDETECTED;
243         asset->layers = 1;
244         asset->audio_data = 0;
245         asset->video_data = 1;
246
247 // Get all the paths
248         int missing = 0;
249         while(!feof(stream) && fgets(string, BCTEXTLEN, stream) ) {
250                 int len = strlen(string);
251                 if( !len || string[0] == '#' || string[0] == ' ') continue;
252                 string[len-1] = 0;
253                 if( access(string,R_OK) && !missing++ )
254                         eprintf(_("%s:no such file"), string);
255                 path_list.append(cstrdup(string));
256         }
257
258 //for(int i = 0; i < path_list.total; i++) printf("%s\n", path_list.values[i]);
259         fclose(stream);
260         if( !(asset->video_length = path_list.total) )
261                 eprintf(_("%s:\nlist empty"), asset->path);
262         if( missing )
263                 eprintf(_("%s:\n%d files not found"), asset->path, missing);
264         return 0;
265 }
266
267 int FileList::read_frame(VFrame *frame)
268 {
269         int result = 0;
270
271 //      PRINT_TRACE
272 // printf("FileList::read_frame %d %d use_header=%d current_frame=%d total=%d\n",
273 // __LINE__,
274 // result,
275 // asset->use_header,
276 // file->current_frame,
277 // path_list.total);
278
279         if(file->current_frame < 0 ||
280                 (asset->use_header && file->current_frame >= path_list.total &&
281                         asset->format == list_type))
282                 return 1;
283
284         if(asset->format == list_type)
285         {
286                 char string[BCTEXTLEN];
287                 char *path;
288                 if(asset->use_header)
289                 {
290                         path = path_list.values[file->current_frame];
291                 }
292                 else
293                 {
294                         path = calculate_path(file->current_frame, string);
295                 }
296
297                 FILE *in;
298
299 // Fix path for VFS.  Not used anymore.
300                 if(!strncmp(asset->path, RENDERFARM_FS_PREFIX, strlen(RENDERFARM_FS_PREFIX)))
301                         sprintf(string, "%s%s", RENDERFARM_FS_PREFIX, path);
302                 else
303                         strcpy(string, path);
304
305
306
307                 if(!use_path() || frame->get_color_model() == BC_COMPRESSED)
308                 {
309                         if(!(in = fopen(string, "rb"))) {
310                                 eprintf(_("Error while opening \"%s\" for reading. \n%m\n"), string);
311                         }
312                         else
313                         {
314                                 struct stat ostat;
315                                 stat(string, &ostat);
316
317                                 switch(frame->get_color_model())
318                                 {
319                                         case BC_COMPRESSED:
320                                                 frame->allocate_compressed_data(ostat.st_size);
321                                                 frame->set_compressed_size(ostat.st_size);
322                                                 (void)fread(frame->get_data(), ostat.st_size, 1, in);
323                                                 break;
324                                         default:
325                                                 data->allocate_compressed_data(ostat.st_size);
326                                                 data->set_compressed_size(ostat.st_size);
327                                                 (void)fread(data->get_data(), ostat.st_size, 1, in);
328                                                 result = read_frame(frame, data);
329                                                 break;
330                                 }
331
332                                 fclose(in);
333                         }
334                 }
335                 else
336                 {
337 //printf("FileList::read_frame %d %s\n", __LINE__, string);
338                         result = read_frame(frame, string);
339                 }
340         }
341         else
342         {
343                 asset->single_frame = 1;
344 // Allocate and decompress single frame into new temporary
345 //printf("FileList::read_frame %d\n", frame->get_color_model());
346                 if(!temp || temp->get_color_model() != frame->get_color_model())
347                 {
348                         if(temp) delete temp;
349                         temp = 0;
350
351
352                         if(!use_path() || frame->get_color_model() == BC_COMPRESSED)
353                         {
354                                 FILE *fd = fopen(asset->path, "rb");
355                                 if(fd)
356                                 {
357                                         struct stat ostat;
358                                         stat(asset->path, &ostat);
359
360                                         switch(frame->get_color_model())
361                                         {
362                                                 case BC_COMPRESSED:
363                                                         frame->allocate_compressed_data(ostat.st_size);
364                                                         frame->set_compressed_size(ostat.st_size);
365                                                         (void)fread(frame->get_data(), ostat.st_size, 1, fd);
366                                                         break;
367                                                 default:
368                                                         data->allocate_compressed_data(ostat.st_size);
369                                                         data->set_compressed_size(ostat.st_size);
370                                                         (void)fread(data->get_data(), ostat.st_size, 1, fd);
371                                                         temp = new VFrame(asset->width, asset->height,
372                                                                         frame->get_color_model(), 0);
373                                                         read_frame(temp, data);
374                                                         break;
375                                         }
376
377                                         fclose(fd);
378                                 }
379                                 else
380                                 {
381                                         eprintf(_("Error while opening \"%s\" for reading. \n%m\n"), asset->path);
382                                         result = 1;
383                                 }
384                         }
385                         else
386                         {
387                                 temp = new VFrame(asset->width, asset->height, frame->get_color_model(), 0);
388                                 read_frame(temp, asset->path);
389                         }
390                 }
391
392                 if(!temp) return result;
393
394 //printf("FileList::read_frame frame=%d temp=%d\n",
395 // frame->get_color_model(), // temp->get_color_model());
396                 frame->transfer_from(temp);
397         }
398
399
400 // printf("FileList::read_frame %d %d\n", __LINE__, result);
401 //
402 // if(frame->get_y())
403 // for(int i = 0; i < 100000; i++)
404 // {
405 //      frame->get_y()[i] = 0xff;
406 // }
407 // if(frame->get_rows())
408 // for(int i = 0; i < 100000; i++)
409 // {
410 //      frame->get_rows()[0][i] = 0xff;
411 // }
412
413
414         return result;
415 }
416
417 int FileList::write_frames(VFrame ***frames, int len)
418 {
419         return_value = 0;
420
421 //printf("FileList::write_frames 1\n");
422         if(frames[0][0]->get_color_model() == BC_COMPRESSED)
423         {
424                 for(int i = 0; i < asset->layers && !return_value; i++)
425                 {
426                         for(int j = 0; j < len && !return_value; j++)
427                         {
428                                 VFrame *frame = frames[i][j];
429                                 char *path = create_path(frame->get_number());
430 //printf("FileList::write_frames %d %jd\n", __LINE__, frame->get_number());
431
432
433                                 FILE *fd = fopen(path, "wb");
434                                 if(fd)
435                                 {
436                                         return_value = !fwrite(frames[i][j]->get_data(),
437                                                 frames[i][j]->get_compressed_size(),
438                                                 1,
439                                                 fd);
440
441                                         fclose(fd);
442                                 }
443                                 else
444                                 {
445                                         eprintf(_("Error while opening \"%s\" for writing. \n%m\n"), asset->path);
446                                         return_value++;
447                                 }
448                         }
449                 }
450         }
451         else
452         {
453 //printf("FileList::write_frames 2\n");
454                 writer->write_frames(frames, len);
455 //printf("FileList::write_frames 100\n");
456         }
457         return return_value;
458 }
459
460
461
462
463
464
465
466
467
468 void FileList::add_return_value(int amount)
469 {
470         table_lock->lock("FileList::add_return_value");
471         return_value += amount;
472         table_lock->unlock();
473 }
474
475 char* FileList::calculate_path(int number, char *string)
476 {
477 // Synthesize filename.
478 // If a header is used, the filename number must be in a different location.
479         if(asset->use_header)
480         {
481                 int k;
482                 strcpy(string, asset->path);
483                 for(k = strlen(string) - 1; k > 0 && string[k] != '.'; k--)
484                         ;
485                 if(k <= 0) k = strlen(string);
486
487                 sprintf(&string[k], "%06d%s",
488                         number,
489                         file_extension);
490         }
491         else
492 // Without a header, the original filename can be altered.
493         {
494                 Render::create_filename(string,
495                         asset->path,
496                         number,
497                         number_digits,
498                         number_start);
499         }
500
501         return string;
502 }
503
504 char* FileList::create_path(int number_override)
505 {
506         if(asset->format != list_type) return asset->path;
507
508         table_lock->lock("FileList::create_path");
509
510
511
512         char *path = 0;
513         char output[BCTEXTLEN];
514         if(file->current_frame >= path_list.total || !asset->use_header)
515         {
516                 int number;
517                 if(number_override < 0)
518                         number = file->current_frame++;
519                 else
520                 {
521                         number = number_override;
522                         file->current_frame++;
523                 }
524
525                 if(!asset->use_header)
526                 {
527                         number += first_number;
528                 }
529
530                 calculate_path(number, output);
531
532                 path = new char[strlen(output) + 1];
533                 strcpy(path, output);
534                 path_list.append(path);
535         }
536         else
537         {
538 // Overwrite an old path
539                 path = path_list.values[file->current_frame];
540         }
541
542
543         table_lock->unlock();
544
545         return path;
546 }
547
548 FrameWriterUnit* FileList::new_writer_unit(FrameWriter *writer)
549 {
550         return new FrameWriterUnit(writer);
551 }
552
553 int64_t FileList::get_memory_usage()
554 {
555         int64_t result = 0;
556         if(data) result += data->get_compressed_allocated();
557         if(temp) result += temp->get_data_size();
558 // printf("FileList::get_memory_usage %d %p %s %jd\n",
559 // __LINE__,
560 // this,
561 // file->asset->path,
562 // result);
563         return result;
564 }
565
566 int FileList::get_units()
567 {
568         return !writer ? 0 : writer->get_total_clients();
569 }
570
571 FrameWriterUnit* FileList::get_unit(int number)
572 {
573         return !writer ? 0 : (FrameWriterUnit*)writer->get_client(number);
574 }
575
576 int FileList::use_path()
577 {
578         return 0;
579 }
580
581
582
583
584
585
586 FrameWriterPackage::FrameWriterPackage()
587 {
588 }
589
590 FrameWriterPackage::~FrameWriterPackage()
591 {
592 }
593
594
595
596
597
598
599
600
601
602
603
604 FrameWriterUnit::FrameWriterUnit(FrameWriter *server)
605  : LoadClient(server)
606 {
607 // Don't use server here since subclasses call this with no server.
608         this->server = server;
609         output = new VFrame;
610 }
611
612 FrameWriterUnit::~FrameWriterUnit()
613 {
614         delete output;
615 }
616
617 void FrameWriterUnit::process_package(LoadPackage *package)
618 {
619 //printf("FrameWriterUnit::process_package 1\n");
620         FrameWriterPackage *ptr = (FrameWriterPackage*)package;
621
622         FILE *file;
623
624 //printf("FrameWriterUnit::process_package 2 %s\n", ptr->path);
625         if(!(file = fopen(ptr->path, "wb")))
626         {
627                 eprintf(_("Error while opening \"%s\" for writing. \n%m\n"), ptr->path);
628                 return;
629         }
630 //printf("FrameWriterUnit::process_package 3");
631
632
633         int result = server->file->write_frame(ptr->input, output, this);
634
635 //printf("FrameWriterUnit::process_package 4 %s %d\n", ptr->path, output->get_compressed_size());
636         if(!result) result = !fwrite(output->get_data(), output->get_compressed_size(), 1, file);
637 //TRACE("FrameWriterUnit::process_package 4");
638         fclose(file);
639 //TRACE("FrameWriterUnit::process_package 5");
640
641         server->file->add_return_value(result);
642 //TRACE("FrameWriterUnit::process_package 6");
643 }
644
645
646
647
648
649
650
651
652
653
654
655 FrameWriter::FrameWriter(FileList *file, int cpus)
656  : LoadServer(cpus, 0)
657 {
658         this->file = file;
659 }
660
661
662 FrameWriter::~FrameWriter()
663 {
664 }
665
666 void FrameWriter::init_packages()
667 {
668         for(int i = 0, layer = 0, number = 0;
669                 i < get_total_packages();
670                 i++)
671         {
672                 FrameWriterPackage *package = (FrameWriterPackage*)get_package(i);
673                 package->input = frames[layer][number];
674                 package->path = file->create_path(package->input->get_number());
675 // printf("FrameWriter::init_packages 1 %p %d %s\n",
676 // package->input,
677 // package->input->get_number(),
678 // package->path);
679                 number++;
680                 if(number >= len)
681                 {
682                         layer++;
683                         number = 0;
684                 }
685         }
686 }
687
688 void FrameWriter::write_frames(VFrame ***frames, int len)
689 {
690         this->frames = frames;
691         this->len = len;
692         set_package_count(len * file->asset->layers);
693
694         process_packages();
695 }
696
697 LoadClient* FrameWriter::new_client()
698 {
699         return file->new_writer_unit(this);
700 }
701
702 LoadPackage* FrameWriter::new_package()
703 {
704         return new FrameWriterPackage;
705 }
706
707
708