vpx_encoder.c 11.9 KB
Newer Older
John Koleszar's avatar
John Koleszar committed
1
/*
2
 *  Copyright (c) 2010 The WebM project authors. All Rights Reserved.
John Koleszar's avatar
John Koleszar committed
3
 *
4
 *  Use of this source code is governed by a BSD-style license
5 6
 *  that can be found in the LICENSE file in the root of the source
 *  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.
John Koleszar's avatar
John Koleszar committed
9 10 11
 */


12
/*!\file
John Koleszar's avatar
John Koleszar committed
13 14 15 16 17
 * \brief Provides the high level interface to wrap encoder algorithms.
 *
 */
#include <limits.h>
#include <string.h>
18
#include "vpx_config.h"
19
#include "vpx/internal/vpx_codec_internal.h"
John Koleszar's avatar
John Koleszar committed
20 21 22

#define SAVE_STATUS(ctx,var) (ctx?(ctx->err = var):var)

23 24 25 26
static vpx_codec_alg_priv_t *get_alg_priv(vpx_codec_ctx_t *ctx) {
  return (vpx_codec_alg_priv_t *)ctx->priv;
}

John Koleszar's avatar
John Koleszar committed
27 28
vpx_codec_err_t vpx_codec_enc_init_ver(vpx_codec_ctx_t      *ctx,
                                       vpx_codec_iface_t    *iface,
29
                                       const vpx_codec_enc_cfg_t *cfg,
John Koleszar's avatar
John Koleszar committed
30
                                       vpx_codec_flags_t     flags,
John Koleszar's avatar
John Koleszar committed
31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53
                                       int                   ver) {
  vpx_codec_err_t res;

  if (ver != VPX_ENCODER_ABI_VERSION)
    res = VPX_CODEC_ABI_MISMATCH;
  else if (!ctx || !iface || !cfg)
    res = VPX_CODEC_INVALID_PARAM;
  else if (iface->abi_version != VPX_CODEC_INTERNAL_ABI_VERSION)
    res = VPX_CODEC_ABI_MISMATCH;
  else if (!(iface->caps & VPX_CODEC_CAP_ENCODER))
    res = VPX_CODEC_INCAPABLE;
  else if ((flags & VPX_CODEC_USE_PSNR)
           && !(iface->caps & VPX_CODEC_CAP_PSNR))
    res = VPX_CODEC_INCAPABLE;
  else if ((flags & VPX_CODEC_USE_OUTPUT_PARTITION)
           && !(iface->caps & VPX_CODEC_CAP_OUTPUT_PARTITION))
    res = VPX_CODEC_INCAPABLE;
  else {
    ctx->iface = iface;
    ctx->name = iface->name;
    ctx->priv = NULL;
    ctx->init_flags = flags;
    ctx->config.enc = cfg;
John Koleszar's avatar
John Koleszar committed
54
    res = ctx->iface->init(ctx, NULL);
John Koleszar's avatar
John Koleszar committed
55 56 57 58

    if (res) {
      ctx->err_detail = ctx->priv ? ctx->priv->err_detail : NULL;
      vpx_codec_destroy(ctx);
John Koleszar's avatar
John Koleszar committed
59
    }
John Koleszar's avatar
John Koleszar committed
60 61 62
  }

  return SAVE_STATUS(ctx, res);
John Koleszar's avatar
John Koleszar committed
63 64
}

John Koleszar's avatar
John Koleszar committed
65 66 67 68 69 70 71
vpx_codec_err_t vpx_codec_enc_init_multi_ver(vpx_codec_ctx_t      *ctx,
                                             vpx_codec_iface_t    *iface,
                                             vpx_codec_enc_cfg_t  *cfg,
                                             int                   num_enc,
                                             vpx_codec_flags_t     flags,
                                             vpx_rational_t       *dsf,
                                             int                   ver) {
72
  vpx_codec_err_t res = VPX_CODEC_OK;
John Koleszar's avatar
John Koleszar committed
73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145

  if (ver != VPX_ENCODER_ABI_VERSION)
    res = VPX_CODEC_ABI_MISMATCH;
  else if (!ctx || !iface || !cfg || (num_enc > 16 || num_enc < 1))
    res = VPX_CODEC_INVALID_PARAM;
  else if (iface->abi_version != VPX_CODEC_INTERNAL_ABI_VERSION)
    res = VPX_CODEC_ABI_MISMATCH;
  else if (!(iface->caps & VPX_CODEC_CAP_ENCODER))
    res = VPX_CODEC_INCAPABLE;
  else if ((flags & VPX_CODEC_USE_PSNR)
           && !(iface->caps & VPX_CODEC_CAP_PSNR))
    res = VPX_CODEC_INCAPABLE;
  else if ((flags & VPX_CODEC_USE_OUTPUT_PARTITION)
           && !(iface->caps & VPX_CODEC_CAP_OUTPUT_PARTITION))
    res = VPX_CODEC_INCAPABLE;
  else {
    int i;
    void *mem_loc = NULL;

    if (!(res = iface->enc.mr_get_mem_loc(cfg, &mem_loc))) {
      for (i = 0; i < num_enc; i++) {
        vpx_codec_priv_enc_mr_cfg_t mr_cfg;

        /* Validate down-sampling factor. */
        if (dsf->num < 1 || dsf->num > 4096 || dsf->den < 1 ||
            dsf->den > dsf->num) {
          res = VPX_CODEC_INVALID_PARAM;
          break;
        }

        mr_cfg.mr_low_res_mode_info = mem_loc;
        mr_cfg.mr_total_resolutions = num_enc;
        mr_cfg.mr_encoder_id = num_enc - 1 - i;
        mr_cfg.mr_down_sampling_factor.num = dsf->num;
        mr_cfg.mr_down_sampling_factor.den = dsf->den;

        /* Force Key-frame synchronization. Namely, encoder at higher
         * resolution always use the same frame_type chosen by the
         * lowest-resolution encoder.
         */
        if (mr_cfg.mr_encoder_id)
          cfg->kf_mode = VPX_KF_DISABLED;

        ctx->iface = iface;
        ctx->name = iface->name;
        ctx->priv = NULL;
        ctx->init_flags = flags;
        ctx->config.enc = cfg;
        res = ctx->iface->init(ctx, &mr_cfg);

        if (res) {
          const char *error_detail =
            ctx->priv ? ctx->priv->err_detail : NULL;
          /* Destroy current ctx */
          ctx->err_detail = error_detail;
          vpx_codec_destroy(ctx);

          /* Destroy already allocated high-level ctx */
          while (i) {
            ctx--;
            ctx->err_detail = error_detail;
            vpx_codec_destroy(ctx);
            i--;
          }
        }

        if (res)
          break;

        ctx++;
        cfg++;
        dsf++;
      }
146
      ctx--;
John Koleszar's avatar
John Koleszar committed
147 148 149 150 151
    }
  }

  return SAVE_STATUS(ctx, res);
}
John Koleszar's avatar
John Koleszar committed
152 153 154


vpx_codec_err_t  vpx_codec_enc_config_default(vpx_codec_iface_t    *iface,
John Koleszar's avatar
John Koleszar committed
155 156 157 158
                                              vpx_codec_enc_cfg_t  *cfg,
                                              unsigned int          usage) {
  vpx_codec_err_t res;
  vpx_codec_enc_cfg_map_t *map;
159
  int i;
John Koleszar's avatar
John Koleszar committed
160 161 162 163 164 165 166 167

  if (!iface || !cfg || usage > INT_MAX)
    res = VPX_CODEC_INVALID_PARAM;
  else if (!(iface->caps & VPX_CODEC_CAP_ENCODER))
    res = VPX_CODEC_INCAPABLE;
  else {
    res = VPX_CODEC_INVALID_PARAM;

168 169
    for (i = 0; i < iface->enc.cfg_map_count; ++i) {
      map = iface->enc.cfg_maps + i;
John Koleszar's avatar
John Koleszar committed
170 171 172 173 174 175
      if (map->usage == (int)usage) {
        *cfg = map->cfg;
        cfg->g_usage = usage;
        res = VPX_CODEC_OK;
        break;
      }
John Koleszar's avatar
John Koleszar committed
176
    }
John Koleszar's avatar
John Koleszar committed
177
  }
John Koleszar's avatar
John Koleszar committed
178

John Koleszar's avatar
John Koleszar committed
179
  return res;
John Koleszar's avatar
John Koleszar committed
180 181 182 183 184 185 186 187 188
}


#if ARCH_X86 || ARCH_X86_64
/* On X86, disable the x87 unit's internal 80 bit precision for better
 * consistency with the SSE unit's 64 bit precision.
 */
#include "vpx_ports/x86.h"
#define FLOATING_POINT_INIT() do {\
John Koleszar's avatar
John Koleszar committed
189
    unsigned short x87_orig_mode = x87_set_double_precision();
John Koleszar's avatar
John Koleszar committed
190
#define FLOATING_POINT_RESTORE() \
John Koleszar's avatar
John Koleszar committed
191
  x87_set_control_word(x87_orig_mode); }while(0)
John Koleszar's avatar
John Koleszar committed
192 193 194 195 196 197 198 199 200 201 202 203 204


#else
static void FLOATING_POINT_INIT() {}
static void FLOATING_POINT_RESTORE() {}
#endif


vpx_codec_err_t  vpx_codec_encode(vpx_codec_ctx_t            *ctx,
                                  const vpx_image_t          *img,
                                  vpx_codec_pts_t             pts,
                                  unsigned long               duration,
                                  vpx_enc_frame_flags_t       flags,
John Koleszar's avatar
John Koleszar committed
205
                                  unsigned long               deadline) {
206
  vpx_codec_err_t res = VPX_CODEC_OK;
John Koleszar's avatar
John Koleszar committed
207 208 209 210 211 212 213 214

  if (!ctx || (img && !duration))
    res = VPX_CODEC_INVALID_PARAM;
  else if (!ctx->iface || !ctx->priv)
    res = VPX_CODEC_ERROR;
  else if (!(ctx->iface->caps & VPX_CODEC_CAP_ENCODER))
    res = VPX_CODEC_INCAPABLE;
  else {
215 216
    unsigned int num_enc = ctx->priv->enc.total_encoders;

John Koleszar's avatar
John Koleszar committed
217 218 219 220
    /* Execute in a normalized floating point environment, if the platform
     * requires it.
     */
    FLOATING_POINT_INIT();
John Koleszar's avatar
John Koleszar committed
221 222

    if (num_enc == 1)
223
      res = ctx->iface->enc.encode(get_alg_priv(ctx), img, pts,
John Koleszar's avatar
John Koleszar committed
224 225 226 227 228 229 230 231 232 233 234 235 236
                                   duration, flags, deadline);
    else {
      /* Multi-resolution encoding:
       * Encode multi-levels in reverse order. For example,
       * if mr_total_resolutions = 3, first encode level 2,
       * then encode level 1, and finally encode level 0.
       */
      int i;

      ctx += num_enc - 1;
      if (img) img += num_enc - 1;

      for (i = num_enc - 1; i >= 0; i--) {
237
        if ((res = ctx->iface->enc.encode(get_alg_priv(ctx), img, pts,
John Koleszar's avatar
John Koleszar committed
238 239 240 241 242 243 244 245 246
                                          duration, flags, deadline)))
          break;

        ctx--;
        if (img) img--;
      }
      ctx++;
    }

John Koleszar's avatar
John Koleszar committed
247 248 249 250
    FLOATING_POINT_RESTORE();
  }

  return SAVE_STATUS(ctx, res);
John Koleszar's avatar
John Koleszar committed
251 252 253
}


254 255
const vpx_codec_cx_pkt_t *vpx_codec_get_cx_data(vpx_codec_ctx_t *ctx,
                                                vpx_codec_iter_t *iter) {
John Koleszar's avatar
John Koleszar committed
256 257 258 259 260 261 262 263 264 265
  const vpx_codec_cx_pkt_t *pkt = NULL;

  if (ctx) {
    if (!iter)
      ctx->err = VPX_CODEC_INVALID_PARAM;
    else if (!ctx->iface || !ctx->priv)
      ctx->err = VPX_CODEC_ERROR;
    else if (!(ctx->iface->caps & VPX_CODEC_CAP_ENCODER))
      ctx->err = VPX_CODEC_INCAPABLE;
    else
266
      pkt = ctx->iface->enc.get_cx_data(get_alg_priv(ctx), iter);
John Koleszar's avatar
John Koleszar committed
267 268 269
  }

  if (pkt && pkt->kind == VPX_CODEC_CX_FRAME_PKT) {
270 271 272 273 274 275 276 277 278 279 280 281 282 283
    // If the application has specified a destination area for the
    // compressed data, and the codec has not placed the data there,
    // and it fits, copy it.
    vpx_codec_priv_t *const priv = ctx->priv;
    char *const dst_buf = (char *)priv->enc.cx_data_dst_buf.buf;

    if (dst_buf &&
        pkt->data.raw.buf != dst_buf &&
        pkt->data.raw.sz + priv->enc.cx_data_pad_before +
            priv->enc.cx_data_pad_after <= priv->enc.cx_data_dst_buf.sz) {
      vpx_codec_cx_pkt_t *modified_pkt = &priv->enc.cx_data_pkt;

      memcpy(dst_buf + priv->enc.cx_data_pad_before, pkt->data.raw.buf,
             pkt->data.raw.sz);
John Koleszar's avatar
John Koleszar committed
284 285
      *modified_pkt = *pkt;
      modified_pkt->data.raw.buf = dst_buf;
286 287
      modified_pkt->data.raw.sz += priv->enc.cx_data_pad_before +
                                       priv->enc.cx_data_pad_after;
John Koleszar's avatar
John Koleszar committed
288
      pkt = modified_pkt;
John Koleszar's avatar
John Koleszar committed
289 290
    }

John Koleszar's avatar
John Koleszar committed
291
    if (dst_buf == pkt->data.raw.buf) {
292 293
      priv->enc.cx_data_dst_buf.buf = dst_buf + pkt->data.raw.sz;
      priv->enc.cx_data_dst_buf.sz -= pkt->data.raw.sz;
John Koleszar's avatar
John Koleszar committed
294
    }
John Koleszar's avatar
John Koleszar committed
295
  }
John Koleszar's avatar
John Koleszar committed
296

John Koleszar's avatar
John Koleszar committed
297
  return pkt;
John Koleszar's avatar
John Koleszar committed
298 299 300 301
}


vpx_codec_err_t vpx_codec_set_cx_data_buf(vpx_codec_ctx_t       *ctx,
John Koleszar's avatar
John Koleszar committed
302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319
                                          const vpx_fixed_buf_t *buf,
                                          unsigned int           pad_before,
                                          unsigned int           pad_after) {
  if (!ctx || !ctx->priv)
    return VPX_CODEC_INVALID_PARAM;

  if (buf) {
    ctx->priv->enc.cx_data_dst_buf = *buf;
    ctx->priv->enc.cx_data_pad_before = pad_before;
    ctx->priv->enc.cx_data_pad_after = pad_after;
  } else {
    ctx->priv->enc.cx_data_dst_buf.buf = NULL;
    ctx->priv->enc.cx_data_dst_buf.sz = 0;
    ctx->priv->enc.cx_data_pad_before = 0;
    ctx->priv->enc.cx_data_pad_after = 0;
  }

  return VPX_CODEC_OK;
John Koleszar's avatar
John Koleszar committed
320 321 322
}


John Koleszar's avatar
John Koleszar committed
323 324 325 326 327 328 329 330 331 332 333
const vpx_image_t *vpx_codec_get_preview_frame(vpx_codec_ctx_t   *ctx) {
  vpx_image_t *img = NULL;

  if (ctx) {
    if (!ctx->iface || !ctx->priv)
      ctx->err = VPX_CODEC_ERROR;
    else if (!(ctx->iface->caps & VPX_CODEC_CAP_ENCODER))
      ctx->err = VPX_CODEC_INCAPABLE;
    else if (!ctx->iface->enc.get_preview)
      ctx->err = VPX_CODEC_INCAPABLE;
    else
334
      img = ctx->iface->enc.get_preview(get_alg_priv(ctx));
John Koleszar's avatar
John Koleszar committed
335
  }
John Koleszar's avatar
John Koleszar committed
336

John Koleszar's avatar
John Koleszar committed
337
  return img;
John Koleszar's avatar
John Koleszar committed
338 339 340
}


John Koleszar's avatar
John Koleszar committed
341 342
vpx_fixed_buf_t *vpx_codec_get_global_headers(vpx_codec_ctx_t   *ctx) {
  vpx_fixed_buf_t *buf = NULL;
John Koleszar's avatar
John Koleszar committed
343

John Koleszar's avatar
John Koleszar committed
344 345 346 347 348 349 350 351
  if (ctx) {
    if (!ctx->iface || !ctx->priv)
      ctx->err = VPX_CODEC_ERROR;
    else if (!(ctx->iface->caps & VPX_CODEC_CAP_ENCODER))
      ctx->err = VPX_CODEC_INCAPABLE;
    else if (!ctx->iface->enc.get_glob_hdrs)
      ctx->err = VPX_CODEC_INCAPABLE;
    else
352
      buf = ctx->iface->enc.get_glob_hdrs(get_alg_priv(ctx));
John Koleszar's avatar
John Koleszar committed
353 354 355
  }

  return buf;
John Koleszar's avatar
John Koleszar committed
356 357 358 359
}


vpx_codec_err_t  vpx_codec_enc_config_set(vpx_codec_ctx_t            *ctx,
John Koleszar's avatar
John Koleszar committed
360 361
                                          const vpx_codec_enc_cfg_t  *cfg) {
  vpx_codec_err_t res;
John Koleszar's avatar
John Koleszar committed
362

John Koleszar's avatar
John Koleszar committed
363 364 365 366 367
  if (!ctx || !ctx->iface || !ctx->priv || !cfg)
    res = VPX_CODEC_INVALID_PARAM;
  else if (!(ctx->iface->caps & VPX_CODEC_CAP_ENCODER))
    res = VPX_CODEC_INCAPABLE;
  else
368
    res = ctx->iface->enc.cfg_set(get_alg_priv(ctx), cfg);
John Koleszar's avatar
John Koleszar committed
369

John Koleszar's avatar
John Koleszar committed
370
  return SAVE_STATUS(ctx, res);
John Koleszar's avatar
John Koleszar committed
371 372 373 374
}


int vpx_codec_pkt_list_add(struct vpx_codec_pkt_list *list,
John Koleszar's avatar
John Koleszar committed
375 376 377 378 379
                           const struct vpx_codec_cx_pkt *pkt) {
  if (list->cnt < list->max) {
    list->pkts[list->cnt++] = *pkt;
    return 0;
  }
John Koleszar's avatar
John Koleszar committed
380

John Koleszar's avatar
John Koleszar committed
381
  return 1;
John Koleszar's avatar
John Koleszar committed
382 383 384 385
}


const vpx_codec_cx_pkt_t *vpx_codec_pkt_list_get(struct vpx_codec_pkt_list *list,
John Koleszar's avatar
John Koleszar committed
386 387
                                                 vpx_codec_iter_t           *iter) {
  const vpx_codec_cx_pkt_t *pkt;
John Koleszar's avatar
John Koleszar committed
388

John Koleszar's avatar
John Koleszar committed
389 390 391
  if (!(*iter)) {
    *iter = list->pkts;
  }
John Koleszar's avatar
John Koleszar committed
392

393
  pkt = (const vpx_codec_cx_pkt_t *)*iter;
John Koleszar's avatar
John Koleszar committed
394

John Koleszar's avatar
John Koleszar committed
395 396 397 398
  if ((size_t)(pkt - list->pkts) < list->cnt)
    *iter = pkt + 1;
  else
    pkt = NULL;
John Koleszar's avatar
John Koleszar committed
399

John Koleszar's avatar
John Koleszar committed
400
  return pkt;
John Koleszar's avatar
John Koleszar committed
401
}