aboutsummaryrefslogtreecommitdiffstats
path: root/libavcodec/libxevd.c
blob: 5d73486a96b584f9ace23b159c503918d9f2c670 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
/*
 * libxevd decoder
 * EVC (MPEG-5 Essential Video Coding) decoding using XEVD MPEG-5 EVC decoder library
 *
 * Copyright (C) 2021 Dawid Kozinski <d.kozinski@samsung.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 <float.h>
#include <stdlib.h>

#include <xevd.h>

#include "libavutil/internal.h"
#include "libavutil/common.h"
#include "libavutil/opt.h"
#include "libavutil/pixdesc.h"
#include "libavutil/pixfmt.h"
#include "libavutil/imgutils.h"
#include "libavutil/cpu.h"

#include "avcodec.h"
#include "internal.h"
#include "packet_internal.h"
#include "codec_internal.h"
#include "profiles.h"
#include "decode.h"

#define XEVD_PARAM_BAD_NAME -1
#define XEVD_PARAM_BAD_VALUE -2

#define EVC_NAL_HEADER_SIZE 2 /* byte */

/**
 * The structure stores all the states associated with the instance of Xeve MPEG-5 EVC decoder
 */
typedef struct XevdContext {
    const AVClass *class;

    XEVD id;            // XEVD instance identifier @see xevd.h
    XEVD_CDSC cdsc;     // decoding parameters @see xevd.h

    // If end of stream occurs it is required "flushing" (aka draining) the codec,
    // as the codec might buffer multiple frames or packets internally.
    int draining_mode; // The flag is set if codec enters draining mode.

    AVPacket *pkt;     // access unit (a set of NAL units that are consecutive in decoding order and containing exactly one encoded image)
} XevdContext;

/**
 * The function populates the XEVD_CDSC structure.
 * XEVD_CDSC contains all decoder parameters that should be initialized before its use.
 *
 * @param[in] avctx codec context
 * @param[out] cdsc contains all decoder parameters that should be initialized before its use
 *
 */
static void get_conf(AVCodecContext *avctx, XEVD_CDSC *cdsc)
{
    int cpu_count = av_cpu_count();

    /* clear XEVS_CDSC structure */
    memset(cdsc, 0, sizeof(XEVD_CDSC));

    /* init XEVD_CDSC */
    if (avctx->thread_count <= 0)
        cdsc->threads = (cpu_count < XEVD_MAX_TASK_CNT) ? cpu_count : XEVD_MAX_TASK_CNT;
    else if (avctx->thread_count > XEVD_MAX_TASK_CNT)
        cdsc->threads = XEVD_MAX_TASK_CNT;
    else
        cdsc->threads = avctx->thread_count;
}

/**
 * Read NAL unit length
 * @param bs input data (bitstream)
 * @return the length of NAL unit on success, 0 value on failure
 */
static uint32_t read_nal_unit_length(const uint8_t *bs, int bs_size, AVCodecContext *avctx)
{
    uint32_t len = 0;
    XEVD_INFO info;
    int ret;

    if (bs_size == XEVD_NAL_UNIT_LENGTH_BYTE) {
        ret = xevd_info((void *)bs, XEVD_NAL_UNIT_LENGTH_BYTE, 1, &info);
        if (XEVD_FAILED(ret)) {
            av_log(avctx, AV_LOG_ERROR, "Cannot get bitstream information\n");
            return 0;
        }
        len = info.nalu_len;
        if (len == 0) {
            av_log(avctx, AV_LOG_ERROR, "Invalid bitstream size! [%d]\n", bs_size);
            return 0;
        }
    }

    return len;
}

/**
 * @param[in] xectx the structure that stores all the state associated with the instance of Xeve MPEG-5 EVC decoder
 * @param[out] avctx codec context
 * @return 0 on success, negative value on failure
 */
static int export_stream_params(const XevdContext *xectx, AVCodecContext *avctx)
{
    int ret;
    int size;
    int color_space;

    avctx->pix_fmt = AV_PIX_FMT_YUV420P10;

    size = 4;
    ret = xevd_config(xectx->id, XEVD_CFG_GET_CODED_WIDTH, &avctx->coded_width, &size);
    if (XEVD_FAILED(ret)) {
        av_log(avctx, AV_LOG_ERROR, "Failed to get coded_width\n");
        return AVERROR_EXTERNAL;
    }

    ret = xevd_config(xectx->id, XEVD_CFG_GET_CODED_HEIGHT, &avctx->coded_height, &size);
    if (XEVD_FAILED(ret)) {
        av_log(avctx, AV_LOG_ERROR, "Failed to get coded_height\n");
        return AVERROR_EXTERNAL;
    }

    ret = xevd_config(xectx->id, XEVD_CFG_GET_WIDTH, &avctx->width, &size);
    if (XEVD_FAILED(ret)) {
        av_log(avctx, AV_LOG_ERROR, "Failed to get width\n");
        return AVERROR_EXTERNAL;
    }

    ret = xevd_config(xectx->id, XEVD_CFG_GET_HEIGHT, &avctx->height, &size);
    if (XEVD_FAILED(ret)) {
        av_log(avctx, AV_LOG_ERROR, "Failed to get height\n");
        return AVERROR_EXTERNAL;
    }

    ret = xevd_config(xectx->id, XEVD_CFG_GET_COLOR_SPACE, &color_space, &size);
    if (XEVD_FAILED(ret)) {
        av_log(avctx, AV_LOG_ERROR, "Failed to get color_space\n");
        return AVERROR_EXTERNAL;
    }
    switch(color_space) {
    case XEVD_CS_YCBCR400_10LE:
        avctx->pix_fmt = AV_PIX_FMT_GRAY10LE;
        break;
    case XEVD_CS_YCBCR420_10LE:
        avctx->pix_fmt = AV_PIX_FMT_YUV420P10LE;
        break;
    case XEVD_CS_YCBCR422_10LE:
        avctx->pix_fmt = AV_PIX_FMT_YUV422P10LE;
        break;
    case XEVD_CS_YCBCR444_10LE:
        avctx->pix_fmt = AV_PIX_FMT_YUV444P10LE;
        break;
    default:
        av_log(avctx, AV_LOG_ERROR, "Unknown color space\n");
        avctx->pix_fmt = AV_PIX_FMT_NONE;
        return AVERROR_INVALIDDATA;
    }

    // the function returns sps->num_reorder_pics
    ret = xevd_config(xectx->id, XEVD_CFG_GET_MAX_CODING_DELAY, &avctx->max_b_frames, &size);
    if (XEVD_FAILED(ret)) {
        av_log(avctx, AV_LOG_ERROR, "Failed to get max_coding_delay\n");
        return AVERROR_EXTERNAL;
    }

    avctx->has_b_frames = (avctx->max_b_frames) ? 1 : 0;

    return 0;
}

/**
 * @brief Copy image in imgb to frame.
 *
 * @param avctx codec context
 * @param[in] imgb
 * @param[out] frame
 * @return 0 on success, negative value on failure
 */
static int libxevd_image_copy(struct AVCodecContext *avctx, XEVD_IMGB *imgb, struct AVFrame *frame)
{
    int ret;
    if (imgb->cs != XEVD_CS_YCBCR420_10LE) {
        av_log(avctx, AV_LOG_ERROR, "Not supported pixel format: %s\n", av_get_pix_fmt_name(avctx->pix_fmt));
        return AVERROR_INVALIDDATA;
    }

    if (imgb->w[0] != avctx->width || imgb->h[0] != avctx->height) { // stream resolution changed
        if (ff_set_dimensions(avctx, imgb->w[0], imgb->h[0]) < 0) {
            av_log(avctx, AV_LOG_ERROR, "Cannot set new dimension\n");
            return AVERROR_INVALIDDATA;
        }
    }

    if (ret = ff_get_buffer(avctx, frame, 0) < 0)
        return ret;

    av_image_copy(frame->data, frame->linesize, (const uint8_t **)imgb->a,
                  imgb->s, avctx->pix_fmt,
                  imgb->w[0], imgb->h[0]);

    return 0;
}

/**
 * Initialize decoder
 * Create a decoder instance and allocate all the needed resources
 *
 * @param avctx codec context
 * @return 0 on success, negative error code on failure
 */
static av_cold int libxevd_init(AVCodecContext *avctx)
{
    XevdContext *xectx = avctx->priv_data;
    XEVD_CDSC *cdsc = &(xectx->cdsc);

    /* read configurations and set values for created descriptor (XEVD_CDSC) */
    get_conf(avctx, cdsc);

    /* create decoder */
    xectx->id = xevd_create(&(xectx->cdsc), NULL);
    if (xectx->id == NULL) {
        av_log(avctx, AV_LOG_ERROR, "Cannot create XEVD encoder\n");
        return AVERROR_EXTERNAL;
    }

    xectx->draining_mode = 0;
    xectx->pkt = av_packet_alloc();
    if (!xectx->pkt) {
        av_log(avctx, AV_LOG_ERROR, "Cannot allocate memory for AVPacket\n");
        return AVERROR(ENOMEM);
    }

    return 0;
}

static int libxevd_return_frame(AVCodecContext *avctx, AVFrame *frame,
                                XEVD_IMGB *imgb, AVPacket **pkt_au)
{
    AVPacket *pkt_au_imgb = (AVPacket*)imgb->pdata[0];
    int ret;

    if (!pkt_au_imgb) {
        av_log(avctx, AV_LOG_ERROR, "Invalid data needed to fill frame properties\n");

        if (pkt_au)
            av_packet_free(pkt_au);
        av_frame_unref(frame);

        imgb->release(imgb);
        imgb = NULL;

        return AVERROR_INVALIDDATA;
    }

    // got frame
    ret = libxevd_image_copy(avctx, imgb, frame);
    if (ret < 0) {
        av_log(avctx, AV_LOG_ERROR, "Image copying error\n");

        av_packet_free(&pkt_au_imgb);
        av_frame_unref(frame);

        imgb->release(imgb);
        imgb = NULL;

        return ret;
    }

    // use ff_decode_frame_props_from_pkt() to fill frame properties
    ret = ff_decode_frame_props_from_pkt(avctx, frame, pkt_au_imgb);
    if (ret < 0) {
        av_log(avctx, AV_LOG_ERROR, "ff_decode_frame_props_from_pkt error\n");

        av_packet_free(&pkt_au_imgb);
        av_frame_unref(frame);

        imgb->release(imgb);
        imgb = NULL;

        return ret;
    }

    frame->pkt_dts = imgb->ts[XEVD_TS_DTS];
    frame->pts = imgb->ts[XEVD_TS_PTS];

    av_packet_free(&pkt_au_imgb);

    // xevd_pull uses pool of objects of type XEVD_IMGB.
    // The pool size is equal MAX_PB_SIZE (26), so release object when it is no more needed
    imgb->release(imgb);
    imgb = NULL;

    return 0;
}
/**
  * Decode frame with decoupled packet/frame dataflow
  *
  * @param avctx codec context
  * @param[out] frame decoded frame
  *
  * @return 0 on success, negative error code on failure
  */
static int libxevd_receive_frame(AVCodecContext *avctx, AVFrame *frame)
{
    XevdContext *xectx = avctx->priv_data;
    AVPacket *pkt = xectx->pkt;
    XEVD_IMGB *imgb = NULL;

    int xevd_ret = 0;
    int ret = 0;

    // obtain access unit (input data) - a set of NAL units that are consecutive in decoding order and containing exactly one encoded image
    ret = ff_decode_get_packet(avctx, pkt);
    if (ret < 0 && ret != AVERROR_EOF) {
        av_packet_unref(pkt);

        return ret;
    } else if(ret == AVERROR_EOF && xectx->draining_mode == 0) { // End of stream situations. Enter draining mode

        xectx->draining_mode = 1;
        av_packet_unref(pkt);
    }

    if (pkt->size > 0) {
        int bs_read_pos = 0;
        XEVD_STAT stat;
        XEVD_BITB bitb;
        int nalu_size;
        AVPacket *pkt_au = av_packet_alloc();
        imgb = NULL;

        if (!pkt_au) {
            av_packet_unref(pkt);
            return AVERROR(ENOMEM);
        }
        FFSWAP(AVPacket*, pkt_au, xectx->pkt);

        // get all nal units from AU
        while(pkt_au->size > (bs_read_pos + XEVD_NAL_UNIT_LENGTH_BYTE)) {
            memset(&stat, 0, sizeof(XEVD_STAT));

            nalu_size = read_nal_unit_length(pkt_au->data + bs_read_pos, XEVD_NAL_UNIT_LENGTH_BYTE, avctx);
            if (nalu_size == 0) {
                av_log(avctx, AV_LOG_ERROR, "Invalid bitstream\n");
                av_packet_free(&pkt_au);
                ret = AVERROR_INVALIDDATA;

                return ret;
            }
            bs_read_pos += XEVD_NAL_UNIT_LENGTH_BYTE;

            bitb.addr = pkt_au->data + bs_read_pos;
            bitb.ssize = nalu_size;
            bitb.pdata[0] = pkt_au;
            bitb.ts[XEVD_TS_DTS] = pkt_au->dts;

            /* main decoding block */
            xevd_ret = xevd_decode(xectx->id, &bitb, &stat);
            if (XEVD_FAILED(xevd_ret)) {
                av_log(avctx, AV_LOG_ERROR, "Failed to decode bitstream\n");
                av_packet_free(&pkt_au);

                return AVERROR_EXTERNAL;
            }

            bs_read_pos += nalu_size;

            if (stat.nalu_type == XEVD_NUT_SPS) { // EVC stream parameters changed
                if ((ret = export_stream_params(xectx, avctx)) != 0) {
                    av_log(avctx, AV_LOG_ERROR, "Failed to export stream params\n");
                    av_packet_free(&pkt_au);

                    return ret;
                }
            }

            if (stat.read != nalu_size)
                av_log(avctx, AV_LOG_INFO, "Different reading of bitstream (in:%d, read:%d)\n,", nalu_size, stat.read);

            // stat.fnum - has negative value if the decoded data is not frame
            if (stat.fnum >= 0) {

                xevd_ret = xevd_pull(xectx->id, &imgb); // The function returns a valid image only if the return code is XEVD_OK

                if (XEVD_FAILED(xevd_ret)) {
                    av_log(avctx, AV_LOG_ERROR, "Failed to pull the decoded image (xevd error code: %d, frame#=%d)\n", xevd_ret, stat.fnum);

                    av_packet_free(&pkt_au);

                    return AVERROR_EXTERNAL;
                } else if (xevd_ret == XEVD_OK_FRM_DELAYED) {
                    if(bs_read_pos == pkt_au->size) {
                        return AVERROR(EAGAIN);
                    }
                } else { // XEVD_OK
                    if (!imgb) {
                        if(bs_read_pos == pkt_au->size) {
                            av_log(avctx, AV_LOG_ERROR, "Invalid decoded image data\n");

                            av_packet_free(&pkt_au);
                            return  AVERROR(EAGAIN);
                        }
                    } else {
                        return libxevd_return_frame(avctx, frame, imgb, &pkt_au);
                    }
                }
            }
        }
    } else { // decoder draining mode handling

        xevd_ret = xevd_pull(xectx->id, &imgb);

        if (xevd_ret == XEVD_ERR_UNEXPECTED) { // draining process completed
            av_log(avctx, AV_LOG_DEBUG, "Draining process completed\n");

            return AVERROR_EOF;
        } else if (XEVD_FAILED(xevd_ret)) { // handle all other errors
            av_log(avctx, AV_LOG_ERROR, "Failed to pull the decoded image (xevd error code: %d)\n", xevd_ret);

            return AVERROR_EXTERNAL;
        } else { // XEVD_OK
            if (!imgb) {
                av_log(avctx, AV_LOG_ERROR, "Invalid decoded image data\n");

                return AVERROR_EXTERNAL;
            }

            return libxevd_return_frame(avctx, frame, imgb, NULL);
        }
    }

    return ret;
}

/**
 * Destroy decoder
 *
 * @param avctx codec context
 * @return 0 on success
 */
static av_cold int libxevd_close(AVCodecContext *avctx)
{
    XevdContext *xectx = avctx->priv_data;
    if (xectx->id) {
        xevd_delete(xectx->id);
        xectx->id = NULL;
    }

    xectx->draining_mode = 0;
    av_packet_free(&xectx->pkt);

    return 0;
}

#define OFFSET(x) offsetof(XevdContext, x)
#define VD AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_DECODING_PARAM

static const AVClass libxevd_class = {
    .class_name = "libxevd",
    .item_name  = av_default_item_name,
    .version    = LIBAVUTIL_VERSION_INT,
};

const FFCodec ff_libxevd_decoder = {
    .p.name             = "evc",
    .p.long_name        = NULL_IF_CONFIG_SMALL("EVC / MPEG-5 Essential Video Coding (EVC)"),
    .p.type             = AVMEDIA_TYPE_VIDEO,
    .p.id               = AV_CODEC_ID_EVC,
    .init               = libxevd_init,
    FF_CODEC_RECEIVE_FRAME_CB(libxevd_receive_frame),
    .close              = libxevd_close,
    .priv_data_size     = sizeof(XevdContext),
    .p.priv_class       = &libxevd_class,
    .p.capabilities     = AV_CODEC_CAP_DR1 | AV_CODEC_CAP_DELAY |
                          AV_CODEC_CAP_OTHER_THREADS | AV_CODEC_CAP_AVOID_PROBING,
    .p.profiles         = NULL_IF_CONFIG_SMALL(ff_evc_profiles),
    .p.wrapper_name     = "libxevd",
    .caps_internal      = FF_CODEC_CAP_INIT_CLEANUP | FF_CODEC_CAP_NOT_INIT_THREADSAFE |
                          FF_CODEC_CAP_SETS_FRAME_PROPS,
};