Olive
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros
cacher.h
Go to the documentation of this file.
1 /***
2 
3  Olive - Non-Linear Video Editor
4  Copyright (C) 2019 Olive Team
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 3 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, see <http://www.gnu.org/licenses/>.
18 
19 ***/
20 
21 #ifndef CACHER_H
22 #define CACHER_H
23 
24 #define __STDC_FORMAT_MACROS 1
25 #include <inttypes.h>
26 
27 extern "C" {
28 #include <libavformat/avformat.h>
29 #include <libavcodec/avcodec.h>
30 #include <libswscale/swscale.h>
31 #include <libswresample/swresample.h>
32 #include <libavfilter/avfilter.h>
33 #include <libavfilter/buffersrc.h>
34 #include <libavfilter/buffersink.h>
35 #include <libavutil/opt.h>
36 #include <libavutil/pixdesc.h>
37 }
38 
39 #include <memory>
40 #include <QThread>
41 #include <QVector>
42 #include <QWaitCondition>
43 #include <QMutex>
44 
45 #include "rendering/clipqueue.h"
46 
47 class Clip;
48 
100 class Cacher : public QThread
101 {
102  Q_OBJECT
103 public:
111  Cacher(Clip* c);
112 
119  void run();
120 
130  void Open();
131 
163  void Cache(long playhead, bool scrubbing, QVector<Clip*>& nests, int playback_speed);
164 
179  AVFrame* Retrieve();
180 
193  void Close(bool wait_for_finish);
194 
203  void ResetAudio();
204 
218  int media_width();
219 
231  int media_height();
232 
246  AVRational media_time_base();
247 
255  ClipQueue* queue();
256 
257 private:
262 
269 
276  QWaitCondition wait_cond_;
277 
283  QWaitCondition main_thread_wait_;
284 
291 
297  QWaitCondition retrieve_wait_;
298 
305 
312 
316  long playhead_;
317 
322 
327 
331  QVector<Clip*> nests_;
332 
343  bool queued_;
344 
353 
354  // ffmpeg media handling
358  AVFormatContext* formatCtx;
359 
363  AVCodecContext* codecCtx;
364 
368  AVStream* stream;
369 
373  AVPacket* pkt;
374 
381  AVFrame* frame_;
382 
389  AVFrame* retrieved_frame = nullptr;
390 
391  // converters/filters
399  AVFilterGraph* filter_graph;
400 
406  AVFilterContext* buffersrc_ctx;
407 
413  AVFilterContext* buffersink_ctx;
414 
418  AVCodec* codec;
419 
423  AVDictionary* opts;
424 
425  // audio playback variables
432 
440 
447 
454 
459 
466  bool caching_;
467 
474 
485  void OpenWorker();
486 
493  void CacheWorker();
494 
501  void CloseWorker();
502 
509  void Reset();
510 
518  void SetRetrievedFrame(AVFrame* f);
519 
528  void WakeMainThread();
529 
545  int RetrieveFrameFromDecoder(AVFrame* f);
546 
563  int RetrieveFrameAndProcess(AVFrame **f);
564 
571  void CacheVideoWorker();
572 
579  void CacheAudioWorker();
580 
584  bool IsReversed();
585 };
586 
587 #endif // CACHER_H
QWaitCondition wait_cond_
Main wait condition.
Definition: cacher.h:276
AVStream * stream
FFmpeg stream - used for media decoding.
Definition: cacher.h:368
void OpenWorker()
Internal function for opening the file handles and decoder.
Definition: cacher.cpp:861
void Open()
Open the cacher.
Definition: cacher.cpp:1184
AVFilterContext * buffersrc_ctx
FFmpeg buffer source.
Definition: cacher.h:406
void CacheWorker()
Internal function for starting a cache cycle.
Definition: cacher.cpp:1100
Clip * clip
Reference to the parent clip. Set in the constructor and never changed during this object&#39;s lifetime...
Definition: cacher.h:261
QWaitCondition main_thread_wait_
Main thread wait condition.
Definition: cacher.h:283
bool queued_
Signal cache to continue operation after one cycle rather than wait for another signal.
Definition: cacher.h:343
QMutex main_thread_lock_
Main thread mutex.
Definition: cacher.h:290
The ClipQueue class.
Definition: clipqueue.h:36
void SetRetrievedFrame(AVFrame *f)
Internal function for setting retrieved_frame and waking up any threads waiting for it...
Definition: cacher.cpp:833
AVPacket * pkt
FFmpeg packet - used for media decoding.
Definition: cacher.h:373
AVFrame * frame_
FFmpeg frame - used for media decoding.
Definition: cacher.h:381
void WakeMainThread()
Internal function to wake an external calling thread.
Definition: cacher.cpp:843
void Close(bool wait_for_finish)
Close the cacher and free any allocated memory.
Definition: cacher.cpp:1299
AVCodec * codec
FFmpeg codec reference.
Definition: cacher.h:418
bool reached_end
Set and used by CacheAudioWorker if the decoder receives an EOF.
Definition: cacher.h:311
void Reset()
Internal function for resetting audio state.
Definition: cacher.cpp:794
int RetrieveFrameAndProcess(AVFrame **f)
Retrieve frame from decoder and run it through filter stack.
Definition: cacher.cpp:1383
ClipQueue queue_
Frame queue.
Definition: cacher.h:268
int RetrieveFrameFromDecoder(AVFrame *f)
Retrieve frame from decoder.
Definition: cacher.cpp:1341
QMutex retrieve_lock_
Retrieve() mutex.
Definition: cacher.h:304
AVFrame * Retrieve()
Retrieve frame requested by Cache()
Definition: cacher.cpp:1263
The Cacher class.
Definition: cacher.h:100
void Cache(long playhead, bool scrubbing, QVector< Clip * > &nests, int playback_speed)
Request a frame to be cached.
Definition: cacher.cpp:1195
long playhead_
Current Sequence playhead set by Cache()
Definition: cacher.h:316
Definition: clip.h:56
Cacher(Clip *c)
Cacher Constructor.
Definition: cacher.cpp:850
int64_t reverse_target_
Internal reverse target variable.
Definition: cacher.h:439
AVFilterContext * buffersink_ctx
FFmpeg buffer sink.
Definition: cacher.h:413
AVFrame * retrieved_frame
Retrieved frame reference for Retrieve()
Definition: cacher.h:389
int media_height()
Retrieve current media height.
Definition: cacher.cpp:1326
void CloseWorker()
Internal function for closing cacher.
Definition: cacher.cpp:1110
qint64 audio_buffer_write
Internal audio buffer write variable.
Definition: cacher.h:453
QVector< Clip * > nests_
Current nested Sequence hierarchy set by Cache()
Definition: cacher.h:331
int playback_speed_
Current Sequence playback speed set by Cache()
Definition: cacher.h:326
void run()
The main QThread loop.
Definition: cacher.cpp:1153
bool caching_
Main while loop condition to determine whether thread should continue looping.
Definition: cacher.h:466
void CacheVideoWorker()
Internal video caching function.
Definition: cacher.cpp:473
AVFilterGraph * filter_graph
FFmpeg filter stack.
Definition: cacher.h:399
AVFormatContext * formatCtx
FFmpeg format/file context - used for media decoding.
Definition: cacher.h:358
bool is_valid_state_
Internal variable for whether the current Cacher state is valid or not.
Definition: cacher.h:473
AVRational media_time_base()
Retrieve media time base.
Definition: cacher.cpp:1331
int frame_sample_index_
Internal frame sample index variable.
Definition: cacher.h:446
ClipQueue * queue()
Get cacher queue object.
Definition: cacher.cpp:1336
QWaitCondition retrieve_wait_
Retrieve() wait condition.
Definition: cacher.h:297
AVCodecContext * codecCtx
FFmpeg decoder context - used for media decoding.
Definition: cacher.h:363
bool interrupt_
Interrupt the current cache cycle.
Definition: cacher.h:352
long audio_target_frame
Internal variable that holds the playhead the last time the audio state was reset.
Definition: cacher.h:458
bool audio_reset_
Internal audio reset variable.
Definition: cacher.h:431
int media_width()
Retrieve current media width.
Definition: cacher.cpp:1321
bool IsReversed()
Internal function using the Cacher&#39;s known information to determine whether this media is playing in ...
Definition: cacher.cpp:467
bool scrubbing_
Current Sequence scrubbing state set by Cache()
Definition: cacher.h:321
AVDictionary * opts
Options set by the cacher for FFmpeg&#39;s decoders (settings like multithreading or other optimizations)...
Definition: cacher.h:423
void CacheAudioWorker()
Internal audio caching function.
Definition: cacher.cpp:104
void ResetAudio()
Interrupt and reset audio state.
Definition: cacher.cpp:1309