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.
Unified · Split
| @@ -28,13 +28,12 @@ · 27 unmodified lines | ||
| 28 | 28 | * FF Video Codec 1 (a lossless codec) |
| 29 | 29 | */ |
| 30 | 30 | |
| 31 | −#include "libavutil/imgutils.h" | |
| 32 | 31 | #include "avcodec.h" |
| 33 | 32 | #include "get_bits.h" |
| 34 | 33 | #include "mathops.h" |
| 34 | +#include "progressframe.h" | |
| 35 | 35 | #include "put_bits.h" |
| 36 | 36 | #include "rangecoder.h" |
| 37 | −#include "threadframe.h" | |
| 38 | 37 | |
| 39 | 38 | #ifdef __INTEL_COMPILER |
| 40 | 39 | #undef av_flatten |
| @@ -87,7 +86,7 @@ typedef struct FFV1Context { · 46 unmodified lines | ||
| 87 | 86 | int flags; |
| 88 | 87 | int64_t picture_number; |
| 89 | 88 | int key_frame; |
| 90 | − ThreadFrame picture, last_picture; | |
| 89 | + ProgressFrame picture, last_picture; | |
| 91 | 90 | struct FFV1Context *fsrc; |
| 92 | 91 | |
| 93 | 92 | AVFrame *cur; |
| @@ -37,8 +37,8 @@ · 36 unmodified lines | ||
| 37 | 37 | #include "golomb.h" |
| 38 | 38 | #include "mathops.h" |
| 39 | 39 | #include "ffv1.h" |
| 40 | +#include "progressframe.h" | |
| 40 | 41 | #include "thread.h" |
| 41 | −#include "threadframe.h" | |
| 42 | 42 | |
| 43 | 43 | static inline av_flatten int get_symbol_inline(RangeCoder *c, uint8_t *state, |
| 44 | 44 | int is_signed) |
| @@ -264,8 +264,8 @@ static int decode_slice(AVCodecContext *c, void *arg) · 219 unmodified lines | ||
| 264 | 264 | for( si=0; fs != f->slice_context[si]; si ++) |
| 265 | 265 | ; |
| 266 | 266 | |
| 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); | |
| 269 | 269 | |
| 270 | 270 | if(f->fsrc && !(p->flags & AV_FRAME_FLAG_KEY)) { |
| 271 | 271 | FFV1Context *fssrc = f->fsrc->slice_context[si]; |
| @@ -370,7 +370,7 @@ static int decode_slice(AVCodecContext *c, void *arg) · 98 unmodified lines | ||
| 370 | 370 | } |
| 371 | 371 | } |
| 372 | 372 | |
| 373 | − ff_thread_report_progress(&f->picture, si, 0); | |
| 373 | + ff_progress_frame_report(&f->picture, si); | |
| 374 | 374 | |
| 375 | 375 | return 0; |
| 376 | 376 | } |
| @@ -858,11 +858,6 @@ static av_cold int decode_init(AVCodecContext *avctx) · 481 unmodified lines | ||
| 858 | 858 | if ((ret = ff_ffv1_common_init(avctx)) < 0) |
| 859 | 859 | return ret; |
| 860 | 860 | |
| 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 | − | |
| 866 | 861 | if (avctx->extradata_size > 0 && (ret = read_extra_header(f)) < 0) |
| 867 | 862 | return ret; |
| 868 | 863 | |
| @@ -879,31 +874,21 @@ static int decode_frame(AVCodecContext *avctx, AVFrame *rframe, · 10 unmodified lines | ||
| 879 | 874 | int buf_size = avpkt->size; |
| 880 | 875 | FFV1Context *f = avctx->priv_data; |
| 881 | 876 | RangeCoder *const c = &f->slice_context[0]->c; |
| 882 | − int i, ret; | |
| 877 | + int i, ret, key_frame; | |
| 883 | 878 | uint8_t keystate = 128; |
| 884 | 879 | uint8_t *buf_p; |
| 885 | 880 | AVFrame *p; |
| 886 | 881 | |
| 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); | |
| 892 | 884 | |
| 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 | − } | |
| 899 | 885 | |
| 900 | 886 | f->avctx = avctx; |
| 901 | 887 | ff_init_range_decoder(c, buf, buf_size); |
| 902 | 888 | ff_build_rac_states(c, 0.05 * (1LL << 32), 256 - 8); |
| 903 | 889 | |
| 904 | − p->pict_type = AV_PICTURE_TYPE_I; //FIXME I vs. P | |
| 905 | 890 | if (get_rac(c, &keystate)) { |
| 906 | − p->flags |= AV_FRAME_FLAG_KEY; | |
| 891 | + key_frame = AV_FRAME_FLAG_KEY; | |
| 907 | 892 | f->key_frame_ok = 0; |
| 908 | 893 | if ((ret = read_header(f)) < 0) |
| 909 | 894 | return ret; |
| @@ -914,7 +899,7 @@ static int decode_frame(AVCodecContext *avctx, AVFrame *rframe, · 4 unmodified lines | ||
| 914 | 899 | "Cannot decode non-keyframe without valid keyframe\n"); |
| 915 | 900 | return AVERROR_INVALIDDATA; |
| 916 | 901 | } |
| 917 | − p->flags &= ~AV_FRAME_FLAG_KEY; | |
| 902 | + key_frame = 0; | |
| 918 | 903 | } |
| 919 | 904 | |
| 920 | 905 | if (f->ac != AC_GOLOMB_RICE) { |
| @@ -932,10 +917,23 @@ static int decode_frame(AVCodecContext *avctx, AVFrame *rframe, · 11 unmodified lines | ||
| 932 | 917 | return AVERROR_INVALIDDATA; |
| 933 | 918 | } |
| 934 | 919 | |
| 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); | |
| 936 | 922 | if (ret < 0) |
| 937 | 923 | return ret; |
| 938 | 924 | |
| 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 | + | |
| 939 | 937 | if (avctx->debug & FF_DEBUG_PICT_INFO) |
| 940 | 938 | av_log(avctx, AV_LOG_DEBUG, "ver:%d keyframe:%d coder:%d ec:%d slices:%d bps:%d\n", |
| 941 | 939 | 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 | ||
| 954 | 952 | } else v = buf_p - c->bytestream_start; |
| 955 | 953 | if (buf_p - c->bytestream_start < v) { |
| 956 | 954 | 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); | |
| 958 | 956 | return AVERROR_INVALIDDATA; |
| 959 | 957 | } |
| 960 | 958 | buf_p -= v; |
| @@ -996,11 +994,11 @@ static int decode_frame(AVCodecContext *avctx, AVFrame *rframe, · 35 unmodified lines | ||
| 996 | 994 | for (i = f->slice_count - 1; i >= 0; i--) { |
| 997 | 995 | FFV1Context *fs = f->slice_context[i]; |
| 998 | 996 | int j; |
| 999 | − if (fs->slice_damaged && f->last_picture.f->data[0]) { | |
| 997 | + if (fs->slice_damaged && f->last_picture.f) { | |
| 1000 | 998 | const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(avctx->pix_fmt); |
| 1001 | 999 | const uint8_t *src[4]; |
| 1002 | 1000 | 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); | |
| 1004 | 1002 | for (j = 0; j < desc->nb_components; j++) { |
| 1005 | 1003 | int pixshift = desc->comp[j].depth > 8; |
| 1006 | 1004 | 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 | ||
| 1022 | 1020 | fs->slice_height); |
| 1023 | 1021 | } |
| 1024 | 1022 | } |
| 1025 | − ff_thread_report_progress(&f->picture, INT_MAX, 0); | |
| 1023 | + ff_progress_frame_report(&f->picture, INT_MAX); | |
| 1026 | 1024 | |
| 1027 | − if (f->last_picture.f) | |
| 1028 | − ff_thread_release_ext_buffer(&f->last_picture); | |
| 1025 | + ff_progress_frame_unref(&f->last_picture); | |
| 1029 | 1026 | if ((ret = av_frame_ref(rframe, f->picture.f)) < 0) |
| 1030 | 1027 | return ret; |
| 1031 | 1028 | |
| @@ -1067,7 +1064,7 @@ static int update_thread_context(AVCodecContext *dst, const AVCodecContext *src) · 35 unmodified lines | ||
| 1067 | 1064 | { |
| 1068 | 1065 | FFV1Context *fsrc = src->priv_data; |
| 1069 | 1066 | FFV1Context *fdst = dst->priv_data; |
| 1070 | − int i, ret; | |
| 1067 | + int i; | |
| 1071 | 1068 | |
| 1072 | 1069 | if (dst == src) |
| 1073 | 1070 | return 0; |
| @@ -1088,12 +1085,7 @@ static int update_thread_context(AVCodecContext *dst, const AVCodecContext *src) · 14 unmodified lines | ||
| 1088 | 1085 | |
| 1089 | 1086 | av_assert1(fdst->max_slice_count == fsrc->max_slice_count); |
| 1090 | 1087 | |
| 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); | |
| 1097 | 1089 | |
| 1098 | 1090 | fdst->fsrc = fsrc; |
| 1099 | 1091 | |
| @@ -1105,15 +1097,9 @@ static av_cold int ffv1_decode_close(AVCodecContext *avctx) · 5 unmodified lines | ||
| 1105 | 1097 | { |
| 1106 | 1098 | FFV1Context *const s = avctx->priv_data; |
| 1107 | 1099 | |
| 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); | |
| 1112 | 1102 | |
| 1113 | − if (s->last_picture.f) { | |
| 1114 | − ff_thread_release_ext_buffer(&s->last_picture); | |
| 1115 | − av_frame_free(&s->last_picture.f); | |
| 1116 | − } | |
| 1117 | 1103 | return ff_ffv1_close(avctx); |
| 1118 | 1104 | } |
| 1119 | 1105 | |
| @@ -1129,5 +1115,6 @@ const FFCodec ff_ffv1_decoder = { · 9 unmodified lines | ||
| 1129 | 1115 | UPDATE_THREAD_CONTEXT(update_thread_context), |
| 1130 | 1116 | .p.capabilities = AV_CODEC_CAP_DR1 | |
| 1131 | 1117 | 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, | |
| 1133 | 1120 | }; |