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 * Copyright 2008 Lin YANG <oxcsnicho@gmail.com>
8 *
9 * This library is licensed under 3 different licenses and you
10 * can choose to use it under the terms of any one of them. The
11 * three licenses are the MPL 1.1, the LGPL and the MIT license.
12 *
13 * MPL:
14 *
15 * The contents of this file are subject to the Mozilla Public License
16 * Version 1.1 (the "License"); you may not use this file except in
17 * compliance with the License. You may obtain a copy of the License at
18 * http://www.mozilla.org/MPL/.
19 *
20 * Software distributed under the License is distributed on an "AS IS"
21 * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
22 * License for the specific language governing rights and limitations
23 * under the License.
24 *
25 * LGPL:
26 *
27 * This library is free software; you can redistribute it and/or
28 * modify it under the terms of the GNU Library General Public
29 * License as published by the Free Software Foundation; either
30 * version 2 of the License, or (at your option) any later version.
31 *
32 * This library is distributed in the hope that it will be useful,
33 * but WITHOUT ANY WARRANTY; without even the implied warranty of
34 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
35 * Library General Public License for more details.
36 *
37 * You should have received a copy of the GNU Library General Public
38 * License along with this library; if not, write to the
39 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
40 * Boston, MA 02110-1301, USA.
41 *
42 * MIT:
43 *
44 * Unless otherwise indicated, Source Code is licensed under MIT license.
45 * See further explanation attached in License Statement (distributed in the file
46 * LICENSE).
47 *
48 * Permission is hereby granted, free of charge, to any person obtaining a copy of
49 * this software and associated documentation files (the "Software"), to deal in
50 * the Software without restriction, including without limitation the rights to
51 * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
52 * of the Software, and to permit persons to whom the Software is furnished to do
53 * so, subject to the following conditions:
54 *
55 * The above copyright notice and this permission notice shall be included in all
56 * copies or substantial portions of the Software.
57 *
58 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
59 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
60 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
61 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
62 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
63 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
64 * SOFTWARE.
65 *
66 * SPDX-License-Identifier: MPL-1.1 OR MIT OR LGPL-2.0-or-later
67 */
68
69 #ifdef HAVE_CONFIG_H
70 #include "config.h"
71 #endif
72
73 #include "mpegpsmux_h264.h"
74 #include <gst/base/gstbytewriter.h>
75 #include <string.h>
76
77 GST_DEBUG_CATEGORY_EXTERN (mpegpsmux_debug);
78 #define GST_CAT_DEFAULT mpegpsmux_debug
79
80 GstBuffer *
mpegpsmux_prepare_h264(GstBuffer * buf,MpegPsPadData * data,MpegPsMux * mux)81 mpegpsmux_prepare_h264 (GstBuffer * buf, MpegPsPadData * data, MpegPsMux * mux)
82 {
83 GstByteWriter bw;
84 GstMapInfo codec_data, map;
85 guint8 nal_length_size = 0;
86 GstBuffer *out_buf;
87 guint8 nb_sps = 0, nb_pps = 0;
88 gint offset = 4, i = 0;
89 gsize in_offset;
90
91 GST_DEBUG_OBJECT (mux, "Preparing H264 buffer for output");
92
93 /* FIXME: are we prepending SPS/PPS in front of every single buffer?
94 * (should only be in front of keyframes really, if at all) */
95 /* FIXME: create a byte-stream version of SPS/PPS once in set_caps */
96 if (!gst_buffer_map (data->codec_data, &codec_data, GST_MAP_READ))
97 return NULL;
98
99 gst_byte_writer_init_with_size (&bw, gst_buffer_get_size (buf) * 2, FALSE);
100
101 /* Get NAL length size */
102 nal_length_size = (codec_data.data[offset] & 0x03) + 1;
103 GST_LOG_OBJECT (mux, "NAL length will be coded on %u bytes", nal_length_size);
104 offset++;
105
106 /* Generate SPS */
107 nb_sps = codec_data.data[offset] & 0x1f;
108 GST_DEBUG_OBJECT (mux, "we have %d Sequence Parameter Set", nb_sps);
109 offset++;
110
111 /* For each SPS */
112 for (i = 0; i < nb_sps; i++) {
113 guint16 sps_size = GST_READ_UINT16_BE (codec_data.data + offset);
114
115 GST_LOG_OBJECT (mux, "Sequence Parameter Set is %d bytes", sps_size);
116
117 /* Jump over SPS size */
118 offset += 2;
119
120 /* Fake a start code */
121 gst_byte_writer_put_uint32_be (&bw, 0x00000001);
122
123 /* Now push the SPS */
124 gst_byte_writer_put_data (&bw, codec_data.data + offset, sps_size);
125
126 offset += sps_size;
127 }
128
129 nb_pps = codec_data.data[offset];
130 GST_LOG_OBJECT (mux, "we have %d Picture Parameter Set", nb_sps);
131 offset++;
132
133 /* For each PPS */
134 for (i = 0; i < nb_pps; i++) {
135 gint pps_size = GST_READ_UINT16_BE (codec_data.data + offset);
136
137 GST_LOG_OBJECT (mux, "Picture Parameter Set is %d bytes", pps_size);
138
139 /* Jump over PPS size */
140 offset += 2;
141
142 /* Fake a start code */
143 gst_byte_writer_put_uint32_be (&bw, 0x00000001);
144
145 /* Now push the PPS */
146 gst_byte_writer_put_data (&bw, codec_data.data + offset, pps_size);
147
148 offset += pps_size;
149 }
150
151 gst_buffer_unmap (data->codec_data, &codec_data);
152
153 if (!gst_buffer_map (buf, &map, GST_MAP_READ))
154 return NULL;
155
156 /* now process NALs and change them to byte-stream format */
157 in_offset = 0;
158 while (in_offset < map.size) {
159 guint32 nal_size = 0;
160
161 switch (nal_length_size) {
162 case 1:
163 nal_size = GST_READ_UINT8 (map.data + in_offset);
164 break;
165 case 2:
166 nal_size = GST_READ_UINT16_BE (map.data + in_offset);
167 break;
168 case 4:
169 nal_size = GST_READ_UINT32_BE (map.data + in_offset);
170 break;
171 default:
172 GST_WARNING_OBJECT (mux, "unsupported NAL length size %u",
173 nal_length_size);
174 break;
175 }
176 in_offset += nal_length_size;
177
178 /* Generate an Elementary stream buffer by inserting a startcode */
179 gst_byte_writer_put_uint32_be (&bw, 0x00000001);
180 gst_byte_writer_put_data (&bw, map.data + in_offset,
181 MIN (nal_size, map.size - in_offset));
182 in_offset += nal_size;
183 }
184 gst_buffer_unmap (buf, &map);
185
186 out_buf = gst_byte_writer_reset_and_get_buffer (&bw);
187
188 /* We want the same metadata */
189 gst_buffer_copy_into (out_buf, buf, GST_BUFFER_COPY_METADATA, 0, 0);
190
191 return out_buf;
192 }
193