| 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 |
|
|---|
| 30 |
|
|---|
| 31 |
|
|---|
| 32 |
|
|---|
| 33 |
|
|---|
| 34 |
|
|---|
| 35 |
|
|---|
| 36 |
|
|---|
| 37 |
|
|---|
| 38 |
|
|---|
| 39 |
|
|---|
| 40 |
|
|---|
| 41 |
|
|---|
| 42 |
|
|---|
| 43 |
|
|---|
| 44 |
|
|---|
| 45 |
#ifdef HAVE_CONFIG_H |
|---|
| 46 |
# include "config.h" |
|---|
| 47 |
#endif |
|---|
| 48 |
|
|---|
| 49 |
#include <vlc_common.h> |
|---|
| 50 |
#include <vlc_plugin.h> |
|---|
| 51 |
#include <vlc_block.h> |
|---|
| 52 |
#include <vlc_codec.h> |
|---|
| 53 |
#include <vlc_block_helper.h> |
|---|
| 54 |
#include "../codec/cc.h" |
|---|
| 55 |
|
|---|
| 56 |
#define SYNC_INTRAFRAME_TEXT N_("Sync on Intra Frame") |
|---|
| 57 |
#define SYNC_INTRAFRAME_LONGTEXT N_("Normally the packetizer would " \ |
|---|
| 58 |
"sync on the next full frame. This flags instructs the packetizer " \ |
|---|
| 59 |
"to sync on the first Intra Frame found.") |
|---|
| 60 |
|
|---|
| 61 |
|
|---|
| 62 |
|
|---|
| 63 |
|
|---|
| 64 |
static int Open ( vlc_object_t * ); |
|---|
| 65 |
static void Close( vlc_object_t * ); |
|---|
| 66 |
|
|---|
| 67 |
vlc_module_begin(); |
|---|
| 68 |
set_category( CAT_SOUT ); |
|---|
| 69 |
set_subcategory( SUBCAT_SOUT_PACKETIZER ); |
|---|
| 70 |
set_description( N_("MPEG-I/II video packetizer") ); |
|---|
| 71 |
set_capability( "packetizer", 50 ); |
|---|
| 72 |
set_callbacks( Open, Close ); |
|---|
| 73 |
|
|---|
| 74 |
add_bool( "packetizer-mpegvideo-sync-iframe", 0, NULL, SYNC_INTRAFRAME_TEXT, |
|---|
| 75 |
SYNC_INTRAFRAME_LONGTEXT, true ); |
|---|
| 76 |
vlc_module_end(); |
|---|
| 77 |
|
|---|
| 78 |
|
|---|
| 79 |
|
|---|
| 80 |
|
|---|
| 81 |
static block_t *Packetize( decoder_t *, block_t ** ); |
|---|
| 82 |
static block_t *ParseMPEGBlock( decoder_t *, block_t * ); |
|---|
| 83 |
static block_t *GetCc( decoder_t *p_dec, bool pb_present[4] ); |
|---|
| 84 |
|
|---|
| 85 |
struct decoder_sys_t |
|---|
| 86 |
{ |
|---|
| 87 |
|
|---|
| 88 |
|
|---|
| 89 |
|
|---|
| 90 |
block_bytestream_t bytestream; |
|---|
| 91 |
int i_state; |
|---|
| 92 |
size_t i_offset; |
|---|
| 93 |
uint8_t p_startcode[3]; |
|---|
| 94 |
|
|---|
| 95 |
|
|---|
| 96 |
block_t *p_seq; |
|---|
| 97 |
block_t *p_ext; |
|---|
| 98 |
|
|---|
| 99 |
|
|---|
| 100 |
block_t *p_frame; |
|---|
| 101 |
block_t **pp_last; |
|---|
| 102 |
|
|---|
| 103 |
bool b_frame_slice; |
|---|
| 104 |
mtime_t i_pts; |
|---|
| 105 |
mtime_t i_dts; |
|---|
| 106 |
|
|---|
| 107 |
|
|---|
| 108 |
int i_frame_rate; |
|---|
| 109 |
int i_frame_rate_base; |
|---|
| 110 |
bool b_seq_progressive; |
|---|
| 111 |
bool b_low_delay; |
|---|
| 112 |
int i_aspect_ratio_info; |
|---|
| 113 |
bool b_inited; |
|---|
| 114 |
|
|---|
| 115 |
|
|---|
| 116 |
int i_temporal_ref; |
|---|
| 117 |
int i_picture_type; |
|---|
| 118 |
int i_picture_structure; |
|---|
| 119 |
int i_top_field_first; |
|---|
| 120 |
int i_repeat_first_field; |
|---|
| 121 |
int i_progressive_frame; |
|---|
| 122 |
|
|---|
| 123 |
mtime_t i_interpolated_dts; |
|---|
| 124 |
mtime_t i_last_ref_pts; |
|---|
| 125 |
bool b_second_field; |
|---|
| 126 |
|
|---|
| 127 |
|
|---|
| 128 |
int i_seq_old; |
|---|
| 129 |
|
|---|
| 130 |
|
|---|
| 131 |
bool b_sync_on_intra_frame; |
|---|
| 132 |
bool b_discontinuity; |
|---|
| 133 |
|
|---|
| 134 |
|
|---|
| 135 |
bool b_cc_reset; |
|---|
| 136 |
uint32_t i_cc_flags; |
|---|
| 137 |
mtime_t i_cc_pts; |
|---|
| 138 |
mtime_t i_cc_dts; |
|---|
| 139 |
cc_data_t cc; |
|---|
| 140 |
}; |
|---|
| 141 |
|
|---|
| 142 |
enum { |
|---|
| 143 |
STATE_NOSYNC, |
|---|
| 144 |
STATE_NEXT_SYNC |
|---|
| 145 |
}; |
|---|
| 146 |
|
|---|
| 147 |
|
|---|
| 148 |
|
|---|
| 149 |
|
|---|
| 150 |
static int Open( vlc_object_t *p_this ) |
|---|
| 151 |
{ |
|---|
| 152 |
decoder_t *p_dec = (decoder_t*)p_this; |
|---|
| 153 |
decoder_sys_t *p_sys; |
|---|
| 154 |
|
|---|
| 155 |
if( p_dec->fmt_in.i_codec != VLC_FOURCC( 'm', 'p', 'g', '1' ) && |
|---|
| 156 |
p_dec->fmt_in.i_codec != VLC_FOURCC( 'm', 'p', 'g', '2' ) && |
|---|
| 157 |
p_dec->fmt_in.i_codec != VLC_FOURCC( 'm', 'p', 'g', 'v' ) ) |
|---|
| 158 |
{ |
|---|
| 159 |
return VLC_EGENERIC; |
|---|
| 160 |
} |
|---|
| 161 |
|
|---|
| 162 |
es_format_Init( &p_dec->fmt_out, VIDEO_ES, VLC_FOURCC('m','p','g','v') ); |
|---|
| 163 |
p_dec->pf_packetize = Packetize; |
|---|
| 164 |
p_dec->pf_get_cc = GetCc; |
|---|
| 165 |
|
|---|
| 166 |
p_dec->p_sys = p_sys = malloc( sizeof( decoder_sys_t ) ); |
|---|
| 167 |
if( !p_dec->p_sys ) |
|---|
| 168 |
return VLC_ENOMEM; |
|---|
| 169 |
memset( p_dec->p_sys, 0, sizeof( decoder_sys_t ) ); |
|---|
| 170 |
|
|---|
| 171 |
|
|---|
| 172 |
p_sys->i_state = STATE_NOSYNC; |
|---|
| 173 |
p_sys->bytestream = block_BytestreamInit(); |
|---|
| 174 |
p_sys->p_startcode[0] = 0; |
|---|
| 175 |
p_sys->p_startcode[1] = 0; |
|---|
| 176 |
p_sys->p_startcode[2] = 1; |
|---|
| 177 |
p_sys->i_offset = 0; |
|---|
| 178 |
|
|---|
| 179 |
p_sys->p_seq = NULL; |
|---|
| 180 |
p_sys->p_ext = NULL; |
|---|
| 181 |
p_sys->p_frame = NULL; |
|---|
| 182 |
p_sys->pp_last = &p_sys->p_frame; |
|---|
| 183 |
p_sys->b_frame_slice = false; |
|---|
| 184 |
|
|---|
| 185 |
p_sys->i_dts = p_sys->i_pts = 0; |
|---|
| 186 |
|
|---|
| 187 |
p_sys->i_frame_rate = 1; |
|---|
| 188 |
p_sys->i_frame_rate_base = 1; |
|---|
| 189 |
p_sys->b_seq_progressive = true; |
|---|
| 190 |
p_sys->b_low_delay = true; |
|---|
| 191 |
p_sys->i_seq_old = 0; |
|---|
| 192 |
|
|---|
| 193 |
p_sys->i_temporal_ref = 0; |
|---|
| 194 |
p_sys->i_picture_type = 0; |
|---|
| 195 |
p_sys->i_picture_structure = 0x03; |
|---|
| 196 |
p_sys->i_top_field_first = 0; |
|---|
| 197 |
p_sys->i_repeat_first_field = 0; |
|---|
| 198 |
p_sys->i_progressive_frame = 0; |
|---|
| 199 |
p_sys->b_inited = 0; |
|---|
| 200 |
|
|---|
| 201 |
p_sys->i_interpolated_dts = 0; |
|---|
| 202 |
p_sys->i_last_ref_pts = 0; |
|---|
| 203 |
p_sys->b_second_field = 0; |
|---|
| 204 |
|
|---|
| 205 |
p_sys->b_discontinuity = false; |
|---|
| 206 |
p_sys->b_sync_on_intra_frame = var_CreateGetBool( p_dec, "packetizer-mpegvideo-sync-iframe" ); |
|---|
| 207 |
if( p_sys->b_sync_on_intra_frame ) |
|---|
| 208 |
msg_Dbg( p_dec, "syncing on intra frame now" ); |
|---|
| 209 |
|
|---|
| 210 |
p_sys->b_cc_reset = false; |
|---|
| 211 |
p_sys->i_cc_pts = 0; |
|---|
| 212 |
p_sys->i_cc_dts = 0; |
|---|
| 213 |
p_sys->i_cc_flags = 0; |
|---|
| 214 |
cc_Init( &p_sys->cc ); |
|---|
| 215 |
|
|---|
| 216 |
return VLC_SUCCESS; |
|---|
| 217 |
} |
|---|
| 218 |
|
|---|
| 219 |
|
|---|
| 220 |
|
|---|
| 221 |
|
|---|
| 222 |
static void Close( vlc_object_t *p_this ) |
|---|
| 223 |
{ |
|---|
| 224 |
decoder_t *p_dec = (decoder_t*)p_this; |
|---|
| 225 |
decoder_sys_t *p_sys = p_dec->p_sys; |
|---|
| 226 |
|
|---|
| 227 |
block_BytestreamRelease( &p_sys->bytestream ); |
|---|
| 228 |
|
|---|
| 229 |
if( p_sys->p_seq ) |
|---|
| 230 |
{ |
|---|
| 231 |
block_Release( p_sys->p_seq ); |
|---|
| 232 |
} |
|---|
| 233 |
if( p_sys->p_ext ) |
|---|
| 234 |
{ |
|---|
| 235 |
block_Release( p_sys->p_ext ); |
|---|
| 236 |
} |
|---|
| 237 |
if( p_sys->p_frame ) |
|---|
| 238 |
{ |
|---|
| 239 |
block_ChainRelease( p_sys->p_frame ); |
|---|
| 240 |
} |
|---|
| 241 |
|
|---|
| 242 |
var_Destroy( p_dec, "packetizer-mpegvideo-sync-iframe" ); |
|---|
| 243 |
|
|---|
| 244 |
free( p_sys ); |
|---|
| 245 |
} |
|---|
| 246 |
|
|---|
| 247 |
|
|---|
| 248 |
|
|---|
| 249 |
|
|---|
| 250 |
static block_t *Packetize( decoder_t *p_dec, block_t **pp_block ) |
|---|
| 251 |
{ |
|---|
| 252 |
decoder_sys_t *p_sys = p_dec->p_sys; |
|---|
| 253 |
block_t *p_pic; |
|---|
| 254 |
|
|---|
| 255 |
if( pp_block == NULL || *pp_block == NULL ) |
|---|
| 256 |
{ |
|---|
| 257 |
return NULL; |
|---|
| 258 |
} |
|---|
| 259 |
|
|---|
| 260 |
if( (*pp_block)->i_flags&(BLOCK_FLAG_DISCONTINUITY|BLOCK_FLAG_CORRUPTED) ) |
|---|
| 261 |
{ |
|---|
| 262 |
if( (*pp_block)->i_flags&BLOCK_FLAG_CORRUPTED ) |
|---|
| 263 |
{ |
|---|
| 264 |
p_sys->i_state = STATE_NOSYNC; |
|---|
| 265 |
block_BytestreamFlush( &p_sys->bytestream ); |
|---|
| 266 |
|
|---|
| 267 |
p_sys->b_discontinuity = true; |
|---|
| 268 |
if( p_sys->p_frame ) |
|---|
| 269 |
block_ChainRelease( p_sys->p_frame ); |
|---|
| 270 |
p_sys->p_frame = NULL; |
|---|
| 271 |
p_sys->pp_last = &p_sys->p_frame; |
|---|
| 272 |
p_sys->b_frame_slice = false; |
|---|
| 273 |
} |
|---|
| 274 |
|
|---|
| 275 |
|
|---|
| 276 |
|
|---|
| 277 |
block_Release( *pp_block ); |
|---|
| 278 |
return NULL; |
|---|
| 279 |
} |
|---|
| 280 |
|
|---|
| 281 |
|
|---|
| 282 |
block_BytestreamPush( &p_sys->bytestream, *pp_block ); |
|---|
| 283 |
|
|---|
| 284 |
while( 1 ) |
|---|
| 285 |
{ |
|---|
| 286 |
switch( p_sys->i_state ) |
|---|
| 287 |
{ |
|---|
| 288 |
|
|---|
| 289 |
case STATE_NOSYNC: |
|---|
| 290 |
if( block_FindStartcodeFromOffset( &p_sys->bytestream, |
|---|
| 291 |
&p_sys->i_offset, p_sys->p_startcode, 3 ) == VLC_SUCCESS ) |
|---|
| 292 |
{ |
|---|
| 293 |
p_sys->i_state = STATE_NEXT_SYNC; |
|---|
| 294 |
} |
|---|
| 295 |
|
|---|
| 296 |
if( p_sys->i_offset ) |
|---|
| 297 |
{ |
|---|
| 298 |
block_SkipBytes( &p_sys->bytestream, p_sys->i_offset ); |
|---|
| 299 |
p_sys->i_offset = 0; |
|---|
| 300 |
block_BytestreamFlush( &p_sys->bytestream ); |
|---|
| 301 |
} |
|---|
| 302 |
|
|---|
| 303 |
if( p_sys->i_state != STATE_NEXT_SYNC ) |
|---|
| 304 |
{ |
|---|
| 305 |
|
|---|
| 306 |
return NULL; |
|---|
| 307 |
} |
|---|
| 308 |
|
|---|
| 309 |
p_sys->i_offset = 1; |
|---|
| 310 |
|
|---|
| 311 |
case STATE_NEXT_SYNC: |
|---|
| 312 |
|
|---|
| 313 |
|
|---|
| 314 |
|
|---|
| 315 |
|
|---|
| 316 |
if( block_FindStartcodeFromOffset( &p_sys->bytestream, |
|---|
| 317 |
&p_sys->i_offset, p_sys->p_startcode, 3 ) != VLC_SUCCESS ) |
|---|
| 318 |
{ |
|---|
| 319 |
|
|---|
| 320 |
return NULL; |
|---|
| 321 |
} |
|---|
| 322 |
|
|---|
| 323 |
|
|---|
| 324 |
p_pic = block_New( p_dec, p_sys->i_offset ); |
|---|
| 325 |
block_BytestreamFlush( &p_sys->bytestream ); |
|---|
| 326 |
p_pic->i_pts = p_sys->bytestream.p_block->i_pts; |
|---|
| 327 |
p_pic->i_dts = p_sys->bytestream.p_block->i_dts; |
|---|
| 328 |
|
|---|
| 329 |
block_GetBytes( &p_sys->bytestream, p_pic->p_buffer, |
|---|
| 330 |
p_pic->i_buffer ); |
|---|
| 331 |
|
|---|
| 332 |
|
|---|
| 333 |
if( p_pic->i_buffer >= 4 && p_pic->p_buffer[3] == 0x00 ) |
|---|
| 334 |
{ |
|---|
| 335 |
|
|---|
| 336 |
p_sys->bytestream.p_block->i_pts = 0; |
|---|
| 337 |
p_sys->bytestream.p_block->i_dts = 0; |
|---|
| 338 |
} |
|---|
| 339 |
|
|---|
| 340 |
p_sys->i_offset = 0; |
|---|
| 341 |
|
|---|
| 342 |
|
|---|
| 343 |
if( !( p_pic = ParseMPEGBlock( p_dec, p_pic ) ) ) |
|---|
| 344 |
{ |
|---|
| 345 |
p_sys->i_state = STATE_NOSYNC; |
|---|
| 346 |
break; |
|---|
| 347 |
} |
|---|
| 348 |
|
|---|
| 349 |
|
|---|
| 350 |
|
|---|
| 351 |
if( p_sys->b_discontinuity && |
|---|
| 352 |
p_sys->b_sync_on_intra_frame ) |
|---|
| 353 |
{ |
|---|
| 354 |
if( p_pic->i_flags & BLOCK_FLAG_TYPE_I ) |
|---|
| 355 |
{ |
|---|
| 356 |
msg_Dbg( p_dec, "synced on intra frame" ); |
|---|
| 357 |
p_sys->b_discontinuity = false; |
|---|
| 358 |
p_pic->i_flags |= BLOCK_FLAG_DISCONTINUITY; |
|---|
| 359 |
} |
|---|
| 360 |
else |
|---|
| 361 |
{ |
|---|
| 362 |
msg_Dbg( p_dec, "waiting on intra frame" ); |
|---|
| 363 |
p_sys->i_state = STATE_NOSYNC; |
|---|
| 364 |
block_Release( p_pic ); |
|---|
| 365 |
break; |
|---|
| 366 |
} |
|---|
| 367 |
} |
|---|
| 368 |
|
|---|
| 369 |
|
|---|
| 370 |
|
|---|
| 371 |
if( p_sys->i_dts <= 0 && p_sys->i_pts <= 0 && |
|---|
| 372 |
p_sys->i_interpolated_dts <= 0 ) |
|---|
| 373 |
{ |
|---|
| 374 |
msg_Dbg( p_dec, "need a starting pts/dts" ); |
|---|
| 375 |
p_sys->i_state = STATE_NOSYNC; |
|---|
| 376 |
block_Release( p_pic ); |
|---|
| 377 |
break; |
|---|
| 378 |
} |
|---|
| 379 |
|
|---|
| 380 |
|
|---|
| 381 |
|
|---|
| 382 |
if( !p_pic->i_dts ) p_pic->i_dts = p_pic->i_pts; |
|---|
| 383 |
|
|---|
| 384 |
|
|---|
| 385 |
*pp_block = block_BytestreamPop( &p_sys->bytestream ); |
|---|
| 386 |
|
|---|
| 387 |
p_sys->i_state = STATE_NOSYNC; |
|---|
| 388 |
|
|---|
| 389 |
return p_pic; |
|---|
| 390 |
} |
|---|
| 391 |
} |
|---|
| 392 |
} |
|---|
| 393 |
|
|---|
| 394 |
|
|---|
| 395 |
|
|---|
| 396 |
|
|---|
| 397 |
static block_t *GetCc( decoder_t *p_dec, bool pb_present[4] ) |
|---|
| 398 |
{ |
|---|
| 399 |
decoder_sys_t *p_sys = p_dec->p_sys; |
|---|
| 400 |
block_t *p_cc; |
|---|
| 401 |
int i; |
|---|
| 402 |
|
|---|
| 403 |
for( i = 0; i < 4; i++ ) |
|---|
| 404 |
pb_present[i] = p_sys->cc.pb_present[i]; |
|---|
| 405 |
|
|---|
| 406 |
if( p_sys->cc.i_data <= 0 ) |
|---|
| 407 |
return NULL; |
|---|
| 408 |
|
|---|
| 409 |
p_cc = block_New( p_dec, p_sys->cc.i_data); |
|---|
| 410 |
if( p_cc ) |
|---|
| 411 |
{ |
|---|
| 412 |
memcpy( p_cc->p_buffer, p_sys->cc.p_data, p_sys->cc.i_data ); |
|---|
| 413 |
p_cc->i_dts = |
|---|
| 414 |
p_cc->i_pts = p_sys->cc.b_reorder ? p_sys->i_cc_pts : p_sys->i_cc_dts; |
|---|
| 415 |
p_cc->i_flags = ( p_sys->cc.b_reorder ? p_sys->i_cc_flags : BLOCK_FLAG_TYPE_P ) & ( BLOCK_FLAG_TYPE_I|BLOCK_FLAG_TYPE_P|BLOCK_FLAG_TYPE_B); |
|---|
| 416 |
} |
|---|
| 417 |
cc_Flush( &p_sys->cc ); |
|---|
| 418 |
return p_cc; |
|---|
| 419 |
} |
|---|
| 420 |
|
|---|
| 421 |
|
|---|
| 422 |
|
|---|
| 423 |
|
|---|
| 424 |
static block_t *ParseMPEGBlock( decoder_t *p_dec, block_t *p_frag ) |
|---|
| 425 |
{ |
|---|
| 426 |
decoder_sys_t *p_sys = p_dec->p_sys; |
|---|
| 427 |
block_t *p_pic = NULL; |
|---|
| 428 |
|
|---|
| 429 |
|
|---|
| 430 |
|
|---|
| 431 |
|
|---|
| 432 |
if( ( p_sys->b_frame_slice && |
|---|
| 433 |
(p_frag->p_buffer[3] == 0x00 || p_frag->p_buffer[3] > 0xaf) ) && |
|---|
| 434 |
p_sys->p_seq == NULL ) |
|---|
| 435 |
{ |
|---|
| 436 |
|
|---|
| 437 |
|
|---|
| 438 |
msg_Dbg( p_dec, "waiting for sequence start" ); |
|---|
| 439 |
if( p_sys->p_frame ) block_ChainRelease( p_sys->p_frame ); |
|---|
| 440 |
p_sys->p_frame = NULL; |
|---|
| 441 |
p_sys->pp_last = &p_sys->p_frame; |
|---|
| 442 |
p_sys->b_frame_slice = false; |
|---|
| 443 |
|
|---|
| 444 |
} |
|---|
| 445 |
else if( p_sys->b_frame_slice && |
|---|
| 446 |
(p_frag->p_buffer[3] == 0x00 || p_frag->p_buffer[3] > 0xaf) ) |
|---|
| 447 |
{ |
|---|
| 448 |
const bool b_eos = p_frag->p_buffer[3] == 0xb7; |
|---|
| 449 |
|
|---|
| 450 |
mtime_t i_duration; |
|---|
| 451 |
|
|---|
| 452 |
if( b_eos ) |
|---|
| 453 |
{ |
|---|
| 454 |
block_ChainLastAppend( &p_sys->pp_last, p_frag ); |
|---|
| 455 |
p_frag = NULL; |
|---|
| 456 |
} |
|---|
| 457 |
|
|---|
| 458 |
p_pic = block_ChainGather( p_sys->p_frame ); |
|---|
| 459 |
|
|---|
| 460 |
if( b_eos ) |
|---|
| 461 |
p_pic->i_flags |= BLOCK_FLAG_END_OF_SEQUENCE; |
|---|
| 462 |
|
|---|
| 463 |
i_duration = (mtime_t)( 1000000 * p_sys->i_frame_rate_base / |
|---|
| 464 |
p_sys->i_frame_rate ); |
|---|
| 465 |
|
|---|
| 466 |
if( !p_sys->b_seq_progressive && p_sys->i_picture_structure != 0x03 ) |
|---|
| 467 |
{ |
|---|
| 468 |
i_duration /= 2; |
|---|
| 469 |
} |
|---|
| 470 |
|
|---|
| 471 |
if( p_sys->b_seq_progressive ) |
|---|
| 472 |
{ |
|---|
| 473 |
if( p_sys->i_top_field_first == 0 && |
|---|
| 474 |
p_sys->i_repeat_first_field == 1 ) |
|---|
| 475 |
{ |
|---|
| 476 |
i_duration *= 2; |
|---|
| 477 |
} |
|---|
| 478 |
else if( p_sys->i_top_field_first == 1 && |
|---|
| 479 |
p_sys->i_repeat_first_field == 1 ) |
|---|
| 480 |
{ |
|---|
| 481 |
i_duration *= 3; |
|---|
| 482 |
} |
|---|
| 483 |
} |
|---|
| 484 |
else |
|---|
| 485 |
{ |
|---|
| 486 |
if( p_sys->i_picture_structure == 0x03 ) |
|---|
| 487 |
{ |
|---|
| 488 |
if( p_sys->i_progressive_frame && p_sys->i_repeat_first_field ) |
|---|
| 489 |
{ |
|---|
| 490 |
i_duration += i_duration / 2; |
|---|
| 491 |
} |
|---|
| 492 |
} |
|---|
| 493 |
} |
|---|
| 494 |
|
|---|
| 495 |
if( p_sys->b_low_delay || p_sys->i_picture_type == 0x03 ) |
|---|
| 496 |
{ |
|---|
| 497 |
|
|---|
| 498 |
|
|---|
| 499 |
if( p_sys->i_pts > 0 ) p_sys->i_interpolated_dts = p_sys->i_pts; |
|---|
| 500 |
if( p_sys->i_dts > 0 ) p_sys->i_interpolated_dts = p_sys->i_dts; |
|---|
| 501 |
} |
|---|
| 502 |
else |
|---|
| 503 |
{ |
|---|
| 504 |
|
|---|
| 505 |
if( p_sys->i_last_ref_pts > 0 && !p_sys->b_second_field ) |
|---|
| 506 |
p_sys->i_interpolated_dts = p_sys->i_last_ref_pts; |
|---|
| 507 |
if( p_sys->i_dts > 0 ) p_sys->i_interpolated_dts = p_sys->i_dts; |
|---|
| 508 |
|
|---|
| 509 |
if( !p_sys->b_second_field ) |
|---|
| 510 |
p_sys->i_last_ref_pts = p_sys->i_pts; |
|---|
| 511 |
} |
|---|
| 512 |
|
|---|
| 513 |
p_pic->i_dts = p_sys->i_interpolated_dts; |
|---|
| 514 |
p_sys->i_interpolated_dts += i_duration; |
|---|
| 515 |
|
|---|
| 516 |
|
|---|
| 517 |
if( p_sys->i_pts > 0 ) |
|---|
| 518 |
{ |
|---|
| 519 |
p_pic->i_pts = p_sys->i_pts; |
|---|
| 520 |
} |
|---|
| 521 |
else if( p_sys->i_picture_type == 0x03 ) |
|---|
| 522 |
{ |
|---|
| 523 |
p_pic->i_pts = p_pic->i_dts; |
|---|
| 524 |
} |
|---|
| 525 |
else |
|---|
| 526 |
{ |
|---|
| 527 |
p_pic->i_pts = 0; |
|---|
| 528 |
} |
|---|
| 529 |
|
|---|
| 530 |
switch ( p_sys->i_picture_type ) |
|---|
| 531 |
{ |
|---|
| 532 |
case 0x01: |
|---|
| 533 |
p_pic->i_flags |= BLOCK_FLAG_TYPE_I; |
|---|
| 534 |
break; |
|---|
| 535 |
case 0x02: |
|---|
| 536 |
p_pic->i_flags |= BLOCK_FLAG_TYPE_P; |
|---|
| 537 |
break; |
|---|
| 538 |
case 0x03: |
|---|
| 539 |
p_pic->i_flags |= BLOCK_FLAG_TYPE_B; |
|---|
| 540 |
break; |
|---|
| 541 |
} |
|---|
| 542 |
|
|---|
| 543 |
p_pic->i_length = p_sys->i_interpolated_dts - p_pic->i_dts; |
|---|
| 544 |
|
|---|
| 545 |
#if 0 |
|---|
| 546 |
msg_Dbg( p_dec, "pic: type=%d dts=%"PRId64" pts-dts=%"PRId64, |
|---|
| 547 |
p_sys->i_picture_type, p_pic->i_dts, p_pic->i_pts - p_pic->i_dts); |
|---|
| 548 |
#endif |
|---|
| 549 |
|
|---|
| 550 |
|
|---|
| 551 |
p_sys->p_frame = NULL; |
|---|
| 552 |
p_sys->pp_last = &p_sys->p_frame; |
|---|
| 553 |
p_sys->b_frame_slice = false; |
|---|
| 554 |
|
|---|
| 555 |
if( p_sys->i_picture_structure != 0x03 ) |
|---|
| 556 |
{ |
|---|
| 557 |
p_sys->b_second_field = !p_sys->b_second_field; |
|---|
| 558 |
} |
|---|
| 559 |
else |
|---|
| 560 |
{ |
|---|
| 561 |
p_sys->b_second_field = 0; |
|---|
| 562 |
} |
|---|
| 563 |
|
|---|
| 564 |
|
|---|
| 565 |
p_sys->b_cc_reset = true; |
|---|
| 566 |
p_sys->i_cc_pts = p_pic->i_pts; |
|---|
| 567 |
p_sys->i_cc_dts = p_pic->i_dts; |
|---|
| 568 |
p_sys->i_cc_flags = p_pic->i_flags; |
|---|
| 569 |
} |
|---|
| 570 |
|
|---|
| 571 |
if( !p_pic && p_sys->b_cc_reset ) |
|---|
| 572 |
{ |
|---|
| 573 |
p_sys->b_cc_reset = false; |
|---|
| 574 |
cc_Flush( &p_sys->cc ); |
|---|
| 575 |
} |
|---|
| 576 |
|
|---|
| 577 |
if( !p_frag ) |
|---|
| 578 |
return p_pic; |
|---|
| 579 |
|
|---|
| 580 |
|
|---|
| 581 |
|
|---|
| 582 |
if( p_frag->p_buffer[3] == 0xb8 ) |
|---|
| 583 |
{ |
|---|
| 584 |
|
|---|
| 585 |
if( p_sys->p_seq && |
|---|
| 586 |
p_sys->i_seq_old > p_sys->i_frame_rate/p_sys->i_frame_rate_base ) |
|---|
| 587 |
{ |
|---|
| 588 |
|
|---|
| 589 |
block_ChainLastAppend( &p_sys->pp_last, block_Duplicate( p_sys->p_seq ) ); |
|---|
| 590 |
if( p_sys->p_ext ) |
|---|
| 591 |
{ |
|---|
| 592 |
block_ChainLastAppend( &p_sys->pp_last, block_Duplicate( p_sys->p_ext ) ); |
|---|
| 593 |
} |
|---|
| 594 |
|
|---|
| 595 |
p_sys->i_seq_old = 0; |
|---|
| 596 |
} |
|---|
| 597 |
} |
|---|
| 598 |
else if( p_frag->p_buffer[3] == 0xb3 && p_frag->i_buffer >= 8 ) |
|---|
| 599 |
{ |
|---|
| 600 |
|
|---|
| 601 |
static const int code_to_frame_rate[16][2] = |
|---|
| 602 |
{ |
|---|
| 603 |
{ 1, 1 }, |
|---|
| 604 |
{ 24000, 1001 }, { 24, 1 }, { 25, 1 }, { 30000, 1001 }, |
|---|
| 605 |
{ 30, 1 }, { 50, 1 }, { 60000, 1001 }, { 60, 1 }, |
|---|
| 606 |
|
|---|
| 607 |
{ 15, 1001 }, |
|---|
| 608 |
|
|---|
| 609 |
{ 5, 1001 }, { 10, 1001 }, { 12, 1001 }, { 15, 1001 }, |
|---|
| 610 |
{ 1, 1 }, { 1, 1 } |
|---|
| 611 |
}; |
|---|
| 612 |
|
|---|
| 613 |
if( p_sys->p_seq ) block_Release( p_sys->p_seq ); |
|---|
| 614 |
if( p_sys->p_ext ) block_Release( p_sys->p_ext ); |
|---|
| 615 |
|
|---|
| 616 |
p_sys->p_seq = block_Duplicate( p_frag ); |
|---|
| 617 |
p_sys->i_seq_old = 0; |
|---|
| 618 |
p_sys->p_ext = NULL; |
|---|
| 619 |
|
|---|
| 620 |
p_dec->fmt_out.video.i_width = |
|---|
| 621 |
( p_frag->p_buffer[4] << 4)|(p_frag->p_buffer[5] >> 4 ); |
|---|
| 622 |
p_dec->fmt_out.video.i_height = |
|---|
| 623 |
( (p_frag->p_buffer[5]&0x0f) << 8 )|p_frag->p_buffer[6]; |
|---|
| 624 |
p_sys->i_aspect_ratio_info = p_frag->p_buffer[7] >> 4; |
|---|
| 625 |
|
|---|
| 626 |
|
|---|
| 627 |
|
|---|
| 628 |
p_sys->i_frame_rate = code_to_frame_rate[p_frag->p_buffer[7]&0x0f][0]; |
|---|
| 629 |
p_sys->i_frame_rate_base = |
|---|
| 630 |
code_to_frame_rate[p_frag->p_buffer[7]&0x0f][1]; |
|---|
| 631 |
|
|---|
| 632 |
p_dec->fmt_out.video.i_frame_rate = p_sys->i_frame_rate; |
|---|
| 633 |
p_dec->fmt_out.video.i_frame_rate_base = p_sys->i_frame_rate_base; |
|---|
| 634 |
|
|---|
| 635 |
p_sys->b_seq_progressive = true; |
|---|
| 636 |
p_sys->b_low_delay = true; |
|---|
| 637 |
|
|---|
| 638 |
if ( !p_sys->b_inited ) |
|---|
| 639 |
{ |
|---|
| 640 |
msg_Dbg( p_dec, "size %dx%d fps=%.3f", |
|---|
| 641 |
p_dec->fmt_out.video.i_width, p_dec->fmt_out.video.i_height, |
|---|
| 642 |
p_sys->i_frame_rate / (float)p_sys->i_frame_rate_base ); |
|---|
| 643 |
p_sys->b_inited = 1; |
|---|
| 644 |
} |
|---|
| 645 |
} |
|---|
| 646 |
else if( p_frag->p_buffer[3] == 0xb5 ) |
|---|
| 647 |
{ |
|---|
| 648 |
int i_type = p_frag->p_buffer[4] >> 4; |
|---|
| 649 |
|
|---|
| 650 |
|
|---|
| 651 |
if( i_type == 0x01 ) |
|---|
| 652 |
{ |
|---|
| 653 |
#if 0 |
|---|
| 654 |
static const int mpeg2_aspect[16][2] = |
|---|
| 655 |
{ |
|---|
| 656 |
{0,1}, {1,1}, {4,3}, {16,9}, {221,100}, |
|---|
| 657 |
{0,1}, {0,1}, {0,1}, {0,1}, {0,1}, {0,1}, {0,1}, {0,1}, {0,1}, |
|---|
| 658 |
{0,1}, {0,1} |
|---|
| 659 |
}; |
|---|
| 660 |
#endif |
|---|
| 661 |
|
|---|
| 662 |
|
|---|
| 663 |
if( p_sys->p_ext) block_Release( p_sys->p_ext ); |
|---|
| 664 |
p_sys->p_ext = block_Duplicate( p_frag ); |
|---|
| 665 |
|
|---|
| 666 |
if( p_frag->i_buffer >= 10 ) |
|---|
| 667 |
{ |
|---|
| 668 |
p_sys->b_seq_progressive = |
|---|
| 669 |
p_frag->p_buffer[5]&0x08 ? true : false; |
|---|
| 670 |
p_sys->b_low_delay = |
|---|
| 671 |
p_frag->p_buffer[9]&0x80 ? true : false; |
|---|
| 672 |
} |
|---|
| 673 |
|
|---|
| 674 |
|
|---|
| 675 |
|
|---|
| 676 |
|
|---|
| 677 |
|
|---|
| 678 |
|
|---|
| 679 |
|
|---|
| 680 |
#if 0 |
|---|
| 681 |
p_dec->fmt_out.video.i_aspect = |
|---|
| 682 |
mpeg2_aspect[p_sys->i_aspect_ratio_info][0] * |
|---|
| 683 |
VOUT_ASPECT_FACTOR / |
|---|
| 684 |
mpeg2_aspect[p_sys->i_aspect_ratio_info][1]; |
|---|
| 685 |
#endif |
|---|
| 686 |
|
|---|
| 687 |
} |
|---|
| 688 |
else if( i_type == 0x08 ) |
|---|
| 689 |
{ |
|---|
| 690 |
|
|---|
| 691 |
p_sys->i_picture_structure = p_frag->p_buffer[6]&0x03; |
|---|
| 692 |
p_sys->i_top_field_first = p_frag->p_buffer[7] >> 7; |
|---|
| 693 |
p_sys->i_repeat_first_field= (p_frag->p_buffer[7]>>1)&0x01; |
|---|
| 694 |
p_sys->i_progressive_frame = p_frag->p_buffer[8] >> 7; |
|---|
| 695 |
} |
|---|
| 696 |
} |
|---|
| 697 |
else if( p_frag->p_buffer[3] == 0xb2 && p_frag->i_buffer > 4 ) |
|---|
| 698 |
{ |
|---|
| 699 |
cc_Extract( &p_sys->cc, &p_frag->p_buffer[4], p_frag->i_buffer - 4 ); |
|---|
| 700 |
} |
|---|
| 701 |
else if( p_frag->p_buffer[3] == 0x00 ) |
|---|
| 702 |
{ |
|---|
| 703 |
|
|---|
| 704 |
p_sys->i_seq_old++; |
|---|
| 705 |
|
|---|
| 706 |
if( p_frag->i_buffer >= 6 ) |
|---|
| 707 |
{ |
|---|
| 708 |
p_sys->i_temporal_ref = |
|---|
| 709 |
( p_frag->p_buffer[4] << 2 )|(p_frag->p_buffer[5] >> 6); |
|---|
| 710 |
p_sys->i_picture_type = ( p_frag->p_buffer[5] >> 3 ) & 0x03; |
|---|
| 711 |
} |
|---|
| 712 |
|
|---|
| 713 |
p_sys->i_dts = p_frag->i_dts; |
|---|
| 714 |
p_sys->i_pts = p_frag->i_pts; |
|---|
| 715 |
} |
|---|
| 716 |
else if( p_frag->p_buffer[3] >= 0x01 && p_frag->p_buffer[3] <= 0xaf ) |
|---|
| 717 |
{ |
|---|
| 718 |
|
|---|
| 719 |
p_sys->b_frame_slice = true; |
|---|
| 720 |
} |
|---|
| 721 |
|
|---|
| 722 |
|
|---|
| 723 |
block_ChainLastAppend( &p_sys->pp_last, p_frag ); |
|---|
| 724 |
|
|---|
| 725 |
return p_pic; |
|---|
| 726 |
} |
|---|