media-codec-decoder.h 3.05 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
/*
 Mediastreamer2 media-codec-decoder.h
 Copyright (C) 2018 Belledonne Communications SARL

 This program is free software; you can redistribute it and/or
 modify it under the terms of the GNU General Public License
 as published by the Free Software Foundation; either version 2
 of the License, or (at your option) any later version.

 This program is distributed in the hope that it will be useful,
 but WITHOUT ANY WARRANTY; without even the implied warranty of
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 GNU General Public License for more details.

 You should have received a copy of the GNU General Public License
 along with this program; if not, write to the Free Software
 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 */

#pragma once

#include <cstdint>
23
#include <list>
24 25 26 27 28 29 30 31 32 33 34 35
#include <memory>
#include <string>
#include <vector>

#include <media/NdkMediaCodec.h>

#include "mediastreamer2/msvideo.h"

#include "nal-unpacker.h"

namespace mediastreamer {

36 37 38 39 40
class MediaCodecDecoder {
public:
	MediaCodecDecoder(const std::string &mime);
	~MediaCodecDecoder();

41
	void setParameterSets(MSQueue *paramterSets);
42
	void waitForKeyFrame() {_needKeyFrame = true;}
43 44
	void flush();

45
	bool feed(MSQueue *encodedFrame, uint64_t timestamp);
46 47 48
	mblk_t *fetch();

private:
49 50 51 52 53
	enum class State {
		Reset,
		Ready
	};

54 55 56 57 58 59 60 61 62
	class BufferFlag {
	public:
		static const uint32_t None = 0;
		static const uint32_t KeyFrame = 1;
		static const uint32_t CodecConfig = 1<<1;
		static const uint32_t EndOfStream = 1<<2;
		static const uint32_t PartialFrame = 1<<3;
	};

63
	void createImpl(const std::string &mime);
64 65
	bool feed(MSQueue *encodedFrame, uint64_t timestamp, bool isPs);
	bool isKeyFrame(const MSQueue *frame) const;
66 67
	void setState(State state);
	static const char *toString(State state);
68 69 70 71 72

	AMediaCodec *_impl = nullptr;
	int _pendingFrames = 0;
	MSVideoSize _vsize;
	MSYuvBufAllocator *_bufAllocator = nullptr;
73
	uint64_t _lastTs = 0;
74
	State _state = State::Reset;
75
	std::vector<uint8_t> _bitstream;
76 77
	std::unique_ptr<H26xNaluHeader> _naluHeader;
	bool _needKeyFrame = true;
78 79 80 81

	static const unsigned int _timeoutUs = 0;
};

82 83
class MediaCodecDecoderFilterImpl {
public:
84
	MediaCodecDecoderFilterImpl(MSFilter *f, const std::string &mime);
85
	virtual ~MediaCodecDecoderFilterImpl() = default;
86 87 88 89 90 91 92 93

	void preprocess();
	void process();
	void postprocess();

	MSVideoSize getVideoSize() const;
	float getFps() const;
	const MSFmtDescriptor *getOutFmt() const;
94
	void addFmtp(const char *fmtp) {}
95 96 97 98 99 100

	void enableAvpf(bool enable);
	void enableFreezeOnError(bool enable);
	void resetFirstImage();

protected:
101
	void extractParameterSets(MSQueue *frame, MSQueue *paramterSets);
102 103
	virtual bool isKeyFrame(const MSQueue *frame) const = 0;

104 105 106 107 108 109 110
	MSVideoSize _vsize;
	MSAverageFPS _fps;
	bool _avpfEnabled = false;
	bool _freezeOnError = true;

	MSFilter *_f = nullptr;
	std::unique_ptr<NalUnpacker> _unpacker;
111
	std::unique_ptr<H26xParameterSetsStore> _psStore;
112
	std::unique_ptr<H26xNaluHeader> _naluHeader;
113
	MediaCodecDecoder _codec;
114 115
	bool _firstImageDecoded = false;

116

117 118 119 120
	static const unsigned int _timeoutUs = 0;
};

}