Coverage Report

Created: 2026-09-28 07:27

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/vlc/modules/codec/vpx.c
Line
Count
Source
1
/*****************************************************************************
2
 * vpx.c: libvpx decoder (VP8/VP9) module
3
 *****************************************************************************
4
 * Copyright (C) 2013 Rafaël Carré
5
 *
6
 * Authors: Rafaël Carré <funman@videolanorg>
7
 *
8
 * This program is free software; you can redistribute it and/or modify it
9
 * under the terms of the GNU Lesser General Public License as published by
10
 * the Free Software Foundation; either version 2.1 of the License, or
11
 * (at your option) any later version.
12
 *
13
 * This program is distributed in the hope that it will be useful,
14
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16
 * GNU Lesser General Public License for more details.
17
 *
18
 * You should have received a copy of the GNU Lesser General Public License
19
 * along with this program; if not, write to the Free Software Foundation,
20
 * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
21
 *****************************************************************************/
22
23
/*****************************************************************************
24
 * Preamble
25
 *****************************************************************************/
26
#ifdef HAVE_CONFIG_H
27
# include "config.h"
28
#endif
29
30
#include <vlc_common.h>
31
#include <vlc_configuration.h>
32
#include <vlc_plugin.h>
33
#include <vlc_codec.h>
34
35
#include <vpx/vpx_decoder.h>
36
#include <vpx/vp8dx.h>
37
#include <vpx/vpx_image.h>
38
39
#ifdef ENABLE_SOUT
40
# include <vpx/vpx_encoder.h>
41
# include <vpx/vp8cx.h>
42
#endif
43
44
/****************************************************************************
45
 * Local prototypes
46
 ****************************************************************************/
47
static int OpenDecoder(vlc_object_t *);
48
static void CloseDecoder(vlc_object_t *);
49
#ifdef ENABLE_SOUT
50
static const char *const ppsz_sout_options[] = { "quality-mode", NULL };
51
static int OpenEncoder(vlc_object_t *);
52
static void CloseEncoder(encoder_t *);
53
static block_t *Encode(encoder_t *p_enc, picture_t *p_pict);
54
55
#define QUALITY_MODE_TEXT N_("Quality mode")
56
#define QUALITY_MODE_LONGTEXT N_("Quality setting which will determine max encoding time.")
57
58
static const int quality_values[] = {
59
    VPX_DL_GOOD_QUALITY, VPX_DL_REALTIME, VPX_DL_BEST_QUALITY
60
};
61
static const char* const quality_desc[] = {
62
    N_("Good"), N_("Realtime"), N_("Best"),
63
};
64
#endif
65
#define THREADS_TEXT N_( "Threads" )
66
#define THREADS_LONGTEXT N_( "Number of threads used for decoding, 0 meaning auto" )
67
68
/*****************************************************************************
69
 * Module descriptor
70
 *****************************************************************************/
71
72
174
vlc_module_begin ()
73
87
    set_shortname("vpx")
74
87
    set_description(N_("WebM video decoder"))
75
87
    set_capability("video decoder", 60)
76
174
    set_callbacks(OpenDecoder, CloseDecoder)
77
87
    add_integer( "vpx-threads", 0, THREADS_TEXT, THREADS_LONGTEXT );
78
87
    set_subcategory(SUBCAT_INPUT_VCODEC)
79
87
#ifdef ENABLE_SOUT
80
87
    add_submodule()
81
87
    set_shortname("vpx")
82
87
    set_capability("video encoder", 60)
83
87
    set_description(N_("WebM video encoder"))
84
87
    set_callback(OpenEncoder)
85
86
87
    add_submodule()
87
87
    set_shortname("vpx")
88
87
    set_capability("image encoder", 60)
89
87
    set_description(N_("WebP image encoder"))
90
87
    set_callback(OpenEncoder)
91
92
87
#   define ENC_CFG_PREFIX "sout-vpx-"
93
87
    add_integer( ENC_CFG_PREFIX "quality-mode", VPX_DL_BEST_QUALITY, QUALITY_MODE_TEXT,
94
87
                 QUALITY_MODE_LONGTEXT )
95
87
        change_integer_list( quality_values, quality_desc )
96
87
#endif
97
87
vlc_module_end ()
98
99
static void vpx_err_msg(vlc_object_t *this, struct vpx_codec_ctx *ctx,
100
                        const char *msg)
101
2.05k
{
102
2.05k
    const char *error  = vpx_codec_error(ctx);
103
2.05k
    const char *detail = vpx_codec_error_detail(ctx);
104
2.05k
    if (!detail)
105
1.91k
        detail = "no specific information";
106
2.05k
    msg_Err(this, msg, error, detail);
107
2.05k
}
108
109
2.05k
#define VPX_ERR(this, ctx, msg) vpx_err_msg(VLC_OBJECT(this), ctx, msg ": %s (%s)")
110
111
/*****************************************************************************
112
 * decoder_sys_t: libvpx decoder descriptor
113
 *****************************************************************************/
114
typedef struct
115
{
116
    struct vpx_codec_ctx ctx;
117
} decoder_sys_t;
118
119
static const struct
120
{
121
    vlc_fourcc_t     i_chroma;
122
    enum vpx_img_fmt i_chroma_id;
123
    uint8_t          i_bitdepth;
124
    enum vpx_color_space cs;
125
} chroma_table[] =
126
{
127
    /* Transfer characteristic-dependent mappings must come first */
128
    { VLC_CODEC_GBR_PLANAR, VPX_IMG_FMT_I444, 8, VPX_CS_SRGB },
129
    { VLC_CODEC_GBR_PLANAR_10L, VPX_IMG_FMT_I44416, 10, VPX_CS_SRGB },
130
131
    { VLC_CODEC_I420, VPX_IMG_FMT_I420, 8, VPX_CS_UNKNOWN },
132
    { VLC_CODEC_I422, VPX_IMG_FMT_I422, 8, VPX_CS_UNKNOWN },
133
    { VLC_CODEC_I444, VPX_IMG_FMT_I444, 8, VPX_CS_UNKNOWN },
134
    { VLC_CODEC_I440, VPX_IMG_FMT_I440, 8, VPX_CS_UNKNOWN },
135
136
    { VLC_CODEC_YV12, VPX_IMG_FMT_YV12, 8, VPX_CS_UNKNOWN },
137
138
    { VLC_CODEC_I420_10L, VPX_IMG_FMT_I42016, 10, VPX_CS_UNKNOWN },
139
    { VLC_CODEC_I422_10L, VPX_IMG_FMT_I42216, 10, VPX_CS_UNKNOWN },
140
    { VLC_CODEC_I444_10L, VPX_IMG_FMT_I44416, 10, VPX_CS_UNKNOWN },
141
142
    { VLC_CODEC_I420_12L, VPX_IMG_FMT_I42016, 12, VPX_CS_UNKNOWN },
143
    { VLC_CODEC_I422_12L, VPX_IMG_FMT_I42216, 12, VPX_CS_UNKNOWN },
144
    { VLC_CODEC_I444_12L, VPX_IMG_FMT_I44416, 12, VPX_CS_UNKNOWN },
145
146
    { VLC_CODEC_I444_16L, VPX_IMG_FMT_I44416, 16, VPX_CS_UNKNOWN },
147
};
148
149
struct video_color
150
{
151
    video_color_primaries_t primaries;
152
    video_transfer_func_t transfer;
153
    video_color_space_t space;
154
};
155
156
const struct video_color vpx_color_mapping_table[] =
157
{
158
    [VPX_CS_UNKNOWN]   =  { COLOR_PRIMARIES_UNDEF,
159
                            TRANSFER_FUNC_UNDEF,
160
                            COLOR_SPACE_UNDEF },
161
    [VPX_CS_BT_601]    =  { COLOR_PRIMARIES_BT601_525,
162
                            TRANSFER_FUNC_BT709,
163
                            COLOR_SPACE_BT601 },
164
    [VPX_CS_BT_709]    =  { COLOR_PRIMARIES_BT709,
165
                            TRANSFER_FUNC_BT709,
166
                            COLOR_SPACE_BT709 },
167
    [VPX_CS_SMPTE_170] =  { COLOR_PRIMARIES_SMTPE_170,
168
                            TRANSFER_FUNC_BT709,
169
                            COLOR_SPACE_BT601 },
170
    [VPX_CS_SMPTE_240] =  { COLOR_PRIMARIES_SMTPE_240,
171
                            TRANSFER_FUNC_SMPTE_240,
172
                            COLOR_SPACE_UNDEF },
173
    [VPX_CS_BT_2020]   =  { COLOR_PRIMARIES_BT2020,
174
                            TRANSFER_FUNC_BT2020,
175
                            COLOR_SPACE_BT2020 },
176
    [VPX_CS_RESERVED]  =  { COLOR_PRIMARIES_UNDEF,
177
                            TRANSFER_FUNC_UNDEF,
178
                            COLOR_SPACE_UNDEF },
179
    [VPX_CS_SRGB]      =  { COLOR_PRIMARIES_SRGB,
180
                            TRANSFER_FUNC_SRGB,
181
                            COLOR_SPACE_UNDEF },
182
};
183
184
static vlc_fourcc_t FindVlcChroma( struct vpx_image *img )
185
3.26k
{
186
9.78k
    for( unsigned int i = 0; i < ARRAY_SIZE(chroma_table); i++ )
187
9.78k
        if( chroma_table[i].i_chroma_id == img->fmt &&
188
3.26k
            chroma_table[i].i_bitdepth == img->bit_depth &&
189
3.26k
            ( chroma_table[i].cs == VPX_CS_UNKNOWN ||
190
0
              chroma_table[i].cs == img->cs ) )
191
3.26k
            return chroma_table[i].i_chroma;
192
193
0
    return 0;
194
3.26k
}
195
196
/****************************************************************************
197
 * Decode: the whole thing
198
 ****************************************************************************/
199
static int Decode(decoder_t *dec, block_t *block)
200
17.1k
{
201
17.1k
    decoder_sys_t *p_sys = dec->p_sys;
202
17.1k
    struct vpx_codec_ctx *ctx = &p_sys->ctx;
203
204
17.1k
    if (block == NULL) /* No Drain */
205
11.4k
        return VLCDEC_SUCCESS;
206
207
5.67k
    if (block->i_flags & (BLOCK_FLAG_CORRUPTED)) {
208
0
        block_Release(block);
209
0
        return VLCDEC_SUCCESS;
210
0
    }
211
212
    /* Associate packet PTS with decoded frame */
213
5.67k
    vlc_tick_t *pkt_pts = malloc(sizeof(*pkt_pts));
214
5.67k
    if (!pkt_pts) {
215
0
        block_Release(block);
216
0
        return VLCDEC_SUCCESS;
217
0
    }
218
219
5.67k
    *pkt_pts = (block->i_pts != VLC_TICK_INVALID) ? block->i_pts : block->i_dts;
220
221
5.67k
    vpx_codec_err_t err;
222
5.67k
    err = vpx_codec_decode(ctx, block->p_buffer, block->i_buffer, pkt_pts, 0);
223
224
5.67k
    block_Release(block);
225
226
5.67k
    if (err != VPX_CODEC_OK && err != VPX_CODEC_CORRUPT_FRAME) {
227
2.05k
        free(pkt_pts);
228
2.05k
        VPX_ERR(dec, ctx, "Failed to decode frame");
229
2.05k
        if (err == VPX_CODEC_UNSUP_BITSTREAM)
230
1.97k
            return VLCDEC_ECRITICAL;
231
80
        else
232
80
            return VLCDEC_SUCCESS;
233
2.05k
    }
234
235
3.61k
    const void *iter = NULL;
236
3.61k
    struct vpx_image *img = vpx_codec_get_frame(ctx, &iter);
237
3.61k
    if (!img) {
238
352
        free(pkt_pts);
239
352
        return VLCDEC_SUCCESS;
240
352
    }
241
242
3.26k
    if (err == VPX_CODEC_CORRUPT_FRAME) {
243
4
        VPX_ERR(dec, ctx, "Failed to decode frame");
244
4
        free(pkt_pts);
245
4
        return VLCDEC_SUCCESS;
246
4
    }
247
248
    /* fetches back the PTS */
249
3.26k
    pkt_pts = img->user_priv;
250
3.26k
    vlc_tick_t pts = *pkt_pts;
251
3.26k
    free(pkt_pts);
252
253
3.26k
    dec->fmt_out.i_codec = FindVlcChroma(img);
254
255
3.26k
    if( dec->fmt_out.i_codec == 0 ) {
256
0
        msg_Err(dec, "Unsupported output colorspace %d", img->fmt);
257
0
        return VLCDEC_SUCCESS;
258
0
    }
259
260
3.26k
    video_format_t *v = &dec->fmt_out.video;
261
262
3.26k
    if (img->d_w != v->i_visible_width || img->d_h != v->i_visible_height) {
263
160
        v->i_visible_width = dec->fmt_out.video.i_width = img->d_w;
264
160
        v->i_visible_height = dec->fmt_out.video.i_height = img->d_h;
265
160
    }
266
267
3.26k
    if( !dec->fmt_out.video.i_sar_num || !dec->fmt_out.video.i_sar_den )
268
2
    {
269
2
        dec->fmt_out.video.i_sar_num = 1;
270
2
        dec->fmt_out.video.i_sar_den = 1;
271
2
    }
272
273
3.26k
    if(dec->fmt_in->video.primaries == COLOR_PRIMARIES_UNDEF &&
274
3.26k
       img->cs >= 0 && img->cs < ARRAY_SIZE(vpx_color_mapping_table))
275
3.26k
    {
276
3.26k
        v->primaries = vpx_color_mapping_table[img->cs].primaries;
277
3.26k
        v->transfer = vpx_color_mapping_table[img->cs].transfer;
278
3.26k
        v->space = vpx_color_mapping_table[img->cs].space;
279
3.26k
        v->color_range = img->range == VPX_CR_FULL_RANGE ? COLOR_RANGE_FULL : COLOR_RANGE_LIMITED;
280
3.26k
    }
281
282
3.26k
    dec->fmt_out.video.projection_mode = dec->fmt_in->video.projection_mode;
283
3.26k
    dec->fmt_out.video.multiview_mode = dec->fmt_in->video.multiview_mode;
284
3.26k
    dec->fmt_out.video.pose = dec->fmt_in->video.pose;
285
286
3.26k
    if (decoder_UpdateVideoFormat(dec))
287
0
        return VLCDEC_SUCCESS;
288
3.26k
    picture_t *pic = decoder_NewPicture(dec);
289
3.26k
    if (!pic)
290
1
        return VLCDEC_SUCCESS;
291
292
13.0k
    for (int plane = 0; plane < pic->i_planes; plane++ ) {
293
9.77k
        plane_t src_plane = pic->p[plane];
294
9.77k
        src_plane.p_pixels = img->planes[plane];
295
9.77k
        src_plane.i_pitch = img->stride[plane];
296
9.77k
        plane_CopyPixels(&pic->p[plane], &src_plane);
297
9.77k
    }
298
299
3.25k
    pic->b_progressive = true; /* codec does not support interlacing */
300
3.25k
    pic->date = pts;
301
302
3.25k
    decoder_QueueVideo(dec, pic);
303
3.25k
    return VLCDEC_SUCCESS;
304
3.26k
}
305
306
/*****************************************************************************
307
 * OpenDecoder: probe the decoder
308
 *****************************************************************************/
309
static int OpenDecoder(vlc_object_t *p_this)
310
27.0k
{
311
27.0k
    decoder_t *dec = (decoder_t *)p_this;
312
27.0k
    const struct vpx_codec_iface *iface;
313
27.0k
    int vp_version;
314
315
27.0k
    switch (dec->fmt_in->i_codec)
316
27.0k
    {
317
0
#ifdef ENABLE_VP8_DECODER
318
187
    case VLC_CODEC_VP8:
319
187
        if (es_format_HasVpxAlpha(dec->fmt_in)) // contains alpha extradata
320
9
            return VLC_ENOTSUP;
321
        // fallthrough
322
217
    case VLC_CODEC_WEBP:
323
225
    case VLC_CODEC_VP8ALPHA_ES:
324
225
        iface = &vpx_codec_vp8_dx_algo;
325
225
        vp_version = 8;
326
225
        break;
327
0
#endif
328
0
#ifdef ENABLE_VP9_DECODER
329
4
    case VLC_CODEC_VP9:
330
4
        if (es_format_HasVpxAlpha(dec->fmt_in)) // contains alpha extradata
331
0
            return VLC_ENOTSUP;
332
        // fallthrough
333
4
    case VLC_CODEC_VP9ALPHA_ES:
334
4
        iface = &vpx_codec_vp9_dx_algo;
335
4
        vp_version = 9;
336
4
        break;
337
0
#endif
338
26.7k
    default:
339
26.7k
        return VLC_EGENERIC;
340
27.0k
    }
341
342
229
    decoder_sys_t *sys = malloc(sizeof(*sys));
343
229
    if (!sys)
344
0
        return VLC_ENOMEM;
345
229
    dec->p_sys = sys;
346
347
229
    int i_thread_count = var_InheritInteger(p_this, "vpx-threads");
348
229
    if (i_thread_count <= 0)
349
229
        i_thread_count = vlc_GetCPUCount();
350
229
    struct vpx_codec_dec_cfg deccfg = {
351
229
        .threads = __MIN(i_thread_count, 16)
352
229
    };
353
354
229
    msg_Dbg(p_this, "VP%d: using libvpx version %s (build options %s)",
355
229
        vp_version, vpx_codec_version_str(), vpx_codec_build_config());
356
357
229
    if (vpx_codec_dec_init(&sys->ctx, iface, &deccfg, 0) != VPX_CODEC_OK) {
358
0
        VPX_ERR(p_this, &sys->ctx, "Failed to initialize decoder");
359
0
        free(sys);
360
0
        return VLC_EGENERIC;
361
0
    }
362
363
229
    dec->pf_decode = Decode;
364
365
229
    dec->fmt_out.video.i_width = dec->fmt_in->video.i_width;
366
229
    dec->fmt_out.video.i_height = dec->fmt_in->video.i_height;
367
368
229
    if (dec->fmt_in->video.i_sar_num > 0 && dec->fmt_in->video.i_sar_den > 0) {
369
180
        dec->fmt_out.video.i_sar_num = dec->fmt_in->video.i_sar_num;
370
180
        dec->fmt_out.video.i_sar_den = dec->fmt_in->video.i_sar_den;
371
180
    }
372
373
229
    return VLC_SUCCESS;
374
229
}
375
376
/*****************************************************************************
377
 * CloseDecoder: decoder destruction
378
 *****************************************************************************/
379
static void CloseDecoder(vlc_object_t *p_this)
380
229
{
381
229
    decoder_t *dec = (decoder_t *)p_this;
382
229
    decoder_sys_t *sys = dec->p_sys;
383
384
    /* Free our PTS */
385
229
    const void *iter = NULL;
386
229
    for (;;) {
387
229
        struct vpx_image *img = vpx_codec_get_frame(&sys->ctx, &iter);
388
229
        if (!img)
389
229
            break;
390
0
        free(img->user_priv);
391
0
    }
392
393
229
    vpx_codec_destroy(&sys->ctx);
394
395
229
    free(sys);
396
229
}
397
398
#ifdef ENABLE_SOUT
399
400
/*****************************************************************************
401
 * encoder_sys_t: libvpx encoder descriptor
402
 *****************************************************************************/
403
typedef struct
404
{
405
    struct vpx_codec_ctx ctx;
406
    unsigned long quality;
407
} encoder_sys_t;
408
409
/*****************************************************************************
410
 * OpenEncoder: probe the encoder
411
 *****************************************************************************/
412
static int OpenEncoder(vlc_object_t *p_this)
413
0
{
414
0
    encoder_t *p_enc = (encoder_t *)p_this;
415
0
    encoder_sys_t *p_sys;
416
417
0
    const struct vpx_codec_iface *iface;
418
0
    int vp_version;
419
420
0
    switch (p_enc->fmt_out.i_codec)
421
0
    {
422
0
#ifdef ENABLE_VP8_ENCODER
423
0
    case VLC_CODEC_VP8:
424
0
        if (es_format_HasVpxAlpha(&p_enc->fmt_out)) // contains alpha extradata
425
0
            return VLC_ENOTSUP;
426
        // fallthrough
427
0
    case VLC_CODEC_WEBP:
428
0
        iface = &vpx_codec_vp8_cx_algo;
429
0
        vp_version = 8;
430
0
        break;
431
0
#endif
432
0
#ifdef ENABLE_VP9_ENCODER
433
0
    case VLC_CODEC_VP9:
434
0
        if (es_format_HasVpxAlpha(&p_enc->fmt_out)) // contains alpha extradata
435
0
            return VLC_ENOTSUP;
436
0
        iface = &vpx_codec_vp9_cx_algo;
437
0
        vp_version = 9;
438
0
        break;
439
0
#endif
440
0
    default:
441
0
        return VLC_EGENERIC;
442
0
    }
443
444
    /* Allocate the memory needed to store the encoder's structure */
445
0
    p_sys = malloc(sizeof(*p_sys));
446
0
    if (p_sys == NULL)
447
0
        return VLC_ENOMEM;
448
449
0
    struct vpx_codec_enc_cfg enccfg = {0};
450
0
    vpx_codec_enc_config_default(iface, &enccfg, 0);
451
0
    enccfg.g_threads = __MIN(vlc_GetCPUCount(), 4);
452
0
    enccfg.g_w = p_enc->fmt_in.video.i_visible_width;
453
0
    enccfg.g_h = p_enc->fmt_in.video.i_visible_height;
454
455
0
    msg_Dbg(p_this, "VP%d: using libvpx version %s (build options %s)",
456
0
        vp_version, vpx_codec_version_str(), vpx_codec_build_config());
457
458
0
    struct vpx_codec_ctx *ctx = &p_sys->ctx;
459
0
    if (vpx_codec_enc_init(ctx, iface, &enccfg, 0) != VPX_CODEC_OK) {
460
0
        VPX_ERR(p_this, ctx, "Failed to initialize encoder");
461
0
        goto error;
462
0
    }
463
464
0
    p_enc->fmt_in.i_codec = p_enc->fmt_in.video.i_chroma = VLC_CODEC_I420;
465
0
    config_ChainParse(p_enc, ENC_CFG_PREFIX, ppsz_sout_options, p_enc->p_cfg);
466
467
    /* Deadline (in ms) to spend in encoder */
468
0
    const unsigned long quality = var_GetInteger(p_enc, ENC_CFG_PREFIX "quality-mode");
469
0
    switch (quality) {
470
0
        case VPX_DL_REALTIME:
471
0
        case VPX_DL_BEST_QUALITY:
472
0
        case VPX_DL_GOOD_QUALITY:
473
0
            p_sys->quality = quality;
474
0
            break;
475
0
        default:
476
0
            msg_Warn(p_this, "Unexpected quality %lu, forcing %lu", quality, (unsigned long)VPX_DL_BEST_QUALITY);
477
0
            p_sys->quality = VPX_DL_BEST_QUALITY;
478
0
            break;
479
0
    }
480
481
0
    static const struct vlc_encoder_operations ops =
482
0
    {
483
0
        .close = CloseEncoder,
484
0
        .encode_video = Encode,
485
0
    };
486
0
    p_enc->ops = &ops;
487
0
    p_enc->p_sys = p_sys;
488
489
0
    return VLC_SUCCESS;
490
0
error:
491
0
    free(p_sys);
492
0
    return VLC_EGENERIC;
493
0
}
494
495
static const uint32_t webp_simple_lossy_header[5] = {
496
    VLC_FOURCC('R', 'I', 'F', 'F'),
497
    0, /* TBD: total size of VP8 data plus 12 bytes for WEBP fourcc + VP8 ChunkHeader */
498
    VLC_FOURCC('W', 'E', 'B', 'P'),
499
    VLC_FOURCC('V', 'P', '8', ' '),
500
    0, /* TBD: total size of VP8 data */
501
};
502
503
static void webp_write_header(uint8_t *p_header, uint32_t i_size, size_t i_header_size)
504
0
{
505
0
    assert(i_header_size == sizeof(webp_simple_lossy_header));
506
507
0
    memcpy(p_header, webp_simple_lossy_header, i_header_size);
508
0
    SetDWLE(p_header + 1*sizeof(uint32_t), i_size + 4 + 8);
509
0
    SetDWLE(p_header + 4*sizeof(uint32_t), i_size);
510
0
}
511
512
/****************************************************************************
513
 * Encode: the whole thing
514
 ****************************************************************************/
515
static block_t *Encode(encoder_t *p_enc, picture_t *p_pict)
516
0
{
517
0
    encoder_sys_t *p_sys = p_enc->p_sys;
518
0
    struct vpx_codec_ctx *ctx = &p_sys->ctx;
519
520
0
    if (!p_pict) return NULL;
521
522
0
    vpx_image_t img = {0};
523
0
    unsigned i_w = p_enc->fmt_in.video.i_visible_width;
524
0
    unsigned i_h = p_enc->fmt_in.video.i_visible_height;
525
526
    /* Create and initialize the vpx_image (use 1 and correct later to avoid getting
527
       rejected for non-power of 2 pitch) */
528
0
    if (!vpx_img_wrap(&img, VPX_IMG_FMT_I420, i_w, i_h, 1, p_pict->p[0].p_pixels)) {
529
0
        VPX_ERR(p_enc, ctx, "Failed to wrap image");
530
0
        return NULL;
531
0
    }
532
533
    /* Fill in real plane/stride values. */
534
0
    for (int plane = 0; plane < p_pict->i_planes; plane++) {
535
0
        img.planes[plane] = p_pict->p[plane].p_pixels;
536
0
        img.stride[plane] = p_pict->p[plane].i_pitch;
537
0
    }
538
539
0
    int flags = 0;
540
541
0
    vpx_codec_err_t res = vpx_codec_encode(ctx, &img, p_pict->date, 1,
542
0
     flags, p_sys->quality);
543
0
    if (res != VPX_CODEC_OK) {
544
0
        VPX_ERR(p_enc, ctx, "Failed to encode frame");
545
0
        vpx_img_free(&img);
546
0
        return NULL;
547
0
    }
548
549
0
    const vpx_codec_cx_pkt_t *pkt = NULL;
550
0
    vpx_codec_iter_t iter = NULL;
551
0
    block_t *p_out = NULL;
552
553
    /* WebP container specific context */
554
0
    uint32_t i_vp8_data_size = 0;
555
0
    uint8_t *p_header = NULL;
556
0
    const bool b_is_webp = p_enc->fmt_out.i_codec == VLC_CODEC_WEBP;
557
0
    static const size_t i_webp_header_size = sizeof(webp_simple_lossy_header);
558
559
0
    while ((pkt = vpx_codec_get_cx_data(ctx, &iter)) != NULL)
560
0
    {
561
0
        if (pkt->kind == VPX_CODEC_CX_FRAME_PKT)
562
0
        {
563
0
            size_t i_block_sz = pkt->data.frame.sz;
564
0
            const bool b_needs_padding_byte = b_is_webp && (pkt->data.frame.sz & 1);
565
0
            int keyframe = pkt->data.frame.flags & VPX_FRAME_IS_KEY;
566
567
0
            if (b_is_webp && p_header == NULL) {
568
0
                i_block_sz += i_webp_header_size;
569
0
                i_block_sz += b_needs_padding_byte;
570
0
            }
571
572
0
            block_t *p_block = block_Alloc(i_block_sz);
573
0
            if (unlikely(p_block == NULL))
574
0
            {
575
0
                block_ChainRelease(p_out);
576
0
                p_out = NULL;
577
0
                break;
578
0
            }
579
580
0
            uint8_t *p_buffer = p_block->p_buffer;
581
582
            /* Leave room at the beginning for the WebP header data. */
583
0
            if (b_is_webp && p_header == NULL) {
584
0
                p_header = p_buffer;
585
0
                p_buffer += i_webp_header_size;
586
0
                i_vp8_data_size += pkt->data.frame.sz;
587
0
            }
588
589
0
            memcpy(p_buffer, pkt->data.frame.buf, pkt->data.frame.sz);
590
0
            p_block->i_dts = p_block->i_pts = pkt->data.frame.pts;
591
0
            if (keyframe)
592
0
                p_block->i_flags |= BLOCK_FLAG_TYPE_I;
593
594
            /* If Chunk Size is odd, a single padding byte -- that MUST be 0 to
595
               conform with RIFF -- is added. */
596
0
            if (b_needs_padding_byte)
597
0
                p_block->p_buffer[i_block_sz - 1] = 0;
598
599
0
            block_ChainAppend(&p_out, p_block);
600
0
        }
601
0
    }
602
603
    /* For WebP, now that we have the total size, write the RIFF header. */
604
0
    if (b_is_webp && p_header)
605
0
        webp_write_header(p_header, i_vp8_data_size, i_webp_header_size);
606
607
0
    vpx_img_free(&img);
608
0
    return p_out;
609
0
}
610
611
/*****************************************************************************
612
 * CloseEncoder: encoder destruction
613
 *****************************************************************************/
614
static void CloseEncoder(encoder_t *p_enc)
615
0
{
616
0
    encoder_sys_t *p_sys = p_enc->p_sys;
617
0
    if (vpx_codec_destroy(&p_sys->ctx))
618
0
        VPX_ERR(&p_enc->obj, &p_sys->ctx, "Failed to destroy codec");
619
0
    free(p_sys);
620
0
}
621
622
#endif  /* ENABLE_SOUT */