SysIo  1.9.0
Embedded Library and tools
ssdv.h
1 
2 /* SSDV - Slow Scan Digital Video */
3 /*=======================================================================*/
4 /* Copyright 2011-2012 Philip Heron <phil@sanslogic.co.uk */
5 /* */
6 /* This program is free software: you can redistribute it and/or modify */
7 /* it under the terms of the GNU General Public License as published by */
8 /* the Free Software Foundation, either version 3 of the License, or */
9 /* (at your option) any later version. */
10 /* */
11 /* This program is distributed in the hope that it will be useful, */
12 /* but WITHOUT ANY WARRANTY; without even the implied warranty of */
13 /* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the */
14 /* GNU General Public License for more details. */
15 /* */
16 /* You should have received a copy of the GNU General Public License */
17 /* along with this program. If not, see <http://www.gnu.org/licenses/>. */
18 
19 #include <stdint.h>
20 
21 #ifndef INC_SSDV_H
22 #define INC_SSDV_H
23 #ifdef __cplusplus
24 extern "C" {
25 #endif
26 
27 #define SSDV_ERROR (-1)
28 #define SSDV_OK (0)
29 #define SSDV_FEED_ME (1)
30 #define SSDV_HAVE_PACKET (2)
31 #define SSDV_BUFFER_FULL (3)
32 #define SSDV_EOI (4)
33 
34 /* Packet details */
35 #define SSDV_PKT_SIZE (0x100)
36 #define SSDV_PKT_SIZE_HEADER (0x0F)
37 #define SSDV_PKT_SIZE_CRC (0x04)
38 #define SSDV_PKT_SIZE_RSCODES (0x20)
39 #define SSDV_PKT_SIZE_PAYLOAD (SSDV_PKT_SIZE - SSDV_PKT_SIZE_HEADER - SSDV_PKT_SIZE_CRC - SSDV_PKT_SIZE_RSCODES)
40 #define SSDV_PKT_SIZE_CRCDATA (SSDV_PKT_SIZE_HEADER + SSDV_PKT_SIZE_PAYLOAD - 1)
41 
42 #define TBL_LEN (546) /* Maximum size of the DQT and DHT tables */
43 #define HBUFF_LEN (16) /* Extra space for reading marker data */
44 //#define COMPONENTS (3)
45 
46 typedef struct
47 {
48  /* Image information */
49  uint16_t width;
50  uint16_t height;
51  uint32_t callsign;
52  uint8_t image_id;
53  uint16_t packet_id;
54  uint8_t mcu_mode; /* 0 = 2x2, 1 = 2x1, 2 = 1x2, 3 = 1x1 */
55  uint16_t mcu_id;
56  uint16_t mcu_count;
57  uint16_t packet_mcu_id;
58  uint8_t packet_mcu_offset;
59 
60  /* Source buffer */
61  uint8_t *inp; /* Pointer to next input byte */
62  size_t in_len; /* Number of input bytes remaining */
63  size_t in_skip; /* Number of input bytes to skip */
64 
65  /* Source bits */
66  uint32_t workbits; /* Input bits currently being worked on */
67  uint8_t worklen; /* Number of bits in the input bit buffer */
68 
69  /* JPEG / Packet output buffer */
70  uint8_t *out; /* Pointer to the beginning of the output buffer */
71  uint8_t *outp; /* Pointer to the next output byte */
72  size_t out_len; /* Number of output bytes remaining */
73  char out_stuff; /* Flag to add stuffing bytes to output */
74 
75  /* Output bits */
76  uint32_t outbits; /* Output bit buffer */
77  uint8_t outlen; /* Number of bits in the output bit buffer */
78 
79  /* JPEG decoder state */
80  enum {
81  S_MARKER = 0,
82  S_MARKER_LEN,
83  S_MARKER_DATA,
84  S_HUFF,
85  S_INT,
86  S_EOI,
87  } state;
88  uint16_t marker; /* Current marker */
89  uint16_t marker_len; /* Length of data following marker */
90  uint8_t *marker_data; /* Where to copy marker data too */
91  uint16_t marker_data_len; /* How much is there */
92  uint8_t component; /* 0 = Y, 1 = Cb, 2 = Cr */
93  uint8_t ycparts; /* Number of Y component parts per MCU */
94  uint8_t mcupart; /* 0-3 = Y, 4 = Cb, 5 = Cr */
95  uint8_t acpart; /* 0 - 64; 0 = DC, 1 - 64 = AC */
96  int dc[3]; /* DC value for each component */
97  int adc[3]; /* DC adjusted value for each component */
98  uint8_t acrle; /* RLE value for current AC value */
99  uint8_t accrle; /* Accumulative RLE value */
100  uint16_t dri; /* Reset interval */
101  enum {
102  S_ENCODING = 0,
103  S_DECODING,
104  } mode;
105  uint32_t reset_mcu; /* MCU block to do absolute encoding */
106  char needbits; /* Number of bits needed to decode integer */
107 
108  /* The input huffman and quantisation tables */
109  uint8_t stbls[TBL_LEN + HBUFF_LEN];
110  uint8_t *sdht[2][2], *sdqt[2];
111  uint16_t stbl_len;
112 
113  /* The same for output */
114  uint8_t dtbls[TBL_LEN];
115  uint8_t *ddht[2][2], *ddqt[2];
116  uint16_t dtbl_len;
117 
118 } ssdv_t;
119 
120 typedef struct {
121  uint32_t callsign;
122  uint8_t image_id;
123  uint16_t packet_id;
124  uint16_t width;
125  uint16_t height;
126  uint16_t mcu_mode;
127  uint8_t mcu_offset;
128  uint16_t mcu_id;
129  uint16_t mcu_count;
131 
132 /* Encoding */
133 extern char ssdv_enc_init(ssdv_t *s, const char *callsign, uint8_t image_id);
134 extern char ssdv_enc_set_buffer(ssdv_t *s, uint8_t *buffer);
135 extern char ssdv_enc_get_packet(ssdv_t *s);
136 extern char ssdv_enc_feed(ssdv_t *s, uint8_t *buffer, size_t length);
137 
138 /* Decoding */
139 extern char ssdv_dec_init(ssdv_t *s);
140 extern char ssdv_dec_set_buffer(ssdv_t *s, uint8_t *buffer, size_t length);
141 extern char ssdv_dec_feed(ssdv_t *s, uint8_t *packet);
142 extern char ssdv_dec_get_jpeg(ssdv_t *s, uint8_t **jpeg, size_t *length);
143 
144 extern char ssdv_dec_is_packet(uint8_t *packet, int *errors);
145 extern void ssdv_dec_header(ssdv_packet_info_t *info, uint8_t *packet);
146 extern char * ssdv_dec_callsign(char *callsign, uint32_t code);
147 
148 #ifdef __cplusplus
149 }
150 #endif
151 #endif
152 
Definition: ssdv.h:46