1 /* GStreamer
2 * Copyright (2019) Collabora Ltd.
3 * Contact: Aaron Boxer <aaron.boxer@collabora.com>
4 *
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Library General Public
7 * License as published by the Free Software Foundation; either
8 * version 2 of the License, or (at your option) any later version.
9 *
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Library General Public License for more details.
14 *
15 * You should have received a copy of the GNU Library General Public
16 * License along with this library; if not, write to the
17 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
18 * Boston, MA 02110-1301, USA.
19 */
20
21 #ifdef HAVE_CONFIG_H
22 #include "config.h"
23 #endif
24
25 #include <string.h>
26 #include <gst/base/base.h>
27 #include <gst/pbutils/pbutils.h>
28 #include <gst/video/video.h>
29 #include <gst/base/gstbitreader.h>
30 #include <gstvideoparseutils.h>
31
32 GST_DEBUG_CATEGORY_EXTERN (videoparseutils_debug);
33 #define GST_CAT_DEFAULT videoparseutils_debug
34
35 static gboolean gst_video_parse_utils_parse_bar (const guint8 * data,
36 gsize size, guint field, GstVideoBarData * bar);
37
38 static gboolean gst_video_parse_utils_parse_afd (const guint8 data,
39 GstVideoAFD * afd, GstVideoAFDSpec spec);
40
41
42 /*
43 * gst_video_parse_user_data:
44 * @elt: #GstElement that is parsing user data
45 * @user_data: #GstVideoParseUserData struct to hold parsed closed caption, bar and AFD data
46 * @br: #GstByteReader attached to buffer of user data
47 * @field: 0 for progressive or field 1 and 1 for field 2
48 * @provider_code: Currently, only (US) ATSC and DirecTV provider codes are supported
49 *
50 * Parse user data and store in @user_data
51 */
52 void
gst_video_parse_user_data(GstElement * elt,GstVideoParseUserData * user_data,GstByteReader * br,guint8 field,guint16 provider_code)53 gst_video_parse_user_data (GstElement * elt, GstVideoParseUserData * user_data,
54 GstByteReader * br, guint8 field, guint16 provider_code)
55 {
56
57 guint32 user_data_id = 0;
58 guint8 user_data_type_code = 0;
59 gboolean a53_process_708_cc_data = FALSE;
60 gboolean process_708_em_data = FALSE;
61 guint8 temp = 0;
62 guint8 cc_count;
63 guint cc_size;
64 guint bar_size = 0;
65 const guint8 *data = NULL;
66 guint8 directv_size = 0;
67
68
69 /* https://en.wikipedia.org/wiki/CEA-708#Picture_User_Data */
70 switch (provider_code) {
71 case ITU_T_T35_MANUFACTURER_US_ATSC:
72 if (!gst_byte_reader_peek_uint32_be (br, &user_data_id)) {
73 GST_WARNING_OBJECT (elt, "Missing user data id, ignoring");
74 return;
75 }
76 switch (user_data_id) {
77 case A53_USER_DATA_ID_DTG1:
78 case A53_USER_DATA_ID_GA94:
79 /* ANSI/SCTE 128-2010a section 8.1.2 */
80 if (!gst_byte_reader_get_uint32_be (br, &user_data_id)) {
81 GST_WARNING_OBJECT (elt, "Missing user data id, ignoring");
82 return;
83 }
84 break;
85 default:
86 /* check for SCTE 20 */
87 if (user_data_id >> 24 == A53_USER_DATA_TYPE_CODE_CC_DATA) {
88 user_data_id = USER_DATA_ID_SCTE_20_CC;
89 gst_byte_reader_skip (br, 1);
90 }
91 break;
92 }
93 break;
94 case ITU_T_T35_MANUFACTURER_US_DIRECTV:
95 user_data_id = USER_DATA_ID_DIRECTV_CC;
96 break;
97 default:
98 GST_LOG_OBJECT (elt, "Unsupported provider code %d", provider_code);
99 return;
100 }
101
102 switch (user_data_id) {
103 case USER_DATA_ID_SCTE_20_CC:
104 GST_DEBUG_OBJECT (elt, "Unsupported SCTE 20 closed captions");
105 break;
106 case A53_USER_DATA_ID_DTG1:
107 if (!gst_byte_reader_get_uint8 (br, &temp)) {
108 GST_WARNING_OBJECT (elt, "Missing active format flag, ignoring");
109 break;
110 }
111
112 /* check active format flag for presence of AFD */
113 if (temp & 0x40) {
114 if (!gst_byte_reader_get_uint8 (br, &temp)) {
115 GST_WARNING_OBJECT (elt,
116 "Missing active format description, ignoring");
117 break;
118 }
119
120 GST_LOG_OBJECT (elt, "parsed active format description (AFD): %d",
121 temp);
122 user_data->afd_spec = GST_VIDEO_AFD_SPEC_ATSC_A53;
123 user_data->afd = temp;
124 user_data->active_format_flag = TRUE;
125 } else {
126 user_data->active_format_flag = FALSE;
127 }
128 user_data->has_afd = TRUE;
129 user_data->field = field;
130 break;
131 case USER_DATA_ID_DIRECTV_CC:
132 case A53_USER_DATA_ID_GA94:
133 if (!gst_byte_reader_get_uint8 (br, &user_data_type_code)) {
134 GST_WARNING_OBJECT (elt, "Missing user data type code, ignoring");
135 break;
136 }
137 if (provider_code == ITU_T_T35_MANUFACTURER_US_DIRECTV) {
138 if (!gst_byte_reader_get_uint8 (br, &directv_size)) {
139 GST_WARNING_OBJECT (elt, "Missing DirecTV size, ignoring");
140 break;
141 }
142 }
143 switch (user_data_type_code) {
144 case A53_USER_DATA_TYPE_CODE_CC_DATA:
145 /* 1 (cc count byte) +
146 * 1 (reserved byte, 0xff) +
147 * 1 (marker_bits, 0xff)
148 */
149 if (gst_byte_reader_get_remaining (br) < 3) {
150 GST_WARNING_OBJECT (elt,
151 "Closed caption data packet too short, ignoring");
152 break;
153 }
154 if (!gst_byte_reader_get_uint8 (br, &cc_count)) {
155 GST_WARNING_OBJECT (elt, "Missing closed caption count, ignoring");
156 break;
157 }
158
159 /* A53 part 4 closed captions */
160 a53_process_708_cc_data =
161 (cc_count & CEA_708_PROCESS_CC_DATA_FLAG) != 0;
162 if (!a53_process_708_cc_data) {
163 GST_WARNING_OBJECT (elt,
164 "ignoring closed captions as CEA_708_PROCESS_CC_DATA_FLAG is not set");
165 }
166
167 process_708_em_data = (cc_count & CEA_708_PROCESS_EM_DATA_FLAG) != 0;
168 if (!process_708_em_data) {
169 GST_WARNING_OBJECT (elt,
170 "CEA_708_PROCESS_EM_DATA_FLAG flag is not set");
171 }
172 if (!gst_byte_reader_get_uint8 (br, &temp)) {
173 GST_WARNING_OBJECT (elt, "Missing em bits, ignoring");
174 break;
175 }
176 if (temp != 0xff) {
177 GST_WARNING_OBJECT (elt, "em data does not equal 0xFF");
178 }
179 process_708_em_data = process_708_em_data && (temp == 0xff);
180 /* ignore process_708_em_data as there is content that doesn't follow spec for this field */
181
182 if (a53_process_708_cc_data) {
183 cc_count = cc_count & 0x1f;
184 cc_size = cc_count * 3;
185
186 if (cc_size == 0 || cc_size > gst_byte_reader_get_remaining (br)) {
187 GST_DEBUG_OBJECT (elt,
188 "ignoring closed captions, not enough data");
189 break;
190 }
191
192 /* Shouldn't really happen so let's not go out of our way to handle it */
193 if (user_data->closedcaptions_size > 0)
194 GST_WARNING_OBJECT (elt, "unused pending closed captions!");
195
196 g_assert (cc_size <= sizeof (user_data->closedcaptions));
197 if (!gst_byte_reader_get_data (br, cc_size, &data))
198 break;
199 memcpy (user_data->closedcaptions, data, cc_size);
200 user_data->closedcaptions_size = cc_size;
201 user_data->closedcaptions_type = GST_VIDEO_CAPTION_TYPE_CEA708_RAW;
202 user_data->field = field;
203 GST_DEBUG_OBJECT (elt, "CEA-708 closed captions, %u bytes",
204 cc_size);
205 }
206 break;
207 case A53_USER_DATA_TYPE_CODE_SCTE_21_EIA_608_CC_DATA:
208 GST_DEBUG_OBJECT (elt, "Unsupported SCTE 21 closed captions");
209 break;
210 case A53_USER_DATA_TYPE_CODE_BAR_DATA:
211 bar_size = gst_byte_reader_get_remaining (br);
212 if (bar_size == 0) {
213 GST_WARNING_OBJECT (elt, "Bar data packet too short, ignoring");
214 break;
215 }
216 if (bar_size > GST_VIDEO_BAR_MAX_BYTES) {
217 GST_WARNING_OBJECT (elt,
218 "Bar data packet of size %d is too long, ignoring", bar_size);
219 break;
220 }
221 if (!gst_byte_reader_get_data (br, bar_size, &data))
222 break;
223 memcpy (user_data->bar_data, data, bar_size);
224 user_data->bar_data_size = bar_size;
225 user_data->has_bar_data = TRUE;
226 user_data->field = field;
227 GST_DEBUG_OBJECT (elt, "Bar data, %u bytes", bar_size);
228 break;
229 default:
230 GST_DEBUG_OBJECT (elt,
231 "Unrecognized user data type code %d of size %d",
232 user_data_type_code, gst_byte_reader_get_remaining (br));
233 break;
234 }
235 break;
236 default:
237 GST_DEBUG_OBJECT (elt,
238 "Unrecognized user data id %d of size %d", user_data_id,
239 gst_byte_reader_get_remaining (br));
240 break;
241 }
242 }
243
244 /*
245 * gst_video_push_user_data:
246 * @elt: #GstElement that is pushing user data
247 * @user_data: #GstVideoParseUserData holding parsed closed caption, bar and AFD data
248 * @buf: #GstBuffer that receives the parsed data
249 *
250 * After user data has been parsed, add the data to @buf
251 */
252 void
gst_video_push_user_data(GstElement * elt,GstVideoParseUserData * user_data,GstBuffer * buf)253 gst_video_push_user_data (GstElement * elt, GstVideoParseUserData * user_data,
254 GstBuffer * buf)
255 {
256
257 /* 1. handle closed captions */
258 if (user_data->closedcaptions_size > 0) {
259 if (!gst_buffer_get_meta (buf, GST_VIDEO_CAPTION_META_API_TYPE)) {
260 gst_buffer_add_video_caption_meta (buf,
261 user_data->closedcaptions_type, user_data->closedcaptions,
262 user_data->closedcaptions_size);
263 } else {
264 GST_DEBUG_OBJECT (elt, "Closed caption data already found on buffer, "
265 "discarding to avoid duplication");
266 }
267
268 user_data->closedcaptions_type = GST_VIDEO_CAPTION_TYPE_UNKNOWN;
269 user_data->closedcaptions_size = 0;
270 }
271
272 /* 2. handle AFD */
273 if (user_data->has_afd) {
274 GstVideoAFD afd;
275 afd.field = 0;
276 afd.aspect_ratio = GST_VIDEO_AFD_ASPECT_RATIO_UNDEFINED;
277 afd.spec = GST_VIDEO_AFD_SPEC_ATSC_A53;
278 afd.afd = GST_VIDEO_AFD_UNAVAILABLE;
279 if (gst_video_parse_utils_parse_afd (user_data->afd, &afd, afd.spec)) {
280 gst_buffer_add_video_afd_meta (buf, afd.field, afd.spec, afd.afd);
281 } else {
282 GST_WARNING_OBJECT (elt, "Invalid AFD value %d", user_data->afd);
283 }
284 } else if (user_data->active_format_flag) {
285 /* AFD was present, but now it is no longer present */
286 GST_DEBUG_OBJECT (elt,
287 "AFD was present in previous frame, now no longer present");
288 user_data->active_format_flag = 0;
289 }
290 user_data->has_afd = FALSE;
291
292 /* 3. handle Bar data */
293 if (user_data->has_bar_data) {
294 GstVideoBarData data;
295 if (gst_video_parse_utils_parse_bar (user_data->bar_data,
296 user_data->bar_data_size, user_data->field, &data)) {
297 gst_buffer_add_video_bar_meta (buf, data.field, data.is_letterbox,
298 data.bar_data[0], data.bar_data[1]);
299 } else {
300 GST_WARNING_OBJECT (elt, "Invalid Bar data");
301 }
302 } else if (user_data->bar_data_size) {
303 /* bar data was present, but now it is no longer present */
304 GST_DEBUG_OBJECT (elt,
305 "Bar data was present in previous frame, now no longer present");
306 user_data->bar_data_size = 0;
307 }
308 user_data->has_bar_data = FALSE;
309 }
310
311
312 /*
313 * gst_video_parse_utils_parse_bar:
314 * @data: bar data array
315 * @size:size of bar data array
316 * @bar: #GstVideoBarData structure
317 *
318 * Parse bar data bytes into #GstVideoBarData structure
319 *
320 * See Table in https://www.atsc.org/wp-content/uploads/2015/03/a_53-Part-4-2009.pdf
321 *
322 * Returns: TRUE if parsing was successful, otherwise FALSE
323 */
324 static gboolean
gst_video_parse_utils_parse_bar(const guint8 * data,gsize size,guint field,GstVideoBarData * bar)325 gst_video_parse_utils_parse_bar (const guint8 * data, gsize size,
326 guint field, GstVideoBarData * bar)
327 {
328 guint8 temp;
329 int i = 0;
330 guint8 bar_flags[4];
331 guint16 bar_vals[4] = { 0, 0, 0, 0 };
332 GstBitReader bar_tender;
333
334 /* there must be at least one byte, and not more than GST_VIDEO_BAR_MAX_BYTES bytes */
335 if (!bar || size == 0 || size > GST_VIDEO_BAR_MAX_BYTES)
336 return FALSE;
337
338 gst_bit_reader_init (&bar_tender, data, size);
339
340
341 /* parse bar flags */
342 for (i = 0; i < 4; ++i) {
343 if (!gst_bit_reader_get_bits_uint8 (&bar_tender, bar_flags + i, 1))
344 return FALSE;
345 }
346
347 /* the next four bits must equal 1111 */
348 if (!gst_bit_reader_get_bits_uint8 (&bar_tender, &temp, 4) || temp != 0xF)
349 return FALSE;
350
351 /* parse bar values */
352 for (i = 0; i < 4; ++i) {
353 if (bar_flags[i]) {
354 /* the next two bits must equal 11 */
355 if (!gst_bit_reader_get_bits_uint8 (&bar_tender, &temp, 2) || temp != 0x3)
356 return FALSE;
357
358 if (!gst_bit_reader_get_bits_uint16 (&bar_tender, bar_vals + i, 14))
359 return FALSE;
360 }
361 }
362
363 /* bars are signaled in pairs: either top/bottom or left/right, but not both */
364 if ((bar_flags[0] != bar_flags[1]) || (bar_flags[2] != bar_flags[3]))
365 return FALSE;
366 if (bar_flags[0] && bar_flags[2])
367 return FALSE;
368
369 bar->is_letterbox = bar_flags[0];
370 if (bar->is_letterbox) {
371 bar->bar_data[0] = bar_vals[0];
372 bar->bar_data[1] = bar_vals[1];
373 } else {
374 bar->bar_data[0] = bar_vals[2];
375 bar->bar_data[1] = bar_vals[3];
376 }
377 bar->field = field;
378
379 return TRUE;
380 }
381
382 /*
383 * gst_video_parse_utils_parse_afd:
384 * @data: bar byte
385 * @afd: pointer to #GstVideoAFD struct
386 * @spec : #GstVideoAFDSpec indicating specification that applies to AFD byte
387 *
388 * Parse afd byte into #GstVideoAFD struct
389 *
390 * See:
391 *
392 * https://www.atsc.org/wp-content/uploads/2015/03/a_53-Part-4-2009.pdf
393 *
394 * https://en.wikipedia.org/wiki/Active_Format_Description#Complete_list_of_AFD_codes
395 *
396 * and
397 *
398 * SMPTE ST2016-1
399 *
400 * Returns: TRUE if parsing was successful, otherwise FALSE
401 */
402 static gboolean
gst_video_parse_utils_parse_afd(const guint8 data,GstVideoAFD * afd,GstVideoAFDSpec spec)403 gst_video_parse_utils_parse_afd (const guint8 data, GstVideoAFD * afd,
404 GstVideoAFDSpec spec)
405 {
406 guint8 afd_data;
407 g_return_val_if_fail (afd != NULL, FALSE);
408 g_return_val_if_fail ((guint8) spec <= 2, FALSE);
409 switch (spec) {
410 case GST_VIDEO_AFD_SPEC_DVB_ETSI:
411 case GST_VIDEO_AFD_SPEC_ATSC_A53:
412 if ((data & 0x40) == 0)
413 return FALSE;
414 afd_data = data & 0xF;
415 break;
416 case GST_VIDEO_AFD_SPEC_SMPTE_ST2016_1:
417 if ((data & 0x80) || (data & 0x3))
418 return FALSE;
419 afd_data = data >> 3;
420 afd->aspect_ratio = (GstVideoAFDAspectRatio) (((data >> 2) & 1) + 1);
421 break;
422 default:
423 return FALSE;
424 }
425
426 /* AFD is stored in a nybble */
427 g_return_val_if_fail (afd_data <= 0xF, FALSE);
428 /* reserved values for all specifications */
429 g_return_val_if_fail (afd_data != 1 && (afd_data < 5 || afd_data > 7)
430 && afd_data != 12, FALSE);
431 /* reserved for DVB/ETSI */
432 g_return_val_if_fail ((spec != GST_VIDEO_AFD_SPEC_DVB_ETSI)
433 || (afd_data != 0), FALSE);
434
435 afd->spec = spec;
436 afd->afd = (GstVideoAFDValue) afd_data;
437 return TRUE;
438 }
439