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
38 #define GST_TYPE_DUCATIVIDDEC (gst_ducati_viddec_get_type())
39 #define GST_DUCATIVIDDEC(obj) (G_TYPE_CHECK_INSTANCE_CAST((obj), GST_TYPE_DUCATIVIDDEC, GstDucatiVidDec))
40 #define GST_DUCATIVIDDEC_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST((klass), GST_TYPE_DUCATIVIDDEC, GstDucatiVidDecClass))
41 #define GST_IS_DUCATIVIDDEC(obj) (G_TYPE_CHECK_INSTANCE_TYPE((obj), GST_TYPE_DUCATIVIDDEC))
42 #define GST_IS_DUCATIVIDDEC_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE((klass), GST_TYPE_DUCATIVIDDEC))
43 #define GST_DUCATIVIDDEC_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS((obj), GST_TYPE_DUCATIVIDDEC, GstDucatiVidDecClass))
45 typedef struct _GstDucatiVidDec GstDucatiVidDec;
46 typedef struct _GstDucatiVidDecClass GstDucatiVidDecClass;
48 /* For re-ordering in normal playback */
49 #define MAX_BACKLOG_FRAMES 16
50 /* For re-ordering in reverse playback */
51 #define MAX_BACKLOG_ARRAY_SIZE 120
53 struct _GstDucatiVidDec
54 {
55 GstElement parent;
57 GstPad *sinkpad, *srcpad;
59 GstDRMBufferPool *pool;
61 /* minimum output size required by the codec: */
62 gint outsize;
64 /* minimum number of buffers required by the codec: */
65 gint min_buffers;
67 /* input (unpadded, unaligned) size of video: */
68 gint input_width, input_height;
70 /* input (unpadded, aligned to MB) size of video: */
71 gint width, height;
73 gint fps_n, fps_d;
75 /* output (padded) size including any codec padding: */
76 gint padded_width, padded_height;
78 /* output stride (>= padded_width) */
79 gint stride;
81 gboolean interlaced;
83 struct omap_bo *input_bo;
84 /* input buffer, allocated when codec is created: */
85 guint8 *input;
87 /* number of bytes pushed to input on current frame: */
88 gint in_size;
90 /* on first output buffer, we need to send crop info to sink.. and some
91 * operations like flushing should be avoided if we haven't sent any
92 * input buffers:
93 */
94 gboolean first_out_buffer, first_in_buffer;
96 GstSegment segment;
97 gdouble qos_proportion;
98 GstClockTime qos_earliest_time;
100 gboolean need_out_buf;
102 /* by default, codec_data from sinkpad is prepended to first buffer: */
103 GstBuffer *codec_data;
105 /* workaround enabled to indicate that timestamp from demuxer is PTS,
106 * not DTS (cough, cough.. avi):
107 */
108 gboolean ts_is_pts;
110 /* auto-detection for ts_is_pts workaround.. if we detect out of order
111 * timestamps from demuxer/parser, then the ts is definitely DTS,
112 * otherwise it may be PTS and out of order timestamps out of decoder
113 * will trigger the ts_is_pts workaround.
114 */
115 gboolean ts_may_be_pts;
117 gboolean wait_keyframe;
119 gboolean needs_flushing;
121 GHashTable *passed_in_bufs;
123 GHashTable *dce_locked_bufs;
125 #define NDTS 32
126 GstClockTime dts_queue[NDTS];
127 gint dts_ridx, dts_widx;
128 GstClockTime last_dts, last_pts;
130 Engine_Handle engine;
131 VIDDEC3_Handle codec;
132 VIDDEC3_Params *params;
133 VIDDEC3_DynamicParams *dynParams;
134 VIDDEC3_Status *status;
135 XDM2_BufDesc *inBufs;
136 XDM2_BufDesc *outBufs;
137 VIDDEC3_InArgs *inArgs;
138 VIDDEC3_OutArgs *outArgs;
140 XDAS_Int16 pageMemType;
141 struct omap_device *device;
143 /* Frames waiting to be reordered */
144 GstBuffer *backlog_frames[MAX_BACKLOG_ARRAY_SIZE + 1];
145 gint backlog_maxframes;
146 gint backlog_nframes;
147 gint backlog_max_maxframes;
149 gboolean codec_debug_info;
151 const char *error_strings[32];
152 };
154 struct _GstDucatiVidDecClass
155 {
156 GstElementClass parent_class;
158 const gchar *codec_name;
160 /**
161 * Parse codec specific fields the given caps structure. The base-
162 * class implementation of this method handles standard stuff like
163 * width/height/framerate/codec_data.
164 */
165 gboolean (*parse_caps) (GstDucatiVidDec * self, GstStructure * s);
167 /**
168 * Called when the input buffer size changes, to recalculate codec required
169 * output buffer size and minimum count
170 */
171 void (*update_buffer_size) (GstDucatiVidDec * self);
173 /**
174 * Called to allocate/initialize params/dynParams/status/inArgs/outArgs
175 */
176 gboolean (*allocate_params) (GstDucatiVidDec * self, gint params_sz,
177 gint dynparams_sz, gint status_sz, gint inargs_sz, gint outargs_sz);
179 /**
180 * Push input data into codec's input buffer, returning a sub-buffer of
181 * any remaining data, or NULL if none. Consumes reference to 'buf'
182 */
183 GstBuffer * (*push_input) (GstDucatiVidDec * self, GstBuffer * buf);
185 /**
186 * Called to handle errors returned by VIDDEC3_process.
187 */
188 gint (*handle_error) (GstDucatiVidDec * self, gint ret, gint extended_error,
189 gint status_extended_error);
191 /**
192 * Called to check whether it's a good idea to drop buf or not.
193 */
194 gboolean (*can_drop_frame) (GstDucatiVidDec * self, GstBuffer * buf, gint64 diff);
196 gboolean (*query) (GstDucatiVidDec * self, GstPad * pad, GstQuery * query,
197 gboolean * forward);
199 /**
200 * Called to push a decoder buffer. Consumes reference to 'buf'.
201 */
202 GstFlowReturn (*push_output) (GstDucatiVidDec * self, GstBuffer * buf);
204 /**
205 * Called before a flush happens.
206 */
207 void (*on_flush) (GstDucatiVidDec * self, gboolean eos);
209 /**
210 * Called to set new caps on the sink pad.
211 */
212 gboolean (*set_sink_caps) (GstDucatiVidDec * self, GstCaps *caps);
213 };
215 GType gst_ducati_viddec_get_type (void);
217 /* helper methods for derived classes: */
219 static inline void
220 push_input (GstDucatiVidDec * self, const guint8 *in, gint sz)
221 {
222 GST_DEBUG_OBJECT (self, "push: %d bytes)", sz);
223 memcpy (self->input + self->in_size, in, sz);
224 self->in_size += sz;
225 }
227 static inline int
228 check_start_code (const guint8 *sc, gint scsize,
229 const guint8 *inbuf, gint insize)
230 {
231 if (insize < scsize)
232 return FALSE;
234 while (scsize) {
235 if (*sc != *inbuf)
236 return FALSE;
237 scsize--;
238 sc++;
239 inbuf++;
240 }
242 return TRUE;
243 }
245 static inline int
246 find_start_code (const guint8 *sc, gint scsize,
247 const guint8 *inbuf, gint insize)
248 {
249 gint size = 0;
250 while (insize) {
251 if (check_start_code (sc, scsize, inbuf, insize))
252 break;
253 insize--;
254 size++;
255 inbuf++;
256 }
257 return size;
258 }
260 gboolean
261 gst_ducati_viddec_codec_flush (GstDucatiVidDec * self, gboolean eos);
263 G_END_DECLS
265 #endif /* __GST_DUCATIVIDDEC_H__ */