gstaudiodecoder.h 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406
  1. /* GStreamer
  2. * Copyright (C) 2009 Igalia S.L.
  3. * Author: Iago Toral Quiroga <itoral@igalia.com>
  4. * Copyright (C) 2011 Mark Nauwelaerts <mark.nauwelaerts@collabora.co.uk>.
  5. * Copyright (C) 2011 Nokia Corporation. All rights reserved.
  6. * Contact: Stefan Kost <stefan.kost@nokia.com>
  7. *
  8. * This library is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU Library General Public
  10. * License as published by the Free Software Foundation; either
  11. * version 2 of the License, or (at your option) any later version.
  12. *
  13. * This library is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  16. * Library General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU Library General Public
  19. * License along with this library; if not, write to the
  20. * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
  21. * Boston, MA 02110-1301, USA.
  22. */
  23. #ifndef __GST_AUDIO_AUDIO_H__
  24. #include <gst/audio/audio.h>
  25. #endif
  26. #ifndef _GST_AUDIO_DECODER_H_
  27. #define _GST_AUDIO_DECODER_H_
  28. #include <gst/gst.h>
  29. #include <gst/base/gstadapter.h>
  30. G_BEGIN_DECLS
  31. #define GST_TYPE_AUDIO_DECODER \
  32. (gst_audio_decoder_get_type())
  33. #define GST_AUDIO_DECODER(obj) \
  34. (G_TYPE_CHECK_INSTANCE_CAST((obj),GST_TYPE_AUDIO_DECODER,GstAudioDecoder))
  35. #define GST_AUDIO_DECODER_CLASS(klass) \
  36. (G_TYPE_CHECK_CLASS_CAST((klass),GST_TYPE_AUDIO_DECODER,GstAudioDecoderClass))
  37. #define GST_AUDIO_DECODER_GET_CLASS(obj) \
  38. (G_TYPE_INSTANCE_GET_CLASS((obj),GST_TYPE_AUDIO_DECODER,GstAudioDecoderClass))
  39. #define GST_IS_AUDIO_DECODER(obj) \
  40. (G_TYPE_CHECK_INSTANCE_TYPE((obj),GST_TYPE_AUDIO_DECODER))
  41. #define GST_IS_AUDIO_DECODER_CLASS(obj) \
  42. (G_TYPE_CHECK_CLASS_TYPE((klass),GST_TYPE_AUDIO_DECODER))
  43. #define GST_AUDIO_DECODER_CAST(obj) \
  44. ((GstAudioDecoder *)(obj))
  45. /**
  46. * GST_AUDIO_DECODER_SINK_NAME:
  47. *
  48. * The name of the templates for the sink pad.
  49. */
  50. #define GST_AUDIO_DECODER_SINK_NAME "sink"
  51. /**
  52. * GST_AUDIO_DECODER_SRC_NAME:
  53. *
  54. * The name of the templates for the source pad.
  55. */
  56. #define GST_AUDIO_DECODER_SRC_NAME "src"
  57. /**
  58. * GST_AUDIO_DECODER_SRC_PAD:
  59. * @obj: base audio codec instance
  60. *
  61. * Gives the pointer to the source #GstPad object of the element.
  62. */
  63. #define GST_AUDIO_DECODER_SRC_PAD(obj) (((GstAudioDecoder *) (obj))->srcpad)
  64. /**
  65. * GST_AUDIO_DECODER_SINK_PAD:
  66. * @obj: base audio codec instance
  67. *
  68. * Gives the pointer to the sink #GstPad object of the element.
  69. */
  70. #define GST_AUDIO_DECODER_SINK_PAD(obj) (((GstAudioDecoder *) (obj))->sinkpad)
  71. #define GST_AUDIO_DECODER_STREAM_LOCK(dec) g_rec_mutex_lock (&GST_AUDIO_DECODER (dec)->stream_lock)
  72. #define GST_AUDIO_DECODER_STREAM_UNLOCK(dec) g_rec_mutex_unlock (&GST_AUDIO_DECODER (dec)->stream_lock)
  73. /**
  74. * GST_AUDIO_DECODER_INPUT_SEGMENT:
  75. * @obj: audio decoder instance
  76. *
  77. * Gives the input segment of the element.
  78. */
  79. #define GST_AUDIO_DECODER_INPUT_SEGMENT(obj) (GST_AUDIO_DECODER_CAST (obj)->input_segment)
  80. /**
  81. * GST_AUDIO_DECODER_OUTPUT_SEGMENT:
  82. * @obj: audio decoder instance
  83. *
  84. * Gives the output segment of the element.
  85. */
  86. #define GST_AUDIO_DECODER_OUTPUT_SEGMENT(obj) (GST_AUDIO_DECODER_CAST (obj)->output_segment)
  87. typedef struct _GstAudioDecoder GstAudioDecoder;
  88. typedef struct _GstAudioDecoderClass GstAudioDecoderClass;
  89. typedef struct _GstAudioDecoderPrivate GstAudioDecoderPrivate;
  90. /* do not use this one, use macro below */
  91. GstFlowReturn _gst_audio_decoder_error (GstAudioDecoder *dec, gint weight,
  92. GQuark domain, gint code,
  93. gchar *txt, gchar *debug,
  94. const gchar *file, const gchar *function,
  95. gint line);
  96. /**
  97. * GST_AUDIO_DECODER_ERROR:
  98. * @el: the base audio decoder element that generates the error
  99. * @weight: element defined weight of the error, added to error count
  100. * @domain: like CORE, LIBRARY, RESOURCE or STREAM (see #gstreamer-GstGError)
  101. * @code: error code defined for that domain (see #gstreamer-GstGError)
  102. * @text: the message to display (format string and args enclosed in
  103. * parentheses)
  104. * @debug: debugging information for the message (format string and args
  105. * enclosed in parentheses)
  106. * @ret: variable to receive return value
  107. *
  108. * Utility function that audio decoder elements can use in case they encountered
  109. * a data processing error that may be fatal for the current "data unit" but
  110. * need not prevent subsequent decoding. Such errors are counted and if there
  111. * are too many, as configured in the context's max_errors, the pipeline will
  112. * post an error message and the application will be requested to stop further
  113. * media processing. Otherwise, it is considered a "glitch" and only a warning
  114. * is logged. In either case, @ret is set to the proper value to
  115. * return to upstream/caller (indicating either GST_FLOW_ERROR or GST_FLOW_OK).
  116. */
  117. #define GST_AUDIO_DECODER_ERROR(el, weight, domain, code, text, debug, ret) \
  118. G_STMT_START { \
  119. gchar *__txt = _gst_element_error_printf text; \
  120. gchar *__dbg = _gst_element_error_printf debug; \
  121. GstAudioDecoder *__dec = GST_AUDIO_DECODER (el); \
  122. ret = _gst_audio_decoder_error (__dec, weight, GST_ ## domain ## _ERROR, \
  123. GST_ ## domain ## _ERROR_ ## code, __txt, __dbg, __FILE__, \
  124. GST_FUNCTION, __LINE__); \
  125. } G_STMT_END
  126. /**
  127. * GST_AUDIO_DECODER_MAX_ERRORS:
  128. *
  129. * Default maximum number of errors tolerated before signaling error.
  130. */
  131. #define GST_AUDIO_DECODER_MAX_ERRORS 10
  132. /**
  133. * GstAudioDecoder:
  134. *
  135. * The opaque #GstAudioDecoder data structure.
  136. */
  137. struct _GstAudioDecoder
  138. {
  139. GstElement element;
  140. /*< protected >*/
  141. /* source and sink pads */
  142. GstPad *sinkpad;
  143. GstPad *srcpad;
  144. /* protects all data processing, i.e. is locked
  145. * in the chain function, finish_frame and when
  146. * processing serialized events */
  147. GRecMutex stream_lock;
  148. /* MT-protected (with STREAM_LOCK) */
  149. GstSegment input_segment;
  150. GstSegment output_segment;
  151. /*< private >*/
  152. GstAudioDecoderPrivate *priv;
  153. gpointer _gst_reserved[GST_PADDING_LARGE];
  154. };
  155. /**
  156. * GstAudioDecoderClass:
  157. * @element_class: The parent class structure
  158. * @start: Optional.
  159. * Called when the element starts processing.
  160. * Allows opening external resources.
  161. * @stop: Optional.
  162. * Called when the element stops processing.
  163. * Allows closing external resources.
  164. * @set_format: Notifies subclass of incoming data format (caps).
  165. * @parse: Optional.
  166. * Allows chopping incoming data into manageable units (frames)
  167. * for subsequent decoding. This division is at subclass
  168. * discretion and may or may not correspond to 1 (or more)
  169. * frames as defined by audio format.
  170. * @handle_frame: Provides input data (or NULL to clear any remaining data)
  171. * to subclass. Input data ref management is performed by
  172. * base class, subclass should not care or intervene,
  173. * and input data is only valid until next call to base class,
  174. * most notably a call to gst_audio_decoder_finish_frame().
  175. * @flush: Optional.
  176. * Instructs subclass to clear any codec caches and discard
  177. * any pending samples and not yet returned decoded data.
  178. * @hard indicates whether a FLUSH is being processed,
  179. * or otherwise a DISCONT (or conceptually similar).
  180. * @sink_event: Optional.
  181. * Event handler on the sink pad. Subclasses should chain up to
  182. * the parent implementation to invoke the default handler.
  183. * @src_event: Optional.
  184. * Event handler on the src pad. Subclasses should chain up to
  185. * the parent implementation to invoke the default handler.
  186. * @pre_push: Optional.
  187. * Called just prior to pushing (encoded data) buffer downstream.
  188. * Subclass has full discretionary access to buffer,
  189. * and a not OK flow return will abort downstream pushing.
  190. * @open: Optional.
  191. * Called when the element changes to GST_STATE_READY.
  192. * Allows opening external resources.
  193. * @close: Optional.
  194. * Called when the element changes to GST_STATE_NULL.
  195. * Allows closing external resources.
  196. * @negotiate: Optional.
  197. * Negotiate with downstream and configure buffer pools, etc.
  198. * Subclasses should chain up to the parent implementation to
  199. * invoke the default handler.
  200. * @decide_allocation: Optional.
  201. * Setup the allocation parameters for allocating output
  202. * buffers. The passed in query contains the result of the
  203. * downstream allocation query.
  204. * Subclasses should chain up to the parent implementation to
  205. * invoke the default handler.
  206. * @propose_allocation: Optional.
  207. * Propose buffer allocation parameters for upstream elements.
  208. * Subclasses should chain up to the parent implementation to
  209. * invoke the default handler.
  210. * @sink_query: Optional.
  211. * Query handler on the sink pad. This function should
  212. * return TRUE if the query could be performed. Subclasses
  213. * should chain up to the parent implementation to invoke the
  214. * default handler. Since 1.6
  215. * @src_query: Optional.
  216. * Query handler on the source pad. This function should
  217. * return TRUE if the query could be performed. Subclasses
  218. * should chain up to the parent implementation to invoke the
  219. * default handler. Since 1.6
  220. * @getcaps: Optional.
  221. * Allows for a custom sink getcaps implementation.
  222. * If not implemented,
  223. * default returns gst_audio_decoder_proxy_getcaps
  224. * applied to sink template caps.
  225. * @transform_meta: Optional. Transform the metadata on the input buffer to the
  226. * output buffer. By default this method copies all meta without
  227. * tags and meta with only the "audio" tag. subclasses can
  228. * implement this method and return %TRUE if the metadata is to be
  229. * copied. Since 1.6
  230. *
  231. * Subclasses can override any of the available virtual methods or not, as
  232. * needed. At minimum @handle_frame (and likely @set_format) needs to be
  233. * overridden.
  234. */
  235. struct _GstAudioDecoderClass
  236. {
  237. GstElementClass element_class;
  238. /*< public >*/
  239. /* virtual methods for subclasses */
  240. gboolean (*start) (GstAudioDecoder *dec);
  241. gboolean (*stop) (GstAudioDecoder *dec);
  242. gboolean (*set_format) (GstAudioDecoder *dec,
  243. GstCaps *caps);
  244. GstFlowReturn (*parse) (GstAudioDecoder *dec,
  245. GstAdapter *adapter,
  246. gint *offset, gint *length);
  247. GstFlowReturn (*handle_frame) (GstAudioDecoder *dec,
  248. GstBuffer *buffer);
  249. void (*flush) (GstAudioDecoder *dec, gboolean hard);
  250. GstFlowReturn (*pre_push) (GstAudioDecoder *dec,
  251. GstBuffer **buffer);
  252. gboolean (*sink_event) (GstAudioDecoder *dec,
  253. GstEvent *event);
  254. gboolean (*src_event) (GstAudioDecoder *dec,
  255. GstEvent *event);
  256. gboolean (*open) (GstAudioDecoder *dec);
  257. gboolean (*close) (GstAudioDecoder *dec);
  258. gboolean (*negotiate) (GstAudioDecoder *dec);
  259. gboolean (*decide_allocation) (GstAudioDecoder *dec, GstQuery *query);
  260. gboolean (*propose_allocation) (GstAudioDecoder *dec,
  261. GstQuery * query);
  262. gboolean (*sink_query) (GstAudioDecoder *dec, GstQuery *query);
  263. gboolean (*src_query) (GstAudioDecoder *dec, GstQuery *query);
  264. GstCaps * (*getcaps) (GstAudioDecoder * dec,
  265. GstCaps * filter);
  266. gboolean (*transform_meta) (GstAudioDecoder *enc, GstBuffer *outbuf,
  267. GstMeta *meta, GstBuffer *inbuf);
  268. /*< private >*/
  269. gpointer _gst_reserved[GST_PADDING_LARGE - 4];
  270. };
  271. GType gst_audio_decoder_get_type (void);
  272. gboolean gst_audio_decoder_set_output_format (GstAudioDecoder * dec,
  273. const GstAudioInfo * info);
  274. GstCaps * gst_audio_decoder_proxy_getcaps (GstAudioDecoder * decoder,
  275. GstCaps * caps,
  276. GstCaps * filter);
  277. gboolean gst_audio_decoder_negotiate (GstAudioDecoder * dec);
  278. GstFlowReturn gst_audio_decoder_finish_frame (GstAudioDecoder * dec,
  279. GstBuffer * buf, gint frames);
  280. GstBuffer * gst_audio_decoder_allocate_output_buffer (GstAudioDecoder * dec,
  281. gsize size);
  282. /* context parameters */
  283. GstAudioInfo * gst_audio_decoder_get_audio_info (GstAudioDecoder * dec);
  284. void gst_audio_decoder_set_plc_aware (GstAudioDecoder * dec,
  285. gboolean plc);
  286. gint gst_audio_decoder_get_plc_aware (GstAudioDecoder * dec);
  287. void gst_audio_decoder_set_estimate_rate (GstAudioDecoder * dec,
  288. gboolean enabled);
  289. gint gst_audio_decoder_get_estimate_rate (GstAudioDecoder * dec);
  290. gint gst_audio_decoder_get_delay (GstAudioDecoder * dec);
  291. void gst_audio_decoder_set_max_errors (GstAudioDecoder * dec,
  292. gint num);
  293. gint gst_audio_decoder_get_max_errors (GstAudioDecoder * dec);
  294. void gst_audio_decoder_set_latency (GstAudioDecoder * dec,
  295. GstClockTime min,
  296. GstClockTime max);
  297. void gst_audio_decoder_get_latency (GstAudioDecoder * dec,
  298. GstClockTime * min,
  299. GstClockTime * max);
  300. void gst_audio_decoder_get_parse_state (GstAudioDecoder * dec,
  301. gboolean * sync,
  302. gboolean * eos);
  303. /* object properties */
  304. void gst_audio_decoder_set_plc (GstAudioDecoder * dec,
  305. gboolean enabled);
  306. gboolean gst_audio_decoder_get_plc (GstAudioDecoder * dec);
  307. void gst_audio_decoder_set_min_latency (GstAudioDecoder * dec,
  308. GstClockTime num);
  309. GstClockTime gst_audio_decoder_get_min_latency (GstAudioDecoder * dec);
  310. void gst_audio_decoder_set_tolerance (GstAudioDecoder * dec,
  311. GstClockTime tolerance);
  312. GstClockTime gst_audio_decoder_get_tolerance (GstAudioDecoder * dec);
  313. void gst_audio_decoder_set_drainable (GstAudioDecoder * dec,
  314. gboolean enabled);
  315. gboolean gst_audio_decoder_get_drainable (GstAudioDecoder * dec);
  316. void gst_audio_decoder_set_needs_format (GstAudioDecoder * dec,
  317. gboolean enabled);
  318. gboolean gst_audio_decoder_get_needs_format (GstAudioDecoder * dec);
  319. void gst_audio_decoder_get_allocator (GstAudioDecoder * dec,
  320. GstAllocator ** allocator,
  321. GstAllocationParams * params);
  322. void gst_audio_decoder_merge_tags (GstAudioDecoder * dec,
  323. const GstTagList * tags, GstTagMergeMode mode);
  324. void gst_audio_decoder_set_use_default_pad_acceptcaps (GstAudioDecoder * decoder,
  325. gboolean use);
  326. #ifdef G_DEFINE_AUTOPTR_CLEANUP_FUNC
  327. G_DEFINE_AUTOPTR_CLEANUP_FUNC(GstAudioDecoder, gst_object_unref)
  328. #endif
  329. G_END_DECLS
  330. #endif /* _GST_AUDIO_DECODER_H_ */