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);
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;
42 RefCountedFrame frame;
43 bmusb::VideoFormat video_format;
44 size_t y_offset, cbcr_offset;
47 void encoder_thread_func();
48 std::vector<uint8_t> encode_jpeg(const QueuedFrame &qf);
49 std::vector<uint8_t> encode_jpeg_va(const QueuedFrame &qf);
50 std::vector<uint8_t> encode_jpeg_libjpeg(const QueuedFrame &qf);
51 void init_jpeg_422(unsigned width, unsigned height, VectorDestinationManager *dest, jpeg_compress_struct *cinfo);
52 std::vector<uint8_t> get_jpeg_header(unsigned width, unsigned height, jpeg_compress_struct *cinfo);
54 static int write_packet2_thunk(void *opaque, uint8_t *buf, int buf_size, AVIODataMarkerType type, int64_t time);
55 int write_packet2(uint8_t *buf, int buf_size, AVIODataMarkerType type, int64_t time);
57 std::thread encoder_thread;
60 std::queue<QueuedFrame> frames_to_be_encoded; // Under mu.
61 std::condition_variable any_frames_to_be_encoded;
63 std::queue<QueuedFrame> frames_encoding; // Under mu.
64 std::condition_variable any_frames_encoding;
66 AVFormatContextWithCloser avctx;
68 std::string mux_header;
69 std::atomic<bool> should_quit{false};
72 std::unique_ptr<VADisplayWithCleanup> va_dpy;
76 std::vector<uint8_t> jpeg_header;
77 VAEncPictureParameterBufferJPEG pic_param;
78 VAQMatrixBufferJPEG q;
79 VAHuffmanTableBufferJPEGBaseline huff;
80 VAEncSliceParameterBufferJPEG parms;
82 std::map<std::pair<unsigned, unsigned>, VAData> va_data_for_resolution;
83 VAData get_va_data_for_resolution(unsigned width, unsigned height);
86 unsigned width, height;
89 VABufferID data_buffer;
91 std::list<VAResources> va_resources_freelist;
92 std::mutex va_resources_mutex;
93 VAResources get_va_resources(unsigned width, unsigned height);
94 void release_va_resources(VAResources resources);
96 // RAII wrapper to release VAResources on return (even on error).
97 class ReleaseVAResources {
99 ReleaseVAResources(MJPEGEncoder *mjpeg, const VAResources &resources)
100 : mjpeg(mjpeg), resources(resources) {}
101 ~ReleaseVAResources()
104 mjpeg->release_va_resources(resources);
108 void commit() { committed = true; }
111 MJPEGEncoder * const mjpeg;
112 const VAResources &resources;
113 bool committed = false;
116 static std::unique_ptr<VADisplayWithCleanup> try_open_va(const std::string &va_display, std::string *error, VAConfigID *config_id);
118 uint8_t *tmp_y, *tmp_cbcr, *tmp_cb, *tmp_cr; // Private to the encoder thread.
121 #endif // !defined(_MJPEG_ENCODER_H)