1 /*
2  * Copyright 2015 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 #ifndef GOLDFISH_MEDIA_H264_DEC_H_
18 #define GOLDFISH_MEDIA_H264_DEC_H_
19 
20 struct h264_init_result_t {
21     uint64_t host_handle;
22     int ret;
23 };
24 
25 struct h264_result_t {
26     int ret;
27     uint64_t bytesProcessed;
28 };
29 
30 struct h264_image_t {
31     const uint8_t* data;
32     uint32_t width;
33     uint32_t height;
34     uint64_t pts; // presentation time stamp
35     uint64_t color_primaries;
36     uint64_t color_range;
37     uint64_t color_trc;
38     uint64_t colorspace;
39     // on success, |ret| will indicate the size of |data|.
40     // If failed, |ret| will contain some negative error code.
41     int ret;
42 };
43 
44 enum class RenderMode {
45   RENDER_BY_HOST_GPU = 1,
46   RENDER_BY_GUEST_CPU = 2,
47 };
48 
49 class MediaH264Decoder {
50     uint64_t mHostHandle = 0;
51     uint32_t mVersion = 100;
52     RenderMode  mRenderMode = RenderMode::RENDER_BY_GUEST_CPU;
53 
54     bool mHasAddressSpaceMemory = false;
55     uint64_t mAddressOffSet = 0;
56     int mSlot = -1;
57 
58 public:
59     MediaH264Decoder(RenderMode renderMode);
60     virtual ~MediaH264Decoder() = default;
61 
62     enum class PixelFormat : uint8_t {
63         YUV420P = 0,
64         UYVY422 = 1,
65         BGRA8888 = 2,
66     };
67 
68     enum class Err : int {
69         NoErr = 0,
70         NoDecodedFrame = -1,
71         InitContextFailed = -2,
72         DecoderRestarted = -3,
73         NALUIgnored = -4,
74     };
75 
76     bool getAddressSpaceMemory();
77     void initH264Context(unsigned int width,
78                          unsigned int height,
79                          unsigned int outWidth,
80                          unsigned int outHeight,
81                          PixelFormat pixFmt);
82     void resetH264Context(unsigned int width,
83                          unsigned int height,
84                          unsigned int outWidth,
85                          unsigned int outHeight,
86                          PixelFormat pixFmt);
87     void destroyH264Context();
88     h264_result_t decodeFrame(uint8_t* img, size_t szBytes, uint64_t pts);
89     void flush();
90     // ask host to copy image data back to guest, with image metadata
91     // to guest as well
92     h264_image_t getImage();
93     // ask host to render to hostColorBufferId, return only image metadata back to
94     // guest
95     h264_image_t renderOnHostAndReturnImageMetadata(int hostColorBufferId);
96 };
97 #endif
98