1 #ifndef _MJPEG_ENCODER_H
2 #define _MJPEG_ENCODER_H 1
4 #include "ffmpeg_raii.h"
5 #include "ref_counted_frame.h"
9 #include <libavformat/avio.h>
14 #include <bmusb/bmusb.h>
15 #include <condition_variable>
26 struct jpeg_compress_struct;
27 struct VADisplayWithCleanup;
28 struct VectorDestinationManager;
32 MJPEGEncoder(HTTPD *httpd, const std::string &va_display);
34 void upload_frame(int64_t pts, unsigned card_index, RefCountedFrame frame, const bmusb::VideoFormat &video_format, size_t y_offset, size_t cbcr_offset);
37 static constexpr int quality = 90;
40 unsigned width, height;
43 VABufferID data_buffer;
46 // RAII wrapper to release VAResources on return (even on error).
47 class ReleaseVAResources {
49 ReleaseVAResources() : committed(true) {}
51 ReleaseVAResources(MJPEGEncoder *mjpeg, const VAResources &resources)
52 : mjpeg(mjpeg), resources(resources) {}
54 ReleaseVAResources(ReleaseVAResources &) = delete;
56 ReleaseVAResources(ReleaseVAResources &&other)
57 : mjpeg(other.mjpeg), resources(other.resources), committed(other.committed) {
61 ReleaseVAResources &operator= (ReleaseVAResources &) = delete;
63 ReleaseVAResources &operator= (ReleaseVAResources &&other) {
65 mjpeg->release_va_resources(resources);
68 resources = std::move(other.resources);
69 committed = other.committed;
77 mjpeg->release_va_resources(resources);
81 void commit() { committed = true; }
84 MJPEGEncoder *mjpeg = nullptr;
85 VAResources resources;
86 bool committed = false;
92 RefCountedFrame frame;
93 bmusb::VideoFormat video_format;
94 size_t y_offset, cbcr_offset;
96 // Only for frames in the process of being encoded by VA-API.
97 VAResources resources;
98 ReleaseVAResources resource_releaser;
101 void encoder_thread_func();
102 void va_receiver_thread_func();
103 void encode_jpeg_va(QueuedFrame &&qf);
104 std::vector<uint8_t> encode_jpeg_libjpeg(const QueuedFrame &qf);
105 void write_mjpeg_packet(int64_t pts, unsigned card_index, const std::vector<uint8_t> &jpeg);
106 void init_jpeg_422(unsigned width, unsigned height, VectorDestinationManager *dest, jpeg_compress_struct *cinfo);
107 std::vector<uint8_t> get_jpeg_header(unsigned width, unsigned height, jpeg_compress_struct *cinfo);
109 static int write_packet2_thunk(void *opaque, uint8_t *buf, int buf_size, AVIODataMarkerType type, int64_t time);
110 int write_packet2(uint8_t *buf, int buf_size, AVIODataMarkerType type, int64_t time);
112 std::thread encoder_thread, va_receiver_thread;
115 std::queue<QueuedFrame> frames_to_be_encoded; // Under mu.
116 std::condition_variable any_frames_to_be_encoded; // Governs changes in both frames_to_be_encoded and frames_under_encoding
118 std::queue<QueuedFrame> frames_encoding; // Under mu. Used for VA-API only.
119 std::condition_variable any_frames_encoding;
121 AVFormatContextWithCloser avctx;
123 std::string mux_header;
124 std::atomic<bool> should_quit{false};
125 bool running = false;
127 std::unique_ptr<VADisplayWithCleanup> va_dpy;
128 VAConfigID config_id;
131 std::vector<uint8_t> jpeg_header;
132 VAEncPictureParameterBufferJPEG pic_param;
133 VAQMatrixBufferJPEG q;
134 VAHuffmanTableBufferJPEGBaseline huff;
135 VAEncSliceParameterBufferJPEG parms;
137 std::map<std::pair<unsigned, unsigned>, VAData> va_data_for_resolution;
138 VAData get_va_data_for_resolution(unsigned width, unsigned height);
140 std::list<VAResources> va_resources_freelist;
141 std::mutex va_resources_mutex;
142 VAResources get_va_resources(unsigned width, unsigned height);
143 void release_va_resources(VAResources resources);
145 static std::unique_ptr<VADisplayWithCleanup> try_open_va(const std::string &va_display, std::string *error, VAConfigID *config_id);
147 uint8_t *tmp_y, *tmp_cbcr, *tmp_cb, *tmp_cr; // Private to the encoder thread. Used by the libjpeg backend only.
150 #endif // !defined(_MJPEG_ENCODER_H)