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

Subversion Repositories bluespec-h264

[/] [bluespec-h264/] [trunk/] [src/] [mkInterpolator.bsv] - Blame information for rev 15

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

powered by: WebSVN 2.1.0

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