gstdspadec.c 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287
  1. /*
  2. * Copyright (C) 2010 Víctor M. Jáquez Leal
  3. *
  4. * Author: Víctor M. Jáquez Leal <vjaquez@igalia.com>
  5. *
  6. * This file may be used under the terms of the GNU Lesser General Public
  7. * License version 2.1, a copy of which is found in LICENSE included in the
  8. * packaging of this file.
  9. */
  10. #include "gstdspadec.h"
  11. #include "log.h"
  12. #include "util.h"
  13. #define GST_CAT_DEFAULT gstdsp_debug
  14. static GstDspBaseClass *parent_class;
  15. static inline GstCaps *
  16. generate_sink_template(void)
  17. {
  18. GstCaps *caps;
  19. GstStructure *struc;
  20. caps = gst_caps_new_empty();
  21. struc = gst_structure_new("audio/mpeg",
  22. "mpegversion", GST_TYPE_INT_RANGE, 1, 4,
  23. NULL);
  24. gst_caps_append_structure(caps, struc);
  25. return caps;
  26. }
  27. static inline GstCaps *
  28. generate_src_template(void)
  29. {
  30. GstCaps *caps;
  31. GstStructure *struc;
  32. caps = gst_caps_new_empty();
  33. struc = gst_structure_new("audio/x-raw-int",
  34. "endianness", G_TYPE_INT, G_BYTE_ORDER,
  35. "signed", G_TYPE_BOOLEAN, TRUE,
  36. "width", G_TYPE_INT, 16,
  37. "depth", G_TYPE_INT, 16,
  38. "rate", GST_TYPE_INT_RANGE, 8000, 96000,
  39. "channels", GST_TYPE_INT_RANGE, 1, 8,
  40. NULL);
  41. gst_caps_append_structure(caps, struc);
  42. return caps;
  43. }
  44. static void *
  45. create_node(GstDspBase *base)
  46. {
  47. GstDspADec *self;
  48. struct td_codec *codec;
  49. int dsp_handle;
  50. struct dsp_node *node;
  51. const struct dsp_uuid usn_uuid = { 0x79A3C8B3, 0x95F2, 0x403F, 0x9A, 0x4B,
  52. { 0xCF, 0x80, 0x57, 0x73, 0x05, 0x41 } };
  53. self = GST_DSP_ADEC(base);
  54. dsp_handle = base->dsp_handle;
  55. if (!gstdsp_register(dsp_handle, &usn_uuid, DSP_DCD_LIBRARYTYPE, "usn.dll64P")) {
  56. pr_err(self, "failed to register usn node library");
  57. return NULL;
  58. }
  59. codec = base->codec;
  60. if (!codec) {
  61. pr_err(self, "unknown algorithm");
  62. return NULL;
  63. }
  64. pr_info(base, "algo=%s", codec->filename);
  65. if (!gstdsp_register(dsp_handle, codec->uuid, DSP_DCD_LIBRARYTYPE, codec->filename)) {
  66. pr_err(self, "failed to register algo node library");
  67. return NULL;
  68. }
  69. if (!gstdsp_register(dsp_handle, codec->uuid, DSP_DCD_NODETYPE, codec->filename)) {
  70. pr_err(self, "failed to register algo node");
  71. return NULL;
  72. }
  73. {
  74. struct dsp_node_attr_in attrs = {
  75. .cb = sizeof(attrs),
  76. .priority = 10,
  77. .timeout = 10000,
  78. };
  79. void *arg_data;
  80. codec->create_args(base, &attrs.profile_id, &arg_data);
  81. if (!arg_data)
  82. return NULL;
  83. if (!dsp_node_allocate(dsp_handle, base->proc, codec->uuid, arg_data, &attrs, &node)) {
  84. pr_err(self, "dsp node allocate failed");
  85. free(arg_data);
  86. return NULL;
  87. }
  88. free(arg_data);
  89. }
  90. if (!dsp_node_create(dsp_handle, node)) {
  91. pr_err(self, "dsp node create failed");
  92. dsp_node_free(dsp_handle, node);
  93. return NULL;
  94. }
  95. pr_info(self, "dsp node created");
  96. if (codec->send_params)
  97. codec->send_params(base, node);
  98. if (codec->setup_params)
  99. codec->setup_params(base);
  100. base->flush_buffer = codec->flush_buffer;
  101. return node;
  102. }
  103. static inline void
  104. configure_caps(GstDspADec *self,
  105. GstCaps *in,
  106. GstCaps *out)
  107. {
  108. GstDspBase *base;
  109. GstStructure *out_struc, *in_struc;
  110. int channels;
  111. base = GST_DSP_BASE(self);
  112. in_struc = gst_caps_get_structure(in, 0);
  113. out_struc = gst_structure_new("audio/x-raw-int",
  114. "endianness", G_TYPE_INT, G_BYTE_ORDER,
  115. "signed", G_TYPE_BOOLEAN, TRUE,
  116. "width", G_TYPE_INT, 16,
  117. "depth", G_TYPE_INT, 16,
  118. NULL);
  119. if (gst_structure_get_int(in_struc, "channels", &channels))
  120. gst_structure_set(out_struc, "channels", G_TYPE_INT, channels, NULL);
  121. if (gst_structure_get_int(in_struc, "rate", &self->samplerate))
  122. gst_structure_set(out_struc, "rate", G_TYPE_INT, self->samplerate, NULL);
  123. if (base->alg == GSTDSP_AACDEC) {
  124. const char *fmt;
  125. gboolean tmp;
  126. gst_structure_get_boolean(in_struc, "framed", &tmp);
  127. self->packetized = tmp;
  128. fmt = gst_structure_get_string(in_struc, "stream-format");
  129. self->raw = strcmp(fmt, "raw") == 0;
  130. }
  131. base->output_buffer_size = 4 * 1024;
  132. gst_caps_append_structure(out, out_struc);
  133. }
  134. static gboolean
  135. sink_setcaps(GstPad *pad,
  136. GstCaps *caps)
  137. {
  138. GstDspADec *self;
  139. GstDspBase *base;
  140. GstStructure *in_struc;
  141. const char *name;
  142. GstCaps *out_caps;
  143. self = GST_DSP_ADEC(GST_PAD_PARENT(pad));
  144. base = GST_DSP_BASE(self);
  145. {
  146. gchar *str = gst_caps_to_string(caps);
  147. pr_info(self, "sink caps: %s", str);
  148. g_free(str);
  149. }
  150. in_struc = gst_caps_get_structure(caps, 0);
  151. name = gst_structure_get_name(in_struc);
  152. if (strcmp(name, "audio/mpeg") == 0) {
  153. int version = 1;
  154. gst_structure_get_int(in_struc, "mpegversion", &version);
  155. if (version == 2 || version == 4) {
  156. base->alg = GSTDSP_AACDEC;
  157. base->codec = &td_aacdec_codec;
  158. }
  159. }
  160. du_port_alloc_buffers(base->ports[0], 4);
  161. du_port_alloc_buffers(base->ports[1], 4);
  162. out_caps = gst_caps_new_empty();
  163. configure_caps(self, caps, out_caps);
  164. base->tmp_caps = out_caps;
  165. return TRUE;
  166. }
  167. static void
  168. instance_init(GTypeInstance *instance,
  169. gpointer g_class)
  170. {
  171. GstDspBase *base;
  172. base = GST_DSP_BASE(instance);
  173. base->use_pad_alloc = TRUE;
  174. base->create_node = create_node;
  175. gst_pad_set_setcaps_function(base->sinkpad, sink_setcaps);
  176. }
  177. static void
  178. base_init(gpointer g_class)
  179. {
  180. GstElementClass *element_class;
  181. GstPadTemplate *template;
  182. element_class = GST_ELEMENT_CLASS(g_class);
  183. gst_element_class_set_details_simple(element_class,
  184. "DSP audio decoder",
  185. "Codec/Decoder/Audio",
  186. "Decodes audio with TI's DSP algorithms",
  187. "Víctor M. Jáquez Leal");
  188. template = gst_pad_template_new("src", GST_PAD_SRC,
  189. GST_PAD_ALWAYS,
  190. generate_src_template());
  191. gst_element_class_add_pad_template(element_class, template);
  192. gst_object_unref(template);
  193. template = gst_pad_template_new("sink", GST_PAD_SINK,
  194. GST_PAD_ALWAYS,
  195. generate_sink_template());
  196. gst_element_class_add_pad_template(element_class, template);
  197. gst_object_unref(template);
  198. }
  199. static void
  200. class_init(gpointer g_class,
  201. gpointer class_data)
  202. {
  203. parent_class = g_type_class_peek_parent(g_class);
  204. }
  205. GType
  206. gst_dsp_adec_get_type(void)
  207. {
  208. static GType type;
  209. if (G_UNLIKELY(type == 0)) {
  210. GTypeInfo type_info = {
  211. .class_size = sizeof(GstDspADecClass),
  212. .class_init = class_init,
  213. .base_init = base_init,
  214. .instance_size = sizeof(GstDspADec),
  215. .instance_init = instance_init,
  216. };
  217. type = g_type_register_static(GST_DSP_BASE_TYPE, "GstDspADec", &type_info, 0);
  218. }
  219. return type;
  220. }