| 1 |
|
|---|
| 2 |
|
|---|
| 3 |
|
|---|
| 4 |
|
|---|
| 5 |
|
|---|
| 6 |
|
|---|
| 7 |
|
|---|
| 8 |
|
|---|
| 9 |
|
|---|
| 10 |
|
|---|
| 11 |
|
|---|
| 12 |
|
|---|
| 13 |
|
|---|
| 14 |
|
|---|
| 15 |
|
|---|
| 16 |
|
|---|
| 17 |
|
|---|
| 18 |
|
|---|
| 19 |
|
|---|
| 20 |
|
|---|
| 21 |
|
|---|
| 22 |
|
|---|
| 23 |
|
|---|
| 24 |
|
|---|
| 25 |
|
|---|
| 26 |
|
|---|
| 27 |
|
|---|
| 28 |
#ifdef HAVE_CONFIG_H |
|---|
| 29 |
# include "config.h" |
|---|
| 30 |
#endif |
|---|
| 31 |
|
|---|
| 32 |
#include <vlc_common.h> |
|---|
| 33 |
#include <vlc_plugin.h> |
|---|
| 34 |
#include <vlc_demux.h> |
|---|
| 35 |
#include <vlc_meta.h> |
|---|
| 36 |
#include <vlc_input.h> |
|---|
| 37 |
#include <vlc_codec.h> |
|---|
| 38 |
#include <assert.h> |
|---|
| 39 |
#include <vlc_charset.h> |
|---|
| 40 |
|
|---|
| 41 |
|
|---|
| 42 |
|
|---|
| 43 |
|
|---|
| 44 |
static int Open ( vlc_object_t * ); |
|---|
| 45 |
static void Close ( vlc_object_t * ); |
|---|
| 46 |
|
|---|
| 47 |
vlc_module_begin(); |
|---|
| 48 |
set_description( N_("FLAC demuxer") ); |
|---|
| 49 |
set_capability( "demux", 155 ); |
|---|
| 50 |
set_category( CAT_INPUT ); |
|---|
| 51 |
set_subcategory( SUBCAT_INPUT_DEMUX ); |
|---|
| 52 |
set_callbacks( Open, Close ); |
|---|
| 53 |
add_shortcut( "flac" ); |
|---|
| 54 |
vlc_module_end(); |
|---|
| 55 |
|
|---|
| 56 |
|
|---|
| 57 |
|
|---|
| 58 |
|
|---|
| 59 |
static int Demux ( demux_t * ); |
|---|
| 60 |
static int Control( demux_t *, int, va_list ); |
|---|
| 61 |
|
|---|
| 62 |
static int ReadMeta( demux_t *, uint8_t **pp_streaminfo, int *pi_streaminfo ); |
|---|
| 63 |
|
|---|
| 64 |
struct demux_sys_t |
|---|
| 65 |
{ |
|---|
| 66 |
bool b_start; |
|---|
| 67 |
es_out_id_t *p_es; |
|---|
| 68 |
|
|---|
| 69 |
|
|---|
| 70 |
decoder_t *p_packetizer; |
|---|
| 71 |
|
|---|
| 72 |
vlc_meta_t *p_meta; |
|---|
| 73 |
audio_replay_gain_t replay_gain; |
|---|
| 74 |
|
|---|
| 75 |
int64_t i_time_offset; |
|---|
| 76 |
int64_t i_pts; |
|---|
| 77 |
int64_t i_pts_start; |
|---|
| 78 |
|
|---|
| 79 |
int64_t i_length; |
|---|
| 80 |
int64_t i_data_pos; |
|---|
| 81 |
|
|---|
| 82 |
|
|---|
| 83 |
int i_seekpoint; |
|---|
| 84 |
seekpoint_t **seekpoint; |
|---|
| 85 |
|
|---|
| 86 |
|
|---|
| 87 |
int i_attachments; |
|---|
| 88 |
input_attachment_t **attachments; |
|---|
| 89 |
int i_cover_idx; |
|---|
| 90 |
int i_cover_score; |
|---|
| 91 |
}; |
|---|
| 92 |
|
|---|
| 93 |
#define STREAMINFO_SIZE 38 |
|---|
| 94 |
#define FLAC_PACKET_SIZE 16384 |
|---|
| 95 |
|
|---|
| 96 |
|
|---|
| 97 |
|
|---|
| 98 |
|
|---|
| 99 |
static int Open( vlc_object_t * p_this ) |
|---|
| 100 |
{ |
|---|
| 101 |
demux_t *p_demux = (demux_t*)p_this; |
|---|
| 102 |
demux_sys_t *p_sys; |
|---|
| 103 |
const uint8_t *p_peek; |
|---|
| 104 |
uint8_t *p_streaminfo; |
|---|
| 105 |
int i_streaminfo; |
|---|
| 106 |
es_format_t fmt; |
|---|
| 107 |
|
|---|
| 108 |
|
|---|
| 109 |
if( stream_Peek( p_demux->s, &p_peek, 4 ) < 4 ) return VLC_EGENERIC; |
|---|
| 110 |
|
|---|
| 111 |
if( p_peek[0]!='f' || p_peek[1]!='L' || p_peek[2]!='a' || p_peek[3]!='C' ) |
|---|
| 112 |
{ |
|---|
| 113 |
if( !p_demux->b_force ) return VLC_EGENERIC; |
|---|
| 114 |
|
|---|
| 115 |
|
|---|
| 116 |
msg_Err( p_demux, "this doesn't look like a flac stream, " |
|---|
| 117 |
"continuing anyway" ); |
|---|
| 118 |
} |
|---|
| 119 |
|
|---|
| 120 |
p_demux->pf_demux = Demux; |
|---|
| 121 |
p_demux->pf_control = Control; |
|---|
| 122 |
p_demux->p_sys = p_sys = malloc( sizeof( demux_sys_t ) ); |
|---|
| 123 |
p_sys->b_start = true; |
|---|
| 124 |
p_sys->p_meta = NULL; |
|---|
| 125 |
memset( &p_sys->replay_gain, 0, sizeof(p_sys->replay_gain) ); |
|---|
| 126 |
p_sys->i_length = 0; |
|---|
| 127 |
p_sys->i_time_offset = 0; |
|---|
| 128 |
p_sys->i_pts = 0; |
|---|
| 129 |
p_sys->i_pts_start = 0; |
|---|
| 130 |
p_sys->p_es = NULL; |
|---|
| 131 |
TAB_INIT( p_sys->i_seekpoint, p_sys->seekpoint ); |
|---|
| 132 |
TAB_INIT( p_sys->i_attachments, p_sys->attachments); |
|---|
| 133 |
p_sys->i_cover_idx = 0; |
|---|
| 134 |
p_sys->i_cover_score = 0; |
|---|
| 135 |
|
|---|
| 136 |
|
|---|
| 137 |
if( ReadMeta( p_demux, &p_streaminfo, &i_streaminfo ) ) |
|---|
| 138 |
{ |
|---|
| 139 |
free( p_sys ); |
|---|
| 140 |
return VLC_EGENERIC; |
|---|
| 141 |
} |
|---|
| 142 |
|
|---|
| 143 |
|
|---|
| 144 |
|
|---|
| 145 |
p_streaminfo[4] |= 0x80; |
|---|
| 146 |
es_format_Init( &fmt, AUDIO_ES, VLC_FOURCC( 'f', 'l', 'a', 'c' ) ); |
|---|
| 147 |
fmt.i_extra = i_streaminfo; |
|---|
| 148 |
fmt.p_extra = p_streaminfo; |
|---|
| 149 |
|
|---|
| 150 |
p_sys->p_packetizer = demux_PacketizerNew( p_demux, &fmt, "flac" ); |
|---|
| 151 |
if( !p_sys->p_packetizer ) |
|---|
| 152 |
{ |
|---|
| 153 |
free( p_sys ); |
|---|
| 154 |
return VLC_EGENERIC; |
|---|
| 155 |
} |
|---|
| 156 |
|
|---|
| 157 |
if( p_sys->i_cover_idx < p_sys->i_attachments ) |
|---|
| 158 |
{ |
|---|
| 159 |
char psz_url[128]; |
|---|
| 160 |
if( !p_sys->p_meta ) |
|---|
| 161 |
p_sys->p_meta = vlc_meta_New(); |
|---|
| 162 |
snprintf( psz_url, sizeof(psz_url), "attachment://%s", |
|---|
| 163 |
p_sys->attachments[p_sys->i_cover_idx]->psz_name ); |
|---|
| 164 |
vlc_meta_Set( p_sys->p_meta, vlc_meta_ArtworkURL, psz_url ); |
|---|
| 165 |
} |
|---|
| 166 |
vlc_audio_replay_gain_MergeFromMeta( &p_sys->replay_gain, p_sys->p_meta ); |
|---|
| 167 |
return VLC_SUCCESS; |
|---|
| 168 |
} |
|---|
| 169 |
|
|---|
| 170 |
|
|---|
| 171 |
|
|---|
| 172 |
|
|---|
| 173 |
static void Close( vlc_object_t * p_this ) |
|---|
| 174 |
{ |
|---|
| 175 |
demux_t *p_demux = (demux_t*)p_this; |
|---|
| 176 |
demux_sys_t *p_sys = p_demux->p_sys; |
|---|
| 177 |
|
|---|
| 178 |
TAB_CLEAN( p_sys->i_seekpoint, p_sys->seekpoint ); |
|---|
| 179 |
|
|---|
| 180 |
int i; |
|---|
| 181 |
for( i = 0; i < p_sys->i_attachments; i++ ) |
|---|
| 182 |
free( p_sys->attachments[i] ); |
|---|
| 183 |
TAB_CLEAN( p_sys->i_attachments, p_sys->attachments); |
|---|
| 184 |
|
|---|
| 185 |
|
|---|
| 186 |
demux_PacketizerDestroy( p_sys->p_packetizer ); |
|---|
| 187 |
|
|---|
| 188 |
if( p_sys->p_meta ) |
|---|
| 189 |
vlc_meta_Delete( p_sys->p_meta ); |
|---|
| 190 |
free( p_sys ); |
|---|
| 191 |
} |
|---|
| 192 |
|
|---|
| 193 |
|
|---|
| 194 |
|
|---|
| 195 |
|
|---|
| 196 |
|
|---|
| 197 |
|
|---|
| 198 |
static int Demux( demux_t *p_demux ) |
|---|
| 199 |
{ |
|---|
| 200 |
demux_sys_t *p_sys = p_demux->p_sys; |
|---|
| 201 |
block_t *p_block_in, *p_block_out; |
|---|
| 202 |
|
|---|
| 203 |
if( !( p_block_in = stream_Block( p_demux->s, FLAC_PACKET_SIZE ) ) ) |
|---|
| 204 |
return 0; |
|---|
| 205 |
|
|---|
| 206 |
p_block_in->i_pts = p_block_in->i_dts = p_sys->b_start ? 1 : 0; |
|---|
| 207 |
p_sys->b_start = false; |
|---|
| 208 |
|
|---|
| 209 |
while( (p_block_out = p_sys->p_packetizer->pf_packetize( |
|---|
| 210 |
p_sys->p_packetizer, &p_block_in )) ) |
|---|
| 211 |
{ |
|---|
| 212 |
while( p_block_out ) |
|---|
| 213 |
{ |
|---|
| 214 |
block_t *p_next = p_block_out->p_next; |
|---|
| 215 |
|
|---|
| 216 |
p_block_out->p_next = NULL; |
|---|
| 217 |
|
|---|
| 218 |
if( p_sys->p_es == NULL ) |
|---|
| 219 |
{ |
|---|
| 220 |
p_sys->p_packetizer->fmt_out.b_packetized = true; |
|---|
| 221 |
p_sys->p_packetizer->fmt_out.audio_replay_gain = p_sys->replay_gain; |
|---|
| 222 |
p_sys->p_es = es_out_Add( p_demux->out, &p_sys->p_packetizer->fmt_out); |
|---|
| 223 |
} |
|---|
| 224 |
|
|---|
| 225 |
p_sys->i_pts = p_block_out->i_dts; |
|---|
| 226 |
|
|---|
| 227 |
|
|---|
| 228 |
p_block_out->i_pts += p_sys->i_time_offset; |
|---|
| 229 |
p_block_out->i_dts += p_sys->i_time_offset; |
|---|
| 230 |
|
|---|
| 231 |
|
|---|
| 232 |
if( p_block_out->i_dts >= p_sys->i_pts_start + p_sys->i_time_offset ) |
|---|
| 233 |
es_out_Control( p_demux->out, ES_OUT_SET_PCR, p_block_out->i_dts ); |
|---|
| 234 |
else |
|---|
| 235 |
es_out_Control( p_demux->out, ES_OUT_RESET_PCR ); |
|---|
| 236 |
|
|---|
| 237 |
es_out_Send( p_demux->out, p_sys->p_es, p_block_out ); |
|---|
| 238 |
|
|---|
| 239 |
p_block_out = p_next; |
|---|
| 240 |
} |
|---|
| 241 |
} |
|---|
| 242 |
return 1; |
|---|
| 243 |
} |
|---|
| 244 |
|
|---|
| 245 |
|
|---|
| 246 |
|
|---|
| 247 |
|
|---|
| 248 |
static int64_t ControlGetLength( demux_t *p_demux ) |
|---|
| 249 |
{ |
|---|
| 250 |
demux_sys_t *p_sys = p_demux->p_sys; |
|---|
| 251 |
const int64_t i_size = stream_Size(p_demux->s) - p_sys->i_data_pos; |
|---|
| 252 |
int64_t i_length = p_sys->i_length; |
|---|
| 253 |
int i; |
|---|
| 254 |
|
|---|
| 255 |
|
|---|
| 256 |
for( i = p_sys->i_seekpoint-1; i >= 0; i-- ) |
|---|
| 257 |
{ |
|---|
| 258 |
seekpoint_t *s = p_sys->seekpoint[i]; |
|---|
| 259 |
if( s->i_byte_offset <= i_size ) |
|---|
| 260 |
{ |
|---|
| 261 |
if( i+1 < p_sys->i_seekpoint ) |
|---|
| 262 |
{ |
|---|
| 263 |
|
|---|
| 264 |
seekpoint_t *n = p_sys->seekpoint[i+1]; |
|---|
| 265 |
assert( n->i_byte_offset != s->i_byte_offset); |
|---|
| 266 |
i_length = s->i_time_offset + (n->i_time_offset-s->i_time_offset) * (i_size-s->i_byte_offset) / (n->i_byte_offset-s->i_byte_offset); |
|---|
| 267 |
} |
|---|
| 268 |
break; |
|---|
| 269 |
} |
|---|
| 270 |
} |
|---|
| 271 |
return i_length; |
|---|
| 272 |
} |
|---|
| 273 |
|
|---|
| 274 |
static int64_t ControlGetTime( demux_t *p_demux ) |
|---|
| 275 |
{ |
|---|
| 276 |
demux_sys_t *p_sys = p_demux->p_sys; |
|---|
| 277 |
return __MAX(p_sys->i_pts, p_sys->i_pts_start) + p_sys->i_time_offset; |
|---|
| 278 |
} |
|---|
| 279 |
|
|---|
| 280 |
static int ControlSetTime( demux_t *p_demux, int64_t i_time ) |
|---|
| 281 |
{ |
|---|
| 282 |
demux_sys_t *p_sys = p_demux->p_sys; |
|---|
| 283 |
int64_t i_delta_time; |
|---|
| 284 |
bool b_seekable; |
|---|
| 285 |
int i; |
|---|
| 286 |
|
|---|
| 287 |
|
|---|
| 288 |
stream_Control( p_demux->s, STREAM_CAN_SEEK, &b_seekable ); |
|---|
| 289 |
if( !b_seekable ) |
|---|
| 290 |
return VLC_EGENERIC; |
|---|
| 291 |
|
|---|
| 292 |
|
|---|
| 293 |
assert( p_sys->i_seekpoint > 0 ); |
|---|
| 294 |
for( i = p_sys->i_seekpoint-1; i >= 0; i-- ) |
|---|
| 295 |
{ |
|---|
| 296 |
if( p_sys->seekpoint[i]->i_time_offset <= i_time ) |
|---|
| 297 |
break; |
|---|
| 298 |
} |
|---|
| 299 |
i_delta_time = i_time - p_sys->seekpoint[i]->i_time_offset; |
|---|
| 300 |
|
|---|
| 301 |
|
|---|
| 302 |
if( i_delta_time < 45*INT64_C(1000000) ) |
|---|
| 303 |
{ |
|---|
| 304 |
if( stream_Seek( p_demux->s, p_sys->seekpoint[i]->i_byte_offset+p_sys->i_data_pos ) ) |
|---|
| 305 |
return VLC_EGENERIC; |
|---|
| 306 |
|
|---|
| 307 |
p_sys->i_time_offset = p_sys->seekpoint[i]->i_time_offset - p_sys->i_pts; |
|---|
| 308 |
p_sys->i_pts_start = p_sys->i_pts+i_delta_time; |
|---|
| 309 |
es_out_Control( p_demux->out, ES_OUT_SET_NEXT_DISPLAY_TIME, p_sys->p_es, p_sys->i_pts_start + p_sys->i_time_offset ); |
|---|
| 310 |
} |
|---|
| 311 |
else |
|---|
| 312 |
{ |
|---|
| 313 |
int64_t i_delta_offset; |
|---|
| 314 |
int64_t i_next_time; |
|---|
| 315 |
int64_t i_next_offset; |
|---|
| 316 |
|
|---|
| 317 |
if( i+1 < p_sys->i_seekpoint ) |
|---|
| 318 |
{ |
|---|
| 319 |
i_next_time = p_sys->seekpoint[i+1]->i_time_offset; |
|---|
| 320 |
i_next_offset = p_sys->seekpoint[i+1]->i_byte_offset; |
|---|
| 321 |
} |
|---|
| 322 |
else |
|---|
| 323 |
{ |
|---|
| 324 |
i_next_time = p_sys->i_length; |
|---|
| 325 |
i_next_offset = stream_Size(p_demux->s)-p_sys->i_data_pos; |
|---|
| 326 |
} |
|---|
| 327 |
|
|---|
| 328 |
i_delta_offset = 0; |
|---|
| 329 |
if( i_next_time-p_sys->seekpoint[i]->i_time_offset > 0 ) |
|---|
| 330 |
i_delta_offset = (i_next_offset - p_sys->seekpoint[i]->i_byte_offset) * i_delta_time / |
|---|
| 331 |
(i_next_time-p_sys->seekpoint[i]->i_time_offset); |
|---|
| 332 |
|
|---|
| 333 |
if( stream_Seek( p_demux->s, p_sys->seekpoint[i]->i_byte_offset+p_sys->i_data_pos + i_delta_offset ) ) |
|---|
| 334 |
return VLC_EGENERIC; |
|---|
| 335 |
|
|---|
| 336 |
p_sys->i_pts_start = p_sys->i_pts; |
|---|
| 337 |
p_sys->i_time_offset = (p_sys->seekpoint[i]->i_time_offset+i_delta_time) - p_sys->i_pts; |
|---|
| 338 |
} |
|---|
| 339 |
return VLC_SUCCESS; |
|---|
| 340 |
} |
|---|
| 341 |
|
|---|
| 342 |
static int Control( demux_t *p_demux, int i_query, va_list args ) |
|---|
| 343 |
{ |
|---|
| 344 |
demux_sys_t *p_sys = p_demux->p_sys; |
|---|
| 345 |
|
|---|
| 346 |
if( i_query == DEMUX_GET_META ) |
|---|
| 347 |
{ |
|---|
| 348 |
vlc_meta_t *p_meta = (vlc_meta_t *)va_arg( args, vlc_meta_t* ); |
|---|
| 349 |
if( p_demux->p_sys->p_meta ) |
|---|
| 350 |
vlc_meta_Merge( p_meta, p_demux->p_sys->p_meta ); |
|---|
| 351 |
return VLC_SUCCESS; |
|---|
| 352 |
} |
|---|
| 353 |
else if( i_query == DEMUX_HAS_UNSUPPORTED_META ) |
|---|
| 354 |
{ |
|---|
| 355 |
bool *pb_bool = (bool*)va_arg( args, bool* ); |
|---|
| 356 |
*pb_bool = true; |
|---|
| 357 |
return VLC_SUCCESS; |
|---|
| 358 |
} |
|---|
| 359 |
else if( i_query == DEMUX_GET_LENGTH ) |
|---|
| 360 |
{ |
|---|
| 361 |
int64_t *pi64 = (int64_t*)va_arg( args, int64_t * ); |
|---|
| 362 |
*pi64 = ControlGetLength( p_demux ); |
|---|
| 363 |
return VLC_SUCCESS; |
|---|
| 364 |
} |
|---|
| 365 |
else if( i_query == DEMUX_SET_TIME ) |
|---|
| 366 |
{ |
|---|
| 367 |
int64_t i_time = (int64_t)va_arg( args, int64_t ); |
|---|
| 368 |
return ControlSetTime( p_demux, i_time ); |
|---|
| 369 |
} |
|---|
| 370 |
else if( i_query == DEMUX_SET_POSITION ) |
|---|
| 371 |
{ |
|---|
| 372 |
const double f = (double)va_arg( args, double ); |
|---|
| 373 |
int64_t i_time = f * ControlGetLength( p_demux ); |
|---|
| 374 |
return ControlSetTime( p_demux, i_time ); |
|---|
| 375 |
} |
|---|
| 376 |
else if( i_query == DEMUX_GET_TIME ) |
|---|
| 377 |
{ |
|---|
| 378 |
int64_t *pi64 = (int64_t*)va_arg( args, int64_t * ); |
|---|
| 379 |
*pi64 = ControlGetTime( p_demux ); |
|---|
| 380 |
return VLC_SUCCESS; |
|---|
| 381 |
} |
|---|
| 382 |
else if( i_query == DEMUX_GET_POSITION ) |
|---|
| 383 |
{ |
|---|
| 384 |
double *pf = (double*)va_arg( args, double * ); |
|---|
| 385 |
const int64_t i_length = ControlGetLength(p_demux); |
|---|
| 386 |
if( i_length > 0 ) |
|---|
| 387 |
*pf = (double)ControlGetTime(p_demux) / (double)i_length; |
|---|
| 388 |
else |
|---|
| 389 |
*pf= 0.0; |
|---|
| 390 |
return VLC_SUCCESS; |
|---|
| 391 |
} |
|---|
| 392 |
else if( i_query == DEMUX_GET_ATTACHMENTS ) |
|---|
| 393 |
{ |
|---|
| 394 |
input_attachment_t ***ppp_attach = |
|---|
| 395 |
(input_attachment_t***)va_arg( args, input_attachment_t*** ); |
|---|
| 396 |
int *pi_int = (int*)va_arg( args, int * ); |
|---|
| 397 |
int i; |
|---|
| 398 |
|
|---|
| 399 |
if( p_sys->i_attachments <= 0 ) |
|---|
| 400 |
return VLC_EGENERIC; |
|---|
| 401 |
|
|---|
| 402 |
*pi_int = p_sys->i_attachments;; |
|---|
| 403 |
*ppp_attach = malloc( sizeof(input_attachment_t**) * p_sys->i_attachments ); |
|---|
| 404 |
for( i = 0; i < p_sys->i_attachments; i++ ) |
|---|
| 405 |
(*ppp_attach)[i] = vlc_input_attachment_Duplicate( p_sys->attachments[i] ); |
|---|
| 406 |
return VLC_SUCCESS; |
|---|
| 407 |
} |
|---|
| 408 |
|
|---|
| 409 |
return demux_vaControlHelper( p_demux->s, p_sys->i_data_pos, -1, |
|---|
| 410 |
8*0, 1, i_query, args ); |
|---|
| 411 |
} |
|---|
| 412 |
|
|---|
| 413 |
enum |
|---|
| 414 |
{ |
|---|
| 415 |
META_STREAMINFO = 0, |
|---|
| 416 |
META_SEEKTABLE = 3, |
|---|
| 417 |
META_COMMENT = 4, |
|---|
| 418 |
META_PICTURE = 6, |
|---|
| 419 |
}; |
|---|
| 420 |
|
|---|
| 421 |
static inline int Get24bBE( const uint8_t *p ) |
|---|
| 422 |
{ |
|---|
| 423 |
return (p[0] << 16)|(p[1] << 8)|(p[2]); |
|---|
| 424 |
} |
|---|
| 425 |
|
|---|
| 426 |
static void ParseStreamInfo( int *pi_rate, int64_t *pi_count, uint8_t *p_data ); |
|---|
| 427 |
static void ParseSeekTable( demux_t *p_demux, const uint8_t *p_data, int i_data, |
|---|
| 428 |
int i_sample_rate ); |
|---|
| 429 |
static void ParseComment( demux_t *, const uint8_t *p_data, int i_data ); |
|---|
| 430 |
static void ParsePicture( demux_t *, const uint8_t *p_data, int i_data ); |
|---|
| 431 |
|
|---|
| 432 |
static int ReadMeta( demux_t *p_demux, uint8_t **pp_streaminfo, int *pi_streaminfo ) |
|---|
| 433 |
{ |
|---|
| 434 |
demux_sys_t *p_sys = p_demux->p_sys; |
|---|
| 435 |
int i_peek; |
|---|
| 436 |
const uint8_t *p_peek; |
|---|
| 437 |
bool b_last; |
|---|
| 438 |
int i_sample_rate; |
|---|
| 439 |
int64_t i_sample_count; |
|---|
| 440 |
seekpoint_t *s; |
|---|
| 441 |
|
|---|
| 442 |
|
|---|
| 443 |
i_peek = stream_Peek( p_demux->s, &p_peek, 8 ); |
|---|
| 444 |
if( (p_peek[4] & 0x7F) != META_STREAMINFO ) |
|---|
| 445 |
{ |
|---|
| 446 |
msg_Err( p_demux, "this isn't a STREAMINFO metadata block" ); |
|---|
| 447 |
return VLC_EGENERIC; |
|---|
| 448 |
} |
|---|
| 449 |
if( Get24bBE(&p_peek[5]) != (STREAMINFO_SIZE - 4) ) |
|---|
| 450 |
{ |
|---|
| 451 |
msg_Err( p_demux, "invalid size for a STREAMINFO metadata block" ); |
|---|
| 452 |
return VLC_EGENERIC; |
|---|
| 453 |
} |
|---|
| 454 |
|
|---|
| 455 |
*pi_streaminfo = 4 + STREAMINFO_SIZE; |
|---|
| 456 |
*pp_streaminfo = malloc( 4 + STREAMINFO_SIZE ); |
|---|
| 457 |
if( *pp_streaminfo == NULL ) |
|---|
| 458 |
return VLC_EGENERIC; |
|---|
| 459 |
|
|---|
| 460 |
if( stream_Read( p_demux->s, *pp_streaminfo, 4+STREAMINFO_SIZE ) != 4+STREAMINFO_SIZE ) |
|---|
| 461 |
{ |
|---|
| 462 |
msg_Err( p_demux, "failed to read STREAMINFO metadata block" ); |
|---|
| 463 |
free( *pp_streaminfo ); |
|---|
| 464 |
return VLC_EGENERIC; |
|---|
| 465 |
} |
|---|
| 466 |
|
|---|
| 467 |
|
|---|
| 468 |
ParseStreamInfo( &i_sample_rate, &i_sample_count, *pp_streaminfo ); |
|---|
| 469 |
if( i_sample_rate > 0 ) |
|---|
| 470 |
p_sys->i_length = i_sample_count * INT64_C(1000000)/i_sample_rate; |
|---|
| 471 |
|
|---|
| 472 |
|
|---|
| 473 |
s = vlc_seekpoint_New(); |
|---|
| 474 |
s->i_time_offset = 0; |
|---|
| 475 |
s->i_byte_offset = 0; |
|---|
| 476 |
TAB_APPEND( p_sys->i_seekpoint, p_sys->seekpoint, s ); |
|---|
| 477 |
|
|---|
| 478 |
b_last = (*pp_streaminfo)[4]&0x80; |
|---|
| 479 |
while( !b_last ) |
|---|
| 480 |
{ |
|---|
| 481 |
int i_len; |
|---|
| 482 |
int i_type; |
|---|
| 483 |
|
|---|
| 484 |
i_peek = stream_Peek( p_demux->s, &p_peek, 4 ); |
|---|
| 485 |
if( i_peek < 4 ) |
|---|
| 486 |
break; |
|---|
| 487 |
b_last = p_peek[0]&0x80; |
|---|
| 488 |
i_type = p_peek[0]&0x7f; |
|---|
| 489 |
i_len = Get24bBE( &p_peek[1] ); |
|---|
| 490 |
|
|---|
| 491 |
if( i_type == META_SEEKTABLE ) |
|---|
| 492 |
{ |
|---|
| 493 |
i_peek = stream_Peek( p_demux->s, &p_peek, 4+i_len ); |
|---|
| 494 |
if( i_peek == 4+i_len ) |
|---|
| 495 |
ParseSeekTable( p_demux, p_peek, i_peek, i_sample_rate ); |
|---|
| 496 |
} |
|---|
| 497 |
else if( i_type == META_COMMENT ) |
|---|
| 498 |
{ |
|---|
| 499 |
i_peek = stream_Peek( p_demux->s, &p_peek, 4+i_len ); |
|---|
| 500 |
if( i_peek == 4+i_len ) |
|---|
| 501 |
ParseComment( p_demux, p_peek, i_peek ); |
|---|
| 502 |
} |
|---|
| 503 |
else if( i_type == META_PICTURE ) |
|---|
| 504 |
{ |
|---|
| 505 |
i_peek = stream_Peek( p_demux->s, &p_peek, 4+i_len ); |
|---|
| 506 |
if( i_peek == 4+i_len ) |
|---|
| 507 |
ParsePicture( p_demux, p_peek, i_peek ); |
|---|
| 508 |
} |
|---|
| 509 |
|
|---|
| 510 |
if( stream_Read( p_demux->s, NULL, 4+i_len ) < 4+i_len ) |
|---|
| 511 |
break; |
|---|
| 512 |
} |
|---|
| 513 |
|
|---|
| 514 |
|
|---|
| 515 |
p_sys->i_data_pos = stream_Tell( p_demux->s ); |
|---|
| 516 |
|
|---|
| 517 |
return VLC_SUCCESS; |
|---|
| 518 |
} |
|---|
| 519 |
static void ParseStreamInfo( int *pi_rate, int64_t *pi_count, uint8_t *p_data ) |
|---|
| 520 |
{ |
|---|
| 521 |
const int i_skip = 4+4; |
|---|
| 522 |
|
|---|
| 523 |
*pi_rate = GetDWBE(&p_data[i_skip+4+6]) >> 12; |
|---|
| 524 |
*pi_count = GetQWBE(&p_data[i_skip+4+6]) & ((INT64_C(1)<<36)-1); |
|---|
| 525 |
} |
|---|
| 526 |
|
|---|
| 527 |
static void ParseSeekTable( demux_t *p_demux, const uint8_t *p_data, int i_data, |
|---|
| 528 |
int i_sample_rate ) |
|---|
| 529 |
{ |
|---|
| 530 |
demux_sys_t *p_sys = p_demux->p_sys; |
|---|
| 531 |
seekpoint_t *s; |
|---|
| 532 |
int i; |
|---|
| 533 |
|
|---|
| 534 |
if( i_sample_rate <= 0 ) |
|---|
| 535 |
return; |
|---|
| 536 |
|
|---|
| 537 |
|
|---|
| 538 |
for( i = 0; i < (i_data-4)/18; i++ ) |
|---|
| 539 |
{ |
|---|
| 540 |
const int64_t i_sample = GetQWBE( &p_data[4+18*i+0] ); |
|---|
| 541 |
int j; |
|---|
| 542 |
|
|---|
| 543 |
if( i_sample < 0 || i_sample >= INT64_MAX ) |
|---|
| 544 |
continue; |
|---|
| 545 |
|
|---|
| 546 |
s = vlc_seekpoint_New(); |
|---|
| 547 |
s->i_time_offset = i_sample * INT64_C(1000000)/i_sample_rate; |
|---|
| 548 |
s->i_byte_offset = GetQWBE( &p_data[4+18*i+8] ); |
|---|
| 549 |
|
|---|
| 550 |
|
|---|
| 551 |
for( j = 0; j < p_sys->i_seekpoint; j++ ) |
|---|
| 552 |
{ |
|---|
| 553 |
if( p_sys->seekpoint[j]->i_time_offset == s->i_time_offset || |
|---|
| 554 |
p_sys->seekpoint[j]->i_byte_offset == s->i_byte_offset ) |
|---|
| 555 |
{ |
|---|
| 556 |
vlc_seekpoint_Delete( s ); |
|---|
| 557 |
s = NULL; |
|---|
| 558 |
break; |
|---|
| 559 |
} |
|---|
| 560 |
} |
|---|
| 561 |
if( s ) |
|---|
| 562 |
{ |
|---|
| 563 |
TAB_APPEND( p_sys->i_seekpoint, p_sys->seekpoint, s ); |
|---|
| 564 |
} |
|---|
| 565 |
} |
|---|
| 566 |
|
|---|
| 567 |
} |
|---|
| 568 |
|
|---|
| 569 |
#define RM(x) do { i_data -= (x); p_data += (x); } while(0) |
|---|
| 570 |
static void ParseComment( demux_t *p_demux, const uint8_t *p_data, int i_data ) |
|---|
| 571 |
{ |
|---|
| 572 |
demux_sys_t *p_sys = p_demux->p_sys; |
|---|
| 573 |
int n; |
|---|
| 574 |
int i_comment; |
|---|
| 575 |
|
|---|
| 576 |
if( i_data < 8 ) |
|---|
| 577 |
return; |
|---|
| 578 |
|
|---|
| 579 |
RM(4); |
|---|
| 580 |
|
|---|
| 581 |
n = GetDWLE(p_data); RM(4); |
|---|
| 582 |
if( n < 0 || n > i_data ) |
|---|
| 583 |
return; |
|---|
| 584 |
#if 0 |
|---|
| 585 |
if( n > 0 ) |
|---|
| 586 |
{ |
|---|
| 587 |
|
|---|
| 588 |
char *psz_vendor = psz_vendor = strndup( p_data, n ); |
|---|
| 589 |
msg_Dbg( p_demux, "FLAC: COMMENT vendor length=%d vendor=%s\n", n, psz_vendor ); |
|---|
| 590 |
free( psz_vendor ); |
|---|
| 591 |
} |
|---|
| 592 |
#endif |
|---|
| 593 |
RM(n); |
|---|
| 594 |
|
|---|
| 595 |
if( i_data < 4 ) |
|---|
| 596 |
return; |
|---|
| 597 |
|
|---|
| 598 |
i_comment = GetDWLE(p_data); RM(4); |
|---|
| 599 |
if( i_comment <= 0 ) |
|---|
| 600 |
return; |
|---|
| 601 |
|
|---|
| 602 |
p_sys->p_meta = vlc_meta_New(); |
|---|
| 603 |
|
|---|
| 604 |
for( ; i_comment > 0; i_comment-- ) |
|---|
| 605 |
{ |
|---|
| 606 |
char *psz; |
|---|
| 607 |
if( i_data < 4 ) |
|---|
| 608 |
break; |
|---|
| 609 |
n = GetDWLE(p_data); RM(4); |
|---|
| 610 |
if( n > i_data ) |
|---|
| 611 |
break; |
|---|
| 612 |
if( n <= 0 ) |
|---|
| 613 |
continue; |
|---|
| 614 |
|
|---|
| 615 |
psz = strndup( (const char*)p_data, n ); |
|---|
| 616 |
RM(n); |
|---|
| 617 |
|
|---|
| 618 |
EnsureUTF8( psz ); |
|---|
| 619 |
|
|---|
| 620 |
#define IF_EXTRACT(txt,var) \ |
|---|
| 621 |
if( !strncasecmp(psz, txt, strlen(txt)) ) \ |
|---|
| 622 |
{ \ |
|---|
| 623 |
const char *oldval = vlc_meta_Get( p_sys->p_meta, vlc_meta_ ## var ); \ |
|---|
| 624 |
if( oldval ) \ |
|---|
| 625 |
{ \ |
|---|
| 626 |
char * newval; \ |
|---|
| 627 |
if( asprintf( &newval, "%s,%s", oldval, &psz[strlen(txt)] ) == -1 ) \ |
|---|
| 628 |
newval = NULL; \ |
|---|
| 629 |
vlc_meta_Set( p_sys->p_meta, vlc_meta_ ## var, newval ); \ |
|---|
| 630 |
free( newval ); \ |
|---|
| 631 |
} \ |
|---|
| 632 |
else \ |
|---|
| 633 |
vlc_meta_Set( p_sys->p_meta, vlc_meta_ ## var, &psz[strlen(txt)] ); \ |
|---|
| 634 |
} |
|---|
| 635 |
IF_EXTRACT("TITLE=", Title ) |
|---|
| 636 |
else IF_EXTRACT("ALBUM=", Album ) |
|---|
| 637 |
else IF_EXTRACT("TRACKNUMBER=", TrackNumber ) |
|---|
| 638 |
else IF_EXTRACT("ARTIST=", Artist ) |
|---|
| 639 |
else IF_EXTRACT("COPYRIGHT=", Copyright ) |
|---|
| 640 |
else IF_EXTRACT("DESCRIPTION=", Description ) |
|---|
| 641 |
else IF_EXTRACT("GENRE=", Genre ) |
|---|
| 642 |
else IF_EXTRACT("DATE=", Date ) |
|---|
| 643 |
else if( strchr( psz, '=' ) ) |
|---|
| 644 |
{ |
|---|
| 645 |
|
|---|
| 646 |
|
|---|
| 647 |
char *p = strchr( psz, '=' ); |
|---|
| 648 |
*p++ = '\0'; |
|---|
| 649 |
vlc_meta_AddExtra( p_sys->p_meta, psz, p ); |
|---|
| 650 |
} |
|---|
| 651 |
#undef IF_EXTRACT |
|---|
| 652 |
free( psz ); |
|---|
| 653 |
} |
|---|
| 654 |
#undef RM |
|---|
| 655 |
} |
|---|
| 656 |
|
|---|
| 657 |
static void ParsePicture( demux_t *p_demux, const uint8_t *p_data, int i_data ) |
|---|
| 658 |
{ |
|---|
| 659 |
static const int pi_cover_score[] = { |
|---|
| 660 |
0, |
|---|
| 661 |
2, 1, |
|---|
| 662 |
10, |
|---|
| 663 |
9, |
|---|
| 664 |
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
|---|
| 665 |
6, |
|---|
| 666 |
0, |
|---|
| 667 |
7, |
|---|
| 668 |
8, |
|---|
| 669 |
0, |
|---|
| 670 |
}; |
|---|
| 671 |
demux_sys_t *p_sys = p_demux->p_sys; |
|---|
| 672 |
int i_type; |
|---|
| 673 |
int i_len; |
|---|
| 674 |
char *psz_mime = NULL; |
|---|
| 675 |
char *psz_description = NULL; |
|---|
| 676 |
input_attachment_t *p_attachment; |
|---|
| 677 |
char psz_name[128]; |
|---|
| 678 |
|
|---|
| 679 |
if( i_data < 4 + 3*4 ) |
|---|
| 680 |
return; |
|---|
| 681 |
#define RM(x) do { i_data -= (x); p_data += (x); } while(0) |
|---|
| 682 |
RM(4); |
|---|
| 683 |
|
|---|
| 684 |
i_type = GetDWBE( p_data ); RM(4); |
|---|
| 685 |
i_len = GetDWBE( p_data ); RM(4); |
|---|
| 686 |
if( i_len < 0 || i_data < i_len + 4 ) |
|---|
| 687 |
goto error; |
|---|
| 688 |
psz_mime = strndup( (const char*)p_data, i_len ); RM(i_len); |
|---|
| 689 |
i_len = GetDWBE( p_data ); RM(4); |
|---|
| 690 |
if( i_len < 0 || i_data < i_len + 4*4 + 4) |
|---|
| 691 |
goto error; |
|---|
| 692 |
psz_description = strndup( (const char*)p_data, i_len ); RM(i_len); |
|---|
| 693 |
EnsureUTF8( psz_description ); |
|---|
| 694 |
RM(4*4); |
|---|
| 695 |
i_len = GetDWBE( p_data ); RM(4); |
|---|
| 696 |
if( i_len < 0 || i_len > i_data ) |
|---|
| 697 |
goto error; |
|---|
| 698 |
|
|---|
| 699 |
msg_Dbg( p_demux, "FLAC: Picture type=%d mime=%s description='%s' file length=%d", |
|---|
| 700 |
i_type, psz_mime, psz_description, i_len ); |
|---|
| 701 |
|
|---|
| 702 |
snprintf( psz_name, sizeof(psz_name), "picture%d", p_sys->i_attachments ); |
|---|
| 703 |
if( !strcasecmp( psz_mime, "image/jpeg" ) ) |
|---|
| 704 |
strcat( psz_name, ".jpg" ); |
|---|
| 705 |
else if( !strcasecmp( psz_mime, "image/png" ) ) |
|---|
| 706 |
strcat( psz_name, ".png" ); |
|---|
| 707 |
|
|---|
| 708 |
p_attachment = vlc_input_attachment_New( psz_name, psz_mime, psz_description, |
|---|
| 709 |
p_data, i_data ); |
|---|
| 710 |
TAB_APPEND( p_sys->i_attachments, p_sys->attachments, p_attachment ); |
|---|
| 711 |
|
|---|
| 712 |
if( i_type >= 0 && (unsigned int)i_type < sizeof(pi_cover_score)/sizeof(pi_cover_score[0]) && |
|---|
| 713 |
p_sys->i_cover_score < pi_cover_score[i_type] ) |
|---|
| 714 |
{ |
|---|
| 715 |
p_sys->i_cover_idx = p_sys->i_attachments-1; |
|---|
| 716 |
p_sys->i_cover_score = pi_cover_score[i_type]; |
|---|
| 717 |
} |
|---|
| 718 |
error: |
|---|
| 719 |
free( psz_mime ); |
|---|
| 720 |
free( psz_description ); |
|---|
| 721 |
} |
|---|
| 722 |
#undef RM |
|---|
| 723 |
|
|---|