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