2 * Copyright (C) 2018 Edward Hervey <edward@centricular.com>
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.
26 #include <gst/base/gstbytereader.h>
27 #include "video-anc.h"
31 * @title: GstVideo Ancillary
32 * @short_description: Utilities for Ancillary data, VBI and Closed Caption
34 * A collection of objects and methods to assist with handling Ancillary Data
35 * present in Vertical Blanking Interval as well as Closed Caption.
38 #ifndef GST_DISABLE_GST_DEBUG
39 #define GST_CAT_DEFAULT ensure_debug_category()
40 static GstDebugCategory *
41 ensure_debug_category (void)
43 static gsize cat_gonce = 0;
45 if (g_once_init_enter (&cat_gonce)) {
48 cat_done = (gsize) _gst_debug_category_new ("video-anc", 0,
49 "Ancillary data, VBI and CC utilities");
51 g_once_init_leave (&cat_gonce, cat_done);
54 return (GstDebugCategory *) cat_gonce;
57 #define ensure_debug_category() /* NOOP */
58 #endif /* GST_DISABLE_GST_DEBUG */
60 struct _GstVideoVBIParser
62 GstVideoInfo info; /* format of the lines provided */
63 guint8 *work_data; /* Converted line in planar 16bit format */
64 guint32 work_data_size; /* Size in bytes of work_data */
65 guint offset; /* Current offset (in bytes) in work_data */
66 gboolean bit16; /* Data is stored as 16bit if TRUE. Else 8bit(without parity) */
69 G_DEFINE_BOXED_TYPE (GstVideoVBIParser, gst_video_vbi_parser,
70 (GBoxedCopyFunc) gst_video_vbi_parser_copy,
71 (GBoxedFreeFunc) gst_video_vbi_parser_free);
74 gst_video_vbi_parser_copy (const GstVideoVBIParser * parser)
76 GstVideoVBIParser *res;
78 g_return_val_if_fail (parser != NULL, NULL);
80 res = gst_video_vbi_parser_new (GST_VIDEO_INFO_FORMAT (&parser->info),
83 memcpy (res->work_data, parser->work_data, parser->work_data_size);
89 static GstVideoVBIParserResult
90 get_ancillary_16 (GstVideoVBIParser * parser, GstVideoAncillary * anc)
92 gboolean found = FALSE;
93 const guint16 *data = (const guint16 *) parser->work_data;
95 g_return_val_if_fail (parser != NULL, GST_VIDEO_VBI_PARSER_RESULT_ERROR);
96 g_return_val_if_fail (anc != NULL, GST_VIDEO_VBI_PARSER_RESULT_ERROR);
98 /* 3 words are needed at least to detect what kind of packet we look at
100 * - ADF (SMPTE S291 3.2.1) in case of component ancillary format:
101 * 0x000 0x3ff 0x3ff (followed by DID, SDID)
102 * - ADF (SMPTE S291 3.2.2) in case of composite ancillary format:
105 while (parser->offset + 3 < parser->work_data_size) {
106 guint8 DID, SDID, DC;
112 if (data[parser->offset] == 0x3fc) {
116 } else if (data[parser->offset] == 0x000 &&
117 data[parser->offset + 1] == 0x3ff &&
118 data[parser->offset + 2] == 0x3ff) {
127 /* TODO: Might want to check parity bits here but the checksum in
128 * the end should really be enough */
130 /* 4 words: DID, SDID, DC, [DATA], checksum */
131 if (parser->offset + i + 4 >= parser->work_data_size)
132 goto not_enough_data;
134 /* We have a valid ADF */
135 DID = data[parser->offset + i] & 0xff;
136 SDID = data[parser->offset + i + 1] & 0xff;
137 DC = data[parser->offset + i + 2] & 0xff;
140 /* Check if we have enough room to get the User Data and checksum */
141 if (parser->offset + i + DC + 1 >= parser->work_data_size)
142 goto not_enough_data;
144 /* We found a valid ANC \o/ */
146 anc->SDID_block_number = SDID;
147 anc->data_count = DC;
148 memset (anc->data, 0, 256);
150 /* FIXME: We assume here the same data format for the user data as for the
151 * DID/SDID: 10 bits with parity in the upper 2 bits. In theory some
152 * standards could define this differently and even have full 10 bits of
153 * user data but there does not seem to be a single such standard after
157 /* i is at the beginning of the user data now */
158 for (j = 0; j < anc->data_count; j++)
159 anc->data[j] = data[parser->offset + i + j] & 0xff;
162 /* Checksum calculation SMPTE S291 3.2.1 */
163 for (j = (composite ? 1 : 3); j < i; j++)
164 checksum += data[parser->offset + j] & 0x1ff;
166 checksum |= (!(checksum >> 8)) << 9;
168 if (checksum != (data[parser->offset + i] & 0x3ff)) {
169 GST_WARNING ("ADF checksum mismatch: expected 0x%03x, got 0x%03x",
170 checksum, (data[parser->offset + i] & 0x3ff));
183 return GST_VIDEO_VBI_PARSER_RESULT_OK;
185 return GST_VIDEO_VBI_PARSER_RESULT_DONE;
190 GST_WARNING ("ANC requires more User Data than available line size");
191 /* Avoid further calls to go in the same error */
192 parser->offset = parser->work_data_size;
193 return GST_VIDEO_VBI_PARSER_RESULT_ERROR;
198 static GstVideoVBIParserResult
199 get_ancillary_8 (GstVideoVBIParser * parser, GstVideoAncillary * anc)
201 gboolean found = FALSE;
202 const guint8 *data = parser->work_data;
204 g_return_val_if_fail (parser != NULL, GST_VIDEO_VBI_PARSER_RESULT_ERROR);
205 g_return_val_if_fail (anc != NULL, GST_VIDEO_VBI_PARSER_RESULT_ERROR);
207 /* 3 words are needed at least to detect what kind of packet we look at
209 * - ADF (SMPTE S291 3.2.1) in case of component ancillary format:
210 * 0x000 0x3ff 0x3ff (followed by DID, SDID)
211 * - ADF (SMPTE S291 3.2.2) in case of composite ancillary format:
214 while (parser->offset + 3 < parser->work_data_size) {
215 guint8 DID, SDID, DC;
221 if (data[parser->offset] == 0xfc) {
225 } else if (data[parser->offset] == 0x00 &&
226 data[parser->offset + 1] == 0xff && data[parser->offset + 2] == 0xff) {
235 /* 4 words: DID, SDID, DC, [DATA], checksum */
236 if (parser->offset + i + 4 >= parser->work_data_size)
237 goto not_enough_data;
239 /* We have a valid ADF */
240 DID = data[parser->offset + i];
241 SDID = data[parser->offset + i + 1];
242 DC = data[parser->offset + i + 2];
245 /* Check if we have enough room to get the User Data and checksum */
246 if (parser->offset + i + DC + 1 >= parser->work_data_size)
247 goto not_enough_data;
249 /* We found a valid ANC \o/ */
251 anc->SDID_block_number = SDID;
252 anc->data_count = DC;
253 memset (anc->data, 0, 256);
255 /* i is at the beginning of the user data now */
256 for (j = 0; j < anc->data_count; j++)
257 anc->data[j] = data[parser->offset + i + j] & 0xff;
260 /* Checksum calculation SMPTE S291 3.2.1 */
261 for (j = (composite ? 1 : 3); j < i; j++)
262 checksum += data[parser->offset + j];
265 if (checksum != data[parser->offset + i]) {
266 GST_WARNING ("ADF checksum mismatch: expected 0x%02x, got 0x%02x",
267 checksum, data[parser->offset + i]);
280 return GST_VIDEO_VBI_PARSER_RESULT_OK;
282 return GST_VIDEO_VBI_PARSER_RESULT_DONE;
287 GST_WARNING ("ANC requires more User Data than available line size");
288 /* Avoid further calls to go in the same error */
289 parser->offset = parser->work_data_size;
290 return GST_VIDEO_VBI_PARSER_RESULT_ERROR;
295 * gst_video_vbi_parser_get_ancillary:
296 * @parser: a #GstVideoVBIParser
297 * @anc: (out caller-allocates): a #GstVideoAncillary to start the eventual ancillary data
299 * Parse the line provided previously by gst_video_vbi_parser_add_line().
303 * Returns: %GST_VIDEO_VBI_PARSER_RESULT_OK if ancillary data was found and
304 * @anc was filled. %GST_VIDEO_VBI_PARSER_RESULT_DONE if there wasn't any
308 GstVideoVBIParserResult
309 gst_video_vbi_parser_get_ancillary (GstVideoVBIParser * parser,
310 GstVideoAncillary * anc)
312 g_return_val_if_fail (parser != NULL, GST_VIDEO_VBI_PARSER_RESULT_ERROR);
313 g_return_val_if_fail (anc != NULL, GST_VIDEO_VBI_PARSER_RESULT_ERROR);
316 return get_ancillary_16 (parser, anc);
317 return get_ancillary_8 (parser, anc);
321 * gst_video_vbi_parser_new:
322 * @format: a #GstVideoFormat
323 * @pixel_width: The width in pixel to use
325 * Create a new #GstVideoVBIParser for the specified @format and @pixel_width.
329 * Returns: (nullable): The new #GstVideoVBIParser or %NULL if the @format and/or @pixel_width
333 gst_video_vbi_parser_new (GstVideoFormat format, guint32 pixel_width)
335 GstVideoVBIParser *parser;
337 g_return_val_if_fail (pixel_width > 0, NULL);
340 case GST_VIDEO_FORMAT_v210:
341 parser = g_new0 (GstVideoVBIParser, 1);
342 parser->bit16 = TRUE;
344 case GST_VIDEO_FORMAT_UYVY:
345 parser = g_new0 (GstVideoVBIParser, 1);
346 parser->bit16 = FALSE;
349 GST_WARNING ("Format not supported by GstVideoVBIParser");
353 gst_video_info_init (&parser->info);
354 if (!gst_video_info_set_format (&parser->info, format, pixel_width, 1)) {
355 GST_ERROR ("Could not create GstVideoInfo");
360 /* Allocate the workspace which is going to be 2 * pixel_width big
361 * 2 : number of pixels per "component" (we only deal with 4:2:2)
362 * We use 1 or 2 bytes per pixel depending on whether we are internally
363 * working in 8 or 16bit */
364 parser->work_data_size = 2 * pixel_width;
366 parser->work_data = g_malloc0 (parser->work_data_size * 2);
368 parser->work_data = g_malloc0 (parser->work_data_size);
375 * gst_video_vbi_parser_free:
376 * @parser: a #GstVideoVBIParser
383 gst_video_vbi_parser_free (GstVideoVBIParser * parser)
385 g_return_if_fail (parser != NULL);
387 g_free (parser->work_data);
392 convert_line_from_uyvy (GstVideoVBIParser * parser, const guint8 * data)
395 guint8 *y = parser->work_data;
397 /* Data is stored differently in SD, making no distinction between Y and UV */
398 if (parser->info.width < 1280) {
399 for (i = 0; i < parser->info.width - 3; i += 4) {
400 *y++ = data[(i / 4) * 4 + 0];
401 *y++ = data[(i / 4) * 4 + 1];
402 *y++ = data[(i / 4) * 4 + 2];
403 *y++ = data[(i / 4) * 4 + 3];
406 guint8 *uv = y + parser->info.width;
408 for (i = 0; i < parser->info.width - 3; i += 4) {
409 *uv++ = data[(i / 4) * 4 + 0];
410 *y++ = data[(i / 4) * 4 + 1];
411 *uv++ = data[(i / 4) * 4 + 2];
412 *y++ = data[(i / 4) * 4 + 3];
415 GST_MEMDUMP ("Converted line", parser->work_data, 128);
419 gst_info_dump_mem16_line (gchar * linebuf, gsize linebuf_size,
420 const guint16 * mem, gsize mem_offset, gsize mem_size)
422 gchar hexstr[50], digitstr[6];
433 while (i < mem_size) {
434 g_snprintf (digitstr, sizeof (digitstr), "%04x ", mem[i]);
435 g_strlcat (hexstr, digitstr, sizeof (hexstr));
440 g_snprintf (linebuf, linebuf_size, "%08x: %-48.48s",
441 (guint) mem_offset, hexstr);
445 convert_line_from_v210 (GstVideoVBIParser * parser, const guint8 * data)
448 guint16 *y = (guint16 *) parser->work_data;
451 /* Data is stored differently in SD, making no distinction between Y and UV */
452 if (parser->info.width < 1280) {
453 /* Convert the line */
454 for (i = 0; i < parser->info.width - 5; i += 6) {
455 a = GST_READ_UINT32_LE (data + (i / 6) * 16 + 0);
456 b = GST_READ_UINT32_LE (data + (i / 6) * 16 + 4);
457 c = GST_READ_UINT32_LE (data + (i / 6) * 16 + 8);
458 d = GST_READ_UINT32_LE (data + (i / 6) * 16 + 12);
460 *y++ = (a >> 0) & 0x3ff;
461 *y++ = (a >> 10) & 0x3ff;
462 *y++ = (a >> 20) & 0x3ff;
463 *y++ = (b >> 0) & 0x3ff;
465 *y++ = (b >> 10) & 0x3ff;
466 *y++ = (b >> 20) & 0x3ff;
467 *y++ = (c >> 0) & 0x3ff;
468 *y++ = (c >> 10) & 0x3ff;
470 *y++ = (c >> 20) & 0x3ff;
471 *y++ = (d >> 0) & 0x3ff;
472 *y++ = (d >> 10) & 0x3ff;
473 *y++ = (d >> 20) & 0x3ff;
476 guint16 *uv = y + parser->info.width;
478 /* Convert the line */
479 for (i = 0; i < parser->info.width - 5; i += 6) {
480 a = GST_READ_UINT32_LE (data + (i / 6) * 16 + 0);
481 b = GST_READ_UINT32_LE (data + (i / 6) * 16 + 4);
482 c = GST_READ_UINT32_LE (data + (i / 6) * 16 + 8);
483 d = GST_READ_UINT32_LE (data + (i / 6) * 16 + 12);
485 *uv++ = (a >> 0) & 0x3ff;
486 *y++ = (a >> 10) & 0x3ff;
487 *uv++ = (a >> 20) & 0x3ff;
488 *y++ = (b >> 0) & 0x3ff;
490 *uv++ = (b >> 10) & 0x3ff;
491 *y++ = (b >> 20) & 0x3ff;
492 *uv++ = (c >> 0) & 0x3ff;
493 *y++ = (c >> 10) & 0x3ff;
495 *uv++ = (c >> 20) & 0x3ff;
496 *y++ = (d >> 0) & 0x3ff;
497 *uv++ = (d >> 10) & 0x3ff;
498 *y++ = (d >> 20) & 0x3ff;
504 gsize length = parser->info.width * 2;
506 GST_TRACE ("--------"
507 "-------------------------------------------------------------------");
509 while (off < length) {
512 /* gst_info_dump_mem_line will process 16 bytes (8 16bit chunks) at most */
513 gst_info_dump_mem16_line (buf, sizeof (buf),
514 (guint16 *) parser->work_data, off, length - off);
515 GST_TRACE ("%s", buf);
518 GST_TRACE ("--------"
519 "-------------------------------------------------------------------");
524 * gst_video_vbi_parser_add_line:
525 * @parser: a #GstVideoVBIParser
526 * @data: (array) (transfer none): The line of data to parse
528 * Provide a new line of data to the @parser. Call gst_video_vbi_parser_get_ancillary()
529 * to get the Ancillary data that might be present on that line.
534 gst_video_vbi_parser_add_line (GstVideoVBIParser * parser, const guint8 * data)
536 g_return_if_fail (parser != NULL);
537 g_return_if_fail (data != NULL);
542 switch (GST_VIDEO_INFO_FORMAT (&parser->info)) {
543 case GST_VIDEO_FORMAT_v210:
544 convert_line_from_v210 (parser, data);
546 case GST_VIDEO_FORMAT_UYVY:
547 convert_line_from_uyvy (parser, data);
550 GST_ERROR ("UNSUPPORTED FORMAT !");
551 g_assert_not_reached ();
556 struct _GstVideoVBIEncoder
558 GstVideoInfo info; /* format of the lines provided */
559 guint8 *work_data; /* Converted line in planar 16bit format */
560 guint32 work_data_size; /* Size in bytes of work_data */
561 guint offset; /* Current offset (in bytes) in work_data */
562 gboolean bit16; /* Data is stored as 16bit if TRUE. Else 8bit(without parity) */
565 G_DEFINE_BOXED_TYPE (GstVideoVBIEncoder, gst_video_vbi_encoder,
566 (GBoxedCopyFunc) gst_video_vbi_encoder_copy,
567 (GBoxedFreeFunc) gst_video_vbi_encoder_free);
570 gst_video_vbi_encoder_copy (const GstVideoVBIEncoder * encoder)
572 GstVideoVBIEncoder *res;
574 g_return_val_if_fail (encoder != NULL, NULL);
576 res = gst_video_vbi_encoder_new (GST_VIDEO_INFO_FORMAT (&encoder->info),
577 encoder->info.width);
579 memcpy (res->work_data, encoder->work_data, encoder->work_data_size);
585 * gst_video_vbi_encoder_free:
586 * @encoder: a #GstVideoVBIEncoder
588 * Frees the @encoder.
593 gst_video_vbi_encoder_free (GstVideoVBIEncoder * encoder)
595 g_return_if_fail (encoder != NULL);
597 g_free (encoder->work_data);
602 * gst_video_vbi_encoder_new:
603 * @format: a #GstVideoFormat
604 * @pixel_width: The width in pixel to use
606 * Create a new #GstVideoVBIEncoder for the specified @format and @pixel_width.
610 * Returns: (nullable): The new #GstVideoVBIEncoder or %NULL if the @format and/or @pixel_width
614 gst_video_vbi_encoder_new (GstVideoFormat format, guint32 pixel_width)
616 GstVideoVBIEncoder *encoder;
618 g_return_val_if_fail (pixel_width > 0, NULL);
621 case GST_VIDEO_FORMAT_v210:
622 encoder = g_new0 (GstVideoVBIEncoder, 1);
623 encoder->bit16 = TRUE;
625 case GST_VIDEO_FORMAT_UYVY:
626 encoder = g_new0 (GstVideoVBIEncoder, 1);
627 encoder->bit16 = FALSE;
630 GST_WARNING ("Format not supported by GstVideoVBIEncoder");
634 gst_video_info_init (&encoder->info);
635 if (!gst_video_info_set_format (&encoder->info, format, pixel_width, 1)) {
636 GST_ERROR ("Could not create GstVideoInfo");
641 /* Allocate the workspace which is going to be 2 * pixel_width big
642 * 2 : number of pixels per "component" (we only deal with 4:2:2)
643 * We use 1 or 2 bytes per pixel depending on whether we are internally
644 * working in 8 or 16bit */
645 encoder->work_data_size = 2 * pixel_width;
647 encoder->work_data = g_malloc0 (encoder->work_data_size * 2);
649 encoder->work_data = g_malloc0 (encoder->work_data_size);
655 #if G_GNUC_CHECK_VERSION(3,4)
659 return __builtin_parity (x);
676 /* Odd/even parity in the upper two bits */
677 #define SET_WITH_PARITY(buf, val) G_STMT_START { \
686 * gst_video_vbi_encoder_add_ancillary:
687 * @encoder: a #GstVideoVBIEncoder
688 * @composite: %TRUE if composite ADF should be created, component otherwise
689 * @DID: The Data Identifier
690 * @SDID_block_number: The Secondary Data Identifier (if type 2) or the Data
691 * Block Number (if type 1)
692 * @data_count: The amount of data (in bytes) in @data (max 255 bytes)
693 * @data: (array length=data_count): The user data content of the Ancillary packet.
694 * Does not contain the ADF, DID, SDID nor CS.
696 * Stores Video Ancillary data, according to SMPTE-291M specification.
698 * Note that the contents of the data are always read as 8bit data (i.e. do not contain
699 * the parity check bits).
703 * Returns: %TRUE if enough space was left in the current line, %FALSE
707 gst_video_vbi_encoder_add_ancillary (GstVideoVBIEncoder * encoder,
708 gboolean composite, guint8 DID, guint8 SDID_block_number,
709 const guint8 * data, guint data_count)
711 g_return_val_if_fail (encoder != NULL, FALSE);
712 g_return_val_if_fail (data != NULL, FALSE);
713 g_return_val_if_fail (data_count < 256, FALSE);
715 /* Doesn't fit into this line anymore */
716 if (encoder->offset + data_count + (composite ? 5 : 7) >
717 encoder->work_data_size)
720 if (encoder->bit16) {
721 guint16 *work_data = ((guint16 *) encoder->work_data) + encoder->offset;
727 work_data[i] = 0x3fc;
730 work_data[i] = 0x000;
731 work_data[i + 1] = 0x3ff;
732 work_data[i + 2] = 0x3ff;
736 SET_WITH_PARITY (&work_data[i], DID);
737 SET_WITH_PARITY (&work_data[i + 1], SDID_block_number);
738 SET_WITH_PARITY (&work_data[i + 2], data_count);
741 for (j = 0; j < data_count; j++)
742 SET_WITH_PARITY (&work_data[i + j], data[j]);
745 for (j = (composite ? 1 : 3); j < i; j++)
746 checksum += work_data[j];
748 checksum |= (!(checksum >> 8)) << 9;
750 work_data[i] = checksum;
753 encoder->offset += i;
755 guint8 *work_data = ((guint8 *) encoder->work_data) + encoder->offset;
765 work_data[i + 1] = 0xff;
766 work_data[i + 2] = 0xff;
771 work_data[i + 1] = SDID_block_number;
772 work_data[i + 2] = data_count;
775 for (j = 0; j < data_count; j++)
776 work_data[i + j] = data[j];
779 for (j = (composite ? 1 : 3); j < i; j++)
780 checksum += work_data[j];
783 work_data[i] = checksum;
786 encoder->offset += i;
793 convert_line_to_v210 (GstVideoVBIEncoder * encoder, guint8 * data)
796 const guint16 *y = (const guint16 *) encoder->work_data;
799 /* Data is stored differently in SD, making no distinction between Y and UV */
800 if (encoder->info.width < 1280) {
801 /* Convert the line */
802 for (i = 0; i < encoder->info.width - 5; i += 6) {
803 a = ((y[0] & 0x3ff) << 0)
804 | ((y[1] & 0x3ff) << 10)
805 | ((y[2] & 0x3ff) << 20);
808 b = ((y[0] & 0x3ff) << 0)
809 | ((y[1] & 0x3ff) << 10)
810 | ((y[2] & 0x3ff) << 20);
813 c = ((y[0] & 0x3ff) << 0)
814 | ((y[1] & 0x3ff) << 10)
815 | ((y[2] & 0x3ff) << 20);
818 d = ((y[0] & 0x3ff) << 0)
819 | ((y[1] & 0x3ff) << 10)
820 | ((y[2] & 0x3ff) << 20);
823 GST_WRITE_UINT32_LE (data + (i / 6) * 16 + 0, a);
824 GST_WRITE_UINT32_LE (data + (i / 6) * 16 + 4, b);
825 GST_WRITE_UINT32_LE (data + (i / 6) * 16 + 8, c);
826 GST_WRITE_UINT32_LE (data + (i / 6) * 16 + 12, d);
829 const guint16 *uv = y + encoder->info.width;
831 /* Convert the line */
832 for (i = 0; i < encoder->info.width - 5; i += 6) {
833 a = ((uv[0] & 0x3ff) << 0)
834 | ((y[0] & 0x3ff) << 10)
835 | ((uv[1] & 0x3ff) << 20);
839 b = ((y[0] & 0x3ff) << 0)
840 | ((uv[0] & 0x3ff) << 10)
841 | ((y[1] & 0x3ff) << 20);
845 c = ((uv[0] & 0x3ff) << 0)
846 | ((y[0] & 0x3ff) << 10)
847 | ((uv[1] & 0x3ff) << 20);
851 d = ((y[0] & 0x3ff) << 0)
852 | ((uv[0] & 0x3ff) << 10)
853 | ((y[1] & 0x3ff) << 20);
857 GST_WRITE_UINT32_LE (data + (i / 6) * 16 + 0, a);
858 GST_WRITE_UINT32_LE (data + (i / 6) * 16 + 4, b);
859 GST_WRITE_UINT32_LE (data + (i / 6) * 16 + 8, c);
860 GST_WRITE_UINT32_LE (data + (i / 6) * 16 + 12, d);
866 convert_line_to_uyvy (GstVideoVBIEncoder * encoder, guint8 * data)
869 const guint8 *y = encoder->work_data;
871 /* Data is stored differently in SD, making no distinction between Y and UV */
872 if (encoder->info.width < 1280) {
873 for (i = 0; i < encoder->info.width - 3; i += 4) {
874 data[(i / 4) * 4 + 0] = *y++;
875 data[(i / 4) * 4 + 1] = *y++;
876 data[(i / 4) * 4 + 2] = *y++;
877 data[(i / 4) * 4 + 3] = *y++;
880 const guint8 *uv = y + encoder->info.width;
882 for (i = 0; i < encoder->info.width - 3; i += 4) {
883 data[(i / 4) * 4 + 0] = *uv++;
884 data[(i / 4) * 4 + 1] = *y++;
885 data[(i / 4) * 4 + 2] = *uv++;
886 data[(i / 4) * 4 + 3] = *y++;
892 gst_video_vbi_encoder_write_line (GstVideoVBIEncoder * encoder, guint8 * data)
894 g_return_if_fail (encoder != NULL);
895 g_return_if_fail (data != NULL);
897 /* nothing to write? just exit early */
898 if (!encoder->offset)
901 switch (GST_VIDEO_INFO_FORMAT (&encoder->info)) {
902 case GST_VIDEO_FORMAT_v210:
903 convert_line_to_v210 (encoder, data);
905 case GST_VIDEO_FORMAT_UYVY:
906 convert_line_to_uyvy (encoder, data);
909 GST_ERROR ("UNSUPPORTED FORMAT !");
910 g_assert_not_reached ();
915 memset (encoder->work_data, 0,
916 encoder->work_data_size * (encoder->bit16 ? 2 : 1));
919 /* Closed Caption Meta implementation *******************************************/
922 gst_video_caption_meta_api_get_type (void)
924 static GType type = 0;
926 if (g_once_init_enter (&type)) {
927 static const gchar *tags[] = { NULL };
928 GType _type = gst_meta_api_type_register ("GstVideoCaptionMetaAPI", tags);
929 GST_INFO ("registering");
930 g_once_init_leave (&type, _type);
937 gst_video_caption_meta_transform (GstBuffer * dest, GstMeta * meta,
938 GstBuffer * buffer, GQuark type, gpointer data)
940 GstVideoCaptionMeta *dmeta, *smeta;
942 /* We always copy over the caption meta */
943 smeta = (GstVideoCaptionMeta *) meta;
945 GST_DEBUG ("copy caption metadata");
947 gst_buffer_add_video_caption_meta (dest, smeta->caption_type,
948 smeta->data, smeta->size);
956 gst_video_caption_meta_init (GstMeta * meta, gpointer params,
959 GstVideoCaptionMeta *emeta = (GstVideoCaptionMeta *) meta;
961 emeta->caption_type = GST_VIDEO_CAPTION_TYPE_UNKNOWN;
969 gst_video_caption_meta_free (GstMeta * meta, GstBuffer * buffer)
971 GstVideoCaptionMeta *emeta = (GstVideoCaptionMeta *) meta;
973 g_free (emeta->data);
977 gst_video_caption_meta_get_info (void)
979 static const GstMetaInfo *meta_info = NULL;
981 if (g_once_init_enter ((GstMetaInfo **) & meta_info)) {
982 const GstMetaInfo *mi = gst_meta_register (GST_VIDEO_CAPTION_META_API_TYPE,
983 "GstVideoCaptionMeta",
984 sizeof (GstVideoCaptionMeta),
985 gst_video_caption_meta_init,
986 gst_video_caption_meta_free,
987 gst_video_caption_meta_transform);
988 g_once_init_leave ((GstMetaInfo **) & meta_info, (GstMetaInfo *) mi);
994 * gst_buffer_add_video_caption_meta:
995 * @buffer: a #GstBuffer
996 * @caption_type: The type of Closed Caption to add
997 * @data: (array length=size) (transfer none): The Closed Caption data
998 * @size: The size of @data in bytes
1000 * Attaches #GstVideoCaptionMeta metadata to @buffer with the given
1003 * Returns: (transfer none): the #GstVideoCaptionMeta on @buffer.
1007 GstVideoCaptionMeta *
1008 gst_buffer_add_video_caption_meta (GstBuffer * buffer,
1009 GstVideoCaptionType caption_type, const guint8 * data, gsize size)
1011 GstVideoCaptionMeta *meta;
1013 g_return_val_if_fail (GST_IS_BUFFER (buffer), NULL);
1014 g_return_val_if_fail (data != NULL, NULL);
1015 g_return_val_if_fail (size > 0, NULL);
1017 switch (caption_type) {
1018 case GST_VIDEO_CAPTION_TYPE_CEA608_RAW:
1019 case GST_VIDEO_CAPTION_TYPE_CEA608_S334_1A:
1020 case GST_VIDEO_CAPTION_TYPE_CEA708_RAW:
1021 case GST_VIDEO_CAPTION_TYPE_CEA708_CDP:
1024 GST_ERROR ("Unknown caption type !");
1027 /* FIXME : Add checks for content ? */
1029 meta = (GstVideoCaptionMeta *) gst_buffer_add_meta (buffer,
1030 GST_VIDEO_CAPTION_META_INFO, NULL);
1031 g_return_val_if_fail (meta != NULL, NULL);
1033 meta->caption_type = caption_type;
1034 meta->data = g_memdup2 (data, size);
1041 * gst_video_caption_type_from_caps:
1042 * @caps: Fixed #GstCaps to parse
1044 * Parses fixed Closed Caption #GstCaps and returns the corresponding caption
1045 * type, or %GST_VIDEO_CAPTION_TYPE_UNKNOWN.
1047 * Returns: #GstVideoCaptionType.
1052 gst_video_caption_type_from_caps (const GstCaps * caps)
1054 const GstStructure *s;
1055 const gchar *format;
1057 g_return_val_if_fail (gst_caps_is_fixed (caps),
1058 GST_VIDEO_CAPTION_TYPE_UNKNOWN);
1060 s = gst_caps_get_structure (caps, 0);
1061 g_return_val_if_fail (s != NULL, GST_VIDEO_CAPTION_TYPE_UNKNOWN);
1063 format = gst_structure_get_string (s, "format");
1064 if (gst_structure_has_name (s, "closedcaption/x-cea-608")) {
1065 if (g_strcmp0 (format, "raw") == 0) {
1066 return GST_VIDEO_CAPTION_TYPE_CEA608_RAW;
1067 } else if (g_strcmp0 (format, "s334-1a") == 0) {
1068 return GST_VIDEO_CAPTION_TYPE_CEA608_S334_1A;
1070 } else if (gst_structure_has_name (s, "closedcaption/x-cea-708")) {
1071 if (g_strcmp0 (format, "cc_data") == 0) {
1072 return GST_VIDEO_CAPTION_TYPE_CEA708_RAW;
1073 } else if (g_strcmp0 (format, "cdp") == 0) {
1074 return GST_VIDEO_CAPTION_TYPE_CEA708_CDP;
1077 return GST_VIDEO_CAPTION_TYPE_UNKNOWN;
1081 * gst_video_caption_type_to_caps:
1082 * @type: #GstVideoCaptionType
1084 * Creates new caps corresponding to @type.
1086 * Returns: (transfer full): new #GstCaps
1091 gst_video_caption_type_to_caps (GstVideoCaptionType type)
1093 GstCaps *caption_caps;
1095 g_return_val_if_fail (type != GST_VIDEO_CAPTION_TYPE_UNKNOWN, NULL);
1098 case GST_VIDEO_CAPTION_TYPE_CEA608_RAW:
1099 caption_caps = gst_caps_new_simple ("closedcaption/x-cea-608",
1100 "format", G_TYPE_STRING, "raw", NULL);
1102 case GST_VIDEO_CAPTION_TYPE_CEA608_S334_1A:
1103 caption_caps = gst_caps_new_simple ("closedcaption/x-cea-608",
1104 "format", G_TYPE_STRING, "s334-1a", NULL);
1106 case GST_VIDEO_CAPTION_TYPE_CEA708_RAW:
1107 caption_caps = gst_caps_new_simple ("closedcaption/x-cea-708",
1108 "format", G_TYPE_STRING, "cc_data", NULL);
1110 case GST_VIDEO_CAPTION_TYPE_CEA708_CDP:
1111 caption_caps = gst_caps_new_simple ("closedcaption/x-cea-708",
1112 "format", G_TYPE_STRING, "cdp", NULL);
1115 g_return_val_if_reached (NULL);
1119 return caption_caps;
1122 /* Active Format Description (AFD) Meta implementation */
1125 gst_video_afd_meta_api_get_type (void)
1127 static GType type = 0;
1129 if (g_once_init_enter (&type)) {
1130 static const gchar *tags[] = {
1131 GST_META_TAG_VIDEO_SIZE_STR,
1132 GST_META_TAG_VIDEO_ORIENTATION_STR,
1133 GST_META_TAG_VIDEO_STR,
1136 GType _type = gst_meta_api_type_register ("GstVideoAFDMetaAPI", tags);
1137 g_once_init_leave (&type, _type);
1143 gst_video_afd_meta_init (GstMeta * meta, gpointer params, GstBuffer * buffer)
1145 GstVideoAFDMeta *emeta = (GstVideoAFDMeta *) meta;
1148 emeta->spec = GST_VIDEO_AFD_SPEC_ATSC_A53;
1149 emeta->afd = GST_VIDEO_AFD_UNAVAILABLE;
1155 gst_video_afd_meta_transform (GstBuffer * dest, GstMeta * meta,
1156 GstBuffer * buffer, GQuark type, gpointer data)
1158 GstVideoAFDMeta *smeta = (GstVideoAFDMeta *) meta;
1160 if (GST_META_TRANSFORM_IS_COPY (type)) {
1161 GST_DEBUG ("copy AFD metadata");
1162 gst_buffer_add_video_afd_meta (dest, smeta->field, smeta->spec, smeta->afd);
1164 } else if (GST_VIDEO_META_TRANSFORM_IS_SCALE (type)) {
1165 GstVideoMetaTransform *trans = data;
1167 gint ow, oh, nw, nh;
1168 gint opn, opd, npn, npd;
1170 ow = GST_VIDEO_INFO_WIDTH (trans->in_info);
1171 nw = GST_VIDEO_INFO_WIDTH (trans->out_info);
1172 oh = GST_VIDEO_INFO_HEIGHT (trans->in_info);
1173 nh = GST_VIDEO_INFO_HEIGHT (trans->out_info);
1174 opn = GST_VIDEO_INFO_PAR_N (trans->in_info);
1175 opd = GST_VIDEO_INFO_PAR_D (trans->in_info);
1176 npn = GST_VIDEO_INFO_PAR_N (trans->out_info);
1177 npd = GST_VIDEO_INFO_PAR_D (trans->out_info);
1179 /* if the aspect ratio stays the same we can copy the meta, otherwise
1180 * we can't know if the aspect ratio was changed or black borders were
1181 * introduced. Both would invalidate the AFD meta */
1184 ABS (((gdouble) ow / (gdouble) oh) * ((gdouble) opn / (gdouble) opd) -
1185 ((gdouble) nw / (gdouble) nh) * ((gdouble) npn / (gdouble) npd));
1186 if (diff < 0.0001) {
1187 GST_DEBUG ("copying AFD metadata, aspect ratio did not change");
1188 gst_buffer_add_video_afd_meta (dest, smeta->field, smeta->spec,
1195 /* return FALSE, if transform type is not supported */
1202 gst_video_afd_meta_get_info (void)
1204 static const GstMetaInfo *meta_info = NULL;
1206 if (g_once_init_enter ((GstMetaInfo **) & meta_info)) {
1207 const GstMetaInfo *mi = gst_meta_register (GST_VIDEO_AFD_META_API_TYPE,
1209 sizeof (GstVideoAFDMeta),
1210 gst_video_afd_meta_init,
1212 gst_video_afd_meta_transform);
1213 g_once_init_leave ((GstMetaInfo **) & meta_info, (GstMetaInfo *) mi);
1219 * gst_buffer_add_video_afd_meta:
1220 * @buffer: a #GstBuffer
1221 * @field: 0 for progressive or field 1 and 1 for field 2
1222 * @spec: #GstVideoAFDSpec that applies to AFD value
1223 * @afd: #GstVideoAFDValue AFD enumeration
1225 * Attaches #GstVideoAFDMeta metadata to @buffer with the given
1228 * Returns: (transfer none): the #GstVideoAFDMeta on @buffer.
1233 gst_buffer_add_video_afd_meta (GstBuffer * buffer, guint8 field,
1234 GstVideoAFDSpec spec, GstVideoAFDValue afd)
1236 GstVideoAFDMeta *meta;
1237 gint8 afd_data = (gint8) afd;
1238 g_return_val_if_fail (GST_IS_BUFFER (buffer), NULL);
1239 g_return_val_if_fail (field <= 1, NULL);
1240 g_return_val_if_fail ((guint8) spec <= 2, NULL);
1241 /* AFD is stored in a nybble */
1242 g_return_val_if_fail (afd_data <= 0xF, NULL);
1243 /* reserved values for all specifications */
1244 g_return_val_if_fail (afd_data != 1 && (afd_data < 5 || afd_data > 7)
1245 && afd_data != 12, NULL);
1246 /* reserved for DVB/ETSI */
1247 g_return_val_if_fail ((spec != GST_VIDEO_AFD_SPEC_DVB_ETSI)
1248 || (afd_data != 0), NULL);
1250 meta = (GstVideoAFDMeta *) gst_buffer_add_meta (buffer,
1251 GST_VIDEO_AFD_META_INFO, NULL);
1252 g_assert (meta != NULL);
1254 meta->field = field;
1261 /* Bar Meta implementation */
1264 gst_video_bar_meta_api_get_type (void)
1266 static GType type = 0;
1268 if (g_once_init_enter (&type)) {
1269 static const gchar *tags[] = {
1270 GST_META_TAG_VIDEO_SIZE_STR,
1271 GST_META_TAG_VIDEO_ORIENTATION_STR,
1272 GST_META_TAG_VIDEO_STR,
1275 GType _type = gst_meta_api_type_register ("GstVideoBarMetaAPI", tags);
1276 g_once_init_leave (&type, _type);
1282 gst_video_bar_meta_init (GstMeta * meta, gpointer params, GstBuffer * buffer)
1284 GstVideoBarMeta *emeta = (GstVideoBarMeta *) meta;
1287 emeta->is_letterbox = FALSE;
1288 emeta->bar_data1 = 0;
1289 emeta->bar_data2 = 0;
1295 gst_video_bar_meta_transform (GstBuffer * dest, GstMeta * meta,
1296 GstBuffer * buffer, GQuark type, gpointer data)
1298 GstVideoBarMeta *smeta = (GstVideoBarMeta *) meta;
1300 if (GST_META_TRANSFORM_IS_COPY (type)) {
1301 GST_DEBUG ("copy Bar metadata");
1302 gst_buffer_add_video_bar_meta (dest, smeta->field, smeta->is_letterbox,
1303 smeta->bar_data1, smeta->bar_data2);
1306 /* return FALSE, if transform type is not supported */
1312 gst_video_bar_meta_get_info (void)
1314 static const GstMetaInfo *meta_info = NULL;
1316 if (g_once_init_enter ((GstMetaInfo **) & meta_info)) {
1317 const GstMetaInfo *mi = gst_meta_register (GST_VIDEO_BAR_META_API_TYPE,
1319 sizeof (GstVideoBarMeta),
1320 gst_video_bar_meta_init,
1322 gst_video_bar_meta_transform);
1323 g_once_init_leave ((GstMetaInfo **) & meta_info, (GstMetaInfo *) mi);
1329 * gst_buffer_add_video_bar_meta:
1330 * @buffer: a #GstBuffer
1331 * @field: 0 for progressive or field 1 and 1 for field 2
1332 * @is_letterbox: if true then bar data specifies letterbox, otherwise pillarbox
1333 * @bar_data1: If @is_letterbox is true, then the value specifies the
1334 * last line of a horizontal letterbox bar area at top of reconstructed frame.
1335 * Otherwise, it specifies the last horizontal luminance sample of a vertical pillarbox
1336 * bar area at the left side of the reconstructed frame
1337 * @bar_data2: If @is_letterbox is true, then the value specifies the
1338 * first line of a horizontal letterbox bar area at bottom of reconstructed frame.
1339 * Otherwise, it specifies the first horizontal
1340 * luminance sample of a vertical pillarbox bar area at the right side of the reconstructed frame.
1342 * Attaches #GstVideoBarMeta metadata to @buffer with the given
1345 * Returns: (transfer none): the #GstVideoBarMeta on @buffer.
1347 * See Table 6.11 Bar Data Syntax
1349 * https://www.atsc.org/wp-content/uploads/2015/03/a_53-Part-4-2009.pdf
1354 gst_buffer_add_video_bar_meta (GstBuffer * buffer, guint8 field,
1355 gboolean is_letterbox, guint bar_data1, guint bar_data2)
1357 GstVideoBarMeta *meta;
1359 g_return_val_if_fail (GST_IS_BUFFER (buffer), NULL);
1360 g_return_val_if_fail (field <= 1, NULL);
1362 meta = (GstVideoBarMeta *) gst_buffer_add_meta (buffer,
1363 GST_VIDEO_BAR_META_INFO, NULL);
1364 g_assert (meta != NULL);
1366 meta->field = field;
1367 meta->is_letterbox = is_letterbox;
1368 meta->bar_data1 = bar_data1;
1369 meta->bar_data2 = bar_data2;