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.

197 lines
5.4KB

  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/channel_layout.h"
  30. #include "libavutil/frame.h"
  31. #include "libavutil/mem.h"
  32. #include "libavcodec/avcodec.h"
  33. #define AUDIO_INBUF_SIZE 20480
  34. #define AUDIO_REFILL_THRESH 4096
  35. static void decode(AVCodecContext *dec_ctx, AVPacket *pkt, AVFrame *frame,
  36. FILE *outfile)
  37. {
  38. int16_t *interleave_buf;
  39. int ret, data_size, i;
  40. /* send the packet with the compressed data to the decoder */
  41. ret = avcodec_send_packet(dec_ctx, pkt);
  42. if (ret < 0) {
  43. fprintf(stderr, "Error submitting the packet to the decoder\n");
  44. exit(1);
  45. }
  46. /* read all the output frames (in general there may be any number of them */
  47. while (ret >= 0) {
  48. ret = avcodec_receive_frame(dec_ctx, frame);
  49. if (ret == AVERROR(EAGAIN) || ret == AVERROR_EOF)
  50. return;
  51. else if (ret < 0) {
  52. fprintf(stderr, "Error during decoding\n");
  53. exit(1);
  54. }
  55. /* the stream parameters may change at any time, check that they are
  56. * what we expect */
  57. if (av_get_channel_layout_nb_channels(frame->channel_layout) != 2 ||
  58. frame->format != AV_SAMPLE_FMT_S16P) {
  59. fprintf(stderr, "Unsupported frame parameters\n");
  60. exit(1);
  61. }
  62. /* The decoded data is signed 16-bit planar -- each channel in its own
  63. * buffer. We interleave the two channels manually here, but using
  64. * libavresample is recommended instead. */
  65. data_size = sizeof(*interleave_buf) * 2 * frame->nb_samples;
  66. interleave_buf = av_malloc(data_size);
  67. if (!interleave_buf)
  68. exit(1);
  69. for (i = 0; i < frame->nb_samples; i++) {
  70. interleave_buf[2 * i] = ((int16_t*)frame->data[0])[i];
  71. interleave_buf[2 * i + 1] = ((int16_t*)frame->data[1])[i];
  72. }
  73. fwrite(interleave_buf, 1, data_size, outfile);
  74. av_freep(&interleave_buf);
  75. }
  76. }
  77. int main(int argc, char **argv)
  78. {
  79. const char *outfilename, *filename;
  80. const AVCodec *codec;
  81. AVCodecContext *c= NULL;
  82. AVCodecParserContext *parser = NULL;
  83. int len, ret;
  84. FILE *f, *outfile;
  85. uint8_t inbuf[AUDIO_INBUF_SIZE + AV_INPUT_BUFFER_PADDING_SIZE];
  86. uint8_t *data;
  87. size_t data_size;
  88. AVPacket *pkt;
  89. AVFrame *decoded_frame = NULL;
  90. if (argc <= 2) {
  91. fprintf(stderr, "Usage: %s <input file> <output file>\n", argv[0]);
  92. exit(0);
  93. }
  94. filename = argv[1];
  95. outfilename = argv[2];
  96. /* register all the codecs */
  97. avcodec_register_all();
  98. pkt = av_packet_alloc();
  99. /* find the MPEG audio decoder */
  100. codec = avcodec_find_decoder(AV_CODEC_ID_MP2);
  101. if (!codec) {
  102. fprintf(stderr, "codec not found\n");
  103. exit(1);
  104. }
  105. parser = av_parser_init(codec->id);
  106. if (!parser) {
  107. fprintf(stderr, "parser not found\n");
  108. exit(1);
  109. }
  110. c = avcodec_alloc_context3(codec);
  111. /* open it */
  112. if (avcodec_open2(c, codec, NULL) < 0) {
  113. fprintf(stderr, "could not open codec\n");
  114. exit(1);
  115. }
  116. f = fopen(filename, "rb");
  117. if (!f) {
  118. fprintf(stderr, "could not open %s\n", filename);
  119. exit(1);
  120. }
  121. outfile = fopen(outfilename, "wb");
  122. if (!outfile) {
  123. av_free(c);
  124. exit(1);
  125. }
  126. /* decode until eof */
  127. data = inbuf;
  128. data_size = fread(inbuf, 1, AUDIO_INBUF_SIZE, f);
  129. while (data_size > 0) {
  130. if (!decoded_frame) {
  131. if (!(decoded_frame = av_frame_alloc())) {
  132. fprintf(stderr, "out of memory\n");
  133. exit(1);
  134. }
  135. }
  136. ret = av_parser_parse2(parser, c, &pkt->data, &pkt->size,
  137. data, data_size,
  138. AV_NOPTS_VALUE, AV_NOPTS_VALUE, 0);
  139. if (ret < 0) {
  140. fprintf(stderr, "Error while parsing\n");
  141. exit(1);
  142. }
  143. data += ret;
  144. data_size -= ret;
  145. if (pkt->size)
  146. decode(c, pkt, decoded_frame, outfile);
  147. if (data_size < AUDIO_REFILL_THRESH) {
  148. memmove(inbuf, data, data_size);
  149. data = inbuf;
  150. len = fread(data + data_size, 1,
  151. AUDIO_INBUF_SIZE - data_size, f);
  152. if (len > 0)
  153. data_size += len;
  154. }
  155. }
  156. /* flush the decoder */
  157. pkt->data = NULL;
  158. pkt->size = 0;
  159. decode(c, pkt, decoded_frame, outfile);
  160. fclose(outfile);
  161. fclose(f);
  162. avcodec_free_context(&c);
  163. av_parser_close(parser);
  164. av_frame_free(&decoded_frame);
  165. av_packet_free(&pkt);
  166. return 0;
  167. }