Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
F
FFmpeg
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Container Registry
Model registry
Operate
Environments
Monitor
Incidents
Service Desk
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
libremedia
Tethys
FFmpeg
Commits
63fd0d86
Commit
63fd0d86
authored
10 years ago
by
Anton Khirnov
Browse files
Options
Downloads
Patches
Plain Diff
output example: allocate the audio frame only once
parent
edd5f957
No related branches found
No related tags found
No related merge requests found
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
doc/examples/output.c
+23
-26
23 additions, 26 deletions
doc/examples/output.c
with
23 additions
and
26 deletions
doc/examples/output.c
+
23
−
26
View file @
63fd0d86
...
@@ -55,7 +55,6 @@ typedef struct OutputStream {
...
@@ -55,7 +55,6 @@ typedef struct OutputStream {
AVFrame
*
tmp_frame
;
AVFrame
*
tmp_frame
;
float
t
,
tincr
,
tincr2
;
float
t
,
tincr
,
tincr2
;
int
audio_input_frame_size
;
}
OutputStream
;
}
OutputStream
;
/**************************************************************/
/**************************************************************/
...
@@ -100,6 +99,7 @@ static void add_audio_stream(OutputStream *ost, AVFormatContext *oc,
...
@@ -100,6 +99,7 @@ static void add_audio_stream(OutputStream *ost, AVFormatContext *oc,
static
void
open_audio
(
AVFormatContext
*
oc
,
OutputStream
*
ost
)
static
void
open_audio
(
AVFormatContext
*
oc
,
OutputStream
*
ost
)
{
{
AVCodecContext
*
c
;
AVCodecContext
*
c
;
int
ret
;
c
=
ost
->
st
->
codec
;
c
=
ost
->
st
->
codec
;
...
@@ -115,10 +115,24 @@ static void open_audio(AVFormatContext *oc, OutputStream *ost)
...
@@ -115,10 +115,24 @@ static void open_audio(AVFormatContext *oc, OutputStream *ost)
/* increment frequency by 110 Hz per second */
/* increment frequency by 110 Hz per second */
ost
->
tincr2
=
2
*
M_PI
*
110
.
0
/
c
->
sample_rate
/
c
->
sample_rate
;
ost
->
tincr2
=
2
*
M_PI
*
110
.
0
/
c
->
sample_rate
/
c
->
sample_rate
;
ost
->
frame
=
av_frame_alloc
();
if
(
!
ost
->
frame
)
exit
(
1
);
ost
->
frame
->
sample_rate
=
c
->
sample_rate
;
ost
->
frame
->
format
=
AV_SAMPLE_FMT_S16
;
ost
->
frame
->
channel_layout
=
c
->
channel_layout
;
if
(
c
->
codec
->
capabilities
&
CODEC_CAP_VARIABLE_FRAME_SIZE
)
if
(
c
->
codec
->
capabilities
&
CODEC_CAP_VARIABLE_FRAME_SIZE
)
ost
->
audio_input_frame_size
=
10000
;
ost
->
frame
->
nb_samples
=
10000
;
else
else
ost
->
audio_input_frame_size
=
c
->
frame_size
;
ost
->
frame
->
nb_samples
=
c
->
frame_size
;
ret
=
av_frame_get_buffer
(
ost
->
frame
,
0
);
if
(
ret
<
0
)
{
fprintf
(
stderr
,
"Could not allocate an audio frame.
\n
"
);
exit
(
1
);
}
}
}
/* Prepare a 16 bit dummy audio frame of 'frame_size' samples and
/* Prepare a 16 bit dummy audio frame of 'frame_size' samples and
...
@@ -149,25 +163,14 @@ static void write_audio_frame(AVFormatContext *oc, OutputStream *ost)
...
@@ -149,25 +163,14 @@ static void write_audio_frame(AVFormatContext *oc, OutputStream *ost)
{
{
AVCodecContext
*
c
;
AVCodecContext
*
c
;
AVPacket
pkt
=
{
0
};
// data and size must be 0;
AVPacket
pkt
=
{
0
};
// data and size must be 0;
AVFrame
*
frame
=
av_frame_alloc
();
int
got_packet
,
ret
;
int
got_packet
,
ret
;
av_init_packet
(
&
pkt
);
av_init_packet
(
&
pkt
);
c
=
ost
->
st
->
codec
;
c
=
ost
->
st
->
codec
;
frame
->
sample_rate
=
c
->
sample_rate
;
get_audio_frame
(
ost
,
ost
->
frame
,
c
->
channels
);
frame
->
nb_samples
=
ost
->
audio_input_frame_size
;
frame
->
format
=
AV_SAMPLE_FMT_S16
;
frame
->
channel_layout
=
c
->
channel_layout
;
ret
=
av_frame_get_buffer
(
frame
,
0
);
if
(
ret
<
0
)
{
fprintf
(
stderr
,
"Could not allocate an audio frame.
\n
"
);
exit
(
1
);
}
get_audio_frame
(
ost
,
frame
,
c
->
channels
);
avcodec_encode_audio2
(
c
,
&
pkt
,
frame
,
&
got_packet
);
avcodec_encode_audio2
(
c
,
&
pkt
,
ost
->
frame
,
&
got_packet
);
if
(
!
got_packet
)
if
(
!
got_packet
)
return
;
return
;
...
@@ -178,12 +181,6 @@ static void write_audio_frame(AVFormatContext *oc, OutputStream *ost)
...
@@ -178,12 +181,6 @@ static void write_audio_frame(AVFormatContext *oc, OutputStream *ost)
fprintf
(
stderr
,
"Error while writing audio frame
\n
"
);
fprintf
(
stderr
,
"Error while writing audio frame
\n
"
);
exit
(
1
);
exit
(
1
);
}
}
av_frame_free
(
&
frame
);
}
static
void
close_audio
(
AVFormatContext
*
oc
,
OutputStream
*
ost
)
{
avcodec_close
(
ost
->
st
->
codec
);
}
}
/**************************************************************/
/**************************************************************/
...
@@ -399,7 +396,7 @@ static void write_video_frame(AVFormatContext *oc, OutputStream *ost)
...
@@ -399,7 +396,7 @@ static void write_video_frame(AVFormatContext *oc, OutputStream *ost)
frame_count
++
;
frame_count
++
;
}
}
static
void
close_
video
(
AVFormatContext
*
oc
,
OutputStream
*
ost
)
static
void
close_
stream
(
AVFormatContext
*
oc
,
OutputStream
*
ost
)
{
{
avcodec_close
(
ost
->
st
->
codec
);
avcodec_close
(
ost
->
st
->
codec
);
av_frame_free
(
&
ost
->
frame
);
av_frame_free
(
&
ost
->
frame
);
...
@@ -411,7 +408,7 @@ static void close_video(AVFormatContext *oc, OutputStream *ost)
...
@@ -411,7 +408,7 @@ static void close_video(AVFormatContext *oc, OutputStream *ost)
int
main
(
int
argc
,
char
**
argv
)
int
main
(
int
argc
,
char
**
argv
)
{
{
OutputStream
video_st
,
audio_st
;
OutputStream
video_st
=
{
0
}
,
audio_st
=
{
0
}
;
const
char
*
filename
;
const
char
*
filename
;
AVOutputFormat
*
fmt
;
AVOutputFormat
*
fmt
;
AVFormatContext
*
oc
;
AVFormatContext
*
oc
;
...
@@ -517,9 +514,9 @@ int main(int argc, char **argv)
...
@@ -517,9 +514,9 @@ int main(int argc, char **argv)
/* Close each codec. */
/* Close each codec. */
if
(
have_video
)
if
(
have_video
)
close_
video
(
oc
,
&
video_st
);
close_
stream
(
oc
,
&
video_st
);
if
(
have_audio
)
if
(
have_audio
)
close_
audio
(
oc
,
&
audio_st
);
close_
stream
(
oc
,
&
audio_st
);
/* Free the streams. */
/* Free the streams. */
for
(
i
=
0
;
i
<
oc
->
nb_streams
;
i
++
)
{
for
(
i
=
0
;
i
<
oc
->
nb_streams
;
i
++
)
{
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment