]> git.sesse.net Git - bmusb/blob - bmusb.h
Add an interleaved mode to split UYVY into YV and YY on-the-fly.
[bmusb] / bmusb.h
1 #ifndef _BMUSB_H
2 #define _BMUSB_H
3
4 #include <stdint.h>
5 #include <functional>
6
7 // An interface for frame allocators; if you do not specify one
8 // (using set_video_frame_allocator), a default one that pre-allocates
9 // a freelist of eight frames using new[] will be used. Specifying
10 // your own can be useful if you have special demands for where you want the
11 // frame to end up and don't want to spend the extra copy to get it there, for
12 // instance GPU memory.
13 class FrameAllocator {
14  public:
15         struct Frame {
16                 uint8_t *data = nullptr;
17                 uint8_t *data2 = nullptr;  // Only if interleaved == true.
18                 size_t len = 0;  // Number of bytes we actually have.
19                 size_t size = 0;  // Number of bytes we have room for.
20                 void *userdata = nullptr;
21                 FrameAllocator *owner = nullptr;
22
23                 // If set to true, every other byte will go to data and to data2.
24                 // If so, <len> and <size> are still about the number of total bytes
25                 // so if size == 1024, there's 512 bytes in data and 512 in data2.
26                 bool interleaved = false;
27         };
28
29         virtual ~FrameAllocator();
30
31         // Request a video frame. Note that this is called from the
32         // USB thread, which runs with realtime priority and is
33         // very sensitive to delays. Thus, you should not do anything
34         // here that might sleep, including calling malloc().
35         // (Taking a mutex is borderline.)
36         //
37         // The Frame object will be given to the frame callback,
38         // which is responsible for releasing the video frame back
39         // once it is usable for new frames (ie., it will no longer
40         // be read from). You can use the "userdata" pointer for
41         // whatever you want to identify this frame if you need to.
42         //
43         // Returning a Frame with data==nullptr is allowed;
44         // if so, the frame in progress will be dropped.
45         virtual Frame alloc_frame() = 0;
46
47         virtual void release_frame(Frame frame) = 0;
48 };
49
50 typedef std::function<void(uint16_t timecode,
51                            FrameAllocator::Frame video_frame, size_t video_offset, uint16_t video_format,
52                            FrameAllocator::Frame audio_frame, size_t audio_offset, uint16_t audio_format)>
53         frame_callback_t;
54
55 void set_video_frame_allocator(FrameAllocator *allocator);  // Does not take ownership.
56 FrameAllocator *get_video_frame_allocator();
57
58 void set_audio_frame_allocator(FrameAllocator *allocator);  // Does not take ownership.
59 FrameAllocator *get_audio_frame_allocator();
60
61 void set_frame_callback(frame_callback_t callback);
62 void start_bm_capture();
63 void stop_bm_capture();
64
65 #endif