avcodec/hevcdec: 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).

Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt@outlook.com>
release/7.1
Andreas Rheinhardt 2 years ago
parent 2b46ae6407
commit a807e469d5
  1. 8
      libavcodec/hevc_filter.c
  2. 6
      libavcodec/hevc_mvs.c
  3. 22
      libavcodec/hevc_refs.c
  4. 24
      libavcodec/hevcdec.c
  5. 4
      libavcodec/hevcdec.h

@ -26,7 +26,7 @@
#include "libavutil/internal.h" #include "libavutil/internal.h"
#include "hevcdec.h" #include "hevcdec.h"
#include "threadframe.h" #include "progressframe.h"
#define LUMA 0 #define LUMA 0
#define CB 1 #define CB 1
@ -874,15 +874,15 @@ void ff_hevc_hls_filter(HEVCLocalContext *lc, int x, int y, int ctb_size)
if (y && x_end) { if (y && x_end) {
sao_filter_CTB(lc, s, x, y - ctb_size); sao_filter_CTB(lc, s, x, y - ctb_size);
if (s->threads_type & FF_THREAD_FRAME ) if (s->threads_type & FF_THREAD_FRAME )
ff_thread_report_progress(&s->ref->tf, y, 0); ff_progress_frame_report(&s->ref->tf, y);
} }
if (x_end && y_end) { if (x_end && y_end) {
sao_filter_CTB(lc, s, x , y); sao_filter_CTB(lc, s, x , y);
if (s->threads_type & FF_THREAD_FRAME ) if (s->threads_type & FF_THREAD_FRAME )
ff_thread_report_progress(&s->ref->tf, y + ctb_size, 0); ff_progress_frame_report(&s->ref->tf, y + ctb_size);
} }
} else if (s->threads_type & FF_THREAD_FRAME && x_end) } else if (s->threads_type & FF_THREAD_FRAME && x_end)
ff_thread_report_progress(&s->ref->tf, y + ctb_size - 4, 0); ff_progress_frame_report(&s->ref->tf, y + ctb_size - 4);
} }
void ff_hevc_hls_filters(HEVCLocalContext *lc, int x_ctb, int y_ctb, int ctb_size) void ff_hevc_hls_filters(HEVCLocalContext *lc, int x_ctb, int y_ctb, int ctb_size)

@ -23,7 +23,7 @@
#include "hevc.h" #include "hevc.h"
#include "hevcdec.h" #include "hevcdec.h"
#include "threadframe.h" #include "progressframe.h"
static const uint8_t l0_l1_cand_idx[12][2] = { static const uint8_t l0_l1_cand_idx[12][2] = {
{ 0, 1, }, { 0, 1, },
@ -248,7 +248,7 @@ static int temporal_luma_motion_vector(const HEVCContext *s, int x0, int y0,
x &= ~15; x &= ~15;
y &= ~15; y &= ~15;
if (s->threads_type == FF_THREAD_FRAME) if (s->threads_type == FF_THREAD_FRAME)
ff_thread_await_progress(&ref->tf, y, 0); ff_progress_frame_await(&ref->tf, y);
x_pu = x >> s->ps.sps->log2_min_pu_size; x_pu = x >> s->ps.sps->log2_min_pu_size;
y_pu = y >> s->ps.sps->log2_min_pu_size; y_pu = y >> s->ps.sps->log2_min_pu_size;
temp_col = TAB_MVF(x_pu, y_pu); temp_col = TAB_MVF(x_pu, y_pu);
@ -262,7 +262,7 @@ static int temporal_luma_motion_vector(const HEVCContext *s, int x0, int y0,
x &= ~15; x &= ~15;
y &= ~15; y &= ~15;
if (s->threads_type == FF_THREAD_FRAME) if (s->threads_type == FF_THREAD_FRAME)
ff_thread_await_progress(&ref->tf, y, 0); ff_progress_frame_await(&ref->tf, y);
x_pu = x >> s->ps.sps->log2_min_pu_size; x_pu = x >> s->ps.sps->log2_min_pu_size;
y_pu = y >> s->ps.sps->log2_min_pu_size; y_pu = y >> s->ps.sps->log2_min_pu_size;
temp_col = TAB_MVF(x_pu, y_pu); temp_col = TAB_MVF(x_pu, y_pu);

@ -26,18 +26,15 @@
#include "decode.h" #include "decode.h"
#include "hevc.h" #include "hevc.h"
#include "hevcdec.h" #include "hevcdec.h"
#include "progressframe.h"
#include "refstruct.h" #include "refstruct.h"
#include "threadframe.h"
void ff_hevc_unref_frame(HEVCFrame *frame, int flags) void ff_hevc_unref_frame(HEVCFrame *frame, int flags)
{ {
/* frame->frame can be NULL if context init failed */
if (!frame->frame || !frame->frame->buf[0])
return;
frame->flags &= ~flags; frame->flags &= ~flags;
if (!frame->flags) { if (!frame->flags) {
ff_thread_release_ext_buffer(&frame->tf); ff_progress_frame_unref(&frame->tf);
frame->frame = NULL;
av_frame_unref(frame->frame_grain); av_frame_unref(frame->frame_grain);
frame->needs_fg = 0; frame->needs_fg = 0;
@ -83,13 +80,14 @@ static HEVCFrame *alloc_frame(HEVCContext *s)
int i, j, ret; int i, j, ret;
for (i = 0; i < FF_ARRAY_ELEMS(s->DPB); i++) { for (i = 0; i < FF_ARRAY_ELEMS(s->DPB); i++) {
HEVCFrame *frame = &s->DPB[i]; HEVCFrame *frame = &s->DPB[i];
if (frame->frame->buf[0]) if (frame->frame)
continue; continue;
ret = ff_thread_get_ext_buffer(s->avctx, &frame->tf, ret = ff_progress_frame_get_buffer(s->avctx, &frame->tf,
AV_GET_BUFFER_FLAG_REF); AV_GET_BUFFER_FLAG_REF);
if (ret < 0) if (ret < 0)
return NULL; return NULL;
frame->frame = frame->tf.f;
frame->rpl = ff_refstruct_allocz(s->pkt.nb_nals * sizeof(*frame->rpl)); frame->rpl = ff_refstruct_allocz(s->pkt.nb_nals * sizeof(*frame->rpl));
if (!frame->rpl) if (!frame->rpl)
@ -135,7 +133,7 @@ int ff_hevc_set_new_ref(HEVCContext *s, AVFrame **frame, int poc)
for (i = 0; i < FF_ARRAY_ELEMS(s->DPB); i++) { for (i = 0; i < FF_ARRAY_ELEMS(s->DPB); i++) {
HEVCFrame *frame = &s->DPB[i]; HEVCFrame *frame = &s->DPB[i];
if (frame->frame->buf[0] && frame->sequence == s->seq_decode && if (frame->frame && frame->sequence == s->seq_decode &&
frame->poc == poc) { frame->poc == poc) {
av_log(s->avctx, AV_LOG_ERROR, "Duplicate POC in a sequence: %d.\n", av_log(s->avctx, AV_LOG_ERROR, "Duplicate POC in a sequence: %d.\n",
poc); poc);
@ -394,7 +392,7 @@ static HEVCFrame *find_ref_idx(HEVCContext *s, int poc, uint8_t use_msb)
for (i = 0; i < FF_ARRAY_ELEMS(s->DPB); i++) { for (i = 0; i < FF_ARRAY_ELEMS(s->DPB); i++) {
HEVCFrame *ref = &s->DPB[i]; HEVCFrame *ref = &s->DPB[i];
if (ref->frame->buf[0] && ref->sequence == s->seq_decode) { if (ref->frame && ref->sequence == s->seq_decode) {
if ((ref->poc & mask) == poc && (use_msb || ref->poc != s->poc)) if ((ref->poc & mask) == poc && (use_msb || ref->poc != s->poc))
return ref; return ref;
} }
@ -441,7 +439,7 @@ static HEVCFrame *generate_missing_ref(HEVCContext *s, int poc)
frame->flags = 0; frame->flags = 0;
if (s->threads_type == FF_THREAD_FRAME) if (s->threads_type == FF_THREAD_FRAME)
ff_thread_report_progress(&frame->tf, INT_MAX, 0); ff_progress_frame_report(&frame->tf, INT_MAX);
return frame; return frame;
} }

@ -49,9 +49,9 @@
#include "hwconfig.h" #include "hwconfig.h"
#include "internal.h" #include "internal.h"
#include "profiles.h" #include "profiles.h"
#include "progressframe.h"
#include "refstruct.h" #include "refstruct.h"
#include "thread.h" #include "thread.h"
#include "threadframe.h"
static const uint8_t hevc_pel_weight[65] = { [2] = 0, [4] = 1, [6] = 2, [8] = 3, [12] = 4, [16] = 5, [24] = 6, [32] = 7, [48] = 8, [64] = 9 }; static const uint8_t hevc_pel_weight[65] = { [2] = 0, [4] = 1, [6] = 2, [8] = 3, [12] = 4, [16] = 5, [24] = 6, [32] = 7, [48] = 8, [64] = 9 };
@ -1867,7 +1867,7 @@ static void hevc_await_progress(const HEVCContext *s, const HEVCFrame *ref,
if (s->threads_type == FF_THREAD_FRAME ) { if (s->threads_type == FF_THREAD_FRAME ) {
int y = FFMAX(0, (mv->y >> 2) + y0 + height + 9); int y = FFMAX(0, (mv->y >> 2) + y0 + height + 9);
ff_thread_await_progress(&ref->tf, y, 0); ff_progress_frame_await(&ref->tf, y);
} }
} }
@ -3239,7 +3239,7 @@ static int decode_nal_units(HEVCContext *s, const uint8_t *buf, int length)
fail: fail:
if (s->ref && s->threads_type == FF_THREAD_FRAME) if (s->ref && s->threads_type == FF_THREAD_FRAME)
ff_thread_report_progress(&s->ref->tf, INT_MAX, 0); ff_progress_frame_report(&s->ref->tf, INT_MAX);
return ret; return ret;
} }
@ -3417,14 +3417,15 @@ static int hevc_ref_frame(HEVCFrame *dst, HEVCFrame *src)
{ {
int ret; int ret;
ret = ff_thread_ref_frame(&dst->tf, &src->tf); ff_progress_frame_ref(&dst->tf, &src->tf);
if (ret < 0) dst->frame = dst->tf.f;
return ret;
if (src->needs_fg) { if (src->needs_fg) {
ret = av_frame_ref(dst->frame_grain, src->frame_grain); ret = av_frame_ref(dst->frame_grain, src->frame_grain);
if (ret < 0) if (ret < 0) {
ff_hevc_unref_frame(dst, ~0);
return ret; return ret;
}
dst->needs_fg = 1; dst->needs_fg = 1;
} }
@ -3464,7 +3465,6 @@ static av_cold int hevc_decode_free(AVCodecContext *avctx)
for (i = 0; i < FF_ARRAY_ELEMS(s->DPB); i++) { for (i = 0; i < FF_ARRAY_ELEMS(s->DPB); i++) {
ff_hevc_unref_frame(&s->DPB[i], ~0); ff_hevc_unref_frame(&s->DPB[i], ~0);
av_frame_free(&s->DPB[i].frame);
av_frame_free(&s->DPB[i].frame_grain); av_frame_free(&s->DPB[i].frame_grain);
} }
@ -3510,11 +3510,6 @@ static av_cold int hevc_init_context(AVCodecContext *avctx)
return AVERROR(ENOMEM); return AVERROR(ENOMEM);
for (i = 0; i < FF_ARRAY_ELEMS(s->DPB); i++) { for (i = 0; i < FF_ARRAY_ELEMS(s->DPB); i++) {
s->DPB[i].frame = av_frame_alloc();
if (!s->DPB[i].frame)
return AVERROR(ENOMEM);
s->DPB[i].tf.f = s->DPB[i].frame;
s->DPB[i].frame_grain = av_frame_alloc(); s->DPB[i].frame_grain = av_frame_alloc();
if (!s->DPB[i].frame_grain) if (!s->DPB[i].frame_grain)
return AVERROR(ENOMEM); return AVERROR(ENOMEM);
@ -3546,7 +3541,7 @@ static int hevc_update_thread_context(AVCodecContext *dst,
for (i = 0; i < FF_ARRAY_ELEMS(s->DPB); i++) { for (i = 0; i < FF_ARRAY_ELEMS(s->DPB); i++) {
ff_hevc_unref_frame(&s->DPB[i], ~0); ff_hevc_unref_frame(&s->DPB[i], ~0);
if (s0->DPB[i].frame->buf[0]) { if (s0->DPB[i].frame) {
ret = hevc_ref_frame(&s->DPB[i], &s0->DPB[i]); ret = hevc_ref_frame(&s->DPB[i], &s0->DPB[i]);
if (ret < 0) if (ret < 0)
return ret; return ret;
@ -3720,6 +3715,7 @@ const FFCodec ff_hevc_decoder = {
.p.capabilities = AV_CODEC_CAP_DR1 | AV_CODEC_CAP_DELAY | .p.capabilities = AV_CODEC_CAP_DR1 | AV_CODEC_CAP_DELAY |
AV_CODEC_CAP_SLICE_THREADS | AV_CODEC_CAP_FRAME_THREADS, AV_CODEC_CAP_SLICE_THREADS | AV_CODEC_CAP_FRAME_THREADS,
.caps_internal = FF_CODEC_CAP_EXPORTS_CROPPING | .caps_internal = FF_CODEC_CAP_EXPORTS_CROPPING |
FF_CODEC_CAP_USES_PROGRESSFRAMES |
FF_CODEC_CAP_INIT_CLEANUP, FF_CODEC_CAP_INIT_CLEANUP,
.p.profiles = NULL_IF_CONFIG_SMALL(ff_hevc_profiles), .p.profiles = NULL_IF_CONFIG_SMALL(ff_hevc_profiles),
.hw_configs = (const AVCodecHWConfigInternal *const []) { .hw_configs = (const AVCodecHWConfigInternal *const []) {

@ -40,7 +40,7 @@
#include "hevc_sei.h" #include "hevc_sei.h"
#include "hevcdsp.h" #include "hevcdsp.h"
#include "h274.h" #include "h274.h"
#include "threadframe.h" #include "progressframe.h"
#include "videodsp.h" #include "videodsp.h"
#define SHIFT_CTB_WPP 2 #define SHIFT_CTB_WPP 2
@ -354,7 +354,7 @@ typedef struct DBParams {
typedef struct HEVCFrame { typedef struct HEVCFrame {
AVFrame *frame; AVFrame *frame;
AVFrame *frame_grain; AVFrame *frame_grain;
ThreadFrame tf; ProgressFrame tf;
int needs_fg; /* 1 if grain needs to be applied by the decoder */ int needs_fg; /* 1 if grain needs to be applied by the decoder */
MvField *tab_mvf; ///< RefStruct reference MvField *tab_mvf; ///< RefStruct reference
RefPicList *refPicList; RefPicList *refPicList;

Loading…
Cancel
Save