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

Subversion Repositories bluespec-h264

[/] [bluespec-h264/] [trunk/] [LumaChromaParallel/] [mkPrediction.bsv] - Blame information for rev 86

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

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

powered by: WebSVN 2.1.0

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