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.

177 lines
5.0KB

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