Ignore:
Timestamp:
10/27/06 18:42:05 (17 years ago)
Author:
r2d
Message:
  • added crc32
  • first sv8 encodes, main changes in bitstream.c
File:
1 edited

Legend:

Unmodified
Added
Removed
  • mppenc/branches/r2d/src/mppenc.c

    r77 r78  
    14081408
    14091409
    1410 void OverdriveReport ( mpc_encoder_t * e )
     1410static void OverdriveReport ( mpc_encoder_t * e )
    14111411{
    14121412        if ( e->Overflows > 0 ) {                                                // report internal clippings
     
    14321432#include <fpu_control.h>
    14331433
    1434 void Init_FPU ( void )
     1434static void Init_FPU ( void )
    14351435{
    14361436        mpc_uint16_t  cw;
     
    14571457}
    14581458
     1459static FILE * OpenStream(char * OutputName)
     1460{
     1461        FILE * OutputFile = NULL;
     1462
     1463        /* open bitstream file */
     1464        if      ( 0 == strcmp ( OutputName, "/dev/null") )
     1465                OutputFile = fopen (DEV_NULL, "wb");
     1466        else if ( 0 == strcmp ( OutputName, "-")  ||  0 == strcmp ( OutputName, "/dev/stdout") )
     1467                OutputFile = SETBINARY_OUT (stdout);
     1468        else
     1469                switch ( WriteMode ) {
     1470                        default:
     1471                                stderr_printf ( "\033[33;41;1mERROR\033[0m: Invalid Write mode, internal error\n" );
     1472                                exit(1);
     1473                        case MODE_NEVER_OVERWRITE:
     1474                                OutputFile = fopen ( OutputName, "rb" );
     1475                                if ( OutputFile != NULL ) {
     1476                                        fclose ( OutputFile );
     1477                                        stderr_printf ( "\033[33;41;1mERROR\033[0m: Output file '%s' already exists\n", OutputName );
     1478                                        exit(1);
     1479                                }
     1480                                OutputFile = fopen ( OutputName, "w+b" );
     1481                                break;
     1482                        case MODE_OVERWRITE:
     1483                                OutputFile = fopen ( OutputName, "w+b" );
     1484                                break;
     1485                        case MODE_ASK_FOR_OVERWRITE:
     1486                                OutputFile = fopen ( OutputName, "rb" );
     1487                                if ( OutputFile != NULL ) {
     1488                                        char c;
     1489                                        fclose ( OutputFile );
     1490                                        stderr_printf ( "\nmppenc: Output file '%s' already exists, overwrite (Y/n)? ", OutputName );
     1491                                        c = waitkey ();
     1492                                        if ( c != 'Y'  &&  c != 'y' ) {
     1493                                                stderr_printf ( "No!!!\n\n*** Canceled overwrite ***\n" );
     1494                                                exit(1);
     1495                                        }
     1496                                        stderr_printf ( " YES\n" );
     1497                                }
     1498                                OutputFile = fopen ( OutputName, "w+b" );
     1499                                break;
     1500                }
     1501
     1502        if ( OutputFile == NULL ) {
     1503                stderr_printf ( "\033[33;41;1mERROR\033[0m: Could not create output file '%s'\n", OutputName );
     1504                exit(1);
     1505        }
     1506        return OutputFile;
     1507}
     1508
    14591509static int
    14601510mainloop ( int argc, char** argv )
     
    14681518    unsigned int     CurrentRead      =    0;   // current read Samples per channel
    14691519    unsigned int     N;                         // counter for processed frames
    1470     unsigned int     LastValidSamples =    0;   // number of valid samples for the last frame
    1471     unsigned int     LastValidFrame   =    0;   // overall number of frames
    14721520    char*            InputName        = NULL;   // Name of WAVE file
    14731521    char*            OutputName       = NULL;   // Name of bitstream file
    1474     FILE*            OutputFile       = NULL;   // Filepointer to output file
    14751522    int              Silence          =    0;
    14761523    int              OldSilence       =    0;
    14771524    time_t           T;
    1478     UintMax_t        OldBufferedBits;
    1479     BitstreamPos     bitstreampos;
    14801525    int              TransientL [PART_SHORT];   // Flag of transient detection
    14811526    int              TransientR [PART_SHORT];   // Flag of transient detection
     
    14911536        Init_Psychoakustik (&m);
    14921537        Init_FPU ();
    1493         Init_SV7 (&e);
     1538        Init_SV8 (&e);
    14941539
    14951540    // initialize PCM-data
     
    15561601    }
    15571602
    1558     /* open bitstream file */
    1559     if      ( 0 == strcmp ( OutputName, "/dev/null") ) {
    1560         OutputFile = fopen (DEV_NULL, "wb");
    1561     }
    1562     else if ( 0 == strcmp ( OutputName, "-")  ||  0 == strcmp ( OutputName, "/dev/stdout") ) {
    1563         OutputFile = SETBINARY_OUT (stdout);
    1564     }
    1565     else
    1566         switch ( WriteMode ) {
    1567         default:
    1568             stderr_printf ( "\033[33;41;1mERROR\033[0m: Invalid Write mode, internal error\n" );
    1569             return 1;
    1570         case MODE_NEVER_OVERWRITE:
    1571             OutputFile = fopen ( OutputName, "rb" );
    1572             if ( OutputFile != NULL ) {
    1573                 fclose ( OutputFile );
    1574                 stderr_printf ( "\033[33;41;1mERROR\033[0m: Output file '%s' already exists\n", OutputName );
    1575                 return 1;
    1576             }
    1577             OutputFile = fopen ( OutputName, "w+b" );
    1578             break;
    1579         case MODE_OVERWRITE:
    1580             OutputFile = fopen ( OutputName, "w+b" );
    1581             break;
    1582         case MODE_ASK_FOR_OVERWRITE:
    1583             OutputFile = fopen ( OutputName, "rb" );
    1584             if ( OutputFile != NULL ) {
    1585                 char c;
    1586                 fclose ( OutputFile );
    1587                 stderr_printf ( "\nmppenc: Output file '%s' already exists, overwrite (Y/n)? ", OutputName );
    1588                 c = waitkey ();
    1589                 if ( c != 'Y'  &&  c != 'y' ) {
    1590                     stderr_printf ( "No!!!\n\n*** Canceled overwrite ***\n" );
    1591                     return 1;
    1592                 }
    1593                                 stderr_printf ( " YES\n" );
    1594             }
    1595             OutputFile = fopen ( OutputName, "w+b" );
    1596             break;
    1597         }
    1598 
    1599     if ( OutputFile == NULL ) {
    1600         stderr_printf ( "\033[33;41;1mERROR\033[0m: Could not create output file '%s'\n", OutputName );
    1601         return 1;
    1602     }
    1603 
    1604 #ifndef IO_BUFFERING
    1605     setvbuf ( OutputFile, NULL, _IONBF, 0 );
    1606 #endif
     1603        e.outputFile = OpenStream(OutputName);
    16071604
    16081605    ShowParameters (&m, InputName, OutputName );
     
    16241621
    16251622        e.MS_Channelmode = m.MS_Channelmode;
    1626     e.BufferedBits     = 0;
    1627     LastValidFrame   = (SamplesInWAVE + BLOCK - 1) / BLOCK;
    1628     LastValidSamples = (SamplesInWAVE + BLOCK - 1) - BLOCK * LastValidFrame + 1;
    1629     WriteHeader_SV7 ( &e, m.Max_Band, m.MainQual, m.MS_Channelmode > 0, LastValidFrame, LastValidSamples, m.PNS > 0 ? 0x17 : 0x07, m.SampleFreq );
     1623//     e.BufferedBits     = 0;
     1624        writeMagic(&e);
     1625        WriteHeader_SV8 ( &e, m.Max_Band, m.MS_Channelmode > 0, SamplesInWAVE,
     1626                                           0x08, m.PNS > 0, m.SampleFreq,
     1627                                           Wave.Channels > 2 ? 2 : Wave.Channels);
     1628        writeBlock(&e, "SI", TRUE);
    16301629
    16311630
    16321631    // initialize timer
    1633     ShowProgress (&m, 0, SamplesInWAVE, e.BufferedBits );
     1632    ShowProgress (&m, 0, SamplesInWAVE, e.outputBits );
    16341633    T            = time ( NULL );
    16351634
     
    16531652                                                UintMAX_FP(AllSamplesRead) / m.SampleFreq, UintMAX_FP(SamplesInWAVE) / m.SampleFreq );
    16541653        SamplesInWAVE = AllSamplesRead;
    1655 
    1656         // in the case of a broken wav-header, recalculate the overall frames
    1657         // and the valid samples for the last frame
    1658         LastValidFrame   = (SamplesInWAVE + BLOCK - 1) / BLOCK;
    1659         LastValidSamples = (SamplesInWAVE + BLOCK - 1) - BLOCK * LastValidFrame + 1;
    1660         // fprintf ( stderr, "\nKorrupt WAV file in Frame %d: NEU!: Frames: %u, last valid: %u\n", -1, LastValidFrame, LastValidSamples );
    16611654    }
    16621655
     
    17081701        }
    17091702
    1710         if ( e.Zaehler >= BUFFER_ALMOST_FULL  ||  LowDelay ) {
    1711             FlushBitstream ( OutputFile, e.Buffer, e.Zaehler );
    1712             e.Zaehler = 0;
    1713          }
    1714 
    17151703        OldSilence      = Silence;
    1716         OldBufferedBits = e.BufferedBits;
    1717         GetBitstreamPos    ( &e, &bitstreampos );
    1718         WriteBits          ( &e, 0, 20 );                                                      // Reserve 20 bits for jump-information
    17191704        WriteBitstream_SV7 ( &e, m.Max_Band, Q );                                                // write SV7-Bitstream
    1720         WriteBitsAt        ( &e, (Uint32_t)(e.BufferedBits - OldBufferedBits - 20), 20, bitstreampos );      // Patch 20 bits for jump-information to the right value
    17211705
    17221706        if ( (Int)(time (NULL) - T) >= 0 ) {                            // output
    17231707            T += labs (DisplayUpdateTime);
    1724             ShowProgress (&m, (UintMax_t)(N+1) * BLOCK, SamplesInWAVE, e.BufferedBits );
     1708                        ShowProgress (&m, (UintMax_t)(N+1) * BLOCK, SamplesInWAVE, e.outputBits );
    17251709        }
    17261710
     
    17421726                                                        UintMAX_FP(AllSamplesRead) / m.SampleFreq, UintMAX_FP(SamplesInWAVE) / m.SampleFreq );
    17431727            SamplesInWAVE = AllSamplesRead;
    1744 
    1745             // in the case of broken wav-header, recalculate the overall frames
    1746             // and the valid samples for the last frame
    1747             LastValidFrame   = (SamplesInWAVE + BLOCK - 1) / BLOCK;
    1748             LastValidSamples = (SamplesInWAVE + BLOCK - 1) - BLOCK * LastValidFrame + 1;
    1749             // fprintf ( stderr, "\nKorrupt WAV file in Frame %d: NEU!: Frames: %u, last valid: %u\n", N, LastValidFrame, LastValidSamples );
    1750         }
    1751 
    1752         if ( N == LastValidFrame - 1 ) {
    1753             WriteBits ( &e, LastValidSamples, 11 );
    1754             // fprintf ( stderr, "\nGltige Samples im letzten Frame: %4u   \n", LastValidSamples );
    1755         }
    1756         if ( N >= LastValidFrame ) {
    1757             // fprintf ( stderr, "Zusï¿œzlicher Frame %u (von %u) angehï¿œgt.   \n", N, LastValidFrame );
    1758         }
    1759 
    1760     }
    1761 
    1762     // write the last incomplete word to buffer, so it's written during the next flush
    1763     FinishBitstream(&e);
    1764     ShowProgress (&m, SamplesInWAVE, SamplesInWAVE, e.BufferedBits );
    1765 
    1766     FlushBitstream ( OutputFile, e.Buffer, e.Zaehler );
    1767     e.Zaehler = 0;
    1768 
    1769     UpdateHeader ( OutputFile, LastValidFrame, LastValidSamples );
     1728        }
     1729    }
     1730
     1731    // write the last incomplete block
     1732        writeBlock(&e, "AD", FALSE);
     1733    ShowProgress (&m, SamplesInWAVE, SamplesInWAVE, e.outputBits );
    17701734
    17711735    if(EnableTags)
    1772         FinalizeTags ( OutputFile, APE_Version );
    1773     fclose ( OutputFile );
     1736        FinalizeTags ( e.outputFile, APE_Version );
     1737    fclose ( e.outputFile );
    17741738    fclose ( Wave.fp );
    17751739
Note: See TracChangeset for help on using the changeset viewer.