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 100

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 91 jamey.hick
               $display( "ERROR Deblocking Filter(%s): passing PBoutput", csStr);
522
               match {.colorFlag, .data} = xdata;
523
               if(chromaFlag != colorFlag)
524
                  begin
525
                     $display( "ERROR Deblocking Filter(%s): color mismatch", csStr);
526
                  end
527 86 jamey.hick
            end
528
         tagged EndOfFile :
529
            begin
530
               infifo.deq();
531
               outfifo.enq(EDOT (infifo.first()));
532 88 jamey.hick
               $display( "ccl5: EndOfFile reached: %0d", total_cycles);
533 86 jamey.hick
               //$finish(0);
534
            end
535
         default:
536
            begin
537
               infifo.deq();
538
               outfifo.enq(EDOT (infifo.first()));
539
            end
540
      endcase
541
   endrule
542
 
543
   // What does this rule do?
544
   rule currMbHorUpdate( !(currMbHor
545
      $display( "TRACE Deblocking Filter: strange update rule firing... %0d", currMb);
546
      Bit#(PicAreaSz) temp = zeroExtend(picWidth);
547
      if((currMbHor >> 3) >= temp)
548
         begin
549
            currMbHor <= currMbHor - (temp << 3);
550
            currMbVer <= currMbVer + 8;
551
         end
552
      else
553
         begin
554
            currMbHor <= currMbHor - temp;
555
            currMbVer <= currMbVer + 1;
556
         end
557
   endrule
558
 
559
 
560
   rule initialize ( process==Initialize && currMbHor
561
      $display( "TRACE Deblocking Filter: initialize %0d", currMb);
562
      process <= Horizontal;
563
      dataReqCount <= 1;
564
      dataRespCount <= 1;
565
      filterTopMbEdgeFlag <= !(currMb
566
      filterLeftMbEdgeFlag <= !(currMbHor==0 || disable_deblocking_filter_idc==1 || (disable_deblocking_filter_idc==2 && currMb==firstMb));
567
      filterInternalEdgesFlag <= !(disable_deblocking_filter_idc==1);
568
      blockNum <= 0;
569
      pixelNum <= 0;
570
      topVectorValidBits <= 0;
571
   endrule
572
 
573
   rule dataSendReq ( dataReqCount>0 && currMbHor
574
      $display( "TRACE Deblocking Filter: dataSendReq %0d", dataReqCount);
575
      Bit#(PicWidthSz) temp = truncate(currMbHor);
576
      if(currMb
577
         dataReqCount <= 0;
578
      else
579
         begin
580
            if(dataReqCount==1)
581
               parameterMemReqQ.enq(LoadReq (temp));
582
            Bit#(4) temp2 = truncate(dataReqCount-1);
583
            let temp3 = {temp,pack(chromaFlag)[0],temp2};
584
            memReqDataSendReq.enq(LoadReq (temp3));
585
            if(dataReqCount==16)
586
               dataReqCount <= 0;
587
            else
588
               dataReqCount <= dataReqCount+1;
589
         end
590
   endrule
591
 
592
 
593
   rule dataReceiveNoResp ( dataRespCount>0 && currMb
594
      $display( "TRACE Deblocking Filter: dataReceiveNoResp");
595
      dataRespCount <= 0;
596
   endrule
597
 
598
   function Action deque(FIFO#(Bit#(32)) fifo);
599
     return fifo.deq();
600
   endfunction
601
 
602
   // rotate column to row major after applying the horizontal filter
603
   rule rowToColumnConversion;
604
     // Check to see if we're even filtering the top edge
605
     Bit#(2) blockVer = {tpl_1(rowToColumnStoreBlock.first())[3],tpl_1(rowToColumnStoreBlock.first())[1]};
606
     Bit#(2) blockHor = {tpl_1(rowToColumnStoreBlock.first())[2],tpl_1(rowToColumnStoreBlock.first())[0]};
607
     Bool storeBottomRightBlock = tpl_2(rowToColumnStoreBlock.first()) == 1;
608
 
609
     rowToColumnState  <= rowToColumnState + 1;
610
     Bit#(32) data_out = 0;
611
     Bit#(PicWidthSz) adjustedMbHor = ((currMbHor==0) ? (picWidth-1) : truncate(currMbHor-1));
612
 
613
     case(rowToColumnState)
614
       2'b00: data_out = {(rowToColumnStore[3].first())[7:0], (rowToColumnStore[2].first())[7:0],
615
                          (rowToColumnStore[1].first())[7:0], (rowToColumnStore[0].first())[7:0]};
616
 
617
       2'b01: data_out = {(rowToColumnStore[3].first())[15:8], (rowToColumnStore[2].first())[15:8],
618
                          (rowToColumnStore[1].first())[15:8], (rowToColumnStore[0].first())[15:8]};
619
 
620
       2'b10: data_out = {(rowToColumnStore[3].first())[23:16], (rowToColumnStore[2].first())[23:16],
621
                          (rowToColumnStore[1].first())[23:16], (rowToColumnStore[0].first())[23:16]};
622
 
623
       2'b11: begin
624
                data_out = {(rowToColumnStore[3].first())[31:24], (rowToColumnStore[2].first())[31:24],
625
                            (rowToColumnStore[1].first())[31:24], (rowToColumnStore[0].first())[31:24]};
626
                mapM_(deque, rowToColumnStore); // Deq the vector elements
627
                rowToColumnStoreBlock.deq();
628
             end
629
       endcase
630
 
631
     if(storeBottomRightBlock) // The right bottom block is not complete until the top filtering has occured
632
                               // It has to be rotated to the column major ordering used in the top vector
633
                               // memory
634
       begin
635
          $display( "TRACE Deblocking Filter: rowToColumnRotate rotating block (%0d, %0d) rowtoColumnState: %d bottomRightBlock: %d, data: %h", blockHor, blockVer, rowToColumnState, storeBottomRightBlock, data_out);
636
         // The block hor calculation may be questionable... between U and V.
637
         if(chromaFlag == Luma)
638
           begin
639
             memReqRowToColumnConversion.enq(StoreReq {addr:{adjustedMbHor,pack(chromaFlag)[0],2'b11,rowToColumnState},data:data_out});
640
           end
641
         else
642
           begin  //differentiate between u and v
643
             memReqRowToColumnConversion.enq(StoreReq {addr:{adjustedMbHor,pack(chromaFlag)[0],blockHor[1],1'b1,rowToColumnState},data:data_out});
644
           end
645
 
646
       end
647
     else // pass data along to vertical filter
648
       begin
649
         verticalFilterBlock.enq(tuple2(tpl_1(rowToColumnStoreBlock.first()),data_out));
650
 
651
         $display( "TRACE Deblocking Filter: rowToColumnRotate rotating block (%0d, %0d) rowtoColumnState: %d bottomRightBlock: %d, data: %h", blockHor, blockVer, rowToColumnState, storeBottomRightBlock, data_out);
652
       end
653
   endrule
654
 
655
   // rotate row to column after applying the vertical filter
656
   rule columnToRowConversion;
657
     Bit#(32) data_out = 0;
658
     Bool topValues = tpl_2(columnToRowStoreBlock.first()) == 1;
659
     Bit#(4) blockNumCols = tpl_1(columnToRowStoreBlock.first());
660
     Bit#(2) blockHor = {blockNumCols[2],blockNumCols[0]};
661
     Bit#(2) blockVer = {blockNumCols[3],blockNumCols[1]} - 1; // Subtract 1, because these output values lag slightly
662
     columnToRowState  <= columnToRowState + 1;
663
 
664
     case(columnToRowState)  // not to sure about this ordering
665
       2'b00: data_out = {(columnToRowStore[3].first())[7:0],
666
                          (columnToRowStore[2].first())[7:0],
667
                          (columnToRowStore[1].first())[7:0],
668
                          (columnToRowStore[0].first())[7:0]};
669
 
670
       2'b01: data_out = {(columnToRowStore[3].first())[15:8],
671
                          (columnToRowStore[2].first())[15:8],
672
                          (columnToRowStore[1].first())[15:8],
673
                          (columnToRowStore[0].first())[15:8]};
674
       2'b10: data_out = {(columnToRowStore[3].first())[23:16],
675
                          (columnToRowStore[2].first())[23:16],
676
                          (columnToRowStore[1].first())[23:16],
677
                          (columnToRowStore[0].first())[23:16]};
678
       2'b11: begin
679
                data_out = {(columnToRowStore[3].first())[31:24],
680
                            (columnToRowStore[2].first())[31:24],
681
                            (columnToRowStore[1].first())[31:24],
682
                            (columnToRowStore[0].first())[31:24]};
683
                mapM_(deque, columnToRowStore); // Deq the vector elements
684
                columnToRowStoreBlock.deq();
685
              end
686
     endcase
687
     $write( "TRACE Deblocking Filter: columnToRow rotate block(%0d, %0d) columnToRowState %d, topValues: %d, data: %h", blockHor, blockVer, columnToRowState, topValues, data_out);
688
 
689
     Bit#(PicWidthSz) currMbHorT = truncate(currMbHor);
690
     // Actually send the data out. This stuff is not the bottom row or left column, and is therefore done.
691
     // THe bottom row was sent out to the temporary buffer in the vertical rule.  But if we're on the last row of
692
     // the frame, there coming here.  Also, if we're in the last block, we must output the leftvector values
693
     if( !topValues && (!(blockHor==3 || (blockHor[0]==1 && chromaFlag==Chroma)) || (currMbVer==picHeight-1)))
694
       begin
695
         $display( " Normal");
696
         if(chromaFlag==Luma)
697
           begin
698
             $display("TRACE mkDeblockFilter: Outputting Luma ver{mbVer, blockVer(2), state}: %h, hor{mbHor, blockHor(2)}: %b, data: %h", {currMbVer,blockVer}, {currMbHorT,blockHor}, data_out);
699
             outfifo.enq(DFBLuma {ver:{currMbVer,blockVer,columnToRowState},
700
                                  hor:{currMbHorT,blockHor},
701
                                  data:data_out});
702
           end
703
         else
704
           begin
705
 $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);
706
             outfifo.enq(DFBChroma {uv:blockHor[1],
707
                                    ver:{currMbVer,blockVer[0],columnToRowState},
708
                                    hor:{currMbHorT,blockHor[0]},
709
                                    data:data_out});
710
           end
711
       end
712
 
713
     if(topValues)// These are the previous top values, and they must be sent out.  Note, that since this is a past
714
                       // Mb, we must adjust the the Mbs used.
715
       begin
716
         $display( " TopValues");
717
         if(chromaFlag==Luma)
718
           begin
719
             $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);
720
             outfifo.enq(DFBLuma {ver:{currMbVer-1,2'b11,columnToRowState},
721
                                  hor:{currMbHorT,blockHor},
722
                                  data:data_out});
723
           end
724
         else
725
           begin
726
             $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);
727
             outfifo.enq(DFBChroma {uv:blockHor[1],
728
                                    ver:{currMbVer-1,1'b1,columnToRowState},
729
                                    hor:{currMbHorT,blockHor[0]},
730
                                    data:data_out});
731
           end
732
       end
733
 
734
     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.
735
          // It may be wise at some point to
736
       begin
737
         // We need to check for the last point in the pipeline. This is the bottom right corner of the Mb.
738
         $display( " Left Vector");
739
         if(chromaFlag==Luma)
740
           begin
741
             if((blockVer == 3) && (columnToRowState == 3))
742
               begin
743
                 process <= Cleanup;
744
               end
745
             //check for last macro block
746
             leftVector.upd({1'b0,blockVer,columnToRowState}, data_out);
747
           end
748
         else
749
           begin
750
             // Only cleanup a single time after the chroma blocks
751
             if((blockHor == 3) && (blockVer[0] == 1) && (columnToRowState == 3))
752
               begin
753
                 process <= Cleanup;
754
               end
755
             leftVector.upd({1'b1,blockHor[1],blockVer[0],columnToRowState}, data_out);
756
           end
757
         end
758
 
759
   endrule
760
 
761
 
762
   rule dataReceiveResp ( dataRespCount>0 && !(currMb
763
      $display( "TRACE Deblocking Filter: dataReceiveResp %0d", dataRespCount);
764
      Bit#(4) temp = truncate(dataRespCount-1);
765
      if(dataRespCount==1)
766
         begin
767
            Bit#(13) tempParameters=0;
768
            if(parameterMemRespQ.first() matches tagged LoadResp .xdata)
769
               tempParameters = xdata;
770
            top_qpy <= tempParameters[5:0];
771
            top_qpc <= tempParameters[11:6];
772
            top_intra <= tempParameters[12];
773
            parameterMemRespQ.deq();
774
         end
775
      if(dataRespCount==16)
776
         dataRespCount <= 0;
777
      else
778
         dataRespCount <= dataRespCount+1;
779
      if(dataMemRespQ.first() matches tagged LoadResp .xdata)
780
        begin
781
          topVectorValidBits[temp] <= 1;
782
          topVector.upd(temp, xdata);
783
        end
784
      dataMemRespQ.deq();
785
      //$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]);
786
   endrule
787
 
788
 
789
   rule horizontal ( process==Horizontal && currMbHor
790
      Bit#(2) blockHor = {blockNum[2],blockNum[0]};
791
      Bit#(2) blockVer = {blockNum[3],blockNum[1]};
792
      Bit#(2) pixelVer = pixelNum;
793
 
794
      Bool leftEdge = (blockNum[0]==0 && (blockNum[2]==0 || chromaFlag==Chroma));
795
      if(blockNum==0 && pixelNum==0)
796
         begin
797
            Bit#(6) qpav = (chromaFlag==Luma ? curr_qpy : curr_qpc);
798
            Bit#(8) indexAtemp = zeroExtend(qpav)+signExtend(slice_alpha_c0_offset);
799
            Bit#(8) indexBtemp = zeroExtend(qpav)+signExtend(slice_beta_offset);
800
            Bit#(6) indexA = (indexAtemp[7]==1 ? 0 : (indexAtemp[6:0]>51 ? 51 : indexAtemp[5:0]));
801
            Bit#(6) indexB = (indexBtemp[7]==1 ? 0 : (indexBtemp[6:0]>51 ? 51 : indexBtemp[5:0]));
802
            alphaInternal <= alpha_table[indexA];
803
            betaInternal <= beta_table[indexB];
804
            Vector#(3,Bit#(5)) tc0temp = arrayToVector(tc0_table[indexA]);
805
            tc0Internal <= tc0temp;
806
         end
807
      case (infifo.first()) matches
808
         tagged PBbS .xdata :
809
            begin
810
               infifo.deq();
811 87 jamey.hick
               bSfileHor.upd(xdata.blockNum, xdata.bShor);
812
               bSfileVer.upd(xdata.blockNum, xdata.bSver);
813
               $display( "TRACE Deblocking Filter(%s): horizontal bsFIFO data: %d, subblock(%0d, %0d) row: %0d, ",csStr,infifo.first(), blockHor, blockVer, pixelNum);
814 91 jamey.hick
 
815 86 jamey.hick
            end
816
         tagged PBoutput .predOutput :
817
            begin
818
               match {.predChromaFlag, .xdata} = predOutput;
819
               if(predChromaFlag != chromaFlag)
820
                  begin
821 91 jamey.hick
                    $display("PARDEBLOCK deblockingFilter(%s) ERROR chroma flag mismatch in deblocking filter",csStr);
822 86 jamey.hick
                  end
823
               Bit#(PicWidthSz) currMbHorT = truncate(currMbHor);
824
               if((chromaFlag == Chroma) && (blockHor[1] == 1))
825
                 begin
826
                   $display("PRE %h %h %h", {currMbVer,blockVer[0],pixelVer},{currMbHorT,blockHor[0]}, {xdata[0],xdata[1],xdata[2],xdata[3]});
827
                 end
828
               $display( "TRACE Deblocking Filter: horizontal chroma: %d, subblock(%0d, %0d) row: %0d, data: %h", chromaFlag, blockHor, blockVer, pixelNum, xdata);
829
               infifo.deq();
830
               Bit#(6) addrq = {blockHor,blockVer,pixelVer};
831
               Bit#(5) addrpLeft = (chromaFlag==Luma ? {1'b0,blockVer,pixelVer} : {1'b1,blockHor[1],blockVer[0],pixelVer});
832
               Bit#(6) addrpCurr = {(blockHor-1),blockVer,pixelVer};
833
               Bit#(32) pixelq = {xdata[3],xdata[2],xdata[1],xdata[0]};
834
               Bit#(32) pixelp;
835
               if(leftEdge)
836
                 begin
837
                   pixelp <- leftVector.sub(addrpLeft);
838
                   $display( "TRACE Deblocking Filter: horizontal P (left) addr %h, data %h ",addrpLeft, pixelp);
839
                 end
840
               else
841
                 begin
842
                   pixelp <- workVectorRows.sub({blockVer[0], pixelVer});
843
                   $display( "TRACE Deblocking Filter: horizontal P (work) addr %h, data %h ",addrpCurr, pixelp);
844
                 end
845
               Bit#(64) result = {pixelq,pixelp};
846
               if(leftEdge && filterLeftMbEdgeFlag)
847
                  begin
848
                    Bit#(6) curr_qp = (chromaFlag==Luma ? curr_qpy : curr_qpc);
849
                    Bit#(6) left_qp = (chromaFlag==Luma ? left_qpy : left_qpc);
850
                    Bit#(7) qpavtemp = zeroExtend(curr_qp)+zeroExtend(left_qp)+1;
851
                    Bit#(6) qpav = qpavtemp[6:1];
852
                    Bit#(8) indexAtemp = zeroExtend(qpav)+signExtend(slice_alpha_c0_offset);
853
                    Bit#(8) indexBtemp = zeroExtend(qpav)+signExtend(slice_beta_offset);
854
                    Bit#(6) indexA = (indexAtemp[7]==1 ? 0 : (indexAtemp[6:0]>51 ? 51 : indexAtemp[5:0]));
855
                    Bit#(6) indexB = (indexBtemp[7]==1 ? 0 : (indexBtemp[6:0]>51 ? 51 : indexBtemp[5:0]));
856
                    Bit#(8) alphaMbLeft = alpha_table[indexA];
857
                    Bit#(5) betaMbLeft = beta_table[indexB];
858
                    Vector#(3,Bit#(5)) tc0MbLeft = arrayToVector(tc0_table[indexA]);
859
                    if(filter_test({pixelq[15:0],pixelp[31:16]},alphaMbLeft,betaMbLeft))
860
                      begin
861
                         $display("TRACE mkDeblockFilter: Applying horizontal, left filter");
862
                         Bit#(3) bsData <- bSfileHor.sub((chromaFlag==Luma?blockNum:{blockNum[1:0],pixelVer[1],1'b0}));
863
                         result = filter_input({pixelq,pixelp},chromaFlag==Chroma,bsData,alphaMbLeft,betaMbLeft,tc0MbLeft);
864
                       end
865
                  end
866
               else if(!leftEdge && filterInternalEdgesFlag)
867
                  begin
868
                     if(filter_test({pixelq[15:0],pixelp[31:16]},alphaInternal,betaInternal))
869
                       begin
870
                         $display("TRACE mkDeblockFilter: Applying horizontal, internal filter");
871
                         Bit#(3) bSData <- bSfileHor.sub((chromaFlag==Luma?blockNum:{blockNum[1:0],pixelVer[1],1'b0}));
872
                         result = filter_input({pixelq,pixelp},chromaFlag==Chroma,bSData,alphaInternal,betaInternal,tc0Internal);
873
                       end
874
                  end
875
 
876
 
877
               if(leftEdge)
878
                 begin
879
                   // write out the left edge
880
                   //Check to store this value to the memory.  I think the rotation is off here.
881
                   // I should also adjust the vertical Mb...  Figure out MbHorT
882
 
883
                   Bit#(PicHeightSz) adjustedMbVer = ((currMbHorT==0) && (currMbVer!=0)) ? currMbVer-1 : currMbVer;
884
                   Bit#(PicWidthSz)  adjustedMbHor = currMbHorT==0 ? picWidth-1 : currMbHorT-1;
885
                   // In this case we buffer the bottom vertical element, since it has to be used again
886
                   if(((blockVer == 3) || ((chromaFlag == Chroma) && (blockVer == 1))) && (adjustedMbVer != picHeight - 1))
887
                     begin
888
                       rowToColumnStore[pixelNum[1:0]].enq(result[31:0]);
889
                       // only push in a command for the bottom leftblock.  It has to be rotated.
890
                       if(pixelNum == 3)
891
                         begin
892
                           rowToColumnStoreBlock.enq(tuple2(blockNum,1));
893
                         end
894
                    end
895
                   // these outputs occur in the past, so we must use the adjusted Mb numbers
896
                   else if(chromaFlag==Luma)
897
                     begin
898
                       $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] );
899
                       outfifoVertical.enq(DFBLuma {ver:{adjustedMbVer,blockVer,pixelVer},
900
                                            hor:{adjustedMbHor,2'b11},
901
                                            data:result[31:0]});
902
                     end
903
                   else
904
                     begin
905
                       $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]);
906
                       outfifoVertical.enq(DFBChroma {uv:blockHor[1],
907
                                              ver:{adjustedMbVer,blockVer[0],pixelVer},
908
                                              hor:{adjustedMbHor,1'b1},
909
                                              data:result[31:0]});
910
                      end
911
                 end
912
               else
913
                  begin
914
                    // push the correction into reorder block;
915
                    rowToColumnStore[addrpCurr[1:0]].enq(result[31:0]);
916
                    // Push down the block number and the chroma flag into the pipeline
917
                    if(pixelNum == 3)
918
                      begin
919
                        let blockHorPast = blockHor - 1;
920
                        let blockNumPast = {blockVer[1], blockHorPast[1], blockVer[0], blockHorPast[0]};
921
                        rowToColumnStoreBlock.enq(tuple2(blockNumPast,0));
922
                      end
923
                  end
924
               $display( "TRACE Deblocking Filter: horizontal Q (work) addr %h, data %h, original data: %h ",addrq, result[63:32], pixelq);
925
               workVectorRows.upd({blockVer[0],pixelVer}, result[63:32]);
926
 
927
               // Step out to clean up the edge block
928
               // What about the chroma?
929
               if((pixelNum == 3) && ((blockHor == 3) || ((chromaFlag == Chroma) && (blockHor == 1))))
930
                 begin
931
                    $display( "TRACE Deblocking Filter: Heading to Horizontal Cleanup");
932
                   process <= HorizontalCleanup;// we enter this state to push out the remaining
933
                                                  // blocks, that haven't been shoved out.  Namely, the
934
                                                  // left blocks.
935
                 end
936
               else if(pixelNum==3)
937
                 begin
938 87 jamey.hick
                   $display( "TRACE Deblocking Filter(%s): horizontal bsFIFO completed subblock(%0d, %0d)",csStr, blockHor, blockVer);
939 86 jamey.hick
                   blockNum <= blockNum+1;
940
                 end
941
               pixelNum <= pixelNum+1;
942
            end
943 91 jamey.hick
         default: begin
944
                     $display( "ERROR Deblocking Filter(%s): horizontal non-PBoutput input at sublock (%0d,%0d)", csStr,blockHor,blockVer);
945
                     case(infifo.first()) matches
946
                       tagged NewUnit . xdata : $display("Caused by NewUnit");
947
                       tagged SPSpic_width_in_mbs .xdata : $display("Caused by SPSpic_width_in_mbs");
948
                       tagged SPSpic_height_in_map_units .xdata : $display("Caused by SPSpic_height_in_map_units");
949
                       tagged PPSdeblocking_filter_control_present_flag .xdata : $display("Caused by PPSdeblocking_filter_control_present_flag");
950
                       tagged SHfirst_mb_in_slice .xdata : $display("Caused by SHfirst_mb_in_slice");
951
                       tagged SHdisable_deblocking_filter_idc .xdata : $display("Caused by SHdisable_deblocking_filter_idc");
952
                       tagged SHslice_alpha_c0_offset .xdata : $display("Caused by SHslice_alpha_c0_offset");
953
                       tagged SHslice_beta_offset .xdata : $display("Caused by SHslice_beta_offset");
954
                       tagged IBTmb_qp .xdata : $display("Caused by IBTmb_qp");
955
                       tagged EndOfFile : $display("Caused by EndOfFile");
956
                       default:
957
                         $display("Caused by Unknown");
958
                     endcase
959
                  end
960 86 jamey.hick
      endcase
961
   endrule
962
 
963
  rule horizontal_cleanup(process == HorizontalCleanup);
964
    Bit#(2) blockHor = {blockNum[2],blockNum[0]};
965
    Bit#(2) blockVer = {blockNum[3],blockNum[1]};
966
    $display( "TRACE Deblocking Filter: horizontal_cleanup (%0d, %0d) row: %d", blockHor, blockVer, pixelNum);
967
    if(pixelNum==3 && (blockNum==15 || (blockNum==7 && chromaFlag==Chroma)))
968
      begin
969
        if(blockNum == 15)
970
          begin
971
            $display( "TRACE Deblocking Filter: horizontal completed Mb (%0d) Luma", currMb);
972
          end
973
        else
974
          begin
975
            $display( "TRACE Deblocking Filter: horizontal completed Mb (%0d) Chroma", currMb);
976
          end
977
        blockNum <= 0;
978
        process <= Vertical;// we enter this state to wait for the vertical processing to complete
979
        rowToColumnStoreBlock.enq(tuple2(blockNum,0));
980
      end
981
    else if(pixelNum == 3)
982
      begin
983
        blockNum <= blockNum + 1;
984
        process <= Horizontal; // not done with this Mb yet.
985
        rowToColumnStoreBlock.enq(tuple2(blockNum,0));
986
      end
987
    pixelNum <= pixelNum + 1;
988
    // push the correction into reorder block;
989
    Bit#(32) work_data <- workVectorRows.sub({blockVer[0], pixelNum});
990
    rowToColumnStore[pixelNum].enq(work_data);
991
  endrule
992
 
993
 
994
  // declare these to share the rule
995
  begin
996
   Bit#(4) blockNumCols = tpl_1(verticalFilterBlock.first());
997
   Bit#(2) blockVer = {blockNumCols[3],blockNumCols[1]};
998
   Bit#(2) blockHor = {blockNumCols[2],blockNumCols[0]};
999
   Bool topEdge = (blockVer==0);
1000
 
1001
 
1002
  rule vertical_filter_halt((verticalState == NormalOperation) && !((!topEdge) || (topVectorValidBits[{blockHor,columnNumber}] == 1) || (currMb
1003
        if(process == Vertical || process == Horizontal)
1004
          begin
1005
            $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);
1006
          end
1007
 
1008
  endrule
1009
 
1010
 
1011
  // As with horizontal, the q data will be read from the data store, and the p data will be streamed in via the
1012
  // reordering FIFO.  The new p data must be stored, but the q data will need to be spooled out, since it needs to
1013
  // make it to the left vector.
1014
  rule vertical((verticalState == NormalOperation) && ((!topEdge) || (topVectorValidBits[{blockHor,columnNumber}] == 1) || (currMb
1015
    //$display( "TRACE Deblocking Filter: vertical %0d %0d", colNum, rowNum);
1016
    //$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]);
1017
    //Process the block according to what got passed to us.
1018
    Bit#(32) workV = tpl_2(verticalFilterBlock.first());
1019
    Bit#(32) tempV = 0;
1020
    Bit#(64) resultV = 0;
1021
    Bit#(8) alpha;
1022
    Bit#(5) beta;
1023
    Vector#(3,Bit#(5)) tc0;
1024
 
1025
 
1026
      $display( "TRACE Deblocking Filter: vertical subblock (%0d, %0d), column: %d, data: %h", blockHor, blockVer, columnNumber, workV);
1027
      columnNumber <= columnNumber + 1;
1028
      verticalFilterBlock.deq();
1029
      if(topEdge)
1030
         begin
1031
            Bit#(6) curr_qp = (chromaFlag==Luma ? curr_qpy : curr_qpc);
1032
            Bit#(6) top_qp = (chromaFlag==Luma ? top_qpy : top_qpc);
1033
            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 <- topVector.sub({blockHor,columnNumber});
1043
            $display( "TRACE Deblocking Filter: vertical P (top) addr %h, orig data %h ",{blockVer,columnNumber}, tempV);
1044
            alpha = alphaMbTop;
1045
            beta = betaMbTop;
1046
            tc0 = tc0MbTop;
1047
         end
1048
      else
1049
         begin
1050
            // We read this value from the original vector
1051
            tempV <- topVector.sub({blockHor, columnNumber});
1052
            $display( "TRACE Deblocking Filter: vertical P (work) addr %h, orig data %h ",{blockHor, blockVer - 1, columnNumber}, tempV);
1053
            alpha = alphaInternal;
1054
            beta = betaInternal;
1055
            tc0 = tc0Internal;
1056
         end
1057
 
1058
      // Marshalling data in things upon which the filter blocks can be applied
1059
 
1060
      resultV = {tpl_2(verticalFilterBlock.first()),tempV};
1061
 
1062
      // Apply filter, only if filter test passes, and we are either filtering the top edge, or we aren't on the top edge
1063
      $display( "TRACE Deblocking Filter: vertical Filter test: P1P0Q0Q1: %h",{workV[15:8],workV[7:0],tempV[31:24],tempV[23:16]});
1064
      if((filter_test({workV[15:8],workV[7:0],tempV[31:24],tempV[23:16]},alpha,beta)) && ((topEdge && filterTopMbEdgeFlag)|| (!topEdge && filterInternalEdgesFlag) ))
1065
        begin
1066
          $display("TRACE mkDeblockFilter: Applying vertical filter");
1067
          Bit#(3) bsData <- bSfileVer.sub((chromaFlag==Luma?blockNumCols:{blockVer[0],blockHor[0],1'b0,columnNumber[1]}));
1068
          resultV = filter_input(resultV,chromaFlag==Chroma,bsData,alpha,beta,tc0);
1069
        end
1070
      //Write out the result data  31:0 are the done q values
1071
      if(topEdge)
1072
         begin
1073
            // We really need to just output these values -> need to shove them to the rotation unit, but only if the
1074
            // current Mb vertical component is larger than 0.  All of these are done and can be dumped out
1075
            if(currMbVer > 0)
1076
              begin
1077
                if(columnNumber == 3)
1078
                  begin
1079
                    columnToRowStoreBlock.enq(tuple2(blockNumCols,1'b1));
1080
                  end
1081
                columnToRowStore[columnNumber].enq(resultV[31:0]);
1082
              end
1083
         end
1084
      else
1085
         begin
1086
            // We should make the decision as to whether to store these values. We will store the bottom row, except
1087
            // for the left most block which will be stored the next time that an Mb gets processed.
1088
            // The values to store are in the P vector... except the bottom right block, which is different.
1089
            Bit#(PicWidthSz) currMbHorT = truncate(currMbHor);
1090
 
1091
            if(((blockVer == 3) && (blockHor == 3)) || ((chromaFlag == Chroma) && (blockVer == 1) && (blockHor[0] == 1)))
1092
              begin
1093
                // need to enter escape state to write the bottom left block to the leftVector.
1094
                if(columnNumber == 3)
1095
                  begin
1096
                    blockHorVerticalCleanup <= blockHor;
1097
                    verticalState <= VerticalCleanup;
1098
                  end
1099
              end
1100
            else if((blockVer == 3) || ((chromaFlag == Chroma) && (blockVer == 1)))
1101
              begin
1102
                if((currMbVer == picHeight - 1) && (columnNumber == 3)) // If we're at the bottom of the frame, we'd
1103
                                                                        // roll through the block clean up.
1104
                  begin
1105
                    blockHorVerticalCleanup <= blockHor;
1106
                    verticalState <= VerticalCleanup;
1107
                  end
1108
                memReqVertical.enq(StoreReq {addr:{currMbHorT,pack(chromaFlag)[0],blockHor,columnNumber},data:resultV[63:32]});
1109
              end
1110
            columnToRowStore[columnNumber].enq(resultV[31:0]);
1111
            if(columnNumber == 0)
1112
              begin
1113
                columnToRowStoreBlock.enq(tuple2(blockNumCols,1'b0));
1114
              end
1115
         end
1116
 
1117
        $display( "TRACE Deblocking Filter: vertical P                 data %h                     ",  resultV[31:0]);
1118
        $display( "TRACE Deblocking Filter: vertical Q (work) addr %h, data %h, original data: %h  ",{blockHor,blockVer,columnNumber}, resultV[63:32], workV);
1119
 
1120
        topVector.upd({blockHor,columnNumber}, resultV[63:32]);
1121
  endrule
1122
end
1123
 
1124
  rule vertical_cleanup(verticalState == VerticalCleanup);
1125
    $display( "TRACE Deblocking Filter: vertical_cleanup at block end column: %d ", columnNumber);
1126
    columnNumber <= columnNumber + 1;
1127
    if(columnNumber == 3)
1128
      begin
1129
        verticalState <= NormalOperation;
1130
      end
1131
    if(chromaFlag == Luma)
1132
      begin
1133
        Bit#(2) blockHor = blockHorVerticalCleanup;
1134
        Bit#(2) blockVer = 0;
1135
        if(columnNumber == 3)
1136
          begin
1137
            // Horizontal Postion is 3, but vertical position is 0, owing to subtraction in the rotation unit
1138
            columnToRowStoreBlock.enq(tuple2({blockVer[1],blockHor[1],blockVer[0],blockHor[0]},1'b0));
1139
          end
1140
        Bit#(32) w_data <- topVector.sub({blockHor, columnNumber});
1141
        columnToRowStore[columnNumber].enq(w_data);
1142
     end
1143
   else
1144
     begin
1145
       Bit#(2) blockHor = blockHorVerticalCleanup;
1146
       Bit#(2) blockVer = 2; // need to make this two for subtraction in rotation unit
1147
       if(columnNumber == 3)
1148
         begin
1149
           // Horizontal Postion is 3, but vertical position is 0, owing to subtraction in the rotation unit
1150
           columnToRowStoreBlock.enq(tuple2({blockVer[1],blockHor[1],blockVer[0],blockHor[0]},1'b0));
1151
         end
1152
       Bit#(32) w_data <- topVector.sub({blockHor, columnNumber});
1153
       columnToRowStore[columnNumber].enq(w_data);
1154
     end
1155
  endrule
1156
 
1157
 
1158
  rule cleanup ( process==Cleanup && currMbHor
1159
    $display( "TRACE Deblocking Filter: cleanup %0d", currMb);
1160
    Bit#(PicWidthSz) currMbHorT = truncate(currMbHor);
1161
    if(chromaFlag==Luma)
1162
      begin
1163 87 jamey.hick
        $display( "TRACE Deblocking Filter(%s): horizontal bsFIFO luma completed",csStr);
1164 86 jamey.hick
      end
1165
    else
1166
      begin
1167 87 jamey.hick
         $display( "TRACE Deblocking Filter(%s): horizontal bsFIFO chroma completed",csStr);
1168
      end
1169 86 jamey.hick
        process <= Passing;
1170
        Bit#(PicWidthSz) temp = truncate(currMbHor);
1171 87 jamey.hick
        parameterMemReqQ.enq(StoreReq {addr:temp,data:{curr_intra,curr_qpc,curr_qpy}}); // this may need to change....
1172 86 jamey.hick
        left_intra <= curr_intra;
1173
        left_qpc <= curr_qpc;
1174
        left_qpy <= curr_qpy;
1175
        currMb <= currMb+1;
1176
        currMbHor <= currMbHor+1;
1177
        if(currMbVer==picHeight-1 && currMbHor==zeroExtend(picWidth-1))
1178
          begin
1179
            outfifo.enq(EndOfFrame);
1180 87 jamey.hick
          end
1181 86 jamey.hick
   endrule
1182
 
1183
   interface Client mem_client_data;
1184
      interface Get request  = fifoToGet(dataMemReqQ);
1185
      interface Put response = fifoToPut(dataMemRespQ);
1186
   endinterface
1187
 
1188
   interface Client mem_client_parameter;
1189
      interface Get request  = fifoToGet(parameterMemReqQ);
1190
      interface Put response = fifoToPut(parameterMemRespQ);
1191
   endinterface
1192
 
1193
   interface Put ioin  = fifoToPut(fifofToFifo(infifo));
1194
   interface Get ioout = fifoToGet(outfifo);
1195
 
1196
endmodule
1197
 
1198
endpackage

powered by: WebSVN 2.1.0

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