1 /*==================================================================
3 ! mardrone application AR-Drone for MeeGo
5 ! Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies).
8 ! Author:Kate Alhola kate.alhola@nokia.com
10 ! GNU Lesser General Public License Usage
11 ! This file may be used under the terms of the GNU Lesser
12 ! General Public License version 2.1 as published by the Free Software
13 ! Foundation and appearing in the file LICENSE.LGPL included in the
14 ! packaging of this file. Please review the following information to
15 ! ensure the GNU Lesser General Public License version 2.1 requirements
16 ! will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
20 *===================================================================*/
22 #include <QGraphicsView>
28 DroneVideo::DroneVideo()
30 droneHost.setAddress("192.168.1.1");
34 VideoThread::VideoThread(DroneVideo *parentp,QHostAddress host,QImage *_image)
37 qDebug() << "videoThread::videoThread";
40 videoSock=new QUdpSocket();
41 videoSock->bind(QHostAddress::Any,5555);
47 void DroneVideo::paint(QPainter *painter,const QStyleOptionGraphicsItem *option,
50 if(!initialized) { // We need initialize QImage here because we don't know display depth before
52 depth=painter->device()->depth();
53 qDebug() << "depth=" << depth;
56 image=new QImage(320,240, QImage::Format_RGB32);
57 image->fill(0x555555);
60 image=new QImage(320,240, QImage::Format_RGB16);
64 p.drawLine(0,0,image->width(),image->height());
65 p.drawLine(image->width(),0,0,image->height());
66 update(boundingRect());
67 videoThread=new VideoThread(this,droneHost,image);
70 painter->drawImage(boundingRect(),*image,image->rect());
73 QRectF DroneVideo::boundingRect() const
75 return QRectF(0.0,0.0,size().width(),size().height());
79 void VideoThread::run()
82 #define ACQ_HEIGHT 240
84 memset(&controller,0,sizeof(controller));
85 memset(&picture,0,sizeof(picture));
86 pictureWidth= image->width();
87 pictureHeight=image->height();
88 int codec_type=UVLC_CODEC;
89 qDebug() << "videoThread::run()";
90 stateTimer=new QTimer();
91 connect(stateTimer,SIGNAL(timeout()),this,SLOT(timer()));
92 connect(videoSock,SIGNAL(readyRead()),this,SLOT(videoDataReady()));
93 qDebug() << "videoThread::run() 2";
95 num_picture_decoded=0;
96 /// Picture configuration
98 picture.format = PIX_FMT_YUV420P;
99 picture.width = pictureWidth;
100 picture.height = pictureHeight;
101 picture.framerate = 30;
102 picture.y_buf = (uint8_t*)(void*)vp_os_malloc((size_t) pictureWidth*pictureHeight );
103 picture.cr_buf = (uint8_t*)vp_os_malloc( pictureWidth*pictureHeight/4 );
104 picture.cb_buf = (uint8_t*)vp_os_malloc( pictureWidth*pictureHeight/4 );
105 picture.y_line_size = pictureWidth;
106 picture.cb_line_size = pictureWidth / 2;
107 picture.cr_line_size = pictureWidth / 2;
111 qDebug() << "video_codec_open=" << video_codec_open(&controller, (codec_type_t)codec_type);
113 //stateTimer->start(1000);
114 qDebug() << "videoThread::run() initialized";
122 void VideoThread::timer()
124 // qDebug() << "thread Timer";
128 void VideoThread::sendVideoPort(QString cmd)
131 dgram=cmd.toLatin1();
132 qDebug() << "videoThread::sendCmd= " << cmd+"\n" << "to " << droneHost ;
133 videoSock->writeDatagram(dgram.data(),dgram.size(),droneHost,5555);
136 void VideoThread::videoDataReady()
139 QByteArray videoData;
143 videoData.resize(videoSock->pendingDatagramSize ());
144 l=videoSock->readDatagram(videoData.data(),videoData.size(),&host,&port);
145 // qDebug() << "videoThread::videoDataReady" <<" l=" << l << "from" << host ;
146 decodeTransform(videoData);
149 void VideoThread::decodeTransform(QByteArray &videoData)
151 controller.in_stream.bytes = (uint32_t*)videoData.data();
152 controller.in_stream.used = videoData.size();
153 controller.in_stream.size = videoData.size();
154 controller.in_stream.index = 0;
155 controller.in_stream.length = 32;
156 controller.in_stream.code = 0;
158 bool_t got_image = FALSE;
159 //qDebug() <<"VideoThread::decodeTransform" << controller.video_codec;
161 if(controller.video_codec!=NULL) video_decode_blockline( &controller, &picture, &got_image );
162 //else qDebug() << "No video controller";
163 //qDebug() <<"VideoThread::decodeTransform 2";
164 //video_decode_picture( &controller, &picture, &stream, &got_image );
167 // qDebug() <<"VideoThread::decodeTransform got image" << picture.width << picture.height << image->byteCount() << image->bytesPerLine();
168 // we got one picture
170 picture.complete = 1;
171 num_picture_decoded++;
172 if(image->depth()<24)
173 vp_stages_YUV420P_to_RGB565(NULL,&picture,image->bits(),image->bytesPerLine());
175 vp_stages_YUV420P_to_ARGB32(NULL,&picture,image->bits(),image->bytesPerLine());
177 // qDebug() << "pic " << num_picture_decoded;