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

Subversion Repositories bluespec-h264

[/] [bluespec-h264/] [trunk/] [release/] [mkPrediction_intra32.bsv] - Blame information for rev 100

Details | Compare with Previous | View Log

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

powered by: WebSVN 2.1.0

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