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, unsigned int height, 78 unsigned int outWidth, unsigned int outHeight, 79 PixelFormat pixFmt); 80 void resetH264Context(unsigned int width, unsigned int height, 81 unsigned int outWidth, unsigned int outHeight, 82 PixelFormat pixFmt); 83 void destroyH264Context(); 84 h264_result_t decodeFrame(uint8_t *img, size_t szBytes, uint64_t pts); 85 void flush(); 86 // ask host to copy image data back to guest, with image metadata 87 // to guest as well 88 h264_image_t getImage(); 89 // ask host to render to hostColorBufferId, return only image metadata back 90 // to guest 91 h264_image_t renderOnHostAndReturnImageMetadata(int hostColorBufferId); 92 }; 93 #endif 94