Commit 357537f1 authored by Philippe Gorley's avatar Philippe Gorley Committed by Andreas Traczyk

debug: add video writer

Writes raw frames of any pixel format. File can be played with:
ffplay -f rawvideo -pixel_format <format> -video_size <width>x<height> <file>

Change-Id: I774642f84694a36c16436ba2aa1363edc7ee3d62
parent 2011f366
......@@ -24,8 +24,11 @@
#include "audio/resampler.h"
#include "libav_deps.h"
#include "media_io_handle.h"
#include "system_codec_container.h"
#include <chrono>
#include <cstdio>
#include <fstream>
#include <ios>
#include <ratio>
......@@ -176,4 +179,63 @@ private:
std::unique_ptr<Resampler> resampler_; // convert from float to integer samples
* Minimally invasive video writer. Writes raw frames. Helps debug what goes wrong with video.
class VideoWriter {
VideoWriter(const std::string& filename, AVPixelFormat format, int width, int height)
: filename_(filename)
, format_(format)
, width_(width)
, height_(height)
f_ = fopen(filename.c_str(), "wb");
// so an int (VideoFrame.format()) can be passed without casting
VideoWriter(const std::string& filename, int format, int width, int height)
: VideoWriter(filename, static_cast<AVPixelFormat>(format), width, height)
RING_DBG("Play video file with: ffplay -f rawvideo -pixel_format %s -video_size %dx%d %s",
av_get_pix_fmt_name(format_), width_, height_, filename_.c_str());
void write(VideoFrame& frame)
int ret = 0;
uint8_t* buffer = nullptr;
auto f = frame.pointer();
if (format_ != f->format || width_ != f->width || height_ != f->height)
int size = av_image_get_buffer_size(format_, width_, height_, 1);
buffer = reinterpret_cast<uint8_t*>(av_malloc(size));
if (!buffer) {
if ((ret = av_image_copy_to_buffer(buffer, size,
reinterpret_cast<const uint8_t* const*>(f->data),
reinterpret_cast<const int*>(f->linesize),
format_, width_, height_, 1)) < 0) {
fwrite(buffer, 1, size, f_);
FILE* f_;
std::string filename_;
AVPixelFormat format_;
int width_, height_;
}} // namespace ring::debug
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment