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.

233 lines
6.0KB

  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. static void encode(AVCodecContext *ctx, AVFrame *frame, AVPacket *pkt,
  79. FILE *output)
  80. {
  81. int ret;
  82. /* send the frame for encoding */
  83. ret = avcodec_send_frame(ctx, frame);
  84. if (ret < 0) {
  85. fprintf(stderr, "error sending the frame to the encoder\n");
  86. exit(1);
  87. }
  88. /* read all the available output packets (in general there may be any
  89. * number of them */
  90. while (ret >= 0) {
  91. ret = avcodec_receive_packet(ctx, pkt);
  92. if (ret == AVERROR(EAGAIN) || ret == AVERROR_EOF)
  93. return;
  94. else if (ret < 0) {
  95. fprintf(stderr, "error encoding audio frame\n");
  96. exit(1);
  97. }
  98. fwrite(pkt->data, 1, pkt->size, output);
  99. av_packet_unref(pkt);
  100. }
  101. }
  102. int main(int argc, char **argv)
  103. {
  104. const char *filename;
  105. const AVCodec *codec;
  106. AVCodecContext *c= NULL;
  107. AVFrame *frame;
  108. AVPacket *pkt;
  109. int i, j, k, ret;
  110. FILE *f;
  111. uint16_t *samples;
  112. float t, tincr;
  113. if (argc <= 1) {
  114. fprintf(stderr, "Usage: %s <output file>\n", argv[0]);
  115. return 0;
  116. }
  117. filename = argv[1];
  118. /* register all the codecs */
  119. avcodec_register_all();
  120. /* find the MP2 encoder */
  121. codec = avcodec_find_encoder(AV_CODEC_ID_MP2);
  122. if (!codec) {
  123. fprintf(stderr, "codec not found\n");
  124. exit(1);
  125. }
  126. c = avcodec_alloc_context3(codec);
  127. /* put sample parameters */
  128. c->bit_rate = 64000;
  129. /* check that the encoder supports s16 pcm input */
  130. c->sample_fmt = AV_SAMPLE_FMT_S16;
  131. if (!check_sample_fmt(codec, c->sample_fmt)) {
  132. fprintf(stderr, "encoder does not support %s",
  133. av_get_sample_fmt_name(c->sample_fmt));
  134. exit(1);
  135. }
  136. /* select other audio parameters supported by the encoder */
  137. c->sample_rate = select_sample_rate(codec);
  138. c->channel_layout = select_channel_layout(codec);
  139. c->channels = av_get_channel_layout_nb_channels(c->channel_layout);
  140. /* open it */
  141. if (avcodec_open2(c, codec, NULL) < 0) {
  142. fprintf(stderr, "could not open codec\n");
  143. exit(1);
  144. }
  145. f = fopen(filename, "wb");
  146. if (!f) {
  147. fprintf(stderr, "could not open %s\n", filename);
  148. exit(1);
  149. }
  150. /* packet for holding encoded output */
  151. pkt = av_packet_alloc();
  152. if (!pkt) {
  153. fprintf(stderr, "could not allocate the packet\n");
  154. exit(1);
  155. }
  156. /* frame containing input raw audio */
  157. frame = av_frame_alloc();
  158. if (!frame) {
  159. fprintf(stderr, "could not allocate audio frame\n");
  160. exit(1);
  161. }
  162. frame->nb_samples = c->frame_size;
  163. frame->format = c->sample_fmt;
  164. frame->channel_layout = c->channel_layout;
  165. /* allocate the data buffers */
  166. ret = av_frame_get_buffer(frame, 0);
  167. if (ret < 0) {
  168. fprintf(stderr, "could not allocate audio data buffers\n");
  169. exit(1);
  170. }
  171. /* encode a single tone sound */
  172. t = 0;
  173. tincr = 2 * M_PI * 440.0 / c->sample_rate;
  174. for(i=0;i<200;i++) {
  175. /* make sure the frame is writable -- makes a copy if the encoder
  176. * kept a reference internally */
  177. ret = av_frame_make_writable(frame);
  178. if (ret < 0)
  179. exit(1);
  180. samples = (uint16_t*)frame->data[0];
  181. for (j = 0; j < c->frame_size; j++) {
  182. samples[2*j] = (int)(sin(t) * 10000);
  183. for (k = 1; k < c->channels; k++)
  184. samples[2*j + k] = samples[2*j];
  185. t += tincr;
  186. }
  187. encode(c, frame, pkt, f);
  188. }
  189. /* flush the encoder */
  190. encode(c, NULL, pkt, f);
  191. fclose(f);
  192. av_frame_free(&frame);
  193. av_packet_free(&pkt);
  194. avcodec_free_context(&c);
  195. }