1 /*
2 * GStreamer
3 * Copyright (C) 2019 Mathieu Duponchelle <mathieu@centricular.com>
4 *
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Library General Public
7 * License as published by the Free Software Foundation; either
8 * version 2 of the License, or (at your option) any later version.
9 *
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Library General Public License for more details.
14 *
15 * You should have received a copy of the GNU Library General Public
16 * License along with this library; if not, write to the
17 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
18 * Boston, MA 02110-1301, USA.
19 */
20
21 /**
22 * SECTION:element-line21encoder
23 * @title: line21encoder
24 *
25 */
26
27 #ifdef HAVE_CONFIG_H
28 # include <config.h>
29 #endif
30
31 #include <gst/gst.h>
32 #include <gst/video/video.h>
33 #include <string.h>
34
35 #include "gstline21enc.h"
36 #include "io-sim.h"
37
38 GST_DEBUG_CATEGORY_STATIC (gst_line_21_encoder_debug);
39 #define GST_CAT_DEFAULT gst_line_21_encoder_debug
40
41 #define CAPS "video/x-raw, format={ I420, YUY2, YVYU, UYVY, VYUY }, width=(int)720, height=(int)[ 23, MAX ], interlace-mode=interleaved"
42
43 static GstStaticPadTemplate sinktemplate = GST_STATIC_PAD_TEMPLATE ("sink",
44 GST_PAD_SINK,
45 GST_PAD_ALWAYS,
46 GST_STATIC_CAPS (CAPS));
47
48 static GstStaticPadTemplate srctemplate = GST_STATIC_PAD_TEMPLATE ("src",
49 GST_PAD_SRC,
50 GST_PAD_ALWAYS,
51 GST_STATIC_CAPS (CAPS));
52
53 G_DEFINE_TYPE (GstLine21Encoder, gst_line_21_encoder, GST_TYPE_VIDEO_FILTER);
54 #define parent_class gst_line_21_encoder_parent_class
55
56 static gboolean gst_line_21_encoder_set_info (GstVideoFilter * filter,
57 GstCaps * incaps, GstVideoInfo * in_info,
58 GstCaps * outcaps, GstVideoInfo * out_info);
59 static GstFlowReturn gst_line_21_encoder_transform_ip (GstVideoFilter * filter,
60 GstVideoFrame * frame);
61
62 static void
gst_line_21_encoder_class_init(GstLine21EncoderClass * klass)63 gst_line_21_encoder_class_init (GstLine21EncoderClass * klass)
64 {
65 GstElementClass *gstelement_class;
66 GstVideoFilterClass *filter_class;
67
68 gstelement_class = (GstElementClass *) klass;
69 filter_class = (GstVideoFilterClass *) klass;
70
71 gst_element_class_set_static_metadata (gstelement_class,
72 "Line 21 CC Encoder",
73 "Filter/Video/ClosedCaption",
74 "Inject line21 CC in SD video streams",
75 "Mathieu Duponchelle <mathieu@centricular.com>");
76
77 gst_element_class_add_static_pad_template (gstelement_class, &sinktemplate);
78 gst_element_class_add_static_pad_template (gstelement_class, &srctemplate);
79
80 filter_class->set_info = gst_line_21_encoder_set_info;
81 filter_class->transform_frame_ip = gst_line_21_encoder_transform_ip;
82
83 GST_DEBUG_CATEGORY_INIT (gst_line_21_encoder_debug, "line21encoder",
84 0, "Line 21 CC Encoder");
85 vbi_initialize_gst_debug ();
86 }
87
88 static void
gst_line_21_encoder_init(GstLine21Encoder * filter)89 gst_line_21_encoder_init (GstLine21Encoder * filter)
90 {
91 }
92
93 static vbi_pixfmt
vbi_pixfmt_from_gst_video_format(GstVideoFormat format)94 vbi_pixfmt_from_gst_video_format (GstVideoFormat format)
95 {
96 switch (format) {
97 case GST_VIDEO_FORMAT_I420:
98 return VBI_PIXFMT_YUV420;
99 case GST_VIDEO_FORMAT_YUY2:
100 return VBI_PIXFMT_YUYV;
101 case GST_VIDEO_FORMAT_YVYU:
102 return VBI_PIXFMT_YVYU;
103 case GST_VIDEO_FORMAT_UYVY:
104 return VBI_PIXFMT_UYVY;
105 case GST_VIDEO_FORMAT_VYUY:
106 return VBI_PIXFMT_VYUY;
107 default:
108 g_assert_not_reached ();
109 return (vbi_pixfmt) 0;
110 }
111 #undef NATIVE_VBI_FMT
112 }
113
114 static gboolean
gst_line_21_encoder_set_info(GstVideoFilter * filter,GstCaps * incaps,GstVideoInfo * in_info,GstCaps * outcaps,GstVideoInfo * out_info)115 gst_line_21_encoder_set_info (GstVideoFilter * filter,
116 GstCaps * incaps, GstVideoInfo * in_info,
117 GstCaps * outcaps, GstVideoInfo * out_info)
118 {
119 GstLine21Encoder *self = GST_LINE21ENCODER (filter);
120
121 self->info = *in_info;
122
123 /*
124 * Set up blank / black / white levels fit for NTSC, no actual relation
125 * with the height of the video
126 */
127 self->sp.scanning = 525;
128 /* The pixel format */
129 self->sp.sampling_format =
130 vbi_pixfmt_from_gst_video_format (GST_VIDEO_INFO_FORMAT (&self->info));
131 /* Sampling rate. For BT.601 it's 13.5MHz */
132 self->sp.sampling_rate = 13.5e6;
133 /* Stride */
134 self->sp.bytes_per_line = GST_VIDEO_INFO_COMP_STRIDE (&self->info, 0);
135 /* Horizontal offset of the VBI image */
136 self->sp.offset = 122;
137
138 /* FIXME: magic numbers */
139 self->sp.start[0] = 21;
140 self->sp.count[0] = 1;
141 self->sp.start[1] = 284;
142 self->sp.count[1] = 1;
143
144 self->sp.interlaced = FALSE;
145 self->sp.synchronous = TRUE;
146
147 return TRUE;
148 }
149
150 static GstFlowReturn
gst_line_21_encoder_transform_ip(GstVideoFilter * filter,GstVideoFrame * frame)151 gst_line_21_encoder_transform_ip (GstVideoFilter * filter,
152 GstVideoFrame * frame)
153 {
154 GstLine21Encoder *self = GST_LINE21ENCODER (filter);
155 GstVideoCaptionMeta *cc_meta;
156 guint8 *buf;
157 vbi_sliced sliced[2];
158 gpointer iter = NULL;
159 GstFlowReturn ret = GST_FLOW_ERROR;
160
161 sliced[0].id = VBI_SLICED_CAPTION_525_F1;
162 sliced[0].line = self->sp.start[0];
163 sliced[1].id = VBI_SLICED_CAPTION_525_F2;
164 sliced[1].line = self->sp.start[1];
165
166 sliced[0].data[0] = 0x80;
167 sliced[0].data[1] = 0x80;
168 sliced[1].data[0] = 0x80;
169 sliced[1].data[1] = 0x80;
170
171 /* We loop over caption metas until we find the first CEA608 meta */
172 while ((cc_meta = (GstVideoCaptionMeta *)
173 gst_buffer_iterate_meta_filtered (frame->buffer, &iter,
174 GST_VIDEO_CAPTION_META_API_TYPE))) {
175 guint n = cc_meta->size;
176 guint i;
177
178 if (cc_meta->caption_type != GST_VIDEO_CAPTION_TYPE_CEA608_S334_1A)
179 continue;
180
181 if (n % 3 != 0) {
182 GST_ERROR_OBJECT (filter, "Invalid S334-1A CEA608 buffer size");
183 goto done;
184 }
185
186 n /= 3;
187
188 if (n >= 3) {
189 GST_ERROR_OBJECT (filter, "Too many S334-1A CEA608 triplets %u", n);
190 goto done;
191 }
192
193 for (i = 0; i < n; i++) {
194 if (cc_meta->data[i * 3] & 0x80) {
195 sliced[0].data[0] = cc_meta->data[i * 3 + 1];
196 sliced[0].data[1] = cc_meta->data[i * 3 + 2];
197 } else {
198 sliced[1].data[0] = cc_meta->data[i * 3 + 1];
199 sliced[1].data[1] = cc_meta->data[i * 3 + 2];
200 }
201 }
202
203 break;
204 }
205
206 /* We've encoded this meta, it can now be removed */
207 if (cc_meta)
208 gst_buffer_remove_meta (frame->buffer, (GstMeta *) cc_meta);
209
210 buf =
211 (guint8 *) GST_VIDEO_FRAME_PLANE_DATA (frame,
212 0) + 21 * GST_VIDEO_INFO_COMP_STRIDE (&self->info, 0);
213
214 if (!vbi_raw_video_image (buf, GST_VIDEO_INFO_COMP_STRIDE (&self->info,
215 0) * 2, &self->sp, 0, 0, 0, 0x000000FF, 0, sliced, 2)) {
216 GST_ERROR_OBJECT (filter, "Failed to encode CC data");
217 goto done;
218 }
219
220 ret = GST_FLOW_OK;
221
222 done:
223 return ret;
224 }
225