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
f27e262d
Commit
f27e262d
authored
Oct 19, 2016
by
Anton Khirnov
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
examples/encode_audio: switch to the new audio encoding API
parent
44c9f374
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
43 additions
and
16 deletions
+43
-16
encode_audio.c
doc/examples/encode_audio.c
+43
-16
No files found.
doc/examples/encode_audio.c
View file @
f27e262d
...
@@ -89,14 +89,42 @@ static int select_channel_layout(const AVCodec *codec)
...
@@ -89,14 +89,42 @@ static int select_channel_layout(const AVCodec *codec)
return
best_ch_layout
;
return
best_ch_layout
;
}
}
static
void
encode
(
AVCodecContext
*
ctx
,
AVFrame
*
frame
,
AVPacket
*
pkt
,
FILE
*
output
)
{
int
ret
;
/* send the frame for encoding */
ret
=
avcodec_send_frame
(
ctx
,
frame
);
if
(
ret
<
0
)
{
fprintf
(
stderr
,
"error sending the frame to the encoder
\n
"
);
exit
(
1
);
}
/* read all the available output packets (in general there may be any
* number of them */
while
(
ret
>=
0
)
{
ret
=
avcodec_receive_packet
(
ctx
,
pkt
);
if
(
ret
==
AVERROR
(
EAGAIN
)
||
ret
==
AVERROR_EOF
)
return
;
else
if
(
ret
<
0
)
{
fprintf
(
stderr
,
"error encoding audio frame
\n
"
);
exit
(
1
);
}
fwrite
(
pkt
->
data
,
1
,
pkt
->
size
,
output
);
av_packet_unref
(
pkt
);
}
}
int
main
(
int
argc
,
char
**
argv
)
int
main
(
int
argc
,
char
**
argv
)
{
{
const
char
*
filename
;
const
char
*
filename
;
const
AVCodec
*
codec
;
const
AVCodec
*
codec
;
AVCodecContext
*
c
=
NULL
;
AVCodecContext
*
c
=
NULL
;
AVFrame
*
frame
;
AVFrame
*
frame
;
AVPacket
pkt
;
AVPacket
*
pkt
;
int
i
,
j
,
k
,
ret
,
got_output
;
int
i
,
j
,
k
,
ret
;
FILE
*
f
;
FILE
*
f
;
uint16_t
*
samples
;
uint16_t
*
samples
;
float
t
,
tincr
;
float
t
,
tincr
;
...
@@ -147,6 +175,13 @@ int main(int argc, char **argv)
...
@@ -147,6 +175,13 @@ int main(int argc, char **argv)
exit
(
1
);
exit
(
1
);
}
}
/* packet for holding encoded output */
pkt
=
av_packet_alloc
();
if
(
!
pkt
)
{
fprintf
(
stderr
,
"could not allocate the packet
\n
"
);
exit
(
1
);
}
/* frame containing input raw audio */
/* frame containing input raw audio */
frame
=
av_frame_alloc
();
frame
=
av_frame_alloc
();
if
(
!
frame
)
{
if
(
!
frame
)
{
...
@@ -169,10 +204,6 @@ int main(int argc, char **argv)
...
@@ -169,10 +204,6 @@ int main(int argc, char **argv)
t
=
0
;
t
=
0
;
tincr
=
2
*
M_PI
*
440
.
0
/
c
->
sample_rate
;
tincr
=
2
*
M_PI
*
440
.
0
/
c
->
sample_rate
;
for
(
i
=
0
;
i
<
200
;
i
++
)
{
for
(
i
=
0
;
i
<
200
;
i
++
)
{
av_init_packet
(
&
pkt
);
pkt
.
data
=
NULL
;
// packet data will be allocated by the encoder
pkt
.
size
=
0
;
/* make sure the frame is writable -- makes a copy if the encoder
/* make sure the frame is writable -- makes a copy if the encoder
* kept a reference internally */
* kept a reference internally */
ret
=
av_frame_make_writable
(
frame
);
ret
=
av_frame_make_writable
(
frame
);
...
@@ -187,19 +218,15 @@ int main(int argc, char **argv)
...
@@ -187,19 +218,15 @@ int main(int argc, char **argv)
samples
[
2
*
j
+
k
]
=
samples
[
2
*
j
];
samples
[
2
*
j
+
k
]
=
samples
[
2
*
j
];
t
+=
tincr
;
t
+=
tincr
;
}
}
/* encode the samples */
encode
(
c
,
frame
,
pkt
,
f
);
ret
=
avcodec_encode_audio2
(
c
,
&
pkt
,
frame
,
&
got_output
);
if
(
ret
<
0
)
{
fprintf
(
stderr
,
"error encoding audio frame
\n
"
);
exit
(
1
);
}
if
(
got_output
)
{
fwrite
(
pkt
.
data
,
1
,
pkt
.
size
,
f
);
av_packet_unref
(
&
pkt
);
}
}
}
/* flush the encoder */
encode
(
c
,
NULL
,
pkt
,
f
);
fclose
(
f
);
fclose
(
f
);
av_frame_free
(
&
frame
);
av_frame_free
(
&
frame
);
av_packet_free
(
&
pkt
);
avcodec_free_context
(
&
c
);
avcodec_free_context
(
&
c
);
}
}
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