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>
23 #include <movit/effect.h>
27 struct jpeg_compress_struct;
28 struct VADisplayWithCleanup;
29 struct VectorDestinationManager;
31 #define CHECK_VASTATUS(va_status, func) \
32 if (va_status != VA_STATUS_SUCCESS) { \
33 fprintf(stderr, "%s:%d (%s) failed with %d\n", __func__, __LINE__, func, va_status); \
39 MJPEGEncoder(HTTPD *httpd, const std::string &va_display);
42 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);
43 bool using_vaapi() const { return va_dpy != nullptr; }
45 // Returns -1 for inactive (ie., don't encode frames for this card right now).
46 int get_mjpeg_stream_for_card(unsigned card_index);
49 static constexpr int quality = 90;
52 unsigned width, height;
55 VABufferID data_buffer;
59 // RAII wrapper to release VAResources on return (even on error).
60 class ReleaseVAResources {
62 ReleaseVAResources() : committed(true) {}
64 ReleaseVAResources(MJPEGEncoder *mjpeg, const VAResources &resources)
65 : mjpeg(mjpeg), resources(resources) {}
67 ReleaseVAResources(ReleaseVAResources &) = delete;
69 ReleaseVAResources(ReleaseVAResources &&other)
70 : mjpeg(other.mjpeg), resources(other.resources), committed(other.committed) {
74 ReleaseVAResources &operator= (ReleaseVAResources &) = delete;
76 ReleaseVAResources &operator= (ReleaseVAResources &&other) {
78 mjpeg->release_va_resources(resources);
81 resources = std::move(other.resources);
82 committed = other.committed;
90 mjpeg->release_va_resources(resources);
94 void commit() { committed = true; }
97 MJPEGEncoder *mjpeg = nullptr;
98 VAResources resources;
99 bool committed = false;
105 RefCountedFrame frame;
106 bmusb::VideoFormat video_format;
107 size_t y_offset, cbcr_offset;
108 std::vector<int32_t> audio;
109 movit::RGBTriplet white_balance;
111 // Only for frames in the process of being encoded by VA-API.
112 VAResources resources;
113 ReleaseVAResources resource_releaser;
116 void encoder_thread_func();
117 void va_receiver_thread_func();
118 void encode_jpeg_va(QueuedFrame &&qf);
119 std::vector<uint8_t> encode_jpeg_libjpeg(const QueuedFrame &qf);
120 void write_mjpeg_packet(int64_t pts, unsigned card_index, const uint8_t *jpeg, size_t jpeg_size);
121 void write_audio_packet(int64_t pts, unsigned card_index, const std::vector<int32_t> &audio);
122 void init_jpeg_422(unsigned width, unsigned height, const movit::RGBTriplet &white_balance, VectorDestinationManager *dest, jpeg_compress_struct *cinfo);
123 std::vector<uint8_t> get_jpeg_header(unsigned width, unsigned height, const movit::RGBTriplet &white_balance, jpeg_compress_struct *cinfo);
125 static int write_packet2_thunk(void *opaque, uint8_t *buf, int buf_size, AVIODataMarkerType type, int64_t time);
126 int write_packet2(uint8_t *buf, int buf_size, AVIODataMarkerType type, int64_t time);
128 std::thread encoder_thread, va_receiver_thread;
131 std::queue<QueuedFrame> frames_to_be_encoded; // Under mu.
132 std::condition_variable any_frames_to_be_encoded; // Governs changes in both frames_to_be_encoded and frames_under_encoding
134 std::queue<QueuedFrame> frames_encoding; // Under mu. Used for VA-API only.
135 std::condition_variable any_frames_encoding;
137 AVFormatContextWithCloser avctx;
139 std::string mux_header;
140 std::atomic<bool> should_quit{false};
141 bool running = false;
143 std::unique_ptr<VADisplayWithCleanup> va_dpy;
144 VAConfigID config_id;
147 std::vector<uint8_t> jpeg_header;
148 VAEncPictureParameterBufferJPEG pic_param;
149 VAQMatrixBufferJPEG q;
150 VAHuffmanTableBufferJPEGBaseline huff;
151 VAEncSliceParameterBufferJPEG parms;
153 std::map<std::pair<unsigned, unsigned>, VAData> va_data_for_resolution;
154 VAData get_va_data_for_resolution(unsigned width, unsigned height, const movit::RGBTriplet &white_balance);
156 std::list<VAResources> va_resources_freelist;
157 std::mutex va_resources_mutex;
158 VAResources get_va_resources(unsigned width, unsigned height);
159 void release_va_resources(VAResources resources);
161 static std::unique_ptr<VADisplayWithCleanup> try_open_va(const std::string &va_display, std::string *error, VAConfigID *config_id);
163 uint8_t *tmp_y, *tmp_cbcr, *tmp_cb, *tmp_cr; // Private to the encoder thread. Used by the libjpeg backend only.
165 std::atomic<int64_t> metric_mjpeg_frames_zero_size_dropped{0};
166 std::atomic<int64_t> metric_mjpeg_frames_interlaced_dropped{0};
167 std::atomic<int64_t> metric_mjpeg_frames_unsupported_pixel_format_dropped{0};
168 std::atomic<int64_t> metric_mjpeg_frames_oversized_dropped{0};
169 std::atomic<int64_t> metric_mjpeg_overrun_dropped{0};
170 std::atomic<int64_t> metric_mjpeg_overrun_submitted{0};
172 friend class PBOFrameAllocator; // FIXME
175 #endif // !defined(_MJPEG_ENCODER_H)