Git Inbox Mirror of the ffmpeg-devel mailing list - see https://ffmpeg.org/mailman/listinfo/ffmpeg-devel
 help / color / mirror / Atom feed
From: James Almer <jamrial@gmail.com>
To: ffmpeg-devel@ffmpeg.org
Subject: [FFmpeg-devel] [PATCH 2/5] avcodec/aac_ac3_parser: don't fill stream info in the sync function
Date: Sat, 22 Oct 2022 18:02:23 -0300
Message-ID: <20221022210226.2200-2-jamrial@gmail.com> (raw)
In-Reply-To: <20221022210226.2200-1-jamrial@gmail.com>

Have it only find frame boundaries. The stream props will then be filled once
we have an assembled frame.

Signed-off-by: James Almer <jamrial@gmail.com>
---
 libavcodec/aac_ac3_parser.c | 63 +++++++++++++++++++++++++++++--------
 libavcodec/aac_ac3_parser.h | 12 +------
 libavcodec/aac_parser.c     |  7 +----
 libavcodec/ac3_parser.c     | 16 +---------
 4 files changed, 53 insertions(+), 45 deletions(-)

diff --git a/libavcodec/aac_ac3_parser.c b/libavcodec/aac_ac3_parser.c
index b14b1e31f9..6df064e28d 100644
--- a/libavcodec/aac_ac3_parser.c
+++ b/libavcodec/aac_ac3_parser.c
@@ -26,6 +26,8 @@
 #include "libavutil/common.h"
 #include "parser.h"
 #include "aac_ac3_parser.h"
+#include "ac3_parser_internal.h"
+#include "adts_header.h"
 
 int ff_aac_ac3_parse(AVCodecParserContext *s1,
                      AVCodecContext *avctx,
@@ -48,7 +50,7 @@ get_next:
             len=0;
             for(i=s->remaining_size; i<buf_size; i++){
                 s->state = (s->state<<8) + buf[i];
-                if((len=s->sync(s->state, s, &s->need_next_header, &new_frame_start)))
+                if((len=s->sync(s->state, &s->need_next_header, &new_frame_start)))
                     break;
             }
             if(len<=0){
@@ -79,42 +81,77 @@ get_next:
     *poutbuf = buf;
     *poutbuf_size = buf_size;
 
-    /* update codec info */
-    if(s->codec_id)
-        avctx->codec_id = s->codec_id;
-
     if (got_frame) {
+        int bit_rate;
+
         /* Due to backwards compatible HE-AAC the sample rate, channel count,
            and total number of samples found in an AAC ADTS header are not
            reliable. Bit rate is still accurate because the total frame
            duration in seconds is still correct (as is the number of bits in
            the frame). */
         if (avctx->codec_id != AV_CODEC_ID_AAC) {
-            avctx->sample_rate = s->sample_rate;
+            AC3HeaderInfo hdr, *phrd = &hdr;
+            int offset = ff_ac3_find_syncword(buf, buf_size);
+
+            if (offset < 0)
+                return i;
+
+            buf += offset;
+            buf_size -= offset;
+            while (buf_size > 0) {
+                int ret = avpriv_ac3_parse_header(&phrd, buf, buf_size);
+
+                if (ret < 0 || hdr.frame_size > buf_size)
+                    return i;
+
+                if (buf_size > hdr.frame_size) {
+                    buf += hdr.frame_size;
+                    buf_size -= hdr.frame_size;
+                    continue;
+                }
+                break;
+            }
+
+            avctx->sample_rate = hdr.sample_rate;
+
+            if (hdr.bitstream_id > 10)
+                avctx->codec_id = AV_CODEC_ID_EAC3;
+
             if (!CONFIG_EAC3_DECODER || avctx->codec_id != AV_CODEC_ID_EAC3) {
                 av_channel_layout_uninit(&avctx->ch_layout);
-                if (s->channel_layout) {
-                    av_channel_layout_from_mask(&avctx->ch_layout, s->channel_layout);
+                if (hdr.channel_layout) {
+                    av_channel_layout_from_mask(&avctx->ch_layout, hdr.channel_layout);
                 } else {
                     avctx->ch_layout.order       = AV_CHANNEL_ORDER_UNSPEC;
-                    avctx->ch_layout.nb_channels = s->channels;
+                    avctx->ch_layout.nb_channels = hdr.channels;
                 }
 #if FF_API_OLD_CHANNEL_LAYOUT
 FF_DISABLE_DEPRECATION_WARNINGS
                 avctx->channels = avctx->ch_layout.nb_channels;
-                avctx->channel_layout = s->channel_layout;
+                avctx->channel_layout = hdr.channel_layout;
 FF_ENABLE_DEPRECATION_WARNINGS
 #endif
             }
-            s1->duration = s->samples;
-            avctx->audio_service_type = s->service_type;
+            s1->duration = hdr.num_blocks * 256;
+            avctx->audio_service_type = hdr.bitstream_mode;
+            if (hdr.bitstream_mode == 0x7 && hdr.channels > 1)
+                avctx->audio_service_type = AV_AUDIO_SERVICE_TYPE_KARAOKE;
+            bit_rate = hdr.bit_rate;
+        } else {
+            AACADTSHeaderInfo hdr, *phrd = &hdr;
+            int ret = avpriv_adts_header_parse(&phrd, buf, buf_size);
+
+            if (ret < 0)
+                return i;
+
+            bit_rate = hdr.bit_rate;
         }
 
         /* Calculate the average bit rate */
         s->frame_number++;
         if (!CONFIG_EAC3_DECODER || avctx->codec_id != AV_CODEC_ID_EAC3) {
             avctx->bit_rate +=
-                (s->bit_rate - avctx->bit_rate) / s->frame_number;
+                (bit_rate - avctx->bit_rate) / s->frame_number;
         }
     }
 
diff --git a/libavcodec/aac_ac3_parser.h b/libavcodec/aac_ac3_parser.h
index 8b93cbf84f..560bba54f5 100644
--- a/libavcodec/aac_ac3_parser.h
+++ b/libavcodec/aac_ac3_parser.h
@@ -39,24 +39,14 @@ typedef enum {
 
 typedef struct AACAC3ParseContext {
     ParseContext pc;
-    int frame_size;
     int header_size;
-    int (*sync)(uint64_t state, struct AACAC3ParseContext *hdr_info,
-            int *need_next_header, int *new_frame_start);
-
-    int channels;
-    int sample_rate;
-    int bit_rate;
-    int samples;
-    uint64_t channel_layout;
-    int service_type;
+    int (*sync)(uint64_t state, int *need_next_header, int *new_frame_start);
 
     int remaining_size;
     uint64_t state;
 
     int need_next_header;
     int frame_number;
-    enum AVCodecID codec_id;
 } AACAC3ParseContext;
 
 int ff_aac_ac3_parse(AVCodecParserContext *s1,
diff --git a/libavcodec/aac_parser.c b/libavcodec/aac_parser.c
index f3baf7cde3..f295dfccdd 100644
--- a/libavcodec/aac_parser.c
+++ b/libavcodec/aac_parser.c
@@ -27,8 +27,7 @@
 #include "get_bits.h"
 #include "mpeg4audio.h"
 
-static int aac_sync(uint64_t state, AACAC3ParseContext *hdr_info,
-        int *need_next_header, int *new_frame_start)
+static int aac_sync(uint64_t state, int *need_next_header, int *new_frame_start)
 {
     GetBitContext bits;
     AACADTSHeaderInfo hdr;
@@ -46,10 +45,6 @@ static int aac_sync(uint64_t state, AACAC3ParseContext *hdr_info,
         return 0;
     *need_next_header = 0;
     *new_frame_start  = 1;
-    hdr_info->sample_rate = hdr.sample_rate;
-    hdr_info->channels    = ff_mpeg4audio_channels[hdr.chan_config];
-    hdr_info->samples     = hdr.samples;
-    hdr_info->bit_rate    = hdr.bit_rate;
     return size;
 }
 
diff --git a/libavcodec/ac3_parser.c b/libavcodec/ac3_parser.c
index 425e1b4742..8885e1c72e 100644
--- a/libavcodec/ac3_parser.c
+++ b/libavcodec/ac3_parser.c
@@ -215,8 +215,7 @@ int av_ac3_parse_header(const uint8_t *buf, size_t size,
     return 0;
 }
 
-static int ac3_sync(uint64_t state, AACAC3ParseContext *hdr_info,
-        int *need_next_header, int *new_frame_start)
+static int ac3_sync(uint64_t state, int *need_next_header, int *new_frame_start)
 {
     int err;
     union {
@@ -238,19 +237,6 @@ static int ac3_sync(uint64_t state, AACAC3ParseContext *hdr_info,
     if(err < 0)
         return 0;
 
-    hdr_info->sample_rate = hdr.sample_rate;
-    hdr_info->bit_rate = hdr.bit_rate;
-    hdr_info->channels = hdr.channels;
-    hdr_info->channel_layout = hdr.channel_layout;
-    hdr_info->samples = hdr.num_blocks * 256;
-    hdr_info->service_type = hdr.bitstream_mode;
-    if (hdr.bitstream_mode == 0x7 && hdr.channels > 1)
-        hdr_info->service_type = AV_AUDIO_SERVICE_TYPE_KARAOKE;
-    if(hdr.bitstream_id>10)
-        hdr_info->codec_id = AV_CODEC_ID_EAC3;
-    else if (hdr_info->codec_id == AV_CODEC_ID_NONE)
-        hdr_info->codec_id = AV_CODEC_ID_AC3;
-
     *new_frame_start  = (hdr.frame_type != EAC3_FRAME_TYPE_DEPENDENT);
     *need_next_header = *new_frame_start || (hdr.frame_type != EAC3_FRAME_TYPE_AC3_CONVERT);
     return hdr.frame_size;
-- 
2.37.3

_______________________________________________
ffmpeg-devel mailing list
ffmpeg-devel@ffmpeg.org
https://ffmpeg.org/mailman/listinfo/ffmpeg-devel

To unsubscribe, visit link above, or email
ffmpeg-devel-request@ffmpeg.org with subject "unsubscribe".

  reply	other threads:[~2022-10-22 21:03 UTC|newest]

Thread overview: 12+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-10-22 21:02 [FFmpeg-devel] [PATCH 1/5 v2] avcodec/ac3dec: split off code discarding garbage at the beginning of a packet James Almer
2022-10-22 21:02 ` James Almer [this message]
2022-10-23 15:35   ` [FFmpeg-devel] [PATCH 2/5] avcodec/aac_ac3_parser: don't fill stream info in the sync function Michael Niedermayer
2022-10-23 17:34     ` Andreas Rheinhardt
2022-10-23 22:59       ` James Almer
2022-10-22 21:02 ` [FFmpeg-devel] [PATCH 3/5] avcodec/aac_ac3_parser: don't try to sync when the parser is configured to handle complete frames James Almer
2022-10-24 21:49   ` Michael Niedermayer
2022-10-24 22:26     ` [FFmpeg-devel] [PATCH 4/6 v2] " James Almer
2022-10-22 21:02 ` [FFmpeg-devel] [PATCH 4/5] avcodec/aac_ac3_parser: reindent after previous commit James Almer
2022-10-22 21:02 ` [FFmpeg-devel] [PATCH 5/5 v2] avcodec/ac3_parser: improve false positive detection when parsing sync frames James Almer
2022-10-23 23:04 ` [FFmpeg-devel] [PATCH 2/6] avcodec/adts_parsed: allow passing a pre allocated AACADTSHeaderInfo to avpriv_adts_header_parse() James Almer
2022-10-26 12:09 ` [FFmpeg-devel] [PATCH 1/5 v2] avcodec/ac3dec: split off code discarding garbage at the beginning of a packet James Almer

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20221022210226.2200-2-jamrial@gmail.com \
    --to=jamrial@gmail.com \
    --cc=ffmpeg-devel@ffmpeg.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link

Git Inbox Mirror of the ffmpeg-devel mailing list - see https://ffmpeg.org/mailman/listinfo/ffmpeg-devel

This inbox may be cloned and mirrored by anyone:

	git clone --mirror https://master.gitmailbox.com/ffmpegdev/0 ffmpegdev/git/0.git

	# If you have public-inbox 1.1+ installed, you may
	# initialize and index your mirror using the following commands:
	public-inbox-init -V2 ffmpegdev ffmpegdev/ https://master.gitmailbox.com/ffmpegdev \
		ffmpegdev@gitmailbox.com
	public-inbox-index ffmpegdev

Example config snippet for mirrors.


AGPL code for this site: git clone https://public-inbox.org/public-inbox.git