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.

206 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 encoding with libavcodec API example.
  23. *
  24. * @example encode_audio.c
  25. */
  26. #include <stdint.h>
  27. #include <stdio.h>
  28. #include <stdlib.h>
  29. #include "libavcodec/avcodec.h"
  30. #include "libavutil/channel_layout.h"
  31. #include "libavutil/common.h"
  32. #include "libavutil/frame.h"
  33. #include "libavutil/samplefmt.h"
  34. /* check that a given sample format is supported by the encoder */
  35. static int check_sample_fmt(const AVCodec *codec, enum AVSampleFormat sample_fmt)
  36. {
  37. const enum AVSampleFormat *p = codec->sample_fmts;
  38. while (*p != AV_SAMPLE_FMT_NONE) {
  39. if (*p == sample_fmt)
  40. return 1;
  41. p++;
  42. }
  43. return 0;
  44. }
  45. /* just pick the highest supported samplerate */
  46. static int select_sample_rate(const AVCodec *codec)
  47. {
  48. const int *p;
  49. int best_samplerate = 0;
  50. if (!codec->supported_samplerates)
  51. return 44100;
  52. p = codec->supported_samplerates;
  53. while (*p) {
  54. best_samplerate = FFMAX(*p, best_samplerate);
  55. p++;
  56. }
  57. return best_samplerate;
  58. }
  59. /* select layout with the highest channel count */
  60. static int select_channel_layout(const AVCodec *codec)
  61. {
  62. const uint64_t *p;
  63. uint64_t best_ch_layout = 0;
  64. int best_nb_channels = 0;
  65. if (!codec->channel_layouts)
  66. return AV_CH_LAYOUT_STEREO;
  67. p = codec->channel_layouts;
  68. while (*p) {
  69. int nb_channels = av_get_channel_layout_nb_channels(*p);
  70. if (nb_channels > best_nb_channels) {
  71. best_ch_layout = *p;
  72. best_nb_channels = nb_channels;
  73. }
  74. p++;
  75. }
  76. return best_ch_layout;
  77. }
  78. int main(int argc, char **argv)
  79. {
  80. const char *filename;
  81. const AVCodec *codec;
  82. AVCodecContext *c= NULL;
  83. AVFrame *frame;
  84. AVPacket pkt;
  85. int i, j, k, ret, got_output;
  86. FILE *f;
  87. uint16_t *samples;
  88. float t, tincr;
  89. if (argc <= 1) {
  90. fprintf(stderr, "Usage: %s <output file>\n", argv[0]);
  91. return 0;
  92. }
  93. filename = argv[1];
  94. /* register all the codecs */
  95. avcodec_register_all();
  96. /* find the MP2 encoder */
  97. codec = avcodec_find_encoder(AV_CODEC_ID_MP2);
  98. if (!codec) {
  99. fprintf(stderr, "codec not found\n");
  100. exit(1);
  101. }
  102. c = avcodec_alloc_context3(codec);
  103. /* put sample parameters */
  104. c->bit_rate = 64000;
  105. /* check that the encoder supports s16 pcm input */
  106. c->sample_fmt = AV_SAMPLE_FMT_S16;
  107. if (!check_sample_fmt(codec, c->sample_fmt)) {
  108. fprintf(stderr, "encoder does not support %s",
  109. av_get_sample_fmt_name(c->sample_fmt));
  110. exit(1);
  111. }
  112. /* select other audio parameters supported by the encoder */
  113. c->sample_rate = select_sample_rate(codec);
  114. c->channel_layout = select_channel_layout(codec);
  115. c->channels = av_get_channel_layout_nb_channels(c->channel_layout);
  116. /* open it */
  117. if (avcodec_open2(c, codec, NULL) < 0) {
  118. fprintf(stderr, "could not open codec\n");
  119. exit(1);
  120. }
  121. f = fopen(filename, "wb");
  122. if (!f) {
  123. fprintf(stderr, "could not open %s\n", filename);
  124. exit(1);
  125. }
  126. /* frame containing input raw audio */
  127. frame = av_frame_alloc();
  128. if (!frame) {
  129. fprintf(stderr, "could not allocate audio frame\n");
  130. exit(1);
  131. }
  132. frame->nb_samples = c->frame_size;
  133. frame->format = c->sample_fmt;
  134. frame->channel_layout = c->channel_layout;
  135. /* allocate the data buffers */
  136. ret = av_frame_get_buffer(frame, 0);
  137. if (ret < 0) {
  138. fprintf(stderr, "could not allocate audio data buffers\n");
  139. exit(1);
  140. }
  141. /* encode a single tone sound */
  142. t = 0;
  143. tincr = 2 * M_PI * 440.0 / c->sample_rate;
  144. for(i=0;i<200;i++) {
  145. av_init_packet(&pkt);
  146. pkt.data = NULL; // packet data will be allocated by the encoder
  147. pkt.size = 0;
  148. /* make sure the frame is writable -- makes a copy if the encoder
  149. * kept a reference internally */
  150. ret = av_frame_make_writable(frame);
  151. if (ret < 0)
  152. exit(1);
  153. samples = (uint16_t*)frame->data[0];
  154. for (j = 0; j < c->frame_size; j++) {
  155. samples[2*j] = (int)(sin(t) * 10000);
  156. for (k = 1; k < c->channels; k++)
  157. samples[2*j + k] = samples[2*j];
  158. t += tincr;
  159. }
  160. /* encode the samples */
  161. ret = avcodec_encode_audio2(c, &pkt, frame, &got_output);
  162. if (ret < 0) {
  163. fprintf(stderr, "error encoding audio frame\n");
  164. exit(1);
  165. }
  166. if (got_output) {
  167. fwrite(pkt.data, 1, pkt.size, f);
  168. av_packet_unref(&pkt);
  169. }
  170. }
  171. fclose(f);
  172. av_frame_free(&frame);
  173. avcodec_free_context(&c);
  174. }