• 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 <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,
54             bool thumbnail = false, uint32_t bitDepth = 0);
55 
56 protected:
57     virtual ~FrameDecoder();
58 
59     virtual sp<AMessage> onGetFormatAndSeekOptions(
60             int64_t frameTimeUs,
61             int seekMode,
62             MediaSource::ReadOptions *options,
63             sp<Surface> *window) = 0;
64 
65     virtual status_t onExtractRect(FrameRect *rect) = 0;
66 
67     virtual status_t onInputReceived(
68             const sp<MediaCodecBuffer> &codecBuffer,
69             MetaDataBase &sampleMeta,
70             bool firstSample,
71             uint32_t *flags) = 0;
72 
73     virtual status_t onOutputReceived(
74             const sp<MediaCodecBuffer> &videoFrameBuffer,
75             const sp<AMessage> &outputFormat,
76             int64_t timeUs,
77             bool *done) = 0;
78 
trackMetaFrameDecoder79     sp<MetaData> trackMeta()     const      { return mTrackMeta; }
dstFormatFrameDecoder80     OMX_COLOR_FORMATTYPE dstFormat() const  { return mDstFormat; }
captureFormatFrameDecoder81     ui::PixelFormat captureFormat() const   { return mCaptureFormat; }
dstBppFrameDecoder82     int32_t dstBpp()             const      { return mDstBpp; }
setFrameFrameDecoder83     void setFrame(const sp<IMemory> &frameMem) { mFrameMemory = frameMem; }
84 
85 private:
86     AString mComponentName;
87     sp<MetaData> mTrackMeta;
88     sp<IMediaSource> mSource;
89     OMX_COLOR_FORMATTYPE mDstFormat;
90     ui::PixelFormat mCaptureFormat;
91     int32_t mDstBpp;
92     sp<IMemory> mFrameMemory;
93     MediaSource::ReadOptions mReadOptions;
94     sp<MediaCodec> mDecoder;
95     sp<AMessage> mOutputFormat;
96     bool mHaveMoreInputs;
97     bool mFirstSample;
98     sp<Surface> mSurface;
99 
100     status_t extractInternal();
101 
102     DISALLOW_EVIL_CONSTRUCTORS(FrameDecoder);
103 };
104 struct FrameCaptureLayer;
105 
106 struct VideoFrameDecoder : public FrameDecoder {
107     VideoFrameDecoder(
108             const AString &componentName,
109             const sp<MetaData> &trackMeta,
110             const sp<IMediaSource> &source);
111 
112 protected:
113     virtual sp<AMessage> onGetFormatAndSeekOptions(
114             int64_t frameTimeUs,
115             int seekMode,
116             MediaSource::ReadOptions *options,
117             sp<Surface> *window) override;
118 
onExtractRectVideoFrameDecoder119     virtual status_t onExtractRect(FrameRect *rect) override {
120         // Rect extraction for sequences is not supported for now.
121         return (rect == NULL) ? OK : ERROR_UNSUPPORTED;
122     }
123 
124     virtual status_t onInputReceived(
125             const sp<MediaCodecBuffer> &codecBuffer,
126             MetaDataBase &sampleMeta,
127             bool firstSample,
128             uint32_t *flags) override;
129 
130     virtual status_t onOutputReceived(
131             const sp<MediaCodecBuffer> &videoFrameBuffer,
132             const sp<AMessage> &outputFormat,
133             int64_t timeUs,
134             bool *done) override;
135 
136 private:
137     sp<FrameCaptureLayer> mCaptureLayer;
138     VideoFrame *mFrame;
139     bool mIsAvc;
140     bool mIsHevc;
141     MediaSource::ReadOptions::SeekMode mSeekMode;
142     int64_t mTargetTimeUs;
143     List<int64_t> mSampleDurations;
144     int64_t mDefaultSampleDurationUs;
145 
146     sp<Surface> initSurface();
147     status_t captureSurface();
148 };
149 
150 struct MediaImageDecoder : public FrameDecoder {
151    MediaImageDecoder(
152             const AString &componentName,
153             const sp<MetaData> &trackMeta,
154             const sp<IMediaSource> &source);
155 
156 protected:
157     virtual sp<AMessage> onGetFormatAndSeekOptions(
158             int64_t frameTimeUs,
159             int seekMode,
160             MediaSource::ReadOptions *options,
161             sp<Surface> *window) override;
162 
163     virtual status_t onExtractRect(FrameRect *rect) override;
164 
onInputReceivedMediaImageDecoder165     virtual status_t onInputReceived(
166             const sp<MediaCodecBuffer> &codecBuffer __unused,
167             MetaDataBase &sampleMeta __unused,
168             bool firstSample __unused,
169             uint32_t *flags __unused) override { return OK; }
170 
171     virtual status_t onOutputReceived(
172             const sp<MediaCodecBuffer> &videoFrameBuffer,
173             const sp<AMessage> &outputFormat,
174             int64_t timeUs,
175             bool *done) override;
176 
177 private:
178     VideoFrame *mFrame;
179     int32_t mWidth;
180     int32_t mHeight;
181     int32_t mGridRows;
182     int32_t mGridCols;
183     int32_t mTileWidth;
184     int32_t mTileHeight;
185     int32_t mTilesDecoded;
186     int32_t mTargetTiles;
187 };
188 
189 }  // namespace android
190 
191 #endif  // FRAME_DECODER_H_
192