0aa269b3e9a950677877e216c755262c415be0d1
[mardrone] / mardrone / dronelib / 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
48         depth=painter->device()->depth();
49         qDebug() << "depth=" << depth;
50
51         if(depth>=24) {
52             image=new QImage(320,240, QImage::Format_RGB32);
53              image->fill(0x555555);
54         }
55         else {
56             image=new QImage(320,240, QImage::Format_RGB16);
57             image->fill(0x5555);
58         }
59         QPainter p(image);
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);
64         initialized=true;
65     } else
66     painter->drawImage(boundingRect(),*image,image->rect());
67  };
68
69 QRectF DroneVideo::boundingRect() const
70 {
71     return QRectF(0.0,0.0,size().width(),size().height());
72 }
73
74
75 void VideoThread::run()
76 {
77 #define ACQ_WIDTH     320
78 #define ACQ_HEIGHT    240
79 #undef memset
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";
90     luma_only=FALSE;
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;
103     picture.y_pad         = 0;
104     picture.c_pad         = 0;
105     video_codec_open(&controller, (codec_type_t)UVLC_CODEC);
106     //stateTimer->start(1000);
107     qDebug() << "videoThread::run() initialized";
108     sendVideoPort("AT");
109     while(!stopped) {
110         exec();
111     }
112
113 }
114
115 void VideoThread::timer()
116 {
117   //  qDebug() << "thread Timer";
118
119 }
120
121 void VideoThread::sendVideoPort(QString cmd)
122 {
123     QByteArray dgram;
124     dgram=cmd.toLatin1();
125     qDebug() << "videoThread::sendCmd= " << cmd+"\n" << "to " << droneHost ;
126     videoSock->writeDatagram(dgram.data(),dgram.size(),droneHost,5555);
127 }
128
129 void VideoThread::videoDataReady()
130 {
131    qint64 l;
132    QByteArray videoData;
133
134    QHostAddress host;
135    quint16 port;
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);
140 }
141
142 void VideoThread::decodeTransform(QByteArray &videoData)
143 {
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;
150
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 );
156     if( got_image )
157         {
158           //  qDebug() <<"VideoThread::decodeTransform got image" << picture.width << picture.height << image->byteCount() << image->bytesPerLine();
159           // we got one picture
160           // out->size = 1;
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());
165           else
166             vp_stages_YUV420P_to_ARGB32(NULL,&picture,image->bits(),image->bytesPerLine());
167
168        //   qDebug() << "pic " << num_picture_decoded;
169         }
170
171
172 };
173
174