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 69

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

powered by: WebSVN 2.1.0

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