3 * Copyright (C) 2018 Sebastian Dröge <sebastian@centricular.com>
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.
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.
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.
27 #include <gst/base/base.h>
28 #include <gst/video/video.h>
31 #include "gstccconverter.h"
33 GST_DEBUG_CATEGORY_STATIC (gst_cc_converter_debug);
34 #define GST_CAT_DEFAULT gst_cc_converter_debug
37 * GstCCConverterCDPMode:
38 * @GST_CC_CONVERTER_CDP_MODE_TIME_CODE: Store time code information in CDP packets
39 * @GST_CC_CONVERTER_CDP_MODE_CC_DATA: Store CC data in CDP packets
40 * @GST_CC_CONVERTER_CDP_MODE_CC_SVC_INFO: Store CC service information in CDP packets
51 #define DEFAULT_CDP_MODE (GST_CC_CONVERTER_CDP_MODE_TIME_CODE | GST_CC_CONVERTER_CDP_MODE_CC_DATA | GST_CC_CONVERTER_CDP_MODE_CC_SVC_INFO)
53 /* Ordered by the amount of information they can contain */
55 "closedcaption/x-cea-708,format=(string) cdp; " \
56 "closedcaption/x-cea-708,format=(string) cc_data; " \
57 "closedcaption/x-cea-608,format=(string) s334-1a; " \
58 "closedcaption/x-cea-608,format=(string) raw"
60 #define VAL_OR_0(v) ((v) ? (*(v)) : 0)
62 static GstStaticPadTemplate sinktemplate = GST_STATIC_PAD_TEMPLATE ("sink",
65 GST_STATIC_CAPS (CC_CAPS));
67 static GstStaticPadTemplate srctemplate = GST_STATIC_PAD_TEMPLATE ("src",
70 GST_STATIC_CAPS (CC_CAPS));
72 #define parent_class gst_cc_converter_parent_class
73 G_DEFINE_TYPE (GstCCConverter, gst_cc_converter, GST_TYPE_BASE_TRANSFORM);
74 GST_ELEMENT_REGISTER_DEFINE (ccconverter, "ccconverter",
75 GST_RANK_NONE, GST_TYPE_CCCONVERTER);
77 #define GST_TYPE_CC_CONVERTER_CDP_MODE (gst_cc_converter_cdp_mode_get_type())
79 gst_cc_converter_cdp_mode_get_type (void)
81 static const GFlagsValue values[] = {
82 {GST_CC_CONVERTER_CDP_MODE_TIME_CODE,
83 "Store time code information in CDP packets", "time-code"},
84 {GST_CC_CONVERTER_CDP_MODE_CC_DATA, "Store CC data in CDP packets",
86 {GST_CC_CONVERTER_CDP_MODE_CC_SVC_INFO,
87 "Store CC service information in CDP packets", "cc-svc-info"},
92 if (g_once_init_enter ((gsize *) & id)) {
95 _id = g_flags_register_static ("GstCCConverterCDPMode", values);
97 g_once_init_leave ((gsize *) & id, _id);
104 gst_cc_converter_transform_size (GstBaseTransform * base,
105 GstPadDirection direction,
106 GstCaps * caps, gsize size, GstCaps * othercaps, gsize * othersize)
108 /* We can't really convert from an output size to an input size */
109 if (direction != GST_PAD_SINK)
112 /* Assume worst-case here and over-allocate, and in ::transform() we then
113 * downsize the buffer as needed. The worst-case is one CDP packet, which
114 * can be up to MAX_CDP_PACKET_LEN bytes large */
116 *othersize = MAX_CDP_PACKET_LEN;
122 gst_cc_converter_transform_caps (GstBaseTransform * base,
123 GstPadDirection direction, GstCaps * caps, GstCaps * filter)
125 static GstStaticCaps non_cdp_caps =
126 GST_STATIC_CAPS ("closedcaption/x-cea-708, format=(string)cc_data; "
127 "closedcaption/x-cea-608,format=(string) s334-1a; "
128 "closedcaption/x-cea-608,format=(string) raw");
129 static GstStaticCaps cdp_caps =
130 GST_STATIC_CAPS ("closedcaption/x-cea-708, format=(string)cdp");
131 static GstStaticCaps cdp_caps_framerate =
132 GST_STATIC_CAPS ("closedcaption/x-cea-708, format=(string)cdp, "
133 "framerate=(fraction){60/1, 60000/1001, 50/1, 30/1, 30000/1001, 25/1, 24/1, 24000/1001}");
135 GstCCConverter *self = GST_CCCONVERTER (base);
137 GstCaps *res, *templ;
139 templ = gst_pad_get_pad_template_caps (base->srcpad);
141 GST_DEBUG_OBJECT (self, "direction %s from caps %" GST_PTR_FORMAT,
142 direction == GST_PAD_SRC ? "src" : "sink", caps);
144 res = gst_caps_new_empty ();
145 n = gst_caps_get_size (caps);
146 for (i = 0; i < n; i++) {
147 const GstStructure *s = gst_caps_get_structure (caps, i);
148 const GValue *framerate = gst_structure_get_value (s, "framerate");
150 if (gst_structure_has_name (s, "closedcaption/x-cea-608")) {
152 if (direction == GST_PAD_SRC) {
153 /* SRC direction: We produce upstream caps
155 * Downstream wanted CEA608 caps. If it had a framerate, we
156 * also need upstream to provide exactly that same framerate
157 * and otherwise we don't care.
159 * We can convert everything to CEA608.
161 res = gst_caps_merge (res, gst_static_caps_get (&cdp_caps_framerate));
163 /* we can only keep the same framerate for non-cdp */
166 tmp = gst_caps_make_writable (gst_static_caps_get (&non_cdp_caps));
167 gst_caps_set_value (tmp, "framerate", framerate);
168 res = gst_caps_merge (res, tmp);
170 res = gst_caps_merge (res, gst_static_caps_get (&non_cdp_caps));
173 /* SINK: We produce downstream caps
175 * Upstream provided CEA608 caps. We can convert that to CDP if
176 * also a CDP compatible framerate was provided, and we can convert
177 * it to anything else regardless.
179 * If upstream provided a framerate we can pass that through, possibly
180 * filtered for the CDP case.
186 /* Create caps that contain the intersection of all framerates with
187 * the CDP allowed framerates */
189 gst_caps_make_writable (gst_static_caps_get
190 (&cdp_caps_framerate));
191 t = gst_caps_get_structure (tmp, 0);
192 gst_structure_set_name (t, "closedcaption/x-cea-608");
193 gst_structure_remove_field (t, "format");
194 if (gst_structure_can_intersect (s, t)) {
195 gst_caps_unref (tmp);
198 gst_caps_make_writable (gst_static_caps_get
199 (&cdp_caps_framerate));
201 res = gst_caps_merge (res, tmp);
203 gst_caps_unref (tmp);
205 /* And we can convert to everything else with the given framerate */
206 tmp = gst_caps_make_writable (gst_static_caps_get (&non_cdp_caps));
207 gst_caps_set_value (tmp, "framerate", framerate);
208 res = gst_caps_merge (res, tmp);
210 res = gst_caps_merge (res, gst_static_caps_get (&non_cdp_caps));
213 } else if (gst_structure_has_name (s, "closedcaption/x-cea-708")) {
214 if (direction == GST_PAD_SRC) {
215 /* SRC direction: We produce upstream caps
217 * Downstream wanted CEA708 caps. If downstream wants *only* CDP we
218 * either need CDP from upstream, or anything else with a CDP
220 * If downstream also wants non-CDP we can accept anything.
222 * We pass through any framerate as-is, except for filtering
223 * for CDP framerates if downstream wants only CDP.
226 if (g_strcmp0 (gst_structure_get_string (s, "format"), "cdp") == 0) {
227 /* Downstream wants only CDP */
229 /* We need CDP from upstream in that case */
230 res = gst_caps_merge (res, gst_static_caps_get (&cdp_caps_framerate));
232 /* Or anything else with a CDP framerate */
236 const GValue *cdp_framerate;
238 /* Create caps that contain the intersection of all framerates with
239 * the CDP allowed framerates */
241 gst_caps_make_writable (gst_static_caps_get
242 (&cdp_caps_framerate));
243 t = gst_caps_get_structure (tmp, 0);
245 /* There's an intersection between the framerates so we can convert
246 * into CDP with exactly those framerates from anything else */
247 cdp_framerate = gst_structure_get_value (t, "framerate");
248 tmp = gst_caps_make_writable (gst_static_caps_get (&non_cdp_caps));
249 gst_caps_set_value (tmp, "framerate", cdp_framerate);
250 res = gst_caps_merge (res, tmp);
252 GstCaps *tmp, *cdp_caps;
253 const GValue *cdp_framerate;
255 /* Get all CDP framerates, we can accept anything that has those
257 cdp_caps = gst_static_caps_get (&cdp_caps_framerate);
259 gst_structure_get_value (gst_caps_get_structure (cdp_caps, 0),
262 tmp = gst_caps_make_writable (gst_static_caps_get (&non_cdp_caps));
263 gst_caps_set_value (tmp, "framerate", cdp_framerate);
264 gst_caps_unref (cdp_caps);
266 res = gst_caps_merge (res, tmp);
269 /* Downstream wants not only CDP, we can do everything */
270 res = gst_caps_merge (res, gst_static_caps_get (&cdp_caps_framerate));
272 /* we can only keep the same framerate for non-cdp */
275 tmp = gst_caps_make_writable (gst_static_caps_get (&non_cdp_caps));
276 gst_caps_set_value (tmp, "framerate", framerate);
277 res = gst_caps_merge (res, tmp);
279 res = gst_caps_merge (res, gst_static_caps_get (&non_cdp_caps));
285 /* SINK: We produce downstream caps
287 * Upstream provided CEA708 caps. If upstream provided CDP we can
288 * output CDP, no matter what (-> passthrough). If upstream did not
289 * provide CDP, we can output CDP only if the framerate fits.
290 * We can always produce everything else apart from CDP.
292 * If upstream provided a framerate we pass that through for non-CDP
293 * output, and pass it through filtered for CDP output.
296 if (gst_structure_can_intersect (s,
297 gst_caps_get_structure (gst_static_caps_get (&cdp_caps), 0))) {
298 /* Upstream provided CDP caps, we can do everything independent of
300 res = gst_caps_merge (res, gst_static_caps_get (&cdp_caps_framerate));
301 } else if (framerate) {
302 const GValue *cdp_framerate;
305 /* Upstream did not provide CDP. We can only do CDP if upstream
306 * happened to have a CDP framerate */
308 /* Create caps that contain the intersection of all framerates with
309 * the CDP allowed framerates */
311 gst_caps_make_writable (gst_static_caps_get
312 (&cdp_caps_framerate));
313 t = gst_caps_get_structure (tmp, 0);
315 /* There's an intersection between the framerates so we can convert
316 * into CDP with exactly those framerates */
317 cdp_framerate = gst_structure_get_value (t, "framerate");
318 if (gst_value_intersect (NULL, cdp_framerate, framerate)) {
319 gst_caps_set_value (tmp, "framerate", cdp_framerate);
321 res = gst_caps_merge (res, tmp);
323 gst_clear_caps (&tmp);
326 /* We can always convert CEA708 to all non-CDP formats */
328 /* we can only keep the same framerate for non-cdp */
331 tmp = gst_caps_make_writable (gst_static_caps_get (&non_cdp_caps));
332 gst_caps_set_value (tmp, "framerate", framerate);
333 res = gst_caps_merge (res, tmp);
335 res = gst_caps_merge (res, gst_static_caps_get (&non_cdp_caps));
339 g_assert_not_reached ();
343 GST_DEBUG_OBJECT (self, "pre filter caps %" GST_PTR_FORMAT, res);
345 /* We can convert anything into anything but it might involve loss of
346 * information so always filter according to the order in our template caps
350 filter = gst_caps_intersect_full (templ, filter, GST_CAPS_INTERSECT_FIRST);
352 tmp = gst_caps_intersect_full (filter, res, GST_CAPS_INTERSECT_FIRST);
353 gst_caps_unref (res);
354 gst_caps_unref (filter);
358 gst_caps_unref (templ);
360 GST_DEBUG_OBJECT (self, "Transformed in direction %s caps %" GST_PTR_FORMAT,
361 direction == GST_PAD_SRC ? "src" : "sink", caps);
362 GST_DEBUG_OBJECT (self, "filter %" GST_PTR_FORMAT, filter);
363 GST_DEBUG_OBJECT (self, "to %" GST_PTR_FORMAT, res);
369 gst_cc_converter_fixate_caps (GstBaseTransform * base,
370 GstPadDirection direction, GstCaps * incaps, GstCaps * outcaps)
372 GstCCConverter *self = GST_CCCONVERTER (base);
373 const GstStructure *s;
375 const GValue *framerate;
376 GstCaps *intersection, *templ;
378 GST_DEBUG_OBJECT (self, "Fixating in direction %s incaps %" GST_PTR_FORMAT,
379 direction == GST_PAD_SRC ? "src" : "sink", incaps);
380 GST_DEBUG_OBJECT (self, "and outcaps %" GST_PTR_FORMAT, outcaps);
382 /* Prefer passthrough if we can */
383 if (gst_caps_is_subset (incaps, outcaps)) {
384 gst_caps_unref (outcaps);
385 return GST_BASE_TRANSFORM_CLASS (parent_class)->fixate_caps (base,
386 direction, incaps, gst_caps_ref (incaps));
389 /* Otherwise prefer caps in the order of our template caps */
390 templ = gst_pad_get_pad_template_caps (base->srcpad);
392 gst_caps_intersect_full (templ, outcaps, GST_CAPS_INTERSECT_FIRST);
393 gst_caps_unref (outcaps);
394 outcaps = intersection;
397 GST_BASE_TRANSFORM_CLASS (parent_class)->fixate_caps (base, direction,
400 s = gst_caps_get_structure (incaps, 0);
401 framerate = gst_structure_get_value (s, "framerate");
402 outcaps = gst_caps_make_writable (outcaps);
403 t = gst_caps_get_structure (outcaps, 0);
405 /* remove any output framerate that might've been added by basetransform
406 * due to intersecting with downstream */
407 gst_structure_remove_field (t, "framerate");
409 /* or passthrough the input framerate if possible */
412 n = gst_value_get_fraction_numerator (framerate);
413 d = gst_value_get_fraction_denominator (framerate);
415 if (gst_structure_has_field (t, "framerate"))
416 gst_structure_fixate_field_nearest_fraction (t, "framerate", n, d);
418 gst_structure_set (t, "framerate", GST_TYPE_FRACTION, n, d, NULL);
421 GST_DEBUG_OBJECT (self,
422 "Fixated caps %" GST_PTR_FORMAT " to %" GST_PTR_FORMAT, incaps, outcaps);
428 gst_cc_converter_set_caps (GstBaseTransform * base, GstCaps * incaps,
431 GstCCConverter *self = GST_CCCONVERTER (base);
432 const GstStructure *s;
433 gboolean passthrough;
435 self->input_caption_type = gst_video_caption_type_from_caps (incaps);
436 self->output_caption_type = gst_video_caption_type_from_caps (outcaps);
438 if (self->input_caption_type == GST_VIDEO_CAPTION_TYPE_UNKNOWN ||
439 self->output_caption_type == GST_VIDEO_CAPTION_TYPE_UNKNOWN)
442 s = gst_caps_get_structure (incaps, 0);
443 if (!gst_structure_get_fraction (s, "framerate", &self->in_fps_n,
445 self->in_fps_n = self->in_fps_d = 0;
447 s = gst_caps_get_structure (outcaps, 0);
448 if (!gst_structure_get_fraction (s, "framerate", &self->out_fps_n,
450 self->out_fps_n = self->out_fps_d = 0;
452 gst_video_time_code_clear (&self->current_output_timecode);
454 /* Caps can be different but we can passthrough as long as they can
455 * intersect, i.e. have same caps name and format */
456 passthrough = gst_caps_can_intersect (incaps, outcaps);
457 gst_base_transform_set_passthrough (base, passthrough);
459 GST_DEBUG_OBJECT (self,
460 "Got caps %" GST_PTR_FORMAT " to %" GST_PTR_FORMAT " (passthrough %d)",
461 incaps, outcaps, passthrough);
467 GST_ERROR_OBJECT (self,
468 "Invalid caps: in %" GST_PTR_FORMAT " out: %" GST_PTR_FORMAT, incaps,
480 guint max_cea608_count;
483 static const struct cdp_fps_entry cdp_fps_table[] = {
484 {0x1f, 24000, 1001, 25, 22, 3 /* FIXME: alternating max cea608 count! */ },
485 {0x2f, 24, 1, 25, 22, 2},
486 {0x3f, 25, 1, 24, 22, 2},
487 {0x4f, 30000, 1001, 20, 18, 2},
488 {0x5f, 30, 1, 20, 18, 2},
489 {0x6f, 50, 1, 12, 11, 1},
490 {0x7f, 60000, 1001, 10, 9, 1},
491 {0x8f, 60, 1, 10, 9, 1},
493 static const struct cdp_fps_entry null_fps_entry = { 0, 0, 0, 0 };
495 static const struct cdp_fps_entry *
496 cdp_fps_entry_from_id (guint8 id)
499 for (i = 0; i < G_N_ELEMENTS (cdp_fps_table); i++) {
500 if (cdp_fps_table[i].fps_idx == id)
501 return &cdp_fps_table[i];
503 return &null_fps_entry;
506 static const struct cdp_fps_entry *
507 cdp_fps_entry_from_fps (guint fps_n, guint fps_d)
510 for (i = 0; i < G_N_ELEMENTS (cdp_fps_table); i++) {
511 if (cdp_fps_table[i].fps_n == fps_n && cdp_fps_table[i].fps_d == fps_d)
512 return &cdp_fps_table[i];
514 return &null_fps_entry;
518 get_framerate_output_scale (GstCCConverter * self,
519 const struct cdp_fps_entry *in_fps_entry, gint * scale_n, gint * scale_d)
521 if (self->in_fps_n == 0 || self->out_fps_d == 0) {
527 /* compute the relative rates of the two framerates */
528 if (!gst_util_fraction_multiply (in_fps_entry->fps_d, in_fps_entry->fps_n,
529 self->out_fps_n, self->out_fps_d, scale_n, scale_d))
530 /* we should never overflow */
531 g_assert_not_reached ();
535 interpolate_time_code_with_framerate (GstCCConverter * self,
536 const GstVideoTimeCode * tc, gint out_fps_n, gint out_fps_d,
537 gint scale_n, gint scale_d, GstVideoTimeCode * out)
540 gint output_n, output_d;
542 GstVideoTimeCodeFlags flags;
544 g_return_val_if_fail (tc != NULL, FALSE);
545 g_return_val_if_fail (out != NULL, FALSE);
546 /* out_n/d can only be 0 if scale_n/d are 1/1 */
547 g_return_val_if_fail ((scale_n == 1 && scale_d == 1) || (out_fps_n != 0
548 && out_fps_d != 0), FALSE);
550 if (!tc || tc->config.fps_n == 0)
553 if (!gst_util_fraction_multiply (tc->frames, 1, scale_n, scale_d, &output_n,
555 /* we should never overflow */
556 g_assert_not_reached ();
558 tc_str = gst_video_time_code_to_string (tc);
559 GST_TRACE_OBJECT (self, "interpolating time code %s with scale %d/%d "
560 "to frame %d/%d", tc_str, scale_n, scale_d, output_n, output_d);
563 if (out_fps_n == 0 || out_fps_d == 0) {
564 out_fps_n = tc->config.fps_n;
565 out_fps_d = tc->config.fps_d;
568 flags = tc->config.flags;
569 if ((flags & GST_VIDEO_TIME_CODE_FLAGS_DROP_FRAME) != 0 && out_fps_d != 1001
570 && out_fps_n != 60000 && out_fps_n != 30000) {
571 flags &= ~GST_VIDEO_TIME_CODE_FLAGS_DROP_FRAME;
572 } else if ((flags & GST_VIDEO_TIME_CODE_FLAGS_DROP_FRAME) == 0
573 && out_fps_d == 1001 && (out_fps_n == 60000 || out_fps_n == 30000)) {
574 /* XXX: theoretically, not quite correct however this is an assumption
575 * we have elsewhere that these framerates are always drop-framed */
576 flags |= GST_VIDEO_TIME_CODE_FLAGS_DROP_FRAME;
579 output_frame = output_n / output_d;
581 *out = (GstVideoTimeCode) GST_VIDEO_TIME_CODE_INIT;
583 /* here we try to find the next available valid timecode. The dropped
584 * (when they exist) frames in time codes are that the beginning of each
586 gst_video_time_code_clear (out);
587 gst_video_time_code_init (out, out_fps_n, out_fps_d,
588 tc->config.latest_daily_jam, flags, tc->hours, tc->minutes,
589 tc->seconds, output_frame, tc->field_count);
591 } while ((flags & GST_VIDEO_TIME_CODE_FLAGS_DROP_FRAME) != 0
592 && output_frame < 10 && !gst_video_time_code_is_valid (out));
594 tc_str = gst_video_time_code_to_string (out);
595 GST_TRACE_OBJECT (self, "interpolated to %s", tc_str);
601 /* remove padding bytes from a cc_data packet. Returns the length of the new
602 * data in @cc_data */
604 compact_cc_data (guint8 * cc_data, guint cc_data_len)
606 gboolean started_ccp = FALSE;
610 if (cc_data_len % 3 != 0) {
611 GST_WARNING ("Invalid cc_data buffer size");
612 cc_data_len = cc_data_len - (cc_data_len % 3);
615 for (i = 0; i < cc_data_len / 3; i++) {
616 gboolean cc_valid = (cc_data[i * 3] & 0x04) == 0x04;
617 guint8 cc_type = cc_data[i * 3] & 0x03;
619 if (!started_ccp && (cc_type == 0x00 || cc_type == 0x01)) {
621 /* copy over valid 608 data */
622 cc_data[out_len++] = cc_data[i * 3];
623 cc_data[out_len++] = cc_data[i * 3 + 1];
624 cc_data[out_len++] = cc_data[i * 3 + 2];
635 if (cc_type == 0x00 || cc_type == 0x01) {
636 GST_WARNING ("Invalid cc_data. cea608 bytes after cea708");
640 cc_data[out_len++] = cc_data[i * 3];
641 cc_data[out_len++] = cc_data[i * 3 + 1];
642 cc_data[out_len++] = cc_data[i * 3 + 2];
645 GST_LOG ("compacted cc_data from %u to %u", cc_data_len, out_len);
651 cc_data_extract_cea608 (guint8 * cc_data, guint cc_data_len,
652 guint8 * cea608_field1, guint * cea608_field1_len,
653 guint8 * cea608_field2, guint * cea608_field2_len)
655 guint i, field_1_len = 0, field_2_len = 0;
657 if (cea608_field1_len) {
658 field_1_len = *cea608_field1_len;
659 *cea608_field1_len = 0;
661 if (cea608_field2_len) {
662 field_2_len = *cea608_field2_len;
663 *cea608_field2_len = 0;
666 if (cc_data_len % 3 != 0) {
667 GST_WARNING ("Invalid cc_data buffer size %u. Truncating to a multiple "
668 "of 3", cc_data_len);
669 cc_data_len = cc_data_len - (cc_data_len % 3);
672 for (i = 0; i < cc_data_len / 3; i++) {
673 gboolean cc_valid = (cc_data[i * 3] & 0x04) == 0x04;
674 guint8 cc_type = cc_data[i * 3] & 0x03;
676 GST_TRACE ("0x%02x 0x%02x 0x%02x, valid: %u, type: 0b%u%u",
677 cc_data[i * 3 + 0], cc_data[i * 3 + 1], cc_data[i * 3 + 2], cc_valid,
678 cc_type & 0x2, cc_type & 0x1);
680 if (cc_type == 0x00) {
684 if (cea608_field1 && cea608_field1_len) {
685 if (*cea608_field1_len + 2 > field_1_len) {
686 GST_WARNING ("Too many cea608 input bytes %u for field 1",
687 *cea608_field1_len + 2);
690 cea608_field1[(*cea608_field1_len)++] = cc_data[i * 3 + 1];
691 cea608_field1[(*cea608_field1_len)++] = cc_data[i * 3 + 2];
693 } else if (cc_type == 0x01) {
697 if (cea608_field2 && cea608_field2_len) {
698 if (*cea608_field2_len + 2 > field_2_len) {
699 GST_WARNING ("Too many cea608 input bytes %u for field 2",
700 *cea608_field2_len + 2);
703 cea608_field2[(*cea608_field2_len)++] = cc_data[i * 3 + 1];
704 cea608_field2[(*cea608_field2_len)++] = cc_data[i * 3 + 2];
707 /* all cea608 packets must be at the beginning of a cc_data */
712 g_assert_cmpint (i * 3, <=, cc_data_len);
714 GST_LOG ("Extracted cea608-1 of length %u and cea608-2 of length %u",
715 VAL_OR_0 (cea608_field1_len), VAL_OR_0 (cea608_field2_len));
721 store_cc_data (GstCCConverter * self, const guint8 * ccp_data,
722 guint ccp_data_len, const guint8 * cea608_1, guint cea608_1_len,
723 const guint8 * cea608_2, guint cea608_2_len)
725 GST_DEBUG_OBJECT (self, "holding data of len ccp:%u, cea608 1:%u, "
726 "cea608 2:%u until next input buffer", ccp_data_len, cea608_1_len,
729 if (ccp_data && ccp_data_len > 0) {
730 memcpy (self->scratch_ccp, ccp_data, ccp_data_len);
731 self->scratch_ccp_len = ccp_data_len;
733 self->scratch_ccp_len = 0;
735 g_assert_cmpint (self->scratch_ccp_len, <, sizeof (self->scratch_ccp));
737 if (cea608_1 && cea608_1_len > 0) {
738 memcpy (self->scratch_cea608_1, cea608_1, cea608_1_len);
739 self->scratch_cea608_1_len = cea608_1_len;
741 self->scratch_cea608_1_len = 0;
743 g_assert_cmpint (self->scratch_cea608_1_len, <,
744 sizeof (self->scratch_cea608_1));
746 if (cea608_2 && cea608_2_len > 0) {
747 memcpy (self->scratch_cea608_2, cea608_2, cea608_2_len);
748 self->scratch_cea608_2_len = cea608_2_len;
750 self->scratch_cea608_2_len = 0;
752 g_assert_cmpint (self->scratch_cea608_2_len, <,
753 sizeof (self->scratch_cea608_2));
757 combine_cc_data (GstCCConverter * self, gboolean pad_cea608,
758 const struct cdp_fps_entry *out_fps_entry, const guint8 * ccp_data,
759 guint ccp_data_len, const guint8 * cea608_1, guint cea608_1_len,
760 const guint8 * cea608_2, guint cea608_2_len, guint8 * out, guint * out_size)
762 guint i = 0, out_i = 0, max_size = 0, cea608_1_i = 0, cea608_2_i = 0;
763 guint cea608_output_count;
764 guint total_cea608_1_count, total_cea608_2_count;
768 g_assert (!ccp_data || ccp_data_len % 3 == 0);
769 g_assert (!cea608_1 || cea608_1_len % 2 == 0);
770 g_assert (!cea608_2 || cea608_2_len % 2 == 0);
774 /* FIXME: if cea608 field 2 is generated, field 1 needs to be generated,
775 * However that is not possible for 60fps (where only one cea608 field fits)
776 * without adding previous output buffer tracking */
777 g_assert_cmpint (cea608_1_len >= cea608_2_len);
779 g_assert_cmpint (cea608_1_len + cea608_2_len, <=,
780 out_fps_entry->max_cea608_count);
782 total_cea608_1_count = cea608_1_len;
783 total_cea608_2_count = cea608_2_len;
786 /* FIXME: if cea608 field 2 is generated, field 1 needs to be generated. */
787 if (cea608_1_len < cea608_2_len)
788 total_cea608_1_count += cea608_2_len - cea608_1_len;
791 max_size = ccp_data_len + (total_cea608_1_count + total_cea608_2_count) * 3;
792 if (*out_size < max_size) {
793 GST_WARNING_OBJECT (self, "Output data too small (%u < %u)", *out_size,
798 /* FIXME: interlacing, tff, rff, ensuring cea608 field1 is generated if
799 * field2 exists even across packets */
801 cea608_output_count = cea608_1_len + cea608_2_len;
803 for (i = total_cea608_1_count + total_cea608_2_count;
804 i < out_fps_entry->max_cea608_count; i++) {
805 /* try to pad evenly */
806 if (i > cea608_1_len / 2)
807 total_cea608_1_count++;
809 total_cea608_2_count++;
810 cea608_output_count++;
814 GST_LOG ("writing %u cea608-1 fields and %u cea608-2 fields",
815 total_cea608_1_count, total_cea608_2_count);
816 g_assert_cmpint (total_cea608_1_count + total_cea608_2_count, <=,
817 out_fps_entry->max_cea608_count);
819 while (cea608_1_i + cea608_2_i < cea608_output_count) {
820 if (cea608_1_i < cea608_1_len) {
822 out[out_i++] = cea608_1[cea608_1_i * 2];
823 out[out_i++] = cea608_1[cea608_1_i * 2 + 1];
826 } else if (cea608_1_i < total_cea608_1_count) {
834 if (cea608_2_i < cea608_2_len) {
836 out[out_i++] = cea608_2[cea608_2_i * 2];
837 out[out_i++] = cea608_2[cea608_2_i * 2 + 1];
840 } else if (cea608_2_i < total_cea608_2_count) {
849 g_assert_cmpint (out_i / 3, <=, out_fps_entry->max_cea608_count);
854 memcpy (&out[out_i], ccp_data, ccp_data_len);
855 *out_size += ccp_data_len;
858 g_assert_cmpint (*out_size, <, MAX_CDP_PACKET_LEN);
863 /* takes cc_data cea608_1, cea608_2 and attempts to fit it into a hypothetical
864 * output packet. Any leftover data is stored for later addition. Returns
865 * whether any output can be generated. @ccp_data_len, @cea608_1_len,
866 * @cea608_2_len are also updated to reflect the size of that data to add to
867 * the output packet */
869 fit_and_scale_cc_data (GstCCConverter * self,
870 const struct cdp_fps_entry *in_fps_entry,
871 const struct cdp_fps_entry *out_fps_entry, const guint8 * ccp_data,
872 guint * ccp_data_len, const guint8 * cea608_1, guint * cea608_1_len,
873 const guint8 * cea608_2, guint * cea608_2_len, const GstVideoTimeCode * tc)
875 if (!in_fps_entry || in_fps_entry->fps_n == 0) {
876 in_fps_entry = cdp_fps_entry_from_fps (self->in_fps_n, self->in_fps_d);
877 if (!in_fps_entry || in_fps_entry->fps_n == 0)
878 g_assert_not_reached ();
881 /* This is slightly looser than checking for the exact framerate as the cdp
882 * spec allow for 0.1% difference between framerates to be considered equal */
883 if (in_fps_entry->max_cc_count == out_fps_entry->max_cc_count) {
884 if (tc && tc->config.fps_n != 0)
885 interpolate_time_code_with_framerate (self, tc, out_fps_entry->fps_n,
886 out_fps_entry->fps_d, 1, 1, &self->current_output_timecode);
888 int input_frame_n, input_frame_d, output_frame_n, output_frame_d;
889 int output_time_cmp, scale_n, scale_d, rate_cmp;
891 /* TODO: handle input discont */
893 /* compute the relative frame count for each */
894 if (!gst_util_fraction_multiply (self->in_fps_d, self->in_fps_n,
895 self->input_frames, 1, &input_frame_n, &input_frame_d))
896 /* we should never overflow */
897 g_assert_not_reached ();
899 if (!gst_util_fraction_multiply (self->out_fps_d, self->out_fps_n,
900 self->output_frames, 1, &output_frame_n, &output_frame_d))
901 /* we should never overflow */
902 g_assert_not_reached ();
904 output_time_cmp = gst_util_fraction_compare (input_frame_n, input_frame_d,
905 output_frame_n, output_frame_d);
907 /* compute the relative rates of the two framerates */
908 get_framerate_output_scale (self, in_fps_entry, &scale_n, &scale_d);
910 rate_cmp = gst_util_fraction_compare (scale_n, scale_d, 1, 1);
912 GST_TRACE_OBJECT (self, "performing framerate conversion at scale %d/%d "
913 "of cc data of with sizes, ccp:%u, cea608-1:%u, cea608-2:%u", scale_n,
914 scale_d, VAL_OR_0 (ccp_data_len), VAL_OR_0 (cea608_1_len),
915 VAL_OR_0 (cea608_2_len));
918 /* we are not scaling. Should never happen with current conditions
920 g_assert_not_reached ();
921 } else if (output_time_cmp < 0) {
922 /* we can't generate an output yet */
923 guint cd_len = ccp_data_len ? *ccp_data_len : 0;
924 guint c1_len = cea608_1_len ? *cea608_1_len : 0;
925 guint c2_len = cea608_2_len ? *cea608_2_len : 0;
927 store_cc_data (self, ccp_data, cd_len, cea608_1, c1_len, cea608_2,
936 } else if (rate_cmp != 0) {
937 /* we are changing the framerate and may overflow the max output packet
938 * size. Split them where necessary. */
939 gint extra_ccp = 0, extra_cea608_1 = 0, extra_cea608_2 = 0;
940 gint ccp_off = 0, cea608_1_off = 0, cea608_2_off = 0;
942 if (output_time_cmp == 0) {
943 /* we have completed a cycle and can reset our counters to avoid
944 * overflow. Anything that fits into the output packet will be written */
945 GST_LOG_OBJECT (self, "cycle completed, resetting frame counters");
946 self->scratch_ccp_len = 0;
947 self->scratch_cea608_1_len = 0;
948 self->scratch_cea608_2_len = 0;
949 self->input_frames = 0;
950 self->output_frames = 0;
954 extra_ccp = *ccp_data_len - 3 * out_fps_entry->max_ccp_count;
955 extra_ccp = MAX (0, extra_ccp);
956 ccp_off = *ccp_data_len - extra_ccp;
960 extra_cea608_1 = *cea608_1_len - 2 * out_fps_entry->max_cea608_count;
961 extra_cea608_1 = MAX (0, extra_cea608_1);
962 cea608_1_off = *cea608_1_len - extra_cea608_1;
966 /* this prefers using field1 data first. This may not be quite correct */
967 if (extra_cea608_1 > 0) {
968 /* all the cea608 space is for field 1 */
969 extra_cea608_2 = *cea608_2_len;
971 } else if (cea608_1_len) {
972 /* cea608 space is shared between field 1 and field 2 */
974 *cea608_1_len + *cea608_2_len -
975 2 * out_fps_entry->max_cea608_count;
976 extra_cea608_2 = MAX (0, extra_cea608_2);
977 cea608_2_off = *cea608_2_len - extra_cea608_2;
979 /* all of the cea608 space is for field 2 */
980 extra_cea608_2 = *cea608_2_len - 2 * out_fps_entry->max_cea608_count;
981 extra_cea608_2 = MAX (0, extra_cea608_2);
982 cea608_2_off = *cea608_2_len - extra_cea608_2;
986 if (extra_ccp > 0 || extra_cea608_1 > 0 || extra_cea608_2 > 0) {
987 /* packet would overflow, push extra bytes into the next packet */
988 GST_DEBUG_OBJECT (self, "buffer would overflow by %u ccp bytes, "
989 "%u cea608 field 1 bytes, or %u cea608 field 2 bytes", extra_ccp,
990 extra_cea608_1, extra_cea608_2);
991 store_cc_data (self, &ccp_data[ccp_off], extra_ccp,
992 &cea608_1[cea608_1_off], extra_cea608_1, &cea608_2[cea608_2_off],
995 *ccp_data_len = MIN (*ccp_data_len, ccp_off);
997 *cea608_1_len = MIN (*cea608_1_len, cea608_1_off);
999 *cea608_2_len = MIN (*cea608_2_len, cea608_2_off);
1001 GST_DEBUG_OBJECT (self, "section sizes of %u ccp bytes, "
1002 "%u cea608 field 1 bytes, and %u cea608 field 2 bytes fit within "
1003 "output packet", VAL_OR_0 (ccp_data_len), VAL_OR_0 (cea608_1_len),
1004 VAL_OR_0 (cea608_2_len));
1005 self->scratch_ccp_len = 0;
1006 self->scratch_cea608_1_len = 0;
1007 self->scratch_cea608_2_len = 0;
1010 g_assert_not_reached ();
1013 if (tc && tc->config.fps_n != 0)
1014 interpolate_time_code_with_framerate (self, tc, out_fps_entry->fps_n,
1015 out_fps_entry->fps_d, scale_n, scale_d,
1016 &self->current_output_timecode);
1019 g_assert_cmpint (VAL_OR_0 (ccp_data_len) + (VAL_OR_0 (cea608_1_len) +
1020 VAL_OR_0 (cea608_2_len)) / 2 * 3, <=,
1021 3 * out_fps_entry->max_cc_count);
1023 GST_DEBUG_OBJECT (self, "write out packet with lengths ccp:%u, cea608-1:%u, "
1024 "cea608-2:%u", VAL_OR_0 (ccp_data_len), VAL_OR_0 (cea608_1_len),
1025 VAL_OR_0 (cea608_2_len));
1030 /* Converts raw CEA708 cc_data and an optional timecode into CDP */
1032 convert_cea708_cc_data_cea708_cdp_internal (GstCCConverter * self,
1033 const guint8 * cc_data, guint cc_data_len, guint8 * cdp, guint cdp_len,
1034 const GstVideoTimeCode * tc, const struct cdp_fps_entry *fps_entry)
1037 guint8 flags, checksum;
1040 GST_DEBUG_OBJECT (self, "writing out cdp packet from cc_data with length %u",
1043 gst_byte_writer_init_with_data (&bw, cdp, cdp_len, FALSE);
1044 gst_byte_writer_put_uint16_be_unchecked (&bw, 0x9669);
1045 /* Write a length of 0 for now */
1046 gst_byte_writer_put_uint8_unchecked (&bw, 0);
1048 gst_byte_writer_put_uint8_unchecked (&bw, fps_entry->fps_idx);
1050 if (cc_data_len / 3 > fps_entry->max_cc_count) {
1051 GST_WARNING_OBJECT (self, "Too many cc_data triplets for framerate: %u. "
1052 "Truncating to %u", cc_data_len / 3, fps_entry->max_cc_count);
1053 cc_data_len = 3 * fps_entry->max_cc_count;
1056 /* caption_service_active */
1059 /* ccdata_present */
1060 if ((self->cdp_mode & GST_CC_CONVERTER_CDP_MODE_CC_DATA))
1063 /* time_code_present */
1064 if ((self->cdp_mode & GST_CC_CONVERTER_CDP_MODE_TIME_CODE) && tc
1065 && tc->config.fps_n > 0)
1071 gst_byte_writer_put_uint8_unchecked (&bw, flags);
1073 gst_byte_writer_put_uint16_be_unchecked (&bw, self->cdp_hdr_sequence_cntr);
1075 if ((self->cdp_mode & GST_CC_CONVERTER_CDP_MODE_TIME_CODE) && tc
1076 && tc->config.fps_n > 0) {
1079 gst_byte_writer_put_uint8_unchecked (&bw, 0x71);
1080 /* reserved 11 - 2 bits */
1082 /* tens of hours - 2 bits */
1083 u8 |= ((tc->hours / 10) & 0x3) << 4;
1084 /* units of hours - 4 bits */
1085 u8 |= (tc->hours % 10) & 0xf;
1086 gst_byte_writer_put_uint8_unchecked (&bw, u8);
1088 /* reserved 1 - 1 bit */
1090 /* tens of minutes - 3 bits */
1091 u8 |= ((tc->minutes / 10) & 0x7) << 4;
1092 /* units of minutes - 4 bits */
1093 u8 |= (tc->minutes % 10) & 0xf;
1094 gst_byte_writer_put_uint8_unchecked (&bw, u8);
1096 /* field flag - 1 bit */
1097 u8 = tc->field_count < 2 ? 0x00 : 0x80;
1098 /* tens of seconds - 3 bits */
1099 u8 |= ((tc->seconds / 10) & 0x7) << 4;
1100 /* units of seconds - 4 bits */
1101 u8 |= (tc->seconds % 10) & 0xf;
1102 gst_byte_writer_put_uint8_unchecked (&bw, u8);
1104 /* drop frame flag - 1 bit */
1105 u8 = (tc->config.flags & GST_VIDEO_TIME_CODE_FLAGS_DROP_FRAME) ? 0x80 :
1107 /* reserved0 - 1 bit */
1108 /* tens of frames - 2 bits */
1109 u8 |= ((tc->frames / 10) & 0x3) << 4;
1110 /* units of frames 4 bits */
1111 u8 |= (tc->frames % 10) & 0xf;
1112 gst_byte_writer_put_uint8_unchecked (&bw, u8);
1115 if ((self->cdp_mode & GST_CC_CONVERTER_CDP_MODE_CC_DATA)) {
1116 gst_byte_writer_put_uint8_unchecked (&bw, 0x72);
1117 gst_byte_writer_put_uint8_unchecked (&bw, 0xe0 | fps_entry->max_cc_count);
1118 gst_byte_writer_put_data_unchecked (&bw, cc_data, cc_data_len);
1119 while (fps_entry->max_cc_count > cc_data_len / 3) {
1120 gst_byte_writer_put_uint8_unchecked (&bw, 0xfa);
1121 gst_byte_writer_put_uint8_unchecked (&bw, 0x00);
1122 gst_byte_writer_put_uint8_unchecked (&bw, 0x00);
1127 gst_byte_writer_put_uint8_unchecked (&bw, 0x74);
1128 gst_byte_writer_put_uint16_be_unchecked (&bw, self->cdp_hdr_sequence_cntr);
1129 self->cdp_hdr_sequence_cntr++;
1130 /* We calculate the checksum afterwards */
1131 gst_byte_writer_put_uint8_unchecked (&bw, 0);
1133 len = gst_byte_writer_get_pos (&bw);
1134 gst_byte_writer_set_pos (&bw, 2);
1135 gst_byte_writer_put_uint8_unchecked (&bw, len);
1138 for (i = 0; i < len; i++) {
1142 checksum = 256 - checksum;
1143 cdp[len - 1] = checksum;
1148 /* Converts CDP into raw CEA708 cc_data */
1150 convert_cea708_cdp_cea708_cc_data_internal (GstCCConverter * self,
1151 const guint8 * cdp, guint cdp_len, guint8 cc_data[MAX_CDP_PACKET_LEN],
1152 GstVideoTimeCode * tc, const struct cdp_fps_entry **out_fps_entry)
1159 const struct cdp_fps_entry *fps_entry;
1161 *out_fps_entry = &null_fps_entry;
1162 memset (tc, 0, sizeof (*tc));
1164 /* Header + footer length */
1166 GST_WARNING_OBJECT (self, "cdp packet too short (%u). expected at "
1167 "least %u", cdp_len, 11);
1171 gst_byte_reader_init (&br, cdp, cdp_len);
1172 u16 = gst_byte_reader_get_uint16_be_unchecked (&br);
1173 if (u16 != 0x9669) {
1174 GST_WARNING_OBJECT (self, "cdp packet does not have initial magic bytes "
1179 u8 = gst_byte_reader_get_uint8_unchecked (&br);
1180 if (u8 != cdp_len) {
1181 GST_WARNING_OBJECT (self, "cdp packet length (%u) does not match passed "
1182 "in value (%u)", u8, cdp_len);
1186 u8 = gst_byte_reader_get_uint8_unchecked (&br);
1187 fps_entry = cdp_fps_entry_from_id (u8);
1188 if (!fps_entry || fps_entry->fps_n == 0) {
1189 GST_WARNING_OBJECT (self, "cdp packet does not have a valid framerate "
1194 flags = gst_byte_reader_get_uint8_unchecked (&br);
1196 if ((flags & 0x40) == 0) {
1197 GST_DEBUG_OBJECT (self, "cdp packet does have any cc_data");
1201 /* cdp_hdr_sequence_cntr */
1202 gst_byte_reader_skip_unchecked (&br, 2);
1204 /* time_code_present */
1206 guint8 hours, minutes, seconds, frames, fields;
1207 gboolean drop_frame;
1209 if (gst_byte_reader_get_remaining (&br) < 5) {
1210 GST_WARNING_OBJECT (self, "cdp packet does not have enough data to "
1211 "contain a timecode (%u). Need at least 5 bytes",
1212 gst_byte_reader_get_remaining (&br));
1215 u8 = gst_byte_reader_get_uint8_unchecked (&br);
1217 GST_WARNING_OBJECT (self, "cdp packet does not have timecode start byte "
1218 "of 0x71, found 0x%02x", u8);
1222 u8 = gst_byte_reader_get_uint8_unchecked (&br);
1223 if ((u8 & 0xc0) != 0xc0) {
1224 GST_WARNING_OBJECT (self, "reserved bits are not 0xc0, found 0x%02x", u8);
1228 hours = ((u8 >> 4) & 0x3) * 10 + (u8 & 0xf);
1230 u8 = gst_byte_reader_get_uint8_unchecked (&br);
1231 if ((u8 & 0x80) != 0x80) {
1232 GST_WARNING_OBJECT (self, "reserved bit is not 0x80, found 0x%02x", u8);
1235 minutes = ((u8 >> 4) & 0x7) * 10 + (u8 & 0xf);
1237 u8 = gst_byte_reader_get_uint8_unchecked (&br);
1242 seconds = ((u8 >> 4) & 0x7) * 10 + (u8 & 0xf);
1244 u8 = gst_byte_reader_get_uint8_unchecked (&br);
1246 GST_WARNING_OBJECT (self, "reserved bit is not 0x0, found 0x%02x", u8);
1250 drop_frame = ! !(u8 & 0x80);
1251 frames = ((u8 >> 4) & 0x3) * 10 + (u8 & 0xf);
1253 gst_video_time_code_init (tc, fps_entry->fps_n, fps_entry->fps_d, NULL,
1254 drop_frame ? GST_VIDEO_TIME_CODE_FLAGS_DROP_FRAME :
1255 GST_VIDEO_TIME_CODE_FLAGS_NONE, hours, minutes, seconds, frames,
1259 /* ccdata_present */
1263 if (gst_byte_reader_get_remaining (&br) < 2) {
1264 GST_WARNING_OBJECT (self, "not enough data to contain valid cc_data");
1267 u8 = gst_byte_reader_get_uint8_unchecked (&br);
1269 GST_WARNING_OBJECT (self, "missing cc_data start code of 0x72, "
1270 "found 0x%02x", u8);
1274 cc_count = gst_byte_reader_get_uint8_unchecked (&br);
1275 if ((cc_count & 0xe0) != 0xe0) {
1276 GST_WARNING_OBJECT (self, "reserved bits are not 0xe0, found 0x%02x", u8);
1282 if (gst_byte_reader_get_remaining (&br) < len)
1285 memcpy (cc_data, gst_byte_reader_get_data_unchecked (&br, len), len);
1288 *out_fps_entry = fps_entry;
1290 /* skip everything else we don't care about */
1295 copy_from_stored_data (GstCCConverter * self, guint8 * out_ccp,
1296 guint * ccp_size, guint8 * cea608_1, guint * cea608_1_len,
1297 guint8 * cea608_2, guint * cea608_2_len)
1299 guint ccp_in_size = 0, cea608_1_in_size = 0, cea608_2_in_size = 0;
1301 g_assert ((out_ccp && ccp_size) || (!out_ccp && !ccp_size));
1302 g_assert ((cea608_1 && cea608_1_len) || (!cea608_1 && !cea608_1_len));
1303 g_assert ((cea608_2 && cea608_2_len) || (!cea608_2 && !cea608_2_len));
1306 ccp_in_size = *ccp_size;
1310 cea608_1_in_size = *cea608_1_len;
1314 cea608_2_in_size = *cea608_2_len;
1318 if (out_ccp && self->scratch_ccp_len > 0) {
1319 GST_DEBUG_OBJECT (self, "copying from previous scratch ccp buffer of "
1320 "%u bytes", self->scratch_ccp_len);
1321 if (ccp_in_size < *ccp_size + self->scratch_ccp_len) {
1322 GST_WARNING_OBJECT (self, "output buffer too small %u < %u", ccp_in_size,
1323 *ccp_size + self->scratch_ccp_len);
1326 memcpy (&out_ccp[*ccp_size], self->scratch_ccp, self->scratch_ccp_len);
1327 *ccp_size += self->scratch_ccp_len;
1330 if (cea608_1 && self->scratch_cea608_1_len > 0) {
1331 GST_DEBUG_OBJECT (self, "copying from previous scratch cea608 field 1 "
1332 "buffer of %u bytes", self->scratch_cea608_1_len);
1333 if (cea608_1_in_size < *cea608_1_len + self->scratch_cea608_1_len) {
1334 GST_WARNING_OBJECT (self, "output buffer too small %u < %u",
1335 cea608_1_in_size, *cea608_1_len + self->scratch_cea608_1_len);
1338 memcpy (&cea608_1[*cea608_1_len], self->scratch_cea608_1,
1339 self->scratch_cea608_1_len);
1340 *cea608_1_len += self->scratch_cea608_1_len;
1343 if (cea608_2 && self->scratch_cea608_2_len > 0) {
1344 GST_DEBUG_OBJECT (self, "copying from previous scratch cea608 field 2 "
1345 "buffer of %u bytes", self->scratch_cea608_2_len);
1346 if (cea608_2_in_size < *cea608_2_len + self->scratch_cea608_2_len) {
1347 GST_WARNING_OBJECT (self, "output buffer too small %u < %u",
1348 cea608_2_in_size, *cea608_2_len + self->scratch_cea608_2_len);
1351 memcpy (&cea608_2[*cea608_2_len], self->scratch_cea608_2,
1352 self->scratch_cea608_2_len);
1353 *cea608_2_len += self->scratch_cea608_2_len;
1369 cc_data_to_cea608_ccp (GstCCConverter * self, guint8 * cc_data,
1370 guint cc_data_len, guint8 * out_ccp, guint * ccp_size, guint8 * cea608_1,
1371 guint * cea608_1_len, guint8 * cea608_2, guint * cea608_2_len,
1372 const struct cdp_fps_entry *in_fps_entry)
1374 guint ccp_in_size = 0, cea608_1_in_size = 0, cea608_2_in_size = 0;
1376 g_assert (cc_data || cc_data_len == 0);
1379 ccp_in_size = *ccp_size;
1381 cea608_1_in_size = *cea608_1_len;
1383 cea608_2_in_size = *cea608_2_len;
1385 if (!copy_from_stored_data (self, out_ccp, ccp_size, cea608_1, cea608_1_len,
1386 cea608_2, cea608_2_len))
1390 gint ccp_offset = 0;
1391 guint new_cea608_1_len = 0, new_cea608_2_len = 0;
1392 guint8 *new_cea608_1 = cea608_1, *new_cea608_2 = cea608_2;
1395 new_cea608_1_len = cea608_1_in_size - *cea608_1_len;
1396 new_cea608_1 = &cea608_1[*cea608_1_len];
1399 new_cea608_2_len = cea608_2_in_size - *cea608_2_len;
1400 new_cea608_2 = &cea608_2[*cea608_2_len];
1403 cc_data_len = compact_cc_data (cc_data, cc_data_len);
1405 if (cc_data_len / 3 > in_fps_entry->max_cc_count) {
1406 GST_WARNING_OBJECT (self, "Too many cc_data triples in CDP packet %u. "
1407 "Truncating to %u", cc_data_len / 3, in_fps_entry->max_cc_count);
1408 cc_data_len = 3 * in_fps_entry->max_cc_count;
1411 ccp_offset = cc_data_extract_cea608 (cc_data, cc_data_len, new_cea608_1,
1412 &new_cea608_1_len, new_cea608_2, &new_cea608_2_len);
1413 if (ccp_offset < 0) {
1414 GST_WARNING_OBJECT (self, "Failed to extract cea608 from cc_data");
1418 if ((new_cea608_1_len + new_cea608_2_len) / 2 >
1419 in_fps_entry->max_cea608_count) {
1420 GST_WARNING_OBJECT (self, "Too many cea608 triples in CDP packet %u. "
1421 "Truncating to %u", (new_cea608_1_len + new_cea608_2_len) / 2,
1422 in_fps_entry->max_cea608_count);
1423 if ((new_cea608_1_len + new_cea608_2_len) / 2 >
1424 in_fps_entry->max_cea608_count) {
1425 new_cea608_1_len = 2 * in_fps_entry->max_cea608_count;
1426 new_cea608_2_len = 0;
1429 2 * in_fps_entry->max_cea608_count - new_cea608_1_len;
1434 *cea608_1_len += new_cea608_1_len;
1436 *cea608_2_len += new_cea608_2_len;
1439 if (ccp_in_size < *ccp_size + cc_data_len - ccp_offset) {
1440 GST_WARNING_OBJECT (self, "output buffer too small %u < %u",
1441 ccp_in_size, *ccp_size + cc_data_len - ccp_offset);
1444 memcpy (&out_ccp[*ccp_size], &cc_data[ccp_offset],
1445 cc_data_len - ccp_offset);
1446 *ccp_size += cc_data_len - ccp_offset;
1463 cdp_to_cea608_cc_data (GstCCConverter * self, GstBuffer * inbuf,
1464 guint8 * out_ccp, guint * ccp_size, guint8 * cea608_1, guint * cea608_1_len,
1465 guint8 * cea608_2, guint * cea608_2_len, GstVideoTimeCode * out_tc,
1466 const struct cdp_fps_entry **in_fps_entry)
1468 guint8 cc_data[MAX_CDP_PACKET_LEN];
1469 guint cc_data_len = 0;
1473 gst_buffer_map (inbuf, &in, GST_MAP_READ);
1476 convert_cea708_cdp_cea708_cc_data_internal (self, in.data, in.size,
1477 cc_data, out_tc, in_fps_entry);
1479 gst_buffer_unmap (inbuf, &in);
1480 self->input_frames++;
1483 return cc_data_to_cea608_ccp (self, inbuf ? cc_data : NULL, cc_data_len,
1484 out_ccp, ccp_size, cea608_1, cea608_1_len, cea608_2, cea608_2_len,
1485 inbuf ? *in_fps_entry : NULL);
1488 static GstFlowReturn
1489 convert_cea608_raw_cea608_s334_1a (GstCCConverter * self, GstBuffer * inbuf,
1495 n = gst_buffer_get_size (inbuf);
1497 GST_WARNING_OBJECT (self, "Invalid raw CEA608 buffer size");
1498 gst_buffer_set_size (outbuf, 0);
1505 GST_WARNING_OBJECT (self, "Too many CEA608 pairs %u. Truncating to %u", n,
1510 gst_buffer_set_size (outbuf, 3 * n);
1512 gst_buffer_map (inbuf, &in, GST_MAP_READ);
1513 gst_buffer_map (outbuf, &out, GST_MAP_WRITE);
1515 /* We have to assume that each value is from the first field and
1516 * don't know from which line offset it originally is */
1517 for (i = 0; i < n; i++) {
1518 out.data[i * 3] = 0x80;
1519 out.data[i * 3 + 1] = in.data[i * 2];
1520 out.data[i * 3 + 2] = in.data[i * 2 + 1];
1523 gst_buffer_unmap (inbuf, &in);
1524 gst_buffer_unmap (outbuf, &out);
1529 static GstFlowReturn
1530 convert_cea608_raw_cea708_cc_data (GstCCConverter * self, GstBuffer * inbuf,
1536 n = gst_buffer_get_size (inbuf);
1538 GST_WARNING_OBJECT (self, "Invalid raw CEA608 buffer size");
1539 gst_buffer_set_size (outbuf, 0);
1546 GST_WARNING_OBJECT (self, "Too many CEA608 pairs %u. Truncating to %u", n,
1551 gst_buffer_set_size (outbuf, 3 * n);
1553 gst_buffer_map (inbuf, &in, GST_MAP_READ);
1554 gst_buffer_map (outbuf, &out, GST_MAP_WRITE);
1556 /* We have to assume that each value is from the first field and
1557 * don't know from which line offset it originally is */
1558 for (i = 0; i < n; i++) {
1559 out.data[i * 3] = 0xfc;
1560 out.data[i * 3 + 1] = in.data[i * 2];
1561 out.data[i * 3 + 2] = in.data[i * 2 + 1];
1564 gst_buffer_unmap (inbuf, &in);
1565 gst_buffer_unmap (outbuf, &out);
1570 static GstFlowReturn
1571 convert_cea608_raw_cea708_cdp (GstCCConverter * self, GstBuffer * inbuf,
1572 GstBuffer * outbuf, const GstVideoTimeCodeMeta * tc_meta)
1575 const struct cdp_fps_entry *in_fps_entry, *out_fps_entry;
1576 guint cc_data_len = MAX_CDP_PACKET_LEN;
1577 guint cea608_1_len = MAX_CDP_PACKET_LEN;
1578 guint8 cc_data[MAX_CDP_PACKET_LEN], cea608_1[MAX_CEA608_LEN];
1580 in_fps_entry = cdp_fps_entry_from_fps (self->in_fps_n, self->in_fps_d);
1581 if (!in_fps_entry || in_fps_entry->fps_n == 0)
1582 g_assert_not_reached ();
1584 if (!copy_from_stored_data (self, NULL, 0, cea608_1, &cea608_1_len, NULL, 0))
1590 n = gst_buffer_get_size (inbuf);
1592 GST_WARNING_OBJECT (self, "Invalid raw CEA608 buffer size");
1593 gst_buffer_set_size (outbuf, 0);
1599 if (n > in_fps_entry->max_cea608_count) {
1600 GST_WARNING_OBJECT (self, "Too many CEA608 pairs %u. Truncating to %u",
1601 n, in_fps_entry->max_cea608_count);
1602 n = in_fps_entry->max_cea608_count;
1605 gst_buffer_map (inbuf, &in, GST_MAP_READ);
1606 memcpy (&cea608_1[cea608_1_len], in.data, n * 2);
1607 gst_buffer_unmap (inbuf, &in);
1608 cea608_1_len += n * 2;
1609 self->input_frames++;
1612 out_fps_entry = cdp_fps_entry_from_fps (self->out_fps_n, self->out_fps_d);
1613 if (!out_fps_entry || out_fps_entry->fps_n == 0)
1614 g_assert_not_reached ();
1616 if (!fit_and_scale_cc_data (self, in_fps_entry, out_fps_entry, NULL, 0,
1617 cea608_1, &cea608_1_len, NULL, 0, tc_meta ? &tc_meta->tc : NULL))
1620 if (!combine_cc_data (self, TRUE, out_fps_entry, NULL, 0, cea608_1,
1621 cea608_1_len, NULL, 0, cc_data, &cc_data_len))
1624 gst_buffer_map (outbuf, &out, GST_MAP_WRITE);
1626 convert_cea708_cc_data_cea708_cdp_internal (self, cc_data, cc_data_len,
1627 out.data, out.size, &self->current_output_timecode, out_fps_entry);
1628 self->output_frames++;
1629 gst_buffer_unmap (outbuf, &out);
1632 gst_buffer_set_size (outbuf, cc_data_len);
1641 static GstFlowReturn
1642 convert_cea608_s334_1a_cea608_raw (GstCCConverter * self, GstBuffer * inbuf,
1649 n = gst_buffer_get_size (inbuf);
1651 GST_WARNING_OBJECT (self, "Invalid S334-1A CEA608 buffer size");
1658 GST_WARNING_OBJECT (self, "Too many S334-1A CEA608 triplets %u", n);
1662 gst_buffer_map (inbuf, &in, GST_MAP_READ);
1663 gst_buffer_map (outbuf, &out, GST_MAP_WRITE);
1665 for (i = 0; i < n; i++) {
1666 if (in.data[i * 3] & 0x80) {
1667 out.data[i * 2] = in.data[i * 3 + 1];
1668 out.data[i * 2 + 1] = in.data[i * 3 + 2];
1673 gst_buffer_unmap (inbuf, &in);
1674 gst_buffer_unmap (outbuf, &out);
1676 gst_buffer_set_size (outbuf, 2 * cea608);
1681 static GstFlowReturn
1682 convert_cea608_s334_1a_cea708_cc_data (GstCCConverter * self, GstBuffer * inbuf,
1688 n = gst_buffer_get_size (inbuf);
1690 GST_WARNING_OBJECT (self, "Invalid S334-1A CEA608 buffer size");
1697 GST_WARNING_OBJECT (self, "Too many S334-1A CEA608 triplets %u", n);
1701 gst_buffer_set_size (outbuf, 3 * n);
1703 gst_buffer_map (inbuf, &in, GST_MAP_READ);
1704 gst_buffer_map (outbuf, &out, GST_MAP_WRITE);
1706 for (i = 0; i < n; i++) {
1707 out.data[i * 3] = (in.data[i * 3] & 0x80) ? 0xfc : 0xfd;
1708 out.data[i * 3 + 1] = in.data[i * 3 + 1];
1709 out.data[i * 3 + 2] = in.data[i * 3 + 2];
1712 gst_buffer_unmap (inbuf, &in);
1713 gst_buffer_unmap (outbuf, &out);
1718 static GstFlowReturn
1719 convert_cea608_s334_1a_cea708_cdp (GstCCConverter * self, GstBuffer * inbuf,
1720 GstBuffer * outbuf, const GstVideoTimeCodeMeta * tc_meta)
1723 const struct cdp_fps_entry *in_fps_entry, *out_fps_entry;
1724 guint cc_data_len = MAX_CDP_PACKET_LEN;
1725 guint cea608_1_len = MAX_CDP_PACKET_LEN, cea608_2_len = MAX_CDP_PACKET_LEN;
1726 guint8 cc_data[MAX_CDP_PACKET_LEN];
1727 guint8 cea608_1[MAX_CEA608_LEN], cea608_2[MAX_CEA608_LEN];
1730 in_fps_entry = cdp_fps_entry_from_fps (self->in_fps_n, self->in_fps_d);
1731 if (!in_fps_entry || in_fps_entry->fps_n == 0)
1732 g_assert_not_reached ();
1734 if (!copy_from_stored_data (self, NULL, 0, cea608_1, &cea608_1_len,
1735 cea608_2, &cea608_2_len))
1739 n = gst_buffer_get_size (inbuf);
1741 GST_WARNING_OBJECT (self, "Invalid S334-1A CEA608 buffer size");
1747 if (n > in_fps_entry->max_cea608_count) {
1748 GST_WARNING_OBJECT (self, "Too many S334-1A CEA608 triplets %u", n);
1749 n = in_fps_entry->max_cea608_count;
1752 gst_buffer_map (inbuf, &in, GST_MAP_READ);
1754 for (i = 0; i < n; i++) {
1755 if (in.data[i * 3] & 0x80) {
1756 cea608_1[cea608_1_len++] = in.data[i * 3 + 1];
1757 cea608_1[cea608_1_len++] = in.data[i * 3 + 2];
1759 cea608_2[cea608_2_len++] = in.data[i * 3 + 1];
1760 cea608_2[cea608_2_len++] = in.data[i * 3 + 2];
1763 gst_buffer_unmap (inbuf, &in);
1764 self->input_frames++;
1767 out_fps_entry = cdp_fps_entry_from_fps (self->out_fps_n, self->out_fps_d);
1768 if (!out_fps_entry || out_fps_entry->fps_n == 0)
1769 g_assert_not_reached ();
1771 if (!fit_and_scale_cc_data (self, in_fps_entry, out_fps_entry, NULL, 0,
1772 cea608_1, &cea608_1_len, cea608_2, &cea608_2_len,
1773 tc_meta ? &tc_meta->tc : NULL)) {
1777 if (!combine_cc_data (self, TRUE, out_fps_entry, NULL, 0, cea608_1,
1778 cea608_1_len, cea608_2, cea608_2_len, cc_data, &cc_data_len)) {
1782 gst_buffer_map (outbuf, &out, GST_MAP_WRITE);
1784 convert_cea708_cc_data_cea708_cdp_internal (self, cc_data, cc_data_len,
1785 out.data, out.size, &self->current_output_timecode, out_fps_entry);
1786 self->output_frames++;
1787 gst_buffer_unmap (outbuf, &out);
1790 gst_buffer_set_size (outbuf, cc_data_len);
1799 static GstFlowReturn
1800 convert_cea708_cc_data_cea608_raw (GstCCConverter * self, GstBuffer * inbuf,
1807 n = gst_buffer_get_size (inbuf);
1809 GST_WARNING_OBJECT (self, "Invalid raw CEA708 buffer size");
1816 GST_WARNING_OBJECT (self, "Too many CEA708 triplets %u", n);
1820 gst_buffer_map (inbuf, &in, GST_MAP_READ);
1821 gst_buffer_map (outbuf, &out, GST_MAP_WRITE);
1823 for (i = 0; i < n; i++) {
1824 /* We can only really copy the first field here as there can't be any
1825 * signalling in raw CEA608 and we must not mix the streams of different
1828 if (in.data[i * 3] == 0xfc) {
1829 out.data[cea608 * 2] = in.data[i * 3 + 1];
1830 out.data[cea608 * 2 + 1] = in.data[i * 3 + 2];
1835 gst_buffer_unmap (inbuf, &in);
1836 gst_buffer_unmap (outbuf, &out);
1838 gst_buffer_set_size (outbuf, 2 * cea608);
1843 static GstFlowReturn
1844 convert_cea708_cc_data_cea608_s334_1a (GstCCConverter * self, GstBuffer * inbuf,
1851 n = gst_buffer_get_size (inbuf);
1853 GST_WARNING_OBJECT (self, "Invalid raw CEA708 buffer size");
1860 GST_WARNING_OBJECT (self, "Too many CEA708 triplets %u", n);
1864 gst_buffer_map (inbuf, &in, GST_MAP_READ);
1865 gst_buffer_map (outbuf, &out, GST_MAP_WRITE);
1867 for (i = 0; i < n; i++) {
1868 if (in.data[i * 3] == 0xfc || in.data[i * 3] == 0xfd) {
1869 /* We have to assume a line offset of 0 */
1870 out.data[cea608 * 3] = in.data[i * 3] == 0xfc ? 0x80 : 0x00;
1871 out.data[cea608 * 3 + 1] = in.data[i * 3 + 1];
1872 out.data[cea608 * 3 + 2] = in.data[i * 3 + 2];
1877 gst_buffer_unmap (inbuf, &in);
1878 gst_buffer_unmap (outbuf, &out);
1880 gst_buffer_set_size (outbuf, 3 * cea608);
1885 static GstFlowReturn
1886 convert_cea708_cc_data_cea708_cdp (GstCCConverter * self, GstBuffer * inbuf,
1887 GstBuffer * outbuf, const GstVideoTimeCodeMeta * tc_meta)
1890 const struct cdp_fps_entry *in_fps_entry, *out_fps_entry;
1891 guint in_cc_data_len;
1892 guint cc_data_len = MAX_CDP_PACKET_LEN, ccp_data_len = MAX_CDP_PACKET_LEN;
1893 guint cea608_1_len = MAX_CEA608_LEN, cea608_2_len = MAX_CEA608_LEN;
1894 guint8 cc_data[MAX_CDP_PACKET_LEN], ccp_data[MAX_CDP_PACKET_LEN];
1895 guint8 cea608_1[MAX_CEA608_LEN], cea608_2[MAX_CEA608_LEN];
1899 gst_buffer_map (inbuf, &in, GST_MAP_READ);
1900 in_cc_data = in.data;
1901 in_cc_data_len = in.size;
1902 self->input_frames++;
1908 in_fps_entry = cdp_fps_entry_from_fps (self->in_fps_n, self->in_fps_d);
1909 if (!in_fps_entry || in_fps_entry->fps_n == 0)
1910 g_assert_not_reached ();
1912 out_fps_entry = cdp_fps_entry_from_fps (self->out_fps_n, self->out_fps_d);
1913 if (!out_fps_entry || out_fps_entry->fps_n == 0)
1914 g_assert_not_reached ();
1916 if (!cc_data_to_cea608_ccp (self, in_cc_data, in_cc_data_len, ccp_data,
1917 &ccp_data_len, cea608_1, &cea608_1_len, cea608_2, &cea608_2_len,
1920 gst_buffer_unmap (inbuf, &in);
1925 gst_buffer_unmap (inbuf, &in);
1927 if (!fit_and_scale_cc_data (self, in_fps_entry, out_fps_entry, ccp_data,
1928 &ccp_data_len, cea608_1, &cea608_1_len, cea608_2, &cea608_2_len,
1929 tc_meta ? &tc_meta->tc : NULL))
1932 if (!combine_cc_data (self, TRUE, out_fps_entry, ccp_data, ccp_data_len,
1933 cea608_1, cea608_1_len, cea608_2, cea608_2_len, cc_data,
1937 gst_buffer_map (outbuf, &out, GST_MAP_WRITE);
1939 convert_cea708_cc_data_cea708_cdp_internal (self, cc_data, cc_data_len,
1940 out.data, out.size, &self->current_output_timecode, out_fps_entry);
1941 self->output_frames++;
1942 gst_buffer_unmap (outbuf, &out);
1945 gst_buffer_set_size (outbuf, cc_data_len);
1954 static GstFlowReturn
1955 convert_cea708_cdp_cea608_raw (GstCCConverter * self, GstBuffer * inbuf,
1956 GstBuffer * outbuf, const GstVideoTimeCodeMeta * tc_meta)
1959 GstVideoTimeCode tc = GST_VIDEO_TIME_CODE_INIT;
1961 const struct cdp_fps_entry *in_fps_entry = NULL, *out_fps_entry;
1963 gst_buffer_map (outbuf, &out, GST_MAP_WRITE);
1964 cea608_1_len = (guint) out.size;
1965 if (!cdp_to_cea608_cc_data (self, inbuf, NULL, NULL, out.data, &cea608_1_len,
1966 NULL, NULL, &tc, &in_fps_entry)) {
1967 gst_buffer_set_size (outbuf, 0);
1971 out_fps_entry = cdp_fps_entry_from_fps (self->out_fps_n, self->out_fps_d);
1972 if (!out_fps_entry || out_fps_entry->fps_n == 0)
1973 out_fps_entry = in_fps_entry;
1975 if (fit_and_scale_cc_data (self, in_fps_entry, out_fps_entry, NULL, 0,
1976 out.data, &cea608_1_len, NULL, NULL, &tc)) {
1977 self->output_frames++;
1981 gst_buffer_unmap (outbuf, &out);
1983 gst_buffer_set_size (outbuf, cea608_1_len);
1985 if (self->current_output_timecode.config.fps_n != 0 && !tc_meta) {
1986 gst_buffer_add_video_time_code_meta (outbuf,
1987 &self->current_output_timecode);
1988 gst_video_time_code_increment_frame (&self->current_output_timecode);
1994 static GstFlowReturn
1995 convert_cea708_cdp_cea608_s334_1a (GstCCConverter * self, GstBuffer * inbuf,
1996 GstBuffer * outbuf, const GstVideoTimeCodeMeta * tc_meta)
1999 GstVideoTimeCode tc = GST_VIDEO_TIME_CODE_INIT;
2000 const struct cdp_fps_entry *in_fps_entry = NULL, *out_fps_entry;
2001 guint8 cea608_1[MAX_CEA608_LEN], cea608_2[MAX_CEA608_LEN];
2002 guint cea608_1_len = MAX_CEA608_LEN, cea608_2_len = MAX_CEA608_LEN;
2003 guint i, cc_data_len;
2005 if (!cdp_to_cea608_cc_data (self, inbuf, NULL, NULL, cea608_1, &cea608_1_len,
2006 cea608_2, &cea608_2_len, &tc, &in_fps_entry))
2009 out_fps_entry = cdp_fps_entry_from_fps (self->out_fps_n, self->out_fps_d);
2010 if (!out_fps_entry || out_fps_entry->fps_n == 0)
2011 out_fps_entry = in_fps_entry;
2013 if (!fit_and_scale_cc_data (self, in_fps_entry, out_fps_entry, NULL, 0,
2014 cea608_1, &cea608_1_len, cea608_2, &cea608_2_len, &tc))
2017 cc_data_len = gst_buffer_get_sizes (outbuf, NULL, NULL);
2019 gst_buffer_map (outbuf, &out, GST_MAP_READWRITE);
2020 if (!combine_cc_data (self, FALSE, out_fps_entry, NULL, 0, cea608_1,
2021 cea608_1_len, cea608_2, cea608_2_len, out.data, &cc_data_len)) {
2022 gst_buffer_unmap (outbuf, &out);
2026 for (i = 0; i < cc_data_len / 3; i++)
2027 /* We have to assume a line offset of 0 */
2028 out.data[i * 3] = out.data[i * 3] == 0xfc ? 0x80 : 0x00;
2030 gst_buffer_unmap (outbuf, &out);
2031 self->output_frames++;
2033 gst_buffer_set_size (outbuf, cc_data_len);
2035 if (self->current_output_timecode.config.fps_n != 0 && !tc_meta) {
2036 gst_buffer_add_video_time_code_meta (outbuf,
2037 &self->current_output_timecode);
2038 gst_video_time_code_increment_frame (&self->current_output_timecode);
2044 gst_buffer_set_size (outbuf, 0);
2048 static GstFlowReturn
2049 convert_cea708_cdp_cea708_cc_data (GstCCConverter * self, GstBuffer * inbuf,
2050 GstBuffer * outbuf, const GstVideoTimeCodeMeta * tc_meta)
2053 GstVideoTimeCode tc = GST_VIDEO_TIME_CODE_INIT;
2054 const struct cdp_fps_entry *in_fps_entry = NULL, *out_fps_entry;
2055 guint8 cea608_1[MAX_CEA608_LEN], cea608_2[MAX_CEA608_LEN];
2056 guint8 ccp_data[MAX_CDP_PACKET_LEN];
2057 guint cea608_1_len = MAX_CEA608_LEN, cea608_2_len = MAX_CEA608_LEN;
2058 guint ccp_data_len = MAX_CDP_PACKET_LEN;
2061 if (!cdp_to_cea608_cc_data (self, inbuf, ccp_data, &ccp_data_len,
2062 cea608_1, &cea608_1_len, cea608_2, &cea608_2_len, &tc, &in_fps_entry))
2065 out_fps_entry = cdp_fps_entry_from_fps (self->out_fps_n, self->out_fps_d);
2066 if (!out_fps_entry || out_fps_entry->fps_n == 0)
2067 out_fps_entry = in_fps_entry;
2069 if (!fit_and_scale_cc_data (self, in_fps_entry, out_fps_entry, ccp_data,
2070 &ccp_data_len, cea608_1, &cea608_1_len, cea608_2, &cea608_2_len, &tc))
2073 gst_buffer_map (outbuf, &out, GST_MAP_WRITE);
2074 out_len = (guint) out.size;
2075 if (!combine_cc_data (self, FALSE, out_fps_entry, ccp_data, ccp_data_len,
2076 cea608_1, cea608_1_len, cea608_2, cea608_2_len, out.data, &out_len)) {
2077 gst_buffer_unmap (outbuf, &out);
2082 gst_buffer_unmap (outbuf, &out);
2083 self->output_frames++;
2085 if (self->current_output_timecode.config.fps_n != 0 && !tc_meta) {
2086 gst_buffer_add_video_time_code_meta (outbuf,
2087 &self->current_output_timecode);
2088 gst_video_time_code_increment_frame (&self->current_output_timecode);
2092 gst_buffer_set_size (outbuf, out_len);
2097 static GstFlowReturn
2098 convert_cea708_cdp_cea708_cdp (GstCCConverter * self, GstBuffer * inbuf,
2102 GstVideoTimeCode tc = GST_VIDEO_TIME_CODE_INIT;
2103 const struct cdp_fps_entry *in_fps_entry = NULL, *out_fps_entry;
2104 guint8 cea608_1[MAX_CEA608_LEN], cea608_2[MAX_CEA608_LEN];
2105 guint8 ccp_data[MAX_CDP_PACKET_LEN], cc_data[MAX_CDP_PACKET_LEN];
2106 guint cea608_1_len = MAX_CEA608_LEN, cea608_2_len = MAX_CEA608_LEN;
2107 guint ccp_data_len = MAX_CDP_PACKET_LEN, cc_data_len = MAX_CDP_PACKET_LEN;
2110 if (!cdp_to_cea608_cc_data (self, inbuf, ccp_data, &ccp_data_len,
2111 cea608_1, &cea608_1_len, cea608_2, &cea608_2_len, &tc, &in_fps_entry))
2114 out_fps_entry = cdp_fps_entry_from_fps (self->out_fps_n, self->out_fps_d);
2115 if (!out_fps_entry || out_fps_entry->fps_n == 0)
2116 out_fps_entry = in_fps_entry;
2118 if (!fit_and_scale_cc_data (self, in_fps_entry, out_fps_entry, ccp_data,
2119 &ccp_data_len, cea608_1, &cea608_1_len, cea608_2, &cea608_2_len, &tc))
2122 if (!combine_cc_data (self, TRUE, out_fps_entry, ccp_data, ccp_data_len,
2123 cea608_1, cea608_1_len, cea608_2, cea608_2_len, cc_data,
2128 gst_buffer_map (outbuf, &out, GST_MAP_WRITE);
2130 convert_cea708_cc_data_cea708_cdp_internal (self, cc_data, cc_data_len,
2131 out.data, out.size, &self->current_output_timecode, out_fps_entry);
2133 gst_buffer_unmap (outbuf, &out);
2134 self->output_frames++;
2137 gst_buffer_set_size (outbuf, out_len);
2142 static GstFlowReturn
2143 gst_cc_converter_transform (GstCCConverter * self, GstBuffer * inbuf,
2146 GstVideoTimeCodeMeta *tc_meta = NULL;
2147 GstFlowReturn ret = GST_FLOW_OK;
2149 GST_DEBUG_OBJECT (self, "Converting %" GST_PTR_FORMAT " from %u to %u", inbuf,
2150 self->input_caption_type, self->output_caption_type);
2153 tc_meta = gst_buffer_get_video_time_code_meta (inbuf);
2156 if (self->current_output_timecode.config.fps_n <= 0) {
2157 /* XXX: this assumes the input time codes are well-formed and increase
2158 * at the rate of one frame for each input buffer */
2159 const struct cdp_fps_entry *in_fps_entry;
2160 gint scale_n, scale_d;
2162 in_fps_entry = cdp_fps_entry_from_fps (self->in_fps_n, self->in_fps_d);
2163 if (!in_fps_entry || in_fps_entry->fps_n == 0)
2164 scale_n = scale_d = 1;
2166 get_framerate_output_scale (self, in_fps_entry, &scale_n, &scale_d);
2168 interpolate_time_code_with_framerate (self, &tc_meta->tc,
2169 self->out_fps_n, self->out_fps_d, scale_n, scale_d,
2170 &self->current_output_timecode);
2174 switch (self->input_caption_type) {
2175 case GST_VIDEO_CAPTION_TYPE_CEA608_RAW:
2177 switch (self->output_caption_type) {
2178 case GST_VIDEO_CAPTION_TYPE_CEA608_S334_1A:
2179 ret = convert_cea608_raw_cea608_s334_1a (self, inbuf, outbuf);
2181 case GST_VIDEO_CAPTION_TYPE_CEA708_RAW:
2182 ret = convert_cea608_raw_cea708_cc_data (self, inbuf, outbuf);
2184 case GST_VIDEO_CAPTION_TYPE_CEA708_CDP:
2185 ret = convert_cea608_raw_cea708_cdp (self, inbuf, outbuf, tc_meta);
2187 case GST_VIDEO_CAPTION_TYPE_CEA608_RAW:
2189 g_assert_not_reached ();
2194 case GST_VIDEO_CAPTION_TYPE_CEA608_S334_1A:
2196 switch (self->output_caption_type) {
2197 case GST_VIDEO_CAPTION_TYPE_CEA608_RAW:
2198 ret = convert_cea608_s334_1a_cea608_raw (self, inbuf, outbuf);
2200 case GST_VIDEO_CAPTION_TYPE_CEA708_RAW:
2201 ret = convert_cea608_s334_1a_cea708_cc_data (self, inbuf, outbuf);
2203 case GST_VIDEO_CAPTION_TYPE_CEA708_CDP:
2205 convert_cea608_s334_1a_cea708_cdp (self, inbuf, outbuf, tc_meta);
2207 case GST_VIDEO_CAPTION_TYPE_CEA608_S334_1A:
2209 g_assert_not_reached ();
2214 case GST_VIDEO_CAPTION_TYPE_CEA708_RAW:
2216 switch (self->output_caption_type) {
2217 case GST_VIDEO_CAPTION_TYPE_CEA608_RAW:
2218 ret = convert_cea708_cc_data_cea608_raw (self, inbuf, outbuf);
2220 case GST_VIDEO_CAPTION_TYPE_CEA608_S334_1A:
2221 ret = convert_cea708_cc_data_cea608_s334_1a (self, inbuf, outbuf);
2223 case GST_VIDEO_CAPTION_TYPE_CEA708_CDP:
2225 convert_cea708_cc_data_cea708_cdp (self, inbuf, outbuf, tc_meta);
2227 case GST_VIDEO_CAPTION_TYPE_CEA708_RAW:
2229 g_assert_not_reached ();
2234 case GST_VIDEO_CAPTION_TYPE_CEA708_CDP:
2236 switch (self->output_caption_type) {
2237 case GST_VIDEO_CAPTION_TYPE_CEA608_RAW:
2238 ret = convert_cea708_cdp_cea608_raw (self, inbuf, outbuf, tc_meta);
2240 case GST_VIDEO_CAPTION_TYPE_CEA608_S334_1A:
2242 convert_cea708_cdp_cea608_s334_1a (self, inbuf, outbuf, tc_meta);
2244 case GST_VIDEO_CAPTION_TYPE_CEA708_RAW:
2246 convert_cea708_cdp_cea708_cc_data (self, inbuf, outbuf, tc_meta);
2248 case GST_VIDEO_CAPTION_TYPE_CEA708_CDP:
2249 ret = convert_cea708_cdp_cea708_cdp (self, inbuf, outbuf);
2252 g_assert_not_reached ();
2258 g_assert_not_reached ();
2262 if (ret != GST_FLOW_OK) {
2263 GST_DEBUG_OBJECT (self, "returning %s", gst_flow_get_name (ret));
2267 GST_DEBUG_OBJECT (self, "Converted to %" GST_PTR_FORMAT, outbuf);
2269 if (gst_buffer_get_size (outbuf) > 0) {
2270 if (self->current_output_timecode.config.fps_n > 0) {
2271 gst_buffer_add_video_time_code_meta (outbuf,
2272 &self->current_output_timecode);
2273 gst_video_time_code_increment_frame (&self->current_output_timecode);
2283 gst_cc_converter_transform_meta (GstBaseTransform * base, GstBuffer * outbuf,
2284 GstMeta * meta, GstBuffer * inbuf)
2286 const GstMetaInfo *info = meta->info;
2288 /* we do this manually for framerate scaling */
2289 if (info->api == GST_VIDEO_TIME_CODE_META_API_TYPE)
2292 return GST_BASE_TRANSFORM_CLASS (parent_class)->transform_meta (base, outbuf,
2297 can_generate_output (GstCCConverter * self)
2299 int input_frame_n, input_frame_d, output_frame_n, output_frame_d;
2300 int output_time_cmp;
2302 if (self->in_fps_n == 0 || self->out_fps_n == 0)
2305 /* compute the relative frame count for each */
2306 if (!gst_util_fraction_multiply (self->in_fps_d, self->in_fps_n,
2307 self->input_frames, 1, &input_frame_n, &input_frame_d))
2308 /* we should never overflow */
2309 g_assert_not_reached ();
2311 if (!gst_util_fraction_multiply (self->out_fps_d, self->out_fps_n,
2312 self->output_frames, 1, &output_frame_n, &output_frame_d))
2313 /* we should never overflow */
2314 g_assert_not_reached ();
2316 output_time_cmp = gst_util_fraction_compare (input_frame_n, input_frame_d,
2317 output_frame_n, output_frame_d);
2319 /* if the next output frame is at or before the current input frame */
2320 if (output_time_cmp >= 0)
2327 reset_counters (GstCCConverter * self)
2329 self->scratch_ccp_len = 0;
2330 self->scratch_cea608_1_len = 0;
2331 self->scratch_cea608_2_len = 0;
2332 self->input_frames = 0;
2333 self->output_frames = 1;
2334 gst_video_time_code_clear (&self->current_output_timecode);
2335 gst_clear_buffer (&self->previous_buffer);
2338 static GstFlowReturn
2339 drain_input (GstCCConverter * self)
2341 GstBaseTransformClass *bclass = GST_BASE_TRANSFORM_GET_CLASS (self);
2342 GstBaseTransform *trans = GST_BASE_TRANSFORM (self);
2343 GstFlowReturn ret = GST_FLOW_OK;
2345 while (self->scratch_ccp_len > 0 || self->scratch_cea608_1_len > 0
2346 || self->scratch_cea608_2_len > 0 || can_generate_output (self)) {
2349 if (!self->previous_buffer) {
2350 GST_WARNING_OBJECT (self, "Attempt to draining without a previous "
2351 "buffer. Aborting");
2355 outbuf = gst_buffer_new_allocate (NULL, MAX_CDP_PACKET_LEN, NULL);
2357 if (bclass->copy_metadata) {
2358 if (!bclass->copy_metadata (trans, self->previous_buffer, outbuf)) {
2359 /* something failed, post a warning */
2360 GST_ELEMENT_WARNING (self, STREAM, NOT_IMPLEMENTED,
2361 ("could not copy metadata"), (NULL));
2365 ret = gst_cc_converter_transform (self, NULL, outbuf);
2366 if (gst_buffer_get_size (outbuf) <= 0) {
2367 /* try to move the output along */
2368 self->input_frames++;
2369 gst_buffer_unref (outbuf);
2371 } else if (ret != GST_FLOW_OK) {
2372 gst_buffer_unref (outbuf);
2376 ret = gst_pad_push (GST_BASE_TRANSFORM_SRC_PAD (trans), outbuf);
2377 if (ret != GST_FLOW_OK) {
2385 static GstFlowReturn
2386 gst_cc_converter_generate_output (GstBaseTransform * base, GstBuffer ** outbuf)
2388 GstBaseTransformClass *bclass = GST_BASE_TRANSFORM_GET_CLASS (base);
2389 GstCCConverter *self = GST_CCCONVERTER (base);
2390 GstBuffer *inbuf = base->queued_buf;
2394 base->queued_buf = NULL;
2395 if (!inbuf && !can_generate_output (self)) {
2399 if (gst_base_transform_is_passthrough (base)) {
2403 if (inbuf && GST_BUFFER_IS_DISCONT (inbuf)) {
2404 ret = drain_input (self);
2405 reset_counters (self);
2406 if (ret != GST_FLOW_OK)
2410 *outbuf = gst_buffer_new_allocate (NULL, MAX_CDP_PACKET_LEN, NULL);
2411 if (*outbuf == NULL)
2415 gst_buffer_replace (&self->previous_buffer, inbuf);
2417 if (bclass->copy_metadata) {
2418 if (!bclass->copy_metadata (base, self->previous_buffer, *outbuf)) {
2419 /* something failed, post a warning */
2420 GST_ELEMENT_WARNING (self, STREAM, NOT_IMPLEMENTED,
2421 ("could not copy metadata"), (NULL));
2425 ret = gst_cc_converter_transform (self, inbuf, *outbuf);
2426 if (gst_buffer_get_size (*outbuf) <= 0) {
2427 gst_buffer_unref (*outbuf);
2433 gst_buffer_unref (inbuf);
2441 gst_buffer_unref (inbuf);
2443 GST_WARNING_OBJECT (self, "could not allocate buffer");
2444 return GST_FLOW_ERROR;
2449 gst_cc_converter_sink_event (GstBaseTransform * trans, GstEvent * event)
2451 GstCCConverter *self = GST_CCCONVERTER (trans);
2453 switch (GST_EVENT_TYPE (event)) {
2455 GST_DEBUG_OBJECT (self, "received EOS");
2460 case GST_EVENT_FLUSH_START:
2461 reset_counters (self);
2467 return GST_BASE_TRANSFORM_CLASS (parent_class)->sink_event (trans, event);
2471 gst_cc_converter_start (GstBaseTransform * base)
2473 GstCCConverter *self = GST_CCCONVERTER (base);
2475 /* Resetting this is not really needed but makes debugging easier */
2476 self->cdp_hdr_sequence_cntr = 0;
2477 self->current_output_timecode = (GstVideoTimeCode) GST_VIDEO_TIME_CODE_INIT;
2478 self->input_frames = 0;
2479 self->output_frames = 1;
2480 self->scratch_ccp_len = 0;
2481 self->scratch_cea608_1_len = 0;
2482 self->scratch_cea608_2_len = 0;
2488 gst_cc_converter_stop (GstBaseTransform * base)
2490 GstCCConverter *self = GST_CCCONVERTER (base);
2492 gst_video_time_code_clear (&self->current_output_timecode);
2493 gst_clear_buffer (&self->previous_buffer);
2499 gst_cc_converter_set_property (GObject * object, guint prop_id,
2500 const GValue * value, GParamSpec * pspec)
2502 GstCCConverter *filter = GST_CCCONVERTER (object);
2506 filter->cdp_mode = g_value_get_flags (value);
2509 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
2515 gst_cc_converter_get_property (GObject * object, guint prop_id, GValue * value,
2518 GstCCConverter *filter = GST_CCCONVERTER (object);
2522 g_value_set_flags (value, filter->cdp_mode);
2525 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
2531 gst_cc_converter_class_init (GstCCConverterClass * klass)
2533 GObjectClass *gobject_class;
2534 GstElementClass *gstelement_class;
2535 GstBaseTransformClass *basetransform_class;
2537 gobject_class = (GObjectClass *) klass;
2538 gstelement_class = (GstElementClass *) klass;
2539 basetransform_class = (GstBaseTransformClass *) klass;
2541 gobject_class->set_property = gst_cc_converter_set_property;
2542 gobject_class->get_property = gst_cc_converter_get_property;
2545 * GstCCConverter:cdp-mode
2547 * Only insert the selection sections into CEA 708 CDP packets.
2549 * Various software does not handle any other information than CC data
2550 * contained in CDP packets and might fail parsing the packets otherwise.
2554 g_object_class_install_property (G_OBJECT_CLASS (klass),
2555 PROP_CDP_MODE, g_param_spec_flags ("cdp-mode",
2557 "Select which CDP sections to store in CDP packets",
2558 GST_TYPE_CC_CONVERTER_CDP_MODE, DEFAULT_CDP_MODE,
2559 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
2561 gst_element_class_set_static_metadata (gstelement_class,
2562 "Closed Caption Converter",
2563 "Filter/ClosedCaption",
2564 "Converts Closed Captions between different formats",
2565 "Sebastian Dröge <sebastian@centricular.com>");
2567 gst_element_class_add_static_pad_template (gstelement_class, &sinktemplate);
2568 gst_element_class_add_static_pad_template (gstelement_class, &srctemplate);
2570 basetransform_class->start = GST_DEBUG_FUNCPTR (gst_cc_converter_start);
2571 basetransform_class->stop = GST_DEBUG_FUNCPTR (gst_cc_converter_stop);
2572 basetransform_class->sink_event =
2573 GST_DEBUG_FUNCPTR (gst_cc_converter_sink_event);
2574 basetransform_class->transform_size =
2575 GST_DEBUG_FUNCPTR (gst_cc_converter_transform_size);
2576 basetransform_class->transform_caps =
2577 GST_DEBUG_FUNCPTR (gst_cc_converter_transform_caps);
2578 basetransform_class->fixate_caps =
2579 GST_DEBUG_FUNCPTR (gst_cc_converter_fixate_caps);
2580 basetransform_class->set_caps = GST_DEBUG_FUNCPTR (gst_cc_converter_set_caps);
2581 basetransform_class->transform_meta =
2582 GST_DEBUG_FUNCPTR (gst_cc_converter_transform_meta);
2583 basetransform_class->generate_output =
2584 GST_DEBUG_FUNCPTR (gst_cc_converter_generate_output);
2585 basetransform_class->passthrough_on_same_caps = TRUE;
2587 GST_DEBUG_CATEGORY_INIT (gst_cc_converter_debug, "ccconverter",
2588 0, "Closed Caption converter");
2590 gst_type_mark_as_plugin_api (GST_TYPE_CC_CONVERTER_CDP_MODE, 0);
2594 gst_cc_converter_init (GstCCConverter * self)
2596 self->cdp_mode = DEFAULT_CDP_MODE;