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_channels = 0;
91 if (nb_channels > best_nb_channels) {
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");
173 if (buffer_size < 0) {
174 fprintf(stderr,
"Could not get sample buffer size\n");
179 fprintf(stderr,
"Could not allocate %d bytes for samples buffer\n",
185 (
const uint8_t*)samples, buffer_size, 0);
187 fprintf(stderr,
"Could not setup audio frame\n");
194 for (i = 0; i < 200; i++) {
200 samples[2*j] = (int)(sin(t) * 10000);
203 samples[2*j + k] = samples[2*j];
209 fprintf(stderr,
"Error encoding audio frame\n");
219 for (got_output = 1; got_output; i++) {
222 fprintf(stderr,
"Error encoding frame\n");
254 printf(
"Decode audio file %s to %s\n", filename, outfilename);
259 fprintf(stderr,
"Codec not found\n");
265 fprintf(stderr,
"Could not allocate audio codec context\n");
271 fprintf(stderr,
"Could not open codec\n");
275 f = fopen(filename,
"rb");
277 fprintf(stderr,
"Could not open %s\n", filename);
280 outfile = fopen(outfilename,
"wb");
290 while (avpkt.
size > 0) {
293 if (!decoded_frame) {
295 fprintf(stderr,
"Could not allocate audio frame\n");
302 fprintf(stderr,
"Error while decoding\n");
312 fprintf(stderr,
"Failed to calculate data size\n");
315 fwrite(decoded_frame->
data[0], 1, data_size, outfile);
326 memmove(inbuf, avpkt.
data, avpkt.
size);
328 len = fread(avpkt.
data + avpkt.
size, 1,
350 int i,
ret, x,
y, got_output;
354 uint8_t endcode[] = { 0, 0, 1, 0xb7 };
356 printf(
"Encode video file %s\n", filename);
361 fprintf(stderr,
"Codec not found\n");
367 fprintf(stderr,
"Could not allocate video codec context\n");
393 fprintf(stderr,
"Could not open codec\n");
397 f = fopen(filename,
"wb");
399 fprintf(stderr,
"Could not open %s\n", filename);
405 fprintf(stderr,
"Could not allocate video frame\n");
417 fprintf(stderr,
"Could not allocate raw picture buffer\n");
422 for (i = 0; i < 25; i++) {
430 for (y = 0; y < c->
height; y++) {
431 for (x = 0; x < c->
width; x++) {
432 frame->
data[0][y * frame->
linesize[0] + x] = x + y + i * 3;
437 for (y = 0; y < c->
height/2; y++) {
438 for (x = 0; x < c->
width/2; x++) {
439 frame->
data[1][y * frame->
linesize[1] + x] = 128 + y + i * 2;
440 frame->
data[2][y * frame->
linesize[2] + x] = 64 + x + i * 5;
449 fprintf(stderr,
"Error encoding frame\n");
454 printf(
"Write frame %3d (size=%5d)\n", i, pkt.
size);
461 for (got_output = 1; got_output; i++) {
466 fprintf(stderr,
"Error encoding frame\n");
471 printf(
"Write frame %3d (size=%5d)\n", i, pkt.
size);
478 fwrite(endcode, 1,
sizeof(endcode), f);
498 f = fopen(filename,
"w");
499 fprintf(f,
"P5\n%d %d\n%d\n", xsize, ysize, 255);
500 for (i = 0; i < ysize; i++)
501 fwrite(buf + i * wrap, 1, xsize, f);
513 fprintf(stderr,
"Error while decoding frame %d\n", *frame_count);
517 printf(
"Saving %sframe %3d\n", last ?
"last " :
"", *frame_count);
521 snprintf(buf,
sizeof(buf), outfilename, *frame_count);
548 printf(
"Decode video file %s to %s\n", filename, outfilename);
553 fprintf(stderr,
"Codec not found\n");
559 fprintf(stderr,
"Could not allocate video codec context\n");
572 fprintf(stderr,
"Could not open codec\n");
576 f = fopen(filename,
"rb");
578 fprintf(stderr,
"Could not open %s\n", filename);
584 fprintf(stderr,
"Could not allocate video frame\n");
610 while (avpkt.
size > 0)
630 int main(
int argc,
char **argv)
632 const char *output_type;
638 printf(
"usage: %s output_type\n"
639 "API example program to decode/encode a media stream with libavcodec.\n"
640 "This program generates a synthetic stream and encodes it to a file\n"
641 "named test.h264, test.mp2 or test.mpg depending on output_type.\n"
642 "The encoded stream is then decoded and written to a raw data output.\n"
643 "output_type must be chosen between 'h264', 'mp2', 'mpg'.\n",
647 output_type = argv[1];
649 if (!strcmp(output_type,
"h264")) {
651 }
else if (!strcmp(output_type,
"mp2")) {
654 }
else if (!strcmp(output_type,
"mpg")) {
658 fprintf(stderr,
"Invalid output type '%s', choose between 'h264', 'mp2', or 'mpg'\n",