• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2023 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #ifndef GST_SUBTITLE_BASE_PARSE_H
17 #define GST_SUBTITLE_BASE_PARSE_H
18 
19 #include "gst/gst.h"
20 #include "gst/base/gstadapter.h"
21 
22 #define MAX_SUB_STREAM_NUM 20
23 
24 #define GST_TYPE_SUBTITLE_BASE_PARSE (gst_subtitle_base_parse_get_type())
25 #define GST_SUBTITLE_BASE_PARSE(obj) \
26     (G_TYPE_CHECK_INSTANCE_CAST((obj), GST_TYPE_SUBTITLE_BASE_PARSE, GstSubtitleBaseParse))
27 #define GST_SUBTITLE_BASE_PARSE_CLASS(klass) \
28     (G_TYPE_CHECK_CLASS_CAST((klass), GST_TYPE_SUBTITLE_BASE_PARSE, GstSubtitleBaseParseClass))
29 #define GST_IS_SUBTITLE_BASE_PARSE(obj) (G_TYPE_CHECK_INSTANCE_TYPE((obj), GST_TYPE_SUBTITLE_BASE_PARSE))
30 #define GST_IS_SUBTITLE_BASE_PARSE_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE((klass), GST_TYPE_SUBTITLE_BASE_PARSE))
31 #define GST_SUBTITLE_BASE_PARSE_GET_CLASS(obj) \
32     (G_TYPE_INSTANCE_GET_CLASS((obj), GST_TYPE_SUBTITLE_BASE_PARSE, GstSubtitleBaseParseClass))
33 
34 typedef struct {
35     guint64 start_time;
36     guint64 duration;
37 } GstSubtitleBaseParseState;
38 
39 typedef struct {
40     guint8 *data;
41     gsize len;
42     guint64 pts;
43     guint64 timestamp;
44     guint64 duration;
45     gint stream_index;
46 } GstSubtitleFrame;
47 
48 typedef struct {
49     guint8 *data;
50     gsize len;
51     guint64 pts;
52     guint64 timestamp;
53     guint64 duration;
54     gint stream_index;
55 } GstSubtitleDecodedFrame;
56 
57 typedef struct {
58     gint stream_id;
59     gchar *desc;
60 } GstSubtitleInfo;
61 
62 typedef struct {
63     gint stream_id;
64     GstPad *pad;
65     GstCaps *caps;
66     GstTagList *tags;
67     gboolean active;
68     GstFlowReturn last_result;
69 } GstSubtitleStream;
70 
71 typedef struct {
72     GstAdapter *adapter; // external subtitles use @adapter for buffering
73     GString *text;       // subtitle text string is stored in @text
74 } GstSubtitleBufferContext;
75 
76 typedef struct {
77     GstElement element;
78     GstPad *sinkpad;
79     gboolean need_srcpad_caps;
80     GstSubtitleBaseParseState state;
81     guint64 offset; // seek
82     GstSegment *segment;
83     GstSegment *event_segment;
84     gboolean need_segment;
85     gboolean flushing;
86     gboolean first_buffer;
87     GstSubtitleBufferContext buffer_ctx;
88     gboolean from_internal;
89     gboolean recv_eos;
90     gint stream_id;
91     gchar *language;
92     gint stream_num;
93     GstSubtitleInfo *subinfos[MAX_SUB_STREAM_NUM];
94     GstSubtitleStream *streams[MAX_SUB_STREAM_NUM];
95     GstPadTemplate *srcpadtmpl;
96     gboolean got_streams;
97     guint32 last_seekseq;
98     gint pad_num;
99     gboolean seek_snap_after;
100     gboolean switching;
101     gboolean has_send_stream_start;
102     gboolean first_segment;
103     GMutex buffermutex;
104     GMutex segmentmutex;
105     GMutex pushmutex;
106 } GstSubtitleBaseParse;
107 
108 typedef GstCaps *(*GstSubtitleFormatDetect)(const gchar *match_str);
109 typedef GstCaps *(*GstSubtitleGetSrcCaps)(const GstSubtitleBaseParse *self, gint stream_id);
110 typedef gsize (*GstSubtitleReadFrame)(GstSubtitleBaseParse *self, GstSubtitleFrame *frame);
111 typedef gboolean (*GstSubtitleDecodeFrame)(GstSubtitleBaseParse *self, const GstSubtitleFrame *frame,
112                                            GstSubtitleDecodedFrame *decoded_frame);
113 typedef void (*GstSubtitleOnSeek)(GstSubtitleBaseParse *self, const GstEvent *event);
114 typedef GstFlowReturn (*GstSubtitleHandleBuffer)(GstSubtitleBaseParse *self);
115 typedef gboolean (*GstSubtitleOnSinkEvent)(GstSubtitleBaseParse *self, GstEvent *event);
116 typedef void (*GstSubtitleSetVideoBaseTime)(GstSubtitleBaseParse *self, gint64 base_time);
117 
118 typedef struct {
119     GstElementClass parent_class;
120 
121     GstSubtitleGetSrcCaps get_srcpad_caps_pfn; // get src pad caps
122     GstSubtitleReadFrame read_frame_pfn;
123     GstSubtitleDecodeFrame decode_frame_pfn;
124     GstSubtitleOnSeek on_seek_pfn;
125     GstSubtitleHandleBuffer handle_buffer_pfn;
126     GstSubtitleOnSinkEvent on_sink_event_pfn;
127 } GstSubtitleBaseParseClass;
128 
129 GType gst_subtitle_base_parse_get_type(void);
130 
131 #endif // GST_SUBTITLE_BASE_PARSE_H
132