]> git.sesse.net Git - nageru/blob - video_encoder.h
Support switching Y'CbCr coefficients midway, which will allow doing the Right Thing...
[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 "ref_counted_gl_sync.h"
24
25 class AudioEncoder;
26 class DiskSpaceEstimator;
27 class HTTPD;
28 class Mux;
29 class QSurface;
30 class QuickSyncEncoder;
31 class RefCountedFrame;
32 class X264Encoder;
33
34 namespace movit {
35 class ResourcePool;
36 }  // namespace movit
37
38 class VideoEncoder {
39 public:
40         VideoEncoder(movit::ResourcePool *resource_pool, QSurface *surface, const std::string &va_display, int width, int height, HTTPD *httpd, DiskSpaceEstimator *disk_space_estimator);
41         ~VideoEncoder();
42
43         void add_audio(int64_t pts, std::vector<float> audio);
44
45         // Allocate a frame to render into. The returned two textures
46         // are yours to render into (build them into an FBO).
47         // Call end_frame() when you're done.
48         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);
49
50         // Call after you are done rendering into the frame; at this point,
51         // y_tex and cbcr_tex will be assumed done, and handed over to the
52         // encoder. The returned fence is purely a convenience; you do not
53         // need to use it for anything, but it's useful if you wanted to set
54         // one anyway.
55         RefCountedGLsync end_frame();
56
57         // Does a cut of the disk stream immediately ("frame" is used for the filename only).
58         void do_cut(int frame);
59
60         void change_x264_bitrate(unsigned rate_kbit);
61
62 private:
63         void open_output_stream();
64         static int write_packet2_thunk(void *opaque, uint8_t *buf, int buf_size, AVIODataMarkerType type, int64_t time);
65         int write_packet2(uint8_t *buf, int buf_size, AVIODataMarkerType type, int64_t time);
66
67         AVOutputFormat *oformat;
68         std::mutex qs_mu;
69         std::unique_ptr<QuickSyncEncoder> quicksync_encoder;  // Under <qs_mu>.
70         movit::ResourcePool *resource_pool;
71         QSurface *surface;
72         std::string va_display;
73         int width, height;
74         HTTPD *httpd;
75         DiskSpaceEstimator *disk_space_estimator;
76
77         bool seen_sync_markers = false;
78
79         std::unique_ptr<Mux> stream_mux;  // To HTTP.
80         std::unique_ptr<AudioEncoder> stream_audio_encoder;
81         std::unique_ptr<X264Encoder> x264_encoder;  // nullptr if not using x264.
82
83         std::string stream_mux_header;
84
85         std::atomic<int> quicksync_encoders_in_shutdown{0};
86
87         // Encoders that are shutdown, but need to call release_gl_resources()
88         // (or be deleted) from some thread with an OpenGL context.
89         std::vector<std::unique_ptr<QuickSyncEncoder>> qs_needing_cleanup;  // Under <qs_mu>.
90 };
91
92 #endif