• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2008 Jan Schmidt <thaytan@noraisin.net>
3  */
4 
5 #ifdef HAVE_CONFIG_H
6 #  include <config.h>
7 #endif
8 
9 #include <gst/gst.h>
10 #include <gst/glib-compat-private.h>
11 #include <gst/video/video.h>
12 #include <string.h>
13 
14 #include "rsnparsetter.h"
15 
16 GST_DEBUG_CATEGORY_STATIC (rsn_parsetter_debug);
17 #define GST_CAT_DEFAULT rsn_parsetter_debug
18 
19 static GstStaticPadTemplate sink_factory = GST_STATIC_PAD_TEMPLATE ("sink",
20     GST_PAD_SINK,
21     GST_PAD_ALWAYS,
22     GST_STATIC_CAPS (GST_VIDEO_CAPS_MAKE (GST_VIDEO_FORMATS_ALL))
23     );
24 
25 static GstStaticPadTemplate src_factory = GST_STATIC_PAD_TEMPLATE ("src",
26     GST_PAD_SRC,
27     GST_PAD_ALWAYS,
28     GST_STATIC_CAPS (GST_VIDEO_CAPS_MAKE (GST_VIDEO_FORMATS_ALL))
29     );
30 
31 #define rsn_parsetter_parent_class parent_class
32 G_DEFINE_TYPE_WITH_CODE (RsnParSetter, rsn_parsetter, GST_TYPE_ELEMENT,
33     GST_DEBUG_CATEGORY_INIT (rsn_parsetter_debug, "rsnparsetter", 0,
34         "Resin DVD aspect ratio adjuster"));
35 
36 static void rsn_parsetter_finalize (GObject * object);
37 static GstFlowReturn rsn_parsetter_chain (GstPad * pad,
38     RsnParSetter * parset, GstBuffer * buf);
39 static gboolean rsn_parsetter_sink_event (GstPad * pad,
40     RsnParSetter * parset, GstEvent * event);
41 
42 static gboolean rsn_parsetter_src_query (GstPad * pad, RsnParSetter * parset,
43     GstQuery * query);
44 static GstCaps *rsn_parsetter_convert_caps (RsnParSetter * parset,
45     GstCaps * caps, gboolean widescreen);
46 static gboolean rsn_parsetter_check_caps (RsnParSetter * parset,
47     GstCaps * caps);
48 static void rsn_parsetter_update_caps (RsnParSetter * parset, GstCaps * caps);
49 
50 static void
rsn_parsetter_class_init(RsnParSetterClass * klass)51 rsn_parsetter_class_init (RsnParSetterClass * klass)
52 {
53   GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
54   GstElementClass *element_class = GST_ELEMENT_CLASS (klass);
55 
56   gobject_class->finalize = rsn_parsetter_finalize;
57 
58   gst_element_class_add_static_pad_template (element_class, &src_factory);
59   gst_element_class_add_static_pad_template (element_class, &sink_factory);
60 
61   gst_element_class_set_static_metadata (element_class,
62       "Resin Aspect Ratio Setter", "Filter/Video",
63       "Overrides caps on video buffers to force a particular display ratio",
64       "Jan Schmidt <thaytan@noraisin.net>");
65 }
66 
67 static void
rsn_parsetter_init(RsnParSetter * parset)68 rsn_parsetter_init (RsnParSetter * parset)
69 {
70   parset->sinkpad = gst_pad_new_from_static_template (&sink_factory, "sink");
71   gst_pad_set_chain_function (parset->sinkpad,
72       (GstPadChainFunction) GST_DEBUG_FUNCPTR (rsn_parsetter_chain));
73   gst_pad_set_event_function (parset->sinkpad,
74       (GstPadEventFunction) GST_DEBUG_FUNCPTR (rsn_parsetter_sink_event));
75   GST_PAD_SET_PROXY_CAPS (parset->sinkpad);
76   GST_PAD_SET_PROXY_ALLOCATION (parset->sinkpad);
77   gst_element_add_pad (GST_ELEMENT (parset), parset->sinkpad);
78 
79   parset->srcpad = gst_pad_new_from_static_template (&src_factory, "src");
80   gst_pad_set_query_function (parset->srcpad,
81       (GstPadQueryFunction) GST_DEBUG_FUNCPTR (rsn_parsetter_src_query));
82   GST_PAD_SET_PROXY_CAPS (parset->srcpad);
83   gst_element_add_pad (GST_ELEMENT (parset), parset->srcpad);
84 }
85 
86 static void
rsn_parsetter_finalize(GObject * object)87 rsn_parsetter_finalize (GObject * object)
88 {
89   RsnParSetter *parset = RSN_PARSETTER (object);
90 
91   gst_caps_replace (&parset->outcaps, NULL);
92   gst_caps_replace (&parset->in_caps_last, NULL);
93   gst_caps_replace (&parset->in_caps_converted, NULL);
94 
95   G_OBJECT_CLASS (parent_class)->finalize (object);
96 }
97 
98 static GstFlowReturn
rsn_parsetter_chain(GstPad * pad,RsnParSetter * parset,GstBuffer * buf)99 rsn_parsetter_chain (GstPad * pad, RsnParSetter * parset, GstBuffer * buf)
100 {
101   return gst_pad_push (parset->srcpad, buf);
102 }
103 
104 static gboolean
rsn_parsetter_sink_event(GstPad * pad,RsnParSetter * parset,GstEvent * event)105 rsn_parsetter_sink_event (GstPad * pad, RsnParSetter * parset, GstEvent * event)
106 {
107   switch (GST_EVENT_TYPE (event)) {
108     case GST_EVENT_CUSTOM_DOWNSTREAM:{
109       const GstStructure *structure = gst_event_get_structure (event);
110 
111       if (structure != NULL &&
112           gst_structure_has_name (structure, "application/x-gst-dvd")) {
113         const char *type = gst_structure_get_string (structure, "event");
114         GstEvent *caps_event = NULL;
115 
116         if (type == NULL)
117           goto out;
118 
119         if (strcmp (type, "dvd-video-format") == 0) {
120           gboolean is_widescreen;
121 
122           gst_structure_get_boolean (structure, "video-widescreen",
123               &is_widescreen);
124 
125           GST_DEBUG_OBJECT (parset, "Video is %s",
126               parset->is_widescreen ? "16:9" : "4:3");
127 
128           if (parset->in_caps_last && parset->is_widescreen != is_widescreen) {
129             /* Force caps check */
130             gst_caps_replace (&parset->in_caps_converted, NULL);
131             rsn_parsetter_update_caps (parset, parset->in_caps_last);
132             if (parset->override_outcaps)
133               caps_event = gst_event_new_caps (parset->outcaps);
134           }
135           parset->is_widescreen = is_widescreen;
136 
137           /* FIXME: Added for testing: */
138           // parset->is_widescreen = FALSE;
139 
140           if (caps_event)
141             gst_pad_push_event (parset->srcpad, caps_event);
142         }
143       }
144       break;
145     }
146     case GST_EVENT_CAPS:
147     {
148       GstCaps *caps = NULL;
149       gst_event_parse_caps (event, &caps);
150       rsn_parsetter_update_caps (parset, caps);
151       if (parset->override_outcaps) {
152         gst_event_unref (event);
153         GST_DEBUG_OBJECT (parset,
154             "Handling caps event. Overriding upstream caps"
155             " with %" GST_PTR_FORMAT, parset->outcaps);
156         event = gst_event_new_caps (parset->outcaps);
157       } else {
158         GST_DEBUG_OBJECT (parset,
159             "Handling caps event. Upstream caps %" GST_PTR_FORMAT
160             " acceptable", caps);
161       }
162       break;
163     }
164     default:
165       break;
166   }
167 
168 out:
169   return gst_pad_event_default (pad, (GstObject *) (parset), event);
170 }
171 
172 static gboolean
rsn_parsetter_src_query(GstPad * pad,RsnParSetter * parset,GstQuery * query)173 rsn_parsetter_src_query (GstPad * pad, RsnParSetter * parset, GstQuery * query)
174 {
175   GstCaps *caps = NULL;
176 
177   if (!gst_pad_peer_query (parset->sinkpad, query))
178     return FALSE;
179 
180   if (GST_QUERY_TYPE (query) != GST_QUERY_CAPS)
181     return TRUE;
182 
183   gst_query_parse_caps_result (query, &caps);
184 
185   GST_DEBUG_OBJECT (parset, "Handling caps query. Upstream caps %"
186       GST_PTR_FORMAT, caps);
187 
188   if (caps == NULL) {
189     GstCaps *templ_caps = gst_pad_get_pad_template_caps (pad);
190     gst_query_set_caps_result (query, templ_caps);
191     gst_caps_unref (templ_caps);
192   } else {
193     caps = rsn_parsetter_convert_caps (parset, caps, parset->is_widescreen);
194     gst_query_set_caps_result (query, caps);
195     gst_caps_unref (caps);
196   }
197 
198   return TRUE;
199 }
200 
201 /* Check if the DAR of the passed matches the required DAR */
202 static gboolean
rsn_parsetter_check_caps(RsnParSetter * parset,GstCaps * caps)203 rsn_parsetter_check_caps (RsnParSetter * parset, GstCaps * caps)
204 {
205   GstStructure *s;
206   gint width, height;
207   gint par_n, par_d;
208   guint dar_n, dar_d;
209   gboolean ret = FALSE;
210 
211   if (parset->in_caps_last &&
212       (caps == parset->in_caps_last ||
213           gst_caps_is_equal (caps, parset->in_caps_last))) {
214     ret = parset->in_caps_was_ok;
215     goto out;
216   }
217 
218   /* Calculate the DAR from the incoming caps, and return TRUE if it matches
219    * the required DAR, FALSE if not */
220   s = gst_caps_get_structure (caps, 0);
221   if (s == NULL)
222     goto out;
223 
224   if (!gst_structure_get_int (s, "width", &width) ||
225       !gst_structure_get_int (s, "height", &height))
226     goto out;
227 
228   if (!gst_structure_get_fraction (s, "pixel-aspect-ratio", &par_n, &par_d))
229     par_n = par_d = 1;
230 
231   if (!gst_video_calculate_display_ratio (&dar_n, &dar_d, width, height,
232           par_n, par_d, 1, 1))
233     goto out;
234 
235   GST_DEBUG_OBJECT (parset,
236       "Incoming video caps now: w %d h %d PAR %d/%d = DAR %d/%d",
237       width, height, par_n, par_d, dar_n, dar_d);
238 
239   if (parset->is_widescreen) {
240     if (dar_n == 16 && dar_d == 9)
241       ret = TRUE;
242   } else {
243     if (dar_n == 4 && dar_d == 3)
244       ret = TRUE;
245   }
246 
247   gst_caps_replace (&parset->in_caps_last, caps);
248   gst_caps_replace (&parset->in_caps_converted, NULL);
249   parset->in_caps_was_ok = ret;
250 
251 out:
252   return ret;
253 }
254 
255 static GstCaps *
rsn_parsetter_convert_caps(RsnParSetter * parset,GstCaps * caps,gboolean widescreen)256 rsn_parsetter_convert_caps (RsnParSetter * parset, GstCaps * caps,
257     gboolean widescreen)
258 {
259   /* Duplicate the given caps, with a PAR that provides the desired DAR */
260   GstCaps *outcaps;
261   GstStructure *s;
262   gint width, height;
263   gint par_n, par_d;
264   guint dar_n, dar_d;
265   GValue par = { 0, };
266 
267   if (caps == parset->in_caps_last && parset->in_caps_converted) {
268     outcaps = gst_caps_ref (parset->in_caps_converted);
269     goto out;
270   }
271 
272   outcaps = gst_caps_copy (caps);
273 
274   /* Calculate the DAR from the incoming caps, and return TRUE if it matches
275    * the required DAR, FALSE if not */
276   s = gst_caps_get_structure (outcaps, 0);
277   if (s == NULL)
278     goto out;
279 
280   if (!gst_structure_get_int (s, "width", &width) ||
281       !gst_structure_get_int (s, "height", &height))
282     goto out;
283 
284   if (widescreen) {
285     dar_n = 16;
286     dar_d = 9;
287   } else {
288     dar_n = 4;
289     dar_d = 3;
290   }
291 
292   par_n = dar_n * height;
293   par_d = dar_d * width;
294 
295   g_value_init (&par, GST_TYPE_FRACTION);
296   gst_value_set_fraction (&par, par_n, par_d);
297   gst_structure_set_value (s, "pixel-aspect-ratio", &par);
298   g_value_unset (&par);
299 
300   gst_caps_replace (&parset->in_caps_converted, outcaps);
301 out:
302   return outcaps;
303 }
304 
305 static void
rsn_parsetter_update_caps(RsnParSetter * parset,GstCaps * caps)306 rsn_parsetter_update_caps (RsnParSetter * parset, GstCaps * caps)
307 {
308   /* Check the new incoming caps against our current DAR, and mark
309    * whether the caps need adjusting */
310   if (rsn_parsetter_check_caps (parset, caps)) {
311     parset->override_outcaps = FALSE;
312     gst_caps_replace (&parset->outcaps, caps);
313   } else {
314     GstCaps *override_caps = rsn_parsetter_convert_caps (parset, caps,
315         parset->is_widescreen);
316     if (parset->outcaps)
317       gst_caps_unref (parset->outcaps);
318     parset->outcaps = override_caps;
319 
320     parset->override_outcaps = TRUE;
321   }
322 
323   GST_DEBUG_OBJECT (parset, "caps changed: need_override now = %d",
324       parset->override_outcaps);
325 }
326