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