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