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

Subversion Repositories bluespec-h264

[/] [bluespec-h264/] [trunk/] [release/] [mkDeblockFilter_orig.bsv] - Blame information for rev 84

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

Line No. Rev Author Line
1 84 jamey.hick
//**********************************************************************
2
// Deblocking Filter
3
//----------------------------------------------------------------------
4
//
5
//
6
 
7
package mkDeblockFilter;
8
 
9
import H264Types::*;
10
 
11
import IDeblockFilter::*;
12
import FIFO::*;
13
import Vector::*;
14
 
15
import Connectable::*;
16
import GetPut::*;
17
import ClientServer::*;
18
 
19
 
20
 
21
 
22
//-----------------------------------------------------------
23
// Local Datatypes
24
//-----------------------------------------------------------
25
 
26
 
27
typedef union tagged
28
{
29
 void     Passing;          //not working on anything in particular
30
 void     Initialize;
31
 void     Horizontal;
32
 void     Vertical;
33
 void     Cleanup;
34
}
35
Process deriving(Eq,Bits);
36
 
37
 
38
 
39
//-----------------------------------------------------------
40
// Helper functions
41
 
42
 
43
function Bit#(8) absdiff8(Bit#(8) in0, Bit#(8) in1);
44
   return (in1>=in0 ? in1-in0 : in0-in1);
45
endfunction
46
 
47
 
48
function Bool filter_test(Bit#(32) in_pixels, Bit#(8) alpha, Bit#(5) beta);
49
   Bit#(8) p1 = in_pixels[7:0];
50
   Bit#(8) p0 = in_pixels[15:8];
51
   Bit#(8) q0 = in_pixels[23:16];
52
   Bit#(8) q1 = in_pixels[31:24];
53
   return((absdiff8(p0,q0) < alpha) &&
54
          (absdiff8(p0,p1) < zeroExtend(beta))  &&
55
          (absdiff8(q0,q1) < zeroExtend(beta)));
56
endfunction
57
 
58
 
59
function Bit#(6) clip3symmetric9to6(Bit#(9) val, Bit#(5) bound);
60
   Int#(9) intval = unpack(val);
61
   Int#(6) intbound = unpack({1'b0,bound});
62
   Int#(6) intout = (intvalsignExtend(intbound) ? intbound : truncate(intval)));
63
   return pack(intout);
64
endfunction
65
 
66
 
67
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);
68
   Bit#(8) p[4];
69
   Bit#(8) q[4];
70
   p[3] = in_pixels[7:0];
71
   p[2] = in_pixels[15:8];
72
   p[1] = in_pixels[23:16];
73
   p[0] = in_pixels[31:24];
74
   q[0] = in_pixels[39:32];
75
   q[1] = in_pixels[47:40];
76
   q[2] = in_pixels[55:48];
77
   q[3] = in_pixels[63:56];
78
   Bit#(8) p_out[4];
79
   Bit#(8) q_out[4];
80
   Bool a_p_test = absdiff8(p[2],p[0]) < zeroExtend(beta);
81
   Bool a_q_test = absdiff8(q[2],q[0]) < zeroExtend(beta);
82
   Bit#(9) p0q0 = zeroExtend(p[0])+zeroExtend(q[0]);
83
   if (bs == 4)
84
      begin
85
         Bool small_gap_test = absdiff8(p[0],q[0]) < (alpha >> 2)+2;
86
         Bit#(11) p_outtemp[3];
87
         Bit#(11) q_outtemp[3];
88
         if (!chroma_flag && a_p_test && small_gap_test)
89
            begin
90
               Bit#(11) sum = zeroExtend(p[1])+zeroExtend(p0q0);
91
               p_outtemp[0] = (zeroExtend(p[2]) + (sum<<1) + zeroExtend(q[1]) + 4) >> 3;
92
               p_outtemp[1] = (zeroExtend(p[2]) + sum + 2) >> 2;
93
               p_outtemp[2] = (((zeroExtend(p[3])+zeroExtend(p[2]))<<1) + zeroExtend(p[2]) + sum + 4) >> 3;
94
            end
95
         else
96
            begin
97
               p_outtemp[0] = ((zeroExtend(p[1])<<1) + zeroExtend(p[0]) + zeroExtend(q[1]) + 2) >> 2;
98
               p_outtemp[1] = zeroExtend(p[1]);
99
               p_outtemp[2] = zeroExtend(p[2]);
100
            end
101
         if (!chroma_flag && a_q_test && small_gap_test)
102
            begin
103
               Bit#(11) sum = zeroExtend(q[1])+zeroExtend(p0q0);
104
               q_outtemp[0] = (zeroExtend(p[1]) + (sum<<1) + zeroExtend(q[2]) + 4) >> 3;
105
               q_outtemp[1] = (zeroExtend(q[2]) + sum + 2) >> 2;
106
               q_outtemp[2] = (((zeroExtend(q[3])+zeroExtend(q[2]))<<1) + zeroExtend(q[2]) + sum + 4) >> 3;
107
            end
108
         else
109
            begin
110
               q_outtemp[0] = ((zeroExtend(q[1])<<1) + zeroExtend(q[0]) + zeroExtend(p[1]) + 2) >> 2;
111
               q_outtemp[1] = zeroExtend(q[1]);
112
               q_outtemp[2] = zeroExtend(q[2]);
113
            end
114
         p_out[0] = truncate(p_outtemp[0]);
115
         p_out[1] = truncate(p_outtemp[1]);
116
         p_out[2] = truncate(p_outtemp[2]);
117
         q_out[0] = truncate(q_outtemp[0]);
118
         q_out[1] = truncate(q_outtemp[1]);
119
         q_out[2] = truncate(q_outtemp[2]);
120
      end
121
   else if(bs > 0)
122
      begin
123
         Bit#(5) t_c0 = tc0_vector[bs-1];
124
         Bit#(5) t_c = chroma_flag ? t_c0+1 : t_c0 + (a_p_test ? 1:0) + (a_q_test ? 1:0);
125
         Bit#(12) deltatemp = (((zeroExtend(q[0])-zeroExtend(p[0]))<<2)+zeroExtend(p[1])-zeroExtend(q[1])+4);
126
         Bit#(6) delta = clip3symmetric9to6(deltatemp[11:3],t_c);
127
 
128
         Bit#(10) p_out0temp = zeroExtend(p[0]) + signExtend(delta);
129
         p_out[0] = (p_out0temp[9]==1 ? 0 : (p_out0temp[8]==1 ? 255 : p_out0temp[7:0]));
130
         Bit#(10) q_out0temp = zeroExtend(q[0]) - signExtend(delta);
131
         q_out[0] = (q_out0temp[9]==1 ? 0 : (q_out0temp[8]==1 ? 255 : q_out0temp[7:0]));
132
 
133
         Bit#(9) p0q0PLUS1 = p0q0+1;
134
         Bit#(8) p0q0_av = p0q0PLUS1[8:1];
135
         if (!chroma_flag && a_p_test)
136
            begin
137
               Bit#(10) p_out1temp = zeroExtend(p[2]) + zeroExtend(p0q0_av) - (zeroExtend(p[1])<<1);
138
               p_out[1] = p[1]+signExtend(clip3symmetric9to6(p_out1temp[9:1],t_c0));
139
            end
140
         else
141
            p_out[1] = p[1];
142
 
143
         if (!chroma_flag && a_q_test)
144
            begin
145
               Bit#(10) q_out1temp = zeroExtend(q[2]) + zeroExtend(p0q0_av) - (zeroExtend(q[1])<<1);
146
               q_out[1] = q[1]+signExtend(clip3symmetric9to6(q_out1temp[9:1],t_c0));
147
            end
148
         else
149
            q_out[1] = q[1];
150
 
151
         p_out[2] = p[2];
152
         q_out[2] = q[2];
153
      end
154
   else
155
      begin
156
         p_out[0] = p[0];
157
         q_out[0] = q[0];
158
         p_out[1] = p[1];
159
         q_out[1] = q[1];
160
         p_out[2] = p[2];
161
         q_out[2] = q[2];
162
      end
163
   p_out[3] = p[3];
164
   q_out[3] = q[3];
165
   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]});
166
endfunction
167
 
168
 
169
 
170
//-----------------------------------------------------------
171
// Deblocking Filter Module
172
//-----------------------------------------------------------
173
 
174
 
175
(* synthesize *)
176
module mkDeblockFilter( IDeblockFilter );
177
 
178
   FIFO#(EntropyDecOT) infifo     <- mkSizedFIFO(deblockFilter_infifo_size);
179
   FIFO#(DeblockFilterOT) outfifo <- mkFIFO();
180
 
181
   FIFO#(MemReq#(TAdd#(PicWidthSz,5),32)) dataMemReqQ       <- mkFIFO;
182
   FIFO#(MemReq#(PicWidthSz,13))          parameterMemReqQ  <- mkFIFO;
183
   FIFO#(MemResp#(32))                    dataMemRespQ      <- mkFIFO;
184
   FIFO#(MemResp#(13))                    parameterMemRespQ <- mkFIFO;
185
 
186
   Reg#(Process) process       <- mkReg(Passing);
187
   Reg#(Bit#(1)) chromaFlag    <- mkReg(0);
188
   Reg#(Bit#(5)) dataReqCount  <- mkReg(0);
189
   Reg#(Bit#(5)) dataRespCount <- mkReg(0);
190
   Reg#(Bit#(4)) blockNum      <- mkReg(0);
191
   Reg#(Bit#(4)) pixelNum      <- mkReg(0);
192
 
193
   Reg#(Bool) filterTopMbEdgeFlag     <- mkReg(False);
194
   Reg#(Bool) filterLeftMbEdgeFlag    <- mkReg(False);
195
   Reg#(Bool) filterInternalEdgesFlag <- mkReg(False);
196
 
197
   Reg#(Bit#(PicWidthSz))  picWidth  <- mkReg(maxPicWidthInMB);
198
   Reg#(Bit#(PicHeightSz)) picHeight <- mkReg(0);
199
   Reg#(Bit#(PicAreaSz))   firstMb   <- mkReg(0);
200
   Reg#(Bit#(PicAreaSz))   currMb    <- mkReg(0);
201
   Reg#(Bit#(PicAreaSz))   currMbHor <- mkReg(0);//horizontal position of currMb
202
   Reg#(Bit#(PicHeightSz)) currMbVer <- mkReg(0);//vertical position of currMb
203
 
204
   Reg#(Bit#(2)) disable_deblocking_filter_idc <- mkReg(0);
205
   Reg#(Bit#(5)) slice_alpha_c0_offset <- mkReg(0);
206
   Reg#(Bit#(5)) slice_beta_offset <- mkReg(0);
207
 
208
   Reg#(Bit#(6)) curr_qpy   <- mkReg(0);
209
   Reg#(Bit#(6)) left_qpy   <- mkReg(0);
210
   Reg#(Bit#(6)) top_qpy    <- mkReg(0);
211
   Reg#(Bit#(6)) curr_qpc   <- mkReg(0);
212
   Reg#(Bit#(6)) left_qpc   <- mkReg(0);
213
   Reg#(Bit#(6)) top_qpc    <- mkReg(0);
214
   Reg#(Bit#(1)) curr_intra <- mkReg(0);
215
   Reg#(Bit#(1)) left_intra <- mkReg(0);
216
   Reg#(Bit#(1)) top_intra  <- mkReg(0);
217
 
218
   Reg#(Bit#(8)) alphaMbEdge    <- mkReg(0);
219
   Reg#(Bit#(8)) alphaInternal  <- mkReg(0);
220
   Reg#(Bit#(5)) betaMbEdge     <- mkReg(0);
221
   Reg#(Bit#(5)) betaInternal   <- mkReg(0);
222
   Reg#(Vector#(3,Bit#(5))) tc0MbEdge   <- mkRegU();
223
   Reg#(Vector#(3,Bit#(5))) tc0Internal <- mkRegU();
224
 
225
   Bit#(8) alpha_table[52] = {0,  0,  0,  0,  0,  0,  0,  0,  0,  0,
226
                              0,  0,  0,  0,  0,  0,  4,  4,  5,  6,
227
                              7,  8,  9, 10, 12, 13, 15, 17, 20, 22,
228
                             25, 28, 32, 36, 40, 45, 50, 56, 63, 71,
229
                             80, 90,101,113,127,144,162,182,203,226,
230
                            255,255};
231
   Bit#(5) beta_table[52] = {0,  0,  0,  0,  0,  0,  0,  0,  0,  0,
232
                             0,  0,  0,  0,  0,  0,  2,  2,  2,  3,
233
                             3,  3,  3,  4,  4,  4,  6,  6,  7,  7,
234
                             8,  8,  9,  9, 10, 10, 11, 11, 12, 12,
235
                            13, 13, 14, 14, 15, 15, 16, 16, 17, 17,
236
                            18, 18};
237
   Bit#(5) tc0_table[52][3] = {{ 0, 0, 0 }, { 0, 0, 0 }, { 0, 0, 0 }, { 0, 0, 0 }, { 0, 0, 0 }, { 0, 0, 0 },
238
                               { 0, 0, 0 }, { 0, 0, 0 }, { 0, 0, 0 }, { 0, 0, 0 }, { 0, 0, 0 }, { 0, 0, 0 },
239
                               { 0, 0, 0 }, { 0, 0, 0 }, { 0, 0, 0 }, { 0, 0, 0 }, { 0, 0, 0 }, { 0, 0, 1 },
240
                               { 0, 0, 1 }, { 0, 0, 1 }, { 0, 0, 1 }, { 0, 1, 1 }, { 0, 1, 1 }, { 1, 1, 1 },
241
                               { 1, 1, 1 }, { 1, 1, 1 }, { 1, 1, 1 }, { 1, 1, 2 }, { 1, 1, 2 }, { 1, 1, 2 },
242
                               { 1, 1, 2 }, { 1, 2, 3 }, { 1, 2, 3 }, { 2, 2, 3 }, { 2, 2, 4 }, { 2, 3, 4 },
243
                               { 2, 3, 4 }, { 3, 3, 5 }, { 3, 4, 6 }, { 3, 4, 6 }, { 4, 5, 7 }, { 4, 5, 8 },
244
                               { 4, 6, 9 }, { 5, 7,10 }, { 6, 8,11 }, { 6, 8,13 }, { 7,10,14 }, { 8,11,16 },
245
                               { 9,12,18 }, {10,13,20 }, {11,15,23 }, {13,17,25 }};
246
 
247
   Reg#(Vector#(64,Bit#(32))) workVector <- mkRegU();
248
   Reg#(Vector#(96,Bit#(32))) leftVector <- mkRegU();
249
   Reg#(Vector#(16,Bit#(32))) topVector  <- mkRegU();
250
 
251
   Reg#(Bool) startLastOutput <- mkReg(False);
252
   Reg#(Bool) outputingFinished <- mkReg(False);
253
   Reg#(Bit#(2)) colNum <- mkReg(0);
254
   Reg#(Bit#(2)) rowNum <- mkReg(0);
255
 
256
   RFile1#(Bit#(4),Tuple2#(Bit#(3),Bit#(3))) bSfile <- mkRFile1Full();
257
 
258
 
259
   //-----------------------------------------------------------
260
   // Rules
261
 
262
   rule passing ( process matches Passing );
263
      case (infifo.first()) matches
264
         tagged NewUnit . xdata :
265
            begin
266
               infifo.deq();
267
               outfifo.enq(EDOT infifo.first());
268
               $display("ccl5newunit");
269
               $display("ccl5rbspbyte %h", xdata);
270
            end
271
         tagged SPSpic_width_in_mbs .xdata :
272
            begin
273
               infifo.deq();
274
               outfifo.enq(EDOT infifo.first());
275
               picWidth <= xdata;
276
            end
277
         tagged SPSpic_height_in_map_units .xdata :
278
            begin
279
               infifo.deq();
280
               outfifo.enq(EDOT infifo.first());
281
               picHeight <= xdata;
282
            end
283
         tagged PPSdeblocking_filter_control_present_flag .xdata :
284
            begin
285
               infifo.deq();
286
               if (xdata == 0)
287
                  begin
288
                     disable_deblocking_filter_idc <= 0;
289
                     slice_alpha_c0_offset <= 0;
290
                     slice_beta_offset <= 0;
291
                  end
292
            end
293
         tagged SHfirst_mb_in_slice .xdata :
294
            begin
295
               infifo.deq();
296
               outfifo.enq(EDOT infifo.first());
297
               firstMb   <= xdata;
298
               currMb    <= xdata;
299
               currMbHor <= xdata;
300
               currMbVer <= 0;
301
            end
302
         tagged SHdisable_deblocking_filter_idc .xdata :
303
            begin
304
               infifo.deq();
305
               disable_deblocking_filter_idc <= xdata;
306
            end
307
         tagged SHslice_alpha_c0_offset .xdata :
308
            begin
309
               infifo.deq();
310
               slice_alpha_c0_offset <= xdata;
311
            end
312
         tagged SHslice_beta_offset .xdata :
313
            begin
314
               infifo.deq();
315
               slice_beta_offset <= xdata;
316
            end
317
         tagged IBTmb_qp .xdata :
318
            begin
319
               infifo.deq();
320
               curr_qpy <= xdata.qpy;
321
               curr_qpc <= xdata.qpc;
322
            end
323
         tagged PBbS .xdata :
324
            begin
325
               process <= Initialize;
326
            end
327
         tagged PBoutput .xdata :
328
            begin
329
               $display( "ERROR Deblocking Filter: passing PBoutput");
330
            end
331
         tagged EndOfFile :
332
            begin
333
               infifo.deq();
334
               outfifo.enq(EDOT infifo.first());
335
               $display( "ccl5: EndOfFile reached");
336
               //$finish(0);
337
            end
338
         default:
339
            begin
340
               infifo.deq();
341
               outfifo.enq(EDOT infifo.first());
342
            end
343
      endcase
344
   endrule
345
 
346
 
347
   rule currMbHorUpdate( !(currMbHor
348
      Bit#(PicAreaSz) temp = zeroExtend(picWidth);
349
      if((currMbHor >> 3) >= temp)
350
         begin
351
            currMbHor <= currMbHor - (temp << 3);
352
            currMbVer <= currMbVer + 8;
353
         end
354
      else
355
         begin
356
            currMbHor <= currMbHor - temp;
357
            currMbVer <= currMbVer + 1;
358
         end
359
   endrule
360
 
361
 
362
   rule initialize ( process==Initialize && currMbHor
363
      //$display( "TRACE Deblocking Filter: initialize %0d", currMb);
364
      process <= Horizontal;
365
      dataReqCount <= 1;
366
      dataRespCount <= 1;
367
      filterTopMbEdgeFlag <= !(currMb
368
      filterLeftMbEdgeFlag <= !(currMbHor==0 || disable_deblocking_filter_idc==1 || (disable_deblocking_filter_idc==2 && currMb==firstMb));
369
      filterInternalEdgesFlag <= !(disable_deblocking_filter_idc==1);
370
      blockNum <= 0;
371
      pixelNum <= 0;
372
      Bit#(6) curr_qp = (chromaFlag==0 ? curr_qpy : curr_qpc);
373
      Bit#(6) left_qp = (chromaFlag==0 ? left_qpy : left_qpc);
374
      Bit#(7) qpavtemp = zeroExtend(curr_qp)+zeroExtend(left_qp)+1;
375
      Bit#(6) qpav = qpavtemp[6:1];
376
      Bit#(8) indexAtemp = zeroExtend(qpav)+signExtend(slice_alpha_c0_offset);
377
      Bit#(8) indexBtemp = zeroExtend(qpav)+signExtend(slice_beta_offset);
378
      Bit#(6) indexA = (indexAtemp[7]==1 ? 0 : (indexAtemp[6:0]>51 ? 51 : indexAtemp[5:0]));
379
      Bit#(6) indexB = (indexBtemp[7]==1 ? 0 : (indexBtemp[6:0]>51 ? 51 : indexBtemp[5:0]));
380
      alphaMbEdge <= alpha_table[indexA];
381
      betaMbEdge <= beta_table[indexB];
382
      Vector#(3,Bit#(5)) tc0temp = arrayToVector(tc0_table[indexA]);
383
      tc0MbEdge <= tc0temp;
384
   endrule
385
 
386
 
387
   rule dataSendReq ( dataReqCount>0 && currMbHor
388
      //$display( "TRACE Deblocking Filter: dataSendReq %0d", dataReqCount);
389
      Bit#(PicWidthSz) temp = truncate(currMbHor);
390
      if(currMb
391
         dataReqCount <= 0;
392
      else
393
         begin
394
            if(dataReqCount==1)
395
               parameterMemReqQ.enq(LoadReq temp);
396
            Bit#(4) temp2 = truncate(dataReqCount-1);
397
            let temp3 = {temp,chromaFlag,temp2};
398
            dataMemReqQ.enq(LoadReq temp3);
399
            if(dataReqCount==16)
400
               dataReqCount <= 0;
401
            else
402
               dataReqCount <= dataReqCount+1;
403
         end
404
   endrule
405
 
406
 
407
   rule dataReceiveNoResp ( dataRespCount>0 && currMb
408
      //$display( "TRACE Deblocking Filter: dataReceiveNoResp");
409
      dataRespCount <= 0;
410
   endrule
411
 
412
 
413
   rule dataReceiveResp ( dataRespCount>0 && !(currMb
414
      //$display( "TRACE Deblocking Filter: dataReceiveResp %0d", dataRespCount);
415
      Bit#(4) temp = truncate(dataRespCount-1);
416
      Vector#(16,Bit#(32)) topVectorNext = topVector;
417
      if(dataRespCount==1)
418
         begin
419
            Bit#(13) tempParameters=0;
420
            if(parameterMemRespQ.first() matches tagged LoadResp .xdata)
421
               tempParameters = xdata;
422
            top_qpy <= tempParameters[5:0];
423
            top_qpc <= tempParameters[11:6];
424
            top_intra <= tempParameters[12];
425
            parameterMemRespQ.deq();
426
         end
427
      if(dataRespCount==16)
428
         dataRespCount <= 0;
429
      else
430
         dataRespCount <= dataRespCount+1;
431
      if(dataMemRespQ.first() matches tagged LoadResp .xdata)
432
            topVectorNext[temp] = xdata;
433
      dataMemRespQ.deq();
434
      topVector <= topVectorNext;
435
      //$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]);
436
   endrule
437
 
438
 
439
   rule horizontal ( process==Horizontal && currMbHor
440
      //$display( "TRACE Deblocking Filter: horizontal %0d %0d %0d", blockNum, pixelNum, infifo.first());
441
      Bit#(2) blockHor = {blockNum[2],blockNum[0]};
442
      Bit#(2) blockVer = {blockNum[3],blockNum[1]};
443
      Bit#(2) pixelVer = {pixelNum[3],pixelNum[2]};
444
      Vector#(96,Bit#(32)) leftVectorNext = leftVector;
445
      Vector#(64,Bit#(32)) workVectorNext = workVector;
446
      Bool leftEdge = (blockNum[0]==0 && (blockNum[2]==0 || chromaFlag==1));
447
      if(blockNum==0 && pixelNum==0)
448
         begin
449
            Bit#(6) qpav = (chromaFlag==0 ? curr_qpy : curr_qpc);
450
            Bit#(8) indexAtemp = zeroExtend(qpav)+signExtend(slice_alpha_c0_offset);
451
            Bit#(8) indexBtemp = zeroExtend(qpav)+signExtend(slice_beta_offset);
452
            Bit#(6) indexA = (indexAtemp[7]==1 ? 0 : (indexAtemp[6:0]>51 ? 51 : indexAtemp[5:0]));
453
            Bit#(6) indexB = (indexBtemp[7]==1 ? 0 : (indexBtemp[6:0]>51 ? 51 : indexBtemp[5:0]));
454
            alphaInternal <= alpha_table[indexA];
455
            betaInternal <= beta_table[indexB];
456
            Vector#(3,Bit#(5)) tc0temp = arrayToVector(tc0_table[indexA]);
457
            tc0Internal <= tc0temp;
458
         end
459
      case (infifo.first()) matches
460
         tagged PBbS .xdata :
461
            begin
462
               infifo.deq();
463
               bSfile.upd(blockNum,tuple2(xdata.bShor,xdata.bSver));
464
            end
465
         tagged PBoutput .xdata :
466
            begin
467
               infifo.deq();
468
               Bit#(6) addrq = {blockHor,blockVer,pixelVer};
469
               Bit#(7) addrpLeft = (chromaFlag==0 ? {3'b011,blockVer,pixelVer} : {2'b10,blockHor[1],1'b1,blockVer[0],pixelVer});
470
               Bit#(6) addrpCurr = {(blockHor-1),blockVer,pixelVer};
471
               Bit#(32) pixelq = {xdata[3],xdata[2],xdata[1],xdata[0]};
472
               Bit#(32) pixelp;
473
               if(leftEdge)
474
                  pixelp = leftVector[addrpLeft];
475
               else
476
                  pixelp = workVector[addrpCurr];
477
               Bit#(64) result = {pixelq,pixelp};
478
               if(leftEdge && filterLeftMbEdgeFlag)
479
                  begin
480
                     if(filter_test({pixelq[15:0],pixelp[31:16]},alphaMbEdge,betaMbEdge))
481
                        result = filter_input({pixelq,pixelp},chromaFlag==1,tpl_1(bSfile.sub((chromaFlag==0?blockNum:{blockNum[1:0],pixelVer[1],1'b0}))),alphaMbEdge,betaMbEdge,tc0MbEdge);
482
                  end
483
               else if(!leftEdge && filterInternalEdgesFlag)
484
                  begin
485
                     if(filter_test({pixelq[15:0],pixelp[31:16]},alphaInternal,betaInternal))
486
                        result = filter_input({pixelq,pixelp},chromaFlag==1,tpl_1(bSfile.sub((chromaFlag==0?blockNum:{blockNum[1:0],pixelVer[1],1'b0}))),alphaInternal,betaInternal,tc0Internal);
487
                  end
488
               if(leftEdge)
489
                  leftVectorNext[addrpLeft] = result[31:0];
490
               else
491
                  workVectorNext[addrpCurr] = result[31:0];
492
               workVectorNext[addrq] = result[63:32];
493
               leftVector <= leftVectorNext;
494
               workVector <= workVectorNext;
495
               if(pixelNum==12 && (blockNum==15 || (blockNum==7 && chromaFlag==1)))
496
                  begin
497
                     blockNum <= 0;
498
                     process <= Vertical;
499
                     startLastOutput <= False;
500
                     outputingFinished <= False;
501
                     colNum <= 0;
502
                     if(filterTopMbEdgeFlag)
503
                        rowNum <= 0;
504
                     else
505
                        rowNum <= 1;
506
                     Bit#(6) curr_qp = (chromaFlag==0 ? curr_qpy : curr_qpc);
507
                     Bit#(6) top_qp = (chromaFlag==0 ? top_qpy : top_qpc);
508
                     Bit#(7) qpavtemp = zeroExtend(curr_qp)+zeroExtend(top_qp)+1;
509
                     Bit#(6) qpav = qpavtemp[6:1];
510
                     Bit#(8) indexAtemp = zeroExtend(qpav)+signExtend(slice_alpha_c0_offset);
511
                     Bit#(8) indexBtemp = zeroExtend(qpav)+signExtend(slice_beta_offset);
512
                     Bit#(6) indexA = (indexAtemp[7]==1 ? 0 : (indexAtemp[6:0]>51 ? 51 : indexAtemp[5:0]));
513
                     Bit#(6) indexB = (indexBtemp[7]==1 ? 0 : (indexBtemp[6:0]>51 ? 51 : indexBtemp[5:0]));
514
                     alphaMbEdge <= alpha_table[indexA];
515
                     betaMbEdge <= beta_table[indexB];
516
                     Vector#(3,Bit#(5)) tc0temp = arrayToVector(tc0_table[indexA]);
517
                     tc0MbEdge <= tc0temp;
518
                  end
519
               else if(pixelNum==12)
520
                  blockNum <= blockNum+1;
521
               pixelNum <= pixelNum+4;
522
            end
523
         //default: $display( "ERROR Deblocking Filter: horizontal non-PBoutput input");
524
      endcase
525
   endrule
526
 
527
 
528
   rule vertical ( process==Vertical && !startLastOutput && dataRespCount==0 && currMbHor
529
      //$display( "TRACE Deblocking Filter: vertical %0d %0d", colNum, rowNum);
530
      //$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]);
531
      Bool topEdge = (rowNum==0);
532
      Vector#(64,Bit#(32)) workVectorNext = workVector;
533
      Vector#(16,Bit#(32)) topVectorNext = topVector;
534
      Vector#(64,Bit#(32)) workV = workVector;
535
      Vector#(4,Bit#(32)) tempV = replicate(0);
536
      Vector#(4,Bit#(64)) resultV = replicate(0);
537
      Bit#(8) alpha;
538
      Bit#(5) beta;
539
      Vector#(3,Bit#(5)) tc0;
540
      Bit#(4) crNum = {colNum,rowNum};
541
      if(topEdge)
542
         begin
543
            tempV[0] = topVector[{colNum,2'b00}];
544
            tempV[1] = topVector[{colNum,2'b01}];
545
            tempV[2] = topVector[{colNum,2'b10}];
546
            tempV[3] = topVector[{colNum,2'b11}];
547
            alpha = alphaMbEdge;
548
            beta = betaMbEdge;
549
            tc0 = tc0MbEdge;
550
         end
551
      else
552
         begin
553
            tempV[0] = workV[{(crNum-1),2'b00}];
554
            tempV[1] = workV[{(crNum-1),2'b01}];
555
            tempV[2] = workV[{(crNum-1),2'b10}];
556
            tempV[3] = workV[{(crNum-1),2'b11}];
557
            alpha = alphaInternal;
558
            beta = betaInternal;
559
            tc0 = tc0Internal;
560
         end
561
      resultV[0] = {workV[{crNum,2'b11}][7:0],workV[{crNum,2'b10}][7:0],workV[{crNum,2'b01}][7:0],workV[{crNum,2'b00}][7:0],tempV[3][7:0],tempV[2][7:0],tempV[1][7:0],tempV[0][7:0]};
562
      resultV[1] = {workV[{crNum,2'b11}][15:8],workV[{crNum,2'b10}][15:8],workV[{crNum,2'b01}][15:8],workV[{crNum,2'b00}][15:8],tempV[3][15:8],tempV[2][15:8],tempV[1][15:8],tempV[0][15:8]};
563
      resultV[2] = {workV[{crNum,2'b11}][23:16],workV[{crNum,2'b10}][23:16],workV[{crNum,2'b01}][23:16],workV[{crNum,2'b00}][23:16],tempV[3][23:16],tempV[2][23:16],tempV[1][23:16],tempV[0][23:16]};
564
      resultV[3] = {workV[{crNum,2'b11}][31:24],workV[{crNum,2'b10}][31:24],workV[{crNum,2'b01}][31:24],workV[{crNum,2'b00}][31:24],tempV[3][31:24],tempV[2][31:24],tempV[1][31:24],tempV[0][31:24]};
565
      if(filter_test({workV[{crNum,2'b01}][7:0],workV[{crNum,2'b00}][7:0],tempV[3][7:0],tempV[2][7:0]},alpha,beta))
566
         resultV[0] = filter_input(resultV[0],chromaFlag==1,tpl_2(bSfile.sub((chromaFlag==0?{rowNum[1],colNum[1],rowNum[0],colNum[0]}:{rowNum[0],colNum[0],2'b00}))),alpha,beta,tc0);
567
      if(filter_test({workV[{crNum,2'b01}][15:8],workV[{crNum,2'b00}][15:8],tempV[3][15:8],tempV[2][15:8]},alpha,beta))
568
         resultV[1] = filter_input(resultV[1],chromaFlag==1,tpl_2(bSfile.sub((chromaFlag==0?{rowNum[1],colNum[1],rowNum[0],colNum[0]}:{rowNum[0],colNum[0],2'b00}))),alpha,beta,tc0);
569
      if(filter_test({workV[{crNum,2'b01}][23:16],workV[{crNum,2'b00}][23:16],tempV[3][23:16],tempV[2][23:16]},alpha,beta))
570
         resultV[2] = filter_input(resultV[2],chromaFlag==1,tpl_2(bSfile.sub((chromaFlag==0?{rowNum[1],colNum[1],rowNum[0],colNum[0]}:{rowNum[0],colNum[0],2'b01}))),alpha,beta,tc0);
571
      if(filter_test({workV[{crNum,2'b01}][31:24],workV[{crNum,2'b00}][31:24],tempV[3][31:24],tempV[2][31:24]},alpha,beta))
572
         resultV[3] = filter_input(resultV[3],chromaFlag==1,tpl_2(bSfile.sub((chromaFlag==0?{rowNum[1],colNum[1],rowNum[0],colNum[0]}:{rowNum[0],colNum[0],2'b01}))),alpha,beta,tc0);
573
      if(topEdge)
574
         begin
575
            topVectorNext[{colNum,2'b00}] = {resultV[3][7:0],resultV[2][7:0],resultV[1][7:0],resultV[0][7:0]};
576
            topVectorNext[{colNum,2'b01}] = {resultV[3][15:8],resultV[2][15:8],resultV[1][15:8],resultV[0][15:8]};
577
            topVectorNext[{colNum,2'b10}] = {resultV[3][23:16],resultV[2][23:16],resultV[1][23:16],resultV[0][23:16]};
578
            topVectorNext[{colNum,2'b11}] = {resultV[3][31:24],resultV[2][31:24],resultV[1][31:24],resultV[0][31:24]};
579
         end
580
      else
581
         begin
582
            workVectorNext[{(crNum-1),2'b00}] = {resultV[3][7:0],resultV[2][7:0],resultV[1][7:0],resultV[0][7:0]};
583
            workVectorNext[{(crNum-1),2'b01}] = {resultV[3][15:8],resultV[2][15:8],resultV[1][15:8],resultV[0][15:8]};
584
            workVectorNext[{(crNum-1),2'b10}] = {resultV[3][23:16],resultV[2][23:16],resultV[1][23:16],resultV[0][23:16]};
585
            workVectorNext[{(crNum-1),2'b11}] = {resultV[3][31:24],resultV[2][31:24],resultV[1][31:24],resultV[0][31:24]};
586
         end
587
      workVectorNext[{crNum,2'b00}] =  {resultV[3][39:32],resultV[2][39:32],resultV[1][39:32],resultV[0][39:32]};
588
      workVectorNext[{crNum,2'b01}] =  {resultV[3][47:40],resultV[2][47:40],resultV[1][47:40],resultV[0][47:40]};
589
      workVectorNext[{crNum,2'b10}] =  {resultV[3][55:48],resultV[2][55:48],resultV[1][55:48],resultV[0][55:48]};
590
      workVectorNext[{crNum,2'b11}] =  {resultV[3][63:56],resultV[2][63:56],resultV[1][63:56],resultV[0][63:56]};
591
      if(topEdge)
592
         topVector <= topVectorNext;
593
      workVector <= workVectorNext;
594
      if(rowNum==3 || (chromaFlag==1 && rowNum==1))
595
         begin
596
            if(colNum==3)
597
               startLastOutput <= True;
598
            else
599
               begin
600
                  if(filterTopMbEdgeFlag)
601
                     rowNum <= 0;
602
                  else
603
                     rowNum <= 1;
604
               end
605
            colNum <= colNum+1;
606
         end
607
      else
608
        rowNum <= rowNum+1;
609
   endrule
610
 
611
 
612
   rule outputing ( process==Vertical && !outputingFinished && currMbHor
613
      //$display( "TRACE Deblocking Filter: outputting %0d %0d", blockNum, pixelNum);
614
      Bit#(2) blockHor = pixelNum[1:0];
615
      Bit#(2) blockVer = blockNum[1:0];
616
      Bit#(2) pixelVer = pixelNum[3:2];
617
      Bit#(PicWidthSz) currMbHorT = truncate(currMbHor);
618
      Bool stalling = False;
619
      if(currMb==0)
620
         begin
621
            if(startLastOutput)
622
               outputingFinished <= True;
623
         end
624
      else
625
         begin
626
            Bit#(7) leftAddr;
627
            if(chromaFlag==0)
628
               leftAddr = {1'b0,blockHor,blockVer,pixelVer};
629
            else
630
               leftAddr = {2'b10,blockHor,blockVer[0],pixelVer};
631
            Bit#(32) leftData = leftVector[leftAddr];
632
            if(!(blockNum==3 || (blockNum==1 && chromaFlag==1)))
633
               begin
634
                  if(chromaFlag==0)
635
                     outfifo.enq(DFBLuma {ver:{(currMbHorT==0 ? currMbVer-1 : currMbVer),blockVer,pixelVer},hor:{(currMbHorT==0 ? picWidth-1 : currMbHorT-1),blockHor},data:leftData});
636
                  else
637
                     outfifo.enq(DFBChroma {uv:blockHor[1],ver:{(currMbHorT==0 ? currMbVer-1 : currMbVer),blockVer[0],pixelVer},hor:{(currMbHorT==0 ? picWidth-1 : currMbHorT-1),blockHor[0]},data:leftData});
638
               end
639
            else if(startLastOutput)
640
               begin
641
                  Bit#(PicWidthSz) temp = ((currMbHor==0) ? (picWidth-1) : truncate(currMbHor-1));
642
                  dataMemReqQ.enq(StoreReq {addr:{temp,chromaFlag,blockHor,pixelVer},data:leftData});
643
                  if(currMbVer > 0)
644
                     begin
645
                        //$display( "TRACE Deblocking Filter: outputting last output %0d %0d %h", blockHor, pixelVer, topVector[{blockHor,pixelVer}]);
646
                        Bit#(32) topData = topVector[{blockHor,pixelVer}];
647
                        if(chromaFlag==0)
648
                           outfifo.enq(DFBLuma {ver:{currMbVer-1,2'b11,pixelVer},hor:{currMbHorT,blockHor},data:topData});
649
                        else
650
                           outfifo.enq(DFBChroma {uv:blockHor[1],ver:{currMbVer-1,1'b1,pixelVer},hor:{currMbHorT,blockHor[0]},data:topData});
651
                     end
652
               end
653
            else
654
               stalling = True;
655
            if(!stalling)
656
               begin
657
                  if(pixelNum==15)
658
                     begin
659
                        if(blockNum==3 || (chromaFlag==1 && blockNum==1))
660
                           begin
661
                              if(currMbVer==picHeight-1)
662
                                 blockNum <= (chromaFlag==0 ? 3 : 1);
663
                              else
664
                                 blockNum <= 0;
665
                              outputingFinished <= True;
666
                           end
667
                        else
668
                           blockNum <= blockNum+1;
669
                     end
670
                  pixelNum <= pixelNum+1;
671
               end
672
         end
673
   endrule
674
 
675
 
676
   rule verticaltocleanup  ( process==Vertical && startLastOutput && outputingFinished);
677
      process <= Cleanup;
678
      startLastOutput <= False;
679
      outputingFinished <= False;
680
   endrule
681
 
682
 
683
   rule cleanup ( process==Cleanup && currMbHor
684
      //$display( "TRACE Deblocking Filter: cleanup %0d %0d", blockNum, pixelNum);
685
      Bit#(2) blockHor = pixelNum[1:0];
686
      Bit#(2) blockVer = blockNum[1:0];
687
      Bit#(2) pixelVer = pixelNum[3:2];
688
      Bit#(PicWidthSz) currMbHorT = truncate(currMbHor);
689
      Vector#(96,Bit#(32)) leftVectorNext = leftVector;
690
      if(blockNum==0)
691
         begin
692
            if(chromaFlag==0)
693
               begin
694
                  for(Integer ii=0; ii<64; ii=ii+1)
695
                     leftVectorNext[fromInteger(ii)] = workVector[fromInteger(ii)];
696
                  chromaFlag <= 1;
697
                  process <= Initialize;
698
               end
699
            else
700
               begin
701
                  for(Integer ii=0; ii<32; ii=ii+1)
702
                     begin
703
                        Bit#(5) tempAddr = fromInteger(ii);
704
                        leftVectorNext[{2'b10,tempAddr}] = workVector[{tempAddr[4:3],1'b0,tempAddr[2:0]}];
705
                     end
706
                  chromaFlag <= 0;
707
                  process <= Passing;
708
                  Bit#(PicWidthSz) temp = truncate(currMbHor);
709
                  parameterMemReqQ.enq(StoreReq {addr:temp,data:{curr_intra,curr_qpc,curr_qpy}});
710
                  left_intra <= curr_intra;
711
                  left_qpc <= curr_qpc;
712
                  left_qpy <= curr_qpy;
713
                  currMb <= currMb+1;
714
                  currMbHor <= currMbHor+1;
715
                  if(currMbVer==picHeight-1 && currMbHor==zeroExtend(picWidth-1))
716
                     outfifo.enq(EndOfFrame);
717
               end
718
            leftVector <= leftVectorNext;
719
         end
720
      else if(blockNum < 8)
721
         begin
722
            Bit#(7) leftAddr;
723
            if(chromaFlag==0)
724
               leftAddr = {1'b0,blockHor,blockVer,pixelVer};
725
            else
726
               leftAddr = {2'b10,blockHor,blockVer[0],pixelVer};
727
            Bit#(32) leftData = leftVector[leftAddr];
728
            if(chromaFlag==0)
729
               outfifo.enq(DFBLuma {ver:{(currMbHorT==0 ? currMbVer-1 : currMbVer),blockVer,pixelVer},hor:{(currMbHorT==0 ? picWidth-1 : currMbHorT-1),blockHor},data:leftData});
730
            else
731
               outfifo.enq(DFBChroma {uv:blockHor[1],ver:{(currMbHorT==0 ? currMbVer-1 : currMbVer),blockVer[0],pixelVer},hor:{(currMbHorT==0 ? picWidth-1 : currMbHorT-1),blockHor[0]},data:leftData});
732
            if(pixelNum==15)
733
               begin
734
                  if(currMbHor==zeroExtend(picWidth-1))
735
                     blockNum <= 8;
736
                  else
737
                     blockNum <= 0;
738
               end
739
            pixelNum <= pixelNum+1;
740
         end
741
      else
742
         begin
743
            Bit#(6) currAddr = {blockHor,blockVer,pixelVer};
744
            Bit#(32) currData = workVector[currAddr];
745
            if(chromaFlag==0)
746
               outfifo.enq(DFBLuma {ver:{currMbVer,blockVer,pixelVer},hor:{currMbHorT,blockHor},data:currData});
747
            else
748
               outfifo.enq(DFBChroma {uv:blockHor[1],ver:{currMbVer,blockVer[0],pixelVer},hor:{currMbHorT,blockHor[0]},data:currData});
749
            if(pixelNum==15)
750
               begin
751
                  if(blockNum[1:0]==3 || (blockNum[1:0]==1 && chromaFlag==1))
752
                     blockNum <= 0;
753
                  else
754
                     blockNum <= blockNum+1;
755
               end
756
            pixelNum <= pixelNum+1;
757
         end
758
   endrule
759
 
760
 
761
 
762
 
763
 
764
 
765
   interface Client mem_client_data;
766
      interface Get request  = fifoToGet(dataMemReqQ);
767
      interface Put response = fifoToPut(dataMemRespQ);
768
   endinterface
769
 
770
   interface Client mem_client_parameter;
771
      interface Get request  = fifoToGet(parameterMemReqQ);
772
      interface Put response = fifoToPut(parameterMemRespQ);
773
   endinterface
774
 
775
   interface Put ioin  = fifoToPut(infifo);
776
   interface Get ioout = fifoToGet(outfifo);
777
 
778
endmodule
779
 
780
endpackage

powered by: WebSVN 2.1.0

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