History

Merge remote-tracking branch 'qatar/master'

* qatar/master: (23 commits) build: cosmetics: Reorder some lists in a more logical fashion x86: pngdsp: Fix assembly for OS/2 fate: add test for RTjpeg in nuv with frameheader rtmp: send check_bw as notification g723_1: clip argument for 15-bit version of normalize_bits() g723_1: use all LPC vectors in formant postfilter id3v2: Support v2.2 PIC avplay: fix build with lavfi disabled. avconv: split configuring filter configuration to a separate file. avconv: split option parsing into a separate file. mpc8: do not leave padding after last frame in buffer for the next decode call mpegaudioenc: list supported channel layouts. mpegaudiodec: don't print an error on > 1 frame in a packet. api-example: update to new audio encoding API. configure: add --enable/disable-random option doc: cygwin: Update list of FATE package requirements build: Remove all installed headers and header directories on uninstall build: change checkheaders to use regular build rules rtmp: Add a new option 'rtmp_subscribe' rtmp: Add support for subscribing live streams ... Conflicts: Makefile common.mak configure doc/examples/decoding_encoding.c ffmpeg.c libavcodec/g723_1.c libavcodec/mpegaudiodec.c libavcodec/x86/pngdsp.asm libavformat/version.h library.mak tests/fate/video.mak

Michael Niedermayer committed 14 years ago

Commit 11a10332 parents 4270d8c 5864eb427 files · +6014 −5660Browse files

Showing the changes to doc/examples/decoding_encoding.c (1 of 27 files). Show all

Unified · Split

doc/examples/decoding_encoding.c+119−17
@@ -34,6 +34,7 @@ · 33 unmodified lines
3434 #include <libavutil/imgutils.h>
3535 #include <libavutil/opt.h>
3636 #include <libavcodec/avcodec.h>
37+#include <libavutil/audioconvert.h>
3738 #include <libavutil/mathematics.h>
3839 #include <libavutil/samplefmt.h>
3940
@@ -41,6 +42,59 @@ · 1 unmodified line
4142 #define AUDIO_INBUF_SIZE 20480
4243 #define AUDIO_REFILL_THRESH 4096
4344
45+/* check that a given sample format is supported by the encoder */
46+static int check_sample_fmt(AVCodec *codec, enum AVSampleFormat sample_fmt)
47+{
48+ const enum AVSampleFormat *p = codec->sample_fmts;
49+
50+ while (*p != AV_SAMPLE_FMT_NONE) {
51+ if (*p == sample_fmt)
52+ return 1;
53+ p++;
54+ }
55+ return 0;
56+}
57+
58+/* just pick the highest supported samplerate */
59+static int select_sample_rate(AVCodec *codec)
60+{
61+ const int *p;
62+ int best_samplerate = 0;
63+
64+ if (!codec->supported_samplerates)
65+ return 44100;
66+
67+ p = codec->supported_samplerates;
68+ while (*p) {
69+ best_samplerate = FFMAX(*p, best_samplerate);
70+ p++;
71+ }
72+ return best_samplerate;
73+}
74+
75+/* select layout with the highest channel count */
76+static int select_channel_layout(AVCodec *codec)
77+{
78+ const uint64_t *p;
79+ uint64_t best_ch_layout = 0;
80+ int best_nb_channells = 0;
81+
82+ if (!codec->channel_layouts)
83+ return AV_CH_LAYOUT_STEREO;
84+
85+ p = codec->channel_layouts;
86+ while (*p) {
87+ int nb_channels = av_get_channel_layout_nb_channels(*p);
88+
89+ if (nb_channels > best_nb_channells) {
90+ best_ch_layout = *p;
91+ best_nb_channells = nb_channels;
92+ }
93+ p++;
94+ }
95+ return best_ch_layout;
96+}
97+
4498 /*
4599 * Audio encoding example
46100 */
@@ -48,11 +102,13 @@ static void audio_encode_example(const char *filename) · 1 unmodified line
48102 {
49103 AVCodec *codec;
50104 AVCodecContext *c= NULL;
51− int frame_size, i, j, out_size, outbuf_size;
105+ AVFrame *frame;
106+ AVPacket pkt;
107+ int i, j, k, ret, got_output;
108+ int buffer_size;
52109 FILE *f;
53− short *samples;
110+ uint16_t *samples;
54111 float t, tincr;
55− uint8_t *outbuf;
56112
57113 printf("Encode audio file %s\n", filename);
58114
@@ -67,9 +123,19 @@ static void audio_encode_example(const char *filename) · 8 unmodified lines
67123
68124 /* put sample parameters */
69125 c->bit_rate = 64000;
70− c->sample_rate = 44100;
71− c->channels = 2;
126+
127+ /* check that the encoder supports s16 pcm input */
72128 c->sample_fmt = AV_SAMPLE_FMT_S16;
129+ if (!check_sample_fmt(codec, c->sample_fmt)) {
130+ fprintf(stderr, "encoder does not support %s",
131+ av_get_sample_fmt_name(c->sample_fmt));
132+ exit(1);
133+ }
134+
135+ /* select other audio parameters supported by the encoder */
136+ c->sample_rate = select_sample_rate(codec);
137+ c->channel_layout = select_channel_layout(codec);
138+ c->channels = av_get_channel_layout_nb_channels(c->channel_layout);
73139
74140 /* open it */
75141 if (avcodec_open2(c, codec, NULL) < 0) {
@@ -77,35 +143,71 @@ static void audio_encode_example(const char *filename) · 1 unmodified line
77143 exit(1);
78144 }
79145
80− /* the codec gives us the frame size, in samples */
81− frame_size = c->frame_size;
82− samples = malloc(frame_size * 2 * c->channels);
83− outbuf_size = 10000;
84− outbuf = malloc(outbuf_size);
85−
86146 f = fopen(filename, "wb");
87147 if (!f) {
88148 fprintf(stderr, "could not open %s\n", filename);
89149 exit(1);
90150 }
91151
152+ /* frame containing input raw audio */
153+ frame = avcodec_alloc_frame();
154+ if (!frame) {
155+ fprintf(stderr, "could not allocate audio frame\n");
156+ exit(1);
157+ }
158+
159+ frame->nb_samples = c->frame_size;
160+ frame->format = c->sample_fmt;
161+ frame->channel_layout = c->channel_layout;
162+
163+ /* the codec gives us the frame size, in samples,
164+ * we calculate the size of the samples buffer in bytes */
165+ buffer_size = av_samples_get_buffer_size(NULL, c->channels, c->frame_size,
166+ c->sample_fmt, 0);
167+ samples = av_malloc(buffer_size);
168+ if (!samples) {
169+ fprintf(stderr, "could not allocate %d bytes for samples buffer\n",
170+ buffer_size);
171+ exit(1);
172+ }
173+ /* setup the data pointers in the AVFrame */
174+ ret = avcodec_fill_audio_frame(frame, c->channels, c->sample_fmt,
175+ (const uint8_t*)samples, buffer_size, 0);
176+ if (ret < 0) {
177+ fprintf(stderr, "could not setup audio frame\n");
178+ exit(1);
179+ }
180+
92181 /* encode a single tone sound */
93182 t = 0;
94183 tincr = 2 * M_PI * 440.0 / c->sample_rate;
95184 for(i=0;i<200;i++) {
96− for(j=0;j<frame_size;j++) {
185+ av_init_packet(&pkt);
186+ pkt.data = NULL; // packet data will be allocated by the encoder
187+ pkt.size = 0;
188+
189+ for (j = 0; j < c->frame_size; j++) {
97190 samples[2*j] = (int)(sin(t) * 10000);
98− samples[2*j+1] = samples[2*j];
191+
192+ for (k = 1; k < c->channels; k++)
193+ samples[2*j + k] = samples[2*j];
99194 t += tincr;
100195 }
101196 /* encode the samples */
102− out_size = avcodec_encode_audio(c, outbuf, outbuf_size, samples);
103− fwrite(outbuf, 1, out_size, f);
197+ ret = avcodec_encode_audio2(c, &pkt, frame, &got_output);
198+ if (ret < 0) {
199+ fprintf(stderr, "error encoding audio frame\n");
200+ exit(1);
201+ }
202+ if (got_output) {
203+ fwrite(pkt.data, 1, pkt.size, f);
204+ av_free_packet(&pkt);
205+ }
104206 }
105207 fclose(f);
106− free(outbuf);
107− free(samples);
108208
209+ av_freep(&samples);
210+ av_freep(&frame);
109211 avcodec_close(c);
110212 av_free(c);
111213 }

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t