Gong Ke | fa504d7 | 2023-04-18 10:23:35 +0800 | [diff] [blame] | 1 | /** |
| 2 | * \file |
| 3 | * TS indexer. |
| 4 | */ |
| 5 | |
| 6 | #ifndef _TS_INDEXER_H_ |
| 7 | #define _TS_INDEXER_H_ |
| 8 | |
| 9 | #include <inttypes.h> |
| 10 | |
| 11 | #ifdef __cplusplus |
| 12 | extern "C" { |
| 13 | #endif |
| 14 | |
| 15 | /**Video format*/ |
| 16 | typedef enum { |
| 17 | TS_INDEXER_VIDEO_FORMAT_MPEG2, /**< MPEG2*/ |
| 18 | TS_INDEXER_VIDEO_FORMAT_H264, /**< H264*/ |
| 19 | TS_INDEXER_VIDEO_FORMAT_HEVC /**< HEVC*/ |
| 20 | } TS_Indexer_StreamFormat_t; |
| 21 | |
| 22 | /**Event type.*/ |
| 23 | typedef enum { |
Yahui Han | 63de708 | 2023-07-12 18:30:10 +0800 | [diff] [blame^] | 24 | TS_INDEXER_EVENT_TYPE_START_INDICATOR, /**< TS start indicator.*/ |
| 25 | TS_INDEXER_EVENT_TYPE_DISCONTINUITY_INDICATOR, /**< TS discontinuity indicator.*/ |
| 26 | TS_INDEXER_EVENT_TYPE_MPEG2_I_FRAME, /**< MPEG2 I frame.*/ |
| 27 | TS_INDEXER_EVENT_TYPE_MPEG2_P_FRAME, /**< MPEG2 P frame.*/ |
| 28 | TS_INDEXER_EVENT_TYPE_MPEG2_B_FRAME, /**< MPEG2 B frame.*/ |
| 29 | TS_INDEXER_EVENT_TYPE_MPEG2_SEQUENCE, /**< MPEG2 Video Sequence header.*/ |
| 30 | TS_INDEXER_EVENT_TYPE_AVC_I_SLICE, /**< AVC I slice.*/ |
| 31 | TS_INDEXER_EVENT_TYPE_AVC_B_SLICE, /**< AVC B slice.*/ |
| 32 | TS_INDEXER_EVENT_TYPE_AVC_P_SLICE, /**< AVC P slice.*/ |
| 33 | TS_INDEXER_EVENT_TYPE_AVC_SI_SLICE, /**< AVC SI slice.*/ |
| 34 | TS_INDEXER_EVENT_TYPE_AVC_SP_SLICE, /**< AVC SP slice.*/ |
| 35 | TS_INDEXER_EVENT_TYPE_HEVC_SPS, /**< HEVC NAL unit type SPS_NUT.*/ |
| 36 | TS_INDEXER_EVENT_TYPE_HEVC_AUD, /**< HEVC NAL unit type AUD_NUT.*/ |
| 37 | TS_INDEXER_EVENT_TYPE_HEVC_BLA_W_LP, /**< HEVC NAL unit type BLA_W_LP.*/ |
| 38 | TS_INDEXER_EVENT_TYPE_HEVC_BLA_W_RADL, /**< HEVC NAL unit type BLA_W_RADL.*/ |
| 39 | TS_INDEXER_EVENT_TYPE_HEVC_IDR_W_RADL, /**< HEVC NAL unit type IDR_W_RADL.*/ |
| 40 | TS_INDEXER_EVENT_TYPE_HEVC_IDR_N_LP, /**< HEVC NAL unit type IDR_N_LP.*/ |
| 41 | TS_INDEXER_EVENT_TYPE_HEVC_TRAIL_CRA, /**< HEVC NAL unit type TRAIL_CRA.*/ |
| 42 | TS_INDEXER_EVENT_TYPE_VIDEO_PTS, /**< MEPG2/AVC/HEVC PTS.*/ |
| 43 | TS_INDEXER_EVENT_TYPE_AUDIO_PTS /**< Audio PTS.*/ |
Gong Ke | fa504d7 | 2023-04-18 10:23:35 +0800 | [diff] [blame] | 44 | } TS_Indexer_EventType_t; |
| 45 | |
| 46 | /**Stream Parser state.*/ |
| 47 | typedef enum { |
| 48 | TS_INDEXER_STATE_INIT, /**< Init state.*/ |
| 49 | TS_INDEXER_STATE_TS_START, /**< TS header state with start_indicator==1.*/ |
| 50 | TS_INDEXER_STATE_PES_HEADER, /**< PES header state.*/ |
| 51 | TS_INDEXER_STATE_PES_PTS, /**< PES pts state.*/ |
| 52 | TS_INDEXER_STATE_PES_I_FRAME /**< PES I-frame state.*/ |
| 53 | } TS_Indexer_State_t; |
| 54 | |
| 55 | /**Event.*/ |
| 56 | typedef struct { |
| 57 | TS_Indexer_EventType_t type; /**< Event type.*/ |
| 58 | int pid; /**< The PID of the stream.*/ |
Yahui Han | 63de708 | 2023-07-12 18:30:10 +0800 | [diff] [blame^] | 59 | uint64_t offset; /**< the TS packet offset of this event.*/ |
Gong Ke | fa504d7 | 2023-04-18 10:23:35 +0800 | [diff] [blame] | 60 | uint64_t pts; /**< The PTS of this frame.*/ |
| 61 | } TS_Indexer_Event_t; |
| 62 | |
| 63 | /**TS indexer.*/ |
| 64 | typedef struct TS_Indexer_s TS_Indexer_t; |
| 65 | |
| 66 | /**Event callback function.*/ |
| 67 | typedef void (*TS_Indexer_EventCallback_t) (TS_Indexer_t *ts_indexer, TS_Indexer_Event_t *event); |
| 68 | |
| 69 | /**PES parser.*/ |
| 70 | typedef struct { |
| 71 | uint64_t pts; /**< The a/v PTS.*/ |
| 72 | uint64_t offset; /**< The current offset.*/ |
| 73 | uint8_t data[184+16]; /**< The PES data.*/ |
| 74 | int len; /**< The length of PES data`.*/ |
| 75 | TS_Indexer_State_t state; /**< The stream state.*/ |
| 76 | } PESParser; |
| 77 | |
| 78 | /**TS parser.*/ |
| 79 | typedef struct { |
| 80 | int pid; /**< The a/v PID.*/ |
| 81 | TS_Indexer_StreamFormat_t format; /**< The a/v format.*/ |
| 82 | PESParser PES; /**< The PES parser.*/ |
| 83 | uint64_t offset; /**< The offset of packet with start indicator.*/ |
| 84 | } TSParser; |
| 85 | |
| 86 | /**TS indexer.*/ |
| 87 | struct TS_Indexer_s { |
| 88 | TSParser video_parser; /**< The video parser.*/ |
| 89 | TSParser audio_parser; /**< The audio parser.*/ |
| 90 | uint64_t offset; /**< The current offset.*/ |
| 91 | TS_Indexer_EventCallback_t callback; /**< The event callback function.*/ |
| 92 | }; |
| 93 | |
| 94 | /** |
| 95 | * Initialize the TS indexer. |
| 96 | * \param ts_indexer The TS indexer to be initialized. |
| 97 | * \retval 0 On success. |
| 98 | * \retval -1 On error. |
| 99 | */ |
| 100 | int ts_indexer_init (TS_Indexer_t *ts_indexer); |
| 101 | |
| 102 | /** |
| 103 | * Release the TS indexer. |
| 104 | * \param ts_indexer The TS indexer to be released. |
| 105 | */ |
| 106 | void ts_indexer_destroy (TS_Indexer_t *ts_indexer); |
| 107 | |
| 108 | /** |
| 109 | * Set the video format. |
| 110 | * \param ts_indexer The TS indexer. |
| 111 | * \param format The video format. |
| 112 | * \retval 0 On success. |
| 113 | * \retval -1 On error. |
| 114 | */ |
| 115 | int ts_indexer_set_video_format (TS_Indexer_t *ts_indexer, TS_Indexer_StreamFormat_t format); |
| 116 | |
| 117 | /** |
| 118 | * Set the video PID. |
| 119 | * \param ts_indexer The TS indexer. |
| 120 | * \param pid The video PID. |
| 121 | * \retval 0 On success. |
| 122 | * \retval -1 On error. |
| 123 | */ |
| 124 | int ts_indexer_set_video_pid (TS_Indexer_t *ts_indexer, int pid); |
| 125 | |
| 126 | /** |
| 127 | * Set the audio PID. |
| 128 | * \param ts_indexer The TS indexer. |
| 129 | * \param pid The audio PID. |
| 130 | * \retval 0 On success. |
| 131 | * \retval -1 On error. |
| 132 | */ |
| 133 | int ts_indexer_set_audio_pid (TS_Indexer_t *ts_indexer, int pid); |
| 134 | |
| 135 | /** |
| 136 | * Set the event callback function. |
| 137 | * \param ts_indexer The TS indexer. |
| 138 | * \param callback The event callback function. |
| 139 | * \retval 0 On success. |
| 140 | * \retval -1 On error. |
| 141 | */ |
| 142 | int ts_indexer_set_event_callback (TS_Indexer_t *ts_indexer, TS_Indexer_EventCallback_t callback); |
| 143 | |
| 144 | /** |
| 145 | * Parse the TS stream and generate the index data. |
| 146 | * \param ts_indexer The TS indexer. |
| 147 | * \param data The TS data. |
| 148 | * \param len The length of the TS data in bytes. |
| 149 | * \return The left TS data length of bytes. |
| 150 | */ |
| 151 | int ts_indexer_parse (TS_Indexer_t *ts_indexer, uint8_t *data, int len); |
| 152 | |
| 153 | #ifdef __cplusplus |
| 154 | } |
| 155 | #endif |
| 156 | |
| 157 | #endif /*_TS_INDEXER_H_*/ |
| 158 | |