codecs: Keep track of non-decoding-essential input state change
[platform/upstream/gstreamer.git] / subprojects / gst-plugins-bad / gst-libs / gst / codecs / gsth264picture.h
1 /* GStreamer
2  * Copyright (C) 2019 Seungha Yang <seungha.yang@navercorp.com>
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Library General Public
6  * License as published by the Free Software Foundation; either
7  * version 2 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * Library General Public License for more details.
13  *
14  * You should have received a copy of the GNU Library General Public
15  * License along with this library; if not, write to the
16  * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
17  * Boston, MA 02110-1301, USA.
18  */
19
20 #ifndef __GST_H264_PICTURE_H__
21 #define __GST_H264_PICTURE_H__
22
23 #include <gst/codecs/codecs-prelude.h>
24 #include <gst/codecparsers/gsth264parser.h>
25 #include <gst/video/video.h>
26
27 G_BEGIN_DECLS
28
29 #define GST_TYPE_H264_PICTURE     (gst_h264_picture_get_type())
30 #define GST_IS_H264_PICTURE(obj)  (GST_IS_MINI_OBJECT_TYPE(obj, GST_TYPE_H264_PICTURE))
31 #define GST_H264_PICTURE(obj)     ((GstH264Picture *)obj)
32 #define GST_H264_PICTURE_CAST(obj) (GST_H264_PICTURE(obj))
33
34 typedef struct _GstH264Slice GstH264Slice;
35 typedef struct _GstH264Picture GstH264Picture;
36
37 /* As specified in A.3.1 h) and A.3.2 f) */
38 #define GST_H264_DPB_MAX_SIZE 16
39
40 /**
41  * GST_H264_PICTURE_IS_REF:
42  * @picture: a #GstH264Picture
43  *
44  * Check whether @picture is used for short-term or long-term reference
45  *
46  * Since: 1.20
47  */
48 #define GST_H264_PICTURE_IS_REF(picture) \
49     ((picture)->ref != GST_H264_PICTURE_REF_NONE)
50
51 /**
52  * GST_H264_PICTURE_IS_SHORT_TERM_REF:
53  * @picture: a #GstH264Picture
54  *
55  * Check whether @picture is used for short-term reference
56  *
57  * Since: 1.20
58  */
59 #define GST_H264_PICTURE_IS_SHORT_TERM_REF(picture) \
60     ((picture)->ref == GST_H264_PICTURE_REF_SHORT_TERM)
61
62 /**
63  * GST_H264_PICTURE_IS_LONG_TERM_REF:
64  * @picture: a #GstH264Picture
65  *
66  * Check whether @picture is used for long-term reference
67  *
68  * Since: 1.20
69  */
70 #define GST_H264_PICTURE_IS_LONG_TERM_REF(picture) \
71     ((picture)->ref == GST_H264_PICTURE_REF_LONG_TERM)
72
73 /**
74  * GST_H264_PICTURE_IS_FRAME:
75  * @picture: a #GstH264Picture
76  *
77  * Check whether @picture is a frame (not a field picture)
78  *
79  * Since: 1.20
80  */
81 #define GST_H264_PICTURE_IS_FRAME(picture) \
82     ((picture)->field == GST_H264_PICTURE_FIELD_FRAME)
83
84 struct _GstH264Slice
85 {
86   GstH264SliceHdr header;
87
88   /* parsed nal unit (doesn't take ownership of raw data) */
89   GstH264NalUnit nalu;
90 };
91
92 typedef enum
93 {
94   GST_H264_PICTURE_FIELD_FRAME,
95   GST_H264_PICTURE_FIELD_TOP_FIELD,
96   GST_H264_PICTURE_FIELD_BOTTOM_FIELD,
97 } GstH264PictureField;
98
99 /**
100  * GstH264PictureReference:
101  * @GST_H264_PICTURE_REF_NONE: Not used for reference picture
102  * @GST_H264_PICTURE_REF_SHORT_TERM: Used for short-term reference picture
103  * @GST_H264_PICTURE_REF_LONG_TERM: Used for long-term reference picture
104  *
105  * Since: 1.20
106  */
107 typedef enum
108 {
109   GST_H264_PICTURE_REF_NONE = 0,
110   GST_H264_PICTURE_REF_SHORT_TERM,
111   GST_H264_PICTURE_REF_LONG_TERM,
112 } GstH264PictureReference;
113
114 struct _GstH264Picture
115 {
116   /*< private >*/
117   GstMiniObject parent;
118
119   GstH264SliceType type;
120
121   /* From GstVideoCodecFrame */
122   guint32 system_frame_number;
123
124   guint8 pic_order_cnt_type;  /* SPS */
125   gint32 top_field_order_cnt;
126   gint32 bottom_field_order_cnt;
127
128   gint pic_order_cnt;
129   gint pic_order_cnt_msb;
130   gint pic_order_cnt_lsb;
131   gint delta_pic_order_cnt_bottom;
132   gint delta_pic_order_cnt0;
133   gint delta_pic_order_cnt1;
134
135   gint pic_num;
136   gint long_term_pic_num;
137   gint frame_num;
138   gint frame_num_offset;
139   gint frame_num_wrap;
140   gint long_term_frame_idx;
141
142   gint nal_ref_idc;
143   gboolean idr;
144   gint idr_pic_id;
145   gboolean field_pic_flag;
146   GstH264PictureReference ref;
147   /* Whether a reference picture. */
148   gboolean ref_pic;
149   gboolean needed_for_output;
150   gboolean mem_mgmt_5;
151
152   gboolean nonexisting;
153
154   GstH264PictureField field;
155
156   GstH264DecRefPicMarking dec_ref_pic_marking;
157
158   /* For interlaced decoding */
159   gboolean second_field;
160   GstH264Picture * other_field;
161
162   GstVideoBufferFlags buffer_flags;
163
164   /* decoder input state if this picture is discont point */
165   GstVideoCodecState *discont_state;
166
167   gpointer user_data;
168   GDestroyNotify notify;
169 };
170
171 /**
172  * GstH264DpbBumpMode:
173  * @GST_H264_DPB_BUMP_NORMAL_LATENCY: No latency requirement for DBP bumping.
174  * @GST_H264_DPB_BUMP_LOW_LATENCY: Low-latency requirement for DBP bumping.
175  * @GST_H264_DPB_BUMP_VERY_LOW_LATENCY: Very low-latency requirement for DBP bumping.
176  *
177  * Since: 1.20
178  */
179 typedef enum
180 {
181   GST_H264_DPB_BUMP_NORMAL_LATENCY,
182   GST_H264_DPB_BUMP_LOW_LATENCY,
183   GST_H264_DPB_BUMP_VERY_LOW_LATENCY
184 } GstH264DpbBumpMode;
185
186 GST_CODECS_API
187 GType gst_h264_picture_get_type (void);
188
189 GST_CODECS_API
190 GstH264Picture * gst_h264_picture_new (void);
191
192 static inline GstH264Picture *
193 gst_h264_picture_ref (GstH264Picture * picture)
194 {
195   return (GstH264Picture *) gst_mini_object_ref (GST_MINI_OBJECT_CAST (picture));
196 }
197
198 static inline void
199 gst_h264_picture_unref (GstH264Picture * picture)
200 {
201   gst_mini_object_unref (GST_MINI_OBJECT_CAST (picture));
202 }
203
204 static inline gboolean
205 gst_h264_picture_replace (GstH264Picture ** old_picture,
206     GstH264Picture * new_picture)
207 {
208   return gst_mini_object_replace ((GstMiniObject **) old_picture,
209       (GstMiniObject *) new_picture);
210 }
211
212 static inline void
213 gst_clear_h264_picture (GstH264Picture ** picture)
214 {
215   if (picture && *picture) {
216     gst_h264_picture_unref (*picture);
217     *picture = NULL;
218   }
219 }
220
221 GST_CODECS_API
222 void gst_h264_picture_set_user_data (GstH264Picture * picture,
223                                      gpointer user_data,
224                                      GDestroyNotify notify);
225
226 GST_CODECS_API
227 gpointer gst_h264_picture_get_user_data (GstH264Picture * picture);
228
229 /*******************
230  * GstH264Dpb *
231  *******************/
232 typedef struct _GstH264Dpb GstH264Dpb;
233
234 GST_CODECS_API
235 GstH264Dpb * gst_h264_dpb_new (void);
236
237 GST_CODECS_API
238 void  gst_h264_dpb_set_max_num_frames (GstH264Dpb * dpb,
239                                        gint max_num_frames);
240
241 GST_CODECS_API
242 gint gst_h264_dpb_get_max_num_frames  (GstH264Dpb * dpb);
243
244 GST_CODECS_API
245 void gst_h264_dpb_set_interlaced      (GstH264Dpb * dpb,
246                                        gboolean interlaced);
247
248 GST_CODECS_API
249 void gst_h264_dpb_set_max_num_reorder_frames (GstH264Dpb * dpb,
250                                               guint32 max_num_reorder_frames);
251
252 GST_CODECS_API
253 gboolean gst_h264_dpb_get_interlaced  (GstH264Dpb * dpb);
254
255 GST_CODECS_API
256 void  gst_h264_dpb_free             (GstH264Dpb * dpb);
257
258 GST_CODECS_API
259 void  gst_h264_dpb_clear            (GstH264Dpb * dpb);
260
261 GST_CODECS_API
262 void  gst_h264_dpb_add              (GstH264Dpb * dpb,
263                                      GstH264Picture * picture);
264
265 GST_CODECS_API
266 void  gst_h264_dpb_delete_unused    (GstH264Dpb * dpb);
267
268 GST_CODECS_API
269 gint  gst_h264_dpb_num_ref_frames (GstH264Dpb * dpb);
270
271 GST_CODECS_API
272 void  gst_h264_dpb_mark_all_non_ref (GstH264Dpb * dpb);
273
274 GST_CODECS_API
275 GstH264Picture * gst_h264_dpb_get_short_ref_by_pic_num (GstH264Dpb * dpb,
276                                                         gint pic_num);
277
278 GST_CODECS_API
279 GstH264Picture * gst_h264_dpb_get_long_ref_by_long_term_pic_num (GstH264Dpb * dpb,
280                                                                  gint long_term_pic_num);
281
282 GST_CODECS_API
283 GstH264Picture * gst_h264_dpb_get_lowest_frame_num_short_ref (GstH264Dpb * dpb);
284
285 GST_CODECS_API
286 void  gst_h264_dpb_get_pictures_short_term_ref (GstH264Dpb * dpb,
287                                                 gboolean include_non_existing,
288                                                 gboolean include_second_field,
289                                                 GArray * out);
290
291 GST_CODECS_API
292 void  gst_h264_dpb_get_pictures_long_term_ref  (GstH264Dpb * dpb,
293                                                 gboolean include_second_field,
294                                                 GArray * out);
295
296 GST_CODECS_API
297 GArray * gst_h264_dpb_get_pictures_all         (GstH264Dpb * dpb);
298
299 GST_CODECS_API
300 GstH264Picture * gst_h264_dpb_get_picture      (GstH264Dpb * dpb,
301                                                 guint32 system_frame_number);
302
303 GST_CODECS_API
304 gint  gst_h264_dpb_get_size   (GstH264Dpb * dpb);
305
306 GST_CODECS_API
307 gboolean gst_h264_dpb_has_empty_frame_buffer   (GstH264Dpb * dpb);
308
309 GST_CODECS_API
310 gboolean gst_h264_dpb_needs_bump (GstH264Dpb * dpb,
311                                   GstH264Picture * to_insert,
312                                   GstH264DpbBumpMode latency_mode);
313
314 GST_CODECS_API
315 GstH264Picture * gst_h264_dpb_bump (GstH264Dpb * dpb,
316                                     gboolean drain);
317
318 GST_CODECS_API
319 void gst_h264_dpb_set_last_output (GstH264Dpb * dpb,
320                                    GstH264Picture * picture);
321
322 GST_CODECS_API
323 gboolean         gst_h264_dpb_perform_memory_management_control_operation (GstH264Dpb * dpb,
324                                                                            GstH264RefPicMarking *ref_pic_marking,
325                                                                            GstH264Picture * picture);
326
327 /* Internal methods */
328 void  gst_h264_picture_set_reference (GstH264Picture * picture,
329                                       GstH264PictureReference reference,
330                                       gboolean other_field);
331
332 G_DEFINE_AUTOPTR_CLEANUP_FUNC(GstH264Picture, gst_h264_picture_unref)
333
334 G_END_DECLS
335
336 #endif /* __GST_H264_PICTURE_H__ */