Esempio n. 1
0
/**
 * 
 * @param orig
 * @param dest
 * @return 
 */
int Transcode::transcode(string orig, string dest, TID3Tags *tags)
{
    AVFormatContext *input_format_context = NULL, *output_format_context = NULL;
    AVCodecContext *input_codec_context = NULL, *output_codec_context = NULL;
    SwrContext *resample_context = NULL;
    AVAudioFifo *fifo = NULL;
    int ret = AVERROR_EXIT;
    this->pts = 0;

    if (orig.empty() || dest.empty()) {
        fprintf(stderr, "Files to process not especified\n");
        return ret;
    }

    /* Register all codecs and formats so that they can be used. */
    av_register_all();
    /* Open the input file for reading. */
    if (open_input_file(orig.c_str(), &input_format_context,
                        &input_codec_context))
        goto cleanup;
    /* Open the output file for writing. */
    if (open_output_file(dest.c_str(), input_codec_context,
                         &output_format_context, &output_codec_context))
        goto cleanup;
    
    /**Copy the metadata if exist*/
    copy_metadata(input_format_context, output_format_context, tags);
    
    
    /* Initialize the resampler to be able to convert audio sample formats. */
    if (init_resampler(input_codec_context, output_codec_context,
                       &resample_context))
        goto cleanup;
    /* Initialize the FIFO buffer to store audio samples to be encoded. */
    if (init_fifo(&fifo, output_codec_context))
        goto cleanup;
    /* Write the header of the output file container. */
    if (write_output_file_header(output_format_context))
        goto cleanup;

    /* Loop as long as we have input samples to read or output samples
     * to write; abort as soon as we have neither. */
    while (1) {
        /* Use the encoder's desired frame size for processing. */
        const int output_frame_size = output_codec_context->frame_size;
        int finished                = 0;

        /* Make sure that there is one frame worth of samples in the FIFO
         * buffer so that the encoder can do its work.
         * Since the decoder's and the encoder's frame size may differ, we
         * need to FIFO buffer to store as many frames worth of input samples
         * that they make up at least one frame worth of output samples. */
        while (av_audio_fifo_size(fifo) < output_frame_size) {
            /* Decode one frame worth of audio samples, convert it to the
             * output sample format and put it into the FIFO buffer. */
            if (read_decode_convert_and_store(fifo, input_format_context,
                                              input_codec_context,
                                              output_codec_context,
                                              resample_context, &finished))
                goto cleanup;

            /* If we are at the end of the input file, we continue
             * encoding the remaining audio samples to the output file. */
            if (finished)
                break;
        }

        /* If we have enough samples for the encoder, we encode them.
         * At the end of the file, we pass the remaining samples to
         * the encoder. */
        while (av_audio_fifo_size(fifo) >= output_frame_size ||
               (finished && av_audio_fifo_size(fifo) > 0))
            /* Take one frame worth of audio samples from the FIFO buffer,
             * encode it and write it to the output file. */
            if (load_encode_and_write(fifo, output_format_context,
                                      output_codec_context))
                goto cleanup;

        /* If we are at the end of the input file and have encoded
         * all remaining samples, we can exit this loop and finish. */
        if (finished) {
            int data_written;
            /* Flush the encoder as it may have delayed frames. */
            do {
                if (encode_audio_frame(NULL, output_format_context,
                                       output_codec_context, &data_written))
                    goto cleanup;
            } while (data_written);
            break;
        }
    }

    /* Write the trailer of the output file container. */
    if (write_output_file_trailer(output_format_context))
        goto cleanup;
    ret = 0;

cleanup:
    if (fifo)
        av_audio_fifo_free(fifo);
    swr_free(&resample_context);
    if (output_codec_context)
        avcodec_free_context(&output_codec_context);
    if (output_format_context) {
        avio_closep(&output_format_context->pb);
        avformat_free_context(output_format_context);
    }
    if (input_codec_context)
        avcodec_free_context(&input_codec_context);
    if (input_format_context)
        avformat_close_input(&input_format_context);

    return ret;
}
Esempio n. 2
0
/** Convert an audio file to an AAC file in an MP4 container. */
int compress(int argc, char **argv)
{
    AVFormatContext *input_format_context = NULL, *output_format_context = NULL;
    AVCodecContext *input_codec_context = NULL, *output_codec_context = NULL;
    SwrContext *resample_context = NULL;
    AVAudioFifo *fifo = NULL;
    int ret = AVERROR_EXIT;

    if (argc < 3) {
        fprintf(stderr, "Usage: %s <input file> <output file>\n", argv[0]);
        exit(1);
    }

    const char *input_filename = argv[1];
    const char *output_filename = argv[2];

    /** Register all codecs and formats so that they can be used. */
    av_register_all();
    /** Open the input file for reading. */
    if (open_input_file(input_filename, &input_format_context, &input_codec_context))
        goto cleanup;

    /** Open the output file for writing. */
    if (open_output_file(output_filename, input_codec_context, &output_format_context, &output_codec_context))
        goto cleanup;

    /** Initialize the resampler to be able to convert audio sample formats. */
    if (init_resampler(input_codec_context, output_codec_context, &resample_context))
        goto cleanup;

    /** Initialize the FIFO buffer to store audio samples to be encoded. */
    if (init_fifo(&fifo, output_codec_context))
        goto cleanup;

    /** Write the header of the output file container. */
    if (write_output_file_header(output_format_context))
        goto cleanup;

    /**
     * Loop as long as we have input samples to read or output samples
     * to write; abort as soon as we have neither.
     */
    while (1) {
        /** Use the encoder's desired frame size for processing. */
        const int output_frame_size = output_codec_context->frame_size;
        int finished                = 0;

        /**
         * Make sure that there is one frame worth of samples in the FIFO
         * buffer so that the encoder can do its work.
         * Since the decoder's and the encoder's frame size may differ, we
         * need to FIFO buffer to store as many frames worth of input samples
         * that they make up at least one frame worth of output samples.
         */
        while (av_audio_fifo_size(fifo) < output_frame_size) {
            /**
             * Decode one frame worth of audio samples, convert it to the
             * output sample format and put it into the FIFO buffer.
             */
            if (read_decode_convert_and_store(fifo, input_format_context,
                                              input_codec_context,
                                              output_codec_context,
                                              resample_context, &finished))
                goto cleanup;

            /**
             * If we are at the end of the input file, we continue
             * encoding the remaining audio samples to the output file.
             */
            if (finished)
                break;
        }

        /**
         * If we have enough samples for the encoder, we encode them.
         * At the end of the file, we pass the remaining samples to
         * the encoder.
         */
        while (av_audio_fifo_size(fifo) >= output_frame_size ||
               (finished && av_audio_fifo_size(fifo) > 0))
            /**
             * Take one frame worth of audio samples from the FIFO buffer,
             * encode it and write it to the output file.
             */
            if (load_encode_and_write(fifo, output_format_context, output_codec_context))
                goto cleanup;

        /**
         * If we are at the end of the input file and have encoded
         * all remaining samples, we can exit this loop and finish.
         */
        if (finished) {
            int data_written;
            /** Flush the encoder as it may have delayed frames. */
            do {
                if (encode_audio_frame(NULL, output_format_context, output_codec_context, &data_written))
                    goto cleanup;
            } while (data_written);
            break;
        }
    }

    /** Write the trailer of the output file container. */
    if (write_output_file_trailer(output_format_context))
        goto cleanup;
    ret = 0;

cleanup:
    if (fifo)
        av_audio_fifo_free(fifo);
    swr_free(&resample_context);
    if (output_codec_context)
        avcodec_close(output_codec_context);
    if (output_format_context) {
        avio_closep(&output_format_context->pb);
        avformat_free_context(output_format_context);
    }
    if (input_codec_context)
        avcodec_close(input_codec_context);
    if (input_format_context)
        avformat_close_input(&input_format_context);

    return ret;
}
Esempio n. 3
0
	// Data output wrapper function
	void data(){

		// check calling of routine if error checking is activated
		if(err::check==true){std::cout << "vout::data has been called" << std::endl;}

		// Calculate MPI Timings since last data output
		#ifdef MPICF
		if(vmpi::DetailedMPITiming){

			// Calculate Average times
			MPI_Reduce (&vmpi::TotalComputeTime,&vmpi::AverageComputeTime,1,MPI_DOUBLE,MPI_SUM,0,MPI_COMM_WORLD);
			MPI_Reduce (&vmpi::TotalWaitTime,&vmpi::AverageWaitTime,1,MPI_DOUBLE,MPI_SUM,0,MPI_COMM_WORLD);
			vmpi::AverageComputeTime/=double(vmpi::num_processors);
			vmpi::AverageWaitTime/=double(vmpi::num_processors);

			// Calculate Maximum times
			MPI_Reduce (&vmpi::TotalComputeTime,&vmpi::MaximumComputeTime,1,MPI_DOUBLE,MPI_MAX,0,MPI_COMM_WORLD);
			MPI_Reduce (&vmpi::TotalWaitTime,&vmpi::MaximumWaitTime,1,MPI_DOUBLE,MPI_MAX,0,MPI_COMM_WORLD);

			// Save times for timing matrix
			vmpi::ComputeTimeArray.push_back(vmpi::TotalComputeTime);
			vmpi::WaitTimeArray.push_back(vmpi::TotalWaitTime);

			// reset until next data output
			vmpi::TotalComputeTime=0.0;
			vmpi::TotalWaitTime=0.0;
		}
		#endif

      // check for open ofstream on root process only
      if(vmpi::my_rank == 0){
         if(!zmag.is_open()){
            // check for checkpoint continue and append data
            if(sim::load_checkpoint_flag && sim::load_checkpoint_continue_flag) zmag.open("output",std::ofstream::app);
            // otherwise overwrite file
            else{
               zmag.open("output",std::ofstream::trunc);
               // write file header information
               write_output_file_header(zmag, file_output_list);
            }
         }
      }

		// Only output 1/output_rate time steps
		if(sim::time%vout::output_rate==0){

		// Output data to output
		if(vmpi::my_rank==0){

         // For gpu acceleration get statistics from device
         if(gpu::acceleration) gpu::stats::get();

			for(unsigned int item=0;item<file_output_list.size();item++){
				switch(file_output_list[item]){
					case 0:
						vout::time(zmag);
						break;
					case 1:
						vout::real_time(zmag);
						break;
					case 2:
						vout::temperature(zmag);
						break;
					case 3:
						vout::Happ(zmag);
						break;
					case 4:
						vout::Hvec(zmag);
						break;
					case 5:
						vout::mvec(zmag);
						break;
					case 6:
						vout::magm(zmag);
						break;
					case 7:
						vout::mean_magm(zmag);
						break;
					case 8:
						vout::mat_mvec(zmag);
						break;
					case 9:
						vout::mat_mean_magm(zmag);
						break;
					case 12:
						vout::mdoth(zmag);
						break;
					case 14:
						vout::systorque(zmag);
						break;
					case 15:
						vout::mean_systorque(zmag);
						break;
					case 16:
						vout::constraint_phi(zmag);
						break;
					case 17:
						vout::constraint_theta(zmag);
						break;
					case 18:
						vout::material_constraint_phi(zmag);
						break;
					case 19:
						vout::material_constraint_theta(zmag);
						break;
					case 20:
						vout::material_mean_systorque(zmag);
						break;
					case 21:
						vout::mean_system_susceptibility(zmag);
						break;
					case 22:
						vout::phonon_temperature(zmag);
						break;
					case 23:
						vout::material_temperature(zmag);
						break;
					case 24:
						vout::material_applied_field_strength(zmag);
						break;
					case 25:
						vout::material_fmr_field_strength(zmag);
						break;
					case 26:
						vout::mat_mdoth(zmag);
						break;
					case 27:
						vout::total_energy(zmag);
						break;
					case 28:
						vout::mean_total_energy(zmag);
						break;
					case 29:
						vout::total_anisotropy_energy(zmag);
						break;
					case 30:
						vout::mean_total_anisotropy_energy(zmag);
						break;
					case 31:
						vout::total_cubic_anisotropy_energy(zmag);
						break;
					case 32:
						vout::mean_total_cubic_anisotropy_energy(zmag);
						break;
					case 33:
						vout::total_surface_anisotropy_energy(zmag);
						break;
					case 34:
						vout::mean_total_surface_anisotropy_energy(zmag);
						break;
					case 35:
						vout::total_exchange_energy(zmag);
						break;
					case 36:
						vout::mean_total_exchange_energy(zmag);
						break;
					case 37:
						vout::total_applied_field_energy(zmag);
						break;
					case 38:
						vout::mean_total_applied_field_energy(zmag);
						break;
					case 39:
						vout::total_magnetostatic_energy(zmag);
						break;
					case 40:
						vout::mean_total_magnetostatic_energy(zmag);
						break;
					case 41:
						vout::total_so_anisotropy_energy(zmag);
						break;
					case 42:
						vout::mean_total_so_anisotropy_energy(zmag);
						break;
					case 43:
						vout::height_mvec(zmag);
						break;
					case 44:
						vout::material_height_mvec(zmag);
						break;
					case 45:
						vout::height_mvec_actual(zmag);
						break;
					case 46:
						vout::material_height_mvec_actual(zmag);
						break;
					case 47:
						vout::fmr_field_strength(zmag);
						break;
               case 48:
						vout::mean_mvec(zmag);
						break;
               case 49:
						vout::mat_mean_mvec(zmag);
						break;
               case 50:
						vout::mean_material_susceptibility(zmag);
						break;
					case 51:
						vout::mean_height_magnetisation_length(zmag);
						break;
					case 52:
						vout::mean_height_magnetisation(zmag);
						break;
					case 60:
						vout::MPITimings(zmag);
						break;
				}
			}
			// Carriage return
			if(file_output_list.size()>0) zmag << std::endl;

			} // end of code for rank 0 only
		} // end of if statement for output rate

		// Output data to cout
		if(vmpi::my_rank==0){
			if(sim::time%vout::output_rate==0){ // needs to be altered to separate variable at some point

            // For gpu acceleration get statistics from device (repeated here so additional performance cost for doing this)
            if(gpu::acceleration) gpu::stats::get();

				for(unsigned int item=0;item<screen_output_list.size();item++){
				switch(screen_output_list[item]){
					case 0:
						vout::time(std::cout);
						break;
					case 1:
						vout::real_time(std::cout);
						break;
					case 2:
						vout::temperature(std::cout);
						break;
					case 3:
						vout::Happ(std::cout);
						break;
					case 4:
						vout::Hvec(std::cout);
						break;
					case 5:
						vout::mvec(std::cout);
						break;
					case 6:
						vout::magm(std::cout);
						break;
					case 7:
						vout::mean_magm(std::cout);
						break;
					case 8:
						vout::mat_mvec(std::cout);
						break;
					case 9:
						vout::mat_mean_magm(std::cout);
						break;
					case 12:
						vout::mdoth(std::cout);
						break;
					case 14:
						vout::systorque(std::cout);
						break;
					case 15:
						vout::mean_systorque(std::cout);
						break;
					case 16:
						vout::constraint_phi(std::cout);
						break;
					case 17:
						vout::constraint_theta(std::cout);
						break;
					case 18:
						vout::material_constraint_phi(std::cout);
						break;
					case 19:
						vout::material_constraint_theta(std::cout);
						break;
					case 20:
						vout::material_mean_systorque(std::cout);
						break;
					case 21:
						vout::mean_system_susceptibility(std::cout);
						break;
					case 22:
						vout::phonon_temperature(std::cout);
						break;
					case 23:
						vout::material_temperature(std::cout);
						break;
					case 24:
						vout::material_applied_field_strength(std::cout);
						break;
					case 25:
						vout::material_fmr_field_strength(std::cout);
						break;
					case 26:
						vout::mat_mdoth(std::cout);
						break;
					case 27:
						vout::total_energy(std::cout);
						break;
					case 28:
						vout::mean_total_energy(std::cout);
						break;
					case 29:
						vout::total_anisotropy_energy(std::cout);
						break;
					case 30:
						vout::mean_total_anisotropy_energy(std::cout);
						break;
					case 31:
						vout::total_cubic_anisotropy_energy(std::cout);
						break;
					case 32:
						vout::mean_total_cubic_anisotropy_energy(std::cout);
						break;
					case 33:
						vout::total_surface_anisotropy_energy(std::cout);
						break;
					case 34:
						vout::mean_total_surface_anisotropy_energy(std::cout);
						break;
					case 35:
						vout::total_exchange_energy(std::cout);
						break;
					case 36:
						vout::mean_total_exchange_energy(std::cout);
						break;
					case 37:
						vout::total_applied_field_energy(std::cout);
						break;
					case 38:
						vout::mean_total_applied_field_energy(std::cout);
						break;
					case 39:
						vout::total_magnetostatic_energy(std::cout);
						break;
					case 40:
						vout::mean_total_magnetostatic_energy(std::cout);
						break;
					case 41:
						vout::total_so_anisotropy_energy(std::cout);
						break;
					case 42:
						vout::mean_total_so_anisotropy_energy(std::cout);
						break;
					case 47:
						vout::fmr_field_strength(std::cout);
						break;
               case 48:
						vout::mean_mvec(std::cout);
						break;
               case 49:
						vout::mat_mean_mvec(std::cout);
						break;
               case 50:
						vout::mean_material_susceptibility(std::cout);
						break;
					case 60:
						vout::MPITimings(std::cout);
						break;
				}
			}

			// Carriage return
			if(screen_output_list.size()>0) std::cout << std::endl;
			}

		} // End of if statement to output data to screen

		if(sim::time%vout::output_grain_rate==0){

		// calculate grain magnetisations
		grains::mag();

		// Output data to zgrain
		if(vmpi::my_rank==0){

			// check for open ofstream
			if(vout::grain_output_list.size() > 0 && !zgrain.is_open()){
				// check for checkpoint continue and append data
				if(sim::load_checkpoint_flag && sim::load_checkpoint_continue_flag) zgrain.open("grain",std::ofstream::app);
				// otherwise overwrite file
				else zgrain.open("grain",std::ofstream::trunc);
			}

			for(unsigned int item=0;item<vout::grain_output_list.size();item++){
			switch(vout::grain_output_list[item]){
				case 0:
					vout::time(zgrain);
					break;
				case 1:
					vout::real_time(zgrain);
					break;
				case 2:
					vout::temperature(zgrain);
					break;
				case 3:
					vout::Happ(zgrain);
					break;
				case 4:
					vout::Hvec(zgrain);
					break;
				case 10:
					vout::grain_mvec(zgrain);
					break;
				case 11:
					vout::grain_magm(zgrain);
					break;
				case 13:
					vout::grain_mat_mvec(zgrain);
					break;
				case 22:
					vout::phonon_temperature(zgrain);
					break;
			}
		}

		// Carriage return
		if(vout::grain_output_list.size()>0) zgrain << std::endl;
		}
		}

		// Output configuration files to disk
		config::output();

		// optionally save checkpoint file
		if(sim::save_checkpoint_flag==true && sim::save_checkpoint_continuous_flag==true && sim::time%sim::save_checkpoint_rate==0) save_checkpoint();

	} // end of data