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.
Showing the changes to libavcodec (6 of 6 files). Show all
Unified · Split
| @@ -24,24 +24,18 @@ · 23 unmodified lines | |||
| 24 | #include "aac_ac3_parser.h" | 24 | #include "aac_ac3_parser.h" |
| 25 | #include "adts_header.h" | 25 | #include "adts_header.h" |
| 26 | #include "adts_parser.h" | 26 | #include "adts_parser.h" |
| 27 | #include "get_bits.h" | 27 | #include "libavutil/intreadwrite.h" |
| 28 | #include "mpeg4audio.h" | ||
| 29 | 28 | ||
| 30 | static int aac_sync(uint64_t state, int *need_next_header, int *new_frame_start) | 29 | static int aac_sync(uint64_t state, int *need_next_header, int *new_frame_start) |
| 31 | { | 30 | { |
| 32 | GetBitContext bits; | 31 | uint8_t tmp[8 + AV_INPUT_BUFFER_PADDING_SIZE]; |
| 33 | AACADTSHeaderInfo hdr; | 32 | AACADTSHeaderInfo hdr; |
| 34 | int size; | 33 | int size; |
| 35 | union { | ||
| 36 | uint64_t u64; | ||
| 37 | uint8_t u8[8 + AV_INPUT_BUFFER_PADDING_SIZE]; | ||
| 38 | } tmp; | ||
| 39 | 34 | ||
| 40 | tmp.u64 = av_be2ne64(state); | 35 | AV_WB64(tmp, state); |
| 41 | init_get_bits(&bits, tmp.u8 + 8 - AV_AAC_ADTS_HEADER_SIZE, | ||
| 42 | AV_AAC_ADTS_HEADER_SIZE * 8); | ||
| 43 | 36 | ||
| 44 | if ((size = ff_adts_header_parse(&bits, &hdr)) < 0) | 37 | size = ff_adts_header_parse_buf(tmp + 8 - AV_AAC_ADTS_HEADER_SIZE, &hdr); |
| 38 | if (size < 0) | ||
| 45 | return 0; | 39 | return 0; |
| 46 | *need_next_header = 0; | 40 | *need_next_header = 0; |
| 47 | *new_frame_start = 1; | 41 | *new_frame_start = 1; |
| @@ -25,6 +25,7 @@ · 24 unmodified lines | |||
| 25 | #include "adts_parser.h" | 25 | #include "adts_parser.h" |
| 26 | #include "get_bits.h" | 26 | #include "get_bits.h" |
| 27 | #include "mpeg4audio.h" | 27 | #include "mpeg4audio.h" |
| 28 | #include "libavutil/avassert.h" | ||
| 28 | 29 | ||
| 29 | int ff_adts_header_parse(GetBitContext *gbc, AACADTSHeaderInfo *hdr) | 30 | int ff_adts_header_parse(GetBitContext *gbc, AACADTSHeaderInfo *hdr) |
| 30 | { | 31 | { |
| @@ -71,3 +72,12 @@ int ff_adts_header_parse(GetBitContext *gbc, AACADTSHeaderInfo *hdr) · 40 unmodified lines | |||
| 71 | 72 | ||
| 72 | return size; | 73 | return size; |
| 73 | } | 74 | } |
| 75 | |||
| 76 | int ff_adts_header_parse_buf(const uint8_t buf[AV_AAC_ADTS_HEADER_SIZE + AV_INPUT_BUFFER_PADDING_SIZE], | ||
| 77 | AACADTSHeaderInfo *hdr) | ||
| 78 | { | ||
| 79 | GetBitContext gb; | ||
| 80 | av_unused int ret = init_get_bits8(&gb, buf, AV_AAC_ADTS_HEADER_SIZE); | ||
| 81 | av_assert1(ret >= 0); | ||
| 82 | return ff_adts_header_parse(&gb, hdr); | ||
| 83 | } | ||
| @@ -23,7 +23,8 @@ · 22 unmodified lines | |||
| 23 | #ifndef AVCODEC_ADTS_HEADER_H | 23 | #ifndef AVCODEC_ADTS_HEADER_H |
| 24 | #define AVCODEC_ADTS_HEADER_H | 24 | #define AVCODEC_ADTS_HEADER_H |
| 25 | 25 | ||
| 26 | #include "get_bits.h" | 26 | #include "adts_parser.h" |
| 27 | #include "defs.h" | ||
| 27 | 28 | ||
| 28 | typedef enum { | 29 | typedef enum { |
| 29 | AAC_PARSE_ERROR_SYNC = -0x1030c0a, | 30 | AAC_PARSE_ERROR_SYNC = -0x1030c0a, |
| @@ -43,6 +44,8 @@ typedef struct AACADTSHeaderInfo { · 13 unmodified lines | |||
| 43 | uint32_t frame_length; | 44 | uint32_t frame_length; |
| 44 | } AACADTSHeaderInfo; | 45 | } AACADTSHeaderInfo; |
| 45 | 46 | ||
| 47 | struct GetBitContext; | ||
| 48 | |||
| 46 | /** | 49 | /** |
| 47 | * Parse the ADTS frame header to the end of the variable header, which is | 50 | * Parse the ADTS frame header to the end of the variable header, which is |
| 48 | * the first 54 bits. | 51 | * the first 54 bits. |
| @@ -51,7 +54,14 @@ typedef struct AACADTSHeaderInfo { · 2 unmodified lines | |||
| 51 | * @return the size in bytes of the header parsed on success and | 54 | * @return the size in bytes of the header parsed on success and |
| 52 | * AAC_PARSE_ERROR_* values otherwise. | 55 | * AAC_PARSE_ERROR_* values otherwise. |
| 53 | */ | 56 | */ |
| 54 | int ff_adts_header_parse(GetBitContext *gbc, AACADTSHeaderInfo *hdr); | 57 | int ff_adts_header_parse(struct GetBitContext *gbc, AACADTSHeaderInfo *hdr); |
| 58 | |||
| 59 | /** | ||
| 60 | * Wrapper around ff_adts_header_parse() for users that don't already have | ||
| 61 | * a suitable GetBitContext. | ||
| 62 | */ | ||
| 63 | int ff_adts_header_parse_buf(const uint8_t buf[AV_AAC_ADTS_HEADER_SIZE + AV_INPUT_BUFFER_PADDING_SIZE], | ||
| 64 | AACADTSHeaderInfo *hdr); | ||
| 55 | 65 | ||
| 56 | /** | 66 | /** |
| 57 | * Parse the ADTS frame header contained in the buffer, which is | 67 | * Parse the ADTS frame header contained in the buffer, which is |
| @@ -20,7 +20,9 @@ · 19 unmodified lines | |||
| 20 | 20 | ||
| 21 | #include <stddef.h> | 21 | #include <stddef.h> |
| 22 | #include <stdint.h> | 22 | #include <stdint.h> |
| 23 | #include <string.h> | ||
| 23 | 24 | ||
| 25 | #include "libavutil/error.h" | ||
| 24 | #include "libavutil/mem.h" | 26 | #include "libavutil/mem.h" |
| 25 | #include "adts_header.h" | 27 | #include "adts_header.h" |
| 26 | #include "adts_parser.h" | 28 | #include "adts_parser.h" |
| @@ -29,16 +31,12 @@ int av_adts_header_parse(const uint8_t *buf, uint32_t *samples, uint8_t *frames) · 2 unmodified lines | |||
| 29 | { | 31 | { |
| 30 | #if CONFIG_ADTS_HEADER | 32 | #if CONFIG_ADTS_HEADER |
| 31 | uint8_t tmpbuf[AV_AAC_ADTS_HEADER_SIZE + AV_INPUT_BUFFER_PADDING_SIZE]; | 33 | uint8_t tmpbuf[AV_AAC_ADTS_HEADER_SIZE + AV_INPUT_BUFFER_PADDING_SIZE]; |
| 32 | GetBitContext gb; | ||
| 33 | AACADTSHeaderInfo hdr; | 34 | AACADTSHeaderInfo hdr; |
| 34 | int err; | 35 | int err; |
| 35 | if (!buf) | 36 | if (!buf) |
| 36 | return AVERROR(EINVAL); | 37 | return AVERROR(EINVAL); |
| 37 | memcpy(tmpbuf, buf, AV_AAC_ADTS_HEADER_SIZE); | 38 | memcpy(tmpbuf, buf, AV_AAC_ADTS_HEADER_SIZE); |
| 38 | err = init_get_bits8(&gb, tmpbuf, AV_AAC_ADTS_HEADER_SIZE); | 39 | err = ff_adts_header_parse_buf(tmpbuf, &hdr); |
| 39 | if (err < 0) | ||
| 40 | return err; | ||
| 41 | err = ff_adts_header_parse(&gb, &hdr); | ||
| 42 | if (err < 0) | 40 | if (err < 0) |
| 43 | return err; | 41 | return err; |
| 44 | *samples = hdr.samples; | 42 | *samples = hdr.samples; |
| @@ -54,7 +52,6 @@ int avpriv_adts_header_parse(AACADTSHeaderInfo **phdr, const uint8_t *buf, size_ · 9 unmodified lines | |||
| 54 | #if CONFIG_ADTS_HEADER | 52 | #if CONFIG_ADTS_HEADER |
| 55 | int ret = 0; | 53 | int ret = 0; |
| 56 | int allocated = 0; | 54 | int allocated = 0; |
| 57 | GetBitContext gb; | ||
| 58 | 55 | ||
| 59 | if (!phdr || !buf || size < AV_AAC_ADTS_HEADER_SIZE) | 56 | if (!phdr || !buf || size < AV_AAC_ADTS_HEADER_SIZE) |
| 60 | return AVERROR_INVALIDDATA; | 57 | return AVERROR_INVALIDDATA; |
| @@ -66,14 +63,7 @@ int avpriv_adts_header_parse(AACADTSHeaderInfo **phdr, const uint8_t *buf, size_ · 5 unmodified lines | |||
| 66 | if (!*phdr) | 63 | if (!*phdr) |
| 67 | return AVERROR(ENOMEM); | 64 | return AVERROR(ENOMEM); |
| 68 | 65 | ||
| 69 | ret = init_get_bits8(&gb, buf, AV_AAC_ADTS_HEADER_SIZE); | 66 | ret = ff_adts_header_parse_buf(buf, *phdr); |
| 70 | if (ret < 0) { | ||
| 71 | if (allocated) | ||
| 72 | av_freep(phdr); | ||
| 73 | return ret; | ||
| 74 | } | ||
| 75 | |||
| 76 | ret = ff_adts_header_parse(&gb, *phdr); | ||
| 77 | if (ret < 0) { | 67 | if (ret < 0) { |
| 78 | if (allocated) | 68 | if (allocated) |
| 79 | av_freep(phdr); | 69 | av_freep(phdr); |
| @@ -40,7 +40,6 @@ static int aac_adtstoasc_filter(AVBSFContext *bsfc, AVPacket *pkt) · 39 unmodified lines | |||
| 40 | { | 40 | { |
| 41 | AACBSFContext *ctx = bsfc->priv_data; | 41 | AACBSFContext *ctx = bsfc->priv_data; |
| 42 | 42 | ||
| 43 | GetBitContext gb; | ||
| 44 | PutBitContext pb; | 43 | PutBitContext pb; |
| 45 | AACADTSHeaderInfo hdr; | 44 | AACADTSHeaderInfo hdr; |
| 46 | int ret; | 45 | int ret; |
| @@ -55,9 +54,7 @@ static int aac_adtstoasc_filter(AVBSFContext *bsfc, AVPacket *pkt) · 8 unmodified lines | |||
| 55 | if (pkt->size < AV_AAC_ADTS_HEADER_SIZE) | 54 | if (pkt->size < AV_AAC_ADTS_HEADER_SIZE) |
| 56 | goto packet_too_small; | 55 | goto packet_too_small; |
| 57 | 56 | ||
| 58 | init_get_bits(&gb, pkt->data, AV_AAC_ADTS_HEADER_SIZE * 8); | 57 | if (ff_adts_header_parse_buf(pkt->data, &hdr) < 0) { |
| 59 | |||
| 60 | if (ff_adts_header_parse(&gb, &hdr) < 0) { | ||
| 61 | av_log(bsfc, AV_LOG_ERROR, "Error parsing ADTS frame header!\n"); | 58 | av_log(bsfc, AV_LOG_ERROR, "Error parsing ADTS frame header!\n"); |
| 62 | ret = AVERROR_INVALIDDATA; | 59 | ret = AVERROR_INVALIDDATA; |
| 63 | goto fail; | 60 | goto fail; |
| @@ -81,6 +78,7 @@ static int aac_adtstoasc_filter(AVBSFContext *bsfc, AVPacket *pkt) · 17 unmodified lines | |||
| 81 | uint8_t *extradata; | 78 | uint8_t *extradata; |
| 82 | 79 | ||
| 83 | if (!hdr.chan_config) { | 80 | if (!hdr.chan_config) { |
| 81 | GetBitContext gb; | ||
| 84 | init_get_bits(&gb, pkt->data, pkt->size * 8); | 82 | init_get_bits(&gb, pkt->data, pkt->size * 8); |
| 85 | if (get_bits(&gb, 3) != 5) { | 83 | if (get_bits(&gb, 3) != 5) { |
| 86 | avpriv_report_missing_feature(bsfc, | 84 | avpriv_report_missing_feature(bsfc, |
| @@ -25,7 +25,6 @@ · 24 unmodified lines | |||
| 25 | */ | 25 | */ |
| 26 | 26 | ||
| 27 | #include "parser.h" | 27 | #include "parser.h" |
| 28 | #include "get_bits.h" | ||
| 29 | #include "adts_header.h" | 28 | #include "adts_header.h" |
| 30 | #include "adts_parser.h" | 29 | #include "adts_parser.h" |
| 31 | #include "mpeg4audio.h" | 30 | #include "mpeg4audio.h" |
| @@ -45,7 +44,6 @@ static int ftr_parse(AVCodecParserContext *s, AVCodecContext *avctx, · 13 unmodified lines | |||
| 45 | FTRParseContext *ftr = s->priv_data; | 44 | FTRParseContext *ftr = s->priv_data; |
| 46 | uint64_t state = ftr->pc.state64; | 45 | uint64_t state = ftr->pc.state64; |
| 47 | int next = END_NOT_FOUND; | 46 | int next = END_NOT_FOUND; |
| 48 | GetBitContext bits; | ||
| 49 | AACADTSHeaderInfo hdr; | 47 | AACADTSHeaderInfo hdr; |
| 50 | int size; | 48 | int size; |
| 51 | 49 | ||
| @@ -71,10 +69,9 @@ static int ftr_parse(AVCodecParserContext *s, AVCodecContext *avctx, · 19 unmodified lines | |||
| 71 | 69 | ||
| 72 | state = (state << 8) | buf[i]; | 70 | state = (state << 8) | buf[i]; |
| 73 | AV_WB64(tmp, state); | 71 | AV_WB64(tmp, state); |
| 74 | init_get_bits(&bits, tmp + 8 - AV_AAC_ADTS_HEADER_SIZE, | 72 | size = ff_adts_header_parse_buf(tmp + 8 - AV_AAC_ADTS_HEADER_SIZE, &hdr); |
| 75 | AV_AAC_ADTS_HEADER_SIZE * 8); | ||
| 76 | 73 | ||
| 77 | if ((size = ff_adts_header_parse(&bits, &hdr)) > 0) { | 74 | if (size > 0) { |
| 78 | ftr->skip = size - 6; | 75 | ftr->skip = size - 6; |
| 79 | ftr->frame_index += ff_mpeg4audio_channels[hdr.chan_config]; | 76 | ftr->frame_index += ff_mpeg4audio_channels[hdr.chan_config]; |
| 80 | if (ftr->frame_index >= avctx->ch_layout.nb_channels) { | 77 | if (ftr->frame_index >= avctx->ch_layout.nb_channels) { |