/*
* Copyright (c) 2001 Fabrice Bellard
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
/**
* @file
* audio encoding with libavcodec API example.
*
* @example encode_audio.c
*/
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
/* check that a given sample format is supported by the encoder */
{
if (*p == sample_fmt)
return 1;
p++;
}
return 0;
}
/* just pick the highest supported samplerate */
{
const int *p;
int best_samplerate = 0;
return 44100;
while (*p) {
if (!best_samplerate ||
abs(44100 - *p) <
abs(44100 - best_samplerate))
best_samplerate = *p;
p++;
}
return best_samplerate;
}
/* select layout with the highest channel count */
{
int best_nb_channels = 0;
if (nb_channels > best_nb_channels) {
best_ch_layout = p;
best_nb_channels = nb_channels;
}
p++;
}
}
{
/* send the frame for encoding */
fprintf(stderr, "Error sending the frame to the encoder\n");
exit(1);
}
/* read all the available output packets (in general there may be any
* number of them */
return;
fprintf(stderr, "Error encoding audio frame\n");
exit(1);
}
}
}
int main(
int argc,
char **argv)
{
const char *filename;
float t, tincr;
if (argc <= 1) {
fprintf(stderr, "Usage: %s <output file>\n", argv[0]);
return 0;
}
filename = argv[1];
/* find the MP2 encoder */
if (!codec) {
fprintf(stderr, "Codec not found\n");
exit(1);
}
fprintf(stderr, "Could not allocate audio codec context\n");
exit(1);
}
/* put sample parameters */
/* check that the encoder supports s16 pcm input */
fprintf(stderr, "Encoder does not support sample format %s",
exit(1);
}
/* select other audio parameters supported by the encoder */
exit(1);
/* open it */
fprintf(stderr, "Could not open codec\n");
exit(1);
}
f = fopen(filename,
"wb");
fprintf(stderr, "Could not open %s\n", filename);
exit(1);
}
/* packet for holding encoded output */
fprintf(stderr, "could not allocate the packet\n");
exit(1);
}
/* frame containing input raw audio */
fprintf(stderr, "Could not allocate audio frame\n");
exit(1);
}
frame->nb_samples =
c->frame_size;
frame->format =
c->sample_fmt;
exit(1);
/* allocate the data buffers */
fprintf(stderr, "Could not allocate audio data buffers\n");
exit(1);
}
/* encode a single tone sound */
t = 0;
tincr = 2 *
M_PI * 440.0 /
c->sample_rate;
for (
i = 0;
i < 200;
i++) {
/* make sure the frame is writable -- makes a copy if the encoder
* kept a reference internally */
exit(1);
for (j = 0; j <
c->frame_size; j++) {
for (k = 1; k <
c->ch_layout.nb_channels; k++)
t += tincr;
}
}
/* flush the encoder */
return 0;
}