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

Subversion Repositories bluespec-h264

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

powered by: WebSVN 2.1.0

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