1 /***************************************************************************
2                           Swap Fields.cpp  -  description
3                              -------------------
4 Swap each line  (shift up for odd, down for even)
5 
6 
7     begin                : Thu Mar 21 2002
8     copyright            : (C) 2002 by mean
9     email                : fixounet@free.fr
10  ***************************************************************************/
11 
12 /***************************************************************************
13  *                                                                         *
14  *   This program is free software; you can redistribute it and/or modify  *
15  *   it under the terms of the GNU General Public License as published by  *
16  *   the Free Software Foundation; either version 2 of the License, or     *
17  *   (at your option) any later version.                                   *
18  *                                                                         *
19  ***************************************************************************/
20 
21 #include "ADM_default.h"
22 
23 
24 #include "ADM_videoFilterDynamic.h"
25 #include "ADM_vidSwapSmart.h"
26 #include "ADM_interlaced.h"
27 
28 #define aprintf(...) {}
29 
30 static FILTER_PARAM nullParam={0,{""}};
31 //********** Register chunk ************
32 //REGISTERX(VF_INTERLACING, "smartswapfield",QT_TR_NOOP("Smart swap fields"),QT_TR_NOOP(
33     //"Smartly swap fields. Needed when field order changes."),VF_SMARTSWAPFIELDS,1,swapsmart_create,swapsmart_script);
34 VF_DEFINE_FILTER(AVDMVideoSwapSmart,nullParam,
35     smartswapfield,
36                 QT_TR_NOOP("Smart swap fields"),
37                 1,
38                 VF_INTERLACING,
39                 QT_TR_NOOP("Smartly swap fields. Needed when field order changes."));
40 //********** Register chunk ************
41 
42 
printConf(void)43 char *AVDMVideoSwapSmart::printConf( void )
44 {
45  ADM_FILTER_DECLARE_CONF(" Smart Swap fields");
46 
47 }
48 
49 //_______________________________________________________________
AVDMVideoSwapSmart(AVDMGenericVideoStream * in,CONFcouple * setup)50 AVDMVideoSwapSmart::AVDMVideoSwapSmart(
51 									AVDMGenericVideoStream *in,CONFcouple *setup)
52 {
53 UNUSED_ARG(setup);
54   	_in=in;
55    	memcpy(&_info,_in->getInfo(),sizeof(_info));
56 	_uncompressed=new ADMImage(_info.width,_info.height);
57 
58 
59 
60 }
61 // ___ destructor_____________
~AVDMVideoSwapSmart()62 AVDMVideoSwapSmart::~AVDMVideoSwapSmart()
63 {
64  	delete  _uncompressed;
65 	_uncompressed=NULL;
66 
67 }
68 
69 //
70 //	Basically ask a uncompressed frame from editor and ask
71 //		GUI to decompress it .
72 //
73 
getFrameNumberNoAlloc(uint32_t frame,uint32_t * len,ADMImage * data,uint32_t * flags)74 uint8_t AVDMVideoSwapSmart::getFrameNumberNoAlloc(uint32_t frame,
75 				uint32_t *len,
76    				ADMImage *data,
77 				uint32_t *flags)
78 {
79 //static Image in,out;
80 			if(frame>=_info.nb_frames) return 0;
81 
82 
83 			// read uncompressed frame
84        		if(!_in->getFrameNumberNoAlloc(frame, len,data,flags)) return 0;
85 
86 		uint32_t w=_info.width;
87 		uint32_t h=_info.height;
88 		uint32_t page=w*h;
89 		uint32_t stride;
90 
91 
92 
93 		uint8_t *odd,*even,*target,*target2;
94 
95 		even=YPLANE(data);
96 		odd=even+w;
97 		target=YPLANE(_uncompressed);
98 		target2=_uncompressed->data+w;
99 		stride=2*w;
100 
101 		h>>=1;
102 		for(;h--;h>0)
103 		{
104 			memcpy(target,odd,w);
105 			memcpy(target2,even,w);
106 			target+=stride;
107 			target2+=stride;
108 			odd+=stride;
109 			even+=stride;
110 		}
111 		// now we have straight and swapped
112 		// which one is better ?
113 		uint32_t s,m;
114 		s=      ADMVideo_interlaceCount( YPLANE(data),_info.width, _info.height);
115 		m=      ADMVideo_interlaceCount( YPLANE(_uncompressed),_info.width, _info.height);
116 
117 		aprintf(" %lu straight vs %lu swapped => %s\n",s,m,(s*2>m*3?"swapped":"straight"));
118 
119 		// if swapped <= 66 % straight
120 
121 		if(s*2>m*3)  // swapped is better
122 		{
123 			memcpy(YPLANE(data),YPLANE(_uncompressed),page);
124 
125 		}
126 
127       return 1;
128 }
129 
130 
131