[dc92b3]: / src / AVHandler.h  Maximize  Restore  History

Download this file

244 lines (181 with data), 5.1 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
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
/* Copyright (C) 2005 Stefan van der Walt <stefan@sun.ac.za>
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are
met:
1. Redistributions of source code must retain the above copyright notice,
this list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution.
THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */
#ifndef _AVHANDLER_H
#define _AVHANDLER_H
#define VIDEO_OUTBUF_SIZE 200000
#define INT64_C
#define __STDC_CONSTANT_MACROS
#include <errno.h>
#include <ffmpeg/avformat.h>
#include <iostream>
class AVHandler {
public:
AVHandler() {
av_output = NULL;
av_input = NULL;
vstream = NULL;
astream = NULL;
frame = NULL;
rgbframe = NULL;
video_outbuf = NULL;
filename = "";
frame_nr = 0;
bitrate = 400000;
framerate = 25;
gop_size = 10;
width = 352;
height = 288;
codec_name = "msmpeg4v2";
title = "";
author = "";
comment = "";
lock_parameters = false;
}
~AVHandler(void);
int setup_write();
int setup_read();
void draw_background(unsigned char r, unsigned char g, unsigned char b);
// write rgbframe to file
int write_frame();
// read frame nr from file into rgbframe
int read_frame(unsigned int nr);
static void print_file_formats();
static void print_codecs();
// The following routines can be used before `setup_write'
void set_filename(const std::string &filename) {
this->filename = filename;
}
std::string get_filename() const {
return filename;
}
void set_codec(const std::string &codec) {
codec_name = codec;
}
std::string get_codec() const {
return codec_name;
}
std::string get_audio_codec() const {
if (!astream) return "";
AVCodec *codec;
codec = avcodec_find_decoder(astream->codec->codec_id);
if (!codec) {
return "";
}
return std::string(codec->name);
}
unsigned int get_audio_samplerate() const {
if (!astream) return 0;
return astream->codec->sample_rate;
}
unsigned int get_audio_channels() const {
if (!astream) return 0;
return astream->codec->channels;
}
void set_bitrate(const unsigned int br) {
bitrate = br;
}
unsigned int get_bitrate() const {
return bitrate;
}
void set_framerate(double fr) {
framerate = fr;
}
void set_gop_size(int gop) {
gop_size = gop;
}
double get_framerate() const {
return framerate;
}
void set_width(unsigned int width) {
this->width = width;
}
unsigned int get_width() const {
return width;
}
void set_height(unsigned int height) {
this->height = height;
}
unsigned int get_height() const {
return height;
}
unsigned int get_total_frames() const {
if (vstream) {
return (unsigned int)((double)framerate * vstream->duration / AV_TIME_BASE);
} else {
return 0;
}
}
unsigned int get_filesize() const {
if (av_input) {
return av_input->file_size;
} else {
return 0;
}
}
void set_title(const std::string &t) {
title = t;
}
std::string get_title() const {
return title;
}
void set_author(const std::string &a) {
author = a;
}
std::string get_author() const {
return author;
}
void set_comment(const std::string &c) {
comment = c;
}
std::string get_comment() const {
return comment;
}
AVFrame *get_rgbframe() {
return rgbframe;
}
static void set_log(std::ostream *log) {
AVHandler::out = log; }
private:
static std::ostream *out;
AVFormatContext *av_output;
AVFormatContext *av_input;
AVStream *vstream;
AVStream *astream;
AVFrame *frame;
AVFrame *rgbframe;
uint8_t *video_outbuf;
std::string filename;
int frame_nr;
unsigned int bitrate;
double framerate;
int gop_size;
unsigned int width;
unsigned int height;
std::string codec_name;
std::string title;
std::string author;
std::string comment;
bool lock_parameters;
int add_video_stream();
int init_video_codecs();
AVFrame *create_frame(PixelFormat fmt);
};
#endif