-
Notifications
You must be signed in to change notification settings - Fork 3
/
gstmpp.c
507 lines (416 loc) · 14.1 KB
/
gstmpp.c
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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
/*
* Copyright 2017 Rockchip Electronics Co., Ltd
* Author: Randy Li <randy.li@rock-chips.com>
*
* Copyright 2021 Rockchip Electronics Co., Ltd
* Author: Jeffy Chen <jeffy.chen@rock-chips.com>
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Library General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library 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
* Library General Public License for more details.
*
* You should have received a copy of the GNU Library General Public
* License along with this library; if not, write to the
* Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
* Boston, MA 02110-1301, USA.
*
*/
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include <string.h>
#include <gst/allocators/gstdmabuf.h>
#include "gstmpp.h"
#include "gstmpph264enc.h"
#include "gstmpph265enc.h"
#include "gstmppvp8enc.h"
#include "gstmppjpegenc.h"
#include "gstmppjpegdec.h"
#include "gstmppvideodec.h"
#include "gstmppvpxalphadecodebin.h"
GST_DEBUG_CATEGORY_STATIC (mpp_debug);
#define GST_CAT_DEFAULT mpp_debug
struct gst_mpp_format
{
GstVideoFormat gst_format;
MppFrameFormat mpp_format;
#ifdef HAVE_RGA
RgaSURF_FORMAT rga_format;
#endif
gint pixel_stride0;
gboolean is_yuv;
};
#ifdef HAVE_RGA
#define GST_MPP_FORMAT(gst, mpp, rga, pixel_stride0, yuv) \
{ GST_VIDEO_FORMAT_ ## gst, MPP_FMT_ ## mpp, RK_FORMAT_ ## rga, \
pixel_stride0, yuv }
#else
#define GST_MPP_FORMAT(gst, mpp, rga, pixel_stride0, yuv) \
{ GST_VIDEO_FORMAT_ ## gst, MPP_FMT_ ## mpp, pixel_stride0, yuv}
#endif
struct gst_mpp_format gst_mpp_formats[] = {
GST_MPP_FORMAT (I420, YUV420P, YCbCr_420_P, 1, 1),
GST_MPP_FORMAT (YV12, BUTT, YCrCb_420_P, 1, 1),
GST_MPP_FORMAT (NV12, YUV420SP, YCbCr_420_SP, 1, 1),
GST_MPP_FORMAT (NV21, YUV420SP_VU, YCrCb_420_SP, 1, 1),
#ifdef HAVE_NV12_10LE40
GST_MPP_FORMAT (NV12_10LE40, YUV420SP_10BIT, YCbCr_420_SP_10B, 1, 1),
#else
GST_MPP_FORMAT (UNKNOWN, YUV420SP_10BIT, YCbCr_420_SP_10B, 1, 1),
#endif
#ifdef HAVE_NV16_10LE40
GST_MPP_FORMAT (NV16_10LE40, YUV422SP_10BIT, YCbCr_422_SP_10B, 1, 1),
#else
GST_MPP_FORMAT (UNKNOWN, YUV422SP_10BIT, YCbCr_422_SP_10B, 1, 1),
#endif
GST_MPP_FORMAT (Y42B, YUV422P, YCbCr_422_P, 1, 1),
GST_MPP_FORMAT (NV16, YUV422SP, YCbCr_422_SP, 1, 1),
GST_MPP_FORMAT (NV61, YUV422SP_VU, YCrCb_422_SP, 1, 1),
GST_MPP_FORMAT (NV24, YUV444SP, UNKNOWN, 1, 1),
GST_MPP_FORMAT (Y444, YUV444P, UNKNOWN, 1, 1),
GST_MPP_FORMAT (YUY2, YUV422_YUYV, UNKNOWN, 2, 1),
GST_MPP_FORMAT (YVYU, YUV422_YVYU, UNKNOWN, 2, 1),
GST_MPP_FORMAT (UYVY, YUV422_UYVY, UNKNOWN, 2, 1),
GST_MPP_FORMAT (VYUY, YUV422_VYUY, UNKNOWN, 2, 1),
GST_MPP_FORMAT (RGB16, RGB565LE, UNKNOWN, 2, 0),
GST_MPP_FORMAT (BGR16, BGR565LE, RGB_565, 2, 0),
GST_MPP_FORMAT (RGB, RGB888, RGB_888, 3, 0),
GST_MPP_FORMAT (BGR, BGR888, BGR_888, 3, 0),
GST_MPP_FORMAT (ARGB, ARGB8888, UNKNOWN, 4, 0),
GST_MPP_FORMAT (ABGR, ABGR8888, UNKNOWN, 4, 0),
GST_MPP_FORMAT (RGBA, RGBA8888, RGBA_8888, 4, 0),
GST_MPP_FORMAT (BGRA, BGRA8888, BGRA_8888, 4, 0),
GST_MPP_FORMAT (xRGB, ARGB8888, UNKNOWN, 4, 0),
GST_MPP_FORMAT (xBGR, ABGR8888, UNKNOWN, 4, 0),
GST_MPP_FORMAT (RGBx, RGBA8888, RGBX_8888, 4, 0),
GST_MPP_FORMAT (BGRx, BGRA8888, BGRX_8888, 4, 0),
};
#define GST_MPP_GET_FORMAT(type, format) ({ \
struct gst_mpp_format *_tmp; \
for (guint i = 0; i < ARRAY_SIZE (gst_mpp_formats) || (_tmp = NULL); i++) { \
_tmp = &gst_mpp_formats[i]; \
if (_tmp->type ## _format == (format)) break;\
}; _tmp; \
})
gboolean
gst_mpp_use_rga ()
{
static int mpp_use_rga = -1;
#ifdef HAVE_RGA
if (mpp_use_rga < 0) {
const gchar *buf = g_getenv ("GST_MPP_NO_RGA");
if (!buf || buf[0] == '0')
mpp_use_rga = 1;
else
mpp_use_rga = 0;
}
#endif
return mpp_use_rga > 0;
}
const gchar *
gst_mpp_video_format_to_string (GstVideoFormat format)
{
if (format == GST_VIDEO_FORMAT_UNKNOWN)
return "UNKNOWN";
return gst_video_format_to_string (format);
}
GstVideoFormat
gst_mpp_mpp_format_to_gst_format (MppFrameFormat mpp_format)
{
struct gst_mpp_format *format = GST_MPP_GET_FORMAT (mpp,
mpp_format & MPP_FRAME_FMT_MASK);
return format ? format->gst_format : GST_VIDEO_FORMAT_UNKNOWN;
}
MppFrameFormat
gst_mpp_gst_format_to_mpp_format (GstVideoFormat gst_format)
{
struct gst_mpp_format *format = GST_MPP_GET_FORMAT (gst, gst_format);
return format ? format->mpp_format : MPP_FMT_BUTT;
}
#ifdef HAVE_RGA
static RgaSURF_FORMAT
gst_mpp_mpp_format_to_rga_format (MppFrameFormat mpp_format)
{
struct gst_mpp_format *format = GST_MPP_GET_FORMAT (mpp,
mpp_format & MPP_FRAME_FMT_MASK);
return format ? format->rga_format : RK_FORMAT_UNKNOWN;
}
static RgaSURF_FORMAT
gst_mpp_gst_format_to_rga_format (GstVideoFormat gst_format)
{
struct gst_mpp_format *format = GST_MPP_GET_FORMAT (gst, gst_format);
return format ? format->rga_format : RK_FORMAT_UNKNOWN;
}
static gboolean
gst_mpp_set_rga_info (rga_info_t * info, RgaSURF_FORMAT rga_format,
guint width, guint height, guint hstride, guint vstride)
{
struct gst_mpp_format *format = GST_MPP_GET_FORMAT (rga, rga_format);
g_assert (format);
if (format->is_yuv) {
/* RGA requires yuv image rect align to 2 */
width &= ~1;
height &= ~1;
if (vstride % 2)
return FALSE;
}
if (info->fd < 0 && !info->virAddr)
return FALSE;
info->mmuFlag = 1;
rga_set_rect (&info->rect, 0, 0, width, height, hstride, vstride, rga_format);
return TRUE;
}
static gboolean
gst_mpp_rga_info_from_mpp_frame (rga_info_t * info, MppFrame mframe)
{
MppFrameFormat mpp_format = mpp_frame_get_fmt (mframe);
MppBuffer mbuf = mpp_frame_get_buffer (mframe);
guint width = mpp_frame_get_width (mframe);
guint height = mpp_frame_get_height (mframe);
guint hstride = mpp_frame_get_hor_stride_pixel (mframe);
guint vstride = mpp_frame_get_ver_stride (mframe);
RgaSURF_FORMAT rga_format = gst_mpp_mpp_format_to_rga_format (mpp_format);
g_return_val_if_fail (mbuf, FALSE);
info->fd = mpp_buffer_get_fd (mbuf);
return gst_mpp_set_rga_info (info, rga_format, width, height,
hstride, vstride);
}
static gboolean
gst_mpp_rga_info_from_video_info (rga_info_t * info, GstVideoInfo * vinfo)
{
GstVideoFormat format = GST_VIDEO_INFO_FORMAT (vinfo);
guint width = GST_VIDEO_INFO_WIDTH (vinfo);
guint height = GST_VIDEO_INFO_HEIGHT (vinfo);
guint hstride = gst_mpp_get_pixel_stride (vinfo);
guint vstride = GST_MPP_VIDEO_INFO_VSTRIDE (vinfo);
RgaSURF_FORMAT rga_format = gst_mpp_gst_format_to_rga_format (format);
return gst_mpp_set_rga_info (info, rga_format, width, height,
hstride, vstride);
}
static gboolean
gst_mpp_rga_do_convert (rga_info_t * src_info, rga_info_t * dst_info)
{
static gint rga_supported = 1;
static gint rga_inited = 0;
if (!rga_supported || !gst_mpp_use_rga ())
return FALSE;
if (!rga_inited) {
if (c_RkRgaInit () < 0) {
rga_supported = 0;
GST_WARNING ("failed to init RGA");
return FALSE;
}
rga_inited = 1;
}
if (c_RkRgaBlit (src_info, dst_info, NULL) < 0) {
GST_WARNING ("failed to blit");
return FALSE;
}
GST_DEBUG ("converted with RGA");
return TRUE;
}
static gint
gst_mpp_rga_get_rotation (gint rotation)
{
switch (rotation) {
case 0:
return 0;
case 90:
return HAL_TRANSFORM_ROT_90;
case 180:
return HAL_TRANSFORM_ROT_180;
case 270:
return HAL_TRANSFORM_ROT_270;
default:
return -1;
}
}
gboolean
gst_mpp_rga_convert (GstBuffer * inbuf, GstVideoInfo * src_vinfo,
GstMemory * out_mem, GstVideoInfo * dst_vinfo, gint rotation)
{
GstMapInfo mapinfo = { 0, };
gboolean ret;
rga_info_t src_info = { 0, };
rga_info_t dst_info = { 0, };
if (!gst_mpp_rga_info_from_video_info (&src_info, src_vinfo))
return FALSE;
if (!gst_mpp_rga_info_from_video_info (&dst_info, dst_vinfo))
return FALSE;
/* Prefer using dma fd */
if (gst_buffer_n_memory (inbuf) == 1) {
GstMemory *mem = gst_buffer_peek_memory (inbuf, 0);
gsize offset;
if (gst_is_dmabuf_memory (mem)) {
gst_memory_get_sizes (mem, &offset, NULL);
if (!offset)
src_info.fd = gst_dmabuf_memory_get_fd (mem);
}
}
if (src_info.fd <= 0) {
gst_buffer_map (inbuf, &mapinfo, GST_MAP_READ);
src_info.virAddr = mapinfo.data;
}
dst_info.fd = gst_dmabuf_memory_get_fd (out_mem);
src_info.rotation = gst_mpp_rga_get_rotation (rotation);
if (src_info.rotation < 0)
return FALSE;
ret = gst_mpp_rga_do_convert (&src_info, &dst_info);
gst_buffer_unmap (inbuf, &mapinfo);
return ret;
}
gboolean
gst_mpp_rga_convert_from_mpp_frame (MppFrame * mframe,
GstMemory * out_mem, GstVideoInfo * dst_vinfo, gint rotation)
{
rga_info_t src_info = { 0, };
rga_info_t dst_info = { 0, };
if (!gst_mpp_rga_info_from_mpp_frame (&src_info, mframe))
return FALSE;
if (!gst_mpp_rga_info_from_video_info (&dst_info, dst_vinfo))
return FALSE;
dst_info.fd = gst_dmabuf_memory_get_fd (out_mem);
src_info.rotation = gst_mpp_rga_get_rotation (rotation);
if (src_info.rotation < 0)
return FALSE;
return gst_mpp_rga_do_convert (&src_info, &dst_info);
}
#endif
void
gst_mpp_video_info_update_format (GstVideoInfo * info, GstVideoFormat format,
guint width, guint height)
{
GstCaps *caps;
const gchar *fmt = gst_mpp_video_format_to_string (format);
GST_DEBUG ("update format to %s (%dx%d)", fmt, width, height);
if (GST_VIDEO_INFO_FORMAT (info) == GST_VIDEO_FORMAT_UNKNOWN) {
gst_video_info_set_format (info, format, width, height);
return;
}
caps = gst_video_info_to_caps (info);
gst_caps_set_simple (caps, "format", G_TYPE_STRING, fmt,
"width", G_TYPE_INT, width, "height", G_TYPE_INT, height, NULL);
gst_video_info_from_caps (info, caps);
gst_caps_unref (caps);
}
gboolean
gst_mpp_video_info_align (GstVideoInfo * info, gint hstride, gint vstride)
{
GstVideoAlignment align;
guint stride;
guint i;
if (!hstride)
hstride = GST_MPP_ALIGN (GST_MPP_VIDEO_INFO_HSTRIDE (info));
if (!vstride)
vstride = GST_MPP_ALIGN (GST_MPP_VIDEO_INFO_VSTRIDE (info));
if (hstride == GST_MPP_VIDEO_INFO_HSTRIDE (info) &&
vstride == GST_MPP_VIDEO_INFO_VSTRIDE (info))
return TRUE;
GST_DEBUG ("aligning %dx%d to %dx%d", GST_VIDEO_INFO_WIDTH (info),
GST_VIDEO_INFO_HEIGHT (info), hstride, vstride);
gst_video_alignment_reset (&align);
/* Apply vstride */
align.padding_bottom = vstride - GST_VIDEO_INFO_HEIGHT (info);
if (!gst_video_info_align (info, &align))
return FALSE;
/* Apply vstride for single-plane */
if (GST_VIDEO_INFO_N_PLANES (info) == 1)
GST_VIDEO_INFO_SIZE (info) =
GST_VIDEO_INFO_PLANE_STRIDE (info, 0) * vstride;
if (GST_VIDEO_INFO_PLANE_STRIDE (info, 0) == hstride)
return TRUE;
/* Apply hstride */
stride = GST_VIDEO_INFO_PLANE_STRIDE (info, 0);
for (i = 0; i < GST_VIDEO_INFO_N_PLANES (info); i++) {
GST_VIDEO_INFO_PLANE_STRIDE (info, i) =
GST_VIDEO_INFO_PLANE_STRIDE (info, i) * hstride / stride;
GST_VIDEO_INFO_PLANE_OFFSET (info, i) =
GST_VIDEO_INFO_PLANE_OFFSET (info, i) / stride * hstride;
GST_DEBUG ("plane %d, stride %d, offset %" G_GSIZE_FORMAT, i,
GST_VIDEO_INFO_PLANE_STRIDE (info, i),
GST_VIDEO_INFO_PLANE_OFFSET (info, i));
}
GST_VIDEO_INFO_SIZE (info) = GST_VIDEO_INFO_SIZE (info) / stride * hstride;
GST_DEBUG ("aligned size %" G_GSIZE_FORMAT, GST_VIDEO_INFO_SIZE (info));
return TRUE;
}
gboolean
gst_mpp_video_info_matched (GstVideoInfo * info, GstVideoInfo * other)
{
guint i;
if (GST_VIDEO_INFO_FORMAT (info) != GST_VIDEO_INFO_FORMAT (other))
return FALSE;
if (GST_VIDEO_INFO_WIDTH (info) != GST_VIDEO_INFO_WIDTH (other))
return FALSE;
if (GST_VIDEO_INFO_HEIGHT (info) != GST_VIDEO_INFO_HEIGHT (other))
return FALSE;
for (i = 0; i < GST_VIDEO_INFO_N_PLANES (info); i++) {
if (GST_VIDEO_INFO_PLANE_STRIDE (info,
i) != GST_VIDEO_INFO_PLANE_STRIDE (other, i))
return FALSE;
if (GST_VIDEO_INFO_PLANE_OFFSET (info,
i) != GST_VIDEO_INFO_PLANE_OFFSET (other, i))
return FALSE;
}
return TRUE;
}
gboolean
gst_mpp_info_changed (GstVideoInfo * info, MppFrame * mframe)
{
MppFrameFormat mpp_format = mpp_frame_get_fmt (mframe);
gint width = mpp_frame_get_width (mframe);
gint height = mpp_frame_get_height (mframe);
gint hstride = mpp_frame_get_hor_stride (mframe);
gint vstride = mpp_frame_get_ver_stride (mframe);
GstVideoFormat format = gst_mpp_mpp_format_to_gst_format (mpp_format);
if (GST_VIDEO_INFO_FORMAT (info) != format)
return FALSE;
if (GST_VIDEO_INFO_WIDTH (info) != width)
return FALSE;
if (GST_VIDEO_INFO_HEIGHT (info) != height)
return FALSE;
if (GST_MPP_VIDEO_INFO_HSTRIDE (info) != hstride)
return FALSE;
if (GST_VIDEO_INFO_N_PLANES (info) == 1)
return TRUE;
if (GST_MPP_VIDEO_INFO_VSTRIDE (info) != vstride)
return FALSE;
return TRUE;
}
guint
gst_mpp_get_pixel_stride (GstVideoInfo * info)
{
GstVideoFormat gst_format = GST_VIDEO_INFO_FORMAT (info);
struct gst_mpp_format *format = GST_MPP_GET_FORMAT (gst, gst_format);
guint hstride = GST_MPP_VIDEO_INFO_HSTRIDE (info);
if (!format)
return hstride;
return hstride / format->pixel_stride0;
}
static gboolean
plugin_init (GstPlugin * plugin)
{
GST_DEBUG_CATEGORY_INIT (GST_CAT_DEFAULT, "mpp", 0, "MPP");
gst_mpp_h264_enc_register (plugin, GST_RANK_PRIMARY + 1);
gst_mpp_h265_enc_register (plugin, GST_RANK_PRIMARY + 1);
gst_mpp_vp8_enc_register (plugin, GST_RANK_PRIMARY + 1);
gst_mpp_jpeg_enc_register (plugin, GST_RANK_PRIMARY + 1);
gst_mpp_video_dec_register (plugin, GST_RANK_PRIMARY + 1);
gst_mpp_jpeg_dec_register (plugin, GST_RANK_PRIMARY + 1);
#ifdef USE_VPXALPHADEC
gst_mpp_vpx_alpha_decode_bin_register (plugin,
GST_RANK_PRIMARY + GST_MPP_ALPHA_DECODE_BIN_RANK_OFFSET);
#endif
return TRUE;
}
GST_PLUGIN_DEFINE (GST_VERSION_MAJOR,
GST_VERSION_MINOR,
rockchipmpp,
"Rockchip Mpp Video Plugin",
plugin_init, VERSION, GST_LICENSE, GST_PACKAGE_NAME, GST_PACKAGE_ORIGIN);