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