example_xma.c 5.48 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 13 14 15 16 17 18
 */


/* This is a simple program showing how to initialize the decoder in XMA mode */
#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>
#include <string.h>
#define VPX_CODEC_DISABLE_COMPAT 1
#include "vpx_config.h"
19 20
#include "vpx/vpx_decoder.h"
#include "vpx/vpx_integer.h"
21
#if CONFIG_VP9_DECODER
22
#include "vpx/vp8dx.h"
John Koleszar's avatar
John Koleszar committed
23 24 25 26 27
#endif

static char *exec_name;
static int   verbose = 0;

John Koleszar's avatar
John Koleszar committed
28 29 30 31
static const struct {
  const char *name;
  const vpx_codec_iface_t *iface;
} ifaces[] = {
32 33
#if CONFIG_VP9_DECODER
  {"vp9",  &vpx_codec_vp8_dx_algo},
John Koleszar's avatar
John Koleszar committed
34 35 36
#endif
};

John Koleszar's avatar
John Koleszar committed
37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58
static void usage_exit(void) {
  int i;

  printf("Usage: %s <options>\n\n"
         "Options:\n"
         "\t--codec <name>\tCodec to use (default=%s)\n"
         "\t-h <height>\tHeight of the simulated video frame, in pixels\n"
         "\t-w <width> \tWidth of the simulated video frame, in pixels\n"
         "\t-v         \tVerbose mode (show individual segment sizes)\n"
         "\t--help     \tShow this message\n"
         "\n"
         "Included decoders:\n"
         "\n",
         exec_name,
         ifaces[0].name);

  for (i = 0; i < sizeof(ifaces) / sizeof(ifaces[0]); i++)
    printf("    %-6s - %s\n",
           ifaces[i].name,
           vpx_codec_iface_name(ifaces[i].iface));

  exit(EXIT_FAILURE);
John Koleszar's avatar
John Koleszar committed
59 60
}

John Koleszar's avatar
John Koleszar committed
61 62 63 64 65 66
static void usage_error(const char *fmt, ...) {
  va_list ap;
  va_start(ap, fmt);
  vprintf(fmt, ap);
  printf("\n");
  usage_exit();
John Koleszar's avatar
John Koleszar committed
67 68
}

John Koleszar's avatar
John Koleszar committed
69 70 71
void my_mem_dtor(vpx_codec_mmap_t *mmap) {
  if (verbose)
    printf("freeing segment %d\n", mmap->id);
John Koleszar's avatar
John Koleszar committed
72

John Koleszar's avatar
John Koleszar committed
73
  free(mmap->priv);
John Koleszar's avatar
John Koleszar committed
74 75
}

John Koleszar's avatar
John Koleszar committed
76 77 78 79 80 81 82 83 84 85
int main(int argc, char **argv) {
  vpx_codec_ctx_t           decoder;
  vpx_codec_iface_t        *iface = ifaces[0].iface;
  vpx_codec_iter_t          iter;
  vpx_codec_dec_cfg_t       cfg;
  vpx_codec_err_t           res = VPX_CODEC_OK;
  unsigned int            alloc_sz = 0;
  unsigned int            w = 352;
  unsigned int            h = 288;
  int                     i;
John Koleszar's avatar
John Koleszar committed
86

John Koleszar's avatar
John Koleszar committed
87
  exec_name = argv[0];
John Koleszar's avatar
John Koleszar committed
88

John Koleszar's avatar
John Koleszar committed
89 90 91 92
  for (i = 1; i < argc; i++) {
    if (!strcmp(argv[i], "--codec")) {
      if (i + 1 < argc) {
        int j, k = -1;
John Koleszar's avatar
John Koleszar committed
93

John Koleszar's avatar
John Koleszar committed
94 95 96 97 98
        i++;

        for (j = 0; j < sizeof(ifaces) / sizeof(ifaces[0]); j++)
          if (!strcmp(ifaces[j].name, argv[i]))
            k = j;
John Koleszar's avatar
John Koleszar committed
99

John Koleszar's avatar
John Koleszar committed
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 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182
        if (k >= 0)
          iface = ifaces[k].iface;
        else
          usage_error("Error: Unrecognized argument (%s) to --codec\n",
                      argv[i]);
      } else
        usage_error("Error: Option --codec requires argument.\n");
    } else if (!strcmp(argv[i], "-v"))
      verbose = 1;
    else if (!strcmp(argv[i], "-h"))
      if (i + 1 < argc) {
        h = atoi(argv[++i]);
      } else
        usage_error("Error: Option -h requires argument.\n");
    else if (!strcmp(argv[i], "-w"))
      if (i + 1 < argc) {
        w = atoi(argv[++i]);
      } else
        usage_error("Error: Option -w requires argument.\n");
    else if (!strcmp(argv[i], "--help"))
      usage_exit();
    else
      usage_error("Error: Unrecognized option %s\n\n", argv[i]);
  }

  if (argc == 1)
    printf("Using built-in defaults. For options, rerun with --help\n\n");

  /* XMA mode is not supported on all decoders! */
  if (!(vpx_codec_get_caps(iface) & VPX_CODEC_CAP_XMA)) {
    printf("%s does not support XMA mode!\n", vpx_codec_iface_name(iface));
    return EXIT_FAILURE;
  }

  /* The codec knows how much memory to allocate based on the size of the
   * encoded frames. This data can be parsed from the bitstream with
   * vpx_codec_peek_stream_info() if a bitstream is available. Otherwise,
   * a fixed size can be used that will be the upper limit on the frame
   * size the decoder can decode.
   */
  cfg.w = w;
  cfg.h = h;

  /* Initialize the decoder in XMA mode. */
  if (vpx_codec_dec_init(&decoder, iface, &cfg, VPX_CODEC_USE_XMA)) {
    printf("Failed to initialize decoder in XMA mode: %s\n", vpx_codec_error(&decoder));
    return EXIT_FAILURE;
  }

  /* Iterate through the list of memory maps, allocating them with the
   * requested alignment.
   */
  iter = NULL;

  do {
    vpx_codec_mmap_t  mmap;
    unsigned int    align;

    res = vpx_codec_get_mem_map(&decoder, &mmap, &iter);
    align = mmap.align ? mmap.align - 1 : 0;

    if (!res) {
      if (verbose)
        printf("Allocating segment %u, size %lu, align %u %s\n",
               mmap.id, mmap.sz, mmap.align,
               mmap.flags & VPX_CODEC_MEM_ZERO ? "(ZEROED)" : "");

      if (mmap.flags & VPX_CODEC_MEM_ZERO)
        mmap.priv = calloc(1, mmap.sz + align);
      else
        mmap.priv = malloc(mmap.sz + align);

      mmap.base = (void *)((((uintptr_t)mmap.priv) + align) & ~(uintptr_t)align);
      mmap.dtor = my_mem_dtor;
      alloc_sz += mmap.sz + align;

      if (vpx_codec_set_mem_map(&decoder, &mmap, 1)) {
        printf("Failed to set mmap: %s\n", vpx_codec_error(&decoder));
        return EXIT_FAILURE;
      }
    } else if (res != VPX_CODEC_LIST_END) {
      printf("Failed to get mmap: %s\n", vpx_codec_error(&decoder));
      return EXIT_FAILURE;
John Koleszar's avatar
John Koleszar committed
183
    }
John Koleszar's avatar
John Koleszar committed
184
  } while (res != VPX_CODEC_LIST_END);
John Koleszar's avatar
John Koleszar committed
185

John Koleszar's avatar
John Koleszar committed
186 187 188 189
  printf("%s\n    %d bytes external memory required for %dx%d.\n",
         decoder.name, alloc_sz, cfg.w, cfg.h);
  vpx_codec_destroy(&decoder);
  return EXIT_SUCCESS;
John Koleszar's avatar
John Koleszar committed
190 191

}