]> git.sesse.net Git - nageru/blob - nageru/video_encoder.h
Fix a comment typo.
[nageru] / nageru / video_encoder.h
1 // A class to orchestrate the concept of video encoding. Will keep track of
2 // the muxes to stream and disk, the QuickSyncEncoder, and also the X264Encoder
3 // (for the stream) if there is one.
4
5 #ifndef _VIDEO_ENCODER_H
6 #define _VIDEO_ENCODER_H
7
8 #include <epoxy/gl.h>
9 #include <movit/image_format.h>
10 #include <stdbool.h>
11 #include <stdint.h>
12 #include <atomic>
13 #include <memory>
14 #include <mutex>
15 #include <string>
16 #include <vector>
17
18 extern "C" {
19 #include <libavformat/avformat.h>
20 #include <libavformat/avio.h>
21 }
22
23 #include "shared/mux.h"
24 #include "shared/ref_counted_gl_sync.h"
25
26 class AudioEncoder;
27 class DiskSpaceEstimator;
28 class HTTPD;
29 class Mux;
30 class QSurface;
31 class QuickSyncEncoder;
32 class RefCountedFrame;
33 class X264Encoder;
34
35 namespace movit {
36 class ResourcePool;
37 }  // namespace movit
38
39 class VideoEncoder {
40 public:
41         VideoEncoder(movit::ResourcePool *resource_pool, QSurface *surface, const std::string &va_display, int width, int height, HTTPD *httpd, DiskSpaceEstimator *disk_space_estimator);
42         ~VideoEncoder();
43
44         void add_audio(int64_t pts, std::vector<float> audio);
45
46         bool is_zerocopy() const;
47
48         // Allocate a frame to render into. The returned two textures
49         // are yours to render into (build them into an FBO).
50         // Call end_frame() when you're done.
51         //
52         // The semantics of y_tex and cbcr_tex depend on is_zerocopy():
53         //
54         //   - If false, they are input parameters, ie., the caller
55         //     allocates textures. (The contents are not read before
56         //     end_frame() is called.)
57         //   - If true, they are output parameters, ie., VideoEncoder
58         //     allocates textures and borrow them to you for rendering.
59         //     In this case, after end_frame(), you are no longer allowed
60         //     to use the textures; they are torn down and given to the
61         //     H.264 encoder.
62         bool begin_frame(int64_t pts, int64_t duration, movit::YCbCrLumaCoefficients ycbcr_coefficients, const std::vector<RefCountedFrame> &input_frames, GLuint *y_tex, GLuint *cbcr_tex);
63
64         // Call after you are done rendering into the frame; at this point,
65         // y_tex and cbcr_tex will be assumed done, and handed over to the
66         // encoder. The returned fence is purely a convenience; you do not
67         // need to use it for anything, but it's useful if you wanted to set
68         // one anyway.
69         RefCountedGLsync end_frame();
70
71         // Does a cut of the disk stream immediately ("frame" is used for the filename only).
72         void do_cut(int frame);
73
74         void change_x264_bitrate(unsigned rate_kbit);
75
76 private:
77         void open_output_stream();
78         static int write_packet2_thunk(void *opaque, uint8_t *buf, int buf_size, AVIODataMarkerType type, int64_t time);
79         int write_packet2(uint8_t *buf, int buf_size, AVIODataMarkerType type, int64_t time);
80
81         AVOutputFormat *oformat;
82         mutable std::mutex qs_mu, qs_audio_mu;
83         std::unique_ptr<QuickSyncEncoder> quicksync_encoder;  // Under <qs_mu> _and_ <qs_audio_mu>.
84         movit::ResourcePool *resource_pool;
85         QSurface *surface;
86         std::string va_display;
87         int width, height;
88         HTTPD *httpd;
89         DiskSpaceEstimator *disk_space_estimator;
90
91         bool seen_sync_markers = false;
92
93         std::unique_ptr<Mux> stream_mux;  // To HTTP.
94         std::unique_ptr<AudioEncoder> stream_audio_encoder;
95         std::unique_ptr<X264Encoder> x264_encoder;  // nullptr if not using x264.
96
97         std::string stream_mux_header;
98         MuxMetrics stream_mux_metrics;
99
100         std::atomic<int> quicksync_encoders_in_shutdown{0};
101         std::atomic<int> overriding_bitrate{0};
102
103         // Encoders that are shutdown, but need to call release_gl_resources()
104         // (or be deleted) from some thread with an OpenGL context.
105         std::vector<std::unique_ptr<QuickSyncEncoder>> qs_needing_cleanup;  // Under <qs_mu>.
106 };
107
108 #endif