1 #ifndef _MJPEG_ENCODER_H
2 #define _MJPEG_ENCODER_H 1
4 #include "shared/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);
35 void upload_frame(int64_t pts, unsigned card_index, RefCountedFrame frame, const bmusb::VideoFormat &video_format, size_t y_offset, size_t cbcr_offset);
38 static constexpr int quality = 90;
41 unsigned width, height;
44 VABufferID data_buffer;
47 // RAII wrapper to release VAResources on return (even on error).
48 class ReleaseVAResources {
50 ReleaseVAResources() : committed(true) {}
52 ReleaseVAResources(MJPEGEncoder *mjpeg, const VAResources &resources)
53 : mjpeg(mjpeg), resources(resources) {}
55 ReleaseVAResources(ReleaseVAResources &) = delete;
57 ReleaseVAResources(ReleaseVAResources &&other)
58 : mjpeg(other.mjpeg), resources(other.resources), committed(other.committed) {
62 ReleaseVAResources &operator= (ReleaseVAResources &) = delete;
64 ReleaseVAResources &operator= (ReleaseVAResources &&other) {
66 mjpeg->release_va_resources(resources);
69 resources = std::move(other.resources);
70 committed = other.committed;
78 mjpeg->release_va_resources(resources);
82 void commit() { committed = true; }
85 MJPEGEncoder *mjpeg = nullptr;
86 VAResources resources;
87 bool committed = false;
93 RefCountedFrame frame;
94 bmusb::VideoFormat video_format;
95 size_t y_offset, cbcr_offset;
97 // Only for frames in the process of being encoded by VA-API.
98 VAResources resources;
99 ReleaseVAResources resource_releaser;
102 void encoder_thread_func();
103 void va_receiver_thread_func();
104 void encode_jpeg_va(QueuedFrame &&qf);
105 std::vector<uint8_t> encode_jpeg_libjpeg(const QueuedFrame &qf);
106 void write_mjpeg_packet(int64_t pts, unsigned card_index, const std::vector<uint8_t> &jpeg);
107 void init_jpeg_422(unsigned width, unsigned height, VectorDestinationManager *dest, jpeg_compress_struct *cinfo);
108 std::vector<uint8_t> get_jpeg_header(unsigned width, unsigned height, jpeg_compress_struct *cinfo);
110 static int write_packet2_thunk(void *opaque, uint8_t *buf, int buf_size, AVIODataMarkerType type, int64_t time);
111 int write_packet2(uint8_t *buf, int buf_size, AVIODataMarkerType type, int64_t time);
113 std::thread encoder_thread, va_receiver_thread;
116 std::queue<QueuedFrame> frames_to_be_encoded; // Under mu.
117 std::condition_variable any_frames_to_be_encoded; // Governs changes in both frames_to_be_encoded and frames_under_encoding
119 std::queue<QueuedFrame> frames_encoding; // Under mu. Used for VA-API only.
120 std::condition_variable any_frames_encoding;
122 AVFormatContextWithCloser avctx;
124 std::string mux_header;
125 std::atomic<bool> should_quit{false};
126 bool running = false;
128 std::unique_ptr<VADisplayWithCleanup> va_dpy;
129 VAConfigID config_id;
132 std::vector<uint8_t> jpeg_header;
133 VAEncPictureParameterBufferJPEG pic_param;
134 VAQMatrixBufferJPEG q;
135 VAHuffmanTableBufferJPEGBaseline huff;
136 VAEncSliceParameterBufferJPEG parms;
138 std::map<std::pair<unsigned, unsigned>, VAData> va_data_for_resolution;
139 VAData get_va_data_for_resolution(unsigned width, unsigned height);
141 std::list<VAResources> va_resources_freelist;
142 std::mutex va_resources_mutex;
143 VAResources get_va_resources(unsigned width, unsigned height);
144 void release_va_resources(VAResources resources);
146 static std::unique_ptr<VADisplayWithCleanup> try_open_va(const std::string &va_display, std::string *error, VAConfigID *config_id);
148 uint8_t *tmp_y, *tmp_cbcr, *tmp_cb, *tmp_cr; // Private to the encoder thread. Used by the libjpeg backend only.
150 std::atomic<int64_t> metric_mjpeg_frames_zero_size_dropped{0};
151 std::atomic<int64_t> metric_mjpeg_frames_interlaced_dropped{0};
152 std::atomic<int64_t> metric_mjpeg_frames_unsupported_pixel_format_dropped{0};
153 std::atomic<int64_t> metric_mjpeg_frames_oversized_dropped{0};
154 std::atomic<int64_t> metric_mjpeg_overrun_dropped{0};
155 std::atomic<int64_t> metric_mjpeg_overrun_submitted{0};
158 #endif // !defined(_MJPEG_ENCODER_H)