Home | History | Annotate | Download | only in include
      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/MediaSource.h>
     26 #include <media/openmax/OMX_Video.h>
     27 #include <system/graphics-base.h>
     28 
     29 namespace android {
     30 
     31 struct AMessage;
     32 class MediaCodecBuffer;
     33 class IMediaSource;
     34 class VideoFrame;
     35 struct MediaCodec;
     36 
     37 struct FrameRect {
     38     int32_t left, top, right, bottom;
     39 };
     40 
     41 struct FrameDecoder : public RefBase {
     42     FrameDecoder(
     43             const AString &componentName,
     44             const sp<MetaData> &trackMeta,
     45             const sp<IMediaSource> &source);
     46 
     47     status_t init(
     48             int64_t frameTimeUs, size_t numFrames, int option, int colorFormat);
     49 
     50     sp<IMemory> extractFrame(FrameRect *rect = NULL);
     51 
     52     status_t extractFrames(std::vector<sp<IMemory> >* frames);
     53 
     54     static sp<IMemory> getMetadataOnly(
     55             const sp<MetaData> &trackMeta, int colorFormat, bool thumbnail = false);
     56 
     57 protected:
     58     virtual ~FrameDecoder();
     59 
     60     virtual sp<AMessage> onGetFormatAndSeekOptions(
     61             int64_t frameTimeUs,
     62             size_t numFrames,
     63             int seekMode,
     64             MediaSource::ReadOptions *options) = 0;
     65 
     66     virtual status_t onExtractRect(FrameRect *rect) = 0;
     67 
     68     virtual status_t onInputReceived(
     69             const sp<MediaCodecBuffer> &codecBuffer,
     70             MetaDataBase &sampleMeta,
     71             bool firstSample,
     72             uint32_t *flags) = 0;
     73 
     74     virtual status_t onOutputReceived(
     75             const sp<MediaCodecBuffer> &videoFrameBuffer,
     76             const sp<AMessage> &outputFormat,
     77             int64_t timeUs,
     78             bool *done) = 0;
     79 
     80     sp<MetaData> trackMeta()     const      { return mTrackMeta; }
     81     OMX_COLOR_FORMATTYPE dstFormat() const  { return mDstFormat; }
     82     int32_t dstBpp()             const      { return mDstBpp; }
     83 
     84     void addFrame(const sp<IMemory> &frame) {
     85         mFrames.push_back(frame);
     86     }
     87 
     88 private:
     89     AString mComponentName;
     90     sp<MetaData> mTrackMeta;
     91     sp<IMediaSource> mSource;
     92     OMX_COLOR_FORMATTYPE mDstFormat;
     93     int32_t mDstBpp;
     94     std::vector<sp<IMemory> > mFrames;
     95     MediaSource::ReadOptions mReadOptions;
     96     sp<MediaCodec> mDecoder;
     97     sp<AMessage> mOutputFormat;
     98     bool mHaveMoreInputs;
     99     bool mFirstSample;
    100 
    101     status_t extractInternal();
    102 
    103     DISALLOW_EVIL_CONSTRUCTORS(FrameDecoder);
    104 };
    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             size_t numFrames,
    116             int seekMode,
    117             MediaSource::ReadOptions *options) override;
    118 
    119     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     bool mIsAvcOrHevc;
    138     MediaSource::ReadOptions::SeekMode mSeekMode;
    139     int64_t mTargetTimeUs;
    140     size_t mNumFrames;
    141     size_t mNumFramesDecoded;
    142 };
    143 
    144 struct ImageDecoder : public FrameDecoder {
    145     ImageDecoder(
    146             const AString &componentName,
    147             const sp<MetaData> &trackMeta,
    148             const sp<IMediaSource> &source);
    149 
    150 protected:
    151     virtual sp<AMessage> onGetFormatAndSeekOptions(
    152             int64_t frameTimeUs,
    153             size_t numFrames,
    154             int seekMode,
    155             MediaSource::ReadOptions *options) override;
    156 
    157     virtual status_t onExtractRect(FrameRect *rect) override;
    158 
    159     virtual status_t onInputReceived(
    160             const sp<MediaCodecBuffer> &codecBuffer __unused,
    161             MetaDataBase &sampleMeta __unused,
    162             bool firstSample __unused,
    163             uint32_t *flags __unused) override { return OK; }
    164 
    165     virtual status_t onOutputReceived(
    166             const sp<MediaCodecBuffer> &videoFrameBuffer,
    167             const sp<AMessage> &outputFormat,
    168             int64_t timeUs,
    169             bool *done) override;
    170 
    171 private:
    172     VideoFrame *mFrame;
    173     int32_t mWidth;
    174     int32_t mHeight;
    175     int32_t mGridRows;
    176     int32_t mGridCols;
    177     int32_t mTileWidth;
    178     int32_t mTileHeight;
    179     int32_t mTilesDecoded;
    180     int32_t mTargetTiles;
    181 };
    182 
    183 }  // namespace android
    184 
    185 #endif  // FRAME_DECODER_H_
    186