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.

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