1 /*
2 * Copyright (C) 2020 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 <stdlib.h>
18 #include "../includes/common.h"
19
20 // This PoC is only for 64-bit builds
21 #if _64_BIT
22 #include <media/NdkMediaCodec.h>
23
24 #define DEQUEUE_BUFFER_TIMEOUT_MICROSECONDS 1000
25 #define TOTAL_TIMEOUT_MICROSECONDS (300 * 1000 * 1000)
26 #define FILE_SIZE 72
27 #define VIDEO_MAX_WIDTH 176
28 #define VIDEO_MAX_HEIGHT 144
29 #endif /* _64_BIT */
30
main(int argc,char * argv[])31 int main(int argc, char *argv[]) {
32 (void)argc;
33 (void)argv;
34
35 // This PoC is only for 64-bit builds
36 #if _64_BIT
37 if (argc != 2) {
38 return EXIT_FAILURE;
39 }
40
41 FILE *inFile = fopen(argv[1], "rb");
42 if (!inFile) {
43 return EXIT_FAILURE;
44 }
45 AMediaCodec *codec;
46 media_status_t status;
47 int64_t inActiveTime = 0ll;
48 bool isEncoder = false;
49
50 codec = AMediaCodec_createCodecByName("c2.android.av1-aom.decoder");
51 if (!codec) {
52 fclose(inFile);
53 return EXIT_FAILURE;
54 }
55 /* Set Format */
56 AMediaFormat *format = AMediaFormat_new();
57 if (!format) {
58 fclose(inFile);
59 AMediaCodec_delete(codec);
60 return EXIT_FAILURE;
61 }
62 AMediaFormat_setString(format, AMEDIAFORMAT_KEY_MIME, "video/av01");
63 AMediaFormat_setInt32(format, AMEDIAFORMAT_KEY_MAX_INPUT_SIZE, FILE_SIZE);
64 AMediaFormat_setInt32(format, AMEDIAFORMAT_KEY_WIDTH, VIDEO_MAX_WIDTH);
65 AMediaFormat_setInt32(format, AMEDIAFORMAT_KEY_HEIGHT, VIDEO_MAX_HEIGHT);
66 AMediaCodec_configure(codec, format, nullptr, nullptr, isEncoder);
67 AMediaCodec_start(codec);
68
69 size_t filePos = 0;
70 bool inputEOS = false;
71 while (inActiveTime < TOTAL_TIMEOUT_MICROSECONDS) {
72 /* Queue input data */
73 if (!inputEOS) {
74 uint32_t bufferFlags = 0;
75 ssize_t inIdx =
76 AMediaCodec_dequeueInputBuffer(codec, DEQUEUE_BUFFER_TIMEOUT_MICROSECONDS);
77 if (inIdx >= 0) {
78 ssize_t bytesRead = 0;
79 size_t bufSize;
80 uint8_t *buf = AMediaCodec_getInputBuffer(codec, inIdx, &bufSize);
81 if (filePos < FILE_SIZE) {
82 bytesRead = fread(buf, 1, FILE_SIZE, inFile);
83 filePos += FILE_SIZE;
84 fseek(inFile, filePos, SEEK_SET);
85 }
86 if (bytesRead <= 0) {
87 bytesRead = 0;
88 inputEOS = true;
89 bufferFlags |= AMEDIACODEC_BUFFER_FLAG_END_OF_STREAM;
90 }
91 status = AMediaCodec_queueInputBuffer(codec, inIdx, 0, bytesRead, 0, bufferFlags);
92 if (status != AMEDIA_OK) {
93 break;
94 }
95 inActiveTime = 0;
96 } else if (inIdx == AMEDIACODEC_INFO_TRY_AGAIN_LATER) {
97 inActiveTime += DEQUEUE_BUFFER_TIMEOUT_MICROSECONDS;
98 } else {
99 break;
100 }
101 }
102 /* Dequeue output */
103 AMediaCodecBufferInfo info;
104 ssize_t outIdx =
105 AMediaCodec_dequeueOutputBuffer(codec, &info, DEQUEUE_BUFFER_TIMEOUT_MICROSECONDS);
106 if (outIdx == AMEDIACODEC_INFO_OUTPUT_FORMAT_CHANGED ||
107 outIdx == AMEDIACODEC_INFO_OUTPUT_BUFFERS_CHANGED) {
108 inActiveTime = 0;
109 } else if (outIdx >= 0) {
110 status = AMediaCodec_releaseOutputBuffer(codec, outIdx, false);
111 if (status != AMEDIA_OK) {
112 break;
113 }
114 if (info.flags & AMEDIACODEC_BUFFER_FLAG_END_OF_STREAM) {
115 break;
116 }
117 inActiveTime = 0;
118 } else if (outIdx == AMEDIACODEC_INFO_TRY_AGAIN_LATER) {
119 inActiveTime += DEQUEUE_BUFFER_TIMEOUT_MICROSECONDS;
120 } else {
121 break;
122 }
123 }
124 AMediaFormat_delete(format);
125 AMediaCodec_stop(codec);
126 AMediaCodec_delete(codec);
127 fclose(inFile);
128 #endif /* _64_BIT */
129
130 return EXIT_SUCCESS;
131 }
132