]> git.sesse.net Git - bmusb/blob - bmusb.h
Add a way to get the current video mode.
[bmusb] / bmusb.h
1 #ifndef _BMUSB_H
2 #define _BMUSB_H
3
4 #include <stdint.h>
5 #include <atomic>
6 #include <condition_variable>
7 #include <deque>
8 #include <functional>
9 #include <map>
10 #include <mutex>
11 #include <stack>
12 #include <string>
13 #include <thread>
14 #include <vector>
15
16 struct libusb_transfer;
17
18 // An interface for frame allocators; if you do not specify one
19 // (using set_video_frame_allocator), a default one that pre-allocates
20 // a freelist of eight frames using new[] will be used. Specifying
21 // your own can be useful if you have special demands for where you want the
22 // frame to end up and don't want to spend the extra copy to get it there, for
23 // instance GPU memory.
24 class FrameAllocator {
25  public:
26         struct Frame {
27                 uint8_t *data = nullptr;
28                 uint8_t *data2 = nullptr;  // Only if interleaved == true.
29                 size_t len = 0;  // Number of bytes we actually have.
30                 size_t size = 0;  // Number of bytes we have room for.
31                 size_t overflow = 0;
32                 void *userdata = nullptr;
33                 FrameAllocator *owner = nullptr;
34
35                 // If set to true, every other byte will go to data and to data2.
36                 // If so, <len> and <size> are still about the number of total bytes
37                 // so if size == 1024, there's 512 bytes in data and 512 in data2.
38                 bool interleaved = false;
39         };
40
41         virtual ~FrameAllocator();
42
43         // Request a video frame. Note that this is called from the
44         // USB thread, which runs with realtime priority and is
45         // very sensitive to delays. Thus, you should not do anything
46         // here that might sleep, including calling malloc().
47         // (Taking a mutex is borderline.)
48         //
49         // The Frame object will be given to the frame callback,
50         // which is responsible for releasing the video frame back
51         // once it is usable for new frames (ie., it will no longer
52         // be read from). You can use the "userdata" pointer for
53         // whatever you want to identify this frame if you need to.
54         //
55         // Returning a Frame with data==nullptr is allowed;
56         // if so, the frame in progress will be dropped.
57         virtual Frame alloc_frame() = 0;
58
59         virtual void release_frame(Frame frame) = 0;
60 };
61
62 // Audio is more important than video, and also much cheaper.
63 // By having many more audio frames available, hopefully if something
64 // starts to drop, we'll have CPU load go down (from not having to
65 // process as much video) before we have to drop audio.
66 #define NUM_QUEUED_VIDEO_FRAMES 16
67 #define NUM_QUEUED_AUDIO_FRAMES 64
68
69 class MallocFrameAllocator : public FrameAllocator {
70 public:
71         MallocFrameAllocator(size_t frame_size, size_t num_queued_frames);
72         Frame alloc_frame() override;
73         void release_frame(Frame frame) override;
74
75 private:
76         size_t frame_size;
77
78         std::mutex freelist_mutex;
79         std::stack<std::unique_ptr<uint8_t[]>> freelist;  // All of size <frame_size>.
80 };
81
82 // Represents an input mode you can tune a card to.
83 struct VideoMode {
84         std::string name;
85         bool autodetect = false;  // If true, all the remaining fields are irrelevant.
86         unsigned width = 0, height = 0;
87         unsigned frame_rate_num = 0, frame_rate_den = 0;
88         bool interlaced = false;
89 };
90
91 // Represents the format of an actual frame coming in.
92 struct VideoFormat {
93         uint16_t id = 0;  // For debugging/logging only.
94         unsigned width = 0, height = 0, second_field_start = 0;
95         unsigned extra_lines_top = 0, extra_lines_bottom = 0;
96         unsigned frame_rate_nom = 0, frame_rate_den = 0;
97         bool interlaced = false;
98         bool has_signal = false;
99 };
100
101 struct AudioFormat {
102         uint16_t id = 0;  // For debugging/logging only.
103         unsigned bits_per_sample = 0;
104         unsigned num_channels = 0;
105 };
106
107 typedef std::function<void(uint16_t timecode,
108                            FrameAllocator::Frame video_frame, size_t video_offset, VideoFormat video_format,
109                            FrameAllocator::Frame audio_frame, size_t audio_offset, AudioFormat audio_format)>
110         frame_callback_t;
111
112 class CaptureInterface {
113  public:
114         virtual ~CaptureInterface() {}
115
116         virtual std::map<uint32_t, VideoMode> get_available_video_modes() const = 0;
117         virtual uint32_t get_current_video_mode() const = 0;
118         virtual void set_video_mode(uint32_t video_mode_id) = 0;
119
120         // Does not take ownership.
121         virtual void set_video_frame_allocator(FrameAllocator *allocator) = 0;
122
123         virtual FrameAllocator *get_video_frame_allocator() = 0;
124
125         // Does not take ownership.
126         virtual void set_audio_frame_allocator(FrameAllocator *allocator) = 0;
127
128         virtual FrameAllocator *get_audio_frame_allocator() = 0;
129
130         virtual void set_frame_callback(frame_callback_t callback) = 0;
131
132         // Needs to be run before configure_card().
133         virtual void set_dequeue_thread_callbacks(std::function<void()> init, std::function<void()> cleanup) = 0;
134
135         // Only valid after configure_card().
136         virtual std::string get_description() const = 0;
137
138         virtual void configure_card() = 0;
139
140         virtual void start_bm_capture() = 0;
141
142         virtual void stop_dequeue_thread() = 0;
143 };
144
145 // The actual capturing class, representing capture from a single card.
146 class BMUSBCapture : public CaptureInterface {
147  public:
148         BMUSBCapture(int card_index)
149                 : card_index(card_index)
150         {
151         }
152
153         ~BMUSBCapture() {}
154
155         std::map<uint32_t, VideoMode> get_available_video_modes() const override;
156         uint32_t get_current_video_mode() const override;
157         void set_video_mode(uint32_t video_mode_id) override;
158
159         // Does not take ownership.
160         void set_video_frame_allocator(FrameAllocator *allocator) override
161         {
162                 video_frame_allocator = allocator;
163         }
164
165         FrameAllocator *get_video_frame_allocator() override
166         {
167                 return video_frame_allocator;
168         }
169
170         // Does not take ownership.
171         void set_audio_frame_allocator(FrameAllocator *allocator) override
172         {
173                 audio_frame_allocator = allocator;
174         }
175
176         FrameAllocator *get_audio_frame_allocator() override
177         {
178                 return audio_frame_allocator;
179         }
180
181         void set_frame_callback(frame_callback_t callback) override
182         {
183                 frame_callback = callback;
184         }
185
186         // Needs to be run before configure_card().
187         void set_dequeue_thread_callbacks(std::function<void()> init, std::function<void()> cleanup) override
188         {
189                 dequeue_init_callback = init;
190                 dequeue_cleanup_callback = cleanup;
191                 has_dequeue_callbacks = true;
192         }
193
194         // Only valid after configure_card().
195         std::string get_description() const override {
196                 return description;
197         }
198
199         void configure_card() override;
200         void start_bm_capture() override;
201         void stop_dequeue_thread() override;
202
203         // TODO: It's rather messy to have these outside the interface.
204         static void start_bm_thread();
205         static void stop_bm_thread();
206
207  private:
208         struct QueuedFrame {
209                 uint16_t timecode;
210                 uint16_t format;
211                 FrameAllocator::Frame frame;
212         };
213
214         void start_new_audio_block(const uint8_t *start);
215         void start_new_frame(const uint8_t *start);
216
217         void queue_frame(uint16_t format, uint16_t timecode, FrameAllocator::Frame frame, std::deque<QueuedFrame> *q);
218         void dequeue_thread_func();
219
220         static void usb_thread_func();
221         static void cb_xfr(struct libusb_transfer *xfr);
222
223         std::string description;
224
225         FrameAllocator::Frame current_video_frame;
226         FrameAllocator::Frame current_audio_frame;
227
228         std::mutex queue_lock;
229         std::condition_variable queues_not_empty;
230         std::deque<QueuedFrame> pending_video_frames;
231         std::deque<QueuedFrame> pending_audio_frames;
232
233         FrameAllocator *video_frame_allocator = nullptr;
234         FrameAllocator *audio_frame_allocator = nullptr;
235         frame_callback_t frame_callback = nullptr;
236
237         std::thread dequeue_thread;
238         std::atomic<bool> dequeue_thread_should_quit;
239         bool has_dequeue_callbacks = false;
240         std::function<void()> dequeue_init_callback = nullptr;
241         std::function<void()> dequeue_cleanup_callback = nullptr;
242
243         int current_register = 0;
244
245         static constexpr int NUM_BMUSB_REGISTERS = 60;
246         uint8_t register_file[NUM_BMUSB_REGISTERS];
247
248         int card_index;
249         std::vector<libusb_transfer *> iso_xfrs;
250         int assumed_frame_width = 1280;
251 };
252
253 // Get details for the given video format; returns false if detection was incomplete.
254 // Note: Frame rate is _frame_ rate, not field rate. So 1080i60 gets 30/1, _not_ 60/1.
255 // "second_field_start" is only valid for interlaced modes; it signifies
256 // how many lines from the very top of the frame there are before the second field
257 // starts (so it will always be >= height/2 + extra_lines_top).
258 bool decode_video_format(uint16_t video_format, VideoFormat *decoded_video_format);
259
260 #endif