]> git.sesse.net Git - nageru/blob - nageru/mjpeg_encoder.h
8b68294a48d9e01dfd0254b213b2770d57e0330d
[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 "ref_counted_frame.h"
6
7 extern "C" {
8
9 #include <libavformat/avio.h>
10
11 }  // extern "C"
12
13 #include <atomic>
14 #include <bmusb/bmusb.h>
15 #include <condition_variable>
16 #include <list>
17 #include <mutex>
18 #include <queue>
19 #include <stdint.h>
20 #include <string>
21 #include <thread>
22
23 #include <va/va.h>
24
25 class HTTPD;
26 struct jpeg_compress_struct;
27 struct VADisplayWithCleanup;
28 struct VectorDestinationManager;
29
30 class MJPEGEncoder {
31 public:
32         MJPEGEncoder(HTTPD *httpd, const std::string &va_display);
33         ~MJPEGEncoder();
34         void stop();
35         void upload_frame(int64_t pts, unsigned card_index, RefCountedFrame frame, const bmusb::VideoFormat &video_format, size_t y_offset, size_t cbcr_offset);
36
37 private:
38         static constexpr int quality = 90;
39
40         struct VAResources {
41                 unsigned width, height;
42                 VASurfaceID surface;
43                 VAContextID context;
44                 VABufferID data_buffer;
45         };
46
47         // RAII wrapper to release VAResources on return (even on error).
48         class ReleaseVAResources {
49         public:
50                 ReleaseVAResources() : committed(true) {}
51
52                 ReleaseVAResources(MJPEGEncoder *mjpeg, const VAResources &resources)
53                         : mjpeg(mjpeg), resources(resources) {}
54
55                 ReleaseVAResources(ReleaseVAResources &) = delete;
56
57                 ReleaseVAResources(ReleaseVAResources &&other)
58                         : mjpeg(other.mjpeg), resources(other.resources), committed(other.committed) {
59                         other.commit();
60                 }
61
62                 ReleaseVAResources &operator= (ReleaseVAResources &) = delete;
63
64                 ReleaseVAResources &operator= (ReleaseVAResources &&other) {
65                         if (!committed) {
66                                 mjpeg->release_va_resources(resources);
67                         }
68                         mjpeg = other.mjpeg;
69                         resources = std::move(other.resources);
70                         committed = other.committed;
71                         other.commit();
72                         return *this;
73                 }
74
75                 ~ReleaseVAResources()
76                 {
77                         if (!committed) {
78                                 mjpeg->release_va_resources(resources);
79                         }
80                 }
81
82                 void commit() { committed = true; }
83
84         private:
85                 MJPEGEncoder *mjpeg = nullptr;
86                 VAResources resources;
87                 bool committed = false;
88         };
89
90         struct QueuedFrame {
91                 int64_t pts;
92                 unsigned card_index;
93                 RefCountedFrame frame;
94                 bmusb::VideoFormat video_format;
95                 size_t y_offset, cbcr_offset;
96
97                 // Only for frames in the process of being encoded by VA-API.
98                 VAResources resources;
99                 ReleaseVAResources resource_releaser;
100         };
101
102         void encoder_thread_func();
103         void va_receiver_thread_func();
104         void encode_jpeg_va(QueuedFrame &&qf);
105         std::vector<uint8_t> encode_jpeg_libjpeg(const QueuedFrame &qf);
106         void write_mjpeg_packet(int64_t pts, unsigned card_index, const uint8_t *jpeg, size_t jpeg_size);
107         void init_jpeg_422(unsigned width, unsigned height, VectorDestinationManager *dest, jpeg_compress_struct *cinfo);
108         std::vector<uint8_t> get_jpeg_header(unsigned width, unsigned height, jpeg_compress_struct *cinfo);
109
110         static int write_packet2_thunk(void *opaque, uint8_t *buf, int buf_size, AVIODataMarkerType type, int64_t time);
111         int write_packet2(uint8_t *buf, int buf_size, AVIODataMarkerType type, int64_t time);
112
113         std::thread encoder_thread, va_receiver_thread;
114
115         std::mutex mu;
116         std::queue<QueuedFrame> frames_to_be_encoded;  // Under mu.
117         std::condition_variable any_frames_to_be_encoded;  // Governs changes in both frames_to_be_encoded and frames_under_encoding
118
119         std::queue<QueuedFrame> frames_encoding;  // Under mu. Used for VA-API only.
120         std::condition_variable any_frames_encoding;
121
122         AVFormatContextWithCloser avctx;
123         HTTPD *httpd;
124         std::string mux_header;
125         std::atomic<bool> should_quit{false};
126         bool running = false;
127
128         std::unique_ptr<VADisplayWithCleanup> va_dpy;
129         VAConfigID config_id;
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<std::pair<unsigned, unsigned>, VAData> va_data_for_resolution;
139         VAData get_va_data_for_resolution(unsigned width, unsigned height);
140
141         std::list<VAResources> va_resources_freelist;
142         std::mutex va_resources_mutex;
143         VAResources get_va_resources(unsigned width, unsigned height);
144         void release_va_resources(VAResources resources);
145
146         static std::unique_ptr<VADisplayWithCleanup> try_open_va(const std::string &va_display, std::string *error, VAConfigID *config_id);
147
148         uint8_t *tmp_y, *tmp_cbcr, *tmp_cb, *tmp_cr;  // Private to the encoder thread. Used by the libjpeg backend only.
149
150         std::atomic<int64_t> metric_mjpeg_frames_zero_size_dropped{0};
151         std::atomic<int64_t> metric_mjpeg_frames_interlaced_dropped{0};
152         std::atomic<int64_t> metric_mjpeg_frames_unsupported_pixel_format_dropped{0};
153         std::atomic<int64_t> metric_mjpeg_frames_oversized_dropped{0};
154         std::atomic<int64_t> metric_mjpeg_overrun_dropped{0};
155         std::atomic<int64_t> metric_mjpeg_overrun_submitted{0};
156 };
157
158 #endif  // !defined(_MJPEG_ENCODER_H)