-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy path02-reading-from-memory.cpp
358 lines (291 loc) · 13 KB
/
02-reading-from-memory.cpp
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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
/*
*
* File: 02-reading-from-memory.cpp
*
* Author: Rim Zaydullin
* Repo: https://github.com/tinybit/ffmpeg_code_examples
*
* more advanced libav remuxing example.
* read video file from disk, write data into memory buffer, configure AVFormatContext to use
* customized AVIOContext to read from memory buffer, remux to FLV and write result to a file
*
* input file requirements:
* - video must be encoded with wither h264 or vp6 video codecs
* - audio must be encoded with mp3 or aac codecs
* the above are FLV container limitations
*
*/
#include <stdio.h>
#include <stdlib.h>
#include <iostream>
#include <fstream>
extern "C" {
#include <libavformat/avformat.h>
}
#include "helpers.hpp"
// FileReader class emulates reading from memory, you can implement your own memory reader/buffer following this
// code. you only need to feed AVIOContext.read_packet callback with data, that's all.
// AVIOContext.read_packet callback will be called during av_read_frame(context, packet) and other context read
// operations. see more in functions make_input_ctx(), read_callback() below
class FileReader {
public:
FileReader(const char* filename) {
input_file.open(filename, std::ifstream::binary | std::ifstream::in);
}
int read(char* data, int size) {
if (input_file.eof()) {
return -1;
}
input_file.read(data, size);
return input_file.gcount();
}
void close() {
input_file.close();
}
std::ifstream input_file;
};
// functions predeclarations
bool make_input_ctx(AVFormatContext** input_ctx, AVIOContext** avio_input_ctx, FileReader* reader, const char* filename);
bool make_output_ctx(AVFormatContext** output_ctx, const char* format_name, const char* filename);
bool make_streams_map(AVFormatContext** input_ctx, int** streams_map);
bool ctx_init_output_from_input(AVFormatContext** input_ctx, AVFormatContext** output_ctx);
bool open_output_file(AVFormatContext** output_ctx, const char* filename);
bool remux_streams(AVFormatContext** input_ctx, AVFormatContext** output_ctx, int* streams_map);
bool close_output_file(AVFormatContext** output_ctx);
int main(int argc, char **argv) {
if (argc != 3) {
std::cout << "Usage: " << argv[0] << " <input file> <output file>\n";
return EXIT_FAILURE;
}
const char* in_filename = argv[1];
const char* out_filename = argv[2];
// create input format context
FileReader reader(in_filename); // this is out "memory reader"
AVIOContext* avio_input_ctx = NULL; // this is IO (input/output) context, needed for i/o customizations
AVFormatContext* input_ctx = NULL; // this is AV (audio/video) context
if (!make_input_ctx(&input_ctx, &avio_input_ctx, &reader, in_filename)) {
return EXIT_FAILURE;
}
// create output format context
AVFormatContext* output_ctx = NULL; // this is AV (audio/video) context
if (!make_output_ctx(&output_ctx, "flv", out_filename)) {
return EXIT_FAILURE;
}
// create streams map, filtering out all streams except audio/video
int* streams_map = NULL;
if (!make_streams_map(&input_ctx, &streams_map)) {
return EXIT_FAILURE;
}
// init output context from input context (create output streams in output context, copying codec params from input)
if (!ctx_init_output_from_input(&input_ctx, &output_ctx)) {
return EXIT_FAILURE;
}
// dump input and output formats/streams info
// https://ffmpeg.org/doxygen/trunk/group__lavf__misc.html#gae2645941f2dc779c307eb6314fd39f10
std::cout << "-------------------------------- IN ------------------------------------\n";
av_dump_format(input_ctx, 0, in_filename, 0);
std::cout << "-------------------------------- OUT -----------------------------------\n";
av_dump_format(output_ctx, 0, out_filename, 1);
std::cout << "------------------------------------------------------------------------\n";
// create and open output file and write file header
if (!open_output_file(&output_ctx, out_filename)) {
return EXIT_FAILURE;
}
// read input file streams, remux them and write into output file
if (!remux_streams(&input_ctx, &output_ctx, streams_map)) {
return EXIT_FAILURE;
}
// close output file
if (!close_output_file(&output_ctx)) {
return EXIT_FAILURE;
}
// close input context
avformat_close_input(&input_ctx);
// close our "memory reader"
reader.close();
// cleanup: free memory
avformat_free_context(input_ctx);
avformat_free_context(output_ctx);
av_freep(&streams_map);
av_freep(&avio_input_ctx);
return EXIT_SUCCESS;
}
// this callback will be used for our custom i/o context (AVIOContext)
static int read_callback(void* opaque, uint8_t* buf, int buf_size) {
auto& reader = *reinterpret_cast<FileReader*>(opaque);
int read_data_size = reader.read((char*)buf, buf_size);
if (read_data_size == -1) {
return AVERROR_EOF; // this is the way to tell our input context that there's no more data
}
return read_data_size;
}
bool make_input_ctx(AVFormatContext** input_ctx, AVIOContext** avio_input_ctx, FileReader* reader, const char* filename) {
// now we need to allocate a memory buffer for our context to use. keep in mind, that buffer size
// should be chosen correctly for various containers, this noticeably affectes performance
// NOTE: this buffer is managed by AVIOContext and you should not deallocate by yourself
const size_t buffer_size = 8192;
unsigned char* ctx_buffer = (unsigned char*)(av_malloc(buffer_size));
if (ctx_buffer == NULL) {
std::cout << "Could not allocate read buffer for AVIOContext\n";
return false;
}
// let's setup a custom AVIOContext for AVFormatContext
// cast reader to convenient short variable
void* reader_ptr = reinterpret_cast<void*>(static_cast<FileReader*>(reader));
// now the important part, we need to create a custom AVIOContext, provide it buffer and
// buffer size for reading and read callback that will do the actual reading into the buffer
*avio_input_ctx = avio_alloc_context(
ctx_buffer, // memory buffer
buffer_size, // memory buffer size
0, // 0 for reading, 1 for writing. we're reading, so — 0.
reader_ptr, // pass our reader to context, it will be transparenty passed to read callback on each invocation
&read_callback, // out read callback
NULL, // write callback — we don't need one
NULL // seek callback - we don't need one
);
// allocate new AVFormatContext
*input_ctx = avformat_alloc_context();
// assign our new and shiny custom i/o context to AVFormatContext
(*input_ctx)->pb = *avio_input_ctx;
// tell our input context that we're using custom i/o and there's no backing file
(*input_ctx)->flags |= AVFMT_FLAG_CUSTOM_IO | AVFMT_NOFILE;
// note "some_dummy_filename", ffmpeg requires it as some default non-empty placeholder
int ret = avformat_open_input(input_ctx, "some_dummy_filename", NULL, NULL);
if (ret < 0) {
std::cout << "Could not open input file " << filename << ", reason: " << av_err2str(ret) << '\n';
return false;
}
ret = avformat_find_stream_info(*input_ctx, NULL);
if (ret < 0) {
std::cout << "Failed to retrieve input stream information from " << filename << ", reason: " << av_err2str(ret) << '\n';
return false;
}
return true;
}
bool make_output_ctx(AVFormatContext** output_ctx, const char* format_name, const char* filename) {
int ret = avformat_alloc_output_context2(output_ctx, NULL, format_name, filename);
if (ret < 0) {
std::cout << "Could not create output context, reason: " << av_err2str(ret) << '\n';
return false;
}
if (!(*output_ctx)) {
std::cout << "Could not create output context, no further details.\n";
return false;
}
return true;
}
bool make_streams_map(AVFormatContext** input_ctx, int** streams_map) {
int* smap = NULL;
int stream_index = 0;
int input_streams_count = (*input_ctx)->nb_streams;
smap = (int*)av_mallocz_array(input_streams_count, sizeof(int));
if (!smap) {
std::cout << "Could not allocate streams list.\n";
return false;
}
for (int i = 0; i < input_streams_count; i++) {
AVCodecParameters* c = (*input_ctx)->streams[i]->codecpar;
if (c->codec_type != AVMEDIA_TYPE_AUDIO && c->codec_type != AVMEDIA_TYPE_VIDEO) {
smap[i] = -1;
continue;
}
smap[i] = stream_index++;
}
*streams_map = smap;
return true;
}
bool ctx_init_output_from_input(AVFormatContext** input_ctx, AVFormatContext** output_ctx) {
int input_streams_count = (*input_ctx)->nb_streams;
for (int i = 0; i < input_streams_count; i++) {
AVStream* in_stream = (*input_ctx)->streams[i];
AVCodecParameters* in_codecpar = in_stream->codecpar;
if (in_codecpar->codec_type != AVMEDIA_TYPE_AUDIO && in_codecpar->codec_type != AVMEDIA_TYPE_VIDEO) {
continue;
}
AVStream* out_stream = avformat_new_stream(*output_ctx, NULL);
if (!out_stream) {
std::cout << "Failed allocating output stream\n";
return false;
}
int ret = avcodec_parameters_copy(out_stream->codecpar, in_codecpar);
if (ret < 0) {
std::cout << "Failed to copy codec parameters, reason: " << av_err2str(ret) << '\n';
return false;
}
// set stream codec tag to 0, for libav to detect automatically
out_stream->codecpar->codec_tag = 0;
}
return true;
}
bool open_output_file(AVFormatContext** output_ctx, const char* filename) {
// unless it's a no file (we'll talk later about that) write to the disk (FLAG_WRITE)
// but basically it's a way to save the file to a buffer so you can store it
// wherever you want.
int ret = avio_open(&((*output_ctx)->pb), filename, AVIO_FLAG_WRITE);
if (ret < 0) {
std::cout << "Could not open output file " << filename << ", reason: " << av_err2str(ret) << '\n';
return false;
}
// https://ffmpeg.org/doxygen/trunk/group__lavf__encoding.html#ga18b7b10bb5b94c4842de18166bc677cb
ret = avformat_write_header(*output_ctx, NULL);
if (ret < 0) {
std::cout << "Failed to write output file header to " << filename << ", reason: " << av_err2str(ret) << '\n';
return false;
}
return true;
}
bool remux_streams(AVFormatContext** input_ctx, AVFormatContext** output_ctx, int* streams_map) {
AVPacket packet;
int input_streams_count = (*input_ctx)->nb_streams;
while (1) {
int ret = av_read_frame(*input_ctx, &packet);
if (ret == AVERROR_EOF) { // we have reached end of input file
break;
}
// handle any other error
if (ret < 0) {
std::cout << "Failed to read packet from input, reason: " << av_err2str(ret) << '\n';
return false;
}
// ignore any packets that are present in non-mapped streams
if (packet.stream_index >= input_streams_count || streams_map[packet.stream_index] < 0) {
av_packet_unref(&packet);
continue;
}
// set stream index, based on our map
packet.stream_index = streams_map[packet.stream_index];
/* copy packet */
AVStream* in_stream = (*input_ctx)->streams[packet.stream_index];
AVStream* out_stream = (*output_ctx)->streams[packet.stream_index];
packet.pts = av_rescale_q_rnd(packet.pts, in_stream->time_base, out_stream->time_base, AVRounding(AV_ROUND_NEAR_INF|AV_ROUND_PASS_MINMAX));
packet.dts = av_rescale_q_rnd(packet.dts, in_stream->time_base, out_stream->time_base, AVRounding(AV_ROUND_NEAR_INF|AV_ROUND_PASS_MINMAX));
packet.duration = av_rescale_q(packet.duration, in_stream->time_base, out_stream->time_base);
// https://ffmpeg.org/doxygen/trunk/structAVPacket.html#ab5793d8195cf4789dfb3913b7a693903
packet.pos = -1;
//https://ffmpeg.org/doxygen/trunk/group__lavf__encoding.html#ga37352ed2c63493c38219d935e71db6c1
ret = av_interleaved_write_frame(*output_ctx, &packet);
if (ret < 0) {
std::cout << "Failed to write packet to output, reason: " << av_err2str(ret) << '\n';
return false;
}
av_packet_unref(&packet);
}
return true;
}
bool close_output_file(AVFormatContext** output_ctx) {
//https://ffmpeg.org/doxygen/trunk/group__lavf__encoding.html#ga7f14007e7dc8f481f054b21614dfec13
int ret = av_write_trailer(*output_ctx);
if (ret < 0) {
std::cout << "Failed to write trailer to output, reason: " << av_err2str(ret) << '\n';
return false;
}
/* close output */
if (output_ctx && !((*output_ctx)->oformat->flags & AVFMT_NOFILE)) {
ret = avio_closep(&(*output_ctx)->pb);
if (ret < 0) {
std::cout << "Failed to close AV output, reason: " << av_err2str(ret) << '\n';
return false;
}
}
return true;
}