gstvideodecoder.h 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404
  1. /* GStreamer
  2. * Copyright (C) 2008 David Schleef <[email protected]>
  3. * Copyright (C) 2011 Mark Nauwelaerts <[email protected]>.
  4. * Copyright (C) 2011 Nokia Corporation. All rights reserved.
  5. * Contact: Stefan Kost <[email protected]>
  6. * Copyright (C) 2012 Collabora Ltd.
  7. * Author : Edward Hervey <[email protected]>
  8. *
  9. * This library is free software; you can redistribute it and/or
  10. * modify it under the terms of the GNU Library General Public
  11. * License as published by the Free Software Foundation; either
  12. * version 2 of the License, or (at your option) any later version.
  13. *
  14. * This library is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  17. * Library General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU Library General Public
  20. * License along with this library; if not, write to the
  21. * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
  22. * Boston, MA 02110-1301, USA.
  23. */
  24. #ifndef _GST_VIDEO_DECODER_H_
  25. #define _GST_VIDEO_DECODER_H_
  26. #include <gst/base/gstadapter.h>
  27. #include <gst/video/gstvideoutils.h>
  28. G_BEGIN_DECLS
  29. #define GST_TYPE_VIDEO_DECODER \
  30. (gst_video_decoder_get_type())
  31. #define GST_VIDEO_DECODER(obj) \
  32. (G_TYPE_CHECK_INSTANCE_CAST((obj),GST_TYPE_VIDEO_DECODER,GstVideoDecoder))
  33. #define GST_VIDEO_DECODER_CLASS(klass) \
  34. (G_TYPE_CHECK_CLASS_CAST((klass),GST_TYPE_VIDEO_DECODER,GstVideoDecoderClass))
  35. #define GST_VIDEO_DECODER_GET_CLASS(obj) \
  36. (G_TYPE_INSTANCE_GET_CLASS((obj),GST_TYPE_VIDEO_DECODER,GstVideoDecoderClass))
  37. #define GST_IS_VIDEO_DECODER(obj) \
  38. (G_TYPE_CHECK_INSTANCE_TYPE((obj),GST_TYPE_VIDEO_DECODER))
  39. #define GST_IS_VIDEO_DECODER_CLASS(obj) \
  40. (G_TYPE_CHECK_CLASS_TYPE((klass),GST_TYPE_VIDEO_DECODER))
  41. #define GST_VIDEO_DECODER_CAST(obj) ((GstVideoDecoder *)(obj))
  42. /**
  43. * GST_VIDEO_DECODER_SINK_NAME:
  44. *
  45. * The name of the templates for the sink pad.
  46. */
  47. #define GST_VIDEO_DECODER_SINK_NAME "sink"
  48. /**
  49. * GST_VIDEO_DECODER_SRC_NAME:
  50. *
  51. * The name of the templates for the source pad.
  52. */
  53. #define GST_VIDEO_DECODER_SRC_NAME "src"
  54. /**
  55. * GST_VIDEO_DECODER_SRC_PAD:
  56. * @obj: a #GstVideoDecoder
  57. *
  58. * Gives the pointer to the source #GstPad object of the element.
  59. */
  60. #define GST_VIDEO_DECODER_SRC_PAD(obj) (((GstVideoDecoder *) (obj))->srcpad)
  61. /**
  62. * GST_VIDEO_DECODER_SINK_PAD:
  63. * @obj: a #GstVideoDecoder
  64. *
  65. * Gives the pointer to the sink #GstPad object of the element.
  66. */
  67. #define GST_VIDEO_DECODER_SINK_PAD(obj) (((GstVideoDecoder *) (obj))->sinkpad)
  68. /**
  69. * GST_VIDEO_DECODER_FLOW_NEED_DATA:
  70. *
  71. * Returned while parsing to indicate more data is needed.
  72. **/
  73. #define GST_VIDEO_DECODER_FLOW_NEED_DATA GST_FLOW_CUSTOM_SUCCESS
  74. /**
  75. * GST_VIDEO_DECODER_INPUT_SEGMENT:
  76. * @obj: base decoder instance
  77. *
  78. * Gives the segment of the element.
  79. */
  80. #define GST_VIDEO_DECODER_INPUT_SEGMENT(obj) (GST_VIDEO_DECODER_CAST (obj)->input_segment)
  81. /**
  82. * GST_VIDEO_DECODER_OUTPUT_SEGMENT:
  83. * @obj: base decoder instance
  84. *
  85. * Gives the segment of the element.
  86. */
  87. #define GST_VIDEO_DECODER_OUTPUT_SEGMENT(obj) (GST_VIDEO_DECODER_CAST (obj)->output_segment)
  88. /**
  89. * GST_VIDEO_DECODER_STREAM_LOCK:
  90. * @decoder: video decoder instance
  91. *
  92. * Obtain a lock to protect the decoder function from concurrent access.
  93. */
  94. #define GST_VIDEO_DECODER_STREAM_LOCK(decoder) g_rec_mutex_lock (&GST_VIDEO_DECODER (decoder)->stream_lock)
  95. /**
  96. * GST_VIDEO_DECODER_STREAM_UNLOCK:
  97. * @decoder: video decoder instance
  98. *
  99. * Release the lock that protects the decoder function from concurrent access.
  100. */
  101. #define GST_VIDEO_DECODER_STREAM_UNLOCK(decoder) g_rec_mutex_unlock (&GST_VIDEO_DECODER (decoder)->stream_lock)
  102. typedef struct _GstVideoDecoder GstVideoDecoder;
  103. typedef struct _GstVideoDecoderClass GstVideoDecoderClass;
  104. typedef struct _GstVideoDecoderPrivate GstVideoDecoderPrivate;
  105. /* do not use this one, use macro below */
  106. GstFlowReturn _gst_video_decoder_error (GstVideoDecoder *dec, gint weight,
  107. GQuark domain, gint code,
  108. gchar *txt, gchar *debug,
  109. const gchar *file, const gchar *function,
  110. gint line);
  111. /**
  112. * GST_VIDEO_DECODER_ERROR:
  113. * @el: the base video decoder element that generates the error
  114. * @w: element defined weight of the error, added to error count
  115. * @domain: like CORE, LIBRARY, RESOURCE or STREAM (see #gstreamer-GstGError)
  116. * @code: error code defined for that domain (see #gstreamer-GstGError)
  117. * @text: the message to display (format string and args enclosed in
  118. * parentheses)
  119. * @debug: debugging information for the message (format string and args
  120. * enclosed in parentheses)
  121. * @ret: variable to receive return value
  122. *
  123. * Utility function that video decoder elements can use in case they encountered
  124. * a data processing error that may be fatal for the current "data unit" but
  125. * need not prevent subsequent decoding. Such errors are counted and if there
  126. * are too many, as configured in the context's max_errors, the pipeline will
  127. * post an error message and the application will be requested to stop further
  128. * media processing. Otherwise, it is considered a "glitch" and only a warning
  129. * is logged. In either case, @ret is set to the proper value to
  130. * return to upstream/caller (indicating either GST_FLOW_ERROR or GST_FLOW_OK).
  131. */
  132. #define GST_VIDEO_DECODER_ERROR(el, w, domain, code, text, debug, ret) \
  133. G_STMT_START { \
  134. gchar *__txt = _gst_element_error_printf text; \
  135. gchar *__dbg = _gst_element_error_printf debug; \
  136. GstVideoDecoder *__dec = GST_VIDEO_DECODER (el); \
  137. ret = _gst_video_decoder_error (__dec, w, GST_ ## domain ## _ERROR, \
  138. GST_ ## domain ## _ERROR_ ## code, __txt, __dbg, __FILE__, \
  139. GST_FUNCTION, __LINE__); \
  140. } G_STMT_END
  141. /**
  142. * GST_VIDEO_DECODER_MAX_ERRORS:
  143. *
  144. * Default maximum number of errors tolerated before signaling error.
  145. */
  146. #define GST_VIDEO_DECODER_MAX_ERRORS 10
  147. /**
  148. * GstVideoDecoder:
  149. *
  150. * The opaque #GstVideoDecoder data structure.
  151. */
  152. struct _GstVideoDecoder
  153. {
  154. /*< private >*/
  155. GstElement element;
  156. /*< protected >*/
  157. GstPad *sinkpad;
  158. GstPad *srcpad;
  159. /* protects all data processing, i.e. is locked
  160. * in the chain function, finish_frame and when
  161. * processing serialized events */
  162. GRecMutex stream_lock;
  163. /* MT-protected (with STREAM_LOCK) */
  164. GstSegment input_segment;
  165. GstSegment output_segment;
  166. GstVideoDecoderPrivate *priv;
  167. /*< private >*/
  168. void *padding[GST_PADDING_LARGE];
  169. };
  170. /**
  171. * GstVideoDecoderClass:
  172. * @open: Optional.
  173. * Called when the element changes to GST_STATE_READY.
  174. * Allows opening external resources.
  175. * @close: Optional.
  176. * Called when the element changes to GST_STATE_NULL.
  177. * Allows closing external resources.
  178. * @start: Optional.
  179. * Called when the element starts processing.
  180. * Allows opening external resources.
  181. * @stop: Optional.
  182. * Called when the element stops processing.
  183. * Allows closing external resources.
  184. * @set_format: Notifies subclass of incoming data format (caps).
  185. * @parse: Required for non-packetized input.
  186. * Allows chopping incoming data into manageable units (frames)
  187. * for subsequent decoding.
  188. * @reset: Optional.
  189. * Allows subclass (decoder) to perform post-seek semantics reset.
  190. * Deprecated.
  191. * @handle_frame: Provides input data frame to subclass.
  192. * @finish: Optional.
  193. * Called to request subclass to dispatch any pending remaining
  194. * data (e.g. at EOS or segment end). Sub-classes should be prepared
  195. * to handle new data afterward, or seamless segment processing will break.
  196. * @sink_event: Optional.
  197. * Event handler on the sink pad. This function should return
  198. * TRUE if the event was handled and should be discarded
  199. * (i.e. not unref'ed).
  200. * Subclasses should chain up to the parent implementation to
  201. * invoke the default handler.
  202. * @src_event: Optional.
  203. * Event handler on the source pad. This function should return
  204. * TRUE if the event was handled and should be discarded
  205. * (i.e. not unref'ed).
  206. * Subclasses should chain up to the parent implementation to
  207. * invoke the default handler.
  208. * @negotiate: Optional.
  209. * Negotiate with downstream and configure buffer pools, etc.
  210. * Subclasses should chain up to the parent implementation to
  211. * invoke the default handler.
  212. * @decide_allocation: Optional.
  213. * Setup the allocation parameters for allocating output
  214. * buffers. The passed in query contains the result of the
  215. * downstream allocation query.
  216. * Subclasses should chain up to the parent implementation to
  217. * invoke the default handler.
  218. * @propose_allocation: Optional.
  219. * Propose buffer allocation parameters for upstream elements.
  220. * Subclasses should chain up to the parent implementation to
  221. * invoke the default handler.
  222. * @flush: Optional.
  223. * Flush all remaining data from the decoder without
  224. * pushing it downstream. Since: 1.2
  225. * @sink_query: Optional.
  226. * Query handler on the sink pad. This function should
  227. * return TRUE if the query could be performed. Subclasses
  228. * should chain up to the parent implementation to invoke the
  229. * default handler. Since 1.4
  230. * @src_query: Optional.
  231. * Query handler on the source pad. This function should
  232. * return TRUE if the query could be performed. Subclasses
  233. * should chain up to the parent implementation to invoke the
  234. * default handler. Since 1.4
  235. *
  236. * Subclasses can override any of the available virtual methods or not, as
  237. * needed. At minimum @handle_frame needs to be overridden, and @set_format
  238. * and likely as well. If non-packetized input is supported or expected,
  239. * @parse needs to be overridden as well.
  240. */
  241. struct _GstVideoDecoderClass
  242. {
  243. /*< private >*/
  244. GstElementClass element_class;
  245. /*< public >*/
  246. gboolean (*open) (GstVideoDecoder *decoder);
  247. gboolean (*close) (GstVideoDecoder *decoder);
  248. gboolean (*start) (GstVideoDecoder *decoder);
  249. gboolean (*stop) (GstVideoDecoder *decoder);
  250. GstFlowReturn (*parse) (GstVideoDecoder *decoder,
  251. GstVideoCodecFrame *frame,
  252. GstAdapter *adapter,
  253. gboolean at_eos);
  254. gboolean (*set_format) (GstVideoDecoder *decoder,
  255. GstVideoCodecState * state);
  256. gboolean (*reset) (GstVideoDecoder *decoder,
  257. gboolean hard);
  258. GstFlowReturn (*finish) (GstVideoDecoder *decoder);
  259. GstFlowReturn (*handle_frame) (GstVideoDecoder *decoder,
  260. GstVideoCodecFrame *frame);
  261. gboolean (*sink_event) (GstVideoDecoder *decoder,
  262. GstEvent *event);
  263. gboolean (*src_event) (GstVideoDecoder *decoder,
  264. GstEvent *event);
  265. gboolean (*negotiate) (GstVideoDecoder *decoder);
  266. gboolean (*decide_allocation) (GstVideoDecoder *decoder, GstQuery *query);
  267. gboolean (*propose_allocation) (GstVideoDecoder *decoder, GstQuery * query);
  268. gboolean (*flush) (GstVideoDecoder *decoder);
  269. gboolean (*sink_query) (GstVideoDecoder *decoder,
  270. GstQuery *query);
  271. gboolean (*src_query) (GstVideoDecoder *decoder,
  272. GstQuery *query);
  273. /*< private >*/
  274. void *padding[GST_PADDING_LARGE-3];
  275. };
  276. GType gst_video_decoder_get_type (void);
  277. /* Context parameters */
  278. void gst_video_decoder_set_packetized (GstVideoDecoder * decoder,
  279. gboolean packetized);
  280. gboolean gst_video_decoder_get_packetized (GstVideoDecoder * decoder);
  281. void gst_video_decoder_set_estimate_rate (GstVideoDecoder * dec,
  282. gboolean enabled);
  283. gint gst_video_decoder_get_estimate_rate (GstVideoDecoder * dec);
  284. void gst_video_decoder_set_max_errors (GstVideoDecoder * dec,
  285. gint num);
  286. gint gst_video_decoder_get_max_errors (GstVideoDecoder * dec);
  287. void gst_video_decoder_set_needs_format (GstVideoDecoder * dec,
  288. gboolean enabled);
  289. gboolean gst_video_decoder_get_needs_format (GstVideoDecoder * dec);
  290. void gst_video_decoder_set_latency (GstVideoDecoder *decoder,
  291. GstClockTime min_latency,
  292. GstClockTime max_latency);
  293. void gst_video_decoder_get_latency (GstVideoDecoder *decoder,
  294. GstClockTime *min_latency,
  295. GstClockTime *max_latency);
  296. void gst_video_decoder_get_allocator (GstVideoDecoder *decoder,
  297. GstAllocator **allocator,
  298. GstAllocationParams *params);
  299. GstBufferPool *gst_video_decoder_get_buffer_pool (GstVideoDecoder *decoder);
  300. /* Object methods */
  301. GstVideoCodecFrame *gst_video_decoder_get_frame (GstVideoDecoder *decoder,
  302. int frame_number);
  303. GstVideoCodecFrame *gst_video_decoder_get_oldest_frame (GstVideoDecoder *decoder);
  304. GList * gst_video_decoder_get_frames (GstVideoDecoder *decoder);
  305. /* Parsing related methods */
  306. void gst_video_decoder_add_to_frame (GstVideoDecoder *decoder,
  307. int n_bytes);
  308. GstFlowReturn gst_video_decoder_have_frame (GstVideoDecoder *decoder);
  309. gsize gst_video_decoder_get_pending_frame_size (GstVideoDecoder *decoder);
  310. GstBuffer *gst_video_decoder_allocate_output_buffer (GstVideoDecoder * decoder);
  311. GstFlowReturn gst_video_decoder_allocate_output_frame (GstVideoDecoder *decoder,
  312. GstVideoCodecFrame *frame);
  313. GstVideoCodecState *gst_video_decoder_set_output_state (GstVideoDecoder *decoder,
  314. GstVideoFormat fmt, guint width, guint height,
  315. GstVideoCodecState *reference);
  316. GstVideoCodecState *gst_video_decoder_get_output_state (GstVideoDecoder *decoder);
  317. gboolean gst_video_decoder_negotiate (GstVideoDecoder * decoder);
  318. GstClockTimeDiff gst_video_decoder_get_max_decode_time (GstVideoDecoder *decoder,
  319. GstVideoCodecFrame *frame);
  320. gdouble gst_video_decoder_get_qos_proportion (GstVideoDecoder * decoder);
  321. GstFlowReturn gst_video_decoder_finish_frame (GstVideoDecoder *decoder,
  322. GstVideoCodecFrame *frame);
  323. GstFlowReturn gst_video_decoder_drop_frame (GstVideoDecoder *dec,
  324. GstVideoCodecFrame *frame);
  325. void gst_video_decoder_release_frame (GstVideoDecoder * dec,
  326. GstVideoCodecFrame * frame);
  327. void gst_video_decoder_merge_tags (GstVideoDecoder *decoder,
  328. const GstTagList *tags,
  329. GstTagMergeMode mode);
  330. G_END_DECLS
  331. #endif