1 /***************************************************************************
2  *                                                                         *
3  *   This program is free software; you can redistribute it and/or modify  *
4  *   it under the terms of the GNU General Public License as published by  *
5  *   the Free Software Foundation; either version 2 of the License, or     *
6  *   (at your option) any later version.                                   *
7  *                                                                         *
8  ***************************************************************************/
9 
10 #include <math.h>
11 
12 #include "ADM_default.h"
13 #include "ADM_videoFilterDynamic.h"
14 #include "DIA_enter.h"
15 #include "DIA_factory.h"
16 #include "DIA_coreToolkit.h"
17 #include "ADM_vidFade_param.h"
18 class AVDM_Fade : public AVDMGenericVideoStream
19 {
20   VideoCache      *vidCache;
21   VIDFADE_PARAM   *_param;
22   uint16_t         lookupLuma[256][256];
23   uint16_t         lookupChroma[256][256];
24   uint8_t         buildLut(void);
25   public:
26 
27                     AVDM_Fade(AVDMGenericVideoStream *in,CONFcouple *couples);
28                     ~AVDM_Fade(void);
29     uint8_t         getFrameNumberNoAlloc(uint32_t frame, uint32_t *len,
30                                           ADMImage *data,uint32_t *flags);
31 
32     char            *printConf( void );
33     uint8_t         configure(AVDMGenericVideoStream *in);
34     uint8_t         getCoupledConf( CONFcouple **couples);
35 };
36 
37 static FILTER_PARAM fadeParam={4,{"startFade","endFade","inOut","toBlack"}};
38 
39 VF_DEFINE_FILTER(AVDM_Fade,fadeParam,
40                 fade,
41                 QT_TR_NOOP("Fade"),
42                 1,
43                 VF_TRANSFORM,
44                 QT_TR_NOOP("Fade in/out."));
45 /*************************************/
configure(AVDMGenericVideoStream * in)46 uint8_t AVDM_Fade::configure(AVDMGenericVideoStream *in)
47 {
48   uint32_t mx=_info.nb_frames;
49   _in=in;
50 
51   diaMenuEntry menuE[2]={{0,QT_TR_NOOP("Out"),QT_TR_NOOP("Fade out")},{1,QT_TR_NOOP("In"),QT_TR_NOOP("Fade in")}};
52   uint32_t start,end;
53   VIDFADE_PARAM param=*_param;
54 
55 while(1)
56 {
57     diaElemMenu     menu(&(param.inOut),QT_TR_NOOP("_Fade type:"), 2,menuE);
58     diaElemUInteger start(&(param.startFade),QT_TR_NOOP("_Start frame:"),0,mx);
59     diaElemUInteger end(&(param.endFade),QT_TR_NOOP("_End frame:"),0,mx);
60     diaElemToggle   black(&(param.toBlack),QT_TR_NOOP("Fade to _black"));
61 
62     diaElem *elems[4]={&menu,&start,&end,&black};
63 
64     if( diaFactoryRun(QT_TR_NOOP("Fade"),4,elems))
65     {
66       // Check it is consistent
67       if(param.startFade>=param.endFade || (param.startFade>=mx) || (param.endFade>=mx))
68       {
69         GUI_Error_HIG(QT_TR_NOOP("Parameter Error"),QT_TR_NOOP("Start must be before end, and both within video # of frames."));
70         continue;
71       }
72       //
73       *_param=param;
74       return 1;
75     }else
76         return 0;
77 }
78   return 1;
79 }
80 
printConf(void)81 char *AVDM_Fade::printConf( void )
82 {
83 	ADM_FILTER_DECLARE_CONF(" Fade : Start %u End %u",_param->startFade,_param->endFade);
84 }
85 
86 
AVDM_Fade(AVDMGenericVideoStream * in,CONFcouple * couples)87 AVDM_Fade::AVDM_Fade(AVDMGenericVideoStream *in,CONFcouple *couples)
88 
89 {
90 
91   int count = 0;
92   char buf[80];
93   unsigned int *p;
94 
95   _in=in;
96   memcpy(&_info,_in->getInfo(),sizeof(_info));
97   _info.encoding=1;
98   vidCache=new VideoCache(3,in);
99 
100   _param=new VIDFADE_PARAM;
101   if(couples)
102   {
103     GET(startFade);
104     GET(endFade);
105     GET(inOut);
106     GET(toBlack);
107 
108   }else
109   {
110     _param->startFade=0;
111     _param->endFade=_info.nb_frames-1;
112     _param->inOut=0;
113     _param->toBlack=0;
114   }
115   buildLut();
116 }
117 //________________________________________________________
getCoupledConf(CONFcouple ** couples)118 uint8_t AVDM_Fade::getCoupledConf( CONFcouple **couples)
119 {
120   *couples=new CONFcouple(4);
121   CSET(startFade);
122   CSET(endFade);
123   CSET(inOut);
124   CSET(toBlack);
125   return 1;
126 }
127 //________________________________________________________
~AVDM_Fade(void)128 AVDM_Fade::~AVDM_Fade(void)
129 {
130 
131   if(vidCache) delete vidCache;
132   vidCache=NULL;
133   if(_param) delete _param;
134   _param=NULL;
135 }
getFrameNumberNoAlloc(uint32_t frame,uint32_t * len,ADMImage * data,uint32_t * flags)136 uint8_t AVDM_Fade::getFrameNumberNoAlloc(uint32_t frame, uint32_t *len,
137                                             ADMImage *data,uint32_t *flags)
138 {
139 
140   uint32_t num_frames,tgt;
141 
142   ADMImage *src;
143 
144   num_frames=_info.nb_frames;   // ??
145 
146   tgt=frame+_info.orgFrame;
147   if(frame>=num_frames)
148   {
149     printf("[Fade] out of bound\n");
150     return 0;
151   }
152 
153   src=vidCache->getImage(frame);
154   if(!src) return 0;
155   if(tgt>_param->endFade || tgt <_param->startFade ||_param->endFade==_param->startFade )
156   {
157     //printf("Cur %u start %u end %u\n",tgt,_param->startFade,_param->endFade);
158     data->duplicate(src);
159     vidCache->unlockAll();
160     return 1;
161   }
162   uint8_t *s,*d,*s2;
163   uint16_t *index,*invertedIndex;
164   uint32_t count=_info.width*_info.height,w;
165   float num,den;
166 
167   den=_param->endFade-_param->startFade;
168 
169   num=tgt-_param->startFade;
170 
171   num=num/den;
172   num*=255.;
173   w=(uint32_t)floor(num+0.4);
174 
175 //printf("w :%u\n",w);
176 
177   s=src->data;
178   d=data->data;
179   if(_param->toBlack)
180   {
181         index=lookupLuma[w];
182         for(int i=0;i<count;i++)
183         {
184           *d++=(index[*s++]>>8);
185         }
186         // Now do chroma
187         count>>=2;
188         s=UPLANE(src);
189         d=UPLANE(data);
190         index=lookupChroma[w];
191         for(int i=0;i<count;i++)
192         {
193           *d++=(index[*s++]>>8);
194         }
195         s=VPLANE(src);
196         d=VPLANE(data);
197         for(int i=0;i<count;i++)
198         {
199           *d++=(index[*s++]>>8);
200         }
201   }
202   else
203   {
204         uint32_t x,alpha;
205         ADMImage *final;
206 
207         final=vidCache->getImage(_param->endFade-_info.orgFrame);
208         if(!final)
209         {
210               data->duplicate(src);
211               vidCache->unlockAll();
212               return 1;
213         }
214 
215         s2=final->data;
216 
217         index=lookupLuma[w];
218 
219         invertedIndex=lookupLuma[255-w];
220         for(int i=0;i<count;i++)
221         {
222           *d++=(index[*s++]+invertedIndex[*s2++])>>8;
223         }
224         // Now do chroma
225         count>>=2;
226         s=UPLANE(src);
227         d=UPLANE(data);
228         s2=UPLANE(final);
229         index=lookupChroma[w];
230         invertedIndex=lookupChroma[255-w];
231         for(int i=0;i<count;i++)
232         {
233             *d++=(index[*s++]+invertedIndex[*s2++]-(128<<8))>>8;
234         }
235         s=VPLANE(src);
236         d=VPLANE(data);
237         s2=VPLANE(final);
238         for(int i=0;i<count;i++)
239         {
240             *d++=(index[*s++]+invertedIndex[*s2++]-(128<<8))>>8;
241 
242         }
243   }
244   vidCache->unlockAll();
245   return 1;
246 }
247 
buildLut(void)248 uint8_t AVDM_Fade::buildLut(void)
249 {
250   float f,ration;
251   for(int i=0;i<256;i++)
252   {
253     if(!_param->inOut) ration=255-i;
254     else ration=i;
255     for(int r=0;r<256;r++)
256     {
257       f=r;
258       f=f*ration;
259       lookupLuma[i][r]=(uint16_t)(f+0.4);
260 
261       f=r-128;
262       f=f*ration;
263       lookupChroma[i][r]=(128<<8)+(uint16_t)(f+0.4);
264 
265     }
266 
267   }
268   return 1;
269 }
270 //EOF
271 
272 
273 
274