* [FFmpeg-devel] [PATCH] lavf/mpeg.c: improve readability of packet identification logic
@ 2022-02-13 22:42 Scott Theisen
2022-05-15 20:18 ` [FFmpeg-devel] [PATCH v2] " Scott Theisen
0 siblings, 1 reply; 4+ messages in thread
From: Scott Theisen @ 2022-02-13 22:42 UTC (permalink / raw)
To: ffmpeg-devel; +Cc: Scott Theisen
switch-case over es_type and then perform a linear search over
startcode.
---
libavformat/mpeg.c | 215 ++++++++++++++++++++++++++++-----------------
1 file changed, 132 insertions(+), 83 deletions(-)
diff --git a/libavformat/mpeg.c b/libavformat/mpeg.c
index ca15d9f241..c1b1c7e7de 100644
--- a/libavformat/mpeg.c
+++ b/libavformat/mpeg.c
@@ -19,6 +19,8 @@
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
+#include <stdbool.h>
+
#include "libavutil/channel_layout.h"
#include "avformat.h"
#include "avio_internal.h"
@@ -480,6 +482,7 @@ static int mpegps_read_packet(AVFormatContext *s,
AVStream *st;
FFStream *sti;
int len, startcode, i, es_type, ret;
+ bool identified = false;
int pcm_dvd = 0;
int request_probe= 0;
enum AVCodecID codec_id = AV_CODEC_ID_NONE;
@@ -520,92 +523,138 @@ redo:
goto found;
}
+ // identify packet encoding
+ identified = true;
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 = false;
+ break;
+ }
+ if (!identified) {
+ identified = true;
+ if (startcode < 0x20) {
+ identified = false;
}
- } 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 = false;
+ }
+ 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 = false; // 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 = false;
+ }
+ 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 = false;
+ }
+ else if (startcode <= 0xFD5F) { // 0xFD55 to 0xFD5F
+ type = AVMEDIA_TYPE_VIDEO;
+ codec_id = AV_CODEC_ID_VC1;
+ }
+ else {
+ identified = false;
+ }
+ }
+
+ if (!identified) {
skip:
/* skip packet */
avio_skip(s->pb, len);
--
2.32.0
_______________________________________________
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".
^ permalink raw reply [flat|nested] 4+ messages in thread
* [FFmpeg-devel] [PATCH v2] lavf/mpeg.c: improve readability of packet identification logic
2022-02-13 22:42 [FFmpeg-devel] [PATCH] lavf/mpeg.c: improve readability of packet identification logic Scott Theisen
@ 2022-05-15 20:18 ` Scott Theisen
2022-09-16 18:33 ` Scott Theisen
0 siblings, 1 reply; 4+ messages in thread
From: Scott Theisen @ 2022-05-15 20:18 UTC (permalink / raw)
To: ffmpeg-devel; +Cc: Scott Theisen
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".
^ permalink raw reply [flat|nested] 4+ messages in thread
* Re: [FFmpeg-devel] [PATCH v2] lavf/mpeg.c: improve readability of packet identification logic
2022-05-15 20:18 ` [FFmpeg-devel] [PATCH v2] " Scott Theisen
@ 2022-09-16 18:33 ` Scott Theisen
2022-09-16 19:37 ` Michael Niedermayer
0 siblings, 1 reply; 4+ messages in thread
From: Scott Theisen @ 2022-09-16 18:33 UTC (permalink / raw)
To: ffmpeg-devel
Ping. Still applies cleanly.
On 5/15/22 16:18, Scott Theisen wrote:
> 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);
_______________________________________________
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".
^ permalink raw reply [flat|nested] 4+ messages in thread
* Re: [FFmpeg-devel] [PATCH v2] lavf/mpeg.c: improve readability of packet identification logic
2022-09-16 18:33 ` Scott Theisen
@ 2022-09-16 19:37 ` Michael Niedermayer
0 siblings, 0 replies; 4+ messages in thread
From: Michael Niedermayer @ 2022-09-16 19:37 UTC (permalink / raw)
To: FFmpeg development discussions and patches
[-- Attachment #1.1: Type: text/plain, Size: 462 bytes --]
On Fri, Sep 16, 2022 at 02:33:02PM -0400, Scott Theisen wrote:
> Ping. Still applies cleanly.
iam not against this but iam also not really sure i prefer the new
code.
Maybe other people have a clearer opinion ...
thx
[...]
--
Michael GnuPG fingerprint: 9FF2128B147EF6730BADF133611EC787040B0FAB
Complexity theory is the science of finding the exact solution to an
approximation. Benchmarking OTOH is finding an approximation of the exact
[-- Attachment #1.2: signature.asc --]
[-- Type: application/pgp-signature, Size: 195 bytes --]
[-- Attachment #2: Type: text/plain, Size: 251 bytes --]
_______________________________________________
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".
^ permalink raw reply [flat|nested] 4+ messages in thread
end of thread, other threads:[~2022-09-16 19:37 UTC | newest]
Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-02-13 22:42 [FFmpeg-devel] [PATCH] lavf/mpeg.c: improve readability of packet identification logic Scott Theisen
2022-05-15 20:18 ` [FFmpeg-devel] [PATCH v2] " Scott Theisen
2022-09-16 18:33 ` Scott Theisen
2022-09-16 19:37 ` Michael Niedermayer
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