annotate ffmpeg/libavcodec/aac_adtstoasc_bsf.c @ 13:844d341cf643 tip

Back up before ISMIR
author Yading Song <yading.song@eecs.qmul.ac.uk>
date Thu, 31 Oct 2013 13:17:06 +0000
parents 6840f77b83aa
children
rev   line source
yading@10 1 /*
yading@10 2 * MPEG-2/4 AAC ADTS to MPEG-4 Audio Specific Configuration bitstream filter
yading@10 3 * Copyright (c) 2009 Alex Converse <alex.converse@gmail.com>
yading@10 4 *
yading@10 5 * This file is part of FFmpeg.
yading@10 6 *
yading@10 7 * FFmpeg is free software; you can redistribute it and/or
yading@10 8 * modify it under the terms of the GNU Lesser General Public
yading@10 9 * License as published by the Free Software Foundation; either
yading@10 10 * version 2.1 of the License, or (at your option) any later version.
yading@10 11 *
yading@10 12 * FFmpeg is distributed in the hope that it will be useful,
yading@10 13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
yading@10 14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
yading@10 15 * Lesser General Public License for more details.
yading@10 16 *
yading@10 17 * You should have received a copy of the GNU Lesser General Public
yading@10 18 * License along with FFmpeg; if not, write to the Free Software
yading@10 19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
yading@10 20 */
yading@10 21
yading@10 22 #include "avcodec.h"
yading@10 23 #include "aacadtsdec.h"
yading@10 24 #include "put_bits.h"
yading@10 25 #include "get_bits.h"
yading@10 26 #include "mpeg4audio.h"
yading@10 27 #include "internal.h"
yading@10 28
yading@10 29 typedef struct AACBSFContext {
yading@10 30 int first_frame_done;
yading@10 31 } AACBSFContext;
yading@10 32
yading@10 33 /**
yading@10 34 * This filter creates an MPEG-4 AudioSpecificConfig from an MPEG-2/4
yading@10 35 * ADTS header and removes the ADTS header.
yading@10 36 */
yading@10 37 static int aac_adtstoasc_filter(AVBitStreamFilterContext *bsfc,
yading@10 38 AVCodecContext *avctx, const char *args,
yading@10 39 uint8_t **poutbuf, int *poutbuf_size,
yading@10 40 const uint8_t *buf, int buf_size,
yading@10 41 int keyframe)
yading@10 42 {
yading@10 43 GetBitContext gb;
yading@10 44 PutBitContext pb;
yading@10 45 AACADTSHeaderInfo hdr;
yading@10 46
yading@10 47 AACBSFContext *ctx = bsfc->priv_data;
yading@10 48
yading@10 49 init_get_bits(&gb, buf, AAC_ADTS_HEADER_SIZE*8);
yading@10 50
yading@10 51 *poutbuf = (uint8_t*) buf;
yading@10 52 *poutbuf_size = buf_size;
yading@10 53
yading@10 54 if (avctx->extradata)
yading@10 55 if (show_bits(&gb, 12) != 0xfff)
yading@10 56 return 0;
yading@10 57
yading@10 58 if (avpriv_aac_parse_header(&gb, &hdr) < 0) {
yading@10 59 av_log(avctx, AV_LOG_ERROR, "Error parsing ADTS frame header!\n");
yading@10 60 return -1;
yading@10 61 }
yading@10 62
yading@10 63 if (!hdr.crc_absent && hdr.num_aac_frames > 1) {
yading@10 64 avpriv_report_missing_feature(avctx,
yading@10 65 "Multiple RDBs per frame with CRC");
yading@10 66 return AVERROR_PATCHWELCOME;
yading@10 67 }
yading@10 68
yading@10 69 buf += AAC_ADTS_HEADER_SIZE + 2*!hdr.crc_absent;
yading@10 70 buf_size -= AAC_ADTS_HEADER_SIZE + 2*!hdr.crc_absent;
yading@10 71
yading@10 72 if (!ctx->first_frame_done) {
yading@10 73 int pce_size = 0;
yading@10 74 uint8_t pce_data[MAX_PCE_SIZE];
yading@10 75 if (!hdr.chan_config) {
yading@10 76 init_get_bits(&gb, buf, buf_size * 8);
yading@10 77 if (get_bits(&gb, 3) != 5) {
yading@10 78 avpriv_report_missing_feature(avctx,
yading@10 79 "PCE-based channel configuration "
yading@10 80 "without PCE as first syntax "
yading@10 81 "element");
yading@10 82 return AVERROR_PATCHWELCOME;
yading@10 83 }
yading@10 84 init_put_bits(&pb, pce_data, MAX_PCE_SIZE);
yading@10 85 pce_size = avpriv_copy_pce_data(&pb, &gb)/8;
yading@10 86 flush_put_bits(&pb);
yading@10 87 buf_size -= get_bits_count(&gb)/8;
yading@10 88 buf += get_bits_count(&gb)/8;
yading@10 89 }
yading@10 90 avctx->extradata_size = 2 + pce_size;
yading@10 91 avctx->extradata = av_mallocz(avctx->extradata_size + FF_INPUT_BUFFER_PADDING_SIZE);
yading@10 92
yading@10 93 init_put_bits(&pb, avctx->extradata, avctx->extradata_size);
yading@10 94 put_bits(&pb, 5, hdr.object_type);
yading@10 95 put_bits(&pb, 4, hdr.sampling_index);
yading@10 96 put_bits(&pb, 4, hdr.chan_config);
yading@10 97 put_bits(&pb, 1, 0); //frame length - 1024 samples
yading@10 98 put_bits(&pb, 1, 0); //does not depend on core coder
yading@10 99 put_bits(&pb, 1, 0); //is not extension
yading@10 100 flush_put_bits(&pb);
yading@10 101 if (pce_size) {
yading@10 102 memcpy(avctx->extradata + 2, pce_data, pce_size);
yading@10 103 }
yading@10 104
yading@10 105 ctx->first_frame_done = 1;
yading@10 106 }
yading@10 107
yading@10 108 *poutbuf = (uint8_t*) buf;
yading@10 109 *poutbuf_size = buf_size;
yading@10 110
yading@10 111 return 0;
yading@10 112 }
yading@10 113
yading@10 114 AVBitStreamFilter ff_aac_adtstoasc_bsf = {
yading@10 115 "aac_adtstoasc",
yading@10 116 sizeof(AACBSFContext),
yading@10 117 aac_adtstoasc_filter,
yading@10 118 };