1 // Copyright (C) 2020 The Android Open Source Project
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
15 #include "host-common/MediaH264DecoderVideoToolBoxProxy.h"
16 
17 #include <cstdint>
18 #include <string>
19 #include <vector>
20 
21 #include <stdio.h>
22 #include <string.h>
23 
24 #include <stddef.h>
25 
26 #define MEDIA_H264_DEBUG 0
27 
28 #if MEDIA_H264_DEBUG
29 #define H264_DPRINT(fmt,...) fprintf(stderr, "h264-videotoolbox-proxy-dec: %s:%d " fmt "\n", __func__, __LINE__, ##__VA_ARGS__);
30 #else
31 #define H264_DPRINT(fmt,...)
32 #endif
33 
34 
35 namespace android {
36 namespace emulation {
37 
38 using InitContextParam = H264PingInfoParser::InitContextParam;
39 using DecodeFrameParam = H264PingInfoParser::DecodeFrameParam;
40 using ResetParam = H264PingInfoParser::ResetParam;
41 using GetImageParam = H264PingInfoParser::GetImageParam;
MediaH264DecoderVideoToolBoxProxy(uint64_t id,H264PingInfoParser parser)42 MediaH264DecoderVideoToolBoxProxy::MediaH264DecoderVideoToolBoxProxy(
43         uint64_t id,
44         H264PingInfoParser parser)
45     : mId(id),
46       mParser(parser),
47       mFfmpegDecoder(id, parser),
48       mVideoToolBoxDecoder(id, parser) {
49     mCurrentDecoder = &mVideoToolBoxDecoder;
50 }
51 
clone()52 MediaH264DecoderPlugin* MediaH264DecoderVideoToolBoxProxy::clone() {
53     return new MediaH264DecoderVideoToolBoxProxy(mId, mParser);
54 }
55 
~MediaH264DecoderVideoToolBoxProxy()56 MediaH264DecoderVideoToolBoxProxy::~MediaH264DecoderVideoToolBoxProxy() {
57     mCurrentDecoder->destroyH264Context();
58     mCurrentDecoder = nullptr;
59 }
60 
initH264Context(void * ptr)61 void MediaH264DecoderVideoToolBoxProxy::initH264Context(void* ptr) {
62     mVideoToolBoxDecoder.initH264Context(ptr);
63     mFfmpegDecoder.initH264Context(ptr);
64 }
65 
reset(void * ptr)66 void MediaH264DecoderVideoToolBoxProxy::reset(void* ptr) {
67     mCurrentDecoder->reset(ptr);
68 }
69 
prefixNaluHeader(std::vector<uint8_t> data)70 std::vector<uint8_t> MediaH264DecoderVideoToolBoxProxy::prefixNaluHeader(std::vector<uint8_t> data) {
71     std::vector<uint8_t> result {0x00, 0x00, 0x00,0x01};
72     result.insert(result.end(), data.begin(), data.end());
73     return result;
74 }
75 
decodeFrame(void * ptr)76 void MediaH264DecoderVideoToolBoxProxy::decodeFrame(void* ptr) {
77     if (mIsVideoToolBoxDecoderInGoodState) {
78         mVideoToolBoxDecoder.decodeFrame(ptr);
79         if (mVideoToolBoxDecoder.getState() == DecoderState::BAD_STATE) {
80             mSPS = prefixNaluHeader(mVideoToolBoxDecoder.getSPS());
81             mPPS = prefixNaluHeader(mVideoToolBoxDecoder.getPPS());
82             // right now, the only place that videotoolbox can fail is when it gets SPS and PPS, and
83             // failed to create decoding session
84             mVideoToolBoxDecoder.destroyH264Context();
85             mIsVideoToolBoxDecoderInGoodState = false;
86             mFfmpegDecoder.decodeFrameDirect(ptr, &(mSPS[0]), mSPS.size(), 0);
87             mFfmpegDecoder.decodeFrameDirect(ptr, &(mPPS[0]), mPPS.size(), 0);
88             mCurrentDecoder = &mFfmpegDecoder;
89         }
90     } else {
91         mFfmpegDecoder.decodeFrame(ptr);
92     }
93 }
94 
flush(void * ptr)95 void MediaH264DecoderVideoToolBoxProxy::flush(void* ptr) {
96     mCurrentDecoder->flush(ptr);
97 }
98 
getImage(void * ptr)99 void MediaH264DecoderVideoToolBoxProxy::getImage(void* ptr) {
100     mCurrentDecoder->getImage(ptr);
101 }
102 
destroyH264Context()103 void MediaH264DecoderVideoToolBoxProxy::destroyH264Context() {
104     mCurrentDecoder->destroyH264Context();
105 }
106 
save(base::Stream * stream) const107 void MediaH264DecoderVideoToolBoxProxy::save(base::Stream* stream) const {
108     H264_DPRINT("saving ...");
109     stream->putBe32(mParser.version());
110     const int useHardwareDecoder = mIsVideoToolBoxDecoderInGoodState ? 1 : 0;
111     stream->putBe32(useHardwareDecoder);
112     mFfmpegDecoder.save(stream);
113     mVideoToolBoxDecoder.save(stream);
114 }
115 
load(base::Stream * stream)116 bool MediaH264DecoderVideoToolBoxProxy::load(base::Stream* stream) {
117     H264_DPRINT("loading ...");
118     uint32_t version = stream->getBe32();
119     mParser = H264PingInfoParser{version};
120     const int useHardwareDecoder = stream->getBe32();
121     mFfmpegDecoder.load(stream);
122     mVideoToolBoxDecoder.load(stream);
123     if (useHardwareDecoder) {
124         mCurrentDecoder = &mVideoToolBoxDecoder;
125     } else {
126         mCurrentDecoder = &mFfmpegDecoder;
127     }
128     return true;
129 }
130 
131 }  // namespace emulation
132 }  // namespace android
133