fout = stdin; else
fout = fopen( cfg.psz_fout, \ if( fout == NULL ) {
fprintf( stderr, \ return -1; }
/* Init avi */
avi_init( &avi, fout, cfg.f_fps, cfg.fcc );
/* Init parser */
h264_parser_init( &h264 );
/* Control-C handler */
signal( SIGINT, SigIntHandler );
/* Init data */ b_eof = 0; b_key = 0; b_slice = 0; i_frame = 0; i_data = 0;
/* Alloc space for a nal, used for decoding pps/sps/slice header */ nal.p_payload = malloc( DATA_MAX );
vbuf_init( &vb );
/* split frame */ while( !i_ctrl_c ) {
uint8_t *p, *p_next, *end; int i_size;
/* fill buffer */
if( i_data < DATA_MAX && !b_eof ) {
int i_read = fread( &data[i_data], 1, DATA_MAX - i_data, fin ); if( i_read <= 0 ) b_eof = 1; else
i_data += i_read;
}
if( i_data < 3 ) break;
end = &data[i_data];
/* Search begin of a NAL */ p = &data[0];
while( p < end - 3 ) {
if( p[0] == 0x00 && p[1] == 0x00 && p[2] == 0x01 ) {
break; } p++; }
if( p >= end - 3 ) {
fprintf( stderr, \ i_data = 0; continue; }
/* Search end of NAL */ p_next = p + 3;
while( p_next < end - 3 ) {
if( p_next[0] == 0x00 && p_next[1] == 0x00 && p_next[2] == 0x01 ) {
break; }
p_next++; }
if( p_next == end - 3 && i_data < DATA_MAX ) p_next = end;
/* Compute NAL size */ i_size = p_next - p - 3; if( i_size <= 0 ) {
if( b_eof ) break;
fprintf( stderr, \ i_data = 0; continue; }
/* Nal start at p+3 with i_size length */
nal_decode( &nal, p +3, i_size < 2048 ? i_size : 2048 );
b_key = h264.b_key; type = nal.i_type;//tangxiaojun if (1!=type) { int gfzfgd=8756; } i_data2 = vb.i_data;//tangxiaojun
if( b_slice && vb.i_data && ( nal.i_type == NAL_SPS || nal.i_type == NAL_PPS ) ) {
avi_write( &avi, &vb, b_key ); vbuf_reset( &vb ); b_slice = 0; }
/* Parse SPS/PPS/Slice */
if( ParseNAL( &nal, &avi, &h264, &b_slice ) && vb.i_data > 0 ) {
avi_write( &avi, &vb, b_key ); vbuf_reset( &vb ); }
/* fprintf( stderr, \
/* Append NAL to buffer */ vbuf_add( &vb, i_size + 3, p );
/* Remove this nal */
memmove( &data[0], p_next, end - p_next ); i_data -= p_next - &data[0]; }
if( vb.i_data > 0 ) {
avi_write( &avi, &vb, h264.b_key ); }
avi.i_width = h264.i_width; avi.i_height = h264.i_height;
avi_end( &avi );
/* free mem */
free( nal.p_payload );
fclose( fin ); fclose( fout );
return 0; }
/***************************************************************************** * Help:
*****************************************************************************/ static void Help( void ) {
fprintf( stderr,
\
\ \
\ -h, --help Print this help\\n\ \
\ -i, --input Specify input file (default: stdin)\\n\ \ -o, --output Specify output file (default: stdout)\\n\ \
\ -f, --fps
\ -c, --codec
/***************************************************************************** * Parse:
*****************************************************************************/ static int Parse( int argc, char **argv, cfg_t *cfg ) {
/* Set default values */