-
Notifications
You must be signed in to change notification settings - Fork 4
/
utils.hpp
81 lines (60 loc) · 2.32 KB
/
utils.hpp
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
#ifndef _UTILS_H_
#define _UTILS_H_
#include <fstream>
#include <iostream>
#include <sstream>
#include <stdexcept>
#include <cmath>
extern "C" {
#include <libavcodec/avcodec.h>
}
void saveFrame(const AVFrame* frame, int width, int height, int frameNumber, const std::string& _frame_base)
{
std::stringstream oname;
oname << _frame_base << "-slice" << frameNumber << ".ppm";
std::ofstream file(oname.str().c_str(),
std::ios_base::binary |
std::ios_base::trunc |
std::ios_base::out);
if (!file.good())
{
throw std::runtime_error("Unable to open the file to write the frame");
}
file << "P5\n" << width << '\n' << height << "\n255\n";
for (int i = 0; i < height; ++i)
{
file.write((char*)(frame->data[0] + i * frame->linesize[0]), width);
}
file.close();
}
//https://ffmpeg.org/doxygen/trunk/avio_reading_8c-example.html#a18
struct buffer_data {
const uint8_t *ptr;
size_t size; ///< size left in the buffer
};
static int read_packet(void *opaque, uint8_t *buf, int buf_size)
{
struct buffer_data *bd = (struct buffer_data *)opaque;
buf_size = std::min((decltype(bd->size))buf_size, bd->size);
printf("ptr:%p size:%zu\n", bd->ptr, bd->size);
/* copy internal buffer data to buf */
memcpy(buf, bd->ptr, buf_size);
bd->ptr += buf_size;
bd->size -= buf_size;
return buf_size;
}
// //http://stackoverflow.com/questions/9604633/reading-a-file-located-in-memory-with-libavformat
//
// static int readFunction(void* opaque, uint8_t* buf, int buf_size) {
// auto& me = *reinterpret_cast<buffer_data*>(opaque);
// me.read(reinterpret_cast<char*>(buf), buf_size);
// return ;
// }
// std::ifstream stream("file.avi", std::ios::binary);
// const std::shared_ptr<unsigned char> buffer(reinterpret_cast<unsigned char*>(av_malloc(8192)), &av_free);
// const std::shared_ptr<AVIOContext> avioContext(avio_alloc_context(buffer.get(), 8192, 0, reinterpret_cast<void*>(static_cast<std::istream*>(&stream)), &readFunction, nullptr, nullptr), &av_free);
// const auto avFormat = std::shared_ptr<AVFormatContext>(avformat_alloc_context(), &avformat_free_context);
// auto avFormatPtr = avFormat.get();
// avFormat->pb = avioContext.get();
// avformat_open_input(&avFormatPtr, "dummyFilename", nullptr, nullptr);
#endif /* _UTILS_H_ */