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 42

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

powered by: WebSVN 2.1.0

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