Coverage Report

Created: 2026-04-12 07:27

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/vlc/modules/demux/mp4/heif.c
Line
Count
Source
1
/*****************************************************************************
2
 * heif.c : ISO/IEC 23008-12 HEIF still picture demuxer
3
 *****************************************************************************
4
 * Copyright (C) 2018 Videolabs, VLC authors and VideoLAN
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, write to the Free Software Foundation,
18
 * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
19
 *****************************************************************************/
20
#ifdef HAVE_CONFIG_H
21
# include "config.h"
22
#endif
23
24
/*****************************************************************************
25
 * Preamble
26
 *****************************************************************************/
27
#include <vlc_common.h>
28
#include <vlc_arrays.h>
29
#include <vlc_demux.h>
30
#include <vlc_input.h>
31
#include <vlc_image.h>
32
#include <assert.h>
33
#include <limits.h>
34
35
#include "libmp4.h"
36
#include "heif.h"
37
#include "../../packetizer/iso_color_tables.h"
38
39
struct heif_private_t
40
{
41
    MP4_Box_t *p_root;
42
    es_out_id_t *id;
43
    vlc_tick_t i_pcr;
44
    vlc_tick_t i_end_display_time;
45
    vlc_tick_t i_image_duration;
46
    bool b_seekpoint_changed;
47
    uint32_t i_seekpoint;
48
    input_title_t *p_title;
49
50
    struct
51
    {
52
        MP4_Box_t *p_infe;
53
        es_format_t fmt;
54
        const MP4_Box_t *p_shared_header;
55
    } current;
56
};
57
58
static MP4_Box_t * NextAtom( MP4_Box_t *p_root,
59
                             vlc_fourcc_t i_type, const char *psz_path,
60
                             MP4_Box_t *p_infe )
61
87
{
62
87
    if( p_infe == NULL )
63
44
        p_infe = MP4_BoxGet( p_root, psz_path );
64
43
    else
65
43
        p_infe = p_infe->p_next;
66
111
    for( ; p_infe; p_infe = p_infe->p_next )
67
68
    {
68
68
        if( p_infe->i_type == i_type )
69
44
            return p_infe;
70
68
    }
71
43
    return NULL;
72
87
}
73
74
static MP4_Box_t * GetAtom( MP4_Box_t *p_root, MP4_Box_t *p_atom,
75
                            vlc_fourcc_t i_type, const char *psz_path,
76
                            bool(*pf_match)(const MP4_Box_t *, void *),
77
                            void *priv )
78
38
{
79
39
    while( (p_atom = NextAtom( p_root, i_type, psz_path, p_atom )) )
80
18
    {
81
18
        if( pf_match( p_atom, priv ) )
82
17
            return p_atom;
83
18
    }
84
21
    return NULL;
85
38
}
86
87
static bool MatchInfeID( const MP4_Box_t *p_infe, void *priv )
88
18
{
89
18
    return BOXDATA(p_infe)->i_item_id == *((uint32_t *) priv);
90
18
}
91
92
static bool MatchPureImage( const MP4_Box_t *p_infe, void *priv )
93
18
{
94
18
    MP4_Box_t *p_root = priv;
95
18
    const MP4_Box_t *p_iref = MP4_BoxGet( p_root, "meta/iref" );
96
18
    if( !p_iref )
97
18
        return true;
98
0
    for( const MP4_Box_t *p_refbox = p_iref->p_first;
99
0
                          p_refbox; p_refbox = p_refbox->p_next )
100
0
    {
101
0
        if( BOXDATA(p_refbox)->i_from_item_id == BOXDATA(p_infe)->i_item_id )
102
0
            return false;
103
0
    }
104
0
    return true;
105
0
}
106
107
static void SeekToPrevImageEnd( struct heif_private_t *p_sys, int i_picture )
108
0
{
109
0
    int i = 0;
110
0
    MP4_Box_t *p_infe = NULL;
111
0
    while( i < i_picture &&
112
0
          (p_infe = NextAtom( p_sys->p_root, ATOM_infe, "meta/iinf/infe", p_infe )) )
113
0
    {
114
0
        if( (BOXDATA(p_infe)->i_flags & 0x01) != 0x00 ||
115
0
                !MatchPureImage( p_infe, p_sys->p_root ) )
116
0
            continue;
117
0
        i++;
118
0
    }
119
0
    p_sys->current.p_infe = p_infe;
120
0
    p_sys->i_end_display_time = 0;
121
0
    p_sys->i_pcr = i * p_sys->i_image_duration;
122
0
}
123
124
static int ControlHEIF( demux_t *p_demux, int i_query, va_list args )
125
0
{
126
0
    struct heif_private_t *p_sys = (void *) p_demux->p_sys;
127
128
0
    switch( i_query )
129
0
    {
130
0
        case DEMUX_CAN_SEEK:
131
0
            *va_arg(args, bool *) = true;
132
0
            return VLC_SUCCESS;
133
0
        case DEMUX_GET_TITLE_INFO:
134
0
        {
135
0
            input_title_t ***ppp_title = va_arg( args, input_title_t *** );
136
0
            int *pi_int = va_arg( args, int* );
137
0
            int *pi_title_offset = va_arg( args, int* );
138
0
            int *pi_seekpoint_offset = va_arg( args, int* );
139
140
0
            if( !p_sys->p_title )
141
0
                return VLC_EGENERIC;
142
143
0
            *pi_int = 1;
144
0
            input_title_t **titles =  malloc(sizeof(*titles));
145
0
            if (titles == NULL)
146
0
                return VLC_ENOMEM;
147
148
0
            titles[0] = vlc_input_title_Duplicate(p_sys->p_title);
149
0
            if (titles[0] == NULL)
150
0
            {
151
0
                free(titles);
152
0
                return VLC_ENOMEM;
153
0
            }
154
155
0
            *ppp_title = titles;
156
0
            *pi_title_offset = 0;
157
0
            *pi_seekpoint_offset = 0;
158
0
            return VLC_SUCCESS;
159
0
        }
160
0
        case DEMUX_SET_TITLE:
161
0
        {
162
0
            const int i_title = va_arg( args, int );
163
0
            if( !p_sys->p_title || i_title != 0 )
164
0
                return VLC_EGENERIC;
165
0
            return VLC_SUCCESS;
166
0
        }
167
0
        case DEMUX_GET_SEEKPOINT:
168
0
            *va_arg( args, int * ) = p_sys->i_seekpoint;
169
0
            return VLC_SUCCESS;
170
0
        case DEMUX_SET_SEEKPOINT:
171
0
        {
172
0
            const int i_seekpoint = va_arg( args, int );
173
0
            if( !p_sys->p_title )
174
0
                return VLC_EGENERIC;
175
0
            SeekToPrevImageEnd( p_sys, i_seekpoint );
176
0
            return VLC_SUCCESS;
177
0
        }
178
0
        case DEMUX_TEST_AND_CLEAR_FLAGS:
179
0
        {
180
0
            unsigned *restrict flags = va_arg( args, unsigned * );
181
182
0
            if ((*flags & INPUT_UPDATE_SEEKPOINT) && p_sys->b_seekpoint_changed)
183
0
            {
184
0
                *flags = INPUT_UPDATE_SEEKPOINT;
185
0
                p_sys->b_seekpoint_changed = false;
186
0
            }
187
0
            else
188
0
                *flags = 0;
189
0
            return VLC_SUCCESS;
190
0
        }
191
0
        case DEMUX_GET_LENGTH:
192
0
            *(va_arg( args, vlc_tick_t * )) = p_sys->p_title->i_seekpoint *
193
0
                                              p_sys->i_image_duration;
194
0
            return VLC_SUCCESS;
195
0
        case DEMUX_GET_TIME:
196
0
            *(va_arg(args, vlc_tick_t *)) = p_sys->i_pcr;
197
0
            return VLC_SUCCESS;
198
0
        case DEMUX_SET_TIME:
199
0
        {
200
0
            SeekToPrevImageEnd( p_sys, va_arg(args, vlc_tick_t) /
201
0
                                p_sys->i_image_duration );
202
0
            return VLC_SUCCESS;
203
0
        }
204
0
        case DEMUX_GET_POSITION:
205
0
            if( !p_sys->p_title->i_seekpoint )
206
0
                return VLC_EGENERIC;
207
0
            *(va_arg(args, double *)) = (double) p_sys->i_pcr /
208
0
                    (p_sys->p_title->i_seekpoint * p_sys->i_image_duration);
209
0
            return VLC_SUCCESS;
210
0
        case DEMUX_SET_POSITION:
211
0
        {
212
0
            SeekToPrevImageEnd( p_sys,  va_arg(args, double) * p_sys->p_title->i_seekpoint );
213
0
            return VLC_SUCCESS;
214
0
        }
215
0
        case DEMUX_CAN_PAUSE:
216
0
        case DEMUX_SET_PAUSE_STATE:
217
0
        case DEMUX_CAN_CONTROL_PACE:
218
0
        case DEMUX_GET_PTS_DELAY:
219
0
            return demux_vaControlHelper( p_demux->s, 0, -1, 0, 1, i_query, args );
220
221
0
        default:
222
0
            return VLC_EGENERIC;
223
224
0
    }
225
0
}
226
227
//static int DemuxCompositeImage( demux_t *p_demux )
228
//{
229
230
//}
231
232
static block_t *ReadItemExtents( demux_t *p_demux, uint32_t i_item_id,
233
                                 const MP4_Box_t *p_shared_header )
234
5
{
235
5
    struct heif_private_t *p_sys = (void *) p_demux->p_sys;
236
5
    block_t *p_block = NULL;
237
238
5
    MP4_Box_t *p_iloc = MP4_BoxGet( p_sys->p_root, "meta/iloc" );
239
5
    if( !p_iloc )
240
0
        return p_block;
241
242
5
    for( uint32_t i=0; i<BOXDATA(p_iloc)->i_item_count; i++ )
243
5
    {
244
5
        if( BOXDATA(p_iloc)->p_items[i].i_item_id != i_item_id )
245
0
            continue;
246
247
5
        block_t **pp_append = &p_block;
248
249
        /* Shared prefix data, ex: JPEG */
250
5
        if( p_shared_header )
251
0
        {
252
0
            *pp_append = block_Alloc( p_shared_header->data.p_binary->i_blob );
253
0
            if( *pp_append )
254
0
            {
255
0
                memcpy( (*pp_append)->p_buffer,
256
0
                        p_shared_header->data.p_binary->p_blob,
257
0
                        p_shared_header->data.p_binary->i_blob );
258
0
                pp_append = &((*pp_append)->p_next);
259
0
            }
260
0
        }
261
262
10
        for( uint16_t j=0; j<BOXDATA(p_iloc)->p_items[i].i_extent_count; j++ )
263
5
        {
264
5
            uint64_t i_offset = BOXDATA(p_iloc)->p_items[i].i_base_offset +
265
5
                                BOXDATA(p_iloc)->p_items[i].p_extents[j].i_extent_offset;
266
5
            uint64_t i_length = BOXDATA(p_iloc)->p_items[i].p_extents[j].i_extent_length;
267
268
5
            if( BOXDATA(p_iloc)->p_items[i].i_construction_method < 2 )
269
5
            {
270
                /* Extents are in 0:file, 1:idat */
271
5
                if( BOXDATA(p_iloc)->p_items[i].i_construction_method == 1 )
272
0
                {
273
0
                    MP4_Box_t *idat = MP4_BoxGet( p_sys->p_root, "meta/idat" );
274
0
                    if(!idat)
275
0
                        break;
276
0
                    i_offset += idat->i_pos + mp4_box_headersize(idat);
277
0
                    if( i_length == 0 ) /* Entire container */
278
0
                        i_length = idat->i_size - mp4_box_headersize(idat);
279
0
                }
280
5
                else
281
5
                {
282
5
                    if( i_length == 0 ) /* Entire container == file */
283
5
                    {
284
5
                        if( vlc_stream_GetSize( p_demux->s, &i_length )
285
5
                                == VLC_SUCCESS && i_length > i_offset )
286
0
                            i_length -= i_offset;
287
5
                        else
288
5
                            i_length = 0;
289
5
                    }
290
5
                }
291
5
                if( vlc_stream_Seek( p_demux->s, i_offset ) != VLC_SUCCESS )
292
0
                    break;
293
5
                *pp_append = vlc_stream_Block( p_demux->s, i_length );
294
5
            }
295
            /* Extents are 3:iloc reference */
296
0
            else if( BOXDATA(p_iloc)->p_items[i].i_construction_method == 2 )
297
0
            {
298
                /* FIXME ? That's totally untested and really complicated */
299
0
                uint32_t i_extent_index = BOXDATA(p_iloc)->p_items[i].p_extents[j].i_extent_index;
300
0
                if(i_extent_index == 0)
301
0
                    i_extent_index = 1; /* Inferred. Indexes start 1 */
302
0
                const MP4_Box_t *p_iref = MP4_BoxGet( p_sys->p_root, "meta/iref" );
303
0
                if(!p_iref)
304
0
                    break;
305
0
                for( const MP4_Box_t *p_refbox = p_iref->p_first;
306
0
                                      p_refbox; p_refbox = p_refbox->p_next )
307
0
                {
308
0
                    if( p_refbox->i_type != VLC_FOURCC('i','l','o','c') ||
309
0
                        BOXDATA(p_refbox)->i_from_item_id == i_item_id )
310
0
                        continue;
311
312
0
                    for( uint16_t k=0; k< BOXDATA(p_refbox)->i_reference_count; k++ )
313
0
                    {
314
0
                        if( --i_extent_index > 0 )
315
0
                            continue;
316
0
                        if( BOXDATA(p_refbox)->p_references[k].i_to_item_id != i_item_id )
317
0
                        {
318
0
                            *pp_append = ReadItemExtents(p_demux,
319
0
                                            BOXDATA(p_refbox)->p_references[k].i_to_item_id,
320
0
                                            NULL);
321
0
                        }
322
0
                    }
323
324
0
                    break;
325
0
                }
326
0
            }
327
328
5
            while( *pp_append )
329
0
                pp_append = &((*pp_append)->p_next);
330
5
        }
331
5
        break;
332
5
    }
333
334
5
    if( p_block )
335
0
        p_block = block_ChainGather( p_block );
336
337
5
    return p_block;
338
5
}
339
340
static int SetPictureProperties( demux_t *p_demux, uint32_t i_item_id,
341
                                 es_format_t *fmt, const MP4_Box_t **p_header )
342
5
{
343
5
    struct heif_private_t *p_sys = (void *) p_demux->p_sys;
344
345
5
    const MP4_Box_t *p_ipma = MP4_BoxGet( p_sys->p_root, "meta/iprp/ipma" );
346
5
    if( !p_ipma )
347
0
        return VLC_EGENERIC;
348
349
    /* Load properties */
350
10
    for( uint32_t i=0; i<BOXDATA(p_ipma)->i_entry_count; i++ )
351
5
    {
352
5
        if( BOXDATA(p_ipma)->p_entries[i].i_item_id != i_item_id )
353
0
            continue;
354
355
15
        for( uint8_t j=0; j<BOXDATA(p_ipma)->p_entries[i].i_association_count; j++ )
356
10
        {
357
10
            if( !BOXDATA(p_ipma)->p_entries[i].p_assocs[j].i_property_index )
358
10
                continue;
359
360
0
            const MP4_Box_t *p_prop = MP4_BoxGetVa( p_sys->p_root, "meta/iprp/ipco/[%u]",
361
0
                BOXDATA(p_ipma)->p_entries[i].p_assocs[j].i_property_index - 1 );
362
0
            if( !p_prop )
363
0
                continue;
364
365
0
            switch( p_prop->i_type )
366
0
            {
367
0
                case ATOM_hvcC:
368
0
                case ATOM_avcC:
369
0
                    if( !fmt->p_extra && p_prop->data.p_binary &&
370
0
                       ((fmt->i_codec == VLC_CODEC_HEVC && p_prop->i_type == ATOM_hvcC) ||
371
0
                        (fmt->i_codec == VLC_CODEC_H264 && p_prop->i_type == ATOM_avcC) ))
372
0
                    {
373
0
                        fmt->p_extra = malloc( p_prop->data.p_binary->i_blob );
374
0
                        if( fmt->p_extra )
375
0
                        {
376
0
                            fmt->i_extra = p_prop->data.p_binary->i_blob;
377
0
                            memcpy( fmt->p_extra, p_prop->data.p_binary->p_blob, fmt->i_extra );
378
0
                        }
379
0
                    }
380
0
                    break;
381
0
                case ATOM_av1C:
382
0
                    if( fmt->i_codec == VLC_CODEC_AV1 && !fmt->i_extra &&
383
0
                        p_prop->data.p_av1C->i_av1C >= 4 )
384
0
                    {
385
0
                        fmt->p_extra = malloc( p_prop->data.p_av1C->i_av1C );
386
0
                        if( fmt->p_extra )
387
0
                        {
388
0
                            fmt->i_extra = p_prop->data.p_av1C->i_av1C ;
389
0
                            memcpy( fmt->p_extra, p_prop->data.p_av1C->p_av1C, fmt->i_extra );
390
0
                        }
391
0
                    }
392
0
                    break;
393
0
                case ATOM_jpeC:
394
0
                    if( fmt->i_codec == VLC_CODEC_JPEG )
395
0
                        *p_header = p_prop;
396
0
                    break;
397
0
                case ATOM_ispe:
398
0
                    if (p_prop->data.p_ispe->i_width <= fmt->video.i_width &&
399
0
                        p_prop->data.p_ispe->i_height <= fmt->video.i_height)
400
0
                    {
401
0
                        fmt->video.i_visible_width = p_prop->data.p_ispe->i_width;
402
0
                        fmt->video.i_visible_height = p_prop->data.p_ispe->i_height;
403
0
                    }
404
0
                    break;
405
0
                case ATOM_clap:
406
0
                    if(p_prop->data.p_clap->i_width + p_prop->data.p_clap->i_x_offset <= fmt->video.i_width &&
407
0
                       p_prop->data.p_clap->i_height + p_prop->data.p_clap->i_y_offset <= fmt->video.i_height)
408
0
                    {
409
0
                        fmt->video.i_visible_width = p_prop->data.p_clap->i_width;
410
0
                        fmt->video.i_visible_height = p_prop->data.p_clap->i_height;
411
0
                        fmt->video.i_x_offset = p_prop->data.p_clap->i_x_offset;
412
0
                        fmt->video.i_y_offset = p_prop->data.p_clap->i_y_offset;
413
0
                    }
414
0
                    break;
415
0
                case ATOM_pasp:
416
0
                    if( p_prop->data.p_pasp->i_horizontal_spacing &&
417
0
                        p_prop->data.p_pasp->i_vertical_spacing )
418
0
                    {
419
0
                        fmt->video.i_sar_num = p_prop->data.p_pasp->i_horizontal_spacing;
420
0
                        fmt->video.i_sar_den = p_prop->data.p_pasp->i_vertical_spacing;
421
0
                    }
422
0
                    break;
423
0
                case ATOM_irot:
424
0
                    switch( p_prop->data.p_irot->i_ccw_degrees % 360 )
425
0
                    {
426
0
                        default:
427
0
                        case 0:   fmt->video.orientation = ORIENT_NORMAL ; break;
428
0
                        case 90:  fmt->video.orientation = ORIENT_ROTATED_270; break;
429
0
                        case 180: fmt->video.orientation = ORIENT_ROTATED_180 ; break;
430
0
                        case 270: fmt->video.orientation = ORIENT_ROTATED_90 ; break;
431
0
                    }
432
0
                    break;
433
0
                case ATOM_colr:
434
0
                    fmt->video.primaries = iso_23001_8_cp_to_vlc_primaries(
435
0
                                            p_prop->data.p_colr->nclc.i_primary_idx );
436
0
                    fmt->video.transfer = iso_23001_8_tc_to_vlc_xfer(
437
0
                                            p_prop->data.p_colr->nclc.i_transfer_function_idx );
438
0
                    fmt->video.space = iso_23001_8_mc_to_vlc_coeffs(
439
0
                                        p_prop->data.p_colr->nclc.i_matrix_idx );
440
0
                    fmt->video.color_range = p_prop->data.p_colr->nclc.i_full_range ?
441
0
                                COLOR_RANGE_FULL : COLOR_RANGE_LIMITED;
442
0
                    break;
443
0
                case ATOM_clli:
444
0
                    fmt->video.lighting.MaxCLL = p_prop->data.p_CoLL->i_maxCLL;
445
0
                    fmt->video.lighting.MaxFALL = p_prop->data.p_CoLL->i_maxFALL;
446
0
                    break;
447
0
                case ATOM_mdcv:
448
0
                    memcpy( fmt->video.mastering.primaries,
449
0
                            p_prop->data.p_SmDm->primaries, sizeof(fmt->video.mastering.primaries) );
450
0
                    memcpy( fmt->video.mastering.white_point,
451
0
                            p_prop->data.p_SmDm->white_point, sizeof(fmt->video.mastering.white_point) );
452
0
                    fmt->video.mastering.max_luminance = p_prop->data.p_SmDm->i_luminanceMax;
453
0
                    fmt->video.mastering.min_luminance = p_prop->data.p_SmDm->i_luminanceMin;
454
0
                    break;
455
0
            }
456
0
        }
457
5
    }
458
459
5
    fmt->video.i_frame_rate      = 1000;
460
5
    fmt->video.i_frame_rate_base = p_sys->i_image_duration / 1000;
461
462
5
    return VLC_SUCCESS;
463
5
}
464
465
static int SetupPicture( demux_t *p_demux, const MP4_Box_t *p_infe,
466
                         es_format_t *fmt, const MP4_Box_t **p_header )
467
16
{
468
16
    fmt->i_codec = 0;
469
16
    *p_header = NULL;
470
471
16
    const uint32_t i_item_id = BOXDATA(p_infe)->i_item_id;
472
16
    const char *psz_mime = BOXDATA(p_infe)->psz_content_type;
473
16
    switch( BOXDATA(p_infe)->item_type )
474
16
    {
475
0
        case VLC_FOURCC('h','v','c','1'):
476
0
            es_format_Init( fmt, VIDEO_ES, VLC_CODEC_HEVC );
477
0
            break;
478
0
        case VLC_FOURCC('a','v','c','1'):
479
0
            es_format_Init( fmt, VIDEO_ES, VLC_CODEC_H264 );
480
0
            break;
481
0
        case ATOM_av01:
482
0
            es_format_Init( fmt, VIDEO_ES, VLC_CODEC_AV1 );
483
0
            break;
484
5
        case VLC_FOURCC('j','p','e','g'):
485
5
            es_format_Init( fmt, VIDEO_ES, VLC_CODEC_JPEG );
486
5
            break;
487
11
        default:
488
11
            if( psz_mime )
489
0
            {
490
0
                if( !strcasecmp( "image/jpeg", psz_mime ) )
491
0
                    es_format_Init( fmt, VIDEO_ES, VLC_CODEC_JPEG );
492
0
                else if( !strcasecmp( "image/avif", psz_mime ) )
493
0
                    es_format_Init( fmt, VIDEO_ES, VLC_CODEC_AV1 );
494
0
            }
495
11
            break;
496
16
    }
497
498
16
    if( fmt->i_codec == 0 )
499
11
        return VLC_EGENERIC;
500
501
5
    return SetPictureProperties( p_demux, i_item_id, fmt, p_header );
502
16
}
503
504
union heif_derivation_data
505
{
506
    struct
507
    {
508
        uint8_t rows_minus_one;
509
        uint8_t columns_minus_one;
510
        uint32_t output_width;
511
        uint32_t output_height;
512
    } ImageGrid;
513
};
514
515
static int ReadDerivationData_Grid( const uint8_t *p_data, size_t i_data,
516
                                    union heif_derivation_data *d )
517
0
{
518
0
    if( i_data < 8 || p_data[0] != 0x00 )
519
0
        return VLC_EGENERIC;
520
521
0
    uint8_t i_fieldlength = ((p_data[1] & 0x01) + 1) << 1;
522
    /* length is either 2 or 4 bytes */
523
0
    d->ImageGrid.rows_minus_one = p_data[2];
524
0
    d->ImageGrid.columns_minus_one = p_data[3];
525
0
    if(i_fieldlength == 2)
526
0
    {
527
0
        d->ImageGrid.output_width = GetWBE(&p_data[4]);
528
0
        d->ImageGrid.output_height = GetWBE(&p_data[6]);
529
0
    }
530
0
    else
531
0
    {
532
0
        if(i_data < 12)
533
0
            return VLC_EGENERIC;
534
0
        d->ImageGrid.output_width = GetDWBE(&p_data[4]);
535
0
        d->ImageGrid.output_height = GetDWBE(&p_data[8]);
536
0
    }
537
0
    return VLC_SUCCESS;
538
0
}
539
540
static int ReadDerivationData( demux_t *p_demux, vlc_fourcc_t type,
541
                               uint32_t i_item_id,
542
                               union heif_derivation_data *d )
543
0
{
544
0
    int i_ret = VLC_EGENERIC;
545
0
    block_t *p_data = ReadItemExtents( p_demux, i_item_id, NULL );
546
0
    if( p_data )
547
0
    {
548
0
        switch( type )
549
0
        {
550
0
            case VLC_FOURCC('g','r','i','d'):
551
0
                i_ret = ReadDerivationData_Grid( p_data->p_buffer,
552
0
                                                 p_data->i_buffer, d );
553
                /* fallthrough */
554
0
            default:
555
0
                break;
556
0
        }
557
0
        block_Release( p_data );
558
0
    }
559
0
    return i_ret;
560
0
}
561
562
static int LoadGridImage( demux_t *p_demux,
563
                          image_handler_t *handler,
564
                          uint32_t i_pic_item_id,
565
                               uint8_t *p_buffer,
566
                               unsigned tile, unsigned gridcols,
567
                               unsigned imagewidth, unsigned imageheight )
568
0
{
569
0
    struct heif_private_t *p_sys = (void *) p_demux->p_sys;
570
571
0
    MP4_Box_t *p_infe = GetAtom( p_sys->p_root, NULL,
572
0
                                 ATOM_infe, "meta/iinf/infe",
573
0
                                 MatchInfeID, &i_pic_item_id );
574
0
    if( !p_infe )
575
0
        return VLC_EGENERIC;
576
577
0
    es_format_t fmt;
578
0
    es_format_Init(&fmt, UNKNOWN_ES, 0);
579
580
0
    const MP4_Box_t *p_shared_header = NULL;
581
0
    if( SetupPicture( p_demux, p_infe, &fmt, &p_shared_header ) != VLC_SUCCESS )
582
0
    {
583
0
        es_format_Clean( &fmt );
584
0
        return VLC_EGENERIC; /* Unsupported picture, goto next */
585
0
    }
586
587
0
    block_t *p_sample = ReadItemExtents( p_demux, i_pic_item_id,
588
0
                                         p_shared_header );
589
0
    if(!p_sample)
590
0
    {
591
0
        es_format_Clean( &fmt );
592
0
        return VLC_EGENERIC;
593
0
    }
594
595
0
    video_format_t decoded;
596
0
    video_format_Init( &decoded, VLC_CODEC_RGBA );
597
598
0
    fmt.video.i_chroma = fmt.i_codec;
599
600
0
    picture_t *p_picture = image_Read( handler, p_sample, &fmt, &decoded );
601
602
0
    es_format_Clean( &fmt );
603
604
0
    if ( !p_picture )
605
0
        return VLC_EGENERIC;
606
607
0
    const unsigned tilewidth = p_picture->format.i_visible_width;
608
0
    const unsigned tileheight = p_picture->format.i_visible_height;
609
0
    uint8_t *dstline = p_buffer;
610
0
    dstline += (tile / gridcols) * (imagewidth * tileheight * 4);
611
0
    for(;1;)
612
0
    {
613
0
        const unsigned offsetpxw = (tile % gridcols) * tilewidth;
614
0
        const unsigned offsetpxh = (tile / gridcols) * tileheight;
615
0
        if( offsetpxw > imagewidth )
616
0
            break;
617
0
        const uint8_t *srcline = p_picture->p[0].p_pixels +
618
0
                                 p_picture->format.i_y_offset * p_picture->p[0].i_pitch +
619
0
                                 p_picture->format.i_x_offset * 4;
620
0
        unsigned tocopylines = p_picture->p[0].i_visible_lines;
621
0
        if(offsetpxh + tocopylines >= imageheight)
622
0
            tocopylines = imageheight - offsetpxh;
623
0
        for(unsigned i=0; i<tocopylines; i++)
624
0
        {
625
0
            size_t tocopypx = tilewidth;
626
0
            if( offsetpxw + tilewidth > imagewidth )
627
0
                tocopypx = imagewidth - offsetpxw;
628
0
            memcpy( &dstline[offsetpxw * 4], srcline, tocopypx * 4 );
629
0
            dstline += imagewidth * 4;
630
0
            srcline += p_picture->p[0].i_pitch;
631
0
        }
632
633
0
        break;
634
0
    }
635
636
0
    picture_Release( p_picture );
637
638
0
    return VLC_SUCCESS;
639
0
}
640
641
static int DerivedImageAssembleGrid( demux_t *p_demux, uint32_t i_grid_item_id,
642
                                     es_format_t *fmt, block_t **pp_block )
643
0
{
644
0
    struct heif_private_t *p_sys = (void *) p_demux->p_sys;
645
646
0
    const MP4_Box_t *p_iref = MP4_BoxGet( p_sys->p_root, "meta/iref" );
647
0
    if(!p_iref)
648
0
        return VLC_EGENERIC;
649
650
0
    const MP4_Box_t *p_refbox;
651
0
    for( p_refbox = p_iref->p_first; p_refbox; p_refbox = p_refbox->p_next )
652
0
    {
653
0
        if( p_refbox->i_type == VLC_FOURCC('d','i','m','g') &&
654
0
            BOXDATA(p_refbox)->i_from_item_id == i_grid_item_id )
655
0
            break;
656
0
    }
657
658
0
    if(!p_refbox)
659
0
        return VLC_EGENERIC;
660
661
0
    union heif_derivation_data derivation_data;
662
0
    if( ReadDerivationData( p_demux,
663
0
                            p_sys->current.BOXDATA(p_infe)->item_type,
664
0
                            i_grid_item_id, &derivation_data ) != VLC_SUCCESS )
665
0
        return VLC_EGENERIC;
666
667
0
    msg_Dbg(p_demux,"%ux%upx image %ux%u tiles composition",
668
0
            derivation_data.ImageGrid.output_width,
669
0
            derivation_data.ImageGrid.output_height,
670
0
            derivation_data.ImageGrid.columns_minus_one + 1,
671
0
            derivation_data.ImageGrid.columns_minus_one + 1);
672
673
0
    image_handler_t *handler = image_HandlerCreate( p_demux );
674
0
    if( !handler )
675
0
        return VLC_EGENERIC;
676
677
0
    block_t *p_block = block_Alloc( derivation_data.ImageGrid.output_width *
678
0
                                    derivation_data.ImageGrid.output_height * 4 );
679
0
    if( !p_block )
680
0
    {
681
0
        image_HandlerDelete( handler );
682
0
        return VLC_EGENERIC;
683
0
    }
684
0
    *pp_block = p_block;
685
686
0
    es_format_Init( fmt, VIDEO_ES, VLC_CODEC_RGBA );
687
0
    fmt->video.i_width =
688
0
    fmt->video.i_visible_width = derivation_data.ImageGrid.output_width;
689
0
    fmt->video.i_height =
690
0
    fmt->video.i_visible_height = derivation_data.ImageGrid.output_height;
691
692
0
    for( uint16_t i=0; i<BOXDATA(p_refbox)->i_reference_count; i++ )
693
0
    {
694
0
        msg_Dbg( p_demux, "Loading tile %d/%d", i,
695
0
                 (derivation_data.ImageGrid.rows_minus_one + 1) *
696
0
                 (derivation_data.ImageGrid.columns_minus_one + 1) );
697
0
        LoadGridImage( p_demux, handler,
698
0
                       BOXDATA(p_refbox)->p_references[i].i_to_item_id,
699
0
                       p_block->p_buffer, i,
700
0
                       derivation_data.ImageGrid.columns_minus_one + 1,
701
0
                       derivation_data.ImageGrid.output_width,
702
0
                       derivation_data.ImageGrid.output_height );
703
0
    }
704
705
0
    SetPictureProperties( p_demux, i_grid_item_id, fmt, NULL );
706
707
0
    image_HandlerDelete( handler );
708
709
0
    return VLC_SUCCESS;
710
0
}
711
712
static int DemuxHEIF( demux_t *p_demux )
713
38
{
714
38
    struct heif_private_t *p_sys = (void *) p_demux->p_sys;
715
716
    /* Displaying a picture */
717
38
    if( p_sys->i_end_display_time > 0 )
718
0
    {
719
0
        bool b_empty = true;
720
0
        int ret = es_out_Control( p_demux->out, ES_OUT_DRAIN );
721
0
        if( ret == VLC_SUCCESS )
722
0
            es_out_Control( p_demux->out, ES_OUT_IS_EMPTY, &b_empty );
723
0
        if( !b_empty || vlc_tick_now() <= p_sys->i_end_display_time )
724
0
        {
725
0
            vlc_tick_sleep( VLC_TICK_FROM_MS(40) );
726
0
            return VLC_DEMUXER_SUCCESS;
727
0
        }
728
0
        p_sys->i_end_display_time = 0;
729
0
    }
730
731
    /* Reset prev pic params */
732
38
    p_sys->current.p_shared_header = NULL;
733
734
    /* First or next picture */
735
38
    if( !p_sys->current.p_infe )
736
22
    {
737
22
        MP4_Box_t *p_pitm = MP4_BoxGet( p_sys->p_root, "meta/pitm" );
738
22
        if( !p_pitm )
739
0
            return VLC_DEMUXER_EOF;
740
741
22
        p_sys->current.p_infe = GetAtom( p_sys->p_root, NULL,
742
22
                                         ATOM_infe, "meta/iinf/infe",
743
22
                                         MatchInfeID, &BOXDATA(p_pitm)->i_item_id );
744
22
    }
745
16
    else
746
16
    {
747
16
        p_sys->current.p_infe = GetAtom( p_sys->p_root, p_sys->current.p_infe,
748
16
                                         ATOM_infe, "meta/iinf/infe",
749
16
                                         MatchPureImage, p_sys->p_root );
750
16
    }
751
752
38
    if( !p_sys->current.p_infe )
753
21
        return VLC_DEMUXER_EOF;
754
755
17
    const uint32_t i_current_item_id = p_sys->current.BOXDATA(p_infe)->i_item_id;
756
17
    const MP4_Box_t *p_ipco = MP4_BoxGet( p_sys->p_root, "meta/iprp/ipco" );
757
17
    if( !p_ipco )
758
1
        return VLC_DEMUXER_EOF;
759
760
16
    es_format_t fmt;
761
16
    es_format_Init(&fmt, UNKNOWN_ES, 0);
762
763
16
    block_t *p_block = NULL;
764
16
    if( p_sys->current.BOXDATA(p_infe)->item_type == VLC_FOURCC('g','r','i','d') )
765
0
    {
766
0
        if( DerivedImageAssembleGrid( p_demux, i_current_item_id,
767
0
                                      &fmt, &p_block ) != VLC_SUCCESS )
768
0
        {
769
0
            es_format_Clean( &fmt );
770
0
            return VLC_DEMUXER_SUCCESS;
771
0
        }
772
0
    }
773
16
    else
774
16
    {
775
16
        if( SetupPicture( p_demux, p_sys->current.p_infe,
776
16
                          &fmt, &p_sys->current.p_shared_header ) != VLC_SUCCESS )
777
11
        {
778
11
            es_format_Clean( &fmt );
779
11
            return VLC_DEMUXER_SUCCESS;
780
11
        }
781
782
5
        p_block = ReadItemExtents( p_demux, i_current_item_id,
783
5
                                   p_sys->current.p_shared_header );
784
5
        if( !p_block )
785
5
        {
786
5
            es_format_Clean( &fmt );
787
5
            return VLC_DEMUXER_SUCCESS; /* Goto next picture */
788
5
        }
789
5
    }
790
791
0
    es_format_Clean( &p_sys->current.fmt );
792
0
    es_format_Copy( &p_sys->current.fmt, &fmt );
793
0
    es_format_Clean( &fmt );
794
0
    if( p_sys->id )
795
0
        es_out_Del( p_demux->out, p_sys->id );
796
0
    p_sys->id = es_out_Add( p_demux->out, &p_sys->current.fmt );
797
798
0
    if( !p_sys->id )
799
0
    {
800
0
        p_sys->current.p_infe = NULL; /* Goto next picture */
801
0
        return VLC_DEMUXER_SUCCESS;
802
0
    }
803
804
0
    if( p_sys->i_pcr == VLC_TICK_INVALID )
805
0
    {
806
0
        p_sys->i_pcr = VLC_TICK_0;
807
0
        es_out_SetPCR( p_demux->out, p_sys->i_pcr );
808
0
    }
809
810
0
    p_block->i_dts = p_block->i_pts = p_sys->i_pcr;
811
0
    p_block->i_length = p_sys->i_image_duration;
812
813
0
    p_block->i_flags |= BLOCK_FLAG_END_OF_SEQUENCE;
814
815
0
    p_sys->i_end_display_time = vlc_tick_now() + p_block->i_length;
816
0
    p_sys->b_seekpoint_changed = true;
817
818
0
    p_sys->i_pcr = p_block->i_dts + p_block->i_length;
819
0
    es_out_Send( p_demux->out, p_sys->id, p_block );
820
0
    es_out_SetPCR( p_demux->out, p_sys->i_pcr );
821
822
0
    return VLC_DEMUXER_SUCCESS;
823
0
}
824
825
int OpenHEIF( vlc_object_t * p_this )
826
2.70k
{
827
2.70k
    demux_t  *p_demux = (demux_t *)p_this;
828
2.70k
    const uint8_t *p_peek;
829
830
2.70k
    if( vlc_stream_Peek( p_demux->s, &p_peek, 12 ) < 12 )
831
0
        return VLC_EGENERIC;
832
833
2.70k
    if( VLC_FOURCC( p_peek[4], p_peek[5], p_peek[6], p_peek[7] ) != ATOM_ftyp )
834
2.53k
        return VLC_EGENERIC;
835
836
171
    switch( VLC_FOURCC( p_peek[8], p_peek[9], p_peek[10], p_peek[11] ) )
837
171
    {
838
0
        case BRAND_mif1:
839
0
        case BRAND_heic:
840
0
        case BRAND_heix:
841
0
        case BRAND_jpeg:
842
0
        case BRAND_avci:
843
22
        case BRAND_avif:
844
22
            break;
845
0
        case BRAND_msf1:
846
1
        case BRAND_hevc:
847
1
        case BRAND_hevx:
848
1
        case BRAND_avcs:
849
1
        case BRAND_avis:
850
149
        default:
851
149
            return VLC_EGENERIC;
852
171
    }
853
854
22
    MP4_Box_t *p_root = MP4_BoxGetRoot( p_demux->s );
855
22
    if( !p_root )
856
0
        return VLC_EGENERIC;
857
858
22
    MP4_BoxDumpStructure( VLC_OBJECT(p_demux), p_root );
859
860
22
    struct heif_private_t *p_sys = calloc( 1, sizeof(*p_sys) );
861
22
    p_demux->p_sys = (void *) p_sys;
862
22
    p_sys->p_root = p_root;
863
22
    p_sys->p_title = vlc_input_title_New();
864
22
    if( !p_sys->p_title )
865
0
    {
866
0
        free( p_sys );
867
0
        return VLC_ENOMEM;
868
0
    }
869
870
22
    p_sys->i_image_duration = vlc_tick_from_sec(var_InheritFloat( p_demux, "heif-image-duration" ));
871
22
    if( p_sys->i_image_duration <= 0 )
872
0
        p_sys->i_image_duration = VLC_TICK_FROM_SEC(HEIF_DEFAULT_DURATION);
873
874
22
    MP4_Box_t *p_infe = NULL;
875
48
    while( (p_infe = NextAtom( p_root, ATOM_infe, "meta/iinf/infe", p_infe )) )
876
26
    {
877
26
        if( (BOXDATA(p_infe)->i_flags & 0x01) != 0x00 ||
878
18
                !MatchPureImage( p_infe, p_root ) )
879
8
            continue;
880
18
        seekpoint_t *s = vlc_seekpoint_New();
881
18
        if( s )
882
18
        {
883
18
            s->i_time_offset = p_sys->p_title->i_seekpoint * p_sys->i_image_duration;
884
18
            if( BOXDATA(p_infe)->psz_item_name )
885
6
                s->psz_name = strdup( BOXDATA(p_infe)->psz_item_name );
886
18
            TAB_APPEND( p_sys->p_title->i_seekpoint, p_sys->p_title->seekpoint, s );
887
18
        }
888
18
    }
889
890
22
    es_format_Init( &p_sys->current.fmt, UNKNOWN_ES, 0 );
891
892
22
    p_demux->pf_demux = DemuxHEIF;
893
22
    p_demux->pf_control = ControlHEIF;
894
895
22
    return VLC_SUCCESS;
896
22
}
897
898
void CloseHEIF ( vlc_object_t * p_this )
899
22
{
900
22
    demux_t *p_demux = (demux_t *)p_this;
901
22
    struct heif_private_t *p_sys = (void *) p_demux->p_sys;
902
22
    MP4_BoxFree( p_sys->p_root );
903
22
    if( p_sys->id )
904
0
        es_out_Del( p_demux->out, p_sys->id );
905
22
    es_format_Clean( &p_sys->current.fmt );
906
22
    vlc_input_title_Delete( p_sys->p_title );
907
22
    free( p_sys );
908
22
}