2 * Copyright 2006, 2007, 2008, 2009, 2010 Fluendo S.A.
3 * Authors: Jan Schmidt <jan@fluendo.com>
4 * Kapil Agrawal <kapil@fluendo.com>
5 * Julien Moutte <julien@fluendo.com>
7 * This library is licensed under 4 different licenses and you
8 * can choose to use it under the terms of any one of them. The
9 * four licenses are the MPL 1.1, the LGPL, the GPL and the MIT
14 * The contents of this file are subject to the Mozilla Public License
15 * Version 1.1 (the "License"); you may not use this file except in
16 * compliance with the License. You may obtain a copy of the License at
17 * http://www.mozilla.org/MPL/.
19 * Software distributed under the License is distributed on an "AS IS"
20 * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
21 * License for the specific language governing rights and limitations
26 * This library is free software; you can redistribute it and/or
27 * modify it under the terms of the GNU Library General Public
28 * License as published by the Free Software Foundation; either
29 * version 2 of the License, or (at your option) any later version.
31 * This library is distributed in the hope that it will be useful,
32 * but WITHOUT ANY WARRANTY; without even the implied warranty of
33 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
34 * Library General Public License for more details.
36 * You should have received a copy of the GNU Library General Public
37 * License along with this library; if not, write to the
38 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
39 * Boston, MA 02111-1307, USA.
43 * This program is free software; you can redistribute it and/or modify
44 * it under the terms of the GNU General Public License as published by
45 * the Free Software Foundation; either version 2 of the License, or
46 * (at your option) any later version.
48 * This program is distributed in the hope that it will be useful,
49 * but WITHOUT ANY WARRANTY; without even the implied warranty of
50 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
51 * GNU General Public License for more details.
53 * You should have received a copy of the GNU General Public License
54 * along with this program; if not, write to the Free Software
55 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
59 * Unless otherwise indicated, Source Code is licensed under MIT license.
60 * See further explanation attached in License Statement (distributed in the file
63 * Permission is hereby granted, free of charge, to any person obtaining a copy of
64 * this software and associated documentation files (the "Software"), to deal in
65 * the Software without restriction, including without limitation the rights to
66 * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
67 * of the Software, and to permit persons to whom the Software is furnished to do
68 * so, subject to the following conditions:
70 * The above copyright notice and this permission notice shall be included in all
71 * copies or substantial portions of the Software.
73 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
74 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
75 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
76 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
77 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
78 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
87 #include "mpegtsmux_h264.h"
90 #define GST_CAT_DEFAULT mpegtsmux_debug
92 #define SPS_PPS_PERIOD GST_SECOND
94 typedef struct PrivDataH264 PrivDataH264;
98 GstBuffer *last_codec_data;
99 GstClockTime last_resync_ts;
100 GstBuffer *cached_es;
101 guint8 nal_length_size;
105 mpegtsmux_free_h264 (gpointer prepare_data)
107 PrivDataH264 *h264_data = (PrivDataH264 *) prepare_data;
108 if (h264_data->cached_es) {
109 gst_buffer_unref (h264_data->cached_es);
110 h264_data->cached_es = NULL;
112 g_free (prepare_data);
115 static inline gboolean
116 mpegtsmux_process_codec_data_h264 (MpegTsPadData * data, MpegTsMux * mux)
118 PrivDataH264 *h264_data;
119 gboolean ret = FALSE;
121 /* Initialize our private data structure for caching */
122 if (G_UNLIKELY (!data->prepare_data)) {
123 data->prepare_data = g_new0 (PrivDataH264, 1);
124 h264_data = (PrivDataH264 *) data->prepare_data;
125 h264_data->last_resync_ts = GST_CLOCK_TIME_NONE;
128 h264_data = (PrivDataH264 *) data->prepare_data;
130 /* Detect a codec data change */
131 if (h264_data->last_codec_data != data->codec_data) {
132 if (h264_data->cached_es) {
133 gst_buffer_unref (h264_data->cached_es);
134 h264_data->cached_es = NULL;
139 /* Generate the SPS/PPS ES header that will be prepended regularly */
140 if (G_UNLIKELY (!h264_data->cached_es)) {
141 gint offset = 4, i = 0, nb_sps = 0, nb_pps = 0;
142 gsize out_offset = 0;
143 guint8 startcode[4] = { 0x00, 0x00, 0x00, 0x01 };
144 h264_data->last_codec_data = data->codec_data;
145 h264_data->cached_es =
146 gst_buffer_new_and_alloc (GST_BUFFER_SIZE (data->codec_data) * 10);
148 /* Get NAL length size */
149 h264_data->nal_length_size =
150 (GST_READ_UINT8 (GST_BUFFER_DATA (data->codec_data) + offset) & 0x03) +
152 GST_LOG_OBJECT (mux, "NAL length will be coded on %u bytes",
153 h264_data->nal_length_size);
158 GST_READ_UINT8 (GST_BUFFER_DATA (data->codec_data) + offset) & 0x1f;
159 GST_DEBUG_OBJECT (mux, "we have %d Sequence Parameter Set", nb_sps);
163 for (i = 0; i < nb_sps; i++) {
165 GST_READ_UINT16_BE (GST_BUFFER_DATA (data->codec_data) + offset);
167 GST_LOG_OBJECT (mux, "Sequence Parameter Set is %d bytes", sps_size);
169 /* Jump over SPS size */
172 /* Fake a start code */
173 memcpy (GST_BUFFER_DATA (h264_data->cached_es) + out_offset,
176 /* Now push the SPS */
177 memcpy (GST_BUFFER_DATA (h264_data->cached_es) + out_offset,
178 GST_BUFFER_DATA (data->codec_data) + offset, sps_size);
180 out_offset += sps_size;
185 nb_pps = GST_READ_UINT8 (GST_BUFFER_DATA (data->codec_data) + offset);
186 GST_LOG_OBJECT (mux, "we have %d Picture Parameter Set", nb_sps);
190 for (i = 0; i < nb_pps; i++) {
192 GST_READ_UINT16_BE (GST_BUFFER_DATA (data->codec_data) + offset);
194 GST_LOG_OBJECT (mux, "Picture Parameter Set is %d bytes", pps_size);
196 /* Jump over PPS size */
199 /* Fake a start code */
200 memcpy (GST_BUFFER_DATA (h264_data->cached_es) + out_offset,
203 /* Now push the PPS */
204 memcpy (GST_BUFFER_DATA (h264_data->cached_es) + out_offset,
205 GST_BUFFER_DATA (data->codec_data) + offset, pps_size);
207 out_offset += pps_size;
210 GST_BUFFER_SIZE (h264_data->cached_es) = out_offset;
211 GST_DEBUG_OBJECT (mux, "generated a %" G_GSIZE_FORMAT
212 " bytes SPS/PPS header", out_offset);
218 mpegtsmux_prepare_h264 (GstBuffer * buf, MpegTsPadData * data, MpegTsMux * mux)
220 guint8 startcode[4] = { 0x00, 0x00, 0x00, 0x01 };
221 gsize out_offset = 0, in_offset = 0;
224 PrivDataH264 *h264_data;
225 GstClockTimeDiff diff = GST_CLOCK_TIME_NONE;
227 GST_DEBUG_OBJECT (mux, "Preparing H264 buffer for output");
229 changed = mpegtsmux_process_codec_data_h264 (data, mux);
230 h264_data = (PrivDataH264 *) data->prepare_data;
232 if (GST_CLOCK_TIME_IS_VALID (h264_data->last_resync_ts) &&
233 GST_CLOCK_TIME_IS_VALID (GST_BUFFER_TIMESTAMP (buf))) {
234 diff = GST_CLOCK_DIFF (h264_data->last_resync_ts,
235 GST_BUFFER_TIMESTAMP (buf));
238 if (changed || (GST_CLOCK_TIME_IS_VALID (diff) && diff > SPS_PPS_PERIOD)) {
239 out_buf = gst_buffer_new_and_alloc (GST_BUFFER_SIZE (buf) * 2 +
240 GST_BUFFER_SIZE (h264_data->cached_es));
241 h264_data->last_resync_ts = GST_BUFFER_TIMESTAMP (buf);
242 memcpy (GST_BUFFER_DATA (out_buf), GST_BUFFER_DATA (h264_data->cached_es),
243 GST_BUFFER_SIZE (h264_data->cached_es));
244 out_offset = GST_BUFFER_SIZE (h264_data->cached_es);
245 GST_DEBUG_OBJECT (mux, "prepending SPS/PPS information to that packet");
247 out_buf = gst_buffer_new_and_alloc (GST_BUFFER_SIZE (buf) * 2);
250 /* We want the same metadata */
251 gst_buffer_copy_metadata (out_buf, buf, GST_BUFFER_COPY_ALL);
253 while (in_offset < GST_BUFFER_SIZE (buf) &&
254 out_offset < GST_BUFFER_SIZE (out_buf) - 4) {
255 guint32 nal_size = 0;
257 switch (h264_data->nal_length_size) {
259 nal_size = GST_READ_UINT8 (GST_BUFFER_DATA (buf) + in_offset);
262 nal_size = GST_READ_UINT16_BE (GST_BUFFER_DATA (buf) + in_offset);
265 nal_size = GST_READ_UINT32_BE (GST_BUFFER_DATA (buf) + in_offset);
268 GST_WARNING_OBJECT (mux, "unsupported NAL length size %u",
269 h264_data->nal_length_size);
271 in_offset += h264_data->nal_length_size;
273 /* Generate an Elementary stream buffer by inserting a startcode */
274 memcpy (GST_BUFFER_DATA (out_buf) + out_offset, startcode, 4);
276 memcpy (GST_BUFFER_DATA (out_buf) + out_offset,
277 GST_BUFFER_DATA (buf) + in_offset,
278 MIN (nal_size, GST_BUFFER_SIZE (out_buf) - out_offset));
279 in_offset += nal_size;
280 out_offset += nal_size;
283 if (out_offset > GST_BUFFER_SIZE (out_buf)) {
284 GST_WARNING_OBJECT (mux, "Calculated buffer size %" G_GSIZE_FORMAT
285 " is greater than max expected size %u, "
286 "using max expected size (Input might not be in "
287 "avc format", out_offset, GST_BUFFER_SIZE (out_buf));
288 out_offset = GST_BUFFER_SIZE (out_buf);
290 GST_BUFFER_SIZE (out_buf) = out_offset;