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

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