[dvblast-devel] [PATCH 1/5] dvb/si: Complete support for EIT.

Georgi Chorbadzhiyski gf at unixsol.org
Mon Sep 19 19:57:24 CEST 2011


---
 dvb/si.h                |  121 ++++++++++++++++++++++++++++++++++++++++++++++-
 dvb/si_print.h          |  113 +++++++++++++++++++++++++++++++++++++++++++
 examples/dvb_print_si.c |    2 +
 3 files changed, 234 insertions(+), 2 deletions(-)

diff --git a/dvb/si.h b/dvb/si.h
index e87559b..bb88112 100644
--- a/dvb/si.h
+++ b/dvb/si.h
@@ -1843,11 +1843,130 @@ static inline uint16_t eit_get_tsid(const uint8_t *p_eit)
     return (p_eit[8] << 8) | p_eit[9];
 }
 
+static inline void eit_set_org_nid(uint8_t *p_eit, uint16_t i_org_nid)
+{
+    p_eit[10] = i_org_nid >> 8;
+    p_eit[11] = i_org_nid & 0xff;
+}
+
+static inline uint16_t eit_get_org_nid(const uint8_t *p_eit)
+{
+    return (p_eit[10] << 8) | p_eit[11];
+}
+
+static inline void eit_set_segment_last_sec_number(uint8_t *p_eit, uint8_t i_segment_last_sec_number)
+{
+    p_eit[12] = i_segment_last_sec_number;
+}
+
+static inline uint8_t eit_get_segment_last_sec_number(const uint8_t *p_eit)
+{
+    return p_eit[12];
+}
+
+static inline void eit_set_last_table_id(uint8_t *p_eit, uint8_t i_last_table_id)
+{
+    p_eit[13] = i_last_table_id;
+}
+
+static inline uint8_t eit_get_last_table_id(const uint8_t *p_eit)
+{
+    return p_eit[13];
+}
+
+static inline uint16_t eitn_get_event_id(const uint8_t *p_eit_n)
+{
+    return (p_eit_n[0] << 8) | p_eit_n[1];
+}
+
+static inline void eitn_set_event_id(uint8_t *p_eit, uint16_t i_event_id)
+{
+    p_eit[0] = i_event_id >> 8;
+    p_eit[1] = i_event_id & 0xff;
+}
+
+static inline uint16_t eitn_get_start_time_mjd(const uint8_t *p_eit_n)
+{
+    return (p_eit_n[2] << 8) | p_eit_n[3];
+}
+
+static inline void eitn_set_start_time_mjd(uint8_t *p_eit, uint16_t i_start_time_mjd)
+{
+    p_eit[2] = i_start_time_mjd >> 8;
+    p_eit[3] = i_start_time_mjd & 0xff;
+}
+
+static inline uint32_t eitn_get_start_time_bcd(const uint8_t *p_eit_n)
+{
+    return ((p_eit_n[4] << 16) | (p_eit_n[5] << 8)) | p_eit_n[6];
+}
+
+static inline void eitn_set_start_time_bcd(uint8_t *p_eit_n, uint32_t i_start_time_bcd)
+{
+    p_eit_n[4] = i_start_time_bcd >> 16;
+    p_eit_n[5] = (i_start_time_bcd >> 8) & 0xff;
+    p_eit_n[6] = i_start_time_bcd & 0xff;
+}
+
+static inline uint32_t eitn_get_duration_bcd(const uint8_t *p_eit_n)
+{
+    return ((p_eit_n[7] << 16) | (p_eit_n[8] << 8)) | p_eit_n[9];
+}
+
+static inline void eitn_set_duration_bcd(uint8_t *p_eit_n, uint32_t i_duration_bcd)
+{
+    p_eit_n[7] = i_duration_bcd >> 16;
+    p_eit_n[8] = (i_duration_bcd >> 8) & 0xff;
+    p_eit_n[9] = i_duration_bcd & 0xff;
+}
+
+static inline uint8_t eitn_get_running_status(const uint8_t *p_eit_n)
+{
+    return p_eit_n[10] >> 5; // 111xxxxx
+}
+
+static inline void eitn_set_running_status(uint8_t *p_eit_n, uint8_t i_running_status)
+{
+    p_eit_n[10] |= i_running_status << 5;
+}
+
+static inline bool eitn_get_free_CA_mode(const uint8_t *p_eit_n)
+{
+    return (p_eit_n[10] & 0x10) == 0x10; // xxx1xxxx
+}
+
+static inline void eitn_set_free_CA_mode(uint8_t *p_eit_n, bool b_free_CA_mode)
+{
+    p_eit_n[10] |= b_free_CA_mode << 4;
+}
+
 static inline uint16_t eitn_get_desclength(const uint8_t *p_eit_n)
 {
     return ((p_eit_n[10] & 0xf) << 8) | p_eit_n[11];
 }
 
+static inline uint8_t *eitn_get_descs(uint8_t *p_eit_n)
+{
+    return &p_eit_n[10];
+}
+
+static inline uint8_t *eit_get_event(uint8_t *p_eit, uint8_t n)
+{
+    uint16_t i_section_size = psi_get_length(p_eit) + PSI_HEADER_SIZE
+                               - PSI_CRC_SIZE;
+    uint8_t *p_eit_n = p_eit + EIT_HEADER_SIZE;
+    if (p_eit_n - p_eit > i_section_size) return NULL;
+
+    while (n) {
+        if (p_eit_n + EIT_EVENT_SIZE - p_eit > i_section_size) return NULL;
+        p_eit_n += EIT_EVENT_SIZE + eitn_get_desclength(p_eit_n);
+        n--;
+    }
+    if (p_eit_n - p_eit >= i_section_size) return NULL;
+
+    return p_eit_n;
+}
+
 static inline bool eit_validate_event(const uint8_t *p_eit,
                                       const uint8_t *p_eit_n,
                                       uint16_t i_desclength)
@@ -1891,8 +2010,6 @@ static inline bool eit_validate(const uint8_t *p_eit)
     return (p_eit_n - p_eit == i_section_size);
 }
 
-/* TODO: unfinished support */
-
 /*****************************************************************************
  * Running Status Table
  *****************************************************************************/
diff --git a/dvb/si_print.h b/dvb/si_print.h
index 6791830..058f332 100644
--- a/dvb/si_print.h
+++ b/dvb/si_print.h
@@ -20,6 +20,8 @@
 #ifndef __BITSTREAM_DVB_SI_PRINT_H__
 #define __BITSTREAM_DVB_SI_PRINT_H__
 
+#include <time.h>
+
 #include <bitstream/common.h>
 #include <bitstream/mpeg/psi.h>
 #include <bitstream/dvb/si.h>
@@ -352,6 +354,117 @@ static inline void sdt_table_print(uint8_t **pp_sections,
     }
 }
 
+/*****************************************************************************
+ * Event Information Table
+ *****************************************************************************/
+static inline void eit_print(uint8_t *p_eit,
+                             f_print pf_print, void *print_opaque,
+                             f_iconv pf_iconv, void *iconv_opaque,
+                             print_type_t i_print_type)
+{
+    uint8_t *p_event;
+    uint8_t j = 0;
+
+    switch (i_print_type) {
+    case PRINT_XML:
+        pf_print(print_opaque, "<EIT tableid=\"0x%02x\" version=\"%u\" current_next=\"%u\" tsid=\"%u\" org_nid=\"%u\" seg_last_sec_number=\"%u\" last_table_id=\"0x%02x\">",
+                 psi_get_tableid(p_eit),
+                 psi_get_version(p_eit),
+                 !psi_get_current(p_eit) ? 0 : 1,
+                 eit_get_tsid(p_eit),
+                 eit_get_org_nid(p_eit),
+                 eit_get_segment_last_sec_number(p_eit),
+                 eit_get_last_table_id(p_eit)
+                );
+        break;
+    default:
+        pf_print(print_opaque, "new EIT tableid=0x%02x version=%u%s tsid=%u org_nid=%u seg_last_sec_number=%u last_table_id=0x%02x",
+                 psi_get_tableid(p_eit),
+                 psi_get_version(p_eit),
+                 !psi_get_current(p_eit) ? " (next)" : "",
+                 eit_get_tsid(p_eit),
+                 eit_get_org_nid(p_eit),
+                 eit_get_segment_last_sec_number(p_eit),
+                 eit_get_last_table_id(p_eit)
+                );
+    }
+
+    while ((p_event = eit_get_event(p_eit, j)) != NULL) {
+        j++;
+        time_t start_ts;
+        unsigned int duration_sec;
+        struct tm tm = { 0, 0, 0, 0, 0, 0, 0, 0, 0 };
+        char start_str[32], duration_str[16];
+        int year, month, day, hour, min, sec;
+        uint32_t start_time_bcd = eitn_get_start_time_bcd(p_event);
+        uint32_t duration_bcd = eitn_get_duration_bcd(p_event);
+
+#define bcd2dec(__bcd) (int)((((__bcd) >> 4) * 10) + (__bcd) % 16)
+        dvb_mjd_get(eitn_get_start_time_mjd(p_event), &year, &month, &day);
+        hour = bcd2dec( (start_time_bcd >> 16) & 0xff );
+        min  = bcd2dec( (start_time_bcd >> 8 ) & 0xff );
+        sec  = bcd2dec(  start_time_bcd        & 0xff );
+        sprintf(start_str, "%04d-%02d-%02d_%02d:%02d:%02d_UTC",
+            year + 1900, month, day, hour, min, sec);
+
+        tm.tm_year = year;
+        tm.tm_mon  = month - 1;
+        tm.tm_mday = day;
+        tm.tm_hour = hour;
+        tm.tm_min  = min;
+        tm.tm_sec  = sec;
+        start_ts = timegm(&tm);
+
+        hour = bcd2dec( (duration_bcd >> 16) & 0xff );
+        min  = bcd2dec( (duration_bcd >> 8 ) & 0xff );
+        sec  = bcd2dec(  duration_bcd        & 0xff );
+        duration_sec = hour * 3600 + min * 60 + sec;
+        sprintf(duration_str, "%02d:%02d:%02d", hour, min, sec);
+#undef bcd2dec
+
+        switch (i_print_type) {
+        case PRINT_XML:
+            pf_print(print_opaque, "<EVENT id=\"%u\" start_time=\"%ld\" start_time_dec=\"%s\""
+                                   " duration=\"%u\" duration_dec=\"%s\""
+                                   " running_status=\"%d\" free_CA_mode=\"%d\">",
+                     eitn_get_event_id(p_event),
+                     start_ts, start_str,
+                     duration_sec, duration_str,
+                     eitn_get_running_status(p_event),
+                     eitn_get_free_CA_mode(p_event)
+                    );
+            break;
+        default:
+            pf_print(print_opaque, "  * EVENT id=%u start_time=%ld start_time_dec=%s duration=%u duration_dec=%s running_status=%d free_CA_mode=%d",
+                     eitn_get_event_id(p_event),
+                     start_ts, start_str,
+                     duration_sec, duration_str,
+                     eitn_get_running_status(p_event),
+                     eitn_get_free_CA_mode(p_event)
+                    );
+        }
+
+        descs_print(eitn_get_descs(p_event), pf_print, print_opaque,
+                    pf_iconv, iconv_opaque, i_print_type);
+
+        switch (i_print_type) {
+        case PRINT_XML:
+            pf_print(print_opaque, "</EVENT>");
+            break;
+        default:
+            break;
+        }
+    }
+
+    switch (i_print_type) {
+    case PRINT_XML:
+        pf_print(print_opaque, "</EIT>");
+        break;
+    default:
+        pf_print(print_opaque, "end EIT");
+    }
+}
+
 #ifdef __cplusplus
 }
 #endif
diff --git a/examples/dvb_print_si.c b/examples/dvb_print_si.c
index af99999..a101cf1 100644
--- a/examples/dvb_print_si.c
+++ b/examples/dvb_print_si.c
@@ -526,6 +526,8 @@ static void handle_eit_section(uint16_t i_pid, uint8_t *p_eit)
         return;
     }
 
+    eit_print(p_eit, print_wrapper, NULL, iconv_wrapper, NULL, i_print_type);
+
     free(p_eit);
 }
 
-- 
1.7.5.1



More information about the dvblast-devel mailing list