forked from felipec/gst-dsp
/
gstdspvenc.c
699 lines (580 loc) · 16.8 KB
/
gstdspvenc.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
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
/*
* Copyright (C) 2009-2010 Felipe Contreras
*
* Authors:
* Felipe Contreras <felipe.contreras@gmail.com>
* Juha Alanen <juha.m.alanen@nokia.com>
*
* This file may be used under the terms of the GNU Lesser General Public
* License version 2.1, a copy of which is found in LICENSE included in the
* packaging of this file.
*/
#include "gstdspvenc.h"
#include "plugin.h"
#include "util.h"
#include "dsp_bridge.h"
#include <string.h> /* for memcpy */
#include "log.h"
#define GST_CAT_DEFAULT gstdsp_debug
static GstDspBaseClass *parent_class;
enum {
ARG_0,
ARG_BITRATE,
ARG_MODE,
ARG_KEYFRAME_INTERVAL,
ARG_MAX_BITRATE,
ARG_INTRA_REFRESH,
};
#define DEFAULT_BITRATE 0
#define DEFAULT_MAX_BITRATE 0
#define DEFAULT_MODE 0
#define DEFAULT_KEYFRAME_INTERVAL 1
#define DEFAULT_INTRA_REFRESH (DEFAULT_MODE == 1)
#define GST_TYPE_DSPVENC_MODE gst_dspvenc_mode_get_type()
static GType
gst_dspvenc_mode_get_type(void)
{
static GType gst_dspvenc_mode_type;
if (!gst_dspvenc_mode_type) {
static GEnumValue modes[] = {
{0, "Storage", "storage"},
{1, "Streaming", "streaming"},
{0, NULL, NULL},
};
gst_dspvenc_mode_type = g_enum_register_static("GstDspVEncMode", modes);
}
return gst_dspvenc_mode_type;
}
static inline GstCaps *
generate_sink_template(void)
{
GstCaps *caps;
GstStructure *struc;
caps = gst_caps_new_empty();
struc = gst_structure_new("video/x-raw-yuv",
"format", GST_TYPE_FOURCC, GST_MAKE_FOURCC('U', 'Y', 'V', 'Y'),
NULL);
gst_caps_append_structure(caps, struc);
struc = gst_structure_new("video/x-raw-yuv",
"format", GST_TYPE_FOURCC, GST_MAKE_FOURCC('I', '4', '2', '0'),
NULL);
gst_caps_append_structure(caps, struc);
return caps;
}
static inline void *
create_node(GstDspVEnc *self)
{
GstDspBase *base;
int dsp_handle;
struct dsp_node *node;
struct td_codec *codec;
const struct dsp_uuid usn_uuid = { 0x79A3C8B3, 0x95F2, 0x403F, 0x9A, 0x4B,
{ 0xCF, 0x80, 0x57, 0x73, 0x05, 0x41 } };
const struct dsp_uuid conversions_uuid = { 0x722DD0DA, 0xF532, 0x4238, 0xB8, 0x46,
{ 0xAB, 0xFF, 0x5D, 0xA4, 0xBA, 0x02 } };
base = GST_DSP_BASE(self);
dsp_handle = base->dsp_handle;
if (!gstdsp_register(dsp_handle, &usn_uuid, DSP_DCD_LIBRARYTYPE, "usn.dll64P")) {
pr_err(self, "failed to register usn node library");
return NULL;
}
codec = base->codec;
if (!codec) {
pr_err(self, "unknown algorithm");
return NULL;
}
if (!gstdsp_register(dsp_handle, codec->uuid, DSP_DCD_LIBRARYTYPE, codec->filename)) {
pr_err(self, "failed to register algo node library");
return NULL;
}
if (!gstdsp_register(dsp_handle, codec->uuid, DSP_DCD_NODETYPE, codec->filename)) {
pr_err(self, "failed to register algo node");
return NULL;
}
/* only needed for jpegenc */
if (base->alg == GSTDSP_JPEGENC) {
/* SN_API == 0 doesn't have it, so don't fail */
(void) gstdsp_register(dsp_handle, &conversions_uuid, DSP_DCD_LIBRARYTYPE, "conversions.dll64P");
}
{
struct dsp_node_attr_in attrs = {
.cb = sizeof(attrs),
.priority = 5,
.timeout = 1000,
};
void *arg_data;
codec->create_args(base, &attrs.profile_id, &arg_data);
if (!arg_data)
return NULL;
if (!dsp_node_allocate(dsp_handle, base->proc, codec->uuid, arg_data, &attrs, &node)) {
pr_err(self, "dsp node allocate failed");
free(arg_data);
return NULL;
}
free(arg_data);
}
if (!dsp_node_create(dsp_handle, node)) {
pr_err(self, "dsp node create failed");
dsp_node_free(dsp_handle, node);
return NULL;
}
pr_info(self, "dsp node created");
return node;
}
static void check_supported_levels(GstDspVEnc *self, gint tgt_level)
{
guint i;
gint tgt_mbps, tgt_mbpf, tgt_bitrate;
struct gstdsp_codec_level *cur, *level;
if (!self->supported_levels)
return;
tgt_bitrate = self->user_max_bitrate;
tgt_mbpf = ROUND_UP(self->width, 16) / 16 * ROUND_UP(self->height, 16) / 16;
tgt_mbps = tgt_mbpf * self->framerate;
if (!tgt_bitrate)
tgt_bitrate = self->bitrate;
search:
level = cur = self->supported_levels;
for (i = 0; i < self->nr_supported_levels; i++, cur++) {
bool ok = false;
/* is this the level we want? */
if (tgt_level > 0 && tgt_level != cur->id)
continue;
/* is the bitrate enough? (and doesn't overshoot) */
if (tgt_bitrate && cur->bitrate >= tgt_bitrate && level->bitrate < tgt_bitrate)
ok = true;
/* are the mbps enough? (and don't overshoot) */
if (cur->mbps >= tgt_mbps && level->mbps < tgt_mbps)
ok = true;
/* at same level of mbps, get the biggest bitrate */
if (cur->mbps >= tgt_mbps && cur->mbps == level->mbps &&
!tgt_bitrate && cur->bitrate >= level->bitrate)
ok = true;
/* are the mbpf enough? (and don't overshoot) */
if (cur->mbpf >= tgt_mbpf && level->mbpf < tgt_mbpf)
ok = true;
if (!ok)
continue;
/* we got a winner */
level = cur;
}
if (tgt_level > 0 && tgt_level != level->id) {
pr_warning(self, "invalid level: %d; ignoring", tgt_level);
tgt_level = -1;
goto search;
}
/* went beyond the table looking for a target; pick last one */
if ((tgt_bitrate && level->bitrate < tgt_bitrate) || (level->mbps < tgt_mbps)) {
if (tgt_level > 0) {
pr_warning(self, "invalid level: %d; ignoring", tgt_level);
tgt_level = -1;
goto search;
} else
level = --cur;
}
if (!self->user_max_bitrate || self->user_max_bitrate >= level->bitrate)
self->max_bitrate = level->bitrate;
else
self->max_bitrate = self->user_max_bitrate;
self->level = level->id;
pr_info(self, "level bitrate: %d", level->bitrate);
pr_info(self, "max bitrate: %d", self->max_bitrate);
pr_info(self, "level: %d", self->level);
}
static gboolean
sink_setcaps(GstPad *pad,
GstCaps *caps)
{
GstDspVEnc *self;
GstDspBase *base;
GstStructure *in_struc;
GstCaps *out_caps;
GstStructure *out_struc;
gint width = 0, height = 0;
GstCaps *allowed_caps;
gint tgt_level = -1;
struct td_codec *codec;
self = GST_DSP_VENC(GST_PAD_PARENT(pad));
base = GST_DSP_BASE(self);
codec = base->codec;
if (!codec)
return FALSE;
#ifdef DEBUG
{
gchar *str = gst_caps_to_string(caps);
pr_info(self, "sink caps: %s", str);
g_free(str);
}
#endif
in_struc = gst_caps_get_structure(caps, 0);
out_caps = gst_caps_new_empty();
switch (base->alg) {
case GSTDSP_JPEGENC:
out_struc = gst_structure_new("image/jpeg",
NULL);
break;
case GSTDSP_H263ENC:
out_struc = gst_structure_new("video/x-h263",
"variant", G_TYPE_STRING, "itu",
NULL);
break;
case GSTDSP_MP4VENC:
out_struc = gst_structure_new("video/mpeg",
"mpegversion", G_TYPE_INT, 4,
"systemstream", G_TYPE_BOOLEAN, FALSE,
NULL);
break;
case GSTDSP_H264ENC:
out_struc = gst_structure_new("video/x-h264",
"alignment", G_TYPE_STRING, "au",
NULL);
break;
default:
return FALSE;
}
if (gst_structure_get_int(in_struc, "width", &width))
gst_structure_set(out_struc, "width", G_TYPE_INT, width, NULL);
if (gst_structure_get_int(in_struc, "height", &height))
gst_structure_set(out_struc, "height", G_TYPE_INT, height, NULL);
gst_structure_get_fourcc(in_struc, "format", &self->color_format);
switch (base->alg) {
case GSTDSP_H263ENC:
case GSTDSP_MP4VENC:
case GSTDSP_H264ENC:
base->output_buffer_size = width * height / 2;
break;
case GSTDSP_JPEGENC:
if (width % 2 || height % 2)
return FALSE;
if (self->color_format == GST_MAKE_FOURCC('I', '4', '2', '0'))
base->input_buffer_size = ROUND_UP(width, 16) * ROUND_UP(height, 16) * 3 / 2;
else
base->input_buffer_size = ROUND_UP(width, 16) * ROUND_UP(height, 16) * 2;
base->output_buffer_size = width * height;
if (self->quality < 10)
base->output_buffer_size /= 10;
else if (self->quality < 100)
base->output_buffer_size /= (100 / self->quality);
break;
default:
break;
}
if (base->node)
goto skip_setup;
switch (base->alg) {
case GSTDSP_JPEGENC:
du_port_alloc_buffers(base->ports[0], 1);
#if SN_API > 1
du_port_alloc_buffers(base->ports[1], 2);
#else
/* old versions of the sn can't handle 2 buffers */
/*
* Some constrained pipelines might starve because of this. You
* might want to try enable-last-buffer=false on some sinks.
* TODO Is there any way around this?
*/
du_port_alloc_buffers(base->ports[1], 1);
#endif
break;
default:
du_port_alloc_buffers(base->ports[0], 2);
du_port_alloc_buffers(base->ports[1], 4);
break;
}
skip_setup:
self->width = width;
self->height = height;
{
const GValue *framerate = NULL;
framerate = gst_structure_get_value(in_struc, "framerate");
if (framerate) {
gst_structure_set_value(out_struc, "framerate", framerate);
/* calculate nearest integer */
self->framerate = (gst_value_get_fraction_numerator(framerate) * 2 /
gst_value_get_fraction_denominator(framerate) + 1) / 2;
}
}
/* see if downstream caps express something */
allowed_caps = gst_pad_get_allowed_caps(base->srcpad);
if (allowed_caps) {
if (gst_caps_get_size(allowed_caps) > 0) {
GstStructure *s;
s = gst_caps_get_structure(allowed_caps, 0);
gst_structure_get_int(s, "level", &tgt_level);
if (base->alg == GSTDSP_H264ENC) {
const char *stream_format;
stream_format = gst_structure_get_string(s, "stream-format");
if (stream_format && !strcmp(stream_format, "avc"))
self->priv.h264.bytestream = false;
else
stream_format = "byte-stream";
gst_structure_set(out_struc, "stream-format", G_TYPE_STRING, stream_format, NULL);
}
}
gst_caps_unref(allowed_caps);
}
check_supported_levels(self, tgt_level);
if (self->bitrate == 0)
self->bitrate = self->max_bitrate;
else if (self->bitrate > self->max_bitrate)
self->bitrate = self->max_bitrate;
gst_caps_append_structure(out_caps, out_struc);
#ifdef DEBUG
{
gchar *str = gst_caps_to_string(out_caps);
pr_info(self, "src caps: %s", str);
g_free(str);
}
#endif
if (!gst_pad_take_caps(base->srcpad, out_caps))
return FALSE;
if (base->node)
return TRUE;
base->node = create_node(self);
if (!base->node) {
pr_err(self, "dsp node creation failed");
return FALSE;
}
if (codec->setup_params)
codec->setup_params(base);
if (!gstdsp_start(base)) {
pr_err(self, "dsp start failed");
return FALSE;
}
if (codec->send_params)
codec->send_params(base, base->node);
return TRUE;
}
static gboolean
sink_event(GstDspBase *base,
GstEvent *event)
{
GstDspVEnc *self = GST_DSP_VENC(base);
switch (GST_EVENT_TYPE(event)) {
case GST_EVENT_CUSTOM_DOWNSTREAM:
{
const GstStructure *s;
s = gst_event_get_structure(event);
if (gst_structure_has_name(s, "GstForceKeyUnit")) {
g_mutex_lock(self->keyframe_mutex);
if (self->keyframe_event)
gst_event_unref(self->keyframe_event);
self->keyframe_event = event;
g_mutex_unlock(self->keyframe_mutex);
return TRUE;
}
break;
}
default:
break;
}
if (parent_class->sink_event)
return parent_class->sink_event(base, event);
return gst_pad_push_event(base->srcpad, event);
}
static gboolean
src_event(GstDspBase *base,
GstEvent *event)
{
GstDspVEnc *self = GST_DSP_VENC(base);
switch (GST_EVENT_TYPE(event)) {
case GST_EVENT_CUSTOM_UPSTREAM:
{
const GstStructure *s;
s = gst_event_get_structure(event);
if (gst_structure_has_name(s, "GstForceKeyUnit")) {
g_mutex_lock(self->keyframe_mutex);
/* make it downstream */
GST_EVENT_TYPE(event) = GST_EVENT_CUSTOM_DOWNSTREAM;
if (self->keyframe_event)
gst_event_unref(self->keyframe_event);
self->keyframe_event = event;
g_mutex_unlock(self->keyframe_mutex);
return TRUE;
}
break;
}
default:
break;
}
if (parent_class->src_event)
return parent_class->src_event(base, event);
return gst_pad_push_event(base->sinkpad, event);
}
static void
set_property(GObject *obj,
guint prop_id,
const GValue *value,
GParamSpec *pspec)
{
GstDspVEnc *self = GST_DSP_VENC(obj);
switch (prop_id) {
case ARG_BITRATE: {
guint bitrate;
bitrate = g_value_get_uint(value);
if (self->max_bitrate && bitrate > (unsigned) self->max_bitrate)
bitrate = self->max_bitrate;
g_atomic_int_set(&self->bitrate, bitrate);
break;
}
case ARG_MODE:
self->mode = g_value_get_enum(value);
/* guess intra-refresh, if not manually set */
if (self->intra_refresh_set)
break;
self->intra_refresh = (self->mode == 1);
break;
case ARG_KEYFRAME_INTERVAL:
g_atomic_int_set(&self->keyframe_interval, g_value_get_int(value));
break;
case ARG_MAX_BITRATE:
self->user_max_bitrate = g_value_get_uint(value);
break;
case ARG_INTRA_REFRESH:
self->intra_refresh = g_value_get_boolean(value);
self->intra_refresh_set = true;
break;
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID(obj, prop_id, pspec);
break;
}
}
static void
get_property(GObject *obj,
guint prop_id,
GValue *value,
GParamSpec *pspec)
{
GstDspVEnc *self = GST_DSP_VENC(obj);
switch (prop_id) {
case ARG_BITRATE:
g_value_set_uint(value, g_atomic_int_get(&self->bitrate));
break;
case ARG_MODE:
g_value_set_enum(value, self->mode);
break;
case ARG_KEYFRAME_INTERVAL:
g_value_set_int(value, g_atomic_int_get(&self->keyframe_interval));
break;
case ARG_MAX_BITRATE: {
guint bitrate;
bitrate = self->user_max_bitrate;
if (!bitrate)
bitrate = self->max_bitrate;
g_value_set_uint(value, bitrate);
break;
}
case ARG_INTRA_REFRESH:
g_value_set_boolean(value, self->intra_refresh);
break;
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID(obj, prop_id, pspec);
break;
}
}
static void
reset(GstDspBase *base)
{
GstDspVEnc *self = GST_DSP_VENC(base);
pr_debug(self, "venc reset");
/* some cleanup */
if (base->alg == GSTDSP_H264ENC) {
self->priv.h264.codec_data_done = FALSE;
self->priv.h264.sps_received = FALSE;
self->priv.h264.pps_received = FALSE;
gst_buffer_replace(&self->priv.h264.sps, NULL);
gst_buffer_replace(&self->priv.h264.pps, NULL);
gst_buffer_replace(&self->priv.h264.codec_data, NULL);
} else
self->priv.mpeg4.codec_data_done = FALSE;
}
static void
instance_init(GTypeInstance *instance,
gpointer g_class)
{
GstDspBase *base = GST_DSP_BASE(instance);
GstDspVEnc *self = GST_DSP_VENC(instance);
gst_pad_set_setcaps_function(base->sinkpad, sink_setcaps);
base->reset = reset;
self->bitrate = DEFAULT_BITRATE;
self->mode = DEFAULT_MODE;
self->keyframe_interval = DEFAULT_KEYFRAME_INTERVAL;
self->intra_refresh = DEFAULT_INTRA_REFRESH;
self->keyframe_mutex = g_mutex_new();
}
static void
finalize(GObject *obj)
{
GstDspVEnc *self = GST_DSP_VENC(obj);
g_mutex_free(self->keyframe_mutex);
if (self->keyframe_event)
gst_event_unref(self->keyframe_event);
G_OBJECT_CLASS(parent_class)->finalize(obj);
}
static void
base_init(gpointer g_class)
{
GstElementClass *element_class;
GstPadTemplate *template;
element_class = GST_ELEMENT_CLASS(g_class);
template = gst_pad_template_new("sink", GST_PAD_SINK,
GST_PAD_ALWAYS,
generate_sink_template());
gst_element_class_add_pad_template(element_class, template);
gst_object_unref(template);
}
static void
class_init(gpointer g_class,
gpointer class_data)
{
GObjectClass *gobject_class;
GstDspBaseClass *base_class;
parent_class = g_type_class_peek_parent(g_class);
gobject_class = G_OBJECT_CLASS(g_class);
base_class = GST_DSP_BASE_CLASS(g_class);
gobject_class->set_property = set_property;
gobject_class->get_property = get_property;
g_object_class_install_property(gobject_class, ARG_BITRATE,
g_param_spec_uint("bitrate", "Bit-rate",
"Encoding bit-rate (0 for auto)",
0, G_MAXUINT, DEFAULT_BITRATE,
G_PARAM_READWRITE));
g_object_class_install_property(gobject_class, ARG_MODE,
g_param_spec_enum("mode", "Encoding mode",
"Encoding mode",
GST_TYPE_DSPVENC_MODE,
DEFAULT_MODE,
G_PARAM_READWRITE));
g_object_class_install_property(gobject_class, ARG_KEYFRAME_INTERVAL,
g_param_spec_int("keyframe-interval", "Keyframe interval",
"Generate keyframes at every specified intervals (seconds)",
0, G_MAXINT, DEFAULT_KEYFRAME_INTERVAL, G_PARAM_READWRITE));
g_object_class_install_property(gobject_class, ARG_MAX_BITRATE,
g_param_spec_uint("max-bitrate", "Maximum Bit-rate",
"Maximum Encoding bit-rate (0 for auto)",
0, G_MAXUINT, DEFAULT_MAX_BITRATE,
G_PARAM_READWRITE));
g_object_class_install_property(gobject_class, ARG_INTRA_REFRESH,
g_param_spec_boolean("intra-refresh", "Intra-refresh",
"Whether or not to use periodic intra-refresh",
DEFAULT_INTRA_REFRESH, G_PARAM_READWRITE));
gobject_class->finalize = finalize;
base_class->src_event = src_event;
base_class->sink_event = sink_event;
}
GType
gst_dsp_venc_get_type(void)
{
static GType type;
if (G_UNLIKELY(type == 0)) {
GTypeInfo type_info = {
.class_size = sizeof(GstDspVEncClass),
.class_init = class_init,
.base_init = base_init,
.instance_size = sizeof(GstDspVEnc),
.instance_init = instance_init,
};
type = g_type_register_static(GST_DSP_BASE_TYPE, "GstDspVEnc", &type_info, 0);
}
return type;
}