OpenCores
URL https://opencores.org/ocsvn/mpeg2fpga/mpeg2fpga/trunk

Subversion Repositories mpeg2fpga

[/] [mpeg2fpga/] [trunk/] [rtl/] [mpeg2/] [vld.v] - Blame information for rev 2

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 2 kdv
/*
2
 * vld.v
3
 *
4
 * Copyright (c) 2007 Koen De Vleeschauwer.
5
 *
6
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
7
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
8
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
9
 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
10
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
11
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
12
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
13
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
14
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
15
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
16
 * SUCH DAMAGE.
17
 */
18
 
19
/*
20
 * vld - Variable Length Decoder. Scalability not implemented. Maximum vertical size of image in pixels: 2800.
21
 */
22
 
23
`include "timescale.v"
24
 
25
`undef DEBUG
26
//`define DEBUG 1
27
//`define DEBUG_VLC 1
28
 
29
/*
30
 * video decoding process as specified in 13818-2, par. 7.1 through 7.6
31
 */
32
 
33
module vld(clk, clk_en, rst,
34
  getbits, signbit, advance, align, wait_state,                                             // interface with getbits
35
  quant_wr_data, quant_wr_addr, quant_rst,                                                  // interface with quantizer rams via rld_fifo
36
  wr_intra_quant, wr_non_intra_quant, wr_chroma_intra_quant, wr_chroma_non_intra_quant,     // interface with quantizer rams via rld_fifo
37
  rld_wr_en, rld_cmd, dct_coeff_run, dct_coeff_signed_level, dct_coeff_end,                 // interface with rld_fifo
38
  quantiser_scale_code, alternate_scan, q_scale_type, macroblock_intra, intra_dc_precision, // interface with rld_fifo
39
  picture_coding_type, picture_structure, motion_type, dct_type,                            // interface with motcomp
40
  motion_vert_field_select_0_0, motion_vert_field_select_0_1,                               // interface with motcomp
41
  motion_vert_field_select_1_0, motion_vert_field_select_1_1,                               // interface with motcomp
42
  second_field, update_picture_buffers, last_frame, mb_width, mb_height, chroma_format,     // interface with motcomp
43
  macroblock_address, macroblock_motion_forward, macroblock_motion_backward,                // interface with motcomp
44
  motion_vector_valid,                                                                      // interface with motcomp - motion vectors
45
  pmv_0_0_0, pmv_0_0_1, pmv_1_0_0, pmv_1_0_1, pmv_0_1_0, pmv_0_1_1, pmv_1_1_0, pmv_1_1_1,   // interface with motcomp - motion vectors
46
  dmv_0_0, dmv_0_1, dmv_1_0, dmv_1_1,                                                       // interface with motcomp - dual prime motion vectors
47
  horizontal_size, vertical_size, display_horizontal_size, display_vertical_size,           // interface with syncgen
48
  matrix_coefficients,                                                                      // interface with yuv2rgb
49
  frame_rate_code, frame_rate_extension_n, frame_rate_extension_d,                          // interface with regfile
50
  aspect_ratio_information,
51
  progressive_sequence, progressive_frame, repeat_first_field, top_field_first,             // interface with resample
52
  vld_err                                                                                   // asserted when vld code parse error
53
  );
54
 
55
  input            clk;                           // clock
56
  input            clk_en;                        // clock enable
57
  input            rst;                           // synchronous active low reset
58
  input      [23:0]getbits;                       // bit-aligned slice data. 16 bits long as longest variable length code is 16 bits.
59
  input            signbit;                       // sign bit of dct coefficient
60
 
61
  output reg  [4:0]advance;                       // number of bits to advance the bitstream (advance <= 24)
62
  output reg       align;                         // byte-align getbits and move forward one byte.
63
 
64
  output reg  [7:0]quant_wr_data;                 // data bus for quantizer matrix rams
65
  output reg  [5:0]quant_wr_addr;                 // address bus for quantizer matrix rams
66
  output reg       quant_rst;                     // reset quant matrices to default values
67
  output reg       wr_intra_quant;                // write enable for intra quantiser matrix
68
  output reg       wr_non_intra_quant;            // write enable for non intra quantiser matrix
69
  output reg       wr_chroma_intra_quant;         // write enable for chroma intra quantiser matrix
70
  output reg       wr_chroma_non_intra_quant;     // write enable for chroma non intra quantiser matrix
71
 
72
  output reg       vld_err;                       // vld_err is asserted when a slice parsing error has occurred. self-repairing.
73
 
74
  parameter [7:0]
75
    STATE_NEXT_START_CODE             = 8'h00,
76
    STATE_START_CODE                  = 8'h01,
77
 
78
    STATE_PICTURE_HEADER              = 8'h02,
79
    STATE_PICTURE_HEADER0             = 8'h03,
80
    STATE_PICTURE_HEADER1             = 8'h04,
81
    STATE_PICTURE_HEADER2             = 8'h05,
82
 
83
    STATE_SEQUENCE_HEADER             = 8'h06,
84
    STATE_SEQUENCE_HEADER0            = 8'h07,
85
    STATE_SEQUENCE_HEADER1            = 8'h08,
86
    STATE_SEQUENCE_HEADER2            = 8'h09,
87
    STATE_SEQUENCE_HEADER3            = 8'h0a,
88
 
89
    STATE_GROUP_HEADER                = 8'h0b,
90
    STATE_GROUP_HEADER0               = 8'h0c,
91
 
92
    STATE_EXTENSION_START_CODE        = 8'h10,
93
    STATE_SEQUENCE_EXT                = 8'h11,
94
    STATE_SEQUENCE_EXT0               = 8'h12,
95
    STATE_SEQUENCE_EXT1               = 8'h13,
96
    STATE_SEQUENCE_DISPLAY_EXT        = 8'h14,
97
    STATE_SEQUENCE_DISPLAY_EXT0       = 8'h15,
98
    STATE_SEQUENCE_DISPLAY_EXT1       = 8'h16,
99
    STATE_SEQUENCE_DISPLAY_EXT2       = 8'h17,
100
    STATE_QUANT_MATRIX_EXT            = 8'h18,
101
    STATE_PICTURE_CODING_EXT          = 8'h19,
102
    STATE_PICTURE_CODING_EXT0         = 8'h1a,
103
    STATE_PICTURE_CODING_EXT1         = 8'h1b,
104
 
105
    STATE_LD_INTRA_QUANT0             = 8'h20,
106
    STATE_LD_INTRA_QUANT1             = 8'h21,
107
    STATE_LD_NON_INTRA_QUANT0         = 8'h22,
108
    STATE_LD_NON_INTRA_QUANT1         = 8'h23,
109
    STATE_LD_CHROMA_INTRA_QUANT1      = 8'h24,
110
    STATE_LD_CHROMA_NON_INTRA_QUANT1  = 8'h25,
111
 
112
    STATE_SLICE                       = 8'h31,
113
    STATE_SLICE_EXTENSION             = 8'h32,
114
    STATE_SLICE_EXTRA_INFORMATION     = 8'h33,
115
    STATE_NEXT_MACROBLOCK             = 8'h34,
116
    STATE_MACROBLOCK_SKIP             = 8'h35,
117
    STATE_DELAY_EMPTY_BLOCK           = 8'h36,
118
    STATE_EMIT_EMPTY_BLOCK            = 8'h37,
119
    STATE_MACROBLOCK_TYPE             = 8'h38,
120
    STATE_MOTION_TYPE                 = 8'h39,
121
    STATE_DCT_TYPE                    = 8'h3a,
122
    STATE_MACROBLOCK_QUANT            = 8'h3b,
123
 
124
    STATE_NEXT_MOTION_VECTOR          = 8'h40,
125
    STATE_MOTION_VERT_FLD_SEL         = 8'h41, // motion_vectors: motion_vertical_field_select
126
    STATE_MOTION_CODE                 = 8'h42, // motion_vector: motion_code
127
    STATE_MOTION_RESIDUAL             = 8'h43, // motion_vector: motion_residual
128
    STATE_MOTION_DMVECTOR             = 8'h44, // motion_vector: dmvector
129
    STATE_MOTION_PREDICT              = 8'h45, // motion_vector: prediction pipeline begin
130
    STATE_MOTION_PIPELINE_FLUSH       = 8'h46, // motion_vector: prediction pipeline end
131
 
132
    STATE_MARKER_BIT_0                = 8'h60,
133
    STATE_CODED_BLOCK_PATTERN         = 8'h61,
134
    STATE_CODED_BLOCK_PATTERN_1       = 8'h62,
135
    STATE_CODED_BLOCK_PATTERN_2       = 8'h63,
136
 
137
    STATE_BLOCK                       = 8'h70,
138
    STATE_NEXT_BLOCK                  = 8'h71,
139
    STATE_DCT_DC_LUMI_SIZE            = 8'h72,
140
    STATE_DCT_DC_CHROMI_SIZE          = 8'h73,
141
    STATE_DCT_DC_DIFF                 = 8'h74,
142
    STATE_DCT_DC_DIFF_0               = 8'h75,
143
    STATE_DCT_SUBS_B15                = 8'h76,
144
    STATE_DCT_ESCAPE_B15              = 8'h77,
145
    STATE_DCT_SUBS_B14                = 8'h78,
146
    STATE_DCT_ESCAPE_B14              = 8'h79,
147
    STATE_DCT_NON_INTRA_FIRST         = 8'h7a,
148
    STATE_NON_CODED_BLOCK             = 8'h7b,
149
    STATE_DCT_ERROR                   = 8'h7c,
150
 
151
    STATE_SEQUENCE_END                = 8'h80,
152
 
153
    STATE_ERROR                       = 8'hff;
154
 
155
  /* start codes */
156
  parameter [7:0]
157
    CODE_PICTURE_START                = 8'h00,
158
    CODE_USER_DATA_START              = 8'hb2,
159
    CODE_SEQUENCE_HEADER              = 8'hb3,
160
    CODE_SEQUENCE_ERROR               = 8'hb4,
161
    CODE_EXTENSION_START              = 8'hb5,
162
    CODE_SEQUENCE_END                 = 8'hb7,
163
    CODE_GROUP_START                  = 8'hb8;
164
 
165
  /* extension start codes */
166
  parameter [3:0]
167
    EXT_SEQUENCE                      = 4'b0001,
168
    EXT_SEQUENCE_DISPLAY              = 4'b0010,
169
    EXT_QUANT_MATRIX                  = 4'b0011,
170
    EXT_COPYRIGHT                     = 4'b0100,
171
    EXT_SEQUENCE_SCALABLE             = 4'b0101,
172
    EXT_PICTURE_DISPLAY               = 4'b0111,
173
    EXT_PICTURE_CODING                = 4'b1000,
174
    EXT_PICTURE_SPATIAL_SCALABLE      = 4'b1001,
175
    EXT_PICTURE_TEMPORAL_SCALABLE     = 4'b1010,
176
    EXT_CAMERA_PARAMETERS             = 4'b1011,
177
    EXT_ITU_T                         = 4'b1100;
178
 
179
`include "vld_codes.v"
180
 
181
  parameter
182
    STRICT_MARKER_BIT = 1'b0;  /* set to 1 to check marker bits are 1. May break some streams. */
183
 
184
  /*
185
   *  Table 7-7 Meaning of indices in PMV[r][s][t], vector[r][s][t] and vector'[r][s][t]
186
   *
187
   *    0                                 1
188
   *  r First motion vector in Macroblock Second motion vector in Macroblock
189
   *  s Forward motion Vector             Backwards motion Vector
190
   *  t Horizontal Component              Vertical Component
191
   *
192
   *  NOTE r also takes the values 2 and 3 for derived motion vectors used with dual-prime prediction.
193
   *  Since these motion vectors are derived they do not themselves have motion vector predictors.
194
   */
195
  parameter [2:0] /* motion vector codes */
196
    MOTION_VECTOR_0_0_0 = 3'd0,
197
    MOTION_VECTOR_0_0_1 = 3'd1,
198
    MOTION_VECTOR_1_0_0 = 3'd2,
199
    MOTION_VECTOR_1_0_1 = 3'd3,
200
    MOTION_VECTOR_0_1_0 = 3'd4,
201
    MOTION_VECTOR_0_1_1 = 3'd5,
202
    MOTION_VECTOR_1_1_0 = 3'd6,
203
    MOTION_VECTOR_1_1_1 = 3'd7;
204
 
205
  reg         [7:0]state;
206
  reg         [7:0]next;
207
 
208
  reg         [4:0]next_advance;
209
 
210
  reg         [5:0]cnt; // counter used when loading quant matrices
211
 
212
  /* in start code */
213
  wire        [7:0]start_code;
214
 
215
  /* position in video stream */
216
  reg              sequence_header_seen;    /* set when sequence header encountered, cleared when sequence end encountered  */
217
  reg              sequence_extension_seen; /* set when sequence extension encountered, cleared when sequence end encountered  */
218
  reg              picture_header_seen;     /* set when picture header encountered, cleared when sequence end encountered  */
219
 
220
  /* in sequence header */
221
  output wire[13:0]horizontal_size;
222
  output wire[13:0]vertical_size;
223
  output reg  [7:0]mb_width;   /* par. 6.3.3. width of the encoded luminance component of pictures in macroblocks */
224
  output reg  [7:0]mb_height;  /* par. 6.3.3. height of the encoded luminance component of frame pictures in macroblocks */
225
  output wire [3:0]aspect_ratio_information;
226
  output wire [3:0]frame_rate_code;
227
  wire       [29:0]bit_rate;
228
  wire       [17:0]vbv_buffer_size;
229
  wire             constrained_parameters_flag;
230
 
231
  /* in sequence header extension */
232
  wire        [7:0]profile_and_level_indication;
233
  output wire      progressive_sequence;
234
  output wire [1:0]chroma_format;
235
  wire             low_delay;
236
  output wire [1:0]frame_rate_extension_n;
237
  output wire [4:0]frame_rate_extension_d;
238
 
239
  /* in sequence display extension */
240
  wire        [2:0]video_format;
241
  wire        [7:0]colour_primaries;
242
  wire        [7:0]transfer_characteristics;
243
  output wire [7:0]matrix_coefficients;
244
  output wire[13:0]display_horizontal_size;
245
  output wire[13:0]display_vertical_size;
246
 
247
  /* in picture coding extension */
248
  wire        [3:0]f_code_00;
249
  wire        [3:0]f_code_01;
250
  wire        [3:0]f_code_10;
251
  wire        [3:0]f_code_11;
252
  output wire [1:0]intra_dc_precision;
253
  output wire [1:0]picture_structure;
254
  output wire      top_field_first;
255
  wire             frame_pred_frame_dct;
256
  wire             concealment_motion_vectors;
257
  output wire      q_scale_type;
258
  wire             intra_vlc_format;
259
  output wire      alternate_scan;
260
  output wire      repeat_first_field;
261
  wire             chroma_420_type;
262
  output wire      progressive_frame;
263
  wire             composite_display_flag;
264
  wire             v_axis;
265
  wire        [2:0]field_sequence;
266
  wire             sub_carrier;
267
  wire        [6:0]burst_amplitude;
268
  wire        [7:0]sub_carrier_phase;
269
 
270
  /* in group of pictures header */
271
  wire             drop_flag;
272
  wire        [4:0]time_code_hours;
273
  wire        [5:0]time_code_minutes;
274
  wire        [5:0]time_code_seconds;
275
  wire        [5:0]time_code_pictures;
276
  wire             closed_gop;
277
  wire             broken_link;
278
 
279
  /* in picture header */
280
  wire        [9:0]temporal_reference;
281
  output wire [2:0]picture_coding_type;
282
  wire       [15:0]vbv_delay;
283
  wire             full_pel_forward_vector;
284
  wire        [2:0]forward_f_code;
285
  wire             full_pel_backward_vector;
286
  wire        [2:0]backward_f_code;
287
 
288
  /* in slice */
289
  output reg  [4:0]quantiser_scale_code;
290
  reg              slice_extension_flag;
291
  reg              intra_slice;
292
  reg              slice_picture_id_enable;
293
  reg         [5:0]slice_picture_id;
294
  reg         [7:0]slice_vertical_position;
295
  reg              first_macroblock_of_slice;
296
 
297
  /* macroblock address increment */
298
  wire        [3:0]macroblock_addr_inc_length;
299
  wire        [5:0]macroblock_addr_inc_value;
300
  wire        [6:0]macroblock_addr_inc_value_ext = macroblock_addr_inc_value;
301
  wire             macroblock_addr_inc_escape;
302
  reg         [6:0]macroblock_address_increment;
303
  /*
304
   * macroblock address is valid after STATE_MACROBLOCK_TYPE and before STATE_NEXT_BLOCK.
305
   * In particular: In the first slice of a picture, macroblock_address may be 0xffff after STATE_SLICE up to STATE_MACROBLOCK_TYPE.
306
   */
307
  output reg [12:0]macroblock_address; // High Level: macroblock_address is 13 bits, 0..8159, 8160..8191 unused.
308
  reg        [10:0]empty_blocks;
309
 
310
  /* macroblock type */
311
  wire        [3:0]macroblock_type_length;
312
  wire        [5:0]macroblock_type_value;
313
  reg              macroblock_quant;
314
  output reg       macroblock_motion_forward;
315
  output reg       macroblock_motion_backward;
316
  reg              macroblock_pattern;
317
  output reg       macroblock_intra;
318
  reg              macroblock_type_intra;
319
  reg              spatial_temporal_weight_code_flag; // ought always to be zero, as we don't do scaleability
320
  output reg  [1:0]motion_type; // one reg for both frame_motion_type and field_motion_type
321
  output reg       dct_type; // dct_type == 1 : field dct coded; dct_type == 0 : frame dct coded
322
  /* derived variables (par. 6.3.17.1, tables 6.17 and 6.18) */
323
  wire             motion_vector_count_is_one;
324
  reg         [1:0]mv_format;
325
  reg              dmv;
326
 
327
  /* motion vectors */
328
  reg         [2:0]motion_vector;  // current motion vector
329
  reg         [7:0]motion_vector_reg;  // bitmask of motion vectors present
330
  reg         [3:0]r_size; // f_code_xx  - 4'd1;
331
  wire             vertical_field_select_present;
332
  reg         [4:0]motion_code;
333
  reg              motion_code_neg;
334
  reg         [7:0]motion_code_residual; // motion_code_residual has up to r_size = f_code_xx - 1 bits. (par. 6.3.17.3) with f_code = 1..9 (Table E-8, high level)
335
  reg signed  [1:0]dmvector;
336
 
337
  /*
338
   * par. 7.6.3.1, if f_code_xx = 15 the pmv range low..high would need 21 bits.
339
   * But restricting ourselves to high level@high profile allows us to pack
340
   * the motion vectors tighter.
341
   * Table E-8: for High Level f_code_0_0, f_code_1_0: 1..9
342
   *                           f_code_0_1, f_code_1_1: 1..5
343
   *
344
   * Hence all f_code values are in the range 1..9.
345
   * This implies the motion vectors are in the range -4096..4095. (par. 7.6.3.1)
346
   * This means we can represent all motion vectors with 13 bits.
347
   *
348
   */
349
  output reg signed [12:0]pmv_0_0_0;
350
  output reg signed [12:0]pmv_0_0_1;
351
  output reg signed [12:0]pmv_1_0_0;
352
  output reg signed [12:0]pmv_1_0_1;
353
  output reg signed [12:0]pmv_0_1_0;
354
  output reg signed [12:0]pmv_0_1_1;
355
  output reg signed [12:0]pmv_1_1_0;
356
  output reg signed [12:0]pmv_1_1_1;
357
  output reg signed [12:0]dmv_0_0; // dual-prime motion vectors
358
  output reg signed [12:0]dmv_1_0;
359
  output reg signed [12:0]dmv_0_1;
360
  output reg signed [12:0]dmv_1_1;
361
 
362
  /* motion vector pipeline, stage 1 */
363
  reg         [2:0]motion_vector_0;
364
  reg              motion_vector_valid_0;
365
  reg              motion_code_neg_0;
366
  reg         [3:0]r_size_0;
367
  reg signed  [1:0]dmvector_0;
368
  reg signed [12:0]pmv_0;
369
  reg signed [12:0]pmv_delta_0;
370
 
371
  /* motion vector pipeline, stage 2 */
372
  reg         [2:0]motion_vector_1;
373
  reg              motion_vector_valid_1;
374
  reg         [3:0]r_size_1;
375
  reg signed  [1:0]dmvector_1;
376
  reg signed [12:0]pmv_1;
377
 
378
  /* motion vector pipeline, stage 3 */
379
  reg         [2:0]motion_vector_2;
380
  reg              motion_vector_valid_2;
381
  reg signed  [1:0]dmvector_2;
382
  reg signed [12:0]pmv_2;
383
 
384
  /* motion vector pipeline, stage 4 */
385
  reg         [2:0]motion_vector_3;
386
  reg              motion_vector_valid_3;
387
  reg signed [12:0]pmv_3;
388
  reg signed  [1:0]dmvector_3;
389
 
390
  /* motion vector pipeline, stage 5 */
391
  reg         [2:0]motion_vector_4;
392
  reg              motion_vector_valid_4;
393
  reg signed [12:0]pmv_4;
394
 
395
  /* motion vector pipeline, stage 6 */
396
  reg         [2:0]motion_vector_5;
397
  reg              motion_vector_valid_5;
398
  reg signed [12:0]pmv_5;
399
 
400
  /* motion vector pipeline, output */
401
  output reg       motion_vector_valid; // motion_vector valid is asserted when pmv_x_x_x dmv_x_x valid
402
 
403
  /* coded block pattern */
404
  reg        [11:0]coded_block_pattern;  // derived from coded_block_pattern_420, coded_block_pattern_1 and coded_block_pattern_2
405
 
406
  /* dct coefficients */
407
  reg         [3:0]dct_dc_size;          // holds dct_dc_size_luminance or dct_dc_size_chrominance, depending.
408
  reg        [10:0]dct_dc_pred;          // dct_dc_pred_0, dct_dc_pred_1 or dct_dc_pred_2, depending upon whether it's a Y, Cr or Cb block.
409
 
410
  reg        [10:0]dct_dc_pred_0;        // luminance (Y) dct dc predictor, par. 7.2.1
411
  reg        [10:0]dct_dc_pred_1;        // first chrominance (Cb) dct dc predictor, par. 7.2.1
412
  reg        [10:0]dct_dc_pred_2;        // second chrominance (Cr) dct dc predictor, par. 7.2.1
413
 
414
  /*
415
   * Each bit in block_pattern_code, block_lumi_code, ... corresponds to a block.
416
   * The first block corresponds to the leftmost bit (= bit 11) the second block to bit 10, etc.
417
   *
418
   * For instance, this is what things look like before the first block of a 4:2:0 macroblock:
419
   *
420
   * testbench.mpeg2_decoder.vld     STATE_BLOCK
421
   * testbench.mpeg2_decoder.vld     coded_block_pattern:  b111111000000
422
   * testbench.mpeg2_decoder.vld     block_pattern_code:   b111111000000
423
   * testbench.mpeg2_decoder.vld     block_lumi_code:     b011110
424
   * testbench.mpeg2_decoder.vld     block_chromi1_code:  b0000010101010
425
   * testbench.mpeg2_decoder.vld     block_chromi2_code:  b0000001010101
426
   *
427
   * 6 blocks, first 4 luminance blocks, then a chromi1 (Cb) block, finally a chromi2 (Cr) block.
428
   * Each block, block_pattern_code, block_lumi_code, block_chromi1_code and block_chromi2_code shift left one bit:
429
   *
430
   * testbench.mpeg2_decoder.vld     STATE_NEXT_BLOCK
431
   * testbench.mpeg2_decoder.vld     coded_block_pattern:  b111110000000
432
   * testbench.mpeg2_decoder.vld     block_pattern_code:   b111110000000
433
   * testbench.mpeg2_decoder.vld     block_lumi_code:     b111100
434
   * testbench.mpeg2_decoder.vld     block_chromi1_code:  b0000101010100
435
   * testbench.mpeg2_decoder.vld     block_chromi2_code:  b0000010101010
436
   * ...
437
   * testbench.mpeg2_decoder.vld     STATE_NEXT_BLOCK
438
   * testbench.mpeg2_decoder.vld     coded_block_pattern:  b111100000000
439
   * testbench.mpeg2_decoder.vld     block_pattern_code:   b111100000000
440
   * testbench.mpeg2_decoder.vld     block_lumi_code:     b111000
441
   * testbench.mpeg2_decoder.vld     block_chromi1_code:  b0001010101000
442
   * testbench.mpeg2_decoder.vld     block_chromi2_code:  b0000101010100
443
   * ...
444
   *
445
   * The loop ends when all bits have been shifted out of block_pattern_code:
446
   *
447
   * testbench.mpeg2_decoder.vld     STATE_NEXT_BLOCK
448
   * testbench.mpeg2_decoder.vld     coded_block_pattern:  b000000000000
449
   * testbench.mpeg2_decoder.vld     block_pattern_code:   b000000000000
450
   * testbench.mpeg2_decoder.vld     block_lumi_code:     b000000
451
   * testbench.mpeg2_decoder.vld     block_chromi1_code:  b0101010000000
452
   * testbench.mpeg2_decoder.vld     block_chromi2_code:  b1010101000000
453
   *
454
   * Note there still remain bits in block_chromi1_code and block_chromi2_code:
455
   * these would have been used in 4:2:2 and 4:4:4 video.
456
   *
457
   */
458
 
459
  reg        [11:0]block_pattern_code;   // block_pattern_code[i] is one if the corresponding block exists in the macroblock.
460
  reg        [12:7]block_lumi_code;      // block_lumi_code[i] is one if the corresponding block is a luminance block. bits 6:0 are always zero.
461
  reg        [12:0]block_chromi1_code;   // block_chromi1_code[i] is one if the corresponding block is a Cb chrominance block.
462
  reg        [12:0]block_chromi2_code;   // block_chromi2_code[i] is one if the corresponding block is a Cr chrominance block.
463
 
464
  wire             dct_coefficient_escape;
465
  output reg  [5:0]dct_coeff_run;
466
  output reg signed [11:0]dct_coeff_signed_level;
467
  output reg       dct_coeff_end;        // asserted after last dct_coeff_run/dct_coeff_signed_level
468
  reg              dct_coeff_valid;
469
  reg              dct_error;            // set when error occurs in dct decoding
470
  reg         [5:0]dct_coeff_run_0;
471
  reg signed [11:0]dct_coeff_signed_level_0;
472
  reg              dct_coeff_valid_0;
473
  reg              dct_coeff_end_0;
474
  reg              dct_coeff_apply_signbit_0;
475
  output reg       rld_wr_en;            // asserted when dct_coeff_run, dct_coeff_signed_level, and dct_coeff_end valid, or when quantizer rams need update
476
  output reg  [1:0]rld_cmd;              // RLD_DCT when dct_coeff_run, dct_coeff_signed_level, and dct_coeff_end valid, RLD_QUANT when quantizer rams need update, RLD_NOOP otherwise
477
 
478
  /* coded block pattern vlc lookup */
479
  wire        [3:0]coded_block_pattern_length;
480
  wire        [5:0]coded_block_pattern_value;
481
 
482
  /* motion code vlc lookup */
483
  wire        [3:0]motion_code_length;
484
  wire        [4:0]motion_code_value;
485
  wire             motion_code_sign;
486
 
487
  /* dmvector vlc lookup */
488
  wire        [1:0]dmvector_length;
489
  wire             dmvector_value;
490
  wire             dmvector_sign;
491
 
492
  /* dct dc size luminance vlc lookup */
493
  wire        [3:0]dct_dc_size_luminance_length;
494
  wire        [4:0]dct_dc_size_luminance_value;
495
 
496
  /* dct dc size chrominance vlc lookup */
497
  wire        [3:0]dct_dc_size_chrominance_length;
498
  wire        [4:0]dct_dc_size_chrominance_value;
499
 
500
  /* dct coefficient 0 vlc lookup */
501
  reg        [15:0]dct_coefficient_0_decoded;
502
  reg        [15:0]dct_non_intra_first_coefficient_0_decoded;
503
 
504
  /* dct coefficient 1 vlc lookup */
505
  reg        [15:0]dct_coefficient_1_decoded;
506
 
507
`include "vlc_tables.v"
508
 
509
  /* next state logic */
510
  always @*
511
    casex (state)
512
      STATE_NEXT_START_CODE:           if (getbits == 24'h000001) next = STATE_START_CODE;
513
                                       else next = STATE_NEXT_START_CODE;
514
 
515
      STATE_START_CODE:                casex(getbits[7:0])
516
                                         CODE_PICTURE_START:    if (sequence_header_seen & sequence_extension_seen) next = STATE_PICTURE_HEADER;
517
                                                                else next = STATE_NEXT_START_CODE;
518
                                         CODE_USER_DATA_START:  next = STATE_NEXT_START_CODE;
519
                                         CODE_SEQUENCE_HEADER:  next = STATE_SEQUENCE_HEADER;
520
                                         CODE_SEQUENCE_ERROR:   next = STATE_NEXT_START_CODE;
521
                                         CODE_EXTENSION_START:  next = STATE_EXTENSION_START_CODE;
522
                                         CODE_SEQUENCE_END:     next = STATE_SEQUENCE_END;
523
                                         CODE_GROUP_START:      next = STATE_GROUP_HEADER;
524
                                         8'h01,
525
                                         8'h02,
526
                                         8'h03,
527
                                         8'h04,
528
                                         8'h05,
529
                                         8'h06,
530
                                         8'h07,
531
                                         8'h08,
532
                                         8'h09,
533
                                         8'h0a,
534
                                         8'h0b,
535
                                         8'h0c,
536
                                         8'h0d,
537
                                         8'h0e,
538
                                         8'h0f,
539
                                         8'h1x,
540
                                         8'h2x,
541
                                         8'h3x,
542
                                         8'h4x,
543
                                         8'h5x,
544
                                         8'h6x,
545
                                         8'h7x,
546
                                         8'h8x,
547
                                         8'h9x,
548
                                         8'hax:                  if (sequence_header_seen & sequence_extension_seen & picture_header_seen) next = STATE_SLICE;
549
                                                                 else next = STATE_NEXT_START_CODE;
550
                                         default                 next = STATE_NEXT_START_CODE;
551
                                       endcase
552
 
553
      STATE_EXTENSION_START_CODE:      casex(getbits[23:20])
554
                                         EXT_SEQUENCE:                  next = STATE_SEQUENCE_EXT;
555
                                         EXT_SEQUENCE_DISPLAY:          next = STATE_SEQUENCE_DISPLAY_EXT;
556
                                         EXT_QUANT_MATRIX:              next = STATE_QUANT_MATRIX_EXT;
557
                                         EXT_COPYRIGHT:                 next = STATE_NEXT_START_CODE;
558
                                         EXT_SEQUENCE_SCALABLE:         next = STATE_NEXT_START_CODE;
559
                                         EXT_PICTURE_DISPLAY:           next = STATE_NEXT_START_CODE; // Pan & scan
560
                                         EXT_PICTURE_CODING:            next = STATE_PICTURE_CODING_EXT;
561
                                         EXT_PICTURE_SPATIAL_SCALABLE:  next = STATE_NEXT_START_CODE;
562
                                         EXT_PICTURE_TEMPORAL_SCALABLE: next = STATE_NEXT_START_CODE;
563
                                         EXT_CAMERA_PARAMETERS:         next = STATE_NEXT_START_CODE;
564
                                         EXT_ITU_T:                     next = STATE_NEXT_START_CODE;
565
                                         default                        next = STATE_NEXT_START_CODE;
566
                                       endcase
567
 
568
      /* par. 6.2.2.1: sequence header */
569
      STATE_SEQUENCE_HEADER:           next = STATE_SEQUENCE_HEADER0;
570
 
571
      STATE_SEQUENCE_HEADER0:          next = STATE_SEQUENCE_HEADER1;
572
 
573
      STATE_SEQUENCE_HEADER1:          next = STATE_SEQUENCE_HEADER2;
574
 
575
      STATE_SEQUENCE_HEADER2:          if (getbits[5] || ~STRICT_MARKER_BIT) next = STATE_SEQUENCE_HEADER3; // check marker bit
576
                                       else next = STATE_SEQUENCE_HEADER3;
577
 
578
      STATE_SEQUENCE_HEADER3:          if (getbits[12]) next = STATE_LD_INTRA_QUANT0;
579
                                       else if (getbits[11]) next = STATE_LD_NON_INTRA_QUANT0;
580
                                       else next = STATE_NEXT_START_CODE;
581
 
582
      STATE_LD_INTRA_QUANT0:           if (cnt != 6'b111111) next = STATE_LD_INTRA_QUANT0;
583
                                       else if (getbits[15]) next = STATE_LD_NON_INTRA_QUANT0;
584
                                       else next = STATE_NEXT_START_CODE;
585
 
586
      STATE_LD_NON_INTRA_QUANT0:       if (cnt != 6'b111111) next = STATE_LD_NON_INTRA_QUANT0;
587
                                       else next = STATE_NEXT_START_CODE;
588
 
589
      /* par. 6.2.2.3: Sequence extension */
590
      STATE_SEQUENCE_EXT:              next = STATE_SEQUENCE_EXT0;
591
 
592
      STATE_SEQUENCE_EXT0:             if (getbits[11] || ~STRICT_MARKER_BIT) next = STATE_SEQUENCE_EXT1; // check marker bit
593
                                       else next = STATE_ERROR;
594
 
595
      STATE_SEQUENCE_EXT1:             next = STATE_NEXT_START_CODE;
596
 
597
      /* par. 6.2.2.4: Sequence display extension */
598
      STATE_SEQUENCE_DISPLAY_EXT:      if (getbits[20]) next = STATE_SEQUENCE_DISPLAY_EXT0;
599
                                       else next = STATE_SEQUENCE_DISPLAY_EXT1;
600
 
601
      STATE_SEQUENCE_DISPLAY_EXT0:     next = STATE_SEQUENCE_DISPLAY_EXT1;
602
 
603
      STATE_SEQUENCE_DISPLAY_EXT1:     if (getbits[9]) next = STATE_SEQUENCE_DISPLAY_EXT2;
604
                                       else next = STATE_NEXT_START_CODE;
605
 
606
      STATE_SEQUENCE_DISPLAY_EXT2:     next = STATE_NEXT_START_CODE;
607
 
608
      /* par. 6.2.3.2: Quant matrix  extension */
609
      STATE_QUANT_MATRIX_EXT:          if (getbits[23]) next = STATE_LD_INTRA_QUANT1;
610
                                       else if (getbits[22]) next = STATE_LD_NON_INTRA_QUANT1;
611
                                       else if (getbits[21]) next = STATE_LD_CHROMA_INTRA_QUANT1;
612
                                       else if (getbits[20]) next = STATE_LD_CHROMA_NON_INTRA_QUANT1;
613
                                       else next = STATE_NEXT_START_CODE;
614
 
615
      STATE_LD_INTRA_QUANT1:           if (cnt != 6'b111111) next = STATE_LD_INTRA_QUANT1;
616
                                       else if (getbits[14]) next = STATE_LD_NON_INTRA_QUANT1;
617
                                       else if (getbits[13]) next = STATE_LD_CHROMA_INTRA_QUANT1;
618
                                       else if (getbits[12]) next = STATE_LD_CHROMA_NON_INTRA_QUANT1;
619
                                       else next = STATE_NEXT_START_CODE;
620
 
621
      STATE_LD_NON_INTRA_QUANT1:       if (cnt != 6'b111111) next = STATE_LD_NON_INTRA_QUANT1;
622
                                       else if (getbits[13]) next = STATE_LD_CHROMA_INTRA_QUANT1;
623
                                       else if (getbits[12]) next = STATE_LD_CHROMA_NON_INTRA_QUANT1;
624
                                       else next = STATE_NEXT_START_CODE;
625
 
626
      STATE_LD_CHROMA_INTRA_QUANT1:    if (cnt != 6'b111111) next = STATE_LD_CHROMA_INTRA_QUANT1;
627
                                       else if (getbits[12]) next = STATE_LD_CHROMA_NON_INTRA_QUANT1;
628
                                       else next = STATE_NEXT_START_CODE;
629
 
630
      STATE_LD_CHROMA_NON_INTRA_QUANT1: if (cnt != 6'b111111) next = STATE_LD_CHROMA_NON_INTRA_QUANT1;
631
                                       else next = STATE_NEXT_START_CODE;
632
 
633
      /* par. 6.2.3.1: Picture coding extension */
634
      STATE_PICTURE_CODING_EXT:        next = STATE_PICTURE_CODING_EXT0;
635
 
636
      STATE_PICTURE_CODING_EXT0:       if (getbits[10]) next = STATE_PICTURE_CODING_EXT1;
637
                                       else next = STATE_NEXT_START_CODE;
638
 
639
      STATE_PICTURE_CODING_EXT1:       next = STATE_NEXT_START_CODE;
640
 
641
      /* par. 6.2.2.6: group of pictures header */
642
      STATE_GROUP_HEADER:              next = STATE_GROUP_HEADER0;
643
 
644
      STATE_GROUP_HEADER0:             next = STATE_NEXT_START_CODE;
645
 
646
      /* par. 6.2.3: picture header */
647
      STATE_PICTURE_HEADER:            next = STATE_PICTURE_HEADER0;
648
 
649
      STATE_PICTURE_HEADER0:           if ((picture_coding_type == 3'h2) || (picture_coding_type == 3'h3)) next = STATE_PICTURE_HEADER1;
650
                                       else next = STATE_NEXT_START_CODE;
651
 
652
      STATE_PICTURE_HEADER1:           if (picture_coding_type == 3'h3) next = STATE_PICTURE_HEADER2;
653
                                       else next = STATE_NEXT_START_CODE;
654
 
655
      STATE_PICTURE_HEADER2:           next = STATE_NEXT_START_CODE;
656
 
657
      /* par. 6.2.4: slice */
658
      STATE_SLICE:                     if (getbits[18]) next = STATE_SLICE_EXTENSION; // getbits[18] is slice_extension_flag
659
                                       else next = STATE_NEXT_MACROBLOCK;
660
 
661
      STATE_SLICE_EXTENSION:           if (getbits[15]) next = STATE_SLICE_EXTRA_INFORMATION; // getbits[15] is extra_bit_slice
662
                                       else next = STATE_NEXT_MACROBLOCK;
663
 
664
      STATE_SLICE_EXTRA_INFORMATION:   if (getbits[15]) next = STATE_SLICE_EXTRA_INFORMATION; // getbits[15] indicates another extra_information_slice byte follows
665
                                       else next = STATE_NEXT_MACROBLOCK;
666
 
667
      STATE_NEXT_MACROBLOCK:           if (macroblock_addr_inc_escape) next = STATE_NEXT_MACROBLOCK; // macroblock address escape
668
                                       else if (macroblock_addr_inc_value == 6'd0) next = STATE_ERROR;
669
                                       else if (first_macroblock_of_slice) next = STATE_MACROBLOCK_TYPE; // par. 6.3.16.1: syntax does not allow the first and last macroblock of a slice to be skipped
670
                                       else if ((macroblock_address_increment + macroblock_addr_inc_value_ext) != 7'd1) next = STATE_MACROBLOCK_SKIP; // macroblocks skipped. macroblock_address_increment + macroblock_addr_inc_value_ext is next value of macroblock_address_increment.
671
                                       else next = STATE_MACROBLOCK_TYPE;
672
 
673
      STATE_MACROBLOCK_SKIP:           if (macroblock_address_increment == 7'd1) next = STATE_MACROBLOCK_TYPE;
674
                                       else next = STATE_DELAY_EMPTY_BLOCK;
675
 
676
      STATE_DELAY_EMPTY_BLOCK:         next = STATE_EMIT_EMPTY_BLOCK; // to avoid motion vector and dct_coeff valid at same moment
677
 
678
      STATE_EMIT_EMPTY_BLOCK:          if ((empty_blocks[10] == 1'b0) && (macroblock_address_increment == 7'd1)) next = STATE_MACROBLOCK_TYPE;
679
                                       else if (empty_blocks[10] == 1'b0) next = STATE_MACROBLOCK_SKIP;  // STATE_EMIT_EMPTY_BLOCK emits a block of 8x8 zeroes. par. 7.7.2.
680
                                       else next = STATE_EMIT_EMPTY_BLOCK;
681
 
682
      STATE_MACROBLOCK_TYPE:           /* This is what the following lines should look like, only the variables haven't been clocked in yet,
683
                                        * so we address macroblock_type_value directly.
684
                                        * if ((macroblock_type_length == 4'b0) || (spatial_temporal_weight_code_flag)) next = STATE_ERROR; // we don't do scaleability
685
                                        * else next = STATE_MOTION_TYPE; // frame or field motion_type
686
                                        */
687
 
688
                                       // macroblock_type_length == 0 if macroblock_type code lookup fails.
689
                                       // macroblock_type_value[0] indicates scaleability; we don't do scaleability.
690
                                       if ((macroblock_type_length == 4'b0) || macroblock_type_value[0]) next = STATE_ERROR; // we don't do scaleability
691
                                       else next = STATE_MOTION_TYPE; // frame or field motion_type
692
 
693
      STATE_MOTION_TYPE:               if ((picture_structure == FRAME_PICTURE) && (frame_pred_frame_dct == 1'b0) && (macroblock_intra || macroblock_pattern)) next = STATE_DCT_TYPE;
694
                                       else next = STATE_MACROBLOCK_QUANT;
695
 
696
      STATE_DCT_TYPE:                  next = STATE_MACROBLOCK_QUANT;
697
 
698
      STATE_MACROBLOCK_QUANT:          next = STATE_NEXT_MOTION_VECTOR;
699
 
700
      /* motion_vectors */
701
      STATE_NEXT_MOTION_VECTOR:        if (motion_vector_reg == 8'b0) next = STATE_MOTION_PIPELINE_FLUSH;
702
                                       // motion_vector_reg[7] indicates whether the current motion_vector is present
703
                                       else if (motion_vector_reg[7] && vertical_field_select_present) next = STATE_MOTION_VERT_FLD_SEL;
704
                                       else if (motion_vector_reg[7]) next = STATE_MOTION_CODE;
705
                                       else next = STATE_NEXT_MOTION_VECTOR; // increment motion_vector; shift motion_vector_reg one bit to the left
706
 
707
      STATE_MOTION_PIPELINE_FLUSH:     if (motion_vector_valid_0 || motion_vector_valid_1 || motion_vector_valid_2 || motion_vector_valid_3 || motion_vector_valid_4 || motion_vector_valid_5) next = STATE_MOTION_PIPELINE_FLUSH; // wait for motion vector pipeline to flush.   
708
                                       else // no more motion vectors present, motion vector pipeline empty.
709
                                         begin
710
                                           if (macroblock_intra && concealment_motion_vectors) next = STATE_MARKER_BIT_0;
711
                                           else if (macroblock_pattern) next = STATE_CODED_BLOCK_PATTERN;
712
                                           else next = STATE_BLOCK;
713
                                         end
714
 
715
      STATE_MOTION_VERT_FLD_SEL:       next = STATE_MOTION_CODE; // motion_vectors(0), motion_vertical_field_select(0,0)
716
 
717
      STATE_MOTION_CODE:               // motion_vectors(0), motion_vector(0,0), motion_code(0,0,0) 
718
                                       if (motion_code_length == 4'b0) next = STATE_ERROR;
719
                                       else if ((r_size != 4'd0) && (getbits[23] != 1'b1)) next = STATE_MOTION_RESIDUAL; // (r_size != 4'd0) is equivalent to (f_code_xx != 4'd1)
720
                                       else if (dmv) next = STATE_MOTION_DMVECTOR;
721
                                       else next = STATE_MOTION_PREDICT;
722
 
723
      STATE_MOTION_RESIDUAL:           // motion_vectors(0), motion_vector(0,0), motion_residual(0,0,0)
724
                                       if (dmv) next = STATE_MOTION_DMVECTOR;
725
                                       else next = STATE_MOTION_PREDICT;
726
 
727
      STATE_MOTION_DMVECTOR:           // motion_vectors(0), motion_vector(0,0), dmvector(0)
728
                                       if (dmvector_length  == 2'b0) next = STATE_ERROR;
729
                                       else next = STATE_MOTION_PREDICT;
730
 
731
      STATE_MOTION_PREDICT:            next = STATE_NEXT_MOTION_VECTOR; // increment motion_vector; shift motion_vector_reg one bit to the left
732
 
733
      /* coded block pattern */
734
      STATE_MARKER_BIT_0:              // skip marker bit
735
                                       if (~getbits[23] && STRICT_MARKER_BIT) next = STATE_ERROR;
736
                                       else if (macroblock_pattern) next = STATE_CODED_BLOCK_PATTERN;
737
                                       else next = STATE_BLOCK;
738
 
739
      STATE_CODED_BLOCK_PATTERN:       if (coded_block_pattern_length == 4'b0) next = STATE_ERROR; // Invalid coded_block_pattern code
740
                                       else if (chroma_format == CHROMA422) next = STATE_CODED_BLOCK_PATTERN_1;
741
                                       else if (chroma_format == CHROMA444) next = STATE_CODED_BLOCK_PATTERN_2;
742
                                       else next = STATE_BLOCK;
743
 
744
      STATE_CODED_BLOCK_PATTERN_1:     next = STATE_BLOCK;
745
 
746
      STATE_CODED_BLOCK_PATTERN_2:     next = STATE_BLOCK;
747
 
748
      /* DCT coefficients */
749
      STATE_BLOCK:                     next = STATE_NEXT_BLOCK; // initialize coded_block_pattern, block_pattern_code,  block_lumi_code, block_chromi1_code, block_chromi2_code
750
 
751
      STATE_NEXT_BLOCK:                if (coded_block_pattern[11] && macroblock_intra && block_lumi_code[11]) next = STATE_DCT_DC_LUMI_SIZE; // luminance block
752
                                       else if (coded_block_pattern[11] && macroblock_intra) next = STATE_DCT_DC_CHROMI_SIZE; // chrominance block
753
                                       else if (coded_block_pattern[11]) next = STATE_DCT_NON_INTRA_FIRST;
754
                                       else if (block_pattern_code[11]) next = STATE_NON_CODED_BLOCK;
755
                                       else if (block_pattern_code != 12'b0) next = STATE_NEXT_BLOCK; // shift block_pattern_code and block_lumi_code one bit, find next block
756
                                       else if ((getbits[23:1] == 23'b0) || dct_error) next = STATE_NEXT_START_CODE; // end of slice, go to next start code (par. 6.2.4). In case of error, synchronize at next start code.
757
                                       else next = STATE_NEXT_MACROBLOCK; // end of macroblock, but not end of slice: go to next macroblock. 
758
 
759
      STATE_DCT_DC_LUMI_SIZE:          if (dct_dc_size_luminance_length == 4'b0) next = STATE_DCT_ERROR;
760
                                       else next = STATE_DCT_DC_DIFF; // table B-12 lookup of first luminance dct coefficient
761
 
762
      STATE_DCT_DC_CHROMI_SIZE:        if (dct_dc_size_chrominance_length == 4'b0) next = STATE_DCT_ERROR;
763
                                       else next = STATE_DCT_DC_DIFF; // table B-13 lookup of first chrominance dct coefficient
764
 
765
      STATE_DCT_DC_DIFF:               next = STATE_DCT_DC_DIFF_0;
766
 
767
      STATE_DCT_DC_DIFF_0:             if (intra_vlc_format) next = STATE_DCT_SUBS_B15; // see table 7-3. look up subsequent dct coefficient of intra block in table B-15
768
                                       else next = STATE_DCT_SUBS_B14; // see table 7-3. look up subsequent dct coefficient of intra block in table B-14
769
 
770
      STATE_DCT_SUBS_B15:              // subsequent dct coefficients of intra block, as in table B-15
771
                                       if (getbits[23:20] == 4'b0110) next = STATE_NEXT_BLOCK; // end of this block, go to next block
772
                                       else if (dct_coefficient_escape) next = STATE_DCT_ESCAPE_B15; // Escape
773
                                       else if (dct_coefficient_1_decoded[15:11] == 5'b0) next = STATE_DCT_ERROR; // unknown code
774
                                       else next = STATE_DCT_SUBS_B15;
775
 
776
      STATE_DCT_ESCAPE_B15:            // table B-16 escapes to table B-15
777
                                       next = STATE_DCT_SUBS_B15;
778
 
779
      STATE_DCT_NON_INTRA_FIRST:       // first dct coefficient of non-intra block, as in table B-14, note 2 and 3
780
                                       if (dct_coefficient_escape) next = STATE_DCT_ESCAPE_B14; // table B-14 escape
781
                                       else if (dct_non_intra_first_coefficient_0_decoded[15:11] == 5'b0) next = STATE_DCT_ERROR; // unknown code
782
                                       else  next = STATE_DCT_SUBS_B14;
783
 
784
      STATE_DCT_SUBS_B14:              // table B-14 (with B-16 escapes) lookup of subsequent dct coefficients.
785
                                       if (getbits[23:22] == 2'b10) next = STATE_NEXT_BLOCK; // end of this block, go to next block
786
                                       else if (dct_coefficient_escape) next = STATE_DCT_ESCAPE_B14; // Escape
787
                                       else if (dct_coefficient_0_decoded[15:11] == 5'b0) next = STATE_DCT_ERROR; // unknown code
788
                                       else next = STATE_DCT_SUBS_B14;
789
 
790
      STATE_DCT_ESCAPE_B14:            // table B-16 escapes to table B-15
791
                                       next = STATE_DCT_SUBS_B14;
792
 
793
      STATE_NON_CODED_BLOCK:           next = STATE_NEXT_BLOCK; // Output end-of-block for all-zeroes non-coded block
794
 
795
      STATE_DCT_ERROR:                 next = STATE_NON_CODED_BLOCK; // Output all remaining blocks as non-coded blocks. clears coded_block_pattern and sets dct_error and vld_err flags.
796
 
797
      STATE_SEQUENCE_END:              next = STATE_NEXT_START_CODE; // Output last frame 
798
 
799
      STATE_ERROR:                     next = STATE_NEXT_START_CODE;
800
 
801
      default                          next = STATE_ERROR;
802
 
803
    endcase
804
 
805
  /* advance and align logic. advance is number of bits to advance the bitstream. */
806
  always @*
807
    if (~rst) next_advance = 5'b0;
808
    else
809
      case (state)
810
        STATE_NEXT_START_CODE:            next_advance = 5'd0;             // next_advance is zero in STATE_INIT; but align = 1: we move one byte at a time.
811
        STATE_START_CODE:                 next_advance = 5'd24;            // skip over the 24'h0001sc, where sc = start_code
812
        STATE_EXTENSION_START_CODE:       next_advance = 5'd4;             // skip over the extension start code
813
        /* par. 6.2.2.1: sequence header */
814
        STATE_SEQUENCE_HEADER:            next_advance = 5'd12;            // size of horizontal_size_value
815
        STATE_SEQUENCE_HEADER0:           next_advance = 5'd12;            // size of vertical_size_value
816
        STATE_SEQUENCE_HEADER1:           next_advance = 5'd8;             // size of aspect_ratio_information and frame_rate_code
817
        STATE_SEQUENCE_HEADER2:           next_advance = 5'd19;            // size of bit_rate_value and marker bit
818
        STATE_SEQUENCE_HEADER3:           next_advance = 5'd12;            // size of vbv_buffer_size[9:0], constrained_parameters_flag and load_intra_quantiser_matrix
819
        STATE_LD_INTRA_QUANT0:            next_advance = 5'd8;             // size of one item of quantization table
820
        STATE_LD_NON_INTRA_QUANT0:        next_advance = 5'd8;             // size of one item of quantization table
821
        /* par. 6.2.2.3: Sequence extension */
822
        STATE_SEQUENCE_EXT:               next_advance = 5'd15;            // size of profile_and_level_indication, progressive_sequence, chroma_format, horizontal_size ext, vertical_size ext.
823
        STATE_SEQUENCE_EXT0:              next_advance = 5'd13;            // size of bit_rate ext, marker bit.
824
        STATE_SEQUENCE_EXT1:              next_advance = 5'd16;            // size of vbv_buffer_size ext, low_delay, frame_rate_extension_n, frame_rate_extension_d
825
        /* par. 6.2.2.4: Sequence display extension */
826
        STATE_SEQUENCE_DISPLAY_EXT:       next_advance = 5'd4;             // size of video_format, colour_description
827
        STATE_SEQUENCE_DISPLAY_EXT0:      next_advance = 5'd24;            // size of colour_primaries, transfer_characteristics, matrix_coefficients
828
        STATE_SEQUENCE_DISPLAY_EXT1:      next_advance = 5'd15;            // size of display_horizontal_size, marker bit
829
        STATE_SEQUENCE_DISPLAY_EXT2:      next_advance = 5'd14;            // size of display_vertical_size
830
        /* par. 6.2.3.2: Quant matrix  extension */
831
        STATE_QUANT_MATRIX_EXT:           next_advance = 5'd0;             // no move
832
        STATE_LD_INTRA_QUANT1:            next_advance = 5'd8;             // size of one item of quantization table
833
        STATE_LD_NON_INTRA_QUANT1:        next_advance = 5'd8;             // size of one item of quantization table
834
        STATE_LD_CHROMA_INTRA_QUANT1:     next_advance = 5'd8;             // size of one item of quantization table
835
        STATE_LD_CHROMA_NON_INTRA_QUANT1: next_advance = 5'd8;             // size of one item of quantization table
836
        /* par. 6.2.3.1: Picture coding extension */
837
        STATE_PICTURE_CODING_EXT:         next_advance = 5'd16;            // size of f_code_00, f_code_01, f_code_10, f_code_11
838
        STATE_PICTURE_CODING_EXT0:        next_advance = 5'd14;            // size of intra_dc_precision .. composite_display_flag
839
        STATE_PICTURE_CODING_EXT1:        next_advance = 5'd20;            // size of v_axis, field_sequence, sub_carrier, burst_amplitude, sub_carrier_phase
840
        /* par. 6.2.2.6: group of pictures header */
841
        STATE_GROUP_HEADER:               next_advance = 5'd19;
842
        STATE_GROUP_HEADER0:              next_advance = 5'd8;
843
        /* par. 6.2.3: picture header */
844
        STATE_PICTURE_HEADER:             next_advance = 5'd13;            // size of temporal_reference, picture_coding_type
845
        STATE_PICTURE_HEADER0:            next_advance = 5'd16;            // size of vbv_delay
846
        STATE_PICTURE_HEADER1:            next_advance = 5'd4;             // size of full_pel_forward_vector, forward_f_code
847
        STATE_PICTURE_HEADER2:            next_advance = 5'd4;             // size of full_pel_backward_vector, backward_f_code
848
 
849
        /* par. 6.2.4: slice */
850
        STATE_SLICE:                      next_advance = 5'd6;
851
        STATE_SLICE_EXTENSION:            next_advance = 5'd9;
852
        STATE_SLICE_EXTRA_INFORMATION:    next_advance = 5'd9;
853
        STATE_NEXT_MACROBLOCK:            next_advance = macroblock_addr_inc_length;
854
        STATE_MACROBLOCK_SKIP:            next_advance = 5'd0;
855
        STATE_MACROBLOCK_TYPE:            next_advance = macroblock_type_length;
856
        STATE_MOTION_TYPE:                next_advance = (macroblock_motion_forward || macroblock_motion_backward) ?
857
                                                          (((picture_structure == FRAME_PICTURE) && (frame_pred_frame_dct == 1'b1)) ? 5'd0 : 5'd2)
858
                                                          : 5'd0;
859
        STATE_DCT_TYPE:                   next_advance = 5'd1;
860
        STATE_MACROBLOCK_QUANT:           next_advance = macroblock_quant ? 5'd5 : 5'd0;
861
 
862
        STATE_NEXT_MOTION_VECTOR:         next_advance = 5'd0;
863
        STATE_MOTION_VERT_FLD_SEL:        next_advance = 5'd1;                         // motion_vertical_field_select
864
        STATE_MOTION_CODE:                next_advance = motion_code_length;           // motion_code
865
        STATE_MOTION_RESIDUAL:            next_advance = r_size;                       // motion_residual
866
        STATE_MOTION_DMVECTOR:            next_advance = dmvector_length;              // dmvector
867
        STATE_MOTION_PREDICT:             next_advance = 5'd0;
868
        STATE_MOTION_PIPELINE_FLUSH:      next_advance = 5'd0;
869
 
870
        STATE_MARKER_BIT_0:               next_advance = 5'd1;
871
        STATE_CODED_BLOCK_PATTERN:        next_advance = coded_block_pattern_length;
872
        STATE_CODED_BLOCK_PATTERN_1:      next_advance = 5'd2;
873
        STATE_CODED_BLOCK_PATTERN_2:      next_advance = 5'd6;
874
        STATE_BLOCK:                      next_advance = 5'd0;
875
        STATE_NEXT_BLOCK:                 next_advance = 5'd0;
876
        STATE_DCT_DC_LUMI_SIZE:           next_advance = dct_dc_size_luminance_length;
877
        STATE_DCT_DC_CHROMI_SIZE:         next_advance = dct_dc_size_chrominance_length;
878
        STATE_DCT_DC_DIFF:                next_advance = dct_dc_size;
879
 
880
        STATE_DCT_SUBS_B15:               next_advance = dct_coefficient_escape ? 5'd12 : dct_coefficient_1_decoded[15:11]; // escape + fixed-length run encoding as in table B-16 or variable length encoding as in table B-15
881
        STATE_DCT_ESCAPE_B15:             next_advance = 5'd12; // 12-bit fixed-length signed_level (table B-16)
882
        STATE_DCT_SUBS_B14:               next_advance = dct_coefficient_escape ? 5'd12 : dct_coefficient_0_decoded[15:11]; // escape + fixed-length run encoding as in table B-16 or variable length encoding as in table B-14
883
        STATE_DCT_ESCAPE_B14      :       next_advance = 5'd12; // 12-bit fixed-length signed_level (table B-16)
884
        STATE_DCT_NON_INTRA_FIRST:        next_advance = dct_coefficient_escape ? 5'd12 : dct_non_intra_first_coefficient_0_decoded[15:11]; // escape + fixed-length run encoding as in table B-16 or variable length encoding as in table B-14, modified as in table b-14 note 2, 3
885
        STATE_NON_CODED_BLOCK:            next_advance = 5'd0;
886
        STATE_DCT_ERROR:                  next_advance = 5'd0;
887
 
888
        STATE_SEQUENCE_END:               next_advance = 5'd0;
889
        STATE_ERROR:                      next_advance = 5'd0;
890
 
891
        /* default value */
892
        default                           next_advance = 5'd0;
893
      endcase
894
 
895
 
896
  /*
897
   * Note: align and advance are zero when clk_en is false;
898
   * this avoids the fifo moving forward while the vld is not enabled.
899
   */
900
 
901
  wire next_align = (state == STATE_NEXT_START_CODE);
902
 
903
  always @(posedge clk)
904
    if (~rst) align <= 1'b0;
905
    else if (clk_en) align <= next_align;
906
    else align <= 1'b0;
907
 
908
  always @(posedge clk)
909
    if (~rst) advance <= 1'b0;
910
    else if (clk_en) advance <= next_advance;
911
    else advance <= 1'b0;
912
 
913
  /*
914
   * wait_state is asserted if align or advance will be non-zero during the next clock cycle,
915
   * and getbits will need to do some work.
916
   * Unregistered output; the registering happens in getbits.
917
   */
918
  output wait_state;
919
  assign wait_state = ((next_align != 1'b0) || (next_advance != 4'b0));
920
 
921
  /* state */
922
 
923
  always @(posedge clk)
924
    if(~rst) state <= STATE_NEXT_START_CODE;
925
    else if (clk_en) state <= next;
926
    else state <= state;
927
 
928
  always @(posedge clk)
929
    if (~rst) dct_error <= 1'b0;
930
    else if (clk_en && (state == STATE_NEXT_START_CODE)) dct_error <= 1'b0;
931
    else if (clk_en && (state == STATE_DCT_ERROR)) dct_error <= 1'b1;
932
    else dct_error <= dct_error;
933
 
934
  always @(posedge clk)
935
    if (~rst) vld_err <= 1'b0;
936
    else if (clk_en && (state == STATE_NEXT_START_CODE)) vld_err <= 1'b0;
937
    else if (clk_en && ((state == STATE_ERROR) || (state == STATE_DCT_ERROR))) vld_err <= 1'b1;
938
    else vld_err <= vld_err;
939
 
940
  /* position in video stream */
941
 
942
  always @(posedge clk)
943
    if (~rst) sequence_header_seen <= 1'b0;
944
    else if (clk_en && (state == STATE_SEQUENCE_HEADER)) sequence_header_seen <= 1'b1;
945
    else if (clk_en && (state == STATE_SEQUENCE_END)) sequence_header_seen <= 1'b0;
946
    else sequence_header_seen <= sequence_header_seen;
947
 
948
  always @(posedge clk)
949
    if (~rst) sequence_extension_seen <= 1'b0;
950
    else if (clk_en && (state == STATE_SEQUENCE_EXT)) sequence_extension_seen <= 1'b1;
951
    else if (clk_en && (state == STATE_SEQUENCE_END)) sequence_extension_seen <= 1'b0;
952
    else sequence_extension_seen <= sequence_extension_seen;
953
 
954
  always @(posedge clk)
955
    if (~rst) picture_header_seen <= 1'b0;
956
    else if (clk_en && (state == STATE_PICTURE_HEADER)) picture_header_seen <= 1'b1;
957
    else if (clk_en && (state == STATE_SEQUENCE_END)) picture_header_seen <= 1'b0;
958
    else picture_header_seen <= picture_header_seen;
959
 
960
  /* par. 6.2.2.1: Sequence header */
961
  loadreg #( .offset(16), .width(8), .fsm_state(STATE_START_CODE))                    loadreg_start_code (.fsm_reg(start_code), .clk(clk), .clk_en(clk_en), .rst(rst), .state(state), .getbits(getbits));
962
  loadreg #( .offset(0), .width(12), .fsm_state(STATE_SEQUENCE_HEADER))               loadreg_horizontal_size_lsb (.fsm_reg(horizontal_size[11:0]), .clk(clk), .clk_en(clk_en), .rst(rst), .state(state), .getbits(getbits));
963
  loadreg #( .offset(0), .width(12), .fsm_state(STATE_SEQUENCE_HEADER0))              loadreg_vertical_size_lsb (.fsm_reg(vertical_size[11:0]), .clk(clk), .clk_en(clk_en), .rst(rst), .state(state), .getbits(getbits));
964
  loadreg #( .offset(0), .width(4), .fsm_state(STATE_SEQUENCE_HEADER1))               loadreg_aspect_ratio_information(.fsm_reg(aspect_ratio_information), .clk(clk), .clk_en(clk_en), .rst(rst), .state(state), .getbits(getbits));
965
  loadreg #( .offset(4), .width(4), .fsm_state(STATE_SEQUENCE_HEADER1))               loadreg_frame_rate_code(.fsm_reg(frame_rate_code), .clk(clk), .clk_en(clk_en), .rst(rst), .state(state), .getbits(getbits));
966
  loadreg #( .offset(0), .width(18), .fsm_state(STATE_SEQUENCE_HEADER2))              loadreg_bit_rate_lsb(.fsm_reg(bit_rate[17:0]), .clk(clk), .clk_en(clk_en), .rst(rst), .state(state), .getbits(getbits));
967
  loadreg #( .offset(0), .width(10), .fsm_state(STATE_SEQUENCE_HEADER3))              loadreg_vbv_buffer_size_lsb(.fsm_reg(vbv_buffer_size[9:0]), .clk(clk), .clk_en(clk_en), .rst(rst), .state(state), .getbits(getbits));
968
  loadreg #( .offset(10), .width(1), .fsm_state(STATE_SEQUENCE_HEADER3))              loadreg_constrained_parameters_flag(.fsm_reg(constrained_parameters_flag), .clk(clk), .clk_en(clk_en), .rst(rst), .state(state), .getbits(getbits));
969
 
970
  /* calculation of picture width in macroblocks , par. 6.3.3 */
971
  always @(posedge clk)
972
    if (~rst) mb_width <= 8'b0;
973
    else if (clk_en) mb_width <= (horizontal_size + 11'd15) >> 4;
974
    else mb_width <= mb_width;
975
 
976
  /* calculation of picture height in macroblocks , par. 6.3.3 */
977
  always @(posedge clk)
978
    if (~rst) mb_height <= 8'b0;
979
    else if (clk_en) mb_height <= progressive_sequence ? ((vertical_size + 11'd15) >> 4)  :
980
                                    ((vertical_size + 11'd31) >> 5) << 1;
981
//                                    ( (picture_structure==FRAME_PICTURE) ? (((vertical_size + 11'd31) >> 5) << 1) : ((vertical_size + 11'd31) >> 5) );  
982
    else mb_height <= mb_height;
983
 
984
  /* Reset quantisation matrices to default values when sequence header code is decoded (par. 6.3.11) */
985
  always @(posedge clk)
986
    if(~rst) quant_rst <= 1'b1;
987
    else if (clk_en) quant_rst <= (state == STATE_SEQUENCE_HEADER);
988
    else quant_rst <= quant_rst;
989
 
990
  always @(posedge clk)
991
    if (~rst) cnt <= 6'b0;
992
    else if (clk_en && ((state == STATE_SEQUENCE_HEADER3) || (state == STATE_QUANT_MATRIX_EXT))) cnt <= 6'h0;
993
    else if (clk_en && ((state == STATE_LD_INTRA_QUANT0)
994
                     || (state == STATE_LD_NON_INTRA_QUANT0)
995
                     || (state == STATE_LD_INTRA_QUANT1)
996
                     || (state == STATE_LD_NON_INTRA_QUANT1)
997
                     || (state == STATE_LD_CHROMA_INTRA_QUANT1)
998
                     || (state == STATE_LD_CHROMA_NON_INTRA_QUANT1))) cnt <= cnt + 1;
999
    else cnt <= cnt;
1000
 
1001
  always @(posedge clk)
1002
    if (~rst) quant_wr_data <= 8'b0;
1003
    else if (clk_en && (state == STATE_LD_INTRA_QUANT0)) quant_wr_data <= getbits[23:16];
1004
    else if (clk_en && (state == STATE_LD_NON_INTRA_QUANT0)) quant_wr_data <= getbits[22:15];
1005
    else if (clk_en && (state == STATE_LD_INTRA_QUANT1)) quant_wr_data <= getbits[22:15];
1006
    else if (clk_en && (state == STATE_LD_NON_INTRA_QUANT1)) quant_wr_data <= getbits[21:14];
1007
    else if (clk_en && (state == STATE_LD_CHROMA_INTRA_QUANT1)) quant_wr_data <= getbits[20:13];
1008
    else if (clk_en && (state == STATE_LD_CHROMA_NON_INTRA_QUANT1)) quant_wr_data <= getbits[19:12];
1009
    else quant_wr_data <= quant_wr_data;
1010
 
1011
  always @(posedge clk)
1012
    if (~rst) quant_wr_addr <= 6'b0;
1013
    else if (clk_en) quant_wr_addr <= cnt;
1014
    else quant_wr_addr <= quant_wr_addr;
1015
 
1016
  always @(posedge clk)
1017
    if (~rst) wr_intra_quant <= 1'b0;
1018
    else if (clk_en) wr_intra_quant <= ((state == STATE_LD_INTRA_QUANT0) || (state == STATE_LD_INTRA_QUANT1));
1019
    else wr_intra_quant <= wr_intra_quant;
1020
 
1021
  always @(posedge clk)
1022
    if (~rst) wr_non_intra_quant <= 1'b0;
1023
    else if (clk_en) wr_non_intra_quant <= ((state == STATE_LD_NON_INTRA_QUANT0) || (state == STATE_LD_NON_INTRA_QUANT1));
1024
    else wr_non_intra_quant <= wr_non_intra_quant;
1025
 
1026
  always @(posedge clk)
1027
    if (~rst) wr_chroma_intra_quant <= 1'b0;
1028
    else if (clk_en) wr_chroma_intra_quant <= ((state == STATE_LD_INTRA_QUANT0) || (state == STATE_LD_INTRA_QUANT1) || (state == STATE_LD_CHROMA_INTRA_QUANT1));
1029
    else wr_chroma_intra_quant <= wr_chroma_intra_quant;
1030
 
1031
  always @(posedge clk)
1032
    if (~rst) wr_chroma_non_intra_quant <= 1'b0;
1033
    else if (clk_en) wr_chroma_non_intra_quant <= ((state == STATE_LD_NON_INTRA_QUANT0) || (state == STATE_LD_NON_INTRA_QUANT1) || (state == STATE_LD_CHROMA_NON_INTRA_QUANT1));
1034
    else wr_chroma_non_intra_quant <= wr_chroma_non_intra_quant;
1035
 
1036
  /* rld fifo interface */
1037
  always @(posedge clk)
1038
    if (~rst) rld_wr_en <= 1'b0;
1039
    else if (clk_en) rld_wr_en <= dct_coeff_valid_0 ||
1040
                                  ((state == STATE_SEQUENCE_HEADER) ||               // quant_rst
1041
                                   (state == STATE_LD_INTRA_QUANT0) ||               // wr_intra_quant, wr_chroma_intra_quant
1042
                                   (state == STATE_LD_INTRA_QUANT1) ||               // wr_intra_quant, wr_chroma_intra_quant
1043
                                   (state == STATE_LD_NON_INTRA_QUANT0) ||           // wr_non_intra_quant, wr_chroma_non_intra_quant
1044
                                   (state == STATE_LD_NON_INTRA_QUANT1) ||           // wr_non_intra_quant, wr_chroma_non_intra_quant
1045
                                   (state == STATE_LD_CHROMA_INTRA_QUANT1) ||        // wr_chroma_intra_quant
1046
                                   (state == STATE_LD_CHROMA_NON_INTRA_QUANT1));     // wr_chroma_non_intra_quant
1047
    else rld_wr_en <= 1'b0;
1048
 
1049
  always @(posedge clk)
1050
    if (~rst) rld_cmd <= 1'b0;
1051
    else if (clk_en) rld_cmd <= ( dct_coeff_valid_0 ? RLD_DCT :
1052
                                  ((state == STATE_SEQUENCE_HEADER) ||               // quant_rst
1053
                                   (state == STATE_LD_INTRA_QUANT0) ||               // wr_intra_quant, wr_chroma_intra_quant
1054
                                   (state == STATE_LD_INTRA_QUANT1) ||               // wr_intra_quant, wr_chroma_intra_quant
1055
                                   (state == STATE_LD_NON_INTRA_QUANT0) ||           // wr_non_intra_quant, wr_chroma_non_intra_quant
1056
                                   (state == STATE_LD_NON_INTRA_QUANT1) ||           // wr_non_intra_quant, wr_chroma_non_intra_quant
1057
                                   (state == STATE_LD_CHROMA_INTRA_QUANT1) ||        // wr_chroma_intra_quant
1058
                                   (state == STATE_LD_CHROMA_NON_INTRA_QUANT1))      // wr_chroma_non_intra_quant
1059
                                  ? RLD_QUANT : RLD_NOOP);
1060
    else rld_cmd <= rld_cmd;
1061
 
1062
  /* par. 6.2.2.3: Sequence extension */
1063
  loadreg #( .offset(0), .width(8), .fsm_state(STATE_SEQUENCE_EXT))                  loadreg_profile_and_level_indication(.fsm_reg(profile_and_level_indication), .clk(clk), .clk_en(clk_en), .rst(rst), .state(state), .getbits(getbits));
1064
  loadreg #( .offset(8), .width(1), .fsm_state(STATE_SEQUENCE_EXT))                  loadreg_progressive_sequence(.fsm_reg(progressive_sequence), .clk(clk), .clk_en(clk_en), .rst(rst), .state(state), .getbits(getbits));
1065
  loadreg #( .offset(9), .width(2), .fsm_state(STATE_SEQUENCE_EXT))                  loadreg_chroma_format(.fsm_reg(chroma_format), .clk(clk), .clk_en(clk_en), .rst(rst), .state(state), .getbits(getbits));
1066
  loadreg #( .offset(11), .width(2), .fsm_state(STATE_SEQUENCE_EXT))                 loadreg_horizontal_size_msb(.fsm_reg(horizontal_size[13:12]), .clk(clk), .clk_en(clk_en), .rst(rst), .state(state), .getbits(getbits));
1067
  loadreg #( .offset(13), .width(2), .fsm_state(STATE_SEQUENCE_EXT))                 loadreg_vertical_size_msb(.fsm_reg(vertical_size[13:12]), .clk(clk), .clk_en(clk_en), .rst(rst), .state(state), .getbits(getbits));
1068
  loadreg #( .offset(0), .width(12), .fsm_state(STATE_SEQUENCE_EXT0))                loadreg_bit_rate_msb(.fsm_reg(bit_rate[29:18]), .clk(clk), .clk_en(clk_en), .rst(rst), .state(state), .getbits(getbits));
1069
  loadreg #( .offset(0), .width(8), .fsm_state(STATE_SEQUENCE_EXT1))                 loadreg_vbv_buffer_size_msb(.fsm_reg(vbv_buffer_size[17:10]), .clk(clk), .clk_en(clk_en), .rst(rst), .state(state), .getbits(getbits));
1070
  loadreg #( .offset(8), .width(1), .fsm_state(STATE_SEQUENCE_EXT1))                 loadreg_low_delay(.fsm_reg(low_delay), .clk(clk), .clk_en(clk_en), .rst(rst), .state(state), .getbits(getbits));
1071
  loadreg #( .offset(9), .width(2), .fsm_state(STATE_SEQUENCE_EXT1))                 loadreg_frame_rate_extension_n(.fsm_reg(frame_rate_extension_n), .clk(clk), .clk_en(clk_en), .rst(rst), .state(state), .getbits(getbits));
1072
  loadreg #( .offset(11), .width(5), .fsm_state(STATE_SEQUENCE_EXT1))                loadreg_frame_rate_extension_d(.fsm_reg(frame_rate_extension_d), .clk(clk), .clk_en(clk_en), .rst(rst), .state(state), .getbits(getbits));
1073
 
1074
  /* par. 6.2.2.4: Sequence display extension */
1075
  loadreg #( .offset(0), .width(3), .fsm_state(STATE_SEQUENCE_DISPLAY_EXT))          loadreg_video_format(.fsm_reg(video_format), .clk(clk), .clk_en(clk_en), .rst(rst), .state(state), .getbits(getbits));
1076
  loadreg #( .offset(0), .width(8), .fsm_state(STATE_SEQUENCE_DISPLAY_EXT0))         loadreg_colour_primaries(.fsm_reg(colour_primaries), .clk(clk), .clk_en(clk_en), .rst(rst), .state(state), .getbits(getbits));
1077
  loadreg #( .offset(8), .width(8), .fsm_state(STATE_SEQUENCE_DISPLAY_EXT0))         loadreg_transfer_characteristics(.fsm_reg(transfer_characteristics), .clk(clk), .clk_en(clk_en), .rst(rst), .state(state), .getbits(getbits));
1078
  loadreg #( .offset(16), .width(8), .fsm_state(STATE_SEQUENCE_DISPLAY_EXT0))        loadreg_matrix_coefficients(.fsm_reg(matrix_coefficients), .clk(clk), .clk_en(clk_en), .rst(rst), .state(state), .getbits(getbits));
1079
  loadreg #( .offset(0), .width(14), .fsm_state(STATE_SEQUENCE_DISPLAY_EXT1))        loadreg_display_horizontal_size(.fsm_reg(display_horizontal_size), .clk(clk), .clk_en(clk_en), .rst(rst), .state(state), .getbits(getbits));
1080
  loadreg #( .offset(0), .width(14), .fsm_state(STATE_SEQUENCE_DISPLAY_EXT2))        loadreg_display_vertical_size(.fsm_reg(display_vertical_size), .clk(clk), .clk_en(clk_en), .rst(rst), .state(state), .getbits(getbits));
1081
 
1082
  /* par. 6.2.3.1: Picture coding extension */
1083
  loadreg #( .offset(0), .width(4), .fsm_state(STATE_PICTURE_CODING_EXT))            loadreg_f_code_00(.fsm_reg(f_code_00), .clk(clk), .clk_en(clk_en), .rst(rst), .state(state), .getbits(getbits));
1084
  loadreg #( .offset(4), .width(4), .fsm_state(STATE_PICTURE_CODING_EXT))            loadreg_f_code_01(.fsm_reg(f_code_01), .clk(clk), .clk_en(clk_en), .rst(rst), .state(state), .getbits(getbits));
1085
  loadreg #( .offset(8), .width(4), .fsm_state(STATE_PICTURE_CODING_EXT))            loadreg_f_code_10(.fsm_reg(f_code_10), .clk(clk), .clk_en(clk_en), .rst(rst), .state(state), .getbits(getbits));
1086
  loadreg #( .offset(12), .width(4), .fsm_state(STATE_PICTURE_CODING_EXT))           loadreg_f_code_11(.fsm_reg(f_code_11), .clk(clk), .clk_en(clk_en), .rst(rst), .state(state), .getbits(getbits));
1087
  loadreg #( .offset(0), .width(2), .fsm_state(STATE_PICTURE_CODING_EXT0))           loadreg_intra_dc_precision(.fsm_reg(intra_dc_precision), .clk(clk), .clk_en(clk_en), .rst(rst), .state(state), .getbits(getbits));
1088
  loadreg #( .offset(2), .width(2), .fsm_state(STATE_PICTURE_CODING_EXT0))           loadreg_picture_structure(.fsm_reg(picture_structure), .clk(clk), .clk_en(clk_en), .rst(rst), .state(state), .getbits(getbits));
1089
  loadreg #( .offset(4), .width(1), .fsm_state(STATE_PICTURE_CODING_EXT0))           loadreg_top_field_first(.fsm_reg(top_field_first), .clk(clk), .clk_en(clk_en), .rst(rst), .state(state), .getbits(getbits));
1090
  loadreg #( .offset(5), .width(1), .fsm_state(STATE_PICTURE_CODING_EXT0))           loadreg_frame_pred_frame_dct(.fsm_reg(frame_pred_frame_dct), .clk(clk), .clk_en(clk_en), .rst(rst), .state(state), .getbits(getbits));
1091
  loadreg #( .offset(6), .width(1), .fsm_state(STATE_PICTURE_CODING_EXT0))           loadreg_concealment_motion_vectors(.fsm_reg(concealment_motion_vectors), .clk(clk), .clk_en(clk_en), .rst(rst), .state(state), .getbits(getbits));
1092
  loadreg #( .offset(7), .width(1), .fsm_state(STATE_PICTURE_CODING_EXT0))           loadreg_q_scale_type(.fsm_reg(q_scale_type), .clk(clk), .clk_en(clk_en), .rst(rst), .state(state), .getbits(getbits));
1093
  loadreg #( .offset(8), .width(1), .fsm_state(STATE_PICTURE_CODING_EXT0))           loadreg_intra_vlc_format(.fsm_reg(intra_vlc_format), .clk(clk), .clk_en(clk_en), .rst(rst), .state(state), .getbits(getbits));
1094
  loadreg #( .offset(9), .width(1), .fsm_state(STATE_PICTURE_CODING_EXT0))           loadreg_alternate_scan(.fsm_reg(alternate_scan), .clk(clk), .clk_en(clk_en), .rst(rst), .state(state), .getbits(getbits));
1095
  loadreg #( .offset(10), .width(1), .fsm_state(STATE_PICTURE_CODING_EXT0))          loadreg_repeat_first_field(.fsm_reg(repeat_first_field), .clk(clk), .clk_en(clk_en), .rst(rst), .state(state), .getbits(getbits));
1096
  loadreg #( .offset(11), .width(1), .fsm_state(STATE_PICTURE_CODING_EXT0))          loadreg_chroma_420_type(.fsm_reg(chroma_420_type), .clk(clk), .clk_en(clk_en), .rst(rst), .state(state), .getbits(getbits));
1097
  loadreg #( .offset(12), .width(1), .fsm_state(STATE_PICTURE_CODING_EXT0))          loadreg_progressive_frame(.fsm_reg(progressive_frame), .clk(clk), .clk_en(clk_en), .rst(rst), .state(state), .getbits(getbits));
1098
  loadreg #( .offset(13), .width(1), .fsm_state(STATE_PICTURE_CODING_EXT0))          loadreg_composite_display_flag(.fsm_reg(composite_display_flag), .clk(clk), .clk_en(clk_en), .rst(rst), .state(state), .getbits(getbits));
1099
  loadreg #( .offset(0), .width(1), .fsm_state(STATE_PICTURE_CODING_EXT1))           loadreg_v_axis(.fsm_reg(v_axis), .clk(clk), .clk_en(clk_en), .rst(rst), .state(state), .getbits(getbits));
1100
  loadreg #( .offset(1), .width(3), .fsm_state(STATE_PICTURE_CODING_EXT1))           loadreg_field_sequence(.fsm_reg(field_sequence), .clk(clk), .clk_en(clk_en), .rst(rst), .state(state), .getbits(getbits));
1101
  loadreg #( .offset(4), .width(1), .fsm_state(STATE_PICTURE_CODING_EXT1))           loadreg_sub_carrier(.fsm_reg(sub_carrier), .clk(clk), .clk_en(clk_en), .rst(rst), .state(state), .getbits(getbits));
1102
  loadreg #( .offset(5), .width(7), .fsm_state(STATE_PICTURE_CODING_EXT1))           loadreg_burst_amplitude(.fsm_reg(burst_amplitude), .clk(clk), .clk_en(clk_en), .rst(rst), .state(state), .getbits(getbits));
1103
  loadreg #( .offset(12), .width(8), .fsm_state(STATE_PICTURE_CODING_EXT1))          loadreg_sub_carrier_phase(.fsm_reg(sub_carrier_phase), .clk(clk), .clk_en(clk_en), .rst(rst), .state(state), .getbits(getbits));
1104
 
1105
  /* par. 6.2.2.6: group of pictures header */
1106
  loadreg #( .offset(0), .width(1), .fsm_state(STATE_GROUP_HEADER))                  loadreg_drop_flag(.fsm_reg(drop_flag), .clk(clk), .clk_en(clk_en), .rst(rst), .state(state), .getbits(getbits));
1107
  loadreg #( .offset(1), .width(5), .fsm_state(STATE_GROUP_HEADER))                  loadreg_time_code_hours(.fsm_reg(time_code_hours), .clk(clk), .clk_en(clk_en), .rst(rst), .state(state), .getbits(getbits));
1108
  loadreg #( .offset(6), .width(6), .fsm_state(STATE_GROUP_HEADER))                  loadreg_time_code_minutes(.fsm_reg(time_code_minutes), .clk(clk), .clk_en(clk_en), .rst(rst), .state(state), .getbits(getbits));
1109
  loadreg #( .offset(13), .width(6), .fsm_state(STATE_GROUP_HEADER))                 loadreg_time_code_seconds(.fsm_reg(time_code_seconds), .clk(clk), .clk_en(clk_en), .rst(rst), .state(state), .getbits(getbits));
1110
  loadreg #( .offset(0), .width(6), .fsm_state(STATE_GROUP_HEADER0))                 loadreg_time_code_pictures(.fsm_reg(time_code_pictures), .clk(clk), .clk_en(clk_en), .rst(rst), .state(state), .getbits(getbits));
1111
  loadreg #( .offset(6), .width(1), .fsm_state(STATE_GROUP_HEADER0))                 loadreg_closed_gop(.fsm_reg(closed_gop), .clk(clk), .clk_en(clk_en), .rst(rst), .state(state), .getbits(getbits));
1112
  loadreg #( .offset(7), .width(1), .fsm_state(STATE_GROUP_HEADER0))                 loadreg_broken_link(.fsm_reg(broken_link), .clk(clk), .clk_en(clk_en), .rst(rst), .state(state), .getbits(getbits));
1113
 
1114
  /* par. 6.2.3: picture header */
1115
  loadreg #( .offset(0), .width(10), .fsm_state(STATE_PICTURE_HEADER))               loadreg_temporal_reference(.fsm_reg(temporal_reference), .clk(clk), .clk_en(clk_en), .rst(rst), .state(state), .getbits(getbits));
1116
  loadreg #( .offset(10), .width(3), .fsm_state(STATE_PICTURE_HEADER))               loadreg_picture_coding_type(.fsm_reg(picture_coding_type), .clk(clk), .clk_en(clk_en), .rst(rst), .state(state), .getbits(getbits));
1117
  loadreg #( .offset(0), .width(16), .fsm_state(STATE_PICTURE_HEADER0))              loadreg_vbv_delay(.fsm_reg(vbv_delay), .clk(clk), .clk_en(clk_en), .rst(rst), .state(state), .getbits(getbits));
1118
  loadreg #( .offset(0), .width(1), .fsm_state(STATE_PICTURE_HEADER1))               loadreg_full_pel_forward_vector(.fsm_reg(full_pel_forward_vector), .clk(clk), .clk_en(clk_en), .rst(rst), .state(state), .getbits(getbits));
1119
  loadreg #( .offset(1), .width(3), .fsm_state(STATE_PICTURE_HEADER1))               loadreg_forward_f_code(.fsm_reg(forward_f_code), .clk(clk), .clk_en(clk_en), .rst(rst), .state(state), .getbits(getbits));
1120
  loadreg #( .offset(0), .width(1), .fsm_state(STATE_PICTURE_HEADER2))               loadreg_full_pel_backward_vector(.fsm_reg(full_pel_backward_vector), .clk(clk), .clk_en(clk_en), .rst(rst), .state(state), .getbits(getbits));
1121
  loadreg #( .offset(1), .width(3), .fsm_state(STATE_PICTURE_HEADER2))               loadreg_backward_f_code(.fsm_reg(backward_f_code), .clk(clk), .clk_en(clk_en), .rst(rst), .state(state), .getbits(getbits));
1122
 
1123
  /* par. 6.2.4: slice */
1124
  /* macroblock address increment vlc lookup */
1125
  assign {macroblock_addr_inc_length, macroblock_addr_inc_value, macroblock_addr_inc_escape} = macroblock_address_increment_dec(getbits[23:13]);
1126
 
1127
  /* macroblock type vlc lookup */
1128
  assign {macroblock_type_length, macroblock_type_value} = macroblock_type_dec(getbits[23:18], picture_coding_type);
1129
 
1130
  /* coded block pattern vlc lookup */
1131
  assign {coded_block_pattern_length, coded_block_pattern_value} = coded_block_pattern_dec(getbits[23:15]);
1132
 
1133
  /* motion code vlc lookup */
1134
  assign {motion_code_length, motion_code_value, motion_code_sign} = motion_code_dec(getbits[23:13]);
1135
 
1136
  /* dmvector vlc lookup */
1137
  assign {dmvector_length, dmvector_value, dmvector_sign} = dmvector_dec(getbits[23:22]);
1138
 
1139
  /* dct dc size luminance vlc lookup */
1140
  assign {dct_dc_size_luminance_length, dct_dc_size_luminance_value} = dct_dc_size_luminance_dec(getbits[23:15]);
1141
 
1142
  /* dct dc size chrominance vlc lookup */
1143
  assign {dct_dc_size_chrominance_length, dct_dc_size_chrominance_value} = dct_dc_size_chrominance_dec(getbits[23:14]);
1144
 
1145
  /* dct coefficient 0 vlc lookup */
1146
  always @(getbits)
1147
    dct_coefficient_0_decoded = dct_coefficient_0_dec(getbits[23:8]);
1148
 
1149
  /* dct first coefficient 0 vlc lookup */
1150
  /* see note 2 and 3 of table B-14: first coefficient handled differently. */
1151
  /* Code 2'b10 = 1, Code 2'b11 = -1 */
1152
  always @(getbits, dct_coefficient_0_decoded)
1153
    dct_non_intra_first_coefficient_0_decoded = getbits[23] ? {5'd2, 5'd0, 6'd1} : dct_coefficient_0_decoded;
1154
 
1155
  /* dct coefficient 1 vlc lookup */
1156
  always @(getbits)
1157
    dct_coefficient_1_decoded = dct_coefficient_1_dec(getbits[23:8]);
1158
 
1159
  /* internal registers */
1160
  /* slice start code */
1161
  /* this does not take into account slice_vertial_position_extension (par.  6.3.16). This limits vertical resolution to 2800 lines. */
1162
  always @(posedge clk)
1163
    if (~rst) slice_vertical_position <= 8'b0;
1164
    else if (clk_en && (state == STATE_SLICE)) slice_vertical_position <= start_code;
1165
    else slice_vertical_position <= slice_vertical_position;
1166
 
1167
  /* slice quantiser scale */
1168
  always @(posedge clk)
1169
    if (~rst) quantiser_scale_code <= 5'b0;
1170
    else if (clk_en && ((state == STATE_SLICE) || ((state == STATE_MACROBLOCK_QUANT) && macroblock_quant))) quantiser_scale_code <= getbits[23:19];
1171
    else quantiser_scale_code <= quantiser_scale_code;
1172
 
1173
  always @(posedge clk)
1174
    if (~rst) slice_extension_flag <= 1'b0;
1175
    else if (clk_en && (state == STATE_SLICE)) slice_extension_flag <= getbits[18];
1176
    else slice_extension_flag <= slice_extension_flag;
1177
 
1178
  /* slice extension */
1179
  always @(posedge clk)
1180
    if (~rst) intra_slice <= 1'b0;
1181
    else if (clk_en && (state == STATE_SLICE_EXTENSION)) intra_slice <= getbits[23];
1182
    else intra_slice <= intra_slice;
1183
 
1184
  always @(posedge clk)
1185
    if (~rst) slice_picture_id_enable <= 1'b0;
1186
    else if (clk_en && (state == STATE_SLICE_EXTENSION)) slice_picture_id_enable <= getbits[22];
1187
    else slice_picture_id_enable <= slice_picture_id_enable;
1188
 
1189
  always @(posedge clk)
1190
    if (~rst) slice_picture_id <= 6'b0;
1191
    else if (clk_en && (state == STATE_SLICE_EXTENSION)) slice_picture_id <= getbits[21:16];
1192
    else slice_picture_id <= slice_picture_id;
1193
 
1194
  /* macroblock address */
1195
  /*
1196
   * Note macroblock address calculation is for the restricted slice structure (par. 6.1.2.2) where slices seamlessly cover the whole picture.
1197
   * Support for the general slice structure (par. 6.1.2.1) with gaps between slices could be added if needed.
1198
   */
1199
 
1200
  always @(posedge clk)
1201
    if (~rst) first_macroblock_of_slice <= 1'b0;
1202
    else if (clk_en && (state == STATE_SLICE)) first_macroblock_of_slice <= 1'b1;
1203
    else if (clk_en && (state == STATE_MACROBLOCK_TYPE)) first_macroblock_of_slice <= 1'b0;
1204
    else first_macroblock_of_slice <= first_macroblock_of_slice;
1205
 
1206
  reg [15:0]mb_row_by_mb_width;
1207
  wire [7:0]mb_row = start_code - 8'd1;
1208
 
1209
  always @(posedge clk)
1210
    if (~rst) mb_row_by_mb_width <= 13'b0;
1211
    else if (clk_en && (state == STATE_SLICE)) mb_row_by_mb_width <= mb_row * mb_width;
1212
    else mb_row_by_mb_width <= mb_row_by_mb_width;
1213
 
1214
  /*
1215
   * no skipped macroblocks allowed at slice start. At slice start:
1216
   * macroblock_address <= ( start_code - 1 ) * mb_width + macroblock_address_increment - 1;
1217
   * macroblock_address_increment <= 1;
1218
   */
1219
 
1220
  wire [15:0]macroblock_address_increment_ext = macroblock_address_increment;
1221
  wire [15:0]next_macroblock_address = mb_row_by_mb_width + macroblock_address_increment_ext - 16'd1;
1222
 
1223
  always @(posedge clk)
1224
    if (~rst) macroblock_address <= 13'b0;
1225
    else if (clk_en && (state == STATE_MACROBLOCK_TYPE) && first_macroblock_of_slice) macroblock_address <= next_macroblock_address[12:0]; // macroblock in 1920x1080 are numbered 0..8159, hence 13 bits.
1226
    else if (clk_en && ((state == STATE_MACROBLOCK_TYPE) || (state == STATE_MACROBLOCK_SKIP))) macroblock_address <= macroblock_address + 13'b1;
1227
    else macroblock_address <= macroblock_address;
1228
 
1229
  always @(posedge clk)
1230
    if (~rst) macroblock_address_increment <= 7'b0;
1231
    else if (clk_en && ((state == STATE_SLICE) || (state == STATE_NEXT_BLOCK))) macroblock_address_increment <= 7'd0;
1232
    else if (clk_en && (state == STATE_NEXT_MACROBLOCK) && macroblock_addr_inc_escape) macroblock_address_increment <= macroblock_address_increment + 7'd33; // par. 6.3.17, macroblock_escape
1233
    else if (clk_en && (state == STATE_NEXT_MACROBLOCK)) macroblock_address_increment <=  macroblock_address_increment + macroblock_addr_inc_value_ext;
1234
    else if (clk_en && (state == STATE_MACROBLOCK_SKIP)) macroblock_address_increment <= macroblock_address_increment - 7'd1; // counting down number of macroblocks to skip
1235
    else macroblock_address_increment <= macroblock_address_increment;
1236
 
1237
  always @(posedge clk)
1238
    if (~rst) empty_blocks <= 11'b0;
1239
    else if (clk_en && (state == STATE_MACROBLOCK_SKIP))
1240
      begin
1241
        case (chroma_format)
1242
          CHROMA420: empty_blocks <= {11'b11111000000};  // emit 6 empty (all-zeroes) blocks for 4:2:0
1243
          CHROMA422: empty_blocks <= {11'b11111110000};  // emit 8 empty (all-zeroes) blocks for 4:2:2
1244
          CHROMA444: empty_blocks <= {11'b11111111111};  // emit 12 empty (all-zeroes) blocks for 4:4:4
1245
          default    empty_blocks <= {11'b00000000000};  // error
1246
        endcase
1247
      end
1248
    else if (clk_en && (state == STATE_EMIT_EMPTY_BLOCK)) empty_blocks <= empty_blocks << 1;
1249
    else empty_blocks <= empty_blocks;
1250
 
1251
  /* macroblock type */
1252
  always @(posedge clk)
1253
    if (~rst) macroblock_quant <= 1'b0;
1254
    else if (clk_en && (state == STATE_MACROBLOCK_TYPE)) macroblock_quant <= macroblock_type_value[5];
1255
    else macroblock_quant <= macroblock_quant;
1256
 
1257
  always @(posedge clk)
1258
    if (~rst) macroblock_motion_forward <= 1'b0;
1259
    else if (clk_en && (state == STATE_MACROBLOCK_TYPE)) macroblock_motion_forward <= macroblock_type_value[4];
1260
    else macroblock_motion_forward <= macroblock_motion_forward;
1261
 
1262
  always @(posedge clk)
1263
    if (~rst) macroblock_motion_backward <= 1'b0;
1264
    else if (clk_en && (state == STATE_MACROBLOCK_TYPE)) macroblock_motion_backward <= macroblock_type_value[3];
1265
    else macroblock_motion_backward <= macroblock_motion_backward;
1266
 
1267
  always @(posedge clk)
1268
    if (~rst) macroblock_pattern <= 1'b0;
1269
    else if (clk_en && (state == STATE_MACROBLOCK_TYPE)) macroblock_pattern <= macroblock_type_value[2];
1270
    else macroblock_pattern <= macroblock_pattern;
1271
 
1272
  /*
1273
   * macroblock_type_intra is asserted when macroblock_type indicates this macroblock is an intra macroblock.
1274
   * Intra macroblocks are fully coded. Skipped macroblocks are fully predicted.
1275
   * However, skipped macroblocks are never intra macroblocks.
1276
   * Hence macroblock_intra is low when we're emitting a skipped macroblock,
1277
   * even if the macroblock being decoded is an intra macroblock.
1278
   * If we're not emitting a skipped macroblock, macroblock_intra has the same
1279
   * value as macroblock_type_intra.
1280
   */
1281
 
1282
  always @(posedge clk)
1283
    if (~rst) macroblock_type_intra <= 1'b0;
1284
    else if (clk_en && (state == STATE_MACROBLOCK_TYPE)) macroblock_type_intra <= macroblock_type_value[1];
1285
    else macroblock_type_intra <= macroblock_type_intra;
1286
 
1287
  always @(posedge clk)
1288
    if (~rst) macroblock_intra <= 1'b0;
1289
    else if (clk_en && (state == STATE_MACROBLOCK_TYPE)) macroblock_intra <= macroblock_type_value[1];
1290
    else if (clk_en && (state == STATE_MACROBLOCK_SKIP)) macroblock_intra <= 1'b0;
1291
    else if (clk_en) macroblock_intra <= macroblock_type_intra;
1292
    else macroblock_intra <= macroblock_intra;
1293
 
1294
  always @(posedge clk)
1295
    if (~rst) spatial_temporal_weight_code_flag <= 1'b0;
1296
    else if (clk_en && (state == STATE_MACROBLOCK_TYPE)) spatial_temporal_weight_code_flag <= macroblock_type_value[0];
1297
    else spatial_temporal_weight_code_flag <= spatial_temporal_weight_code_flag;
1298
 
1299
  /* motion type */
1300
  always @(posedge clk)
1301
    // par. 6.3.17.1: In this case motion vector decoding and prediction formation shall be performed as if frame_motion_type had indicated "Frame-based prediction".
1302
    // 2 == Frame-based prediction.
1303
    // par. 6.3.17.1: In the case of intra macroblocks (in a field picture) when concealment_motion_vectors is equal to 1 field_motion_type is not present in the bitstream.
1304
    // In this case, motion vector decoding and update of the motion vector predictors shall be performed as if field_motion_type had indicated "Field-based"
1305
    // 2'b1 == Field-based prediction.
1306
    if (~rst) motion_type <= 2'd0;
1307
    else if (clk_en && (state == STATE_MOTION_TYPE) && (macroblock_motion_forward || macroblock_motion_backward)  && (picture_structure == FRAME_PICTURE)) motion_type <= frame_pred_frame_dct ? MC_FRAME : getbits[23:22]; /* frame_motion_type */
1308
    else if (clk_en && (state == STATE_MOTION_TYPE) && (macroblock_motion_forward || macroblock_motion_backward)) motion_type <= getbits[23:22]; /* field_motion_type */
1309
    else if (clk_en && (state == STATE_MOTION_TYPE) && (macroblock_intra && concealment_motion_vectors)) motion_type <= (picture_structure==FRAME_PICTURE) ? MC_FRAME : MC_FIELD; /* concealment motion vectors */
1310
    else if (clk_en && (state == STATE_MOTION_TYPE)) motion_type <= MC_NONE;
1311
    else if (clk_en && (state == STATE_MACROBLOCK_SKIP)) motion_type <= (picture_structure == FRAME_PICTURE) ? MC_FRAME : MC_FIELD; // par. 7.6.6 the prediction shall be made as if (field|frame)_motion_type is "Field-based"|"Frame-based"
1312
    else motion_type <= motion_type;
1313
 
1314
  /* dct type */
1315
  /*
1316
   dct_type is a flag indicating whether the macroblock is frame DCT coded or field DCT coded. If this is set to '1', the macroblock is field DCT coded.
1317
   In the case that dct_type is not present in the bitstream, then the value of dct_type (used in the remainder of the decoding process) shall be derived as shown in Table 6-19.
1318
   (par. 6.3.17.1, Macroblock modes)
1319
   */
1320
 
1321
  always @(posedge clk)
1322
    if (~rst) dct_type <= 1'b0;
1323
    else if (clk_en && (state == STATE_MOTION_TYPE)) dct_type <= 1'b0;
1324
    else if (clk_en && (state == STATE_DCT_TYPE)) dct_type <= getbits[23];
1325
    else dct_type <= dct_type;
1326
 
1327
  // derived variables (par. 6.3.17.1, tables 6.17 and 6.18)
1328
  // note  spatial_temporal_weight is always zero, we don't do scaleability.
1329
  assign motion_vector_count_is_one = (picture_structure==FRAME_PICTURE) ? (motion_type != MC_FIELD) : (motion_type != MC_16X8);
1330
 
1331
  always @(posedge clk)
1332
    if (~rst) mv_format <= 2'b0;
1333
    else if (clk_en) mv_format <= (picture_structure==FRAME_PICTURE) ? ((motion_type==MC_FRAME) ? MV_FRAME : MV_FIELD) : MV_FIELD;
1334
    else mv_format <= mv_format;
1335
 
1336
  always @(posedge clk)
1337
    if (~rst) dmv <= 1'b0;
1338
    else if (clk_en) dmv <= (motion_type==MC_DMV);
1339
    else dmv <= dmv;
1340
 
1341
  /* motion vectors */
1342
 
1343
  /* motion_vectors(0), motion_vertical_field_select(0,0) */
1344
  /* par. 6.3.17.3: The number of bits in the bitstream for motion_residual[r][s][t], r_size, is derived from f_code[s][t] as follows:
1345
   * r_size = f_code[s][t] - 1;
1346
   */
1347
  always @(posedge clk)
1348
    if (~rst) r_size <= 4'd0;
1349
    else if (clk_en && (state == STATE_NEXT_MOTION_VECTOR) && ((motion_vector == MOTION_VECTOR_0_0_0) || (motion_vector == MOTION_VECTOR_1_0_0))) r_size <= f_code_00 - 4'd1;
1350
    else if (clk_en && (state == STATE_NEXT_MOTION_VECTOR) && ((motion_vector == MOTION_VECTOR_0_0_1) || (motion_vector == MOTION_VECTOR_1_0_1))) r_size <= f_code_01 - 4'd1;
1351
    else if (clk_en && (state == STATE_NEXT_MOTION_VECTOR) && ((motion_vector == MOTION_VECTOR_0_1_0) || (motion_vector == MOTION_VECTOR_1_1_0))) r_size <= f_code_10 - 4'd1;
1352
    else if (clk_en && (state == STATE_NEXT_MOTION_VECTOR) && ((motion_vector == MOTION_VECTOR_0_1_1) || (motion_vector == MOTION_VECTOR_1_1_1))) r_size <= f_code_11 - 4'd1;
1353
    else r_size <= r_size;
1354
 
1355
  // vertical_field_select_present - one if motion_vector(s) begins with a 'motion_vertical_field_select'.
1356
  assign vertical_field_select_present = (motion_vector_count_is_one ? ( (mv_format == MV_FIELD) && (dmv != 1'b1) ) : 1'd1 )
1357
                                         && ((motion_vector == MOTION_VECTOR_0_0_0) || (motion_vector == MOTION_VECTOR_1_0_0) ||
1358
                                             (motion_vector == MOTION_VECTOR_0_1_0) || (motion_vector == MOTION_VECTOR_1_1_0)); // par. 6.2.5.1
1359
 
1360
  /* motion code variables, par. 6.2.5, 6.2.5.2, 6.2.5.2.1 */
1361
  /*
1362
   * motion_vector cycles through the different motion vectors MOTION_VECTOR_0_0_0 ... MOTION_VECTOR_1_1_1
1363
   * The msb of  motion_vector_reg is one if the motion vector actually occurs in the bitstream.
1364
   */
1365
  always @(posedge clk)
1366
    if (~rst) motion_vector <= 3'b0;
1367
    else if (clk_en && (state == STATE_MACROBLOCK_QUANT)) motion_vector <= MOTION_VECTOR_0_0_0;
1368
    else if (clk_en && (state == STATE_MOTION_PREDICT)) motion_vector <= motion_vector + 1;
1369
    else if (clk_en && (state == STATE_NEXT_MOTION_VECTOR) && (motion_vector_reg[7] == 1'b0)) motion_vector <= motion_vector + 1;
1370
    else motion_vector <= motion_vector;
1371
 
1372
  always @(posedge clk)
1373
    if (~rst) motion_vector_reg <= 8'b0;
1374
    else if (clk_en && (state == STATE_MACROBLOCK_QUANT)) /* set up motion_vector_reg */
1375
      case ({macroblock_motion_forward || (macroblock_intra && concealment_motion_vectors), macroblock_motion_backward, motion_vector_count_is_one})
1376
        3'b000: motion_vector_reg <= 8'b00000000; // no motion vectors
1377
        3'b001: motion_vector_reg <= 8'b00000000; // no motion vectors
1378
        3'b100: motion_vector_reg <= 8'b11110000; // motion_code_000, 001, 100, 101
1379
        3'b101: motion_vector_reg <= 8'b11000000; // motion_code_000, 001
1380
        3'b010: motion_vector_reg <= 8'b00001111; // motion_code_                    010, 011, 110, 111
1381
        3'b011: motion_vector_reg <= 8'b00001100; // motion_code_                    010, 011
1382
        3'b110: motion_vector_reg <= 8'b11111111; // motion_code_000, 001, 100, 101, 010, 011, 110, 111
1383
        3'b111: motion_vector_reg <= 8'b11001100; // motion_code_000, 001,           010, 011
1384
      endcase
1385
    else if (clk_en && (state == STATE_MOTION_PREDICT)) motion_vector_reg <= motion_vector_reg << 1;
1386
    else if (clk_en && (state == STATE_NEXT_MOTION_VECTOR) && (motion_vector_reg[7] == 1'b0)) motion_vector_reg <= motion_vector_reg << 1;
1387
    else motion_vector_reg <= motion_vector_reg;
1388
 
1389
  always @(posedge clk)
1390
    if (~rst) motion_code <= 5'b0;
1391
    else if (clk_en && (state == STATE_NEXT_MOTION_VECTOR)) motion_code <= 5'b0;
1392
    else if (clk_en && (state == STATE_MOTION_CODE)) motion_code <= motion_code_value;
1393
    else motion_code <= motion_code;
1394
 
1395
  always @(posedge clk)
1396
    if (~rst) motion_code_neg <= 1'b0;
1397
    else if (clk_en && (state == STATE_NEXT_MOTION_VECTOR)) motion_code_neg <= 1'b0;
1398
    else if (clk_en && (state == STATE_MOTION_CODE)) motion_code_neg <= motion_code_sign;
1399
    else motion_code_neg <= motion_code_neg;
1400
 
1401
  always @(posedge clk)
1402
    if (~rst) motion_code_residual <= 8'b0;
1403
    else if (clk_en && (state == STATE_NEXT_MOTION_VECTOR)) motion_code_residual <= 8'b0;
1404
    else if (clk_en && (state == STATE_MOTION_RESIDUAL))
1405
                     begin
1406
                       case (r_size)
1407
                        14'd00: motion_code_residual <= {8'b0}; // Error, f_code != 1 hence r_size != 0.
1408
                        14'd01: motion_code_residual <= {7'b0, getbits[23]};
1409
                        14'd02: motion_code_residual <= {6'b0, getbits[23:22]};
1410
                        14'd03: motion_code_residual <= {5'b0, getbits[23:21]};
1411
                        14'd04: motion_code_residual <= {4'b0, getbits[23:20]};
1412
                        14'd05: motion_code_residual <= {3'b0, getbits[23:19]};
1413
                        14'd06: motion_code_residual <= {2'b0, getbits[23:18]};
1414
                        14'd07: motion_code_residual <= {1'b0, getbits[23:17]};
1415
                        14'd08: motion_code_residual <=        getbits[23:16];
1416
                        default motion_code_residual <=        getbits[23:16]; // Should never happen, as r_size is 1..8 (f_code = 1..9 in Table E-8 at high level)
1417
                       endcase
1418
                     end
1419
    else motion_code_residual <= motion_code_residual;
1420
 
1421
  always @(posedge clk)
1422
    if (~rst) dmvector <= 2'b0;
1423
    else if (clk_en && (state == STATE_NEXT_MOTION_VECTOR)) dmvector <= 2'b0;
1424
    else if (clk_en && (state == STATE_MOTION_DMVECTOR))
1425
      begin
1426
        case ({dmvector_sign, dmvector_value})
1427
          {1'b0, 1'b0}:  dmvector <= 2'b0;
1428
          {1'b0, 1'b1}:  dmvector <= 2'b1;
1429
          {1'b1, 1'b0}:  dmvector <= 2'b0;
1430
          {1'b1, 1'b1}:  dmvector <= -2'b1;
1431
        endcase
1432
      end
1433
    else dmvector <= dmvector;
1434
 
1435
  /*
1436
   * Resetting motion vector predictors, par. 7.6.3.4.
1437
   * Also includes updating motion vector predictors to zero, par. 7.6.3.3.
1438
   *
1439
   * The difference between resetting and updating is that updating occurs
1440
   * after the motion vectors have been processed, in STATE_BLOCK
1441
   */
1442
 
1443
  wire pmv_reset = (state == STATE_SLICE) || /* par. 7.6.3.4, At the start of each slice. */
1444
                   ((state == STATE_MACROBLOCK_QUANT) && macroblock_intra && ~concealment_motion_vectors) || /* par. 7.6.3.4, Whenever an intra macroblock is decoded which has no concealment motion vectors. */
1445
                   ((state == STATE_MACROBLOCK_QUANT) && (picture_coding_type == P_TYPE) && ~macroblock_intra && ~macroblock_motion_forward) || /* par. 7.6.3.4, In a P-picture when a non-intra macroblock is decoded in which macroblock_motion_forward is zero. */
1446
                   ((state == STATE_MACROBLOCK_SKIP) && (picture_coding_type == P_TYPE)); /* par. 7.6.3.4, In a P-picture when a macroblock is skipped. Also 7.6.6.1, 7.6.6.2 */
1447
 
1448
 
1449
  /*
1450
   * Updating motion vector predictors. Table 7-9 and 7-10.
1451
   * When to PMV[1][0][1:0] = PMV[0][0][1:0]
1452
   */
1453
 
1454
  wire pmv_update0 =   ((picture_structure == FRAME_PICTURE) && (motion_type == MC_FRAME) && macroblock_intra) ||
1455
                       ((picture_structure == FRAME_PICTURE) && (motion_type == MC_FRAME) && macroblock_motion_forward) ||
1456
                       ((picture_structure == FRAME_PICTURE) && (motion_type == MC_DMV) && macroblock_motion_forward && ~macroblock_motion_backward && ~macroblock_intra) ||
1457
                       (((picture_structure == TOP_FIELD) || (picture_structure == BOTTOM_FIELD)) && (motion_type == MC_FIELD) && macroblock_intra) ||
1458
                       (((picture_structure == TOP_FIELD) || (picture_structure == BOTTOM_FIELD)) && (motion_type == MC_FIELD) && macroblock_motion_forward) ||
1459
                       (((picture_structure == TOP_FIELD) || (picture_structure == BOTTOM_FIELD)) && (motion_type == MC_DMV) && macroblock_motion_forward && ~macroblock_motion_backward && ~macroblock_intra) ;
1460
 
1461
  /*
1462
   * Updating motion vector predictors. Table 7-9 and 7-10.
1463
   * When to PMV[1][1][1:0] = PMV[0][1][1:0]
1464
   */
1465
 
1466
  wire pmv_update1 =  ((picture_structure == FRAME_PICTURE) && (motion_type == MC_FRAME) && ~macroblock_intra && macroblock_motion_backward) ||
1467
                      (((picture_structure == TOP_FIELD) || (picture_structure == BOTTOM_FIELD)) && (motion_type == MC_FIELD) && ~macroblock_intra && macroblock_motion_backward) ;
1468
 
1469
  /* motion vector pipeline */
1470
  always @(posedge clk)
1471
    if (~rst)
1472
      begin
1473
        motion_vector_0 <= 2'b0;
1474
        motion_vector_1 <= 2'b0;
1475
        motion_vector_2 <= 2'b0;
1476
        motion_vector_3 <= 2'b0;
1477
        motion_vector_4 <= 2'b0;
1478
        motion_vector_5 <= 2'b0;
1479
      end
1480
    else if (clk_en)
1481
      begin
1482
        motion_vector_0 <= motion_vector;
1483
        motion_vector_1 <= motion_vector_0;
1484
        motion_vector_2 <= motion_vector_1;
1485
        motion_vector_3 <= motion_vector_2;
1486
        motion_vector_4 <= motion_vector_3;
1487
        motion_vector_5 <= motion_vector_4;
1488
      end
1489
    else
1490
      begin
1491
        motion_vector_0 <= motion_vector_0;
1492
        motion_vector_1 <= motion_vector_1;
1493
        motion_vector_2 <= motion_vector_2;
1494
        motion_vector_3 <= motion_vector_3;
1495
        motion_vector_4 <= motion_vector_4;
1496
        motion_vector_5 <= motion_vector_5;
1497
      end
1498
 
1499
  always @(posedge clk)
1500
    if (~rst)
1501
      begin
1502
        motion_vector_valid_0 <= 1'b0;
1503
        motion_vector_valid_1 <= 1'b0;
1504
        motion_vector_valid_2 <= 1'b0;
1505
        motion_vector_valid_3 <= 1'b0;
1506
        motion_vector_valid_4 <= 1'b0;
1507
        motion_vector_valid_5 <= 1'b0;
1508
      end
1509
    else if (clk_en)
1510
      begin
1511
        motion_vector_valid_0 <= (state == STATE_MOTION_PREDICT);
1512
        motion_vector_valid_1 <= motion_vector_valid_0;
1513
        motion_vector_valid_2 <= motion_vector_valid_1;
1514
        motion_vector_valid_3 <= motion_vector_valid_2;
1515
        motion_vector_valid_4 <= motion_vector_valid_3;
1516
        motion_vector_valid_5 <= motion_vector_valid_4;
1517
      end
1518
    else
1519
      begin
1520
        motion_vector_valid_0 <= motion_vector_valid_0;
1521
        motion_vector_valid_1 <= motion_vector_valid_1;
1522
        motion_vector_valid_2 <= motion_vector_valid_2;
1523
        motion_vector_valid_3 <= motion_vector_valid_3;
1524
        motion_vector_valid_4 <= motion_vector_valid_4;
1525
        motion_vector_valid_5 <= motion_vector_valid_5;
1526
      end
1527
 
1528
  always @(posedge clk)
1529
    if (~rst)
1530
      begin
1531
        r_size_0 <= 4'b0;
1532
        r_size_1 <= 4'b0;
1533
      end
1534
    else if (clk_en)
1535
      begin
1536
        r_size_0 <= r_size;
1537
        r_size_1 <= r_size_0;
1538
      end
1539
    else
1540
      begin
1541
        r_size_0 <= r_size_0;
1542
        r_size_1 <= r_size_1;
1543
      end
1544
 
1545
  always @(posedge clk)
1546
    if (~rst) motion_code_neg_0 <= 1'b0;
1547
    else if (clk_en) motion_code_neg_0 <= motion_code_neg;
1548
    else motion_code_neg_0 <= motion_code_neg_0;
1549
 
1550
  wire signed [12:0]motion_code_signed = {8'b0, motion_code};
1551
  wire signed [12:0]motion_code_residual_signed = {5'b0, motion_code_residual};
1552
 
1553
  always @(posedge clk)
1554
    if (~rst) pmv_delta_0 <= 13'b0;
1555
    else if (clk_en) pmv_delta_0 <= ((r_size == 4'b0) || (motion_code == 5'b0)) ? motion_code_signed : ((motion_code_signed - 13'sd1) <<< r_size) + motion_code_residual_signed + 13'sd1;
1556
    else pmv_delta_0 <= pmv_delta_0;
1557
 
1558
  wire shift_pmv = (mv_format == MV_FIELD) && (picture_structure == FRAME_PICTURE);
1559
 
1560
  always @(posedge clk)
1561
    if (~rst)
1562
      begin
1563
        pmv_0 <= 13'b0;
1564
        pmv_1 <= 13'b0;
1565
        pmv_2 <= 13'b0;
1566
        pmv_3 <= 13'b0;
1567
        pmv_4 <= 13'b0;
1568
        pmv_5 <= 13'b0;
1569
      end
1570
    else if (clk_en)
1571
      begin
1572
        /* stage 1 */
1573
        case (motion_vector)
1574
          MOTION_VECTOR_0_0_0: pmv_0 <= pmv_0_0_0;
1575
          MOTION_VECTOR_0_0_1: pmv_0 <= shift_pmv ? pmv_0_0_1 >>> 1 : pmv_0_0_1;
1576
          MOTION_VECTOR_1_0_0: pmv_0 <= pmv_1_0_0;
1577
          MOTION_VECTOR_1_0_1: pmv_0 <= shift_pmv ? pmv_1_0_1 >>> 1 : pmv_1_0_1;
1578
          MOTION_VECTOR_0_1_0: pmv_0 <= pmv_0_1_0;
1579
          MOTION_VECTOR_0_1_1: pmv_0 <= shift_pmv ? pmv_0_1_1 >>> 1 : pmv_0_1_1;
1580
          MOTION_VECTOR_1_1_0: pmv_0 <= pmv_1_1_0;
1581
          MOTION_VECTOR_1_1_1: pmv_0 <= shift_pmv ? pmv_1_1_1 >>> 1 : pmv_1_1_1;
1582
          default              pmv_0 <= 13'b0;
1583
        endcase
1584
        /* stage 2 */
1585
        pmv_1 <= motion_code_neg_0 ? pmv_0 - pmv_delta_0 : pmv_0 + pmv_delta_0;
1586
        /*
1587
         * stage 3
1588
         * next case statement ought to be equivalent to:
1589
         * pmv <= (pmv < low) ? (pmv + range) : ((pmv > high) ? (pmv - range) : pmv);
1590
         *
1591
         * As f_code_ is restricted to the range 1..9, (Table E-8, High Level)
1592
         * r_size is restricted to the range 0..8.
1593
         */
1594
        case (r_size_1)
1595
          4'd0:   pmv_2 <= { {9{pmv_1[4]}},  pmv_1[3:0]  };
1596
          4'd1:   pmv_2 <= { {8{pmv_1[5]}},  pmv_1[4:0]  };
1597
          4'd2:   pmv_2 <= { {7{pmv_1[6]}},  pmv_1[5:0]  };
1598
          4'd3:   pmv_2 <= { {6{pmv_1[7]}},  pmv_1[6:0]  };
1599
          4'd4:   pmv_2 <= { {5{pmv_1[8]}},  pmv_1[7:0]  };
1600
          4'd5:   pmv_2 <= { {4{pmv_1[9]}},  pmv_1[8:0]  };
1601
          4'd6:   pmv_2 <= { {3{pmv_1[10]}}, pmv_1[9:0]  };
1602
          4'd7:   pmv_2 <= { {2{pmv_1[11]}}, pmv_1[10:0] };
1603
          4'd8:   pmv_2 <= { {1{pmv_1[12]}}, pmv_1[11:0] };
1604
          default pmv_2 <=                   pmv_1[12:0]  ; // never occurs
1605
        endcase
1606
        /*
1607
         * stage 4..5: dmv calculations only
1608
         */
1609
        pmv_3 <= pmv_2;
1610
        pmv_4 <= pmv_3;
1611
        pmv_5 <= pmv_4;
1612
      end
1613
    else
1614
      begin
1615
        pmv_0 <= pmv_0;
1616
        pmv_1 <= pmv_1;
1617
        pmv_2 <= pmv_2;
1618
        pmv_3 <= pmv_3;
1619
        pmv_4 <= pmv_4;
1620
        pmv_5 <= pmv_5;
1621
      end
1622
 
1623
  /*
1624
   * predicted motion vectors.
1625
   * pmv_reset is asserted when the motion vectors are reset to zero;
1626
   * pmv_update0 is asserted when pmv_1_0_x <= pmv_0_0_x
1627
   * pmv_update1 is asserted when pmv_1_1_x <= pmv_0_1_x
1628
   */
1629
  always @(posedge clk)
1630
    if (~rst) pmv_0_0_0 <= 13'b0;
1631
    else if (clk_en && pmv_reset) pmv_0_0_0 <= 13'b0;
1632
    else if (clk_en && (motion_vector_valid_5) && (motion_vector_5 == MOTION_VECTOR_0_0_0)) pmv_0_0_0 <= pmv_5;
1633
    else pmv_0_0_0 <= pmv_0_0_0;
1634
 
1635
  always @(posedge clk)
1636
    if (~rst) pmv_0_0_1 <= 13'b0;
1637
    else if (clk_en && pmv_reset) pmv_0_0_1 <= 13'b0;
1638
    else if (clk_en && (motion_vector_valid_5) && (motion_vector_5 == MOTION_VECTOR_0_0_1)) pmv_0_0_1 <= shift_pmv ? pmv_5 <<< 1 : pmv_5;
1639
    else pmv_0_0_1 <= pmv_0_0_1;
1640
 
1641
  always @(posedge clk)
1642
    if (~rst) pmv_1_0_0 <= 13'b0;
1643
    else if (clk_en && pmv_reset) pmv_1_0_0 <= 13'b0;
1644
    else if (clk_en && (motion_vector_valid_5) && (motion_vector_5 == MOTION_VECTOR_1_0_0)) pmv_1_0_0 <= pmv_5;
1645
    else if (clk_en && (motion_vector_valid_5) && (motion_vector_5 == MOTION_VECTOR_0_0_0) && pmv_update0) pmv_1_0_0 <= pmv_5;
1646
    else pmv_1_0_0 <= pmv_1_0_0;
1647
 
1648
  always @(posedge clk)
1649
    if (~rst) pmv_1_0_1 <= 13'b0;
1650
    else if (clk_en && pmv_reset) pmv_1_0_1 <= 13'b0;
1651
    else if (clk_en && (motion_vector_valid_5) && (motion_vector_5 == MOTION_VECTOR_1_0_1)) pmv_1_0_1 <= shift_pmv ? pmv_5 <<< 1 : pmv_5;
1652
    else if (clk_en && (motion_vector_valid_5) && (motion_vector_5 == MOTION_VECTOR_0_0_1) && pmv_update0) pmv_1_0_1 <= shift_pmv ? pmv_5 <<< 1 : pmv_5;
1653
    else pmv_1_0_1 <= pmv_1_0_1;
1654
 
1655
  always @(posedge clk)
1656
    if (~rst) pmv_0_1_0 <= 13'b0;
1657
    else if (clk_en && pmv_reset) pmv_0_1_0 <= 13'b0;
1658
    else if (clk_en && (motion_vector_valid_5) && (motion_vector_5 == MOTION_VECTOR_0_1_0)) pmv_0_1_0 <= pmv_5;
1659
    else pmv_0_1_0 <= pmv_0_1_0;
1660
 
1661
  always @(posedge clk)
1662
    if (~rst) pmv_0_1_1 <= 13'b0;
1663
    else if (clk_en && pmv_reset) pmv_0_1_1 <= 13'b0;
1664
    else if (clk_en && (motion_vector_valid_5) && (motion_vector_5 == MOTION_VECTOR_0_1_1)) pmv_0_1_1 <= shift_pmv ? pmv_5 <<< 1 : pmv_5;
1665
    else pmv_0_1_1 <= pmv_0_1_1;
1666
 
1667
  always @(posedge clk)
1668
    if (~rst) pmv_1_1_0 <= 13'b0;
1669
    else if (clk_en && pmv_reset) pmv_1_1_0 <= 13'b0;
1670
    else if (clk_en && (motion_vector_valid_5) && (motion_vector_5 == MOTION_VECTOR_1_1_0)) pmv_1_1_0 <= pmv_5;
1671
    else if (clk_en && (motion_vector_valid_5) && (motion_vector_5 == MOTION_VECTOR_0_1_0) && pmv_update1) pmv_1_1_0 <= pmv_5;
1672
    else pmv_1_1_0 <= pmv_1_1_0;
1673
 
1674
  always @(posedge clk)
1675
    if (~rst) pmv_1_1_1 <= 13'b0;
1676
    else if (clk_en && pmv_reset) pmv_1_1_1 <= 13'b0;
1677
    else if (clk_en && (motion_vector_valid_5) && (motion_vector_5 == MOTION_VECTOR_1_1_1)) pmv_1_1_1 <= shift_pmv ? pmv_5 <<< 1 : pmv_5;
1678
    else if (clk_en && (motion_vector_valid_5) && (motion_vector_5 == MOTION_VECTOR_0_1_1) && pmv_update1) pmv_1_1_1 <= shift_pmv ? pmv_5 <<< 1 : pmv_5;
1679
    else pmv_1_1_1 <= pmv_1_1_1;
1680
 
1681
  /* motion vector field select */
1682
  output reg       motion_vert_field_select_0_0; // motion_vertical_field_select. Indicates which reference field shall be used to form the prediction. 
1683
                                                 // If motion_vertical_field_select[r][s] is zero, then the top reference field shall be used, 
1684
                                                 // if it is one then the bottom reference field shall be used.
1685
  output reg       motion_vert_field_select_0_1;
1686
  output reg       motion_vert_field_select_1_0;
1687
  output reg       motion_vert_field_select_1_1;
1688
 
1689
  always @(posedge clk)
1690
    if (~rst) motion_vert_field_select_0_0 <= 1'b0;
1691
    else if (clk_en && (state == STATE_PICTURE_HEADER)) motion_vert_field_select_0_0 <= 1'b0; // I take the liberty of resetting at the start of a picture
1692
    else if (clk_en && (state == STATE_MACROBLOCK_SKIP) && (picture_structure != FRAME_PICTURE)) motion_vert_field_select_0_0 <= (picture_structure==BOTTOM_FIELD); // par. 7.6.6.1 and 7.6.6.3, skipped macroblocks. predict from field of same parity.
1693
    else if (clk_en && (state == STATE_MACROBLOCK_QUANT) && (picture_coding_type == P_TYPE) && ~macroblock_intra && ~macroblock_motion_forward && (picture_structure != FRAME_PICTURE)) motion_vert_field_select_0_0 <= (picture_structure==BOTTOM_FIELD); // par. 7.6.3.5 Prediction in P pictures. non-intra mb without forward mv in a P picture. 
1694
    else if (clk_en && (state == STATE_MOTION_VERT_FLD_SEL) && (motion_vector == MOTION_VECTOR_0_0_0)) motion_vert_field_select_0_0 <= getbits[23];
1695
    else motion_vert_field_select_0_0 <= motion_vert_field_select_0_0;
1696
 
1697
  always @(posedge clk)
1698
    if (~rst) motion_vert_field_select_0_1 <= 1'b0;
1699
    else if (clk_en && (state == STATE_PICTURE_HEADER)) motion_vert_field_select_0_1 <= 1'b0;
1700
    else if (clk_en && (state == STATE_MACROBLOCK_SKIP) && (picture_structure != FRAME_PICTURE)) motion_vert_field_select_0_1 <= (picture_structure==BOTTOM_FIELD); // par. 7.6.6.1 and 7.6.6.3, skipped macroblocks. predict from field of same parity.
1701
    else if (clk_en && (state == STATE_MOTION_VERT_FLD_SEL) && (motion_vector == MOTION_VECTOR_0_1_0)) motion_vert_field_select_0_1 <= getbits[23];
1702
    else motion_vert_field_select_0_1 <= motion_vert_field_select_0_1;
1703
 
1704
  always @(posedge clk)
1705
    if (~rst) motion_vert_field_select_1_0 <= 1'b0;
1706
    else if (clk_en && (state == STATE_PICTURE_HEADER)) motion_vert_field_select_1_0 <= 1'b0;
1707
    else if (clk_en && (state == STATE_MOTION_VERT_FLD_SEL) && (motion_vector == MOTION_VECTOR_1_0_0)) motion_vert_field_select_1_0 <= getbits[23];
1708
    else if (clk_en && (state == STATE_MOTION_VERT_FLD_SEL) && motion_vector_count_is_one && (mv_format == MV_FIELD) && ~dmv && (motion_vector == MOTION_VECTOR_0_0_0)) motion_vert_field_select_1_0 <= getbits[23]; // case of only one motion vector
1709
    else motion_vert_field_select_1_0 <= motion_vert_field_select_1_0;
1710
 
1711
  always @(posedge clk)
1712
    if (~rst) motion_vert_field_select_1_1 <= 1'b0;
1713
    else if (clk_en && (state == STATE_PICTURE_HEADER)) motion_vert_field_select_1_1 <= 1'b0;
1714
    else if (clk_en && (state == STATE_MOTION_VERT_FLD_SEL) && (motion_vector == MOTION_VECTOR_1_1_0)) motion_vert_field_select_1_1 <= getbits[23];
1715
    else if (clk_en && (state == STATE_MOTION_VERT_FLD_SEL) && motion_vector_count_is_one && (mv_format == MV_FIELD) && ~dmv && (motion_vector == MOTION_VECTOR_0_1_0)) motion_vert_field_select_1_1 <= getbits[23]; // case of only one motion vector
1716
    else motion_vert_field_select_1_1 <= motion_vert_field_select_1_1;
1717
 
1718
  /* additional dual-prime arithmetic, par. 7.6.3.6 */
1719
  always @(posedge clk)
1720
    if (~rst)
1721
      begin
1722
        dmvector_0 <= 2'b0;
1723
        dmvector_1 <= 2'b0;
1724
        dmvector_2 <= 2'b0;
1725
        dmvector_3 <= 2'b0;
1726
      end
1727
    else if (clk_en)
1728
      begin
1729
        dmvector_0 <= dmvector;
1730
        dmvector_1 <= dmvector_0;
1731
        dmvector_2 <= dmvector_1;
1732
        dmvector_3 <= dmvector_2;
1733
      end
1734
    else
1735
      begin
1736
        dmvector_0 <= dmvector_0;
1737
        dmvector_1 <= dmvector_1;
1738
        dmvector_2 <= dmvector_2;
1739
        dmvector_3 <= dmvector_3;
1740
      end
1741
 
1742
  wire pmv_2_pos = (~pmv_2[12] && (pmv_2 != 13'b0)); // asserted if pmv_2 > 0
1743
  wire top_field_at_bottom = (picture_structure==FRAME_PICTURE) && (~top_field_first);
1744
 
1745
  reg signed [1:0] e_parity_ref_parity_pred_4;
1746
  reg signed [12:0]dmvector_aux_a_3;
1747
  reg signed [12:0]dmvector_aux_b_3;
1748
  reg signed [12:0]dmvector_aux_a_4;
1749
  reg signed [12:0]dmvector_aux_b_4;
1750
  reg signed [12:0]dmvector_aux_a_5;
1751
  reg signed [12:0]dmvector_aux_b_5;
1752
 
1753
  always @(posedge clk)
1754
    if (~rst) e_parity_ref_parity_pred_4 <= 2'd0;
1755
    else if (clk_en && (motion_vector_3 == MOTION_VECTOR_0_0_0)) e_parity_ref_parity_pred_4 <= 2'd0;
1756
    else if (clk_en && (motion_vector_3 == MOTION_VECTOR_0_0_1)) e_parity_ref_parity_pred_4 <= ((picture_structure==FRAME_PICTURE) || (picture_structure==TOP_FIELD)) ? -2'd1 : 2'd1;
1757
    else e_parity_ref_parity_pred_4 <= e_parity_ref_parity_pred_4;
1758
 
1759
  always @(posedge clk)
1760
    if (~rst)
1761
      begin
1762
        dmvector_aux_a_3 <= 13'd0;
1763
        dmvector_aux_a_4 <= 13'd0;
1764
        dmvector_aux_a_5 <= 13'd0;
1765
      end
1766
    else if (clk_en)
1767
      begin
1768
        dmvector_aux_a_3 <= pmv_2 + pmv_2_pos;
1769
        dmvector_aux_a_4 <= {dmvector_aux_a_3[12], dmvector_aux_a_3[12:1]} + { {12{dmvector_3[1]}}, dmvector_3[0] }; // sign-extend dmvector before adding
1770
        dmvector_aux_a_5 <= (top_field_at_bottom ? dmvector_aux_b_4 : dmvector_aux_a_4) + { {12{e_parity_ref_parity_pred_4[1]}}, e_parity_ref_parity_pred_4[0] }; // sign-extend e_parity_ref_parity_pred before adding
1771
      end
1772
    else
1773
      begin
1774
        dmvector_aux_a_3 <= dmvector_aux_a_3;
1775
        dmvector_aux_a_4 <= dmvector_aux_a_4;
1776
        dmvector_aux_a_5 <= dmvector_aux_a_5;
1777
      end
1778
 
1779
  always @(posedge clk)
1780
    if (~rst)
1781
      begin
1782
        dmvector_aux_b_3 <= 13'd0;
1783
        dmvector_aux_b_4 <= 13'd0;
1784
        dmvector_aux_b_5 <= 13'd0;
1785
      end
1786
    else if (clk_en)
1787
      begin
1788
        dmvector_aux_b_3 <= (pmv_2 <<< 1) + pmv_2 + pmv_2_pos;
1789
        dmvector_aux_b_4 <= {dmvector_aux_b_3[12], dmvector_aux_b_3[12:1]} + { {12{dmvector_3[1]}}, dmvector_3[0] }; // sign-extend dmvector before adding
1790
        dmvector_aux_b_5 <= (top_field_at_bottom ? dmvector_aux_a_4 : dmvector_aux_b_4) - { {12{e_parity_ref_parity_pred_4[1]}}, e_parity_ref_parity_pred_4[0] }; // sign-extend e_parity_ref_parity_pred before adding
1791
      end
1792
    else
1793
      begin
1794
        dmvector_aux_b_3 <= dmvector_aux_b_3;
1795
        dmvector_aux_b_4 <= dmvector_aux_b_4;
1796
        dmvector_aux_b_5 <= dmvector_aux_b_5;
1797
      end
1798
 
1799
  always @(posedge clk)
1800
    if (~rst) dmv_0_0 <= 13'd0;
1801
    else if (clk_en && (motion_vector_valid_5) && dmv && (motion_vector_5 == MOTION_VECTOR_0_0_0)) dmv_0_0 <= dmvector_aux_a_5;
1802
    else dmv_0_0 <= dmv_0_0;
1803
 
1804
  always @(posedge clk)
1805
    if (~rst) dmv_1_0 <= 13'd0;
1806
    else if (clk_en && (motion_vector_valid_5) && dmv && (motion_vector_5 == MOTION_VECTOR_0_0_0)) dmv_1_0 <= dmvector_aux_b_5;
1807
    else dmv_1_0 <= dmv_1_0;
1808
 
1809
  always @(posedge clk)
1810
    if (~rst) dmv_0_1 <= 13'd0;
1811
    else if (clk_en && (motion_vector_valid_5) && dmv && (motion_vector_5 == MOTION_VECTOR_0_0_1)) dmv_0_1 <= dmvector_aux_a_5;
1812
    else dmv_0_1 <= dmv_0_1;
1813
 
1814
  always @(posedge clk)
1815
    if (~rst) dmv_1_1 <= 13'd0;
1816
    else if (clk_en && (motion_vector_valid_5) && dmv && (motion_vector_5 == MOTION_VECTOR_0_0_1)) dmv_1_1 <= dmvector_aux_b_5;
1817
    else dmv_1_1 <= dmv_1_1;
1818
 
1819
  /* motion vector output */
1820
  always @(posedge clk)
1821
    if (~rst) motion_vector_valid <= 1'b0;
1822
    /*
1823
     * par. 7.6.6: Skipped macroblocks.
1824
     * In a P picture the motion vector shall be zero;
1825
     * in a B picture the motion vectors are taken from the appropriate motion vector predictors.
1826
     *
1827
     * Here:
1828
     * - motion vectors of skipped macroblocks are reset (for P pictures) during STATE_MACROBLOCK_SKIP and valid the state after STATE_MACROBLOCK_SKIP
1829
     * - motion vectors of non-skipped macroblocks are computed during STATE_NEXT_MOTION_VECTOR .. STATE_MOTION_PIPELINE_FLUSH. The
1830
     *   motion vectors are valid after STATE_MOTION_PIPELINE_FLUSH, and, by extension, at STATE_BLOCK.
1831
     */
1832
    else if (clk_en) motion_vector_valid <= (state == STATE_MACROBLOCK_SKIP) || (state == STATE_BLOCK);
1833
    else motion_vector_valid <= 1'b0;
1834
 
1835
  /* second field */
1836
  output reg       second_field;
1837
 
1838
  always @(posedge clk)
1839
    if (~rst) second_field <= 1'b0;
1840
    else if (clk_en && ((state == STATE_SEQUENCE_HEADER) || (state == STATE_GROUP_HEADER))) second_field <= 1'b1;
1841
    else if (clk_en && (state == STATE_PICTURE_HEADER) && (picture_structure == FRAME_PICTURE)) second_field <= 1'b0; /* recover from illegal number of field pictures, if necessary */
1842
    else if (clk_en && (state == STATE_PICTURE_HEADER)) second_field <= ~second_field; /* field picture */
1843
    else second_field <= second_field;
1844
 
1845
  /*
1846
     tell motion compensation to switch picture buffers.
1847
     If frame picture, switch picture buffers every picture header.
1848
     If field picture, switch picture buffers every other picture header.
1849
     (Field pictures use two picture headers, one for each field)
1850
   */
1851
  output reg       update_picture_buffers;
1852
  output reg       last_frame;
1853
 
1854
  always @(posedge clk)
1855
    if (~rst) update_picture_buffers <= 1'b0;
1856
    else if (clk_en) update_picture_buffers <= ((state == STATE_PICTURE_HEADER) && ((picture_structure == FRAME_PICTURE) || second_field)) // emit frame at picture header
1857
                                               || ((state == STATE_SEQUENCE_END) && ~last_frame); // emit last frame
1858
    else update_picture_buffers <= 1'b0;
1859
 
1860
  /*
1861
    We're at video end, tell motion compensation to emit the last frame.
1862
   */
1863
 
1864
  always @(posedge clk)
1865
    if (~rst) last_frame <= 1'b0;
1866
    else if (clk_en && (state == STATE_SEQUENCE_END)) last_frame <= 1'b1; // end of this video bitstream
1867
    else if (clk_en && ((state == STATE_SEQUENCE_HEADER) || (state == STATE_PICTURE_HEADER))) last_frame <= 1'b0; // start of new video bitstream
1868
    else last_frame <= last_frame;
1869
 
1870
  /* dct_dc_size_luminance, dct_dc_size_chrominance and dct_dc_differential (par. 7.2.1) */
1871
 
1872
  always @(posedge clk)
1873
    if (~rst) dct_dc_size <= 4'b0;
1874
    else if (clk_en && (state == STATE_DCT_DC_LUMI_SIZE)) dct_dc_size <= dct_dc_size_luminance_value;     // table B-12 lookup
1875
    else if (clk_en && (state == STATE_DCT_DC_CHROMI_SIZE)) dct_dc_size <= dct_dc_size_chrominance_value; // table B-13 lookup
1876
    else dct_dc_size <= dct_dc_size;
1877
 
1878
  reg [10:0]dct_dc_pred_add;
1879
  always @*
1880
      case (dct_dc_size)
1881
        4'd1:   dct_dc_pred_add = getbits[23];
1882
        4'd2:   dct_dc_pred_add = getbits[23:22];
1883
        4'd3:   dct_dc_pred_add = getbits[23:21];
1884
        4'd4:   dct_dc_pred_add = getbits[23:20];
1885
        4'd5:   dct_dc_pred_add = getbits[23:19];
1886
        4'd6:   dct_dc_pred_add = getbits[23:18];
1887
        4'd7:   dct_dc_pred_add = getbits[23:17];
1888
        4'd8:   dct_dc_pred_add = getbits[23:16];
1889
        4'd9:   dct_dc_pred_add = getbits[23:15];
1890
        4'd10:  dct_dc_pred_add = getbits[23:14];
1891
        4'd11:  dct_dc_pred_add = getbits[23:13];
1892
        default dct_dc_pred_add = 11'd0; // error: maximum in table B-12 and B-13 is 11
1893
      endcase
1894
 
1895
  reg [10:0]dct_dc_pred_sub;
1896
  always @*
1897
      case (dct_dc_size)
1898
        4'd1:   dct_dc_pred_sub = 11'b1;
1899
        4'd2:   dct_dc_pred_sub = 11'b11;
1900
        4'd3:   dct_dc_pred_sub = 11'b111;
1901
        4'd4:   dct_dc_pred_sub = 11'b1111;
1902
        4'd5:   dct_dc_pred_sub = 11'b11111;
1903
        4'd6:   dct_dc_pred_sub = 11'b111111;
1904
        4'd7:   dct_dc_pred_sub = 11'b1111111;
1905
        4'd8:   dct_dc_pred_sub = 11'b11111111;
1906
        4'd9:   dct_dc_pred_sub = 11'b111111111;
1907
        4'd10:  dct_dc_pred_sub = 11'b1111111111;
1908
        4'd11:  dct_dc_pred_sub = 11'b11111111111;
1909
        default dct_dc_pred_sub = 11'd0; // error: maximum in table B-12 and B-13 is 11
1910
      endcase
1911
 
1912
  always @(posedge clk)
1913
    if (~rst) dct_dc_pred <= 11'b0;
1914
    else if (clk_en && (state == STATE_DCT_DC_LUMI_SIZE))                             dct_dc_pred <= dct_dc_pred_0; // Y block
1915
    else if (clk_en && (state == STATE_DCT_DC_CHROMI_SIZE) && block_chromi1_code[12]) dct_dc_pred <= dct_dc_pred_1; // Cb block
1916
    else if (clk_en && (state == STATE_DCT_DC_CHROMI_SIZE) && block_chromi2_code[12]) dct_dc_pred <= dct_dc_pred_2; // Cr block
1917
    else if (clk_en && (state == STATE_DCT_DC_DIFF))
1918
      begin
1919
        if (dct_dc_size == 4'd0) dct_dc_pred <=  dct_dc_pred;
1920
        else if (getbits[23] == 1'b0) dct_dc_pred <= dct_dc_pred + dct_dc_pred_add - dct_dc_pred_sub;
1921
        else dct_dc_pred <= dct_dc_pred + dct_dc_pred_add;
1922
      end
1923
    else dct_dc_pred <= dct_dc_pred;
1924
 
1925
  // luminance (Y) dct dc predictor
1926
  always @(posedge clk)
1927
    if (~rst) dct_dc_pred_0 <= 11'b0;
1928
    else if (clk_en && ((state == STATE_SLICE) ||  // at the start of a slice (par. 7.2.1)
1929
                        ((state == STATE_BLOCK) && ~macroblock_intra) || // whenever a non-intra macroblock is decoded (par. 7.2.1.)
1930
                        (state == STATE_MACROBLOCK_SKIP) )) // whenever a macroblock is skipped, i.e. when macroblock_address_increment > 1
1931
                        dct_dc_pred_0 <= 11'b0; // the mpeg2 reference implementation (mpeg2decode) does not reset dct_dc_pred[i] to 11'd128 << intra_dc_precision; rather it adds 128 to the reconstructed output.
1932
    else if (clk_en && (state == STATE_DCT_DC_DIFF_0) && block_lumi_code[12]) dct_dc_pred_0 <= dct_dc_pred;
1933
    else dct_dc_pred_0 <= dct_dc_pred_0;
1934
 
1935
  // first chrominance (Cb) dct dc predictor
1936
  always @(posedge clk)
1937
    if (~rst) dct_dc_pred_1 <= 11'b0;
1938
    else if (clk_en && ((state == STATE_SLICE) ||  // at the start of a slice (par. 7.2.1)
1939
                        ((state == STATE_BLOCK) && ~macroblock_intra) || // whenever a non-intra macroblock is decoded (par. 7.2.1.)
1940
                        (state == STATE_MACROBLOCK_SKIP) )) // whenever a macroblock is skipped, i.e. when macroblock_address_increment > 1
1941
                        dct_dc_pred_1 <= 11'b0; // the mpeg2 reference implementation (mpeg2decode) does not reset dct_dc_pred[i] to 11'd128 << intra_dc_precision; rather it adds 128 to the reconstructed output.
1942
    else if (clk_en && (state == STATE_DCT_DC_DIFF_0) && block_chromi1_code[12]) dct_dc_pred_1 <= dct_dc_pred;
1943
    else dct_dc_pred_1 <= dct_dc_pred_1;
1944
 
1945
  // second chrominance (Cr) dct dc predictor
1946
  always @(posedge clk)
1947
    if (~rst) dct_dc_pred_2 <= 11'b0;
1948
    else if (clk_en && ((state == STATE_SLICE) ||  // at the start of a slice (par. 7.2.1)
1949
                        ((state == STATE_BLOCK) && ~macroblock_intra) || // whenever a non-intra macroblock is decoded (par. 7.2.1.)
1950
                        (state == STATE_MACROBLOCK_SKIP) )) // whenever a macroblock is skipped, i.e. when macroblock_address_increment > 1
1951
                        dct_dc_pred_2 <= 11'b0; // the mpeg2 reference implementation (mpeg2decode) does not reset dct_dc_pred[i] to 11'd128 << intra_dc_precision; rather it adds 128 to the reconstructed output.
1952
    else if (clk_en && (state == STATE_DCT_DC_DIFF_0) && block_chromi2_code[12]) dct_dc_pred_2 <= dct_dc_pred;
1953
    else dct_dc_pred_2 <= dct_dc_pred_2;
1954
 
1955
 
1956
  /* dct coefficients */
1957
  assign dct_coefficient_escape = (getbits[23:18] == 6'b000001); // one if dct coefficient escape in table B-14 or B-15
1958
 
1959
  /* dct coeeficients are vlc decoded - still unsigned - and clocked in dct_coeff_run_0 and dct_coeff_signed_level_0.
1960
   * next clock, the sign bit is applied, and the run/level pair is clocked in dct_coeff_run and dct_coeff_signed_level .
1961
   *
1962
   * A non-coded block (STATE_NON_CODED_BLOCK) or a block of a skipped macroblock (STATE_EMIT_EMPTY_BLOCK) is coded as a single zero,
1963
   * which also is the end of block. run=0, level=0, signed_level=0, end=1, valid=1.
1964
   */
1965
 
1966
  always @(posedge clk)
1967
    if (~rst) dct_coeff_run_0 <= 5'd0;
1968
    else if (clk_en && ((state == STATE_DCT_SUBS_B15) || (state == STATE_DCT_SUBS_B14) || (state == STATE_DCT_NON_INTRA_FIRST)) && dct_coefficient_escape) dct_coeff_run_0 <= getbits[17:12];
1969
    else if (clk_en && (state == STATE_DCT_SUBS_B15)) dct_coeff_run_0 <= dct_coefficient_1_decoded[10:6];
1970
    else if (clk_en && (state == STATE_DCT_SUBS_B14)) dct_coeff_run_0 <= dct_coefficient_0_decoded[10:6];
1971
    else if (clk_en && (state == STATE_DCT_NON_INTRA_FIRST)) dct_coeff_run_0 <= dct_non_intra_first_coefficient_0_decoded[10:6];
1972
    else if (clk_en && (state == STATE_DCT_DC_DIFF_0)) dct_coeff_run_0 <= 5'd0;  // first (=dc)  coefficient of intra block is never preceded by zeroes
1973
    else if (clk_en && ((state == STATE_NON_CODED_BLOCK) || (state == STATE_EMIT_EMPTY_BLOCK))) dct_coeff_run_0 <= 5'd0; // non-coded block: all zeroes.
1974
    else dct_coeff_run_0 <= dct_coeff_run_0;
1975
 
1976
  always @(posedge clk)
1977
    if (~rst) dct_coeff_signed_level_0 <= 12'd0;
1978
    else if (clk_en && ((state == STATE_DCT_ESCAPE_B15) || (state == STATE_DCT_ESCAPE_B14))) dct_coeff_signed_level_0 <= getbits[23:12];
1979
    else if (clk_en && (state == STATE_DCT_SUBS_B15) && ~dct_coefficient_escape) dct_coeff_signed_level_0 <= dct_coefficient_1_decoded[5:0]; // still needs sign
1980
    else if (clk_en && (state == STATE_DCT_SUBS_B14) && ~dct_coefficient_escape) dct_coeff_signed_level_0 <= dct_coefficient_0_decoded[5:0]; // still needs sign
1981
    else if (clk_en && (state == STATE_DCT_NON_INTRA_FIRST) && ~dct_coefficient_escape) dct_coeff_signed_level_0 <= dct_non_intra_first_coefficient_0_decoded[5:0]; // still needs sign
1982
    else if (clk_en && (state == STATE_DCT_DC_DIFF_0)) dct_coeff_signed_level_0 <= {dct_dc_pred[10], dct_dc_pred}; // sign extend dc (first) coefficient of intra block
1983
    else if (clk_en && (state == STATE_NON_CODED_BLOCK) || (state == STATE_EMIT_EMPTY_BLOCK)) dct_coeff_signed_level_0 <= 12'd0; // non-coded block: all zeroes.
1984
    else dct_coeff_signed_level_0 <= dct_coeff_signed_level_0;
1985
 
1986
  always @(posedge clk)
1987
    if (~rst) dct_coeff_apply_signbit_0 <= 1'b0;
1988
    else if (clk_en && ((state == STATE_DCT_DC_DIFF_0) || (state == STATE_DCT_ESCAPE_B15) || (state == STATE_DCT_ESCAPE_B14))) dct_coeff_apply_signbit_0 <= 1'b0;
1989
    else if (clk_en && ((state == STATE_DCT_SUBS_B14) || (state == STATE_DCT_SUBS_B15) || (state == STATE_DCT_NON_INTRA_FIRST))) dct_coeff_apply_signbit_0 <= ~dct_coefficient_escape;
1990
    else if (clk_en && (state == STATE_NON_CODED_BLOCK) || (state == STATE_EMIT_EMPTY_BLOCK)) dct_coeff_apply_signbit_0 <= 1'b0; // non-coded block: all zeroes.
1991
    else dct_coeff_apply_signbit_0 <= dct_coeff_apply_signbit_0;
1992
 
1993
  always @(posedge clk)
1994
    if (~rst) dct_coeff_valid_0 <= 1'b0;
1995
    else if (clk_en && ((state == STATE_DCT_DC_DIFF_0) || (state == STATE_DCT_ESCAPE_B15) || (state == STATE_DCT_ESCAPE_B14) || (state == STATE_NON_CODED_BLOCK) || (state == STATE_EMIT_EMPTY_BLOCK))) dct_coeff_valid_0 <= 1'b1;
1996
    else if (clk_en && ((state == STATE_DCT_SUBS_B14) || (state == STATE_DCT_SUBS_B15) || (state == STATE_DCT_NON_INTRA_FIRST))) dct_coeff_valid_0 <= ~dct_coefficient_escape;
1997
    else if (clk_en) dct_coeff_valid_0 <= 1'b0;
1998
    else dct_coeff_valid_0 <= dct_coeff_valid_0;
1999
 
2000
  always @(posedge clk)
2001
    if (~rst) dct_coeff_end_0 <= 1'b0;
2002
    else if (clk_en) dct_coeff_end_0 <= ((state == STATE_DCT_SUBS_B14) && (getbits[23:22] == 2'b10)) || ((state == STATE_DCT_SUBS_B15) && (getbits[23:20] == 4'b0110)) || (state == STATE_NON_CODED_BLOCK) || (state == STATE_EMIT_EMPTY_BLOCK);
2003
    else dct_coeff_end_0 <= dct_coeff_end_0;
2004
 
2005
  // Now we clock dct_coeff_run_0 into dct_coeff_run, dct_coeff_signed_level_0 into dct_coeff_signed_level, and apply sign bit, if needed.
2006
  //
2007
  always @(posedge clk)
2008
    if (~rst) dct_coeff_run <= 5'd0;
2009
    else if (clk_en) dct_coeff_run <= dct_coeff_run_0;
2010
    else dct_coeff_run <= dct_coeff_run;
2011
 
2012
  always @(posedge clk)
2013
    if (~rst) dct_coeff_signed_level <= 12'd0;
2014
    else if (clk_en) dct_coeff_signed_level <= (dct_coeff_apply_signbit_0 && signbit) ? 12'd1 + ~dct_coeff_signed_level_0 : dct_coeff_signed_level_0; // 2's complement -> negative
2015
    else dct_coeff_signed_level <= dct_coeff_signed_level;
2016
 
2017
  always @(posedge clk)
2018
    if (~rst) dct_coeff_valid <= 1'b0;
2019
    else if (clk_en) dct_coeff_valid <= dct_coeff_valid_0;
2020
    else dct_coeff_valid <= dct_coeff_valid;
2021
 
2022
  always @(posedge clk)
2023
    if (~rst) dct_coeff_end <= 1'b0;
2024
    else if (clk_en) dct_coeff_end <= dct_coeff_end_0;
2025
    else dct_coeff_end <= dct_coeff_end;
2026
 
2027
  /* coded block pattern */
2028
  always @(posedge clk) // // par. 6.3.17.4
2029
    if (~rst) coded_block_pattern <= 12'b0;
2030
    else if (clk_en && (state == STATE_MOTION_PIPELINE_FLUSH))
2031
      begin
2032
        if (macroblock_intra) // default values
2033
          case (chroma_format)
2034
            CHROMA420: coded_block_pattern <= {12'b111111000000};
2035
            CHROMA422: coded_block_pattern <= {12'b111111110000};
2036
            CHROMA444: coded_block_pattern <= {12'b111111111111};
2037
            default    coded_block_pattern <= {12'b000000000000}; // error
2038
          endcase
2039
        else
2040
          coded_block_pattern <= 12'b000000000000;
2041
      end
2042
    else if (clk_en && (state == STATE_CODED_BLOCK_PATTERN)) coded_block_pattern <= {coded_block_pattern_value, 6'b0};
2043
    else if (clk_en && (state == STATE_CODED_BLOCK_PATTERN_1)) coded_block_pattern <= {coded_block_pattern[11:6], getbits[23:22], 4'b0};
2044
    else if (clk_en && (state == STATE_CODED_BLOCK_PATTERN_2)) coded_block_pattern <= {coded_block_pattern[11:6], getbits[23:18]};
2045
    else if (clk_en && (state == STATE_NEXT_BLOCK)) coded_block_pattern <= (coded_block_pattern << 1);
2046
    else if (clk_en && (state == STATE_DCT_ERROR)) coded_block_pattern <= 12'b000000000000; // error occurred; output remaining blocks as non-coded blocks
2047
    else coded_block_pattern <= coded_block_pattern;
2048
 
2049
   /* block loop registers */
2050
   /*
2051
    * block_pattern_code and block_lumi_code are used in the loop which is
2052
    * symbolically coded in par. 6.2.5 as:
2053
    * for ( i = 0; i < block_count; i + + ) {
2054
    *   block( i )
2055
    *   }
2056
    *
2057
    * We set up two registers, block_pattern_code and block_lumi_code.
2058
    * The "one" bits in block_pattern_code correspond to the blocks present
2059
    * in the macroblock.
2060
    * The "one" bits in block_lumi_code correspond to the luminance blocks
2061
    * in the macroblock (the first four blocks).
2062
    * Both block_pattern_code and block_lumi_code are shifted left one bit
2063
    * after each block. Macroblock ends when block_pattern_code is zero.
2064
    */
2065
 
2066
   always @(posedge clk)
2067
     if (~rst) block_pattern_code <= 12'b0;
2068
     else if (clk_en && (state == STATE_MOTION_PIPELINE_FLUSH))
2069
       case (chroma_format)  // Table 6-20
2070
         CHROMA420: block_pattern_code <= {12'b111111000000};
2071
         CHROMA422: block_pattern_code <= {12'b111111110000};
2072
         CHROMA444: block_pattern_code <= {12'b111111111111};
2073
         default    block_pattern_code <= {12'b000000000000}; // error
2074
       endcase
2075
     else if (clk_en && (state == STATE_NEXT_BLOCK)) block_pattern_code <= (block_pattern_code << 1);
2076
     else block_pattern_code <= block_pattern_code;
2077
 
2078
   always @(posedge clk)
2079
     if (~rst) block_lumi_code <= 6'b0;
2080
     else if (clk_en && (state == STATE_BLOCK)) block_lumi_code <= 6'b011110;
2081
     else if (clk_en && (state == STATE_NEXT_BLOCK)) block_lumi_code <= (block_lumi_code << 1);
2082
     else block_lumi_code <= block_lumi_code;
2083
 
2084
   always @(posedge clk)
2085
     if (~rst) block_chromi1_code <= 13'b0;
2086
     else if (clk_en && (state == STATE_BLOCK)) block_chromi1_code <= 13'b0000010101010;
2087
     else if (clk_en && (state == STATE_NEXT_BLOCK)) block_chromi1_code <= (block_chromi1_code << 1);
2088
     else block_chromi1_code <= block_chromi1_code;
2089
 
2090
   always @(posedge clk)
2091
     if (~rst) block_chromi2_code <= 13'b0;
2092
     else if (clk_en && (state == STATE_BLOCK)) block_chromi2_code <= 13'b0000001010101;
2093
     else if (clk_en && (state == STATE_NEXT_BLOCK)) block_chromi2_code <= (block_chromi2_code << 1);
2094
     else block_chromi2_code <= block_chromi2_code;
2095
 
2096
`ifdef DEBUG
2097
   always @(posedge clk)
2098
     if (clk_en)
2099
       case (state)
2100
         STATE_NEXT_START_CODE:            #0 $display("%m\tSTATE_NEXT_START_CODE");
2101
         STATE_START_CODE:                 #0 $display("%m\tSTATE_START_CODE");
2102
         STATE_PICTURE_HEADER:             #0 $display("%m\tSTATE_PICTURE_HEADER");
2103
         STATE_PICTURE_HEADER0:            #0 $display("%m\tSTATE_PICTURE_HEADER0");
2104
         STATE_PICTURE_HEADER1:            #0 $display("%m\tSTATE_PICTURE_HEADER1");
2105
         STATE_PICTURE_HEADER2:            #0 $display("%m\tSTATE_PICTURE_HEADER2");
2106
         STATE_SEQUENCE_HEADER:            #0 $display("%m\tSTATE_SEQUENCE_HEADER");
2107
         STATE_SEQUENCE_HEADER0:           #0 $display("%m\tSTATE_SEQUENCE_HEADER0");
2108
         STATE_SEQUENCE_HEADER1:           #0 $display("%m\tSTATE_SEQUENCE_HEADER1");
2109
         STATE_SEQUENCE_HEADER2:           #0 $display("%m\tSTATE_SEQUENCE_HEADER2");
2110
         STATE_SEQUENCE_HEADER3:           #0 $display("%m\tSTATE_SEQUENCE_HEADER3");
2111
         STATE_GROUP_HEADER:               #0 $display("%m\tSTATE_GROUP_HEADER");
2112
         STATE_GROUP_HEADER0:              #0 $display("%m\tSTATE_GROUP_HEADER0");
2113
         STATE_EXTENSION_START_CODE:       #0 $display("%m\tSTATE_EXTENSION_START_CODE");
2114
         STATE_SEQUENCE_EXT:               #0 $display("%m\tSTATE_SEQUENCE_EXT");
2115
         STATE_SEQUENCE_EXT0:              #0 $display("%m\tSTATE_SEQUENCE_EXT0");
2116
         STATE_SEQUENCE_EXT1:              #0 $display("%m\tSTATE_SEQUENCE_EXT1");
2117
         STATE_SEQUENCE_DISPLAY_EXT:       #0 $display("%m\tSTATE_SEQUENCE_DISPLAY_EXT");
2118
         STATE_SEQUENCE_DISPLAY_EXT0:      #0 $display("%m\tSTATE_SEQUENCE_DISPLAY_EXT0");
2119
         STATE_SEQUENCE_DISPLAY_EXT1:      #0 $display("%m\tSTATE_SEQUENCE_DISPLAY_EXT1");
2120
         STATE_SEQUENCE_DISPLAY_EXT2:      #0 $display("%m\tSTATE_SEQUENCE_DISPLAY_EXT2");
2121
         STATE_QUANT_MATRIX_EXT:           #0 $display("%m\tSTATE_QUANT_MATRIX_EXT");
2122
         STATE_PICTURE_CODING_EXT:         #0 $display("%m\tSTATE_PICTURE_CODING_EXT");
2123
         STATE_PICTURE_CODING_EXT0:        #0 $display("%m\tSTATE_PICTURE_CODING_EXT0");
2124
         STATE_PICTURE_CODING_EXT1:        #0 $display("%m\tSTATE_PICTURE_CODING_EXT1");
2125
         STATE_LD_INTRA_QUANT0:            #0 $display("%m\tSTATE_LD_INTRA_QUANT0");
2126
         STATE_LD_INTRA_QUANT1:            #0 $display("%m\tSTATE_LD_INTRA_QUANT1");
2127
         STATE_LD_NON_INTRA_QUANT0:        #0 $display("%m\tSTATE_LD_NON_INTRA_QUANT0");
2128
         STATE_LD_NON_INTRA_QUANT1:        #0 $display("%m\tSTATE_LD_NON_INTRA_QUANT1");
2129
         STATE_LD_CHROMA_INTRA_QUANT1:     #0 $display("%m\tSTATE_LD_CHROMA_INTRA_QUANT1");
2130
         STATE_LD_CHROMA_NON_INTRA_QUANT1: #0 $display("%m\tSTATE_LD_CHROMA_NON_INTRA_QUANT1");
2131
         STATE_SLICE:                      #0 $display("%m\tSTATE_SLICE");
2132
         STATE_SLICE_EXTENSION:            #0 $display("%m\tSTATE_SLICE_EXTENSION");
2133
         STATE_SLICE_EXTRA_INFORMATION:    #0 $display("%m\tSTATE_SLICE_EXTRA_INFORMATION");
2134
         STATE_NEXT_MACROBLOCK:            #0 $display("%m\tSTATE_NEXT_MACROBLOCK");
2135
         STATE_MACROBLOCK_SKIP:            #0 $display("%m\tSTATE_MACROBLOCK_SKIP");
2136
         STATE_DELAY_EMPTY_BLOCK:          #0 $display("%m\tSTATE_DELAY_EMPTY_BLOCK");
2137
         STATE_EMIT_EMPTY_BLOCK:           #0 $display("%m\tSTATE_EMIT_EMPTY_BLOCK");
2138
         STATE_MACROBLOCK_TYPE:            #0 $display("%m\tSTATE_MACROBLOCK_TYPE");
2139
         STATE_MOTION_TYPE:                #0 $display("%m\tSTATE_MOTION_TYPE");
2140
         STATE_DCT_TYPE:                   #0 $display("%m\tSTATE_DCT_TYPE");
2141
         STATE_MACROBLOCK_QUANT:           #0 $display("%m\tSTATE_MACROBLOCK_QUANT");
2142
         STATE_NEXT_MOTION_VECTOR:         #0 $display("%m\tSTATE_NEXT_MOTION_VECTOR");
2143
         STATE_MOTION_VERT_FLD_SEL:        #0 $display("%m\tSTATE_MOTION_VERT_FLD_SEL");
2144
         STATE_MOTION_CODE:                #0 $display("%m\tSTATE_MOTION_CODE");
2145
         STATE_MOTION_RESIDUAL:            #0 $display("%m\tSTATE_MOTION_RESIDUAL");
2146
         STATE_MOTION_DMVECTOR:            #0 $display("%m\tSTATE_MOTION_DMVECTOR");
2147
         STATE_MOTION_PREDICT:             #0 $display("%m\tSTATE_MOTION_PREDICT");
2148
         STATE_MOTION_PIPELINE_FLUSH:      #0 $display("%m\tSTATE_MOTION_PIPELINE_FLUSH");
2149
         STATE_MARKER_BIT_0:               #0 $display("%m\tSTATE_MARKER_BIT_0");
2150
         STATE_CODED_BLOCK_PATTERN:        #0 $display("%m\tSTATE_CODED_BLOCK_PATTERN");
2151
         STATE_CODED_BLOCK_PATTERN_1:      #0 $display("%m\tSTATE_CODED_BLOCK_PATTERN_1");
2152
         STATE_CODED_BLOCK_PATTERN_2:      #0 $display("%m\tSTATE_CODED_BLOCK_PATTERN_2");
2153
         STATE_BLOCK:                      #0 $display("%m\tSTATE_BLOCK");
2154
         STATE_NEXT_BLOCK:                 #0 $display("%m\tSTATE_NEXT_BLOCK");
2155
         STATE_DCT_DC_LUMI_SIZE:           #0 $display("%m\tSTATE_DCT_DC_LUMI_SIZE");
2156
         STATE_DCT_DC_CHROMI_SIZE:         #0 $display("%m\tSTATE_DCT_DC_CHROMI_SIZE");
2157
         STATE_DCT_DC_DIFF