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>
24 DroneVideo::DroneVideo()
26 droneHost.setAddress("192.168.1.1");
30 VideoThread::VideoThread(DroneVideo *parentp,QHostAddress host,QImage *_image)
33 qDebug() << "videoThread::videoThread";
36 videoSock=new QUdpSocket();
37 videoSock->bind(QHostAddress::Any,5555);
43 void DroneVideo::paint(QPainter *painter,const QStyleOptionGraphicsItem *option,
46 if(!initialized) { // We need initialize QImage here because we don't know display depth before
48 depth=painter->device()->depth();
49 qDebug() << "depth=" << depth;
52 image=new QImage(320,240, QImage::Format_RGB32);
53 image->fill(0x555555);
56 image=new QImage(320,240, QImage::Format_RGB16);
60 p.drawLine(0,0,image->width(),image->height());
61 p.drawLine(image->width(),0,0,image->height());
62 update(boundingRect());
63 videoThread=new VideoThread(this,droneHost,image);
66 painter->drawImage(boundingRect(),*image,image->rect());
69 QRectF DroneVideo::boundingRect() const
71 return QRectF(0.0,0.0,size().width(),size().height());
75 void VideoThread::run()
78 #define ACQ_HEIGHT 240
80 memset(&controller,0,sizeof(controller));
81 memset(&picture,0,sizeof(picture));
82 pictureWidth= image->width();
83 pictureHeight=image->height();
84 int codec_type=UVLC_CODEC;
85 qDebug() << "videoThread::run()";
86 stateTimer=new QTimer();
87 connect(stateTimer,SIGNAL(timeout()),this,SLOT(timer()));
88 connect(videoSock,SIGNAL(readyRead()),this,SLOT(videoDataReady()));
89 qDebug() << "videoThread::run() 2";
91 num_picture_decoded=0;
92 /// Picture configuration
93 picture.format = PIX_FMT_YUV420P;
94 picture.width = pictureWidth;
95 picture.height = pictureHeight;
96 picture.framerate = 30;
97 picture.y_buf = (uint8_t*)(void*)vp_os_malloc((size_t) pictureWidth*pictureHeight );
98 picture.cr_buf = (uint8_t*)vp_os_malloc( pictureWidth*pictureHeight/4 );
99 picture.cb_buf = (uint8_t*)vp_os_malloc( pictureWidth*pictureHeight/4 );
100 picture.y_line_size = pictureWidth;
101 picture.cb_line_size = pictureWidth / 2;
102 picture.cr_line_size = pictureWidth / 2;
105 video_codec_open(&controller, (codec_type_t)UVLC_CODEC);
106 //stateTimer->start(1000);
107 qDebug() << "videoThread::run() initialized";
115 void VideoThread::timer()
117 // qDebug() << "thread Timer";
121 void VideoThread::sendVideoPort(QString cmd)
124 dgram=cmd.toLatin1();
125 qDebug() << "videoThread::sendCmd= " << cmd+"\n" << "to " << droneHost ;
126 videoSock->writeDatagram(dgram.data(),dgram.size(),droneHost,5555);
129 void VideoThread::videoDataReady()
132 QByteArray videoData;
136 videoData.resize(videoSock->pendingDatagramSize ());
137 l=videoSock->readDatagram(videoData.data(),videoData.size(),&host,&port);
138 // qDebug() << "videoThread::videoDataReady" <<" l=" << l << "from" << host ;
139 decodeTransform(videoData);
142 void VideoThread::decodeTransform(QByteArray &videoData)
144 controller.in_stream.bytes = (uint32_t*)videoData.data();
145 controller.in_stream.used = videoData.size();
146 controller.in_stream.size = videoData.size();
147 controller.in_stream.index = 0;
148 controller.in_stream.length = 32;
149 controller.in_stream.code = 0;
151 bool_t got_image = FALSE;
152 //qDebug() <<"VideoThread::decodeTransform";
153 video_decode_blockline( &controller, &picture, &got_image );
154 //qDebug() <<"VideoThread::decodeTransform 2";
155 //video_decode_picture( &controller, &picture, &stream, &got_image );
158 // qDebug() <<"VideoThread::decodeTransform got image" << picture.width << picture.height << image->byteCount() << image->bytesPerLine();
159 // we got one picture
161 picture.complete = 1;
162 num_picture_decoded++;
163 if(image->depth()<24)
164 vp_stages_YUV420P_to_RGB565(NULL,&picture,image->bits(),image->bytesPerLine());
166 vp_stages_YUV420P_to_ARGB32(NULL,&picture,image->bits(),image->bytesPerLine());
168 // qDebug() << "pic " << num_picture_decoded;