decode_audio.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. /*
  2. * Copyright (c) 2001 Fabrice Bellard
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a copy
  5. * of this software and associated documentation files (the "Software"), to deal
  6. * in the Software without restriction, including without limitation the rights
  7. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. * copies of the Software, and to permit persons to whom the Software is
  9. * furnished to do so, subject to the following conditions:
  10. *
  11. * The above copyright notice and this permission notice shall be included in
  12. * all copies or substantial portions of the Software.
  13. *
  14. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  17. * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  19. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  20. * THE SOFTWARE.
  21. */
  22. /**
  23. * @file
  24. * audio decoding with libavcodec API example
  25. *
  26. * @example decode_audio.c
  27. */
  28. #include <stdio.h>
  29. #include <stdlib.h>
  30. #include <string.h>
  31. #include <libavutil/frame.h>
  32. #include <libavutil/mem.h>
  33. #include <libavcodec/avcodec.h>
  34. #define AUDIO_INBUF_SIZE 20480
  35. #define AUDIO_REFILL_THRESH 4096
  36. static void decode(AVCodecContext *dec_ctx, AVPacket *pkt, AVFrame *frame,
  37. FILE *outfile)
  38. {
  39. int i, ch;
  40. int ret, data_size;
  41. /* send the packet with the compressed data to the decoder */
  42. ret = avcodec_send_packet(dec_ctx, pkt);
  43. if (ret < 0) {
  44. fprintf(stderr, "Error submitting the packet to the decoder\n");
  45. exit(1);
  46. }
  47. /* read all the output frames (in general there may be any number of them */
  48. while (ret >= 0) {
  49. ret = avcodec_receive_frame(dec_ctx, frame);
  50. if (ret == AVERROR(EAGAIN) || ret == AVERROR_EOF)
  51. return;
  52. else if (ret < 0) {
  53. fprintf(stderr, "Error during decoding\n");
  54. exit(1);
  55. }
  56. data_size = av_get_bytes_per_sample(dec_ctx->sample_fmt);
  57. if (data_size < 0) {
  58. /* This should not occur, checking just for paranoia */
  59. fprintf(stderr, "Failed to calculate data size\n");
  60. exit(1);
  61. }
  62. for (i = 0; i < frame->nb_samples; i++)
  63. for (ch = 0; ch < dec_ctx->channels; ch++)
  64. fwrite(frame->data[ch] + data_size*i, 1, data_size, outfile);
  65. }
  66. }
  67. int main(int argc, char **argv)
  68. {
  69. const char *outfilename, *filename;
  70. const AVCodec *codec;
  71. AVCodecContext *c= NULL;
  72. AVCodecParserContext *parser = NULL;
  73. int len, ret;
  74. FILE *f, *outfile;
  75. uint8_t inbuf[AUDIO_INBUF_SIZE + AV_INPUT_BUFFER_PADDING_SIZE];
  76. uint8_t *data;
  77. size_t data_size;
  78. AVPacket *pkt;
  79. AVFrame *decoded_frame = NULL;
  80. if (argc <= 2) {
  81. fprintf(stderr, "Usage: %s <input file> <output file>\n", argv[0]);
  82. exit(0);
  83. }
  84. filename = argv[1];
  85. outfilename = argv[2];
  86. pkt = av_packet_alloc();
  87. /* find the MPEG audio decoder */
  88. codec = avcodec_find_decoder(AV_CODEC_ID_MP2);
  89. if (!codec) {
  90. fprintf(stderr, "Codec not found\n");
  91. exit(1);
  92. }
  93. parser = av_parser_init(codec->id);
  94. if (!parser) {
  95. fprintf(stderr, "Parser not found\n");
  96. exit(1);
  97. }
  98. c = avcodec_alloc_context3(codec);
  99. if (!c) {
  100. fprintf(stderr, "Could not allocate audio codec context\n");
  101. exit(1);
  102. }
  103. /* open it */
  104. if (avcodec_open2(c, codec, NULL) < 0) {
  105. fprintf(stderr, "Could not open codec\n");
  106. exit(1);
  107. }
  108. f = fopen(filename, "rb");
  109. if (!f) {
  110. fprintf(stderr, "Could not open %s\n", filename);
  111. exit(1);
  112. }
  113. outfile = fopen(outfilename, "wb");
  114. if (!outfile) {
  115. av_free(c);
  116. exit(1);
  117. }
  118. /* decode until eof */
  119. data = inbuf;
  120. data_size = fread(inbuf, 1, AUDIO_INBUF_SIZE, f);
  121. while (data_size > 0) {
  122. if (!decoded_frame) {
  123. if (!(decoded_frame = av_frame_alloc())) {
  124. fprintf(stderr, "Could not allocate audio frame\n");
  125. exit(1);
  126. }
  127. }
  128. ret = av_parser_parse2(parser, c, &pkt->data, &pkt->size,
  129. data, data_size,
  130. AV_NOPTS_VALUE, AV_NOPTS_VALUE, 0);
  131. if (ret < 0) {
  132. fprintf(stderr, "Error while parsing\n");
  133. exit(1);
  134. }
  135. data += ret;
  136. data_size -= ret;
  137. if (pkt->size)
  138. decode(c, pkt, decoded_frame, outfile);
  139. if (data_size < AUDIO_REFILL_THRESH) {
  140. memmove(inbuf, data, data_size);
  141. data = inbuf;
  142. len = fread(data + data_size, 1,
  143. AUDIO_INBUF_SIZE - data_size, f);
  144. if (len > 0)
  145. data_size += len;
  146. }
  147. }
  148. /* flush the decoder */
  149. pkt->data = NULL;
  150. pkt->size = 0;
  151. decode(c, pkt, decoded_frame, outfile);
  152. fclose(outfile);
  153. fclose(f);
  154. avcodec_free_context(&c);
  155. av_parser_close(parser);
  156. av_frame_free(&decoded_frame);
  157. av_packet_free(&pkt);
  158. return 0;
  159. }