From: Scott Theisen <scott.the.elm@gmail.com> To: ffmpeg-devel@ffmpeg.org Cc: Scott Theisen <scott.the.elm@gmail.com> Subject: [FFmpeg-devel] [PATCH v2] lavf/mpeg.c: improve readability of packet identification logic Date: Sun, 15 May 2022 16:18:44 -0400 Message-ID: <20220515201844.370158-1-scott.the.elm@gmail.com> (raw) In-Reply-To: <20220213224237.178797-1-scott.the.elm@gmail.com> switch-case over es_type and then perform a linear search over startcode. --- This version doesn't use stdbool.h but is otherwise identical. libavformat/mpeg.c | 213 +++++++++++++++++++++++++++------------------ 1 file changed, 130 insertions(+), 83 deletions(-) diff --git a/libavformat/mpeg.c b/libavformat/mpeg.c index 864b08d8f8..dfa6852453 100644 --- a/libavformat/mpeg.c +++ b/libavformat/mpeg.c @@ -483,6 +483,7 @@ static int mpegps_read_packet(AVFormatContext *s, AVStream *st; FFStream *sti; int len, startcode, i, es_type, ret; + int identified = 0; int pcm_dvd = 0; int request_probe= 0; enum AVCodecID codec_id = AV_CODEC_ID_NONE; @@ -523,92 +524,138 @@ redo: goto found; } + // identify packet encoding + identified = 1; es_type = m->psm_es_type[startcode & 0xff]; - if (es_type == STREAM_TYPE_VIDEO_MPEG1) { - codec_id = AV_CODEC_ID_MPEG2VIDEO; - type = AVMEDIA_TYPE_VIDEO; - } else if (es_type == STREAM_TYPE_VIDEO_MPEG2) { - codec_id = AV_CODEC_ID_MPEG2VIDEO; - type = AVMEDIA_TYPE_VIDEO; - } else if (es_type == STREAM_TYPE_AUDIO_MPEG1 || - es_type == STREAM_TYPE_AUDIO_MPEG2) { - codec_id = AV_CODEC_ID_MP3; - type = AVMEDIA_TYPE_AUDIO; - } else if (es_type == STREAM_TYPE_AUDIO_AAC) { - codec_id = AV_CODEC_ID_AAC; - type = AVMEDIA_TYPE_AUDIO; - } else if (es_type == STREAM_TYPE_VIDEO_MPEG4) { - codec_id = AV_CODEC_ID_MPEG4; - type = AVMEDIA_TYPE_VIDEO; - } else if (es_type == STREAM_TYPE_VIDEO_H264) { - codec_id = AV_CODEC_ID_H264; - type = AVMEDIA_TYPE_VIDEO; - } else if (es_type == STREAM_TYPE_VIDEO_HEVC) { - codec_id = AV_CODEC_ID_HEVC; - type = AVMEDIA_TYPE_VIDEO; - } else if (es_type == STREAM_TYPE_AUDIO_AC3) { - codec_id = AV_CODEC_ID_AC3; - type = AVMEDIA_TYPE_AUDIO; - } else if (m->imkh_cctv && es_type == 0x91) { - codec_id = AV_CODEC_ID_PCM_MULAW; - type = AVMEDIA_TYPE_AUDIO; - } else if (startcode >= 0x1e0 && startcode <= 0x1ef) { - static const unsigned char avs_seqh[4] = { 0, 0, 1, 0xb0 }; - unsigned char buf[8]; - - avio_read(s->pb, buf, 8); - avio_seek(s->pb, -8, SEEK_CUR); - if (!memcmp(buf, avs_seqh, 4) && (buf[6] != 0 || buf[7] != 1)) - codec_id = AV_CODEC_ID_CAVS; - else - request_probe= 1; - type = AVMEDIA_TYPE_VIDEO; - } else if (startcode == PRIVATE_STREAM_2) { - type = AVMEDIA_TYPE_DATA; - codec_id = AV_CODEC_ID_DVD_NAV; - } else if (startcode >= 0x1c0 && startcode <= 0x1df) { - type = AVMEDIA_TYPE_AUDIO; - if (m->sofdec > 0) { - codec_id = AV_CODEC_ID_ADPCM_ADX; - // Auto-detect AC-3 - request_probe = 50; - } else if (m->imkh_cctv && startcode == 0x1c0 && len > 80) { - codec_id = AV_CODEC_ID_PCM_ALAW; - request_probe = 50; - } else { - codec_id = AV_CODEC_ID_MP2; - if (m->imkh_cctv) - request_probe = 25; + switch (es_type) { + case STREAM_TYPE_VIDEO_MPEG1: // 0x01 + case STREAM_TYPE_VIDEO_MPEG2: // 0x02 + codec_id = AV_CODEC_ID_MPEG2VIDEO; + type = AVMEDIA_TYPE_VIDEO; + break; + case STREAM_TYPE_VIDEO_MPEG4: // 0x10 + codec_id = AV_CODEC_ID_MPEG4; + type = AVMEDIA_TYPE_VIDEO; + break; + case STREAM_TYPE_VIDEO_H264: // 0x1B + codec_id = AV_CODEC_ID_H264; + type = AVMEDIA_TYPE_VIDEO; + break; + case STREAM_TYPE_VIDEO_HEVC: // 0x24 + codec_id = AV_CODEC_ID_HEVC; + type = AVMEDIA_TYPE_VIDEO; + break; + case STREAM_TYPE_AUDIO_MPEG1: // 0x03 + case STREAM_TYPE_AUDIO_MPEG2: // 0x04 + codec_id = AV_CODEC_ID_MP3; + type = AVMEDIA_TYPE_AUDIO; + break; + case STREAM_TYPE_AUDIO_AAC: // 0x0F + codec_id = AV_CODEC_ID_AAC; + type = AVMEDIA_TYPE_AUDIO; + break; + case STREAM_TYPE_AUDIO_AC3: // 0x81 + codec_id = AV_CODEC_ID_AC3; + type = AVMEDIA_TYPE_AUDIO; + break; + default: + if (m->imkh_cctv && es_type == 0x91) { + codec_id = AV_CODEC_ID_PCM_MULAW; + type = AVMEDIA_TYPE_AUDIO; + break; + } + identified = 0; + break; + } + if (!identified) { + identified = 1; + if (startcode < 0x20) { + identified = 0; } - } else if (startcode >= 0x80 && startcode <= 0x87) { - type = AVMEDIA_TYPE_AUDIO; - codec_id = AV_CODEC_ID_AC3; - } else if ((startcode >= 0x88 && startcode <= 0x8f) || - (startcode >= 0x98 && startcode <= 0x9f)) { - /* 0x90 - 0x97 is reserved for SDDS in DVD specs */ - type = AVMEDIA_TYPE_AUDIO; - codec_id = AV_CODEC_ID_DTS; - } else if (startcode >= 0xa0 && startcode <= 0xaf) { - type = AVMEDIA_TYPE_AUDIO; - if (!pcm_dvd) { - codec_id = AV_CODEC_ID_MLP; - } else { - codec_id = AV_CODEC_ID_PCM_DVD; + else if (startcode <= 0x3F) { // 0x20 to 0x3F + type = AVMEDIA_TYPE_SUBTITLE; + codec_id = AV_CODEC_ID_DVD_SUBTITLE; } - } else if (startcode >= 0xb0 && startcode <= 0xbf) { - type = AVMEDIA_TYPE_AUDIO; - codec_id = AV_CODEC_ID_TRUEHD; - } else if (startcode >= 0xc0 && startcode <= 0xcf) { - /* Used for both AC-3 and E-AC-3 in EVOB files */ - type = AVMEDIA_TYPE_AUDIO; - codec_id = AV_CODEC_ID_AC3; - } else if (startcode >= 0x20 && startcode <= 0x3f) { - type = AVMEDIA_TYPE_SUBTITLE; - codec_id = AV_CODEC_ID_DVD_SUBTITLE; - } else if (startcode >= 0xfd55 && startcode <= 0xfd5f) { - type = AVMEDIA_TYPE_VIDEO; - codec_id = AV_CODEC_ID_VC1; - } else { + else if (startcode < 0x80) { // 0x40 to 0x7F + identified = 0; + } + else if (startcode <= 0x87) { // 0x80 to 0x87 + type = AVMEDIA_TYPE_AUDIO; + codec_id = AV_CODEC_ID_AC3; + } + else if (startcode <= 0x8F) { // 0x88 to 0x8F (to 0x9F excluding 0x90 - 0x97) + type = AVMEDIA_TYPE_AUDIO; + codec_id = AV_CODEC_ID_DTS; + } + else if (startcode <= 0x97) { // exclude 0x90 to 0x97 + identified = 0; // 0x90 - 0x97 is reserved for SDDS in DVD specs + } + else if (startcode <= 0x9F) { // 0x98 to 0x9F (from 0x88 excluding 0x90 - 0x97) + type = AVMEDIA_TYPE_AUDIO; + codec_id = AV_CODEC_ID_DTS; + } + else if (startcode <= 0xAF) { // 0xA0 to 0xAF + type = AVMEDIA_TYPE_AUDIO; + codec_id = (!pcm_dvd) ? AV_CODEC_ID_MLP : AV_CODEC_ID_PCM_DVD; + } + else if (startcode <= 0xBF) { // 0xB0 to 0xBF + type = AVMEDIA_TYPE_AUDIO; + codec_id = AV_CODEC_ID_TRUEHD; + } + else if (startcode <= 0xCF) { // 0xC0 to 0xCF + /* Used for both AC-3 and E-AC-3 in EVOB files */ + type = AVMEDIA_TYPE_AUDIO; + codec_id = AV_CODEC_ID_AC3; + } + else if (startcode < 0x1BF) { // 0xD0 to 0x1BE + identified = 0; + } + else if (startcode == PRIVATE_STREAM_2) { // 0x1BF + type = AVMEDIA_TYPE_DATA; + codec_id = AV_CODEC_ID_DVD_NAV; + } + else if (startcode <= 0x1DF) { // 0x1C0 to 0x1DF + type = AVMEDIA_TYPE_AUDIO; + if (m->sofdec > 0) { + codec_id = AV_CODEC_ID_ADPCM_ADX; + // Auto-detect AC-3 + request_probe = 50; + } + else if (m->imkh_cctv && startcode == 0x1c0 && len > 80) { + codec_id = AV_CODEC_ID_PCM_ALAW; + request_probe = 50; + } + else { + codec_id = AV_CODEC_ID_MP2; + if (m->imkh_cctv) + request_probe = 25; + } + } + else if (startcode <= 0x1EF) { // 0x1E0 to 0x1EF + static const unsigned char avs_seqh[4] = { 0, 0, 1, 0xb0 }; + unsigned char buf[8]; + + avio_read(s->pb, buf, 8); + avio_seek(s->pb, -8, SEEK_CUR); + if (!memcmp(buf, avs_seqh, 4) && (buf[6] != 0 || buf[7] != 1)) + codec_id = AV_CODEC_ID_CAVS; + else + request_probe= 1; + type = AVMEDIA_TYPE_VIDEO; + } + else if (startcode < 0xFD55) { // 0x01F0 to 0xFD54 + identified = 0; + } + else if (startcode <= 0xFD5F) { // 0xFD55 to 0xFD5F + type = AVMEDIA_TYPE_VIDEO; + codec_id = AV_CODEC_ID_VC1; + } + else { + identified = 0; + } + } + + if (!identified) { skip: /* skip packet */ avio_skip(s->pb, len); -- 2.34.1 _______________________________________________ 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".
next prev parent reply other threads:[~2022-05-15 20:19 UTC|newest] Thread overview: 4+ messages / expand[flat|nested] mbox.gz Atom feed top 2022-02-13 22:42 [FFmpeg-devel] [PATCH] " Scott Theisen 2022-05-15 20:18 ` Scott Theisen [this message] 2022-09-16 18:33 ` [FFmpeg-devel] [PATCH v2] " Scott Theisen 2022-09-16 19:37 ` Michael Niedermayer
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=20220515201844.370158-1-scott.the.elm@gmail.com \ --to=scott.the.elm@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