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

Subversion Repositories bluespec-h264

[/] [bluespec-h264/] [trunk/] [src_fpga/] [mkPrediction.bsv] - Blame information for rev 13

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

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

powered by: WebSVN 2.1.0

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