OLD | NEW |
| (Empty) |
1 /* | |
2 * Copyright (c) 2010 The WebM project authors. All Rights Reserved. | |
3 * | |
4 * Use of this source code is governed by a BSD-style license | |
5 * that can be found in the LICENSE file in the root of the source | |
6 * tree. An additional intellectual property rights grant can be found | |
7 * in the file PATENTS. All contributing project authors may | |
8 * be found in the AUTHORS file in the root of the source tree. | |
9 */ | |
10 | |
11 #include <assert.h> | |
12 #include <limits.h> | |
13 #include <stdio.h> | |
14 | |
15 #include "vp9/common/vp9_onyxc_int.h" | |
16 #if CONFIG_VP9_POSTPROC | |
17 #include "vp9/common/vp9_postproc.h" | |
18 #endif | |
19 #include "vp9/decoder/vp9_onyxd.h" | |
20 #include "vp9/decoder/vp9_onyxd_int.h" | |
21 #include "vpx_mem/vpx_mem.h" | |
22 #include "vp9/common/vp9_alloccommon.h" | |
23 #include "vp9/common/vp9_loopfilter.h" | |
24 #include "vp9/common/vp9_quant_common.h" | |
25 #include "vpx_scale/vpx_scale.h" | |
26 #include "vp9/common/vp9_systemdependent.h" | |
27 #include "vpx_ports/vpx_timer.h" | |
28 #include "vp9/decoder/vp9_decodeframe.h" | |
29 #include "vp9/decoder/vp9_detokenize.h" | |
30 #include "vp9/decoder/vp9_dthread.h" | |
31 #include "./vpx_scale_rtcd.h" | |
32 | |
33 #define WRITE_RECON_BUFFER 0 | |
34 #if WRITE_RECON_BUFFER == 1 | |
35 static void recon_write_yuv_frame(const char *name, | |
36 const YV12_BUFFER_CONFIG *s, | |
37 int w, int _h) { | |
38 FILE *yuv_file = fopen(name, "ab"); | |
39 const uint8_t *src = s->y_buffer; | |
40 int h = _h; | |
41 | |
42 do { | |
43 fwrite(src, w, 1, yuv_file); | |
44 src += s->y_stride; | |
45 } while (--h); | |
46 | |
47 src = s->u_buffer; | |
48 h = (_h + 1) >> 1; | |
49 w = (w + 1) >> 1; | |
50 | |
51 do { | |
52 fwrite(src, w, 1, yuv_file); | |
53 src += s->uv_stride; | |
54 } while (--h); | |
55 | |
56 src = s->v_buffer; | |
57 h = (_h + 1) >> 1; | |
58 | |
59 do { | |
60 fwrite(src, w, 1, yuv_file); | |
61 src += s->uv_stride; | |
62 } while (--h); | |
63 | |
64 fclose(yuv_file); | |
65 } | |
66 #endif | |
67 #if WRITE_RECON_BUFFER == 2 | |
68 void write_dx_frame_to_file(YV12_BUFFER_CONFIG *frame, int this_frame) { | |
69 // write the frame | |
70 FILE *yframe; | |
71 int i; | |
72 char filename[255]; | |
73 | |
74 snprintf(filename, sizeof(filename)-1, "dx\\y%04d.raw", this_frame); | |
75 yframe = fopen(filename, "wb"); | |
76 | |
77 for (i = 0; i < frame->y_height; i++) | |
78 fwrite(frame->y_buffer + i * frame->y_stride, | |
79 frame->y_width, 1, yframe); | |
80 | |
81 fclose(yframe); | |
82 snprintf(filename, sizeof(filename)-1, "dx\\u%04d.raw", this_frame); | |
83 yframe = fopen(filename, "wb"); | |
84 | |
85 for (i = 0; i < frame->uv_height; i++) | |
86 fwrite(frame->u_buffer + i * frame->uv_stride, | |
87 frame->uv_width, 1, yframe); | |
88 | |
89 fclose(yframe); | |
90 snprintf(filename, sizeof(filename)-1, "dx\\v%04d.raw", this_frame); | |
91 yframe = fopen(filename, "wb"); | |
92 | |
93 for (i = 0; i < frame->uv_height; i++) | |
94 fwrite(frame->v_buffer + i * frame->uv_stride, | |
95 frame->uv_width, 1, yframe); | |
96 | |
97 fclose(yframe); | |
98 } | |
99 #endif | |
100 | |
101 void vp9_initialize_dec() { | |
102 static int init_done = 0; | |
103 | |
104 if (!init_done) { | |
105 vp9_initialize_common(); | |
106 vp9_init_quant_tables(); | |
107 init_done = 1; | |
108 } | |
109 } | |
110 | |
111 static void init_macroblockd(VP9D_COMP *const pbi) { | |
112 MACROBLOCKD *xd = &pbi->mb; | |
113 struct macroblockd_plane *const pd = xd->plane; | |
114 int i; | |
115 | |
116 for (i = 0; i < MAX_MB_PLANE; ++i) | |
117 pd[i].dqcoeff = pbi->dqcoeff[i]; | |
118 } | |
119 | |
120 VP9D_COMP *vp9_create_decompressor(VP9D_CONFIG *oxcf) { | |
121 VP9D_COMP *const pbi = vpx_memalign(32, sizeof(VP9D_COMP)); | |
122 VP9_COMMON *const cm = pbi ? &pbi->common : NULL; | |
123 | |
124 if (!cm) | |
125 return NULL; | |
126 | |
127 vp9_zero(*pbi); | |
128 | |
129 // Initialize the references to not point to any frame buffers. | |
130 memset(&cm->ref_frame_map, -1, sizeof(cm->ref_frame_map)); | |
131 | |
132 if (setjmp(cm->error.jmp)) { | |
133 cm->error.setjmp = 0; | |
134 vp9_remove_decompressor(pbi); | |
135 return NULL; | |
136 } | |
137 | |
138 cm->error.setjmp = 1; | |
139 vp9_initialize_dec(); | |
140 | |
141 vp9_rtcd(); | |
142 | |
143 pbi->oxcf = *oxcf; | |
144 pbi->ready_for_new_data = 1; | |
145 cm->current_video_frame = 0; | |
146 | |
147 // vp9_init_dequantizer() is first called here. Add check in | |
148 // frame_init_dequantizer() to avoid unnecessary calling of | |
149 // vp9_init_dequantizer() for every frame. | |
150 vp9_init_dequantizer(cm); | |
151 | |
152 vp9_loop_filter_init(cm); | |
153 | |
154 cm->error.setjmp = 0; | |
155 pbi->decoded_key_frame = 0; | |
156 | |
157 init_macroblockd(pbi); | |
158 | |
159 vp9_worker_init(&pbi->lf_worker); | |
160 | |
161 return pbi; | |
162 } | |
163 | |
164 void vp9_remove_decompressor(VP9D_COMP *pbi) { | |
165 int i; | |
166 | |
167 if (!pbi) | |
168 return; | |
169 | |
170 vp9_remove_common(&pbi->common); | |
171 vp9_worker_end(&pbi->lf_worker); | |
172 vpx_free(pbi->lf_worker.data1); | |
173 for (i = 0; i < pbi->num_tile_workers; ++i) { | |
174 VP9Worker *const worker = &pbi->tile_workers[i]; | |
175 vp9_worker_end(worker); | |
176 vpx_free(worker->data1); | |
177 vpx_free(worker->data2); | |
178 } | |
179 vpx_free(pbi->tile_workers); | |
180 | |
181 if (pbi->num_tile_workers) { | |
182 VP9_COMMON *const cm = &pbi->common; | |
183 const int sb_rows = | |
184 mi_cols_aligned_to_sb(cm->mi_rows) >> MI_BLOCK_SIZE_LOG2; | |
185 VP9LfSync *const lf_sync = &pbi->lf_row_sync; | |
186 | |
187 vp9_loop_filter_dealloc(lf_sync, sb_rows); | |
188 } | |
189 | |
190 vpx_free(pbi->mi_streams); | |
191 vpx_free(pbi->above_context[0]); | |
192 vpx_free(pbi->above_seg_context); | |
193 vpx_free(pbi); | |
194 } | |
195 | |
196 static int equal_dimensions(const YV12_BUFFER_CONFIG *a, | |
197 const YV12_BUFFER_CONFIG *b) { | |
198 return a->y_height == b->y_height && a->y_width == b->y_width && | |
199 a->uv_height == b->uv_height && a->uv_width == b->uv_width; | |
200 } | |
201 | |
202 vpx_codec_err_t vp9_copy_reference_dec(VP9D_COMP *pbi, | |
203 VP9_REFFRAME ref_frame_flag, | |
204 YV12_BUFFER_CONFIG *sd) { | |
205 VP9_COMMON *cm = &pbi->common; | |
206 | |
207 /* TODO(jkoleszar): The decoder doesn't have any real knowledge of what the | |
208 * encoder is using the frame buffers for. This is just a stub to keep the | |
209 * vpxenc --test-decode functionality working, and will be replaced in a | |
210 * later commit that adds VP9-specific controls for this functionality. | |
211 */ | |
212 if (ref_frame_flag == VP9_LAST_FLAG) { | |
213 const YV12_BUFFER_CONFIG *const cfg = | |
214 &cm->frame_bufs[cm->ref_frame_map[0]].buf; | |
215 if (!equal_dimensions(cfg, sd)) | |
216 vpx_internal_error(&cm->error, VPX_CODEC_ERROR, | |
217 "Incorrect buffer dimensions"); | |
218 else | |
219 vp8_yv12_copy_frame(cfg, sd); | |
220 } else { | |
221 vpx_internal_error(&cm->error, VPX_CODEC_ERROR, | |
222 "Invalid reference frame"); | |
223 } | |
224 | |
225 return cm->error.error_code; | |
226 } | |
227 | |
228 | |
229 vpx_codec_err_t vp9_set_reference_dec(VP9D_COMP *pbi, | |
230 VP9_REFFRAME ref_frame_flag, | |
231 YV12_BUFFER_CONFIG *sd) { | |
232 VP9_COMMON *cm = &pbi->common; | |
233 RefBuffer *ref_buf = NULL; | |
234 | |
235 /* TODO(jkoleszar): The decoder doesn't have any real knowledge of what the | |
236 * encoder is using the frame buffers for. This is just a stub to keep the | |
237 * vpxenc --test-decode functionality working, and will be replaced in a | |
238 * later commit that adds VP9-specific controls for this functionality. | |
239 */ | |
240 if (ref_frame_flag == VP9_LAST_FLAG) { | |
241 ref_buf = &cm->frame_refs[0]; | |
242 } else if (ref_frame_flag == VP9_GOLD_FLAG) { | |
243 ref_buf = &cm->frame_refs[1]; | |
244 } else if (ref_frame_flag == VP9_ALT_FLAG) { | |
245 ref_buf = &cm->frame_refs[2]; | |
246 } else { | |
247 vpx_internal_error(&pbi->common.error, VPX_CODEC_ERROR, | |
248 "Invalid reference frame"); | |
249 return pbi->common.error.error_code; | |
250 } | |
251 | |
252 if (!equal_dimensions(ref_buf->buf, sd)) { | |
253 vpx_internal_error(&pbi->common.error, VPX_CODEC_ERROR, | |
254 "Incorrect buffer dimensions"); | |
255 } else { | |
256 int *ref_fb_ptr = &ref_buf->idx; | |
257 | |
258 // Find an empty frame buffer. | |
259 const int free_fb = get_free_fb(cm); | |
260 // Decrease ref_count since it will be increased again in | |
261 // ref_cnt_fb() below. | |
262 cm->frame_bufs[free_fb].ref_count--; | |
263 | |
264 // Manage the reference counters and copy image. | |
265 ref_cnt_fb(cm->frame_bufs, ref_fb_ptr, free_fb); | |
266 ref_buf->buf = &cm->frame_bufs[*ref_fb_ptr].buf; | |
267 vp8_yv12_copy_frame(sd, ref_buf->buf); | |
268 } | |
269 | |
270 return pbi->common.error.error_code; | |
271 } | |
272 | |
273 | |
274 int vp9_get_reference_dec(VP9D_COMP *pbi, int index, YV12_BUFFER_CONFIG **fb) { | |
275 VP9_COMMON *cm = &pbi->common; | |
276 | |
277 if (index < 0 || index >= REF_FRAMES) | |
278 return -1; | |
279 | |
280 *fb = &cm->frame_bufs[cm->ref_frame_map[index]].buf; | |
281 return 0; | |
282 } | |
283 | |
284 /* If any buffer updating is signaled it should be done here. */ | |
285 static void swap_frame_buffers(VP9D_COMP *pbi) { | |
286 int ref_index = 0, mask; | |
287 VP9_COMMON *const cm = &pbi->common; | |
288 | |
289 for (mask = pbi->refresh_frame_flags; mask; mask >>= 1) { | |
290 if (mask & 1) { | |
291 const int old_idx = cm->ref_frame_map[ref_index]; | |
292 ref_cnt_fb(cm->frame_bufs, &cm->ref_frame_map[ref_index], | |
293 cm->new_fb_idx); | |
294 if (old_idx >= 0 && cm->frame_bufs[old_idx].ref_count == 0) | |
295 cm->release_fb_cb(cm->cb_priv, | |
296 &cm->frame_bufs[old_idx].raw_frame_buffer); | |
297 } | |
298 ++ref_index; | |
299 } | |
300 | |
301 cm->frame_to_show = get_frame_new_buffer(cm); | |
302 cm->frame_bufs[cm->new_fb_idx].ref_count--; | |
303 | |
304 // Invalidate these references until the next frame starts. | |
305 for (ref_index = 0; ref_index < 3; ref_index++) | |
306 cm->frame_refs[ref_index].idx = INT_MAX; | |
307 } | |
308 | |
309 int vp9_receive_compressed_data(VP9D_COMP *pbi, | |
310 size_t size, const uint8_t **psource, | |
311 int64_t time_stamp) { | |
312 VP9_COMMON *cm = NULL; | |
313 const uint8_t *source = *psource; | |
314 int retcode = 0; | |
315 | |
316 /*if(pbi->ready_for_new_data == 0) | |
317 return -1;*/ | |
318 | |
319 if (!pbi) | |
320 return -1; | |
321 | |
322 cm = &pbi->common; | |
323 cm->error.error_code = VPX_CODEC_OK; | |
324 | |
325 pbi->source = source; | |
326 pbi->source_sz = size; | |
327 | |
328 if (pbi->source_sz == 0) { | |
329 /* This is used to signal that we are missing frames. | |
330 * We do not know if the missing frame(s) was supposed to update | |
331 * any of the reference buffers, but we act conservative and | |
332 * mark only the last buffer as corrupted. | |
333 * | |
334 * TODO(jkoleszar): Error concealment is undefined and non-normative | |
335 * at this point, but if it becomes so, [0] may not always be the correct | |
336 * thing to do here. | |
337 */ | |
338 if (cm->frame_refs[0].idx != INT_MAX) | |
339 cm->frame_refs[0].buf->corrupted = 1; | |
340 } | |
341 | |
342 // Check if the previous frame was a frame without any references to it. | |
343 if (cm->new_fb_idx >= 0 && cm->frame_bufs[cm->new_fb_idx].ref_count == 0) | |
344 cm->release_fb_cb(cm->cb_priv, | |
345 &cm->frame_bufs[cm->new_fb_idx].raw_frame_buffer); | |
346 cm->new_fb_idx = get_free_fb(cm); | |
347 | |
348 if (setjmp(cm->error.jmp)) { | |
349 cm->error.setjmp = 0; | |
350 | |
351 /* We do not know if the missing frame(s) was supposed to update | |
352 * any of the reference buffers, but we act conservative and | |
353 * mark only the last buffer as corrupted. | |
354 * | |
355 * TODO(jkoleszar): Error concealment is undefined and non-normative | |
356 * at this point, but if it becomes so, [0] may not always be the correct | |
357 * thing to do here. | |
358 */ | |
359 if (cm->frame_refs[0].idx != INT_MAX) | |
360 cm->frame_refs[0].buf->corrupted = 1; | |
361 | |
362 if (cm->frame_bufs[cm->new_fb_idx].ref_count > 0) | |
363 cm->frame_bufs[cm->new_fb_idx].ref_count--; | |
364 | |
365 return -1; | |
366 } | |
367 | |
368 cm->error.setjmp = 1; | |
369 | |
370 retcode = vp9_decode_frame(pbi, psource); | |
371 | |
372 if (retcode < 0) { | |
373 cm->error.error_code = VPX_CODEC_ERROR; | |
374 cm->error.setjmp = 0; | |
375 if (cm->frame_bufs[cm->new_fb_idx].ref_count > 0) | |
376 cm->frame_bufs[cm->new_fb_idx].ref_count--; | |
377 return retcode; | |
378 } | |
379 | |
380 swap_frame_buffers(pbi); | |
381 | |
382 #if WRITE_RECON_BUFFER == 2 | |
383 if (cm->show_frame) | |
384 write_dx_frame_to_file(cm->frame_to_show, | |
385 cm->current_video_frame); | |
386 else | |
387 write_dx_frame_to_file(cm->frame_to_show, | |
388 cm->current_video_frame + 1000); | |
389 #endif | |
390 | |
391 if (!pbi->do_loopfilter_inline) { | |
392 // If multiple threads are used to decode tiles, then we use those threads | |
393 // to do parallel loopfiltering. | |
394 if (pbi->num_tile_workers) { | |
395 vp9_loop_filter_frame_mt(pbi, cm, &pbi->mb, cm->lf.filter_level, 0, 0); | |
396 } else { | |
397 vp9_loop_filter_frame(cm, &pbi->mb, cm->lf.filter_level, 0, 0); | |
398 } | |
399 } | |
400 | |
401 #if WRITE_RECON_BUFFER == 2 | |
402 if (cm->show_frame) | |
403 write_dx_frame_to_file(cm->frame_to_show, | |
404 cm->current_video_frame + 2000); | |
405 else | |
406 write_dx_frame_to_file(cm->frame_to_show, | |
407 cm->current_video_frame + 3000); | |
408 #endif | |
409 | |
410 #if WRITE_RECON_BUFFER == 1 | |
411 if (cm->show_frame) | |
412 recon_write_yuv_frame("recon.yuv", cm->frame_to_show, | |
413 cm->width, cm->height); | |
414 #endif | |
415 | |
416 vp9_clear_system_state(); | |
417 | |
418 cm->last_width = cm->width; | |
419 cm->last_height = cm->height; | |
420 | |
421 if (!cm->show_existing_frame) | |
422 cm->last_show_frame = cm->show_frame; | |
423 if (cm->show_frame) { | |
424 if (!cm->show_existing_frame) { | |
425 // current mip will be the prev_mip for the next frame | |
426 MODE_INFO *temp = cm->prev_mip; | |
427 MODE_INFO **temp2 = cm->prev_mi_grid_base; | |
428 cm->prev_mip = cm->mip; | |
429 cm->mip = temp; | |
430 cm->prev_mi_grid_base = cm->mi_grid_base; | |
431 cm->mi_grid_base = temp2; | |
432 | |
433 // update the upper left visible macroblock ptrs | |
434 cm->mi = cm->mip + cm->mode_info_stride + 1; | |
435 cm->prev_mi = cm->prev_mip + cm->mode_info_stride + 1; | |
436 cm->mi_grid_visible = cm->mi_grid_base + cm->mode_info_stride + 1; | |
437 cm->prev_mi_grid_visible = cm->prev_mi_grid_base + | |
438 cm->mode_info_stride + 1; | |
439 | |
440 pbi->mb.mi_8x8 = cm->mi_grid_visible; | |
441 pbi->mb.mi_8x8[0] = cm->mi; | |
442 } | |
443 cm->current_video_frame++; | |
444 } | |
445 | |
446 pbi->ready_for_new_data = 0; | |
447 pbi->last_time_stamp = time_stamp; | |
448 pbi->source_sz = 0; | |
449 | |
450 cm->error.setjmp = 0; | |
451 return retcode; | |
452 } | |
453 | |
454 int vp9_get_raw_frame(VP9D_COMP *pbi, YV12_BUFFER_CONFIG *sd, | |
455 int64_t *time_stamp, int64_t *time_end_stamp, | |
456 vp9_ppflags_t *flags) { | |
457 int ret = -1; | |
458 | |
459 if (pbi->ready_for_new_data == 1) | |
460 return ret; | |
461 | |
462 /* ie no raw frame to show!!! */ | |
463 if (pbi->common.show_frame == 0) | |
464 return ret; | |
465 | |
466 pbi->ready_for_new_data = 1; | |
467 *time_stamp = pbi->last_time_stamp; | |
468 *time_end_stamp = 0; | |
469 | |
470 #if CONFIG_VP9_POSTPROC | |
471 ret = vp9_post_proc_frame(&pbi->common, sd, flags); | |
472 #else | |
473 | |
474 if (pbi->common.frame_to_show) { | |
475 *sd = *pbi->common.frame_to_show; | |
476 sd->y_width = pbi->common.width; | |
477 sd->y_height = pbi->common.height; | |
478 sd->uv_width = sd->y_width >> pbi->common.subsampling_x; | |
479 sd->uv_height = sd->y_height >> pbi->common.subsampling_y; | |
480 | |
481 ret = 0; | |
482 } else { | |
483 ret = -1; | |
484 } | |
485 | |
486 #endif /*!CONFIG_POSTPROC*/ | |
487 vp9_clear_system_state(); | |
488 return ret; | |
489 } | |
OLD | NEW |