vendredi 13 décembre 2013

STL String to/from wide string

Yesterday I spend sometimes to help one of my teammate with a piece of code he wrote to convert a std::string  into a std::wstring. At the beginning it seems to be a complex issue for him and he used a complex C based code managing wchar buffer convert char to wchar with mbstowcs.

But finally as often the STL provide everything we need to convert one string type into the other and it’s quite simple.

I quickly use Ideone to demonstrate how to deal with:

As you see we can just use the iterator (old-style or new style like below)

wstring ws(s.begin(), s.end()); or wstring ws(begin(s), end(s));

lundi 2 décembre 2013

Redirect X from remote linux machine on your windows host.

When you use a remote linux machine from your own Windows Desktop machine using a putty connection, you can easily redirect X application on your desktop.

To do that, there is several steps:

1) You need to have a cygwin setup on your windows host, and follow the following instruction to setup all package required to run a X server.

2) Setup your putty connection to allow X11 forwarding.

Putty-Enable-X11Forwarding

To check if everything is OK, restart a connection and from linux prompt, check the value of “DISPLAY” (echo $DISPLAY). It should return something similar to:

localhost:13.0

3) To start your windows X11 server, create a shortcut file in your cygwin folder.

the target will be: C:\ cygwin\bin\run.exe /usr/bin/bash.exe -l -c /bin/startxwin.exe

And for its name, “StartX” is a good choice!

Double-click on that shortcut and check that you now have the X11-server icon in your Windows systray.

4) To run that server every time you start your window session, save a copy of that shortcut in:

%USERPROFILE%\AppData\Roaming\Microsoft\Windows\Start Menu\Programs\Startup

5) Final check, start xclock from your Linux prompt, the xclock windows will appear on your Windows Desktop !

vendredi 29 novembre 2013

Text processing: Word List Challenge.

I read this morning a question on Stackoverflow where user tried to solve that text processing challenge with a really complex code.

With only 3 line of code we can do that in C++. Using STL and optionally a BOOST header.

It works in 3 steps:

1) transform to lower case,

2) split using multiple delimiters (boost::any_of),

3) sort the resulting vector.

….

#include <iostream>
#include <string>
#include <vector>
#include <iterator>
#include <algorithm>
using namespace std;

#include "boost/algorithm/string.hpp"

const char* testStr = "This is. a sample piece of, text to illustrate \n this problem.";

int main(int argc, char* argv[])
{
string inputText(testStr);
cout << inputText << endl << "*******************" << endl;
vector<string> strs;
boost::split(strs,boost::algorithm::to_lower_copy(inputText),boost::is_any_of("\t ,\n,,,."),boost::token_compress_on);
cout << "list : " << strs.size() << endl;
cout << "without sort\n", copy(begin(strs), end(strs), ostream_iterator<string>(cout, "\n"));
std::sort(strs.begin(), strs.end());
cout << "with sort\n", copy(begin(strs), end(strs), ostream_iterator<string>(cout, "\n"));


return 0;
}

lundi 25 novembre 2013

Override a default STL operator >>

In that question, user need to override the default STL operator >> for complex number. First keep in mind that the STL provide a template for complex number define in <complex> and that this type come with its own syntax for I/O.

By default the syntax is (real,imag).

As the user want to use: real+imagi, we have to change the default behavior.

In the following you will see how I wrote a new global function to override the default operator and how I use a Regular Expression to extract the real and imaginary number.

template<class _Ty> inline istream& operator>>(istream& _Istr, complex<_Ty>& _Right)
{
// extract a complex<_Ty> with syntax REAL+IMAGi
long double _Real = 0;
long double _Imag = 0;
string s; _Istr >> s;
string regexPattern = string(R"r(([-+]?\d+\.?\d*|[-+]?\d*\.?\d+)\s*\+\s*([-+]?\d+\.?\d*|[-+]?\d*\.?\d+)i)r");
regex reg(regexPattern);
smatch sm; // same as std::match_results<const char*> cm;
regex_match (s,sm,reg);

_Real = std::atof(sm[1].str().c_str());
_Imag = std::atof(sm[2].str().c_str());

_Right = std::complex<_Ty>((_Ty)(_Real), (_Ty)(_Imag));

return (_Istr);
}


There is place for improvement in that version:


  • no match

  • match are not number

  • etc…

But it’s a good example of how to start!

vendredi 22 novembre 2013

FFMPEG from Compressed to RAW video.

I f consult some of my previous post, you may noticed that I’m an ffmpeg fan.

I often used ffmpeg command line to pipe raw video, in my own soft or some soft I develop in my professional environment. But I recently discover a good to share trick about ffmpeg.

Imagine a video file of 20min at 24fps, you have extract those information using the ffmpeg –i command. But when you extract the raw video frame in a file, you discover that the output file is bigger than expected (like a Tardis….).

Example of simple decoding as YUV 420p:

ffmpeg –I input.mp4 –c:v raw_video –pix_fmt yuv420p – >> output.yuv

the reason can be that if the input file hasn’t PTS corresponding to a Constant Frame-Rate (CFR), ffmpeg duplicate or drop some frames.

hopefully ffmpeg is explicit and full of useful information to troubleshoot those situation, for example in the following ffmpeg output latest line we see that it “duplicate” 60 frame.

frame=  119 fps=0.0 q=0.0 Lsize=  160650kB time=00:00:02.00 bitrate=657102.5kbits/s dup=60 drop=0

To prevent that the parameter to use is “-vsync”.


-vsync parameter

Video sync method. For compatibility reasons old values can be specified as numbers. Newly added values will have to be specified as strings always.

0, passthrough

Each frame is passed with its timestamp from the demuxer to the muxer.

1, cfr

Frames will be duplicated and dropped to achieve exactly the requested constant frame rate.

2, vfr

Frames are passed through with their timestamp or dropped so as to prevent 2 frames from having the same timestamp.

drop

As passthrough but destroys all timestamps, making the muxer generate fresh timestamps based on frame-rate.

-1, auto

Chooses between 1 and 2 depending on muxer capabilities. This is the default method.


So to only have the encoded frame in our output the correct line is:

ffmpeg –I input.mp4 –vsync 0 –c:v raw_video –pix_fmt yuv420p – >> output.yuv

OpenMP support in different compiler.

OpenMP is a powerful and useful standard supported by many compiler to introduce parallelism (multi-threading) in your C/C++ source code with “minimal effort.

But, depending of your developing environment, you may face some problems and the 1st from my POV is that if you compile the same source code with GCC/G++ and Visual Studio C++ compiler you cannot use the complete and last OpenMP functionnalities.

The reason is that VS' C/C++ compiler (even the last version VS’2013) only support OpenMP 2.0. It’s an old version released in March 2002.

While OpenMp support in VS was frozen since its 1st introduction in VS’2005, GCC/G++ support evolve and a recent GCC version like v4.9 support the last OpenMP v4.0 (July 2013).

You can consul that page to find which version of OpenMP is supported in the GCC version you used to build.

Note that CLang also provide OpenMP support but only for the v3.1.

---------------------------------

So, to conclude if you develop for several compiler and want to share your source code you should choose to keep using the ‘old’ v2.0. It’s a bit silly because the 3.1 and v4 introduce a lot of new functionalities like task, simd, device, etc …

Extract PTS from a video stream

 

I often used ffmpeg to extract raw_video data from compressed file or device stream, but when we do that we lost a useful information the PTS (presentation timestamp) of each frame.

For example, if you have a program using the output of ffmpeg (pipe mode),

ffprobe –show_frames –select_streams v:0 input.ts | grep pkt_pts_time= >> output.pts

output.pts file will contain:

pkt_pts_time=0.000000
pkt_pts_time=0.034022
pkt_pts_time=0.067689
…..

those number are in seconds, so you can use them easily in any software you develop.