1 /*****************************************************************************
3 *****************************************************************************
4 * Copyright (C) 2001, 2002, 2006 the VideoLAN team
5 * $Id: copy.c 18231 2006-12-03 17:02:02Z courmisch $
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 *****************************************************************************/
28 #include <stdlib.h> /* malloc(), free() */
31 #include <vlc_codec.h>
32 #include <vlc_block.h>
35 #include "vlc_block_helper.h"
37 /*****************************************************************************
39 *****************************************************************************/
40 static int Open ( vlc_object_t * );
41 static void Close( vlc_object_t * );
44 set_category( CAT_SOUT );
45 set_subcategory( SUBCAT_SOUT_PACKETIZER );
46 set_description( _("VC-1 packetizer") );
47 set_capability( "packetizer", 50 );
48 set_callbacks( Open, Close );
51 /*****************************************************************************
53 *****************************************************************************/
59 block_bytestream_t bytestream;
62 uint8_t p_startcode[3];
64 /* Current sequence header */
65 vlc_bool_t b_sequence_header;
69 vlc_bool_t b_advanced_profile;
70 vlc_bool_t b_interlaced;
71 vlc_bool_t b_frame_interpolation;
72 vlc_bool_t b_range_reduction;
73 vlc_bool_t b_has_bframe;
75 vlc_bool_t b_entry_point;
84 /* Current frame being built */
89 mtime_t i_interpolated_dts;
100 IDU_TYPE_SEQUENCE_HEADER = 0x0f,
101 IDU_TYPE_ENTRY_POINT = 0x0e,
102 IDU_TYPE_FRAME = 0x0D,
103 IDU_TYPE_FIELD = 0x0C,
104 IDU_TYPE_SLICE = 0x0B,
105 IDU_TYPE_END_OF_SEQUENCE = 0x0B,
107 IDU_TYPE_SEQUENCE_LEVEL_USER_DATA = 0x1F,
108 IDU_TYPE_ENTRY_POINT_USER_DATA = 0x1E,
109 IDU_TYPE_FRAME_USER_DATA = 0x1D,
110 IDU_TYPE_FIELD_USER_DATA = 0x1C,
111 IDU_TYPE_SLICE_USER_DATA = 0x1B,
114 static block_t *Packetize( decoder_t *p_dec, block_t **pp_block );
116 /*****************************************************************************
117 * Open: probe the packetizer and return score
118 *****************************************************************************
119 * Tries to launch a decoder and return score so that the interface is able
121 *****************************************************************************/
122 static int Open( vlc_object_t *p_this )
124 decoder_t *p_dec = (decoder_t*)p_this;
125 decoder_sys_t *p_sys;
127 if( p_dec->fmt_in.i_codec != VLC_FOURCC( 'W', 'V', 'C', '1' ) )
130 p_dec->pf_packetize = Packetize;
132 /* Create the output format */
133 es_format_Copy( &p_dec->fmt_out, &p_dec->fmt_in );
134 p_dec->p_sys = p_sys = malloc( sizeof( decoder_sys_t ) );
136 p_sys->i_state = STATE_NOSYNC;
137 p_sys->bytestream = block_BytestreamInit( p_dec );
138 p_sys->p_startcode[0] = 0x00;
139 p_sys->p_startcode[1] = 0x00;
140 p_sys->p_startcode[2] = 0x01;
143 p_sys->b_sequence_header = VLC_FALSE;
144 p_sys->sh.p_sh = NULL;
145 p_sys->b_entry_point = VLC_FALSE;
146 p_sys->ep.p_ep = NULL;
148 p_sys->b_frame = VLC_FALSE;
149 p_sys->p_frame = NULL;
150 p_sys->pp_last = &p_sys->p_frame;
152 p_sys->i_interpolated_dts = -1;
155 if( p_dec->fmt_in.i_extra > 0 )
157 block_t *p_init = block_New( p_dec, p_dec->fmt_in.i_extra );
160 memcpy( p_init->p_buffer, p_dec->fmt_in.p_extra,
161 p_dec->fmt_in.i_extra );
163 while( ( p_pic = Packetize( p_dec, &p_init ) ) )
164 block_Release( p_pic ); /* Should not happen (only sequence header) */
170 /*****************************************************************************
172 *****************************************************************************/
173 static void Close( vlc_object_t *p_this )
175 decoder_t *p_dec = (decoder_t*)p_this;
176 decoder_sys_t *p_sys = p_dec->p_sys;
178 block_BytestreamRelease( &p_sys->bytestream );
180 block_Release( p_sys->p_frame );
184 /*****************************************************************************
185 * Packetize: packetize an access unit
186 *****************************************************************************/
187 static block_t *ParseIDU( decoder_t *p_dec, block_t *p_frag );
189 static block_t *Packetize( decoder_t *p_dec, block_t **pp_block )
191 decoder_sys_t *p_sys = p_dec->p_sys;
194 if( pp_block == NULL || *pp_block == NULL )
197 if( (*pp_block)->i_flags & BLOCK_FLAG_DISCONTINUITY )
199 block_Release( *pp_block );
202 if( (*pp_block)->i_flags & BLOCK_FLAG_CORRUPTED )
204 p_sys->i_state = STATE_NOSYNC;
206 block_ChainRelease( p_sys->p_frame );
207 p_sys->p_frame = NULL;
208 p_sys->pp_last = &p_sys->p_frame;
209 block_Release( *pp_block );
213 block_BytestreamPush( &p_sys->bytestream, *pp_block );
217 switch( p_sys->i_state )
221 if( block_FindStartcodeFromOffset( &p_sys->bytestream, &p_sys->i_offset, p_sys->p_startcode, 3 ) == VLC_SUCCESS )
222 p_sys->i_state = STATE_NEXT_SYNC;
224 if( p_sys->i_offset )
226 block_SkipBytes( &p_sys->bytestream, p_sys->i_offset );
228 block_BytestreamFlush( &p_sys->bytestream );
231 if( p_sys->i_state != STATE_NEXT_SYNC )
232 return NULL; /* Need more data */
234 p_sys->i_offset = 4; /* To find next startcode */
236 case STATE_NEXT_SYNC:
237 /* TODO: If p_block == NULL, flush the buffer without checking the
240 /* Find the next startcode */
241 if( block_FindStartcodeFromOffset( &p_sys->bytestream, &p_sys->i_offset, p_sys->p_startcode, 3 ) != VLC_SUCCESS )
242 return NULL; /* Need more data */
244 /* Get the new fragment and set the pts/dts */
245 p_pic = block_New( p_dec, p_sys->i_offset );
246 block_BytestreamFlush( &p_sys->bytestream );
247 p_pic->i_pts = p_sys->bytestream.p_block->i_pts;
248 p_pic->i_dts = p_sys->bytestream.p_block->i_dts;
249 p_pic->i_rate = p_sys->bytestream.p_block->i_rate;
251 block_GetBytes( &p_sys->bytestream, p_pic->p_buffer, p_pic->i_buffer );
255 /* Parse and return complete frame */
256 p_pic = ParseIDU( p_dec, p_pic );
258 /* Don't reuse the same timestamps several times */
259 if( p_sys->p_frame &&
260 p_sys->p_frame->i_dts == p_sys->bytestream.p_block->i_dts &&
261 p_sys->p_frame->i_pts == p_sys->bytestream.p_block->i_pts )
263 p_sys->bytestream.p_block->i_pts = -1;
264 p_sys->bytestream.p_block->i_dts = -1;
270 p_sys->i_state = STATE_NOSYNC;
274 if( p_sys->i_interpolated_dts < 0 )
276 msg_Dbg( p_dec, "need a starting pts/dts" );
277 p_sys->i_state = STATE_NOSYNC;
278 block_Release( p_pic );
282 /* So p_block doesn't get re-added several times */
283 *pp_block = block_BytestreamPop( &p_sys->bytestream );
285 p_sys->i_state = STATE_NOSYNC;
292 /* DecodeRIDU: decode the startcode emulation prevention (same than h264) */
293 static void DecodeRIDU( uint8_t *p_ret, int *pi_ret, uint8_t *src, int i_src )
295 uint8_t *end = &src[i_src];
296 uint8_t *dst_end = &p_ret[*pi_ret];
297 uint8_t *dst = p_ret;
299 while( src < end && dst < dst_end )
301 if( src < end - 3 && src[0] == 0x00 && src[1] == 0x00 &&
313 *pi_ret = dst - p_ret;
315 /* BuildExtraData: gather sequence header and entry point */
316 static void BuildExtraData( decoder_t *p_dec )
318 decoder_sys_t *p_sys = p_dec->p_sys;
319 es_format_t *p_es = &p_dec->fmt_out;
321 if( !p_sys->b_sequence_header || !p_sys->b_entry_point )
324 i_extra = p_sys->sh.p_sh->i_buffer + p_sys->ep.p_ep->i_buffer;
325 if( p_es->i_extra != i_extra )
327 p_es->i_extra = i_extra;
328 p_es->p_extra = realloc( p_dec->fmt_out.p_extra, p_es->i_extra );
330 memcpy( p_es->p_extra,
331 p_sys->sh.p_sh->p_buffer, p_sys->sh.p_sh->i_buffer );
332 memcpy( (uint8_t*)p_es->p_extra + p_sys->sh.p_sh->i_buffer,
333 p_sys->ep.p_ep->p_buffer, p_sys->ep.p_ep->i_buffer );
335 /* ParseIDU: parse an Independant Decoding Unit */
336 static block_t *ParseIDU( decoder_t *p_dec, block_t *p_frag )
338 decoder_sys_t *p_sys = p_dec->p_sys;
340 const idu_type_t idu = p_frag->p_buffer[3];
342 if( !p_sys->b_sequence_header && idu != IDU_TYPE_SEQUENCE_HEADER )
344 msg_Warn( p_dec, "waiting for sequence header" );
345 block_Release( p_frag );
348 if( p_sys->b_sequence_header && !p_sys->b_entry_point && idu != IDU_TYPE_ENTRY_POINT )
350 msg_Warn( p_dec, "waiting for entry point" );
351 block_Release( p_frag );
354 /* TODO we do not gather ENTRY_POINT and SEQUENCE_DATA user data
355 * But It should not be a problem for decoder */
357 /* Do we have completed a frame */
359 if( p_sys->b_frame &&
360 idu != IDU_TYPE_FRAME_USER_DATA &&
361 idu != IDU_TYPE_FIELD && idu != IDU_TYPE_FIELD_USER_DATA &&
362 idu != IDU_TYPE_SLICE && idu != IDU_TYPE_SLICE_USER_DATA &&
363 idu != IDU_TYPE_END_OF_SEQUENCE )
366 p_pic = block_ChainGather( p_sys->p_frame );
368 /* We can interpolate dts/pts only if we have a frame rate */
369 if( p_dec->fmt_out.video.i_frame_rate != 0 && p_dec->fmt_out.video.i_frame_rate_base != 0 )
371 //msg_Dbg( p_dec, "-------------- XXX0 dts="I64Fd" pts="I64Fd" interpolated="I64Fd, p_pic->i_dts, p_pic->i_pts, p_sys->i_interpolated_dts );
372 if( p_pic->i_dts <= 0 )
373 p_pic->i_dts = p_sys->i_interpolated_dts;
375 p_sys->i_interpolated_dts += I64C(1000000) * p_dec->fmt_out.video.i_frame_rate_base / p_dec->fmt_out.video.i_frame_rate;
376 if( p_pic->i_pts <= 0 )
378 if( !p_sys->sh.b_has_bframe || (p_pic->i_flags & BLOCK_FLAG_TYPE_B ) )
379 p_pic->i_pts = p_pic->i_dts;
380 /* TODO compute pts for other case */
383 p_sys->i_interpolated_dts = p_pic->i_dts;
385 //msg_Dbg( p_dec, "-------------- dts="I64Fd" pts="I64Fd, p_pic->i_dts, p_pic->i_pts );
388 p_sys->p_frame = NULL;
389 p_sys->pp_last = &p_sys->p_frame;
395 block_t *p_frame = p_sys->p_frame;
396 if( p_frame->i_dts < 0 )
397 p_frame->i_dts = p_frag->i_dts;
398 if( p_frame->i_pts < 0 )
399 p_frame->i_pts = p_frag->i_pts;
401 block_ChainLastAppend( &p_sys->pp_last, p_frag );
404 if( idu == IDU_TYPE_SEQUENCE_HEADER )
406 es_format_t *p_es = &p_dec->fmt_out;
410 int i_ridu = sizeof(ridu);
414 block_Release( p_sys->sh.p_sh );
415 p_sys->sh.p_sh = block_Duplicate( p_frag );
417 /* Extract the raw IDU */
418 DecodeRIDU( ridu, &i_ridu, &p_frag->p_buffer[4], p_frag->i_buffer - 4 );
420 /* Auto detect VC-1_SPMP_PESpacket_PayloadFormatHeader (SMPTE RP 227) for simple/main profile
421 * TODO find a test case and valid it */
422 if( i_ridu > 4 && (ridu[0]&0x80) == 0 ) /* for advanced profile, the first bit is 1 */
424 video_format_t *p_v = &p_dec->fmt_in.video;
425 const int i_potential_width = GetWBE( &ridu[0] );
426 const int i_potential_height = GetWBE( &ridu[2] );
428 if( i_potential_width >= 2 && i_potential_width <= 8192 &&
429 i_potential_height >= 2 && i_potential_height <= 8192 )
431 if( ( p_v->i_width <= 0 && p_v->i_height <= 0 ) ||
432 ( p_v->i_width == i_potential_width && p_v->i_height == i_potential_height ) )
434 static const uint8_t startcode[4] = { 0x00, 0x00, 0x01, IDU_TYPE_SEQUENCE_HEADER };
435 p_es->video.i_width = i_potential_width;
436 p_es->video.i_height = i_potential_height;
439 p_frag->p_buffer += 4;
440 p_frag->i_buffer -= 4;
441 memcpy( p_frag->p_buffer, startcode, sizeof(startcode) );
447 bs_init( &s, ridu, i_ridu );
448 i_profile = bs_read( &s, 2 );
451 const int i_level = bs_read( &s, 3 );
453 /* Advanced profile */
454 p_sys->sh.b_advanced_profile = VLC_TRUE;
455 p_sys->sh.b_range_reduction = VLC_FALSE;
456 p_sys->sh.b_has_bframe = VLC_TRUE;
458 bs_skip( &s, 2+3+5+1 ); // chroma format + frame rate Q + bit rate Q + postprocflag
460 p_es->video.i_width = 2*bs_read( &s, 12 )+2;
461 p_es->video.i_height = 2*bs_read( &s, 12 )+2;
463 if( !p_sys->b_sequence_header )
464 msg_Dbg( p_dec, "found sequence header for advanced profile level L%d resolution %dx%d",
465 i_level, p_es->video.i_width, p_es->video.i_height);
467 bs_skip( &s, 1 );// pulldown
468 p_sys->sh.b_interlaced = bs_read( &s, 1 );
469 bs_skip( &s, 1 );// frame counter
470 p_sys->sh.b_frame_interpolation = bs_read( &s, 1 );
471 bs_skip( &s, 1 ); // Reserved
472 bs_skip( &s, 1 ); // Psf
474 if( bs_read( &s, 1 ) ) /* Display extension */
476 const int i_display_width = bs_read( &s, 14 )+1;
477 const int i_display_height = bs_read( &s, 14 )+1;
479 p_es->video.i_aspect = VOUT_ASPECT_FACTOR * i_display_width / i_display_height;
481 if( !p_sys->b_sequence_header )
482 msg_Dbg( p_dec, "display size %dx%d", i_display_width, i_display_height );
484 if( bs_read( &s, 1 ) ) /* Pixel aspect ratio (PAR/SAR) */
486 static const int p_ar[16][2] = {
487 { 0, 0}, { 1, 1}, {12,11}, {10,11}, {16,11}, {40,33},
488 {24,11}, {20,11}, {32,11}, {80,33}, {18,11}, {15,11},
489 {64,33}, {160,99},{ 0, 0}, { 0, 0}
491 int i_ar = bs_read( &s, 4 );
496 i_ar_w = bs_read( &s, 8 );
497 i_ar_h = bs_read( &s, 8 );
501 i_ar_w = p_ar[i_ar][0];
502 i_ar_h = p_ar[i_ar][1];
504 vlc_ureduce( &i_ar_w, &i_ar_h, i_ar_w, i_ar_h, 0 );
505 if( !p_sys->b_sequence_header )
506 msg_Dbg( p_dec, "aspect ratio %d:%d", i_ar_w, i_ar_h );
509 if( bs_read( &s, 1 ) ) /* Frame rate */
513 if( bs_read( &s, 1 ) )
515 i_fps_num = bs_read( &s, 16 )+1;
520 const int i_nr = bs_read( &s, 8 );
521 const int i_dn = bs_read( &s, 4 );
525 case 1: i_fps_num = 24000; break;
526 case 2: i_fps_num = 25000; break;
527 case 3: i_fps_num = 30000; break;
528 case 4: i_fps_num = 50000; break;
529 case 5: i_fps_num = 60000; break;
530 case 6: i_fps_num = 48000; break;
531 case 7: i_fps_num = 72000; break;
535 case 1: i_fps_den = 1000; break;
536 case 2: i_fps_den = 1001; break;
539 if( i_fps_num != 0 && i_fps_den != 0 )
540 vlc_ureduce( &p_es->video.i_frame_rate, &p_es->video.i_frame_rate_base, i_fps_num, i_fps_den, 0 );
542 if( !p_sys->b_sequence_header )
543 msg_Dbg( p_dec, "frame rate %d/%d", p_es->video.i_frame_rate, p_es->video.i_frame_rate_base );
548 /* Simple and main profile */
549 p_sys->sh.b_advanced_profile = VLC_FALSE;
550 p_sys->sh.b_interlaced = VLC_FALSE;
552 if( !p_sys->b_sequence_header )
553 msg_Dbg( p_dec, "found sequence header for %s profile", i_profile == 0 ? "simple" : "main" );
555 bs_skip( &s, 2+3+5+1+1+ // reserved + frame rate Q + bit rate Q + loop filter + reserved
556 1+1+1+1+2+ // multiresolution + reserved + fast uv mc + extended mv + dquant
557 1+1+1+1 ); // variable size transform + reserved + overlap + sync marker
558 p_sys->sh.b_range_reduction = bs_read( &s, 1 );
559 if( bs_read( &s, 3 ) > 0 )
560 p_sys->sh.b_has_bframe = VLC_TRUE;
562 p_sys->sh.b_has_bframe = VLC_FALSE;
563 bs_skip( &s, 2 ); // quantizer
565 p_sys->sh.b_frame_interpolation = bs_read( &s, 1 );
567 p_sys->b_sequence_header = VLC_TRUE;
568 BuildExtraData( p_dec );
570 else if( idu == IDU_TYPE_ENTRY_POINT )
573 block_Release( p_sys->ep.p_ep );
574 p_sys->ep.p_ep = block_Duplicate( p_frag );
576 p_sys->b_entry_point = VLC_TRUE;
577 BuildExtraData( p_dec );
579 else if( idu == IDU_TYPE_FRAME )
583 int i_ridu = sizeof(ridu);
585 /* Extract the raw IDU */
586 DecodeRIDU( ridu, &i_ridu, &p_frag->p_buffer[4], p_frag->i_buffer - 4 );
588 /* Parse it + interpolate pts/dts if possible */
589 bs_init( &s, ridu, i_ridu );
591 if( p_sys->sh.b_advanced_profile )
595 if( p_sys->sh.b_interlaced )
597 if( bs_read( &s, 1 ) )
599 if( bs_read( &s, 1 ) )
600 i_fcm = 1; /* interlaced field */
602 i_fcm = 2; /* interlaced frame */
606 if( i_fcm == 1 ) /*interlaced field */
608 /* XXX for mixed I/P we should check reference usage before marking them I (too much work) */
609 switch( bs_read( &s, 3 ) )
614 p_sys->p_frame->i_flags |= BLOCK_FLAG_TYPE_I;
615 p_sys->p_frame->i_flags |= BLOCK_FLAG_TYPE_I;
616 p_sys->p_frame->i_flags |= BLOCK_FLAG_TYPE_I;
619 p_sys->p_frame->i_flags |= BLOCK_FLAG_TYPE_P;
625 p_sys->p_frame->i_flags |= BLOCK_FLAG_TYPE_B;
631 if( !bs_read( &s, 1 ) )
632 p_sys->p_frame->i_flags |= BLOCK_FLAG_TYPE_P;
633 else if( !bs_read( &s, 1 ) )
634 p_sys->p_frame->i_flags |= BLOCK_FLAG_TYPE_B;
635 else if( !bs_read( &s, 1 ) )
636 p_sys->p_frame->i_flags |= BLOCK_FLAG_TYPE_I;
637 else if( !bs_read( &s, 1 ) )
638 p_sys->p_frame->i_flags |= BLOCK_FLAG_TYPE_B; /* Bi */
640 p_sys->p_frame->i_flags |= BLOCK_FLAG_TYPE_P; /* P Skip */
645 if( p_sys->sh.b_frame_interpolation )
646 bs_skip( &s, 1 ); // interpolate
647 bs_skip( &s, 2 ); // frame count
648 if( p_sys->sh.b_range_reduction )
649 bs_skip( &s, 1 ); // range reduction
651 if( bs_read( &s, 1 ) )
652 p_sys->p_frame->i_flags |= BLOCK_FLAG_TYPE_P;
653 else if( !p_sys->sh.b_has_bframe || bs_read( &s, 1 ) )
654 p_sys->p_frame->i_flags |= BLOCK_FLAG_TYPE_I;
656 p_sys->p_frame->i_flags |= BLOCK_FLAG_TYPE_B;
658 p_sys->b_frame = VLC_TRUE;