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) {
294 if (!decoded_frame) {
296 fprintf(stderr,
"Could not allocate audio frame\n");
303 fprintf(stderr,
"Error while decoding\n");
311 fprintf(stderr,
"Failed to calculate data size\n");
316 fwrite(decoded_frame->
data[ch] + data_size*i, 1, data_size, outfile);
327 memmove(inbuf, avpkt.
data, avpkt.
size);
329 len = fread(avpkt.
data + avpkt.
size, 1,
351 int i,
ret, x,
y, got_output;
355 uint8_t endcode[] = { 0, 0, 1, 0xb7 };
357 printf(
"Encode video file %s\n", filename);
362 fprintf(stderr,
"Codec not found\n");
368 fprintf(stderr,
"Could not allocate video codec context\n");
394 fprintf(stderr,
"Could not open codec\n");
398 f = fopen(filename,
"wb");
400 fprintf(stderr,
"Could not open %s\n", filename);
406 fprintf(stderr,
"Could not allocate video frame\n");
418 fprintf(stderr,
"Could not allocate raw picture buffer\n");
423 for (i = 0; i < 25; i++) {
431 for (y = 0; y < c->
height; y++) {
432 for (x = 0; x < c->
width; x++) {
433 frame->
data[0][y * frame->
linesize[0] + x] = x + y + i * 3;
438 for (y = 0; y < c->
height/2; y++) {
439 for (x = 0; x < c->
width/2; x++) {
440 frame->
data[1][y * frame->
linesize[1] + x] = 128 + y + i * 2;
441 frame->
data[2][y * frame->
linesize[2] + x] = 64 + x + i * 5;
450 fprintf(stderr,
"Error encoding frame\n");
455 printf(
"Write frame %3d (size=%5d)\n", i, pkt.
size);
462 for (got_output = 1; got_output; i++) {
467 fprintf(stderr,
"Error encoding frame\n");
472 printf(
"Write frame %3d (size=%5d)\n", i, pkt.
size);
479 fwrite(endcode, 1,
sizeof(endcode), f);
499 f = fopen(filename,
"w");
500 fprintf(f,
"P5\n%d %d\n%d\n", xsize, ysize, 255);
501 for (i = 0; i < ysize; i++)
502 fwrite(buf + i * wrap, 1, xsize, f);
514 fprintf(stderr,
"Error while decoding frame %d\n", *frame_count);
518 printf(
"Saving %sframe %3d\n", last ?
"last " :
"", *frame_count);
522 snprintf(buf,
sizeof(buf), outfilename, *frame_count);
549 printf(
"Decode video file %s to %s\n", filename, outfilename);
554 fprintf(stderr,
"Codec not found\n");
560 fprintf(stderr,
"Could not allocate video codec context\n");
573 fprintf(stderr,
"Could not open codec\n");
577 f = fopen(filename,
"rb");
579 fprintf(stderr,
"Could not open %s\n", filename);
585 fprintf(stderr,
"Could not allocate video frame\n");
611 while (avpkt.
size > 0)
631 int main(
int argc,
char **argv)
633 const char *output_type;
639 printf(
"usage: %s output_type\n"
640 "API example program to decode/encode a media stream with libavcodec.\n"
641 "This program generates a synthetic stream and encodes it to a file\n"
642 "named test.h264, test.mp2 or test.mpg depending on output_type.\n"
643 "The encoded stream is then decoded and written to a raw data output.\n"
644 "output_type must be chosen between 'h264', 'mp2', 'mpg'.\n",
648 output_type = argv[1];
650 if (!strcmp(output_type,
"h264")) {
652 }
else if (!strcmp(output_type,
"mp2")) {
655 }
else if (!strcmp(output_type,
"mpg")) {
659 fprintf(stderr,
"Invalid output type '%s', choose between 'h264', 'mp2', or 'mpg'\n",