]> git.sesse.net Git - nageru/blob - mjpeg_encoder.h
Add an option to control the mapping of streams to export to MJPEG (or turn it off...
[nageru] / mjpeg_encoder.h
1 #ifndef _MJPEG_ENCODER_H
2 #define _MJPEG_ENCODER_H 1
3
4 #include "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         void stop();
34         void upload_frame(int64_t pts, unsigned card_index, RefCountedFrame frame, const bmusb::VideoFormat &video_format, size_t y_offset, size_t cbcr_offset);
35
36 private:
37         static constexpr int quality = 90;
38
39         struct VAResources {
40                 unsigned width, height;
41                 VASurfaceID surface;
42                 VAContextID context;
43                 VABufferID data_buffer;
44         };
45
46         // RAII wrapper to release VAResources on return (even on error).
47         class ReleaseVAResources {
48         public:
49                 ReleaseVAResources() : committed(true) {}
50
51                 ReleaseVAResources(MJPEGEncoder *mjpeg, const VAResources &resources)
52                         : mjpeg(mjpeg), resources(resources) {}
53
54                 ReleaseVAResources(ReleaseVAResources &) = delete;
55
56                 ReleaseVAResources(ReleaseVAResources &&other)
57                         : mjpeg(other.mjpeg), resources(other.resources), committed(other.committed) {
58                         other.commit();
59                 }
60
61                 ReleaseVAResources &operator= (ReleaseVAResources &) = delete;
62
63                 ReleaseVAResources &operator= (ReleaseVAResources &&other) {
64                         if (!committed) {
65                                 mjpeg->release_va_resources(resources);
66                         }
67                         mjpeg = other.mjpeg;
68                         resources = std::move(other.resources);
69                         committed = other.committed;
70                         other.commit();
71                         return *this;
72                 }
73
74                 ~ReleaseVAResources()
75                 {
76                         if (!committed) {
77                                 mjpeg->release_va_resources(resources);
78                         }
79                 }
80
81                 void commit() { committed = true; }
82
83         private:
84                 MJPEGEncoder *mjpeg = nullptr;
85                 VAResources resources;
86                 bool committed = false;
87         };
88
89         struct QueuedFrame {
90                 int64_t pts;
91                 unsigned card_index;
92                 RefCountedFrame frame;
93                 bmusb::VideoFormat video_format;
94                 size_t y_offset, cbcr_offset;
95
96                 // Only for frames in the process of being encoded by VA-API.
97                 VAResources resources;
98                 ReleaseVAResources resource_releaser;
99         };
100
101         void encoder_thread_func();
102         void va_receiver_thread_func();
103         void encode_jpeg_va(QueuedFrame &&qf);
104         std::vector<uint8_t> encode_jpeg_libjpeg(const QueuedFrame &qf);
105         void write_mjpeg_packet(int64_t pts, unsigned card_index, const std::vector<uint8_t> &jpeg);
106         void init_jpeg_422(unsigned width, unsigned height, VectorDestinationManager *dest, jpeg_compress_struct *cinfo);
107         std::vector<uint8_t> get_jpeg_header(unsigned width, unsigned height, jpeg_compress_struct *cinfo);
108
109         static int write_packet2_thunk(void *opaque, uint8_t *buf, int buf_size, AVIODataMarkerType type, int64_t time);
110         int write_packet2(uint8_t *buf, int buf_size, AVIODataMarkerType type, int64_t time);
111
112         std::thread encoder_thread, va_receiver_thread;
113
114         std::mutex mu;
115         std::queue<QueuedFrame> frames_to_be_encoded;  // Under mu.
116         std::condition_variable any_frames_to_be_encoded;  // Governs changes in both frames_to_be_encoded and frames_under_encoding
117
118         std::queue<QueuedFrame> frames_encoding;  // Under mu. Used for VA-API only.
119         std::condition_variable any_frames_encoding;
120
121         AVFormatContextWithCloser avctx;
122         HTTPD *httpd;
123         std::string mux_header;
124         std::atomic<bool> should_quit{false};
125         bool running = false;
126
127         std::unique_ptr<VADisplayWithCleanup> va_dpy;
128         VAConfigID config_id;
129
130         struct VAData {
131                 std::vector<uint8_t> jpeg_header;
132                 VAEncPictureParameterBufferJPEG pic_param;
133                 VAQMatrixBufferJPEG q;
134                 VAHuffmanTableBufferJPEGBaseline huff;
135                 VAEncSliceParameterBufferJPEG parms;
136         };
137         std::map<std::pair<unsigned, unsigned>, VAData> va_data_for_resolution;
138         VAData get_va_data_for_resolution(unsigned width, unsigned height);
139
140         std::list<VAResources> va_resources_freelist;
141         std::mutex va_resources_mutex;
142         VAResources get_va_resources(unsigned width, unsigned height);
143         void release_va_resources(VAResources resources);
144
145         static std::unique_ptr<VADisplayWithCleanup> try_open_va(const std::string &va_display, std::string *error, VAConfigID *config_id);
146
147         uint8_t *tmp_y, *tmp_cbcr, *tmp_cb, *tmp_cr;  // Private to the encoder thread. Used by the libjpeg backend only.
148 };
149
150 #endif  // !defined(_MJPEG_ENCODER_H)