1 /*
2 * Copyright 2006, 2007, 2008 Fluendo S.A.
3 * Authors: Jan Schmidt <jan@fluendo.com>
4 * Kapil Agrawal <kapil@fluendo.com>
5 * Julien Moutte <julien@fluendo.com>
6 *
7 * This library is licensed under 3 different licenses and you
8 * can choose to use it under the terms of any one of them. The
9 * three licenses are the MPL 1.1, the LGPL and the MIT license.
10 *
11 * MPL:
12 *
13 * The contents of this file are subject to the Mozilla Public License
14 * Version 1.1 (the "License"); you may not use this file except in
15 * compliance with the License. You may obtain a copy of the License at
16 * http://www.mozilla.org/MPL/.
17 *
18 * Software distributed under the License is distributed on an "AS IS"
19 * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
20 * License for the specific language governing rights and limitations
21 * under the License.
22 *
23 * LGPL:
24 *
25 * This library is free software; you can redistribute it and/or
26 * modify it under the terms of the GNU Library General Public
27 * License as published by the Free Software Foundation; either
28 * version 2 of the License, or (at your option) any later version.
29 *
30 * This library is distributed in the hope that it will be useful,
31 * but WITHOUT ANY WARRANTY; without even the implied warranty of
32 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
33 * Library General Public License for more details.
34 *
35 * You should have received a copy of the GNU Library General Public
36 * License along with this library; if not, write to the
37 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
38 * Boston, MA 02110-1301, USA.
39 *
40 * MIT:
41 *
42 * Unless otherwise indicated, Source Code is licensed under MIT license.
43 * See further explanation attached in License Statement (distributed in the file
44 * LICENSE).
45 *
46 * Permission is hereby granted, free of charge, to any person obtaining a copy of
47 * this software and associated documentation files (the "Software"), to deal in
48 * the Software without restriction, including without limitation the rights to
49 * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
50 * of the Software, and to permit persons to whom the Software is furnished to do
51 * so, subject to the following conditions:
52 *
53 * The above copyright notice and this permission notice shall be included in all
54 * copies or substantial portions of the Software.
55 *
56 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
57 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
58 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
59 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
60 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
61 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
62 * SOFTWARE.
63 *
64 * SPDX-License-Identifier: MPL-1.1 OR MIT OR LGPL-2.0-or-later
65 */
66
67 #ifdef HAVE_CONFIG_H
68 #include "config.h"
69 #endif
70
71 #include "gstbasetsmuxttxt.h"
72 #include <string.h>
73
74 #define GST_CAT_DEFAULT gst_base_ts_mux_debug
75
76 /* from EN 300 472 spec: ITU-R System B Teletext in DVB
77 *
78 * PES packet = PES header + PES payload data
79 * where PES header must be fixed at 45 bytes (likely by using PES stuffing)
80 * PES packet must completely fill an integral number of TS packets
81 * using (184 bytes) payload data only (so no adaptation field stuffing)
82 */
83
84 GstBuffer *
gst_base_ts_mux_prepare_teletext(GstBuffer * buf,GstBaseTsMuxPad * pad,GstBaseTsMux * mux)85 gst_base_ts_mux_prepare_teletext (GstBuffer * buf, GstBaseTsMuxPad * pad,
86 GstBaseTsMux * mux)
87 {
88 GstBuffer *out_buf;
89 guint8 *data, *odata;
90 gint size, stuff;
91 gboolean add_id = FALSE;
92 GstMapInfo map, omap;
93
94 gst_buffer_map (buf, &map, GST_MAP_READ);
95 size = map.size;
96 data = map.data;
97
98 /* check if leading data_identifier byte is already present,
99 * if not increase size since it will need to be added */
100 if (data[0] < 0x10 || data[0] > 0x1F) {
101 size += 1;
102 add_id = TRUE;
103 }
104
105 if (size <= 184 - 45) {
106 stuff = 184 - 45 - size;
107 } else {
108 stuff = size - (184 - 45);
109 stuff = 184 - (stuff % 184);
110 }
111 if (G_UNLIKELY (stuff == 1))
112 stuff += 184;
113
114 GST_DEBUG_OBJECT (mux, "Preparing teletext buffer for output");
115
116 out_buf = gst_buffer_new_and_alloc (size + stuff);
117 gst_buffer_copy_into (out_buf, buf,
118 GST_BUFFER_COPY_METADATA | GST_BUFFER_COPY_TIMESTAMPS, 0, 0);
119
120 /* copy data */
121 gst_buffer_map (out_buf, &omap, GST_MAP_WRITE);
122 odata = omap.data;
123 /* add data_identifier if needed */
124 if (add_id) {
125 *odata = 0x10;
126 memcpy (odata + 1, data, size - 1);
127 } else {
128 memcpy (odata, data, size);
129 }
130
131 /* add stuffing data_unit */
132 odata += size;
133 *odata++ = 0xFF;
134 *odata = stuff;
135
136 gst_buffer_unmap (buf, &map);
137 gst_buffer_unmap (out_buf, &omap);
138
139 return out_buf;
140 }
141