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 #include <sys/types.h>
22 #include <sys/wait.h>
23 #include "../includes/memutils.h"
24 #include "vpx/vp8cx.h"
25 #include "vpx/vpx_codec.h"
26 #include "vpx/vpx_encoder.h"
27
28 #define ENCODE_WIDTH 11
29 #define ENCODE_HEIGHT 10000
30 #define LIBNAME "/system/apex/com.android.media.swcodec/lib64/libvpx.so"
31 #define LIBNAME_APEX "/apex/com.android.media.swcodec/lib64/libvpx.so"
32
33 char enable_selective_overload = ENABLE_NONE;
34
main()35 int main() {
36 enable_selective_overload = ENABLE_ALL;
37 void *libHandle = dlopen(LIBNAME, RTLD_NOW | RTLD_LOCAL);
38 if (!libHandle) {
39 libHandle = dlopen(LIBNAME_APEX, RTLD_NOW | RTLD_LOCAL);
40 if (!libHandle) {
41 return EXIT_FAILURE;
42 }
43 }
44 vpx_codec_err_t codec_return = VPX_CODEC_OK;
45 vpx_codec_enc_cfg_t mCodecConfiguration;
46 vpx_image_t raw_frame;
47 uint32_t framerate = (30 << 16);
48 vpx_codec_iface_t *(*func_ptr1)() =
49 (vpx_codec_iface_t * (*)()) dlsym(libHandle, "vpx_codec_vp8_cx");
50 if (!func_ptr1) {
51 dlclose(libHandle);
52 return EXIT_FAILURE;
53 }
54 vpx_codec_iface_t *mCodecInterface = (*func_ptr1)();
55 if (!mCodecInterface) {
56 dlclose(libHandle);
57 return EXIT_FAILURE;
58 }
59 vpx_codec_err_t (*func_ptr2)(vpx_codec_iface_t *, vpx_codec_enc_cfg_t *, unsigned int) =
60 (vpx_codec_err_t(*)(vpx_codec_iface_t *, vpx_codec_enc_cfg_t *, unsigned int))dlsym(
61 libHandle, "vpx_codec_enc_config_default");
62 if (!func_ptr2) {
63 dlclose(libHandle);
64 return EXIT_FAILURE;
65 }
66 codec_return = (*func_ptr2)(mCodecInterface, &mCodecConfiguration, 0);
67 mCodecConfiguration.g_w = ENCODE_WIDTH;
68 mCodecConfiguration.g_h = ENCODE_HEIGHT;
69 vpx_codec_ctx_t mCodecContext;
70 vpx_codec_err_t (*func_ptr3)(vpx_codec_ctx_t *, vpx_codec_iface_t *, vpx_codec_enc_cfg_t *,
71 vpx_codec_flags_t, int) =
72 (vpx_codec_err_t(*)(vpx_codec_ctx_t *, vpx_codec_iface_t *, vpx_codec_enc_cfg_t *,
73 vpx_codec_flags_t, int))dlsym(libHandle, "vpx_codec_enc_init_ver");
74 if (!func_ptr3) {
75 dlclose(libHandle);
76 return EXIT_FAILURE;
77 }
78 codec_return = (*func_ptr3)(&mCodecContext, mCodecInterface, &mCodecConfiguration, 0,
79 VPX_ENCODER_ABI_VERSION);
80
81 if (codec_return != VPX_CODEC_OK) {
82 return EXIT_FAILURE;
83 }
84 unsigned char *source = (unsigned char *)memalign(16, (ENCODE_WIDTH * ENCODE_HEIGHT * 3 / 2));
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 enable_selective_overload = ENABLE_NONE;
124 dlclose(libHandle);
125 free(source);
126 return EXIT_SUCCESS;
127 }
128