1 /*****************************************************************************
3 *****************************************************************************
4 * Copyright (C) 2001, 2002, 2006 the VideoLAN team
7 * Authors: Laurent Aimar <fenrir@via.ecp.fr>
8 * Gildas Bazin <gbazin@videolan.org>
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
23 *****************************************************************************/
25 /*****************************************************************************
27 *****************************************************************************/
33 #include <vlc_common.h>
34 #include <vlc_plugin.h>
35 #include <vlc_codec.h>
36 #include <vlc_block.h>
39 #include "vlc_block_helper.h"
41 /*****************************************************************************
43 *****************************************************************************/
44 static int Open ( vlc_object_t * );
45 static void Close( vlc_object_t * );
48 set_category( CAT_SOUT );
49 set_subcategory( SUBCAT_SOUT_PACKETIZER );
50 set_description( N_("VC-1 packetizer") );
51 set_capability( "packetizer", 50 );
52 set_callbacks( Open, Close );
55 /*****************************************************************************
57 *****************************************************************************/
63 block_bytestream_t bytestream;
66 uint8_t p_startcode[3];
68 /* Current sequence header */
69 bool b_sequence_header;
73 bool b_advanced_profile;
75 bool b_frame_interpolation;
76 bool b_range_reduction;
88 /* Current frame being built */
93 mtime_t i_interpolated_dts;
104 IDU_TYPE_SEQUENCE_HEADER = 0x0f,
105 IDU_TYPE_ENTRY_POINT = 0x0e,
106 IDU_TYPE_FRAME = 0x0D,
107 IDU_TYPE_FIELD = 0x0C,
108 IDU_TYPE_SLICE = 0x0B,
109 IDU_TYPE_END_OF_SEQUENCE = 0x0B,
111 IDU_TYPE_SEQUENCE_LEVEL_USER_DATA = 0x1F,
112 IDU_TYPE_ENTRY_POINT_USER_DATA = 0x1E,
113 IDU_TYPE_FRAME_USER_DATA = 0x1D,
114 IDU_TYPE_FIELD_USER_DATA = 0x1C,
115 IDU_TYPE_SLICE_USER_DATA = 0x1B,
118 static block_t *Packetize( decoder_t *p_dec, block_t **pp_block );
120 /*****************************************************************************
121 * Open: probe the packetizer and return score
122 *****************************************************************************
123 * Tries to launch a decoder and return score so that the interface is able
125 *****************************************************************************/
126 static int Open( vlc_object_t *p_this )
128 decoder_t *p_dec = (decoder_t*)p_this;
129 decoder_sys_t *p_sys;
131 if( p_dec->fmt_in.i_codec != VLC_FOURCC( 'W', 'V', 'C', '1' ) )
134 p_dec->pf_packetize = Packetize;
136 /* Create the output format */
137 es_format_Copy( &p_dec->fmt_out, &p_dec->fmt_in );
138 p_dec->p_sys = p_sys = malloc( sizeof( decoder_sys_t ) );
140 p_sys->i_state = STATE_NOSYNC;
141 p_sys->bytestream = block_BytestreamInit();
142 p_sys->p_startcode[0] = 0x00;
143 p_sys->p_startcode[1] = 0x00;
144 p_sys->p_startcode[2] = 0x01;
147 p_sys->b_sequence_header = false;
148 p_sys->sh.p_sh = NULL;
149 p_sys->b_entry_point = false;
150 p_sys->ep.p_ep = NULL;
152 p_sys->b_frame = false;
153 p_sys->p_frame = NULL;
154 p_sys->pp_last = &p_sys->p_frame;
156 p_sys->i_interpolated_dts = -1;
159 if( p_dec->fmt_in.i_extra > 0 )
161 block_t *p_init = block_New( p_dec, p_dec->fmt_in.i_extra );
164 memcpy( p_init->p_buffer, p_dec->fmt_in.p_extra,
165 p_dec->fmt_in.i_extra );
167 while( ( p_pic = Packetize( p_dec, &p_init ) ) )
168 block_Release( p_pic ); /* Should not happen (only sequence header) */
174 /*****************************************************************************
176 *****************************************************************************/
177 static void Close( vlc_object_t *p_this )
179 decoder_t *p_dec = (decoder_t*)p_this;
180 decoder_sys_t *p_sys = p_dec->p_sys;
182 block_BytestreamRelease( &p_sys->bytestream );
184 block_Release( p_sys->p_frame );
188 /*****************************************************************************
189 * Packetize: packetize an access unit
190 *****************************************************************************/
191 static block_t *ParseIDU( decoder_t *p_dec, block_t *p_frag );
193 static block_t *Packetize( decoder_t *p_dec, block_t **pp_block )
195 decoder_sys_t *p_sys = p_dec->p_sys;
198 if( pp_block == NULL || *pp_block == NULL )
201 if( (*pp_block)->i_flags&(BLOCK_FLAG_DISCONTINUITY|BLOCK_FLAG_CORRUPTED) )
203 if( (*pp_block)->i_flags&BLOCK_FLAG_CORRUPTED )
205 p_sys->i_state = STATE_NOSYNC;
206 block_BytestreamFlush( &p_sys->bytestream );
209 block_ChainRelease( p_sys->p_frame );
210 p_sys->p_frame = NULL;
211 p_sys->pp_last = &p_sys->p_frame;
212 p_sys->b_frame = false;
214 // p_sys->i_interpolated_dts = 0;
215 block_Release( *pp_block );
219 block_BytestreamPush( &p_sys->bytestream, *pp_block );
223 switch( p_sys->i_state )
227 if( block_FindStartcodeFromOffset( &p_sys->bytestream, &p_sys->i_offset, p_sys->p_startcode, 3 ) == VLC_SUCCESS )
228 p_sys->i_state = STATE_NEXT_SYNC;
230 if( p_sys->i_offset )
232 block_SkipBytes( &p_sys->bytestream, p_sys->i_offset );
234 block_BytestreamFlush( &p_sys->bytestream );
237 if( p_sys->i_state != STATE_NEXT_SYNC )
238 return NULL; /* Need more data */
240 p_sys->i_offset = 4; /* To find next startcode */
242 case STATE_NEXT_SYNC:
243 /* TODO: If p_block == NULL, flush the buffer without checking the
246 /* Find the next startcode */
247 if( block_FindStartcodeFromOffset( &p_sys->bytestream, &p_sys->i_offset, p_sys->p_startcode, 3 ) != VLC_SUCCESS )
248 return NULL; /* Need more data */
250 /* Get the new fragment and set the pts/dts */
251 p_pic = block_New( p_dec, p_sys->i_offset );
252 block_BytestreamFlush( &p_sys->bytestream );
253 p_pic->i_pts = p_sys->bytestream.p_block->i_pts;
254 p_pic->i_dts = p_sys->bytestream.p_block->i_dts;
255 p_pic->i_rate = p_sys->bytestream.p_block->i_rate;
257 block_GetBytes( &p_sys->bytestream, p_pic->p_buffer, p_pic->i_buffer );
261 /* Parse and return complete frame */
262 p_pic = ParseIDU( p_dec, p_pic );
264 /* Don't reuse the same timestamps several times */
265 if( p_sys->p_frame &&
266 p_sys->p_frame->i_dts == p_sys->bytestream.p_block->i_dts &&
267 p_sys->p_frame->i_pts == p_sys->bytestream.p_block->i_pts )
269 p_sys->bytestream.p_block->i_pts = -1;
270 p_sys->bytestream.p_block->i_dts = -1;
276 p_sys->i_state = STATE_NOSYNC;
280 if( p_sys->i_interpolated_dts < 0 )
282 msg_Dbg( p_dec, "need a starting pts/dts" );
283 p_sys->i_state = STATE_NOSYNC;
284 block_Release( p_pic );
288 /* So p_block doesn't get re-added several times */
289 *pp_block = block_BytestreamPop( &p_sys->bytestream );
291 p_sys->i_state = STATE_NOSYNC;
298 /* DecodeRIDU: decode the startcode emulation prevention (same than h264) */
299 static void DecodeRIDU( uint8_t *p_ret, int *pi_ret, uint8_t *src, int i_src )
301 uint8_t *end = &src[i_src];
302 uint8_t *dst_end = &p_ret[*pi_ret];
303 uint8_t *dst = p_ret;
305 while( src < end && dst < dst_end )
307 if( src < end - 3 && src[0] == 0x00 && src[1] == 0x00 &&
319 *pi_ret = dst - p_ret;
321 /* BuildExtraData: gather sequence header and entry point */
322 static void BuildExtraData( decoder_t *p_dec )
324 decoder_sys_t *p_sys = p_dec->p_sys;
325 es_format_t *p_es = &p_dec->fmt_out;
327 if( !p_sys->b_sequence_header || !p_sys->b_entry_point )
330 i_extra = p_sys->sh.p_sh->i_buffer + p_sys->ep.p_ep->i_buffer;
331 if( p_es->i_extra != i_extra )
333 p_es->i_extra = i_extra;
334 p_es->p_extra = realloc( p_dec->fmt_out.p_extra, p_es->i_extra );
336 memcpy( p_es->p_extra,
337 p_sys->sh.p_sh->p_buffer, p_sys->sh.p_sh->i_buffer );
338 memcpy( (uint8_t*)p_es->p_extra + p_sys->sh.p_sh->i_buffer,
339 p_sys->ep.p_ep->p_buffer, p_sys->ep.p_ep->i_buffer );
341 /* ParseIDU: parse an Independent Decoding Unit */
342 static block_t *ParseIDU( decoder_t *p_dec, block_t *p_frag )
344 decoder_sys_t *p_sys = p_dec->p_sys;
346 const idu_type_t idu = p_frag->p_buffer[3];
348 if( !p_sys->b_sequence_header && idu != IDU_TYPE_SEQUENCE_HEADER )
350 msg_Warn( p_dec, "waiting for sequence header" );
351 block_Release( p_frag );
354 if( p_sys->b_sequence_header && !p_sys->b_entry_point && idu != IDU_TYPE_ENTRY_POINT )
356 msg_Warn( p_dec, "waiting for entry point" );
357 block_Release( p_frag );
360 /* TODO we do not gather ENTRY_POINT and SEQUENCE_DATA user data
361 * But It should not be a problem for decoder */
363 /* Do we have completed a frame */
365 if( p_sys->b_frame &&
366 idu != IDU_TYPE_FRAME_USER_DATA &&
367 idu != IDU_TYPE_FIELD && idu != IDU_TYPE_FIELD_USER_DATA &&
368 idu != IDU_TYPE_SLICE && idu != IDU_TYPE_SLICE_USER_DATA &&
369 idu != IDU_TYPE_END_OF_SEQUENCE )
372 p_pic = block_ChainGather( p_sys->p_frame );
374 /* We can interpolate dts/pts only if we have a frame rate */
375 if( p_dec->fmt_out.video.i_frame_rate != 0 && p_dec->fmt_out.video.i_frame_rate_base != 0 )
377 //msg_Dbg( p_dec, "-------------- XXX0 dts=%"PRId64" pts=%"PRId64" interpolated=%"PRId64, p_pic->i_dts, p_pic->i_pts, p_sys->i_interpolated_dts );
378 if( p_pic->i_dts <= 0 )
379 p_pic->i_dts = p_sys->i_interpolated_dts;
381 p_sys->i_interpolated_dts += INT64_C(1000000) * p_dec->fmt_out.video.i_frame_rate_base / p_dec->fmt_out.video.i_frame_rate;
382 if( p_pic->i_pts <= 0 )
384 if( !p_sys->sh.b_has_bframe || (p_pic->i_flags & BLOCK_FLAG_TYPE_B ) )
385 p_pic->i_pts = p_pic->i_dts;
386 /* TODO compute pts for other case */
389 p_sys->i_interpolated_dts = p_pic->i_dts;
391 //msg_Dbg( p_dec, "-------------- dts=%"PRId64" pts=%"PRId64, p_pic->i_dts, p_pic->i_pts );
394 p_sys->p_frame = NULL;
395 p_sys->pp_last = &p_sys->p_frame;
401 block_t *p_frame = p_sys->p_frame;
402 if( p_frame->i_dts < 0 )
403 p_frame->i_dts = p_frag->i_dts;
404 if( p_frame->i_pts < 0 )
405 p_frame->i_pts = p_frag->i_pts;
407 block_ChainLastAppend( &p_sys->pp_last, p_frag );
410 if( idu == IDU_TYPE_SEQUENCE_HEADER )
412 es_format_t *p_es = &p_dec->fmt_out;
416 int i_ridu = sizeof(ridu);
420 block_Release( p_sys->sh.p_sh );
421 p_sys->sh.p_sh = block_Duplicate( p_frag );
423 /* Extract the raw IDU */
424 DecodeRIDU( ridu, &i_ridu, &p_frag->p_buffer[4], p_frag->i_buffer - 4 );
426 /* Auto detect VC-1_SPMP_PESpacket_PayloadFormatHeader (SMPTE RP 227) for simple/main profile
427 * TODO find a test case and valid it */
428 if( i_ridu > 4 && (ridu[0]&0x80) == 0 ) /* for advanced profile, the first bit is 1 */
430 video_format_t *p_v = &p_dec->fmt_in.video;
431 const size_t i_potential_width = GetWBE( &ridu[0] );
432 const size_t i_potential_height = GetWBE( &ridu[2] );
434 if( i_potential_width >= 2 && i_potential_width <= 8192 &&
435 i_potential_height >= 2 && i_potential_height <= 8192 )
437 if( ( p_v->i_width <= 0 && p_v->i_height <= 0 ) ||
438 ( p_v->i_width == i_potential_width && p_v->i_height == i_potential_height ) )
440 static const uint8_t startcode[4] = { 0x00, 0x00, 0x01, IDU_TYPE_SEQUENCE_HEADER };
441 p_es->video.i_width = i_potential_width;
442 p_es->video.i_height = i_potential_height;
445 p_frag->p_buffer += 4;
446 p_frag->i_buffer -= 4;
447 memcpy( p_frag->p_buffer, startcode, sizeof(startcode) );
453 bs_init( &s, ridu, i_ridu );
454 i_profile = bs_read( &s, 2 );
457 const int i_level = bs_read( &s, 3 );
459 /* Advanced profile */
460 p_sys->sh.b_advanced_profile = true;
461 p_sys->sh.b_range_reduction = false;
462 p_sys->sh.b_has_bframe = true;
464 bs_skip( &s, 2+3+5+1 ); // chroma format + frame rate Q + bit rate Q + postprocflag
466 p_es->video.i_width = 2*bs_read( &s, 12 )+2;
467 p_es->video.i_height = 2*bs_read( &s, 12 )+2;
469 if( !p_sys->b_sequence_header )
470 msg_Dbg( p_dec, "found sequence header for advanced profile level L%d resolution %dx%d",
471 i_level, p_es->video.i_width, p_es->video.i_height);
473 bs_skip( &s, 1 );// pulldown
474 p_sys->sh.b_interlaced = bs_read( &s, 1 );
475 bs_skip( &s, 1 );// frame counter
476 p_sys->sh.b_frame_interpolation = bs_read( &s, 1 );
477 bs_skip( &s, 1 ); // Reserved
478 bs_skip( &s, 1 ); // Psf
480 if( bs_read( &s, 1 ) ) /* Display extension */
482 const int i_display_width = bs_read( &s, 14 )+1;
483 const int i_display_height = bs_read( &s, 14 )+1;
485 p_es->video.i_aspect = VOUT_ASPECT_FACTOR * i_display_width / i_display_height;
487 if( !p_sys->b_sequence_header )
488 msg_Dbg( p_dec, "display size %dx%d", i_display_width, i_display_height );
490 if( bs_read( &s, 1 ) ) /* Pixel aspect ratio (PAR/SAR) */
492 static const int p_ar[16][2] = {
493 { 0, 0}, { 1, 1}, {12,11}, {10,11}, {16,11}, {40,33},
494 {24,11}, {20,11}, {32,11}, {80,33}, {18,11}, {15,11},
495 {64,33}, {160,99},{ 0, 0}, { 0, 0}
497 int i_ar = bs_read( &s, 4 );
498 unsigned i_ar_w, i_ar_h;
502 i_ar_w = bs_read( &s, 8 );
503 i_ar_h = bs_read( &s, 8 );
507 i_ar_w = p_ar[i_ar][0];
508 i_ar_h = p_ar[i_ar][1];
510 vlc_ureduce( &i_ar_w, &i_ar_h, i_ar_w, i_ar_h, 0 );
511 if( !p_sys->b_sequence_header )
512 msg_Dbg( p_dec, "aspect ratio %d:%d", i_ar_w, i_ar_h );
515 if( bs_read( &s, 1 ) ) /* Frame rate */
519 if( bs_read( &s, 1 ) )
521 i_fps_num = bs_read( &s, 16 )+1;
526 const int i_nr = bs_read( &s, 8 );
527 const int i_dn = bs_read( &s, 4 );
531 case 1: i_fps_num = 24000; break;
532 case 2: i_fps_num = 25000; break;
533 case 3: i_fps_num = 30000; break;
534 case 4: i_fps_num = 50000; break;
535 case 5: i_fps_num = 60000; break;
536 case 6: i_fps_num = 48000; break;
537 case 7: i_fps_num = 72000; break;
541 case 1: i_fps_den = 1000; break;
542 case 2: i_fps_den = 1001; break;
545 if( i_fps_num != 0 && i_fps_den != 0 )
546 vlc_ureduce( &p_es->video.i_frame_rate, &p_es->video.i_frame_rate_base, i_fps_num, i_fps_den, 0 );
548 if( !p_sys->b_sequence_header )
549 msg_Dbg( p_dec, "frame rate %d/%d", p_es->video.i_frame_rate, p_es->video.i_frame_rate_base );
554 /* Simple and main profile */
555 p_sys->sh.b_advanced_profile = false;
556 p_sys->sh.b_interlaced = false;
558 if( !p_sys->b_sequence_header )
559 msg_Dbg( p_dec, "found sequence header for %s profile", i_profile == 0 ? "simple" : "main" );
561 bs_skip( &s, 2+3+5+1+1+ // reserved + frame rate Q + bit rate Q + loop filter + reserved
562 1+1+1+1+2+ // multiresolution + reserved + fast uv mc + extended mv + dquant
563 1+1+1+1 ); // variable size transform + reserved + overlap + sync marker
564 p_sys->sh.b_range_reduction = bs_read( &s, 1 );
565 if( bs_read( &s, 3 ) > 0 )
566 p_sys->sh.b_has_bframe = true;
568 p_sys->sh.b_has_bframe = false;
569 bs_skip( &s, 2 ); // quantizer
571 p_sys->sh.b_frame_interpolation = bs_read( &s, 1 );
573 p_sys->b_sequence_header = true;
574 BuildExtraData( p_dec );
576 else if( idu == IDU_TYPE_ENTRY_POINT )
579 block_Release( p_sys->ep.p_ep );
580 p_sys->ep.p_ep = block_Duplicate( p_frag );
582 p_sys->b_entry_point = true;
583 BuildExtraData( p_dec );
585 else if( idu == IDU_TYPE_FRAME )
589 int i_ridu = sizeof(ridu);
591 /* Extract the raw IDU */
592 DecodeRIDU( ridu, &i_ridu, &p_frag->p_buffer[4], p_frag->i_buffer - 4 );
594 /* Parse it + interpolate pts/dts if possible */
595 bs_init( &s, ridu, i_ridu );
597 if( p_sys->sh.b_advanced_profile )
601 if( p_sys->sh.b_interlaced )
603 if( bs_read( &s, 1 ) )
605 if( bs_read( &s, 1 ) )
606 i_fcm = 1; /* interlaced field */
608 i_fcm = 2; /* interlaced frame */
612 if( i_fcm == 1 ) /*interlaced field */
614 /* XXX for mixed I/P we should check reference usage before marking them I (too much work) */
615 switch( bs_read( &s, 3 ) )
620 p_sys->p_frame->i_flags |= BLOCK_FLAG_TYPE_I;
621 p_sys->p_frame->i_flags |= BLOCK_FLAG_TYPE_I;
622 p_sys->p_frame->i_flags |= BLOCK_FLAG_TYPE_I;
625 p_sys->p_frame->i_flags |= BLOCK_FLAG_TYPE_P;
631 p_sys->p_frame->i_flags |= BLOCK_FLAG_TYPE_B;
637 if( !bs_read( &s, 1 ) )
638 p_sys->p_frame->i_flags |= BLOCK_FLAG_TYPE_P;
639 else if( !bs_read( &s, 1 ) )
640 p_sys->p_frame->i_flags |= BLOCK_FLAG_TYPE_B;
641 else if( !bs_read( &s, 1 ) )
642 p_sys->p_frame->i_flags |= BLOCK_FLAG_TYPE_I;
643 else if( !bs_read( &s, 1 ) )
644 p_sys->p_frame->i_flags |= BLOCK_FLAG_TYPE_B; /* Bi */
646 p_sys->p_frame->i_flags |= BLOCK_FLAG_TYPE_P; /* P Skip */
651 if( p_sys->sh.b_frame_interpolation )
652 bs_skip( &s, 1 ); // interpolate
653 bs_skip( &s, 2 ); // frame count
654 if( p_sys->sh.b_range_reduction )
655 bs_skip( &s, 1 ); // range reduction
657 if( bs_read( &s, 1 ) )
658 p_sys->p_frame->i_flags |= BLOCK_FLAG_TYPE_P;
659 else if( !p_sys->sh.b_has_bframe || bs_read( &s, 1 ) )
660 p_sys->p_frame->i_flags |= BLOCK_FLAG_TYPE_I;
662 p_sys->p_frame->i_flags |= BLOCK_FLAG_TYPE_B;
664 p_sys->b_frame = true;