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");
387 fprintf(stderr,
"Could not open codec\n");
391 f = fopen(filename,
"wb");
393 fprintf(stderr,
"Could not open %s\n", filename);
399 fprintf(stderr,
"Could not allocate video frame\n");
411 fprintf(stderr,
"Could not allocate raw picture buffer\n");
416 for (i = 0; i < 25; i++) {
424 for (y = 0; y < c->
height; y++) {
425 for (x = 0; x < c->
width; x++) {
426 frame->
data[0][y * frame->
linesize[0] + x] = x + y + i * 3;
431 for (y = 0; y < c->
height/2; y++) {
432 for (x = 0; x < c->
width/2; x++) {
433 frame->
data[1][y * frame->
linesize[1] + x] = 128 + y + i * 2;
434 frame->
data[2][y * frame->
linesize[2] + x] = 64 + x + i * 5;
443 fprintf(stderr,
"Error encoding frame\n");
448 printf(
"Write frame %3d (size=%5d)\n", i, pkt.
size);
455 for (got_output = 1; got_output; i++) {
460 fprintf(stderr,
"Error encoding frame\n");
465 printf(
"Write frame %3d (size=%5d)\n", i, pkt.
size);
472 fwrite(endcode, 1,
sizeof(endcode), f);
492 f = fopen(filename,
"w");
493 fprintf(f,
"P5\n%d %d\n%d\n", xsize, ysize, 255);
494 for (i = 0; i < ysize; i++)
495 fwrite(buf + i * wrap, 1, xsize, f);
507 fprintf(stderr,
"Error while decoding frame %d\n", *frame_count);
511 printf(
"Saving %sframe %3d\n", last ?
"last " :
"", *frame_count);
515 snprintf(buf,
sizeof(buf), outfilename, *frame_count);
542 printf(
"Decode video file %s to %s\n", filename, outfilename);
547 fprintf(stderr,
"Codec not found\n");
553 fprintf(stderr,
"Could not allocate video codec context\n");
566 fprintf(stderr,
"Could not open codec\n");
570 f = fopen(filename,
"rb");
572 fprintf(stderr,
"Could not open %s\n", filename);
578 fprintf(stderr,
"Could not allocate video frame\n");
604 while (avpkt.
size > 0)
624 int main(
int argc,
char **argv)
626 const char *output_type;
632 printf(
"usage: %s output_type\n"
633 "API example program to decode/encode a media stream with libavcodec.\n"
634 "This program generates a synthetic stream and encodes it to a file\n"
635 "named test.h264, test.mp2 or test.mpg depending on output_type.\n"
636 "The encoded stream is then decoded and written to a raw data output.\n"
637 "output_type must be choosen between 'h264', 'mp2', 'mpg'.\n",
641 output_type = argv[1];
643 if (!strcmp(output_type,
"h264")) {
645 }
else if (!strcmp(output_type,
"mp2")) {
648 }
else if (!strcmp(output_type,
"mpg")) {
652 fprintf(stderr,
"Invalid output type '%s', choose between 'h264', 'mp2', or 'mpg'\n",