1 /*****************************************************************************
2 * avc2avi.c: raw h264 -> AVI
3 *****************************************************************************
4 * Copyright (C) 2004 Laurent Aimar
5 * $Id: avc2avi.c,v 1.1 2004/06/03 19:27:08 fenrir Exp $
7 * Authors: Laurent Aimar <fenrir@via.ecp.fr>
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., 59 Temple Place - Suite 330, 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 */
135 int i_log2_max_frame_num;
138 void h264_parser_init( h264_t * );
139 void h264_parser_parse( h264_t *h, nal_t *n, int *pb_nal_start );
142 static int nal_decode( nal_t *nal, void *p_data, int i_data );
144 static void Help( void );
145 static int Parse( int argc, char **argv, cfg_t * );
146 static int ParseNAL( nal_t *nal, avi_t *a, h264_t *h, int *pb_slice );
148 /****************************************************************************
150 ****************************************************************************/
151 int main( int argc, char **argv )
170 _setmode(_fileno(stdin), _O_BINARY); /* thanks to Marcos Morais <morais at dee.ufcg.edu.br> */
171 _setmode(_fileno(stdout), _O_BINARY);
174 /* Parse command line */
175 if( Parse( argc, argv, &cfg ) < 0 )
181 if( cfg.psz_fin == NULL || *cfg.psz_fin == '\0' || !strcmp( cfg.psz_fin, "-" ) )
184 fin = fopen( cfg.psz_fin, "rb" );
187 fprintf( stderr, "cannot open input file\n" );
192 if( cfg.psz_fout == NULL || *cfg.psz_fout == '\0' || !strcmp( cfg.psz_fout, "-" ) )
195 fout = fopen( cfg.psz_fout, "wb" );
198 fprintf( stderr, "cannot open output file\n" );
203 avi_init( &avi, fout, cfg.f_fps, cfg.fcc );
206 h264_parser_init( &h264 );
208 /* Control-C handler */
209 signal( SIGINT, SigIntHandler );
218 /* Alloc space for a nal, used for decoding pps/sps/slice header */
219 nal.p_payload = malloc( DATA_MAX );
226 uint8_t *p, *p_next, *end;
230 if( i_data < DATA_MAX && !b_eof )
232 int i_read = fread( &data[i_data], 1, DATA_MAX - i_data, fin );
243 /* Search begin of a NAL */
247 if( p[0] == 0x00 && p[1] == 0x00 && p[2] == 0x01 )
256 fprintf( stderr, "garbage (i_data = %d)\n", i_data );
261 /* Search end of NAL */
263 while( p_next < end - 3 )
265 if( p_next[0] == 0x00 && p_next[1] == 0x00 && p_next[2] == 0x01 )
272 if( p_next == end - 3 && i_data < DATA_MAX )
275 /* Compute NAL size */
276 i_size = p_next - p - 3;
282 fprintf( stderr, "nal too large (FIXME) ?\n" );
287 /* Nal start at p+3 with i_size length */
288 nal_decode( &nal, p +3, i_size < 2048 ? i_size : 2048 );
292 if( b_slice && vb.i_data && ( nal.i_type == NAL_SPS || nal.i_type == NAL_PPS ) )
294 avi_write( &avi, &vb, b_key );
299 /* Parse SPS/PPS/Slice */
300 if( ParseNAL( &nal, &avi, &h264, &b_slice ) && vb.i_data > 0 )
302 avi_write( &avi, &vb, b_key );
306 /* fprintf( stderr, "nal:%d ref:%d\n", nal.i_type, nal.i_ref_idc ); */
308 /* Append NAL to buffer */
309 vbuf_add( &vb, i_size + 3, p );
311 /* Remove this nal */
312 memmove( &data[0], p_next, end - p_next );
313 i_data -= p_next - &data[0];
318 avi_write( &avi, &vb, h264.b_key );
321 avi.i_width = h264.i_width;
322 avi.i_height = h264.i_height;
327 free( nal.p_payload );
335 /*****************************************************************************
337 *****************************************************************************/
338 static void Help( void )
342 "Syntax: avc2avi [options] [ -i input.h264 ] [ -o output.avi ]\n"
344 " -h, --help Print this help\n"
346 " -i, --input Specify input file (default: stdin)\n"
347 " -o, --output Specify output file (default: stdout)\n"
349 " -f, --fps <float> Set FPS (default: 25.0)\n"
350 " -c, --codec <string> Set the codec fourcc (default: 'h264')\n"
354 /*****************************************************************************
356 *****************************************************************************/
357 static int Parse( int argc, char **argv, cfg_t *cfg )
359 /* Set default values */
361 cfg->psz_fout = NULL;
363 memcpy( cfg->fcc, "h264", 4 );
365 /* Parse command line options */
366 opterr = 0; // no error message
369 int long_options_index;
370 static struct option long_options[] =
372 { "help", no_argument, NULL, 'h' },
373 { "input", required_argument, NULL, 'i' },
374 { "output", required_argument, NULL, 'o' },
375 { "fps", required_argument, NULL, 'f' },
376 { "codec", required_argument, NULL, 'c' },
382 c = getopt_long( argc, argv, "hi:o:f:c:",
383 long_options, &long_options_index);
399 cfg->psz_fin = strdup( optarg );
402 cfg->psz_fout = strdup( optarg );
405 cfg->f_fps = atof( optarg );
408 memset( cfg->fcc, ' ', 4 );
409 memcpy( cfg->fcc, optarg, strlen( optarg ) < 4 ? strlen( optarg ) : 4 );
413 fprintf( stderr, "unknown option (%c)\n", optopt );
422 /*****************************************************************************
424 *****************************************************************************/
425 void h264_parser_init( h264_t *h )
432 h->i_idr_pic_id = -1;
434 h->i_log2_max_frame_num = 0;
436 void h264_parser_parse( h264_t *h, nal_t *nal, int *pb_nal_start )
441 if( nal->i_type == NAL_SPS || nal->i_type == NAL_PPS )
444 bs_init( &s, nal->p_payload, nal->i_payload );
445 if( nal->i_type == NAL_SPS )
449 bs_skip( &s, 8 + 1+1+1 + 5 + 8 );
452 /* Skip i_log2_max_frame_num */
453 h->i_log2_max_frame_num = bs_read_ue( &s ) + 4;
455 i_tmp = bs_read_ue( &s );
458 /* skip i_log2_max_poc_lsb */
461 else if( i_tmp == 1 )
464 /* skip b_delta_pic_order_always_zero */
466 /* skip i_offset_for_non_ref_pic */
468 /* skip i_offset_for_top_to_bottom_field */
470 /* read i_num_ref_frames_in_poc_cycle */
471 i_cycle = bs_read_ue( &s );
472 if( i_cycle > 256 ) i_cycle = 256;
475 /* skip i_offset_for_ref_frame */
479 /* i_num_ref_frames */
481 /* b_gaps_in_frame_num_value_allowed */
485 h->i_width = 16 * ( bs_read_ue( &s ) + 1 );
486 h->i_height = 16 * ( bs_read_ue( &s ) + 1 );
488 /* b_frame_mbs_only */
489 i_tmp = bs_read( &s, 1 );
494 /* b_direct8x8_inference */
498 i_tmp = bs_read( &s, 1 );
502 h->i_width -= 2 * bs_read_ue( &s );
504 h->i_width -= 2 * bs_read_ue( &s );
506 h->i_height -= 2 * bs_read_ue( &s );
508 h->i_height -= 2 * bs_read_ue( &s );
513 else if( nal->i_type >= NAL_SLICE && nal->i_type <= NAL_SLICE_IDR )
520 switch( bs_read_ue( &s ) )
522 case 0: case 5: /* P */
523 case 1: case 6: /* B */
524 case 3: case 8: /* SP */
527 case 2: case 7: /* I */
530 case 4: case 9: /* ? */
538 i_tmp = bs_read( &s, h->i_log2_max_frame_num );
540 if( i_tmp != h->i_frame_num )
543 h->i_frame_num = i_tmp;
545 if( nal->i_type == NAL_SLICE_IDR )
547 i_tmp = bs_read_ue( &s );
548 if( h->i_nal_type == NAL_SLICE_IDR && h->i_idr_pic_id != i_tmp )
551 h->i_idr_pic_id = i_tmp;
554 h->i_nal_type = nal->i_type;
555 h->i_ref_idc = nal->i_ref_idc;
559 static int ParseNAL( nal_t *nal, avi_t *a, h264_t *h, int *pb_slice )
564 h264_parser_parse( h, nal, &b_start );
566 if( b_start && *pb_slice )
572 if( nal->i_type >= NAL_SLICE && nal->i_type <= NAL_SLICE_IDR )
578 /*****************************************************************************
579 * vbuf: variable buffer
580 *****************************************************************************/
581 void vbuf_init( vbuf_t *v )
584 v->i_data_max = 10000;
585 v->p_data = malloc( v->i_data_max );
587 void vbuf_add( vbuf_t *v, int i_data, void *p_data )
589 if( i_data + v->i_data >= v->i_data_max )
591 v->i_data_max += i_data;
592 v->p_data = realloc( v->p_data, v->i_data_max );
594 memcpy( &v->p_data[v->i_data], p_data, i_data );
598 void vbuf_reset( vbuf_t *v )
603 /*****************************************************************************
605 *****************************************************************************/
606 void avi_write_uint16( avi_t *a, uint16_t w )
608 fputc( ( w ) & 0xff, a->f );
609 fputc( ( w >> 8 ) & 0xff, a->f );
612 void avi_write_uint32( avi_t *a, uint32_t dw )
614 fputc( ( dw ) & 0xff, a->f );
615 fputc( ( dw >> 8 ) & 0xff, a->f );
616 fputc( ( dw >> 16) & 0xff, a->f );
617 fputc( ( dw >> 24) & 0xff, a->f );
620 void avi_write_fourcc( avi_t *a, char fcc[4] )
622 fputc( fcc[0], a->f );
623 fputc( fcc[1], a->f );
624 fputc( fcc[2], a->f );
625 fputc( fcc[3], a->f );
629 #define AVIF_HASINDEX 0x00000010 // Index at end of file?
630 #define AVIF_ISINTERLEAVED 0x00000100
631 #define AVIF_TRUSTCKTYPE 0x00000800 // Use CKType to find key frames?
633 #define AVIIF_KEYFRAME 0x00000010L /* this frame is a key frame.*/
635 void avi_write_header( avi_t *a )
637 avi_write_fourcc( a, "RIFF" );
638 avi_write_uint32( a, a->i_riff > 0 ? a->i_riff - 8 : 0xFFFFFFFF );
639 avi_write_fourcc( a, "AVI " );
641 avi_write_fourcc( a, "LIST" );
642 avi_write_uint32( a, 4 + 4*16 + 12 + 4*16 + 4*12 );
643 avi_write_fourcc( a, "hdrl" );
645 avi_write_fourcc( a, "avih" );
646 avi_write_uint32( a, 4*16 - 8 );
647 avi_write_uint32( a, 1000000 / a->f_fps );
648 avi_write_uint32( a, 0xffffffff );
649 avi_write_uint32( a, 0 );
650 avi_write_uint32( a, AVIF_HASINDEX|AVIF_ISINTERLEAVED|AVIF_TRUSTCKTYPE);
651 avi_write_uint32( a, a->i_frame );
652 avi_write_uint32( a, 0 );
653 avi_write_uint32( a, 1 );
654 avi_write_uint32( a, 1000000 );
655 avi_write_uint32( a, a->i_width );
656 avi_write_uint32( a, a->i_height );
657 avi_write_uint32( a, 0 );
658 avi_write_uint32( a, 0 );
659 avi_write_uint32( a, 0 );
660 avi_write_uint32( a, 0 );
662 avi_write_fourcc( a, "LIST" );
663 avi_write_uint32( a, 4 + 4*16 + 4*12 );
664 avi_write_fourcc( a, "strl" );
666 avi_write_fourcc( a, "strh" );
667 avi_write_uint32( a, 4*16 - 8 );
668 avi_write_fourcc( a, "vids" );
669 avi_write_fourcc( a, a->fcc );
670 avi_write_uint32( a, 0 );
671 avi_write_uint32( a, 0 );
672 avi_write_uint32( a, 0 );
673 avi_write_uint32( a, 1000 );
674 avi_write_uint32( a, a->f_fps * 1000 );
675 avi_write_uint32( a, 0 );
676 avi_write_uint32( a, a->i_frame );
677 avi_write_uint32( a, 1024*1024 );
678 avi_write_uint32( a, -1 );
679 avi_write_uint32( a, a->i_width * a->i_height );
680 avi_write_uint32( a, 0 );
681 avi_write_uint16( a, a->i_width );
682 avi_write_uint16( a, a->i_height );
684 avi_write_fourcc( a, "strf" );
685 avi_write_uint32( a, 4*12 - 8 );
686 avi_write_uint32( a, 4*12 - 8 );
687 avi_write_uint32( a, a->i_width );
688 avi_write_uint32( a, a->i_height );
689 avi_write_uint16( a, 1 );
690 avi_write_uint16( a, 24 );
691 avi_write_fourcc( a, a->fcc );
692 avi_write_uint32( a, a->i_width * a->i_height );
693 avi_write_uint32( a, 0 );
694 avi_write_uint32( a, 0 );
695 avi_write_uint32( a, 0 );
696 avi_write_uint32( a, 0 );
698 avi_write_fourcc( a, "LIST" );
699 avi_write_uint32( a, a->i_movi_end > 0 ? a->i_movi_end - a->i_movi + 4: 0xFFFFFFFF );
700 avi_write_fourcc( a, "movi" );
703 void avi_write_idx( avi_t *a )
705 avi_write_fourcc( a, "idx1" );
706 avi_write_uint32( a, a->i_frame * 16 );
707 fwrite( a->idx, a->i_frame * 16, 1, a->f );
710 void avi_init( avi_t *a, FILE *f, float f_fps, char fcc[4] )
714 memcpy( a->fcc, fcc, 4 );
724 avi_write_header( a );
726 a->i_movi = ftell( a->f );
729 static void avi_set_dw( void *_p, uint32_t dw )
734 p[1] = ( dw >> 8 )&0xff;
735 p[2] = ( dw >> 16)&0xff;
736 p[3] = ( dw >> 24)&0xff;
739 void avi_write( avi_t *a, vbuf_t *v, int b_key )
741 int64_t i_pos = ftell( a->f );
744 avi_write_fourcc( a, "00dc" );
745 avi_write_uint32( a, v->i_data );
747 fwrite( v->p_data, v->i_data, 1, a->f );
755 /* Append idx chunk */
756 if( a->i_idx_max <= a->i_frame )
758 a->i_idx_max += 1000;
759 a->idx = realloc( a->idx, a->i_idx_max * 16 );
762 memcpy( &a->idx[4*a->i_frame+0], "00dc", 4 );
763 avi_set_dw( &a->idx[4*a->i_frame+1], b_key ? AVIIF_KEYFRAME : 0 );
764 avi_set_dw( &a->idx[4*a->i_frame+2], i_pos );
765 avi_set_dw( &a->idx[4*a->i_frame+3], v->i_data );
770 void avi_end( avi_t *a )
772 a->i_movi_end = ftell( a->f );
777 a->i_riff = ftell( a->f );
780 fseek( a->f, 0, SEEK_SET );
781 avi_write_header( a );
783 fprintf( stderr, "avi file written\n" );
784 fprintf( stderr, " - codec: %4.4s\n", a->fcc );
785 fprintf( stderr, " - size: %dx%d\n", a->i_width, a->i_height );
786 fprintf( stderr, " - fps: %.3f\n", a->f_fps );
787 fprintf( stderr, " - frames: %d\n", a->i_frame );
790 /*****************************************************************************
792 *****************************************************************************/
793 int nal_decode( nal_t *nal, void *p_data, int i_data )
795 uint8_t *src = p_data;
796 uint8_t *end = &src[i_data];
797 uint8_t *dst = nal->p_payload;
799 nal->i_type = src[0]&0x1f;
800 nal->i_ref_idc = (src[0] >> 5)&0x03;
806 if( src < end - 3 && src[0] == 0x00 && src[1] == 0x00 && src[2] == 0x03 )
817 nal->i_payload = dst - (uint8_t*)p_data;