aboutsummaryrefslogtreecommitdiffstats
path: root/mediaplugin/src/plugins/media/ffmpeg/ffmpeg_core.h
blob: e55b6a963ca25d8da28c0b1a6fccfeb874fd938a (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
/* UltraStar Deluxe - Karaoke Game
 *
 * UltraStar Deluxe is the legal property of its developers, whose names
 * are too numerous to list here. Please refer to the COPYRIGHT
 * file distributed with this source distribution.
 *
 * 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; see the file COPYING. If not, write to
 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
 * Boston, MA 02110-1301, USA.
 *
 * $URL$
 * $Id$
 */
#ifndef _FFMPEG_CORE_H_
#define _FFMPEG_CORE_H_

#ifndef __STDC_CONSTANT_MACROS
#define __STDC_CONSTANT_MACROS
#endif
#include <inttypes.h>
#include <string>
extern "C" {
#include <libavcodec/avcodec.h>
#include <libavformat/avformat.h>
#include <libavformat/avio.h>
#include <libavutil/avutil.h>
#include <libavutil/mathematics.h>
#include <libavutil/rational.h> // used for av_rescale_q
#include <libswscale/swscale.h>
}
#include "core/util.h"

class PacketQueue {
private:
	AVPacketList *_firstListEntry;
	AVPacketList *_lastListEntry;
	int _packetCount;
	Mutex _mutex;
	Condition _condition;
	int _size;
	bool _abortRequest;
public:
	PacketQueue();
	~PacketQueue();

	int put(AVPacket *packet);
	int putStatus(int statusFlag, void *statusInfo);
	void freeStatusInfo(AVPacket *packet);
	void* getStatusInfo(AVPacket *packet);
	int get(AVPacket *packet, bool blocking);
	int getSize();
	void flush();
	void abort();
	bool isAborted();
};

extern const uint8_t* STATUS_PACKET;
enum {
	PKT_STATUS_FLAG_EOF = 1, // signal end-of-file
	PKT_STATUS_FLAG_FLUSH, // request the decoder to flush its avcodec decode buffers
	PKT_STATUS_FLAG_ERROR, // signal an error state
	PKT_STATUS_FLAG_EMPTY  // request the decoder to output empty data (silence or black frames)
};

class MediaCore_FFmpeg;

// Note: singleton pattern does not work here as static vars
// are initialized before the plugin itself is initialized.
extern MediaCore_FFmpeg *ffmpegCore;

class MediaCore_FFmpeg {
private:
	URLProtocol utf8FileProtocol;
	Mutex _codecLock;

	std::string hexVerToStr(unsigned version);
	void checkVersions();
	void registerUTF8FileProtocol();
public:
	MediaCore_FFmpeg();
	~MediaCore_FFmpeg();

	std::string getErrorString(int errorNum) const;
	bool findStreamIDs(AVFormatContext *formatCtx, int *firstVideoStream, int *firstAudioStream) const;
	int findAudioStreamIndex(AVFormatContext *formatCtx) const;
	bool convertFFmpegToAudioFormat(SampleFormat ffmpegFormat, audioSampleFormat_t *format) const;

public:
	class AVCodecLock {
	public:
		AVCodecLock() {
			ffmpegCore->_codecLock.lock();
		}

		~AVCodecLock() {
			ffmpegCore->_codecLock.unlock();
		}
	};
};

// FFmpeg compatibility with older versions

#if LIBAVCODEC_VERSION_INT < AV_VERSION_INT(52,64,0)
#define AVMEDIA_TYPE_VIDEO CODEC_TYPE_VIDEO
#define AVMEDIA_TYPE_AUDIO CODEC_TYPE_AUDIO
#endif

#if LIBAVCODEC_VERSION_INT < AV_VERSION_INT(52,26,0)
#define avcodec_decode_video2(avctx, picture, got_picture_ptr, avpkt) \
	avcodec_decode_video((avctx), (picture), (got_picture_ptr), \
			(avpkt)->data, (avpkt)->size)
#endif

#if LIBAVCODEC_VERSION_INT < AV_VERSION_INT(51,30,0)
#define avcodec_decode_audio3(avctx, samples, frame_size_ptr, avpkt) \
	avcodec_decode_audio((avctx), (samples), (frame_size_ptr), \
			(avpkt)->data, (avpkt)->size)
#elif LIBAVCODEC_VERSION_INT < AV_VERSION_INT(52,26,0)
#define avcodec_decode_audio3(avctx, samples, frame_size_ptr, avpkt) \
	avcodec_decode_audio2((avctx), (samples), (frame_size_ptr), \
			(avpkt)->data, (avpkt)->size)
#endif

#if LIBAVFORMAT_VERSION_INT < AV_VERSION_INT(52,69,0)
#define av_register_protocol2(prot, size) \
	av_register_protocol(prot)
#endif

#endif /* _FFMPEG_CORE_H_ */