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 
57 public:
58     MediaH264Decoder(RenderMode renderMode);
59     virtual ~MediaH264Decoder() = default;
60 
61     enum class PixelFormat : uint8_t {
62         YUV420P = 0,
63         UYVY422 = 1,
64         BGRA8888 = 2,
65     };
66 
67     enum class Err : int {
68         NoErr = 0,
69         NoDecodedFrame = -1,
70         InitContextFailed = -2,
71         DecoderRestarted = -3,
72         NALUIgnored = -4,
73     };
74 
75     bool getAddressSpaceMemory();
76     void initH264Context(unsigned int width,
77                          unsigned int height,
78                          unsigned int outWidth,
79                          unsigned int outHeight,
80                          PixelFormat pixFmt);
81     void resetH264Context(unsigned int width,
82                          unsigned int height,
83                          unsigned int outWidth,
84                          unsigned int outHeight,
85                          PixelFormat pixFmt);
86     void destroyH264Context();
87     h264_result_t decodeFrame(uint8_t* img, size_t szBytes, uint64_t pts);
88     void flush();
89     // ask host to copy image data back to guest, with image metadata
90     // to guest as well
91     h264_image_t getImage();
92     // ask host to render to hostColorBufferId, return only image metadata back to
93     // guest
94     h264_image_t renderOnHostAndReturnImageMetadata(int hostColorBufferId);
95 };
96 #endif
97