1 /* GStreamer
2 * Copyright (C) 2015 Sebastian Dröge <sebastian@centricular.com>
3 *
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Library General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the License, or (at your option) any later version.
8 *
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Library General Public License for more details.
13 *
14 * You should have received a copy of the GNU Library General Public
15 * License along with this library; if not, write to the
16 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
17 * Boston, MA 02110-1301, USA.
18 */
19
20 #include "gstrtputils.h"
21
22 typedef struct
23 {
24 GstElement *element;
25 GstBuffer *outbuf;
26 GQuark copy_tag;
27 } CopyMetaData;
28
29 GQuark rtp_quark_meta_tag_video;
30 GQuark rtp_quark_meta_tag_audio;
31
32 static gboolean
foreach_metadata_copy(GstBuffer * inbuf,GstMeta ** meta,gpointer user_data)33 foreach_metadata_copy (GstBuffer * inbuf, GstMeta ** meta, gpointer user_data)
34 {
35 CopyMetaData *data = user_data;
36 GstElement *element = data->element;
37 GstBuffer *outbuf = data->outbuf;
38 GQuark copy_tag = data->copy_tag;
39 const GstMetaInfo *info = (*meta)->info;
40 const gchar *const *tags = gst_meta_api_type_get_tags (info->api);
41
42 if (info->transform_func && (!tags || !tags[0] || (copy_tag != 0
43 && g_strv_length ((gchar **) tags) == 1
44 && gst_meta_api_type_has_tag (info->api, copy_tag)))) {
45 GstMetaTransformCopy copy_data = { FALSE, 0, -1 };
46 GST_DEBUG_OBJECT (element, "copy metadata %s", g_type_name (info->api));
47 /* simply copy then */
48 info->transform_func (outbuf, *meta, inbuf,
49 _gst_meta_transform_copy, ©_data);
50 } else {
51 GST_DEBUG_OBJECT (element, "not copying metadata %s",
52 g_type_name (info->api));
53 }
54
55 return TRUE;
56 }
57
58 /* TODO: Should probably make copy_tag an array at some point */
59 void
gst_rtp_copy_meta(GstElement * element,GstBuffer * outbuf,GstBuffer * inbuf,GQuark copy_tag)60 gst_rtp_copy_meta (GstElement * element, GstBuffer * outbuf, GstBuffer * inbuf,
61 GQuark copy_tag)
62 {
63 CopyMetaData data = { element, outbuf, copy_tag };
64
65 gst_buffer_foreach_meta (inbuf, foreach_metadata_copy, &data);
66 }
67
68 void
gst_rtp_copy_video_meta(gpointer element,GstBuffer * outbuf,GstBuffer * inbuf)69 gst_rtp_copy_video_meta (gpointer element, GstBuffer * outbuf,
70 GstBuffer * inbuf)
71 {
72 gst_rtp_copy_meta (element, outbuf, inbuf, rtp_quark_meta_tag_video);
73 }
74
75 void
gst_rtp_copy_audio_meta(gpointer element,GstBuffer * outbuf,GstBuffer * inbuf)76 gst_rtp_copy_audio_meta (gpointer element, GstBuffer * outbuf,
77 GstBuffer * inbuf)
78 {
79 gst_rtp_copy_meta (element, outbuf, inbuf, rtp_quark_meta_tag_audio);
80 }
81
82 typedef struct
83 {
84 GstElement *element;
85 GQuark keep_tag;
86 } DropMetaData;
87
88 static gboolean
foreach_metadata_drop(GstBuffer * inbuf,GstMeta ** meta,gpointer user_data)89 foreach_metadata_drop (GstBuffer * inbuf, GstMeta ** meta, gpointer user_data)
90 {
91 DropMetaData *data = user_data;
92 GstElement *element = data->element;
93 GQuark keep_tag = data->keep_tag;
94 const GstMetaInfo *info = (*meta)->info;
95 const gchar *const *tags = gst_meta_api_type_get_tags (info->api);
96
97 if (!tags || !tags[0] || (keep_tag != 0
98 && g_strv_length ((gchar **) tags) == 1
99 && gst_meta_api_type_has_tag (info->api, keep_tag))) {
100 GST_DEBUG_OBJECT (element, "keeping metadata %s", g_type_name (info->api));
101 } else {
102 GST_DEBUG_OBJECT (element, "dropping metadata %s", g_type_name (info->api));
103 *meta = NULL;
104 }
105
106 return TRUE;
107 }
108
109 /* TODO: Should probably make keep_tag an array at some point */
110 void
gst_rtp_drop_meta(GstElement * element,GstBuffer * buf,GQuark keep_tag)111 gst_rtp_drop_meta (GstElement * element, GstBuffer * buf, GQuark keep_tag)
112 {
113 DropMetaData data = { element, keep_tag };
114
115 gst_buffer_foreach_meta (buf, foreach_metadata_drop, &data);
116 }
117
118 void
gst_rtp_drop_non_audio_meta(gpointer element,GstBuffer * buf)119 gst_rtp_drop_non_audio_meta (gpointer element, GstBuffer * buf)
120 {
121 gst_rtp_drop_meta (element, buf, rtp_quark_meta_tag_audio);
122 }
123
124 void
gst_rtp_drop_non_video_meta(gpointer element,GstBuffer * buf)125 gst_rtp_drop_non_video_meta (gpointer element, GstBuffer * buf)
126 {
127 gst_rtp_drop_meta (element, buf, rtp_quark_meta_tag_video);
128 }
129
130 /* Stolen from bad/gst/mpegtsdemux/payloader_parsers.c */
131 /* variable length Exp-Golomb parsing according to H.265 spec section 9.2*/
132 gboolean
gst_rtp_read_golomb(GstBitReader * br,guint32 * value)133 gst_rtp_read_golomb (GstBitReader * br, guint32 * value)
134 {
135 guint8 b, leading_zeros = -1;
136 *value = 1;
137
138 for (b = 0; !b; leading_zeros++) {
139 if (!gst_bit_reader_get_bits_uint8 (br, &b, 1))
140 return FALSE;
141 *value *= 2;
142 }
143
144 *value = (*value >> 1) - 1;
145 if (leading_zeros > 0) {
146 guint32 tmp = 0;
147 if (!gst_bit_reader_get_bits_uint32 (br, &tmp, leading_zeros))
148 return FALSE;
149 *value += tmp;
150 }
151
152 return TRUE;
153 }
154