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.

153 lines
4.5KB

  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. int main(int argc, char **argv)
  37. {
  38. const char *outfilename, *filename;
  39. const AVCodec *codec;
  40. AVCodecContext *c= NULL;
  41. int len;
  42. FILE *f, *outfile;
  43. uint8_t inbuf[AUDIO_INBUF_SIZE + AV_INPUT_BUFFER_PADDING_SIZE];
  44. AVPacket avpkt;
  45. AVFrame *decoded_frame = NULL;
  46. if (argc <= 2) {
  47. fprintf(stderr, "Usage: %s <input file> <output file>\n", argv[0]);
  48. exit(0);
  49. }
  50. filename = argv[1];
  51. outfilename = argv[2];
  52. /* register all the codecs */
  53. avcodec_register_all();
  54. av_init_packet(&avpkt);
  55. /* find the MPEG audio decoder */
  56. codec = avcodec_find_decoder(AV_CODEC_ID_MP2);
  57. if (!codec) {
  58. fprintf(stderr, "Codec not found\n");
  59. exit(1);
  60. }
  61. c = avcodec_alloc_context3(codec);
  62. if (!c) {
  63. fprintf(stderr, "Could not allocate audio codec context\n");
  64. exit(1);
  65. }
  66. /* open it */
  67. if (avcodec_open2(c, codec, NULL) < 0) {
  68. fprintf(stderr, "Could not open codec\n");
  69. exit(1);
  70. }
  71. f = fopen(filename, "rb");
  72. if (!f) {
  73. fprintf(stderr, "Could not open %s\n", filename);
  74. exit(1);
  75. }
  76. outfile = fopen(outfilename, "wb");
  77. if (!outfile) {
  78. av_free(c);
  79. exit(1);
  80. }
  81. /* decode until eof */
  82. avpkt.data = inbuf;
  83. avpkt.size = fread(inbuf, 1, AUDIO_INBUF_SIZE, f);
  84. while (avpkt.size > 0) {
  85. int i, ch;
  86. int got_frame = 0;
  87. if (!decoded_frame) {
  88. if (!(decoded_frame = av_frame_alloc())) {
  89. fprintf(stderr, "Could not allocate audio frame\n");
  90. exit(1);
  91. }
  92. }
  93. len = avcodec_decode_audio4(c, decoded_frame, &got_frame, &avpkt);
  94. if (len < 0) {
  95. fprintf(stderr, "Error while decoding\n");
  96. exit(1);
  97. }
  98. if (got_frame) {
  99. /* if a frame has been decoded, output it */
  100. int data_size = av_get_bytes_per_sample(c->sample_fmt);
  101. if (data_size < 0) {
  102. /* This should not occur, checking just for paranoia */
  103. fprintf(stderr, "Failed to calculate data size\n");
  104. exit(1);
  105. }
  106. for (i=0; i<decoded_frame->nb_samples; i++)
  107. for (ch=0; ch<c->channels; ch++)
  108. fwrite(decoded_frame->data[ch] + data_size*i, 1, data_size, outfile);
  109. }
  110. avpkt.size -= len;
  111. avpkt.data += len;
  112. avpkt.dts =
  113. avpkt.pts = AV_NOPTS_VALUE;
  114. if (avpkt.size < AUDIO_REFILL_THRESH) {
  115. /* Refill the input buffer, to avoid trying to decode
  116. * incomplete frames. Instead of this, one could also use
  117. * a parser, or use a proper container format through
  118. * libavformat. */
  119. memmove(inbuf, avpkt.data, avpkt.size);
  120. avpkt.data = inbuf;
  121. len = fread(avpkt.data + avpkt.size, 1,
  122. AUDIO_INBUF_SIZE - avpkt.size, f);
  123. if (len > 0)
  124. avpkt.size += len;
  125. }
  126. }
  127. fclose(outfile);
  128. fclose(f);
  129. avcodec_free_context(&c);
  130. av_frame_free(&decoded_frame);
  131. return 0;
  132. }