Note that this library only handles codecs (mpeg, mpeg4, etc...), not file formats (avi, vob, etc...). See library 'libavformat' for the format handling
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#ifdef HAVE_AV_CONFIG_H
#undef HAVE_AV_CONFIG_H
#endif
#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_channells = 0;
while (*p) {
if (nb_channels > best_nb_channells) {
best_ch_layout = *p;
}
p++;
}
return best_ch_layout;
}
{
int i, j, k, ret, got_output;
int buffer_size;
FILE *f;
printf("Audio encoding\n");
if (!codec) {
fprintf(stderr, "codec not found\n");
exit(1);
}
fprintf(stderr, "encoder does not support %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 (!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];
}
if (ret < 0) {
fprintf(stderr, "error encoding audio frame\n");
exit(1);
}
if (got_output) {
}
}
fclose(f);
}
{
printf("Audio decoding\n");
if (!codec) {
fprintf(stderr, "codec not found\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, "out of memory\n");
exit(1);
}
} else
if (len < 0) {
fprintf(stderr, "Error while decoding\n");
exit(1);
}
if (got_frame) {
fwrite(decoded_frame->
data[0], 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("Video encoding\n");
if (!codec) {
fprintf(stderr, "codec not found\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 (ret < 0) {
fprintf(stderr, "could not alloc raw picture buffer\n");
exit(1);
}
for(i=0;i<25;i++) {
fflush(stdout);
for(x=0;x<c->
width;x++) {
picture->
data[0][y * picture->
linesize[0] + x] = x + y + i * 3;
}
}
for(x=0;x<c->
width/2;x++) {
picture->
data[1][y * picture->
linesize[1] + x] = 128 + y + i * 2;
picture->
data[2][y * picture->
linesize[2] + x] = 64 + x + i * 5;
}
}
if (ret < 0) {
fprintf(stderr, "error encoding frame\n");
exit(1);
}
if (got_output) {
printf(
"encoding 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(
"encoding frame %3d (size=%5d)\n", i, pkt.
size);
}
}
fwrite(endcode, 1, sizeof(endcode), f);
fclose(f);
printf("\n");
}
static void pgm_save(
unsigned char *buf,
int wrap,
int xsize,
int ysize,
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);
}
{
int frame, got_picture,
len;
FILE *f;
char buf[1024];
printf("Video decoding\n");
if (!codec) {
fprintf(stderr, "codec not found\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);
}
frame = 0;
for(;;) {
break;
if (len < 0) {
fprintf(stderr, "Error while decoding frame %d\n", frame);
exit(1);
}
if (got_picture) {
printf("saving frame %3d\n", frame);
fflush(stdout);
snprintf(buf, sizeof(buf), outfilename, frame);
frame++;
}
}
}
if (got_picture) {
printf("saving last frame %3d\n", frame);
fflush(stdout);
snprintf(buf, sizeof(buf), outfilename, frame);
frame++;
}
fclose(f);
printf("\n");
}
int main(
int argc,
char **argv)
{
const char *filename;
if (argc <= 1) {
filename = "/tmp/test.mpg";
} else {
filename = argv[1];
}
return 0;
}