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

Subversion Repositories bluespec-h264

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

powered by: WebSVN 2.1.0

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