26 #include "VideoSink.hh"
27 #include <VideoSource/VideoSource_Disk.hh>
32 : _Bitrate(-1), _BPP(4)
34 static bool firstCall =
true;
53 : _ImgFiles(Images), _Video(Video)
63 _ImgFiles.push_back(Image);
67 _ImgFiles.insert(_ImgFiles.end(), Images.begin(), Images.end());
111 int width,
int height)
119 int size = width * height;
126 for (
int y = 0; y < height; y++)
128 for (
int x = 0; x < width; x++)
132 g = *(rgb + size + p);
133 b = *(rgb + 2*size + p);
136 *(yuv + p) = (0.257*r) + (0.504*g) + (0.098*b) + 16;
138 if (y % 2 == 0 && x % 2 == 0)
144 + *(rgb + p + width + 1)) / 4;
146 + *(rgb + size + p + 1)
147 + *(rgb + size + p + width)
148 + *(rgb + size + p + width + 1)) / 4;
150 + *(rgb + 2*size + p + 1)
151 + *(rgb + 2*size + p + width)
152 + *(rgb + 2*size + p + width + 1)) / 4;
154 u = yuv + size + (y/2) * (width/2) + x/2;
155 *u = -(0.148*r) - (0.291*g) + (0.439*b) + 128;
157 *(u + size/4) = (0.439*r) - (0.368*g) - (0.071*b) + 128;
178 if (!_SetupVideoSource())
182 h = _VSD.GetHeight();
185 _Codec = avcodec_find_encoder(CODEC_ID_MPEG1VIDEO);
188 _ErrMsg =
"Codec not found";
191 cout <<
"Privdatasize: " << _Codec->priv_data_size << endl;
193 _CodecCtx = avcodec_alloc_context();
194 pict = avcodec_alloc_frame();
195 yuvPict = avcodec_alloc_frame();
197 int bitrate = _Bitrate;
199 bitrate = _BPP * size;
200 _CodecCtx->bit_rate = bitrate;
201 _CodecCtx->width = w;
202 _CodecCtx->height = h;
203 _CodecCtx->time_base.num = 1;
204 _CodecCtx->time_base.den = _FPS;
205 _CodecCtx->gop_size = 10;
206 _CodecCtx->pix_fmt = PIX_FMT_YUV420P;
207 _CodecCtx->priv_data = (
void*)
"title=BIAS FFMPEG Encoder";
209 if (avcodec_open(_CodecCtx, _Codec) < 0)
211 _ErrMsg =
"Could not open codec";
215 f = fopen(_Video.c_str(),
"wb");
218 _ErrMsg = string(
"Could not open output file: ") + _Video;
223 outbuf = (uint8_t*)malloc(outbufSize);
225 pictBuf = (uint8_t*)malloc(size * 3);
227 pictBuf = (uint8_t*)malloc((size * 3) / 2);
229 pict->linesize[0] = w;
230 pict->linesize[1] = w;
231 pict->linesize[2] = w;
232 pict->data[0] = pictBuf;
233 pict->data[1] = pictBuf + size;
234 pict->data[2] = pictBuf + size + size;
238 yuvBuf = (uint8_t*)malloc((size * 3) / 2);
240 yuvPict->linesize[0] = w;
241 yuvPict->linesize[1] = w / 2;
242 yuvPict->linesize[2] = w / 2;
243 yuvPict->data[0] = yuvBuf;
244 yuvPict->data[1] = yuvBuf + size;
245 yuvPict->data[2] = yuvBuf + size + size / 4;
255 while (_VSD.GetNumFramesRemaining() != 0)
259 if (_VSD.GrabSingle(img) != 0)
261 _ErrMsg =
"Error grabbing image";
272 _RGB888pToYUV420p((uint8_t*)img.
GetImageData(), yuvBuf, w, h);
279 outSize = avcodec_encode_video(_CodecCtx, outbuf, outbufSize, yuvPict);
280 fwrite(outbuf, 1, outSize, f);
282 cout <<
"Frame " << i <<
", remaining: "
283 << _VSD.GetNumFramesRemaining() << endl;
292 fwrite(outbuf,
sizeof(uint8_t), 4, f);
299 avcodec_close(_CodecCtx);
316 _VSD.SetLoopMode(
false);
318 if (_VSD.OpenDevice(_ImgFiles) != 0)
320 _ErrMsg =
"Loading images failed";
331 _ErrMsg =
"VideoSource returns unsupported colormodel";
EColorModel
These are the most often used color models.
void SetBitrate(int Bitrate)
Set the bitrate of the output video.
void SetFPS(float FPS)
Set the framerate for the video.
bool Encode()
Encode the video.
static void _RGB888pToYUV420p(uint8_t *rgb, uint8_t *yuv, int width, int height)
Convert RGB888p data to Y'UV420p.
static BIASImageBase_EXPORT int ToPlanar(const Image< StorageType > &source, Image< StorageType > &dest)
Converts the existing image to an planar version.
unsigned int GetWidth() const
YUV420P, 2 channels, full luminance Y, 1 U, 1 V. Y, U and V are grouped together for better compressi...
std::string GetErrMsg()
Return the last produced errormessage.
color values, 3 channels, order: red,green,blue
void SetBPP(double BPP)
Set the bits per pixel of the output video.
The image template class for specific storage types.
const StorageType * GetImageData() const
overloaded GetImageData() from ImageBase
void SetOutputVideo(std::string Video)
Set the file where the video will be written to.
void AddImage(std::string Image)
Append an image to the end of the list.
void AddImages(std::vector< std::string > Images)