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

Subversion Repositories bluespec-h264

[/] [bluespec-h264/] [trunk/] [src_fpga/] [mkInterpolator.bsv] - Blame information for rev 100

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 83 jamey.hick
 
2
// The MIT License
3
 
4
// Copyright (c) 2006-2007 Massachusetts Institute of Technology
5
 
6
// Permission is hereby granted, free of charge, to any person obtaining a copy
7
// of this software and associated documentation files (the "Software"), to deal
8
// in the Software without restriction, including without limitation the rights
9
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10
// copies of the Software, and to permit persons to whom the Software is
11
// furnished to do so, subject to the following conditions:
12
 
13
// The above copyright notice and this permission notice shall be included in
14
// all copies or substantial portions of the Software.
15
 
16
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22
// THE SOFTWARE.
23
 
24 3 jamey.hick
//**********************************************************************
25
// interpolator implementation
26
//----------------------------------------------------------------------
27
//
28
//
29
 
30
package mkInterpolator;
31
 
32
import H264Types::*;
33
import IInterpolator::*;
34
import FIFO::*;
35
import Vector::*;
36
 
37
import Connectable::*;
38
import GetPut::*;
39
import ClientServer::*;
40
 
41
 
42
//-----------------------------------------------------------
43
// Local Datatypes
44
//-----------------------------------------------------------
45
 
46
typedef union tagged
47
{
48
 struct { Bit#(2) xFracL; Bit#(2) yFracL; Bit#(2) offset; IPBlockType bt; } IPWLuma;
49
 struct { Bit#(3) xFracC; Bit#(3) yFracC; Bit#(2) offset; IPBlockType bt; } IPWChroma;
50
}
51
InterpolatorWT deriving(Eq,Bits);
52
 
53
 
54
//-----------------------------------------------------------
55
// Helper functions
56
 
57
function Bit#(8) clip1y10to8( Bit#(10) innum );
58
   if(innum[9] == 1)
59
      return 0;
60
   else if(innum[8] == 1)
61
      return 255;
62
   else
63
      return truncate(innum);
64
endfunction
65
 
66
function Bit#(15) interpolate8to15( Bit#(8) in0, Bit#(8) in1, Bit#(8) in2, Bit#(8) in3, Bit#(8) in4, Bit#(8) in5 );
67
   return zeroExtend(in0) - 5*zeroExtend(in1) + 20*zeroExtend(in2) + 20*zeroExtend(in3) - 5*zeroExtend(in4) + zeroExtend(in5);
68
endfunction
69
 
70
function Bit#(8) interpolate15to8( Bit#(15) in0, Bit#(15) in1, Bit#(15) in2, Bit#(15) in3, Bit#(15) in4, Bit#(15) in5 );
71
   Bit#(20) temp = signExtend(in0) - 5*signExtend(in1) + 20*signExtend(in2) + 20*signExtend(in3) - 5*signExtend(in4) + signExtend(in5) + 512;
72
   return clip1y10to8(truncate(temp>>10));
73
endfunction
74
 
75
 
76
 
77
//-----------------------------------------------------------
78
// Interpolation Module
79
//-----------------------------------------------------------
80
 
81
 
82
(* synthesize *)
83
module mkInterpolator( Interpolator );
84
 
85
   FIFO#(InterpolatorIT) reqfifoLoad <- mkSizedFIFO(interpolator_reqfifoLoad_size);
86
   FIFO#(InterpolatorWT) reqfifoWork <- mkSizedFIFO(interpolator_reqfifoWork_size);
87
   FIFO#(Vector#(4,Bit#(8))) outfifo <- mkFIFO;
88
   Reg#(Bool) endOfFrameFlag <- mkReg(False);
89
   FIFO#(InterpolatorLoadReq)  memReqQ  <- mkFIFO;
90
   FIFO#(InterpolatorLoadResp) memRespQ <- mkSizedFIFO(interpolator_memRespQ_size);
91
 
92
   Reg#(Bit#(PicWidthSz))  picWidth  <- mkReg(maxPicWidthInMB);
93
   Reg#(Bit#(PicHeightSz)) picHeight <- mkReg(0);
94
 
95
   RFile1#(Bit#(5),Vector#(4,Bit#(15))) workFile  <- mkRFile1Full();
96
   RFile1#(Bit#(4),Vector#(4,Bit#(8))) resultFile <- mkRFile1Full();
97
 
98
   Reg#(Bit#(1)) loadStage  <- mkReg(0);
99
   Reg#(Bit#(2)) loadHorNum <- mkReg(0);
100
   Reg#(Bit#(4)) loadVerNum <- mkReg(0);
101
 
102
   Reg#(Bit#(1)) workStage     <- mkReg(0);
103
   Reg#(Bit#(2)) workMbPart    <- mkReg(0);//only for Chroma
104
   Reg#(Bit#(2)) workSubMbPart <- mkReg(0);
105
   Reg#(Bit#(2)) workHorNum    <- mkReg(0);
106
   Reg#(Bit#(4)) workVerNum    <- mkReg(0);
107
   Reg#(Vector#(20,Bit#(8))) workVector8 <- mkRegU;
108
   Reg#(Vector#(20,Bit#(15))) workVector15 <- mkRegU;
109
   Reg#(Vector#(4,Bit#(1))) resultReady <- mkRegU;
110
   Reg#(Bool) workDone <- mkReg(False);
111
 
112
   Reg#(Bit#(2)) outBlockNum <- mkReg(0);
113
   Reg#(Bit#(2)) outPixelNum <- mkReg(0);
114
   Reg#(Bool) outDone <- mkReg(False);
115
 
116
 
117
   rule sendEndOfFrameReq( endOfFrameFlag );
118
      endOfFrameFlag <= False;
119
      memReqQ.enq(IPLoadEndFrame);
120
   endrule
121
 
122
 
123
   rule loadLuma( reqfifoLoad.first() matches tagged IPLuma .reqdata &&& !endOfFrameFlag );
124
      Bit#(2) xfracl = reqdata.mvhor[1:0];
125
      Bit#(2) yfracl = reqdata.mvver[1:0];
126
      Bool twoStage = (xfracl==1||xfracl==3) && (yfracl==1||yfracl==3);
127
      Bool horInter = (twoStage ? loadStage==1 : xfracl!=0);
128
      Bool verInter = (twoStage ? loadStage==0 : yfracl!=0);
129
      Bit#(1) horOut = 0;
130
      Bit#(2) offset = reqdata.mvhor[3:2] + ((twoStage&&verInter&&xfracl==3) ? 1 : 0);
131
      Bit#(TAdd#(PicWidthSz,2)) horAddr;
132
      Bit#(TAdd#(PicHeightSz,4)) verAddr;
133
      Bit#(TAdd#(PicWidthSz,12)) horTemp = zeroExtend({reqdata.hor,2'b00}) + zeroExtend({loadHorNum,2'b00}) + (xfracl==3&&(yfracl==1||yfracl==3)&&loadStage==0 ? 1 : 0);
134
      Bit#(TAdd#(PicHeightSz,10)) verTemp = zeroExtend(reqdata.ver) + zeroExtend(loadVerNum) + (yfracl==3&&(xfracl==1||xfracl==3)&&loadStage==1 ? 1 : 0);
135
      Bit#(13) mvhortemp = signExtend(reqdata.mvhor[13:2])-(horInter?2:0);
136
      Bit#(11) mvvertemp = signExtend(reqdata.mvver[11:2])-(verInter?2:0);
137
      if(mvhortemp[12]==1 && zeroExtend(0-mvhortemp)>horTemp)
138
         begin
139
            horAddr = 0;
140
            horOut = 1;
141
         end
142
      else
143
         begin
144
            horTemp = horTemp + signExtend(mvhortemp);
145
            if(horTemp>=zeroExtend({picWidth,4'b0000}))
146
               begin
147
                  horAddr = {picWidth-1,2'b11};
148
                  horOut = 1;
149
               end
150
            else
151
               horAddr = truncate(horTemp>>2);
152
         end
153
      if(mvvertemp[10]==1 && zeroExtend(0-mvvertemp)>verTemp)
154
         verAddr = 0;
155
      else
156
         begin
157
            verTemp = verTemp + signExtend(mvvertemp);
158
            if(verTemp>=zeroExtend({picHeight,4'b0000}))
159
               verAddr = {picHeight-1,4'b1111};
160
            else
161
               verAddr = truncate(verTemp);
162
         end
163
      memReqQ.enq(IPLoadLuma {refIdx:reqdata.refIdx,horOutOfBounds:horOut,hor:horAddr,ver:verAddr});
164
      Bool verFirst = (twoStage&&loadStage==0) || (yfracl==2&&(xfracl==1||xfracl==3));
165
      Bit#(2) loadHorNumMax = (reqdata.bt==IP8x8||reqdata.bt==IP8x4 ? 1 : 0) + (horInter ? 2 : (offset==0 ? 0 : 1));
166
      Bit#(4) loadVerNumMax = (reqdata.bt==IP8x8||reqdata.bt==IP4x8 ? 7 : 3) + (verInter ? 5 : 0);
167
      if(verFirst)
168
         begin
169
            if(loadVerNum < loadVerNumMax)
170
               loadVerNum <= loadVerNum+1;
171
            else
172
               begin
173
                  loadVerNum <= 0;
174
                  if(loadHorNum < loadHorNumMax)
175
                     loadHorNum <= loadHorNum+1;
176
                  else
177
                     begin
178
                        loadHorNum <= 0;
179
                        if(twoStage)
180
                           loadStage <= 1;
181
                        else
182
                           reqfifoLoad.deq();
183
                     end
184
               end
185
         end
186
      else
187
         begin
188
            if(loadHorNum < loadHorNumMax)
189
               loadHorNum <= loadHorNum+1;
190
            else
191
               begin
192
                  loadHorNum <= 0;
193
                  if(loadVerNum < loadVerNumMax)
194
                     loadVerNum <= loadVerNum+1;
195
                  else
196
                     begin
197
                        loadVerNum <= 0;
198
                        loadStage <= 0;
199
                        reqfifoLoad.deq();
200
                     end
201
               end
202
         end
203
      if(reqdata.bt==IP16x16 || reqdata.bt==IP16x8 || reqdata.bt==IP8x16)
204
         $display( "ERROR Interpolation: loadLuma block sizes > 8x8 not supported");
205
      //$display( "Trace interpolator: loadLuma %h %h %h %h %h %h %h", xfracl, yfracl, loadHorNum, loadVerNum, reqdata.refIdx, horAddr, verAddr);
206
   endrule
207
 
208
 
209
   rule loadChroma( reqfifoLoad.first() matches tagged IPChroma .reqdata &&& !endOfFrameFlag );
210
      Bit#(3) xfracc = reqdata.mvhor[2:0];
211
      Bit#(3) yfracc = reqdata.mvver[2:0];
212
      Bit#(2) offset = reqdata.mvhor[4:3]+{reqdata.hor[0],1'b0};
213
      Bit#(1) horOut = 0;
214
      Bit#(TAdd#(PicWidthSz,1)) horAddr;
215
      Bit#(TAdd#(PicHeightSz,3)) verAddr;
216
      Bit#(TAdd#(PicWidthSz,11)) horTemp = zeroExtend({reqdata.hor,1'b0}) + zeroExtend({loadHorNum,2'b00});
217
      Bit#(TAdd#(PicHeightSz,9)) verTemp = zeroExtend(reqdata.ver) + zeroExtend(loadVerNum);
218
      if(reqdata.mvhor[13]==1 && zeroExtend(0-reqdata.mvhor[13:3])>horTemp)
219
         begin
220
            horAddr = 0;
221
            horOut = 1;
222
         end
223
      else
224
         begin
225
            horTemp = horTemp + signExtend(reqdata.mvhor[13:3]);
226
            if(horTemp>=zeroExtend({picWidth,3'b000}))
227
               begin
228
                  horAddr = {picWidth-1,1'b1};
229
                  horOut = 1;
230
               end
231
            else
232
               horAddr = truncate(horTemp>>2);
233
         end
234
      if(reqdata.mvver[11]==1 && zeroExtend(0-reqdata.mvver[11:3])>verTemp)
235
         verAddr = 0;
236
      else
237
         begin
238
            verTemp = verTemp + signExtend(reqdata.mvver[11:3]);
239
            if(verTemp>=zeroExtend({picHeight,3'b000}))
240
               verAddr = {picHeight-1,3'b111};
241
            else
242
               verAddr = truncate(verTemp);
243
         end
244
      memReqQ.enq(IPLoadChroma {refIdx:reqdata.refIdx,uv:reqdata.uv,horOutOfBounds:horOut,hor:horAddr,ver:verAddr});
245
      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)));
246
      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);
247
      if(loadHorNum < loadHorNumMax)
248
         loadHorNum <= loadHorNum+1;
249
      else
250
         begin
251
            loadHorNum <= 0;
252
            if(loadVerNum < loadVerNumMax)
253
               loadVerNum <= loadVerNum+1;
254
            else
255
               begin
256
                  loadVerNum <= 0;
257
                  reqfifoLoad.deq();
258
               end
259
         end
260
      //$display( "Trace interpolator: loadChroma %h %h %h %h %h %h %h", xfracc, yfracc, loadHorNum, loadVerNum, reqdata.refIdx, horAddr, verAddr);
261
   endrule
262
 
263
 
264
   rule workLuma ( reqfifoWork.first() matches tagged IPWLuma .reqdata &&& !workDone );
265
      let xfracl = reqdata.xFracL;
266
      let yfracl = reqdata.yFracL;
267
      let offset = reqdata.offset;
268
      let blockT = reqdata.bt;
269
      Vector#(20,Bit#(8)) workVector8Next = workVector8;
270
      Vector#(20,Bit#(15)) workVector15Next = workVector15;
271
      Vector#(4,Bit#(1)) resultReadyNext = resultReady;
272
      if(workStage == 0)
273
         begin
274
            if(memRespQ.first() matches tagged IPLoadResp .tempreaddata)
275
               begin
276
                  memRespQ.deq();
277
                  Vector#(4,Bit#(8)) readdata = replicate(0);
278
                  readdata[0] = tempreaddata[7:0];
279
                  readdata[1] = tempreaddata[15:8];
280
                  readdata[2] = tempreaddata[23:16];
281
                  readdata[3] = tempreaddata[31:24];
282
                  //$display( "Trace interpolator: workLuma stage 0 readdata %h %h %h %h %h %h", workHorNum, workVerNum, readdata[3], readdata[2], readdata[1], readdata[0] );
283
                  Vector#(4,Bit#(8)) tempResult8 = replicate(0);
284
                  Vector#(4,Bit#(15)) tempResult15 = replicate(0);
285
                  if(xfracl==0 || yfracl==0 || xfracl==2)
286
                     begin
287
                        if(xfracl==0)//reorder
288
                           begin
289
                              for(Integer ii=0; ii<4; ii=ii+1)
290
                                 begin
291
                                    Bit#(2) offsetplusii = offset+fromInteger(ii);
292
                                    if(offset <= 3-fromInteger(ii) && offset!=0)
293
                                       tempResult8[ii] = workVector8[offsetplusii];
294
                                    else
295
                                       tempResult8[ii] = readdata[offsetplusii];
296
                                    workVector8Next[ii] = readdata[ii];
297
                                 end
298
                              for(Integer ii=0; ii<4; ii=ii+1)
299
                                 tempResult15[ii] = zeroExtend({tempResult8[ii],5'b00000});
300
                           end
301
                        else//horizontal interpolation
302
                           begin
303
                              offset = offset-2;
304
                              for(Integer ii=0; ii<8; ii=ii+1)
305
                                 workVector8Next[ii] = workVector8[ii+4];
306
                              for(Integer ii=0; ii<4; ii=ii+1)
307
                                 begin
308
                                    Bit#(4) tempIndex = fromInteger(ii) + 8 - zeroExtend(offset);
309
                                    workVector8Next[tempIndex] = readdata[ii];
310
                                 end
311
                              for(Integer ii=0; ii<4; ii=ii+1)
312
                                 begin
313
                                    tempResult15[ii] = interpolate8to15(workVector8Next[ii],workVector8Next[ii+1],workVector8Next[ii+2],workVector8Next[ii+3],workVector8Next[ii+4],workVector8Next[ii+5]);
314
                                    tempResult8[ii] = clip1y10to8(truncate((tempResult15[ii]+16)>>5));
315
                                    if(xfracl == 1)
316
                                       tempResult8[ii] = truncate(({1'b0,tempResult8[ii]} + {1'b0,workVector8Next[ii+2]} + 1) >> 1);
317
                                    else if(xfracl == 3)
318
                                       tempResult8[ii] = truncate(({1'b0,tempResult8[ii]} + {1'b0,workVector8Next[ii+3]} + 1) >> 1);
319
                                 end
320
                           end
321
                        Bit#(2) workHorNumOffset = (xfracl!=0 ? 2 : (reqdata.offset==0 ? 0 : 1));
322
                        if(workHorNum >= workHorNumOffset)
323
                           begin
324
                              Bit#(1) horAddr = truncate(workHorNum-workHorNumOffset);
325
                              if(yfracl == 0)//write to resultFile
326
                                 begin
327
                                    Bit#(3) verAddr = truncate(workVerNum);
328
                                    horAddr = horAddr + ((blockT==IP4x8&&workSubMbPart==1)||(blockT==IP4x4&&workSubMbPart[0]==1) ? 1 : 0);
329
                                    verAddr = verAddr + ((blockT==IP8x4&&workSubMbPart==1)||(blockT==IP4x4&&workSubMbPart[1]==1) ? 4 : 0);
330
                                    resultFile.upd({verAddr,horAddr},tempResult8);
331
                                    if(verAddr[1:0] == 3)
332
                                       resultReadyNext[{verAddr[2],horAddr}] = 1;
333
                                 end
334
                              else//write to workFile
335
                                 workFile.upd({workVerNum,horAddr},tempResult15);
336
                           end
337
                        Bit#(2) workHorNumMax = (blockT==IP8x8||blockT==IP8x4 ? 1 : 0) + workHorNumOffset;
338
                        Bit#(4) workVerNumMax = (blockT==IP8x8||blockT==IP4x8 ? 7 : 3) + (yfracl!=0 ? 5 : 0);
339
                        if(workHorNum < workHorNumMax)
340
                           workHorNum <= workHorNum+1;
341
                        else
342
                           begin
343
                              workHorNum <= 0;
344
                              if(workVerNum < workVerNumMax)
345
                                 workVerNum <= workVerNum+1;
346
                              else
347
                                 begin
348
                                    workVerNum <= 0;
349
                                    if(yfracl!=0)
350
                                       workStage <= 1;
351
                                    else
352
                                       begin
353
                                          if(((blockT==IP4x8 || blockT==IP8x4) && workSubMbPart==0) || (blockT==IP4x4 && workSubMbPart<3))
354
                                             workSubMbPart <= workSubMbPart+1;
355
                                          else
356
                                             begin
357
                                                workSubMbPart <= 0;
358
                                                workDone <= True;
359
                                             end
360
                                          reqfifoWork.deq();
361
                                       end
362
                                 end
363
                           end
364
                     end
365
                  else//vertical interpolation
366
                     begin
367
                        offset = offset + (xfracl==3&&(yfracl==1||yfracl==3) ? 1 : 0);
368
                        for(Integer ii=0; ii<4; ii=ii+1)
369
                           tempResult15[ii] = interpolate8to15(workVector8[ii],workVector8[ii+4],workVector8[ii+8],workVector8[ii+12],workVector8[ii+16],readdata[ii]);
370
                        for(Integer ii=0; ii<16; ii=ii+1)
371
                           workVector8Next[ii] = workVector8[ii+4];
372
                        for(Integer ii=0; ii<4; ii=ii+1)
373
                           workVector8Next[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 = workHorNum;
377
                        Bit#(3) verAddr = truncate(workVerNum-5);
378
                        if(workVerNum > 4)
379
                           begin
380
                              workFile.upd({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(workVerNum < workVerNumMax)
384
                           workVerNum <= workVerNum+1;
385
                        else
386
                           begin
387
                              workVerNum <= 0;
388
                              if(workHorNum < workHorNumMax)
389
                                 workHorNum <= workHorNum+1;
390
                              else
391
                                 begin
392
                                    workHorNum <= 0;
393
                                    workStage <= 1;
394
                                 end
395
                           end
396
                     end
397
               end
398
         end
399
      else
400
         begin
401
            Vector#(4,Bit#(8)) tempResult8 = replicate(0);
402
            Vector#(4,Bit#(15)) readdata = replicate(0);
403
            if(yfracl==0)
404
               $display( "ERROR Interpolation: workLuma loadStage==1 and yfracl==0");
405
            if(xfracl==0 || xfracl==2)//vertical interpolation
406
               begin
407
                  readdata = workFile.sub({workVerNum,workHorNum[0]});
408
                  for(Integer ii=0; ii<4; ii=ii+1)
409
                     begin
410
                        tempResult8[ii] = interpolate15to8(workVector15[ii],workVector15[ii+4],workVector15[ii+8],workVector15[ii+12],workVector15[ii+16],readdata[ii]);
411
                        if(yfracl == 1)
412
                           tempResult8[ii] = truncate(({1'b0,tempResult8[ii]} + {1'b0,clip1y10to8(truncate((workVector15[ii+8]+16)>>5))} + 1) >> 1);
413
                        else if(yfracl == 3)
414
                           tempResult8[ii] = truncate(({1'b0,tempResult8[ii]} + {1'b0,clip1y10to8(truncate((workVector15[ii+12]+16)>>5))} + 1) >> 1);
415
                     end
416
                  for(Integer ii=0; ii<16; ii=ii+1)
417
                     workVector15Next[ii] = workVector15[ii+4];
418
                  for(Integer ii=0; ii<4; ii=ii+1)
419
                     workVector15Next[ii+16] = readdata[ii];
420
                  Bit#(2) workHorNumMax = 1;
421
                  Bit#(4) workVerNumMax = (blockT==IP8x8||blockT==IP4x8 ? 7 : 3) + 5;
422
                  if(workVerNum > 4)
423
                     begin
424
                        Bit#(1) horAddr = truncate(workHorNum);
425
                        Bit#(3) verAddr = truncate(workVerNum-5);
426
                        horAddr = horAddr + ((blockT==IP4x8&&workSubMbPart==1)||(blockT==IP4x4&&workSubMbPart[0]==1) ? 1 : 0);
427
                        verAddr = verAddr + ((blockT==IP8x4&&workSubMbPart==1)||(blockT==IP4x4&&workSubMbPart[1]==1) ? 4 : 0);
428
                        resultFile.upd({verAddr,horAddr},tempResult8);
429
                        if(verAddr[1:0] == 3)
430
                           resultReadyNext[{verAddr[2],horAddr}] = 1;
431
                     end
432
                  if(workVerNum < workVerNumMax)
433
                     workVerNum <= workVerNum+1;
434
                  else
435
                     begin
436
                        workVerNum <= 0;
437
                        if(workHorNum < workHorNumMax)
438
                           workHorNum <= workHorNum+1;
439
                        else
440
                           begin
441
                              workHorNum <= 0;
442
                              workStage <= 0;
443
                              if(((blockT==IP4x8 || blockT==IP8x4) && workSubMbPart==0) || (blockT==IP4x4 && workSubMbPart<3))
444
                                 workSubMbPart <= workSubMbPart+1;
445
                              else
446
                                 begin
447
                                    workSubMbPart <= 0;
448
                                    workDone <= True;
449
                                 end
450
                              reqfifoWork.deq();
451
                           end
452
                     end
453
               end
454
            else//horizontal interpolation
455
               begin
456
                  offset = offset-2;
457
                  if(yfracl == 2)
458
                     begin
459
                        readdata = workFile.sub({workVerNum[2:0],workHorNum});
460
                        for(Integer ii=0; ii<8; ii=ii+1)
461
                           workVector15Next[ii] = workVector15[ii+4];
462
                        for(Integer ii=0; ii<4; ii=ii+1)
463
                           begin
464
                              Bit#(4) tempIndex = fromInteger(ii) + 8 - zeroExtend(offset);
465
                              workVector15Next[tempIndex] = readdata[ii];
466
                           end
467
                        for(Integer ii=0; ii<4; ii=ii+1)
468
                           begin
469
                              tempResult8[ii] = interpolate15to8(workVector15Next[ii],workVector15Next[ii+1],workVector15Next[ii+2],workVector15Next[ii+3],workVector15Next[ii+4],workVector15Next[ii+5]);
470
                              if(xfracl == 1)
471
                                 tempResult8[ii] = truncate(({1'b0,tempResult8[ii]} + {1'b0,clip1y10to8(truncate((workVector15Next[ii+2]+16)>>5))} + 1) >> 1);
472
                              else if(xfracl == 3)
473
                                 tempResult8[ii] = truncate(({1'b0,tempResult8[ii]} + {1'b0,clip1y10to8(truncate((workVector15Next[ii+3]+16)>>5))} + 1) >> 1);
474
                           end
475
                     end
476
                  else
477
                     begin
478
                        if(memRespQ.first() matches tagged IPLoadResp .tempreaddata8)
479
                           begin
480
                              memRespQ.deq();
481
                              Vector#(4,Bit#(8)) readdata8 = replicate(0);
482
                              readdata8[0] = tempreaddata8[7:0];
483
                              readdata8[1] = tempreaddata8[15:8];
484
                              readdata8[2] = tempreaddata8[23:16];
485
                              readdata8[3] = tempreaddata8[31:24];
486
                              for(Integer ii=0; ii<8; ii=ii+1)
487
                                 workVector8Next[ii] = workVector8[ii+4];
488
                              for(Integer ii=0; ii<4; ii=ii+1)
489
                                 begin
490
                                    Bit#(4) tempIndex = fromInteger(ii) + 8 - zeroExtend(offset);
491
                                    workVector8Next[tempIndex] = readdata8[ii];
492
                                 end
493
                              Vector#(4,Bit#(15)) tempResult15 = replicate(0);
494
                              for(Integer ii=0; ii<4; ii=ii+1)
495
                                 begin
496
                                    tempResult15[ii] = interpolate8to15(workVector8Next[ii],workVector8Next[ii+1],workVector8Next[ii+2],workVector8Next[ii+3],workVector8Next[ii+4],workVector8Next[ii+5]);
497
                                    tempResult8[ii] = clip1y10to8(truncate((tempResult15[ii]+16)>>5));
498
                                 end
499
                              Bit#(2) verOffset;
500
                              Vector#(4,Bit#(15)) verResult15 = replicate(0);
501
                              if(xfracl == 1)
502
                                 verOffset = reqdata.offset;
503
                              else
504
                                 verOffset = reqdata.offset+1;
505
                              readdata = workFile.sub({workVerNum[2:0],(workHorNum-2+(verOffset==0?0:1))});
506
                              for(Integer ii=0; ii<4; ii=ii+1)
507
                                 begin
508
                                    Bit#(2) offsetplusii = verOffset+fromInteger(ii);
509
                                    if(verOffset <= 3-fromInteger(ii) && verOffset!=0)
510
                                       verResult15[ii] = workVector15[offsetplusii];
511
                                    else
512
                                       verResult15[ii] = readdata[offsetplusii];
513
                                    workVector15Next[ii] = readdata[ii];
514
                                 end
515
                              for(Integer ii=0; ii<4; ii=ii+1)
516
                                 begin
517
                                    Bit#(9) tempVal = zeroExtend(clip1y10to8(truncate((verResult15[ii]+16)>>5)));
518
                                    tempResult8[ii] = truncate((tempVal+zeroExtend(tempResult8[ii])+1)>>1);
519
                                 end
520
                           end
521
                     end
522
                  if(workHorNum >= 2)
523
                     begin
524
                        Bit#(1) horAddr = truncate(workHorNum-2);
525
                        Bit#(3) verAddr = truncate(workVerNum);
526
                        horAddr = horAddr + ((blockT==IP4x8&&workSubMbPart==1)||(blockT==IP4x4&&workSubMbPart[0]==1) ? 1 : 0);
527
                        verAddr = verAddr + ((blockT==IP8x4&&workSubMbPart==1)||(blockT==IP4x4&&workSubMbPart[1]==1) ? 4 : 0);
528
                        resultFile.upd({verAddr,horAddr},tempResult8);
529
                        if(verAddr[1:0] == 3)
530
                           resultReadyNext[{verAddr[2],horAddr}] = 1;
531
                        //$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));
532
                     end
533
                  Bit#(2) workHorNumMax = (blockT==IP8x8||blockT==IP8x4 ? 1 : 0) + 2;
534
                  Bit#(4) workVerNumMax = (blockT==IP8x8||blockT==IP4x8 ? 7 : 3);
535
                  if(workHorNum < workHorNumMax)
536
                     workHorNum <= workHorNum+1;
537
                  else
538
                     begin
539
                        workHorNum <= 0;
540
                        if(workVerNum < workVerNumMax)
541
                           workVerNum <= workVerNum+1;
542
                        else
543
                           begin
544
                              workVerNum <= 0;
545
                              workStage <= 0;
546
                              if(((blockT==IP4x8 || blockT==IP8x4) && workSubMbPart==0) || (blockT==IP4x4 && workSubMbPart<3))
547
                                 workSubMbPart <= workSubMbPart+1;
548
                              else
549
                                 begin
550
                                    workSubMbPart <= 0;
551
                                    workDone <= True;
552
                                 end
553
                              reqfifoWork.deq();
554
                           end
555
                     end
556
               end
557
         end
558
      workVector8 <= workVector8Next;
559
      workVector15 <= workVector15Next;
560
      resultReady <= resultReadyNext;
561
      //$display( "Trace interpolator: workLuma %h %h %h %h %h %h", xfracl, yfracl, workHorNum, workVerNum, offset, workStage);
562
   endrule
563
 
564
 
565
   rule workChroma ( reqfifoWork.first() matches tagged IPWChroma .reqdata &&& !workDone );
566
      Bit#(4) xfracc = zeroExtend(reqdata.xFracC);
567
      Bit#(4) yfracc = zeroExtend(reqdata.yFracC);
568
      let offset = reqdata.offset;
569
      let blockT = reqdata.bt;
570
      Vector#(20,Bit#(8)) workVector8Next = workVector8;
571
      Vector#(4,Bit#(1)) resultReadyNext = resultReady;
572
      if(memRespQ.first() matches tagged IPLoadResp .tempreaddata)
573
         begin
574
            memRespQ.deq();
575
            Vector#(4,Bit#(8)) readdata = replicate(0);
576
            readdata[0] = tempreaddata[7:0];
577
            readdata[1] = tempreaddata[15:8];
578
            readdata[2] = tempreaddata[23:16];
579
            readdata[3] = tempreaddata[31:24];
580
            Vector#(5,Bit#(8)) tempWork8 = replicate(0);
581
            Vector#(5,Bit#(8)) tempPrev8 = replicate(0);
582
            Vector#(4,Bit#(8)) tempResult8 = replicate(0);
583
            Bool resultReadyFlag = False;
584
            for(Integer ii=0; ii<4; ii=ii+1)
585
               begin
586
                  Bit#(2) offsetplusii = offset+fromInteger(ii);
587
                  if(offset <= 3-fromInteger(ii) && !((blockT==IP4x8||blockT==IP4x4)&&(offset[1]==0||(xfracc==0&&offset!=3))) && !(xfracc==0&&offset==0))
588
                     tempWork8[ii] = workVector8[offsetplusii];
589
                  else
590
                     tempWork8[ii] = readdata[offsetplusii];
591
                  workVector8Next[ii] = readdata[ii];
592
               end
593
            tempWork8[4] = readdata[offset];
594
            if((blockT==IP16x8 || blockT==IP16x16) && workHorNum==(xfracc==0&&offset==0 ? 1 : 2))
595
               begin
596
                  for(Integer ii=0; ii<5; ii=ii+1)
597
                     begin
598
                        tempPrev8[ii] = workVector8[ii+9];
599
                        workVector8Next[ii+9] = tempWork8[ii];
600
                     end
601
               end
602
            else
603
               begin
604
                  for(Integer ii=0; ii<5; ii=ii+1)
605
                     tempPrev8[ii] = workVector8[ii+4];
606
                  if(workHorNum==(xfracc==0&&offset==0 ? 0 : 1) || ((blockT==IP4x8||blockT==IP4x4)&&(offset[1]==0||(xfracc==0&&offset!=3))))
607
                     begin
608
                        for(Integer ii=0; ii<5; ii=ii+1)
609
                           workVector8Next[ii+4] = tempWork8[ii];
610
                     end
611
               end
612
            if(yfracc==0)
613
               begin
614
                  for(Integer ii=0; ii<5; ii=ii+1)
615
                     tempPrev8[ii] = tempWork8[ii];
616
               end
617
            for(Integer ii=0; ii<4; ii=ii+1)
618
               begin
619
                  Bit#(14) tempVal = zeroExtend((8-xfracc))*zeroExtend((8-yfracc))*zeroExtend(tempPrev8[ii]);
620
                  tempVal = tempVal + zeroExtend(xfracc)*zeroExtend((8-yfracc))*zeroExtend(tempPrev8[ii+1]);
621
                  tempVal = tempVal + zeroExtend((8-xfracc))*zeroExtend(yfracc)*zeroExtend(tempWork8[ii]);
622
                  tempVal = tempVal + zeroExtend(xfracc)*zeroExtend(yfracc)*zeroExtend(tempWork8[ii+1]);
623
                  tempResult8[ii] = truncate((tempVal+32)>>6);
624
               end
625
            if(workVerNum > 0 || yfracc==0)
626
               begin
627
                  if(blockT==IP4x8 || blockT==IP4x4)
628
                     begin
629
                        Bit#(5) tempIndex = 10 + zeroExtend(workVerNum<<1);
630
                        workVector8Next[tempIndex] = tempResult8[0];
631
                        workVector8Next[tempIndex+1] = tempResult8[1];
632
                        tempResult8[2] = tempResult8[0];
633
                        tempResult8[3] = tempResult8[1];
634
                        tempResult8[0] = workVector8[tempIndex];
635
                        tempResult8[1] = workVector8[tempIndex+1];
636
                        if((workHorNum>0 || offset[1]==0) && workSubMbPart[0]==1)
637
                           resultReadyFlag = True;
638
                     end
639
                  else
640
                     begin
641
                        if(workHorNum>0 || (xfracc==0 && offset==0))
642
                           resultReadyFlag = True;
643
                     end
644
               end
645
            if(resultReadyFlag)
646
               begin
647
                  Bit#(1) horAddr = ((blockT==IP4x8 || blockT==IP4x4) ? 0 : truncate(((xfracc==0 && offset==0) ? workHorNum : workHorNum-1)));
648
                  Bit#(3) verAddr = truncate((yfracc==0 ? workVerNum : workVerNum-1));
649
                  horAddr = horAddr + ((blockT==IP16x8||blockT==IP16x16) ? 0 : workMbPart[0]);
650
                  verAddr = verAddr + ((blockT==IP8x16||blockT==IP16x16) ? 0 : ((blockT==IP16x8) ? {workMbPart[0],2'b00} : {workMbPart[1],2'b00}));
651
                  verAddr = verAddr + ((blockT==IP8x4&&workSubMbPart==1)||(blockT==IP4x4&&workSubMbPart[1]==1) ? 2 : 0);
652
                  resultFile.upd({verAddr,horAddr},tempResult8);
653
                  if(verAddr[1:0] == 3)
654
                     resultReadyNext[{verAddr[2],horAddr}] = 1;
655
               end
656
            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)));
657
            Bit#(4) workVerNumMax = (blockT==IP16x16||blockT==IP8x16 ? 7 : (blockT==IP16x8||blockT==IP8x8||blockT==IP4x8 ? 3 : 1)) + (yfracc==0 ? 0 : 1);
658
            if(workHorNum < workHorNumMax)
659
               workHorNum <= workHorNum+1;
660
            else
661
               begin
662
                  workHorNum <= 0;
663
                  if(workVerNum < workVerNumMax)
664
                     workVerNum <= workVerNum+1;
665
                  else
666
                     begin
667
                        workVerNum <= 0;
668
                        if(((blockT==IP4x8 || blockT==IP8x4) && workSubMbPart==0) || (blockT==IP4x4 && workSubMbPart<3))
669
                           workSubMbPart <= workSubMbPart+1;
670
                        else
671
                           begin
672
                              workSubMbPart <= 0;
673
                              if(((blockT==IP16x8 || blockT==IP8x16) && workMbPart==0) || (!(blockT==IP16x8 || blockT==IP8x16 || blockT==IP16x16) && workMbPart<3))
674
                                 workMbPart <= workMbPart+1;
675
                              else
676
                                 begin
677
                                    workMbPart <= 0;
678
                                    workDone <= True;
679
                                 end
680
                           end
681
                        reqfifoWork.deq();
682
                     end
683
               end
684
         end
685
      workVector8 <= workVector8Next;
686
      resultReady <= resultReadyNext;
687
      //$display( "Trace interpolator: workChroma %h %h %h %h %h", xfracc, yfracc, workHorNum, workVerNum, offset);
688
   endrule
689
 
690
 
691
   rule outputing( !outDone && resultReady[outBlockNum]==1 );
692
      outfifo.enq(resultFile.sub({outBlockNum[1],outPixelNum,outBlockNum[0]}));
693
      outPixelNum <= outPixelNum+1;
694
      if(outPixelNum == 3)
695
         begin
696
            outBlockNum <= outBlockNum+1;
697
            if(outBlockNum == 3)
698
               outDone <= True;
699
         end
700
      //$display( "Trace interpolator: outputing %h %h %h %h %h %h", outBlockNum, outPixelNum, tempVector[3], tempVector[2], tempVector[1], tempVector[0]);
701
   endrule
702
 
703
 
704
   rule switching( outDone && workDone );
705
      outDone <= False;
706
      workDone <= False;
707
      resultReady <= replicate(0);
708
      //$display( "Trace interpolator: switching %h %h", outBlockNum, outPixelNum);
709
   endrule
710
 
711
 
712
   method Action   setPicWidth( Bit#(PicWidthSz) newPicWidth );
713
      picWidth <= newPicWidth;
714
   endmethod
715
 
716
   method Action   setPicHeight( Bit#(PicHeightSz) newPicHeight );
717
      picHeight <= newPicHeight;
718
   endmethod
719
 
720
   method Action request( InterpolatorIT inputdata );
721
      reqfifoLoad.enq(inputdata);
722
      if(inputdata matches tagged IPLuma .indata)
723
         reqfifoWork.enq(IPWLuma {xFracL:indata.mvhor[1:0],yFracL:indata.mvver[1:0],offset:indata.mvhor[3:2],bt:indata.bt});
724
      else if(inputdata matches tagged IPChroma .indata)
725
         reqfifoWork.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});
726
   endmethod
727
 
728
   method Vector#(4,Bit#(8)) first();
729
      return outfifo.first();
730
   endmethod
731
 
732
   method Action deq();
733
      outfifo.deq();
734
   endmethod
735
 
736
   method Action endOfFrame();
737
      endOfFrameFlag <= True;
738
   endmethod
739
 
740
   interface Client mem_client;
741
      interface Get request  = fifoToGet(memReqQ);
742
      interface Put response = fifoToPut(memRespQ);
743
   endinterface
744
 
745
 
746
endmodule
747
 
748
 
749
endpackage

powered by: WebSVN 2.1.0

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