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 85

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

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

powered by: WebSVN 2.1.0

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