00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00032 #include <stdlib.h>
00033 #include <stdio.h>
00034 #include <string.h>
00035 #include <math.h>
00036
00037 #include <libavutil/mathematics.h>
00038 #include <libavformat/avformat.h>
00039 #include <libswscale/swscale.h>
00040
00041
00042 #define STREAM_DURATION 200.0
00043 #define STREAM_FRAME_RATE 25
00044 #define STREAM_NB_FRAMES ((int)(STREAM_DURATION * STREAM_FRAME_RATE))
00045 #define STREAM_PIX_FMT AV_PIX_FMT_YUV420P
00046
00047 static int sws_flags = SWS_BICUBIC;
00048
00049
00050
00051
00052 static float t, tincr, tincr2;
00053 static int16_t *samples;
00054 static int audio_input_frame_size;
00055
00056
00057 static AVStream *add_stream(AVFormatContext *oc, AVCodec **codec,
00058 enum AVCodecID codec_id)
00059 {
00060 AVCodecContext *c;
00061 AVStream *st;
00062
00063
00064 *codec = avcodec_find_encoder(codec_id);
00065 if (!(*codec)) {
00066 fprintf(stderr, "Could not find codec\n");
00067 exit(1);
00068 }
00069
00070 st = avformat_new_stream(oc, *codec);
00071 if (!st) {
00072 fprintf(stderr, "Could not allocate stream\n");
00073 exit(1);
00074 }
00075 st->id = oc->nb_streams-1;
00076 c = st->codec;
00077
00078 switch ((*codec)->type) {
00079 case AVMEDIA_TYPE_AUDIO:
00080 st->id = 1;
00081 c->sample_fmt = AV_SAMPLE_FMT_S16;
00082 c->bit_rate = 64000;
00083 c->sample_rate = 44100;
00084 c->channels = 2;
00085 break;
00086
00087 case AVMEDIA_TYPE_VIDEO:
00088 avcodec_get_context_defaults3(c, *codec);
00089 c->codec_id = codec_id;
00090
00091 c->bit_rate = 400000;
00092
00093 c->width = 352;
00094 c->height = 288;
00095
00096
00097
00098
00099 c->time_base.den = STREAM_FRAME_RATE;
00100 c->time_base.num = 1;
00101 c->gop_size = 12;
00102 c->pix_fmt = STREAM_PIX_FMT;
00103 if (c->codec_id == AV_CODEC_ID_MPEG2VIDEO) {
00104
00105 c->max_b_frames = 2;
00106 }
00107 if (c->codec_id == AV_CODEC_ID_MPEG1VIDEO) {
00108
00109
00110
00111 c->mb_decision = 2;
00112 }
00113 break;
00114
00115 default:
00116 break;
00117 }
00118
00119
00120 if (oc->oformat->flags & AVFMT_GLOBALHEADER)
00121 c->flags |= CODEC_FLAG_GLOBAL_HEADER;
00122
00123 return st;
00124 }
00125
00126
00127
00128
00129 static float t, tincr, tincr2;
00130 static int16_t *samples;
00131 static int audio_input_frame_size;
00132
00133 static void open_audio(AVFormatContext *oc, AVCodec *codec, AVStream *st)
00134 {
00135 AVCodecContext *c;
00136
00137 c = st->codec;
00138
00139
00140 if (avcodec_open2(c, codec, NULL) < 0) {
00141 fprintf(stderr, "Could not open audio codec\n");
00142 exit(1);
00143 }
00144
00145
00146 t = 0;
00147 tincr = 2 * M_PI * 110.0 / c->sample_rate;
00148
00149 tincr2 = 2 * M_PI * 110.0 / c->sample_rate / c->sample_rate;
00150
00151 if (c->codec->capabilities & CODEC_CAP_VARIABLE_FRAME_SIZE)
00152 audio_input_frame_size = 10000;
00153 else
00154 audio_input_frame_size = c->frame_size;
00155 samples = av_malloc(audio_input_frame_size *
00156 av_get_bytes_per_sample(c->sample_fmt) *
00157 c->channels);
00158 if (!samples) {
00159 fprintf(stderr, "Could not allocate audio samples buffer\n");
00160 exit(1);
00161 }
00162 }
00163
00164
00165
00166 static void get_audio_frame(int16_t *samples, int frame_size, int nb_channels)
00167 {
00168 int j, i, v;
00169 int16_t *q;
00170
00171 q = samples;
00172 for (j = 0; j < frame_size; j++) {
00173 v = (int)(sin(t) * 10000);
00174 for (i = 0; i < nb_channels; i++)
00175 *q++ = v;
00176 t += tincr;
00177 tincr += tincr2;
00178 }
00179 }
00180
00181 static void write_audio_frame(AVFormatContext *oc, AVStream *st)
00182 {
00183 AVCodecContext *c;
00184 AVPacket pkt = { 0 };
00185 AVFrame *frame = avcodec_alloc_frame();
00186 int got_packet, ret;
00187
00188 av_init_packet(&pkt);
00189 c = st->codec;
00190
00191 get_audio_frame(samples, audio_input_frame_size, c->channels);
00192 frame->nb_samples = audio_input_frame_size;
00193 avcodec_fill_audio_frame(frame, c->channels, c->sample_fmt,
00194 (uint8_t *)samples,
00195 audio_input_frame_size *
00196 av_get_bytes_per_sample(c->sample_fmt) *
00197 c->channels, 1);
00198
00199 ret = avcodec_encode_audio2(c, &pkt, frame, &got_packet);
00200 if (ret < 0) {
00201 fprintf(stderr, "Error encoding audio frame\n");
00202 exit(1);
00203 }
00204
00205 if (!got_packet)
00206 return;
00207
00208 pkt.stream_index = st->index;
00209
00210
00211 if (av_interleaved_write_frame(oc, &pkt) != 0) {
00212 fprintf(stderr, "Error while writing audio frame\n");
00213 exit(1);
00214 }
00215 avcodec_free_frame(&frame);
00216 }
00217
00218 static void close_audio(AVFormatContext *oc, AVStream *st)
00219 {
00220 avcodec_close(st->codec);
00221
00222 av_free(samples);
00223 }
00224
00225
00226
00227
00228 static AVFrame *frame;
00229 static AVPicture src_picture, dst_picture;
00230 static int frame_count;
00231
00232 static void open_video(AVFormatContext *oc, AVCodec *codec, AVStream *st)
00233 {
00234 int ret;
00235 AVCodecContext *c = st->codec;
00236
00237
00238 if (avcodec_open2(c, codec, NULL) < 0) {
00239 fprintf(stderr, "Could not open video codec\n");
00240 exit(1);
00241 }
00242
00243
00244 frame = avcodec_alloc_frame();
00245 if (!frame) {
00246 fprintf(stderr, "Could not allocate video frame\n");
00247 exit(1);
00248 }
00249
00250
00251 ret = avpicture_alloc(&dst_picture, c->pix_fmt, c->width, c->height);
00252 if (ret < 0) {
00253 fprintf(stderr, "Could not allocate picture\n");
00254 exit(1);
00255 }
00256
00257
00258
00259
00260 if (c->pix_fmt != AV_PIX_FMT_YUV420P) {
00261 ret = avpicture_alloc(&src_picture, AV_PIX_FMT_YUV420P, c->width, c->height);
00262 if (ret < 0) {
00263 fprintf(stderr, "Could not allocate temporary picture\n");
00264 exit(1);
00265 }
00266 }
00267
00268
00269 *((AVPicture *)frame) = dst_picture;
00270 }
00271
00272
00273 static void fill_yuv_image(AVPicture *pict, int frame_index,
00274 int width, int height)
00275 {
00276 int x, y, i;
00277
00278 i = frame_index;
00279
00280
00281 for (y = 0; y < height; y++)
00282 for (x = 0; x < width; x++)
00283 pict->data[0][y * pict->linesize[0] + x] = x + y + i * 3;
00284
00285
00286 for (y = 0; y < height / 2; y++) {
00287 for (x = 0; x < width / 2; x++) {
00288 pict->data[1][y * pict->linesize[1] + x] = 128 + y + i * 2;
00289 pict->data[2][y * pict->linesize[2] + x] = 64 + x + i * 5;
00290 }
00291 }
00292 }
00293
00294 static void write_video_frame(AVFormatContext *oc, AVStream *st)
00295 {
00296 int ret;
00297 static struct SwsContext *sws_ctx;
00298 AVCodecContext *c = st->codec;
00299
00300 if (frame_count >= STREAM_NB_FRAMES) {
00301
00302
00303
00304 } else {
00305 if (c->pix_fmt != AV_PIX_FMT_YUV420P) {
00306
00307
00308 if (!sws_ctx) {
00309 sws_ctx = sws_getContext(c->width, c->height, AV_PIX_FMT_YUV420P,
00310 c->width, c->height, c->pix_fmt,
00311 sws_flags, NULL, NULL, NULL);
00312 if (!sws_ctx) {
00313 fprintf(stderr,
00314 "Could not initialize the conversion context\n");
00315 exit(1);
00316 }
00317 }
00318 fill_yuv_image(&src_picture, frame_count, c->width, c->height);
00319 sws_scale(sws_ctx,
00320 (const uint8_t * const *)src_picture.data, src_picture.linesize,
00321 0, c->height, dst_picture.data, dst_picture.linesize);
00322 } else {
00323 fill_yuv_image(&dst_picture, frame_count, c->width, c->height);
00324 }
00325 }
00326
00327 if (oc->oformat->flags & AVFMT_RAWPICTURE) {
00328
00329 AVPacket pkt;
00330 av_init_packet(&pkt);
00331
00332 pkt.flags |= AV_PKT_FLAG_KEY;
00333 pkt.stream_index = st->index;
00334 pkt.data = dst_picture.data[0];
00335 pkt.size = sizeof(AVPicture);
00336
00337 ret = av_interleaved_write_frame(oc, &pkt);
00338 } else {
00339
00340 AVPacket pkt;
00341 int got_output;
00342
00343 av_init_packet(&pkt);
00344 pkt.data = NULL;
00345 pkt.size = 0;
00346
00347 ret = avcodec_encode_video2(c, &pkt, frame, &got_output);
00348 if (ret < 0) {
00349 fprintf(stderr, "Error encoding video frame\n");
00350 exit(1);
00351 }
00352
00353
00354 if (got_output) {
00355 if (c->coded_frame->key_frame)
00356 pkt.flags |= AV_PKT_FLAG_KEY;
00357
00358 pkt.stream_index = st->index;
00359
00360
00361 ret = av_interleaved_write_frame(oc, &pkt);
00362 } else {
00363 ret = 0;
00364 }
00365 }
00366 if (ret != 0) {
00367 fprintf(stderr, "Error while writing video frame\n");
00368 exit(1);
00369 }
00370 frame_count++;
00371 }
00372
00373 static void close_video(AVFormatContext *oc, AVStream *st)
00374 {
00375 avcodec_close(st->codec);
00376 av_free(src_picture.data[0]);
00377 av_free(dst_picture.data[0]);
00378 av_free(frame);
00379 }
00380
00381
00382
00383
00384 int main(int argc, char **argv)
00385 {
00386 const char *filename;
00387 AVOutputFormat *fmt;
00388 AVFormatContext *oc;
00389 AVStream *audio_st, *video_st;
00390 AVCodec *audio_codec, *video_codec;
00391 double audio_pts, video_pts;
00392 int i;
00393
00394
00395 av_register_all();
00396
00397 if (argc != 2) {
00398 printf("usage: %s output_file\n"
00399 "API example program to output a media file with libavformat.\n"
00400 "This program generates a synthetic audio and video stream, encodes and\n"
00401 "muxes them into a file named output_file.\n"
00402 "The output format is automatically guessed according to the file extension.\n"
00403 "Raw images can also be output by using '%%d' in the filename.\n"
00404 "\n", argv[0]);
00405 return 1;
00406 }
00407
00408 filename = argv[1];
00409
00410
00411 avformat_alloc_output_context2(&oc, NULL, NULL, filename);
00412 if (!oc) {
00413 printf("Could not deduce output format from file extension: using MPEG.\n");
00414 avformat_alloc_output_context2(&oc, NULL, "mpeg", filename);
00415 }
00416 if (!oc) {
00417 return 1;
00418 }
00419 fmt = oc->oformat;
00420
00421
00422
00423 video_st = NULL;
00424 audio_st = NULL;
00425
00426 if (fmt->video_codec != AV_CODEC_ID_NONE) {
00427 video_st = add_stream(oc, &video_codec, fmt->video_codec);
00428 }
00429 if (fmt->audio_codec != AV_CODEC_ID_NONE) {
00430 audio_st = add_stream(oc, &audio_codec, fmt->audio_codec);
00431 }
00432
00433
00434
00435 if (video_st)
00436 open_video(oc, video_codec, video_st);
00437 if (audio_st)
00438 open_audio(oc, audio_codec, audio_st);
00439
00440 av_dump_format(oc, 0, filename, 1);
00441
00442
00443 if (!(fmt->flags & AVFMT_NOFILE)) {
00444 if (avio_open(&oc->pb, filename, AVIO_FLAG_WRITE) < 0) {
00445 fprintf(stderr, "Could not open '%s'\n", filename);
00446 return 1;
00447 }
00448 }
00449
00450
00451 if (avformat_write_header(oc, NULL) < 0) {
00452 fprintf(stderr, "Error occurred when opening output file\n");
00453 return 1;
00454 }
00455
00456 if (frame)
00457 frame->pts = 0;
00458 for (;;) {
00459
00460 if (audio_st)
00461 audio_pts = (double)audio_st->pts.val * audio_st->time_base.num / audio_st->time_base.den;
00462 else
00463 audio_pts = 0.0;
00464
00465 if (video_st)
00466 video_pts = (double)video_st->pts.val * video_st->time_base.num /
00467 video_st->time_base.den;
00468 else
00469 video_pts = 0.0;
00470
00471 if ((!audio_st || audio_pts >= STREAM_DURATION) &&
00472 (!video_st || video_pts >= STREAM_DURATION))
00473 break;
00474
00475
00476 if (!video_st || (video_st && audio_st && audio_pts < video_pts)) {
00477 write_audio_frame(oc, audio_st);
00478 } else {
00479 write_video_frame(oc, video_st);
00480 frame->pts += av_rescale_q(1, video_st->codec->time_base, video_st->time_base);
00481 }
00482 }
00483
00484
00485
00486
00487
00488 av_write_trailer(oc);
00489
00490
00491 if (video_st)
00492 close_video(oc, video_st);
00493 if (audio_st)
00494 close_audio(oc, audio_st);
00495
00496
00497 for (i = 0; i < oc->nb_streams; i++) {
00498 av_freep(&oc->streams[i]->codec);
00499 av_freep(&oc->streams[i]);
00500 }
00501
00502 if (!(fmt->flags & AVFMT_NOFILE))
00503
00504 avio_close(oc->pb);
00505
00506
00507 av_free(oc);
00508
00509 return 0;
00510 }