From 9e1bf2fdea82fbeb156f6a3087d3f81b553315d7 Mon Sep 17 00:00:00 2001 From: Devaev Maxim Date: Wed, 9 Dec 2020 19:06:14 +0300 Subject: [PATCH] using uint8_t --- src/rawsink/rawsink.c | 2 +- src/rawsink/rawsink.h | 15 +++++++------ src/ustreamer/data/blank_jpeg.c | 2 +- src/ustreamer/data/blank_jpeg.h | 4 +++- src/ustreamer/device.h | 15 +++++++------ src/ustreamer/encoders/cpu/encoder.c | 32 ++++++++++++++-------------- src/ustreamer/encoders/cpu/encoder.h | 1 + src/ustreamer/encoders/hw/encoder.c | 12 +++++------ src/ustreamer/encoders/hw/encoder.h | 1 + src/ustreamer/encoders/hw/huffman.h | 5 ++++- src/ustreamer/frame.c | 4 ++-- src/ustreamer/frame.h | 23 ++++++++++---------- src/ustreamer/http/base64.c | 8 +++---- src/ustreamer/http/base64.h | 3 ++- src/ustreamer/http/server.c | 2 +- src/ustreamer/http/server.h | 1 + tools/make-jpeg-h.py | 2 +- 17 files changed, 72 insertions(+), 60 deletions(-) diff --git a/src/rawsink/rawsink.c b/src/rawsink/rawsink.c index ad28e5f..1957281 100644 --- a/src/rawsink/rawsink.c +++ b/src/rawsink/rawsink.c @@ -142,7 +142,7 @@ void rawsink_destroy(struct rawsink_t *rawsink) { void rawsink_put( struct rawsink_t *rawsink, - const unsigned char *data, size_t size, + const uint8_t *data, size_t size, unsigned format, unsigned width, unsigned height, long double grab_ts) { diff --git a/src/rawsink/rawsink.h b/src/rawsink/rawsink.h index 2f4c575..0120dcf 100644 --- a/src/rawsink/rawsink.h +++ b/src/rawsink/rawsink.h @@ -22,6 +22,7 @@ #pragma once +#include #include #include #include @@ -44,12 +45,12 @@ struct rawsink_shared_t { - unsigned format; - unsigned width; - unsigned height; - long double grab_ts; - size_t size; - unsigned char data[RAWSINK_MAX_DATA]; + unsigned format; + unsigned width; + unsigned height; + long double grab_ts; + size_t size; + uint8_t data[RAWSINK_MAX_DATA]; }; struct rawsink_t { @@ -75,7 +76,7 @@ void rawsink_destroy(struct rawsink_t *rawsink); void rawsink_put( struct rawsink_t *rawsink, - const unsigned char *data, size_t size, + const uint8_t *data, size_t size, unsigned format, unsigned witdh, unsigned height, long double grab_ts); diff --git a/src/ustreamer/data/blank_jpeg.c b/src/ustreamer/data/blank_jpeg.c index 78a408d..6863c3d 100644 --- a/src/ustreamer/data/blank_jpeg.c +++ b/src/ustreamer/data/blank_jpeg.c @@ -26,7 +26,7 @@ const unsigned BLANK_JPEG_WIDTH = 640; const unsigned BLANK_JPEG_HEIGHT = 480; const size_t BLANK_JPEG_DATA_SIZE = 13845; -const unsigned char BLANK_JPEG_DATA[] = { +const uint8_t BLANK_JPEG_DATA[] = { 0xFF, 0xD8, 0xFF, 0xE1, 0x09, 0x50, 0x68, 0x74, 0x74, 0x70, 0x3A, 0x2F, 0x2F, 0x6E, 0x73, 0x2E, 0x61, 0x64, 0x6F, 0x62, 0x65, 0x2E, 0x63, 0x6F, 0x6D, 0x2F, 0x78, 0x61, 0x70, 0x2F, 0x31, 0x2E, 0x30, 0x2F, 0x00, 0x3C, 0x3F, 0x78, 0x70, 0x61, 0x63, 0x6B, 0x65, 0x74, 0x20, 0x62, 0x65, 0x67, 0x69, 0x6E, 0x3D, 0x22, 0xEF, 0xBB, 0xBF, 0x22, 0x20, 0x69, 0x64, 0x3D, diff --git a/src/ustreamer/data/blank_jpeg.h b/src/ustreamer/data/blank_jpeg.h index 55aab3f..0ca47c3 100644 --- a/src/ustreamer/data/blank_jpeg.h +++ b/src/ustreamer/data/blank_jpeg.h @@ -22,6 +22,8 @@ #pragma once +#include + #include @@ -29,4 +31,4 @@ extern const unsigned BLANK_JPEG_WIDTH; extern const unsigned BLANK_JPEG_HEIGHT; extern const size_t BLANK_JPEG_DATA_SIZE; -extern const unsigned char BLANK_JPEG_DATA[]; +extern const uint8_t BLANK_JPEG_DATA[]; diff --git a/src/ustreamer/device.h b/src/ustreamer/device.h index 1d377a6..8f96604 100644 --- a/src/ustreamer/device.h +++ b/src/ustreamer/device.h @@ -24,6 +24,7 @@ #include #include +#include #include #include #include @@ -70,13 +71,13 @@ struct hw_buffer_t { - unsigned char *data; - size_t used; - size_t allocated; - unsigned width; - unsigned height; - unsigned format; - long double grab_ts; + uint8_t *data; + size_t used; + size_t allocated; + unsigned width; + unsigned height; + unsigned format; + long double grab_ts; struct v4l2_buffer buf_info; diff --git a/src/ustreamer/encoders/cpu/encoder.c b/src/ustreamer/encoders/cpu/encoder.c index b9100c3..bbb1809 100644 --- a/src/ustreamer/encoders/cpu/encoder.c +++ b/src/ustreamer/encoders/cpu/encoder.c @@ -38,19 +38,19 @@ struct _jpeg_dest_manager_t { static void _jpeg_set_picture(j_compress_ptr jpeg, struct frame_t *frame); static void _jpeg_write_scanlines_yuyv( - struct jpeg_compress_struct *jpeg, const unsigned char *data, + struct jpeg_compress_struct *jpeg, const uint8_t *data, unsigned width, unsigned height); static void _jpeg_write_scanlines_uyvy( - struct jpeg_compress_struct *jpeg, const unsigned char *data, + struct jpeg_compress_struct *jpeg, const uint8_t *data, unsigned width, unsigned height); static void _jpeg_write_scanlines_rgb565( - struct jpeg_compress_struct *jpeg, const unsigned char *data, + struct jpeg_compress_struct *jpeg, const uint8_t *data, unsigned width, unsigned height); static void _jpeg_write_scanlines_rgb24( - struct jpeg_compress_struct *jpeg, const unsigned char *data, + struct jpeg_compress_struct *jpeg, const uint8_t *data, unsigned width, unsigned height); static void _jpeg_init_destination(j_compress_ptr jpeg); @@ -123,17 +123,17 @@ static void _jpeg_set_picture(j_compress_ptr jpeg, struct frame_t *frame) { #define NORM_COMPONENT(_x) (((_x) > 255) ? 255 : (((_x) < 0) ? 0 : (_x))) static void _jpeg_write_scanlines_yuyv( - struct jpeg_compress_struct *jpeg, const unsigned char *data, + struct jpeg_compress_struct *jpeg, const uint8_t *data, unsigned width, unsigned height) { - unsigned char *line_buffer; + uint8_t *line_buffer; JSAMPROW scanlines[1]; unsigned z = 0; A_CALLOC(line_buffer, width * 3); while (jpeg->next_scanline < height) { - unsigned char *ptr = line_buffer; + uint8_t *ptr = line_buffer; for (unsigned x = 0; x < width; ++x) { int y = (!z ? data[0] << 8 : data[2] << 8); @@ -162,17 +162,17 @@ static void _jpeg_write_scanlines_yuyv( } static void _jpeg_write_scanlines_uyvy( - struct jpeg_compress_struct *jpeg, const unsigned char *data, + struct jpeg_compress_struct *jpeg, const uint8_t *data, unsigned width, unsigned height) { - unsigned char *line_buffer; + uint8_t *line_buffer; JSAMPROW scanlines[1]; unsigned z = 0; A_CALLOC(line_buffer, width * 3); while (jpeg->next_scanline < height) { - unsigned char *ptr = line_buffer; + uint8_t *ptr = line_buffer; for(unsigned x = 0; x < width; ++x) { int y = (!z ? data[1] << 8 : data[3] << 8); @@ -206,22 +206,22 @@ static void _jpeg_write_scanlines_uyvy( #undef YUV_R static void _jpeg_write_scanlines_rgb565( - struct jpeg_compress_struct *jpeg, const unsigned char *data, + struct jpeg_compress_struct *jpeg, const uint8_t *data, unsigned width, unsigned height) { - unsigned char *line_buffer; + uint8_t *line_buffer; JSAMPROW scanlines[1]; A_CALLOC(line_buffer, width * 3); while (jpeg->next_scanline < height) { - unsigned char *ptr = line_buffer; + uint8_t *ptr = line_buffer; for(unsigned x = 0; x < width; ++x) { unsigned int two_byte = (data[1] << 8) + data[0]; *(ptr++) = data[1] & 248; // Red - *(ptr++) = (unsigned char)((two_byte & 2016) >> 3); // Green + *(ptr++) = (uint8_t)((two_byte & 2016) >> 3); // Green *(ptr++) = (data[0] & 31) * 8; // Blue data += 2; @@ -235,13 +235,13 @@ static void _jpeg_write_scanlines_rgb565( } static void _jpeg_write_scanlines_rgb24( - struct jpeg_compress_struct *jpeg, const unsigned char *data, + struct jpeg_compress_struct *jpeg, const uint8_t *data, unsigned width, unsigned height) { JSAMPROW scanlines[1]; while (jpeg->next_scanline < height) { - scanlines[0] = (unsigned char *)(data + jpeg->next_scanline * width * 3); + scanlines[0] = (uint8_t *)(data + jpeg->next_scanline * width * 3); jpeg_write_scanlines(jpeg, scanlines, 1); } } diff --git a/src/ustreamer/encoders/cpu/encoder.h b/src/ustreamer/encoders/cpu/encoder.h index d1f0e87..9836b54 100644 --- a/src/ustreamer/encoders/cpu/encoder.h +++ b/src/ustreamer/encoders/cpu/encoder.h @@ -24,6 +24,7 @@ #include #include +#include #include #include diff --git a/src/ustreamer/encoders/hw/encoder.c b/src/ustreamer/encoders/hw/encoder.c index cf938b3..cf1cb13 100644 --- a/src/ustreamer/encoders/hw/encoder.c +++ b/src/ustreamer/encoders/hw/encoder.c @@ -29,7 +29,7 @@ void _copy_plus_huffman(const struct hw_buffer_t *src, struct frame_t *dest); -static bool _is_huffman(const unsigned char *data); +static bool _is_huffman(const uint8_t *data); int hw_encoder_prepare(struct device_t *dev, unsigned quality) { @@ -58,8 +58,8 @@ void hw_encoder_compress_buffer(struct hw_buffer_t *hw, struct frame_t *frame) { void _copy_plus_huffman(const struct hw_buffer_t *src, struct frame_t *dest) { if (!_is_huffman(src->data)) { - const unsigned char *src_ptr = src->data; - const unsigned char *src_end = src->data + src->used; + const uint8_t *src_ptr = src->data; + const uint8_t *src_end = src->data + src->used; size_t paste; while ((((src_ptr[0] << 8) | src_ptr[1]) != 0xFFC0) && (src_ptr < src_end)) { @@ -79,14 +79,14 @@ void _copy_plus_huffman(const struct hw_buffer_t *src, struct frame_t *dest) { } } -static bool _is_huffman(const unsigned char *data) { +static bool _is_huffman(const uint8_t *data) { unsigned count = 0; - while (((data[0] << 8) | data[1]) != 0xFFDA) { + while ((((uint16_t)data[0] << 8) | data[1]) != 0xFFDA) { if (count++ > 2048) { return false; } - if (((data[0] << 8) | data[1]) == 0xFFC4) { + if ((((uint16_t)data[0] << 8) | data[1]) == 0xFFC4) { return true; } data += 1; diff --git a/src/ustreamer/encoders/hw/encoder.h b/src/ustreamer/encoders/hw/encoder.h index 436f1a8..2987fd7 100644 --- a/src/ustreamer/encoders/hw/encoder.h +++ b/src/ustreamer/encoders/hw/encoder.h @@ -22,6 +22,7 @@ #pragma once +#include #include #include #include diff --git a/src/ustreamer/encoders/hw/huffman.h b/src/ustreamer/encoders/hw/huffman.h index 1cef0cb..8f89d7c 100644 --- a/src/ustreamer/encoders/hw/huffman.h +++ b/src/ustreamer/encoders/hw/huffman.h @@ -27,7 +27,10 @@ #pragma once -static const unsigned char HUFFMAN_TABLE[] = { +#include + + +static const uint8_t HUFFMAN_TABLE[] = { 0xFF, 0xC4, 0x01, 0xA2, 0x00, 0x00, 0x01, 0x05, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0A, 0x0B, 0x01, 0x00, 0x03, diff --git a/src/ustreamer/frame.c b/src/ustreamer/frame.c index 45a413f..8d7b6ff 100644 --- a/src/ustreamer/frame.c +++ b/src/ustreamer/frame.c @@ -48,13 +48,13 @@ void frame_realloc_data(struct frame_t *frame, size_t size) { } } -void frame_set_data(struct frame_t *frame, const unsigned char *data, size_t size) { +void frame_set_data(struct frame_t *frame, const uint8_t *data, size_t size) { frame_realloc_data(frame, size); memcpy(frame->data, data, size); frame->used = size; } -void frame_append_data(struct frame_t *frame, const unsigned char *data, size_t size) { +void frame_append_data(struct frame_t *frame, const uint8_t *data, size_t size) { size_t new_used = frame->used + size; frame_realloc_data(frame, new_used); diff --git a/src/ustreamer/frame.h b/src/ustreamer/frame.h index 162eee4..c9ebec0 100644 --- a/src/ustreamer/frame.h +++ b/src/ustreamer/frame.h @@ -23,6 +23,7 @@ #pragma once #include +#include #include #include #include @@ -33,15 +34,15 @@ struct frame_t { - const char *role; - unsigned char *data; - size_t used; - size_t allocated; - unsigned width; - unsigned height; - long double grab_ts; - long double encode_begin_ts; - long double encode_end_ts; + const char *role; + uint8_t *data; + size_t used; + size_t allocated; + unsigned width; + unsigned height; + long double grab_ts; + long double encode_begin_ts; + long double encode_end_ts; }; @@ -49,8 +50,8 @@ struct frame_t *frame_init(const char *role); void frame_destroy(struct frame_t *frame); void frame_realloc_data(struct frame_t *frame, size_t size); -void frame_set_data(struct frame_t *frame, const unsigned char *data, size_t size); -void frame_append_data(struct frame_t *frame, const unsigned char *data, size_t size); +void frame_set_data(struct frame_t *frame, const uint8_t *data, size_t size); +void frame_append_data(struct frame_t *frame, const uint8_t *data, size_t size); void frame_copy(const struct frame_t *src, struct frame_t *dest); bool frame_compare(const struct frame_t *a, const struct frame_t *b); diff --git a/src/ustreamer/http/base64.c b/src/ustreamer/http/base64.c index b5fcb6f..49ad5d0 100644 --- a/src/ustreamer/http/base64.c +++ b/src/ustreamer/http/base64.c @@ -37,7 +37,7 @@ static const char _ENCODING_TABLE[] = { static const unsigned _MOD_TABLE[] = {0, 2, 1}; -char *base64_encode(const unsigned char *str) { +char *base64_encode(const uint8_t *str) { size_t str_len = strlen((const char *)str); size_t encoded_size = 4 * ((str_len + 2) / 3) + 1; // +1 for '\0' char *encoded; @@ -45,9 +45,9 @@ char *base64_encode(const unsigned char *str) { A_CALLOC(encoded, encoded_size); for (unsigned str_index = 0, encoded_index = 0; str_index < str_len;) { - unsigned octet_a = (str_index < str_len ? (unsigned char)str[str_index++] : 0); - unsigned octet_b = (str_index < str_len ? (unsigned char)str[str_index++] : 0); - unsigned octet_c = (str_index < str_len ? (unsigned char)str[str_index++] : 0); + unsigned octet_a = (str_index < str_len ? (uint8_t)str[str_index++] : 0); + unsigned octet_b = (str_index < str_len ? (uint8_t)str[str_index++] : 0); + unsigned octet_c = (str_index < str_len ? (uint8_t)str[str_index++] : 0); unsigned triple = (octet_a << 0x10) + (octet_b << 0x08) + octet_c; diff --git a/src/ustreamer/http/base64.h b/src/ustreamer/http/base64.h index cc57136..8d5febd 100644 --- a/src/ustreamer/http/base64.h +++ b/src/ustreamer/http/base64.h @@ -23,9 +23,10 @@ #pragma once #include +#include #include #include "../../common/tools.h" -char *base64_encode(const unsigned char *str); +char *base64_encode(const uint8_t *str); diff --git a/src/ustreamer/http/server.c b/src/ustreamer/http/server.c index 8bbe134..ca6463e 100644 --- a/src/ustreamer/http/server.c +++ b/src/ustreamer/http/server.c @@ -157,7 +157,7 @@ int http_server_listen(struct http_server_t *server) { A_CALLOC(raw_token, strlen(server->user) + strlen(server->passwd) + 2); sprintf(raw_token, "%s:%s", server->user, server->passwd); - encoded_token = base64_encode((unsigned char *)raw_token); + encoded_token = base64_encode((uint8_t *)raw_token); free(raw_token); A_CALLOC(RUN(auth_token), strlen(encoded_token) + 16); diff --git a/src/ustreamer/http/server.h b/src/ustreamer/http/server.h index 3aa5039..5eb0642 100644 --- a/src/ustreamer/http/server.h +++ b/src/ustreamer/http/server.h @@ -24,6 +24,7 @@ #include #include +#include #include #include #include diff --git a/tools/make-jpeg-h.py b/tools/make-jpeg-h.py index 06b5329..ea50723 100755 --- a/tools/make-jpeg-h.py +++ b/tools/make-jpeg-h.py @@ -85,7 +85,7 @@ def main() -> None: text += f"const unsigned {name}_JPEG_WIDTH = {width};\n" text += f"const unsigned {name}_JPEG_HEIGHT = {height};\n\n" text += f"const size_t {name}_JPEG_DATA_SIZE = {len(jpeg_data)};\n" - text += f"const unsigned char {name}_JPEG_DATA[] = {jpeg_data_text};\n" + text += f"const uint8_t {name}_JPEG_DATA[] = {jpeg_data_text};\n" with open(c_path, "w") as c_file: c_file.write(text)