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