2013-10-12 12:55:48 +03:00
|
|
|
/*
|
2013-12-10 17:37:21 +03:00
|
|
|
* HEVC video decoder
|
2013-10-12 12:55:48 +03:00
|
|
|
*
|
|
|
|
* Copyright (C) 2012 - 2013 Guillaume Martres
|
|
|
|
* Copyright (C) 2012 - 2013 Gildas Cocherel
|
|
|
|
*
|
|
|
|
* This file is part of FFmpeg.
|
|
|
|
*
|
|
|
|
* FFmpeg is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU Lesser General Public
|
|
|
|
* License as published by the Free Software Foundation; either
|
|
|
|
* version 2.1 of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* FFmpeg is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
* Lesser General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Lesser General Public
|
|
|
|
* License along with FFmpeg; if not, write to the Free Software
|
|
|
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
|
|
|
*/
|
|
|
|
|
2024-03-25 02:30:37 +02:00
|
|
|
#include "libavutil/mem.h"
|
2024-06-25 23:32:44 +02:00
|
|
|
#include "libavutil/stereo3d.h"
|
2013-10-12 12:55:48 +03:00
|
|
|
|
2024-06-20 21:01:49 +02:00
|
|
|
#include "container_fifo.h"
|
2022-03-10 19:03:05 +02:00
|
|
|
#include "decode.h"
|
2016-08-21 17:35:28 +02:00
|
|
|
#include "hevc.h"
|
2016-08-21 17:14:24 +02:00
|
|
|
#include "hevcdec.h"
|
2022-08-13 21:59:02 +02:00
|
|
|
#include "progressframe.h"
|
2022-08-04 16:21:03 +02:00
|
|
|
#include "refstruct.h"
|
2013-10-12 12:55:48 +03:00
|
|
|
|
2023-10-12 16:52:49 +02:00
|
|
|
void ff_hevc_unref_frame(HEVCFrame *frame, int flags)
|
2013-10-12 12:55:48 +03:00
|
|
|
{
|
|
|
|
frame->flags &= ~flags;
|
|
|
|
if (!frame->flags) {
|
2022-08-13 21:59:02 +02:00
|
|
|
ff_progress_frame_unref(&frame->tf);
|
2023-10-12 16:52:49 +02:00
|
|
|
av_frame_unref(frame->frame_grain);
|
2021-09-29 01:06:02 +02:00
|
|
|
frame->needs_fg = 0;
|
2013-10-12 12:55:48 +03:00
|
|
|
|
2024-06-05 12:02:27 +02:00
|
|
|
ff_refstruct_unref(&frame->pps);
|
2022-08-05 22:57:56 +02:00
|
|
|
ff_refstruct_unref(&frame->tab_mvf);
|
2013-10-12 12:55:48 +03:00
|
|
|
|
2022-08-04 17:33:29 +02:00
|
|
|
ff_refstruct_unref(&frame->rpl);
|
|
|
|
frame->nb_rpl_elems = 0;
|
2022-08-05 22:57:56 +02:00
|
|
|
ff_refstruct_unref(&frame->rpl_tab);
|
2013-10-12 12:55:48 +03:00
|
|
|
frame->refPicList = NULL;
|
|
|
|
|
2022-08-04 16:21:03 +02:00
|
|
|
ff_refstruct_unref(&frame->hwaccel_picture_private);
|
2013-10-12 12:55:48 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-06-05 12:02:27 +02:00
|
|
|
const RefPicList *ff_hevc_get_ref_list(const HEVCFrame *ref, int x0, int y0)
|
2013-10-12 12:55:48 +03:00
|
|
|
{
|
2024-06-05 12:02:27 +02:00
|
|
|
const HEVCSPS *sps = ref->pps->sps;
|
|
|
|
int x_cb = x0 >> sps->log2_ctb_size;
|
|
|
|
int y_cb = y0 >> sps->log2_ctb_size;
|
|
|
|
int pic_width_cb = sps->ctb_width;
|
|
|
|
int ctb_addr_ts = ref->pps->ctb_addr_rs_to_ts[y_cb * pic_width_cb + x_cb];
|
2022-06-29 16:49:24 +02:00
|
|
|
return &ref->rpl_tab[ctb_addr_ts]->refPicList[0];
|
2013-10-12 12:55:48 +03:00
|
|
|
}
|
|
|
|
|
2024-06-05 07:14:06 +02:00
|
|
|
void ff_hevc_clear_refs(HEVCLayerContext *l)
|
2013-10-12 12:55:48 +03:00
|
|
|
{
|
|
|
|
int i;
|
2024-06-05 07:14:06 +02:00
|
|
|
for (i = 0; i < FF_ARRAY_ELEMS(l->DPB); i++)
|
|
|
|
ff_hevc_unref_frame(&l->DPB[i],
|
2013-11-02 02:31:49 +03:00
|
|
|
HEVC_FRAME_FLAG_SHORT_REF |
|
|
|
|
HEVC_FRAME_FLAG_LONG_REF);
|
2013-10-12 12:55:48 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void ff_hevc_flush_dpb(HEVCContext *s)
|
|
|
|
{
|
2024-06-05 07:14:06 +02:00
|
|
|
for (int layer = 0; layer < FF_ARRAY_ELEMS(s->layers); layer++) {
|
|
|
|
HEVCLayerContext *l = &s->layers[layer];
|
|
|
|
for (int i = 0; i < FF_ARRAY_ELEMS(l->DPB); i++)
|
|
|
|
ff_hevc_unref_frame(&l->DPB[i], ~0);
|
|
|
|
}
|
2013-10-12 12:55:48 +03:00
|
|
|
}
|
|
|
|
|
2024-06-05 07:14:06 +02:00
|
|
|
static HEVCFrame *alloc_frame(HEVCContext *s, HEVCLayerContext *l)
|
2013-10-12 12:55:48 +03:00
|
|
|
{
|
2024-06-12 10:56:15 +02:00
|
|
|
const HEVCVPS *vps = l->sps->vps;
|
|
|
|
const int view_id = vps->view_id[s->cur_layer];
|
2013-10-12 12:55:48 +03:00
|
|
|
int i, j, ret;
|
2024-06-05 07:14:06 +02:00
|
|
|
for (i = 0; i < FF_ARRAY_ELEMS(l->DPB); i++) {
|
|
|
|
HEVCFrame *frame = &l->DPB[i];
|
2024-05-29 08:38:27 +02:00
|
|
|
if (frame->f)
|
2013-10-12 12:55:48 +03:00
|
|
|
continue;
|
|
|
|
|
2024-09-10 04:05:22 +02:00
|
|
|
ret = ff_progress_frame_alloc(s->avctx, &frame->tf);
|
|
|
|
if (ret < 0)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
// Add LCEVC SEI metadata here, as it's needed in get_buffer()
|
|
|
|
if (s->sei.common.lcevc.info) {
|
|
|
|
HEVCSEILCEVC *lcevc = &s->sei.common.lcevc;
|
|
|
|
ret = ff_frame_new_side_data_from_buf(s->avctx, frame->tf.f,
|
|
|
|
AV_FRAME_DATA_LCEVC, &lcevc->info);
|
|
|
|
if (ret < 0)
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
|
2024-06-12 10:56:15 +02:00
|
|
|
// add view ID side data if it's nontrivial
|
|
|
|
if (vps->nb_layers > 1 || view_id) {
|
2024-06-25 23:32:44 +02:00
|
|
|
HEVCSEITDRDI *tdrdi = &s->sei.tdrdi;
|
2024-06-12 10:56:15 +02:00
|
|
|
AVFrameSideData *sd = av_frame_side_data_new(&frame->f->side_data,
|
|
|
|
&frame->f->nb_side_data,
|
|
|
|
AV_FRAME_DATA_VIEW_ID,
|
|
|
|
sizeof(int), 0);
|
|
|
|
if (!sd)
|
|
|
|
goto fail;
|
|
|
|
*(int*)sd->data = view_id;
|
2024-06-25 23:32:44 +02:00
|
|
|
|
|
|
|
if (tdrdi->num_ref_displays) {
|
|
|
|
AVStereo3D *stereo_3d;
|
|
|
|
|
|
|
|
stereo_3d = av_stereo3d_create_side_data(frame->f);
|
|
|
|
if (!stereo_3d)
|
|
|
|
goto fail;
|
|
|
|
|
|
|
|
stereo_3d->type = AV_STEREO3D_FRAMESEQUENCE;
|
|
|
|
if (tdrdi->left_view_id[0] == view_id)
|
|
|
|
stereo_3d->view = AV_STEREO3D_VIEW_LEFT;
|
|
|
|
else if (tdrdi->right_view_id[0] == view_id)
|
|
|
|
stereo_3d->view = AV_STEREO3D_VIEW_RIGHT;
|
|
|
|
else
|
|
|
|
stereo_3d->view = AV_STEREO3D_VIEW_UNSPEC;
|
|
|
|
}
|
2024-06-12 10:56:15 +02:00
|
|
|
}
|
|
|
|
|
2022-08-13 21:59:02 +02:00
|
|
|
ret = ff_progress_frame_get_buffer(s->avctx, &frame->tf,
|
|
|
|
AV_GET_BUFFER_FLAG_REF);
|
2013-10-12 12:55:48 +03:00
|
|
|
if (ret < 0)
|
|
|
|
return NULL;
|
|
|
|
|
2022-08-04 17:33:29 +02:00
|
|
|
frame->rpl = ff_refstruct_allocz(s->pkt.nb_nals * sizeof(*frame->rpl));
|
|
|
|
if (!frame->rpl)
|
2013-10-12 12:55:48 +03:00
|
|
|
goto fail;
|
2022-08-04 17:33:29 +02:00
|
|
|
frame->nb_rpl_elems = s->pkt.nb_nals;
|
2013-10-12 12:55:48 +03:00
|
|
|
|
2024-06-05 09:01:16 +02:00
|
|
|
frame->tab_mvf = ff_refstruct_pool_get(l->tab_mvf_pool);
|
2022-08-05 22:57:56 +02:00
|
|
|
if (!frame->tab_mvf)
|
2013-10-12 12:55:48 +03:00
|
|
|
goto fail;
|
|
|
|
|
2024-06-05 09:01:16 +02:00
|
|
|
frame->rpl_tab = ff_refstruct_pool_get(l->rpl_tab_pool);
|
2022-08-05 22:57:56 +02:00
|
|
|
if (!frame->rpl_tab)
|
2013-10-12 12:55:48 +03:00
|
|
|
goto fail;
|
2024-06-05 16:37:30 +02:00
|
|
|
frame->ctb_count = l->sps->ctb_width * l->sps->ctb_height;
|
2013-10-12 12:55:48 +03:00
|
|
|
for (j = 0; j < frame->ctb_count; j++)
|
2022-08-04 17:33:29 +02:00
|
|
|
frame->rpl_tab[j] = frame->rpl;
|
2013-10-12 12:55:48 +03:00
|
|
|
|
2023-04-11 20:02:14 +02:00
|
|
|
if (s->sei.picture_timing.picture_struct == AV_PICTURE_STRUCTURE_TOP_FIELD)
|
2024-05-29 08:38:27 +02:00
|
|
|
frame->f->flags |= AV_FRAME_FLAG_TOP_FIELD_FIRST;
|
2023-04-11 20:02:14 +02:00
|
|
|
if ((s->sei.picture_timing.picture_struct == AV_PICTURE_STRUCTURE_TOP_FIELD) ||
|
|
|
|
(s->sei.picture_timing.picture_struct == AV_PICTURE_STRUCTURE_BOTTOM_FIELD))
|
2024-05-29 08:38:27 +02:00
|
|
|
frame->f->flags |= AV_FRAME_FLAG_INTERLACED;
|
2015-01-25 14:01:09 +02:00
|
|
|
|
2022-08-04 16:21:03 +02:00
|
|
|
ret = ff_hwaccel_frame_priv_alloc(s->avctx, &frame->hwaccel_picture_private);
|
2023-08-01 21:02:13 +02:00
|
|
|
if (ret < 0)
|
|
|
|
goto fail;
|
2015-01-25 14:01:09 +02:00
|
|
|
|
2024-06-05 12:02:27 +02:00
|
|
|
frame->pps = ff_refstruct_ref_c(s->pps);
|
|
|
|
|
2013-10-12 12:55:48 +03:00
|
|
|
return frame;
|
|
|
|
fail:
|
2023-10-12 16:52:49 +02:00
|
|
|
ff_hevc_unref_frame(frame, ~0);
|
2013-10-12 12:55:48 +03:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
av_log(s->avctx, AV_LOG_ERROR, "Error allocating frame, DPB full.\n");
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2024-06-05 07:14:06 +02:00
|
|
|
int ff_hevc_set_new_ref(HEVCContext *s, HEVCLayerContext *l, int poc)
|
2013-10-12 12:55:48 +03:00
|
|
|
{
|
|
|
|
HEVCFrame *ref;
|
|
|
|
int i;
|
|
|
|
|
|
|
|
/* check that this POC doesn't already exist */
|
2024-06-05 07:14:06 +02:00
|
|
|
for (i = 0; i < FF_ARRAY_ELEMS(l->DPB); i++) {
|
|
|
|
HEVCFrame *frame = &l->DPB[i];
|
2013-10-12 12:55:48 +03:00
|
|
|
|
2024-06-21 12:03:40 +02:00
|
|
|
if (frame->f && frame->poc == poc) {
|
2013-10-12 12:55:48 +03:00
|
|
|
av_log(s->avctx, AV_LOG_ERROR, "Duplicate POC in a sequence: %d.\n",
|
|
|
|
poc);
|
|
|
|
return AVERROR_INVALIDDATA;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-06-05 07:14:06 +02:00
|
|
|
ref = alloc_frame(s, l);
|
2013-10-12 12:55:48 +03:00
|
|
|
if (!ref)
|
|
|
|
return AVERROR(ENOMEM);
|
|
|
|
|
2024-05-29 08:38:27 +02:00
|
|
|
s->cur_frame = ref;
|
2024-06-12 11:26:13 +02:00
|
|
|
l->cur_frame = ref;
|
2022-08-22 20:33:32 +02:00
|
|
|
s->collocated_ref = NULL;
|
2013-10-12 12:55:48 +03:00
|
|
|
|
2024-06-12 10:56:15 +02:00
|
|
|
ref->base_layer_frame = (l != &s->layers[0] && s->layers[0].cur_frame) ?
|
|
|
|
s->layers[0].cur_frame - s->layers[0].DPB : -1;
|
|
|
|
|
2014-07-04 18:21:56 +03:00
|
|
|
if (s->sh.pic_output_flag)
|
|
|
|
ref->flags = HEVC_FRAME_FLAG_OUTPUT | HEVC_FRAME_FLAG_SHORT_REF;
|
|
|
|
else
|
|
|
|
ref->flags = HEVC_FRAME_FLAG_SHORT_REF;
|
|
|
|
|
2013-11-02 02:31:49 +03:00
|
|
|
ref->poc = poc;
|
2024-06-05 16:37:30 +02:00
|
|
|
ref->f->crop_left = l->sps->output_window.left_offset;
|
|
|
|
ref->f->crop_right = l->sps->output_window.right_offset;
|
|
|
|
ref->f->crop_top = l->sps->output_window.top_offset;
|
|
|
|
ref->f->crop_bottom = l->sps->output_window.bottom_offset;
|
2013-10-12 12:55:48 +03:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2024-06-05 07:14:06 +02:00
|
|
|
static void unref_missing_refs(HEVCLayerContext *l)
|
2022-07-15 07:06:42 +02:00
|
|
|
{
|
2024-06-05 07:14:06 +02:00
|
|
|
for (int i = 0; i < FF_ARRAY_ELEMS(l->DPB); i++) {
|
|
|
|
HEVCFrame *frame = &l->DPB[i];
|
2024-06-21 12:03:40 +02:00
|
|
|
if (frame->flags & HEVC_FRAME_FLAG_UNAVAILABLE) {
|
2023-10-12 16:52:49 +02:00
|
|
|
ff_hevc_unref_frame(frame, ~0);
|
2022-07-15 07:06:42 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-06-12 10:56:15 +02:00
|
|
|
int ff_hevc_output_frames(HEVCContext *s,
|
|
|
|
unsigned layers_active_decode, unsigned layers_active_output,
|
2024-06-05 07:14:06 +02:00
|
|
|
unsigned max_output, unsigned max_dpb, int discard)
|
2013-10-12 12:55:48 +03:00
|
|
|
{
|
2024-06-21 12:03:40 +02:00
|
|
|
while (1) {
|
2024-06-12 10:56:15 +02:00
|
|
|
int nb_dpb[HEVC_VPS_MAX_LAYERS] = { 0 };
|
2013-10-27 14:56:48 +03:00
|
|
|
int nb_output = 0;
|
|
|
|
int min_poc = INT_MAX;
|
2024-06-12 10:56:15 +02:00
|
|
|
int min_layer = -1;
|
|
|
|
int min_idx, ret = 0;
|
|
|
|
|
|
|
|
for (int layer = 0; layer < FF_ARRAY_ELEMS(s->layers); layer++) {
|
|
|
|
HEVCLayerContext *l = &s->layers[layer];
|
|
|
|
|
|
|
|
if (!(layers_active_decode & (1 << layer)))
|
|
|
|
continue;
|
|
|
|
|
|
|
|
for (int i = 0; i < FF_ARRAY_ELEMS(l->DPB); i++) {
|
|
|
|
HEVCFrame *frame = &l->DPB[i];
|
|
|
|
if (frame->flags & HEVC_FRAME_FLAG_OUTPUT) {
|
|
|
|
// nb_output counts AUs with an output-pending frame
|
|
|
|
// in at least one layer
|
|
|
|
if (!(frame->base_layer_frame >= 0 &&
|
|
|
|
(s->layers[0].DPB[frame->base_layer_frame].flags & HEVC_FRAME_FLAG_OUTPUT)))
|
|
|
|
nb_output++;
|
|
|
|
if (min_layer < 0 || frame->poc < min_poc) {
|
|
|
|
min_poc = frame->poc;
|
|
|
|
min_idx = i;
|
|
|
|
min_layer = layer;
|
|
|
|
}
|
2013-10-12 12:55:48 +03:00
|
|
|
}
|
2024-06-12 10:56:15 +02:00
|
|
|
nb_dpb[layer] += !!frame->flags;
|
2013-10-12 12:55:48 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-06-21 12:03:40 +02:00
|
|
|
if (nb_output > max_output ||
|
2024-06-12 10:56:15 +02:00
|
|
|
(nb_output &&
|
|
|
|
(nb_dpb[0] > max_dpb || nb_dpb[1] > max_dpb))) {
|
|
|
|
HEVCFrame *frame = &s->layers[min_layer].DPB[min_idx];
|
2024-09-11 16:37:07 +02:00
|
|
|
AVFrame *f = frame->needs_fg ? frame->frame_grain : frame->f;
|
2024-06-12 10:56:15 +02:00
|
|
|
int output = !discard && (layers_active_output & (1 << min_layer));
|
2013-10-12 12:55:48 +03:00
|
|
|
|
2024-06-12 10:56:15 +02:00
|
|
|
if (output) {
|
2024-09-11 16:37:07 +02:00
|
|
|
f->pkt_dts = s->pkt_dts;
|
|
|
|
ret = ff_container_fifo_write(s->output_fifo, f);
|
|
|
|
}
|
2024-06-21 12:03:40 +02:00
|
|
|
ff_hevc_unref_frame(frame, HEVC_FRAME_FLAG_OUTPUT);
|
2013-10-12 12:55:48 +03:00
|
|
|
if (ret < 0)
|
|
|
|
return ret;
|
|
|
|
|
2024-06-12 10:56:15 +02:00
|
|
|
av_log(s->avctx, AV_LOG_DEBUG, "%s frame with POC %d/%d.\n",
|
|
|
|
output ? "Output" : "Discarded", min_layer, frame->poc);
|
2024-06-21 12:03:40 +02:00
|
|
|
continue;
|
2014-07-26 15:27:56 +03:00
|
|
|
}
|
2024-06-21 12:03:40 +02:00
|
|
|
return 0;
|
2014-07-26 15:27:56 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-10-12 12:55:48 +03:00
|
|
|
static int init_slice_rpl(HEVCContext *s)
|
|
|
|
{
|
2024-05-29 08:38:27 +02:00
|
|
|
HEVCFrame *frame = s->cur_frame;
|
2013-11-02 02:31:49 +03:00
|
|
|
int ctb_count = frame->ctb_count;
|
2024-06-01 10:36:00 +02:00
|
|
|
int ctb_addr_ts = s->pps->ctb_addr_rs_to_ts[s->sh.slice_segment_addr];
|
2013-10-12 12:55:48 +03:00
|
|
|
int i;
|
|
|
|
|
2022-08-04 17:33:29 +02:00
|
|
|
if (s->slice_idx >= frame->nb_rpl_elems)
|
2013-10-12 12:55:48 +03:00
|
|
|
return AVERROR_INVALIDDATA;
|
|
|
|
|
|
|
|
for (i = ctb_addr_ts; i < ctb_count; i++)
|
2022-08-04 17:33:29 +02:00
|
|
|
frame->rpl_tab[i] = frame->rpl + s->slice_idx;
|
2013-10-12 12:55:48 +03:00
|
|
|
|
2013-11-02 02:31:49 +03:00
|
|
|
frame->refPicList = (RefPicList *)frame->rpl_tab[ctb_addr_ts];
|
2013-10-12 12:55:48 +03:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int ff_hevc_slice_rpl(HEVCContext *s)
|
|
|
|
{
|
|
|
|
SliceHeader *sh = &s->sh;
|
|
|
|
|
2016-10-18 21:14:18 +02:00
|
|
|
uint8_t nb_list = sh->slice_type == HEVC_SLICE_B ? 2 : 1;
|
2013-10-12 12:55:48 +03:00
|
|
|
uint8_t list_idx;
|
|
|
|
int i, j, ret;
|
|
|
|
|
|
|
|
ret = init_slice_rpl(s);
|
|
|
|
if (ret < 0)
|
|
|
|
return ret;
|
|
|
|
|
|
|
|
if (!(s->rps[ST_CURR_BEF].nb_refs + s->rps[ST_CURR_AFT].nb_refs +
|
2024-06-12 11:26:13 +02:00
|
|
|
s->rps[LT_CURR].nb_refs +
|
|
|
|
s->rps[INTER_LAYER0].nb_refs + s->rps[INTER_LAYER1].nb_refs) &&
|
|
|
|
!s->pps->pps_curr_pic_ref_enabled_flag) {
|
2013-10-12 12:55:48 +03:00
|
|
|
av_log(s->avctx, AV_LOG_ERROR, "Zero refs in the frame RPS.\n");
|
|
|
|
return AVERROR_INVALIDDATA;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (list_idx = 0; list_idx < nb_list; list_idx++) {
|
|
|
|
RefPicList rpl_tmp = { { 0 } };
|
2024-05-29 08:38:27 +02:00
|
|
|
RefPicList *rpl = &s->cur_frame->refPicList[list_idx];
|
2013-10-12 12:55:48 +03:00
|
|
|
|
|
|
|
/* The order of the elements is
|
2024-06-12 11:26:13 +02:00
|
|
|
* ST_CURR_BEF - INTER_LAYER0 - ST_CURR_AFT - LT_CURR - INTER_LAYER1 for the L0 and
|
|
|
|
* ST_CURR_AFT - INTER_LAYER1 - ST_CURR_BEF - LT_CURR - INTER_LAYER0 for the L1 */
|
|
|
|
int cand_lists[] = { list_idx ? ST_CURR_AFT : ST_CURR_BEF,
|
|
|
|
list_idx ? INTER_LAYER1 : INTER_LAYER0,
|
|
|
|
list_idx ? ST_CURR_BEF : ST_CURR_AFT,
|
|
|
|
LT_CURR,
|
|
|
|
list_idx ? INTER_LAYER0 : INTER_LAYER1
|
|
|
|
};
|
2013-10-12 12:55:48 +03:00
|
|
|
|
|
|
|
/* concatenate the candidate lists for the current frame */
|
|
|
|
while (rpl_tmp.nb_refs < sh->nb_refs[list_idx]) {
|
|
|
|
for (i = 0; i < FF_ARRAY_ELEMS(cand_lists); i++) {
|
|
|
|
RefPicList *rps = &s->rps[cand_lists[i]];
|
2016-08-21 18:02:02 +02:00
|
|
|
for (j = 0; j < rps->nb_refs && rpl_tmp.nb_refs < HEVC_MAX_REFS; j++) {
|
2013-11-02 02:31:49 +03:00
|
|
|
rpl_tmp.list[rpl_tmp.nb_refs] = rps->list[j];
|
|
|
|
rpl_tmp.ref[rpl_tmp.nb_refs] = rps->ref[j];
|
2024-06-12 11:26:13 +02:00
|
|
|
// multiview inter-layer refs are treated as long-term here,
|
|
|
|
// cf. G.8.1.3
|
|
|
|
rpl_tmp.isLongTerm[rpl_tmp.nb_refs] = cand_lists[i] == LT_CURR ||
|
|
|
|
cand_lists[i] == INTER_LAYER0 ||
|
|
|
|
cand_lists[i] == INTER_LAYER1;
|
2013-10-12 12:55:48 +03:00
|
|
|
rpl_tmp.nb_refs++;
|
|
|
|
}
|
|
|
|
}
|
2023-02-16 07:46:33 +02:00
|
|
|
// Construct RefPicList0, RefPicList1 (8-8, 8-10)
|
2024-06-01 10:36:00 +02:00
|
|
|
if (s->pps->pps_curr_pic_ref_enabled_flag && rpl_tmp.nb_refs < HEVC_MAX_REFS) {
|
2024-05-29 08:38:27 +02:00
|
|
|
rpl_tmp.list[rpl_tmp.nb_refs] = s->cur_frame->poc;
|
|
|
|
rpl_tmp.ref[rpl_tmp.nb_refs] = s->cur_frame;
|
2023-02-16 07:46:33 +02:00
|
|
|
rpl_tmp.isLongTerm[rpl_tmp.nb_refs] = 1;
|
|
|
|
rpl_tmp.nb_refs++;
|
|
|
|
}
|
2013-10-12 12:55:48 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/* reorder the references if necessary */
|
|
|
|
if (sh->rpl_modification_flag[list_idx]) {
|
|
|
|
for (i = 0; i < sh->nb_refs[list_idx]; i++) {
|
|
|
|
int idx = sh->list_entry_lx[list_idx][i];
|
|
|
|
|
|
|
|
if (idx >= rpl_tmp.nb_refs) {
|
|
|
|
av_log(s->avctx, AV_LOG_ERROR, "Invalid reference index.\n");
|
|
|
|
return AVERROR_INVALIDDATA;
|
|
|
|
}
|
|
|
|
|
|
|
|
rpl->list[i] = rpl_tmp.list[idx];
|
|
|
|
rpl->ref[i] = rpl_tmp.ref[idx];
|
|
|
|
rpl->isLongTerm[i] = rpl_tmp.isLongTerm[idx];
|
|
|
|
rpl->nb_refs++;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
memcpy(rpl, &rpl_tmp, sizeof(*rpl));
|
|
|
|
rpl->nb_refs = FFMIN(rpl->nb_refs, sh->nb_refs[list_idx]);
|
|
|
|
}
|
|
|
|
|
2023-02-16 07:46:33 +02:00
|
|
|
// 8-9
|
2024-06-01 10:36:00 +02:00
|
|
|
if (s->pps->pps_curr_pic_ref_enabled_flag &&
|
2023-02-16 07:46:33 +02:00
|
|
|
!sh->rpl_modification_flag[list_idx] &&
|
|
|
|
rpl_tmp.nb_refs > sh->nb_refs[L0]) {
|
2024-05-29 08:38:27 +02:00
|
|
|
rpl->list[sh->nb_refs[L0] - 1] = s->cur_frame->poc;
|
|
|
|
rpl->ref[sh->nb_refs[L0] - 1] = s->cur_frame;
|
2023-02-16 07:46:33 +02:00
|
|
|
}
|
|
|
|
|
2013-10-12 12:55:48 +03:00
|
|
|
if (sh->collocated_list == list_idx &&
|
|
|
|
sh->collocated_ref_idx < rpl->nb_refs)
|
2022-08-22 20:33:32 +02:00
|
|
|
s->collocated_ref = rpl->ref[sh->collocated_ref_idx];
|
2013-10-12 12:55:48 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2024-06-05 07:14:06 +02:00
|
|
|
static HEVCFrame *find_ref_idx(HEVCContext *s, HEVCLayerContext *l,
|
|
|
|
int poc, uint8_t use_msb)
|
2013-10-12 12:55:48 +03:00
|
|
|
{
|
2024-06-05 16:37:30 +02:00
|
|
|
int mask = use_msb ? ~0 : (1 << l->sps->log2_max_poc_lsb) - 1;
|
2013-10-12 12:55:48 +03:00
|
|
|
int i;
|
|
|
|
|
2024-06-05 07:14:06 +02:00
|
|
|
for (i = 0; i < FF_ARRAY_ELEMS(l->DPB); i++) {
|
|
|
|
HEVCFrame *ref = &l->DPB[i];
|
2024-06-21 12:03:40 +02:00
|
|
|
if (ref->f) {
|
2022-07-15 07:06:43 +02:00
|
|
|
if ((ref->poc & mask) == poc && (use_msb || ref->poc != s->poc))
|
2020-05-27 10:46:50 +02:00
|
|
|
return ref;
|
2013-10-27 14:56:48 +03:00
|
|
|
}
|
2013-10-12 12:55:48 +03:00
|
|
|
}
|
|
|
|
|
2017-03-23 19:27:48 +02:00
|
|
|
if (s->nal_unit_type != HEVC_NAL_CRA_NUT && !IS_BLA(s))
|
2015-03-15 20:42:48 +02:00
|
|
|
av_log(s->avctx, AV_LOG_ERROR,
|
|
|
|
"Could not find ref with POC %d\n", poc);
|
2013-10-12 12:55:48 +03:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void mark_ref(HEVCFrame *frame, int flag)
|
|
|
|
{
|
|
|
|
frame->flags &= ~(HEVC_FRAME_FLAG_LONG_REF | HEVC_FRAME_FLAG_SHORT_REF);
|
|
|
|
frame->flags |= flag;
|
|
|
|
}
|
|
|
|
|
2024-06-05 07:14:06 +02:00
|
|
|
static HEVCFrame *generate_missing_ref(HEVCContext *s, HEVCLayerContext *l, int poc)
|
2013-10-12 12:55:48 +03:00
|
|
|
{
|
|
|
|
HEVCFrame *frame;
|
2019-08-06 18:05:02 +02:00
|
|
|
int i, y;
|
2013-10-12 12:55:48 +03:00
|
|
|
|
2024-06-05 07:14:06 +02:00
|
|
|
frame = alloc_frame(s, l);
|
2013-10-12 12:55:48 +03:00
|
|
|
if (!frame)
|
|
|
|
return NULL;
|
|
|
|
|
2015-01-25 14:01:09 +02:00
|
|
|
if (!s->avctx->hwaccel) {
|
2024-06-05 16:37:30 +02:00
|
|
|
if (!l->sps->pixel_shift) {
|
2024-05-29 08:38:27 +02:00
|
|
|
for (i = 0; frame->f->data[i]; i++)
|
2024-06-05 16:37:30 +02:00
|
|
|
memset(frame->f->data[i], 1 << (l->sps->bit_depth - 1),
|
|
|
|
frame->f->linesize[i] * AV_CEIL_RSHIFT(l->sps->height, l->sps->vshift[i]));
|
2015-01-25 14:01:10 +02:00
|
|
|
} else {
|
2024-05-29 08:38:27 +02:00
|
|
|
for (i = 0; frame->f->data[i]; i++)
|
2024-06-05 16:37:30 +02:00
|
|
|
for (y = 0; y < (l->sps->height >> l->sps->vshift[i]); y++) {
|
2024-05-29 08:38:27 +02:00
|
|
|
uint8_t *dst = frame->f->data[i] + y * frame->f->linesize[i];
|
2024-06-05 16:37:30 +02:00
|
|
|
AV_WN16(dst, 1 << (l->sps->bit_depth - 1));
|
|
|
|
av_memcpy_backptr(dst + 2, 2, 2*(l->sps->width >> l->sps->hshift[i]) - 2);
|
2019-08-06 18:05:02 +02:00
|
|
|
}
|
2015-01-25 14:01:10 +02:00
|
|
|
}
|
2015-01-25 14:01:09 +02:00
|
|
|
}
|
2013-10-12 12:55:48 +03:00
|
|
|
|
|
|
|
frame->poc = poc;
|
2024-06-21 12:03:40 +02:00
|
|
|
frame->flags = HEVC_FRAME_FLAG_UNAVAILABLE;
|
2013-10-12 12:55:48 +03:00
|
|
|
|
2024-06-01 20:21:06 +02:00
|
|
|
if (s->avctx->active_thread_type == FF_THREAD_FRAME)
|
2022-08-13 21:59:02 +02:00
|
|
|
ff_progress_frame_report(&frame->tf, INT_MAX);
|
2013-10-12 12:55:48 +03:00
|
|
|
|
|
|
|
return frame;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* add a reference with the given poc to the list and mark it as used in DPB */
|
2024-06-05 07:14:06 +02:00
|
|
|
static int add_candidate_ref(HEVCContext *s, HEVCLayerContext *l,
|
|
|
|
RefPicList *list,
|
2020-05-12 15:44:21 +02:00
|
|
|
int poc, int ref_flag, uint8_t use_msb)
|
2013-10-12 12:55:48 +03:00
|
|
|
{
|
2024-06-05 07:14:06 +02:00
|
|
|
HEVCFrame *ref = find_ref_idx(s, l, poc, use_msb);
|
2013-10-12 12:55:48 +03:00
|
|
|
|
2024-05-29 08:38:27 +02:00
|
|
|
if (ref == s->cur_frame || list->nb_refs >= HEVC_MAX_REFS)
|
2013-10-12 12:55:48 +03:00
|
|
|
return AVERROR_INVALIDDATA;
|
|
|
|
|
|
|
|
if (!ref) {
|
2024-06-05 07:14:06 +02:00
|
|
|
ref = generate_missing_ref(s, l, poc);
|
2013-10-12 12:55:48 +03:00
|
|
|
if (!ref)
|
|
|
|
return AVERROR(ENOMEM);
|
|
|
|
}
|
|
|
|
|
|
|
|
list->list[list->nb_refs] = ref->poc;
|
|
|
|
list->ref[list->nb_refs] = ref;
|
|
|
|
list->nb_refs++;
|
|
|
|
|
|
|
|
mark_ref(ref, ref_flag);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2024-06-05 07:14:06 +02:00
|
|
|
int ff_hevc_frame_rps(HEVCContext *s, HEVCLayerContext *l)
|
2013-10-12 12:55:48 +03:00
|
|
|
{
|
|
|
|
const ShortTermRPS *short_rps = s->sh.short_term_rps;
|
|
|
|
const LongTermRPS *long_rps = &s->sh.long_term_rps;
|
|
|
|
RefPicList *rps = s->rps;
|
2014-11-20 10:26:35 +02:00
|
|
|
int i, ret = 0;
|
2013-10-12 12:55:48 +03:00
|
|
|
|
2024-06-05 07:14:06 +02:00
|
|
|
unref_missing_refs(l);
|
2022-07-15 07:06:42 +02:00
|
|
|
|
2013-10-12 12:55:48 +03:00
|
|
|
/* clear the reference flags on all frames except the current one */
|
2024-06-05 07:14:06 +02:00
|
|
|
for (i = 0; i < FF_ARRAY_ELEMS(l->DPB); i++) {
|
|
|
|
HEVCFrame *frame = &l->DPB[i];
|
2013-10-12 12:55:48 +03:00
|
|
|
|
2024-05-29 08:38:27 +02:00
|
|
|
if (frame == s->cur_frame)
|
2013-10-12 12:55:48 +03:00
|
|
|
continue;
|
|
|
|
|
|
|
|
mark_ref(frame, 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
for (i = 0; i < NB_RPS_TYPE; i++)
|
|
|
|
rps[i].nb_refs = 0;
|
|
|
|
|
2024-06-12 11:26:13 +02:00
|
|
|
if (!short_rps)
|
|
|
|
goto inter_layer;
|
|
|
|
|
2013-10-12 12:55:48 +03:00
|
|
|
/* add the short refs */
|
|
|
|
for (i = 0; i < short_rps->num_delta_pocs; i++) {
|
|
|
|
int poc = s->poc + short_rps->delta_poc[i];
|
|
|
|
int list;
|
|
|
|
|
2024-04-10 11:27:51 +02:00
|
|
|
if (!(short_rps->used & (1 << i)))
|
2013-10-12 12:55:48 +03:00
|
|
|
list = ST_FOLL;
|
|
|
|
else if (i < short_rps->num_negative_pics)
|
|
|
|
list = ST_CURR_BEF;
|
|
|
|
else
|
|
|
|
list = ST_CURR_AFT;
|
|
|
|
|
2024-06-05 07:14:06 +02:00
|
|
|
ret = add_candidate_ref(s, l, &rps[list], poc,
|
|
|
|
HEVC_FRAME_FLAG_SHORT_REF, 1);
|
2013-10-12 12:55:48 +03:00
|
|
|
if (ret < 0)
|
2014-11-20 10:26:35 +02:00
|
|
|
goto fail;
|
2013-10-12 12:55:48 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/* add the long refs */
|
|
|
|
for (i = 0; i < long_rps->nb_refs; i++) {
|
|
|
|
int poc = long_rps->poc[i];
|
|
|
|
int list = long_rps->used[i] ? LT_CURR : LT_FOLL;
|
|
|
|
|
2024-06-05 07:14:06 +02:00
|
|
|
ret = add_candidate_ref(s, l, &rps[list], poc,
|
|
|
|
HEVC_FRAME_FLAG_LONG_REF, long_rps->poc_msb_present[i]);
|
2013-10-12 12:55:48 +03:00
|
|
|
if (ret < 0)
|
2014-11-20 10:26:35 +02:00
|
|
|
goto fail;
|
2013-10-12 12:55:48 +03:00
|
|
|
}
|
|
|
|
|
2024-06-12 11:26:13 +02:00
|
|
|
inter_layer:
|
|
|
|
/* add inter-layer refs */
|
|
|
|
if (s->sh.inter_layer_pred) {
|
|
|
|
HEVCLayerContext *l0 = &s->layers[0];
|
|
|
|
|
|
|
|
av_assert0(l != l0);
|
|
|
|
|
|
|
|
/* Given the assumption of at most two layers, refPicSet0Flag is
|
|
|
|
* always 1, so only RefPicSetInterLayer0 can ever contain a frame. */
|
|
|
|
if (l0->cur_frame) {
|
|
|
|
// inter-layer refs are treated as short-term here, cf. F.8.1.6
|
|
|
|
ret = add_candidate_ref(s, l0, &rps[INTER_LAYER0], l0->cur_frame->poc,
|
|
|
|
HEVC_FRAME_FLAG_SHORT_REF, 1);
|
|
|
|
if (ret < 0)
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-11-20 10:26:35 +02:00
|
|
|
fail:
|
2013-10-12 12:55:48 +03:00
|
|
|
/* release any frames that are now unused */
|
2024-06-05 07:14:06 +02:00
|
|
|
for (i = 0; i < FF_ARRAY_ELEMS(l->DPB); i++)
|
|
|
|
ff_hevc_unref_frame(&l->DPB[i], 0);
|
2013-10-12 12:55:48 +03:00
|
|
|
|
2014-11-20 10:26:35 +02:00
|
|
|
return ret;
|
2013-10-12 12:55:48 +03:00
|
|
|
}
|
|
|
|
|
2024-06-12 11:26:13 +02:00
|
|
|
int ff_hevc_frame_nb_refs(const SliceHeader *sh, const HEVCPPS *pps,
|
|
|
|
unsigned layer_idx)
|
2013-10-12 12:55:48 +03:00
|
|
|
{
|
|
|
|
int ret = 0;
|
|
|
|
int i;
|
2024-06-01 16:36:16 +02:00
|
|
|
const ShortTermRPS *rps = sh->short_term_rps;
|
|
|
|
const LongTermRPS *long_rps = &sh->long_term_rps;
|
2013-10-12 12:55:48 +03:00
|
|
|
|
|
|
|
if (rps) {
|
|
|
|
for (i = 0; i < rps->num_negative_pics; i++)
|
2024-04-10 11:27:51 +02:00
|
|
|
ret += !!(rps->used & (1 << i));
|
2013-10-12 12:55:48 +03:00
|
|
|
for (; i < rps->num_delta_pocs; i++)
|
2024-04-10 11:27:51 +02:00
|
|
|
ret += !!(rps->used & (1 << i));
|
2013-10-12 12:55:48 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
if (long_rps) {
|
|
|
|
for (i = 0; i < long_rps->nb_refs; i++)
|
|
|
|
ret += !!long_rps->used[i];
|
|
|
|
}
|
2023-02-16 07:46:33 +02:00
|
|
|
|
2024-06-12 11:26:13 +02:00
|
|
|
if (sh->inter_layer_pred) {
|
|
|
|
av_assert0(pps->sps->vps->num_direct_ref_layers[layer_idx] < 2);
|
|
|
|
ret++;
|
|
|
|
}
|
|
|
|
|
2024-06-01 16:36:16 +02:00
|
|
|
if (pps->pps_curr_pic_ref_enabled_flag)
|
2023-02-16 07:46:33 +02:00
|
|
|
ret++;
|
|
|
|
|
2013-10-12 12:55:48 +03:00
|
|
|
return ret;
|
|
|
|
}
|