blob: 660077ec30e25ece73b1216a976779db127f9585 [file] [log] [blame]
Gong Kefa504d72023-04-18 10:23:35 +08001/**
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
12extern "C" {
13#endif
14
15/**Video format*/
16typedef 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.*/
23typedef enum {
Yahui Han63de7082023-07-12 18:30:10 +080024 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.*/
Yahui Hana082ab62023-07-14 14:41:39 +080039 TS_INDEXER_EVENT_TYPE_HEVC_BLA_N_LP, /**< HEVC NAL unit type BLA_N_LP.*/
Yahui Han63de7082023-07-12 18:30:10 +080040 TS_INDEXER_EVENT_TYPE_HEVC_IDR_W_RADL, /**< HEVC NAL unit type IDR_W_RADL.*/
41 TS_INDEXER_EVENT_TYPE_HEVC_IDR_N_LP, /**< HEVC NAL unit type IDR_N_LP.*/
42 TS_INDEXER_EVENT_TYPE_HEVC_TRAIL_CRA, /**< HEVC NAL unit type TRAIL_CRA.*/
43 TS_INDEXER_EVENT_TYPE_VIDEO_PTS, /**< MEPG2/AVC/HEVC PTS.*/
44 TS_INDEXER_EVENT_TYPE_AUDIO_PTS /**< Audio PTS.*/
Gong Kefa504d72023-04-18 10:23:35 +080045} TS_Indexer_EventType_t;
46
47/**Stream Parser state.*/
48typedef enum {
49 TS_INDEXER_STATE_INIT, /**< Init state.*/
50 TS_INDEXER_STATE_TS_START, /**< TS header state with start_indicator==1.*/
51 TS_INDEXER_STATE_PES_HEADER, /**< PES header state.*/
52 TS_INDEXER_STATE_PES_PTS, /**< PES pts state.*/
53 TS_INDEXER_STATE_PES_I_FRAME /**< PES I-frame state.*/
54} TS_Indexer_State_t;
55
56/**Event.*/
57typedef struct {
58 TS_Indexer_EventType_t type; /**< Event type.*/
59 int pid; /**< The PID of the stream.*/
Yahui Han63de7082023-07-12 18:30:10 +080060 uint64_t offset; /**< the TS packet offset of this event.*/
Gong Kefa504d72023-04-18 10:23:35 +080061 uint64_t pts; /**< The PTS of this frame.*/
62} TS_Indexer_Event_t;
63
64/**TS indexer.*/
65typedef struct TS_Indexer_s TS_Indexer_t;
66
67/**Event callback function.*/
68typedef void (*TS_Indexer_EventCallback_t) (TS_Indexer_t *ts_indexer, TS_Indexer_Event_t *event);
69
70/**PES parser.*/
71typedef struct {
72 uint64_t pts; /**< The a/v PTS.*/
73 uint64_t offset; /**< The current offset.*/
74 uint8_t data[184+16]; /**< The PES data.*/
75 int len; /**< The length of PES data`.*/
76 TS_Indexer_State_t state; /**< The stream state.*/
77} PESParser;
78
79/**TS parser.*/
80typedef struct {
81 int pid; /**< The a/v PID.*/
82 TS_Indexer_StreamFormat_t format; /**< The a/v format.*/
83 PESParser PES; /**< The PES parser.*/
84 uint64_t offset; /**< The offset of packet with start indicator.*/
85} TSParser;
86
87/**TS indexer.*/
88struct TS_Indexer_s {
89 TSParser video_parser; /**< The video parser.*/
90 TSParser audio_parser; /**< The audio parser.*/
91 uint64_t offset; /**< The current offset.*/
92 TS_Indexer_EventCallback_t callback; /**< The event callback function.*/
93};
94
95/**
96 * Initialize the TS indexer.
97 * \param ts_indexer The TS indexer to be initialized.
98 * \retval 0 On success.
99 * \retval -1 On error.
100 */
101int ts_indexer_init (TS_Indexer_t *ts_indexer);
102
103/**
104 * Release the TS indexer.
105 * \param ts_indexer The TS indexer to be released.
106 */
107void ts_indexer_destroy (TS_Indexer_t *ts_indexer);
108
109/**
110 * Set the video format.
111 * \param ts_indexer The TS indexer.
112 * \param format The video format.
113 * \retval 0 On success.
114 * \retval -1 On error.
115 */
116int ts_indexer_set_video_format (TS_Indexer_t *ts_indexer, TS_Indexer_StreamFormat_t format);
117
118/**
119 * Set the video PID.
120 * \param ts_indexer The TS indexer.
121 * \param pid The video PID.
122 * \retval 0 On success.
123 * \retval -1 On error.
124 */
125int ts_indexer_set_video_pid (TS_Indexer_t *ts_indexer, int pid);
126
127/**
128 * Set the audio PID.
129 * \param ts_indexer The TS indexer.
130 * \param pid The audio PID.
131 * \retval 0 On success.
132 * \retval -1 On error.
133 */
134int ts_indexer_set_audio_pid (TS_Indexer_t *ts_indexer, int pid);
135
136/**
137 * Set the event callback function.
138 * \param ts_indexer The TS indexer.
139 * \param callback The event callback function.
140 * \retval 0 On success.
141 * \retval -1 On error.
142 */
143int ts_indexer_set_event_callback (TS_Indexer_t *ts_indexer, TS_Indexer_EventCallback_t callback);
144
145/**
146 * Parse the TS stream and generate the index data.
147 * \param ts_indexer The TS indexer.
148 * \param data The TS data.
149 * \param len The length of the TS data in bytes.
150 * \return The left TS data length of bytes.
151 */
152int ts_indexer_parse (TS_Indexer_t *ts_indexer, uint8_t *data, int len);
153
154#ifdef __cplusplus
155}
156#endif
157
158#endif /*_TS_INDEXER_H_*/
159