Note that libavcodec only handles codecs (MPEG, MPEG-4, etc...), not file formats (AVI, VOB, MP4, MOV, MKV, MXF, FLV, MPEG-TS, MPEG-PS, etc...).See library 'libavformat' for the format handling
#include <math.h>
#define INBUF_SIZE 4096
#define AUDIO_INBUF_SIZE 20480
#define AUDIO_REFILL_THRESH 4096
{
if (*p == sample_fmt)
return 1;
p++;
}
return 0;
}
{
const int *p;
int best_samplerate = 0;
return 44100;
while (*p) {
best_samplerate =
FFMAX(*p, best_samplerate);
p++;
}
return best_samplerate;
}
{
const uint64_t *p;
uint64_t best_ch_layout = 0;
int best_nb_channels = 0;
while (*p) {
if (nb_channels > best_nb_channels) {
best_ch_layout = *p;
}
p++;
}
return best_ch_layout;
}
{
int i, j, k, ret, got_output;
int buffer_size;
FILE *f;
uint16_t *samples;
float t, tincr;
printf("Encode audio file %s\n", filename);
if (!codec) {
fprintf(stderr, "Codec not found\n");
exit(1);
}
if (!c) {
fprintf(stderr, "Could not allocate audio codec context\n");
exit(1);
}
fprintf(stderr, "Encoder does not support sample format %s",
exit(1);
}
fprintf(stderr, "Could not open codec\n");
exit(1);
}
f = fopen(filename, "wb");
if (!f) {
fprintf(stderr, "Could not open %s\n", filename);
exit(1);
}
if (!frame) {
fprintf(stderr, "Could not allocate audio frame\n");
exit(1);
}
if (buffer_size < 0) {
fprintf(stderr, "Could not get sample buffer size\n");
exit(1);
}
if (!samples) {
fprintf(stderr, "Could not allocate %d bytes for samples buffer\n",
buffer_size);
exit(1);
}
(
const uint8_t*)samples, buffer_size, 0);
if (ret < 0) {
fprintf(stderr, "Could not setup audio frame\n");
exit(1);
}
t = 0;
for (i = 0; i < 200; i++) {
samples[2*j] = (
int)(sin(t) * 10000);
samples[2*j + k] = samples[2*j];
t += tincr;
}
if (ret < 0) {
fprintf(stderr, "Error encoding audio frame\n");
exit(1);
}
if (got_output) {
}
}
for (got_output = 1; got_output; i++) {
if (ret < 0) {
fprintf(stderr, "Error encoding frame\n");
exit(1);
}
if (got_output) {
}
}
fclose(f);
}
{
printf("Decode audio file %s to %s\n", filename, outfilename);
if (!codec) {
fprintf(stderr, "Codec not found\n");
exit(1);
}
if (!c) {
fprintf(stderr, "Could not allocate audio codec context\n");
exit(1);
}
fprintf(stderr, "Could not open codec\n");
exit(1);
}
f = fopen(filename, "rb");
if (!f) {
fprintf(stderr, "Could not open %s\n", filename);
exit(1);
}
outfile = fopen(outfilename, "wb");
if (!outfile) {
exit(1);
}
int got_frame = 0;
if (!decoded_frame) {
fprintf(stderr, "Could not allocate audio frame\n");
exit(1);
}
}
if (len < 0) {
fprintf(stderr, "Error while decoding\n");
exit(1);
}
if (got_frame) {
if (data_size < 0) {
fprintf(stderr, "Failed to calculate data size\n");
exit(1);
}
fwrite(decoded_frame->
data[ch] + data_size*i, 1, data_size, outfile);
}
if (len > 0)
}
}
fclose(outfile);
fclose(f);
}
{
int i, ret, x, y, got_output;
FILE *f;
uint8_t endcode[] = { 0, 0, 1, 0xb7 };
printf("Encode video file %s\n", filename);
if (!codec) {
fprintf(stderr, "Codec not found\n");
exit(1);
}
if (!c) {
fprintf(stderr, "Could not allocate video codec context\n");
exit(1);
}
fprintf(stderr, "Could not open codec\n");
exit(1);
}
f = fopen(filename, "wb");
if (!f) {
fprintf(stderr, "Could not open %s\n", filename);
exit(1);
}
if (!frame) {
fprintf(stderr, "Could not allocate video frame\n");
exit(1);
}
if (ret < 0) {
fprintf(stderr, "Could not allocate raw picture buffer\n");
exit(1);
}
for (i = 0; i < 25; i++) {
fflush(stdout);
for (y = 0; y < c->
height; y++) {
for (x = 0; x < c->
width; x++) {
}
}
for (y = 0; y < c->
height/2; y++) {
for (x = 0; x < c->
width/2; x++) {
frame->
data[1][y * frame->
linesize[1] + x] = 128 + y + i * 2;
frame->
data[2][y * frame->
linesize[2] + x] = 64 + x + i * 5;
}
}
if (ret < 0) {
fprintf(stderr, "Error encoding frame\n");
exit(1);
}
if (got_output) {
printf(
"Write frame %3d (size=%5d)\n", i, pkt.
size);
}
}
for (got_output = 1; got_output; i++) {
fflush(stdout);
if (ret < 0) {
fprintf(stderr, "Error encoding frame\n");
exit(1);
}
if (got_output) {
printf(
"Write frame %3d (size=%5d)\n", i, pkt.
size);
}
}
fwrite(endcode, 1, sizeof(endcode), f);
fclose(f);
printf("\n");
}
char *filename)
{
FILE *f;
int i;
f = fopen(filename,"w");
fprintf(f, "P5\n%d %d\n%d\n", xsize, ysize, 255);
for (i = 0; i < ysize; i++)
fwrite(buf + i * wrap, 1, xsize, f);
fclose(f);
}
{
char buf[1024];
if (len < 0) {
fprintf(stderr, "Error while decoding frame %d\n", *frame_count);
}
if (got_frame) {
printf("Saving %sframe %3d\n", last ? "last " : "", *frame_count);
fflush(stdout);
snprintf(buf,
sizeof(buf), outfilename, *frame_count);
(*frame_count)++;
}
}
return 0;
}
{
int frame_count;
FILE *f;
printf("Decode video file %s to %s\n", filename, outfilename);
if (!codec) {
fprintf(stderr, "Codec not found\n");
exit(1);
}
if (!c) {
fprintf(stderr, "Could not allocate video codec context\n");
exit(1);
}
fprintf(stderr, "Could not open codec\n");
exit(1);
}
f = fopen(filename, "rb");
if (!f) {
fprintf(stderr, "Could not open %s\n", filename);
exit(1);
}
if (!frame) {
fprintf(stderr, "Could not allocate video frame\n");
exit(1);
}
frame_count = 0;
for (;;) {
break;
exit(1);
}
fclose(f);
printf("\n");
}
int main(
int argc,
char **argv)
{
const char *output_type;
if (argc < 2) {
printf("usage: %s output_type\n"
"API example program to decode/encode a media stream with libavcodec.\n"
"This program generates a synthetic stream and encodes it to a file\n"
"named test.h264, test.mp2 or test.mpg depending on output_type.\n"
"The encoded stream is then decoded and written to a raw data output.\n"
"output_type must be chosen between 'h264', 'mp2', 'mpg'.\n",
argv[0]);
return 1;
}
output_type = argv[1];
if (!strcmp(output_type, "h264")) {
} else if (!strcmp(output_type, "mp2")) {
} else if (!strcmp(output_type, "mpg")) {
} else {
fprintf(stderr, "Invalid output type '%s', choose between 'h264', 'mp2', or 'mpg'\n",
output_type);
return 1;
}
return 0;
}