example_xma

00001 /*
00002  *  Copyright (c) 2010 The VP8 project authors. All Rights Reserved.
00003  *
00004  *  Use of this source code is governed by a BSD-style license and patent
00005  *  grant that can be found in the LICENSE file in the root of the source
00006  *  tree. All contributing project authors may be found in the AUTHORS
00007  *  file in the root of the source tree.
00008  */
00009 
00010 
00011 /* This is a simple program showing how to initialize the decoder in XMA mode */
00012 #include <stdio.h>
00013 #include <stdlib.h>
00014 #include <stdarg.h>
00015 #include <string.h>
00016 #define VPX_CODEC_DISABLE_COMPAT 1
00017 #include "vpx_config.h"
00018 #include "vpx_decoder.h"
00019 #include "vpx_integer.h"
00020 #if CONFIG_VP8_DECODER
00021 #include "vp8dx.h"
00022 #endif
00023 
00024 static char *exec_name;
00025 static int   verbose = 0;
00026 
00027 static const struct
00028 {
00029     const char *name;
00030     const vpx_codec_iface_t *iface;
00031 } ifaces[] =
00032 {
00033 #if CONFIG_VP8_DECODER
00034     {"vp8",  &vpx_codec_vp8_dx_algo},
00035 #endif
00036 };
00037 
00038 static void usage_exit(void)
00039 {
00040     int i;
00041 
00042     printf("Usage: %s <options>\n\n"
00043            "Options:\n"
00044            "\t--codec <name>\tCodec to use (default=%s)\n"
00045            "\t-h <height>\tHeight of the simulated video frame, in pixels\n"
00046            "\t-w <width> \tWidth of the simulated video frame, in pixels\n"
00047            "\t-v         \tVerbose mode (show individual segment sizes)\n"
00048            "\t--help     \tShow this message\n"
00049            "\n"
00050            "Included decoders:\n"
00051            "\n",
00052            exec_name,
00053            ifaces[0].name);
00054 
00055     for (i = 0; i < sizeof(ifaces) / sizeof(ifaces[0]); i++)
00056         printf("    %-6s - %s\n",
00057                ifaces[i].name,
00058                vpx_codec_iface_name(ifaces[i].iface));
00059 
00060     exit(EXIT_FAILURE);
00061 }
00062 
00063 static void usage_error(const char *fmt, ...)
00064 {
00065     va_list ap;
00066     va_start(ap, fmt);
00067     vprintf(fmt, ap);
00068     printf("\n");
00069     usage_exit();
00070 }
00071 
00072 void my_mem_dtor(vpx_codec_mmap_t *mmap)
00073 {
00074     if (verbose)
00075         printf("freeing segment %d\n", mmap->id);
00076 
00077     free(mmap->priv);
00078 }
00079 
00080 int main(int argc, char **argv)
00081 {
00082     vpx_codec_ctx_t           decoder;
00083     vpx_codec_iface_t        *iface = ifaces[0].iface;
00084     vpx_codec_iter_t          iter;
00085     vpx_codec_dec_cfg_t       cfg;
00086     vpx_codec_err_t           res = VPX_CODEC_OK;
00087     unsigned int            alloc_sz = 0;
00088     unsigned int            w = 352;
00089     unsigned int            h = 288;
00090     int                     i;
00091 
00092     exec_name = argv[0];
00093 
00094     for (i = 1; i < argc; i++)
00095     {
00096         if (!strcmp(argv[i], "--codec"))
00097         {
00098             if (i + 1 < argc)
00099             {
00100                 int j, k = -1;
00101 
00102                 i++;
00103 
00104                 for (j = 0; j < sizeof(ifaces) / sizeof(ifaces[0]); j++)
00105                     if (!strcmp(ifaces[j].name, argv[i]))
00106                         k = j;
00107 
00108                 if (k >= 0)
00109                     iface = ifaces[k].iface;
00110                 else
00111                     usage_error("Error: Unrecognized argument (%s) to --codec\n",
00112                                 argv[i]);
00113             }
00114             else
00115                 usage_error("Error: Option --codec requires argument.\n");
00116         }
00117         else if (!strcmp(argv[i], "-v"))
00118             verbose = 1;
00119         else if (!strcmp(argv[i], "-h"))
00120             if (i + 1 < argc)
00121             {
00122                 h = atoi(argv[++i]);
00123             }
00124             else
00125                 usage_error("Error: Option -h requires argument.\n");
00126         else if (!strcmp(argv[i], "-w"))
00127             if (i + 1 < argc)
00128             {
00129                 w = atoi(argv[++i]);
00130             }
00131             else
00132                 usage_error("Error: Option -w requires argument.\n");
00133         else if (!strcmp(argv[i], "--help"))
00134             usage_exit();
00135         else
00136             usage_error("Error: Unrecognized option %s\n\n", argv[i]);
00137     }
00138 
00139     if (argc == 1)
00140         printf("Using built-in defaults. For options, rerun with --help\n\n");
00141 
00142     /* XMA mode is not supported on all decoders! */
00143     if (!(vpx_codec_get_caps(iface) & VPX_CODEC_CAP_XMA))
00144     {
00145         printf("%s does not support XMA mode!\n", vpx_codec_iface_name(iface));
00146         return EXIT_FAILURE;
00147     }
00148 
00149     /* The codec knows how much memory to allocate based on the size of the
00150      * encoded frames. This data can be parsed from the bitstream with
00151      * vpx_codec_peek_stream_info() if a bitstream is available. Otherwise,
00152      * a fixed size can be used that will be the upper limit on the frame
00153      * size the decoder can decode.
00154      */
00155     cfg.w = w;
00156     cfg.h = h;
00157 
00158     /* Initialize the decoder in XMA mode. */
00159     if (vpx_codec_dec_init(&decoder, iface, &cfg, VPX_CODEC_USE_XMA))
00160     {
00161         printf("Failed to initialize decoder in XMA mode: %s\n", vpx_codec_error(&decoder));
00162         return EXIT_FAILURE;
00163     }
00164 
00165     /* Iterate through the list of memory maps, allocating them with the
00166      * requested alignment.
00167      */
00168     iter = NULL;
00169 
00170     do
00171     {
00172         vpx_codec_mmap_t  mmap;
00173         unsigned int    align;
00174 
00175         res = vpx_codec_get_mem_map(&decoder, &mmap, &iter);
00176         align = mmap.align ? mmap.align - 1 : 0;
00177 
00178         if (!res)
00179         {
00180             if (verbose)
00181                 printf("Allocating segment %u, size %lu, align %u %s\n",
00182                        mmap.id, mmap.sz, mmap.align,
00183                        mmap.flags & VPX_CODEC_MEM_ZERO ? "(ZEROED)" : "");
00184 
00185             if (mmap.flags & VPX_CODEC_MEM_ZERO)
00186                 mmap.priv = calloc(1, mmap.sz + align);
00187             else
00188                 mmap.priv = malloc(mmap.sz + align);
00189 
00190             mmap.base = (void *)((((uintptr_t)mmap.priv) + align) & ~(uintptr_t)align);
00191             mmap.dtor = my_mem_dtor;
00192             alloc_sz += mmap.sz + align;
00193 
00194             if (vpx_codec_set_mem_map(&decoder, &mmap, 1))
00195             {
00196                 printf("Failed to set mmap: %s\n", vpx_codec_error(&decoder));
00197                 return EXIT_FAILURE;
00198             }
00199         }
00200         else if (res != VPX_CODEC_LIST_END)
00201         {
00202             printf("Failed to get mmap: %s\n", vpx_codec_error(&decoder));
00203             return EXIT_FAILURE;
00204         }
00205     }
00206     while (res != VPX_CODEC_LIST_END);
00207 
00208     printf("%s\n    %d bytes external memory required for %dx%d.\n",
00209            decoder.name, alloc_sz, cfg.w, cfg.h);
00210     vpx_codec_destroy(&decoder);
00211     return EXIT_SUCCESS;
00212 
00213 }

Generated on 12 Nov 2011 for WebM VP8 Codec SDK by  doxygen 1.6.1