History

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.

Andreas Rheinhardt committed 4 years ago

Commit ac8288e1 parent 2f291472 files · +36 −50Browse files

Unified · Split

2 files
  1. libavcodec/ffv1.h +2 −3
  2. libavcodec/ffv1dec.c +34 −47
libavcodec/ffv1.h+2−3
@@ -28,13 +28,12 @@ · 27 unmodified lines
2828 * FF Video Codec 1 (a lossless codec)
2929 */
3030
31−#include "libavutil/imgutils.h"
3231 #include "avcodec.h"
3332 #include "get_bits.h"
3433 #include "mathops.h"
34+#include "progressframe.h"
3535 #include "put_bits.h"
3636 #include "rangecoder.h"
37−#include "threadframe.h"
3837
3938 #ifdef __INTEL_COMPILER
4039 #undef av_flatten
@@ -87,7 +86,7 @@ typedef struct FFV1Context { · 46 unmodified lines
8786 int flags;
8887 int64_t picture_number;
8988 int key_frame;
90− ThreadFrame picture, last_picture;
89+ ProgressFrame picture, last_picture;
9190 struct FFV1Context *fsrc;
9291
9392 AVFrame *cur;
libavcodec/ffv1dec.c+34−47
@@ -37,8 +37,8 @@ · 36 unmodified lines
3737 #include "golomb.h"
3838 #include "mathops.h"
3939 #include "ffv1.h"
40+#include "progressframe.h"
4041 #include "thread.h"
41−#include "threadframe.h"
4242
4343 static inline av_flatten int get_symbol_inline(RangeCoder *c, uint8_t *state,
4444 int is_signed)
@@ -264,8 +264,8 @@ static int decode_slice(AVCodecContext *c, void *arg) · 219 unmodified lines
264264 for( si=0; fs != f->slice_context[si]; si ++)
265265 ;
266266
267− if(f->fsrc && !(p->flags & AV_FRAME_FLAG_KEY))
268− ff_thread_await_progress(&f->last_picture, si, 0);
267+ if (f->fsrc && !(p->flags & AV_FRAME_FLAG_KEY) && f->last_picture.f)
268+ ff_progress_frame_await(&f->last_picture, si);
269269
270270 if(f->fsrc && !(p->flags & AV_FRAME_FLAG_KEY)) {
271271 FFV1Context *fssrc = f->fsrc->slice_context[si];
@@ -370,7 +370,7 @@ static int decode_slice(AVCodecContext *c, void *arg) · 98 unmodified lines
370370 }
371371 }
372372
373− ff_thread_report_progress(&f->picture, si, 0);
373+ ff_progress_frame_report(&f->picture, si);
374374
375375 return 0;
376376 }
@@ -858,11 +858,6 @@ static av_cold int decode_init(AVCodecContext *avctx) · 481 unmodified lines
858858 if ((ret = ff_ffv1_common_init(avctx)) < 0)
859859 return ret;
860860
861− f->picture.f = av_frame_alloc();
862− f->last_picture.f = av_frame_alloc();
863− if (!f->picture.f || !f->last_picture.f)
864− return AVERROR(ENOMEM);
865−
866861 if (avctx->extradata_size > 0 && (ret = read_extra_header(f)) < 0)
867862 return ret;
868863
@@ -879,31 +874,21 @@ static int decode_frame(AVCodecContext *avctx, AVFrame *rframe, · 10 unmodified lines
879874 int buf_size = avpkt->size;
880875 FFV1Context *f = avctx->priv_data;
881876 RangeCoder *const c = &f->slice_context[0]->c;
882− int i, ret;
877+ int i, ret, key_frame;
883878 uint8_t keystate = 128;
884879 uint8_t *buf_p;
885880 AVFrame *p;
886881
887− if (f->last_picture.f)
888− ff_thread_release_ext_buffer(&f->last_picture);
889− FFSWAP(ThreadFrame, f->picture, f->last_picture);
890−
891− f->cur = p = f->picture.f;
882+ ff_progress_frame_unref(&f->last_picture);
883+ FFSWAP(ProgressFrame, f->picture, f->last_picture);
892884
893− if (f->version < 3 && avctx->field_order > AV_FIELD_PROGRESSIVE) {
894− /* we have interlaced material flagged in container */
895− p->flags |= AV_FRAME_FLAG_INTERLACED;
896− if (avctx->field_order == AV_FIELD_TT || avctx->field_order == AV_FIELD_TB)
897− p->flags |= AV_FRAME_FLAG_TOP_FIELD_FIRST;
898− }
899885
900886 f->avctx = avctx;
901887 ff_init_range_decoder(c, buf, buf_size);
902888 ff_build_rac_states(c, 0.05 * (1LL << 32), 256 - 8);
903889
904− p->pict_type = AV_PICTURE_TYPE_I; //FIXME I vs. P
905890 if (get_rac(c, &keystate)) {
906− p->flags |= AV_FRAME_FLAG_KEY;
891+ key_frame = AV_FRAME_FLAG_KEY;
907892 f->key_frame_ok = 0;
908893 if ((ret = read_header(f)) < 0)
909894 return ret;
@@ -914,7 +899,7 @@ static int decode_frame(AVCodecContext *avctx, AVFrame *rframe, · 4 unmodified lines
914899 "Cannot decode non-keyframe without valid keyframe\n");
915900 return AVERROR_INVALIDDATA;
916901 }
917− p->flags &= ~AV_FRAME_FLAG_KEY;
902+ key_frame = 0;
918903 }
919904
920905 if (f->ac != AC_GOLOMB_RICE) {
@@ -932,10 +917,23 @@ static int decode_frame(AVCodecContext *avctx, AVFrame *rframe, · 11 unmodified lines
932917 return AVERROR_INVALIDDATA;
933918 }
934919
935− ret = ff_thread_get_ext_buffer(avctx, &f->picture, AV_GET_BUFFER_FLAG_REF);
920+ ret = ff_progress_frame_get_buffer(avctx, &f->picture,
921+ AV_GET_BUFFER_FLAG_REF);
936922 if (ret < 0)
937923 return ret;
938924
925+ f->cur = p = f->picture.f;
926+
927+ p->pict_type = AV_PICTURE_TYPE_I; //FIXME I vs. P
928+ p->flags = (p->flags & ~AV_FRAME_FLAG_KEY) | key_frame;
929+
930+ if (f->version < 3 && avctx->field_order > AV_FIELD_PROGRESSIVE) {
931+ /* we have interlaced material flagged in container */
932+ p->flags |= AV_FRAME_FLAG_INTERLACED;
933+ if (avctx->field_order == AV_FIELD_TT || avctx->field_order == AV_FIELD_TB)
934+ p->flags |= AV_FRAME_FLAG_TOP_FIELD_FIRST;
935+ }
936+
939937 if (avctx->debug & FF_DEBUG_PICT_INFO)
940938 av_log(avctx, AV_LOG_DEBUG, "ver:%d keyframe:%d coder:%d ec:%d slices:%d bps:%d\n",
941939 f->version, !!(p->flags & AV_FRAME_FLAG_KEY), f->ac, f->ec, f->slice_count, f->avctx->bits_per_raw_sample);
@@ -954,7 +952,7 @@ static int decode_frame(AVCodecContext *avctx, AVFrame *rframe, · 12 unmodified lines
954952 } else v = buf_p - c->bytestream_start;
955953 if (buf_p - c->bytestream_start < v) {
956954 av_log(avctx, AV_LOG_ERROR, "Slice pointer chain broken\n");
957− ff_thread_report_progress(&f->picture, INT_MAX, 0);
955+ ff_progress_frame_report(&f->picture, INT_MAX);
958956 return AVERROR_INVALIDDATA;
959957 }
960958 buf_p -= v;
@@ -996,11 +994,11 @@ static int decode_frame(AVCodecContext *avctx, AVFrame *rframe, · 35 unmodified lines
996994 for (i = f->slice_count - 1; i >= 0; i--) {
997995 FFV1Context *fs = f->slice_context[i];
998996 int j;
999− if (fs->slice_damaged && f->last_picture.f->data[0]) {
997+ if (fs->slice_damaged && f->last_picture.f) {
1000998 const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(avctx->pix_fmt);
1001999 const uint8_t *src[4];
10021000 uint8_t *dst[4];
1003− ff_thread_await_progress(&f->last_picture, INT_MAX, 0);
1001+ ff_progress_frame_await(&f->last_picture, INT_MAX);
10041002 for (j = 0; j < desc->nb_components; j++) {
10051003 int pixshift = desc->comp[j].depth > 8;
10061004 int sh = (j == 1 || j == 2) ? f->chroma_h_shift : 0;
@@ -1022,10 +1020,9 @@ static int decode_frame(AVCodecContext *avctx, AVFrame *rframe, · 15 unmodified lines
10221020 fs->slice_height);
10231021 }
10241022 }
1025− ff_thread_report_progress(&f->picture, INT_MAX, 0);
1023+ ff_progress_frame_report(&f->picture, INT_MAX);
10261024
1027− if (f->last_picture.f)
1028− ff_thread_release_ext_buffer(&f->last_picture);
1025+ ff_progress_frame_unref(&f->last_picture);
10291026 if ((ret = av_frame_ref(rframe, f->picture.f)) < 0)
10301027 return ret;
10311028
@@ -1067,7 +1064,7 @@ static int update_thread_context(AVCodecContext *dst, const AVCodecContext *src) · 35 unmodified lines
10671064 {
10681065 FFV1Context *fsrc = src->priv_data;
10691066 FFV1Context *fdst = dst->priv_data;
1070− int i, ret;
1067+ int i;
10711068
10721069 if (dst == src)
10731070 return 0;
@@ -1088,12 +1085,7 @@ static int update_thread_context(AVCodecContext *dst, const AVCodecContext *src) · 14 unmodified lines
10881085
10891086 av_assert1(fdst->max_slice_count == fsrc->max_slice_count);
10901087
1091−
1092− ff_thread_release_ext_buffer(&fdst->picture);
1093− if (fsrc->picture.f->data[0]) {
1094− if ((ret = ff_thread_ref_frame(&fdst->picture, &fsrc->picture)) < 0)
1095− return ret;
1096− }
1088+ ff_progress_frame_replace(&fdst->picture, &fsrc->picture);
10971089
10981090 fdst->fsrc = fsrc;
10991091
@@ -1105,15 +1097,9 @@ static av_cold int ffv1_decode_close(AVCodecContext *avctx) · 5 unmodified lines
11051097 {
11061098 FFV1Context *const s = avctx->priv_data;
11071099
1108− if (s->picture.f) {
1109− ff_thread_release_ext_buffer(&s->picture);
1110− av_frame_free(&s->picture.f);
1111− }
1100+ ff_progress_frame_unref(&s->picture);
1101+ ff_progress_frame_unref(&s->last_picture);
11121102
1113− if (s->last_picture.f) {
1114− ff_thread_release_ext_buffer(&s->last_picture);
1115− av_frame_free(&s->last_picture.f);
1116− }
11171103 return ff_ffv1_close(avctx);
11181104 }
11191105
@@ -1129,5 +1115,6 @@ const FFCodec ff_ffv1_decoder = { · 9 unmodified lines
11291115 UPDATE_THREAD_CONTEXT(update_thread_context),
11301116 .p.capabilities = AV_CODEC_CAP_DR1 |
11311117 AV_CODEC_CAP_FRAME_THREADS | AV_CODEC_CAP_SLICE_THREADS,
1132− .caps_internal = FF_CODEC_CAP_INIT_CLEANUP,
1118+ .caps_internal = FF_CODEC_CAP_INIT_CLEANUP |
1119+ FF_CODEC_CAP_USES_PROGRESSFRAMES,
11331120 };

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t