/*************************************************************** ** ** Broadcom Corp. Confidential ** Copyright 1998-2001 Broadcom Corp. All Rights Reserved. ** ** THIS SOFTWARE MAY ONLY BE USED SUBJECT TO AN EXECUTED ** SOFTWARE LICENSE AGREEMENT BETWEEN THE USER AND BROADCOM. ** YOU HAVE NO RIGHT TO USE OR EXPLOIT THIS MATERIAL EXCEPT ** SUBJECT TO THE TERMS OF SUCH AN AGREEMENT. ** ** File: main.c ** Description: Broadcom Stream Indexer Main ** ** Created: 05/16/2001 by Marcus Kellerman ** ** REVISION: ** ** $Log: $ ** ** ****************************************************************/ #include #include #include /* strcmp() */ #include #if !defined(__vxworks) #include /* ftime() */ #endif #include "tsindexer.h" #include "bcmindexer.h" #include "brcm_dbg.h" #define NUM_STREAM_BYTES_TO_READ (188*1024) #define NUM_SC_BYTES_TO_READ (sizeof(sIndexEntry)*1024) void TimetoString( long ctime, char *outStr ) { long hh,mm,ss; char *p; ss = ctime % 60; mm = (ctime/60) % 60; hh = (ctime/3600) % 24; p = outStr; *p++ = (hh/10) + 0x30; *p++ = (hh%10) + 0x30; *p++ = ':'; *p++ = (mm/10) + 0x30; *p++ = (mm%10) + 0x30; *p++ = ':'; *p++ = (ss/10) + 0x30; *p++ = (ss%10) + 0x30; *p++ = 0; } double getms( void ) { #if defined(__vxworks) return 0; #else double usec1; struct timeb t; ftime( &t ); usec1 = t.time + t.millitm/1000.0; return usec1; #endif } void OffsetToString( long offset, char *outStr ) { long mb, kb, b; b = offset % 1000; kb = (offset/1000)%1000; mb = (offset/(1000*1000)) % (1000*1000); sprintf( outStr, "%ld,%03ld,%03ld", mb, kb, b ); } /** * Feed from tsindexer to bcmindexer. **/ static unsigned long write_to_bcmindexer( const void *p_bfr, unsigned long entrySize, unsigned long numEntries, void *fp ) { return BNAV_Indexer_Feed( (BNAV_Indexer_Handle)fp, (BSCT_Entry *)p_bfr, (numEntries * entrySize) / sizeof(BSCT_Entry)) * sizeof(BSCT_Entry) / entrySize; } void printHeader() { printf("\ncreateindex, built on %s\n\n", __DATE__); printf("Broadcom Corp. Confidential\n"); printf("Copyright 1998-2003 Broadcom Corp. All Rights Reserved.\n\n"); } /** * This is a modification of createindex which allows * the stuff of gigabytes of bogus MPEG into the * index in order to test wrap-detection. * * This was needed to test PR7218. **/ #define FAKE_GAP #ifdef FAKE_GAP off_t fake_data_sent = 0; #endif static FILE *g_mpegFile = NULL; // global hack int mpegSizeCallback(BNAV_Indexer_Handle handle, unsigned long *hi, unsigned long *lo) { off_t o = ftello(g_mpegFile); if (o == -1) return -1; #ifdef FAKE_GAP o += fake_data_sent; #endif *hi = o >> 32; *lo = o & 0xFFFFFFFF; return 0; } /** * This is the entry point for VxWorks. * * input: 0=ts,1=pes,2=sct * output: 0=sct,1=bcm **/ int createindex(const char *inputfile, const char *outputfile, int pid, int input, int output, BNAV_Version navVersion) { unsigned char *bfr; FILE *fin, *fout; sTsIndexer *indexer = NULL; BNAV_Indexer_Handle bcmindexer = NULL; unsigned long readBytes; unsigned long totalBytes = 0; double time1, time2; int pesId = 0; int generateBcmIndex = 0; int sctInput = 0; unsigned long numBytesToRead; printHeader(); time1 = getms(); if (input == 1) pesId = pid; else if (input == 2) sctInput = 1; if (sctInput) numBytesToRead = NUM_SC_BYTES_TO_READ; else numBytesToRead = NUM_STREAM_BYTES_TO_READ; if (output == 1) generateBcmIndex = 1; if (pid == 0 && !sctInput) printf( "WARNING\n" "WARNING\n" "WARNING: Are you sure you want PID == 0 ??????????????\n" "WARNING\n" "WARNING\n" "\n" ); if (sctInput && !generateBcmIndex) { printf("If your input is a start code index, then -bcm is required.\n"); exit(1); } bfr = malloc( numBytesToRead ); if (!strcmp(inputfile, "-")) { fin = stdin; inputfile = "stdin"; } else if( (fin = fopen(inputfile, "rb" )) == NULL ) { printf("Unable to open input file %s\n", inputfile); exit(255); } if (!strcmp(outputfile, "-")) { fout = stdout; outputfile = "stdout"; } else if( (fout = fopen(outputfile, "wb" )) == NULL ) { printf("Unable to open output file %s\n", outputfile); fclose(fin); exit(255); } printf("Processing %s 0x%X from %s input file '%s'.\n", pesId?"StreamID":"PID", pesId?pesId:pid, pesId?"MPEG PES":sctInput?"SCT":"MPEG TS", inputfile); if (generateBcmIndex) printf("Writing BCM index (ver %d) to '%s'.\n", navVersion, outputfile); else printf("Writing SCT index to '%s'.\n", outputfile); if (generateBcmIndex) { BNAV_Indexer_Settings settings; BNAV_Indexer_GetDefaultSettings(&settings); settings.writeCallback = (INDEX_WRITE_CB)fwrite; settings.filePointer = (void *)fout; settings.navVersion = navVersion; if (!sctInput) { settings.mpegSizeCallback = mpegSizeCallback; g_mpegFile = fin; } if (BNAV_Indexer_Open(&bcmindexer, &settings)) { exit(1); } } if (!sctInput) { if (generateBcmIndex) /* pass through to bcmindex */ indexer = tsindex_allocate( (INDEX_WRITE_CB)write_to_bcmindexer, (void *)bcmindexer, (unsigned short)pid, -1 ); else indexer = tsindex_allocate( (INDEX_WRITE_CB)fwrite, (void *)fout, (unsigned short)pid, -1 ); if( pesId ) tsindex_setPesId( indexer, (unsigned char)pesId ); } while( (readBytes = fread( bfr, 1, numBytesToRead, fin )) != 0 ) { totalBytes += readBytes; if ( totalBytes%(1*numBytesToRead) == 0 ) { double rate1; char timeStr[32], offsetStr[32]; time2 = getms() - time1; rate1 = (totalBytes/1024)/time2; TimetoString( (long)time2, timeStr ); OffsetToString( totalBytes, offsetStr ); printf( "Byte Offset: %s Rate: %0.1fK/s, Elapsed Time: %s \r", offsetStr, rate1, timeStr ); } if (sctInput) { BNAV_Indexer_Feed(bcmindexer, (BSCT_Entry*)bfr, readBytes/sizeof(BSCT_Entry) ); } else if( pesId ) { tsindex_feedPes( indexer, bfr, readBytes ); } else { tsindex_feed( indexer, bfr, readBytes ); } #ifdef FAKE_GAP #define START_OF_JUNK (10 * 1024 * 1024) #define AMOUNT_OF_JUNK1 ((unsigned long)1024 * 1024 * 1024) #define AMOUNT_OF_JUNK2 16 { static int doonce = 0; if (sctInput || pesId) { BRCM_DBG_ERR(("FAKE_GAP must use TS in")); exit(1); } if (!doonce && totalBytes >= START_OF_JUNK) { int i,j; unsigned char junkbuf[188]; doonce = 1; junkbuf[0] = 0x47; // just enough to fake it out printf("\nFeeding junk\n"); for (j=0;j3 && argv[3][0] != '-') { if (strstr(argv[3], "0x") == argv[3]) sscanf( argv[3], "0x%X", &pid ); else pid = atoi( argv[3] ); ++nextparam; } while (nextparam < argc) { if (!strcmp(argv[nextparam], "-ts")) input = 0; else if (!strcmp(argv[nextparam], "-pes")) input = 1; else if (!strcmp(argv[nextparam], "-sct")) input = 2; else if (!strcmp(argv[nextparam], "-bcm")) output = 1; else if (!strcmp(argv[nextparam], "-bcmver") && nextparam+1 < argc) navVersion = atoi(argv[++nextparam]); nextparam++; } if (input == -1) { char *s; // check the suffix of the inputfile if (((s = strstr(source, ".sct")) && !s[4]) || ((s = strstr(source, ".idx")) && !s[4])) { input = 2; } else if ((s = strstr(source, ".pes")) && !s[4]) { input = 1; } else input = 0; } if (output == -1) { char *s; if (((s = strstr(dest, ".nav")) && !s[4]) || ((s = strstr(dest, ".bcm")) && !s[4])) output = 1; else output = 0; } return createindex(source,dest,pid,input,output,navVersion); } } #endif