![](/media/cache/profiles/f095cedd23b99f1696fc8caecbcf257e.jpg.50x50_q85.jpg)
see: http://mbed.org/users/okini3939/notebook/wifi_webcam/
Dependencies: GSwifiInterface_ap_webcam USBHost mbed
USBHostC270/decodeMJPEG/decodeMJPEG.cpp@0:8558bdecb0fa, 2014-06-06 (annotated)
- Committer:
- okini3939
- Date:
- Fri Jun 06 00:44:06 2014 +0000
- Revision:
- 0:8558bdecb0fa
1st build
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
okini3939 | 0:8558bdecb0fa | 1 | // decodeMJPEG.cpp 2012/12/8 |
okini3939 | 0:8558bdecb0fa | 2 | // decode motion-jpeg to jpeg |
okini3939 | 0:8558bdecb0fa | 3 | #include "mbed.h" |
okini3939 | 0:8558bdecb0fa | 4 | #include "decodeMJPEG.h" |
okini3939 | 0:8558bdecb0fa | 5 | |
okini3939 | 0:8558bdecb0fa | 6 | #define MARK_SOF0 0xc0 |
okini3939 | 0:8558bdecb0fa | 7 | #define MARK_DHT 0xc4 |
okini3939 | 0:8558bdecb0fa | 8 | #define MARK_RST0 0xd0 |
okini3939 | 0:8558bdecb0fa | 9 | #define MARK_RST7 0xd7 |
okini3939 | 0:8558bdecb0fa | 10 | #define MARK_SOI 0xd8 |
okini3939 | 0:8558bdecb0fa | 11 | #define MARK_EOI 0xd9 |
okini3939 | 0:8558bdecb0fa | 12 | #define MARK_SOS 0xda |
okini3939 | 0:8558bdecb0fa | 13 | #define MARK_DQT 0xdb |
okini3939 | 0:8558bdecb0fa | 14 | #define MARK_DRI 0xdd |
okini3939 | 0:8558bdecb0fa | 15 | #define MARK_APP 0xe0 |
okini3939 | 0:8558bdecb0fa | 16 | |
okini3939 | 0:8558bdecb0fa | 17 | #define SEQ_INIT 0 |
okini3939 | 0:8558bdecb0fa | 18 | #define SEQ_SOI 1 |
okini3939 | 0:8558bdecb0fa | 19 | #define SEQ_FRAME 2 |
okini3939 | 0:8558bdecb0fa | 20 | #define SEQ_MARK 3 |
okini3939 | 0:8558bdecb0fa | 21 | #define SEQ_SEG_LEN 4 |
okini3939 | 0:8558bdecb0fa | 22 | #define SEQ_SEG_LEN2 5 |
okini3939 | 0:8558bdecb0fa | 23 | #define SEQ_SEG_BODY 6 |
okini3939 | 0:8558bdecb0fa | 24 | #define SEQ_SOS 7 |
okini3939 | 0:8558bdecb0fa | 25 | #define SEQ_SOS2 8 |
okini3939 | 0:8558bdecb0fa | 26 | |
okini3939 | 0:8558bdecb0fa | 27 | static const uint8_t dht[] = { |
okini3939 | 0:8558bdecb0fa | 28 | 0xFF,0xC4,0x01,0xA2,0x00,0x00,0x01,0x05,0x01,0x01,0x01,0x01,0x01,0x01,0x00,0x00, |
okini3939 | 0:8558bdecb0fa | 29 | 0x00,0x00,0x00,0x00,0x00,0x00,0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,0x09,0x0A, |
okini3939 | 0:8558bdecb0fa | 30 | 0x0B,0x01,0x00,0x03,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x00,0x00,0x00, |
okini3939 | 0:8558bdecb0fa | 31 | 0x00,0x00,0x00,0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,0x09,0x0A,0x0B,0x10,0x00, |
okini3939 | 0:8558bdecb0fa | 32 | 0x02,0x01,0x03,0x03,0x02,0x04,0x03,0x05,0x05,0x04,0x04,0x00,0x00,0x01,0x7D,0x01, |
okini3939 | 0:8558bdecb0fa | 33 | 0x02,0x03,0x00,0x04,0x11,0x05,0x12,0x21,0x31,0x41,0x06,0x13,0x51,0x61,0x07,0x22, |
okini3939 | 0:8558bdecb0fa | 34 | 0x71,0x14,0x32,0x81,0x91,0xA1,0x08,0x23,0x42,0xB1,0xC1,0x15,0x52,0xD1,0xF0,0x24, |
okini3939 | 0:8558bdecb0fa | 35 | 0x33,0x62,0x72,0x82,0x09,0x0A,0x16,0x17,0x18,0x19,0x1A,0x25,0x26,0x27,0x28,0x29, |
okini3939 | 0:8558bdecb0fa | 36 | 0x2A,0x34,0x35,0x36,0x37,0x38,0x39,0x3A,0x43,0x44,0x45,0x46,0x47,0x48,0x49,0x4A, |
okini3939 | 0:8558bdecb0fa | 37 | 0x53,0x54,0x55,0x56,0x57,0x58,0x59,0x5A,0x63,0x64,0x65,0x66,0x67,0x68,0x69,0x6A, |
okini3939 | 0:8558bdecb0fa | 38 | 0x73,0x74,0x75,0x76,0x77,0x78,0x79,0x7A,0x83,0x84,0x85,0x86,0x87,0x88,0x89,0x8A, |
okini3939 | 0:8558bdecb0fa | 39 | 0x92,0x93,0x94,0x95,0x96,0x97,0x98,0x99,0x9A,0xA2,0xA3,0xA4,0xA5,0xA6,0xA7,0xA8, |
okini3939 | 0:8558bdecb0fa | 40 | 0xA9,0xAA,0xB2,0xB3,0xB4,0xB5,0xB6,0xB7,0xB8,0xB9,0xBA,0xC2,0xC3,0xC4,0xC5,0xC6, |
okini3939 | 0:8558bdecb0fa | 41 | 0xC7,0xC8,0xC9,0xCA,0xD2,0xD3,0xD4,0xD5,0xD6,0xD7,0xD8,0xD9,0xDA,0xE1,0xE2,0xE3, |
okini3939 | 0:8558bdecb0fa | 42 | 0xE4,0xE5,0xE6,0xE7,0xE8,0xE9,0xEA,0xF1,0xF2,0xF3,0xF4,0xF5,0xF6,0xF7,0xF8,0xF9, |
okini3939 | 0:8558bdecb0fa | 43 | 0xFA,0x11,0x00,0x02,0x01,0x02,0x04,0x04,0x03,0x04,0x07,0x05,0x04,0x04,0x00,0x01, |
okini3939 | 0:8558bdecb0fa | 44 | 0x02,0x77,0x00,0x01,0x02,0x03,0x11,0x04,0x05,0x21,0x31,0x06,0x12,0x41,0x51,0x07, |
okini3939 | 0:8558bdecb0fa | 45 | 0x61,0x71,0x13,0x22,0x32,0x81,0x08,0x14,0x42,0x91,0xA1,0xB1,0xC1,0x09,0x23,0x33, |
okini3939 | 0:8558bdecb0fa | 46 | 0x52,0xF0,0x15,0x62,0x72,0xD1,0x0A,0x16,0x24,0x34,0xE1,0x25,0xF1,0x17,0x18,0x19, |
okini3939 | 0:8558bdecb0fa | 47 | 0x1A,0x26,0x27,0x28,0x29,0x2A,0x35,0x36,0x37,0x38,0x39,0x3A,0x43,0x44,0x45,0x46, |
okini3939 | 0:8558bdecb0fa | 48 | 0x47,0x48,0x49,0x4A,0x53,0x54,0x55,0x56,0x57,0x58,0x59,0x5A,0x63,0x64,0x65,0x66, |
okini3939 | 0:8558bdecb0fa | 49 | 0x67,0x68,0x69,0x6A,0x73,0x74,0x75,0x76,0x77,0x78,0x79,0x7A,0x82,0x83,0x84,0x85, |
okini3939 | 0:8558bdecb0fa | 50 | 0x86,0x87,0x88,0x89,0x8A,0x92,0x93,0x94,0x95,0x96,0x97,0x98,0x99,0x9A,0xA2,0xA3, |
okini3939 | 0:8558bdecb0fa | 51 | 0xA4,0xA5,0xA6,0xA7,0xA8,0xA9,0xAA,0xB2,0xB3,0xB4,0xB5,0xB6,0xB7,0xB8,0xB9,0xBA, |
okini3939 | 0:8558bdecb0fa | 52 | 0xC2,0xC3,0xC4,0xC5,0xC6,0xC7,0xC8,0xC9,0xCA,0xD2,0xD3,0xD4,0xD5,0xD6,0xD7,0xD8, |
okini3939 | 0:8558bdecb0fa | 53 | 0xD9,0xDA,0xE2,0xE3,0xE4,0xE5,0xE6,0xE7,0xE8,0xE9,0xEA,0xF2,0xF3,0xF4,0xF5,0xF6, |
okini3939 | 0:8558bdecb0fa | 54 | 0xF7,0xF8,0xF9,0xFA, |
okini3939 | 0:8558bdecb0fa | 55 | }; |
okini3939 | 0:8558bdecb0fa | 56 | |
okini3939 | 0:8558bdecb0fa | 57 | decodeMJPEG::decodeMJPEG() |
okini3939 | 0:8558bdecb0fa | 58 | { |
okini3939 | 0:8558bdecb0fa | 59 | m_seq = SEQ_INIT; |
okini3939 | 0:8558bdecb0fa | 60 | } |
okini3939 | 0:8558bdecb0fa | 61 | |
okini3939 | 0:8558bdecb0fa | 62 | void decodeMJPEG::inputPacket(const uint8_t* buf, int len) |
okini3939 | 0:8558bdecb0fa | 63 | { |
okini3939 | 0:8558bdecb0fa | 64 | for(int i = 12; i < len; i++) { |
okini3939 | 0:8558bdecb0fa | 65 | input(buf[i]); |
okini3939 | 0:8558bdecb0fa | 66 | } |
okini3939 | 0:8558bdecb0fa | 67 | } |
okini3939 | 0:8558bdecb0fa | 68 | |
okini3939 | 0:8558bdecb0fa | 69 | void decodeMJPEG::input(uint8_t c) |
okini3939 | 0:8558bdecb0fa | 70 | { |
okini3939 | 0:8558bdecb0fa | 71 | switch(m_seq) { |
okini3939 | 0:8558bdecb0fa | 72 | case SEQ_INIT: |
okini3939 | 0:8558bdecb0fa | 73 | if (c == 0xff) { |
okini3939 | 0:8558bdecb0fa | 74 | m_seq = SEQ_SOI; |
okini3939 | 0:8558bdecb0fa | 75 | } |
okini3939 | 0:8558bdecb0fa | 76 | break; |
okini3939 | 0:8558bdecb0fa | 77 | case SEQ_SOI: |
okini3939 | 0:8558bdecb0fa | 78 | if (c == MARK_SOI) { |
okini3939 | 0:8558bdecb0fa | 79 | outputJPEG(0xff, JPEG_START); // start |
okini3939 | 0:8558bdecb0fa | 80 | outputJPEG(c); |
okini3939 | 0:8558bdecb0fa | 81 | m_bDHT = false; |
okini3939 | 0:8558bdecb0fa | 82 | m_seq = SEQ_FRAME; |
okini3939 | 0:8558bdecb0fa | 83 | } else { |
okini3939 | 0:8558bdecb0fa | 84 | m_seq = SEQ_INIT; |
okini3939 | 0:8558bdecb0fa | 85 | } |
okini3939 | 0:8558bdecb0fa | 86 | break; |
okini3939 | 0:8558bdecb0fa | 87 | case SEQ_FRAME: |
okini3939 | 0:8558bdecb0fa | 88 | if (c == 0xff) { |
okini3939 | 0:8558bdecb0fa | 89 | m_seq = SEQ_MARK; |
okini3939 | 0:8558bdecb0fa | 90 | } else { |
okini3939 | 0:8558bdecb0fa | 91 | m_seq = SEQ_INIT; |
okini3939 | 0:8558bdecb0fa | 92 | } |
okini3939 | 0:8558bdecb0fa | 93 | break; |
okini3939 | 0:8558bdecb0fa | 94 | case SEQ_MARK: |
okini3939 | 0:8558bdecb0fa | 95 | if (c == MARK_SOI || c == MARK_EOI || c == 0x00) { |
okini3939 | 0:8558bdecb0fa | 96 | m_seq = SEQ_INIT; |
okini3939 | 0:8558bdecb0fa | 97 | break; |
okini3939 | 0:8558bdecb0fa | 98 | } |
okini3939 | 0:8558bdecb0fa | 99 | m_mark = c; |
okini3939 | 0:8558bdecb0fa | 100 | m_seq = SEQ_SEG_LEN; |
okini3939 | 0:8558bdecb0fa | 101 | break; |
okini3939 | 0:8558bdecb0fa | 102 | case SEQ_SEG_LEN: |
okini3939 | 0:8558bdecb0fa | 103 | m_seg_len = c; |
okini3939 | 0:8558bdecb0fa | 104 | m_seq = SEQ_SEG_LEN2; |
okini3939 | 0:8558bdecb0fa | 105 | break; |
okini3939 | 0:8558bdecb0fa | 106 | case SEQ_SEG_LEN2: |
okini3939 | 0:8558bdecb0fa | 107 | m_seg_len <<= 8; |
okini3939 | 0:8558bdecb0fa | 108 | m_seg_len |= c; |
okini3939 | 0:8558bdecb0fa | 109 | m_seg_len -= 2; |
okini3939 | 0:8558bdecb0fa | 110 | m_seg_pos = 0; |
okini3939 | 0:8558bdecb0fa | 111 | m_seq = SEQ_SEG_BODY; |
okini3939 | 0:8558bdecb0fa | 112 | if (m_mark == MARK_SOS) { |
okini3939 | 0:8558bdecb0fa | 113 | if (m_bDHT == false) { |
okini3939 | 0:8558bdecb0fa | 114 | for(int i = 0; i < sizeof(dht); i++) { |
okini3939 | 0:8558bdecb0fa | 115 | outputJPEG(dht[i]); |
okini3939 | 0:8558bdecb0fa | 116 | } |
okini3939 | 0:8558bdecb0fa | 117 | } |
okini3939 | 0:8558bdecb0fa | 118 | m_output_desable = false; |
okini3939 | 0:8558bdecb0fa | 119 | } else if (m_mark == MARK_DHT) { |
okini3939 | 0:8558bdecb0fa | 120 | m_bDHT = true; |
okini3939 | 0:8558bdecb0fa | 121 | m_output_desable = false; |
okini3939 | 0:8558bdecb0fa | 122 | } else { |
okini3939 | 0:8558bdecb0fa | 123 | m_output_desable = false; |
okini3939 | 0:8558bdecb0fa | 124 | } |
okini3939 | 0:8558bdecb0fa | 125 | if (!m_output_desable) { |
okini3939 | 0:8558bdecb0fa | 126 | outputJPEG(0xff); |
okini3939 | 0:8558bdecb0fa | 127 | outputJPEG(m_mark); |
okini3939 | 0:8558bdecb0fa | 128 | outputJPEG((m_seg_len+2) >> 8); |
okini3939 | 0:8558bdecb0fa | 129 | outputJPEG((m_seg_len+2) & 0xff); |
okini3939 | 0:8558bdecb0fa | 130 | } |
okini3939 | 0:8558bdecb0fa | 131 | break; |
okini3939 | 0:8558bdecb0fa | 132 | case SEQ_SEG_BODY: |
okini3939 | 0:8558bdecb0fa | 133 | if (!m_output_desable) { |
okini3939 | 0:8558bdecb0fa | 134 | outputJPEG(c); |
okini3939 | 0:8558bdecb0fa | 135 | } |
okini3939 | 0:8558bdecb0fa | 136 | if (++m_seg_pos < m_seg_len) { |
okini3939 | 0:8558bdecb0fa | 137 | break; |
okini3939 | 0:8558bdecb0fa | 138 | } |
okini3939 | 0:8558bdecb0fa | 139 | if (m_mark == MARK_SOS) { |
okini3939 | 0:8558bdecb0fa | 140 | m_seq = SEQ_SOS; |
okini3939 | 0:8558bdecb0fa | 141 | break; |
okini3939 | 0:8558bdecb0fa | 142 | } |
okini3939 | 0:8558bdecb0fa | 143 | m_seq = SEQ_FRAME; |
okini3939 | 0:8558bdecb0fa | 144 | break; |
okini3939 | 0:8558bdecb0fa | 145 | case SEQ_SOS: |
okini3939 | 0:8558bdecb0fa | 146 | if (c == 0xff) { |
okini3939 | 0:8558bdecb0fa | 147 | m_seq = SEQ_SOS2; |
okini3939 | 0:8558bdecb0fa | 148 | break; |
okini3939 | 0:8558bdecb0fa | 149 | } |
okini3939 | 0:8558bdecb0fa | 150 | outputJPEG(c); |
okini3939 | 0:8558bdecb0fa | 151 | break; |
okini3939 | 0:8558bdecb0fa | 152 | case SEQ_SOS2: |
okini3939 | 0:8558bdecb0fa | 153 | if (c == 0x00) { |
okini3939 | 0:8558bdecb0fa | 154 | outputJPEG(0xff); |
okini3939 | 0:8558bdecb0fa | 155 | outputJPEG(0x00); |
okini3939 | 0:8558bdecb0fa | 156 | m_seq = SEQ_SOS; |
okini3939 | 0:8558bdecb0fa | 157 | break; |
okini3939 | 0:8558bdecb0fa | 158 | } else if (c >= MARK_RST0 && c <= MARK_RST7) { |
okini3939 | 0:8558bdecb0fa | 159 | outputJPEG(0xff); |
okini3939 | 0:8558bdecb0fa | 160 | outputJPEG(c); |
okini3939 | 0:8558bdecb0fa | 161 | m_seq = SEQ_SOS; |
okini3939 | 0:8558bdecb0fa | 162 | break; |
okini3939 | 0:8558bdecb0fa | 163 | } else if (c == MARK_EOI) { |
okini3939 | 0:8558bdecb0fa | 164 | outputJPEG(0xff); |
okini3939 | 0:8558bdecb0fa | 165 | outputJPEG(c, JPEG_END); |
okini3939 | 0:8558bdecb0fa | 166 | m_seq = SEQ_INIT; |
okini3939 | 0:8558bdecb0fa | 167 | break; |
okini3939 | 0:8558bdecb0fa | 168 | } else if (c == MARK_SOI) { |
okini3939 | 0:8558bdecb0fa | 169 | outputJPEG(0xff); |
okini3939 | 0:8558bdecb0fa | 170 | outputJPEG(c); |
okini3939 | 0:8558bdecb0fa | 171 | m_seq = SEQ_INIT; |
okini3939 | 0:8558bdecb0fa | 172 | break; |
okini3939 | 0:8558bdecb0fa | 173 | } |
okini3939 | 0:8558bdecb0fa | 174 | m_seq = SEQ_INIT; |
okini3939 | 0:8558bdecb0fa | 175 | break; |
okini3939 | 0:8558bdecb0fa | 176 | default: |
okini3939 | 0:8558bdecb0fa | 177 | m_seq = SEQ_INIT; |
okini3939 | 0:8558bdecb0fa | 178 | break; |
okini3939 | 0:8558bdecb0fa | 179 | } |
okini3939 | 0:8558bdecb0fa | 180 | } |