]> git.sesse.net Git - bmusb/commitdiff
Unify the audio and video add_frame functions.
authorSteinar H. Gunderson <sgunderson@bigfoot.com>
Thu, 17 Sep 2015 20:03:50 +0000 (22:03 +0200)
committerSteinar H. Gunderson <sgunderson@bigfoot.com>
Thu, 17 Sep 2015 20:03:50 +0000 (22:03 +0200)
bmusb.cpp

index 4f64d6c443c0348dbc2386d94fb8bfbd2f45e824..04b54a6a9c2ac6bd582098a24cd2e8cd69aa37e1 100644 (file)
--- a/bmusb.cpp
+++ b/bmusb.cpp
@@ -199,22 +199,6 @@ void dequeue_thread()
        }
 }
 
-void add_current_frame(const uint8_t *start, const uint8_t *end)
-{
-       if (current_video_frame.data == nullptr ||
-           current_video_frame.len > current_video_frame.size) return;
-       if (start == end) return;
-
-       int bytes = end - start;
-       if (current_video_frame.len + bytes > current_video_frame.size) {
-               printf("%d bytes overflow after last video frame\n", current_video_frame.len + bytes - current_video_frame.size);
-               //dump_frame();
-       } else {
-               memcpy(current_video_frame.data + current_video_frame.len, start, bytes);
-               current_video_frame.len += bytes;
-       }
-}
-
 void start_new_frame(const uint8_t *start)
 {
        uint16_t format = (start[3] << 8) | start[2];
@@ -237,22 +221,6 @@ void start_new_frame(const uint8_t *start)
        //}
 }
 
-void add_current_audio(const uint8_t *start, const uint8_t *end)
-{
-       if (current_audio_frame.data == nullptr ||
-           current_audio_frame.len > current_audio_frame.size) return;
-       if (start == end) return;
-
-       int bytes = end - start;
-       if (current_audio_frame.len + bytes > current_audio_frame.size) {
-               printf("%d bytes overflow after last audio block\n", current_audio_frame.len + bytes - current_audio_frame.size);
-               //dump_audio_block();
-       } else {
-               memcpy(current_audio_frame.data + current_audio_frame.len, start, bytes);
-               current_audio_frame.len += bytes;
-       }
-}
-
 void start_new_audio_block(const uint8_t *start)
 {
        uint16_t format = (start[3] << 8) | start[2];
@@ -281,7 +249,30 @@ static void dump_pack(const libusb_transfer *xfr, int offset, const libusb_iso_p
        }
 }
 
-void decode_packs(const libusb_transfer *xfr, const char *sync_pattern, int sync_length, function<void(const uint8_t *start, const uint8_t *end)> add_callback, function<void(const uint8_t *start)> start_callback)
+void add_to_frame(FrameAllocator::Frame *current_frame, const char *frame_type_name, const uint8_t *start, const uint8_t *end)
+{
+       if (current_frame->data == nullptr ||
+           current_frame->len > current_video_frame.size ||
+           start == end) {
+               return;
+       }
+
+       int bytes = end - start;
+       if (current_frame->len + bytes > current_frame->size) {
+               printf("%d bytes overflow after last %s frame\n", current_frame->len + bytes - current_frame->size, frame_type_name);
+               //dump_frame();
+       } else {
+               memcpy(current_frame->data + current_frame->len, start, bytes);
+               current_frame->len += bytes;
+       }
+}
+
+void decode_packs(const libusb_transfer *xfr,
+                  const char *sync_pattern,
+                  int sync_length,
+                  FrameAllocator::Frame *current_frame,
+                  const char *frame_type_name,
+                  function<void(const uint8_t *start)> start_callback)
 {
        int offset = 0;
        for (unsigned i = 0; i < xfr->num_iso_packets; i++) {
@@ -298,10 +289,14 @@ void decode_packs(const libusb_transfer *xfr, const char *sync_pattern, int sync
                        const unsigned char* start_next_frame = (const unsigned char *)memmem(iso_start + iso_offset, pack->actual_length - iso_offset, sync_pattern, sync_length);
                        if (start_next_frame == nullptr) {
                                // add the rest of the buffer
-                               add_callback(iso_start + iso_offset, iso_start + pack->actual_length);
+                               const uint8_t *start = iso_start + iso_offset;
+                               const uint8_t *end = iso_start + pack->actual_length;
+                               add_to_frame(current_frame, frame_type_name, start, end);
                                break;
                        } else {
-                               add_callback(iso_start + iso_offset, start_next_frame);
+                               const uint8_t *start = iso_start + iso_offset;
+                               const uint8_t *end = start_next_frame;
+                               add_to_frame(current_frame, frame_type_name, start, end);
                                start_callback(start_next_frame + sync_length);
 
                                int suboffset = start_next_frame - iso_start;
@@ -325,9 +320,9 @@ static void cb_xfr(struct libusb_transfer *xfr)
 
        if (xfr->type == LIBUSB_TRANSFER_TYPE_ISOCHRONOUS) {
                if (xfr->endpoint == 0x84) {
-                       decode_packs(xfr, "DeckLinkAudioResyncT", 20, add_current_audio, start_new_audio_block);
+                       decode_packs(xfr, "DeckLinkAudioResyncT", 20, &current_audio_frame, "audio", start_new_audio_block);
                } else {
-                       decode_packs(xfr, "\x00\x00\xff\xff", 4, add_current_frame, start_new_frame);
+                       decode_packs(xfr, "\x00\x00\xff\xff", 4, &current_video_frame, "video", start_new_frame);
                }
        }
        if (xfr->type == LIBUSB_TRANSFER_TYPE_CONTROL) {