1/*
2 * ADTS muxer.
3 * Copyright (c) 2006 Baptiste Coudurier <baptiste.coudurier@smartjog.com>
4 *                    Mans Rullgard <mans@mansr.com>
5 *
6 * This file is part of FFmpeg.
7 *
8 * FFmpeg is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; either
11 * version 2.1 of the License, or (at your option) any later version.
12 *
13 * FFmpeg is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16 * Lesser General Public License for more details.
17 *
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with FFmpeg; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21 */
22
23#include "libavcodec/get_bits.h"
24#include "libavcodec/put_bits.h"
25#include "libavcodec/avcodec.h"
26#include "libavcodec/mpeg4audio.h"
27#include "libavutil/opt.h"
28#include "avformat.h"
29#include "apetag.h"
30
31#define ADTS_HEADER_SIZE 7
32
33typedef struct {
34    AVClass *class;
35    int write_adts;
36    int objecttype;
37    int sample_rate_index;
38    int channel_conf;
39    int pce_size;
40    int apetag;
41    uint8_t pce_data[MAX_PCE_SIZE];
42} ADTSContext;
43
44#define ADTS_MAX_FRAME_BYTES ((1 << 13) - 1)
45
46static int adts_decode_extradata(AVFormatContext *s, ADTSContext *adts, uint8_t *buf, int size)
47{
48    GetBitContext gb;
49    PutBitContext pb;
50    MPEG4AudioConfig m4ac;
51    int off;
52
53    init_get_bits(&gb, buf, size * 8);
54    off = avpriv_mpeg4audio_get_config(&m4ac, buf, size * 8, 1);
55    if (off < 0)
56        return off;
57    skip_bits_long(&gb, off);
58    adts->objecttype        = m4ac.object_type - 1;
59    adts->sample_rate_index = m4ac.sampling_index;
60    adts->channel_conf      = m4ac.chan_config;
61
62    if (adts->objecttype > 3U) {
63        av_log(s, AV_LOG_ERROR, "MPEG-4 AOT %d is not allowed in ADTS\n", adts->objecttype+1);
64        return -1;
65    }
66    if (adts->sample_rate_index == 15) {
67        av_log(s, AV_LOG_ERROR, "Escape sample rate index illegal in ADTS\n");
68        return -1;
69    }
70    if (get_bits(&gb, 1)) {
71        av_log(s, AV_LOG_ERROR, "960/120 MDCT window is not allowed in ADTS\n");
72        return -1;
73    }
74    if (get_bits(&gb, 1)) {
75        av_log(s, AV_LOG_ERROR, "Scalable configurations are not allowed in ADTS\n");
76        return -1;
77    }
78    if (get_bits(&gb, 1)) {
79        av_log(s, AV_LOG_ERROR, "Extension flag is not allowed in ADTS\n");
80        return -1;
81    }
82    if (!adts->channel_conf) {
83        init_put_bits(&pb, adts->pce_data, MAX_PCE_SIZE);
84
85        put_bits(&pb, 3, 5); //ID_PCE
86        adts->pce_size = (avpriv_copy_pce_data(&pb, &gb) + 3) / 8;
87        flush_put_bits(&pb);
88    }
89
90    adts->write_adts = 1;
91
92    return 0;
93}
94
95static int adts_write_header(AVFormatContext *s)
96{
97    ADTSContext *adts = s->priv_data;
98    AVCodecContext *avc = s->streams[0]->codec;
99
100    if (avc->extradata_size > 0 &&
101            adts_decode_extradata(s, adts, avc->extradata, avc->extradata_size) < 0)
102        return -1;
103
104    return 0;
105}
106
107static int adts_write_frame_header(ADTSContext *ctx,
108                                   uint8_t *buf, int size, int pce_size)
109{
110    PutBitContext pb;
111
112    unsigned full_frame_size = (unsigned)ADTS_HEADER_SIZE + size + pce_size;
113    if (full_frame_size > ADTS_MAX_FRAME_BYTES) {
114        av_log(NULL, AV_LOG_ERROR, "ADTS frame size too large: %u (max %d)\n",
115               full_frame_size, ADTS_MAX_FRAME_BYTES);
116        return AVERROR_INVALIDDATA;
117    }
118
119    init_put_bits(&pb, buf, ADTS_HEADER_SIZE);
120
121    /* adts_fixed_header */
122    put_bits(&pb, 12, 0xfff);   /* syncword */
123    put_bits(&pb, 1, 0);        /* ID */
124    put_bits(&pb, 2, 0);        /* layer */
125    put_bits(&pb, 1, 1);        /* protection_absent */
126    put_bits(&pb, 2, ctx->objecttype); /* profile_objecttype */
127    put_bits(&pb, 4, ctx->sample_rate_index);
128    put_bits(&pb, 1, 0);        /* private_bit */
129    put_bits(&pb, 3, ctx->channel_conf); /* channel_configuration */
130    put_bits(&pb, 1, 0);        /* original_copy */
131    put_bits(&pb, 1, 0);        /* home */
132
133    /* adts_variable_header */
134    put_bits(&pb, 1, 0);        /* copyright_identification_bit */
135    put_bits(&pb, 1, 0);        /* copyright_identification_start */
136    put_bits(&pb, 13, full_frame_size); /* aac_frame_length */
137    put_bits(&pb, 11, 0x7ff);   /* adts_buffer_fullness */
138    put_bits(&pb, 2, 0);        /* number_of_raw_data_blocks_in_frame */
139
140    flush_put_bits(&pb);
141
142    return 0;
143}
144
145static int adts_write_packet(AVFormatContext *s, AVPacket *pkt)
146{
147    ADTSContext *adts = s->priv_data;
148    AVIOContext *pb = s->pb;
149    uint8_t buf[ADTS_HEADER_SIZE];
150
151    if (!pkt->size)
152        return 0;
153    if (adts->write_adts) {
154        int err = adts_write_frame_header(adts, buf, pkt->size,
155                                             adts->pce_size);
156        if (err < 0)
157            return err;
158        avio_write(pb, buf, ADTS_HEADER_SIZE);
159        if (adts->pce_size) {
160            avio_write(pb, adts->pce_data, adts->pce_size);
161            adts->pce_size = 0;
162        }
163    }
164    avio_write(pb, pkt->data, pkt->size);
165
166    return 0;
167}
168
169static int adts_write_trailer(AVFormatContext *s)
170{
171    ADTSContext *adts = s->priv_data;
172
173    if (adts->apetag)
174        ff_ape_write_tag(s);
175
176    return 0;
177}
178
179#define ENC AV_OPT_FLAG_ENCODING_PARAM
180#define OFFSET(obj) offsetof(ADTSContext, obj)
181static const AVOption options[] = {
182    { "write_apetag", "Enable APE tag writing", OFFSET(apetag), AV_OPT_TYPE_INT, {.i64 = 0}, 0, 1, ENC},
183    { NULL },
184};
185
186static const AVClass adts_muxer_class = {
187    .class_name     = "ADTS muxer",
188    .item_name      = av_default_item_name,
189    .option         = options,
190    .version        = LIBAVUTIL_VERSION_INT,
191};
192
193AVOutputFormat ff_adts_muxer = {
194    .name              = "adts",
195    .long_name         = NULL_IF_CONFIG_SMALL("ADTS AAC (Advanced Audio Coding)"),
196    .mime_type         = "audio/aac",
197    .extensions        = "aac,adts",
198    .priv_data_size    = sizeof(ADTSContext),
199    .audio_codec       = AV_CODEC_ID_AAC,
200    .video_codec       = AV_CODEC_ID_NONE,
201    .write_header      = adts_write_header,
202    .write_packet      = adts_write_packet,
203    .write_trailer     = adts_write_trailer,
204    .priv_class        = &adts_muxer_class,
205    .flags             = AVFMT_NOTIMESTAMPS,
206};
207