4 * Copyright (C) 2009 Adam Williams <broadcast at earthling dot net>
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.
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.
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
23 #include "bcsignals.h"
24 #include "condition.h"
26 #include "filethread.h"
31 #include "videodevice.inc"
37 FileThreadFrame::FileThreadFrame()
43 FileThreadFrame::~FileThreadFrame()
45 if(frame) delete frame;
50 FileThread::FileThread(File *file, int do_audio, int do_video)
59 FileThread::~FileThread()
66 void FileThread::reset()
87 bzero(read_frames, sizeof(FileThreadFrame*) * MAX_READ_FRAMES);
91 void FileThread::create_objects(File *file,
96 this->do_audio = do_audio;
97 this->do_video = do_video;
98 file_lock = new Mutex("FileThread::file_lock");
99 read_wait_lock = new Condition(0, "FileThread::read_wait_lock");
100 user_wait_lock = new Condition(0, "FileThread::user_wait_lock");
101 frame_lock = new Mutex("FileThread::frame_lock");
102 for(int i = 0; i < MAX_READ_FRAMES; i++)
103 read_frames[i] = new FileThreadFrame;
107 void FileThread::delete_objects()
109 for(int i = 0; i < MAX_READ_FRAMES; i++)
110 delete read_frames[i];
114 for(int i = 0; i < ring_buffers; i++)
116 delete output_lock[i];
118 delete [] output_lock;
123 for(int i = 0; i < ring_buffers; i++)
125 delete input_lock[i];
127 delete [] input_lock;
132 delete [] last_buffer;
135 delete [] output_size;
140 delete read_wait_lock;
141 delete user_wait_lock;
147 void FileThread::run()
151 if(debug) PRINT_TRACE
155 if(debug) PRINT_TRACE
157 while(!done && !disable_read)
159 if(debug) PRINT_TRACE
160 frame_lock->lock("FileThread::run 1");
161 int local_total_frames = total_frames;
162 frame_lock->unlock();
164 if(local_total_frames >= MAX_READ_FRAMES)
166 read_wait_lock->lock("FileThread::run");
170 if(debug) PRINT_TRACE
171 if(done || disable_read) break;
173 // Make local copes of the locked parameters
174 FileThreadFrame *local_frame = 0;
175 int64_t local_position = 0;
177 if(debug) PRINT_TRACE
179 frame_lock->lock("FileThread::run 2");
180 // Get position of next frame to read
182 local_position = read_frames[total_frames - 1]->position + 1;
184 local_position = start_position;
185 //printf("FileThread::run 1 %d " _LD "\n", total_frames, local_position);
187 // Get first available frame
188 local_total_frames = total_frames;
189 local_frame = read_frames[local_total_frames];
191 local_frame->valid = 0;
192 frame_lock->unlock();
197 if(debug) PRINT_TRACE
198 file->set_layer(local_layer, 1);
199 file->set_video_position(local_position, 1);
200 int supported_colormodel =
201 file->get_best_colormodel(PLAYBACK_ASYNCHRONOUS);
202 if(debug) PRINT_TRACE
206 if(local_frame->frame &&
207 !local_frame->frame->params_match(file->asset->width,
209 supported_colormodel))
211 delete local_frame->frame;
212 local_frame->frame = 0;
215 //printf("FileThread::run %d\n", __LINE__);
216 if(!local_frame->frame)
218 local_frame->frame = new VFrame(0,
222 supported_colormodel,
227 // printf("FileThread::run %d w=%d h=%d supported_colormodel=%d\n",
229 // local_frame->frame->get_w(),
230 // local_frame->frame->get_h(),
231 // local_frame->frame->get_color_model());
235 printf("file=%p local_frame->frame=%p\n", file, local_frame->frame);
237 file->read_frame(local_frame->frame, 1);
238 if(debug) PRINT_TRACE
239 local_frame->position = local_position;
240 local_frame->layer = local_layer;
242 // Put frame in last position but since the last position now may be
243 // lower than it was when we got the frame, swap the current
244 // last position with the previous last position.
245 frame_lock->lock("FileThread::run 3");
246 FileThreadFrame *old_frame = read_frames[total_frames];
247 read_frames[local_total_frames] = old_frame;
248 read_frames[total_frames++] = local_frame;
249 local_frame->valid = 1;
250 if(debug) PRINT_TRACE
251 frame_lock->unlock();
254 user_wait_lock->unlock();
255 if(debug) PRINT_TRACE
263 output_lock[local_buffer]->lock("FileThread::run 1");
269 if(!last_buffer[local_buffer])
271 if(output_size[local_buffer])
274 file_lock->lock("FileThread::run 2");
277 result = file->write_samples(
278 audio_buffer[local_buffer],
279 output_size[local_buffer]);
286 for(j = 0; j < file->asset->layers && !result; j++)
287 for(i = 0; i < output_size[local_buffer] && !result; i++)
288 result = file->write_compressed_frame(video_buffer[local_buffer][j][i]);
292 result = file->write_frames(video_buffer[local_buffer],
293 output_size[local_buffer]);
298 return_value = result;
303 output_size[local_buffer] = 0;
308 input_lock[local_buffer]->unlock();
310 if(local_buffer >= ring_buffers) local_buffer = 0;
317 int FileThread::stop_writing()
321 int i, buffer, layer, frame;
324 input_lock[current_buffer]->lock("FileThread::stop_writing 1");
326 last_buffer[current_buffer] = 1;
328 for(i = 0; i < ring_buffers; i++)
329 output_lock[i]->unlock();
333 // wait for thread to finish
337 file_lock->lock("FileThread::stop_writing 2");
340 for(buffer = 0; buffer < ring_buffers; buffer++)
342 for(i = 0; i < file->asset->channels; i++)
343 delete audio_buffer[buffer][i];
344 delete [] audio_buffer[buffer];
346 delete [] audio_buffer;
350 // printf("FileThread::stop_writing %d %d %d %d\n",
353 // file->asset->layers,
357 for(buffer = 0; buffer < ring_buffers; buffer++)
359 for(layer = 0; layer < file->asset->layers; layer++)
361 for(frame = 0; frame < buffer_size; frame++)
363 delete video_buffer[buffer][layer][frame];
365 delete [] video_buffer[buffer][layer];
367 delete [] video_buffer[buffer];
369 delete [] video_buffer;
378 int FileThread::start_writing(long buffer_size,
384 int buffer, layer, frame;
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;
394 file_lock->lock("FileThread::start_writing 1");
399 // Buffer is swapped before first get
400 last_buffer = new int[ring_buffers];
401 output_size = new long[ring_buffers];
404 output_lock = new Condition*[ring_buffers];
405 input_lock = new Condition*[ring_buffers];
406 for(int i = 0; i < ring_buffers; i++)
408 output_lock[i] = new Condition(0, "FileThread::output_lock");
409 input_lock[i] = new Condition(1, "FileThread::input_lock");
418 audio_buffer = new Samples**[ring_buffers];
419 for(buffer = 0; buffer < ring_buffers; buffer++)
421 audio_buffer[buffer] = new Samples*[file->asset->channels];
423 for(int channel = 0; channel < file->asset->channels; channel++)
425 audio_buffer[buffer][channel] = new Samples(buffer_size);
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);
436 video_buffer = new VFrame***[ring_buffers];
437 // printf("FileThread::start_writing 1 %d %d %d %p\n",
439 // file->asset->layers,
442 for(buffer = 0; buffer < ring_buffers; buffer++)
444 video_buffer[buffer] = new VFrame**[file->asset->layers];
445 for(layer = 0; layer < file->asset->layers; layer++)
447 video_buffer[buffer][layer] = new VFrame*[buffer_size];
448 for(frame = 0; frame < buffer_size; frame++)
452 video_buffer[buffer][layer][frame] = new VFrame;
453 //printf("FileThread::start_writing %d %d\n", __LINE__);
457 video_buffer[buffer][layer][frame] =
464 // printf("FileThread::start_writing %d %d %d %d %p\n",
469 // video_buffer[buffer][layer]);
477 for(int i = 0; i < ring_buffers; i++)
488 int FileThread::start_reading()
499 int FileThread::stop_reading()
501 if(is_reading && Thread::running())
504 read_wait_lock->unlock();
510 int FileThread::set_video_position(int64_t position)
512 // If the new position can't be added to the buffer without restarting,
514 if((position < this->start_position ||
515 position >= this->start_position + MAX_READ_FRAMES) &&
519 read_wait_lock->unlock();
523 for(int i = 0; i < MAX_READ_FRAMES; i++)
524 read_frames[i]->valid = 0;
525 this->start_position = position;
528 // If a sequential read, enable reading
529 if(this->start_position + 1 == position && disable_read)
531 this->start_position = position;
538 this->start_position = position;
541 this->read_position = position;
545 int FileThread::set_layer(int layer)
547 if(layer != this->layer)
550 read_wait_lock->unlock();
558 int FileThread::read_frame(VFrame *frame)
560 FileThreadFrame *local_frame = 0;
564 //printf("FileThread::read_frame %d this=%p\n", __LINE__, this);
566 // Search thread for frame
567 while(!got_it && !disable_read)
569 frame_lock->lock("FileThread::read_frame 1");
570 // printf("FileThread::read_frame: 1 read_position=" _LD " ", read_position);
571 // for(int i = 0; i < total_frames; i++)
572 // printf("" _LD " ", read_frames[i]->position);
574 for(int i = 0; i < total_frames; i++)
576 local_frame = read_frames[i];
577 if(local_frame->position == read_position &&
578 local_frame->layer == layer &&
579 local_frame->frame &&
580 local_frame->frame->equal_stacks(frame) &&
588 frame_lock->unlock();
590 // Not decoded yet but thread active
591 if(!got_it && !disable_read)
593 user_wait_lock->lock("FileThread::read_frame");
597 //printf("FileThread::read_frame %d this=%p\n", __LINE__, this);
601 // printf("FileThread::read_frame 1 color_model=%d disable_read=%d\n",
602 // frame->get_color_model(),
605 if(frame->get_color_model() != local_frame->frame->get_color_model() ||
606 frame->get_w() != local_frame->frame->get_w() ||
607 frame->get_h() != local_frame->frame->get_h())
609 // printf("FileThread::read_frame %d this=%p out cmodel=%d h=%d in cmodel=%d h=%d\n",
612 // frame->get_color_model(),
614 // local_frame->frame->get_color_model(),
615 // local_frame->frame->get_w());
616 BC_CModels::transfer(frame->get_rows(),
617 local_frame->frame->get_rows(),
621 local_frame->frame->get_y(),
622 local_frame->frame->get_u(),
623 local_frame->frame->get_v(),
626 local_frame->frame->get_w(),
627 local_frame->frame->get_h(),
632 local_frame->frame->get_color_model(),
633 frame->get_color_model(),
635 local_frame->frame->get_w(),
637 //for(int i = 0; i < 3000 * 1000 * 4; i++)
638 //((float*)frame->get_rows()[0])[i] = 1;
639 //printf("FileThread::read_frame %d this=%p\n", __LINE__, this);
643 //printf("FileThread::read_frame %d this=%p\n", __LINE__, this);
644 frame->copy_from(local_frame->frame);
645 //printf("FileThread::read_frame %d this=%p\n", __LINE__, this);
648 // Can't copy stacks because the stack is needed by the plugin requestor.
649 frame->copy_params(local_frame->frame);
650 //printf("FileThread::read_frame %d this=%p\n", __LINE__, this);
652 // Recycle all frames before current one but not including current one.
653 // This handles redrawing of a single frame but because FileThread has no
654 // notion of a still frame, it has to call read_frame for those.
655 frame_lock->lock("FileThread::read_frame 1");
656 FileThreadFrame *new_table[MAX_READ_FRAMES];
658 for(int j = number; j < total_frames; j++, k++)
660 new_table[k] = read_frames[j];
662 for(int j = 0; j < number; j++, k++)
664 new_table[k] = read_frames[j];
666 memcpy(read_frames, new_table, sizeof(FileThreadFrame*) * total_frames);
667 //printf("FileThread::read_frame %d this=%p\n", __LINE__, this);
668 total_frames -= number;
670 start_position = read_position;
672 frame_lock->unlock();
673 read_wait_lock->unlock();
678 // printf("FileThread::read_frame 2 color_model=%d disable_read=%d\n",
679 // frame->get_color_model(),
681 //printf("FileThread::read_frame %d this=%p\n", __LINE__, this);
682 // Use traditional read function
683 file->set_layer(layer, 1);
684 file->set_video_position(read_position, 1);
686 int result = file->read_frame(frame, 1);
687 //printf("FileThread::read_frame %d this=%p\n", __LINE__, this);
692 //printf("FileThread::read_frame %d this=%p\n", __LINE__, this);
695 int64_t FileThread::get_memory_usage()
697 frame_lock->lock("FileThread::get_memory_usage");
699 for(int i = 0; i < MAX_READ_FRAMES; i++)
700 if(read_frames[i] && read_frames[i]->frame)
701 result += read_frames[i]->frame->get_data_size();
702 frame_lock->unlock();
707 Samples** FileThread::get_audio_buffer()
711 input_lock[current_buffer]->lock("FileThread::get_audio_buffer");
712 return audio_buffer[current_buffer];
715 VFrame*** FileThread::get_video_buffer()
719 input_lock[current_buffer]->lock("FileThread::get_video_buffer");
720 return video_buffer[current_buffer];
723 VFrame*** FileThread::get_last_video_buffer()
725 return video_buffer[current_buffer];
728 int FileThread::write_buffer(long size)
730 output_size[current_buffer] = size;
732 // unlock the output lock
733 output_lock[current_buffer]->unlock();
738 int FileThread::swap_buffer()
741 if(current_buffer >= ring_buffers) current_buffer = 0;