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 "internal.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)
uavs3d_find_next_start_code(const unsigned char *bs_data, int bs_len, int *left)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 
uavs3d_output_callback(uavs3d_io_frm_t *dec_frame)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     frm->pkt_pos   = dec_frame->pkt_pos;
83     frm->pkt_size  = dec_frame->pkt_size;
84     frm->coded_picture_number   = dec_frame->dtr;
85     frm->display_picture_number = dec_frame->ptr;
86 
87     if (dec_frame->type < 0 || dec_frame->type >= FF_ARRAY_ELEMS(ff_avs3_image_type)) {
88         av_log(NULL, AV_LOG_WARNING, "Error frame type in uavs3d: %d.\n", dec_frame->type);
89     } else {
90         frm->pict_type = ff_avs3_image_type[dec_frame->type];
91         frm->key_frame = (frm->pict_type == AV_PICTURE_TYPE_I);
92     }
93 
94     for (i = 0; i < 3; i++) {
95         frm_out.width [i] = dec_frame->width[i];
96         frm_out.height[i] = dec_frame->height[i];
97         frm_out.stride[i] = frm->linesize[i];
98         frm_out.buffer[i] = frm->data[i];
99     }
100 
101     uavs3d_img_cpy_cvt(&frm_out, dec_frame, dec_frame->bit_depth);
102 }
103 
libuavs3d_init(AVCodecContext *avctx)104 static av_cold int libuavs3d_init(AVCodecContext *avctx)
105 {
106     uavs3d_context *h = avctx->priv_data;
107     uavs3d_cfg_t cdsc;
108 
109     cdsc.frm_threads = avctx->thread_count > 0 ? avctx->thread_count : av_cpu_count();
110     cdsc.check_md5 = 0;
111     h->dec_handle = uavs3d_create(&cdsc, uavs3d_output_callback, NULL);
112     h->got_seqhdr = 0;
113 
114     if (!h->dec_handle) {
115         return AVERROR(ENOMEM);
116     }
117 
118     return 0;
119 }
120 
libuavs3d_end(AVCodecContext *avctx)121 static av_cold int libuavs3d_end(AVCodecContext *avctx)
122 {
123     uavs3d_context *h = avctx->priv_data;
124 
125     if (h->dec_handle) {
126         uavs3d_flush(h->dec_handle, NULL);
127         uavs3d_delete(h->dec_handle);
128         h->dec_handle = NULL;
129     }
130     h->got_seqhdr = 0;
131 
132     return 0;
133 }
134 
libuavs3d_flush(AVCodecContext * avctx)135 static void libuavs3d_flush(AVCodecContext * avctx)
136 {
137     uavs3d_context *h = avctx->priv_data;
138 
139     if (h->dec_handle) {
140         uavs3d_reset(h->dec_handle);
141     }
142 }
143 
144 #define UAVS3D_CHECK_INVALID_RANGE(v, l, r) ((v)<(l)||(v)>(r))
libuavs3d_decode_frame(AVCodecContext *avctx, AVFrame *frm, int *got_frame, AVPacket *avpkt)145 static int libuavs3d_decode_frame(AVCodecContext *avctx, AVFrame *frm,
146                                   int *got_frame, AVPacket *avpkt)
147 {
148     uavs3d_context *h = avctx->priv_data;
149     const uint8_t *buf = avpkt->data;
150     int buf_size = avpkt->size;
151     const uint8_t *buf_end;
152     const uint8_t *buf_ptr = buf;
153     int left_bytes;
154     int ret, finish = 0;
155 
156     *got_frame = 0;
157     frm->pts = -1;
158     frm->pict_type = AV_PICTURE_TYPE_NONE;
159 
160     if (!buf_size) {
161         if (h->got_seqhdr) {
162             if (!frm->data[0] && (ret = ff_get_buffer(avctx, frm, 0)) < 0) {
163                 return ret;
164             }
165             h->dec_frame.priv = frm;   // AVFrame
166         }
167         do {
168             ret = uavs3d_flush(h->dec_handle, &h->dec_frame);
169         } while (ret > 0 && !h->dec_frame.got_pic);
170     } else {
171         uavs3d_io_frm_t *frm_dec = &h->dec_frame;
172 
173         buf_end = buf + buf_size;
174         frm_dec->pkt_pos  = avpkt->pos;
175         frm_dec->pkt_size = avpkt->size;
176 
177         while (!finish) {
178             int bs_len;
179 
180             if (h->got_seqhdr) {
181                 if (!frm->data[0] && (ret = ff_get_buffer(avctx, frm, 0)) < 0) {
182                     return ret;
183                 }
184                 h->dec_frame.priv = frm;   // AVFrame
185             }
186 
187             if (uavs3d_find_next_start_code(buf_ptr, buf_end - buf_ptr, &left_bytes)) {
188                 bs_len = buf_end - buf_ptr - left_bytes;
189             } else {
190                 bs_len = buf_end - buf_ptr;
191                 finish = 1;
192             }
193             frm_dec->bs = (unsigned char *)buf_ptr;
194             frm_dec->bs_len = bs_len;
195             frm_dec->pts = avpkt->pts;
196             frm_dec->dts = avpkt->dts;
197             uavs3d_decode(h->dec_handle, frm_dec);
198             buf_ptr += bs_len;
199 
200             if (frm_dec->nal_type == NAL_SEQ_HEADER) {
201                 struct uavs3d_com_seqh_t *seqh = frm_dec->seqhdr;
202                 if (UAVS3D_CHECK_INVALID_RANGE(seqh->frame_rate_code, 0, 15)) {
203                     av_log(avctx, AV_LOG_ERROR, "Invalid frame rate code: %d.\n", seqh->frame_rate_code);
204                     seqh->frame_rate_code = 3; // default 25 fps
205                 } else {
206                     avctx->framerate.num = ff_avs3_frame_rate_tab[seqh->frame_rate_code].num;
207                     avctx->framerate.den = ff_avs3_frame_rate_tab[seqh->frame_rate_code].den;
208                 }
209                 avctx->has_b_frames = seqh->output_reorder_delay;
210                 avctx->pix_fmt = seqh->bit_depth_internal == 8 ? AV_PIX_FMT_YUV420P : AV_PIX_FMT_YUV420P10LE;
211                 ret = ff_set_dimensions(avctx, seqh->horizontal_size, seqh->vertical_size);
212                 if (ret < 0)
213                     return ret;
214                 h->got_seqhdr = 1;
215 
216                 if (seqh->colour_description) {
217                     if (UAVS3D_CHECK_INVALID_RANGE(seqh->colour_primaries, 0, 9) ||
218                         UAVS3D_CHECK_INVALID_RANGE(seqh->transfer_characteristics, 0, 14) ||
219                         UAVS3D_CHECK_INVALID_RANGE(seqh->matrix_coefficients, 0, 11)) {
220                         av_log(avctx, AV_LOG_ERROR,
221                                "Invalid colour description: primaries: %d"
222                                "transfer characteristics: %d"
223                                "matrix coefficients: %d.\n",
224                                seqh->colour_primaries,
225                                seqh->transfer_characteristics,
226                                seqh->matrix_coefficients);
227                     } else {
228                         avctx->color_primaries = ff_avs3_color_primaries_tab[seqh->colour_primaries];
229                         avctx->color_trc       = ff_avs3_color_transfer_tab [seqh->transfer_characteristics];
230                         avctx->colorspace      = ff_avs3_color_matrix_tab   [seqh->matrix_coefficients];
231                     }
232                 }
233             }
234             if (frm_dec->got_pic) {
235                 break;
236             }
237         }
238     }
239 
240     *got_frame = h->dec_frame.got_pic;
241 
242     if (!(*got_frame)) {
243         av_frame_unref(frm);
244     }
245 
246     return buf_ptr - buf;
247 }
248 
249 const FFCodec ff_libuavs3d_decoder = {
250     .p.name         = "libuavs3d",
251     .p.long_name    = NULL_IF_CONFIG_SMALL("libuavs3d AVS3-P2/IEEE1857.10"),
252     .p.type         = AVMEDIA_TYPE_VIDEO,
253     .p.id           = AV_CODEC_ID_AVS3,
254     .priv_data_size = sizeof(uavs3d_context),
255     .init           = libuavs3d_init,
256     .close          = libuavs3d_end,
257     FF_CODEC_DECODE_CB(libuavs3d_decode_frame),
258     .p.capabilities = AV_CODEC_CAP_DR1 | AV_CODEC_CAP_DELAY | AV_CODEC_CAP_OTHER_THREADS,
259     .caps_internal  = FF_CODEC_CAP_AUTO_THREADS,
260     .flush          = libuavs3d_flush,
261     .p.pix_fmts     = (const enum AVPixelFormat[]) { AV_PIX_FMT_YUV420P,
262                                                      AV_PIX_FMT_YUV420P10LE,
263                                                      AV_PIX_FMT_NONE },
264     .p.wrapper_name = "libuavs3d",
265 };
266