yading@10: /* yading@10: * Copyright (c) 2003 Fabrice Bellard yading@10: * yading@10: * Permission is hereby granted, free of charge, to any person obtaining a copy yading@10: * of this software and associated documentation files (the "Software"), to deal yading@10: * in the Software without restriction, including without limitation the rights yading@10: * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell yading@10: * copies of the Software, and to permit persons to whom the Software is yading@10: * furnished to do so, subject to the following conditions: yading@10: * yading@10: * The above copyright notice and this permission notice shall be included in yading@10: * all copies or substantial portions of the Software. yading@10: * yading@10: * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR yading@10: * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, yading@10: * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL yading@10: * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER yading@10: * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, yading@10: * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN yading@10: * THE SOFTWARE. yading@10: */ yading@10: yading@10: /** yading@10: * @file yading@10: * libavformat API example. yading@10: * yading@10: * Output a media file in any supported libavformat format. yading@10: * The default codecs are used. yading@10: * @example doc/examples/muxing.c yading@10: */ yading@10: yading@10: #include yading@10: #include yading@10: #include yading@10: #include yading@10: yading@10: #include yading@10: #include yading@10: #include yading@10: yading@10: /* 5 seconds stream duration */ yading@10: #define STREAM_DURATION 200.0 yading@10: #define STREAM_FRAME_RATE 25 /* 25 images/s */ yading@10: #define STREAM_NB_FRAMES ((int)(STREAM_DURATION * STREAM_FRAME_RATE)) yading@10: #define STREAM_PIX_FMT AV_PIX_FMT_YUV420P /* default pix_fmt */ yading@10: yading@10: static int sws_flags = SWS_BICUBIC; yading@10: yading@10: /**************************************************************/ yading@10: /* audio output */ yading@10: yading@10: static float t, tincr, tincr2; yading@10: static int16_t *samples; yading@10: static int audio_input_frame_size; yading@10: yading@10: /* Add an output stream. */ yading@10: static AVStream *add_stream(AVFormatContext *oc, AVCodec **codec, yading@10: enum AVCodecID codec_id) yading@10: { yading@10: AVCodecContext *c; yading@10: AVStream *st; yading@10: yading@10: /* find the encoder */ yading@10: *codec = avcodec_find_encoder(codec_id); yading@10: if (!(*codec)) { yading@10: fprintf(stderr, "Could not find encoder for '%s'\n", yading@10: avcodec_get_name(codec_id)); yading@10: exit(1); yading@10: } yading@10: yading@10: st = avformat_new_stream(oc, *codec); yading@10: if (!st) { yading@10: fprintf(stderr, "Could not allocate stream\n"); yading@10: exit(1); yading@10: } yading@10: st->id = oc->nb_streams-1; yading@10: c = st->codec; yading@10: yading@10: switch ((*codec)->type) { yading@10: case AVMEDIA_TYPE_AUDIO: yading@10: st->id = 1; yading@10: c->sample_fmt = AV_SAMPLE_FMT_S16; yading@10: c->bit_rate = 64000; yading@10: c->sample_rate = 44100; yading@10: c->channels = 2; yading@10: break; yading@10: yading@10: case AVMEDIA_TYPE_VIDEO: yading@10: c->codec_id = codec_id; yading@10: yading@10: c->bit_rate = 400000; yading@10: /* Resolution must be a multiple of two. */ yading@10: c->width = 352; yading@10: c->height = 288; yading@10: /* timebase: This is the fundamental unit of time (in seconds) in terms yading@10: * of which frame timestamps are represented. For fixed-fps content, yading@10: * timebase should be 1/framerate and timestamp increments should be yading@10: * identical to 1. */ yading@10: c->time_base.den = STREAM_FRAME_RATE; yading@10: c->time_base.num = 1; yading@10: c->gop_size = 12; /* emit one intra frame every twelve frames at most */ yading@10: c->pix_fmt = STREAM_PIX_FMT; yading@10: if (c->codec_id == AV_CODEC_ID_MPEG2VIDEO) { yading@10: /* just for testing, we also add B frames */ yading@10: c->max_b_frames = 2; yading@10: } yading@10: if (c->codec_id == AV_CODEC_ID_MPEG1VIDEO) { yading@10: /* Needed to avoid using macroblocks in which some coeffs overflow. yading@10: * This does not happen with normal video, it just happens here as yading@10: * the motion of the chroma plane does not match the luma plane. */ yading@10: c->mb_decision = 2; yading@10: } yading@10: break; yading@10: yading@10: default: yading@10: break; yading@10: } yading@10: yading@10: /* Some formats want stream headers to be separate. */ yading@10: if (oc->oformat->flags & AVFMT_GLOBALHEADER) yading@10: c->flags |= CODEC_FLAG_GLOBAL_HEADER; yading@10: yading@10: return st; yading@10: } yading@10: yading@10: /**************************************************************/ yading@10: /* audio output */ yading@10: yading@10: static float t, tincr, tincr2; yading@10: static int16_t *samples; yading@10: static int audio_input_frame_size; yading@10: yading@10: static void open_audio(AVFormatContext *oc, AVCodec *codec, AVStream *st) yading@10: { yading@10: AVCodecContext *c; yading@10: int ret; yading@10: yading@10: c = st->codec; yading@10: yading@10: /* open it */ yading@10: ret = avcodec_open2(c, codec, NULL); yading@10: if (ret < 0) { yading@10: fprintf(stderr, "Could not open audio codec: %s\n", av_err2str(ret)); yading@10: exit(1); yading@10: } yading@10: yading@10: /* init signal generator */ yading@10: t = 0; yading@10: tincr = 2 * M_PI * 110.0 / c->sample_rate; yading@10: /* increment frequency by 110 Hz per second */ yading@10: tincr2 = 2 * M_PI * 110.0 / c->sample_rate / c->sample_rate; yading@10: yading@10: if (c->codec->capabilities & CODEC_CAP_VARIABLE_FRAME_SIZE) yading@10: audio_input_frame_size = 10000; yading@10: else yading@10: audio_input_frame_size = c->frame_size; yading@10: samples = av_malloc(audio_input_frame_size * yading@10: av_get_bytes_per_sample(c->sample_fmt) * yading@10: c->channels); yading@10: if (!samples) { yading@10: fprintf(stderr, "Could not allocate audio samples buffer\n"); yading@10: exit(1); yading@10: } yading@10: } yading@10: yading@10: /* Prepare a 16 bit dummy audio frame of 'frame_size' samples and yading@10: * 'nb_channels' channels. */ yading@10: static void get_audio_frame(int16_t *samples, int frame_size, int nb_channels) yading@10: { yading@10: int j, i, v; yading@10: int16_t *q; yading@10: yading@10: q = samples; yading@10: for (j = 0; j < frame_size; j++) { yading@10: v = (int)(sin(t) * 10000); yading@10: for (i = 0; i < nb_channels; i++) yading@10: *q++ = v; yading@10: t += tincr; yading@10: tincr += tincr2; yading@10: } yading@10: } yading@10: yading@10: static void write_audio_frame(AVFormatContext *oc, AVStream *st) yading@10: { yading@10: AVCodecContext *c; yading@10: AVPacket pkt = { 0 }; // data and size must be 0; yading@10: AVFrame *frame = avcodec_alloc_frame(); yading@10: int got_packet, ret; yading@10: yading@10: av_init_packet(&pkt); yading@10: c = st->codec; yading@10: yading@10: get_audio_frame(samples, audio_input_frame_size, c->channels); yading@10: frame->nb_samples = audio_input_frame_size; yading@10: avcodec_fill_audio_frame(frame, c->channels, c->sample_fmt, yading@10: (uint8_t *)samples, yading@10: audio_input_frame_size * yading@10: av_get_bytes_per_sample(c->sample_fmt) * yading@10: c->channels, 1); yading@10: yading@10: ret = avcodec_encode_audio2(c, &pkt, frame, &got_packet); yading@10: if (ret < 0) { yading@10: fprintf(stderr, "Error encoding audio frame: %s\n", av_err2str(ret)); yading@10: exit(1); yading@10: } yading@10: yading@10: if (!got_packet) yading@10: return; yading@10: yading@10: pkt.stream_index = st->index; yading@10: yading@10: /* Write the compressed frame to the media file. */ yading@10: ret = av_interleaved_write_frame(oc, &pkt); yading@10: if (ret != 0) { yading@10: fprintf(stderr, "Error while writing audio frame: %s\n", yading@10: av_err2str(ret)); yading@10: exit(1); yading@10: } yading@10: avcodec_free_frame(&frame); yading@10: } yading@10: yading@10: static void close_audio(AVFormatContext *oc, AVStream *st) yading@10: { yading@10: avcodec_close(st->codec); yading@10: yading@10: av_free(samples); yading@10: } yading@10: yading@10: /**************************************************************/ yading@10: /* video output */ yading@10: yading@10: static AVFrame *frame; yading@10: static AVPicture src_picture, dst_picture; yading@10: static int frame_count; yading@10: yading@10: static void open_video(AVFormatContext *oc, AVCodec *codec, AVStream *st) yading@10: { yading@10: int ret; yading@10: AVCodecContext *c = st->codec; yading@10: yading@10: /* open the codec */ yading@10: ret = avcodec_open2(c, codec, NULL); yading@10: if (ret < 0) { yading@10: fprintf(stderr, "Could not open video codec: %s\n", av_err2str(ret)); yading@10: exit(1); yading@10: } yading@10: yading@10: /* allocate and init a re-usable frame */ yading@10: frame = avcodec_alloc_frame(); yading@10: if (!frame) { yading@10: fprintf(stderr, "Could not allocate video frame\n"); yading@10: exit(1); yading@10: } yading@10: yading@10: /* Allocate the encoded raw picture. */ yading@10: ret = avpicture_alloc(&dst_picture, c->pix_fmt, c->width, c->height); yading@10: if (ret < 0) { yading@10: fprintf(stderr, "Could not allocate picture: %s\n", av_err2str(ret)); yading@10: exit(1); yading@10: } yading@10: yading@10: /* If the output format is not YUV420P, then a temporary YUV420P yading@10: * picture is needed too. It is then converted to the required yading@10: * output format. */ yading@10: if (c->pix_fmt != AV_PIX_FMT_YUV420P) { yading@10: ret = avpicture_alloc(&src_picture, AV_PIX_FMT_YUV420P, c->width, c->height); yading@10: if (ret < 0) { yading@10: fprintf(stderr, "Could not allocate temporary picture: %s\n", yading@10: av_err2str(ret)); yading@10: exit(1); yading@10: } yading@10: } yading@10: yading@10: /* copy data and linesize picture pointers to frame */ yading@10: *((AVPicture *)frame) = dst_picture; yading@10: } yading@10: yading@10: /* Prepare a dummy image. */ yading@10: static void fill_yuv_image(AVPicture *pict, int frame_index, yading@10: int width, int height) yading@10: { yading@10: int x, y, i; yading@10: yading@10: i = frame_index; yading@10: yading@10: /* Y */ yading@10: for (y = 0; y < height; y++) yading@10: for (x = 0; x < width; x++) yading@10: pict->data[0][y * pict->linesize[0] + x] = x + y + i * 3; yading@10: yading@10: /* Cb and Cr */ yading@10: for (y = 0; y < height / 2; y++) { yading@10: for (x = 0; x < width / 2; x++) { yading@10: pict->data[1][y * pict->linesize[1] + x] = 128 + y + i * 2; yading@10: pict->data[2][y * pict->linesize[2] + x] = 64 + x + i * 5; yading@10: } yading@10: } yading@10: } yading@10: yading@10: static void write_video_frame(AVFormatContext *oc, AVStream *st) yading@10: { yading@10: int ret; yading@10: static struct SwsContext *sws_ctx; yading@10: AVCodecContext *c = st->codec; yading@10: yading@10: if (frame_count >= STREAM_NB_FRAMES) { yading@10: /* No more frames to compress. The codec has a latency of a few yading@10: * frames if using B-frames, so we get the last frames by yading@10: * passing the same picture again. */ yading@10: } else { yading@10: if (c->pix_fmt != AV_PIX_FMT_YUV420P) { yading@10: /* as we only generate a YUV420P picture, we must convert it yading@10: * to the codec pixel format if needed */ yading@10: if (!sws_ctx) { yading@10: sws_ctx = sws_getContext(c->width, c->height, AV_PIX_FMT_YUV420P, yading@10: c->width, c->height, c->pix_fmt, yading@10: sws_flags, NULL, NULL, NULL); yading@10: if (!sws_ctx) { yading@10: fprintf(stderr, yading@10: "Could not initialize the conversion context\n"); yading@10: exit(1); yading@10: } yading@10: } yading@10: fill_yuv_image(&src_picture, frame_count, c->width, c->height); yading@10: sws_scale(sws_ctx, yading@10: (const uint8_t * const *)src_picture.data, src_picture.linesize, yading@10: 0, c->height, dst_picture.data, dst_picture.linesize); yading@10: } else { yading@10: fill_yuv_image(&dst_picture, frame_count, c->width, c->height); yading@10: } yading@10: } yading@10: yading@10: if (oc->oformat->flags & AVFMT_RAWPICTURE) { yading@10: /* Raw video case - directly store the picture in the packet */ yading@10: AVPacket pkt; yading@10: av_init_packet(&pkt); yading@10: yading@10: pkt.flags |= AV_PKT_FLAG_KEY; yading@10: pkt.stream_index = st->index; yading@10: pkt.data = dst_picture.data[0]; yading@10: pkt.size = sizeof(AVPicture); yading@10: yading@10: ret = av_interleaved_write_frame(oc, &pkt); yading@10: } else { yading@10: AVPacket pkt = { 0 }; yading@10: int got_packet; yading@10: av_init_packet(&pkt); yading@10: yading@10: /* encode the image */ yading@10: ret = avcodec_encode_video2(c, &pkt, frame, &got_packet); yading@10: if (ret < 0) { yading@10: fprintf(stderr, "Error encoding video frame: %s\n", av_err2str(ret)); yading@10: exit(1); yading@10: } yading@10: /* If size is zero, it means the image was buffered. */ yading@10: yading@10: if (!ret && got_packet && pkt.size) { yading@10: pkt.stream_index = st->index; yading@10: yading@10: /* Write the compressed frame to the media file. */ yading@10: ret = av_interleaved_write_frame(oc, &pkt); yading@10: } else { yading@10: ret = 0; yading@10: } yading@10: } yading@10: if (ret != 0) { yading@10: fprintf(stderr, "Error while writing video frame: %s\n", av_err2str(ret)); yading@10: exit(1); yading@10: } yading@10: frame_count++; yading@10: } yading@10: yading@10: static void close_video(AVFormatContext *oc, AVStream *st) yading@10: { yading@10: avcodec_close(st->codec); yading@10: av_free(src_picture.data[0]); yading@10: av_free(dst_picture.data[0]); yading@10: av_free(frame); yading@10: } yading@10: yading@10: /**************************************************************/ yading@10: /* media file output */ yading@10: yading@10: int main(int argc, char **argv) yading@10: { yading@10: const char *filename; yading@10: AVOutputFormat *fmt; yading@10: AVFormatContext *oc; yading@10: AVStream *audio_st, *video_st; yading@10: AVCodec *audio_codec, *video_codec; yading@10: double audio_pts, video_pts; yading@10: int ret; yading@10: yading@10: /* Initialize libavcodec, and register all codecs and formats. */ yading@10: av_register_all(); yading@10: yading@10: if (argc != 2) { yading@10: printf("usage: %s output_file\n" yading@10: "API example program to output a media file with libavformat.\n" yading@10: "This program generates a synthetic audio and video stream, encodes and\n" yading@10: "muxes them into a file named output_file.\n" yading@10: "The output format is automatically guessed according to the file extension.\n" yading@10: "Raw images can also be output by using '%%d' in the filename.\n" yading@10: "\n", argv[0]); yading@10: return 1; yading@10: } yading@10: yading@10: filename = argv[1]; yading@10: yading@10: /* allocate the output media context */ yading@10: avformat_alloc_output_context2(&oc, NULL, NULL, filename); yading@10: if (!oc) { yading@10: printf("Could not deduce output format from file extension: using MPEG.\n"); yading@10: avformat_alloc_output_context2(&oc, NULL, "mpeg", filename); yading@10: } yading@10: if (!oc) { yading@10: return 1; yading@10: } yading@10: fmt = oc->oformat; yading@10: yading@10: /* Add the audio and video streams using the default format codecs yading@10: * and initialize the codecs. */ yading@10: video_st = NULL; yading@10: audio_st = NULL; yading@10: yading@10: if (fmt->video_codec != AV_CODEC_ID_NONE) { yading@10: video_st = add_stream(oc, &video_codec, fmt->video_codec); yading@10: } yading@10: if (fmt->audio_codec != AV_CODEC_ID_NONE) { yading@10: audio_st = add_stream(oc, &audio_codec, fmt->audio_codec); yading@10: } yading@10: yading@10: /* Now that all the parameters are set, we can open the audio and yading@10: * video codecs and allocate the necessary encode buffers. */ yading@10: if (video_st) yading@10: open_video(oc, video_codec, video_st); yading@10: if (audio_st) yading@10: open_audio(oc, audio_codec, audio_st); yading@10: yading@10: av_dump_format(oc, 0, filename, 1); yading@10: yading@10: /* open the output file, if needed */ yading@10: if (!(fmt->flags & AVFMT_NOFILE)) { yading@10: ret = avio_open(&oc->pb, filename, AVIO_FLAG_WRITE); yading@10: if (ret < 0) { yading@10: fprintf(stderr, "Could not open '%s': %s\n", filename, yading@10: av_err2str(ret)); yading@10: return 1; yading@10: } yading@10: } yading@10: yading@10: /* Write the stream header, if any. */ yading@10: ret = avformat_write_header(oc, NULL); yading@10: if (ret < 0) { yading@10: fprintf(stderr, "Error occurred when opening output file: %s\n", yading@10: av_err2str(ret)); yading@10: return 1; yading@10: } yading@10: yading@10: if (frame) yading@10: frame->pts = 0; yading@10: for (;;) { yading@10: /* Compute current audio and video time. */ yading@10: if (audio_st) yading@10: audio_pts = (double)audio_st->pts.val * audio_st->time_base.num / audio_st->time_base.den; yading@10: else yading@10: audio_pts = 0.0; yading@10: yading@10: if (video_st) yading@10: video_pts = (double)video_st->pts.val * video_st->time_base.num / yading@10: video_st->time_base.den; yading@10: else yading@10: video_pts = 0.0; yading@10: yading@10: if ((!audio_st || audio_pts >= STREAM_DURATION) && yading@10: (!video_st || video_pts >= STREAM_DURATION)) yading@10: break; yading@10: yading@10: /* write interleaved audio and video frames */ yading@10: if (!video_st || (video_st && audio_st && audio_pts < video_pts)) { yading@10: write_audio_frame(oc, audio_st); yading@10: } else { yading@10: write_video_frame(oc, video_st); yading@10: frame->pts += av_rescale_q(1, video_st->codec->time_base, video_st->time_base); yading@10: } yading@10: } yading@10: yading@10: /* Write the trailer, if any. The trailer must be written before you yading@10: * close the CodecContexts open when you wrote the header; otherwise yading@10: * av_write_trailer() may try to use memory that was freed on yading@10: * av_codec_close(). */ yading@10: av_write_trailer(oc); yading@10: yading@10: /* Close each codec. */ yading@10: if (video_st) yading@10: close_video(oc, video_st); yading@10: if (audio_st) yading@10: close_audio(oc, audio_st); yading@10: yading@10: if (!(fmt->flags & AVFMT_NOFILE)) yading@10: /* Close the output file. */ yading@10: avio_close(oc->pb); yading@10: yading@10: /* free the stream */ yading@10: avformat_free_context(oc); yading@10: yading@10: return 0; yading@10: }