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

Subversion Repositories bluespec-h264

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

powered by: WebSVN 2.1.0

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