msvideo.h 11.9 KB
Newer Older
aymeric's avatar
aymeric committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
/*
mediastreamer2 library - modular sound and video processing and streaming
Copyright (C) 2006  Simon MORLAT (simon.morlat@linphone.org)

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., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
*/

#ifndef msvideo_h
#define msvideo_h

Simon Morlat's avatar
Simon Morlat committed
23
#include <mediastreamer2/msfilter.h>
aymeric's avatar
aymeric committed
24 25

/* some global constants for video MSFilter(s) */
aymeric's avatar
aymeric committed
26 27
#define MS_VIDEO_SIZE_SQCIF_W 128
#define MS_VIDEO_SIZE_SQCIF_H 96
28

Aymeric Moizard's avatar
Aymeric Moizard committed
29 30
#define MS_VIDEO_SIZE_WQCIF_W 256
#define MS_VIDEO_SIZE_WQCIF_H 144
31

aymeric's avatar
aymeric committed
32 33
#define MS_VIDEO_SIZE_QCIF_W 176
#define MS_VIDEO_SIZE_QCIF_H 144
34

aymeric's avatar
aymeric committed
35 36
#define MS_VIDEO_SIZE_CIF_W 352
#define MS_VIDEO_SIZE_CIF_H 288
37 38 39 40

#define MS_VIDEO_SIZE_CVD_W 352
#define MS_VIDEO_SIZE_CVD_H 480

aymeric's avatar
aymeric committed
41 42
#define MS_VIDEO_SIZE_ICIF_W 352
#define MS_VIDEO_SIZE_ICIF_H 576
43

aymeric's avatar
aymeric committed
44 45
#define MS_VIDEO_SIZE_4CIF_W 704
#define MS_VIDEO_SIZE_4CIF_H 576
46

Aymeric Moizard's avatar
Aymeric Moizard committed
47 48
#define MS_VIDEO_SIZE_W4CIF_W 1024
#define MS_VIDEO_SIZE_W4CIF_H 576
aymeric's avatar
aymeric committed
49 50 51

#define MS_VIDEO_SIZE_QQVGA_W 160
#define MS_VIDEO_SIZE_QQVGA_H 120
52

53 54 55
#define MS_VIDEO_SIZE_HQVGA_W 160
#define MS_VIDEO_SIZE_HQVGA_H 240

aymeric's avatar
aymeric committed
56 57
#define MS_VIDEO_SIZE_QVGA_W 320
#define MS_VIDEO_SIZE_QVGA_H 240
58

59 60 61
#define MS_VIDEO_SIZE_HVGA_W 320
#define MS_VIDEO_SIZE_HVGA_H 480

aymeric's avatar
aymeric committed
62 63
#define MS_VIDEO_SIZE_VGA_W 640
#define MS_VIDEO_SIZE_VGA_H 480
64

aymeric's avatar
aymeric committed
65 66
#define MS_VIDEO_SIZE_SVGA_W 800
#define MS_VIDEO_SIZE_SVGA_H 600
aymeric's avatar
aymeric committed
67 68 69 70

#define MS_VIDEO_SIZE_NS1_W 324
#define MS_VIDEO_SIZE_NS1_H 248

aymeric's avatar
aymeric committed
71 72
#define MS_VIDEO_SIZE_QSIF_W 176
#define MS_VIDEO_SIZE_QSIF_H 120
73

aymeric's avatar
aymeric committed
74 75
#define MS_VIDEO_SIZE_SIF_W 352
#define MS_VIDEO_SIZE_SIF_H 240
76

77 78 79
#define MS_VIDEO_SIZE_IOS_MEDIUM_W 480
#define MS_VIDEO_SIZE_IOS_MEDIUM_H 360

aymeric's avatar
aymeric committed
80 81
#define MS_VIDEO_SIZE_ISIF_W 352
#define MS_VIDEO_SIZE_ISIF_H 480
82

aymeric's avatar
aymeric committed
83 84 85 86 87
#define MS_VIDEO_SIZE_4SIF_W 704
#define MS_VIDEO_SIZE_4SIF_H 480

#define MS_VIDEO_SIZE_288P_W 512
#define MS_VIDEO_SIZE_288P_H 288
88

Aymeric Moizard's avatar
Aymeric Moizard committed
89 90
#define MS_VIDEO_SIZE_432P_W 768
#define MS_VIDEO_SIZE_432P_H 432
91

aymeric's avatar
aymeric committed
92 93
#define MS_VIDEO_SIZE_448P_W 768
#define MS_VIDEO_SIZE_448P_H 448
94

Aymeric Moizard's avatar
Aymeric Moizard committed
95 96
#define MS_VIDEO_SIZE_480P_W 848
#define MS_VIDEO_SIZE_480P_H 480
97

aymeric's avatar
aymeric committed
98 99
#define MS_VIDEO_SIZE_576P_W 1024
#define MS_VIDEO_SIZE_576P_H 576
100

aymeric's avatar
aymeric committed
101 102
#define MS_VIDEO_SIZE_720P_W 1280
#define MS_VIDEO_SIZE_720P_H 720
103

aymeric's avatar
aymeric committed
104 105 106 107 108
#define MS_VIDEO_SIZE_1080P_W 1920
#define MS_VIDEO_SIZE_1080P_H 1080

#define MS_VIDEO_SIZE_SDTV_W 768
#define MS_VIDEO_SIZE_SDTV_H 576
109

aymeric's avatar
aymeric committed
110 111
#define MS_VIDEO_SIZE_HDTVP_W 1920
#define MS_VIDEO_SIZE_HDTVP_H 1200
112

113 114
#define MS_VIDEO_SIZE_XGA_W 1024
#define MS_VIDEO_SIZE_XGA_H 768
115

aymeric's avatar
aymeric committed
116 117
#define MS_VIDEO_SIZE_WXGA_W 1080
#define MS_VIDEO_SIZE_WXGA_H 768
smorlat's avatar
smorlat committed
118

119 120
#define MS_VIDEO_SIZE_MAX_W MS_VIDEO_SIZE_1024_W
#define MS_VIDEO_SIZE_MAX_H MS_VIDEO_SIZE_1024_H
aymeric's avatar
aymeric committed
121

122

smorlat's avatar
smorlat committed
123 124
/* those structs are part of the ABI: don't change their size otherwise binary plugins will be broken*/

aymeric's avatar
aymeric committed
125
typedef struct MSVideoSize{
smorlat's avatar
smorlat committed
126
	int width,height;
aymeric's avatar
aymeric committed
127 128 129 130 131 132 133 134 135
} MSVideoSize;

typedef struct MSRect{
	int x,y,w,h;
} MSRect;

#define MS_VIDEO_SIZE_CIF (MSVideoSize){MS_VIDEO_SIZE_CIF_W,MS_VIDEO_SIZE_CIF_H}
#define MS_VIDEO_SIZE_QCIF (MSVideoSize){MS_VIDEO_SIZE_QCIF_W,MS_VIDEO_SIZE_QCIF_H}
#define MS_VIDEO_SIZE_4CIF (MSVideoSize){MS_VIDEO_SIZE_4CIF_W,MS_VIDEO_SIZE_4CIF_H}
136
#define MS_VIDEO_SIZE_CVD (MSVideoSize){MS_VIDEO_SIZE_CVD_W, MS_VIDEO_SIZE_CVD_H}
aymeric's avatar
aymeric committed
137 138 139 140 141 142 143 144
#define MS_VIDEO_SIZE_QQVGA (MSVideoSize){MS_VIDEO_SIZE_QQVGA_W,MS_VIDEO_SIZE_QQVGA_H}
#define MS_VIDEO_SIZE_QVGA (MSVideoSize){MS_VIDEO_SIZE_QVGA_W,MS_VIDEO_SIZE_QVGA_H}
#define MS_VIDEO_SIZE_VGA (MSVideoSize){MS_VIDEO_SIZE_VGA_W,MS_VIDEO_SIZE_VGA_H}

#define MS_VIDEO_SIZE_720P (MSVideoSize){MS_VIDEO_SIZE_720P_W, MS_VIDEO_SIZE_720P_H}

#define MS_VIDEO_SIZE_NS1 (MSVideoSize){MS_VIDEO_SIZE_NS1_W,MS_VIDEO_SIZE_NS1_H}

145
#define MS_VIDEO_SIZE_XGA (MSVideoSize){MS_VIDEO_SIZE_XGA_W, MS_VIDEO_SIZE_XGA_H}
146

147
#define MS_VIDEO_SIZE_SVGA (MSVideoSize){MS_VIDEO_SIZE_SVGA_W, MS_VIDEO_SIZE_SVGA_H}
smorlat's avatar
smorlat committed
148

Simon Morlat's avatar
Simon Morlat committed
149 150 151 152 153 154 155 156 157 158 159
#ifdef _MSC_VER
#define MS_VIDEO_SIZE_ASSIGN(vsize,name) \
	{\
	(vsize).width=MS_VIDEO_SIZE_##name##_W; \
	(vsize).height=MS_VIDEO_SIZE_##name##_H; \
	}
#else
#define MS_VIDEO_SIZE_ASSIGN(vsize,name) \
	vsize=MS_VIDEO_SIZE_##name
#endif

160 161 162 163 164 165 166 167
/*deprecated: use MS_VIDEO_SIZE_SVGA*/
#define MS_VIDEO_SIZE_800X600_W MS_VIDEO_SIZE_SVGA_W
#define MS_VIDEO_SIZE_800X600_H MS_VIDEO_SIZE_SVGA_H
#define MS_VIDEO_SIZE_800X600 MS_VIDEO_SIZE_SVGA
/*deprecated use MS_VIDEO_SIZE_XGA*/
#define MS_VIDEO_SIZE_1024_W 1024
#define MS_VIDEO_SIZE_1024_H 768
#define MS_VIDEO_SIZE_1024 MS_VIDEO_SIZE_XGA
smorlat's avatar
smorlat committed
168

169 170 171 172 173 174 175
typedef enum{
	MS_NO_MIRROR,
	MS_HORIZONTAL_MIRROR, /*according to a vertical line in the center of buffer*/
	MS_CENTRAL_MIRROR, /*both*/
	MS_VERTICAL_MIRROR /*according to an horizontal line*/
}MSMirrorType;

Simon Morlat's avatar
Simon Morlat committed
176 177 178 179 180
typedef enum MSVideoOrientation{
	MS_VIDEO_LANDSCAPE = 0,
	MS_VIDEO_PORTRAIT =1
}MSVideoOrientation;

aymeric's avatar
aymeric committed
181 182 183 184
typedef enum{
	MS_YUV420P,
	MS_YUYV,
	MS_RGB24,
185
	MS_RGB24_REV, /*->microsoft down-top bitmaps */
aymeric's avatar
aymeric committed
186 187
	MS_MJPEG,
	MS_UYVY,
188
	MS_YUY2,   /* -> same as MS_YUYV */
Aymeric Moizard's avatar
Aymeric Moizard committed
189
	MS_RGBA32,
190
	MS_RGB565,
191
	MS_PIX_FMT_UNKNOWN
aymeric's avatar
aymeric committed
192 193 194 195 196 197 198 199 200 201 202 203 204 205
}MSPixFmt;

typedef struct _MSPicture{
	int w,h;
	uint8_t *planes[4]; /*we usually use 3 planes, 4th is for compatibility */
	int strides[4];	/*with ffmpeg's swscale.h */
}MSPicture;

typedef struct _MSPicture YuvBuf; /*for backward compatibility*/

#ifdef __cplusplus
extern "C"{
#endif

Simon Morlat's avatar
Simon Morlat committed
206 207 208 209 210 211 212 213
MS2_PUBLIC int ms_pix_fmt_to_ffmpeg(MSPixFmt fmt);
MS2_PUBLIC MSPixFmt ffmpeg_pix_fmt_to_ms(int fmt);
MS2_PUBLIC MSPixFmt ms_fourcc_to_pix_fmt(uint32_t fourcc);
MS2_PUBLIC void ms_ffmpeg_check_init(void);
MS2_PUBLIC int ms_yuv_buf_init_from_mblk(MSPicture *buf, mblk_t *m);
MS2_PUBLIC int ms_yuv_buf_init_from_mblk_with_size(MSPicture *buf, mblk_t *m, int w, int h);
MS2_PUBLIC int ms_picture_init_from_mblk_with_size(MSPicture *buf, mblk_t *m, MSPixFmt fmt, int w, int h);
MS2_PUBLIC mblk_t * ms_yuv_buf_alloc(MSPicture *buf, int w, int h);
214
MS2_PUBLIC mblk_t * ms_yuv_buf_alloc_from_buffer(int w, int h, mblk_t* buffer);
215
MS2_PUBLIC void ms_yuv_buf_copy(uint8_t *src_planes[], const int src_strides[],
aymeric's avatar
aymeric committed
216
		uint8_t *dst_planes[], const int dst_strides[3], MSVideoSize roi);
Simon Morlat's avatar
Simon Morlat committed
217
MS2_PUBLIC void ms_yuv_buf_mirror(YuvBuf *buf);
218
MS2_PUBLIC void ms_yuv_buf_mirrors(YuvBuf *buf,const MSMirrorType type);
Simon Morlat's avatar
Simon Morlat committed
219 220 221
MS2_PUBLIC void rgb24_mirror(uint8_t *buf, int w, int h, int linesize);
MS2_PUBLIC void rgb24_revert(uint8_t *buf, int w, int h, int linesize);
MS2_PUBLIC void rgb24_copy_revert(uint8_t *dstbuf, int dstlsz,
222
				const uint8_t *srcbuf, int srclsz, MSVideoSize roi);
smorlat's avatar
smorlat committed
223

Simon Morlat's avatar
Simon Morlat committed
224
MS2_PUBLIC void ms_rgb_to_yuv(const uint8_t rgb[3], uint8_t yuv[3]);
225

226 227 228 229 230 231

#ifdef __arm__
MS2_PUBLIC void rotate_plane_neon_clockwise(int wDest, int hDest, int full_width, uint8_t* src, uint8_t* dst);
MS2_PUBLIC void rotate_plane_neon_anticlockwise(int wDest, int hDest, int full_width, uint8_t* src, uint8_t* dst);
MS2_PUBLIC void rotate_cbcr_to_cr_cb(int wDest, int hDest, int full_width, uint8_t* cbcr_src, uint8_t* cr_dst, uint8_t* cb_dst,bool_t clockWise);
MS2_PUBLIC void deinterlace_and_rotate_180_neon(uint8_t* ysrc, uint8_t* cbcrsrc, uint8_t* ydst, uint8_t* udst, uint8_t* vdst, int w, int h, int y_byte_per_row,int cbcr_byte_per_row);
232 233 234
void deinterlace_down_scale_and_rotate_180_neon(uint8_t* ysrc, uint8_t* cbcrsrc, uint8_t* ydst, uint8_t* udst, uint8_t* vdst, int w, int h, int y_byte_per_row,int cbcr_byte_per_row,bool_t down_scale);
	void deinterlace_down_scale_neon(uint8_t* ysrc, uint8_t* cbcrsrc, uint8_t* ydst, uint8_t* u_dst, uint8_t* v_dst, int w, int h, int y_byte_per_row,int cbcr_byte_per_row,bool_t down_scale);
	mblk_t *copy_ycbcrbiplanar_to_true_yuv_with_rotation_and_down_scale_by_2(uint8_t* y, uint8_t * cbcr, int rotation, int w, int h, int y_byte_per_row,int cbcr_byte_per_row, bool_t uFirstvSecond, bool_t down_scale);
235 236
#endif

smorlat's avatar
smorlat committed
237 238 239 240
static inline bool_t ms_video_size_greater_than(MSVideoSize vs1, MSVideoSize vs2){
	return (vs1.width>=vs2.width) && (vs1.height>=vs2.height);
}

Simon Morlat's avatar
Simon Morlat committed
241 242 243 244
static inline bool_t ms_video_size_area_greater_than(MSVideoSize vs1, MSVideoSize vs2){
	return (vs1.width*vs1.height >= vs2.width*vs2.height);
}

smorlat's avatar
smorlat committed
245 246 247 248 249 250 251 252
static inline MSVideoSize ms_video_size_max(MSVideoSize vs1, MSVideoSize vs2){
	return ms_video_size_greater_than(vs1,vs2) ? vs1 : vs2;
}

static inline MSVideoSize ms_video_size_min(MSVideoSize vs1, MSVideoSize vs2){
	return ms_video_size_greater_than(vs1,vs2) ? vs2 : vs1;
}

Simon Morlat's avatar
Simon Morlat committed
253 254 255 256 257 258 259 260
static inline MSVideoSize ms_video_size_area_max(MSVideoSize vs1, MSVideoSize vs2){
	return ms_video_size_area_greater_than(vs1,vs2) ? vs1 : vs2;
}

static inline MSVideoSize ms_video_size_area_min(MSVideoSize vs1, MSVideoSize vs2){
	return ms_video_size_area_greater_than(vs1,vs2) ? vs2 : vs1;
}

smorlat's avatar
smorlat committed
261 262 263 264
static inline bool_t ms_video_size_equal(MSVideoSize vs1, MSVideoSize vs2){
	return vs1.width==vs2.width && vs1.height==vs2.height;
}

Simon Morlat's avatar
Simon Morlat committed
265
MS2_PUBLIC MSVideoSize ms_video_size_get_just_lower_than(MSVideoSize vs);
smorlat's avatar
smorlat committed
266

Simon Morlat's avatar
Simon Morlat committed
267 268 269 270 271 272 273 274 275 276 277 278
static inline MSVideoOrientation ms_video_size_get_orientation(MSVideoSize vs){
	return vs.width>=vs.height ? MS_VIDEO_LANDSCAPE : MS_VIDEO_PORTRAIT;
}

static inline MSVideoSize ms_video_size_change_orientation(MSVideoSize vs, MSVideoOrientation o){
	MSVideoSize ret;
	if (o!=ms_video_size_get_orientation(vs)){
		ret.width=vs.height;
		ret.height=vs.width;
	}else ret=vs;
	return ret;
}
279

280 281 282 283 284 285 286 287 288 289 290 291
/* abstraction for image scaling and color space conversion routines*/

typedef struct _MSScalerContext MSScalerContext;

#define MS_SCALER_METHOD_NEIGHBOUR 1
#define MS_SCALER_METHOD_BILINEAR (1<<1)

struct _MSScalerDesc {
	MSScalerContext * (*create_context)(int src_w, int src_h, MSPixFmt src_fmt,
                                         int dst_w, int dst_h, MSPixFmt dst_fmt, int flags);
	int (*context_process)(MSScalerContext *ctx, uint8_t *src[], int src_strides[], uint8_t *dst[], int dst_strides[]);
	void (*context_free)(MSScalerContext *ctx);
292 293
};

294 295
typedef struct  _MSScalerDesc MSScalerDesc;

Simon Morlat's avatar
Simon Morlat committed
296
MS2_PUBLIC MSScalerContext *ms_scaler_create_context(int src_w, int src_h, MSPixFmt src_fmt,
297 298
                                          int dst_w, int dst_h, MSPixFmt dst_fmt, int flags);

Simon Morlat's avatar
Simon Morlat committed
299
MS2_PUBLIC int ms_scaler_process(MSScalerContext *ctx, uint8_t *src[], int src_strides[], uint8_t *dst[], int dst_strides[]);
300

Simon Morlat's avatar
Simon Morlat committed
301
MS2_PUBLIC void ms_scaler_context_free(MSScalerContext *ctx);
302

Simon Morlat's avatar
Simon Morlat committed
303
MS2_PUBLIC void ms_video_set_scaler_impl(MSScalerDesc *desc);
304

305
MS2_PUBLIC mblk_t *copy_ycbcrbiplanar_to_true_yuv_with_rotation(uint8_t* y, uint8_t* cbcr, int rotation, int w, int h, int y_byte_per_row,int cbcr_byte_per_row, bool_t uFirstvSecond);
aymeric's avatar
aymeric committed
306

307 308
/*** Encoder Helpers ***/
/* Frame rate controller */
309 310 311 312 313 314 315 316
struct _MSFrameRateController {
	unsigned int start_time;
	int th_frame_count;
	float fps;
};
typedef struct _MSFrameRateController MSFrameRateController;
MS2_PUBLIC void ms_video_init_framerate_controller(MSFrameRateController* ctrl, float fps);
MS2_PUBLIC bool_t ms_video_capture_new_frame(MSFrameRateController* ctrl, uint32_t current_time);
317

318 319 320 321
/* Average FPS calculator */
struct _MSAverageFPS {
	unsigned int last_frame_time, last_print_time;
	float mean_inter_frame;
322
	const char* context;
323 324
};
typedef struct _MSAverageFPS MSAverageFPS;
325 326
MS2_PUBLIC void ms_video_init_average_fps(MSAverageFPS* afps, const char* context);
MS2_PUBLIC bool_t ms_video_update_average_fps(MSAverageFPS* afps, uint32_t current_time);
327

328 329 330 331 332 333 334 335 336 337 338 339 340 341
#ifdef __cplusplus
}
#endif

#define MS_FILTER_SET_VIDEO_SIZE	MS_FILTER_BASE_METHOD(100,MSVideoSize)
#define MS_FILTER_GET_VIDEO_SIZE	MS_FILTER_BASE_METHOD(101,MSVideoSize)

#define MS_FILTER_SET_PIX_FMT		MS_FILTER_BASE_METHOD(102,MSPixFmt)
#define MS_FILTER_GET_PIX_FMT		MS_FILTER_BASE_METHOD(103,MSPixFmt)

#define MS_FILTER_SET_FPS		MS_FILTER_BASE_METHOD(104,float)
#define MS_FILTER_GET_FPS		MS_FILTER_BASE_METHOD(105,float)

/* request a video-fast-update (=I frame for H263,MP4V-ES) to a video encoder*/
342
/* DEPRECATED: Use MS_VIDEO_ENCODER_REQ_VFU instead */
343 344
#define MS_FILTER_REQ_VFU		MS_FILTER_BASE_METHOD_NO_ARG(106)

aymeric's avatar
aymeric committed
345
#endif