decode_audio.c 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  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 int get_format_from_sample_fmt(const char **fmt,
  37. enum AVSampleFormat sample_fmt)
  38. {
  39. int i;
  40. struct sample_fmt_entry {
  41. enum AVSampleFormat sample_fmt; const char *fmt_be, *fmt_le;
  42. } sample_fmt_entries[] = {
  43. { AV_SAMPLE_FMT_U8, "u8", "u8" },
  44. { AV_SAMPLE_FMT_S16, "s16be", "s16le" },
  45. { AV_SAMPLE_FMT_S32, "s32be", "s32le" },
  46. { AV_SAMPLE_FMT_FLT, "f32be", "f32le" },
  47. { AV_SAMPLE_FMT_DBL, "f64be", "f64le" },
  48. };
  49. *fmt = NULL;
  50. for (i = 0; i < FF_ARRAY_ELEMS(sample_fmt_entries); i++) {
  51. struct sample_fmt_entry *entry = &sample_fmt_entries[i];
  52. if (sample_fmt == entry->sample_fmt) {
  53. *fmt = AV_NE(entry->fmt_be, entry->fmt_le);
  54. return 0;
  55. }
  56. }
  57. fprintf(stderr,
  58. "sample format %s is not supported as output format\n",
  59. av_get_sample_fmt_name(sample_fmt));
  60. return -1;
  61. }
  62. static void decode(AVCodecContext *dec_ctx, AVPacket *pkt, AVFrame *frame,
  63. FILE *outfile)
  64. {
  65. int i, ch;
  66. int ret, data_size;
  67. /* send the packet with the compressed data to the decoder */
  68. ret = avcodec_send_packet(dec_ctx, pkt);
  69. if (ret < 0) {
  70. fprintf(stderr, "Error submitting the packet to the decoder\n");
  71. exit(1);
  72. }
  73. /* read all the output frames (in general there may be any number of them */
  74. while (ret >= 0) {
  75. ret = avcodec_receive_frame(dec_ctx, frame);
  76. if (ret == AVERROR(EAGAIN) || ret == AVERROR_EOF)
  77. return;
  78. else if (ret < 0) {
  79. fprintf(stderr, "Error during decoding\n");
  80. exit(1);
  81. }
  82. data_size = av_get_bytes_per_sample(dec_ctx->sample_fmt);
  83. if (data_size < 0) {
  84. /* This should not occur, checking just for paranoia */
  85. fprintf(stderr, "Failed to calculate data size\n");
  86. exit(1);
  87. }
  88. for (i = 0; i < frame->nb_samples; i++)
  89. for (ch = 0; ch < dec_ctx->ch_layout.nb_channels; ch++)
  90. fwrite(frame->data[ch] + data_size*i, 1, data_size, outfile);
  91. }
  92. }
  93. int main(int argc, char **argv)
  94. {
  95. const char *outfilename, *filename;
  96. const AVCodec *codec;
  97. AVCodecContext *c= NULL;
  98. AVCodecParserContext *parser = NULL;
  99. int len, ret;
  100. FILE *f, *outfile;
  101. uint8_t inbuf[AUDIO_INBUF_SIZE + AV_INPUT_BUFFER_PADDING_SIZE];
  102. uint8_t *data;
  103. size_t data_size;
  104. AVPacket *pkt;
  105. AVFrame *decoded_frame = NULL;
  106. enum AVSampleFormat sfmt;
  107. int n_channels = 0;
  108. const char *fmt;
  109. if (argc <= 2) {
  110. fprintf(stderr, "Usage: %s <input file> <output file>\n", argv[0]);
  111. exit(0);
  112. }
  113. filename = argv[1];
  114. outfilename = argv[2];
  115. pkt = av_packet_alloc();
  116. /* find the MPEG audio decoder */
  117. codec = avcodec_find_decoder(AV_CODEC_ID_MP2);
  118. if (!codec) {
  119. fprintf(stderr, "Codec not found\n");
  120. exit(1);
  121. }
  122. parser = av_parser_init(codec->id);
  123. if (!parser) {
  124. fprintf(stderr, "Parser not found\n");
  125. exit(1);
  126. }
  127. c = avcodec_alloc_context3(codec);
  128. if (!c) {
  129. fprintf(stderr, "Could not allocate audio codec context\n");
  130. exit(1);
  131. }
  132. /* open it */
  133. if (avcodec_open2(c, codec, NULL) < 0) {
  134. fprintf(stderr, "Could not open codec\n");
  135. exit(1);
  136. }
  137. f = fopen(filename, "rb");
  138. if (!f) {
  139. fprintf(stderr, "Could not open %s\n", filename);
  140. exit(1);
  141. }
  142. outfile = fopen(outfilename, "wb");
  143. if (!outfile) {
  144. av_free(c);
  145. exit(1);
  146. }
  147. /* decode until eof */
  148. data = inbuf;
  149. data_size = fread(inbuf, 1, AUDIO_INBUF_SIZE, f);
  150. while (data_size > 0) {
  151. if (!decoded_frame) {
  152. if (!(decoded_frame = av_frame_alloc())) {
  153. fprintf(stderr, "Could not allocate audio frame\n");
  154. exit(1);
  155. }
  156. }
  157. ret = av_parser_parse2(parser, c, &pkt->data, &pkt->size,
  158. data, data_size,
  159. AV_NOPTS_VALUE, AV_NOPTS_VALUE, 0);
  160. if (ret < 0) {
  161. fprintf(stderr, "Error while parsing\n");
  162. exit(1);
  163. }
  164. data += ret;
  165. data_size -= ret;
  166. if (pkt->size)
  167. decode(c, pkt, decoded_frame, outfile);
  168. if (data_size < AUDIO_REFILL_THRESH) {
  169. memmove(inbuf, data, data_size);
  170. data = inbuf;
  171. len = fread(data + data_size, 1,
  172. AUDIO_INBUF_SIZE - data_size, f);
  173. if (len > 0)
  174. data_size += len;
  175. }
  176. }
  177. /* flush the decoder */
  178. pkt->data = NULL;
  179. pkt->size = 0;
  180. decode(c, pkt, decoded_frame, outfile);
  181. /* print output pcm infomations, because there have no metadata of pcm */
  182. sfmt = c->sample_fmt;
  183. if (av_sample_fmt_is_planar(sfmt)) {
  184. const char *packed = av_get_sample_fmt_name(sfmt);
  185. printf("Warning: the sample format the decoder produced is planar "
  186. "(%s). This example will output the first channel only.\n",
  187. packed ? packed : "?");
  188. sfmt = av_get_packed_sample_fmt(sfmt);
  189. }
  190. n_channels = c->ch_layout.nb_channels;
  191. if ((ret = get_format_from_sample_fmt(&fmt, sfmt)) < 0)
  192. goto end;
  193. printf("Play the output audio file with the command:\n"
  194. "ffplay -f %s -ac %d -ar %d %s\n",
  195. fmt, n_channels, c->sample_rate,
  196. outfilename);
  197. end:
  198. fclose(outfile);
  199. fclose(f);
  200. avcodec_free_context(&c);
  201. av_parser_close(parser);
  202. av_frame_free(&decoded_frame);
  203. av_packet_free(&pkt);
  204. return 0;
  205. }