Imported Upstream version 6.1
[platform/upstream/ffmpeg.git] / libavcodec / libuavs3d.c
1 /*
2  * AVS3-P2/IEEE1857.10 video decoder (using the uavs3d library)
3  * Copyright (c) 2020 Zhenyu Wang <wangzhenyu@pkusz.edu.cn>
4  *                    Bingjie Han <hanbj@pkusz.edu.cn>
5  *                    Huiwen Ren  <hwrenx@gmail.com>
6  *
7  * This file is part of FFmpeg.
8  *
9  * FFmpeg is free software; you can redistribute it and/or
10  * modify it under the terms of the GNU Lesser General Public
11  * License as published by the Free Software Foundation; either
12  * version 2.1 of the License, or (at your option) any later version.
13  *
14  * FFmpeg is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
17  * Lesser General Public License for more details.
18  *
19  * You should have received a copy of the GNU Lesser General Public
20  * License along with FFmpeg; if not, write to the Free Software
21  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
22  */
23
24 #include "libavutil/avutil.h"
25 #include "libavutil/common.h"
26 #include "libavutil/cpu.h"
27 #include "libavutil/imgutils.h"
28 #include "libavutil/intreadwrite.h"
29 #include "libavutil/opt.h"
30 #include "avcodec.h"
31 #include "avs3.h"
32 #include "codec_internal.h"
33 #include "decode.h"
34 #include "uavs3d.h"
35
36 typedef struct uavs3d_context {
37     AVCodecContext  *avctx;
38     void            *dec_handle;
39     int              frame_threads;
40     int              got_seqhdr;
41     uavs3d_io_frm_t  dec_frame;
42 } uavs3d_context;
43
44 #define UAVS3D_CHECK_START_CODE(data_ptr, PIC_START_CODE) \
45         (AV_RL32(data_ptr) != (PIC_START_CODE << 24) + AVS3_NAL_START_CODE)
46 static int uavs3d_find_next_start_code(const unsigned char *bs_data, int bs_len, int *left)
47 {
48     const unsigned char *data_ptr = bs_data + 4;
49     int count = bs_len - 4;
50
51     while (count >= 4 &&
52            UAVS3D_CHECK_START_CODE(data_ptr, AVS3_INTER_PIC_START_CODE) &&
53            UAVS3D_CHECK_START_CODE(data_ptr, AVS3_INTRA_PIC_START_CODE) &&
54            UAVS3D_CHECK_START_CODE(data_ptr, AVS3_SEQ_START_CODE) &&
55            UAVS3D_CHECK_START_CODE(data_ptr, AVS3_FIRST_SLICE_START_CODE) &&
56            UAVS3D_CHECK_START_CODE(data_ptr, AVS3_SEQ_END_CODE)) {
57         data_ptr++;
58         count--;
59     }
60
61     if (count >= 4) {
62         *left = count;
63         return 1;
64     }
65
66     return 0;
67 }
68
69 static void uavs3d_output_callback(uavs3d_io_frm_t *dec_frame) {
70     uavs3d_io_frm_t frm_out;
71     AVFrame *frm = (AVFrame *)dec_frame->priv;
72     int i;
73
74     if (!frm || !frm->data[0]) {
75         dec_frame->got_pic = 0;
76         av_log(NULL, AV_LOG_ERROR, "Invalid AVFrame in uavs3d output.\n");
77         return;
78     }
79
80     frm->pts       = dec_frame->pts;
81     frm->pkt_dts   = dec_frame->dts;
82 #if FF_API_FRAME_PKT
83 FF_DISABLE_DEPRECATION_WARNINGS
84     frm->pkt_pos   = dec_frame->pkt_pos;
85     frm->pkt_size  = dec_frame->pkt_size;
86 FF_ENABLE_DEPRECATION_WARNINGS
87 #endif
88 #if FF_API_FRAME_PICTURE_NUMBER
89 FF_DISABLE_DEPRECATION_WARNINGS
90     frm->coded_picture_number   = dec_frame->dtr;
91     frm->display_picture_number = dec_frame->ptr;
92 FF_ENABLE_DEPRECATION_WARNINGS
93 #endif
94
95     if (dec_frame->type < 0 || dec_frame->type >= FF_ARRAY_ELEMS(ff_avs3_image_type)) {
96         av_log(NULL, AV_LOG_WARNING, "Error frame type in uavs3d: %d.\n", dec_frame->type);
97     } else {
98         frm->pict_type = ff_avs3_image_type[dec_frame->type];
99         if (frm->pict_type == AV_PICTURE_TYPE_I)
100             frm->flags |= AV_FRAME_FLAG_KEY;
101         else
102             frm->flags &= ~AV_FRAME_FLAG_KEY;
103     }
104
105     for (i = 0; i < 3; i++) {
106         frm_out.width [i] = dec_frame->width[i];
107         frm_out.height[i] = dec_frame->height[i];
108         frm_out.stride[i] = frm->linesize[i];
109         frm_out.buffer[i] = frm->data[i];
110     }
111
112     uavs3d_img_cpy_cvt(&frm_out, dec_frame, dec_frame->bit_depth);
113 }
114
115 static av_cold int libuavs3d_init(AVCodecContext *avctx)
116 {
117     uavs3d_context *h = avctx->priv_data;
118     uavs3d_cfg_t cdsc;
119
120     cdsc.frm_threads = avctx->thread_count > 0 ? avctx->thread_count : av_cpu_count();
121     cdsc.check_md5 = 0;
122     h->dec_handle = uavs3d_create(&cdsc, uavs3d_output_callback, NULL);
123     h->got_seqhdr = 0;
124
125     if (!h->dec_handle) {
126         return AVERROR(ENOMEM);
127     }
128
129     return 0;
130 }
131
132 static av_cold int libuavs3d_end(AVCodecContext *avctx)
133 {
134     uavs3d_context *h = avctx->priv_data;
135
136     if (h->dec_handle) {
137         uavs3d_flush(h->dec_handle, NULL);
138         uavs3d_delete(h->dec_handle);
139         h->dec_handle = NULL;
140     }
141     h->got_seqhdr = 0;
142
143     return 0;
144 }
145
146 static void libuavs3d_flush(AVCodecContext * avctx)
147 {
148     uavs3d_context *h = avctx->priv_data;
149
150     if (h->dec_handle) {
151         uavs3d_reset(h->dec_handle);
152     }
153 }
154
155 #define UAVS3D_CHECK_INVALID_RANGE(v, l, r) ((v)<(l)||(v)>(r))
156 static int libuavs3d_decode_frame(AVCodecContext *avctx, AVFrame *frm,
157                                   int *got_frame, AVPacket *avpkt)
158 {
159     uavs3d_context *h = avctx->priv_data;
160     const uint8_t *buf = avpkt->data;
161     int buf_size = avpkt->size;
162     const uint8_t *buf_end;
163     const uint8_t *buf_ptr = buf;
164     int left_bytes;
165     int ret, finish = 0;
166
167     *got_frame = 0;
168     frm->pts = -1;
169     frm->pict_type = AV_PICTURE_TYPE_NONE;
170
171     if (!buf_size) {
172         if (h->got_seqhdr) {
173             if (!frm->data[0] && (ret = ff_get_buffer(avctx, frm, 0)) < 0) {
174                 return ret;
175             }
176             h->dec_frame.priv = frm;   // AVFrame
177         }
178         do {
179             ret = uavs3d_flush(h->dec_handle, &h->dec_frame);
180         } while (ret > 0 && !h->dec_frame.got_pic);
181     } else {
182         uavs3d_io_frm_t *frm_dec = &h->dec_frame;
183
184         buf_end = buf + buf_size;
185 #if FF_API_FRAME_PKT
186 FF_DISABLE_DEPRECATION_WARNINGS
187         frm_dec->pkt_pos  = avpkt->pos;
188         frm_dec->pkt_size = avpkt->size;
189 FF_ENABLE_DEPRECATION_WARNINGS
190 #endif
191
192         while (!finish) {
193             int bs_len;
194
195             if (h->got_seqhdr) {
196                 if (!frm->data[0] && (ret = ff_get_buffer(avctx, frm, 0)) < 0) {
197                     return ret;
198                 }
199                 h->dec_frame.priv = frm;   // AVFrame
200             }
201
202             if (uavs3d_find_next_start_code(buf_ptr, buf_end - buf_ptr, &left_bytes)) {
203                 bs_len = buf_end - buf_ptr - left_bytes;
204             } else {
205                 bs_len = buf_end - buf_ptr;
206                 finish = 1;
207             }
208             frm_dec->bs = (unsigned char *)buf_ptr;
209             frm_dec->bs_len = bs_len;
210             frm_dec->pts = avpkt->pts;
211             frm_dec->dts = avpkt->dts;
212             uavs3d_decode(h->dec_handle, frm_dec);
213             buf_ptr += bs_len;
214
215             if (frm_dec->nal_type == NAL_SEQ_HEADER) {
216                 struct uavs3d_com_seqh_t *seqh = frm_dec->seqhdr;
217                 if (UAVS3D_CHECK_INVALID_RANGE(seqh->frame_rate_code, 0, 15)) {
218                     av_log(avctx, AV_LOG_ERROR, "Invalid frame rate code: %d.\n", seqh->frame_rate_code);
219                     seqh->frame_rate_code = 3; // default 25 fps
220                 } else {
221                     avctx->framerate.num = ff_avs3_frame_rate_tab[seqh->frame_rate_code].num;
222                     avctx->framerate.den = ff_avs3_frame_rate_tab[seqh->frame_rate_code].den;
223                 }
224                 avctx->has_b_frames = seqh->output_reorder_delay;
225                 avctx->pix_fmt = seqh->bit_depth_internal == 8 ? AV_PIX_FMT_YUV420P : AV_PIX_FMT_YUV420P10LE;
226                 ret = ff_set_dimensions(avctx, seqh->horizontal_size, seqh->vertical_size);
227                 if (ret < 0)
228                     return ret;
229                 h->got_seqhdr = 1;
230
231                 if (seqh->colour_description) {
232                     if (UAVS3D_CHECK_INVALID_RANGE(seqh->colour_primaries, 0, 9) ||
233                         UAVS3D_CHECK_INVALID_RANGE(seqh->transfer_characteristics, 0, 14) ||
234                         UAVS3D_CHECK_INVALID_RANGE(seqh->matrix_coefficients, 0, 11)) {
235                         av_log(avctx, AV_LOG_ERROR,
236                                "Invalid colour description: primaries: %d"
237                                "transfer characteristics: %d"
238                                "matrix coefficients: %d.\n",
239                                seqh->colour_primaries,
240                                seqh->transfer_characteristics,
241                                seqh->matrix_coefficients);
242                     } else {
243                         avctx->color_primaries = ff_avs3_color_primaries_tab[seqh->colour_primaries];
244                         avctx->color_trc       = ff_avs3_color_transfer_tab [seqh->transfer_characteristics];
245                         avctx->colorspace      = ff_avs3_color_matrix_tab   [seqh->matrix_coefficients];
246                     }
247                 }
248             }
249             if (frm_dec->got_pic) {
250                 break;
251             }
252         }
253     }
254
255     *got_frame = h->dec_frame.got_pic;
256
257     if (!(*got_frame)) {
258         av_frame_unref(frm);
259     }
260
261     return buf_ptr - buf;
262 }
263
264 const FFCodec ff_libuavs3d_decoder = {
265     .p.name         = "libuavs3d",
266     CODEC_LONG_NAME("libuavs3d AVS3-P2/IEEE1857.10"),
267     .p.type         = AVMEDIA_TYPE_VIDEO,
268     .p.id           = AV_CODEC_ID_AVS3,
269     .priv_data_size = sizeof(uavs3d_context),
270     .init           = libuavs3d_init,
271     .close          = libuavs3d_end,
272     FF_CODEC_DECODE_CB(libuavs3d_decode_frame),
273     .p.capabilities = AV_CODEC_CAP_DR1 | AV_CODEC_CAP_DELAY | AV_CODEC_CAP_OTHER_THREADS,
274     .caps_internal  = FF_CODEC_CAP_NOT_INIT_THREADSAFE |
275                       FF_CODEC_CAP_AUTO_THREADS,
276     .flush          = libuavs3d_flush,
277     .p.pix_fmts     = (const enum AVPixelFormat[]) { AV_PIX_FMT_YUV420P,
278                                                      AV_PIX_FMT_YUV420P10LE,
279                                                      AV_PIX_FMT_NONE },
280     .p.wrapper_name = "libuavs3d",
281 };