1
0
Fork 0
Commit Graph

64 Commits

Author SHA1 Message Date
Andreas Rheinhardt 790f793844 avutil/common: Don't auto-include mem.h
There are lots of files that don't need it: The number of object
files that actually need it went down from 2011 to 884 here.

Keep it for external users in order to not cause breakages.

Also improve the other headers a bit while just at it.

Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt@outlook.com>
2024-03-31 00:08:43 +01:00
Anton Khirnov e1f384adbf lavc/frame_thread_encoder: avoid assigning a whole AVCodecContext
It is highly unsafe, as AVCodecContext contains many allocated fields.
Almost everything needed by worker threads should be covered by
routing through AVCodecParameters and av_opt_copy(), except for a few
fields that are copied manually.

avcodec_free_context() can now be used for per-thread contexts.
2024-03-28 08:40:01 +01:00
Anton Khirnov 1cc24d7495 lavc: deprecate avcodec_close()
Its use has been discouraged since 2016, but now is no longer used in
avformat, so there is no reason to keep it public.
2024-02-09 16:14:56 +01:00
James Almer e0786a8eeb avcodec: remove FF_API_THREAD_SAFE_CALLBACKS
Signed-off-by: James Almer <jamrial@gmail.com>
Signed-off-by: Anton Khirnov <anton@khirnov.net>
2023-02-09 15:24:16 +01:00
Andreas Rheinhardt 65f6851448 avcodec/frame_thread_encoder: Stop serializing unreferencing AVFrames
Currently, the frame-threaded decoding API still supports thread-unsafe
callbacks. If one uses a thread-unsafe get_buffer2() callback,
calls to av_frame_unref() by the decoder are serialized, because
it is presumed that the underlying deallocator is thread-unsafe.

The frame-threaded encoder seems to have been written with this
restriction in mind: It always serializes unreferencing its AVFrames,
although no documentation forces it to do so.

This commit schedules to change this behaviour as soon as thread-unsafe
callbacks are removed. For this reason, the FF_API_THREAD_SAFE_CALLBACKS
define is reused.

Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt@outlook.com>
2022-09-03 15:41:19 +02:00
Andreas Rheinhardt 1e6307f46c avcodec/encode, frame_thread_encoder: Unify calling encode callback
The encode-callback (the callback used by the FF_CODEC_CB_TYPE_ENCODE
encoders) is currently called in two places: encode_simple_internal()
and by the worker threads of frame-threaded encoders.

After the call, some packet properties are set based upon
the corresponding AVFrame properties and the packet is made
refcounted if it isn't already. So there is some code duplication.

There was also non-duplicated code in encode_simple_internal()
which is executed even when using frame-threading. This included
an emms_c() (which is needed for frame-threading, too, if it is
needed for the single-threaded case, because there are allocations
(via av_packet_make_refcounted()) immediately after returning
from the encode-callback).

Furthermore, some further properties are only set in
encode_simple_internal(): For audio, pts and duration are derived
from the corresponding fields of the frame if the encoder does not
have the AV_CODEC_CAP_DELAY set. Yet this is wrong for frame-threaded
encoders, because frame-threading always introduces delay regardless
of whether the underlying codec has said cap. This only worked because
there are no frame-threaded audio encoders.

This commit fixes the code duplication and the above issue by factoring
this code out and reusing it in both places. It would work in case
of audio codecs with frame-threading, because now the values are
derived from the correct AVFrame.

Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt@outlook.com>
2022-08-27 02:23:43 +02:00
Andreas Rheinhardt 4dddcd08c4 avcodec/frame_thread_encoder: Forward got_packet directly
Instead of indicating whether we got a packet by setting
pkt->data and pkt->size to zero.

Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt@outlook.com>
2022-08-27 02:23:43 +02:00
Andreas Rheinhardt 4243da4ff4 avcodec/codec_internal: Use union for FFCodec decode/encode callbacks
This is possible, because every given FFCodec has to implement
exactly one of these. Doing so decreases sizeof(FFCodec) and
therefore decreases the size of the binary.
Notice that in case of position-independent code the decrease
is in .data.rel.ro, so that this translates to decreased
memory consumption.

Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt@outlook.com>
2022-04-05 20:02:37 +02:00
Andreas Rheinhardt 20f9727018 avcodec/codec_internal: Add FFCodec, hide internal part of AVCodec
Up until now, codec.h contains both public and private parts
of AVCodec. This exposes the internals of AVCodec to users
and leads them into the temptation of actually using them
and forces us to forward-declare structures and types that
users can't use at all.

This commit changes this by adding a new structure FFCodec to
codec_internal.h that extends AVCodec, i.e. contains the public
AVCodec as first member; the private fields of AVCodec are moved
to this structure, leaving codec.h clean.

Reviewed-by: Anton Khirnov <anton@khirnov.net>
Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt@outlook.com>
2022-03-21 01:33:09 +01:00
Andreas Rheinhardt 16fc8cef74 avcodec/frame_thread_encoder: Mark init and free functions as av_cold
Reviewed-by: Paul B Mahol <onemda@gmail.com>
Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt@outlook.com>
2021-09-04 08:03:33 +02:00
Andreas Rheinhardt 5f6fcb0395 avcodec/frame_thread_encoder: Return proper error codes
Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt@outlook.com>
2021-09-04 08:03:33 +02:00
Andreas Rheinhardt f1847dbc1f avcodec/frame_thread_encoder: Don't shadow variables
Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt@outlook.com>
2021-09-04 08:03:33 +02:00
Andreas Rheinhardt 2e574235b0 avcodec/frame_thread_encoder: Reindent after the previous commit
Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt@outlook.com>
2021-09-04 08:03:33 +02:00
Andreas Rheinhardt 754b8454b7 avcodec/frame_thread_encoder: Check initializing mutexes/conditions
Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt@outlook.com>
2021-09-04 08:03:33 +02:00
Michael Niedermayer 7bba0dd638 avcodec/frame_thread_encoder: Free AVCodecContext structure on error during init
Fixes: MemLeak
Fixes: 8281
Fixes: PoC_option158.jpg
Fixes: CVE-2020-22037

Reviewed-by: Andreas Rheinhardt <andreas.rheinhardt@outlook.com>
Signed-off-by: Michael Niedermayer <michael@niedermayer.cc>
2021-08-17 10:34:10 +02:00
Andreas Rheinhardt 69f120ead7 avcodec/avcodec: Don't include cpu.h
It is not used here at all; instead, add it where it is used without
including it or any of the arch-specific CPU headers.

Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt@outlook.com>
2021-07-22 12:59:07 +02:00
Andreas Rheinhardt cbf6047c83 avcodec/frame_thread_encoder: Remove redundant memcpy
In case the underlying AVCodec has no private class, the private data
of both the main as well as each worker AVCodecContext is just zeroed
(the codec's init function has not been called on any of them and
without a private class there is no way to legitimately set anything
before the aforementioned init function).

Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt@outlook.com>
2021-06-08 12:52:50 +02:00
Andreas Rheinhardt 3f6e715336 avcodec/frame_thread_encoder: Avoid dictionaries
avcodec_open2() allows to provide options via an AVDictionary;
but it is also allowed to set options by simply setting the value
of the AVCodecContext or via the AVOptions API if the codec has
a private class. Any options provided via an AVDictionary have already
been applied before ff_frame_thread_init(), so in order to copy
all the options from the main AVCodecContext and its private context,
it is enough to av_opt_copy() these options.

The current code does this, but it does more: It also copies the
user-provided AVDictionary and uses it for the initialization of
each of the worker-AVCodecContexts. This is completely unnecessary,
because said options have already been copied from the main context.

Furthermore, these options were also examined to decide if frame
threading should be used for huffman encoding in case this would incur
nondeterminism. This is wrong, because options not set via
an AVDictionary are ignored. Instead inspect the values stored in the
contexts directly. (In order to maintain the current behaviour, the
default value of the "non_deterministic" option has been changed to false,
because the absence of an entry with said key in the AVDictionary
had the consequence of disallowing nondeterminism.)

Finally, the AVDictionary has been removed from the signature of
ff_frame_thread_encoder_init().

Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt@outlook.com>
2021-06-08 12:52:50 +02:00
Andreas Rheinhardt 15cb5d00d9 avcodec/frame_thread_encoder: Use more natural types
Reviewed-by: Paul B Mahol <onemda@gmail.com>
Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt@gmail.com>
2021-02-16 22:11:53 +01:00
Andreas Rheinhardt 739175bfe5 avcodec/frame_thread_encoder: Reduce amount of code guarded by mutex
Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt@gmail.com>
2021-02-16 22:11:53 +01:00
Andreas Rheinhardt 74b0af998a avcodec/frame_thread_encoder: Avoid FIFO
It can be replaced by a simple counter.

Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt@gmail.com>
2021-02-16 22:11:53 +01:00
Andreas Rheinhardt de1b20a82b avcodec/frame_thread_encoder: Avoid allocations of AVFrames
Up until now, when using frame threaded encoding, an AVFrame would be
allocated for every frame to be encoded. These AVFrames would reach the
worker threads via a FIFO of tasks, a structure which contained the
AVFrame as well as an index into an array which gives the place where
the worker thread shall put the returned packet; in addition to that,
said structure also contained several unused fields.

This commit changes this: The AVFrames are now allocated during init in
the array that is up until now only used to return the packets. The
contents to be encoded are put into the AVFrame in the same array
element that is also used to return the packets.

Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt@gmail.com>
2021-02-16 22:11:53 +01:00
Andreas Rheinhardt 76d428e090 avcodec/frame_thread_encoder: Avoid creating reference to frame
Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt@gmail.com>
2021-02-16 22:11:53 +01:00
Andreas Rheinhardt fbf242a51d avcodec/frame_thread_encoder: Avoid allocations of AVPackets, fix deadlock
Up until now, when doing frame thread encoding, each worker thread
tried to allocate an AVPacket for every AVFrame to be encoded; said
packets would then be handed back to the main thread, where the content
of said packet is copied into the packet actually destined for output;
the temporary AVPacket is then freed.

Besides being wasteful this also has another problem: There is a risk of
deadlock, namely if no AVPacket can be allocated at all. The user
doesn't get an error at all in this case and the worker threads will
simply try to allocate a packet again and again. If the user has
supplied enough frames, the user's thread will block until a task has
been completed, which just doesn't happen if no packet can ever be
allocated.

This patch instead modifies the code to allocate the packets during
init; they are then reused again and again.

Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt@gmail.com>
2021-02-16 22:11:53 +01:00
Andreas Rheinhardt 2ccbc40eef avcodec/frame_thread_encoder: Fix segfault on allocation error
Fixes a segfault from av_fifo_size(NULL) that happens in
ff_frame_thread_encoder_free if the fifo couldn't be allocted;
furthermore the mutexes and conditions that are destroyed in
ff_frame_thread_encoder_free are not even initialized at this point,
so don't call said function.

Reviewed-by: Paul B Mahol <onemda@gmail.com>
Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt@gmail.com>
2021-02-16 22:11:53 +01:00
Andreas Rheinhardt e0f5086fab avcodec/frame_thread_encoder: Improve type safety
Reviewed-by: Paul B Mahol <onemda@gmail.com>
Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt@gmail.com>
2021-02-16 22:11:53 +01:00
James Almer cde7818d9f avcodec/frame_thread_encoder: remove usage of avcodec_encode_video2()
Call the encoder's internal AVCodec.encode2() function instead.

Signed-off-by: James Almer <jamrial@gmail.com>
2020-05-25 12:46:22 -03:00
James Almer 6275a7ec73 avcodec/frame_thread_encoder: check for frame threading codec cap instead of intra only
It's the correct dedicated capability reported by supported encoders.
Otherwise, the frame thread path will be used for unsupported encoders
like r210 for no gain.

Reviewed-by: Anton Khirnov <anton@khirnov.net>
Signed-off-by: James Almer <jamrial@gmail.com>
2020-05-18 12:39:34 -03:00
Carl Eugen Hoyos e82a619c2a lavc/frame_thread_encoder: Do not memcpy() from NULL.
Fixes ticket #7981.
2019-08-11 00:03:35 +02:00
lee ju 6a0feafebe avcodec/frame_thread_encoder: fix memory leak that occurs when close encoder without sending eof and receiving to end
Signed-off-by: Michael Niedermayer <michael@niedermayer.cc>
2018-08-16 03:05:54 +02:00
James Almer de0526070e avcodec/frame_thread_encoder: remove usage of av_dup_packet()
Signed-off-by: James Almer <jamrial@gmail.com>
2018-04-01 23:40:41 -03:00
James Almer 015f976aae avcodec/frame_thread_encoder: use av_packet_alloc()
Signed-off-by: James Almer <jamrial@gmail.com>
2017-09-23 17:52:32 -03:00
Carl Eugen Hoyos 3118e81f86 lavc/frame_thread_encoder: Do not mix variable declaration and code.
Fixes a warning:
ISO C90 forbids mixed declarations and code
2017-09-18 03:24:52 +02:00
Reimar Döffinger a149fa97d9 avcodec/frame_thread_encoder: Fix AV_OPT_TYPE_STRING handling in avctx
This is the equivalent to what 7d317d4706
did for the codec-specific options.
av_opt_copy has specific handling so it's fine that we already copied
the whole context before.

Signed-off-by: Reimar Döffinger <Reimar.Doeffinger@gmx.de>
2017-09-16 16:46:00 +02:00
Ronald S. Bultje 183216b218 frame_thread_encoder: make 'exit' member atomic.
Should fix the following tsan warning:

WARNING: ThreadSanitizer: data race (pid=19806)
  Read of size 4 at 0x7b84000012f0 by thread T9:
    #0 worker src/libavcodec/frame_thread_encoder.c:66 (ffmpeg+0x0000007f349e)
[..]
  Previous write of size 4 at 0x7b84000012f0 by main thread (mutexes: write M1395):
    #0 ff_frame_thread_encoder_free src/libavcodec/frame_thread_encoder.c:239 (ffmpeg+0x0000007f379e)
[..]
2017-09-12 08:21:10 -04:00
Michael Niedermayer 7d317d4706 avcodec/frame_thread_encoder: Fix AV_OPT_TYPE_STRING handling in priv_data
Signed-off-by: Michael Niedermayer <michael@niedermayer.cc>
2017-06-26 01:06:04 +02:00
Ronald S. Bultje d98f34d7d4 frame_thread_encoder: extend critical code covered by finished_task_mutex.
Should fix tsan errors in utvideoenc_rgb_left and related tests.
2017-05-25 10:29:07 -04:00
Michael Niedermayer f90c9c306f Check av_dup_packet() return code
Fixes: CID1338320

Signed-off-by: Michael Niedermayer <michael@niedermayer.cc>
2016-06-05 15:22:59 +02:00
Derek Buitenhuis 1a2d6055be avcodec/frame_thread_encoder: Check the private option for huffy's context modelling
Signed-off-by: Derek Buitenhuis <derek.buitenhuis@gmail.com>
2016-01-28 17:19:06 +00:00
Clément Bœsch a8bb81a05c lavc, lavu: use avutil/thread.h instead of redundant conditional includes 2015-12-07 17:25:51 +01:00
Michael Niedermayer 444e9874a7 Merge commit 'def97856de6021965db86c25a732d78689bd6bb0'
* commit 'def97856de6021965db86c25a732d78689bd6bb0':
  lavc: AV-prefix all codec capabilities

Conflicts:
	cmdutils.c
	ffmpeg.c
	ffplay.c
	libavcodec/8svx.c
	libavcodec/aacenc.c
	libavcodec/ac3dec.c
	libavcodec/adpcm.c
	libavcodec/alac.c
	libavcodec/atrac3plusdec.c
	libavcodec/bink.c
	libavcodec/dnxhddec.c
	libavcodec/dvdec.c
	libavcodec/dvenc.c
	libavcodec/ffv1dec.c
	libavcodec/ffv1enc.c
	libavcodec/fic.c
	libavcodec/flacdec.c
	libavcodec/flacenc.c
	libavcodec/flvdec.c
	libavcodec/fraps.c
	libavcodec/frwu.c
	libavcodec/gifdec.c
	libavcodec/h261dec.c
	libavcodec/hevc.c
	libavcodec/iff.c
	libavcodec/imc.c
	libavcodec/libopenjpegdec.c
	libavcodec/libvo-aacenc.c
	libavcodec/libvorbisenc.c
	libavcodec/libvpxdec.c
	libavcodec/libvpxenc.c
	libavcodec/libx264.c
	libavcodec/mjpegbdec.c
	libavcodec/mjpegdec.c
	libavcodec/mpegaudiodec_float.c
	libavcodec/msmpeg4dec.c
	libavcodec/mxpegdec.c
	libavcodec/nvenc_h264.c
	libavcodec/nvenc_hevc.c
	libavcodec/pngdec.c
	libavcodec/qpeg.c
	libavcodec/ra288.c
	libavcodec/rv10.c
	libavcodec/s302m.c
	libavcodec/sp5xdec.c
	libavcodec/takdec.c
	libavcodec/tiff.c
	libavcodec/tta.c
	libavcodec/utils.c
	libavcodec/v210dec.c
	libavcodec/vp6.c
	libavcodec/vp9.c
	libavcodec/wavpack.c
	libavcodec/yop.c

Merged-by: Michael Niedermayer <michael@niedermayer.cc>
2015-07-27 22:50:18 +02:00
Michael Niedermayer 94d68a41fa Merge commit '7c6eb0a1b7bf1aac7f033a7ec6d8cacc3b5c2615'
* commit '7c6eb0a1b7bf1aac7f033a7ec6d8cacc3b5c2615':
  lavc: AV-prefix all codec flags

Conflicts:
	doc/examples/muxing.c
	ffmpeg.c
	ffmpeg_opt.c
	ffplay.c
	libavcodec/aacdec.c
	libavcodec/aacenc.c
	libavcodec/ac3dec.c
	libavcodec/ac3enc_float.c
	libavcodec/atrac1.c
	libavcodec/atrac3.c
	libavcodec/atrac3plusdec.c
	libavcodec/dcadec.c
	libavcodec/ffv1enc.c
	libavcodec/h264.c
	libavcodec/h264_loopfilter.c
	libavcodec/h264_mb.c
	libavcodec/imc.c
	libavcodec/libmp3lame.c
	libavcodec/libtheoraenc.c
	libavcodec/libtwolame.c
	libavcodec/libvpxenc.c
	libavcodec/libxavs.c
	libavcodec/libxvid.c
	libavcodec/mpeg12dec.c
	libavcodec/mpeg12enc.c
	libavcodec/mpegaudiodec_template.c
	libavcodec/mpegvideo.c
	libavcodec/mpegvideo_enc.c
	libavcodec/mpegvideo_motion.c
	libavcodec/nellymoserdec.c
	libavcodec/nellymoserenc.c
	libavcodec/nvenc.c
	libavcodec/on2avc.c
	libavcodec/options_table.h
	libavcodec/opus_celt.c
	libavcodec/pngenc.c
	libavcodec/ra288.c
	libavcodec/ratecontrol.c
	libavcodec/twinvq.c
	libavcodec/vc1_block.c
	libavcodec/vc1_loopfilter.c
	libavcodec/vc1_mc.c
	libavcodec/vc1dec.c
	libavcodec/vorbisdec.c
	libavcodec/vp3.c
	libavcodec/wma.c
	libavcodec/wmaprodec.c
	libavcodec/x86/hpeldsp_init.c
	libavcodec/x86/me_cmp_init.c

Merged-by: Michael Niedermayer <michael@niedermayer.cc>
2015-07-27 22:10:35 +02:00
Hendrik Leppkes 1ffcf6ac90 frame_thread_encoder: use ref-counting to avoid memcpy of all input frames
Apparently uneeded lock/unlock removed by commiter

Signed-off-by: Michael Niedermayer <michaelni@gmx.at>
2014-12-23 01:36:13 +01:00
Christophe Gisquet 1467780772 huffyuvenc: add a non-deterministic option
Not actually used in huffyuvenc, but rather in setting the frame
threading.

Example for some files:
context=0:       851974   27226   1137281
context=1,ND=0:  471819   22604    972351
context=1,ND=1:  472875   22673    972582

Signed-off-by: Michael Niedermayer <michaelni@gmx.at>
2014-08-19 13:47:18 +02:00
Lukasz Marek bc4f362c92 lavc/frame_thread_encoder: use av_fifo_alloc_array
Signed-off-by: Lukasz Marek <lukasz.m.luki2@gmail.com>
2014-05-20 00:00:45 +02:00
Lukasz Marek 9b60d907ae lavc: use av_fifo_freep
Signed-off-by: Lukasz Marek <lukasz.m.luki2@gmail.com>
2014-05-07 23:39:47 +02:00
Michael Niedermayer 466988ab75 Fix dont and doesnt typos
Signed-off-by: Michael Niedermayer <michaelni@gmx.at>
2014-04-27 15:14:32 +02:00
Michael Niedermayer 3c7220fc60 avcodec/frame_thread_encoder: warn about huffyuv limitations
Signed-off-by: Michael Niedermayer <michaelni@gmx.at>
2014-02-15 15:16:51 +01:00
Michael Niedermayer f7459bcfc5 avcodec/frame_thread_encoder: restructure huffyuv checks
This makes them similar to the other checks

Signed-off-by: Michael Niedermayer <michaelni@gmx.at>
2014-02-15 15:14:46 +01:00
Carl Eugen Hoyos e1cb6dc59e Warn the user if mjpeg cbr encoding with frame threading was requested. 2014-01-30 01:37:27 +01:00