Fixed DevStudio 2005 build.
[opal.git] / samples / vidcodectest / main.cxx
blob551ff6ab48c840394220d60935690e2e292c8307
1 #include <ptlib.h>
3 #include <ptclib/pvfiledev.h>
4 #include <opal/transcoders.h>
5 #include <codec/vidcodec.h>
7 #include "intel.h"
9 #define MAJOR_VERSION 1
10 #define MINOR_VERSION 0
11 #define BUILD_TYPE ReleaseCode
12 #define BUILD_NUMBER 0
14 #define RAW_VIDEO_FORMAT OpalYUV420P
16 class VidCodecTest : public PProcess
18 PCLASSINFO(VidCodecTest, PProcess)
20 public:
21 VidCodecTest();
22 void Main();
25 namespace PWLibStupidLinkerHacks {
26 extern int opalLoader;
29 extern int intelLoader;
32 PCREATE_PROCESS(VidCodecTest);
34 VidCodecTest::VidCodecTest()
35 : PProcess("Post Increment", "VidCodecTest",
36 MAJOR_VERSION, MINOR_VERSION, BUILD_TYPE, BUILD_NUMBER)
38 PWLibStupidLinkerHacks::opalLoader = 1;
39 intelLoader = 1;
42 void ListCodecs()
44 OpalTranscoderList keys = OpalTranscoderFactory::GetKeyList();
45 OpalTranscoderList::const_iterator r;
46 for (r = keys.begin(); r != keys.end(); ++r) {
47 const OpalMediaFormatPair & transcoder = *r;
48 if (transcoder.GetInputFormat().GetDefaultSessionID() == OpalMediaFormat::DefaultVideoSessionID) {
49 cout << "Name: " << transcoder << "\n"
50 << " Input: " << transcoder.GetInputFormat() << "\n"
51 << " Output: " << transcoder.GetOutputFormat() << "\n";
56 ////////////////////////////////////////////////////////////////////////////////
58 void VidCodecTest::Main()
60 cout << GetName()
61 << " Version " << GetVersion(TRUE)
62 << " by " << GetManufacturer()
63 << " on " << GetOSClass() << ' ' << GetOSName()
64 << " (" << GetOSVersion() << '-' << GetOSHardware() << ")\n\n";
66 PConfigArgs args(GetArguments());
68 args.Parse(
69 #if PTRACING
70 "o-output:"
71 "t-trace."
72 #endif
73 "h-help."
74 , FALSE);
76 #if PTRACING
77 PTrace::Initialise(args.GetOptionCount('t'),
78 args.HasOption('o') ? (const char *)args.GetOptionString('o') : NULL);
79 #endif
81 if ((args.GetCount() == 1) && (args[0] *= "list")) {
82 ListCodecs();
83 return;
86 if (args.GetCount() < 4) {
87 PError << "usage: h263test 'encode'|'decode'|'xcode' codec infilename outfilename" << endl;
88 return;
91 char coding = tolower(args[0][0]);
93 PString inCodec, outCodec;
94 OpalTranscoder * encoder = NULL;
95 OpalTranscoder * decoder = NULL;
97 BOOL error = FALSE;
98 if (coding == 'd' || coding == 'x') {
99 inCodec = args[1];
100 //inCodec = "H.264-QCIF";
101 outCodec = RAW_VIDEO_FORMAT;
102 decoder = OpalTranscoder::Create(inCodec, outCodec);
103 if (decoder == NULL) {
104 PError << "error: unable to create decoder of " << inCodec << endl;
105 error = TRUE;
107 else {
108 cout << "Created decoder from " << inCodec << " to " << outCodec << endl;
111 if (coding == 'e' || coding == 'x') {
112 inCodec = RAW_VIDEO_FORMAT;
113 outCodec = args[1];
114 encoder = OpalTranscoder::Create(inCodec, outCodec);
115 if (encoder == NULL) {
116 PError << "error: unable to create encoder of " << outCodec << endl;
117 error = TRUE;
119 else
120 cout << "Created encoder from " << inCodec << " to " << outCodec << endl;
123 if (error) {
124 PError << "Valid transcoders are:";
125 ListCodecs();
126 return;
129 PYUVFile yuvIn;
130 PYUVFile yuvOut;
131 if (coding == 'e' || coding == 'x') {
132 if (!yuvIn.Open(args[2], PFile::ReadOnly, PFile::MustExist)) {
133 PError << "error: cannot open YUV input file " << args[2] << endl;
134 return;
137 if (coding == 'd' || coding == 'x') {
138 if (!yuvOut.Open(args[3], PFile::WriteOnly)) {
139 PError << "error: cannot open YUV output file " << args[3] << endl;
140 return;
144 if (coding == 'd') {
145 PError << "error: decoding not yet implemented" << endl;
146 return;
148 if (coding == 'e') {
149 PError << "error: encoding not yet implemented" << endl;
150 return;
152 if (coding == 'x') {
154 PINDEX frameCount = 0;
155 WORD sequence = 1;
157 for (;;) {
159 RTP_DataFrame yuvInFrame(sizeof(PluginCodec_Video_FrameHeader) + (yuvIn.GetWidth() * yuvOut.GetHeight() * 3) / 2);
160 RTP_DataFrameList encodedFrames, yuvOutFrame;
162 PluginCodec_Video_FrameHeader * header = (PluginCodec_Video_FrameHeader *)yuvInFrame.GetPayloadPtr();
164 header->x = 0;
165 header->y = 0;
166 header->width = yuvIn.GetWidth();
167 header->height = yuvIn.GetHeight();
169 if (!yuvIn.ReadFrame(OPAL_VIDEO_FRAME_DATA_PTR(header))) {
170 break;
173 encodedFrames.Append(new RTP_DataFrame(100000));
174 if (!encoder->ConvertFrames(yuvInFrame, encodedFrames)) {
175 PError << "error: encoder returned error" << endl;
176 break;
179 yuvOutFrame.RemoveAll();
180 PINDEX i;
181 for (i = 0; i < encodedFrames.GetSize(); ++i) {
182 if (!decoder->ConvertFrames(encodedFrames[i], yuvOutFrame)) {
183 PError << "error: decoder returned error" << endl;
184 break;
186 if (yuvOutFrame.GetSize() > 0)
187 break;
189 if (i != encodedFrames.GetSize()-1) {
190 PError << "warning: frame created from incomplete input frame list" << endl;
193 PluginCodec_Video_FrameHeader * headerOut = (PluginCodec_Video_FrameHeader *)yuvOutFrame[0].GetPayloadPtr();
195 if (!yuvOut.WriteFrame(OPAL_VIDEO_FRAME_DATA_PTR(headerOut))) {
196 PError << "error: output file write failed" << endl;
197 break;
200 ++frameCount;
203 cout << frameCount << " frames transcoded" << endl;