1 #ifndef _MJPEG_ENCODER_H
2 #define _MJPEG_ENCODER_H 1
4 #include "shared/ffmpeg_raii.h"
5 #include "shared/httpd.h"
6 #include "shared/va_resource_pool.h"
7 #include "ref_counted_frame.h"
11 #include <libavformat/avio.h>
16 #include <bmusb/bmusb.h>
17 #include <condition_variable>
28 #include <movit/effect.h>
30 #include <va/va_enc_jpeg.h>
32 struct jpeg_compress_struct;
33 struct VADisplayWithCleanup;
34 struct VectorDestinationManager;
36 #define CHECK_VASTATUS(va_status, func) \
37 if (va_status != VA_STATUS_SUCCESS) { \
38 fprintf(stderr, "%s:%d (%s) failed: %s\n", __func__, __LINE__, func, vaErrorStr(va_status)); \
44 MJPEGEncoder(HTTPD *httpd, const std::string &va_display);
47 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);
48 bool using_vaapi() const { return va_dpy != nullptr; }
50 bool should_encode_mjpeg_for_card(unsigned card_index);
51 VAResourcePool *get_va_pool() const { return va_pool.get(); }
54 static constexpr int quality = 90;
59 RefCountedFrame frame;
60 bmusb::VideoFormat video_format;
61 size_t y_offset, cbcr_offset;
62 std::vector<int32_t> audio;
63 movit::RGBTriplet white_balance;
65 // Only for frames in the process of being encoded by VA-API.
66 VAResourcePool::VAResources resources;
67 ReleaseVAResources resource_releaser;
70 void encoder_thread_func();
71 void va_receiver_thread_func();
72 void encode_jpeg_va(QueuedFrame &&qf);
73 std::vector<uint8_t> encode_jpeg_libjpeg(const QueuedFrame &qf);
74 void write_mjpeg_packet(AVFormatContext *avctx, int64_t pts, unsigned stream_index, const uint8_t *jpeg, size_t jpeg_size);
75 void write_audio_packet(AVFormatContext *avctx, int64_t pts, unsigned stream_index, const std::vector<int32_t> &audio);
76 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);
77 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);
78 void add_stream(HTTPD::StreamID stream_id); // Can only be called from the constructor, or the thread owning <streams>.
79 void update_siphon_streams(); // Same.
80 void create_ffmpeg_context(HTTPD::StreamID stream_id);
82 struct WritePacket2Context {
83 MJPEGEncoder *mjpeg_encoder;
84 HTTPD::StreamID stream_id;
86 std::map<HTTPD::StreamID, WritePacket2Context> ffmpeg_contexts; // Statically set up, so we never need to worry about dangling pointers.
87 static int write_packet2_thunk(void *opaque, uint8_t *buf, int buf_size, AVIODataMarkerType type, int64_t time);
88 int write_packet2(HTTPD::StreamID stream_id, uint8_t *buf, int buf_size, AVIODataMarkerType type, int64_t time);
90 std::thread encoder_thread, va_receiver_thread;
93 std::queue<QueuedFrame> frames_to_be_encoded; // Under mu.
94 std::condition_variable any_frames_to_be_encoded; // Governs changes in both frames_to_be_encoded and frames_under_encoding
96 std::queue<QueuedFrame> frames_encoding; // Under mu. Used for VA-API only.
97 std::condition_variable any_frames_encoding;
100 AVFormatContextWithCloser avctx;
101 std::string mux_header;
103 std::map<HTTPD::StreamID, Stream> streams; // Owned by the VA-API receiver thread if VA-API is active, or the encoder thread if not.
105 std::atomic<bool> should_quit{false};
106 bool running = false;
108 std::unique_ptr<VADisplayWithCleanup> va_dpy;
109 std::unique_ptr<VAResourcePool> va_pool;
112 unsigned width, height, y_h_samp_factor, y_v_samp_factor;
113 movit::RGBTriplet white_balance;
115 bool operator< (const VAKey &other) const {
116 if (width != other.width)
117 return width < other.width;
118 if (height != other.height)
119 return height < other.height;
120 if (y_h_samp_factor != other.y_h_samp_factor)
121 return y_h_samp_factor < other.y_h_samp_factor;
122 if (y_v_samp_factor != other.y_v_samp_factor)
123 return y_v_samp_factor < other.y_v_samp_factor;
124 if (white_balance.r != other.white_balance.r)
125 return white_balance.r < other.white_balance.r;
126 if (white_balance.g != other.white_balance.g)
127 return white_balance.g < other.white_balance.g;
128 return white_balance.b < other.white_balance.b;
132 std::vector<uint8_t> jpeg_header;
133 VAEncPictureParameterBufferJPEG pic_param;
134 VAQMatrixBufferJPEG q;
135 VAHuffmanTableBufferJPEGBaseline huff;
136 VAEncSliceParameterBufferJPEG parms;
138 std::map<VAKey, VAData> va_data_for_parameters;
139 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);
141 uint8_t *tmp_y, *tmp_cbcr, *tmp_cb, *tmp_cr; // Private to the encoder thread. Used by the libjpeg backend only.
143 std::atomic<int64_t> metric_mjpeg_frames_zero_size_dropped{0};
144 std::atomic<int64_t> metric_mjpeg_frames_interlaced_dropped{0};
145 std::atomic<int64_t> metric_mjpeg_frames_unsupported_pixel_format_dropped{0};
146 std::atomic<int64_t> metric_mjpeg_frames_oversized_dropped{0};
147 std::atomic<int64_t> metric_mjpeg_overrun_dropped{0};
148 std::atomic<int64_t> metric_mjpeg_overrun_submitted{0};
150 friend class PBOFrameAllocator; // FIXME
153 #endif // !defined(_MJPEG_ENCODER_H)