1 /*
2  *			GPAC - Multimedia Framework C SDK
3  *
4  *			Authors: Jean Le Feuvre
5  *			Copyright (c) Telecom ParisTech 2000-2012
6  *					All rights reserved
7  *
8  *  This file is part of GPAC / MP4 reader module
9  *
10  *  GPAC is free software; you can redistribute it and/or modify
11  *  it under the terms of the GNU Lesser General Public License as published by
12  *  the Free Software Foundation; either version 2, or (at your option)
13  *  any later version.
14  *
15  *  GPAC is distributed in the hope that it will be useful,
16  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
17  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  *  GNU Lesser General Public License for more details.
19  *
20  *  You should have received a copy of the GNU Lesser General Public
21  *  License along with this library; see the file COPYING.  If not, write to
22  *  the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
23  *
24  */
25 
26 
27 #ifndef __FFMPEG_IN_H
28 #define __FFMPEG_IN_H
29 
30 
31 
32 /*include net API*/
33 #include <gpac/modules/service.h>
34 /*include decoder API*/
35 #include <gpac/modules/codec.h>
36 #include <gpac/constants.h>
37 #include <gpac/thread.h>
38 
39 
40 //#define DISABLE_FFMPEG_DEMUX
41 
42 #if defined(WIN32) && !defined(__MINGW32__)
43 
44 #define EMULATE_INTTYPES
45 #define EMULATE_FAST_INT
46 #ifndef inline
47 #define inline __inline
48 #endif
49 
50 #if defined(__SYMBIAN32__)
51 #define EMULATE_INTTYPES
52 #endif
53 
54 
55 #ifndef __MINGW32__
56 #define __attribute__(s)
57 #endif
58 
59 #endif
60 
61 
62 /*include FFMPEG APIs*/
63 #ifdef _WIN32_WCE
64 #define inline	__inline
65 #endif
66 
67 
68 #if defined(WIN32)
69 #  define INT64_C(x)  (x ## i64)
70 #  define UINT64_C(x)  (x ## Ui64)
71 #endif
72 
73 
74 #ifdef FFMPEG_OLD_HEADERS
75 #include <ffmpeg/avformat.h>
76 #else
77 #include <libavformat/avformat.h>
78 #endif
79 
80 void gf_av_vlog(void* avcl, int level, const char *fmt, va_list vl);
81 
82 
83 #if LIBAVCODEC_VERSION_INT > AV_VERSION_INT(52, 0, 0)
84 #define FFMPEG_SWSCALE
85 #ifdef FFMPEG_OLD_HEADERS
86 #include <ffmpeg/swscale.h>
87 #else
88 #include <libswscale/swscale.h>
89 #endif
90 #endif
91 
92 
93 
94 #if LIBAVUTIL_VERSION_MAJOR<51
95 #define AVMEDIA_TYPE_AUDIO CODEC_TYPE_AUDIO
96 #define AVMEDIA_TYPE_VIDEO CODEC_TYPE_VIDEO
97 #endif
98 
99 
100 #ifndef FFMPEG_OLD_HEADERS
101 
102 #if ((LIBAVCODEC_VERSION_MAJOR == 52) && (LIBAVCODEC_VERSION_MINOR <= 20)) || (LIBAVCODEC_VERSION_MAJOR < 52)
103 #undef USE_AVCODEC2
104 #else
105 #define USE_AVCODEC2	1
106 #endif
107 
108 #else
109 #undef USE_AVCODEC2
110 #endif
111 
112 #if (LIBAVCODEC_VERSION_MAJOR >= 55)
113 #define USE_AVCTX3
114 #elif (LIBAVCODEC_VERSION_MAJOR >= 54) && (LIBAVCODEC_VERSION_MINOR >= 35)
115 #define USE_AVCTX3
116 #endif
117 
118 
119 /*FFMPEG decoder module */
120 typedef struct
121 {
122 	char szCodec[100];
123 	u32 out_size;
124 	u32 oti, st;
125 	u32 previous_par;
126 	Bool no_par_update;
127 	Bool needs_output_resize;
128 	Bool had_pic;
129 
130 	Bool check_short_header;
131 	u32 pix_fmt;
132 	u32 out_pix_fmt;
133 	Bool is_image;
134 
135 	u32 raw_pix_fmt;
136 	Bool flipped;
137 	u32 direct_output_mode;
138 
139 	u32 stride;
140 
141 	u32 output_cb_size;
142 	/*for audio packed frames*/
143 	u32 frame_start;
144 	char audio_buf[192000];
145 	Bool check_h264_isma;
146 
147 	Bool frame_size_changed;
148 
149 	u32 base_ES_ID;
150 	AVCodecContext *base_ctx;
151 	AVCodec *base_codec;
152 	AVFrame *base_frame;
153 #ifdef FFMPEG_SWSCALE
154 	struct SwsContext *base_sws;
155 #endif
156 
157 	u32 depth_ES_ID;
158 	u32 yuv_size;
159 	AVCodecContext *depth_ctx;
160 	AVCodec *depth_codec;
161 	AVFrame *depth_frame;
162 #ifdef FFMPEG_SWSCALE
163 	struct SwsContext *depth_sws;
164 #endif
165 
166 #ifdef USE_AVCTX3
167 	AVFrame *audio_frame;
168 #endif
169 
170 } FFDec;
171 
172 void *FFDEC_Load();
173 void FFDEC_Delete(void *ifce);
174 
175 
176 /*
177 		reader interface
178 
179 */
180 
181 //#define FFMPEG_IO_BUF_SIZE	16384
182 
183 //#define FFMPEG_DUMP_REMOTE
184 
185 #if LIBAVCODEC_VERSION_INT < AV_VERSION_INT(52, 113, 1)
186 #define USE_PRE_0_7 1
187 #endif
188 
189 typedef struct
190 {
191 	/*the service we're responsible for*/
192 	GF_ClientService *service;
193 
194 	/*input file*/
195 	AVFormatContext *ctx;
196 
197 	Bool seekable;
198 	Double seek_time;
199 
200 	s32 audio_st, video_st;
201 	/*app channels (only deal with 1 audio and one video for now)*/
202 	LPNETCHANNEL audio_ch;
203 	LPNETCHANNEL video_ch;
204 	Bool audio_run, video_run;
205 	AVRational audio_tscale, video_tscale;
206 	u32 data_buffer_ms;
207 
208 	/*demuxer thread - we cannot use direct fetching because of demultiplex structure of libavformat
209 	(reading one channel may lock the other)*/
210 	GF_Thread *thread;
211 	GF_Mutex *mx;
212 	u32 is_paused, is_running;
213 
214 	u32 service_type;
215 	Bool unreliable_audio_timing;
216 
217 	/*IO wrapper*/
218 	/*file downloader*/
219 	GF_DownloadSession *dnload;
220 
221 #ifdef USE_PRE_0_7
222 	ByteIOContext   io;
223 	void *options;
224 #else
225 	AVIOContext io;
226 	AVDictionary *options;
227 #endif
228 	char *buffer;
229 	u32 buffer_size;
230 
231 	u32 buffer_used;
232 
233 #ifdef FFMPEG_DUMP_REMOTE
234 	FILE *outdbg;
235 #endif
236 } FFDemux;
237 
238 void *New_FFMPEG_Demux();
239 void Delete_FFMPEG_Demux(void *ifce);
240 
241 
242 /*The DSI sent is:
243 
244 	u32 codec_id
245 
246 - for audio -
247 	u32 sample_rate: sampling rate or 0 if unknown
248 	u16 nb_channels: num channels or 0 if unknown
249 	u16 nb_bits_per_sample: nb bits or 0 if unknown
250 	u16 num_samples: num audio samples per frame or 0 if unknown
251 	u16 block_align: audio block align
252 
253 - for video -
254 	u32 width: video width or 0 if unknown;
255 	u32 height: video height or 0 if unknown;
256 
257 - for both -
258 	u32 codec_tag: ffmpeg ctx codec tag
259 	u32 bit_rate: ffmpeg ctx bit rate
260 
261 - till end of DSI bitstream-
262 	char *data: extra_data
263 */
264 
265 
266 /*TODO - we need to cleanup the ffmpeg code to align with only latest version and remove old compatibility code*/
267 
268 #if LIBAVCODEC_VERSION_INT >= AV_VERSION_INT(54, 25, 0 )
269 
270 #define CODEC_ID_SVQ3	AV_CODEC_ID_SVQ3
271 #define CODEC_ID_MPEG4	AV_CODEC_ID_MPEG4
272 #define CODEC_ID_H264	AV_CODEC_ID_H264
273 #define CODEC_ID_MPEG2VIDEO AV_CODEC_ID_MPEG2VIDEO
274 #define CODEC_ID_MJPEG	AV_CODEC_ID_MJPEG
275 #define CODEC_ID_MP2	AV_CODEC_ID_MP2
276 #define CODEC_ID_AC3	AV_CODEC_ID_AC3
277 #define CODEC_ID_EAC3	AV_CODEC_ID_EAC3
278 #define CODEC_ID_DVD_SUBTITLE	AV_CODEC_ID_DVD_SUBTITLE
279 #define CODEC_ID_RAWVIDEO	AV_CODEC_ID_RAWVIDEO
280 #define CODEC_ID_MJPEGB	AV_CODEC_ID_MJPEGB
281 #define CODEC_ID_LJPEG	AV_CODEC_ID_LJPEG
282 #define CODEC_ID_GIF	AV_CODEC_ID_GIF
283 #define CODEC_ID_H263	AV_CODEC_ID_H263
284 #define CODEC_ID_MP3	AV_CODEC_ID_MP3
285 #define CODEC_ID_AAC	AV_CODEC_ID_AAC
286 #define CODEC_ID_MPEG1VIDEO	AV_CODEC_ID_MPEG1VIDEO
287 #define CODEC_ID_PNG	AV_CODEC_ID_PNG
288 #define CODEC_ID_AMR_NB	AV_CODEC_ID_AMR_NB
289 #define CODEC_ID_AMR_WB	AV_CODEC_ID_AMR_WB
290 
291 #if LIBAVCODEC_VERSION_INT >= AV_VERSION_INT(54, 92, 100 )
292 #define CODEC_ID_VP9	AV_CODEC_ID_VP9
293 #endif
294 
295 #if LIBAVCODEC_VERSION_INT >= AV_VERSION_INT(57, 1, 100 )
296 #define CODEC_ID_OPUS	AV_CODEC_ID_OPUS
297 #endif
298 
299 #if LIBAVCODEC_VERSION_INT >= AV_VERSION_INT(58, 0, 0 )
300 #define CODEC_ID_AV1	AV_CODEC_ID_AV1
301 #endif
302 
303 #endif
304 
305 #if LIBAVUTIL_VERSION_INT >= AV_VERSION_INT(51, 42, 0)
306 #define PIX_FMT_YUV420P AV_PIX_FMT_YUV420P
307 #define PIX_FMT_YUV422P AV_PIX_FMT_YUV422P
308 #define PIX_FMT_YUV444P AV_PIX_FMT_YUV444P
309 #define PIX_FMT_YUV420P10LE AV_PIX_FMT_YUV420P10LE
310 #define PIX_FMT_YUV422P10LE AV_PIX_FMT_YUV422P10LE
311 #define PIX_FMT_YUV444P10LE AV_PIX_FMT_YUV444P10LE
312 #define PIX_FMT_BGR24 AV_PIX_FMT_BGR24
313 #define PIX_FMT_RGB24 AV_PIX_FMT_RGB24
314 #define PIX_FMT_RGBA AV_PIX_FMT_RGBA
315 #endif
316 
317 #if (LIBAVCODEC_VERSION_MAJOR>56)
318 #ifndef FF_API_AVFRAME_LAVC
319 #define FF_API_AVFRAME_LAVC
320 #endif
321 #endif
322 
323 
324 
325 #endif
326 
327