2009-06-03 18:50:46 +00:00
|
|
|
/*
|
|
|
|
* MPEG-2/4 AAC ADTS to MPEG-4 Audio Specific Configuration bitstream filter
|
|
|
|
* Copyright (c) 2009 Alex Converse <alex.converse@gmail.com>
|
|
|
|
*
|
|
|
|
* This file is part of FFmpeg.
|
|
|
|
*
|
|
|
|
* FFmpeg is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU Lesser General Public
|
|
|
|
* License as published by the Free Software Foundation; either
|
|
|
|
* version 2.1 of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* FFmpeg is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
* Lesser General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Lesser General Public
|
|
|
|
* License along with FFmpeg; if not, write to the Free Software
|
|
|
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
|
|
|
*/
|
|
|
|
|
2017-04-26 16:57:54 +00:00
|
|
|
#include "adts_header.h"
|
|
|
|
#include "adts_parser.h"
|
2020-04-07 14:41:48 +00:00
|
|
|
#include "bsf.h"
|
2020-04-07 14:49:28 +00:00
|
|
|
#include "bsf_internal.h"
|
2009-06-03 18:50:46 +00:00
|
|
|
#include "put_bits.h"
|
|
|
|
#include "get_bits.h"
|
|
|
|
#include "mpeg4audio.h"
|
|
|
|
|
|
|
|
typedef struct AACBSFContext {
|
|
|
|
int first_frame_done;
|
|
|
|
} AACBSFContext;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* This filter creates an MPEG-4 AudioSpecificConfig from an MPEG-2/4
|
|
|
|
* ADTS header and removes the ADTS header.
|
|
|
|
*/
|
2018-03-16 23:06:40 +00:00
|
|
|
static int aac_adtstoasc_filter(AVBSFContext *bsfc, AVPacket *pkt)
|
2009-06-03 18:50:46 +00:00
|
|
|
{
|
2013-11-23 10:43:13 +00:00
|
|
|
AACBSFContext *ctx = bsfc->priv_data;
|
|
|
|
|
2009-06-03 18:50:46 +00:00
|
|
|
GetBitContext gb;
|
|
|
|
PutBitContext pb;
|
|
|
|
AACADTSHeaderInfo hdr;
|
2013-11-23 10:43:13 +00:00
|
|
|
int ret;
|
2009-06-03 18:50:46 +00:00
|
|
|
|
2018-03-16 23:06:40 +00:00
|
|
|
ret = ff_bsf_get_packet_ref(bsfc, pkt);
|
2013-11-23 10:43:13 +00:00
|
|
|
if (ret < 0)
|
|
|
|
return ret;
|
2009-06-03 18:50:46 +00:00
|
|
|
|
2018-03-16 23:06:40 +00:00
|
|
|
if (bsfc->par_in->extradata && pkt->size >= 2 && (AV_RB16(pkt->data) >> 4) != 0xfff)
|
|
|
|
return 0;
|
2017-04-11 04:03:51 +00:00
|
|
|
|
2018-03-16 23:06:40 +00:00
|
|
|
if (pkt->size < AV_AAC_ADTS_HEADER_SIZE)
|
2013-11-23 10:43:13 +00:00
|
|
|
goto packet_too_small;
|
2009-06-03 18:50:46 +00:00
|
|
|
|
2018-03-16 23:06:40 +00:00
|
|
|
init_get_bits(&gb, pkt->data, AV_AAC_ADTS_HEADER_SIZE * 8);
|
2009-06-03 18:50:46 +00:00
|
|
|
|
2017-04-26 16:57:54 +00:00
|
|
|
if (ff_adts_header_parse(&gb, &hdr) < 0) {
|
2013-11-23 10:43:13 +00:00
|
|
|
av_log(bsfc, AV_LOG_ERROR, "Error parsing ADTS frame header!\n");
|
|
|
|
ret = AVERROR_INVALIDDATA;
|
|
|
|
goto fail;
|
2009-06-03 18:50:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!hdr.crc_absent && hdr.num_aac_frames > 1) {
|
2013-11-23 10:43:13 +00:00
|
|
|
avpriv_report_missing_feature(bsfc,
|
2013-02-25 22:53:52 +00:00
|
|
|
"Multiple RDBs per frame with CRC");
|
2013-11-23 10:43:13 +00:00
|
|
|
ret = AVERROR_PATCHWELCOME;
|
|
|
|
goto fail;
|
2009-06-03 18:50:46 +00:00
|
|
|
}
|
|
|
|
|
2018-03-16 23:06:40 +00:00
|
|
|
pkt->size -= AV_AAC_ADTS_HEADER_SIZE + 2 * !hdr.crc_absent;
|
|
|
|
if (pkt->size <= 0)
|
2013-11-23 10:43:13 +00:00
|
|
|
goto packet_too_small;
|
2018-03-16 23:06:40 +00:00
|
|
|
pkt->data += AV_AAC_ADTS_HEADER_SIZE + 2 * !hdr.crc_absent;
|
2009-06-03 18:50:46 +00:00
|
|
|
|
|
|
|
if (!ctx->first_frame_done) {
|
|
|
|
int pce_size = 0;
|
|
|
|
uint8_t pce_data[MAX_PCE_SIZE];
|
2013-11-23 10:43:13 +00:00
|
|
|
uint8_t *extradata;
|
|
|
|
|
2009-06-03 18:50:46 +00:00
|
|
|
if (!hdr.chan_config) {
|
2018-03-16 23:06:40 +00:00
|
|
|
init_get_bits(&gb, pkt->data, pkt->size * 8);
|
2009-06-03 18:50:46 +00:00
|
|
|
if (get_bits(&gb, 3) != 5) {
|
2013-11-23 10:43:13 +00:00
|
|
|
avpriv_report_missing_feature(bsfc,
|
2013-02-25 22:53:52 +00:00
|
|
|
"PCE-based channel configuration "
|
|
|
|
"without PCE as first syntax "
|
|
|
|
"element");
|
2013-11-23 10:43:13 +00:00
|
|
|
ret = AVERROR_PATCHWELCOME;
|
|
|
|
goto fail;
|
2009-06-03 18:50:46 +00:00
|
|
|
}
|
|
|
|
init_put_bits(&pb, pce_data, MAX_PCE_SIZE);
|
2017-03-16 16:34:12 +00:00
|
|
|
pce_size = ff_copy_pce_data(&pb, &gb) / 8;
|
2009-06-03 18:50:46 +00:00
|
|
|
flush_put_bits(&pb);
|
2018-03-16 23:06:40 +00:00
|
|
|
pkt->size -= get_bits_count(&gb)/8;
|
|
|
|
pkt->data += get_bits_count(&gb)/8;
|
2009-06-03 18:50:46 +00:00
|
|
|
}
|
|
|
|
|
2018-03-16 23:06:40 +00:00
|
|
|
extradata = av_packet_new_side_data(pkt, AV_PKT_DATA_NEW_EXTRADATA,
|
2017-04-10 16:36:19 +00:00
|
|
|
2 + pce_size);
|
2013-11-23 10:43:13 +00:00
|
|
|
if (!extradata) {
|
|
|
|
ret = AVERROR(ENOMEM);
|
|
|
|
goto fail;
|
2015-02-17 21:58:31 +00:00
|
|
|
}
|
2009-06-03 18:50:46 +00:00
|
|
|
|
2013-11-23 10:43:13 +00:00
|
|
|
init_put_bits(&pb, extradata, 2 + pce_size);
|
2009-06-03 18:50:46 +00:00
|
|
|
put_bits(&pb, 5, hdr.object_type);
|
|
|
|
put_bits(&pb, 4, hdr.sampling_index);
|
|
|
|
put_bits(&pb, 4, hdr.chan_config);
|
|
|
|
put_bits(&pb, 1, 0); //frame length - 1024 samples
|
|
|
|
put_bits(&pb, 1, 0); //does not depend on core coder
|
|
|
|
put_bits(&pb, 1, 0); //is not extension
|
|
|
|
flush_put_bits(&pb);
|
|
|
|
if (pce_size) {
|
2013-11-23 10:43:13 +00:00
|
|
|
memcpy(extradata + 2, pce_data, pce_size);
|
2009-06-03 18:50:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
ctx->first_frame_done = 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
2013-11-23 10:43:13 +00:00
|
|
|
|
|
|
|
packet_too_small:
|
|
|
|
av_log(bsfc, AV_LOG_ERROR, "Input packet too small\n");
|
|
|
|
ret = AVERROR_INVALIDDATA;
|
|
|
|
fail:
|
2018-03-16 23:06:40 +00:00
|
|
|
av_packet_unref(pkt);
|
2013-11-23 10:43:13 +00:00
|
|
|
return ret;
|
2009-06-03 18:50:46 +00:00
|
|
|
}
|
|
|
|
|
2013-11-23 10:43:13 +00:00
|
|
|
static int aac_adtstoasc_init(AVBSFContext *ctx)
|
|
|
|
{
|
2016-11-25 00:10:47 +00:00
|
|
|
/* Validate the extradata if the stream is already MPEG-4 AudioSpecificConfig */
|
|
|
|
if (ctx->par_in->extradata) {
|
|
|
|
MPEG4AudioConfig mp4ac;
|
2019-09-21 23:04:33 +00:00
|
|
|
int ret = avpriv_mpeg4audio_get_config2(&mp4ac, ctx->par_in->extradata,
|
|
|
|
ctx->par_in->extradata_size, 1, ctx);
|
2016-11-25 00:10:47 +00:00
|
|
|
if (ret < 0) {
|
|
|
|
av_log(ctx, AV_LOG_ERROR, "Error parsing AudioSpecificConfig extradata!\n");
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
}
|
2009-06-03 18:50:46 +00:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2013-11-23 10:43:13 +00:00
|
|
|
static const enum AVCodecID codec_ids[] = {
|
|
|
|
AV_CODEC_ID_AAC, AV_CODEC_ID_NONE,
|
|
|
|
};
|
|
|
|
|
|
|
|
const AVBitStreamFilter ff_aac_adtstoasc_bsf = {
|
2013-10-02 17:20:29 +00:00
|
|
|
.name = "aac_adtstoasc",
|
|
|
|
.priv_data_size = sizeof(AACBSFContext),
|
2013-11-23 10:43:13 +00:00
|
|
|
.init = aac_adtstoasc_init,
|
2013-10-02 17:20:29 +00:00
|
|
|
.filter = aac_adtstoasc_filter,
|
2013-11-23 10:43:13 +00:00
|
|
|
.codec_ids = codec_ids,
|
2009-06-03 18:50:46 +00:00
|
|
|
};
|