• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 <mutex>
22 #include <queue>
23 #include <vector>
24 
25 #include <media/openmax/OMX_Video.h>
26 #include <media/stagefright/MediaSource.h>
27 #include <media/stagefright/foundation/ABase.h>
28 #include <media/stagefright/foundation/AHandler.h>
29 #include <media/stagefright/foundation/AString.h>
30 #include <ui/GraphicTypes.h>
31 
32 namespace android {
33 
34 struct AMessage;
35 struct MediaCodec;
36 class IMediaSource;
37 class MediaCodecBuffer;
38 class Surface;
39 class VideoFrame;
40 struct AsyncCodecHandler;
41 
42 struct FrameRect {
43     int32_t left, top, right, bottom;
44 };
45 
46 struct InputBufferIndexQueue {
47 public:
48     void enqueue(int32_t index);
49     bool dequeue(int32_t* index, int32_t timeOutUs);
50 
51 private:
52     std::queue<int32_t> mQueue;
53     std::mutex mMutex;
54     std::condition_variable mCondition;
55 };
56 
57 struct FrameDecoder : public RefBase {
58     FrameDecoder(
59             const AString &componentName,
60             const sp<MetaData> &trackMeta,
61             const sp<IMediaSource> &source);
62 
63     status_t init(int64_t frameTimeUs, int option, int colorFormat);
64 
65     sp<IMemory> extractFrame(FrameRect *rect = NULL);
66 
67     static sp<IMemory> getMetadataOnly(
68             const sp<MetaData> &trackMeta, int colorFormat,
69             bool thumbnail = false, uint32_t bitDepth = 0);
70 
71     status_t handleInputBufferAsync(int32_t index);
72     status_t handleOutputBufferAsync(int32_t index, int64_t timeUs);
73     status_t handleOutputFormatChangeAsync(sp<AMessage> format);
74 
75     enum {
76         kWhatCallbackNotify,
77     };
78 
79 protected:
80     AString mComponentName;
81     sp<AMessage> mOutputFormat;
82     bool mUseBlockModel;
83 
84     virtual ~FrameDecoder();
85 
86     virtual sp<AMessage> onGetFormatAndSeekOptions(
87             int64_t frameTimeUs,
88             int seekMode,
89             MediaSource::ReadOptions *options,
90             sp<Surface> *window) = 0;
91 
92     virtual status_t onExtractRect(FrameRect *rect) = 0;
93 
94     virtual status_t onInputReceived(uint8_t* data, size_t size, MetaDataBase& sampleMeta,
95                                      bool firstSample, uint32_t* flags) = 0;
96 
97     virtual status_t onOutputReceived(
98             uint8_t* data,
99             sp<ABuffer> imgObj,
100             const sp<AMessage> &outputFormat,
101             int64_t timeUs,
102             bool *done) = 0;
103 
trackMetaFrameDecoder104     sp<MetaData> trackMeta()     const      { return mTrackMeta; }
dstFormatFrameDecoder105     OMX_COLOR_FORMATTYPE dstFormat() const  { return mDstFormat; }
captureFormatFrameDecoder106     ui::PixelFormat captureFormat() const   { return mCaptureFormat; }
dstBppFrameDecoder107     int32_t dstBpp()             const      { return mDstBpp; }
setFrameFrameDecoder108     void setFrame(const sp<IMemory> &frameMem) { mFrameMemory = frameMem; }
109 
110 private:
111     sp<MetaData> mTrackMeta;
112     sp<IMediaSource> mSource;
113     OMX_COLOR_FORMATTYPE mDstFormat;
114     ui::PixelFormat mCaptureFormat;
115     int32_t mDstBpp;
116     sp<IMemory> mFrameMemory;
117     MediaSource::ReadOptions mReadOptions;
118     sp<MediaCodec> mDecoder;
119     sp<AsyncCodecHandler> mHandler;
120     sp<ALooper> mAsyncLooper;
121     bool mHaveMoreInputs;
122     bool mFirstSample;
123     bool mSourceStopped;
124     bool mHandleOutputBufferAsyncDone;
125     sp<Surface> mSurface;
126     std::mutex mMutex;
127     std::condition_variable mOutputFramePending;
128     InputBufferIndexQueue mInputBufferIndexQueue;
129 
130     status_t extractInternal();
131     status_t extractInternalUsingBlockModel();
132 
133     DISALLOW_EVIL_CONSTRUCTORS(FrameDecoder);
134 };
135 struct FrameCaptureLayer;
136 
137 struct AsyncCodecHandler : public AHandler {
138 public:
139     explicit AsyncCodecHandler(const wp<FrameDecoder>& frameDecoder);
140     virtual void onMessageReceived(const sp<AMessage>& msg);
141 
142 private:
143     wp<FrameDecoder> mFrameDecoder;
144 };
145 
146 struct VideoFrameDecoder : public FrameDecoder {
147     VideoFrameDecoder(
148             const AString &componentName,
149             const sp<MetaData> &trackMeta,
150             const sp<IMediaSource> &source);
151 
152 protected:
153     virtual sp<AMessage> onGetFormatAndSeekOptions(
154             int64_t frameTimeUs,
155             int seekMode,
156             MediaSource::ReadOptions *options,
157             sp<Surface> *window) override;
158 
onExtractRectVideoFrameDecoder159     virtual status_t onExtractRect(FrameRect *rect) override {
160         // Rect extraction for sequences is not supported for now.
161         return (rect == NULL) ? OK : ERROR_UNSUPPORTED;
162     }
163 
164     virtual status_t onInputReceived(uint8_t* data, size_t size, MetaDataBase& sampleMeta,
165                                      bool firstSample, uint32_t* flags) override;
166 
167     virtual status_t onOutputReceived(
168             uint8_t* data,
169             sp<ABuffer> imgObj,
170             const sp<AMessage> &outputFormat,
171             int64_t timeUs,
172             bool *done) override;
173 
174 private:
175     sp<FrameCaptureLayer> mCaptureLayer;
176     VideoFrame *mFrame;
177     bool mIsAvc;
178     bool mIsHevc;
179     MediaSource::ReadOptions::SeekMode mSeekMode;
180     int64_t mTargetTimeUs;
181     List<int64_t> mSampleDurations;
182     int64_t mDefaultSampleDurationUs;
183 
184     sp<Surface> initSurface();
185     status_t captureSurface();
186 };
187 
188 struct MediaImageDecoder : public FrameDecoder {
189    MediaImageDecoder(
190             const AString &componentName,
191             const sp<MetaData> &trackMeta,
192             const sp<IMediaSource> &source);
193 
194 protected:
195     virtual sp<AMessage> onGetFormatAndSeekOptions(
196             int64_t frameTimeUs,
197             int seekMode,
198             MediaSource::ReadOptions *options,
199             sp<Surface> *window) override;
200 
201     virtual status_t onExtractRect(FrameRect *rect) override;
202 
onInputReceivedMediaImageDecoder203     virtual status_t onInputReceived(uint8_t* __unused, size_t __unused,
204                                      MetaDataBase& sampleMeta __unused, bool firstSample __unused,
205                                      uint32_t* flags __unused) override { return OK; }
206 
207     virtual status_t onOutputReceived(
208             uint8_t* data,
209             sp<ABuffer> imgObj,
210             const sp<AMessage> &outputFormat,
211             int64_t timeUs,
212             bool *done) override;
213 
214 private:
215     VideoFrame *mFrame;
216     int32_t mWidth;
217     int32_t mHeight;
218     int32_t mGridRows;
219     int32_t mGridCols;
220     int32_t mTileWidth;
221     int32_t mTileHeight;
222     int32_t mTilesDecoded;
223     int32_t mTargetTiles;
224 };
225 
226 }  // namespace android
227 
228 #endif  // FRAME_DECODER_H_
229