-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathffmpeg_decode.cpp
More file actions
188 lines (156 loc) · 5.55 KB
/
ffmpeg_decode.cpp
File metadata and controls
188 lines (156 loc) · 5.55 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
/* Decode audio file using ffmpeg and write decoded samples to stdout.
* Output format:
* - two channels (front left, front right)
* - samples in interleaved format (L R L R ...)
* - samples are 32-bit floats
* - sample rate is 44100
*
* Usage:
* ./ffmpeg_decode cool_song.mp3 > cool_song_samples
*/
#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>
#include <assert.h>
extern "C" {
#include <libavformat/avformat.h>
#include <libavcodec/avcodec.h>
#include <libswresample/swresample.h>
}
int main(int argc, char** argv) {
if (argc != 2) {
fprintf(stderr, "usage: %s input_file > output_file\n", argv[0]);
exit(1);
}
const int out_channels = 2, out_samples = 512, sample_rate = 44100;
const int max_buffer_size =
av_samples_get_buffer_size(
NULL, out_channels, out_samples, AV_SAMPLE_FMT_S16, 1);
// register supported formats and codecs
av_register_all();
// allocate empty format context
// provides methods for reading input packets
AVFormatContext* fmt_ctx = avformat_alloc_context();
assert(fmt_ctx);
// determine input file type and initialize format context
if (avformat_open_input(&fmt_ctx, argv[1], NULL, NULL) != 0) {
fprintf(stderr, "error: avformat_open_input()\n");
exit(1);
}
// determine supported codecs for input file streams and add
// them to format context
if (avformat_find_stream_info(fmt_ctx, NULL) < 0) {
fprintf(stderr, "error: avformat_find_stream_info()\n");
exit(1);
}
#if 0
av_dump_format(fmt_ctx, 0, argv[1], false);
#endif
// find audio stream in format context
size_t stream = 0;
for (; stream < fmt_ctx->nb_streams; stream++) {
if (fmt_ctx->streams[stream]->codec->codec_type == AVMEDIA_TYPE_AUDIO) {
break;
}
}
if (stream == fmt_ctx->nb_streams) {
fprintf(stderr, "error: no audio stream found\n");
exit(1);
}
// get codec context for audio stream
// provides methods for decoding input packets received from format context
AVCodecContext* codec_ctx = fmt_ctx->streams[stream]->codec;
assert(codec_ctx);
if (codec_ctx->channel_layout == 0) {
codec_ctx->channel_layout = AV_CH_FRONT_LEFT | AV_CH_FRONT_RIGHT;
}
// find decoder for audio stream
AVCodec* codec = avcodec_find_decoder(codec_ctx->codec_id);
if (!codec) {
fprintf(stderr, "error: avcodec_find_decoder()\n");
exit(1);
}
// initialize codec context with decoder we've found
if (avcodec_open2(codec_ctx, codec, NULL) < 0) {
fprintf(stderr, "error: avcodec_open2()\n");
exit(1);
}
// initialize converter from input audio stream to output stream
// provides methods for converting decoded packets to output stream
SwrContext* swr_ctx =
swr_alloc_set_opts(NULL,
AV_CH_FRONT_LEFT | AV_CH_FRONT_RIGHT, // output
AV_SAMPLE_FMT_S16, // output
sample_rate, // output
codec_ctx->channel_layout, // input
codec_ctx->sample_fmt, // input
codec_ctx->sample_rate, // input
0,
NULL);
if (!swr_ctx) {
fprintf(stderr, "error: swr_alloc_set_opts()\n");
exit(1);
}
swr_init(swr_ctx);
// create empty packet for input stream
AVPacket packet;
av_init_packet(&packet);
packet.data = NULL;
packet.size = 0;
// allocate empty frame for decoding
AVFrame* frame = av_frame_alloc();
assert(frame);
// allocate buffer for output stream
uint8_t* buffer = (uint8_t*)av_malloc(max_buffer_size);
assert(buffer);
// read packet from input audio file
while (av_read_frame(fmt_ctx, &packet) >= 0) {
// skip non-audio packets
if (packet.stream_index != stream) {
continue;
}
// decode packet to frame
int got_frame = 0;
if (avcodec_decode_audio4(codec_ctx, frame, &got_frame, &packet) < 0) {
fprintf(stderr, "error: avcodec_decode_audio4()\n");
exit(1);
}
if (!got_frame) {
continue;
}
// convert input frame to output buffer
int got_samples = swr_convert(
swr_ctx,
&buffer, out_samples,
(const uint8_t **)frame->data, frame->nb_samples);
if (got_samples < 0) {
fprintf(stderr, "error: swr_convert()\n");
exit(1);
}
while (got_samples > 0) {
int buffer_size =
av_samples_get_buffer_size(
NULL, out_channels, got_samples, AV_SAMPLE_FMT_S16, 1);
assert(buffer_size <= max_buffer_size);
// write output buffer to stdout
if (write(STDOUT_FILENO, buffer, buffer_size) != buffer_size) {
fprintf(stderr, "error: write(stdout)\n");
exit(1);
}
// process samples buffered inside swr context
got_samples = swr_convert(swr_ctx, &buffer, out_samples, NULL, 0);
if (got_samples < 0) {
fprintf(stderr, "error: swr_convert()\n");
exit(1);
}
}
// free packet created by decoder
av_free_packet(&packet);
}
av_free(buffer);
av_frame_free(&frame);
swr_free(&swr_ctx);
avcodec_close(codec_ctx);
avformat_close_input(&fmt_ctx);
return 0;
}