• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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/base/base.h>
33 #include <gst/video/video.h>
34 #include <string.h>
35 
36 #include "gstline21enc.h"
37 #include "io-sim.h"
38 
39 GST_DEBUG_CATEGORY_STATIC (gst_line_21_encoder_debug);
40 #define GST_CAT_DEFAULT gst_line_21_encoder_debug
41 
42 enum
43 {
44   PROP_0,
45   PROP_REMOVE_CAPTION_META,
46 };
47 
48 /* FIXME: add and test support for PAL resolutions */
49 #define CAPS "video/x-raw, format={ I420, YUY2, YVYU, UYVY, VYUY }, width=(int)720, height=(int){ 525, 486 }, interlace-mode=interleaved"
50 
51 static GstStaticPadTemplate sinktemplate = GST_STATIC_PAD_TEMPLATE ("sink",
52     GST_PAD_SINK,
53     GST_PAD_ALWAYS,
54     GST_STATIC_CAPS (CAPS));
55 
56 static GstStaticPadTemplate srctemplate = GST_STATIC_PAD_TEMPLATE ("src",
57     GST_PAD_SRC,
58     GST_PAD_ALWAYS,
59     GST_STATIC_CAPS (CAPS));
60 
61 G_DEFINE_TYPE (GstLine21Encoder, gst_line_21_encoder, GST_TYPE_VIDEO_FILTER);
62 #define parent_class gst_line_21_encoder_parent_class
63 GST_ELEMENT_REGISTER_DEFINE (line21encoder, "line21encoder",
64     GST_RANK_NONE, GST_TYPE_LINE21ENCODER);
65 
66 static gboolean gst_line_21_encoder_set_info (GstVideoFilter * filter,
67     GstCaps * incaps, GstVideoInfo * in_info,
68     GstCaps * outcaps, GstVideoInfo * out_info);
69 static GstFlowReturn gst_line_21_encoder_transform_ip (GstVideoFilter * filter,
70     GstVideoFrame * frame);
71 static void gst_line_21_encoder_set_property (GObject * self, guint prop_id,
72     const GValue * value, GParamSpec * pspec);
73 static void gst_line_21_encoder_get_property (GObject * self, guint prop_id,
74     GValue * value, GParamSpec * pspec);
75 
76 
77 static void
gst_line_21_encoder_class_init(GstLine21EncoderClass * klass)78 gst_line_21_encoder_class_init (GstLine21EncoderClass * klass)
79 {
80   GObjectClass *gobject_class;
81   GstElementClass *gstelement_class;
82   GstVideoFilterClass *filter_class;
83 
84   gobject_class = (GObjectClass *) klass;
85   gstelement_class = (GstElementClass *) klass;
86   filter_class = (GstVideoFilterClass *) klass;
87 
88   gobject_class->set_property = gst_line_21_encoder_set_property;
89   gobject_class->get_property = gst_line_21_encoder_get_property;
90 
91   /**
92    * line21encoder:remove-caption-meta
93    *
94    * Selects whether the encoded #GstVideoCaptionMeta should be removed from
95    * the outgoing video buffers or whether it should be kept.
96    *
97    * Since: 1.20
98    */
99   g_object_class_install_property (G_OBJECT_CLASS (klass),
100       PROP_REMOVE_CAPTION_META, g_param_spec_boolean ("remove-caption-meta",
101           "Remove Caption Meta",
102           "Remove encoded caption meta from outgoing video buffers", FALSE,
103           G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
104 
105 
106   gst_element_class_set_static_metadata (gstelement_class,
107       "Line 21 CC Encoder",
108       "Filter/Video/ClosedCaption",
109       "Inject line21 CC in SD video streams",
110       "Mathieu Duponchelle <mathieu@centricular.com>");
111 
112   gst_element_class_add_static_pad_template (gstelement_class, &sinktemplate);
113   gst_element_class_add_static_pad_template (gstelement_class, &srctemplate);
114 
115   filter_class->set_info = gst_line_21_encoder_set_info;
116   filter_class->transform_frame_ip = gst_line_21_encoder_transform_ip;
117 
118   GST_DEBUG_CATEGORY_INIT (gst_line_21_encoder_debug, "line21encoder",
119       0, "Line 21 CC Encoder");
120   vbi_initialize_gst_debug ();
121 }
122 
123 static void
gst_line_21_encoder_init(GstLine21Encoder * filter)124 gst_line_21_encoder_init (GstLine21Encoder * filter)
125 {
126 }
127 
128 static void
gst_line_21_encoder_set_property(GObject * object,guint prop_id,const GValue * value,GParamSpec * pspec)129 gst_line_21_encoder_set_property (GObject * object, guint prop_id,
130     const GValue * value, GParamSpec * pspec)
131 {
132   GstLine21Encoder *enc = GST_LINE21ENCODER (object);
133 
134   switch (prop_id) {
135     case PROP_REMOVE_CAPTION_META:
136       enc->remove_caption_meta = g_value_get_boolean (value);
137       break;
138     default:
139       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
140       break;
141   }
142 }
143 
144 static void
gst_line_21_encoder_get_property(GObject * object,guint prop_id,GValue * value,GParamSpec * pspec)145 gst_line_21_encoder_get_property (GObject * object, guint prop_id,
146     GValue * value, GParamSpec * pspec)
147 {
148   GstLine21Encoder *enc = GST_LINE21ENCODER (object);
149 
150   switch (prop_id) {
151     case PROP_REMOVE_CAPTION_META:
152       g_value_set_boolean (value, enc->remove_caption_meta);
153       break;
154     default:
155       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
156       break;
157   }
158 }
159 
160 static vbi_pixfmt
vbi_pixfmt_from_gst_video_format(GstVideoFormat format)161 vbi_pixfmt_from_gst_video_format (GstVideoFormat format)
162 {
163   switch (format) {
164     case GST_VIDEO_FORMAT_I420:
165       return VBI_PIXFMT_YUV420;
166     case GST_VIDEO_FORMAT_YUY2:
167       return VBI_PIXFMT_YUYV;
168     case GST_VIDEO_FORMAT_YVYU:
169       return VBI_PIXFMT_YVYU;
170     case GST_VIDEO_FORMAT_UYVY:
171       return VBI_PIXFMT_UYVY;
172     case GST_VIDEO_FORMAT_VYUY:
173       return VBI_PIXFMT_VYUY;
174     default:
175       g_assert_not_reached ();
176       return (vbi_pixfmt) 0;
177   }
178 #undef NATIVE_VBI_FMT
179 }
180 
181 static gboolean
gst_line_21_encoder_set_info(GstVideoFilter * filter,GstCaps * incaps,GstVideoInfo * in_info,GstCaps * outcaps,GstVideoInfo * out_info)182 gst_line_21_encoder_set_info (GstVideoFilter * filter,
183     GstCaps * incaps, GstVideoInfo * in_info,
184     GstCaps * outcaps, GstVideoInfo * out_info)
185 {
186   GstLine21Encoder *self = GST_LINE21ENCODER (filter);
187 
188   self->info = *in_info;
189 
190   /*
191    * Set up blank / black / white levels fit for NTSC, no actual relation
192    * with the height of the video
193    */
194   self->sp.scanning = 525;
195   /* The pixel format */
196   self->sp.sampling_format =
197       vbi_pixfmt_from_gst_video_format (GST_VIDEO_INFO_FORMAT (&self->info));
198   /* Sampling rate. For BT.601 it's 13.5MHz */
199   self->sp.sampling_rate = 13.5e6;
200   /* Stride */
201   self->sp.bytes_per_line = GST_VIDEO_INFO_COMP_STRIDE (&self->info, 0);
202   /* Horizontal offset of the VBI image */
203   self->sp.offset = 122;
204 
205   /* FIXME: magic numbers */
206   self->sp.start[0] = 21;
207   self->sp.count[0] = 1;
208   self->sp.start[1] = 284;
209   self->sp.count[1] = 1;
210 
211   self->sp.interlaced = FALSE;
212   self->sp.synchronous = TRUE;
213 
214   return TRUE;
215 }
216 
217 #define MAX_CDP_PACKET_LEN 256
218 #define MAX_CEA608_LEN 32
219 
220 /* Converts CDP into raw CEA708 cc_data, taken from ccconverter */
221 static guint
convert_cea708_cdp_cea708_cc_data_internal(GstLine21Encoder * self,const guint8 * cdp,guint cdp_len,guint8 cc_data[256])222 convert_cea708_cdp_cea708_cc_data_internal (GstLine21Encoder * self,
223     const guint8 * cdp, guint cdp_len, guint8 cc_data[256])
224 {
225   GstByteReader br;
226   guint16 u16;
227   guint8 u8;
228   guint8 flags;
229   guint len = 0;
230 
231   /* Header + footer length */
232   if (cdp_len < 11) {
233     GST_WARNING_OBJECT (self, "cdp packet too short (%u). expected at "
234         "least %u", cdp_len, 11);
235     return 0;
236   }
237 
238   gst_byte_reader_init (&br, cdp, cdp_len);
239   u16 = gst_byte_reader_get_uint16_be_unchecked (&br);
240   if (u16 != 0x9669) {
241     GST_WARNING_OBJECT (self, "cdp packet does not have initial magic bytes "
242         "of 0x9669");
243     return 0;
244   }
245 
246   u8 = gst_byte_reader_get_uint8_unchecked (&br);
247   if (u8 != cdp_len) {
248     GST_WARNING_OBJECT (self, "cdp packet length (%u) does not match passed "
249         "in value (%u)", u8, cdp_len);
250     return 0;
251   }
252 
253   gst_byte_reader_skip_unchecked (&br, 1);
254 
255   flags = gst_byte_reader_get_uint8_unchecked (&br);
256   /* No cc_data? */
257   if ((flags & 0x40) == 0) {
258     GST_DEBUG_OBJECT (self, "cdp packet does have any cc_data");
259     return 0;
260   }
261 
262   /* cdp_hdr_sequence_cntr */
263   gst_byte_reader_skip_unchecked (&br, 2);
264 
265   /* time_code_present */
266   if (flags & 0x80) {
267     if (gst_byte_reader_get_remaining (&br) < 5) {
268       GST_WARNING_OBJECT (self, "cdp packet does not have enough data to "
269           "contain a timecode (%u). Need at least 5 bytes",
270           gst_byte_reader_get_remaining (&br));
271       return 0;
272     }
273 
274     gst_byte_reader_skip_unchecked (&br, 5);
275   }
276 
277   /* ccdata_present */
278   if (flags & 0x40) {
279     guint8 cc_count;
280 
281     if (gst_byte_reader_get_remaining (&br) < 2) {
282       GST_WARNING_OBJECT (self, "not enough data to contain valid cc_data");
283       return 0;
284     }
285     u8 = gst_byte_reader_get_uint8_unchecked (&br);
286     if (u8 != 0x72) {
287       GST_WARNING_OBJECT (self, "missing cc_data start code of 0x72, "
288           "found 0x%02x", u8);
289       return 0;
290     }
291 
292     cc_count = gst_byte_reader_get_uint8_unchecked (&br);
293     if ((cc_count & 0xe0) != 0xe0) {
294       GST_WARNING_OBJECT (self, "reserved bits are not 0xe0, found 0x%02x", u8);
295       return 0;
296     }
297     cc_count &= 0x1f;
298 
299     len = 3 * cc_count;
300     if (gst_byte_reader_get_remaining (&br) < len)
301       return 0;
302 
303     memcpy (cc_data, gst_byte_reader_get_data_unchecked (&br, len), len);
304   }
305 
306   /* skip everything else we don't care about */
307   return len;
308 }
309 
310 #define VAL_OR_0(v) ((v) ? (*(v)) : 0)
311 
312 static guint
compact_cc_data(guint8 * cc_data,guint cc_data_len)313 compact_cc_data (guint8 * cc_data, guint cc_data_len)
314 {
315   gboolean started_ccp = FALSE;
316   guint out_len = 0;
317   guint i;
318 
319   if (cc_data_len % 3 != 0) {
320     GST_WARNING ("Invalid cc_data buffer size");
321     cc_data_len = cc_data_len - (cc_data_len % 3);
322   }
323 
324   for (i = 0; i < cc_data_len / 3; i++) {
325     gboolean cc_valid = (cc_data[i * 3] & 0x04) == 0x04;
326     guint8 cc_type = cc_data[i * 3] & 0x03;
327 
328     if (!started_ccp && (cc_type == 0x00 || cc_type == 0x01)) {
329       if (cc_valid) {
330         /* copy over valid 608 data */
331         cc_data[out_len++] = cc_data[i * 3];
332         cc_data[out_len++] = cc_data[i * 3 + 1];
333         cc_data[out_len++] = cc_data[i * 3 + 2];
334       }
335       continue;
336     }
337 
338     if (cc_type & 0x10)
339       started_ccp = TRUE;
340 
341     if (!cc_valid)
342       continue;
343 
344     if (cc_type == 0x00 || cc_type == 0x01) {
345       GST_WARNING ("Invalid cc_data.  cea608 bytes after cea708");
346       return 0;
347     }
348 
349     cc_data[out_len++] = cc_data[i * 3];
350     cc_data[out_len++] = cc_data[i * 3 + 1];
351     cc_data[out_len++] = cc_data[i * 3 + 2];
352   }
353 
354   GST_LOG ("compacted cc_data from %u to %u", cc_data_len, out_len);
355 
356   return out_len;
357 }
358 
359 static gint
cc_data_extract_cea608(guint8 * cc_data,guint cc_data_len,guint8 * cea608_field1,guint * cea608_field1_len,guint8 * cea608_field2,guint * cea608_field2_len)360 cc_data_extract_cea608 (guint8 * cc_data, guint cc_data_len,
361     guint8 * cea608_field1, guint * cea608_field1_len,
362     guint8 * cea608_field2, guint * cea608_field2_len)
363 {
364   guint i, field_1_len = 0, field_2_len = 0;
365 
366   if (cea608_field1_len) {
367     field_1_len = *cea608_field1_len;
368     *cea608_field1_len = 0;
369   }
370   if (cea608_field2_len) {
371     field_2_len = *cea608_field2_len;
372     *cea608_field2_len = 0;
373   }
374 
375   if (cc_data_len % 3 != 0) {
376     GST_WARNING ("Invalid cc_data buffer size %u. Truncating to a multiple "
377         "of 3", cc_data_len);
378     cc_data_len = cc_data_len - (cc_data_len % 3);
379   }
380 
381   for (i = 0; i < cc_data_len / 3; i++) {
382     gboolean cc_valid = (cc_data[i * 3] & 0x04) == 0x04;
383     guint8 cc_type = cc_data[i * 3] & 0x03;
384 
385     GST_TRACE ("0x%02x 0x%02x 0x%02x, valid: %u, type: 0b%u%u",
386         cc_data[i * 3 + 0], cc_data[i * 3 + 1], cc_data[i * 3 + 2], cc_valid,
387         cc_type & 0x2, cc_type & 0x1);
388 
389     if (cc_type == 0x00) {
390       if (!cc_valid)
391         continue;
392 
393       if (cea608_field1 && cea608_field1_len) {
394         if (*cea608_field1_len + 2 > field_1_len) {
395           GST_WARNING ("Too many cea608 input bytes %u for field 1",
396               *cea608_field1_len + 2);
397           return -1;
398         }
399         cea608_field1[(*cea608_field1_len)++] = cc_data[i * 3 + 1];
400         cea608_field1[(*cea608_field1_len)++] = cc_data[i * 3 + 2];
401       }
402     } else if (cc_type == 0x01) {
403       if (!cc_valid)
404         continue;
405 
406       if (cea608_field2 && cea608_field2_len) {
407         if (*cea608_field2_len + 2 > field_2_len) {
408           GST_WARNING ("Too many cea608 input bytes %u for field 2",
409               *cea608_field2_len + 2);
410           return -1;
411         }
412         cea608_field2[(*cea608_field2_len)++] = cc_data[i * 3 + 1];
413         cea608_field2[(*cea608_field2_len)++] = cc_data[i * 3 + 2];
414       }
415     } else {
416       /* all cea608 packets must be at the beginning of a cc_data */
417       break;
418     }
419   }
420 
421   g_assert_cmpint (i * 3, <=, cc_data_len);
422 
423   GST_LOG ("Extracted cea608-1 of length %u and cea608-2 of length %u",
424       VAL_OR_0 (cea608_field1_len), VAL_OR_0 (cea608_field2_len));
425 
426   return i * 3;
427 }
428 
429 static GstFlowReturn
gst_line_21_encoder_transform_ip(GstVideoFilter * filter,GstVideoFrame * frame)430 gst_line_21_encoder_transform_ip (GstVideoFilter * filter,
431     GstVideoFrame * frame)
432 {
433   GstLine21Encoder *self = GST_LINE21ENCODER (filter);
434   GstVideoCaptionMeta *cc_meta;
435   guint8 *buf;
436   vbi_sliced sliced[2];
437   gpointer iter = NULL;
438   GstFlowReturn ret = GST_FLOW_ERROR;
439   guint offset;
440 
441   sliced[0].id = VBI_SLICED_CAPTION_525_F1;
442   sliced[0].line = self->sp.start[0];
443   sliced[1].id = VBI_SLICED_CAPTION_525_F2;
444   sliced[1].line = self->sp.start[1];
445 
446   sliced[0].data[0] = 0x80;
447   sliced[0].data[1] = 0x80;
448   sliced[1].data[0] = 0x80;
449   sliced[1].data[1] = 0x80;
450 
451   /* We loop over caption metas until we find the first CEA608 meta */
452   while ((cc_meta = (GstVideoCaptionMeta *)
453           gst_buffer_iterate_meta_filtered (frame->buffer, &iter,
454               GST_VIDEO_CAPTION_META_API_TYPE))) {
455     guint n = cc_meta->size;
456     guint i;
457 
458     if (cc_meta->caption_type == GST_VIDEO_CAPTION_TYPE_CEA708_CDP) {
459       guint8 cc_data[MAX_CDP_PACKET_LEN];
460       guint8 cea608_field1[MAX_CEA608_LEN];
461       guint8 cea608_field2[MAX_CEA608_LEN];
462       guint cea608_field1_len = MAX_CEA608_LEN;
463       guint cea608_field2_len = MAX_CEA608_LEN;
464       guint cc_data_len = 0;
465 
466       cc_data_len =
467           convert_cea708_cdp_cea708_cc_data_internal (self, cc_meta->data,
468           cc_meta->size, cc_data);
469 
470       cc_data_len = compact_cc_data (cc_data, cc_data_len);
471 
472       cc_data_extract_cea608 (cc_data, cc_data_len,
473           cea608_field1, &cea608_field1_len, cea608_field2, &cea608_field2_len);
474 
475       if (cea608_field1_len == 2) {
476         sliced[0].data[0] = cea608_field1[0];
477         sliced[0].data[1] = cea608_field1[1];
478       }
479 
480       if (cea608_field2_len == 2) {
481         sliced[1].data[0] = cea608_field2[0];
482         sliced[1].data[1] = cea608_field2[1];
483       }
484 
485       break;
486     } else if (cc_meta->caption_type == GST_VIDEO_CAPTION_TYPE_CEA608_S334_1A) {
487       if (n % 3 != 0) {
488         GST_ERROR_OBJECT (filter, "Invalid S334-1A CEA608 buffer size");
489         goto done;
490       }
491 
492       n /= 3;
493 
494       if (n >= 3) {
495         GST_ERROR_OBJECT (filter, "Too many S334-1A CEA608 triplets %u", n);
496         goto done;
497       }
498 
499       for (i = 0; i < n; i++) {
500         if (cc_meta->data[i * 3] & 0x80) {
501           sliced[0].data[0] = cc_meta->data[i * 3 + 1];
502           sliced[0].data[1] = cc_meta->data[i * 3 + 2];
503         } else {
504           sliced[1].data[0] = cc_meta->data[i * 3 + 1];
505           sliced[1].data[1] = cc_meta->data[i * 3 + 2];
506         }
507       }
508 
509       break;
510     }
511   }
512 
513   /* We've encoded this meta, it can now be removed if required */
514   if (cc_meta && self->remove_caption_meta)
515     gst_buffer_remove_meta (frame->buffer, (GstMeta *) cc_meta);
516 
517   /* When dealing with standard NTSC resolution, field 1 goes at line 21,
518    * when dealing with a reduced height the image has 3 VBI lines at the
519    * top and 3 at the bottom, and field 1 goes at line 1 */
520   offset = self->info.height == 525 ? 21 : 1;
521 
522   buf =
523       (guint8 *) GST_VIDEO_FRAME_PLANE_DATA (frame,
524       0) + offset * GST_VIDEO_INFO_COMP_STRIDE (&self->info, 0);
525 
526   if (!vbi_raw_video_image (buf, GST_VIDEO_INFO_COMP_STRIDE (&self->info,
527               0) * 2, &self->sp, 0, 0, 0, 0x000000FF, 0, sliced, 2)) {
528     GST_ERROR_OBJECT (filter, "Failed to encode CC data");
529     goto done;
530   }
531 
532   ret = GST_FLOW_OK;
533 
534 done:
535   return ret;
536 }
537