• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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_aac.h"
90 #include <string.h>
91 
92 GST_DEBUG_CATEGORY_EXTERN (mpegpsmux_debug);
93 #define GST_CAT_DEFAULT mpegpsmux_debug
94 
95 GstBuffer *
mpegpsmux_prepare_aac(GstBuffer * buf,MpegPsPadData * data,MpegPsMux * mux)96 mpegpsmux_prepare_aac (GstBuffer * buf, MpegPsPadData * data, MpegPsMux * mux)
97 {
98   GstBuffer *out_buf;
99   GstMemory *mem;
100   gsize out_size;
101   guint8 *adts_header, codec_data[2];
102   guint8 rate_idx = 0, channels = 0, obj_type = 0;
103 
104   GST_DEBUG_OBJECT (mux, "Preparing AAC buffer for output");
105 
106   adts_header = g_malloc0 (7);
107 
108   /* We want the same data and metadata, and then prepend some bytes */
109   out_buf = gst_buffer_copy (buf);
110   out_size = gst_buffer_get_size (buf) + 7;
111 
112   gst_buffer_extract (data->codec_data, 0, codec_data, 2);
113 
114   /* Generate ADTS header */
115   obj_type = (codec_data[0] & 0xC) >> 2;
116   obj_type++;
117   rate_idx = (codec_data[0] & 0x3) << 1;
118   rate_idx |= (codec_data[1] & 0x80) >> 7;
119   channels = (codec_data[1] & 0x78) >> 3;
120   GST_DEBUG_OBJECT (mux, "Rate index %u, channels %u, object type %u", rate_idx,
121       channels, obj_type);
122   /* Sync point over a full byte */
123   adts_header[0] = 0xFF;
124   /* Sync point continued over first 4 bits + static 4 bits
125    * (ID, layer, protection)*/
126   adts_header[1] = 0xF1;
127   /* Object type over first 2 bits */
128   adts_header[2] = obj_type << 6;
129   /* rate index over next 4 bits */
130   adts_header[2] |= (rate_idx << 2);
131   /* channels over last 2 bits */
132   adts_header[2] |= (channels & 0x4) >> 2;
133   /* channels continued over next 2 bits + 4 bits at zero */
134   adts_header[3] = (channels & 0x3) << 6;
135   /* frame size over last 2 bits */
136   adts_header[3] |= (gst_buffer_get_size (out_buf) & 0x1800) >> 11;
137   /* frame size continued over full byte */
138   adts_header[4] = (out_size & 0x1FF8) >> 3;
139   /* frame size continued first 3 bits */
140   adts_header[5] = (out_size & 0x7) << 5;
141   /* buffer fullness (0x7FF for VBR) over 5 last bits */
142   adts_header[5] |= 0x1F;
143   /* buffer fullness (0x7FF for VBR) continued over 6 first bits + 2 zeros for
144    * number of raw data blocks */
145   adts_header[6] = 0xFC;
146 
147   /* Prepend ADTS header */
148   mem = gst_memory_new_wrapped (0, adts_header, 7, 0, 7, adts_header, g_free);
149   gst_buffer_prepend_memory (out_buf, mem);
150 
151   return out_buf;
152 }
153