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