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.
5 #ifndef _VIDEO_ENCODER_H
6 #define _VIDEO_ENCODER_H
9 #include <movit/image_format.h>
19 #include <libavformat/avformat.h>
20 #include <libavformat/avio.h>
23 #include "ref_counted_gl_sync.h"
26 class DiskSpaceEstimator;
30 class QuickSyncEncoder;
31 class RefCountedFrame;
40 VideoEncoder(movit::ResourcePool *resource_pool, QSurface *surface, const std::string &va_display, int width, int height, HTTPD *httpd, DiskSpaceEstimator *disk_space_estimator);
43 void add_audio(int64_t pts, std::vector<float> audio);
45 bool is_zerocopy() const;
47 // Allocate a frame to render into. The returned two textures
48 // are yours to render into (build them into an FBO).
49 // Call end_frame() when you're done.
51 // The semantics of y_tex and cbcr_tex depend on is_zerocopy():
53 // - If false, the are input parameters, ie., the caller
54 // allocates textures. (The contents are not read before
55 // end_frame() is called.)
56 // - If true, they are output parameters, ie., VideoEncoder
57 // allocates textures and borrow them to you for rendering.
58 // In this case, after end_frame(), you are no longer allowed
59 // to use the textures; they are torn down and given to the
61 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 // Call after you are done rendering into the frame; at this point,
64 // y_tex and cbcr_tex will be assumed done, and handed over to the
65 // encoder. The returned fence is purely a convenience; you do not
66 // need to use it for anything, but it's useful if you wanted to set
68 RefCountedGLsync end_frame();
70 // Does a cut of the disk stream immediately ("frame" is used for the filename only).
71 void do_cut(int frame);
73 void change_x264_bitrate(unsigned rate_kbit);
76 void open_output_stream();
77 static int write_packet2_thunk(void *opaque, uint8_t *buf, int buf_size, AVIODataMarkerType type, int64_t time);
78 int write_packet2(uint8_t *buf, int buf_size, AVIODataMarkerType type, int64_t time);
80 AVOutputFormat *oformat;
81 mutable std::mutex qs_mu;
82 std::unique_ptr<QuickSyncEncoder> quicksync_encoder; // Under <qs_mu>.
83 movit::ResourcePool *resource_pool;
85 std::string va_display;
88 DiskSpaceEstimator *disk_space_estimator;
90 bool seen_sync_markers = false;
92 std::unique_ptr<Mux> stream_mux; // To HTTP.
93 std::unique_ptr<AudioEncoder> stream_audio_encoder;
94 std::unique_ptr<X264Encoder> x264_encoder; // nullptr if not using x264.
96 std::string stream_mux_header;
98 std::atomic<int> quicksync_encoders_in_shutdown{0};
99 std::atomic<int> overriding_bitrate{0};
101 // Encoders that are shutdown, but need to call release_gl_resources()
102 // (or be deleted) from some thread with an OpenGL context.
103 std::vector<std::unique_ptr<QuickSyncEncoder>> qs_needing_cleanup; // Under <qs_mu>.