Example #1
0
/* set up the encoder state */
static gboolean
gst_two_lame_setup (GstTwoLame * twolame)
{

#define CHECK_ERROR(command) G_STMT_START {\
  if ((command) < 0) { \
    GST_ERROR_OBJECT (twolame, "setup failed: " G_STRINGIFY (command)); \
    return FALSE; \
  } \
}G_STMT_END

    int retval;
    GstCaps *allowed_caps;

    GST_DEBUG_OBJECT (twolame, "starting setup");

    /* check if we're already setup; if we are, we might want to check
     * if this initialization is compatible with the previous one */
    /* FIXME: do this */
    if (twolame->setup) {
        GST_WARNING_OBJECT (twolame, "already setup");
        twolame->setup = FALSE;
    }

    twolame->glopts = twolame_init ();

    if (twolame->glopts == NULL)
        return FALSE;

    /* copy the parameters over */
    twolame_set_in_samplerate (twolame->glopts, twolame->samplerate);

    /* let twolame choose default samplerate unless outgoing sample rate is fixed */
    allowed_caps = gst_pad_get_allowed_caps (twolame->srcpad);

    if (allowed_caps != NULL) {
        GstStructure *structure;
        gint samplerate;

        structure = gst_caps_get_structure (allowed_caps, 0);

        if (gst_structure_get_int (structure, "rate", &samplerate)) {
            GST_DEBUG_OBJECT (twolame,
                              "Setting sample rate to %d as fixed in src caps", samplerate);
            twolame_set_out_samplerate (twolame->glopts, samplerate);
        } else {
            GST_DEBUG_OBJECT (twolame, "Letting twolame choose sample rate");
            twolame_set_out_samplerate (twolame->glopts, 0);
        }
        gst_caps_unref (allowed_caps);
        allowed_caps = NULL;
    } else {
        GST_DEBUG_OBJECT (twolame,
                          "No peer yet, letting twolame choose sample rate");
        twolame_set_out_samplerate (twolame->glopts, 0);
    }

    /* force mono encoding if we only have one channel */
    if (twolame->num_channels == 1)
        twolame->mode = 3;

    /* Fix bitrates and MPEG version */

    CHECK_ERROR (twolame_set_num_channels (twolame->glopts,
                                           twolame->num_channels));

    CHECK_ERROR (twolame_set_mode (twolame->glopts, twolame->mode));
    CHECK_ERROR (twolame_set_psymodel (twolame->glopts, twolame->psymodel));
    CHECK_AND_FIXUP_BITRATE (twolame, "bitrate", twolame->bitrate);
    CHECK_ERROR (twolame_set_bitrate (twolame->glopts, twolame->bitrate));
    CHECK_ERROR (twolame_set_padding (twolame->glopts, twolame->padding));
    CHECK_ERROR (twolame_set_energy_levels (twolame->glopts,
                                            twolame->energy_level_extension));
    CHECK_ERROR (twolame_set_emphasis (twolame->glopts, twolame->emphasis));
    CHECK_ERROR (twolame_set_error_protection (twolame->glopts,
                 twolame->error_protection));
    CHECK_ERROR (twolame_set_copyright (twolame->glopts, twolame->copyright));
    CHECK_ERROR (twolame_set_original (twolame->glopts, twolame->original));
    CHECK_ERROR (twolame_set_VBR (twolame->glopts, twolame->vbr));
    CHECK_ERROR (twolame_set_VBR_level (twolame->glopts, twolame->vbr_level));
    CHECK_ERROR (twolame_set_ATH_level (twolame->glopts, twolame->ath_level));
    CHECK_AND_FIXUP_BITRATE (twolame, "vbr-max-bitrate",
                             twolame->vbr_max_bitrate);
    CHECK_ERROR (twolame_set_VBR_max_bitrate_kbps (twolame->glopts,
                 twolame->vbr_max_bitrate));
    CHECK_ERROR (twolame_set_quick_mode (twolame->glopts, twolame->quick_mode));
    CHECK_ERROR (twolame_set_quick_count (twolame->glopts,
                                          twolame->quick_mode_count));

    /* initialize the twolame encoder */
    if ((retval = twolame_init_params (twolame->glopts)) >= 0) {
        twolame->setup = TRUE;
        /* FIXME: it would be nice to print out the mode here */
        GST_INFO ("twolame encoder setup (%d kbit/s, %d Hz, %d channels)",
                  twolame->bitrate, twolame->samplerate, twolame->num_channels);
    } else {
        GST_ERROR_OBJECT (twolame, "twolame_init_params returned %d", retval);
    }

    GST_DEBUG_OBJECT (twolame, "done with setup");

    return twolame->setup;
#undef CHECK_ERROR
}
Example #2
0
/* 
  parse_args() 
  Parse the command line arguments
*/
void
parse_args(int argc, char **argv, twolame_options * encopts )
{
    int ch=0;

    // process args
    struct option longopts[] = {
    
        // Input
        { "raw-input",      no_argument,            NULL,       'r' },
        { "byte-swap",      no_argument,            NULL,       'x' },
        { "samplerate",     required_argument,      NULL,       's' },
        { "channels",       required_argument,      NULL,       'N' },
        { "swap-channels",  no_argument,            NULL,       'g' },
        { "scale",          required_argument,      NULL,       1000 },
        { "scale-l",        required_argument,      NULL,       1001 },
        { "scale-r",        required_argument,      NULL,       1002 },
        
        // Output
        { "mode",           required_argument,      NULL,       'm' },
        { "downmix",        no_argument,            NULL,       'a' },
        { "bitrate",        required_argument,      NULL,       'b' },
        { "psyc-mode",      required_argument,      NULL,       'P' },
        { "vbr",            no_argument,  		    NULL,       'v' },
        { "vbr-level",      required_argument,      NULL,       'V' },
        { "max-bitrate",    required_argument,      NULL,       'B' },
        { "ath",            required_argument,      NULL,       'l' },
        { "quick",          required_argument,      NULL,       'q' },
        { "single-frame",   no_argument,            NULL,       'S' },
        
        // Misc
        { "copyright",      no_argument,            NULL,       'c' },
        { "non-original",   no_argument,            NULL,       'o' },
        { "original",   	no_argument,            NULL,       1003 },
        { "protect", 		no_argument,            NULL,       'p' },
        { "padding",        no_argument,            NULL,       'd' },
        { "reserve-bits",   required_argument,      NULL,       'R' },
        { "deemphasis",     required_argument,      NULL,       'e' },
        { "energy",         no_argument,            NULL,       'E' },
        
        // Verbosity
        { "talkativity",    required_argument,      NULL,       't' },
        { "quiet",          no_argument,            NULL,       1004 },
        { "brief",          no_argument,            NULL,       1005 },
        { "verbose",        no_argument,            NULL,       1006 },
        { "help",           no_argument,            NULL,       'h' },
        
        { NULL,             0,                      NULL,       0 }
    };

    
    // Create a short options structure from the long one
    char* shortopts = build_shortopt_string( longopts );
    //printf("shortopts: %s\n", shortopts);
    
    
    while( (ch = getopt_long( argc, argv,  shortopts, longopts, NULL )) != -1) 
    {
        switch(ch) {
        
        // Input
            case 'r':
                sfinfo.format = SF_FORMAT_RAW | SF_FORMAT_PCM_16;
                break;

            case 'x':
                byteswap = TRUE;
                break;

            case 's':
                twolame_set_out_samplerate(encopts, atoi(optarg));
                sfinfo.samplerate = atoi(optarg);
                break;

            case 'N':
                sfinfo.channels = atoi(optarg);
                break;
                
            case 'g':
                channelswap = TRUE;
                break;
                
            case 1000:  // --scale
                twolame_set_scale( encopts, atof(optarg) );
                break;

            case 1001:  // --scale-l
                twolame_set_scale_left( encopts, atof(optarg) );
                break;

            case 1002:  // --scale-r
                twolame_set_scale_right( encopts, atof(optarg) );
                break;



        // Output
            case 'm':
                if (*optarg == 's') {
                    twolame_set_mode(encopts, TWOLAME_STEREO);
                } else if (*optarg == 'd') {
                    twolame_set_mode(encopts, TWOLAME_DUAL_CHANNEL);
                } else if (*optarg == 'j') {
                    twolame_set_mode(encopts, TWOLAME_JOINT_STEREO);
                } else if (*optarg == 'm') {
                    twolame_set_mode(encopts, TWOLAME_MONO);
                } else if (*optarg == 'a') {
                    twolame_set_mode(encopts, TWOLAME_AUTO_MODE);
                } else {
                    fprintf(stderr, "Error: mode must be a/s/d/j/m not '%s'\n\n", optarg);
                    usage_long();
                }
                break;

            case 'a':	// downmix
                twolame_set_mode(encopts, TWOLAME_MONO);
                 break;
                
            case 'b':
                twolame_set_bitrate(encopts, atoi(optarg));
                break;

            case 'P':
                twolame_set_psymodel(encopts, atoi(optarg));
                break;
                
            case 'v':
                twolame_set_VBR(encopts, TRUE);
                break;

            case 'V':
                twolame_set_VBR(encopts, TRUE);
                twolame_set_VBR_level(encopts, atof(optarg));
                break;

            case 'B':
                twolame_set_VBR_max_bitrate_kbps(encopts, atoi(optarg));
                break;

            case 'l':
                twolame_set_ATH_level(encopts, atof(optarg));
                break;
                
            case 'q':
                twolame_set_quick_mode(encopts, TRUE);
                twolame_set_quick_count(encopts, atoi(optarg));
                break;

            case 'S':
                single_frame_mode = TRUE;
                break;
                
                
    // Miscellaneous            
            case 'c':
                twolame_set_copyright(encopts, TRUE);
                break;
            case 'o':	// --non-original
                twolame_set_original(encopts, FALSE);
                break;
            case 1003:  // --original
                twolame_set_original(encopts, TRUE);
                break;
            case 'p':
                twolame_set_error_protection(encopts, TRUE);
                break;
            case 'd':
                twolame_set_padding(encopts, TWOLAME_PAD_ALL);
                break;
            case 'R':
                twolame_set_num_ancillary_bits(encopts, atoi(optarg));
                break;
            case 'e':
                if (*optarg == 'n')
                    twolame_set_emphasis(encopts, TWOLAME_EMPHASIS_N);
                else if (*optarg == '5')
                    twolame_set_emphasis(encopts, TWOLAME_EMPHASIS_5);
                else if (*optarg == 'c')
                    twolame_set_emphasis(encopts, TWOLAME_EMPHASIS_C);
                else {
                    fprintf(stderr, "Error: emphasis must be n/5/c not '%s'\n\n", optarg);
                    usage_long();
                }
                break;
            case 'E':
                twolame_set_energy_levels(encopts, TRUE);
                break;
        

    // Verbosity
            case 't':
                twolame_set_verbosity(encopts, atoi(optarg));
                break;

            case 1004:  // --quiet
                twolame_set_verbosity(encopts, 0);
                break;
                
            case 1005: // --brief
                twolame_set_verbosity(encopts, 1);
                break;
                
            case 1006: // --verbose
                twolame_set_verbosity(encopts, 4);
                break;
                
            case 'h':
                usage_long();
            break;
        
            default:
                usage_short();
            break;
        }
    }
    

    // Look for the input and output file names
    argc -= optind;
    argv += optind;
    while( argc ) {
        if (inputfilename[0] == '\0')
            strncpy(inputfilename, *argv, MAX_NAME_SIZE);
        else if (outputfilename[0] == '\0')
            strncpy(outputfilename, *argv, MAX_NAME_SIZE);
        else {
            fprintf(stderr, "excess argument: %s\n", *argv);
            usage_short();
        }
    
        argv++;
        argc--;
    }
    
    
    // Check that we now have input and output file names ok
    if ( inputfilename[0] == '\0') {
        fprintf(stderr, "Missing input filename.\n");
        usage_short();
    }
    if ( outputfilename[0] == '\0' && strcmp(inputfilename, "-")!=0 ) {
        // Create output filename from the inputfilename 
        // and change the suffix
        new_extension( inputfilename, OUTPUT_SUFFIX, outputfilename );
    }
    if ( outputfilename[0] == '\0') {
        fprintf(stderr, "Missing output filename.\n");
        usage_short();
    }
        
}