/** * Load one audio frame from the FIFO buffer, encode and write it to the * output file. * @param fifo Buffer used for temporary storage * @param output_format_context Format context of the output file * @param output_codec_context Codec context of the output file * @return Error code (0 if successful) */ int Transcode::load_encode_and_write(AVAudioFifo *fifo, AVFormatContext *output_format_context, AVCodecContext *output_codec_context) { /* Temporary storage of the output samples of the frame written to the file. */ AVFrame *output_frame; /* Use the maximum number of possible samples per frame. * If there is less than the maximum possible frame size in the FIFO * buffer use this number. Otherwise, use the maximum possible frame size. */ const int frame_size = FFMIN(av_audio_fifo_size(fifo), output_codec_context->frame_size); int data_written; /* Initialize temporary storage for one output frame. */ if (init_output_frame(&output_frame, output_codec_context, frame_size)) return AVERROR_EXIT; /* Read as many samples from the FIFO buffer as required to fill the frame. * The samples are stored in the frame temporarily. */ if (av_audio_fifo_read(fifo, (void **)output_frame->data, frame_size) < frame_size) { fprintf(stderr, "Could not read data from FIFO\n"); av_frame_free(&output_frame); return AVERROR_EXIT; } /* Encode one frame worth of audio samples. */ if (encode_audio_frame(output_frame, output_format_context, output_codec_context, &data_written)) { av_frame_free(&output_frame); return AVERROR_EXIT; } av_frame_free(&output_frame); return 0; }
/** * Load one audio frame from the FIFO buffer, encode and write it to the * output file. */ int AudioDecoder::load_encode(AVPacket& output_packet) { /** Temporary storage of the output samples of the frame written to the file. */ AVFrame *output_frame; /** * Use the maximum number of possible samples per frame. * If there is less than the maximum possible frame size in the FIFO * buffer use this number. Otherwise, use the maximum possible frame size */ const int frame_size = FFMIN(av_audio_fifo_size(fifo), output_codec_context->frame_size); /** Initialize temporary storage for one output frame. */ if (init_output_frame(&output_frame, output_codec_context, frame_size)) { ELOG_WARN(" init_output_frame failed!! frame_size=%d", frame_size); return 0; } /** * Read as many samples from the FIFO buffer as required to fill the frame. * The samples are stored in the frame temporarily. */ if (av_audio_fifo_read(fifo, (void **)output_frame->data, frame_size) < frame_size) { ELOG_WARN("Could not read data from FIFO\n"); av_frame_free(&output_frame); return 0; } ELOG_DEBUG("fifo read %d, now left %d", frame_size, av_audio_fifo_size(fifo)); /** Encode one frame worth of audio samples. */ int pktlen = encode_audio_frame(output_frame, output_packet); if (pktlen <= 0) { ELOG_WARN("Failed to encode_audio_frame!!"); } av_frame_free(&output_frame); return pktlen; }