BLE demo for mbed Ported RunningElectronics's SBDBT firmware for BLE. It can communicate with iOS
Dependencies: FatFileSystem mbed
Fork of BTstack by
uvc/usb_mjpeg.cpp@6:cf06ba884429, 2013-02-20 (annotated)
- Committer:
- todotani
- Date:
- Wed Feb 20 14:18:38 2013 +0000
- Revision:
- 6:cf06ba884429
- Parent:
- 0:1ed23ab1345f
Change tick timer to 1ms. Change attribute 0xFFF1 as read of DigitalIn p5
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
va009039 | 0:1ed23ab1345f | 1 | #include "mbed.h" |
va009039 | 0:1ed23ab1345f | 2 | #include "usb_mjpeg.h" |
va009039 | 0:1ed23ab1345f | 3 | //#define __DEBUG |
va009039 | 0:1ed23ab1345f | 4 | #define __DEBUG2 |
va009039 | 0:1ed23ab1345f | 5 | #include "mydbg.h" |
va009039 | 0:1ed23ab1345f | 6 | #include "Utils.h" |
va009039 | 0:1ed23ab1345f | 7 | #include "myjpeg.h" |
va009039 | 0:1ed23ab1345f | 8 | |
va009039 | 0:1ed23ab1345f | 9 | #ifdef __DEBUG |
va009039 | 0:1ed23ab1345f | 10 | extern DigitalOut led4; |
va009039 | 0:1ed23ab1345f | 11 | #endif // __DEBUG |
va009039 | 0:1ed23ab1345f | 12 | |
va009039 | 0:1ed23ab1345f | 13 | #define MJPEG_FID 0x01 |
va009039 | 0:1ed23ab1345f | 14 | #define MJPEG_EOF 0x02 |
va009039 | 0:1ed23ab1345f | 15 | #define MJPEG_PTS 0x04 |
va009039 | 0:1ed23ab1345f | 16 | #define MJPEG_SCR 0x08 |
va009039 | 0:1ed23ab1345f | 17 | #define MJPEG_STI 0x20 |
va009039 | 0:1ed23ab1345f | 18 | #define MJPEG_ERR 0x40 |
va009039 | 0:1ed23ab1345f | 19 | #define MJPEG_EOH 0x80 |
va009039 | 0:1ed23ab1345f | 20 | |
va009039 | 0:1ed23ab1345f | 21 | |
va009039 | 0:1ed23ab1345f | 22 | usb_mjpeg::usb_mjpeg(uint8_t* buf, int size) |
va009039 | 0:1ed23ab1345f | 23 | { |
va009039 | 0:1ed23ab1345f | 24 | DBG_ASSERT(size >= 1024 && size <= 16000); |
va009039 | 0:1ed23ab1345f | 25 | m_size = size; |
va009039 | 0:1ed23ab1345f | 26 | m_seq = 0; |
va009039 | 0:1ed23ab1345f | 27 | m_buf = buf; |
va009039 | 0:1ed23ab1345f | 28 | ReportErrorFID = 0; |
va009039 | 0:1ed23ab1345f | 29 | ReportErrorPTS = 0; |
va009039 | 0:1ed23ab1345f | 30 | } |
va009039 | 0:1ed23ab1345f | 31 | |
va009039 | 0:1ed23ab1345f | 32 | usb_mjpeg::~usb_mjpeg() |
va009039 | 0:1ed23ab1345f | 33 | { |
va009039 | 0:1ed23ab1345f | 34 | |
va009039 | 0:1ed23ab1345f | 35 | } |
va009039 | 0:1ed23ab1345f | 36 | |
va009039 | 0:1ed23ab1345f | 37 | void usb_mjpeg::input(uint16_t frame, uint8_t* buf, int len) |
va009039 | 0:1ed23ab1345f | 38 | { |
va009039 | 0:1ed23ab1345f | 39 | uint8_t* StreamHeader = buf; |
va009039 | 0:1ed23ab1345f | 40 | DBG_ASSERT(buf); |
va009039 | 0:1ed23ab1345f | 41 | if (len > 12) { |
va009039 | 0:1ed23ab1345f | 42 | //DBG_PRINTF("%d %02X %d\n", frame, buf[1], len); |
va009039 | 0:1ed23ab1345f | 43 | //DBG_HEX(buf, len); |
va009039 | 0:1ed23ab1345f | 44 | DBG_ASSERT(StreamHeader[0] == 0x0c); |
va009039 | 0:1ed23ab1345f | 45 | //DBG_ASSERT(buf[1] == 0x8c || buf[1] == 0x8d); |
va009039 | 0:1ed23ab1345f | 46 | //DBG("bfh:%02X\n", buf[1]); |
va009039 | 0:1ed23ab1345f | 47 | } |
va009039 | 0:1ed23ab1345f | 48 | DBG_ASSERT(len >= 2); |
va009039 | 0:1ed23ab1345f | 49 | |
va009039 | 0:1ed23ab1345f | 50 | int hle = StreamHeader[0]; |
va009039 | 0:1ed23ab1345f | 51 | uint8_t bfh = StreamHeader[1]; |
va009039 | 0:1ed23ab1345f | 52 | DBG_ASSERT(len >= hle); |
va009039 | 0:1ed23ab1345f | 53 | DBG_ASSERT(hle == 12); |
va009039 | 0:1ed23ab1345f | 54 | uint8_t* data = buf + hle; |
va009039 | 0:1ed23ab1345f | 55 | int data_len = len - hle; |
va009039 | 0:1ed23ab1345f | 56 | if (m_seq == 0) { |
va009039 | 0:1ed23ab1345f | 57 | if (check_SOI(buf, len)) { |
va009039 | 0:1ed23ab1345f | 58 | DBG_PRINTF("%04X SOI\n", frame); |
va009039 | 0:1ed23ab1345f | 59 | DBG_BYTES("SOI", buf, 16); |
va009039 | 0:1ed23ab1345f | 60 | _open(); |
va009039 | 0:1ed23ab1345f | 61 | m_bfh = bfh; // save FID |
va009039 | 0:1ed23ab1345f | 62 | if (bfh & MJPEG_PTS) { |
va009039 | 0:1ed23ab1345f | 63 | m_pts = LE32(StreamHeader+2); // save PTS |
va009039 | 0:1ed23ab1345f | 64 | } |
va009039 | 0:1ed23ab1345f | 65 | _wrtie(data, data_len); |
va009039 | 0:1ed23ab1345f | 66 | m_seq++; |
va009039 | 0:1ed23ab1345f | 67 | } |
va009039 | 0:1ed23ab1345f | 68 | } else if (m_seq == 1) { |
va009039 | 0:1ed23ab1345f | 69 | if (len > hle) { |
va009039 | 0:1ed23ab1345f | 70 | _wrtie(data, data_len); |
va009039 | 0:1ed23ab1345f | 71 | } |
va009039 | 0:1ed23ab1345f | 72 | if (check_EOI(buf, len)) { |
va009039 | 0:1ed23ab1345f | 73 | DBG_PRINTF("%04X EOI\n", frame); |
va009039 | 0:1ed23ab1345f | 74 | DBG_BYTES("EOI", buf, 12); |
va009039 | 0:1ed23ab1345f | 75 | _close(); |
va009039 | 0:1ed23ab1345f | 76 | m_seq = 2; // done |
va009039 | 0:1ed23ab1345f | 77 | } else if ((m_bfh ^ bfh) & MJPEG_FID) { |
va009039 | 0:1ed23ab1345f | 78 | ReportErrorFID++; |
va009039 | 0:1ed23ab1345f | 79 | DBG("ReportErrorFID=%d\n", ReportErrorFID); |
va009039 | 0:1ed23ab1345f | 80 | m_seq = 0; // restart |
va009039 | 0:1ed23ab1345f | 81 | } else if ((bfh & MJPEG_PTS) && m_pts != LE32(StreamHeader+2)) { |
va009039 | 0:1ed23ab1345f | 82 | ReportErrorPTS++; |
va009039 | 0:1ed23ab1345f | 83 | DBG("ReportErrorPTS=%d\n", ReportErrorPTS); |
va009039 | 0:1ed23ab1345f | 84 | m_seq = 0; // restart |
va009039 | 0:1ed23ab1345f | 85 | } |
va009039 | 0:1ed23ab1345f | 86 | } else { // done |
va009039 | 0:1ed23ab1345f | 87 | } |
va009039 | 0:1ed23ab1345f | 88 | DBG_LED4(buf[1] & MJPEG_FID); // FID |
va009039 | 0:1ed23ab1345f | 89 | } |
va009039 | 0:1ed23ab1345f | 90 | |
va009039 | 0:1ed23ab1345f | 91 | int usb_mjpeg::status() |
va009039 | 0:1ed23ab1345f | 92 | { |
va009039 | 0:1ed23ab1345f | 93 | if (m_seq <= 1) { |
va009039 | 0:1ed23ab1345f | 94 | return USBERR_PROCESSING; |
va009039 | 0:1ed23ab1345f | 95 | } |
va009039 | 0:1ed23ab1345f | 96 | return m_pos; |
va009039 | 0:1ed23ab1345f | 97 | } |
va009039 | 0:1ed23ab1345f | 98 | |
va009039 | 0:1ed23ab1345f | 99 | bool usb_mjpeg::check_SOI(uint8_t* buf, int len) |
va009039 | 0:1ed23ab1345f | 100 | { |
va009039 | 0:1ed23ab1345f | 101 | if (len >= (12+2)) { |
va009039 | 0:1ed23ab1345f | 102 | if (buf[12] == 0xff && buf[13] == 0xd8) { |
va009039 | 0:1ed23ab1345f | 103 | return true; |
va009039 | 0:1ed23ab1345f | 104 | } |
va009039 | 0:1ed23ab1345f | 105 | } |
va009039 | 0:1ed23ab1345f | 106 | return false; |
va009039 | 0:1ed23ab1345f | 107 | } |
va009039 | 0:1ed23ab1345f | 108 | |
va009039 | 0:1ed23ab1345f | 109 | bool usb_mjpeg::check_EOI(uint8_t* buf, int len) |
va009039 | 0:1ed23ab1345f | 110 | { |
va009039 | 0:1ed23ab1345f | 111 | if (len >= 12) { |
va009039 | 0:1ed23ab1345f | 112 | if (buf[1] & MJPEG_EOF) { |
va009039 | 0:1ed23ab1345f | 113 | return true; |
va009039 | 0:1ed23ab1345f | 114 | } |
va009039 | 0:1ed23ab1345f | 115 | } |
va009039 | 0:1ed23ab1345f | 116 | return false; |
va009039 | 0:1ed23ab1345f | 117 | } |
va009039 | 0:1ed23ab1345f | 118 | |
va009039 | 0:1ed23ab1345f | 119 | void usb_mjpeg::_open() |
va009039 | 0:1ed23ab1345f | 120 | { |
va009039 | 0:1ed23ab1345f | 121 | m_pos = 0; |
va009039 | 0:1ed23ab1345f | 122 | } |
va009039 | 0:1ed23ab1345f | 123 | |
va009039 | 0:1ed23ab1345f | 124 | void usb_mjpeg::_wrtie(uint8_t* buf, int len) |
va009039 | 0:1ed23ab1345f | 125 | { |
va009039 | 0:1ed23ab1345f | 126 | if (m_buf == NULL) { |
va009039 | 0:1ed23ab1345f | 127 | return; |
va009039 | 0:1ed23ab1345f | 128 | } |
va009039 | 0:1ed23ab1345f | 129 | for(int i = 0; i < len && m_pos < m_size; i++) { |
va009039 | 0:1ed23ab1345f | 130 | m_buf[m_pos++] = buf[i]; |
va009039 | 0:1ed23ab1345f | 131 | } |
va009039 | 0:1ed23ab1345f | 132 | } |
va009039 | 0:1ed23ab1345f | 133 | |
va009039 | 0:1ed23ab1345f | 134 | void usb_mjpeg::_close() |
va009039 | 0:1ed23ab1345f | 135 | { |
va009039 | 0:1ed23ab1345f | 136 | //DBG("m_pos=%d\n", m_pos); |
va009039 | 0:1ed23ab1345f | 137 | //DBG_HEX(m_buf, m_pos); |
va009039 | 0:1ed23ab1345f | 138 | myjpeg JPEG(m_buf, m_pos, m_size); |
va009039 | 0:1ed23ab1345f | 139 | JPEG.analytics(); |
va009039 | 0:1ed23ab1345f | 140 | if (JPEG.DHT_pos == 0) { |
va009039 | 0:1ed23ab1345f | 141 | m_pos = JPEG.insertDHT(); |
va009039 | 0:1ed23ab1345f | 142 | } |
va009039 | 0:1ed23ab1345f | 143 | } |