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

Subversion Repositories bluespec-h264

[/] [bluespec-h264/] [trunk/] [src/] [mkPrediction.bsv] - Blame information for rev 9

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

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

powered by: WebSVN 2.1.0

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