OpenCores
URL https://opencores.org/ocsvn/bluespec-h264/bluespec-h264/trunk

Subversion Repositories bluespec-h264

[/] [bluespec-h264/] [trunk/] [src/] [mkDeblockFilter.bsv] - Blame information for rev 13

Go to most recent revision | Details | Compare with Previous | View Log

Line No. Rev Author Line
1 2 jamey.hick
//**********************************************************************
2
// Deblocking Filter
3
//----------------------------------------------------------------------
4
//
5
//
6
 
7
package mkDeblockFilter;
8
 
9
import H264Types::*;
10
 
11
import IDeblockFilter::*;
12
import FIFO::*;
13
import Vector::*;
14
 
15
import Connectable::*;
16
import GetPut::*;
17
import ClientServer::*;
18 8 jamey.hick
import RegFile::*;
19 2 jamey.hick
 
20
 
21
 
22
//-----------------------------------------------------------
23
// Local Datatypes
24
//-----------------------------------------------------------
25
 
26
 
27 8 jamey.hick
typedef enum
28 2 jamey.hick
{
29 8 jamey.hick
  Passing,          //not working on anything in particular
30
  Initialize,
31
  Horizontal,
32
  Cleanup,
33
  HorizontalCleanup,
34
  Vertical
35 2 jamey.hick
}
36
Process deriving(Eq,Bits);
37
 
38 8 jamey.hick
typedef enum
39
{
40
  NormalOperation,
41
  VerticalCleanup
42
}
43
VerticalState deriving(Eq,Bits);
44 2 jamey.hick
 
45
//-----------------------------------------------------------
46
// Helper functions
47
 
48
 
49
function Bit#(8) absdiff8(Bit#(8) in0, Bit#(8) in1);
50
   return (in1>=in0 ? in1-in0 : in0-in1);
51
endfunction
52
 
53
 
54
function Bool filter_test(Bit#(32) in_pixels, Bit#(8) alpha, Bit#(5) beta);
55
   Bit#(8) p1 = in_pixels[7:0];
56
   Bit#(8) p0 = in_pixels[15:8];
57
   Bit#(8) q0 = in_pixels[23:16];
58
   Bit#(8) q1 = in_pixels[31:24];
59
   return((absdiff8(p0,q0) < alpha) &&
60
          (absdiff8(p0,p1) < zeroExtend(beta))  &&
61
          (absdiff8(q0,q1) < zeroExtend(beta)));
62
endfunction
63
 
64
 
65
function Bit#(6) clip3symmetric9to6(Bit#(9) val, Bit#(5) bound);
66
   Int#(9) intval = unpack(val);
67
   Int#(6) intbound = unpack({1'b0,bound});
68
   Int#(6) intout = (intvalsignExtend(intbound) ? intbound : truncate(intval)));
69
   return pack(intout);
70
endfunction
71
 
72
 
73
function Bit#(64) filter_input(Bit#(64) in_pixels, Bool chroma_flag, Bit#(3) bs, Bit#(8) alpha, Bit#(5) beta, Vector#(3,Bit#(5)) tc0_vector);
74
   Bit#(8) p[4];
75
   Bit#(8) q[4];
76
   p[3] = in_pixels[7:0];
77
   p[2] = in_pixels[15:8];
78
   p[1] = in_pixels[23:16];
79
   p[0] = in_pixels[31:24];
80
   q[0] = in_pixels[39:32];
81
   q[1] = in_pixels[47:40];
82
   q[2] = in_pixels[55:48];
83
   q[3] = in_pixels[63:56];
84
   Bit#(8) p_out[4];
85
   Bit#(8) q_out[4];
86
   Bool a_p_test = absdiff8(p[2],p[0]) < zeroExtend(beta);
87
   Bool a_q_test = absdiff8(q[2],q[0]) < zeroExtend(beta);
88
   Bit#(9) p0q0 = zeroExtend(p[0])+zeroExtend(q[0]);
89
   if (bs == 4)
90
      begin
91
         Bool small_gap_test = absdiff8(p[0],q[0]) < (alpha >> 2)+2;
92
         Bit#(11) p_outtemp[3];
93
         Bit#(11) q_outtemp[3];
94
         if (!chroma_flag && a_p_test && small_gap_test)
95
            begin
96
               Bit#(11) sum = zeroExtend(p[1])+zeroExtend(p0q0);
97
               p_outtemp[0] = (zeroExtend(p[2]) + (sum<<1) + zeroExtend(q[1]) + 4) >> 3;
98
               p_outtemp[1] = (zeroExtend(p[2]) + sum + 2) >> 2;
99
               p_outtemp[2] = (((zeroExtend(p[3])+zeroExtend(p[2]))<<1) + zeroExtend(p[2]) + sum + 4) >> 3;
100
            end
101
         else
102
            begin
103
               p_outtemp[0] = ((zeroExtend(p[1])<<1) + zeroExtend(p[0]) + zeroExtend(q[1]) + 2) >> 2;
104
               p_outtemp[1] = zeroExtend(p[1]);
105
               p_outtemp[2] = zeroExtend(p[2]);
106
            end
107
         if (!chroma_flag && a_q_test && small_gap_test)
108
            begin
109
               Bit#(11) sum = zeroExtend(q[1])+zeroExtend(p0q0);
110
               q_outtemp[0] = (zeroExtend(p[1]) + (sum<<1) + zeroExtend(q[2]) + 4) >> 3;
111
               q_outtemp[1] = (zeroExtend(q[2]) + sum + 2) >> 2;
112
               q_outtemp[2] = (((zeroExtend(q[3])+zeroExtend(q[2]))<<1) + zeroExtend(q[2]) + sum + 4) >> 3;
113
            end
114
         else
115
            begin
116
               q_outtemp[0] = ((zeroExtend(q[1])<<1) + zeroExtend(q[0]) + zeroExtend(p[1]) + 2) >> 2;
117
               q_outtemp[1] = zeroExtend(q[1]);
118
               q_outtemp[2] = zeroExtend(q[2]);
119
            end
120
         p_out[0] = truncate(p_outtemp[0]);
121
         p_out[1] = truncate(p_outtemp[1]);
122
         p_out[2] = truncate(p_outtemp[2]);
123
         q_out[0] = truncate(q_outtemp[0]);
124
         q_out[1] = truncate(q_outtemp[1]);
125
         q_out[2] = truncate(q_outtemp[2]);
126
      end
127
   else if(bs > 0)
128
      begin
129
         Bit#(5) t_c0 = tc0_vector[bs-1];
130
         Bit#(5) t_c = chroma_flag ? t_c0+1 : t_c0 + (a_p_test ? 1:0) + (a_q_test ? 1:0);
131
         Bit#(12) deltatemp = (((zeroExtend(q[0])-zeroExtend(p[0]))<<2)+zeroExtend(p[1])-zeroExtend(q[1])+4);
132
         Bit#(6) delta = clip3symmetric9to6(deltatemp[11:3],t_c);
133
 
134
         Bit#(10) p_out0temp = zeroExtend(p[0]) + signExtend(delta);
135
         p_out[0] = (p_out0temp[9]==1 ? 0 : (p_out0temp[8]==1 ? 255 : p_out0temp[7:0]));
136
         Bit#(10) q_out0temp = zeroExtend(q[0]) - signExtend(delta);
137
         q_out[0] = (q_out0temp[9]==1 ? 0 : (q_out0temp[8]==1 ? 255 : q_out0temp[7:0]));
138
 
139
         Bit#(9) p0q0PLUS1 = p0q0+1;
140
         Bit#(8) p0q0_av = p0q0PLUS1[8:1];
141
         if (!chroma_flag && a_p_test)
142
            begin
143
               Bit#(10) p_out1temp = zeroExtend(p[2]) + zeroExtend(p0q0_av) - (zeroExtend(p[1])<<1);
144
               p_out[1] = p[1]+signExtend(clip3symmetric9to6(p_out1temp[9:1],t_c0));
145
            end
146
         else
147
            p_out[1] = p[1];
148
 
149
         if (!chroma_flag && a_q_test)
150
            begin
151
               Bit#(10) q_out1temp = zeroExtend(q[2]) + zeroExtend(p0q0_av) - (zeroExtend(q[1])<<1);
152
               q_out[1] = q[1]+signExtend(clip3symmetric9to6(q_out1temp[9:1],t_c0));
153
            end
154
         else
155
            q_out[1] = q[1];
156
 
157
         p_out[2] = p[2];
158
         q_out[2] = q[2];
159
      end
160
   else
161
      begin
162
         p_out[0] = p[0];
163
         q_out[0] = q[0];
164
         p_out[1] = p[1];
165
         q_out[1] = q[1];
166
         p_out[2] = p[2];
167
         q_out[2] = q[2];
168
      end
169
   p_out[3] = p[3];
170
   q_out[3] = q[3];
171
   return({q_out[3], q_out[2], q_out[1], q_out[0], p_out[0], p_out[1], p_out[2], p_out[3]});
172
endfunction
173
 
174
 
175
 
176 6 jamey.hick
 
177
 
178 2 jamey.hick
//-----------------------------------------------------------
179
// Deblocking Filter Module
180
//-----------------------------------------------------------
181 6 jamey.hick
interface ILeftVector;
182 8 jamey.hick
  method ActionValue#(Bit#(32)) sub(Bit#(5) addr);
183
  method Action upd(Bit#(5) addr, Bit#(32) data);
184 6 jamey.hick
endinterface
185
 
186
(*synthesize*)
187
module mkLeftVector(ILeftVector);
188 8 jamey.hick
  RFile1#(Bit#(5),Bit#(32)) leftVector <- mkRFile1Full;
189 6 jamey.hick
  method sub = leftVector.sub;
190
  method upd = leftVector.upd;
191
endmodule
192 2 jamey.hick
 
193 8 jamey.hick
interface IWorkVectorVer;
194
  method ActionValue#(Bit#(32)) sub(Bit#(4) addr);
195
  method Action upd(Bit#(4) addr, Bit#(32) data);
196
endinterface
197
 
198
(*synthesize*)
199
module mkWorkVectorVer(IWorkVectorVer);
200
  RFile1#(Bit#(4),Bit#(32)) workVector <- mkRFile1Full();
201
  method sub = workVector.sub;
202
  method upd = workVector.upd;
203
endmodule
204 2 jamey.hick
 
205 8 jamey.hick
interface IWorkVectorHor;
206
  method ActionValue#(Bit#(32)) sub(Bit#(3) addr);
207
  method Action upd(Bit#(3) addr, Bit#(32) data);
208
endinterface
209
 
210
(*synthesize*)
211
module mkWorkVectorHor(IWorkVectorHor);
212
  RFile1#(Bit#(3),Bit#(32)) workVector <- mkRFile1Full();
213
  method sub = workVector.sub;
214
  method upd = workVector.upd;
215
endmodule
216 6 jamey.hick
 
217 8 jamey.hick
interface ITopVector;
218
  method ActionValue#(Bit#(32)) sub(Bit#(4) addr);
219
  method Action upd(Bit#(4) addr, Bit#(32) data);
220
endinterface
221
 
222
(*synthesize*)
223
module mkTopVector(ITopVector);
224
  RFile1#(Bit#(4),Bit#(32)) topVector <- mkRFile1Full();
225
  method sub = topVector.sub;
226
  method upd = topVector.upd;
227
endmodule
228 6 jamey.hick
 
229 11 jamey.hick
interface IbSVector;
230
  method ActionValue#(Bit#(3)) sub(Bit#(4) addr);
231
  method Action upd(Bit#(4) addr, Bit#(3) data);
232
endinterface
233
 
234
(*synthesize*)
235
module mkbSVector(IbSVector);
236
  RFile1#(Bit#(4),Bit#(3)) bsVector <- mkRFile1Full();
237
  method sub = bsVector.sub;
238
  method upd = bsVector.upd;
239
endmodule
240 8 jamey.hick
 
241
 
242
 
243 11 jamey.hick
 
244
 
245 2 jamey.hick
(* synthesize *)
246
module mkDeblockFilter( IDeblockFilter );
247
 
248
   FIFO#(EntropyDecOT) infifo     <- mkSizedFIFO(deblockFilter_infifo_size);
249
   FIFO#(DeblockFilterOT) outfifo <- mkFIFO();
250
 
251
   FIFO#(MemReq#(TAdd#(PicWidthSz,5),32)) dataMemReqQ       <- mkFIFO;
252
   FIFO#(MemReq#(PicWidthSz,13))          parameterMemReqQ  <- mkFIFO;
253
   FIFO#(MemResp#(32))                    dataMemRespQ      <- mkFIFO;
254
   FIFO#(MemResp#(13))                    parameterMemRespQ <- mkFIFO;
255
 
256
   Reg#(Process) process       <- mkReg(Passing);
257 8 jamey.hick
   Reg#(VerticalState) verticalState <- mkReg(NormalOperation);
258 2 jamey.hick
   Reg#(Bit#(1)) chromaFlag    <- mkReg(0);
259
   Reg#(Bit#(5)) dataReqCount  <- mkReg(0);
260
   Reg#(Bit#(5)) dataRespCount <- mkReg(0);
261
   Reg#(Bit#(4)) blockNum      <- mkReg(0);
262 8 jamey.hick
   Reg#(Bit#(2)) pixelNum      <- mkReg(0);
263 2 jamey.hick
 
264
   Reg#(Bool) filterTopMbEdgeFlag     <- mkReg(False);
265
   Reg#(Bool) filterLeftMbEdgeFlag    <- mkReg(False);
266
   Reg#(Bool) filterInternalEdgesFlag <- mkReg(False);
267
 
268
   Reg#(Bit#(PicWidthSz))  picWidth  <- mkReg(maxPicWidthInMB);
269
   Reg#(Bit#(PicHeightSz)) picHeight <- mkReg(0);
270
   Reg#(Bit#(PicAreaSz))   firstMb   <- mkReg(0);
271
   Reg#(Bit#(PicAreaSz))   currMb    <- mkReg(0);
272
   Reg#(Bit#(PicAreaSz))   currMbHor <- mkReg(0);//horizontal position of currMb
273
   Reg#(Bit#(PicHeightSz)) currMbVer <- mkReg(0);//vertical position of currMb
274
 
275
   Reg#(Bit#(2)) disable_deblocking_filter_idc <- mkReg(0);
276
   Reg#(Bit#(5)) slice_alpha_c0_offset <- mkReg(0);
277
   Reg#(Bit#(5)) slice_beta_offset <- mkReg(0);
278
 
279
   Reg#(Bit#(6)) curr_qpy   <- mkReg(0);
280
   Reg#(Bit#(6)) left_qpy   <- mkReg(0);
281
   Reg#(Bit#(6)) top_qpy    <- mkReg(0);
282
   Reg#(Bit#(6)) curr_qpc   <- mkReg(0);
283
   Reg#(Bit#(6)) left_qpc   <- mkReg(0);
284
   Reg#(Bit#(6)) top_qpc    <- mkReg(0);
285
   Reg#(Bit#(1)) curr_intra <- mkReg(0);
286
   Reg#(Bit#(1)) left_intra <- mkReg(0);
287
   Reg#(Bit#(1)) top_intra  <- mkReg(0);
288
 
289 8 jamey.hick
   Reg#(Bit#(2)) blockHorVerticalCleanup <- mkReg(0);
290
 
291 2 jamey.hick
   Reg#(Bit#(8)) alphaInternal  <- mkReg(0);
292
   Reg#(Bit#(5)) betaInternal   <- mkReg(0);
293 8 jamey.hick
   Reg#(Vector#(3,Bit#(5))) tc0Internal <- mkRegU();
294 2 jamey.hick
 
295
   Bit#(8) alpha_table[52] = {0,  0,  0,  0,  0,  0,  0,  0,  0,  0,
296
                              0,  0,  0,  0,  0,  0,  4,  4,  5,  6,
297
                              7,  8,  9, 10, 12, 13, 15, 17, 20, 22,
298
                             25, 28, 32, 36, 40, 45, 50, 56, 63, 71,
299
                             80, 90,101,113,127,144,162,182,203,226,
300
                            255,255};
301
   Bit#(5) beta_table[52] = {0,  0,  0,  0,  0,  0,  0,  0,  0,  0,
302
                             0,  0,  0,  0,  0,  0,  2,  2,  2,  3,
303
                             3,  3,  3,  4,  4,  4,  6,  6,  7,  7,
304
                             8,  8,  9,  9, 10, 10, 11, 11, 12, 12,
305
                            13, 13, 14, 14, 15, 15, 16, 16, 17, 17,
306
                            18, 18};
307
   Bit#(5) tc0_table[52][3] = {{ 0, 0, 0 }, { 0, 0, 0 }, { 0, 0, 0 }, { 0, 0, 0 }, { 0, 0, 0 }, { 0, 0, 0 },
308
                               { 0, 0, 0 }, { 0, 0, 0 }, { 0, 0, 0 }, { 0, 0, 0 }, { 0, 0, 0 }, { 0, 0, 0 },
309
                               { 0, 0, 0 }, { 0, 0, 0 }, { 0, 0, 0 }, { 0, 0, 0 }, { 0, 0, 0 }, { 0, 0, 1 },
310
                               { 0, 0, 1 }, { 0, 0, 1 }, { 0, 0, 1 }, { 0, 1, 1 }, { 0, 1, 1 }, { 1, 1, 1 },
311
                               { 1, 1, 1 }, { 1, 1, 1 }, { 1, 1, 1 }, { 1, 1, 2 }, { 1, 1, 2 }, { 1, 1, 2 },
312
                               { 1, 1, 2 }, { 1, 2, 3 }, { 1, 2, 3 }, { 2, 2, 3 }, { 2, 2, 4 }, { 2, 3, 4 },
313
                               { 2, 3, 4 }, { 3, 3, 5 }, { 3, 4, 6 }, { 3, 4, 6 }, { 4, 5, 7 }, { 4, 5, 8 },
314
                               { 4, 6, 9 }, { 5, 7,10 }, { 6, 8,11 }, { 6, 8,13 }, { 7,10,14 }, { 8,11,16 },
315
                               { 9,12,18 }, {10,13,20 }, {11,15,23 }, {13,17,25 }};
316
 
317 8 jamey.hick
   IWorkVectorHor workVectorRows <- mkWorkVectorHor();
318
   IWorkVectorVer workVectorCols <- mkWorkVectorVer();
319 6 jamey.hick
   ILeftVector leftVector <- mkLeftVector();
320 8 jamey.hick
   ITopVector  topVector  <- mkTopVector();
321
   Reg#(Bit#(16)) topVectorValidBits <- mkReg(0);
322 2 jamey.hick
 
323 11 jamey.hick
   IbSVector bSfileHor <- mkbSVector();
324
   IbSVector bSfileVer <- mkbSVector();
325 2 jamey.hick
 
326 6 jamey.hick
   Reg#(Bit#(6)) cleanup_state <- mkReg(0);
327 2 jamey.hick
 
328 8 jamey.hick
   Vector#(4, FIFO#(Bit#(32))) rowToColumnStore <- replicateM(mkFIFO);
329
   Reg#(Bit#(2)) rowToColumnState <- mkReg(0);
330 10 jamey.hick
   FIFO#(Tuple2#(Bit#(4),Bit#(1))) rowToColumnStoreBlock <- mkFIFO(); // The third bit 1 is to rotate the damned
331 8 jamey.hick
                                                                              // last left vector block
332 10 jamey.hick
   FIFO#(Tuple2#(Bit#(4), Bit#(32))) verticalFilterBlock <- mkFIFO();
333 6 jamey.hick
 
334 8 jamey.hick
   Reg#(Bit#(2)) columnState <- mkReg(0);
335
   Vector#(4, FIFO#(Bit#(32))) columnToRowStore <- replicateM(mkFIFO);
336
   Reg#(Bit#(2)) columnToRowState <- mkReg(0);
337 10 jamey.hick
   FIFO#(Tuple2#(Bit#(4), Bit#(1))) columnToRowStoreBlock <- mkFIFO();
338 8 jamey.hick
 
339
   Reg#(Bit#(2)) columnNumber <- mkReg(0);
340
 
341
 
342 2 jamey.hick
   //-----------------------------------------------------------
343
   // Rules
344
 
345
 
346
   rule checkFIFO ( True );
347
      $display( "Trace DeblockFilter: checkFIFO %h", infifo.first() );
348 8 jamey.hick
      $display( "TRACE DeblockFilter: checkFIFO %h", infifo.first() );
349 2 jamey.hick
   endrule
350
 
351
 
352
   rule passing ( process matches Passing );
353
      case (infifo.first()) matches
354
         tagged NewUnit . xdata :
355
            begin
356
               infifo.deq();
357 13 jamey.hick
               outfifo.enq(EDOT (infifo.first()));
358 2 jamey.hick
               $display("ccl5newunit");
359
               $display("ccl5rbspbyte %h", xdata);
360
            end
361
         tagged SPSpic_width_in_mbs .xdata :
362
            begin
363
               infifo.deq();
364 13 jamey.hick
               outfifo.enq(EDOT (infifo.first()));
365 2 jamey.hick
               picWidth <= xdata;
366
            end
367
         tagged SPSpic_height_in_map_units .xdata :
368
            begin
369
               infifo.deq();
370 13 jamey.hick
               outfifo.enq(EDOT (infifo.first()));
371 2 jamey.hick
               picHeight <= xdata;
372
            end
373
         tagged PPSdeblocking_filter_control_present_flag .xdata :
374
            begin
375
               infifo.deq();
376
               if (xdata == 0)
377
                  begin
378
                     disable_deblocking_filter_idc <= 0;
379
                     slice_alpha_c0_offset <= 0;
380
                     slice_beta_offset <= 0;
381
                  end
382
            end
383
         tagged SHfirst_mb_in_slice .xdata :
384
            begin
385
               infifo.deq();
386 13 jamey.hick
               outfifo.enq(EDOT (infifo.first()));
387 2 jamey.hick
               firstMb   <= xdata;
388
               currMb    <= xdata;
389
               currMbHor <= xdata;
390
               currMbVer <= 0;
391
            end
392
         tagged SHdisable_deblocking_filter_idc .xdata :
393
            begin
394
               infifo.deq();
395
               disable_deblocking_filter_idc <= xdata;
396
            end
397
         tagged SHslice_alpha_c0_offset .xdata :
398
            begin
399
               infifo.deq();
400
               slice_alpha_c0_offset <= xdata;
401
            end
402
         tagged SHslice_beta_offset .xdata :
403
            begin
404
               infifo.deq();
405
               slice_beta_offset <= xdata;
406
            end
407
         tagged IBTmb_qp .xdata :
408
            begin
409
               infifo.deq();
410
               curr_qpy <= xdata.qpy;
411
               curr_qpc <= xdata.qpc;
412
            end
413
         tagged PBbS .xdata :
414
            begin
415
               process <= Initialize;
416
            end
417
         tagged PBoutput .xdata :
418
            begin
419
               $display( "ERROR Deblocking Filter: passing PBoutput");
420
            end
421
         tagged EndOfFile :
422
            begin
423
               infifo.deq();
424 13 jamey.hick
               outfifo.enq(EDOT (infifo.first()));
425 2 jamey.hick
               $display( "ccl5: EndOfFile reached");
426
               //$finish(0);
427
            end
428
         default:
429
            begin
430
               infifo.deq();
431 13 jamey.hick
               outfifo.enq(EDOT (infifo.first()));
432 2 jamey.hick
            end
433
      endcase
434
   endrule
435
 
436 8 jamey.hick
   // What does this rule do?
437 2 jamey.hick
   rule currMbHorUpdate( !(currMbHor
438 8 jamey.hick
      $display( "TRACE Deblocking Filter: strange update rule firing... %0d", currMb);
439 2 jamey.hick
      Bit#(PicAreaSz) temp = zeroExtend(picWidth);
440
      if((currMbHor >> 3) >= temp)
441
         begin
442
            currMbHor <= currMbHor - (temp << 3);
443
            currMbVer <= currMbVer + 8;
444
         end
445
      else
446
         begin
447
            currMbHor <= currMbHor - temp;
448
            currMbVer <= currMbVer + 1;
449
         end
450
   endrule
451
 
452
 
453
   rule initialize ( process==Initialize && currMbHor
454 8 jamey.hick
      $display( "TRACE Deblocking Filter: initialize %0d", currMb);
455 2 jamey.hick
      process <= Horizontal;
456
      dataReqCount <= 1;
457
      dataRespCount <= 1;
458
      filterTopMbEdgeFlag <= !(currMb
459
      filterLeftMbEdgeFlag <= !(currMbHor==0 || disable_deblocking_filter_idc==1 || (disable_deblocking_filter_idc==2 && currMb==firstMb));
460
      filterInternalEdgesFlag <= !(disable_deblocking_filter_idc==1);
461
      blockNum <= 0;
462
      pixelNum <= 0;
463 8 jamey.hick
      topVectorValidBits <= 0;
464 2 jamey.hick
   endrule
465
 
466
 
467
   rule dataSendReq ( dataReqCount>0 && currMbHor
468 8 jamey.hick
      $display( "TRACE Deblocking Filter: dataSendReq %0d", dataReqCount);
469 2 jamey.hick
      Bit#(PicWidthSz) temp = truncate(currMbHor);
470
      if(currMb
471
         dataReqCount <= 0;
472
      else
473
         begin
474
            if(dataReqCount==1)
475 13 jamey.hick
               parameterMemReqQ.enq(LoadReq (temp));
476 2 jamey.hick
            Bit#(4) temp2 = truncate(dataReqCount-1);
477
            let temp3 = {temp,chromaFlag,temp2};
478 13 jamey.hick
            dataMemReqQ.enq(LoadReq (temp3));
479 2 jamey.hick
            if(dataReqCount==16)
480
               dataReqCount <= 0;
481
            else
482
               dataReqCount <= dataReqCount+1;
483
         end
484
   endrule
485
 
486
 
487
   rule dataReceiveNoResp ( dataRespCount>0 && currMb
488 8 jamey.hick
      $display( "TRACE Deblocking Filter: dataReceiveNoResp");
489 2 jamey.hick
      dataRespCount <= 0;
490
   endrule
491
 
492 8 jamey.hick
   function Action deque(FIFO#(Bit#(32)) fifo);
493
     return fifo.deq();
494
   endfunction
495
 
496
   // rotate column to row major after applying the horizontal filter
497
   rule rowToColumnConversion;
498
     // Check to see if we're even filtering the top edge
499
     Bit#(2) blockVer = {tpl_1(rowToColumnStoreBlock.first())[3],tpl_1(rowToColumnStoreBlock.first())[1]};
500
     Bit#(2) blockHor = {tpl_1(rowToColumnStoreBlock.first())[2],tpl_1(rowToColumnStoreBlock.first())[0]};
501 10 jamey.hick
     Bool storeBottomRightBlock = tpl_2(rowToColumnStoreBlock.first()) == 1;
502 9 jamey.hick
 
503 11 jamey.hick
     rowToColumnState  <= rowToColumnState + 1;
504
     Bit#(32) data_out = 0;
505
     Bit#(PicWidthSz) adjustedMbHor = ((currMbHor==0) ? (picWidth-1) : truncate(currMbHor-1));
506
 
507
     case(rowToColumnState)
508
       2'b00: data_out = {(rowToColumnStore[3].first())[7:0], (rowToColumnStore[2].first())[7:0],
509
                          (rowToColumnStore[1].first())[7:0], (rowToColumnStore[0].first())[7:0]};
510
 
511
       2'b01: data_out = {(rowToColumnStore[3].first())[15:8], (rowToColumnStore[2].first())[15:8],
512
                          (rowToColumnStore[1].first())[15:8], (rowToColumnStore[0].first())[15:8]};
513
 
514
       2'b10: data_out = {(rowToColumnStore[3].first())[23:16], (rowToColumnStore[2].first())[23:16],
515
                          (rowToColumnStore[1].first())[23:16], (rowToColumnStore[0].first())[23:16]};
516
 
517
       2'b11: begin
518
                data_out = {(rowToColumnStore[3].first())[31:24], (rowToColumnStore[2].first())[31:24],
519
                            (rowToColumnStore[1].first())[31:24], (rowToColumnStore[0].first())[31:24]};
520
                mapM_(deque, rowToColumnStore); // Deq the vector elements
521
                rowToColumnStoreBlock.deq();
522
             end
523
       endcase
524
 
525 8 jamey.hick
     if(storeBottomRightBlock) // The right bottom block is not complete until the top filtering has occured
526
                               // It has to be rotated to the column major ordering used in the top vector
527
                               // memory
528
       begin
529 9 jamey.hick
          $display( "TRACE Deblocking Filter: rowToColumnRotate rotating block (%0d, %0d) rowtoColumnState: %d bottomRightBlock: %d, data: %h", blockHor, blockVer, rowToColumnState, storeBottomRightBlock, data_out);
530 8 jamey.hick
         // The block hor calculation may be questionable... between U and V.
531
         if(chromaFlag == 0)
532
           begin
533
             dataMemReqQ.enq(StoreReq {addr:{adjustedMbHor,chromaFlag,2'b11,rowToColumnState},data:data_out});
534
           end
535
         else
536
           begin  //differentiate between u and v
537
             dataMemReqQ.enq(StoreReq {addr:{adjustedMbHor,chromaFlag,blockHor[1],1'b1,rowToColumnState},data:data_out});
538
           end
539
 
540
       end
541
     else // pass data along to vertical filter
542 11 jamey.hick
       begin
543
         verticalFilterBlock.enq(tuple2(tpl_1(rowToColumnStoreBlock.first()),data_out));
544 9 jamey.hick
 
545 11 jamey.hick
         $display( "TRACE Deblocking Filter: rowToColumnRotate rotating block (%0d, %0d) rowtoColumnState: %d bottomRightBlock: %d, data: %h", blockHor, blockVer, rowToColumnState, storeBottomRightBlock, data_out);
546 8 jamey.hick
       end
547
   endrule
548
 
549
   // rotate row to column after applying the vertical filter
550
   rule columnToRowConversion;
551
     Bit#(32) data_out = 0;
552 10 jamey.hick
     Bool topValues = tpl_2(columnToRowStoreBlock.first()) == 1;
553 8 jamey.hick
     Bit#(4) blockNumCols = tpl_1(columnToRowStoreBlock.first());
554
     Bit#(2) blockHor = {blockNumCols[2],blockNumCols[0]};
555
     Bit#(2) blockVer = {blockNumCols[3],blockNumCols[1]} - 1; // Subtract 1, because these output values lag slightly
556
     columnToRowState  <= columnToRowState + 1;
557
 
558
     case(columnToRowState)  // not to sure about this ordering
559
       2'b00: data_out = {(columnToRowStore[0].first())[7:0],
560
                          (columnToRowStore[1].first())[7:0],
561
                          (columnToRowStore[2].first())[7:0],
562
                          (columnToRowStore[3].first())[7:0]};
563
 
564
       2'b01: data_out = {(columnToRowStore[0].first())[15:8],
565
                          (columnToRowStore[1].first())[15:8],
566
                          (columnToRowStore[2].first())[15:8],
567
                          (columnToRowStore[3].first())[15:8]};
568
       2'b10: data_out = {(columnToRowStore[0].first())[23:16],
569
                          (columnToRowStore[1].first())[23:16],
570
                          (columnToRowStore[2].first())[23:16],
571
                          (columnToRowStore[3].first())[23:16]};
572
       2'b11: begin
573
                data_out = {(columnToRowStore[0].first())[31:24],
574
                            (columnToRowStore[1].first())[31:24],
575
                            (columnToRowStore[2].first())[31:24],
576
                            (columnToRowStore[3].first())[31:24]};
577
                mapM_(deque, columnToRowStore); // Deq the vector elements
578
                columnToRowStoreBlock.deq();
579
              end
580
     endcase
581 9 jamey.hick
     $write( "TRACE Deblocking Filter: columnToRow rotate block(%0d, %0d) columnToRowState %d, topValues: %d, data: %h", blockHor, blockVer, columnToRowState, topValues, data_out);
582 8 jamey.hick
 
583
     Bit#(PicWidthSz) currMbHorT = truncate(currMbHor);
584
     // Actually send the data out. This stuff is not the bottom row or left column, and is therefore done.
585
     // THe bottom row was sent out to the temporary buffer in the vertical rule.  But if we're on the last row of
586
     // the frame, there coming here.  Also, if we're in the last block, we must output the leftvector values
587
     if( !topValues && (!(blockHor==3 || (blockHor[0]==1 && chromaFlag==1)) || (currMbVer==picHeight-1)))
588
       begin
589
         $display( " Normal");
590
         if(chromaFlag==0)
591
           begin
592
             $display("TRACE mkDeblockFilter: Outputting Luma ver{mbVer, blockVer(2), state}: %h, hor{mbHor, blockHor(2)}: %b, data: %h", {currMbVer,blockVer}, {currMbHorT,blockHor}, data_out);
593
             outfifo.enq(DFBLuma {ver:{currMbVer,blockVer,columnToRowState},
594
                                  hor:{currMbHorT,blockHor},
595
                                  data:data_out});
596
           end
597
         else
598
           begin
599
 $display("TRACE mkDeblockFilter: Outputting Chroma %d ver{mbVer, blockVer(1), state(2)}: %b, hor{mbHor, blockHor(1)}: %b, data: %h",blockHor[1],{currMbVer,blockVer[0],columnToRowState},{currMbHorT,blockHor[0]},data_out);
600
             outfifo.enq(DFBChroma {uv:blockHor[1],
601
                                    ver:{currMbVer,blockVer[0],columnToRowState},
602
                                    hor:{currMbHorT,blockHor[0]},
603
                                    data:data_out});
604
           end
605
       end
606
 
607
     if(topValues)// These are the previous top values, and they must be sent out.  Note, that since this is a past
608
                       // Mb, we must adjust the the Mbs used.
609
       begin
610
         $display( " TopValues");
611
         if(chromaFlag==0)
612
           begin
613
             $display("TRACE mkDeblockFilter: (Top Value) Outputting Luma ver{mbVer, blockVer(2), state(2)}: %b, hor{mbHor, blockHor(2)}: %h, data: %h",{currMbVer-1,2'b11,columnToRowState}, {currMbHorT,blockHor}, data_out);
614
             outfifo.enq(DFBLuma {ver:{currMbVer-1,2'b11,columnToRowState},
615
                                  hor:{currMbHorT,blockHor},
616
                                  data:data_out});
617
           end
618
         else
619
           begin
620
             $display("TRACE mkDeblockFilter: (Top Value) Outputting Chroma %d ver{mbVer, blockVer(1), state(2)}: %b, hor{mbHor, blockHor(1)}: %b, data: %h",blockHor[1],{currMbVer-1,1'b1,columnToRowState},{currMbHorT,blockHor[0]},data_out);
621
             outfifo.enq(DFBChroma {uv:blockHor[1],
622
                                    ver:{currMbVer-1,1'b1,columnToRowState},
623
                                    hor:{currMbHorT,blockHor[0]},
624
                                    data:data_out});
625
           end
626
       end
627
 
628
     if( !topValues && (blockHor==3 || (blockHor[0]==1 && chromaFlag==1))) // We need to write to the left Vector which will be used in the future. These values will not be written out.
629
          // It may be wise at some point to
630
       begin
631
         // We need to check for the last point in the pipeline. This is the bottom right corner of the Mb.
632
         $display( " Left Vector");
633
         if(chromaFlag==0)
634
           begin
635
             if((blockVer == 3) && (columnToRowState == 3))
636
               begin
637
                 process <= Cleanup;
638
               end
639
             //check for last macro block
640 9 jamey.hick
             leftVector.upd({1'b0,blockVer,columnToRowState}, {data_out[7:0], data_out[15:8],data_out[23:16],data_out[31:24]});
641 8 jamey.hick
           end
642
         else
643
           begin
644
             // Only cleanup a single time after the chroma blocks
645
             if((blockHor == 3) && (blockVer[0] == 1) && (columnToRowState == 3))
646
               begin
647
                 process <= Cleanup;
648
               end
649 9 jamey.hick
             leftVector.upd({1'b1,blockHor[1],blockVer[0],columnToRowState}, {data_out[7:0], data_out[15:8],data_out[23:16],data_out[31:24]});
650 8 jamey.hick
           end
651
         end
652
 
653
   endrule
654
 
655 2 jamey.hick
 
656
   rule dataReceiveResp ( dataRespCount>0 && !(currMb
657 8 jamey.hick
      $display( "TRACE Deblocking Filter: dataReceiveResp %0d", dataRespCount);
658 2 jamey.hick
      Bit#(4) temp = truncate(dataRespCount-1);
659
      if(dataRespCount==1)
660
         begin
661
            Bit#(13) tempParameters=0;
662
            if(parameterMemRespQ.first() matches tagged LoadResp .xdata)
663
               tempParameters = xdata;
664
            top_qpy <= tempParameters[5:0];
665
            top_qpc <= tempParameters[11:6];
666
            top_intra <= tempParameters[12];
667
            parameterMemRespQ.deq();
668
         end
669
      if(dataRespCount==16)
670
         dataRespCount <= 0;
671
      else
672
         dataRespCount <= dataRespCount+1;
673
      if(dataMemRespQ.first() matches tagged LoadResp .xdata)
674 8 jamey.hick
        begin
675
          topVectorValidBits[temp] <= 1;
676
          topVector.upd(temp, xdata);
677
        end
678 2 jamey.hick
      dataMemRespQ.deq();
679
      //$display( "TRACE Deblocking Filter: dataReceiveResp topVector %h %h %h %h %h %h %h %h %h %h %h %h %h %h %h %h", topVector[0], topVector[1], topVector[2], topVector[3], topVector[4], topVector[5], topVector[6], topVector[7], topVector[8], topVector[9], topVector[10], topVector[11], topVector[12], topVector[13], topVector[14], topVector[15]);
680
   endrule
681
 
682
 
683
   rule horizontal ( process==Horizontal && currMbHor
684
      Bit#(2) blockHor = {blockNum[2],blockNum[0]};
685
      Bit#(2) blockVer = {blockNum[3],blockNum[1]};
686 8 jamey.hick
      Bit#(2) pixelVer = pixelNum;
687
 
688 2 jamey.hick
      Bool leftEdge = (blockNum[0]==0 && (blockNum[2]==0 || chromaFlag==1));
689
      if(blockNum==0 && pixelNum==0)
690
         begin
691
            Bit#(6) qpav = (chromaFlag==0 ? curr_qpy : curr_qpc);
692
            Bit#(8) indexAtemp = zeroExtend(qpav)+signExtend(slice_alpha_c0_offset);
693
            Bit#(8) indexBtemp = zeroExtend(qpav)+signExtend(slice_beta_offset);
694
            Bit#(6) indexA = (indexAtemp[7]==1 ? 0 : (indexAtemp[6:0]>51 ? 51 : indexAtemp[5:0]));
695
            Bit#(6) indexB = (indexBtemp[7]==1 ? 0 : (indexBtemp[6:0]>51 ? 51 : indexBtemp[5:0]));
696
            alphaInternal <= alpha_table[indexA];
697
            betaInternal <= beta_table[indexB];
698
            Vector#(3,Bit#(5)) tc0temp = arrayToVector(tc0_table[indexA]);
699
            tc0Internal <= tc0temp;
700
         end
701
      case (infifo.first()) matches
702
         tagged PBbS .xdata :
703
            begin
704 11 jamey.hick
               infifo.deq();
705
               bSfileHor.upd(blockNum, xdata.bShor);
706
               bSfileVer.upd(blockNum, xdata.bSver);
707 10 jamey.hick
               $display( "TRACE Deblocking Filter: horizontal bsFIFO data: %d, subblock(%0d, %0d) row: %0d, ",infifo.first(), blockHor, blockVer, pixelNum);
708 2 jamey.hick
            end
709
         tagged PBoutput .xdata :
710
            begin
711 8 jamey.hick
               $display( "TRACE Deblocking Filter: horizontal subblock(%0d, %0d) row: %0d, data: %h", blockHor, blockVer, pixelNum, xdata);
712 2 jamey.hick
               infifo.deq();
713
               Bit#(6) addrq = {blockHor,blockVer,pixelVer};
714 8 jamey.hick
               Bit#(5) addrpLeft = (chromaFlag==0 ? {1'b0,blockVer,pixelVer} : {1'b1,blockHor[1],blockVer[0],pixelVer});
715 2 jamey.hick
               Bit#(6) addrpCurr = {(blockHor-1),blockVer,pixelVer};
716
               Bit#(32) pixelq = {xdata[3],xdata[2],xdata[1],xdata[0]};
717
               Bit#(32) pixelp;
718
               if(leftEdge)
719 8 jamey.hick
                 begin
720
                   pixelp <- leftVector.sub(addrpLeft);
721
                   $display( "TRACE Deblocking Filter: horizontal P (left) addr %h, data %h ",addrpLeft, pixelp);
722
                 end
723 2 jamey.hick
               else
724 8 jamey.hick
                 begin
725
                   pixelp <- workVectorRows.sub({blockVer[0], pixelVer});
726
                   $display( "TRACE Deblocking Filter: horizontal P (work) addr %h, data %h ",addrpCurr, pixelp);
727
                 end
728 2 jamey.hick
               Bit#(64) result = {pixelq,pixelp};
729
               if(leftEdge && filterLeftMbEdgeFlag)
730
                  begin
731 8 jamey.hick
                    Bit#(6) curr_qp = (chromaFlag==0 ? curr_qpy : curr_qpc);
732
                    Bit#(6) left_qp = (chromaFlag==0 ? left_qpy : left_qpc);
733
                    Bit#(7) qpavtemp = zeroExtend(curr_qp)+zeroExtend(left_qp)+1;
734
                    Bit#(6) qpav = qpavtemp[6:1];
735
                    Bit#(8) indexAtemp = zeroExtend(qpav)+signExtend(slice_alpha_c0_offset);
736
                    Bit#(8) indexBtemp = zeroExtend(qpav)+signExtend(slice_beta_offset);
737
                    Bit#(6) indexA = (indexAtemp[7]==1 ? 0 : (indexAtemp[6:0]>51 ? 51 : indexAtemp[5:0]));
738
                    Bit#(6) indexB = (indexBtemp[7]==1 ? 0 : (indexBtemp[6:0]>51 ? 51 : indexBtemp[5:0]));
739
                    Bit#(8) alphaMbLeft = alpha_table[indexA];
740
                    Bit#(5) betaMbLeft = beta_table[indexB];
741
                    Vector#(3,Bit#(5)) tc0MbLeft = arrayToVector(tc0_table[indexA]);
742
                    if(filter_test({pixelq[15:0],pixelp[31:16]},alphaMbLeft,betaMbLeft))
743
                      begin
744 11 jamey.hick
                         Bit#(3) bsData <- bSfileHor.sub((chromaFlag==0?blockNum:{blockNum[1:0],pixelVer[1],1'b0}));
745
                         result = filter_input({pixelq,pixelp},chromaFlag==1,bsData,alphaMbLeft,betaMbLeft,tc0MbLeft);
746 8 jamey.hick
                       end
747 2 jamey.hick
                  end
748
               else if(!leftEdge && filterInternalEdgesFlag)
749
                  begin
750
                     if(filter_test({pixelq[15:0],pixelp[31:16]},alphaInternal,betaInternal))
751 8 jamey.hick
                       begin
752 11 jamey.hick
                         Bit#(3) bSData <- bSfileHor.sub((chromaFlag==0?blockNum:{blockNum[1:0],pixelVer[1],1'b0}));
753
                         result = filter_input({pixelq,pixelp},chromaFlag==1,bSData,alphaInternal,betaInternal,tc0Internal);
754 8 jamey.hick
                       end
755 2 jamey.hick
                  end
756 9 jamey.hick
               Bit#(PicWidthSz) currMbHorT = truncate(currMbHor);
757 8 jamey.hick
 
758 2 jamey.hick
               if(leftEdge)
759 8 jamey.hick
                 begin
760
                   // write out the left edge
761
                   //Check to store this value to the memory.  I think the rotation is off here.
762
                   // I should also adjust the vertical Mb...  Figure out MbHorT
763
                   Bit#(PicWidthSz) currMbHorT = truncate(currMbHor);
764 9 jamey.hick
                   Bit#(PicHeightSz) adjustedMbVer = ((currMbHorT==0) && (currMbVer!=0)) ? currMbVer-1 : currMbVer;
765 8 jamey.hick
                   Bit#(PicWidthSz)  adjustedMbHor = currMbHorT==0 ? picWidth-1 : currMbHorT-1;
766
                   // In this case we buffer the bottom vertical element, since it has to be used again
767 10 jamey.hick
                   if(((blockVer == 3) || ((chromaFlag == 1) && (blockVer == 1))) && (adjustedMbVer != picHeight - 1))
768 8 jamey.hick
                     begin
769
                       rowToColumnStore[pixelNum[1:0]].enq(result[31:0]);
770
                       // only push in a command for the bottom leftblock.  It has to be rotated.
771
                       if(pixelNum == 0)
772
                         begin
773 10 jamey.hick
                           rowToColumnStoreBlock.enq(tuple2(blockNum,1));
774 8 jamey.hick
                         end
775
                    end
776
                   // these outputs occur in the past, so we must use the adjusted Mb numbers
777
                   else if(chromaFlag==0)
778
                     begin
779
                       $display("TRACE mkDeblockFilter: (Left Vector) Outputting Luma ver{mbVer, blockVer(2), state(2)}: %b, hor{mbHor, blockHor(2)}: %b, data: %h",{adjustedMbVer,blockVer,pixelNum},{adjustedMbHor,2'b11} ,result[31:0] );
780
                       outfifo.enq(DFBLuma {ver:{adjustedMbVer,blockVer,pixelVer},
781
                                            hor:{adjustedMbHor,2'b11},
782 9 jamey.hick
                                            data:{result[7:0],result[15:8],result[23:16],result[31:24]}});
783 8 jamey.hick
                     end
784
                   else
785
                     begin
786
                       $display("TRACE mkDeblockFilter: (Left Vector) Outputting Chroma %d ver{mbVer, blockVer(2), state(2)}: %b, hor{mbHor, blockHor(2)}: %b, data: %h",blockHor[1],{adjustedMbVer,blockVer[0],pixelNum},{adjustedMbHor,1'b1}  ,result[31:0]);
787
                       outfifo.enq(DFBChroma {uv:blockHor[1],
788
                                              ver:{adjustedMbVer,blockVer[0],pixelVer},
789
                                              hor:{adjustedMbHor,1'b1},
790 9 jamey.hick
                                              data:{result[7:0],result[15:8],result[23:16],result[31:24]}});
791 8 jamey.hick
                      end
792
                 end
793 2 jamey.hick
               else
794 8 jamey.hick
                  begin
795
                    // push the correction into reorder block;
796
                    rowToColumnStore[addrpCurr[1:0]].enq(result[31:0]);
797
                    // Push down the block number and the chroma flag into the pipeline
798
                    if(pixelNum == 0)
799
                      begin
800
                        let blockHorPast = blockHor - 1;
801
                        let blockNumPast = {blockVer[1], blockHorPast[1], blockVer[0], blockHorPast[0]};
802 10 jamey.hick
                        rowToColumnStoreBlock.enq(tuple2(blockNumPast,0));
803 8 jamey.hick
                      end
804
                  end
805
               $display( "TRACE Deblocking Filter: horizontal Q (work) addr %h, data %h, original data: %h ",addrq, result[63:32], pixelq);
806
               workVectorRows.upd({blockVer[0],pixelVer}, result[63:32]);
807 6 jamey.hick
 
808 8 jamey.hick
               // Step out to clean up the edge block
809
               // What about the chroma?
810
               if((pixelNum == 3) && ((blockHor == 3) || ((chromaFlag == 1) && (blockHor == 1))))
811
                 begin
812
                    $display( "TRACE Deblocking Filter: Heading to Horizontal Cleanup");
813
                   process <= HorizontalCleanup;// we enter this state to push out the remaining
814
                                                  // blocks, that haven't been shoved out.  Namely, the
815
                                                  // left blocks.
816
                 end
817
               else if(pixelNum==3)
818
                 begin
819 10 jamey.hick
                   $display( "TRACE Deblocking Filter: horizontal bsFIFO completed subblock(%0d, %0d)", blockHor, blockVer);
820 8 jamey.hick
                   blockNum <= blockNum+1;
821
                 end
822
               pixelNum <= pixelNum+1;
823 2 jamey.hick
            end
824
         default: $display( "ERROR Deblocking Filter: horizontal non-PBoutput input");
825
      endcase
826
   endrule
827
 
828 8 jamey.hick
  rule horizontal_cleanup(process == HorizontalCleanup);
829
    Bit#(2) blockHor = {blockNum[2],blockNum[0]};
830
    Bit#(2) blockVer = {blockNum[3],blockNum[1]};
831
    $display( "TRACE Deblocking Filter: horizontal_cleanup (%0d, %0d) row: %d", blockHor, blockVer, pixelNum);
832
    if(pixelNum==3 && (blockNum==15 || (blockNum==7 && chromaFlag==1)))
833
      begin
834
        if(blockNum == 15)
835
          begin
836
            $display( "TRACE Deblocking Filter: horizontal completed Mb (%0d) Luma", currMb);
837
          end
838
        else
839
          begin
840
            $display( "TRACE Deblocking Filter: horizontal completed Mb (%0d) Chroma", currMb);
841
          end
842
        blockNum <= 0;
843
        process <= Vertical;// we enter this state to wait for the vertical processing to complete
844 10 jamey.hick
        rowToColumnStoreBlock.enq(tuple2(blockNum,0));
845 8 jamey.hick
      end
846
    else if(pixelNum == 3)
847
      begin
848
        blockNum <= blockNum + 1;
849
        process <= Horizontal; // not done with this Mb yet.
850 10 jamey.hick
        rowToColumnStoreBlock.enq(tuple2(blockNum,0));
851 8 jamey.hick
      end
852
    pixelNum <= pixelNum + 1;
853
    // push the correction into reorder block;
854
    Bit#(32) work_data <- workVectorRows.sub({blockVer[0], pixelNum});
855
    rowToColumnStore[pixelNum].enq(work_data);
856
  endrule
857 2 jamey.hick
 
858 8 jamey.hick
 
859
  // declare these to share the rule
860
  begin
861
   Bit#(4) blockNumCols = tpl_1(verticalFilterBlock.first());
862
   Bit#(2) blockVer = {blockNumCols[3],blockNumCols[1]};
863
   Bit#(2) blockHor = {blockNumCols[2],blockNumCols[0]};
864
   Bool topEdge = (blockVer==0);
865 10 jamey.hick
 
866 8 jamey.hick
 
867
  rule vertical_filter_halt((verticalState == NormalOperation) && !((!topEdge) || (topVectorValidBits[{blockHor,columnNumber}] == 1) || (!filterTopMbEdgeFlag)));
868
        if(process == Vertical || process == Horizontal)
869
          begin
870
            $display("TRACE Deblocking Filter: Vertical processing halted on block: %h (%0d, %0d), column %d chromaFlag %d due to data dependency",  blockNumCols, blockHor, blockVer, columnNumber, chromaFlag);
871
          end
872
 
873
  endrule
874
 
875
 
876
  // As with horizontal, the q data will be read from the data store, and the p data will be streamed in via the
877
  // reordering FIFO.  The new p data must be stored, but the q data will need to be spooled out, since it needs to
878
  // make it to the left vector.
879
  rule vertical((verticalState == NormalOperation) && ((!topEdge) || (topVectorValidBits[{blockHor,columnNumber}] == 1) || (!filterTopMbEdgeFlag)));
880
    //$display( "TRACE Deblocking Filter: vertical %0d %0d", colNum, rowNum);
881
    //$display( "TRACE Deblocking Filter: vertical topVector %h %h %h %h %h %h %h %h %h %h %h %h %h %h %h %h", topVector[0], topVector[1], topVector[2], topVector[3], topVector[4], topVector[5], topVector[6], topVector[7], topVector[8], topVector[9], topVector[10], topVector[11], topVector[12], topVector[13], topVector[14], topVector[15]);
882
    //Process the block according to what got passed to us.
883
    Bit#(32) workV = tpl_2(verticalFilterBlock.first());
884
    Bit#(32) tempV = 0;
885
    Bit#(64) resultV = 0;
886
    Bit#(8) alpha;
887
    Bit#(5) beta;
888
    Vector#(3,Bit#(5)) tc0;
889
 
890
    // Check to see if the topVector block has been loaded, if needed
891
//    if((!topEdge) || (topVectorValidBits[{blockHor,columnNumber}] == 1) || (!filterTopMbEdgeFlag))
892
//      begin
893
      $display( "TRACE Deblocking Filter: vertical subblock (%0d, %0d), column: %d, data: %h", blockHor, blockVer, columnNumber, workV);
894
      columnNumber <= columnNumber + 1;
895
      verticalFilterBlock.deq();
896 2 jamey.hick
      if(topEdge)
897
         begin
898 8 jamey.hick
            Bit#(6) curr_qp = (chromaFlag==0 ? curr_qpy : curr_qpc);
899
            Bit#(6) top_qp = (chromaFlag==0 ? top_qpy : top_qpc);
900
            Bit#(7) qpavtemp = zeroExtend(curr_qp)+zeroExtend(top_qp)+1;
901
            Bit#(6) qpav = qpavtemp[6:1];
902
            Bit#(8) indexAtemp = zeroExtend(qpav)+signExtend(slice_alpha_c0_offset);
903
            Bit#(8) indexBtemp = zeroExtend(qpav)+signExtend(slice_beta_offset);
904
            Bit#(6) indexA = (indexAtemp[7]==1 ? 0 : (indexAtemp[6:0]>51 ? 51 : indexAtemp[5:0]));
905
            Bit#(6) indexB = (indexBtemp[7]==1 ? 0 : (indexBtemp[6:0]>51 ? 51 : indexBtemp[5:0]));
906
            Bit#(8) alphaMbTop = alpha_table[indexA];
907
            Bit#(5) betaMbTop = beta_table[indexB];
908
            Vector#(3,Bit#(5)) tc0MbTop = arrayToVector(tc0_table[indexA]);
909
            tempV <- topVector.sub({blockHor,columnNumber});
910 9 jamey.hick
            $display( "TRACE Deblocking Filter: vertical P (top) addr %h, orig data %h ",{blockVer,columnNumber}, tempV);
911 8 jamey.hick
            alpha = alphaMbTop;
912
            beta = betaMbTop;
913
            tc0 = tc0MbTop;
914 2 jamey.hick
         end
915
      else
916 8 jamey.hick
         begin
917
            // We read this value from the original vector
918
            tempV <- workVectorCols.sub({blockHor, columnNumber});
919 9 jamey.hick
            $display( "TRACE Deblocking Filter: vertical P (work) addr %h, orig data %h ",{blockHor, blockVer - 1, columnNumber}, tempV);
920 2 jamey.hick
            alpha = alphaInternal;
921
            beta = betaInternal;
922
            tc0 = tc0Internal;
923
         end
924 8 jamey.hick
 
925
      // Marshalling data in things upon which the filter blocks can be applied
926
 
927
      resultV = {tpl_2(verticalFilterBlock.first()),tempV};
928
 
929
      // Apply filter, only if filter test passes, and we are either filtering the top edge, or we aren't on the top edge
930 9 jamey.hick
      $display( "TRACE Deblocking Filter: vertical Filter test: P1P0Q0Q1: %h",{workV[15:8],workV[7:0],tempV[31:24],tempV[23:16]});
931 8 jamey.hick
      if((filter_test({workV[15:8],workV[7:0],tempV[31:24],tempV[23:16]},alpha,beta)) && (!topEdge || filterTopMbEdgeFlag))
932
        begin
933 11 jamey.hick
        Bit#(3) bsData <- bSfileVer.sub((chromaFlag==0?blockNumCols:{blockVer[0],blockHor[0],1'b0,columnNumber[1]}));
934
         resultV = filter_input(resultV,chromaFlag==1,bsData,alpha,beta,tc0);
935 8 jamey.hick
        end
936
      //Write out the result data  31:0 are the done q values
937 2 jamey.hick
      if(topEdge)
938
         begin
939 8 jamey.hick
            // We really need to just output these values -> need to shove them to the rotation unit, but only if the
940
            // current Mb vertical component is larger than 0.  All of these are done and can be dumped out
941
            if(currMbVer > 0)
942
              begin
943
                if(columnNumber == 0)
944
                  begin
945 10 jamey.hick
                    columnToRowStoreBlock.enq(tuple2(blockNumCols,1'b1));
946 8 jamey.hick
                  end
947
                columnToRowStore[columnNumber].enq(resultV[31:0]);
948
              end
949 2 jamey.hick
         end
950
      else
951
         begin
952 8 jamey.hick
            // We should make the decision as to whether to store these values. We will store the bottom row, except
953
            // for the left most block which will be stored the next time that an Mb gets processed.
954
            // The values to store are in the P vector... except the bottom right block, which is different.
955
            Bit#(PicWidthSz) currMbHorT = truncate(currMbHor);
956
 
957
            if(((blockVer == 3) && (blockHor == 3)) || ((chromaFlag == 1) && (blockVer == 1) && (blockHor[0] == 1)))
958
              begin
959
                // need to enter escape state to write the bottom left block to the leftVector.
960
                if(columnNumber == 3)
961
                  begin
962
                    blockHorVerticalCleanup <= blockHor;
963
                    verticalState <= VerticalCleanup;
964
                  end
965
              end
966
            else if((blockVer == 3) || ((chromaFlag == 1) && (blockVer == 1)))
967
              begin
968
                if((currMbVer == picHeight - 1) && (columnNumber == 3)) // If we're at the bottom of the frame, we'd
969
                                                                        // roll through the block clean up.
970
                  begin
971
                    blockHorVerticalCleanup <= blockHor;
972
                    verticalState <= VerticalCleanup;
973
                  end
974
                dataMemReqQ.enq(StoreReq {addr:{currMbHorT,chromaFlag,blockHor,columnNumber},data:resultV[63:32]});
975
              end
976
            columnToRowStore[columnNumber].enq(resultV[31:0]);
977
            if(columnNumber == 0)
978
              begin
979 10 jamey.hick
                columnToRowStoreBlock.enq(tuple2(blockNumCols,1'b0));
980 8 jamey.hick
              end
981 2 jamey.hick
         end
982
 
983 9 jamey.hick
        $display( "TRACE Deblocking Filter: vertical P                 data %h                     ",  resultV[31:0]);
984 8 jamey.hick
        $display( "TRACE Deblocking Filter: vertical Q (work) addr %h, data %h, original data: %h  ",{blockHor,blockVer,columnNumber}, resultV[63:32], workV);
985 2 jamey.hick
 
986 8 jamey.hick
        workVectorCols.upd({blockHor,columnNumber}, resultV[63:32]);
987
  endrule
988
end
989 2 jamey.hick
 
990 8 jamey.hick
  rule vertical_cleanup(verticalState == VerticalCleanup);
991
    $display( "TRACE Deblocking Filter: vertical_cleanup at block end column: %d ", columnNumber);
992
    columnNumber <= columnNumber + 1;
993
    if(columnNumber == 3)
994
      begin
995
        verticalState <= NormalOperation;
996
      end
997
    if(chromaFlag == 0)
998
      begin
999
        Bit#(2) blockHor = blockHorVerticalCleanup;
1000
        Bit#(2) blockVer = 0;
1001
        if(columnNumber == 0)
1002
          begin
1003
            // Horizontal Postion is 3, but vertical position is 0, owing to subtraction in the rotation unit
1004 10 jamey.hick
            columnToRowStoreBlock.enq(tuple2({blockVer[1],blockHor[1],blockVer[0],blockHor[0]},1'b0));
1005 8 jamey.hick
          end
1006
        Bit#(32) w_data <- workVectorCols.sub({blockHor, columnNumber});
1007
        columnToRowStore[columnNumber].enq(w_data);
1008
     end
1009
   else
1010
     begin
1011
       Bit#(2) blockHor = blockHorVerticalCleanup;
1012 10 jamey.hick
       Bit#(2) blockVer = 2; // need to make this two for subtraction in rotation unit
1013 8 jamey.hick
       if(columnNumber == 0)
1014
         begin
1015
           // Horizontal Postion is 3, but vertical position is 0, owing to subtraction in the rotation unit
1016 10 jamey.hick
           columnToRowStoreBlock.enq(tuple2({blockVer[1],blockHor[1],blockVer[0],blockHor[0]},1'b0));
1017 8 jamey.hick
         end
1018
       Bit#(32) w_data <- workVectorCols.sub({blockHor, columnNumber});
1019
       columnToRowStore[columnNumber].enq(w_data);
1020
     end
1021
  endrule
1022 2 jamey.hick
 
1023
 
1024 8 jamey.hick
  rule cleanup ( process==Cleanup && currMbHor
1025
    $display( "TRACE Deblocking Filter: cleanup %0d", currMb);
1026
    Bit#(PicWidthSz) currMbHorT = truncate(currMbHor);
1027
    if(chromaFlag==0)
1028
      begin
1029
        chromaFlag <= 1;
1030
        process <= Initialize;
1031 10 jamey.hick
        $display( "TRACE Deblocking Filter: horizontal bsFIFO luma completed");
1032 8 jamey.hick
      end
1033
    else
1034
      begin
1035 10 jamey.hick
        $display( "TRACE Deblocking Filter: horizontal bsFIFO chroma completed");
1036 8 jamey.hick
        chromaFlag <= 0;
1037
        process <= Passing;
1038 9 jamey.hick
        Bit#(PicWidthSz) temp = truncate(currMbHor);
1039
        parameterMemReqQ.enq(StoreReq {addr:temp,data:{curr_intra,curr_qpc,curr_qpy}});
1040 8 jamey.hick
        left_intra <= curr_intra;
1041
        left_qpc <= curr_qpc;
1042
        left_qpy <= curr_qpy;
1043
        currMb <= currMb+1;
1044
        currMbHor <= currMbHor+1;
1045
        if(currMbVer==picHeight-1 && currMbHor==zeroExtend(picWidth-1))
1046
          begin
1047
            outfifo.enq(EndOfFrame);
1048 6 jamey.hick
          end
1049 8 jamey.hick
      end
1050 2 jamey.hick
   endrule
1051 8 jamey.hick
 
1052 2 jamey.hick
   interface Client mem_client_data;
1053
      interface Get request  = fifoToGet(dataMemReqQ);
1054
      interface Put response = fifoToPut(dataMemRespQ);
1055
   endinterface
1056
 
1057
   interface Client mem_client_parameter;
1058
      interface Get request  = fifoToGet(parameterMemReqQ);
1059
      interface Put response = fifoToPut(parameterMemRespQ);
1060
   endinterface
1061
 
1062
   interface Put ioin  = fifoToPut(infifo);
1063
   interface Get ioout = fifoToGet(outfifo);
1064
 
1065
endmodule
1066
 
1067
endpackage

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.