fullscreen segv fix, popup for 4opts preview, renderfarm print fix, pan widget upgrad...
[goodguy/cinelerra.git] / cinelerra-5.1 / cinelerra / filethread.C
1
2 /*
3  * CINELERRA
4  * Copyright (C) 2009 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 "condition.h"
25 #include "file.h"
26 #include "filethread.h"
27 #include "mutex.h"
28 #include "samples.h"
29 #include "vframe.h"
30 #include "videodevice.inc"
31
32 #include <string.h>
33 #include <unistd.h>
34
35
36 FileThreadFrame::FileThreadFrame()
37 {
38         position = 0;
39         frame = 0;
40 }
41
42 FileThreadFrame::~FileThreadFrame()
43 {
44         if(frame) delete frame;
45 }
46
47
48
49 FileThread::FileThread(File *file, int do_audio, int do_video)
50  : Thread(1, 0, 0)
51 {
52         reset();
53         create_objects(file,
54                 do_audio,
55                 do_video);
56 }
57
58 FileThread::~FileThread()
59 {
60         delete_objects();
61
62
63 }
64
65 void FileThread::reset()
66 {
67         audio_buffer = 0;
68         video_buffer = 0;
69         output_size = 0;
70         input_lock = 0;
71         output_lock = 0;
72         last_buffer = 0;
73         is_writing = 0;
74         is_reading = 0;
75         file_lock = 0;
76
77         read_wait_lock = 0;
78         user_wait_lock = 0;
79         frame_lock = 0;
80         total_frames = 0;
81         done = 0;
82         disable_read = 1;
83         start_position = -1;
84         layer = -1;
85         read_position = 0;
86         bzero(read_frames, sizeof(FileThreadFrame*) * MAX_READ_FRAMES);
87 }
88
89
90 void FileThread::create_objects(File *file,
91                 int do_audio,
92                 int do_video)
93 {
94         this->file = file;
95         this->do_audio = do_audio;
96         this->do_video = do_video;
97         file_lock = new Mutex("FileThread::file_lock");
98         read_wait_lock = new Condition(0, "FileThread::read_wait_lock");
99         user_wait_lock = new Condition(0, "FileThread::user_wait_lock");
100         frame_lock = new Mutex("FileThread::frame_lock");
101         for(int i = 0; i < MAX_READ_FRAMES; i++)
102                 read_frames[i] = new FileThreadFrame;
103 }
104
105
106 void FileThread::delete_objects()
107 {
108         for(int i = 0; i < MAX_READ_FRAMES; i++)
109                 delete read_frames[i];
110
111         if(output_lock)
112         {
113                 for(int i = 0; i < ring_buffers; i++)
114                 {
115                         delete output_lock[i];
116                 }
117                 delete [] output_lock;
118         }
119
120         if(input_lock)
121         {
122                 for(int i = 0; i < ring_buffers; i++)
123                 {
124                         delete input_lock[i];
125                 }
126                 delete [] input_lock;
127         }
128
129
130         if(last_buffer)
131                 delete [] last_buffer;
132
133
134         delete [] output_size;
135
136         delete file_lock;
137
138
139         delete read_wait_lock;
140         delete user_wait_lock;
141         delete frame_lock;
142
143         reset();
144 }
145
146 void FileThread::run()
147 {
148         int i, j;
149         int debug = 0;
150         if(debug) PRINT_TRACE
151
152         if(is_reading)
153         {
154                 if(debug) PRINT_TRACE
155
156                 while(!done && !disable_read)
157                 {
158                         if(debug) PRINT_TRACE
159                         frame_lock->lock("FileThread::run 1");
160                         int local_total_frames = total_frames;
161                         frame_lock->unlock();
162
163                         if(local_total_frames >= MAX_READ_FRAMES)
164                         {
165                                 read_wait_lock->lock("FileThread::run");
166                                 continue;
167                         }
168
169                         if(debug) PRINT_TRACE
170                         if(done || disable_read) break;
171
172 // Make local copes of the locked parameters
173                         FileThreadFrame *local_frame = 0;
174                         int64_t local_position = 0;
175                         int local_layer;
176                         if(debug) PRINT_TRACE
177
178                         frame_lock->lock("FileThread::run 2");
179 // Get position of next frame to read
180                         if(total_frames)
181                                 local_position = read_frames[total_frames - 1]->position + 1;
182                         else
183                                 local_position = start_position;
184 //printf("FileThread::run 1 %d %jd\n", total_frames, local_position);
185
186 // Get first available frame
187                         local_total_frames = total_frames;
188                         local_frame = read_frames[local_total_frames];
189                         local_layer = layer;
190                         local_frame->valid = 0;
191                         frame_lock->unlock();
192
193 // Read frame
194                         if(local_frame)
195                         {
196                                 if(debug) PRINT_TRACE
197                                 file->set_layer(local_layer, 1);
198                                 file->set_video_position(local_position, 1);
199                                 int supported_colormodel =
200                                         file->get_best_colormodel(PLAYBACK_ASYNCHRONOUS, local_layer);
201                                 if(debug) PRINT_TRACE
202
203
204 // Allocate frame
205                                 if(local_frame->frame &&
206                                         !local_frame->frame->params_match(file->asset->width,
207                                                 file->asset->height,
208                                                 supported_colormodel))
209                                 {
210                                         delete local_frame->frame;
211                                         local_frame->frame = 0;
212                                 }
213
214 //printf("FileThread::run %d\n", __LINE__);
215                                 if(!local_frame->frame)
216                                 {
217                                         local_frame->frame =
218                                                 new VFrame(file->asset->width, file->asset->height,
219                                                         supported_colormodel, 0);
220                                 }
221
222 // Read it
223 // printf("FileThread::run %d w=%d h=%d supported_colormodel=%d\n",
224 // __LINE__,
225 // local_frame->frame->get_w(),
226 // local_frame->frame->get_h(),
227 // local_frame->frame->get_color_model());
228                                 if(debug)
229                                 {
230                                         PRINT_TRACE
231                                         printf("file=%p local_frame->frame=%p\n", file, local_frame->frame);
232                                 }
233                                 file->read_frame(local_frame->frame, 1);
234                                 if(debug) PRINT_TRACE
235                                 local_frame->position = local_position;
236                                 local_frame->layer = local_layer;
237
238 // Put frame in last position but since the last position now may be
239 // lower than it was when we got the frame, swap the current
240 // last position with the previous last position.
241                                 frame_lock->lock("FileThread::run 3");
242                                 FileThreadFrame *old_frame = read_frames[total_frames];
243                                 read_frames[local_total_frames] = old_frame;
244                                 read_frames[total_frames++] = local_frame;
245                                 local_frame->valid = 1;
246                                 if(debug) PRINT_TRACE
247                                 frame_lock->unlock();
248
249 // Que the user
250                                 user_wait_lock->unlock();
251                                 if(debug) PRINT_TRACE
252                         }
253                 }
254         }
255         else
256         {
257                 while(!done)
258                 {
259                         output_lock[local_buffer]->lock("FileThread::run 1");
260                         return_value = 0;
261
262
263 // Timer timer;
264 // timer.update();
265                         if(!last_buffer[local_buffer])
266                         {
267                                 if(output_size[local_buffer])
268                                 {
269                                         int result = 0;
270                                         file_lock->lock("FileThread::run 2");
271                                         if(do_audio)
272                                         {
273                                                 result = file->write_samples(
274                                                         audio_buffer[local_buffer],
275                                                         output_size[local_buffer]);
276                                         }
277                                         else
278                                         if(do_video)
279                                         {
280                                                 if(compressed)
281                                                 {
282                                                         for(j = 0; j < file->asset->layers && !result; j++)
283                                                                 for(i = 0; i < output_size[local_buffer] && !result; i++)
284                                                                         result = file->write_compressed_frame(video_buffer[local_buffer][j][i]);
285                                                 }
286                                                 else
287                                                 {
288                                                         result = file->write_frames(video_buffer[local_buffer],
289                                                                 output_size[local_buffer]);
290                                                 }
291                                         }
292
293                                         file_lock->unlock();
294                                         return_value = result;
295                                 }
296                                 else
297                                         return_value = 0;
298
299                                 output_size[local_buffer] = 0;
300                         }
301                         else
302                                 done = 1;
303
304                         input_lock[local_buffer]->unlock();
305                         local_buffer++;
306                         if(local_buffer >= ring_buffers) local_buffer = 0;
307                 }
308         }
309 }
310
311
312
313 int FileThread::stop_writing()
314 {
315         if(is_writing)
316         {
317                 int i, buffer, layer, frame;
318
319                 swap_buffer();
320                 input_lock[current_buffer]->lock("FileThread::stop_writing 1");
321
322                 last_buffer[current_buffer] = 1;
323
324                 for(i = 0; i < ring_buffers; i++)
325                         output_lock[i]->unlock();
326
327                 swap_buffer();
328
329 // wait for thread to finish
330                 Thread::join();
331
332 // delete buffers
333                 file_lock->lock("FileThread::stop_writing 2");
334                 if(do_audio)
335                 {
336                         for(buffer = 0; buffer < ring_buffers; buffer++)
337                         {
338                                 for(i = 0; i < file->asset->channels; i++)
339                                         delete audio_buffer[buffer][i];
340                                 delete [] audio_buffer[buffer];
341                         }
342                         delete [] audio_buffer;
343                         audio_buffer = 0;
344                 }
345
346 // printf("FileThread::stop_writing %d %d %d %d\n",
347 // do_video,
348 // ring_buffers,
349 // file->asset->layers,
350 // buffer_size);
351                 if(do_video)
352                 {
353                         for(buffer = 0; buffer < ring_buffers; buffer++)
354                         {
355                                 for(layer = 0; layer < file->asset->layers; layer++)
356                                 {
357                                         for(frame = 0; frame < buffer_size; frame++)
358                                         {
359                                                 delete video_buffer[buffer][layer][frame];
360                                         }
361                                         delete [] video_buffer[buffer][layer];
362                                 }
363                                 delete [] video_buffer[buffer];
364                         }
365                         delete [] video_buffer;
366                         video_buffer = 0;
367                 }
368
369                 file_lock->unlock();
370         }
371         return 0;
372 }
373
374 int FileThread::start_writing(long buffer_size,
375                 int color_model,
376                 int ring_buffers,
377                 int compressed)
378 {
379 // allocate buffers
380         int buffer, layer, frame;
381
382         this->ring_buffers = ring_buffers;
383         this->buffer_size = buffer_size;
384         this->color_model = color_model;
385         this->compressed = compressed;
386         this->current_buffer = ring_buffers - 1;
387         return_value = 0;
388         local_buffer = 0;
389
390         file_lock->lock("FileThread::start_writing 1");
391
392
393
394
395 // Buffer is swapped before first get
396         last_buffer = new int[ring_buffers];
397         output_size = new long[ring_buffers];
398
399
400         output_lock = new Condition*[ring_buffers];
401         input_lock = new Condition*[ring_buffers];
402         for(int i = 0; i < ring_buffers; i++)
403         {
404                 output_lock[i] = new Condition(0, "FileThread::output_lock");
405                 input_lock[i] = new Condition(1, "FileThread::input_lock");
406                 last_buffer[i] = 0;
407                 output_size[i] = 0;
408         }
409
410
411
412         if(do_audio)
413         {
414                 audio_buffer = new Samples**[ring_buffers];
415                 for(buffer = 0; buffer < ring_buffers; buffer++)
416                 {
417                         audio_buffer[buffer] = new Samples*[file->asset->channels];
418
419                         for(int channel = 0; channel < file->asset->channels; channel++)
420                         {
421                                 audio_buffer[buffer][channel] = new Samples(buffer_size);
422                         }
423                 }
424         }
425
426         if(do_video)
427         {
428                 this->color_model = color_model;
429                 //long bytes_per_frame = VFrame::calculate_data_size(file->asset->width,
430                 //      file->asset->height, -1, color_model);
431
432                 video_buffer = new VFrame***[ring_buffers];
433 // printf("FileThread::start_writing 1 %d %d %d %p\n",
434 // ring_buffers,
435 // file->asset->layers,
436 // buffer_size,
437 // video_buffer);
438                 for(buffer = 0; buffer < ring_buffers; buffer++)
439                 {
440                         video_buffer[buffer] = new VFrame**[file->asset->layers];
441                         for(layer = 0; layer < file->asset->layers; layer++)
442                         {
443                                 video_buffer[buffer][layer] = new VFrame*[buffer_size];
444                                 for(frame = 0; frame < buffer_size; frame++)
445                                 {
446                                         if(compressed)
447                                         {
448                                                 video_buffer[buffer][layer][frame] = new VFrame;
449 //printf("FileThread::start_writing %d %d\n", __LINE__);
450                                         }
451                                         else
452                                         {
453                                                 video_buffer[buffer][layer][frame] =
454                                                         new VFrame( file->asset->width, file->asset->height,
455                                                                 color_model, -1);
456 // printf("FileThread::start_writing %d %d %d %d %p\n",
457 // __LINE__,
458 // buffer,
459 // layer,
460 // frame,
461 // video_buffer[buffer][layer]);
462                                         }
463                                 }
464                         }
465                 }
466         }
467         file_lock->unlock();
468
469         for(int i = 0; i < ring_buffers; i++)
470         {
471                 last_buffer[i] = 0;
472         }
473
474         is_writing = 1;
475         done = 0;
476         Thread::start();
477         return 0;
478 }
479
480 int FileThread::start_reading()
481 {
482         if(!is_reading)
483         {
484                 is_reading = 1;
485                 disable_read = 1;
486                 done = 0;
487         }
488         return 0;
489 }
490
491 int FileThread::stop_reading()
492 {
493         if(is_reading && Thread::running())
494         {
495                 done = 1;
496                 read_wait_lock->unlock();
497                 Thread::join();
498         }
499         return 0;
500 }
501
502 int FileThread::set_video_position(int64_t position)
503 {
504 // If the new position can't be added to the buffer without restarting,
505 // disable reading.
506         if((position < this->start_position ||
507                 position >= this->start_position + MAX_READ_FRAMES) &&
508                 !disable_read)
509         {
510                 disable_read = 1;
511                 read_wait_lock->unlock();
512                 Thread::join();
513
514                 total_frames = 0;
515                 for(int i = 0; i < MAX_READ_FRAMES; i++)
516                         read_frames[i]->valid = 0;
517                 this->start_position = position;
518         }
519         else
520 // If a sequential read, enable reading
521         if(this->start_position + 1 == position && disable_read)
522         {
523                 this->start_position = position;
524                 disable_read = 0;
525                 Thread::start();
526         }
527         else
528         if(disable_read)
529         {
530                 this->start_position = position;
531         }
532
533         this->read_position = position;
534         return 0;
535 }
536
537 int FileThread::set_layer(int layer)
538 {
539         if(layer != this->layer)
540         {
541                 disable_read = 1;
542                 read_wait_lock->unlock();
543                 Thread::join();
544                 total_frames = 0;
545         }
546         this->layer = layer;
547         return 0;
548 }
549
550 int FileThread::read_frame(VFrame *frame)
551 {
552         FileThreadFrame *local_frame = 0;
553         int got_it = 0;
554         int number = 0;
555
556 //printf("FileThread::read_frame %d this=%p\n", __LINE__, this);
557
558 // Search thread for frame
559         while(!got_it && !disable_read)
560         {
561                 frame_lock->lock("FileThread::read_frame 1");
562 // printf("FileThread::read_frame: 1 read_position=%jd ", read_position);
563 // for(int i = 0; i < total_frames; i++)
564 // printf("%jd ", read_frames[i]->position);
565 // printf("\n");
566                 for(int i = 0; i < total_frames; i++)
567                 {
568                         local_frame = read_frames[i];
569                         if(local_frame->position == read_position &&
570                                 local_frame->layer == layer &&
571                                 local_frame->frame &&
572                                 local_frame->frame->equal_stacks(frame) &&
573                                 local_frame->valid)
574                         {
575                                 got_it = 1;
576                                 number = i;
577                                 break;
578                         }
579                 }
580                 frame_lock->unlock();
581
582 // Not decoded yet but thread active
583                 if(!got_it && !disable_read)
584                 {
585                         user_wait_lock->lock("FileThread::read_frame");
586                 }
587         }
588
589 //printf("FileThread::read_frame %d this=%p\n", __LINE__, this);
590
591         if(got_it)
592         {
593 // printf("FileThread::read_frame 1 color_model=%d disable_read=%d\n",
594 // frame->get_color_model(),
595 // disable_read);
596 // Copy image
597                 if(frame->get_color_model() != local_frame->frame->get_color_model() ||
598                         frame->get_w() != local_frame->frame->get_w() ||
599                         frame->get_h() != local_frame->frame->get_h())
600                 {
601 // printf("FileThread::read_frame %d this=%p out cmodel=%d h=%d in cmodel=%d h=%d\n",
602 // __LINE__,
603 // this,
604 // frame->get_color_model(),
605 // frame->get_w(),
606 // local_frame->frame->get_color_model(),
607 // local_frame->frame->get_w());
608                         BC_CModels::transfer(frame->get_rows(),
609                                 local_frame->frame->get_rows(),
610                                 frame->get_y(),
611                                 frame->get_u(),
612                                 frame->get_v(),
613                                 local_frame->frame->get_y(),
614                                 local_frame->frame->get_u(),
615                                 local_frame->frame->get_v(),
616                                 0,
617                                 0,
618                                 local_frame->frame->get_w(),
619                                 local_frame->frame->get_h(),
620                                 0,
621                                 0,
622                                 frame->get_w(),
623                                 frame->get_h(),
624                                 local_frame->frame->get_color_model(),
625                                 frame->get_color_model(),
626                                 0,
627                                 local_frame->frame->get_w(),
628                                 frame->get_w());
629 //for(int i = 0; i < 3000 * 1000 * 4; i++)
630 //((float*)frame->get_rows()[0])[i] = 1;
631 //printf("FileThread::read_frame %d this=%p\n", __LINE__, this);
632                 }
633                 else
634                 {
635 //printf("FileThread::read_frame %d this=%p\n", __LINE__, this);
636                         frame->copy_from(local_frame->frame);
637 //printf("FileThread::read_frame %d this=%p\n", __LINE__, this);
638                 }
639
640 // Can't copy stacks because the stack is needed by the plugin requestor.
641                 frame->copy_params(local_frame->frame);
642 //printf("FileThread::read_frame %d this=%p\n", __LINE__, this);
643
644 // Recycle all frames before current one but not including current one.
645 // This handles redrawing of a single frame but because FileThread has no
646 // notion of a still frame, it has to call read_frame for those.
647                 frame_lock->lock("FileThread::read_frame 1");
648                 FileThreadFrame *new_table[MAX_READ_FRAMES];
649                 int k = 0;
650                 for(int j = number; j < total_frames; j++, k++)
651                 {
652                         new_table[k] = read_frames[j];
653                 }
654                 for(int j = 0; j < number; j++, k++)
655                 {
656                         new_table[k] = read_frames[j];
657                 }
658                 memcpy(read_frames, new_table, sizeof(FileThreadFrame*) * total_frames);
659 //printf("FileThread::read_frame %d this=%p\n", __LINE__, this);
660                 total_frames -= number;
661
662                 start_position = read_position;
663                 read_position++;
664                 frame_lock->unlock();
665                 read_wait_lock->unlock();
666                 return 0;
667         }
668         else
669         {
670 // printf("FileThread::read_frame 2 color_model=%d disable_read=%d\n",
671 // frame->get_color_model(),
672 // disable_read);
673 //printf("FileThread::read_frame %d this=%p\n", __LINE__, this);
674 // Use traditional read function
675                 file->set_layer(layer, 1);
676                 file->set_video_position(read_position, 1);
677                 read_position++;
678                 int result = file->read_frame(frame, 1);
679 //printf("FileThread::read_frame %d this=%p\n", __LINE__, this);
680                 return result;
681         }
682
683
684 //printf("FileThread::read_frame %d this=%p\n", __LINE__, this);
685 }
686
687 int64_t FileThread::get_memory_usage()
688 {
689         frame_lock->lock("FileThread::get_memory_usage");
690         int64_t result = 0;
691         for(int i = 0; i < MAX_READ_FRAMES; i++)
692                 if(read_frames[i] && read_frames[i]->frame)
693                         result += read_frames[i]->frame->get_data_size();
694         frame_lock->unlock();
695         return result;
696 }
697
698
699 Samples** FileThread::get_audio_buffer()
700 {
701         swap_buffer();
702
703         input_lock[current_buffer]->lock("FileThread::get_audio_buffer");
704         return audio_buffer[current_buffer];
705 }
706
707 VFrame*** FileThread::get_video_buffer()
708 {
709         swap_buffer();
710
711         input_lock[current_buffer]->lock("FileThread::get_video_buffer");
712         return video_buffer[current_buffer];
713 }
714
715 VFrame*** FileThread::get_last_video_buffer()
716 {
717         return video_buffer[current_buffer];
718 }
719
720 int FileThread::write_buffer(long size)
721 {
722         output_size[current_buffer] = size;
723
724 // unlock the output lock
725         output_lock[current_buffer]->unlock();
726
727         return return_value;
728 }
729
730 void FileThread::swap_buffer()
731 {
732         current_buffer++;
733         if(current_buffer >= ring_buffers) current_buffer = 0;
734 }
735
736