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

Subversion Repositories bluespec-h264

[/] [bluespec-h264/] [trunk/] [src/] [mkPrediction_intra32.bsv] - Blame information for rev 15

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

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

powered by: WebSVN 2.1.0

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