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 63

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

powered by: WebSVN 2.1.0

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