/*
* Libavformat API example: Output a media file in any supported
* libavformat format. The default codecs are used.
*
* Copyright (c) 2003 Fabrice Bellard
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <math.h>
//#include <time.h>
#include <Windows.h>
#ifndef M_PI
#define M_PI 3.14159265358979323846
#endif
extern "C"{
#include <libavformat/avformat.h>
#include <libswscale/swscale.h>
};
#undef exit
/* 5 seconds stream duration */
#define STREAM_DURATION 5.0
#define STREAM_FRAME_RATE 25 /* 25 images/s */
#define STREAM_NB_FRAMES ((int)(STREAM_DURATION * STREAM_FRAME_RATE))
#define STREAM_PIX_FMT PIX_FMT_YUV420P /* default pix_fmt */
static int sws_flags = SWS_BICUBIC;
/**************************************************************/
/* audio output */
float t, tincr, tincr2;
int16_t *samples;
uint8_t *audio_outbuf;
int audio_outbuf_size;
int audio_input_frame_size;
/*
* add an audio output stream
*/
static AVStream *add_audio_stream(AVFormatContext *oc, int codec_id)
{
AVCodecContext *c;
AVStream *st;
st = av_new_stream(oc, 1);
if (!st) {
fprintf(stderr, "Could not alloc stream\n");
exit(1);
}
c = st->codec;
c->codec_id = (CodecID)codec_id;
c->codec_type = AVMEDIA_TYPE_AUDIO;
/* put sample parameters */
c->sample_fmt = AV_SAMPLE_FMT_S16;
c->sample_rate = 8000;
c->channels = 1;
c->bit_rate = 16000;
// some formats want stream headers to be separate
if(!strcmp(oc->oformat->name, "mp4") || !strcmp(oc->oformat->name, "mov") || !strcmp(oc->oformat->name, "3gp"))
c->flags |= CODEC_FLAG_GLOBAL_HEADER;
return st;
}
static void open_audio(AVFormatContext *oc, AVStream *st)
{
AVCodecContext *c;
AVCodec *codec;
c = st->codec;
/* find the audio encoder */
codec = avcodec_find_encoder(c->codec_id);
if (!codec) {
fprintf(stderr, "codec not found\n");
exit(1);
}
/* open it */
if (avcodec_open(c, codec) < 0) {
fprintf(stderr, "could not open audio codec\n");
exit(1);
}
/* init signal generator */
t = 0;
tincr = 2 * M_PI * 110.0 / c->sample_rate;
/* increment frequency by 110 Hz per second */
tincr2 = 2 * M_PI * 110.0 / c->sample_rate / c->sample_rate;
audio_outbuf_size = 10000;
audio_outbuf = (uint8_t *)av_malloc(audio_outbuf_size);
/* ugly hack for PCM codecs (will be removed ASAP with new PCM
support to compute the input frame size in samples */
if (c->frame_size <= 1) {
audio_input_frame_size = audio_outbuf_size / c->channels;
switch(st->codec->codec_id) {
case CODEC_ID_PCM_S16LE:
case CODEC_ID_PCM_S16BE:
case CODEC_ID_PCM_U16LE:
case CODEC_ID_PCM_U16BE:
audio_input_frame_size >>= 1;
break;
default:
break;
}
} else {
audio_input_frame_size = c->frame_size;
}
samples = (int16_t *)av_malloc(audio_input_frame_size * 2 * c->channels);
}
/* prepare a 16 bit dummy audio frame of 'frame_size' samples and
'nb_channels' channels */
static void get_audio_frame(int16_t *samples, int frame_size, int nb_channels)
{
int j, i, v;
int16_t *q;
q = samples;
for(j=0;j<frame_size;j++) {
v = (int)(sin(t) * 10000);
for(i = 0; i < nb_channels; i++)
*q++ = v;
t += tincr;
tincr += tincr2;
}
}
static void write_audio_frame(AVFormatContext *oc, AVStream *st)
{
AVCodecContext *c;
AVPacket pkt;
av_init_packet(&pkt);
c = st->codec;
get_audio_frame(samples, audio_input_frame_size, c->channels);
pkt.size = avcodec_encode_audio(c, audio_outbuf, audio_outbuf_size, samples);
printf("[audio] AAC frame len=%d\n", pkt.size);
//pkt.pts= av_rescale_q(c->coded_frame->pts, c->time_base, st->time_base);
//pkt.flags |= AV_PKT_FLAG_KEY;
pkt.stream_index= st->index;
pkt.data= audio_outbuf;
/* write the compressed frame in the media file */
if (av_write_frame(oc, &pkt) != 0) {
fprintf(stderr, "Error while writing audio frame\n");
exit(1);
}
}
static void close_audio(AVFormatContext *oc, AVStream *st)
{
avcodec_close(st->codec);
av_free(samples);
av_free(audio_outbuf);
}
/**************************************************************/
/* video output */
AVFrame *picture, *tmp_picture;
uint8_t *video_outbuf;
int frame_count, video_outbuf_size;
/* add a video output stream */
static AVStream *add_video_stream(AVFormatContext *oc, int codec_id)
{
AVCodecContext *c;
AVStream *st;
st = av_new_stream(oc, 0);
if (!st) {
fprintf(stderr, "Could not alloc stream\n");
exit(1);
}
c = st->codec;
c->codec_id = (CodecID)codec_id;
c->codec_type = AVMEDIA_TYPE_VIDEO;
c->me_range = 16;
c->max_qdiff = 4;
c->qmin = 10;
c->qmax = 51;
c->qcompress = 0.6;
/* put sample parameters */
c->bit_rate = 400000;
/* resolution must be a multiple of two */
c->width = 352;
c->height = 288;
/* time base: this is the fundamental unit of time (in seconds) in terms
of which frame timestamps are represented. for fixed-fps content,
timebase should be 1/framerate and timestamp increments should be
identically 1. */
c->time_base.den = STREAM_FRAME_RATE;
c->time_base.num = 1;
c->gop_size = 12; /* emit one intra frame every twelve frames at most */
c->pix_fmt = STREAM_PIX_FMT;
if (c->codec_id == CODEC_ID_MPEG2VIDEO) {
/* just for testing, we also add B frames */
c->max_b_frames = 2;
}
if (c->codec_id == CODEC_ID_MPEG1VIDEO){
/* Needed to avoid using macroblocks in which some coeffs overflow.
This does not happen with normal video, it just happens here as
the motion of the chroma plane does not match the luma plane. */
c->mb_decision=2;
}
// some formats want stream headers to be separate
if(!strcmp(oc->oformat->name, "mp4") || !strcmp(oc->oformat->name, "mov") || !strcmp(oc->oformat->name, "3gp"))
c->flags |= CODEC_FLAG_GLOBAL_HEADER;
return st;
}
static AVFrame *alloc_picture(int pix_fmt, int width, int height)
{
AVFrame *picture;
uint8_t *picture_buf;
int size;
picture = avcodec_alloc_frame();
if (!picture)
return NULL;
size = avpicture_get_size((PixelFormat)pix_fmt, width, height);
picture_buf = (uint8_t *)av_malloc(size);
if (!picture_buf) {
av_free(picture);
return NULL;
}
avpicture_fill((AVPicture *)picture, picture_buf,
(PixelFor
没有合适的资源?快使用搜索试试~ 我知道了~
资源推荐
资源详情
资源评论
收起资源包目录
output-example.rar (87个子文件)
output-example
include
libpostproc
postprocess.h 3KB
libswscale
swscale.h 12KB
libavdevice
avdevice.h 2KB
libavcodec
dxva2.h 2KB
version.h 3KB
opt.h 267B
avfft.h 3KB
avcodec.h 136KB
vdpau.h 3KB
vaapi.h 4KB
xvmc.h 6KB
libavfilter
avfiltergraph.h 5KB
vsink_buffer.h 2KB
vsrc_buffer.h 2KB
avfilter.h 35KB
avcodec.h 2KB
libavutil
intreadwrite.h 14KB
bswap.h 3KB
random_seed.h 1KB
attributes.h 3KB
log.h 5KB
base64.h 2KB
dict.h 3KB
cpu.h 2KB
lzo.h 2KB
md5.h 1KB
pixdesc.h 7KB
rational.h 4KB
file.h 2KB
eval.h 6KB
mem.h 5KB
samplefmt.h 5KB
common.h 11KB
opt.h 9KB
parseutils.h 4KB
avutil.h 4KB
avassert.h 2KB
avstring.h 5KB
crc.h 1KB
intfloat_readwrite.h 1KB
audioconvert.h 4KB
mathematics.h 4KB
sha.h 2KB
imgutils.h 5KB
aes.h 2KB
adler32.h 1KB
fifo.h 4KB
pixfmt.h 13KB
lfg.h 2KB
avconfig.h 170B
error.h 3KB
libavformat
version.h 3KB
avio.h 24KB
avformat.h 58KB
output-example
output-example.vcproj 4KB
output-example.vcproj.ajaxhe-PC.ajaxhe.user 1KB
output_example.cpp 17KB
Debug
BuildLog.htm 8KB
output-example.exe.embed.manifest 663B
output-example.exe.embed.manifest.res 728B
output-example.vcproj.ajaxhe-THINK.ajaxhe.user 1KB
output-example.vcproj.HUST-D4EB68F404.ajaxhe.user 1KB
Debug
avformat-53.dll 1.24MB
output-example.lib 2KB
avutil-51.dll 130KB
avfilter-2.dll 780KB
swscale-2.dll 229KB
output-example.exp 664B
avcodec-53.dll 11.89MB
avdevice-53.dll 54KB
output-example.ncb 14.87MB
output-example.suo 44KB
output-example.sln 908B
lib
swscale.lib 12KB
libpostproc.dll.a 6KB
postproc.lib 3KB
libswscale.dll.a 33KB
libavcodec.dll.a 4.37MB
avcodec.lib 1.45MB
libavutil.dll.a 102KB
avformat.lib 177KB
avutil.lib 34KB
libavfilter.dll.a 74KB
libavdevice.dll.a 3KB
avfilter.lib 27KB
avdevice.lib 2KB
libavformat.dll.a 542KB
共 87 条
- 1
ajaxhe
- 粉丝: 62
- 资源: 17
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功
- 1
- 2
- 3
- 4
- 5
- 6
前往页