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 61

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 51 jamey.hick
//
5 2 jamey.hick
//
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 51 jamey.hick
  Initialize,
31 8 jamey.hick
  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 43 jamey.hick
   RegFile#(idx_t,d_t) rf <- mkRegFileWCF(lo,hi);
194 19 jamey.hick
   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 43 jamey.hick
   provisos (Bits#(idx_t, si),Bits#(d_t, sa),Bounded#(idx_t),Literal#(idx_t) );
206
   RegFile#(idx_t,d_t) rf <- mkRegFileWCF(0,fromInteger(valueof(TSub#(TExp#(si),1))));
207 19 jamey.hick
   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 43 jamey.hick
   FIFO#(DeblockFilterOT) outfifoVertical <- mkSizedFIFO(5);
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 60 jamey.hick
   Reg#(Bit#(1)) chromaFlagHor <- mkReg(0);
301
   Reg#(Bit#(1)) chromaFlagVer <- mkReg(0);
302 2 jamey.hick
   Reg#(Bit#(5)) dataReqCount  <- mkReg(0);
303
   Reg#(Bit#(5)) dataRespCount <- mkReg(0);
304
   Reg#(Bit#(4)) blockNum      <- mkReg(0);
305 8 jamey.hick
   Reg#(Bit#(2)) pixelNum      <- mkReg(0);
306 2 jamey.hick
 
307
   Reg#(Bool) filterTopMbEdgeFlag     <- mkReg(False);
308
   Reg#(Bool) filterLeftMbEdgeFlag    <- mkReg(False);
309
   Reg#(Bool) filterInternalEdgesFlag <- mkReg(False);
310
 
311
   Reg#(Bit#(PicWidthSz))  picWidth  <- mkReg(maxPicWidthInMB);
312
   Reg#(Bit#(PicHeightSz)) picHeight <- mkReg(0);
313
   Reg#(Bit#(PicAreaSz))   firstMb   <- mkReg(0);
314
   Reg#(Bit#(PicAreaSz))   currMb    <- mkReg(0);
315
   Reg#(Bit#(PicAreaSz))   currMbHor <- mkReg(0);//horizontal position of currMb
316
   Reg#(Bit#(PicHeightSz)) currMbVer <- mkReg(0);//vertical position of currMb
317
 
318
   Reg#(Bit#(2)) disable_deblocking_filter_idc <- mkReg(0);
319
   Reg#(Bit#(5)) slice_alpha_c0_offset <- mkReg(0);
320
   Reg#(Bit#(5)) slice_beta_offset <- mkReg(0);
321
 
322
   Reg#(Bit#(6)) curr_qpy   <- mkReg(0);
323
   Reg#(Bit#(6)) left_qpy   <- mkReg(0);
324
   Reg#(Bit#(6)) top_qpy    <- mkReg(0);
325
   Reg#(Bit#(6)) curr_qpc   <- mkReg(0);
326
   Reg#(Bit#(6)) left_qpc   <- mkReg(0);
327
   Reg#(Bit#(6)) top_qpc    <- mkReg(0);
328
   Reg#(Bit#(1)) curr_intra <- mkReg(0);
329
   Reg#(Bit#(1)) left_intra <- mkReg(0);
330
   Reg#(Bit#(1)) top_intra  <- mkReg(0);
331
 
332 8 jamey.hick
   Reg#(Bit#(2)) blockHorVerticalCleanup <- mkReg(0);
333
 
334 2 jamey.hick
   Reg#(Bit#(8)) alphaInternal  <- mkReg(0);
335
   Reg#(Bit#(5)) betaInternal   <- mkReg(0);
336 8 jamey.hick
   Reg#(Vector#(3,Bit#(5))) tc0Internal <- mkRegU();
337 2 jamey.hick
 
338
   Bit#(8) alpha_table[52] = {0,  0,  0,  0,  0,  0,  0,  0,  0,  0,
339
                              0,  0,  0,  0,  0,  0,  4,  4,  5,  6,
340
                              7,  8,  9, 10, 12, 13, 15, 17, 20, 22,
341
                             25, 28, 32, 36, 40, 45, 50, 56, 63, 71,
342
                             80, 90,101,113,127,144,162,182,203,226,
343
                            255,255};
344
   Bit#(5) beta_table[52] = {0,  0,  0,  0,  0,  0,  0,  0,  0,  0,
345
                             0,  0,  0,  0,  0,  0,  2,  2,  2,  3,
346
                             3,  3,  3,  4,  4,  4,  6,  6,  7,  7,
347
                             8,  8,  9,  9, 10, 10, 11, 11, 12, 12,
348
                            13, 13, 14, 14, 15, 15, 16, 16, 17, 17,
349
                            18, 18};
350
   Bit#(5) tc0_table[52][3] = {{ 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, 0 },
352
                               { 0, 0, 0 }, { 0, 0, 0 }, { 0, 0, 0 }, { 0, 0, 0 }, { 0, 0, 0 }, { 0, 0, 1 },
353
                               { 0, 0, 1 }, { 0, 0, 1 }, { 0, 0, 1 }, { 0, 1, 1 }, { 0, 1, 1 }, { 1, 1, 1 },
354
                               { 1, 1, 1 }, { 1, 1, 1 }, { 1, 1, 1 }, { 1, 1, 2 }, { 1, 1, 2 }, { 1, 1, 2 },
355
                               { 1, 1, 2 }, { 1, 2, 3 }, { 1, 2, 3 }, { 2, 2, 3 }, { 2, 2, 4 }, { 2, 3, 4 },
356
                               { 2, 3, 4 }, { 3, 3, 5 }, { 3, 4, 6 }, { 3, 4, 6 }, { 4, 5, 7 }, { 4, 5, 8 },
357
                               { 4, 6, 9 }, { 5, 7,10 }, { 6, 8,11 }, { 6, 8,13 }, { 7,10,14 }, { 8,11,16 },
358
                               { 9,12,18 }, {10,13,20 }, {11,15,23 }, {13,17,25 }};
359
 
360 8 jamey.hick
   IWorkVectorHor workVectorRows <- mkWorkVectorHor();
361
   IWorkVectorVer workVectorCols <- mkWorkVectorVer();
362 6 jamey.hick
   ILeftVector leftVector <- mkLeftVector();
363 8 jamey.hick
   ITopVector  topVector  <- mkTopVector();
364
   Reg#(Bit#(16)) topVectorValidBits <- mkReg(0);
365 2 jamey.hick
 
366 11 jamey.hick
   IbSVector bSfileHor <- mkbSVector();
367
   IbSVector bSfileVer <- mkbSVector();
368 2 jamey.hick
 
369 6 jamey.hick
   Reg#(Bit#(6)) cleanup_state <- mkReg(0);
370 2 jamey.hick
 
371 51 jamey.hick
   Vector#(4, FIFO#(Bit#(32))) rowToColumnStore <- replicateM(mkSizedFIFO(3));
372 8 jamey.hick
   Reg#(Bit#(2)) rowToColumnState <- mkReg(0);
373 42 jamey.hick
   FIFO#(Tuple2#(Bit#(4),Bit#(1))) rowToColumnStoreBlock <- mkFIFO(); // The third bit 1 is to rotate the damned
374 8 jamey.hick
                                                                              // last left vector block
375 10 jamey.hick
   FIFO#(Tuple2#(Bit#(4), Bit#(32))) verticalFilterBlock <- mkFIFO();
376 6 jamey.hick
 
377 8 jamey.hick
   Reg#(Bit#(2)) columnState <- mkReg(0);
378 51 jamey.hick
   Vector#(4, FIFO#(Bit#(32))) columnToRowStore <- replicateM(mkSizedFIFO(3));
379 8 jamey.hick
   Reg#(Bit#(2)) columnToRowState <- mkReg(0);
380 42 jamey.hick
   FIFO#(Tuple2#(Bit#(4), Bit#(1))) columnToRowStoreBlock <- mkFIFO();
381 8 jamey.hick
 
382
   Reg#(Bit#(2)) columnNumber <- mkReg(0);
383 43 jamey.hick
 
384 37 jamey.hick
   // Debugging register
385
   Reg#(Bit#(32)) fifo_full_count <- mkReg(0);
386 39 jamey.hick
   Reg#(Bit#(32)) fifo_empty_count <- mkReg(0);
387 37 jamey.hick
   Reg#(Bit#(32)) total_cycles <- mkReg(0);
388 8 jamey.hick
 
389 2 jamey.hick
 
390 37 jamey.hick
   rule incr;
391
     total_cycles <= total_cycles + 1;
392
   endrule
393 2 jamey.hick
 
394 39 jamey.hick
   rule emptyFIFO;
395
     if(!infifo.notEmpty)
396
       begin
397
          fifo_empty_count <= fifo_empty_count + 1;
398
          $display("DEBLOCK FIFO EMPTY: %d of %d",fifo_empty_count, total_cycles);
399
       end
400
   endrule
401 37 jamey.hick
 
402 2 jamey.hick
   rule checkFIFO ( True );
403 37 jamey.hick
      $display( "Trace DeblockFilter: checkFIFO %h cycle: %d", infifo.first(), total_cycles );
404
      $display( "TRACE DeblockFilter: checkFIFO %h", infifo.first() );
405
      if(!infifo.notFull)
406
        begin
407
          fifo_full_count <= fifo_full_count + 1;
408 44 jamey.hick
          $display("DEBLOCK FIFO(%d) FULL: %d of %d",deblockFilter_infifo_size, fifo_full_count, total_cycles);
409 39 jamey.hick
        end
410 2 jamey.hick
   endrule
411 37 jamey.hick
 
412
   rule memReqMergeRowToColumnConversion;
413
     memReqRowToColumnConversion.deq();
414
     dataMemReqQ.enq(memReqRowToColumnConversion.first());
415
   endrule
416
 
417
   rule memReqMergeVertical;
418
     memReqVertical.deq();
419
     dataMemReqQ.enq(memReqVertical.first());
420
   endrule
421
 
422
   rule memReqMergeDataSendReq;
423
     memReqDataSendReq.deq();
424
     dataMemReqQ.enq(memReqDataSendReq.first());
425
   endrule
426
 
427
   rule outfifoVerticalSplit;
428
     outfifoVertical.deq();
429
     outfifo.enq(outfifoVertical.first());
430
   endrule
431
 
432 2 jamey.hick
   rule passing ( process matches Passing );
433
      case (infifo.first()) matches
434
         tagged NewUnit . xdata :
435
            begin
436
               infifo.deq();
437 13 jamey.hick
               outfifo.enq(EDOT (infifo.first()));
438 2 jamey.hick
               $display("ccl5newunit");
439
               $display("ccl5rbspbyte %h", xdata);
440
            end
441
         tagged SPSpic_width_in_mbs .xdata :
442
            begin
443
               infifo.deq();
444 13 jamey.hick
               outfifo.enq(EDOT (infifo.first()));
445 2 jamey.hick
               picWidth <= xdata;
446
            end
447
         tagged SPSpic_height_in_map_units .xdata :
448
            begin
449
               infifo.deq();
450 13 jamey.hick
               outfifo.enq(EDOT (infifo.first()));
451 37 jamey.hick
               picHeight <= xdata;
452 2 jamey.hick
            end
453
         tagged PPSdeblocking_filter_control_present_flag .xdata :
454
            begin
455
               infifo.deq();
456
               if (xdata == 0)
457
                  begin
458
                     disable_deblocking_filter_idc <= 0;
459
                     slice_alpha_c0_offset <= 0;
460
                     slice_beta_offset <= 0;
461
                  end
462
            end
463
         tagged SHfirst_mb_in_slice .xdata :
464
            begin
465
               infifo.deq();
466 13 jamey.hick
               outfifo.enq(EDOT (infifo.first()));
467 2 jamey.hick
               firstMb   <= xdata;
468
               currMb    <= xdata;
469
               currMbHor <= xdata;
470
               currMbVer <= 0;
471
            end
472
         tagged SHdisable_deblocking_filter_idc .xdata :
473
            begin
474
               infifo.deq();
475
               disable_deblocking_filter_idc <= xdata;
476
            end
477
         tagged SHslice_alpha_c0_offset .xdata :
478
            begin
479
               infifo.deq();
480
               slice_alpha_c0_offset <= xdata;
481
            end
482
         tagged SHslice_beta_offset .xdata :
483
            begin
484
               infifo.deq();
485
               slice_beta_offset <= xdata;
486
            end
487
         tagged IBTmb_qp .xdata :
488
            begin
489
               infifo.deq();
490
               curr_qpy <= xdata.qpy;
491
               curr_qpc <= xdata.qpc;
492
            end
493
         tagged PBbS .xdata :
494
            begin
495 51 jamey.hick
               process <= Initialize;
496 2 jamey.hick
            end
497
         tagged PBoutput .xdata :
498
            begin
499
               $display( "ERROR Deblocking Filter: passing PBoutput");
500
            end
501
         tagged EndOfFile :
502
            begin
503
               infifo.deq();
504 13 jamey.hick
               outfifo.enq(EDOT (infifo.first()));
505 2 jamey.hick
               $display( "ccl5: EndOfFile reached");
506
               //$finish(0);
507
            end
508
         default:
509
            begin
510
               infifo.deq();
511 13 jamey.hick
               outfifo.enq(EDOT (infifo.first()));
512 2 jamey.hick
            end
513
      endcase
514
   endrule
515
 
516 8 jamey.hick
   // What does this rule do?
517 2 jamey.hick
   rule currMbHorUpdate( !(currMbHor
518 8 jamey.hick
      $display( "TRACE Deblocking Filter: strange update rule firing... %0d", currMb);
519 2 jamey.hick
      Bit#(PicAreaSz) temp = zeroExtend(picWidth);
520
      if((currMbHor >> 3) >= temp)
521
         begin
522
            currMbHor <= currMbHor - (temp << 3);
523
            currMbVer <= currMbVer + 8;
524
         end
525
      else
526
         begin
527
            currMbHor <= currMbHor - temp;
528
            currMbVer <= currMbVer + 1;
529
         end
530
   endrule
531
 
532
 
533 51 jamey.hick
   rule initialize ( process==Initialize && currMbHor
534
      $display( "TRACE Deblocking Filter: initialize %0d", currMb);
535
      process <= Horizontal;
536
      dataReqCount <= 1;
537
      dataRespCount <= 1;
538
      filterTopMbEdgeFlag <= !(currMb
539
      filterLeftMbEdgeFlag <= !(currMbHor==0 || disable_deblocking_filter_idc==1 || (disable_deblocking_filter_idc==2 && currMb==firstMb));
540
      filterInternalEdgesFlag <= !(disable_deblocking_filter_idc==1);
541
      blockNum <= 0;
542
      pixelNum <= 0;
543
      topVectorValidBits <= 0;
544
   endrule
545
 
546 2 jamey.hick
   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 60 jamey.hick
            let temp3 = {temp,chromaFlagHor,temp2}; // here the troubles begin
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 60 jamey.hick
         if(chromaFlagHor == 0)
611 8 jamey.hick
           begin
612 60 jamey.hick
             memReqRowToColumnConversion.enq(StoreReq {addr:{adjustedMbHor,chromaFlagHor,2'b11,rowToColumnState},data:data_out});
613 8 jamey.hick
           end
614
         else
615
           begin  //differentiate between u and v
616 60 jamey.hick
             memReqRowToColumnConversion.enq(StoreReq {addr:{adjustedMbHor,chromaFlagHor,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 60 jamey.hick
     if( !topValues && (!(blockHor==3 || (blockHor[0]==1 && chromaFlagVer==1)) || (currMbVer==picHeight-1)))
667 8 jamey.hick
       begin
668
         $display( " Normal");
669 60 jamey.hick
         if(chromaFlagVer==0)
670 8 jamey.hick
           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 60 jamey.hick
         if(chromaFlagVer==0)
691 8 jamey.hick
           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 60 jamey.hick
     if( !topValues && (blockHor==3 || (blockHor[0]==1 && chromaFlagVer==1))) // We need to write to the left Vector which will be used in the future. These values will not be written out.
708 8 jamey.hick
          // 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 60 jamey.hick
         if(chromaFlagVer==0)
713 8 jamey.hick
           begin
714
             if((blockVer == 3) && (columnToRowState == 3))
715
               begin
716 61 jamey.hick
                 chromaFlagVer <= 1;
717
                 process <= Initialize;
718 8 jamey.hick
               end
719
             //check for last macro block
720 19 jamey.hick
             leftVector.upd({1'b0,blockVer,columnToRowState}, data_out);
721 8 jamey.hick
           end
722
         else
723
           begin
724
             // Only cleanup a single time after the chroma blocks
725
             if((blockHor == 3) && (blockVer[0] == 1) && (columnToRowState == 3))
726
               begin
727 61 jamey.hick
                 $display( "TRACE Deblocking Filter: horizontal bsFIFO chroma completed");
728
                 chromaFlagVer <= 0;
729
                 Bit#(PicWidthSz) temp = truncate(currMbHor);
730
                 parameterMemReqQ.enq(StoreReq {addr:temp,data:{curr_intra,curr_qpc,curr_qpy}});
731
                 currMb <= currMb+1;
732
                 currMbHor <= currMbHor+1;
733
                 if(currMbVer==picHeight-1 && currMbHor==zeroExtend(picWidth-1))
734
                   begin
735
                     process <= Cleanup;
736
                   end
737
                 else
738
                   begin
739
                     process <= Passing;
740
                   end
741 8 jamey.hick
               end
742 19 jamey.hick
             leftVector.upd({1'b1,blockHor[1],blockVer[0],columnToRowState}, data_out);
743 8 jamey.hick
           end
744
         end
745
 
746
   endrule
747
 
748 2 jamey.hick
 
749
   rule dataReceiveResp ( dataRespCount>0 && !(currMb
750 8 jamey.hick
      $display( "TRACE Deblocking Filter: dataReceiveResp %0d", dataRespCount);
751 2 jamey.hick
      Bit#(4) temp = truncate(dataRespCount-1);
752
      if(dataRespCount==1)
753
         begin
754
            Bit#(13) tempParameters=0;
755
            if(parameterMemRespQ.first() matches tagged LoadResp .xdata)
756
               tempParameters = xdata;
757
            top_qpy <= tempParameters[5:0];
758
            top_qpc <= tempParameters[11:6];
759
            top_intra <= tempParameters[12];
760
            parameterMemRespQ.deq();
761
         end
762
      if(dataRespCount==16)
763
         dataRespCount <= 0;
764
      else
765
         dataRespCount <= dataRespCount+1;
766
      if(dataMemRespQ.first() matches tagged LoadResp .xdata)
767 8 jamey.hick
        begin
768
          topVectorValidBits[temp] <= 1;
769
          topVector.upd(temp, xdata);
770
        end
771 2 jamey.hick
      dataMemRespQ.deq();
772
      //$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]);
773
   endrule
774
 
775
 
776
   rule horizontal ( process==Horizontal && currMbHor
777
      Bit#(2) blockHor = {blockNum[2],blockNum[0]};
778
      Bit#(2) blockVer = {blockNum[3],blockNum[1]};
779 8 jamey.hick
      Bit#(2) pixelVer = pixelNum;
780
 
781 60 jamey.hick
      Bool leftEdge = (blockNum[0]==0 && (blockNum[2]==0 || chromaFlagHor==1));
782 2 jamey.hick
      if(blockNum==0 && pixelNum==0)
783
         begin
784 60 jamey.hick
            Bit#(6) qpav = (chromaFlagHor==0 ? curr_qpy : curr_qpc);
785 2 jamey.hick
            Bit#(8) indexAtemp = zeroExtend(qpav)+signExtend(slice_alpha_c0_offset);
786
            Bit#(8) indexBtemp = zeroExtend(qpav)+signExtend(slice_beta_offset);
787
            Bit#(6) indexA = (indexAtemp[7]==1 ? 0 : (indexAtemp[6:0]>51 ? 51 : indexAtemp[5:0]));
788
            Bit#(6) indexB = (indexBtemp[7]==1 ? 0 : (indexBtemp[6:0]>51 ? 51 : indexBtemp[5:0]));
789
            alphaInternal <= alpha_table[indexA];
790
            betaInternal <= beta_table[indexB];
791
            Vector#(3,Bit#(5)) tc0temp = arrayToVector(tc0_table[indexA]);
792
            tc0Internal <= tc0temp;
793
         end
794
      case (infifo.first()) matches
795
         tagged PBbS .xdata :
796
            begin
797 11 jamey.hick
               infifo.deq();
798
               bSfileHor.upd(blockNum, xdata.bShor);
799
               bSfileVer.upd(blockNum, xdata.bSver);
800 10 jamey.hick
               $display( "TRACE Deblocking Filter: horizontal bsFIFO data: %d, subblock(%0d, %0d) row: %0d, ",infifo.first(), blockHor, blockVer, pixelNum);
801 2 jamey.hick
            end
802
         tagged PBoutput .xdata :
803
            begin
804 19 jamey.hick
               Bit#(PicWidthSz) currMbHorT = truncate(currMbHor);
805 60 jamey.hick
               if((chromaFlagHor == 1) && (blockHor[1] == 1))
806 19 jamey.hick
                 begin
807
                   $display("PRE %h %h %h", {currMbVer,blockVer[0],pixelVer},{currMbHorT,blockHor[0]}, {xdata[0],xdata[1],xdata[2],xdata[3]});
808
                 end
809 60 jamey.hick
               $display( "TRACE Deblocking Filter: horizontal chroma: %d, subblock(%0d, %0d) row: %0d, data: %h", chromaFlagHor, blockHor, blockVer, pixelNum, xdata);
810 2 jamey.hick
               infifo.deq();
811
               Bit#(6) addrq = {blockHor,blockVer,pixelVer};
812 60 jamey.hick
               Bit#(5) addrpLeft = (chromaFlagHor==0 ? {1'b0,blockVer,pixelVer} : {1'b1,blockHor[1],blockVer[0],pixelVer});
813 2 jamey.hick
               Bit#(6) addrpCurr = {(blockHor-1),blockVer,pixelVer};
814
               Bit#(32) pixelq = {xdata[3],xdata[2],xdata[1],xdata[0]};
815
               Bit#(32) pixelp;
816
               if(leftEdge)
817 8 jamey.hick
                 begin
818
                   pixelp <- leftVector.sub(addrpLeft);
819
                   $display( "TRACE Deblocking Filter: horizontal P (left) addr %h, data %h ",addrpLeft, pixelp);
820
                 end
821 2 jamey.hick
               else
822 8 jamey.hick
                 begin
823
                   pixelp <- workVectorRows.sub({blockVer[0], pixelVer});
824
                   $display( "TRACE Deblocking Filter: horizontal P (work) addr %h, data %h ",addrpCurr, pixelp);
825
                 end
826 2 jamey.hick
               Bit#(64) result = {pixelq,pixelp};
827
               if(leftEdge && filterLeftMbEdgeFlag)
828
                  begin
829 60 jamey.hick
                    Bit#(6) curr_qp = (chromaFlagHor==0 ? curr_qpy : curr_qpc);
830
                    Bit#(6) left_qp = (chromaFlagHor==0 ? left_qpy : left_qpc);
831 8 jamey.hick
                    Bit#(7) qpavtemp = zeroExtend(curr_qp)+zeroExtend(left_qp)+1;
832
                    Bit#(6) qpav = qpavtemp[6:1];
833
                    Bit#(8) indexAtemp = zeroExtend(qpav)+signExtend(slice_alpha_c0_offset);
834
                    Bit#(8) indexBtemp = zeroExtend(qpav)+signExtend(slice_beta_offset);
835
                    Bit#(6) indexA = (indexAtemp[7]==1 ? 0 : (indexAtemp[6:0]>51 ? 51 : indexAtemp[5:0]));
836
                    Bit#(6) indexB = (indexBtemp[7]==1 ? 0 : (indexBtemp[6:0]>51 ? 51 : indexBtemp[5:0]));
837
                    Bit#(8) alphaMbLeft = alpha_table[indexA];
838
                    Bit#(5) betaMbLeft = beta_table[indexB];
839
                    Vector#(3,Bit#(5)) tc0MbLeft = arrayToVector(tc0_table[indexA]);
840
                    if(filter_test({pixelq[15:0],pixelp[31:16]},alphaMbLeft,betaMbLeft))
841
                      begin
842 19 jamey.hick
                         $display("TRACE mkDeblockFilter: Applying horizontal, left filter");
843 60 jamey.hick
                         Bit#(3) bsData <- bSfileHor.sub((chromaFlagHor==0?blockNum:{blockNum[1:0],pixelVer[1],1'b0}));
844
                         result = filter_input({pixelq,pixelp},chromaFlagHor==1,bsData,alphaMbLeft,betaMbLeft,tc0MbLeft);
845 8 jamey.hick
                       end
846 2 jamey.hick
                  end
847
               else if(!leftEdge && filterInternalEdgesFlag)
848
                  begin
849
                     if(filter_test({pixelq[15:0],pixelp[31:16]},alphaInternal,betaInternal))
850 8 jamey.hick
                       begin
851 19 jamey.hick
                         $display("TRACE mkDeblockFilter: Applying horizontal, internal filter");
852 60 jamey.hick
                         Bit#(3) bSData <- bSfileHor.sub((chromaFlagHor==0?blockNum:{blockNum[1:0],pixelVer[1],1'b0}));
853
                         result = filter_input({pixelq,pixelp},chromaFlagHor==1,bSData,alphaInternal,betaInternal,tc0Internal);
854 8 jamey.hick
                       end
855 2 jamey.hick
                  end
856 19 jamey.hick
 
857 8 jamey.hick
 
858 2 jamey.hick
               if(leftEdge)
859 8 jamey.hick
                 begin
860
                   // write out the left edge
861
                   //Check to store this value to the memory.  I think the rotation is off here.
862
                   // I should also adjust the vertical Mb...  Figure out MbHorT
863 19 jamey.hick
 
864 9 jamey.hick
                   Bit#(PicHeightSz) adjustedMbVer = ((currMbHorT==0) && (currMbVer!=0)) ? currMbVer-1 : currMbVer;
865 8 jamey.hick
                   Bit#(PicWidthSz)  adjustedMbHor = currMbHorT==0 ? picWidth-1 : currMbHorT-1;
866
                   // In this case we buffer the bottom vertical element, since it has to be used again
867 60 jamey.hick
                   if(((blockVer == 3) || ((chromaFlagHor == 1) && (blockVer == 1))) && (adjustedMbVer != picHeight - 1))
868 8 jamey.hick
                     begin
869
                       rowToColumnStore[pixelNum[1:0]].enq(result[31:0]);
870
                       // only push in a command for the bottom leftblock.  It has to be rotated.
871 42 jamey.hick
                       if(pixelNum == 3)
872 8 jamey.hick
                         begin
873 10 jamey.hick
                           rowToColumnStoreBlock.enq(tuple2(blockNum,1));
874 8 jamey.hick
                         end
875
                    end
876
                   // these outputs occur in the past, so we must use the adjusted Mb numbers
877 60 jamey.hick
                   else if(chromaFlagHor==0)
878 8 jamey.hick
                     begin
879
                       $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] );
880 37 jamey.hick
                       outfifoVertical.enq(DFBLuma {ver:{adjustedMbVer,blockVer,pixelVer},
881 8 jamey.hick
                                            hor:{adjustedMbHor,2'b11},
882 19 jamey.hick
                                            data:result[31:0]});
883 8 jamey.hick
                     end
884
                   else
885
                     begin
886
                       $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]);
887 37 jamey.hick
                       outfifoVertical.enq(DFBChroma {uv:blockHor[1],
888 8 jamey.hick
                                              ver:{adjustedMbVer,blockVer[0],pixelVer},
889
                                              hor:{adjustedMbHor,1'b1},
890 19 jamey.hick
                                              data:result[31:0]});
891 8 jamey.hick
                      end
892
                 end
893 2 jamey.hick
               else
894 8 jamey.hick
                  begin
895
                    // push the correction into reorder block;
896
                    rowToColumnStore[addrpCurr[1:0]].enq(result[31:0]);
897
                    // Push down the block number and the chroma flag into the pipeline
898 42 jamey.hick
                    if(pixelNum == 3)
899 8 jamey.hick
                      begin
900
                        let blockHorPast = blockHor - 1;
901
                        let blockNumPast = {blockVer[1], blockHorPast[1], blockVer[0], blockHorPast[0]};
902 10 jamey.hick
                        rowToColumnStoreBlock.enq(tuple2(blockNumPast,0));
903 8 jamey.hick
                      end
904
                  end
905
               $display( "TRACE Deblocking Filter: horizontal Q (work) addr %h, data %h, original data: %h ",addrq, result[63:32], pixelq);
906
               workVectorRows.upd({blockVer[0],pixelVer}, result[63:32]);
907 6 jamey.hick
 
908 8 jamey.hick
               // Step out to clean up the edge block
909
               // What about the chroma?
910 60 jamey.hick
               if((pixelNum == 3) && ((blockHor == 3) || ((chromaFlagHor == 1) && (blockHor == 1))))
911 8 jamey.hick
                 begin
912
                    $display( "TRACE Deblocking Filter: Heading to Horizontal Cleanup");
913
                   process <= HorizontalCleanup;// we enter this state to push out the remaining
914
                                                  // blocks, that haven't been shoved out.  Namely, the
915
                                                  // left blocks.
916
                 end
917
               else if(pixelNum==3)
918
                 begin
919 10 jamey.hick
                   $display( "TRACE Deblocking Filter: horizontal bsFIFO completed subblock(%0d, %0d)", blockHor, blockVer);
920 8 jamey.hick
                   blockNum <= blockNum+1;
921
                 end
922
               pixelNum <= pixelNum+1;
923 2 jamey.hick
            end
924
         default: $display( "ERROR Deblocking Filter: horizontal non-PBoutput input");
925
      endcase
926
   endrule
927
 
928 8 jamey.hick
  rule horizontal_cleanup(process == HorizontalCleanup);
929
    Bit#(2) blockHor = {blockNum[2],blockNum[0]};
930
    Bit#(2) blockVer = {blockNum[3],blockNum[1]};
931
    $display( "TRACE Deblocking Filter: horizontal_cleanup (%0d, %0d) row: %d", blockHor, blockVer, pixelNum);
932 60 jamey.hick
    if(pixelNum==3 && (blockNum==15 || (blockNum==7 && chromaFlagHor==1)))
933 8 jamey.hick
      begin
934
        if(blockNum == 15)
935
          begin
936
            $display( "TRACE Deblocking Filter: horizontal completed Mb (%0d) Luma", currMb);
937
          end
938
        else
939
          begin
940
            $display( "TRACE Deblocking Filter: horizontal completed Mb (%0d) Chroma", currMb);
941
          end
942
        blockNum <= 0;
943
        process <= Vertical;// we enter this state to wait for the vertical processing to complete
944 60 jamey.hick
        if(chromaFlagHor == 1)
945
          begin
946 61 jamey.hick
            chromaFlagHor <= 0;
947 60 jamey.hick
            left_intra <= curr_intra;
948
            left_qpc <= curr_qpc;
949
            left_qpy <= curr_qpy;
950
          end
951 61 jamey.hick
        else
952
          begin
953
            chromaFlagHor <= 1;
954
          end
955 10 jamey.hick
        rowToColumnStoreBlock.enq(tuple2(blockNum,0));
956 8 jamey.hick
      end
957
    else if(pixelNum == 3)
958
      begin
959
        blockNum <= blockNum + 1;
960
        process <= Horizontal; // not done with this Mb yet.
961 10 jamey.hick
        rowToColumnStoreBlock.enq(tuple2(blockNum,0));
962 8 jamey.hick
      end
963
    pixelNum <= pixelNum + 1;
964
    // push the correction into reorder block;
965
    Bit#(32) work_data <- workVectorRows.sub({blockVer[0], pixelNum});
966
    rowToColumnStore[pixelNum].enq(work_data);
967
  endrule
968 2 jamey.hick
 
969 8 jamey.hick
 
970
  // declare these to share the rule
971
  begin
972
   Bit#(4) blockNumCols = tpl_1(verticalFilterBlock.first());
973
   Bit#(2) blockVer = {blockNumCols[3],blockNumCols[1]};
974
   Bit#(2) blockHor = {blockNumCols[2],blockNumCols[0]};
975
   Bool topEdge = (blockVer==0);
976 10 jamey.hick
 
977 8 jamey.hick
 
978 19 jamey.hick
  rule vertical_filter_halt((verticalState == NormalOperation) && !((!topEdge) || (topVectorValidBits[{blockHor,columnNumber}] == 1) || (currMb
979 8 jamey.hick
        if(process == Vertical || process == Horizontal)
980
          begin
981 60 jamey.hick
            $display("TRACE Deblocking Filter: Vertical processing halted on block: %h (%0d, %0d), column %d chromaFlag %d due to data dependency",  blockNumCols, blockHor, blockVer, columnNumber, chromaFlagVer);
982 8 jamey.hick
          end
983
 
984
  endrule
985
 
986
 
987
  // As with horizontal, the q data will be read from the data store, and the p data will be streamed in via the
988
  // reordering FIFO.  The new p data must be stored, but the q data will need to be spooled out, since it needs to
989
  // make it to the left vector.
990 19 jamey.hick
  rule vertical((verticalState == NormalOperation) && ((!topEdge) || (topVectorValidBits[{blockHor,columnNumber}] == 1) || (currMb
991 8 jamey.hick
    //$display( "TRACE Deblocking Filter: vertical %0d %0d", colNum, rowNum);
992
    //$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]);
993
    //Process the block according to what got passed to us.
994
    Bit#(32) workV = tpl_2(verticalFilterBlock.first());
995
    Bit#(32) tempV = 0;
996
    Bit#(64) resultV = 0;
997
    Bit#(8) alpha;
998
    Bit#(5) beta;
999
    Vector#(3,Bit#(5)) tc0;
1000
 
1001 19 jamey.hick
 
1002 8 jamey.hick
      $display( "TRACE Deblocking Filter: vertical subblock (%0d, %0d), column: %d, data: %h", blockHor, blockVer, columnNumber, workV);
1003
      columnNumber <= columnNumber + 1;
1004
      verticalFilterBlock.deq();
1005 2 jamey.hick
      if(topEdge)
1006
         begin
1007 60 jamey.hick
            Bit#(6) curr_qp = (chromaFlagVer==0 ? curr_qpy : curr_qpc); // may need to check these
1008
            Bit#(6) top_qp = (chromaFlagVer==0 ? top_qpy : top_qpc);
1009 8 jamey.hick
            Bit#(7) qpavtemp = zeroExtend(curr_qp)+zeroExtend(top_qp)+1;
1010
            Bit#(6) qpav = qpavtemp[6:1];
1011
            Bit#(8) indexAtemp = zeroExtend(qpav)+signExtend(slice_alpha_c0_offset);
1012
            Bit#(8) indexBtemp = zeroExtend(qpav)+signExtend(slice_beta_offset);
1013
            Bit#(6) indexA = (indexAtemp[7]==1 ? 0 : (indexAtemp[6:0]>51 ? 51 : indexAtemp[5:0]));
1014
            Bit#(6) indexB = (indexBtemp[7]==1 ? 0 : (indexBtemp[6:0]>51 ? 51 : indexBtemp[5:0]));
1015
            Bit#(8) alphaMbTop = alpha_table[indexA];
1016
            Bit#(5) betaMbTop = beta_table[indexB];
1017
            Vector#(3,Bit#(5)) tc0MbTop = arrayToVector(tc0_table[indexA]);
1018
            tempV <- topVector.sub({blockHor,columnNumber});
1019 9 jamey.hick
            $display( "TRACE Deblocking Filter: vertical P (top) addr %h, orig data %h ",{blockVer,columnNumber}, tempV);
1020 8 jamey.hick
            alpha = alphaMbTop;
1021
            beta = betaMbTop;
1022
            tc0 = tc0MbTop;
1023 2 jamey.hick
         end
1024
      else
1025 8 jamey.hick
         begin
1026
            // We read this value from the original vector
1027 36 jamey.hick
            tempV <- topVector.sub({blockHor, columnNumber});
1028 9 jamey.hick
            $display( "TRACE Deblocking Filter: vertical P (work) addr %h, orig data %h ",{blockHor, blockVer - 1, columnNumber}, tempV);
1029 2 jamey.hick
            alpha = alphaInternal;
1030
            beta = betaInternal;
1031
            tc0 = tc0Internal;
1032
         end
1033 8 jamey.hick
 
1034
      // Marshalling data in things upon which the filter blocks can be applied
1035
 
1036
      resultV = {tpl_2(verticalFilterBlock.first()),tempV};
1037
 
1038
      // Apply filter, only if filter test passes, and we are either filtering the top edge, or we aren't on the top edge
1039 9 jamey.hick
      $display( "TRACE Deblocking Filter: vertical Filter test: P1P0Q0Q1: %h",{workV[15:8],workV[7:0],tempV[31:24],tempV[23:16]});
1040 19 jamey.hick
      if((filter_test({workV[15:8],workV[7:0],tempV[31:24],tempV[23:16]},alpha,beta)) && ((topEdge && filterTopMbEdgeFlag)|| (!topEdge && filterInternalEdgesFlag) ))
1041 8 jamey.hick
        begin
1042 19 jamey.hick
          $display("TRACE mkDeblockFilter: Applying vertical filter");
1043 60 jamey.hick
          Bit#(3) bsData <- bSfileVer.sub((chromaFlagVer==0?blockNumCols:{blockVer[0],blockHor[0],1'b0,columnNumber[1]}));
1044
          resultV = filter_input(resultV,chromaFlagVer==1,bsData,alpha,beta,tc0);
1045 8 jamey.hick
        end
1046
      //Write out the result data  31:0 are the done q values
1047 2 jamey.hick
      if(topEdge)
1048
         begin
1049 8 jamey.hick
            // We really need to just output these values -> need to shove them to the rotation unit, but only if the
1050
            // current Mb vertical component is larger than 0.  All of these are done and can be dumped out
1051
            if(currMbVer > 0)
1052
              begin
1053 42 jamey.hick
                if(columnNumber == 3)
1054 8 jamey.hick
                  begin
1055 10 jamey.hick
                    columnToRowStoreBlock.enq(tuple2(blockNumCols,1'b1));
1056 8 jamey.hick
                  end
1057
                columnToRowStore[columnNumber].enq(resultV[31:0]);
1058
              end
1059 2 jamey.hick
         end
1060
      else
1061
         begin
1062 8 jamey.hick
            // We should make the decision as to whether to store these values. We will store the bottom row, except
1063
            // for the left most block which will be stored the next time that an Mb gets processed.
1064
            // The values to store are in the P vector... except the bottom right block, which is different.
1065
            Bit#(PicWidthSz) currMbHorT = truncate(currMbHor);
1066
 
1067 60 jamey.hick
            if(((blockVer == 3) && (blockHor == 3)) || ((chromaFlagVer == 1) && (blockVer == 1) && (blockHor[0] == 1)))
1068 8 jamey.hick
              begin
1069
                // need to enter escape state to write the bottom left block to the leftVector.
1070
                if(columnNumber == 3)
1071
                  begin
1072
                    blockHorVerticalCleanup <= blockHor;
1073
                    verticalState <= VerticalCleanup;
1074
                  end
1075
              end
1076 60 jamey.hick
            else if((blockVer == 3) || ((chromaFlagVer == 1) && (blockVer == 1)))
1077 8 jamey.hick
              begin
1078
                if((currMbVer == picHeight - 1) && (columnNumber == 3)) // If we're at the bottom of the frame, we'd
1079
                                                                        // roll through the block clean up.
1080
                  begin
1081
                    blockHorVerticalCleanup <= blockHor;
1082
                    verticalState <= VerticalCleanup;
1083
                  end
1084 60 jamey.hick
                memReqVertical.enq(StoreReq {addr:{currMbHorT,chromaFlagVer,blockHor,columnNumber},data:resultV[63:32]});
1085 8 jamey.hick
              end
1086
            columnToRowStore[columnNumber].enq(resultV[31:0]);
1087
            if(columnNumber == 0)
1088
              begin
1089 10 jamey.hick
                columnToRowStoreBlock.enq(tuple2(blockNumCols,1'b0));
1090 8 jamey.hick
              end
1091 2 jamey.hick
         end
1092
 
1093 9 jamey.hick
        $display( "TRACE Deblocking Filter: vertical P                 data %h                     ",  resultV[31:0]);
1094 8 jamey.hick
        $display( "TRACE Deblocking Filter: vertical Q (work) addr %h, data %h, original data: %h  ",{blockHor,blockVer,columnNumber}, resultV[63:32], workV);
1095 2 jamey.hick
 
1096 36 jamey.hick
        topVector.upd({blockHor,columnNumber}, resultV[63:32]);
1097 8 jamey.hick
  endrule
1098
end
1099 2 jamey.hick
 
1100 8 jamey.hick
  rule vertical_cleanup(verticalState == VerticalCleanup);
1101
    $display( "TRACE Deblocking Filter: vertical_cleanup at block end column: %d ", columnNumber);
1102
    columnNumber <= columnNumber + 1;
1103
    if(columnNumber == 3)
1104
      begin
1105
        verticalState <= NormalOperation;
1106
      end
1107 60 jamey.hick
    if(chromaFlagVer == 0)
1108 8 jamey.hick
      begin
1109
        Bit#(2) blockHor = blockHorVerticalCleanup;
1110
        Bit#(2) blockVer = 0;
1111 42 jamey.hick
        if(columnNumber == 3)
1112 8 jamey.hick
          begin
1113
            // Horizontal Postion is 3, but vertical position is 0, owing to subtraction in the rotation unit
1114 10 jamey.hick
            columnToRowStoreBlock.enq(tuple2({blockVer[1],blockHor[1],blockVer[0],blockHor[0]},1'b0));
1115 8 jamey.hick
          end
1116 36 jamey.hick
        Bit#(32) w_data <- topVector.sub({blockHor, columnNumber});
1117 8 jamey.hick
        columnToRowStore[columnNumber].enq(w_data);
1118
     end
1119
   else
1120
     begin
1121
       Bit#(2) blockHor = blockHorVerticalCleanup;
1122 10 jamey.hick
       Bit#(2) blockVer = 2; // need to make this two for subtraction in rotation unit
1123 42 jamey.hick
       if(columnNumber == 3)
1124 8 jamey.hick
         begin
1125
           // Horizontal Postion is 3, but vertical position is 0, owing to subtraction in the rotation unit
1126 10 jamey.hick
           columnToRowStoreBlock.enq(tuple2({blockVer[1],blockHor[1],blockVer[0],blockHor[0]},1'b0));
1127 8 jamey.hick
         end
1128 36 jamey.hick
       Bit#(32) w_data <- topVector.sub({blockHor, columnNumber});
1129 8 jamey.hick
       columnToRowStore[columnNumber].enq(w_data);
1130
     end
1131
  endrule
1132 2 jamey.hick
 
1133
 
1134 61 jamey.hick
  rule cleanup ( process==Cleanup && currMbHor
1135 8 jamey.hick
    $display( "TRACE Deblocking Filter: cleanup %0d", currMb);
1136 61 jamey.hick
    outfifo.enq(EndOfFrame);
1137
    process <= Passing;
1138
  endrule
1139 8 jamey.hick
 
1140 2 jamey.hick
   interface Client mem_client_data;
1141
      interface Get request  = fifoToGet(dataMemReqQ);
1142
      interface Put response = fifoToPut(dataMemRespQ);
1143
   endinterface
1144
 
1145
   interface Client mem_client_parameter;
1146
      interface Get request  = fifoToGet(parameterMemReqQ);
1147 61 jamey.hick
 
1148 2 jamey.hick
      interface Put response = fifoToPut(parameterMemRespQ);
1149
   endinterface
1150
 
1151 37 jamey.hick
   interface Put ioin  = fifoToPut(fifofToFifo(infifo));
1152 2 jamey.hick
   interface Get ioout = fifoToGet(outfifo);
1153
 
1154
endmodule
1155
 
1156
endpackage

powered by: WebSVN 2.1.0

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