Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * File: main.c
- * Author: antonello
- *
- * Created on 29 gennaio 2013, 14.26
- */
- #include <stdio.h>
- #include <stdlib.h>
- #include <libavutil/imgutils.h>
- #include <libavutil/samplefmt.h>
- #include <libavutil/timestamp.h>
- #include <libavformat/avformat.h>
- #include <libavcodec/avcodec.h>
- #include <libavutil/mem.h>
- #include <libavutil/channel_layout.h>
- #define INBUF_SIZE 4096
- #define AUDIO_INBUF_SIZE 20480
- #define AUDIO_REFILL_THRESH 4096
- static FILE* file;
- int alloc_samples_array_and_data(uint8_t ***data, int *linesize, int nb_channels,
- int nb_samples, enum AVSampleFormat sample_fmt, int align) {
- int nb_planes = av_sample_fmt_is_planar(sample_fmt) ? nb_channels : 1;
- *data = av_malloc(sizeof (*data) * nb_planes);
- if (!*data)
- return AVERROR(ENOMEM);
- return av_samples_alloc(*data, linesize, nb_channels,
- nb_samples, sample_fmt, align);
- }
- static int open_codec_context(int *stream_idx,
- AVFormatContext *fmt_ctx, enum AVMediaType type) {
- int ret;
- AVStream *st;
- AVCodecContext *dec_ctx = NULL;
- AVCodec *dec = NULL;
- ret = av_find_best_stream(fmt_ctx, type, -1, -1, NULL, 0);
- if (ret < 0) {
- fprintf(stderr, "Could not find %s stream'\n",
- av_get_media_type_string(type));
- return ret;
- } else {
- *stream_idx = ret;
- st = fmt_ctx->streams[*stream_idx];
- /* find decoder for the stream */
- dec_ctx = st->codec;
- dec = avcodec_find_decoder(dec_ctx->codec_id);
- if (!dec) {
- fprintf(stderr, "Failed to find %s codec\n",
- av_get_media_type_string(type));
- return ret;
- }
- if ((ret = avcodec_open2(dec_ctx, dec, NULL)) < 0) {
- fprintf(stderr, "Failed to open %s codec\n",
- av_get_media_type_string(type));
- return ret;
- }
- }
- return 0;
- }
- static int decode_packet(int *got_frame, int audio_stream_idx, AVPacket *pkt, AVCodecContext *audio_dec_ctx, AVFrame *frame) {
- int ret = 0;
- int audio_dst_bufsize = 0;
- if (pkt->stream_index == audio_stream_idx) {
- /* decode audio frame */
- ret = avcodec_decode_audio4(audio_dec_ctx, frame, got_frame, pkt);
- if (ret < 0) {
- fprintf(stderr, "Error decoding audio frame\n");
- return ret;
- }
- if (*got_frame) {
- // audio_dst_bufsize =
- // av_samples_get_buffer_size(NULL, frame->channels,
- // frame->nb_samples, frame->format, 1);
- // fwrite(frame->data[0],1,audio_dst_bufsize,file);
- }
- }
- return ret;
- }
- static int convertSampleFormat(AVFrame *src_frame1, AVCodecContext *src_ctx ,uint8_t **dst_data1 , int64_t dst_ch_layout1, int dst_nb_channels1, enum AVSampleFormat dst_sample_fmt1, int dst_rate1) {
- int dst_nb_channels = 0;
- int dst_linesize;
- int dst_nb_samples, max_dst_nb_samples;
- int dst_bufsize;
- struct SwrContext *swr_ctx;
- int ret;
- uint8_t **dst_data=NULL;
- swr_ctx= swr_alloc();
- swr_ctx = swr_alloc_set_opts(swr_ctx, dst_ch_layout1, dst_sample_fmt1, dst_rate1, AV_CH_LAYOUT_MONO, src_frame1->format, src_frame1->sample_rate, 0, 0);
- if ((ret = swr_init(swr_ctx)) < 0) {
- fprintf(stderr, "Failed to initialize the resampling context\n");
- }
- max_dst_nb_samples = dst_nb_samples =
- av_rescale_rnd(src_frame1->sample_rate, dst_rate1, dst_rate1, AV_ROUND_UP);
- /* buffer is going to be directly written to a rawaudio file, no alignment */
- dst_nb_channels = av_get_channel_layout_nb_channels(dst_ch_layout1);
- ret = alloc_samples_array_and_data(&dst_data, &dst_linesize, dst_nb_channels1,
- dst_nb_samples, dst_sample_fmt1, 0);
- if (ret < 0) {
- fprintf(stderr, "Could not allocate destination samples\n");
- }
- /* convert to destination format */
- ret = swr_convert(swr_ctx, dst_data, dst_nb_samples, (const uint8_t **) src_frame1->data, src_frame1->nb_samples);
- if (ret < 0) {
- fprintf(stderr, "Error while converting\n");
- }
- dst_bufsize = av_samples_get_buffer_size(&dst_linesize, dst_nb_channels1,
- ret, dst_sample_fmt1, 1);
- fwrite(dst_data[0], 1, dst_bufsize, file);
- *dst_data1=dst_data;
- return dst_bufsize;
- }
- int main(int argc, char** argv) {
- AVFormatContext* formatcontext = avformat_alloc_context();
- AVCodecContext *audio_dec_ctx = NULL;
- AVStream *audio_stream = NULL;
- AVCodec *audio_codec=NULL;
- uint8_t **transform_dst_data = NULL;
- AVPacket avpkt;
- AVFrame *decoded_frame = NULL, *transform_frame = NULL;
- int audio_stream_idx = 0;
- int got_frame=0;
- FILE *input_file=NULL;
- uint8_t inbuf[AUDIO_INBUF_SIZE+ FF_INPUT_BUFFER_PADDING_SIZE];
- uint8_t header[10];
- int len=0;
- printf("read %s",argv[1]);
- input_file=fopen(argv[1], "rb");
- if (!input_file) {
- fprintf(stderr, "Could not open file\n");
- exit(1);
- }
- fread(header, 1, 9,input_file);
- //library initialize
- av_register_all();
- //network componet init
- av_init_packet(&avpkt);
- /* find the AAC audio decoder */
- audio_codec = avcodec_find_decoder(AV_CODEC_ID_AAC);
- if (!audio_codec) {
- fprintf(stderr, "Codec not found\n");
- exit(1);
- }
- audio_dec_ctx = avcodec_alloc_context3(audio_codec);
- if (!audio_dec_ctx) {
- fprintf(stderr, "Could not allocate audio codec context\n");
- exit(1);
- }
- /* open it */
- if (avcodec_open2(audio_dec_ctx, audio_codec, NULL) < 0) {
- fprintf(stderr, "Could not open codec\n");
- exit(1);
- }
- audio_dec_ctx->channels=1;
- audio_dec_ctx->sample_rate=8000;
- audio_dec_ctx->bit_rate=12000;
- audio_dec_ctx->sample_fmt=AV_SAMPLE_FMT_S16;
- audio_dec_ctx->channel_layout=AV_CH_LAYOUT_STEREO;
- /* decode until eof */
- avpkt.data = inbuf;
- avpkt.size = fread(inbuf, 1, AUDIO_INBUF_SIZE,input_file);
- /******/
- file = fopen("test", "wb");
- /*****/
- if (!(decoded_frame = avcodec_alloc_frame())) {
- fprintf(stderr, "Could not allocate audio frame\n");
- exit(1);
- }
- //printf("\ncodec inf bit_rate=%d channels=%d rate=%d",audio_dec_ctx->bit_rate,audio_dec_ctx->channels,audio_dec_ctx->sample_rate);
- while (avpkt.size > 0) {
- len=decode_packet(&got_frame, audio_stream_idx, &avpkt, audio_dec_ctx, decoded_frame);
- // printf("\ncodec inf bit_rate=%d channels=%d rate=%d",audio_dec_ctx->bit_rate,audio_dec_ctx->channels,audio_dec_ctx->sample_rate);
- if (got_frame) {
- convertSampleFormat(decoded_frame, audio_dec_ctx,&transform_dst_data ,AV_CH_LAYOUT_MONO, 1, AV_SAMPLE_FMT_S32, 8000);
- av_free(transform_dst_data);
- }
- avpkt.size -= len;
- avpkt.data += len;
- avpkt.dts =
- avpkt.pts = AV_NOPTS_VALUE;
- if (avpkt.size < AUDIO_REFILL_THRESH) {
- memmove(inbuf, avpkt.data, avpkt.size);
- avpkt.data = inbuf;
- len = fread(avpkt.data + avpkt.size, 1,
- AUDIO_INBUF_SIZE - avpkt.size,input_file);
- if (len > 0)
- avpkt.size += len;
- }
- }
- printf("\n init terminate\n");
- return (EXIT_SUCCESS);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement