OpenCores
URL https://opencores.org/ocsvn/bluespec-h264/bluespec-h264/trunk

Subversion Repositories bluespec-h264

[/] [bluespec-h264/] [trunk/] [LumaChromaParallel/] [mkDeblockFilter.bsv] - Blame information for rev 88

Go to most recent revision | Details | Compare with Previous | View Log

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

powered by: WebSVN 2.1.0

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