sync repo
[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 #ifdef QT_IOS
25 #define NO_VIDEO
26 #endif
27
28 DroneVideo::DroneVideo()
29 {
30      droneHost.setAddress("192.168.1.1");
31      initialized=false;
32 }
33
34 VideoThread::VideoThread(DroneVideo *parentp,QHostAddress host,QImage *_image)
35 {
36     image=_image;
37     qDebug() << "videoThread::videoThread";
38     stopped=false;
39     parent=parentp;
40     videoSock=new QUdpSocket();
41     videoSock->bind(QHostAddress::Any,5555);
42     droneHost=host;
43     start();
44
45 };
46
47 void DroneVideo::paint(QPainter *painter,const QStyleOptionGraphicsItem *option,
48                         QWidget *widget)
49  {
50     if(!initialized) { // We need initialize QImage here because we don't know display depth before
51
52         depth=painter->device()->depth();
53         qDebug() << "depth=" << depth;
54
55         if(depth>=24) {
56             image=new QImage(320,240, QImage::Format_RGB32);
57              image->fill(0x555555);
58         }
59         else {
60             image=new QImage(320,240, QImage::Format_RGB16);
61             image->fill(0x5555);
62         }
63         QPainter p(image);
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);
68         initialized=true;
69     } else
70     painter->drawImage(boundingRect(),*image,image->rect());
71  };
72
73 QRectF DroneVideo::boundingRect() const
74 {
75     return QRectF(0.0,0.0,size().width(),size().height());
76 }
77
78
79 void VideoThread::run()
80 {
81 #define ACQ_WIDTH     320
82 #define ACQ_HEIGHT    240
83 #undef memset
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";
94     luma_only=FALSE;
95     num_picture_decoded=0;
96     /// Picture configuration
97 #ifndef NO_VIDEO
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;
108     picture.y_pad         = 0;
109     picture.c_pad         = 0;
110
111     qDebug() << "video_codec_open=" << video_codec_open(&controller, (codec_type_t)codec_type);
112 #endif
113     //stateTimer->start(1000);
114     qDebug() << "videoThread::run() initialized";
115     sendVideoPort("AT");
116     while(!stopped) {
117         exec();
118     }
119
120 }
121
122 void VideoThread::timer()
123 {
124   //  qDebug() << "thread Timer";
125
126 }
127
128 void VideoThread::sendVideoPort(QString cmd)
129 {
130     QByteArray dgram;
131     dgram=cmd.toLatin1();
132     qDebug() << "videoThread::sendCmd= " << cmd+"\n" << "to " << droneHost ;
133     videoSock->writeDatagram(dgram.data(),dgram.size(),droneHost,5555);
134 }
135
136 void VideoThread::videoDataReady()
137 {
138    qint64 l;
139    QByteArray videoData;
140
141    QHostAddress host;
142    quint16 port;
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);
147 }
148
149 void VideoThread::decodeTransform(QByteArray &videoData)
150 {
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;
157
158     bool_t got_image = FALSE;
159     //qDebug() <<"VideoThread::decodeTransform" << controller.video_codec;
160 #ifndef NO_VIDEO
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 );
165     if( got_image )
166         {
167           //  qDebug() <<"VideoThread::decodeTransform got image" << picture.width << picture.height << image->byteCount() << image->bytesPerLine();
168           // we got one picture
169           // out->size = 1;
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());
174           else
175             vp_stages_YUV420P_to_ARGB32(NULL,&picture,image->bits(),image->bytesPerLine());
176
177        //   qDebug() << "pic " << num_picture_decoded;
178         }
179 #endif
180
181 };
182
183