190d3ed91ae9228e1c8bab561b6138d4cb8c1e4fdAndreas Huber/*
2f71323e297a928af368937089d3ed71239786f86Andreas Huber *  Copyright (c) 2010 The WebM project authors. All Rights Reserved.
390d3ed91ae9228e1c8bab561b6138d4cb8c1e4fdAndreas Huber *
4f71323e297a928af368937089d3ed71239786f86Andreas Huber *  Use of this source code is governed by a BSD-style license
5f71323e297a928af368937089d3ed71239786f86Andreas Huber *  that can be found in the LICENSE file in the root of the source
6f71323e297a928af368937089d3ed71239786f86Andreas Huber *  tree. An additional intellectual property rights grant can be found
7f71323e297a928af368937089d3ed71239786f86Andreas Huber *  in the file PATENTS.  All contributing project authors may
8f71323e297a928af368937089d3ed71239786f86Andreas Huber *  be found in the AUTHORS file in the root of the source tree.
990d3ed91ae9228e1c8bab561b6138d4cb8c1e4fdAndreas Huber */
1090d3ed91ae9228e1c8bab561b6138d4cb8c1e4fdAndreas Huber
1190d3ed91ae9228e1c8bab561b6138d4cb8c1e4fdAndreas Huber#include "onyx_int.h"
1279f15823c34ae1e423108295e416213200bb280fAndreas Huber#include "vp8/common/threading.h"
1379f15823c34ae1e423108295e416213200bb280fAndreas Huber#include "vp8/common/common.h"
1479f15823c34ae1e423108295e416213200bb280fAndreas Huber#include "vp8/common/extend.h"
151b362b15af34006e6a11974088a46d42b903418eJohann#include "bitstream.h"
161b362b15af34006e6a11974088a46d42b903418eJohann#include "encodeframe.h"
17df37111358d02836cb29bbcb9c6e4c95dff90a16Johann#include "ethreading.h"
1890d3ed91ae9228e1c8bab561b6138d4cb8c1e4fdAndreas Huber
1979f15823c34ae1e423108295e416213200bb280fAndreas Huber#if CONFIG_MULTITHREAD
2090d3ed91ae9228e1c8bab561b6138d4cb8c1e4fdAndreas Huber
217bc9febe8749e98a3812a0dc4380ceae75c29450Johannextern void vp8cx_mb_init_quantizer(VP8_COMP *cpi, MACROBLOCK *x,
227bc9febe8749e98a3812a0dc4380ceae75c29450Johann                                    int ok_to_skip);
2390d3ed91ae9228e1c8bab561b6138d4cb8c1e4fdAndreas Huber
247bc9febe8749e98a3812a0dc4380ceae75c29450Johannstatic THREAD_FUNCTION thread_loopfilter(void *p_data) {
257bc9febe8749e98a3812a0dc4380ceae75c29450Johann  VP8_COMP *cpi = (VP8_COMP *)(((LPFTHREAD_DATA *)p_data)->ptr1);
267bc9febe8749e98a3812a0dc4380ceae75c29450Johann  VP8_COMMON *cm = &cpi->common;
2779f15823c34ae1e423108295e416213200bb280fAndreas Huber
287bc9febe8749e98a3812a0dc4380ceae75c29450Johann  while (1) {
29df37111358d02836cb29bbcb9c6e4c95dff90a16Johann    if (vpx_atomic_load_acquire(&cpi->b_multi_threaded) == 0) break;
3079f15823c34ae1e423108295e416213200bb280fAndreas Huber
317bc9febe8749e98a3812a0dc4380ceae75c29450Johann    if (sem_wait(&cpi->h_event_start_lpf) == 0) {
327bc9febe8749e98a3812a0dc4380ceae75c29450Johann      /* we're shutting down */
33df37111358d02836cb29bbcb9c6e4c95dff90a16Johann      if (vpx_atomic_load_acquire(&cpi->b_multi_threaded) == 0) break;
3479f15823c34ae1e423108295e416213200bb280fAndreas Huber
357bc9febe8749e98a3812a0dc4380ceae75c29450Johann      vp8_loopfilter_frame(cpi, cm);
3679f15823c34ae1e423108295e416213200bb280fAndreas Huber
377bc9febe8749e98a3812a0dc4380ceae75c29450Johann      sem_post(&cpi->h_event_end_lpf);
3879f15823c34ae1e423108295e416213200bb280fAndreas Huber    }
397bc9febe8749e98a3812a0dc4380ceae75c29450Johann  }
4079f15823c34ae1e423108295e416213200bb280fAndreas Huber
417bc9febe8749e98a3812a0dc4380ceae75c29450Johann  return 0;
4279f15823c34ae1e423108295e416213200bb280fAndreas Huber}
4379f15823c34ae1e423108295e416213200bb280fAndreas Huber
447bc9febe8749e98a3812a0dc4380ceae75c29450Johannstatic THREAD_FUNCTION thread_encoding_proc(void *p_data) {
457bc9febe8749e98a3812a0dc4380ceae75c29450Johann  int ithread = ((ENCODETHREAD_DATA *)p_data)->ithread;
467bc9febe8749e98a3812a0dc4380ceae75c29450Johann  VP8_COMP *cpi = (VP8_COMP *)(((ENCODETHREAD_DATA *)p_data)->ptr1);
477bc9febe8749e98a3812a0dc4380ceae75c29450Johann  MB_ROW_COMP *mbri = (MB_ROW_COMP *)(((ENCODETHREAD_DATA *)p_data)->ptr2);
487bc9febe8749e98a3812a0dc4380ceae75c29450Johann  ENTROPY_CONTEXT_PLANES mb_row_left_context;
497bc9febe8749e98a3812a0dc4380ceae75c29450Johann
507bc9febe8749e98a3812a0dc4380ceae75c29450Johann  while (1) {
51df37111358d02836cb29bbcb9c6e4c95dff90a16Johann    if (vpx_atomic_load_acquire(&cpi->b_multi_threaded) == 0) break;
527bc9febe8749e98a3812a0dc4380ceae75c29450Johann
537bc9febe8749e98a3812a0dc4380ceae75c29450Johann    if (sem_wait(&cpi->h_event_start_encoding[ithread]) == 0) {
547bc9febe8749e98a3812a0dc4380ceae75c29450Johann      const int nsync = cpi->mt_sync_range;
557bc9febe8749e98a3812a0dc4380ceae75c29450Johann      VP8_COMMON *cm = &cpi->common;
567bc9febe8749e98a3812a0dc4380ceae75c29450Johann      int mb_row;
577bc9febe8749e98a3812a0dc4380ceae75c29450Johann      MACROBLOCK *x = &mbri->mb;
587bc9febe8749e98a3812a0dc4380ceae75c29450Johann      MACROBLOCKD *xd = &x->e_mbd;
597bc9febe8749e98a3812a0dc4380ceae75c29450Johann      TOKENEXTRA *tp;
601b362b15af34006e6a11974088a46d42b903418eJohann#if CONFIG_REALTIME_ONLY & CONFIG_ONTHEFLY_BITPACKING
617bc9febe8749e98a3812a0dc4380ceae75c29450Johann      TOKENEXTRA *tp_start = cpi->tok + (1 + ithread) * (16 * 24);
627bc9febe8749e98a3812a0dc4380ceae75c29450Johann      const int num_part = (1 << cm->multi_token_partition);
631b362b15af34006e6a11974088a46d42b903418eJohann#endif
6479f15823c34ae1e423108295e416213200bb280fAndreas Huber
657bc9febe8749e98a3812a0dc4380ceae75c29450Johann      int *segment_counts = mbri->segment_counts;
667bc9febe8749e98a3812a0dc4380ceae75c29450Johann      int *totalrate = &mbri->totalrate;
677bc9febe8749e98a3812a0dc4380ceae75c29450Johann
687bc9febe8749e98a3812a0dc4380ceae75c29450Johann      /* we're shutting down */
69df37111358d02836cb29bbcb9c6e4c95dff90a16Johann      if (vpx_atomic_load_acquire(&cpi->b_multi_threaded) == 0) break;
707bc9febe8749e98a3812a0dc4380ceae75c29450Johann
717bc9febe8749e98a3812a0dc4380ceae75c29450Johann      xd->mode_info_context = cm->mi + cm->mode_info_stride * (ithread + 1);
727bc9febe8749e98a3812a0dc4380ceae75c29450Johann      xd->mode_info_stride = cm->mode_info_stride;
737bc9febe8749e98a3812a0dc4380ceae75c29450Johann
747bc9febe8749e98a3812a0dc4380ceae75c29450Johann      for (mb_row = ithread + 1; mb_row < cm->mb_rows;
757bc9febe8749e98a3812a0dc4380ceae75c29450Johann           mb_row += (cpi->encoding_thread_count + 1)) {
767bc9febe8749e98a3812a0dc4380ceae75c29450Johann        int recon_yoffset, recon_uvoffset;
777bc9febe8749e98a3812a0dc4380ceae75c29450Johann        int mb_col;
787bc9febe8749e98a3812a0dc4380ceae75c29450Johann        int ref_fb_idx = cm->lst_fb_idx;
797bc9febe8749e98a3812a0dc4380ceae75c29450Johann        int dst_fb_idx = cm->new_fb_idx;
807bc9febe8749e98a3812a0dc4380ceae75c29450Johann        int recon_y_stride = cm->yv12_fb[ref_fb_idx].y_stride;
817bc9febe8749e98a3812a0dc4380ceae75c29450Johann        int recon_uv_stride = cm->yv12_fb[ref_fb_idx].uv_stride;
827bc9febe8749e98a3812a0dc4380ceae75c29450Johann        int map_index = (mb_row * cm->mb_cols);
83df37111358d02836cb29bbcb9c6e4c95dff90a16Johann        const vpx_atomic_int *last_row_current_mb_col;
84df37111358d02836cb29bbcb9c6e4c95dff90a16Johann        vpx_atomic_int *current_mb_col = &cpi->mt_current_mb_col[mb_row];
857bc9febe8749e98a3812a0dc4380ceae75c29450Johann
867bc9febe8749e98a3812a0dc4380ceae75c29450Johann#if (CONFIG_REALTIME_ONLY & CONFIG_ONTHEFLY_BITPACKING)
877bc9febe8749e98a3812a0dc4380ceae75c29450Johann        vp8_writer *w = &cpi->bc[1 + (mb_row % num_part)];
881b362b15af34006e6a11974088a46d42b903418eJohann#else
897bc9febe8749e98a3812a0dc4380ceae75c29450Johann        tp = cpi->tok + (mb_row * (cm->mb_cols * 16 * 24));
907bc9febe8749e98a3812a0dc4380ceae75c29450Johann        cpi->tplist[mb_row].start = tp;
911b362b15af34006e6a11974088a46d42b903418eJohann#endif
9290d3ed91ae9228e1c8bab561b6138d4cb8c1e4fdAndreas Huber
937bc9febe8749e98a3812a0dc4380ceae75c29450Johann        last_row_current_mb_col = &cpi->mt_current_mb_col[mb_row - 1];
9490d3ed91ae9228e1c8bab561b6138d4cb8c1e4fdAndreas Huber
957bc9febe8749e98a3812a0dc4380ceae75c29450Johann        /* reset above block coeffs */
967bc9febe8749e98a3812a0dc4380ceae75c29450Johann        xd->above_context = cm->above_context;
977bc9febe8749e98a3812a0dc4380ceae75c29450Johann        xd->left_context = &mb_row_left_context;
9890d3ed91ae9228e1c8bab561b6138d4cb8c1e4fdAndreas Huber
997bc9febe8749e98a3812a0dc4380ceae75c29450Johann        vp8_zero(mb_row_left_context);
10090d3ed91ae9228e1c8bab561b6138d4cb8c1e4fdAndreas Huber
1017bc9febe8749e98a3812a0dc4380ceae75c29450Johann        xd->up_available = (mb_row != 0);
1027bc9febe8749e98a3812a0dc4380ceae75c29450Johann        recon_yoffset = (mb_row * recon_y_stride * 16);
1037bc9febe8749e98a3812a0dc4380ceae75c29450Johann        recon_uvoffset = (mb_row * recon_uv_stride * 8);
10490d3ed91ae9228e1c8bab561b6138d4cb8c1e4fdAndreas Huber
1057bc9febe8749e98a3812a0dc4380ceae75c29450Johann        /* Set the mb activity pointer to the start of the row. */
1067bc9febe8749e98a3812a0dc4380ceae75c29450Johann        x->mb_activity_ptr = &cpi->mb_activity_map[map_index];
10779f15823c34ae1e423108295e416213200bb280fAndreas Huber
1087bc9febe8749e98a3812a0dc4380ceae75c29450Johann        /* for each macroblock col in image */
1097bc9febe8749e98a3812a0dc4380ceae75c29450Johann        for (mb_col = 0; mb_col < cm->mb_cols; ++mb_col) {
1107bc9febe8749e98a3812a0dc4380ceae75c29450Johann          if (((mb_col - 1) % nsync) == 0) {
111df37111358d02836cb29bbcb9c6e4c95dff90a16Johann            vpx_atomic_store_release(current_mb_col, mb_col - 1);
1127bc9febe8749e98a3812a0dc4380ceae75c29450Johann          }
11390d3ed91ae9228e1c8bab561b6138d4cb8c1e4fdAndreas Huber
1147bc9febe8749e98a3812a0dc4380ceae75c29450Johann          if (mb_row && !(mb_col & (nsync - 1))) {
115df37111358d02836cb29bbcb9c6e4c95dff90a16Johann            vp8_atomic_spin_wait(mb_col, last_row_current_mb_col, nsync);
1167bc9febe8749e98a3812a0dc4380ceae75c29450Johann          }
11790d3ed91ae9228e1c8bab561b6138d4cb8c1e4fdAndreas Huber
1181b362b15af34006e6a11974088a46d42b903418eJohann#if CONFIG_REALTIME_ONLY & CONFIG_ONTHEFLY_BITPACKING
1197bc9febe8749e98a3812a0dc4380ceae75c29450Johann          tp = tp_start;
1201b362b15af34006e6a11974088a46d42b903418eJohann#endif
1211b362b15af34006e6a11974088a46d42b903418eJohann
1227bc9febe8749e98a3812a0dc4380ceae75c29450Johann          /* Distance of Mb to the various image edges.
1237bc9febe8749e98a3812a0dc4380ceae75c29450Johann           * These specified to 8th pel as they are always compared
1247bc9febe8749e98a3812a0dc4380ceae75c29450Johann           * to values that are in 1/8th pel units
1257bc9febe8749e98a3812a0dc4380ceae75c29450Johann           */
1267bc9febe8749e98a3812a0dc4380ceae75c29450Johann          xd->mb_to_left_edge = -((mb_col * 16) << 3);
1277bc9febe8749e98a3812a0dc4380ceae75c29450Johann          xd->mb_to_right_edge = ((cm->mb_cols - 1 - mb_col) * 16) << 3;
1287bc9febe8749e98a3812a0dc4380ceae75c29450Johann          xd->mb_to_top_edge = -((mb_row * 16) << 3);
1297bc9febe8749e98a3812a0dc4380ceae75c29450Johann          xd->mb_to_bottom_edge = ((cm->mb_rows - 1 - mb_row) * 16) << 3;
1307bc9febe8749e98a3812a0dc4380ceae75c29450Johann
1317bc9febe8749e98a3812a0dc4380ceae75c29450Johann          /* Set up limit values for motion vectors used to prevent
1327bc9febe8749e98a3812a0dc4380ceae75c29450Johann           * them extending outside the UMV borders
1337bc9febe8749e98a3812a0dc4380ceae75c29450Johann           */
1347bc9febe8749e98a3812a0dc4380ceae75c29450Johann          x->mv_col_min = -((mb_col * 16) + (VP8BORDERINPIXELS - 16));
1357bc9febe8749e98a3812a0dc4380ceae75c29450Johann          x->mv_col_max =
1367bc9febe8749e98a3812a0dc4380ceae75c29450Johann              ((cm->mb_cols - 1 - mb_col) * 16) + (VP8BORDERINPIXELS - 16);
1377bc9febe8749e98a3812a0dc4380ceae75c29450Johann          x->mv_row_min = -((mb_row * 16) + (VP8BORDERINPIXELS - 16));
1387bc9febe8749e98a3812a0dc4380ceae75c29450Johann          x->mv_row_max =
1397bc9febe8749e98a3812a0dc4380ceae75c29450Johann              ((cm->mb_rows - 1 - mb_row) * 16) + (VP8BORDERINPIXELS - 16);
1407bc9febe8749e98a3812a0dc4380ceae75c29450Johann
1417bc9febe8749e98a3812a0dc4380ceae75c29450Johann          xd->dst.y_buffer = cm->yv12_fb[dst_fb_idx].y_buffer + recon_yoffset;
1427bc9febe8749e98a3812a0dc4380ceae75c29450Johann          xd->dst.u_buffer = cm->yv12_fb[dst_fb_idx].u_buffer + recon_uvoffset;
1437bc9febe8749e98a3812a0dc4380ceae75c29450Johann          xd->dst.v_buffer = cm->yv12_fb[dst_fb_idx].v_buffer + recon_uvoffset;
1447bc9febe8749e98a3812a0dc4380ceae75c29450Johann          xd->left_available = (mb_col != 0);
1457bc9febe8749e98a3812a0dc4380ceae75c29450Johann
1467bc9febe8749e98a3812a0dc4380ceae75c29450Johann          x->rddiv = cpi->RDDIV;
1477bc9febe8749e98a3812a0dc4380ceae75c29450Johann          x->rdmult = cpi->RDMULT;
1487bc9febe8749e98a3812a0dc4380ceae75c29450Johann
1497bc9febe8749e98a3812a0dc4380ceae75c29450Johann          /* Copy current mb to a buffer */
1507bc9febe8749e98a3812a0dc4380ceae75c29450Johann          vp8_copy_mem16x16(x->src.y_buffer, x->src.y_stride, x->thismb, 16);
1517bc9febe8749e98a3812a0dc4380ceae75c29450Johann
1527bc9febe8749e98a3812a0dc4380ceae75c29450Johann          if (cpi->oxcf.tuning == VP8_TUNE_SSIM) vp8_activity_masking(cpi, x);
1537bc9febe8749e98a3812a0dc4380ceae75c29450Johann
1547bc9febe8749e98a3812a0dc4380ceae75c29450Johann          /* Is segmentation enabled */
1557bc9febe8749e98a3812a0dc4380ceae75c29450Johann          /* MB level adjustment to quantizer */
1567bc9febe8749e98a3812a0dc4380ceae75c29450Johann          if (xd->segmentation_enabled) {
1577bc9febe8749e98a3812a0dc4380ceae75c29450Johann            /* Code to set segment id in xd->mbmi.segment_id for
1587bc9febe8749e98a3812a0dc4380ceae75c29450Johann             * current MB (with range checking)
1597bc9febe8749e98a3812a0dc4380ceae75c29450Johann             */
1607bc9febe8749e98a3812a0dc4380ceae75c29450Johann            if (cpi->segmentation_map[map_index + mb_col] <= 3) {
1617bc9febe8749e98a3812a0dc4380ceae75c29450Johann              xd->mode_info_context->mbmi.segment_id =
1627bc9febe8749e98a3812a0dc4380ceae75c29450Johann                  cpi->segmentation_map[map_index + mb_col];
1637bc9febe8749e98a3812a0dc4380ceae75c29450Johann            } else {
1647bc9febe8749e98a3812a0dc4380ceae75c29450Johann              xd->mode_info_context->mbmi.segment_id = 0;
1657bc9febe8749e98a3812a0dc4380ceae75c29450Johann            }
1667bc9febe8749e98a3812a0dc4380ceae75c29450Johann
1677bc9febe8749e98a3812a0dc4380ceae75c29450Johann            vp8cx_mb_init_quantizer(cpi, x, 1);
1687bc9febe8749e98a3812a0dc4380ceae75c29450Johann          } else {
1697bc9febe8749e98a3812a0dc4380ceae75c29450Johann            /* Set to Segment 0 by default */
1707bc9febe8749e98a3812a0dc4380ceae75c29450Johann            xd->mode_info_context->mbmi.segment_id = 0;
1717bc9febe8749e98a3812a0dc4380ceae75c29450Johann          }
1727bc9febe8749e98a3812a0dc4380ceae75c29450Johann
1737bc9febe8749e98a3812a0dc4380ceae75c29450Johann          x->active_ptr = cpi->active_map + map_index + mb_col;
1747bc9febe8749e98a3812a0dc4380ceae75c29450Johann
1757bc9febe8749e98a3812a0dc4380ceae75c29450Johann          if (cm->frame_type == KEY_FRAME) {
1767bc9febe8749e98a3812a0dc4380ceae75c29450Johann            *totalrate += vp8cx_encode_intra_macroblock(cpi, x, &tp);
17790d3ed91ae9228e1c8bab561b6138d4cb8c1e4fdAndreas Huber#ifdef MODE_STATS
1787bc9febe8749e98a3812a0dc4380ceae75c29450Johann            y_modes[xd->mbmi.mode]++;
17990d3ed91ae9228e1c8bab561b6138d4cb8c1e4fdAndreas Huber#endif
1807bc9febe8749e98a3812a0dc4380ceae75c29450Johann          } else {
1817bc9febe8749e98a3812a0dc4380ceae75c29450Johann            *totalrate += vp8cx_encode_inter_macroblock(
1827bc9febe8749e98a3812a0dc4380ceae75c29450Johann                cpi, x, &tp, recon_yoffset, recon_uvoffset, mb_row, mb_col);
18390d3ed91ae9228e1c8bab561b6138d4cb8c1e4fdAndreas Huber
18490d3ed91ae9228e1c8bab561b6138d4cb8c1e4fdAndreas Huber#ifdef MODE_STATS
1857bc9febe8749e98a3812a0dc4380ceae75c29450Johann            inter_y_modes[xd->mbmi.mode]++;
18690d3ed91ae9228e1c8bab561b6138d4cb8c1e4fdAndreas Huber
1877bc9febe8749e98a3812a0dc4380ceae75c29450Johann            if (xd->mbmi.mode == SPLITMV) {
1887bc9febe8749e98a3812a0dc4380ceae75c29450Johann              int b;
18990d3ed91ae9228e1c8bab561b6138d4cb8c1e4fdAndreas Huber
1907bc9febe8749e98a3812a0dc4380ceae75c29450Johann              for (b = 0; b < xd->mbmi.partition_count; ++b) {
1917bc9febe8749e98a3812a0dc4380ceae75c29450Johann                inter_b_modes[x->partition->bmi[b].mode]++;
1927bc9febe8749e98a3812a0dc4380ceae75c29450Johann              }
1937bc9febe8749e98a3812a0dc4380ceae75c29450Johann            }
19490d3ed91ae9228e1c8bab561b6138d4cb8c1e4fdAndreas Huber
19579f15823c34ae1e423108295e416213200bb280fAndreas Huber#endif
1967bc9febe8749e98a3812a0dc4380ceae75c29450Johann            // Keep track of how many (consecutive) times a  block
1977bc9febe8749e98a3812a0dc4380ceae75c29450Johann            // is coded as ZEROMV_LASTREF, for base layer frames.
1987bc9febe8749e98a3812a0dc4380ceae75c29450Johann            // Reset to 0 if its coded as anything else.
1997bc9febe8749e98a3812a0dc4380ceae75c29450Johann            if (cpi->current_layer == 0) {
2007bc9febe8749e98a3812a0dc4380ceae75c29450Johann              if (xd->mode_info_context->mbmi.mode == ZEROMV &&
2017bc9febe8749e98a3812a0dc4380ceae75c29450Johann                  xd->mode_info_context->mbmi.ref_frame == LAST_FRAME) {
2027bc9febe8749e98a3812a0dc4380ceae75c29450Johann                // Increment, check for wrap-around.
2037bc9febe8749e98a3812a0dc4380ceae75c29450Johann                if (cpi->consec_zero_last[map_index + mb_col] < 255) {
2047bc9febe8749e98a3812a0dc4380ceae75c29450Johann                  cpi->consec_zero_last[map_index + mb_col] += 1;
2057bc9febe8749e98a3812a0dc4380ceae75c29450Johann                }
2067bc9febe8749e98a3812a0dc4380ceae75c29450Johann                if (cpi->consec_zero_last_mvbias[map_index + mb_col] < 255) {
2077bc9febe8749e98a3812a0dc4380ceae75c29450Johann                  cpi->consec_zero_last_mvbias[map_index + mb_col] += 1;
2087bc9febe8749e98a3812a0dc4380ceae75c29450Johann                }
2097bc9febe8749e98a3812a0dc4380ceae75c29450Johann              } else {
2107bc9febe8749e98a3812a0dc4380ceae75c29450Johann                cpi->consec_zero_last[map_index + mb_col] = 0;
2117bc9febe8749e98a3812a0dc4380ceae75c29450Johann                cpi->consec_zero_last_mvbias[map_index + mb_col] = 0;
2127bc9febe8749e98a3812a0dc4380ceae75c29450Johann              }
2137bc9febe8749e98a3812a0dc4380ceae75c29450Johann              if (x->zero_last_dot_suppress) {
2147bc9febe8749e98a3812a0dc4380ceae75c29450Johann                cpi->consec_zero_last_mvbias[map_index + mb_col] = 0;
2157bc9febe8749e98a3812a0dc4380ceae75c29450Johann              }
2167bc9febe8749e98a3812a0dc4380ceae75c29450Johann            }
2177bc9febe8749e98a3812a0dc4380ceae75c29450Johann
2187bc9febe8749e98a3812a0dc4380ceae75c29450Johann            /* Special case code for cyclic refresh
2197bc9febe8749e98a3812a0dc4380ceae75c29450Johann             * If cyclic update enabled then copy
2207bc9febe8749e98a3812a0dc4380ceae75c29450Johann             * xd->mbmi.segment_id; (which may have been updated
2217bc9febe8749e98a3812a0dc4380ceae75c29450Johann             * based on mode during
2227bc9febe8749e98a3812a0dc4380ceae75c29450Johann             * vp8cx_encode_inter_macroblock()) back into the
2237bc9febe8749e98a3812a0dc4380ceae75c29450Johann             * global segmentation map
2247bc9febe8749e98a3812a0dc4380ceae75c29450Johann             */
2257bc9febe8749e98a3812a0dc4380ceae75c29450Johann            if ((cpi->current_layer == 0) &&
2267bc9febe8749e98a3812a0dc4380ceae75c29450Johann                (cpi->cyclic_refresh_mode_enabled &&
2277bc9febe8749e98a3812a0dc4380ceae75c29450Johann                 xd->segmentation_enabled)) {
2287bc9febe8749e98a3812a0dc4380ceae75c29450Johann              const MB_MODE_INFO *mbmi = &xd->mode_info_context->mbmi;
2297bc9febe8749e98a3812a0dc4380ceae75c29450Johann              cpi->segmentation_map[map_index + mb_col] = mbmi->segment_id;
2307bc9febe8749e98a3812a0dc4380ceae75c29450Johann
2317bc9febe8749e98a3812a0dc4380ceae75c29450Johann              /* If the block has been refreshed mark it as clean
2327bc9febe8749e98a3812a0dc4380ceae75c29450Johann               * (the magnitude of the -ve influences how long it
2337bc9febe8749e98a3812a0dc4380ceae75c29450Johann               * will be before we consider another refresh):
2347bc9febe8749e98a3812a0dc4380ceae75c29450Johann               * Else if it was coded (last frame 0,0) and has
2357bc9febe8749e98a3812a0dc4380ceae75c29450Johann               * not already been refreshed then mark it as a
2367bc9febe8749e98a3812a0dc4380ceae75c29450Johann               * candidate for cleanup next time (marked 0) else
2377bc9febe8749e98a3812a0dc4380ceae75c29450Johann               * mark it as dirty (1).
2387bc9febe8749e98a3812a0dc4380ceae75c29450Johann               */
2397bc9febe8749e98a3812a0dc4380ceae75c29450Johann              if (mbmi->segment_id) {
2407bc9febe8749e98a3812a0dc4380ceae75c29450Johann                cpi->cyclic_refresh_map[map_index + mb_col] = -1;
2417bc9febe8749e98a3812a0dc4380ceae75c29450Johann              } else if ((mbmi->mode == ZEROMV) &&
2427bc9febe8749e98a3812a0dc4380ceae75c29450Johann                         (mbmi->ref_frame == LAST_FRAME)) {
2437bc9febe8749e98a3812a0dc4380ceae75c29450Johann                if (cpi->cyclic_refresh_map[map_index + mb_col] == 1) {
2447bc9febe8749e98a3812a0dc4380ceae75c29450Johann                  cpi->cyclic_refresh_map[map_index + mb_col] = 0;
2457bc9febe8749e98a3812a0dc4380ceae75c29450Johann                }
2467bc9febe8749e98a3812a0dc4380ceae75c29450Johann              } else {
2477bc9febe8749e98a3812a0dc4380ceae75c29450Johann                cpi->cyclic_refresh_map[map_index + mb_col] = 1;
2487bc9febe8749e98a3812a0dc4380ceae75c29450Johann              }
2497bc9febe8749e98a3812a0dc4380ceae75c29450Johann            }
2507bc9febe8749e98a3812a0dc4380ceae75c29450Johann          }
25190d3ed91ae9228e1c8bab561b6138d4cb8c1e4fdAndreas Huber
2521b362b15af34006e6a11974088a46d42b903418eJohann#if CONFIG_REALTIME_ONLY & CONFIG_ONTHEFLY_BITPACKING
2537bc9febe8749e98a3812a0dc4380ceae75c29450Johann          /* pack tokens for this MB */
2547bc9febe8749e98a3812a0dc4380ceae75c29450Johann          {
2557bc9febe8749e98a3812a0dc4380ceae75c29450Johann            int tok_count = tp - tp_start;
2567bc9febe8749e98a3812a0dc4380ceae75c29450Johann            vp8_pack_tokens(w, tp_start, tok_count);
2577bc9febe8749e98a3812a0dc4380ceae75c29450Johann          }
2581b362b15af34006e6a11974088a46d42b903418eJohann#else
2597bc9febe8749e98a3812a0dc4380ceae75c29450Johann          cpi->tplist[mb_row].stop = tp;
2601b362b15af34006e6a11974088a46d42b903418eJohann#endif
2617bc9febe8749e98a3812a0dc4380ceae75c29450Johann          /* Increment pointer into gf usage flags structure. */
2627bc9febe8749e98a3812a0dc4380ceae75c29450Johann          x->gf_active_ptr++;
26390d3ed91ae9228e1c8bab561b6138d4cb8c1e4fdAndreas Huber
2647bc9febe8749e98a3812a0dc4380ceae75c29450Johann          /* Increment the activity mask pointers. */
2657bc9febe8749e98a3812a0dc4380ceae75c29450Johann          x->mb_activity_ptr++;
26690d3ed91ae9228e1c8bab561b6138d4cb8c1e4fdAndreas Huber
2677bc9febe8749e98a3812a0dc4380ceae75c29450Johann          /* adjust to the next column of macroblocks */
2687bc9febe8749e98a3812a0dc4380ceae75c29450Johann          x->src.y_buffer += 16;
2697bc9febe8749e98a3812a0dc4380ceae75c29450Johann          x->src.u_buffer += 8;
2707bc9febe8749e98a3812a0dc4380ceae75c29450Johann          x->src.v_buffer += 8;
27190d3ed91ae9228e1c8bab561b6138d4cb8c1e4fdAndreas Huber
2727bc9febe8749e98a3812a0dc4380ceae75c29450Johann          recon_yoffset += 16;
2737bc9febe8749e98a3812a0dc4380ceae75c29450Johann          recon_uvoffset += 8;
27490d3ed91ae9228e1c8bab561b6138d4cb8c1e4fdAndreas Huber
2757bc9febe8749e98a3812a0dc4380ceae75c29450Johann          /* Keep track of segment usage */
2767bc9febe8749e98a3812a0dc4380ceae75c29450Johann          segment_counts[xd->mode_info_context->mbmi.segment_id]++;
27779f15823c34ae1e423108295e416213200bb280fAndreas Huber
2787bc9febe8749e98a3812a0dc4380ceae75c29450Johann          /* skip to next mb */
2797bc9febe8749e98a3812a0dc4380ceae75c29450Johann          xd->mode_info_context++;
2807bc9febe8749e98a3812a0dc4380ceae75c29450Johann          x->partition_info++;
2817bc9febe8749e98a3812a0dc4380ceae75c29450Johann          xd->above_context++;
28290d3ed91ae9228e1c8bab561b6138d4cb8c1e4fdAndreas Huber        }
2837bc9febe8749e98a3812a0dc4380ceae75c29450Johann
2847bc9febe8749e98a3812a0dc4380ceae75c29450Johann        vp8_extend_mb_row(&cm->yv12_fb[dst_fb_idx], xd->dst.y_buffer + 16,
2857bc9febe8749e98a3812a0dc4380ceae75c29450Johann                          xd->dst.u_buffer + 8, xd->dst.v_buffer + 8);
2867bc9febe8749e98a3812a0dc4380ceae75c29450Johann
287df37111358d02836cb29bbcb9c6e4c95dff90a16Johann        vpx_atomic_store_release(current_mb_col, mb_col + nsync);
2887bc9febe8749e98a3812a0dc4380ceae75c29450Johann
2897bc9febe8749e98a3812a0dc4380ceae75c29450Johann        /* this is to account for the border */
2907bc9febe8749e98a3812a0dc4380ceae75c29450Johann        xd->mode_info_context++;
2917bc9febe8749e98a3812a0dc4380ceae75c29450Johann        x->partition_info++;
2927bc9febe8749e98a3812a0dc4380ceae75c29450Johann
2937bc9febe8749e98a3812a0dc4380ceae75c29450Johann        x->src.y_buffer +=
2947bc9febe8749e98a3812a0dc4380ceae75c29450Johann            16 * x->src.y_stride * (cpi->encoding_thread_count + 1) -
2957bc9febe8749e98a3812a0dc4380ceae75c29450Johann            16 * cm->mb_cols;
2967bc9febe8749e98a3812a0dc4380ceae75c29450Johann        x->src.u_buffer +=
2977bc9febe8749e98a3812a0dc4380ceae75c29450Johann            8 * x->src.uv_stride * (cpi->encoding_thread_count + 1) -
2987bc9febe8749e98a3812a0dc4380ceae75c29450Johann            8 * cm->mb_cols;
2997bc9febe8749e98a3812a0dc4380ceae75c29450Johann        x->src.v_buffer +=
3007bc9febe8749e98a3812a0dc4380ceae75c29450Johann            8 * x->src.uv_stride * (cpi->encoding_thread_count + 1) -
3017bc9febe8749e98a3812a0dc4380ceae75c29450Johann            8 * cm->mb_cols;
3027bc9febe8749e98a3812a0dc4380ceae75c29450Johann
3037bc9febe8749e98a3812a0dc4380ceae75c29450Johann        xd->mode_info_context +=
3047bc9febe8749e98a3812a0dc4380ceae75c29450Johann            xd->mode_info_stride * cpi->encoding_thread_count;
3057bc9febe8749e98a3812a0dc4380ceae75c29450Johann        x->partition_info += xd->mode_info_stride * cpi->encoding_thread_count;
3067bc9febe8749e98a3812a0dc4380ceae75c29450Johann        x->gf_active_ptr += cm->mb_cols * cpi->encoding_thread_count;
3077bc9febe8749e98a3812a0dc4380ceae75c29450Johann      }
3087bc9febe8749e98a3812a0dc4380ceae75c29450Johann      /* Signal that this thread has completed processing its rows. */
3097bc9febe8749e98a3812a0dc4380ceae75c29450Johann      sem_post(&cpi->h_event_end_encoding[ithread]);
31090d3ed91ae9228e1c8bab561b6138d4cb8c1e4fdAndreas Huber    }
3117bc9febe8749e98a3812a0dc4380ceae75c29450Johann  }
31290d3ed91ae9228e1c8bab561b6138d4cb8c1e4fdAndreas Huber
3137bc9febe8749e98a3812a0dc4380ceae75c29450Johann  /* printf("exit thread %d\n", ithread); */
3147bc9febe8749e98a3812a0dc4380ceae75c29450Johann  return 0;
31590d3ed91ae9228e1c8bab561b6138d4cb8c1e4fdAndreas Huber}
31690d3ed91ae9228e1c8bab561b6138d4cb8c1e4fdAndreas Huber
3177bc9febe8749e98a3812a0dc4380ceae75c29450Johannstatic void setup_mbby_copy(MACROBLOCK *mbdst, MACROBLOCK *mbsrc) {
3187bc9febe8749e98a3812a0dc4380ceae75c29450Johann  MACROBLOCK *x = mbsrc;
3197bc9febe8749e98a3812a0dc4380ceae75c29450Johann  MACROBLOCK *z = mbdst;
3207bc9febe8749e98a3812a0dc4380ceae75c29450Johann  int i;
3217bc9febe8749e98a3812a0dc4380ceae75c29450Johann
3227bc9febe8749e98a3812a0dc4380ceae75c29450Johann  z->ss = x->ss;
3237bc9febe8749e98a3812a0dc4380ceae75c29450Johann  z->ss_count = x->ss_count;
3247bc9febe8749e98a3812a0dc4380ceae75c29450Johann  z->searches_per_step = x->searches_per_step;
3257bc9febe8749e98a3812a0dc4380ceae75c29450Johann  z->errorperbit = x->errorperbit;
3267bc9febe8749e98a3812a0dc4380ceae75c29450Johann
3277bc9febe8749e98a3812a0dc4380ceae75c29450Johann  z->sadperbit16 = x->sadperbit16;
3287bc9febe8749e98a3812a0dc4380ceae75c29450Johann  z->sadperbit4 = x->sadperbit4;
3297bc9febe8749e98a3812a0dc4380ceae75c29450Johann
3307bc9febe8749e98a3812a0dc4380ceae75c29450Johann  /*
3317bc9febe8749e98a3812a0dc4380ceae75c29450Johann  z->mv_col_min    = x->mv_col_min;
3327bc9febe8749e98a3812a0dc4380ceae75c29450Johann  z->mv_col_max    = x->mv_col_max;
3337bc9febe8749e98a3812a0dc4380ceae75c29450Johann  z->mv_row_min    = x->mv_row_min;
3347bc9febe8749e98a3812a0dc4380ceae75c29450Johann  z->mv_row_max    = x->mv_row_max;
3357bc9febe8749e98a3812a0dc4380ceae75c29450Johann  */
3367bc9febe8749e98a3812a0dc4380ceae75c29450Johann
3377bc9febe8749e98a3812a0dc4380ceae75c29450Johann  z->short_fdct4x4 = x->short_fdct4x4;
3387bc9febe8749e98a3812a0dc4380ceae75c29450Johann  z->short_fdct8x4 = x->short_fdct8x4;
3397bc9febe8749e98a3812a0dc4380ceae75c29450Johann  z->short_walsh4x4 = x->short_walsh4x4;
3407bc9febe8749e98a3812a0dc4380ceae75c29450Johann  z->quantize_b = x->quantize_b;
3417bc9febe8749e98a3812a0dc4380ceae75c29450Johann  z->optimize = x->optimize;
3427bc9febe8749e98a3812a0dc4380ceae75c29450Johann
3437bc9febe8749e98a3812a0dc4380ceae75c29450Johann  /*
3447bc9febe8749e98a3812a0dc4380ceae75c29450Johann  z->mvc              = x->mvc;
3457bc9febe8749e98a3812a0dc4380ceae75c29450Johann  z->src.y_buffer      = x->src.y_buffer;
3467bc9febe8749e98a3812a0dc4380ceae75c29450Johann  z->src.u_buffer      = x->src.u_buffer;
3477bc9febe8749e98a3812a0dc4380ceae75c29450Johann  z->src.v_buffer      = x->src.v_buffer;
3487bc9febe8749e98a3812a0dc4380ceae75c29450Johann  */
3497bc9febe8749e98a3812a0dc4380ceae75c29450Johann
3507bc9febe8749e98a3812a0dc4380ceae75c29450Johann  z->mvcost[0] = x->mvcost[0];
3517bc9febe8749e98a3812a0dc4380ceae75c29450Johann  z->mvcost[1] = x->mvcost[1];
3527bc9febe8749e98a3812a0dc4380ceae75c29450Johann  z->mvsadcost[0] = x->mvsadcost[0];
3537bc9febe8749e98a3812a0dc4380ceae75c29450Johann  z->mvsadcost[1] = x->mvsadcost[1];
3547bc9febe8749e98a3812a0dc4380ceae75c29450Johann
3557bc9febe8749e98a3812a0dc4380ceae75c29450Johann  z->token_costs = x->token_costs;
3567bc9febe8749e98a3812a0dc4380ceae75c29450Johann  z->inter_bmode_costs = x->inter_bmode_costs;
3577bc9febe8749e98a3812a0dc4380ceae75c29450Johann  z->mbmode_cost = x->mbmode_cost;
3587bc9febe8749e98a3812a0dc4380ceae75c29450Johann  z->intra_uv_mode_cost = x->intra_uv_mode_cost;
3597bc9febe8749e98a3812a0dc4380ceae75c29450Johann  z->bmode_costs = x->bmode_costs;
3607bc9febe8749e98a3812a0dc4380ceae75c29450Johann
3617bc9febe8749e98a3812a0dc4380ceae75c29450Johann  for (i = 0; i < 25; ++i) {
3627bc9febe8749e98a3812a0dc4380ceae75c29450Johann    z->block[i].quant = x->block[i].quant;
3637bc9febe8749e98a3812a0dc4380ceae75c29450Johann    z->block[i].quant_fast = x->block[i].quant_fast;
3647bc9febe8749e98a3812a0dc4380ceae75c29450Johann    z->block[i].quant_shift = x->block[i].quant_shift;
3657bc9febe8749e98a3812a0dc4380ceae75c29450Johann    z->block[i].zbin = x->block[i].zbin;
3667bc9febe8749e98a3812a0dc4380ceae75c29450Johann    z->block[i].zrun_zbin_boost = x->block[i].zrun_zbin_boost;
3677bc9febe8749e98a3812a0dc4380ceae75c29450Johann    z->block[i].round = x->block[i].round;
3687bc9febe8749e98a3812a0dc4380ceae75c29450Johann    z->block[i].src_stride = x->block[i].src_stride;
3697bc9febe8749e98a3812a0dc4380ceae75c29450Johann  }
3707bc9febe8749e98a3812a0dc4380ceae75c29450Johann
3717bc9febe8749e98a3812a0dc4380ceae75c29450Johann  z->q_index = x->q_index;
3727bc9febe8749e98a3812a0dc4380ceae75c29450Johann  z->act_zbin_adj = x->act_zbin_adj;
3737bc9febe8749e98a3812a0dc4380ceae75c29450Johann  z->last_act_zbin_adj = x->last_act_zbin_adj;
3747bc9febe8749e98a3812a0dc4380ceae75c29450Johann
3757bc9febe8749e98a3812a0dc4380ceae75c29450Johann  {
3767bc9febe8749e98a3812a0dc4380ceae75c29450Johann    MACROBLOCKD *xd = &x->e_mbd;
3777bc9febe8749e98a3812a0dc4380ceae75c29450Johann    MACROBLOCKD *zd = &z->e_mbd;
37890d3ed91ae9228e1c8bab561b6138d4cb8c1e4fdAndreas Huber
37990d3ed91ae9228e1c8bab561b6138d4cb8c1e4fdAndreas Huber    /*
3807bc9febe8749e98a3812a0dc4380ceae75c29450Johann    zd->mode_info_context = xd->mode_info_context;
3817bc9febe8749e98a3812a0dc4380ceae75c29450Johann    zd->mode_info        = xd->mode_info;
3827bc9febe8749e98a3812a0dc4380ceae75c29450Johann
3837bc9febe8749e98a3812a0dc4380ceae75c29450Johann    zd->mode_info_stride  = xd->mode_info_stride;
3847bc9febe8749e98a3812a0dc4380ceae75c29450Johann    zd->frame_type       = xd->frame_type;
3857bc9febe8749e98a3812a0dc4380ceae75c29450Johann    zd->up_available     = xd->up_available   ;
3867bc9febe8749e98a3812a0dc4380ceae75c29450Johann    zd->left_available   = xd->left_available;
3877bc9febe8749e98a3812a0dc4380ceae75c29450Johann    zd->left_context     = xd->left_context;
3887bc9febe8749e98a3812a0dc4380ceae75c29450Johann    zd->last_frame_dc     = xd->last_frame_dc;
3897bc9febe8749e98a3812a0dc4380ceae75c29450Johann    zd->last_frame_dccons = xd->last_frame_dccons;
3907bc9febe8749e98a3812a0dc4380ceae75c29450Johann    zd->gold_frame_dc     = xd->gold_frame_dc;
3917bc9febe8749e98a3812a0dc4380ceae75c29450Johann    zd->gold_frame_dccons = xd->gold_frame_dccons;
3927bc9febe8749e98a3812a0dc4380ceae75c29450Johann    zd->mb_to_left_edge    = xd->mb_to_left_edge;
3937bc9febe8749e98a3812a0dc4380ceae75c29450Johann    zd->mb_to_right_edge   = xd->mb_to_right_edge;
3947bc9febe8749e98a3812a0dc4380ceae75c29450Johann    zd->mb_to_top_edge     = xd->mb_to_top_edge   ;
3957bc9febe8749e98a3812a0dc4380ceae75c29450Johann    zd->mb_to_bottom_edge  = xd->mb_to_bottom_edge;
3967bc9febe8749e98a3812a0dc4380ceae75c29450Johann    zd->gf_active_ptr     = xd->gf_active_ptr;
3977bc9febe8749e98a3812a0dc4380ceae75c29450Johann    zd->frames_since_golden       = xd->frames_since_golden;
3987bc9febe8749e98a3812a0dc4380ceae75c29450Johann    zd->frames_till_alt_ref_frame   = xd->frames_till_alt_ref_frame;
39990d3ed91ae9228e1c8bab561b6138d4cb8c1e4fdAndreas Huber    */
4007bc9febe8749e98a3812a0dc4380ceae75c29450Johann    zd->subpixel_predict = xd->subpixel_predict;
4017bc9febe8749e98a3812a0dc4380ceae75c29450Johann    zd->subpixel_predict8x4 = xd->subpixel_predict8x4;
4027bc9febe8749e98a3812a0dc4380ceae75c29450Johann    zd->subpixel_predict8x8 = xd->subpixel_predict8x8;
4037bc9febe8749e98a3812a0dc4380ceae75c29450Johann    zd->subpixel_predict16x16 = xd->subpixel_predict16x16;
4047bc9febe8749e98a3812a0dc4380ceae75c29450Johann    zd->segmentation_enabled = xd->segmentation_enabled;
4057bc9febe8749e98a3812a0dc4380ceae75c29450Johann    zd->mb_segement_abs_delta = xd->mb_segement_abs_delta;
4067bc9febe8749e98a3812a0dc4380ceae75c29450Johann    memcpy(zd->segment_feature_data, xd->segment_feature_data,
4077bc9febe8749e98a3812a0dc4380ceae75c29450Johann           sizeof(xd->segment_feature_data));
4087bc9febe8749e98a3812a0dc4380ceae75c29450Johann
4097bc9febe8749e98a3812a0dc4380ceae75c29450Johann    memcpy(zd->dequant_y1_dc, xd->dequant_y1_dc, sizeof(xd->dequant_y1_dc));
4107bc9febe8749e98a3812a0dc4380ceae75c29450Johann    memcpy(zd->dequant_y1, xd->dequant_y1, sizeof(xd->dequant_y1));
4117bc9febe8749e98a3812a0dc4380ceae75c29450Johann    memcpy(zd->dequant_y2, xd->dequant_y2, sizeof(xd->dequant_y2));
4127bc9febe8749e98a3812a0dc4380ceae75c29450Johann    memcpy(zd->dequant_uv, xd->dequant_uv, sizeof(xd->dequant_uv));
4131b362b15af34006e6a11974088a46d42b903418eJohann
4141b362b15af34006e6a11974088a46d42b903418eJohann#if 1
4157bc9febe8749e98a3812a0dc4380ceae75c29450Johann    /*TODO:  Remove dequant from BLOCKD.  This is a temporary solution until
4167bc9febe8749e98a3812a0dc4380ceae75c29450Johann     * the quantizer code uses a passed in pointer to the dequant constants.
4177bc9febe8749e98a3812a0dc4380ceae75c29450Johann     * This will also require modifications to the x86 and neon assembly.
4187bc9febe8749e98a3812a0dc4380ceae75c29450Johann     * */
4197bc9febe8749e98a3812a0dc4380ceae75c29450Johann    for (i = 0; i < 16; ++i) zd->block[i].dequant = zd->dequant_y1;
4207bc9febe8749e98a3812a0dc4380ceae75c29450Johann    for (i = 16; i < 24; ++i) zd->block[i].dequant = zd->dequant_uv;
4217bc9febe8749e98a3812a0dc4380ceae75c29450Johann    zd->block[24].dequant = zd->dequant_y2;
4221b362b15af34006e6a11974088a46d42b903418eJohann#endif
423ba164dffc5a6795bce97fae02b51ccf3330e15e4hkuang
4247bc9febe8749e98a3812a0dc4380ceae75c29450Johann    memcpy(z->rd_threshes, x->rd_threshes, sizeof(x->rd_threshes));
4257bc9febe8749e98a3812a0dc4380ceae75c29450Johann    memcpy(z->rd_thresh_mult, x->rd_thresh_mult, sizeof(x->rd_thresh_mult));
426ba164dffc5a6795bce97fae02b51ccf3330e15e4hkuang
4277bc9febe8749e98a3812a0dc4380ceae75c29450Johann    z->zbin_over_quant = x->zbin_over_quant;
4287bc9febe8749e98a3812a0dc4380ceae75c29450Johann    z->zbin_mode_boost_enabled = x->zbin_mode_boost_enabled;
4297bc9febe8749e98a3812a0dc4380ceae75c29450Johann    z->zbin_mode_boost = x->zbin_mode_boost;
430ba164dffc5a6795bce97fae02b51ccf3330e15e4hkuang
4317bc9febe8749e98a3812a0dc4380ceae75c29450Johann    memset(z->error_bins, 0, sizeof(z->error_bins));
4327bc9febe8749e98a3812a0dc4380ceae75c29450Johann  }
43390d3ed91ae9228e1c8bab561b6138d4cb8c1e4fdAndreas Huber}
43490d3ed91ae9228e1c8bab561b6138d4cb8c1e4fdAndreas Huber
4357bc9febe8749e98a3812a0dc4380ceae75c29450Johannvoid vp8cx_init_mbrthread_data(VP8_COMP *cpi, MACROBLOCK *x,
4367bc9febe8749e98a3812a0dc4380ceae75c29450Johann                               MB_ROW_COMP *mbr_ei, int count) {
4377bc9febe8749e98a3812a0dc4380ceae75c29450Johann  VP8_COMMON *const cm = &cpi->common;
4387bc9febe8749e98a3812a0dc4380ceae75c29450Johann  MACROBLOCKD *const xd = &x->e_mbd;
4397bc9febe8749e98a3812a0dc4380ceae75c29450Johann  int i;
44090d3ed91ae9228e1c8bab561b6138d4cb8c1e4fdAndreas Huber
4417bc9febe8749e98a3812a0dc4380ceae75c29450Johann  for (i = 0; i < count; ++i) {
4427bc9febe8749e98a3812a0dc4380ceae75c29450Johann    MACROBLOCK *mb = &mbr_ei[i].mb;
4437bc9febe8749e98a3812a0dc4380ceae75c29450Johann    MACROBLOCKD *mbd = &mb->e_mbd;
44490d3ed91ae9228e1c8bab561b6138d4cb8c1e4fdAndreas Huber
4457bc9febe8749e98a3812a0dc4380ceae75c29450Johann    mbd->subpixel_predict = xd->subpixel_predict;
4467bc9febe8749e98a3812a0dc4380ceae75c29450Johann    mbd->subpixel_predict8x4 = xd->subpixel_predict8x4;
4477bc9febe8749e98a3812a0dc4380ceae75c29450Johann    mbd->subpixel_predict8x8 = xd->subpixel_predict8x8;
4487bc9febe8749e98a3812a0dc4380ceae75c29450Johann    mbd->subpixel_predict16x16 = xd->subpixel_predict16x16;
4497bc9febe8749e98a3812a0dc4380ceae75c29450Johann    mb->gf_active_ptr = x->gf_active_ptr;
45090d3ed91ae9228e1c8bab561b6138d4cb8c1e4fdAndreas Huber
4517bc9febe8749e98a3812a0dc4380ceae75c29450Johann    memset(mbr_ei[i].segment_counts, 0, sizeof(mbr_ei[i].segment_counts));
4527bc9febe8749e98a3812a0dc4380ceae75c29450Johann    mbr_ei[i].totalrate = 0;
45390d3ed91ae9228e1c8bab561b6138d4cb8c1e4fdAndreas Huber
4547bc9febe8749e98a3812a0dc4380ceae75c29450Johann    mb->partition_info = x->pi + x->e_mbd.mode_info_stride * (i + 1);
455f71323e297a928af368937089d3ed71239786f86Andreas Huber
4567bc9febe8749e98a3812a0dc4380ceae75c29450Johann    mbd->frame_type = cm->frame_type;
45790d3ed91ae9228e1c8bab561b6138d4cb8c1e4fdAndreas Huber
4587bc9febe8749e98a3812a0dc4380ceae75c29450Johann    mb->src = *cpi->Source;
4597bc9febe8749e98a3812a0dc4380ceae75c29450Johann    mbd->pre = cm->yv12_fb[cm->lst_fb_idx];
4607bc9febe8749e98a3812a0dc4380ceae75c29450Johann    mbd->dst = cm->yv12_fb[cm->new_fb_idx];
46190d3ed91ae9228e1c8bab561b6138d4cb8c1e4fdAndreas Huber
4627bc9febe8749e98a3812a0dc4380ceae75c29450Johann    mb->src.y_buffer += 16 * x->src.y_stride * (i + 1);
4637bc9febe8749e98a3812a0dc4380ceae75c29450Johann    mb->src.u_buffer += 8 * x->src.uv_stride * (i + 1);
4647bc9febe8749e98a3812a0dc4380ceae75c29450Johann    mb->src.v_buffer += 8 * x->src.uv_stride * (i + 1);
46590d3ed91ae9228e1c8bab561b6138d4cb8c1e4fdAndreas Huber
4667bc9febe8749e98a3812a0dc4380ceae75c29450Johann    vp8_build_block_offsets(mb);
46790d3ed91ae9228e1c8bab561b6138d4cb8c1e4fdAndreas Huber
4687bc9febe8749e98a3812a0dc4380ceae75c29450Johann    mbd->left_context = &cm->left_context;
4697bc9febe8749e98a3812a0dc4380ceae75c29450Johann    mb->mvc = cm->fc.mvc;
47090d3ed91ae9228e1c8bab561b6138d4cb8c1e4fdAndreas Huber
4717bc9febe8749e98a3812a0dc4380ceae75c29450Johann    setup_mbby_copy(&mbr_ei[i].mb, x);
47290d3ed91ae9228e1c8bab561b6138d4cb8c1e4fdAndreas Huber
4737bc9febe8749e98a3812a0dc4380ceae75c29450Johann    mbd->fullpixel_mask = 0xffffffff;
4747bc9febe8749e98a3812a0dc4380ceae75c29450Johann    if (cm->full_pixel) mbd->fullpixel_mask = 0xfffffff8;
4751b362b15af34006e6a11974088a46d42b903418eJohann
4767bc9febe8749e98a3812a0dc4380ceae75c29450Johann    vp8_zero(mb->coef_counts);
4777bc9febe8749e98a3812a0dc4380ceae75c29450Johann    vp8_zero(x->ymode_count);
4787bc9febe8749e98a3812a0dc4380ceae75c29450Johann    mb->skip_true_count = 0;
4797bc9febe8749e98a3812a0dc4380ceae75c29450Johann    vp8_zero(mb->MVcount);
4807bc9febe8749e98a3812a0dc4380ceae75c29450Johann    mb->prediction_error = 0;
4817bc9febe8749e98a3812a0dc4380ceae75c29450Johann    mb->intra_error = 0;
4827bc9febe8749e98a3812a0dc4380ceae75c29450Johann    vp8_zero(mb->count_mb_ref_frame_usage);
4837bc9febe8749e98a3812a0dc4380ceae75c29450Johann    mb->mbs_tested_so_far = 0;
4847bc9febe8749e98a3812a0dc4380ceae75c29450Johann    mb->mbs_zero_last_dot_suppress = 0;
4857bc9febe8749e98a3812a0dc4380ceae75c29450Johann  }
48690d3ed91ae9228e1c8bab561b6138d4cb8c1e4fdAndreas Huber}
48790d3ed91ae9228e1c8bab561b6138d4cb8c1e4fdAndreas Huber
4887bc9febe8749e98a3812a0dc4380ceae75c29450Johannint vp8cx_create_encoder_threads(VP8_COMP *cpi) {
4897bc9febe8749e98a3812a0dc4380ceae75c29450Johann  const VP8_COMMON *cm = &cpi->common;
49090d3ed91ae9228e1c8bab561b6138d4cb8c1e4fdAndreas Huber
491df37111358d02836cb29bbcb9c6e4c95dff90a16Johann  vpx_atomic_init(&cpi->b_multi_threaded, 0);
4927bc9febe8749e98a3812a0dc4380ceae75c29450Johann  cpi->encoding_thread_count = 0;
4937bc9febe8749e98a3812a0dc4380ceae75c29450Johann  cpi->b_lpf_running = 0;
49490d3ed91ae9228e1c8bab561b6138d4cb8c1e4fdAndreas Huber
4957bc9febe8749e98a3812a0dc4380ceae75c29450Johann  if (cm->processor_core_count > 1 && cpi->oxcf.multi_threaded > 1) {
4967bc9febe8749e98a3812a0dc4380ceae75c29450Johann    int ithread;
4977bc9febe8749e98a3812a0dc4380ceae75c29450Johann    int th_count = cpi->oxcf.multi_threaded - 1;
4987bc9febe8749e98a3812a0dc4380ceae75c29450Johann    int rc = 0;
4991b362b15af34006e6a11974088a46d42b903418eJohann
5007bc9febe8749e98a3812a0dc4380ceae75c29450Johann    /* don't allocate more threads than cores available */
5017bc9febe8749e98a3812a0dc4380ceae75c29450Johann    if (cpi->oxcf.multi_threaded > cm->processor_core_count) {
5027bc9febe8749e98a3812a0dc4380ceae75c29450Johann      th_count = cm->processor_core_count - 1;
5037bc9febe8749e98a3812a0dc4380ceae75c29450Johann    }
5041b362b15af34006e6a11974088a46d42b903418eJohann
5057bc9febe8749e98a3812a0dc4380ceae75c29450Johann    /* we have th_count + 1 (main) threads processing one row each */
5067bc9febe8749e98a3812a0dc4380ceae75c29450Johann    /* no point to have more threads than the sync range allows */
5077bc9febe8749e98a3812a0dc4380ceae75c29450Johann    if (th_count > ((cm->mb_cols / cpi->mt_sync_range) - 1)) {
5087bc9febe8749e98a3812a0dc4380ceae75c29450Johann      th_count = (cm->mb_cols / cpi->mt_sync_range) - 1;
5097bc9febe8749e98a3812a0dc4380ceae75c29450Johann    }
5101b362b15af34006e6a11974088a46d42b903418eJohann
5117bc9febe8749e98a3812a0dc4380ceae75c29450Johann    if (th_count == 0) return 0;
5121b362b15af34006e6a11974088a46d42b903418eJohann
5137bc9febe8749e98a3812a0dc4380ceae75c29450Johann    CHECK_MEM_ERROR(cpi->h_encoding_thread,
5147bc9febe8749e98a3812a0dc4380ceae75c29450Johann                    vpx_malloc(sizeof(pthread_t) * th_count));
5157bc9febe8749e98a3812a0dc4380ceae75c29450Johann    CHECK_MEM_ERROR(cpi->h_event_start_encoding,
5167bc9febe8749e98a3812a0dc4380ceae75c29450Johann                    vpx_malloc(sizeof(sem_t) * th_count));
5177bc9febe8749e98a3812a0dc4380ceae75c29450Johann    CHECK_MEM_ERROR(cpi->h_event_end_encoding,
5187bc9febe8749e98a3812a0dc4380ceae75c29450Johann                    vpx_malloc(sizeof(sem_t) * th_count));
5197bc9febe8749e98a3812a0dc4380ceae75c29450Johann    CHECK_MEM_ERROR(cpi->mb_row_ei,
5207bc9febe8749e98a3812a0dc4380ceae75c29450Johann                    vpx_memalign(32, sizeof(MB_ROW_COMP) * th_count));
5217bc9febe8749e98a3812a0dc4380ceae75c29450Johann    memset(cpi->mb_row_ei, 0, sizeof(MB_ROW_COMP) * th_count);
5227bc9febe8749e98a3812a0dc4380ceae75c29450Johann    CHECK_MEM_ERROR(cpi->en_thread_data,
5237bc9febe8749e98a3812a0dc4380ceae75c29450Johann                    vpx_malloc(sizeof(ENCODETHREAD_DATA) * th_count));
52468e1c830ade592be74773e249bf94e2bbfb50de7Johann
525df37111358d02836cb29bbcb9c6e4c95dff90a16Johann    vpx_atomic_store_release(&cpi->b_multi_threaded, 1);
5267bc9febe8749e98a3812a0dc4380ceae75c29450Johann    cpi->encoding_thread_count = th_count;
52790d3ed91ae9228e1c8bab561b6138d4cb8c1e4fdAndreas Huber
5287bc9febe8749e98a3812a0dc4380ceae75c29450Johann    /*
5297bc9febe8749e98a3812a0dc4380ceae75c29450Johann    printf("[VP8:] multi_threaded encoding is enabled with %d threads\n\n",
5307bc9febe8749e98a3812a0dc4380ceae75c29450Johann           (cpi->encoding_thread_count +1));
5317bc9febe8749e98a3812a0dc4380ceae75c29450Johann    */
5321b362b15af34006e6a11974088a46d42b903418eJohann
5337bc9febe8749e98a3812a0dc4380ceae75c29450Johann    for (ithread = 0; ithread < th_count; ++ithread) {
5347bc9febe8749e98a3812a0dc4380ceae75c29450Johann      ENCODETHREAD_DATA *ethd = &cpi->en_thread_data[ithread];
53590d3ed91ae9228e1c8bab561b6138d4cb8c1e4fdAndreas Huber
5367bc9febe8749e98a3812a0dc4380ceae75c29450Johann      /* Setup block ptrs and offsets */
5377bc9febe8749e98a3812a0dc4380ceae75c29450Johann      vp8_setup_block_ptrs(&cpi->mb_row_ei[ithread].mb);
5387bc9febe8749e98a3812a0dc4380ceae75c29450Johann      vp8_setup_block_dptrs(&cpi->mb_row_ei[ithread].mb.e_mbd);
53990d3ed91ae9228e1c8bab561b6138d4cb8c1e4fdAndreas Huber
5407bc9febe8749e98a3812a0dc4380ceae75c29450Johann      sem_init(&cpi->h_event_start_encoding[ithread], 0, 0);
5417bc9febe8749e98a3812a0dc4380ceae75c29450Johann      sem_init(&cpi->h_event_end_encoding[ithread], 0, 0);
5421b362b15af34006e6a11974088a46d42b903418eJohann
5437bc9febe8749e98a3812a0dc4380ceae75c29450Johann      ethd->ithread = ithread;
5447bc9febe8749e98a3812a0dc4380ceae75c29450Johann      ethd->ptr1 = (void *)cpi;
5457bc9febe8749e98a3812a0dc4380ceae75c29450Johann      ethd->ptr2 = (void *)&cpi->mb_row_ei[ithread];
5461b362b15af34006e6a11974088a46d42b903418eJohann
5477bc9febe8749e98a3812a0dc4380ceae75c29450Johann      rc = pthread_create(&cpi->h_encoding_thread[ithread], 0,
5487bc9febe8749e98a3812a0dc4380ceae75c29450Johann                          thread_encoding_proc, ethd);
5497bc9febe8749e98a3812a0dc4380ceae75c29450Johann      if (rc) break;
5507bc9febe8749e98a3812a0dc4380ceae75c29450Johann    }
55168e1c830ade592be74773e249bf94e2bbfb50de7Johann
5527bc9febe8749e98a3812a0dc4380ceae75c29450Johann    if (rc) {
5537bc9febe8749e98a3812a0dc4380ceae75c29450Johann      /* shutdown other threads */
554df37111358d02836cb29bbcb9c6e4c95dff90a16Johann      vpx_atomic_store_release(&cpi->b_multi_threaded, 0);
5557bc9febe8749e98a3812a0dc4380ceae75c29450Johann      for (--ithread; ithread >= 0; ithread--) {
5567bc9febe8749e98a3812a0dc4380ceae75c29450Johann        pthread_join(cpi->h_encoding_thread[ithread], 0);
5577bc9febe8749e98a3812a0dc4380ceae75c29450Johann        sem_destroy(&cpi->h_event_start_encoding[ithread]);
5587bc9febe8749e98a3812a0dc4380ceae75c29450Johann        sem_destroy(&cpi->h_event_end_encoding[ithread]);
5597bc9febe8749e98a3812a0dc4380ceae75c29450Johann      }
5607bc9febe8749e98a3812a0dc4380ceae75c29450Johann
5617bc9febe8749e98a3812a0dc4380ceae75c29450Johann      /* free thread related resources */
5627bc9febe8749e98a3812a0dc4380ceae75c29450Johann      vpx_free(cpi->h_event_start_encoding);
5637bc9febe8749e98a3812a0dc4380ceae75c29450Johann      vpx_free(cpi->h_event_end_encoding);
5647bc9febe8749e98a3812a0dc4380ceae75c29450Johann      vpx_free(cpi->h_encoding_thread);
5657bc9febe8749e98a3812a0dc4380ceae75c29450Johann      vpx_free(cpi->mb_row_ei);
5667bc9febe8749e98a3812a0dc4380ceae75c29450Johann      vpx_free(cpi->en_thread_data);
5677bc9febe8749e98a3812a0dc4380ceae75c29450Johann
5687bc9febe8749e98a3812a0dc4380ceae75c29450Johann      return -1;
56990d3ed91ae9228e1c8bab561b6138d4cb8c1e4fdAndreas Huber    }
57090d3ed91ae9228e1c8bab561b6138d4cb8c1e4fdAndreas Huber
57190d3ed91ae9228e1c8bab561b6138d4cb8c1e4fdAndreas Huber    {
5727bc9febe8749e98a3812a0dc4380ceae75c29450Johann      LPFTHREAD_DATA *lpfthd = &cpi->lpf_thread_data;
57390d3ed91ae9228e1c8bab561b6138d4cb8c1e4fdAndreas Huber
5747bc9febe8749e98a3812a0dc4380ceae75c29450Johann      sem_init(&cpi->h_event_start_lpf, 0, 0);
5757bc9febe8749e98a3812a0dc4380ceae75c29450Johann      sem_init(&cpi->h_event_end_lpf, 0, 0);
57679f15823c34ae1e423108295e416213200bb280fAndreas Huber
5777bc9febe8749e98a3812a0dc4380ceae75c29450Johann      lpfthd->ptr1 = (void *)cpi;
5787bc9febe8749e98a3812a0dc4380ceae75c29450Johann      rc = pthread_create(&cpi->h_filter_thread, 0, thread_loopfilter, lpfthd);
57990d3ed91ae9228e1c8bab561b6138d4cb8c1e4fdAndreas Huber
5807bc9febe8749e98a3812a0dc4380ceae75c29450Johann      if (rc) {
5817bc9febe8749e98a3812a0dc4380ceae75c29450Johann        /* shutdown other threads */
582df37111358d02836cb29bbcb9c6e4c95dff90a16Johann        vpx_atomic_store_release(&cpi->b_multi_threaded, 0);
5837bc9febe8749e98a3812a0dc4380ceae75c29450Johann        for (--ithread; ithread >= 0; ithread--) {
5847bc9febe8749e98a3812a0dc4380ceae75c29450Johann          sem_post(&cpi->h_event_start_encoding[ithread]);
5857bc9febe8749e98a3812a0dc4380ceae75c29450Johann          sem_post(&cpi->h_event_end_encoding[ithread]);
5867bc9febe8749e98a3812a0dc4380ceae75c29450Johann          pthread_join(cpi->h_encoding_thread[ithread], 0);
5877bc9febe8749e98a3812a0dc4380ceae75c29450Johann          sem_destroy(&cpi->h_event_start_encoding[ithread]);
5887bc9febe8749e98a3812a0dc4380ceae75c29450Johann          sem_destroy(&cpi->h_event_end_encoding[ithread]);
58990d3ed91ae9228e1c8bab561b6138d4cb8c1e4fdAndreas Huber        }
59079f15823c34ae1e423108295e416213200bb280fAndreas Huber        sem_destroy(&cpi->h_event_end_lpf);
59179f15823c34ae1e423108295e416213200bb280fAndreas Huber        sem_destroy(&cpi->h_event_start_lpf);
59279f15823c34ae1e423108295e416213200bb280fAndreas Huber
5931b362b15af34006e6a11974088a46d42b903418eJohann        /* free thread related resources */
59479f15823c34ae1e423108295e416213200bb280fAndreas Huber        vpx_free(cpi->h_event_start_encoding);
5957bc9febe8749e98a3812a0dc4380ceae75c29450Johann        vpx_free(cpi->h_event_end_encoding);
59690d3ed91ae9228e1c8bab561b6138d4cb8c1e4fdAndreas Huber        vpx_free(cpi->h_encoding_thread);
59790d3ed91ae9228e1c8bab561b6138d4cb8c1e4fdAndreas Huber        vpx_free(cpi->mb_row_ei);
59890d3ed91ae9228e1c8bab561b6138d4cb8c1e4fdAndreas Huber        vpx_free(cpi->en_thread_data);
5997bc9febe8749e98a3812a0dc4380ceae75c29450Johann
6007bc9febe8749e98a3812a0dc4380ceae75c29450Johann        return -2;
6017bc9febe8749e98a3812a0dc4380ceae75c29450Johann      }
60290d3ed91ae9228e1c8bab561b6138d4cb8c1e4fdAndreas Huber    }
6037bc9febe8749e98a3812a0dc4380ceae75c29450Johann  }
6047bc9febe8749e98a3812a0dc4380ceae75c29450Johann  return 0;
6057bc9febe8749e98a3812a0dc4380ceae75c29450Johann}
6067bc9febe8749e98a3812a0dc4380ceae75c29450Johann
6077bc9febe8749e98a3812a0dc4380ceae75c29450Johannvoid vp8cx_remove_encoder_threads(VP8_COMP *cpi) {
608df37111358d02836cb29bbcb9c6e4c95dff90a16Johann  if (vpx_atomic_load_acquire(&cpi->b_multi_threaded)) {
6097bc9febe8749e98a3812a0dc4380ceae75c29450Johann    /* shutdown other threads */
610df37111358d02836cb29bbcb9c6e4c95dff90a16Johann    vpx_atomic_store_release(&cpi->b_multi_threaded, 0);
6117bc9febe8749e98a3812a0dc4380ceae75c29450Johann    {
6127bc9febe8749e98a3812a0dc4380ceae75c29450Johann      int i;
6137bc9febe8749e98a3812a0dc4380ceae75c29450Johann
6147bc9febe8749e98a3812a0dc4380ceae75c29450Johann      for (i = 0; i < cpi->encoding_thread_count; ++i) {
6157bc9febe8749e98a3812a0dc4380ceae75c29450Johann        sem_post(&cpi->h_event_start_encoding[i]);
6167bc9febe8749e98a3812a0dc4380ceae75c29450Johann        sem_post(&cpi->h_event_end_encoding[i]);
6177bc9febe8749e98a3812a0dc4380ceae75c29450Johann
6187bc9febe8749e98a3812a0dc4380ceae75c29450Johann        pthread_join(cpi->h_encoding_thread[i], 0);
6197bc9febe8749e98a3812a0dc4380ceae75c29450Johann
6207bc9febe8749e98a3812a0dc4380ceae75c29450Johann        sem_destroy(&cpi->h_event_start_encoding[i]);
6217bc9febe8749e98a3812a0dc4380ceae75c29450Johann        sem_destroy(&cpi->h_event_end_encoding[i]);
6227bc9febe8749e98a3812a0dc4380ceae75c29450Johann      }
6237bc9febe8749e98a3812a0dc4380ceae75c29450Johann
6247bc9febe8749e98a3812a0dc4380ceae75c29450Johann      sem_post(&cpi->h_event_start_lpf);
6257bc9febe8749e98a3812a0dc4380ceae75c29450Johann      pthread_join(cpi->h_filter_thread, 0);
6267bc9febe8749e98a3812a0dc4380ceae75c29450Johann    }
6277bc9febe8749e98a3812a0dc4380ceae75c29450Johann
6287bc9febe8749e98a3812a0dc4380ceae75c29450Johann    sem_destroy(&cpi->h_event_end_lpf);
6297bc9febe8749e98a3812a0dc4380ceae75c29450Johann    sem_destroy(&cpi->h_event_start_lpf);
6307bc9febe8749e98a3812a0dc4380ceae75c29450Johann
6317bc9febe8749e98a3812a0dc4380ceae75c29450Johann    /* free thread related resources */
6327bc9febe8749e98a3812a0dc4380ceae75c29450Johann    vpx_free(cpi->h_event_start_encoding);
6337bc9febe8749e98a3812a0dc4380ceae75c29450Johann    vpx_free(cpi->h_event_end_encoding);
6347bc9febe8749e98a3812a0dc4380ceae75c29450Johann    vpx_free(cpi->h_encoding_thread);
6357bc9febe8749e98a3812a0dc4380ceae75c29450Johann    vpx_free(cpi->mb_row_ei);
6367bc9febe8749e98a3812a0dc4380ceae75c29450Johann    vpx_free(cpi->en_thread_data);
6377bc9febe8749e98a3812a0dc4380ceae75c29450Johann  }
63890d3ed91ae9228e1c8bab561b6138d4cb8c1e4fdAndreas Huber}
63979f15823c34ae1e423108295e416213200bb280fAndreas Huber#endif
640