Merge "Fix another multithreaded encoder loopfilter race condition"
[profile/ivi/libvpx.git] / vp8 / decoder / onyxd_if.c
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
12 #include "vp8/common/onyxc_int.h"
13 #if CONFIG_POSTPROC
14 #include "vp8/common/postproc.h"
15 #endif
16 #include "vp8/common/onyxd.h"
17 #include "onyxd_int.h"
18 #include "vpx_mem/vpx_mem.h"
19 #include "vp8/common/alloccommon.h"
20 #include "vp8/common/loopfilter.h"
21 #include "vp8/common/swapyv12buffer.h"
22 #include "vp8/common/threading.h"
23 #include "decoderthreading.h"
24 #include <stdio.h>
25 #include <assert.h>
26
27 #include "vp8/common/quant_common.h"
28 #include "vpx_scale/vpxscale.h"
29 #include "vp8/common/systemdependent.h"
30 #include "vpx_ports/vpx_timer.h"
31 #include "detokenize.h"
32 #if CONFIG_ERROR_CONCEALMENT
33 #include "error_concealment.h"
34 #endif
35 #if ARCH_ARM
36 #include "vpx_ports/arm.h"
37 #endif
38
39 extern void vp8_init_loop_filter(VP8_COMMON *cm);
40 extern void vp8cx_init_de_quantizer(VP8D_COMP *pbi);
41 static int get_free_fb (VP8_COMMON *cm);
42 static void ref_cnt_fb (int *buf, int *idx, int new_idx);
43
44 struct VP8D_COMP * vp8dx_create_decompressor(VP8D_CONFIG *oxcf)
45 {
46     VP8D_COMP *pbi = vpx_memalign(32, sizeof(VP8D_COMP));
47
48     if (!pbi)
49         return NULL;
50
51     vpx_memset(pbi, 0, sizeof(VP8D_COMP));
52
53     if (setjmp(pbi->common.error.jmp))
54     {
55         pbi->common.error.setjmp = 0;
56         vp8dx_remove_decompressor(pbi);
57         return 0;
58     }
59
60     pbi->common.error.setjmp = 1;
61
62     vp8_create_common(&pbi->common);
63
64     pbi->common.current_video_frame = 0;
65     pbi->ready_for_new_data = 1;
66
67 #if CONFIG_MULTITHREAD
68     pbi->max_threads = oxcf->max_threads;
69     vp8_decoder_create_threads(pbi);
70 #endif
71
72     /* vp8cx_init_de_quantizer() is first called here. Add check in frame_init_dequantizer() to avoid
73      *  unnecessary calling of vp8cx_init_de_quantizer() for every frame.
74      */
75     vp8cx_init_de_quantizer(pbi);
76
77     vp8_loop_filter_init(&pbi->common);
78
79     pbi->common.error.setjmp = 0;
80
81 #if CONFIG_ERROR_CONCEALMENT
82     pbi->ec_enabled = oxcf->error_concealment;
83     pbi->overlaps = NULL;
84 #else
85     pbi->ec_enabled = 0;
86 #endif
87     /* Error concealment is activated after a key frame has been
88      * decoded without errors when error concealment is enabled.
89      */
90     pbi->ec_active = 0;
91
92     pbi->decoded_key_frame = 0;
93
94     pbi->input_fragments = oxcf->input_fragments;
95     pbi->num_fragments = 0;
96
97     /* Independent partitions is activated when a frame updates the
98      * token probability table to have equal probabilities over the
99      * PREV_COEF context.
100      */
101     pbi->independent_partitions = 0;
102
103     vp8_setup_block_dptrs(&pbi->mb);
104
105     return pbi;
106 }
107
108
109 void vp8dx_remove_decompressor(VP8D_COMP *pbi)
110 {
111     if (!pbi)
112         return;
113
114 #if CONFIG_MULTITHREAD
115     if (pbi->b_multithreaded_rd)
116         vp8mt_de_alloc_temp_buffers(pbi, pbi->common.mb_rows);
117     vp8_decoder_remove_threads(pbi);
118 #endif
119 #if CONFIG_ERROR_CONCEALMENT
120     vp8_de_alloc_overlap_lists(pbi);
121 #endif
122     vp8_remove_common(&pbi->common);
123     vpx_free(pbi->mbc);
124     vpx_free(pbi);
125 }
126
127
128 vpx_codec_err_t vp8dx_get_reference(VP8D_COMP *pbi, VP8_REFFRAME ref_frame_flag, YV12_BUFFER_CONFIG *sd)
129 {
130     VP8_COMMON *cm = &pbi->common;
131     int ref_fb_idx;
132
133     if (ref_frame_flag == VP8_LAST_FLAG)
134         ref_fb_idx = cm->lst_fb_idx;
135     else if (ref_frame_flag == VP8_GOLD_FLAG)
136         ref_fb_idx = cm->gld_fb_idx;
137     else if (ref_frame_flag == VP8_ALT_FLAG)
138         ref_fb_idx = cm->alt_fb_idx;
139     else{
140         vpx_internal_error(&pbi->common.error, VPX_CODEC_ERROR,
141             "Invalid reference frame");
142         return pbi->common.error.error_code;
143     }
144
145     if(cm->yv12_fb[ref_fb_idx].y_height != sd->y_height ||
146         cm->yv12_fb[ref_fb_idx].y_width != sd->y_width ||
147         cm->yv12_fb[ref_fb_idx].uv_height != sd->uv_height ||
148         cm->yv12_fb[ref_fb_idx].uv_width != sd->uv_width){
149         vpx_internal_error(&pbi->common.error, VPX_CODEC_ERROR,
150             "Incorrect buffer dimensions");
151     }
152     else
153         vp8_yv12_copy_frame(&cm->yv12_fb[ref_fb_idx], sd);
154
155     return pbi->common.error.error_code;
156 }
157
158
159 vpx_codec_err_t vp8dx_set_reference(VP8D_COMP *pbi, VP8_REFFRAME ref_frame_flag, YV12_BUFFER_CONFIG *sd)
160 {
161     VP8_COMMON *cm = &pbi->common;
162     int *ref_fb_ptr = NULL;
163     int free_fb;
164
165     if (ref_frame_flag == VP8_LAST_FLAG)
166         ref_fb_ptr = &cm->lst_fb_idx;
167     else if (ref_frame_flag == VP8_GOLD_FLAG)
168         ref_fb_ptr = &cm->gld_fb_idx;
169     else if (ref_frame_flag == VP8_ALT_FLAG)
170         ref_fb_ptr = &cm->alt_fb_idx;
171     else{
172         vpx_internal_error(&pbi->common.error, VPX_CODEC_ERROR,
173             "Invalid reference frame");
174         return pbi->common.error.error_code;
175     }
176
177     if(cm->yv12_fb[*ref_fb_ptr].y_height != sd->y_height ||
178         cm->yv12_fb[*ref_fb_ptr].y_width != sd->y_width ||
179         cm->yv12_fb[*ref_fb_ptr].uv_height != sd->uv_height ||
180         cm->yv12_fb[*ref_fb_ptr].uv_width != sd->uv_width){
181         vpx_internal_error(&pbi->common.error, VPX_CODEC_ERROR,
182             "Incorrect buffer dimensions");
183     }
184     else{
185         /* Find an empty frame buffer. */
186         free_fb = get_free_fb(cm);
187         /* Decrease fb_idx_ref_cnt since it will be increased again in
188          * ref_cnt_fb() below. */
189         cm->fb_idx_ref_cnt[free_fb]--;
190
191         /* Manage the reference counters and copy image. */
192         ref_cnt_fb (cm->fb_idx_ref_cnt, ref_fb_ptr, free_fb);
193         vp8_yv12_copy_frame(sd, &cm->yv12_fb[*ref_fb_ptr]);
194     }
195
196    return pbi->common.error.error_code;
197 }
198
199 /*For ARM NEON, d8-d15 are callee-saved registers, and need to be saved by us.*/
200 #if HAVE_NEON
201 extern void vp8_push_neon(int64_t *store);
202 extern void vp8_pop_neon(int64_t *store);
203 #endif
204
205 static int get_free_fb (VP8_COMMON *cm)
206 {
207     int i;
208     for (i = 0; i < NUM_YV12_BUFFERS; i++)
209         if (cm->fb_idx_ref_cnt[i] == 0)
210             break;
211
212     assert(i < NUM_YV12_BUFFERS);
213     cm->fb_idx_ref_cnt[i] = 1;
214     return i;
215 }
216
217 static void ref_cnt_fb (int *buf, int *idx, int new_idx)
218 {
219     if (buf[*idx] > 0)
220         buf[*idx]--;
221
222     *idx = new_idx;
223
224     buf[new_idx]++;
225 }
226
227 /* If any buffer copy / swapping is signalled it should be done here. */
228 static int swap_frame_buffers (VP8_COMMON *cm)
229 {
230     int err = 0;
231
232     /* The alternate reference frame or golden frame can be updated
233      *  using the new, last, or golden/alt ref frame.  If it
234      *  is updated using the newly decoded frame it is a refresh.
235      *  An update using the last or golden/alt ref frame is a copy.
236      */
237     if (cm->copy_buffer_to_arf)
238     {
239         int new_fb = 0;
240
241         if (cm->copy_buffer_to_arf == 1)
242             new_fb = cm->lst_fb_idx;
243         else if (cm->copy_buffer_to_arf == 2)
244             new_fb = cm->gld_fb_idx;
245         else
246             err = -1;
247
248         ref_cnt_fb (cm->fb_idx_ref_cnt, &cm->alt_fb_idx, new_fb);
249     }
250
251     if (cm->copy_buffer_to_gf)
252     {
253         int new_fb = 0;
254
255         if (cm->copy_buffer_to_gf == 1)
256             new_fb = cm->lst_fb_idx;
257         else if (cm->copy_buffer_to_gf == 2)
258             new_fb = cm->alt_fb_idx;
259         else
260             err = -1;
261
262         ref_cnt_fb (cm->fb_idx_ref_cnt, &cm->gld_fb_idx, new_fb);
263     }
264
265     if (cm->refresh_golden_frame)
266         ref_cnt_fb (cm->fb_idx_ref_cnt, &cm->gld_fb_idx, cm->new_fb_idx);
267
268     if (cm->refresh_alt_ref_frame)
269         ref_cnt_fb (cm->fb_idx_ref_cnt, &cm->alt_fb_idx, cm->new_fb_idx);
270
271     if (cm->refresh_last_frame)
272     {
273         ref_cnt_fb (cm->fb_idx_ref_cnt, &cm->lst_fb_idx, cm->new_fb_idx);
274
275         cm->frame_to_show = &cm->yv12_fb[cm->lst_fb_idx];
276     }
277     else
278         cm->frame_to_show = &cm->yv12_fb[cm->new_fb_idx];
279
280     cm->fb_idx_ref_cnt[cm->new_fb_idx]--;
281
282     return err;
283 }
284
285 int vp8dx_receive_compressed_data(VP8D_COMP *pbi, unsigned long size, const unsigned char *source, int64_t time_stamp)
286 {
287 #if HAVE_NEON
288     int64_t dx_store_reg[8];
289 #endif
290     VP8_COMMON *cm = &pbi->common;
291     int retcode = 0;
292
293     /*if(pbi->ready_for_new_data == 0)
294         return -1;*/
295
296     if (pbi == 0)
297     {
298         return -1;
299     }
300
301     pbi->common.error.error_code = VPX_CODEC_OK;
302
303     if (pbi->num_fragments == 0)
304     {
305         /* New frame, reset fragment pointers and sizes */
306         vpx_memset((void*)pbi->fragments, 0, sizeof(pbi->fragments));
307         vpx_memset(pbi->fragment_sizes, 0, sizeof(pbi->fragment_sizes));
308     }
309     if (pbi->input_fragments && !(source == NULL && size == 0))
310     {
311         /* Store a pointer to this fragment and return. We haven't
312          * received the complete frame yet, so we will wait with decoding.
313          */
314         assert(pbi->num_fragments < MAX_PARTITIONS);
315         pbi->fragments[pbi->num_fragments] = source;
316         pbi->fragment_sizes[pbi->num_fragments] = size;
317         pbi->num_fragments++;
318         if (pbi->num_fragments > (1 << EIGHT_PARTITION) + 1)
319         {
320             pbi->common.error.error_code = VPX_CODEC_UNSUP_BITSTREAM;
321             pbi->common.error.setjmp = 0;
322             pbi->num_fragments = 0;
323             return -1;
324         }
325         return 0;
326     }
327
328     if (!pbi->input_fragments)
329     {
330         pbi->fragments[0] = source;
331         pbi->fragment_sizes[0] = size;
332         pbi->num_fragments = 1;
333     }
334     assert(pbi->common.multi_token_partition <= EIGHT_PARTITION);
335     if (pbi->num_fragments == 0)
336     {
337         pbi->num_fragments = 1;
338         pbi->fragments[0] = NULL;
339         pbi->fragment_sizes[0] = 0;
340     }
341
342     if (!pbi->ec_active &&
343         pbi->num_fragments <= 1 && pbi->fragment_sizes[0] == 0)
344     {
345         /* If error concealment is disabled we won't signal missing frames
346          * to the decoder.
347          */
348         if (cm->fb_idx_ref_cnt[cm->lst_fb_idx] > 1)
349         {
350             /* The last reference shares buffer with another reference
351              * buffer. Move it to its own buffer before setting it as
352              * corrupt, otherwise we will make multiple buffers corrupt.
353              */
354             const int prev_idx = cm->lst_fb_idx;
355             cm->fb_idx_ref_cnt[prev_idx]--;
356             cm->lst_fb_idx = get_free_fb(cm);
357             vp8_yv12_copy_frame(&cm->yv12_fb[prev_idx],
358                                     &cm->yv12_fb[cm->lst_fb_idx]);
359         }
360         /* This is used to signal that we are missing frames.
361          * We do not know if the missing frame(s) was supposed to update
362          * any of the reference buffers, but we act conservative and
363          * mark only the last buffer as corrupted.
364          */
365         cm->yv12_fb[cm->lst_fb_idx].corrupted = 1;
366
367         /* Signal that we have no frame to show. */
368         cm->show_frame = 0;
369
370         pbi->num_fragments = 0;
371
372         /* Nothing more to do. */
373         return 0;
374     }
375
376 #if HAVE_NEON
377 #if CONFIG_RUNTIME_CPU_DETECT
378     if (cm->cpu_caps & HAS_NEON)
379 #endif
380     {
381         vp8_push_neon(dx_store_reg);
382     }
383 #endif
384
385     cm->new_fb_idx = get_free_fb (cm);
386
387     if (setjmp(pbi->common.error.jmp))
388     {
389 #if HAVE_NEON
390 #if CONFIG_RUNTIME_CPU_DETECT
391         if (cm->cpu_caps & HAS_NEON)
392 #endif
393         {
394             vp8_pop_neon(dx_store_reg);
395         }
396 #endif
397         pbi->common.error.setjmp = 0;
398
399         pbi->num_fragments = 0;
400
401        /* We do not know if the missing frame(s) was supposed to update
402         * any of the reference buffers, but we act conservative and
403         * mark only the last buffer as corrupted.
404         */
405         cm->yv12_fb[cm->lst_fb_idx].corrupted = 1;
406
407         if (cm->fb_idx_ref_cnt[cm->new_fb_idx] > 0)
408           cm->fb_idx_ref_cnt[cm->new_fb_idx]--;
409         return -1;
410     }
411
412     pbi->common.error.setjmp = 1;
413
414     retcode = vp8_decode_frame(pbi);
415
416     if (retcode < 0)
417     {
418 #if HAVE_NEON
419 #if CONFIG_RUNTIME_CPU_DETECT
420         if (cm->cpu_caps & HAS_NEON)
421 #endif
422         {
423             vp8_pop_neon(dx_store_reg);
424         }
425 #endif
426         pbi->common.error.error_code = VPX_CODEC_ERROR;
427         pbi->common.error.setjmp = 0;
428         pbi->num_fragments = 0;
429         if (cm->fb_idx_ref_cnt[cm->new_fb_idx] > 0)
430           cm->fb_idx_ref_cnt[cm->new_fb_idx]--;
431         return retcode;
432     }
433
434 #if CONFIG_MULTITHREAD
435     if (pbi->b_multithreaded_rd && cm->multi_token_partition != ONE_PARTITION)
436     {
437         if (swap_frame_buffers (cm))
438         {
439 #if HAVE_NEON
440 #if CONFIG_RUNTIME_CPU_DETECT
441             if (cm->cpu_caps & HAS_NEON)
442 #endif
443             {
444                 vp8_pop_neon(dx_store_reg);
445             }
446 #endif
447             pbi->common.error.error_code = VPX_CODEC_ERROR;
448             pbi->common.error.setjmp = 0;
449             pbi->num_fragments = 0;
450             return -1;
451         }
452     } else
453 #endif
454     {
455         if (swap_frame_buffers (cm))
456         {
457 #if HAVE_NEON
458 #if CONFIG_RUNTIME_CPU_DETECT
459             if (cm->cpu_caps & HAS_NEON)
460 #endif
461             {
462                 vp8_pop_neon(dx_store_reg);
463             }
464 #endif
465             pbi->common.error.error_code = VPX_CODEC_ERROR;
466             pbi->common.error.setjmp = 0;
467             pbi->num_fragments = 0;
468             return -1;
469         }
470
471         if(cm->filter_level)
472         {
473             /* Apply the loop filter if appropriate. */
474             vp8_loop_filter_frame(cm, &pbi->mb, cm->frame_type);
475         }
476         vp8_yv12_extend_frame_borders(cm->frame_to_show);
477     }
478
479
480     vp8_clear_system_state();
481
482 #if CONFIG_ERROR_CONCEALMENT
483     /* swap the mode infos to storage for future error concealment */
484     if (pbi->ec_enabled && pbi->common.prev_mi)
485     {
486         MODE_INFO* tmp = pbi->common.prev_mi;
487         int row, col;
488         pbi->common.prev_mi = pbi->common.mi;
489         pbi->common.mi = tmp;
490
491         /* Propagate the segment_ids to the next frame */
492         for (row = 0; row < pbi->common.mb_rows; ++row)
493         {
494             for (col = 0; col < pbi->common.mb_cols; ++col)
495             {
496                 const int i = row*pbi->common.mode_info_stride + col;
497                 pbi->common.mi[i].mbmi.segment_id =
498                         pbi->common.prev_mi[i].mbmi.segment_id;
499             }
500         }
501     }
502 #endif
503
504     /*vp8_print_modes_and_motion_vectors( cm->mi, cm->mb_rows,cm->mb_cols, cm->current_video_frame);*/
505
506     if (cm->show_frame)
507         cm->current_video_frame++;
508
509     pbi->ready_for_new_data = 0;
510     pbi->last_time_stamp = time_stamp;
511     pbi->num_fragments = 0;
512
513 #if 0
514     {
515         int i;
516         int64_t earliest_time = pbi->dr[0].time_stamp;
517         int64_t latest_time = pbi->dr[0].time_stamp;
518         int64_t time_diff = 0;
519         int bytes = 0;
520
521         pbi->dr[pbi->common.current_video_frame&0xf].size = pbi->bc.pos + pbi->bc2.pos + 4;;
522         pbi->dr[pbi->common.current_video_frame&0xf].time_stamp = time_stamp;
523
524         for (i = 0; i < 16; i++)
525         {
526
527             bytes += pbi->dr[i].size;
528
529             if (pbi->dr[i].time_stamp < earliest_time)
530                 earliest_time = pbi->dr[i].time_stamp;
531
532             if (pbi->dr[i].time_stamp > latest_time)
533                 latest_time = pbi->dr[i].time_stamp;
534         }
535
536         time_diff = latest_time - earliest_time;
537
538         if (time_diff > 0)
539         {
540             pbi->common.bitrate = 80000.00 * bytes / time_diff  ;
541             pbi->common.framerate = 160000000.00 / time_diff ;
542         }
543
544     }
545 #endif
546
547 #if HAVE_NEON
548 #if CONFIG_RUNTIME_CPU_DETECT
549     if (cm->cpu_caps & HAS_NEON)
550 #endif
551     {
552         vp8_pop_neon(dx_store_reg);
553     }
554 #endif
555     pbi->common.error.setjmp = 0;
556     return retcode;
557 }
558 int vp8dx_get_raw_frame(VP8D_COMP *pbi, YV12_BUFFER_CONFIG *sd, int64_t *time_stamp, int64_t *time_end_stamp, vp8_ppflags_t *flags)
559 {
560     int ret = -1;
561
562     if (pbi->ready_for_new_data == 1)
563         return ret;
564
565     /* ie no raw frame to show!!! */
566     if (pbi->common.show_frame == 0)
567         return ret;
568
569     pbi->ready_for_new_data = 1;
570     *time_stamp = pbi->last_time_stamp;
571     *time_end_stamp = 0;
572
573     sd->clrtype = pbi->common.clr_type;
574 #if CONFIG_POSTPROC
575     ret = vp8_post_proc_frame(&pbi->common, sd, flags);
576 #else
577
578     if (pbi->common.frame_to_show)
579     {
580         *sd = *pbi->common.frame_to_show;
581         sd->y_width = pbi->common.Width;
582         sd->y_height = pbi->common.Height;
583         sd->uv_height = pbi->common.Height / 2;
584         ret = 0;
585     }
586     else
587     {
588         ret = -1;
589     }
590
591 #endif /*!CONFIG_POSTPROC*/
592     vp8_clear_system_state();
593     return ret;
594 }
595
596
597 /* This function as written isn't decoder specific, but the encoder has
598  * much faster ways of computing this, so it's ok for it to live in a
599  * decode specific file.
600  */
601 int vp8dx_references_buffer( VP8_COMMON *oci, int ref_frame )
602 {
603     const MODE_INFO *mi = oci->mi;
604     int mb_row, mb_col;
605
606     for (mb_row = 0; mb_row < oci->mb_rows; mb_row++)
607     {
608         for (mb_col = 0; mb_col < oci->mb_cols; mb_col++,mi++)
609         {
610             if( mi->mbmi.ref_frame == ref_frame)
611               return 1;
612         }
613         mi++;
614     }
615     return 0;
616
617 }