gstdspvpp.c 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309
  1. /*
  2. * Copyright (C) 2009-2010 Felipe Contreras
  3. *
  4. * Author: Felipe Contreras <felipe.contreras@gmail.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 "gstdspvpp.h"
  11. #include "gstdspparse.h"
  12. #include "plugin.h"
  13. #include "util.h"
  14. #include "dsp_bridge.h"
  15. #include "log.h"
  16. static GstDspBaseClass *parent_class;
  17. static inline GstCaps *
  18. generate_sink_template(void)
  19. {
  20. GstCaps *caps;
  21. GstStructure *struc;
  22. caps = gst_caps_new_empty();
  23. struc = gst_structure_new("video/x-raw-rgb",
  24. "bpp", G_TYPE_INT, 16, "depth", G_TYPE_INT, 16,
  25. "endianness", G_TYPE_INT, 1234,
  26. "red_mask", G_TYPE_INT, 63488,
  27. "green_mask", G_TYPE_INT, 2016,
  28. "blue_mask", G_TYPE_INT, 31,
  29. NULL);
  30. gst_caps_append_structure(caps, struc);
  31. return caps;
  32. }
  33. static inline GstCaps *
  34. generate_src_template(void)
  35. {
  36. GstCaps *caps;
  37. GstStructure *struc;
  38. caps = gst_caps_new_empty();
  39. struc = gst_structure_new("video/x-raw-yuv",
  40. "format", GST_TYPE_FOURCC, GST_MAKE_FOURCC('I', '4', '2', '0'),
  41. NULL);
  42. gst_caps_append_structure(caps, struc);
  43. return caps;
  44. }
  45. static void *
  46. create_node(GstDspBase *base)
  47. {
  48. GstDspVpp *self;
  49. struct td_codec *codec;
  50. int dsp_handle;
  51. struct dsp_node *node;
  52. const struct dsp_uuid usn_uuid = { 0x79A3C8B3, 0x95F2, 0x403F, 0x9A, 0x4B,
  53. { 0xCF, 0x80, 0x57, 0x73, 0x05, 0x41 } };
  54. self = GST_DSP_VPP(base);
  55. dsp_handle = base->dsp_handle;
  56. if (!gstdsp_register(dsp_handle, &usn_uuid, DSP_DCD_LIBRARYTYPE, "usn.dll64P")) {
  57. pr_err(self, "failed to register usn node library");
  58. return NULL;
  59. }
  60. codec = base->codec;
  61. if (!codec) {
  62. pr_err(self, "unknown algorithm");
  63. return NULL;
  64. }
  65. pr_info(base, "algo=%s", codec->filename);
  66. if (!gstdsp_register(dsp_handle, codec->uuid, DSP_DCD_LIBRARYTYPE, codec->filename)) {
  67. pr_err(self, "failed to register algo node library");
  68. return NULL;
  69. }
  70. if (!gstdsp_register(dsp_handle, codec->uuid, DSP_DCD_NODETYPE, codec->filename)) {
  71. pr_err(self, "failed to register algo node");
  72. return NULL;
  73. }
  74. {
  75. struct dsp_node_attr_in attrs = {
  76. .cb = sizeof(attrs),
  77. .priority = 5,
  78. .timeout = 1000,
  79. };
  80. void *arg_data;
  81. codec->create_args(base, &attrs.profile_id, &arg_data);
  82. if (!arg_data)
  83. return NULL;
  84. if (!dsp_node_allocate(dsp_handle, base->proc, codec->uuid, arg_data, &attrs, &node)) {
  85. pr_err(self, "dsp node allocate failed");
  86. free(arg_data);
  87. return NULL;
  88. }
  89. free(arg_data);
  90. }
  91. if (!dsp_node_create(dsp_handle, node)) {
  92. pr_err(self, "dsp node create failed");
  93. dsp_node_free(dsp_handle, node);
  94. return NULL;
  95. }
  96. pr_info(self, "dsp node created");
  97. if (codec->setup_params)
  98. codec->setup_params(base);
  99. if (codec->send_params)
  100. codec->send_params(base, node);
  101. return node;
  102. }
  103. static inline bool
  104. destroy_node(GstDspVpp *self,
  105. int dsp_handle,
  106. struct dsp_node *node)
  107. {
  108. if (node) {
  109. if (!dsp_node_free(dsp_handle, node)) {
  110. pr_err(self, "dsp node free failed");
  111. return false;
  112. }
  113. pr_info(self, "dsp node deleted");
  114. }
  115. return true;
  116. }
  117. static inline void
  118. configure_caps(GstDspVpp *self,
  119. GstCaps *in,
  120. GstCaps *out)
  121. {
  122. GstDspBase *base;
  123. GstStructure *out_struc, *in_struc;
  124. const GValue *aspect_ratio;
  125. const GValue *framerate;
  126. GstCaps *allowed_caps;
  127. base = GST_DSP_BASE(self);
  128. in_struc = gst_caps_get_structure(in, 0);
  129. out_struc = gst_structure_new("video/x-raw-yuv",
  130. "format", GST_TYPE_FOURCC, GST_MAKE_FOURCC('I', '4', '2', '0'),
  131. NULL);
  132. if (gst_structure_get_int(in_struc, "width", &self->width))
  133. self->out_width = self->width;
  134. if (gst_structure_get_int(in_struc, "height", &self->height))
  135. self->out_height = self->height;
  136. allowed_caps = gst_pad_get_allowed_caps(base->srcpad);
  137. if (allowed_caps) {
  138. if (gst_caps_get_size(allowed_caps) > 0) {
  139. GstStructure *s;
  140. s = gst_caps_get_structure(allowed_caps, 0);
  141. gst_structure_get_int(s, "width", &self->out_width);
  142. gst_structure_get_int(s, "height", &self->out_height);
  143. }
  144. gst_caps_unref(allowed_caps);
  145. }
  146. gst_structure_set(out_struc, "width", G_TYPE_INT, self->out_width, NULL);
  147. gst_structure_set(out_struc, "height", G_TYPE_INT, self->out_height, NULL);
  148. aspect_ratio = gst_structure_get_value(in_struc, "pixel-aspect-ratio");
  149. if (aspect_ratio)
  150. gst_structure_set_value(out_struc, "pixel-aspect-ratio", aspect_ratio);
  151. framerate = gst_structure_get_value(in_struc, "framerate");
  152. if (framerate)
  153. gst_structure_set_value(out_struc, "framerate", framerate);
  154. base->output_buffer_size = self->out_width * self->out_height * 1.5;
  155. gst_caps_append_structure(out, out_struc);
  156. }
  157. static gboolean
  158. sink_setcaps(GstPad *pad,
  159. GstCaps *caps)
  160. {
  161. GstDspVpp *self;
  162. GstDspBase *base;
  163. GstCaps *out_caps;
  164. gboolean ret;
  165. self = GST_DSP_VPP(GST_PAD_PARENT(pad));
  166. base = GST_DSP_BASE(self);
  167. {
  168. gchar *str = gst_caps_to_string(caps);
  169. pr_info(self, "sink caps: %s", str);
  170. g_free(str);
  171. }
  172. base->codec = &td_vpp_codec;
  173. du_port_alloc_buffers(base->ports[0], 4);
  174. du_port_alloc_buffers(base->ports[1], 4);
  175. out_caps = gst_caps_new_empty();
  176. configure_caps(self, caps, out_caps);
  177. base->tmp_caps = out_caps;
  178. ret = gst_pad_set_caps(pad, caps);
  179. if (!ret)
  180. return FALSE;
  181. return TRUE;
  182. }
  183. static void
  184. instance_init(GTypeInstance *instance,
  185. gpointer g_class)
  186. {
  187. GstDspBase *base;
  188. base = GST_DSP_BASE(instance);
  189. base->use_pad_alloc = TRUE;
  190. base->create_node = create_node;
  191. /* ugly, but needed */
  192. base->ports[1]->id = 3;
  193. gst_pad_set_setcaps_function(base->sinkpad, sink_setcaps);
  194. }
  195. static void
  196. base_init(gpointer g_class)
  197. {
  198. GstElementClass *element_class;
  199. GstPadTemplate *template;
  200. element_class = GST_ELEMENT_CLASS(g_class);
  201. gst_element_class_set_details_simple(element_class,
  202. "DSP VPP filter",
  203. "Filter/Converter/Video",
  204. "Converts video from different formats",
  205. "Felipe Contreras");
  206. template = gst_pad_template_new("src", GST_PAD_SRC,
  207. GST_PAD_ALWAYS,
  208. generate_src_template());
  209. gst_element_class_add_pad_template(element_class, template);
  210. gst_object_unref(template);
  211. template = gst_pad_template_new("sink", GST_PAD_SINK,
  212. GST_PAD_ALWAYS,
  213. generate_sink_template());
  214. gst_element_class_add_pad_template(element_class, template);
  215. gst_object_unref(template);
  216. }
  217. static void
  218. class_init(gpointer g_class,
  219. gpointer class_data)
  220. {
  221. parent_class = g_type_class_peek_parent(g_class);
  222. }
  223. GType
  224. gst_dsp_vpp_get_type(void)
  225. {
  226. static GType type;
  227. if (G_UNLIKELY(type == 0)) {
  228. GTypeInfo type_info = {
  229. .class_size = sizeof(GstDspVppClass),
  230. .class_init = class_init,
  231. .base_init = base_init,
  232. .instance_size = sizeof(GstDspVpp),
  233. .instance_init = instance_init,
  234. };
  235. type = g_type_register_static(GST_DSP_BASE_TYPE, "GstDspVpp", &type_info, 0);
  236. }
  237. return type;
  238. }