summaryrefslogtreecommitdiff
path: root/libavformat/segment.c
Commit message (Collapse)AuthorAgeFilesLines
* avformat: introduce AVFormatContext io_close2 which returns an intMarton Balint2021-12-121-0/+1
| | | | | | | | | | | | | | | | | Otherwise there is no way to detect an error returned by avio_close() because ff_format_io_close cannot get the return value. Checking the return value of the close function is important in order to check if all data was successfully written and the underlying close() operation was successful. It can also be useful even for read mode because it can return any pending AVIOContext error, so the user don't have to manually check AVIOContext->error. In order to still support if the user overrides io_close, the generic code only uses io_close2 if io_close is either NULL or the default io_close callback. Signed-off-by: Marton Balint <cus@passwd.hu>
* avformat: do not use AVIO_FLAG_* with avio_alloc_contextMarvin Scholz2021-12-041-1/+1
| | | | | | | | | | | | | The documentation states that here 0 should be used for read-only and 1 for a writable buffer. AVIO_FLAG_WRITE however is 2, while it works due to the way the flag is handled internally, it is still wrong according to the documentation. Additionally it makes it seem as if the AVIO_FLAG_* values could be used here, which is actually not true, as when AVIO_FLAG_READ would be used here it would create a writable buffer as AVIO_FLAG_READ is defined as 1. Signed-off-by: Anton Khirnov <anton@khirnov.net>
* avformat/avformat: Add AVStream parameter to check_bitstream() sigAndreas Rheinhardt2021-11-271-4/+6
| | | | | | | | | | For most check_bitstream() functions this just avoids having to dereference s->streams[pkt->stream_index] themselves; but for meta-muxers it will allow to forward the packet to stream with a different stream_index (belonging to a different AVFormatContext) without using a spare packet. Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt@outlook.com>
* avformat: Avoid allocation for AVStreamInternalAndreas Rheinhardt2021-09-171-4/+4
| | | | | | | | | | Do this by allocating AVStream together with the data that is currently in AVStreamInternal; or rather: Put AVStream at the beginning of a new structure called FFStream (which encompasses more than just the internal fields and is a proper context in its own right, hence the name) and remove AVStreamInternal altogether. Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt@outlook.com>
* avformat/mux: Fix double-free when using AVPacket.opaque_refAndreas Rheinhardt2021-09-031-1/+3
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | Up until now, ff_write_chained() copied the packet (manually, not with av_packet_move_ref()) from a packet given to it to a stack packet whose timing and stream_index is then modified before being sent to another muxer via av_(interleaved_)write_frame(). Afterwards it is intended to sync the fields of the packet relevant to freeing again; yet this only encompasses buf, side_data and side_data_elems and not the newly added opaque_ref. The other fields are not synced so that the returned packet can have a size > 0 and data != NULL despite its buf being NULL (this always happens in the interleaved codepath; before commit fe251f77c80b0512ab8907902e1dbed3f4fe1aad it could also happen in the noninterleaved one). This leads to double-frees if the interleaved codepath is used and opaque_ref is set. This commit therefore changes this by directly reusing the packet instead of a spare packet. Given that av_write_frame() does not change the packet given to it, one only needs to restore the timing information to return it as it was; for the interleaved codepath it is not possible to do likewise*, because av_interleaved_write_frame() takes ownership of the packets given to it and returns blank packets. But precisely because of this users of the interleaved codepath have no legitimate expectation that their packet will be returned unchanged. In line with av_interleaved_write_frame() ff_write_chained() therefore returns blank packets when using the interleaved codepath. Making the only user of said codepath compatible with this was trivial. *: Unless one wanted to create a full new reference. Reviewed-by: Lynne <dev@lynne.ee> Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt@outlook.com>
* avformat/segment: Deduplicate AVClassesAndreas Rheinhardt2021-07-081-10/+3
| | | | | | | | The child_class_next API relied on different (de)muxers to use different AVClasses; yet this API has been replaced by child_class_iterate. Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt@outlook.com>
* avformat: Constify all muxer/demuxersAndreas Rheinhardt2021-04-271-2/+2
| | | | | | | This is possible now that the next-API is gone. Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt@outlook.com> Signed-off-by: James Almer <jamrial@gmail.com>
* avformat: remove deprecated AVStream.codecJames Almer2021-04-271-6/+0
| | | | Signed-off-by: James Almer <jamrial@gmail.com>
* avutil/buffer: Switch AVBuffer API to size_tAndreas Rheinhardt2021-04-271-1/+1
| | | | | | | Announced in 14040a1d913794d9a3fd6406a6d8c2f0e37e0062. Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt@outlook.com> Signed-off-by: James Almer <jamrial@gmail.com>
* avformat: Constify the API wrt AV(In|Out)putFormatAndreas Rheinhardt2021-04-271-1/+1
| | | | | | | Also constify AVProbeData. Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt@gmail.com> Signed-off-by: James Almer <jamrial@gmail.com>
* avformat/segment: Use ff_stream_encode_params_copy()Andreas Rheinhardt2021-04-181-6/+3
| | | | | | It is simpler and more complete (e.g. it copies the id). Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt@outlook.com>
* avformat: use the buffer_size_t typedef where requiredJames Almer2021-03-101-1/+1
| | | | Signed-off-by: James Almer <jamrial@gmail.com>
* avformat/segment: Avoid duplicating string when parsing frames listAndreas Rheinhardt2020-09-101-24/+16
| | | | | Reviewed-by: Ridley Combs <rcombs@rcombs.me> Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt@gmail.com>
* avformat/segment: Fix error messagesAndreas Rheinhardt2020-09-101-3/+3
| | | | | Reviewed-by: Ridley Combs <rcombs@rcombs.me> Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt@gmail.com>
* avformat/segment: Remove redundant code for freeing in write_trailerAndreas Rheinhardt2020-09-101-13/+4
| | | | | | | | This code mostly duplicates code in the deinit function; the only exception is av_opt_free(): The options are freed generically lateron. Reviewed-by: Ridley Combs <rcombs@rcombs.me> Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt@gmail.com>
* avformat/segment: Free SegmentListEntries in deinit, not write_trailerAndreas Rheinhardt2020-09-101-9/+10
| | | | | | | This fixes leaks when the trailer is never written. Reviewed-by: Ridley Combs <rcombs@rcombs.me> Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt@gmail.com>
* avformat/segment: Fix leak and invalid free of AVIOContextAndreas Rheinhardt2020-09-101-3/+12
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | seg_init() and seg_write_header() currently contain a few error paths in which an already opened AVIOContext for the child muxer leaks (namely if there are unrecognized options for the child muxer or if writing the header of the child muxer fails); the reason for this is that this AVIOContext is not closed in the deinit function. If all goes well, it is closed when writing the trailer. From this it also follows that the AVIOContext also leaks when the trailer is never written, even when writing the header succeeds. But simply freeing said AVIOContext in the deinit function is complicated by the fact that the AVIOContext may or may not have been opened via the io_open callback: If options are set to discard header and trailer, said AVIOContext can also be a null context which must not be closed via the io_close callback. This may lead to crashes, as io_close may presume the AVIOContext's opaque to be set. It currently works with the default io_close callback which simply calls avio_close(), because avio_close() doesn't care about opaque being NULL since commit 6e8e8431e15a58aa44cfdd8c11f9ea096837c0fa. Therefore this commit records which of the two kinds of AVIOContext is currently in use to use the right way to close it. Finally there was one instance (namely if initializing the child muxer fails with no unrecognized options) where the AVIOContext was always closed via the io_close callback. The above remark applies to this; it has been fixed, too. Reviewed-by: Ridley Combs <rcombs@rcombs.me> Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt@gmail.com>
* avformat/segment: Fix leak of string on errorAndreas Rheinhardt2020-09-101-1/+1
| | | | | | | | | | | A string containing the segment's filename that the segment muxer allocates got only freed in its write_trailer function. This implies that it leaks if write_trailer is never called, e.g. if initializing the child muxer fails. This commit fixes this by freeing the string in the deinit function instead. Reviewed-by: Ridley Combs <rcombs@rcombs.me> Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt@gmail.com>
* avformat/segment: Fix segfault when error happens and segment list is outputAndreas Rheinhardt2020-09-101-1/+1
| | | | | | | | | | | | | | | | | | | | | | | The segment muxer has an option to output a file containing a list of the segments written. The AVIOContext used for writing this file is opened via the main AVFormatContext's io_open callback; seg_free() meanwhile unconditionally closes this AVIOContext by calling ff_format_io_close() with the child muxer (the one for the actual output format) as AVFormatContext. The problem hereby is that the child AVFormatContext need not exist, even when the AVIOContext does. This leads to a segfault in ff_format_io_close() when the child muxer's io_close callback is called. Situations in which the AVFormatContext can be NULL range from an invalid reference stream parameter to an unavailable/bogus/unsupported output format to inability to allocate the AVFormatContext. The solution is to simply close the AVIOContext with the AVFormatContext that was used to open it: The main AVFormatContext. Reviewed-by: Ridley Combs <rcombs@rcombs.me> Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt@gmail.com>
* avformat/segment: Fix segfault on allocation error, avoid allocationAndreas Rheinhardt2020-09-101-12/+2
| | | | | | | | | | | | | | | If the user has set none of the options specifying the segments' durations, a default value of 2s is used by duplicating a "2" string and using av_parse_time() on it. Yet duplicating the string was unchecked and if the allocation failed, one would get a segfault in av_parse_time(). This commit solves this by turning said option into an option of type AV_OPT_TYPE_DURATION (which also uses av_parse_time() internally), avoiding duplicating the string altogether. Reviewed-by: Ridley Combs <rcombs@rcombs.me> Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt@gmail.com>
* avformat/segment: Fix leak of duration/framenumber lists upon errorAndreas Rheinhardt2020-09-101-2/+2
| | | | | | | | | | | | | | | The code to free them is not in the segment muxer's deinit function, but in its write_trailer function which means that these lists leak if write_trailer isn't called after their allocation. This happens e.g. if the given lists are invalid (e.g. consisting only of ',' (which delimit entries)), so that parsing them fails and so does the muxer's init function; write_trailer is then never called. This has been fixed by moving the code to free them to the deinit function. Reviewed-by: Ridley Combs <rcombs@rcombs.me> Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt@gmail.com>
* avformat/segment: Don't overwrite AVCodecParameters after initAndreas Rheinhardt2020-09-101-18/+1
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | The segment muxer copies the user-provided AVCodecParameters to the newly created child streams in its init function before initializing the child muxer; and since commit 8e6478b723affe4d44f94d34b98e0c47f6a0b411, it does this again before calling avformat_write_header() if that is called from seg_write_header(). The reason for this is complicated: At that time writing the header was delayed, i.e. it was not triggered by avformat_write_header() (unless the AVFMT_FLAG_AUTO_BSF was unset), but instead by writing the very first packet. The rationale behind this was to allow to run bitstream filters on the packets in the interleavement queue in order to generate missing extradata from them before the muxer's write_header function is actually called. The segment muxer went even further: It initialized the child muxer and ran the child muxer's check_bitstream functions on the packets in its own muxing queue and stole any bitstream filters that got inserted. The reason for this is that the segment muxer has an option to write the header to a separate file and for this it is needed to write the child muxer's header without delay, but with correct extradata. Unsetting AVFMT_FLAG_AUTO_BSF for the child muxer accomplished the first goal and stealing the bitstream filters the second; and in order for the child muxer to actually use the updated extradata, the old AVCodecParameters (set before avformat_init_output()) were overwritten with the new ones. Updating the extradata proceeded as follows: The bitstream filter itself simply updated the AVBSFContext's par_out when processing a packet, in violation of the new BSF API (where par_out may only be set in the init function); the muxing code then simply forwarded the updated extradata, overwriting the par_in of the next BSF in the BSF chain with the fresh par_out of the last one and the AVStream's par with the par_out of the last BSF. This was an API violation, too, of course, but it made remuxing ADTS AAC into mp4/matroska work. But this no longer serves a useful purpose since the aac_adtstoasc BSF was updated to propagate new extradata via packet side data in commit f63c3516577d605e51cf16358cbdfa0bc97565d8; the next commit then removed the code in mux.c passing new extradata along the filter chain. This alone justifies removing the code for setting the AVCodecParameters a second time. But there is even another reason to do so: It is harmful. The ogg muxer parses the extradata of Theora and Vorbis in its init function and keeps pointers to parts of it. Said pointers become dangling when the extradata is overwritten by the segment muxer, leading to use-after-frees as has happened in ticket #8881 which this commit fixes. Ticket #8517 is about another issue caused by this: Immediately after having overwritten the old AVCodecParameters the segment muxer checks whether the codec_tag is ok (the codec_tag is set generically when initializing the child muxer based upon muxer-specific lists). The check used is: If the child output format has such a list and if the codec tag of the non-child stream does not match the codec id given the list of codec tags and if there is a match for the codec id in the codec tag list, then set the codec tag to zero (and not to the existing match), otherwise set the codec tag of the child stream to the codec tag of the corresponding stream of the main AVFormatContext (which is btw redundant given that the child AVCodecParameters have just been overwritten with the AVCodecParameters of the corresponding stream of the main AVFormatContext). Reviewed-by: Ridley Combs <rcombs@rcombs.me> Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt@gmail.com>
* avcodec, avformat: Remove unnecessary initializations of side data sizeAndreas Rheinhardt2020-06-221-1/+1
| | | | | Reviewed-by: Anton Khirnov <anton@khirnov.net> Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt@gmail.com>
* avformat/segment: Propagate dispositionAndreas Rheinhardt2020-05-231-0/+1
| | | | Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt@gmail.com>
* avformat/segment: Access AVStream more directlyAndreas Rheinhardt2020-05-231-9/+8
| | | | | | | by storing s->streams[i] in a pointer instead of constantly using s->streams[i]->... Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt@gmail.com>
* avformat: only allow a single bitstream filter when muxingMarton Balint2020-04-261-4/+2
| | | | | | | | | | | | Current muxers only use a single bitstream filter, so there is no need to maintain code which operates on a list of bitstream filters. When multiple bitstream filters are needed muxers can simply use a list bitstream filter. If there is a use case in the future when different bitstream filters should be added at subsequent packets then a new API possibly involving reconfiguring the list bitstream filter can be added knowing the exact requirements. Signed-off-by: Marton Balint <cus@passwd.hu>
* avformat/segment: always use interleaved writes for formats with custom ↵Marton Balint2020-04-081-1/+2
| | | | | | interleaving Signed-off-by: Marton Balint <cus@passwd.hu>
* remove CHAR_MIN/CHAR_MAX usagePaul B Mahol2020-03-171-1/+1
| | | | It is not needed at all.
* avformat/segment: Don't set extradata size twiceAndreas Rheinhardt2020-03-021-1/+0
| | | | | | | | | ff_alloc_extradata() already sets the size of the extradata so doing it again is unnecessary. Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt@gmail.com> Reviewed-by: Gyan Doshi <ffmpeg@gyani.pro> Signed-off-by: Michael Niedermayer <michael@niedermayer.cc>
* avformat/segment: use AV_OPT_TYPE_DICT for segment_format_optionsMarton Balint2020-01-011-13/+2
| | | | Signed-off-by: Marton Balint <cus@passwd.hu>
* avformat/segment: fix muxing tmcd tracks in MOVGyan Doshi2019-06-201-0/+6
| | | | | | avformat/movenc still relies on AVCodecContext time_base to mux tmcd tracks and segment muxer did not copy that field to inner streams leading to SIGFPE in the child muxer instance.
* avformat/segment: fix increment_tcGyan Doshi2019-06-181-2/+19
| | | | | | inner stream avg_frame_rate wasn't populated, so tc formation failed. Also, extended increment_tc to cover individual stream timecode.
* avformat/segment: populate empty outer stream extradata from packetGyan Doshi2019-05-281-0/+14
| | | | | | | | | | | | | | At present, if the outer stream extradata is empty but first packet has extradata as a side data element, then only the first segment's muxer instance may be able to extract this side data and use it. For all other segments, extradata in packet side data could be missing and generated segments may be invalid or unplayable in some apps e.g. for an ADTS AAC stream segmented to MP4, the adtstoasc BSF will add extradata to the first packet. The MOV muxer for the first segment will add this to codecpar for the inner stream and write Decoder Specific Information within the esds box. For other segments, their esds' will not have this decSpecificInfo and they can't be opened in Quicktime player or by services like nginx-vod-module.
* lavf: Constify AVOutputFormat pointer.Carl Eugen Hoyos2019-03-201-1/+1
|
* avformat/segment: fix segment_time_delta option min/max valueMarton Balint2018-10-071-1/+1
| | | | Signed-off-by: Marton Balint <cus@passwd.hu>
* avformat: fix stream_segment muxer build dependenciesJames Almer2018-02-071-0/+4
| | | | Signed-off-by: James Almer <jamrial@gmail.com>
* avformat: migrate to AVFormatContext->urlMarton Balint2018-01-281-15/+21
| | | | Signed-off-by: Marton Balint <cus@passwd.hu>
* Fix several typosLou Logan2017-09-211-2/+2
| | | | | | | "apix_fmts" found by Marc Péchaud. "speedloss" found by Mikhail V. Signed-off-by: Lou Logan <lou@lrcd.com>
* Use the new AVIOContext destructor.Anton Khirnov2017-09-011-1/+1
| | | | | (cherry picked from commit 6f554521afdf7ab4edbfaa9536660a1dca946b19) Signed-off-by: James Almer <jamrial@gmail.com>
* avformat: remove obsolete commented-out DEBUG defineTobias Rapp2017-07-071-2/+0
| | | | | Signed-off-by: Tobias Rapp <t.rapp@noa-archive.com> Signed-off-by: Derek Buitenhuis <derek.buitenhuis@gmail.com>
* avformat/segment: remove last_cut check when detecting a new segmentMarton Balint2017-02-041-4/+1
| | | | | | | | | Not starting a new segment if the elapsed microsecs since the start of the day equals the the elapsed microsecs since the start of the day at the time of the last cut seems plain wrong to me, Deti do you remember the original reason behind this check? Signed-off-by: Marton Balint <cus@passwd.hu>
* lavf/segment: fix crash when failing to open segment listRodger Combs2017-01-231-1/+4
| | | | | | | | | | | | | | | This happens because segment_end() returns an error, so seg_write_packet never proceeds to segment_start(), and seg->avf->pb is never re-set, so we crash with a null pb when av_write_trailer flushes the packet queue. This doesn't seem to be clearly recoverable, so I'm just failing more gracefully. Repro: ffmpeg -i input.ts -f segment -c copy -segment_list /noaxx.m3u8 test-%05d.ts (assuming you don't have write access to /)
* lavf/segment: fix autobsfRodger Combs2016-11-021-1/+39
|
* lavf/segment: fix writing separate header with auto BSFRodger Combs2016-10-241-1/+28
|
* lavf/segment: add deinit functionRodger Combs2016-10-241-27/+20
|
* lavf/segment: decide whether to rename based on list URIRodger Combs2016-10-211-1/+1
| | | | | This fixes the case of writing segments to local files, but the list over a network protocol.
* avformat/segment: give a warning message for remove initial_offset optionSteven Liu2016-09-091-0/+5
| | | | | | | ffmpeg have a generic solution working with all muxer named output_ts_offset, output_ts_offset will instead of initial_offset Signed-off-by: Michael Niedermayer <michael@niedermayer.cc>
* avformat/segment: remove the check_bitstream from segmentSteven Liu2016-07-141-21/+0
| | | | | | | | because the BSF logic was re-factored into a shareable function and both av_write_frame and av_interleaved_write_frame use it it Signed-off-by: LiuQi <liuqi@gosun.com> Signed-off-by: Michael Niedermayer <michael@niedermayer.cc>
* lavf: update auto-bsf to new BSF APIRodger Combs2016-06-251-2/+4
|
* avformat/segment: Pass flags to child contextMichael Niedermayer2016-04-111-0/+1
| | | | | | This is needed as the bitexact flag is not in the codecpar context, and thus not copied Signed-off-by: Michael Niedermayer <michael@niedermayer.cc>