1 /* 2 * Copyright (C) 2017 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 FRAME_DECODER_H_ 18 #define FRAME_DECODER_H_ 19 20 #include <memory> 21 #include <vector> 22 23 #include <media/stagefright/foundation/AString.h> 24 #include <media/stagefright/foundation/ABase.h> 25 #include <media/stagefright/MediaSource.h> 26 #include <media/openmax/OMX_Video.h> 27 #include <ui/GraphicTypes.h> 28 29 namespace android { 30 31 struct AMessage; 32 struct MediaCodec; 33 class IMediaSource; 34 class MediaCodecBuffer; 35 class Surface; 36 class VideoFrame; 37 38 struct FrameRect { 39 int32_t left, top, right, bottom; 40 }; 41 42 struct FrameDecoder : public RefBase { 43 FrameDecoder( 44 const AString &componentName, 45 const sp<MetaData> &trackMeta, 46 const sp<IMediaSource> &source); 47 48 status_t init(int64_t frameTimeUs, int option, int colorFormat); 49 50 sp<IMemory> extractFrame(FrameRect *rect = NULL); 51 52 static sp<IMemory> getMetadataOnly( 53 const sp<MetaData> &trackMeta, int colorFormat, bool thumbnail = false); 54 55 protected: 56 virtual ~FrameDecoder(); 57 58 virtual sp<AMessage> onGetFormatAndSeekOptions( 59 int64_t frameTimeUs, 60 int seekMode, 61 MediaSource::ReadOptions *options, 62 sp<Surface> *window) = 0; 63 64 virtual status_t onExtractRect(FrameRect *rect) = 0; 65 66 virtual status_t onInputReceived( 67 const sp<MediaCodecBuffer> &codecBuffer, 68 MetaDataBase &sampleMeta, 69 bool firstSample, 70 uint32_t *flags) = 0; 71 72 virtual status_t onOutputReceived( 73 const sp<MediaCodecBuffer> &videoFrameBuffer, 74 const sp<AMessage> &outputFormat, 75 int64_t timeUs, 76 bool *done) = 0; 77 trackMetaFrameDecoder78 sp<MetaData> trackMeta() const { return mTrackMeta; } dstFormatFrameDecoder79 OMX_COLOR_FORMATTYPE dstFormat() const { return mDstFormat; } captureFormatFrameDecoder80 ui::PixelFormat captureFormat() const { return mCaptureFormat; } dstBppFrameDecoder81 int32_t dstBpp() const { return mDstBpp; } setFrameFrameDecoder82 void setFrame(const sp<IMemory> &frameMem) { mFrameMemory = frameMem; } 83 84 private: 85 AString mComponentName; 86 sp<MetaData> mTrackMeta; 87 sp<IMediaSource> mSource; 88 OMX_COLOR_FORMATTYPE mDstFormat; 89 ui::PixelFormat mCaptureFormat; 90 int32_t mDstBpp; 91 sp<IMemory> mFrameMemory; 92 MediaSource::ReadOptions mReadOptions; 93 sp<MediaCodec> mDecoder; 94 sp<AMessage> mOutputFormat; 95 bool mHaveMoreInputs; 96 bool mFirstSample; 97 sp<Surface> mSurface; 98 99 status_t extractInternal(); 100 101 DISALLOW_EVIL_CONSTRUCTORS(FrameDecoder); 102 }; 103 struct FrameCaptureLayer; 104 105 struct VideoFrameDecoder : public FrameDecoder { 106 VideoFrameDecoder( 107 const AString &componentName, 108 const sp<MetaData> &trackMeta, 109 const sp<IMediaSource> &source); 110 111 protected: 112 virtual sp<AMessage> onGetFormatAndSeekOptions( 113 int64_t frameTimeUs, 114 int seekMode, 115 MediaSource::ReadOptions *options, 116 sp<Surface> *window) override; 117 onExtractRectVideoFrameDecoder118 virtual status_t onExtractRect(FrameRect *rect) override { 119 // Rect extraction for sequences is not supported for now. 120 return (rect == NULL) ? OK : ERROR_UNSUPPORTED; 121 } 122 123 virtual status_t onInputReceived( 124 const sp<MediaCodecBuffer> &codecBuffer, 125 MetaDataBase &sampleMeta, 126 bool firstSample, 127 uint32_t *flags) override; 128 129 virtual status_t onOutputReceived( 130 const sp<MediaCodecBuffer> &videoFrameBuffer, 131 const sp<AMessage> &outputFormat, 132 int64_t timeUs, 133 bool *done) override; 134 135 private: 136 sp<FrameCaptureLayer> mCaptureLayer; 137 VideoFrame *mFrame; 138 bool mIsAvc; 139 bool mIsHevc; 140 MediaSource::ReadOptions::SeekMode mSeekMode; 141 int64_t mTargetTimeUs; 142 List<int64_t> mSampleDurations; 143 int64_t mDefaultSampleDurationUs; 144 145 sp<Surface> initSurface(); 146 status_t captureSurface(); 147 }; 148 149 struct MediaImageDecoder : public FrameDecoder { 150 MediaImageDecoder( 151 const AString &componentName, 152 const sp<MetaData> &trackMeta, 153 const sp<IMediaSource> &source); 154 155 protected: 156 virtual sp<AMessage> onGetFormatAndSeekOptions( 157 int64_t frameTimeUs, 158 int seekMode, 159 MediaSource::ReadOptions *options, 160 sp<Surface> *window) override; 161 162 virtual status_t onExtractRect(FrameRect *rect) override; 163 onInputReceivedMediaImageDecoder164 virtual status_t onInputReceived( 165 const sp<MediaCodecBuffer> &codecBuffer __unused, 166 MetaDataBase &sampleMeta __unused, 167 bool firstSample __unused, 168 uint32_t *flags __unused) override { return OK; } 169 170 virtual status_t onOutputReceived( 171 const sp<MediaCodecBuffer> &videoFrameBuffer, 172 const sp<AMessage> &outputFormat, 173 int64_t timeUs, 174 bool *done) override; 175 176 private: 177 VideoFrame *mFrame; 178 int32_t mWidth; 179 int32_t mHeight; 180 int32_t mGridRows; 181 int32_t mGridCols; 182 int32_t mTileWidth; 183 int32_t mTileHeight; 184 int32_t mTilesDecoded; 185 int32_t mTargetTiles; 186 }; 187 188 } // namespace android 189 190 #endif // FRAME_DECODER_H_ 191