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

Subversion Repositories bluespec-h264

[/] [bluespec-h264/] [trunk/] [src/] [mkInterpolator_4stage.bsv] - Blame information for rev 27

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

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

powered by: WebSVN 2.1.0

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