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