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 3 different licenses and you
10 * can choose to use it under the terms of any one of them. The
11 * three licenses are the MPL 1.1, the LGPL and the MIT license.
12 *
13 * MPL:
14 *
15 * The contents of this file are subject to the Mozilla Public License
16 * Version 1.1 (the "License"); you may not use this file except in
17 * compliance with the License. You may obtain a copy of the License at
18 * http://www.mozilla.org/MPL/.
19 *
20 * Software distributed under the License is distributed on an "AS IS"
21 * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
22 * License for the specific language governing rights and limitations
23 * under the License.
24 *
25 * LGPL:
26 *
27 * This library is free software; you can redistribute it and/or
28 * modify it under the terms of the GNU Library General Public
29 * License as published by the Free Software Foundation; either
30 * version 2 of the License, or (at your option) any later version.
31 *
32 * This library is distributed in the hope that it will be useful,
33 * but WITHOUT ANY WARRANTY; without even the implied warranty of
34 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
35 * Library General Public License for more details.
36 *
37 * You should have received a copy of the GNU Library General Public
38 * License along with this library; if not, write to the
39 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
40 * Boston, MA 02110-1301, USA.
41 *
42 * MIT:
43 *
44 * Unless otherwise indicated, Source Code is licensed under MIT license.
45 * See further explanation attached in License Statement (distributed in the file
46 * LICENSE).
47 *
48 * Permission is hereby granted, free of charge, to any person obtaining a copy of
49 * this software and associated documentation files (the "Software"), to deal in
50 * the Software without restriction, including without limitation the rights to
51 * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
52 * of the Software, and to permit persons to whom the Software is furnished to do
53 * so, subject to the following conditions:
54 *
55 * The above copyright notice and this permission notice shall be included in all
56 * copies or substantial portions of the Software.
57 *
58 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
59 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
60 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
61 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
62 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
63 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
64 * SOFTWARE.
65 *
66 * SPDX-License-Identifier: MPL-1.1 OR MIT OR LGPL-2.0-or-later
67 */
68
69 #ifdef HAVE_CONFIG_H
70 #include "config.h"
71 #endif
72
73 #include "mpegpsmux_aac.h"
74 #include <string.h>
75
76 GST_DEBUG_CATEGORY_EXTERN (mpegpsmux_debug);
77 #define GST_CAT_DEFAULT mpegpsmux_debug
78
79 GstBuffer *
mpegpsmux_prepare_aac(GstBuffer * buf,MpegPsPadData * data,MpegPsMux * mux)80 mpegpsmux_prepare_aac (GstBuffer * buf, MpegPsPadData * data, MpegPsMux * mux)
81 {
82 GstBuffer *out_buf;
83 GstMemory *mem;
84 gsize out_size;
85 guint8 *adts_header, codec_data[2];
86 guint8 rate_idx = 0, channels = 0, obj_type = 0;
87
88 GST_DEBUG_OBJECT (mux, "Preparing AAC buffer for output");
89
90 adts_header = g_malloc0 (7);
91
92 /* We want the same data and metadata, and then prepend some bytes */
93 out_buf = gst_buffer_copy (buf);
94 out_size = gst_buffer_get_size (buf) + 7;
95
96 gst_buffer_extract (data->codec_data, 0, codec_data, 2);
97
98 /* Generate ADTS header */
99 obj_type = (codec_data[0] & 0xC) >> 2;
100 obj_type++;
101 rate_idx = (codec_data[0] & 0x3) << 1;
102 rate_idx |= (codec_data[1] & 0x80) >> 7;
103 channels = (codec_data[1] & 0x78) >> 3;
104 GST_DEBUG_OBJECT (mux, "Rate index %u, channels %u, object type %u", rate_idx,
105 channels, obj_type);
106 /* Sync point over a full byte */
107 adts_header[0] = 0xFF;
108 /* Sync point continued over first 4 bits + static 4 bits
109 * (ID, layer, protection)*/
110 adts_header[1] = 0xF1;
111 /* Object type over first 2 bits */
112 adts_header[2] = obj_type << 6;
113 /* rate index over next 4 bits */
114 adts_header[2] |= (rate_idx << 2);
115 /* channels over last 2 bits */
116 adts_header[2] |= (channels & 0x4) >> 2;
117 /* channels continued over next 2 bits + 4 bits at zero */
118 adts_header[3] = (channels & 0x3) << 6;
119 /* frame size over last 2 bits */
120 adts_header[3] |= (gst_buffer_get_size (out_buf) & 0x1800) >> 11;
121 /* frame size continued over full byte */
122 adts_header[4] = (out_size & 0x1FF8) >> 3;
123 /* frame size continued first 3 bits */
124 adts_header[5] = (out_size & 0x7) << 5;
125 /* buffer fullness (0x7FF for VBR) over 5 last bits */
126 adts_header[5] |= 0x1F;
127 /* buffer fullness (0x7FF for VBR) continued over 6 first bits + 2 zeros for
128 * number of raw data blocks */
129 adts_header[6] = 0xFC;
130
131 /* Prepend ADTS header */
132 mem = gst_memory_new_wrapped (0, adts_header, 7, 0, 7, adts_header, g_free);
133 gst_buffer_prepend_memory (out_buf, mem);
134
135 return out_buf;
136 }
137