John Koleszar | 0ea50ce | 2010-05-18 11:58:33 -0400 | [diff] [blame] | 1 | /* |
John Koleszar | c2140b8 | 2010-09-09 08:16:39 -0400 | [diff] [blame] | 2 | * Copyright (c) 2010 The WebM project authors. All Rights Reserved. |
John Koleszar | 0ea50ce | 2010-05-18 11:58:33 -0400 | [diff] [blame] | 3 | * |
John Koleszar | 94c52e4 | 2010-06-18 12:39:21 -0400 | [diff] [blame] | 4 | * Use of this source code is governed by a BSD-style license |
John Koleszar | 09202d8 | 2010-06-04 16:19:40 -0400 | [diff] [blame] | 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 |
John Koleszar | 94c52e4 | 2010-06-18 12:39:21 -0400 | [diff] [blame] | 7 | * in the file PATENTS. All contributing project authors may |
John Koleszar | 09202d8 | 2010-06-04 16:19:40 -0400 | [diff] [blame] | 8 | * be found in the AUTHORS file in the root of the source tree. |
John Koleszar | 0ea50ce | 2010-05-18 11:58:33 -0400 | [diff] [blame] | 9 | */ |
| 10 | |
| 11 | |
| 12 | /* This is a simple program showing how to initialize the decoder in XMA mode */ |
| 13 | #include <stdio.h> |
| 14 | #include <stdlib.h> |
| 15 | #include <stdarg.h> |
| 16 | #include <string.h> |
| 17 | #define VPX_CODEC_DISABLE_COMPAT 1 |
| 18 | #include "vpx_config.h" |
John Koleszar | b749234 | 2010-05-24 11:39:59 -0400 | [diff] [blame] | 19 | #include "vpx/vpx_decoder.h" |
| 20 | #include "vpx/vpx_integer.h" |
Ronald S. Bultje | 4b2c2b9 | 2012-11-01 11:09:58 -0700 | [diff] [blame] | 21 | #if CONFIG_VP9_DECODER |
John Koleszar | b749234 | 2010-05-24 11:39:59 -0400 | [diff] [blame] | 22 | #include "vpx/vp8dx.h" |
John Koleszar | 0ea50ce | 2010-05-18 11:58:33 -0400 | [diff] [blame] | 23 | #endif |
| 24 | |
| 25 | static char *exec_name; |
| 26 | static int verbose = 0; |
| 27 | |
John Koleszar | c6b9039 | 2012-07-13 15:21:29 -0700 | [diff] [blame] | 28 | static const struct { |
| 29 | const char *name; |
| 30 | const vpx_codec_iface_t *iface; |
| 31 | } ifaces[] = { |
Ronald S. Bultje | 4b2c2b9 | 2012-11-01 11:09:58 -0700 | [diff] [blame] | 32 | #if CONFIG_VP9_DECODER |
| 33 | {"vp9", &vpx_codec_vp8_dx_algo}, |
John Koleszar | 0ea50ce | 2010-05-18 11:58:33 -0400 | [diff] [blame] | 34 | #endif |
| 35 | }; |
| 36 | |
John Koleszar | c6b9039 | 2012-07-13 15:21:29 -0700 | [diff] [blame] | 37 | static void usage_exit(void) { |
| 38 | int i; |
John Koleszar | 0ea50ce | 2010-05-18 11:58:33 -0400 | [diff] [blame] | 39 | |
John Koleszar | c6b9039 | 2012-07-13 15:21:29 -0700 | [diff] [blame] | 40 | printf("Usage: %s <options>\n\n" |
| 41 | "Options:\n" |
| 42 | "\t--codec <name>\tCodec to use (default=%s)\n" |
| 43 | "\t-h <height>\tHeight of the simulated video frame, in pixels\n" |
| 44 | "\t-w <width> \tWidth of the simulated video frame, in pixels\n" |
| 45 | "\t-v \tVerbose mode (show individual segment sizes)\n" |
| 46 | "\t--help \tShow this message\n" |
| 47 | "\n" |
| 48 | "Included decoders:\n" |
| 49 | "\n", |
| 50 | exec_name, |
| 51 | ifaces[0].name); |
John Koleszar | 0ea50ce | 2010-05-18 11:58:33 -0400 | [diff] [blame] | 52 | |
John Koleszar | c6b9039 | 2012-07-13 15:21:29 -0700 | [diff] [blame] | 53 | for (i = 0; i < sizeof(ifaces) / sizeof(ifaces[0]); i++) |
| 54 | printf(" %-6s - %s\n", |
| 55 | ifaces[i].name, |
| 56 | vpx_codec_iface_name(ifaces[i].iface)); |
John Koleszar | 0ea50ce | 2010-05-18 11:58:33 -0400 | [diff] [blame] | 57 | |
John Koleszar | c6b9039 | 2012-07-13 15:21:29 -0700 | [diff] [blame] | 58 | exit(EXIT_FAILURE); |
John Koleszar | 0ea50ce | 2010-05-18 11:58:33 -0400 | [diff] [blame] | 59 | } |
| 60 | |
John Koleszar | c6b9039 | 2012-07-13 15:21:29 -0700 | [diff] [blame] | 61 | static void usage_error(const char *fmt, ...) { |
| 62 | va_list ap; |
| 63 | va_start(ap, fmt); |
| 64 | vprintf(fmt, ap); |
| 65 | printf("\n"); |
| 66 | usage_exit(); |
John Koleszar | 0ea50ce | 2010-05-18 11:58:33 -0400 | [diff] [blame] | 67 | } |
| 68 | |
John Koleszar | c6b9039 | 2012-07-13 15:21:29 -0700 | [diff] [blame] | 69 | void my_mem_dtor(vpx_codec_mmap_t *mmap) { |
| 70 | if (verbose) |
| 71 | printf("freeing segment %d\n", mmap->id); |
John Koleszar | 0ea50ce | 2010-05-18 11:58:33 -0400 | [diff] [blame] | 72 | |
John Koleszar | c6b9039 | 2012-07-13 15:21:29 -0700 | [diff] [blame] | 73 | free(mmap->priv); |
John Koleszar | 0ea50ce | 2010-05-18 11:58:33 -0400 | [diff] [blame] | 74 | } |
| 75 | |
John Koleszar | c6b9039 | 2012-07-13 15:21:29 -0700 | [diff] [blame] | 76 | int main(int argc, char **argv) { |
| 77 | vpx_codec_ctx_t decoder; |
| 78 | vpx_codec_iface_t *iface = ifaces[0].iface; |
| 79 | vpx_codec_iter_t iter; |
| 80 | vpx_codec_dec_cfg_t cfg; |
| 81 | vpx_codec_err_t res = VPX_CODEC_OK; |
| 82 | unsigned int alloc_sz = 0; |
| 83 | unsigned int w = 352; |
| 84 | unsigned int h = 288; |
| 85 | int i; |
John Koleszar | 0ea50ce | 2010-05-18 11:58:33 -0400 | [diff] [blame] | 86 | |
John Koleszar | c6b9039 | 2012-07-13 15:21:29 -0700 | [diff] [blame] | 87 | exec_name = argv[0]; |
John Koleszar | 0ea50ce | 2010-05-18 11:58:33 -0400 | [diff] [blame] | 88 | |
John Koleszar | c6b9039 | 2012-07-13 15:21:29 -0700 | [diff] [blame] | 89 | for (i = 1; i < argc; i++) { |
| 90 | if (!strcmp(argv[i], "--codec")) { |
| 91 | if (i + 1 < argc) { |
| 92 | int j, k = -1; |
John Koleszar | 0ea50ce | 2010-05-18 11:58:33 -0400 | [diff] [blame] | 93 | |
John Koleszar | c6b9039 | 2012-07-13 15:21:29 -0700 | [diff] [blame] | 94 | i++; |
John Koleszar | 0ea50ce | 2010-05-18 11:58:33 -0400 | [diff] [blame] | 95 | |
John Koleszar | c6b9039 | 2012-07-13 15:21:29 -0700 | [diff] [blame] | 96 | for (j = 0; j < sizeof(ifaces) / sizeof(ifaces[0]); j++) |
| 97 | if (!strcmp(ifaces[j].name, argv[i])) |
| 98 | k = j; |
John Koleszar | 0ea50ce | 2010-05-18 11:58:33 -0400 | [diff] [blame] | 99 | |
John Koleszar | c6b9039 | 2012-07-13 15:21:29 -0700 | [diff] [blame] | 100 | if (k >= 0) |
| 101 | iface = ifaces[k].iface; |
John Koleszar | 0ea50ce | 2010-05-18 11:58:33 -0400 | [diff] [blame] | 102 | else |
John Koleszar | c6b9039 | 2012-07-13 15:21:29 -0700 | [diff] [blame] | 103 | usage_error("Error: Unrecognized argument (%s) to --codec\n", |
| 104 | argv[i]); |
| 105 | } else |
| 106 | usage_error("Error: Option --codec requires argument.\n"); |
| 107 | } else if (!strcmp(argv[i], "-v")) |
| 108 | verbose = 1; |
| 109 | else if (!strcmp(argv[i], "-h")) |
| 110 | if (i + 1 < argc) { |
| 111 | h = atoi(argv[++i]); |
| 112 | } else |
| 113 | usage_error("Error: Option -h requires argument.\n"); |
| 114 | else if (!strcmp(argv[i], "-w")) |
| 115 | if (i + 1 < argc) { |
| 116 | w = atoi(argv[++i]); |
| 117 | } else |
| 118 | usage_error("Error: Option -w requires argument.\n"); |
| 119 | else if (!strcmp(argv[i], "--help")) |
| 120 | usage_exit(); |
| 121 | else |
| 122 | usage_error("Error: Unrecognized option %s\n\n", argv[i]); |
| 123 | } |
John Koleszar | 0ea50ce | 2010-05-18 11:58:33 -0400 | [diff] [blame] | 124 | |
John Koleszar | c6b9039 | 2012-07-13 15:21:29 -0700 | [diff] [blame] | 125 | if (argc == 1) |
| 126 | printf("Using built-in defaults. For options, rerun with --help\n\n"); |
John Koleszar | 0ea50ce | 2010-05-18 11:58:33 -0400 | [diff] [blame] | 127 | |
John Koleszar | c6b9039 | 2012-07-13 15:21:29 -0700 | [diff] [blame] | 128 | /* XMA mode is not supported on all decoders! */ |
| 129 | if (!(vpx_codec_get_caps(iface) & VPX_CODEC_CAP_XMA)) { |
| 130 | printf("%s does not support XMA mode!\n", vpx_codec_iface_name(iface)); |
| 131 | return EXIT_FAILURE; |
| 132 | } |
| 133 | |
| 134 | /* The codec knows how much memory to allocate based on the size of the |
| 135 | * encoded frames. This data can be parsed from the bitstream with |
| 136 | * vpx_codec_peek_stream_info() if a bitstream is available. Otherwise, |
| 137 | * a fixed size can be used that will be the upper limit on the frame |
| 138 | * size the decoder can decode. |
| 139 | */ |
| 140 | cfg.w = w; |
| 141 | cfg.h = h; |
| 142 | |
| 143 | /* Initialize the decoder in XMA mode. */ |
| 144 | if (vpx_codec_dec_init(&decoder, iface, &cfg, VPX_CODEC_USE_XMA)) { |
| 145 | printf("Failed to initialize decoder in XMA mode: %s\n", vpx_codec_error(&decoder)); |
| 146 | return EXIT_FAILURE; |
| 147 | } |
| 148 | |
| 149 | /* Iterate through the list of memory maps, allocating them with the |
| 150 | * requested alignment. |
| 151 | */ |
| 152 | iter = NULL; |
| 153 | |
| 154 | do { |
| 155 | vpx_codec_mmap_t mmap; |
| 156 | unsigned int align; |
| 157 | |
| 158 | res = vpx_codec_get_mem_map(&decoder, &mmap, &iter); |
| 159 | align = mmap.align ? mmap.align - 1 : 0; |
| 160 | |
| 161 | if (!res) { |
| 162 | if (verbose) |
| 163 | printf("Allocating segment %u, size %lu, align %u %s\n", |
| 164 | mmap.id, mmap.sz, mmap.align, |
| 165 | mmap.flags & VPX_CODEC_MEM_ZERO ? "(ZEROED)" : ""); |
| 166 | |
| 167 | if (mmap.flags & VPX_CODEC_MEM_ZERO) |
| 168 | mmap.priv = calloc(1, mmap.sz + align); |
| 169 | else |
| 170 | mmap.priv = malloc(mmap.sz + align); |
| 171 | |
| 172 | mmap.base = (void *)((((uintptr_t)mmap.priv) + align) & ~(uintptr_t)align); |
| 173 | mmap.dtor = my_mem_dtor; |
| 174 | alloc_sz += mmap.sz + align; |
| 175 | |
| 176 | if (vpx_codec_set_mem_map(&decoder, &mmap, 1)) { |
| 177 | printf("Failed to set mmap: %s\n", vpx_codec_error(&decoder)); |
John Koleszar | 0ea50ce | 2010-05-18 11:58:33 -0400 | [diff] [blame] | 178 | return EXIT_FAILURE; |
John Koleszar | c6b9039 | 2012-07-13 15:21:29 -0700 | [diff] [blame] | 179 | } |
| 180 | } else if (res != VPX_CODEC_LIST_END) { |
| 181 | printf("Failed to get mmap: %s\n", vpx_codec_error(&decoder)); |
| 182 | return EXIT_FAILURE; |
John Koleszar | 0ea50ce | 2010-05-18 11:58:33 -0400 | [diff] [blame] | 183 | } |
John Koleszar | c6b9039 | 2012-07-13 15:21:29 -0700 | [diff] [blame] | 184 | } while (res != VPX_CODEC_LIST_END); |
John Koleszar | 0ea50ce | 2010-05-18 11:58:33 -0400 | [diff] [blame] | 185 | |
John Koleszar | c6b9039 | 2012-07-13 15:21:29 -0700 | [diff] [blame] | 186 | printf("%s\n %d bytes external memory required for %dx%d.\n", |
| 187 | decoder.name, alloc_sz, cfg.w, cfg.h); |
| 188 | vpx_codec_destroy(&decoder); |
| 189 | return EXIT_SUCCESS; |
John Koleszar | 0ea50ce | 2010-05-18 11:58:33 -0400 | [diff] [blame] | 190 | |
| 191 | } |