Re: [FFmpeg-devel] [PATCH v2] lavf/mpeg.c: improve readability of packet identification logic

2022-09-16 Thread Michael Niedermayer
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


signature.asc
Description: PGP signature
___
ffmpeg-devel mailing list
ffmpeg-devel@ffmpeg.org
https://ffmpeg.org/mailman/listinfo/ffmpeg-devel

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


Re: [FFmpeg-devel] [PATCH v2] lavf/mpeg.c: improve readability of packet identification logic

2022-09-16 Thread Scott Theisen

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 &

[FFmpeg-devel] [PATCH v2] lavf/mpeg.c: improve readability of packet identification logic

2022-05-15 Thread 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)) {
-