Grab frames in test apps.

How can I save the frames coming through the deepstream-test3-app pipeline to jpeg images?

GstBuffer *buf = (GstBuffer *) info->data;
          write(buf); //C++ extern function to write image

I have written one extern function in C++ which gets invoked from deepstream_test3_app.
But not sure about conversion from GstBuffer* to cv::Mat in C++ code.