change thread join strategy, fix a few leaks, fix a few bugs
[goodguy/history.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);
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 = new VFrame(0,
218                                                 -1,
219                                                 file->asset->width,
220                                                 file->asset->height,
221                                                 supported_colormodel,
222                                                 -1);
223                                 }
224
225 // Read it
226 // printf("FileThread::run %d w=%d h=%d supported_colormodel=%d\n",
227 // __LINE__,
228 // local_frame->frame->get_w(),
229 // local_frame->frame->get_h(),
230 // local_frame->frame->get_color_model());
231                                 if(debug)
232                                 {
233                                         PRINT_TRACE
234                                         printf("file=%p local_frame->frame=%p\n", file, local_frame->frame);
235                                 }
236                                 file->read_frame(local_frame->frame, 1);
237                                 if(debug) PRINT_TRACE
238                                 local_frame->position = local_position;
239                                 local_frame->layer = local_layer;
240
241 // Put frame in last position but since the last position now may be
242 // lower than it was when we got the frame, swap the current
243 // last position with the previous last position.
244                                 frame_lock->lock("FileThread::run 3");
245                                 FileThreadFrame *old_frame = read_frames[total_frames];
246                                 read_frames[local_total_frames] = old_frame;
247                                 read_frames[total_frames++] = local_frame;
248                                 local_frame->valid = 1;
249                                 if(debug) PRINT_TRACE
250                                 frame_lock->unlock();
251
252 // Que the user
253                                 user_wait_lock->unlock();
254                                 if(debug) PRINT_TRACE
255                         }
256                 }
257         }
258         else
259         {
260                 while(!done)
261                 {
262                         output_lock[local_buffer]->lock("FileThread::run 1");
263                         return_value = 0;
264
265
266 // Timer timer;
267 // timer.update();
268                         if(!last_buffer[local_buffer])
269                         {
270                                 if(output_size[local_buffer])
271                                 {
272                                         int result = 0;
273                                         file_lock->lock("FileThread::run 2");
274                                         if(do_audio)
275                                         {
276                                                 result = file->write_samples(
277                                                         audio_buffer[local_buffer],
278                                                         output_size[local_buffer]);
279                                         }
280                                         else
281                                         if(do_video)
282                                         {
283                                                 if(compressed)
284                                                 {
285                                                         for(j = 0; j < file->asset->layers && !result; j++)
286                                                                 for(i = 0; i < output_size[local_buffer] && !result; i++)
287                                                                         result = file->write_compressed_frame(video_buffer[local_buffer][j][i]);
288                                                 }
289                                                 else
290                                                 {
291                                                         result = file->write_frames(video_buffer[local_buffer],
292                                                                 output_size[local_buffer]);
293                                                 }
294                                         }
295
296                                         file_lock->unlock();
297                                         return_value = result;
298                                 }
299                                 else
300                                         return_value = 0;
301
302                                 output_size[local_buffer] = 0;
303                         }
304                         else
305                                 done = 1;
306
307                         input_lock[local_buffer]->unlock();
308                         local_buffer++;
309                         if(local_buffer >= ring_buffers) local_buffer = 0;
310                 }
311         }
312 }
313
314
315
316 int FileThread::stop_writing()
317 {
318         if(is_writing)
319         {
320                 int i, buffer, layer, frame;
321
322                 swap_buffer();
323                 input_lock[current_buffer]->lock("FileThread::stop_writing 1");
324
325                 last_buffer[current_buffer] = 1;
326
327                 for(i = 0; i < ring_buffers; i++)
328                         output_lock[i]->unlock();
329
330                 swap_buffer();
331
332 // wait for thread to finish
333                 Thread::join();
334
335 // delete buffers
336                 file_lock->lock("FileThread::stop_writing 2");
337                 if(do_audio)
338                 {
339                         for(buffer = 0; buffer < ring_buffers; buffer++)
340                         {
341                                 for(i = 0; i < file->asset->channels; i++)
342                                         delete audio_buffer[buffer][i];
343                                 delete [] audio_buffer[buffer];
344                         }
345                         delete [] audio_buffer;
346                         audio_buffer = 0;
347                 }
348
349 // printf("FileThread::stop_writing %d %d %d %d\n",
350 // do_video,
351 // ring_buffers,
352 // file->asset->layers,
353 // buffer_size);
354                 if(do_video)
355                 {
356                         for(buffer = 0; buffer < ring_buffers; buffer++)
357                         {
358                                 for(layer = 0; layer < file->asset->layers; layer++)
359                                 {
360                                         for(frame = 0; frame < buffer_size; frame++)
361                                         {
362                                                 delete video_buffer[buffer][layer][frame];
363                                         }
364                                         delete [] video_buffer[buffer][layer];
365                                 }
366                                 delete [] video_buffer[buffer];
367                         }
368                         delete [] video_buffer;
369                         video_buffer = 0;
370                 }
371
372                 file_lock->unlock();
373         }
374         return 0;
375 }
376
377 int FileThread::start_writing(long buffer_size,
378                 int color_model,
379                 int ring_buffers,
380                 int compressed)
381 {
382 // allocate buffers
383         int buffer, layer, frame;
384
385         this->ring_buffers = ring_buffers;
386         this->buffer_size = buffer_size;
387         this->color_model = color_model;
388         this->compressed = compressed;
389         this->current_buffer = ring_buffers - 1;
390         return_value = 0;
391         local_buffer = 0;
392
393         file_lock->lock("FileThread::start_writing 1");
394
395
396
397
398 // Buffer is swapped before first get
399         last_buffer = new int[ring_buffers];
400         output_size = new long[ring_buffers];
401
402
403         output_lock = new Condition*[ring_buffers];
404         input_lock = new Condition*[ring_buffers];
405         for(int i = 0; i < ring_buffers; i++)
406         {
407                 output_lock[i] = new Condition(0, "FileThread::output_lock");
408                 input_lock[i] = new Condition(1, "FileThread::input_lock");
409                 last_buffer[i] = 0;
410                 output_size[i] = 0;
411         }
412
413
414
415         if(do_audio)
416         {
417                 audio_buffer = new Samples**[ring_buffers];
418                 for(buffer = 0; buffer < ring_buffers; buffer++)
419                 {
420                         audio_buffer[buffer] = new Samples*[file->asset->channels];
421
422                         for(int channel = 0; channel < file->asset->channels; channel++)
423                         {
424                                 audio_buffer[buffer][channel] = new Samples(buffer_size);
425                         }
426                 }
427         }
428
429         if(do_video)
430         {
431                 this->color_model = color_model;
432                 //long bytes_per_frame = VFrame::calculate_data_size(file->asset->width,
433                 //      file->asset->height, -1, color_model);
434
435                 video_buffer = new VFrame***[ring_buffers];
436 // printf("FileThread::start_writing 1 %d %d %d %p\n",
437 // ring_buffers,
438 // file->asset->layers,
439 // buffer_size,
440 // video_buffer);
441                 for(buffer = 0; buffer < ring_buffers; buffer++)
442                 {
443                         video_buffer[buffer] = new VFrame**[file->asset->layers];
444                         for(layer = 0; layer < file->asset->layers; layer++)
445                         {
446                                 video_buffer[buffer][layer] = new VFrame*[buffer_size];
447                                 for(frame = 0; frame < buffer_size; frame++)
448                                 {
449                                         if(compressed)
450                                         {
451                                                 video_buffer[buffer][layer][frame] = new VFrame;
452 //printf("FileThread::start_writing %d %d\n", __LINE__);
453                                         }
454                                         else
455                                         {
456                                                 video_buffer[buffer][layer][frame] =
457                                                         new VFrame(0,
458                                                                 -1,
459                                                                 file->asset->width,
460                                                                 file->asset->height,
461                                                                 color_model,
462                                                                 -1);
463 // printf("FileThread::start_writing %d %d %d %d %p\n",
464 // __LINE__,
465 // buffer,
466 // layer,
467 // frame,
468 // video_buffer[buffer][layer]);
469                                         }
470                                 }
471                         }
472                 }
473         }
474         file_lock->unlock();
475
476         for(int i = 0; i < ring_buffers; i++)
477         {
478                 last_buffer[i] = 0;
479         }
480
481         is_writing = 1;
482         done = 0;
483         Thread::start();
484         return 0;
485 }
486
487 int FileThread::start_reading()
488 {
489         if(!is_reading)
490         {
491                 is_reading = 1;
492                 disable_read = 1;
493                 done = 0;
494         }
495         return 0;
496 }
497
498 int FileThread::stop_reading()
499 {
500         if(is_reading && Thread::running())
501         {
502                 done = 1;
503                 read_wait_lock->unlock();
504         }
505         Thread::join();
506         return 0;
507 }
508
509 int FileThread::set_video_position(int64_t position)
510 {
511 // If the new position can't be added to the buffer without restarting,
512 // disable reading.
513         if((position < this->start_position ||
514                 position >= this->start_position + MAX_READ_FRAMES) &&
515                 !disable_read)
516         {
517                 disable_read = 1;
518                 read_wait_lock->unlock();
519                 Thread::join();
520
521                 total_frames = 0;
522                 for(int i = 0; i < MAX_READ_FRAMES; i++)
523                         read_frames[i]->valid = 0;
524                 this->start_position = position;
525         }
526         else
527 // If a sequential read, enable reading
528         if(this->start_position + 1 == position && disable_read)
529         {
530                 this->start_position = position;
531                 disable_read = 0;
532                 Thread::start();
533         }
534         else
535         if(disable_read)
536         {
537                 this->start_position = position;
538         }
539
540         this->read_position = position;
541         return 0;
542 }
543
544 int FileThread::set_layer(int layer)
545 {
546         if(layer != this->layer)
547         {
548                 disable_read = 1;
549                 read_wait_lock->unlock();
550                 Thread::join();
551                 total_frames = 0;
552         }
553         this->layer = layer;
554         return 0;
555 }
556
557 int FileThread::read_frame(VFrame *frame)
558 {
559         FileThreadFrame *local_frame = 0;
560         int got_it = 0;
561         int number = 0;
562
563 //printf("FileThread::read_frame %d this=%p\n", __LINE__, this);
564
565 // Search thread for frame
566         while(!got_it && !disable_read)
567         {
568                 frame_lock->lock("FileThread::read_frame 1");
569 // printf("FileThread::read_frame: 1 read_position=%jd ", read_position);
570 // for(int i = 0; i < total_frames; i++)
571 // printf("%jd ", read_frames[i]->position);
572 // printf("\n");
573                 for(int i = 0; i < total_frames; i++)
574                 {
575                         local_frame = read_frames[i];
576                         if(local_frame->position == read_position &&
577                                 local_frame->layer == layer &&
578                                 local_frame->frame &&
579                                 local_frame->frame->equal_stacks(frame) &&
580                                 local_frame->valid)
581                         {
582                                 got_it = 1;
583                                 number = i;
584                                 break;
585                         }
586                 }
587                 frame_lock->unlock();
588
589 // Not decoded yet but thread active
590                 if(!got_it && !disable_read)
591                 {
592                         user_wait_lock->lock("FileThread::read_frame");
593                 }
594         }
595
596 //printf("FileThread::read_frame %d this=%p\n", __LINE__, this);
597
598         if(got_it)
599         {
600 // printf("FileThread::read_frame 1 color_model=%d disable_read=%d\n",
601 // frame->get_color_model(),
602 // disable_read);
603 // Copy image
604                 if(frame->get_color_model() != local_frame->frame->get_color_model() ||
605                         frame->get_w() != local_frame->frame->get_w() ||
606                         frame->get_h() != local_frame->frame->get_h())
607                 {
608 // printf("FileThread::read_frame %d this=%p out cmodel=%d h=%d in cmodel=%d h=%d\n",
609 // __LINE__,
610 // this,
611 // frame->get_color_model(),
612 // frame->get_w(),
613 // local_frame->frame->get_color_model(),
614 // local_frame->frame->get_w());
615                         BC_CModels::transfer(frame->get_rows(),
616                                 local_frame->frame->get_rows(),
617                                 frame->get_y(),
618                                 frame->get_u(),
619                                 frame->get_v(),
620                                 local_frame->frame->get_y(),
621                                 local_frame->frame->get_u(),
622                                 local_frame->frame->get_v(),
623                                 0,
624                                 0,
625                                 local_frame->frame->get_w(),
626                                 local_frame->frame->get_h(),
627                                 0,
628                                 0,
629                                 frame->get_w(),
630                                 frame->get_h(),
631                                 local_frame->frame->get_color_model(),
632                                 frame->get_color_model(),
633                                 0,
634                                 local_frame->frame->get_w(),
635                                 frame->get_w());
636 //for(int i = 0; i < 3000 * 1000 * 4; i++)
637 //((float*)frame->get_rows()[0])[i] = 1;
638 //printf("FileThread::read_frame %d this=%p\n", __LINE__, this);
639                 }
640                 else
641                 {
642 //printf("FileThread::read_frame %d this=%p\n", __LINE__, this);
643                         frame->copy_from(local_frame->frame);
644 //printf("FileThread::read_frame %d this=%p\n", __LINE__, this);
645                 }
646
647 // Can't copy stacks because the stack is needed by the plugin requestor.
648                 frame->copy_params(local_frame->frame);
649 //printf("FileThread::read_frame %d this=%p\n", __LINE__, this);
650
651 // Recycle all frames before current one but not including current one.
652 // This handles redrawing of a single frame but because FileThread has no
653 // notion of a still frame, it has to call read_frame for those.
654                 frame_lock->lock("FileThread::read_frame 1");
655                 FileThreadFrame *new_table[MAX_READ_FRAMES];
656                 int k = 0;
657                 for(int j = number; j < total_frames; j++, k++)
658                 {
659                         new_table[k] = read_frames[j];
660                 }
661                 for(int j = 0; j < number; j++, k++)
662                 {
663                         new_table[k] = read_frames[j];
664                 }
665                 memcpy(read_frames, new_table, sizeof(FileThreadFrame*) * total_frames);
666 //printf("FileThread::read_frame %d this=%p\n", __LINE__, this);
667                 total_frames -= number;
668
669                 start_position = read_position;
670                 read_position++;
671                 frame_lock->unlock();
672                 read_wait_lock->unlock();
673                 return 0;
674         }
675         else
676         {
677 // printf("FileThread::read_frame 2 color_model=%d disable_read=%d\n",
678 // frame->get_color_model(),
679 // disable_read);
680 //printf("FileThread::read_frame %d this=%p\n", __LINE__, this);
681 // Use traditional read function
682                 file->set_layer(layer, 1);
683                 file->set_video_position(read_position, 1);
684                 read_position++;
685                 int result = file->read_frame(frame, 1);
686 //printf("FileThread::read_frame %d this=%p\n", __LINE__, this);
687                 return result;
688         }
689
690
691 //printf("FileThread::read_frame %d this=%p\n", __LINE__, this);
692 }
693
694 int64_t FileThread::get_memory_usage()
695 {
696         frame_lock->lock("FileThread::get_memory_usage");
697         int64_t result = 0;
698         for(int i = 0; i < MAX_READ_FRAMES; i++)
699                 if(read_frames[i] && read_frames[i]->frame)
700                         result += read_frames[i]->frame->get_data_size();
701         frame_lock->unlock();
702         return result;
703 }
704
705
706 Samples** FileThread::get_audio_buffer()
707 {
708         swap_buffer();
709
710         input_lock[current_buffer]->lock("FileThread::get_audio_buffer");
711         return audio_buffer[current_buffer];
712 }
713
714 VFrame*** FileThread::get_video_buffer()
715 {
716         swap_buffer();
717
718         input_lock[current_buffer]->lock("FileThread::get_video_buffer");
719         return video_buffer[current_buffer];
720 }
721
722 VFrame*** FileThread::get_last_video_buffer()
723 {
724         return video_buffer[current_buffer];
725 }
726
727 int FileThread::write_buffer(long size)
728 {
729         output_size[current_buffer] = size;
730
731 // unlock the output lock
732         output_lock[current_buffer]->unlock();
733
734         return return_value;
735 }
736
737 void FileThread::swap_buffer()
738 {
739         current_buffer++;
740         if(current_buffer >= ring_buffers) current_buffer = 0;
741 }
742
743