43 #define INBUF_SIZE 4096
44 #define AUDIO_INBUF_SIZE 20480
45 #define AUDIO_REFILL_THRESH 4096
64 int best_samplerate = 0;
71 best_samplerate =
FFMAX(*p, best_samplerate);
74 return best_samplerate;
81 uint64_t best_ch_layout = 0;
82 int best_nb_channells = 0;
91 if (nb_channels > best_nb_channells) {
97 return best_ch_layout;
109 int i, j, k, ret, got_output;
115 printf(
"Encode audio file %s\n", filename);
120 fprintf(stderr,
"Codec not found\n");
126 fprintf(stderr,
"Could not allocate audio codec context\n");
136 fprintf(stderr,
"Encoder does not support sample format %s",
148 fprintf(stderr,
"Could not open codec\n");
152 f = fopen(filename,
"wb");
154 fprintf(stderr,
"Could not open %s\n", filename);
161 fprintf(stderr,
"Could not allocate audio frame\n");
175 fprintf(stderr,
"Could not allocate %d bytes for samples buffer\n",
181 (
const uint8_t*)samples, buffer_size, 0);
183 fprintf(stderr,
"Could not setup audio frame\n");
196 samples[2*j] = (int)(sin(t) * 10000);
199 samples[2*j + k] = samples[2*j];
205 fprintf(stderr,
"Error encoding audio frame\n");
215 for (got_output = 1; got_output; i++) {
218 fprintf(stderr,
"Error encoding frame\n");
250 printf(
"Decode audio file %s to %s\n", filename, outfilename);
255 fprintf(stderr,
"Codec not found\n");
261 fprintf(stderr,
"Could not allocate audio codec context\n");
267 fprintf(stderr,
"Could not open codec\n");
271 f = fopen(filename,
"rb");
273 fprintf(stderr,
"Could not open %s\n", filename);
276 outfile = fopen(outfilename,
"wb");
286 while (avpkt.
size > 0) {
289 if (!decoded_frame) {
291 fprintf(stderr,
"Could not allocate audio frame\n");
299 fprintf(stderr,
"Error while decoding\n");
307 fwrite(decoded_frame->
data[0], 1, data_size, outfile);
318 memmove(inbuf, avpkt.
data, avpkt.
size);
320 len = fread(avpkt.
data + avpkt.
size, 1,
342 int i, ret, x,
y, got_output;
346 uint8_t endcode[] = { 0, 0, 1, 0xb7 };
348 printf(
"Encode video file %s\n", filename);
353 fprintf(stderr,
"Codec not found\n");
359 fprintf(stderr,
"Could not allocate video codec context\n");
379 fprintf(stderr,
"Could not open codec\n");
383 f = fopen(filename,
"wb");
385 fprintf(stderr,
"Could not open %s\n", filename);
391 fprintf(stderr,
"Could not allocate video frame\n");
403 fprintf(stderr,
"Could not allocate raw picture buffer\n");
416 for(y=0;y<c->
height;y++) {
417 for(x=0;x<c->
width;x++) {
418 frame->
data[0][y * frame->
linesize[0] + x] = x + y + i * 3;
423 for(y=0;y<c->
height/2;y++) {
424 for(x=0;x<c->
width/2;x++) {
425 frame->
data[1][y * frame->
linesize[1] + x] = 128 + y + i * 2;
426 frame->
data[2][y * frame->
linesize[2] + x] = 64 + x + i * 5;
435 fprintf(stderr,
"Error encoding frame\n");
440 printf(
"Write frame %3d (size=%5d)\n", i, pkt.
size);
447 for (got_output = 1; got_output; i++) {
452 fprintf(stderr,
"Error encoding frame\n");
457 printf(
"Write frame %3d (size=%5d)\n", i, pkt.
size);
464 fwrite(endcode, 1,
sizeof(endcode), f);
478 static void pgm_save(
unsigned char *buf,
int wrap,
int xsize,
int ysize,
484 f=fopen(filename,
"w");
485 fprintf(f,
"P5\n%d %d\n%d\n",xsize,ysize,255);
487 fwrite(buf + i * wrap,1,xsize,f);
499 fprintf(stderr,
"Error while decoding frame %d\n", *frame_count);
503 printf(
"Saving %sframe %3d\n", last ?
"last " :
"", *frame_count);
507 snprintf(buf,
sizeof(buf), outfilename, *frame_count);
534 printf(
"Decode video file %s to %s\n", filename, outfilename);
539 fprintf(stderr,
"Codec not found\n");
545 fprintf(stderr,
"Could not allocate video codec context\n");
558 fprintf(stderr,
"Could not open codec\n");
562 f = fopen(filename,
"rb");
564 fprintf(stderr,
"Could not open %s\n", filename);
570 fprintf(stderr,
"Could not allocate video frame\n");
596 while (avpkt.
size > 0)
616 int main(
int argc,
char **argv)
618 const char *output_type;
624 printf(
"usage: %s output_type\n"
625 "API example program to decode/encode a media stream with libavcodec.\n"
626 "This program generates a synthetic stream and encodes it to a file\n"
627 "named test.h264, test.mp2 or test.mpg depending on output_type.\n"
628 "The encoded stream is then decoded and written to a raw data output.\n"
629 "output_type must be choosen between 'h264', 'mp2', 'mpg'.\n",
633 output_type = argv[1];
635 if (!strcmp(output_type,
"h264")) {
637 }
else if (!strcmp(output_type,
"mp2")) {
640 }
else if (!strcmp(output_type,
"mpg")) {
644 fprintf(stderr,
"Invalid output type '%s', choose between 'h264', 'mp2', or 'mpg'\n",