summaryrefslogtreecommitdiff
path: root/libavformat/hlsenc.c
Commit message (Collapse)AuthorAge
* hlsenc: expand hls_fmp4_init_filename with strftime()Nikola Pajkovsky2020-12-08
| | | | | | | | the init.mp4 can be expanded with strftime the same way as hls_segment_filename. Signed-off-by: Nikola Pajkovsky <nikola@pajkovsky.cz> Signed-off-by: liuqi05 <liuqi05@kuaishou.com>
* avformat/hls: Fixes overwriting existing #EXT-X-PROGRAM-DATE-TIME value in ↵Vignesh Ravichandran2020-12-08
| | | | | | | | | | | | | | | | | | | | | | | | | | | | HLS playlist fix ticket: 8989 This is is due to the following behavior in the current code: 1. The initial_prog_date_time gets set to the current local time 2. The existing playlist (.m3u8) file gets parsed and the segments present are added to the variant stream 3. The new segment is created and added 4. The existing segments and the new segment are written to the playlist file. The initial_prog_date_time from point 1 is used for calculating "#EXT-X-PROGRAM-DATE-TIME" for the segments, which results in incorrect "#EXT-X-PROGRAM-DATE-TIME" values for existing segments The following approach fixes this bug: 1. Add a new variable "discont_program_date_time" of type double to HLSSegment struct 2. Store the "EXT-X-PROGRAM-DATE-TIME" value from the existing segments in this variable 3. When writing to playlist file if "discont_program_date_time" is set, then use that value for "EXT-X-PROGRAM-DATE-TIME" else use the value present in vs->initial_prog_date_time Signed-off-by: Vignesh Ravichandran <vignesh.ravichandran02@gmail.com> Signed-off-by: liuqi05 <liuqi05@kuaishou.com>
* avformat/hlsenc: Fix check for SPSAndreas Rheinhardt2020-10-20
| | | | | | Check all the six bits, not only the two bits that are set for SPS. Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt@gmail.com>
* avformat/hlsenc: Fix mixed declaration and codeAndreas Rheinhardt2020-10-20
| | | | Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt@gmail.com>
* avformat/hlsenc: Don't use uninitialized buffer in av_stristr()Andreas Rheinhardt2020-10-20
| | | | | | Introduced in a2b1dd0ce301450a47c972745a6b33c4c273aa5d. Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt@gmail.com>
* avformat/hlsenc: Fix extradata length checkAndreas Rheinhardt2020-10-20
| | | | | | | | | | | | | | | | | | | Commit a2b1dd0ce301450a47c972745a6b33c4c273aa5d added support for parsing annex B HEVC extradata to extract profile and level information. Yet it only checks for there to be enough data left for the startcode and the first byte of the NAL unit header and not for the full NAL unit header; it simply presumes the second byte of the NAL unit header to be present and skips it. Then the remaining size of the extradata is calculated which ends up negative if the second byte of the NAL unit header is not present. Yet when calling ff_nal_unit_extract_rbsp() it will be converted to an uint32_t and end up as UINT32_MAX which will cause mayhem. This is solved by making sure that there is always enough remaining extradata that could (pending 0x03 escapes) contain the data that we are interested in. Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt@gmail.com>
* avformat/hlsenc: support CODECS Attribute in hevc EXT-X-STREAM-INFSteven Liu2020-10-20
| | | | | | | | | | | fix ticket: 8904 parse the SPS from extradata and get profile_tier_level write the profile_tier_level info into CODECS Attribute HLS CODECS Attribute reference to :https://developer.apple.com/documentation/http_live_streaming/hls_authoring_specification_for_apple_devices/hls_authoring_specification_for_apple_devices_appendixes Signed-off-by: Steven Liu <lq@chinaffmpeg.org> Signed-off-by: liuqi05 <liuqi05@kuaishou.com>
* avformat/hlsenc: compute video_keyframe_size after write keyframeSteven Liu2020-10-20
| | | | | | | | | | | | | | fix ticket: 8636 When write keyframe and the keyframe is the frist packet of the segment, then compute the size of the keyframe which have been write into segment first packet. and set the start position of the segment, should not use avio_tell(vs->out) to get the keyframe position, because it can be set to 0 if close at above of the workflow, that maybe inaccurate, but the start_pos can be used here, because start_pos is set after write the previous packet. Signed-off-by: Steven Liu <lq@chinaffmpeg.org> Signed-off-by: liuqi05 <liuqi05@kuaishou.com>
* avformat/hlsenc: process hls_time value too small senceSteven Liu2020-10-20
| | | | | | | | | | | | | | | | | The target duration will be a negative value when there are some b frames after prevous frame, the pts after current packet is large than the pts of current packet, so the target duration will compute as 0.040000 - 0.080000, then the value of the target duration will be -0.040000. so hls muxer should check the pts after current packet minus the pts of current packet, hls muxer can split the stream as a segment if the target duration is neither negative nor zero, hls muxer cannot split the stream as a segment if the target duration is either negative or zero then get the next packet until the target duration is not negative or zero. Signed-off-by: Steven Liu <lq@chinaffmpeg.org> Suggested-by: Zhili Zhao <quinkblack@foxmail.com> Signed-off-by: liuqi05 <liuqi05@kuaishou.com>
* avformat/hlsenc: write temp file for append single file by encryption modeSteven Liu2020-08-10
| | | | | | | | | | | | fix ticket: 8783 Because in single file by encryption mode, it cannot get the last one block of the file, it need ff_format_io_close for get full file size, then hlsenc can get the total size of the encryption content, so write the content into temp file first, and get the temp file content append the temp file content into append to single file, then hlsenc can get the correct file/content size and offset. Signed-off-by: Steven Liu <liuqi05@kuaishou.com>
* avformat/hlsenc: increase initial program date time precisionMarton Balint2020-07-29
| | | | | | | | | Also query time only once, not for every variant stream, otherwise variant streams might get a slightly different initial program date time. And we can set this unconditionally because HLS_PROGRAM_DATE_TIME flag is checked elsewhere. Signed-off-by: Marton Balint <cus@passwd.hu>
* avformat/hlsenc: check fragment size plus start_pos large than hls_segment_sizeSteven Liu2020-07-07
| | | | | | if vs->size + vs->start_pos > hls->max_seg_size, should split segment. Signed-off-by: Steven Liu <lq@chinaffmpeg.org>
* libavformat/hlsenc: Remove duplicate close of the output stream.Andrey Semashev2020-07-06
| | | | | The result of the first close attempt is ignored and may be lost. By removing it we ensure the close result code is properly analyzed.
* avformat/hlsenc: simplify code to pass s directlyLimin Wang2020-07-02
| | | | | Suggested-by: Nicolas George <george@nsup.org> Signed-off-by: Limin Wang <lance.lmwang@gmail.com>
* avformat/hlsenc: fix av_bprint_finalize() usageLimin Wang2020-07-02
| | | | | | | Don't need to do double check by the description of the API. Reviewed-by: Nicolas George <george@nsup.org> Signed-off-by: Limin Wang <lance.lmwang@gmail.com>
* avformat/hlsenc: use proper error codesLimin Wang2020-07-02
| | | | Signed-off-by: Limin Wang <lance.lmwang@gmail.com>
* avformat/hlsenc: Always treat numbers as decimalAndreas Rheinhardt2020-06-15
| | | | | | | | | | | c801ab43c36e8c4f88121aa09af26c77bcbd671b caused a regression: The stream number is now parsed with strtoll without a fixed basis; as a consequence, the "010" in a variant stream mapping like "a:010" is now treated as an octal number (i.e. as eight, not ten). This was not intended and may break some scripts, so this commit restores the old behaviour. Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt@gmail.com>
* avformat/hlsenc, hlsplaylist: CosmeticsAndreas Rheinhardt2020-05-28
| | | | Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt@gmail.com>
* avformat/hlsenc: Avoid duplicating strings when parsingAndreas Rheinhardt2020-05-28
| | | | | | | | | | | | | | | | | | | Up until now, the HLS muxer uses av_strtok() to split an input string controlling parameters of the VariantStreams and then duplicates parts of this string containing parameters such as the language or the name of the VariantStream. But these parts are proper zero-terminated strings of their own that are never modified lateron, so one can simply use the substring as-is without creating a copy. This commit implements this. The same also happened for the string controlling the closed caption groups. Furthermore, add const to indicate that the pointers to these substrings are not used to modify them and also to indicate that these strings are not allocated on their own. Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt@gmail.com>
* avformat/hlsenc: Don't unnecessarily duplicate baseurl stringAndreas Rheinhardt2020-05-28
| | | | | | | | Up until now, the HLS muxer duplicated a string for every VariantStream, although neither the original nor the copies are ever modified. So use the original directly and stop copying. Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt@gmail.com>
* avformat/hlsenc: Don't segfault on uncommon namesAndreas Rheinhardt2020-05-17
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | The parsing process of the AVOpt-enabled string controlling the mapping of input streams to variant streams is roughly as follows: Space and tab separate variant stream group maps while the entries in each variant stream group map are separated by ','. The parsing process of each variant stream group proceeded as follows: At first the number of occurences of "a:", "v:" and "s:" in each variant stream group is calculated so that one can can allocate an array of streams with this number of entries. Then the string is split along ',' and each substring is parsed. If such a substring starts with "a:", "s:" or "v:" it is treated as stream specifier and (if there is a correct number after ':') a stream of the variant stream is mapped to one of the actual input streams. Nothing actually guarantees that the number of streams allocated initially equals the number of streams that are mapped to an actual input stream. These numbers can differ if e.g. the name, the sgroup, agroup or ccgroup of the variant stream contain "a:", "s:" or "v:". The problem hereby is that the rest of the code presumes these numbers to be equal and segfaults if it isn't (because the corresponding input stream is NULL). This commit fixes this by modifying the initial counting process to only count occurences of "a:", "s:" or "v:" that are at the beginning or that immediately follow a ','. Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt@gmail.com>
* avformat/hlsenc: free options fix memleak in hls_write_trailerSteven Liu2020-05-12
| | | | Signed-off-by: Steven Liu <liuqi05@kuaishou.com>
* avformat/hlsenc: fix filename memleak in hls_write_packetSteven Liu2020-05-12
| | | | Signed-off-by: Steven Liu <liuqi05@kuaishou.com>
* avformat/hlsenc: move number out of hls_startSteven Liu2020-05-12
| | | | Signed-off-by: Steven Liu <liuqi05@kuaishou.com>
* avformat/hlsenc: Simplify setting base_output_dirnameAndreas Rheinhardt2020-05-10
| | | | Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt@gmail.com>
* avformat/hlsenc: Simplify setting basename with av_asprintf()Andreas Rheinhardt2020-05-10
| | | | Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt@gmail.com>
* avformat/hlsenc: Simplify setting subtitle basename with av_asprintfAndreas Rheinhardt2020-05-10
| | | | Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt@gmail.com>
* avformat/hlsenc: Don't cast const awayAndreas Rheinhardt2020-05-10
| | | | Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt@gmail.com>
* avformat/hlsenc: Remove redundant initializationsAndreas Rheinhardt2020-05-10
| | | | | | | | | | | | | For every variantstream vs, vs->packets_written is set to one, only to be set to zero a few lines below. Given that the relevant structure has been zeroed during the allocation, this commit removes both assignments. A redundant initialization for vs->init_range_length has been removed as well a few lines below. Given that the relevant structure has been zeroed during the allocation, this commit removes both assignments. A redundant initialization for vs->init_range_length has been removed as well. Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt@gmail.com>
* avformat/hlsenc: Don't reset AVIOContext pointer manually a second timeAndreas Rheinhardt2020-05-10
| | | | | | ff_format_io_close() already does it for us. Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt@gmail.com>
* avformat/hlsenc: CosmeticsAndreas Rheinhardt2020-05-08
| | | | | | | Mainly includes reindentation and returning directly (i.e. without a goto fail when possible). Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt@gmail.com>
* avformat/hlsenc: Factor check out of loopAndreas Rheinhardt2020-05-08
| | | | | | The check will be true at most once anyway. Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt@gmail.com>
* avformat/hlsenc: Localize initialization of subtitle streamsAndreas Rheinhardt2020-05-08
| | | | | | Before this commit, the checks were unnecessarily scattered. Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt@gmail.com>
* avformat/hlsenc: Unconditionally free some stringsAndreas Rheinhardt2020-05-08
| | | | | | | | | | | | hls_init() would at first allocate the vtt_basename string, then allocate the vtt_m3u8_name string followed by several operations that may fail and then open the subtitles' output context. Yet upon freeing, these strings were only freed when the subtitles' output context existed, ensuring that they leak if something goes wrong between their allocation and the opening of the subtitles' output context. So drop the check for whether this output context exists. Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt@gmail.com>
* avformat/hlsenc: Check some unchecked allocationsAndreas Rheinhardt2020-05-08
| | | | Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt@gmail.com>
* avformat/hlsenc: Add deinit functionAndreas Rheinhardt2020-05-08
| | | | | | | | | | | | | | | | This fixes memleaks in instances such as: a) When an allocation fails at one of the two places in hls_init() where the error is returned immediately without goto fail first. b) When an error happens when writing the header. c) When an allocation fails at one of the three places in hls_write_trailer() where the error is returned immediately without goto fail first. d) When one decides not to write the trailer at all (e.g. because of errors when writing packets). Furthermore, it removes code duplication and allows to return immediately, without goto fail first. Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt@gmail.com>
* avformat/hlsenc: Avoid setting unused variablesAndreas Rheinhardt2020-05-08
| | | | | | | Several variables which are only used when the HLS_SINGLE_FILE flag is unset have been set even when this flag is set. This has been changed. Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt@gmail.com>
* avformat/hlsenc: Remove redundant setting of output formatAndreas Rheinhardt2020-05-07
| | | | | | | | avformat_alloc_output_context2() already sets the oformat member, so that there is no reason to overwrite it again with the value it already has. Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt@gmail.com>
* avformat/hlsenc: Improve checks for invalid stream mappingsAndreas Rheinhardt2020-05-06
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | The mapping of streams to the various variant streams to be created by the HLS muxer is roughly as follows: Space and tab separate variant stream group maps while the entries in each variant stream group map are separated by ','. The parsing process of each variant stream group proceeded as follows: At first the number of occurences of "a:", "v:" and "s:" in each variant stream group is calculated so that one can can allocate an array of streams with this number of entries. Then each entry is checked and the check for stream numbers was deficient: It did check that there is a number beginning after the ":", but it did not check that the number extends until the next "," (or until the end). This means that an invalid variant stream group like v:0_v:1 will not be rejected; the problem is that the variant stream in this example is supposed to have two streams associated with it (because it contains two "v:"), yet only one stream is actually associated with it (because there is no ',' to start a second stream specifier). This discrepancy led to segfaults (null pointer dereferencing) in the rest of the code (when the nonexistent second stream associated to the variant stream was inspected). Furthermore, this commit also removes an instance of using atoi() whose behaviour on a range error is undefined. Fixes ticket #8652. Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt@gmail.com>
* avformat/hlsenc: resend full url of the init fragment mp4Steven Liu2020-05-06
| | | | | | | | | fix ticket: 8651 because the init fragment mp4 file name is without base url name, so just modify it use the full url which splice after init function. Tested-by: matclayton Signed-off-by: Steven Liu <liuqi05@kuaishou.com>
* libavformat/hlsenc: Allow usage of 'periodic-rekey' with multi-variant streamsYaroslav Pogrebnyak2020-05-06
| | | | | | | | | | | | | | | | This patch adds possibility to use 'periodic-rekey' option with multi-variant streams to hlsenc muxer. All streams variants use parameters from the same key_info_file. There are 2 sets of encryption options that kind of overlaps and add complexity, so I tried to do the thing without changing too much code. There is a little duplication of the key_file, key_uri, iv_string, etc in the VariantStream since we copy it from hls to each variant stream, but generally all the code remains the same to minimise appearing of unexpected bugs. Refactoring could be done as a separate patch then as needed. Signed-off-by: Yaroslav Pogrebnyak <yyyaroslav@gmail.com>
* avformat/hlsenc: compute segment duration use current pts minus last segment ↵Steven Liu2020-05-06
| | | | | | | | | | | | | end pts segment duration is using vs duration which compute by frame per second, that can not fix problem of VFR video stream, so compute the duration when split the segment, set the segment target duration use current packet pts minus the prev segment end pts.. Reported-by: Zhao Jun <barryjzhao@tencent.com> Reviewed-by: Zhao Jun <barryjzhao@tencent.com> Signed-off-by: Steven Liu <liuqi05@kuaishou.com>
* avformat/hlsenc: add support for microseconds since epoch based sequence numberMarton Balint2020-05-01
| | | | | | | | | Sequence numbers of segments should be unique, if an encoder is using shorter than 1 second segments and it is restarted, then future segments will be using already used sequence numbers if initial sequence number is based on the number of seconds since epoch and not microseconds. Signed-off-by: Marton Balint <cus@passwd.hu>
* avformat/hlsenc: add hls_fmp4_init_resend optionSteven Liu2020-04-15
| | | | | | add option for resend init file after m3u8 refresh everytime. Signed-off-by: Steven Liu <lq@chinaffmpeg.org>
* avformat/hlsenc: return media_url directly if failed to get seperatorLimin Wang2020-04-10
| | | | | Fix ticket: 8606 Signed-off-by: Limin Wang <lance.lmwang@gmail.com>
* avformat/hlsenc: use av_asprintf()Limin Wang2020-04-08
| | | | Signed-off-by: Limin Wang <lance.lmwang@gmail.com>
* avformat/hlsenc: Factor out deleting files from deleting segmentsAndreas Rheinhardt2020-04-08
| | | | | | Removes code duplication. Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt@gmail.com>
* avformat: add subtitle support in master playlist m3u8Limin Wang2020-04-08
| | | | | | | | | | | | | | | | | | | | | | | | | Test with the following command for the webvtt subtitle: $ ./ffmpeg -y -i input_with_subtitle.mkv \ -b:v:0 5250k -c:v h264 -pix_fmt yuv420p -profile:v main -level 4.1 \ -b:a:0 256k \ -c:s webvtt -c:a mp2 -ar 48000 -ac 2 -map 0:v -map 0:a:0 -map 0:s:0 \ -f hls -var_stream_map "v:0,a:0,s:0,sgroup:subtitle" \ -master_pl_name master.m3u8 -t 300 -hls_time 10 -hls_init_time 4 -hls_list_size \ 10 -master_pl_publish_rate 10 -hls_flags \ delete_segments+discont_start+split_by_time ./tmp/video.m3u8 Check the master m3u8: $ cat tmp/master.m3u8 #EXTM3U #EXT-X-VERSION:3 #EXT-X-MEDIA:TYPE=SUBTITLES,GROUP-ID="subtitle",NAME="subtitle_0",DEFAULT=YES,URI="video_vtt.m3u8" #EXT-X-STREAM-INF:BANDWIDTH=6056600,RESOLUTION=1280x720,CODECS="avc1.4d4829,mp4a.40.33",SUBTITLES="subtitle" video.m3u8 Check the result by convert to mkv: $ ./ffmpeg -strict experimental -i ./tmp/master.m3u8 -c:v copy -c:a mp2 -c:s srt ./test.mkv Signed-off-by: Limin Wang <lance.lmwang@gmail.com>
* avformat/hlsenc: remove the first slash of the relative path line in the ↵Limin Wang2020-04-08
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | master m3u8 file Please testing with the following command: ./ffmpeg -y -i input.mkv \ -b:v:0 5250k -c:v h264 -pix_fmt yuv420p -profile:v main -level 4.1 \ -b:a:0 256k \ -c:a mp2 -ar 48000 -ac 2 -map 0:v -map 0:a:0\ -f hls -var_stream_map "v:0,a:0" \ -master_pl_name master.m3u8 -t 300 -hls_time 10 -hls_init_time 4 -hls_list_size \ 10 -master_pl_publish_rate 10 -hls_flags \ delete_segments+discont_start+split_by_time ./tmp/video.m3u8 then cat ./tmp/master.m3u8 before: #EXTM3U #EXT-X-VERSION:3 #EXT-X-STREAM-INF:BANDWIDTH=6056600,RESOLUTION=1280x720,CODECS="avc1.4d4829,mp4a.40.33" /video.m3u8 $ ./ffmpeg -i ./tmp/master.m3u8 -c:v copy -c:a mp2 ./test.mkv [hls @ 0x7f82f9000000] Skip ('#EXT-X-VERSION:3') [hls @ 0x7f82f9000000] Opening '/video.m3u8' for reading [hls @ 0x7f82f9000000] parse_playlist error No such file or directory [/video.m3u8] ./tmp/master.m3u8: No such file or directory after: #EXTM3U #EXT-X-VERSION:3 #EXT-X-STREAM-INF:BANDWIDTH=6056600,RESOLUTION=1280x720,CODECS="avc1.4d4829,mp4a.40.33" video.m3u8 Signed-off-by: Limin Wang <lance.lmwang@gmail.com>
* avformat/hlsenc: Use AVBPrint to avoid allocations of stringsAndreas Rheinhardt2020-04-08
| | | | | | when deleting old segments. Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt@gmail.com>