1 |
2 |
kdv |
/*
|
2 |
|
|
* motcomp_picbuf.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 |
|
|
* motcomp_picbuf.v - Motion compensation: picture buffer management
|
21 |
|
|
*/
|
22 |
|
|
|
23 |
|
|
`include "timescale.v"
|
24 |
|
|
|
25 |
|
|
`undef DEBUG
|
26 |
|
|
//`define DEBUG 1
|
27 |
|
|
|
28 |
|
|
`undef CHECK
|
29 |
|
|
`ifdef __IVERILOG__
|
30 |
|
|
`define CHECK 1
|
31 |
|
|
`endif
|
32 |
|
|
|
33 |
|
|
module motcomp_picbuf(
|
34 |
|
|
clk, clk_en, rst,
|
35 |
|
|
source_select,
|
36 |
|
|
progressive_sequence, progressive_frame, top_field_first, repeat_first_field, last_frame,
|
37 |
|
|
picture_coding_type,
|
38 |
|
|
forward_reference_frame, backward_reference_frame, current_frame,
|
39 |
|
|
output_frame, output_frame_valid, output_frame_rd, output_progressive_sequence, output_progressive_frame, output_top_field_first, output_repeat_first_field,
|
40 |
|
|
update_picture_buffers, picbuf_busy
|
41 |
|
|
);
|
42 |
|
|
|
43 |
|
|
input clk; // clock
|
44 |
|
|
input clk_en; // clock enable
|
45 |
|
|
input rst; // synchronous active low reset
|
46 |
|
|
|
47 |
|
|
input [2:0]source_select; // select video out source
|
48 |
|
|
input [2:0]picture_coding_type; // identifies whether a picture is an I, P or B picture.
|
49 |
|
|
input progressive_sequence;
|
50 |
|
|
input progressive_frame;
|
51 |
|
|
input top_field_first;
|
52 |
|
|
input repeat_first_field;
|
53 |
|
|
input last_frame; // asserted when frame is the last frame of a bitstream
|
54 |
|
|
|
55 |
|
|
/* saved values */
|
56 |
|
|
reg [2:0]vld_picture_coding_type;
|
57 |
|
|
reg vld_progressive_sequence;
|
58 |
|
|
reg vld_progressive_frame;
|
59 |
|
|
reg vld_top_field_first;
|
60 |
|
|
reg vld_repeat_first_field;
|
61 |
|
|
reg vld_last_frame;
|
62 |
|
|
|
63 |
|
|
/* picture buffers */
|
64 |
|
|
output reg [2:0]forward_reference_frame; /* forward reference frame. Has value 2'd0 or 2'd1 */
|
65 |
|
|
output reg [2:0]backward_reference_frame; /* backward reference frame. Has value 2'd0 or 2'd1 */
|
66 |
|
|
|
67 |
|
|
reg [2:0]aux_frame; /* auxiliary frame 1. Has value 2'd2 or 2'd3 */
|
68 |
|
|
reg [2:0]prev_aux_frame;
|
69 |
|
|
|
70 |
|
|
output reg [2:0]current_frame; /* current frame being decoded */
|
71 |
|
|
reg current_frame_valid;
|
72 |
|
|
reg [2:0]current_frame_coding_type; /* I, P or B-TYPE */
|
73 |
|
|
reg current_frame_progressive_sequence;
|
74 |
|
|
reg current_frame_progressive_frame;
|
75 |
|
|
reg current_frame_top_field_first;
|
76 |
|
|
reg current_frame_repeat_first_field;
|
77 |
|
|
|
78 |
|
|
output reg [2:0]output_frame; /* frame being displayed */
|
79 |
|
|
output reg output_frame_valid; /* asserted during one clock cycle */
|
80 |
|
|
input output_frame_rd; /* asserted when output_frame read */
|
81 |
|
|
|
82 |
|
|
output reg output_progressive_sequence;
|
83 |
|
|
output reg output_progressive_frame;
|
84 |
|
|
output reg output_top_field_first;
|
85 |
|
|
output reg output_repeat_first_field;
|
86 |
|
|
output reg picbuf_busy;
|
87 |
|
|
|
88 |
|
|
input update_picture_buffers;
|
89 |
|
|
|
90 |
|
|
reg prev_output_frame_valid;
|
91 |
|
|
|
92 |
|
|
reg [2:0]prev_i_p_frame; /* previous I or P frame */
|
93 |
|
|
reg prev_i_p_frame_valid; /* high if previous I or P frame exists, low when no previous I or P frame exists (at video sequence start) */
|
94 |
|
|
reg prev_i_p_frame_progressive_sequence;
|
95 |
|
|
reg prev_i_p_frame_progressive_frame;
|
96 |
|
|
reg prev_i_p_frame_top_field_first;
|
97 |
|
|
reg prev_i_p_frame_repeat_first_field;
|
98 |
|
|
|
99 |
|
|
`include "vld_codes.v"
|
100 |
|
|
|
101 |
|
|
parameter [3:0]
|
102 |
|
|
STATE_IDLE = 4'h0,
|
103 |
|
|
STATE_UPDATE = 4'h1,
|
104 |
|
|
STATE_IP_FRAME_0 = 4'h2,
|
105 |
|
|
STATE_IP_FRAME_1 = 4'h3,
|
106 |
|
|
STATE_B_FRAME_0 = 4'h4,
|
107 |
|
|
STATE_B_FRAME_1 = 4'h5,
|
108 |
|
|
STATE_LAST_FRAME = 4'h6,
|
109 |
|
|
STATE_WAIT_0 = 4'h7,
|
110 |
|
|
STATE_WAIT_1 = 4'h8,
|
111 |
|
|
STATE_SRC_SELECT_0 = 4'h9,
|
112 |
|
|
STATE_SRC_SELECT_1 = 4'ha;
|
113 |
|
|
|
114 |
|
|
reg [3:0]state;
|
115 |
|
|
reg [3:0]next;
|
116 |
|
|
|
117 |
|
|
/*
|
118 |
|
|
* We need to synchronize motion compensation (computing the next image) and
|
119 |
|
|
* resampling (showing the previous image). These processes run at different
|
120 |
|
|
* speeds.
|
121 |
|
|
*
|
122 |
|
|
* goals:
|
123 |
|
|
* - start decoding the next picture as soon as possible
|
124 |
|
|
* - don't overwrite a picture which still needs to be displayed
|
125 |
|
|
*
|
126 |
|
|
* if (output_frame_rd == 1'b1): resampling finished the previous image, and wants the next image.
|
127 |
|
|
* if (picbuf_busy == 1'b1): next image computed.
|
128 |
|
|
*/
|
129 |
|
|
|
130 |
|
|
/* next state */
|
131 |
|
|
always @*
|
132 |
|
|
case (state)
|
133 |
|
|
/* Wait until next frame decoded (picbuf_busy asserted) */
|
134 |
|
|
STATE_IDLE: if ((source_select != 3'd0) && ~source_select[2]) next = STATE_IDLE; // blank screen if source_select == 3'd1..3
|
135 |
|
|
else if ((source_select != 3'd0) && source_select[2])next = STATE_SRC_SELECT_0; // next image forced through "source select" if source_select == 3'd4..7
|
136 |
|
|
else if (picbuf_busy) next = STATE_UPDATE; // next frame
|
137 |
|
|
else next = STATE_IDLE; // blank screen
|
138 |
|
|
|
139 |
|
|
/* update forward reference frame, backward reference frame, current frame, output frame. */
|
140 |
|
|
STATE_UPDATE: if (vld_last_frame) next = STATE_LAST_FRAME;
|
141 |
|
|
else if (vld_picture_coding_type == B_TYPE) next = STATE_B_FRAME_0; /* next frame is B-frame */
|
142 |
|
|
else next = STATE_IP_FRAME_0; /* next frame is I- or P-frame */
|
143 |
|
|
|
144 |
|
|
/* I-frame. wait until resample has finished showing previous frame, and wants to show this frame */
|
145 |
|
|
STATE_IP_FRAME_0: if (~output_frame_valid) next = STATE_IP_FRAME_1; // there's no previous I/P frame to show, start decoding right away
|
146 |
|
|
else if (output_frame_rd) next = STATE_IP_FRAME_1; // resample has picked up previous I/P frame, start decoding
|
147 |
|
|
else next = STATE_IP_FRAME_0;
|
148 |
|
|
|
149 |
|
|
/* drop picbuf_busy. start decoding I or P-frame */
|
150 |
|
|
STATE_IP_FRAME_1: next = STATE_WAIT_1; // wait until resample has finished showing previous I- or P-frame
|
151 |
|
|
|
152 |
|
|
/* B-frame. drop picbuf_busy, start decoding B-frame */
|
153 |
|
|
STATE_B_FRAME_0: next = STATE_B_FRAME_1;
|
154 |
|
|
|
155 |
|
|
/* wait until B-frame has been decoded (picbuf_busy high). Then raise output_frame_valid. */
|
156 |
|
|
STATE_B_FRAME_1: if (picbuf_busy) next = STATE_WAIT_0;
|
157 |
|
|
else next = STATE_B_FRAME_1;
|
158 |
|
|
|
159 |
|
|
STATE_LAST_FRAME: next = STATE_WAIT_0; // show last frame; start decoding next video
|
160 |
|
|
|
161 |
|
|
/* wait until resample has finished showing the previous frame, and wants to show this frame */
|
162 |
|
|
STATE_WAIT_0: if (~output_frame_valid) next = STATE_IDLE;
|
163 |
|
|
else if (output_frame_rd) next = STATE_WAIT_1;
|
164 |
|
|
else next = STATE_WAIT_0;
|
165 |
|
|
|
166 |
|
|
/* drop output_frame_valid, wait for output_frame_rd to drop */
|
167 |
|
|
STATE_WAIT_1: if (~output_frame_rd) next = STATE_IDLE;
|
168 |
|
|
else next = STATE_WAIT_1;
|
169 |
|
|
|
170 |
|
|
/* source_select is set. display selected frame and raise output_frame_valid. wait for resample to assert output_frame_rd */
|
171 |
|
|
STATE_SRC_SELECT_0: if (output_frame_rd) next = STATE_SRC_SELECT_1;
|
172 |
|
|
else next = STATE_SRC_SELECT_0;
|
173 |
|
|
|
174 |
|
|
/* drop output_frame_valid. wait for resample to drop output_frame_rd; then go to the next frame */
|
175 |
|
|
STATE_SRC_SELECT_1: if (~output_frame_rd) next = STATE_IDLE;
|
176 |
|
|
else next = STATE_SRC_SELECT_1;
|
177 |
|
|
|
178 |
|
|
default next = STATE_IDLE;
|
179 |
|
|
endcase
|
180 |
|
|
|
181 |
|
|
/* state */
|
182 |
|
|
always @(posedge clk)
|
183 |
|
|
if(~rst) state <= STATE_IDLE;
|
184 |
|
|
else if (clk_en) state <= next;
|
185 |
|
|
else state <= state;
|
186 |
|
|
|
187 |
|
|
/*
|
188 |
|
|
* clock enable for variable-length decoding;
|
189 |
|
|
* pause variable-length decoding
|
190 |
|
|
*
|
191 |
|
|
*/
|
192 |
|
|
|
193 |
|
|
always @(posedge clk)
|
194 |
|
|
if (~rst) picbuf_busy <= 1'd0;
|
195 |
|
|
else if (clk_en && update_picture_buffers) picbuf_busy <= 1'b1;
|
196 |
|
|
else if (clk_en && ((state == STATE_IP_FRAME_1) || (state == STATE_B_FRAME_0) || (state == STATE_LAST_FRAME))) picbuf_busy <= 1'b0;
|
197 |
|
|
else picbuf_busy <= picbuf_busy;
|
198 |
|
|
|
199 |
|
|
/*
|
200 |
|
|
* Save vld values
|
201 |
|
|
*/
|
202 |
|
|
|
203 |
|
|
always @(posedge clk)
|
204 |
|
|
if (~rst)
|
205 |
|
|
begin
|
206 |
|
|
vld_picture_coding_type <= 3'd0;
|
207 |
|
|
vld_progressive_sequence <= 1'b0;
|
208 |
|
|
vld_progressive_frame <= 1'b0;
|
209 |
|
|
vld_top_field_first <= 1'b0;
|
210 |
|
|
vld_repeat_first_field <= 1'b0;
|
211 |
|
|
vld_last_frame <= 1'b0;
|
212 |
|
|
end
|
213 |
|
|
else if (clk_en && update_picture_buffers)
|
214 |
|
|
begin
|
215 |
|
|
vld_picture_coding_type <= picture_coding_type;
|
216 |
|
|
vld_progressive_sequence <= progressive_sequence;
|
217 |
|
|
vld_progressive_frame <= progressive_frame;
|
218 |
|
|
vld_top_field_first <= top_field_first;
|
219 |
|
|
vld_repeat_first_field <= repeat_first_field;
|
220 |
|
|
vld_last_frame <= last_frame;
|
221 |
|
|
end
|
222 |
|
|
else
|
223 |
|
|
begin
|
224 |
|
|
vld_picture_coding_type <= vld_picture_coding_type;
|
225 |
|
|
vld_progressive_sequence <= vld_progressive_sequence;
|
226 |
|
|
vld_progressive_frame <= vld_progressive_frame;
|
227 |
|
|
vld_top_field_first <= vld_top_field_first;
|
228 |
|
|
vld_repeat_first_field <= vld_repeat_first_field;
|
229 |
|
|
vld_last_frame <= vld_last_frame;
|
230 |
|
|
end
|
231 |
|
|
|
232 |
|
|
/*
|
233 |
|
|
* current frame
|
234 |
|
|
* If we're decoding a B picture, we store it in the auxiliary frame.
|
235 |
|
|
* This is acceptable as no future pictures will use the B picture as reference frame.
|
236 |
|
|
*/
|
237 |
|
|
|
238 |
|
|
always @(posedge clk)
|
239 |
|
|
if (~rst)
|
240 |
|
|
begin
|
241 |
|
|
current_frame <= 3'b0;
|
242 |
|
|
current_frame_valid <= 1'b0;
|
243 |
|
|
current_frame_coding_type <= I_TYPE;
|
244 |
|
|
current_frame_progressive_sequence <= 1'b0;
|
245 |
|
|
current_frame_progressive_frame <= 1'b0;
|
246 |
|
|
current_frame_top_field_first <= 1'b0;
|
247 |
|
|
current_frame_repeat_first_field <= 1'b0;
|
248 |
|
|
end
|
249 |
|
|
else if (clk_en && (state == STATE_UPDATE) && (vld_picture_coding_type != B_TYPE) && ~vld_last_frame)
|
250 |
|
|
begin
|
251 |
|
|
current_frame <= forward_reference_frame;
|
252 |
|
|
current_frame_valid <= 1'b1;
|
253 |
|
|
current_frame_coding_type <= vld_picture_coding_type;
|
254 |
|
|
current_frame_progressive_sequence <= vld_progressive_sequence;
|
255 |
|
|
current_frame_progressive_frame <= vld_progressive_frame;
|
256 |
|
|
current_frame_top_field_first <= vld_top_field_first;
|
257 |
|
|
current_frame_repeat_first_field <= vld_repeat_first_field;
|
258 |
|
|
end
|
259 |
|
|
else if (clk_en && (state == STATE_UPDATE) && (vld_picture_coding_type == B_TYPE) && ~vld_last_frame)
|
260 |
|
|
begin
|
261 |
|
|
current_frame <= aux_frame;
|
262 |
|
|
current_frame_valid <= 1'b1;
|
263 |
|
|
current_frame_coding_type <= vld_picture_coding_type;
|
264 |
|
|
current_frame_progressive_sequence <= vld_progressive_sequence;
|
265 |
|
|
current_frame_progressive_frame <= vld_progressive_frame;
|
266 |
|
|
current_frame_top_field_first <= vld_top_field_first;
|
267 |
|
|
current_frame_repeat_first_field <= vld_repeat_first_field;
|
268 |
|
|
end
|
269 |
|
|
else
|
270 |
|
|
begin
|
271 |
|
|
current_frame <= current_frame;
|
272 |
|
|
current_frame_valid <= current_frame_valid;
|
273 |
|
|
current_frame_coding_type <= current_frame_coding_type;
|
274 |
|
|
current_frame_progressive_sequence <= current_frame_progressive_sequence;
|
275 |
|
|
current_frame_progressive_frame <= current_frame_progressive_frame;
|
276 |
|
|
current_frame_top_field_first <= current_frame_top_field_first;
|
277 |
|
|
current_frame_repeat_first_field <= current_frame_repeat_first_field;
|
278 |
|
|
end
|
279 |
|
|
|
280 |
|
|
/*
|
281 |
|
|
If we are decoding an I or P picture,
|
282 |
|
|
the old backward reference frame becomes the new forward reference frame,
|
283 |
|
|
while the old forward reference frame is overwritten with the current frame and becomes the new backward reference frame.
|
284 |
|
|
This is ok as decoding the I or P pictures doesn't need the old backward reference frame,
|
285 |
|
|
and subsequent B pictures will use the I or P picture being decoded as backward reference frame.
|
286 |
|
|
*/
|
287 |
|
|
|
288 |
|
|
always @(posedge clk)
|
289 |
|
|
if (~rst) forward_reference_frame <= 3'd0;
|
290 |
|
|
else if (clk_en && (state == STATE_UPDATE) && (vld_picture_coding_type != B_TYPE)) forward_reference_frame <= backward_reference_frame;
|
291 |
|
|
else forward_reference_frame <= forward_reference_frame;
|
292 |
|
|
|
293 |
|
|
always @(posedge clk)
|
294 |
|
|
if (~rst) backward_reference_frame <= 3'd1;
|
295 |
|
|
else if (clk_en && (state == STATE_UPDATE) && (vld_picture_coding_type != B_TYPE)) backward_reference_frame <= forward_reference_frame;
|
296 |
|
|
else backward_reference_frame <= backward_reference_frame;
|
297 |
|
|
|
298 |
|
|
always @(posedge clk)
|
299 |
|
|
if (~rst) aux_frame <= 3'd2;
|
300 |
|
|
else if (clk_en && (state == STATE_UPDATE) && (vld_picture_coding_type == B_TYPE)) aux_frame <= prev_aux_frame;
|
301 |
|
|
else aux_frame <= aux_frame;
|
302 |
|
|
|
303 |
|
|
always @(posedge clk)
|
304 |
|
|
if (~rst) prev_aux_frame <= 3'd3;
|
305 |
|
|
else if (clk_en && (state == STATE_UPDATE) && (vld_picture_coding_type == B_TYPE)) prev_aux_frame <= aux_frame;
|
306 |
|
|
else prev_aux_frame <= prev_aux_frame;
|
307 |
|
|
|
308 |
|
|
/*
|
309 |
|
|
* prev_i_p_frame stores the previous I or P frame.
|
310 |
|
|
* prev_i_p_frame_valid is zero if no previous I or P frame exists, e,g, at video start.
|
311 |
|
|
*/
|
312 |
|
|
|
313 |
|
|
always @(posedge clk)
|
314 |
|
|
if (~rst)
|
315 |
|
|
begin
|
316 |
|
|
prev_i_p_frame <= 3'b0;
|
317 |
|
|
prev_i_p_frame_valid <= 1'b0;
|
318 |
|
|
prev_i_p_frame_progressive_sequence <= 1'b0;
|
319 |
|
|
prev_i_p_frame_progressive_frame <= 1'b0;
|
320 |
|
|
prev_i_p_frame_top_field_first <= 1'b0;
|
321 |
|
|
prev_i_p_frame_repeat_first_field <= 1'b0;
|
322 |
|
|
end
|
323 |
|
|
else if (clk_en && update_picture_buffers && (current_frame_coding_type != B_TYPE) && ~vld_last_frame)
|
324 |
|
|
begin
|
325 |
|
|
prev_i_p_frame <= current_frame;
|
326 |
|
|
prev_i_p_frame_valid <= current_frame_valid;
|
327 |
|
|
prev_i_p_frame_progressive_sequence <= current_frame_progressive_sequence;
|
328 |
|
|
prev_i_p_frame_progressive_frame <= current_frame_progressive_frame;
|
329 |
|
|
prev_i_p_frame_top_field_first <= current_frame_top_field_first;
|
330 |
|
|
prev_i_p_frame_repeat_first_field <= current_frame_repeat_first_field;
|
331 |
|
|
end
|
332 |
|
|
/*
|
333 |
|
|
* Clear prev_i_p_frame_valid, in case a (new) mpeg2 stream comes after the last frame
|
334 |
|
|
*/
|
335 |
|
|
else if (clk_en && (state == STATE_LAST_FRAME))
|
336 |
|
|
begin
|
337 |
|
|
prev_i_p_frame <= prev_i_p_frame;
|
338 |
|
|
prev_i_p_frame_valid <= 1'b0;
|
339 |
|
|
prev_i_p_frame_progressive_sequence <= prev_i_p_frame_progressive_sequence;
|
340 |
|
|
prev_i_p_frame_progressive_frame <= prev_i_p_frame_progressive_frame;
|
341 |
|
|
prev_i_p_frame_top_field_first <= prev_i_p_frame_top_field_first;
|
342 |
|
|
prev_i_p_frame_repeat_first_field <= prev_i_p_frame_repeat_first_field;
|
343 |
|
|
end
|
344 |
|
|
else
|
345 |
|
|
begin
|
346 |
|
|
prev_i_p_frame <= prev_i_p_frame;
|
347 |
|
|
prev_i_p_frame_valid <= prev_i_p_frame_valid;
|
348 |
|
|
prev_i_p_frame_progressive_sequence <= prev_i_p_frame_progressive_sequence;
|
349 |
|
|
prev_i_p_frame_progressive_frame <= prev_i_p_frame_progressive_frame;
|
350 |
|
|
prev_i_p_frame_top_field_first <= prev_i_p_frame_top_field_first;
|
351 |
|
|
prev_i_p_frame_repeat_first_field <= prev_i_p_frame_repeat_first_field;
|
352 |
|
|
end
|
353 |
|
|
|
354 |
|
|
/*
|
355 |
|
|
* output frame selection
|
356 |
|
|
*
|
357 |
|
|
* par. 6.1.1.11 Frame reordering
|
358 |
|
|
*
|
359 |
|
|
* ... sequence re-ordering is performed according to the following rules:
|
360 |
|
|
* If the current frame in coded order is a B-frame the output frame is the frame reconstructed from that B-frame.
|
361 |
|
|
* If the current frame in coded order is a I-frame or P-frame the output frame is the frame reconstructed
|
362 |
|
|
* from the previous I-frame or P-frame if one exists. If none exists, at the start of the sequence, no frame is output.
|
363 |
|
|
* The frame reconstructed from the final I-frame or P-frame in the sequence is output immediately after the
|
364 |
|
|
* frame reconstructed when the last coded frame in the sequence was removed from the VBV buffer.
|
365 |
|
|
*
|
366 |
|
|
*/
|
367 |
|
|
|
368 |
|
|
always @(posedge clk)
|
369 |
|
|
if (~rst)
|
370 |
|
|
begin
|
371 |
|
|
output_frame <= 3'd0;
|
372 |
|
|
output_frame_valid <= 1'b0;
|
373 |
|
|
output_progressive_sequence <= 1'b0;
|
374 |
|
|
output_progressive_frame <= 1'b0;
|
375 |
|
|
output_top_field_first <= 1'b0;
|
376 |
|
|
output_repeat_first_field <= 1'b0;
|
377 |
|
|
prev_output_frame_valid <= 1'b0;
|
378 |
|
|
end
|
379 |
|
|
else if (clk_en && (state == STATE_IDLE))
|
380 |
|
|
begin
|
381 |
|
|
output_frame <= output_frame;
|
382 |
|
|
output_frame_valid <= 1'b0;
|
383 |
|
|
output_progressive_sequence <= output_progressive_sequence;
|
384 |
|
|
output_progressive_frame <= output_progressive_frame;
|
385 |
|
|
output_top_field_first <= output_top_field_first;
|
386 |
|
|
output_repeat_first_field <= output_repeat_first_field;
|
387 |
|
|
prev_output_frame_valid <= prev_output_frame_valid;
|
388 |
|
|
end
|
389 |
|
|
/*
|
390 |
|
|
* Emit last frame of sequence
|
391 |
|
|
*/
|
392 |
|
|
else if (clk_en && (state == STATE_LAST_FRAME)) /* emit last frame: Emit last decoded I or P frame */
|
393 |
|
|
begin
|
394 |
|
|
output_frame <= prev_i_p_frame;
|
395 |
|
|
output_frame_valid <= prev_i_p_frame_valid;
|
396 |
|
|
output_progressive_sequence <= prev_i_p_frame_progressive_sequence;
|
397 |
|
|
output_progressive_frame <= prev_i_p_frame_progressive_frame;
|
398 |
|
|
output_top_field_first <= prev_i_p_frame_top_field_first;
|
399 |
|
|
output_repeat_first_field <= prev_i_p_frame_repeat_first_field;
|
400 |
|
|
prev_output_frame_valid <= prev_i_p_frame_valid;
|
401 |
|
|
end
|
402 |
|
|
/*
|
403 |
|
|
* Emit B-frame
|
404 |
|
|
*/
|
405 |
|
|
else if (clk_en && (state == STATE_B_FRAME_1) && (next == STATE_WAIT_0))
|
406 |
|
|
begin
|
407 |
|
|
output_frame <= current_frame;
|
408 |
|
|
output_frame_valid <= current_frame_valid;
|
409 |
|
|
output_progressive_sequence <= current_frame_progressive_sequence;
|
410 |
|
|
output_progressive_frame <= current_frame_progressive_frame;
|
411 |
|
|
output_top_field_first <= current_frame_top_field_first;
|
412 |
|
|
output_repeat_first_field <= current_frame_repeat_first_field;
|
413 |
|
|
prev_output_frame_valid <= current_frame_valid;
|
414 |
|
|
end
|
415 |
|
|
/*
|
416 |
|
|
* Emit I-/P-frame
|
417 |
|
|
*/
|
418 |
|
|
else if (clk_en && (state == STATE_UPDATE) && (next == STATE_IP_FRAME_0))
|
419 |
|
|
begin
|
420 |
|
|
output_frame <= prev_i_p_frame;
|
421 |
|
|
output_frame_valid <= prev_i_p_frame_valid;
|
422 |
|
|
output_progressive_sequence <= prev_i_p_frame_progressive_sequence;
|
423 |
|
|
output_progressive_frame <= prev_i_p_frame_progressive_frame;
|
424 |
|
|
output_top_field_first <= prev_i_p_frame_top_field_first;
|
425 |
|
|
output_repeat_first_field <= prev_i_p_frame_repeat_first_field;
|
426 |
|
|
prev_output_frame_valid <= prev_i_p_frame_valid;
|
427 |
|
|
end
|
428 |
|
|
/*
|
429 |
|
|
* Housekeeping
|
430 |
|
|
*/
|
431 |
|
|
else if (clk_en && ((state == STATE_WAIT_1) || (state == STATE_SRC_SELECT_1) || (state == STATE_IP_FRAME_1)))
|
432 |
|
|
begin
|
433 |
|
|
output_frame <= output_frame;
|
434 |
|
|
output_frame_valid <= output_frame_rd; // drop output_frame_valid when output_frame_rd is lowered
|
435 |
|
|
output_progressive_sequence <= output_progressive_sequence;
|
436 |
|
|
output_progressive_frame <= output_progressive_frame;
|
437 |
|
|
output_top_field_first <= output_top_field_first;
|
438 |
|
|
output_repeat_first_field <= output_repeat_first_field;
|
439 |
|
|
prev_output_frame_valid <= prev_output_frame_valid;
|
440 |
|
|
end
|
441 |
|
|
/*
|
442 |
|
|
* source select
|
443 |
|
|
*/
|
444 |
|
|
else if (clk_en && (state == STATE_SRC_SELECT_0)) /* force frame */
|
445 |
|
|
begin
|
446 |
|
|
output_frame <= source_select[1:0];
|
447 |
|
|
output_frame_valid <= source_select[2];
|
448 |
|
|
output_progressive_sequence <= progressive_sequence;
|
449 |
|
|
output_progressive_frame <= progressive_frame;
|
450 |
|
|
output_top_field_first <= top_field_first;
|
451 |
|
|
output_repeat_first_field <= 1'b0;
|
452 |
|
|
prev_output_frame_valid <= source_select[2];
|
453 |
|
|
end
|
454 |
|
|
else /* no change */
|
455 |
|
|
begin
|
456 |
|
|
output_frame <= output_frame;
|
457 |
|
|
output_frame_valid <= output_frame_valid;
|
458 |
|
|
output_progressive_sequence <= output_progressive_sequence;
|
459 |
|
|
output_progressive_frame <= output_progressive_frame;
|
460 |
|
|
output_top_field_first <= output_top_field_first;
|
461 |
|
|
output_repeat_first_field <= output_repeat_first_field;
|
462 |
|
|
prev_output_frame_valid <= prev_output_frame_valid;
|
463 |
|
|
end
|
464 |
|
|
|
465 |
|
|
`ifdef DEBUG
|
466 |
|
|
|
467 |
|
|
always @(posedge clk)
|
468 |
|
|
case (state)
|
469 |
|
|
STATE_IDLE: #0 $display("%m\tSTATE_IDLE");
|
470 |
|
|
STATE_UPDATE: #0 $display("%m\tSTATE_UPDATE");
|
471 |
|
|
STATE_IP_FRAME_0: #0 $display("%m\tSTATE_IP_FRAME_0");
|
472 |
|
|
STATE_IP_FRAME_1: #0 $display("%m\tSTATE_IP_FRAME_1");
|
473 |
|
|
STATE_B_FRAME_0: #0 $display("%m\tSTATE_B_FRAME_0");
|
474 |
|
|
STATE_B_FRAME_1: #0 $display("%m\tSTATE_B_FRAME_1");
|
475 |
|
|
STATE_WAIT_0: #0 $display("%m\tSTATE_WAIT_0");
|
476 |
|
|
STATE_WAIT_1: #0 $display("%m\tSTATE_WAIT_1");
|
477 |
|
|
STATE_SRC_SELECT_0: #0 $display("%m\tSTATE_SRC_SELECT_0");
|
478 |
|
|
STATE_SRC_SELECT_1: #0 $display("%m\tSTATE_SRC_SELECT_1");
|
479 |
|
|
default #0 $display("%m\t*** Error: unknown state %d", state);
|
480 |
|
|
endcase
|
481 |
|
|
|
482 |
|
|
always @(posedge clk)
|
483 |
|
|
begin
|
484 |
|
|
$strobe("%m\tupdate_picture_buffers: %d output_frame_rd: %d picbuf_busy: %d",
|
485 |
|
|
update_picture_buffers, output_frame_rd, picbuf_busy);
|
486 |
|
|
|
487 |
|
|
$strobe("%m\tforward_reference_frame: %d backward_reference_frame: %d aux_frame: %d prev_aux_frame: %d current_frame: %d",
|
488 |
|
|
forward_reference_frame, backward_reference_frame, aux_frame, prev_aux_frame, current_frame);
|
489 |
|
|
|
490 |
|
|
$strobe("%m\tcurrent_frame_coding_type: %d output_frame: %d output_frame_valid: %d prev_i_p_frame: %d prev_i_p_frame_valid: %d",
|
491 |
|
|
current_frame_coding_type, output_frame, output_frame_valid, prev_i_p_frame, prev_i_p_frame_valid);
|
492 |
|
|
end
|
493 |
|
|
|
494 |
|
|
`endif
|
495 |
|
|
endmodule
|
496 |
|
|
|
497 |
|
|
/* not truncated */
|