project file fixes
[mardrone] / mardrone / video.cpp
1 /*==================================================================
2   !
3   !  mardrone application AR-Drone for MeeGo
4
5   ! Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies).
6   ! All rights reserved.
7   !
8   !  Author:Kate Alhola  kate.alhola@nokia.com
9   !
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.
17   !
18   !
19   !
20   *===================================================================*/
21 #include "video.h"
22 #include <QGraphicsView>
23
24 DroneVideo::DroneVideo()
25 {
26      droneHost.setAddress("192.168.1.1");
27      initialized=false;
28 }
29
30 VideoThread::VideoThread(DroneVideo *parentp,QHostAddress host,QImage *_image)
31 {
32     image=_image;
33     qDebug() << "videoThread::videoThread";
34     stopped=false;
35     parent=parentp;
36     videoSock=new QUdpSocket();
37     videoSock->bind(QHostAddress::Any,5555);
38     droneHost=host;
39     start();
40
41 };
42
43 void DroneVideo::paint(QPainter *painter,const QStyleOptionGraphicsItem *option,
44                         QWidget *widget)
45  {
46     if(!initialized) { // We need initialize QImage here because we don't know display depth before
47         int depth=0;
48
49         depth=painter->device()->depth();
50         qDebug() << "depth=" << depth;
51
52         if(depth==24) {
53             image=new QImage(320,240, QImage::Format_RGB32);
54              image->fill(0x555555);
55         }
56         else {
57             image=new QImage(320,240, QImage::Format_RGB16);
58             image->fill(0x5555);
59         }
60         QPainter p(image);
61         p.drawLine(0,0,image->width(),image->height());
62         p.drawLine(image->width(),0,0,image->height());
63         update(boundingRect());
64         videoThread=new VideoThread(this,droneHost,image);
65         initialized=true;
66     } else
67     painter->drawImage(boundingRect(),*image,image->rect());
68  };
69
70 QRectF DroneVideo::boundingRect() const
71 {
72     return QRectF(0.0,0.0,size().width(),size().height());
73 }
74
75
76 void VideoThread::run()
77 {
78 #define ACQ_WIDTH     320
79 #define ACQ_HEIGHT    240
80 #undef memset
81     memset(&controller,0,sizeof(controller));
82     memset(&picture,0,sizeof(picture));
83     pictureWidth= image->width();
84     pictureHeight=image->height();
85     int codec_type=UVLC_CODEC;
86     qDebug() << "videoThread::run()";
87     stateTimer=new QTimer();
88     connect(stateTimer,SIGNAL(timeout()),this,SLOT(timer()));
89     connect(videoSock,SIGNAL(readyRead()),this,SLOT(videoDataReady()));
90     qDebug() << "videoThread::run() 2";
91     luma_only=FALSE;
92     num_picture_decoded=0;
93     /// Picture configuration
94     picture.format        = PIX_FMT_YUV420P;
95     picture.width         = pictureWidth;
96     picture.height        = pictureHeight;
97     picture.framerate     = 30;
98     picture.y_buf         = (uint8_t*)(void*)vp_os_malloc((size_t) pictureWidth*pictureHeight );
99     picture.cr_buf        = (uint8_t*)vp_os_malloc( pictureWidth*pictureHeight/4 );
100     picture.cb_buf        = (uint8_t*)vp_os_malloc( pictureWidth*pictureHeight/4 );
101     picture.y_line_size   = pictureWidth;
102     picture.cb_line_size  = pictureWidth / 2;
103     picture.cr_line_size  = pictureWidth / 2;
104     picture.y_pad         = 0;
105     picture.c_pad         = 0;
106     qDebug() << "videoThread::run() 3";
107     video_codec_open(&controller, (codec_type_t)UVLC_CODEC);
108     //stateTimer->start(1000);
109     qDebug() << "videoThread::run() initialized";
110     sendVideoPort("AT");
111     while(!stopped) {
112         exec();
113     }
114
115 }
116
117 void VideoThread::timer()
118 {
119   //  qDebug() << "thread Timer";
120
121 }
122
123 void VideoThread::sendVideoPort(QString cmd)
124 {
125     QByteArray dgram;
126     dgram=cmd.toLatin1();
127     qDebug() << "videoThread::sendCmd= " << cmd+"\n" << "to " << droneHost ;
128     videoSock->writeDatagram(dgram.data(),dgram.size(),droneHost,5555);
129 }
130
131 void VideoThread::videoDataReady()
132 {
133    qint64 l;
134    QByteArray videoData;
135
136    QHostAddress host;
137    quint16 port;
138    videoData.resize(videoSock->pendingDatagramSize ());
139    l=videoSock->readDatagram(videoData.data(),videoData.size(),&host,&port);
140    qDebug() << "videoThread::videoDataReady" <<" l=" << l << "from"  << host ;
141    decodeTransform(videoData);
142 }
143
144 void VideoThread::decodeTransform(QByteArray &videoData)
145 {
146     controller.in_stream.bytes   = (uint32_t*)videoData.data();
147     controller.in_stream.used    = videoData.size();
148     controller.in_stream.size    = videoData.size();
149     controller.in_stream.index   = 0;
150     controller.in_stream.length  = 32;
151     controller.in_stream.code    = 0;
152
153     bool_t got_image = FALSE;
154     //qDebug() <<"VideoThread::decodeTransform";
155     video_decode_blockline( &controller, &picture, &got_image );
156     //qDebug() <<"VideoThread::decodeTransform 2";
157     //video_decode_picture( &controller, &picture, &stream, &got_image );
158     if( got_image )
159         {
160             qDebug() <<"VideoThread::decodeTransform got image" << picture.width << picture.height << image->byteCount() << image->bytesPerLine();
161           // we got one picture
162           // out->size = 1;
163           picture.complete     = 1;
164           num_picture_decoded++;
165           vp_stages_YUV420P_to_RGB565(NULL,&picture,image->bits(),image->bytesPerLine());
166
167
168           qDebug() << "pic " << num_picture_decoded;
169         }
170
171
172 };
173
174