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 100

Details | Compare with Previous | View Log

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

powered by: WebSVN 2.1.0

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