1 // Copyright (C) 2019 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 #pragma once
16 
17 #include "host-common/MediaSnapshotState.h"
18 #include "host-common/MediaVideoHelper.h"
19 #include "host-common/YuvConverter.h"
20 
21 #include <cstdint>
22 #include <list>
23 #include <mutex>
24 #include <string>
25 #include <vector>
26 
27 extern "C" {
28 #include <libavcodec/avcodec.h>   // for AVCodecContext, AVPacket
29 #include <libavformat/avio.h>     // for avio_open, AVIO_FLAG_...
30 #include <libavutil/avutil.h>     // for AVMediaType, AVMEDIA_...
31 #include <libavutil/dict.h>       // for AVDictionary
32 #include <libavutil/error.h>      // for av_make_error_string
33 #include <libavutil/frame.h>      // for AVFrame, av_frame_alloc
34 #include <libavutil/log.h>        // for av_log_set_callback
35 #include <libavutil/pixfmt.h>     // for AVPixelFormat, AV_PIX...
36 #include <libavutil/rational.h>   // for AVRational
37 #include <libavutil/samplefmt.h>  // for AVSampleFormat
38 #include <libavutil/timestamp.h>
39 }
40 
41 #include <stdio.h>
42 #include <string.h>
43 
44 #include <stddef.h>
45 
46 namespace android {
47 namespace emulation {
48 
49 class MediaFfmpegVideoHelper : public MediaVideoHelper {
50 public:
51     MediaFfmpegVideoHelper(int type, int threads);
52     ~MediaFfmpegVideoHelper() override;
53 
54     // return true if success; false otherwise
55     bool init() override;
56     void decode(const uint8_t* frame,
57                 size_t szBytes,
58                 uint64_t inputPts) override;
59     void flush() override;
60     void deInit() override;
61 
62     // this is special helper function, mostly used by apple vtb
63     // to reorder the output frames
64     int frameReorderBufferSize() const;
65 private:
66     std::vector<uint8_t> mDecodedFrame;
67 
68     int mType = 0;
69     int mThreadCount = 1;
70 
71     // ffmpeg stuff
72     AVCodec* mCodec = nullptr;
73     AVCodecContext* mCodecCtx = nullptr;
74     AVFrame* mFrame = nullptr;
75     AVPacket mPacket;
76 
77     void copyFrame();
78     void fetchAllFrames();
79 };  // MediaFfmpegVideoHelper
80 
81 }  // namespace emulation
82 }  // namespace android
83