]> git.sesse.net Git - nageru/blob - nageru/mjpeg_encoder.h
Include the raw audio in the MJPEG output.
[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 #define CHECK_VASTATUS(va_status, func)                                 \
31     if (va_status != VA_STATUS_SUCCESS) {                               \
32         fprintf(stderr, "%s:%d (%s) failed with %d\n", __func__, __LINE__, func, va_status); \
33         exit(1);                                                        \
34     }
35
36 class MJPEGEncoder {
37 public:
38         MJPEGEncoder(HTTPD *httpd, const std::string &va_display);
39         ~MJPEGEncoder();
40         void stop();
41         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);
42         bool using_vaapi() const { return va_dpy != nullptr; }
43
44         // Returns -1 for inactive (ie., don't encode frames for this card right now).
45         int get_mjpeg_stream_for_card(unsigned card_index);
46
47 private:
48         static constexpr int quality = 90;
49
50         struct VAResources {
51                 unsigned width, height;
52                 VASurfaceID surface;
53                 VAContextID context;
54                 VABufferID data_buffer;
55                 VAImage image;
56         };
57
58         // RAII wrapper to release VAResources on return (even on error).
59         class ReleaseVAResources {
60         public:
61                 ReleaseVAResources() : committed(true) {}
62
63                 ReleaseVAResources(MJPEGEncoder *mjpeg, const VAResources &resources)
64                         : mjpeg(mjpeg), resources(resources) {}
65
66                 ReleaseVAResources(ReleaseVAResources &) = delete;
67
68                 ReleaseVAResources(ReleaseVAResources &&other)
69                         : mjpeg(other.mjpeg), resources(other.resources), committed(other.committed) {
70                         other.commit();
71                 }
72
73                 ReleaseVAResources &operator= (ReleaseVAResources &) = delete;
74
75                 ReleaseVAResources &operator= (ReleaseVAResources &&other) {
76                         if (!committed) {
77                                 mjpeg->release_va_resources(resources);
78                         }
79                         mjpeg = other.mjpeg;
80                         resources = std::move(other.resources);
81                         committed = other.committed;
82                         other.commit();
83                         return *this;
84                 }
85
86                 ~ReleaseVAResources()
87                 {
88                         if (!committed) {
89                                 mjpeg->release_va_resources(resources);
90                         }
91                 }
92
93                 void commit() { committed = true; }
94
95         private:
96                 MJPEGEncoder *mjpeg = nullptr;
97                 VAResources resources;
98                 bool committed = false;
99         };
100
101         struct QueuedFrame {
102                 int64_t pts;
103                 unsigned card_index;
104                 RefCountedFrame frame;
105                 bmusb::VideoFormat video_format;
106                 size_t y_offset, cbcr_offset;
107                 std::vector<int32_t> audio;
108
109                 // Only for frames in the process of being encoded by VA-API.
110                 VAResources resources;
111                 ReleaseVAResources resource_releaser;
112         };
113
114         void encoder_thread_func();
115         void va_receiver_thread_func();
116         void encode_jpeg_va(QueuedFrame &&qf);
117         std::vector<uint8_t> encode_jpeg_libjpeg(const QueuedFrame &qf);
118         void write_mjpeg_packet(int64_t pts, unsigned card_index, const uint8_t *jpeg, size_t jpeg_size);
119         void write_audio_packet(int64_t pts, unsigned card_index, const std::vector<int32_t> &audio);
120         void init_jpeg_422(unsigned width, unsigned height, VectorDestinationManager *dest, jpeg_compress_struct *cinfo);
121         std::vector<uint8_t> get_jpeg_header(unsigned width, unsigned height, jpeg_compress_struct *cinfo);
122
123         static int write_packet2_thunk(void *opaque, uint8_t *buf, int buf_size, AVIODataMarkerType type, int64_t time);
124         int write_packet2(uint8_t *buf, int buf_size, AVIODataMarkerType type, int64_t time);
125
126         std::thread encoder_thread, va_receiver_thread;
127
128         std::mutex mu;
129         std::queue<QueuedFrame> frames_to_be_encoded;  // Under mu.
130         std::condition_variable any_frames_to_be_encoded;  // Governs changes in both frames_to_be_encoded and frames_under_encoding
131
132         std::queue<QueuedFrame> frames_encoding;  // Under mu. Used for VA-API only.
133         std::condition_variable any_frames_encoding;
134
135         AVFormatContextWithCloser avctx;
136         HTTPD *httpd;
137         std::string mux_header;
138         std::atomic<bool> should_quit{false};
139         bool running = false;
140
141         std::unique_ptr<VADisplayWithCleanup> va_dpy;
142         VAConfigID config_id;
143
144         struct VAData {
145                 std::vector<uint8_t> jpeg_header;
146                 VAEncPictureParameterBufferJPEG pic_param;
147                 VAQMatrixBufferJPEG q;
148                 VAHuffmanTableBufferJPEGBaseline huff;
149                 VAEncSliceParameterBufferJPEG parms;
150         };
151         std::map<std::pair<unsigned, unsigned>, VAData> va_data_for_resolution;
152         VAData get_va_data_for_resolution(unsigned width, unsigned height);
153
154         std::list<VAResources> va_resources_freelist;
155         std::mutex va_resources_mutex;
156         VAResources get_va_resources(unsigned width, unsigned height);
157         void release_va_resources(VAResources resources);
158
159         static std::unique_ptr<VADisplayWithCleanup> try_open_va(const std::string &va_display, std::string *error, VAConfigID *config_id);
160
161         uint8_t *tmp_y, *tmp_cbcr, *tmp_cb, *tmp_cr;  // Private to the encoder thread. Used by the libjpeg backend only.
162
163         std::atomic<int64_t> metric_mjpeg_frames_zero_size_dropped{0};
164         std::atomic<int64_t> metric_mjpeg_frames_interlaced_dropped{0};
165         std::atomic<int64_t> metric_mjpeg_frames_unsupported_pixel_format_dropped{0};
166         std::atomic<int64_t> metric_mjpeg_frames_oversized_dropped{0};
167         std::atomic<int64_t> metric_mjpeg_overrun_dropped{0};
168         std::atomic<int64_t> metric_mjpeg_overrun_submitted{0};
169
170         friend class PBOFrameAllocator;  // FIXME
171 };
172
173 #endif  // !defined(_MJPEG_ENCODER_H)