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

Subversion Repositories bluespec-h264

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

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

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

powered by: WebSVN 2.1.0

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