Hax4Us

DecoderMp3

Aug 3rd, 2019
194
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.45 KB | None | 0 0
  1. /* FFmpeg Usage Example
  2. * Date : 28 July 2019
  3. */
  4. #include <stdio.h>
  5. #include <libavformat/avformat.h>
  6. #include <libavcodec/avcodec.h>
  7. #include <libavutil/avutil.h>
  8. #include <assert.h>
  9.  
  10. int decode_packet(AVCodecContext*, AVPacket*, AVFrame*);
  11.  
  12. int main(void) {
  13. AVFormatContext *pFormatContext = avformat_alloc_context();
  14. AVCodecParameters *pCodecParameters = NULL;
  15. if(avformat_open_input(&pFormatContext,"song.mp3",NULL,NULL)!=0) {
  16. fprintf(stderr,"Could not open file\n");
  17. return -1;
  18. }
  19. if(avformat_find_stream_info(pFormatContext,NULL)<0) {
  20. fprintf(stderr,"Could not find stream\n");
  21. return -1;
  22. }
  23.  
  24. size_t stream_index = 0;
  25. for(;stream_index<pFormatContext->nb_streams;stream_index++) {
  26. if(pFormatContext->streams[stream_index]->codecpar->codec_type == AVMEDIA_TYPE_AUDIO) {
  27. pCodecParameters = pFormatContext->streams[stream_index]->codecpar;
  28. }
  29. break;
  30. }
  31.  
  32. if(stream_index == -1) {
  33. fprintf(stderr,"could not retrive stream info from file\n");
  34. return -1;
  35. }
  36. AVStream *stream = pFormatContext->streams[stream_index];
  37. pCodecParameters = stream->codecpar;
  38. AVCodec *cdc = avcodec_find_decoder(pCodecParameters->codec_id);
  39. AVCodecContext *cdc_ctx = avcodec_alloc_context3(cdc);
  40. assert(pCodecParameters);
  41.  
  42. if(avcodec_parameters_to_context(cdc_ctx,pCodecParameters) < 0) {
  43. fprintf(stderr,"Can't copy params to codec context\n");
  44. return -1;
  45. }
  46.  
  47. if(avcodec_open2(cdc_ctx,cdc,NULL) < 0) {
  48. fprintf(stderr,"Failed to open decoder for stream\n");
  49. return -1;
  50. }
  51.  
  52. AVFrame *frame = av_frame_alloc();
  53. if(!frame) {
  54. fprintf(stderr,"could not allocate memory for frame\n");
  55. return -1;
  56. }
  57.  
  58. AVPacket *packet = av_packet_alloc();
  59. // av_init_packet(&packet);
  60. if(!packet) {
  61. fprintf(stderr,"could not allocate memory for packet");
  62. return -1;
  63. }
  64.  
  65. packet->data=NULL;
  66. packet->size=0;
  67. // lets read the packets
  68. while(av_read_frame(pFormatContext,packet) >= 0) {
  69. if(packet->stream_index==stream_index) {
  70. int response = 0 ;
  71. response = decode_packet(cdc_ctx,packet,frame);
  72.  
  73. if(response < 0)
  74. continue;
  75. }
  76. av_packet_unref(packet);
  77. }
  78.  
  79. return 0;
  80. }
  81.  
  82. int decode_packet(AVCodecContext *cdc_ctx , AVPacket *pkt, AVFrame *frm) {
  83. int response = avcodec_send_packet(cdc_ctx,pkt); if(response < 0)
  84. return response; while(response >= 0) {
  85. response = avcodec_receive_frame(cdc_ctx,frm);
  86. if(response == AVERROR(EAGAIN) || response == AVERROR_EOF)
  87. return -1;
  88. else if(response < 0)
  89. return response;
  90. }
  91. return 0;
  92. }
  93. ~
Add Comment
Please, Sign In to add comment