From b5f19f7478492307e4b4763aeac3180faf50e17f Mon Sep 17 00:00:00 2001 From: Diego Biurrun Date: Wed, 26 Apr 2017 18:57:54 +0200 Subject: [PATCH] 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. --- configure | 8 +++- doc/developer.texi | 2 +- libavcodec/Makefile | 11 +++--- libavcodec/aac_adtstoasc_bsf.c | 13 ++++--- libavcodec/aac_parser.c | 10 +++-- libavcodec/aacdec.c | 4 +- libavcodec/{aacadtsdec.c => adts_header.c} | 7 ++-- libavcodec/{aacadtsdec.h => adts_header.h} | 12 ++---- libavcodec/adts_parser.c | 44 ++++++++++++++++++++++ libavcodec/adts_parser.h | 37 ++++++++++++++++++ libavformat/spdifdec.c | 24 +++++++----- libavformat/spdifenc.c | 17 ++++----- 12 files changed, 139 insertions(+), 50 deletions(-) rename libavcodec/{aacadtsdec.c => adts_header.c} (93%) rename libavcodec/{aacadtsdec.h => adts_header.h} (86%) create mode 100644 libavcodec/adts_parser.c create mode 100644 libavcodec/adts_parser.h diff --git a/configure b/configure index c7d0363618..6f696c9ab5 100755 --- a/configure +++ b/configure @@ -1728,6 +1728,7 @@ HAVE_LIST=" CONFIG_EXTRA=" aandcttables ac3dsp + adts_header audio_frame_queue audiodsp blockdsp @@ -1966,7 +1967,7 @@ mpegvideoenc_select="me_cmp mpegvideo pixblockdsp qpeldsp" vc1dsp_select="h264chroma qpeldsp startcode" # decoders / encoders -aac_decoder_select="imdct15 mdct sinewin" +aac_decoder_select="adts_header imdct15 mdct sinewin" aac_encoder_select="audio_frame_queue iirfilter mdct sinewin" aac_latm_decoder_select="aac_decoder aac_latm_parser" ac3_decoder_select="ac3_parser ac3dsp bswapdsp fmtconvert mdct" @@ -2280,6 +2281,7 @@ nvenc_h264_encoder_select="h264_nvenc_encoder" nvenc_hevc_encoder_select="hevc_nvenc_encoder" # parsers +aac_parser_select="adts_header" h264_parser_select="golomb h264dsp h264parse" hevc_parser_select="hevc_ps" mpegaudio_parser_select="mpegaudioheader" @@ -2288,6 +2290,7 @@ mpeg4video_parser_select="error_resilience h263dsp mpegvideo qpeldsp" vc1_parser_select="vc1dsp" # bitstream_filters +aac_adtstoasc_bsf_select="adts_header" mjpeg2jpeg_bsf_select="jpegtables" # external libraries @@ -2396,7 +2399,8 @@ sap_demuxer_select="sdp_demuxer" sap_muxer_select="rtp_muxer rtp_protocol rtpenc_chain" sdp_demuxer_select="rtpdec" smoothstreaming_muxer_select="ismv_muxer" -spdif_muxer_select="aac_parser" +spdif_demuxer_select="adts_header" +spdif_muxer_select="adts_header" spx_muxer_select="ogg_muxer" swf_demuxer_suggest="zlib" tak_demuxer_select="tak_parser" diff --git a/doc/developer.texi b/doc/developer.texi index 31c2bafbe4..824fba4591 100644 --- a/doc/developer.texi +++ b/doc/developer.texi @@ -218,7 +218,7 @@ e.g. @samp{ff_w64_demuxer}. @item For variables and functions visible outside of file scope, used internally across multiple libraries, use @code{avpriv_} as prefix, for example, -@samp{avpriv_aac_parse_header}. +@samp{avpriv_report_missing_feature}. @item For externally visible symbols, each library has its own prefix. Check diff --git a/libavcodec/Makefile b/libavcodec/Makefile index b60ba5ef96..a1bb4b5950 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 3a83d488c4..778387cabb 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 eae120a249..41b301ca17 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/aacdec.c b/libavcodec/aacdec.c index 4ba346ac48..e436b4f2f7 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/aacadtsdec.c b/libavcodec/adts_header.c similarity index 93% rename from libavcodec/aacadtsdec.c rename to libavcodec/adts_header.c index 2994bce243..3b845058ff 100644 --- a/libavcodec/aacadtsdec.c +++ b/libavcodec/adts_header.c @@ -22,11 +22,12 @@ */ #include "aac_ac3_parser.h" -#include "aacadtsdec.h" +#include "adts_header.h" +#include "adts_parser.h" #include "get_bits.h" #include "mpeg4audio.h" -int avpriv_aac_parse_header(GetBitContext *gbc, AACADTSHeaderInfo *hdr) +int ff_adts_header_parse(GetBitContext *gbc, AACADTSHeaderInfo *hdr) { int size, rdb, ch, sr; int aot, crc_abs; @@ -51,7 +52,7 @@ int avpriv_aac_parse_header(GetBitContext *gbc, AACADTSHeaderInfo *hdr) 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) + if (size < AV_AAC_ADTS_HEADER_SIZE) return AAC_AC3_PARSE_ERROR_FRAME_SIZE; skip_bits(gbc, 11); /* adts_buffer_fullness */ diff --git a/libavcodec/aacadtsdec.h b/libavcodec/adts_header.h similarity index 86% rename from libavcodec/aacadtsdec.h rename to libavcodec/adts_header.h index 6319efcea3..386aa0a331 100644 --- a/libavcodec/aacadtsdec.h +++ b/libavcodec/adts_header.h @@ -20,14 +20,11 @@ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA */ -#ifndef AVCODEC_AACADTSDEC_H -#define AVCODEC_AACADTSDEC_H +#ifndef AVCODEC_ADTS_HEADER_H +#define AVCODEC_ADTS_HEADER_H -#include #include "get_bits.h" -#define AAC_ADTS_HEADER_SIZE 7 - typedef struct AACADTSHeaderInfo { uint32_t sample_rate; uint32_t samples; @@ -40,7 +37,6 @@ typedef struct AACADTSHeaderInfo { } 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. @@ -49,6 +45,6 @@ typedef struct AACADTSHeaderInfo { * -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); +int ff_adts_header_parse(GetBitContext *gbc, AACADTSHeaderInfo *hdr); -#endif /* AVCODEC_AACADTSDEC_H */ +#endif /* AVCODEC_ADTS_HEADER_H */ diff --git a/libavcodec/adts_parser.c b/libavcodec/adts_parser.c new file mode 100644 index 0000000000..5821e6fb79 --- /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 0000000000..1a3328f10e --- /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 */ diff --git a/libavformat/spdifdec.c b/libavformat/spdifdec.c index 7c212353f1..38692c27e6 100644 --- a/libavformat/spdifdec.c +++ b/libavformat/spdifdec.c @@ -25,18 +25,22 @@ * @author Anssi Hannula */ +#include "libavutil/bswap.h" + +#include "libavcodec/ac3.h" +#include "libavcodec/adts_parser.h" + #include "avformat.h" #include "spdif.h" -#include "libavcodec/ac3.h" -#include "libavcodec/aacadtsdec.h" static int spdif_get_offset_and_codec(AVFormatContext *s, enum IEC61937DataType data_type, const char *buf, int *offset, enum AVCodecID *codec) { - AACADTSHeaderInfo aac_hdr; - GetBitContext gbc; + uint32_t samples; + uint8_t frames; + int ret; switch (data_type & 0xff) { case IEC61937_AC3: @@ -56,13 +60,13 @@ static int spdif_get_offset_and_codec(AVFormatContext *s, *codec = AV_CODEC_ID_MP3; break; case IEC61937_MPEG2_AAC: - init_get_bits(&gbc, buf, AAC_ADTS_HEADER_SIZE * 8); - if (avpriv_aac_parse_header(&gbc, &aac_hdr)) { + ret = av_adts_header_parse(buf, &samples, &frames); + if (ret < 0) { if (s) /* be silent during a probe */ av_log(s, AV_LOG_ERROR, "Invalid AAC packet in IEC 61937\n"); - return AVERROR_INVALIDDATA; + return ret; } - *offset = aac_hdr.samples << 2; + *offset = samples << 2; *codec = AV_CODEC_ID_AAC; break; case IEC61937_MPEG2_LAYER1_LSF: @@ -100,7 +104,7 @@ static int spdif_get_offset_and_codec(AVFormatContext *s, } /* Largest offset between bursts we currently handle, i.e. AAC with - aac_hdr.samples = 4096 */ + samples = 4096 */ #define SPDIF_MAX_OFFSET 16384 static int spdif_probe(AVProbeData *p) @@ -127,7 +131,7 @@ static int spdif_probe(AVProbeData *p) } else consecutive_codes = 0; - if (buf + 4 + AAC_ADTS_HEADER_SIZE > p->buf + p->buf_size) + if (buf + 4 + AV_AAC_ADTS_HEADER_SIZE > p->buf + p->buf_size) break; /* continue probing to find more sync codes */ diff --git a/libavformat/spdifenc.c b/libavformat/spdifenc.c index a9b3b522b3..6497a82734 100644 --- a/libavformat/spdifenc.c +++ b/libavformat/spdifenc.c @@ -50,9 +50,9 @@ #include "avio_internal.h" #include "spdif.h" #include "libavcodec/ac3.h" +#include "libavcodec/adts_parser.h" #include "libavcodec/dca.h" #include "libavcodec/dca_syncwords.h" -#include "libavcodec/aacadtsdec.h" #include "libavutil/opt.h" typedef struct IEC61937Context { @@ -349,19 +349,18 @@ static int spdif_header_mpeg(AVFormatContext *s, AVPacket *pkt) static int spdif_header_aac(AVFormatContext *s, AVPacket *pkt) { IEC61937Context *ctx = s->priv_data; - AACADTSHeaderInfo hdr; - GetBitContext gbc; + uint32_t samples; + uint8_t frames; int ret; - init_get_bits(&gbc, pkt->data, AAC_ADTS_HEADER_SIZE * 8); - ret = avpriv_aac_parse_header(&gbc, &hdr); + ret = av_adts_header_parse(pkt->data, &samples, &frames); if (ret < 0) { av_log(s, AV_LOG_ERROR, "Wrong AAC file format\n"); - return AVERROR_INVALIDDATA; + return ret; } - ctx->pkt_offset = hdr.samples << 2; - switch (hdr.num_aac_frames) { + ctx->pkt_offset = samples << 2; + switch (frames) { case 1: ctx->data_type = IEC61937_MPEG2_AAC; break; @@ -373,7 +372,7 @@ static int spdif_header_aac(AVFormatContext *s, AVPacket *pkt) break; default: av_log(s, AV_LOG_ERROR, - "%"PRIu32" samples in AAC frame not supported\n", hdr.samples); + "%"PRIu32" samples in AAC frame not supported\n", samples); return AVERROR(EINVAL); } //TODO Data type dependent info (LC profile/SBR)