00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029 #include <stdlib.h>
00030
00031 #include <vlc/vlc.h>
00032 #include <vlc/decoder.h>
00033 #include <vlc/sout.h>
00034
00035 #include "vlc_block_helper.h"
00036 #include "vlc_bits.h"
00037
00038
00039
00040
00041 static int Open ( vlc_object_t * );
00042 static void Close( vlc_object_t * );
00043
00044 vlc_module_begin();
00045 set_category( CAT_SOUT );
00046 set_subcategory( SUBCAT_SOUT_PACKETIZER );
00047 set_description( _("H264 video packetizer") );
00048 set_capability( "packetizer", 50 );
00049 set_callbacks( Open, Close );
00050 vlc_module_end();
00051
00052
00053
00054
00055
00056 static block_t *Packetize( decoder_t *, block_t ** );
00057 static block_t *PacketizeAVC1( decoder_t *, block_t ** );
00058
00059 struct decoder_sys_t
00060 {
00061 block_bytestream_t bytestream;
00062
00063 int i_state;
00064 int i_offset;
00065 uint8_t startcode[4];
00066
00067 vlc_bool_t b_slice;
00068 block_t *p_frame;
00069
00070 vlc_bool_t b_sps;
00071 vlc_bool_t b_pps;
00072
00073
00074 int i_avcC_length_size;
00075 block_t *p_sps;
00076 block_t *p_pps;
00077
00078
00079 int i_log2_max_frame_num;
00080 int b_frame_mbs_only;
00081
00082
00083 int i_nal_type;
00084 int i_nal_ref_idc;
00085 int i_idr_pic_id;
00086 int i_frame_num;
00087 };
00088
00089 enum
00090 {
00091 STATE_NOSYNC,
00092 STATE_NEXT_SYNC,
00093 };
00094
00095 enum nal_unit_type_e
00096 {
00097 NAL_UNKNOWN = 0,
00098 NAL_SLICE = 1,
00099 NAL_SLICE_DPA = 2,
00100 NAL_SLICE_DPB = 3,
00101 NAL_SLICE_DPC = 4,
00102 NAL_SLICE_IDR = 5,
00103 NAL_SEI = 6,
00104 NAL_SPS = 7,
00105 NAL_PPS = 8,
00106 NAL_AU_DELIMITER= 9
00107
00108 };
00109
00110 enum nal_priority_e
00111 {
00112 NAL_PRIORITY_DISPOSABLE = 0,
00113 NAL_PRIORITY_LOW = 1,
00114 NAL_PRIORITY_HIGH = 2,
00115 NAL_PRIORITY_HIGHEST = 3,
00116 };
00117
00118 static block_t *ParseNALBlock( decoder_t *, block_t * );
00119
00120 static block_t *nal_get_annexeb( decoder_t *, uint8_t *p, int );
00121
00122
00123
00124
00125 static int Open( vlc_object_t *p_this )
00126 {
00127 decoder_t *p_dec = (decoder_t*)p_this;
00128 decoder_sys_t *p_sys;
00129
00130 if( p_dec->fmt_in.i_codec != VLC_FOURCC( 'h', '2', '6', '4') &&
00131 p_dec->fmt_in.i_codec != VLC_FOURCC( 'H', '2', '6', '4') &&
00132 p_dec->fmt_in.i_codec != VLC_FOURCC( 'V', 'S', 'S', 'H') &&
00133 p_dec->fmt_in.i_codec != VLC_FOURCC( 'v', 's', 's', 'h') &&
00134 ( p_dec->fmt_in.i_codec != VLC_FOURCC( 'a', 'v', 'c', '1') ||
00135 p_dec->fmt_in.i_extra < 7 ) )
00136 {
00137 return VLC_EGENERIC;
00138 }
00139
00140
00141 if( ( p_dec->p_sys = p_sys = malloc( sizeof(decoder_sys_t) ) ) == NULL )
00142 {
00143 msg_Err( p_dec, "out of memory" );
00144 return VLC_EGENERIC;
00145 }
00146 p_sys->i_state = STATE_NOSYNC;
00147 p_sys->i_offset = 0;
00148 p_sys->startcode[0] = 0;
00149 p_sys->startcode[1] = 0;
00150 p_sys->startcode[2] = 0;
00151 p_sys->startcode[3] = 1;
00152 p_sys->bytestream = block_BytestreamInit( p_dec );
00153 p_sys->b_slice = VLC_FALSE;
00154 p_sys->p_frame = NULL;
00155 p_sys->b_sps = VLC_FALSE;
00156 p_sys->b_pps = VLC_FALSE;
00157 p_sys->p_sps = 0;
00158 p_sys->p_pps = 0;
00159
00160 p_sys->i_nal_type = -1;
00161 p_sys->i_nal_ref_idc = -1;
00162 p_sys->i_idr_pic_id = -1;
00163 p_sys->i_frame_num = -1;
00164
00165
00166 es_format_Copy( &p_dec->fmt_out, &p_dec->fmt_in );
00167 p_dec->fmt_out.i_codec = VLC_FOURCC( 'h', '2', '6', '4' );
00168
00169 if( p_dec->fmt_out.i_extra ) free( p_dec->fmt_out.p_extra );
00170 p_dec->fmt_out.i_extra = 0; p_dec->fmt_out.p_extra = 0;
00171
00172 if( p_dec->fmt_in.i_codec == VLC_FOURCC( 'a', 'v', 'c', '1' ) )
00173 {
00174 uint8_t *p = &((uint8_t*)p_dec->fmt_in.p_extra)[4];
00175 int i_sps, i_pps;
00176 int i;
00177
00178
00179 p_sys->i_avcC_length_size = 1 + ((*p++)&0x03);
00180
00181
00182 i_sps = (*p++)&0x1f;
00183
00184 for( i = 0; i < i_sps; i++ )
00185 {
00186 int i_length = GetWBE( p );
00187 block_t *p_sps = nal_get_annexeb( p_dec, p + 2, i_length );
00188
00189 p_sys->p_sps = block_Duplicate( p_sps );
00190 p_sps->i_pts = p_sps->i_dts = mdate();
00191 ParseNALBlock( p_dec, p_sps );
00192 p += 2 + i_length;
00193 }
00194
00195 i_pps = *p++;
00196 for( i = 0; i < i_pps; i++ )
00197 {
00198 int i_length = GetWBE( p );
00199 block_t *p_pps = nal_get_annexeb( p_dec, p + 2, i_length );
00200
00201 p_sys->p_pps = block_Duplicate( p_pps );
00202 p_pps->i_pts = p_pps->i_dts = mdate();
00203 ParseNALBlock( p_dec, p_pps );
00204 p += 2 + i_length;
00205 }
00206 msg_Dbg( p_dec, "avcC length size=%d sps=%d pps=%d",
00207 p_sys->i_avcC_length_size, i_sps, i_pps );
00208
00209
00210 p_dec->pf_packetize = PacketizeAVC1;
00211 }
00212 else
00213 {
00214
00215 p_dec->pf_packetize = Packetize;
00216
00217
00218 if( p_dec->fmt_in.i_extra > 0 )
00219 {
00220 block_t *p_init = block_New( p_dec, p_dec->fmt_in.i_extra );
00221 block_t *p_pic;
00222
00223 memcpy( p_init->p_buffer, p_dec->fmt_in.p_extra,
00224 p_dec->fmt_in.i_extra );
00225
00226 while( ( p_pic = Packetize( p_dec, &p_init ) ) )
00227 {
00228
00229 block_Release( p_pic );
00230 }
00231 }
00232 }
00233
00234 return VLC_SUCCESS;
00235 }
00236
00237
00238
00239
00240 static void Close( vlc_object_t *p_this )
00241 {
00242 decoder_t *p_dec = (decoder_t*)p_this;
00243 decoder_sys_t *p_sys = p_dec->p_sys;
00244
00245 if( p_sys->p_frame ) block_ChainRelease( p_sys->p_frame );
00246 if( p_sys->p_sps ) block_Release( p_sys->p_sps );
00247 if( p_sys->p_pps ) block_Release( p_sys->p_pps );
00248 block_BytestreamRelease( &p_sys->bytestream );
00249 free( p_sys );
00250 }
00251
00252
00253
00254
00255 static block_t *Packetize( decoder_t *p_dec, block_t **pp_block )
00256 {
00257 decoder_sys_t *p_sys = p_dec->p_sys;
00258 block_t *p_pic;
00259
00260 if( !pp_block || !*pp_block ) return NULL;
00261
00262 block_BytestreamPush( &p_sys->bytestream, *pp_block );
00263
00264 for( ;; )
00265 {
00266 switch( p_sys->i_state )
00267 {
00268 case STATE_NOSYNC:
00269 if( block_FindStartcodeFromOffset( &p_sys->bytestream,
00270 &p_sys->i_offset, p_sys->startcode+1, 3 ) == VLC_SUCCESS)
00271 {
00272 p_sys->i_state = STATE_NEXT_SYNC;
00273 }
00274
00275 if( p_sys->i_offset )
00276 {
00277 block_SkipBytes( &p_sys->bytestream, p_sys->i_offset );
00278 p_sys->i_offset = 0;
00279 block_BytestreamFlush( &p_sys->bytestream );
00280 }
00281
00282 if( p_sys->i_state != STATE_NEXT_SYNC )
00283 {
00284
00285 return NULL;
00286 }
00287
00288 p_sys->i_offset = 1;
00289
00290 case STATE_NEXT_SYNC:
00291
00292 if( block_FindStartcodeFromOffset( &p_sys->bytestream,
00293 &p_sys->i_offset, p_sys->startcode+1, 3 ) != VLC_SUCCESS)
00294 {
00295
00296 return NULL;
00297 }
00298
00299
00300 p_pic = block_New( p_dec, p_sys->i_offset );
00301 p_pic->i_pts = p_sys->bytestream.p_block->i_pts;
00302 p_pic->i_dts = p_sys->bytestream.p_block->i_dts;
00303
00304 block_GetBytes( &p_sys->bytestream, p_pic->p_buffer,
00305 p_pic->i_buffer );
00306
00307 if( !p_pic->p_buffer[p_pic->i_buffer-1] ) p_pic->i_buffer--;
00308 p_sys->i_offset = 0;
00309
00310
00311 if( !( p_pic = ParseNALBlock( p_dec, p_pic ) ) )
00312 {
00313 p_sys->i_state = STATE_NOSYNC;
00314 break;
00315 }
00316 #if 0
00317 msg_Dbg( p_dec, "pts="I64Fd" dts="I64Fd,
00318 p_pic->i_pts, p_pic->i_dts );
00319 #endif
00320
00321
00322 *pp_block = block_BytestreamPop( &p_sys->bytestream );
00323
00324 p_sys->i_state = STATE_NOSYNC;
00325
00326 return p_pic;
00327 }
00328 }
00329 }
00330
00331
00332
00333
00334 static block_t *PacketizeAVC1( decoder_t *p_dec, block_t **pp_block )
00335 {
00336 decoder_sys_t *p_sys = p_dec->p_sys;
00337 block_t *p_block;
00338 block_t *p_ret = NULL;
00339 uint8_t *p;
00340
00341 if( !pp_block || !*pp_block ) return NULL;
00342
00343 p_block = *pp_block;
00344 *pp_block = NULL;
00345
00346 #if 0
00347 if(
00348 p_sys->p_sps && p_sys->p_pps )
00349 {
00350 block_t *p_pic;
00351 block_t *p_sps = block_Duplicate( p_sys->p_sps );
00352 block_t *p_pps = block_Duplicate( p_sys->p_pps );
00353 p_sps->i_dts = p_pps->i_dts = p_block->i_dts;
00354 p_sps->i_pts = p_pps->i_pts = p_block->i_pts;
00355 p_pic = ParseNALBlock( p_dec, p_sps );
00356 if( p_pic ) block_ChainAppend( &p_ret, p_pic );
00357 p_pic = ParseNALBlock( p_dec, p_pps );
00358 if( p_pic ) block_ChainAppend( &p_ret, p_pic );
00359 }
00360 #endif
00361
00362 for( p = p_block->p_buffer; p < &p_block->p_buffer[p_block->i_buffer]; )
00363 {
00364 block_t *p_pic;
00365 int i_size = 0;
00366 int i;
00367
00368 for( i = 0; i < p_sys->i_avcC_length_size; i++ )
00369 {
00370 i_size = (i_size << 8) | (*p++);
00371 }
00372
00373 if( i_size > 0 )
00374 {
00375 block_t *p_part = nal_get_annexeb( p_dec, p, i_size );
00376
00377 p_part->i_dts = p_block->i_dts;
00378 p_part->i_pts = p_block->i_pts;
00379
00380
00381 if( ( p_pic = ParseNALBlock( p_dec, p_part ) ) )
00382 {
00383 block_ChainAppend( &p_ret, p_pic );
00384 }
00385 }
00386 p += i_size;
00387 }
00388 block_Release( p_block );
00389
00390 return p_ret;
00391 }
00392
00393 static block_t *nal_get_annexeb( decoder_t *p_dec, uint8_t *p, int i_size )
00394 {
00395 block_t *p_nal;
00396
00397 p_nal = block_New( p_dec, 3 + i_size );
00398
00399
00400 p_nal->p_buffer[0] = 0x00;
00401 p_nal->p_buffer[1] = 0x00;
00402 p_nal->p_buffer[2] = 0x01;
00403
00404
00405 memcpy( &p_nal->p_buffer[3], p, i_size );
00406
00407 return p_nal;
00408 }
00409
00410 static void nal_get_decoded( uint8_t **pp_ret, int *pi_ret,
00411 uint8_t *src, int i_src )
00412 {
00413 uint8_t *end = &src[i_src];
00414 uint8_t *dst = malloc( i_src );
00415
00416 *pp_ret = dst;
00417
00418 while( src < end )
00419 {
00420 if( src < end - 3 && src[0] == 0x00 && src[1] == 0x00 &&
00421 src[2] == 0x03 )
00422 {
00423 *dst++ = 0x00;
00424 *dst++ = 0x00;
00425
00426 src += 3;
00427 continue;
00428 }
00429 *dst++ = *src++;
00430 }
00431
00432 *pi_ret = dst - *pp_ret;
00433 }
00434
00435 static inline int bs_read_ue( bs_t *s )
00436 {
00437 int i = 0;
00438
00439 while( bs_read1( s ) == 0 && s->p < s->p_end && i < 32 )
00440 {
00441 i++;
00442 }
00443 return( ( 1 << i) - 1 + bs_read( s, i ) );
00444 }
00445
00446 static inline int bs_read_se( bs_t *s )
00447 {
00448 int val = bs_read_ue( s );
00449
00450 return val&0x01 ? (val+1)/2 : -(val/2);
00451 }
00452
00453
00454 static block_t *ParseNALBlock( decoder_t *p_dec, block_t *p_frag )
00455 {
00456 decoder_sys_t *p_sys = p_dec->p_sys;
00457 block_t *p_pic = NULL;
00458
00459 const int i_nal_ref_idc = (p_frag->p_buffer[3] >> 5)&0x03;
00460 const int i_nal_type = p_frag->p_buffer[3]&0x1f;
00461
00462 #define OUTPUT \
00463 do { \
00464 p_pic = block_ChainGather( p_sys->p_frame ); \
00465 p_pic->i_length = 0; \
00466 \
00467 p_sys->p_frame = NULL; \
00468 p_sys->b_slice = VLC_FALSE; \
00469 } while(0)
00470
00471
00472 if( p_sys->b_slice && !p_sys->b_sps )
00473 {
00474 block_ChainRelease( p_sys->p_frame );
00475 msg_Warn( p_dec, "waiting for SPS" );
00476
00477
00478 p_sys->p_frame = NULL;
00479 p_sys->b_slice = VLC_FALSE;
00480 }
00481
00482 if( !p_sys->b_sps &&
00483 i_nal_type >= NAL_SLICE && i_nal_type <= NAL_SLICE_IDR )
00484 {
00485 p_sys->b_slice = VLC_TRUE;
00486
00487 }
00488 else if( i_nal_type >= NAL_SLICE && i_nal_type <= NAL_SLICE_IDR )
00489 {
00490 uint8_t *dec;
00491 int i_dec, i_first_mb, i_slice_type, i_frame_num, i_pic_flags = 0;
00492 vlc_bool_t b_pic = VLC_FALSE;
00493 bs_t s;
00494
00495
00496 nal_get_decoded( &dec, &i_dec, &p_frag->p_buffer[4],
00497 __MIN( p_frag->i_buffer - 4, 60 ) );
00498 bs_init( &s, dec, i_dec );
00499
00500
00501 i_first_mb = bs_read_ue( &s );
00502
00503
00504 switch( (i_slice_type = bs_read_ue( &s )) )
00505 {
00506 case 0: case 5:
00507 i_pic_flags = BLOCK_FLAG_TYPE_P;
00508 break;
00509 case 1: case 6:
00510 i_pic_flags = BLOCK_FLAG_TYPE_B;
00511 break;
00512 case 2: case 7:
00513 i_pic_flags = BLOCK_FLAG_TYPE_I;
00514 break;
00515 case 3: case 8:
00516 i_pic_flags = BLOCK_FLAG_TYPE_P;
00517 break;
00518 case 4: case 9:
00519 i_pic_flags = BLOCK_FLAG_TYPE_I;
00520 break;
00521 }
00522
00523
00524 bs_read_ue( &s );
00525
00526 i_frame_num = bs_read( &s, p_sys->i_log2_max_frame_num + 4 );
00527
00528
00529
00530 if( i_frame_num != p_sys->i_frame_num ||
00531 ( (i_nal_ref_idc != p_sys->i_nal_ref_idc) &&
00532 (!i_nal_ref_idc || !p_sys->i_nal_ref_idc) ) )
00533 {
00534 b_pic = VLC_TRUE;
00535 }
00536 p_sys->i_frame_num = i_frame_num;
00537 p_sys->i_nal_ref_idc = i_nal_ref_idc;
00538
00539 if( !p_sys->b_frame_mbs_only )
00540 {
00541
00542 if( bs_read( &s, 1 ) )
00543 {
00544
00545 bs_read( &s, 1 );
00546 }
00547 }
00548
00549 if( i_nal_type == NAL_SLICE_IDR )
00550 {
00551
00552 int i_idr_pic_id = bs_read_ue( &s );
00553 if( p_sys->i_nal_type != i_nal_type ) b_pic = VLC_TRUE;
00554 if( p_sys->i_idr_pic_id != i_idr_pic_id ) b_pic = VLC_TRUE;
00555 p_sys->i_idr_pic_id = i_idr_pic_id;
00556 }
00557 p_sys->i_nal_type = i_nal_type;
00558
00559 if( b_pic && p_sys->b_slice ) OUTPUT;
00560
00561 p_sys->b_slice = VLC_TRUE;
00562
00563 free( dec );
00564 }
00565 else if( i_nal_type == NAL_SPS )
00566 {
00567 uint8_t *dec;
00568 int i_dec;
00569 bs_t s;
00570 int i_tmp;
00571
00572 if( !p_sys->b_sps ) msg_Dbg( p_dec, "found NAL_SPS" );
00573
00574 p_sys->b_sps = VLC_TRUE;
00575
00576 nal_get_decoded( &dec, &i_dec, &p_frag->p_buffer[4],
00577 p_frag->i_buffer - 4 );
00578
00579 bs_init( &s, dec, i_dec );
00580
00581 bs_skip( &s, 8 + 1+1+1 + 5 + 8 );
00582
00583 bs_read_ue( &s );
00584
00585 p_sys->i_log2_max_frame_num = bs_read_ue( &s );
00586
00587 i_tmp = bs_read_ue( &s );
00588 if( i_tmp == 0 )
00589 {
00590
00591 bs_read_ue( &s );
00592 }
00593 else if( i_tmp == 1 )
00594 {
00595 int i_cycle;
00596
00597 bs_skip( &s, 1 );
00598
00599 bs_read_se( &s );
00600
00601 bs_read_se( &s );
00602
00603 i_cycle = bs_read_ue( &s );
00604 if( i_cycle > 256 ) i_cycle = 256;
00605 while( i_cycle > 0 )
00606 {
00607
00608 bs_read_se(&s );
00609 }
00610 }
00611
00612 bs_read_ue( &s );
00613
00614 bs_skip( &s, 1 );
00615
00616
00617 p_dec->fmt_out.video.i_width = 16 * ( bs_read_ue( &s ) + 1 );
00618 p_dec->fmt_out.video.i_height = 16 * ( bs_read_ue( &s ) + 1 );
00619
00620
00621 p_sys->b_frame_mbs_only = bs_read( &s, 1 );
00622 if( p_sys->b_frame_mbs_only == 0 )
00623 {
00624 bs_skip( &s, 1 );
00625 }
00626
00627 bs_skip( &s, 1 );
00628
00629
00630 i_tmp = bs_read( &s, 1 );
00631 if( i_tmp )
00632 {
00633
00634 bs_read_ue( &s );
00635
00636 bs_read_ue( &s );
00637
00638 bs_read_ue( &s );
00639
00640 bs_read_ue( &s );
00641 }
00642
00643
00644 i_tmp = bs_read( &s, 1 );
00645 if( i_tmp )
00646 {
00647
00648 i_tmp = bs_read( &s, 1 );
00649 if( i_tmp )
00650 {
00651 static const struct { int w, h; } sar[14] =
00652 {
00653 { 0, 0 }, { 1, 1 }, { 12, 11 }, { 10, 11 },
00654 { 16, 11 }, { 40, 33 }, { 24, 11 }, { 20, 11 },
00655 { 32, 11 }, { 80, 33 }, { 18, 11 }, { 15, 11 },
00656 { 64, 33 }, { 160,99 },
00657 };
00658 int i_sar = bs_read( &s, 8 );
00659 int w, h;
00660
00661 if( i_sar < 14 )
00662 {
00663 w = sar[i_sar].w;
00664 h = sar[i_sar].h;
00665 }
00666 else
00667 {
00668 w = bs_read( &s, 16 );
00669 h = bs_read( &s, 16 );
00670 }
00671 if( h != 0 )
00672 p_dec->fmt_out.video.i_aspect = VOUT_ASPECT_FACTOR * w /
00673 h * p_dec->fmt_out.video.i_width /
00674 p_dec->fmt_out.video.i_height;
00675 else
00676 p_dec->fmt_out.video.i_aspect = VOUT_ASPECT_FACTOR;
00677 }
00678 }
00679
00680 free( dec );
00681
00682
00683 if( p_sys->b_slice ) OUTPUT;
00684 }
00685 else if( i_nal_type == NAL_PPS )
00686 {
00687 bs_t s;
00688 bs_init( &s, &p_frag->p_buffer[4], p_frag->i_buffer - 4 );
00689
00690 if( !p_sys->b_pps ) msg_Dbg( p_dec, "found NAL_PPS" );
00691 p_sys->b_pps = VLC_TRUE;
00692
00693
00694
00695 if( p_sys->b_slice ) OUTPUT;
00696 }
00697 else if( i_nal_type == NAL_AU_DELIMITER ||
00698 i_nal_type == NAL_SEI ||
00699 ( i_nal_type >= 13 && i_nal_type <= 18 ) )
00700 {
00701 if( p_sys->b_slice ) OUTPUT;
00702 }
00703
00704 #undef OUTPUT
00705
00706
00707 block_ChainAppend( &p_sys->p_frame, p_frag );
00708
00709 return p_pic;
00710 }