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

Subversion Repositories bluespec-h264

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

powered by: WebSVN 2.1.0

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