Advertisement
Guest User

transcode

a guest
Jan 31st, 2013
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 7.50 KB | None | 0 0
  1. /*
  2.  * File:   main.c
  3.  * Author: antonello
  4.  *
  5.  * Created on 29 gennaio 2013, 14.26
  6.  */
  7.  
  8. #include <stdio.h>
  9. #include <stdlib.h>
  10.  
  11. #include <libavutil/imgutils.h>
  12. #include <libavutil/samplefmt.h>
  13. #include <libavutil/timestamp.h>
  14. #include <libavformat/avformat.h>
  15. #include <libavcodec/avcodec.h>
  16. #include <libavutil/mem.h>
  17. #include <libavutil/channel_layout.h>
  18.  
  19. #define INBUF_SIZE 4096
  20. #define AUDIO_INBUF_SIZE 20480
  21. #define AUDIO_REFILL_THRESH 4096
  22.  
  23. static FILE* file;
  24.  
  25. int alloc_samples_array_and_data(uint8_t ***data, int *linesize, int nb_channels,
  26.         int nb_samples, enum AVSampleFormat sample_fmt, int align) {
  27.     int nb_planes = av_sample_fmt_is_planar(sample_fmt) ? nb_channels : 1;
  28.  
  29.     *data = av_malloc(sizeof (*data) * nb_planes);
  30.     if (!*data)
  31.         return AVERROR(ENOMEM);
  32.     return av_samples_alloc(*data, linesize, nb_channels,
  33.             nb_samples, sample_fmt, align);
  34. }
  35.  
  36. static int open_codec_context(int *stream_idx,
  37.         AVFormatContext *fmt_ctx, enum AVMediaType type) {
  38.     int ret;
  39.     AVStream *st;
  40.     AVCodecContext *dec_ctx = NULL;
  41.     AVCodec *dec = NULL;
  42.  
  43.     ret = av_find_best_stream(fmt_ctx, type, -1, -1, NULL, 0);
  44.     if (ret < 0) {
  45.         fprintf(stderr, "Could not find %s stream'\n",
  46.                 av_get_media_type_string(type));
  47.         return ret;
  48.     } else {
  49.         *stream_idx = ret;
  50.         st = fmt_ctx->streams[*stream_idx];
  51.  
  52.         /* find decoder for the stream */
  53.         dec_ctx = st->codec;
  54.         dec = avcodec_find_decoder(dec_ctx->codec_id);
  55.         if (!dec) {
  56.             fprintf(stderr, "Failed to find %s codec\n",
  57.                     av_get_media_type_string(type));
  58.             return ret;
  59.         }
  60.  
  61.         if ((ret = avcodec_open2(dec_ctx, dec, NULL)) < 0) {
  62.             fprintf(stderr, "Failed to open %s codec\n",
  63.                     av_get_media_type_string(type));
  64.             return ret;
  65.         }
  66.     }
  67.  
  68.     return 0;
  69. }
  70.  
  71. static int decode_packet(int *got_frame, int audio_stream_idx, AVPacket *pkt, AVCodecContext *audio_dec_ctx, AVFrame *frame) {
  72.     int ret = 0;
  73.     int audio_dst_bufsize = 0;
  74.    
  75.     if (pkt->stream_index == audio_stream_idx) {
  76.         /* decode audio frame */
  77.         ret = avcodec_decode_audio4(audio_dec_ctx, frame, got_frame, pkt);
  78.         if (ret < 0) {
  79.             fprintf(stderr, "Error decoding audio frame\n");
  80.             return ret;
  81.         }
  82.  
  83.         if (*got_frame) {
  84. //                        audio_dst_bufsize =
  85. //                            av_samples_get_buffer_size(NULL, frame->channels,
  86. //                                                       frame->nb_samples, frame->format, 1);
  87. //                        fwrite(frame->data[0],1,audio_dst_bufsize,file);
  88.         }
  89.     }
  90.  
  91.     return ret;
  92. }
  93.  
  94. 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) {
  95.    
  96.     int  dst_nb_channels = 0;
  97.     int  dst_linesize;
  98.     int  dst_nb_samples, max_dst_nb_samples;
  99.     int dst_bufsize;
  100.     struct SwrContext *swr_ctx;
  101.     int ret;
  102.     uint8_t  **dst_data=NULL;
  103.     swr_ctx= swr_alloc();
  104.     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);
  105.     if ((ret = swr_init(swr_ctx)) < 0) {
  106.         fprintf(stderr, "Failed to initialize the resampling context\n");
  107.     }
  108.     max_dst_nb_samples = dst_nb_samples =
  109.             av_rescale_rnd(src_frame1->sample_rate, dst_rate1, dst_rate1, AV_ROUND_UP);
  110.      /* buffer is going to be directly written to a rawaudio file, no alignment */
  111.     dst_nb_channels = av_get_channel_layout_nb_channels(dst_ch_layout1);
  112.        
  113.     ret = alloc_samples_array_and_data(&dst_data, &dst_linesize, dst_nb_channels1,
  114.             dst_nb_samples, dst_sample_fmt1, 0);
  115.     if (ret < 0) {
  116.         fprintf(stderr, "Could not allocate destination samples\n");
  117.     }
  118.    
  119.     /* convert to destination format */
  120.     ret = swr_convert(swr_ctx, dst_data, dst_nb_samples, (const uint8_t **) src_frame1->data, src_frame1->nb_samples);
  121.     if (ret < 0) {
  122.         fprintf(stderr, "Error while converting\n");
  123.     }
  124.     dst_bufsize = av_samples_get_buffer_size(&dst_linesize, dst_nb_channels1,
  125.             ret, dst_sample_fmt1, 1);
  126.     fwrite(dst_data[0], 1, dst_bufsize, file);
  127.    
  128.     *dst_data1=dst_data;
  129.  return dst_bufsize;  
  130.  
  131. }
  132.  
  133.  
  134.  
  135. int main(int argc, char** argv) {
  136.     AVFormatContext* formatcontext = avformat_alloc_context();
  137.     AVCodecContext *audio_dec_ctx = NULL;
  138.     AVStream *audio_stream = NULL;
  139.     AVCodec *audio_codec=NULL;
  140.     uint8_t  **transform_dst_data = NULL;
  141.     AVPacket avpkt;
  142.     AVFrame *decoded_frame = NULL, *transform_frame = NULL;
  143.     int audio_stream_idx = 0;
  144.     int got_frame=0;
  145.     FILE *input_file=NULL;
  146.     uint8_t inbuf[AUDIO_INBUF_SIZE+ FF_INPUT_BUFFER_PADDING_SIZE];
  147.     uint8_t header[10];
  148.     int len=0;
  149.    
  150.     printf("read %s",argv[1]);
  151.     input_file=fopen(argv[1], "rb");
  152.     if (!input_file) {
  153.         fprintf(stderr, "Could not open file\n");
  154.         exit(1);
  155.     }
  156.     fread(header, 1, 9,input_file);
  157.    
  158.     //library initialize
  159.     av_register_all();
  160.     //network componet init
  161.    
  162.     av_init_packet(&avpkt);
  163.    /* find the AAC audio decoder */
  164.     audio_codec = avcodec_find_decoder(AV_CODEC_ID_AAC);
  165.     if (!audio_codec) {
  166.         fprintf(stderr, "Codec not found\n");
  167.         exit(1);
  168.     }
  169.    
  170.     audio_dec_ctx = avcodec_alloc_context3(audio_codec);
  171.    
  172.     if (!audio_dec_ctx) {
  173.         fprintf(stderr, "Could not allocate audio codec context\n");
  174.         exit(1);
  175.     }
  176.  
  177.     /* open it */
  178.     if (avcodec_open2(audio_dec_ctx, audio_codec, NULL) < 0) {
  179.         fprintf(stderr, "Could not open codec\n");
  180.         exit(1);
  181.     }
  182.  
  183.  
  184.     audio_dec_ctx->channels=1;
  185.     audio_dec_ctx->sample_rate=8000;
  186.     audio_dec_ctx->bit_rate=12000;
  187.     audio_dec_ctx->sample_fmt=AV_SAMPLE_FMT_S16;
  188.     audio_dec_ctx->channel_layout=AV_CH_LAYOUT_STEREO;
  189.    
  190.     /* decode until eof */
  191.     avpkt.data = inbuf;
  192.     avpkt.size = fread(inbuf, 1, AUDIO_INBUF_SIZE,input_file);
  193.  
  194.     /******/
  195.     file = fopen("test", "wb");
  196.     /*****/
  197.     if (!(decoded_frame = avcodec_alloc_frame())) {
  198.                 fprintf(stderr, "Could not allocate audio frame\n");
  199.                 exit(1);
  200.             }
  201.    //printf("\ncodec inf bit_rate=%d channels=%d rate=%d",audio_dec_ctx->bit_rate,audio_dec_ctx->channels,audio_dec_ctx->sample_rate);
  202.        
  203.     while (avpkt.size > 0) {
  204.         len=decode_packet(&got_frame, audio_stream_idx, &avpkt, audio_dec_ctx, decoded_frame);
  205.       //  printf("\ncodec inf bit_rate=%d channels=%d rate=%d",audio_dec_ctx->bit_rate,audio_dec_ctx->channels,audio_dec_ctx->sample_rate);
  206.         if (got_frame) {
  207.         convertSampleFormat(decoded_frame, audio_dec_ctx,&transform_dst_data ,AV_CH_LAYOUT_MONO, 1, AV_SAMPLE_FMT_S32, 8000);
  208.         av_free(transform_dst_data);
  209.         }
  210.         avpkt.size -= len;
  211.         avpkt.data += len;
  212.         avpkt.dts =
  213.         avpkt.pts = AV_NOPTS_VALUE;
  214.         if (avpkt.size < AUDIO_REFILL_THRESH) {      
  215.             memmove(inbuf, avpkt.data, avpkt.size);
  216.             avpkt.data = inbuf;
  217.             len = fread(avpkt.data + avpkt.size, 1,
  218.                         AUDIO_INBUF_SIZE - avpkt.size,input_file);
  219.             if (len > 0)
  220.                 avpkt.size += len;
  221.         }
  222.        
  223.     }
  224.  
  225.    
  226.    
  227.    
  228.     printf("\n init terminate\n");
  229.     return (EXIT_SUCCESS);
  230. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement