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  * 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., 51 Franklin St, Fifth Floor,
39  * Boston, MA 02110-1301, 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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, 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_aac.h"
88 #include <string.h>
89 
90 #define GST_CAT_DEFAULT mpegtsmux_debug
91 
92 GstBuffer *
mpegtsmux_prepare_aac(GstBuffer * buf,MpegTsPadData * data,MpegTsMux * mux)93 mpegtsmux_prepare_aac (GstBuffer * buf, MpegTsPadData * data, MpegTsMux * mux)
94 {
95   guint8 adts_header[7] = { 0, };
96   gsize out_size = gst_buffer_get_size (buf) + 7;
97   GstBuffer *out_buf = gst_buffer_new_and_alloc (out_size);
98   gsize out_offset = 0;
99   guint8 rate_idx = 0, channels = 0, obj_type = 0;
100   GstMapInfo codec_data_map;
101   GstMapInfo buf_map;
102 
103   GST_DEBUG_OBJECT (mux, "Preparing AAC buffer for output");
104 
105   gst_buffer_copy_into (out_buf, buf,
106       GST_BUFFER_COPY_METADATA | GST_BUFFER_COPY_TIMESTAMPS, 0, 0);
107 
108   gst_buffer_map (data->codec_data, &codec_data_map, GST_MAP_READ);
109 
110   /* Generate ADTS header */
111   obj_type = GST_READ_UINT8 (codec_data_map.data) >> 3;
112   rate_idx = (GST_READ_UINT8 (codec_data_map.data) & 0x7) << 1;
113   rate_idx |= (GST_READ_UINT8 (codec_data_map.data + 1) & 0x80) >> 7;
114   channels = (GST_READ_UINT8 (codec_data_map.data + 1) & 0x78) >> 3;
115   GST_DEBUG_OBJECT (mux, "Rate index %u, channels %u, object type %u", rate_idx,
116       channels, obj_type);
117   /* Sync point over a full byte */
118   adts_header[0] = 0xFF;
119   /* Sync point continued over first 4 bits + static 4 bits
120    * (ID, layer, protection)*/
121   adts_header[1] = 0xF1;
122   /* Object type over first 2 bits */
123   adts_header[2] = (obj_type - 1) << 6;
124   /* rate index over next 4 bits */
125   adts_header[2] |= (rate_idx << 2);
126   /* channels over last 2 bits */
127   adts_header[2] |= (channels & 0x4) >> 2;
128   /* channels continued over next 2 bits + 4 bits at zero */
129   adts_header[3] = (channels & 0x3) << 6;
130   /* frame size over last 2 bits */
131   adts_header[3] |= (out_size & 0x1800) >> 11;
132   /* frame size continued over full byte */
133   adts_header[4] = (out_size & 0x1FF8) >> 3;
134   /* frame size continued first 3 bits */
135   adts_header[5] = (out_size & 0x7) << 5;
136   /* buffer fullness (0x7FF for VBR) over 5 last bits */
137   adts_header[5] |= 0x1F;
138   /* buffer fullness (0x7FF for VBR) continued over 6 first bits + 2 zeros for
139    * number of raw data blocks */
140   adts_header[6] = 0xFC;
141 
142   /* Insert ADTS header */
143   gst_buffer_fill (out_buf, out_offset, adts_header, 7);
144   out_offset += 7;
145 
146   gst_buffer_map (buf, &buf_map, GST_MAP_READ);
147 
148   /* Now copy complete frame */
149   gst_buffer_fill (out_buf, out_offset, buf_map.data, buf_map.size);
150 
151   gst_buffer_unmap (data->codec_data, &codec_data_map);
152   gst_buffer_unmap (buf, &buf_map);
153 
154   return out_buf;
155 }
156