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.

212 lines
5.7KB

  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(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(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(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. AVCodec *codec;
  82. AVCodecContext *c= NULL;
  83. AVFrame *frame;
  84. AVPacket pkt;
  85. int i, j, k, ret, got_output;
  86. int buffer_size;
  87. FILE *f;
  88. uint16_t *samples;
  89. float t, tincr;
  90. if (argc <= 1) {
  91. fprintf(stderr, "Usage: %s <output file>\n", argv[0]);
  92. return 0;
  93. }
  94. filename = argv[1];
  95. /* register all the codecs */
  96. avcodec_register_all();
  97. /* find the MP2 encoder */
  98. codec = avcodec_find_encoder(AV_CODEC_ID_MP2);
  99. if (!codec) {
  100. fprintf(stderr, "codec not found\n");
  101. exit(1);
  102. }
  103. c = avcodec_alloc_context3(codec);
  104. /* put sample parameters */
  105. c->bit_rate = 64000;
  106. /* check that the encoder supports s16 pcm input */
  107. c->sample_fmt = AV_SAMPLE_FMT_S16;
  108. if (!check_sample_fmt(codec, c->sample_fmt)) {
  109. fprintf(stderr, "encoder does not support %s",
  110. av_get_sample_fmt_name(c->sample_fmt));
  111. exit(1);
  112. }
  113. /* select other audio parameters supported by the encoder */
  114. c->sample_rate = select_sample_rate(codec);
  115. c->channel_layout = select_channel_layout(codec);
  116. c->channels = av_get_channel_layout_nb_channels(c->channel_layout);
  117. /* open it */
  118. if (avcodec_open2(c, codec, NULL) < 0) {
  119. fprintf(stderr, "could not open codec\n");
  120. exit(1);
  121. }
  122. f = fopen(filename, "wb");
  123. if (!f) {
  124. fprintf(stderr, "could not open %s\n", filename);
  125. exit(1);
  126. }
  127. /* frame containing input raw audio */
  128. frame = av_frame_alloc();
  129. if (!frame) {
  130. fprintf(stderr, "could not allocate audio frame\n");
  131. exit(1);
  132. }
  133. frame->nb_samples = c->frame_size;
  134. frame->format = c->sample_fmt;
  135. frame->channel_layout = c->channel_layout;
  136. /* the codec gives us the frame size, in samples,
  137. * we calculate the size of the samples buffer in bytes */
  138. buffer_size = av_samples_get_buffer_size(NULL, c->channels, c->frame_size,
  139. c->sample_fmt, 0);
  140. samples = av_malloc(buffer_size);
  141. if (!samples) {
  142. fprintf(stderr, "could not allocate %d bytes for samples buffer\n",
  143. buffer_size);
  144. exit(1);
  145. }
  146. /* setup the data pointers in the AVFrame */
  147. ret = avcodec_fill_audio_frame(frame, c->channels, c->sample_fmt,
  148. (const uint8_t*)samples, buffer_size, 0);
  149. if (ret < 0) {
  150. fprintf(stderr, "could not setup audio frame\n");
  151. exit(1);
  152. }
  153. /* encode a single tone sound */
  154. t = 0;
  155. tincr = 2 * M_PI * 440.0 / c->sample_rate;
  156. for(i=0;i<200;i++) {
  157. av_init_packet(&pkt);
  158. pkt.data = NULL; // packet data will be allocated by the encoder
  159. pkt.size = 0;
  160. for (j = 0; j < c->frame_size; j++) {
  161. samples[2*j] = (int)(sin(t) * 10000);
  162. for (k = 1; k < c->channels; k++)
  163. samples[2*j + k] = samples[2*j];
  164. t += tincr;
  165. }
  166. /* encode the samples */
  167. ret = avcodec_encode_audio2(c, &pkt, frame, &got_output);
  168. if (ret < 0) {
  169. fprintf(stderr, "error encoding audio frame\n");
  170. exit(1);
  171. }
  172. if (got_output) {
  173. fwrite(pkt.data, 1, pkt.size, f);
  174. av_packet_unref(&pkt);
  175. }
  176. }
  177. fclose(f);
  178. av_freep(&samples);
  179. av_frame_free(&frame);
  180. avcodec_free_context(&c);
  181. }