Merge "configure: detect toolchain if not specified"
[profile/ivi/libvpx.git] / example_xma.c
1 /*
2  *  Copyright (c) 2010 The VP8 project authors. All Rights Reserved.
3  *
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  */
9
10
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
23
24 static char *exec_name;
25 static int   verbose = 0;
26
27 static const struct
28 {
29     const char *name;
30     const vpx_codec_iface_t *iface;
31 } ifaces[] =
32 {
33 #if CONFIG_VP8_DECODER
34     {"vp8",  &vpx_codec_vp8_dx_algo},
35 #endif
36 };
37
38 static void usage_exit(void)
39 {
40     int i;
41
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);
54
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));
59
60     exit(EXIT_FAILURE);
61 }
62
63 static void usage_error(const char *fmt, ...)
64 {
65     va_list ap;
66     va_start(ap, fmt);
67     vprintf(fmt, ap);
68     printf("\n");
69     usage_exit();
70 }
71
72 void my_mem_dtor(vpx_codec_mmap_t *mmap)
73 {
74     if (verbose)
75         printf("freeing segment %d\n", mmap->id);
76
77     free(mmap->priv);
78 }
79
80 int main(int argc, char **argv)
81 {
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;
91
92     exec_name = argv[0];
93
94     for (i = 1; i < argc; i++)
95     {
96         if (!strcmp(argv[i], "--codec"))
97         {
98             if (i + 1 < argc)
99             {
100                 int j, k = -1;
101
102                 i++;
103
104                 for (j = 0; j < sizeof(ifaces) / sizeof(ifaces[0]); j++)
105                     if (!strcmp(ifaces[j].name, argv[i]))
106                         k = j;
107
108                 if (k >= 0)
109                     iface = ifaces[k].iface;
110                 else
111                     usage_error("Error: Unrecognized argument (%s) to --codec\n",
112                                 argv[i]);
113             }
114             else
115                 usage_error("Error: Option --codec requires argument.\n");
116         }
117         else if (!strcmp(argv[i], "-v"))
118             verbose = 1;
119         else if (!strcmp(argv[i], "-h"))
120             if (i + 1 < argc)
121             {
122                 h = atoi(argv[++i]);
123             }
124             else
125                 usage_error("Error: Option -h requires argument.\n");
126         else if (!strcmp(argv[i], "-w"))
127             if (i + 1 < argc)
128             {
129                 w = atoi(argv[++i]);
130             }
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]);
137     }
138
139     if (argc == 1)
140         printf("Using built-in defaults. For options, rerun with --help\n\n");
141
142     /* XMA mode is not supported on all decoders! */
143     if (!(vpx_codec_get_caps(iface) & VPX_CODEC_CAP_XMA))
144     {
145         printf("%s does not support XMA mode!\n", vpx_codec_iface_name(iface));
146         return EXIT_FAILURE;
147     }
148
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.
154      */
155     cfg.w = w;
156     cfg.h = h;
157
158     /* Initialize the decoder in XMA mode. */
159     if (vpx_codec_dec_init(&decoder, iface, &cfg, VPX_CODEC_USE_XMA))
160     {
161         printf("Failed to initialize decoder in XMA mode: %s\n", vpx_codec_error(&decoder));
162         return EXIT_FAILURE;
163     }
164
165     /* Iterate through the list of memory maps, allocating them with the
166      * requested alignment.
167      */
168     iter = NULL;
169
170     do
171     {
172         vpx_codec_mmap_t  mmap;
173         unsigned int    align;
174
175         res = vpx_codec_get_mem_map(&decoder, &mmap, &iter);
176         align = mmap.align ? mmap.align - 1 : 0;
177
178         if (!res)
179         {
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)" : "");
184
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);
189
190             mmap.base = (void *)((((uintptr_t)mmap.priv) + align) & ~(uintptr_t)align);
191             mmap.dtor = my_mem_dtor;
192             alloc_sz += mmap.sz + align;
193
194             if (vpx_codec_set_mem_map(&decoder, &mmap, 1))
195             {
196                 printf("Failed to set mmap: %s\n", vpx_codec_error(&decoder));
197                 return EXIT_FAILURE;
198             }
199         }
200         else if (res != VPX_CODEC_LIST_END)
201         {
202             printf("Failed to get mmap: %s\n", vpx_codec_error(&decoder));
203             return EXIT_FAILURE;
204         }
205     }
206     while (res != VPX_CODEC_LIST_END);
207
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;
212
213 }