35 #ifdef HAVE_AV_CONFIG_H
36 #undef HAVE_AV_CONFIG_H
43 #define INBUF_SIZE 4096
44 #define AUDIO_INBUF_SIZE 20480
45 #define AUDIO_REFILL_THRESH 4096
54 int frame_size, i, j, out_size, outbuf_size;
60 printf(
"Audio encoding\n");
65 fprintf(stderr,
"codec not found\n");
77 if (avcodec_open(c, codec) < 0) {
78 fprintf(stderr,
"could not open codec\n");
86 outbuf =
malloc(outbuf_size);
88 f = fopen(filename,
"wb");
90 fprintf(stderr,
"could not open %s\n", filename);
98 for(j=0;j<frame_size;j++) {
99 samples[2*j] = (int)(sin(t) * 10000);
100 samples[2*j+1] = samples[2*j];
104 out_size = avcodec_encode_audio(c, outbuf, outbuf_size, samples);
105 fwrite(outbuf, 1, out_size, f);
130 printf(
"Audio decoding\n");
135 fprintf(stderr,
"codec not found\n");
142 if (avcodec_open(c, codec) < 0) {
143 fprintf(stderr,
"could not open codec\n");
147 f = fopen(filename,
"rb");
149 fprintf(stderr,
"could not open %s\n", filename);
152 outfile = fopen(outfilename,
"wb");
162 while (avpkt.
size > 0) {
165 if (!decoded_frame) {
167 fprintf(stderr,
"out of memory\n");
175 fprintf(stderr,
"Error while decoding\n");
183 fwrite(decoded_frame->
data[0], 1, data_size, outfile);
192 memmove(inbuf, avpkt.
data, avpkt.
size);
194 len = fread(avpkt.
data + avpkt.
size, 1,
216 int i, out_size,
size, x, y, outbuf_size;
219 uint8_t *outbuf, *picture_buf;
221 printf(
"Video encoding\n");
226 fprintf(stderr,
"codec not found\n");
245 if (avcodec_open(c, codec) < 0) {
246 fprintf(stderr,
"could not open codec\n");
250 f = fopen(filename,
"wb");
252 fprintf(stderr,
"could not open %s\n", filename);
257 outbuf_size = 100000;
258 outbuf =
malloc(outbuf_size);
260 picture_buf =
malloc((size * 3) / 2);
262 picture->
data[0] = picture_buf;
264 picture->
data[2] = picture->
data[1] + size / 4;
274 for(y=0;y<c->
height;y++) {
275 for(x=0;x<c->
width;x++) {
276 picture->
data[0][y * picture->
linesize[0] + x] = x + y + i * 3;
281 for(y=0;y<c->
height/2;y++) {
282 for(x=0;x<c->
width/2;x++) {
283 picture->
data[1][y * picture->
linesize[1] + x] = 128 + y + i * 2;
284 picture->
data[2][y * picture->
linesize[2] + x] = 64 + x + i * 5;
290 printf(
"encoding frame %3d (size=%5d)\n", i, out_size);
291 fwrite(outbuf, 1, out_size, f);
295 for(; out_size; i++) {
299 printf(
"write frame %3d (size=%5d)\n", i, out_size);
300 fwrite(outbuf, 1, out_size, f);
308 fwrite(outbuf, 1, 4, f);
323 static void pgm_save(
unsigned char *buf,
int wrap,
int xsize,
int ysize,
329 f=fopen(filename,
"w");
330 fprintf(f,
"P5\n%d %d\n%d\n",xsize,ysize,255);
332 fwrite(buf + i * wrap,1,xsize,f);
340 int frame, got_picture,
len;
352 printf(
"Video decoding\n");
357 fprintf(stderr,
"codec not found\n");
372 if (avcodec_open(c, codec) < 0) {
373 fprintf(stderr,
"could not open codec\n");
379 f = fopen(filename,
"rb");
381 fprintf(stderr,
"could not open %s\n", filename);
407 while (avpkt.
size > 0) {
410 fprintf(stderr,
"Error while decoding frame %d\n", frame);
414 printf(
"saving frame %3d\n", frame);
419 snprintf(buf,
sizeof(buf), outfilename, frame);
436 printf(
"saving last frame %3d\n", frame);
441 snprintf(buf,
sizeof(buf), outfilename, frame);
455 int main(
int argc,
char **argv)
457 const char *filename;
470 filename =
"/tmp/test.mpg";