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 60

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

powered by: WebSVN 2.1.0

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