From b5f19f7478492307e4b4763aeac3180faf50e17f Mon Sep 17 00:00:00 2001 From: Diego Biurrun Date: Wed, 26 Apr 2017 18:57:54 +0200 Subject: aac: Split function to parse ADTS header data into public and private part This makes the currently semi-public avpriv_aac_parse_header() function private to libavcodec and adds a proper public API function to return the parts of the ADTS header required in libavformat. --- libavcodec/Makefile | 11 ++++--- libavcodec/aac_adtstoasc_bsf.c | 13 ++++---- libavcodec/aac_parser.c | 10 +++--- libavcodec/aacadtsdec.c | 70 ----------------------------------------- libavcodec/aacadtsdec.h | 54 -------------------------------- libavcodec/aacdec.c | 4 +-- libavcodec/adts_header.c | 71 ++++++++++++++++++++++++++++++++++++++++++ libavcodec/adts_header.h | 50 +++++++++++++++++++++++++++++ libavcodec/adts_parser.c | 44 ++++++++++++++++++++++++++ libavcodec/adts_parser.h | 37 ++++++++++++++++++++++ 10 files changed, 223 insertions(+), 141 deletions(-) delete mode 100644 libavcodec/aacadtsdec.c delete mode 100644 libavcodec/aacadtsdec.h create mode 100644 libavcodec/adts_header.c create mode 100644 libavcodec/adts_header.h create mode 100644 libavcodec/adts_parser.c create mode 100644 libavcodec/adts_parser.h (limited to 'libavcodec') diff --git a/libavcodec/Makefile b/libavcodec/Makefile index b60ba5e..a1bb4b5 100644 --- a/libavcodec/Makefile +++ b/libavcodec/Makefile @@ -2,6 +2,7 @@ NAME = avcodec DESC = Libav codec library HEADERS = ac3_parser.h \ + adts_parser.h \ avcodec.h \ avfft.h \ d3d11va.h \ @@ -16,6 +17,7 @@ HEADERS = ac3_parser.h \ vorbis_parser.h \ OBJS = ac3_parser.o \ + adts_parser.o \ allcodecs.o \ avpacket.o \ avpicture.o \ @@ -45,6 +47,7 @@ OBJS = ac3_parser.o \ # subsystems OBJS-$(CONFIG_AANDCTTABLES) += aandcttab.o OBJS-$(CONFIG_AC3DSP) += ac3dsp.o +OBJS-$(CONFIG_ADTS_HEADER) += adts_header.o mpeg4audio.o OBJS-$(CONFIG_AUDIO_FRAME_QUEUE) += audio_frame_queue.o OBJS-$(CONFIG_AUDIODSP) += audiodsp.o OBJS-$(CONFIG_BLOCKDSP) += blockdsp.o @@ -126,7 +129,7 @@ OBJS-$(CONFIG_WMV2DSP) += wmv2dsp.o OBJS-$(CONFIG_A64MULTI_ENCODER) += a64multienc.o elbg.o OBJS-$(CONFIG_A64MULTI5_ENCODER) += a64multienc.o elbg.o OBJS-$(CONFIG_AAC_DECODER) += aacdec.o aactab.o aacsbr.o aacps.o \ - aacadtsdec.o mpeg4audio.o kbdwin.o \ + mpeg4audio.o kbdwin.o \ sbrdsp.o aacpsdsp.o OBJS-$(CONFIG_AAC_ENCODER) += aacenc.o aaccoder.o \ aacpsy.o aactab.o \ @@ -666,7 +669,6 @@ OBJS-$(CONFIG_MATROSKA_AUDIO_MUXER) += mpeg4audio.o OBJS-$(CONFIG_MATROSKA_MUXER) += mpeg4audio.o OBJS-$(CONFIG_MOV_DEMUXER) += ac3tab.o OBJS-$(CONFIG_NUT_MUXER) += mpegaudiodata.o -OBJS-$(CONFIG_SPDIF_DEMUXER) += aacadtsdec.o mpeg4audio.o OBJS-$(CONFIG_SPDIF_MUXER) += dca.o OBJS-$(CONFIG_TAK_DEMUXER) += tak.o OBJS-$(CONFIG_WEBM_MUXER) += mpeg4audio.o @@ -722,7 +724,7 @@ OBJS-$(CONFIG_LIBXVID_ENCODER) += libxvid.o # parsers OBJS-$(CONFIG_AAC_LATM_PARSER) += latm_parser.o OBJS-$(CONFIG_AAC_PARSER) += aac_parser.o aac_ac3_parser.o \ - aacadtsdec.o mpeg4audio.o + mpeg4audio.o OBJS-$(CONFIG_AC3_PARSER) += ac3tab.o aac_ac3_parser.o OBJS-$(CONFIG_ADX_PARSER) += adx_parser.o adx.o OBJS-$(CONFIG_BMP_PARSER) += bmp_parser.o @@ -760,8 +762,7 @@ OBJS-$(CONFIG_VP3_PARSER) += vp3_parser.o OBJS-$(CONFIG_VP8_PARSER) += vp8_parser.o # bitstream filters -OBJS-$(CONFIG_AAC_ADTSTOASC_BSF) += aac_adtstoasc_bsf.o aacadtsdec.o \ - mpeg4audio.o +OBJS-$(CONFIG_AAC_ADTSTOASC_BSF) += aac_adtstoasc_bsf.o mpeg4audio.o OBJS-$(CONFIG_CHOMP_BSF) += chomp_bsf.o OBJS-$(CONFIG_DUMP_EXTRADATA_BSF) += dump_extradata_bsf.o OBJS-$(CONFIG_EXTRACT_EXTRADATA_BSF) += extract_extradata_bsf.o \ diff --git a/libavcodec/aac_adtstoasc_bsf.c b/libavcodec/aac_adtstoasc_bsf.c index 3a83d48..778387c 100644 --- a/libavcodec/aac_adtstoasc_bsf.c +++ b/libavcodec/aac_adtstoasc_bsf.c @@ -19,8 +19,9 @@ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA */ +#include "adts_header.h" +#include "adts_parser.h" #include "avcodec.h" -#include "aacadtsdec.h" #include "bsf.h" #include "put_bits.h" #include "get_bits.h" @@ -49,15 +50,15 @@ static int aac_adtstoasc_filter(AVBSFContext *bsfc, AVPacket *out) if (ret < 0) return ret; - if (in->size < AAC_ADTS_HEADER_SIZE) + if (in->size < AV_AAC_ADTS_HEADER_SIZE) goto packet_too_small; - init_get_bits(&gb, in->data, AAC_ADTS_HEADER_SIZE * 8); + init_get_bits(&gb, in->data, AV_AAC_ADTS_HEADER_SIZE * 8); if (bsfc->par_in->extradata && show_bits(&gb, 12) != 0xfff) goto finish; - if (avpriv_aac_parse_header(&gb, &hdr) < 0) { + if (ff_adts_header_parse(&gb, &hdr) < 0) { av_log(bsfc, AV_LOG_ERROR, "Error parsing ADTS frame header!\n"); ret = AVERROR_INVALIDDATA; goto fail; @@ -70,10 +71,10 @@ static int aac_adtstoasc_filter(AVBSFContext *bsfc, AVPacket *out) goto fail; } - in->size -= AAC_ADTS_HEADER_SIZE + 2 * !hdr.crc_absent; + in->size -= AV_AAC_ADTS_HEADER_SIZE + 2 * !hdr.crc_absent; if (in->size <= 0) goto packet_too_small; - in->data += AAC_ADTS_HEADER_SIZE + 2 * !hdr.crc_absent; + in->data += AV_AAC_ADTS_HEADER_SIZE + 2 * !hdr.crc_absent; if (!ctx->first_frame_done) { int pce_size = 0; diff --git a/libavcodec/aac_parser.c b/libavcodec/aac_parser.c index eae120a..41b301c 100644 --- a/libavcodec/aac_parser.c +++ b/libavcodec/aac_parser.c @@ -22,7 +22,8 @@ #include "parser.h" #include "aac_ac3_parser.h" -#include "aacadtsdec.h" +#include "adts_header.h" +#include "adts_parser.h" #include "get_bits.h" #include "mpeg4audio.h" @@ -38,9 +39,10 @@ static int aac_sync(uint64_t state, AACAC3ParseContext *hdr_info, } tmp; tmp.u64 = av_be2ne64(state); - init_get_bits(&bits, tmp.u8+8-AAC_ADTS_HEADER_SIZE, AAC_ADTS_HEADER_SIZE * 8); + init_get_bits(&bits, tmp.u8 + 8 - AV_AAC_ADTS_HEADER_SIZE, + AV_AAC_ADTS_HEADER_SIZE * 8); - if ((size = avpriv_aac_parse_header(&bits, &hdr)) < 0) + if ((size = ff_adts_header_parse(&bits, &hdr)) < 0) return 0; *need_next_header = 0; *new_frame_start = 1; @@ -54,7 +56,7 @@ static int aac_sync(uint64_t state, AACAC3ParseContext *hdr_info, static av_cold int aac_parse_init(AVCodecParserContext *s1) { AACAC3ParseContext *s = s1->priv_data; - s->header_size = AAC_ADTS_HEADER_SIZE; + s->header_size = AV_AAC_ADTS_HEADER_SIZE; s->sync = aac_sync; return 0; } diff --git a/libavcodec/aacadtsdec.c b/libavcodec/aacadtsdec.c deleted file mode 100644 index 2994bce..0000000 --- a/libavcodec/aacadtsdec.c +++ /dev/null @@ -1,70 +0,0 @@ -/* - * Audio and Video frame extraction - * Copyright (c) 2003 Fabrice Bellard - * Copyright (c) 2003 Michael Niedermayer - * Copyright (c) 2009 Alex Converse - * - * This file is part of Libav. - * - * Libav 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. - * - * Libav 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 Libav; if not, write to the Free Software - * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA - */ - -#include "aac_ac3_parser.h" -#include "aacadtsdec.h" -#include "get_bits.h" -#include "mpeg4audio.h" - -int avpriv_aac_parse_header(GetBitContext *gbc, AACADTSHeaderInfo *hdr) -{ - int size, rdb, ch, sr; - int aot, crc_abs; - - if (get_bits(gbc, 12) != 0xfff) - return AAC_AC3_PARSE_ERROR_SYNC; - - skip_bits1(gbc); /* id */ - skip_bits(gbc, 2); /* layer */ - crc_abs = get_bits1(gbc); /* protection_absent */ - aot = get_bits(gbc, 2); /* profile_objecttype */ - sr = get_bits(gbc, 4); /* sample_frequency_index */ - if (!avpriv_mpeg4audio_sample_rates[sr]) - return AAC_AC3_PARSE_ERROR_SAMPLE_RATE; - skip_bits1(gbc); /* private_bit */ - ch = get_bits(gbc, 3); /* channel_configuration */ - - skip_bits1(gbc); /* original/copy */ - skip_bits1(gbc); /* home */ - - /* adts_variable_header */ - skip_bits1(gbc); /* copyright_identification_bit */ - skip_bits1(gbc); /* copyright_identification_start */ - size = get_bits(gbc, 13); /* aac_frame_length */ - if (size < AAC_ADTS_HEADER_SIZE) - return AAC_AC3_PARSE_ERROR_FRAME_SIZE; - - skip_bits(gbc, 11); /* adts_buffer_fullness */ - rdb = get_bits(gbc, 2); /* number_of_raw_data_blocks_in_frame */ - - hdr->object_type = aot + 1; - hdr->chan_config = ch; - hdr->crc_absent = crc_abs; - hdr->num_aac_frames = rdb + 1; - hdr->sampling_index = sr; - hdr->sample_rate = avpriv_mpeg4audio_sample_rates[sr]; - hdr->samples = (rdb + 1) * 1024; - hdr->bit_rate = size * 8 * hdr->sample_rate / hdr->samples; - - return size; -} diff --git a/libavcodec/aacadtsdec.h b/libavcodec/aacadtsdec.h deleted file mode 100644 index 6319efc..0000000 --- a/libavcodec/aacadtsdec.h +++ /dev/null @@ -1,54 +0,0 @@ -/* - * AAC ADTS header decoding prototypes and structures - * Copyright (c) 2003 Fabrice Bellard - * Copyright (c) 2003 Michael Niedermayer - * - * This file is part of Libav. - * - * Libav 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. - * - * Libav 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 Libav; if not, write to the Free Software - * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA - */ - -#ifndef AVCODEC_AACADTSDEC_H -#define AVCODEC_AACADTSDEC_H - -#include -#include "get_bits.h" - -#define AAC_ADTS_HEADER_SIZE 7 - -typedef struct AACADTSHeaderInfo { - uint32_t sample_rate; - uint32_t samples; - uint32_t bit_rate; - uint8_t crc_absent; - uint8_t object_type; - uint8_t sampling_index; - uint8_t chan_config; - uint8_t num_aac_frames; -} AACADTSHeaderInfo; - -/** - * Parse AAC frame header. - * Parse the ADTS frame header to the end of the variable header, which is - * the first 54 bits. - * @param[in] gbc BitContext containing the first 54 bits of the frame. - * @param[out] hdr Pointer to struct where header info is written. - * @return Returns 0 on success, -1 if there is a sync word mismatch, - * -2 if the version element is invalid, -3 if the sample rate - * element is invalid, or -4 if the bit rate element is invalid. - */ -int avpriv_aac_parse_header(GetBitContext *gbc, AACADTSHeaderInfo *hdr); - -#endif /* AVCODEC_AACADTSDEC_H */ diff --git a/libavcodec/aacdec.c b/libavcodec/aacdec.c index 4ba346a..e436b4f 100644 --- a/libavcodec/aacdec.c +++ b/libavcodec/aacdec.c @@ -93,11 +93,11 @@ #include "aac.h" #include "aactab.h" #include "aacdectab.h" +#include "adts_header.h" #include "cbrt_tablegen.h" #include "sbr.h" #include "aacsbr.h" #include "mpeg4audio.h" -#include "aacadtsdec.h" #include "libavutil/intfloat.h" #include @@ -2705,7 +2705,7 @@ static int parse_adts_frame_header(AACContext *ac, GetBitContext *gb) uint8_t layout_map[MAX_ELEM_ID*4][3]; int layout_map_tags, ret; - size = avpriv_aac_parse_header(gb, &hdr_info); + size = ff_adts_header_parse(gb, &hdr_info); if (size > 0) { if (hdr_info.num_aac_frames != 1) { avpriv_report_missing_feature(ac->avctx, diff --git a/libavcodec/adts_header.c b/libavcodec/adts_header.c new file mode 100644 index 0000000..3b84505 --- /dev/null +++ b/libavcodec/adts_header.c @@ -0,0 +1,71 @@ +/* + * Audio and Video frame extraction + * Copyright (c) 2003 Fabrice Bellard + * Copyright (c) 2003 Michael Niedermayer + * Copyright (c) 2009 Alex Converse + * + * This file is part of Libav. + * + * Libav 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. + * + * Libav 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 Libav; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + */ + +#include "aac_ac3_parser.h" +#include "adts_header.h" +#include "adts_parser.h" +#include "get_bits.h" +#include "mpeg4audio.h" + +int ff_adts_header_parse(GetBitContext *gbc, AACADTSHeaderInfo *hdr) +{ + int size, rdb, ch, sr; + int aot, crc_abs; + + if (get_bits(gbc, 12) != 0xfff) + return AAC_AC3_PARSE_ERROR_SYNC; + + skip_bits1(gbc); /* id */ + skip_bits(gbc, 2); /* layer */ + crc_abs = get_bits1(gbc); /* protection_absent */ + aot = get_bits(gbc, 2); /* profile_objecttype */ + sr = get_bits(gbc, 4); /* sample_frequency_index */ + if (!avpriv_mpeg4audio_sample_rates[sr]) + return AAC_AC3_PARSE_ERROR_SAMPLE_RATE; + skip_bits1(gbc); /* private_bit */ + ch = get_bits(gbc, 3); /* channel_configuration */ + + skip_bits1(gbc); /* original/copy */ + skip_bits1(gbc); /* home */ + + /* adts_variable_header */ + skip_bits1(gbc); /* copyright_identification_bit */ + skip_bits1(gbc); /* copyright_identification_start */ + size = get_bits(gbc, 13); /* aac_frame_length */ + if (size < AV_AAC_ADTS_HEADER_SIZE) + return AAC_AC3_PARSE_ERROR_FRAME_SIZE; + + skip_bits(gbc, 11); /* adts_buffer_fullness */ + rdb = get_bits(gbc, 2); /* number_of_raw_data_blocks_in_frame */ + + hdr->object_type = aot + 1; + hdr->chan_config = ch; + hdr->crc_absent = crc_abs; + hdr->num_aac_frames = rdb + 1; + hdr->sampling_index = sr; + hdr->sample_rate = avpriv_mpeg4audio_sample_rates[sr]; + hdr->samples = (rdb + 1) * 1024; + hdr->bit_rate = size * 8 * hdr->sample_rate / hdr->samples; + + return size; +} diff --git a/libavcodec/adts_header.h b/libavcodec/adts_header.h new file mode 100644 index 0000000..386aa0a --- /dev/null +++ b/libavcodec/adts_header.h @@ -0,0 +1,50 @@ +/* + * AAC ADTS header decoding prototypes and structures + * Copyright (c) 2003 Fabrice Bellard + * Copyright (c) 2003 Michael Niedermayer + * + * This file is part of Libav. + * + * Libav 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. + * + * Libav 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 Libav; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + */ + +#ifndef AVCODEC_ADTS_HEADER_H +#define AVCODEC_ADTS_HEADER_H + +#include "get_bits.h" + +typedef struct AACADTSHeaderInfo { + uint32_t sample_rate; + uint32_t samples; + uint32_t bit_rate; + uint8_t crc_absent; + uint8_t object_type; + uint8_t sampling_index; + uint8_t chan_config; + uint8_t num_aac_frames; +} AACADTSHeaderInfo; + +/** + * Parse the ADTS frame header to the end of the variable header, which is + * the first 54 bits. + * @param[in] gbc BitContext containing the first 54 bits of the frame. + * @param[out] hdr Pointer to struct where header info is written. + * @return Returns 0 on success, -1 if there is a sync word mismatch, + * -2 if the version element is invalid, -3 if the sample rate + * element is invalid, or -4 if the bit rate element is invalid. + */ +int ff_adts_header_parse(GetBitContext *gbc, AACADTSHeaderInfo *hdr); + +#endif /* AVCODEC_ADTS_HEADER_H */ diff --git a/libavcodec/adts_parser.c b/libavcodec/adts_parser.c new file mode 100644 index 0000000..5821e6f --- /dev/null +++ b/libavcodec/adts_parser.c @@ -0,0 +1,44 @@ +/* + * This file is part of Libav. + * + * Libav 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. + * + * Libav 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 Libav; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + */ + +#include "config.h" + +#include +#include + +#include "adts_header.h" +#include "adts_parser.h" + +int av_adts_header_parse(const uint8_t *buf, uint32_t *samples, uint8_t *frames) +{ +#if CONFIG_ADTS_HEADER + GetBitContext gb; + AACADTSHeaderInfo hdr; + int err = init_get_bits8(&gb, buf, AV_AAC_ADTS_HEADER_SIZE); + if (err < 0) + return err; + err = ff_adts_header_parse(&gb, &hdr); + if (err < 0) + return err; + *samples = hdr.samples; + *frames = hdr.num_aac_frames; + return 0; +#else + return AVERROR(ENOSYS); +#endif +} diff --git a/libavcodec/adts_parser.h b/libavcodec/adts_parser.h new file mode 100644 index 0000000..1a3328f --- /dev/null +++ b/libavcodec/adts_parser.h @@ -0,0 +1,37 @@ +/* + * This file is part of Libav. + * + * Libav 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. + * + * Libav 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 Libav; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + */ + +#ifndef AVCODEC_ADTS_PARSER_H +#define AVCODEC_ADTS_PARSER_H + +#include +#include + +#define AV_AAC_ADTS_HEADER_SIZE 7 + +/** + * Extract the number of samples and frames from AAC data. + * @param[in] buf pointer to AAC data buffer + * @param[out] samples Pointer to where number of samples is written + * @param[out] frames Pointer to where number of frames is written + * @return Returns 0 on success, error code on failure. + */ +int av_adts_header_parse(const uint8_t *buf, uint32_t *samples, + uint8_t *frames); + +#endif /* AVCODEC_ADTS_PARSER_H */ -- cgit v1.1