diff --git a/bin/windows/vpx/include/vpx/vp8.h b/bin/windows/vpx/include/vpx/vp8.h new file mode 100644 index 0000000000..8a035f9770 --- /dev/null +++ b/bin/windows/vpx/include/vpx/vp8.h @@ -0,0 +1,148 @@ +/* + * Copyright (c) 2010 The WebM project authors. All Rights Reserved. + * + * Use of this source code is governed by a BSD-style license + * that can be found in the LICENSE file in the root of the source + * tree. An additional intellectual property rights grant can be found + * in the file PATENTS. All contributing project authors may + * be found in the AUTHORS file in the root of the source tree. + */ + +/*!\defgroup vp8 VP8 + * \ingroup codecs + * VP8 is vpx's newest video compression algorithm that uses motion + * compensated prediction, Discrete Cosine Transform (DCT) coding of the + * prediction error signal and context dependent entropy coding techniques + * based on arithmetic principles. It features: + * - YUV 4:2:0 image format + * - Macro-block based coding (16x16 luma plus two 8x8 chroma) + * - 1/4 (1/8) pixel accuracy motion compensated prediction + * - 4x4 DCT transform + * - 128 level linear quantizer + * - In loop deblocking filter + * - Context-based entropy coding + * + * @{ + */ +/*!\file + * \brief Provides controls common to both the VP8 encoder and decoder. + */ +#ifndef VPX_VP8_H_ +#define VPX_VP8_H_ + +#include "./vpx_codec.h" +#include "./vpx_image.h" + +#ifdef __cplusplus +extern "C" { +#endif + +/*!\brief Control functions + * + * The set of macros define the control functions of VP8 interface + */ +enum vp8_com_control_id { + VP8_SET_REFERENCE = 1, /**< pass in an external frame into decoder to be used as reference frame */ + VP8_COPY_REFERENCE = 2, /**< get a copy of reference frame from the decoder */ + VP8_SET_POSTPROC = 3, /**< set the decoder's post processing settings */ + VP8_SET_DBG_COLOR_REF_FRAME = 4, /**< set the reference frames to color for each macroblock */ + VP8_SET_DBG_COLOR_MB_MODES = 5, /**< set which macro block modes to color */ + VP8_SET_DBG_COLOR_B_MODES = 6, /**< set which blocks modes to color */ + VP8_SET_DBG_DISPLAY_MV = 7, /**< set which motion vector modes to draw */ + + /* TODO(jkoleszar): The encoder incorrectly reuses some of these values (5+) + * for its control ids. These should be migrated to something like the + * VP8_DECODER_CTRL_ID_START range next time we're ready to break the ABI. + */ + VP9_GET_REFERENCE = 128, /**< get a pointer to a reference frame */ + VP8_COMMON_CTRL_ID_MAX, + VP8_DECODER_CTRL_ID_START = 256 +}; + +/*!\brief post process flags + * + * The set of macros define VP8 decoder post processing flags + */ +enum vp8_postproc_level { + VP8_NOFILTERING = 0, + VP8_DEBLOCK = 1 << 0, + VP8_DEMACROBLOCK = 1 << 1, + VP8_ADDNOISE = 1 << 2, + VP8_DEBUG_TXT_FRAME_INFO = 1 << 3, /**< print frame information */ + VP8_DEBUG_TXT_MBLK_MODES = 1 << 4, /**< print macro block modes over each macro block */ + VP8_DEBUG_TXT_DC_DIFF = 1 << 5, /**< print dc diff for each macro block */ + VP8_DEBUG_TXT_RATE_INFO = 1 << 6, /**< print video rate info (encoder only) */ + VP8_MFQE = 1 << 10 +}; + +/*!\brief post process flags + * + * This define a structure that describe the post processing settings. For + * the best objective measure (using the PSNR metric) set post_proc_flag + * to VP8_DEBLOCK and deblocking_level to 1. + */ + +typedef struct vp8_postproc_cfg { + int post_proc_flag; /**< the types of post processing to be done, should be combination of "vp8_postproc_level" */ + int deblocking_level; /**< the strength of deblocking, valid range [0, 16] */ + int noise_level; /**< the strength of additive noise, valid range [0, 16] */ +} vp8_postproc_cfg_t; + +/*!\brief reference frame type + * + * The set of macros define the type of VP8 reference frames + */ +typedef enum vpx_ref_frame_type { + VP8_LAST_FRAME = 1, + VP8_GOLD_FRAME = 2, + VP8_ALTR_FRAME = 4 +} vpx_ref_frame_type_t; + +/*!\brief reference frame data struct + * + * Define the data struct to access vp8 reference frames. + */ +typedef struct vpx_ref_frame { + vpx_ref_frame_type_t frame_type; /**< which reference frame */ + vpx_image_t img; /**< reference frame data in image format */ +} vpx_ref_frame_t; + +/*!\brief VP9 specific reference frame data struct + * + * Define the data struct to access vp9 reference frames. + */ +typedef struct vp9_ref_frame { + int idx; /**< frame index to get (input) */ + vpx_image_t img; /**< img structure to populate (output) */ +} vp9_ref_frame_t; + +/*!\cond */ +/*!\brief vp8 decoder control function parameter type + * + * defines the data type for each of VP8 decoder control function requires + */ +VPX_CTRL_USE_TYPE(VP8_SET_REFERENCE, vpx_ref_frame_t *) +#define VPX_CTRL_VP8_SET_REFERENCE +VPX_CTRL_USE_TYPE(VP8_COPY_REFERENCE, vpx_ref_frame_t *) +#define VPX_CTRL_VP8_COPY_REFERENCE +VPX_CTRL_USE_TYPE(VP8_SET_POSTPROC, vp8_postproc_cfg_t *) +#define VPX_CTRL_VP8_SET_POSTPROC +VPX_CTRL_USE_TYPE(VP8_SET_DBG_COLOR_REF_FRAME, int) +#define VPX_CTRL_VP8_SET_DBG_COLOR_REF_FRAME +VPX_CTRL_USE_TYPE(VP8_SET_DBG_COLOR_MB_MODES, int) +#define VPX_CTRL_VP8_SET_DBG_COLOR_MB_MODES +VPX_CTRL_USE_TYPE(VP8_SET_DBG_COLOR_B_MODES, int) +#define VPX_CTRL_VP8_SET_DBG_COLOR_B_MODES +VPX_CTRL_USE_TYPE(VP8_SET_DBG_DISPLAY_MV, int) +#define VPX_CTRL_VP8_SET_DBG_DISPLAY_MV +VPX_CTRL_USE_TYPE(VP9_GET_REFERENCE, vp9_ref_frame_t *) +#define VPX_CTRL_VP9_GET_REFERENCE + +/*!\endcond */ +/*! @} - end defgroup vp8 */ + +#ifdef __cplusplus +} // extern "C" +#endif + +#endif // VPX_VP8_H_ diff --git a/bin/windows/vpx/include/vpx/vp8dx.h b/bin/windows/vpx/include/vpx/vp8dx.h new file mode 100644 index 0000000000..67c97bb6c9 --- /dev/null +++ b/bin/windows/vpx/include/vpx/vp8dx.h @@ -0,0 +1,176 @@ +/* + * Copyright (c) 2010 The WebM project authors. All Rights Reserved. + * + * Use of this source code is governed by a BSD-style license + * that can be found in the LICENSE file in the root of the source + * tree. An additional intellectual property rights grant can be found + * in the file PATENTS. All contributing project authors may + * be found in the AUTHORS file in the root of the source tree. + */ + + +/*!\defgroup vp8_decoder WebM VP8/VP9 Decoder + * \ingroup vp8 + * + * @{ + */ +/*!\file + * \brief Provides definitions for using VP8 or VP9 within the vpx Decoder + * interface. + */ +#ifndef VPX_VP8DX_H_ +#define VPX_VP8DX_H_ + +#ifdef __cplusplus +extern "C" { +#endif + +/* Include controls common to both the encoder and decoder */ +#include "./vp8.h" + +/*!\name Algorithm interface for VP8 + * + * This interface provides the capability to decode VP8 streams. + * @{ + */ +extern vpx_codec_iface_t vpx_codec_vp8_dx_algo; +extern vpx_codec_iface_t *vpx_codec_vp8_dx(void); +/*!@} - end algorithm interface member group*/ + +/*!\name Algorithm interface for VP9 + * + * This interface provides the capability to decode VP9 streams. + * @{ + */ +extern vpx_codec_iface_t vpx_codec_vp9_dx_algo; +extern vpx_codec_iface_t *vpx_codec_vp9_dx(void); +/*!@} - end algorithm interface member group*/ + +/*!\enum vp8_dec_control_id + * \brief VP8 decoder control functions + * + * This set of macros define the control functions available for the VP8 + * decoder interface. + * + * \sa #vpx_codec_control + */ +enum vp8_dec_control_id { + /** control function to get info on which reference frames were updated + * by the last decode + */ + VP8D_GET_LAST_REF_UPDATES = VP8_DECODER_CTRL_ID_START, + + /** check if the indicated frame is corrupted */ + VP8D_GET_FRAME_CORRUPTED, + + /** control function to get info on which reference frames were used + * by the last decode + */ + VP8D_GET_LAST_REF_USED, + + /** decryption function to decrypt encoded buffer data immediately + * before decoding. Takes a vpx_decrypt_init, which contains + * a callback function and opaque context pointer. + */ + VPXD_SET_DECRYPTOR, + VP8D_SET_DECRYPTOR = VPXD_SET_DECRYPTOR, + + /** control function to get the dimensions that the current frame is decoded + * at. This may be different to the intended display size for the frame as + * specified in the wrapper or frame header (see VP9D_GET_DISPLAY_SIZE). */ + VP9D_GET_FRAME_SIZE, + + /** control function to get the current frame's intended display dimensions + * (as specified in the wrapper or frame header). This may be different to + * the decoded dimensions of this frame (see VP9D_GET_FRAME_SIZE). */ + VP9D_GET_DISPLAY_SIZE, + + /** control function to get the bit depth of the stream. */ + VP9D_GET_BIT_DEPTH, + + /** control function to set the byte alignment of the planes in the reference + * buffers. Valid values are power of 2, from 32 to 1024. A value of 0 sets + * legacy alignment. I.e. Y plane is aligned to 32 bytes, U plane directly + * follows Y plane, and V plane directly follows U plane. Default value is 0. + */ + VP9_SET_BYTE_ALIGNMENT, + + /** control function to invert the decoding order to from right to left. The + * function is used in a test to confirm the decoding independence of tile + * columns. The function may be used in application where this order + * of decoding is desired. + * + * TODO(yaowu): Rework the unit test that uses this control, and in a future + * release, this test-only control shall be removed. + */ + VP9_INVERT_TILE_DECODE_ORDER, + + /** control function to set the skip loop filter flag. Valid values are + * integers. The decoder will skip the loop filter when its value is set to + * nonzero. If the loop filter is skipped the decoder may accumulate decode + * artifacts. The default value is 0. + */ + VP9_SET_SKIP_LOOP_FILTER, + + VP8_DECODER_CTRL_ID_MAX +}; + +/** Decrypt n bytes of data from input -> output, using the decrypt_state + * passed in VPXD_SET_DECRYPTOR. + */ +typedef void (*vpx_decrypt_cb)(void *decrypt_state, const unsigned char *input, + unsigned char *output, int count); + +/*!\brief Structure to hold decryption state + * + * Defines a structure to hold the decryption state and access function. + */ +typedef struct vpx_decrypt_init { + /*! Decrypt callback. */ + vpx_decrypt_cb decrypt_cb; + + /*! Decryption state. */ + void *decrypt_state; +} vpx_decrypt_init; + +/*!\brief A deprecated alias for vpx_decrypt_init. + */ +typedef vpx_decrypt_init vp8_decrypt_init; + + +/*!\cond */ +/*!\brief VP8 decoder control function parameter type + * + * Defines the data types that VP8D control functions take. Note that + * additional common controls are defined in vp8.h + * + */ + + +VPX_CTRL_USE_TYPE(VP8D_GET_LAST_REF_UPDATES, int *) +#define VPX_CTRL_VP8D_GET_LAST_REF_UPDATES +VPX_CTRL_USE_TYPE(VP8D_GET_FRAME_CORRUPTED, int *) +#define VPX_CTRL_VP8D_GET_FRAME_CORRUPTED +VPX_CTRL_USE_TYPE(VP8D_GET_LAST_REF_USED, int *) +#define VPX_CTRL_VP8D_GET_LAST_REF_USED +VPX_CTRL_USE_TYPE(VPXD_SET_DECRYPTOR, vpx_decrypt_init *) +#define VPX_CTRL_VPXD_SET_DECRYPTOR +VPX_CTRL_USE_TYPE(VP8D_SET_DECRYPTOR, vpx_decrypt_init *) +#define VPX_CTRL_VP8D_SET_DECRYPTOR +VPX_CTRL_USE_TYPE(VP9D_GET_DISPLAY_SIZE, int *) +#define VPX_CTRL_VP9D_GET_DISPLAY_SIZE +VPX_CTRL_USE_TYPE(VP9D_GET_BIT_DEPTH, unsigned int *) +#define VPX_CTRL_VP9D_GET_BIT_DEPTH +VPX_CTRL_USE_TYPE(VP9D_GET_FRAME_SIZE, int *) +#define VPX_CTRL_VP9D_GET_FRAME_SIZE +VPX_CTRL_USE_TYPE(VP9_INVERT_TILE_DECODE_ORDER, int) +#define VPX_CTRL_VP9_INVERT_TILE_DECODE_ORDER + +/*!\endcond */ +/*! @} - end defgroup vp8_decoder */ + +#ifdef __cplusplus +} // extern "C" +#endif + +#endif // VPX_VP8DX_H_ diff --git a/bin/windows/vpx/include/vpx/vpx_codec.h b/bin/windows/vpx/include/vpx/vpx_codec.h new file mode 100644 index 0000000000..b6037bb4d7 --- /dev/null +++ b/bin/windows/vpx/include/vpx/vpx_codec.h @@ -0,0 +1,479 @@ +/* + * Copyright (c) 2010 The WebM project authors. All Rights Reserved. + * + * Use of this source code is governed by a BSD-style license + * that can be found in the LICENSE file in the root of the source + * tree. An additional intellectual property rights grant can be found + * in the file PATENTS. All contributing project authors may + * be found in the AUTHORS file in the root of the source tree. + */ + + +/*!\defgroup codec Common Algorithm Interface + * This abstraction allows applications to easily support multiple video + * formats with minimal code duplication. This section describes the interface + * common to all codecs (both encoders and decoders). + * @{ + */ + +/*!\file + * \brief Describes the codec algorithm interface to applications. + * + * This file describes the interface between an application and a + * video codec algorithm. + * + * An application instantiates a specific codec instance by using + * vpx_codec_init() and a pointer to the algorithm's interface structure: + *
+ *     my_app.c:
+ *       extern vpx_codec_iface_t my_codec;
+ *       {
+ *           vpx_codec_ctx_t algo;
+ *           res = vpx_codec_init(&algo, &my_codec);
+ *       }
+ *     
+ * + * Once initialized, the instance is manged using other functions from + * the vpx_codec_* family. + */ +#ifndef VPX_VPX_CODEC_H_ +#define VPX_VPX_CODEC_H_ + +#ifdef __cplusplus +extern "C" { +#endif + +#include "./vpx_integer.h" +#include "./vpx_image.h" + + /*!\brief Decorator indicating a function is deprecated */ +#ifndef DEPRECATED +#if defined(__GNUC__) && __GNUC__ +#define DEPRECATED __attribute__ ((deprecated)) +#elif defined(_MSC_VER) +#define DEPRECATED +#else +#define DEPRECATED +#endif +#endif /* DEPRECATED */ + +#ifndef DECLSPEC_DEPRECATED +#if defined(__GNUC__) && __GNUC__ +#define DECLSPEC_DEPRECATED /**< \copydoc #DEPRECATED */ +#elif defined(_MSC_VER) +#define DECLSPEC_DEPRECATED __declspec(deprecated) /**< \copydoc #DEPRECATED */ +#else +#define DECLSPEC_DEPRECATED /**< \copydoc #DEPRECATED */ +#endif +#endif /* DECLSPEC_DEPRECATED */ + + /*!\brief Decorator indicating a function is potentially unused */ +#ifdef UNUSED +#elif defined(__GNUC__) || defined(__clang__) +#define UNUSED __attribute__ ((unused)) +#else +#define UNUSED +#endif + + /*!\brief Current ABI version number + * + * \internal + * If this file is altered in any way that changes the ABI, this value + * must be bumped. Examples include, but are not limited to, changing + * types, removing or reassigning enums, adding/removing/rearranging + * fields to structures + */ +#define VPX_CODEC_ABI_VERSION (3 + VPX_IMAGE_ABI_VERSION) /**<\hideinitializer*/ + + /*!\brief Algorithm return codes */ + typedef enum { + /*!\brief Operation completed without error */ + VPX_CODEC_OK, + + /*!\brief Unspecified error */ + VPX_CODEC_ERROR, + + /*!\brief Memory operation failed */ + VPX_CODEC_MEM_ERROR, + + /*!\brief ABI version mismatch */ + VPX_CODEC_ABI_MISMATCH, + + /*!\brief Algorithm does not have required capability */ + VPX_CODEC_INCAPABLE, + + /*!\brief The given bitstream is not supported. + * + * The bitstream was unable to be parsed at the highest level. The decoder + * is unable to proceed. This error \ref SHOULD be treated as fatal to the + * stream. */ + VPX_CODEC_UNSUP_BITSTREAM, + + /*!\brief Encoded bitstream uses an unsupported feature + * + * The decoder does not implement a feature required by the encoder. This + * return code should only be used for features that prevent future + * pictures from being properly decoded. This error \ref MAY be treated as + * fatal to the stream or \ref MAY be treated as fatal to the current GOP. + */ + VPX_CODEC_UNSUP_FEATURE, + + /*!\brief The coded data for this stream is corrupt or incomplete + * + * There was a problem decoding the current frame. This return code + * should only be used for failures that prevent future pictures from + * being properly decoded. This error \ref MAY be treated as fatal to the + * stream or \ref MAY be treated as fatal to the current GOP. If decoding + * is continued for the current GOP, artifacts may be present. + */ + VPX_CODEC_CORRUPT_FRAME, + + /*!\brief An application-supplied parameter is not valid. + * + */ + VPX_CODEC_INVALID_PARAM, + + /*!\brief An iterator reached the end of list. + * + */ + VPX_CODEC_LIST_END + + } + vpx_codec_err_t; + + + /*! \brief Codec capabilities bitfield + * + * Each codec advertises the capabilities it supports as part of its + * ::vpx_codec_iface_t interface structure. Capabilities are extra interfaces + * or functionality, and are not required to be supported. + * + * The available flags are specified by VPX_CODEC_CAP_* defines. + */ + typedef long vpx_codec_caps_t; +#define VPX_CODEC_CAP_DECODER 0x1 /**< Is a decoder */ +#define VPX_CODEC_CAP_ENCODER 0x2 /**< Is an encoder */ + + + /*! \brief Initialization-time Feature Enabling + * + * Certain codec features must be known at initialization time, to allow for + * proper memory allocation. + * + * The available flags are specified by VPX_CODEC_USE_* defines. + */ + typedef long vpx_codec_flags_t; + + + /*!\brief Codec interface structure. + * + * Contains function pointers and other data private to the codec + * implementation. This structure is opaque to the application. + */ + typedef const struct vpx_codec_iface vpx_codec_iface_t; + + + /*!\brief Codec private data structure. + * + * Contains data private to the codec implementation. This structure is opaque + * to the application. + */ + typedef struct vpx_codec_priv vpx_codec_priv_t; + + + /*!\brief Iterator + * + * Opaque storage used for iterating over lists. + */ + typedef const void *vpx_codec_iter_t; + + + /*!\brief Codec context structure + * + * All codecs \ref MUST support this context structure fully. In general, + * this data should be considered private to the codec algorithm, and + * not be manipulated or examined by the calling application. Applications + * may reference the 'name' member to get a printable description of the + * algorithm. + */ + typedef struct vpx_codec_ctx { + const char *name; /**< Printable interface name */ + vpx_codec_iface_t *iface; /**< Interface pointers */ + vpx_codec_err_t err; /**< Last returned error */ + const char *err_detail; /**< Detailed info, if available */ + vpx_codec_flags_t init_flags; /**< Flags passed at init time */ + union { + /**< Decoder Configuration Pointer */ + const struct vpx_codec_dec_cfg *dec; + /**< Encoder Configuration Pointer */ + const struct vpx_codec_enc_cfg *enc; + const void *raw; + } config; /**< Configuration pointer aliasing union */ + vpx_codec_priv_t *priv; /**< Algorithm private storage */ + } vpx_codec_ctx_t; + + /*!\brief Bit depth for codec + * * + * This enumeration determines the bit depth of the codec. + */ + typedef enum vpx_bit_depth { + VPX_BITS_8 = 8, /**< 8 bits */ + VPX_BITS_10 = 10, /**< 10 bits */ + VPX_BITS_12 = 12, /**< 12 bits */ + } vpx_bit_depth_t; + + /* + * Library Version Number Interface + * + * For example, see the following sample return values: + * vpx_codec_version() (1<<16 | 2<<8 | 3) + * vpx_codec_version_str() "v1.2.3-rc1-16-gec6a1ba" + * vpx_codec_version_extra_str() "rc1-16-gec6a1ba" + */ + + /*!\brief Return the version information (as an integer) + * + * Returns a packed encoding of the library version number. This will only include + * the major.minor.patch component of the version number. Note that this encoded + * value should be accessed through the macros provided, as the encoding may change + * in the future. + * + */ + int vpx_codec_version(void); +#define VPX_VERSION_MAJOR(v) ((v>>16)&0xff) /**< extract major from packed version */ +#define VPX_VERSION_MINOR(v) ((v>>8)&0xff) /**< extract minor from packed version */ +#define VPX_VERSION_PATCH(v) ((v>>0)&0xff) /**< extract patch from packed version */ + + /*!\brief Return the version major number */ +#define vpx_codec_version_major() ((vpx_codec_version()>>16)&0xff) + + /*!\brief Return the version minor number */ +#define vpx_codec_version_minor() ((vpx_codec_version()>>8)&0xff) + + /*!\brief Return the version patch number */ +#define vpx_codec_version_patch() ((vpx_codec_version()>>0)&0xff) + + + /*!\brief Return the version information (as a string) + * + * Returns a printable string containing the full library version number. This may + * contain additional text following the three digit version number, as to indicate + * release candidates, prerelease versions, etc. + * + */ + const char *vpx_codec_version_str(void); + + + /*!\brief Return the version information (as a string) + * + * Returns a printable "extra string". This is the component of the string returned + * by vpx_codec_version_str() following the three digit version number. + * + */ + const char *vpx_codec_version_extra_str(void); + + + /*!\brief Return the build configuration + * + * Returns a printable string containing an encoded version of the build + * configuration. This may be useful to vpx support. + * + */ + const char *vpx_codec_build_config(void); + + + /*!\brief Return the name for a given interface + * + * Returns a human readable string for name of the given codec interface. + * + * \param[in] iface Interface pointer + * + */ + const char *vpx_codec_iface_name(vpx_codec_iface_t *iface); + + + /*!\brief Convert error number to printable string + * + * Returns a human readable string for the last error returned by the + * algorithm. The returned error will be one line and will not contain + * any newline characters. + * + * + * \param[in] err Error number. + * + */ + const char *vpx_codec_err_to_string(vpx_codec_err_t err); + + + /*!\brief Retrieve error synopsis for codec context + * + * Returns a human readable string for the last error returned by the + * algorithm. The returned error will be one line and will not contain + * any newline characters. + * + * + * \param[in] ctx Pointer to this instance's context. + * + */ + const char *vpx_codec_error(vpx_codec_ctx_t *ctx); + + + /*!\brief Retrieve detailed error information for codec context + * + * Returns a human readable string providing detailed information about + * the last error. + * + * \param[in] ctx Pointer to this instance's context. + * + * \retval NULL + * No detailed information is available. + */ + const char *vpx_codec_error_detail(vpx_codec_ctx_t *ctx); + + + /* REQUIRED FUNCTIONS + * + * The following functions are required to be implemented for all codecs. + * They represent the base case functionality expected of all codecs. + */ + + /*!\brief Destroy a codec instance + * + * Destroys a codec context, freeing any associated memory buffers. + * + * \param[in] ctx Pointer to this instance's context + * + * \retval #VPX_CODEC_OK + * The codec algorithm initialized. + * \retval #VPX_CODEC_MEM_ERROR + * Memory allocation failed. + */ + vpx_codec_err_t vpx_codec_destroy(vpx_codec_ctx_t *ctx); + + + /*!\brief Get the capabilities of an algorithm. + * + * Retrieves the capabilities bitfield from the algorithm's interface. + * + * \param[in] iface Pointer to the algorithm interface + * + */ + vpx_codec_caps_t vpx_codec_get_caps(vpx_codec_iface_t *iface); + + + /*!\brief Control algorithm + * + * This function is used to exchange algorithm specific data with the codec + * instance. This can be used to implement features specific to a particular + * algorithm. + * + * This wrapper function dispatches the request to the helper function + * associated with the given ctrl_id. It tries to call this function + * transparently, but will return #VPX_CODEC_ERROR if the request could not + * be dispatched. + * + * Note that this function should not be used directly. Call the + * #vpx_codec_control wrapper macro instead. + * + * \param[in] ctx Pointer to this instance's context + * \param[in] ctrl_id Algorithm specific control identifier + * + * \retval #VPX_CODEC_OK + * The control request was processed. + * \retval #VPX_CODEC_ERROR + * The control request was not processed. + * \retval #VPX_CODEC_INVALID_PARAM + * The data was not valid. + */ + vpx_codec_err_t vpx_codec_control_(vpx_codec_ctx_t *ctx, + int ctrl_id, + ...); +#if defined(VPX_DISABLE_CTRL_TYPECHECKS) && VPX_DISABLE_CTRL_TYPECHECKS +# define vpx_codec_control(ctx,id,data) vpx_codec_control_(ctx,id,data) +# define VPX_CTRL_USE_TYPE(id, typ) +# define VPX_CTRL_USE_TYPE_DEPRECATED(id, typ) +# define VPX_CTRL_VOID(id, typ) + +#else + /*!\brief vpx_codec_control wrapper macro + * + * This macro allows for type safe conversions across the variadic parameter + * to vpx_codec_control_(). + * + * \internal + * It works by dispatching the call to the control function through a wrapper + * function named with the id parameter. + */ +# define vpx_codec_control(ctx,id,data) vpx_codec_control_##id(ctx,id,data)\ + /**<\hideinitializer*/ + + + /*!\brief vpx_codec_control type definition macro + * + * This macro allows for type safe conversions across the variadic parameter + * to vpx_codec_control_(). It defines the type of the argument for a given + * control identifier. + * + * \internal + * It defines a static function with + * the correctly typed arguments as a wrapper to the type-unsafe internal + * function. + */ +# define VPX_CTRL_USE_TYPE(id, typ) \ + static vpx_codec_err_t \ + vpx_codec_control_##id(vpx_codec_ctx_t*, int, typ) UNUSED;\ + \ + static vpx_codec_err_t \ + vpx_codec_control_##id(vpx_codec_ctx_t *ctx, int ctrl_id, typ data) {\ + return vpx_codec_control_(ctx, ctrl_id, data);\ + } /**<\hideinitializer*/ + + + /*!\brief vpx_codec_control deprecated type definition macro + * + * Like #VPX_CTRL_USE_TYPE, but indicates that the specified control is + * deprecated and should not be used. Consult the documentation for your + * codec for more information. + * + * \internal + * It defines a static function with the correctly typed arguments as a + * wrapper to the type-unsafe internal function. + */ +# define VPX_CTRL_USE_TYPE_DEPRECATED(id, typ) \ + DECLSPEC_DEPRECATED static vpx_codec_err_t \ + vpx_codec_control_##id(vpx_codec_ctx_t*, int, typ) DEPRECATED UNUSED;\ + \ + DECLSPEC_DEPRECATED static vpx_codec_err_t \ + vpx_codec_control_##id(vpx_codec_ctx_t *ctx, int ctrl_id, typ data) {\ + return vpx_codec_control_(ctx, ctrl_id, data);\ + } /**<\hideinitializer*/ + + + /*!\brief vpx_codec_control void type definition macro + * + * This macro allows for type safe conversions across the variadic parameter + * to vpx_codec_control_(). It indicates that a given control identifier takes + * no argument. + * + * \internal + * It defines a static function without a data argument as a wrapper to the + * type-unsafe internal function. + */ +# define VPX_CTRL_VOID(id) \ + static vpx_codec_err_t \ + vpx_codec_control_##id(vpx_codec_ctx_t*, int) UNUSED;\ + \ + static vpx_codec_err_t \ + vpx_codec_control_##id(vpx_codec_ctx_t *ctx, int ctrl_id) {\ + return vpx_codec_control_(ctx, ctrl_id);\ + } /**<\hideinitializer*/ + + +#endif + + /*!@} - end defgroup codec*/ +#ifdef __cplusplus +} +#endif +#endif // VPX_VPX_CODEC_H_ + diff --git a/bin/windows/vpx/include/vpx/vpx_decoder.h b/bin/windows/vpx/include/vpx/vpx_decoder.h new file mode 100644 index 0000000000..62fd919756 --- /dev/null +++ b/bin/windows/vpx/include/vpx/vpx_decoder.h @@ -0,0 +1,378 @@ +/* + * Copyright (c) 2010 The WebM project authors. All Rights Reserved. + * + * Use of this source code is governed by a BSD-style license + * that can be found in the LICENSE file in the root of the source + * tree. An additional intellectual property rights grant can be found + * in the file PATENTS. All contributing project authors may + * be found in the AUTHORS file in the root of the source tree. + */ +#ifndef VPX_VPX_DECODER_H_ +#define VPX_VPX_DECODER_H_ + +/*!\defgroup decoder Decoder Algorithm Interface + * \ingroup codec + * This abstraction allows applications using this decoder to easily support + * multiple video formats with minimal code duplication. This section describes + * the interface common to all decoders. + * @{ + */ + +/*!\file + * \brief Describes the decoder algorithm interface to applications. + * + * This file describes the interface between an application and a + * video decoder algorithm. + * + */ +#ifdef __cplusplus +extern "C" { +#endif + +#include "./vpx_codec.h" +#include "./vpx_frame_buffer.h" + + /*!\brief Current ABI version number + * + * \internal + * If this file is altered in any way that changes the ABI, this value + * must be bumped. Examples include, but are not limited to, changing + * types, removing or reassigning enums, adding/removing/rearranging + * fields to structures + */ +#define VPX_DECODER_ABI_VERSION (3 + VPX_CODEC_ABI_VERSION) /**<\hideinitializer*/ + + /*! \brief Decoder capabilities bitfield + * + * Each decoder advertises the capabilities it supports as part of its + * ::vpx_codec_iface_t interface structure. Capabilities are extra interfaces + * or functionality, and are not required to be supported by a decoder. + * + * The available flags are specified by VPX_CODEC_CAP_* defines. + */ +#define VPX_CODEC_CAP_PUT_SLICE 0x10000 /**< Will issue put_slice callbacks */ +#define VPX_CODEC_CAP_PUT_FRAME 0x20000 /**< Will issue put_frame callbacks */ +#define VPX_CODEC_CAP_POSTPROC 0x40000 /**< Can postprocess decoded frame */ +#define VPX_CODEC_CAP_ERROR_CONCEALMENT 0x80000 /**< Can conceal errors due to + packet loss */ +#define VPX_CODEC_CAP_INPUT_FRAGMENTS 0x100000 /**< Can receive encoded frames + one fragment at a time */ + + /*! \brief Initialization-time Feature Enabling + * + * Certain codec features must be known at initialization time, to allow for + * proper memory allocation. + * + * The available flags are specified by VPX_CODEC_USE_* defines. + */ +#define VPX_CODEC_CAP_FRAME_THREADING 0x200000 /**< Can support frame-based + multi-threading */ +#define VPX_CODEC_CAP_EXTERNAL_FRAME_BUFFER 0x400000 /**< Can support external + frame buffers */ + +#define VPX_CODEC_USE_POSTPROC 0x10000 /**< Postprocess decoded frame */ +#define VPX_CODEC_USE_ERROR_CONCEALMENT 0x20000 /**< Conceal errors in decoded + frames */ +#define VPX_CODEC_USE_INPUT_FRAGMENTS 0x40000 /**< The input frame should be + passed to the decoder one + fragment at a time */ +#define VPX_CODEC_USE_FRAME_THREADING 0x80000 /**< Enable frame-based + multi-threading */ + + /*!\brief Stream properties + * + * This structure is used to query or set properties of the decoded + * stream. Algorithms may extend this structure with data specific + * to their bitstream by setting the sz member appropriately. + */ + typedef struct vpx_codec_stream_info { + unsigned int sz; /**< Size of this structure */ + unsigned int w; /**< Width (or 0 for unknown/default) */ + unsigned int h; /**< Height (or 0 for unknown/default) */ + unsigned int is_kf; /**< Current frame is a keyframe */ + } vpx_codec_stream_info_t; + + /* REQUIRED FUNCTIONS + * + * The following functions are required to be implemented for all decoders. + * They represent the base case functionality expected of all decoders. + */ + + + /*!\brief Initialization Configurations + * + * This structure is used to pass init time configuration options to the + * decoder. + */ + typedef struct vpx_codec_dec_cfg { + unsigned int threads; /**< Maximum number of threads to use, default 1 */ + unsigned int w; /**< Width */ + unsigned int h; /**< Height */ + } vpx_codec_dec_cfg_t; /**< alias for struct vpx_codec_dec_cfg */ + + + /*!\brief Initialize a decoder instance + * + * Initializes a decoder context using the given interface. Applications + * should call the vpx_codec_dec_init convenience macro instead of this + * function directly, to ensure that the ABI version number parameter + * is properly initialized. + * + * If the library was configured with --disable-multithread, this call + * is not thread safe and should be guarded with a lock if being used + * in a multithreaded context. + * + * \param[in] ctx Pointer to this instance's context. + * \param[in] iface Pointer to the algorithm interface to use. + * \param[in] cfg Configuration to use, if known. May be NULL. + * \param[in] flags Bitfield of VPX_CODEC_USE_* flags + * \param[in] ver ABI version number. Must be set to + * VPX_DECODER_ABI_VERSION + * \retval #VPX_CODEC_OK + * The decoder algorithm initialized. + * \retval #VPX_CODEC_MEM_ERROR + * Memory allocation failed. + */ + vpx_codec_err_t vpx_codec_dec_init_ver(vpx_codec_ctx_t *ctx, + vpx_codec_iface_t *iface, + const vpx_codec_dec_cfg_t *cfg, + vpx_codec_flags_t flags, + int ver); + + /*!\brief Convenience macro for vpx_codec_dec_init_ver() + * + * Ensures the ABI version parameter is properly set. + */ +#define vpx_codec_dec_init(ctx, iface, cfg, flags) \ + vpx_codec_dec_init_ver(ctx, iface, cfg, flags, VPX_DECODER_ABI_VERSION) + + + /*!\brief Parse stream info from a buffer + * + * Performs high level parsing of the bitstream. Construction of a decoder + * context is not necessary. Can be used to determine if the bitstream is + * of the proper format, and to extract information from the stream. + * + * \param[in] iface Pointer to the algorithm interface + * \param[in] data Pointer to a block of data to parse + * \param[in] data_sz Size of the data buffer + * \param[in,out] si Pointer to stream info to update. The size member + * \ref MUST be properly initialized, but \ref MAY be + * clobbered by the algorithm. This parameter \ref MAY + * be NULL. + * + * \retval #VPX_CODEC_OK + * Bitstream is parsable and stream information updated + */ + vpx_codec_err_t vpx_codec_peek_stream_info(vpx_codec_iface_t *iface, + const uint8_t *data, + unsigned int data_sz, + vpx_codec_stream_info_t *si); + + + /*!\brief Return information about the current stream. + * + * Returns information about the stream that has been parsed during decoding. + * + * \param[in] ctx Pointer to this instance's context + * \param[in,out] si Pointer to stream info to update. The size member + * \ref MUST be properly initialized, but \ref MAY be + * clobbered by the algorithm. This parameter \ref MAY + * be NULL. + * + * \retval #VPX_CODEC_OK + * Bitstream is parsable and stream information updated + */ + vpx_codec_err_t vpx_codec_get_stream_info(vpx_codec_ctx_t *ctx, + vpx_codec_stream_info_t *si); + + + /*!\brief Decode data + * + * Processes a buffer of coded data. If the processing results in a new + * decoded frame becoming available, PUT_SLICE and PUT_FRAME events may be + * generated, as appropriate. Encoded data \ref MUST be passed in DTS (decode + * time stamp) order. Frames produced will always be in PTS (presentation + * time stamp) order. + * If the decoder is configured with VPX_CODEC_USE_INPUT_FRAGMENTS enabled, + * data and data_sz can contain a fragment of the encoded frame. Fragment + * \#n must contain at least partition \#n, but can also contain subsequent + * partitions (\#n+1 - \#n+i), and if so, fragments \#n+1, .., \#n+i must + * be empty. When no more data is available, this function should be called + * with NULL as data and 0 as data_sz. The memory passed to this function + * must be available until the frame has been decoded. + * + * \param[in] ctx Pointer to this instance's context + * \param[in] data Pointer to this block of new coded data. If + * NULL, a VPX_CODEC_CB_PUT_FRAME event is posted + * for the previously decoded frame. + * \param[in] data_sz Size of the coded data, in bytes. + * \param[in] user_priv Application specific data to associate with + * this frame. + * \param[in] deadline Soft deadline the decoder should attempt to meet, + * in us. Set to zero for unlimited. + * + * \return Returns #VPX_CODEC_OK if the coded data was processed completely + * and future pictures can be decoded without error. Otherwise, + * see the descriptions of the other error codes in ::vpx_codec_err_t + * for recoverability capabilities. + */ + vpx_codec_err_t vpx_codec_decode(vpx_codec_ctx_t *ctx, + const uint8_t *data, + unsigned int data_sz, + void *user_priv, + long deadline); + + + /*!\brief Decoded frames iterator + * + * Iterates over a list of the frames available for display. The iterator + * storage should be initialized to NULL to start the iteration. Iteration is + * complete when this function returns NULL. + * + * The list of available frames becomes valid upon completion of the + * vpx_codec_decode call, and remains valid until the next call to vpx_codec_decode. + * + * \param[in] ctx Pointer to this instance's context + * \param[in,out] iter Iterator storage, initialized to NULL + * + * \return Returns a pointer to an image, if one is ready for display. Frames + * produced will always be in PTS (presentation time stamp) order. + */ + vpx_image_t *vpx_codec_get_frame(vpx_codec_ctx_t *ctx, + vpx_codec_iter_t *iter); + + + /*!\defgroup cap_put_frame Frame-Based Decoding Functions + * + * The following functions are required to be implemented for all decoders + * that advertise the VPX_CODEC_CAP_PUT_FRAME capability. Calling these functions + * for codecs that don't advertise this capability will result in an error + * code being returned, usually VPX_CODEC_ERROR + * @{ + */ + + /*!\brief put frame callback prototype + * + * This callback is invoked by the decoder to notify the application of + * the availability of decoded image data. + */ + typedef void (*vpx_codec_put_frame_cb_fn_t)(void *user_priv, + const vpx_image_t *img); + + + /*!\brief Register for notification of frame completion. + * + * Registers a given function to be called when a decoded frame is + * available. + * + * \param[in] ctx Pointer to this instance's context + * \param[in] cb Pointer to the callback function + * \param[in] user_priv User's private data + * + * \retval #VPX_CODEC_OK + * Callback successfully registered. + * \retval #VPX_CODEC_ERROR + * Decoder context not initialized, or algorithm not capable of + * posting slice completion. + */ + vpx_codec_err_t vpx_codec_register_put_frame_cb(vpx_codec_ctx_t *ctx, + vpx_codec_put_frame_cb_fn_t cb, + void *user_priv); + + + /*!@} - end defgroup cap_put_frame */ + + /*!\defgroup cap_put_slice Slice-Based Decoding Functions + * + * The following functions are required to be implemented for all decoders + * that advertise the VPX_CODEC_CAP_PUT_SLICE capability. Calling these functions + * for codecs that don't advertise this capability will result in an error + * code being returned, usually VPX_CODEC_ERROR + * @{ + */ + + /*!\brief put slice callback prototype + * + * This callback is invoked by the decoder to notify the application of + * the availability of partially decoded image data. The + */ + typedef void (*vpx_codec_put_slice_cb_fn_t)(void *user_priv, + const vpx_image_t *img, + const vpx_image_rect_t *valid, + const vpx_image_rect_t *update); + + + /*!\brief Register for notification of slice completion. + * + * Registers a given function to be called when a decoded slice is + * available. + * + * \param[in] ctx Pointer to this instance's context + * \param[in] cb Pointer to the callback function + * \param[in] user_priv User's private data + * + * \retval #VPX_CODEC_OK + * Callback successfully registered. + * \retval #VPX_CODEC_ERROR + * Decoder context not initialized, or algorithm not capable of + * posting slice completion. + */ + vpx_codec_err_t vpx_codec_register_put_slice_cb(vpx_codec_ctx_t *ctx, + vpx_codec_put_slice_cb_fn_t cb, + void *user_priv); + + + /*!@} - end defgroup cap_put_slice*/ + + /*!\defgroup cap_external_frame_buffer External Frame Buffer Functions + * + * The following section is required to be implemented for all decoders + * that advertise the VPX_CODEC_CAP_EXTERNAL_FRAME_BUFFER capability. + * Calling this function for codecs that don't advertise this capability + * will result in an error code being returned, usually VPX_CODEC_ERROR. + * + * \note + * Currently this only works with VP9. + * @{ + */ + + /*!\brief Pass in external frame buffers for the decoder to use. + * + * Registers functions to be called when libvpx needs a frame buffer + * to decode the current frame and a function to be called when libvpx does + * not internally reference the frame buffer. This set function must + * be called before the first call to decode or libvpx will assume the + * default behavior of allocating frame buffers internally. + * + * \param[in] ctx Pointer to this instance's context + * \param[in] cb_get Pointer to the get callback function + * \param[in] cb_release Pointer to the release callback function + * \param[in] cb_priv Callback's private data + * + * \retval #VPX_CODEC_OK + * External frame buffers will be used by libvpx. + * \retval #VPX_CODEC_INVALID_PARAM + * One or more of the callbacks were NULL. + * \retval #VPX_CODEC_ERROR + * Decoder context not initialized, or algorithm not capable of + * using external frame buffers. + * + * \note + * When decoding VP9, the application may be required to pass in at least + * #VP9_MAXIMUM_REF_BUFFERS + #VPX_MAXIMUM_WORK_BUFFERS external frame + * buffers. + */ + vpx_codec_err_t vpx_codec_set_frame_buffer_functions( + vpx_codec_ctx_t *ctx, + vpx_get_frame_buffer_cb_fn_t cb_get, + vpx_release_frame_buffer_cb_fn_t cb_release, void *cb_priv); + + /*!@} - end defgroup cap_external_frame_buffer */ + + /*!@} - end defgroup decoder*/ +#ifdef __cplusplus +} +#endif +#endif // VPX_VPX_DECODER_H_ + diff --git a/bin/windows/vpx/include/vpx/vpx_frame_buffer.h b/bin/windows/vpx/include/vpx/vpx_frame_buffer.h new file mode 100644 index 0000000000..9036459af0 --- /dev/null +++ b/bin/windows/vpx/include/vpx/vpx_frame_buffer.h @@ -0,0 +1,83 @@ +/* + * Copyright (c) 2014 The WebM project authors. All Rights Reserved. + * + * Use of this source code is governed by a BSD-style license + * that can be found in the LICENSE file in the root of the source + * tree. An additional intellectual property rights grant can be found + * in the file PATENTS. All contributing project authors may + * be found in the AUTHORS file in the root of the source tree. + */ + +#ifndef VPX_VPX_FRAME_BUFFER_H_ +#define VPX_VPX_FRAME_BUFFER_H_ + +/*!\file + * \brief Describes the decoder external frame buffer interface. + */ + +#ifdef __cplusplus +extern "C" { +#endif + +#include "./vpx_integer.h" + +/*!\brief The maximum number of work buffers used by libvpx. + * Support maximum 4 threads to decode video in parallel. + * Each thread will use one work buffer. + * TODO(hkuang): Add support to set number of worker threads dynamically. + */ +#define VPX_MAXIMUM_WORK_BUFFERS 8 + +/*!\brief The maximum number of reference buffers that a VP9 encoder may use. + */ +#define VP9_MAXIMUM_REF_BUFFERS 8 + +/*!\brief External frame buffer + * + * This structure holds allocated frame buffers used by the decoder. + */ +typedef struct vpx_codec_frame_buffer { + uint8_t *data; /**< Pointer to the data buffer */ + size_t size; /**< Size of data in bytes */ + void *priv; /**< Frame's private data */ +} vpx_codec_frame_buffer_t; + +/*!\brief get frame buffer callback prototype + * + * This callback is invoked by the decoder to retrieve data for the frame + * buffer in order for the decode call to complete. The callback must + * allocate at least min_size in bytes and assign it to fb->data. The callback + * must zero out all the data allocated. Then the callback must set fb->size + * to the allocated size. The application does not need to align the allocated + * data. The callback is triggered when the decoder needs a frame buffer to + * decode a compressed image into. This function may be called more than once + * for every call to vpx_codec_decode. The application may set fb->priv to + * some data which will be passed back in the ximage and the release function + * call. |fb| is guaranteed to not be NULL. On success the callback must + * return 0. Any failure the callback must return a value less than 0. + * + * \param[in] priv Callback's private data + * \param[in] new_size Size in bytes needed by the buffer + * \param[in,out] fb Pointer to vpx_codec_frame_buffer_t + */ +typedef int (*vpx_get_frame_buffer_cb_fn_t)( + void *priv, size_t min_size, vpx_codec_frame_buffer_t *fb); + +/*!\brief release frame buffer callback prototype + * + * This callback is invoked by the decoder when the frame buffer is not + * referenced by any other buffers. |fb| is guaranteed to not be NULL. On + * success the callback must return 0. Any failure the callback must return + * a value less than 0. + * + * \param[in] priv Callback's private data + * \param[in] fb Pointer to vpx_codec_frame_buffer_t + */ +typedef int (*vpx_release_frame_buffer_cb_fn_t)( + void *priv, vpx_codec_frame_buffer_t *fb); + +#ifdef __cplusplus +} // extern "C" +#endif + +#endif // VPX_VPX_FRAME_BUFFER_H_ diff --git a/bin/windows/vpx/include/vpx/vpx_image.h b/bin/windows/vpx/include/vpx/vpx_image.h new file mode 100644 index 0000000000..7958c69806 --- /dev/null +++ b/bin/windows/vpx/include/vpx/vpx_image.h @@ -0,0 +1,235 @@ +/* + * Copyright (c) 2010 The WebM project authors. All Rights Reserved. + * + * Use of this source code is governed by a BSD-style license + * that can be found in the LICENSE file in the root of the source + * tree. An additional intellectual property rights grant can be found + * in the file PATENTS. All contributing project authors may + * be found in the AUTHORS file in the root of the source tree. + */ + + +/*!\file + * \brief Describes the vpx image descriptor and associated operations + * + */ +#ifndef VPX_VPX_IMAGE_H_ +#define VPX_VPX_IMAGE_H_ + +#ifdef __cplusplus +extern "C" { +#endif + + /*!\brief Current ABI version number + * + * \internal + * If this file is altered in any way that changes the ABI, this value + * must be bumped. Examples include, but are not limited to, changing + * types, removing or reassigning enums, adding/removing/rearranging + * fields to structures + */ +#define VPX_IMAGE_ABI_VERSION (4) /**<\hideinitializer*/ + + +#define VPX_IMG_FMT_PLANAR 0x100 /**< Image is a planar format. */ +#define VPX_IMG_FMT_UV_FLIP 0x200 /**< V plane precedes U in memory. */ +#define VPX_IMG_FMT_HAS_ALPHA 0x400 /**< Image has an alpha channel. */ +#define VPX_IMG_FMT_HIGHBITDEPTH 0x800 /**< Image uses 16bit framebuffer. */ + + /*!\brief List of supported image formats */ + typedef enum vpx_img_fmt { + VPX_IMG_FMT_NONE, + VPX_IMG_FMT_RGB24, /**< 24 bit per pixel packed RGB */ + VPX_IMG_FMT_RGB32, /**< 32 bit per pixel packed 0RGB */ + VPX_IMG_FMT_RGB565, /**< 16 bit per pixel, 565 */ + VPX_IMG_FMT_RGB555, /**< 16 bit per pixel, 555 */ + VPX_IMG_FMT_UYVY, /**< UYVY packed YUV */ + VPX_IMG_FMT_YUY2, /**< YUYV packed YUV */ + VPX_IMG_FMT_YVYU, /**< YVYU packed YUV */ + VPX_IMG_FMT_BGR24, /**< 24 bit per pixel packed BGR */ + VPX_IMG_FMT_RGB32_LE, /**< 32 bit packed BGR0 */ + VPX_IMG_FMT_ARGB, /**< 32 bit packed ARGB, alpha=255 */ + VPX_IMG_FMT_ARGB_LE, /**< 32 bit packed BGRA, alpha=255 */ + VPX_IMG_FMT_RGB565_LE, /**< 16 bit per pixel, gggbbbbb rrrrrggg */ + VPX_IMG_FMT_RGB555_LE, /**< 16 bit per pixel, gggbbbbb 0rrrrrgg */ + VPX_IMG_FMT_YV12 = VPX_IMG_FMT_PLANAR | VPX_IMG_FMT_UV_FLIP | 1, /**< planar YVU */ + VPX_IMG_FMT_I420 = VPX_IMG_FMT_PLANAR | 2, + VPX_IMG_FMT_VPXYV12 = VPX_IMG_FMT_PLANAR | VPX_IMG_FMT_UV_FLIP | 3, /** < planar 4:2:0 format with vpx color space */ + VPX_IMG_FMT_VPXI420 = VPX_IMG_FMT_PLANAR | 4, + VPX_IMG_FMT_I422 = VPX_IMG_FMT_PLANAR | 5, + VPX_IMG_FMT_I444 = VPX_IMG_FMT_PLANAR | 6, + VPX_IMG_FMT_I440 = VPX_IMG_FMT_PLANAR | 7, + VPX_IMG_FMT_444A = VPX_IMG_FMT_PLANAR | VPX_IMG_FMT_HAS_ALPHA | 6, + VPX_IMG_FMT_I42016 = VPX_IMG_FMT_I420 | VPX_IMG_FMT_HIGHBITDEPTH, + VPX_IMG_FMT_I42216 = VPX_IMG_FMT_I422 | VPX_IMG_FMT_HIGHBITDEPTH, + VPX_IMG_FMT_I44416 = VPX_IMG_FMT_I444 | VPX_IMG_FMT_HIGHBITDEPTH, + VPX_IMG_FMT_I44016 = VPX_IMG_FMT_I440 | VPX_IMG_FMT_HIGHBITDEPTH + } vpx_img_fmt_t; /**< alias for enum vpx_img_fmt */ + + /*!\brief List of supported color spaces */ + typedef enum vpx_color_space { + VPX_CS_UNKNOWN = 0, /**< Unknown */ + VPX_CS_BT_601 = 1, /**< BT.601 */ + VPX_CS_BT_709 = 2, /**< BT.709 */ + VPX_CS_SMPTE_170 = 3, /**< SMPTE.170 */ + VPX_CS_SMPTE_240 = 4, /**< SMPTE.240 */ + VPX_CS_BT_2020 = 5, /**< BT.2020 */ + VPX_CS_RESERVED = 6, /**< Reserved */ + VPX_CS_SRGB = 7 /**< sRGB */ + } vpx_color_space_t; /**< alias for enum vpx_color_space */ + + /*!\brief List of supported color range */ + typedef enum vpx_color_range { + VPX_CR_STUDIO_RANGE = 0, /**< Y [16..235], UV [16..240] */ + VPX_CR_FULL_RANGE = 1 /**< YUV/RGB [0..255] */ + } vpx_color_range_t; /**< alias for enum vpx_color_range */ + + /**\brief Image Descriptor */ + typedef struct vpx_image { + vpx_img_fmt_t fmt; /**< Image Format */ + vpx_color_space_t cs; /**< Color Space */ + vpx_color_range_t range; /**< Color Range */ + + /* Image storage dimensions */ + unsigned int w; /**< Stored image width */ + unsigned int h; /**< Stored image height */ + unsigned int bit_depth; /**< Stored image bit-depth */ + + /* Image display dimensions */ + unsigned int d_w; /**< Displayed image width */ + unsigned int d_h; /**< Displayed image height */ + + /* Image intended rendering dimensions */ + unsigned int r_w; /**< Intended rendering image width */ + unsigned int r_h; /**< Intended rendering image height */ + + /* Chroma subsampling info */ + unsigned int x_chroma_shift; /**< subsampling order, X */ + unsigned int y_chroma_shift; /**< subsampling order, Y */ + + /* Image data pointers. */ +#define VPX_PLANE_PACKED 0 /**< To be used for all packed formats */ +#define VPX_PLANE_Y 0 /**< Y (Luminance) plane */ +#define VPX_PLANE_U 1 /**< U (Chroma) plane */ +#define VPX_PLANE_V 2 /**< V (Chroma) plane */ +#define VPX_PLANE_ALPHA 3 /**< A (Transparency) plane */ + unsigned char *planes[4]; /**< pointer to the top left pixel for each plane */ + int stride[4]; /**< stride between rows for each plane */ + + int bps; /**< bits per sample (for packed formats) */ + + /* The following member may be set by the application to associate data + * with this image. + */ + void *user_priv; /**< may be set by the application to associate data + * with this image. */ + + /* The following members should be treated as private. */ + unsigned char *img_data; /**< private */ + int img_data_owner; /**< private */ + int self_allocd; /**< private */ + + void *fb_priv; /**< Frame buffer data associated with the image. */ + } vpx_image_t; /**< alias for struct vpx_image */ + + /**\brief Representation of a rectangle on a surface */ + typedef struct vpx_image_rect { + unsigned int x; /**< leftmost column */ + unsigned int y; /**< topmost row */ + unsigned int w; /**< width */ + unsigned int h; /**< height */ + } vpx_image_rect_t; /**< alias for struct vpx_image_rect */ + + /*!\brief Open a descriptor, allocating storage for the underlying image + * + * Returns a descriptor for storing an image of the given format. The + * storage for the descriptor is allocated on the heap. + * + * \param[in] img Pointer to storage for descriptor. If this parameter + * is NULL, the storage for the descriptor will be + * allocated on the heap. + * \param[in] fmt Format for the image + * \param[in] d_w Width of the image + * \param[in] d_h Height of the image + * \param[in] align Alignment, in bytes, of the image buffer and + * each row in the image(stride). + * + * \return Returns a pointer to the initialized image descriptor. If the img + * parameter is non-null, the value of the img parameter will be + * returned. + */ + vpx_image_t *vpx_img_alloc(vpx_image_t *img, + vpx_img_fmt_t fmt, + unsigned int d_w, + unsigned int d_h, + unsigned int align); + + /*!\brief Open a descriptor, using existing storage for the underlying image + * + * Returns a descriptor for storing an image of the given format. The + * storage for descriptor has been allocated elsewhere, and a descriptor is + * desired to "wrap" that storage. + * + * \param[in] img Pointer to storage for descriptor. If this parameter + * is NULL, the storage for the descriptor will be + * allocated on the heap. + * \param[in] fmt Format for the image + * \param[in] d_w Width of the image + * \param[in] d_h Height of the image + * \param[in] align Alignment, in bytes, of each row in the image. + * \param[in] img_data Storage to use for the image + * + * \return Returns a pointer to the initialized image descriptor. If the img + * parameter is non-null, the value of the img parameter will be + * returned. + */ + vpx_image_t *vpx_img_wrap(vpx_image_t *img, + vpx_img_fmt_t fmt, + unsigned int d_w, + unsigned int d_h, + unsigned int align, + unsigned char *img_data); + + + /*!\brief Set the rectangle identifying the displayed portion of the image + * + * Updates the displayed rectangle (aka viewport) on the image surface to + * match the specified coordinates and size. + * + * \param[in] img Image descriptor + * \param[in] x leftmost column + * \param[in] y topmost row + * \param[in] w width + * \param[in] h height + * + * \return 0 if the requested rectangle is valid, nonzero otherwise. + */ + int vpx_img_set_rect(vpx_image_t *img, + unsigned int x, + unsigned int y, + unsigned int w, + unsigned int h); + + + /*!\brief Flip the image vertically (top for bottom) + * + * Adjusts the image descriptor's pointers and strides to make the image + * be referenced upside-down. + * + * \param[in] img Image descriptor + */ + void vpx_img_flip(vpx_image_t *img); + + /*!\brief Close an image descriptor + * + * Frees all allocated storage associated with an image descriptor. + * + * \param[in] img Image descriptor + */ + void vpx_img_free(vpx_image_t *img); + +#ifdef __cplusplus +} // extern "C" +#endif + +#endif // VPX_VPX_IMAGE_H_ diff --git a/bin/windows/vpx/include/vpx/vpx_integer.h b/bin/windows/vpx/include/vpx/vpx_integer.h new file mode 100644 index 0000000000..829c9d132c --- /dev/null +++ b/bin/windows/vpx/include/vpx/vpx_integer.h @@ -0,0 +1,74 @@ +/* + * Copyright (c) 2010 The WebM project authors. All Rights Reserved. + * + * Use of this source code is governed by a BSD-style license + * that can be found in the LICENSE file in the root of the source + * tree. An additional intellectual property rights grant can be found + * in the file PATENTS. All contributing project authors may + * be found in the AUTHORS file in the root of the source tree. + */ + + +#ifndef VPX_VPX_INTEGER_H_ +#define VPX_VPX_INTEGER_H_ + +/* get ptrdiff_t, size_t, wchar_t, NULL */ +#include + +#if defined(_MSC_VER) +#define VPX_FORCE_INLINE __forceinline +#define VPX_INLINE __inline +#else +#define VPX_FORCE_INLINE __inline__ __attribute__(always_inline) +// TODO(jbb): Allow a way to force inline off for older compilers. +#define VPX_INLINE inline +#endif + +#if (defined(_MSC_VER) && (_MSC_VER < 1600)) || defined(VPX_EMULATE_INTTYPES) +typedef signed char int8_t; +typedef signed short int16_t; +typedef signed int int32_t; + +typedef unsigned char uint8_t; +typedef unsigned short uint16_t; +typedef unsigned int uint32_t; + +#if (defined(_MSC_VER) && (_MSC_VER < 1600)) +typedef signed __int64 int64_t; +typedef unsigned __int64 uint64_t; +#define INT64_MAX _I64_MAX +#define INT32_MAX _I32_MAX +#define INT32_MIN _I32_MIN +#define INT16_MAX _I16_MAX +#define INT16_MIN _I16_MIN +#endif + +#ifndef _UINTPTR_T_DEFINED +typedef size_t uintptr_t; +#endif + +#else + +/* Most platforms have the C99 standard integer types. */ + +#if defined(__cplusplus) +# if !defined(__STDC_FORMAT_MACROS) +# define __STDC_FORMAT_MACROS +# endif +# if !defined(__STDC_LIMIT_MACROS) +# define __STDC_LIMIT_MACROS +# endif +#endif // __cplusplus + +#include + +#endif + +/* VS2010 defines stdint.h, but not inttypes.h */ +#if defined(_MSC_VER) && _MSC_VER < 1800 +#define PRId64 "I64d" +#else +#include +#endif + +#endif // VPX_VPX_INTEGER_H_ diff --git a/bin/windows/vpx/lib/32/libcompat-to-msvc.lib b/bin/windows/vpx/lib/32/libcompat-to-msvc.lib new file mode 100644 index 0000000000..6ec8b870e2 Binary files /dev/null and b/bin/windows/vpx/lib/32/libcompat-to-msvc.lib differ diff --git a/bin/windows/vpx/lib/32/libvpx.lib b/bin/windows/vpx/lib/32/libvpx.lib new file mode 100644 index 0000000000..1a9753e32c Binary files /dev/null and b/bin/windows/vpx/lib/32/libvpx.lib differ diff --git a/bin/windows/vpx/lib/64/libcompat-to-msvc.lib b/bin/windows/vpx/lib/64/libcompat-to-msvc.lib new file mode 100644 index 0000000000..5428547cca Binary files /dev/null and b/bin/windows/vpx/lib/64/libcompat-to-msvc.lib differ diff --git a/bin/windows/vpx/lib/64/libvpx.lib b/bin/windows/vpx/lib/64/libvpx.lib new file mode 100644 index 0000000000..3b552c027d Binary files /dev/null and b/bin/windows/vpx/lib/64/libvpx.lib differ diff --git a/bin/windows/vpx/lib/instructions.txt b/bin/windows/vpx/lib/instructions.txt new file mode 100644 index 0000000000..a028c75957 --- /dev/null +++ b/bin/windows/vpx/lib/instructions.txt @@ -0,0 +1,44 @@ +Instructions for Building EDuke32's Library Dependencies Targeting Win32 and Win64 +================================================================================== + +First, follow these instructions: http://wiki.eduke32.com/wiki/Building_EDuke32_on_Windows + +Download the latest sources from the link provided. + +The build output listed as "Desired Results" is what EDuke32 needs to function. + +The desired results for each library in some cases may need to be installed to the compiler. "x depends on the results of y to compile" means that the build output of x must be added to the compiler in this way. Copy files listed in each category to the appropriate destinations. Unless otherwise noted, do NOT copy the ".dll.a" file or else the final product may depend on external DLLs (which you may actually want). + +For MinGW (MinGW32): +from the compiler root (ex. C:/MinGW/) + * headers: include/ + * libraries: lib/ + +For MinGW-w64: +from the compiler root (ex. C:/MinGW-w64/mingw32-dw2/) + * headers: -w64-mingw32/include/ + * libraries: -w64-mingw32/lib/ + +Binaries (if mentioned) need to be present with the finished EDuke32 executables. They are not needed during compilation. + +NB: Text formatted as code blocks are commands to be pasted into the Windows command prompt. +http://wiki.eduke32.com/wiki/Working_with_the_Windows_Command_Prompt + +[//]: # (Plain text readers: This refers to lines beginning with exactly four spaces.) + + +libvpx +------ +### Prerequisites ### +Download the binary of yasm (http://yasm.tortall.net/) for your host system architecture. Both builds target both architectures. +The build environment needs pr.exe (https://mingw-lib.googlecode.com/files/coreutils-5.97-MSYS-1.0.11-snapshot.tar.bz2). +### Download ### + * Instructions: http://www.webmproject.org/code/ + * Stable: http://downloads.webmproject.org/releases/webm/ + * Git: https://chromium.googlesource.com/webm/libvpx +### Build ### + sh ./configure --disable-vp8-encoder --disable-vp9-encoder --disable-multithread --disable-spatial-resampling --as=yasm && make libvpx.a +### Desired Results ### + * headers: vpx/vp8.h vpx/vp8dx.h vpx/vpx_codec.h vpx/vpx_decoder.h vpx/vpx_frame_buffer.h vpx/vpx_image.h vpx/vpx_integer.h + * libraries: libvpx.a + diff --git a/bin/windows/vpx/src/_dbg_LOAD_IMAGE.h b/bin/windows/vpx/src/_dbg_LOAD_IMAGE.h new file mode 100644 index 0000000000..0568f30168 --- /dev/null +++ b/bin/windows/vpx/src/_dbg_LOAD_IMAGE.h @@ -0,0 +1,55 @@ +/** + * This file has no copyright assigned and is placed in the Public Domain. + * This file is part of the w64 mingw-runtime package. + * No warranty is given; refer to the file DISCLAIMER.PD within this package. + */ +#ifndef _dbg_LOAD_IMAGE_h +#define _dbg_LOAD_IMAGE_h + +#ifndef WINAPI +#define WINAPI __stdcall +#endif + +#define IMAGEAPI DECLSPEC_IMPORT WINAPI +#define DBHLP_DEPRECIATED __declspec(deprecated) + +#define DBHLPAPI IMAGEAPI + +#ifndef EBACKTRACE_MINGW32 + +#define IMAGE_SEPARATION (64*1024) + + typedef struct _LOADED_IMAGE { + PSTR ModuleName; + HANDLE hFile; + PUCHAR MappedAddress; +#ifdef _IMAGEHLP64 + PIMAGE_NT_HEADERS64 FileHeader; +#else + PIMAGE_NT_HEADERS32 FileHeader; +#endif + PIMAGE_SECTION_HEADER LastRvaSection; + ULONG NumberOfSections; + PIMAGE_SECTION_HEADER Sections; + ULONG Characteristics; + BOOLEAN fSystemImage; + BOOLEAN fDOSImage; + BOOLEAN fReadOnly; + UCHAR Version; + LIST_ENTRY Links; + ULONG SizeOfImage; + } LOADED_IMAGE,*PLOADED_IMAGE; + +#endif + +#define MAX_SYM_NAME 2000 + + typedef struct _MODLOAD_DATA { + DWORD ssize; + DWORD ssig; + PVOID data; + DWORD size; + DWORD flags; + } MODLOAD_DATA,*PMODLOAD_DATA; + +#endif diff --git a/bin/windows/vpx/src/_dbg_common.h b/bin/windows/vpx/src/_dbg_common.h new file mode 100644 index 0000000000..2c412e4cca --- /dev/null +++ b/bin/windows/vpx/src/_dbg_common.h @@ -0,0 +1,2051 @@ +/** + * This file has no copyright assigned and is placed in the Public Domain. + * This file is part of the w64 mingw-runtime package. + * No warranty is given; refer to the file DISCLAIMER.PD within this package. + */ +#ifndef _dbg_common_h +#define _dbg_common_h + +// from _mingw_mac.h +#ifndef __MINGW_EXTENSION +#if defined(__GNUC__) || defined(__GNUG__) +#define __MINGW_EXTENSION __extension__ +#else +#define __MINGW_EXTENSION +#endif +#endif + +/* Special case nameless struct/union. */ +#ifndef __C89_NAMELESS +#define __C89_NAMELESS __MINGW_EXTENSION + +#define __C89_NAMELESSSTRUCTNAME +#define __C89_NAMELESSUNIONNAME +#endif + +#include "_dbg_LOAD_IMAGE.h" + +// from winnt.h +#if defined(UNICODE) + typedef LPWSTR LPTCH,PTCH; + typedef LPWSTR PTSTR,LPTSTR; + typedef LPCWSTR PCTSTR,LPCTSTR; + typedef LPUWSTR PUTSTR,LPUTSTR; + typedef LPCUWSTR PCUTSTR,LPCUTSTR; + typedef LPWSTR LP; +#else + typedef LPSTR LPTCH,PTCH; + typedef LPSTR PTSTR,LPTSTR,PUTSTR,LPUTSTR; + typedef LPCSTR PCTSTR,LPCTSTR,PCUTSTR,LPCUTSTR; +#endif + +#ifdef __cplusplus +extern "C" { +#endif + + typedef WINBOOL (CALLBACK *PFIND_DEBUG_FILE_CALLBACK)(HANDLE FileHandle,PCSTR FileName,PVOID CallerData); + typedef WINBOOL (CALLBACK *PFIND_DEBUG_FILE_CALLBACKW)(HANDLE FileHandle,PCWSTR FileName,PVOID CallerData); + typedef WINBOOL (CALLBACK *PFINDFILEINPATHCALLBACK)(PCSTR filename,PVOID context); + typedef WINBOOL (CALLBACK *PFINDFILEINPATHCALLBACKW)(PCWSTR filename,PVOID context); + typedef WINBOOL (CALLBACK *PFIND_EXE_FILE_CALLBACK)(HANDLE FileHandle,PCSTR FileName,PVOID CallerData); + typedef WINBOOL (CALLBACK *PFIND_EXE_FILE_CALLBACKW)(HANDLE FileHandle,PCWSTR FileName,PVOID CallerData); + + typedef WINBOOL (WINAPI *PSYMBOLSERVERPROC)(LPCSTR,LPCSTR,PVOID,DWORD,DWORD,LPSTR); + typedef WINBOOL (WINAPI *PSYMBOLSERVEROPENPROC)(VOID); + typedef WINBOOL (WINAPI *PSYMBOLSERVERCLOSEPROC)(VOID); + typedef WINBOOL (WINAPI *PSYMBOLSERVERSETOPTIONSPROC)(UINT_PTR,ULONG64); + typedef WINBOOL (CALLBACK WINAPI *PSYMBOLSERVERCALLBACKPROC)(UINT_PTR action,ULONG64 data,ULONG64 context); + typedef UINT_PTR (WINAPI *PSYMBOLSERVERGETOPTIONSPROC)(); + typedef WINBOOL (WINAPI *PSYMBOLSERVERPINGPROC)(LPCSTR); + +#ifndef EBACKTRACE_MINGW32 + HANDLE IMAGEAPI FindDebugInfoFile(PCSTR FileName,PCSTR SymbolPath,PSTR DebugFilePath); +#endif + HANDLE IMAGEAPI FindDebugInfoFileEx(PCSTR FileName,PCSTR SymbolPath,PSTR DebugFilePath,PFIND_DEBUG_FILE_CALLBACK Callback,PVOID CallerData); + HANDLE IMAGEAPI FindDebugInfoFileExW(PCWSTR FileName,PCWSTR SymbolPath,PWSTR DebugFilePath,PFIND_DEBUG_FILE_CALLBACKW Callback,PVOID CallerData); + WINBOOL IMAGEAPI SymFindFileInPath(HANDLE hprocess,PCSTR SearchPath,PCSTR FileName,PVOID id,DWORD two,DWORD three,DWORD flags,LPSTR FoundFile,PFINDFILEINPATHCALLBACK callback,PVOID context); + WINBOOL IMAGEAPI SymFindFileInPathW(HANDLE hprocess,PCWSTR SearchPath,PCWSTR FileName,PVOID id,DWORD two,DWORD three,DWORD flags,LPSTR FoundFile,PFINDFILEINPATHCALLBACKW callback,PVOID context); +#ifndef EBACKTRACE_MINGW32 + HANDLE IMAGEAPI FindExecutableImage(PCSTR FileName,PCSTR SymbolPath,PSTR ImageFilePath); +#endif + HANDLE IMAGEAPI FindExecutableImageEx(PCSTR FileName,PCSTR SymbolPath,PSTR ImageFilePath,PFIND_EXE_FILE_CALLBACK Callback,PVOID CallerData); + HANDLE IMAGEAPI FindExecutableImageExW(PCWSTR FileName,PCWSTR SymbolPath,PWSTR ImageFilePath,PFIND_EXE_FILE_CALLBACKW Callback,PVOID CallerData); + PIMAGE_NT_HEADERS IMAGEAPI ImageNtHeader(PVOID Base); + PVOID IMAGEAPI ImageDirectoryEntryToDataEx(PVOID Base,BOOLEAN MappedAsImage,USHORT DirectoryEntry,PULONG Size,PIMAGE_SECTION_HEADER *FoundHeader); + PVOID IMAGEAPI ImageDirectoryEntryToData(PVOID Base,BOOLEAN MappedAsImage,USHORT DirectoryEntry,PULONG Size); + PIMAGE_SECTION_HEADER IMAGEAPI ImageRvaToSection(PIMAGE_NT_HEADERS NtHeaders,PVOID Base,ULONG Rva); + PVOID IMAGEAPI ImageRvaToVa(PIMAGE_NT_HEADERS NtHeaders,PVOID Base,ULONG Rva,PIMAGE_SECTION_HEADER *LastRvaSection); + +#define SSRVOPT_CALLBACK 0x0001 +#define SSRVOPT_DWORD 0x0002 +#define SSRVOPT_DWORDPTR 0x0004 +#define SSRVOPT_GUIDPTR 0x0008 +#define SSRVOPT_OLDGUIDPTR 0x0010 +#define SSRVOPT_UNATTENDED 0x0020 +#define SSRVOPT_NOCOPY 0x0040 +#define SSRVOPT_PARENTWIN 0x0080 +#define SSRVOPT_PARAMTYPE 0x0100 +#define SSRVOPT_SECURE 0x0200 +#define SSRVOPT_TRACE 0x0400 +#define SSRVOPT_SETCONTEXT 0x0800 +#define SSRVOPT_PROXY 0x1000 +#define SSRVOPT_DOWNSTREAM_STORE 0x2000 +#define SSRVOPT_RESET ((ULONG_PTR)-1) + +#define SSRVACTION_TRACE 1 +#define SSRVACTION_QUERYCANCEL 2 +#define SSRVACTION_EVENT 3 + +#if !defined _WIN64 && !defined EBACKTRACE_MINGW32 + typedef struct _IMAGE_DEBUG_INFORMATION { + LIST_ENTRY List; + DWORD ReservedSize; + PVOID ReservedMappedBase; + USHORT ReservedMachine; + USHORT ReservedCharacteristics; + DWORD ReservedCheckSum; + DWORD ImageBase; + DWORD SizeOfImage; + DWORD ReservedNumberOfSections; + PIMAGE_SECTION_HEADER ReservedSections; + DWORD ReservedExportedNamesSize; + PSTR ReservedExportedNames; + DWORD ReservedNumberOfFunctionTableEntries; + PIMAGE_FUNCTION_ENTRY ReservedFunctionTableEntries; + DWORD ReservedLowestFunctionStartingAddress; + DWORD ReservedHighestFunctionEndingAddress; + DWORD ReservedNumberOfFpoTableEntries; + PFPO_DATA ReservedFpoTableEntries; + DWORD SizeOfCoffSymbols; + PIMAGE_COFF_SYMBOLS_HEADER CoffSymbols; + DWORD ReservedSizeOfCodeViewSymbols; + PVOID ReservedCodeViewSymbols; + PSTR ImageFilePath; + PSTR ImageFileName; + PSTR ReservedDebugFilePath; + DWORD ReservedTimeDateStamp; + WINBOOL ReservedRomImage; + PIMAGE_DEBUG_DIRECTORY ReservedDebugDirectory; + DWORD ReservedNumberOfDebugDirectories; + DWORD ReservedOriginalFunctionTableBaseAddress; + DWORD Reserved[2]; + } IMAGE_DEBUG_INFORMATION,*PIMAGE_DEBUG_INFORMATION; + + PIMAGE_DEBUG_INFORMATION IMAGEAPI MapDebugInformation(HANDLE FileHandle,PSTR FileName,PSTR SymbolPath,DWORD ImageBase); + WINBOOL IMAGEAPI UnmapDebugInformation(PIMAGE_DEBUG_INFORMATION DebugInfo); +#endif + + typedef WINBOOL (CALLBACK *PENUMDIRTREE_CALLBACK)(LPCSTR FilePath,PVOID CallerData); + + WINBOOL IMAGEAPI SearchTreeForFile(PSTR RootPath,PSTR InputPathName,PSTR OutputPathBuffer); + WINBOOL IMAGEAPI SearchTreeForFileW(PWSTR RootPath,PWSTR InputPathName,PWSTR OutputPathBuffer); + WINBOOL IMAGEAPI EnumDirTree(HANDLE hProcess,PSTR RootPath,PSTR InputPathName,PSTR OutputPathBuffer,PENUMDIRTREE_CALLBACK Callback,PVOID CallbackData); + WINBOOL IMAGEAPI MakeSureDirectoryPathExists(PCSTR DirPath); + +#ifndef EBACKTRACE_MINGW32 +#define UNDNAME_COMPLETE (0x0000) +#define UNDNAME_NO_LEADING_UNDERSCORES (0x0001) +#define UNDNAME_NO_MS_KEYWORDS (0x0002) +#define UNDNAME_NO_FUNCTION_RETURNS (0x0004) +#define UNDNAME_NO_ALLOCATION_MODEL (0x0008) +#define UNDNAME_NO_ALLOCATION_LANGUAGE (0x0010) +#define UNDNAME_NO_MS_THISTYPE (0x0020) +#define UNDNAME_NO_CV_THISTYPE (0x0040) +#define UNDNAME_NO_THISTYPE (0x0060) +#define UNDNAME_NO_ACCESS_SPECIFIERS (0x0080) +#define UNDNAME_NO_THROW_SIGNATURES (0x0100) +#define UNDNAME_NO_MEMBER_TYPE (0x0200) +#define UNDNAME_NO_RETURN_UDT_MODEL (0x0400) +#define UNDNAME_32_BIT_DECODE (0x0800) +#define UNDNAME_NAME_ONLY (0x1000) +#define UNDNAME_NO_ARGUMENTS (0x2000) +#define UNDNAME_NO_SPECIAL_SYMS (0x4000) + +#define UNDNAME_NO_ARGUMENTS (0x2000) +#define UNDNAME_NO_SPECIAL_SYMS (0x4000) +#endif + + DWORD IMAGEAPI WINAPI UnDecorateSymbolName(PCSTR DecoratedName,PSTR UnDecoratedName,DWORD UndecoratedLength,DWORD Flags); + DWORD IMAGEAPI WINAPI UnDecorateSymbolNameW(PCWSTR DecoratedName,PWSTR UnDecoratedName,DWORD UndecoratedLength,DWORD Flags); + +#ifdef DBGHELP_TRANSLATE_TCHAR +#define UnDecorateSymbolName UnDecorateSymbolNameW +#endif + +#define DBHHEADER_DEBUGDIRS 0x1 +#define DBHHEADER_CVMISC 0x2 + + typedef struct _MODLOAD_CVMISC { + DWORD oCV; + size_t cCV; + DWORD oMisc; + size_t cMisc; + DWORD dtImage; + DWORD cImage; + } MODLOAD_CVMISC, *PMODLOAD_CVMISC; + +#ifndef EBACKTRACE_MINGW32 + typedef enum { + AddrMode1616, + AddrMode1632, + AddrModeReal, + AddrModeFlat + } ADDRESS_MODE; +#endif + + typedef struct _tagADDRESS64 { + DWORD64 Offset; + WORD Segment; + ADDRESS_MODE Mode; + } ADDRESS64,*LPADDRESS64; + +#ifdef _IMAGEHLP64 +#define ADDRESS ADDRESS64 +#define LPADDRESS LPADDRESS64 +#elif !defined(EBACKTRACE_MINGW32) + typedef struct _tagADDRESS { + DWORD Offset; + WORD Segment; + ADDRESS_MODE Mode; + } ADDRESS,*LPADDRESS; + + static __inline void Address32To64(LPADDRESS a32,LPADDRESS64 a64) { + a64->Offset = (ULONG64)(LONG64)(LONG)a32->Offset; + a64->Segment = a32->Segment; + a64->Mode = a32->Mode; + } + + static __inline void Address64To32(LPADDRESS64 a64,LPADDRESS a32) { + a32->Offset = (ULONG)a64->Offset; + a32->Segment = a64->Segment; + a32->Mode = a64->Mode; + } +#endif + + typedef struct _KDHELP64 { + DWORD64 Thread; + DWORD ThCallbackStack; + DWORD ThCallbackBStore; + DWORD NextCallback; + DWORD FramePointer; + DWORD64 KiCallUserMode; + DWORD64 KeUserCallbackDispatcher; + DWORD64 SystemRangeStart; + DWORD64 KiUserExceptionDispatcher; + DWORD64 StackBase; + DWORD64 StackLimit; + DWORD64 Reserved[5]; + } KDHELP64,*PKDHELP64; + +#ifdef _IMAGEHLP64 +#define KDHELP KDHELP64 +#define PKDHELP PKDHELP64 +#elif !defined(EBACKTRACE_MINGW32) + typedef struct _KDHELP { + DWORD Thread; + DWORD ThCallbackStack; + DWORD NextCallback; + DWORD FramePointer; + DWORD KiCallUserMode; + DWORD KeUserCallbackDispatcher; + DWORD SystemRangeStart; + DWORD ThCallbackBStore; + DWORD KiUserExceptionDispatcher; + DWORD StackBase; + DWORD StackLimit; + DWORD Reserved[5]; + } KDHELP,*PKDHELP; + + static __inline void KdHelp32To64(PKDHELP p32,PKDHELP64 p64) { + p64->Thread = p32->Thread; + p64->ThCallbackStack = p32->ThCallbackStack; + p64->NextCallback = p32->NextCallback; + p64->FramePointer = p32->FramePointer; + p64->KiCallUserMode = p32->KiCallUserMode; + p64->KeUserCallbackDispatcher = p32->KeUserCallbackDispatcher; + p64->SystemRangeStart = p32->SystemRangeStart; + p64->KiUserExceptionDispatcher = p32->KiUserExceptionDispatcher; + p64->StackBase = p32->StackBase; + p64->StackLimit = p32->StackLimit; + } +#endif + + typedef struct _tagSTACKFRAME64 { + ADDRESS64 AddrPC; + ADDRESS64 AddrReturn; + ADDRESS64 AddrFrame; + ADDRESS64 AddrStack; + ADDRESS64 AddrBStore; + PVOID FuncTableEntry; + DWORD64 Params[4]; + WINBOOL Far; + WINBOOL Virtual; + DWORD64 Reserved[3]; + KDHELP64 KdHelp; + } STACKFRAME64,*LPSTACKFRAME64; + +#ifdef _IMAGEHLP64 +#define STACKFRAME STACKFRAME64 +#define LPSTACKFRAME LPSTACKFRAME64 +#elif !defined(EBACKTRACE_MINGW32) + typedef struct _tagSTACKFRAME { + ADDRESS AddrPC; + ADDRESS AddrReturn; + ADDRESS AddrFrame; + ADDRESS AddrStack; + PVOID FuncTableEntry; + DWORD Params[4]; + WINBOOL Far; + WINBOOL Virtual; + DWORD Reserved[3]; + KDHELP KdHelp; + ADDRESS AddrBStore; + } STACKFRAME,*LPSTACKFRAME; +#endif + + typedef WINBOOL (WINAPI *PREAD_PROCESS_MEMORY_ROUTINE64)(HANDLE hProcess,DWORD64 qwBaseAddress,PVOID lpBuffer,DWORD nSize,LPDWORD lpNumberOfBytesRead); + typedef PVOID (WINAPI *PFUNCTION_TABLE_ACCESS_ROUTINE64)(HANDLE hProcess,DWORD64 AddrBase); + typedef DWORD64 (WINAPI *PGET_MODULE_BASE_ROUTINE64)(HANDLE hProcess,DWORD64 Address); + typedef DWORD64 (WINAPI *PTRANSLATE_ADDRESS_ROUTINE64)(HANDLE hProcess,HANDLE hThread,LPADDRESS64 lpaddr); + + WINBOOL IMAGEAPI StackWalk64(DWORD MachineType,HANDLE hProcess,HANDLE hThread,LPSTACKFRAME64 StackFrame,PVOID ContextRecord,PREAD_PROCESS_MEMORY_ROUTINE64 ReadMemoryRoutine,PFUNCTION_TABLE_ACCESS_ROUTINE64 FunctionTableAccessRoutine,PGET_MODULE_BASE_ROUTINE64 +GetModuleBaseRoutine,PTRANSLATE_ADDRESS_ROUTINE64 TranslateAddress); + +#ifdef _IMAGEHLP64 +#define PREAD_PROCESS_MEMORY_ROUTINE PREAD_PROCESS_MEMORY_ROUTINE64 +#define PFUNCTION_TABLE_ACCESS_ROUTINE PFUNCTION_TABLE_ACCESS_ROUTINE64 +#define PGET_MODULE_BASE_ROUTINE PGET_MODULE_BASE_ROUTINE64 +#define PTRANSLATE_ADDRESS_ROUTINE PTRANSLATE_ADDRESS_ROUTINE64 +#define StackWalk StackWalk64 +#elif !defined(EBACKTRACE_MINGW32) + typedef WINBOOL (WINAPI *PREAD_PROCESS_MEMORY_ROUTINE)(HANDLE hProcess,DWORD lpBaseAddress,PVOID lpBuffer,DWORD nSize,PDWORD lpNumberOfBytesRead); + typedef PVOID (WINAPI *PFUNCTION_TABLE_ACCESS_ROUTINE)(HANDLE hProcess,DWORD AddrBase); + typedef DWORD (WINAPI *PGET_MODULE_BASE_ROUTINE)(HANDLE hProcess,DWORD Address); + typedef DWORD (WINAPI *PTRANSLATE_ADDRESS_ROUTINE)(HANDLE hProcess,HANDLE hThread,LPADDRESS lpaddr); + + WINBOOL IMAGEAPI StackWalk(DWORD MachineType,HANDLE hProcess,HANDLE hThread,LPSTACKFRAME StackFrame,PVOID ContextRecord,PREAD_PROCESS_MEMORY_ROUTINE ReadMemoryRoutine,PFUNCTION_TABLE_ACCESS_ROUTINE FunctionTableAccessRoutine,PGET_MODULE_BASE_ROUTINE +GetModuleBaseRoutine,PTRANSLATE_ADDRESS_ROUTINE TranslateAddress); +#endif + +#ifndef EBACKTRACE_MINGW32 +#define API_VERSION_NUMBER 11 + + typedef struct API_VERSION { + USHORT MajorVersion; + USHORT MinorVersion; + USHORT Revision; + USHORT Reserved; + } API_VERSION,*LPAPI_VERSION; +#endif + + LPAPI_VERSION IMAGEAPI ImagehlpApiVersion(VOID); + LPAPI_VERSION IMAGEAPI ImagehlpApiVersionEx(LPAPI_VERSION AppVersion); + DWORD IMAGEAPI GetTimestampForLoadedLibrary(HMODULE Module); + + typedef WINBOOL (CALLBACK *PSYM_ENUMMODULES_CALLBACK64)(PCSTR ModuleName,DWORD64 BaseOfDll,PVOID UserContext); + typedef WINBOOL (CALLBACK *PSYM_ENUMMODULES_CALLBACKW64)(PCWSTR ModuleName,DWORD64 BaseOfDll,PVOID UserContext); + typedef WINBOOL (CALLBACK *PSYM_ENUMSYMBOLS_CALLBACK64)(PCSTR SymbolName,DWORD64 SymbolAddress,ULONG SymbolSize,PVOID UserContext); + typedef WINBOOL (CALLBACK *PSYM_ENUMSYMBOLS_CALLBACK64W)(PCWSTR SymbolName,DWORD64 SymbolAddress,ULONG SymbolSize,PVOID UserContext); + typedef WINBOOL (CALLBACK *PENUMLOADED_MODULES_CALLBACK64)(PCSTR ModuleName,DWORD64 ModuleBase,ULONG ModuleSize,PVOID UserContext); + typedef WINBOOL (CALLBACK *PENUMLOADED_MODULES_CALLBACKW64)(PCWSTR ModuleName,DWORD64 ModuleBase,ULONG ModuleSize,PVOID UserContext); + typedef WINBOOL (CALLBACK *PSYMBOL_REGISTERED_CALLBACK64)(HANDLE hProcess,ULONG ActionCode,ULONG64 CallbackData,ULONG64 UserContext); + typedef PVOID (CALLBACK *PSYMBOL_FUNCENTRY_CALLBACK)(HANDLE hProcess,DWORD AddrBase,PVOID UserContext); + typedef PVOID (CALLBACK *PSYMBOL_FUNCENTRY_CALLBACK64)(HANDLE hProcess,ULONG64 AddrBase,ULONG64 UserContext); + +#ifdef _IMAGEHLP64 +#define PSYM_ENUMMODULES_CALLBACK PSYM_ENUMMODULES_CALLBACK64 +#define PSYM_ENUMSYMBOLS_CALLBACK PSYM_ENUMSYMBOLS_CALLBACK64 +#define PSYM_ENUMSYMBOLS_CALLBACKW PSYM_ENUMSYMBOLS_CALLBACK64W +#define PENUMLOADED_MODULES_CALLBACK PENUMLOADED_MODULES_CALLBACK64 +#define PSYMBOL_REGISTERED_CALLBACK PSYMBOL_REGISTERED_CALLBACK64 +#define PSYMBOL_FUNCENTRY_CALLBACK PSYMBOL_FUNCENTRY_CALLBACK64 +#elif !defined(EBACKTRACE_MINGW32) + typedef WINBOOL (CALLBACK *PSYM_ENUMMODULES_CALLBACK)(PCSTR ModuleName,ULONG BaseOfDll,PVOID UserContext); + typedef WINBOOL (CALLBACK *PSYM_ENUMSYMBOLS_CALLBACK)(PCSTR SymbolName,ULONG SymbolAddress,ULONG SymbolSize,PVOID UserContext); + typedef WINBOOL (CALLBACK *PSYM_ENUMSYMBOLS_CALLBACKW)(PCWSTR SymbolName,ULONG SymbolAddress,ULONG SymbolSize,PVOID UserContext); + typedef WINBOOL (CALLBACK *PENUMLOADED_MODULES_CALLBACK)(PCSTR ModuleName,ULONG ModuleBase,ULONG ModuleSize,PVOID UserContext); + typedef WINBOOL (CALLBACK *PSYMBOL_REGISTERED_CALLBACK)(HANDLE hProcess,ULONG ActionCode,PVOID CallbackData,PVOID UserContext); +#endif + +#define SYMFLAG_VALUEPRESENT 0x00000001 +#define SYMFLAG_REGISTER 0x00000008 +#define SYMFLAG_REGREL 0x00000010 +#define SYMFLAG_FRAMEREL 0x00000020 +#define SYMFLAG_PARAMETER 0x00000040 +#define SYMFLAG_LOCAL 0x00000080 +#define SYMFLAG_CONSTANT 0x00000100 +#define SYMFLAG_EXPORT 0x00000200 +#define SYMFLAG_FORWARDER 0x00000400 +#define SYMFLAG_FUNCTION 0x00000800 +#define SYMFLAG_VIRTUAL 0x00001000 +#define SYMFLAG_THUNK 0x00002000 +#define SYMFLAG_TLSREL 0x00004000 + +#ifndef EBACKTRACE_MINGW32 + typedef enum { + SymNone = 0, + SymCoff, + SymCv, + SymPdb, + SymExport, + SymDeferred, + SymSym, + SymDia, + SymVirtual, + NumSymTypes + } SYM_TYPE; +#endif + + typedef struct _IMAGEHLP_SYMBOL64 { + DWORD SizeOfStruct; + DWORD64 Address; + DWORD Size; + DWORD Flags; + DWORD MaxNameLength; + CHAR Name[1]; + } IMAGEHLP_SYMBOL64,*PIMAGEHLP_SYMBOL64; + + typedef struct _IMAGEHLP_SYMBOL64_PACKAGE { + IMAGEHLP_SYMBOL64 sym; + CHAR name[MAX_SYM_NAME + 1]; + } IMAGEHLP_SYMBOL64_PACKAGE,*PIMAGEHLP_SYMBOL64_PACKAGE; + +#ifdef _IMAGEHLP64 + +#define IMAGEHLP_SYMBOL IMAGEHLP_SYMBOL64 +#define PIMAGEHLP_SYMBOL PIMAGEHLP_SYMBOL64 +#define IMAGEHLP_SYMBOL_PACKAGE IMAGEHLP_SYMBOL64_PACKAGE +#define PIMAGEHLP_SYMBOL_PACKAGE PIMAGEHLP_SYMBOL64_PACKAGE +#elif !defined(EBACKTRACE_MINGW32) + + typedef struct _IMAGEHLP_SYMBOL { + DWORD SizeOfStruct; + DWORD Address; + DWORD Size; + DWORD Flags; + DWORD MaxNameLength; + CHAR Name[1]; + } IMAGEHLP_SYMBOL,*PIMAGEHLP_SYMBOL; + + typedef struct _IMAGEHLP_SYMBOL_PACKAGE { + IMAGEHLP_SYMBOL sym; + CHAR name[MAX_SYM_NAME + 1]; + } IMAGEHLP_SYMBOL_PACKAGE,*PIMAGEHLP_SYMBOL_PACKAGE; +#endif + + typedef struct _IMAGEHLP_MODULE64 { + DWORD SizeOfStruct; + DWORD64 BaseOfImage; + DWORD ImageSize; + DWORD TimeDateStamp; + DWORD CheckSum; + DWORD NumSyms; + SYM_TYPE SymType; + CHAR ModuleName[32]; + CHAR ImageName[256]; + CHAR LoadedImageName[256]; + CHAR LoadedPdbName[256]; + DWORD CVSig; + CHAR CVData[MAX_PATH*3]; + DWORD PdbSig; + GUID PdbSig70; + DWORD PdbAge; + WINBOOL PdbUnmatched; + WINBOOL DbgUnmatched; + WINBOOL LineNumbers; + WINBOOL GlobalSymbols; + WINBOOL TypeInfo; + WINBOOL SourceIndexed; + WINBOOL Publics; + } IMAGEHLP_MODULE64,*PIMAGEHLP_MODULE64; + + typedef struct _IMAGEHLP_MODULE64W { + DWORD SizeOfStruct; + DWORD64 BaseOfImage; + DWORD ImageSize; + DWORD TimeDateStamp; + DWORD CheckSum; + DWORD NumSyms; + SYM_TYPE SymType; + WCHAR ModuleName[32]; + WCHAR ImageName[256]; + WCHAR LoadedImageName[256]; + WCHAR LoadedPdbName[256]; + DWORD CVSig; + WCHAR CVData[MAX_PATH*3]; + DWORD PdbSig; + GUID PdbSig70; + DWORD PdbAge; + WINBOOL PdbUnmatched; + WINBOOL DbgUnmatched; + WINBOOL LineNumbers; + WINBOOL GlobalSymbols; + WINBOOL TypeInfo; + WINBOOL SourceIndexed; + WINBOOL Publics; + } IMAGEHLP_MODULEW64,*PIMAGEHLP_MODULEW64; + +#ifdef _IMAGEHLP64 +#define IMAGEHLP_MODULE IMAGEHLP_MODULE64 +#define PIMAGEHLP_MODULE PIMAGEHLP_MODULE64 +#define IMAGEHLP_MODULEW IMAGEHLP_MODULEW64 +#define PIMAGEHLP_MODULEW PIMAGEHLP_MODULEW64 +#elif !defined(EBACKTRACE_MINGW32) + typedef struct _IMAGEHLP_MODULE { + DWORD SizeOfStruct; + DWORD BaseOfImage; + DWORD ImageSize; + DWORD TimeDateStamp; + DWORD CheckSum; + DWORD NumSyms; + SYM_TYPE SymType; + CHAR ModuleName[32]; + CHAR ImageName[256]; + CHAR LoadedImageName[256]; + } IMAGEHLP_MODULE,*PIMAGEHLP_MODULE; + + typedef struct _IMAGEHLP_MODULEW { + DWORD SizeOfStruct; + DWORD BaseOfImage; + DWORD ImageSize; + DWORD TimeDateStamp; + DWORD CheckSum; + DWORD NumSyms; + SYM_TYPE SymType; + WCHAR ModuleName[32]; + WCHAR ImageName[256]; + WCHAR LoadedImageName[256]; + } IMAGEHLP_MODULEW,*PIMAGEHLP_MODULEW; +#endif + + typedef struct _IMAGEHLP_LINE64 { + DWORD SizeOfStruct; + PVOID Key; + DWORD LineNumber; + PCHAR FileName; + DWORD64 Address; + } IMAGEHLP_LINE64,*PIMAGEHLP_LINE64; + + typedef struct _IMAGEHLP_LINEW64 { + DWORD SizeOfStruct; + PVOID Key; + DWORD LineNumber; + PWSTR FileName; + DWORD64 Address; + } IMAGEHLP_LINEW64, *PIMAGEHLP_LINEW64; + +#ifdef _IMAGEHLP64 +#define IMAGEHLP_LINE IMAGEHLP_LINE64 +#define PIMAGEHLP_LINE PIMAGEHLP_LINE64 +#elif !defined(EBACKTRACE_MINGW32) + typedef struct _IMAGEHLP_LINE { + DWORD SizeOfStruct; + PVOID Key; + DWORD LineNumber; + PCHAR FileName; + DWORD Address; + } IMAGEHLP_LINE,*PIMAGEHLP_LINE; +#endif + + typedef struct _SOURCEFILE { + DWORD64 ModBase; + PCHAR FileName; + } SOURCEFILE,*PSOURCEFILE; + + typedef struct _SOURCEFILEW { + DWORD64 ModBase; + PWCHAR FileName; + } SOURCEFILEW,*PSOURCEFILEW; + +#ifndef EBACKTRACE_MINGW32 +#define CBA_DEFERRED_SYMBOL_LOAD_START 0x00000001 +#define CBA_DEFERRED_SYMBOL_LOAD_COMPLETE 0x00000002 +#define CBA_DEFERRED_SYMBOL_LOAD_FAILURE 0x00000003 +#define CBA_SYMBOLS_UNLOADED 0x00000004 +#define CBA_DUPLICATE_SYMBOL 0x00000005 +#define CBA_READ_MEMORY 0x00000006 +#define CBA_DEFERRED_SYMBOL_LOAD_CANCEL 0x00000007 +#define CBA_SET_OPTIONS 0x00000008 +#define CBA_EVENT 0x00000010 +#define CBA_DEFERRED_SYMBOL_LOAD_PARTIAL 0x00000020 +#define CBA_DEBUG_INFO 0x10000000 +#define CBA_SRCSRV_INFO 0x20000000 +#define CBA_SRCSRV_EVENT 0x40000000 +#endif + + typedef struct _IMAGEHLP_CBA_READ_MEMORY { + DWORD64 addr; + PVOID buf; + DWORD bytes; + DWORD *bytesread; + } IMAGEHLP_CBA_READ_MEMORY,*PIMAGEHLP_CBA_READ_MEMORY; + + enum { + sevInfo = 0, + sevProblem, + sevAttn, + sevFatal, + sevMax + }; + + typedef struct _IMAGEHLP_CBA_EVENT { + DWORD severity; + DWORD code; + PCHAR desc; + PVOID object; + } IMAGEHLP_CBA_EVENT,*PIMAGEHLP_CBA_EVENT; + + typedef struct _IMAGEHLP_DEFERRED_SYMBOL_LOAD64 { + DWORD SizeOfStruct; + DWORD64 BaseOfImage; + DWORD CheckSum; + DWORD TimeDateStamp; + CHAR FileName[MAX_PATH]; + BOOLEAN Reparse; + HANDLE hFile; + DWORD Flags; + } IMAGEHLP_DEFERRED_SYMBOL_LOAD64,*PIMAGEHLP_DEFERRED_SYMBOL_LOAD64; + +#define DSLFLAG_MISMATCHED_PDB 0x1 +#define DSLFLAG_MISMATCHED_DBG 0x2 + +#ifdef _IMAGEHLP64 +#define IMAGEHLP_DEFERRED_SYMBOL_LOAD IMAGEHLP_DEFERRED_SYMBOL_LOAD64 +#define PIMAGEHLP_DEFERRED_SYMBOL_LOAD PIMAGEHLP_DEFERRED_SYMBOL_LOAD64 +#elif !defined(EBACKTRACE_MINGW32) + typedef struct _IMAGEHLP_DEFERRED_SYMBOL_LOAD { + DWORD SizeOfStruct; + DWORD BaseOfImage; + DWORD CheckSum; + DWORD TimeDateStamp; + CHAR FileName[MAX_PATH]; + BOOLEAN Reparse; + HANDLE hFile; + } IMAGEHLP_DEFERRED_SYMBOL_LOAD,*PIMAGEHLP_DEFERRED_SYMBOL_LOAD; +#endif + + typedef struct _IMAGEHLP_DUPLICATE_SYMBOL64 { + DWORD SizeOfStruct; + DWORD NumberOfDups; + PIMAGEHLP_SYMBOL64 Symbol; + DWORD SelectedSymbol; + } IMAGEHLP_DUPLICATE_SYMBOL64,*PIMAGEHLP_DUPLICATE_SYMBOL64; + +#ifdef _IMAGEHLP64 +#define IMAGEHLP_DUPLICATE_SYMBOL IMAGEHLP_DUPLICATE_SYMBOL64 +#define PIMAGEHLP_DUPLICATE_SYMBOL PIMAGEHLP_DUPLICATE_SYMBOL64 +#elif !defined(EBACKTRACE_MINGW32) + typedef struct _IMAGEHLP_DUPLICATE_SYMBOL { + DWORD SizeOfStruct; + DWORD NumberOfDups; + PIMAGEHLP_SYMBOL Symbol; + DWORD SelectedSymbol; + } IMAGEHLP_DUPLICATE_SYMBOL,*PIMAGEHLP_DUPLICATE_SYMBOL; +#endif + +typedef struct _SYMSRV_INDEX_INFO { + DWORD sizeofstruct; + CHAR file[MAX_PATH +1]; + WINBOOL stripped; + DWORD timestamp; + DWORD size; + CHAR dbgfile[MAX_PATH +1]; + CHAR pdbfile[MAX_PATH + 1]; + GUID guid; + DWORD sig; + DWORD age; +} SYMSRV_INDEX_INFO, *PSYMSRV_INDEX_INFO; + +typedef struct _SYMSRV_INDEX_INFOW { + DWORD sizeofstruct; + WCHAR file[MAX_PATH +1]; + WINBOOL stripped; + DWORD timestamp; + DWORD size; + WCHAR dbgfile[MAX_PATH +1]; + WCHAR pdbfile[MAX_PATH + 1]; + GUID guid; + DWORD sig; + DWORD age; +} SYMSRV_INDEX_INFOW, *PSYMSRV_INDEX_INFOW; + + WINBOOL IMAGEAPI SymSetParentWindow(HWND hwnd); + PCHAR IMAGEAPI SymSetHomeDirectory(HANDLE hProcess,PCSTR dir); + PCHAR IMAGEAPI SymSetHomeDirectoryW(HANDLE hProcess,PCWSTR dir); + PCHAR IMAGEAPI SymGetHomeDirectory(DWORD type,PSTR dir,size_t size); + PWCHAR IMAGEAPI SymGetHomeDirectoryW(DWORD type,PWSTR dir,size_t size); + +#define hdBase 0 +#define hdSym 1 +#define hdSrc 2 +#define hdMax 3 + +#ifndef EBACKTRACE_MINGW32 +#define SYMOPT_CASE_INSENSITIVE 0x00000001 +#define SYMOPT_UNDNAME 0x00000002 +#define SYMOPT_DEFERRED_LOADS 0x00000004 +#define SYMOPT_NO_CPP 0x00000008 +#define SYMOPT_LOAD_LINES 0x00000010 +#define SYMOPT_OMAP_FIND_NEAREST 0x00000020 +#define SYMOPT_LOAD_ANYTHING 0x00000040 +#define SYMOPT_IGNORE_CVREC 0x00000080 +#define SYMOPT_NO_UNQUALIFIED_LOADS 0x00000100 +#define SYMOPT_FAIL_CRITICAL_ERRORS 0x00000200 +#define SYMOPT_EXACT_SYMBOLS 0x00000400 +#define SYMOPT_ALLOW_ABSOLUTE_SYMBOLS 0x00000800 +#define SYMOPT_IGNORE_NT_SYMPATH 0x00001000 +#define SYMOPT_INCLUDE_32BIT_MODULES 0x00002000 +#define SYMOPT_PUBLICS_ONLY 0x00004000 +#define SYMOPT_NO_PUBLICS 0x00008000 +#define SYMOPT_AUTO_PUBLICS 0x00010000 +#define SYMOPT_NO_IMAGE_SEARCH 0x00020000 +#define SYMOPT_SECURE 0x00040000 +#define SYMOPT_NO_PROMPTS 0x00080000 +#define SYMOPT_ALLOW_ZERO_ADDRESS 0x01000000 +#define SYMOPT_DISABLE_SYMSRV_AUTODETECT 0x02000000 +#define SYMOPT_FAVOR_COMPRESSED 0x00800000 +#define SYMOPT_FLAT_DIRECTORY 0x00400000 +#define SYMOPT_IGNORE_IMAGEDIR 0x00200000 +#define SYMOPT_OVERWRITE 0x00100000 + +#define SYMOPT_DEBUG 0x80000000 +#endif + + DWORD IMAGEAPI SymSetOptions(DWORD SymOptions); + DWORD IMAGEAPI SymGetOptions(VOID); + WINBOOL IMAGEAPI SymCleanup(HANDLE hProcess); + WINBOOL IMAGEAPI SymMatchString(PCSTR string,PCSTR expression,WINBOOL fCase); + WINBOOL IMAGEAPI SymMatchStringW(PCWSTR string,PCWSTR expression,WINBOOL fCase); + + typedef WINBOOL (CALLBACK *PSYM_ENUMSOURCEFILES_CALLBACK)(PSOURCEFILE pSourceFile,PVOID UserContext); + typedef WINBOOL (CALLBACK *PSYM_ENUMSOURCEFILES_CALLBACKW)(PSOURCEFILEW pSourceFile,PVOID UserContext); +#define PSYM_ENUMSOURCFILES_CALLBACK PSYM_ENUMSOURCEFILES_CALLBACK + + WINBOOL IMAGEAPI SymEnumSourceFiles(HANDLE hProcess,ULONG64 ModBase,PCSTR Mask,PSYM_ENUMSOURCEFILES_CALLBACK cbSrcFiles,PVOID UserContext); + WINBOOL IMAGEAPI SymEnumSourceFilesW(HANDLE hProcess,ULONG64 ModBase,PCWSTR Mask,PSYM_ENUMSOURCEFILES_CALLBACKW cbSrcFiles,PVOID UserContext); + WINBOOL IMAGEAPI SymEnumerateModules64(HANDLE hProcess,PSYM_ENUMMODULES_CALLBACK64 EnumModulesCallback,PVOID UserContext); + WINBOOL IMAGEAPI SymEnumerateModulesW64(HANDLE hProcess,PSYM_ENUMMODULES_CALLBACKW64 EnumModulesCallback,PVOID UserContext); + +#ifdef _IMAGEHLP64 +#define SymEnumerateModules SymEnumerateModules64 +#elif !defined(EBACKTRACE_MINGW32) + WINBOOL IMAGEAPI SymEnumerateModules(HANDLE hProcess,PSYM_ENUMMODULES_CALLBACK EnumModulesCallback,PVOID UserContext); +#endif + + WINBOOL IMAGEAPI SymEnumerateSymbols64(HANDLE hProcess,DWORD64 BaseOfDll,PSYM_ENUMSYMBOLS_CALLBACK64 EnumSymbolsCallback,PVOID UserContext); + WINBOOL IMAGEAPI SymEnumerateSymbolsW64(HANDLE hProcess,DWORD64 BaseOfDll,PSYM_ENUMSYMBOLS_CALLBACK64W EnumSymbolsCallback,PVOID UserContext); + +#ifdef _IMAGEHLP64 +#define SymEnumerateSymbols SymEnumerateSymbols64 +#define SymEnumerateSymbolsW SymEnumerateSymbolsW64 +#elif !defined(EBACKTRACE_MINGW32) + WINBOOL IMAGEAPI SymEnumerateSymbols(HANDLE hProcess,DWORD BaseOfDll,PSYM_ENUMSYMBOLS_CALLBACK EnumSymbolsCallback,PVOID UserContext); + WINBOOL IMAGEAPI SymEnumerateSymbolsW(HANDLE hProcess,DWORD BaseOfDll,PSYM_ENUMSYMBOLS_CALLBACKW EnumSymbolsCallback,PVOID UserContext); +#endif + + WINBOOL IMAGEAPI EnumerateLoadedModules64(HANDLE hProcess,PENUMLOADED_MODULES_CALLBACK64 EnumLoadedModulesCallback,PVOID UserContext); + WINBOOL IMAGEAPI EnumerateLoadedModulesW64(HANDLE hProcess,PENUMLOADED_MODULES_CALLBACKW64 EnumLoadedModulesCallback,PVOID UserContext); + +#ifdef DBGHELP_TRANSLATE_TCHAR + #define EnumerateLoadedModules64 EnumerateLoadedModulesW64 +#endif + +#ifdef _IMAGEHLP64 +#define EnumerateLoadedModules EnumerateLoadedModules64 +#elif !defined(EBACKTRACE_MINGW32) + WINBOOL IMAGEAPI EnumerateLoadedModules(HANDLE hProcess,PENUMLOADED_MODULES_CALLBACK EnumLoadedModulesCallback,PVOID UserContext); +#endif + + PVOID IMAGEAPI SymFunctionTableAccess64(HANDLE hProcess,DWORD64 AddrBase); + +#ifdef _IMAGEHLP64 +#define SymFunctionTableAccess SymFunctionTableAccess64 +#elif !defined(EBACKTRACE_MINGW32) + PVOID IMAGEAPI SymFunctionTableAccess(HANDLE hProcess,DWORD AddrBase); +#endif + + WINBOOL IMAGEAPI SymGetModuleInfo64(HANDLE hProcess,DWORD64 qwAddr,PIMAGEHLP_MODULE64 ModuleInfo); + WINBOOL IMAGEAPI SymGetModuleInfoW64(HANDLE hProcess,DWORD64 qwAddr,PIMAGEHLP_MODULEW64 ModuleInfo); + +#ifdef _IMAGEHLP64 +#define SymGetModuleInfo SymGetModuleInfo64 +#define SymGetModuleInfoW SymGetModuleInfoW64 +#elif !defined(EBACKTRACE_MINGW32) + WINBOOL IMAGEAPI SymGetModuleInfo(HANDLE hProcess,DWORD dwAddr,PIMAGEHLP_MODULE ModuleInfo); + WINBOOL IMAGEAPI SymGetModuleInfoW(HANDLE hProcess,DWORD dwAddr,PIMAGEHLP_MODULEW ModuleInfo); +#endif + + DWORD64 IMAGEAPI SymGetModuleBase64(HANDLE hProcess,DWORD64 qwAddr); + +#ifdef _IMAGEHLP64 +#define SymGetModuleBase SymGetModuleBase64 +#elif !defined(EBACKTRACE_MINGW32) + DWORD IMAGEAPI SymGetModuleBase(HANDLE hProcess,DWORD dwAddr); +#endif + + WINBOOL IMAGEAPI SymGetSymNext64(HANDLE hProcess,PIMAGEHLP_SYMBOL64 Symbol); + +#ifdef _IMAGEHLP64 +#define SymGetSymNext SymGetSymNext64 +#elif !defined(EBACKTRACE_MINGW32) + WINBOOL IMAGEAPI SymGetSymNext(HANDLE hProcess,PIMAGEHLP_SYMBOL Symbol); +#endif + + WINBOOL IMAGEAPI SymGetSymPrev64(HANDLE hProcess,PIMAGEHLP_SYMBOL64 Symbol); + +#ifdef _IMAGEHLP64 +#define SymGetSymPrev SymGetSymPrev64 +#elif !defined(EBACKTRACE_MINGW32) + WINBOOL IMAGEAPI SymGetSymPrev(HANDLE hProcess,PIMAGEHLP_SYMBOL Symbol); +#endif + + typedef struct _SRCCODEINFO { + DWORD SizeOfStruct; + PVOID Key; + DWORD64 ModBase; + CHAR Obj[MAX_PATH + 1]; + CHAR FileName[MAX_PATH + 1]; + DWORD LineNumber; + DWORD64 Address; + } SRCCODEINFO,*PSRCCODEINFO; + + typedef struct _SRCCODEINFOW { + DWORD SizeOfStruct; + PVOID Key; + DWORD64 ModBase; + WCHAR Obj[MAX_PATH + 1]; + WCHAR FileName[MAX_PATH + 1]; + DWORD LineNumber; + DWORD64 Address; + } SRCCODEINFOW,*PSRCCODEINFOW; + + typedef WINBOOL (CALLBACK *PSYM_ENUMLINES_CALLBACK)(PSRCCODEINFO LineInfo,PVOID UserContext); + typedef WINBOOL (CALLBACK *PSYM_ENUMLINES_CALLBACKW)(PSRCCODEINFOW LineInfo,PVOID UserContext); + + WINBOOL IMAGEAPI SymEnumLines(HANDLE hProcess,ULONG64 Base,PCSTR Obj,PCSTR File,PSYM_ENUMLINES_CALLBACK EnumLinesCallback,PVOID UserContext); + WINBOOL IMAGEAPI SymEnumLinesW(HANDLE hProcess,ULONG64 Base,PCWSTR Obj,PCSTR File,PSYM_ENUMLINES_CALLBACKW EnumLinesCallback,PVOID UserContext); + WINBOOL IMAGEAPI SymGetLineFromAddr64(HANDLE hProcess,DWORD64 qwAddr,PDWORD pdwDisplacement,PIMAGEHLP_LINE64 Line64); + WINBOOL IMAGEAPI SymGetLineFromAddrW64(HANDLE hProcess,DWORD64 qwAddr,PDWORD pdwDisplacement,PIMAGEHLP_LINEW64 Line64); + +#ifdef _IMAGEHLP64 +#define SymGetLineFromAddr SymGetLineFromAddr64 +#elif !defined(EBACKTRACE_MINGW32) + WINBOOL IMAGEAPI SymGetLineFromAddr(HANDLE hProcess,DWORD dwAddr,PDWORD pdwDisplacement,PIMAGEHLP_LINE Line); +#endif + + WINBOOL IMAGEAPI SymGetLineFromName64(HANDLE hProcess,PCSTR ModuleName,PCSTR FileName,DWORD dwLineNumber,PLONG plDisplacement,PIMAGEHLP_LINE64 Line); + WINBOOL IMAGEAPI SymGetLineFromNameW64(HANDLE hProcess,PCWSTR ModuleName,PCWSTR FileName,DWORD dwLineNumber,PLONG plDisplacement,PIMAGEHLP_LINEW64 Line); + +#ifdef _IMAGEHLP64 +#define SymGetLineFromName SymGetLineFromName64 +#elif !defined(EBACKTRACE_MINGW32) + WINBOOL IMAGEAPI SymGetLineFromName(HANDLE hProcess,PCSTR ModuleName,PCSTR FileName,DWORD dwLineNumber,PLONG plDisplacement,PIMAGEHLP_LINE Line); +#endif + + WINBOOL IMAGEAPI SymGetLineNext64(HANDLE hProcess,PIMAGEHLP_LINE64 Line); + WINBOOL IMAGEAPI SymGetLineNextW64(HANDLE hProcess,PIMAGEHLP_LINEW64 Line); + +#ifdef _IMAGEHLP64 +#define SymGetLineNext SymGetLineNext64 +#elif !defined(EBACKTRACE_MINGW32) + WINBOOL IMAGEAPI SymGetLineNext(HANDLE hProcess,PIMAGEHLP_LINE Line); +#endif + + WINBOOL IMAGEAPI SymGetLinePrev64(HANDLE hProcess,PIMAGEHLP_LINE64 Line); + WINBOOL IMAGEAPI SymGetLinePrevW64(HANDLE hProcess,PIMAGEHLP_LINEW64 Line); + +#ifdef _IMAGEHLP64 +#define SymGetLinePrev SymGetLinePrev64 +#elif !defined(EBACKTRACE_MINGW32) + WINBOOL IMAGEAPI SymGetLinePrev(HANDLE hProcess,PIMAGEHLP_LINE Line); +#endif + +#ifndef EBACKTRACE_MINGW32 + WINBOOL IMAGEAPI SymMatchFileName(PCSTR FileName,PCSTR Match,PSTR *FileNameStop,PSTR *MatchStop); +#endif + WINBOOL IMAGEAPI SymMatchFileNameW(PCWSTR FileName,PCWSTR Match,PWSTR *FileNameStop,PWSTR *MatchStop); +#ifndef EBACKTRACE_MINGW32 + WINBOOL IMAGEAPI SymInitialize(HANDLE hProcess,PCSTR UserSearchPath,WINBOOL fInvadeProcess); +#endif + WINBOOL IMAGEAPI SymInitializeW(HANDLE hProcess,PCWSTR UserSearchPath,WINBOOL fInvadeProcess); +#ifndef EBACKTRACE_MINGW32 + WINBOOL IMAGEAPI SymGetSearchPath(HANDLE hProcess,PSTR SearchPath,DWORD SearchPathLength); +#endif + WINBOOL IMAGEAPI SymGetSearchPathW(HANDLE hProcess,PWSTR SearchPath,DWORD SearchPathLength); +#ifndef EBACKTRACE_MINGW32 + WINBOOL IMAGEAPI SymSetSearchPath(HANDLE hProcess,PCSTR SearchPath); +#endif + WINBOOL IMAGEAPI SymSetSearchPathW(HANDLE hProcess,PCWSTR SearchPath); + DWORD64 IMAGEAPI SymLoadModule64(HANDLE hProcess,HANDLE hFile,PSTR ImageName,PSTR ModuleName,DWORD64 BaseOfDll,DWORD SizeOfDll); + +#define SLMFLAG_VIRTUAL 0x1 + + DWORD64 IMAGEAPI SymLoadModuleEx(HANDLE hProcess,HANDLE hFile,PCSTR ImageName,PCSTR ModuleName,DWORD64 BaseOfDll,DWORD DllSize,PMODLOAD_DATA Data,DWORD Flags); + DWORD64 IMAGEAPI SymLoadModuleExW(HANDLE hProcess,HANDLE hFile,PCWSTR ImageName,PCWSTR ModuleName,DWORD64 BaseOfDll,DWORD DllSize,PMODLOAD_DATA Data,DWORD Flags); + +#ifdef _IMAGEHLP64 +#define SymLoadModule SymLoadModule64 +#elif !defined(EBACKTRACE_MINGW32) + DWORD IMAGEAPI SymLoadModule(HANDLE hProcess,HANDLE hFile,PCSTR ImageName,PCSTR ModuleName,DWORD BaseOfDll,DWORD SizeOfDll); +#endif + + WINBOOL IMAGEAPI SymUnloadModule64(HANDLE hProcess,DWORD64 BaseOfDll); + +#ifdef _IMAGEHLP64 +#define SymUnloadModule SymUnloadModule64 +#elif !defined(EBACKTRACE_MINGW32) + WINBOOL IMAGEAPI SymUnloadModule(HANDLE hProcess,DWORD BaseOfDll); +#endif + + WINBOOL IMAGEAPI SymUnDName64(PIMAGEHLP_SYMBOL64 sym,PSTR UnDecName,DWORD UnDecNameLength); + +#ifdef _IMAGEHLP64 +#define SymUnDName SymUnDName64 +#elif !defined(EBACKTRACE_MINGW32) + WINBOOL IMAGEAPI SymUnDName(PIMAGEHLP_SYMBOL sym,PSTR UnDecName,DWORD UnDecNameLength); +#endif + + WINBOOL IMAGEAPI SymRegisterCallback64(HANDLE hProcess,PSYMBOL_REGISTERED_CALLBACK64 CallbackFunction,ULONG64 UserContext); + WINBOOL IMAGEAPI SymRegisterCallback64W(HANDLE hProcess,PSYMBOL_REGISTERED_CALLBACK64 CallbackFunction,ULONG64 UserContext); + + WINBOOL IMAGEAPI SymRegisterFunctionEntryCallback64(HANDLE hProcess,PSYMBOL_FUNCENTRY_CALLBACK64 CallbackFunction,ULONG64 UserContext); + +#ifdef _IMAGEHLP64 +#define SymRegisterCallback SymRegisterCallback64 +#define SymRegisterFunctionEntryCallback SymRegisterFunctionEntryCallback64 +#elif !defined(EBACKTRACE_MINGW32) + WINBOOL IMAGEAPI SymRegisterCallback(HANDLE hProcess,PSYMBOL_REGISTERED_CALLBACK CallbackFunction,PVOID UserContext); + WINBOOL IMAGEAPI SymRegisterFunctionEntryCallback(HANDLE hProcess,PSYMBOL_FUNCENTRY_CALLBACK CallbackFunction,PVOID UserContext); +#endif + + typedef struct _IMAGEHLP_SYMBOL_SRC { + DWORD sizeofstruct; + DWORD type; + char file[MAX_PATH]; + } IMAGEHLP_SYMBOL_SRC,*PIMAGEHLP_SYMBOL_SRC; + + typedef struct _MODULE_TYPE_INFO { + USHORT dataLength; + USHORT leaf; + BYTE data[1]; + } MODULE_TYPE_INFO,*PMODULE_TYPE_INFO; + + typedef struct _SYMBOL_INFO { + ULONG SizeOfStruct; + ULONG TypeIndex; + ULONG64 Reserved[2]; + ULONG info; + ULONG Size; + ULONG64 ModBase; + ULONG Flags; + ULONG64 Value; + ULONG64 Address; + ULONG Register; + ULONG Scope; + ULONG Tag; + ULONG NameLen; + ULONG MaxNameLen; + CHAR Name[1]; + } SYMBOL_INFO,*PSYMBOL_INFO; + + typedef struct _SYMBOL_INFOW { + ULONG SizeOfStruct; + ULONG TypeIndex; + ULONG64 Reserved[2]; + ULONG info; + ULONG Size; + ULONG64 ModBase; + ULONG Flags; + ULONG64 Value; + ULONG64 Address; + ULONG Register; + ULONG Scope; + ULONG Tag; + ULONG NameLen; + ULONG MaxNameLen; + WCHAR Name[1]; + } SYMBOL_INFOW,*PSYMBOL_INFOW; + +#define SYMFLAG_CLR_TOKEN 0x00040000 +#define SYMFLAG_CONSTANT 0x00000100 +#define SYMFLAG_EXPORT 0x00000200 +#define SYMFLAG_FORWARDER 0x00000400 +#define SYMFLAG_FRAMEREL 0x00000020 +#define SYMFLAG_FUNCTION 0x00000800 +#define SYMFLAG_ILREL 0x00010000 +#define SYMFLAG_LOCAL 0x00000080 +#define SYMFLAG_METADATA 0x00020000 +#define SYMFLAG_PARAMETER 0x00000040 +#define SYMFLAG_REGISTER 0x00000008 +#define SYMFLAG_REGREL 0x00000010 +#define SYMFLAG_SLOT 0x00008000 +#define SYMFLAG_THUNK 0x00002000 +#define SYMFLAG_TLSREL 0x00004000 +#define SYMFLAG_VALUEPRESENT 0x00000001 +#define SYMFLAG_VIRTUAL 0x00001000 + + typedef struct _SYMBOL_INFO_PACKAGE { + SYMBOL_INFO si; + CHAR name[MAX_SYM_NAME + 1]; + } SYMBOL_INFO_PACKAGE,*PSYMBOL_INFO_PACKAGE; + + typedef struct _IMAGEHLP_STACK_FRAME { + ULONG64 InstructionOffset; + ULONG64 ReturnOffset; + ULONG64 FrameOffset; + ULONG64 StackOffset; + ULONG64 BackingStoreOffset; + ULONG64 FuncTableEntry; + ULONG64 Params[4]; + ULONG64 Reserved[5]; + WINBOOL Virtual; + ULONG Reserved2; + } IMAGEHLP_STACK_FRAME,*PIMAGEHLP_STACK_FRAME; + + typedef VOID IMAGEHLP_CONTEXT,*PIMAGEHLP_CONTEXT; + + WINBOOL IMAGEAPI SymSetContext(HANDLE hProcess,PIMAGEHLP_STACK_FRAME StackFrame,PIMAGEHLP_CONTEXT Context); + WINBOOL IMAGEAPI SymFromAddr(HANDLE hProcess,DWORD64 Address,PDWORD64 Displacement,PSYMBOL_INFO Symbol); + WINBOOL IMAGEAPI SymFromAddrW(HANDLE hProcess,DWORD64 Address,PDWORD64 Displacement,PSYMBOL_INFOW Symbol); + WINBOOL IMAGEAPI SymFromToken(HANDLE hProcess,DWORD64 Base,DWORD Token,PSYMBOL_INFO Symbol); + WINBOOL IMAGEAPI SymFromTokenW(HANDLE hProcess,DWORD64 Base,DWORD Token,PSYMBOL_INFOW Symbol); + WINBOOL IMAGEAPI SymFromName(HANDLE hProcess,PCSTR Name,PSYMBOL_INFO Symbol); + WINBOOL IMAGEAPI SymFromNameW(HANDLE hProcess,PCWSTR Name,PSYMBOL_INFOW Symbol); + + typedef WINBOOL (CALLBACK *PSYM_ENUMERATESYMBOLS_CALLBACK)(PSYMBOL_INFO pSymInfo,ULONG SymbolSize,PVOID UserContext); + typedef WINBOOL (CALLBACK *PSYM_ENUMERATESYMBOLS_CALLBACKW)(PSYMBOL_INFOW pSymInfo,ULONG SymbolSize,PVOID UserContext); + + WINBOOL IMAGEAPI SymEnumSymbols(HANDLE hProcess,ULONG64 BaseOfDll,PCSTR Mask,PSYM_ENUMERATESYMBOLS_CALLBACK EnumSymbolsCallback,PVOID UserContext); + WINBOOL IMAGEAPI SymEnumSymbolsW(HANDLE hProcess,ULONG64 BaseOfDll,PCWSTR Mask,PSYM_ENUMERATESYMBOLS_CALLBACKW EnumSymbolsCallback,PVOID UserContext); + WINBOOL IMAGEAPI SymEnumSymbolsForAddr(HANDLE hProcess,DWORD64 Address,PSYM_ENUMERATESYMBOLS_CALLBACK EnumSymbolsCallback,PVOID UserContext); + WINBOOL IMAGEAPI SymEnumSymbolsForAddrW(HANDLE hProcess,DWORD64 Address,PSYM_ENUMERATESYMBOLS_CALLBACKW EnumSymbolsCallback,PVOID UserContext); + +#define SYMENUMFLAG_FULLSRCH 1 +#define SYMENUMFLAG_SPEEDSRCH 2 + + typedef enum _IMAGEHLP_SYMBOL_TYPE_INFO { + TI_GET_SYMTAG, + TI_GET_SYMNAME, + TI_GET_LENGTH, + TI_GET_TYPE, + TI_GET_TYPEID, + TI_GET_BASETYPE, + TI_GET_ARRAYINDEXTYPEID, + TI_FINDCHILDREN, + TI_GET_DATAKIND, + TI_GET_ADDRESSOFFSET, + TI_GET_OFFSET, + TI_GET_VALUE, + TI_GET_COUNT, + TI_GET_CHILDRENCOUNT, + TI_GET_BITPOSITION, + TI_GET_VIRTUALBASECLASS, + TI_GET_VIRTUALTABLESHAPEID, + TI_GET_VIRTUALBASEPOINTEROFFSET, + TI_GET_CLASSPARENTID, + TI_GET_NESTED, + TI_GET_SYMINDEX, + TI_GET_LEXICALPARENT, + TI_GET_ADDRESS, + TI_GET_THISADJUST, + TI_GET_UDTKIND, + TI_IS_EQUIV_TO, + TI_GET_CALLING_CONVENTION + } IMAGEHLP_SYMBOL_TYPE_INFO; + + typedef struct _TI_FINDCHILDREN_PARAMS { + ULONG Count; + ULONG Start; + ULONG ChildId[1]; + } TI_FINDCHILDREN_PARAMS; + + WINBOOL IMAGEAPI SymGetTypeInfo(HANDLE hProcess,DWORD64 ModBase,ULONG TypeId,IMAGEHLP_SYMBOL_TYPE_INFO GetType,PVOID pInfo); + WINBOOL IMAGEAPI SymEnumTypes(HANDLE hProcess,ULONG64 BaseOfDll,PSYM_ENUMERATESYMBOLS_CALLBACK EnumSymbolsCallback,PVOID UserContext); + WINBOOL IMAGEAPI SymEnumTypesW(HANDLE hProcess,ULONG64 BaseOfDll,PSYM_ENUMERATESYMBOLS_CALLBACKW EnumSymbolsCallback,PVOID UserContext); + WINBOOL IMAGEAPI SymGetTypeFromName(HANDLE hProcess,ULONG64 BaseOfDll,PCSTR Name,PSYMBOL_INFO Symbol); + WINBOOL IMAGEAPI SymGetTypeFromNameW(HANDLE hProcess,ULONG64 BaseOfDll,PCWSTR Name,PSYMBOL_INFOW Symbol); + WINBOOL IMAGEAPI SymAddSymbol(HANDLE hProcess,ULONG64 BaseOfDll,PCSTR Name,DWORD64 Address,DWORD Size,DWORD Flags); + WINBOOL IMAGEAPI SymAddSymbolW(HANDLE hProcess,ULONG64 BaseOfDll,PCWSTR Name,DWORD64 Address,DWORD Size,DWORD Flags); + WINBOOL IMAGEAPI SymDeleteSymbol(HANDLE hProcess,ULONG64 BaseOfDll,PCSTR Name,DWORD64 Address,DWORD Flags); + WINBOOL IMAGEAPI SymDeleteSymbolW(HANDLE hProcess,ULONG64 BaseOfDll,PCWSTR Name,DWORD64 Address,DWORD Flags); + + typedef WINBOOL (WINAPI *PDBGHELP_CREATE_USER_DUMP_CALLBACK)(DWORD DataType,PVOID *Data,LPDWORD DataLength,PVOID UserData); + + WINBOOL WINAPI DbgHelpCreateUserDump(LPCSTR FileName,PDBGHELP_CREATE_USER_DUMP_CALLBACK Callback,PVOID UserData); + WINBOOL WINAPI DbgHelpCreateUserDumpW(LPCWSTR FileName,PDBGHELP_CREATE_USER_DUMP_CALLBACK Callback,PVOID UserData); + WINBOOL IMAGEAPI SymGetSymFromAddr64(HANDLE hProcess,DWORD64 qwAddr,PDWORD64 pdwDisplacement,PIMAGEHLP_SYMBOL64 Symbol); + +#ifdef _IMAGEHLP64 +#define SymGetSymFromAddr SymGetSymFromAddr64 +#elif !defined(EBACKTRACE_MINGW32) + WINBOOL IMAGEAPI SymGetSymFromAddr(HANDLE hProcess,DWORD dwAddr,PDWORD pdwDisplacement,PIMAGEHLP_SYMBOL Symbol); +#endif + + WINBOOL IMAGEAPI SymGetSymFromName64(HANDLE hProcess,PCSTR Name,PIMAGEHLP_SYMBOL64 Symbol); + +#ifdef _IMAGEHLP64 +#define SymGetSymFromName SymGetSymFromName64 +#elif !defined(EBACKTRACE_MINGW32) + WINBOOL IMAGEAPI SymGetSymFromName(HANDLE hProcess,PCSTR Name,PIMAGEHLP_SYMBOL Symbol); +#endif + + DBHLP_DEPRECIATED WINBOOL IMAGEAPI FindFileInPath(HANDLE hprocess,PCSTR SearchPath,PCSTR FileName,PVOID id,DWORD two,DWORD three,DWORD flags,PSTR FilePath); + DBHLP_DEPRECIATED WINBOOL IMAGEAPI FindFileInSearchPath(HANDLE hprocess,PCSTR SearchPath,PCSTR FileName,DWORD one,DWORD two,DWORD three,PSTR FilePath); + DBHLP_DEPRECIATED WINBOOL IMAGEAPI SymEnumSym(HANDLE hProcess,ULONG64 BaseOfDll,PSYM_ENUMERATESYMBOLS_CALLBACK EnumSymbolsCallback,PVOID UserContext); + +#ifdef __cplusplus +} +#endif + +#ifndef EBACKTRACE_MINGW32 +#define SYMF_OMAP_GENERATED 0x00000001 +#define SYMF_OMAP_MODIFIED 0x00000002 +#define SYMF_REGISTER 0x00000008 +#define SYMF_REGREL 0x00000010 +#define SYMF_FRAMEREL 0x00000020 +#define SYMF_PARAMETER 0x00000040 +#define SYMF_LOCAL 0x00000080 +#define SYMF_CONSTANT 0x00000100 +#define SYMF_EXPORT 0x00000200 +#define SYMF_FORWARDER 0x00000400 +#define SYMF_FUNCTION 0x00000800 +#define SYMF_VIRTUAL 0x00001000 +#define SYMF_THUNK 0x00002000 +#define SYMF_TLSREL 0x00004000 +#endif + +#define IMAGEHLP_SYMBOL_INFO_VALUEPRESENT 1 +#define IMAGEHLP_SYMBOL_INFO_REGISTER SYMF_REGISTER +#define IMAGEHLP_SYMBOL_INFO_REGRELATIVE SYMF_REGREL +#define IMAGEHLP_SYMBOL_INFO_FRAMERELATIVE SYMF_FRAMEREL +#define IMAGEHLP_SYMBOL_INFO_PARAMETER SYMF_PARAMETER +#define IMAGEHLP_SYMBOL_INFO_LOCAL SYMF_LOCAL +#define IMAGEHLP_SYMBOL_INFO_CONSTANT SYMF_CONSTANT +#define IMAGEHLP_SYMBOL_FUNCTION SYMF_FUNCTION +#define IMAGEHLP_SYMBOL_VIRTUAL SYMF_VIRTUAL +#define IMAGEHLP_SYMBOL_THUNK SYMF_THUNK +#define IMAGEHLP_SYMBOL_INFO_TLSRELATIVE SYMF_TLSREL + +#include + +#define MINIDUMP_SIGNATURE ('PMDM') +#define MINIDUMP_VERSION (42899) + typedef DWORD RVA; + typedef ULONG64 RVA64; + + typedef struct _MINIDUMP_LOCATION_DESCRIPTOR { + ULONG32 DataSize; + RVA Rva; + } MINIDUMP_LOCATION_DESCRIPTOR; + + typedef struct _MINIDUMP_LOCATION_DESCRIPTOR64 { + ULONG64 DataSize; + RVA64 Rva; + } MINIDUMP_LOCATION_DESCRIPTOR64; + + typedef struct _MINIDUMP_MEMORY_DESCRIPTOR { + ULONG64 StartOfMemoryRange; + MINIDUMP_LOCATION_DESCRIPTOR Memory; + } MINIDUMP_MEMORY_DESCRIPTOR,*PMINIDUMP_MEMORY_DESCRIPTOR; + + typedef struct _MINIDUMP_MEMORY_DESCRIPTOR64 { + ULONG64 StartOfMemoryRange; + ULONG64 DataSize; + } MINIDUMP_MEMORY_DESCRIPTOR64,*PMINIDUMP_MEMORY_DESCRIPTOR64; + + typedef struct _MINIDUMP_HEADER { + ULONG32 Signature; + ULONG32 Version; + ULONG32 NumberOfStreams; + RVA StreamDirectoryRva; + ULONG32 CheckSum; + __C89_NAMELESS union { + ULONG32 Reserved; + ULONG32 TimeDateStamp; + }; + ULONG64 Flags; + } MINIDUMP_HEADER,*PMINIDUMP_HEADER; + + typedef struct _MINIDUMP_DIRECTORY { + ULONG32 StreamType; + MINIDUMP_LOCATION_DESCRIPTOR Location; + } MINIDUMP_DIRECTORY,*PMINIDUMP_DIRECTORY; + + typedef struct _MINIDUMP_STRING { + ULONG32 Length; + WCHAR Buffer[0]; + } MINIDUMP_STRING,*PMINIDUMP_STRING; + + typedef enum _MINIDUMP_STREAM_TYPE { + UnusedStream = 0, + ReservedStream0 = 1, + ReservedStream1 = 2, + ThreadListStream = 3, + ModuleListStream = 4, + MemoryListStream = 5, + ExceptionStream = 6, + SystemInfoStream = 7, + ThreadExListStream = 8, + Memory64ListStream = 9, + CommentStreamA = 10, + CommentStreamW = 11, + HandleDataStream = 12, + FunctionTableStream = 13, + UnloadedModuleListStream = 14, + MiscInfoStream = 15, + LastReservedStream = 0xffff + } MINIDUMP_STREAM_TYPE; + + typedef union _CPU_INFORMATION { + struct { + ULONG32 VendorId[3]; + ULONG32 VersionInformation; + ULONG32 FeatureInformation; + ULONG32 AMDExtendedCpuFeatures; + } X86CpuInfo; + struct { + ULONG64 ProcessorFeatures[2]; + } OtherCpuInfo; + } CPU_INFORMATION,*PCPU_INFORMATION; + + typedef struct _MINIDUMP_SYSTEM_INFO { + USHORT ProcessorArchitecture; + USHORT ProcessorLevel; + USHORT ProcessorRevision; + __C89_NAMELESS union { + USHORT Reserved0; + __C89_NAMELESS struct { + UCHAR NumberOfProcessors; + UCHAR ProductType; + }; + }; + ULONG32 MajorVersion; + ULONG32 MinorVersion; + ULONG32 BuildNumber; + ULONG32 PlatformId; + RVA CSDVersionRva; + __C89_NAMELESS union { + ULONG32 Reserved1; + __C89_NAMELESS struct { + USHORT SuiteMask; + USHORT Reserved2; + }; + }; + CPU_INFORMATION Cpu; + } MINIDUMP_SYSTEM_INFO,*PMINIDUMP_SYSTEM_INFO; + + C_ASSERT(sizeof(((PPROCESS_INFORMATION)0)->dwThreadId)==4); + + typedef struct _MINIDUMP_THREAD { + ULONG32 ThreadId; + ULONG32 SuspendCount; + ULONG32 PriorityClass; + ULONG32 Priority; + ULONG64 Teb; + MINIDUMP_MEMORY_DESCRIPTOR Stack; + MINIDUMP_LOCATION_DESCRIPTOR ThreadContext; + } MINIDUMP_THREAD,*PMINIDUMP_THREAD; + + typedef struct _MINIDUMP_THREAD_LIST { + ULONG32 NumberOfThreads; + MINIDUMP_THREAD Threads[0]; + } MINIDUMP_THREAD_LIST,*PMINIDUMP_THREAD_LIST; + + typedef struct _MINIDUMP_THREAD_EX { + ULONG32 ThreadId; + ULONG32 SuspendCount; + ULONG32 PriorityClass; + ULONG32 Priority; + ULONG64 Teb; + MINIDUMP_MEMORY_DESCRIPTOR Stack; + MINIDUMP_LOCATION_DESCRIPTOR ThreadContext; + MINIDUMP_MEMORY_DESCRIPTOR BackingStore; + } MINIDUMP_THREAD_EX,*PMINIDUMP_THREAD_EX; + + typedef struct _MINIDUMP_THREAD_EX_LIST { + ULONG32 NumberOfThreads; + MINIDUMP_THREAD_EX Threads[0]; + } MINIDUMP_THREAD_EX_LIST,*PMINIDUMP_THREAD_EX_LIST; + + typedef struct _MINIDUMP_EXCEPTION { + ULONG32 ExceptionCode; + ULONG32 ExceptionFlags; + ULONG64 ExceptionRecord; + ULONG64 ExceptionAddress; + ULONG32 NumberParameters; + ULONG32 __unusedAlignment; + ULONG64 ExceptionInformation[EXCEPTION_MAXIMUM_PARAMETERS]; + } MINIDUMP_EXCEPTION,*PMINIDUMP_EXCEPTION; + + typedef struct MINIDUMP_EXCEPTION_STREAM { + ULONG32 ThreadId; + ULONG32 __alignment; + MINIDUMP_EXCEPTION ExceptionRecord; + MINIDUMP_LOCATION_DESCRIPTOR ThreadContext; + } MINIDUMP_EXCEPTION_STREAM,*PMINIDUMP_EXCEPTION_STREAM; + + typedef struct _MINIDUMP_MODULE { + ULONG64 BaseOfImage; + ULONG32 SizeOfImage; + ULONG32 CheckSum; + ULONG32 TimeDateStamp; + RVA ModuleNameRva; + VS_FIXEDFILEINFO VersionInfo; + MINIDUMP_LOCATION_DESCRIPTOR CvRecord; + MINIDUMP_LOCATION_DESCRIPTOR MiscRecord; + ULONG64 Reserved0; + ULONG64 Reserved1; + } MINIDUMP_MODULE,*PMINIDUMP_MODULE; + + typedef struct _MINIDUMP_MODULE_LIST { + ULONG32 NumberOfModules; + MINIDUMP_MODULE Modules[0]; + } MINIDUMP_MODULE_LIST,*PMINIDUMP_MODULE_LIST; + + typedef struct _MINIDUMP_MEMORY_LIST { + ULONG32 NumberOfMemoryRanges; + MINIDUMP_MEMORY_DESCRIPTOR MemoryRanges[0]; + } MINIDUMP_MEMORY_LIST,*PMINIDUMP_MEMORY_LIST; + + typedef struct _MINIDUMP_MEMORY64_LIST { + ULONG64 NumberOfMemoryRanges; + RVA64 BaseRva; + MINIDUMP_MEMORY_DESCRIPTOR64 MemoryRanges[0]; + } MINIDUMP_MEMORY64_LIST,*PMINIDUMP_MEMORY64_LIST; + + typedef struct _MINIDUMP_EXCEPTION_INFORMATION { + DWORD ThreadId; + PEXCEPTION_POINTERS ExceptionPointers; + WINBOOL ClientPointers; + } MINIDUMP_EXCEPTION_INFORMATION,*PMINIDUMP_EXCEPTION_INFORMATION; + + typedef struct _MINIDUMP_EXCEPTION_INFORMATION64 { + DWORD ThreadId; + ULONG64 ExceptionRecord; + ULONG64 ContextRecord; + WINBOOL ClientPointers; + } MINIDUMP_EXCEPTION_INFORMATION64,*PMINIDUMP_EXCEPTION_INFORMATION64; + + typedef struct _MINIDUMP_HANDLE_DESCRIPTOR { + ULONG64 Handle; + RVA TypeNameRva; + RVA ObjectNameRva; + ULONG32 Attributes; + ULONG32 GrantedAccess; + ULONG32 HandleCount; + ULONG32 PointerCount; + } MINIDUMP_HANDLE_DESCRIPTOR,*PMINIDUMP_HANDLE_DESCRIPTOR; + + typedef struct _MINIDUMP_HANDLE_DATA_STREAM { + ULONG32 SizeOfHeader; + ULONG32 SizeOfDescriptor; + ULONG32 NumberOfDescriptors; + ULONG32 Reserved; + } MINIDUMP_HANDLE_DATA_STREAM,*PMINIDUMP_HANDLE_DATA_STREAM; + + typedef struct _MINIDUMP_FUNCTION_TABLE_DESCRIPTOR { + ULONG64 MinimumAddress; + ULONG64 MaximumAddress; + ULONG64 BaseAddress; + ULONG32 EntryCount; + ULONG32 SizeOfAlignPad; + } MINIDUMP_FUNCTION_TABLE_DESCRIPTOR,*PMINIDUMP_FUNCTION_TABLE_DESCRIPTOR; + + typedef struct _MINIDUMP_FUNCTION_TABLE_STREAM { + ULONG32 SizeOfHeader; + ULONG32 SizeOfDescriptor; + ULONG32 SizeOfNativeDescriptor; + ULONG32 SizeOfFunctionEntry; + ULONG32 NumberOfDescriptors; + ULONG32 SizeOfAlignPad; + } MINIDUMP_FUNCTION_TABLE_STREAM,*PMINIDUMP_FUNCTION_TABLE_STREAM; + + typedef struct _MINIDUMP_UNLOADED_MODULE { + ULONG64 BaseOfImage; + ULONG32 SizeOfImage; + ULONG32 CheckSum; + ULONG32 TimeDateStamp; + RVA ModuleNameRva; + } MINIDUMP_UNLOADED_MODULE,*PMINIDUMP_UNLOADED_MODULE; + + typedef struct _MINIDUMP_UNLOADED_MODULE_LIST { + ULONG32 SizeOfHeader; + ULONG32 SizeOfEntry; + ULONG32 NumberOfEntries; + } MINIDUMP_UNLOADED_MODULE_LIST,*PMINIDUMP_UNLOADED_MODULE_LIST; + +#define MINIDUMP_MISC1_PROCESS_ID 0x00000001 +#define MINIDUMP_MISC1_PROCESS_TIMES 0x00000002 +#define MINIDUMP_MISC1_PROCESSOR_POWER_INFO 0x00000004 + + typedef struct _MINIDUMP_MISC_INFO { + ULONG32 SizeOfInfo; + ULONG32 Flags1; + ULONG32 ProcessId; + ULONG32 ProcessCreateTime; + ULONG32 ProcessUserTime; + ULONG32 ProcessKernelTime; + } MINIDUMP_MISC_INFO,*PMINIDUMP_MISC_INFO; + + typedef struct _MINIDUMP_USER_RECORD { + ULONG32 Type; + MINIDUMP_LOCATION_DESCRIPTOR Memory; + } MINIDUMP_USER_RECORD,*PMINIDUMP_USER_RECORD; + + typedef struct _MINIDUMP_USER_STREAM { + ULONG32 Type; + ULONG BufferSize; + PVOID Buffer; + } MINIDUMP_USER_STREAM,*PMINIDUMP_USER_STREAM; + + typedef struct _MINIDUMP_USER_STREAM_INFORMATION { + ULONG UserStreamCount; + PMINIDUMP_USER_STREAM UserStreamArray; + } MINIDUMP_USER_STREAM_INFORMATION,*PMINIDUMP_USER_STREAM_INFORMATION; + + typedef enum _MINIDUMP_CALLBACK_TYPE { + ModuleCallback, + ThreadCallback, + ThreadExCallback, + IncludeThreadCallback, + IncludeModuleCallback, + MemoryCallback, + CancelCallback, + WriteKernelMinidumpCallback, + KernelMinidumpStatusCallback, + RemoveMemoryCallback, + IncludeVmRegionCallback, + IoStartCallback, + IoWriteAllCallback, + IoFinishCallback, + ReadMemoryFailureCallback, + SecondaryFlagsCallback + } MINIDUMP_CALLBACK_TYPE; + + typedef struct _MINIDUMP_THREAD_CALLBACK { + ULONG ThreadId; + HANDLE ThreadHandle; + CONTEXT Context; + ULONG SizeOfContext; + ULONG64 StackBase; + ULONG64 StackEnd; + } MINIDUMP_THREAD_CALLBACK,*PMINIDUMP_THREAD_CALLBACK; + + typedef struct _MINIDUMP_THREAD_EX_CALLBACK { + ULONG ThreadId; + HANDLE ThreadHandle; + CONTEXT Context; + ULONG SizeOfContext; + ULONG64 StackBase; + ULONG64 StackEnd; + ULONG64 BackingStoreBase; + ULONG64 BackingStoreEnd; + } MINIDUMP_THREAD_EX_CALLBACK,*PMINIDUMP_THREAD_EX_CALLBACK; + + typedef struct _MINIDUMP_INCLUDE_THREAD_CALLBACK { + ULONG ThreadId; + } MINIDUMP_INCLUDE_THREAD_CALLBACK,*PMINIDUMP_INCLUDE_THREAD_CALLBACK; + + typedef enum _THREAD_WRITE_FLAGS { + ThreadWriteThread = 0x0001, + ThreadWriteStack = 0x0002, + ThreadWriteContext = 0x0004, + ThreadWriteBackingStore = 0x0008, + ThreadWriteInstructionWindow = 0x0010, + ThreadWriteThreadData = 0x0020, + ThreadWriteThreadInfo = 0x0040 + } THREAD_WRITE_FLAGS; + + typedef struct _MINIDUMP_MODULE_CALLBACK { + PWCHAR FullPath; + ULONG64 BaseOfImage; + ULONG SizeOfImage; + ULONG CheckSum; + ULONG TimeDateStamp; + VS_FIXEDFILEINFO VersionInfo; + PVOID CvRecord; + ULONG SizeOfCvRecord; + PVOID MiscRecord; + ULONG SizeOfMiscRecord; + } MINIDUMP_MODULE_CALLBACK,*PMINIDUMP_MODULE_CALLBACK; + + typedef struct _MINIDUMP_INCLUDE_MODULE_CALLBACK { + ULONG64 BaseOfImage; + } MINIDUMP_INCLUDE_MODULE_CALLBACK,*PMINIDUMP_INCLUDE_MODULE_CALLBACK; + + typedef enum _MODULE_WRITE_FLAGS { + ModuleWriteModule = 0x0001, + ModuleWriteDataSeg = 0x0002, + ModuleWriteMiscRecord = 0x0004, + ModuleWriteCvRecord = 0x0008, + ModuleReferencedByMemory = 0x0010, + ModuleWriteTlsData = 0x0020, + ModuleWriteCodeSegs = 0x0040 + } MODULE_WRITE_FLAGS; + + typedef enum _MINIDUMP_SECONDARY_FLAGS { + MiniSecondaryWithoutPowerInfo = 0x00000001 + } MINIDUMP_SECONDARY_FLAGS; + + typedef struct _MINIDUMP_CALLBACK_INPUT { + ULONG ProcessId; + HANDLE ProcessHandle; + ULONG CallbackType; + __C89_NAMELESS union { + MINIDUMP_THREAD_CALLBACK Thread; + MINIDUMP_THREAD_EX_CALLBACK ThreadEx; + MINIDUMP_MODULE_CALLBACK Module; + MINIDUMP_INCLUDE_THREAD_CALLBACK IncludeThread; + MINIDUMP_INCLUDE_MODULE_CALLBACK IncludeModule; + }; + } MINIDUMP_CALLBACK_INPUT,*PMINIDUMP_CALLBACK_INPUT; + +typedef struct _MINIDUMP_MEMORY_INFO { + ULONG64 BaseAddress; + ULONG64 AllocationBase; + ULONG32 AllocationProtect; + ULONG32 __alignment1; + ULONG64 RegionSize; + ULONG32 State; + ULONG32 Protect; + ULONG32 Type; + ULONG32 __alignment2; +} MINIDUMP_MEMORY_INFO, *PMINIDUMP_MEMORY_INFO; + +typedef struct _MINIDUMP_MISC_INFO_2 { + ULONG32 SizeOfInfo; + ULONG32 Flags1; + ULONG32 ProcessId; + ULONG32 ProcessCreateTime; + ULONG32 ProcessUserTime; + ULONG32 ProcessKernelTime; + ULONG32 ProcessorMaxMhz; + ULONG32 ProcessorCurrentMhz; + ULONG32 ProcessorMhzLimit; + ULONG32 ProcessorMaxIdleState; + ULONG32 ProcessorCurrentIdleState; +} MINIDUMP_MISC_INFO_2, *PMINIDUMP_MISC_INFO_2; + +typedef struct _MINIDUMP_MEMORY_INFO_LIST { + ULONG SizeOfHeader; + ULONG SizeOfEntry; + ULONG64 NumberOfEntries; +} MINIDUMP_MEMORY_INFO_LIST, *PMINIDUMP_MEMORY_INFO_LIST; + + typedef struct _MINIDUMP_CALLBACK_OUTPUT { + __C89_NAMELESS union { + ULONG ModuleWriteFlags; + ULONG ThreadWriteFlags; + ULONG SecondaryFlags; + __C89_NAMELESS struct { + ULONG64 MemoryBase; + ULONG MemorySize; + }; + __C89_NAMELESS struct { + WINBOOL CheckCancel; + WINBOOL Cancel; + }; + HANDLE Handle; + }; + __C89_NAMELESS struct { + MINIDUMP_MEMORY_INFO VmRegion; + WINBOOL Continue; + }; + HRESULT Status; + } MINIDUMP_CALLBACK_OUTPUT, *PMINIDUMP_CALLBACK_OUTPUT; + + typedef enum _MINIDUMP_TYPE { + MiniDumpNormal = 0x00000000, + MiniDumpWithDataSegs = 0x00000001, + MiniDumpWithFullMemory = 0x00000002, + MiniDumpWithHandleData = 0x00000004, + MiniDumpFilterMemory = 0x00000008, + MiniDumpScanMemory = 0x00000010, + MiniDumpWithUnloadedModules = 0x00000020, + MiniDumpWithIndirectlyReferencedMemory = 0x00000040, + MiniDumpFilterModulePaths = 0x00000080, + MiniDumpWithProcessThreadData = 0x00000100, + MiniDumpWithPrivateReadWriteMemory = 0x00000200, + MiniDumpWithoutOptionalData = 0x00000400, + MiniDumpWithFullMemoryInfo = 0x00000800, + MiniDumpWithThreadInfo = 0x00001000, + MiniDumpWithCodeSegs = 0x00002000, + MiniDumpWithoutAuxiliaryState = 0x00004000, + MiniDumpWithFullAuxiliaryState = 0x00008000, + MiniDumpWithPrivateWriteCopyMemory = 0x00010000, + MiniDumpIgnoreInaccessibleMemory = 0x00020000, + MiniDumpWithTokenInformation = 0x00040000 + } MINIDUMP_TYPE; + +#define MINIDUMP_THREAD_INFO_ERROR_THREAD 0x00000001 +#define MINIDUMP_THREAD_INFO_WRITING_THREAD 0x00000002 +#define MINIDUMP_THREAD_INFO_EXITED_THREAD 0x00000004 +#define MINIDUMP_THREAD_INFO_INVALID_INFO 0x00000008 +#define MINIDUMP_THREAD_INFO_INVALID_CONTEXT 0x00000010 +#define MINIDUMP_THREAD_INFO_INVALID_TEB 0x00000020 + +typedef struct _MINIDUMP_THREAD_INFO { + ULONG32 ThreadId; + ULONG32 DumpFlags; + ULONG32 DumpError; + ULONG32 ExitStatus; + ULONG64 CreateTime; + ULONG64 ExitTime; + ULONG64 KernelTime; + ULONG64 UserTime; + ULONG64 StartAddress; + ULONG64 Affinity; +} MINIDUMP_THREAD_INFO, *PMINIDUMP_THREAD_INFO; + +typedef struct _MINIDUMP_THREAD_INFO_LIST { + ULONG SizeOfHeader; + ULONG SizeOfEntry; + ULONG NumberOfEntries; +} MINIDUMP_THREAD_INFO_LIST, *PMINIDUMP_THREAD_INFO_LIST; + +#ifdef __cplusplus +extern "C" { +#endif + + typedef WINBOOL (WINAPI *MINIDUMP_CALLBACK_ROUTINE)(PVOID CallbackParam,CONST PMINIDUMP_CALLBACK_INPUT CallbackInput,PMINIDUMP_CALLBACK_OUTPUT CallbackOutput); + + typedef struct _MINIDUMP_CALLBACK_INFORMATION { + MINIDUMP_CALLBACK_ROUTINE CallbackRoutine; + PVOID CallbackParam; + } MINIDUMP_CALLBACK_INFORMATION,*PMINIDUMP_CALLBACK_INFORMATION; + +#define RVA_TO_ADDR(Mapping,Rva) ((PVOID)(((ULONG_PTR) (Mapping)) + (Rva))) + + WINBOOL WINAPI MiniDumpWriteDump(HANDLE hProcess,DWORD ProcessId,HANDLE hFile,MINIDUMP_TYPE DumpType,CONST PMINIDUMP_EXCEPTION_INFORMATION ExceptionParam,CONST PMINIDUMP_USER_STREAM_INFORMATION UserStreamParam,CONST PMINIDUMP_CALLBACK_INFORMATION CallbackParam); + WINBOOL WINAPI MiniDumpReadDumpStream(PVOID BaseOfDump,ULONG StreamNumber,PMINIDUMP_DIRECTORY *Dir,PVOID *StreamPointer,ULONG *StreamSize); + +WINBOOL WINAPI EnumerateLoadedModulesEx( + HANDLE hProcess, + PENUMLOADED_MODULES_CALLBACK64 EnumLoadedModulesCallback, + PVOID UserContext +); + +WINBOOL WINAPI EnumerateLoadedModulesExW( + HANDLE hProcess, + PENUMLOADED_MODULES_CALLBACKW64 EnumLoadedModulesCallback, + PVOID UserContext +); + +WINBOOL WINAPI SymAddSourceStream( + HANDLE hProcess, + ULONG64 Base, + PCSTR StreamFile, + PBYTE Buffer, + size_t Size +); + +WINBOOL WINAPI SymAddSourceStreamW( + HANDLE hProcess, + ULONG64 Base, + PCWSTR StreamFile, + PBYTE Buffer, + size_t Size +); + +WINBOOL WINAPI SymEnumSourceLines( + HANDLE hProcess, + ULONG64 Base, + PCSTR Obj, + PCSTR File, + DWORD Line, + DWORD Flags, + PSYM_ENUMLINES_CALLBACK EnumLinesCallback, + PVOID UserContext +); + +WINBOOL WINAPI SymEnumSourceLinesW( + HANDLE hProcess, + ULONG64 Base, + PCWSTR Obj, + PCWSTR File, + DWORD Line, + DWORD Flags, + PSYM_ENUMLINES_CALLBACKW EnumLinesCallback, + PVOID UserContext +); + +WINBOOL WINAPI SymEnumTypesByName( + HANDLE hProcess, + ULONG64 BaseOfDll, + PCSTR mask, + PSYM_ENUMERATESYMBOLS_CALLBACK EnumSymbolsCallback, + PVOID UserContext +); + +WINBOOL WINAPI SymEnumTypesByNameW( + HANDLE hProcess, + ULONG64 BaseOfDll, + PCSTR mask, + PSYM_ENUMERATESYMBOLS_CALLBACKW EnumSymbolsCallback, + PVOID UserContext +); + +HANDLE WINAPI SymFindDebugInfoFile( + HANDLE hProcess, + PCSTR FileName, + PSTR DebugFilePath, + PFIND_DEBUG_FILE_CALLBACK Callback, + PVOID CallerData +); + +HANDLE WINAPI SymFindDebugInfoFileW( + HANDLE hProcess, + PCWSTR FileName, + PWSTR DebugFilePath, + PFIND_DEBUG_FILE_CALLBACKW Callback, + PVOID CallerData +); + +HANDLE WINAPI SymFindExecutableImage( + HANDLE hProcess, + PCSTR FileName, + PSTR ImageFilePath, + PFIND_EXE_FILE_CALLBACK Callback, + PVOID CallerData +); + +HANDLE WINAPI SymFindExecutableImageW( + HANDLE hProcess, + PCWSTR FileName, + PWSTR ImageFilePath, + PFIND_EXE_FILE_CALLBACKW Callback, + PVOID CallerData +); + +WINBOOL WINAPI SymFromIndex( + HANDLE hProcess, + ULONG64 BaseOfDll, + DWORD Index, + PSYMBOL_INFO Symbol +); + +WINBOOL WINAPI SymFromIndexW( + HANDLE hProcess, + ULONG64 BaseOfDll, + DWORD Index, + PSYMBOL_INFOW Symbol +); + +WINBOOL WINAPI SymGetScope( + HANDLE hProcess, + ULONG64 BaseOfDll, + DWORD Index, + PSYMBOL_INFO Symbol +); + +WINBOOL WINAPI SymGetScopeW( + HANDLE hProcess, + ULONG64 BaseOfDll, + DWORD Index, + PSYMBOL_INFOW Symbol +); + +WINBOOL WINAPI SymGetSourceFileFromToken( + HANDLE hProcess, + PVOID Token, + PCSTR Params, + PSTR FilePath, + DWORD Size +); + +WINBOOL WINAPI SymGetSourceFileFromTokenW( + HANDLE hProcess, + PVOID Token, + PCWSTR Params, + PWSTR FilePath, + DWORD Size +); + +WINBOOL WINAPI SymGetSourceFileToken( + HANDLE hProcess, + ULONG64 Base, + PCSTR FileSpec, + PVOID *Token, + DWORD *Size +); + +WINBOOL WINAPI SymGetSourceFileTokenW( + HANDLE hProcess, + ULONG64 Base, + PCWSTR FileSpec, + PVOID *Token, + DWORD *Size +); + +WINBOOL WINAPI SymGetSourceFile( + HANDLE hProcess, + ULONG64 Base, + PCSTR Params, + PCSTR FileSpec, + PSTR FilePath, + DWORD Size +); + +WINBOOL WINAPI SymGetSourceFileW( + HANDLE hProcess, + ULONG64 Base, + PCWSTR Params, + PCWSTR FileSpec, + PWSTR FilePath, + DWORD Size +); + +WINBOOL WINAPI SymGetSourceVarFromToken( + HANDLE hProcess, + PVOID Token, + PCSTR Params, + PCSTR VarName, + PSTR Value, + DWORD Size +); + +WINBOOL WINAPI SymGetSourceVarFromTokenW( + HANDLE hProcess, + PVOID Token, + PCWSTR Params, + PCWSTR VarName, + PWSTR Value, + DWORD Size +); + +WINBOOL WINAPI SymGetSymbolFile( + HANDLE hProcess, + PCSTR SymPath, + PCSTR ImageFile, + DWORD Type, + PSTR SymbolFile, + size_t cSymbolFile, + PSTR DbgFile, + size_t cDbgFile +); + +WINBOOL WINAPI SymGetSymbolFileW( + HANDLE hProcess, + PCWSTR SymPath, + PCWSTR ImageFile, + DWORD Type, + PWSTR SymbolFile, + size_t cSymbolFile, + PWSTR DbgFile, + size_t cDbgFile +); + +WINBOOL WINAPI SymNext( + HANDLE hProcess, + PSYMBOL_INFO Symbol +); + +WINBOOL WINAPI SymNextW( + HANDLE hProcess, + PSYMBOL_INFOW Symbol +); + +WINBOOL WINAPI SymPrev( + HANDLE hProcess, + PSYMBOL_INFO Symbol +); + +WINBOOL WINAPI SymPrevW( + HANDLE hProcess, + PSYMBOL_INFOW Symbol +); + +WINBOOL WINAPI SymRefreshModuleList( + HANDLE hProcess +); + +#define SYMSEARCH_MASKOBJS 0x01 +#define SYMSEARCH_RECURSE 0x02 +#define SYMSEARCH_GLOBALSONLY 0x04 +#define SYMSEARCH_ALLITEMS 0x08 + +WINBOOL WINAPI SymSearch( + HANDLE hProcess, + ULONG64 BaseOfDll, + DWORD Index, + DWORD SymTag, + PCSTR Mask, + DWORD64 Address, + PSYM_ENUMERATESYMBOLS_CALLBACK EnumSymbolsCallback, + PVOID UserContext, + DWORD Options +); + +WINBOOL WINAPI SymSearchW( + HANDLE hProcess, + ULONG64 BaseOfDll, + DWORD Index, + DWORD SymTag, + PCWSTR Mask, + DWORD64 Address, + PSYM_ENUMERATESYMBOLS_CALLBACKW EnumSymbolsCallback, + PVOID UserContext, + DWORD Options +); + +WINBOOL WINAPI SymSrvGetFileIndexString( + HANDLE hProcess, + PCSTR SrvPath, + PCSTR File, + PSTR Index, + size_t Size, + DWORD Flags +); + +WINBOOL WINAPI SymSrvGetFileIndexStringW( + HANDLE hProcess, + PCWSTR SrvPath, + PCWSTR File, + PWSTR Index, + size_t Size, + DWORD Flags +); + +#ifdef DBGHELP_TRANSLATE_TCHAR +#define SymSrvGetFileIndexString SymSrvGetFileIndexStringW +#endif + +WINBOOL WINAPI SymSrvGetFileIndexInfo( + PCSTR File, + PSYMSRV_INDEX_INFO Info, + DWORD Flags +); + +WINBOOL WINAPI SymSrvGetFileIndexInfoW( + PCWSTR File, + PSYMSRV_INDEX_INFOW Info, + DWORD Flags +); + +#ifdef DBGHELP_TRANSLATE_TCHAR +#define SymSrvGetFileIndexInfo SymSrvGetFileIndexInfoW +#endif + +WINBOOL WINAPI SymSrvGetFileIndexes( + PCTSTR File, + GUID *Id, + DWORD *Val1, + DWORD *Val2, + DWORD Flags +); + +WINBOOL WINAPI SymSrvGetFileIndexesW( + PCWSTR File, + GUID *Id, + DWORD *Val1, + DWORD *Val2, + DWORD Flags +); + +#ifdef DBGHELP_TRANSLATE_TCHAR +#define SymSrvGetFileIndexes SymSrvGetFileIndexesW +#endif + +PCSTR WINAPI SymSrvGetSupplement( + HANDLE hProcess, + PCSTR SymPath, + PCSTR Node, + PCSTR File +); + +PCWSTR WINAPI SymSrvGetSupplementW( + HANDLE hProcess, + PCWSTR SymPath, + PCWSTR Node, + PCWSTR File +); + +#ifdef DBGHELP_TRANSLATE_TCHAR +#define SymSrvGetSupplement SymSrvGetSupplementW +#endif + +WINBOOL WINAPI SymSrvIsStore( + HANDLE hProcess, + PCSTR path +); + +WINBOOL WINAPI SymSrvIsStoreW( + HANDLE hProcess, + PCWSTR path +); + +#ifdef DBGHELP_TRANSLATE_TCHAR +#define SymSrvIsStore SymSrvIsStoreW +#endif + +PCSTR WINAPI SymSrvStoreFile( + HANDLE hProcess, + PCSTR SrvPath, + PCSTR File, + DWORD Flags +); + +PCWSTR WINAPI SymSrvStoreFileW( + HANDLE hProcess, + PCWSTR SrvPath, + PCWSTR File, + DWORD Flags +); + +#define SYMSTOREOPT_COMPRESS 0x01 +#define SYMSTOREOPT_OVERWRITE 0x02 +#define SYMSTOREOPT_RETURNINDEX 0x04 +#define SYMSTOREOPT_POINTER 0x08 +#define SYMSTOREOPT_PASS_IF_EXISTS 0x40 + +#ifdef DBGHELP_TRANSLATE_TCHAR +#define SymSrvStoreFile SymSrvStoreFileW +#endif + +PCSTR WINAPI SymSrvStoreSupplement( + HANDLE hProcess, + const PCTSTR SymPath, + PCSTR Node, + PCSTR File, + DWORD Flags +); + +PCWSTR WINAPI SymSrvStoreSupplementW( + HANDLE hProcess, + const PCWSTR SymPath, + PCWSTR Node, + PCWSTR File, + DWORD Flags +); + +#ifdef DBGHELP_TRANSLATE_TCHAR +#define SymSrvStoreSupplement SymSrvStoreSupplementW +#endif + +PCSTR WINAPI SymSrvDeltaName( + HANDLE hProcess, + PCSTR SymPath, + PCSTR Type, + PCSTR File1, + PCSTR File2 +); + +PCWSTR WINAPI SymSrvDeltaNameW( + HANDLE hProcess, + PCWSTR SymPath, + PCWSTR Type, + PCWSTR File1, + PCWSTR File2 +); + +#ifdef DBGHELP_TRANSLATE_TCHAR +#define SymSrvDeltaName SymSrvDeltaNameW +#endif + +#include + +#ifdef __cplusplus +} +#endif + +#endif diff --git a/bin/windows/vpx/src/backtrace.c b/bin/windows/vpx/src/backtrace.c new file mode 100644 index 0000000000..4bbe88bbe9 --- /dev/null +++ b/bin/windows/vpx/src/backtrace.c @@ -0,0 +1,702 @@ +/* + Copyright (c) 2010 , + Cloud Wu . All rights reserved. + + http://www.codingnow.com + + Use, modification and distribution are subject to the "New BSD License" + as listed at . + + filename: backtrace.c + + build command: gcc -O2 -shared -Wall -o backtrace.dll backtrace.c -lbfd -liberty -limagehlp + + how to use: Call LoadLibraryA("backtrace.dll"); at beginning of your program . + + */ + +/* modified from original for EDuke32 */ + +// warnings cleaned up, ported to 64-bit, and heavily extended by Hendricks266 + +#include +#include +#include + +// Tenuous: MinGW provides _IMAGEHLP_H while MinGW-w64 defines _IMAGEHLP_. +#ifdef _IMAGEHLP_H +# define EBACKTRACE_MINGW32 +#endif +#ifdef _IMAGEHLP_ +# define EBACKTRACE_MINGW_W64 +#endif +#if defined(EBACKTRACE_MINGW32) && !defined(EBACKTRACE_MINGW_W64) +# include "_dbg_common.h" +#endif + +#ifndef PACKAGE +# define PACKAGE EBACKTRACE1 +#endif +#ifndef PACKAGE_VERSION +# define PACKAGE_VERSION 1 +#endif + +#if defined(_M_X64) || defined(__amd64__) || defined(__x86_64__) || defined(_WIN64) +# define EBACKTRACE64 +#endif + +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include +#include + +#include + +#ifndef DBG_PRINTEXCEPTION_C +# define DBG_PRINTEXCEPTION_C (0x40010006) +#endif +#ifndef MS_VC_EXCEPTION +# define MS_VC_EXCEPTION 1080890248 +#endif + +#if defined __GNUC__ || defined __clang__ +# define ATTRIBUTE(attrlist) __attribute__(attrlist) +#else +# define ATTRIBUTE(attrlist) +#endif + +#define BUFFER_MAX (16*1024) + +struct bfd_ctx { + bfd * handle; + asymbol ** symbol; +}; + +struct bfd_set { + char * name; + struct bfd_ctx * bc; + struct bfd_set *next; +}; + +struct find_info { + asymbol **symbol; + bfd_vma counter; + const char *file; + const char *func; + unsigned line; +}; + +struct output_buffer { + char * buf; + size_t sz; + size_t ptr; +}; + +static void +output_init(struct output_buffer *ob, char * buf, size_t sz) +{ + ob->buf = buf; + ob->sz = sz; + ob->ptr = 0; + ob->buf[0] = '\0'; +} + +static void +output_print(struct output_buffer *ob, const char * format, ...) +{ + va_list ap; + + if (ob->sz == ob->ptr) + return; + ob->buf[ob->ptr] = '\0'; + va_start(ap,format); + vsnprintf(ob->buf + ob->ptr , ob->sz - ob->ptr , format, ap); + va_end(ap); + + ob->ptr = strlen(ob->buf + ob->ptr) + ob->ptr; +} + +static void +lookup_section(bfd *abfd, asection *sec, void *opaque_data) +{ + struct find_info *data = opaque_data; + bfd_vma vma; + + if (data->func) + return; + + if (!(bfd_get_section_flags(abfd, sec) & SEC_ALLOC)) + return; + + vma = bfd_get_section_vma(abfd, sec); + if (data->counter < vma || vma + bfd_get_section_size(sec) <= data->counter) + return; + + bfd_find_nearest_line(abfd, sec, data->symbol, data->counter - vma, &(data->file), &(data->func), &(data->line)); +} + +static void +find(struct bfd_ctx * b, DWORD offset, const char **file, const char **func, unsigned *line) +{ + struct find_info data; + data.func = NULL; + data.symbol = b->symbol; + data.counter = offset; + data.file = NULL; + data.func = NULL; + data.line = 0; + + bfd_map_over_sections(b->handle, &lookup_section, &data); + if (file) { + *file = data.file; + } + if (func) { + *func = data.func; + } + if (line) { + *line = data.line; + } +} + +static int +init_bfd_ctx(struct bfd_ctx *bc, const char * procname, struct output_buffer *ob) +{ + int r1, r2, r3; + bfd *b; + void *symbol_table; + unsigned dummy = 0; + bc->handle = NULL; + bc->symbol = NULL; + + b = bfd_openr(procname, 0); + if (!b) { + output_print(ob,"Failed to open bfd from (%s)\n" , procname); + return 1; + } + + r1 = bfd_check_format(b, bfd_object); + r2 = bfd_check_format_matches(b, bfd_object, NULL); + r3 = bfd_get_file_flags(b) & HAS_SYMS; + + if (!(r1 && r2 && r3)) { + bfd_close(b); + if (!(r1 && r2)) + output_print(ob,"Failed to init bfd from (%s): %d %d %d\n", procname, r1, r2, r3); + return 1; + } + + if (bfd_read_minisymbols(b, FALSE, &symbol_table, &dummy) == 0) { + if (bfd_read_minisymbols(b, TRUE, &symbol_table, &dummy) < 0) { + free(symbol_table); + bfd_close(b); + output_print(ob,"Failed to read symbols from (%s)\n", procname); + return 1; + } + } + + bc->handle = b; + bc->symbol = symbol_table; + + return 0; +} + +static void +close_bfd_ctx(struct bfd_ctx *bc) +{ + if (bc) { + if (bc->symbol) { + free(bc->symbol); + } + if (bc->handle) { + bfd_close(bc->handle); + } + } +} + +static struct bfd_ctx * +get_bc(struct output_buffer *ob , struct bfd_set *set , const char *procname) +{ + struct bfd_ctx bc; + while(set->name) { + if (strcmp(set->name , procname) == 0) { + return set->bc; + } + set = set->next; + } + if (init_bfd_ctx(&bc, procname , ob)) { + return NULL; + } + set->next = calloc(1, sizeof(*set)); + set->bc = malloc(sizeof(struct bfd_ctx)); + memcpy(set->bc, &bc, sizeof(bc)); + set->name = strdup(procname); + + return set->bc; +} + +static void +release_set(struct bfd_set *set) +{ + while(set) { + struct bfd_set * temp = set->next; + if (set->name) + free(set->name); + close_bfd_ctx(set->bc); + free(set); + set = temp; + } +} + +static char procname[MAX_PATH]; + +#ifdef EBACKTRACE64 +# define MachineType IMAGE_FILE_MACHINE_AMD64 +# define MAYBE64(x) x ## 64 +#else +# define MachineType IMAGE_FILE_MACHINE_I386 +# define MAYBE64(x) x +#endif + +static void +_backtrace(struct output_buffer *ob, struct bfd_set *set, int depth , LPCONTEXT context) +{ + MAYBE64(STACKFRAME) frame; + HANDLE process, thread; + char symbol_buffer[sizeof(MAYBE64(IMAGEHLP_SYMBOL)) + 255]; + char module_name_raw[MAX_PATH]; + struct bfd_ctx *bc = NULL; + + GetModuleFileNameA(NULL, procname, sizeof procname); + + memset(&frame,0,sizeof(frame)); + +#ifdef EBACKTRACE64 + frame.AddrPC.Offset = context->Rip; + frame.AddrStack.Offset = context->Rsp; + frame.AddrFrame.Offset = context->Rbp; +#else + frame.AddrPC.Offset = context->Eip; + frame.AddrStack.Offset = context->Esp; + frame.AddrFrame.Offset = context->Ebp; +#endif + + frame.AddrPC.Mode = AddrModeFlat; + frame.AddrStack.Mode = AddrModeFlat; + frame.AddrFrame.Mode = AddrModeFlat; + + process = GetCurrentProcess(); + thread = GetCurrentThread(); + + while(MAYBE64(StackWalk)(MachineType, + process, + thread, + &frame, + context, + NULL, + MAYBE64(SymFunctionTableAccess), + MAYBE64(SymGetModuleBase), NULL)) { + MAYBE64(IMAGEHLP_SYMBOL) *symbol; + MAYBE64(DWORD) module_base; + const char * module_name = "[unknown module]"; + + const char * file = NULL; + const char * func = NULL; + unsigned line = 0; + + --depth; + if (depth < 0) + break; + + symbol = (MAYBE64(IMAGEHLP_SYMBOL) *)symbol_buffer; + symbol->SizeOfStruct = (sizeof *symbol) + 255; + symbol->MaxNameLength = 254; + + module_base = MAYBE64(SymGetModuleBase)(process, frame.AddrPC.Offset); + + if (module_base && + GetModuleFileNameA((HINSTANCE)(intptr_t)module_base, module_name_raw, MAX_PATH)) { + module_name = module_name_raw; + bc = get_bc(ob, set, module_name); + } + + if (bc) { + find(bc,frame.AddrPC.Offset,&file,&func,&line); + } + + if (file == NULL) { + MAYBE64(DWORD) dummy = 0; + if (MAYBE64(SymGetSymFromAddr)(process, frame.AddrPC.Offset, &dummy, symbol)) { + file = symbol->Name; + } + else { + file = "[unknown file]"; + } + } + + output_print(ob,"0x%p : %s : %s", frame.AddrPC.Offset, module_name, file); + if (func != NULL) + output_print(ob, " (%d) : in function (%s)", line, func); + output_print(ob, "\n"); + } +} + +static LPTSTR FormatErrorMessage(DWORD dwMessageId) +{ + LPTSTR lpBuffer = NULL; + + // adapted from http://stackoverflow.com/a/455533 + FormatMessage( + FORMAT_MESSAGE_FROM_SYSTEM + |FORMAT_MESSAGE_ALLOCATE_BUFFER + |FORMAT_MESSAGE_IGNORE_INSERTS, + NULL, + dwMessageId, + MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), // MAKELANGID(LANG_ENGLISH, SUBLANG_ENGLISH_US) + (LPTSTR)&lpBuffer, + 0, + NULL); + + return lpBuffer; // must be LocalFree()'d by caller +} + +static LPTSTR FormatExceptionCodeMessage(DWORD dwMessageId) +{ + LPTSTR lpBuffer = NULL; + + FormatMessage( + FORMAT_MESSAGE_FROM_HMODULE + |FORMAT_MESSAGE_ALLOCATE_BUFFER + |FORMAT_MESSAGE_IGNORE_INSERTS, + GetModuleHandleA("ntdll.dll"), + dwMessageId, + MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), // MAKELANGID(LANG_ENGLISH, SUBLANG_ENGLISH_US) + (LPTSTR)&lpBuffer, + 0, + NULL); + + return lpBuffer; // must be LocalFree()'d by caller +} + + +// adapted from http://www.catch22.net/tuts/custom-messagebox +static HHOOK hMsgBoxHook; + +LRESULT CALLBACK CBTProc(int nCode, WPARAM wParam, LPARAM lParam) +{ + if (nCode < 0) + return CallNextHookEx(hMsgBoxHook, nCode, wParam, lParam); + + switch (nCode) + { + case HCBT_ACTIVATE: + { + // Get handle to the message box! + HWND hwnd = (HWND)wParam; + + // Do customization! + SetWindowTextA(GetDlgItem(hwnd, IDYES), "Quit"); + SetWindowTextA(GetDlgItem(hwnd, IDNO), "Continue"); + SetWindowTextA(GetDlgItem(hwnd, IDCANCEL), "Ignore"); + return 0; + } + break; + } + + // Call the next hook, if there is one + return CallNextHookEx(hMsgBoxHook, nCode, wParam, lParam); +} + +int ExceptionMessage(TCHAR *szText, TCHAR *szCaption) +{ + int retval; + + // Install a window hook, so we can intercept the message-box + // creation, and customize it + hMsgBoxHook = SetWindowsHookEx( + WH_CBT, + CBTProc, + NULL, + GetCurrentThreadId() // Only install for THIS thread!!! + ); + + // Display a standard message box + retval = MessageBoxA(NULL, szText, szCaption, MB_YESNOCANCEL|MB_ICONERROR|MB_TASKMODAL); + + // remove the window hook + UnhookWindowsHookEx(hMsgBoxHook); + + return retval; +} + +static char crashlogfilename[MAX_PATH] = "crash.log"; +static char propername[MAX_PATH] = "this application"; + +__declspec(dllexport) void SetTechnicalName(const char* input) +{ + snprintf(crashlogfilename, MAX_PATH, "%s.crash.log", input); +} +__declspec(dllexport) void SetProperName(const char* input) +{ + strncpy(propername, input, MAX_PATH); +} + +static char * g_output = NULL; +static PVOID g_prev = NULL; + +static LONG WINAPI +exception_filter(LPEXCEPTION_POINTERS info) +{ + struct output_buffer ob; + int logfd, written, msgboxID; + PEXCEPTION_RECORD exception; + BOOL initialized = FALSE; + char *ExceptionPrinted; + + for (exception = info->ExceptionRecord; exception != NULL; exception = exception->ExceptionRecord) + { +#if 0 + if (exception->ExceptionFlags & EXCEPTION_NONCONTINUABLE) + continuable = FALSE; +#endif + + switch (exception->ExceptionCode) + { + case EXCEPTION_BREAKPOINT: + case EXCEPTION_SINGLE_STEP: + case DBG_CONTROL_C: + case DBG_PRINTEXCEPTION_C: + case MS_VC_EXCEPTION: + break; + default: + { + LPTSTR ExceptionCodeMsg = FormatExceptionCodeMessage(exception->ExceptionCode); + // The message for this exception code is broken. + LPTSTR ExceptionText = exception->ExceptionCode == EXCEPTION_ACCESS_VIOLATION ? "Access violation." : ExceptionCodeMsg; + + if (!initialized) + { + output_init(&ob, g_output, BUFFER_MAX); + initialized = TRUE; + } + + output_print(&ob, "Caught exception 0x%08X at 0x%p: %s\n", exception->ExceptionCode, exception->ExceptionAddress, ExceptionText); + + LocalFree(ExceptionCodeMsg); + } + break; + } + } + + if (!initialized) + return EXCEPTION_CONTINUE_SEARCH; // EXCEPTION_CONTINUE_EXECUTION + + ExceptionPrinted = (char*)calloc(strlen(g_output) + 37 + 2*MAX_PATH, sizeof(char)); + strcpy(ExceptionPrinted, g_output); + strcat(ExceptionPrinted, "\nPlease send "); + strcat(ExceptionPrinted, crashlogfilename); + strcat(ExceptionPrinted, " to the maintainers of "); + strcat(ExceptionPrinted, propername); + strcat(ExceptionPrinted, "."); + + { + DWORD error = 0; + BOOL SymInitialized = SymInitialize(GetCurrentProcess(), NULL, TRUE); + + if (!SymInitialized) + { + LPTSTR errorText; + + error = GetLastError(); + errorText = FormatErrorMessage(error); + output_print(&ob, "SymInitialize() failed with error %d: %s\n", error, errorText); + LocalFree(errorText); + } + + if (SymInitialized || error == 87) + { + struct bfd_set *set = calloc(1,sizeof(*set)); + bfd_init(); + _backtrace(&ob , set , 128 , info->ContextRecord); + release_set(set); + + SymCleanup(GetCurrentProcess()); + } + } + + logfd = open(crashlogfilename, O_APPEND | O_CREAT | O_RDWR, S_IRUSR | S_IWUSR); + + if (logfd) { + time_t curtime; + struct tm *curltime; + const char *theasctime; + const char *finistr = "---------------\n"; + + while ((written = write(logfd, g_output, strlen(g_output)))) { + g_output += written; + } + + curtime = time(NULL); + curltime = localtime(&curtime); + theasctime = curltime ? asctime(curltime) : NULL; + + if (theasctime) + write(logfd, theasctime, strlen(theasctime)); + write(logfd, finistr, strlen(finistr)); + close(logfd); + } + + //fputs(g_output, stderr); + + msgboxID = ExceptionMessage(ExceptionPrinted, propername); + + free(ExceptionPrinted); + + switch (msgboxID) + { + case IDYES: + exit(0xBAC); + break; + case IDNO: + break; + case IDCANCEL: + return EXCEPTION_CONTINUE_EXECUTION; + break; + } + + return EXCEPTION_CONTINUE_SEARCH; +} + +static void +backtrace_register(void) +{ + if (g_output == NULL) { + g_output = malloc(BUFFER_MAX); + g_prev = AddVectoredExceptionHandler(1, exception_filter); + } +} + +static void +backtrace_unregister(void) +{ + if (g_output) { + free(g_output); + RemoveVectoredExceptionHandler(g_prev); + g_prev = NULL; + g_output = NULL; + } +} + +BOOL WINAPI +DllMain(HINSTANCE hinstDLL ATTRIBUTE((unused)), DWORD dwReason, LPVOID lpvReserved ATTRIBUTE((unused))) +{ + switch (dwReason) { + case DLL_PROCESS_ATTACH: + backtrace_register(); + break; + case DLL_PROCESS_DETACH: + backtrace_unregister(); + break; + } + return TRUE; +} + + +/* cut dependence on libintl... libbfd needs this */ +char *libintl_dgettext (const char *domain_name ATTRIBUTE((unused)), const char *msgid ATTRIBUTE((unused))) +{ + static char buf[1024] = "XXX placeholder XXX"; + return buf; +} + +int __printf__ ( const char * format, ... ); +int libintl_fprintf ( FILE * stream, const char * format, ... ); +int libintl_sprintf ( char * str, const char * format, ... ); +int libintl_snprintf ( char *buffer, int buf_size, const char *format, ... ); +int libintl_vprintf ( const char * format, va_list arg ); +int libintl_vfprintf ( FILE * stream, const char * format, va_list arg ); +int libintl_vsprintf ( char * str, const char * format, va_list arg ); + +int __printf__ ( const char * format, ... ) +{ + int value; + va_list arg; + va_start(arg, format); + value = vprintf ( format, arg ); + va_end(arg); + return value; +} + +int libintl_fprintf ( FILE * stream, const char * format, ... ) +{ + int value; + va_list arg; + va_start(arg, format); + value = vfprintf ( stream, format, arg ); + va_end(arg); + return value; +} +int libintl_sprintf ( char * str, const char * format, ... ) +{ + int value; + va_list arg; + va_start(arg, format); + value = vsprintf ( str, format, arg ); + va_end(arg); + return value; +} +int libintl_snprintf ( char *buffer, int buf_size, const char *format, ... ) +{ + int value; + va_list arg; + va_start(arg, format); + value = vsnprintf ( buffer, buf_size, format, arg ); + va_end(arg); + return value; +} +int libintl_vprintf ( const char * format, va_list arg ) +{ + return vprintf ( format, arg ); +} +int libintl_vfprintf ( FILE * stream, const char * format, va_list arg ) +{ + return vfprintf ( stream, format, arg ); +} +int libintl_vsprintf ( char * str, const char * format, va_list arg ) +{ + return vsprintf ( str, format, arg ); +} + +/* cut dependence on zlib... libbfd needs this */ + +int compress (unsigned char *dest ATTRIBUTE((unused)), unsigned long destLen ATTRIBUTE((unused)), const unsigned char source ATTRIBUTE((unused)), unsigned long sourceLen ATTRIBUTE((unused))) +{ + return 0; +} +unsigned long compressBound (unsigned long sourceLen) +{ + return sourceLen + (sourceLen >> 12) + (sourceLen >> 14) + (sourceLen >> 25) + 13; +} +int inflateEnd(void *strm ATTRIBUTE((unused))) +{ + return 0; +} +int inflateInit_(void *strm ATTRIBUTE((unused)), const char *version ATTRIBUTE((unused)), int stream_size ATTRIBUTE((unused))) +{ + return 0; +} +int inflateReset(void *strm ATTRIBUTE((unused))) +{ + return 0; +} +int inflate(void *strm ATTRIBUTE((unused)), int flush ATTRIBUTE((unused))) +{ + return 0; +} diff --git a/bin/windows/vpx/src/compat-to-msvc/Makefile b/bin/windows/vpx/src/compat-to-msvc/Makefile new file mode 100644 index 0000000000..7c5f3760de --- /dev/null +++ b/bin/windows/vpx/src/compat-to-msvc/Makefile @@ -0,0 +1,22 @@ + +o=o + +NAME:=libcompat-to-msvc + + +%.$o: %.c + gcc -Wall -Wextra -O3 -c $< -o $@ + +%.$o: %.S + gcc -c $< -o $@ + +OBJS=dll_math.$o io_math.$o dll_dependency.$o vsnprintf.$o + +.INTERMEDIATE: $(OBJS) + +$(NAME).a: $(OBJS) + ar rc $@ $^ + ranlib $@ + +clean: + -rm -f *.a *.o diff --git a/bin/windows/vpx/src/compat-to-msvc/dll_dependency.S b/bin/windows/vpx/src/compat-to-msvc/dll_dependency.S new file mode 100644 index 0000000000..31fd95c822 --- /dev/null +++ b/bin/windows/vpx/src/compat-to-msvc/dll_dependency.S @@ -0,0 +1,88 @@ +/* Implementation for gcc's internal stack-allocation routines. */ +.global ___chkstk +.global __alloca + +.global ___chkstk_ms +___chkstk_ms: +#ifdef _WIN64 + pushq %rax + pushq %rcx + cmpq $0x1000, %rax + leaq 24(%rsp), %rcx + jb .Lchkstk_ms_end +.Lchkstk_ms_loop: + subq $0x1000, %rcx + subq $0x1000, %rax + orq $0x0, (%rcx) + cmpq $0x1000, %rax + ja .Lchkstk_ms_loop +.Lchkstk_ms_end: + subq %rax, %rcx + orq $0x0, (%rcx) + popq %rcx + popq %rax + ret +#else + pushl %eax + pushl %ecx + cmpl $0x1000, %eax + leal 12(%esp), %ecx + jb chkstk_ms_end +chkstk_ms_loop: + subl $0x1000, %ecx + subl $0x1000, %eax + orl $0x0, (%ecx) + cmpl $0x1000, %eax + ja chkstk_ms_loop +chkstk_ms_end: + subl %eax, %ecx + orl $0x0, (%ecx) + popl %ecx + popl %eax + ret +#endif + +#ifdef _WIN64 +__alloca: + movq %rcx, %rax +.align 4 +___chkstk: + popq %r11 + movq %rsp, %r10 + cmpq $0x1000, %rax + jb .Lchkstk_end +.Lchkstk_loop: + subq $0x1000, %r10 + subq $0x1000, %rax + orl $0x0, (%r10) + cmpq $0x1000, %rax + ja .Lchkstk_loop +.Lchkstk_end: + subq %rax, %r10 + movq %rsp, %rax + orl $0x0, (%r10) + movq %r10, %rsp + pushq %r11 + ret +#else +___chkstk: +__alloca: + pushl %ecx + leal 8(%esp), %ecx + cmpl $0x1000, %eax /* > 4k ?*/ + jb chkstk_end +chkstk_loop: + subl $0x1000, %ecx + subl $0x1000, %eax + orl $0x0, (%ecx) + cmpl $0x1000, %eax + ja chkstk_loop +chkstk_end: + subl %eax, %ecx + orl $0x0, (%ecx) + movl %esp, %eax + movl %ecx, %esp + movl (%eax), %ecx + pushl 4(%eax) + ret +#endif diff --git a/bin/windows/vpx/src/compat-to-msvc/dll_math.c b/bin/windows/vpx/src/compat-to-msvc/dll_math.c new file mode 100644 index 0000000000..966fb99d71 --- /dev/null +++ b/bin/windows/vpx/src/compat-to-msvc/dll_math.c @@ -0,0 +1,572 @@ +/*- + * Copyright (c) 1992, 1993 + * The Regents of the University of California. All rights reserved. + * + * This software was developed by the Computer Systems Engineering group + * at Lawrence Berkeley Laboratory under DARPA contract BG 91-66 and + * contributed to Berkeley. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 4. Neither the name of the University nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + */ + +#ifndef _LIBKERN_QUAD_H_ +#define _LIBKERN_QUAD_H_ + +/* + * Quad arithmetic. + * + * This library makes the following assumptions: + * + * - The type long long (aka quad_t) exists. + * + * - A quad variable is exactly twice as long as `long'. + * + * - The machine's arithmetic is two's complement. + * + * This library can provide 128-bit arithmetic on a machine with 128-bit + * quads and 64-bit longs, for instance, or 96-bit arithmetic on machines + * with 48-bit longs. + */ +/* +#include +#include +#include +#include +*/ + +#include +typedef long long quad_t; +typedef unsigned long long u_quad_t; +typedef unsigned long u_long; +#ifndef CHAR_BIT +# define CHAR_BIT __CHAR_BIT__ +#endif + +/* + * Define the order of 32-bit words in 64-bit words. + * For little endian only. + */ +#define _QUAD_HIGHWORD 1 +#define _QUAD_LOWWORD 0 + +/* + * Depending on the desired operation, we view a `long long' (aka quad_t) in + * one or more of the following formats. + */ +union uu { + quad_t q; /* as a (signed) quad */ + quad_t uq; /* as an unsigned quad */ + long sl[2]; /* as two signed longs */ + u_long ul[2]; /* as two unsigned longs */ +}; + +/* + * Define high and low longwords. + */ +#define H _QUAD_HIGHWORD +#define L _QUAD_LOWWORD + +/* + * Total number of bits in a quad_t and in the pieces that make it up. + * These are used for shifting, and also below for halfword extraction + * and assembly. + */ +#define QUAD_BITS (sizeof(quad_t) * CHAR_BIT) +#define LONG_BITS (sizeof(long) * CHAR_BIT) +#define HALF_BITS (sizeof(long) * CHAR_BIT / 2) + +/* + * Extract high and low shortwords from longword, and move low shortword of + * longword to upper half of long, i.e., produce the upper longword of + * ((quad_t)(x) << (number_of_bits_in_long/2)). (`x' must actually be u_long.) + * + * These are used in the multiply code, to split a longword into upper + * and lower halves, and to reassemble a product as a quad_t, shifted left + * (sizeof(long)*CHAR_BIT/2). + */ +#define HHALF(x) ((x) >> HALF_BITS) +#define LHALF(x) ((x) & ((1 << HALF_BITS) - 1)) +#define LHUP(x) ((x) << HALF_BITS) + +typedef unsigned int qshift_t; + +quad_t __ashldi3(quad_t, qshift_t); +quad_t __ashrdi3(quad_t, qshift_t); +int __cmpdi2(quad_t a, quad_t b); +quad_t __divdi3(quad_t a, quad_t b); +quad_t __lshrdi3(quad_t, qshift_t); +quad_t __moddi3(quad_t a, quad_t b); +u_quad_t __qdivrem(u_quad_t u, u_quad_t v, u_quad_t *rem); +u_quad_t __udivdi3(u_quad_t a, u_quad_t b); +u_quad_t __umoddi3(u_quad_t a, u_quad_t b); +int __ucmpdi2(u_quad_t a, u_quad_t b); + +#endif /* !_LIBKERN_QUAD_H_ */ + +#if defined (_X86_) && !defined (__x86_64__) +/* + * Shift a (signed) quad value left (arithmetic shift left). + * This is the same as logical shift left! + */ +quad_t +__ashldi3(a, shift) + quad_t a; + qshift_t shift; +{ + union uu aa; + + aa.q = a; + if (shift >= LONG_BITS) { + aa.ul[H] = shift >= QUAD_BITS ? 0 : + aa.ul[L] << (shift - LONG_BITS); + aa.ul[L] = 0; + } else if (shift > 0) { + aa.ul[H] = (aa.ul[H] << shift) | + (aa.ul[L] >> (LONG_BITS - shift)); + aa.ul[L] <<= shift; + } + return (aa.q); +} + +/* + * Shift a (signed) quad value right (arithmetic shift right). + */ +quad_t +__ashrdi3(a, shift) + quad_t a; + qshift_t shift; +{ + union uu aa; + + aa.q = a; + if (shift >= LONG_BITS) { + long s; + + /* + * Smear bits rightward using the machine's right-shift + * method, whether that is sign extension or zero fill, + * to get the `sign word' s. Note that shifting by + * LONG_BITS is undefined, so we shift (LONG_BITS-1), + * then 1 more, to get our answer. + */ + s = (aa.sl[H] >> (LONG_BITS - 1)) >> 1; + aa.ul[L] = shift >= QUAD_BITS ? s : + aa.sl[H] >> (shift - LONG_BITS); + aa.ul[H] = s; + } else if (shift > 0) { + aa.ul[L] = (aa.ul[L] >> shift) | + (aa.ul[H] << (LONG_BITS - shift)); + aa.sl[H] >>= shift; + } + return (aa.q); +} + +/* + * Return 0, 1, or 2 as a <, =, > b respectively. + * Both a and b are considered signed---which means only the high word is + * signed. + */ +int +__cmpdi2(a, b) + quad_t a, b; +{ + union uu aa, bb; + + aa.q = a; + bb.q = b; + return (aa.sl[H] < bb.sl[H] ? 0 : aa.sl[H] > bb.sl[H] ? 2 : + aa.ul[L] < bb.ul[L] ? 0 : aa.ul[L] > bb.ul[L] ? 2 : 1); +} + +/* + * Divide two signed quads. + * ??? if -1/2 should produce -1 on this machine, this code is wrong + */ +quad_t +__divdi3(a, b) + quad_t a, b; +{ + u_quad_t ua, ub, uq; + int neg; + + if (a < 0) + ua = -(u_quad_t)a, neg = 1; + else + ua = a, neg = 0; + if (b < 0) + ub = -(u_quad_t)b, neg ^= 1; + else + ub = b; + uq = __qdivrem(ua, ub, (u_quad_t *)0); + return (neg ? -uq : uq); +} + +/* + * Shift an (unsigned) quad value right (logical shift right). + */ +quad_t +__lshrdi3(a, shift) + quad_t a; + qshift_t shift; +{ + union uu aa; + + aa.q = a; + if (shift >= LONG_BITS) { + aa.ul[L] = shift >= QUAD_BITS ? 0 : + aa.ul[H] >> (shift - LONG_BITS); + aa.ul[H] = 0; + } else if (shift > 0) { + aa.ul[L] = (aa.ul[L] >> shift) | + (aa.ul[H] << (LONG_BITS - shift)); + aa.ul[H] >>= shift; + } + return (aa.q); +} + +/* + * Return remainder after dividing two signed quads. + * + * XXX + * If -1/2 should produce -1 on this machine, this code is wrong. + */ +quad_t +__moddi3(a, b) + quad_t a, b; +{ + u_quad_t ua, ub, ur; + int neg; + + if (a < 0) + ua = -(u_quad_t)a, neg = 1; + else + ua = a, neg = 0; + if (b < 0) + ub = -(u_quad_t)b; + else + ub = b; + (void)__qdivrem(ua, ub, &ur); + return (neg ? -ur : ur); +} + + +/* + * Multiprecision divide. This algorithm is from Knuth vol. 2 (2nd ed), + * section 4.3.1, pp. 257--259. + */ + +#define B (1 << HALF_BITS) /* digit base */ + +/* Combine two `digits' to make a single two-digit number. */ +#define COMBINE(a, b) (((u_long)(a) << HALF_BITS) | (b)) + +/* select a type for digits in base B: use unsigned short if they fit */ +#if ULONG_MAX == 0xffffffff && USHRT_MAX >= 0xffff +typedef unsigned short digit; +#else +typedef u_long digit; +#endif + +/* + * Shift p[0]..p[len] left `sh' bits, ignoring any bits that + * `fall out' the left (there never will be any such anyway). + * We may assume len >= 0. NOTE THAT THIS WRITES len+1 DIGITS. + */ +static void +__shl(register digit *p, register int len, register int sh) +{ + register int i; + + for (i = 0; i < len; i++) + p[i] = LHALF(p[i] << sh) | (p[i + 1] >> (HALF_BITS - sh)); + p[i] = LHALF(p[i] << sh); +} + +/* + * __qdivrem(u, v, rem) returns u/v and, optionally, sets *rem to u%v. + * + * We do this in base 2-sup-HALF_BITS, so that all intermediate products + * fit within u_long. As a consequence, the maximum length dividend and + * divisor are 4 `digits' in this base (they are shorter if they have + * leading zeros). + */ +u_quad_t +__qdivrem(uq, vq, arq) + u_quad_t uq, vq, *arq; +{ + union uu tmp; + digit *u, *v, *q; + register digit v1, v2; + u_long qhat, rhat, t; + int m, n, d, j, i; + digit uspace[5], vspace[5], qspace[5]; + + /* + * Take care of special cases: divide by zero, and u < v. + */ + if (vq == 0) { + /* divide by zero. */ + static volatile const unsigned int zero = 0; + + tmp.ul[H] = tmp.ul[L] = 1 / zero; + if (arq) + *arq = uq; + return (tmp.q); + } + if (uq < vq) { + if (arq) + *arq = uq; + return (0); + } + u = &uspace[0]; + v = &vspace[0]; + q = &qspace[0]; + + /* + * Break dividend and divisor into digits in base B, then + * count leading zeros to determine m and n. When done, we + * will have: + * u = (u[1]u[2]...u[m+n]) sub B + * v = (v[1]v[2]...v[n]) sub B + * v[1] != 0 + * 1 < n <= 4 (if n = 1, we use a different division algorithm) + * m >= 0 (otherwise u < v, which we already checked) + * m + n = 4 + * and thus + * m = 4 - n <= 2 + */ + tmp.uq = uq; + u[0] = 0; + u[1] = HHALF(tmp.ul[H]); + u[2] = LHALF(tmp.ul[H]); + u[3] = HHALF(tmp.ul[L]); + u[4] = LHALF(tmp.ul[L]); + tmp.uq = vq; + v[1] = HHALF(tmp.ul[H]); + v[2] = LHALF(tmp.ul[H]); + v[3] = HHALF(tmp.ul[L]); + v[4] = LHALF(tmp.ul[L]); + for (n = 4; v[1] == 0; v++) { + if (--n == 1) { + u_long rbj; /* r*B+u[j] (not root boy jim) */ + digit q1, q2, q3, q4; + + /* + * Change of plan, per exercise 16. + * r = 0; + * for j = 1..4: + * q[j] = floor((r*B + u[j]) / v), + * r = (r*B + u[j]) % v; + * We unroll this completely here. + */ + t = v[2]; /* nonzero, by definition */ + q1 = u[1] / t; + rbj = COMBINE(u[1] % t, u[2]); + q2 = rbj / t; + rbj = COMBINE(rbj % t, u[3]); + q3 = rbj / t; + rbj = COMBINE(rbj % t, u[4]); + q4 = rbj / t; + if (arq) + *arq = rbj % t; + tmp.ul[H] = COMBINE(q1, q2); + tmp.ul[L] = COMBINE(q3, q4); + return (tmp.q); + } + } + + /* + * By adjusting q once we determine m, we can guarantee that + * there is a complete four-digit quotient at &qspace[1] when + * we finally stop. + */ + for (m = 4 - n; u[1] == 0; u++) + m--; + for (i = 4 - m; --i >= 0;) + q[i] = 0; + q += 4 - m; + + /* + * Here we run Program D, translated from MIX to C and acquiring + * a few minor changes. + * + * D1: choose multiplier 1 << d to ensure v[1] >= B/2. + */ + d = 0; + for (t = v[1]; t < B / 2; t <<= 1) + d++; + if (d > 0) { + __shl(&u[0], m + n, d); /* u <<= d */ + __shl(&v[1], n - 1, d); /* v <<= d */ + } + /* + * D2: j = 0. + */ + j = 0; + v1 = v[1]; /* for D3 -- note that v[1..n] are constant */ + v2 = v[2]; /* for D3 */ + do { + register digit uj0, uj1, uj2; + + /* + * D3: Calculate qhat (\^q, in TeX notation). + * Let qhat = min((u[j]*B + u[j+1])/v[1], B-1), and + * let rhat = (u[j]*B + u[j+1]) mod v[1]. + * While rhat < B and v[2]*qhat > rhat*B+u[j+2], + * decrement qhat and increase rhat correspondingly. + * Note that if rhat >= B, v[2]*qhat < rhat*B. + */ + uj0 = u[j + 0]; /* for D3 only -- note that u[j+...] change */ + uj1 = u[j + 1]; /* for D3 only */ + uj2 = u[j + 2]; /* for D3 only */ + if (uj0 == v1) { + qhat = B; + rhat = uj1; + goto qhat_too_big; + } else { + u_long nn = COMBINE(uj0, uj1); + qhat = nn / v1; + rhat = nn % v1; + } + while (v2 * qhat > COMBINE(rhat, uj2)) { + qhat_too_big: + qhat--; + if ((rhat += v1) >= B) + break; + } + /* + * D4: Multiply and subtract. + * The variable `t' holds any borrows across the loop. + * We split this up so that we do not require v[0] = 0, + * and to eliminate a final special case. + */ + for (t = 0, i = n; i > 0; i--) { + t = u[i + j] - v[i] * qhat - t; + u[i + j] = LHALF(t); + t = (B - HHALF(t)) & (B - 1); + } + t = u[j] - t; + u[j] = LHALF(t); + /* + * D5: test remainder. + * There is a borrow if and only if HHALF(t) is nonzero; + * in that (rare) case, qhat was too large (by exactly 1). + * Fix it by adding v[1..n] to u[j..j+n]. + */ + if (HHALF(t)) { + qhat--; + for (t = 0, i = n; i > 0; i--) { /* D6: add back. */ + t += u[i + j] + v[i]; + u[i + j] = LHALF(t); + t = HHALF(t); + } + u[j] = LHALF(u[j] + t); + } + q[j] = qhat; + } while (++j <= m); /* D7: loop on j. */ + + /* + * If caller wants the remainder, we have to calculate it as + * u[m..m+n] >> d (this is at most n digits and thus fits in + * u[m+1..m+n], but we may need more source digits). + */ + if (arq) { + if (d) { + for (i = m + n; i > m; --i) + u[i] = (u[i] >> d) | + LHALF(u[i - 1] << (HALF_BITS - d)); + u[i] = 0; + } + tmp.ul[H] = COMBINE(uspace[1], uspace[2]); + tmp.ul[L] = COMBINE(uspace[3], uspace[4]); + *arq = tmp.q; + } + + tmp.ul[H] = COMBINE(qspace[1], qspace[2]); + tmp.ul[L] = COMBINE(qspace[3], qspace[4]); + return (tmp.q); +} + +/* + * Return 0, 1, or 2 as a <, =, > b respectively. + * Neither a nor b are considered signed. + */ +int +__ucmpdi2(a, b) + u_quad_t a, b; +{ + union uu aa, bb; + + aa.uq = a; + bb.uq = b; + return (aa.ul[H] < bb.ul[H] ? 0 : aa.ul[H] > bb.ul[H] ? 2 : + aa.ul[L] < bb.ul[L] ? 0 : aa.ul[L] > bb.ul[L] ? 2 : 1); +} + +/* + * Divide two unsigned quads. + */ +u_quad_t +__udivdi3(a, b) + u_quad_t a, b; +{ + + return (__qdivrem(a, b, (u_quad_t *)0)); +} + +/* + * Return remainder after dividing two unsigned quads. + */ +u_quad_t +__umoddi3(a, b) + u_quad_t a, b; +{ + u_quad_t r; + + (void)__qdivrem(a, b, &r); + return (r); +} + +/* + * Divide two unsigned quads. + * This function is new in GCC 7. + */ +u_quad_t +__udivmoddi4(a, b, rem) + u_quad_t a, b, *rem; +{ + u_quad_t ua, ub, uq, ur; + + ua = a; + ub = b; + uq = __qdivrem(ua, ub, &ur); + if (rem) + *rem = ur; + return uq; +} +#else +static int __attribute__((unused)) dummy; +#endif /* defined (_X86_) && !defined (__x86_64__) */ + diff --git a/bin/windows/vpx/src/compat-to-msvc/io_math.c b/bin/windows/vpx/src/compat-to-msvc/io_math.c new file mode 100644 index 0000000000..39d5a8e394 --- /dev/null +++ b/bin/windows/vpx/src/compat-to-msvc/io_math.c @@ -0,0 +1,39 @@ +// Some libraries expect these functions, for which Visual Studio (pre-2013) falls down on the job. + +#include +#include + +#ifndef _MSC_VER +# include +int64_t _ftelli64( + FILE *stream +); +int _fseeki64( + FILE *stream, + int64_t offset, + int origin +); +#endif + +int fseeko(FILE *fp, off_t offset, int whence) +{ + return _fseeki64(fp, (int64_t)offset, whence); +} +int fseeko64(FILE *fp, off64_t offset, int whence) +{ + return _fseeki64(fp, (int64_t)offset, whence); +} + +off_t ftello(FILE *stream) +{ + return (off_t)_ftelli64(stream); +} +off64_t ftello64(FILE *stream) +{ + return (off64_t)_ftelli64(stream); +} + +long lround(double d) +{ + return (long)(d > 0 ? d + 0.5 : ceil(d - 0.5)); +} diff --git a/bin/windows/vpx/src/compat-to-msvc/vsnprintf.c b/bin/windows/vpx/src/compat-to-msvc/vsnprintf.c new file mode 100644 index 0000000000..15de014c4a --- /dev/null +++ b/bin/windows/vpx/src/compat-to-msvc/vsnprintf.c @@ -0,0 +1,19 @@ +/** + * This file has no copyright assigned and is placed in the Public Domain. + * This file is part of the mingw-w64 runtime package. + * No warranty is given; refer to the file DISCLAIMER.PD within this package. + */ +#define __CRT__NO_INLINE +#include +#include + +extern int __cdecl _vsnprintf(char * __restrict__, size_t, const char * __restrict__, va_list); + +int __cdecl __ms_vsnprintf (char * __restrict__ s, size_t n, const char * __restrict__ format, va_list arg) +{ + return _vsnprintf(s, n, format, arg); +} +int __cdecl __mingw_vsnprintf (char * __restrict__ s, size_t n, const char * __restrict__ format, va_list arg) +{ + return _vsnprintf(s, n, format, arg); +} diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index cf624e071d..cd75f0c825 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -406,6 +406,29 @@ set( ZDOOM_LIBS ${ZDOOM_LIBS} "${ZLIB_LIBRARIES}" "${JPEG_LIBRARIES}" "${BZIP2_L if (HAVE_VULKAN) set( ZDOOM_LIBS ${ZDOOM_LIBS} "glslang" "SPIRV" "OGLCompiler") endif() +# VPX + +if( MSVC AND NOT VPX_FOUND ) + # Use prebuilt library + set( VPX_ROOT_PATH "${CMAKE_CURRENT_SOURCE_DIR}/../bin/Windows/vpx" ) + set( VPX_INCLUDE_DIR ${VPX_ROOT_PATH}/include ) + set( VPX_LIBRARIES libvpx libcompat-to-msvc ) + if( ARM64 ) + link_directories( ${VPX_ROOT_PATH}/lib/arm64 ) + else() + link_directories( ${VPX_ROOT_PATH}/lib/64 ) + endif() + set( VPX_FOUND TRUE ) +endif() + +if( VPX_FOUND ) + add_definitions( "-DUSE_LIBVPX=1" ) + include_directories( "${VPX_INCLUDE_DIR}" ) + set( ZDOOM_LIBS ${ZDOOM_LIBS} ${VPX_LIBRARIES} ) +else() + message( SEND_ERROR "Could not find libvpx" ) +endif() + include_directories( "${ZLIB_INCLUDE_DIR}" "${BZIP2_INCLUDE_DIR}" "${LZMA_INCLUDE_DIR}" "${JPEG_INCLUDE_DIR}" "${ZMUSIC_INCLUDE_DIR}" "${DRPC_INCLUDE_DIR}") if( ${HAVE_VM_JIT} ) @@ -587,6 +610,7 @@ file( GLOB HEADER_FILES common/audio/music/*.h* common/2d/*.h common/console/*.h + common/cutscenes/*.h common/utility/*.h common/engine/*.h common/menu/*.h @@ -605,6 +629,7 @@ file( GLOB HEADER_FILES common/thirdparty/*.h common/thirdparty/rapidjson/*.h common/thirdparty/math/*h + common/thirdparty/libsmackerdec/include/*.h common/rendering/*.h common/rendering/gl_load/*.h common/rendering/gles/*.h @@ -624,6 +649,7 @@ file( GLOB HEADER_FILES common/scripting/interface/*.h common/scripting/backend/*.h common/scripting/frontend/*.h + utility/*.h scripting/*.h scripting/backend/*.h @@ -1077,6 +1103,9 @@ set (PCH_SOURCES common/console/c_notifybufferbase.cpp common/console/c_tabcomplete.cpp common/console/c_expr.cpp + common/cutscenes/playmve.cpp + common/cutscenes/movieplayer.cpp + common/cutscenes/screenjob.cpp common/utility/engineerrors.cpp common/utility/i_module.cpp common/utility/m_alloc.cpp @@ -1096,6 +1125,11 @@ set (PCH_SOURCES common/thirdparty/base64.cpp common/thirdparty/md5.cpp common/thirdparty/superfasthash.cpp + common/thirdparty/libsmackerdec/src/BitReader.cpp + common/thirdparty/libsmackerdec/src/FileStream.cpp + common/thirdparty/libsmackerdec/src/HuffmanVLC.cpp + common/thirdparty/libsmackerdec/src/LogError.cpp + common/thirdparty/libsmackerdec/src/SmackerDecoder.cpp common/filesystem/filesystem.cpp common/filesystem/ancientzip.cpp common/filesystem/file_7z.cpp @@ -1262,6 +1296,8 @@ include_directories( . common/audio/sound common/audio/music common/2d + common/cutscenes + common/thirdparty/libsmackerdec/include common/thirdparty common/textures common/textures/formats diff --git a/src/common/cutscenes/movieplayer.cpp b/src/common/cutscenes/movieplayer.cpp new file mode 100644 index 0000000000..cf3222fd60 --- /dev/null +++ b/src/common/cutscenes/movieplayer.cpp @@ -0,0 +1,828 @@ +/* +** movieplayer.cpp +** +**--------------------------------------------------------------------------- +** Copyright 2020 Christoph Oelckers +** All rights reserved. +** +** Redistribution and use in source and binary forms, with or without +** modification, are permitted provided that the following conditions +** are met: +** +** 1. Redistributions of source code must retain the above copyright +** notice, this list of conditions and the following disclaimer. +** 2. Redistributions in binary form must reproduce the above copyright +** notice, this list of conditions and the following disclaimer in the +** documentation and/or other materials provided with the distribution. +** 3. The name of the author may not be used to endorse or promote products +** derived from this software without specific prior written permission. +** +** THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR +** IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES +** OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. +** IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, +** INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT +** NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF +** THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +**--------------------------------------------------------------------------- +** +*/ + +#include "types.h" +#include "screenjob.h" +#include "i_time.h" +#include "v_2ddrawer.h" +#include "animlib.h" +#include "v_draw.h" +#include "s_soundinternal.h" +#include "animtexture.h" +#include "gamestate.h" +#include "SmackerDecoder.h" +#include "playmve.h" +#include +#include +#include "filesystem.h" +#include "vm.h" +#include "printf.h" + +#ifdef _WIN32 +#pragma comment(lib, "legacy_stdio_definitions.lib") +#endif + +class MoviePlayer +{ +protected: + enum EMovieFlags + { + NOSOUNDCUTOFF = 1, + FIXEDVIEWPORT = 2, // Forces fixed 640x480 screen size like for Blood's intros. + }; + + int flags; +public: + virtual void Start() {} + virtual bool Frame(uint64_t clock) = 0; + virtual void Stop() {} + virtual ~MoviePlayer() = default; + virtual FTextureID GetTexture() = 0; +}; + +//--------------------------------------------------------------------------- +// +// +// +//--------------------------------------------------------------------------- + +class AnmPlayer : public MoviePlayer +{ + // This doesn't need its own class type + anim_t anim; + TArray buffer; + int numframes = 0; + int curframe = 1; + int frametime = 0; + int nextframetime = 0; + AnimTextures animtex; + const TArray animSnd; + int frameTicks[3]; + +public: + bool isvalid() { return numframes > 0; } + + AnmPlayer(FileReader& fr, TArray& ans, const int *frameticks, int flags_) + : animSnd(std::move(ans)) + { + memcpy(frameTicks, frameticks, 3 * sizeof(int)); + flags = flags_; + buffer = fr.ReadPadded(1); + fr.Close(); + + if (ANIM_LoadAnim(&anim, buffer.Data(), buffer.Size() - 1) < 0) + { + return; + } + numframes = ANIM_NumFrames(&anim); + animtex.SetSize(AnimTexture::Paletted, 320, 200); + } + + //--------------------------------------------------------------------------- + // + // + // + //--------------------------------------------------------------------------- + + bool Frame(uint64_t clock) override + { + int currentclock = int(clock * 120 / 1'000'000'000); + + if (currentclock < nextframetime - 1) + { + return true; + } + + animtex.SetFrame(ANIM_GetPalette(&anim), ANIM_DrawFrame(&anim, curframe)); + frametime = currentclock; + + int delay = 20; + if (frameTicks) + { + if (curframe == 1) delay = frameTicks[0]; + else if (curframe < numframes - 2) delay = frameTicks[1]; + else delay = frameTicks[2]; + } + nextframetime += delay; + + bool nostopsound = (flags & NOSOUNDCUTOFF); + for (unsigned i = 0; i < animSnd.Size(); i+=2) + { + if (animSnd[i] == curframe) + { + int sound = animSnd[i+1]; + if (sound == -1) + soundEngine->StopAllChannels(); + else + soundEngine->StartSound(SOURCE_None, nullptr, nullptr, CHAN_AUTO, nostopsound? CHANF_UI : CHANF_NONE, sound, 1.f, ATTN_NONE); + } + } + if (!nostopsound && curframe == numframes && soundEngine->GetSoundPlayingInfo(SOURCE_None, nullptr, -1)) return true; + curframe++; + return curframe < numframes; + } + + void Stop() override + { + bool nostopsound = (flags & NOSOUNDCUTOFF); + if (!nostopsound) soundEngine->StopAllChannels(); + } + + + ~AnmPlayer() + { + buffer.Reset(); + animtex.Clean(); + } + + FTextureID GetTexture() override + { + return animtex.GetFrameID(); + } +}; + +//--------------------------------------------------------------------------- +// +// +// +//--------------------------------------------------------------------------- + +class MvePlayer : public MoviePlayer +{ + InterplayDecoder decoder; + bool failed = false; + +public: + bool isvalid() { return !failed; } + + MvePlayer(FileReader& fr) : decoder(SoundEnabled()) + { + failed = !decoder.Open(fr); + } + + //--------------------------------------------------------------------------- + // + // + // + //--------------------------------------------------------------------------- + + bool Frame(uint64_t clock) override + { + if (failed) return false; + bool playon = decoder.RunFrame(clock); + return playon; + } + + ~MvePlayer() + { + decoder.Close(); + } + + FTextureID GetTexture() override + { + return decoder.animTex().GetFrameID(); + } +}; + +//--------------------------------------------------------------------------- +// +// +// +//--------------------------------------------------------------------------- + +class VpxPlayer : public MoviePlayer +{ + bool failed = false; + FileReader fr; + AnimTextures animtex; + const TArray animSnd; + + unsigned width, height; + TArray Pic; + TArray readBuf; + vpx_codec_ctx_t codec{}; + vpx_codec_iter_t iter = nullptr; + + uint32_t convnumer; + uint32_t convdenom; + + uint64_t nsecsperframe; + uint64_t nextframetime; + + int decstate = 0; + int framenum = 0; + int numframes; + int lastsoundframe = -1; +public: + int soundtrack = -1; + + +public: + bool isvalid() { return !failed; } + + VpxPlayer(FileReader& fr_, TArray& animSnd_, int flags_, int origframedelay, FString& error) : animSnd(std::move(animSnd_)) + { + fr = std::move(fr_); + flags = flags_; + + if (!ReadIVFHeader(origframedelay)) + { + // We should never get here, because any file failing this has been eliminated before this constructor got called. + error.Format("Failed reading IVF header\n"); + failed = true; + } + + Pic.Resize(width * height * 4); + + + // Todo: Support VP9 as well? + vpx_codec_dec_cfg_t cfg = { 1, width, height }; + if (vpx_codec_dec_init(&codec, &vpx_codec_vp8_dx_algo, &cfg, 0)) + { + error.Format("Error initializing VPX codec.\n"); + failed = true; + } + } + + //--------------------------------------------------------------------------- + // + // + // + //--------------------------------------------------------------------------- + + bool ReadIVFHeader(int origframedelay) + { + // IVF format: http://wiki.multimedia.cx/index.php?title=IVF + uint32_t magic; fr.Read(&magic, 4); // do not byte swap! + if (magic != MAKE_ID('D', 'K', 'I', 'F')) return false; + uint16_t version = fr.ReadUInt16(); + if (version != 0) return false; + uint16_t length = fr.ReadUInt16(); + if (length != 32) return false; + fr.Read(&magic, 4); + if (magic != MAKE_ID('V', 'P', '8', '0')) return false; + + width = fr.ReadUInt16(); + height = fr.ReadUInt16(); + uint32_t fpsdenominator = fr.ReadUInt32(); + uint32_t fpsnumerator = fr.ReadUInt32(); + numframes = fr.ReadUInt32(); + if (numframes == 0) return false; + fr.Seek(4, FileReader::SeekCur); + + if (fpsdenominator > 1000 || fpsnumerator == 0 || fpsdenominator == 0) + { + // default to 30 fps if the header does not provide useful info. + fpsdenominator = 30; + fpsnumerator = 1; + } + + convnumer = 120 * fpsnumerator; + convdenom = fpsdenominator * origframedelay; + + nsecsperframe = int64_t(fpsnumerator) * 1'000'000'000 / fpsdenominator; + nextframetime = 0; + + return true; + } + + //--------------------------------------------------------------------------- + // + // + // + //--------------------------------------------------------------------------- + + bool ReadFrame() + { + int corrupted = 0; + int framesize = fr.ReadInt32(); + fr.Seek(8, FileReader::SeekCur); + if (framesize == 0) return false; + + readBuf.Resize(framesize); + if (fr.Read(readBuf.Data(), framesize) != framesize) return false; + if (vpx_codec_decode(&codec, readBuf.Data(), readBuf.Size(), NULL, 0) != VPX_CODEC_OK) return false; + if (vpx_codec_control(&codec, VP8D_GET_FRAME_CORRUPTED, &corrupted) != VPX_CODEC_OK) return false; + return true; + } + + //--------------------------------------------------------------------------- + // + // + // + //--------------------------------------------------------------------------- + + vpx_image_t *GetFrameData() + { + vpx_image_t *img; + do + { + if (decstate == 0) // first time / begin + { + if (!ReadFrame()) return nullptr; + decstate = 1; + } + + img = vpx_codec_get_frame(&codec, &iter); + if (img == nullptr) + { + decstate = 0; + iter = nullptr; + } + } while (img == nullptr); + + return img->d_w == width && img->d_h == height? img : nullptr; + } + + //--------------------------------------------------------------------------- + // + // + // + //--------------------------------------------------------------------------- + + void SetPixel(uint8_t* dest, uint8_t y, uint8_t u, uint8_t v) + { + dest[0] = y; + dest[1] = u; + dest[2] = v; + } + + bool CreateNextFrame() + { + auto img = GetFrameData(); + if (!img) return false; + uint8_t const* const yplane = img->planes[VPX_PLANE_Y]; + uint8_t const* const uplane = img->planes[VPX_PLANE_U]; + uint8_t const* const vplane = img->planes[VPX_PLANE_V]; + + const int ystride = img->stride[VPX_PLANE_Y]; + const int ustride = img->stride[VPX_PLANE_U]; + const int vstride = img->stride[VPX_PLANE_V]; + + for (unsigned int y = 0; y < height; y += 2) + { + unsigned int y1 = y + 1; + unsigned int wy = width * y; + unsigned int wy1 = width * y1; + + for (unsigned int x = 0; x < width; x += 2) + { + uint8_t u = uplane[ustride * (y >> 1) + (x >> 1)]; + uint8_t v = vplane[vstride * (y >> 1) + (x >> 1)]; + + SetPixel(&Pic[(wy + x) << 2], yplane[ystride * y + x], u, v); + SetPixel(&Pic[(wy + x + 1) << 2], yplane[ystride * y + x + 1], u, v); + SetPixel(&Pic[(wy1 + x) << 2], yplane[ystride * y1 + x], u, v); + SetPixel(&Pic[(wy1 + x + 1) << 2], yplane[ystride * y1 + x + 1], u, v); + } + } + return true; + } + + //--------------------------------------------------------------------------- + // + // + // + //--------------------------------------------------------------------------- + + void Start() override + { + if (soundtrack > 0) + { + S_ChangeMusic(fileSystem.GetFileFullName(soundtrack, false), 0, false); + } + animtex.SetSize(AnimTexture::YUV, width, height); + } + + //--------------------------------------------------------------------------- + // + // + // + //--------------------------------------------------------------------------- + + bool Frame(uint64_t clock) override + { + bool stop = false; + if (clock > nextframetime) + { + nextframetime += nsecsperframe; + + if (!CreateNextFrame()) + { + Printf(PRINT_BOLD, "Failed reading next frame\n"); + stop = true; + } + else + { + animtex.SetFrame(nullptr, Pic.Data()); + } + framenum++; + if (framenum >= numframes) stop = true; + + bool nostopsound = (flags & NOSOUNDCUTOFF); + int soundframe = convdenom ? Scale(framenum, convnumer, convdenom) : framenum; + if (soundframe > lastsoundframe) + { + if (soundtrack == -1) + { + for (unsigned i = 0; i < animSnd.Size(); i += 2) + { + if (animSnd[i] == soundframe) + { + int sound = animSnd[i + 1]; + if (sound == -1) + soundEngine->StopAllChannels(); + else + soundEngine->StartSound(SOURCE_None, nullptr, nullptr, CHAN_AUTO, nostopsound ? CHANF_UI : CHANF_NONE, sound, 1.f, ATTN_NONE); + } + } + } + lastsoundframe = soundframe; + } + } + return !stop; + } + + void Stop() + { + S_StopMusic(true); + bool nostopsound = (flags & NOSOUNDCUTOFF); + if (!nostopsound) soundEngine->StopAllChannels(); + } + + ~VpxPlayer() + { + vpx_codec_destroy(&codec); + animtex.Clean(); + } + + FTextureID GetTexture() override + { + return animtex.GetFrameID(); + } +}; + +//--------------------------------------------------------------------------- +// +// +// +//--------------------------------------------------------------------------- + +struct AudioData +{ + int hFx; + SmackerAudioInfo inf; + + int16_t samples[6000 * 20]; // must be a multiple of the stream buffer size and larger than the initial chunk of audio + + int nWrite; + int nRead; +}; + +class SmkPlayer : public MoviePlayer +{ + SmackerHandle hSMK{}; + int numAudioTracks; + AudioData adata; + uint32_t nWidth, nHeight; + uint8_t palette[768]; + AnimTextures animtex; + TArray pFrame; + TArray audioBuffer; + int nFrames; + bool fullscreenScale; + uint64_t nFrameNs; + int nFrame = 0; + const TArray animSnd; + FString filename; + SoundStream* stream = nullptr; + bool hassound = false; + +public: + bool isvalid() { return hSMK.isValid; } + + static bool StreamCallbackFunc(SoundStream* stream, void* buff, int len, void* userdata) + { + SmkPlayer* pId = (SmkPlayer*)userdata; + memcpy(buff, &pId->adata.samples[pId->adata.nRead], len); + pId->adata.nRead += len / 2; + if (pId->adata.nRead >= countof(pId->adata.samples)) pId->adata.nRead = 0; + return true; + } + + void copy8bitSamples(unsigned count) + { + for (unsigned i = 0; i < count; i++) + { + adata.samples[adata.nWrite] = (audioBuffer[i] - 128) << 8; + if (++adata.nWrite >= countof(adata.samples)) adata.nWrite = 0; + } + } + + void copy16bitSamples(unsigned count) + { + auto ptr = (uint16_t*)audioBuffer.Data(); + for (unsigned i = 0; i < count/2; i++) + { + adata.samples[adata.nWrite] = *ptr++; + if (++adata.nWrite >= countof(adata.samples)) adata.nWrite = 0; + } + } + + + SmkPlayer(const char *fn, TArray& ans, int flags_) : animSnd(std::move(ans)) + { + hSMK = Smacker_Open(fn); + if (!hSMK.isValid) + { + return; + } + flags = flags_; + Smacker_GetFrameSize(hSMK, nWidth, nHeight); + pFrame.Resize(nWidth * nHeight + std::max(nWidth, nHeight)); + float frameRate = Smacker_GetFrameRate(hSMK); + nFrameNs = uint64_t(1'000'000'000 / frameRate); + nFrames = Smacker_GetNumFrames(hSMK); + Smacker_GetPalette(hSMK, palette); + + numAudioTracks = Smacker_GetNumAudioTracks(hSMK); + if (numAudioTracks) + { + adata.nWrite = 0; + adata.nRead = 0; + adata.inf = Smacker_GetAudioTrackDetails(hSMK, 0); + if (adata.inf.idealBufferSize > 0) + { + audioBuffer.Resize(adata.inf.idealBufferSize); + auto read = Smacker_GetAudioData(hSMK, 0, (int16_t*)audioBuffer.Data()); + if (adata.inf.bitsPerSample == 8) copy8bitSamples(read); + else copy16bitSamples(read); + hassound = true; + } + } + if (!hassound) + { + adata.inf = {}; + } + + } + + //--------------------------------------------------------------------------- +// +// +// +//--------------------------------------------------------------------------- + + void Start() override + { + animtex.SetSize(AnimTexture::Paletted, nWidth, nHeight); + } + + //--------------------------------------------------------------------------- + // + // + // + //--------------------------------------------------------------------------- + + bool Frame(uint64_t clock) override + { + int frame = int(clock / nFrameNs); + + twod->ClearScreen(); + if (frame > nFrame) + { + Smacker_GetPalette(hSMK, palette); + Smacker_GetFrame(hSMK, pFrame.Data()); + animtex.SetFrame(palette, pFrame.Data()); + if (numAudioTracks && SoundEnabled()) + { + auto read = Smacker_GetAudioData(hSMK, 0, (int16_t*)audioBuffer.Data()); + if (adata.inf.bitsPerSample == 8) copy8bitSamples(read); + else copy16bitSamples(read); + if (!stream && read) // the sound may not start in the first frame, but the stream cannot start without any sound data present. + stream = S_CreateCustomStream(6000, adata.inf.sampleRate, adata.inf.nChannels, StreamCallbackFunc, this); + + } + + } + + if (frame > nFrame) + { + nFrame++; + Smacker_GetNextFrame(hSMK); + bool nostopsound = (flags & NOSOUNDCUTOFF); + if (!hassound) for (unsigned i = 0; i < animSnd.Size(); i += 2) + { + if (animSnd[i] == nFrame) + { + int sound = animSnd[i + 1]; + if (sound == -1) + soundEngine->StopAllChannels(); + else + soundEngine->StartSound(SOURCE_None, nullptr, nullptr, CHAN_AUTO, nostopsound ? CHANF_UI : CHANF_NONE, sound, 1.f, ATTN_NONE); + } + } + } + + return nFrame < nFrames; + } + + void Stop() override + { + if (stream) S_StopCustomStream(stream); + bool nostopsound = (flags & NOSOUNDCUTOFF); + if (!nostopsound && !hassound) soundEngine->StopAllChannels(); + } + + ~SmkPlayer() + { + Smacker_Close(hSMK); + animtex.Clean(); + } + + FTextureID GetTexture() override + { + return animtex.GetFrameID(); + } + +}; + +//--------------------------------------------------------------------------- +// +// +// +//--------------------------------------------------------------------------- + +MoviePlayer* OpenMovie(const char* filename, TArray& ans, const int* frameticks, int flags, FString& error) +{ + FileReader fr; + // first try as .ivf - but only if sounds are provided - the decoder is video only. + if (ans.Size()) + { + auto fn = StripExtension(filename); + DefaultExtension(fn, ".ivf"); + fr = fileSystem.OpenFileReader(fn); + } + + if (!fr.isOpen()) fr = fileSystem.OpenFileReader(filename); + if (!fr.isOpen()) + { + size_t nLen = strlen(filename); + // Strip the drive letter and retry. + if (nLen >= 3 && isalpha(filename[0]) && filename[1] == ':' && filename[2] == '/') + { + filename += 3; + fr = fileSystem.OpenFileReader(filename); + } + if (!fr.isOpen()) + { + error.Format("%s: Unable to open video\n", filename); + return nullptr; + } + } + char id[20] = {}; + + fr.Read(&id, 20); + fr.Seek(-20, FileReader::SeekCur); + + if (!memcmp(id, "LPF ", 4)) + { + auto anm = new AnmPlayer(fr, ans, frameticks, flags); + if (!anm->isvalid()) + { + error.Format("%s: invalid ANM file.\n", filename); + delete anm; + return nullptr; + } + return anm; + } + else if (!memcmp(id, "SMK2", 4)) + { + fr.Close(); + auto anm = new SmkPlayer(filename, ans, flags); + if (!anm->isvalid()) + { + error.Format("%s: invalid SMK file.\n", filename); + delete anm; + return nullptr; + } + return anm; + } + else if (!memcmp(id, "Interplay MVE File", 18)) + { + auto anm = new MvePlayer(fr); + if (!anm->isvalid()) + { + delete anm; + return nullptr; + } + return anm; + } + else if (!memcmp(id, "DKIF\0\0 \0VP80", 12)) + { + auto anm = new VpxPlayer(fr, ans, frameticks ? frameticks[1] : 0, flags, error); + if (!anm->isvalid()) + { + delete anm; + return nullptr; + } + // VPX files have no sound track, so look for a same-named sound file with a known extension as the soundtrack to be played. + static const char* knownSoundExts[] = { "OGG", "FLAC", "MP3", "OPUS", "WAV" }; + FString name = StripExtension(filename); + anm->soundtrack = fileSystem.FindFileWithExtensions(name, knownSoundExts, countof(knownSoundExts)); + return anm; + } + // add more formats here. + else + { + error.Format("%s: Unknown video format\n", filename); + return nullptr; + } +} + +//--------------------------------------------------------------------------- +// +// +// +//--------------------------------------------------------------------------- + +DEFINE_ACTION_FUNCTION(_MoviePlayer, Create) +{ + PARAM_PROLOGUE; + PARAM_STRING(filename); + PARAM_POINTER(sndinf, TArray); + PARAM_INT(flags); + PARAM_INT(frametime); + PARAM_INT(firstframetime); + PARAM_INT(lastframetime); + + FString error; + if (firstframetime == -1) firstframetime = frametime; + if (lastframetime == -1) lastframetime = frametime; + int frametimes[] = { firstframetime, frametime, lastframetime }; + auto movie = OpenMovie(filename, *sndinf, frametime == -1? nullptr : frametimes, flags, error); + if (!movie) + { + Printf(TEXTCOLOR_YELLOW, "%s", error.GetChars()); + } + ACTION_RETURN_POINTER(movie); +} + +DEFINE_ACTION_FUNCTION(_MoviePlayer, Start) +{ + PARAM_SELF_STRUCT_PROLOGUE(MoviePlayer); + self->Start(); + return 0; +} + +DEFINE_ACTION_FUNCTION(_MoviePlayer, Frame) +{ + PARAM_SELF_STRUCT_PROLOGUE(MoviePlayer); + PARAM_FLOAT(clock); + ACTION_RETURN_INT(self->Frame(int64_t(clock))); + return 0; +} + +DEFINE_ACTION_FUNCTION(_MoviePlayer, Destroy) +{ + PARAM_SELF_STRUCT_PROLOGUE(MoviePlayer); + self->Stop(); + delete self; + return 0; +} + +DEFINE_ACTION_FUNCTION(_MoviePlayer, GetTexture) +{ + PARAM_SELF_STRUCT_PROLOGUE(MoviePlayer); + ACTION_RETURN_INT(self->GetTexture().GetIndex()); +} diff --git a/src/common/cutscenes/playmve.cpp b/src/common/cutscenes/playmve.cpp new file mode 100644 index 0000000000..4967d2ccc7 --- /dev/null +++ b/src/common/cutscenes/playmve.cpp @@ -0,0 +1,1008 @@ +/* + * InterplayDecoder + * Copyright (C) 2020 sirlemonhead + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + */ + +/* This code is based on interplayvideo.c, dpcm.c and ipmovie.c from the FFmpeg project which can be obtained + * from http://www.ffmpeg.org/. Below is the license from interplayvideo.c + * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */ + +/* + * Interplay MVE Video Decoder + * Copyright (C) 2003 The FFmpeg project + * + * This file is part of FFmpeg. + * + * FFmpeg is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * FFmpeg is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with FFmpeg; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + */ + + +#include +#include "playmve.h" +#include "printf.h" +#include "v_draw.h" +#include "s_music.h" +#include "cmdlib.h" +#include "templates.h" + + + +static const int16_t delta_table[] = { + 0, 1, 2, 3, 4, 5, 6, 7, + 8, 9, 10, 11, 12, 13, 14, 15, + 16, 17, 18, 19, 20, 21, 22, 23, + 24, 25, 26, 27, 28, 29, 30, 31, + 32, 33, 34, 35, 36, 37, 38, 39, + 40, 41, 42, 43, 47, 51, 56, 61, + 66, 72, 79, 86, 94, 102, 112, 122, + 133, 145, 158, 173, 189, 206, 225, 245, + 267, 292, 318, 348, 379, 414, 452, 493, + 538, 587, 640, 699, 763, 832, 908, 991, + 1081, 1180, 1288, 1405, 1534, 1673, 1826, 1993, + 2175, 2373, 2590, 2826, 3084, 3365, 3672, 4008, + 4373, 4772, 5208, 5683, 6202, 6767, 7385, 8059, + 8794, 9597, 10472, 11428, 12471, 13609, 14851, 16206, + 17685, 19298, 21060, 22981, 25078, 27367, 29864, 32589, + -29973, -26728, -23186, -19322, -15105, -10503, -5481, -1, + 1, 1, 5481, 10503, 15105, 19322, 23186, 26728, + 29973, -32589, -29864, -27367, -25078, -22981, -21060, -19298, + -17685, -16206, -14851, -13609, -12471, -11428, -10472, -9597, + -8794, -8059, -7385, -6767, -6202, -5683, -5208, -4772, + -4373, -4008, -3672, -3365, -3084, -2826, -2590, -2373, + -2175, -1993, -1826, -1673, -1534, -1405, -1288, -1180, + -1081, -991, -908, -832, -763, -699, -640, -587, + -538, -493, -452, -414, -379, -348, -318, -292, + -267, -245, -225, -206, -189, -173, -158, -145, + -133, -122, -112, -102, -94, -86, -79, -72, + -66, -61, -56, -51, -47, -43, -42, -41, + -40, -39, -38, -37, -36, -35, -34, -33, + -32, -31, -30, -29, -28, -27, -26, -25, + -24, -23, -22, -21, -20, -19, -18, -17, + -16, -15, -14, -13, -12, -11, -10, -9, + -8, -7, -6, -5, -4, -3, -2, -1 +}; + + +// macro to fetch 16-bit little-endian words from a bytestream +#define LE_16(x) ((*x) | ((*(x+1)) << 8)) + +static bool StreamCallbackFunc(SoundStream* stream, void* buff, int len, void* userdata) +{ + InterplayDecoder* pId = (InterplayDecoder*)userdata; + memcpy(buff, &pId->audio.samples[pId->audio.nRead], len); + pId->audio.nRead += len / 2; + if (pId->audio.nRead >= countof(pId->audio.samples)) pId->audio.nRead = 0; + return true; +} + +InterplayDecoder::InterplayDecoder(bool soundenabled) +{ + bIsPlaying = false; + bAudioStarted = !soundenabled; // This prevents the stream from getting created + + nWidth = 0; + nHeight = 0; + nFrame = 0; + + memset(palette, 0, sizeof(palette)); + memset(&audio, 0, sizeof(audio)); + audio.nRead = 18000; // skip the initial silence. This is needed to sync audio and video because OpenAL's lag is a bit on the high side. + + + nFps = 0.0; + nFrameDuration = 0; + nTimerRate = 0; + nTimerDiv = 0; + + pVideoBuffers[0] = nullptr; + pVideoBuffers[1] = nullptr; + + decodeMap.pData = nullptr; + decodeMap.nSize = 0; + + nCurrentVideoBuffer = 0; + nPreviousVideoBuffer = 1; + + videoStride = 0; +} + +InterplayDecoder::~InterplayDecoder() +{ + Close(); +} + +void InterplayDecoder::SwapFrames() +{ + int t = nPreviousVideoBuffer; + nPreviousVideoBuffer = nCurrentVideoBuffer; + nCurrentVideoBuffer = t; +} + +void InterplayDecoder::Close() +{ + fr.Close(); + bIsPlaying = false; + if (stream) + S_StopCustomStream(stream); + stream = nullptr; + + if (decodeMap.pData) { + delete[] decodeMap.pData; + decodeMap.pData = nullptr; + } + + if (pVideoBuffers[0]) { + delete[] pVideoBuffers[0]; + pVideoBuffers[0] = nullptr; + } + if (pVideoBuffers[1]) { + delete[] pVideoBuffers[1]; + pVideoBuffers[1] = nullptr; + } + +} + +bool InterplayDecoder::Open(FileReader &fr_) +{ + // open the file (read only) + char lsig[20]; + + // check the file signature + fr_.Read((uint8_t*)lsig, sizeof(lsig)); + if (memcmp(lsig, "Interplay MVE File\x1A\0", sizeof(lsig)) != 0) + { + Printf(TEXTCOLOR_RED "InterplayDecoder: Unknown MVE signature\n "); + return false; + } + + // skip the next 6 bytes + fr_.Seek(6, FileReader::SeekCur); + fr = std::move(fr_); + + //Run(); + + return true; +} + +bool InterplayDecoder::RunFrame(uint64_t clock) +{ + uint8_t chunkPreamble[CHUNK_PREAMBLE_SIZE]; + uint8_t opcodePreamble[OPCODE_PREAMBLE_SIZE]; + uint8_t opcodeType; + uint8_t opcodeVersion; + int opcodeSize, chunkSize; + int chunkType = 0; + + // iterate through the chunks in the file + do + { + // handle timing - wait until we're ready to process the next frame. + if (nNextFrameTime > clock) { + return true; + } + else { + nNextFrameTime += nFrameDuration; + } + + if (fr.Read(chunkPreamble, CHUNK_PREAMBLE_SIZE) != CHUNK_PREAMBLE_SIZE) { + Printf(TEXTCOLOR_RED "InterplayDecoder: could not read from file (EOF?)\n"); + return false; + } + + chunkSize = LE_16(&chunkPreamble[0]); + chunkType = LE_16(&chunkPreamble[2]); + + // iterate through individual opcodes + while (chunkSize > 0) + { + if (fr.Read(opcodePreamble, OPCODE_PREAMBLE_SIZE) != OPCODE_PREAMBLE_SIZE) + { + Printf(TEXTCOLOR_RED "InterplayDecoder: could not read from file (EOF?)\n"); + return false; + } + + opcodeSize = LE_16(&opcodePreamble[0]); + opcodeType = opcodePreamble[2]; + opcodeVersion = opcodePreamble[3]; + + chunkSize -= OPCODE_PREAMBLE_SIZE; + chunkSize -= opcodeSize; + + switch (opcodeType) + { + case OPCODE_END_OF_STREAM: + { + fr.Seek(opcodeSize, FileReader::SeekCur); + break; + } + + case OPCODE_END_OF_CHUNK: + { + fr.Seek(opcodeSize, FileReader::SeekCur); + break; + } + + case OPCODE_CREATE_TIMER: + { + nTimerRate = fr.ReadUInt32(); + nTimerDiv = fr.ReadUInt16(); + nFrameDuration = ((uint64_t)nTimerRate * nTimerDiv) * 1000; + break; + } + + case OPCODE_INIT_AUDIO_BUFFERS: + { + fr.Seek(2, FileReader::SeekCur); + uint16_t flags = fr.ReadUInt16(); + audio.nSampleRate = fr.ReadUInt16(); + + uint32_t nBufferBytes; + + if (opcodeVersion == 0) { + nBufferBytes = fr.ReadUInt16(); + } + else { + nBufferBytes = fr.ReadUInt32(); + } + + if (flags & 0x1) { + audio.nChannels = 2; + } + else { + audio.nChannels = 1; + } + if (flags & 0x2) { + audio.nBitDepth = 16; + } + else { + audio.nBitDepth = 8; + } + break; + } + + case OPCODE_START_STOP_AUDIO: + { + if (!bAudioStarted) + { + // start audio playback + stream = S_CreateCustomStream(6000, audio.nSampleRate, audio.nChannels, StreamCallbackFunc, this); + bAudioStarted = true; + } + + fr.Seek(opcodeSize, FileReader::SeekCur); + break; + } + + case OPCODE_INIT_VIDEO_BUFFERS: + { + assert(opcodeSize == 8); + nWidth = fr.ReadUInt16() * 8; + nHeight = fr.ReadUInt16() * 8; + + int count = fr.ReadUInt16(); + int truecolour = fr.ReadUInt16(); + assert(truecolour == 0); + + pVideoBuffers[0] = new uint8_t[nWidth * nHeight]; + pVideoBuffers[1] = new uint8_t[nWidth * nHeight]; + + videoStride = nWidth; + + animtex.SetSize(AnimTexture::Paletted, nWidth, nHeight); + break; + } + + case OPCODE_UNKNOWN_06: + case OPCODE_UNKNOWN_0E: + case OPCODE_UNKNOWN_10: + case OPCODE_UNKNOWN_12: + case OPCODE_UNKNOWN_13: + case OPCODE_UNKNOWN_14: + case OPCODE_UNKNOWN_15: + { + fr.Seek(opcodeSize, FileReader::SeekCur); + break; + } + + case OPCODE_SEND_BUFFER: + { + int nPalStart = fr.ReadUInt16(); + int nPalCount = fr.ReadUInt16(); + + animtex.SetFrame(&palette[0].r , GetCurrentFrame()); + + nFrame++; + SwapFrames(); + + fr.Seek(opcodeSize-4, FileReader::SeekCur); + break; + } + + case OPCODE_AUDIO_FRAME: + { + int nStart = (int)fr.Tell(); + uint16_t seqIndex = fr.ReadUInt16(); + uint16_t streamMask = fr.ReadUInt16(); + uint16_t nSamples = fr.ReadUInt16(); // number of samples this chunk + + int predictor[2]; + int i = 0; + + for (int ch = 0; ch < audio.nChannels; ch++) + { + predictor[ch] = fr.ReadUInt16(); + i++; + + if (predictor[ch] & 0x8000) { + predictor[ch] |= 0xFFFF0000; // sign extend + } + + audio.samples[audio.nWrite++] = predictor[ch]; + if (audio.nWrite >= countof(audio.samples)) audio.nWrite = 0; + } + + int ch = 0; + for (; i < (nSamples / 2); i++) + { + predictor[ch] += delta_table[fr.ReadUInt8()]; + predictor[ch] = clamp(predictor[ch], -32768, 32768); + + audio.samples[audio.nWrite++] = predictor[ch]; + if (audio.nWrite >= countof(audio.samples)) audio.nWrite = 0; + + // toggle channel + ch ^= audio.nChannels - 1; + } + + int nEnd = (int)fr.Tell(); + int nRead = nEnd - nStart; + assert(opcodeSize == nRead); + break; + } + + case OPCODE_SILENCE_FRAME: + { + uint16_t seqIndex = fr.ReadUInt16(); + uint16_t streamMask = fr.ReadUInt16(); + uint16_t nStreamLen = fr.ReadUInt16(); + break; + } + + case OPCODE_INIT_VIDEO_MODE: + { + fr.Seek(opcodeSize, FileReader::SeekCur); + break; + } + + case OPCODE_CREATE_GRADIENT: + { + fr.Seek(opcodeSize, FileReader::SeekCur); + Printf("InterplayDecoder: Create gradient not supported.\n"); + break; + } + + case OPCODE_SET_PALETTE: + { + if (opcodeSize > 0x304 || opcodeSize < 4) { + Printf("set_palette opcode with invalid size\n"); + chunkType = CHUNK_BAD; + break; + } + + int nPalStart = fr.ReadUInt16(); + int nPalCount = fr.ReadUInt16(); + for (int i = nPalStart; i <= nPalCount; i++) + { + palette[i].r = fr.ReadUInt8() << 2; + palette[i].g = fr.ReadUInt8() << 2; + palette[i].b = fr.ReadUInt8() << 2; + } + break; + } + + case OPCODE_SET_PALETTE_COMPRESSED: + { + fr.Seek(opcodeSize, FileReader::SeekCur); + Printf("InterplayDecoder: Set palette compressed not supported.\n"); + break; + } + + case OPCODE_SET_DECODING_MAP: + { + if (!decodeMap.pData) + { + decodeMap.pData = new uint8_t[opcodeSize]; + decodeMap.nSize = opcodeSize; + } + else + { + if (opcodeSize != decodeMap.nSize) { + delete[] decodeMap.pData; + decodeMap.pData = new uint8_t[opcodeSize]; + decodeMap.nSize = opcodeSize; + } + } + + int nRead = (int)fr.Read(decodeMap.pData, opcodeSize); + assert(nRead == opcodeSize); + break; + } + + case OPCODE_VIDEO_DATA: + { + int nStart = (int)fr.Tell(); + + // need to skip 14 bytes + fr.Seek(14, FileReader::SeekCur); + + if (decodeMap.nSize) + { + int i = 0; + + for (uint32_t y = 0; y < nHeight; y += 8) + { + for (uint32_t x = 0; x < nWidth; x += 8) + { + uint32_t opcode; + + // alternate between getting low and high 4 bits + if (i & 1) { + opcode = decodeMap.pData[i >> 1] >> 4; + } + else { + opcode = decodeMap.pData[i >> 1] & 0x0F; + } + i++; + + int32_t offset = x + (y * videoStride); + + switch (opcode) + { + default: + break; + case 0: + DecodeBlock0(offset); + break; + case 1: + DecodeBlock1(offset); + break; + case 2: + DecodeBlock2(offset); + break; + case 3: + DecodeBlock3(offset); + break; + case 4: + DecodeBlock4(offset); + break; + case 5: + DecodeBlock5(offset); + break; + case 7: + DecodeBlock7(offset); + break; + case 8: + DecodeBlock8(offset); + break; + case 9: + DecodeBlock9(offset); + break; + case 10: + DecodeBlock10(offset); + break; + case 11: + DecodeBlock11(offset); + break; + case 12: + DecodeBlock12(offset); + break; + case 13: + DecodeBlock13(offset); + break; + case 14: + DecodeBlock14(offset); + break; + case 15: + DecodeBlock15(offset); + break; + } + } + } + } + + int nEnd = (int)fr.Tell(); + int nSkipBytes = opcodeSize - (nEnd - nStart); // we can end up with 1 byte left we need to skip + assert(nSkipBytes <= 1); + + fr.Seek(nSkipBytes, FileReader::SeekCur); + break; + } + + default: + break; + } + } + + } + while (chunkType < CHUNK_VIDEO && bIsPlaying); + return chunkType != CHUNK_END; +} + +void InterplayDecoder::CopyBlock(uint8_t* pDest, uint8_t* pSrc) +{ + for (int y = 0; y < 8; y++) + { + memcpy(pDest, pSrc, 8); + pSrc += (intptr_t)videoStride; + pDest += (intptr_t)videoStride; + } +} + +void InterplayDecoder::DecodeBlock0(int32_t offset) +{ + // copy from the same offset but from the previous frame + uint8_t* pDest = GetCurrentFrame() + (intptr_t)offset; + uint8_t* pSrc = GetPreviousFrame() + (intptr_t)offset; + + CopyBlock(pDest, pSrc); +} + +void InterplayDecoder::DecodeBlock1(int32_t offset) +{ + // nothing to do for this. +} + +void InterplayDecoder::DecodeBlock2(int32_t offset) +{ + // copy block from 2 frames ago using a motion vector; need 1 more byte + uint8_t B = fr.ReadUInt8(); + + int x, y; + + if (B < 56) { + x = 8 + (B % 7); + y = B / 7; + } + else { + x = -14 + ((B - 56) % 29); + y = 8 + ((B - 56) / 29); + } + + uint8_t* pDest = GetCurrentFrame() + (intptr_t)offset; + uint8_t* pSrc = GetCurrentFrame() + (intptr_t)(int64_t)offset + (int64_t)x + (int64_t(y) * (int64_t)videoStride); + + CopyBlock(pDest, pSrc); +} + +void InterplayDecoder::DecodeBlock3(int32_t offset) +{ + // copy 8x8 block from current frame from an up/left block + uint8_t B = fr.ReadUInt8(); + + int x, y; + + // need 1 more byte for motion + if (B < 56) { + x = -(8 + (B % 7)); + y = -(B / 7); + } + else { + x = -(-14 + ((B - 56) % 29)); + y = -(8 + ((B - 56) / 29)); + } + + uint8_t* pDest = GetCurrentFrame() + (intptr_t)offset; + uint8_t* pSrc = GetCurrentFrame() + (intptr_t)(int64_t)offset + (int64_t)x + (int64_t(y) * (int64_t)videoStride); + + CopyBlock(pDest, pSrc); +} + +void InterplayDecoder::DecodeBlock4(int32_t offset) +{ + // copy a block from the previous frame; need 1 more byte + int x, y; + uint8_t B, BL, BH; + + B = fr.ReadUInt8(); + + BL = B & 0x0F; + BH = (B >> 4) & 0x0F; + x = -8 + BL; + y = -8 + BH; + + uint8_t* pDest = GetCurrentFrame() + (intptr_t)offset; + uint8_t* pSrc = GetPreviousFrame() + (intptr_t)(int64_t)offset + (int64_t)x + (int64_t(y) * (int64_t)videoStride); + + CopyBlock(pDest, pSrc); +} + +void InterplayDecoder::DecodeBlock5(int32_t offset) +{ + // copy a block from the previous frame using an expanded range; need 2 more bytes + int8_t x = fr.ReadUInt8(); + int8_t y = fr.ReadUInt8(); + + uint8_t* pDest = GetCurrentFrame() + (intptr_t)offset; + uint8_t* pSrc = GetPreviousFrame() + (intptr_t)(int64_t)offset + (int64_t)x + (int64_t(y) * (int64_t)videoStride); + + CopyBlock(pDest, pSrc); +} + +// Block6 is unknown and skipped + +void InterplayDecoder::DecodeBlock7(int32_t offset) +{ + uint8_t* pBuffer = GetCurrentFrame() + (intptr_t)offset; + uint32_t flags = 0; + + uint8_t P[2]; + P[0] = fr.ReadUInt8(); + P[1] = fr.ReadUInt8(); + + // 2-color encoding + if (P[0] <= P[1]) + { + // need 8 more bytes from the stream + for (int y = 0; y < 8; y++) + { + flags = fr.ReadUInt8() | 0x100; + for (; flags != 1; flags >>= 1) { + *pBuffer++ = P[flags & 1]; + } + pBuffer += (videoStride - 8); + } + } + else + { + // need 2 more bytes from the stream + flags = fr.ReadUInt16(); + + for (int y = 0; y < 8; y += 2) + { + for (int x = 0; x < 8; x += 2, flags >>= 1) + { + pBuffer[x] = + pBuffer[x + 1] = + pBuffer[x + videoStride] = + pBuffer[x + 1 + videoStride] = P[flags & 1]; + } + pBuffer += videoStride * 2; + } + } +} + +void InterplayDecoder::DecodeBlock8(int32_t offset) +{ + uint8_t* pBuffer = GetCurrentFrame() + (intptr_t)offset; + uint32_t flags = 0; + uint8_t P[4]; + + // 2-color encoding for each 4x4 quadrant, or 2-color encoding on either top and bottom or left and right halves + P[0] = fr.ReadUInt8(); + P[1] = fr.ReadUInt8(); + + if (P[0] <= P[1]) + { + for (int y = 0; y < 16; y++) + { + // new values for each 4x4 block + if (!(y & 3)) + { + if (y) { + P[0] = fr.ReadUInt8(); + P[1] = fr.ReadUInt8(); + } + flags = fr.ReadUInt16(); + } + + for (int x = 0; x < 4; x++, flags >>= 1) { + *pBuffer++ = P[flags & 1]; + } + + pBuffer += videoStride - 4; + // switch to right half + if (y == 7) pBuffer -= 8 * videoStride - 4; + } + } + else + { + flags = fr.ReadUInt32(); + P[2] = fr.ReadUInt8(); + P[3] = fr.ReadUInt8(); + + if (P[2] <= P[3]) + { + // vertical split; left & right halves are 2-color encoded + for (int y = 0; y < 16; y++) + { + for (int x = 0; x < 4; x++, flags >>= 1) { + *pBuffer++ = P[flags & 1]; + } + + pBuffer += videoStride - 4; + + // switch to right half + if (y == 7) { + pBuffer -= 8 * videoStride - 4; + P[0] = P[2]; + P[1] = P[3]; + flags = fr.ReadUInt32(); + } + } + } + else + { + // horizontal split; top & bottom halves are 2-color encoded + for (int y = 0; y < 8; y++) + { + if (y == 4) { + P[0] = P[2]; + P[1] = P[3]; + flags = fr.ReadUInt32(); + } + + for (int x = 0; x < 8; x++, flags >>= 1) + *pBuffer++ = P[flags & 1]; + + pBuffer += (videoStride - 8); + } + } + } +} + +void InterplayDecoder::DecodeBlock9(int32_t offset) +{ + uint8_t* pBuffer = GetCurrentFrame() + (intptr_t)offset; + uint8_t P[4]; + + fr.Read(P, 4); + + // 4-color encoding + if (P[0] <= P[1]) + { + if (P[2] <= P[3]) + { + // 1 of 4 colors for each pixel, need 16 more bytes + for (int y = 0; y < 8; y++) + { + // get the next set of 8 2-bit flags + int flags = fr.ReadUInt16(); + + for (int x = 0; x < 8; x++, flags >>= 2) { + *pBuffer++ = P[flags & 0x03]; + } + + pBuffer += (videoStride - 8); + } + } + else + { + // 1 of 4 colors for each 2x2 block, need 4 more bytes + uint32_t flags = fr.ReadUInt32(); + + for (int y = 0; y < 8; y += 2) + { + for (int x = 0; x < 8; x += 2, flags >>= 2) + { + pBuffer[x] = + pBuffer[x + 1] = + pBuffer[x + videoStride] = + pBuffer[x + 1 + videoStride] = P[flags & 0x03]; + } + + pBuffer += videoStride * 2; + } + } + } + else + { + // 1 of 4 colors for each 2x1 or 1x2 block, need 8 more bytes + uint64_t flags = fr.ReadUInt64(); + + if (P[2] <= P[3]) + { + for (int y = 0; y < 8; y++) + { + for (int x = 0; x < 8; x += 2, flags >>= 2) + { + pBuffer[x] = + pBuffer[x + 1] = P[flags & 0x03]; + } + pBuffer += videoStride; + } + } + else + { + for (int y = 0; y < 8; y += 2) + { + for (int x = 0; x < 8; x++, flags >>= 2) + { + pBuffer[x] = + pBuffer[x + videoStride] = P[flags & 0x03]; + } + pBuffer += videoStride * 2; + } + } + } +} + +void InterplayDecoder::DecodeBlock10(int32_t offset) +{ + uint8_t* pBuffer = GetCurrentFrame() + (intptr_t)offset; + uint8_t P[8]; + + fr.Read(P, 4); + + // 4-color encoding for each 4x4 quadrant, or 4-color encoding on either top and bottom or left and right halves + if (P[0] <= P[1]) + { + int flags = 0; + + // 4-color encoding for each quadrant; need 32 bytes + for (int y = 0; y < 16; y++) + { + // new values for each 4x4 block + if (!(y & 3)) { + if (y) fr.Read(P, 4); + flags = fr.ReadUInt32(); + } + + for (int x = 0; x < 4; x++, flags >>= 2) { + *pBuffer++ = P[flags & 0x03]; + } + + pBuffer += videoStride - 4; + // switch to right half + if (y == 7) pBuffer -= 8 * videoStride - 4; + } + } + else + { + // vertical split? + int vert; + uint64_t flags = fr.ReadUInt64(); + + fr.Read(P + 4, 4); + vert = P[4] <= P[5]; + + // 4-color encoding for either left and right or top and bottom halves + for (int y = 0; y < 16; y++) + { + for (int x = 0; x < 4; x++, flags >>= 2) + *pBuffer++ = P[flags & 0x03]; + + if (vert) + { + pBuffer += videoStride - 4; + // switch to right half + if (y == 7) pBuffer -= 8 * videoStride - 4; + } + else if (y & 1) pBuffer += (videoStride - 8); + + // load values for second half + if (y == 7) { + memcpy(P, P + 4, 4); + flags = fr.ReadUInt64(); + } + } + } +} + +void InterplayDecoder::DecodeBlock11(int32_t offset) +{ + // 64-color encoding (each pixel in block is a different color) + uint8_t* pBuffer = GetCurrentFrame() + (intptr_t)offset; + + for (int y = 0; y < 8; y++) + { + fr.Read(pBuffer, 8); + pBuffer += videoStride; + } +} + +void InterplayDecoder::DecodeBlock12(int32_t offset) +{ + // 16-color block encoding: each 2x2 block is a different color + uint8_t* pBuffer = GetCurrentFrame() + (intptr_t)offset; + + for (int y = 0; y < 8; y += 2) + { + for (int x = 0; x < 8; x += 2) + { + pBuffer[x] = + pBuffer[x + 1] = + pBuffer[x + videoStride] = + pBuffer[x + 1 + videoStride] = fr.ReadUInt8(); + } + pBuffer += videoStride * 2; + } +} + +void InterplayDecoder::DecodeBlock13(int32_t offset) +{ + // 4-color block encoding: each 4x4 block is a different color + uint8_t* pBuffer = GetCurrentFrame() + (intptr_t)offset; + uint8_t P[2]; + + for (int y = 0; y < 8; y++) + { + if (!(y & 3)) + { + P[0] = fr.ReadUInt8(); + P[1] = fr.ReadUInt8(); + } + + memset(pBuffer, P[0], 4); + memset(pBuffer + 4, P[1], 4); + pBuffer += videoStride; + } +} + +void InterplayDecoder::DecodeBlock14(int32_t offset) +{ + // 1-color encoding : the whole block is 1 solid color + uint8_t* pBuffer = GetCurrentFrame() + (intptr_t)offset; + uint8_t pix = fr.ReadUInt8(); + + for (int y = 0; y < 8; y++) + { + memset(pBuffer, pix, 8); + pBuffer += videoStride; + } +} + +void InterplayDecoder::DecodeBlock15(int32_t offset) +{ + // dithered encoding + uint8_t* pBuffer = GetCurrentFrame() + (intptr_t)offset; + uint8_t P[2]; + + P[0] = fr.ReadUInt8(); + P[1] = fr.ReadUInt8(); + + for (int y = 0; y < 8; y++) + { + for (int x = 0; x < 8; x += 2) + { + *pBuffer++ = P[y & 1]; + *pBuffer++ = P[!(y & 1)]; + } + pBuffer += (videoStride - 8); + } +} + +uint8_t* InterplayDecoder::GetCurrentFrame() +{ + return pVideoBuffers[nCurrentVideoBuffer]; +} + +uint8_t* InterplayDecoder::GetPreviousFrame() +{ + return pVideoBuffers[nPreviousVideoBuffer]; +} diff --git a/src/common/cutscenes/playmve.h b/src/common/cutscenes/playmve.h new file mode 100644 index 0000000000..77e89a10be --- /dev/null +++ b/src/common/cutscenes/playmve.h @@ -0,0 +1,179 @@ +/* + * InterplayDecoder + * Copyright (C) 2020 sirlemonhead + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + */ + +/* This code is based on interplayvideo.c, dpcm.c and ipmovie.c from the FFmpeg project which can be obtained + * from http://www.ffmpeg.org/. Below is the license from interplayvideo.c + * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */ + +/* + * Interplay MVE Video Decoder + * Copyright (C) 2003 The FFmpeg project + * + * This file is part of FFmpeg. + * + * FFmpeg is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * FFmpeg is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with FFmpeg; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + */ + +#pragma once + +#include "files.h" +#include "animtexture.h" +#include "s_music.h" + +#ifndef playmve_h_ +#define playmve_h_ + +class InterplayDecoder +{ +public: + enum + { + CHUNK_PREAMBLE_SIZE = 4, + OPCODE_PREAMBLE_SIZE = 4, + + CHUNK_INIT_AUDIO = 0x0000, + CHUNK_AUDIO_ONLY = 0x0001, + CHUNK_INIT_VIDEO = 0x0002, + CHUNK_VIDEO = 0x0003, + CHUNK_SHUTDOWN = 0x0004, + CHUNK_END = 0x0005, + /* these last types are used internally */ + CHUNK_DONE = 0xFFFC, + CHUNK_NOMEM = 0xFFFD, + CHUNK_EOF = 0xFFFE, + CHUNK_BAD = 0xFFFF, + + OPCODE_END_OF_STREAM = 0x00, + OPCODE_END_OF_CHUNK = 0x01, + OPCODE_CREATE_TIMER = 0x02, + OPCODE_INIT_AUDIO_BUFFERS = 0x03, + OPCODE_START_STOP_AUDIO = 0x04, + OPCODE_INIT_VIDEO_BUFFERS = 0x05, + OPCODE_UNKNOWN_06 = 0x06, + OPCODE_SEND_BUFFER = 0x07, + OPCODE_AUDIO_FRAME = 0x08, + OPCODE_SILENCE_FRAME = 0x09, + OPCODE_INIT_VIDEO_MODE = 0x0A, + OPCODE_CREATE_GRADIENT = 0x0B, + OPCODE_SET_PALETTE = 0x0C, + OPCODE_SET_PALETTE_COMPRESSED = 0x0D, + OPCODE_UNKNOWN_0E = 0x0E, + OPCODE_SET_DECODING_MAP = 0x0F, + OPCODE_UNKNOWN_10 = 0x10, + OPCODE_VIDEO_DATA = 0x11, + OPCODE_UNKNOWN_12 = 0x12, + OPCODE_UNKNOWN_13 = 0x13, + OPCODE_UNKNOWN_14 = 0x14, + OPCODE_UNKNOWN_15 = 0x15, + + PALETTE_COUNT = 256, + kAudioBlocks = 20 // alloc a lot of blocks - need to store lots of audio data before video frames start. + }; + + InterplayDecoder(bool soundenabled); + ~InterplayDecoder(); + + bool Open(FileReader &fr); + void Close(); + bool RunFrame(uint64_t clock); + + struct AudioData + { + int hFx; + int nChannels; + uint16_t nSampleRate; + uint8_t nBitDepth; + + int16_t samples[6000 * kAudioBlocks]; // must be a multiple of the stream buffer size + int nWrite; + int nRead; + }; + + AudioData audio; + AnimTextures animtex; + + AnimTextures& animTex() { return animtex; } + +private: + struct DecodeMap + { + uint8_t* pData; + uint32_t nSize; + }; + + struct Palette + { + uint8_t r; + uint8_t g; + uint8_t b; + }; + + uint8_t* GetCurrentFrame(); + uint8_t* GetPreviousFrame(); + void SwapFrames(); + void CopyBlock(uint8_t* pDest, uint8_t* pSrc); + void DecodeBlock0(int32_t offset); + void DecodeBlock1(int32_t offset); + void DecodeBlock2(int32_t offset); + void DecodeBlock3(int32_t offset); + void DecodeBlock4(int32_t offset); + void DecodeBlock5(int32_t offset); + void DecodeBlock7(int32_t offset); + void DecodeBlock8(int32_t offset); + void DecodeBlock9(int32_t offset); + void DecodeBlock10(int32_t offset); + void DecodeBlock11(int32_t offset); + void DecodeBlock12(int32_t offset); + void DecodeBlock13(int32_t offset); + void DecodeBlock14(int32_t offset); + void DecodeBlock15(int32_t offset); + + FileReader fr; + + bool bIsPlaying, bAudioStarted; + + uint32_t nTimerRate, nTimerDiv; + uint32_t nWidth, nHeight, nFrame; + double nFps; + uint64_t nFrameDuration; + + uint8_t* pVideoBuffers[2]; + uint32_t nCurrentVideoBuffer, nPreviousVideoBuffer; + int32_t videoStride; + + DecodeMap decodeMap; + + Palette palette[256]; + uint64_t nNextFrameTime = 0; + SoundStream* stream = nullptr; +}; + +#endif diff --git a/src/common/cutscenes/screenjob.cpp b/src/common/cutscenes/screenjob.cpp new file mode 100644 index 0000000000..e828284822 --- /dev/null +++ b/src/common/cutscenes/screenjob.cpp @@ -0,0 +1,364 @@ +/* +** screenjob.cpp +** +** Generic cutscene display +** +**--------------------------------------------------------------------------- +** Copyright 2020 Christoph Oelckers +** All rights reserved. +** +** Redistribution and use in source and binary forms, with or without +** modification, are permitted provided that the following conditions +** are met: +** +** 1. Redistributions of source code must retain the above copyright +** notice, this list of conditions and the following disclaimer. +** 2. Redistributions in binary form must reproduce the above copyright +** notice, this list of conditions and the following disclaimer in the +** documentation and/or other materials provided with the distribution. +** 3. The name of the author may not be used to endorse or promote products +** derived from this software without specific prior written permission. +** +** THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR +** IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES +** OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. +** IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, +** INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT +** NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF +** THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +**--------------------------------------------------------------------------- +** +*/ + +#include "types.h" +#include "screenjob.h" +#include "i_time.h" +#include "v_2ddrawer.h" +#include "animlib.h" +#include "v_draw.h" +#include "s_soundinternal.h" +#include "animtexture.h" +#include "gamestate.h" +#include "vm.h" +#include "c_bind.h" +#include "c_console.h" +#include "gamestate.h" +#include "printf.h" +#include "c_dispatch.h" +#include "s_music.h" +#include "m_argv.h" + +static DObject* runner; +static PClass* runnerclass; +static PType* runnerclasstype; +static CompletionFunc completion; +static int ticks; +int intermissiondelay; + +//============================================================================= +// +// +// +//============================================================================= + +void Job_Init() +{ + static bool done = false; + if (!done) + { + done = true; + GC::AddMarkerFunc([] { GC::Mark(runner); }); + } + runnerclass = PClass::FindClass("ScreenJobRunner"); + if (!runnerclass) I_FatalError("ScreenJobRunner not defined"); + runnerclasstype = NewPointer(runnerclass); +} + +//============================================================================= +// +// +// +//============================================================================= + +static VMFunction* LookupFunction(const char* qname, bool validate = true) +{ + size_t p = strcspn(qname, "."); + if (p == 0) I_Error("Call to undefined function %s", qname); + FString clsname(qname, p); + FString funcname = qname + p + 1; + + auto func = PClass::FindFunction(clsname, funcname); + if (func == nullptr) I_Error("Call to undefined function %s", qname); + if (validate) + { + // these conditions must be met by all functions for this interface. + if (func->Proto->ReturnTypes.Size() != 0) I_Error("Bad cutscene function %s. Return value not allowed", qname); + if (func->ImplicitArgs != 0) I_Error("Bad cutscene function %s. Must be static", qname); + } + return func; +} + +//============================================================================= +// +// +// +//============================================================================= + +void CallCreateFunction(const char* qname, DObject* runner) +{ + auto func = LookupFunction(qname); + if (func->Proto->ArgumentTypes.Size() != 1) I_Error("Bad cutscene function %s. Must receive precisely one argument.", qname); + if (func->Proto->ArgumentTypes[0] != runnerclasstype) I_Error("Bad cutscene function %s. Must receive ScreenJobRunner reference.", qname); + VMValue val = runner; + VMCall(func, &val, 1, nullptr, 0); +} + +//============================================================================= +// +// +// +//============================================================================= + +DObject* CreateRunner(bool clearbefore = true) +{ + auto obj = runnerclass->CreateNew(); + auto func = LookupFunction("ScreenJobRunner.Init", false); + VMValue val[3] = { obj, clearbefore, false }; + VMCall(func, val, 3, nullptr, 0); + return obj; +} + +//============================================================================= +// +// +// +//============================================================================= + +void AddGenericVideo(DObject* runner, const FString& fn, int soundid, int fps) +{ + auto obj = runnerclass->CreateNew(); + auto func = LookupFunction("ScreenJobRunner.AddGenericVideo", false); + VMValue val[] = { runner, &fn, soundid, fps }; + VMCall(func, val, 4, nullptr, 0); +} + +//============================================================================= +// +// +// +//============================================================================= + +int CutsceneDef::GetSound() +{ + int id; + if (soundName.IsNotEmpty()) id = soundEngine->FindSound(soundName); + if (id <= 0) id = soundEngine->FindSoundByResID(soundID); + return id; +} + +void CutsceneDef::Create(DObject* runner) +{ + if (function.IsNotEmpty()) + { + CallCreateFunction(function, runner); + } + else if (video.IsNotEmpty()) + { + AddGenericVideo(runner, video, GetSound(), framespersec); + } +} + +//============================================================================= +// +// +// +//============================================================================= + +void DeleteScreenJob() +{ + if (runner) runner->Destroy(); + runner = nullptr; +} + +void EndScreenJob() +{ + DeleteScreenJob(); + if (completion) completion(false); + completion = nullptr; +} + + +//============================================================================= +// +// +// +//============================================================================= + +bool ScreenJobResponder(event_t* ev) +{ + if (ev->type == EV_KeyDown) + { + // We never reach the key binding checks in G_Responder, so for the console we have to check for ourselves here. + auto binding = Bindings.GetBinding(ev->data1); + if (binding.CompareNoCase("toggleconsole") == 0) + { + C_ToggleConsole(); + return true; + } + } + FInputEvent evt = ev; + if (runner) + { + IFVIRTUALPTRNAME(runner, NAME_ScreenJobRunner, OnEvent) + { + int result = 0; + VMValue parm[] = { runner, &evt }; + VMReturn ret(&result); + VMCall(func, parm, 2, &ret, 1); + return result; + } + } + return false; +} + +//============================================================================= +// +// +// +//============================================================================= + +bool ScreenJobTick() +{ + ticks++; + if (runner) + { + IFVIRTUALPTRNAME(runner, NAME_ScreenJobRunner, OnTick) + { + int result = 0; + VMValue parm[] = { runner }; + VMReturn ret(&result); + VMCall(func, parm, 1, &ret, 1); + return result; + } + } + return false; +} + +//============================================================================= +// +// +// +//============================================================================= + +void ScreenJobDraw() +{ + double smoothratio = I_GetTimeFrac(); + + if (runner) + { + twod->ClearScreen(); + IFVIRTUALPTRNAME(runner, NAME_ScreenJobRunner, RunFrame) + { + VMValue parm[] = { runner, smoothratio }; + VMCall(func, parm, 2, nullptr, 0); + } + } +} + +//============================================================================= +// +// +// +//============================================================================= + +bool ScreenJobValidate() +{ + if (runner) + { + IFVIRTUALPTRNAME(runner, NAME_ScreenJobRunner, Validate) + { + int res; + VMValue parm[] = { runner }; + VMReturn ret(&res); + VMCall(func, parm, 1, &ret, 1); + return res; + } + } + return false; +} + +//============================================================================= +// +// +// +//============================================================================= + +bool StartCutscene(CutsceneDef& cs, int flags, const CompletionFunc& completion_) +{ + if ((cs.function.IsNotEmpty() || cs.video.IsNotEmpty()) && cs.function.CompareNoCase("none") != 0) + { + completion = completion_; + runner = CreateRunner(); + GC::WriteBarrier(runner); + try + { + cs.Create(runner); + if (!ScreenJobValidate()) + { + runner->Destroy(); + runner = nullptr; + return false; + } + if (flags & SJ_DELAY) intermissiondelay = 10; // need to wait a bit at the start to let the timer catch up. + else intermissiondelay = 0; + gameaction = (flags & SJ_BLOCKUI) ? ga_intro : ga_intermission; + } + catch (...) + { + if (runner) runner->Destroy(); + runner = nullptr; + throw; + } + return true; + } + return false; +} + +bool StartCutscene(const char* s, int flags, const CompletionFunc& completion) +{ + CutsceneDef def; + def.function = s; + return StartCutscene(def, 0, completion); +} + +//============================================================================= +// +// +// +//============================================================================= + +CCMD(testcutscene) +{ + if (argv.argc() < 2) + { + Printf("Usage: testcutscene \n"); + return; + } + try + { + if (StartCutscene(argv[1], 0, [](bool) {})) + { + C_HideConsole(); + } + } + catch (const CRecoverableError& err) + { + Printf(TEXTCOLOR_RED "Unable to play cutscene: %s\n", err.what()); + } +} + + + diff --git a/src/common/cutscenes/screenjob.h b/src/common/cutscenes/screenjob.h new file mode 100644 index 0000000000..d843d50561 --- /dev/null +++ b/src/common/cutscenes/screenjob.h @@ -0,0 +1,43 @@ +#pragma once +#include +#include "dobject.h" +#include "v_2ddrawer.h" +#include "d_eventbase.h" +#include "s_soundinternal.h" +#include "gamestate.h" +#include "zstring.h" + +using CompletionFunc = std::function; + +void Job_Init(); + +enum +{ + SJ_BLOCKUI = 1, + SJ_DELAY = 2, +}; + +struct CutsceneDef +{ + FString video; + FString function; + FString soundName; + int soundID = -1; // ResID not SoundID! + int framespersec = 0; // only relevant for ANM. + bool transitiononly = false; // only play when transitioning between maps, but not when starting on a map or ending a game. + + void Create(DObject* runner); + bool isdefined() { return video.IsNotEmpty() || function.IsNotEmpty(); } + int GetSound(); +}; + +void EndScreenJob(); +void DeleteScreenJob(); +bool ScreenJobResponder(event_t* ev); +bool ScreenJobTick(); +void ScreenJobDraw(); + +struct CutsceneDef; +bool StartCutscene(const char* s, int flags, const CompletionFunc& completion); + +extern int intermissiondelay; diff --git a/src/common/thirdparty/libsmackerdec/COPYING b/src/common/thirdparty/libsmackerdec/COPYING new file mode 100644 index 0000000000..00b4fedfe7 --- /dev/null +++ b/src/common/thirdparty/libsmackerdec/COPYING @@ -0,0 +1,504 @@ + GNU LESSER GENERAL PUBLIC LICENSE + Version 2.1, February 1999 + + Copyright (C) 1991, 1999 Free Software Foundation, Inc. + 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + Everyone is permitted to copy and distribute verbatim copies + of this license document, but changing it is not allowed. + +[This is the first released version of the Lesser GPL. It also counts + as the successor of the GNU Library Public License, version 2, hence + the version number 2.1.] + + Preamble + + The licenses for most software are designed to take away your +freedom to share and change it. By contrast, the GNU General Public +Licenses are intended to guarantee your freedom to share and change +free software--to make sure the software is free for all its users. + + This license, the Lesser General Public License, applies to some +specially designated software packages--typically libraries--of the +Free Software Foundation and other authors who decide to use it. You +can use it too, but we suggest you first think carefully about whether +this license or the ordinary General Public License is the better +strategy to use in any particular case, based on the explanations below. + + When we speak of free software, we are referring to freedom of use, +not price. Our General Public Licenses are designed to make sure that +you have the freedom to distribute copies of free software (and charge +for this service if you wish); that you receive source code or can get +it if you want it; that you can change the software and use pieces of +it in new free programs; and that you are informed that you can do +these things. + + To protect your rights, we need to make restrictions that forbid +distributors to deny you these rights or to ask you to surrender these +rights. These restrictions translate to certain responsibilities for +you if you distribute copies of the library or if you modify it. + + For example, if you distribute copies of the library, whether gratis +or for a fee, you must give the recipients all the rights that we gave +you. You must make sure that they, too, receive or can get the source +code. If you link other code with the library, you must provide +complete object files to the recipients, so that they can relink them +with the library after making changes to the library and recompiling +it. And you must show them these terms so they know their rights. + + We protect your rights with a two-step method: (1) we copyright the +library, and (2) we offer you this license, which gives you legal +permission to copy, distribute and/or modify the library. + + To protect each distributor, we want to make it very clear that +there is no warranty for the free library. Also, if the library is +modified by someone else and passed on, the recipients should know +that what they have is not the original version, so that the original +author's reputation will not be affected by problems that might be +introduced by others. + + Finally, software patents pose a constant threat to the existence of +any free program. We wish to make sure that a company cannot +effectively restrict the users of a free program by obtaining a +restrictive license from a patent holder. Therefore, we insist that +any patent license obtained for a version of the library must be +consistent with the full freedom of use specified in this license. + + Most GNU software, including some libraries, is covered by the +ordinary GNU General Public License. This license, the GNU Lesser +General Public License, applies to certain designated libraries, and +is quite different from the ordinary General Public License. We use +this license for certain libraries in order to permit linking those +libraries into non-free programs. + + When a program is linked with a library, whether statically or using +a shared library, the combination of the two is legally speaking a +combined work, a derivative of the original library. The ordinary +General Public License therefore permits such linking only if the +entire combination fits its criteria of freedom. The Lesser General +Public License permits more lax criteria for linking other code with +the library. + + We call this license the "Lesser" General Public License because it +does Less to protect the user's freedom than the ordinary General +Public License. It also provides other free software developers Less +of an advantage over competing non-free programs. These disadvantages +are the reason we use the ordinary General Public License for many +libraries. However, the Lesser license provides advantages in certain +special circumstances. + + For example, on rare occasions, there may be a special need to +encourage the widest possible use of a certain library, so that it becomes +a de-facto standard. To achieve this, non-free programs must be +allowed to use the library. A more frequent case is that a free +library does the same job as widely used non-free libraries. In this +case, there is little to gain by limiting the free library to free +software only, so we use the Lesser General Public License. + + In other cases, permission to use a particular library in non-free +programs enables a greater number of people to use a large body of +free software. For example, permission to use the GNU C Library in +non-free programs enables many more people to use the whole GNU +operating system, as well as its variant, the GNU/Linux operating +system. + + Although the Lesser General Public License is Less protective of the +users' freedom, it does ensure that the user of a program that is +linked with the Library has the freedom and the wherewithal to run +that program using a modified version of the Library. + + The precise terms and conditions for copying, distribution and +modification follow. Pay close attention to the difference between a +"work based on the library" and a "work that uses the library". The +former contains code derived from the library, whereas the latter must +be combined with the library in order to run. + + GNU LESSER GENERAL PUBLIC LICENSE + TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION + + 0. This License Agreement applies to any software library or other +program which contains a notice placed by the copyright holder or +other authorized party saying it may be distributed under the terms of +this Lesser General Public License (also called "this License"). +Each licensee is addressed as "you". + + A "library" means a collection of software functions and/or data +prepared so as to be conveniently linked with application programs +(which use some of those functions and data) to form executables. + + The "Library", below, refers to any such software library or work +which has been distributed under these terms. A "work based on the +Library" means either the Library or any derivative work under +copyright law: that is to say, a work containing the Library or a +portion of it, either verbatim or with modifications and/or translated +straightforwardly into another language. (Hereinafter, translation is +included without limitation in the term "modification".) + + "Source code" for a work means the preferred form of the work for +making modifications to it. For a library, complete source code means +all the source code for all modules it contains, plus any associated +interface definition files, plus the scripts used to control compilation +and installation of the library. + + Activities other than copying, distribution and modification are not +covered by this License; they are outside its scope. The act of +running a program using the Library is not restricted, and output from +such a program is covered only if its contents constitute a work based +on the Library (independent of the use of the Library in a tool for +writing it). Whether that is true depends on what the Library does +and what the program that uses the Library does. + + 1. You may copy and distribute verbatim copies of the Library's +complete source code as you receive it, in any medium, provided that +you conspicuously and appropriately publish on each copy an +appropriate copyright notice and disclaimer of warranty; keep intact +all the notices that refer to this License and to the absence of any +warranty; and distribute a copy of this License along with the +Library. + + You may charge a fee for the physical act of transferring a copy, +and you may at your option offer warranty protection in exchange for a +fee. + + 2. You may modify your copy or copies of the Library or any portion +of it, thus forming a work based on the Library, and copy and +distribute such modifications or work under the terms of Section 1 +above, provided that you also meet all of these conditions: + + a) The modified work must itself be a software library. + + b) You must cause the files modified to carry prominent notices + stating that you changed the files and the date of any change. + + c) You must cause the whole of the work to be licensed at no + charge to all third parties under the terms of this License. + + d) If a facility in the modified Library refers to a function or a + table of data to be supplied by an application program that uses + the facility, other than as an argument passed when the facility + is invoked, then you must make a good faith effort to ensure that, + in the event an application does not supply such function or + table, the facility still operates, and performs whatever part of + its purpose remains meaningful. + + (For example, a function in a library to compute square roots has + a purpose that is entirely well-defined independent of the + application. Therefore, Subsection 2d requires that any + application-supplied function or table used by this function must + be optional: if the application does not supply it, the square + root function must still compute square roots.) + +These requirements apply to the modified work as a whole. If +identifiable sections of that work are not derived from the Library, +and can be reasonably considered independent and separate works in +themselves, then this License, and its terms, do not apply to those +sections when you distribute them as separate works. But when you +distribute the same sections as part of a whole which is a work based +on the Library, the distribution of the whole must be on the terms of +this License, whose permissions for other licensees extend to the +entire whole, and thus to each and every part regardless of who wrote +it. + +Thus, it is not the intent of this section to claim rights or contest +your rights to work written entirely by you; rather, the intent is to +exercise the right to control the distribution of derivative or +collective works based on the Library. + +In addition, mere aggregation of another work not based on the Library +with the Library (or with a work based on the Library) on a volume of +a storage or distribution medium does not bring the other work under +the scope of this License. + + 3. You may opt to apply the terms of the ordinary GNU General Public +License instead of this License to a given copy of the Library. To do +this, you must alter all the notices that refer to this License, so +that they refer to the ordinary GNU General Public License, version 2, +instead of to this License. (If a newer version than version 2 of the +ordinary GNU General Public License has appeared, then you can specify +that version instead if you wish.) Do not make any other change in +these notices. + + Once this change is made in a given copy, it is irreversible for +that copy, so the ordinary GNU General Public License applies to all +subsequent copies and derivative works made from that copy. + + This option is useful when you wish to copy part of the code of +the Library into a program that is not a library. + + 4. You may copy and distribute the Library (or a portion or +derivative of it, under Section 2) in object code or executable form +under the terms of Sections 1 and 2 above provided that you accompany +it with the complete corresponding machine-readable source code, which +must be distributed under the terms of Sections 1 and 2 above on a +medium customarily used for software interchange. + + If distribution of object code is made by offering access to copy +from a designated place, then offering equivalent access to copy the +source code from the same place satisfies the requirement to +distribute the source code, even though third parties are not +compelled to copy the source along with the object code. + + 5. A program that contains no derivative of any portion of the +Library, but is designed to work with the Library by being compiled or +linked with it, is called a "work that uses the Library". Such a +work, in isolation, is not a derivative work of the Library, and +therefore falls outside the scope of this License. + + However, linking a "work that uses the Library" with the Library +creates an executable that is a derivative of the Library (because it +contains portions of the Library), rather than a "work that uses the +library". The executable is therefore covered by this License. +Section 6 states terms for distribution of such executables. + + When a "work that uses the Library" uses material from a header file +that is part of the Library, the object code for the work may be a +derivative work of the Library even though the source code is not. +Whether this is true is especially significant if the work can be +linked without the Library, or if the work is itself a library. The +threshold for this to be true is not precisely defined by law. + + If such an object file uses only numerical parameters, data +structure layouts and accessors, and small macros and small inline +functions (ten lines or less in length), then the use of the object +file is unrestricted, regardless of whether it is legally a derivative +work. (Executables containing this object code plus portions of the +Library will still fall under Section 6.) + + Otherwise, if the work is a derivative of the Library, you may +distribute the object code for the work under the terms of Section 6. +Any executables containing that work also fall under Section 6, +whether or not they are linked directly with the Library itself. + + 6. As an exception to the Sections above, you may also combine or +link a "work that uses the Library" with the Library to produce a +work containing portions of the Library, and distribute that work +under terms of your choice, provided that the terms permit +modification of the work for the customer's own use and reverse +engineering for debugging such modifications. + + You must give prominent notice with each copy of the work that the +Library is used in it and that the Library and its use are covered by +this License. You must supply a copy of this License. If the work +during execution displays copyright notices, you must include the +copyright notice for the Library among them, as well as a reference +directing the user to the copy of this License. Also, you must do one +of these things: + + a) Accompany the work with the complete corresponding + machine-readable source code for the Library including whatever + changes were used in the work (which must be distributed under + Sections 1 and 2 above); and, if the work is an executable linked + with the Library, with the complete machine-readable "work that + uses the Library", as object code and/or source code, so that the + user can modify the Library and then relink to produce a modified + executable containing the modified Library. (It is understood + that the user who changes the contents of definitions files in the + Library will not necessarily be able to recompile the application + to use the modified definitions.) + + b) Use a suitable shared library mechanism for linking with the + Library. A suitable mechanism is one that (1) uses at run time a + copy of the library already present on the user's computer system, + rather than copying library functions into the executable, and (2) + will operate properly with a modified version of the library, if + the user installs one, as long as the modified version is + interface-compatible with the version that the work was made with. + + c) Accompany the work with a written offer, valid for at + least three years, to give the same user the materials + specified in Subsection 6a, above, for a charge no more + than the cost of performing this distribution. + + d) If distribution of the work is made by offering access to copy + from a designated place, offer equivalent access to copy the above + specified materials from the same place. + + e) Verify that the user has already received a copy of these + materials or that you have already sent this user a copy. + + For an executable, the required form of the "work that uses the +Library" must include any data and utility programs needed for +reproducing the executable from it. However, as a special exception, +the materials to be distributed need not include anything that is +normally distributed (in either source or binary form) with the major +components (compiler, kernel, and so on) of the operating system on +which the executable runs, unless that component itself accompanies +the executable. + + It may happen that this requirement contradicts the license +restrictions of other proprietary libraries that do not normally +accompany the operating system. Such a contradiction means you cannot +use both them and the Library together in an executable that you +distribute. + + 7. You may place library facilities that are a work based on the +Library side-by-side in a single library together with other library +facilities not covered by this License, and distribute such a combined +library, provided that the separate distribution of the work based on +the Library and of the other library facilities is otherwise +permitted, and provided that you do these two things: + + a) Accompany the combined library with a copy of the same work + based on the Library, uncombined with any other library + facilities. This must be distributed under the terms of the + Sections above. + + b) Give prominent notice with the combined library of the fact + that part of it is a work based on the Library, and explaining + where to find the accompanying uncombined form of the same work. + + 8. You may not copy, modify, sublicense, link with, or distribute +the Library except as expressly provided under this License. Any +attempt otherwise to copy, modify, sublicense, link with, or +distribute the Library is void, and will automatically terminate your +rights under this License. However, parties who have received copies, +or rights, from you under this License will not have their licenses +terminated so long as such parties remain in full compliance. + + 9. You are not required to accept this License, since you have not +signed it. However, nothing else grants you permission to modify or +distribute the Library or its derivative works. These actions are +prohibited by law if you do not accept this License. Therefore, by +modifying or distributing the Library (or any work based on the +Library), you indicate your acceptance of this License to do so, and +all its terms and conditions for copying, distributing or modifying +the Library or works based on it. + + 10. Each time you redistribute the Library (or any work based on the +Library), the recipient automatically receives a license from the +original licensor to copy, distribute, link with or modify the Library +subject to these terms and conditions. You may not impose any further +restrictions on the recipients' exercise of the rights granted herein. +You are not responsible for enforcing compliance by third parties with +this License. + + 11. If, as a consequence of a court judgment or allegation of patent +infringement or for any other reason (not limited to patent issues), +conditions are imposed on you (whether by court order, agreement or +otherwise) that contradict the conditions of this License, they do not +excuse you from the conditions of this License. If you cannot +distribute so as to satisfy simultaneously your obligations under this +License and any other pertinent obligations, then as a consequence you +may not distribute the Library at all. For example, if a patent +license would not permit royalty-free redistribution of the Library by +all those who receive copies directly or indirectly through you, then +the only way you could satisfy both it and this License would be to +refrain entirely from distribution of the Library. + +If any portion of this section is held invalid or unenforceable under any +particular circumstance, the balance of the section is intended to apply, +and the section as a whole is intended to apply in other circumstances. + +It is not the purpose of this section to induce you to infringe any +patents or other property right claims or to contest validity of any +such claims; this section has the sole purpose of protecting the +integrity of the free software distribution system which is +implemented by public license practices. Many people have made +generous contributions to the wide range of software distributed +through that system in reliance on consistent application of that +system; it is up to the author/donor to decide if he or she is willing +to distribute software through any other system and a licensee cannot +impose that choice. + +This section is intended to make thoroughly clear what is believed to +be a consequence of the rest of this License. + + 12. If the distribution and/or use of the Library is restricted in +certain countries either by patents or by copyrighted interfaces, the +original copyright holder who places the Library under this License may add +an explicit geographical distribution limitation excluding those countries, +so that distribution is permitted only in or among countries not thus +excluded. In such case, this License incorporates the limitation as if +written in the body of this License. + + 13. The Free Software Foundation may publish revised and/or new +versions of the Lesser General Public License from time to time. +Such new versions will be similar in spirit to the present version, +but may differ in detail to address new problems or concerns. + +Each version is given a distinguishing version number. If the Library +specifies a version number of this License which applies to it and +"any later version", you have the option of following the terms and +conditions either of that version or of any later version published by +the Free Software Foundation. If the Library does not specify a +license version number, you may choose any version ever published by +the Free Software Foundation. + + 14. If you wish to incorporate parts of the Library into other free +programs whose distribution conditions are incompatible with these, +write to the author to ask for permission. For software which is +copyrighted by the Free Software Foundation, write to the Free +Software Foundation; we sometimes make exceptions for this. Our +decision will be guided by the two goals of preserving the free status +of all derivatives of our free software and of promoting the sharing +and reuse of software generally. + + NO WARRANTY + + 15. BECAUSE THE LIBRARY IS LICENSED FREE OF CHARGE, THERE IS NO +WARRANTY FOR THE LIBRARY, TO THE EXTENT PERMITTED BY APPLICABLE LAW. +EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR +OTHER PARTIES PROVIDE THE LIBRARY "AS IS" WITHOUT WARRANTY OF ANY +KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE +IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR +PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE +LIBRARY IS WITH YOU. SHOULD THE LIBRARY PROVE DEFECTIVE, YOU ASSUME +THE COST OF ALL NECESSARY SERVICING, REPAIR OR CORRECTION. + + 16. IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN +WRITING WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY +AND/OR REDISTRIBUTE THE LIBRARY AS PERMITTED ABOVE, BE LIABLE TO YOU +FOR DAMAGES, INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL OR +CONSEQUENTIAL DAMAGES ARISING OUT OF THE USE OR INABILITY TO USE THE +LIBRARY (INCLUDING BUT NOT LIMITED TO LOSS OF DATA OR DATA BEING +RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD PARTIES OR A +FAILURE OF THE LIBRARY TO OPERATE WITH ANY OTHER SOFTWARE), EVEN IF +SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH +DAMAGES. + + END OF TERMS AND CONDITIONS + + How to Apply These Terms to Your New Libraries + + If you develop a new library, and you want it to be of the greatest +possible use to the public, we recommend making it free software that +everyone can redistribute and change. You can do so by permitting +redistribution under these terms (or, alternatively, under the terms of the +ordinary General Public License). + + To apply these terms, attach the following notices to the library. It is +safest to attach them to the start of each source file to most effectively +convey the exclusion of warranty; and each file should have at least the +"copyright" line and a pointer to where the full notice is found. + + + Copyright (C) + + This library is free software; you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public + License as published by the Free Software Foundation; either + version 2.1 of the License, or (at your option) any later version. + + This library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public + License along with this library; if not, write to the Free Software + Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + +Also add information on how to contact you by electronic and paper mail. + +You should also get your employer (if you work as a programmer) or your +school, if any, to sign a "copyright disclaimer" for the library, if +necessary. Here is a sample; alter the names: + + Yoyodyne, Inc., hereby disclaims all copyright interest in the + library `Frob' (a library for tweaking knobs) written by James Random Hacker. + + , 1 April 1990 + Ty Coon, President of Vice + +That's all there is to it! + + diff --git a/src/common/thirdparty/libsmackerdec/include/BitReader.h b/src/common/thirdparty/libsmackerdec/include/BitReader.h new file mode 100644 index 0000000000..4863e42a85 --- /dev/null +++ b/src/common/thirdparty/libsmackerdec/include/BitReader.h @@ -0,0 +1,55 @@ +/* + * libsmackerdec - Smacker video decoder + * Copyright (C) 2011 Barry Duncan + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + */ + +#ifndef _SmackerBitReader_h_ +#define _SmackerBitReader_h_ + +#include +#include "FileStream.h" +#include "tarray.h" + +namespace SmackerCommon { + +class BitReader +{ + public: + BitReader(SmackerCommon::FileStream &file, uint32_t size); + ~BitReader(); + uint32_t GetBit(); + uint32_t GetBits(uint32_t n); + void SkipBits(uint32_t n); + + uint32_t GetSize(); + uint32_t GetPosition(); + + private: + uint32_t totalSize; + uint32_t currentOffset; + uint32_t bytesRead; + + SmackerCommon::FileStream *file; + + TArray Cache; + + void FillCache(); +}; + +} // close namespace SmackerCommon + +#endif \ No newline at end of file diff --git a/src/common/thirdparty/libsmackerdec/include/FileStream.h b/src/common/thirdparty/libsmackerdec/include/FileStream.h new file mode 100644 index 0000000000..034ccb7336 --- /dev/null +++ b/src/common/thirdparty/libsmackerdec/include/FileStream.h @@ -0,0 +1,63 @@ +/* + * libsmackerdec - Smacker video decoder + * Copyright (C) 2011 Barry Duncan + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + */ + +#ifndef _SmackerFileStream_h_ +#define _SmackerFileStream_h_ + +#include +#include "files.h" + +namespace SmackerCommon { + +class FileStream +{ + public: + + bool Open(const char *fileName); + bool Is_Open() { return file.isOpen(); } + void Close() { file.Close(); } + + int32_t ReadBytes(uint8_t *data, uint32_t nBytes) + { + return (uint32_t)file.Read(data, static_cast(nBytes)); + } + + uint64_t ReadUint64LE() { return file.ReadUInt64(); } + uint32_t ReadUint32LE() { return file.ReadUInt32(); } + uint16_t ReadUint16LE() { return file.ReadUInt16(); } + uint8_t ReadByte() { return file.ReadInt8(); } + + + enum SeekDirection{ + kSeekCurrent = SEEK_CUR, + kSeekStart = SEEK_SET, + kSeekEnd = SEEK_END + }; + + int32_t Seek(int32_t offset, SeekDirection dir = kSeekStart) { return (int)file.Seek(offset, (FileReader::ESeek) dir); } + int32_t Skip(int32_t offset) { return (int)Seek(offset, kSeekCurrent); } + int32_t GetPosition() { return (int)file.Tell(); } + + private: + FileReader file; +}; + +} // close namespace SmackerCommon + +#endif diff --git a/src/common/thirdparty/libsmackerdec/include/HuffmanVLC.h b/src/common/thirdparty/libsmackerdec/include/HuffmanVLC.h new file mode 100644 index 0000000000..668249317f --- /dev/null +++ b/src/common/thirdparty/libsmackerdec/include/HuffmanVLC.h @@ -0,0 +1,43 @@ +/* + * libsmackerdec - Smacker video decoder + * Copyright (C) 2011 Barry Duncan + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + */ + +#ifndef _SmackerHuffmanVLC_h_ +#define _SmackerHuffmanVLC_h_ + +#include +#include "BitReader.h" +#include + +namespace SmackerCommon { + +struct VLC +{ + uint32_t symbol; + uint32_t code; +}; + +typedef std::vector< std::vector > VLCtable; + +uint16_t VLC_GetCodeBits(BitReader &bits, VLCtable &table); +void VLC_InitTable (VLCtable &table, uint32_t maxLength, uint32_t size, int *lengths, uint32_t *bits); +uint32_t VLC_GetSize (VLCtable &table); + +} // close namespace SmackerCommon + +#endif diff --git a/src/common/thirdparty/libsmackerdec/include/LogError.h b/src/common/thirdparty/libsmackerdec/include/LogError.h new file mode 100644 index 0000000000..b5b8b5f36c --- /dev/null +++ b/src/common/thirdparty/libsmackerdec/include/LogError.h @@ -0,0 +1,31 @@ +/* + * libsmackerdec - Smacker video decoder + * Copyright (C) 2011 Barry Duncan + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + */ + +#ifndef _SmackerLogError_h_ +#define _SmackerLogError_h_ + +//#include + +namespace SmackerCommon { + +//void LogError(const std::string &error); + +} // close namespace SmackerCommon + +#endif \ No newline at end of file diff --git a/src/common/thirdparty/libsmackerdec/include/SmackerDecoder.h b/src/common/thirdparty/libsmackerdec/include/SmackerDecoder.h new file mode 100644 index 0000000000..f35fdc0fc3 --- /dev/null +++ b/src/common/thirdparty/libsmackerdec/include/SmackerDecoder.h @@ -0,0 +1,168 @@ +/* + * libsmackerdec - Smacker video decoder + * Copyright (C) 2011 Barry Duncan + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + */ + +/* This code is based on smacker.c from the FFmpeg project which can be obtained from http://www.ffmpeg.org/ + * below is the license from smacker.c + * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */ + +/* + * Smacker decoder + * Copyright (c) 2006 Konstantin Shishkov + * + * This file is part of FFmpeg. + * + * FFmpeg is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * FFmpeg is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with FFmpeg; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + */ + +#ifndef _SmackerDecoder_h_ +#define _SmackerDecoder_h_ + +#include +#include "FileStream.h" +#include "BitReader.h" +#include + +// exportable interface +struct SmackerHandle +{ + bool isValid; + int instanceIndex; +}; + +struct SmackerAudioInfo +{ + uint32_t sampleRate; + uint8_t nChannels; + uint8_t bitsPerSample; + + uint32_t idealBufferSize; +}; + +SmackerHandle Smacker_Open (const char *fileName); +void Smacker_Close (SmackerHandle &handle); +uint32_t Smacker_GetNumAudioTracks (SmackerHandle &handle); +SmackerAudioInfo Smacker_GetAudioTrackDetails (SmackerHandle &handle, uint32_t trackIndex); +uint32_t Smacker_GetAudioData (SmackerHandle &handle, uint32_t trackIndex, int16_t *data); +uint32_t Smacker_GetNumFrames (SmackerHandle &handle); +void Smacker_GetFrameSize (SmackerHandle &handle, uint32_t &width, uint32_t &height); +uint32_t Smacker_GetCurrentFrameNum (SmackerHandle &handle); +uint32_t Smacker_GetNextFrame (SmackerHandle &handle); +float Smacker_GetFrameRate (SmackerHandle &handle); +void Smacker_GetPalette (SmackerHandle &handle, uint8_t *palette); +void Smacker_GetFrame (SmackerHandle &handle, uint8_t *frame); +void Smacker_GotoFrame (SmackerHandle &handle, uint32_t frameNum); + +const int kMaxAudioTracks = 7; + +// forward declare +struct HuffContext; +struct DBCtx; + +struct SmackerAudioTrack +{ + uint32_t sizeInBytes; + uint32_t flags; + uint32_t sampleRate; + uint8_t nChannels; + uint8_t bitsPerSample; +// int compressionType; + + uint8_t *buffer; + uint32_t bufferSize; + + uint32_t bytesReadThisFrame; +}; + +class SmackerDecoder +{ + public: + uint32_t frameWidth; + uint32_t frameHeight; + + SmackerDecoder(); + ~SmackerDecoder(); + + bool Open(const char *fileName); + void GetPalette(uint8_t *palette); + void GetFrame(uint8_t *frame); + + SmackerAudioInfo GetAudioTrackDetails(uint32_t trackIndex); + uint32_t GetAudioData(uint32_t trackIndex, int16_t *audioBuffer); + uint32_t GetNumFrames(); + uint32_t GetCurrentFrameNum(); + float GetFrameRate(); + void GetNextFrame(); + void GotoFrame(uint32_t frameNum); + + private: + SmackerCommon::FileStream file; + char signature[4]; + + // video related members + uint32_t nFrames; + uint32_t fps; // frames per second + + uint8_t palette[768]; + uint8_t *picture; + + bool isVer4; + + SmackerAudioTrack audioTracks[kMaxAudioTracks]; + + uint32_t treeSize; + uint32_t mMapSize, MClrSize, fullSize, typeSize; + + std::vector mmap_tbl; + std::vector mclr_tbl; + std::vector full_tbl; + std::vector type_tbl; + + int mmap_last[3], mclr_last[3], full_last[3], type_last[3]; + + std::vector frameSizes; + std::vector frameFlags; + + uint32_t currentFrame; + int32_t nextPos; + int32_t firstFrameFilePos; + + bool DecodeHeaderTrees(); + int DecodeHeaderTree(SmackerCommon::BitReader &bits, std::vector &recodes, int *last, int size); + int DecodeTree(SmackerCommon::BitReader &bits, HuffContext *hc, uint32_t prefix, int length); + int DecodeBigTree(SmackerCommon::BitReader &bits, HuffContext *hc, DBCtx *ctx); + int GetCode(SmackerCommon::BitReader &bits, std::vector &recode, int *last); + int ReadPacket(); + int DecodeFrame(uint32_t frameSize); + void GetFrameSize(uint32_t &width, uint32_t &height); + int DecodeAudio(uint32_t size, SmackerAudioTrack &track); +}; + +#endif diff --git a/src/common/thirdparty/libsmackerdec/src/BitReader.cpp b/src/common/thirdparty/libsmackerdec/src/BitReader.cpp new file mode 100644 index 0000000000..ea5169d3f1 --- /dev/null +++ b/src/common/thirdparty/libsmackerdec/src/BitReader.cpp @@ -0,0 +1,90 @@ +/* + * libsmackerdec - Smacker video decoder + * Copyright (C) 2011 Barry Duncan + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + */ + +#include "BitReader.h" +#include + +namespace SmackerCommon { + +BitReader::BitReader(SmackerCommon::FileStream &file, uint32_t size) +{ + this->file = &file; + this->totalSize = size; + this->currentOffset = 0; + this->bytesRead = 0; + + this->Cache.Resize(size); + file.ReadBytes(this->Cache.Data(), size); +} + +BitReader::~BitReader() +{ +} + +void BitReader::FillCache() +{ +} + +uint32_t BitReader::GetSize() +{ + return totalSize * 8; +} + +uint32_t BitReader::GetPosition() +{ + return currentOffset; +} + +uint32_t BitReader::GetBit() +{ + uint32_t ret = (Cache[currentOffset>>3]>>(currentOffset&7))&1; + currentOffset++; + return ret; +} + +uint32_t BitReader::GetBits(uint32_t n) +{ + uint32_t ret = 0; + + int bitsTodo = n; + + uint32_t theShift = 0; + + while (bitsTodo) + { + uint32_t bit = GetBit(); + bit <<= theShift; + + theShift++; + + ret |= bit; + + bitsTodo--; + } + + return ret; +} + +void BitReader::SkipBits(uint32_t n) +{ + GetBits(n); +} + +} // close namespace SmackerCommon + diff --git a/src/common/thirdparty/libsmackerdec/src/FileStream.cpp b/src/common/thirdparty/libsmackerdec/src/FileStream.cpp new file mode 100644 index 0000000000..454143ff10 --- /dev/null +++ b/src/common/thirdparty/libsmackerdec/src/FileStream.cpp @@ -0,0 +1,40 @@ +/* + * libsmackerdec - Smacker video decoder + * Copyright (C) 2011 Barry Duncan + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + */ + +#include "FileStream.h" +#include "filesystem.h" +#include +#include "cmdlib.h" + +namespace SmackerCommon { + +bool FileStream::Open(const char *fileName) +{ + FString fixedname = fileName; + FixPathSeperator(fixedname); + file = fileSystem.OpenFileReader(fixedname); + if (!file.isOpen()) + { + // log error + return false; + } + return true; +} + +} // close namespace SmackerCommon diff --git a/src/common/thirdparty/libsmackerdec/src/HuffmanVLC.cpp b/src/common/thirdparty/libsmackerdec/src/HuffmanVLC.cpp new file mode 100644 index 0000000000..400e622f8f --- /dev/null +++ b/src/common/thirdparty/libsmackerdec/src/HuffmanVLC.cpp @@ -0,0 +1,71 @@ +/* + * libsmackerdec - Smacker video decoder + * Copyright (C) 2011 Barry Duncan + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + */ + +#include + +namespace SmackerCommon { + +uint16_t VLC_GetCodeBits(BitReader &bits, VLCtable &table) +{ + uint32_t codeBits = 0; + + // search each length array + for (uint32_t i = 0; i < table.size(); i++) + { + // get and add a new bit to codeBits + uint32_t theBit = bits.GetBit() << i; + codeBits |= theBit; + + // search for a code match + for (uint32_t j = 0; j < table[i].size(); j++) + { + if (codeBits == table[i][j].code) + { + return table[i][j].symbol; + } + } + } + + // shouldn't get here.. + return 0; +} + +void VLC_InitTable(VLCtable &table, uint32_t maxLength, uint32_t size, int *lengths, uint32_t *bits) +{ + table.resize(maxLength); + + for (uint32_t i = 0; i < size; i++) + { + VLC newCode; + newCode.symbol = i; + newCode.code = bits[i]; + + uint32_t codeLength = lengths[i]; + + if (codeLength) + table[codeLength - 1].push_back(newCode); + } +} + +uint32_t VLC_GetSize(VLCtable &table) +{ + return uint32_t(table.size()); +} + +} // close namespace SmackerCommon diff --git a/src/common/thirdparty/libsmackerdec/src/LogError.cpp b/src/common/thirdparty/libsmackerdec/src/LogError.cpp new file mode 100644 index 0000000000..46a45bb0a6 --- /dev/null +++ b/src/common/thirdparty/libsmackerdec/src/LogError.cpp @@ -0,0 +1,33 @@ +/* + * libsmackerdec - Smacker video decoder + * Copyright (C) 2011 Barry Duncan + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + */ + +#include "LogError.h" + +namespace SmackerCommon { + +#if 0 +static std::string LastError; + +void LogError(const std::string &error) +{ + LastError = error; +} +#endif + +} // close namespace SmackerCommon diff --git a/src/common/thirdparty/libsmackerdec/src/SmackerDecoder.cpp b/src/common/thirdparty/libsmackerdec/src/SmackerDecoder.cpp new file mode 100644 index 0000000000..c981d35a68 --- /dev/null +++ b/src/common/thirdparty/libsmackerdec/src/SmackerDecoder.cpp @@ -0,0 +1,1151 @@ +/* + * libsmackerdec - Smacker video decoder + * Copyright (C) 2011 Barry Duncan + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + */ + +/* This code is heavily based on smacker.c from the FFmpeg project which can be obtained from http://www.ffmpeg.org/ + * below is the license from smacker.c + * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */ + +/* + * Smacker decoder + * Copyright (c) 2006 Konstantin Shishkov + * + * This file is part of FFmpeg. + * + * FFmpeg is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * FFmpeg is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with FFmpeg; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + */ + +#include "SmackerDecoder.h" +#include "HuffmanVLC.h" +#include "LogError.h" +#include "printf.h" +#include "limits.h" +#include +#include + +std::vector classInstances; + +SmackerHandle Smacker_Open(const char* fileName) +{ + SmackerHandle newHandle; + newHandle.isValid = false; + newHandle.instanceIndex = -1; + + SmackerDecoder *newDecoder = new SmackerDecoder(); + if (!newDecoder->Open(fileName)) + { + delete newDecoder; + return newHandle; + } + + // add instance to global instance vector + classInstances.push_back(newDecoder); + + // get a handle ID + newHandle.instanceIndex = int(classInstances.size()) - 1; + + // loaded ok, make handle valid + newHandle.isValid = true; + + return newHandle; +} + +void Smacker_Close(SmackerHandle &handle) +{ + if (!classInstances.at(handle.instanceIndex)) + { + // invalid handle + return; + } + + // close bink decoder + delete classInstances[handle.instanceIndex]; + classInstances[handle.instanceIndex] = 0; + + handle.instanceIndex = -1; + handle.isValid = false; +} + +uint32_t Smacker_GetNumAudioTracks(SmackerHandle &) +{ + // TODO: fixme + return 1; +} + +SmackerAudioInfo Smacker_GetAudioTrackDetails(SmackerHandle &handle, uint32_t trackIndex) +{ + return classInstances[handle.instanceIndex]->GetAudioTrackDetails(trackIndex); +} + +/* Get a frame's worth of audio data. + * + * 'data' needs to be a pointer to allocated memory that this function will fill. + * You can find the size (in bytes) to make this buffer by calling Bink_GetAudioTrackDetails() + * and checking the 'idealBufferSize' member in the returned AudioInfo struct + */ +uint32_t Smacker_GetAudioData(SmackerHandle &handle, uint32_t trackIndex, int16_t *data) +{ + return classInstances[handle.instanceIndex]->GetAudioData(trackIndex, data); +} + +uint32_t Smacker_GetNumFrames(SmackerHandle &handle) +{ + return classInstances[handle.instanceIndex]->GetNumFrames(); +} + +void Smacker_GetFrameSize(SmackerHandle &handle, uint32_t &width, uint32_t &height) +{ + width = classInstances[handle.instanceIndex]->frameWidth; + height = classInstances[handle.instanceIndex]->frameHeight; +} + +uint32_t Smacker_GetCurrentFrameNum(SmackerHandle &handle) +{ + return classInstances[handle.instanceIndex]->GetCurrentFrameNum(); +} + +uint32_t Smacker_GetNextFrame(SmackerHandle &handle) +{ + SmackerDecoder *decoder = classInstances[handle.instanceIndex]; + + uint32_t frameIndex = decoder->GetCurrentFrameNum(); + + decoder->GetNextFrame(); + + return frameIndex; +} + +float Smacker_GetFrameRate(SmackerHandle &handle) +{ + return classInstances[handle.instanceIndex]->GetFrameRate(); +} + +void Smacker_GetPalette(SmackerHandle &handle, uint8_t *palette) +{ + classInstances[handle.instanceIndex]->GetPalette(palette); +} + +void Smacker_GetFrame(SmackerHandle &handle, uint8_t *frame) +{ + classInstances[handle.instanceIndex]->GetFrame(frame); +} + +void Smacker_GotoFrame(SmackerHandle &handle, uint32_t frameNum) +{ + classInstances[handle.instanceIndex]->GotoFrame(frameNum); +} + +SmackerDecoder::SmackerDecoder() +{ + isVer4 = false; + currentFrame = 0; + picture = 0; + nextPos = 0; + + for (int i = 0; i < kMaxAudioTracks; i++) + { + audioTracks[i].buffer = 0; + } +} + +SmackerDecoder::~SmackerDecoder() +{ + for (int i = 0; i < kMaxAudioTracks; i++) + { + delete[] audioTracks[i].buffer; + } + + delete[] picture; +} + +// from bswap.h +static /*av_always_inline av_const*/ uint16_t av_bswap16(uint16_t x) +{ + x= (x>>8) | (x<<8); + return x; +} + +/* possible runs of blocks */ +static const int block_runs[64] = { + 1, 2, 3, 4, 5, 6, 7, 8, + 9, 10, 11, 12, 13, 14, 15, 16, + 17, 18, 19, 20, 21, 22, 23, 24, + 25, 26, 27, 28, 29, 30, 31, 32, + 33, 34, 35, 36, 37, 38, 39, 40, + 41, 42, 43, 44, 45, 46, 47, 48, + 49, 50, 51, 52, 53, 54, 55, 56, + 57, 58, 59, 128, 256, 512, 1024, 2048 }; + +enum SmkBlockTypes { + SMK_BLK_MONO = 0, + SMK_BLK_FULL = 1, + SMK_BLK_SKIP = 2, + SMK_BLK_FILL = 3 }; + +/* palette used in Smacker */ +static const uint8_t smk_pal[64] = { + 0x00, 0x04, 0x08, 0x0C, 0x10, 0x14, 0x18, 0x1C, + 0x20, 0x24, 0x28, 0x2C, 0x30, 0x34, 0x38, 0x3C, + 0x41, 0x45, 0x49, 0x4D, 0x51, 0x55, 0x59, 0x5D, + 0x61, 0x65, 0x69, 0x6D, 0x71, 0x75, 0x79, 0x7D, + 0x82, 0x86, 0x8A, 0x8E, 0x92, 0x96, 0x9A, 0x9E, + 0xA2, 0xA6, 0xAA, 0xAE, 0xB2, 0xB6, 0xBA, 0xBE, + 0xC3, 0xC7, 0xCB, 0xCF, 0xD3, 0xD7, 0xDB, 0xDF, + 0xE3, 0xE7, 0xEB, 0xEF, 0xF3, 0xF7, 0xFB, 0xFF +}; + +enum SAudFlags { + SMK_AUD_PACKED = 0x80000000, + SMK_AUD_16BITS = 0x20000000, + SMK_AUD_STEREO = 0x10000000, + SMK_AUD_BINKAUD = 0x08000000, + SMK_AUD_USEDCT = 0x04000000 +}; + +const int kSMKpal = 0x01; +const int kFlagRingFrame = 0x01; + +const int kTreeBits = 9; +const int kSMKnode = 0x80000000; + +const char *kSMK2iD = "SMK2"; +const char *kSMK4iD = "SMK4"; + + +/** + * Context used for code reconstructing + */ +typedef struct HuffContext { + int length; + int maxlength; + int current; + + std::vector bits; + std::vector lengths; + std::vector values; + +} HuffContext; + +/* common parameters used for decode_bigtree */ +typedef struct DBCtx { + SmackerCommon::VLCtable v1; + SmackerCommon::VLCtable v2; + std::vector recode1, recode2; + int escapes[3]; + int *last; + int lcur; +} DBCtx; + + +static void last_reset(std::vector &recode, int *last) { + recode[last[0]] = recode[last[1]] = recode[last[2]] = 0; +} + +/* get code and update history */ +int SmackerDecoder::GetCode(SmackerCommon::BitReader &bits, std::vector &recode, int *last) +{ + int *table = &recode[0]; + + int v, b; + + b = bits.GetPosition(); + + while (*table & kSMKnode) + { + if (bits.GetBit()) + table += (*table) & (~kSMKnode); + table++; + } + v = *table; + b = bits.GetPosition() - b; + + if (v != recode[last[0]]) { + recode[last[2]] = recode[last[1]]; + recode[last[1]] = recode[last[0]]; + recode[last[0]] = v; + } + return v; +} + +bool SmackerDecoder::Open(const char *fileName) +{ + // open the file (read only) + file.Open(fileName); + if (!file.Is_Open()) + { + Printf("SmackerDecoder::Open() - Can't open file %s\n", fileName); + return false; + } + + // check the file signature + file.ReadBytes((uint8_t*)signature, 4); + if (memcmp(signature, kSMK2iD, 4) != 0 + && memcmp(signature, kSMK4iD, 4) != 0) + { + Printf("SmackerDecoder::Open() - Unknown Smacker signature\n"); + return false; + } + + if (!memcmp(signature, kSMK4iD, 4)) { + isVer4 = true; + } + + frameWidth = file.ReadUint32LE(); + frameHeight = file.ReadUint32LE(); + nFrames = file.ReadUint32LE(); + + picture = new uint8_t[frameWidth * frameHeight]; + + int32_t frameRate = file.ReadUint32LE(); + + if (frameRate > 0) + fps = 1000 / frameRate; + else if (frameRate < 0) + fps = 100000 / (-frameRate); + else + fps = 10; + + uint32_t flags = file.ReadUint32LE(); + + if (flags & kFlagRingFrame) { + nFrames++; + } + + for (int i = 0; i < kMaxAudioTracks; i++) { + audioTracks[i].sizeInBytes = file.ReadUint32LE(); + } + + treeSize = file.ReadUint32LE(); + mMapSize = file.ReadUint32LE(); + MClrSize = file.ReadUint32LE(); + fullSize = file.ReadUint32LE(); + typeSize = file.ReadUint32LE(); + + for (int i = 0; i < kMaxAudioTracks; i++) { + audioTracks[i].flags = file.ReadUint32LE(); + } + + // skip pad + file.Skip(4); + + if (nFrames > 0xFFFFFF) + { + Printf("SmackerDecoder::Open() - Too many frames\n"); + return false; + } + + frameSizes.resize(nFrames); + frameFlags.resize(nFrames); + + // read frame info + for (uint32_t i = 0; i < nFrames; i++) { + frameSizes[i] = file.ReadUint32LE(); + } + for (uint32_t i = 0; i < nFrames; i++) { + frameFlags[i] = file.ReadByte(); + } + + // handle possible audio streams + for (int i = 0; i < kMaxAudioTracks; i++) + { + audioTracks[i].buffer = 0; + audioTracks[i].bufferSize = 0; + audioTracks[i].bytesReadThisFrame = 0; + + if (audioTracks[i].flags & 0xFFFFFF) + { +/* + if (audioTracks[i].flags & SMK_AUD_BINKAUD) { + audioTracks[i].compressionType = SMK_AUD_BINKAUD; + } else if (audioTracks[i].flags & SMK_AUD_USEDCT) { + audioTracks[i].compressionType = SMK_AUD_USEDCT; + } else if (audioTracks[i].flags & SMK_AUD_PACKED){ + ast[i]->codec->codec_id = CODEC_ID_SMACKAUDIO; + ast[i]->codec->codec_tag = MKTAG('S', 'M', 'K', 'A'); + } else { + ast[i]->codec->codec_id = CODEC_ID_PCM_U8; + } +*/ + audioTracks[i].nChannels = (audioTracks[i].flags & SMK_AUD_STEREO) ? 2 : 1; + audioTracks[i].sampleRate = audioTracks[i].flags & 0xFFFFFF; + audioTracks[i].bitsPerSample = (audioTracks[i].flags & SMK_AUD_16BITS) ? 16 : 8; + } + } + + memset(palette, 0, 768); + + DecodeHeaderTrees(); + + // set nextPos to where we are now, as next data is frame 1 + nextPos = file.GetPosition(); + firstFrameFilePos = nextPos; + + // determine max buffer sizes for audio tracks +// file.Seek(nextPos, SmackerCommon::FileStream::kSeekStart); + + uint8_t frameFlag = frameFlags[0]; + + // skip over palette + if (frameFlag & kSMKpal) + { + uint32_t size = file.ReadByte(); + size = size * 4 - 1; + file.Skip(size); + } + + frameFlag >>= 1; + + for (int i = 0; i < kMaxAudioTracks; i++) + { + if (frameFlag & 1) + { + // skip size + file.Skip(4); + + uint32_t unpackedSize = file.ReadUint32LE(); + + audioTracks[i].bufferSize = unpackedSize; + audioTracks[i].buffer = new uint8_t[unpackedSize]; + } + frameFlag >>= 1; + } + + return true; +} + +// static int smacker_decode_tree(GetBitContext *gb, HuffContext *hc, uint32_t prefix, int length) +int SmackerDecoder::DecodeTree(SmackerCommon::BitReader &bits, HuffContext *hc, uint32_t prefix, int length) +{ + if (!bits.GetBit()) // Leaf + { + if (hc->current >= 256){ + Printf("SmackerDecoder::DecodeTree() - Tree size exceeded\n"); + return -1; + } + if (length){ + hc->bits[hc->current] = prefix; + hc->lengths[hc->current] = length; + } else { + hc->bits[hc->current] = 0; + hc->lengths[hc->current] = 0; + } + hc->values[hc->current] = bits.GetBits(8); + + hc->current++; + if (hc->maxlength < length) + hc->maxlength = length; + return 0; + } else { //Node + int r; + length++; + r = DecodeTree(bits, hc, prefix, length); + if (r) + return r; + return DecodeTree(bits, hc, prefix | (1 << (length - 1)), length); + } +} + +/** + * Decode header tree + */ +int SmackerDecoder::DecodeBigTree(SmackerCommon::BitReader &bits, HuffContext *hc, DBCtx *ctx) +{ + if (!bits.GetBit()) // Leaf + { + int val, i1, i2, b1, b2; + + i1 = 0; + i2 = 0; + + if (hc->current >= hc->length){ + Printf("SmackerDecoder::DecodeBigTree() - Tree size exceeded"); + return -1; + } + + b1 = bits.GetPosition(); + + if (VLC_GetSize(ctx->v1)) + { + i1 = VLC_GetCodeBits(bits, ctx->v1); + } + + b1 = bits.GetPosition() - b1; + b2 = bits.GetPosition(); + + if (VLC_GetSize(ctx->v2)) + { + i2 = VLC_GetCodeBits(bits, ctx->v2); + } + + b2 = bits.GetPosition() - b2; + if (i1 < 0 || i2 < 0) + return -1; + val = ctx->recode1[i1] | (ctx->recode2[i2] << 8); + if (val == ctx->escapes[0]) { + ctx->last[0] = hc->current; + val = 0; + } else if (val == ctx->escapes[1]) { + ctx->last[1] = hc->current; + val = 0; + } else if (val == ctx->escapes[2]) { + ctx->last[2] = hc->current; + val = 0; + } + + hc->values[hc->current++] = val; + return 1; + } else { //Node + int r = 0, t; + + t = hc->current++; + r = DecodeBigTree(bits, hc, ctx); + if (r < 0) + return r; + hc->values[t] = kSMKnode | r; + r++; + r += DecodeBigTree(bits, hc, ctx); + return r; + } +} + +/** + * Store large tree as Libav's vlc codes + */ +int SmackerDecoder::DecodeHeaderTree(SmackerCommon::BitReader &bits, std::vector &recodes, int *last, int size) +{ + HuffContext huff; + HuffContext tmp1, tmp2; + int escapes[3]; + DBCtx ctx; + + if ((uint32_t)size >= UINT_MAX>>4) + { + Printf("SmackerDecoder::DecodeHeaderTree() - Size too large\n"); + return -1; + } + + tmp1.length = 256; + tmp1.maxlength = 0; + tmp1.current = 0; + + tmp1.bits.resize(256); + tmp1.lengths.resize(256); + tmp1.values.resize(256); + + tmp2.length = 256; + tmp2.maxlength = 0; + tmp2.current = 0; + + tmp2.bits.resize(256); + tmp2.lengths.resize(256); + tmp2.values.resize(256); + + // low byte tree + if (bits.GetBit()) // 1: Read Tag + { + DecodeTree(bits, &tmp1, 0, 0); + + bits.SkipBits(1); + + VLC_InitTable(ctx.v1, tmp1.maxlength, tmp1.current, &tmp1.lengths[0], &tmp1.bits[0]); + } + else + { + // Skipping low bytes tree + } + + // high byte tree + if (bits.GetBit()) + { + DecodeTree(bits, &tmp2, 0, 0); + + bits.SkipBits(1); + + VLC_InitTable(ctx.v2, tmp2.maxlength, tmp2.current, &tmp2.lengths[0], &tmp2.bits[0]); + } + else + { + // Skipping high bytes tree + } + + escapes[0] = bits.GetBits(8); + escapes[0] |= bits.GetBits(8) << 8; + escapes[1] = bits.GetBits(8); + escapes[1] |= bits.GetBits(8) << 8; + escapes[2] = bits.GetBits(8); + escapes[2] |= bits.GetBits(8) << 8; + + last[0] = last[1] = last[2] = -1; + + ctx.escapes[0] = escapes[0]; + ctx.escapes[1] = escapes[1]; + ctx.escapes[2] = escapes[2]; + + ctx.recode1 = tmp1.values; + ctx.recode2 = tmp2.values; + ctx.last = last; + + huff.length = ((size + 3) >> 2) + 3; + huff.maxlength = 0; + huff.current = 0; + huff.values.resize(huff.length); + + DecodeBigTree(bits, &huff, &ctx); + + bits.SkipBits(1); + + if (ctx.last[0] == -1) ctx.last[0] = huff.current++; + if (ctx.last[1] == -1) ctx.last[1] = huff.current++; + if (ctx.last[2] == -1) ctx.last[2] = huff.current++; + + recodes = huff.values; + + return 0; +} + +// static int decode_header_trees(SmackVContext *smk) { +bool SmackerDecoder::DecodeHeaderTrees() +{ + SmackerCommon::BitReader bits(file, treeSize); + + if (!bits.GetBit()) + { + // Skipping MMAP tree + mmap_tbl.resize(2); + mmap_tbl[0] = 0; + mmap_last[0] = mmap_last[1] = mmap_last[2] = 1; + } + else + { + DecodeHeaderTree(bits, mmap_tbl, mmap_last, mMapSize); + } + + if (!bits.GetBit()) + { + // Skipping MCLR tree + mclr_tbl.resize(2); + mclr_tbl[0] = 0; + mclr_last[0] = mclr_last[1] = mclr_last[2] = 1; + } + else + { + DecodeHeaderTree(bits, mclr_tbl, mclr_last, MClrSize); + } + + if (!bits.GetBit()) + { + // Skipping FULL tree + full_tbl.resize(2); + full_tbl[0] = 0; + full_last[0] = full_last[1] = full_last[2] = 1; + } + else + { + DecodeHeaderTree(bits, full_tbl, full_last, fullSize); + } + + if (!bits.GetBit()) + { + // Skipping TYPE tree + type_tbl.resize(2); + type_tbl[0] = 0; + type_last[0] = type_last[1] = type_last[2] = 1; + } + else + { + DecodeHeaderTree(bits, type_tbl, type_last, typeSize); + } + + /* FIXME - we don't seems to read/use EVERY bit we 'load' into the bit reader + * and as my bitreader reads from the file rather than a buffer read from file + * of size 'treeSize', I need to make sure I consume the remaining bits (and thus increment + * the file read position to where the code expects it to be when this function returns (ie + * 'treeSize' number of bytes must be read + */ + uint32_t left = bits.GetSize() - bits.GetPosition(); + bits.SkipBits(left); + + return true; +} + +void SmackerDecoder::GetNextFrame() +{ + ReadPacket(); +} + +int SmackerDecoder::ReadPacket() +{ + // test-remove + if (currentFrame >= nFrames) + return 1; + + // seek to next frame position + file.Seek(nextPos, SmackerCommon::FileStream::kSeekStart); + + uint32_t frameSize = frameSizes[currentFrame] & (~3); + uint8_t frameFlag = frameFlags[currentFrame]; + + // handle palette change + if (frameFlag & kSMKpal) + { + int size, sz, t, off, j, pos; + uint8_t *pal = palette; + uint8_t oldpal[768]; + + memcpy(oldpal, pal, 768); + size = file.ReadByte(); + size = size * 4 - 1; + frameSize -= size; + frameSize--; + sz = 0; + pos = file.GetPosition() + size; + + while (sz < 256) + { + t = file.ReadByte(); + if (t & 0x80){ /* skip palette entries */ + sz += (t & 0x7F) + 1; + pal += ((t & 0x7F) + 1) * 3; + } else if (t & 0x40){ /* copy with offset */ + off = file.ReadByte() * 3; + j = (t & 0x3F) + 1; + while (j-- && sz < 256) { + *pal++ = oldpal[off + 0]; + *pal++ = oldpal[off + 1]; + *pal++ = oldpal[off + 2]; + sz++; + off += 3; + } + } else { /* new entries */ + *pal++ = smk_pal[t]; + *pal++ = smk_pal[file.ReadByte() & 0x3F]; + *pal++ = smk_pal[file.ReadByte() & 0x3F]; + sz++; + } + } + + file.Seek(pos, SmackerCommon::FileStream::kSeekStart); + } + + frameFlag >>= 1; + + // check for and handle audio + for (int i = 0; i < kMaxAudioTracks; i++) + { + audioTracks[i].bytesReadThisFrame = 0; + + if (frameFlag & 1) + { + uint32_t size = file.ReadUint32LE() - 4; + frameSize -= size; + frameSize -= 4; + + DecodeAudio(size, audioTracks[i]); + } + frameFlag >>= 1; + } + + if (frameSize == 0) { + return -1; + } + + DecodeFrame(frameSize); + + currentFrame++; + + nextPos = file.GetPosition(); + + return 0; +} + +int SmackerDecoder::DecodeFrame(uint32_t frameSize) +{ + last_reset(mmap_tbl, mmap_last); + last_reset(mclr_tbl, mclr_last); + last_reset(full_tbl, full_last); + last_reset(type_tbl, type_last); + + int blocks, blk, bw, bh; + int i; + int stride; + + uint8_t *out = picture; // set to output image + + blk = 0; + bw = frameWidth >> 2; + bh = frameHeight >> 2; + blocks = bw * bh; + + stride = frameWidth; + + SmackerCommon::BitReader bits(file, frameSize); + + while (blk < blocks) + { + int type, run, mode; + uint16_t pix; + + type = GetCode(bits, type_tbl, type_last); + run = block_runs[(type >> 2) & 0x3F]; + switch (type & 3) + { + case SMK_BLK_MONO: + while (run-- && blk < blocks) + { + int clr, map; + int hi, lo; + clr = GetCode(bits, mclr_tbl, mclr_last); + map = GetCode(bits, mmap_tbl, mmap_last); + + out = picture + (blk / bw) * (stride * 4) + (blk % bw) * 4; + + hi = clr >> 8; + lo = clr & 0xFF; + for (i = 0; i < 4; i++) + { + if (map & 1) out[0] = hi; else out[0] = lo; + if (map & 2) out[1] = hi; else out[1] = lo; + if (map & 4) out[2] = hi; else out[2] = lo; + if (map & 8) out[3] = hi; else out[3] = lo; + map >>= 4; + out += stride; + } + blk++; + } + break; + case SMK_BLK_FULL: + mode = 0; + if (isVer4) // In case of Smacker v4 we have three modes + { + if (bits.GetBit()) mode = 1; + else if (bits.GetBit()) mode = 2; + } + + while (run-- && blk < blocks) + { + out = picture + (blk / bw) * (stride * 4) + (blk % bw) * 4; + switch (mode) + { + case 0: + for (i = 0; i < 4; i++) + { + pix = GetCode(bits, full_tbl, full_last); +// FIX AV_WL16(out+2, pix); + out[2] = pix & 0xff; + out[3] = pix >> 8; + + pix = GetCode(bits, full_tbl, full_last); +// FIX AV_WL16(out, pix); + out[0] = pix & 0xff; + out[1] = pix >> 8; + out += stride; + } + break; + case 1: + pix = GetCode(bits, full_tbl, full_last); + out[0] = out[1] = pix & 0xFF; + out[2] = out[3] = pix >> 8; + out += stride; + out[0] = out[1] = pix & 0xFF; + out[2] = out[3] = pix >> 8; + out += stride; + pix = GetCode(bits, full_tbl, full_last); + out[0] = out[1] = pix & 0xFF; + out[2] = out[3] = pix >> 8; + out += stride; + out[0] = out[1] = pix & 0xFF; + out[2] = out[3] = pix >> 8; + out += stride; + break; + case 2: + for (i = 0; i < 2; i++) + { + uint16_t pix1, pix2; + pix2 = GetCode(bits, full_tbl, full_last); + pix1 = GetCode(bits, full_tbl, full_last); + +// FIX AV_WL16(out, pix1); +// FIX AV_WL16(out+2, pix2); + out[0] = pix1 & 0xff; + out[1] = pix1 >> 8; + out[2] = pix2 & 0xff; + out[3] = pix2 >> 8; + + out += stride; + +// FIX AV_WL16(out, pix1); +// FIX AV_WL16(out+2, pix2); + out[0] = pix1 & 0xff; + out[1] = pix1 >> 8; + out[2] = pix2 & 0xff; + out[3] = pix2 >> 8; + + out += stride; + } + break; + } + blk++; + } + break; + case SMK_BLK_SKIP: + while (run-- && blk < blocks) + blk++; + break; + case SMK_BLK_FILL: + mode = type >> 8; + while (run-- && blk < blocks) + { + uint32_t col; + out = picture + (blk / bw) * (stride * 4) + (blk % bw) * 4; + col = mode * 0x01010101; + for (i = 0; i < 4; i++) { + *((uint32_t*)out) = col; + out += stride; + } + blk++; + } + break; + } + } + + /* FIXME - we don't seems to read/use EVERY bit we 'load' into the bit reader + * and as my bitreader reads from the file rather than a buffer read from file + * of size 'frameSize', I need to make sure I consume the remaining bits (and thus increment + * the file read position to where the code expects it to be when this function returns (ie + * 'frameSize' number of bytes must be read + */ + uint32_t left = bits.GetSize() - bits.GetPosition(); + bits.SkipBits(left); + + return 0; +} + +/** + * Decode Smacker audio data + */ +int SmackerDecoder::DecodeAudio(uint32_t size, SmackerAudioTrack &track) +{ + HuffContext h[4]; + SmackerCommon::VLCtable vlc[4]; + int val; + int i, res; + int unpackedSize; + int sampleBits, stereo; + int pred[2] = {0, 0}; + + int16_t *samples = reinterpret_cast(track.buffer); + int8_t *samples8 = reinterpret_cast(track.buffer); + + int buf_size = track.bufferSize; + + if (buf_size <= 4) { + Printf("SmackerDecoder::DecodeAudio() - Packet is too small\n"); + return -1; + } + + SmackerCommon::BitReader bits(file, size); + + unpackedSize = bits.GetBits(32); + + if (!bits.GetBit()) { + // no sound data + return 1; + } + + stereo = bits.GetBit(); + sampleBits = bits.GetBit(); + + if (stereo ^ (track.nChannels != 1)) { + Printf("SmackerDecoder::DecodeAudio() - Channels mismatch\n"); + return -1; + } + + memset(h, 0, sizeof(HuffContext) * 4); + + // Initialize + for (i = 0; i < (1 << (sampleBits + stereo)); i++) { + h[i].length = 256; + h[i].maxlength = 0; + h[i].current = 0; + h[i].bits.resize(256); + h[i].lengths.resize(256); + h[i].values.resize(256); + + bits.SkipBits(1); + DecodeTree(bits, &h[i], 0, 0); + bits.SkipBits(1); + + if (h[i].current > 1) { + VLC_InitTable(vlc[i], h[i].maxlength, h[i].current, &h[i].lengths[0], &h[i].bits[0]); + } + } + if (sampleBits) { //decode 16-bit data + for (i = stereo; i >= 0; i--) + pred[i] = av_bswap16(bits.GetBits(16)); + for (i = 0; i <= stereo; i++) + *samples++ = pred[i]; + for (; i < unpackedSize / 2; i++) { + if (i & stereo) { + if (VLC_GetSize(vlc[2])) + res = VLC_GetCodeBits(bits, vlc[2]); + else + res = 0; + val = h[2].values[res]; + if (VLC_GetSize(vlc[3])) + res = VLC_GetCodeBits(bits, vlc[3]); + else + res = 0; + val |= h[3].values[res] << 8; + pred[1] += (int16_t)val; + *samples++ = pred[1]; + } else { + if (VLC_GetSize(vlc[0])) + res = VLC_GetCodeBits(bits, vlc[0]); + else + res = 0; + val = h[0].values[res]; + if (VLC_GetSize(vlc[1])) + res = VLC_GetCodeBits(bits, vlc[1]); + else + res = 0; + val |= h[1].values[res] << 8; + pred[0] += val; + *samples++ = pred[0]; + } + } + } + else { //8-bit data + for (i = stereo; i >= 0; i--) + pred[i] = bits.GetBits(8); + for (i = 0; i <= stereo; i++) + *samples8++ = pred[i]; + for (; i < unpackedSize; i++) { + if (i & stereo){ + if (VLC_GetSize(vlc[1])) + res = VLC_GetCodeBits(bits, vlc[1]); + else + res = 0; + pred[1] += (int8_t)h[1].values[res]; + *samples8++ = pred[1]; + } else { + if (VLC_GetSize(vlc[0])) + res = VLC_GetCodeBits(bits, vlc[0]); + else + res = 0; + pred[0] += (int8_t)h[0].values[res]; + *samples8++ = pred[0]; + } + } + } + + track.bytesReadThisFrame = unpackedSize; + + uint32_t left = bits.GetSize() - bits.GetPosition(); + bits.SkipBits(left); + + return 0; +} + +void SmackerDecoder::GetPalette(uint8_t *palette) +{ + memcpy(palette, this->palette, 768); +} + +void SmackerDecoder::GetFrame(uint8_t *frame) +{ + memcpy(frame, this->picture, frameWidth * frameHeight); +} + +void SmackerDecoder::GetFrameSize(uint32_t &width, uint32_t &height) +{ + width = this->frameWidth; + height = this->frameHeight; +} + +uint32_t SmackerDecoder::GetNumFrames() +{ + return nFrames; +} + +uint32_t SmackerDecoder::GetCurrentFrameNum() +{ + return currentFrame; +} + +float SmackerDecoder::GetFrameRate() +{ + return (float)fps; +} + +void SmackerDecoder::GotoFrame(uint32_t frameNum) +{ + if (frameNum > nFrames) { + Printf("SmackerDecoder::GotoFrame() - Invalid frame number\n"); + return; + } + +// file.Seek(firstFrameFilePos, SmackerCommon::FileStream::kSeekStart); + + currentFrame = 0; + nextPos = firstFrameFilePos; + + for (unsigned i = 0; i < frameNum + 1; i++) + GetNextFrame(); +} + +SmackerAudioInfo SmackerDecoder::GetAudioTrackDetails(uint32_t trackIndex) +{ + SmackerAudioInfo info; + SmackerAudioTrack *track = &audioTracks[trackIndex]; + + info.sampleRate = track->sampleRate; + info.nChannels = track->nChannels; + info.bitsPerSample = track->bitsPerSample; + + // audio buffer size in bytes + info.idealBufferSize = track->bufferSize; + + return info; +} + +uint32_t SmackerDecoder::GetAudioData(uint32_t trackIndex, int16_t *audioBuffer) +{ + if (!audioBuffer) { + return 0; + } + + SmackerAudioTrack *track = &audioTracks[trackIndex]; + + if (track->bytesReadThisFrame) { + memcpy(audioBuffer, track->buffer, std::min(track->bufferSize, track->bytesReadThisFrame)); + } + + return track->bytesReadThisFrame; +} diff --git a/src/d_event.h b/src/d_event.h index 822286d0e7..da93b75e49 100644 --- a/src/d_event.h +++ b/src/d_event.h @@ -27,31 +27,9 @@ #include "basics.h" #include #include "d_eventbase.h" - +#include "gamestate.h" -enum gameaction_t : int -{ - ga_nothing, - ga_loadlevel, // not used. - ga_newgame, - ga_newgame2, - ga_recordgame, - ga_loadgame, - ga_loadgamehidecon, - ga_loadgameplaydemo, - ga_autoloadgame, - ga_savegame, - ga_autosave, - ga_playdemo, - ga_completed, - ga_slideshow, - ga_worlddone, - ga_screenshot, - ga_togglemap, - ga_fullconsole, - ga_resumeconversation, -}; diff --git a/src/gamestate.h b/src/gamestate.h index 21652392a3..199618f04f 100644 --- a/src/gamestate.h +++ b/src/gamestate.h @@ -23,3 +23,30 @@ enum gamestate_t : int }; extern gamestate_t gamestate; + +enum gameaction_t : int +{ + ga_nothing, + ga_loadlevel, // not used. + ga_newgame, + ga_newgame2, + ga_recordgame, + ga_loadgame, + ga_loadgamehidecon, + ga_loadgameplaydemo, + ga_autoloadgame, + ga_savegame, + ga_autosave, + ga_playdemo, + ga_completed, + ga_slideshow, + ga_worlddone, + ga_screenshot, + ga_togglemap, + ga_fullconsole, + ga_resumeconversation, + ga_intro, + ga_intermission, +}; + +extern gameaction_t gameaction;