History

avcodec/av1dec: Use ProgressFrames

AV1 can put a frame into multiple reference slots; up until now, this involved creating a new reference to the underlying AVFrame; therefore av1_frame_ref() could fail. This commit changes this by using the ProgressFrame API to share the underlying AVFrames. (Hint: vaapi_av1_surface_id() checked whether the AV1Frames contained in the AV1DecContext were NULL or not (of course they were not); this has been changed to actually check for whether said AV1Frame is blank or not.)

Andreas Rheinhardt committed 2 years ago

Commit 0f8763f1 parent 0ec886d7 files · +47 −82Browse files

Unified · Split

7 files
  1. libavcodec/av1dec.c +25 −67
  2. libavcodec/av1dec.h +7 −1
  3. libavcodec/dxva2_av1.c +4 −4
  4. libavcodec/nvdec_av1.c +2 −2
  5. libavcodec/vaapi_av1.c +3 −3
  6. libavcodec/vdpau_av1.c +4 −3
  7. libavcodec/vulkan_av1.c +2 −2
libavcodec/av1dec.c+25−67
@@ -38,8 +38,8 @@ · 37 unmodified lines
3838 #include "itut35.h"
3939 #include "hwconfig.h"
4040 #include "profiles.h"
41+#include "progressframe.h"
4142 #include "refstruct.h"
42−#include "thread.h"
4343
4444 /**< same with Div_Lut defined in spec 7.11.3.7 */
4545 static const uint16_t div_lut[AV1_DIV_LUT_NUM] = {
@@ -672,7 +672,7 @@ static int get_pixel_format(AVCodecContext *avctx) · 626 unmodified lines
672672
673673 static void av1_frame_unref(AV1Frame *f)
674674 {
675− av_frame_unref(f->f);
675+ ff_progress_frame_unref(&f->pf);
676676 ff_refstruct_unref(&f->hwaccel_picture_private);
677677 ff_refstruct_unref(&f->header_ref);
678678 f->raw_frame_header = NULL;
@@ -683,20 +683,16 @@ static void av1_frame_unref(AV1Frame *f) · 4 unmodified lines
683683 f->coded_lossless = 0;
684684 }
685685
686−static int av1_frame_ref(AVCodecContext *avctx, AV1Frame *dst, const AV1Frame *src)
686+static void av1_frame_ref(AVCodecContext *avctx, AV1Frame *dst, const AV1Frame *src)
687687 {
688− int ret;
689−
690688 ff_refstruct_replace(&dst->header_ref, src->header_ref);
691689
692690 dst->raw_frame_header = src->raw_frame_header;
693691
694− if (!src->f->buf[0])
695− return 0;
692+ if (!src->f)
693+ return;
696694
697− ret = av_frame_ref(dst->f, src->f);
698− if (ret < 0)
699− goto fail;
695+ ff_progress_frame_ref(&dst->pf, &src->pf);
700696
701697 ff_refstruct_replace(&dst->hwaccel_picture_private,
702698 src->hwaccel_picture_private);
@@ -725,12 +721,6 @@ static int av1_frame_ref(AVCodecContext *avctx, AV1Frame *dst, const AV1Frame *s · 22 unmodified lines
725721 sizeof(dst->ref_frame_sign_bias));
726722 memcpy(dst->order_hints, src->order_hints,
727723 sizeof(dst->order_hints));
728−
729− return 0;
730−
731−fail:
732− av1_frame_unref(dst);
733− return AVERROR(ENOMEM);
734724 }
735725
736726 static av_cold int av1_decode_free(AVCodecContext *avctx)
@@ -738,16 +728,9 @@ static av_cold int av1_decode_free(AVCodecContext *avctx) · 1 unmodified line
738728 AV1DecContext *s = avctx->priv_data;
739729 AV1RawMetadataITUTT35 itut_t35;
740730
741− for (int i = 0; i < FF_ARRAY_ELEMS(s->ref); i++) {
742− if (s->ref[i].f) {
743− av1_frame_unref(&s->ref[i]);
744− av_frame_free(&s->ref[i].f);
745− }
746− }
747− if (s->cur_frame.f) {
748− av1_frame_unref(&s->cur_frame);
749− av_frame_free(&s->cur_frame.f);
750− }
731+ for (int i = 0; i < FF_ARRAY_ELEMS(s->ref); i++)
732+ av1_frame_unref(&s->ref[i]);
733+ av1_frame_unref(&s->cur_frame);
751734 av_buffer_unref(&s->seq_data_ref);
752735 ff_refstruct_unref(&s->seq_ref);
753736 ff_refstruct_unref(&s->header_ref);
@@ -863,16 +846,6 @@ static av_cold int av1_decode_init(AVCodecContext *avctx) · 109 unmodified lines
863846 s->pkt = avctx->internal->in_pkt;
864847 s->pix_fmt = AV_PIX_FMT_NONE;
865848
866− for (int i = 0; i < FF_ARRAY_ELEMS(s->ref); i++) {
867− s->ref[i].f = av_frame_alloc();
868− if (!s->ref[i].f)
869− return AVERROR(ENOMEM);
870− }
871−
872− s->cur_frame.f = av_frame_alloc();
873− if (!s->cur_frame.f)
874− return AVERROR(ENOMEM);
875−
876849 ret = ff_cbs_init(&s->cbc, AV_CODEC_ID_AV1, avctx);
877850 if (ret < 0)
878851 return ret;
@@ -934,7 +907,8 @@ static int av1_frame_alloc(AVCodecContext *avctx, AV1Frame *f) · 55 unmodified lines
934907 return ret;
935908 }
936909
937− if ((ret = ff_thread_get_buffer(avctx, f->f, AV_GET_BUFFER_FLAG_REF)) < 0)
910+ ret = ff_progress_frame_get_buffer(avctx, &f->pf, AV_GET_BUFFER_FLAG_REF);
911+ if (ret < 0)
938912 goto fail;
939913
940914 frame = f->f;
@@ -1211,23 +1185,17 @@ FF_ENABLE_DEPRECATION_WARNINGS · 270 unmodified lines
12111185 return 0;
12121186 }
12131187
1214−static int update_reference_list(AVCodecContext *avctx)
1188+static void update_reference_list(AVCodecContext *avctx)
12151189 {
12161190 AV1DecContext *s = avctx->priv_data;
12171191 const AV1RawFrameHeader *header = s->raw_frame_header;
1218− int ret;
12191192
12201193 for (int i = 0; i < AV1_NUM_REF_FRAMES; i++) {
12211194 if (header->refresh_frame_flags & (1 << i)) {
12221195 av1_frame_unref(&s->ref[i]);
1223− if ((ret = av1_frame_ref(avctx, &s->ref[i], &s->cur_frame)) < 0) {
1224− av_log(avctx, AV_LOG_ERROR,
1225− "Failed to update frame %d in reference list\n", i);
1226− return ret;
1227− }
1196+ av1_frame_ref(avctx, &s->ref[i], &s->cur_frame);
12281197 }
12291198 }
1230− return 0;
12311199 }
12321200
12331201 static int get_current_frame(AVCodecContext *avctx)
@@ -1358,20 +1326,12 @@ static int av1_receive_frame_internal(AVCodecContext *avctx, AVFrame *frame) · 124 unmodified lines
13581326 if (s->raw_frame_header->show_existing_frame) {
13591327 av1_frame_unref(&s->cur_frame);
13601328
1361− ret = av1_frame_ref(avctx, &s->cur_frame,
1362− &s->ref[s->raw_frame_header->frame_to_show_map_idx]);
1363− if (ret < 0) {
1364− av_log(avctx, AV_LOG_ERROR, "Failed to get reference frame.\n");
1365− goto end;
1366− }
1329+ av1_frame_ref(avctx, &s->cur_frame,
1330+ &s->ref[s->raw_frame_header->frame_to_show_map_idx]);
13671331
1368− ret = update_reference_list(avctx);
1369− if (ret < 0) {
1370− av_log(avctx, AV_LOG_ERROR, "Failed to update reference list.\n");
1371− goto end;
1372− }
1332+ update_reference_list(avctx);
13731333
1374− if (s->cur_frame.f->buf[0]) {
1334+ if (s->cur_frame.f) {
13751335 ret = set_output_frame(avctx, frame);
13761336 if (ret < 0)
13771337 av_log(avctx, AV_LOG_ERROR, "Set output frame error.\n");
@@ -1392,7 +1352,7 @@ static int av1_receive_frame_internal(AVCodecContext *avctx, AVFrame *frame) · 14 unmodified lines
13921352 s->cur_frame.spatial_id = header->spatial_id;
13931353 s->cur_frame.temporal_id = header->temporal_id;
13941354
1395− if (avctx->hwaccel && s->cur_frame.f->buf[0]) {
1355+ if (avctx->hwaccel && s->cur_frame.f) {
13961356 ret = FF_HW_CALL(avctx, start_frame, unit->data, unit->data_size);
13971357 if (ret < 0) {
13981358 av_log(avctx, AV_LOG_ERROR, "HW accel start frame fail.\n");
@@ -1418,7 +1378,7 @@ static int av1_receive_frame_internal(AVCodecContext *avctx, AVFrame *frame) · 19 unmodified lines
14181378 if (ret < 0)
14191379 goto end;
14201380
1421− if (avctx->hwaccel && s->cur_frame.f->buf[0]) {
1381+ if (avctx->hwaccel && s->cur_frame.f) {
14221382 ret = FF_HW_CALL(avctx, decode_slice, raw_tile_group->tile_data.data,
14231383 raw_tile_group->tile_data.data_size);
14241384 if (ret < 0) {
@@ -1469,7 +1429,7 @@ static int av1_receive_frame_internal(AVCodecContext *avctx, AVFrame *frame) · 44 unmodified lines
14691429
14701430 if (raw_tile_group && (s->tile_num == raw_tile_group->tg_end + 1)) {
14711431 int show_frame = s->raw_frame_header->show_frame;
1472− if (avctx->hwaccel && s->cur_frame.f->buf[0]) {
1432+ if (avctx->hwaccel && s->cur_frame.f) {
14731433 ret = FF_HW_SIMPLE_CALL(avctx, end_frame);
14741434 if (ret < 0) {
14751435 av_log(avctx, AV_LOG_ERROR, "HW accel end frame fail.\n");
@@ -1477,13 +1437,9 @@ static int av1_receive_frame_internal(AVCodecContext *avctx, AVFrame *frame) · 1 unmodified line
14771437 }
14781438 }
14791439
1480− ret = update_reference_list(avctx);
1481− if (ret < 0) {
1482− av_log(avctx, AV_LOG_ERROR, "Failed to update reference list.\n");
1483− goto end;
1484− }
1440+ update_reference_list(avctx);
14851441
1486− if (s->raw_frame_header->show_frame && s->cur_frame.f->buf[0]) {
1442+ if (s->raw_frame_header->show_frame && s->cur_frame.f) {
14871443 ret = set_output_frame(avctx, frame);
14881444 if (ret < 0) {
14891445 av_log(avctx, AV_LOG_ERROR, "Set output frame error\n");
@@ -1597,7 +1553,9 @@ const FFCodec ff_av1_decoder = { · 107 unmodified lines
15971553 .close = av1_decode_free,
15981554 FF_CODEC_RECEIVE_FRAME_CB(av1_receive_frame),
15991555 .p.capabilities = AV_CODEC_CAP_DR1,
1600− .caps_internal = FF_CODEC_CAP_INIT_CLEANUP | FF_CODEC_CAP_SKIP_FRAME_FILL_PARAM,
1556+ .caps_internal = FF_CODEC_CAP_INIT_CLEANUP |
1557+ FF_CODEC_CAP_SKIP_FRAME_FILL_PARAM |
1558+ FF_CODEC_CAP_USES_PROGRESSFRAMES,
16011559 .flush = av1_decode_flush,
16021560 .p.profiles = NULL_IF_CONFIG_SMALL(ff_av1_profiles),
16031561 .p.priv_class = &av1_class,
libavcodec/av1dec.h+7−1
@@ -32,9 +32,15 @@ · 31 unmodified lines
3232 #include "cbs.h"
3333 #include "cbs_av1.h"
3434 #include "dovi_rpu.h"
35+#include "progressframe.h"
3536
3637 typedef struct AV1Frame {
37− AVFrame *f;
38+ union {
39+ struct {
40+ struct AVFrame *f;
41+ };
42+ ProgressFrame pf;
43+ };
3844
3945 void *hwaccel_picture_private; ///< RefStruct reference
4046
libavcodec/dxva2_av1.c+4−4
@@ -138,9 +138,9 @@ int ff_dxva2_av1_fill_picture_parameters(const AVCodecContext *avctx, AVDXVACont · 137 unmodified lines
138138 int8_t ref_idx = frame_header->ref_frame_idx[i];
139139 AVFrame *ref_frame = h->ref[ref_idx].f;
140140
141− pp->frame_refs[i].width = ref_frame->width;
142− pp->frame_refs[i].height = ref_frame->height;
143− pp->frame_refs[i].Index = ref_frame->buf[0] ? ref_idx : 0xFF;
141+ pp->frame_refs[i].width = ref_frame ? ref_frame->width : 0;
142+ pp->frame_refs[i].height = ref_frame ? ref_frame->height : 0;
143+ pp->frame_refs[i].Index = ref_frame ? ref_idx : 0xFF;
144144
145145 /* Global Motion */
146146 pp->frame_refs[i].wminvalid = h->cur_frame.gm_invalid[AV1_REF_FRAME_LAST + i];
@@ -151,7 +151,7 @@ int ff_dxva2_av1_fill_picture_parameters(const AVCodecContext *avctx, AVDXVACont · 4 unmodified lines
151151 }
152152 for (i = 0; i < AV1_NUM_REF_FRAMES; i++) {
153153 AVFrame *ref_frame = h->ref[i].f;
154− if (ref_frame->buf[0])
154+ if (ref_frame)
155155 pp->RefFrameMapTextureIndex[i] = ff_dxva2_get_surface_index(avctx, ctx, ref_frame, 0);
156156 }
157157
libavcodec/nvdec_av1.c+2−2
@@ -251,8 +251,8 @@ static int nvdec_av1_start_frame(AVCodecContext *avctx, const uint8_t *buffer, u · 250 unmodified lines
251251 AVFrame *ref_frame = s->ref[ref_idx].f;
252252
253253 ppc->ref_frame[i].index = ppc->ref_frame_map[ref_idx];
254− ppc->ref_frame[i].width = ref_frame->width;
255− ppc->ref_frame[i].height = ref_frame->height;
254+ ppc->ref_frame[i].width = ref_frame ? ref_frame->width : 0;
255+ ppc->ref_frame[i].height = ref_frame ? ref_frame->height : 0;
256256
257257 /* Global Motion */
258258 ppc->global_motion[i].invalid = !frame_header->is_global[AV1_REF_FRAME_LAST + i];
libavcodec/vaapi_av1.c+3−3
@@ -46,7 +46,7 @@ typedef struct VAAPIAV1DecContext { · 45 unmodified lines
4646
4747 static VASurfaceID vaapi_av1_surface_id(AV1Frame *vf)
4848 {
49− if (vf)
49+ if (vf->f)
5050 return ff_vaapi_get_surface_id(vf->f);
5151 else
5252 return VA_INVALID_SURFACE;
@@ -132,7 +132,7 @@ static int vaapi_av1_start_frame(AVCodecContext *avctx, · 79 unmodified lines
132132 goto fail;
133133 pic->output_surface = ff_vaapi_get_surface_id(ctx->tmp_frame);
134134 } else {
135− pic->output_surface = vaapi_av1_surface_id(&s->cur_frame);
135+ pic->output_surface = ff_vaapi_get_surface_id(s->cur_frame.f);
136136 }
137137
138138 memset(&pic_param, 0, sizeof(VADecPictureParameterBufferAV1));
@@ -142,7 +142,7 @@ static int vaapi_av1_start_frame(AVCodecContext *avctx, · 3 unmodified lines
142142 .bit_depth_idx = bit_depth_idx,
143143 .matrix_coefficients = seq->color_config.matrix_coefficients,
144144 .current_frame = pic->output_surface,
145− .current_display_picture = vaapi_av1_surface_id(&s->cur_frame),
145+ .current_display_picture = ff_vaapi_get_surface_id(s->cur_frame.f),
146146 .frame_width_minus1 = frame_header->frame_width_minus_1,
147147 .frame_height_minus1 = frame_header->frame_height_minus_1,
148148 .primary_ref_frame = frame_header->primary_ref_frame,
libavcodec/vdpau_av1.c+4−3
@@ -219,7 +219,8 @@ static int vdpau_av1_start_frame(AVCodecContext *avctx, · 218 unmodified lines
219219 info->loop_filter_ref_deltas[i] = frame_header->loop_filter_ref_deltas[i];
220220
221221 /* Reference Frames */
222− info->ref_frame_map[i] = ff_vdpau_get_surface_id(s->ref[i].f) ? ff_vdpau_get_surface_id(s->ref[i].f) : VDP_INVALID_HANDLE;
222+ info->ref_frame_map[i] = s->ref[i].f && ff_vdpau_get_surface_id(s->ref[i].f) ?
223+ ff_vdpau_get_surface_id(s->ref[i].f) : VDP_INVALID_HANDLE;
223224 }
224225
225226 if (frame_header->primary_ref_frame == AV1_PRIMARY_REF_NONE) {
@@ -235,8 +236,8 @@ static int vdpau_av1_start_frame(AVCodecContext *avctx, · 9 unmodified lines
235236 AVFrame *ref_frame = s->ref[ref_idx].f;
236237
237238 info->ref_frame[i].index = info->ref_frame_map[ref_idx];
238− info->ref_frame[i].width = ref_frame->width;
239− info->ref_frame[i].height = ref_frame->height;
239+ info->ref_frame[i].width = ref_frame ? ref_frame->width : 0;
240+ info->ref_frame[i].height = ref_frame ? ref_frame->height : 0;
240241
241242 /* Global Motion */
242243 info->global_motion[i].invalid = !frame_header->is_global[AV1_REF_FRAME_LAST + i];
libavcodec/vulkan_av1.c+2−2
@@ -284,7 +284,7 @@ static int vk_av1_start_frame(AVCodecContext *avctx, · 283 unmodified lines
284284 AV1VulkanDecodePicture *hp = ref_frame->hwaccel_picture_private;
285285 int found = 0;
286286
287− if (ref_frame->f->pict_type == AV_PICTURE_TYPE_NONE)
287+ if (!ref_frame->f)
288288 continue;
289289
290290 for (int j = 0; j < ref_count; j++) {
@@ -326,7 +326,7 @@ static int vk_av1_start_frame(AVCodecContext *avctx, · 35 unmodified lines
326326 const AV1Frame *ref_frame = &s->ref[idx];
327327 AV1VulkanDecodePicture *hp = ref_frame->hwaccel_picture_private;
328328
329− if (ref_frame->f->pict_type == AV_PICTURE_TYPE_NONE)
329+ if (!ref_frame->f)
330330 ap->av1_pic_info.referenceNameSlotIndices[i] = AV1_REF_FRAME_NONE;
331331 else
332332 ap->av1_pic_info.referenceNameSlotIndices[i] = hp->frame_id;

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