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

Subversion Repositories bluespec-h264

[/] [bluespec-h264/] [trunk/] [release/] [mkPrediction.bsv] - Blame information for rev 84

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

Line No. Rev Author Line
1 84 jamey.hick
//**********************************************************************
2
// Prediction
3
//----------------------------------------------------------------------
4
//
5
//
6
 
7
package mkPrediction;
8
 
9
import H264Types::*;
10
 
11
import IPrediction::*;
12
import IInterpolator::*;
13
import mkInterpolator::*;
14
import FIFO::*;
15
import FIFOF::*;
16
import Vector::*;
17
 
18
import Connectable::*;
19
import GetPut::*;
20
import ClientServer::*;
21
 
22
 
23
//-----------------------------------------------------------
24
// Local Datatypes
25
//-----------------------------------------------------------
26
 
27
typedef union tagged
28
{
29
 void     Intra;            //Intra non-4x4
30
 void     Intra4x4;
31
 void     Inter;
32
}
33
OutState deriving(Eq,Bits);
34
 
35
typedef union tagged
36
{
37
 void     Start;            //not working on anything in particular
38
 void     Intra16x16;
39
 void     Intra4x4;
40
 void     IntraPCM;
41
}
42
IntraState deriving(Eq,Bits);
43
 
44
typedef union tagged
45
{
46
 void     Start;            //not working on anything in particular
47
 void     InterP16x16;
48
 void     InterP16x8;
49
 void     InterP8x16;
50
 void     InterP8x8;
51
 void     InterP8x8ref0;
52
 void     InterPskip;
53
}
54
InterState deriving(Eq,Bits);
55
 
56
typedef union tagged
57
{
58
 Bit#(1) NotInter;//0 for not available, 1 for intra-coded
59
 struct {Bit#(4) refIdx; Bit#(14) mvhor; Bit#(12) mvver; Bit#(1) nonZeroTransCoeff;} BlockMv;
60
}
61
InterBlockMv deriving(Eq,Bits);
62
 
63
typedef union tagged
64
{
65
 void SkipMB;
66
 void NonSkipMB;
67
 void Intra4x4;
68
 void Intra4x4PlusChroma;
69
}
70
NextOutput deriving(Eq,Bits);
71
 
72
 
73
 
74
//-----------------------------------------------------------
75
// Helper functions
76
 
77
function Bit#(8) intra4x4SelectTop( Bit#(72) valVector, Bit#(4) idx );
78
   case(idx)
79
      0: return valVector[15:8];
80
      1: return valVector[23:16];
81
      2: return valVector[31:24];
82
      3: return valVector[39:32];
83
      4: return valVector[47:40];
84
      5: return valVector[55:48];
85
      6: return valVector[63:56];
86
      7: return valVector[71:64];
87
      default: return valVector[7:0];
88
   endcase
89
endfunction
90
 
91
function Bit#(8) intra4x4SelectLeft( Bit#(40) valVector, Bit#(3) idx );
92
   case(idx)
93
      0: return valVector[15:8];
94
      1: return valVector[23:16];
95
      2: return valVector[31:24];
96
      3: return valVector[39:32];
97
      default: return valVector[7:0];
98
   endcase
99
endfunction
100
 
101
function Bit#(8) select32to8( Bit#(32) valVector, Bit#(2) idx );
102
   case(idx)
103
      0: return valVector[7:0];
104
      1: return valVector[15:8];
105
      2: return valVector[23:16];
106
      3: return valVector[31:24];
107
   endcase
108
endfunction
109
 
110
function Bit#(8) select16to8( Bit#(16) valVector, Bit#(1) idx );
111
   case(idx)
112
      0: return valVector[7:0];
113
      1: return valVector[15:8];
114
   endcase
115
endfunction
116
 
117
function Bool absDiffGEFour14( Bit#(14) val1, Bit#(14) val2 );
118
   Int#(15) int1 = unpack(signExtend(val1));
119
   Int#(15) int2 = unpack(signExtend(val2));
120
   if(int1>=int2)
121
      return (int1 >= (int2+4));
122
   else
123
      return (int2 >= (int1+4));
124
endfunction
125
 
126
function Bool absDiffGEFour12( Bit#(12) val1, Bit#(12) val2 );
127
   Int#(13) int1 = unpack(signExtend(val1));
128
   Int#(13) int2 = unpack(signExtend(val2));
129
   if(int1>=int2)
130
      return (int1 >= (int2+4));
131
   else
132
      return (int2 >= (int1+4));
133
endfunction
134
 
135
 
136
//-----------------------------------------------------------
137
// Prediction Module
138
//-----------------------------------------------------------
139
 
140
 
141
(* synthesize *)
142
module mkPrediction( IPrediction );
143
 
144
   //Common state
145
   FIFO#(EntropyDecOT)   infifo     <- mkSizedFIFO(prediction_infifo_size);
146
   FIFO#(InverseTransOT) infifo_ITB <- mkSizedFIFO(prediction_infifo_ITB_size);
147
   FIFO#(EntropyDecOT)   outfifo    <- mkFIFO;
148
   Reg#(Bool)            passFlag   <- mkReg(True);
149
   Reg#(Bit#(4))         blockNum   <- mkReg(0);
150
   Reg#(Bit#(4))         pixelNum   <- mkReg(0);
151
 
152
   Reg#(Bit#(PicWidthSz))  picWidth  <- mkReg(maxPicWidthInMB);
153
   Reg#(Bit#(PicHeightSz)) picHeight <- mkReg(0);
154
   Reg#(Bit#(PicAreaSz))   firstMb   <- mkReg(0);
155
   Reg#(Bit#(PicAreaSz))   currMb    <- mkReg(0);
156
   Reg#(Bit#(PicAreaSz))   currMbHor <- mkReg(0);//horizontal position of currMb
157
   Reg#(Bit#(PicHeightSz)) currMbVer <- mkReg(0);//vertical position of currMb
158
 
159
   FIFOF#(OutState)   outstatefifo   <- mkFIFOF;
160
   FIFOF#(NextOutput) nextoutputfifo <- mkFIFOF;
161
   Reg#(Bit#(4))   outBlockNum    <- mkReg(0);
162
   Reg#(Bit#(4))   outPixelNum    <- mkReg(0);
163
   FIFO#(Vector#(4,Bit#(8))) predictedfifo  <- mkSizedFIFO(prediction_predictedfifo_size);
164
   Reg#(Bit#(1))   outChromaFlag  <- mkReg(0);
165
   Reg#(Bool)      outFirstQPFlag <- mkReg(False);
166
 
167
   DoNotFire donotfire <- mkDoNotFire();
168
 
169
   //Reg#(Vector#(16,Bit#(8))) workVector       <- mkRegU();
170
 
171
   //Inter state
172
   Interpolator interpolator <- mkInterpolator();
173
   Reg#(InterState) interstate <- mkReg(Start);
174
   Reg#(Bit#(PicAreaSz)) interPskipCount <- mkReg(0);
175
   Reg#(Vector#(5,InterBlockMv)) interTopVal <- mkRegU();
176
   Reg#(Vector#(4,InterBlockMv)) interLeftVal <- mkRegU();
177
   Reg#(Vector#(4,InterBlockMv)) interTopLeftVal <- mkRegU();
178
   FIFO#(MemReq#(TAdd#(PicWidthSz,2),32)) interMemReqQ <- mkFIFO;
179
   Reg#(MemReq#(TAdd#(PicWidthSz,2),32)) interMemReqQdelay <- mkRegU();
180
   FIFO#(MemResp#(32))  interMemRespQ <- mkFIFO;
181
   Reg#(Bit#(3)) interReqCount <- mkReg(0);
182
   Reg#(Bit#(3)) interRespCount <- mkReg(0);
183
 
184
   Reg#(Bit#(2)) interStepCount <- mkReg(0);
185
   Reg#(Bit#(2)) interMbPartNum <- mkReg(0);
186
   Reg#(Bit#(2)) interSubMbPartNum <- mkReg(0);
187
   Reg#(Bit#(2)) interPassingCount <- mkReg(0);
188
   Reg#(Vector#(4,Bit#(4))) interRefIdxVector <- mkRegU();
189
   Reg#(Vector#(4,Bit#(2))) interSubMbTypeVector <- mkRegU();
190
   RFile1#(Bit#(4),Tuple2#(Bit#(14),Bit#(12))) interMvFile <- mkRFile1Full();
191
   Reg#(Bit#(15)) interMvDiffTemp <- mkReg(0);
192
   FIFO#(Tuple2#(Bit#(15),Bit#(13))) interMvDiff <- mkFIFO;
193
   Reg#(Bit#(5)) interNewestMv <- mkReg(0);
194
 
195
   // Registers for pipelining the interStage rule
196
 
197
   Reg#(Bit#(3)) partWidthR <- mkRegU();
198
   Reg#(Bit#(3)) partHeightR <- mkRegU();
199
   Reg#(Bit#(3)) numPartR <- mkRegU();
200
   Reg#(Bit#(3)) numSubPartR <- mkRegU();
201
   Reg#(Bit#(2)) subMbTypeR <- mkRegU();
202
   Reg#(Bool) calcmvR <- mkRegU();
203
   Reg#(Bool) leftmvR <- mkRegU();
204
   Reg#(Bit#(4)) refIndexR <- mkRegU();
205
   Reg#(Vector#(3,InterBlockMv)) blockABCR <- mkRegU();
206
   Reg#(Bit#(14)) mvhorfinalR <- mkRegU();
207
   Reg#(Bit#(12)) mvverfinalR <- mkRegU();
208
   Reg#(Bit#(5)) interNewestMvNextR <- mkRegU();
209
 
210
 
211
   Reg#(Bit#(2)) interIPStepCount <- mkReg(0);
212
   Reg#(Bit#(2)) interIPMbPartNum <- mkReg(0);
213
   Reg#(Bit#(2)) interIPSubMbPartNum <- mkReg(0);
214
 
215
   Reg#(Bit#(PicWidthSz)) interCurrMbDiff <- mkReg(0);
216
 
217
   Reg#(Vector#(4,Bool)) interTopNonZeroTransCoeff <- mkRegU();
218
   Reg#(Vector#(4,Bool)) interLeftNonZeroTransCoeff <- mkRegU();
219
   FIFO#(Tuple2#(Bit#(2),Bit#(2))) interBSfifo <- mkSizedFIFO(32);
220
   Reg#(Bool) interBSoutput <- mkReg(True);
221
   FIFO#(InterBlockMv) interOutBlockMvfifo <- mkSizedFIFO(8);
222
 
223
 
224
   //Intra state
225
   Reg#(IntraState)     intrastate      <- mkReg(Start);
226
   Reg#(Bit#(1))        intraChromaFlag <- mkReg(0);
227
   FIFO#(MemReq#(TAdd#(PicWidthSz,2),68)) intraMemReqQ  <- mkFIFO;
228
   Reg#(MemReq#(TAdd#(PicWidthSz,2),68)) intraMemReqQdelay <- mkRegU;
229
   FIFO#(MemResp#(68))  intraMemRespQ <- mkFIFO;
230
   Reg#(Vector#(4,Bit#(4))) intra4x4typeLeft <- mkRegU();//15=unavailable, 14=inter-MB, 13=intra-non-4x4
231
   Reg#(Vector#(4,Bit#(4))) intra4x4typeTop  <- mkRegU();//15=unavailable, 14=inter-MB, 13=intra-non-4x4
232
   Reg#(Bit#(1)) ppsconstrained_intra_pred_flag <- mkReg(0);
233
   Reg#(Vector#(4,Bit#(40))) intraLeftVal <- mkRegU();
234
   Reg#(Vector#(9,Bit#(8))) intraLeftValChroma0 <- mkRegU();
235
   Reg#(Vector#(9,Bit#(8))) intraLeftValChroma1 <- mkRegU();
236
   Reg#(Vector#(5,Bit#(32))) intraTopVal <- mkRegU();
237
   Reg#(Vector#(4,Bit#(16))) intraTopValChroma0 <- mkRegU();
238
   Reg#(Vector#(4,Bit#(16))) intraTopValChroma1 <- mkRegU();
239
   Reg#(Bit#(32)) intraLeftValNext <- mkReg(0);
240
   Reg#(Bit#(2)) intra16x16_pred_mode <- mkReg(0);
241
   FIFO#(Bit#(4)) rem_intra4x4_pred_mode <- mkSizedFIFO(16);
242
   FIFO#(Bit#(2)) intra_chroma_pred_mode <- mkFIFO;
243
   Reg#(Bit#(4)) cur_intra4x4_pred_mode <- mkReg(0);
244
   Reg#(Bit#(1)) intraChromaTopAvailable <- mkReg(0);
245
   Reg#(Bit#(1)) intraChromaLeftAvailable <- mkReg(0);
246
 
247
   Reg#(Bit#(3)) intraReqCount <- mkReg(0);
248
   Reg#(Bit#(3)) intraRespCount <- mkReg(0);
249
   Reg#(Bit#(4)) intraStepCount <- mkReg(0);
250
   Reg#(Bit#(13)) intraSumA <-  mkReg(0);
251
   Reg#(Bit#(15)) intraSumB <-  mkReg(0);
252
   Reg#(Bit#(15)) intraSumC <-  mkReg(0);
253
 
254
   Reg#(Vector#(4,Bit#(8))) intraPredVector <- mkRegU();
255
 
256
   //-----------------------------------------------------------
257
   // Rules
258
 
259
   //////////////////////////////////////////////////////////////////////////////
260
//   rule stateMonitor ( True );
261
//      if(predictedfifo.notEmpty())
262
//       $display( "TRACE Prediction: stateMonitor predictedfifo.first() %0d", predictedfifo.first());////////////////////
263
//      if(infifo.first() matches tagged ITBresidual .xdata)
264
//       $display( "TRACE Prediction: stateMonitor infifo.first() %0d", xdata);////////////////////
265
//      if(infifo.first() matches tagged ITBresidual .xdata)
266
//       $display( "TRACE Prediction: stateMonitor outBlockNum outPixelNum outChromaFlag %0d %0d", outBlockNum, outPixelNum, outChromaFlag);////////////////////
267
//   endrule
268
   //////////////////////////////////////////////////////////////////////////////
269
 
270
   rule checkFIFO ( True );
271
      $display( "Trace Prediction: checkFIFO %h", infifo.first() );
272
   endrule
273
   rule checkFIFO_ITB ( True );
274
      $display( "Trace Prediction: checkFIFO_ITB %h", infifo_ITB.first() );
275
   endrule
276
   rule checkFIFO_predicted ( True );
277
      $display( "Trace Prediction: checkFIFO_predicted %h", predictedfifo.first() );
278
   endrule
279
 
280
 
281
   rule passing ( passFlag && !outstatefifo.notEmpty() && currMbHor
282
      $display( "Trace Prediction: passing infifo packed %h", pack(infifo.first()));
283
      case (infifo.first()) matches
284
         tagged NewUnit . xdata :
285
            begin
286
               infifo.deq();
287
               outfifo.enq(infifo.first());
288
               $display("ccl4newunit");
289
               $display("ccl4rbspbyte %h", xdata);
290
            end
291
         tagged SPSpic_width_in_mbs .xdata :
292
            begin
293
               infifo.deq();
294
               outfifo.enq(infifo.first());
295
               picWidth <= xdata;
296
               interpolator.setPicWidth(xdata);
297
            end
298
         tagged SPSpic_height_in_map_units .xdata :
299
            begin
300
               infifo.deq();
301
               outfifo.enq(infifo.first());
302
               picHeight <= xdata;
303
               interpolator.setPicHeight(xdata);
304
            end
305
         tagged PPSconstrained_intra_pred_flag .xdata :
306
            begin
307
               infifo.deq();
308
               ////outfifo.enq(infifo.first());
309
               ppsconstrained_intra_pred_flag <= xdata;
310
            end
311
         tagged SHfirst_mb_in_slice .xdata :
312
            begin
313
               infifo.deq();
314
               outfifo.enq(infifo.first());
315
               firstMb   <= xdata;
316
               currMb    <= xdata;
317
               currMbHor <= xdata;
318
               currMbVer <= 0;
319
               intra4x4typeLeft <= replicate(15);
320
               interTopLeftVal <= replicate(tagged NotInter 0);
321
               if(xdata==0)
322
                  interLeftVal <= replicate(tagged NotInter 0);
323
               outFirstQPFlag <= True;
324
            end
325
         tagged SDmb_skip_run .xdata : passFlag <= False;
326
         tagged SDMmbtype .xdata : passFlag <= False;
327
         tagged EndOfFile :
328
            begin
329
               infifo.deq();
330
               outfifo.enq(infifo.first());
331
               $display( "INFO Prediction: EndOfFile reached" );
332
               //$finish(0);////////////////////////////////
333
            end
334
         default:
335
            begin
336
               infifo.deq();
337
               outfifo.enq(infifo.first());
338
            end
339
      endcase
340
   endrule
341
 
342
 
343
   rule inputing ( !passFlag );
344
      $display( "Trace Prediction: inputing infifo packed %h", pack(infifo.first()));
345
      case (infifo.first()) matches
346
         tagged SDmb_skip_run .xdata :
347
            begin
348
               if(interstate==Start && intrastate==Start)
349
                  begin
350
                     if(interPskipCount < xdata)
351
                        begin
352
                           if(!outstatefifo.notEmpty() || interCurrMbDiff
353
                              begin
354
                                 $display( "Trace Prediction: passing SDmb_skip_run %0d", xdata);
355
                                 outstatefifo.enq(Inter);
356
                                 interstate <= InterPskip;
357
                                 interReqCount <= 1;
358
                                 interRespCount <= 1;
359
                                 intra4x4typeLeft <= replicate(14);
360
                                 intra4x4typeTop <= replicate(14);
361
                                 interTopLeftVal <= update(interTopLeftVal , 0, (tagged NotInter 0));
362
                                 interTopVal <= replicate(tagged NotInter 0);
363
                                 interPskipCount <= interPskipCount+1;
364
                                 interNewestMv <= 0;
365
                                 interRefIdxVector <= replicate(0);
366
                                 interCurrMbDiff <= interCurrMbDiff+1;
367
                                 nextoutputfifo.enq(SkipMB);
368
                              end
369
                           else
370
                              donotfire.doNotFire();
371
                        end
372
                     else
373
                        begin
374
                           $display( "Trace Prediction: passing no SDmb_skip_run");
375
                           interPskipCount <= 0;
376
                           infifo.deq();
377
                        end
378
                  end
379
               else
380
                  donotfire.doNotFire();
381
            end
382
         tagged SDMmbtype .xdata :
383
            begin
384
               if(interstate==Start && intrastate==Start)//not necessary (just need to keep inter from feeding predictedfifo or change intra state until intrastate==Start)
385
                  begin
386
                     infifo.deq();
387
                     $display( "INFO Prediction: SDMmbtype %0d", xdata);
388
                     if(mbPartPredMode(xdata,0)==Intra_16x16)
389
                        begin
390
                           if(!outstatefifo.notEmpty())
391
                              begin
392
                                 outstatefifo.enq(Intra);
393
                                 intrastate <= Intra16x16;
394
                                 if(xdata matches tagged I_16x16 {intra16x16PredMode:.tempv1, codedBlockPatternChroma:.tempv2, codedBlockPatternLuma:.tempv3})
395
                                    intra16x16_pred_mode <= tempv1;
396
                                 else
397
                                    $display( "ERROR Prediction: MacroblockLayer 5 sdmmbtype not I_16x16" );
398
                                 intraReqCount <= 1;
399
                                 intraRespCount <= 1;
400
                                 interTopLeftVal <= replicate(tagged NotInter 1);
401
                                 interLeftVal <= replicate(tagged NotInter 1);
402
                                 interTopVal <= replicate(tagged NotInter 1);
403
                              end
404
                           else
405
                              donotfire.doNotFire();
406
                        end
407
                     else if(xdata==I_NxN)
408
                        begin
409
                           if(!outstatefifo.notEmpty())
410
                              begin
411
                                 outstatefifo.enq(Intra4x4);
412
                                 intrastate <= Intra4x4;
413
                                 intraReqCount <= 1;
414
                                 intraRespCount <= 1;
415
                                 interTopLeftVal <= replicate(tagged NotInter 1);
416
                                 interLeftVal <= replicate(tagged NotInter 1);
417
                                 interTopVal <= replicate(tagged NotInter 1);
418
                              end
419
                           else
420
                              donotfire.doNotFire();
421
                        end
422
                     else if(xdata==I_PCM)
423
                        begin
424
                           $display( "ERROR Prediction: I_PCM not implemented yet");
425
                           $finish;////////////////////////////////////////////////////////////////////////////////////////
426
                           intra4x4typeLeft <= replicate(13);
427
                           intra4x4typeTop <= replicate(13);
428
                           interTopLeftVal <= replicate(tagged NotInter 1);
429
                           interLeftVal <= replicate(tagged NotInter 1);
430
                           interTopVal <= replicate(tagged NotInter 1);
431
                        end
432
                     else
433
                        begin
434
                           if(!outstatefifo.notEmpty() || interCurrMbDiff
435
                              begin
436
                                 outstatefifo.enq(Inter);
437
                                 case(xdata)
438
                                    P_L0_16x16: interstate <= InterP16x16;
439
                                    P_L0_L0_16x8: interstate <= InterP16x8;
440
                                    P_L0_L0_8x16: interstate <= InterP8x16;
441
                                    P_8x8: interstate <= InterP8x8;
442
                                    P_8x8ref0: interstate <= InterP8x8ref0;
443
                                    default: $display( "ERROR Prediction: passing SDMmbtype inter prediction unknown mbtype");
444
                                 endcase
445
                                 interReqCount <= 1;
446
                                 interRespCount <= 1;
447
                                 intra4x4typeLeft <= replicate(14);/////////////////////////////////////////////////////////////////////////////
448
                                 intra4x4typeTop <= replicate(14);
449
                                 interTopLeftVal <= update(interTopLeftVal , 0, (tagged NotInter 0));
450
                                 interTopVal <= replicate(tagged NotInter 0);
451
                                 interNewestMv <= 0;
452
                                 interRefIdxVector <= replicate(0);
453
                                 nextoutputfifo.enq(NonSkipMB);
454
                              end
455
                           else
456
                              donotfire.doNotFire();
457
                        end
458
                     interCurrMbDiff <= interCurrMbDiff+1;
459
                  end
460
               else
461
                  donotfire.doNotFire();
462
            end
463
         tagged SDMMrem_intra4x4_pred_mode .xdata :
464
            begin
465
               infifo.deq();
466
               ////outfifo.enq(infifo.first());
467
               rem_intra4x4_pred_mode.enq(xdata);
468
            end
469
         tagged SDMMintra_chroma_pred_mode .xdata :
470
            begin
471
               infifo.deq();
472
               ////outfifo.enq(infifo.first());
473
               intra_chroma_pred_mode.enq(xdata);
474
            end
475
         tagged SDMMref_idx_l0 .xdata :
476
            begin
477
               infifo.deq();
478
               ////outfifo.enq(infifo.first());
479
               interRefIdxVector <= update(interRefIdxVector,interPassingCount,xdata[3:0]);
480
               if(interstate==InterP16x16 || interPassingCount==1)
481
                  interPassingCount <= 0;
482
               else
483
                  interPassingCount <= interPassingCount+1;
484
            end
485
         tagged SDMMmvd_l0 .xdata :
486
            begin
487
               infifo.deq();
488
               ////outfifo.enq(infifo.first());
489
               if(interPassingCount==1)
490
                  begin
491
                     Bit#(13) interMvDiffTemp2 = truncate(xdata);
492
                     interMvDiff.enq(tuple2(interMvDiffTemp,interMvDiffTemp2));
493
                     interPassingCount <= 0;
494
                  end
495
               else
496
                  begin
497
                     interMvDiffTemp <= truncate(xdata);
498
                     interPassingCount <= interPassingCount+1;
499
                  end
500
            end
501
         tagged SDMSsub_mb_type .xdata :
502
            begin
503
               infifo.deq();
504
               ////outfifo.enq(infifo.first());
505
               interSubMbTypeVector <= update(interSubMbTypeVector,interPassingCount,xdata);
506
               interPassingCount <= interPassingCount+1;
507
            end
508
         tagged SDMSref_idx_l0 .xdata :
509
            begin
510
               infifo.deq();
511
               ////outfifo.enq(infifo.first());
512
               interRefIdxVector <= update(interRefIdxVector,interPassingCount,xdata[3:0]);
513
               interPassingCount <= interPassingCount+1;
514
            end
515
         tagged SDMSmvd_l0 .xdata :
516
            begin
517
               infifo.deq();
518
               ////outfifo.enq(infifo.first());
519
               if(interPassingCount==1)
520
                  begin
521
                     Bit#(13) interMvDiffTemp2 = truncate(xdata);
522
                     interMvDiff.enq(tuple2(interMvDiffTemp,interMvDiffTemp2));
523
                     interPassingCount <= 0;
524
                  end
525
               else
526
                  begin
527
                     interMvDiffTemp <= truncate(xdata);
528
                     interPassingCount <= interPassingCount+1;
529
                  end
530
            end
531
         default: passFlag <= True;
532
      endcase
533
   endrule
534
 
535
 
536
   rule outputing ( currMbHor
537
      Bit#(1) outputFlag = 0;
538
      Vector#(4,Bit#(8)) outputVector = replicate(0);
539
      Bit#(2) blockHor = {outBlockNum[2],outBlockNum[0]};
540
      Bit#(2) blockVer = {outBlockNum[3],outBlockNum[1]};
541
      Bit#(2) pixelVer = {outPixelNum[3],outPixelNum[2]};
542
      Bit#(4) totalVer = {blockVer,pixelVer};
543
      //$display( "Trace Prediction: outputing" );
544
      if(outFirstQPFlag)
545
         begin
546
            if(infifo_ITB.first() matches tagged IBTmb_qp .xdata)
547
               begin
548
                  infifo_ITB.deq();
549
                  outfifo.enq(IBTmb_qp {qpy:xdata.qpy,qpc:xdata.qpc});
550
                  outFirstQPFlag <= False;
551
                  $display( "Trace Prediction: outputing outFirstQP %h %h %h", outBlockNum, outPixelNum, xdata);
552
               end
553
            else
554
               $display( "ERROR Prediction: outputing unexpected infifo_ITB.first()");
555
         end
556
      else if(nextoutputfifo.first() == SkipMB)
557
         begin
558
            if(interBSoutput && outChromaFlag==0 && outPixelNum==0)
559
               begin
560
                  interBSoutput <= False;
561
                  interBSfifo.deq();
562
                  Bit#(2) tempHorBS = tpl_1(interBSfifo.first());
563
                  Bit#(2) tempVerBS = tpl_2(interBSfifo.first());
564
                  Bit#(3) horBS = (tempHorBS==3 ? 4 : (interLeftNonZeroTransCoeff[blockVer] ? 2 : zeroExtend(tempHorBS)));
565
                  Bit#(3) verBS = (tempVerBS==3 ? 4 : (interTopNonZeroTransCoeff[blockHor]&&blockVer!=0 ? 2 : zeroExtend(tempVerBS)));
566
                  outfifo.enq(PBbS {bShor:horBS,bSver:verBS});
567
                  interLeftNonZeroTransCoeff <= update(interLeftNonZeroTransCoeff, blockVer, False);
568
                  interTopNonZeroTransCoeff <= update(interTopNonZeroTransCoeff, blockHor, False);
569
                  $display( "Trace Prediction: outputing SkipMB bS %h %h %h %h", outBlockNum, outPixelNum, currMbHor, currMbVer);
570
               end
571
            else
572
               begin
573
                  interBSoutput <= True;
574
                  outputVector = predictedfifo.first();
575
                  outfifo.enq(tagged PBoutput outputVector);
576
                  outputFlag = 1;
577
                  predictedfifo.deq();
578
                  $display( "Trace Prediction: outputing SkipMB out %h %h %h", outBlockNum, outPixelNum, outputVector);
579
               end
580
         end
581
      else
582
         begin
583
            case ( infifo_ITB.first() ) matches
584
               tagged IBTmb_qp .xdata :
585
                  begin
586
                     infifo_ITB.deq();
587
                     outfifo.enq(tagged IBTmb_qp {qpy:xdata.qpy,qpc:xdata.qpc});
588
                     outFirstQPFlag <= False;
589
                     $display( "Trace Prediction: outputing ITBmb_qp %h %h %h", outBlockNum, outPixelNum, xdata);
590
                  end
591
               tagged ITBresidual .xdata :
592
                  begin
593
                     if(interBSoutput && outChromaFlag==0 && outPixelNum==0)
594
                        begin
595
                           interBSoutput <= False;
596
                           if(outstatefifo.first() != Inter)
597
                              outfifo.enq(tagged PBbS {bShor:(blockHor==0 ? 4 : 3),bSver:(blockVer==0 ? 4 : 3)});
598
                           else
599
                              begin
600
                                 interBSfifo.deq();
601
                                 Bit#(2) tempHorBS = tpl_1(interBSfifo.first());
602
                                 Bit#(2) tempVerBS = tpl_2(interBSfifo.first());
603
                                 Bit#(3) horBS = (tempHorBS==3 ? 4 : 2);
604
                                 Bit#(3) verBS = (tempVerBS==3 ? 4 : 2);
605
                                 outfifo.enq(tagged PBbS {bShor:horBS,bSver:verBS});
606
                              end
607
                           interLeftNonZeroTransCoeff <= update(interLeftNonZeroTransCoeff, blockVer, True);
608
                           interTopNonZeroTransCoeff <= update(interTopNonZeroTransCoeff, blockHor, True);
609
                           $display( "Trace Prediction: outputing ITBresidual bS %h %h %h %h %h", outChromaFlag, outBlockNum, outPixelNum, currMbHor, currMbVer);
610
                        end
611
                     else
612
                        begin
613
                           interBSoutput <= True;
614
                           Bit#(11) tempOutputValue = 0;
615
                           for(Integer ii=0; ii<4; ii=ii+1)
616
                              begin
617
                                 tempOutputValue = signExtend(xdata[ii]) + zeroExtend((predictedfifo.first())[ii]);
618
                                 if(tempOutputValue[10]==1)
619
                                    outputVector[ii] = 0;
620
                                 else if(tempOutputValue[9:0] > 255)
621
                                    outputVector[ii] = 255;
622
                                 else
623
                                    outputVector[ii] = tempOutputValue[7:0];
624
                              end
625
                           outfifo.enq(tagged PBoutput outputVector);
626
                           infifo_ITB.deq();
627
                           predictedfifo.deq();
628
                           outputFlag = 1;
629
                           $display( "Trace Prediction: outputing ITBresidual out %h %h %h %h %h %h", outChromaFlag, outBlockNum, outPixelNum, predictedfifo.first(), xdata, outputVector);
630
                        end
631
                  end
632
               tagged ITBcoeffLevelZeros :
633
                  begin
634
                     if(interBSoutput && outChromaFlag==0 && outPixelNum==0)
635
                        begin
636
                           interBSoutput <= False;
637
                           if(outstatefifo.first() != Inter)
638
                              outfifo.enq(tagged PBbS {bShor:(blockHor==0 ? 4 : 3),bSver:(blockVer==0 ? 4 : 3)});
639
                           else
640
                              begin
641
                                 interBSfifo.deq();
642
                                 Bit#(2) tempHorBS = tpl_1(interBSfifo.first());
643
                                 Bit#(2) tempVerBS = tpl_2(interBSfifo.first());
644
                                 Bit#(3) horBS = (tempHorBS==3 ? 4 : (interLeftNonZeroTransCoeff[blockVer] ? 2 : zeroExtend(tempHorBS)));
645
                                 Bit#(3) verBS = (tempVerBS==3 ? 4 : (interTopNonZeroTransCoeff[blockHor]&&blockVer!=0 ? 2 : zeroExtend(tempVerBS)));
646
                                 outfifo.enq(tagged PBbS {bShor:horBS,bSver:verBS});
647
                              end
648
                           interLeftNonZeroTransCoeff <= update(interLeftNonZeroTransCoeff, blockVer, False);
649
                           interTopNonZeroTransCoeff <= update(interTopNonZeroTransCoeff, blockHor, False);
650
                           $display( "Trace Prediction: outputing ITBcoeffLevelZeros bS %h %h %h %h %h", outChromaFlag, outBlockNum, outPixelNum, currMbHor, currMbVer);
651
                        end
652
                     else
653
                        begin
654
                           interBSoutput <= True;
655
                           if(outPixelNum == 12)
656
                              infifo_ITB.deq();
657
                           outputVector = predictedfifo.first();
658
                           outfifo.enq(tagged PBoutput outputVector);
659
                           outputFlag = 1;
660
                           predictedfifo.deq();
661
                           $display( "Trace Prediction: outputing ITBcoeffLevelZeros out %h %h %h %h %h", outChromaFlag, outBlockNum, outPixelNum, predictedfifo.first(), outputVector);
662
                        end
663
                  end
664
               default: $display( "ERROR Prediction: outputing unknown infifo_ITB input" );
665
            endcase
666
         end
667
 
668
      if(outputFlag == 1)
669
         begin
670
            $display("ccl4PBoutput %0d", outputVector[0]);
671
            $display("ccl4PBoutput %0d", outputVector[1]);
672
            $display("ccl4PBoutput %0d", outputVector[2]);
673
            $display("ccl4PBoutput %0d", outputVector[3]);
674
 
675
            if(outBlockNum==0 && pixelVer==0 && outChromaFlag==0 && currMb!=firstMb && picWidth>1)
676
               begin
677
                  intraMemReqQ.enq(intraMemReqQdelay);
678
                  interMemReqQ.enq(interMemReqQdelay);
679
                  //$display( "TRACE Prediction: passing storing addr data");//////////////////
680
               end
681
 
682
            if(blockHor==3 || (blockHor[0]==1 && outChromaFlag==1) || (outstatefifo.first()==Intra4x4 && outChromaFlag==0))
683
               begin
684
                  if(outChromaFlag==0)
685
                     begin
686
                        Bit#(32) intraLeftValNextTemp = intraLeftValNext;
687
                        if(totalVer==0 || (outstatefifo.first()==Intra4x4 && pixelVer==0))
688
                           begin
689
                              Bit#(32) tempValSet = select(intraTopVal,zeroExtend(blockHor));
690
                              intraLeftValNextTemp = zeroExtend(tempValSet[31:24]);
691
                           end
692
                        case(pixelVer)
693
                           0:intraLeftValNext <= {intraLeftValNextTemp[31:16],outputVector[3],intraLeftValNextTemp[7:0]};
694
                           1:intraLeftValNext <= {intraLeftValNextTemp[31:24],outputVector[3],intraLeftValNextTemp[15:0]};
695
                           2:intraLeftValNext <= {outputVector[3],intraLeftValNextTemp[23:0]};
696
                           3:
697
                           begin
698
                              intraLeftVal <= update(intraLeftVal,blockVer,{outputVector[3],intraLeftValNextTemp});
699
                              intraLeftValNext <= zeroExtend(outputVector[3]);
700
                              if(outstatefifo.first()==Intra4x4)
701
                                 intra4x4typeLeft <= update(intra4x4typeLeft,blockVer,cur_intra4x4_pred_mode);
702
                              else if(outstatefifo.first()==Intra)
703
                                 intra4x4typeLeft <= update(intra4x4typeLeft,blockVer,13);
704
                              else
705
                                 intra4x4typeLeft <= update(intra4x4typeLeft,blockVer,14);
706
                           end
707
                        endcase
708
                     end
709
                  else
710
                     begin
711
                        if(outBlockNum[2]==0)
712
                           intraLeftValChroma0 <= update(intraLeftValChroma0,totalVer+1,outputVector[3]);
713
                        else
714
                           intraLeftValChroma1 <= update(intraLeftValChroma1,totalVer+1,outputVector[3]);
715
                     end
716
               end
717
 
718
            if(pixelVer==3 && (blockVer==3 || (blockVer[0]==1 && outChromaFlag==1) || (outstatefifo.first()==Intra4x4 && outChromaFlag==0)))
719
               begin
720
                  if(outChromaFlag==0)
721
                     begin
722
                        intraTopVal <= update(intraTopVal,zeroExtend(blockHor),{outputVector[3],outputVector[2],outputVector[1],outputVector[0]});
723
                        if(outstatefifo.first()==Intra4x4)
724
                           intra4x4typeTop <= update(intra4x4typeTop,blockHor,cur_intra4x4_pred_mode);
725
                        else if(outstatefifo.first()==Intra)
726
                           intra4x4typeTop <= update(intra4x4typeTop,blockHor,13);
727
                        else
728
                           intra4x4typeTop <= update(intra4x4typeTop,blockHor,14);
729
                     end
730
                  else
731
                     begin
732
                        if(outBlockNum[2]==0)
733
                           begin
734
                              Vector#(4,Bit#(16)) intraTopValChroma0Next = intraTopValChroma0;
735
                              intraTopValChroma0Next[{blockHor[0],1'b0}] = {outputVector[1],outputVector[0]};
736
                              intraTopValChroma0Next[{blockHor[0],1'b1}] = {outputVector[3],outputVector[2]};
737
                              intraTopValChroma0 <= intraTopValChroma0Next;
738
                           end
739
                        else
740
                           begin
741
                              Vector#(4,Bit#(16)) intraTopValChroma1Next = intraTopValChroma1;
742
                              intraTopValChroma1Next[{blockHor[0],1'b0}] = {outputVector[1],outputVector[0]};
743
                              intraTopValChroma1Next[{blockHor[0],1'b1}] = {outputVector[3],outputVector[2]};
744
                              intraTopValChroma1 <= intraTopValChroma1Next;
745
                           end
746
                     end
747
               end
748
 
749
            if(outChromaFlag==1 && outBlockNum==7)
750
               begin
751
                  Bit#(PicWidthSz) tempStoreAddr = truncate(currMbHor);
752
                  InterBlockMv outBlockMv = interOutBlockMvfifo.first();
753
                  if(outBlockMv matches tagged BlockMv .bdata)
754
                     begin
755
                        outBlockMv = (BlockMv {refIdx:bdata.refIdx,mvhor:bdata.mvhor,mvver:bdata.mvver,nonZeroTransCoeff:(interTopNonZeroTransCoeff[pixelVer]?1:0)});
756
                        interOutBlockMvfifo.deq();
757
                     end
758
                  else if(pixelVer==3)
759
                     interOutBlockMvfifo.deq();
760
                  if(pixelVer==3 && picWidth>1)
761
                     interMemReqQdelay <= StoreReq {addr:{tempStoreAddr,pixelVer},data:pack(outBlockMv)};
762
                  else
763
                     interMemReqQ.enq(tagged StoreReq {addr:{tempStoreAddr,pixelVer},data:pack(outBlockMv)});
764
                  if(pixelVer>0)
765
                     begin
766
                        Bit#(4)  intra4x4typeTopStore = ((outstatefifo.first()==Inter) ? 14 : ((outstatefifo.first()!=Intra4x4) ? 13: intra4x4typeTop[(pixelVer-1)]));
767
                        Bit#(32) intraTopValStore = intraTopVal[(pixelVer-1)];
768
                        Bit#(16) intraTopValChroma0Store = intraTopValChroma0[(pixelVer-1)];
769
                        Bit#(16) intraTopValChroma1Store = (pixelVer<3 ? intraTopValChroma1[(pixelVer-1)] : {outputVector[1],outputVector[0]});
770
                        Bit#(68) intraStore = {intra4x4typeTopStore,intraTopValChroma1Store,intraTopValChroma0Store,intraTopValStore};
771
                        intraMemReqQ.enq(tagged StoreReq {addr:{tempStoreAddr,(pixelVer-1)},data:intraStore});
772
                        if(pixelVer==3)
773
                           begin
774
                              intra4x4typeTopStore = ((outstatefifo.first()==Inter) ? 14 : ((outstatefifo.first()!=Intra4x4) ? 13: intra4x4typeTop[3]));
775
                              intraTopValStore = intraTopVal[3];
776
                              intraTopValChroma0Store = intraTopValChroma0[3];
777
                              intraTopValChroma1Store = {outputVector[3],outputVector[2]};
778
                              intraStore = {intra4x4typeTopStore,intraTopValChroma1Store,intraTopValChroma0Store,intraTopValStore};
779
                              intraMemReqQdelay <= StoreReq {addr:{tempStoreAddr,2'b11},data:intraStore};
780
                           end
781
                     end
782
               end
783
            outPixelNum <= outPixelNum+4;
784
            if(outPixelNum == 12)
785
               begin
786
                  if(outChromaFlag==0)
787
                     begin
788
                        outBlockNum <= outBlockNum+1;
789
                        if(outBlockNum == 15)
790
                           outChromaFlag <= 1;
791
                        if(nextoutputfifo.first() == Intra4x4)
792
                           nextoutputfifo.deq();
793
                     end
794
                  else
795
                     begin
796
                        if(outBlockNum == 7)
797
                           begin
798
                              outBlockNum <= 0;
799
                              outChromaFlag <= 0;
800
                              currMb <= currMb+1;
801
                              currMbHor <= currMbHor+1;
802
                              interCurrMbDiff <= interCurrMbDiff-1;
803
                              outstatefifo.deq;
804
                              intrastate <= Start;
805
                              if(truncate(currMbHor)==picWidth-1 && currMbVer==picHeight-1)
806
                                 interpolator.endOfFrame();
807
                              nextoutputfifo.deq();
808
                           end
809
                        else
810
                           outBlockNum <= outBlockNum+1;
811
                     end
812
               end
813
         end
814
   endrule
815
 
816
 
817
   rule currMbHorUpdate( !(currMbHor
818
      Bit#(PicAreaSz) temp = zeroExtend(picWidth);
819
      if((currMbHor >> 3) >= temp)
820
         begin
821
            currMbHor <= currMbHor - (temp << 3);
822
            currMbVer <= currMbVer + 8;
823
         end
824
      else
825
         begin
826
            currMbHor <= currMbHor - temp;
827
            currMbVer <= currMbVer + 1;
828
         end
829
      //$display( "Trace Prediction: currMbHorUpdate %h %h", currMbHor, currMbVer);
830
   endrule
831
 
832
 
833
   // inter prediction rules
834
 
835
   rule interSendReq ( interReqCount>0 && currMbHor
836
      Bit#(PicAreaSz) currMbHorTemp = currMbHor+zeroExtend(interCurrMbDiff)-1;
837
      Bit#(PicAreaSz) currMbTemp = currMb+zeroExtend(interCurrMbDiff)-1;
838
      if( currMbHorTemp >= zeroExtend(picWidth) )
839
         currMbHorTemp = currMbHorTemp-zeroExtend(picWidth);
840
      Bit#(PicWidthSz) temp2 = truncate(currMbHorTemp);
841
      Bit#(TAdd#(PicWidthSz,2)) temp = 0;
842
      Bool noMoreReq = False;
843
      if( currMbTemp < zeroExtend(picWidth) )
844
         noMoreReq = True;
845
      else
846
         begin
847
            if(interReqCount<5)
848
               begin
849
                  Bit#(2) temp3 = truncate(interReqCount-1);
850
                  temp = {temp2,temp3};
851
               end
852
            else if(interReqCount==5)
853
               begin
854
                  if((currMbHorTemp+1)
855
                     temp = {(temp2+1),2'b00};
856
                  else if(currMbHorTemp>0 && currMbTemp-firstMb>zeroExtend(picWidth))
857
                     temp = {(temp2-1),2'b11};
858
                  else
859
                     noMoreReq = True;
860
               end
861
            else if(interReqCount==6)
862
               begin
863
                  if((currMbHorTemp+1)0 && currMbTemp-firstMb>zeroExtend(picWidth))
864
                     temp = {(temp2-1),2'b11};
865
                  else
866
                     noMoreReq = True;
867
               end
868
            else
869
               noMoreReq = True;
870
         end
871
      if(!noMoreReq)
872
         begin
873
            interMemReqQ.enq(tagged LoadReq temp);
874
            interReqCount <= interReqCount+1;
875
            //$display( "TRACE Prediction: interSendReq addr %0d",temp);///////////////////////
876
         end
877
      else
878
         interReqCount <= 0;
879
      $display( "Trace Prediction: interSendReq %h %h %h", interstate, interReqCount, temp);
880
   endrule
881
 
882
 
883
   rule interReceiveNoResp ( interRespCount>0 && currMbHor
884
      Bit#(PicAreaSz) currMbHorTemp = currMbHor+zeroExtend(interCurrMbDiff)-1;
885
      if( currMbHorTemp >= zeroExtend(picWidth) )
886
         currMbHorTemp = currMbHorTemp-zeroExtend(picWidth);
887
      interRespCount <= 0;
888
      interStepCount <= 1;
889
      interIPStepCount <= 1;
890
      if(currMbHorTemp == 0)
891
         begin
892
            interLeftVal <= replicate(tagged NotInter 0);
893
            interTopLeftVal <= replicate(tagged NotInter 0);
894
         end
895
      $display( "Trace Prediction: interReceiveNoResp %h %h", interstate, interRespCount);
896
   endrule
897
 
898
 
899
   rule interReceiveResp ( interRespCount>0 && interRespCount<7 && currMbHor
900
      Bit#(PicAreaSz) currMbHorTemp = currMbHor+zeroExtend(interCurrMbDiff)-1;
901
      Bit#(PicAreaSz) currMbTemp = currMb+zeroExtend(interCurrMbDiff)-1;
902
      if( currMbHorTemp >= zeroExtend(picWidth) )
903
         currMbHorTemp = currMbHorTemp-zeroExtend(picWidth);
904
      Bool noMoreResp = False;
905
      Bit#(2) temp2bit = 0;
906
      InterBlockMv unpackedData = unpack(data);
907
      Vector#(5,InterBlockMv) interTopValNext = interTopVal;
908
      Vector#(4,InterBlockMv) interTopLeftValNext = interTopLeftVal;
909
      if(interRespCount<5)
910
         begin
911
            temp2bit = truncate(interRespCount-1);
912
            interTopValNext[temp2bit] = unpackedData;
913
            if((interRespCount==4 || (interRespCount==1 && (interstate==InterPskip || interstate==InterP16x16 || interstate==InterP16x8)))
914
               && (!((currMbHorTemp+1)0 && currMbTemp-firstMb>zeroExtend(picWidth))))
915
               noMoreResp = True;
916
         end
917
      else if(interRespCount==5)
918
         begin
919
            if((currMbHorTemp+1)
920
               begin
921
                  interTopValNext[4] = unpackedData;
922
                  if(!(currMbHorTemp>0 && currMbTemp-firstMb>zeroExtend(picWidth)))
923
                     noMoreResp = True;
924
               end
925
            else
926
               begin
927
                  interTopLeftValNext[0] = unpackedData;
928
                  noMoreResp = True;
929
               end
930
         end
931
      else
932
         begin
933
            interTopLeftValNext[0] = unpackedData;
934
            noMoreResp = True;
935
         end
936
      interMemRespQ.deq();
937
      //$display( "TRACE Prediction: interReceiveResp data %h",data);///////////////////////
938
      if(!noMoreResp)
939
         interRespCount <= interRespCount+1;
940
      else
941
         begin
942
            interRespCount <= 0;
943
            interStepCount <= 1;
944
            interIPStepCount <= 1;
945
            if(currMbHorTemp == 0)
946
               begin
947
                  interLeftVal <= replicate(tagged NotInter 0);
948
                  interTopLeftValNext = replicate(tagged NotInter 0);
949
               end
950
         end
951
      interTopVal <= interTopValNext;
952
      interTopLeftVal <= interTopLeftValNext;
953
      $display( "Trace Prediction: interReceiveResp %h %h %h", interstate, interRespCount, data);
954
   endrule
955
 
956
 
957
   rule interProcessStep ( interStepCount>0 && currMbHor
958
      Bit#(PicAreaSz) currMbTemp = currMb+zeroExtend(interCurrMbDiff)-1;
959
      Bit#(2) blockHor = {interMbPartNum[0],interSubMbPartNum[0]};
960
      Bit#(2) blockVer = {interMbPartNum[1],interSubMbPartNum[1]};
961
 
962
      if(interStepCount == 1)
963
         begin
964
           Bit#(3) partWidth = 0;
965
           Bit#(3) partHeight = 0;
966
           Bit#(3) numPart = 1;
967
           Bit#(3) numSubPart = 1;
968
           Bit#(2) subMbType = 0;
969
           Bool noBlockC = False;
970
           Bool calcmv = False;
971
           Bool leftmv = False;
972
           if(interstate==InterPskip || interstate==InterP16x16)
973
              begin
974
                 partWidth = 4;
975
                 partHeight = 4;
976
                 numPart = 1;
977
                 calcmv = (interMbPartNum==0 && interSubMbPartNum==0);
978
                 leftmv = (blockHor>0);
979
              end
980
           else if(interstate==InterP16x8)
981
              begin
982
                 partWidth = 4;
983
                 partHeight = 2;
984
                 numPart = 2;
985
                 if(interMbPartNum==2)
986
                    noBlockC = True;
987
                 calcmv = (interMbPartNum[0]==0 && interSubMbPartNum==0);
988
                 leftmv = (blockHor>0);
989
              end
990
           else if(interstate==InterP8x16)
991
              begin
992
                 partWidth = 2;
993
                 partHeight = 4;
994
                 numPart = 2;
995
                 calcmv = (interMbPartNum[1]==0 && interSubMbPartNum==0);
996
                 leftmv = !(blockVer>0);
997
              end
998
           else if(interstate==InterP8x8 || interstate==InterP8x8ref0)
999
              begin
1000
                 numPart = 4;
1001
                 subMbType = interSubMbTypeVector[interMbPartNum];
1002
                 numSubPart = numSubMbPart(subMbType);
1003
                 case(subMbType)
1004
                    0:
1005
                    begin
1006
                       partWidth = 2;
1007
                       partHeight = 2;
1008
                       if(interMbPartNum==3)
1009
                          noBlockC = True;
1010
                       calcmv = (interSubMbPartNum==0);
1011
                       leftmv = (blockHor[0]>0);
1012
                    end
1013
                    1:
1014
                    begin
1015
                       partWidth = 2;
1016
                       partHeight = 1;
1017
                       if(interSubMbPartNum==2)
1018
                          noBlockC = True;
1019
                       calcmv = (interSubMbPartNum[0]==0);
1020
                       leftmv = True;
1021
                    end
1022
                    2:
1023
                    begin
1024
                       partWidth = 1;
1025
                       partHeight = 2;
1026
                       calcmv = (interSubMbPartNum[1]==0);
1027
                       leftmv = False;
1028
                    end
1029
                    3:
1030
                    begin
1031
                      partWidth = 1;
1032
                      partHeight = 1;
1033
                      if(interSubMbPartNum==3)
1034
                         noBlockC = True;
1035
                      calcmv = True;
1036
                    end
1037
                 endcase
1038
             end
1039
          else
1040
            $display( "ERROR Prediction: interProcessStep unexpected interstate");
1041
 
1042
          Bit#(4) refIndex = ((interstate==InterPskip||interstate==InterP8x8ref0) ? 0 : interRefIdxVector[interMbPartNum]);
1043
          Vector#(3,InterBlockMv) blockABC = replicate(tagged NotInter 0);
1044
          if( currMbTemp-firstMb==0 && blockHor==0 )
1045
             blockABC[0] = (tagged NotInter 0);
1046
          else
1047
             blockABC[0] = interLeftVal[blockVer];
1048
          if( currMbTemp-firstMb
1049
             blockABC[1] = (tagged NotInter 0);
1050
          else
1051
             blockABC[1] = interTopVal[blockHor];
1052
          blockABC[2] = interTopVal[{1'b0,blockHor}+partWidth];
1053
          if(noBlockC || blockABC[2]==(tagged NotInter 0))
1054
             blockABC[2] = interTopLeftVal[blockVer];
1055
          partWidthR <= partWidth;
1056
          partHeightR <= partHeight;
1057
          numPartR <= numPart;
1058
          numSubPartR <= numSubPart;
1059
          subMbTypeR <= subMbType;
1060
          calcmvR <= calcmv;
1061
          leftmvR <= leftmv;
1062
          refIndexR <= refIndex;
1063
          blockABCR <= blockABC;
1064
          interStepCount <= 2;
1065
      end
1066
   else if(interStepCount==2)
1067
      begin
1068
         Bit#(3) partWidth = partWidthR;
1069
         Bit#(3) partHeight = partHeightR;
1070
         Bit#(3) numPart = numPartR;
1071
         Bit#(3) numSubPart = numSubPartR;
1072
         Bit#(2) subMbType = subMbTypeR;
1073
         Bool calcmv = calcmvR;
1074
         Bool leftmv = leftmvR;
1075
         Bit#(4) refIndex = refIndexR;
1076
         Vector#(3,InterBlockMv) blockABC = blockABCR;
1077
         Bit#(14) mvhorfinal = 0;
1078
         Bit#(12) mvverfinal = 0;
1079
         Bit#(5) interNewestMvNext = 0;
1080
         if(calcmv)//motion vector caculation
1081
            begin
1082
               Vector#(3,Int#(14)) mvhorABC = replicate(0);
1083
               Vector#(3,Int#(12)) mvverABC = replicate(0);
1084
               Bit#(2) validCount = 0;
1085
               Bit#(14) mvhorPred = 0;
1086
               Bit#(12) mvverPred = 0;
1087
               for(Integer ii=0; ii<3; ii=ii+1)
1088
                  begin
1089
                     if(blockABC[ii] matches tagged BlockMv .xdata)
1090
                        begin
1091
                           mvhorABC[ii] = unpack(xdata.mvhor);
1092
                           mvverABC[ii] = unpack(xdata.mvver);
1093
                           if(xdata.refIdx == refIndex)
1094
                              begin
1095
                                 validCount = validCount+1;
1096
                                 mvhorPred = xdata.mvhor;
1097
                                 mvverPred = xdata.mvver;
1098
                              end
1099
                        end
1100
                     else
1101
                       begin
1102
                          mvhorABC[ii] = 0;
1103
                          mvverABC[ii] = 0;
1104
                       end
1105
                  end
1106
               if(validCount != 1)//median
1107
                  begin
1108
                     if(mvhorABC[0]>mvhorABC[1] && mvhorABC[0]>mvhorABC[2])
1109
                        mvhorPred = pack((mvhorABC[1]>mvhorABC[2]) ? mvhorABC[1] : mvhorABC[2]);
1110
                     else if(mvhorABC[0]
1111
                        mvhorPred = pack((mvhorABC[1]
1112
                     else
1113
                        mvhorPred = pack(mvhorABC[0]);
1114
                     if(mvverABC[0]>mvverABC[1] && mvverABC[0]>mvverABC[2])
1115
                        mvverPred = pack((mvverABC[1]>mvverABC[2]) ? mvverABC[1] : mvverABC[2]);
1116
                     else if(mvverABC[0]
1117
                        mvverPred = pack((mvverABC[1]
1118
                     else
1119
                        mvverPred = pack(mvverABC[0]);
1120
                  end
1121
               if(interstate==InterPskip)
1122
                  begin
1123
                     for(Integer ii=0; ii<2; ii=ii+1)
1124
                        begin
1125
                           if(blockABC[ii] matches tagged BlockMv .xdata)
1126
                              begin
1127
                                 if(xdata.refIdx==0 && xdata.mvhor==0 && xdata.mvver==0)
1128
                                    begin
1129
                                       mvhorPred = 0;
1130
                                       mvverPred = 0;
1131
                                    end
1132
                              end
1133
                           else if(blockABC[ii] matches tagged NotInter 0)
1134
                              begin
1135
                                 mvhorPred = 0;
1136
                                 mvverPred = 0;
1137
                              end
1138
                        end
1139
                  end
1140
               else if(interstate==InterP16x8 || interstate==InterP8x16)
1141
                  begin
1142
                     InterBlockMv blockCheck;
1143
                     if(interstate==InterP16x8)
1144
                        begin
1145
                           if(interMbPartNum==0)
1146
                              blockCheck = blockABC[1];
1147
                           else
1148
                              blockCheck = blockABC[0];
1149
                        end
1150
                     else
1151
                        begin
1152
                           if(interMbPartNum==0)
1153
                              blockCheck = blockABC[0];
1154
                           else
1155
                              blockCheck = blockABC[2];
1156
                        end
1157
                     if(blockCheck matches tagged BlockMv .xdata &&& xdata.refIdx==refIndex)
1158
                        begin
1159
                           mvhorPred = xdata.mvhor;
1160
                           mvverPred = xdata.mvver;
1161
                        end
1162
                  end
1163
               mvhorfinal = mvhorPred;
1164
               mvverfinal = mvverPred;
1165
               if(interstate!=InterPskip)
1166
                  begin
1167
                     mvhorfinal = truncate(tpl_1(interMvDiff.first()) + signExtend(mvhorPred));
1168
                     mvverfinal = truncate(tpl_2(interMvDiff.first()) + signExtend(mvverPred));
1169
                     interMvDiff.deq();
1170
                  end
1171
               interMvFile.upd({interMbPartNum,interSubMbPartNum},tuple2(mvhorfinal,mvverfinal));
1172
               interNewestMvNext = zeroExtend({interMbPartNum,interSubMbPartNum})+1;
1173
               $display( "Trace Prediction: interProcessStep %h %h %h %h %h %h %h %h %h", interstate, interStepCount, interMbPartNum, interSubMbPartNum, pack(blockABC[0]), pack(blockABC[1]), pack(blockABC[2]), mvhorPred, mvverPred);
1174
            end
1175
         else
1176
            begin
1177
               if(leftmv)
1178
                  begin
1179
                     if(blockABC[0] matches tagged BlockMv .xdata)
1180
                        begin
1181
                           mvhorfinal = unpack(xdata.mvhor);
1182
                           mvverfinal = unpack(xdata.mvver);
1183
                        end
1184
                     else
1185
                        $display( "ERROR Prediction: interProcessStep unexpected blockABC[0]");
1186
                  end
1187
               else
1188
                  begin
1189
                     if(blockABC[1] matches tagged BlockMv .xdata)
1190
                        begin
1191
                           mvhorfinal = unpack(xdata.mvhor);
1192
                           mvverfinal = unpack(xdata.mvver);
1193
                        end
1194
                     else
1195
                        $display( "ERROR Prediction: interProcessStep unexpected blockABC[1]");
1196
                  end
1197
            end
1198
 
1199
            mvhorfinalR <= mvhorfinal;
1200
            mvverfinalR <= mvverfinal;
1201
            interNewestMvNextR <= interNewestMvNext;
1202
            interStepCount <= 3;
1203
 
1204
         end
1205
      else // stepCount == 3
1206
         begin
1207
            Bit#(2) tempBShor = 0;//bS calculation
1208
            Bit#(2) tempBSver = 0;
1209
            Bool allDone = False;
1210
            Bit#(4) refIndex = refIndexR;
1211
            Bit#(14) mvhorfinal = mvhorfinalR;
1212
            Bit#(12) mvverfinal = mvverfinalR;
1213
            Bit#(5) interNewestMvNext = interNewestMvNextR;
1214
 
1215
            if(interLeftVal[blockVer] matches tagged BlockMv .xdata)
1216
               begin
1217
                  if(xdata.nonZeroTransCoeff == 1)
1218
                     tempBShor = 2;
1219
                  else
1220
                     begin
1221
                       if(xdata.refIdx!=refIndex || absDiffGEFour14(mvhorfinal,xdata.mvhor) || absDiffGEFour12(mvverfinal,xdata.mvver))
1222
                           tempBShor = 1;
1223
                       else
1224
                           tempBShor = 0;
1225
                     end
1226
               end
1227
            else
1228
            tempBShor = 3;
1229
            if(interTopVal[blockHor] matches tagged BlockMv .xdata)
1230
               begin
1231
                  if(xdata.nonZeroTransCoeff == 1)
1232
                     tempBSver = 2;
1233
                  else
1234
                     begin
1235
                        if(xdata.refIdx!=refIndex || absDiffGEFour14(mvhorfinal,xdata.mvhor) || absDiffGEFour12(mvverfinal,xdata.mvver))
1236
                           tempBSver = 1;
1237
                        else
1238
                           tempBSver = 0;
1239
                     end
1240
               end
1241
            else
1242
               tempBSver = 3;
1243
            interBSfifo.enq(tuple2(tempBShor,tempBSver));
1244
            Vector#(5,InterBlockMv) interTopValNext = interTopVal;//update inter*Val
1245
            Vector#(4,InterBlockMv) interLeftValNext = interLeftVal;
1246
            Vector#(4,InterBlockMv) interTopLeftValNext = interTopLeftVal;
1247
            interLeftValNext[blockVer] = (BlockMv {refIdx:refIndex,mvhor:mvhorfinal,mvver:mvverfinal,nonZeroTransCoeff:0});
1248
            interTopValNext[blockHor] = (BlockMv {refIdx:refIndex,mvhor:mvhorfinal,mvver:mvverfinal,nonZeroTransCoeff:0});
1249
            interTopLeftValNext[blockVer] = interTopVal[blockHor];
1250
            interTopVal <= interTopValNext;
1251
            interLeftVal <= interLeftValNext;
1252
            interTopLeftVal <= interTopLeftValNext;
1253
            if(blockVer == 3)
1254
               interOutBlockMvfifo.enq(tagged BlockMv {refIdx:refIndex,mvhor:mvhorfinal,mvver:mvverfinal,nonZeroTransCoeff:0});
1255
            if(interSubMbPartNum == 3)//next step
1256
               begin
1257
                  interSubMbPartNum <= 0;
1258
                  if(interMbPartNum == 3)
1259
                     begin
1260
                        interMbPartNum <= 0;
1261
                        allDone = True;
1262
                        interNewestMvNext = 16;
1263
                     end
1264
                  else
1265
                     interMbPartNum <= interMbPartNum+1;
1266
               end
1267
            else
1268
               interSubMbPartNum <= interSubMbPartNum+1;
1269
            if(interNewestMvNext > 0)
1270
               interNewestMv <= interNewestMvNext;
1271
 
1272
            // Check to see if we are done.
1273
 
1274
            if(allDone)
1275
               interStepCount <= 0;
1276
            else
1277
               interStepCount <= 1;
1278
 
1279
            $display( "Trace Prediction: interProcessStep final %h %h %h %h %h %h %h",interstate,interStepCount,interMbPartNum,interSubMbPartNum,mvhorfinal,mvverfinal,interNewestMvNext);
1280
 
1281
       end
1282
   endrule
1283
 
1284
 
1285
   rule interIPProcessStep ( interIPStepCount>0 && currMbHorzeroExtend({interIPMbPartNum,interIPSubMbPartNum}) );
1286
      Bit#(PicAreaSz) currMbHorTemp = currMbHor+zeroExtend(interCurrMbDiff)-1;
1287
      Bit#(PicHeightSz) currMbVerTemp = currMbVer;
1288
      if( currMbHorTemp >= zeroExtend(picWidth) )
1289
         begin
1290
            currMbHorTemp = currMbHorTemp-zeroExtend(picWidth);
1291
            currMbVerTemp = currMbVerTemp+1;
1292
         end
1293
      Bit#(2) blockHor = {interIPMbPartNum[0],interIPSubMbPartNum[0]};
1294
      Bit#(2) blockVer = {interIPMbPartNum[1],interIPSubMbPartNum[1]};
1295
      Bit#(3) numPart = 1;
1296
      Bit#(3) numSubPart = 1;
1297
      Bit#(2) subMbType = 0;
1298
      if(interstate==InterPskip || interstate==InterP16x16)
1299
         numPart = 1;
1300
      else if(interstate==InterP16x8)
1301
         numPart = 2;
1302
      else if(interstate==InterP8x16)
1303
         numPart = 2;
1304
      else if(interstate==InterP8x8 || interstate==InterP8x8ref0)
1305
         begin
1306
            numPart = 4;
1307
            subMbType = interSubMbTypeVector[interIPMbPartNum];
1308
            numSubPart = numSubMbPart(subMbType);
1309
         end
1310
      else
1311
         $display( "ERROR Prediction: interIPProcessStep unexpected interstate");
1312
      Bit#(4) refIndex = ((interstate==InterPskip||interstate==InterP8x8ref0) ? 0 : interRefIdxVector[interIPMbPartNum]);
1313
      Bit#(PicWidthSz) currMbHorT = truncate(currMbHorTemp);
1314
      Bit#(TAdd#(PicWidthSz,2)) horTemp = {currMbHorT,blockHor};
1315
      Bit#(TAdd#(PicHeightSz,4)) verTemp = {currMbVerTemp,blockVer,2'b00};
1316
      IPBlockType btTemp = IP16x16;
1317
      if(interstate==InterPskip || interstate==InterP16x16)
1318
         btTemp = IP16x16;
1319
      else if(interstate==InterP16x8)
1320
         btTemp = IP16x8;
1321
      else if(interstate==InterP8x16)
1322
         btTemp = IP8x16;
1323
      else
1324
         begin
1325
            case(subMbType)
1326
               0: btTemp = IP8x8;
1327
               1: btTemp = IP8x4;
1328
               2: btTemp = IP4x8;
1329
               3: btTemp = IP4x4;
1330
            endcase
1331
         end
1332
      Bit#(14) mvhorTemp = tpl_1(interMvFile.sub({interIPMbPartNum,interIPSubMbPartNum}));
1333
      Bit#(12) mvverTemp = tpl_2(interMvFile.sub({interIPMbPartNum,interIPSubMbPartNum}));
1334
      if(interIPStepCount == 1)
1335
         begin
1336
            if(!(interstate==InterP8x8 || interstate==InterP8x8ref0))
1337
               begin
1338
                  numPart = 4;
1339
                  Bit#(2) interIPMbPartNumTemp = interIPMbPartNum;
1340
                  if(btTemp==IP16x16)
1341
                     interIPMbPartNumTemp = 0;
1342
                  else if(btTemp==IP16x8 && interIPMbPartNumTemp[0]==1)
1343
                     interIPMbPartNumTemp = interIPMbPartNumTemp-1;
1344
                  else if(btTemp==IP8x16 && interIPMbPartNumTemp[1]==1)
1345
                     interIPMbPartNumTemp = interIPMbPartNumTemp-2;
1346
                  refIndex = ((interstate==InterPskip||interstate==InterP8x8ref0) ? 0 : interRefIdxVector[interIPMbPartNumTemp]);
1347
                  btTemp = IP8x8;
1348
                  mvhorTemp = tpl_1(interMvFile.sub({interIPMbPartNumTemp,2'b00}));
1349
                  mvverTemp = tpl_2(interMvFile.sub({interIPMbPartNumTemp,2'b00}));
1350
                  interpolator.request(IPLuma {refIdx:refIndex,hor:horTemp,ver:verTemp,mvhor:mvhorTemp,mvver:mvverTemp,bt:btTemp});
1351
               end
1352
            else
1353
               interpolator.request(IPLuma {refIdx:refIndex,hor:horTemp,ver:verTemp,mvhor:mvhorTemp,mvver:mvverTemp,bt:btTemp});
1354
         end
1355
      else
1356
         interpolator.request(IPChroma {refIdx:refIndex,uv:interIPStepCount[0],hor:horTemp,ver:truncate(verTemp>>1),mvhor:mvhorTemp,mvver:mvverTemp,bt:btTemp});
1357
      if(interIPSubMbPartNum >= truncate(numSubPart-1))
1358
         begin
1359
            interIPSubMbPartNum <= 0;
1360
            if(interIPMbPartNum >= truncate(numPart-1))
1361
               begin
1362
                  interIPMbPartNum <= 0;
1363
                  interIPStepCount <= interIPStepCount+1;
1364
               end
1365
            else
1366
               begin
1367
                  if(btTemp == IP16x8)
1368
                     interIPMbPartNum <= 2;
1369
                  else
1370
                     interIPMbPartNum <= interIPMbPartNum+1;
1371
               end
1372
         end
1373
      else
1374
         begin
1375
            if(subMbType == 1)
1376
               interIPSubMbPartNum <= 2;
1377
            else
1378
               interIPSubMbPartNum <= interIPSubMbPartNum+1;
1379
         end
1380
      $display( "Trace Prediction: interIPProcessStep %h %h %h %h %h %h %h %h %h %h", interstate, interIPStepCount, interIPMbPartNum, interIPSubMbPartNum, refIndex, horTemp, verTemp, mvhorTemp, mvverTemp, pack(btTemp));
1381
   endrule
1382
 
1383
 
1384
   rule interDone ( interstate!=Start && interReqCount==0 && interRespCount==0 && interStepCount==0 && interIPStepCount==0 );
1385
      interstate <= Start;
1386
      //$display( "Trace Prediction: interOutputTransfer %h %h", interstate, interOutputCount);
1387
   endrule
1388
 
1389
 
1390
   rule interOutputTransfer ( True );
1391
      predictedfifo.enq(interpolator.first());
1392
      interpolator.deq();
1393
      //$display( "Trace Prediction: interOutputTransfer %h %h", interstate, interOutputCount);
1394
   endrule
1395
 
1396
 
1397
 
1398
   // intra prediction rules
1399
 
1400
   rule intraSendReq ( intraReqCount>0 && currMbHor
1401
      Bit#(PicWidthSz) temp2 = truncate(currMbHor);
1402
      Bit#(TAdd#(PicWidthSz,2)) temp = 0;
1403
      Bit#(1) noMoreReq = 0;
1404
      if( currMb-firstMb < zeroExtend(picWidth) )
1405
         noMoreReq = 1;
1406
      else
1407
         begin
1408
            if(intraReqCount<5)
1409
               begin
1410
                  Bit#(2) temp3 = truncate(intraReqCount-1);
1411
                  temp = {temp2,temp3};
1412
               end
1413
            else if(intraReqCount==5)
1414
               begin
1415
                  if((currMbHor+1)
1416
                     temp = {(temp2+1),2'b00};
1417
                  else if(currMbHor>0 && currMb-firstMb>zeroExtend(picWidth))
1418
                     temp = {(temp2-1),2'b11};
1419
                  else
1420
                     noMoreReq = 1;
1421
               end
1422
            else if(intraReqCount==6)
1423
               begin
1424
                  if((currMbHor+1)0 && currMb-firstMb>zeroExtend(picWidth))
1425
                     temp = {(temp2-1),2'b11};
1426
                  else
1427
                     noMoreReq = 1;
1428
               end
1429
            else
1430
               noMoreReq = 1;
1431
         end
1432
      if(noMoreReq == 0)
1433
         begin
1434
            intraMemReqQ.enq(tagged LoadReq temp);
1435
            intraReqCount <= intraReqCount+1;
1436
            //$display( "TRACE Prediction: intraSendReq addr %0d",temp);///////////////////////
1437
         end
1438
      else
1439
         intraReqCount <= 0;
1440
      $display( "Trace Prediction: intraSendReq");
1441
   endrule
1442
 
1443
 
1444
   rule intraReceiveNoResp ( intraRespCount>0 && currMbHor
1445
      intra4x4typeTop <= replicate(15);
1446
      intraRespCount <= 0;
1447
      intraStepCount <= 1;
1448
      blockNum <= 0;
1449
      pixelNum <= 0;
1450
      interOutBlockMvfifo.enq(tagged NotInter 1);
1451
      $display( "Trace Prediction: intraReceiveNoResp");
1452
   endrule
1453
 
1454
 
1455
   rule intraReceiveResp ( intraRespCount>0 && intraRespCount<7 && currMbHor
1456
      Bit#(1) noMoreResp = 0;
1457
      Bit#(2) temp2bit = 0;
1458
      if(intraRespCount<5)
1459
         begin
1460
            temp2bit = truncate(intraRespCount-1);
1461
            intra4x4typeTop <= update(intra4x4typeTop, temp2bit, data[67:64]);
1462
            if(intraRespCount==4)
1463
               begin
1464
                  Vector#(5,Bit#(32)) intraTopValTemp = intraTopVal;
1465
                  intraTopValTemp[3] = data[31:0];
1466
                  intraTopValTemp[4] = {data[31:24],data[31:24],data[31:24],data[31:24]};
1467
                  intraTopVal <= intraTopValTemp;
1468
                  if(!((currMbHor+1)0 && currMb-firstMb>zeroExtend(picWidth)))
1469
                     noMoreResp = 1;
1470
               end
1471
            else
1472
               intraTopVal <= update(intraTopVal, intraRespCount-1, data[31:0]);
1473
            intraTopValChroma0 <= update(intraTopValChroma0, temp2bit, data[47:32]);
1474
            intraTopValChroma1 <= update(intraTopValChroma1, temp2bit, data[63:48]);
1475
         end
1476
      else if(intraRespCount==5)
1477
         begin
1478
            if((currMbHor+1)
1479
               begin
1480
                  if(!(data[67:64]==15 || (data[67:64]==14 && ppsconstrained_intra_pred_flag==1)))
1481
                     intraTopVal <= update(intraTopVal, 4, data[31:0]);
1482
                  if(!(currMbHor>0 && currMb-firstMb>zeroExtend(picWidth)))
1483
                     noMoreResp = 1;
1484
               end
1485
            else
1486
               begin
1487
                  Bit#(40) temp2 = intraLeftVal[0];
1488
                  intraLeftVal <= update(intraLeftVal, 0, {temp2[39:8],data[31:24]});
1489
                  intraLeftValChroma0 <= update(intraLeftValChroma0, 0, data[47:40]);
1490
                  intraLeftValChroma1 <= update(intraLeftValChroma1, 0, data[63:56]);
1491
                  noMoreResp = 1;
1492
               end
1493
         end
1494
      else
1495
         begin
1496
            Bit#(40) temp2 = intraLeftVal[0];
1497
            intraLeftVal <= update(intraLeftVal, 0, {temp2[39:8],data[31:24]});
1498
            intraLeftValChroma0 <= update(intraLeftValChroma0, 0, data[47:40]);
1499
            intraLeftValChroma1 <= update(intraLeftValChroma1, 0, data[63:56]);
1500
            noMoreResp = 1;
1501
         end
1502
      intraMemRespQ.deq();
1503
      //$display( "TRACE Prediction: intraReceiveResp data %h",data);///////////////////////
1504
      if(noMoreResp == 0)
1505
         intraRespCount <= intraRespCount+1;
1506
      else
1507
         begin
1508
            intraRespCount <= 0;
1509
            intraStepCount <= 1;
1510
            blockNum <= 0;
1511
            pixelNum <= 0;
1512
            interOutBlockMvfifo.enq(tagged NotInter 1);
1513
         end
1514
      $display( "Trace Prediction: intraReceiveResp");
1515
   endrule
1516
 
1517
 
1518
   rule intraPredTypeStep ( intraStepCount==1 && !nextoutputfifo.notEmpty());
1519
      Bit#(2) blockHor = {blockNum[2],blockNum[0]};
1520
      Bit#(2) blockVer = {blockNum[3],blockNum[1]};
1521
      Bit#(4) topType = select(intra4x4typeTop, blockHor);
1522
      Bit#(4) leftType;
1523
      if(currMbHor!=0 || blockNum!=0)
1524
         leftType = select(intra4x4typeLeft, blockVer);
1525
      else
1526
         begin
1527
            leftType = 15;
1528
            intra4x4typeLeft <= replicate(15);
1529
         end
1530
      if(intrastate!=Intra4x4)
1531
         begin
1532
            intraStepCount <= intraStepCount+1;
1533
            nextoutputfifo.enq(NonSkipMB);
1534
         end
1535
      else
1536
         begin
1537
            Bit#(1) topAvailable;
1538
            Bit#(1) leftAvailable;
1539
            if(topType==15 || (topType==14 && ppsconstrained_intra_pred_flag==1))
1540
               topAvailable = 0;
1541
            else
1542
               topAvailable = 1;
1543
            if(leftType==15 || (leftType==14 && ppsconstrained_intra_pred_flag==1))
1544
               leftAvailable = 0;
1545
            else
1546
               leftAvailable = 1;
1547
            Bit#(4) predType = 0;
1548
            Bit#(4) remType = rem_intra4x4_pred_mode.first();
1549
            Bit#(4) curType = 0;
1550
            rem_intra4x4_pred_mode.deq();
1551
            if(topAvailable==0 || leftAvailable==0)
1552
               predType = 2;
1553
            else
1554
               begin
1555
                  Bit#(4) topType2 = topType;
1556
                  Bit#(4) leftType2 = leftType;
1557
                  if(topType>8)
1558
                     topType2 = 2;
1559
                  if(leftType>8)
1560
                     leftType2 = 2;
1561
                  if(topType2 > leftType2)
1562
                     predType = leftType2;
1563
                  else
1564
                     predType = topType2;
1565
               end
1566
            if(remType[3] == 1)
1567
               curType = predType;
1568
            else if(remType < predType)
1569
               curType = remType;
1570
            else
1571
               curType = remType+1;
1572
            cur_intra4x4_pred_mode <= curType;
1573
            intraStepCount <= intraStepCount+1;
1574
            if(blockNum == 15)
1575
               nextoutputfifo.enq(tagged Intra4x4PlusChroma);
1576
            else
1577
               nextoutputfifo.enq(tagged Intra4x4);
1578
            $display( "TRACE Prediction: intraPredTypeStep currMbHor currMbVer blockNum topType leftType predType remType curType %0d %0d %0d %0d %0d %0d %0d %0d",currMbHor,currMbVer,blockNum,topType,leftType,predType,remType,curType);//////////////////
1579
         end
1580
      //$display( "Trace Prediction: intraPredTypeStep");
1581
   endrule
1582
 
1583
 
1584
   rule intraProcessStep ( intraStepCount>1 );
1585
      $display( "TRACE Prediction: intraProcessStep %0d %0d", blockNum, pixelNum);////////////////////
1586
      //$display( "TRACE Prediction: intraProcessStep intraTopVal %h %h %h %h %h",intraTopVal[4],intraTopVal[3],intraTopVal[2],intraTopVal[1],intraTopVal[0]);/////////////////
1587
      Bit#(1) outFlag  = 0;
1588
      Bit#(4) nextIntraStepCount = intraStepCount+1;
1589
      Bit#(2) blockHor = {blockNum[2],blockNum[0]};
1590
      Bit#(2) blockVer = {blockNum[3],blockNum[1]};
1591
      Bit#(2) pixelVer = {pixelNum[3],pixelNum[2]};
1592
      Vector#(4,Bit#(8)) predVector = replicate(0);
1593
 
1594
      Bit#(4) topType = select(intra4x4typeTop, blockHor);
1595
      Bit#(4) leftType = select(intra4x4typeLeft, blockVer);
1596
      Bit#(1) topAvailable;
1597
      Bit#(1) leftAvailable;
1598
      if(topType==15 || (topType==14 && ppsconstrained_intra_pred_flag==1))
1599
         topAvailable = 0;
1600
      else
1601
         topAvailable = 1;
1602
      if(leftType==15 || (leftType==14 && ppsconstrained_intra_pred_flag==1))
1603
         leftAvailable = 0;
1604
      else
1605
         leftAvailable = 1;
1606
      if(blockNum==0 && pixelNum==0 && intraChromaFlag==0)
1607
         begin
1608
            intraChromaTopAvailable <= topAvailable;
1609
            intraChromaLeftAvailable <= leftAvailable;
1610
         end
1611
      if(intrastate==Intra4x4 && intraChromaFlag==0)
1612
         begin
1613
            if(intraStepCount==2)
1614
               begin
1615
                  outFlag = 1;
1616
                  Bit#(40) leftValSet = select(intraLeftVal,blockVer);
1617
                  Bit#(32) topMidValSet = select(intraTopVal,zeroExtend(blockHor));
1618
                  Bit#(32) topRightValSet = select(intraTopVal,{1'b0,blockHor}+1);
1619
                  Bit#(72) topValSet;
1620
                  if((blockNum[3:2]==3 && blockNum[0]==1) || blockNum[1:0]==3)
1621
                     topValSet = {topMidValSet[31:24],topMidValSet[31:24],topMidValSet[31:24],topMidValSet[31:24],topMidValSet,leftValSet[7:0]};
1622
                  else
1623
                     topValSet = {topRightValSet,topMidValSet,leftValSet[7:0]};
1624
                  $display( "TRACE Prediction: intraProcessStep intra4x4 %0d %0d %h %h", cur_intra4x4_pred_mode, blockNum, leftValSet, topValSet);////////////////////
1625
                  Bit#(4) topSelect1 = 0;
1626
                  Bit#(4) topSelect2 = 0;
1627
                  Bit#(4) topSelect3 = 0;
1628
                  Bit#(3) leftSelect1 = 0;
1629
                  Bit#(3) leftSelect2 = 0;
1630
                  Bit#(3) leftSelect3 = 0;
1631
                  Bit#(10) tempVal1 = 0;
1632
                  Bit#(10) tempVal2 = 0;
1633
                  Bit#(10) tempVal3 = 0;
1634
                  case(cur_intra4x4_pred_mode)
1635
                     0://vertical
1636
                     begin
1637
                        for(Integer pixelHor=0; pixelHor<4; pixelHor=pixelHor+1)
1638
                           begin
1639
                              topSelect1 = fromInteger(pixelHor);
1640
                              Bit#(8) topVal = intra4x4SelectTop(topValSet,topSelect1);
1641
                              predVector[pixelHor] = topVal;
1642
                           end
1643
                     end
1644
                     1://horizontal
1645
                     begin
1646
                        leftSelect1 = zeroExtend(pixelVer);
1647
                        Bit#(8) leftVal = intra4x4SelectLeft(leftValSet,leftSelect1);
1648
                        for(Integer pixelHor=0; pixelHor<4; pixelHor=pixelHor+1)
1649
                           predVector[pixelHor] = leftVal;
1650
                     end
1651
                     2://dc
1652
                     begin
1653
                        for(Integer pixelHor=0; pixelHor<4; pixelHor=pixelHor+1)
1654
                           begin
1655
                              Bit#(10) tempTopSum = zeroExtend(topValSet[15:8])+zeroExtend(topValSet[23:16])+zeroExtend(topValSet[31:24])+zeroExtend(topValSet[39:32]) + 2;
1656
                              Bit#(10) tempLeftSum = zeroExtend(leftValSet[15:8])+zeroExtend(leftValSet[23:16])+zeroExtend(leftValSet[31:24])+zeroExtend(leftValSet[39:32]) + 2;
1657
                              Bit#(11) tempTotalSum = zeroExtend(tempTopSum)+zeroExtend(tempLeftSum);
1658
                              Bit#(8) topSum = tempTopSum[9:2];
1659
                              Bit#(8) leftSum = tempLeftSum[9:2];
1660
                              Bit#(8) totalSum = tempTotalSum[10:3];
1661
                              if(topAvailable==1 && leftAvailable==1)
1662
                                 predVector[pixelHor] = totalSum;
1663
                              else if(topAvailable==1)
1664
                                 predVector[pixelHor] = topSum;
1665
                              else if(leftAvailable==1)
1666
                                 predVector[pixelHor] = leftSum;
1667
                              else
1668
                                 predVector[pixelHor] = 8'b10000000;
1669
                           end
1670
                     end
1671
                     3://diagonal down left
1672
                     begin
1673
                        for(Integer pixelHor=0; pixelHor<4; pixelHor=pixelHor+1)
1674
                           begin
1675
                              Bit#(4) selectNum = fromInteger(pixelHor)+zeroExtend(pixelVer);
1676
                              if(pixelHor==3 && pixelVer==3)
1677
                                 begin
1678
                                    topSelect1 = 6;
1679
                                    topSelect2 = 7;
1680
                                    topSelect3 = 7;
1681
                                 end
1682
                              else
1683
                                 begin
1684
                                    topSelect1 = selectNum;
1685
                                    topSelect2 = selectNum+1;
1686
                                    topSelect3 = selectNum+2;
1687
                                 end
1688
                              tempVal1 = zeroExtend(intra4x4SelectTop(topValSet,topSelect1));
1689
                              tempVal2 = zeroExtend(intra4x4SelectTop(topValSet,topSelect2));
1690
                              tempVal3 = zeroExtend(intra4x4SelectTop(topValSet,topSelect3));
1691
                              Bit#(10) predVal = tempVal1 + (tempVal2<<1) + tempVal3 + 2;
1692
                              predVector[pixelHor] = predVal[9:2];
1693
                           end
1694
                     end
1695
                     4://diagonal down right
1696
                     begin
1697
                        for(Integer pixelHor=0; pixelHor<4; pixelHor=pixelHor+1)
1698
                           begin
1699
                              if(fromInteger(pixelHor) > pixelVer)
1700
                                 begin
1701
                                    topSelect3 = fromInteger(pixelHor)-zeroExtend(pixelVer);
1702
                                    topSelect2 = topSelect3-1;
1703
                                    topSelect1 = topSelect3-2;
1704
                                    tempVal1 = zeroExtend(intra4x4SelectTop(topValSet,topSelect1));
1705
                                    tempVal2 = zeroExtend(intra4x4SelectTop(topValSet,topSelect2));
1706
                                    tempVal3 = zeroExtend(intra4x4SelectTop(topValSet,topSelect3));
1707
                                 end
1708
                              else if(fromInteger(pixelHor) < pixelVer)
1709
                                 begin
1710
                                    leftSelect3 = zeroExtend(pixelVer)-fromInteger(pixelHor);
1711
                                    leftSelect2 = leftSelect3-1;
1712
                                    leftSelect1 = leftSelect3-2;
1713
                                    tempVal1 = zeroExtend(intra4x4SelectLeft(leftValSet,leftSelect1));
1714
                                    tempVal2 = zeroExtend(intra4x4SelectLeft(leftValSet,leftSelect2));
1715
                                    tempVal3 = zeroExtend(intra4x4SelectLeft(leftValSet,leftSelect3));
1716
                                 end
1717
                              else
1718
                                 begin
1719
                                    leftSelect1 = 0;
1720
                                    leftSelect2 = -1;
1721
                                    topSelect1 = 0;
1722
                                    tempVal1 = zeroExtend(intra4x4SelectLeft(leftValSet,leftSelect1));
1723
                                    tempVal2 = zeroExtend(intra4x4SelectLeft(leftValSet,leftSelect2));
1724
                                    tempVal3 = zeroExtend(intra4x4SelectTop(topValSet,topSelect1));
1725
                                 end
1726
                              Bit#(10) predVal = tempVal1 + (tempVal2<<1) + tempVal3 + 2;
1727
                              predVector[pixelHor] = predVal[9:2];
1728
                           end
1729
                     end
1730
                     5://vertical right
1731
                     begin
1732
                        for(Integer pixelHor=0; pixelHor<4; pixelHor=pixelHor+1)
1733
                           begin
1734
                              Bit#(4) tempPixelHor = fromInteger(pixelHor);
1735
                              Bit#(4) zVR = (tempPixelHor<<1)-zeroExtend(pixelVer);
1736
                              if(zVR<=6 && zVR>=0)
1737
                                 begin
1738
                                    topSelect3 = fromInteger(pixelHor)-zeroExtend(pixelVer>>1);
1739
                                    topSelect2 = topSelect3-1;
1740
                                    if(zVR==1 || zVR==3 || zVR==5)
1741
                                       topSelect1 = topSelect3-2;
1742
                                    else
1743
                                       topSelect1 = topSelect3;
1744
                                    tempVal1 = zeroExtend(intra4x4SelectTop(topValSet,topSelect1));
1745
                                    tempVal2 = zeroExtend(intra4x4SelectTop(topValSet,topSelect2));
1746
                                    tempVal3 = zeroExtend(intra4x4SelectTop(topValSet,topSelect3));
1747
                                 end
1748
                              else if(zVR==-1)
1749
                                 begin
1750
                                    leftSelect1 = 0;
1751
                                    leftSelect2 = -1;
1752
                                    topSelect1 = 0;
1753
                                    tempVal1 = zeroExtend(intra4x4SelectLeft(leftValSet,leftSelect1));
1754
                                    tempVal2 = zeroExtend(intra4x4SelectLeft(leftValSet,leftSelect2));
1755
                                    tempVal3 = zeroExtend(intra4x4SelectTop(topValSet,topSelect1));
1756
                                 end
1757
                              else
1758
                                 begin
1759
                                    leftSelect1 = zeroExtend(pixelVer)-1;
1760
                                    leftSelect2 = leftSelect1-1;
1761
                                    leftSelect3 = leftSelect1-2;
1762
                                    tempVal1 = zeroExtend(intra4x4SelectLeft(leftValSet,leftSelect1));
1763
                                    tempVal2 = zeroExtend(intra4x4SelectLeft(leftValSet,leftSelect2));
1764
                                    tempVal3 = zeroExtend(intra4x4SelectLeft(leftValSet,leftSelect3));
1765
                                 end
1766
                              Bit#(10) predVal = tempVal1 + (tempVal2<<1) + tempVal3 + 2;
1767
                              predVector[pixelHor] = predVal[9:2];
1768
                           end
1769
                     end
1770
                     6://horizontal down
1771
                     begin
1772
                        for(Integer pixelHor=0; pixelHor<4; pixelHor=pixelHor+1)
1773
                           begin
1774
                              Bit#(4) tempPixelVer = zeroExtend(pixelVer);
1775
                              Bit#(4) zHD = (tempPixelVer<<1)-fromInteger(pixelHor);
1776
                              if(zHD<=6 && zHD>=0)
1777
                                 begin
1778
                                    leftSelect3 = zeroExtend(pixelVer)-fromInteger(pixelHor/2);
1779
                                    leftSelect2 = leftSelect3-1;
1780
                                    if(zHD==1 || zHD==3 || zHD==5)
1781
                                       leftSelect1 = leftSelect3-2;
1782
                                    else
1783
                                       leftSelect1 = leftSelect3;
1784
                                    tempVal1 = zeroExtend(intra4x4SelectLeft(leftValSet,leftSelect1));
1785
                                    tempVal2 = zeroExtend(intra4x4SelectLeft(leftValSet,leftSelect2));
1786
                                    tempVal3 = zeroExtend(intra4x4SelectLeft(leftValSet,leftSelect3));
1787
                                 end
1788
                              else if(zHD==-1)
1789
                                 begin
1790
                                    leftSelect1 = 0;
1791
                                    leftSelect2 = -1;
1792
                                    topSelect1 = 0;
1793
                                    tempVal1 = zeroExtend(intra4x4SelectLeft(leftValSet,leftSelect1));
1794
                                    tempVal2 = zeroExtend(intra4x4SelectLeft(leftValSet,leftSelect2));
1795
                                    tempVal3 = zeroExtend(intra4x4SelectTop(topValSet,topSelect1));
1796
                                 end
1797
                              else
1798
                                 begin
1799
                                    topSelect1 = fromInteger(pixelHor)-1;
1800
                                    topSelect2 = topSelect1-1;
1801
                                    topSelect3 = topSelect1-2;
1802
                                    tempVal1 = zeroExtend(intra4x4SelectTop(topValSet,topSelect1));
1803
                                    tempVal2 = zeroExtend(intra4x4SelectTop(topValSet,topSelect2));
1804
                                    tempVal3 = zeroExtend(intra4x4SelectTop(topValSet,topSelect3));
1805
                                 end
1806
                              Bit#(10) predVal = tempVal1 + (tempVal2<<1) + tempVal3 + 2;
1807
                              predVector[pixelHor] = predVal[9:2];
1808
                           end
1809
                     end
1810
                     7://vertical left
1811
                     begin
1812
                        for(Integer pixelHor=0; pixelHor<4; pixelHor=pixelHor+1)
1813
                           begin
1814
                              topSelect1 = fromInteger(pixelHor)+zeroExtend(pixelVer>>1);
1815
                              topSelect2 = topSelect1+1;
1816
                              if(pixelVer==1 || pixelVer==3)
1817
                                 topSelect3 = topSelect1+2;
1818
                              else
1819
                                 topSelect3 = topSelect1;
1820
                              tempVal1 = zeroExtend(intra4x4SelectTop(topValSet,topSelect1));
1821
                              tempVal2 = zeroExtend(intra4x4SelectTop(topValSet,topSelect2));
1822
                              tempVal3 = zeroExtend(intra4x4SelectTop(topValSet,topSelect3));
1823
                              Bit#(10) predVal = tempVal1 + (tempVal2<<1) + tempVal3 + 2;
1824
                              predVector[pixelHor] = predVal[9:2];
1825
                           end
1826
                     end
1827
                     8://horizontal up
1828
                     begin
1829
                        for(Integer pixelHor=0; pixelHor<4; pixelHor=pixelHor+1)
1830
                           begin
1831
                              Bit#(4) tempPixelVer = zeroExtend(pixelVer);
1832
                              Bit#(4) zHU = (tempPixelVer<<1)+fromInteger(pixelHor);
1833
                              if(zHU<=4)
1834
                                 begin
1835
                                    leftSelect1 = zeroExtend(pixelVer)+fromInteger(pixelHor/2);
1836
                                    leftSelect2 = leftSelect1+1;
1837
                                    if(zHU==1 || zHU==3)
1838
                                       leftSelect3 = leftSelect1+2;
1839
                                    else
1840
                                       leftSelect3 = leftSelect1;
1841
                                 end
1842
                              else
1843
                                 begin
1844
                                    if(zHU==5)
1845
                                       leftSelect1 = 2;
1846
                                    else
1847
                                       leftSelect1 = 3;
1848
                                    leftSelect2 = 3;
1849
                                    leftSelect3 = 3;
1850
                                 end
1851
                              tempVal1 = zeroExtend(intra4x4SelectLeft(leftValSet,leftSelect1));
1852
                              tempVal2 = zeroExtend(intra4x4SelectLeft(leftValSet,leftSelect2));
1853
                              tempVal3 = zeroExtend(intra4x4SelectLeft(leftValSet,leftSelect3));
1854
                              Bit#(10) predVal = tempVal1 + (tempVal2<<1) + tempVal3 + 2;
1855
                              predVector[pixelHor] = predVal[9:2];
1856
                           end
1857
                     end
1858
                     default: $display( "ERROR Prediction: intraProcessStep intra4x4 unknown cur_intra4x4_pred_mode");
1859
                  endcase
1860
               end
1861
            else
1862
               $display( "ERROR Prediction: intraProcessStep intra4x4 unknown intraStepCount");
1863
         end
1864
      else if(intrastate==Intra16x16  && intraChromaFlag==0)
1865
         begin
1866
            //$display( "TRACE Prediction: intraProcessStep intra16x16 %0d %0d %0d %h", intra16x16_pred_mode, currMb, blockNum, select(intraTopVal,blockHor));/////////////////
1867
            case(intra16x16_pred_mode)
1868
               0://vertical
1869
               begin
1870
                  for(Integer pixelHor=0; pixelHor<4; pixelHor=pixelHor+1)
1871
                     begin
1872
                        Bit#(32) topValSet = select(intraTopVal,blockHor);
1873
                        Bit#(8) topVal = select32to8(topValSet,fromInteger(pixelHor));
1874
                        predVector[pixelHor] = topVal;
1875
                     end
1876
                  outFlag = 1;
1877
               end
1878
               1://horizontal
1879
               begin
1880
                  Bit#(40) leftValSet = select(intraLeftVal,blockVer);
1881
                  Bit#(8) leftVal = intra4x4SelectLeft(leftValSet,zeroExtend(pixelVer));
1882
                  for(Integer pixelHor=0; pixelHor<4; pixelHor=pixelHor+1)
1883
                     predVector[pixelHor] = leftVal;
1884
                  outFlag = 1;
1885
               end
1886
               2://dc
1887
               begin
1888
                  case(intraStepCount)
1889
                     2:
1890
                     begin
1891
                        if(topAvailable == 1)
1892
                           begin
1893
                              Bit#(32) topValSet = select(intraTopVal,0);
1894
                              intraSumA <= zeroExtend(topValSet[7:0])+zeroExtend(topValSet[15:8])+zeroExtend(topValSet[23:16])+zeroExtend(topValSet[31:24]);
1895
                           end
1896
                        else
1897
                           begin
1898
                              intraSumA <= 0;
1899
                              nextIntraStepCount = 6;
1900
                           end
1901
                     end
1902
                     3:
1903
                     begin
1904
                        Bit#(32) topValSet = select(intraTopVal,1);
1905
                        intraSumA <= intraSumA+zeroExtend(topValSet[7:0])+zeroExtend(topValSet[15:8])+zeroExtend(topValSet[23:16])+zeroExtend(topValSet[31:24]);
1906
                     end
1907
                     4:
1908
                     begin
1909
                        Bit#(32) topValSet = select(intraTopVal,2);
1910
                        intraSumA <= intraSumA+zeroExtend(topValSet[7:0])+zeroExtend(topValSet[15:8])+zeroExtend(topValSet[23:16])+zeroExtend(topValSet[31:24]);
1911
                     end
1912
                     5:
1913
                     begin
1914
                        Bit#(32) topValSet = select(intraTopVal,3);
1915
                        intraSumA <= intraSumA+zeroExtend(topValSet[7:0])+zeroExtend(topValSet[15:8])+zeroExtend(topValSet[23:16])+zeroExtend(topValSet[31:24])+8;
1916
                     end
1917
                     6:
1918
                     begin
1919
                        if(leftAvailable == 1)
1920
                           begin
1921
                              Bit#(40) leftValSet = select(intraLeftVal,0);
1922
                              intraSumA <= intraSumA+zeroExtend(leftValSet[15:8])+zeroExtend(leftValSet[23:16])+zeroExtend(leftValSet[31:24])+zeroExtend(leftValSet[39:32]);
1923
                           end
1924
                        else
1925
                           nextIntraStepCount = 10;
1926
                     end
1927
                     7:
1928
                     begin
1929
                        Bit#(40) leftValSet = select(intraLeftVal,1);
1930
                        intraSumA <= intraSumA+zeroExtend(leftValSet[15:8])+zeroExtend(leftValSet[23:16])+zeroExtend(leftValSet[31:24])+zeroExtend(leftValSet[39:32]);
1931
                     end
1932
                     8:
1933
                     begin
1934
                        Bit#(40) leftValSet = select(intraLeftVal,2);
1935
                        intraSumA <= intraSumA+zeroExtend(leftValSet[15:8])+zeroExtend(leftValSet[23:16])+zeroExtend(leftValSet[31:24])+zeroExtend(leftValSet[39:32]);
1936
                     end
1937
                     9:
1938
                     begin
1939
                        Bit#(40) leftValSet = select(intraLeftVal,3);
1940
                        intraSumA <= intraSumA+zeroExtend(leftValSet[15:8])+zeroExtend(leftValSet[23:16])+zeroExtend(leftValSet[31:24])+zeroExtend(leftValSet[39:32])+8;
1941
                     end
1942
                     10:
1943
                     begin
1944
                        if(leftAvailable == 1 && topAvailable == 1)
1945
                           intraSumA <= intraSumA >> 5;
1946
                        else if(leftAvailable == 1 || topAvailable == 1)
1947
                           intraSumA <= intraSumA >> 4;
1948
                        else
1949
                           intraSumA <= 128;
1950
                     end
1951
                     11:
1952
                     begin
1953
                        for(Integer pixelHor=0; pixelHor<4; pixelHor=pixelHor+1)
1954
                           predVector[pixelHor] = intraSumA[7:0];
1955
                        outFlag = 1;
1956
                     end
1957
                     default: $display( "ERROR Prediction: intraProcessStep intra16x16 DC unknown intraStepCount");
1958
                  endcase
1959
               end
1960
               3://plane
1961
               begin
1962
                  if(intraStepCount == 2)
1963
                     begin
1964
                        Bit#(32) topValSet = select(intraTopVal,3);
1965
                        Bit#(8) topVal = select32to8(topValSet,3);
1966
                        Bit#(40) leftValSet = select(intraLeftVal,3);
1967
                        Bit#(8) leftVal = intra4x4SelectLeft(leftValSet,3);
1968
                        Bit#(13) tempVal = zeroExtend(topVal) + zeroExtend(leftVal);
1969
                        intraSumA <= tempVal << 4;
1970
                        intraSumB <= 0;
1971
                        intraSumC <= 0;
1972
                     end
1973
                  else if(intraStepCount < 11)
1974
                     begin
1975
                        Bit#(4) xyPlusOne = intraStepCount-2;
1976
                        Bit#(4) xyPlusEight = intraStepCount+5;
1977
                        Bit#(4) sixMinusXY = 9-intraStepCount;
1978
                        Bit#(32) topValSet1 = select(intraTopVal,xyPlusEight[3:2]);
1979
                        Bit#(8) topVal1 = select32to8(topValSet1,xyPlusEight[1:0]);
1980
                        Bit#(40) leftValSet1 = select(intraLeftVal,xyPlusEight[3:2]);
1981
                        Bit#(8) leftVal1 = intra4x4SelectLeft(leftValSet1,zeroExtend(xyPlusEight[1:0]));
1982
                        Bit#(32) topValSet2=0;
1983
                        Bit#(8) topVal2;
1984
                        Bit#(40) leftValSet2;
1985
                        Bit#(8) leftVal2;
1986
                        if(intraStepCount==10)
1987
                           begin
1988
                              leftValSet2 = select(intraLeftVal,0);
1989
                              leftVal2 = intra4x4SelectLeft(leftValSet2,-1);
1990
                              topVal2 = leftVal2;
1991
                           end
1992
                        else
1993
                           begin
1994
                              topValSet2 = select(intraTopVal,sixMinusXY[3:2]);
1995
                              topVal2 = select32to8(topValSet2,sixMinusXY[1:0]);
1996
                              leftValSet2 = select(intraLeftVal,sixMinusXY[3:2]);
1997
                              leftVal2 = intra4x4SelectLeft(leftValSet2,zeroExtend(sixMinusXY[1:0]));
1998
                           end
1999
                        Bit#(15) diffH = zeroExtend(topVal1) - zeroExtend(topVal2);
2000
                        Bit#(15) diffV = zeroExtend(leftVal1) - zeroExtend(leftVal2);
2001
                        intraSumB <= intraSumB + (zeroExtend(xyPlusOne) * diffH);
2002
                        intraSumC <= intraSumC + (zeroExtend(xyPlusOne) * diffV);
2003
                     end
2004
                  else if(intraStepCount == 11)
2005
                     begin
2006
                        Bit#(18) tempSumB = (5*signExtend(intraSumB)) + 32;
2007
                        Bit#(18) tempSumC = (5*signExtend(intraSumC)) + 32;
2008
                        intraSumB <= signExtend(tempSumB[17:6]);
2009
                        intraSumC <= signExtend(tempSumC[17:6]);
2010
                     end
2011
                  else if(intraStepCount == 12)
2012
                     begin
2013
                        for(Integer pixelHor=0; pixelHor<4; pixelHor=pixelHor+1)
2014
                           begin
2015
                              Bit#(5)  positionHor = {1'b0,blockHor,fromInteger(pixelHor)};
2016
                              Bit#(5)  positionVer = {1'b0,blockVer,pixelVer};
2017
                              Bit#(16) tempProductB = signExtend(intraSumB) * signExtend(positionHor-7);
2018
                              Bit#(16) tempProductC = signExtend(intraSumC) * signExtend(positionVer-7);
2019
                              Bit#(16) tempTotal = tempProductB + tempProductC + zeroExtend(intraSumA) + 16;
2020
                              if(tempTotal[15]==1)
2021
                                 predVector[pixelHor] = 0;
2022
                              else if(tempTotal[14:5] > 255)
2023
                                 predVector[pixelHor] = 255;
2024
                              else
2025
                                 predVector[pixelHor] = tempTotal[12:5];
2026
                           end
2027
                        outFlag = 1;
2028
                     end
2029
                  else
2030
                     $display( "ERROR Prediction: intraProcessStep intra16x16 plane unknown intraStepCount");
2031
               end
2032
            endcase
2033
         end
2034
      else if(intraChromaFlag==1)
2035
         begin
2036
            //$display( "TRACE Prediction: intraProcessStep intraChroma %0d %0d %0d %0d %0d %0d %h %h %h %h %h %h %h %h",intra_chroma_pred_mode.first(),intraChromaTopAvailable,intraChromaLeftAvailable,currMb,blockNum,pixelNum,pack(intraLeftValChroma0),pack(intraTopValChroma0),pack(intraLeftValChroma1),pack(intraTopValChroma1),intraLeftValChroma0[0],intraTopValChroma0[3][15:8],intraLeftValChroma1[0],intraTopValChroma1[3][15:8]);///////////////////
2037
            Vector#(9,Bit#(8)) tempLeftVec;
2038
            Vector#(4,Bit#(16)) tempTopVec;
2039
            if(blockNum[2] == 0)
2040
               begin
2041
                  tempLeftVec = intraLeftValChroma0;
2042
                  tempTopVec = intraTopValChroma0;
2043
               end
2044
            else
2045
               begin
2046
                  tempLeftVec = intraLeftValChroma1;
2047
                  tempTopVec = intraTopValChroma1;
2048
               end
2049
            case(intra_chroma_pred_mode.first())
2050
               0://dc
2051
               begin
2052
                  if(intraStepCount == 2)
2053
                     begin
2054
                        Bit#(1) useTop=0;
2055
                        Bit#(1) useLeft=0;
2056
                        if(blockNum[1:0] == 0 || blockNum[1:0] == 3)
2057
                           begin
2058
                              useTop = intraChromaTopAvailable;
2059
                              useLeft = intraChromaLeftAvailable;
2060
                           end
2061
                        else if(blockNum[1:0] == 1)
2062
                           begin
2063
                              if(intraChromaTopAvailable == 1)
2064
                                 useTop = 1;
2065
                              else if(intraChromaLeftAvailable == 1)
2066
                                 useLeft = 1;
2067
                           end
2068
                              else if(blockNum[1:0] == 2)
2069
                                 begin
2070
                                    if(intraChromaLeftAvailable == 1)
2071
                                       useLeft = 1;
2072
                                    else if(intraChromaTopAvailable == 1)
2073
                                       useTop = 1;
2074
                                 end
2075
                        else
2076
                           $display( "ERROR Prediction: intraProcessStep intraChroma dc unknown blockNum");
2077
                        Bit#(10) topSum;
2078
                        Bit#(10) leftSum;
2079
                        Bit#(11) totalSum;
2080
                        if(blockHor[0] == 0)
2081
                           topSum = zeroExtend(tempTopVec[0][15:8])+zeroExtend(tempTopVec[0][7:0])+zeroExtend(tempTopVec[1][15:8])+zeroExtend(tempTopVec[1][7:0])+2;
2082
                        else
2083
                           topSum = zeroExtend(tempTopVec[2][15:8])+zeroExtend(tempTopVec[2][7:0])+zeroExtend(tempTopVec[3][15:8])+zeroExtend(tempTopVec[3][7:0])+2;
2084
                        if(blockVer[0] == 0)
2085
                           leftSum = zeroExtend(tempLeftVec[1])+zeroExtend(tempLeftVec[2])+zeroExtend(tempLeftVec[3])+zeroExtend(tempLeftVec[4])+2;
2086
                        else
2087
                           leftSum = zeroExtend(tempLeftVec[5])+zeroExtend(tempLeftVec[6])+zeroExtend(tempLeftVec[7])+zeroExtend(tempLeftVec[8])+2;
2088
                        totalSum = zeroExtend(topSum) + zeroExtend(leftSum);
2089
                        if(useTop==1 && useLeft==1)
2090
                           intraSumA <= zeroExtend(totalSum[10:3]);
2091
                        else if(useTop==1)
2092
                           intraSumA <= zeroExtend(topSum[9:2]);
2093
                        else if(useLeft==1)
2094
                           intraSumA <= zeroExtend(leftSum[9:2]);
2095
                        else
2096
                           intraSumA <= zeroExtend(8'b10000000);
2097
                     end
2098
                  else if(intraStepCount == 3)
2099
                     begin
2100
                        for(Integer pixelHor=0; pixelHor<4; pixelHor=pixelHor+1)
2101
                           predVector[pixelHor] = intraSumA[7:0];
2102
                        outFlag = 1;
2103
                     end
2104
                  else
2105
                     $display( "ERROR Prediction: intraProcessStep intraChroma dc unknown intraStepCount");
2106
               end
2107
               1://horizontal
2108
               begin
2109
                  for(Integer pixelHor=0; pixelHor<4; pixelHor=pixelHor+1)
2110
                     begin
2111
                        Bit#(4) tempLeftIdx = {1'b0,blockVer[0],pixelVer} + 1;
2112
                        predVector[pixelHor] = select(tempLeftVec,tempLeftIdx);
2113
                     end
2114
                  outFlag = 1;
2115
               end
2116
               2://vertical
2117
               begin
2118
                  for(Integer pixelHor=0; pixelHor<4; pixelHor=pixelHor+1)
2119
                     begin
2120
                        Bit#(2) pixelHorTemp = fromInteger(pixelHor);
2121
                        Bit#(16) tempTopVal = select(tempTopVec,{blockHor[0],pixelHorTemp[1]});
2122
                        if(pixelHorTemp[0] == 0)
2123
                           predVector[pixelHor] = tempTopVal[7:0];
2124
                        else
2125
                           predVector[pixelHor] = tempTopVal[15:8];
2126
                     end
2127
                  outFlag = 1;
2128
               end
2129
               3://plane
2130
               begin
2131
                  if(intraStepCount == 2)
2132
                     begin
2133
                        Bit#(16) topValSet = tempTopVec[3];
2134
                        Bit#(8) topVal = topValSet[15:8];
2135
                        Bit#(8) leftVal = tempLeftVec[8];
2136
                        Bit#(13) tempVal = zeroExtend(topVal) + zeroExtend(leftVal);
2137
                        intraSumA <= tempVal << 4;
2138
                        intraSumB <= 0;
2139
                        intraSumC <= 0;
2140
                     end
2141
                  else if(intraStepCount < 7)
2142
                     begin
2143
                        Bit#(3) xyPlusOne = truncate(intraStepCount)-2;
2144
                        Bit#(3) xyPlusFour = truncate(intraStepCount)+1;
2145
                        Bit#(4) twoMinusXY = 5-intraStepCount;
2146
                        Bit#(16) topValSet1 = select(tempTopVec,xyPlusFour[2:1]);
2147
                        Bit#(8) topVal1 = select16to8(topValSet1,xyPlusFour[0]);
2148
                        Bit#(4) tempLeftIdx1 = {1'b0,xyPlusFour} + 1;
2149
                        Bit#(8) leftVal1 = select(tempLeftVec,tempLeftIdx1);
2150
 
2151
                        Bit#(16) topValSet2 = select(tempTopVec,twoMinusXY[2:1]);
2152
                        Bit#(8) topVal2;
2153
                        Bit#(8) leftVal2 = select(tempLeftVec,twoMinusXY+1);
2154
                        if(intraStepCount==6)
2155
                           topVal2 = leftVal2;
2156
                        else
2157
                           topVal2 = select16to8(topValSet2,twoMinusXY[0]);
2158
                        Bit#(15) diffH = zeroExtend(topVal1) - zeroExtend(topVal2);
2159
                        Bit#(15) diffV = zeroExtend(leftVal1) - zeroExtend(leftVal2);
2160
                        intraSumB <= intraSumB + (zeroExtend(xyPlusOne) * diffH);
2161
                        intraSumC <= intraSumC + (zeroExtend(xyPlusOne) * diffV);
2162
                        Int#(15) tempDisplayH = unpack(zeroExtend(xyPlusOne) * diffH);
2163
                        Int#(15) tempDisplayV = unpack(zeroExtend(xyPlusOne) * diffV);
2164
                        //$display( "TRACE Prediction: intraProcessStep intraChroma plane partH partV %0d %0d",tempDisplayH,tempDisplayV);////////////////////
2165
                     end
2166
                  else if(intraStepCount == 7)
2167
                     begin
2168
                        Int#(15) tempDisplayH = unpack(intraSumB);
2169
                        Int#(15) tempDisplayV = unpack(intraSumC);
2170
                        //$display( "TRACE Prediction: intraProcessStep intraChroma plane H V %0d %0d",tempDisplayH,tempDisplayV);////////////////////
2171
                        Bit#(19) tempSumB = (34*signExtend(intraSumB)) + 32;
2172
                        Bit#(19) tempSumC = (34*signExtend(intraSumC)) + 32;
2173
                        intraSumB <= signExtend(tempSumB[18:6]);
2174
                        intraSumC <= signExtend(tempSumC[18:6]);
2175
                     end
2176
                  else if(intraStepCount == 8)
2177
                     begin
2178
                        for(Integer pixelHor=0; pixelHor<4; pixelHor=pixelHor+1)
2179
                           begin
2180
                              Bit#(4)  positionHor = {1'b0,blockHor[0],fromInteger(pixelHor)};
2181
                              Bit#(4)  positionVer = {1'b0,blockVer[0],pixelVer};
2182
                              Bit#(17) tempProductB = signExtend(intraSumB) * signExtend(positionHor-3);
2183
                              Bit#(17) tempProductC = signExtend(intraSumC) * signExtend(positionVer-3);
2184
                              Bit#(17) tempTotal = tempProductB + tempProductC + zeroExtend(intraSumA) + 16;
2185
                              if(tempTotal[16]==1)
2186
                                 predVector[pixelHor] = 0;
2187
                              else if(tempTotal[15:5] > 255)
2188
                                 predVector[pixelHor] = 255;
2189
                              else
2190
                                 predVector[pixelHor] = tempTotal[12:5];
2191
                           end
2192
                        outFlag = 1;
2193
                     end
2194
                  else
2195
                     $display( "ERROR Prediction: intraProcessStep intraChroma plane unknown intraStepCount");
2196
               end
2197
            endcase
2198
         end
2199
      else
2200
         $display( "ERROR Prediction: intraProcessStep unknown intrastate");
2201
 
2202
      if(outFlag==1)
2203
         begin
2204
            predictedfifo.enq(predVector);
2205
            pixelNum <= pixelNum+4;
2206
            if(pixelNum == 12)
2207
               begin
2208
                  if(intraChromaFlag==0)
2209
                     begin
2210
                        blockNum <= blockNum+1;
2211
                        if(blockNum == 15)
2212
                           begin
2213
                              intraChromaFlag <= 1;
2214
                              intraStepCount <= 2;
2215
                           end
2216
                        else if(intrastate==Intra4x4)
2217
                           intraStepCount <= 1;
2218
                     end
2219
                  else
2220
                     begin
2221
                        if(blockNum == 7)
2222
                           begin
2223
                              blockNum <= 0;
2224
                              intraChromaFlag <= 0;
2225
                              intraStepCount <= 0;
2226
                              intra_chroma_pred_mode.deq();
2227
                           end
2228
                        else
2229
                           begin
2230
                              blockNum <= blockNum+1;
2231
                              if(intra_chroma_pred_mode.first()==0)
2232
                                 intraStepCount <= 2;
2233
                              else if(blockNum==3)
2234
                                 intraStepCount <= 2;
2235
                           end
2236
                     end
2237
               end
2238
         end
2239
      else
2240
         intraStepCount <= nextIntraStepCount;
2241
      //$display( "Trace Prediction: intraProcessStep");
2242
   endrule
2243
 
2244
 
2245
 
2246
   interface Client mem_client_intra;
2247
      interface Get request  = fifoToGet(intraMemReqQ);
2248
      interface Put response = fifoToPut(intraMemRespQ);
2249
   endinterface
2250
   interface Client mem_client_inter;
2251
      interface Get request  = fifoToGet(interMemReqQ);
2252
      interface Put response = fifoToPut(interMemRespQ);
2253
   endinterface
2254
   interface Client mem_client_buffer = interpolator.mem_client;
2255
 
2256
   interface Put ioin  = fifoToPut(infifo);
2257
   interface Put ioin_InverseTrans  = fifoToPut(infifo_ITB);
2258
   interface Get ioout = fifoToGet(outfifo);
2259
 
2260
 
2261
endmodule
2262
 
2263
endpackage

powered by: WebSVN 2.1.0

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