diff mbox

[FFmpeg-devel,2/3] avformat/mpegts: cache PID discard values

Message ID 20190124203801.18484-2-cus@passwd.hu
State Accepted
Headers show

Commit Message

Marton Balint Jan. 24, 2019, 8:38 p.m. UTC
discard_pid can be quite expensive, so let's cache it and recalculate it on
every packet start.

ffmpeg -y -i samples/MPEG-VOB/sdtv/RAI.ts -c copy -map 0:v:0 -map 0:a:0 -f mpegts /dev/null

Before:
   1685 decicycles in handle_packet,  523483 runs,    805 skips

After:
    883 decicycles in handle_packet,  523505 runs,    783 skips

Signed-off-by: Marton Balint <cus@passwd.hu>
---
 libavformat/mpegts.c | 7 +++++--
 1 file changed, 5 insertions(+), 2 deletions(-)

Comments

Carl Eugen Hoyos Jan. 25, 2019, 12:12 a.m. UTC | #1
2019-01-24 21:38 GMT+01:00, Marton Balint <cus@passwd.hu>:
> discard_pid can be quite expensive, so let's cache it and recalculate it on
> every packet start.
>
> ffmpeg -y -i samples/MPEG-VOB/sdtv/RAI.ts -c copy -map 0:v:0 -map 0:a:0
> -f mpegts /dev/null

I can reproduce a 30% overall speedup for this command line and your patch.
(For a relatively low execution time around one second.)

Carl Eugen
Michael Niedermayer Jan. 25, 2019, 8:56 a.m. UTC | #2
On Thu, Jan 24, 2019 at 09:38:00PM +0100, Marton Balint wrote:
> discard_pid can be quite expensive, so let's cache it and recalculate it on
> every packet start.
> 
> ffmpeg -y -i samples/MPEG-VOB/sdtv/RAI.ts -c copy -map 0:v:0 -map 0:a:0 -f mpegts /dev/null
> 
> Before:
>    1685 decicycles in handle_packet,  523483 runs,    805 skips
> 
> After:
>     883 decicycles in handle_packet,  523505 runs,    783 skips
> 
> Signed-off-by: Marton Balint <cus@passwd.hu>
> ---
>  libavformat/mpegts.c | 7 +++++--
>  1 file changed, 5 insertions(+), 2 deletions(-)
> 
> diff --git a/libavformat/mpegts.c b/libavformat/mpegts.c
> index 300db110d4..b04fd7b4f4 100644
> --- a/libavformat/mpegts.c
> +++ b/libavformat/mpegts.c
> @@ -91,6 +91,7 @@ struct MpegTSFilter {
>      int es_id;
>      int last_cc; /* last cc code (-1 if first packet) */
>      int64_t last_pcr;
> +    int discard;
>      enum MpegTSFilterType type;
>      union {
>          MpegTSPESFilter pes_filter;
> @@ -2474,8 +2475,6 @@ static int handle_packet(MpegTSContext *ts, const uint8_t *packet)
>      int64_t pos;
>  
>      pid = AV_RB16(packet + 1) & 0x1fff;
> -    if (pid && discard_pid(ts, pid))
> -        return 0;
>      is_start = packet[1] & 0x40;
>      tss = ts->pids[pid];
>      if (ts->auto_guess && !tss && is_start) {
> @@ -2484,6 +2483,10 @@ static int handle_packet(MpegTSContext *ts, const uint8_t *packet)
>      }
>      if (!tss)
>          return 0;
> +    if (is_start)
> +        tss->discard = discard_pid(ts, pid);
> +    if (tss->discard)
> +        return 0;

this is moving the discard check over the auto_guess /add_pes_stream()
have you checked or know that this is ok ?
its not immedeatly obviouls (to me) why this would have no side effects

thx

[...]
Marton Balint Jan. 25, 2019, 6:51 p.m. UTC | #3
On Fri, 25 Jan 2019, Michael Niedermayer wrote:

> On Thu, Jan 24, 2019 at 09:38:00PM +0100, Marton Balint wrote:
>> discard_pid can be quite expensive, so let's cache it and recalculate it on
>> every packet start.
>>
>> ffmpeg -y -i samples/MPEG-VOB/sdtv/RAI.ts -c copy -map 0:v:0 -map 0:a:0 -f mpegts /dev/null
>>
>> Before:
>>    1685 decicycles in handle_packet,  523483 runs,    805 skips
>>
>> After:
>>     883 decicycles in handle_packet,  523505 runs,    783 skips
>>
>> Signed-off-by: Marton Balint <cus@passwd.hu>
>> ---
>>  libavformat/mpegts.c | 7 +++++--
>>  1 file changed, 5 insertions(+), 2 deletions(-)
>>
>> diff --git a/libavformat/mpegts.c b/libavformat/mpegts.c
>> index 300db110d4..b04fd7b4f4 100644
>> --- a/libavformat/mpegts.c
>> +++ b/libavformat/mpegts.c
>> @@ -91,6 +91,7 @@ struct MpegTSFilter {
>>      int es_id;
>>      int last_cc; /* last cc code (-1 if first packet) */
>>      int64_t last_pcr;
>> +    int discard;
>>      enum MpegTSFilterType type;
>>      union {
>>          MpegTSPESFilter pes_filter;
>> @@ -2474,8 +2475,6 @@ static int handle_packet(MpegTSContext *ts, const uint8_t *packet)
>>      int64_t pos;
>>
>>      pid = AV_RB16(packet + 1) & 0x1fff;
>> -    if (pid && discard_pid(ts, pid))
>> -        return 0;
>>      is_start = packet[1] & 0x40;
>>      tss = ts->pids[pid];
>>      if (ts->auto_guess && !tss && is_start) {
>> @@ -2484,6 +2483,10 @@ static int handle_packet(MpegTSContext *ts, const uint8_t *packet)
>>      }
>>      if (!tss)
>>          return 0;
>> +    if (is_start)
>> +        tss->discard = discard_pid(ts, pid);
>> +    if (tss->discard)
>> +        return 0;
>
> this is moving the discard check over the auto_guess /add_pes_stream()
> have you checked or know that this is ok ?
> its not immedeatly obviouls (to me) why this would have no side effects

As far as I see that code is used to add streams which are not part of 
the detected programs. Therefore program discards should not concern them.

Regards,
Marton
Marton Balint Feb. 1, 2019, 9:29 p.m. UTC | #4
On Fri, 25 Jan 2019, Marton Balint wrote:

>
>
> On Fri, 25 Jan 2019, Michael Niedermayer wrote:
>
>> On Thu, Jan 24, 2019 at 09:38:00PM +0100, Marton Balint wrote:
>>> discard_pid can be quite expensive, so let's cache it and recalculate it 
> on
>>> every packet start.
>>>
>>> ffmpeg -y -i samples/MPEG-VOB/sdtv/RAI.ts -c copy -map 0:v:0 -map 0:a:0 -f 
> mpegts /dev/null
>>>
>>> Before:
>>>    1685 decicycles in handle_packet,  523483 runs,    805 skips
>>>
>>> After:
>>>     883 decicycles in handle_packet,  523505 runs,    783 skips
>>>
>>> Signed-off-by: Marton Balint <cus@passwd.hu>
>>> ---
>>>  libavformat/mpegts.c | 7 +++++--
>>>  1 file changed, 5 insertions(+), 2 deletions(-)
>>>
>>> diff --git a/libavformat/mpegts.c b/libavformat/mpegts.c
>>> index 300db110d4..b04fd7b4f4 100644
>>> --- a/libavformat/mpegts.c
>>> +++ b/libavformat/mpegts.c
>>> @@ -91,6 +91,7 @@ struct MpegTSFilter {
>>>      int es_id;
>>>      int last_cc; /* last cc code (-1 if first packet) */
>>>      int64_t last_pcr;
>>> +    int discard;
>>>      enum MpegTSFilterType type;
>>>      union {
>>>          MpegTSPESFilter pes_filter;
>>> @@ -2474,8 +2475,6 @@ static int handle_packet(MpegTSContext *ts, const 
> uint8_t *packet)
>>>      int64_t pos;
>>>
>>>      pid = AV_RB16(packet + 1) & 0x1fff;
>>> -    if (pid && discard_pid(ts, pid))
>>> -        return 0;
>>>      is_start = packet[1] & 0x40;
>>>      tss = ts->pids[pid];
>>>      if (ts->auto_guess && !tss && is_start) {
>>> @@ -2484,6 +2483,10 @@ static int handle_packet(MpegTSContext *ts, const 
> uint8_t *packet)
>>>      }
>>>      if (!tss)
>>>          return 0;
>>> +    if (is_start)
>>> +        tss->discard = discard_pid(ts, pid);
>>> +    if (tss->discard)
>>> +        return 0;
>>
>> this is moving the discard check over the auto_guess /add_pes_stream()
>> have you checked or know that this is ok ?
>> its not immedeatly obviouls (to me) why this would have no side effects
>
> As far as I see that code is used to add streams which are not part of 
> the detected programs. Therefore program discards should not concern them.

Will apply soon.

Regards,
Marton
Michael Niedermayer Feb. 2, 2019, 12:49 a.m. UTC | #5
On Fri, Feb 01, 2019 at 10:29:13PM +0100, Marton Balint wrote:
> 
> 
> On Fri, 25 Jan 2019, Marton Balint wrote:
> 
> >
> >
> >On Fri, 25 Jan 2019, Michael Niedermayer wrote:
> >
> >>On Thu, Jan 24, 2019 at 09:38:00PM +0100, Marton Balint wrote:
> >>>discard_pid can be quite expensive, so let's cache it and recalculate
> >>>it
> >on
> >>>every packet start.
> >>>
> >>>ffmpeg -y -i samples/MPEG-VOB/sdtv/RAI.ts -c copy -map 0:v:0 -map
> >>>0:a:0 -f
> >mpegts /dev/null
> >>>
> >>>Before:
> >>>   1685 decicycles in handle_packet,  523483 runs,    805 skips
> >>>
> >>>After:
> >>>    883 decicycles in handle_packet,  523505 runs,    783 skips
> >>>
> >>>Signed-off-by: Marton Balint <cus@passwd.hu>
> >>>---
> >>> libavformat/mpegts.c | 7 +++++--
> >>> 1 file changed, 5 insertions(+), 2 deletions(-)
> >>>
> >>>diff --git a/libavformat/mpegts.c b/libavformat/mpegts.c
> >>>index 300db110d4..b04fd7b4f4 100644
> >>>--- a/libavformat/mpegts.c
> >>>+++ b/libavformat/mpegts.c
> >>>@@ -91,6 +91,7 @@ struct MpegTSFilter {
> >>>     int es_id;
> >>>     int last_cc; /* last cc code (-1 if first packet) */
> >>>     int64_t last_pcr;
> >>>+    int discard;
> >>>     enum MpegTSFilterType type;
> >>>     union {
> >>>         MpegTSPESFilter pes_filter;
> >>>@@ -2474,8 +2475,6 @@ static int handle_packet(MpegTSContext *ts,
> >>>const
> >uint8_t *packet)
> >>>     int64_t pos;
> >>>
> >>>     pid = AV_RB16(packet + 1) & 0x1fff;
> >>>-    if (pid && discard_pid(ts, pid))
> >>>-        return 0;
> >>>     is_start = packet[1] & 0x40;
> >>>     tss = ts->pids[pid];
> >>>     if (ts->auto_guess && !tss && is_start) {
> >>>@@ -2484,6 +2483,10 @@ static int handle_packet(MpegTSContext *ts,
> >>>const
> >uint8_t *packet)
> >>>     }
> >>>     if (!tss)
> >>>         return 0;
> >>>+    if (is_start)
> >>>+        tss->discard = discard_pid(ts, pid);
> >>>+    if (tss->discard)
> >>>+        return 0;
> >>
> >>this is moving the discard check over the auto_guess /add_pes_stream()
> >>have you checked or know that this is ok ?
> >>its not immedeatly obviouls (to me) why this would have no side effects
> >
> >As far as I see that code is used to add streams which are not part of the
> >detected programs. Therefore program discards should not concern them.
> 
> Will apply soon.

sure ok

thanks

[...]
Marton Balint Feb. 4, 2019, 8:49 p.m. UTC | #6
On Sat, 2 Feb 2019, Michael Niedermayer wrote:

> On Fri, Feb 01, 2019 at 10:29:13PM +0100, Marton Balint wrote:
>>
>>
>> On Fri, 25 Jan 2019, Marton Balint wrote:
>>
>>>
>>>
>>> On Fri, 25 Jan 2019, Michael Niedermayer wrote:
>>>
>>>> On Thu, Jan 24, 2019 at 09:38:00PM +0100, Marton Balint wrote:
>>>>> discard_pid can be quite expensive, so let's cache it and recalculate
>>>>> it
>>> on
>>>>> every packet start.
>>>>>
>>>>> ffmpeg -y -i samples/MPEG-VOB/sdtv/RAI.ts -c copy -map 0:v:0 -map
>>>>> 0:a:0 -f
>>> mpegts /dev/null
>>>>>
>>>>> Before:
>>>>>   1685 decicycles in handle_packet,  523483 runs,    805 skips
>>>>>
>>>>> After:
>>>>>    883 decicycles in handle_packet,  523505 runs,    783 skips
>>>>>
>>>>> Signed-off-by: Marton Balint <cus@passwd.hu>
>>>>> ---
>>>>> libavformat/mpegts.c | 7 +++++--
>>>>> 1 file changed, 5 insertions(+), 2 deletions(-)
>>>>>
>>>>> diff --git a/libavformat/mpegts.c b/libavformat/mpegts.c
>>>>> index 300db110d4..b04fd7b4f4 100644
>>>>> --- a/libavformat/mpegts.c
>>>>> +++ b/libavformat/mpegts.c
>>>>> @@ -91,6 +91,7 @@ struct MpegTSFilter {
>>>>>     int es_id;
>>>>>     int last_cc; /* last cc code (-1 if first packet) */
>>>>>     int64_t last_pcr;
>>>>> +    int discard;
>>>>>     enum MpegTSFilterType type;
>>>>>     union {
>>>>>         MpegTSPESFilter pes_filter;
>>>>> @@ -2474,8 +2475,6 @@ static int handle_packet(MpegTSContext *ts,
>>>>> const
>>> uint8_t *packet)
>>>>>     int64_t pos;
>>>>>
>>>>>     pid = AV_RB16(packet + 1) & 0x1fff;
>>>>> -    if (pid && discard_pid(ts, pid))
>>>>> -        return 0;
>>>>>     is_start = packet[1] & 0x40;
>>>>>     tss = ts->pids[pid];
>>>>>     if (ts->auto_guess && !tss && is_start) {
>>>>> @@ -2484,6 +2483,10 @@ static int handle_packet(MpegTSContext *ts,
>>>>> const
>>> uint8_t *packet)
>>>>>     }
>>>>>     if (!tss)
>>>>>         return 0;
>>>>> +    if (is_start)
>>>>> +        tss->discard = discard_pid(ts, pid);
>>>>> +    if (tss->discard)
>>>>> +        return 0;
>>>>
>>>> this is moving the discard check over the auto_guess /add_pes_stream()
>>>> have you checked or know that this is ok ?
>>>> its not immedeatly obviouls (to me) why this would have no side effects
>>>
>>> As far as I see that code is used to add streams which are not part of the
>>> detected programs. Therefore program discards should not concern them.
>>
>> Will apply soon.
>
> sure ok

Thanks, applied.

Regards,
Marton
diff mbox

Patch

diff --git a/libavformat/mpegts.c b/libavformat/mpegts.c
index 300db110d4..b04fd7b4f4 100644
--- a/libavformat/mpegts.c
+++ b/libavformat/mpegts.c
@@ -91,6 +91,7 @@  struct MpegTSFilter {
     int es_id;
     int last_cc; /* last cc code (-1 if first packet) */
     int64_t last_pcr;
+    int discard;
     enum MpegTSFilterType type;
     union {
         MpegTSPESFilter pes_filter;
@@ -2474,8 +2475,6 @@  static int handle_packet(MpegTSContext *ts, const uint8_t *packet)
     int64_t pos;
 
     pid = AV_RB16(packet + 1) & 0x1fff;
-    if (pid && discard_pid(ts, pid))
-        return 0;
     is_start = packet[1] & 0x40;
     tss = ts->pids[pid];
     if (ts->auto_guess && !tss && is_start) {
@@ -2484,6 +2483,10 @@  static int handle_packet(MpegTSContext *ts, const uint8_t *packet)
     }
     if (!tss)
         return 0;
+    if (is_start)
+        tss->discard = discard_pid(ts, pid);
+    if (tss->discard)
+        return 0;
     ts->current_pid = pid;
 
     afc = (packet[3] >> 4) & 3;