History

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.

Andreas Rheinhardt committed 2 years ago

Commit 12ded9c1 parent ae937c46 files · +35 −36Browse files

Showing the changes to libavcodec (6 of 6 files). Show all

Unified · Split

6 files
  1. libavcodec/aac_parser.c +5 −11
  2. libavcodec/adts_header.c +10 −0
  3. libavcodec/adts_header.h +12 −2
  4. libavcodec/adts_parser.c +4 −14
  5. libavcodec/bsf/aac_adtstoasc.c +2 −4
  6. libavcodec/ftr_parser.c +2 −5
libavcodec/aac_parser.c+5−11
@@ -24,24 +24,18 @@ · 23 unmodified lines
2424 #include "aac_ac3_parser.h"
2525 #include "adts_header.h"
2626 #include "adts_parser.h"
27−#include "get_bits.h"
28−#include "mpeg4audio.h"
27+#include "libavutil/intreadwrite.h"
2928
3029 static int aac_sync(uint64_t state, int *need_next_header, int *new_frame_start)
3130 {
32− GetBitContext bits;
31+ uint8_t tmp[8 + AV_INPUT_BUFFER_PADDING_SIZE];
3332 AACADTSHeaderInfo hdr;
3433 int size;
35− union {
36− uint64_t u64;
37− uint8_t u8[8 + AV_INPUT_BUFFER_PADDING_SIZE];
38− } tmp;
3934
40− tmp.u64 = av_be2ne64(state);
41− init_get_bits(&bits, tmp.u8 + 8 - AV_AAC_ADTS_HEADER_SIZE,
42− AV_AAC_ADTS_HEADER_SIZE * 8);
35+ AV_WB64(tmp, state);
4336
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)
4539 return 0;
4640 *need_next_header = 0;
4741 *new_frame_start = 1;
libavcodec/adts_header.c+10−0
@@ -25,6 +25,7 @@ · 24 unmodified lines
2525 #include "adts_parser.h"
2626 #include "get_bits.h"
2727 #include "mpeg4audio.h"
28+#include "libavutil/avassert.h"
2829
2930 int ff_adts_header_parse(GetBitContext *gbc, AACADTSHeaderInfo *hdr)
3031 {
@@ -71,3 +72,12 @@ int ff_adts_header_parse(GetBitContext *gbc, AACADTSHeaderInfo *hdr) · 40 unmodified lines
7172
7273 return size;
7374 }
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+}
libavcodec/adts_header.h+12−2
@@ -23,7 +23,8 @@ · 22 unmodified lines
2323 #ifndef AVCODEC_ADTS_HEADER_H
2424 #define AVCODEC_ADTS_HEADER_H
2525
26−#include "get_bits.h"
26+#include "adts_parser.h"
27+#include "defs.h"
2728
2829 typedef enum {
2930 AAC_PARSE_ERROR_SYNC = -0x1030c0a,
@@ -43,6 +44,8 @@ typedef struct AACADTSHeaderInfo { · 13 unmodified lines
4344 uint32_t frame_length;
4445 } AACADTSHeaderInfo;
4546
47+struct GetBitContext;
48+
4649 /**
4750 * Parse the ADTS frame header to the end of the variable header, which is
4851 * the first 54 bits.
@@ -51,7 +54,14 @@ typedef struct AACADTSHeaderInfo { · 2 unmodified lines
5154 * @return the size in bytes of the header parsed on success and
5255 * AAC_PARSE_ERROR_* values otherwise.
5356 */
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);
5565
5666 /**
5767 * Parse the ADTS frame header contained in the buffer, which is
libavcodec/adts_parser.c+4−14
@@ -20,7 +20,9 @@ · 19 unmodified lines
2020
2121 #include <stddef.h>
2222 #include <stdint.h>
23+#include <string.h>
2324
25+#include "libavutil/error.h"
2426 #include "libavutil/mem.h"
2527 #include "adts_header.h"
2628 #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
2931 {
3032 #if CONFIG_ADTS_HEADER
3133 uint8_t tmpbuf[AV_AAC_ADTS_HEADER_SIZE + AV_INPUT_BUFFER_PADDING_SIZE];
32− GetBitContext gb;
3334 AACADTSHeaderInfo hdr;
3435 int err;
3536 if (!buf)
3637 return AVERROR(EINVAL);
3738 memcpy(tmpbuf, buf, AV_AAC_ADTS_HEADER_SIZE);
38− err = init_get_bits8(&gb, tmpbuf, AV_AAC_ADTS_HEADER_SIZE);
39− if (err < 0)
40− return err;
41− err = ff_adts_header_parse(&gb, &hdr);
39+ err = ff_adts_header_parse_buf(tmpbuf, &hdr);
4240 if (err < 0)
4341 return err;
4442 *samples = hdr.samples;
@@ -54,7 +52,6 @@ int avpriv_adts_header_parse(AACADTSHeaderInfo **phdr, const uint8_t *buf, size_ · 9 unmodified lines
5452 #if CONFIG_ADTS_HEADER
5553 int ret = 0;
5654 int allocated = 0;
57− GetBitContext gb;
5855
5956 if (!phdr || !buf || size < AV_AAC_ADTS_HEADER_SIZE)
6057 return AVERROR_INVALIDDATA;
@@ -66,14 +63,7 @@ int avpriv_adts_header_parse(AACADTSHeaderInfo **phdr, const uint8_t *buf, size_ · 5 unmodified lines
6663 if (!*phdr)
6764 return AVERROR(ENOMEM);
6865
69− ret = init_get_bits8(&gb, buf, AV_AAC_ADTS_HEADER_SIZE);
70− if (ret < 0) {
71− if (allocated)
72− av_freep(phdr);
73− return ret;
74− }
75−
76− ret = ff_adts_header_parse(&gb, *phdr);
66+ ret = ff_adts_header_parse_buf(buf, *phdr);
7767 if (ret < 0) {
7868 if (allocated)
7969 av_freep(phdr);
libavcodec/bsf/aac_adtstoasc.c+2−4
@@ -40,7 +40,6 @@ static int aac_adtstoasc_filter(AVBSFContext *bsfc, AVPacket *pkt) · 39 unmodified lines
4040 {
4141 AACBSFContext *ctx = bsfc->priv_data;
4242
43− GetBitContext gb;
4443 PutBitContext pb;
4544 AACADTSHeaderInfo hdr;
4645 int ret;
@@ -55,9 +54,7 @@ static int aac_adtstoasc_filter(AVBSFContext *bsfc, AVPacket *pkt) · 8 unmodified lines
5554 if (pkt->size < AV_AAC_ADTS_HEADER_SIZE)
5655 goto packet_too_small;
5756
58− init_get_bits(&gb, pkt->data, AV_AAC_ADTS_HEADER_SIZE * 8);
59−
60− if (ff_adts_header_parse(&gb, &hdr) < 0) {
57+ if (ff_adts_header_parse_buf(pkt->data, &hdr) < 0) {
6158 av_log(bsfc, AV_LOG_ERROR, "Error parsing ADTS frame header!\n");
6259 ret = AVERROR_INVALIDDATA;
6360 goto fail;
@@ -81,6 +78,7 @@ static int aac_adtstoasc_filter(AVBSFContext *bsfc, AVPacket *pkt) · 17 unmodified lines
8178 uint8_t *extradata;
8279
8380 if (!hdr.chan_config) {
81+ GetBitContext gb;
8482 init_get_bits(&gb, pkt->data, pkt->size * 8);
8583 if (get_bits(&gb, 3) != 5) {
8684 avpriv_report_missing_feature(bsfc,
libavcodec/ftr_parser.c+2−5
@@ -25,7 +25,6 @@ · 24 unmodified lines
2525 */
2626
2727 #include "parser.h"
28−#include "get_bits.h"
2928 #include "adts_header.h"
3029 #include "adts_parser.h"
3130 #include "mpeg4audio.h"
@@ -45,7 +44,6 @@ static int ftr_parse(AVCodecParserContext *s, AVCodecContext *avctx, · 13 unmodified lines
4544 FTRParseContext *ftr = s->priv_data;
4645 uint64_t state = ftr->pc.state64;
4746 int next = END_NOT_FOUND;
48− GetBitContext bits;
4947 AACADTSHeaderInfo hdr;
5048 int size;
5149
@@ -71,10 +69,9 @@ static int ftr_parse(AVCodecParserContext *s, AVCodecContext *avctx, · 19 unmodified lines
7169
7270 state = (state << 8) | buf[i];
7371 AV_WB64(tmp, state);
74− init_get_bits(&bits, tmp + 8 - AV_AAC_ADTS_HEADER_SIZE,
75− AV_AAC_ADTS_HEADER_SIZE * 8);
72+ size = ff_adts_header_parse_buf(tmp + 8 - AV_AAC_ADTS_HEADER_SIZE, &hdr);
7673
77− if ((size = ff_adts_header_parse(&bits, &hdr)) > 0) {
74+ if (size > 0) {
7875 ftr->skip = size - 6;
7976 ftr->frame_index += ff_mpeg4audio_channels[hdr.chan_config];
8077 if (ftr->frame_index >= avctx->ch_layout.nb_channels) {

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t