1 /*
2 * GStreamer
3 * Copyright (c) 2010, Texas Instruments Incorporated
4 *
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation
8 * version 2.1 of the License.
9 *
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
14 *
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this library; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18 */
20 #ifndef __GST_DUCATIVIDDEC_H__
21 #define __GST_DUCATIVIDDEC_H__
23 #include <stdint.h>
24 #include <stddef.h>
25 #include <unistd.h>
26 #include <omap_drm.h>
27 #include <omap_drmif.h>
29 #include "gstducati.h"
30 #include "gstducatibufferpriv.h"
32 #include <gst/drm/gstdrmbufferpool.h>
33 #include <gst/video/video.h>
34 #include <gst/video/gstvideometa.h>
36 G_BEGIN_DECLS
37 #define GST_TYPE_DUCATIVIDDEC (gst_ducati_viddec_get_type())
38 #define GST_DUCATIVIDDEC(obj) (G_TYPE_CHECK_INSTANCE_CAST((obj), GST_TYPE_DUCATIVIDDEC, GstDucatiVidDec))
39 #define GST_DUCATIVIDDEC_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST((klass), GST_TYPE_DUCATIVIDDEC, GstDucatiVidDecClass))
40 #define GST_IS_DUCATIVIDDEC(obj) (G_TYPE_CHECK_INSTANCE_TYPE((obj), GST_TYPE_DUCATIVIDDEC))
41 #define GST_IS_DUCATIVIDDEC_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE((klass), GST_TYPE_DUCATIVIDDEC))
42 #define GST_DUCATIVIDDEC_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS((obj), GST_TYPE_DUCATIVIDDEC, GstDucatiVidDecClass))
43 typedef struct _GstDucatiVidDec GstDucatiVidDec;
44 typedef struct _GstDucatiVidDecClass GstDucatiVidDecClass;
46 /* For re-ordering in normal playback */
47 #define MAX_BACKLOG_FRAMES 16
48 /* For re-ordering in reverse playback */
49 #define MAX_BACKLOG_ARRAY_SIZE 120
51 struct _GstDucatiVidDec
52 {
53 GstElement parent;
55 GstPad *sinkpad, *srcpad;
57 GstDRMBufferPool *pool;
59 /* minimum output size required by the codec: */
60 gint outsize;
62 /* minimum number of buffers required by the codec: */
63 gint min_buffers;
65 /* input (unpadded, unaligned) size of video: */
66 gint input_width, input_height;
68 /* input (unpadded, aligned to MB) size of video: */
69 gint width, height;
71 gint fps_n, fps_d;
73 /* output (padded) size including any codec padding: */
74 gint padded_width, padded_height;
76 /* output stride (>= padded_width) */
77 gint stride;
79 gboolean interlaced;
81 struct omap_bo *input_bo;
82 /* input buffer, allocated when codec is created: */
83 guint8 *input;
85 /* number of bytes pushed to input on current frame: */
86 gint in_size;
88 /* on first output buffer, we need to send crop info to sink.. and some
89 * operations like flushing should be avoided if we haven't sent any
90 * input buffers:
91 */
92 gboolean first_out_buffer, first_in_buffer;
94 GstSegment segment;
95 gdouble qos_proportion;
96 GstClockTime qos_earliest_time;
98 gboolean need_out_buf;
100 /* For storing the external pool query history */
101 gboolean queried_external_pool;
102 GstBufferPool *externalpool;
104 /* by default, codec_data from sinkpad is prepended to first buffer: */
106 guint8 *codecdata;
107 gsize codecdatasize;
109 /* workaround enabled to indicate that timestamp from demuxer is PTS,
110 * not DTS (cough, cough.. avi):
111 */
112 gboolean ts_is_pts;
114 /* auto-detection for ts_is_pts workaround.. if we detect out of order
115 * timestamps from demuxer/parser, then the ts is definitely DTS,
116 * otherwise it may be PTS and out of order timestamps out of decoder
117 * will trigger the ts_is_pts workaround.
118 */
119 gboolean ts_may_be_pts;
121 gboolean wait_keyframe;
123 gboolean needs_flushing;
125 gboolean codec_create_params_changed;
127 GHashTable *passed_in_bufs;
129 GHashTable *dce_locked_bufs;
131 #define NDTS 32
132 GstClockTime dts_queue[NDTS];
133 gint dts_ridx, dts_widx;
134 GstClockTime last_dts, last_pts;
136 Engine_Handle engine;
137 VIDDEC3_Handle codec;
138 VIDDEC3_Params *params;
139 VIDDEC3_DynamicParams *dynParams;
140 VIDDEC3_Status *status;
141 XDM2_BufDesc *inBufs;
142 XDM2_BufDesc *outBufs;
143 VIDDEC3_InArgs *inArgs;
144 VIDDEC3_OutArgs *outArgs;
146 XDAS_Int16 pageMemType;
147 struct omap_device *device;
149 /* Frames waiting to be reordered */
150 GstBuffer *backlog_frames[MAX_BACKLOG_ARRAY_SIZE + 1];
151 gint backlog_maxframes;
152 gint backlog_nframes;
153 gint backlog_max_maxframes;
155 gboolean codec_debug_info;
157 const char *error_strings[32];
158 };
160 struct _GstDucatiVidDecClass
161 {
162 GstElementClass parent_class;
164 const gchar *codec_name;
166 /**
167 * Parse codec specific fields the given caps structure. The base-
168 * class implementation of this method handles standard stuff like
169 * width/height/framerate/codec_data.
170 */
171 gboolean (*parse_caps) (GstDucatiVidDec * self, GstStructure * s);
173 /**
174 * Called when the input buffer size changes, to recalculate codec required
175 * output buffer size and minimum count
176 */
177 void (*update_buffer_size) (GstDucatiVidDec * self);
179 /**
180 * Called to allocate/initialize params/dynParams/status/inArgs/outArgs
181 */
182 gboolean (*allocate_params) (GstDucatiVidDec * self, gint params_sz,
183 gint dynparams_sz, gint status_sz, gint inargs_sz, gint outargs_sz);
185 /**
186 * Push input data into codec's input buffer, returning a sub-buffer of
187 * any remaining data, or NULL if none. Consumes reference to 'buf'
188 */
189 GstBuffer *(*push_input) (GstDucatiVidDec * self, GstBuffer * buf);
191 /**
192 * Called to handle errors returned by VIDDEC3_process.
193 */
194 gint (*handle_error) (GstDucatiVidDec * self, gint ret, gint extended_error,
195 gint status_extended_error);
197 /**
198 * Called to check whether it's a good idea to drop buf or not.
199 */
200 gboolean (*can_drop_frame) (GstDucatiVidDec * self, GstBuffer * buf,
201 gint64 diff);
203 gboolean (*query) (GstDucatiVidDec * self, GstPad * pad, GstQuery * query,
204 gboolean * forward);
206 /**
207 * Called to push a decoder buffer. Consumes reference to 'buf'.
208 */
209 GstFlowReturn (*push_output) (GstDucatiVidDec * self, GstBuffer * buf);
211 /**
212 * Called before a flush happens.
213 */
214 void (*on_flush) (GstDucatiVidDec * self, gboolean eos);
216 /**
217 * Called to set new caps on the sink pad.
218 */
219 gboolean (*set_sink_caps) (GstDucatiVidDec * self, GstCaps * caps);
220 };
222 GType gst_ducati_viddec_get_type (void);
224 /* helper methods for derived classes: */
226 static inline void
227 push_input (GstDucatiVidDec * self, const guint8 * in, gint sz)
228 {
229 GST_DEBUG_OBJECT (self, "push: %d bytes)", sz);
230 memcpy (self->input + self->in_size, in, sz);
231 self->in_size += sz;
232 }
234 static inline int
235 check_start_code (const guint8 * sc, gint scsize,
236 const guint8 * inbuf, gint insize)
237 {
238 if (insize < scsize)
239 return FALSE;
241 while (scsize) {
242 if (*sc != *inbuf)
243 return FALSE;
244 scsize--;
245 sc++;
246 inbuf++;
247 }
249 return TRUE;
250 }
252 static inline int
253 find_start_code (const guint8 * sc, gint scsize,
254 const guint8 * inbuf, gint insize)
255 {
256 gint size = 0;
257 while (insize) {
258 if (check_start_code (sc, scsize, inbuf, insize))
259 break;
260 insize--;
261 size++;
262 inbuf++;
263 }
264 return size;
265 }
267 gboolean gst_ducati_viddec_codec_flush (GstDucatiVidDec * self, gboolean eos);
269 G_END_DECLS
270 #endif /* __GST_DUCATIVIDDEC_H__ */