]> git.sesse.net Git - nageru/blob - nageru/mjpeg_encoder.h
Move VAResourcePool into a shared class between MJPEGEncoder in Nageru and the VA...
[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 "shared/va_resource_pool.h"
8 #include "ref_counted_frame.h"
9
10 extern "C" {
11
12 #include <libavformat/avio.h>
13
14 }  // extern "C"
15
16 #include <atomic>
17 #include <bmusb/bmusb.h>
18 #include <condition_variable>
19 #include <list>
20 #include <mutex>
21 #include <queue>
22 #include <stdint.h>
23 #include <string>
24 #include <thread>
25
26 #include <movit/effect.h>
27 #include <va/va.h>
28
29 struct jpeg_compress_struct;
30 struct VADisplayWithCleanup;
31 struct VectorDestinationManager;
32
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)); \
36         exit(1);                                                        \
37     }
38
39 class MJPEGEncoder {
40 public:
41         MJPEGEncoder(HTTPD *httpd, const std::string &va_display);
42         ~MJPEGEncoder();
43         void stop();
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; }
46
47         bool should_encode_mjpeg_for_card(unsigned card_index);
48         VAResourcePool *get_va_pool() const { return va_pool.get(); }
49
50 private:
51         static constexpr int quality = 90;
52
53         struct QueuedFrame {
54                 int64_t pts;
55                 unsigned card_index;
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;
61
62                 // Only for frames in the process of being encoded by VA-API.
63                 VAResourcePool::VAResources resources;
64                 ReleaseVAResources resource_releaser;
65         };
66
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);
78
79         struct WritePacket2Context {
80                 MJPEGEncoder *mjpeg_encoder;
81                 HTTPD::StreamID stream_id;
82         };
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);
86
87         std::thread encoder_thread, va_receiver_thread;
88
89         std::mutex mu;
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
92
93         std::queue<QueuedFrame> frames_encoding;  // Under mu. Used for VA-API only.
94         std::condition_variable any_frames_encoding;
95
96         struct Stream {
97                 AVFormatContextWithCloser avctx;
98                 std::string mux_header;
99         };
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.
101         HTTPD *httpd;
102         std::atomic<bool> should_quit{false};
103         bool running = false;
104
105         std::unique_ptr<VADisplayWithCleanup> va_dpy;
106         std::unique_ptr<VAResourcePool> va_pool;
107
108         struct VAKey {
109                 unsigned width, height, y_h_samp_factor, y_v_samp_factor;
110                 movit::RGBTriplet white_balance;
111
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;
126                 }
127         };
128         struct VAData {
129                 std::vector<uint8_t> jpeg_header;
130                 VAEncPictureParameterBufferJPEG pic_param;
131                 VAQMatrixBufferJPEG q;
132                 VAHuffmanTableBufferJPEGBaseline huff;
133                 VAEncSliceParameterBufferJPEG parms;
134         };
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);
137
138         uint8_t *tmp_y, *tmp_cbcr, *tmp_cb, *tmp_cr;  // Private to the encoder thread. Used by the libjpeg backend only.
139
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};
146
147         friend class PBOFrameAllocator;  // FIXME
148 };
149
150 #endif  // !defined(_MJPEG_ENCODER_H)