1 #ifndef _MJPEG_ENCODER_H
2 #define _MJPEG_ENCODER_H 1
5 #include "shared/ffmpeg_raii.h"
6 #include "shared/httpd.h"
7 #include "ref_counted_frame.h"
11 #include <libavformat/avio.h>
16 #include <bmusb/bmusb.h>
17 #include <condition_variable>
25 #include <movit/effect.h>
28 struct jpeg_compress_struct;
29 struct VADisplayWithCleanup;
30 struct VectorDestinationManager;
32 #define CHECK_VASTATUS(va_status, func) \
33 if (va_status != VA_STATUS_SUCCESS) { \
34 fprintf(stderr, "%s:%d (%s) failed: %s\n", __func__, __LINE__, func, vaErrorStr(va_status)); \
40 MJPEGEncoder(HTTPD *httpd, const std::string &va_display);
43 void upload_frame(int64_t pts, unsigned card_index, RefCountedFrame frame, const bmusb::VideoFormat &video_format, size_t y_offset, size_t cbcr_offset, std::vector<int32_t> audio, const movit::RGBTriplet &white_balance);
44 bool using_vaapi() const { return va_dpy != nullptr; }
46 bool should_encode_mjpeg_for_card(unsigned card_index);
49 static constexpr int quality = 90;
52 unsigned width, height;
56 VABufferID data_buffer;
60 // RAII wrapper to release VAResources on return (even on error).
61 class ReleaseVAResources {
63 ReleaseVAResources() : committed(true) {}
65 ReleaseVAResources(MJPEGEncoder *mjpeg, const VAResources &resources)
66 : mjpeg(mjpeg), resources(resources) {}
68 ReleaseVAResources(ReleaseVAResources &) = delete;
70 ReleaseVAResources(ReleaseVAResources &&other)
71 : mjpeg(other.mjpeg), resources(other.resources), committed(other.committed) {
75 ReleaseVAResources &operator= (ReleaseVAResources &) = delete;
77 ReleaseVAResources &operator= (ReleaseVAResources &&other) {
79 mjpeg->release_va_resources(resources);
82 resources = std::move(other.resources);
83 committed = other.committed;
91 mjpeg->release_va_resources(resources);
95 void commit() { committed = true; }
98 MJPEGEncoder *mjpeg = nullptr;
99 VAResources resources;
100 bool committed = false;
106 RefCountedFrame frame;
107 bmusb::VideoFormat video_format;
108 size_t y_offset, cbcr_offset;
109 std::vector<int32_t> audio;
110 movit::RGBTriplet white_balance;
112 // Only for frames in the process of being encoded by VA-API.
113 VAResources resources;
114 ReleaseVAResources resource_releaser;
117 void encoder_thread_func();
118 void va_receiver_thread_func();
119 void encode_jpeg_va(QueuedFrame &&qf);
120 std::vector<uint8_t> encode_jpeg_libjpeg(const QueuedFrame &qf);
121 void write_mjpeg_packet(AVFormatContext *avctx, int64_t pts, unsigned stream_index, const uint8_t *jpeg, size_t jpeg_size);
122 void write_audio_packet(AVFormatContext *avctx, int64_t pts, unsigned stream_index, const std::vector<int32_t> &audio);
123 void init_jpeg(unsigned width, unsigned height, const movit::RGBTriplet &white_balance, VectorDestinationManager *dest, jpeg_compress_struct *cinfo, int y_h_samp_factor, int y_v_samp_factor);
124 std::vector<uint8_t> get_jpeg_header(unsigned width, unsigned height, const movit::RGBTriplet &white_balance, int y_h_samp_factor, int y_v_samp_factor, jpeg_compress_struct *cinfo);
125 void add_stream(HTTPD::StreamID stream_id); // Can only be called from the constructor, or the thread owning <streams>.
126 void update_siphon_streams(); // Same.
127 void create_ffmpeg_context(HTTPD::StreamID stream_id);
129 struct WritePacket2Context {
130 MJPEGEncoder *mjpeg_encoder;
131 HTTPD::StreamID stream_id;
133 std::map<HTTPD::StreamID, WritePacket2Context> ffmpeg_contexts; // Statically set up, so we never need to worry about dangling pointers.
134 static int write_packet2_thunk(void *opaque, uint8_t *buf, int buf_size, AVIODataMarkerType type, int64_t time);
135 int write_packet2(HTTPD::StreamID stream_id, uint8_t *buf, int buf_size, AVIODataMarkerType type, int64_t time);
137 std::thread encoder_thread, va_receiver_thread;
140 std::queue<QueuedFrame> frames_to_be_encoded; // Under mu.
141 std::condition_variable any_frames_to_be_encoded; // Governs changes in both frames_to_be_encoded and frames_under_encoding
143 std::queue<QueuedFrame> frames_encoding; // Under mu. Used for VA-API only.
144 std::condition_variable any_frames_encoding;
147 AVFormatContextWithCloser avctx;
148 std::string mux_header;
150 std::map<HTTPD::StreamID, Stream> streams; // Owned by the VA-API receiver thread if VA-API is active, or the encoder thread if not.
152 std::atomic<bool> should_quit{false};
153 bool running = false;
155 std::unique_ptr<VADisplayWithCleanup> va_dpy;
156 VAConfigID config_id_422, config_id_420;
159 unsigned width, height, y_h_samp_factor, y_v_samp_factor;
160 movit::RGBTriplet white_balance;
162 bool operator< (const VAKey &other) const {
163 if (width != other.width)
164 return width < other.width;
165 if (height != other.height)
166 return height < other.height;
167 if (y_h_samp_factor != other.y_h_samp_factor)
168 return y_h_samp_factor < other.y_h_samp_factor;
169 if (y_v_samp_factor != other.y_v_samp_factor)
170 return y_v_samp_factor < other.y_v_samp_factor;
171 if (white_balance.r != other.white_balance.r)
172 return white_balance.r < other.white_balance.r;
173 if (white_balance.g != other.white_balance.g)
174 return white_balance.g < other.white_balance.g;
175 return white_balance.b < other.white_balance.b;
179 std::vector<uint8_t> jpeg_header;
180 VAEncPictureParameterBufferJPEG pic_param;
181 VAQMatrixBufferJPEG q;
182 VAHuffmanTableBufferJPEGBaseline huff;
183 VAEncSliceParameterBufferJPEG parms;
185 std::map<VAKey, VAData> va_data_for_parameters;
186 VAData get_va_data_for_parameters(unsigned width, unsigned height, unsigned y_h_samp_factor, unsigned y_v_samp_factor, const movit::RGBTriplet &white_balance);
188 std::list<VAResources> va_resources_freelist;
189 std::mutex va_resources_mutex;
190 VAResources get_va_resources(unsigned width, unsigned height, uint32_t fourcc);
191 void release_va_resources(VAResources resources);
193 uint8_t *tmp_y, *tmp_cbcr, *tmp_cb, *tmp_cr; // Private to the encoder thread. Used by the libjpeg backend only.
195 std::atomic<int64_t> metric_mjpeg_frames_zero_size_dropped{0};
196 std::atomic<int64_t> metric_mjpeg_frames_interlaced_dropped{0};
197 std::atomic<int64_t> metric_mjpeg_frames_unsupported_pixel_format_dropped{0};
198 std::atomic<int64_t> metric_mjpeg_frames_oversized_dropped{0};
199 std::atomic<int64_t> metric_mjpeg_overrun_dropped{0};
200 std::atomic<int64_t> metric_mjpeg_overrun_submitted{0};
202 friend class PBOFrameAllocator; // FIXME
205 #endif // !defined(_MJPEG_ENCODER_H)