Tizen 2.0 Release
[framework/multimedia/gst-plugins-bad0.10.git] / gst / mpegtsmux / mpegtsmux_h264.c
1 /* 
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>
6  *
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
10  * license.
11  *
12  * MPL:
13  * 
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/.
18  *
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
22  * under the License.
23  *
24  * LGPL:
25  *
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.
30  *
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.
35  *
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.
40  *
41  * GPL:
42  *
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.
47  *
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.
52  *
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.
56  *
57  * MIT:
58  *
59  * Unless otherwise indicated, Source Code is licensed under MIT license.
60  * See further explanation attached in License Statement (distributed in the file
61  * LICENSE).
62  * 
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:
69  * 
70  * The above copyright notice and this permission notice shall be included in all
71  * copies or substantial portions of the Software.
72  * 
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
79  * SOFTWARE.
80  *
81  */
82
83 #ifdef HAVE_CONFIG_H
84 #include "config.h"
85 #endif
86
87 #include "mpegtsmux_h264.h"
88 #include <string.h>
89
90 #define GST_CAT_DEFAULT mpegtsmux_debug
91
92 #define SPS_PPS_PERIOD GST_SECOND
93
94 typedef struct PrivDataH264 PrivDataH264;
95
96 struct PrivDataH264
97 {
98   GstBuffer *last_codec_data;
99   GstClockTime last_resync_ts;
100   GstBuffer *cached_es;
101   guint8 nal_length_size;
102 };
103
104 void
105 mpegtsmux_free_h264 (gpointer prepare_data)
106 {
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;
111   }
112   g_free (prepare_data);
113 }
114
115 static inline gboolean
116 mpegtsmux_process_codec_data_h264 (MpegTsPadData * data, MpegTsMux * mux)
117 {
118   PrivDataH264 *h264_data;
119   gboolean ret = FALSE;
120
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;
126   }
127
128   h264_data = (PrivDataH264 *) data->prepare_data;
129
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;
135     }
136     ret = TRUE;
137   }
138
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);
147
148     /* Get NAL length size */
149     h264_data->nal_length_size =
150         (GST_READ_UINT8 (GST_BUFFER_DATA (data->codec_data) + offset) & 0x03) +
151         1;
152     GST_LOG_OBJECT (mux, "NAL length will be coded on %u bytes",
153         h264_data->nal_length_size);
154     offset++;
155
156     /* How many SPS */
157     nb_sps =
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);
160     offset++;
161
162     /* For each SPS */
163     for (i = 0; i < nb_sps; i++) {
164       guint16 sps_size =
165           GST_READ_UINT16_BE (GST_BUFFER_DATA (data->codec_data) + offset);
166
167       GST_LOG_OBJECT (mux, "Sequence Parameter Set is %d bytes", sps_size);
168
169       /* Jump over SPS size */
170       offset += 2;
171
172       /* Fake a start code */
173       memcpy (GST_BUFFER_DATA (h264_data->cached_es) + out_offset,
174           startcode, 4);
175       out_offset += 4;
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);
179
180       out_offset += sps_size;
181       offset += sps_size;
182     }
183
184     /* How many PPS */
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);
187     offset++;
188
189     /* For each PPS */
190     for (i = 0; i < nb_pps; i++) {
191       gint pps_size =
192           GST_READ_UINT16_BE (GST_BUFFER_DATA (data->codec_data) + offset);
193
194       GST_LOG_OBJECT (mux, "Picture Parameter Set is %d bytes", pps_size);
195
196       /* Jump over PPS size */
197       offset += 2;
198
199       /* Fake a start code */
200       memcpy (GST_BUFFER_DATA (h264_data->cached_es) + out_offset,
201           startcode, 4);
202       out_offset += 4;
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);
206
207       out_offset += pps_size;
208       offset += pps_size;
209     }
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);
213   }
214   return ret;
215 }
216
217 GstBuffer *
218 mpegtsmux_prepare_h264 (GstBuffer * buf, MpegTsPadData * data, MpegTsMux * mux)
219 {
220   guint8 startcode[4] = { 0x00, 0x00, 0x00, 0x01 };
221   gsize out_offset = 0, in_offset = 0;
222   GstBuffer *out_buf;
223   gboolean changed;
224   PrivDataH264 *h264_data;
225   GstClockTimeDiff diff = GST_CLOCK_TIME_NONE;
226
227   GST_DEBUG_OBJECT (mux, "Preparing H264 buffer for output");
228
229   changed = mpegtsmux_process_codec_data_h264 (data, mux);
230   h264_data = (PrivDataH264 *) data->prepare_data;
231
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));
236   }
237
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");
246   } else {
247     out_buf = gst_buffer_new_and_alloc (GST_BUFFER_SIZE (buf) * 2);
248   }
249
250   /* We want the same metadata */
251   gst_buffer_copy_metadata (out_buf, buf, GST_BUFFER_COPY_ALL);
252
253   while (in_offset < GST_BUFFER_SIZE (buf) &&
254       out_offset < GST_BUFFER_SIZE (out_buf) - 4) {
255     guint32 nal_size = 0;
256
257     switch (h264_data->nal_length_size) {
258       case 1:
259         nal_size = GST_READ_UINT8 (GST_BUFFER_DATA (buf) + in_offset);
260         break;
261       case 2:
262         nal_size = GST_READ_UINT16_BE (GST_BUFFER_DATA (buf) + in_offset);
263         break;
264       case 4:
265         nal_size = GST_READ_UINT32_BE (GST_BUFFER_DATA (buf) + in_offset);
266         break;
267       default:
268         GST_WARNING_OBJECT (mux, "unsupported NAL length size %u",
269             h264_data->nal_length_size);
270     }
271     in_offset += h264_data->nal_length_size;
272
273     /* Generate an Elementary stream buffer by inserting a startcode */
274     memcpy (GST_BUFFER_DATA (out_buf) + out_offset, startcode, 4);
275     out_offset += 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;
281   }
282
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);
289   }
290   GST_BUFFER_SIZE (out_buf) = out_offset;
291
292   return out_buf;
293 }