]> git.sesse.net Git - nageru/blob - nageru/mjpeg_encoder.h
Set CEF autoplay policy to be more lenient.
[nageru] / nageru / mjpeg_encoder.h
1 #ifndef _MJPEG_ENCODER_H
2 #define _MJPEG_ENCODER_H 1
3
4 #include "shared/ffmpeg_raii.h"
5 #include "shared/httpd.h"
6 #include "shared/va_resource_pool.h"
7 #include "ref_counted_frame.h"
8
9 extern "C" {
10
11 #include <libavformat/avio.h>
12
13 }  // extern "C"
14
15 #include <atomic>
16 #include <bmusb/bmusb.h>
17 #include <condition_variable>
18 #include <map>
19 #include <memory>
20 #include <mutex>
21 #include <queue>
22 #include <stddef.h>
23 #include <stdint.h>
24 #include <string>
25 #include <thread>
26 #include <vector>
27
28 #include <movit/effect.h>
29 #include <va/va.h>
30 #include <va/va_enc_jpeg.h>
31
32 struct jpeg_compress_struct;
33 struct VADisplayWithCleanup;
34 struct VectorDestinationManager;
35
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)); \
39         exit(1);                                                        \
40     }
41
42 class MJPEGEncoder {
43 public:
44         MJPEGEncoder(HTTPD *httpd, const std::string &va_display);
45         ~MJPEGEncoder();
46         void stop();
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; }
49
50         bool should_encode_mjpeg_for_card(unsigned card_index);
51         VAResourcePool *get_va_pool() const { return va_pool.get(); }
52
53 private:
54         static constexpr int quality = 90;
55
56         struct QueuedFrame {
57                 int64_t pts;
58                 unsigned card_index;
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;
64
65                 // Only for frames in the process of being encoded by VA-API.
66                 VAResourcePool::VAResources resources;
67                 ReleaseVAResources resource_releaser;
68         };
69
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);
81
82         struct WritePacket2Context {
83                 MJPEGEncoder *mjpeg_encoder;
84                 HTTPD::StreamID stream_id;
85         };
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);
89
90         std::thread encoder_thread, va_receiver_thread;
91
92         std::mutex mu;
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
95
96         std::queue<QueuedFrame> frames_encoding;  // Under mu. Used for VA-API only.
97         std::condition_variable any_frames_encoding;
98
99         struct Stream {
100                 AVFormatContextWithCloser avctx;
101                 std::string mux_header;
102         };
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.
104         HTTPD *httpd;
105         std::atomic<bool> should_quit{false};
106         bool running = false;
107
108         std::unique_ptr<VADisplayWithCleanup> va_dpy;
109         std::unique_ptr<VAResourcePool> va_pool;
110
111         struct VAKey {
112                 unsigned width, height, y_h_samp_factor, y_v_samp_factor;
113                 movit::RGBTriplet white_balance;
114
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;
129                 }
130         };
131         struct VAData {
132                 std::vector<uint8_t> jpeg_header;
133                 VAEncPictureParameterBufferJPEG pic_param;
134                 VAQMatrixBufferJPEG q;
135                 VAHuffmanTableBufferJPEGBaseline huff;
136                 VAEncSliceParameterBufferJPEG parms;
137         };
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);
140
141         uint8_t *tmp_y, *tmp_cbcr, *tmp_cb, *tmp_cr;  // Private to the encoder thread. Used by the libjpeg backend only.
142
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};
149
150         friend class PBOFrameAllocator;  // FIXME
151 };
152
153 #endif  // !defined(_MJPEG_ENCODER_H)