* 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
Showing the changes to doc/examples/decoding_encoding.c (1 of 27 files). Show all
Unified · Split
| @@ -34,6 +34,7 @@ · 33 unmodified lines | ||
| 34 | 34 | #include <libavutil/imgutils.h> |
| 35 | 35 | #include <libavutil/opt.h> |
| 36 | 36 | #include <libavcodec/avcodec.h> |
| 37 | +#include <libavutil/audioconvert.h> | |
| 37 | 38 | #include <libavutil/mathematics.h> |
| 38 | 39 | #include <libavutil/samplefmt.h> |
| 39 | 40 | |
| @@ -41,6 +42,59 @@ · 1 unmodified line | ||
| 41 | 42 | #define AUDIO_INBUF_SIZE 20480 |
| 42 | 43 | #define AUDIO_REFILL_THRESH 4096 |
| 43 | 44 | |
| 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 | + | |
| 44 | 98 | /* |
| 45 | 99 | * Audio encoding example |
| 46 | 100 | */ |
| @@ -48,11 +102,13 @@ static void audio_encode_example(const char *filename) · 1 unmodified line | ||
| 48 | 102 | { |
| 49 | 103 | AVCodec *codec; |
| 50 | 104 | 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; | |
| 52 | 109 | FILE *f; |
| 53 | − short *samples; | |
| 110 | + uint16_t *samples; | |
| 54 | 111 | float t, tincr; |
| 55 | − uint8_t *outbuf; | |
| 56 | 112 | |
| 57 | 113 | printf("Encode audio file %s\n", filename); |
| 58 | 114 | |
| @@ -67,9 +123,19 @@ static void audio_encode_example(const char *filename) · 8 unmodified lines | ||
| 67 | 123 | |
| 68 | 124 | /* put sample parameters */ |
| 69 | 125 | c->bit_rate = 64000; |
| 70 | − c->sample_rate = 44100; | |
| 71 | − c->channels = 2; | |
| 126 | + | |
| 127 | + /* check that the encoder supports s16 pcm input */ | |
| 72 | 128 | 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); | |
| 73 | 139 | |
| 74 | 140 | /* open it */ |
| 75 | 141 | if (avcodec_open2(c, codec, NULL) < 0) { |
| @@ -77,35 +143,71 @@ static void audio_encode_example(const char *filename) · 1 unmodified line | ||
| 77 | 143 | exit(1); |
| 78 | 144 | } |
| 79 | 145 | |
| 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 | − | |
| 86 | 146 | f = fopen(filename, "wb"); |
| 87 | 147 | if (!f) { |
| 88 | 148 | fprintf(stderr, "could not open %s\n", filename); |
| 89 | 149 | exit(1); |
| 90 | 150 | } |
| 91 | 151 | |
| 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 | + | |
| 92 | 181 | /* encode a single tone sound */ |
| 93 | 182 | t = 0; |
| 94 | 183 | tincr = 2 * M_PI * 440.0 / c->sample_rate; |
| 95 | 184 | 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++) { | |
| 97 | 190 | 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]; | |
| 99 | 194 | t += tincr; |
| 100 | 195 | } |
| 101 | 196 | /* 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 | + } | |
| 104 | 206 | } |
| 105 | 207 | fclose(f); |
| 106 | − free(outbuf); | |
| 107 | − free(samples); | |
| 108 | 208 | |
| 209 | + av_freep(&samples); | |
| 210 | + av_freep(&frame); | |
| 109 | 211 | avcodec_close(c); |
| 110 | 212 | av_free(c); |
| 111 | 213 | } |