yading@10: /* yading@10: * MPEG-2/4 AAC ADTS to MPEG-4 Audio Specific Configuration bitstream filter yading@10: * Copyright (c) 2009 Alex Converse yading@10: * yading@10: * This file is part of FFmpeg. yading@10: * yading@10: * FFmpeg is free software; you can redistribute it and/or yading@10: * modify it under the terms of the GNU Lesser General Public yading@10: * License as published by the Free Software Foundation; either yading@10: * version 2.1 of the License, or (at your option) any later version. yading@10: * yading@10: * FFmpeg is distributed in the hope that it will be useful, yading@10: * but WITHOUT ANY WARRANTY; without even the implied warranty of yading@10: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU yading@10: * Lesser General Public License for more details. yading@10: * yading@10: * You should have received a copy of the GNU Lesser General Public yading@10: * License along with FFmpeg; if not, write to the Free Software yading@10: * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA yading@10: */ yading@10: yading@10: #include "avcodec.h" yading@10: #include "aacadtsdec.h" yading@10: #include "put_bits.h" yading@10: #include "get_bits.h" yading@10: #include "mpeg4audio.h" yading@10: #include "internal.h" yading@10: yading@10: typedef struct AACBSFContext { yading@10: int first_frame_done; yading@10: } AACBSFContext; yading@10: yading@10: /** yading@10: * This filter creates an MPEG-4 AudioSpecificConfig from an MPEG-2/4 yading@10: * ADTS header and removes the ADTS header. yading@10: */ yading@10: static int aac_adtstoasc_filter(AVBitStreamFilterContext *bsfc, yading@10: AVCodecContext *avctx, const char *args, yading@10: uint8_t **poutbuf, int *poutbuf_size, yading@10: const uint8_t *buf, int buf_size, yading@10: int keyframe) yading@10: { yading@10: GetBitContext gb; yading@10: PutBitContext pb; yading@10: AACADTSHeaderInfo hdr; yading@10: yading@10: AACBSFContext *ctx = bsfc->priv_data; yading@10: yading@10: init_get_bits(&gb, buf, AAC_ADTS_HEADER_SIZE*8); yading@10: yading@10: *poutbuf = (uint8_t*) buf; yading@10: *poutbuf_size = buf_size; yading@10: yading@10: if (avctx->extradata) yading@10: if (show_bits(&gb, 12) != 0xfff) yading@10: return 0; yading@10: yading@10: if (avpriv_aac_parse_header(&gb, &hdr) < 0) { yading@10: av_log(avctx, AV_LOG_ERROR, "Error parsing ADTS frame header!\n"); yading@10: return -1; yading@10: } yading@10: yading@10: if (!hdr.crc_absent && hdr.num_aac_frames > 1) { yading@10: avpriv_report_missing_feature(avctx, yading@10: "Multiple RDBs per frame with CRC"); yading@10: return AVERROR_PATCHWELCOME; yading@10: } yading@10: yading@10: buf += AAC_ADTS_HEADER_SIZE + 2*!hdr.crc_absent; yading@10: buf_size -= AAC_ADTS_HEADER_SIZE + 2*!hdr.crc_absent; yading@10: yading@10: if (!ctx->first_frame_done) { yading@10: int pce_size = 0; yading@10: uint8_t pce_data[MAX_PCE_SIZE]; yading@10: if (!hdr.chan_config) { yading@10: init_get_bits(&gb, buf, buf_size * 8); yading@10: if (get_bits(&gb, 3) != 5) { yading@10: avpriv_report_missing_feature(avctx, yading@10: "PCE-based channel configuration " yading@10: "without PCE as first syntax " yading@10: "element"); yading@10: return AVERROR_PATCHWELCOME; yading@10: } yading@10: init_put_bits(&pb, pce_data, MAX_PCE_SIZE); yading@10: pce_size = avpriv_copy_pce_data(&pb, &gb)/8; yading@10: flush_put_bits(&pb); yading@10: buf_size -= get_bits_count(&gb)/8; yading@10: buf += get_bits_count(&gb)/8; yading@10: } yading@10: avctx->extradata_size = 2 + pce_size; yading@10: avctx->extradata = av_mallocz(avctx->extradata_size + FF_INPUT_BUFFER_PADDING_SIZE); yading@10: yading@10: init_put_bits(&pb, avctx->extradata, avctx->extradata_size); yading@10: put_bits(&pb, 5, hdr.object_type); yading@10: put_bits(&pb, 4, hdr.sampling_index); yading@10: put_bits(&pb, 4, hdr.chan_config); yading@10: put_bits(&pb, 1, 0); //frame length - 1024 samples yading@10: put_bits(&pb, 1, 0); //does not depend on core coder yading@10: put_bits(&pb, 1, 0); //is not extension yading@10: flush_put_bits(&pb); yading@10: if (pce_size) { yading@10: memcpy(avctx->extradata + 2, pce_data, pce_size); yading@10: } yading@10: yading@10: ctx->first_frame_done = 1; yading@10: } yading@10: yading@10: *poutbuf = (uint8_t*) buf; yading@10: *poutbuf_size = buf_size; yading@10: yading@10: return 0; yading@10: } yading@10: yading@10: AVBitStreamFilter ff_aac_adtstoasc_bsf = { yading@10: "aac_adtstoasc", yading@10: sizeof(AACBSFContext), yading@10: aac_adtstoasc_filter, yading@10: };