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.

139 lines
3.8KB

  1. /*
  2. * copyright (c) 2001 Fabrice Bellard
  3. *
  4. * This file is part of Libav.
  5. *
  6. * Libav is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU Lesser General Public
  8. * License as published by the Free Software Foundation; either
  9. * version 2.1 of the License, or (at your option) any later version.
  10. *
  11. * Libav is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * Lesser General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public
  17. * License along with Libav; if not, write to the Free Software
  18. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  19. */
  20. /**
  21. * @file
  22. * audio decoding with libavcodec API example
  23. *
  24. * @example decode_audio.c
  25. */
  26. #include <stdio.h>
  27. #include <stdlib.h>
  28. #include <string.h>
  29. #include "libavutil/frame.h"
  30. #include "libavutil/mem.h"
  31. #include "libavcodec/avcodec.h"
  32. #define AUDIO_INBUF_SIZE 20480
  33. #define AUDIO_REFILL_THRESH 4096
  34. int main(int argc, char **argv)
  35. {
  36. const char *outfilename, *filename;
  37. const AVCodec *codec;
  38. AVCodecContext *c= NULL;
  39. int len;
  40. FILE *f, *outfile;
  41. uint8_t inbuf[AUDIO_INBUF_SIZE + AV_INPUT_BUFFER_PADDING_SIZE];
  42. AVPacket avpkt;
  43. AVFrame *decoded_frame = NULL;
  44. if (argc <= 2) {
  45. fprintf(stderr, "Usage: %s <input file> <output file>\n", argv[0]);
  46. exit(0);
  47. }
  48. filename = argv[1];
  49. outfilename = argv[2];
  50. /* register all the codecs */
  51. avcodec_register_all();
  52. av_init_packet(&avpkt);
  53. /* find the MPEG audio decoder */
  54. codec = avcodec_find_decoder(AV_CODEC_ID_MP2);
  55. if (!codec) {
  56. fprintf(stderr, "codec not found\n");
  57. exit(1);
  58. }
  59. c = avcodec_alloc_context3(codec);
  60. /* open it */
  61. if (avcodec_open2(c, codec, NULL) < 0) {
  62. fprintf(stderr, "could not open codec\n");
  63. exit(1);
  64. }
  65. f = fopen(filename, "rb");
  66. if (!f) {
  67. fprintf(stderr, "could not open %s\n", filename);
  68. exit(1);
  69. }
  70. outfile = fopen(outfilename, "wb");
  71. if (!outfile) {
  72. av_free(c);
  73. exit(1);
  74. }
  75. /* decode until eof */
  76. avpkt.data = inbuf;
  77. avpkt.size = fread(inbuf, 1, AUDIO_INBUF_SIZE, f);
  78. while (avpkt.size > 0) {
  79. int got_frame = 0;
  80. if (!decoded_frame) {
  81. if (!(decoded_frame = av_frame_alloc())) {
  82. fprintf(stderr, "out of memory\n");
  83. exit(1);
  84. }
  85. }
  86. len = avcodec_decode_audio4(c, decoded_frame, &got_frame, &avpkt);
  87. if (len < 0) {
  88. fprintf(stderr, "Error while decoding\n");
  89. exit(1);
  90. }
  91. if (got_frame) {
  92. /* if a frame has been decoded, output it */
  93. int data_size = av_samples_get_buffer_size(NULL, c->channels,
  94. decoded_frame->nb_samples,
  95. c->sample_fmt, 1);
  96. fwrite(decoded_frame->data[0], 1, data_size, outfile);
  97. }
  98. avpkt.size -= len;
  99. avpkt.data += len;
  100. if (avpkt.size < AUDIO_REFILL_THRESH) {
  101. /* Refill the input buffer, to avoid trying to decode
  102. * incomplete frames. Instead of this, one could also use
  103. * a parser, or use a proper container format through
  104. * libavformat. */
  105. memmove(inbuf, avpkt.data, avpkt.size);
  106. avpkt.data = inbuf;
  107. len = fread(avpkt.data + avpkt.size, 1,
  108. AUDIO_INBUF_SIZE - avpkt.size, f);
  109. if (len > 0)
  110. avpkt.size += len;
  111. }
  112. }
  113. fclose(outfile);
  114. fclose(f);
  115. avcodec_free_context(&c);
  116. av_frame_free(&decoded_frame);
  117. return 0;
  118. }