1 /*
2 * Copyright (C) 2021 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 #include <dlfcn.h>
18 #include <stdlib.h>
19 #include <string.h>
20 #include <sys/time.h>
21
22 #include "../includes/memutils.h"
23 #include "vpx/vp8cx.h"
24
25 #define ENCODE_WIDTH 11
26 #define ENCODE_HEIGHT 10000
27 #define LIBNAME "/system/apex/com.android.media.swcodec/lib64/libvpx.so"
28 #define LIBNAME_APEX "/apex/com.android.media.swcodec/lib64/libvpx.so"
29
30 char enable_selective_overload = ENABLE_NONE;
31
main()32 int main() {
33 void *libHandle = dlopen(LIBNAME, RTLD_NOW | RTLD_LOCAL);
34 if (!libHandle) {
35 libHandle = dlopen(LIBNAME_APEX, RTLD_NOW | RTLD_LOCAL);
36 if (!libHandle) {
37 return EXIT_FAILURE;
38 }
39 }
40 vpx_codec_err_t codec_return = VPX_CODEC_OK;
41 vpx_codec_enc_cfg_t mCodecConfiguration;
42 vpx_image_t raw_frame;
43 uint32_t framerate = (30 << 16);
44 vpx_codec_iface_t *(*func_ptr1)() =
45 (vpx_codec_iface_t * (*)()) dlsym(libHandle, "vpx_codec_vp8_cx");
46 if (!func_ptr1) {
47 dlclose(libHandle);
48 return EXIT_FAILURE;
49 }
50 vpx_codec_iface_t *mCodecInterface = (*func_ptr1)();
51 if (!mCodecInterface) {
52 dlclose(libHandle);
53 return EXIT_FAILURE;
54 }
55 vpx_codec_err_t (*func_ptr2)(vpx_codec_iface_t *, vpx_codec_enc_cfg_t *, unsigned int) =
56 (vpx_codec_err_t(*)(vpx_codec_iface_t *, vpx_codec_enc_cfg_t *, unsigned int))dlsym(
57 libHandle, "vpx_codec_enc_config_default");
58 if (!func_ptr2) {
59 dlclose(libHandle);
60 return EXIT_FAILURE;
61 }
62 codec_return = (*func_ptr2)(mCodecInterface, &mCodecConfiguration, 0);
63 mCodecConfiguration.g_w = ENCODE_WIDTH;
64 mCodecConfiguration.g_h = ENCODE_HEIGHT;
65 vpx_codec_ctx_t mCodecContext;
66 vpx_codec_err_t (*func_ptr3)(vpx_codec_ctx_t *, vpx_codec_iface_t *, vpx_codec_enc_cfg_t *,
67 vpx_codec_flags_t, int) =
68 (vpx_codec_err_t(*)(vpx_codec_ctx_t *, vpx_codec_iface_t *, vpx_codec_enc_cfg_t *,
69 vpx_codec_flags_t, int))dlsym(libHandle, "vpx_codec_enc_init_ver");
70 if (!func_ptr3) {
71 dlclose(libHandle);
72 return EXIT_FAILURE;
73 }
74 codec_return = (*func_ptr3)(&mCodecContext, mCodecInterface, &mCodecConfiguration, 0,
75 VPX_ENCODER_ABI_VERSION);
76
77 if (codec_return != VPX_CODEC_OK) {
78 return EXIT_FAILURE;
79 }
80
81 enable_selective_overload = ENABLE_ALL;
82 unsigned char *source = (unsigned char *)memalign(16, (ENCODE_WIDTH * ENCODE_HEIGHT * 3 / 2));
83 enable_selective_overload = ENABLE_FREE_CHECK | ENABLE_REALLOC_CHECK;
84
85 if (!source) {
86 return EXIT_FAILURE;
87 }
88 memset(source, 0, (ENCODE_WIDTH * ENCODE_HEIGHT * 3 / 2));
89 vpx_image_t (*func_ptr4)(vpx_image_t *, vpx_img_fmt_t, unsigned int, unsigned int, unsigned int,
90 unsigned char *) =
91 (vpx_image(*)(vpx_image *, vpx_img_fmt, unsigned int, unsigned int, unsigned int,
92 unsigned char *))dlsym(libHandle, "vpx_img_wrap");
93 if (!func_ptr4) {
94 dlclose(libHandle);
95 free(source);
96 return EXIT_FAILURE;
97 }
98 (*func_ptr4)(&raw_frame, VPX_IMG_FMT_I420, ENCODE_WIDTH, ENCODE_HEIGHT, 1, source);
99 vpx_codec_err_t (*func_ptr5)(vpx_codec_ctx_t *, const vpx_image_t *, vpx_codec_pts_t,
100 unsigned long, vpx_enc_frame_flags_t, unsigned long) =
101 (vpx_codec_err_t(*)(vpx_codec_ctx *, const vpx_image *, long, unsigned long, long,
102 unsigned long))dlsym(libHandle, "vpx_codec_encode");
103 if (!func_ptr5) {
104 dlclose(libHandle);
105 free(source);
106 return EXIT_FAILURE;
107 }
108 codec_return =
109 (*func_ptr5)(&mCodecContext, &raw_frame, framerate,
110 (uint32_t)(((uint64_t)1000000 << 16) / framerate), 0, VPX_DL_REALTIME);
111 if (codec_return != VPX_CODEC_OK) {
112 free(source);
113 return EXIT_FAILURE;
114 }
115 vpx_codec_err_t (*func_ptr6)(vpx_codec_ctx_t *) =
116 (vpx_codec_err_t(*)(vpx_codec_ctx *))dlsym(libHandle, "vpx_codec_destroy");
117 if (!func_ptr6) {
118 dlclose(libHandle);
119 free(source);
120 return EXIT_FAILURE;
121 }
122 (*func_ptr6)(&mCodecContext);
123 dlclose(libHandle);
124 free(source);
125 return EXIT_SUCCESS;
126 }
127