Re: [FFmpeg-devel] [PATCH] mpegts: identify and demux DSMCC-B/MHEG streams

2022-11-28 Thread Scott Theisen

Ping for review.

Thanks in advance,

Scott Theisen

On 11/13/22 18:19, Scott Theisen wrote:

Ping for review.

On 9/17/22 13:08, Scott Theisen wrote:

These changes are from MythTV.
---

The `AV_CODEC_ID`s are probably in the wrong place since these are
data codecs, but that is where they are In MythTV.

There was also a related change to libavformat/demux.c's
avformat_find_stream_info() trying to optimize it for MHEG streams,
but it is unnecessary and was causing FATE to fail.

  libavcodec/codec_desc.c |  12 +++
  libavcodec/codec_id.h   |   6 ++
  libavformat/avformat.h  |   5 +
  libavformat/mpegts.c    | 199 +++-
  libavformat/mpegts.h    |  17 
  5 files changed, 237 insertions(+), 2 deletions(-)

diff --git a/libavcodec/codec_desc.c b/libavcodec/codec_desc.c
index 648c518b3c..0e54087863 100644
--- a/libavcodec/codec_desc.c
+++ b/libavcodec/codec_desc.c
@@ -3488,6 +3488,18 @@ static const AVCodecDescriptor 
codec_descriptors[] = {

  .props = AV_CODEC_PROP_TEXT_SUB,
  .profiles  = NULL_IF_CONFIG_SMALL(ff_arib_caption_profiles),
  },
+    {
+    .id    = AV_CODEC_ID_DVB_VBI,
+    .type  = AVMEDIA_TYPE_DATA,
+    .name  = "dvb_vbi",
+    .long_name = NULL_IF_CONFIG_SMALL("dvb teletext"),
+    },
+    {
+    .id    = AV_CODEC_ID_DSMCC_B,
+    .type  = AVMEDIA_TYPE_DATA,
+    .name  = "dsmcc_b",
+    .long_name = NULL_IF_CONFIG_SMALL("DSMCC B"),
+    },
    /* other kind of codecs and pseudo-codecs */
  {
diff --git a/libavcodec/codec_id.h b/libavcodec/codec_id.h
index bc8226ff07..7e2198a800 100644
--- a/libavcodec/codec_id.h
+++ b/libavcodec/codec_id.h
@@ -559,6 +559,12 @@ enum AVCodecID {
  AV_CODEC_ID_TTML,
  AV_CODEC_ID_ARIB_CAPTION,
  +    /* teletext codecs */
+    AV_CODEC_ID_DVB_VBI,
+
+    /* DSMCC codec */
+    AV_CODEC_ID_DSMCC_B,
+
  /* other specific kind of codecs (generally used for 
attachments) */
  AV_CODEC_ID_FIRST_UNKNOWN = 0x18000,   ///< A dummy ID 
pointing at the start of various fake codecs.

  AV_CODEC_ID_TTF = 0x18000,
diff --git a/libavformat/avformat.h b/libavformat/avformat.h
index 9d46875cce..664a1afa61 100644
--- a/libavformat/avformat.h
+++ b/libavformat/avformat.h
@@ -1117,6 +1117,11 @@ typedef struct AVStream {
   *
   */
  int pts_wrap_bits;
+
+    /* MHEG support */
+    int component_tag; ///< Component tag given in PMT
+    int carousel_id;
+    int data_id;
  } AVStream;
    struct AVCodecParserContext *av_stream_get_parser(const AVStream 
*s);

diff --git a/libavformat/mpegts.c b/libavformat/mpegts.c
index 8a3436f2be..be8edd9e62 100644
--- a/libavformat/mpegts.c
+++ b/libavformat/mpegts.c
@@ -46,6 +46,15 @@
  #include 
  #endif
  +typedef struct SectionContext {
+    int pid;
+    int stream_type;
+    int new_packet;
+    MpegTSContext *ts;
+    AVFormatContext *stream;
+    AVStream *st;
+} SectionContext;
+
  /* maximum size in which we look for synchronization if
   * synchronization is lost */
  #define MAX_RESYNC_SIZE 65536
@@ -412,6 +421,8 @@ static int discard_pid(MpegTSContext *ts, 
unsigned int pid)

  return !used && discarded;
  }
  +static void mpegts_push_section(MpegTSFilter *filter, const 
uint8_t *section, int section_len);

+
  /**
   *  Assemble PES packets out of TS packets, and then call the 
"section_cb"

   *  function when they are complete.
@@ -438,6 +449,11 @@ static void write_section_data(MpegTSContext 
*ts, MpegTSFilter *tss1,

  tss->section_index += len;
  }
  +    if (tss->section_cb == mpegts_push_section) {
+    SectionContext *sect = tss->opaque;
+    sect->new_packet = 1;
+    }
+
  offset = 0;
  cur_section_buf = tss->section_buf;
  while (cur_section_buf - tss->section_buf < MAX_SECTION_SIZE && 
cur_section_buf[0] != 0xff) {

@@ -798,6 +814,7 @@ static const StreamType ISO_types[] = {
  { 0x02, AVMEDIA_TYPE_VIDEO, AV_CODEC_ID_MPEG2VIDEO },
  { 0x03, AVMEDIA_TYPE_AUDIO, AV_CODEC_ID_MP3    },
  { 0x04, AVMEDIA_TYPE_AUDIO, AV_CODEC_ID_MP3    },
+    { 0x0b, AVMEDIA_TYPE_DATA,  AV_CODEC_ID_DSMCC_B    }, /* 
DVB_CAROUSEL_ID */

  { 0x0f, AVMEDIA_TYPE_AUDIO, AV_CODEC_ID_AAC    },
  { 0x10, AVMEDIA_TYPE_VIDEO, AV_CODEC_ID_MPEG4  },
  /* Makito encoder sets stream type 0x11 for AAC,
@@ -882,11 +899,20 @@ static const StreamType DESC_types[] = {
  { 0x6a, AVMEDIA_TYPE_AUDIO,    AV_CODEC_ID_AC3  }, /* 
AC-3 descriptor */
  { 0x7a, AVMEDIA_TYPE_AUDIO,    AV_CODEC_ID_EAC3 }, /* 
E-AC-3 descriptor */

  { 0x7b, AVMEDIA_TYPE_AUDIO,    AV_CODEC_ID_DTS  },
+    { 0x13, AVMEDIA_TYPE_DATA, AV_CODEC_ID_DSMCC_B  }, /* 
DVB_CAROUSEL_ID */
+    { 0x45, AVMEDIA_TYPE_DATA, AV_CODEC_ID_DVB_VBI  }, /* 
DVB_VBI_DATA_ID */
+    { 0x46, AVMEDIA_TYPE_DATA,  

Re: [FFmpeg-devel] [PATCH] mpegts: identify and demux DSMCC-B/MHEG streams

2022-11-13 Thread Scott Theisen

Ping for review.

On 9/17/22 13:08, Scott Theisen wrote:

These changes are from MythTV.
---

The `AV_CODEC_ID`s are probably in the wrong place since these are
data codecs, but that is where they are In MythTV.

There was also a related change to libavformat/demux.c's
avformat_find_stream_info() trying to optimize it for MHEG streams,
but it is unnecessary and was causing FATE to fail.

  libavcodec/codec_desc.c |  12 +++
  libavcodec/codec_id.h   |   6 ++
  libavformat/avformat.h  |   5 +
  libavformat/mpegts.c| 199 +++-
  libavformat/mpegts.h|  17 
  5 files changed, 237 insertions(+), 2 deletions(-)

diff --git a/libavcodec/codec_desc.c b/libavcodec/codec_desc.c
index 648c518b3c..0e54087863 100644
--- a/libavcodec/codec_desc.c
+++ b/libavcodec/codec_desc.c
@@ -3488,6 +3488,18 @@ static const AVCodecDescriptor codec_descriptors[] = {
  .props = AV_CODEC_PROP_TEXT_SUB,
  .profiles  = NULL_IF_CONFIG_SMALL(ff_arib_caption_profiles),
  },
+{
+.id= AV_CODEC_ID_DVB_VBI,
+.type  = AVMEDIA_TYPE_DATA,
+.name  = "dvb_vbi",
+.long_name = NULL_IF_CONFIG_SMALL("dvb teletext"),
+},
+{
+.id= AV_CODEC_ID_DSMCC_B,
+.type  = AVMEDIA_TYPE_DATA,
+.name  = "dsmcc_b",
+.long_name = NULL_IF_CONFIG_SMALL("DSMCC B"),
+},
  
  /* other kind of codecs and pseudo-codecs */

  {
diff --git a/libavcodec/codec_id.h b/libavcodec/codec_id.h
index bc8226ff07..7e2198a800 100644
--- a/libavcodec/codec_id.h
+++ b/libavcodec/codec_id.h
@@ -559,6 +559,12 @@ enum AVCodecID {
  AV_CODEC_ID_TTML,
  AV_CODEC_ID_ARIB_CAPTION,
  
+/* teletext codecs */

+AV_CODEC_ID_DVB_VBI,
+
+/* DSMCC codec */
+AV_CODEC_ID_DSMCC_B,
+
  /* other specific kind of codecs (generally used for attachments) */
  AV_CODEC_ID_FIRST_UNKNOWN = 0x18000,   ///< A dummy ID pointing 
at the start of various fake codecs.
  AV_CODEC_ID_TTF = 0x18000,
diff --git a/libavformat/avformat.h b/libavformat/avformat.h
index 9d46875cce..664a1afa61 100644
--- a/libavformat/avformat.h
+++ b/libavformat/avformat.h
@@ -1117,6 +1117,11 @@ typedef struct AVStream {
   *
   */
  int pts_wrap_bits;
+
+/* MHEG support */
+int component_tag; ///< Component tag given in PMT
+int carousel_id;
+int data_id;
  } AVStream;
  
  struct AVCodecParserContext *av_stream_get_parser(const AVStream *s);

diff --git a/libavformat/mpegts.c b/libavformat/mpegts.c
index 8a3436f2be..be8edd9e62 100644
--- a/libavformat/mpegts.c
+++ b/libavformat/mpegts.c
@@ -46,6 +46,15 @@
  #include 
  #endif
  
+typedef struct SectionContext {

+int pid;
+int stream_type;
+int new_packet;
+MpegTSContext *ts;
+AVFormatContext *stream;
+AVStream *st;
+} SectionContext;
+
  /* maximum size in which we look for synchronization if
   * synchronization is lost */
  #define MAX_RESYNC_SIZE 65536
@@ -412,6 +421,8 @@ static int discard_pid(MpegTSContext *ts, unsigned int pid)
  return !used && discarded;
  }
  
+static void mpegts_push_section(MpegTSFilter *filter, const uint8_t *section, int section_len);

+
  /**
   *  Assemble PES packets out of TS packets, and then call the "section_cb"
   *  function when they are complete.
@@ -438,6 +449,11 @@ static void write_section_data(MpegTSContext *ts, 
MpegTSFilter *tss1,
  tss->section_index += len;
  }
  
+if (tss->section_cb == mpegts_push_section) {

+SectionContext *sect = tss->opaque;
+sect->new_packet = 1;
+}
+
  offset = 0;
  cur_section_buf = tss->section_buf;
  while (cur_section_buf - tss->section_buf < MAX_SECTION_SIZE && 
cur_section_buf[0] != 0xff) {
@@ -798,6 +814,7 @@ static const StreamType ISO_types[] = {
  { 0x02, AVMEDIA_TYPE_VIDEO, AV_CODEC_ID_MPEG2VIDEO },
  { 0x03, AVMEDIA_TYPE_AUDIO, AV_CODEC_ID_MP3},
  { 0x04, AVMEDIA_TYPE_AUDIO, AV_CODEC_ID_MP3},
+{ 0x0b, AVMEDIA_TYPE_DATA,  AV_CODEC_ID_DSMCC_B}, /* DVB_CAROUSEL_ID */
  { 0x0f, AVMEDIA_TYPE_AUDIO, AV_CODEC_ID_AAC},
  { 0x10, AVMEDIA_TYPE_VIDEO, AV_CODEC_ID_MPEG4  },
  /* Makito encoder sets stream type 0x11 for AAC,
@@ -882,11 +899,20 @@ static const StreamType DESC_types[] = {
  { 0x6a, AVMEDIA_TYPE_AUDIO,AV_CODEC_ID_AC3  }, /* AC-3 
descriptor */
  { 0x7a, AVMEDIA_TYPE_AUDIO,AV_CODEC_ID_EAC3 }, /* E-AC-3 
descriptor */
  { 0x7b, AVMEDIA_TYPE_AUDIO,AV_CODEC_ID_DTS  },
+{ 0x13, AVMEDIA_TYPE_DATA, AV_CODEC_ID_DSMCC_B  }, /* 
DVB_CAROUSEL_ID */
+{ 0x45, AVMEDIA_TYPE_DATA, AV_CODEC_ID_DVB_VBI  }, /* 
DVB_VBI_DATA_ID */
+{ 0x46, AVMEDIA_TYPE_DATA, AV_CODEC_ID_DVB_VBI  }, /* 
DVB_VBI_TELETEXT_ID */ //FixMe type subtilte
  { 0x56, AVMEDIA_TYPE_SUBTIT

Re: [FFmpeg-devel] [PATCH] lavc: export flag for MPEG audio dual channel

2022-09-22 Thread Scott Theisen

On 9/22/22 08:43, Anton Khirnov wrote:

Quoting Scott Theisen (2022-09-22 03:04:16)

On 9/21/22 15:51, James Almer wrote:

On 9/21/2022 4:44 PM, Rémi Denis-Courmont wrote:

Le keskiviikkona 21. syyskuuta 2022, 22.26.11 EEST Scott Theisen a
écrit :

diff --git a/libavcodec/avcodec.h b/libavcodec/avcodec.h
index 7db5d1b1c5..bcf3a845a8 100644
--- a/libavcodec/avcodec.h
+++ b/libavcodec/avcodec.h
@@ -2076,6 +2076,17 @@ typedef struct AVCodecContext {
    * The decoder can then override during decoding
as needed.
*/
   AVChannelLayout ch_layout;
+
+    /**
+ * Audio only.  This flag is set when MPEG audio mode dual
channel has
been detected. + * This signals that the audio is two
independent mono
channels. + *
+ * 0 normally, 1 if dual channel flag is set.
+ *
+ * - encoding: currently unused (functionally equivalent to
stereo,
patch welcome) + * - decoding: set by lavc
+ */
+    int mpeg_audio_mode_dual_channel;
   } AVCodecContext;

I agree that the dual mono flag should be exposed to the application
somehow,
but isn't this a slient ABI break?

It's not a break, but it's overkill for what's essentially a flag.

This is how MythTV customized FFmpeg (in 2006) for this and this way was
probably the easiest.  It /is/ a flag, so maybe adding an int as a
bitset instead of as a bool so other flags could be added if necessary?


The proper way to do this would be to signal such a layout as an
actual channel layout, using a custom order one where both channels
are set as Front Center. But i don't know if until the old channel
layout API fields are gone we should have decoders setting something
only new API users will understand. Old API field users would look at
channels and see a 2, and channel_layout and see it's empty, but then
old and new API values would technically conflict, so I'd like to hear
some opinions.

I'm not very familiar with either channel layout API, but the audio is
encoded identically to stereo other than the flag, so could we add
another entry, e.g. AV_CHANNEL_ORDER_MONO, to the `enum AVChannelOrder`
to signify that each channel is independent, but is otherwise identical
to AV_CHANNEL_ORDER_NATIVE?

The whole point is that it's NOT identical to AV_CHANNEL_ORDER_NATIVE.
ORDER_CUSTOM with two FC channels is exactly the right way to handle
this IMO.



I don't really disagree that AV_CHANNEL_ORDER_CUSTOM with two Front 
Center channels is one way to do it, I was just hoping for an easier way.


I think that would require modification to the code calling 
ff_mpa_decode_header.  But I'm not sure how to create that custom order 
layout and I would like consensus that the custom order is the way to go 
before doing it.


libavcodec/audiotoolboxdec.c just sets an unspecified order. 
libavcodec/mpegaudio_parser.c uses av_channel_layout_default.


Also, should we concern ourselves with the old API?  It didn't look like 
either of the above files did.


Regards,

Scott Theisen

___
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] cbs_mpeg2_split_fragment(): cache the buffer end

2022-09-22 Thread Scott Theisen

On 9/22/22 10:59, Anton Khirnov wrote:

Quoting Scott Theisen (2022-09-16 19:41:43)

Ping; it still applies cleanly.

Missing motivation for this change. Is it faster, or why is the new code
better?



To make it easier to read.  `buf_end` is shorter than `frag->data + 
frag->data_size`.  Initialize `start` to `frag->data` so the calls to 
avpriv_find_start_code are the same.


I realize readability is an opinion, but I felt that saving the buffer 
end to a variable and tweaking the comments increased the readability of 
the code.


Regards,

Scott
___
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] lavc: export flag for MPEG audio dual channel

2022-09-21 Thread Scott Theisen

On 9/21/22 15:51, James Almer wrote:

On 9/21/2022 4:44 PM, Rémi Denis-Courmont wrote:
Le keskiviikkona 21. syyskuuta 2022, 22.26.11 EEST Scott Theisen a 
écrit :

diff --git a/libavcodec/avcodec.h b/libavcodec/avcodec.h
index 7db5d1b1c5..bcf3a845a8 100644
--- a/libavcodec/avcodec.h
+++ b/libavcodec/avcodec.h
@@ -2076,6 +2076,17 @@ typedef struct AVCodecContext {
   * The decoder can then override during decoding 
as needed.

*/
  AVChannelLayout ch_layout;
+
+    /**
+ * Audio only.  This flag is set when MPEG audio mode dual 
channel has
been detected. + * This signals that the audio is two 
independent mono

channels. + *
+ * 0 normally, 1 if dual channel flag is set.
+ *
+ * - encoding: currently unused (functionally equivalent to 
stereo,

patch welcome) + * - decoding: set by lavc
+ */
+    int mpeg_audio_mode_dual_channel;
  } AVCodecContext;


I agree that the dual mono flag should be exposed to the application 
somehow,

but isn't this a slient ABI break?


It's not a break, but it's overkill for what's essentially a flag.


This is how MythTV customized FFmpeg (in 2006) for this and this way was 
probably the easiest.  It /is/ a flag, so maybe adding an int as a 
bitset instead of as a bool so other flags could be added if necessary?




The proper way to do this would be to signal such a layout as an 
actual channel layout, using a custom order one where both channels 
are set as Front Center. But i don't know if until the old channel 
layout API fields are gone we should have decoders setting something 
only new API users will understand. Old API field users would look at 
channels and see a 2, and channel_layout and see it's empty, but then 
old and new API values would technically conflict, so I'd like to hear 
some opinions.


I'm not very familiar with either channel layout API, but the audio is 
encoded identically to stereo other than the flag, so could we add 
another entry, e.g. AV_CHANNEL_ORDER_MONO, to the `enum AVChannelOrder` 
to signify that each channel is independent, but is otherwise identical 
to AV_CHANNEL_ORDER_NATIVE?




An alternative could be signaling this using an 
AVFormatContext.metadata entry called "dualmono", and ensuring the 
channel layout is 2 channels of UNSPEC order.




AVFormatContext sounds like the wrong place, since there could, in 
theory, be multiple audio streams utilizing or not dual mono audio, 
which could also be switching back and forth between using dual mono and 
not.


Regards,

Scott
___
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".


[FFmpeg-devel] [PATCH] lavc: export flag for MPEG audio dual channel

2022-09-21 Thread Scott Theisen
From: ulmus-scott 

The flag identifies two independant mono channels recorded as stereo.

This change has been kicking around in the MythTV modifications since 2006.
See 
https://github.com/MythTV/mythtv/commit/435540c9e8ac245ceca968791c67431f37c8d617
I have changed the names and comment.  For the current MythTV modification see
https://github.com/ulmus-scott/FFmpeg/commit/645fa6f9a61d23bac0665851d211bbeb3686deb0
---
 libavcodec/audiotoolboxdec.c|  2 +-
 libavcodec/avcodec.h| 11 +++
 libavcodec/mpegaudio_parser.c   |  2 +-
 libavcodec/mpegaudiodecheader.c |  4 +++-
 libavcodec/mpegaudiodecheader.h |  2 +-
 5 files changed, 17 insertions(+), 4 deletions(-)

diff --git a/libavcodec/audiotoolboxdec.c b/libavcodec/audiotoolboxdec.c
index 82babe3d31..9d11844142 100644
--- a/libavcodec/audiotoolboxdec.c
+++ b/libavcodec/audiotoolboxdec.c
@@ -346,7 +346,7 @@ static av_cold int ffat_create_decoder(AVCodecContext 
*avctx,
 int bit_rate;
 if (ff_mpa_decode_header(AV_RB32(pkt->data), >sample_rate,
  _format.mChannelsPerFrame, 
>frame_size,
- _rate, _id) < 0)
+ _rate, _id, 
>mpeg_audio_mode_dual_channel) < 0)
 return AVERROR_INVALIDDATA;
 avctx->bit_rate = bit_rate;
 in_format.mSampleRate = avctx->sample_rate;
diff --git a/libavcodec/avcodec.h b/libavcodec/avcodec.h
index 7db5d1b1c5..bcf3a845a8 100644
--- a/libavcodec/avcodec.h
+++ b/libavcodec/avcodec.h
@@ -2076,6 +2076,17 @@ typedef struct AVCodecContext {
  * The decoder can then override during decoding as needed.
  */
 AVChannelLayout ch_layout;
+
+/**
+ * Audio only.  This flag is set when MPEG audio mode dual channel has 
been detected.
+ * This signals that the audio is two independent mono channels.
+ *
+ * 0 normally, 1 if dual channel flag is set.
+ *
+ * - encoding: currently unused (functionally equivalent to stereo, patch 
welcome)
+ * - decoding: set by lavc
+ */
+int mpeg_audio_mode_dual_channel;
 } AVCodecContext;
 
 /**
diff --git a/libavcodec/mpegaudio_parser.c b/libavcodec/mpegaudio_parser.c
index d54366f10a..d957cf467f 100644
--- a/libavcodec/mpegaudio_parser.c
+++ b/libavcodec/mpegaudio_parser.c
@@ -70,7 +70,7 @@ static int mpegaudio_parse(AVCodecParserContext *s1,
 
 state= (state<<8) + buf[i++];
 
-ret = ff_mpa_decode_header(state, , , _size, 
_rate, _id);
+ret = ff_mpa_decode_header(state, , , _size, 
_rate, _id, >mpeg_audio_mode_dual_channel);
 if (ret < 4) {
 if (i > 4)
 s->header_count = -2;
diff --git a/libavcodec/mpegaudiodecheader.c b/libavcodec/mpegaudiodecheader.c
index ef63befbf4..6c9a641906 100644
--- a/libavcodec/mpegaudiodecheader.c
+++ b/libavcodec/mpegaudiodecheader.c
@@ -117,7 +117,7 @@ int avpriv_mpegaudio_decode_header(MPADecodeHeader *s, 
uint32_t header)
 return 0;
 }
 
-int ff_mpa_decode_header(uint32_t head, int *sample_rate, int *channels, int 
*frame_size, int *bit_rate, enum AVCodecID *codec_id)
+int ff_mpa_decode_header(uint32_t head, int *sample_rate, int *channels, int 
*frame_size, int *bit_rate, enum AVCodecID *codec_id, int *dual_mono)
 {
 MPADecodeHeader s1, *s = 
 
@@ -148,5 +148,7 @@ int ff_mpa_decode_header(uint32_t head, int *sample_rate, 
int *channels, int *fr
 *sample_rate = s->sample_rate;
 *channels = s->nb_channels;
 *bit_rate = s->bit_rate;
+*dual_mono = (s->mode == MPA_DUAL) ? 1 : 0;
+
 return s->frame_size;
 }
diff --git a/libavcodec/mpegaudiodecheader.h b/libavcodec/mpegaudiodecheader.h
index ed5d1f3b33..e599d287f7 100644
--- a/libavcodec/mpegaudiodecheader.h
+++ b/libavcodec/mpegaudiodecheader.h
@@ -56,7 +56,7 @@ int avpriv_mpegaudio_decode_header(MPADecodeHeader *s, 
uint32_t header);
 /* useful helper to get MPEG audio stream info. Return -1 if error in
header, otherwise the coded frame size in bytes */
 int ff_mpa_decode_header(uint32_t head, int *sample_rate,
- int *channels, int *frame_size, int *bitrate, enum 
AVCodecID *codec_id);
+ int *channels, int *frame_size, int *bitrate, enum 
AVCodecID *codec_id, int *dual_mono);
 
 /* fast header check for resync */
 static inline int ff_mpa_check_header(uint32_t header){
-- 
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-requ...@ffmpeg.org with subject "unsubscribe".


Re: [FFmpeg-devel] [PATCH] lavc: add detection of forced subtitles

2022-09-20 Thread Scott Theisen

On 9/19/22 16:58, Hendrik Leppkes wrote:

On Mon, Sep 19, 2022 at 10:53 PM Andreas Rheinhardt
 wrote:

Scott Theisen:

This is from the following MythTV commits:
libavcodec: add support for detecting forced subtitle segments. 
https://github.com/MythTV/mythtv/commit/5099f1a5777966fba482b623e581c1eef5c8fc09
This adds forced subtitle segment detection to the PGS subtitle decoder and
copies the result to AVSubtitle.

Subtitles: Flag forced DVD subtitles. 
https://github.com/MythTV/mythtv/commit/78f71eecdbd53ba92af2ad639b32564c01f24563
---
  libavcodec/avcodec.h   | 1 +
  libavcodec/dvdsubdec.c | 4 +++-
  libavcodec/pgssubdec.c | 4 
  3 files changed, 8 insertions(+), 1 deletion(-)

diff --git a/libavcodec/avcodec.h b/libavcodec/avcodec.h
index 7db5d1b1c5..b9aa9efb2f 100644
--- a/libavcodec/avcodec.h
+++ b/libavcodec/avcodec.h
@@ -2331,6 +2331,7 @@ typedef struct AVSubtitle {
  unsigned num_rects;
  AVSubtitleRect **rects;
  int64_t pts;///< Same as packet pts, in AV_TIME_BASE
+int forced;

sizeof(AVSubtitle) is part of the public ABI, no additions to it are
possible. I wanted to suggest using AVSubtitleRect.flags, but apparently
AV_SUBTITLE_FLAG_FORCED already exists. And it seems that both the
dvdsub as well as the pgssub decoders already set this.


Which is also more correct, since in PGS for example you can have both
active rects that are forced, and those that are not, at the same
time.

- Hendrik


Thanks for the suggestion, I'll look into switching MythTV to that. 
MythTV's handling of subtitles involves a few customizations to FFmpeg 
and I thought this would be acceptable.  Apparently not, and there 
already exists a method for this.


Thanks,

Scott
___
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".


[FFmpeg-devel] [PATCH] lavc: add detection of forced subtitles

2022-09-19 Thread Scott Theisen
This is from the following MythTV commits:
libavcodec: add support for detecting forced subtitle segments. 
https://github.com/MythTV/mythtv/commit/5099f1a5777966fba482b623e581c1eef5c8fc09
This adds forced subtitle segment detection to the PGS subtitle decoder and
copies the result to AVSubtitle.

Subtitles: Flag forced DVD subtitles. 
https://github.com/MythTV/mythtv/commit/78f71eecdbd53ba92af2ad639b32564c01f24563
---
 libavcodec/avcodec.h   | 1 +
 libavcodec/dvdsubdec.c | 4 +++-
 libavcodec/pgssubdec.c | 4 
 3 files changed, 8 insertions(+), 1 deletion(-)

diff --git a/libavcodec/avcodec.h b/libavcodec/avcodec.h
index 7db5d1b1c5..b9aa9efb2f 100644
--- a/libavcodec/avcodec.h
+++ b/libavcodec/avcodec.h
@@ -2331,6 +2331,7 @@ typedef struct AVSubtitle {
 unsigned num_rects;
 AVSubtitleRect **rects;
 int64_t pts;///< Same as packet pts, in AV_TIME_BASE
+int forced;
 } AVSubtitle;
 
 /**
diff --git a/libavcodec/dvdsubdec.c b/libavcodec/dvdsubdec.c
index a5da0d7b08..1504706d52 100644
--- a/libavcodec/dvdsubdec.c
+++ b/libavcodec/dvdsubdec.c
@@ -418,8 +418,10 @@ static int decode_dvd_subtitles(DVDSubContext *ctx, 
AVSubtitle *sub_header,
 break;
 cmd_pos = next_cmd_pos;
 }
-if (sub_header->num_rects > 0)
+if (sub_header->num_rects > 0) {
+sub_header->forced = is_menu;
 return is_menu;
+}
  fail:
 reset_rects(sub_header);
 return -1;
diff --git a/libavcodec/pgssubdec.c b/libavcodec/pgssubdec.c
index 5f76f12615..7c953263bb 100644
--- a/libavcodec/pgssubdec.c
+++ b/libavcodec/pgssubdec.c
@@ -62,6 +62,7 @@ typedef struct PGSSubPresentation {
 int id_number;
 int palette_id;
 int object_count;
+int object_forced;
 PGSSubObjectRef objects[MAX_OBJECT_REFS];
 int64_t pts;
 } PGSSubPresentation;
@@ -466,6 +467,8 @@ static int parse_presentation_segment(AVCodecContext *avctx,
 object->crop_h = bytestream_get_be16();
 }
 
+ctx->presentation.object_forced = 
(ctx->presentation.objects[i].composition_flag & 0x40) >> 6;
+
 ff_dlog(avctx, "Subtitle Placement x=%d, y=%d\n",
 object->x, object->y);
 
@@ -505,6 +508,7 @@ static int display_end_segment(AVCodecContext *avctx, 
AVSubtitle *sub,
 memset(sub, 0, sizeof(*sub));
 sub->pts = pts;
 ctx->presentation.pts = AV_NOPTS_VALUE;
+sub->forced = ctx->presentation.object_forced;
 sub->start_display_time = 0;
 // There is no explicit end time for PGS subtitles.  The end time
 // is defined by the start of the next sub which may contain no
-- 
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-requ...@ffmpeg.org with subject "unsubscribe".


[FFmpeg-devel] [PATCH] mpegts: identify and demux DSMCC-B/MHEG streams

2022-09-17 Thread Scott Theisen
These changes are from MythTV.
---

The `AV_CODEC_ID`s are probably in the wrong place since these are
data codecs, but that is where they are In MythTV.

There was also a related change to libavformat/demux.c's
avformat_find_stream_info() trying to optimize it for MHEG streams,
but it is unnecessary and was causing FATE to fail.

 libavcodec/codec_desc.c |  12 +++
 libavcodec/codec_id.h   |   6 ++
 libavformat/avformat.h  |   5 +
 libavformat/mpegts.c| 199 +++-
 libavformat/mpegts.h|  17 
 5 files changed, 237 insertions(+), 2 deletions(-)

diff --git a/libavcodec/codec_desc.c b/libavcodec/codec_desc.c
index 648c518b3c..0e54087863 100644
--- a/libavcodec/codec_desc.c
+++ b/libavcodec/codec_desc.c
@@ -3488,6 +3488,18 @@ static const AVCodecDescriptor codec_descriptors[] = {
 .props = AV_CODEC_PROP_TEXT_SUB,
 .profiles  = NULL_IF_CONFIG_SMALL(ff_arib_caption_profiles),
 },
+{
+.id= AV_CODEC_ID_DVB_VBI,
+.type  = AVMEDIA_TYPE_DATA,
+.name  = "dvb_vbi",
+.long_name = NULL_IF_CONFIG_SMALL("dvb teletext"),
+},
+{
+.id= AV_CODEC_ID_DSMCC_B,
+.type  = AVMEDIA_TYPE_DATA,
+.name  = "dsmcc_b",
+.long_name = NULL_IF_CONFIG_SMALL("DSMCC B"),
+},
 
 /* other kind of codecs and pseudo-codecs */
 {
diff --git a/libavcodec/codec_id.h b/libavcodec/codec_id.h
index bc8226ff07..7e2198a800 100644
--- a/libavcodec/codec_id.h
+++ b/libavcodec/codec_id.h
@@ -559,6 +559,12 @@ enum AVCodecID {
 AV_CODEC_ID_TTML,
 AV_CODEC_ID_ARIB_CAPTION,
 
+/* teletext codecs */
+AV_CODEC_ID_DVB_VBI,
+
+/* DSMCC codec */
+AV_CODEC_ID_DSMCC_B,
+
 /* other specific kind of codecs (generally used for attachments) */
 AV_CODEC_ID_FIRST_UNKNOWN = 0x18000,   ///< A dummy ID pointing at 
the start of various fake codecs.
 AV_CODEC_ID_TTF = 0x18000,
diff --git a/libavformat/avformat.h b/libavformat/avformat.h
index 9d46875cce..664a1afa61 100644
--- a/libavformat/avformat.h
+++ b/libavformat/avformat.h
@@ -1117,6 +1117,11 @@ typedef struct AVStream {
  *
  */
 int pts_wrap_bits;
+
+/* MHEG support */
+int component_tag; ///< Component tag given in PMT
+int carousel_id;
+int data_id;
 } AVStream;
 
 struct AVCodecParserContext *av_stream_get_parser(const AVStream *s);
diff --git a/libavformat/mpegts.c b/libavformat/mpegts.c
index 8a3436f2be..be8edd9e62 100644
--- a/libavformat/mpegts.c
+++ b/libavformat/mpegts.c
@@ -46,6 +46,15 @@
 #include 
 #endif
 
+typedef struct SectionContext {
+int pid;
+int stream_type;
+int new_packet;
+MpegTSContext *ts;
+AVFormatContext *stream;
+AVStream *st;
+} SectionContext;
+
 /* maximum size in which we look for synchronization if
  * synchronization is lost */
 #define MAX_RESYNC_SIZE 65536
@@ -412,6 +421,8 @@ static int discard_pid(MpegTSContext *ts, unsigned int pid)
 return !used && discarded;
 }
 
+static void mpegts_push_section(MpegTSFilter *filter, const uint8_t *section, 
int section_len);
+
 /**
  *  Assemble PES packets out of TS packets, and then call the "section_cb"
  *  function when they are complete.
@@ -438,6 +449,11 @@ static void write_section_data(MpegTSContext *ts, 
MpegTSFilter *tss1,
 tss->section_index += len;
 }
 
+if (tss->section_cb == mpegts_push_section) {
+SectionContext *sect = tss->opaque;
+sect->new_packet = 1;
+}
+
 offset = 0;
 cur_section_buf = tss->section_buf;
 while (cur_section_buf - tss->section_buf < MAX_SECTION_SIZE && 
cur_section_buf[0] != 0xff) {
@@ -798,6 +814,7 @@ static const StreamType ISO_types[] = {
 { 0x02, AVMEDIA_TYPE_VIDEO, AV_CODEC_ID_MPEG2VIDEO },
 { 0x03, AVMEDIA_TYPE_AUDIO, AV_CODEC_ID_MP3},
 { 0x04, AVMEDIA_TYPE_AUDIO, AV_CODEC_ID_MP3},
+{ 0x0b, AVMEDIA_TYPE_DATA,  AV_CODEC_ID_DSMCC_B}, /* DVB_CAROUSEL_ID */
 { 0x0f, AVMEDIA_TYPE_AUDIO, AV_CODEC_ID_AAC},
 { 0x10, AVMEDIA_TYPE_VIDEO, AV_CODEC_ID_MPEG4  },
 /* Makito encoder sets stream type 0x11 for AAC,
@@ -882,11 +899,20 @@ static const StreamType DESC_types[] = {
 { 0x6a, AVMEDIA_TYPE_AUDIO,AV_CODEC_ID_AC3  }, /* AC-3 
descriptor */
 { 0x7a, AVMEDIA_TYPE_AUDIO,AV_CODEC_ID_EAC3 }, /* E-AC-3 
descriptor */
 { 0x7b, AVMEDIA_TYPE_AUDIO,AV_CODEC_ID_DTS  },
+{ 0x13, AVMEDIA_TYPE_DATA, AV_CODEC_ID_DSMCC_B  }, /* 
DVB_CAROUSEL_ID */
+{ 0x45, AVMEDIA_TYPE_DATA, AV_CODEC_ID_DVB_VBI  }, /* 
DVB_VBI_DATA_ID */
+{ 0x46, AVMEDIA_TYPE_DATA, AV_CODEC_ID_DVB_VBI  }, /* 
DVB_VBI_TELETEXT_ID */ //FixMe type subtilte
 { 0x56, AVMEDIA_TYPE_SUBTITLE, AV_CODEC_ID_DVB_TELETEXT },
 { 0x59, AVMEDIA_TYPE_SUBTITLE, AV_CODEC_ID_DVB_SUBTITLE }, /* subtitling 
descriptor */
 { 0 },
 };
 
+/* component tags */
+static const StreamType 

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 <= 

Re: [FFmpeg-devel] [PATCH v4 0/8] rewrite avpriv_find_start_code() for clarity

2022-09-16 Thread Scott Theisen

On 9/16/22 14:19, Scott Theisen wrote:

My modified versions of avpriv_find_start_code were accepted into MythTV,
which caused clang-tidy to raise a bugprone-branch-clone warning.  The
for loop's if statement has therefore been modified to not duplicate


This should say while loop.


the branch payload.

There are no other changes from v3.

Regards,

Scott Theisen





___
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".


[FFmpeg-devel] [PATCH v4 8/8] avpriv_find_start_code(): make start_code output only

2022-09-16 Thread Scott Theisen
The input/output functionality was used by only
(ff_)mpeg1_find_frame_end().

If the state/start_code input is a local variable and only one buffer is used,
then no history is needed.

In loops and inline functions: if ignoring history, don't initialize start_code,
so it isn't reset twice each time.

There is a slight functional change:
00 00 01 00 01 XX no longer incorrectly returns a start code at
offset 7 that overlaps the start code at offset 4 if the start_code
input is not modified between the two calls.
---
 libavcodec/cbs_mpeg2.c|  5 
 libavcodec/h264_parser.c  |  2 +-
 libavcodec/mpeg12.c   | 41 +-
 libavcodec/mpeg4_unpack_bframes_bsf.c |  1 -
 libavcodec/mpegvideo_parser.c | 42 +--
 libavcodec/startcode.h| 14 +++--
 libavcodec/utils.c| 16 --
 libavcodec/vc1_common.h   |  2 +-
 libavformat/rtpenc_mpv.c  |  2 +-
 9 files changed, 92 insertions(+), 33 deletions(-)

diff --git a/libavcodec/cbs_mpeg2.c b/libavcodec/cbs_mpeg2.c
index 23839ca47b..fd235a43a7 100644
--- a/libavcodec/cbs_mpeg2.c
+++ b/libavcodec/cbs_mpeg2.c
@@ -160,11 +160,6 @@ static int cbs_mpeg2_split_fragment(CodedBitstreamContext 
*ctx,
 const uint8_t *end;
 size_t unit_size;
 
-// Reset start_code to ensure that avpriv_find_start_code()
-// really reads a new start code and does not reuse the old
-// start code in any way (as e.g. happens when there is a
-// Sequence End unit at the very end of a packet).
-start_code = UINT32_MAX;
 end = avpriv_find_start_code(start--, frag->data + frag->data_size,
  _code);
 
diff --git a/libavcodec/h264_parser.c b/libavcodec/h264_parser.c
index 50810f1789..b67830d40e 100644
--- a/libavcodec/h264_parser.c
+++ b/libavcodec/h264_parser.c
@@ -69,7 +69,7 @@ typedef struct H264ParseContext {
 static int find_start_code(const uint8_t *buf, int buf_size,
   int buf_index, int next_avc)
 {
-uint32_t state = -1;
+uint32_t state;
 
 buf_index = avpriv_find_start_code(buf + buf_index, buf + next_avc + 1, 
) - buf - 1;
 
diff --git a/libavcodec/mpeg12.c b/libavcodec/mpeg12.c
index 5ff1830496..9e3c7e5814 100644
--- a/libavcodec/mpeg12.c
+++ b/libavcodec/mpeg12.c
@@ -166,6 +166,45 @@ av_cold void ff_mpeg12_init_vlcs(void)
 }
 
 #if FF_API_FLAG_TRUNCATED
+/**
+ * By preserving the @p start_code value between subsequent calls, the 
caller can
+ * detect start codes across buffer boundaries.
+ *
+ * @param[in,out] start_code A pointer to a mutable @c uint32_t.
+ *  As input: For no history preset to @c ~0 , otherwise preset 
to the last
+ *returned start code to enable detecting start codes 
across
+ *buffer boundaries.
+ *  On output: Set to the found start code if it exists or an invalid
+ * start code (the 4 bytes prior to the returned value,
+ * using the input history if @f$ end - p < 4 @f$).
+ *
+ * @sa avpriv_find_start_code()
+ */
+static const uint8_t *find_start_code_truncated(const uint8_t *av_restrict p,
+  const uint8_t * const end,
+  uint32_t * const av_restrict start_code)
+{
+av_assert0(p <= end);
+if (p >= end)
+return end;
+
+if (*start_code == 0x100)
+*start_code = ~0;
+// invalidate byte 0 so overlapping start codes are not erroneously 
detected
+
+// read up to the first three bytes in p to enable reading a start code 
across
+// two (to four) buffers
+for (int i = 0; i < 3; i++) {
+*start_code <<= 8;
+*start_code += *p;
+p++;
+if (start_code_is_valid(*start_code) || p == end)
+return p;
+}
+// buffer length is at least 4
+return avpriv_find_start_code(p - 3, end, start_code);
+}
+
 /**
  * Find the end of the current frame in the bitstream.
  * @return the position of the first byte of the next frame, or -1
@@ -200,7 +239,7 @@ int ff_mpeg1_find_frame_end(ParseContext *pc, const uint8_t 
*buf, int buf_size,
 }
 state++;
 } else {
-i = avpriv_find_start_code(buf + i, buf + buf_size, ) - buf 
- 1;
+i = find_start_code_truncated(buf + i, buf + buf_size, ) - 
buf - 1;
 if (pc->frame_start_found == 0 && state >= SLICE_MIN_START_CODE && 
state <= SLICE_MAX_START_CODE) {
 i++;
 pc->frame_start_found = 4;
diff --git a/libavcodec/mpeg4_unpack_bframes_bsf.c 
b/libavcodec/mpeg4_unpack_bframes_bsf.c
index 3a3aad795f..dd351d9d0f 100644
--- a/libavcodec/mpeg4_unpack_bframes_bsf.c
+++ b/libavcodec/mpeg4_unpack_bframes_bsf.c
@@ -36,7 +36,6 @@ static void scan_buffer(const uint8_t *buf, int buf_size,
 const uint8_t *end = buf + 

[FFmpeg-devel] [PATCH v4 7/8] avpriv_find_start_code(): constify pointer parameters

2022-09-16 Thread Scott Theisen
Have the compiler enforce not changing the addresses these parameters point to.

No functional change.
---
 libavcodec/utils.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/libavcodec/utils.c b/libavcodec/utils.c
index 63bf4b8cb7..0635c5dcaa 100644
--- a/libavcodec/utils.c
+++ b/libavcodec/utils.c
@@ -980,8 +980,8 @@ void ff_thread_report_progress2(AVCodecContext *avctx, int 
field, int thread, in
 #endif
 
 const uint8_t *avpriv_find_start_code(const uint8_t *av_restrict p,
-  const uint8_t *end,
-  uint32_t *av_restrict start_code)
+  const uint8_t * const end,
+  uint32_t * const av_restrict start_code)
 {
 av_assert0(p <= end);
 if (p >= end)
-- 
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-requ...@ffmpeg.org with subject "unsubscribe".


[FFmpeg-devel] [PATCH v4 6/8] avpriv_find_start_code(): correct type of start_code parameter

2022-09-16 Thread Scott Theisen
---
 libavcodec/mpeg12dec.c   | 2 +-
 libavformat/rtpenc_mpv.c | 3 +--
 2 files changed, 2 insertions(+), 3 deletions(-)

diff --git a/libavcodec/mpeg12dec.c b/libavcodec/mpeg12dec.c
index b857353ab8..6a7af91fad 100644
--- a/libavcodec/mpeg12dec.c
+++ b/libavcodec/mpeg12dec.c
@@ -1746,7 +1746,7 @@ static int mpeg_decode_slice(MpegEncContext *s, int mb_y,
 
 if (avctx->hwaccel && avctx->hwaccel->decode_slice) {
 const uint8_t *buf_end, *buf_start = *buf - 4; /* include start_code */
-int start_code = -1;
+uint32_t start_code = ~0;
 buf_end = avpriv_find_start_code(buf_start + 2, *buf + buf_size, 
_code);
 if (buf_end < *buf + buf_size)
 buf_end -= 4;
diff --git a/libavformat/rtpenc_mpv.c b/libavformat/rtpenc_mpv.c
index 8b6987b7f2..9c0816ef95 100644
--- a/libavformat/rtpenc_mpv.c
+++ b/libavformat/rtpenc_mpv.c
@@ -51,11 +51,10 @@ void ff_rtp_send_mpegvideo(AVFormatContext *s1, const 
uint8_t *buf1, int size)
 end_of_slice = 1;
 } else {
 const uint8_t *r, *r1;
-int start_code;
 
 r1 = buf1;
 while (1) {
-start_code = -1;
+uint32_t start_code = ~0;
 r = avpriv_find_start_code(r1, end, _code);
 if (start_code_is_valid(start_code)) {
 /* New start code found */
-- 
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-requ...@ffmpeg.org with subject "unsubscribe".


[FFmpeg-devel] [PATCH v4 5/8] avpriv_find_start_code(): add doxygen comment and rename a parameter

2022-09-16 Thread Scott Theisen
---
 libavcodec/startcode.h | 26 +-
 libavcodec/utils.c | 10 +-
 2 files changed, 30 insertions(+), 6 deletions(-)

diff --git a/libavcodec/startcode.h b/libavcodec/startcode.h
index 833754af09..69389c729c 100644
--- a/libavcodec/startcode.h
+++ b/libavcodec/startcode.h
@@ -44,9 +44,33 @@ static av_always_inline int start_code_is_valid(uint32_t 
start_code) {
 return (start_code & 0xFF00) == 0x100;
 }
 
+/**
+ * @brief Find the first start code in the buffer @p p.
+ *
+ * A start code is a sequence of 4 bytes with the hexadecimal value  00 
00 01 XX ,
+ * where  XX  represents any value and memory address 
increases left to right.
+ *
+ * By preserving the @p start_code value between subsequent calls, the 
caller can
+ * detect start codes across buffer boundaries.
+ *
+ * @param[in] p A pointer to the start of the memory buffer to scan.
+ * @param[in] end   A pointer to the past-the-end memory address for the buffer
+ *  given by @p p.  @p p must be ≤ @p end.
+ *
+ * @param[in,out] start_code A pointer to a mutable @c uint32_t.
+ *  As input: For no history preset to @c ~0 , otherwise preset 
to the last
+ *returned start code to enable detecting start codes 
across
+ *buffer boundaries.
+ *  On output: Set to the found start code if it exists or an invalid
+ * start code (the 4 bytes prior to the returned value,
+ * using the input history if @f$ end - p < 4 @f$).
+ *
+ * @return A pointer to the memory address following the found start code, or 
@p end
+ * if no start code was found.
+ */
 const uint8_t *avpriv_find_start_code(const uint8_t *p,
   const uint8_t *end,
-  uint32_t *state);
+  uint32_t *start_code);
 
 int ff_startcode_find_candidate_c(const uint8_t *buf, int size);
 
diff --git a/libavcodec/utils.c b/libavcodec/utils.c
index 83f7d8a01a..63bf4b8cb7 100644
--- a/libavcodec/utils.c
+++ b/libavcodec/utils.c
@@ -981,7 +981,7 @@ void ff_thread_report_progress2(AVCodecContext *avctx, int 
field, int thread, in
 
 const uint8_t *avpriv_find_start_code(const uint8_t *av_restrict p,
   const uint8_t *end,
-  uint32_t *av_restrict state)
+  uint32_t *av_restrict start_code)
 {
 av_assert0(p <= end);
 if (p >= end)
@@ -990,10 +990,10 @@ const uint8_t *avpriv_find_start_code(const uint8_t 
*av_restrict p,
 // read up to the first three bytes in p to enable reading a start code 
across
 // two (to four) buffers
 for (int i = 0; i < 3; i++) {
-*state <<= 8;
-*state += *p;
+*start_code <<= 8;
+*start_code += *p;
 p++;
-if (start_code_is_valid(*state) || p == end)
+if (start_code_is_valid(*start_code) || p == end)
 return p;
 }
 // p is now properly incremented for the negative indices in the while loop
@@ -1023,7 +1023,7 @@ const uint8_t *avpriv_find_start_code(const uint8_t 
*av_restrict p,
 if (p > end)
 p = end;
 // read the previous 4 bytes, i.e. bytes {p - 4, p - 3, p - 2, p - 1}
-*state = AV_RB32(p - 4);
+*start_code = AV_RB32(p - 4);
 return p;
 }
 
-- 
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-requ...@ffmpeg.org with subject "unsubscribe".


[FFmpeg-devel] [PATCH v4 4/8] avpriv_find_start_code(): rewrite for loop for clarity

2022-09-16 Thread Scott Theisen
---
 libavcodec/utils.c | 14 --
 1 file changed, 8 insertions(+), 6 deletions(-)

diff --git a/libavcodec/utils.c b/libavcodec/utils.c
index fc8cd87366..83f7d8a01a 100644
--- a/libavcodec/utils.c
+++ b/libavcodec/utils.c
@@ -983,18 +983,20 @@ const uint8_t *avpriv_find_start_code(const uint8_t 
*av_restrict p,
   const uint8_t *end,
   uint32_t *av_restrict state)
 {
-int i;
-
 av_assert0(p <= end);
 if (p >= end)
 return end;
 
-for (i = 0; i < 3; i++) {
-uint32_t tmp = *state << 8;
-*state = tmp + *(p++);
-if (tmp == 0x100 || p == end)
+// read up to the first three bytes in p to enable reading a start code 
across
+// two (to four) buffers
+for (int i = 0; i < 3; i++) {
+*state <<= 8;
+*state += *p;
+p++;
+if (start_code_is_valid(*state) || p == end)
 return p;
 }
+// p is now properly incremented for the negative indices in the while loop
 
 /* with memory address increasing left to right, we are looking for (in 
hexadecimal):
  * 00 00 01 XX
-- 
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-requ...@ffmpeg.org with subject "unsubscribe".


[FFmpeg-devel] [PATCH v4 3/8] avpriv_find_start_code(): rewrite while loop

2022-09-16 Thread Scott Theisen
The expected number of iterations may increase by one for an input
of alternating 0 and 1 bytes.  Instead of incrementing by 2 everytime,
it now alternates between incrementing by 1 and by 3.

For the check p[-2] != 0:
This slightly reduces the number of iterations by starting with three
new bytes on the next iteration, instead of keeping byte p[-3] which
is invalid, since it is now known to be 01 when it must be 00.

No other observable change.
---
 libavcodec/utils.c | 20 
 1 file changed, 16 insertions(+), 4 deletions(-)

diff --git a/libavcodec/utils.c b/libavcodec/utils.c
index d6ab21b1a0..fc8cd87366 100644
--- a/libavcodec/utils.c
+++ b/libavcodec/utils.c
@@ -996,12 +996,24 @@ const uint8_t *avpriv_find_start_code(const uint8_t 
*av_restrict p,
 return p;
 }
 
+/* with memory address increasing left to right, we are looking for (in 
hexadecimal):
+ * 00 00 01 XX
+ * p points at the address which should have the value of XX
+ */
 while (p < end) {
-if  (p[-1] > 1  ) p += 3;
-else if (p[-2]  ) p += 2;
-else if (p[-3]|(p[-1]-1)) p++;
-else {
+if  (/* UU UU UU */ p[-1]  < 1) { // equivalently p[-1] == 0
 p++;
+// could be in a start code, so check next byte
+}
+else if (/* UU UU UN */ p[-1]  > 1 ||
+ /* UU UU 01 */ p[-2] != 0 ||
+ /* UU 00 01 */ p[-3] != 0) {
+// start check over with 3 new bytes
+p += 3;
+}
+else { /* 00 00 01 */
+p++;
+// p now points at the address following the start code value XX
 break;
 }
 }
-- 
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-requ...@ffmpeg.org with subject "unsubscribe".


[FFmpeg-devel] [PATCH v4 2/8] avpriv_find_start_code(): readability enhancement part 1

2022-09-16 Thread Scott Theisen
No functional change.
---
 libavcodec/utils.c | 9 +
 1 file changed, 5 insertions(+), 4 deletions(-)

diff --git a/libavcodec/utils.c b/libavcodec/utils.c
index 2f57418ff7..d6ab21b1a0 100644
--- a/libavcodec/utils.c
+++ b/libavcodec/utils.c
@@ -1006,10 +1006,11 @@ const uint8_t *avpriv_find_start_code(const uint8_t 
*av_restrict p,
 }
 }
 
-p = FFMIN(p, end) - 4;
-*state = AV_RB32(p);
-
-return p + 4;
+if (p > end)
+p = end;
+// read the previous 4 bytes, i.e. bytes {p - 4, p - 3, p - 2, p - 1}
+*state = AV_RB32(p - 4);
+return p;
 }
 
 AVCPBProperties *av_cpb_properties_alloc(size_t *size)
-- 
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-requ...@ffmpeg.org with subject "unsubscribe".


[FFmpeg-devel] [PATCH v4 1/8] avcodec/startcode.h: create start_code_is_valid()

2022-09-16 Thread Scott Theisen
This slightly changes what is considered valid in the following cases:
cavsdec.c:   0x00XX is now considered invalid.
mpeg12dec.c: 0x00XX is now considered invalid.
(where X is any value)

IS_MARKER is equivalent since VC1_CODE_RES0 = 0x0100
---
 libavcodec/cavsdec.c   |  2 +-
 libavcodec/cbs_mpeg2.c |  6 +++---
 libavcodec/extract_extradata_bsf.c |  2 +-
 libavcodec/mpeg12.c|  2 +-
 libavcodec/mpeg12dec.c |  2 +-
 libavcodec/mpegvideo_parser.c  |  2 +-
 libavcodec/remove_extradata_bsf.c  |  8 +++-
 libavcodec/startcode.h | 17 +
 libavcodec/vaapi_vc1.c |  2 +-
 libavcodec/vc1_common.h|  4 +---
 libavcodec/vc1dec.c|  2 +-
 libavformat/avs2dec.c  |  4 ++--
 libavformat/avs3dec.c  |  4 ++--
 libavformat/cavsvideodec.c |  2 +-
 libavformat/mpegvideodec.c |  2 +-
 libavformat/rtpenc_mpv.c   |  2 +-
 16 files changed, 38 insertions(+), 25 deletions(-)

diff --git a/libavcodec/cavsdec.c b/libavcodec/cavsdec.c
index 3e8be65968..0c19d0e3f6 100644
--- a/libavcodec/cavsdec.c
+++ b/libavcodec/cavsdec.c
@@ -1257,7 +1257,7 @@ static int cavs_decode_frame(AVCodecContext *avctx, 
AVFrame *rframe,
 buf_end = buf + buf_size;
 for(;;) {
 buf_ptr = avpriv_find_start_code(buf_ptr, buf_end, );
-if ((stc & 0xFE00) || buf_ptr == buf_end) {
+if (!start_code_is_valid(stc) || buf_ptr == buf_end) {
 if (!h->stc)
 av_log(h->avctx, AV_LOG_WARNING, "no frame decoded\n");
 return FFMAX(0, buf_ptr - buf);
diff --git a/libavcodec/cbs_mpeg2.c b/libavcodec/cbs_mpeg2.c
index 04b0c7f87d..23839ca47b 100644
--- a/libavcodec/cbs_mpeg2.c
+++ b/libavcodec/cbs_mpeg2.c
@@ -150,7 +150,7 @@ static int cbs_mpeg2_split_fragment(CodedBitstreamContext 
*ctx,
 
 start = avpriv_find_start_code(frag->data, frag->data + frag->data_size,
_code);
-if (start_code >> 8 != 0x01) {
+if (!start_code_is_valid(start_code)) {
 // No start code found.
 return AVERROR_INVALIDDATA;
 }
@@ -172,7 +172,7 @@ static int cbs_mpeg2_split_fragment(CodedBitstreamContext 
*ctx,
 // (may be the last byte of fragment->data); end points to the byte
 // following the byte containing the start code identifier (or to
 // the end of fragment->data).
-if (start_code >> 8 == 0x01) {
+if (start_code_is_valid(start_code)) {
 // Unit runs from start to the beginning of the start code
 // pointed to by end (including any padding zeroes).
 unit_size = (end - 4) - start;
@@ -189,7 +189,7 @@ static int cbs_mpeg2_split_fragment(CodedBitstreamContext 
*ctx,
 start = end;
 
 // Do we have a further unit to add to the fragment?
-} while ((start_code >> 8) == 0x01);
+} while (start_code_is_valid(start_code));
 
 return 0;
 }
diff --git a/libavcodec/extract_extradata_bsf.c 
b/libavcodec/extract_extradata_bsf.c
index 329b1a6174..e698a2632e 100644
--- a/libavcodec/extract_extradata_bsf.c
+++ b/libavcodec/extract_extradata_bsf.c
@@ -239,7 +239,7 @@ static int extract_extradata_vc1(AVBSFContext *ctx, 
AVPacket *pkt,
 ptr = avpriv_find_start_code(ptr, end, );
 if (state == VC1_CODE_SEQHDR || state == VC1_CODE_ENTRYPOINT) {
 has_extradata = 1;
-} else if (has_extradata && IS_MARKER(state)) {
+} else if (has_extradata && start_code_is_valid(state)) {
 extradata_size = ptr - 4 - pkt->data;
 break;
 }
diff --git a/libavcodec/mpeg12.c b/libavcodec/mpeg12.c
index d78e25a777..5ff1830496 100644
--- a/libavcodec/mpeg12.c
+++ b/libavcodec/mpeg12.c
@@ -214,7 +214,7 @@ int ff_mpeg1_find_frame_end(ParseContext *pc, const uint8_t 
*buf, int buf_size,
 pc->frame_start_found = 0;
 if (pc->frame_start_found  < 4 && state == EXT_START_CODE)
 pc->frame_start_found++;
-if (pc->frame_start_found == 4 && (state & 0xFF00) == 0x100) {
+if (pc->frame_start_found == 4 && start_code_is_valid(state)) {
 if (state < SLICE_MIN_START_CODE || state > 
SLICE_MAX_START_CODE) {
 pc->frame_start_found = 0;
 pc->state = -1;
diff --git a/libavcodec/mpeg12dec.c b/libavcodec/mpeg12dec.c
index 7133696f3c..b857353ab8 100644
--- a/libavcodec/mpeg12dec.c
+++ b/libavcodec/mpeg12dec.c
@@ -2446,7 +2446,7 @@ static int decode_chunks(AVCodecContext *avctx, AVFrame 
*picture,
 /* find next start code */
 uint32_t start_code = -1;
 buf_ptr = avpriv_find_start_code(buf_ptr, buf_end, _code);
-if (start_code > 0x1ff) {
+if (!start_code_is_valid(start_code)) {
 if (!skip_frame) {
 if (HAVE_THREADS &&
 

[FFmpeg-devel] [PATCH v4 0/8] rewrite avpriv_find_start_code() for clarity

2022-09-16 Thread Scott Theisen
My modified versions of avpriv_find_start_code were accepted into MythTV,
which caused clang-tidy to raise a bugprone-branch-clone warning.  The
for loop's if statement has therefore been modified to not duplicate
the branch payload.

There are no other changes from v3.

Regards,

Scott Theisen



___
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] cbs_mpeg2_split_fragment(): cache the buffer end

2022-09-16 Thread Scott Theisen

Ping; it still applies cleanly.

-Scott Theisen

On 5/15/22 14:50, Scott Theisen wrote:

Ping; it still applies cleanly.

-Scott Theisen

On 2/8/22 15:32, Scott Theisen wrote:

Also add a few clarifying comments.
---
  libavcodec/cbs_mpeg2.c | 16 
  1 file changed, 8 insertions(+), 8 deletions(-)

diff --git a/libavcodec/cbs_mpeg2.c b/libavcodec/cbs_mpeg2.c
index 33bd3e0998..47732562d1 100644
--- a/libavcodec/cbs_mpeg2.c
+++ b/libavcodec/cbs_mpeg2.c
@@ -144,12 +144,12 @@ static int 
cbs_mpeg2_split_fragment(CodedBitstreamContext *ctx,

  CodedBitstreamFragment *frag,
  int header)
  {
-    const uint8_t *start;
+    const uint8_t *start = frag->data;
+    const uint8_t * const buf_end = frag->data + frag->data_size;
  uint32_t start_code = -1;
  int err;
  -    start = avpriv_find_start_code(frag->data, frag->data + 
frag->data_size,

-   _code);
+    start = avpriv_find_start_code(start, buf_end, _code);
  if (start_code >> 8 != 0x01) {
  // No start code found.
  return AVERROR_INVALIDDATA;
@@ -165,12 +165,11 @@ static int 
cbs_mpeg2_split_fragment(CodedBitstreamContext *ctx,

  // start code in any way (as e.g. happens when there is a
  // Sequence End unit at the very end of a packet).
  start_code = UINT32_MAX;
-    end = avpriv_find_start_code(start--, frag->data + 
frag->data_size,

- _code);
-
-    // start points to the byte containing the 
start_code_identifier

+    end = avpriv_find_start_code(start, buf_end, _code);
+    start--;
+    // decrement so start points to the byte containing the 
start_code_identifier
  // (may be the last byte of fragment->data); end points to 
the byte
-    // following the byte containing the start code identifier 
(or to
+    // following the byte containing the next start code 
identifier (or to

  // the end of fragment->data).
  if (start_code >> 8 == 0x01) {
  // Unit runs from start to the beginning of the start code
@@ -178,6 +177,7 @@ static int 
cbs_mpeg2_split_fragment(CodedBitstreamContext *ctx,

  unit_size = (end - 4) - start;
  } else {
 // We didn't find a start code, so this is the final unit.
+   // There is no start code to remove from end, hence not 
(end - 4).

 unit_size = end - start;
  }




___
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".


[FFmpeg-devel] [PATCH v2] av_get_media_type_string(): replace with av_media_type_get_string()

2022-09-16 Thread Scott Theisen
printf %s with a NULL pointer is undefined behavior.  Not returning
a NULL pointer makes for a nicer function for API users, including
in FFmpeg, since then you don't have to check it, instead you can
just print it.  32/44 uses in FFmpeg did not check for NULL.

This also matches behavior with avcodec_get_name() which never
returns NULL, instead it returns "unknown_codec".

In libavcodec/codec_desc.c, avcodec_get_type(AV_CODEC_ID_NONE) will
return AVMEDIA_TYPE_UNKNOWN.

The mpegts demuxer, for example, will set AV_CODEC_ID_NONE as the
AVCodecID of a stream if it can't identify the codec.  This is
probably true of all demuxers.

Therefore, AVMEDIA_TYPE_UNKNOWN is a valid value for the AVMediaType
enum and this should be differentiated from invalid values by
returning a different string for each case.
---
 doc/APIchanges   |  3 +++
 doc/examples/demuxing_decoding.c | 10 +-
 doc/examples/extract_mvs.c   |  4 ++--
 fftools/ffmpeg.c | 10 +-
 fftools/ffmpeg_demux.c   |  4 ++--
 fftools/ffmpeg_mux.c |  2 +-
 fftools/ffmpeg_opt.c |  2 +-
 fftools/ffplay.c |  4 ++--
 fftools/ffprobe.c| 13 +++--
 fftools/opt_common.c |  7 +++
 libavcodec/avcodec.c |  2 +-
 libavcodec/tests/avcodec.c   | 10 ++
 libavdevice/dshow.c  |  4 ++--
 libavfilter/avfilter.c   |  4 ++--
 libavfilter/avfiltergraph.c  |  4 ++--
 libavfilter/src_movie.c  |  6 +++---
 libavformat/avienc.c |  2 +-
 libavformat/flvenc.c |  2 +-
 libavformat/framehash.c  |  2 +-
 libavformat/mov.c|  2 +-
 libavformat/mpegenc.c|  2 +-
 libavformat/mpegts.c |  2 +-
 libavformat/mxfdec.c |  2 +-
 libavformat/segment.c|  2 +-
 libavformat/tee.c|  2 +-
 libavformat/uncodedframecrcenc.c |  4 +---
 libavutil/avutil.h   | 10 ++
 libavutil/utils.c| 13 +
 28 files changed, 72 insertions(+), 62 deletions(-)

diff --git a/doc/APIchanges b/doc/APIchanges
index 729f56be7b..56d64dbcef 100644
--- a/doc/APIchanges
+++ b/doc/APIchanges
@@ -14,6 +14,9 @@ libavutil: 2021-04-27
 
 API changes, most recent first:
 
+2022-09-XX - xx - lavu 57.19.100 - avutil.h
+  Add av_media_type_get_string() which deprecates av_get_media_type_string().
+
 2022-09-03 - xx - lavu 57.36.100 - pixfmt.h
   Add AV_PIX_FMT_P012, AV_PIX_FMT_Y212, AV_PIX_FMT_XV30, AV_PIX_FMT_XV36
 
diff --git a/doc/examples/demuxing_decoding.c b/doc/examples/demuxing_decoding.c
index 999a78db0d..467eda8a51 100644
--- a/doc/examples/demuxing_decoding.c
+++ b/doc/examples/demuxing_decoding.c
@@ -155,7 +155,7 @@ static int open_codec_context(int *stream_idx,
 ret = av_find_best_stream(fmt_ctx, type, -1, -1, NULL, 0);
 if (ret < 0) {
 fprintf(stderr, "Could not find %s stream in input file '%s'\n",
-av_get_media_type_string(type), src_filename);
+av_media_type_get_string(type), src_filename);
 return ret;
 } else {
 stream_index = ret;
@@ -165,7 +165,7 @@ static int open_codec_context(int *stream_idx,
 dec = avcodec_find_decoder(st->codecpar->codec_id);
 if (!dec) {
 fprintf(stderr, "Failed to find %s codec\n",
-av_get_media_type_string(type));
+av_media_type_get_string(type));
 return AVERROR(EINVAL);
 }
 
@@ -173,21 +173,21 @@ static int open_codec_context(int *stream_idx,
 *dec_ctx = avcodec_alloc_context3(dec);
 if (!*dec_ctx) {
 fprintf(stderr, "Failed to allocate the %s codec context\n",
-av_get_media_type_string(type));
+av_media_type_get_string(type));
 return AVERROR(ENOMEM);
 }
 
 /* Copy codec parameters from input stream to output codec context */
 if ((ret = avcodec_parameters_to_context(*dec_ctx, st->codecpar)) < 0) 
{
 fprintf(stderr, "Failed to copy %s codec parameters to decoder 
context\n",
-av_get_media_type_string(type));
+av_media_type_get_string(type));
 return ret;
 }
 
 /* Init the decoders */
 if ((ret = avcodec_open2(*dec_ctx, dec, NULL)) < 0) {
 fprintf(stderr, "Failed to open %s codec\n",
-av_get_media_type_string(type));
+av_media_type_get_string(type));
 return ret;
 }
 *stream_idx = stream_index;
diff --git a/doc/examples/extract_mvs.c b/doc/examples/extract_mvs.c
index cc1311da91..2fb5517708 100644
--- a/doc/examples/extract_mvs.c
+++ b/doc/examples/extract_mvs.c
@@ -85,7 +85,7 @@ static int open_codec_context(AVFormatContext *fmt_ctx, enum 
AVMediaType type)
 ret = av_find_best_stream(fmt_ctx, type, -1, -1, 

[FFmpeg-devel] [PATCH] av_get_media_type_string(): replace with av_media_type_get_string()

2022-06-01 Thread Scott Theisen
printf %s with a NULL pointer is undefined behavior.  Not returning
a NULL pointer makes for a nicer function for API users, including
in FFmpeg, since then you don't have to check it, instead you can
just print it.  32/44 uses in FFmpeg did not check for NULL.

This also matches behavior with avcodec_get_name() which never
returns NULL, instead it returns "unknown_codec".

In libavcodec/codec_desc.c, avcodec_get_type(AV_CODEC_ID_NONE) will
return AVMEDIA_TYPE_UNKNOWN.

The mpegts demuxer, for example, will set AV_CODEC_ID_NONE as the
AVCodecID of a stream if it can't identify the codec.  This is
probably true of all demuxers.

Therefore, AVMEDIA_TYPE_UNKNOWN is a valid value for the AVMediaType
enum and this should be differentiated from invalid values by
returning a different string for each case.
---
 doc/APIchanges   |  3 +++
 doc/examples/demuxing_decoding.c | 10 +-
 doc/examples/extract_mvs.c   |  4 ++--
 fftools/ffmpeg.c | 14 +++---
 fftools/ffmpeg_mux.c |  2 +-
 fftools/ffmpeg_opt.c |  2 +-
 fftools/ffplay.c |  4 ++--
 fftools/ffprobe.c| 13 +++--
 fftools/opt_common.c |  7 +++
 libavcodec/avcodec.c |  2 +-
 libavcodec/tests/avcodec.c   | 10 ++
 libavdevice/dshow.c  |  4 ++--
 libavfilter/avfilter.c   |  4 ++--
 libavfilter/avfiltergraph.c  |  4 ++--
 libavfilter/src_movie.c  |  6 +++---
 libavformat/avienc.c |  2 +-
 libavformat/flvenc.c |  2 +-
 libavformat/framehash.c  |  2 +-
 libavformat/mov.c|  2 +-
 libavformat/mpegenc.c|  2 +-
 libavformat/mpegts.c |  2 +-
 libavformat/mxfdec.c |  2 +-
 libavformat/segment.c|  2 +-
 libavformat/tee.c|  2 +-
 libavformat/uncodedframecrcenc.c |  4 +---
 libavutil/avutil.h   | 10 ++
 libavutil/utils.c| 13 +
 27 files changed, 72 insertions(+), 62 deletions(-)

diff --git a/doc/APIchanges b/doc/APIchanges
index 337f1466d8..f3ed86b9b2 100644
--- a/doc/APIchanges
+++ b/doc/APIchanges
@@ -14,6 +14,9 @@ libavutil: 2021-04-27
 
 API changes, most recent first:
 
+2022-06-XX - xxx - lavu 57.19.100 - avutil.h
+  Add av_media_type_get_string() which deprecates av_get_media_type_string().
+
 2022-05-23 - x - lavu 57.25.100 - avutil.h
   Deprecate av_fopen_utf8() without replacement.
 
diff --git a/doc/examples/demuxing_decoding.c b/doc/examples/demuxing_decoding.c
index 999a78db0d..467eda8a51 100644
--- a/doc/examples/demuxing_decoding.c
+++ b/doc/examples/demuxing_decoding.c
@@ -155,7 +155,7 @@ static int open_codec_context(int *stream_idx,
 ret = av_find_best_stream(fmt_ctx, type, -1, -1, NULL, 0);
 if (ret < 0) {
 fprintf(stderr, "Could not find %s stream in input file '%s'\n",
-av_get_media_type_string(type), src_filename);
+av_media_type_get_string(type), src_filename);
 return ret;
 } else {
 stream_index = ret;
@@ -165,7 +165,7 @@ static int open_codec_context(int *stream_idx,
 dec = avcodec_find_decoder(st->codecpar->codec_id);
 if (!dec) {
 fprintf(stderr, "Failed to find %s codec\n",
-av_get_media_type_string(type));
+av_media_type_get_string(type));
 return AVERROR(EINVAL);
 }
 
@@ -173,21 +173,21 @@ static int open_codec_context(int *stream_idx,
 *dec_ctx = avcodec_alloc_context3(dec);
 if (!*dec_ctx) {
 fprintf(stderr, "Failed to allocate the %s codec context\n",
-av_get_media_type_string(type));
+av_media_type_get_string(type));
 return AVERROR(ENOMEM);
 }
 
 /* Copy codec parameters from input stream to output codec context */
 if ((ret = avcodec_parameters_to_context(*dec_ctx, st->codecpar)) < 0) 
{
 fprintf(stderr, "Failed to copy %s codec parameters to decoder 
context\n",
-av_get_media_type_string(type));
+av_media_type_get_string(type));
 return ret;
 }
 
 /* Init the decoders */
 if ((ret = avcodec_open2(*dec_ctx, dec, NULL)) < 0) {
 fprintf(stderr, "Failed to open %s codec\n",
-av_get_media_type_string(type));
+av_media_type_get_string(type));
 return ret;
 }
 *stream_idx = stream_index;
diff --git a/doc/examples/extract_mvs.c b/doc/examples/extract_mvs.c
index cc1311da91..2fb5517708 100644
--- a/doc/examples/extract_mvs.c
+++ b/doc/examples/extract_mvs.c
@@ -85,7 +85,7 @@ static int open_codec_context(AVFormatContext *fmt_ctx, enum 
AVMediaType type)
 ret = av_find_best_stream(fmt_ctx, type, -1, -1, , 0);
 if (ret < 0) {
 fprintf(stderr, "Could not 

[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)) {

[FFmpeg-devel] [PATCH v4 2/2] av_get_media_type_string(): replace with av_media_type_get_string()

2022-05-15 Thread Scott Theisen
printf %s with a null pointer is undefined behavior.
---
 doc/examples/demuxing_decoding.c | 10 +-
 doc/examples/extract_mvs.c   |  4 ++--
 fftools/ffmpeg.c | 12 ++--
 fftools/ffmpeg_mux.c |  2 +-
 fftools/ffmpeg_opt.c |  2 +-
 fftools/ffplay.c |  4 ++--
 fftools/ffprobe.c| 13 +++--
 fftools/opt_common.c |  7 +++
 libavcodec/avcodec.c |  2 +-
 libavcodec/tests/avcodec.c   | 10 ++
 libavdevice/dshow.c  |  4 ++--
 libavfilter/avfilter.c   |  4 ++--
 libavfilter/avfiltergraph.c  |  4 ++--
 libavfilter/src_movie.c  |  6 +++---
 libavformat/avienc.c |  2 +-
 libavformat/flvenc.c |  2 +-
 libavformat/framehash.c  |  2 +-
 libavformat/mov.c|  2 +-
 libavformat/mpegenc.c|  2 +-
 libavformat/mpegts.c |  2 +-
 libavformat/mxfdec.c |  2 +-
 libavformat/segment.c|  2 +-
 libavformat/tee.c|  2 +-
 libavformat/uncodedframecrcenc.c |  4 +---
 24 files changed, 45 insertions(+), 61 deletions(-)

diff --git a/doc/examples/demuxing_decoding.c b/doc/examples/demuxing_decoding.c
index 999a78db0d..467eda8a51 100644
--- a/doc/examples/demuxing_decoding.c
+++ b/doc/examples/demuxing_decoding.c
@@ -155,7 +155,7 @@ static int open_codec_context(int *stream_idx,
 ret = av_find_best_stream(fmt_ctx, type, -1, -1, NULL, 0);
 if (ret < 0) {
 fprintf(stderr, "Could not find %s stream in input file '%s'\n",
-av_get_media_type_string(type), src_filename);
+av_media_type_get_string(type), src_filename);
 return ret;
 } else {
 stream_index = ret;
@@ -165,7 +165,7 @@ static int open_codec_context(int *stream_idx,
 dec = avcodec_find_decoder(st->codecpar->codec_id);
 if (!dec) {
 fprintf(stderr, "Failed to find %s codec\n",
-av_get_media_type_string(type));
+av_media_type_get_string(type));
 return AVERROR(EINVAL);
 }
 
@@ -173,21 +173,21 @@ static int open_codec_context(int *stream_idx,
 *dec_ctx = avcodec_alloc_context3(dec);
 if (!*dec_ctx) {
 fprintf(stderr, "Failed to allocate the %s codec context\n",
-av_get_media_type_string(type));
+av_media_type_get_string(type));
 return AVERROR(ENOMEM);
 }
 
 /* Copy codec parameters from input stream to output codec context */
 if ((ret = avcodec_parameters_to_context(*dec_ctx, st->codecpar)) < 0) 
{
 fprintf(stderr, "Failed to copy %s codec parameters to decoder 
context\n",
-av_get_media_type_string(type));
+av_media_type_get_string(type));
 return ret;
 }
 
 /* Init the decoders */
 if ((ret = avcodec_open2(*dec_ctx, dec, NULL)) < 0) {
 fprintf(stderr, "Failed to open %s codec\n",
-av_get_media_type_string(type));
+av_media_type_get_string(type));
 return ret;
 }
 *stream_idx = stream_index;
diff --git a/doc/examples/extract_mvs.c b/doc/examples/extract_mvs.c
index cc1311da91..2fb5517708 100644
--- a/doc/examples/extract_mvs.c
+++ b/doc/examples/extract_mvs.c
@@ -85,7 +85,7 @@ static int open_codec_context(AVFormatContext *fmt_ctx, enum 
AVMediaType type)
 ret = av_find_best_stream(fmt_ctx, type, -1, -1, , 0);
 if (ret < 0) {
 fprintf(stderr, "Could not find %s stream in input file '%s'\n",
-av_get_media_type_string(type), src_filename);
+av_media_type_get_string(type), src_filename);
 return ret;
 } else {
 int stream_idx = ret;
@@ -109,7 +109,7 @@ static int open_codec_context(AVFormatContext *fmt_ctx, 
enum AVMediaType type)
 av_dict_free();
 if (ret < 0) {
 fprintf(stderr, "Failed to open %s codec\n",
-av_get_media_type_string(type));
+av_media_type_get_string(type));
 return ret;
 }
 
diff --git a/fftools/ffmpeg.c b/fftools/ffmpeg.c
index a85ed18b08..0382c80bd1 100644
--- a/fftools/ffmpeg.c
+++ b/fftools/ffmpeg.c
@@ -1447,7 +1447,7 @@ static void print_final_stats(int64_t total_size)
 total_packets += ist->nb_packets;
 
 av_log(NULL, AV_LOG_VERBOSE, "  Input stream #%d:%d (%s): ",
-   i, j, av_get_media_type_string(type));
+   i, j, av_media_type_get_string(type));
 av_log(NULL, AV_LOG_VERBOSE, "%"PRIu64" packets read (%"PRIu64" 
bytes); ",
ist->nb_packets, ist->data_size);
 
@@ -1481,7 +1481,7 @@ static void print_final_stats(int64_t total_size)
 total_packets += ost->packets_written;
 
 av_log(NULL, AV_LOG_VERBOSE, "  Output 

[FFmpeg-devel] [PATCH v4 1/2] libavutil: create av_media_type_get_string()

2022-05-15 Thread Scott Theisen
This deprecates av_get_media_type_string() which does return NULL.

printf %s with a null pointer is undefined behavior

This also matches behavior with avcodec_get_name() which never returns NULL,
instead it returns "unknown_codec".
---
 doc/APIchanges |  3 +++
 libavutil/avutil.h | 10 ++
 libavutil/utils.c  | 13 +
 3 files changed, 26 insertions(+)

diff --git a/doc/APIchanges b/doc/APIchanges
index 1a9f0a303e..53f4378a50 100644
--- a/doc/APIchanges
+++ b/doc/APIchanges
@@ -14,6 +14,9 @@ libavutil: 2021-04-27
 
 API changes, most recent first:
 
+2022-05-XX - xxx - lavu 57.19.100 - avutil.h
+  Add av_media_type_get_string() which deprecates av_get_media_type_string().
+
 2022-03-16 - xx - all libraries - version_major.h
   Add lib/version_major.h as new installed headers, which only
   contain the major version number (and corresponding API deprecation
diff --git a/libavutil/avutil.h b/libavutil/avutil.h
index 4d633156d1..0f03080dcb 100644
--- a/libavutil/avutil.h
+++ b/libavutil/avutil.h
@@ -21,6 +21,8 @@
 #ifndef AVUTIL_AVUTIL_H
 #define AVUTIL_AVUTIL_H
 
+#include "attributes.h"
+
 /**
  * @file
  * @ingroup lavu
@@ -209,9 +211,17 @@ enum AVMediaType {
 /**
  * Return a string describing the media_type enum, NULL if media_type
  * is unknown.
+ *
+ * @deprecated Use av_media_type_get_string() instead.
  */
+attribute_deprecated
 const char *av_get_media_type_string(enum AVMediaType media_type);
 
+/**
+ * Return a string describing the media_type enum, never NULL.
+ */
+const char *av_media_type_get_string(enum AVMediaType media_type);
+
 /**
  * @defgroup lavu_const Constants
  * @{
diff --git a/libavutil/utils.c b/libavutil/utils.c
index 94d247bbee..5e0be8361f 100644
--- a/libavutil/utils.c
+++ b/libavutil/utils.c
@@ -37,6 +37,19 @@ const char *av_get_media_type_string(enum AVMediaType 
media_type)
 }
 }
 
+const char *av_media_type_get_string(enum AVMediaType media_type)
+{
+switch (media_type) {
+case AVMEDIA_TYPE_UNKNOWN:return "unknown";
+case AVMEDIA_TYPE_VIDEO:  return "video";
+case AVMEDIA_TYPE_AUDIO:  return "audio";
+case AVMEDIA_TYPE_DATA:   return "data";
+case AVMEDIA_TYPE_SUBTITLE:   return "subtitle";
+case AVMEDIA_TYPE_ATTACHMENT: return "attachment";
+default:  return "invalid_media_type";
+}
+}
+
 char av_get_picture_type_char(enum AVPictureType pict_type)
 {
 switch (pict_type) {
-- 
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-requ...@ffmpeg.org with subject "unsubscribe".


[FFmpeg-devel] [Patch v4 0/2] create av_media_type_get_string()

2022-05-15 Thread Scott Theisen
printf %s with NULL is undefined behavior.

This version adds #include "attributes.h" for -Wdeprecated-declarations.

-Scott Theisen


___
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] cbs_mpeg2_split_fragment(): cache the buffer end

2022-05-15 Thread Scott Theisen

Ping; it still applies cleanly.

-Scott Theisen

On 2/8/22 15:32, Scott Theisen wrote:

Also add a few clarifying comments.
---
  libavcodec/cbs_mpeg2.c | 16 
  1 file changed, 8 insertions(+), 8 deletions(-)

diff --git a/libavcodec/cbs_mpeg2.c b/libavcodec/cbs_mpeg2.c
index 33bd3e0998..47732562d1 100644
--- a/libavcodec/cbs_mpeg2.c
+++ b/libavcodec/cbs_mpeg2.c
@@ -144,12 +144,12 @@ static int cbs_mpeg2_split_fragment(CodedBitstreamContext 
*ctx,
  CodedBitstreamFragment *frag,
  int header)
  {
-const uint8_t *start;
+const uint8_t *start = frag->data;
+const uint8_t * const buf_end = frag->data + frag->data_size;
  uint32_t start_code = -1;
  int err;
  
-start = avpriv_find_start_code(frag->data, frag->data + frag->data_size,

-   _code);
+start = avpriv_find_start_code(start, buf_end, _code);
  if (start_code >> 8 != 0x01) {
  // No start code found.
  return AVERROR_INVALIDDATA;
@@ -165,12 +165,11 @@ static int cbs_mpeg2_split_fragment(CodedBitstreamContext 
*ctx,
  // start code in any way (as e.g. happens when there is a
  // Sequence End unit at the very end of a packet).
  start_code = UINT32_MAX;
-end = avpriv_find_start_code(start--, frag->data + frag->data_size,
- _code);
-
-// start points to the byte containing the start_code_identifier
+end = avpriv_find_start_code(start, buf_end, _code);
+start--;
+// decrement so start points to the byte containing the 
start_code_identifier
  // (may be the last byte of fragment->data); end points to the byte
-// following the byte containing the start code identifier (or to
+// following the byte containing the next start code identifier (or to
  // the end of fragment->data).
  if (start_code >> 8 == 0x01) {
  // Unit runs from start to the beginning of the start code
@@ -178,6 +177,7 @@ static int cbs_mpeg2_split_fragment(CodedBitstreamContext 
*ctx,
  unit_size = (end - 4) - start;
  } else {
 // We didn't find a start code, so this is the final unit.
+   // There is no start code to remove from end, hence not (end - 4).
 unit_size = end - start;
  }
  


___
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 v3 0/8] rewrite avpriv_find_start_code() for clarity

2022-03-07 Thread Scott Theisen

On 2/8/22 22:28, Scott Theisen wrote:

I have removed most of my added comments.

Rebased to account for Andreas Rheinhardt moving the prototype to
startcode.h.

Per his suggestion, the function signature is now unmodified.
(ff_)mpeg1_find_frame_end() now has a wrapper function preserving
the original behavior allowing start codes across buffer boundaries.

-Scott



Andreas,

Have you had a chance to look at v3?

Thanks,

Scott
___
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] lavu: make av_get_media_type_string() never return NULL

2022-02-28 Thread Scott Theisen

On 2/23/22 02:52, Anton Khirnov wrote:

Quoting Andreas Rheinhardt (2022-02-02 03:13:12)

Who sets invalid media types?
(In case of fftools/*: If they don't set it themselves, they (and all
our users) should be able to rely on our libraries to not set invalid
values. The same goes for all demuxers (as they set this value
themselves). Checks are only necessary where the media type comes from
the user; this means muxers (where the check should be done generically)
and possibly avfiltergraph.c (I am pretty sure that the type has already
been checked earlier for filters).)

Some demuxer code in lavf looks like it might result in a TYPE_UNKNOWN
AVStream. E.g. argo_brp, avi, both asfdecs, gxf.


Also, in libavcodec/codec_desc.c, avcodec_get_type(AV_CODEC_ID_NONE) 
will return AVMEDIA_TYPE_UNKNOWN.  The mpegts demuxer, for example, will 
set (or more likely leave) AV_CODEC_ID_NONE as the AVCodecID of a stream 
if it can't identify the codec.  This is probably true of all demuxers.


-Scott Theisen
___
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".


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

2022-02-13 Thread 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 
+
 #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 = 

[FFmpeg-devel] [PATCH v3 2/2] av_get_media_type_string(): replace with av_media_type_get_string()

2022-02-13 Thread Scott Theisen
printf %s with a null pointer is undefined behavior.
---
 doc/examples/demuxing_decoding.c | 10 +-
 doc/examples/extract_mvs.c   |  4 ++--
 fftools/cmdutils.c   |  7 +++
 fftools/cmdutils.h   |  2 --
 fftools/ffmpeg.c | 14 +++---
 fftools/ffmpeg_opt.c |  2 +-
 fftools/ffplay.c |  4 ++--
 fftools/ffprobe.c| 13 +++--
 libavcodec/avcodec.c |  2 +-
 libavcodec/tests/avcodec.c   | 10 ++
 libavdevice/dshow.c  |  4 ++--
 libavfilter/avfilter.c   |  4 ++--
 libavfilter/avfiltergraph.c  |  4 ++--
 libavfilter/src_movie.c  |  6 +++---
 libavformat/avienc.c |  2 +-
 libavformat/flvenc.c |  2 +-
 libavformat/framehash.c  |  2 +-
 libavformat/mov.c|  2 +-
 libavformat/mpegenc.c|  2 +-
 libavformat/mpegts.c |  2 +-
 libavformat/mxfdec.c |  2 +-
 libavformat/segment.c|  2 +-
 libavformat/tee.c|  2 +-
 libavformat/uncodedframecrcenc.c |  4 +---
 24 files changed, 45 insertions(+), 63 deletions(-)

diff --git a/doc/examples/demuxing_decoding.c b/doc/examples/demuxing_decoding.c
index 8520d5b660..670f08779d 100644
--- a/doc/examples/demuxing_decoding.c
+++ b/doc/examples/demuxing_decoding.c
@@ -155,7 +155,7 @@ static int open_codec_context(int *stream_idx,
 ret = av_find_best_stream(fmt_ctx, type, -1, -1, NULL, 0);
 if (ret < 0) {
 fprintf(stderr, "Could not find %s stream in input file '%s'\n",
-av_get_media_type_string(type), src_filename);
+av_media_type_get_string(type), src_filename);
 return ret;
 } else {
 stream_index = ret;
@@ -165,7 +165,7 @@ static int open_codec_context(int *stream_idx,
 dec = avcodec_find_decoder(st->codecpar->codec_id);
 if (!dec) {
 fprintf(stderr, "Failed to find %s codec\n",
-av_get_media_type_string(type));
+av_media_type_get_string(type));
 return AVERROR(EINVAL);
 }
 
@@ -173,21 +173,21 @@ static int open_codec_context(int *stream_idx,
 *dec_ctx = avcodec_alloc_context3(dec);
 if (!*dec_ctx) {
 fprintf(stderr, "Failed to allocate the %s codec context\n",
-av_get_media_type_string(type));
+av_media_type_get_string(type));
 return AVERROR(ENOMEM);
 }
 
 /* Copy codec parameters from input stream to output codec context */
 if ((ret = avcodec_parameters_to_context(*dec_ctx, st->codecpar)) < 0) 
{
 fprintf(stderr, "Failed to copy %s codec parameters to decoder 
context\n",
-av_get_media_type_string(type));
+av_media_type_get_string(type));
 return ret;
 }
 
 /* Init the decoders */
 if ((ret = avcodec_open2(*dec_ctx, dec, NULL)) < 0) {
 fprintf(stderr, "Failed to open %s codec\n",
-av_get_media_type_string(type));
+av_media_type_get_string(type));
 return ret;
 }
 *stream_idx = stream_index;
diff --git a/doc/examples/extract_mvs.c b/doc/examples/extract_mvs.c
index cc1311da91..2fb5517708 100644
--- a/doc/examples/extract_mvs.c
+++ b/doc/examples/extract_mvs.c
@@ -85,7 +85,7 @@ static int open_codec_context(AVFormatContext *fmt_ctx, enum 
AVMediaType type)
 ret = av_find_best_stream(fmt_ctx, type, -1, -1, , 0);
 if (ret < 0) {
 fprintf(stderr, "Could not find %s stream in input file '%s'\n",
-av_get_media_type_string(type), src_filename);
+av_media_type_get_string(type), src_filename);
 return ret;
 } else {
 int stream_idx = ret;
@@ -109,7 +109,7 @@ static int open_codec_context(AVFormatContext *fmt_ctx, 
enum AVMediaType type)
 av_dict_free();
 if (ret < 0) {
 fprintf(stderr, "Failed to open %s codec\n",
-av_get_media_type_string(type));
+av_media_type_get_string(type));
 return ret;
 }
 
diff --git a/fftools/cmdutils.c b/fftools/cmdutils.c
index 4b50e15eef..4504d2711c 100644
--- a/fftools/cmdutils.c
+++ b/fftools/cmdutils.c
@@ -1961,7 +1961,7 @@ static void show_help_filter(const char *name)
 count = avfilter_filter_pad_count(f, 0);
 for (i = 0; i < count; i++) {
 printf("   #%d: %s (%s)\n", i, avfilter_pad_get_name(f->inputs, i),
-   media_type_string(avfilter_pad_get_type(f->inputs, i)));
+   av_media_type_get_string(avfilter_pad_get_type(f->inputs, i)));
 }
 if (f->flags & AVFILTER_FLAG_DYNAMIC_INPUTS)
 printf("dynamic (depending on the options)\n");
@@ -1972,7 +1972,7 @@ static void show_help_filter(const char *name)
 count = avfilter_filter_pad_count(f, 1);
 

[FFmpeg-devel] [PATCH v3 1/2] libavutil: create av_media_type_get_string()

2022-02-13 Thread Scott Theisen
This deprecates av_get_media_type_string() which does return NULL.

printf %s with a null pointer is undefined behavior

This also matches behavior with avcodec_get_name() which never returns NULL,
instead it returns "unknown_codec".
---
 doc/APIchanges |  3 +++
 libavutil/avutil.h |  7 +++
 libavutil/utils.c  | 13 +
 3 files changed, 23 insertions(+)

diff --git a/doc/APIchanges b/doc/APIchanges
index ea402f6118..7733de8dea 100644
--- a/doc/APIchanges
+++ b/doc/APIchanges
@@ -14,6 +14,9 @@ libavutil: 2021-04-27
 
 API changes, most recent first:
 
+2022-02-XX - xxx - lavu 57.19.100 - avutil.h
+  Add av_media_type_get_string() which deprecates av_get_media_type_string().
+
 2022-02-07 - xx - lavu 57.21.100 - fifo.h
   Deprecate AVFifoBuffer and the API around it, namely av_fifo_alloc(),
   av_fifo_alloc_array(), av_fifo_free(), av_fifo_freep(), av_fifo_reset(),
diff --git a/libavutil/avutil.h b/libavutil/avutil.h
index 4d633156d1..6cabeb405a 100644
--- a/libavutil/avutil.h
+++ b/libavutil/avutil.h
@@ -209,9 +209,16 @@ enum AVMediaType {
 /**
  * Return a string describing the media_type enum, NULL if media_type
  * is unknown.
+ *
+ * @deprecated Use av_media_type_get_string() instead.
  */
 const char *av_get_media_type_string(enum AVMediaType media_type);
 
+/**
+ * Return a string describing the media_type enum, never NULL.
+ */
+const char *av_media_type_get_string(enum AVMediaType media_type);
+
 /**
  * @defgroup lavu_const Constants
  * @{
diff --git a/libavutil/utils.c b/libavutil/utils.c
index ea9b5097b8..92d242f678 100644
--- a/libavutil/utils.c
+++ b/libavutil/utils.c
@@ -80,6 +80,19 @@ const char *av_get_media_type_string(enum AVMediaType 
media_type)
 }
 }
 
+const char *av_media_type_get_string(enum AVMediaType media_type)
+{
+switch (media_type) {
+case AVMEDIA_TYPE_UNKNOWN:return "unknown";
+case AVMEDIA_TYPE_VIDEO:  return "video";
+case AVMEDIA_TYPE_AUDIO:  return "audio";
+case AVMEDIA_TYPE_DATA:   return "data";
+case AVMEDIA_TYPE_SUBTITLE:   return "subtitle";
+case AVMEDIA_TYPE_ATTACHMENT: return "attachment";
+default:  return "invalid_media_type";
+}
+}
+
 char av_get_picture_type_char(enum AVPictureType pict_type)
 {
 switch (pict_type) {
-- 
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-requ...@ffmpeg.org with subject "unsubscribe".


[FFmpeg-devel] [PATCH v3] create av_media_type_get_string()

2022-02-13 Thread Scott Theisen
Identical to v2, but I reworded the second commit's message to add a context.


___
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".


[FFmpeg-devel] [PATCH v3 8/8] avpriv_find_start_code(): make start_code output only

2022-02-08 Thread Scott Theisen
The input/output functionality was used by only
(ff_)mpeg1_find_frame_end().

If the state/start_code input is a local variable and only one buffer is used,
then no history is needed.

In loops and inline functions: if ignoring history, don't initialize start_code,
so it isn't reset twice each time.

There is a slight functional change:
00 00 01 00 01 XX no longer incorrectly returns a start code at
offset 7 that overlaps the start code at offset 4 if the start_code
input is not modified between the two calls.
---
 libavcodec/cbs_mpeg2.c|  5 
 libavcodec/h264_parser.c  |  2 +-
 libavcodec/mpeg12.c   | 41 +-
 libavcodec/mpeg4_unpack_bframes_bsf.c |  1 -
 libavcodec/mpegvideo_parser.c | 42 +--
 libavcodec/startcode.h| 14 +++--
 libavcodec/utils.c| 16 --
 libavcodec/vc1_common.h   |  2 +-
 libavformat/rtpenc_mpv.c  |  2 +-
 9 files changed, 92 insertions(+), 33 deletions(-)

diff --git a/libavcodec/cbs_mpeg2.c b/libavcodec/cbs_mpeg2.c
index f0a2265938..870119bab0 100644
--- a/libavcodec/cbs_mpeg2.c
+++ b/libavcodec/cbs_mpeg2.c
@@ -160,11 +160,6 @@ static int cbs_mpeg2_split_fragment(CodedBitstreamContext 
*ctx,
 const uint8_t *end;
 size_t unit_size;
 
-// Reset start_code to ensure that avpriv_find_start_code()
-// really reads a new start code and does not reuse the old
-// start code in any way (as e.g. happens when there is a
-// Sequence End unit at the very end of a packet).
-start_code = UINT32_MAX;
 end = avpriv_find_start_code(start--, frag->data + frag->data_size,
  _code);
 
diff --git a/libavcodec/h264_parser.c b/libavcodec/h264_parser.c
index 50810f1789..b67830d40e 100644
--- a/libavcodec/h264_parser.c
+++ b/libavcodec/h264_parser.c
@@ -69,7 +69,7 @@ typedef struct H264ParseContext {
 static int find_start_code(const uint8_t *buf, int buf_size,
   int buf_index, int next_avc)
 {
-uint32_t state = -1;
+uint32_t state;
 
 buf_index = avpriv_find_start_code(buf + buf_index, buf + next_avc + 1, 
) - buf - 1;
 
diff --git a/libavcodec/mpeg12.c b/libavcodec/mpeg12.c
index 85f4f432bd..9639423152 100644
--- a/libavcodec/mpeg12.c
+++ b/libavcodec/mpeg12.c
@@ -165,6 +165,45 @@ av_cold void ff_mpeg12_init_vlcs(void)
 }
 
 #if FF_API_FLAG_TRUNCATED
+/**
+ * By preserving the @p start_code value between subsequent calls, the 
caller can
+ * detect start codes across buffer boundaries.
+ *
+ * @param[in,out] start_code A pointer to a mutable @c uint32_t.
+ *  As input: For no history preset to @c ~0 , otherwise preset 
to the last
+ *returned start code to enable detecting start codes 
across
+ *buffer boundaries.
+ *  On output: Set to the found start code if it exists or an invalid
+ * start code (the 4 bytes prior to the returned value,
+ * using the input history if @f$ end - p < 4 @f$).
+ *
+ * @sa avpriv_find_start_code()
+ */
+static const uint8_t *find_start_code_truncated(const uint8_t *av_restrict p,
+  const uint8_t * const end,
+  uint32_t * const av_restrict start_code)
+{
+av_assert0(p <= end);
+if (p >= end)
+return end;
+
+if (*start_code == 0x100)
+*start_code = ~0;
+// invalidate byte 0 so overlapping start codes are not erroneously 
detected
+
+// read up to the first three bytes in p to enable reading a start code 
across
+// two (to four) buffers
+for (int i = 0; i < 3; i++) {
+*start_code <<= 8;
+*start_code += *p;
+p++;
+if (start_code_is_valid(*start_code) || p == end)
+return p;
+}
+// buffer length is at least 4
+return avpriv_find_start_code(p - 3, end, start_code);
+}
+
 /**
  * Find the end of the current frame in the bitstream.
  * @return the position of the first byte of the next frame, or -1
@@ -199,7 +238,7 @@ int ff_mpeg1_find_frame_end(ParseContext *pc, const uint8_t 
*buf, int buf_size,
 }
 state++;
 } else {
-i = avpriv_find_start_code(buf + i, buf + buf_size, ) - buf 
- 1;
+i = find_start_code_truncated(buf + i, buf + buf_size, ) - 
buf - 1;
 if (pc->frame_start_found == 0 && state >= SLICE_MIN_START_CODE && 
state <= SLICE_MAX_START_CODE) {
 i++;
 pc->frame_start_found = 4;
diff --git a/libavcodec/mpeg4_unpack_bframes_bsf.c 
b/libavcodec/mpeg4_unpack_bframes_bsf.c
index ae2c129d88..1030dd60aa 100644
--- a/libavcodec/mpeg4_unpack_bframes_bsf.c
+++ b/libavcodec/mpeg4_unpack_bframes_bsf.c
@@ -36,7 +36,6 @@ static void scan_buffer(const uint8_t *buf, int buf_size,
 const uint8_t *end = buf + 

[FFmpeg-devel] [PATCH v3 7/8] avpriv_find_start_code(): constify pointer parameters

2022-02-08 Thread Scott Theisen
Have the compiler enforce not changing the addresses these parameters point to.

No functional change.
---
 libavcodec/utils.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/libavcodec/utils.c b/libavcodec/utils.c
index 68d126acd8..d11d92d21e 100644
--- a/libavcodec/utils.c
+++ b/libavcodec/utils.c
@@ -937,8 +937,8 @@ void ff_thread_report_progress2(AVCodecContext *avctx, int 
field, int thread, in
 #endif
 
 const uint8_t *avpriv_find_start_code(const uint8_t *av_restrict p,
-  const uint8_t *end,
-  uint32_t *av_restrict start_code)
+  const uint8_t * const end,
+  uint32_t * const av_restrict start_code)
 {
 av_assert0(p <= end);
 if (p >= end)
-- 
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-requ...@ffmpeg.org with subject "unsubscribe".


[FFmpeg-devel] [PATCH v3 6/8] avpriv_find_start_code(): correct type of start_code parameter

2022-02-08 Thread Scott Theisen
---
 libavcodec/mpeg12dec.c   | 2 +-
 libavformat/rtpenc_mpv.c | 3 +--
 2 files changed, 2 insertions(+), 3 deletions(-)

diff --git a/libavcodec/mpeg12dec.c b/libavcodec/mpeg12dec.c
index 27fd61e848..24cd6aac77 100644
--- a/libavcodec/mpeg12dec.c
+++ b/libavcodec/mpeg12dec.c
@@ -1768,7 +1768,7 @@ static int mpeg_decode_slice(MpegEncContext *s, int mb_y,
 
 if (avctx->hwaccel && avctx->hwaccel->decode_slice) {
 const uint8_t *buf_end, *buf_start = *buf - 4; /* include start_code */
-int start_code = -1;
+uint32_t start_code = ~0;
 buf_end = avpriv_find_start_code(buf_start + 2, *buf + buf_size, 
_code);
 if (buf_end < *buf + buf_size)
 buf_end -= 4;
diff --git a/libavformat/rtpenc_mpv.c b/libavformat/rtpenc_mpv.c
index 8b6987b7f2..9c0816ef95 100644
--- a/libavformat/rtpenc_mpv.c
+++ b/libavformat/rtpenc_mpv.c
@@ -51,11 +51,10 @@ void ff_rtp_send_mpegvideo(AVFormatContext *s1, const 
uint8_t *buf1, int size)
 end_of_slice = 1;
 } else {
 const uint8_t *r, *r1;
-int start_code;
 
 r1 = buf1;
 while (1) {
-start_code = -1;
+uint32_t start_code = ~0;
 r = avpriv_find_start_code(r1, end, _code);
 if (start_code_is_valid(start_code)) {
 /* New start code found */
-- 
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-requ...@ffmpeg.org with subject "unsubscribe".


[FFmpeg-devel] [PATCH v3 5/8] avpriv_find_start_code(): add doxygen comment and rename a parameter

2022-02-08 Thread Scott Theisen
---
 libavcodec/startcode.h | 26 +-
 libavcodec/utils.c | 10 +-
 2 files changed, 30 insertions(+), 6 deletions(-)

diff --git a/libavcodec/startcode.h b/libavcodec/startcode.h
index 833754af09..69389c729c 100644
--- a/libavcodec/startcode.h
+++ b/libavcodec/startcode.h
@@ -44,9 +44,33 @@ static av_always_inline int start_code_is_valid(uint32_t 
start_code) {
 return (start_code & 0xFF00) == 0x100;
 }
 
+/**
+ * @brief Find the first start code in the buffer @p p.
+ *
+ * A start code is a sequence of 4 bytes with the hexadecimal value  00 
00 01 XX ,
+ * where  XX  represents any value and memory address 
increases left to right.
+ *
+ * By preserving the @p start_code value between subsequent calls, the 
caller can
+ * detect start codes across buffer boundaries.
+ *
+ * @param[in] p A pointer to the start of the memory buffer to scan.
+ * @param[in] end   A pointer to the past-the-end memory address for the buffer
+ *  given by @p p.  @p p must be ≤ @p end.
+ *
+ * @param[in,out] start_code A pointer to a mutable @c uint32_t.
+ *  As input: For no history preset to @c ~0 , otherwise preset 
to the last
+ *returned start code to enable detecting start codes 
across
+ *buffer boundaries.
+ *  On output: Set to the found start code if it exists or an invalid
+ * start code (the 4 bytes prior to the returned value,
+ * using the input history if @f$ end - p < 4 @f$).
+ *
+ * @return A pointer to the memory address following the found start code, or 
@p end
+ * if no start code was found.
+ */
 const uint8_t *avpriv_find_start_code(const uint8_t *p,
   const uint8_t *end,
-  uint32_t *state);
+  uint32_t *start_code);
 
 int ff_startcode_find_candidate_c(const uint8_t *buf, int size);
 
diff --git a/libavcodec/utils.c b/libavcodec/utils.c
index bdafdaa355..68d126acd8 100644
--- a/libavcodec/utils.c
+++ b/libavcodec/utils.c
@@ -938,7 +938,7 @@ void ff_thread_report_progress2(AVCodecContext *avctx, int 
field, int thread, in
 
 const uint8_t *avpriv_find_start_code(const uint8_t *av_restrict p,
   const uint8_t *end,
-  uint32_t *av_restrict state)
+  uint32_t *av_restrict start_code)
 {
 av_assert0(p <= end);
 if (p >= end)
@@ -947,10 +947,10 @@ const uint8_t *avpriv_find_start_code(const uint8_t 
*av_restrict p,
 // read up to the first three bytes in p to enable reading a start code 
across
 // two (to four) buffers
 for (int i = 0; i < 3; i++) {
-*state <<= 8;
-*state += *p;
+*start_code <<= 8;
+*start_code += *p;
 p++;
-if (start_code_is_valid(*state) || p == end)
+if (start_code_is_valid(*start_code) || p == end)
 return p;
 }
 // p is now properly incremented for the negative indices in the while loop
@@ -983,7 +983,7 @@ const uint8_t *avpriv_find_start_code(const uint8_t 
*av_restrict p,
 if (p > end)
 p = end;
 // read the previous 4 bytes, i.e. bytes {p - 4, p - 3, p - 2, p - 1}
-*state = AV_RB32(p - 4);
+*start_code = AV_RB32(p - 4);
 return p;
 }
 
-- 
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-requ...@ffmpeg.org with subject "unsubscribe".


[FFmpeg-devel] [PATCH v3 4/8] avpriv_find_start_code(): rewrite for loop for clarity

2022-02-08 Thread Scott Theisen
---
 libavcodec/utils.c | 14 --
 1 file changed, 8 insertions(+), 6 deletions(-)

diff --git a/libavcodec/utils.c b/libavcodec/utils.c
index 5b49657b44..bdafdaa355 100644
--- a/libavcodec/utils.c
+++ b/libavcodec/utils.c
@@ -940,18 +940,20 @@ const uint8_t *avpriv_find_start_code(const uint8_t 
*av_restrict p,
   const uint8_t *end,
   uint32_t *av_restrict state)
 {
-int i;
-
 av_assert0(p <= end);
 if (p >= end)
 return end;
 
-for (i = 0; i < 3; i++) {
-uint32_t tmp = *state << 8;
-*state = tmp + *(p++);
-if (tmp == 0x100 || p == end)
+// read up to the first three bytes in p to enable reading a start code 
across
+// two (to four) buffers
+for (int i = 0; i < 3; i++) {
+*state <<= 8;
+*state += *p;
+p++;
+if (start_code_is_valid(*state) || p == end)
 return p;
 }
+// p is now properly incremented for the negative indices in the while loop
 
 /* with memory address increasing left to right, we are looking for (in 
hexadecimal):
  * 00 00 01 XX
-- 
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-requ...@ffmpeg.org with subject "unsubscribe".


[FFmpeg-devel] [PATCH v3 3/8] avpriv_find_start_code(): rewrite while loop

2022-02-08 Thread Scott Theisen
The expected number of iterations may increase by one for an input
of alternating 0 and 1 bytes.  Instead of incrementing by 2 everytime,
it now alternates between incrementing by 1 and by 3.

For the check p[-2] != 0:
This slightly reduces the number of iterations by starting with three
new bytes on the next iteration, instead of keeping byte p[-3] which
is invalid, since it is now known to be 01 when it must be 00.

The comparisons (p[-1]  > 1) and (p[-1] == 0) should be one
comparison against 1 since p is unsigned, which makes
(p[-1] == 0) equivalent to (p[-1] < 1)

No other observable change.
---
 libavcodec/utils.c | 23 +++
 1 file changed, 19 insertions(+), 4 deletions(-)

diff --git a/libavcodec/utils.c b/libavcodec/utils.c
index 42a1885d61..5b49657b44 100644
--- a/libavcodec/utils.c
+++ b/libavcodec/utils.c
@@ -953,12 +953,27 @@ const uint8_t *avpriv_find_start_code(const uint8_t 
*av_restrict p,
 return p;
 }
 
+/* with memory address increasing left to right, we are looking for (in 
hexadecimal):
+ * 00 00 01 XX
+ * p points at the address which should have the value of XX
+ */
 while (p < end) {
-if  (p[-1] > 1  ) p += 3;
-else if (p[-2]  ) p += 2;
-else if (p[-3]|(p[-1]-1)) p++;
-else {
+// UU UU UU
+if  (p[-1]  > 1) {
+p += 3;
+// start check over with 3 new bytes
+}
+else if (p[-1] == 0) {
+p++;
+// could be in a start code, so check next byte
+}
+else if (/* UU UU 01 */ p[-2] != 0 ||
+ /* UU 00 01 */ p[-3] != 0) {
+p += 3;
+}
+else { /* 00 00 01 */
 p++;
+// p now points at the address following the start code value XX
 break;
 }
 }
-- 
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-requ...@ffmpeg.org with subject "unsubscribe".


[FFmpeg-devel] [PATCH v3 2/8] avpriv_find_start_code(): readability enhancement part 1

2022-02-08 Thread Scott Theisen
No functional change.
---
 libavcodec/utils.c | 9 +
 1 file changed, 5 insertions(+), 4 deletions(-)

diff --git a/libavcodec/utils.c b/libavcodec/utils.c
index c7c7323351..42a1885d61 100644
--- a/libavcodec/utils.c
+++ b/libavcodec/utils.c
@@ -963,10 +963,11 @@ const uint8_t *avpriv_find_start_code(const uint8_t 
*av_restrict p,
 }
 }
 
-p = FFMIN(p, end) - 4;
-*state = AV_RB32(p);
-
-return p + 4;
+if (p > end)
+p = end;
+// read the previous 4 bytes, i.e. bytes {p - 4, p - 3, p - 2, p - 1}
+*state = AV_RB32(p - 4);
+return p;
 }
 
 AVCPBProperties *av_cpb_properties_alloc(size_t *size)
-- 
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-requ...@ffmpeg.org with subject "unsubscribe".


[FFmpeg-devel] [PATCH v3 1/8] avcodec/startcode.h: create start_code_is_valid()

2022-02-08 Thread Scott Theisen
This slightly changes what is considered valid in the following cases:
cavsdec.c:   0x00XX is now considered invalid.
mpeg12dec.c: 0x00XX is now considered invalid.
(where X is any value)

IS_MARKER is equivalent since VC1_CODE_RES0 = 0x0100
---
 libavcodec/cavsdec.c   |  2 +-
 libavcodec/cbs_mpeg2.c |  6 +++---
 libavcodec/extract_extradata_bsf.c |  2 +-
 libavcodec/mpeg12.c|  2 +-
 libavcodec/mpeg12dec.c |  2 +-
 libavcodec/mpegvideo_parser.c  |  2 +-
 libavcodec/remove_extradata_bsf.c  |  8 +++-
 libavcodec/startcode.h | 17 +
 libavcodec/vaapi_vc1.c |  2 +-
 libavcodec/vc1_common.h|  4 +---
 libavcodec/vc1dec.c|  2 +-
 libavformat/avs2dec.c  |  4 ++--
 libavformat/avs3dec.c  |  4 ++--
 libavformat/cavsvideodec.c |  2 +-
 libavformat/mpegvideodec.c |  2 +-
 libavformat/rtpenc_mpv.c   |  2 +-
 16 files changed, 38 insertions(+), 25 deletions(-)

diff --git a/libavcodec/cavsdec.c b/libavcodec/cavsdec.c
index 894aa1b54a..75cf17e271 100644
--- a/libavcodec/cavsdec.c
+++ b/libavcodec/cavsdec.c
@@ -1250,7 +1250,7 @@ static int cavs_decode_frame(AVCodecContext *avctx, void 
*data, int *got_frame,
 buf_end = buf + buf_size;
 for(;;) {
 buf_ptr = avpriv_find_start_code(buf_ptr, buf_end, );
-if ((stc & 0xFE00) || buf_ptr == buf_end) {
+if (!start_code_is_valid(stc) || buf_ptr == buf_end) {
 if (!h->stc)
 av_log(h->avctx, AV_LOG_WARNING, "no frame decoded\n");
 return FFMAX(0, buf_ptr - buf);
diff --git a/libavcodec/cbs_mpeg2.c b/libavcodec/cbs_mpeg2.c
index 33bd3e0998..f0a2265938 100644
--- a/libavcodec/cbs_mpeg2.c
+++ b/libavcodec/cbs_mpeg2.c
@@ -150,7 +150,7 @@ static int cbs_mpeg2_split_fragment(CodedBitstreamContext 
*ctx,
 
 start = avpriv_find_start_code(frag->data, frag->data + frag->data_size,
_code);
-if (start_code >> 8 != 0x01) {
+if (!start_code_is_valid(start_code)) {
 // No start code found.
 return AVERROR_INVALIDDATA;
 }
@@ -172,7 +172,7 @@ static int cbs_mpeg2_split_fragment(CodedBitstreamContext 
*ctx,
 // (may be the last byte of fragment->data); end points to the byte
 // following the byte containing the start code identifier (or to
 // the end of fragment->data).
-if (start_code >> 8 == 0x01) {
+if (start_code_is_valid(start_code)) {
 // Unit runs from start to the beginning of the start code
 // pointed to by end (including any padding zeroes).
 unit_size = (end - 4) - start;
@@ -189,7 +189,7 @@ static int cbs_mpeg2_split_fragment(CodedBitstreamContext 
*ctx,
 start = end;
 
 // Do we have a further unit to add to the fragment?
-} while ((start_code >> 8) == 0x01);
+} while (start_code_is_valid(start_code));
 
 return 0;
 }
diff --git a/libavcodec/extract_extradata_bsf.c 
b/libavcodec/extract_extradata_bsf.c
index 027a578af1..9c639933ee 100644
--- a/libavcodec/extract_extradata_bsf.c
+++ b/libavcodec/extract_extradata_bsf.c
@@ -239,7 +239,7 @@ static int extract_extradata_vc1(AVBSFContext *ctx, 
AVPacket *pkt,
 ptr = avpriv_find_start_code(ptr, end, );
 if (state == VC1_CODE_SEQHDR || state == VC1_CODE_ENTRYPOINT) {
 has_extradata = 1;
-} else if (has_extradata && IS_MARKER(state)) {
+} else if (has_extradata && start_code_is_valid(state)) {
 extradata_size = ptr - 4 - pkt->data;
 break;
 }
diff --git a/libavcodec/mpeg12.c b/libavcodec/mpeg12.c
index 5520960b74..85f4f432bd 100644
--- a/libavcodec/mpeg12.c
+++ b/libavcodec/mpeg12.c
@@ -213,7 +213,7 @@ int ff_mpeg1_find_frame_end(ParseContext *pc, const uint8_t 
*buf, int buf_size,
 pc->frame_start_found = 0;
 if (pc->frame_start_found  < 4 && state == EXT_START_CODE)
 pc->frame_start_found++;
-if (pc->frame_start_found == 4 && (state & 0xFF00) == 0x100) {
+if (pc->frame_start_found == 4 && start_code_is_valid(state)) {
 if (state < SLICE_MIN_START_CODE || state > 
SLICE_MAX_START_CODE) {
 pc->frame_start_found = 0;
 pc->state = -1;
diff --git a/libavcodec/mpeg12dec.c b/libavcodec/mpeg12dec.c
index 860e86aa74..27fd61e848 100644
--- a/libavcodec/mpeg12dec.c
+++ b/libavcodec/mpeg12dec.c
@@ -2475,7 +2475,7 @@ static int decode_chunks(AVCodecContext *avctx, AVFrame 
*picture,
 /* find next start code */
 uint32_t start_code = -1;
 buf_ptr = avpriv_find_start_code(buf_ptr, buf_end, _code);
-if (start_code > 0x1ff) {
+if (!start_code_is_valid(start_code)) {
 if (!skip_frame) {
 if (HAVE_THREADS &&
 

[FFmpeg-devel] [PATCH v3 0/8] rewrite avpriv_find_start_code() for clarity

2022-02-08 Thread Scott Theisen
I have removed most of my added comments.

Rebased to account for Andreas Rheinhardt moving the prototype to
startcode.h.

Per his suggestion, the function signature is now unmodified.
(ff_)mpeg1_find_frame_end() now has a wrapper function preserving
the original behavior allowing start codes across buffer boundaries.

-Scott

___
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".


[FFmpeg-devel] [PATCH] cbs_mpeg2_split_fragment(): cache the buffer end

2022-02-08 Thread Scott Theisen
Also add a few clarifying comments.
---
 libavcodec/cbs_mpeg2.c | 16 
 1 file changed, 8 insertions(+), 8 deletions(-)

diff --git a/libavcodec/cbs_mpeg2.c b/libavcodec/cbs_mpeg2.c
index 33bd3e0998..47732562d1 100644
--- a/libavcodec/cbs_mpeg2.c
+++ b/libavcodec/cbs_mpeg2.c
@@ -144,12 +144,12 @@ static int cbs_mpeg2_split_fragment(CodedBitstreamContext 
*ctx,
 CodedBitstreamFragment *frag,
 int header)
 {
-const uint8_t *start;
+const uint8_t *start = frag->data;
+const uint8_t * const buf_end = frag->data + frag->data_size;
 uint32_t start_code = -1;
 int err;
 
-start = avpriv_find_start_code(frag->data, frag->data + frag->data_size,
-   _code);
+start = avpriv_find_start_code(start, buf_end, _code);
 if (start_code >> 8 != 0x01) {
 // No start code found.
 return AVERROR_INVALIDDATA;
@@ -165,12 +165,11 @@ static int cbs_mpeg2_split_fragment(CodedBitstreamContext 
*ctx,
 // start code in any way (as e.g. happens when there is a
 // Sequence End unit at the very end of a packet).
 start_code = UINT32_MAX;
-end = avpriv_find_start_code(start--, frag->data + frag->data_size,
- _code);
-
-// start points to the byte containing the start_code_identifier
+end = avpriv_find_start_code(start, buf_end, _code);
+start--;
+// decrement so start points to the byte containing the 
start_code_identifier
 // (may be the last byte of fragment->data); end points to the byte
-// following the byte containing the start code identifier (or to
+// following the byte containing the next start code identifier (or to
 // the end of fragment->data).
 if (start_code >> 8 == 0x01) {
 // Unit runs from start to the beginning of the start code
@@ -178,6 +177,7 @@ static int cbs_mpeg2_split_fragment(CodedBitstreamContext 
*ctx,
 unit_size = (end - 4) - start;
 } else {
// We didn't find a start code, so this is the final unit.
+   // There is no start code to remove from end, hence not (end - 4).
unit_size = end - start;
 }
 
-- 
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-requ...@ffmpeg.org with subject "unsubscribe".


Re: [FFmpeg-devel] [PATCH v2 08/13] avpriv_find_start_code(): add parameter for ignoring history

2022-02-05 Thread Scott Theisen

On 2/5/22 01:10, Andreas Rheinhardt wrote:

1. This patch is absolutely unacceptable: It breaks ABI.
2. I am surprised that there is apparently only one user that actually
wants this feature of state being an input parameter, namely
(ff_)mpeg1_find_frame_end(). This means that this loop done before the
new check should actually be made by this caller alone, obviating the
need to change the signature.
3. Given that no user of this in libavformat wants this feature,
changing it is IMO acceptable from an ABI-point of view.


I'll look into it.


4. There might be some slight changes introduced by this though:
Consider 00 00 01 00 00 01 xy. If the initial state is -1, a call to
avpriv_find_start_code() will treat the initial four bytes as start code
and return a pointer to the byte preceding the second 0x01. If the user
does not reset the start code between calls to avpriv_find_start_code(),
the second call will combine the last zero byte of the start code with
the rest to form another start code that partially overlaps with the
earlier one. This is (probably) invalid data (definitely for H.262,
H.264 and HEVC).


With input buffer 00 00 01 00 00 01 xy ...
The code in master will (incorrectly) find a second start code at offset 7.
It would need to check if (*start_code == 0x100) and invalidate it.

-Scott

___
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 4/7] avcodec/cbs_mpeg2: Simplify splitting fragment

2022-02-05 Thread Scott Theisen

On 2/4/22 14:01, Scott Theisen wrote:


I assume this is inspired by my patch series.  Did you see v2 of my 
patch series where I made similar and more extensive clarifications?


The check for sequence_end_codes should not be in the loop.  It harms 
readability, although you also eliminated the branch.


I retract my comments.

-Scott
___
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".


[FFmpeg-devel] [PATCH] cbs_mpeg2_split_fragment(): cache the buffer end

2022-02-04 Thread Scott Theisen
It is constant, so instead of recalculating it every time, only
calculate it once.

Also add a few clarifying comments.
---
 libavcodec/cbs_mpeg2.c | 14 +++---
 1 file changed, 7 insertions(+), 7 deletions(-)

diff --git a/libavcodec/cbs_mpeg2.c b/libavcodec/cbs_mpeg2.c
index dfea12ef1b..d3204b885a 100644
--- a/libavcodec/cbs_mpeg2.c
+++ b/libavcodec/cbs_mpeg2.c
@@ -144,12 +144,12 @@ static int cbs_mpeg2_split_fragment(CodedBitstreamContext 
*ctx,
 CodedBitstreamFragment *frag,
 int header)
 {
-const uint8_t *start;
+const uint8_t *start = frag->data;
+const uint8_t * const buf_end = frag->data + frag->data_size;
 uint32_t start_code = -1;
 int err;
 
-start = avpriv_find_start_code(frag->data, frag->data + frag->data_size,
-   _code);
+start = avpriv_find_start_code(start, buf_end, _code);
 if (start_code >> 8 != 0x01) {
 // No start code found.
 return AVERROR_INVALIDDATA;
@@ -165,10 +165,9 @@ static int cbs_mpeg2_split_fragment(CodedBitstreamContext 
*ctx,
 // start code in any way (as e.g. happens when there is a
 // Sequence End unit at the very end of a packet).
 start_code = UINT32_MAX;
-end = avpriv_find_start_code(start--, frag->data + frag->data_size,
- _code);
-
-// start points to the byte containing the start_code_identifier
+end = avpriv_find_start_code(start, buf_end, _code);
+start--;
+// decrement so start points to the byte containing the 
start_code_identifier
 // (may be the last byte of fragment->data); end points to the byte
 // following the byte containing the start code identifier (or to
 // the end of fragment->data).
@@ -178,6 +177,7 @@ static int cbs_mpeg2_split_fragment(CodedBitstreamContext 
*ctx,
 unit_size = (end - 4) - start;
 } else {
// We didn't find a start code, so this is the final unit.
+   // There is no start code to remove from end, hence not (end - 4).
unit_size = end - start;
 }
 
-- 
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-requ...@ffmpeg.org with subject "unsubscribe".


Re: [FFmpeg-devel] [PATCH v2 07/13] avpriv_find_start_code(): constify pointer parameters

2022-02-04 Thread Scott Theisen

On 2/5/22 00:49, Andreas Rheinhardt wrote:

- * @param[in,out] start_code A reference to a mutable @c uint32_t.
+ * @param[in,out] start_code A constant pointer (reference) to a mutable @c 
uint32_t.

There are no references in C.


A pointer is a type of reference.  However, I agree "A pointer to a 
mutable @c uint32_t." would be clearer.



Documenting restrictions on the callee that are irrelevant to the caller
in a public header seems weird.


Fair enough, the added consts shall be limited to libavcodec/utils.c.

-Scott
___
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 01/13] avcodec/internal.h: create avpriv_start_code_is_valid()

2022-02-04 Thread Scott Theisen

On 2/5/22 00:42, Andreas Rheinhardt wrote:

a) We use the avpriv prefix for things that should be exported from one
library to be used in other libraries, but not for public use. Therefore
the avpriv prefix is inappropriate here as this function is static. And
it makes a very long name.


So since this is static av_always_inline, it should just be 
start_code_is_valid()?



b) internal.h is the wrong header for this: There are more start codes
than 00 00 01. That's why I sent the patch to move
avpriv_find_start_code() to libavcodec/startcode.h.


I only put it there because that is where avpriv_find_start_code() is 
declared, so I knew it was already included.


Should I move the definition and declaration of avpriv_find_start_code() 
to startcode.(c|h)?



c) I am not sure that the new code is equivalent to the old one in all
instances: mpeg12dec.c checks for "start_code > 0x1ff" to mean "no valid
start code", yet if the buffer ended with anything in the range
0x00-0xff it would be considered a start code before this patch and now
it would no longer be a start code. I don't think this is a bad change,
though, but it should be noted in the commit message.


I'll have to look at them all again and I'll add that to the commit message.

-Scott
___
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 13/13] cbs_mpeg2.c: improve readability of start code search (part 3)

2022-02-04 Thread Scott Theisen

On 2/4/22 22:54, Andreas Rheinhardt wrote:

I disagree that this is the true loop condition that just needs to be
revealed; in fact, this is basically the loop condition from before
fd93d5efe64206d5f1bce8c702602353444c0c1a, just with an added block to
deal with size one units at the end. I considered this and chose the
current one because it leads to less code duplication for this special case.
Anyway, now that I have taken another look at this and I finally found
the true loop condition: Is there a unit that we have not added to the
fragment yet? This is easily implementable if one always resets the
start code, so that there being an outstanding unit is equivalent to the
start code being valid.


Looking at your patch series again, I agree your changes to cbs_mpeg2.c 
are clearer.


However, I think my changes from patch 11 are a further helpful 
clarification (ignoring the index and loop changes (since you already 
did that) and the "redundant" comment):


@@ -144,23 +144,24 @@  static int 
cbs_mpeg2_split_fragment(CodedBitstreamContext *ctx,
 CodedBitstreamFragment *frag,
 int header)
 {
- const uint8_t *start, *end;
+ const uint8_t *start = frag->data, *end;
+ const uint8_t * const buf_end = frag->data + frag->data_size;
 CodedBitstreamUnitType unit_type;
 uint32_t start_code = -1;
 size_t unit_size;
- int err, i = 0, final = 0;
+ int err, final = 0;
+ int i = -1; // offset for pre-increment
 
- start = avpriv_find_start_code(frag->data, frag->data + frag->data_size,

- _code, 1);
+ start = avpriv_find_start_code(start, buf_end, _code, 1);
 if (!avpriv_start_code_is_valid(start_code)) {
 // No start code found.
 return AVERROR_INVALIDDATA;
 }
 
- while (!final) {

+ do {
 unit_type = start_code & 0xff;
 
- if (start == frag->data + frag->data_size) {

+ if (start == buf_end) {
 // The last four bytes form a start code which constitutes
 // a unit of its own.  In this situation avpriv_find_start_code
 // won't modify start_code at all so modify start_code so that
@@ -168,10 +169,9 @@  static int cbs_mpeg2_split_fragment(CodedBitstreamContext 
*ctx,
 start_code = 0;
 }
 
- end = avpriv_find_start_code(start--, frag->data + frag->data_size,

- _code, 0);
-
- // start points to the byte containing the start_code_identifier
+ end = avpriv_find_start_code(start, buf_end, _code, 0);
+ start--;
+ // decrement so start points to the byte containing the 
start_code_identifier

 // (may be the last byte of fragment->data); end points to the byte
 // following the byte containing the start code identifier (or to
 // the end of fragment->data).



Should I submit a v3 patch series which only includes patches 1-9? (That 
is only the avpriv_find_start_code() changes, since 10-13 were 
cbs_mpeg2.c and separate but related.)


Regards,
Scott

___
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 10/13] cbs_mpeg2.c: use a while loop with a loop condition instead of an infinite loop

2022-02-04 Thread Scott Theisen

On 2/4/22 23:06, Andreas Rheinhardt wrote:

Scott Theisen:

On 2/4/22 20:48, Andreas Rheinhardt wrote:

I disagree that this enhances clarity: When using a counter, a for loop
is the most natural.

The counter is not used as the loop condition, so it is not natural.
Also, you removed the counter and made this a do while loop in your own
patch series.

Yes, after the counter has been removed, using a for-loop stopped being
natural.


It is not really a for-loop, though, since it has no loop condition and 
is thus an infinite loop that just happens to be abusing the for-loop 
syntax.


-Scott
___
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 12/13] cbs_mpeg2.c: improve readability of start code search (part 2)

2022-02-04 Thread Scott Theisen

On 2/4/22 21:24, Andreas Rheinhardt wrote:

end is now uninitialized in case of a unit consisting solely of a
Sequence End.


I hadn't noticed that.  However, I immediately restored it in the next 
patch.

___
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 11/13] cbs_mpeg2.c: improve readability of start code search (part 1)

2022-02-04 Thread Scott Theisen

On 2/4/22 21:16, Andreas Rheinhardt wrote:

Using a pre-increment is unnatural (i is supposed to be the number of
units of the fragment and so it should naturally be incremented after a
unit has been successfully added to the fragment) and impairs clarity.



It *is* incremented after a unit has been successfully added, on the 
*next* iteration.



redundant comment



Debatable, the variable is already a uint8_t*, albeit const qualified.  
The purpose of comments is to explain.

___
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 10/13] cbs_mpeg2.c: use a while loop with a loop condition instead of an infinite loop

2022-02-04 Thread Scott Theisen

On 2/4/22 20:48, Andreas Rheinhardt wrote:

I disagree that this enhances clarity: When using a counter, a for loop
is the most natural.


The counter is not used as the loop condition, so it is not natural.  
Also, you removed the counter and made this a do while loop in your own 
patch series.

___
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 6/7] avcodec/internal.h: Move avpriv_find_start_code() to startcode.h

2022-02-04 Thread Scott Theisen

On 2/4/22 10:16, Andreas Rheinhardt wrote:

This is by definition the appropriate place for it.
Remove all the now unnecessary libavcodec/internal.h inclusions;
also remove other unnecessary headers from the affected files.

Signed-off-by: Andreas Rheinhardt 
---




diff --git a/libavcodec/utils.c b/libavcodec/utils.c
index b19befef21..c7c7323351 100644
--- a/libavcodec/utils.c
+++ b/libavcodec/utils.c
@@ -27,7 +27,6 @@
  
  #include "config.h"

  #include "libavutil/avassert.h"
-#include "libavutil/avstring.h"
  #include "libavutil/channel_layout.h"
  #include "libavutil/intreadwrite.h"
  #include "libavutil/mem.h"
@@ -40,12 +39,9 @@
  #include "thread.h"
  #include "internal.h"
  #include "put_bits.h"
-#include "raw.h"
+#include "startcode.h"
  #include 
-#include 
-#include 
  #include 
-#include 
  
  void av_fast_padded_malloc(void *ptr, unsigned int *size, size_t min_size)

  {


Shouldn't you also move the definition to startcode.c as well? Then 
utils.c doesn't need startcode.h.

___
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 4/7] avcodec/cbs_mpeg2: Simplify splitting fragment

2022-02-04 Thread Scott Theisen

On 2/4/22 10:16, Andreas Rheinhardt wrote:

avpriv_find_start_code() supports non-contiguous buffers
by maintaining a state that allows to find start codes
that span across multiple buffers; a consequence thereof
is that avpriv_find_start_code() is given a zero-sized
buffer, it does not modify this state, so that it appears
as if a start code was found if the state contained a start code.

This can e.g. happen with Sequence End units in MPEG-2 and
to counter this, cbs_mpeg2_split_fragment() reset the state
when it has already encountered the end of the fragment
in order to add the last unit (if it is only of the form 00 00 01 xy)
only once; it also used a flag to set whether this is the final unit.

Yet this can be improved by simply resetting state unconditionally
(thereby avoiding a branch); the flag can be removed by just checking
whether we have a valid start code (of the next unit to add)
at the end.

Signed-off-by: Andreas Rheinhardt 
---
  libavcodec/cbs_mpeg2.c | 17 ++---
  1 file changed, 6 insertions(+), 11 deletions(-)

diff --git a/libavcodec/cbs_mpeg2.c b/libavcodec/cbs_mpeg2.c
index 2bf38c6001..90d667ddc8 100644
--- a/libavcodec/cbs_mpeg2.c
+++ b/libavcodec/cbs_mpeg2.c
@@ -149,7 +149,6 @@ static int cbs_mpeg2_split_fragment(CodedBitstreamContext 
*ctx,
  uint32_t start_code = -1;
  size_t unit_size;
  int err;
-int final = 0;
  
  start = avpriv_find_start_code(frag->data, frag->data + frag->data_size,

 _code);
@@ -161,14 +160,11 @@ static int cbs_mpeg2_split_fragment(CodedBitstreamContext 
*ctx,
  do {
  unit_type = start_code & 0xff;
  
-if (start == frag->data + frag->data_size) {

-// The last four bytes form a start code which constitutes
-// a unit of its own.  In this situation avpriv_find_start_code
-// won't modify start_code at all so modify start_code so that
-// the next unit will be treated as the last unit.
-start_code = 0;
-}
-
+// Reset start_code to ensure that avpriv_find_start_code()
+// really reads a new start code and does not reuse the old
+// start code in any way (as e.g. happens when there is a
+// Sequence End unit at the very end of a packet).
+start_code = UINT32_MAX;
  end = avpriv_find_start_code(start--, frag->data + frag->data_size,
   _code);
  
@@ -183,7 +179,6 @@ static int cbs_mpeg2_split_fragment(CodedBitstreamContext *ctx,

  } else {
 // We didn't find a start code, so this is the final unit.
 unit_size = end - start;
-   final = 1;
  }
  
  err = ff_cbs_append_unit_data(frag, unit_type, (uint8_t*)start,

@@ -192,7 +187,7 @@ static int cbs_mpeg2_split_fragment(CodedBitstreamContext 
*ctx,
  return err;
  
  start = end;

-} while (!final);
+} while ((start_code >> 8) == 0x01);
  
  return 0;

  }


I assume this is inspired by my patch series.  Did you see v2 of my 
patch series where I made similar and more extensive clarifications?


The check for sequence_end_codes should not be in the loop.  It harms 
readability, although you also eliminated the branch.


Regards,
Scott
___
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] avcodec/dvdsubdec: fix incorrect yellow appearance of dvd subtitles

2022-02-03 Thread Scott Theisen

On 2/3/22 17:57, Soft Works wrote:

My suspicion is that 0x00 was written assuming RGBA order instead
of ARGB.


You are missing a byte for either RGBA or ARGB.  RGBA would be cyan.

I'm pretty sure that it's a mistake and it hasn't been an intentional
choice, because even when you would consider a yellow color to for
whatever reason, it wouldn't have been exactly this yellow (#ff0),
because its lightness/saturation values are not in a range of what
is suitable for colored subtitles.


I think using yellow for debugging  purposes is more likely, i.e. it is 
obvious if the pallet was missing/not detected.  However, it shouldn't 
have been committed like that, if that was the reason.


Regards,
Scott
___
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] avcodec/dvdsubdec: fix incorrect yellow appearance of dvd subtitles

2022-02-03 Thread Scott Theisen

On 2/3/22 17:10, Soft Works wrote:



-Original Message-
From: ffmpegagent 
Sent: Tuesday, January 4, 2022 3:19 AM
To: ffmpeg-devel@ffmpeg.org
Cc: softworkz ; softworkz

Subject: [PATCH] avcodec/dvdsubdec: fix incorrect yellow appearance of
dvd subtitles

From: softworkz 

The guess_palette() implementation is questionable in itself
as its results don't match those from other DVD subtitle decoders.

This commit starts cleanup by fixing an obvious bug which has made
certain DVD subs appear yellow instead of white or grey for more than
10 years..

Signed-off-by: softworkz 
---
 avcodec/dvdsubdec: fix incorrect yellow appearance of dvd
subtitles

 Fixes an age-old bug in decoding DVD subtitles.

 Ever wondered why certain DVD subtitles are shown in yellow color
when
 ffmpeg is involved...

Published-As: https://github.com/ffstaging/FFmpeg/releases/tag/pr-
ffstaging-16%2Fsoftworkz%2Fpatch_dvdsubdec_fix-v1
Fetch-It-Via: git fetch https://github.com/ffstaging/FFmpeg pr-
ffstaging-16/softworkz/patch_dvdsubdec_fix-v1
Pull-Request: https://github.com/ffstaging/FFmpeg/pull/16

  libavcodec/dvdsubdec.c | 2 +-
  1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/libavcodec/dvdsubdec.c b/libavcodec/dvdsubdec.c
index 52259f0730..a3fdb535a5 100644
--- a/libavcodec/dvdsubdec.c
+++ b/libavcodec/dvdsubdec.c
@@ -400,7 +400,7 @@ static int decode_dvd_subtitles(DVDSubContext
*ctx, AVSubtitle *sub_header,
  } else {
  sub_header->rects[0]->nb_colors = 4;
  guess_palette(ctx, (uint32_t*)sub_header-

rects[0]->data[1],

-  0x00);
+  0xff);
  }
  sub_header->rects[0]->x = x1;
  sub_header->rects[0]->y = y1;

base-commit: 573b6b8a607398c5f34108efda9c29d41c5727ff
--
ffmpeg-codebot

Ping. (no maintainer seems to be registered for this)


MythTV has used this fix since 2010-06-04.  See 
https://github.com/ulmus-scott/FFmpeg/commit/e2b1a6ee63c01ac9c82d24e2e6cfffeb2bfc


libavcodec/dvdsubdec.c: default to white instead of yellow

from Improved display of DVD subtitles in containers other than the 
original. 
https://github.com/MythTV/mythtv/commit/2510a0821ea4453eb9b34dd96e68ff0441459d0b

references: https://code.mythtv.org/trac/ticket/8222
For whatever strange reason, ffmpeg has always used yellow: 
https://github.com/FFmpeg/FFmpeg/commit/240c1657dcd45adc0e63ef947b920919071ec1f7



___
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".


[FFmpeg-devel] [PATCH 2/2] replace av_get_media_type_string() with av_media_type_get_string()

2022-02-03 Thread Scott Theisen
printf %s with a null pointer is undefined behavior.
---
 doc/examples/demuxing_decoding.c | 10 +-
 doc/examples/extract_mvs.c   |  4 ++--
 fftools/cmdutils.c   |  7 +++
 fftools/cmdutils.h   |  2 --
 fftools/ffmpeg.c | 14 +++---
 fftools/ffmpeg_opt.c |  2 +-
 fftools/ffplay.c |  4 ++--
 fftools/ffprobe.c| 13 +++--
 libavcodec/avcodec.c |  2 +-
 libavcodec/tests/avcodec.c   | 10 ++
 libavdevice/dshow.c  |  4 ++--
 libavfilter/avfilter.c   |  4 ++--
 libavfilter/avfiltergraph.c  |  4 ++--
 libavfilter/src_movie.c  |  6 +++---
 libavformat/avienc.c |  2 +-
 libavformat/flvenc.c |  2 +-
 libavformat/framehash.c  |  2 +-
 libavformat/mov.c|  2 +-
 libavformat/mpegenc.c|  2 +-
 libavformat/mpegts.c |  2 +-
 libavformat/mxfdec.c |  2 +-
 libavformat/segment.c|  2 +-
 libavformat/tee.c|  2 +-
 libavformat/uncodedframecrcenc.c |  4 +---
 24 files changed, 45 insertions(+), 63 deletions(-)

diff --git a/doc/examples/demuxing_decoding.c b/doc/examples/demuxing_decoding.c
index 8520d5b660..670f08779d 100644
--- a/doc/examples/demuxing_decoding.c
+++ b/doc/examples/demuxing_decoding.c
@@ -155,7 +155,7 @@ static int open_codec_context(int *stream_idx,
 ret = av_find_best_stream(fmt_ctx, type, -1, -1, NULL, 0);
 if (ret < 0) {
 fprintf(stderr, "Could not find %s stream in input file '%s'\n",
-av_get_media_type_string(type), src_filename);
+av_media_type_get_string(type), src_filename);
 return ret;
 } else {
 stream_index = ret;
@@ -165,7 +165,7 @@ static int open_codec_context(int *stream_idx,
 dec = avcodec_find_decoder(st->codecpar->codec_id);
 if (!dec) {
 fprintf(stderr, "Failed to find %s codec\n",
-av_get_media_type_string(type));
+av_media_type_get_string(type));
 return AVERROR(EINVAL);
 }
 
@@ -173,21 +173,21 @@ static int open_codec_context(int *stream_idx,
 *dec_ctx = avcodec_alloc_context3(dec);
 if (!*dec_ctx) {
 fprintf(stderr, "Failed to allocate the %s codec context\n",
-av_get_media_type_string(type));
+av_media_type_get_string(type));
 return AVERROR(ENOMEM);
 }
 
 /* Copy codec parameters from input stream to output codec context */
 if ((ret = avcodec_parameters_to_context(*dec_ctx, st->codecpar)) < 0) 
{
 fprintf(stderr, "Failed to copy %s codec parameters to decoder 
context\n",
-av_get_media_type_string(type));
+av_media_type_get_string(type));
 return ret;
 }
 
 /* Init the decoders */
 if ((ret = avcodec_open2(*dec_ctx, dec, NULL)) < 0) {
 fprintf(stderr, "Failed to open %s codec\n",
-av_get_media_type_string(type));
+av_media_type_get_string(type));
 return ret;
 }
 *stream_idx = stream_index;
diff --git a/doc/examples/extract_mvs.c b/doc/examples/extract_mvs.c
index cc1311da91..2fb5517708 100644
--- a/doc/examples/extract_mvs.c
+++ b/doc/examples/extract_mvs.c
@@ -85,7 +85,7 @@ static int open_codec_context(AVFormatContext *fmt_ctx, enum 
AVMediaType type)
 ret = av_find_best_stream(fmt_ctx, type, -1, -1, , 0);
 if (ret < 0) {
 fprintf(stderr, "Could not find %s stream in input file '%s'\n",
-av_get_media_type_string(type), src_filename);
+av_media_type_get_string(type), src_filename);
 return ret;
 } else {
 int stream_idx = ret;
@@ -109,7 +109,7 @@ static int open_codec_context(AVFormatContext *fmt_ctx, 
enum AVMediaType type)
 av_dict_free();
 if (ret < 0) {
 fprintf(stderr, "Failed to open %s codec\n",
-av_get_media_type_string(type));
+av_media_type_get_string(type));
 return ret;
 }
 
diff --git a/fftools/cmdutils.c b/fftools/cmdutils.c
index 4b50e15eef..4504d2711c 100644
--- a/fftools/cmdutils.c
+++ b/fftools/cmdutils.c
@@ -1961,7 +1961,7 @@ static void show_help_filter(const char *name)
 count = avfilter_filter_pad_count(f, 0);
 for (i = 0; i < count; i++) {
 printf("   #%d: %s (%s)\n", i, avfilter_pad_get_name(f->inputs, i),
-   media_type_string(avfilter_pad_get_type(f->inputs, i)));
+   av_media_type_get_string(avfilter_pad_get_type(f->inputs, i)));
 }
 if (f->flags & AVFILTER_FLAG_DYNAMIC_INPUTS)
 printf("dynamic (depending on the options)\n");
@@ -1972,7 +1972,7 @@ static void show_help_filter(const char *name)
 count = avfilter_filter_pad_count(f, 1);
 

[FFmpeg-devel] [PATCH 1/2] libavutil: create av_media_type_get_string()

2022-02-03 Thread Scott Theisen
This deprecates av_get_media_type_string() which does return NULL.

printf %s with a null pointer is undefined behavior

This also matches behavior with avcodec_get_name() which never returns NULL,
instead it returns "unknown_codec".
---
 doc/APIchanges |  3 +++
 libavutil/avutil.h |  7 +++
 libavutil/utils.c  | 13 +
 3 files changed, 23 insertions(+)

diff --git a/doc/APIchanges b/doc/APIchanges
index 315a04c952..5b2ef724be 100644
--- a/doc/APIchanges
+++ b/doc/APIchanges
@@ -14,6 +14,9 @@ libavutil: 2021-04-27
 
 API changes, most recent first:
 
+2022-02-03 - xxx - lavu 57.19.100 - avutil.h
+  Add av_media_type_get_string() which deprecates av_get_media_type_string().
+
 2022-01-26 - af94ab7c7c0 - lavu 57.19.100 - tx.h
   Add AV_TX_FLOAT_RDFT, AV_TX_DOUBLE_RDFT and AV_TX_INT32_RDFT.
 
diff --git a/libavutil/avutil.h b/libavutil/avutil.h
index 4d633156d1..6cabeb405a 100644
--- a/libavutil/avutil.h
+++ b/libavutil/avutil.h
@@ -209,9 +209,16 @@ enum AVMediaType {
 /**
  * Return a string describing the media_type enum, NULL if media_type
  * is unknown.
+ *
+ * @deprecated Use av_media_type_get_string() instead.
  */
 const char *av_get_media_type_string(enum AVMediaType media_type);
 
+/**
+ * Return a string describing the media_type enum, never NULL.
+ */
+const char *av_media_type_get_string(enum AVMediaType media_type);
+
 /**
  * @defgroup lavu_const Constants
  * @{
diff --git a/libavutil/utils.c b/libavutil/utils.c
index ea9b5097b8..92d242f678 100644
--- a/libavutil/utils.c
+++ b/libavutil/utils.c
@@ -80,6 +80,19 @@ const char *av_get_media_type_string(enum AVMediaType 
media_type)
 }
 }
 
+const char *av_media_type_get_string(enum AVMediaType media_type)
+{
+switch (media_type) {
+case AVMEDIA_TYPE_UNKNOWN:return "unknown";
+case AVMEDIA_TYPE_VIDEO:  return "video";
+case AVMEDIA_TYPE_AUDIO:  return "audio";
+case AVMEDIA_TYPE_DATA:   return "data";
+case AVMEDIA_TYPE_SUBTITLE:   return "subtitle";
+case AVMEDIA_TYPE_ATTACHMENT: return "attachment";
+default:  return "invalid_media_type";
+}
+}
+
 char av_get_picture_type_char(enum AVPictureType pict_type)
 {
 switch (pict_type) {
-- 
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-requ...@ffmpeg.org with subject "unsubscribe".


[FFmpeg-devel] [PATCH v2] create av_media_type_get_string()

2022-02-03 Thread Scott Theisen
Instead of making an API breaking change, create a new function instead,
and deprecate the old one.

Then replace all uses in FFmpeg of the old function with the new one.


___
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".


[FFmpeg-devel] [PATCH v2 09/13] avpriv_find_start_code(): fix indent from previous commit

2022-02-03 Thread Scott Theisen
Whitespace change only.  Separate so the previous diff is clearer.
---
 libavcodec/utils.c | 26 +-
 1 file changed, 13 insertions(+), 13 deletions(-)

diff --git a/libavcodec/utils.c b/libavcodec/utils.c
index da057bad3e..255c133737 100644
--- a/libavcodec/utils.c
+++ b/libavcodec/utils.c
@@ -956,19 +956,19 @@ const uint8_t *avpriv_find_start_code(const uint8_t 
*av_restrict p,
 p += 3; // offset for negative indices in while loop
 }
 else {
-if (p >= end)
-return end;
-
-// read up to the first three bytes in p to enable reading a start code 
across
-// two (to four) buffers
-for (int i = 0; i < 3; i++) {
-*start_code <<= 8;
-*start_code += *p;
-p++;
-if (avpriv_start_code_is_valid(*start_code) || p == end)
-return p;
-}
-// p is now properly incremented for the negative indices in the while loop
+if (p >= end)
+return end;
+
+// read up to the first three bytes in p to enable reading a start 
code across
+// two (to four) buffers
+for (int i = 0; i < 3; i++) {
+*start_code <<= 8;
+*start_code += *p;
+p++;
+if (avpriv_start_code_is_valid(*start_code) || p == end)
+return p;
+}
+// p is now properly incremented for the negative indices in the while 
loop
 }
 
 /* with memory address increasing left to right, we are looking for (in 
hexadecimal):
-- 
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-requ...@ffmpeg.org with subject "unsubscribe".


[FFmpeg-devel] [PATCH v2 08/13] avpriv_find_start_code(): add parameter for ignoring history

2022-02-03 Thread Scott Theisen
If true, this skips the for loop at the beginning of avpriv_find_start_code().

If the state/start_code input is a local variable and only one buffer is used,
then no history is needed.

In loops and inline functions: if ignoring history, don't initialize start_code,
so it isn't reset twice each time.

cbs_mpeg2.c needs further changes to use output_only = true.

Use output_only = 0 for no functional change.  For example, if the 
state/start_code
is passed into the function calling avpriv_find_start_code().
---
 libavcodec/cavsdec.c  |  2 +-
 libavcodec/cbs_mpeg2.c|  4 ++--
 libavcodec/extract_extradata_bsf.c|  4 ++--
 libavcodec/h264_parser.c  |  2 +-
 libavcodec/internal.h |  9 -
 libavcodec/mpeg12.c   |  2 +-
 libavcodec/mpeg12dec.c|  9 -
 libavcodec/mpeg4_unpack_bframes_bsf.c |  3 +--
 libavcodec/mpegvideo_parser.c |  5 ++---
 libavcodec/remove_extradata_bsf.c |  8 
 libavcodec/utils.c| 14 +-
 libavcodec/vc1_common.h   |  4 ++--
 libavformat/avidec.c  |  6 +++---
 libavformat/avs2dec.c |  2 +-
 libavformat/avs3dec.c |  2 +-
 libavformat/cavsvideodec.c|  2 +-
 libavformat/mpegtsenc.c   |  4 ++--
 libavformat/mpegvideodec.c|  2 +-
 libavformat/mxfenc.c  |  2 +-
 libavformat/rtpenc_mpv.c  |  4 ++--
 20 files changed, 53 insertions(+), 37 deletions(-)

diff --git a/libavcodec/cavsdec.c b/libavcodec/cavsdec.c
index a62177d520..c4c78cd534 100644
--- a/libavcodec/cavsdec.c
+++ b/libavcodec/cavsdec.c
@@ -1248,7 +1248,7 @@ static int cavs_decode_frame(AVCodecContext *avctx, void 
*data, int *got_frame,
 buf_ptr = buf;
 buf_end = buf + buf_size;
 for(;;) {
-buf_ptr = avpriv_find_start_code(buf_ptr, buf_end, );
+buf_ptr = avpriv_find_start_code(buf_ptr, buf_end, , 1);
 if (!avpriv_start_code_is_valid(stc) || buf_ptr == buf_end) {
 if (!h->stc)
 av_log(h->avctx, AV_LOG_WARNING, "no frame decoded\n");
diff --git a/libavcodec/cbs_mpeg2.c b/libavcodec/cbs_mpeg2.c
index eb45929132..d41477620e 100644
--- a/libavcodec/cbs_mpeg2.c
+++ b/libavcodec/cbs_mpeg2.c
@@ -151,7 +151,7 @@ static int cbs_mpeg2_split_fragment(CodedBitstreamContext 
*ctx,
 int err, i, final = 0;
 
 start = avpriv_find_start_code(frag->data, frag->data + frag->data_size,
-   _code);
+   _code, 1);
 if (!avpriv_start_code_is_valid(start_code)) {
 // No start code found.
 return AVERROR_INVALIDDATA;
@@ -169,7 +169,7 @@ static int cbs_mpeg2_split_fragment(CodedBitstreamContext 
*ctx,
 }
 
 end = avpriv_find_start_code(start--, frag->data + frag->data_size,
- _code);
+ _code, 0);
 
 // start points to the byte containing the start_code_identifier
 // (may be the last byte of fragment->data); end points to the byte
diff --git a/libavcodec/extract_extradata_bsf.c 
b/libavcodec/extract_extradata_bsf.c
index 4df1c97139..1a3ddceff2 100644
--- a/libavcodec/extract_extradata_bsf.c
+++ b/libavcodec/extract_extradata_bsf.c
@@ -237,7 +237,7 @@ static int extract_extradata_vc1(AVBSFContext *ctx, 
AVPacket *pkt,
 int has_extradata = 0, extradata_size = 0;
 
 while (ptr < end) {
-ptr = avpriv_find_start_code(ptr, end, );
+ptr = avpriv_find_start_code(ptr, end, , 1);
 if (state == VC1_CODE_SEQHDR || state == VC1_CODE_ENTRYPOINT) {
 has_extradata = 1;
 } else if (has_extradata && avpriv_start_code_is_valid(state)) {
@@ -300,7 +300,7 @@ static int extract_extradata_mpeg4(AVBSFContext *ctx, 
AVPacket *pkt,
 uint32_t state = UINT32_MAX;
 
 while (ptr < end) {
-ptr = avpriv_find_start_code(ptr, end, );
+ptr = avpriv_find_start_code(ptr, end, , 1);
 if (state == 0x1B3 || state == 0x1B6) {
 if (ptr - pkt->data > 4) {
 *size = ptr - 4 - pkt->data;
diff --git a/libavcodec/h264_parser.c b/libavcodec/h264_parser.c
index 4002bcad77..0ca411c592 100644
--- a/libavcodec/h264_parser.c
+++ b/libavcodec/h264_parser.c
@@ -72,7 +72,7 @@ static int find_start_code(const uint8_t *buf, int buf_size,
 {
 uint32_t state = -1;
 
-buf_index = avpriv_find_start_code(buf + buf_index, buf + next_avc + 1, 
) - buf - 1;
+buf_index = avpriv_find_start_code(buf + buf_index, buf + next_avc + 1, 
, 1) - buf - 1;
 
 return FFMIN(buf_index, buf_size);
 }
diff --git a/libavcodec/internal.h b/libavcodec/internal.h
index dadd8d4a10..57e2816a95 100644
--- a/libavcodec/internal.h
+++ b/libavcodec/internal.h
@@ -309,6 +309,9 @@ static av_always_inline int 
avpriv_start_code_is_valid(uint32_t start_code) {
  * By preserving the @p start_code value 

[FFmpeg-devel] [PATCH v2 13/13] cbs_mpeg2.c: improve readability of start code search (part 3)

2022-02-03 Thread Scott Theisen
Separate from part 2 for a clearer diff.

Now the true loop condition has been revealed: start < buf_end

Clarify loop by moving the detection of sequence_end_codes out of the loop.
See also commit fd93d5efe64206d5f1bce8c702602353444c0c1a regarding 
sequence_end_codes
---
 libavcodec/cbs_mpeg2.c | 31 ++-
 1 file changed, 18 insertions(+), 13 deletions(-)

diff --git a/libavcodec/cbs_mpeg2.c b/libavcodec/cbs_mpeg2.c
index bf95fb7546..53aa0ed3f6 100644
--- a/libavcodec/cbs_mpeg2.c
+++ b/libavcodec/cbs_mpeg2.c
@@ -149,7 +149,7 @@ static int cbs_mpeg2_split_fragment(CodedBitstreamContext 
*ctx,
 CodedBitstreamUnitType unit_type;
 uint32_t start_code = -1;
 size_t unit_size;
-int err, final = 0;
+int err;
 int i = -1; // offset for pre-increment
 
 start = avpriv_find_start_code(start, buf_end, _code, 1);
@@ -161,16 +161,7 @@ static int cbs_mpeg2_split_fragment(CodedBitstreamContext 
*ctx,
 do {
 unit_type = start_code & 0xff;
 
-if (start == buf_end) {
-// The last four bytes form a start code which constitutes
-// a unit of its own.  In this situation avpriv_find_start_code
-// won't modify start_code at all so modify start_code so that
-// the next unit will be treated as the last unit.
-start_code = 0;
-}
-else {
-end = avpriv_find_start_code(start, buf_end, _code, 1);
-}
+end = avpriv_find_start_code(start, buf_end, _code, 1);
 start--;
 // decrement so start points to the byte containing the 
start_code_identifier
 // (may be the last byte of fragment->data); end points to the byte
@@ -182,8 +173,8 @@ static int cbs_mpeg2_split_fragment(CodedBitstreamContext 
*ctx,
 unit_size = (end - 4) - start;
 } else {
// We didn't find a start code, so this is the final unit.
+   // There is no start code to remove from end, hence not (end - 4).
unit_size = end - start;
-   final = 1;
 }
 
 err = ff_cbs_insert_unit_data(frag, ++i, unit_type,
@@ -193,7 +184,21 @@ static int cbs_mpeg2_split_fragment(CodedBitstreamContext 
*ctx,
 return err;
 
 start = end;
-} while (!final);
+} while (start < buf_end);
+
+if (avpriv_start_code_is_valid(start_code)) {
+// The last four bytes form a start code which constitutes
+// a unit of its own, with size 1.
+
+start--; // since start == buf_end because of the loop condition,
+// decrement so start points to the byte containing the 
start_code_identifier
+err = ff_cbs_insert_unit_data(frag, ++i, start_code & 0xFF,
+  (uint8_t*)start /* cast away the const 
to match parameter type */,
+  1, frag->data_ref);
+if (err < 0) {
+return err;
+}
+}
 
 return 0;
 }
-- 
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-requ...@ffmpeg.org with subject "unsubscribe".


[FFmpeg-devel] [PATCH v2 07/13] avpriv_find_start_code(): constify pointer parameters

2022-02-03 Thread Scott Theisen
Have the compiler enforce not changing the addresses these parameters point to.

No functional change.
---
 libavcodec/internal.h | 6 +++---
 libavcodec/utils.c| 4 ++--
 2 files changed, 5 insertions(+), 5 deletions(-)

diff --git a/libavcodec/internal.h b/libavcodec/internal.h
index 94c41aef0b..dadd8d4a10 100644
--- a/libavcodec/internal.h
+++ b/libavcodec/internal.h
@@ -313,7 +313,7 @@ static av_always_inline int 
avpriv_start_code_is_valid(uint32_t start_code) {
  * @param[in] end   A pointer to the past-the-end memory address for the buffer
  *  given by @p p.  @p p must be ≤ @p end.
  *
- * @param[in,out] start_code A reference to a mutable @c uint32_t.
+ * @param[in,out] start_code A constant pointer (reference) to a mutable @c 
uint32_t.
  *  As input: For no history preset to @c ~0 , otherwise preset 
to the last
  *returned start code to enable detecting start codes 
across
  *buffer boundaries.
@@ -325,8 +325,8 @@ static av_always_inline int 
avpriv_start_code_is_valid(uint32_t start_code) {
  * if no start code was found.
  */
 const uint8_t *avpriv_find_start_code(const uint8_t *p,
-  const uint8_t *end,
-  uint32_t *start_code);
+  const uint8_t * const end,
+  uint32_t * const start_code);
 
 int avpriv_codec_get_cap_skip_frame_fill_param(const AVCodec *codec);
 
diff --git a/libavcodec/utils.c b/libavcodec/utils.c
index 80ccde023f..cf92d29f67 100644
--- a/libavcodec/utils.c
+++ b/libavcodec/utils.c
@@ -941,8 +941,8 @@ void ff_thread_report_progress2(AVCodecContext *avctx, int 
field, int thread, in
 #endif
 
 const uint8_t *avpriv_find_start_code(const uint8_t *av_restrict p,
-  const uint8_t *end,
-  uint32_t *av_restrict start_code)
+  const uint8_t * const end,
+  uint32_t * const av_restrict start_code)
 {
 av_assert0(p <= end);
 if (p >= end)
-- 
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-requ...@ffmpeg.org with subject "unsubscribe".


[FFmpeg-devel] [PATCH v2 12/13] cbs_mpeg2.c: improve readability of start code search (part 2)

2022-02-03 Thread Scott Theisen
If the last four bytes form a valid start code, the loop would run another time.
Since (start == buf_end), start_code is invalidated so the loop terminates.

However, calling avpriv_find_start_code() with p == end, it will immediately
return due to the zero size buffer.  Thus the history is not needed.
---
 libavcodec/cbs_mpeg2.c | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)

diff --git a/libavcodec/cbs_mpeg2.c b/libavcodec/cbs_mpeg2.c
index f2efedde5d..bf95fb7546 100644
--- a/libavcodec/cbs_mpeg2.c
+++ b/libavcodec/cbs_mpeg2.c
@@ -168,8 +168,9 @@ static int cbs_mpeg2_split_fragment(CodedBitstreamContext 
*ctx,
 // the next unit will be treated as the last unit.
 start_code = 0;
 }
-
-end = avpriv_find_start_code(start, buf_end, _code, 0);
+else {
+end = avpriv_find_start_code(start, buf_end, _code, 1);
+}
 start--;
 // decrement so start points to the byte containing the 
start_code_identifier
 // (may be the last byte of fragment->data); end points to the byte
-- 
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-requ...@ffmpeg.org with subject "unsubscribe".


[FFmpeg-devel] [PATCH v2 11/13] cbs_mpeg2.c: improve readability of start code search (part 1)

2022-02-03 Thread Scott Theisen
ff_cbs_insert_unit_data() does not modify the data or data_size fields of
the CodedBitstreamFragment.  It also does not modify the value pointed to
by start.  (We don't need that byte in this function anymore, anyway.)
---
 libavcodec/cbs_mpeg2.c | 26 +-
 1 file changed, 13 insertions(+), 13 deletions(-)

diff --git a/libavcodec/cbs_mpeg2.c b/libavcodec/cbs_mpeg2.c
index afe78eef9a..f2efedde5d 100644
--- a/libavcodec/cbs_mpeg2.c
+++ b/libavcodec/cbs_mpeg2.c
@@ -144,23 +144,24 @@ static int cbs_mpeg2_split_fragment(CodedBitstreamContext 
*ctx,
 CodedBitstreamFragment *frag,
 int header)
 {
-const uint8_t *start, *end;
+const uint8_t *start = frag->data, *end;
+const uint8_t * const buf_end = frag->data + frag->data_size;
 CodedBitstreamUnitType unit_type;
 uint32_t start_code = -1;
 size_t unit_size;
-int err, i = 0, final = 0;
+int err, final = 0;
+int i = -1; // offset for pre-increment
 
-start = avpriv_find_start_code(frag->data, frag->data + frag->data_size,
-   _code, 1);
+start = avpriv_find_start_code(start, buf_end, _code, 1);
 if (!avpriv_start_code_is_valid(start_code)) {
 // No start code found.
 return AVERROR_INVALIDDATA;
 }
 
-while (!final) {
+do {
 unit_type = start_code & 0xff;
 
-if (start == frag->data + frag->data_size) {
+if (start == buf_end) {
 // The last four bytes form a start code which constitutes
 // a unit of its own.  In this situation avpriv_find_start_code
 // won't modify start_code at all so modify start_code so that
@@ -168,10 +169,9 @@ static int cbs_mpeg2_split_fragment(CodedBitstreamContext 
*ctx,
 start_code = 0;
 }
 
-end = avpriv_find_start_code(start--, frag->data + frag->data_size,
- _code, 0);
-
-// start points to the byte containing the start_code_identifier
+end = avpriv_find_start_code(start, buf_end, _code, 0);
+start--;
+// decrement so start points to the byte containing the 
start_code_identifier
 // (may be the last byte of fragment->data); end points to the byte
 // following the byte containing the start code identifier (or to
 // the end of fragment->data).
@@ -185,14 +185,14 @@ static int cbs_mpeg2_split_fragment(CodedBitstreamContext 
*ctx,
final = 1;
 }
 
-err = ff_cbs_insert_unit_data(frag, i, unit_type, (uint8_t*)start,
+err = ff_cbs_insert_unit_data(frag, ++i, unit_type,
+  (uint8_t*)start /* cast away the const 
to match parameter type */,
   unit_size, frag->data_ref);
 if (err < 0)
 return err;
 
 start = end;
-i++;
-}
+} while (!final);
 
 return 0;
 }
-- 
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-requ...@ffmpeg.org with subject "unsubscribe".


[FFmpeg-devel] [PATCH v2 06/13] avpriv_find_start_code(): correct type of start_code parameter

2022-02-03 Thread Scott Theisen
---
 libavcodec/mpeg12dec.c   | 2 +-
 libavformat/rtpenc_mpv.c | 3 +--
 2 files changed, 2 insertions(+), 3 deletions(-)

diff --git a/libavcodec/mpeg12dec.c b/libavcodec/mpeg12dec.c
index 65b66d11f8..6b0b84ae64 100644
--- a/libavcodec/mpeg12dec.c
+++ b/libavcodec/mpeg12dec.c
@@ -1770,7 +1770,7 @@ static int mpeg_decode_slice(MpegEncContext *s, int mb_y,
 
 if (avctx->hwaccel && avctx->hwaccel->decode_slice) {
 const uint8_t *buf_end, *buf_start = *buf - 4; /* include start_code */
-int start_code = -1;
+uint32_t start_code = ~0;
 buf_end = avpriv_find_start_code(buf_start + 2, *buf + buf_size, 
_code);
 if (buf_end < *buf + buf_size)
 buf_end -= 4;
diff --git a/libavformat/rtpenc_mpv.c b/libavformat/rtpenc_mpv.c
index 05a77fa11c..91c07574bb 100644
--- a/libavformat/rtpenc_mpv.c
+++ b/libavformat/rtpenc_mpv.c
@@ -51,11 +51,10 @@ void ff_rtp_send_mpegvideo(AVFormatContext *s1, const 
uint8_t *buf1, int size)
 end_of_slice = 1;
 } else {
 const uint8_t *r, *r1;
-int start_code;
 
 r1 = buf1;
 while (1) {
-start_code = -1;
+uint32_t start_code = ~0;
 r = avpriv_find_start_code(r1, end, _code);
 if (avpriv_start_code_is_valid(start_code)) {
 /* New start code found */
-- 
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-requ...@ffmpeg.org with subject "unsubscribe".


[FFmpeg-devel] [PATCH v2 05/13] avpriv_find_start_code(): add doxygen comment and rename a parameter

2022-02-03 Thread Scott Theisen
---
 libavcodec/internal.h | 26 +-
 libavcodec/utils.c| 10 +-
 2 files changed, 30 insertions(+), 6 deletions(-)

diff --git a/libavcodec/internal.h b/libavcodec/internal.h
index 005f308b70..94c41aef0b 100644
--- a/libavcodec/internal.h
+++ b/libavcodec/internal.h
@@ -300,9 +300,33 @@ static av_always_inline int 
avpriv_start_code_is_valid(uint32_t start_code) {
 return (start_code & 0xFF00) == 0x100;
 }
 
+/**
+ * @brief Find the first start code in the buffer @p p.
+ *
+ * A start code is a sequence of 4 bytes with the hexadecimal value  00 
00 01 XX ,
+ * where  XX  represents any value and memory address 
increases left to right.
+ *
+ * By preserving the @p start_code value between subsequent calls, the 
caller can
+ * detect start codes across buffer boundaries.
+ *
+ * @param[in] p A pointer to the start of the memory buffer to scan.
+ * @param[in] end   A pointer to the past-the-end memory address for the buffer
+ *  given by @p p.  @p p must be ≤ @p end.
+ *
+ * @param[in,out] start_code A reference to a mutable @c uint32_t.
+ *  As input: For no history preset to @c ~0 , otherwise preset 
to the last
+ *returned start code to enable detecting start codes 
across
+ *buffer boundaries.
+ *  On output: Set to the found start code if it exists or an invalid
+ * start code (the 4 bytes prior to the returned value,
+ * using the input history if @f$ p - end < 4 @f$).
+ *
+ * @return A pointer to the memory address following the found start code, or 
@p end
+ * if no start code was found.
+ */
 const uint8_t *avpriv_find_start_code(const uint8_t *p,
   const uint8_t *end,
-  uint32_t *state);
+  uint32_t *start_code);
 
 int avpriv_codec_get_cap_skip_frame_fill_param(const AVCodec *codec);
 
diff --git a/libavcodec/utils.c b/libavcodec/utils.c
index 1a806b9fa8..80ccde023f 100644
--- a/libavcodec/utils.c
+++ b/libavcodec/utils.c
@@ -942,7 +942,7 @@ void ff_thread_report_progress2(AVCodecContext *avctx, int 
field, int thread, in
 
 const uint8_t *avpriv_find_start_code(const uint8_t *av_restrict p,
   const uint8_t *end,
-  uint32_t *av_restrict state)
+  uint32_t *av_restrict start_code)
 {
 av_assert0(p <= end);
 if (p >= end)
@@ -951,10 +951,10 @@ const uint8_t *avpriv_find_start_code(const uint8_t 
*av_restrict p,
 // read up to the first three bytes in p to enable reading a start code 
across
 // two (to four) buffers
 for (int i = 0; i < 3; i++) {
-*state <<= 8;
-*state += *p;
+*start_code <<= 8;
+*start_code += *p;
 p++;
-if (avpriv_start_code_is_valid(*state) || p == end)
+if (avpriv_start_code_is_valid(*start_code) || p == end)
 return p;
 }
 // p is now properly incremented for the negative indices in the while loop
@@ -988,7 +988,7 @@ const uint8_t *avpriv_find_start_code(const uint8_t 
*av_restrict p,
 // this will cause the last 4 bytes before end to be read,
 // i.e. no out of bounds memory access occurs
 
-*state = AV_RB32(p - 4);
+*start_code = AV_RB32(p - 4);
 // read the previous 4 bytes, i.e. bytes {p - 4, p - 3, p - 2, p - 1}
 
 return p;
-- 
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-requ...@ffmpeg.org with subject "unsubscribe".


[FFmpeg-devel] [PATCH v2 10/13] cbs_mpeg2.c: use a while loop with a loop condition instead of an infinite loop

2022-02-03 Thread Scott Theisen
This enhances the clarity of the code.
---
 libavcodec/cbs_mpeg2.c | 8 +++-
 1 file changed, 3 insertions(+), 5 deletions(-)

diff --git a/libavcodec/cbs_mpeg2.c b/libavcodec/cbs_mpeg2.c
index d41477620e..afe78eef9a 100644
--- a/libavcodec/cbs_mpeg2.c
+++ b/libavcodec/cbs_mpeg2.c
@@ -148,7 +148,7 @@ static int cbs_mpeg2_split_fragment(CodedBitstreamContext 
*ctx,
 CodedBitstreamUnitType unit_type;
 uint32_t start_code = -1;
 size_t unit_size;
-int err, i, final = 0;
+int err, i = 0, final = 0;
 
 start = avpriv_find_start_code(frag->data, frag->data + frag->data_size,
_code, 1);
@@ -157,7 +157,7 @@ static int cbs_mpeg2_split_fragment(CodedBitstreamContext 
*ctx,
 return AVERROR_INVALIDDATA;
 }
 
-for (i = 0;; i++) {
+while (!final) {
 unit_type = start_code & 0xff;
 
 if (start == frag->data + frag->data_size) {
@@ -190,10 +190,8 @@ static int cbs_mpeg2_split_fragment(CodedBitstreamContext 
*ctx,
 if (err < 0)
 return err;
 
-if (final)
-break;
-
 start = end;
+i++;
 }
 
 return 0;
-- 
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-requ...@ffmpeg.org with subject "unsubscribe".


[FFmpeg-devel] [PATCH v2 04/13] avpriv_find_start_code(): rewrite for loop for clarity

2022-02-03 Thread Scott Theisen
---
 libavcodec/utils.c | 14 --
 1 file changed, 8 insertions(+), 6 deletions(-)

diff --git a/libavcodec/utils.c b/libavcodec/utils.c
index 8f8cc820bd..1a806b9fa8 100644
--- a/libavcodec/utils.c
+++ b/libavcodec/utils.c
@@ -944,18 +944,20 @@ const uint8_t *avpriv_find_start_code(const uint8_t 
*av_restrict p,
   const uint8_t *end,
   uint32_t *av_restrict state)
 {
-int i;
-
 av_assert0(p <= end);
 if (p >= end)
 return end;
 
-for (i = 0; i < 3; i++) {
-uint32_t tmp = *state << 8;
-*state = tmp + *(p++);
-if (tmp == 0x100 || p == end)
+// read up to the first three bytes in p to enable reading a start code 
across
+// two (to four) buffers
+for (int i = 0; i < 3; i++) {
+*state <<= 8;
+*state += *p;
+p++;
+if (avpriv_start_code_is_valid(*state) || p == end)
 return p;
 }
+// p is now properly incremented for the negative indices in the while loop
 
 /* with memory address increasing left to right, we are looking for (in 
hexadecimal):
  * 00 00 01 XX
-- 
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-requ...@ffmpeg.org with subject "unsubscribe".


[FFmpeg-devel] [PATCH v2 03/13] avpriv_find_start_code(): rewrite while loop and add comments for clarity

2022-02-03 Thread Scott Theisen
The expected number of iterations may increase by one for an input of 
alternating
0 and 1 bytes.  Instead of incrementing by 2 everytime, it now alternates 
between
incrementing by 1 and by 3.

No functional change, but now much clearer.

For the check p[-2] != 0:
Also reduce the number of iterations by correctly starting with three new bytes 
on the next iteration,
instead of keeping byte p[-3] which is invalid, i.e. known to be 01 when it 
must be 00.
---
 libavcodec/utils.c | 25 -
 1 file changed, 20 insertions(+), 5 deletions(-)

diff --git a/libavcodec/utils.c b/libavcodec/utils.c
index cb4437edc2..8f8cc820bd 100644
--- a/libavcodec/utils.c
+++ b/libavcodec/utils.c
@@ -957,12 +957,26 @@ const uint8_t *avpriv_find_start_code(const uint8_t 
*av_restrict p,
 return p;
 }
 
+/* with memory address increasing left to right, we are looking for (in 
hexadecimal):
+ * 00 00 01 XX
+ * p points at the address which should have the value of XX
+ */
 while (p < end) {
-if  (p[-1] > 1  ) p += 3;
-else if (p[-2]  ) p += 2;
-else if (p[-3]|(p[-1]-1)) p++;
+// UU UU UU
+if  (p[-1]  > 1) p += 3;// start check over with 3 new bytes
+else if (p[-1] == 0) p++;   // could be in a start code, so check 
next byte
+// this should be one comparison against 1 since p is unsigned,
+// i.e. p[-1] == 0 is equivalent to p[-1] < 1
+
+// UU UU 01
+else if (p[-2] != 0) p += 3;// we have UU YY 01, so increment by 3
+// to start check over with 3 new bytes
+// UU 00 01
+else if (p[-3] != 0) p += 3;// we have YY 00 01, so increment by 3
+// to start check over with 3 new bytes
+// 00 00 01
 else {
-p++;
+p++; // p now points at the address following the start code value 
XX
 break;
 }
 }
@@ -972,7 +986,8 @@ const uint8_t *avpriv_find_start_code(const uint8_t 
*av_restrict p,
 // this will cause the last 4 bytes before end to be read,
 // i.e. no out of bounds memory access occurs
 
-*state = AV_RB32(p - 4); // read the previous 4 bytes
+*state = AV_RB32(p - 4);
+// read the previous 4 bytes, i.e. bytes {p - 4, p - 3, p - 2, p - 1}
 
 return p;
 }
-- 
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-requ...@ffmpeg.org with subject "unsubscribe".


[FFmpeg-devel] [PATCH v2 02/13] avpriv_find_start_code(): readability enhancement part 1

2022-02-03 Thread Scott Theisen
No functional change.
---
 libavcodec/utils.c | 10 +++---
 1 file changed, 7 insertions(+), 3 deletions(-)

diff --git a/libavcodec/utils.c b/libavcodec/utils.c
index b19befef21..cb4437edc2 100644
--- a/libavcodec/utils.c
+++ b/libavcodec/utils.c
@@ -967,10 +967,14 @@ const uint8_t *avpriv_find_start_code(const uint8_t 
*av_restrict p,
 }
 }
 
-p = FFMIN(p, end) - 4;
-*state = AV_RB32(p);
+if (p > end)
+p = end;
+// this will cause the last 4 bytes before end to be read,
+// i.e. no out of bounds memory access occurs
 
-return p + 4;
+*state = AV_RB32(p - 4); // read the previous 4 bytes
+
+return p;
 }
 
 AVCPBProperties *av_cpb_properties_alloc(size_t *size)
-- 
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-requ...@ffmpeg.org with subject "unsubscribe".


[FFmpeg-devel] [PATCH v2 01/13] avcodec/internal.h: create avpriv_start_code_is_valid()

2022-02-03 Thread Scott Theisen
---
 libavcodec/cavsdec.c   |  2 +-
 libavcodec/cbs_mpeg2.c |  4 ++--
 libavcodec/extract_extradata_bsf.c |  2 +-
 libavcodec/internal.h  | 15 +++
 libavcodec/mpeg12.c|  2 +-
 libavcodec/mpeg12dec.c |  2 +-
 libavcodec/mpegvideo_parser.c  |  2 +-
 libavcodec/remove_extradata_bsf.c  |  8 +++-
 libavcodec/vaapi_vc1.c |  2 +-
 libavcodec/vc1_common.h|  4 +---
 libavcodec/vc1dec.c|  2 +-
 libavformat/avs2dec.c  |  4 ++--
 libavformat/avs3dec.c  |  4 ++--
 libavformat/cavsvideodec.c |  2 +-
 libavformat/mpegvideodec.c |  2 +-
 libavformat/rtpenc_mpv.c   |  2 +-
 16 files changed, 35 insertions(+), 24 deletions(-)

diff --git a/libavcodec/cavsdec.c b/libavcodec/cavsdec.c
index 692c77eb39..a62177d520 100644
--- a/libavcodec/cavsdec.c
+++ b/libavcodec/cavsdec.c
@@ -1249,7 +1249,7 @@ static int cavs_decode_frame(AVCodecContext *avctx, void 
*data, int *got_frame,
 buf_end = buf + buf_size;
 for(;;) {
 buf_ptr = avpriv_find_start_code(buf_ptr, buf_end, );
-if ((stc & 0xFE00) || buf_ptr == buf_end) {
+if (!avpriv_start_code_is_valid(stc) || buf_ptr == buf_end) {
 if (!h->stc)
 av_log(h->avctx, AV_LOG_WARNING, "no frame decoded\n");
 return FFMAX(0, buf_ptr - buf);
diff --git a/libavcodec/cbs_mpeg2.c b/libavcodec/cbs_mpeg2.c
index 26400f279f..eb45929132 100644
--- a/libavcodec/cbs_mpeg2.c
+++ b/libavcodec/cbs_mpeg2.c
@@ -152,7 +152,7 @@ static int cbs_mpeg2_split_fragment(CodedBitstreamContext 
*ctx,
 
 start = avpriv_find_start_code(frag->data, frag->data + frag->data_size,
_code);
-if (start_code >> 8 != 0x01) {
+if (!avpriv_start_code_is_valid(start_code)) {
 // No start code found.
 return AVERROR_INVALIDDATA;
 }
@@ -175,7 +175,7 @@ static int cbs_mpeg2_split_fragment(CodedBitstreamContext 
*ctx,
 // (may be the last byte of fragment->data); end points to the byte
 // following the byte containing the start code identifier (or to
 // the end of fragment->data).
-if (start_code >> 8 == 0x01) {
+if (avpriv_start_code_is_valid(start_code)) {
 // Unit runs from start to the beginning of the start code
 // pointed to by end (including any padding zeroes).
 unit_size = (end - 4) - start;
diff --git a/libavcodec/extract_extradata_bsf.c 
b/libavcodec/extract_extradata_bsf.c
index dbcb8508b0..4df1c97139 100644
--- a/libavcodec/extract_extradata_bsf.c
+++ b/libavcodec/extract_extradata_bsf.c
@@ -240,7 +240,7 @@ static int extract_extradata_vc1(AVBSFContext *ctx, 
AVPacket *pkt,
 ptr = avpriv_find_start_code(ptr, end, );
 if (state == VC1_CODE_SEQHDR || state == VC1_CODE_ENTRYPOINT) {
 has_extradata = 1;
-} else if (has_extradata && IS_MARKER(state)) {
+} else if (has_extradata && avpriv_start_code_is_valid(state)) {
 extradata_size = ptr - 4 - pkt->data;
 break;
 }
diff --git a/libavcodec/internal.h b/libavcodec/internal.h
index 72ca1553f6..005f308b70 100644
--- a/libavcodec/internal.h
+++ b/libavcodec/internal.h
@@ -285,6 +285,21 @@ int ff_thread_can_start_frame(AVCodecContext *avctx);
 
 int avpriv_h264_has_num_reorder_frames(AVCodecContext *avctx);
 
+/**
+ * @brief Test whether a start code found by avpriv_find_start_code() is valid.
+ *
+ * Use this to test the validity of a start code especially if a start code can
+ * be at the end of the buffer, where testing the return value of 
avpriv_find_start_code()
+ * would incorrectly imply that the start code is invalid (since the returned 
value
+ * equals @c end ).
+ *
+ * @param[in] start_code The start code to test.
+ * @return A boolean that is true if and only if @p start_code is valid
+ */
+static av_always_inline int avpriv_start_code_is_valid(uint32_t start_code) {
+return (start_code & 0xFF00) == 0x100;
+}
+
 const uint8_t *avpriv_find_start_code(const uint8_t *p,
   const uint8_t *end,
   uint32_t *state);
diff --git a/libavcodec/mpeg12.c b/libavcodec/mpeg12.c
index 58e03c05d4..e45bc74479 100644
--- a/libavcodec/mpeg12.c
+++ b/libavcodec/mpeg12.c
@@ -217,7 +217,7 @@ int ff_mpeg1_find_frame_end(ParseContext *pc, const uint8_t 
*buf, int buf_size,
 pc->frame_start_found = 0;
 if (pc->frame_start_found  < 4 && state == EXT_START_CODE)
 pc->frame_start_found++;
-if (pc->frame_start_found == 4 && (state & 0xFF00) == 0x100) {
+if (pc->frame_start_found == 4 && 
avpriv_start_code_is_valid(state)) {
 if (state < SLICE_MIN_START_CODE || state > 
SLICE_MAX_START_CODE) {
 pc->frame_start_found = 0;
 

[FFmpeg-devel] [PATCH v2 0/13] rewrite avpriv_find_start_code() for clarity

2022-02-03 Thread Scott Theisen
start_code is used as an [in,out] parameter only twice, once each in mpeg12.c
and mpegvideo_parser.c.  The input part of avpriv_find_start_code() has now
been rewritten to still allow this.

I did manage to run FATE this time and it completes without errors.

Regards,
Scott



___
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 3/8] avpriv_find_start_code(): make the state parameter output only

2022-02-01 Thread Scott Theisen

On 2/1/22 19:15, Andreas Rheinhardt wrote:

It seems you didn't get why the code currently is as it is: It allows to
use this function with non-contiguous input (as happens in our parsers
which operate on data that is not cleanly split into packets yet (it is
their job to create proper packets out of the input)). As an example,
say you have a buffer that ends with 0x00 00 and the next buffer
starting with 0x01 01. Then avpriv_find_start_code() will detect a start
code at the beginning of the second buffer if the user passes in the
same state again (and does not modify it in the meantime).


Without documentation or comments, in/out parameters are hard to infer 
and most uses were only out.


I might make a context free version for when the buffer is already 
packetized properly.



You would have noticed this if you had run FATE on your patches (see
https://ffmpeg.org/fate.html#Using-FATE-from-your-FFmpeg-source-directory).


Thanks for the link, I didn't see how to run FATE or get the samples for it.

Unfortunately, FATE fails before that:
```
CC    tests/api/api-threadmessage-test.o
LD    tests/api/api-threadmessage-test
TEST    api-threadmessage
Test api-threadmessage failed. Look at 
tests/data/fate/api-threadmessage.err for details.

make: *** [tests/Makefile:257: fate-api-threadmessage] Error 127
```

```
cat tests/data/fate/api-threadmessage.err
/home/htpc/build/mythtv-ffmpeg/FFmpeg/tests/api/api-threadmessage-test: 
error while loading shared libraries: libavutil.so.57: cannot open 
shared object file: No such file or directory

```

Which is strange because it does exist.
```
ls -l libavutil/libavutil*
-rw-rw-r-- 1 htpc htpc 4426450 Feb  1 22:06 libavutil/libavutil.a
-rw-rw-r-- 1 htpc htpc 353 Feb  1 21:06 libavutil/libavutil.pc
lrwxrwxrwx 1 htpc htpc  15 Feb  1 22:04 libavutil/libavutil.so -> 
libavutil.so.57

-rwxrwxr-x 1 htpc htpc 2602848 Feb  1 22:04 libavutil/libavutil.so.57
-rw-rw-r-- 1 htpc htpc  68 Nov  1 22:34 libavutil/libavutil.v
-rw-rw-r-- 1 htpc htpc  65 Feb  1 22:04 libavutil/libavutil.ver
-rw-rw-r-- 1 htpc htpc  82 Feb  1 21:05 libavutil/libavutil.version
```

I run configure with:
./configure --sysinclude=/usr/include --cc='ccache gcc' --cxx='ccache 
g++' --prefix=/usr/local --libdir=/usr/local/lib --enable-vdpau 
--enable-libxml2 --enable-libass --enable-libbluray --enable-vaapi 
--enable-libdrm --enable-gnutls --disable-stripping --disable-manpages 
--disable-podpages --disable-doc --disable-nvenc --enable-shared 
--disable-static --enable-gpl --enable-pic


Any idea why it's failing?

Thanks,
Scott
___
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] lavu: make av_get_media_type_string() never return NULL

2022-02-01 Thread Scott Theisen

On 2/1/22 21:06, James Almer wrote:

32/44 uses do not check for null.


But do they need to? Those modules have probably ensured media type is 
one of the known and supported ones by the time they call this function.


Why do you need this function to never return NULL, anyway? If you 
know any of these calls where media type can be UNKNOWN, then it's 
easier to just fix them by checking for NULL as per the doxy. 
The impetus was the desire to use this function in MythTV without 
invoking undefined behavior.  Currently MythTV uses a modification to 
ffmpeg: 
https://github.com/MythTV/mythtv/blob/7fa02d4dc3ab0d3f2cbfcbc514047fe2736fe9cf/mythtv/external/FFmpeg/libavcodec/utils-mythtv.c#L228
(For reference, I just tested and a NULL/nullptr const char* becomes an 
empty QString, but this is not documented.)


Also, another reason was to match behavior with avcodec_get_name() which 
never returns NULL, instead it returns "unknown_codec".


On 2/1/22 21:13, Andreas Rheinhardt wrote:

That would need an announcement in doc/APIchanges; it can then be
changed at the next major version bump after two years have passed.
So, would creating a new function instead and deprecating the old one be 
a better option?  (e.g. av_get_media_type_name)

Who sets invalid media types?
(In case of fftools/*: If they don't set it themselves, they (and all
our users) should be able to rely on our libraries to not set invalid
values. The same goes for all demuxers (as they set this value
themselves). Checks are only necessary where the media type comes from
the user; this means muxers (where the check should be done generically)
and possibly avfiltergraph.c (I am pretty sure that the type has already
been checked earlier for filters).)
I agree invalid media types are probably not set by anyone, but without 
checking for non-NULL it potentially invokes undefined behavior.


Returning "unknown" for AVMEDIA_TYPE_UNKNOWN and returning a different 
value for the default case is, in my opinion clearer. 
"invalid_media_type" may be a better default string to match the 
behavior of avcodec_get_name().


Regards,
Scott
___
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] lavu: make av_get_media_type_string() never return NULL

2022-02-01 Thread Scott Theisen

On 2/1/22 17:34, James Almer wrote:
This is an API breakage, so it's not ok. 

I'm new, so would this require a bump to the MINOR or MICRO version number?

The doxy states it returns NULL if media_type is unknown, so you're 
expected to check the returned pointer before trying to use it. 


git grep -E --count "av_get_media_type_string"

doc/APIchanges:1
doc/examples/demuxing_decoding.c:5
doc/examples/extract_mvs.c:2
fftools/cmdutils.c:1
fftools/cmdutils.h:1
fftools/ffmpeg.c:5
fftools/ffmpeg_opt.c:1
fftools/ffplay.c:2
fftools/ffprobe.c:3
libavcodec/avcodec.c:1
libavcodec/tests/avcodec.c:1
libavdevice/dshow.c:2
libavfilter/avfilter.c:2
libavfilter/avfiltergraph.c:2
libavfilter/src_movie.c:3
libavformat/avienc.c:1
libavformat/flvenc.c:1
libavformat/framehash.c:1
libavformat/mov.c:1
libavformat/mpegenc.c:1
libavformat/mpegts.c:1
libavformat/mxfdec.c:1
libavformat/segment.c:1
libavformat/tee.c:1
libavformat/uncodedframecrcenc.c:1
libavutil/avutil.h:1
libavutil/utils.c:1

44 total - 1 non-code - 1 prototype - 1 definition + 4 via macro - 1 
macro = 44 uses


The following uses of av_get_media_type_string() do not check for null:
doc/examples/demuxing_decoding.c: 5
doc/examples/extract_mvs.c: 2
fftools/cmdutils.c: 2 via the define in cmdutils.h
fftools/ffmpeg.c: 7, 2 via the define in cmdutils.h
fftools/ffmpeg_opt.c:1
fftools/ffplay.c:2
libavfilter/avfiltergraph.c:2
libavfilter/src_movie.c:3
libavformat/flvenc.c:1
libavformat/framehash.c:1
libavformat/mov.c:1
libavformat/mpegenc.c:1
libavformat/mpegts.c:1
libavformat/mxfdec.c:1
libavformat/segment.c:1
libavformat/tee.c:1

32/44 uses do not check for null.


libavcodec/tests/avcodec.c: this use is a workaround to 
av_get_media_type_string() returning null; checked 3 times



Let me know which version number to bump and I will submit a new version 
that also removes the now redundant null checks from the rest of the code.


Thanks,
Scott
___
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".


[FFmpeg-devel] [PATCH] lavu: make av_get_media_type_string() never return NULL

2022-02-01 Thread Scott Theisen
printf %s with a null pointer is undefined behavior
---
 libavutil/avutil.h | 3 +--
 libavutil/utils.c  | 3 ++-
 2 files changed, 3 insertions(+), 3 deletions(-)

diff --git a/libavutil/avutil.h b/libavutil/avutil.h
index 4d633156d1..4bd468d72f 100644
--- a/libavutil/avutil.h
+++ b/libavutil/avutil.h
@@ -207,8 +207,7 @@ enum AVMediaType {
 };
 
 /**
- * Return a string describing the media_type enum, NULL if media_type
- * is unknown.
+ * Return a string describing the media_type enum, never NULL.
  */
 const char *av_get_media_type_string(enum AVMediaType media_type);
 
diff --git a/libavutil/utils.c b/libavutil/utils.c
index ea9b5097b8..c85d7abace 100644
--- a/libavutil/utils.c
+++ b/libavutil/utils.c
@@ -71,12 +71,13 @@ const char *avutil_license(void)
 const char *av_get_media_type_string(enum AVMediaType media_type)
 {
 switch (media_type) {
+case AVMEDIA_TYPE_UNKNOWN:return "unknown";
 case AVMEDIA_TYPE_VIDEO:  return "video";
 case AVMEDIA_TYPE_AUDIO:  return "audio";
 case AVMEDIA_TYPE_DATA:   return "data";
 case AVMEDIA_TYPE_SUBTITLE:   return "subtitle";
 case AVMEDIA_TYPE_ATTACHMENT: return "attachment";
-default:  return NULL;
+default:  return "invalid";
 }
 }
 
-- 
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-requ...@ffmpeg.org with subject "unsubscribe".


[FFmpeg-devel] [PATCH 8/8] avpriv_find_start_code(): reduce the number of iterations

2022-02-01 Thread Scott Theisen
by correctly starting with three new bytes on the next iteration,
instead of keeping byte p[-3] which is invalid, i.e. known to be 01
when it must be 00.
---
 libavcodec/utils.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/libavcodec/utils.c b/libavcodec/utils.c
index b4c5fa5009..d485d0c96b 100644
--- a/libavcodec/utils.c
+++ b/libavcodec/utils.c
@@ -965,7 +965,7 @@ const uint8_t *avpriv_find_start_code(const uint8_t 
*av_restrict p,
 // i.e. p[-1] == 0 is equivalent to p[-1] < 1
 
 // UU UU 01
-else if (p[-2] != 0) p += 2;// we have UU YY 01, so increment by 2
+else if (p[-2] != 0) p += 3;// we have UU YY 01, so increment by 3
 // to start check over with 3 new bytes
 // UU 00 01
 else if (p[-3] != 0) p += 3;// we have YY 00 01, so increment by 3
-- 
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-requ...@ffmpeg.org with subject "unsubscribe".


[FFmpeg-devel] [PATCH 7/8] cbs_mpeg2.c: use a while loop with a loop condition instead of an infinite loop

2022-02-01 Thread Scott Theisen
This enhances the clarity of the code.
---
 libavcodec/cbs_mpeg2.c | 8 +++-
 1 file changed, 3 insertions(+), 5 deletions(-)

diff --git a/libavcodec/cbs_mpeg2.c b/libavcodec/cbs_mpeg2.c
index 648b270f44..2b80266910 100644
--- a/libavcodec/cbs_mpeg2.c
+++ b/libavcodec/cbs_mpeg2.c
@@ -148,7 +148,7 @@ static int cbs_mpeg2_split_fragment(CodedBitstreamContext 
*ctx,
 CodedBitstreamUnitType unit_type;
 uint32_t start_code = -1;
 size_t unit_size;
-int err, i, final = 0;
+int err, i = 0, final = 0;
 
 start = avpriv_find_start_code(frag->data, frag->data + frag->data_size,
_code);
@@ -157,7 +157,7 @@ static int cbs_mpeg2_split_fragment(CodedBitstreamContext 
*ctx,
 return AVERROR_INVALIDDATA;
 }
 
-for (i = 0;; i++) {
+while (!final) {
 unit_type = start_code & 0xff;
 
 end = avpriv_find_start_code(start--, frag->data + frag->data_size,
@@ -182,10 +182,8 @@ static int cbs_mpeg2_split_fragment(CodedBitstreamContext 
*ctx,
 if (err < 0)
 return err;
 
-if (final)
-break;
-
 start = end;
+i++;
 }
 
 return 0;
-- 
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-requ...@ffmpeg.org with subject "unsubscribe".


[FFmpeg-devel] [PATCH 6/8] avcodec/internal.h: create avpriv_start_code_is_valid()

2022-02-01 Thread Scott Theisen
---
 libavcodec/cavsdec.c   |  2 +-
 libavcodec/cbs_mpeg2.c |  4 ++--
 libavcodec/extract_extradata_bsf.c |  2 +-
 libavcodec/internal.h  | 14 ++
 libavcodec/mpeg12.c|  2 +-
 libavcodec/mpeg12dec.c |  2 +-
 libavcodec/mpegvideo_parser.c  |  2 +-
 libavcodec/remove_extradata_bsf.c  |  8 +++-
 libavcodec/vaapi_vc1.c |  2 +-
 libavcodec/vc1_common.h|  4 +---
 libavcodec/vc1dec.c|  2 +-
 libavformat/avs2dec.c  |  4 ++--
 libavformat/avs3dec.c  |  4 ++--
 libavformat/cavsvideodec.c |  2 +-
 libavformat/mpegvideodec.c |  2 +-
 libavformat/rtpenc_mpv.c   |  2 +-
 16 files changed, 34 insertions(+), 24 deletions(-)

diff --git a/libavcodec/cavsdec.c b/libavcodec/cavsdec.c
index 692c77eb39..a62177d520 100644
--- a/libavcodec/cavsdec.c
+++ b/libavcodec/cavsdec.c
@@ -1249,7 +1249,7 @@ static int cavs_decode_frame(AVCodecContext *avctx, void 
*data, int *got_frame,
 buf_end = buf + buf_size;
 for(;;) {
 buf_ptr = avpriv_find_start_code(buf_ptr, buf_end, );
-if ((stc & 0xFE00) || buf_ptr == buf_end) {
+if (!avpriv_start_code_is_valid(stc) || buf_ptr == buf_end) {
 if (!h->stc)
 av_log(h->avctx, AV_LOG_WARNING, "no frame decoded\n");
 return FFMAX(0, buf_ptr - buf);
diff --git a/libavcodec/cbs_mpeg2.c b/libavcodec/cbs_mpeg2.c
index 03ea49cbca..648b270f44 100644
--- a/libavcodec/cbs_mpeg2.c
+++ b/libavcodec/cbs_mpeg2.c
@@ -152,7 +152,7 @@ static int cbs_mpeg2_split_fragment(CodedBitstreamContext 
*ctx,
 
 start = avpriv_find_start_code(frag->data, frag->data + frag->data_size,
_code);
-if (start_code >> 8 != 0x01) {
+if (!avpriv_start_code_is_valid(start_code)) {
 // No start code found.
 return AVERROR_INVALIDDATA;
 }
@@ -167,7 +167,7 @@ static int cbs_mpeg2_split_fragment(CodedBitstreamContext 
*ctx,
 // (may be the last byte of fragment->data); end points to the byte
 // following the byte containing the start code identifier (or to
 // the end of fragment->data).
-if (start_code >> 8 == 0x01) {
+if (avpriv_start_code_is_valid(start_code)) {
 // Unit runs from start to the beginning of the start code
 // pointed to by end (including any padding zeroes).
 unit_size = (end - 4) - start;
diff --git a/libavcodec/extract_extradata_bsf.c 
b/libavcodec/extract_extradata_bsf.c
index dbcb8508b0..4df1c97139 100644
--- a/libavcodec/extract_extradata_bsf.c
+++ b/libavcodec/extract_extradata_bsf.c
@@ -240,7 +240,7 @@ static int extract_extradata_vc1(AVBSFContext *ctx, 
AVPacket *pkt,
 ptr = avpriv_find_start_code(ptr, end, );
 if (state == VC1_CODE_SEQHDR || state == VC1_CODE_ENTRYPOINT) {
 has_extradata = 1;
-} else if (has_extradata && IS_MARKER(state)) {
+} else if (has_extradata && avpriv_start_code_is_valid(state)) {
 extradata_size = ptr - 4 - pkt->data;
 break;
 }
diff --git a/libavcodec/internal.h b/libavcodec/internal.h
index 07098e1522..2f9f99482c 100644
--- a/libavcodec/internal.h
+++ b/libavcodec/internal.h
@@ -285,6 +285,20 @@ int ff_thread_can_start_frame(AVCodecContext *avctx);
 
 int avpriv_h264_has_num_reorder_frames(AVCodecContext *avctx);
 
+/**
+ * @brief Test whether a start code found by avpriv_find_start_code() is valid.
+ *
+ * Use this to test the validity of a start code or if a start code can be at 
the
+ * end of the buffer, where testing the return value of 
avpriv_find_start_code()
+ * would incorrectly imply that the start code is invalid.
+ *
+ * @param[in] start_code The start code to test.
+ * @return A boolean that is true if and only if start_code is valid
+ */
+static av_always_inline int avpriv_start_code_is_valid(uint32_t start_code) {
+return (start_code & 0xFF00) == 0x100;
+}
+
 /**
  * Find the first start code in the buffer p.  A start code is a sequence of 4
  * bytes, with memory address increasing left to right and in hexadecimal, with
diff --git a/libavcodec/mpeg12.c b/libavcodec/mpeg12.c
index 58e03c05d4..e45bc74479 100644
--- a/libavcodec/mpeg12.c
+++ b/libavcodec/mpeg12.c
@@ -217,7 +217,7 @@ int ff_mpeg1_find_frame_end(ParseContext *pc, const uint8_t 
*buf, int buf_size,
 pc->frame_start_found = 0;
 if (pc->frame_start_found  < 4 && state == EXT_START_CODE)
 pc->frame_start_found++;
-if (pc->frame_start_found == 4 && (state & 0xFF00) == 0x100) {
+if (pc->frame_start_found == 4 && 
avpriv_start_code_is_valid(state)) {
 if (state < SLICE_MIN_START_CODE || state > 
SLICE_MAX_START_CODE) {
 pc->frame_start_found = 0;
 pc->state = -1;
diff --git a/libavcodec/mpeg12dec.c 

[FFmpeg-devel] [PATCH 5/8] avpriv_find_start_code(): replace unnecessary for loop

2022-02-01 Thread Scott Theisen
start_code will still be invalid, i.e. all ones, but will no longer have
up to the first three bytes in p shifted in.
---
 libavcodec/utils.c | 13 -
 1 file changed, 4 insertions(+), 9 deletions(-)

diff --git a/libavcodec/utils.c b/libavcodec/utils.c
index 54c9dd056d..b4c5fa5009 100644
--- a/libavcodec/utils.c
+++ b/libavcodec/utils.c
@@ -944,19 +944,14 @@ const uint8_t *avpriv_find_start_code(const uint8_t 
*av_restrict p,
   const uint8_t *end,
   uint32_t *av_restrict start_code)
 {
-int i;
+*start_code = ~0;
 
 av_assert0(p <= end);
-if (p >= end)
+// minimum length for a start code
+if (p + 4 > end)
 return end;
 
-*start_code = ~0;
-for (i = 0; i < 3; i++) {
-uint32_t tmp = *start_code << 8;
-*start_code = tmp + *(p++);
-if (tmp == 0x100 || p == end)
-return p;
-}
+p += 3; // offset for negative indices in while loop
 
 /* with memory address increasing left to right, we are looking for (in 
hexadecimal):
  * 00 00 01 XX
-- 
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-requ...@ffmpeg.org with subject "unsubscribe".


[FFmpeg-devel] [PATCH 4/8] avpriv_find_start_code(): add doxygen comment and rename a parameter

2022-02-01 Thread Scott Theisen
---
 libavcodec/internal.h | 15 ++-
 libavcodec/utils.c| 10 +-
 2 files changed, 19 insertions(+), 6 deletions(-)

diff --git a/libavcodec/internal.h b/libavcodec/internal.h
index 72ca1553f6..07098e1522 100644
--- a/libavcodec/internal.h
+++ b/libavcodec/internal.h
@@ -285,9 +285,22 @@ int ff_thread_can_start_frame(AVCodecContext *avctx);
 
 int avpriv_h264_has_num_reorder_frames(AVCodecContext *avctx);
 
+/**
+ * Find the first start code in the buffer p.  A start code is a sequence of 4
+ * bytes, with memory address increasing left to right and in hexadecimal, with
+ * the value 00 00 01 XX, where XX represents any value.
+ *
+ * @param[in] p A pointer to the start of the memory buffer to scan.
+ * @param[in] end   A pointer to the past the end memory address for the buffer
+ *  given by p.  p must be <= end.
+ *
+ * @param[out] start_code The found start code if it exists, otherwise an 
invalid start code.
+ * @return A pointer to the memory address following the found start code, or 
end
+ * if no start code was found.
+ */
 const uint8_t *avpriv_find_start_code(const uint8_t *p,
   const uint8_t *end,
-  uint32_t *state);
+  uint32_t *start_code);
 
 int avpriv_codec_get_cap_skip_frame_fill_param(const AVCodec *codec);
 
diff --git a/libavcodec/utils.c b/libavcodec/utils.c
index cf88e0a759..54c9dd056d 100644
--- a/libavcodec/utils.c
+++ b/libavcodec/utils.c
@@ -942,7 +942,7 @@ void ff_thread_report_progress2(AVCodecContext *avctx, int 
field, int thread, in
 
 const uint8_t *avpriv_find_start_code(const uint8_t *av_restrict p,
   const uint8_t *end,
-  uint32_t *av_restrict state)
+  uint32_t *av_restrict start_code)
 {
 int i;
 
@@ -950,10 +950,10 @@ const uint8_t *avpriv_find_start_code(const uint8_t 
*av_restrict p,
 if (p >= end)
 return end;
 
-*state = ~0;
+*start_code = ~0;
 for (i = 0; i < 3; i++) {
-uint32_t tmp = *state << 8;
-*state = tmp + *(p++);
+uint32_t tmp = *start_code << 8;
+*start_code = tmp + *(p++);
 if (tmp == 0x100 || p == end)
 return p;
 }
@@ -987,7 +987,7 @@ const uint8_t *avpriv_find_start_code(const uint8_t 
*av_restrict p,
 // this will cause the last 4 bytes before end to be read,
 // i.e. no out of bounds memory access occurs
 
-*state = AV_RB32(p - 4);
+*start_code = AV_RB32(p - 4);
 // read the previous 4 bytes, i.e. bytes {p - 4, p - 3, p - 2, p - 1}
 
 return p;
-- 
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-requ...@ffmpeg.org with subject "unsubscribe".


[FFmpeg-devel] [PATCH 3/8] avpriv_find_start_code(): make the state parameter output only

2022-02-01 Thread Scott Theisen
---
 libavcodec/cbs_mpeg2.c| 8 
 libavcodec/mpeg12dec.c| 5 ++---
 libavcodec/mpeg4_unpack_bframes_bsf.c | 1 -
 libavcodec/mpegvideo_parser.c | 3 +--
 libavcodec/utils.c| 1 +
 libavformat/rtpenc_mpv.c  | 3 +--
 6 files changed, 5 insertions(+), 16 deletions(-)

diff --git a/libavcodec/cbs_mpeg2.c b/libavcodec/cbs_mpeg2.c
index 26400f279f..03ea49cbca 100644
--- a/libavcodec/cbs_mpeg2.c
+++ b/libavcodec/cbs_mpeg2.c
@@ -160,14 +160,6 @@ static int cbs_mpeg2_split_fragment(CodedBitstreamContext 
*ctx,
 for (i = 0;; i++) {
 unit_type = start_code & 0xff;
 
-if (start == frag->data + frag->data_size) {
-// The last four bytes form a start code which constitutes
-// a unit of its own.  In this situation avpriv_find_start_code
-// won't modify start_code at all so modify start_code so that
-// the next unit will be treated as the last unit.
-start_code = 0;
-}
-
 end = avpriv_find_start_code(start--, frag->data + frag->data_size,
  _code);
 
diff --git a/libavcodec/mpeg12dec.c b/libavcodec/mpeg12dec.c
index 4a7bd6d466..1110fcb319 100644
--- a/libavcodec/mpeg12dec.c
+++ b/libavcodec/mpeg12dec.c
@@ -1770,7 +1770,7 @@ static int mpeg_decode_slice(MpegEncContext *s, int mb_y,
 
 if (avctx->hwaccel && avctx->hwaccel->decode_slice) {
 const uint8_t *buf_end, *buf_start = *buf - 4; /* include start_code */
-int start_code = -1;
+uint32_t start_code;
 buf_end = avpriv_find_start_code(buf_start + 2, *buf + buf_size, 
_code);
 if (buf_end < *buf + buf_size)
 buf_end -= 4;
@@ -2020,7 +2020,6 @@ static int slice_decode_thread(AVCodecContext *c, void 
*arg)
 if (s->mb_y == s->end_mb_y)
 return 0;
 
-start_code = -1;
 buf= avpriv_find_start_code(buf, s->gb.buffer_end, 
_code);
 if (start_code < SLICE_MIN_START_CODE || start_code > 
SLICE_MAX_START_CODE)
 return AVERROR_INVALIDDATA;
@@ -2475,7 +2474,7 @@ static int decode_chunks(AVCodecContext *avctx, AVFrame 
*picture,
 
 for (;;) {
 /* find next start code */
-uint32_t start_code = -1;
+uint32_t start_code;
 buf_ptr = avpriv_find_start_code(buf_ptr, buf_end, _code);
 if (start_code > 0x1ff) {
 if (!skip_frame) {
diff --git a/libavcodec/mpeg4_unpack_bframes_bsf.c 
b/libavcodec/mpeg4_unpack_bframes_bsf.c
index 6f8595713d..8b3fda0b03 100644
--- a/libavcodec/mpeg4_unpack_bframes_bsf.c
+++ b/libavcodec/mpeg4_unpack_bframes_bsf.c
@@ -36,7 +36,6 @@ static void scan_buffer(const uint8_t *buf, int buf_size,
 const uint8_t *end = buf + buf_size, *pos = buf;
 
 while (pos < end) {
-startcode = -1;
 pos = avpriv_find_start_code(pos, end, );
 
 if (startcode == USER_DATA_STARTCODE && pos_p) {
diff --git a/libavcodec/mpegvideo_parser.c b/libavcodec/mpegvideo_parser.c
index c5dc867d24..c991a82405 100644
--- a/libavcodec/mpegvideo_parser.c
+++ b/libavcodec/mpegvideo_parser.c
@@ -105,7 +105,6 @@ static void mpegvideo_extract_headers(AVCodecParserContext 
*s,
 {
 struct MpvParseContext *pc = s->priv_data;
 const uint8_t *buf_end = buf + buf_size;
-uint32_t start_code;
 int frame_rate_index, ext_type, bytes_left;
 int frame_rate_ext_n, frame_rate_ext_d;
 int top_field_first, repeat_first_field, progressive_frame;
@@ -120,7 +119,7 @@ static void mpegvideo_extract_headers(AVCodecParserContext 
*s,
 s->repeat_pict = 0;
 
 while (buf < buf_end) {
-start_code= -1;
+uint32_t start_code;
 buf= avpriv_find_start_code(buf, buf_end, _code);
 bytes_left = buf_end - buf;
 switch(start_code) {
diff --git a/libavcodec/utils.c b/libavcodec/utils.c
index 882f90be79..cf88e0a759 100644
--- a/libavcodec/utils.c
+++ b/libavcodec/utils.c
@@ -950,6 +950,7 @@ const uint8_t *avpriv_find_start_code(const uint8_t 
*av_restrict p,
 if (p >= end)
 return end;
 
+*state = ~0;
 for (i = 0; i < 3; i++) {
 uint32_t tmp = *state << 8;
 *state = tmp + *(p++);
diff --git a/libavformat/rtpenc_mpv.c b/libavformat/rtpenc_mpv.c
index 4b45f51772..bb63c9bdc6 100644
--- a/libavformat/rtpenc_mpv.c
+++ b/libavformat/rtpenc_mpv.c
@@ -51,11 +51,10 @@ void ff_rtp_send_mpegvideo(AVFormatContext *s1, const 
uint8_t *buf1, int size)
 end_of_slice = 1;
 } else {
 const uint8_t *r, *r1;
-int start_code;
 
 r1 = buf1;
 while (1) {
-start_code = -1;
+uint32_t start_code;
 r = avpriv_find_start_code(r1, end, _code);
 if((start_code & 0xFF00) == 0x100) {
 /* New start code found */
-- 
2.32.0

___
ffmpeg-devel mailing list

[FFmpeg-devel] [PATCH 2/8] avpriv_find_start_code(): rewrite while loop and add comments for clarity

2022-02-01 Thread Scott Theisen
The expected number of iterations may increase by one for an input of 
alternating
0 and 1 bytes.  Instead of incrementing by 2 everytime, it now alternates 
between
incrementing by 1 and by 3.

No functional change, but now much clearer.
---
 libavcodec/utils.c | 25 -
 1 file changed, 20 insertions(+), 5 deletions(-)

diff --git a/libavcodec/utils.c b/libavcodec/utils.c
index cb4437edc2..882f90be79 100644
--- a/libavcodec/utils.c
+++ b/libavcodec/utils.c
@@ -957,12 +957,26 @@ const uint8_t *avpriv_find_start_code(const uint8_t 
*av_restrict p,
 return p;
 }
 
+/* with memory address increasing left to right, we are looking for (in 
hexadecimal):
+ * 00 00 01 XX
+ * p points at the address which should have the value of XX
+ */
 while (p < end) {
-if  (p[-1] > 1  ) p += 3;
-else if (p[-2]  ) p += 2;
-else if (p[-3]|(p[-1]-1)) p++;
+// UU UU UU
+if  (p[-1]  > 1) p += 3;// start check over with 3 new bytes
+else if (p[-1] == 0) p++;   // could be in a start code, so check 
next byte
+// this should be one comparison against 1 since p is unsigned,
+// i.e. p[-1] == 0 is equivalent to p[-1] < 1
+
+// UU UU 01
+else if (p[-2] != 0) p += 2;// we have UU YY 01, so increment by 2
+// to start check over with 3 new bytes
+// UU 00 01
+else if (p[-3] != 0) p += 3;// we have YY 00 01, so increment by 3
+// to start check over with 3 new bytes
+// 00 00 01
 else {
-p++;
+p++; // p now points at the address following the start code value 
XX
 break;
 }
 }
@@ -972,7 +986,8 @@ const uint8_t *avpriv_find_start_code(const uint8_t 
*av_restrict p,
 // this will cause the last 4 bytes before end to be read,
 // i.e. no out of bounds memory access occurs
 
-*state = AV_RB32(p - 4); // read the previous 4 bytes
+*state = AV_RB32(p - 4);
+// read the previous 4 bytes, i.e. bytes {p - 4, p - 3, p - 2, p - 1}
 
 return p;
 }
-- 
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-requ...@ffmpeg.org with subject "unsubscribe".


[FFmpeg-devel] [PATCH 1/8] avpriv_find_start_code(): readability enhancement part 1

2022-02-01 Thread Scott Theisen
No functional change.
---
 libavcodec/utils.c | 10 +++---
 1 file changed, 7 insertions(+), 3 deletions(-)

diff --git a/libavcodec/utils.c b/libavcodec/utils.c
index b19befef21..cb4437edc2 100644
--- a/libavcodec/utils.c
+++ b/libavcodec/utils.c
@@ -967,10 +967,14 @@ const uint8_t *avpriv_find_start_code(const uint8_t 
*av_restrict p,
 }
 }
 
-p = FFMIN(p, end) - 4;
-*state = AV_RB32(p);
+if (p > end)
+p = end;
+// this will cause the last 4 bytes before end to be read,
+// i.e. no out of bounds memory access occurs
 
-return p + 4;
+*state = AV_RB32(p - 4); // read the previous 4 bytes
+
+return p;
 }
 
 AVCPBProperties *av_cpb_properties_alloc(size_t *size)
-- 
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-requ...@ffmpeg.org with subject "unsubscribe".


[FFmpeg-devel] [PATCH 0/8] rewrite avpriv_find_start_code() for clarity

2022-02-01 Thread Scott Theisen
I am endeavoring to reduce MythTV’s downstream changes to FFmpeg.
avpriv_find_start_code() is one of the internal FFmpeg functions that
MythTV uses.

I was planning on copying it into MythTV to eventually eliminate all
uses of internal FFmpeg headers; however, what avpriv_find_start_code()
actually does was not very clear.  Therefore, I rewrote it and added
comments to make its purpose and effects clearer.


___
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".