]> git.vomp.tv Git - vompclient-marten.git/blob - glmocoshader.cc
Motion Compensation, all implemented, all buggy
[vompclient-marten.git] / glmocoshader.cc
1 /*\r
2     Copyright 2012 Marten Richter\r
3 \r
4     This file is part of VOMP.\r
5 \r
6     VOMP is free software; you can redistribute it and/or modify\r
7     it under the terms of the GNU General Public License as published by\r
8     the Free Software Foundation; either version 2 of the License, or\r
9     (at your option) any later version.\r
10 \r
11     VOMP is distributed in the hope that it will be useful,\r
12     but WITHOUT ANY WARRANTY; without even the implied warranty of\r
13     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\r
14     GNU General Public License for more details.\r
15 \r
16     You should have received a copy of the GNU General Public License\r
17     along with VOMP; if not, write to the Free Software\r
18     Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.\r
19 */\r
20 #include "glmocoshader.h"\r
21 #include "videovpeogl.h"\r
22 \r
23 #define BLOCK_TEXTURE_WIDTH 2048\r
24 #define BLOCK_TEXTURE_HEIGHT 1024\r
25 #define BLOCK_SIZE 64\r
26 #define BLOCK_PER_ROW 32\r
27 \r
28 // This implementation is for framed coded pictures only!\r
29 // I did not find any DVB recording with field coded pictures\r
30 // so I do not think they exist here, anyway I suspect we\r
31 // need a totally different shader in this case\r
32 // so first write the frame coded shader\r
33 // and then later add if necessary field based support.\r
34 \r
35 const GLchar moco_vertex_shader[] =\r
36                 "attribute vec4 block_pos;\n"\r
37                 "attribute vec2 block_edge;\n"\r
38                 "attribute vec4 block_types;\n"\r
39                 "attribute vec2 index;\n"\r
40                 "attribute vec4 PMV1;\n"\r
41                 "attribute vec4 PMV2;\n"\r
42                 "attribute float cbp;\n"\r
43                 "uniform vec2 pict_scale;\n"\r
44         //      "attribute vec2 tex_coord;\n"\r
45                 "varying vec4 out_yblockpos_x;\n"\r
46                 "varying vec4 out_yblockpos_y;\n"\r
47                 "varying vec4 out_uvblockpos_xy;\n"\r
48                 "varying vec2 out_block_edge;\n"\r
49                 "varying vec4 out_misc_info;\n"\r
50 \r
51                 "varying float out_unsupported;\n"\r
52                 "varying vec4 out_moco_pos;\n"\r
53                 "varying vec4 out_moco_forward;\n"\r
54                 "varying vec4 out_moco_backward;\n"\r
55 \r
56 \r
57                 "const float one=1.0;\n"\r
58                 "const float czero=0.0;\n"\r
59                 "const float chalf=0.5;\n"\r
60                 "const float c1024=1024.0;\n"\r
61                 "const float c32=32.0;\n"\r
62         //      "const float c1over32=0.03125;\n"\r
63                 "const float c1over32=1./32.;\n"\r
64 \r
65 \r
66                 "const float c65536=65536.;\n"\r
67 \r
68 \r
69 \r
70                 "const vec4  y_cbp=vec4(1.0,2.0,4.0,8.0);\n"\r
71                 "const vec2  uv_cbp=vec2(16.0,32.0);\n"\r
72                 "void main()\n"\r
73                 "{\n"\r
74                 // calculate cbp\r
75                 "  vec4 cbp_vec=vec4(cbp,cbp,cbp,cbp);\n"\r
76                 "  vec4 fmod_cbp_y=mod(cbp_vec,y_cbp);\n"\r
77                 "  vec2 fmod_cbp_uv=mod(cbp_vec.xy,uv_cbp);\n"\r
78                 "  fmod_cbp_y.x=0.;\n"\r
79                 "  fmod_cbp_y=sign(vec4(fmod_cbp_y.yzw,fmod_cbp_uv.x)-fmod_cbp_y);\n"\r
80                 "  fmod_cbp_uv=sign(vec2(fmod_cbp_uv.y,cbp)-fmod_cbp_uv);\n"\r
81                 // resulting vector should indicate with block is present\r
82                 // now transform it to a sum (TODO vectorize this)\r
83                 "  vec4 fmod_cbp_y2=fmod_cbp_y;\n"\r
84                 "  vec2 fmod_cbp_uv2=fmod_cbp_uv;\n"\r
85                 "  fmod_cbp_y2.y+=fmod_cbp_y2.x;\n"\r
86                 "  fmod_cbp_y2.z+=fmod_cbp_y2.y;\n"\r
87                 "  fmod_cbp_y2.w+=fmod_cbp_y2.z;\n"\r
88                 "  fmod_cbp_uv2.x+=fmod_cbp_y2.w;\n"\r
89                 "  fmod_cbp_uv2.y+=fmod_cbp_uv2.x;\n"\r
90 \r
91                 // now calculate their position inside the short array\r
92                 "  float m_index=index.x/*+index.y*c65536*/-one;\n" //Not Endian save\r
93                 "  fmod_cbp_y2+=m_index;\n" //shift to the blocks\r
94                 "  fmod_cbp_uv2+=m_index;\n"\r
95                 "  out_yblockpos_x=(mod(fmod_cbp_y2,c32));\n"\r
96             "  out_uvblockpos_xy.xy=(mod(fmod_cbp_uv2,c32));\n"\r
97                 "  out_yblockpos_y=floor((fmod_cbp_y2)/c32);\n"\r
98                 "  out_uvblockpos_xy.zw=floor((fmod_cbp_uv2)/c32);\n"\r
99 \r
100                 "  out_yblockpos_y=floor((fmod_cbp_y2)*c1over32);\n"\r
101                 "  out_uvblockpos_xy.zw=floor((fmod_cbp_uv2)*c1over32);\n"\r
102                 "  out_yblockpos_x=fmod_cbp_y2-c32*out_yblockpos_y;\n"\r
103             "  out_uvblockpos_xy.xy=fmod_cbp_uv2-c32*out_uvblockpos_xy.zw;\n"\r
104 \r
105 \r
106 \r
107                 //Kick out uncoded blocks\r
108                 "  out_yblockpos_y-=sign(fmod_cbp_y-one)*c1024;\n"\r
109                 "  out_uvblockpos_xy.zw-=sign(fmod_cbp_uv-one)*c1024;\n"\r
110 \r
111                 // set start positions\r
112                 "  vec4 motion_vertical=vec4(block_types.z,block_types.z,block_types.z,block_types.z);\n"\r
113                 "  vec4 mod_motion_vertical=mod(motion_vertical,y_cbp);\n"\r
114                 "  mod_motion_vertical=sign(vec4(mod_motion_vertical.yzw,block_types.z)-mod_motion_vertical);\n"\r
115 \r
116                 "  out_moco_pos.xy=(block_pos.xy+block_edge.xy)*uv_cbp.x/*16*/+one;"\r
117                 "  out_unsupported=czero;\n"\r
118                 "  out_moco_pos.zw=vec2(czero,czero);\n"\r
119                 "  out_misc_info.y=one;\n" // this is the y step\r
120                 "  if (block_types.x<uv_cbp.x /* 16*/) {\n" // not intra coded\r
121                 "         float forward_backward=mod(block_types.x,y_cbp.w /* 8*/);\n"\r
122                 "     out_moco_pos.zw=vec2(one,czero);\n"\r
123                 "     if (forward_backward>y_cbp.z /* 4*/) {\n"\r
124                 "         out_moco_pos.zw=vec2(chalf,chalf);\n"\r
125                 //"          out_unsupported=2.;\n"\r
126                 "     } else if (forward_backward>y_cbp.y /* 2*/ ) {\n" //backward\r
127                 "         out_moco_pos.zw=vec2(czero,one);\n"\r
128                 //"          out_unsupported=2.;\n"\r
129                 "     } else {\n" //forward\r
130                 "         out_moco_pos.zw=vec2(one,czero);\n"\r
131                 "     } \n"\r
132                 "     if ((out_moco_pos.w+out_moco_pos.z)>y_cbp.x) {"\r
133                 "         out_moco_pos.zw*=chalf;\n"\r
134                 "     }\n"\r
135                 "     if (block_types.y==y_cbp.y /*2*/) {" //Frame\r
136                 "        out_moco_forward=vec4(PMV1.xy,PMV1.xy);\n"\r
137                 "        out_moco_backward=vec4(PMV1.zw,PMV1.zw);\n"\r
138                 //"          out_unsupported=2.;\n"\r
139                 "     } else if (block_types.y==y_cbp.x /*1*/) {" //Field\r
140                 "        out_moco_forward=vec4(PMV1.xy,PMV2.xy);\n"\r
141                 "        out_moco_backward=vec4(PMV1.zw,PMV2.zw);\n"\r
142                 "        out_moco_forward.y+=mod_motion_vertical.x;\n"\r
143                 "        out_moco_backward.y+=mod_motion_vertical.y;\n"\r
144                 "        out_moco_forward.w-=one-mod_motion_vertical.z;\n"\r
145                 "        out_moco_backward.w-=one-mod_motion_vertical.w;\n"\r
146                 "         out_misc_info.y=one;\n" // step to for field based\r
147         //      "            out_unsupported=2.;\n"\r
148                 "     } else {\n"\r
149                 "            out_unsupported=1.;\n"\r
150                 "     }\n"\r
151                 "  }\n"\r
152 \r
153 \r
154                 "  out_block_edge=block_edge;\n"\r
155                 "  out_misc_info.x=block_types.w;\n"\r
156                 "  vec4 out_pos=block_pos;\n"\r
157                 "  out_pos.xy+=block_edge;\n"\r
158                 "  out_pos.xy*=pict_scale;\n"\r
159                 "  out_pos.xy-=vec2(one,one);"\r
160                 "  out_pos.zw=vec2(one,one);"\r
161                 " gl_Position=out_pos;\n"\r
162                 "}\n";\r
163 \r
164 const GLchar moco_frag_shader[] =\r
165                 "precision mediump float;\n"\r
166                 "uniform sampler2D blocks;\n"\r
167                 "uniform sampler2D forward_pic;\n"\r
168                 "uniform sampler2D backward_pic;\n"\r
169                 "uniform vec2 pict_size;\n"\r
170                 "varying vec4 out_yblockpos_x;\n"\r
171                 "varying vec4 out_yblockpos_y;\n"\r
172                 "varying vec4 out_uvblockpos_xy;\n"\r
173                 "varying vec2 out_block_edge;\n"\r
174                 "varying vec4 out_misc_info;\n"// misc info x= DCT info, y= field based offsets\r
175                 "varying float out_unsupported;\n"\r
176                 "varying vec4 out_moco_forward;\n"\r
177                 "varying vec4 out_moco_backward;\n"\r
178                 "varying vec4 out_moco_pos;\n"\r
179 \r
180                 "const float halfst=0.5;\n"\r
181                 "const float ctwo=2.0;\n"\r
182                 "const float cone=1.0;\n"\r
183                 "const float czero=0.0;\n"\r
184                 "const float sieben=7.0;\n"\r
185                 "const float acht=8.0;\n"\r
186                 "const float s8=1./8.;\n"\r
187                 "const float s16=1./16.;\n"\r
188                 "const float c1over2048=1./2048.;\n"\r
189                 "const float c1over1024=1./1024.;\n"\r
190                 "const float c255=255.;\n"\r
191                 "const float c65280=65280.;\n"\r
192                 "const float c65536=65536.;\n"\r
193                 "const float cstep=32767.;\n"\r
194                 "\n"\r
195                 "float unpack_short(vec2 income) {\n"\r
196                 "   float temp=income.y*c65280+income.x*c255;\n"\r
197                 "   temp-=step(cstep,temp)*c65536;\n"\r
198                 "   return temp/c255;\n"\r
199                 "}"\r
200         //      "uniform sampler2D textureV;\n"\r
201         //      "uniform sampler2D textureY;\n"\r
202 //              "varying vec2 out_texCoord;\n"\r
203                 "void main()\n"\r
204                 "{\n"\r
205                 // first figure out the block num for y, first decide between up and down, fix me field\r
206         //non field code\r
207                 " vec4 ypos_temp1;\n"\r
208                 " vec4 our_fragcol=vec4(czero,czero,czero,cone);\n"\r
209                 " bool upper_half;\n"\r
210                 " float line_step;\n"\r
211                 " if (out_misc_info.x!=0.) {\n" //test the dct type\r
212                 "  upper_half=mod(floor(out_block_edge.y*acht),ctwo)==cone;\n"\r
213                 "  line_step=cone;\n"\r
214                 "}else{\n"\r
215                 "  upper_half=halfst<out_block_edge.y;\n"\r
216                 "  line_step=ctwo;\n"\r
217                 "}\n"\r
218                 " if (upper_half) {\n"\r
219                 "  ypos_temp1=vec4(out_yblockpos_x.z,out_yblockpos_y.z,out_yblockpos_x.w,out_yblockpos_y.w);\n"\r
220                 "  } else {\n"\r
221                 "    ypos_temp1=vec4(out_yblockpos_x.x,out_yblockpos_y.x,out_yblockpos_x.y,out_yblockpos_y.y);\n"\r
222                 " }\n"\r
223     //  " vec4 ypos_temp1=mix(vec4(out_yblockpos_x.x,out_yblockpos_y.x,out_yblockpos_x.y,out_yblockpos_y.y),"\r
224         //      "                  vec4(out_yblockpos_x.z,out_yblockpos_y.z,out_yblockpos_x.w,out_yblockpos_y.w),step(halfst,out_block_edge.y));\n"\r
225                 //field code\r
226         //      " vec4 ypos_temp1=mix(vec4(out_yblockpos_x.x,out_yblockpos_y.x,out_yblockpos_x.y,out_yblockpos_y.y),"\r
227         //                      "                  vec4(out_yblockpos_x.z,out_yblockpos_y.z,out_yblockpos_x.w,out_yblockpos_y.w),step(s16,mod(out_block_edge.y,s8)));\n"\r
228 \r
229                 //decide between left and right\r
230                 " vec4 ypos_temp2;\n"\r
231                 "if (halfst<out_block_edge.x){\n"\r
232                 "  ypos_temp2.xy=ypos_temp1.zw;\n"\r
233                 " }else {\n"\r
234                 "  ypos_temp2.xy=ypos_temp1.xy;\n"\r
235                 " }\n"\r
236                 //now get the intra 8x8 Block coordinates\r
237                 " vec2 ypos_temp3=floor(mod(out_block_edge*vec2(ctwo,line_step),cone)*acht);\n"\r
238                 " ypos_temp2.x=(ypos_temp3.x+(ypos_temp3.y+acht*(ypos_temp2.x))*acht);\n"\r
239                 //" bool subsample=(mod(ypos_temp2.x,ctwo)>halfst);\n"\r
240                 " bool subsample=mod(ypos_temp3.x,ctwo)==cone;\n"\r
241                 " ypos_temp2.x*=halfst;\n"\r
242                 " ypos_temp2.xy*=c1over1024;\n"\r
243                 " ypos_temp1=texture2D(blocks,ypos_temp2.xy);\n" // now select the right data\r
244                 "if (subsample){\n"\r
245                 "  ypos_temp2.xy=ypos_temp1.zw;\n"\r
246                 //"  ypos_temp2=vec2(cone,0.);\n"\r
247                 " }else {\n"\r
248                 "  ypos_temp2.xy=ypos_temp1.xy;\n"\r
249                 " }\n"\r
250                 " our_fragcol.r=unpack_short(ypos_temp2.rg);\n"\r
251 \r
252                 //now uv\r
253                 " ypos_temp3=floor(out_block_edge*acht);\n"\r
254                 " ypos_temp2.yw=floor(out_uvblockpos_xy.zw);\n"\r
255                 " ypos_temp2.xz=(ypos_temp3.x+(ypos_temp3.y+acht*(out_uvblockpos_xy.xy))*acht);\n"\r
256                                 //" bool subsample=(mod(ypos_temp2.x,ctwo)>halfst);\n"\r
257                 " subsample=mod(ypos_temp3.x,ctwo)==cone;\n"\r
258                 " ypos_temp2.xz*=halfst;\n"\r
259                 " ypos_temp2*=c1over1024;\n"\r
260 \r
261                 " ypos_temp1=texture2D(blocks,ypos_temp2.xy);\n" // now select the right data\r
262                 " if (subsample){\n"\r
263                 "  ypos_temp2.xy=ypos_temp1.zw;\n"\r
264                 " }else {\n"\r
265                 "  ypos_temp2.xy=ypos_temp1.xy;\n"\r
266                 " }\n"\r
267                 " our_fragcol.g=unpack_short(ypos_temp2.rg);\n"\r
268 \r
269                 " ypos_temp1=texture2D(blocks,ypos_temp2.zw);\n" // now select the right data\r
270                 " if (subsample){\n"\r
271                 "  ypos_temp2.xy=ypos_temp1.zw;\n"\r
272                 " }else {\n"\r
273                 "  ypos_temp2.xy=ypos_temp1.xy;\n"\r
274                 " }\n"\r
275                 " our_fragcol.b=unpack_short(ypos_temp2.rg);\n"\r
276                 //MOCO\r
277                 // decide if it is top or bottom\r
278                 " vec4 moco_work;\n"\r
279                 " if (mod(floor(out_moco_pos.y),ctwo)!=cone) {\n"\r
280                 "      moco_work=vec4(out_moco_forward.xy,out_moco_backward.xy);\n"\r
281                 "} else {\n"\r
282                 "      moco_work=vec4(out_moco_forward.zw,out_moco_backward.zw);\n"\r
283                 "}\n"\r
284                 // now we handle the y fetching\r
285                 " vec4 moco_temp;\n"\r
286                 " vec4 subpixel=floor(mod(moco_work,ctwo))*halfst; \n"\r
287                 " moco_temp=floor(moco_work*halfst+halfst)+floor(vec4(out_moco_pos.xy,out_moco_pos.xy));\n"\r
288                 " vec4 c00,c01,c10,c11;\n"\r
289 \r
290                 " c00=texture2D(forward_pic,moco_temp.xy*pict_size);\n"\r
291                 " c01=texture2D(forward_pic,(moco_temp.xy+vec2(czero,out_misc_info.y))*pict_size);\n"\r
292                 " c10=texture2D(forward_pic,(moco_temp.xy+vec2(cone,czero))*pict_size);\n"\r
293                 " c11=texture2D(forward_pic,(moco_temp.xy+vec2(cone,out_misc_info.y))*pict_size);\n"\r
294                 " our_fragcol.r+=mix(mix(c00.r,c10.r,subpixel.x),mix(c01.r,c11.r,subpixel.x),subpixel.y)*out_moco_pos.z;\n"\r
295 \r
296 \r
297                 " c00=texture2D(backward_pic,moco_temp.zw*pict_size);\n"//backward\r
298                 " c01=texture2D(backward_pic,(moco_temp.zw+vec2(czero,out_misc_info.y))*pict_size);\n"\r
299                 " c10=texture2D(backward_pic,(moco_temp.zw+vec2(cone,czero))*pict_size);\n"\r
300                 " c11=texture2D(backward_pic,(moco_temp.zw+vec2(cone,out_misc_info.y))*pict_size);\n"\r
301                 " our_fragcol.r+=mix(mix(c00.r,c10.r,subpixel.z),mix(c01.r,c11.r,subpixel.z),subpixel.w)*out_moco_pos.w;\n"\r
302                 // now handle UV\r
303                 " subpixel=floor(mod(moco_work*halfst,ctwo))*halfst; \n"\r
304                 " moco_temp=(floor(moco_work*halfst*halfst)+floor(vec4(out_moco_pos.xy,out_moco_pos.xy)*halfst))*ctwo;\n"\r
305 \r
306                 " c00=texture2D(forward_pic,moco_temp.xy*pict_size);\n"\r
307                 " c01=texture2D(forward_pic,(moco_temp.xy+vec2(czero,ctwo*out_misc_info.y))*pict_size);\n"\r
308                 " c10=texture2D(forward_pic,(moco_temp.xy+vec2(ctwo,czero))*pict_size);\n"\r
309                 " c11=texture2D(forward_pic,(moco_temp.xy+vec2(ctwo,ctwo*out_misc_info.y))*pict_size);\n"\r
310                 " our_fragcol.gb+=mix(mix(c00.gb,c10.gb,subpixel.x),mix(c01.gb,c11.gb,subpixel.x),subpixel.y)*out_moco_pos.z;\n"\r
311 \r
312                 " c00=texture2D(backward_pic,moco_temp.zw*pict_size);\n"\r
313                 " c01=texture2D(backward_pic,(moco_temp.zw+vec2(czero,ctwo*out_misc_info.y))*pict_size);\n"\r
314                 " c10=texture2D(backward_pic,(moco_temp.zw+vec2(ctwo,czero))*pict_size);\n"\r
315                 " c11=texture2D(backward_pic,(moco_temp.zw+vec2(ctwo,ctwo*out_misc_info.y))*pict_size);\n"\r
316                 " our_fragcol.gb+=mix(mix(c00.gb,c10.gb,subpixel.z),mix(c01.gb,c11.gb,subpixel.z),subpixel.w)*out_moco_pos.w;\n"\r
317 \r
318                 "  gl_FragColor.rgb=vec3(our_fragcol.r,halfst,halfst);\n"\r
319                 " if (out_unsupported==1.) {\n"\r
320                 "    gl_FragColor.gb=vec2(cone,0.0);\n"\r
321                 "}else if (out_unsupported==2.) {\n"\r
322                 "    gl_FragColor.gb=vec2(0.0,cone);\n"\r
323                 "}\n"\r
324 \r
325 \r
326                 //" ypos_temp2=mix(ypos_temp1.rg,ypos_temp1.ba,step(c1over2047,mod(ypos_temp2.x,c1over1023)));\n" //activate this\r
327 \r
328 \r
329                 //" gl_FragColor.r=subsample/acht;\n" //signed later TODO\r
330                 //" gl_FragColor.g=0.5;\n"\r
331                 //" gl_FragColor.b=0.5;\n"\r
332                 " gl_FragColor.a=1.0;\n"\r
333                 //" vec4 blocks=texture2D(blocks,gl_FragCoord.xy/600.);\n"\r
334 //              "help.x=texture2D(textureY,out_texCoord).r;\n"\r
335 //              "help.y=texture2D(textureU,out_texCoord).r;\n"\r
336 //              "help.z=texture2D(textureV,out_texCoord).r;\n" //-uv_corr;\n"\r
337    //   "  gl_FragColor.rgba=blocks;\n"\r
338         //      "  gl_FragColor.rgba=vec4(0.7,0.5,0.5,1.0);\n"\r
339                 //"  gl_FragColor.a=1.;\n"\r
340                 "}\n";\r
341 \r
342 GLMocoShader::GLMocoShader(): GLShader("GLMocoShader")\r
343 {\r
344 \r
345         frame_buf=0;\r
346         blocks_loc=0;\r
347 }\r
348 \r
349 GLMocoShader::~GLMocoShader()\r
350 {\r
351         //parent does everything\r
352 }\r
353 \r
354 int GLMocoShader::init() {\r
355 \r
356         if (!initShaders(moco_vertex_shader, moco_frag_shader)) {\r
357                 Log::getInstance()->log("GLMocoShader", Log::ERR, "init Shaders failed for GLMocoShader");\r
358                 return 0;\r
359         }\r
360         loc_pict_scale = glGetUniformLocation(shad_program, "pict_scale");\r
361         blocks_loc = glGetUniformLocation(shad_program, "blocks");\r
362         forward_pic_loc = glGetUniformLocation(shad_program, "forward_pic");\r
363         backward_pic_loc = glGetUniformLocation(shad_program, "backward_pic");\r
364     pict_size_loc = glGetUniformLocation(shad_program, "pict_size");\r
365 \r
366 //      frame_sampler_locY = glGetUniformLocation(shad_program, "textureY");\r
367         // Log::getInstance()->log("OSD", Log::WARN, "uniform location %x %x",frame_sampler_locY,glGetError());\r
368 //      frame_sampler_locU = glGetUniformLocation(shad_program, "textureU");\r
369         //Log::getInstance()->log("OSD", Log::WARN, "uniform location %x %x",frame_sampler_locU,glGetError());\r
370 //      frame_sampler_locV = glGetUniformLocation(shad_program, "textureV");\r
371         glGenFramebuffers(1, &frame_buf);\r
372         //Log::getInstance()->log("GLMocoShader", Log::WARN, "genframe bufmark1 glerror %x",glGetError());\r
373         glGenTextures(1, &data_blocks);\r
374         glBindTexture(GL_TEXTURE_2D, data_blocks);\r
375         glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, BLOCK_TEXTURE_WIDTH >>1, BLOCK_TEXTURE_HEIGHT, 0, GL_RGBA,\r
376                                 GL_UNSIGNED_BYTE, NULL);\r
377         char buffer[BLOCK_TEXTURE_WIDTH*4];\r
378         memset(buffer,0,BLOCK_TEXTURE_WIDTH*4); // the last line is black, simplifies the shader algorithms\r
379 \r
380         glTexSubImage2D(GL_TEXTURE_2D,0,0,BLOCK_TEXTURE_HEIGHT-1,\r
381                         1024,1,\r
382                         GL_RGBA,GL_UNSIGNED_BYTE,\r
383                         buffer);\r
384 \r
385         glGenBuffers(1,&macro_block);\r
386         glBindBuffer(GL_ARRAY_BUFFER,macro_block);\r
387         glBufferData(GL_ARRAY_BUFFER,4096*4*sizeof(XvMCMacroBlock),NULL,GL_DYNAMIC_DRAW);\r
388         valid_macro_blocks=0;\r
389 \r
390 \r
391         glGenBuffers(1,&triangles);\r
392         glBindBuffer(GL_ELEMENT_ARRAY_BUFFER,triangles);\r
393         GLushort *tri_indices=(GLushort *)malloc(sizeof(GLushort)*4096*6);\r
394         if (!tri_indices) {\r
395                 Log::getInstance()->log("GLMocoShader", Log::WARN, "allocating triindices failed");\r
396                 return 0;\r
397         }\r
398         GLushort *tri_indices_run=tri_indices;\r
399         for (int i=0;i<4096;i++) {\r
400                 *tri_indices_run=i+0*4096;  //strip\r
401                 tri_indices_run++;\r
402                 *tri_indices_run=i+2*4096;  //strip\r
403                 tri_indices_run++;\r
404                 *tri_indices_run=i+1*4096;  //strip\r
405                 tri_indices_run++;\r
406                 *tri_indices_run=i+3*4096;  //strip\r
407                 tri_indices_run++;\r
408                 *tri_indices_run=i+3*4096;  //strip\r
409                 tri_indices_run++;\r
410                 *tri_indices_run=i+1+0*4096;  //strip\r
411                 tri_indices_run++;\r
412         }\r
413 \r
414 \r
415 \r
416 \r
417 \r
418         glBufferData(GL_ELEMENT_ARRAY_BUFFER,4096*6*sizeof(GLushort),tri_indices,GL_STREAM_DRAW);\r
419         free(tri_indices);\r
420 \r
421         glBindBuffer(GL_ELEMENT_ARRAY_BUFFER,0);\r
422         glBindBuffer(GL_ARRAY_BUFFER,0);\r
423 \r
424 \r
425 \r
426         return 1;\r
427 \r
428 }\r
429 \r
430 int GLMocoShader::deinit()\r
431 {\r
432         glDeleteFramebuffers(1, &frame_buf);\r
433         glDeleteTextures(1, &data_blocks);\r
434         return deinitShaders();\r
435 }\r
436 \r
437 \r
438 \r
439 int GLMocoShader::uploadDataBlocks(short* blocks,unsigned int num_blocks, XvMCMacroBlock *m_blocks,unsigned int num_m_blocks)\r
440 {\r
441         unsigned int height=(num_blocks)/(BLOCK_PER_ROW);\r
442 \r
443         glBindTexture(GL_TEXTURE_2D, data_blocks);\r
444         glPixelStorei(GL_UNPACK_ALIGNMENT,1);\r
445 \r
446 \r
447 \r
448 \r
449 //      Log::getInstance()->log("GLMocoShader", Log::WARN, "uploadDataBlocks mark1 glerror %x %d",glGetError(),height);\r
450         glTexSubImage2D(GL_TEXTURE_2D,0,0,0,\r
451                 BLOCK_TEXTURE_WIDTH>>1,height,\r
452                 GL_RGBA,GL_UNSIGNED_BYTE,\r
453                 blocks);\r
454 //      Log::getInstance()->log("GLMocoShader", Log::WARN, "uploadDataBlocks mark2 glerror %x",glGetError());\r
455         glTexSubImage2D(GL_TEXTURE_2D,0,0,height,\r
456                         ((num_blocks%BLOCK_PER_ROW)>>1)*BLOCK_SIZE,1,\r
457                         GL_RGBA,GL_UNSIGNED_BYTE,\r
458                         blocks+height*BLOCK_PER_ROW*BLOCK_SIZE);\r
459 \r
460         //Log::getInstance()->log("GLMocoShader", Log::WARN, "uploadDataBlocks mark2 glerror %x",glGetError());\r
461 \r
462 \r
463 \r
464         glBindTexture(GL_TEXTURE_2D, 0);\r
465 \r
466         valid_macro_blocks=num_m_blocks;\r
467         glBindBuffer(GL_ARRAY_BUFFER,macro_block);\r
468         XvMCMacroBlock *m_blocks_run=m_blocks;\r
469         XvMCMacroBlock *m_blocks_end=m_blocks+num_m_blocks;\r
470         while (m_blocks_run!=m_blocks_end) {\r
471                 m_blocks_run->pad0=0xFF00;\r
472                 m_blocks_run++;\r
473         }\r
474         // debug\r
475 /*      m_blocks[0].x=0;\r
476         m_blocks[0].y=0;\r
477         m_blocks[3]=m_blocks[2]=m_blocks[1]=m_blocks[0];\r
478         m_blocks[0].pad0=0xFF00;\r
479         m_blocks[1].pad0=0xFFFF;\r
480         m_blocks[2].pad0=0x0000;\r
481         m_blocks[3].pad0=0x00FF;\r
482         m_blocks[1].x=0;\r
483         m_blocks[1].y=0;\r
484         m_blocks[2].x=0;\r
485         m_blocks[2].y=0;\r
486         m_blocks[3].x=0;\r
487         m_blocks[3].y=0;*/\r
488 \r
489         glBufferSubData(GL_ARRAY_BUFFER, 0* sizeof(XvMCMacroBlock), num_m_blocks * sizeof(XvMCMacroBlock),\r
490                         m_blocks);\r
491 \r
492 \r
493         m_blocks_run = m_blocks;\r
494         m_blocks_end = m_blocks + num_m_blocks;\r
495         while (m_blocks_run != m_blocks_end) {\r
496                 m_blocks_run->pad0 = 0xFFFF;\r
497                 m_blocks_run++;\r
498         }\r
499         glBufferSubData(GL_ARRAY_BUFFER, 4096* sizeof(XvMCMacroBlock), num_m_blocks * sizeof(XvMCMacroBlock),\r
500                         m_blocks);\r
501 \r
502 \r
503         m_blocks_run = m_blocks;\r
504         m_blocks_end = m_blocks + num_m_blocks;\r
505         while (m_blocks_run != m_blocks_end) {\r
506                 m_blocks_run->pad0 = 0x0000;\r
507                 m_blocks_run++;\r
508         }\r
509         glBufferSubData(GL_ARRAY_BUFFER, 2 * 4096* sizeof(XvMCMacroBlock),\r
510                         num_m_blocks * sizeof(XvMCMacroBlock), m_blocks);\r
511 \r
512 \r
513         m_blocks_run = m_blocks;\r
514         m_blocks_end = m_blocks + num_m_blocks;\r
515         while (m_blocks_run != m_blocks_end) {\r
516                 m_blocks_run->pad0 = 0x00FF;\r
517                 m_blocks_run++;\r
518         }\r
519         glBufferSubData(GL_ARRAY_BUFFER, 3 * 4096* sizeof(XvMCMacroBlock),\r
520                         num_m_blocks * sizeof(XvMCMacroBlock), m_blocks);\r
521 \r
522 \r
523         glBindBuffer(GL_ARRAY_BUFFER,0);\r
524 \r
525         return 1;\r
526 \r
527 \r
528 \r
529 }\r
530 \r
531 int GLMocoShader::doMoCo(VPEOGLFrame *target,VPEOGLFrame *forward,VPEOGLFrame *backward)\r
532 {\r
533 \r
534 //      Log::getInstance()->log("GLMocoShader", Log::WARN, "doMoCo mark-2 glerror %x %x",glGetError(),frame_buf);\r
535 \r
536         //Log::getInstance()->log("GLMocoShader", Log::WARN, "doMoCo mark-1 glerror %x %x",glGetError(),frame_buf);\r
537         glBindFramebuffer(GL_FRAMEBUFFER, frame_buf);\r
538         //Log::getInstance()->log("GLMocoShader", Log::WARN, "doMoCo mark1 glerror %x %x %x",glGetError(),target->textures[0],target->textures[1]);\r
539 \r
540 \r
541         glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0,\r
542                         GL_TEXTURE_2D, target->textures[0], 0);\r
543         //Log::getInstance()->log("GLMocoShader", Log::WARN, "doMoCo mark1apres glerror %x",glGetError());\r
544 \r
545 \r
546 \r
547 \r
548         //Log::getInstance()->log("GLMocoShader", Log::WARN, "doMoCo mark2 glerror %x",glGetError());\r
549 \r
550         //Log::getInstance()->log("GLMocoShader", Log::WARN, "doMoCo mark2a glerror %x",glGetError());\r
551 \r
552         int status = glCheckFramebufferStatus(GL_FRAMEBUFFER);\r
553         if (status == GL_FRAMEBUFFER_COMPLETE) {\r
554                 glViewport(0, 0, target->width,target->height);\r
555         //      Log::getInstance()->log("GLMocoShader", Log::WARN, "doMoCo mark2b glerror %x",glGetError());\r
556 \r
557                 XvMCMacroBlock * helperp=NULL;\r
558 \r
559                 glClearColor(0.0f,0.5f,0.5f,1.f); //this black\r
560                 glClear(GL_COLOR_BUFFER_BIT);\r
561                 //Log::getInstance()->log("GLMocoShader", Log::WARN, "doMoCo mark2c glerror %x",glGetError());\r
562 \r
563                 glUseProgram(shad_program);\r
564                 //Log::getInstance()->log("GLMocoShader", Log::WARN, "doMoCo mark3 glerror %x",glGetError());\r
565 \r
566 \r
567                 const GLfloat pict_scale[]={  32./((float)target->width),32./((float)target->height)};\r
568 \r
569                 glUniform2fv(loc_pict_scale,1,(const GLfloat*)&pict_scale);\r
570 \r
571 \r
572                 const GLfloat pict_size[]={  1./((float)target->width),1./((float)target->height)};\r
573                 glUniform2fv(pict_size_loc,1,(const GLfloat*)&pict_size);\r
574 \r
575                 glBindBuffer(GL_ELEMENT_ARRAY_BUFFER,triangles);\r
576                                 //Log::getInstance()->log("GLMocoShader", Log::WARN, "doMoCo mark5 glerror %x",glGetError());\r
577                 glBindBuffer(GL_ARRAY_BUFFER,macro_block);\r
578 \r
579 \r
580 \r
581                 glVertexAttribPointer(0, 2, GL_SHORT, GL_FALSE,\r
582                                 sizeof(XvMCMacroBlock), NULL);\r
583 \r
584                 glEnableVertexAttribArray(0);\r
585 \r
586                 glVertexAttribPointer(1, 2, GL_UNSIGNED_BYTE, GL_TRUE,sizeof(XvMCMacroBlock), (const void *) &(helperp->pad0));\r
587                 glEnableVertexAttribArray(1);\r
588 \r
589                 glVertexAttribPointer(2, 4, GL_UNSIGNED_BYTE, GL_FALSE,sizeof(XvMCMacroBlock), (const void *) &(helperp->macroblock_type));\r
590                 glEnableVertexAttribArray(2);\r
591 \r
592                 glVertexAttribPointer(3, 1, GL_SHORT, GL_FALSE,sizeof(XvMCMacroBlock), (const void *) &(helperp->coded_block_pattern));\r
593                 glEnableVertexAttribArray(3);\r
594                 glVertexAttribPointer(4, 2, GL_UNSIGNED_SHORT, GL_FALSE,sizeof(XvMCMacroBlock), (const void *) &(helperp->index));\r
595                 glEnableVertexAttribArray(4);\r
596 \r
597                 glVertexAttribPointer(5, 4, GL_SHORT, GL_FALSE,sizeof(XvMCMacroBlock), (const void *) &(helperp->PMV[0][0][0]));\r
598                 glEnableVertexAttribArray(5);\r
599 \r
600                 glVertexAttribPointer(6, 4, GL_SHORT, GL_FALSE,sizeof(XvMCMacroBlock), (const void *) &(helperp->PMV[1][0][0]));\r
601                 glEnableVertexAttribArray(6);\r
602 \r
603 \r
604 \r
605 \r
606                 glActiveTexture(GL_TEXTURE0);\r
607                 glBindTexture(GL_TEXTURE_2D,data_blocks);\r
608                 glUniform1i(blocks_loc,0);\r
609                 glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_NEAREST);\r
610                 glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_NEAREST);\r
611                 glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_WRAP_S,GL_CLAMP_TO_EDGE);\r
612                 glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_WRAP_T,GL_CLAMP_TO_EDGE);\r
613 \r
614 \r
615                 if (forward) {\r
616                         Log::getInstance()->log("GLMocoShader", Log::WARN, "doMoCo forward %x",forward->textures[0]);\r
617                         glActiveTexture(GL_TEXTURE1);\r
618                         glBindTexture(GL_TEXTURE_2D,forward->textures[0]);\r
619                         glUniform1i(forward_pic_loc,1);\r
620                         glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_NEAREST);\r
621                         glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_NEAREST);\r
622                         glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_WRAP_S,GL_CLAMP_TO_EDGE);\r
623                         glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_WRAP_T,GL_CLAMP_TO_EDGE);\r
624 \r
625                 }\r
626 \r
627                 if (backward) {\r
628                         glActiveTexture(GL_TEXTURE2);\r
629                         Log::getInstance()->log("GLMocoShader", Log::WARN, "doMoCo backward %x",backward->textures[0]);\r
630                         glBindTexture(GL_TEXTURE_2D,backward->textures[0]);\r
631                         glUniform1i(backward_pic_loc,2);\r
632                         glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_NEAREST);\r
633                         glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_NEAREST);\r
634                         glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_WRAP_S,GL_CLAMP_TO_EDGE);\r
635                         glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_WRAP_T,GL_CLAMP_TO_EDGE);\r
636 \r
637                 }\r
638 \r
639 \r
640                 glDrawElements(GL_TRIANGLE_STRIP, valid_macro_blocks*6,\r
641                         GL_UNSIGNED_SHORT, NULL);\r
642 \r
643 \r
644                 /*\r
645                 glVertexAttribPointer(0, 2, GL_SHORT, GL_FALSE,sizeof(XvMCMacroBlock), 0);\r
646                 //Log::getInstance()->log("GLMocoShader", Log::WARN, "doMoCo mark3 glerror %x",glGetError());\r
647                 glEnableVertexAttribArray(0);\r
648 \r
649                 glVertexAttribPointer(1, 2, GL_UNSIGNED_BYTE, GL_TRUE,sizeof(XvMCMacroBlock), (const void *) &(helperp->pad0));\r
650                 //Log::getInstance()->log("GLMocoShader", Log::WARN, "doMoCo mark3 glerror %x",glGetError());\r
651                 glEnableVertexAttribArray(1);*/\r
652 \r
653         //      Log::getInstance()->log("GLMocoShader", Log::WARN, "doMoCo mark4 glerror %x",glGetError());\r
654 \r
655 \r
656                 //Log::getInstance()->log("GLMocoShader", Log::WARN, "doMoCo mark6 glerror %x",glGetError());\r
657 \r
658         //      glDrawElements(GL_TRIANGLE_STRIP,/*valid_macro_blocks*/4,GL_UNSIGNED_SHORT,0);\r
659                 //Log::getInstance()->log("GLMocoShader", Log::WARN, "doMoCo mark7 glerror %x %x",glGetError(),valid_macro_blocks);\r
660                 //cleanup\r
661 \r
662                 glBindBuffer(GL_ELEMENT_ARRAY_BUFFER,0);\r
663                 //Log::getInstance()->log("GLMocoShader", Log::WARN, "doMoCo mark5 glerror %x",glGetError());\r
664                 glBindBuffer(GL_ARRAY_BUFFER,0);\r
665 \r
666                 glBindFramebuffer(GL_FRAMEBUFFER, 0);\r
667                 glActiveTexture(GL_TEXTURE0);\r
668                 glBindTexture(GL_TEXTURE_2D, 0);\r
669                 glActiveTexture(GL_TEXTURE1);\r
670                 glBindTexture(GL_TEXTURE_2D, 0);\r
671                 glActiveTexture(GL_TEXTURE2);\r
672                 glBindTexture(GL_TEXTURE_2D, 0);\r
673                 //Log::getInstance()->log("GLMocoShader", Log::WARN, "doMoCo mark8 glerror %x",glGetError());\r
674         } else {\r
675                 glBindFramebuffer(GL_FRAMEBUFFER, 0);\r
676                 Log::getInstance()->log("GLMocoShader", Log::WARN, "framebuffer not complete %x",status);\r
677                 return -1;\r
678         }\r
679 \r
680         glFinish(); // since the main rendering is done in a different thread we have to make sure rendering is finished before passing the texture\r
681         // call this only in the last stage of rendering the moco frame\r
682         return 0;\r
683 \r
684 \r
685 \r
686 }\r
687 \r
688 /*\r
689 \r
690 int GLMocoShader::PrepareRendering(GLuint y_tex, GLuint u_tex, GLuint v_tex) { // This Function setups the rendering pipeline according to the shaders standards\r
691 \r
692         //Log::getInstance()->log("OSD", Log::WARN, "mark1 glerror %x",glGetError());\r
693 \r
694 \r
695         //Log::getInstance()->log("OSD", Log::WARN, "mark2 glerror %x",glGetError());\r
696 \r
697 \r
698         glActiveTexture( GL_TEXTURE2);\r
699         glBindTexture(GL_TEXTURE_2D, v_tex);\r
700         glUniform1i(frame_sampler_locV, 2);\r
701         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);\r
702         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);\r
703 \r
704         glActiveTexture( GL_TEXTURE1);\r
705         glBindTexture(GL_TEXTURE_2D, u_tex);\r
706         glUniform1i(frame_sampler_locU, 1);\r
707         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);\r
708         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);\r
709 \r
710         glActiveTexture( GL_TEXTURE0);\r
711         glBindTexture(GL_TEXTURE_2D, y_tex);\r
712         glUniform1i(frame_sampler_locY, 0);\r
713         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);\r
714         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);\r
715         return 1;\r
716 \r
717 }*/\r
718 \r
719 int GLMocoShader::BindAttributes()\r
720 {\r
721         glBindAttribLocation(shad_program,0,"block_pos");\r
722         glBindAttribLocation(shad_program,1,"block_edge");\r
723         glBindAttribLocation(shad_program,2,"block_types");\r
724         glBindAttribLocation(shad_program,3,"cbp");\r
725         glBindAttribLocation(shad_program,4,"index");\r
726         glBindAttribLocation(shad_program,5,"PMV1");\r
727         glBindAttribLocation(shad_program,6,"PMV2");\r
728 \r
729         //glBindAttribLocation(shad_program,1,"tex_coord");\r
730         return 1;\r
731 }\r