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

Subversion Repositories bluespec-h264

[/] [bluespec-h264/] [trunk/] [LumaChromaParallel/] [mkInterpolator.bsv] - Blame information for rev 90

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

Line No. Rev Author Line
1 90 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
// interpolator implementation
24
//----------------------------------------------------------------------
25
//
26
//
27
 
28
package mkInterpolator;
29
 
30
import H264Types::*;
31
import IInterpolator::*;
32
import FIFO::*;
33
import Vector::*;
34
 
35
import Connectable::*;
36
import GetPut::*;
37
import ClientServer::*;
38
 
39
 
40
//-----------------------------------------------------------
41
// Local Datatypes
42
//-----------------------------------------------------------
43
 
44
typedef union tagged
45
{
46
 struct { Bit#(2) xFracL; Bit#(2) yFracL; Bit#(2) offset; IPBlockType bt; } IPWLuma;
47
 struct { Bit#(3) xFracC; Bit#(3) yFracC; Bit#(2) offset; IPBlockType bt; } IPWChroma;
48
}
49
InterpolatorWT deriving(Eq,Bits);
50
 
51
 
52
//-----------------------------------------------------------
53
// Helper functions
54
 
55
function Bit#(8) clip1y10to8( Bit#(10) innum );
56
   if(innum[9] == 1)
57
      return 0;
58
   else if(innum[8] == 1)
59
      return 255;
60
   else
61
      return truncate(innum);
62
endfunction
63
 
64
function Bit#(15) interpolate8to15( Bit#(8) in0, Bit#(8) in1, Bit#(8) in2, Bit#(8) in3, Bit#(8) in4, Bit#(8) in5 );
65
   return zeroExtend(in0) - 5*zeroExtend(in1) + 20*zeroExtend(in2) + 20*zeroExtend(in3) - 5*zeroExtend(in4) + zeroExtend(in5);
66
endfunction
67
 
68
function Bit#(8) interpolate15to8( Bit#(15) in0, Bit#(15) in1, Bit#(15) in2, Bit#(15) in3, Bit#(15) in4, Bit#(15) in5 );
69
   Bit#(20) temp = signExtend(in0) - 5*signExtend(in1) + 20*signExtend(in2) + 20*signExtend(in3) - 5*signExtend(in4) + signExtend(in5) + 512;
70
   return clip1y10to8(truncate(temp>>10));
71
endfunction
72
 
73
 
74
 
75
//-----------------------------------------------------------
76
// Interpolation Module
77
//-----------------------------------------------------------
78
 
79
 
80
(* synthesize *)
81
module mkInterpolator( Interpolator );
82
 
83
   FIFO#(InterpolatorIT) reqfifoLoad <- mkSizedFIFO(interpolator_reqfifoLoad_size);
84
   FIFO#(InterpolatorWT) reqfifoWork1 <- mkSizedFIFO(interpolator_reqfifoWork_size);
85
   Reg#(Maybe#(InterpolatorWT)) reqregWork2 <- mkReg(Invalid);
86
   FIFO#(Vector#(4,Bit#(8))) outfifo <- mkFIFO;
87
   Reg#(Bool) endOfFrameFlag <- mkReg(False);
88
   FIFO#(InterpolatorLoadReq)  memReqQ  <- mkFIFO;
89
   FIFO#(InterpolatorLoadResp) memRespQ <- mkSizedFIFO(interpolator_memRespQ_size);
90
 
91
   Reg#(Bit#(PicWidthSz))  picWidth  <- mkReg(maxPicWidthInMB);
92
   Reg#(Bit#(PicHeightSz)) picHeight <- mkReg(0);
93
 
94
   RFile1#(Bit#(6),Vector#(4,Bit#(15))) workFile  <- mkRFile1Full();
95
   RFile1#(Bit#(6),Vector#(4,Bit#(8)))  storeFile <- mkRFile1Full();
96
   Reg#(Bit#(1)) workFileFlag <- mkReg(0);
97
   RFile1#(Bit#(4),Vector#(4,Bit#(8))) resultFile <- mkRFile1Full();
98
 
99
   Reg#(Bit#(1)) loadStage  <- mkReg(0);
100
   Reg#(Bit#(2)) loadHorNum <- mkReg(0);
101
   Reg#(Bit#(4)) loadVerNum <- mkReg(0);
102
 
103
   Reg#(Bit#(2)) work1MbPart    <- mkReg(0);//only for Chroma
104
   Reg#(Bit#(2)) work1SubMbPart <- mkReg(0);//only for Chroma
105
   Reg#(Bit#(1)) work1Stage     <- mkReg(0);
106
   Reg#(Bit#(2)) work1HorNum    <- mkReg(0);
107
   Reg#(Bit#(4)) work1VerNum    <- mkReg(0);
108
   Reg#(Vector#(20,Bit#(8))) work1Vector8 <- mkRegU;
109
   Reg#(Bool) work1Done <- mkReg(False);
110
 
111
   Reg#(Bit#(2)) work2SubMbPart <- mkReg(0);
112
   Reg#(Bit#(2)) work2HorNum    <- mkReg(0);
113
   Reg#(Bit#(4)) work2VerNum    <- mkReg(0);
114
   Reg#(Vector#(20,Bit#(8))) work2Vector8 <- mkRegU;
115
   Reg#(Vector#(20,Bit#(15))) work2Vector15 <- mkRegU;
116
   Reg#(Vector#(16,Bit#(1))) resultReady <- mkReg(replicate(0));
117
   Reg#(Bool) work2Done <- mkReg(False);
118
   Reg#(Bool) work8x8Done <- mkReg(False);
119
 
120
   Reg#(Bit#(2)) outBlockNum <- mkReg(0);
121
   Reg#(Bit#(2)) outPixelNum <- mkReg(0);
122
   Reg#(Bool) outDone <- mkReg(False);
123
 
124
 
125
   rule sendEndOfFrameReq( endOfFrameFlag );
126
      endOfFrameFlag <= False;
127
      memReqQ.enq(IPLoadEndFrame);
128
   endrule
129
 
130
 
131
   rule loadLuma( reqfifoLoad.first() matches tagged IPLuma .reqdata &&& !endOfFrameFlag );
132
      Bit#(2) xfracl = reqdata.mvhor[1:0];
133
      Bit#(2) yfracl = reqdata.mvver[1:0];
134
      Bit#(2) offset = reqdata.mvhor[3:2];
135
      Bool twoStage = (xfracl==1||xfracl==3) && (yfracl==1||yfracl==3);
136
      Bool horInter = (twoStage ? loadStage==1 : xfracl!=0);
137
      Bool verInter = (twoStage ? loadStage==0 : yfracl!=0);
138
      Bit#(2) offset2 = reqdata.mvhor[3:2] + ((twoStage&&verInter&&xfracl==3) ? 1 : 0);
139
      Bit#(1) horOut = 0;
140
      Bit#(TAdd#(PicWidthSz,2)) horAddr;
141
      Bit#(TAdd#(PicHeightSz,4)) verAddr;
142
      Bit#(TAdd#(PicWidthSz,12)) horTemp = zeroExtend({reqdata.hor,2'b00}) + zeroExtend({loadHorNum,2'b00}) + (xfracl==3&&(yfracl==1||yfracl==3)&&loadStage==0 ? 1 : 0);
143
      Bit#(TAdd#(PicHeightSz,10)) verTemp = zeroExtend(reqdata.ver) + zeroExtend(loadVerNum) + (yfracl==3&&(xfracl==1||xfracl==3)&&loadStage==1 ? 1 : 0);
144
      Bit#(13) mvhortemp = signExtend(reqdata.mvhor[13:2])-(horInter?2:0);
145
      Bit#(11) mvvertemp = signExtend(reqdata.mvver[11:2])-(verInter?2:0);
146
      if(mvhortemp[12]==1 && zeroExtend(0-mvhortemp)>horTemp)
147
         begin
148
            horAddr = 0;
149
            horOut = 1;
150
         end
151
      else
152
         begin
153
            horTemp = horTemp + signExtend(mvhortemp);
154
            if(horTemp>=zeroExtend({picWidth,4'b0000}))
155
               begin
156
                  horAddr = {picWidth-1,2'b11};
157
                  horOut = 1;
158
               end
159
            else
160
               horAddr = truncate(horTemp>>2);
161
         end
162
      if(mvvertemp[10]==1 && zeroExtend(0-mvvertemp)>verTemp)
163
         verAddr = 0;
164
      else
165
         begin
166
            verTemp = verTemp + signExtend(mvvertemp);
167
            if(verTemp>=zeroExtend({picHeight,4'b0000}))
168
               verAddr = {picHeight-1,4'b1111};
169
            else
170
               verAddr = truncate(verTemp);
171
         end
172
      memReqQ.enq(IPLoadLuma {refIdx:reqdata.refIdx,horOutOfBounds:horOut,hor:horAddr,ver:verAddr});
173
      Bool verFirst = twoStage || (yfracl==2&&(xfracl==1||xfracl==3));
174
      Bit#(2) loadHorNumMax = (reqdata.bt==IP8x8||reqdata.bt==IP8x4 ? 1 : 0) + (horInter ? 2 : (offset2==0 ? 0 : 1));
175
      Bit#(4) loadVerNumMax = (reqdata.bt==IP8x8||reqdata.bt==IP4x8 ? 7 : 3) + (verInter ? 5 : 0);
176
      if(verFirst)
177
         begin
178
            if(loadVerNum < loadVerNumMax)
179
               loadVerNum <= loadVerNum+1;
180
            else
181
               begin
182
                  loadVerNum <= 0;
183
                  if(loadHorNum < loadHorNumMax)
184
                     begin
185
                        if(loadStage == 1)
186
                           begin
187
                              offset = offset + (xfracl==3 ? 1 : 0);
188
                              if(!(offset==1 || (xfracl==3 && offset==2)))
189
                                 loadHorNum <= loadHorNumMax;
190
                              else
191
                                 begin
192
                                    loadHorNum <= 0;
193
                                    loadStage <= 0;
194
                                    reqfifoLoad.deq();
195
                                 end
196
                           end
197
                        else
198
                           loadHorNum <= loadHorNum+1;
199
                     end
200
                  else
201
                     begin
202
                        if(twoStage && loadStage==0)
203
                           begin
204
                              offset = offset + (xfracl==3 ? 1 : 0);
205
                              if((xfracl==3 ? offset<3 : offset<2))
206
                                 loadHorNum <= 0;
207
                              else
208
                                 loadHorNum <= loadHorNumMax+1;
209
                              loadStage <= 1;
210
                           end
211
                        else
212
                           begin
213
                              loadHorNum <= 0;
214
                              loadStage <= 0;
215
                              reqfifoLoad.deq();
216
                           end
217
                     end
218
               end
219
         end
220
      else
221
         begin
222
            if(loadHorNum < loadHorNumMax)
223
               loadHorNum <= loadHorNum+1;
224
            else
225
               begin
226
                  loadHorNum <= 0;
227
                  if(loadVerNum < loadVerNumMax)
228
                     loadVerNum <= loadVerNum+1;
229
                  else
230
                     begin
231
                        loadVerNum <= 0;
232
                        reqfifoLoad.deq();
233
                     end
234
               end
235
         end
236
      if(reqdata.bt==IP16x16 || reqdata.bt==IP16x8 || reqdata.bt==IP8x16)
237
         $display( "ERROR Interpolation: loadLuma block sizes > 8x8 not supported");
238
      $display( "PARDEBLOCK Trace interpolator: loadLuma %h %h %h %h %h %h %h", xfracl, yfracl, loadHorNum, loadVerNum, reqdata.refIdx, horAddr, verAddr);
239
   endrule
240
 
241
 
242
   rule loadChroma( reqfifoLoad.first() matches tagged IPChroma .reqdata &&& !endOfFrameFlag );
243
      Bit#(3) xfracc = reqdata.mvhor[2:0];
244
      Bit#(3) yfracc = reqdata.mvver[2:0];
245
      Bit#(2) offset = reqdata.mvhor[4:3]+{reqdata.hor[0],1'b0};
246
      Bit#(1) horOut = 0;
247
      Bit#(TAdd#(PicWidthSz,1)) horAddr;
248
      Bit#(TAdd#(PicHeightSz,3)) verAddr;
249
      Bit#(TAdd#(PicWidthSz,11)) horTemp = zeroExtend({reqdata.hor,1'b0}) + zeroExtend({loadHorNum,2'b00});
250
      Bit#(TAdd#(PicHeightSz,9)) verTemp = zeroExtend(reqdata.ver) + zeroExtend(loadVerNum);
251
      if(reqdata.mvhor[13]==1 && zeroExtend(0-reqdata.mvhor[13:3])>horTemp)
252
         begin
253
            horAddr = 0;
254
            horOut = 1;
255
         end
256
      else
257
         begin
258
            horTemp = horTemp + signExtend(reqdata.mvhor[13:3]);
259
            if(horTemp>=zeroExtend({picWidth,3'b000}))
260
               begin
261
                  horAddr = {picWidth-1,1'b1};
262
                  horOut = 1;
263
               end
264
            else
265
               horAddr = truncate(horTemp>>2);
266
         end
267
      if(reqdata.mvver[11]==1 && zeroExtend(0-reqdata.mvver[11:3])>verTemp)
268
         verAddr = 0;
269
      else
270
         begin
271
            verTemp = verTemp + signExtend(reqdata.mvver[11:3]);
272
            if(verTemp>=zeroExtend({picHeight,3'b000}))
273
               verAddr = {picHeight-1,3'b111};
274
            else
275
               verAddr = truncate(verTemp);
276
         end
277
      memReqQ.enq(IPLoadChroma {refIdx:reqdata.refIdx,uv:reqdata.uv,horOutOfBounds:horOut,hor:horAddr,ver:verAddr});
278
      Bit#(2) loadHorNumMax = (reqdata.bt==IP4x8||reqdata.bt==IP4x4 ? (offset[1]==0||(xfracc==0&&offset!=3) ? 0 : 1) : ((reqdata.bt==IP16x16||reqdata.bt==IP16x8 ? 1 : 0) + (xfracc==0&&offset==0 ? 0 : 1)));
279
      Bit#(4) loadVerNumMax = (reqdata.bt==IP16x16||reqdata.bt==IP8x16 ? 7 : (reqdata.bt==IP16x8||reqdata.bt==IP8x8||reqdata.bt==IP4x8 ? 3 : 1)) + (yfracc==0 ? 0 : 1);
280
      if(loadHorNum < loadHorNumMax)
281
         loadHorNum <= loadHorNum+1;
282
      else
283
         begin
284
            loadHorNum <= 0;
285
            if(loadVerNum < loadVerNumMax)
286
               loadVerNum <= loadVerNum+1;
287
            else
288
               begin
289
                  loadVerNum <= 0;
290
                  reqfifoLoad.deq();
291
               end
292
         end
293
      $display( "PARDEB Trace interpolator: loadChroma %h %h %h %h %h %h %h", xfracc, yfracc, loadHorNum, loadVerNum, reqdata.refIdx, horAddr, verAddr);
294
   endrule
295
 
296
 
297
   rule work1Luma ( reqfifoWork1.first() matches tagged IPWLuma .reqdata &&& !work1Done );
298
      let xfracl = reqdata.xFracL;
299
      let yfracl = reqdata.yFracL;
300
      let offset = reqdata.offset;
301
      let blockT = reqdata.bt;
302
      Bool twoStage = (xfracl==1||xfracl==3) && (yfracl==1||yfracl==3);
303
      Vector#(20,Bit#(8)) work1Vector8Next = work1Vector8;
304
      if(memRespQ.first() matches tagged IPLoadResp .tempreaddata)
305
         begin
306
            memRespQ.deq();
307
            Vector#(4,Bit#(8)) readdata = replicate(0);
308
            readdata[0] = tempreaddata[7:0];
309
            readdata[1] = tempreaddata[15:8];
310
            readdata[2] = tempreaddata[23:16];
311
            readdata[3] = tempreaddata[31:24];
312
            //$display( "Trace interpolator: workLuma stage 0 readdata %h %h %h %h %h %h", workHorNum, workVerNum, readdata[3], readdata[2], readdata[1], readdata[0] );
313
            Vector#(4,Bit#(8)) tempResult8 = replicate(0);
314
            Vector#(4,Bit#(15)) tempResult15 = replicate(0);
315
            if(xfracl==0 || yfracl==0 || xfracl==2)
316
               begin
317
                  if(xfracl==0)//reorder
318
                     begin
319
                        for(Integer ii=0; ii<4; ii=ii+1)
320
                           begin
321
                              Bit#(2) offsetplusii = offset+fromInteger(ii);
322
                              if(offset <= 3-fromInteger(ii) && offset!=0)
323
                                 tempResult8[ii] = work1Vector8[offsetplusii];
324
                              else
325
                                 tempResult8[ii] = readdata[offsetplusii];
326
                              work1Vector8Next[ii] = readdata[ii];
327
                           end
328
                        for(Integer ii=0; ii<4; ii=ii+1)
329
                           tempResult15[ii] = zeroExtend({tempResult8[ii],5'b00000});
330
                     end
331
                  else//horizontal interpolation
332
                     begin
333
                        offset = offset-2;
334
                        for(Integer ii=0; ii<8; ii=ii+1)
335
                           work1Vector8Next[ii] = work1Vector8[ii+4];
336
                        for(Integer ii=0; ii<4; ii=ii+1)
337
                           begin
338
                              Bit#(4) tempIndex = fromInteger(ii) + 8 - zeroExtend(offset);
339
                              work1Vector8Next[tempIndex] = readdata[ii];
340
                           end
341
                        for(Integer ii=0; ii<4; ii=ii+1)
342
                           begin
343
                              tempResult15[ii] = interpolate8to15(work1Vector8Next[ii],work1Vector8Next[ii+1],work1Vector8Next[ii+2],work1Vector8Next[ii+3],work1Vector8Next[ii+4],work1Vector8Next[ii+5]);
344
                              tempResult8[ii] = clip1y10to8(truncate((tempResult15[ii]+16)>>5));
345
                              if(xfracl == 1)
346
                                 tempResult8[ii] = truncate(({1'b0,tempResult8[ii]} + {1'b0,work1Vector8Next[ii+2]} + 1) >> 1);
347
                              else if(xfracl == 3)
348
                                 tempResult8[ii] = truncate(({1'b0,tempResult8[ii]} + {1'b0,work1Vector8Next[ii+3]} + 1) >> 1);
349
                           end
350
                     end
351
                  Bit#(2) workHorNumOffset = (xfracl!=0 ? 2 : (reqdata.offset==0 ? 0 : 1));
352
                  if(work1HorNum >= workHorNumOffset)
353
                     begin
354
                        Bit#(1) horAddr = truncate(work1HorNum-workHorNumOffset);
355
                        if(yfracl == 0)
356
                           begin
357
                              for(Integer ii=0; ii<4; ii=ii+1)
358
                                 tempResult15[ii] = zeroExtend({tempResult8[ii],5'b00000});
359
                           end
360
                        workFile.upd({workFileFlag,work1VerNum,horAddr},tempResult15);
361
                     end
362
                  Bit#(2) workHorNumMax = (blockT==IP8x8||blockT==IP8x4 ? 1 : 0) + workHorNumOffset;
363
                  Bit#(4) workVerNumMax = (blockT==IP8x8||blockT==IP4x8 ? 7 : 3) + (yfracl!=0 ? 5 : 0);
364
                  if(work1HorNum < workHorNumMax)
365
                     work1HorNum <= work1HorNum+1;
366
                  else
367
                     begin
368
                        work1HorNum <= 0;
369
                        if(work1VerNum < workVerNumMax)
370
                           work1VerNum <= work1VerNum+1;
371
                        else
372
                           begin
373
                              work1VerNum <= 0;
374
                              work1Done <= True;
375
                           end
376
                     end
377
               end
378
            else if(work1Stage == 0)//vertical interpolation
379
               begin
380
                  offset = offset + (xfracl==3&&(yfracl==1||yfracl==3) ? 1 : 0);
381
                  for(Integer ii=0; ii<4; ii=ii+1)
382
                     tempResult15[ii] = interpolate8to15(work1Vector8[ii],work1Vector8[ii+4],work1Vector8[ii+8],work1Vector8[ii+12],work1Vector8[ii+16],readdata[ii]);
383
                  for(Integer ii=0; ii<16; ii=ii+1)
384
                     work1Vector8Next[ii] = work1Vector8[ii+4];
385
                  for(Integer ii=0; ii<4; ii=ii+1)
386
                     work1Vector8Next[ii+16] = readdata[ii];
387
                  Bit#(2) workHorNumMax = (blockT==IP8x8||blockT==IP8x4 ? 1 : 0) + (yfracl==2 ? 2 : (offset==0 ? 0 : 1));
388
                  Bit#(4) workVerNumMax = (blockT==IP8x8||blockT==IP4x8 ? 7 : 3) + 5;
389
                  Bit#(2) horAddr = work1HorNum;
390
                  Bit#(3) verAddr = truncate(work1VerNum-5);
391
                  if(work1VerNum > 4)
392
                     begin
393
                        workFile.upd({workFileFlag,verAddr,horAddr},tempResult15);
394
                        //$display( "Trace interpolator: workLuma stage 0 result %h %h %h %h %h %h %h", workHorNum, workVerNum, {verAddr,horAddr}, tempResult15[3], tempResult15[2], tempResult15[1], tempResult15[0]);
395
                     end
396
                  if(twoStage)
397
                     begin
398
                        Bit#(2) storeHorAddr = work1HorNum;
399
                        Bit#(4) storeVerAddr = work1VerNum;
400
                        if((xfracl==3 ? offset<3 : offset<2))
401
                           storeHorAddr = storeHorAddr+1;
402
                        if(yfracl==3)
403
                           storeVerAddr = storeVerAddr-3;
404
                        else
405
                           storeVerAddr = storeVerAddr-2;
406
                        if(storeVerAddr < 8)
407
                           storeFile.upd({workFileFlag,storeVerAddr[2:0],storeHorAddr},readdata);
408
                     end
409
                  if(work1VerNum < workVerNumMax)
410
                     work1VerNum <= work1VerNum+1;
411
                  else
412
                     begin
413
                        work1VerNum <= 0;
414
                        if(work1HorNum < workHorNumMax)
415
                           work1HorNum <= work1HorNum+1;
416
                        else
417
                           begin
418
                              if(twoStage)
419
                                 begin
420
                                    work1Stage <= 1;
421
                                    if((xfracl==3 ? offset<3 : offset<2))
422
                                       work1HorNum <= 0;
423
                                    else
424
                                       work1HorNum <= workHorNumMax+1;
425
                                 end
426
                              else
427
                                 begin
428
                                    work1HorNum <= 0;
429
                                    work1Done <= True;
430
                                 end
431
                           end
432
                     end
433
               end
434
            else//second stage of twoStage
435
               begin
436
                  storeFile.upd({workFileFlag,work1VerNum[2:0],work1HorNum},readdata);
437
                  Bit#(2) workHorNumMax = (blockT==IP8x8||blockT==IP8x4 ? 1 : 0) + 2;
438
                  Bit#(4) workVerNumMax = (blockT==IP8x8||blockT==IP4x8 ? 7 : 3);
439
                  if(work1VerNum < workVerNumMax)
440
                     work1VerNum <= work1VerNum+1;
441
                  else
442
                     begin
443
                        work1VerNum <= 0;
444
                        offset = offset + (xfracl==3 ? 1 : 0);
445
                        if(work1HorNum
446
                           work1HorNum <= workHorNumMax;
447
                        else
448
                           begin
449
                              work1HorNum <= 0;
450
                              work1Stage <= 0;
451
                              work1Done <= True;
452
                           end
453
                     end
454
               end
455
         end
456
      work1Vector8 <= work1Vector8Next;
457
      $display( "PARDEBLOCK Trace interpolator: work1Luma %h %h %h %h %h %h", xfracl, yfracl, work1HorNum, work1VerNum, offset, work1Stage);
458
   endrule
459
 
460
 
461
   rule work2Luma ( reqregWork2 matches tagged Valid .vdata &&& vdata matches tagged IPWLuma .reqdata &&& !work2Done &&& !work8x8Done );
462
      let xfracl = reqdata.xFracL;
463
      let yfracl = reqdata.yFracL;
464
      let offset = reqdata.offset;
465
      let blockT = reqdata.bt;
466
      Vector#(20,Bit#(8)) work2Vector8Next = work2Vector8;
467
      Vector#(20,Bit#(15)) work2Vector15Next = work2Vector15;
468
      Vector#(16,Bit#(1)) resultReadyNext = resultReady;
469
      Vector#(4,Bit#(8)) tempResult8 = replicate(0);
470
      Vector#(4,Bit#(15)) readdata = replicate(0);
471
      if(yfracl==0)
472
         begin
473
            readdata = workFile.sub({(1-workFileFlag),1'b0,work2VerNum[1],work2HorNum,work2VerNum[0]});
474
            for(Integer ii=0; ii<4; ii=ii+1)
475
               tempResult8[ii] = (readdata[ii])[12:5];
476
            resultFile.upd({work2VerNum[1],work2HorNum,work2VerNum[0]},tempResult8);
477
            resultReadyNext[{work2VerNum[1],work2HorNum,work2VerNum[0]}] = 1;
478
            work2HorNum <= work2HorNum+1;
479
            if(work2HorNum == 3)
480
               begin
481
                  if(work2VerNum == 3)
482
                     begin
483
                        work2VerNum <= 0;
484
                        work2Done <= True;
485
                        if(((blockT==IP4x8 || blockT==IP8x4) && work2SubMbPart==0) || (blockT==IP4x4 && work2SubMbPart<3))
486
                           work2SubMbPart <= work2SubMbPart+1;
487
                        else
488
                           begin
489
                              work2SubMbPart <= 0;
490
                              work8x8Done <= True;
491
                           end
492
                     end
493
                  else
494
                     work2VerNum <= work2VerNum+1;
495
               end
496
         end
497
      else if(xfracl==0 || xfracl==2)//vertical interpolation
498
         begin
499
            readdata = workFile.sub({(1-workFileFlag),work2VerNum,work2HorNum[0]});
500
            for(Integer ii=0; ii<4; ii=ii+1)
501
               begin
502
                  tempResult8[ii] = interpolate15to8(work2Vector15[ii],work2Vector15[ii+4],work2Vector15[ii+8],work2Vector15[ii+12],work2Vector15[ii+16],readdata[ii]);
503
                  if(yfracl == 1)
504
                     tempResult8[ii] = truncate(({1'b0,tempResult8[ii]} + {1'b0,clip1y10to8(truncate((work2Vector15[ii+8]+16)>>5))} + 1) >> 1);
505
                  else if(yfracl == 3)
506
                     tempResult8[ii] = truncate(({1'b0,tempResult8[ii]} + {1'b0,clip1y10to8(truncate((work2Vector15[ii+12]+16)>>5))} + 1) >> 1);
507
               end
508
            for(Integer ii=0; ii<16; ii=ii+1)
509
               work2Vector15Next[ii] = work2Vector15[ii+4];
510
            for(Integer ii=0; ii<4; ii=ii+1)
511
               work2Vector15Next[ii+16] = readdata[ii];
512
            Bit#(2) workHorNumMax = 1;
513
            Bit#(4) workVerNumMax = (blockT==IP8x8||blockT==IP4x8 ? 7 : 3) + 5;
514
            if(work2VerNum > 4)
515
               begin
516
                  Bit#(1) horAddr = truncate(work2HorNum);
517
                  Bit#(3) verAddr = truncate(work2VerNum-5);
518
                  horAddr = horAddr + ((blockT==IP4x8&&work2SubMbPart==1)||(blockT==IP4x4&&work2SubMbPart[0]==1) ? 1 : 0);
519
                  verAddr = verAddr + ((blockT==IP8x4&&work2SubMbPart==1)||(blockT==IP4x4&&work2SubMbPart[1]==1) ? 4 : 0);
520
                  resultFile.upd({verAddr,horAddr},tempResult8);
521
                  resultReadyNext[{verAddr,horAddr}] = 1;
522
               end
523
            if(work2VerNum < workVerNumMax)
524
               work2VerNum <= work2VerNum+1;
525
            else
526
               begin
527
                  work2VerNum <= 0;
528
                  if(work2HorNum < workHorNumMax)
529
                     work2HorNum <= work2HorNum+1;
530
                  else
531
                     begin
532
                        work2HorNum <= 0;
533
                        work2Done <= True;
534
                        if(((blockT==IP4x8 || blockT==IP8x4) && work2SubMbPart==0) || (blockT==IP4x4 && work2SubMbPart<3))
535
                           work2SubMbPart <= work2SubMbPart+1;
536
                        else
537
                           begin
538
                              work2SubMbPart <= 0;
539
                              work8x8Done <= True;
540
                           end
541
                     end
542
               end
543
         end
544
      else//horizontal interpolation
545
         begin
546
            offset = offset-2;
547
            if(yfracl == 2)
548
               begin
549
                  readdata = workFile.sub({(1-workFileFlag),work2VerNum[2:0],work2HorNum});
550
                  for(Integer ii=0; ii<8; ii=ii+1)
551
                     work2Vector15Next[ii] = work2Vector15[ii+4];
552
                  for(Integer ii=0; ii<4; ii=ii+1)
553
                     begin
554
                        Bit#(4) tempIndex = fromInteger(ii) + 8 - zeroExtend(offset);
555
                        work2Vector15Next[tempIndex] = readdata[ii];
556
                     end
557
                  for(Integer ii=0; ii<4; ii=ii+1)
558
                     begin
559
                        tempResult8[ii] = interpolate15to8(work2Vector15Next[ii],work2Vector15Next[ii+1],work2Vector15Next[ii+2],work2Vector15Next[ii+3],work2Vector15Next[ii+4],work2Vector15Next[ii+5]);
560
                        if(xfracl == 1)
561
                           tempResult8[ii] = truncate(({1'b0,tempResult8[ii]} + {1'b0,clip1y10to8(truncate((work2Vector15Next[ii+2]+16)>>5))} + 1) >> 1);
562
                        else if(xfracl == 3)
563
                           tempResult8[ii] = truncate(({1'b0,tempResult8[ii]} + {1'b0,clip1y10to8(truncate((work2Vector15Next[ii+3]+16)>>5))} + 1) >> 1);
564
                     end
565
               end
566
            else
567
               begin
568
                  Vector#(4,Bit#(8)) readdata8 = storeFile.sub({(1-workFileFlag),work2VerNum[2:0],work2HorNum});
569
                  for(Integer ii=0; ii<8; ii=ii+1)
570
                     work2Vector8Next[ii] = work2Vector8[ii+4];
571
                  for(Integer ii=0; ii<4; ii=ii+1)
572
                     begin
573
                        Bit#(4) tempIndex = fromInteger(ii) + 8 - zeroExtend(offset);
574
                        work2Vector8Next[tempIndex] = readdata8[ii];
575
                     end
576
                  Vector#(4,Bit#(15)) tempResult15 = replicate(0);
577
                  for(Integer ii=0; ii<4; ii=ii+1)
578
                     begin
579
                        tempResult15[ii] = interpolate8to15(work2Vector8Next[ii],work2Vector8Next[ii+1],work2Vector8Next[ii+2],work2Vector8Next[ii+3],work2Vector8Next[ii+4],work2Vector8Next[ii+5]);
580
                        tempResult8[ii] = clip1y10to8(truncate((tempResult15[ii]+16)>>5));
581
                     end
582
                  Bit#(2) verOffset;
583
                  Vector#(4,Bit#(15)) verResult15 = replicate(0);
584
                  if(xfracl == 1)
585
                     verOffset = reqdata.offset;
586
                  else
587
                     verOffset = reqdata.offset+1;
588
                  readdata = workFile.sub({(1-workFileFlag),work2VerNum[2:0],(work2HorNum-2+(verOffset==0?0:1))});
589
                  for(Integer ii=0; ii<4; ii=ii+1)
590
                     begin
591
                        Bit#(2) offsetplusii = verOffset+fromInteger(ii);
592
                        if(verOffset <= 3-fromInteger(ii) && verOffset!=0)
593
                           verResult15[ii] = work2Vector15[offsetplusii];
594
                        else
595
                           verResult15[ii] = readdata[offsetplusii];
596
                        work2Vector15Next[ii] = readdata[ii];
597
                     end
598
                  for(Integer ii=0; ii<4; ii=ii+1)
599
                     begin
600
                        Bit#(9) tempVal = zeroExtend(clip1y10to8(truncate((verResult15[ii]+16)>>5)));
601
                        tempResult8[ii] = truncate((tempVal+zeroExtend(tempResult8[ii])+1)>>1);
602
                     end
603
               end
604
            if(work2HorNum >= 2)
605
               begin
606
                  Bit#(1) horAddr = truncate(work2HorNum-2);
607
                  Bit#(3) verAddr = truncate(work2VerNum);
608
                  horAddr = horAddr + ((blockT==IP4x8&&work2SubMbPart==1)||(blockT==IP4x4&&work2SubMbPart[0]==1) ? 1 : 0);
609
                  verAddr = verAddr + ((blockT==IP8x4&&work2SubMbPart==1)||(blockT==IP4x4&&work2SubMbPart[1]==1) ? 4 : 0);
610
                  resultFile.upd({verAddr,horAddr},tempResult8);
611
                  resultReadyNext[{verAddr,horAddr}] = 1;
612
                  //$display( "Trace interpolator: workLuma stage 1 result %h %h %h %h %h %h %h %h", workHorNum, workVerNum, {verAddr,horAddr}, tempResult8[3], tempResult8[2], tempResult8[1], tempResult8[0], pack(resultReadyNext));
613
               end
614
            Bit#(2) workHorNumMax = (blockT==IP8x8||blockT==IP8x4 ? 1 : 0) + 2;
615
            Bit#(4) workVerNumMax = (blockT==IP8x8||blockT==IP4x8 ? 7 : 3);
616
            if(work2HorNum < workHorNumMax)
617
               work2HorNum <= work2HorNum+1;
618
            else
619
               begin
620
                  work2HorNum <= 0;
621
                  if(work2VerNum < workVerNumMax)
622
                     work2VerNum <= work2VerNum+1;
623
                  else
624
                     begin
625
                        work2VerNum <= 0;
626
                        work2Done <= True;
627
                        if(((blockT==IP4x8 || blockT==IP8x4) && work2SubMbPart==0) || (blockT==IP4x4 && work2SubMbPart<3))
628
                           work2SubMbPart <= work2SubMbPart+1;
629
                        else
630
                           begin
631
                              work2SubMbPart <= 0;
632
                              work8x8Done <= True;
633
                           end
634
                     end
635
               end
636
         end
637
      work2Vector8 <= work2Vector8Next;
638
      work2Vector15 <= work2Vector15Next;
639
      resultReady <= resultReadyNext;
640
      $display( "PARDEBLOCK Trace interpolator: work2Luma %h %h %h %h %h", xfracl, yfracl, work2HorNum, work2VerNum, offset);
641
   endrule
642
 
643
 
644
   rule work1Chroma ( reqfifoWork1.first() matches tagged IPWChroma .reqdata &&& !work1Done );
645
      Bit#(4) xfracc = zeroExtend(reqdata.xFracC);
646
      Bit#(4) yfracc = zeroExtend(reqdata.yFracC);
647
      let offset = reqdata.offset;
648
      let blockT = reqdata.bt;
649
      Vector#(20,Bit#(8)) work1Vector8Next = work1Vector8;
650
      if(memRespQ.first() matches tagged IPLoadResp .tempreaddata)
651
         begin
652
            memRespQ.deq();
653
            Vector#(4,Bit#(8)) readdata = replicate(0);
654
            readdata[0] = tempreaddata[7:0];
655
            readdata[1] = tempreaddata[15:8];
656
            readdata[2] = tempreaddata[23:16];
657
            readdata[3] = tempreaddata[31:24];
658
            Vector#(5,Bit#(8)) tempWork8 = replicate(0);
659
            Vector#(5,Bit#(8)) tempPrev8 = replicate(0);
660
            Vector#(4,Bit#(8)) tempResult8 = replicate(0);
661
            Bool resultReadyFlag = False;
662
            for(Integer ii=0; ii<4; ii=ii+1)
663
               begin
664
                  Bit#(2) offsetplusii = offset+fromInteger(ii);
665
                  if(offset <= 3-fromInteger(ii) && !((blockT==IP4x8||blockT==IP4x4)&&(offset[1]==0||(xfracc==0&&offset!=3))) && !(xfracc==0&&offset==0))
666
                     tempWork8[ii] = work1Vector8[offsetplusii];
667
                  else
668
                     tempWork8[ii] = readdata[offsetplusii];
669
                  work1Vector8Next[ii] = readdata[ii];
670
               end
671
            tempWork8[4] = readdata[offset];
672
            if((blockT==IP16x8 || blockT==IP16x16) && work1HorNum==(xfracc==0&&offset==0 ? 1 : 2))
673
               begin
674
                  for(Integer ii=0; ii<5; ii=ii+1)
675
                     begin
676
                        tempPrev8[ii] = work1Vector8[ii+9];
677
                        work1Vector8Next[ii+9] = tempWork8[ii];
678
                     end
679
               end
680
            else
681
               begin
682
                  for(Integer ii=0; ii<5; ii=ii+1)
683
                     tempPrev8[ii] = work1Vector8[ii+4];
684
                  if(work1HorNum==(xfracc==0&&offset==0 ? 0 : 1) || ((blockT==IP4x8||blockT==IP4x4)&&(offset[1]==0||(xfracc==0&&offset!=3))))
685
                     begin
686
                        for(Integer ii=0; ii<5; ii=ii+1)
687
                           work1Vector8Next[ii+4] = tempWork8[ii];
688
                     end
689
               end
690
            if(yfracc==0)
691
               begin
692
                  for(Integer ii=0; ii<5; ii=ii+1)
693
                     tempPrev8[ii] = tempWork8[ii];
694
               end
695
            for(Integer ii=0; ii<4; ii=ii+1)
696
               begin
697
                  Bit#(14) tempVal = zeroExtend((8-xfracc))*zeroExtend((8-yfracc))*zeroExtend(tempPrev8[ii]);
698
                  tempVal = tempVal + zeroExtend(xfracc)*zeroExtend((8-yfracc))*zeroExtend(tempPrev8[ii+1]);
699
                  tempVal = tempVal + zeroExtend((8-xfracc))*zeroExtend(yfracc)*zeroExtend(tempWork8[ii]);
700
                  tempVal = tempVal + zeroExtend(xfracc)*zeroExtend(yfracc)*zeroExtend(tempWork8[ii+1]);
701
                  tempResult8[ii] = truncate((tempVal+32)>>6);
702
               end
703
            if(work1VerNum > 0 || yfracc==0)
704
               begin
705
                  if(blockT==IP4x8 || blockT==IP4x4)
706
                     begin
707
                        Bit#(5) tempIndex = 10 + zeroExtend(work1VerNum<<1);
708
                        work1Vector8Next[tempIndex] = tempResult8[0];
709
                        work1Vector8Next[tempIndex+1] = tempResult8[1];
710
                        tempResult8[2] = tempResult8[0];
711
                        tempResult8[3] = tempResult8[1];
712
                        tempResult8[0] = work1Vector8[tempIndex];
713
                        tempResult8[1] = work1Vector8[tempIndex+1];
714
                        if((work1HorNum>0 || offset[1]==0) && work1SubMbPart[0]==1)
715
                           resultReadyFlag = True;
716
                     end
717
                  else
718
                     begin
719
                        if(work1HorNum>0 || (xfracc==0 && offset==0))
720
                           resultReadyFlag = True;
721
                     end
722
               end
723
            if(resultReadyFlag)
724
               begin
725
                  Bit#(1) horAddr = ((blockT==IP4x8 || blockT==IP4x4) ? 0 : truncate(((xfracc==0 && offset==0) ? work1HorNum : work1HorNum-1)));
726
                  Bit#(3) verAddr = truncate((yfracc==0 ? work1VerNum : work1VerNum-1));
727
                  horAddr = horAddr + ((blockT==IP16x8||blockT==IP16x16) ? 0 : work1MbPart[0]);
728
                  verAddr = verAddr + ((blockT==IP8x16||blockT==IP16x16) ? 0 : ((blockT==IP16x8) ? {work1MbPart[0],2'b00} : {work1MbPart[1],2'b00}));
729
                  verAddr = verAddr + ((blockT==IP8x4&&work1SubMbPart==1)||(blockT==IP4x4&&work1SubMbPart[1]==1) ? 2 : 0);
730
                  storeFile.upd({workFileFlag,1'b0,verAddr,horAddr},tempResult8);
731
               end
732
            Bit#(2) workHorNumMax = (blockT==IP4x8||blockT==IP4x4 ? (offset[1]==0||(xfracc==0&&offset!=3) ? 0 : 1) : ((blockT==IP16x16||blockT==IP16x8 ? 1 : 0) + (xfracc==0&&offset==0 ? 0 : 1)));
733
            Bit#(4) workVerNumMax = (blockT==IP16x16||blockT==IP8x16 ? 7 : (blockT==IP16x8||blockT==IP8x8||blockT==IP4x8 ? 3 : 1)) + (yfracc==0 ? 0 : 1);
734
            if(work1HorNum < workHorNumMax)
735
               work1HorNum <= work1HorNum+1;
736
            else
737
               begin
738
                  work1HorNum <= 0;
739
                  if(work1VerNum < workVerNumMax)
740
                     work1VerNum <= work1VerNum+1;
741
                  else
742
                     begin
743
                        Bool allDone = False;
744
                        work1VerNum <= 0;
745
                        if(((blockT==IP4x8 || blockT==IP8x4) && work1SubMbPart==0) || (blockT==IP4x4 && work1SubMbPart<3))
746
                           work1SubMbPart <= work1SubMbPart+1;
747
                        else
748
                           begin
749
                              work1SubMbPart <= 0;
750
                              if(((blockT==IP16x8 || blockT==IP8x16) && work1MbPart==0) || (!(blockT==IP16x8 || blockT==IP8x16 || blockT==IP16x16) && work1MbPart<3))
751
                                 work1MbPart <= work1MbPart+1;
752
                              else
753
                                 begin
754
                                    work1MbPart <= 0;
755
                                    work1Done <= True;
756
                                    allDone = True;
757
                                 end
758
                           end
759
                        if(!allDone)
760
                           reqfifoWork1.deq();
761
                     end
762
               end
763
         end
764
      work1Vector8 <= work1Vector8Next;
765
      $display( "PARDEBLOCK Trace interpolator: work1Chroma %h %h %h %h %h", xfracc, yfracc, work1HorNum, work1VerNum, offset);
766
   endrule
767
 
768
 
769
   rule work2Chroma ( reqregWork2 matches tagged Valid .vdata &&& vdata matches tagged IPWChroma .reqdata &&& !work2Done &&& !work8x8Done );
770
      Vector#(16,Bit#(1)) resultReadyNext = resultReady;
771
      resultFile.upd({work2VerNum[1],work2HorNum,work2VerNum[0]},storeFile.sub({(1-workFileFlag),1'b0,work2VerNum[1],work2HorNum,work2VerNum[0]}));
772
      resultReadyNext[{work2VerNum[1],work2HorNum,work2VerNum[0]}] = 1;
773
      work2HorNum <= work2HorNum+1;
774
      if(work2HorNum == 3)
775
         begin
776
            if(work2VerNum == 3)
777
               begin
778
                  work2VerNum <= 0;
779
                  work2Done <= True;
780
                  work8x8Done <= True;
781
               end
782
            else
783
               work2VerNum <= work2VerNum+1;
784
         end
785
      resultReady <= resultReadyNext;
786
      $display( "PARDEBLOCK Trace interpolator: work2Chroma %h %h", work2HorNum, work2VerNum);
787
   endrule
788
 
789
 
790
  rule outputing( !outDone && resultReady[{outBlockNum[1],outPixelNum,outBlockNum[0]}]==1 );
791
      outfifo.enq(resultFile.sub({outBlockNum[1],outPixelNum,outBlockNum[0]}));
792
      outPixelNum <= outPixelNum+1;
793
      if(outPixelNum == 3)
794
         begin
795
            outBlockNum <= outBlockNum+1;
796
            if(outBlockNum == 3)
797
               outDone <= True;
798
         end
799
      $display( "Trace interpolator: outputing %h %h", outBlockNum, outPixelNum);
800
   endrule
801
 
802
 
803
   rule switching( work1Done && (work2Done || reqregWork2==Invalid) && !work8x8Done);
804
      work1Done <= False;
805
      work2Done <= False;
806
      reqregWork2 <= (tagged Valid reqfifoWork1.first());
807
      workFileFlag <= 1-workFileFlag;
808
      reqfifoWork1.deq();
809
      $display( "Trace interpolator: switching %h %h", outBlockNum, outPixelNum);
810
   endrule
811
 
812
 
813
   rule switching8x8( work1Done && (work2Done || reqregWork2==Invalid) && work8x8Done && outDone);
814
      outDone <= False;
815
      work8x8Done <= False;
816
      resultReady <= replicate(0);
817
      work1Done <= False;
818
      work2Done <= False;
819
      reqregWork2 <= (tagged Valid reqfifoWork1.first());
820
      workFileFlag <= 1-workFileFlag;
821
      reqfifoWork1.deq();
822
      $display( "Trace interpolator: switching8x8 %h %h", outBlockNum, outPixelNum);
823
   endrule
824
 
825
 
826
 
827
   method Action   setPicWidth( Bit#(PicWidthSz) newPicWidth );
828
      picWidth <= newPicWidth;
829
   endmethod
830
 
831
   method Action   setPicHeight( Bit#(PicHeightSz) newPicHeight );
832
      picHeight <= newPicHeight;
833
   endmethod
834
 
835
   method Action request( InterpolatorIT inputdata );
836
      reqfifoLoad.enq(inputdata);
837
      if(inputdata matches tagged IPLuma .indata)
838
         reqfifoWork1.enq(IPWLuma {xFracL:indata.mvhor[1:0],yFracL:indata.mvver[1:0],offset:indata.mvhor[3:2],bt:indata.bt});
839
      else if(inputdata matches tagged IPChroma .indata)
840
         reqfifoWork1.enq(IPWChroma {xFracC:indata.mvhor[2:0],yFracC:indata.mvver[2:0],offset:indata.mvhor[4:3]+{indata.hor[0],1'b0},bt:indata.bt});
841
   endmethod
842
 
843
   method Vector#(4,Bit#(8)) first();
844
      return outfifo.first();
845
   endmethod
846
 
847
   method Action deq();
848
      outfifo.deq();
849
   endmethod
850
 
851
   method Action endOfFrame();
852
      endOfFrameFlag <= True;
853
   endmethod
854
 
855
   interface Client mem_client;
856
      interface Get request  = fifoToGet(memReqQ);
857
      interface Put response = fifoToPut(memRespQ);
858
   endinterface
859
 
860
 
861
endmodule
862
 
863
 
864
endpackage

powered by: WebSVN 2.1.0

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