You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

188 lines
5.3KB

  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. /* register all the codecs */
  87. avcodec_register_all();
  88. pkt = av_packet_alloc();
  89. /* find the MPEG audio decoder */
  90. codec = avcodec_find_decoder(AV_CODEC_ID_MP2);
  91. if (!codec) {
  92. fprintf(stderr, "Codec not found\n");
  93. exit(1);
  94. }
  95. parser = av_parser_init(codec->id);
  96. if (!parser) {
  97. fprintf(stderr, "Parser not found\n");
  98. exit(1);
  99. }
  100. c = avcodec_alloc_context3(codec);
  101. if (!c) {
  102. fprintf(stderr, "Could not allocate audio codec context\n");
  103. exit(1);
  104. }
  105. /* open it */
  106. if (avcodec_open2(c, codec, NULL) < 0) {
  107. fprintf(stderr, "Could not open codec\n");
  108. exit(1);
  109. }
  110. f = fopen(filename, "rb");
  111. if (!f) {
  112. fprintf(stderr, "Could not open %s\n", filename);
  113. exit(1);
  114. }
  115. outfile = fopen(outfilename, "wb");
  116. if (!outfile) {
  117. av_free(c);
  118. exit(1);
  119. }
  120. /* decode until eof */
  121. data = inbuf;
  122. data_size = fread(inbuf, 1, AUDIO_INBUF_SIZE, f);
  123. while (data_size > 0) {
  124. if (!decoded_frame) {
  125. if (!(decoded_frame = av_frame_alloc())) {
  126. fprintf(stderr, "Could not allocate audio frame\n");
  127. exit(1);
  128. }
  129. }
  130. ret = av_parser_parse2(parser, c, &pkt->data, &pkt->size,
  131. data, data_size,
  132. AV_NOPTS_VALUE, AV_NOPTS_VALUE, 0);
  133. if (ret < 0) {
  134. fprintf(stderr, "Error while parsing\n");
  135. exit(1);
  136. }
  137. data += ret;
  138. data_size -= ret;
  139. if (pkt->size)
  140. decode(c, pkt, decoded_frame, outfile);
  141. if (data_size < AUDIO_REFILL_THRESH) {
  142. memmove(inbuf, data, data_size);
  143. data = inbuf;
  144. len = fread(data + data_size, 1,
  145. AUDIO_INBUF_SIZE - data_size, f);
  146. if (len > 0)
  147. data_size += len;
  148. }
  149. }
  150. /* flush the decoder */
  151. pkt->data = NULL;
  152. pkt->size = 0;
  153. decode(c, pkt, decoded_frame, outfile);
  154. fclose(outfile);
  155. fclose(f);
  156. avcodec_free_context(&c);
  157. av_parser_close(parser);
  158. av_frame_free(&decoded_frame);
  159. av_packet_free(&pkt);
  160. return 0;
  161. }