avcodec/adts_header: Add ff_adts_header_parse_buf()

Most users of ff_adts_header_parse() don't already have
an opened GetBitContext for the header, so add a convenience
function for them.
Also use a forward declaration of GetBitContext in adts_header.h
as this avoids (implicit) inclusion of get_bits.h in some of
the users that now no longer use a GetBitContext of their own.

Reviewed-by: James Almer <jamrial@gmail.com>
Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt@outlook.com>
This commit is contained in:
Andreas Rheinhardt 2024-05-09 16:09:53 +02:00
parent ae937c4902
commit 12ded9cd85
6 changed files with 35 additions and 36 deletions

View File

@ -24,24 +24,18 @@
#include "aac_ac3_parser.h"
#include "adts_header.h"
#include "adts_parser.h"
#include "get_bits.h"
#include "mpeg4audio.h"
#include "libavutil/intreadwrite.h"
static int aac_sync(uint64_t state, int *need_next_header, int *new_frame_start)
{
GetBitContext bits;
uint8_t tmp[8 + AV_INPUT_BUFFER_PADDING_SIZE];
AACADTSHeaderInfo hdr;
int size;
union {
uint64_t u64;
uint8_t u8[8 + AV_INPUT_BUFFER_PADDING_SIZE];
} tmp;
tmp.u64 = av_be2ne64(state);
init_get_bits(&bits, tmp.u8 + 8 - AV_AAC_ADTS_HEADER_SIZE,
AV_AAC_ADTS_HEADER_SIZE * 8);
AV_WB64(tmp, state);
if ((size = ff_adts_header_parse(&bits, &hdr)) < 0)
size = ff_adts_header_parse_buf(tmp + 8 - AV_AAC_ADTS_HEADER_SIZE, &hdr);
if (size < 0)
return 0;
*need_next_header = 0;
*new_frame_start = 1;

View File

@ -25,6 +25,7 @@
#include "adts_parser.h"
#include "get_bits.h"
#include "mpeg4audio.h"
#include "libavutil/avassert.h"
int ff_adts_header_parse(GetBitContext *gbc, AACADTSHeaderInfo *hdr)
{
@ -71,3 +72,12 @@ int ff_adts_header_parse(GetBitContext *gbc, AACADTSHeaderInfo *hdr)
return size;
}
int ff_adts_header_parse_buf(const uint8_t buf[AV_AAC_ADTS_HEADER_SIZE + AV_INPUT_BUFFER_PADDING_SIZE],
AACADTSHeaderInfo *hdr)
{
GetBitContext gb;
av_unused int ret = init_get_bits8(&gb, buf, AV_AAC_ADTS_HEADER_SIZE);
av_assert1(ret >= 0);
return ff_adts_header_parse(&gb, hdr);
}

View File

@ -23,7 +23,8 @@
#ifndef AVCODEC_ADTS_HEADER_H
#define AVCODEC_ADTS_HEADER_H
#include "get_bits.h"
#include "adts_parser.h"
#include "defs.h"
typedef enum {
AAC_PARSE_ERROR_SYNC = -0x1030c0a,
@ -43,6 +44,8 @@ typedef struct AACADTSHeaderInfo {
uint32_t frame_length;
} AACADTSHeaderInfo;
struct GetBitContext;
/**
* Parse the ADTS frame header to the end of the variable header, which is
* the first 54 bits.
@ -51,7 +54,14 @@ typedef struct AACADTSHeaderInfo {
* @return the size in bytes of the header parsed on success and
* AAC_PARSE_ERROR_* values otherwise.
*/
int ff_adts_header_parse(GetBitContext *gbc, AACADTSHeaderInfo *hdr);
int ff_adts_header_parse(struct GetBitContext *gbc, AACADTSHeaderInfo *hdr);
/**
* Wrapper around ff_adts_header_parse() for users that don't already have
* a suitable GetBitContext.
*/
int ff_adts_header_parse_buf(const uint8_t buf[AV_AAC_ADTS_HEADER_SIZE + AV_INPUT_BUFFER_PADDING_SIZE],
AACADTSHeaderInfo *hdr);
/**
* Parse the ADTS frame header contained in the buffer, which is

View File

@ -20,7 +20,9 @@
#include <stddef.h>
#include <stdint.h>
#include <string.h>
#include "libavutil/error.h"
#include "libavutil/mem.h"
#include "adts_header.h"
#include "adts_parser.h"
@ -29,16 +31,12 @@ int av_adts_header_parse(const uint8_t *buf, uint32_t *samples, uint8_t *frames)
{
#if CONFIG_ADTS_HEADER
uint8_t tmpbuf[AV_AAC_ADTS_HEADER_SIZE + AV_INPUT_BUFFER_PADDING_SIZE];
GetBitContext gb;
AACADTSHeaderInfo hdr;
int err;
if (!buf)
return AVERROR(EINVAL);
memcpy(tmpbuf, buf, AV_AAC_ADTS_HEADER_SIZE);
err = init_get_bits8(&gb, tmpbuf, AV_AAC_ADTS_HEADER_SIZE);
if (err < 0)
return err;
err = ff_adts_header_parse(&gb, &hdr);
err = ff_adts_header_parse_buf(tmpbuf, &hdr);
if (err < 0)
return err;
*samples = hdr.samples;
@ -54,7 +52,6 @@ int avpriv_adts_header_parse(AACADTSHeaderInfo **phdr, const uint8_t *buf, size_
#if CONFIG_ADTS_HEADER
int ret = 0;
int allocated = 0;
GetBitContext gb;
if (!phdr || !buf || size < AV_AAC_ADTS_HEADER_SIZE)
return AVERROR_INVALIDDATA;
@ -66,14 +63,7 @@ int avpriv_adts_header_parse(AACADTSHeaderInfo **phdr, const uint8_t *buf, size_
if (!*phdr)
return AVERROR(ENOMEM);
ret = init_get_bits8(&gb, buf, AV_AAC_ADTS_HEADER_SIZE);
if (ret < 0) {
if (allocated)
av_freep(phdr);
return ret;
}
ret = ff_adts_header_parse(&gb, *phdr);
ret = ff_adts_header_parse_buf(buf, *phdr);
if (ret < 0) {
if (allocated)
av_freep(phdr);

View File

@ -40,7 +40,6 @@ static int aac_adtstoasc_filter(AVBSFContext *bsfc, AVPacket *pkt)
{
AACBSFContext *ctx = bsfc->priv_data;
GetBitContext gb;
PutBitContext pb;
AACADTSHeaderInfo hdr;
int ret;
@ -55,9 +54,7 @@ static int aac_adtstoasc_filter(AVBSFContext *bsfc, AVPacket *pkt)
if (pkt->size < AV_AAC_ADTS_HEADER_SIZE)
goto packet_too_small;
init_get_bits(&gb, pkt->data, AV_AAC_ADTS_HEADER_SIZE * 8);
if (ff_adts_header_parse(&gb, &hdr) < 0) {
if (ff_adts_header_parse_buf(pkt->data, &hdr) < 0) {
av_log(bsfc, AV_LOG_ERROR, "Error parsing ADTS frame header!\n");
ret = AVERROR_INVALIDDATA;
goto fail;
@ -81,6 +78,7 @@ static int aac_adtstoasc_filter(AVBSFContext *bsfc, AVPacket *pkt)
uint8_t *extradata;
if (!hdr.chan_config) {
GetBitContext gb;
init_get_bits(&gb, pkt->data, pkt->size * 8);
if (get_bits(&gb, 3) != 5) {
avpriv_report_missing_feature(bsfc,

View File

@ -25,7 +25,6 @@
*/
#include "parser.h"
#include "get_bits.h"
#include "adts_header.h"
#include "adts_parser.h"
#include "mpeg4audio.h"
@ -45,7 +44,6 @@ static int ftr_parse(AVCodecParserContext *s, AVCodecContext *avctx,
FTRParseContext *ftr = s->priv_data;
uint64_t state = ftr->pc.state64;
int next = END_NOT_FOUND;
GetBitContext bits;
AACADTSHeaderInfo hdr;
int size;
@ -71,10 +69,9 @@ static int ftr_parse(AVCodecParserContext *s, AVCodecContext *avctx,
state = (state << 8) | buf[i];
AV_WB64(tmp, state);
init_get_bits(&bits, tmp + 8 - AV_AAC_ADTS_HEADER_SIZE,
AV_AAC_ADTS_HEADER_SIZE * 8);
size = ff_adts_header_parse_buf(tmp + 8 - AV_AAC_ADTS_HEADER_SIZE, &hdr);
if ((size = ff_adts_header_parse(&bits, &hdr)) > 0) {
if (size > 0) {
ftr->skip = size - 6;
ftr->frame_index += ff_mpeg4audio_channels[hdr.chan_config];
if (ftr->frame_index >= avctx->ch_layout.nb_channels) {