2003-06-09 05:24:51 +03:00
|
|
|
/*
|
|
|
|
* FFV1 codec for libavcodec
|
|
|
|
*
|
2013-08-17 02:41:31 +03:00
|
|
|
* Copyright (c) 2003-2013 Michael Niedermayer <michaelni@gmx.at>
|
2003-06-09 05:24:51 +03:00
|
|
|
*
|
2006-10-07 18:30:46 +03:00
|
|
|
* This file is part of FFmpeg.
|
|
|
|
*
|
|
|
|
* FFmpeg is free software; you can redistribute it and/or
|
2003-06-09 05:24:51 +03:00
|
|
|
* modify it under the terms of the GNU Lesser General Public
|
|
|
|
* License as published by the Free Software Foundation; either
|
2006-10-07 18:30:46 +03:00
|
|
|
* version 2.1 of the License, or (at your option) any later version.
|
2003-06-09 05:24:51 +03:00
|
|
|
*
|
2006-10-07 18:30:46 +03:00
|
|
|
* FFmpeg is distributed in the hope that it will be useful,
|
2003-06-09 05:24:51 +03:00
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
* Lesser General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Lesser General Public
|
2006-10-07 18:30:46 +03:00
|
|
|
* License along with FFmpeg; if not, write to the Free Software
|
2006-01-13 00:43:26 +02:00
|
|
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
2003-06-09 05:24:51 +03:00
|
|
|
*/
|
2005-12-17 20:14:38 +02:00
|
|
|
|
2003-06-09 05:24:51 +03:00
|
|
|
/**
|
2010-04-20 17:45:34 +03:00
|
|
|
* @file
|
2010-03-22 16:26:17 +02:00
|
|
|
* FF Video Codec 1 (a lossless codec)
|
2003-06-09 05:24:51 +03:00
|
|
|
*/
|
|
|
|
|
2013-02-01 12:31:59 +03:00
|
|
|
#include "libavutil/attributes.h"
|
2012-10-11 19:55:53 +03:00
|
|
|
#include "libavutil/avassert.h"
|
2024-03-25 02:30:37 +02:00
|
|
|
#include "libavutil/mem.h"
|
2016-04-14 18:24:51 +02:00
|
|
|
|
2003-06-09 05:24:51 +03:00
|
|
|
#include "avcodec.h"
|
2012-10-21 15:05:46 +03:00
|
|
|
#include "ffv1.h"
|
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-07-11 11:08:55 +02:00
|
|
|
#include "refstruct.h"
|
2003-06-09 05:24:51 +03:00
|
|
|
|
2015-08-22 03:14:12 +02:00
|
|
|
av_cold int ff_ffv1_common_init(AVCodecContext *avctx)
|
2012-10-11 19:55:53 +03:00
|
|
|
{
|
2003-06-09 05:24:51 +03:00
|
|
|
FFV1Context *s = avctx->priv_data;
|
|
|
|
|
2012-10-17 15:17:55 +03:00
|
|
|
if (!avctx->width || !avctx->height)
|
2012-10-06 04:12:25 +03:00
|
|
|
return AVERROR_INVALIDDATA;
|
|
|
|
|
2012-10-11 19:55:53 +03:00
|
|
|
s->avctx = avctx;
|
|
|
|
s->flags = avctx->flags;
|
2005-12-17 20:14:38 +02:00
|
|
|
|
2012-10-11 19:55:53 +03:00
|
|
|
s->width = avctx->width;
|
|
|
|
s->height = avctx->height;
|
2003-06-09 05:24:51 +03:00
|
|
|
|
2012-10-11 19:55:53 +03:00
|
|
|
// defaults
|
|
|
|
s->num_h_slices = 1;
|
|
|
|
s->num_v_slices = 1;
|
2010-06-26 18:53:25 +03:00
|
|
|
|
2003-06-09 05:24:51 +03:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
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-07-11 11:08:55 +02:00
|
|
|
static void planes_free(FFRefStructOpaque opaque, void *obj)
|
|
|
|
{
|
|
|
|
PlaneContext *planes = obj;
|
|
|
|
|
|
|
|
for (int i = 0; i < MAX_PLANES; i++) {
|
|
|
|
PlaneContext *p = &planes[i];
|
|
|
|
|
|
|
|
av_freep(&p->state);
|
|
|
|
av_freep(&p->vlc_state);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
PlaneContext* ff_ffv1_planes_alloc(void)
|
|
|
|
{
|
|
|
|
return ff_refstruct_alloc_ext(sizeof(PlaneContext) * MAX_PLANES,
|
|
|
|
0, NULL, planes_free);
|
|
|
|
}
|
|
|
|
|
2024-07-08 18:01:13 +02:00
|
|
|
av_cold int ff_ffv1_init_slice_state(const FFV1Context *f,
|
2024-07-09 08:45:16 +02:00
|
|
|
FFV1SliceContext *sc)
|
2012-10-11 19:55:53 +03:00
|
|
|
{
|
2015-10-17 02:13:42 +02:00
|
|
|
int j, i;
|
2010-10-15 01:03:32 +03:00
|
|
|
|
2012-10-19 13:14:22 +03:00
|
|
|
for (j = 0; j < f->plane_count; j++) {
|
2024-07-08 18:01:13 +02:00
|
|
|
PlaneContext *const p = &sc->plane[j];
|
2012-10-19 13:14:22 +03:00
|
|
|
|
2024-07-08 13:11:52 +02:00
|
|
|
if (f->ac != AC_GOLOMB_RICE) {
|
2012-10-19 13:14:22 +03:00
|
|
|
if (!p->state)
|
2014-04-12 16:41:00 +03:00
|
|
|
p->state = av_malloc_array(p->context_count, CONTEXT_SIZE *
|
2012-10-19 13:14:22 +03:00
|
|
|
sizeof(uint8_t));
|
|
|
|
if (!p->state)
|
|
|
|
return AVERROR(ENOMEM);
|
|
|
|
} else {
|
2015-10-17 02:13:42 +02:00
|
|
|
if (!p->vlc_state) {
|
2021-09-14 21:31:53 +02:00
|
|
|
p->vlc_state = av_calloc(p->context_count, sizeof(*p->vlc_state));
|
2015-10-17 02:13:42 +02:00
|
|
|
if (!p->vlc_state)
|
|
|
|
return AVERROR(ENOMEM);
|
|
|
|
for (i = 0; i < p->context_count; i++) {
|
|
|
|
p->vlc_state[i].error_sum = 4;
|
|
|
|
p->vlc_state[i].count = 1;
|
|
|
|
}
|
|
|
|
}
|
2010-10-15 01:03:32 +03:00
|
|
|
}
|
2012-10-19 13:14:22 +03:00
|
|
|
}
|
2010-10-15 01:03:32 +03:00
|
|
|
|
2024-07-08 13:11:52 +02:00
|
|
|
if (f->ac == AC_RANGE_CUSTOM_TAB) {
|
2012-10-19 13:14:22 +03:00
|
|
|
//FIXME only redo if state_transition changed
|
|
|
|
for (j = 1; j < 256; j++) {
|
2024-07-09 08:45:16 +02:00
|
|
|
sc->c. one_state[ j] = f->state_transition[j];
|
|
|
|
sc->c.zero_state[256 - j] = 256 - sc->c.one_state[j];
|
2010-10-15 01:03:32 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2015-08-22 03:14:12 +02:00
|
|
|
av_cold int ff_ffv1_init_slices_state(FFV1Context *f)
|
2012-10-21 17:03:31 +03:00
|
|
|
{
|
|
|
|
int i, ret;
|
2015-09-24 23:49:30 +02:00
|
|
|
for (i = 0; i < f->max_slice_count; i++) {
|
2024-07-09 08:45:16 +02:00
|
|
|
if ((ret = ff_ffv1_init_slice_state(f, &f->slices[i])) < 0)
|
2012-10-21 17:03:31 +03:00
|
|
|
return AVERROR(ENOMEM);
|
2012-04-20 15:50:07 +03:00
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2024-10-01 22:06:40 +02:00
|
|
|
int ff_need_new_slices(int width, int num_h_slices, int chroma_shift) {
|
|
|
|
int mpw = 1<<chroma_shift;
|
|
|
|
int i = width * (int64_t)(num_h_slices - 1) / num_h_slices;
|
|
|
|
|
|
|
|
return width % mpw && (width - i) % mpw == 0;
|
|
|
|
}
|
|
|
|
|
2024-10-01 22:08:08 +02:00
|
|
|
int ff_slice_coord(const FFV1Context *f, int width, int sx, int num_h_slices, int chroma_shift) {
|
|
|
|
int mpw = 1<<chroma_shift;
|
|
|
|
int awidth = FFALIGN(width, mpw);
|
|
|
|
|
|
|
|
if (f->version < 4 || f->version == 4 && f->micro_version < 3)
|
|
|
|
return width * sx / num_h_slices;
|
|
|
|
|
|
|
|
sx = (2LL * awidth * sx + num_h_slices * mpw) / (2 * num_h_slices * mpw) * mpw;
|
|
|
|
if (sx == awidth)
|
|
|
|
sx = width;
|
|
|
|
return sx;
|
|
|
|
}
|
|
|
|
|
2015-08-22 03:14:12 +02:00
|
|
|
av_cold int ff_ffv1_init_slice_contexts(FFV1Context *f)
|
2012-10-11 19:55:53 +03:00
|
|
|
{
|
2024-07-10 17:25:54 +02:00
|
|
|
int max_slice_count = f->num_h_slices * f->num_v_slices;
|
2010-10-15 01:03:32 +03:00
|
|
|
|
2020-09-14 04:58:34 +02:00
|
|
|
av_assert0(max_slice_count > 0);
|
2012-10-11 19:55:53 +03:00
|
|
|
|
2024-07-04 10:22:53 +02:00
|
|
|
f->slices = av_calloc(max_slice_count, sizeof(*f->slices));
|
|
|
|
if (!f->slices)
|
|
|
|
return AVERROR(ENOMEM);
|
|
|
|
|
2024-07-07 19:46:20 +02:00
|
|
|
f->max_slice_count = max_slice_count;
|
|
|
|
|
2024-07-10 17:25:54 +02:00
|
|
|
for (int i = 0; i < max_slice_count; i++) {
|
2024-07-04 10:22:53 +02:00
|
|
|
FFV1SliceContext *sc = &f->slices[i];
|
2012-10-11 19:55:53 +03:00
|
|
|
int sx = i % f->num_h_slices;
|
|
|
|
int sy = i / f->num_h_slices;
|
2024-10-01 22:08:08 +02:00
|
|
|
int sxs = ff_slice_coord(f, f->avctx->width , sx , f->num_h_slices, f->chroma_h_shift);
|
|
|
|
int sxe = ff_slice_coord(f, f->avctx->width , sx + 1, f->num_h_slices, f->chroma_h_shift);
|
|
|
|
int sys = ff_slice_coord(f, f->avctx->height, sy , f->num_v_slices, f->chroma_v_shift);
|
|
|
|
int sye = ff_slice_coord(f, f->avctx->height, sy + 1, f->num_v_slices, f->chroma_v_shift);
|
2010-10-15 01:03:32 +03:00
|
|
|
|
2024-07-04 10:22:53 +02:00
|
|
|
sc->slice_width = sxe - sxs;
|
|
|
|
sc->slice_height = sye - sys;
|
|
|
|
sc->slice_x = sxs;
|
|
|
|
sc->slice_y = sys;
|
2023-10-07 01:43:42 +02:00
|
|
|
sc->sx = sx;
|
|
|
|
sc->sy = sy;
|
2010-10-15 01:03:32 +03:00
|
|
|
|
2024-07-10 17:25:54 +02:00
|
|
|
sc->sample_buffer = av_malloc_array((f->width + 6), 3 * MAX_PLANES *
|
2024-07-07 19:46:20 +02:00
|
|
|
sizeof(*sc->sample_buffer));
|
2024-07-10 17:25:54 +02:00
|
|
|
sc->sample_buffer32 = av_malloc_array((f->width + 6), 3 * MAX_PLANES *
|
2024-07-07 19:46:20 +02:00
|
|
|
sizeof(*sc->sample_buffer32));
|
|
|
|
if (!sc->sample_buffer || !sc->sample_buffer32)
|
|
|
|
return AVERROR(ENOMEM);
|
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-07-11 11:08:55 +02:00
|
|
|
|
|
|
|
sc->plane = ff_ffv1_planes_alloc();
|
|
|
|
if (!sc->plane)
|
|
|
|
return AVERROR(ENOMEM);
|
2010-10-15 01:03:32 +03:00
|
|
|
}
|
2015-04-09 23:37:59 +02:00
|
|
|
|
2024-07-07 19:46:20 +02:00
|
|
|
return 0;
|
2010-10-15 01:03:32 +03:00
|
|
|
}
|
|
|
|
|
2015-08-22 03:14:12 +02:00
|
|
|
int ff_ffv1_allocate_initial_states(FFV1Context *f)
|
2012-10-11 19:55:53 +03:00
|
|
|
{
|
2010-10-27 02:01:11 +03:00
|
|
|
int i;
|
|
|
|
|
2012-10-11 19:55:53 +03:00
|
|
|
for (i = 0; i < f->quant_table_count; i++) {
|
2014-04-12 16:41:00 +03:00
|
|
|
f->initial_states[i] = av_malloc_array(f->context_count[i],
|
2012-10-11 19:55:53 +03:00
|
|
|
sizeof(*f->initial_states[i]));
|
|
|
|
if (!f->initial_states[i])
|
2010-10-27 02:01:11 +03:00
|
|
|
return AVERROR(ENOMEM);
|
2012-10-11 19:55:53 +03:00
|
|
|
memset(f->initial_states[i], 128,
|
|
|
|
f->context_count[i] * sizeof(*f->initial_states[i]));
|
2010-10-27 02:01:11 +03:00
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2024-07-08 18:01:13 +02:00
|
|
|
void ff_ffv1_clear_slice_state(const FFV1Context *f, FFV1SliceContext *sc)
|
2012-10-11 19:55:53 +03:00
|
|
|
{
|
2012-10-21 15:05:46 +03:00
|
|
|
int i, j;
|
2010-10-24 19:55:42 +03:00
|
|
|
|
2012-10-19 13:14:22 +03:00
|
|
|
for (i = 0; i < f->plane_count; i++) {
|
2024-07-08 18:01:13 +02:00
|
|
|
PlaneContext *p = &sc->plane[i];
|
2012-10-19 13:14:22 +03:00
|
|
|
|
2024-07-08 13:11:52 +02:00
|
|
|
if (f->ac != AC_GOLOMB_RICE) {
|
2012-10-19 13:14:22 +03:00
|
|
|
if (f->initial_states[p->quant_table_index]) {
|
|
|
|
memcpy(p->state, f->initial_states[p->quant_table_index],
|
|
|
|
CONTEXT_SIZE * p->context_count);
|
|
|
|
} else
|
|
|
|
memset(p->state, 128, CONTEXT_SIZE * p->context_count);
|
|
|
|
} else {
|
|
|
|
for (j = 0; j < p->context_count; j++) {
|
|
|
|
p->vlc_state[j].drift = 0;
|
|
|
|
p->vlc_state[j].error_sum = 4; //FFMAX((RANGE + 32)/64, 2);
|
|
|
|
p->vlc_state[j].bias = 0;
|
|
|
|
p->vlc_state[j].count = 1;
|
2010-10-24 19:55:42 +03:00
|
|
|
}
|
|
|
|
}
|
2003-06-09 05:24:51 +03:00
|
|
|
}
|
2010-10-24 19:55:42 +03:00
|
|
|
}
|
|
|
|
|
2012-10-21 15:05:46 +03:00
|
|
|
|
2015-08-22 03:14:12 +02:00
|
|
|
av_cold int ff_ffv1_close(AVCodecContext *avctx)
|
2003-06-09 05:24:51 +03:00
|
|
|
{
|
|
|
|
FFV1Context *s = avctx->priv_data;
|
2012-10-21 15:05:46 +03:00
|
|
|
int i, j;
|
2005-12-17 20:14:38 +02:00
|
|
|
|
2015-09-24 23:49:30 +02:00
|
|
|
for (j = 0; j < s->max_slice_count; j++) {
|
2024-07-07 19:46:20 +02:00
|
|
|
FFV1SliceContext *sc = &s->slices[j];
|
|
|
|
|
|
|
|
av_freep(&sc->sample_buffer);
|
|
|
|
av_freep(&sc->sample_buffer32);
|
|
|
|
|
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-07-11 11:08:55 +02:00
|
|
|
ff_refstruct_unref(&sc->plane);
|
2012-02-12 01:42:58 +03:00
|
|
|
}
|
2003-11-01 19:38:25 +02:00
|
|
|
|
2024-07-16 18:21:29 +02:00
|
|
|
ff_refstruct_unref(&s->slice_damaged);
|
|
|
|
|
2012-10-21 15:05:46 +03:00
|
|
|
av_freep(&avctx->stats_out);
|
|
|
|
for (j = 0; j < s->quant_table_count; j++) {
|
|
|
|
av_freep(&s->initial_states[j]);
|
2015-09-24 23:49:30 +02:00
|
|
|
for (i = 0; i < s->max_slice_count; i++) {
|
2024-07-10 16:51:45 +02:00
|
|
|
FFV1SliceContext *sc = &s->slices[i];
|
|
|
|
av_freep(&sc->rc_stat2[j]);
|
2010-10-27 02:01:17 +03:00
|
|
|
}
|
2012-10-21 15:05:46 +03:00
|
|
|
av_freep(&s->rc_stat2[j]);
|
2010-10-27 02:01:17 +03:00
|
|
|
}
|
2010-10-24 15:31:09 +03:00
|
|
|
|
2024-07-04 10:22:53 +02:00
|
|
|
av_freep(&s->slices);
|
|
|
|
|
2012-04-20 16:05:36 +03:00
|
|
|
return 0;
|
|
|
|
}
|