]> git.sesse.net Git - nageru/blob - mjpeg_encoder.h
Add functionality for MJPEG export.
[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 QueuedFrame {
40                 int64_t pts;
41                 unsigned card_index;
42                 RefCountedFrame frame;
43                 bmusb::VideoFormat video_format;
44                 size_t y_offset, cbcr_offset;
45         };
46
47         void encoder_thread_func();
48         std::vector<uint8_t> encode_jpeg(const QueuedFrame &qf);
49         std::vector<uint8_t> encode_jpeg_va(const QueuedFrame &qf);
50         std::vector<uint8_t> encode_jpeg_libjpeg(const QueuedFrame &qf);
51         void init_jpeg_422(unsigned width, unsigned height, VectorDestinationManager *dest, jpeg_compress_struct *cinfo);
52         std::vector<uint8_t> get_jpeg_header(unsigned width, unsigned height, jpeg_compress_struct *cinfo);
53
54         static int write_packet2_thunk(void *opaque, uint8_t *buf, int buf_size, AVIODataMarkerType type, int64_t time);
55         int write_packet2(uint8_t *buf, int buf_size, AVIODataMarkerType type, int64_t time);
56
57         std::thread encoder_thread;
58
59         std::mutex mu;
60         std::queue<QueuedFrame> frames_to_be_encoded;  // Under mu.
61         std::condition_variable any_frames_to_be_encoded;
62
63         std::queue<QueuedFrame> frames_encoding;  // Under mu.
64         std::condition_variable any_frames_encoding;
65
66         AVFormatContextWithCloser avctx;
67         HTTPD *httpd;
68         std::string mux_header;
69         std::atomic<bool> should_quit{false};
70         bool running = false;
71
72         std::unique_ptr<VADisplayWithCleanup> va_dpy;
73         VAConfigID config_id;
74
75         struct VAData {
76                 std::vector<uint8_t> jpeg_header;
77                 VAEncPictureParameterBufferJPEG pic_param;
78                 VAQMatrixBufferJPEG q;
79                 VAHuffmanTableBufferJPEGBaseline huff;
80                 VAEncSliceParameterBufferJPEG parms;
81         };
82         std::map<std::pair<unsigned, unsigned>, VAData> va_data_for_resolution;
83         VAData get_va_data_for_resolution(unsigned width, unsigned height);
84
85         struct VAResources {
86                 unsigned width, height;
87                 VASurfaceID surface;
88                 VAContextID context;
89                 VABufferID data_buffer;
90         };
91         std::list<VAResources> va_resources_freelist;
92         std::mutex va_resources_mutex;
93         VAResources get_va_resources(unsigned width, unsigned height);
94         void release_va_resources(VAResources resources);
95
96         // RAII wrapper to release VAResources on return (even on error).
97         class ReleaseVAResources {
98         public:
99                 ReleaseVAResources(MJPEGEncoder *mjpeg, const VAResources &resources)
100                         : mjpeg(mjpeg), resources(resources) {}
101                 ~ReleaseVAResources()
102                 {
103                         if (!committed) {
104                                 mjpeg->release_va_resources(resources);
105                         }
106                 }
107
108                 void commit() { committed = true; }
109
110         private:
111                 MJPEGEncoder * const mjpeg;
112                 const VAResources &resources;
113                 bool committed = false;
114         };
115
116         static std::unique_ptr<VADisplayWithCleanup> try_open_va(const std::string &va_display, std::string *error, VAConfigID *config_id);
117
118         uint8_t *tmp_y, *tmp_cbcr, *tmp_cb, *tmp_cr;  // Private to the encoder thread.
119 };
120
121 #endif  // !defined(_MJPEG_ENCODER_H)