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 { |
| 24 | TS_INDEXER_EVENT_TYPE_VIDEO_I_FRAME, /**< Video I frame PTS.*/ |
| 25 | TS_INDEXER_EVENT_TYPE_VIDEO_PTS, /**< Video PTS.*/ |
| 26 | TS_INDEXER_EVENT_TYPE_AUDIO_PTS /**< Audio PTS.*/ |
| 27 | } TS_Indexer_EventType_t; |
| 28 | |
| 29 | /**Stream Parser state.*/ |
| 30 | typedef enum { |
| 31 | TS_INDEXER_STATE_INIT, /**< Init state.*/ |
| 32 | TS_INDEXER_STATE_TS_START, /**< TS header state with start_indicator==1.*/ |
| 33 | TS_INDEXER_STATE_PES_HEADER, /**< PES header state.*/ |
| 34 | TS_INDEXER_STATE_PES_PTS, /**< PES pts state.*/ |
| 35 | TS_INDEXER_STATE_PES_I_FRAME /**< PES I-frame state.*/ |
| 36 | } TS_Indexer_State_t; |
| 37 | |
| 38 | /**Event.*/ |
| 39 | typedef struct { |
| 40 | TS_Indexer_EventType_t type; /**< Event type.*/ |
| 41 | int pid; /**< The PID of the stream.*/ |
| 42 | uint64_t offset; /**< The offset of the first TS packet of this frame.*/ |
| 43 | uint64_t pts; /**< The PTS of this frame.*/ |
| 44 | } TS_Indexer_Event_t; |
| 45 | |
| 46 | /**TS indexer.*/ |
| 47 | typedef struct TS_Indexer_s TS_Indexer_t; |
| 48 | |
| 49 | /**Event callback function.*/ |
| 50 | typedef void (*TS_Indexer_EventCallback_t) (TS_Indexer_t *ts_indexer, TS_Indexer_Event_t *event); |
| 51 | |
| 52 | /**PES parser.*/ |
| 53 | typedef struct { |
| 54 | uint64_t pts; /**< The a/v PTS.*/ |
| 55 | uint64_t offset; /**< The current offset.*/ |
| 56 | uint8_t data[184+16]; /**< The PES data.*/ |
| 57 | int len; /**< The length of PES data`.*/ |
| 58 | TS_Indexer_State_t state; /**< The stream state.*/ |
| 59 | } PESParser; |
| 60 | |
| 61 | /**TS parser.*/ |
| 62 | typedef struct { |
| 63 | int pid; /**< The a/v PID.*/ |
| 64 | TS_Indexer_StreamFormat_t format; /**< The a/v format.*/ |
| 65 | PESParser PES; /**< The PES parser.*/ |
| 66 | uint64_t offset; /**< The offset of packet with start indicator.*/ |
| 67 | } TSParser; |
| 68 | |
| 69 | /**TS indexer.*/ |
| 70 | struct TS_Indexer_s { |
| 71 | TSParser video_parser; /**< The video parser.*/ |
| 72 | TSParser audio_parser; /**< The audio parser.*/ |
| 73 | uint64_t offset; /**< The current offset.*/ |
| 74 | TS_Indexer_EventCallback_t callback; /**< The event callback function.*/ |
| 75 | }; |
| 76 | |
| 77 | /** |
| 78 | * Initialize the TS indexer. |
| 79 | * \param ts_indexer The TS indexer to be initialized. |
| 80 | * \retval 0 On success. |
| 81 | * \retval -1 On error. |
| 82 | */ |
| 83 | int ts_indexer_init (TS_Indexer_t *ts_indexer); |
| 84 | |
| 85 | /** |
| 86 | * Release the TS indexer. |
| 87 | * \param ts_indexer The TS indexer to be released. |
| 88 | */ |
| 89 | void ts_indexer_destroy (TS_Indexer_t *ts_indexer); |
| 90 | |
| 91 | /** |
| 92 | * Set the video format. |
| 93 | * \param ts_indexer The TS indexer. |
| 94 | * \param format The video format. |
| 95 | * \retval 0 On success. |
| 96 | * \retval -1 On error. |
| 97 | */ |
| 98 | int ts_indexer_set_video_format (TS_Indexer_t *ts_indexer, TS_Indexer_StreamFormat_t format); |
| 99 | |
| 100 | /** |
| 101 | * Set the video PID. |
| 102 | * \param ts_indexer The TS indexer. |
| 103 | * \param pid The video PID. |
| 104 | * \retval 0 On success. |
| 105 | * \retval -1 On error. |
| 106 | */ |
| 107 | int ts_indexer_set_video_pid (TS_Indexer_t *ts_indexer, int pid); |
| 108 | |
| 109 | /** |
| 110 | * Set the audio PID. |
| 111 | * \param ts_indexer The TS indexer. |
| 112 | * \param pid The audio PID. |
| 113 | * \retval 0 On success. |
| 114 | * \retval -1 On error. |
| 115 | */ |
| 116 | int ts_indexer_set_audio_pid (TS_Indexer_t *ts_indexer, int pid); |
| 117 | |
| 118 | /** |
| 119 | * Set the event callback function. |
| 120 | * \param ts_indexer The TS indexer. |
| 121 | * \param callback The event callback function. |
| 122 | * \retval 0 On success. |
| 123 | * \retval -1 On error. |
| 124 | */ |
| 125 | int ts_indexer_set_event_callback (TS_Indexer_t *ts_indexer, TS_Indexer_EventCallback_t callback); |
| 126 | |
| 127 | /** |
| 128 | * Parse the TS stream and generate the index data. |
| 129 | * \param ts_indexer The TS indexer. |
| 130 | * \param data The TS data. |
| 131 | * \param len The length of the TS data in bytes. |
| 132 | * \return The left TS data length of bytes. |
| 133 | */ |
| 134 | int ts_indexer_parse (TS_Indexer_t *ts_indexer, uint8_t *data, int len); |
| 135 | |
| 136 | #ifdef __cplusplus |
| 137 | } |
| 138 | #endif |
| 139 | |
| 140 | #endif /*_TS_INDEXER_H_*/ |
| 141 | |