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 #include "goldfish_media_utils.h"
21 
22 struct h264_init_result_t {
23     uint64_t host_handle;
24     int ret;
25 };
26 
27 struct h264_result_t {
28     int ret;
29     uint64_t bytesProcessed;
30 };
31 
32 struct h264_image_t {
33     const uint8_t *data;
34     uint32_t width;
35     uint32_t height;
36     uint64_t pts; // presentation time stamp
37     uint64_t color_primaries;
38     uint64_t color_range;
39     uint64_t color_trc;
40     uint64_t colorspace;
41     // on success, |ret| will indicate the size of |data|.
42     // If failed, |ret| will contain some negative error code.
43     int ret;
44 };
45 
46 enum class RenderMode {
47     RENDER_BY_HOST_GPU = 1,
48     RENDER_BY_GUEST_CPU = 2,
49 };
50 
51 class MediaH264Decoder {
52     uint64_t mHostHandle = 0;
53     uint32_t mVersion = 100;
54     RenderMode mRenderMode = RenderMode::RENDER_BY_GUEST_CPU;
55 
56     bool mHasAddressSpaceMemory = false;
57     uint64_t mAddressOffSet = 0;
58     int mSlot = -1;
59 
60   public:
61     MediaH264Decoder(RenderMode renderMode);
62     virtual ~MediaH264Decoder() = default;
63 
64     enum class PixelFormat : uint8_t {
65         YUV420P = 0,
66         UYVY422 = 1,
67         BGRA8888 = 2,
68     };
69 
70     enum class Err : int {
71         NoErr = 0,
72         NoDecodedFrame = -1,
73         InitContextFailed = -2,
74         DecoderRestarted = -3,
75         NALUIgnored = -4,
76     };
77 
78     bool getAddressSpaceMemory();
79     void initH264Context(unsigned int width, unsigned int height,
80                          unsigned int outWidth, unsigned int outHeight,
81                          PixelFormat pixFmt);
82     void resetH264Context(unsigned int width, unsigned int height,
83                           unsigned int outWidth, unsigned int outHeight,
84                           PixelFormat pixFmt);
85     void destroyH264Context();
86     h264_result_t decodeFrame(uint8_t *img, size_t szBytes, uint64_t pts);
87     void flush();
88     // ask host to copy image data back to guest, with image metadata
89     // to guest as well
90     h264_image_t getImage();
91     // ask host to render to hostColorBufferId, return only image metadata back
92     // to guest
93     h264_image_t renderOnHostAndReturnImageMetadata(int hostColorBufferId);
94 
95     // send metadata about the bitstream to host, such as color aspects that
96     // are set by the framework, e.g., color primaries (601, 709 etc), range
97     // (full range or limited range), transfer etc. given metadata could be
98     // of all kinds of types, the convention is that the first field server as
99     // metadata type id. host will check the type id to decide what to do with
100     // it; unrecognized typeid will be discarded by host side.
101 
102     void sendMetadata(MetaDataColorAspects *ptr);
103 };
104 #endif
105