From patchwork Wed Mar 29 22:59:56 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Paolo Prete X-Patchwork-Id: 3181 Delivered-To: ffmpegpatchwork@gmail.com Received: by 10.103.44.195 with SMTP id s186csp1301013vss; Wed, 29 Mar 2017 16:00:21 -0700 (PDT) X-Received: by 10.28.222.4 with SMTP id v4mr604283wmg.75.1490828421741; Wed, 29 Mar 2017 16:00:21 -0700 (PDT) Return-Path: Received: from ffbox0-bg.mplayerhq.hu (ffbox0-bg.ffmpeg.org. [79.124.17.100]) by mx.google.com with ESMTP id 79si9090227wmv.144.2017.03.29.16.00.21; Wed, 29 Mar 2017 16:00:21 -0700 (PDT) Received-SPF: pass (google.com: domain of ffmpeg-devel-bounces@ffmpeg.org designates 79.124.17.100 as permitted sender) client-ip=79.124.17.100; Authentication-Results: mx.google.com; dkim=neutral (body hash did not verify) header.i=@yahoo.it; spf=pass (google.com: domain of ffmpeg-devel-bounces@ffmpeg.org designates 79.124.17.100 as permitted sender) smtp.mailfrom=ffmpeg-devel-bounces@ffmpeg.org Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id D7312689760; Thu, 30 Mar 2017 01:59:55 +0300 (EEST) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from nm23-vm5.bullet.mail.ir2.yahoo.com (nm23-vm5.bullet.mail.ir2.yahoo.com [212.82.97.20]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id 4FD4B688381 for ; Thu, 30 Mar 2017 01:59:49 +0300 (EEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=yahoo.it; s=s2048; t=1490828411; bh=CfB1SrMq/1mW+qRXn2aB9VJxR//SbS6EOVLDjc7vg7A=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From:Subject; b=F8Ik6rnn81wjwb0wOB62uDvuAzOLDqySg2V4VMxTVvclSVfXHlksqXXAXBUUjwLEj2x86rX4O2pm/pI0QE1q2ORg3U7z1TvRG0zHQ65Y2Y7nXfNFvB0ncAYjodKwrjbDFHKnifdg613uTVj9yZWxZTX3jDsWGh8LKRRW/tWTKidbCNtDBZDbb3EIiIkk9TTMT0HD9bo9BRouEQn0qeFwXfv1Uc/noQ3IzWoJ+ctRfTwNvHxbcenhDhT1FLzxkzdvJG4ueaHPobRLo5pes8bkhPHTCwg3uFiXEXgXh200clt4GNUVZ1viX47FNOPhH5Zw7qKkvzhoNuszHsoqdRtGiQ== Received: from [212.82.98.61] by nm23.bullet.mail.ir2.yahoo.com with NNFMP; 29 Mar 2017 23:00:11 -0000 Received: from [46.228.39.98] by tm14.bullet.mail.ir2.yahoo.com with NNFMP; 29 Mar 2017 23:00:11 -0000 Received: from [127.0.0.1] by smtp135.mail.ir2.yahoo.com with NNFMP; 29 Mar 2017 23:00:11 -0000 X-Yahoo-Newman-Id: 276009.64934.bm@smtp135.mail.ir2.yahoo.com X-Yahoo-Newman-Property: ymail-3 X-YMail-OSG: lmjwbV8VM1kJLaDk.KoaGpUSqSF971O3mtjPBfNYeHEGsZY O6evBfFZi6UayYSEe0PkEwaq0Wps7rzqYJbQx6vusA6gOBOsrPULWd1iHXan 3qn5F2F1EjYOkNKUQIW_TIQ1f6N0ypJbbmLq4ZKySeUghgcmFd0a7G0jqNPO lRj.uPt4p.xWWZhx6YQgxB.XY.0YduRVS7YCPU_egACeKj6xwGOKGq65SKe6 LoqWUzKl3qjA_eMZ9KGt3kGrhKfeALTVU0LM4rA1EeK75TIAhmnmBDZfOyAs uRMBY9PVq0AU24osU7nS.6WkIIk9sq0BgRz1lv4pAk1EGsltMXegD8lT9DCU wYpatRa6j5PgdAyMo.XRIrbxm1na5AqnZj9zA_Y4nwZwrmSzZVYJxm4tVXVr XR0t5pshKnzrqit4vBsGEyCbIA9NESqdS1q9vOMhwTw0TM.K630dVpddbFms 3lWxawAn6_3dWYf16hSKFlr3_TYJpjSwrGM5O6UcpI7nrtN.L6VawUUIr7db tLuxHpRnnKhcMM0yXkGrY5TdGcFc9nIyHFIsOaIkwLz.b9tYayMiWUq.FE1I 7RoXbAm8PpnN8GPo- X-Yahoo-SMTP: F3G2xp.swBB0A1wKHjDaeJZbPU5Pmnfl From: Paolo Prete To: ffmpeg-devel@ffmpeg.org Date: Thu, 30 Mar 2017 00:59:56 +0200 Message-Id: <20170329225956.13930-1-p4olo_prete@yahoo.it> X-Mailer: git-send-email 2.9.3 In-Reply-To: <1898638235.9769159.1490827955583@mail.yahoo.com> References: <1898638235.9769159.1490827955583@mail.yahoo.com> Subject: [FFmpeg-devel] [PATCH] new API usage example (adts-aac encoding from raw audio file) X-BeenThere: ffmpeg-devel@ffmpeg.org X-Mailman-Version: 2.1.20 Precedence: list List-Id: FFmpeg development discussions and patches List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Reply-To: FFmpeg development discussions and patches Cc: Paolo Prete MIME-Version: 1.0 Errors-To: ffmpeg-devel-bounces@ffmpeg.org Sender: "ffmpeg-devel" --- doc/examples/Makefile | 1 + doc/examples/encode_raw_audio_file_to_aac.c | 300 ++++++++++++++++++++++++++++ 2 files changed, 301 insertions(+) create mode 100644 doc/examples/encode_raw_audio_file_to_aac.c diff --git a/doc/examples/Makefile b/doc/examples/Makefile index af38159..81181c7 100644 --- a/doc/examples/Makefile +++ b/doc/examples/Makefile @@ -15,6 +15,7 @@ EXAMPLES= avio_dir_cmd \ avio_reading \ decoding_encoding \ demuxing_decoding \ + encode_raw_audio_file_to_aac \ extract_mvs \ filtering_video \ filtering_audio \ diff --git a/doc/examples/encode_raw_audio_file_to_aac.c b/doc/examples/encode_raw_audio_file_to_aac.c new file mode 100644 index 0000000..546e713 --- /dev/null +++ b/doc/examples/encode_raw_audio_file_to_aac.c @@ -0,0 +1,300 @@ +/* + * Copyright (c) 2017 Paolo Prete (p4olo_prete@yahoo.it) + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL + * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ + +/** + * @file + * API example for adts-aac encoding raw audio files. + * This example reads a raw audio input file, converts it to float-planar format, performs aac encoding and puts the encoded frames into an ADTS container. The encoded stream is written to + * a file named "out.aac" + * The raw input audio file can be created with: ffmpeg -i some_audio_file -f f32le -acodec pcm_f32le -ac 2 -ar 16000 raw_audio_file.raw + * + * @example encode_raw_audio_file_to_aac.c + */ + +#include +#include +#include +#include + +#define ENCODER_BITRATE 64000 +#define SAMPLE_RATE 16000 +#define INPUT_SAMPLE_FMT AV_SAMPLE_FMT_FLT +#define CHANNELS 2 + +static int encoded_pkt_counter = 1; + +static int write_adts_muxed_data(void *opaque, uint8_t *adts_data, int size) +{ + FILE *encoded_audio_file = (FILE *)opaque; + fwrite(adts_data, 1, size, encoded_audio_file); //(f) + return size; +} + +static int mux_aac_packet_to_adts (AVPacket *encoded_audio_packet, AVFormatContext *adts_container_ctx) +{ + int ret_val; + if ((ret_val == av_write_frame(adts_container_ctx, encoded_audio_packet)) < 0) { + av_log(NULL, AV_LOG_ERROR, "Error calling av_write_frame() (error '%s')\n", av_err2str(ret_val)); + } + else { + av_log(NULL, AV_LOG_INFO, "Encoded AAC packet %d, size=%d, pts_time=%s\n", encoded_pkt_counter, encoded_audio_packet->size, av_ts2timestr(encoded_audio_packet->pts, &adts_container_ctx->streams[0]->time_base)); + } + return ret_val; +} + +static int check_if_samplerate_is_supported(AVCodec *audio_codec, int samplerate) +{ + const int *samplerates_list = audio_codec->supported_samplerates; + while (*samplerates_list) { + if (*samplerates_list == samplerate) + return 0; + ++samplerates_list; + } + return 1; +} + +int main(int argc, char **argv) +{ + FILE *input_audio_file = NULL, *encoded_audio_file = NULL; + AVCodec *audio_codec = NULL; + AVCodecContext *audio_encoder_ctx = NULL; + AVFrame *input_audio_frame = NULL, *converted_audio_frame = NULL; + SwrContext *audio_convert_context = NULL; + AVOutputFormat *adts_container = NULL; + AVFormatContext *adts_container_ctx = NULL; + uint8_t *adts_container_buffer = NULL; + size_t adts_container_buffer_size = 4096; + AVIOContext *adts_avio_ctx = NULL; + AVStream *adts_stream = NULL; + AVPacket *encoded_audio_packet = NULL; + int ret_val = 0; + int audio_bytes_to_encode; + int64_t curr_pts; + + if (argc != 2) { + printf("Usage: %s \n", argv[0]); + return 1; + } + + input_audio_file = fopen(argv[1], "rb"); + if (!input_audio_file) { + av_log(NULL, AV_LOG_ERROR, "Could not open input audio file\n"); + return AVERROR_EXIT; + } + + encoded_audio_file = fopen("out.aac", "wb"); + if (!encoded_audio_file) { + av_log(NULL, AV_LOG_ERROR, "Could not open output audio file\n"); + fclose(input_audio_file); + return AVERROR_EXIT; + } + + av_register_all(); + + /** + * Allocate the encoder's context and open the encoder + */ + audio_codec = avcodec_find_encoder(AV_CODEC_ID_AAC); + if (!audio_codec) { + av_log(NULL, AV_LOG_ERROR, "Could not find aac codec\n"); + ret_val = AVERROR_EXIT; + goto end; + } + if ((ret_val = check_if_samplerate_is_supported(audio_codec, SAMPLE_RATE)) != 0) { + av_log(NULL, AV_LOG_ERROR, "Audio codec doesn't support input samplerate %d\n", SAMPLE_RATE); + goto end; + } + audio_encoder_ctx = avcodec_alloc_context3(audio_codec); + if (!audio_codec) { + av_log(NULL, AV_LOG_ERROR, "Could not allocate the encoding context\n"); + ret_val = AVERROR_EXIT; + goto end; + } + audio_encoder_ctx->sample_fmt = AV_SAMPLE_FMT_FLTP; + audio_encoder_ctx->bit_rate = ENCODER_BITRATE; + audio_encoder_ctx->sample_rate = SAMPLE_RATE; + audio_encoder_ctx->channels = CHANNELS; + audio_encoder_ctx->channel_layout = av_get_default_channel_layout(CHANNELS); + audio_encoder_ctx->time_base = (AVRational){1, SAMPLE_RATE}; + audio_encoder_ctx->codec_type = AVMEDIA_TYPE_AUDIO ; + if ((ret_val = avcodec_open2(audio_encoder_ctx, audio_codec, NULL)) < 0) { + av_log(NULL, AV_LOG_ERROR, "Could not open input codec (error '%s')\n", av_err2str(ret_val)); + goto end; + } + + /** + * Allocate an AVFrame which will be filled with the input file's data. + */ + if (!(input_audio_frame = av_frame_alloc())) { + av_log(NULL, AV_LOG_ERROR, "Could not allocate input frame\n"); + ret_val = AVERROR(ENOMEM); + goto end; + } + input_audio_frame->nb_samples = audio_encoder_ctx->frame_size; + input_audio_frame->format = INPUT_SAMPLE_FMT; + input_audio_frame->channels = CHANNELS; + input_audio_frame->sample_rate = SAMPLE_RATE; + input_audio_frame->channel_layout = av_get_default_channel_layout(CHANNELS); + // Allocate the frame's data buffer + if ((ret_val = av_frame_get_buffer(input_audio_frame, 0)) < 0) { + av_log(NULL, AV_LOG_ERROR, "Could not allocate container for input frame samples (error '%s')\n", av_err2str(ret_val)); + ret_val = AVERROR(ENOMEM); + goto end; + } + + /** + * Input data must be converted to float-planar format, which is the format required by the AAC encoder. We allocate a SwrContext and an AVFrame (which will contain the converted samples) + * for this task. The AVFrame will feed the encoding function (avcodec_send_frame()) + */ + audio_convert_context = swr_alloc_set_opts(NULL, av_get_default_channel_layout(CHANNELS), AV_SAMPLE_FMT_FLTP, SAMPLE_RATE, av_get_default_channel_layout(CHANNELS), INPUT_SAMPLE_FMT, SAMPLE_RATE, 0, NULL); + if (!audio_convert_context) { + av_log(NULL, AV_LOG_ERROR, "Could not allocate resample context\n"); + ret_val = AVERROR(ENOMEM); + goto end; + } + if (!(converted_audio_frame = av_frame_alloc())) { + av_log(NULL, AV_LOG_ERROR, "Could not allocate resampled frame\n"); + ret_val = AVERROR(ENOMEM); + goto end; + } + converted_audio_frame->nb_samples = audio_encoder_ctx->frame_size; + converted_audio_frame->format = audio_encoder_ctx->sample_fmt; + converted_audio_frame->channels = audio_encoder_ctx->channels; + converted_audio_frame->channel_layout = audio_encoder_ctx->channel_layout; + converted_audio_frame->sample_rate = SAMPLE_RATE; + if ((ret_val = av_frame_get_buffer(converted_audio_frame, 0)) < 0) { + av_log(NULL, AV_LOG_ERROR, "Could not allocate a buffer for resampled frame samples (error '%s')\n", av_err2str(ret_val)); + goto end; + } + + /** + * Create the ADTS container for the encoded frames + */ + adts_container = av_guess_format("adts", NULL, NULL); + if (!adts_container) { + av_log(NULL, AV_LOG_ERROR, "Could not find adts output format\n"); + ret_val = AVERROR_EXIT; + goto end; + } + if ((ret_val = avformat_alloc_output_context2(&adts_container_ctx, adts_container, "", NULL)) < 0) { + av_log(NULL, AV_LOG_ERROR, "Could not create output context (error '%s')\n", av_err2str(ret_val)); + goto end; + } + if (!(adts_container_buffer = av_malloc(adts_container_buffer_size))) { + av_log(NULL, AV_LOG_ERROR, "Could not allocate a buffer for the I/O output context\n"); + ret_val = AVERROR(ENOMEM); + goto end; + } + // Create an I/O context for the adts container with a write callback (write_adts_muxed_data()), so that muxed data will be accessed through this function. + if (!(adts_avio_ctx = avio_alloc_context(adts_container_buffer, adts_container_buffer_size, 1, encoded_audio_file, NULL , &write_adts_muxed_data, NULL))) { + av_log(NULL, AV_LOG_ERROR, "Could not create I/O output context\n"); + ret_val = AVERROR_EXIT; + goto end; + } + // Link the container's context to the previous I/O context + adts_container_ctx->pb = adts_avio_ctx; + if (!(adts_stream = avformat_new_stream(adts_container_ctx, NULL))) { + av_log(NULL, AV_LOG_ERROR, "Could not create new stream\n"); + ret_val = AVERROR(ENOMEM); + goto end; + } + adts_stream->id = adts_container_ctx->nb_streams-1; + // Copy the encoder's parameters + avcodec_parameters_from_context(adts_stream->codecpar, audio_encoder_ctx); + // Allocate the stream private data and write the stream header + if (avformat_write_header(adts_container_ctx, NULL) < 0) { + av_log(NULL, AV_LOG_ERROR, "avformat_write_header() error\n"); + ret_val = AVERROR_EXIT; + goto end; + } + + /** + * Fill the input frame's data buffer with input file data (a), + * Convert the input frame to float-planar format (b), + * Send the converted frame to the encoder (c), + * Get the encoded packet (d), + * Send the encoded packet to the adts muxer (e). + * Muxed data is caught in write_adts_muxed_data() callback and it is written to the output audio file ( (f) : see above) + */ + encoded_audio_packet = av_packet_alloc(); + while (1) { + audio_bytes_to_encode = fread(input_audio_frame->data[0], 1, input_audio_frame->linesize[0], input_audio_file); //(a) + if (audio_bytes_to_encode != input_audio_frame->linesize[0]) { + break; + } + else { + if ((ret_val = swr_convert_frame(audio_convert_context, converted_audio_frame, (const AVFrame *)input_audio_frame)) != 0) { //(b) + av_log(NULL, AV_LOG_ERROR, "Error resampling input audio frame (error '%s')\n", av_err2str(ret_val)); + goto end; + } + + if ((ret_val = avcodec_send_frame(audio_encoder_ctx, converted_audio_frame)) == 0) //(c) + ret_val = avcodec_receive_packet(audio_encoder_ctx, encoded_audio_packet); //(d) + else { + av_log(NULL, AV_LOG_ERROR, "Error encoding frame (error '%s')\n", av_err2str(ret_val)); + goto end; + } + + if (ret_val == 0) { + curr_pts = converted_audio_frame->nb_samples*(encoded_pkt_counter-1); + encoded_audio_packet->pts = encoded_audio_packet->dts = curr_pts; + if ((ret_val == mux_aac_packet_to_adts(encoded_audio_packet, adts_container_ctx)) < 0) //(e) + goto end; + ++encoded_pkt_counter; + } + else if (ret_val != AVERROR(EAGAIN)) { + av_log(NULL, AV_LOG_ERROR, "Error receiving encoded packet (error '%s')\n", av_err2str(ret_val)); + goto end; + } + } + } + // Flush cached packets + if ((ret_val = avcodec_send_frame(audio_encoder_ctx, NULL)) == 0) + do { + ret_val = avcodec_receive_packet(audio_encoder_ctx, encoded_audio_packet); + if (ret_val == 0) { + curr_pts = converted_audio_frame->nb_samples*(encoded_pkt_counter-1); + encoded_audio_packet->pts = encoded_audio_packet->dts = curr_pts; + if ((ret_val == mux_aac_packet_to_adts(encoded_audio_packet, adts_container_ctx)) < 0) + goto end; + ++encoded_pkt_counter; + } + } while (ret_val == 0); + + av_write_trailer(adts_container_ctx); + +end: + + fclose(input_audio_file); + fclose(encoded_audio_file); + avcodec_free_context(&audio_encoder_ctx); + av_frame_free(&input_audio_frame); + swr_free(&audio_convert_context); + av_frame_free(&converted_audio_frame); + avformat_free_context(adts_container_ctx); + av_freep(&adts_avio_ctx); + av_freep(&adts_container_buffer); + av_packet_free(&encoded_audio_packet); + + return ret_val; + +}