Skip to content

Commit 96a3c79

Browse files
stnk20alalek
authored andcommitted
Merge pull request opencv#8914 from stnk20:gstreamer_yuv
Add gstreamer capture capability for some YUV formats (opencv#8914) * Add gstreamer capture capability for some YUV formats.(only for gstreamer-1.0) * avoid cross initialization error * add checking if pipeline is manualpipeline, for compatibility.
1 parent 6d1cdcf commit 96a3c79

File tree

1 file changed

+35
-3
lines changed

1 file changed

+35
-3
lines changed

modules/videoio/src/cap_gstreamer.cpp

Lines changed: 35 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -287,6 +287,8 @@ IplImage * CvCapture_GStreamer::retrieveFrame(int)
287287
}
288288

289289
int depth = 3;
290+
bool height_extend = false;
291+
290292
#if GST_VERSION_MAJOR > 0
291293
depth = 0;
292294
const gchar* name = gst_structure_get_name(structure);
@@ -295,9 +297,16 @@ IplImage * CvCapture_GStreamer::retrieveFrame(int)
295297
if (!name)
296298
return 0;
297299

298-
// we support 3 types of data:
300+
// we support 11 types of data:
299301
// video/x-raw, format=BGR -> 8bit, 3 channels
300302
// video/x-raw, format=GRAY8 -> 8bit, 1 channel
303+
// video/x-raw, format=UYVY -> 8bit, 2 channel
304+
// video/x-raw, format=YUY2 -> 8bit, 2 channel
305+
// video/x-raw, format=YVYU -> 8bit, 2 channel
306+
// video/x-raw, format=NV12 -> 8bit, 1 channel (height is 1.5x larger than true height)
307+
// video/x-raw, format=NV21 -> 8bit, 1 channel (height is 1.5x larger than true height)
308+
// video/x-raw, format=YV12 -> 8bit, 1 channel (height is 1.5x larger than true height)
309+
// video/x-raw, format=I420 -> 8bit, 1 channel (height is 1.5x larger than true height)
301310
// video/x-bayer -> 8bit, 1 channel
302311
// image/jpeg -> 8bit, mjpeg: buffer_size x 1 x 1
303312
// bayer data is never decoded, the user is responsible for that
@@ -311,6 +320,13 @@ IplImage * CvCapture_GStreamer::retrieveFrame(int)
311320
if (strcasecmp(format, "BGR") == 0) {
312321
depth = 3;
313322
}
323+
else if( (strcasecmp(format, "UYVY") == 0) || (strcasecmp(format, "YUY2") == 0) || (strcasecmp(format, "YVYU") == 0) ){
324+
depth = 2;
325+
}
326+
else if( (strcasecmp(format, "NV12") == 0) || (strcasecmp(format, "NV21") == 0) || (strcasecmp(format, "YV12") == 0) || (strcasecmp(format, "I420") == 0) ){
327+
depth = 1;
328+
height_extend = true;
329+
}
314330
else if(strcasecmp(format, "GRAY8") == 0){
315331
depth = 1;
316332
}
@@ -324,7 +340,11 @@ IplImage * CvCapture_GStreamer::retrieveFrame(int)
324340
}
325341
#endif
326342
if (depth > 0) {
327-
frame = cvCreateImageHeader(cvSize(width, height), IPL_DEPTH_8U, depth);
343+
if(height_extend){
344+
frame = cvCreateImageHeader(cvSize(width, height*3/2), IPL_DEPTH_8U, depth);
345+
}else{
346+
frame = cvCreateImageHeader(cvSize(width, height), IPL_DEPTH_8U, depth);
347+
}
328348
} else {
329349
gst_caps_unref(buffer_caps);
330350
return 0;
@@ -810,8 +830,20 @@ bool CvCapture_GStreamer::open( int type, const char* filename )
810830
"blue_mask", G_TYPE_INT, 0xFF0000,
811831
NULL);
812832
#else
813-
// support 1 and 3 channel 8 bit data, as well as bayer (also 1 channel, 8bit)
833+
814834
caps = gst_caps_from_string("video/x-raw, format=(string){BGR, GRAY8}; video/x-bayer,format=(string){rggb,bggr,grbg,gbrg}; image/jpeg");
835+
836+
if(manualpipeline){
837+
GstPad* sink_pad = gst_element_get_static_pad(sink, "sink");
838+
GstCaps* peer_caps = gst_pad_peer_query_caps(sink_pad,NULL);
839+
if (!gst_caps_can_intersect(caps, peer_caps)) {
840+
gst_caps_unref(caps);
841+
caps = gst_caps_from_string("video/x-raw, format=(string){UYVY,YUY2,YVYU,NV12,NV21,YV12,I420}");
842+
}
843+
gst_object_unref(sink_pad);
844+
gst_caps_unref(peer_caps);
845+
}
846+
815847
#endif
816848
gst_app_sink_set_caps(GST_APP_SINK(sink), caps);
817849
gst_caps_unref(caps);

0 commit comments

Comments
 (0)