[FFmpeg-devel] hlsenc: intialize only on ref_pkt (v2)

Submitted by Miroslav Slugeň on Feb. 13, 2017, 1:15 p.m.

Details

Message ID 58A1B17C.1090607@email.cz
State New
Headers show

Commit Message

Miroslav Slugeň Feb. 13, 2017, 1:15 p.m.
This patch will fix cutting hls segments into exactly same length. 
Because it will intialize only on first ref_packet, which is video 
frame, not audio frame (old behavior)

Now it should be possible to create segments at exactly same length if 
we use new -force_key_frames hls:time_in_seconds parameter.

This is required to support adaptive HLS.

This patch was splitted to two parts, this is first independent part

Comments

Steven Liu Feb. 14, 2017, 3:59 a.m.
2017-02-13 21:15 GMT+08:00 Miroslav Slugeň <thunder.m@email.cz>:

> This patch will fix cutting hls segments into exactly same length. Because
> it will intialize only on first ref_packet, which is video frame, not audio
> frame (old behavior)
>
> Now it should be possible to create segments at exactly same length if we
> use new -force_key_frames hls:time_in_seconds parameter.
>
> This is required to support adaptive HLS.
>
> This patch was splitted to two parts, this is first independent part
>
> --
> Miroslav Slugeň
>
>
>
>
>
>
>
>
>
>
>
>
> _______________________________________________
> ffmpeg-devel mailing list
> ffmpeg-devel@ffmpeg.org
> http://ffmpeg.org/mailman/listinfo/ffmpeg-devel
>
>
Patch can compile passed, but i cannot reproduce the problem before your
patch, can you send a reproduce step and test file?
Miroslav Slugeň Feb. 14, 2017, 8:30 a.m.
Dne 14.2.2017 v 04:59 Steven Liu napsal(a):
> 2017-02-13 21:15 GMT+08:00 Miroslav Slugeň <thunder.m@email.cz>:
>
>> This patch will fix cutting hls segments into exactly same length. Because
>> it will intialize only on first ref_packet, which is video frame, not audio
>> frame (old behavior)
>>
>> Now it should be possible to create segments at exactly same length if we
>> use new -force_key_frames hls:time_in_seconds parameter.
>>
>> This is required to support adaptive HLS.
>>
>> This patch was splitted to two parts, this is first independent part
>>
>> --
>> Miroslav Slugeň
>>
>>
>>
>>
>>
>>
>>
>>
>>
>>
>>
>>
>> _______________________________________________
>> ffmpeg-devel mailing list
>> ffmpeg-devel@ffmpeg.org
>> http://ffmpeg.org/mailman/listinfo/ffmpeg-devel
>>
>>
> Patch can compile passed, but i cannot reproduce the problem before your
> patch, can you send a reproduce step and test file?
> _______________________________________________
> ffmpeg-devel mailing list
> ffmpeg-devel@ffmpeg.org
> http://ffmpeg.org/mailman/listinfo/ffmpeg-devel
I can provide you with sample. But this explanation should be enough:

When you use -copyts for long time audio/video sync when transcoding 
from MPEGTS stream, audio comes always sooner than video (it is around 
1-2 s), so hlsenc is initialized with audio packet PTS instead of video 
packet PTS. That is not correct if you wan't to have all segments with 
same length -forced_keyframes to force keyframe at video position. Every 
other calculations of hls_time is done only on ref_packets which is 
video. This scenario is problematic when encoder is also inserting 
keyframes by its own decision (libx264 default behavior).

M.
Steven Liu Feb. 14, 2017, 8:33 a.m.
2017-02-14 16:30 GMT+08:00 Miroslav Slugeň <thunder.m@email.cz>:

> Dne 14.2.2017 v 04:59 Steven Liu napsal(a):
>
> 2017-02-13 21:15 GMT+08:00 Miroslav Slugeň <thunder.m@email.cz>:
>>
>> This patch will fix cutting hls segments into exactly same length. Because
>>> it will intialize only on first ref_packet, which is video frame, not
>>> audio
>>> frame (old behavior)
>>>
>>> Now it should be possible to create segments at exactly same length if we
>>> use new -force_key_frames hls:time_in_seconds parameter.
>>>
>>> This is required to support adaptive HLS.
>>>
>>> This patch was splitted to two parts, this is first independent part
>>>
>>> --
>>> Miroslav Slugeň
>>>
>>>
>>>
>>>
>>>
>>>
>>>
>>>
>>>
>>>
>>>
>>>
>>> _______________________________________________
>>> ffmpeg-devel mailing list
>>> ffmpeg-devel@ffmpeg.org
>>> http://ffmpeg.org/mailman/listinfo/ffmpeg-devel
>>>
>>>
>>> Patch can compile passed, but i cannot reproduce the problem before your
>> patch, can you send a reproduce step and test file?
>> _______________________________________________
>> ffmpeg-devel mailing list
>> ffmpeg-devel@ffmpeg.org
>> http://ffmpeg.org/mailman/listinfo/ffmpeg-devel
>>
> I can provide you with sample. But this explanation should be enough:
>
> When you use -copyts for long time audio/video sync when transcoding from
> MPEGTS stream, audio comes always sooner than video (it is around 1-2 s),
> so hlsenc is initialized with audio packet PTS instead of video packet PTS.
> That is not correct if you wan't to have all segments with same length
> -forced_keyframes to force keyframe at video position. Every other
> calculations of hls_time is done only on ref_packets which is video. This
> scenario is problematic when encoder is also inserting keyframes by its own
> decision (libx264 default behavior).

command line please!

>
>
> M.
>
> _______________________________________________
> ffmpeg-devel mailing list
> ffmpeg-devel@ffmpeg.org
> http://ffmpeg.org/mailman/listinfo/ffmpeg-devel
>
Miroslav Slugeň Feb. 14, 2017, 9:45 a.m.
Dne 14.2.2017 v 09:33 Steven Liu napsal(a):
> 2017-02-14 16:30 GMT+08:00 Miroslav Slugeň <thunder.m@email.cz>:
>
>> Dne 14.2.2017 v 04:59 Steven Liu napsal(a):
>>
>> 2017-02-13 21:15 GMT+08:00 Miroslav Slugeň <thunder.m@email.cz>:
>>> This patch will fix cutting hls segments into exactly same length. Because
>>>> it will intialize only on first ref_packet, which is video frame, not
>>>> audio
>>>> frame (old behavior)
>>>>
>>>> Now it should be possible to create segments at exactly same length if we
>>>> use new -force_key_frames hls:time_in_seconds parameter.
>>>>
>>>> This is required to support adaptive HLS.
>>>>
>>>> This patch was splitted to two parts, this is first independent part
>>>>
>>>> --
>>>> Miroslav Slugeň
>>>>
>>>>
>>>>
>>>>
>>>>
>>>>
>>>>
>>>>
>>>>
>>>>
>>>>
>>>>
>>>> _______________________________________________
>>>> ffmpeg-devel mailing list
>>>> ffmpeg-devel@ffmpeg.org
>>>> http://ffmpeg.org/mailman/listinfo/ffmpeg-devel
>>>>
>>>>
>>>> Patch can compile passed, but i cannot reproduce the problem before your
>>> patch, can you send a reproduce step and test file?
>>> _______________________________________________
>>> ffmpeg-devel mailing list
>>> ffmpeg-devel@ffmpeg.org
>>> http://ffmpeg.org/mailman/listinfo/ffmpeg-devel
>>>
>> I can provide you with sample. But this explanation should be enough:
>>
>> When you use -copyts for long time audio/video sync when transcoding from
>> MPEGTS stream, audio comes always sooner than video (it is around 1-2 s),
>> so hlsenc is initialized with audio packet PTS instead of video packet PTS.
>> That is not correct if you wan't to have all segments with same length
>> -forced_keyframes to force keyframe at video position. Every other
>> calculations of hls_time is done only on ref_packets which is video. This
>> scenario is problematic when encoder is also inserting keyframes by its own
>> decision (libx264 default behavior).
> command line please!
>
>>
>> M.
>>
>> _______________________________________________
>> ffmpeg-devel mailing list
>> ffmpeg-devel@ffmpeg.org
>> http://ffmpeg.org/mailman/listinfo/ffmpeg-devel
>>
> _______________________________________________
> ffmpeg-devel mailing list
> ffmpeg-devel@ffmpeg.org
> http://ffmpeg.org/mailman/listinfo/ffmpeg-devel
ffmpeg -i dvb_record.ts \
-map 0:v:0 -filter:v yadif -force_key_frames 'expr:gte(t,n_forced*10)' 
-c:v libx264 -g 250 -b:v 500k \
-map 0:a:0 -ac 2 -ar 48000 -c:a libfdk_aac -b:a  128k \
-sn \
-copyts \
-r 25 \
-hls_time 10 -hls_list_size 10 -hls_allow_cache 1 -use_localtime 1 
-hls_flags omit_endlist+discont_start \
-hls_segment_filename "segment-%Y%m%d-%s.ts" -f hls -y "playlist.m3u8"

But this will not work correctly until -force_key_frames is fixed for 
copyts, or until you use -force_key_frames hls:10 from my patch "[PATCH] 
ffmpeg: add new forced_keyframes option hls:time"

M.

Patch hide | download patch | download mbox

From 2d0bbf0a68bc5f85f24c65a65e3ae70c362e82dc Mon Sep 17 00:00:00 2001
From: Miroslav Slugen <thunder.m@email.cz>
Date: Mon, 13 Feb 2017 14:13:18 +0100
Subject: [PATCH 1/1] hlsenc: intialize only on ref_pkt

---
 libavformat/hlsenc.c | 9 +++++----
 1 file changed, 5 insertions(+), 4 deletions(-)

diff --git a/libavformat/hlsenc.c b/libavformat/hlsenc.c
index 930e94b..e107fff 100644
--- a/libavformat/hlsenc.c
+++ b/libavformat/hlsenc.c
@@ -1279,10 +1279,6 @@  static int hls_write_packet(AVFormatContext *s, AVPacket *pkt)
         oc = hls->avf;
         stream_index = pkt->stream_index;
     }
-    if (hls->start_pts == AV_NOPTS_VALUE) {
-        hls->start_pts = pkt->pts;
-        hls->end_pts   = pkt->pts;
-    }
 
     if (hls->has_video) {
         can_split = st->codecpar->codec_type == AVMEDIA_TYPE_VIDEO &&
@@ -1293,6 +1289,11 @@  static int hls_write_packet(AVFormatContext *s, AVPacket *pkt)
         is_ref_pkt = can_split = 0;
 
     if (is_ref_pkt) {
+        if (hls->start_pts == AV_NOPTS_VALUE) {
+            hls->start_pts = pkt->pts;
+            hls->end_pts   = pkt->pts;
+        }
+
         if (hls->new_start) {
             hls->new_start = 0;
             hls->duration = (double)(pkt->pts - hls->end_pts)
-- 
2.1.4