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.

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