Subversion Repositories shark

Rev

Rev 1085 | Blame | Compare with Previous | Last modification | View Log | RSS feed

/*
 * Project: S.Ha.R.K.
 *
 * Coordinators:
 *   Giorgio Buttazzo    <giorgio@sssup.it>
 *   Paolo Gai           <pj@gandalf.sssup.it>
 *
 * Authors     :
 *   Paolo Gai           <pj@gandalf.sssup.it>
 *   (see the web pages for full authors list)
 *
 * ReTiS Lab (Scuola Superiore S.Anna - Pisa - Italy)
 *
 * http://www.sssup.it
 * http://retis.sssup.it
 * http://shark.sssup.it
 */


/*
 * Copyright (C) 1999 Luca Abeni and Massimiliano Giorgi
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 *
 */


/*
 * CVS :        $Id: gvideo.c,v 1.1.1.1 2002-09-02 09:37:41 pj Exp $
 *
 * File:        $File$
 * Revision:    $Revision: 1.1.1.1 $
 * Last update: $Date: 2002-09-02 09:37:41 $
 */


#include "config.h"

#include <kernel/func.h>
#include <kernel/model.h>
#include <kernel/const.h>

#include <drivers/glib.h>

#include <stdlib.h>

#include "mpeg/video.h"
#include "mpeg/util.h"
#include "mpeg/dither.h"
#include "mpeg/mpeg.h"

#include "mutex.h"
#include "gvideo.h"
#include "gbuffer.h"
#include "xread.h"

#define STARTGROUP 513

#define VIDEORATE 70
#define READRATE  30

/*
 * colors
 */


#ifdef FULLCOLOR

#define COLORFG rgb16(255,255,255)
#define COLORBG rgb16(0,0,0)

#define SP rgb16(0,128,0)
#define FG rgb16(255,255,255)
#define BG rgb16(0,0,0)

#else

/* for writing */
#define COLORFG 255
#define COLORBG 0

/* for box */
#define SP 128
#define FG 255
#define BG 0

#endif

/* border size (pixels) */
#define BO 4

/*
 *
 */


#ifdef FULLCOLOR
/* 16bits format 5:6:5 */
/* 32bits format 8:8:8:0 */
void down32to16(WORD *dst, BYTE *src, int size)
{
  int i;
  return;
  for (i=0;i<size;i+=4,dst++,src+=4)
    /* blue green red */
    *dst=((src[2]&0xf8)>>3)|((src[1]&0xfc)<<3)|((src[0]&0xf8)<<8);
}
#endif

/*
 *
 */


void draw_frame(int x, int y, int dx, int dy)
{
#ifndef NOGRX
  grxlock();
  grx_box(x-1-BO,y-1-BO,x+dx+BO,y+dy+BO,SP);
  grx_rect(x-1-BO,y-1-BO,x+dx+BO,y+dy+BO,FG);
  grx_box(x,y,x+dx-1,y+dy-1,BG);
  grx_rect(x-1,y-1,x+dx,y+dy,FG);
  grxunlock();
#endif
}

struct info {
  int       x,y;
  ImageDesc *img;
};

static TASK play(void *arg)
{
  struct info *ptr=(struct info*)arg;
  int x1,y1,x2,y2;
  BYTE *pixels; //,*image;
  int moreframes;
 
  pixels=(BYTE*)malloc(ptr->img->Size*sizeof(BYTE));
#ifdef FULLCOLOR
  image=(BYTE*)malloc(ptr->img->Size/2*sizeof(BYTE));
#endif
  x1=ptr->x;
  y1=ptr->y;
  x2=x1+ptr->img->Width-1;
  y2=y1+ptr->img->Height-1;

  moreframes=1;
  for (;;) {
    while (moreframes) {
      moreframes=GetMPEGFrame(ptr->img,pixels);
#ifndef NOGRX
#ifdef FULLCOLOR
      down32to16((WORD*)image,pixels,ptr->img->Size);
      grxlock();
      grx_putimage(x1, y1, x2, y2, pixels);
      grxunlock();
#else
      grxlock();
      grx_putimage(x1, y1, x2, y2, pixels);
      grxunlock();
#endif
#else
      cprintf("%c",'0');      
#endif
      task_endcycle();
    }
    break;
   
    //if (!loop) break;
    //RewindMPEG (mpeg, img);
    //SetMPEGOption (img, MPEG_DITHER, GRAY_DITHER);
    //moreframes = TRUE;
  }
 
  return NULL;
}

int gvideo_init(char *title, struct gvideoinfo *ptr)
{
  //  static int      groupcounter=0;
  struct info     *info;
  SOFT_TASK_MODEL model;
  FILE            *fin;
  PID             pid,pid2;
  ImageDesc       *img;
  int             res;
  int             i;
  int             period,wcet;
  int             group;

  img=(ImageDesc*)malloc(sizeof(ImageDesc));
  if (img==NULL) return -3;
  img->vid_stream=NULL;
  img->Colormap=NULL;

  info=(struct info*)malloc(sizeof(struct info));
  if (info==NULL) return -11;
  info->img=img;
  info->x=ptr->x;
  info->y=ptr->y;
 
  fin=fopen(ptr->pathname, "r");  
  if (!fin) return -1;

  //group=STARTGROUP+groupcounter++;
  group=STARTGROUP;
  pid2=x_initbuffer(group,fin,ptr->bitrate,ptr->band*READRATE);
  if (pid2<0) return -2;

#ifdef ACTIVATE
  gbuffer_init(table[fileno(fin)],group,20+12,450);
#else
  gbuffer_init(NULL,group,20+12,450);
#endif
 
  res=OpenMPEG(fin,img);
  if (!res) return -4;

#ifdef FULLCOLOR  
  SetMPEGOption(img,MPEG_DITHER,HALF_COLOR_DITHER);
#else
  SetMPEGOption(img,MPEG_DITHER,GRAY_DITHER);
#endif  

  //cprintf("colrmap size=%8i\n",img->ColormapSize);
  //cprintf("picture rate=%8i\n",img->PictureRate);
  //cprintf("bit rate    =%8i\n",img->BitRate);

#ifndef FULLCOLOR
  if (group==STARTGROUP) {
    ColormapEntry *cp;
    cp=img->Colormap;
    for (i=0;i<img->ColormapSize;i++) {
#ifndef NOGRX
      grxlock();
      grx_setcolor (i,img->Colormap[i].red/4,
                    img->Colormap[i].green/4,
                    img->Colormap[i].blue/4);
      grxunlock();
#endif   
    }
  }
#endif
 
  draw_frame(info->x,info->y,img->Width,img->Height);
  ptr->w=img->Width;
  ptr->h=img->Height;

  {
    char buffer[256];

#ifndef NOGRX
    grxlock();
    grx_text(title,ptr->x,ptr->y-14,COLORFG,COLORBG);
    grxunlock();
    sprintf(buffer,"Average bit rate: %i.%03i Mb/s",
            ptr->bitrate/1024/1024,(ptr->bitrate%(1024*1024))*1000/1024/1000);
    grxlock();
    grx_text(buffer,ptr->x,ptr->y+ptr->h+BO*2+2,COLORFG,COLORBG);
    grxunlock();
    sprintf(buffer,"Frame rate      : %02.3f frame/sec",
            (double)ptr->framerate/100.0);
    grxlock();
    grx_text(buffer,ptr->x,ptr->y+ptr->h+BO*2+2+9,COLORFG,COLORBG);
    grxunlock();
#endif
  }
 
  period=100000000/ptr->framerate;
  wcet=period*ptr->band*VIDEORATE/10000;
 
  soft_task_default_model(model);        
  soft_task_def_met(model,wcet);
  soft_task_def_wcet(model,wcet);
  soft_task_def_period(model,period);
  soft_task_def_periodic(model);
  soft_task_def_arg(model,(void*)info);
  soft_task_def_group(model,group);
  soft_task_def_ctrl_jet(model);
 
  pid=task_create("Video",play,&model,NULL);
  if (pid==-1) return -6;

  return group;
}