]> git.sesse.net Git - nageru/blob - nageru/mjpeg_encoder.h
Unify VA-API initialization.
[nageru] / nageru / mjpeg_encoder.h
1 #ifndef _MJPEG_ENCODER_H
2 #define _MJPEG_ENCODER_H 1
3
4 #include "defs.h"
5 #include "shared/ffmpeg_raii.h"
6 #include "shared/httpd.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 <list>
19 #include <mutex>
20 #include <queue>
21 #include <stdint.h>
22 #include <string>
23 #include <thread>
24
25 #include <movit/effect.h>
26 #include <va/va.h>
27
28 struct jpeg_compress_struct;
29 struct VADisplayWithCleanup;
30 struct VectorDestinationManager;
31
32 #define CHECK_VASTATUS(va_status, func)                                 \
33     if (va_status != VA_STATUS_SUCCESS) {                               \
34         fprintf(stderr, "%s:%d (%s) failed: %s\n", __func__, __LINE__, func, vaErrorStr(va_status)); \
35         exit(1);                                                        \
36     }
37
38 class MJPEGEncoder {
39 public:
40         MJPEGEncoder(HTTPD *httpd, const std::string &va_display);
41         ~MJPEGEncoder();
42         void stop();
43         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);
44         bool using_vaapi() const { return va_dpy != nullptr; }
45
46         bool should_encode_mjpeg_for_card(unsigned card_index);
47
48 private:
49         static constexpr int quality = 90;
50
51         struct VAResources {
52                 unsigned width, height;
53                 uint32_t fourcc;
54                 VASurfaceID surface;
55                 VAContextID context;
56                 VABufferID data_buffer;
57                 VAImage image;
58         };
59
60         // RAII wrapper to release VAResources on return (even on error).
61         class ReleaseVAResources {
62         public:
63                 ReleaseVAResources() : committed(true) {}
64
65                 ReleaseVAResources(MJPEGEncoder *mjpeg, const VAResources &resources)
66                         : mjpeg(mjpeg), resources(resources) {}
67
68                 ReleaseVAResources(ReleaseVAResources &) = delete;
69
70                 ReleaseVAResources(ReleaseVAResources &&other)
71                         : mjpeg(other.mjpeg), resources(other.resources), committed(other.committed) {
72                         other.commit();
73                 }
74
75                 ReleaseVAResources &operator= (ReleaseVAResources &) = delete;
76
77                 ReleaseVAResources &operator= (ReleaseVAResources &&other) {
78                         if (!committed) {
79                                 mjpeg->release_va_resources(resources);
80                         }
81                         mjpeg = other.mjpeg;
82                         resources = std::move(other.resources);
83                         committed = other.committed;
84                         other.commit();
85                         return *this;
86                 }
87
88                 ~ReleaseVAResources()
89                 {
90                         if (!committed) {
91                                 mjpeg->release_va_resources(resources);
92                         }
93                 }
94
95                 void commit() { committed = true; }
96
97         private:
98                 MJPEGEncoder *mjpeg = nullptr;
99                 VAResources resources;
100                 bool committed = false;
101         };
102
103         struct QueuedFrame {
104                 int64_t pts;
105                 unsigned card_index;
106                 RefCountedFrame frame;
107                 bmusb::VideoFormat video_format;
108                 size_t y_offset, cbcr_offset;
109                 std::vector<int32_t> audio;
110                 movit::RGBTriplet white_balance;
111
112                 // Only for frames in the process of being encoded by VA-API.
113                 VAResources resources;
114                 ReleaseVAResources resource_releaser;
115         };
116
117         void encoder_thread_func();
118         void va_receiver_thread_func();
119         void encode_jpeg_va(QueuedFrame &&qf);
120         std::vector<uint8_t> encode_jpeg_libjpeg(const QueuedFrame &qf);
121         void write_mjpeg_packet(AVFormatContext *avctx, int64_t pts, unsigned stream_index, const uint8_t *jpeg, size_t jpeg_size);
122         void write_audio_packet(AVFormatContext *avctx, int64_t pts, unsigned stream_index, const std::vector<int32_t> &audio);
123         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);
124         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);
125         void add_stream(HTTPD::StreamID stream_id);  // Can only be called from the constructor, or the thread owning <streams>.
126         void update_siphon_streams();  // Same.
127         void create_ffmpeg_context(HTTPD::StreamID stream_id);
128
129         struct WritePacket2Context {
130                 MJPEGEncoder *mjpeg_encoder;
131                 HTTPD::StreamID stream_id;
132         };
133         std::map<HTTPD::StreamID, WritePacket2Context> ffmpeg_contexts;   // Statically set up, so we never need to worry about dangling pointers.
134         static int write_packet2_thunk(void *opaque, uint8_t *buf, int buf_size, AVIODataMarkerType type, int64_t time);
135         int write_packet2(HTTPD::StreamID stream_id, uint8_t *buf, int buf_size, AVIODataMarkerType type, int64_t time);
136
137         std::thread encoder_thread, va_receiver_thread;
138
139         std::mutex mu;
140         std::queue<QueuedFrame> frames_to_be_encoded;  // Under mu.
141         std::condition_variable any_frames_to_be_encoded;  // Governs changes in both frames_to_be_encoded and frames_under_encoding
142
143         std::queue<QueuedFrame> frames_encoding;  // Under mu. Used for VA-API only.
144         std::condition_variable any_frames_encoding;
145
146         struct Stream {
147                 AVFormatContextWithCloser avctx;
148                 std::string mux_header;
149         };
150         std::map<HTTPD::StreamID, Stream> streams;  // Owned by the VA-API receiver thread if VA-API is active, or the encoder thread if not.
151         HTTPD *httpd;
152         std::atomic<bool> should_quit{false};
153         bool running = false;
154
155         std::unique_ptr<VADisplayWithCleanup> va_dpy;
156         VAConfigID config_id_422, config_id_420;
157
158         struct VAKey {
159                 unsigned width, height, y_h_samp_factor, y_v_samp_factor;
160                 movit::RGBTriplet white_balance;
161
162                 bool operator< (const VAKey &other) const {
163                         if (width != other.width)
164                                 return width < other.width;
165                         if (height != other.height)
166                                 return height < other.height;
167                         if (y_h_samp_factor != other.y_h_samp_factor)
168                                 return y_h_samp_factor < other.y_h_samp_factor;
169                         if (y_v_samp_factor != other.y_v_samp_factor)
170                                 return y_v_samp_factor < other.y_v_samp_factor;
171                         if (white_balance.r != other.white_balance.r)
172                                 return white_balance.r < other.white_balance.r;
173                         if (white_balance.g != other.white_balance.g)
174                                 return white_balance.g < other.white_balance.g;
175                         return white_balance.b < other.white_balance.b;
176                 }
177         };
178         struct VAData {
179                 std::vector<uint8_t> jpeg_header;
180                 VAEncPictureParameterBufferJPEG pic_param;
181                 VAQMatrixBufferJPEG q;
182                 VAHuffmanTableBufferJPEGBaseline huff;
183                 VAEncSliceParameterBufferJPEG parms;
184         };
185         std::map<VAKey, VAData> va_data_for_parameters;
186         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);
187
188         std::list<VAResources> va_resources_freelist;
189         std::mutex va_resources_mutex;
190         VAResources get_va_resources(unsigned width, unsigned height, uint32_t fourcc);
191         void release_va_resources(VAResources resources);
192
193         uint8_t *tmp_y, *tmp_cbcr, *tmp_cb, *tmp_cr;  // Private to the encoder thread. Used by the libjpeg backend only.
194
195         std::atomic<int64_t> metric_mjpeg_frames_zero_size_dropped{0};
196         std::atomic<int64_t> metric_mjpeg_frames_interlaced_dropped{0};
197         std::atomic<int64_t> metric_mjpeg_frames_unsupported_pixel_format_dropped{0};
198         std::atomic<int64_t> metric_mjpeg_frames_oversized_dropped{0};
199         std::atomic<int64_t> metric_mjpeg_overrun_dropped{0};
200         std::atomic<int64_t> metric_mjpeg_overrun_submitted{0};
201
202         friend class PBOFrameAllocator;  // FIXME
203 };
204
205 #endif  // !defined(_MJPEG_ENCODER_H)