| 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 |
|
|---|
| 29 |
#ifdef HAVE_CONFIG_H |
|---|
| 30 |
# include "config.h" |
|---|
| 31 |
#endif |
|---|
| 32 |
|
|---|
| 33 |
#include <vlc_common.h> |
|---|
| 34 |
#include <vlc_demux.h> |
|---|
| 35 |
#include <vlc_stream.h> |
|---|
| 36 |
#include <vlc_meta.h> |
|---|
| 37 |
#include <vlc_input.h> |
|---|
| 38 |
#include <vlc_charset.h> |
|---|
| 39 |
|
|---|
| 40 |
|
|---|
| 41 |
#if defined(HAVE_LIBAVFORMAT_AVFORMAT_H) |
|---|
| 42 |
# include <libavformat/avformat.h> |
|---|
| 43 |
#elif defined(HAVE_FFMPEG_AVFORMAT_H) |
|---|
| 44 |
# include <ffmpeg/avformat.h> |
|---|
| 45 |
#endif |
|---|
| 46 |
|
|---|
| 47 |
#include "../../codec/avcodec/fourcc.h" |
|---|
| 48 |
#include "../../codec/avcodec/chroma.h" |
|---|
| 49 |
#include "avformat.h" |
|---|
| 50 |
|
|---|
| 51 |
|
|---|
| 52 |
|
|---|
| 53 |
|
|---|
| 54 |
#if defined(HAVE_FFMPEG_AVFORMAT_H) || defined(HAVE_LIBAVFORMAT_AVFORMAT_H) |
|---|
| 55 |
|
|---|
| 56 |
#if (LIBAVCODEC_VERSION_INT >= ((51<<16)+(50<<8)+0) ) |
|---|
| 57 |
# define HAVE_FFMPEG_CODEC_ATTACHMENT 1 |
|---|
| 58 |
#endif |
|---|
| 59 |
|
|---|
| 60 |
#if (LIBAVFORMAT_VERSION_INT >= ((52<<16)+(15<<8)+0) ) |
|---|
| 61 |
# define HAVE_FFMPEG_CHAPTERS 1 |
|---|
| 62 |
#endif |
|---|
| 63 |
|
|---|
| 64 |
|
|---|
| 65 |
|
|---|
| 66 |
|
|---|
| 67 |
struct demux_sys_t |
|---|
| 68 |
{ |
|---|
| 69 |
ByteIOContext io; |
|---|
| 70 |
int io_buffer_size; |
|---|
| 71 |
uint8_t *io_buffer; |
|---|
| 72 |
|
|---|
| 73 |
AVInputFormat *fmt; |
|---|
| 74 |
AVFormatContext *ic; |
|---|
| 75 |
URLContext url; |
|---|
| 76 |
URLProtocol prot; |
|---|
| 77 |
|
|---|
| 78 |
int i_tk; |
|---|
| 79 |
es_out_id_t **tk; |
|---|
| 80 |
|
|---|
| 81 |
int64_t i_pcr; |
|---|
| 82 |
int64_t i_pcr_inc; |
|---|
| 83 |
int i_pcr_tk; |
|---|
| 84 |
|
|---|
| 85 |
int i_attachments; |
|---|
| 86 |
input_attachment_t **attachments; |
|---|
| 87 |
|
|---|
| 88 |
|
|---|
| 89 |
input_title_t *p_title; |
|---|
| 90 |
}; |
|---|
| 91 |
|
|---|
| 92 |
|
|---|
| 93 |
|
|---|
| 94 |
|
|---|
| 95 |
static int Demux ( demux_t *p_demux ); |
|---|
| 96 |
static int Control( demux_t *p_demux, int i_query, va_list args ); |
|---|
| 97 |
|
|---|
| 98 |
static int IORead( void *opaque, uint8_t *buf, int buf_size ); |
|---|
| 99 |
static offset_t IOSeek( void *opaque, offset_t offset, int whence ); |
|---|
| 100 |
|
|---|
| 101 |
static void UpdateSeekPoint( demux_t *p_demux, int64_t i_time ); |
|---|
| 102 |
|
|---|
| 103 |
|
|---|
| 104 |
|
|---|
| 105 |
|
|---|
| 106 |
int OpenDemux( vlc_object_t *p_this ) |
|---|
| 107 |
{ |
|---|
| 108 |
demux_t *p_demux = (demux_t*)p_this; |
|---|
| 109 |
demux_sys_t *p_sys; |
|---|
| 110 |
AVProbeData pd; |
|---|
| 111 |
AVInputFormat *fmt; |
|---|
| 112 |
unsigned int i; |
|---|
| 113 |
bool b_avfmt_nofile; |
|---|
| 114 |
int64_t i_start_time = -1; |
|---|
| 115 |
|
|---|
| 116 |
|
|---|
| 117 |
pd.filename = p_demux->psz_path; |
|---|
| 118 |
if( ( pd.buf_size = stream_Peek( p_demux->s, &pd.buf, 2048 ) ) <= 0 ) |
|---|
| 119 |
{ |
|---|
| 120 |
msg_Warn( p_demux, "cannot peek" ); |
|---|
| 121 |
return VLC_EGENERIC; |
|---|
| 122 |
} |
|---|
| 123 |
|
|---|
| 124 |
av_register_all(); |
|---|
| 125 |
|
|---|
| 126 |
|
|---|
| 127 |
if( !( fmt = av_probe_input_format( &pd, 1 ) ) ) |
|---|
| 128 |
{ |
|---|
| 129 |
msg_Dbg( p_demux, "couldn't guess format" ); |
|---|
| 130 |
return VLC_EGENERIC; |
|---|
| 131 |
} |
|---|
| 132 |
|
|---|
| 133 |
|
|---|
| 134 |
if( !p_demux->b_force && |
|---|
| 135 |
( !strcmp( fmt->name, "mpeg" ) || |
|---|
| 136 |
!strcmp( fmt->name, "vcd" ) || |
|---|
| 137 |
!strcmp( fmt->name, "vob" ) || |
|---|
| 138 |
!strcmp( fmt->name, "mpegts" ) || |
|---|
| 139 |
|
|---|
| 140 |
!strcmp( fmt->name, "redir" ) || |
|---|
| 141 |
!strcmp( fmt->name, "sdp" ) ) ) |
|---|
| 142 |
{ |
|---|
| 143 |
return VLC_EGENERIC; |
|---|
| 144 |
} |
|---|
| 145 |
|
|---|
| 146 |
|
|---|
| 147 |
if( !p_demux->b_force && !strcmp( fmt->name, "psxstr" ) ) |
|---|
| 148 |
{ |
|---|
| 149 |
int i_len; |
|---|
| 150 |
|
|---|
| 151 |
if( !p_demux->psz_path ) return VLC_EGENERIC; |
|---|
| 152 |
|
|---|
| 153 |
i_len = strlen( p_demux->psz_path ); |
|---|
| 154 |
if( i_len < 4 ) return VLC_EGENERIC; |
|---|
| 155 |
|
|---|
| 156 |
if( strcasecmp( &p_demux->psz_path[i_len - 4], ".str" ) && |
|---|
| 157 |
strcasecmp( &p_demux->psz_path[i_len - 4], ".xai" ) && |
|---|
| 158 |
strcasecmp( &p_demux->psz_path[i_len - 3], ".xa" ) ) |
|---|
| 159 |
{ |
|---|
| 160 |
return VLC_EGENERIC; |
|---|
| 161 |
} |
|---|
| 162 |
} |
|---|
| 163 |
|
|---|
| 164 |
msg_Dbg( p_demux, "detected format: %s", fmt->name ); |
|---|
| 165 |
|
|---|
| 166 |
|
|---|
| 167 |
p_demux->pf_demux = Demux; |
|---|
| 168 |
p_demux->pf_control = Control; |
|---|
| 169 |
p_demux->p_sys = p_sys = malloc( sizeof( demux_sys_t ) ); |
|---|
| 170 |
p_sys->ic = 0; |
|---|
| 171 |
p_sys->fmt = fmt; |
|---|
| 172 |
p_sys->i_tk = 0; |
|---|
| 173 |
p_sys->tk = NULL; |
|---|
| 174 |
p_sys->i_pcr_tk = -1; |
|---|
| 175 |
p_sys->i_pcr = -1; |
|---|
| 176 |
TAB_INIT( p_sys->i_attachments, p_sys->attachments); |
|---|
| 177 |
p_sys->p_title = NULL; |
|---|
| 178 |
|
|---|
| 179 |
|
|---|
| 180 |
p_sys->io_buffer_size = 32768; |
|---|
| 181 |
p_sys->io_buffer = malloc( p_sys->io_buffer_size ); |
|---|
| 182 |
p_sys->url.priv_data = p_demux; |
|---|
| 183 |
p_sys->url.prot = &p_sys->prot; |
|---|
| 184 |
p_sys->url.prot->name = "VLC I/O wrapper"; |
|---|
| 185 |
p_sys->url.prot->url_open = 0; |
|---|
| 186 |
p_sys->url.prot->url_read = |
|---|
| 187 |
(int (*) (URLContext *, unsigned char *, int))IORead; |
|---|
| 188 |
p_sys->url.prot->url_write = 0; |
|---|
| 189 |
p_sys->url.prot->url_seek = |
|---|
| 190 |
(offset_t (*) (URLContext *, offset_t, int))IOSeek; |
|---|
| 191 |
p_sys->url.prot->url_close = 0; |
|---|
| 192 |
p_sys->url.prot->next = 0; |
|---|
| 193 |
init_put_byte( &p_sys->io, p_sys->io_buffer, p_sys->io_buffer_size, |
|---|
| 194 |
0, &p_sys->url, IORead, NULL, IOSeek ); |
|---|
| 195 |
|
|---|
| 196 |
b_avfmt_nofile = p_sys->fmt->flags & AVFMT_NOFILE; |
|---|
| 197 |
p_sys->fmt->flags |= AVFMT_NOFILE; |
|---|
| 198 |
|
|---|
| 199 |
|
|---|
| 200 |
if( av_open_input_stream( &p_sys->ic, &p_sys->io, p_demux->psz_path, |
|---|
| 201 |
p_sys->fmt, NULL ) ) |
|---|
| 202 |
{ |
|---|
| 203 |
msg_Err( p_demux, "av_open_input_stream failed" ); |
|---|
| 204 |
if( !b_avfmt_nofile ) p_sys->fmt->flags ^= AVFMT_NOFILE; |
|---|
| 205 |
CloseDemux( p_this ); |
|---|
| 206 |
return VLC_EGENERIC; |
|---|
| 207 |
} |
|---|
| 208 |
|
|---|
| 209 |
if( av_find_stream_info( p_sys->ic ) < 0 ) |
|---|
| 210 |
{ |
|---|
| 211 |
msg_Err( p_demux, "av_find_stream_info failed" ); |
|---|
| 212 |
if( !b_avfmt_nofile ) p_sys->fmt->flags ^= AVFMT_NOFILE; |
|---|
| 213 |
CloseDemux( p_this ); |
|---|
| 214 |
return VLC_EGENERIC; |
|---|
| 215 |
} |
|---|
| 216 |
if( !b_avfmt_nofile ) p_sys->fmt->flags ^= AVFMT_NOFILE; |
|---|
| 217 |
|
|---|
| 218 |
for( i = 0; i < p_sys->ic->nb_streams; i++ ) |
|---|
| 219 |
{ |
|---|
| 220 |
AVCodecContext *cc = p_sys->ic->streams[i]->codec; |
|---|
| 221 |
es_out_id_t *es; |
|---|
| 222 |
es_format_t fmt; |
|---|
| 223 |
vlc_fourcc_t fcc; |
|---|
| 224 |
const char *psz_type = "unknown"; |
|---|
| 225 |
|
|---|
| 226 |
if( !GetVlcFourcc( cc->codec_id, NULL, &fcc, NULL ) ) |
|---|
| 227 |
fcc = VLC_FOURCC( 'u', 'n', 'd', 'f' ); |
|---|
| 228 |
|
|---|
| 229 |
switch( cc->codec_type ) |
|---|
| 230 |
{ |
|---|
| 231 |
case CODEC_TYPE_AUDIO: |
|---|
| 232 |
es_format_Init( &fmt, AUDIO_ES, fcc ); |
|---|
| 233 |
fmt.audio.i_channels = cc->channels; |
|---|
| 234 |
fmt.audio.i_rate = cc->sample_rate; |
|---|
| 235 |
#if LIBAVCODEC_VERSION_INT < ((52<<16)+(0<<8)+0) |
|---|
| 236 |
fmt.audio.i_bitspersample = cc->bits_per_sample; |
|---|
| 237 |
#else |
|---|
| 238 |
fmt.audio.i_bitspersample = cc->bits_per_coded_sample; |
|---|
| 239 |
#endif |
|---|
| 240 |
fmt.audio.i_blockalign = cc->block_align; |
|---|
| 241 |
psz_type = "audio"; |
|---|
| 242 |
break; |
|---|
| 243 |
|
|---|
| 244 |
case CODEC_TYPE_VIDEO: |
|---|
| 245 |
es_format_Init( &fmt, VIDEO_ES, fcc ); |
|---|
| 246 |
|
|---|
| 247 |
|
|---|
| 248 |
if( cc->codec_id == CODEC_ID_RAWVIDEO ) |
|---|
| 249 |
{ |
|---|
| 250 |
msg_Dbg( p_demux, "raw video, pixel format: %i", cc->pix_fmt ); |
|---|
| 251 |
if( GetVlcChroma( &fmt.video, cc->pix_fmt ) != VLC_SUCCESS) |
|---|
| 252 |
{ |
|---|
| 253 |
msg_Err( p_demux, "was unable to find a FourCC match for raw video" ); |
|---|
| 254 |
} |
|---|
| 255 |
else |
|---|
| 256 |
fmt.i_codec = fmt.video.i_chroma; |
|---|
| 257 |
} |
|---|
| 258 |
|
|---|
| 259 |
fmt.video.i_width = cc->width; |
|---|
| 260 |
fmt.video.i_height = cc->height; |
|---|
| 261 |
if( cc->palctrl ) |
|---|
| 262 |
{ |
|---|
| 263 |
fmt.video.p_palette = malloc( sizeof(video_palette_t) ); |
|---|
| 264 |
*fmt.video.p_palette = *(video_palette_t *)cc->palctrl; |
|---|
| 265 |
} |
|---|
| 266 |
psz_type = "video"; |
|---|
| 267 |
break; |
|---|
| 268 |
|
|---|
| 269 |
case CODEC_TYPE_SUBTITLE: |
|---|
| 270 |
es_format_Init( &fmt, SPU_ES, fcc ); |
|---|
| 271 |
psz_type = "subtitle"; |
|---|
| 272 |
break; |
|---|
| 273 |
|
|---|
| 274 |
default: |
|---|
| 275 |
es_format_Init( &fmt, UNKNOWN_ES, 0 ); |
|---|
| 276 |
#ifdef HAVE_FFMPEG_CODEC_ATTACHMENT |
|---|
| 277 |
if( cc->codec_type == CODEC_TYPE_ATTACHMENT ) |
|---|
| 278 |
{ |
|---|
| 279 |
input_attachment_t *p_attachment; |
|---|
| 280 |
psz_type = "attachment"; |
|---|
| 281 |
if( cc->codec_id == CODEC_ID_TTF ) |
|---|
| 282 |
{ |
|---|
| 283 |
p_attachment = vlc_input_attachment_New( p_sys->ic->streams[i]->filename, "application/x-truetype-font", NULL, |
|---|
| 284 |
cc->extradata, (int)cc->extradata_size ); |
|---|
| 285 |
TAB_APPEND( p_sys->i_attachments, p_sys->attachments, p_attachment ); |
|---|
| 286 |
} |
|---|
| 287 |
else msg_Warn( p_demux, "unsupported attachment type in ffmpeg demux" ); |
|---|
| 288 |
} |
|---|
| 289 |
break; |
|---|
| 290 |
#endif |
|---|
| 291 |
|
|---|
| 292 |
if( cc->codec_type == CODEC_TYPE_DATA ) |
|---|
| 293 |
psz_type = "data"; |
|---|
| 294 |
|
|---|
| 295 |
msg_Warn( p_demux, "unsupported track type in ffmpeg demux" ); |
|---|
| 296 |
break; |
|---|
| 297 |
} |
|---|
| 298 |
fmt.psz_language = strdup( p_sys->ic->streams[i]->language ); |
|---|
| 299 |
|
|---|
| 300 |
#ifdef HAVE_FFMPEG_CODEC_ATTACHMENT |
|---|
| 301 |
if( cc->codec_type != CODEC_TYPE_ATTACHMENT ) |
|---|
| 302 |
#endif |
|---|
| 303 |
{ |
|---|
| 304 |
fmt.i_extra = cc->extradata_size; |
|---|
| 305 |
fmt.p_extra = cc->extradata; |
|---|
| 306 |
} |
|---|
| 307 |
es = es_out_Add( p_demux->out, &fmt ); |
|---|
| 308 |
|
|---|
| 309 |
msg_Dbg( p_demux, "adding es: %s codec = %4.4s", |
|---|
| 310 |
psz_type, (char*)&fcc ); |
|---|
| 311 |
TAB_APPEND( p_sys->i_tk, p_sys->tk, es ); |
|---|
| 312 |
} |
|---|
| 313 |
if( p_sys->ic->start_time != (int64_t)AV_NOPTS_VALUE ) |
|---|
| 314 |
i_start_time = p_sys->ic->start_time * 1000000 / AV_TIME_BASE; |
|---|
| 315 |
|
|---|
| 316 |
msg_Dbg( p_demux, "AVFormat supported stream" ); |
|---|
| 317 |
msg_Dbg( p_demux, " - format = %s (%s)", |
|---|
| 318 |
p_sys->fmt->name, p_sys->fmt->long_name ); |
|---|
| 319 |
msg_Dbg( p_demux, " - start time = %"PRId64, i_start_time ); |
|---|
| 320 |
msg_Dbg( p_demux, " - duration = %"PRId64, |
|---|
| 321 |
( p_sys->ic->duration != (int64_t)AV_NOPTS_VALUE ) ? |
|---|
| 322 |
p_sys->ic->duration * 1000000 / AV_TIME_BASE : -1 ); |
|---|
| 323 |
|
|---|
| 324 |
#if HAVE_FFMPEG_CHAPTERS |
|---|
| 325 |
p_sys->p_title = vlc_input_title_New(); |
|---|
| 326 |
for( i = 0; i < p_sys->ic->nb_chapters; i++ ) |
|---|
| 327 |
{ |
|---|
| 328 |
seekpoint_t *s = vlc_seekpoint_New(); |
|---|
| 329 |
|
|---|
| 330 |
if( p_sys->ic->chapters[i]->title ) |
|---|
| 331 |
{ |
|---|
| 332 |
s->psz_name = strdup( p_sys->ic->chapters[i]->title ); |
|---|
| 333 |
EnsureUTF8( s->psz_name ); |
|---|
| 334 |
msg_Dbg( p_demux, " - chapter %d: %s", i, s->psz_name ); |
|---|
| 335 |
} |
|---|
| 336 |
s->i_time_offset = p_sys->ic->chapters[i]->start * 1000000 * |
|---|
| 337 |
p_sys->ic->chapters[i]->time_base.num / |
|---|
| 338 |
p_sys->ic->chapters[i]->time_base.den - |
|---|
| 339 |
(i_start_time != -1 ? i_start_time : 0 ); |
|---|
| 340 |
TAB_APPEND( p_sys->p_title->i_seekpoint, p_sys->p_title->seekpoint, s ); |
|---|
| 341 |
} |
|---|
| 342 |
#endif |
|---|
| 343 |
|
|---|
| 344 |
return VLC_SUCCESS; |
|---|
| 345 |
} |
|---|
| 346 |
|
|---|
| 347 |
|
|---|
| 348 |
|
|---|
| 349 |
|
|---|
| 350 |
void CloseDemux( vlc_object_t *p_this ) |
|---|
| 351 |
{ |
|---|
| 352 |
demux_t *p_demux = (demux_t*)p_this; |
|---|
| 353 |
demux_sys_t *p_sys = p_demux->p_sys; |
|---|
| 354 |
bool b_avfmt_nofile; |
|---|
| 355 |
|
|---|
| 356 |
FREENULL( p_sys->tk ); |
|---|
| 357 |
|
|---|
| 358 |
b_avfmt_nofile = p_sys->fmt->flags & AVFMT_NOFILE; |
|---|
| 359 |
p_sys->fmt->flags |= AVFMT_NOFILE; |
|---|
| 360 |
if( p_sys->ic ) av_close_input_file( p_sys->ic ); |
|---|
| 361 |
if( !b_avfmt_nofile ) p_sys->fmt->flags ^= AVFMT_NOFILE; |
|---|
| 362 |
|
|---|
| 363 |
for( int i = 0; i < p_sys->i_attachments; i++ ) |
|---|
| 364 |
free( p_sys->attachments[i] ); |
|---|
| 365 |
TAB_CLEAN( p_sys->i_attachments, p_sys->attachments); |
|---|
| 366 |
|
|---|
| 367 |
if( p_sys->p_title ) |
|---|
| 368 |
vlc_input_title_Delete( p_sys->p_title ); |
|---|
| 369 |
|
|---|
| 370 |
free( p_sys->io_buffer ); |
|---|
| 371 |
free( p_sys ); |
|---|
| 372 |
} |
|---|
| 373 |
|
|---|
| 374 |
|
|---|
| 375 |
|
|---|
| 376 |
|
|---|
| 377 |
static int Demux( demux_t *p_demux ) |
|---|
| 378 |
{ |
|---|
| 379 |
demux_sys_t *p_sys = p_demux->p_sys; |
|---|
| 380 |
AVPacket pkt; |
|---|
| 381 |
block_t *p_frame; |
|---|
| 382 |
int64_t i_start_time; |
|---|
| 383 |
|
|---|
| 384 |
|
|---|
| 385 |
if( av_read_frame( p_sys->ic, &pkt ) ) |
|---|
| 386 |
{ |
|---|
| 387 |
return 0; |
|---|
| 388 |
} |
|---|
| 389 |
if( pkt.stream_index < 0 || pkt.stream_index >= p_sys->i_tk ) |
|---|
| 390 |
{ |
|---|
| 391 |
av_free_packet( &pkt ); |
|---|
| 392 |
return 1; |
|---|
| 393 |
} |
|---|
| 394 |
if( ( p_frame = block_New( p_demux, pkt.size ) ) == NULL ) |
|---|
| 395 |
{ |
|---|
| 396 |
return 0; |
|---|
| 397 |
} |
|---|
| 398 |
|
|---|
| 399 |
memcpy( p_frame->p_buffer, pkt.data, pkt.size ); |
|---|
| 400 |
|
|---|
| 401 |
if( pkt.flags & PKT_FLAG_KEY ) |
|---|
| 402 |
p_frame->i_flags |= BLOCK_FLAG_TYPE_I; |
|---|
| 403 |
|
|---|
| 404 |
i_start_time = ( p_sys->ic->start_time != (int64_t)AV_NOPTS_VALUE ) ? |
|---|
| 405 |
( p_sys->ic->start_time * 1000000 / AV_TIME_BASE ) : 0; |
|---|
| 406 |
|
|---|
| 407 |
p_frame->i_dts = ( pkt.dts == (int64_t)AV_NOPTS_VALUE ) ? |
|---|
| 408 |
0 : (pkt.dts) * 1000000 * |
|---|
| 409 |
p_sys->ic->streams[pkt.stream_index]->time_base.num / |
|---|
| 410 |
p_sys->ic->streams[pkt.stream_index]->time_base.den - i_start_time; |
|---|
| 411 |
p_frame->i_pts = ( pkt.pts == (int64_t)AV_NOPTS_VALUE ) ? |
|---|
| 412 |
0 : (pkt.pts) * 1000000 * |
|---|
| 413 |
p_sys->ic->streams[pkt.stream_index]->time_base.num / |
|---|
| 414 |
p_sys->ic->streams[pkt.stream_index]->time_base.den - i_start_time; |
|---|
| 415 |
if( pkt.duration > 0 ) |
|---|
| 416 |
p_frame->i_length = pkt.duration * 1000000 * |
|---|
| 417 |
p_sys->ic->streams[pkt.stream_index]->time_base.num / |
|---|
| 418 |
p_sys->ic->streams[pkt.stream_index]->time_base.den - i_start_time; |
|---|
| 419 |
|
|---|
| 420 |
#ifdef AVFORMAT_DEBUG |
|---|
| 421 |
msg_Dbg( p_demux, "tk[%d] dts=%"PRId64" pts=%"PRId64, |
|---|
| 422 |
pkt.stream_index, p_frame->i_dts, p_frame->i_pts ); |
|---|
| 423 |
#endif |
|---|
| 424 |
|
|---|
| 425 |
if( pkt.dts > 0 && |
|---|
| 426 |
( pkt.stream_index == p_sys->i_pcr_tk || p_sys->i_pcr_tk < 0 ) ) |
|---|
| 427 |
{ |
|---|
| 428 |
p_sys->i_pcr_tk = pkt.stream_index; |
|---|
| 429 |
p_sys->i_pcr = p_frame->i_dts; |
|---|
| 430 |
|
|---|
| 431 |
es_out_Control( p_demux->out, ES_OUT_SET_PCR, (int64_t)p_sys->i_pcr ); |
|---|
| 432 |
} |
|---|
| 433 |
|
|---|
| 434 |
es_out_Send( p_demux->out, p_sys->tk[pkt.stream_index], p_frame ); |
|---|
| 435 |
|
|---|
| 436 |
UpdateSeekPoint( p_demux, p_sys->i_pcr); |
|---|
| 437 |
av_free_packet( &pkt ); |
|---|
| 438 |
return 1; |
|---|
| 439 |
} |
|---|
| 440 |
|
|---|
| 441 |
static void UpdateSeekPoint( demux_t *p_demux, int64_t i_time ) |
|---|
| 442 |
{ |
|---|
| 443 |
demux_sys_t *p_sys = p_demux->p_sys; |
|---|
| 444 |
int i; |
|---|
| 445 |
|
|---|
| 446 |
if( !p_sys->p_title ) |
|---|
| 447 |
return; |
|---|
| 448 |
|
|---|
| 449 |
for( i = 0; i < p_sys->p_title->i_seekpoint; i++ ) |
|---|
| 450 |
{ |
|---|
| 451 |
if( i_time < p_sys->p_title->seekpoint[i]->i_time_offset ) |
|---|
| 452 |
break; |
|---|
| 453 |
} |
|---|
| 454 |
i--; |
|---|
| 455 |
|
|---|
| 456 |
if( i != p_demux->info.i_seekpoint && i >= 0 ) |
|---|
| 457 |
{ |
|---|
| 458 |
p_demux->info.i_seekpoint = i; |
|---|
| 459 |
p_demux->info.i_update |= INPUT_UPDATE_SEEKPOINT; |
|---|
| 460 |
} |
|---|
| 461 |
} |
|---|
| 462 |
|
|---|
| 463 |
|
|---|
| 464 |
|
|---|
| 465 |
|
|---|
| 466 |
static int Control( demux_t *p_demux, int i_query, va_list args ) |
|---|
| 467 |
{ |
|---|
| 468 |
demux_sys_t *p_sys = p_demux->p_sys; |
|---|
| 469 |
double f, *pf; |
|---|
| 470 |
int64_t i64, *pi64; |
|---|
| 471 |
|
|---|
| 472 |
switch( i_query ) |
|---|
| 473 |
{ |
|---|
| 474 |
case DEMUX_GET_POSITION: |
|---|
| 475 |
pf = (double*) va_arg( args, double* ); *pf = 0.0; |
|---|
| 476 |
i64 = stream_Size( p_demux->s ); |
|---|
| 477 |
if( i64 > 0 ) |
|---|
| 478 |
{ |
|---|
| 479 |
*pf = (double)stream_Tell( p_demux->s ) / (double)i64; |
|---|
| 480 |
} |
|---|
| 481 |
|
|---|
| 482 |
if( (p_sys->ic->duration != (int64_t)AV_NOPTS_VALUE) && (p_sys->i_pcr > 0) ) |
|---|
| 483 |
{ |
|---|
| 484 |
*pf = (double)p_sys->i_pcr / (double)p_sys->ic->duration; |
|---|
| 485 |
} |
|---|
| 486 |
|
|---|
| 487 |
return VLC_SUCCESS; |
|---|
| 488 |
|
|---|
| 489 |
case DEMUX_SET_POSITION: |
|---|
| 490 |
f = (double) va_arg( args, double ); |
|---|
| 491 |
i64 = stream_Tell( p_demux->s ); |
|---|
| 492 |
if( p_sys->i_pcr > 0 ) |
|---|
| 493 |
{ |
|---|
| 494 |
i64 = p_sys->ic->duration * f; |
|---|
| 495 |
if( p_sys->ic->start_time != (int64_t)AV_NOPTS_VALUE ) |
|---|
| 496 |
i64 += p_sys->ic->start_time; |
|---|
| 497 |
|
|---|
| 498 |
msg_Warn( p_demux, "DEMUX_SET_POSITION: %"PRId64, i64 ); |
|---|
| 499 |
|
|---|
| 500 |
|
|---|
| 501 |
|
|---|
| 502 |
if( p_sys->ic->duration == (int64_t)AV_NOPTS_VALUE || |
|---|
| 503 |
(av_seek_frame( p_sys->ic, -1, i64, 0 ) < 0) ) |
|---|
| 504 |
{ |
|---|
| 505 |
int64_t i_size = stream_Size( p_demux->s ); |
|---|
| 506 |
|
|---|
| 507 |
msg_Warn( p_demux, "DEMUX_SET_BYTE_POSITION: %"PRId64, i64 ); |
|---|
| 508 |
if( av_seek_frame( p_sys->ic, -1, (i_size * f), AVSEEK_FLAG_BYTE ) < 0 ) |
|---|
| 509 |
return VLC_EGENERIC; |
|---|
| 510 |
} |
|---|
| 511 |
UpdateSeekPoint( p_demux, i64 ); |
|---|
| 512 |
es_out_Control( p_demux->out, ES_OUT_RESET_PCR ); |
|---|
| 513 |
p_sys->i_pcr = -1; |
|---|
| 514 |
} |
|---|
| 515 |
return VLC_SUCCESS; |
|---|
| 516 |
|
|---|
| 517 |
case DEMUX_GET_LENGTH: |
|---|
| 518 |
pi64 = (int64_t*)va_arg( args, int64_t * ); |
|---|
| 519 |
if( p_sys->ic->duration != (int64_t)AV_NOPTS_VALUE ) |
|---|
| 520 |
{ |
|---|
| 521 |
*pi64 = p_sys->ic->duration; |
|---|
| 522 |
} |
|---|
| 523 |
else *pi64 = 0; |
|---|
| 524 |
return VLC_SUCCESS; |
|---|
| 525 |
|
|---|
| 526 |
case DEMUX_GET_TIME: |
|---|
| 527 |
pi64 = (int64_t*)va_arg( args, int64_t * ); |
|---|
| 528 |
*pi64 = p_sys->i_pcr; |
|---|
| 529 |
return VLC_SUCCESS; |
|---|
| 530 |
|
|---|
| 531 |
case DEMUX_SET_TIME: |
|---|
| 532 |
i64 = (int64_t)va_arg( args, int64_t ); |
|---|
| 533 |
i64 = i64 *AV_TIME_BASE / 1000000; |
|---|
| 534 |
if( p_sys->ic->start_time != (int64_t)AV_NOPTS_VALUE ) |
|---|
| 535 |
i64 += p_sys->ic->start_time; |
|---|
| 536 |
|
|---|
| 537 |
msg_Warn( p_demux, "DEMUX_SET_TIME: %"PRId64, i64 ); |
|---|
| 538 |
|
|---|
| 539 |
if( av_seek_frame( p_sys->ic, -1, i64, 0 ) < 0 ) |
|---|
| 540 |
{ |
|---|
| 541 |
return VLC_EGENERIC; |
|---|
| 542 |
} |
|---|
| 543 |
es_out_Control( p_demux->out, ES_OUT_RESET_PCR ); |
|---|
| 544 |
p_sys->i_pcr = -1; |
|---|
| 545 |
UpdateSeekPoint( p_demux, i64 ); |
|---|
| 546 |
return VLC_SUCCESS; |
|---|
| 547 |
|
|---|
| 548 |
case DEMUX_GET_META: |
|---|
| 549 |
{ |
|---|
| 550 |
vlc_meta_t *p_meta = (vlc_meta_t*)va_arg( args, vlc_meta_t* ); |
|---|
| 551 |
|
|---|
| 552 |
if( !p_sys->ic->title[0] || !p_sys->ic->author[0] || |
|---|
| 553 |
!p_sys->ic->copyright[0] || !p_sys->ic->comment[0] || |
|---|
| 554 |
!p_sys->ic->genre[0] ) |
|---|
| 555 |
{ |
|---|
| 556 |
return VLC_EGENERIC; |
|---|
| 557 |
} |
|---|
| 558 |
|
|---|
| 559 |
if( p_sys->ic->title[0] ) |
|---|
| 560 |
vlc_meta_SetTitle( p_meta, p_sys->ic->title ); |
|---|
| 561 |
if( p_sys->ic->author[0] ) |
|---|
| 562 |
vlc_meta_SetArtist( p_meta, p_sys->ic->author ); |
|---|
| 563 |
if( p_sys->ic->copyright[0] ) |
|---|
| 564 |
vlc_meta_SetCopyright( p_meta, p_sys->ic->copyright ); |
|---|
| 565 |
if( p_sys->ic->comment[0] ) |
|---|
| 566 |
vlc_meta_SetDescription( p_meta, p_sys->ic->comment ); |
|---|
| 567 |
if( p_sys->ic->genre[0] ) |
|---|
| 568 |
vlc_meta_SetGenre( p_meta, p_sys->ic->genre ); |
|---|
| 569 |
return VLC_SUCCESS; |
|---|
| 570 |
} |
|---|
| 571 |
|
|---|
| 572 |
case DEMUX_GET_ATTACHMENTS: |
|---|
| 573 |
{ |
|---|
| 574 |
input_attachment_t ***ppp_attach = |
|---|
| 575 |
(input_attachment_t***)va_arg( args, input_attachment_t*** ); |
|---|
| 576 |
int *pi_int = (int*)va_arg( args, int * ); |
|---|
| 577 |
int i; |
|---|
| 578 |
|
|---|
| 579 |
if( p_sys->i_attachments <= 0 ) |
|---|
| 580 |
return VLC_EGENERIC; |
|---|
| 581 |
|
|---|
| 582 |
*pi_int = p_sys->i_attachments;; |
|---|
| 583 |
*ppp_attach = malloc( sizeof(input_attachment_t**) * p_sys->i_attachments ); |
|---|
| 584 |
for( i = 0; i < p_sys->i_attachments; i++ ) |
|---|
| 585 |
(*ppp_attach)[i] = vlc_input_attachment_Duplicate( p_sys->attachments[i] ); |
|---|
| 586 |
return VLC_SUCCESS; |
|---|
| 587 |
} |
|---|
| 588 |
|
|---|
| 589 |
case DEMUX_GET_TITLE_INFO: |
|---|
| 590 |
{ |
|---|
| 591 |
input_title_t ***ppp_title = (input_title_t***)va_arg( args, input_title_t*** ); |
|---|
| 592 |
int *pi_int = (int*)va_arg( args, int* ); |
|---|
| 593 |
int *pi_title_offset = (int*)va_arg( args, int* ); |
|---|
| 594 |
int *pi_seekpoint_offset = (int*)va_arg( args, int* ); |
|---|
| 595 |
|
|---|
| 596 |
if( !p_sys->p_title ) |
|---|
| 597 |
return VLC_EGENERIC; |
|---|
| 598 |
|
|---|
| 599 |
*pi_int = 1; |
|---|
| 600 |
*ppp_title = malloc( sizeof( input_title_t**) ); |
|---|
| 601 |
(*ppp_title)[0] = vlc_input_title_Duplicate( p_sys->p_title ); |
|---|
| 602 |
*pi_title_offset = 0; |
|---|
| 603 |
*pi_seekpoint_offset = 0; |
|---|
| 604 |
return VLC_SUCCESS; |
|---|
| 605 |
} |
|---|
| 606 |
case DEMUX_SET_TITLE: |
|---|
| 607 |
{ |
|---|
| 608 |
const int i_title = (int)va_arg( args, int ); |
|---|
| 609 |
if( !p_sys->p_title || i_title != 0 ) |
|---|
| 610 |
return VLC_EGENERIC; |
|---|
| 611 |
return VLC_SUCCESS; |
|---|
| 612 |
} |
|---|
| 613 |
case DEMUX_SET_SEEKPOINT: |
|---|
| 614 |
{ |
|---|
| 615 |
const int i_seekpoint = (int)va_arg( args, int ); |
|---|
| 616 |
if( !p_sys->p_title ) |
|---|
| 617 |
return VLC_EGENERIC; |
|---|
| 618 |
|
|---|
| 619 |
i64 = p_sys->p_title->seekpoint[i_seekpoint]->i_time_offset *AV_TIME_BASE / 1000000; |
|---|
| 620 |
if( p_sys->ic->start_time != (int64_t)AV_NOPTS_VALUE ) |
|---|
| 621 |
i64 += p_sys->ic->start_time; |
|---|
| 622 |
|
|---|
| 623 |
msg_Warn( p_demux, "DEMUX_SET_TIME: %"PRId64, i64 ); |
|---|
| 624 |
|
|---|
| 625 |
if( av_seek_frame( p_sys->ic, -1, i64, 0 ) < 0 ) |
|---|
| 626 |
{ |
|---|
| 627 |
return VLC_EGENERIC; |
|---|
| 628 |
} |
|---|
| 629 |
es_out_Control( p_demux->out, ES_OUT_RESET_PCR ); |
|---|
| 630 |
p_sys->i_pcr = -1; |
|---|
| 631 |
UpdateSeekPoint( p_demux, i64 ); |
|---|
| 632 |
return VLC_SUCCESS; |
|---|
| 633 |
} |
|---|
| 634 |
|
|---|
| 635 |
|
|---|
| 636 |
default: |
|---|
| 637 |
return VLC_EGENERIC; |
|---|
| 638 |
} |
|---|
| 639 |
} |
|---|
| 640 |
|
|---|
| 641 |
|
|---|
| 642 |
|
|---|
| 643 |
|
|---|
| 644 |
static int IORead( void *opaque, uint8_t *buf, int buf_size ) |
|---|
| 645 |
{ |
|---|
| 646 |
URLContext *p_url = opaque; |
|---|
| 647 |
demux_t *p_demux = p_url->priv_data; |
|---|
| 648 |
if( buf_size < 0 ) return -1; |
|---|
| 649 |
int i_ret = stream_Read( p_demux->s, buf, buf_size ); |
|---|
| 650 |
return i_ret ? i_ret : -1; |
|---|
| 651 |
} |
|---|
| 652 |
|
|---|
| 653 |
static offset_t IOSeek( void *opaque, offset_t offset, int whence ) |
|---|
| 654 |
{ |
|---|
| 655 |
URLContext *p_url = opaque; |
|---|
| 656 |
demux_t *p_demux = p_url->priv_data; |
|---|
| 657 |
int64_t i_absolute = (int64_t)offset; |
|---|
| 658 |
int64_t i_size = stream_Size( p_demux->s ); |
|---|
| 659 |
|
|---|
| 660 |
#ifdef AVFORMAT_DEBUG |
|---|
| 661 |
msg_Warn( p_demux, "IOSeek offset: %"PRId64", whence: %i", offset, whence ); |
|---|
| 662 |
#endif |
|---|
| 663 |
|
|---|
| 664 |
switch( whence ) |
|---|
| 665 |
{ |
|---|
| 666 |
#ifdef AVSEEK_SIZE |
|---|
| 667 |
case AVSEEK_SIZE: |
|---|
| 668 |
return i_size; |
|---|
| 669 |
#endif |
|---|
| 670 |
case SEEK_SET: |
|---|
| 671 |
i_absolute = (int64_t)offset; |
|---|
| 672 |
break; |
|---|
| 673 |
case SEEK_CUR: |
|---|
| 674 |
i_absolute = stream_Tell( p_demux->s ) + (int64_t)offset; |
|---|
| 675 |
break; |
|---|
| 676 |
case SEEK_END: |
|---|
| 677 |
i_absolute = i_size + (int64_t)offset; |
|---|
| 678 |
break; |
|---|
| 679 |
default: |
|---|
| 680 |
return -1; |
|---|
| 681 |
|
|---|
| 682 |
} |
|---|
| 683 |
if( i_absolute < 0 ) |
|---|
| 684 |
i_absolute = 0; |
|---|
| 685 |
|
|---|
| 686 |
if( i_size > 0 && i_absolute >= i_size ) |
|---|
| 687 |
{ |
|---|
| 688 |
msg_Dbg( p_demux, "Trying to seek too far : EOF?" ); |
|---|
| 689 |
return -1; |
|---|
| 690 |
} |
|---|
| 691 |
|
|---|
| 692 |
if( stream_Seek( p_demux->s, i_absolute ) ) |
|---|
| 693 |
{ |
|---|
| 694 |
msg_Warn( p_demux, "we were not allowed to seek, or EOF " ); |
|---|
| 695 |
return -1; |
|---|
| 696 |
} |
|---|
| 697 |
|
|---|
| 698 |
return stream_Tell( p_demux->s ); |
|---|
| 699 |
} |
|---|
| 700 |
|
|---|
| 701 |
#endif |
|---|