media/libvpx/vp8/encoder/lookahead.c

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/media/libvpx/vp8/encoder/lookahead.c	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,230 @@
     1.4 +/*
     1.5 + *  Copyright (c) 2011 The WebM project authors. All Rights Reserved.
     1.6 + *
     1.7 + *  Use of this source code is governed by a BSD-style license
     1.8 + *  that can be found in the LICENSE file in the root of the source
     1.9 + *  tree. An additional intellectual property rights grant can be found
    1.10 + *  in the file PATENTS.  All contributing project authors may
    1.11 + *  be found in the AUTHORS file in the root of the source tree.
    1.12 + */
    1.13 +#include <assert.h>
    1.14 +#include <stdlib.h>
    1.15 +#include "vpx_config.h"
    1.16 +#include "lookahead.h"
    1.17 +#include "vp8/common/extend.h"
    1.18 +
    1.19 +#define MAX_LAG_BUFFERS (CONFIG_REALTIME_ONLY? 1 : 25)
    1.20 +
    1.21 +struct lookahead_ctx
    1.22 +{
    1.23 +    unsigned int max_sz;         /* Absolute size of the queue */
    1.24 +    unsigned int sz;             /* Number of buffers currently in the queue */
    1.25 +    unsigned int read_idx;       /* Read index */
    1.26 +    unsigned int write_idx;      /* Write index */
    1.27 +    struct lookahead_entry *buf; /* Buffer list */
    1.28 +};
    1.29 +
    1.30 +
    1.31 +/* Return the buffer at the given absolute index and increment the index */
    1.32 +static struct lookahead_entry *
    1.33 +pop(struct lookahead_ctx *ctx,
    1.34 +    unsigned int         *idx)
    1.35 +{
    1.36 +    unsigned int            index = *idx;
    1.37 +    struct lookahead_entry *buf = ctx->buf + index;
    1.38 +
    1.39 +    assert(index < ctx->max_sz);
    1.40 +    if(++index >= ctx->max_sz)
    1.41 +        index -= ctx->max_sz;
    1.42 +    *idx = index;
    1.43 +    return buf;
    1.44 +}
    1.45 +
    1.46 +
    1.47 +void
    1.48 +vp8_lookahead_destroy(struct lookahead_ctx *ctx)
    1.49 +{
    1.50 +    if(ctx)
    1.51 +    {
    1.52 +        if(ctx->buf)
    1.53 +        {
    1.54 +            unsigned int i;
    1.55 +
    1.56 +            for(i = 0; i < ctx->max_sz; i++)
    1.57 +                vp8_yv12_de_alloc_frame_buffer(&ctx->buf[i].img);
    1.58 +            free(ctx->buf);
    1.59 +        }
    1.60 +        free(ctx);
    1.61 +    }
    1.62 +}
    1.63 +
    1.64 +
    1.65 +struct lookahead_ctx*
    1.66 +vp8_lookahead_init(unsigned int width,
    1.67 +                   unsigned int height,
    1.68 +                   unsigned int depth)
    1.69 +{
    1.70 +    struct lookahead_ctx *ctx = NULL;
    1.71 +    unsigned int i;
    1.72 +
    1.73 +    /* Clamp the lookahead queue depth */
    1.74 +    if(depth < 1)
    1.75 +        depth = 1;
    1.76 +    else if(depth > MAX_LAG_BUFFERS)
    1.77 +        depth = MAX_LAG_BUFFERS;
    1.78 +
    1.79 +    /* Keep last frame in lookahead buffer by increasing depth by 1.*/
    1.80 +    depth += 1;
    1.81 +
    1.82 +    /* Align the buffer dimensions */
    1.83 +    width = (width + 15) & ~15;
    1.84 +    height = (height + 15) & ~15;
    1.85 +
    1.86 +    /* Allocate the lookahead structures */
    1.87 +    ctx = calloc(1, sizeof(*ctx));
    1.88 +    if(ctx)
    1.89 +    {
    1.90 +        ctx->max_sz = depth;
    1.91 +        ctx->buf = calloc(depth, sizeof(*ctx->buf));
    1.92 +        if(!ctx->buf)
    1.93 +            goto bail;
    1.94 +        for(i=0; i<depth; i++)
    1.95 +            if (vp8_yv12_alloc_frame_buffer(&ctx->buf[i].img,
    1.96 +                                            width, height, VP8BORDERINPIXELS))
    1.97 +                goto bail;
    1.98 +    }
    1.99 +    return ctx;
   1.100 +bail:
   1.101 +    vp8_lookahead_destroy(ctx);
   1.102 +    return NULL;
   1.103 +}
   1.104 +
   1.105 +
   1.106 +int
   1.107 +vp8_lookahead_push(struct lookahead_ctx *ctx,
   1.108 +                   YV12_BUFFER_CONFIG   *src,
   1.109 +                   int64_t               ts_start,
   1.110 +                   int64_t               ts_end,
   1.111 +                   unsigned int          flags,
   1.112 +                   unsigned char        *active_map)
   1.113 +{
   1.114 +    struct lookahead_entry* buf;
   1.115 +    int row, col, active_end;
   1.116 +    int mb_rows = (src->y_height + 15) >> 4;
   1.117 +    int mb_cols = (src->y_width + 15) >> 4;
   1.118 +
   1.119 +    if(ctx->sz + 2 > ctx->max_sz)
   1.120 +        return 1;
   1.121 +    ctx->sz++;
   1.122 +    buf = pop(ctx, &ctx->write_idx);
   1.123 +
   1.124 +    /* Only do this partial copy if the following conditions are all met:
   1.125 +     * 1. Lookahead queue has has size of 1.
   1.126 +     * 2. Active map is provided.
   1.127 +     * 3. This is not a key frame, golden nor altref frame.
   1.128 +     */
   1.129 +    if (ctx->max_sz == 1 && active_map && !flags)
   1.130 +    {
   1.131 +        for (row = 0; row < mb_rows; ++row)
   1.132 +        {
   1.133 +            col = 0;
   1.134 +
   1.135 +            while (1)
   1.136 +            {
   1.137 +                /* Find the first active macroblock in this row. */
   1.138 +                for (; col < mb_cols; ++col)
   1.139 +                {
   1.140 +                    if (active_map[col])
   1.141 +                        break;
   1.142 +                }
   1.143 +
   1.144 +                /* No more active macroblock in this row. */
   1.145 +                if (col == mb_cols)
   1.146 +                    break;
   1.147 +
   1.148 +                /* Find the end of active region in this row. */
   1.149 +                active_end = col;
   1.150 +
   1.151 +                for (; active_end < mb_cols; ++active_end)
   1.152 +                {
   1.153 +                    if (!active_map[active_end])
   1.154 +                        break;
   1.155 +                }
   1.156 +
   1.157 +                /* Only copy this active region. */
   1.158 +                vp8_copy_and_extend_frame_with_rect(src, &buf->img,
   1.159 +                                                    row << 4,
   1.160 +                                                    col << 4, 16,
   1.161 +                                                    (active_end - col) << 4);
   1.162 +
   1.163 +                /* Start again from the end of this active region. */
   1.164 +                col = active_end;
   1.165 +            }
   1.166 +
   1.167 +            active_map += mb_cols;
   1.168 +        }
   1.169 +    }
   1.170 +    else
   1.171 +    {
   1.172 +        vp8_copy_and_extend_frame(src, &buf->img);
   1.173 +    }
   1.174 +    buf->ts_start = ts_start;
   1.175 +    buf->ts_end = ts_end;
   1.176 +    buf->flags = flags;
   1.177 +    return 0;
   1.178 +}
   1.179 +
   1.180 +
   1.181 +struct lookahead_entry*
   1.182 +vp8_lookahead_pop(struct lookahead_ctx *ctx,
   1.183 +                  int                   drain)
   1.184 +{
   1.185 +    struct lookahead_entry* buf = NULL;
   1.186 +
   1.187 +    if(ctx->sz && (drain || ctx->sz == ctx->max_sz - 1))
   1.188 +    {
   1.189 +        buf = pop(ctx, &ctx->read_idx);
   1.190 +        ctx->sz--;
   1.191 +    }
   1.192 +    return buf;
   1.193 +}
   1.194 +
   1.195 +
   1.196 +struct lookahead_entry*
   1.197 +vp8_lookahead_peek(struct lookahead_ctx *ctx,
   1.198 +                   unsigned int          index,
   1.199 +                   int                   direction)
   1.200 +{
   1.201 +    struct lookahead_entry* buf = NULL;
   1.202 +
   1.203 +    if (direction == PEEK_FORWARD)
   1.204 +    {
   1.205 +        assert(index < ctx->max_sz - 1);
   1.206 +        if(index < ctx->sz)
   1.207 +        {
   1.208 +            index += ctx->read_idx;
   1.209 +            if(index >= ctx->max_sz)
   1.210 +                index -= ctx->max_sz;
   1.211 +            buf = ctx->buf + index;
   1.212 +        }
   1.213 +    }
   1.214 +    else if (direction == PEEK_BACKWARD)
   1.215 +    {
   1.216 +        assert(index == 1);
   1.217 +
   1.218 +        if(ctx->read_idx == 0)
   1.219 +            index = ctx->max_sz - 1;
   1.220 +        else
   1.221 +            index = ctx->read_idx - index;
   1.222 +        buf = ctx->buf + index;
   1.223 +    }
   1.224 +
   1.225 +    return buf;
   1.226 +}
   1.227 +
   1.228 +
   1.229 +unsigned int
   1.230 +vp8_lookahead_depth(struct lookahead_ctx *ctx)
   1.231 +{
   1.232 +    return ctx->sz;
   1.233 +}

mercurial