Coverage Report

Created: 2023-06-07 06:30

/src/vlc/modules/codec/ttml/imageupdater.h
Line
Count
Source (jump to first uncovered line)
1
/*****************************************************************************
2
 * imageupdater.h : TTML image to SPU updater
3
 *****************************************************************************
4
 * Copyright © 2018 Videolabs, VideoLAN and VLC authors
5
 *
6
 * This program is free software; you can redistribute it and/or modify it
7
 * under the terms of the GNU Lesser General Public License as published by
8
 * the Free Software Foundation; either version 2.1 of the License, or
9
 * (at your option) any later version.
10
 *
11
 * This program is distributed in the hope that it will be useful,
12
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14
 * GNU Lesser General Public License for more details.
15
 *
16
 * You should have received a copy of the GNU Lesser General Public License
17
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
18
 *****************************************************************************/
19
#include <vlc_subpicture.h>
20
21
typedef struct ttml_image_updater_region_t ttml_image_updater_region_t;
22
23
struct ttml_image_updater_region_t
24
{
25
    struct
26
    {
27
        float x;
28
        float y;
29
    } origin, extent;
30
    int i_flags;
31
    picture_t *p_pic;
32
    ttml_image_updater_region_t *p_next;
33
};
34
35
enum ttml_image_updater_region_flags_e
36
{
37
    ORIGIN_X_IS_RATIO      = 1 << 0,
38
    ORIGIN_Y_IS_RATIO      = 1 << 1,
39
    EXTENT_X_IS_RATIO      = 1 << 2,
40
    EXTENT_Y_IS_RATIO      = 1 << 3,
41
};
42
43
static void TTML_ImageUpdaterRegionDelete(ttml_image_updater_region_t *p_updtregion)
44
0
{
45
0
    if (p_updtregion->p_pic)
46
0
        picture_Release(p_updtregion->p_pic);
47
0
    free(p_updtregion);
48
0
}
49
50
static ttml_image_updater_region_t *TTML_ImageUpdaterRegionNew(picture_t *p_pic)
51
0
{
52
0
    ttml_image_updater_region_t *p_region = calloc(1, sizeof(*p_region));
53
0
    if(p_region)
54
0
        p_region->p_pic = p_pic;
55
0
    return p_region;
56
0
}
57
58
/*
59
 * UPDATER
60
*/
61
62
typedef struct
63
{
64
    ttml_image_updater_region_t *p_regions;
65
    ttml_image_updater_region_t **pp_append;
66
67
} ttml_image_updater_sys_t;
68
69
static void TTML_ImageSpuAppendRegion(ttml_image_updater_sys_t *p_sys,
70
                                      ttml_image_updater_region_t *p_new)
71
0
{
72
0
    *p_sys->pp_append = p_new;
73
0
    p_sys->pp_append = &p_new->p_next;
74
0
}
75
76
static int TTML_ImageSpuValidate(subpicture_t *p_spu,
77
                                 bool b_src_changed, const video_format_t *p_fmt_src,
78
                                 bool b_dst_changed, const video_format_t *p_fmt_dst,
79
                                 vlc_tick_t ts)
80
0
{
81
0
    VLC_UNUSED(p_spu);
82
0
    VLC_UNUSED(b_src_changed); VLC_UNUSED(p_fmt_src);
83
0
    VLC_UNUSED(p_fmt_dst);
84
0
    VLC_UNUSED(ts);
85
0
    return b_dst_changed ? VLC_EGENERIC: VLC_SUCCESS;
86
0
}
87
88
static void TTML_ImageSpuUpdate(subpicture_t *p_spu,
89
                                const video_format_t *p_fmt_src,
90
                                const video_format_t *p_fmt_dst,
91
                                vlc_tick_t i_ts)
92
0
{
93
0
    VLC_UNUSED(p_fmt_src); VLC_UNUSED(p_fmt_dst);
94
0
    VLC_UNUSED(i_ts);
95
0
    ttml_image_updater_sys_t *p_sys = p_spu->updater.p_sys;
96
0
    subpicture_region_t **pp_last_region = &p_spu->p_region;
97
98
    /* !WARN: SMPTE-TT image profile requires no scaling, and even it
99
              would, it does not store the necessary original pic size */
100
101
0
    for(ttml_image_updater_region_t *p_updtregion = p_sys->p_regions;
102
0
                                     p_updtregion; p_updtregion = p_updtregion->p_next)
103
0
    {
104
0
        subpicture_region_t *r = subpicture_region_New(&p_updtregion->p_pic->format);
105
0
        if (!r)
106
0
            return;
107
0
        picture_Release(r->p_picture);
108
0
        r->p_picture = picture_Clone(p_updtregion->p_pic);
109
0
        if(!r->p_picture)
110
0
        {
111
0
            subpicture_region_Delete(r);
112
0
            return;
113
0
        }
114
115
0
        r->i_align = SUBPICTURE_ALIGN_LEFT|SUBPICTURE_ALIGN_TOP;
116
117
0
        if( p_updtregion->i_flags & ORIGIN_X_IS_RATIO )
118
0
            r->i_x = p_updtregion->origin.x * p_fmt_dst->i_visible_width;
119
0
        else
120
0
            r->i_x = p_updtregion->origin.x;
121
122
0
        if( p_updtregion->i_flags & ORIGIN_Y_IS_RATIO )
123
0
            r->i_y = p_updtregion->origin.y * p_fmt_dst->i_visible_height;
124
0
        else
125
0
            r->i_y = p_updtregion->origin.y;
126
127
0
        *pp_last_region = r;
128
0
        pp_last_region = &r->p_next;
129
0
    }
130
0
}
131
132
static void TTML_ImageSpuDestroy(subpicture_t *p_spu)
133
0
{
134
0
    ttml_image_updater_sys_t *p_sys = p_spu->updater.p_sys;
135
0
    while(p_sys->p_regions)
136
0
    {
137
0
        ttml_image_updater_region_t *p_next = p_sys->p_regions->p_next;
138
0
        TTML_ImageUpdaterRegionDelete(p_sys->p_regions);
139
0
        p_sys->p_regions = p_next;
140
0
    }
141
0
    free(p_sys);
142
0
}
143
144
static inline subpicture_t *decoder_NewTTML_ImageSpu(decoder_t *p_dec)
145
0
{
146
0
    ttml_image_updater_sys_t *p_sys = calloc(1, sizeof(*p_sys));
147
0
    if(!p_sys)
148
0
        return NULL;
149
0
    subpicture_updater_t updater = {
150
0
        .pf_validate = TTML_ImageSpuValidate,
151
0
        .pf_update   = TTML_ImageSpuUpdate,
152
0
        .pf_destroy  = TTML_ImageSpuDestroy,
153
0
        .p_sys       = p_sys,
154
0
    };
155
0
    p_sys->p_regions = NULL;
156
0
    p_sys->pp_append = &p_sys->p_regions;
157
0
    subpicture_t *p_spu = decoder_NewSubpicture(p_dec, &updater);
158
0
    if (!p_spu)
159
0
        free(p_sys);
160
0
    return p_spu;
161
0
}