]> git.sesse.net Git - nageru/blob - video_stream.h
Fix a threading violation.
[nageru] / video_stream.h
1 #ifndef _VIDEO_STREAM_H
2 #define _VIDEO_STREAM_H 1
3
4 #include <stdint.h>
5 #include <epoxy/gl.h>
6
7 extern "C" {
8 #include <libavformat/avio.h>
9 }
10
11 #include <condition_variable>
12 #include <deque>
13 #include <mutex>
14 #include <string>
15 #include <thread>
16
17 #include <movit/effect_chain.h>
18 #include <movit/mix_effect.h>
19 #include <movit/ycbcr_input.h>
20
21 #include "ref_counted_gl_sync.h"
22
23 class ChromaSubsampler;
24 class DISComputeFlow;
25 class Interpolate;
26 class Mux;
27 class QSurface;
28 class QSurfaceFormat;
29 class YCbCrConverter;
30
31 class VideoStream {
32 public:
33         VideoStream();
34         ~VideoStream();
35         void start();
36         void stop();
37
38         void schedule_original_frame(int64_t output_pts, unsigned stream_idx, int64_t input_pts);
39         void schedule_faded_frame(int64_t output_pts, unsigned stream_idx, int64_t input_pts, int secondary_stream_idx, int64_t secondary_input_pts, float fade_alpha);
40         void schedule_interpolated_frame(int64_t output_pts, unsigned stream_idx, int64_t input_first_pts, int64_t input_second_pts, float alpha, int secondary_stream_idx = -1, int64_t secondary_inputs_pts = -1, float fade_alpha = 0.0f); // -1 = no secondary frame.
41
42 private:
43
44         void encode_thread_func();
45         std::thread encode_thread;
46
47         static int write_packet2_thunk(void *opaque, uint8_t *buf, int buf_size, AVIODataMarkerType type, int64_t time);
48         int write_packet2(uint8_t *buf, int buf_size, AVIODataMarkerType type, int64_t time);
49
50         // Allocated at the very start; if we're empty, we start dropping frames
51         // (so that we don't build up an infinite interpolation backlog).
52         struct InterpolatedFrameResources {
53                 GLuint input_tex;  // Layered (contains both input frames), Y'CbCr.
54                 GLuint gray_tex;  // Same, but Y only.
55                 GLuint input_fbos[2];  // For rendering to the two layers of input_tex.
56
57                 // Destination textures and FBO if there is a fade.
58                 GLuint fade_y_output_tex, fade_cbcr_output_tex;
59                 GLuint fade_fbo;
60
61                 GLuint cb_tex, cr_tex;  // Subsampled, final output.
62
63                 GLuint pbo;  // For reading the data back.
64                 void *pbo_contents;  // Persistently mapped.
65         };
66         std::deque<InterpolatedFrameResources> interpolate_resources;  // Under <queue_lock>.
67         static constexpr size_t num_interpolate_slots = 10;
68
69         struct QueuedFrame {
70                 int64_t output_pts;
71                 enum Type { ORIGINAL, FADED, INTERPOLATED, FADED_INTERPOLATED } type;
72                 unsigned stream_idx;
73                 int64_t input_first_pts;  // The only pts for original frames.  
74
75                 // For fades only (including fades against interpolated frames).
76                 int secondary_stream_idx = -1;
77                 int64_t secondary_input_pts;
78
79                 // For interpolated frames only.
80                 int64_t input_second_pts;
81                 float alpha;
82                 InterpolatedFrameResources resources;
83                 RefCountedGLsync fence;  // Set when the interpolated image is read back to the CPU.
84                 GLuint flow_tex, output_tex, cbcr_tex;  // Released in the receiving thread; not really used for anything else.
85         };
86         std::deque<QueuedFrame> frame_queue;  // Under <queue_lock>.
87         std::mutex queue_lock;
88         std::condition_variable queue_nonempty;
89
90         std::unique_ptr<Mux> stream_mux;  // To HTTP.
91         std::string stream_mux_header;
92         bool seen_sync_markers = false;
93
94         std::unique_ptr<YCbCrConverter> ycbcr_converter;
95         std::unique_ptr<YCbCrConverter> ycbcr_semiplanar_converter;
96
97         // Frame interpolation.
98         std::unique_ptr<DISComputeFlow> compute_flow;
99         std::unique_ptr<Interpolate> interpolate, interpolate_no_split;
100         std::unique_ptr<ChromaSubsampler> chroma_subsampler;
101 };
102
103 #endif  // !defined(_VIDEO_STREAM_H)