Skip to content
Projects
Groups
Snippets
Help
Loading...
Sign in / Register
Toggle navigation
F
ffmpeg
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Packages
Packages
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
submodule
ffmpeg
Commits
9897d9f4
Commit
9897d9f4
authored
Feb 10, 2016
by
Anton Khirnov
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
examples/output: convert to codecpar
parent
c23152a9
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
43 additions
and
17 deletions
+43
-17
output.c
doc/examples/output.c
+43
-17
No files found.
doc/examples/output.c
View file @
9897d9f4
...
...
@@ -52,6 +52,7 @@
// a wrapper around a single output AVStream
typedef
struct
OutputStream
{
AVStream
*
st
;
AVCodecContext
*
enc
;
/* pts of the next frame that will be generated */
int64_t
next_pts
;
...
...
@@ -85,13 +86,18 @@ static void add_audio_stream(OutputStream *ost, AVFormatContext *oc,
exit
(
1
);
}
ost
->
st
=
avformat_new_stream
(
oc
,
codec
);
ost
->
st
=
avformat_new_stream
(
oc
,
NULL
);
if
(
!
ost
->
st
)
{
fprintf
(
stderr
,
"Could not alloc stream
\n
"
);
exit
(
1
);
}
c
=
ost
->
st
->
codec
;
c
=
avcodec_alloc_context3
(
codec
);
if
(
!
c
)
{
fprintf
(
stderr
,
"Could not alloc an encoding context
\n
"
);
exit
(
1
);
}
ost
->
enc
=
c
;
/* put sample parameters */
c
->
sample_fmt
=
codec
->
sample_fmts
?
codec
->
sample_fmts
[
0
]
:
AV_SAMPLE_FMT_S16
;
...
...
@@ -162,9 +168,9 @@ static AVFrame *alloc_audio_frame(enum AVSampleFormat sample_fmt,
static
void
open_audio
(
AVFormatContext
*
oc
,
OutputStream
*
ost
)
{
AVCodecContext
*
c
;
int
nb_samples
;
int
nb_samples
,
ret
;
c
=
ost
->
st
->
code
c
;
c
=
ost
->
en
c
;
/* open it */
if
(
avcodec_open2
(
c
,
NULL
,
NULL
)
<
0
)
{
...
...
@@ -187,6 +193,13 @@ static void open_audio(AVFormatContext *oc, OutputStream *ost)
c
->
sample_rate
,
nb_samples
);
ost
->
tmp_frame
=
alloc_audio_frame
(
AV_SAMPLE_FMT_S16
,
AV_CH_LAYOUT_STEREO
,
44100
,
nb_samples
);
/* copy the stream parameters to the muxer */
ret
=
avcodec_parameters_from_context
(
ost
->
st
->
codecpar
,
c
);
if
(
ret
<
0
)
{
fprintf
(
stderr
,
"Could not copy the stream parameters
\n
"
);
exit
(
1
);
}
}
/* Prepare a 16 bit dummy audio frame of 'frame_size' samples and
...
...
@@ -198,14 +211,14 @@ static AVFrame *get_audio_frame(OutputStream *ost)
int16_t
*
q
=
(
int16_t
*
)
frame
->
data
[
0
];
/* check if we want to generate more frames */
if
(
av_compare_ts
(
ost
->
next_pts
,
ost
->
st
->
code
c
->
time_base
,
if
(
av_compare_ts
(
ost
->
next_pts
,
ost
->
en
c
->
time_base
,
STREAM_DURATION
,
(
AVRational
){
1
,
1
})
>=
0
)
return
NULL
;
for
(
j
=
0
;
j
<
frame
->
nb_samples
;
j
++
)
{
v
=
(
int
)(
sin
(
ost
->
t
)
*
10000
);
for
(
i
=
0
;
i
<
ost
->
st
->
code
c
->
channels
;
i
++
)
for
(
i
=
0
;
i
<
ost
->
en
c
->
channels
;
i
++
)
*
q
++
=
v
;
ost
->
t
+=
ost
->
tincr
;
ost
->
tincr
+=
ost
->
tincr2
;
...
...
@@ -224,12 +237,12 @@ static int encode_audio_frame(AVFormatContext *oc, OutputStream *ost,
int
got_packet
;
av_init_packet
(
&
pkt
);
avcodec_encode_audio2
(
ost
->
st
->
code
c
,
&
pkt
,
frame
,
&
got_packet
);
avcodec_encode_audio2
(
ost
->
en
c
,
&
pkt
,
frame
,
&
got_packet
);
if
(
got_packet
)
{
pkt
.
stream_index
=
ost
->
st
->
index
;
av_packet_rescale_ts
(
&
pkt
,
ost
->
st
->
code
c
->
time_base
,
ost
->
st
->
time_base
);
av_packet_rescale_ts
(
&
pkt
,
ost
->
en
c
->
time_base
,
ost
->
st
->
time_base
);
/* Write the compressed frame to the media file. */
if
(
av_interleaved_write_frame
(
oc
,
&
pkt
)
!=
0
)
{
...
...
@@ -324,13 +337,18 @@ static void add_video_stream(OutputStream *ost, AVFormatContext *oc,
exit
(
1
);
}
ost
->
st
=
avformat_new_stream
(
oc
,
codec
);
ost
->
st
=
avformat_new_stream
(
oc
,
NULL
);
if
(
!
ost
->
st
)
{
fprintf
(
stderr
,
"Could not alloc stream
\n
"
);
exit
(
1
);
}
c
=
ost
->
st
->
codec
;
c
=
avcodec_alloc_context3
(
codec
);
if
(
!
c
)
{
fprintf
(
stderr
,
"Could not alloc an encoding context
\n
"
);
exit
(
1
);
}
ost
->
enc
=
c
;
/* Put sample parameters. */
c
->
bit_rate
=
400000
;
...
...
@@ -387,8 +405,9 @@ static AVFrame *alloc_picture(enum AVPixelFormat pix_fmt, int width, int height)
static
void
open_video
(
AVFormatContext
*
oc
,
OutputStream
*
ost
)
{
AVCodecContext
*
c
;
int
ret
;
c
=
ost
->
st
->
code
c
;
c
=
ost
->
en
c
;
/* open the codec */
if
(
avcodec_open2
(
c
,
NULL
,
NULL
)
<
0
)
{
...
...
@@ -414,6 +433,13 @@ static void open_video(AVFormatContext *oc, OutputStream *ost)
exit
(
1
);
}
}
/* copy the stream parameters to the muxer */
ret
=
avcodec_parameters_from_context
(
ost
->
st
->
codecpar
,
c
);
if
(
ret
<
0
)
{
fprintf
(
stderr
,
"Could not copy the stream parameters
\n
"
);
exit
(
1
);
}
}
/* Prepare a dummy image. */
...
...
@@ -448,10 +474,10 @@ static void fill_yuv_image(AVFrame *pict, int frame_index,
static
AVFrame
*
get_video_frame
(
OutputStream
*
ost
)
{
AVCodecContext
*
c
=
ost
->
st
->
code
c
;
AVCodecContext
*
c
=
ost
->
en
c
;
/* check if we want to generate more frames */
if
(
av_compare_ts
(
ost
->
next_pts
,
ost
->
st
->
code
c
->
time_base
,
if
(
av_compare_ts
(
ost
->
next_pts
,
c
->
time_base
,
STREAM_DURATION
,
(
AVRational
){
1
,
1
})
>=
0
)
return
NULL
;
...
...
@@ -494,7 +520,7 @@ static int write_video_frame(AVFormatContext *oc, OutputStream *ost)
AVPacket
pkt
=
{
0
};
int
got_packet
=
0
;
c
=
ost
->
st
->
code
c
;
c
=
ost
->
en
c
;
frame
=
get_video_frame
(
ost
);
...
...
@@ -525,7 +551,7 @@ static int write_video_frame(AVFormatContext *oc, OutputStream *ost)
static
void
close_stream
(
AVFormatContext
*
oc
,
OutputStream
*
ost
)
{
avcodec_
close
(
ost
->
st
->
code
c
);
avcodec_
free_context
(
&
ost
->
en
c
);
av_frame_free
(
&
ost
->
frame
);
av_frame_free
(
&
ost
->
tmp_frame
);
sws_freeContext
(
ost
->
sws_ctx
);
...
...
@@ -614,8 +640,8 @@ int main(int argc, char **argv)
while
(
encode_video
||
encode_audio
)
{
/* select the stream to encode */
if
(
encode_video
&&
(
!
encode_audio
||
av_compare_ts
(
video_st
.
next_pts
,
video_st
.
st
->
code
c
->
time_base
,
audio_st
.
next_pts
,
audio_st
.
st
->
code
c
->
time_base
)
<=
0
))
{
(
!
encode_audio
||
av_compare_ts
(
video_st
.
next_pts
,
video_st
.
en
c
->
time_base
,
audio_st
.
next_pts
,
audio_st
.
en
c
->
time_base
)
<=
0
))
{
encode_video
=
!
write_video_frame
(
oc
,
&
video_st
);
}
else
{
encode_audio
=
!
process_audio_stream
(
oc
,
&
audio_st
);
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment