Commit graph

71 commits

Author SHA1 Message Date
a5c0ed2122
avcodec/ffv1: Support >8bit rice golomb
Signed-off-by: Michael Niedermayer <michael@niedermayer.cc>
2024-11-16 13:24:16 +01:00
2c71366d3b
avcodec/ffv1: Implement new slice tiling
This fixes corner cases (requires version 4 or a spec update)

Fixes: Ticket5548

Sponsored-by: Sovereign Tech Fund
Signed-off-by: Michael Niedermayer <michael@niedermayer.cc>
2024-10-24 23:01:14 +02:00
d0927ed0a8
libavcodec/ffv1enc: Add option to select the quantization table
Sponsored-by: Sovereign Tech Fund
Signed-off-by: Michael Niedermayer <michael@niedermayer.cc>
2024-10-16 21:41:19 +02:00
81a360a5ed
avcodec/ffv1: add a named constant for the quant table size
Sponsored-by: Sovereign Tech Fund
Signed-off-by: Michael Niedermayer <michael@niedermayer.cc>
2024-10-16 21:41:19 +02:00
7bb283aa7b
avcodec/ffv1: Implement CRC with non zero initial and final value
Signed-off-by: Michael Niedermayer <michael@niedermayer.cc>
2024-10-10 19:34:10 +02:00
b7ff66a358
avcodec/ffv1enc: Prevent generation of files with broken slices
Fixes: Ticket5548

Sponsored-by: Sovereign Tech Fund
Signed-off-by: Michael Niedermayer <michael@niedermayer.cc>
2024-10-10 19:34:09 +02:00
7151081e33
avcodec/ffv1: Store and reuse sx/sy
Signed-off-by: Michael Niedermayer <michael@niedermayer.cc>
2024-10-01 19:22:35 +02:00
Anton Khirnov
8d5efc2182 lavc/ffv1dec: fix races in accessing FFV1SliceContext.slice_damaged
That variable is shared between frame threads in the same defective way
described in the previous commit. Fix it by adding a RefStruct-managed
arrays of flags that is propagated across frame threads in the standard
manner.

Remove now-unused FFV1Context.fsrc
2024-08-12 14:42:20 +02:00
Anton Khirnov
bcf08c1171 lavc/ffv1: change FFV1SliceContext.plane into a RefStruct object
Frame threading in the FFV1 decoder works in a very unusual way - the
state that needs to be propagated from the previous frame is not decoded
pixels(¹), but each slice's entropy coder state after decoding the slice.

For that purpose, the decoder's update_thread_context() callback stores
a pointer to the previous frame thread's private data. Then, when
decoding each slice, the frame thread uses the standard progress
mechanism to wait for the corresponding slice in the previous frame to
be completed, then copies the entropy coder state from the
previously-stored pointer.

This approach is highly dubious, as update_thread_context() should be
the only point where frame-thread contexts come into direct contact.
There are no guarantees that the stored pointer will be valid at all, or
will contain any particular data after update_thread_context() finishes.

More specifically, this code can break due to the fact that keyframes
reset entropy coder state and thus do not need to wait for the previous
frame. As an example, consider a decoder process with 2 frame threads -
thread 0 with its context 0, and thread 1 with context 1 - decoding a
previous frame P, current frame F, followed by a keyframe K. Then
consider concurrent execution consistent with the following sequence of
events:
* thread 0 starts decoding P
* thread 0 reads P's slice header, then calls
  ff_thread_finish_setup() allowing next frame thread to start
* main thread calls update_thread_context() to transfer state from
  context 0 to context 1; context 1 stores a pointer to context 0's private
  data
* thread 1 starts decoding F
* thread 1 reads F's slice header, then calls
  ff_thread_finish_setup() allowing the next frame thread to start
  decoding
* thread 0 finishes decoding P
* thread 0 starts decoding K; since K is a keyframe, it does not
  wait for F and reallocates the arrays holding entropy coder state
* thread 0 finishes decoding K
* thread 1 reads entropy coder state from its stored pointer to context
  0, however it finds state from K rather than from P

This execution is currently prevented by special-casing FFV1 in the
generic frame threading code, however that is supremely ugly. It also
involves unnecessary copies of the state arrays, when in fact they can
only be used by one thread at a time.

This commit addresses these deficiencies by changing the array of
PlaneContext (each of which contains the allocated state arrays)
embedded in FFV1SliceContext into a RefStruct object. This object can
then be propagated across frame threads in standard manner. Since the
code structure guarantees only one thread accesses it at a time, no
copies are necessary. It is also re-created for keyframes, solving the
above issue cleanly.

Special-casing of FFV1 in the generic frame threading code will be
removed in a later commit.

(¹) except in the case of a damaged slice, when previous frame's pixels
    are used directly
2024-08-01 10:09:26 +02:00
Anton Khirnov
d44812f7cf lavc/ffv1dec: stop using per-slice FFV1Context
All remaining accesses to them are for fields that have the same value
in the main encoder context.

Drop now-unused FFV1Context.slice_contexts.
2024-08-01 10:09:26 +02:00
Anton Khirnov
2b21cdff6e lavc/ffv1dec: move slice_damaged to per-slice context 2024-08-01 10:09:26 +02:00
Anton Khirnov
f2aeba56c4 lavc/ffv1dec: move slice_reset_contexts to per-slice context 2024-08-01 10:09:26 +02:00
Anton Khirnov
96e8af6c4d lavc/ffv1: move ac_byte_count to per-slice context 2024-08-01 10:09:26 +02:00
Anton Khirnov
e7d0f44138 lavc/ffv1enc: store per-slice rc_stat(2?) in FFV1SliceContext
Instead of the per-slice FFV1Context, which will be removed in future
commits.
2024-08-01 10:09:26 +02:00
Anton Khirnov
7b2bfba55d lavc/ffv1: move RangeCoder to per-slice context 2024-08-01 10:09:26 +02:00
Anton Khirnov
28769f6bc1 lavc/ffv1: move FFV1Context.plane to per-slice context 2024-08-01 10:09:26 +02:00
Anton Khirnov
a57c88d67b lavc/ffv1: move FFV1Context.slice_{coding_mode,rct_.y_coef} to per-slice context 2024-08-01 10:09:26 +02:00
Anton Khirnov
492df65201 lavc/ffv1: drop write-only PlaneContext.interlace_bit_state 2024-08-01 10:09:26 +02:00
Anton Khirnov
a411fc5a84 lavc/ffv1: drop redundant PlaneContext.quant_table
It is a copy of FFV1Context.quant_tables[quant_table_index].
2024-08-01 10:09:26 +02:00
Anton Khirnov
4b9f7c7e3a lavc/ffv1: drop redundant FFV1Context.quant_table
In all cases except decoding version 1 it's either not used, or contains
a copy of a table from quant_tables, which we can just as well use
directly.

When decoding version 1, we can just as well decode into
quant_tables[0], which would otherwise be unused.
2024-08-01 10:09:26 +02:00
Anton Khirnov
d2f507233a lavc/ffv1enc: move bit writer to per-slice context 2024-08-01 10:09:26 +02:00
Anton Khirnov
889faedd26 lavc/ffv1dec: move the bitreader to stack
There is no reason to place it in persistent state.
2024-08-01 10:09:25 +02:00
Anton Khirnov
19e9f3d5f2 lavc/ffv1: move run_index to the per-slice context 2024-08-01 10:09:25 +02:00
Anton Khirnov
91d3c1ac47 lavc/ffv1: move sample_buffer to the per-slice context 2024-08-01 10:09:25 +02:00
Anton Khirnov
54aa33f116 lavc/ffv1: add a per-slice context
FFV1 decoder and encoder currently use the same struct - FFV1Context -
both as codec private data and per-slice context. For this purpose
FFV1Context contains an array of pointers to per-slice FFV1Context
instances.

This pattern is highly confusing, as it is not clear which fields are
per-slice and which per-codec.

Address this by adding a new struct storing only per-slice data. Start
by moving slice_{x,y,width,height} to it.
2024-08-01 10:09:25 +02:00
Anton Khirnov
4da146ba83 lavc/ffv1dec: drop FFV1Context.cur
It is merely a pointer to FFV1Context.picture.f, which can just as well
be used directly.
2024-08-01 10:09:25 +02:00
Andreas Rheinhardt
ac8288e288 avcodec/ffv1dec: Switch to ProgressFrames
Avoids implicit av_frame_ref() and therefore allocations
and error checks. It also avoids explicitly allocating
the AVFrames (done implicitly when getting the buffer).

It also fixes a data race: The AVFrame's sample_aspect_ratio
is currently updated after ff_thread_finish_setup()
and this write is unsynchronized with the read in av_frame_ref().
Removing the implicit av_frame_ref() fixed this.

Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt@outlook.com>
2024-04-19 13:18:04 +02:00
Marton Balint
c0bc804e55 avcodec/ffv1: use 64-bit frame counter
Signed-off-by: Marton Balint <cus@passwd.hu>
2023-01-26 20:37:14 +01:00
Andreas Rheinhardt
a5e59fec07 avcodec/ffv1: Move ffv1_template.c inclusion to dec/enc templates
Both the FFV1 decoder and encoder use a template of their own
to generate code multiple times. They also use a common template,
used by both decoder and encoder templates which is currently
instantiated in ffv1.h (and therefore also in ffv1.c, which
doesn't need it at all).

All these templates have the prerequisite that two macros
are defined, namely RENAME() and TYPE. The codec-specific
templates call the functions generated via the common template
via the RENAME() macro and therefore the macros used for
the common template must coincide with the macros used for
the codec-specific templates. But then it is better to not
instantiate the common template in ffv1.h, but in the codec
specific templates.

Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt@outlook.com>
2022-10-20 06:57:30 +02:00
Andreas Rheinhardt
7e9a790441 avcodec/ffv1enc: Don't create and keep unnecessary reference
Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt@outlook.com>
2022-08-18 16:57:47 +02:00
Andreas Rheinhardt
02220b88fc avcodec/thread: Don't use ThreadFrame when unnecessary
The majority of frame-threaded decoders (mainly the intra-only)
need exactly one part of ThreadFrame: The AVFrame. They don't
need the owners nor the progress, yet they had to use it because
ff_thread_(get|release)_buffer() requires it.

This commit changes this and makes these functions work with ordinary
AVFrames; the decoders that need the extra fields for progress
use ff_thread_(get|release)_ext_buffer() which work exactly
as ff_thread_(get|release)_buffer() used to do.

This also avoids some unnecessary allocations of progress AVBuffers,
namely for H.264 and HEVC film grain frames: These frames are not
used for synchronization and therefore don't need a ThreadFrame.

Also move the ThreadFrame structure as well as ff_thread_ref_frame()
to threadframe.h, the header for frame-threaded decoders with
inter-frame dependencies.

Reviewed-by: Anton Khirnov <anton@khirnov.net>
Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt@outlook.com>
2022-02-09 17:22:35 +01:00
Andreas Rheinhardt
27f22f3383 all: Remove unnecessary libavcodec/internal.h inclusions
Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt@outlook.com>
2022-02-08 06:16:26 +01:00
Andreas Rheinhardt
485121b92c avcodec/ffv1, ffv1dec: Add const where appropriate
Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt@outlook.com>
2021-12-19 01:18:07 +01:00
Andreas Rheinhardt
2934a4b9a5 Remove unnecessary avassert.h inclusions
Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt@outlook.com>
2021-07-22 15:02:30 +02:00
Andreas Rheinhardt
cc2a9509ce libavcodec, libpostproc: Remove outcommented START/STOP_TIMER
as well as includes of libavutil/timer.h.

Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt@gmail.com>
Signed-off-by: Michael Niedermayer <michael@niedermayer.cc>
2020-03-14 18:24:04 +01:00
a53c4f3689 avcodec/ffv1: Simplify update_vlc_state()
About 0.5% faster

Signed-off-by: Michael Niedermayer <michael@niedermayer.cc>
2019-01-31 17:17:17 +01:00
5d0139d5f0 avcodec/ffv1: Simplify fold()
No speed difference, or slightly faster (the difference is too small so it may be noise
that this appears faster)

Signed-off-by: Michael Niedermayer <michael@niedermayer.cc>
2019-01-31 17:17:17 +01:00
449cdfa687 avcodec/ffv1: Increase the maximum number of slices to 1024
Signed-off-by: Michael Niedermayer <michael@niedermayer.cc>
2017-06-26 16:06:25 +02:00
ce2217b25e avcodec/ffv1: add AV_PIX_FMT_GBRP16 support
Signed-off-by: Michael Niedermayer <michael@niedermayer.cc>
2016-08-08 00:32:09 +02:00
74314f1f5f avcodec/ffv1: template functions to allow data types different from int16_t
This is required for >= 16bit RGB support
I tried it without templates but its too much duplicated code

Signed-off-by: Michael Niedermayer <michael@niedermayer.cc>
2016-08-08 00:27:43 +02:00
Derek Buitenhuis
5b0d4c247a Merge commit '96c373c7704aeb1cc1d2c275fbb5d71777665589'
* commit '96c373c7704aeb1cc1d2c275fbb5d71777665589':
  lavc: Move context_model to codec private options

Merged-by: Derek Buitenhuis <derek.buitenhuis@gmail.com>
2016-01-28 16:57:04 +00:00
Vittorio Giovara
96c373c770 lavc: Move context_model to codec private options
This option is only used by ffv1 and ffvhuff.
It is a very codec-specific option, so deprecate the global variant.
Improve documentation a little.

Signed-off-by: Vittorio Giovara <vittorio.giovara@gmail.com>
2016-01-21 15:33:19 -05:00
1c878474fb avcodec/ffv1enc: unbreak -coder option
This fixes a segfault caused by moving the coder option and changing its semantics

Signed-off-by: Michael Niedermayer <michael@niedermayer.cc>
2015-12-18 18:08:00 +01:00
Derek Buitenhuis
bba2488f07 Merge commit '4bb1070c154e49d35805fbcdac9c9e92f702ef96'
* commit '4bb1070c154e49d35805fbcdac9c9e92f702ef96':
  ffv1: Explicitly name the coder type

  Conflicts:
      libavcodec/ffv1.h
      libavcodec/ffv1dec.c
      libavcodec/ffv1enc.c

Merged-by: Derek Buitenhuis <derek.buitenhuis@gmail.com>
2015-11-22 16:57:43 +00:00
Vittorio Giovara
4bb1070c15 ffv1: Explicitly name the coder type
FFv1 uses two types of coders, golomb and range with two different
tables. This is exposed this in a rather convoluted way, for example
mentioning to set coder type 1 while initializing the variable 'ac' to 2,
because encoder does not use range coder with default table.

Appropriate internal coder type values have been added and used in any
check rather than using raw numbers.

Initialization of avctx.coder_type in ffv1dec is removed because this
field is encoder only. An unneeded validation check in the encoder
is dropped too.

Signed-off-by: Vittorio Giovara <vittorio.giovara@gmail.com>
2015-11-16 12:56:31 +01:00
aa6c43f3fd avcodec/ffv1: seperate slice_count from max_slice_count
Fix segfault with too large slice_count
Fixes Ticket4879

Signed-off-by: Michael Niedermayer <michael@niedermayer.cc>
2015-09-24 23:53:02 +02:00
Timothy Gu
f0af25ae11 ffv1: Add missing ff_ prefixes 2015-08-22 08:36:20 -07:00
b97e3e11a9 Merge commit '10a9149de242c7bbc4e130d3d7c593b89e20f80e'
* commit '10a9149de242c7bbc4e130d3d7c593b89e20f80e':
  ffv1enc: Keep coded_frame.key_frame a write-only variable

Conflicts:
	libavcodec/ffv1.h
	libavcodec/ffv1enc.c

Merged-by: Michael Niedermayer <michael@niedermayer.cc>
2015-07-20 22:25:53 +02:00
Vittorio Giovara
10a9149de2 ffv1enc: Keep coded_frame.key_frame a write-only variable 2015-07-20 14:13:42 +01:00
Vittorio Giovara
6503cbf842 ffv1enc: Add const attribute to input frame
warning: assigning to 'AVFrame *' (aka 'struct AVFrame *') from
         'const AVFrame *' (aka 'const struct AVFrame *') discards
         qualifiers [-Wincompatible-pointer-types-discards-qualifiers]
2015-05-01 14:52:24 +01:00