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

Subversion Repositories bluespec-h264

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

Details | Compare with Previous | View Log

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

powered by: WebSVN 2.1.0

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