2020-10-05 14:16:25 +02:00
|
|
|
/*
|
2022-05-02 03:37:49 +02:00
|
|
|
* AVS3-P2/IEEE1857.10 video decoder (using the uavs3d library)
|
2020-10-05 14:16:25 +02:00
|
|
|
* Copyright (c) 2020 Zhenyu Wang <wangzhenyu@pkusz.edu.cn>
|
|
|
|
* Bingjie Han <hanbj@pkusz.edu.cn>
|
|
|
|
* Huiwen Ren <hwrenx@gmail.com>
|
|
|
|
*
|
|
|
|
* 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
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "libavutil/avutil.h"
|
|
|
|
#include "libavutil/common.h"
|
2021-06-12 22:10:11 +02:00
|
|
|
#include "libavutil/cpu.h"
|
2020-10-05 14:16:25 +02:00
|
|
|
#include "libavutil/imgutils.h"
|
|
|
|
#include "libavutil/intreadwrite.h"
|
|
|
|
#include "libavutil/opt.h"
|
|
|
|
#include "avcodec.h"
|
|
|
|
#include "avs3.h"
|
2022-03-16 19:18:28 +02:00
|
|
|
#include "codec_internal.h"
|
2022-08-24 21:28:16 +02:00
|
|
|
#include "decode.h"
|
2020-10-05 14:16:25 +02:00
|
|
|
#include "uavs3d.h"
|
|
|
|
|
|
|
|
typedef struct uavs3d_context {
|
|
|
|
AVCodecContext *avctx;
|
|
|
|
void *dec_handle;
|
|
|
|
int frame_threads;
|
|
|
|
int got_seqhdr;
|
|
|
|
uavs3d_io_frm_t dec_frame;
|
|
|
|
} uavs3d_context;
|
|
|
|
|
|
|
|
#define UAVS3D_CHECK_START_CODE(data_ptr, PIC_START_CODE) \
|
|
|
|
(AV_RL32(data_ptr) != (PIC_START_CODE << 24) + AVS3_NAL_START_CODE)
|
|
|
|
static int uavs3d_find_next_start_code(const unsigned char *bs_data, int bs_len, int *left)
|
|
|
|
{
|
|
|
|
const unsigned char *data_ptr = bs_data + 4;
|
|
|
|
int count = bs_len - 4;
|
|
|
|
|
|
|
|
while (count >= 4 &&
|
|
|
|
UAVS3D_CHECK_START_CODE(data_ptr, AVS3_INTER_PIC_START_CODE) &&
|
|
|
|
UAVS3D_CHECK_START_CODE(data_ptr, AVS3_INTRA_PIC_START_CODE) &&
|
|
|
|
UAVS3D_CHECK_START_CODE(data_ptr, AVS3_SEQ_START_CODE) &&
|
|
|
|
UAVS3D_CHECK_START_CODE(data_ptr, AVS3_FIRST_SLICE_START_CODE) &&
|
|
|
|
UAVS3D_CHECK_START_CODE(data_ptr, AVS3_SEQ_END_CODE)) {
|
|
|
|
data_ptr++;
|
|
|
|
count--;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (count >= 4) {
|
|
|
|
*left = count;
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void uavs3d_output_callback(uavs3d_io_frm_t *dec_frame) {
|
|
|
|
uavs3d_io_frm_t frm_out;
|
|
|
|
AVFrame *frm = (AVFrame *)dec_frame->priv;
|
|
|
|
int i;
|
|
|
|
|
|
|
|
if (!frm || !frm->data[0]) {
|
|
|
|
dec_frame->got_pic = 0;
|
|
|
|
av_log(NULL, AV_LOG_ERROR, "Invalid AVFrame in uavs3d output.\n");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
frm->pts = dec_frame->pts;
|
|
|
|
frm->pkt_dts = dec_frame->dts;
|
lavu/frame: deprecate AVFrame.pkt_{pos,size}
These fields are supposed to store information about the packet the
frame was decoded from, specifically the byte offset it was stored at
and its size.
However,
- the fields are highly ad-hoc - there is no strong reason why
specifically those (and not any other) packet properties should have a
dedicated field in AVFrame; unlike e.g. the timestamps, there is no
fundamental link between coded packet offset/size and decoded frames
- they only make sense for frames produced by decoding demuxed packets,
and even then it is not always the case that the encoded data was
stored in the file as a contiguous sequence of bytes (in order for pos
to be well-defined)
- pkt_pos was added without much explanation, apparently to allow
passthrough of this information through lavfi in order to handle byte
seeking in ffplay. That is now implemented using arbitrary user data
passthrough in AVFrame.opaque_ref.
- several filters use pkt_pos as a variable available to user-supplied
expressions, but there seems to be no established motivation for using them.
- pkt_size was added for use in ffprobe, but that too is now handled
without using this field. Additonally, the values of this field
produced by libavcodec are flawed, as described in the previous
ffprobe conversion commit.
In summary - these fields are ill-defined and insufficiently motivated,
so deprecate them.
2023-03-10 11:48:34 +02:00
|
|
|
#if FF_API_FRAME_PKT
|
|
|
|
FF_DISABLE_DEPRECATION_WARNINGS
|
2020-10-05 14:16:25 +02:00
|
|
|
frm->pkt_pos = dec_frame->pkt_pos;
|
|
|
|
frm->pkt_size = dec_frame->pkt_size;
|
lavu/frame: deprecate AVFrame.pkt_{pos,size}
These fields are supposed to store information about the packet the
frame was decoded from, specifically the byte offset it was stored at
and its size.
However,
- the fields are highly ad-hoc - there is no strong reason why
specifically those (and not any other) packet properties should have a
dedicated field in AVFrame; unlike e.g. the timestamps, there is no
fundamental link between coded packet offset/size and decoded frames
- they only make sense for frames produced by decoding demuxed packets,
and even then it is not always the case that the encoded data was
stored in the file as a contiguous sequence of bytes (in order for pos
to be well-defined)
- pkt_pos was added without much explanation, apparently to allow
passthrough of this information through lavfi in order to handle byte
seeking in ffplay. That is now implemented using arbitrary user data
passthrough in AVFrame.opaque_ref.
- several filters use pkt_pos as a variable available to user-supplied
expressions, but there seems to be no established motivation for using them.
- pkt_size was added for use in ffprobe, but that too is now handled
without using this field. Additonally, the values of this field
produced by libavcodec are flawed, as described in the previous
ffprobe conversion commit.
In summary - these fields are ill-defined and insufficiently motivated,
so deprecate them.
2023-03-10 11:48:34 +02:00
|
|
|
FF_ENABLE_DEPRECATION_WARNINGS
|
2023-01-28 19:36:22 +02:00
|
|
|
#endif
|
2020-10-05 14:16:25 +02:00
|
|
|
|
2022-06-24 09:49:11 +02:00
|
|
|
if (dec_frame->type < 0 || dec_frame->type >= FF_ARRAY_ELEMS(ff_avs3_image_type)) {
|
2020-10-05 14:16:25 +02:00
|
|
|
av_log(NULL, AV_LOG_WARNING, "Error frame type in uavs3d: %d.\n", dec_frame->type);
|
2022-06-24 09:49:11 +02:00
|
|
|
} else {
|
|
|
|
frm->pict_type = ff_avs3_image_type[dec_frame->type];
|
2023-04-12 18:58:54 +02:00
|
|
|
if (frm->pict_type == AV_PICTURE_TYPE_I)
|
|
|
|
frm->flags |= AV_FRAME_FLAG_KEY;
|
|
|
|
else
|
|
|
|
frm->flags &= ~AV_FRAME_FLAG_KEY;
|
2020-10-05 14:16:25 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
for (i = 0; i < 3; i++) {
|
|
|
|
frm_out.width [i] = dec_frame->width[i];
|
|
|
|
frm_out.height[i] = dec_frame->height[i];
|
|
|
|
frm_out.stride[i] = frm->linesize[i];
|
|
|
|
frm_out.buffer[i] = frm->data[i];
|
|
|
|
}
|
|
|
|
|
|
|
|
uavs3d_img_cpy_cvt(&frm_out, dec_frame, dec_frame->bit_depth);
|
|
|
|
}
|
|
|
|
|
|
|
|
static av_cold int libuavs3d_init(AVCodecContext *avctx)
|
|
|
|
{
|
|
|
|
uavs3d_context *h = avctx->priv_data;
|
|
|
|
uavs3d_cfg_t cdsc;
|
|
|
|
|
|
|
|
cdsc.frm_threads = avctx->thread_count > 0 ? avctx->thread_count : av_cpu_count();
|
|
|
|
cdsc.check_md5 = 0;
|
|
|
|
h->dec_handle = uavs3d_create(&cdsc, uavs3d_output_callback, NULL);
|
|
|
|
h->got_seqhdr = 0;
|
|
|
|
|
|
|
|
if (!h->dec_handle) {
|
|
|
|
return AVERROR(ENOMEM);
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static av_cold int libuavs3d_end(AVCodecContext *avctx)
|
|
|
|
{
|
|
|
|
uavs3d_context *h = avctx->priv_data;
|
|
|
|
|
|
|
|
if (h->dec_handle) {
|
|
|
|
uavs3d_flush(h->dec_handle, NULL);
|
|
|
|
uavs3d_delete(h->dec_handle);
|
|
|
|
h->dec_handle = NULL;
|
|
|
|
}
|
|
|
|
h->got_seqhdr = 0;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void libuavs3d_flush(AVCodecContext * avctx)
|
|
|
|
{
|
|
|
|
uavs3d_context *h = avctx->priv_data;
|
|
|
|
|
|
|
|
if (h->dec_handle) {
|
|
|
|
uavs3d_reset(h->dec_handle);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#define UAVS3D_CHECK_INVALID_RANGE(v, l, r) ((v)<(l)||(v)>(r))
|
2022-03-30 21:33:24 +02:00
|
|
|
static int libuavs3d_decode_frame(AVCodecContext *avctx, AVFrame *frm,
|
|
|
|
int *got_frame, AVPacket *avpkt)
|
2020-10-05 14:16:25 +02:00
|
|
|
{
|
|
|
|
uavs3d_context *h = avctx->priv_data;
|
|
|
|
const uint8_t *buf = avpkt->data;
|
|
|
|
int buf_size = avpkt->size;
|
|
|
|
const uint8_t *buf_end;
|
2022-05-12 11:55:31 +02:00
|
|
|
const uint8_t *buf_ptr = buf;
|
2020-10-05 14:16:25 +02:00
|
|
|
int left_bytes;
|
|
|
|
int ret, finish = 0;
|
|
|
|
|
|
|
|
*got_frame = 0;
|
|
|
|
frm->pts = -1;
|
|
|
|
frm->pict_type = AV_PICTURE_TYPE_NONE;
|
|
|
|
|
|
|
|
if (!buf_size) {
|
|
|
|
if (h->got_seqhdr) {
|
|
|
|
if (!frm->data[0] && (ret = ff_get_buffer(avctx, frm, 0)) < 0) {
|
|
|
|
return ret;
|
|
|
|
}
|
2022-03-30 21:33:24 +02:00
|
|
|
h->dec_frame.priv = frm; // AVFrame
|
2020-10-05 14:16:25 +02:00
|
|
|
}
|
|
|
|
do {
|
|
|
|
ret = uavs3d_flush(h->dec_handle, &h->dec_frame);
|
|
|
|
} while (ret > 0 && !h->dec_frame.got_pic);
|
|
|
|
} else {
|
|
|
|
uavs3d_io_frm_t *frm_dec = &h->dec_frame;
|
|
|
|
|
|
|
|
buf_end = buf + buf_size;
|
lavu/frame: deprecate AVFrame.pkt_{pos,size}
These fields are supposed to store information about the packet the
frame was decoded from, specifically the byte offset it was stored at
and its size.
However,
- the fields are highly ad-hoc - there is no strong reason why
specifically those (and not any other) packet properties should have a
dedicated field in AVFrame; unlike e.g. the timestamps, there is no
fundamental link between coded packet offset/size and decoded frames
- they only make sense for frames produced by decoding demuxed packets,
and even then it is not always the case that the encoded data was
stored in the file as a contiguous sequence of bytes (in order for pos
to be well-defined)
- pkt_pos was added without much explanation, apparently to allow
passthrough of this information through lavfi in order to handle byte
seeking in ffplay. That is now implemented using arbitrary user data
passthrough in AVFrame.opaque_ref.
- several filters use pkt_pos as a variable available to user-supplied
expressions, but there seems to be no established motivation for using them.
- pkt_size was added for use in ffprobe, but that too is now handled
without using this field. Additonally, the values of this field
produced by libavcodec are flawed, as described in the previous
ffprobe conversion commit.
In summary - these fields are ill-defined and insufficiently motivated,
so deprecate them.
2023-03-10 11:48:34 +02:00
|
|
|
#if FF_API_FRAME_PKT
|
|
|
|
FF_DISABLE_DEPRECATION_WARNINGS
|
2020-10-05 14:16:25 +02:00
|
|
|
frm_dec->pkt_pos = avpkt->pos;
|
|
|
|
frm_dec->pkt_size = avpkt->size;
|
lavu/frame: deprecate AVFrame.pkt_{pos,size}
These fields are supposed to store information about the packet the
frame was decoded from, specifically the byte offset it was stored at
and its size.
However,
- the fields are highly ad-hoc - there is no strong reason why
specifically those (and not any other) packet properties should have a
dedicated field in AVFrame; unlike e.g. the timestamps, there is no
fundamental link between coded packet offset/size and decoded frames
- they only make sense for frames produced by decoding demuxed packets,
and even then it is not always the case that the encoded data was
stored in the file as a contiguous sequence of bytes (in order for pos
to be well-defined)
- pkt_pos was added without much explanation, apparently to allow
passthrough of this information through lavfi in order to handle byte
seeking in ffplay. That is now implemented using arbitrary user data
passthrough in AVFrame.opaque_ref.
- several filters use pkt_pos as a variable available to user-supplied
expressions, but there seems to be no established motivation for using them.
- pkt_size was added for use in ffprobe, but that too is now handled
without using this field. Additonally, the values of this field
produced by libavcodec are flawed, as described in the previous
ffprobe conversion commit.
In summary - these fields are ill-defined and insufficiently motivated,
so deprecate them.
2023-03-10 11:48:34 +02:00
|
|
|
FF_ENABLE_DEPRECATION_WARNINGS
|
|
|
|
#endif
|
2020-10-05 14:16:25 +02:00
|
|
|
|
|
|
|
while (!finish) {
|
|
|
|
int bs_len;
|
|
|
|
|
|
|
|
if (h->got_seqhdr) {
|
|
|
|
if (!frm->data[0] && (ret = ff_get_buffer(avctx, frm, 0)) < 0) {
|
|
|
|
return ret;
|
|
|
|
}
|
2022-03-30 21:33:24 +02:00
|
|
|
h->dec_frame.priv = frm; // AVFrame
|
2020-10-05 14:16:25 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if (uavs3d_find_next_start_code(buf_ptr, buf_end - buf_ptr, &left_bytes)) {
|
|
|
|
bs_len = buf_end - buf_ptr - left_bytes;
|
|
|
|
} else {
|
|
|
|
bs_len = buf_end - buf_ptr;
|
|
|
|
finish = 1;
|
|
|
|
}
|
|
|
|
frm_dec->bs = (unsigned char *)buf_ptr;
|
|
|
|
frm_dec->bs_len = bs_len;
|
|
|
|
frm_dec->pts = avpkt->pts;
|
|
|
|
frm_dec->dts = avpkt->dts;
|
|
|
|
uavs3d_decode(h->dec_handle, frm_dec);
|
|
|
|
buf_ptr += bs_len;
|
|
|
|
|
|
|
|
if (frm_dec->nal_type == NAL_SEQ_HEADER) {
|
|
|
|
struct uavs3d_com_seqh_t *seqh = frm_dec->seqhdr;
|
|
|
|
if (UAVS3D_CHECK_INVALID_RANGE(seqh->frame_rate_code, 0, 15)) {
|
|
|
|
av_log(avctx, AV_LOG_ERROR, "Invalid frame rate code: %d.\n", seqh->frame_rate_code);
|
|
|
|
seqh->frame_rate_code = 3; // default 25 fps
|
|
|
|
} else {
|
|
|
|
avctx->framerate.num = ff_avs3_frame_rate_tab[seqh->frame_rate_code].num;
|
|
|
|
avctx->framerate.den = ff_avs3_frame_rate_tab[seqh->frame_rate_code].den;
|
|
|
|
}
|
2022-05-13 11:40:48 +02:00
|
|
|
avctx->has_b_frames = seqh->output_reorder_delay;
|
2020-10-05 14:16:25 +02:00
|
|
|
avctx->pix_fmt = seqh->bit_depth_internal == 8 ? AV_PIX_FMT_YUV420P : AV_PIX_FMT_YUV420P10LE;
|
2022-02-10 01:06:12 +02:00
|
|
|
ret = ff_set_dimensions(avctx, seqh->horizontal_size, seqh->vertical_size);
|
|
|
|
if (ret < 0)
|
|
|
|
return ret;
|
2020-10-05 14:16:25 +02:00
|
|
|
h->got_seqhdr = 1;
|
|
|
|
|
|
|
|
if (seqh->colour_description) {
|
|
|
|
if (UAVS3D_CHECK_INVALID_RANGE(seqh->colour_primaries, 0, 9) ||
|
|
|
|
UAVS3D_CHECK_INVALID_RANGE(seqh->transfer_characteristics, 0, 14) ||
|
|
|
|
UAVS3D_CHECK_INVALID_RANGE(seqh->matrix_coefficients, 0, 11)) {
|
|
|
|
av_log(avctx, AV_LOG_ERROR,
|
|
|
|
"Invalid colour description: primaries: %d"
|
|
|
|
"transfer characteristics: %d"
|
|
|
|
"matrix coefficients: %d.\n",
|
|
|
|
seqh->colour_primaries,
|
|
|
|
seqh->transfer_characteristics,
|
|
|
|
seqh->matrix_coefficients);
|
|
|
|
} else {
|
|
|
|
avctx->color_primaries = ff_avs3_color_primaries_tab[seqh->colour_primaries];
|
|
|
|
avctx->color_trc = ff_avs3_color_transfer_tab [seqh->transfer_characteristics];
|
|
|
|
avctx->colorspace = ff_avs3_color_matrix_tab [seqh->matrix_coefficients];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (frm_dec->got_pic) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
*got_frame = h->dec_frame.got_pic;
|
|
|
|
|
|
|
|
if (!(*got_frame)) {
|
|
|
|
av_frame_unref(frm);
|
|
|
|
}
|
|
|
|
|
|
|
|
return buf_ptr - buf;
|
|
|
|
}
|
|
|
|
|
2022-03-16 22:09:54 +02:00
|
|
|
const FFCodec ff_libuavs3d_decoder = {
|
|
|
|
.p.name = "libuavs3d",
|
2022-08-29 13:38:02 +02:00
|
|
|
CODEC_LONG_NAME("libuavs3d AVS3-P2/IEEE1857.10"),
|
2022-03-16 22:09:54 +02:00
|
|
|
.p.type = AVMEDIA_TYPE_VIDEO,
|
|
|
|
.p.id = AV_CODEC_ID_AVS3,
|
2020-10-05 14:16:25 +02:00
|
|
|
.priv_data_size = sizeof(uavs3d_context),
|
|
|
|
.init = libuavs3d_init,
|
|
|
|
.close = libuavs3d_end,
|
2022-03-30 23:28:24 +02:00
|
|
|
FF_CODEC_DECODE_CB(libuavs3d_decode_frame),
|
2022-03-16 22:09:54 +02:00
|
|
|
.p.capabilities = AV_CODEC_CAP_DR1 | AV_CODEC_CAP_DELAY | AV_CODEC_CAP_OTHER_THREADS,
|
2022-07-09 22:25:41 +02:00
|
|
|
.caps_internal = FF_CODEC_CAP_NOT_INIT_THREADSAFE |
|
|
|
|
FF_CODEC_CAP_AUTO_THREADS,
|
2020-10-05 14:16:25 +02:00
|
|
|
.flush = libuavs3d_flush,
|
2022-03-16 22:09:54 +02:00
|
|
|
.p.wrapper_name = "libuavs3d",
|
2020-10-05 14:16:25 +02:00
|
|
|
};
|