diff --git a/README b/README index de6a536..d49fa11 100644 --- a/README +++ b/README @@ -85,6 +85,7 @@ Supported MPEG descriptors * Descriptor 0x1e: SL_descriptor * Descriptor 0x1f: FMC_descriptor * Descriptor 0x20: External ES_ID descriptor + * Descriptor 0x21: MuxCode descriptor * Descriptor 0x23: MultiplexBuffer descriptor * Descriptor 0x27: Metadata STD descriptor * Descriptor 0x28: AVC video descriptor diff --git a/TODO b/TODO index 552fbed..4661e68 100644 --- a/TODO +++ b/TODO @@ -2,7 +2,6 @@ TODO items for biTStream. The items are not ordered by importance so if you like something just do it and send a patch. - Add support (parser, generator, example) for these MPEG descriptors: - - Descriptor 0x21 MuxCode_descriptor - Descriptor 0x22 FmxBufferSize_descriptor - Descriptor 0x24 content_labeling_descriptor - Descriptor 0x25 metadata_pointer_descriptor diff --git a/examples/dvb_gen_si.c b/examples/dvb_gen_si.c index 15ef74a..e1812e8 100644 --- a/examples/dvb_gen_si.c +++ b/examples/dvb_gen_si.c @@ -281,6 +281,90 @@ static void build_desc20(uint8_t *desc) { desc20_set_external_es_id(desc, 0x1234); } +/* MPEG Descriptor 0x21: MuxCode descriptor */ +static void build_desc21(uint8_t *desc) { + uint8_t n = 0, k = 0, r; + uint8_t *entry_n, *entry_k; + + desc21_init(desc); + desc_set_length(desc, 255); + + entry_n = desc21_get_entry(desc, n++); + desc21n_set_muxcode(entry_n, 1); + desc21n_set_version(entry_n, 1); + desc21n_set_length(entry_n, DESC21_ENTRY_HEADER_SIZE - 1); + desc21n_set_substruct_count(entry_n, 0); + + entry_n = desc21_get_entry(desc, n++); + desc21n_set_muxcode(entry_n, 2); + desc21n_set_version(entry_n, 2); + desc21n_set_length(entry_n, 255); + desc21n_set_substruct_count(entry_n, 255); + { + k = 0; + entry_k = desc21n_get_substruct(entry_n, k++); + desc21k_set_repetition_count(entry_k, 1); + desc21k_set_slot_count(entry_k, 0); + + entry_k = desc21n_get_substruct(entry_n, k++); + desc21k_set_repetition_count(entry_k, 5); + desc21k_set_slot_count(entry_k, 5); + for (r=0; r + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/mpeg/psi/desc_21.h b/mpeg/psi/desc_21.h new file mode 100644 index 0000000..40f6f6a --- /dev/null +++ b/mpeg/psi/desc_21.h @@ -0,0 +1,283 @@ +/***************************************************************************** + * desc_21.h: ISO/IEC 13818-1 Descriptor 0x21 (Muxcode descriptor) + ***************************************************************************** + * Copyright (C) 2021 Unix Solutions Ltd. + * + * Authors: Georgi Chorbadzhiyski + * + * Permission is hereby granted, free of charge, to any person obtaining + * a copy of this software and associated documentation files (the + * "Software"), to deal in the Software without restriction, including + * without limitation the rights to use, copy, modify, merge, publish, + * distribute, sublicense, and/or sell copies of the Software, and to + * permit persons to whom the Software is furnished to do so, subject + * to the following conditions: + * + * The above copyright notice and this permission notice shall be + * included in all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, + * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF + * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. + * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY + * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, + * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE + * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + *****************************************************************************/ + +/* + * Normative references: + * - ISO/IEC 13818-1:2007(E) (MPEG-2 Systems) + * - ISO/IEC 14496-1:2001(E) (MPEG-4 Systems) + */ + +#ifndef __BITSTREAM_MPEG_DESC_21_H__ +#define __BITSTREAM_MPEG_DESC_21_H__ + +#include +#include + +#ifdef __cplusplus +extern "C" +{ +#endif + +/***************************************************************************** + * Descriptor 0x21: Muxcode descriptor + *****************************************************************************/ +#define DESC21_HEADER_SIZE DESC_HEADER_SIZE +#define DESC21_ENTRY_HEADER_SIZE 3 +#define DESC21_SUBSTRUCT_HEADER_SIZE 1 + +static inline void desc21_init(uint8_t *p_desc) +{ + desc_set_tag(p_desc, 0x21); +} + +static inline uint8_t desc21n_get_length(const uint8_t *p_desc_n) +{ + return p_desc_n[0]; +} + +static inline void desc21n_set_length(uint8_t *p_desc_n, uint8_t i_length) +{ + p_desc_n[0] = i_length; +} + +static inline uint8_t desc21n_get_muxcode(const uint8_t *p_desc_n) +{ + return (p_desc_n[1] & 0xf0) >> 4; +} + +static inline void desc21n_set_muxcode(uint8_t *p_desc_n, uint8_t i_muxcode) +{ + p_desc_n[1] = (i_muxcode << 4) | (p_desc_n[1] & 0x0f); +} + +static inline uint8_t desc21n_get_version(const uint8_t *p_desc_n) +{ + return p_desc_n[1] & 0x0f; +} + +static inline void desc21n_set_version(uint8_t *p_desc_n, uint8_t i_version) +{ + p_desc_n[1] = (p_desc_n[1] & 0xf0) | (i_version & 0x0f); +} + +static inline uint8_t desc21n_get_substruct_count(const uint8_t *p_desc_n) +{ + return p_desc_n[2]; +} + +static inline void desc21n_set_substruct_count(uint8_t *p_desc_n, uint8_t i_substruct_count) +{ + p_desc_n[2] = i_substruct_count; +} + +static inline uint8_t *desc21_get_entry(const uint8_t *p_desc, uint8_t n) +{ + const uint8_t *p_desc_n = p_desc + DESC21_HEADER_SIZE; + uint8_t i_desc_size = desc_get_length(p_desc); + + while (n) { + if (p_desc_n + desc21n_get_length(p_desc_n) - p_desc > i_desc_size) + return NULL; + p_desc_n += 1 + desc21n_get_length(p_desc_n); + n--; + } + if (p_desc_n - p_desc > i_desc_size) + return NULL; + return (uint8_t *)p_desc_n; +} + +static inline uint8_t desc21k_get_slot_count(const uint8_t *p_desc_k) +{ + return p_desc_k[0] >> 3; +} + +static inline void desc21k_set_slot_count(uint8_t *p_desc_k, uint8_t i_slot_count) +{ + p_desc_k[0] = (i_slot_count << 3) | (p_desc_k[0] & 0x07); +} + +static inline uint8_t desc21k_get_repetition_count(const uint8_t *p_desc_k) +{ + return p_desc_k[0] & 0x07; +} + +static inline void desc21k_set_repetition_count(uint8_t *p_desc_k, uint8_t i_repetition_count) +{ + p_desc_k[0] = (p_desc_k[0] & 0xf8) | (i_repetition_count & 0x07); +} + +static inline uint8_t desc21k_get_flex_mux_channel(const uint8_t *p_desc_k, uint8_t i_index) +{ + return p_desc_k[DESC21_SUBSTRUCT_HEADER_SIZE + i_index * 2]; +} + +static inline void desc21k_set_flex_mux_channel(uint8_t *p_desc_k, uint8_t i_index, uint8_t i_flex_mux_channel) +{ + p_desc_k[DESC21_SUBSTRUCT_HEADER_SIZE + i_index * 2] = i_flex_mux_channel; +} + +static inline uint8_t desc21k_get_number_of_bytes(const uint8_t *p_desc_k, uint8_t i_index) +{ + return p_desc_k[DESC21_SUBSTRUCT_HEADER_SIZE + i_index * 2 + 1]; +} + +static inline void desc21k_set_number_of_bytes(uint8_t *p_desc_k, uint8_t i_index, uint8_t i_number_of_bytes) +{ + p_desc_k[DESC21_SUBSTRUCT_HEADER_SIZE + i_index * 2 + 1] = i_number_of_bytes; +} + +static inline uint8_t *desc21n_get_substruct(const uint8_t *p_desc_n, uint8_t k) +{ + const uint8_t *p_desc_k = p_desc_n + DESC21_ENTRY_HEADER_SIZE; + uint8_t i_entry_size = desc21n_get_length(p_desc_n); + + if (desc21n_get_substruct_count(p_desc_n) == 0) + return NULL; + + if (k > desc21n_get_substruct_count(p_desc_n) - 1) + return NULL; + + while (k) { + if (p_desc_k - p_desc_n > i_entry_size) + return NULL; + p_desc_k += DESC21_SUBSTRUCT_HEADER_SIZE + (2 * desc21k_get_slot_count(p_desc_k)); + k--; + } + if (p_desc_n - p_desc_k > i_entry_size) + return NULL; + return (uint8_t *)p_desc_k; +} + +static inline bool desc21_validate(const uint8_t *p_desc) +{ + uint8_t j = 0; + uint8_t *p_desc_n; + int i_desc_length = desc_get_length(p_desc); + while ((p_desc_n = desc21_get_entry(p_desc, j++)) != NULL) { + int i_calc_entry_length = DESC21_ENTRY_HEADER_SIZE - 1; + uint8_t *p_desc_k; + uint8_t k = 0; + while ((p_desc_k = desc21n_get_substruct(p_desc_n, k++)) != NULL) { + i_calc_entry_length += DESC21_SUBSTRUCT_HEADER_SIZE + (2 * desc21k_get_slot_count(p_desc_k)); + } + if (i_calc_entry_length != desc21n_get_length(p_desc_n)) + return false; + i_desc_length -= desc21n_get_length(p_desc_n); + } + return i_desc_length >= 0; +} + +static inline void desc21_print(const uint8_t *p_desc, f_print pf_print, + void *opaque, print_type_t i_print_type) +{ + const uint8_t *p_desc_n; + uint8_t n = 0; + + switch (i_print_type) { + case PRINT_XML: + pf_print(opaque, ""); + break; + default: + pf_print(opaque, " - desc 21 muxcode"); + } + + while ((p_desc_n = desc21_get_entry(p_desc, n++)) != NULL) { + const uint8_t *p_desc_k; + uint8_t k = 0; + + switch (i_print_type) { + case PRINT_XML: + pf_print(opaque, + "", + desc21n_get_length(p_desc_n), + desc21n_get_muxcode(p_desc_n), + desc21n_get_version(p_desc_n), + desc21n_get_substruct_count(p_desc_n) + ); + break; + default: + pf_print(opaque, + " - muxcode_entry length=%u muxcode=%u" + " version=%u substruct_count=%u", + desc21n_get_length(p_desc_n), + desc21n_get_muxcode(p_desc_n), + desc21n_get_version(p_desc_n), + desc21n_get_substruct_count(p_desc_n) + ); + } + + while ((p_desc_k = desc21n_get_substruct(p_desc_n, k++)) != NULL) { + uint8_t i_slot_count = desc21k_get_slot_count(p_desc_k); + uint8_t i_repetition_count = desc21k_get_repetition_count(p_desc_k); + uint8_t r; + + switch (i_print_type) { + case PRINT_XML: + pf_print(opaque, + "", + i_slot_count, i_repetition_count); + break; + default: + pf_print(opaque, + " - muxcode_substruct slot_count=%u repetition_count=%u", + i_slot_count, i_repetition_count); + } + + for (r = 0; r < i_slot_count; r++) { + uint8_t i_flex_mux_channel = desc21k_get_flex_mux_channel(p_desc_k, r); + uint8_t i_number_of_bytes = desc21k_get_number_of_bytes(p_desc_k, r); + switch (i_print_type) { + case PRINT_XML: + pf_print(opaque, + "", + r, i_flex_mux_channel, i_number_of_bytes); + break; + default: + pf_print(opaque, + " - muxcode_substruct_entry slot=%u flex_mux_channel=%u number_of_bytes=%u", + r, i_flex_mux_channel, i_number_of_bytes); + } + } + + if (i_print_type == PRINT_XML) + pf_print(opaque, ""); + } + + if (i_print_type == PRINT_XML) + pf_print(opaque, ""); + } + + if (i_print_type == PRINT_XML) + pf_print(opaque, ""); +} + +#ifdef __cplusplus +} +#endif + +#endif diff --git a/mpeg/psi/descs_list.h b/mpeg/psi/descs_list.h index dce19e3..efd9913 100644 --- a/mpeg/psi/descs_list.h +++ b/mpeg/psi/descs_list.h @@ -57,6 +57,7 @@ #include #include #include +#include #include #include #include diff --git a/mpeg/psi/descs_print.h b/mpeg/psi/descs_print.h index f48498e..f2d4590 100644 --- a/mpeg/psi/descs_print.h +++ b/mpeg/psi/descs_print.h @@ -123,6 +123,7 @@ static inline void descl_print(uint8_t *p_descl, uint16_t i_length, CASE_DESC(1e) CASE_DESC(1f) CASE_DESC(20) + CASE_DESC(21) CASE_DESC(23) CASE_DESC(27) CASE_DESC(28)