Guest User

Untitled

a guest
Nov 21st, 2019
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.72 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4.  
  5. #include <libavutil/frame.h>
  6. #include <libavutil/mem.h>
  7.  
  8. #include <libavcodec/avcodec.h>
  9.  
  10. #define AUDIO_INBUF_SIZE 20480
  11. #define AUDIO_REFILL_THRESH 4096
  12.  
  13. static void decode(AVCodecContext *dec_ctx, AVPacket *pkt, AVFrame *frame,
  14. FILE *outfile)
  15. {
  16. int i, ch;
  17. int ret, data_size;
  18.  
  19. /* send the packet with the compressed data to the decoder */
  20. ret = avcodec_send_packet(dec_ctx, pkt);
  21.  
  22. if(ret < 0) {
  23. fprintf(stderr, "Error submitting the packet to the decoder\n");
  24. exit(1);
  25. }
  26.  
  27. /* read all the output frames (in general there may be any number of
  28. * them */
  29. while(ret >= 0) {
  30. ret = avcodec_receive_frame(dec_ctx, frame);
  31. if (ret == AVERROR(EAGAIN) || ret == AVERROR_EOF)
  32. return;
  33. else if (ret <0) {
  34. fprintf(stderr, "Error during decoding\n");
  35. exit(1);
  36. }
  37. data_size = av_get_bytes_per_sample(dec_ctx->sample_fmt);
  38. if(data_size < 0){
  39. /* This should not occur, checking just for parania */
  40. fprintf(stderr, "Failed to calculate data size\n");
  41. exit(1);
  42. }
  43. for(i = 0; i< frame->nb_samples; i++)
  44. for(ch =0; ch < dec_ctx->channels; ch++)
  45. fwrite(frame->data[ch] + data_size * i, 1, data_size, outfile);
  46. }
  47. }
  48.  
  49. int main(int argc, char **argv)
  50. {
  51. const char *outfilename, *filename;
  52. const AVCodec *codec;
  53. AVCodecContext *c = NULL;
  54. AVCodecParserContext *parser = NULL;
  55. int len, ret;
  56. FILE *f, *outfile;
  57. uint8_t inbuf[AUDIO_INBUF_SIZE + AV_INPUT_BUFFER_PADDING_SIZE];
  58. uint8_t *data;
  59. size_t data_size;
  60. AVPacket *pkt;
  61. AVFrame *decoded_frame = NULL;
  62.  
  63. if (argc <= 2) {
  64. fprintf(stderr, "Usage: %s <input file> <output file>\n", argv[0]);
  65. exit(0);
  66. }
  67. filename = argv[1];
  68. outfilename = argv[2];
  69.  
  70.  
  71. /* register all the codecs */
  72. avcodec_register_all();
  73.  
  74. pkt = av_packet_alloc();
  75.  
  76. /* find the AMR audio decoder */
  77. codec = avcodec_find_decoder(AV_CODEC_ID_MP2);
  78. if(!codec) {
  79. fprintf(stderr, "Codec not found\n");
  80. exit(1);
  81. }
  82.  
  83. parser = av_parser_init(codec->id);
  84. if(!parser){
  85. fprintf(stderr, "Parser not found\n");
  86. exit(1);
  87. }
  88.  
  89. c = avcodec_alloc_context3(codec);
  90. if(!c) {
  91. fprintf(stderr, "Could not allocate audio codec context\n");
  92. exit(1);
  93. }
  94.  
  95.  
  96. /* open it */
  97. if (avcodec_open2(c, codec, NULL) < 0) {
  98. fprintf(stderr, "Could not open codec\n");
  99. exit(1);
  100. }
  101.  
  102. f = fopen(filename, "rb");
  103. if (!f) {
  104. fprintf(stderr, "could not open %s\n", filename);
  105. exit(1);
  106. }
  107. outfile = fopen(outfilename, "wb");
  108. if(!outfile) {
  109. av_free(c);
  110. exit(1);
  111. }
  112.  
  113. /* decode until eof */
  114. data = inbuf;
  115. data_size = fread(inbuf, 1, AUDIO_INBUF_SIZE, f);
  116.  
  117. while(data_size > 0){
  118. if(!decoded_frame) {
  119. if(!(decoded_frame = av_frame_alloc())) {
  120. fprintf(stderr, "Could not allocate audio frame\n");
  121. exit(1);
  122. }
  123. }
  124.  
  125. ret = av_parser_parse2(parser, c, &pkt->data, &pkt->size,
  126. data, data_size,
  127. AV_NOPTS_VALUE, AV_NOPTS_VALUE, 0);
  128. if (ret <0){
  129. fprintf(stderr, "Error while parsing\n");
  130. exit(1);
  131. }
  132. data += ret;
  133. data_size -= ret;
  134.  
  135.  
  136. if(pkt->size)
  137. decode(c, pkt, decoded_frame, outfile);
  138.  
  139. if(data_size < AUDIO_REFILL_THRESH) {
  140. memmove(inbuf, data, data_size);
  141. data = inbuf;
  142. len = fread(data + data_size, 1, AUDIO_INBUF_SIZE - data_size, f);
  143.  
  144. if(len>0)
  145. data_size += len;
  146. }
  147. }
  148.  
  149. /* flush the decoder */
  150. pkt->data = NULL;
  151. pkt->size = 0;
  152. decode(c, pkt, decoded_frame, outfile);
  153. fclose(outfile);
  154. fclose(f);
  155.  
  156. avcodec_free_context(&c);
  157. av_parser_close(parser);
  158. av_frame_free(&decoded_frame);
  159. av_packet_free(&pkt);
  160.  
  161. return 0;
  162. }
Add Comment
Please, Sign In to add comment