1 /*****************************************************************************
2 * avc2avi.c: raw h264 -> AVI
3 *****************************************************************************
4 * Copyright (C) 2004-2006 x264 project
6 * Authors: Laurent Aimar <fenrir@via.ecp.fr>
7 * Loren Merritt <lorenm@u.washington.edu>
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02111, USA.
22 *****************************************************************************/
34 #include <io.h> /* _setmode() */
35 #include <fcntl.h> /* _O_BINARY */
38 #include "../common/bs.h"
40 #define DATA_MAX 3000000
41 uint8_t data[DATA_MAX];
44 static int i_ctrl_c = 0;
45 static void SigIntHandler( int a )
66 void vbuf_init( vbuf_t * );
67 void vbuf_add( vbuf_t *, int i_data, void *p_data );
68 void vbuf_reset( vbuf_t * );
89 void avi_init( avi_t *, FILE *, float, char fcc[4] );
90 void avi_write( avi_t *, vbuf_t *, int );
91 void avi_end( avi_t * );
100 NAL_SLICE_IDR = 5, /* ref_idc != 0 */
101 NAL_SEI = 6, /* ref_idc == 0 */
104 /* ref_idc == 0 for 6,9,10,11,12 */
108 NAL_PRIORITY_DISPOSABLE = 0,
109 NAL_PRIORITY_LOW = 1,
110 NAL_PRIORITY_HIGH = 2,
111 NAL_PRIORITY_HIGHEST = 3,
116 int i_ref_idc; /* nal_priority_e */
117 int i_type; /* nal_unit_type_e */
119 /* This data are raw payload */
136 int i_log2_max_frame_num;
138 int i_log2_max_poc_lsb;
141 void h264_parser_init( h264_t * );
142 void h264_parser_parse( h264_t *h, nal_t *n, int *pb_nal_start );
145 static int nal_decode( nal_t *nal, void *p_data, int i_data );
147 static void Help( void );
148 static int Parse( int argc, char **argv, cfg_t * );
149 static int ParseNAL( nal_t *nal, avi_t *a, h264_t *h, int *pb_slice );
151 /****************************************************************************
153 ****************************************************************************/
154 int main( int argc, char **argv )
173 _setmode(_fileno(stdin), _O_BINARY); /* thanks to Marcos Morais <morais at dee.ufcg.edu.br> */
174 _setmode(_fileno(stdout), _O_BINARY);
177 /* Parse command line */
178 if( Parse( argc, argv, &cfg ) < 0 )
184 if( cfg.psz_fin == NULL || *cfg.psz_fin == '\0' || !strcmp( cfg.psz_fin, "-" ) )
187 fin = fopen( cfg.psz_fin, "rb" );
190 fprintf( stderr, "cannot open input file\n" );
195 if( cfg.psz_fout == NULL || *cfg.psz_fout == '\0' || !strcmp( cfg.psz_fout, "-" ) )
198 fout = fopen( cfg.psz_fout, "wb" );
201 fprintf( stderr, "cannot open output file\n" );
206 avi_init( &avi, fout, cfg.f_fps, cfg.fcc );
209 h264_parser_init( &h264 );
211 /* Control-C handler */
212 signal( SIGINT, SigIntHandler );
221 /* Alloc space for a nal, used for decoding pps/sps/slice header */
222 nal.p_payload = malloc( DATA_MAX );
229 uint8_t *p, *p_next, *end;
233 if( i_data < DATA_MAX && !b_eof )
235 int i_read = fread( &data[i_data], 1, DATA_MAX - i_data, fin );
246 /* Search begin of a NAL */
250 if( p[0] == 0x00 && p[1] == 0x00 && p[2] == 0x01 )
259 fprintf( stderr, "garbage (i_data = %d)\n", i_data );
264 /* Search end of NAL */
266 while( p_next < end - 3 )
268 if( p_next[0] == 0x00 && p_next[1] == 0x00 && p_next[2] == 0x01 )
275 if( p_next == end - 3 && i_data < DATA_MAX )
278 /* Compute NAL size */
279 i_size = p_next - p - 3;
285 fprintf( stderr, "nal too large (FIXME) ?\n" );
290 /* Nal start at p+3 with i_size length */
291 nal_decode( &nal, p +3, i_size < 2048 ? i_size : 2048 );
295 if( b_slice && vb.i_data && ( nal.i_type == NAL_SPS || nal.i_type == NAL_PPS ) )
297 avi_write( &avi, &vb, b_key );
302 /* Parse SPS/PPS/Slice */
303 if( ParseNAL( &nal, &avi, &h264, &b_slice ) && vb.i_data > 0 )
305 avi_write( &avi, &vb, b_key );
309 /* fprintf( stderr, "nal:%d ref:%d\n", nal.i_type, nal.i_ref_idc ); */
311 /* Append NAL to buffer */
312 vbuf_add( &vb, i_size + 3, p );
314 /* Remove this nal */
315 memmove( &data[0], p_next, end - p_next );
316 i_data -= p_next - &data[0];
321 avi_write( &avi, &vb, h264.b_key );
324 avi.i_width = h264.i_width;
325 avi.i_height = h264.i_height;
330 free( nal.p_payload );
338 /*****************************************************************************
340 *****************************************************************************/
341 static void Help( void )
345 "Syntax: avc2avi [options] [ -i input.h264 ] [ -o output.avi ]\n"
347 " -h, --help Print this help\n"
349 " -i, --input Specify input file (default: stdin)\n"
350 " -o, --output Specify output file (default: stdout)\n"
352 " -f, --fps <float> Set FPS (default: 25.0)\n"
353 " -c, --codec <string> Set the codec fourcc (default: 'h264')\n"
357 /*****************************************************************************
359 *****************************************************************************/
360 static int Parse( int argc, char **argv, cfg_t *cfg )
362 /* Set default values */
364 cfg->psz_fout = NULL;
366 memcpy( cfg->fcc, "h264", 4 );
368 /* Parse command line options */
369 opterr = 0; // no error message
372 int long_options_index;
373 static struct option long_options[] =
375 { "help", no_argument, NULL, 'h' },
376 { "input", required_argument, NULL, 'i' },
377 { "output", required_argument, NULL, 'o' },
378 { "fps", required_argument, NULL, 'f' },
379 { "codec", required_argument, NULL, 'c' },
385 c = getopt_long( argc, argv, "hi:o:f:c:",
386 long_options, &long_options_index);
402 cfg->psz_fin = strdup( optarg );
405 cfg->psz_fout = strdup( optarg );
408 cfg->f_fps = atof( optarg );
411 memset( cfg->fcc, ' ', 4 );
412 memcpy( cfg->fcc, optarg, strlen( optarg ) < 4 ? strlen( optarg ) : 4 );
416 fprintf( stderr, "unknown option (%c)\n", optopt );
425 /*****************************************************************************
427 *****************************************************************************/
428 void h264_parser_init( h264_t *h )
435 h->i_idr_pic_id = -1;
437 h->i_log2_max_frame_num = 0;
441 void h264_parser_parse( h264_t *h, nal_t *nal, int *pb_nal_start )
446 if( nal->i_type == NAL_SPS || nal->i_type == NAL_PPS )
449 bs_init( &s, nal->p_payload, nal->i_payload );
450 if( nal->i_type == NAL_SPS )
454 i_tmp = bs_read( &s, 8 );
455 bs_skip( &s, 1+1+1 + 5 + 8 );
461 bs_read_ue( &s ); // chroma_format_idc
462 bs_read_ue( &s ); // bit_depth_luma_minus8
463 bs_read_ue( &s ); // bit_depth_chroma_minus8
464 bs_skip( &s, 1 ); // qpprime_y_zero_transform_bypass_flag
465 if( bs_read( &s, 1 ) ) // seq_scaling_matrix_present_flag
468 for( i = 0; i < 8; i++ )
470 if( bs_read( &s, 1 ) ) // seq_scaling_list_present_flag[i]
473 for( j = 0; j < (i<6?16:64); j++ )
475 i_tmp += bs_read_se( &s );
484 /* Skip i_log2_max_frame_num */
485 h->i_log2_max_frame_num = bs_read_ue( &s ) + 4;
487 h->i_poc_type = bs_read_ue( &s );
488 if( h->i_poc_type == 0 )
490 h->i_log2_max_poc_lsb = bs_read_ue( &s ) + 4;
492 else if( h->i_poc_type == 1 )
495 /* skip b_delta_pic_order_always_zero */
497 /* skip i_offset_for_non_ref_pic */
499 /* skip i_offset_for_top_to_bottom_field */
501 /* read i_num_ref_frames_in_poc_cycle */
502 i_cycle = bs_read_ue( &s );
503 if( i_cycle > 256 ) i_cycle = 256;
506 /* skip i_offset_for_ref_frame */
510 /* i_num_ref_frames */
512 /* b_gaps_in_frame_num_value_allowed */
516 h->i_width = 16 * ( bs_read_ue( &s ) + 1 );
517 h->i_height = 16 * ( bs_read_ue( &s ) + 1 );
519 /* b_frame_mbs_only */
520 i_tmp = bs_read( &s, 1 );
525 /* b_direct8x8_inference */
529 i_tmp = bs_read( &s, 1 );
533 h->i_width -= 2 * bs_read_ue( &s );
535 h->i_width -= 2 * bs_read_ue( &s );
537 h->i_height -= 2 * bs_read_ue( &s );
539 h->i_height -= 2 * bs_read_ue( &s );
544 else if( nal->i_type >= NAL_SLICE && nal->i_type <= NAL_SLICE_IDR )
551 switch( bs_read_ue( &s ) )
553 case 0: case 5: /* P */
554 case 1: case 6: /* B */
555 case 3: case 8: /* SP */
558 case 2: case 7: /* I */
559 case 4: case 9: /* SI */
560 h->b_key = (nal->i_type == NAL_SLICE_IDR);
567 i_tmp = bs_read( &s, h->i_log2_max_frame_num );
569 if( i_tmp != h->i_frame_num )
572 h->i_frame_num = i_tmp;
574 if( nal->i_type == NAL_SLICE_IDR )
576 i_tmp = bs_read_ue( &s );
577 if( h->i_nal_type == NAL_SLICE_IDR && h->i_idr_pic_id != i_tmp )
580 h->i_idr_pic_id = i_tmp;
583 if( h->i_poc_type == 0 )
585 i_tmp = bs_read( &s, h->i_log2_max_poc_lsb );
586 if( i_tmp != h->i_poc )
591 h->i_nal_type = nal->i_type;
592 h->i_ref_idc = nal->i_ref_idc;
596 static int ParseNAL( nal_t *nal, avi_t *a, h264_t *h, int *pb_slice )
601 h264_parser_parse( h, nal, &b_start );
603 if( b_start && *pb_slice )
609 if( nal->i_type >= NAL_SLICE && nal->i_type <= NAL_SLICE_IDR )
615 /*****************************************************************************
616 * vbuf: variable buffer
617 *****************************************************************************/
618 void vbuf_init( vbuf_t *v )
621 v->i_data_max = 10000;
622 v->p_data = malloc( v->i_data_max );
624 void vbuf_add( vbuf_t *v, int i_data, void *p_data )
626 if( i_data + v->i_data >= v->i_data_max )
628 v->i_data_max += i_data;
629 v->p_data = realloc( v->p_data, v->i_data_max );
631 memcpy( &v->p_data[v->i_data], p_data, i_data );
635 void vbuf_reset( vbuf_t *v )
640 /*****************************************************************************
642 *****************************************************************************/
643 void avi_write_uint16( avi_t *a, uint16_t w )
645 fputc( ( w ) & 0xff, a->f );
646 fputc( ( w >> 8 ) & 0xff, a->f );
649 void avi_write_uint32( avi_t *a, uint32_t dw )
651 fputc( ( dw ) & 0xff, a->f );
652 fputc( ( dw >> 8 ) & 0xff, a->f );
653 fputc( ( dw >> 16) & 0xff, a->f );
654 fputc( ( dw >> 24) & 0xff, a->f );
657 void avi_write_fourcc( avi_t *a, char fcc[4] )
659 fputc( fcc[0], a->f );
660 fputc( fcc[1], a->f );
661 fputc( fcc[2], a->f );
662 fputc( fcc[3], a->f );
666 #define AVIF_HASINDEX 0x00000010 // Index at end of file?
667 #define AVIF_ISINTERLEAVED 0x00000100
668 #define AVIF_TRUSTCKTYPE 0x00000800 // Use CKType to find key frames?
670 #define AVIIF_KEYFRAME 0x00000010L /* this frame is a key frame.*/
672 void avi_write_header( avi_t *a )
674 avi_write_fourcc( a, "RIFF" );
675 avi_write_uint32( a, a->i_riff > 0 ? a->i_riff - 8 : 0xFFFFFFFF );
676 avi_write_fourcc( a, "AVI " );
678 avi_write_fourcc( a, "LIST" );
679 avi_write_uint32( a, 4 + 4*16 + 12 + 4*16 + 4*12 );
680 avi_write_fourcc( a, "hdrl" );
682 avi_write_fourcc( a, "avih" );
683 avi_write_uint32( a, 4*16 - 8 );
684 avi_write_uint32( a, 1000000 / a->f_fps );
685 avi_write_uint32( a, 0xffffffff );
686 avi_write_uint32( a, 0 );
687 avi_write_uint32( a, AVIF_HASINDEX|AVIF_ISINTERLEAVED|AVIF_TRUSTCKTYPE);
688 avi_write_uint32( a, a->i_frame );
689 avi_write_uint32( a, 0 );
690 avi_write_uint32( a, 1 );
691 avi_write_uint32( a, 1000000 );
692 avi_write_uint32( a, a->i_width );
693 avi_write_uint32( a, a->i_height );
694 avi_write_uint32( a, 0 );
695 avi_write_uint32( a, 0 );
696 avi_write_uint32( a, 0 );
697 avi_write_uint32( a, 0 );
699 avi_write_fourcc( a, "LIST" );
700 avi_write_uint32( a, 4 + 4*16 + 4*12 );
701 avi_write_fourcc( a, "strl" );
703 avi_write_fourcc( a, "strh" );
704 avi_write_uint32( a, 4*16 - 8 );
705 avi_write_fourcc( a, "vids" );
706 avi_write_fourcc( a, a->fcc );
707 avi_write_uint32( a, 0 );
708 avi_write_uint32( a, 0 );
709 avi_write_uint32( a, 0 );
710 avi_write_uint32( a, 1000 );
711 avi_write_uint32( a, a->f_fps * 1000 );
712 avi_write_uint32( a, 0 );
713 avi_write_uint32( a, a->i_frame );
714 avi_write_uint32( a, 1024*1024 );
715 avi_write_uint32( a, -1 );
716 avi_write_uint32( a, a->i_width * a->i_height );
717 avi_write_uint32( a, 0 );
718 avi_write_uint16( a, a->i_width );
719 avi_write_uint16( a, a->i_height );
721 avi_write_fourcc( a, "strf" );
722 avi_write_uint32( a, 4*12 - 8 );
723 avi_write_uint32( a, 4*12 - 8 );
724 avi_write_uint32( a, a->i_width );
725 avi_write_uint32( a, a->i_height );
726 avi_write_uint16( a, 1 );
727 avi_write_uint16( a, 24 );
728 avi_write_fourcc( a, a->fcc );
729 avi_write_uint32( a, a->i_width * a->i_height );
730 avi_write_uint32( a, 0 );
731 avi_write_uint32( a, 0 );
732 avi_write_uint32( a, 0 );
733 avi_write_uint32( a, 0 );
735 avi_write_fourcc( a, "LIST" );
736 avi_write_uint32( a, a->i_movi_end > 0 ? a->i_movi_end - a->i_movi + 4: 0xFFFFFFFF );
737 avi_write_fourcc( a, "movi" );
740 void avi_write_idx( avi_t *a )
742 avi_write_fourcc( a, "idx1" );
743 avi_write_uint32( a, a->i_frame * 16 );
744 fwrite( a->idx, a->i_frame * 16, 1, a->f );
747 void avi_init( avi_t *a, FILE *f, float f_fps, char fcc[4] )
751 memcpy( a->fcc, fcc, 4 );
761 avi_write_header( a );
763 a->i_movi = ftell( a->f );
766 static void avi_set_dw( void *_p, uint32_t dw )
771 p[1] = ( dw >> 8 )&0xff;
772 p[2] = ( dw >> 16)&0xff;
773 p[3] = ( dw >> 24)&0xff;
776 void avi_write( avi_t *a, vbuf_t *v, int b_key )
778 int64_t i_pos = ftell( a->f );
781 avi_write_fourcc( a, "00dc" );
782 avi_write_uint32( a, v->i_data );
784 fwrite( v->p_data, v->i_data, 1, a->f );
792 /* Append idx chunk */
793 if( a->i_idx_max <= a->i_frame )
795 a->i_idx_max += 1000;
796 a->idx = realloc( a->idx, a->i_idx_max * 16 );
799 memcpy( &a->idx[4*a->i_frame+0], "00dc", 4 );
800 avi_set_dw( &a->idx[4*a->i_frame+1], b_key ? AVIIF_KEYFRAME : 0 );
801 avi_set_dw( &a->idx[4*a->i_frame+2], i_pos );
802 avi_set_dw( &a->idx[4*a->i_frame+3], v->i_data );
807 void avi_end( avi_t *a )
809 a->i_movi_end = ftell( a->f );
814 a->i_riff = ftell( a->f );
817 fseek( a->f, 0, SEEK_SET );
818 avi_write_header( a );
820 fprintf( stderr, "avi file written\n" );
821 fprintf( stderr, " - codec: %4.4s\n", a->fcc );
822 fprintf( stderr, " - size: %dx%d\n", a->i_width, a->i_height );
823 fprintf( stderr, " - fps: %.3f\n", a->f_fps );
824 fprintf( stderr, " - frames: %d\n", a->i_frame );
827 /*****************************************************************************
829 *****************************************************************************/
830 int nal_decode( nal_t *nal, void *p_data, int i_data )
832 uint8_t *src = p_data;
833 uint8_t *end = &src[i_data];
834 uint8_t *dst = nal->p_payload;
836 nal->i_type = src[0]&0x1f;
837 nal->i_ref_idc = (src[0] >> 5)&0x03;
843 if( src < end - 3 && src[0] == 0x00 && src[1] == 0x00 && src[2] == 0x03 )
854 nal->i_payload = dst - (uint8_t*)nal->p_payload;