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

Subversion Repositories bluespec-h264

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

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

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

powered by: WebSVN 2.1.0

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