decode_audio.c 7.1 KB

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