x86: tell gnu ld that we don't require an executable stack
[libvpx.git] / example_xma.c
blob753ca3c40f002b39dffbc8c9b8ca0347e050a27d
1 /*
2 * Copyright (c) 2010 The VP8 project authors. All Rights Reserved.
4 * Use of this source code is governed by a BSD-style license and patent
5 * grant that can be found in the LICENSE file in the root of the source
6 * tree. All contributing project authors may be found in the AUTHORS
7 * file in the root of the source tree.
8 */
11 /* This is a simple program showing how to initialize the decoder in XMA mode */
12 #include <stdio.h>
13 #include <stdlib.h>
14 #include <stdarg.h>
15 #include <string.h>
16 #define VPX_CODEC_DISABLE_COMPAT 1
17 #include "vpx_config.h"
18 #include "vpx/vpx_decoder.h"
19 #include "vpx/vpx_integer.h"
20 #if CONFIG_VP8_DECODER
21 #include "vpx/vp8dx.h"
22 #endif
24 static char *exec_name;
25 static int verbose = 0;
27 static const struct
29 const char *name;
30 const vpx_codec_iface_t *iface;
31 } ifaces[] =
33 #if CONFIG_VP8_DECODER
34 {"vp8", &vpx_codec_vp8_dx_algo},
35 #endif
38 static void usage_exit(void)
40 int i;
42 printf("Usage: %s <options>\n\n"
43 "Options:\n"
44 "\t--codec <name>\tCodec to use (default=%s)\n"
45 "\t-h <height>\tHeight of the simulated video frame, in pixels\n"
46 "\t-w <width> \tWidth of the simulated video frame, in pixels\n"
47 "\t-v \tVerbose mode (show individual segment sizes)\n"
48 "\t--help \tShow this message\n"
49 "\n"
50 "Included decoders:\n"
51 "\n",
52 exec_name,
53 ifaces[0].name);
55 for (i = 0; i < sizeof(ifaces) / sizeof(ifaces[0]); i++)
56 printf(" %-6s - %s\n",
57 ifaces[i].name,
58 vpx_codec_iface_name(ifaces[i].iface));
60 exit(EXIT_FAILURE);
63 static void usage_error(const char *fmt, ...)
65 va_list ap;
66 va_start(ap, fmt);
67 vprintf(fmt, ap);
68 printf("\n");
69 usage_exit();
72 void my_mem_dtor(vpx_codec_mmap_t *mmap)
74 if (verbose)
75 printf("freeing segment %d\n", mmap->id);
77 free(mmap->priv);
80 int main(int argc, char **argv)
82 vpx_codec_ctx_t decoder;
83 vpx_codec_iface_t *iface = ifaces[0].iface;
84 vpx_codec_iter_t iter;
85 vpx_codec_dec_cfg_t cfg;
86 vpx_codec_err_t res = VPX_CODEC_OK;
87 unsigned int alloc_sz = 0;
88 unsigned int w = 352;
89 unsigned int h = 288;
90 int i;
92 exec_name = argv[0];
94 for (i = 1; i < argc; i++)
96 if (!strcmp(argv[i], "--codec"))
98 if (i + 1 < argc)
100 int j, k = -1;
102 i++;
104 for (j = 0; j < sizeof(ifaces) / sizeof(ifaces[0]); j++)
105 if (!strcmp(ifaces[j].name, argv[i]))
106 k = j;
108 if (k >= 0)
109 iface = ifaces[k].iface;
110 else
111 usage_error("Error: Unrecognized argument (%s) to --codec\n",
112 argv[i]);
114 else
115 usage_error("Error: Option --codec requires argument.\n");
117 else if (!strcmp(argv[i], "-v"))
118 verbose = 1;
119 else if (!strcmp(argv[i], "-h"))
120 if (i + 1 < argc)
122 h = atoi(argv[++i]);
124 else
125 usage_error("Error: Option -h requires argument.\n");
126 else if (!strcmp(argv[i], "-w"))
127 if (i + 1 < argc)
129 w = atoi(argv[++i]);
131 else
132 usage_error("Error: Option -w requires argument.\n");
133 else if (!strcmp(argv[i], "--help"))
134 usage_exit();
135 else
136 usage_error("Error: Unrecognized option %s\n\n", argv[i]);
139 if (argc == 1)
140 printf("Using built-in defaults. For options, rerun with --help\n\n");
142 /* XMA mode is not supported on all decoders! */
143 if (!(vpx_codec_get_caps(iface) & VPX_CODEC_CAP_XMA))
145 printf("%s does not support XMA mode!\n", vpx_codec_iface_name(iface));
146 return EXIT_FAILURE;
149 /* The codec knows how much memory to allocate based on the size of the
150 * encoded frames. This data can be parsed from the bitstream with
151 * vpx_codec_peek_stream_info() if a bitstream is available. Otherwise,
152 * a fixed size can be used that will be the upper limit on the frame
153 * size the decoder can decode.
155 cfg.w = w;
156 cfg.h = h;
158 /* Initialize the decoder in XMA mode. */
159 if (vpx_codec_dec_init(&decoder, iface, &cfg, VPX_CODEC_USE_XMA))
161 printf("Failed to initialize decoder in XMA mode: %s\n", vpx_codec_error(&decoder));
162 return EXIT_FAILURE;
165 /* Iterate through the list of memory maps, allocating them with the
166 * requested alignment.
168 iter = NULL;
172 vpx_codec_mmap_t mmap;
173 unsigned int align;
175 res = vpx_codec_get_mem_map(&decoder, &mmap, &iter);
176 align = mmap.align ? mmap.align - 1 : 0;
178 if (!res)
180 if (verbose)
181 printf("Allocating segment %u, size %lu, align %u %s\n",
182 mmap.id, mmap.sz, mmap.align,
183 mmap.flags & VPX_CODEC_MEM_ZERO ? "(ZEROED)" : "");
185 if (mmap.flags & VPX_CODEC_MEM_ZERO)
186 mmap.priv = calloc(1, mmap.sz + align);
187 else
188 mmap.priv = malloc(mmap.sz + align);
190 mmap.base = (void *)((((uintptr_t)mmap.priv) + align) & ~(uintptr_t)align);
191 mmap.dtor = my_mem_dtor;
192 alloc_sz += mmap.sz + align;
194 if (vpx_codec_set_mem_map(&decoder, &mmap, 1))
196 printf("Failed to set mmap: %s\n", vpx_codec_error(&decoder));
197 return EXIT_FAILURE;
200 else if (res != VPX_CODEC_LIST_END)
202 printf("Failed to get mmap: %s\n", vpx_codec_error(&decoder));
203 return EXIT_FAILURE;
206 while (res != VPX_CODEC_LIST_END);
208 printf("%s\n %d bytes external memory required for %dx%d.\n",
209 decoder.name, alloc_sz, cfg.w, cfg.h);
210 vpx_codec_destroy(&decoder);
211 return EXIT_SUCCESS;