utils.cpp 40.7 KB
Newer Older
André Anjos's avatar
André Anjos committed
1 2 3 4 5 6 7
#include <set>
#include <boost/token_iterator.hpp>
#include <boost/format.hpp>

extern "C" {
#include <libavformat/avformat.h>
#include <libavcodec/avcodec.h>
8 9
#include <libavutil/opt.h>
#include <libavutil/pixdesc.h>
André Anjos's avatar
André Anjos committed
10 11
#include <libavutil/avstring.h>
#include <libavutil/mathematics.h>
12
#include <libavutil/imgutils.h>
André Anjos's avatar
André Anjos committed
13 14
}

15 16
#include <bob.core/logging.h>
#include <bob.core/assert.h>
André Anjos's avatar
André Anjos committed
17

André Anjos's avatar
André Anjos committed
18
#include "utils.h"
André Anjos's avatar
André Anjos committed
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

/**
 * Tries to find an encoder name through a decoder
 */
static AVCodec* try_find_through_decoder(const char* codecname) {
  AVCodec* tmp = avcodec_find_decoder_by_name(codecname);
  if (tmp) return avcodec_find_encoder(tmp->id);
  return 0;
}

/**
 * Returns a list of available codecs from what I wish to support
 */
static void check_codec_support(std::map<std::string, const AVCodec*>& retval) {

  std::string tmp[] = {
    "libvpx",
    "vp8",
    "wmv1",
    "wmv2",
    //"wmv3", /* no encoding support */
    "mjpeg",
    "mpegvideo", // the same as mpeg2video
    "mpeg1video",
    //"mpeg1video_vdpau", //hw accelerated mpeg1video decoding
    "mpeg2video", // the same as mpegvideo
    //"mpegvideo_vdpau", //hw accelerated mpegvideo decoding
    "mpeg4",
    "msmpeg4",
    //"msmpeg4v1", /* no encoding support */
    "msmpeg4v2", // the same as msmpeg4
    "ffv1", // buggy on ffmpeg >= 2.0
    //"h263p", //bogus on libav-0.8.4
    "h264",
    //"h264_vdpau", //hw accelerated h264 decoding
    //"theora", //buggy on some platforms
    //"libtheora", //buggy on some platforms
56
    "libopenh264",
André Anjos's avatar
André Anjos committed
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
    "libx264",
    "zlib",
  };

  std::set<std::string> wishlist(tmp, tmp + (sizeof(tmp)/sizeof(tmp[0])));

  for (AVCodec* it = av_codec_next(0); it != 0; it = av_codec_next(it) ) {
    if (wishlist.find(it->name) == wishlist.end()) continue; ///< ignore this codec
    if (it->type == AVMEDIA_TYPE_VIDEO) {
      auto exists = retval.find(std::string(it->name));
      if (exists != retval.end() && exists->second->id != it->id) {
        bob::core::warn << "Not overriding video codec \"" << it->long_name
          << "\" (" << it->name << ")" << std::endl;
      }
      else {
        // a codec is potentially available, check encoder and decoder
        bool has_decoder = (bool)(avcodec_find_decoder(it->id));
        bool has_encoder = (bool)(avcodec_find_encoder(it->id));
        if (!has_encoder) {
          has_encoder = (bool)try_find_through_decoder(it->name);
        }
        if (has_encoder && has_decoder) retval[it->name] = it;
        // else, skip this one (cannot test encoding loop)
      }
    }
  }
}

static void check_iformat_support(std::map<std::string, AVInputFormat*>& retval) {

  std::string tmp[] = {
    "avi",
    "mov",
    //"flv", //buggy
    "mp4",
  };

  std::set<std::string> wishlist(tmp, tmp + (sizeof(tmp)/sizeof(tmp[0])));

  for (AVInputFormat* it = av_iformat_next(0); it != 0; it = av_iformat_next(it) ) {
    std::vector<std::string> names;
98
    bob::io::video::tokenize_csv(it->name, names);
André Anjos's avatar
André Anjos committed
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
    for (auto k = names.begin(); k != names.end(); ++k) {
      if (wishlist.find(*k) == wishlist.end()) continue; ///< ignore this format
      auto exists = retval.find(*k);
      if (exists != retval.end()) {
        bob::core::warn << "Not overriding input video format \""
          << it->long_name << "\" (" << *k
          << ") which is already assigned to \"" << exists->second->long_name
          << "\"" << std::endl;
      }
      else retval[*k] = it;
    }
  }
}

static void check_oformat_support(std::map<std::string, AVOutputFormat*>& retval) {

  std::string tmp[] = {
    "avi",
    "mov",
    //"flv", //buggy
    "mp4",
  };

  std::set<std::string> wishlist(tmp, tmp + (sizeof(tmp)/sizeof(tmp[0])));

  for (AVOutputFormat* it = av_oformat_next(0); it != 0; it = av_oformat_next(it) ) {
    std::vector<std::string> names;
126
    bob::io::video::tokenize_csv(it->name, names);
André Anjos's avatar
André Anjos committed
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
    for (auto k = names.begin(); k != names.end(); ++k) {
      if (wishlist.find(*k) == wishlist.end()) continue; ///< ignore this format
      auto exists = retval.find(*k);
      if (exists != retval.end()) {
        bob::core::warn << "Not overriding input video format \""
          << it->long_name << "\" (" << *k
          << ") which is already assigned to \"" << exists->second->long_name
          << "\"" << std::endl;
      }
      else retval[*k] = it;
    }
  }
}

/**
 * Defines which combinations of codecs and output formats are valid.
 */
static void define_output_support_map(std::map<AVOutputFormat*, std::vector<const AVCodec*> >& retval) {
  std::map<std::string, const AVCodec*> cdict;
  check_codec_support(cdict);
  std::map<std::string, AVOutputFormat*> odict;
  check_oformat_support(odict);

  auto it = odict.find("avi");
  if (it != odict.end()) { // ".avi" format is available
    retval[it->second].clear();
    for (auto jt = cdict.begin(); jt != cdict.end(); ++jt) {
      retval[it->second].push_back(jt->second); // all formats are possible
    }
  }

  it = odict.find("mov");
  if (it != odict.end()) { // ".mov" format is available
    retval[it->second].clear();
    for (auto jt = cdict.begin(); jt != cdict.end(); ++jt) {
      retval[it->second].push_back(jt->second); // all formats are possible
    }
  }

  it = odict.find("mp4");
  if (it != odict.end()) { // ".mp4" format is available
    retval[it->second].clear();
    std::string tmp[] = {
      "libx264",
171
      "libopenh264",
André Anjos's avatar
André Anjos committed
172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188
      "h264",
      //"h264_vdpau",
      "mjpeg",
      "mpeg1video",
      //"mpegvideo_vdpau"
      "mpeg2video",
      "mpegvideo",
      "mpeg4",
    };
    std::vector<std::string> codecs(tmp, tmp + (sizeof(tmp)/sizeof(tmp[0])));
    for (auto jt = codecs.begin(); jt != codecs.end(); ++jt) {
      auto kt = cdict.find(*jt);
      if (kt != cdict.end()) retval[it->second].push_back(kt->second);
    }
  }
}

189
void bob::io::video::tokenize_csv(const char* what, std::vector<std::string>& values) {
André Anjos's avatar
André Anjos committed
190 191 192 193 194 195 196
  if (!what) return;
  boost::char_separator<char> sep(",");
  std::string w(what);
  boost::tokenizer< boost::char_separator<char> > tok(w, sep);
  for (auto k = tok.begin(); k != tok.end(); ++k) values.push_back(*k);
}

197
void bob::io::video::codecs_installed (std::map<std::string, const AVCodec*>& installed) {
André Anjos's avatar
André Anjos committed
198 199 200 201 202 203 204 205 206 207 208 209 210 211
  for (AVCodec* it = av_codec_next(0); it != 0; it = av_codec_next(it) ) {
    if (it->type == AVMEDIA_TYPE_VIDEO) {
      /**
      auto exists = installed.find(std::string(it->name));
      if (exists != installed.end() && exists->second->id != it->id) {
        bob::core::warn << "Not overriding video codec \"" << it->long_name
          << "\" (" << it->name << ")" << std::endl;
      }
      else **/
      installed[it->name] = it;
    }
  }
}

212
void bob::io::video::codecs_supported (std::map<std::string, const AVCodec*>& installed) {
André Anjos's avatar
André Anjos committed
213 214 215
  check_codec_support(installed);
}

216
bool bob::io::video::codec_is_supported (const std::string& name) {
André Anjos's avatar
André Anjos committed
217
  std::map<std::string, const AVCodec*> cdict;
218
  bob::io::video::codecs_supported(cdict);
André Anjos's avatar
André Anjos committed
219 220 221
  return (cdict.find(name) != cdict.end());
}

222
void bob::io::video::iformats_supported (std::map<std::string, AVInputFormat*>& installed) {
André Anjos's avatar
André Anjos committed
223 224 225
  check_iformat_support(installed);
}

226
bool bob::io::video::iformat_is_supported (const std::string& name) {
André Anjos's avatar
André Anjos committed
227
  std::map<std::string, AVInputFormat*> idict;
228
  bob::io::video::iformats_supported(idict);
André Anjos's avatar
André Anjos committed
229
  std::vector<std::string> names;
230
  bob::io::video::tokenize_csv(name.c_str(), names);
André Anjos's avatar
André Anjos committed
231 232 233 234 235 236
  for (auto k = names.begin(); k != names.end(); ++k) {
    if (idict.find(*k) != idict.end()) return true;
  }
  return false;
}

237
void bob::io::video::iformats_installed (std::map<std::string, AVInputFormat*>& installed) {
André Anjos's avatar
André Anjos committed
238 239
  for (AVInputFormat* it = av_iformat_next(0); it != 0; it = av_iformat_next(it) ) {
    std::vector<std::string> names;
240
    bob::io::video::tokenize_csv(it->name, names);
André Anjos's avatar
André Anjos committed
241 242 243 244 245 246 247 248 249 250 251 252 253
    for (auto k = names.begin(); k != names.end(); ++k) {
      auto exists = installed.find(*k);
      if (exists != installed.end()) {
        bob::core::warn << "Not overriding input video format \""
          << it->long_name << "\" (" << *k
          << ") which is already assigned to \"" << exists->second->long_name
          << "\"" << std::endl;
      }
      else installed[*k] = it;
    }
  }
}

254
void bob::io::video::oformats_supported (std::map<std::string, AVOutputFormat*>& installed) {
André Anjos's avatar
André Anjos committed
255 256 257
  check_oformat_support(installed);
}

258
bool bob::io::video::oformat_is_supported (const std::string& name) {
André Anjos's avatar
André Anjos committed
259
  std::map<std::string, AVOutputFormat*> odict;
260
  bob::io::video::oformats_supported(odict);
André Anjos's avatar
André Anjos committed
261
  std::vector<std::string> names;
262
  bob::io::video::tokenize_csv(name.c_str(), names);
André Anjos's avatar
André Anjos committed
263 264 265 266 267 268
  for (auto k = names.begin(); k != names.end(); ++k) {
    if (odict.find(*k) != odict.end()) return true;
  }
  return false;
}

269
void bob::io::video::oformats_installed (std::map<std::string, AVOutputFormat*>& installed) {
André Anjos's avatar
André Anjos committed
270 271 272
  for (AVOutputFormat* it = av_oformat_next(0); it != 0; it = av_oformat_next(it) ) {
    if (!it->video_codec) continue;
    std::vector<std::string> names;
273
    bob::io::video::tokenize_csv(it->name, names);
André Anjos's avatar
André Anjos committed
274 275 276 277 278 279 280 281 282 283 284 285 286
    for (auto k = names.begin(); k != names.end(); ++k) {
      auto exists = installed.find(*k);
      if (exists != installed.end()) {
        bob::core::warn << "Not overriding output video format \""
          << it->long_name << "\" (" << *k
          << ") which is already assigned to \"" << exists->second->long_name
          << "\"" << std::endl;
      }
      else installed[*k] = it;
    }
  }
}

287
void bob::io::video::oformat_supported_codecs (const std::string& name,
André Anjos's avatar
André Anjos committed
288 289 290 291
    std::vector<const AVCodec*>& installed) {
  std::map<AVOutputFormat*, std::vector<const AVCodec*> > format2codec;
  define_output_support_map(format2codec);
  std::map<std::string, AVOutputFormat*> odict;
292
  bob::io::video::oformats_supported(odict);
André Anjos's avatar
André Anjos committed
293 294 295 296 297 298 299 300 301
  auto it = odict.find(name);
  if (it == odict.end()) {
    boost::format f("output format `%s' is not supported by this build");
    f % name;
    throw std::runtime_error(f.str());
  }
  installed = format2codec[it->second];
}

302
bool bob::io::video::oformat_supports_codec (const std::string& name,
André Anjos's avatar
André Anjos committed
303 304 305 306 307 308 309 310 311 312 313 314 315 316
    const std::string& codecname) {
  std::vector<const AVCodec*> codecs;
  oformat_supported_codecs(name, codecs);
  for (auto k=codecs.begin(); k!=codecs.end(); ++k) {
    if (codecname == (*k)->name) return true;
  }
  return false;
}

static std::string ffmpeg_error(int num) {
  static const int ERROR_SIZE = 1024;
  char message[ERROR_SIZE];
  int ok = av_strerror(num, message, ERROR_SIZE);
  if (ok < 0) {
317
    throw std::runtime_error("bob::io::video::av_strerror() failed to report - maybe you have a memory issue?");
André Anjos's avatar
André Anjos committed
318 319 320 321 322 323 324 325
  }
  return std::string(message);
}

static void deallocate_input_format_context(AVFormatContext* c) {
  avformat_close_input(&c);
}

326
boost::shared_ptr<AVFormatContext> bob::io::video::make_input_format_context(
André Anjos's avatar
André Anjos committed
327 328 329 330 331 332
    const std::string& filename) {

  AVFormatContext* retval = 0;

  int ok = avformat_open_input(&retval, filename.c_str(), 0, 0);
  if (ok != 0) {
333
    boost::format m("bob::io::video::avformat_open_input(filename=`%s') failed: ffmpeg reported %d == `%s'");
André Anjos's avatar
André Anjos committed
334 335 336 337 338 339 340 341 342 343 344
    m % filename % ok % ffmpeg_error(ok);
    throw std::runtime_error(m.str());
  }

  // creates and protects the return value
  boost::shared_ptr<AVFormatContext> shared_retval(retval, std::ptr_fun(deallocate_input_format_context));

  // retrieve stream information, throws if cannot find it
  ok = avformat_find_stream_info(retval, 0);

  if (ok < 0) {
345
    boost::format m("bob::io::video::avformat_find_stream_info(filename=`%s') failed: ffmpeg reported %d == `%s'");
André Anjos's avatar
André Anjos committed
346 347 348 349 350 351 352
    m % filename % ok % ffmpeg_error(ok);
    throw std::runtime_error(m.str());
  }

  return shared_retval;
}

353
int bob::io::video::find_video_stream(const std::string& filename, boost::shared_ptr<AVFormatContext> format_context) {
André Anjos's avatar
André Anjos committed
354 355 356 357 358

  int retval = av_find_best_stream(format_context.get(), AVMEDIA_TYPE_VIDEO,
      -1, -1, 0, 0);

  if (retval < 0) {
359
    boost::format m("bob::io::video::av_find_stream_info(`%s') failed: cannot find any video streams on this file - ffmpeg reports error %d == `%s'");
André Anjos's avatar
André Anjos committed
360 361 362 363 364 365 366 367
    m % filename % retval % ffmpeg_error(retval);
    throw std::runtime_error(m.str());
  }

  return retval;

}

368
AVCodec* bob::io::video::find_decoder(const std::string& filename,
André Anjos's avatar
André Anjos committed
369 370
    boost::shared_ptr<AVFormatContext> format_context, int stream_index) {

371
  AVCodec* retval = avcodec_find_decoder(format_context->streams[stream_index]->codecpar->codec_id);
André Anjos's avatar
André Anjos committed
372 373

  if (!retval) {
374
    boost::format m("bob::io::video::avcodec_find_decoder(0x%x) failed: cannot find a suitable codec to read stream %d of file `%s'");
375
    m % format_context->streams[stream_index]->codecpar->codec_id
André Anjos's avatar
André Anjos committed
376 377 378 379 380 381 382 383
      % stream_index % filename;
    throw std::runtime_error(m.str());
  }

  return retval;
}

static void deallocate_output_format_context(AVFormatContext* f) {
384
  if (f) avformat_free_context(f);
André Anjos's avatar
André Anjos committed
385 386
}

387
boost::shared_ptr<AVFormatContext> bob::io::video::make_output_format_context(
André Anjos's avatar
André Anjos committed
388 389 390 391 392 393 394 395 396 397
    const std::string& filename, const std::string& formatname) {

  AVFormatContext* retval;
  const char* filename_c = filename.c_str();
  const char* formatname_c = formatname.c_str();

  if (formatname.size() != 0) {
    int ok = avformat_alloc_output_context2(&retval, 0, formatname_c,
        filename_c);
    if (ok < 0) {
398
      boost::format m("bob::io::video::avformat_alloc_output_context2() failed: could not allocate output context based on format name == `%s', filename == `%s' - ffmpeg reports error %d == `%s'");
André Anjos's avatar
André Anjos committed
399 400 401 402 403 404 405
      m % formatname_c % filename_c % ok % ffmpeg_error(ok);
      throw std::runtime_error(m.str());
    }
  }
  else {
    int ok = avformat_alloc_output_context2(&retval, 0, 0, filename_c);
    if (ok < 0) {
406
      boost::format m("bob::io::video::avformat_alloc_output_context2() failed: could not allocate output context based only on filename == `%s' - ffmpeg reports error %d == `%s'");
André Anjos's avatar
André Anjos committed
407 408 409 410 411 412 413 414
      m % formatname_c % filename_c % ok % ffmpeg_error(ok);
      throw std::runtime_error(m.str());
    }
  }

  return boost::shared_ptr<AVFormatContext>(retval, std::ptr_fun(deallocate_output_format_context));
}

415
AVCodec* bob::io::video::find_encoder(const std::string& filename,
André Anjos's avatar
André Anjos committed
416 417 418 419 420 421 422 423 424
    boost::shared_ptr<AVFormatContext> fmtctxt, const std::string& codecname) {

  AVCodec* retval = 0;

  /* find the video encoder */
  if (codecname.size() != 0) {
    retval = avcodec_find_encoder_by_name(codecname.c_str());
    if (!retval) retval = try_find_through_decoder(codecname.c_str());
    if (!retval) {
425
      boost::format m("bob::io::video::avcodec_find_encoder_by_name(`%s') failed: could not find a suitable codec for encoding video file `%s' using the output format `%s' == `%s'");
André Anjos's avatar
André Anjos committed
426 427 428 429 430 431 432 433 434 435 436 437 438 439
      m % codecname % filename % fmtctxt->oformat->name
        % fmtctxt->oformat->long_name;
      throw std::runtime_error(m.str());
    }
  }
  else {
    if (fmtctxt->oformat->video_codec == AV_CODEC_ID_NONE) {
      boost::format m("could not identify codec for encoding video file `%s'; tried codec with name `%s' first and then tried output format's `%s' == `%s' video_codec entry, which was also null");
      m % filename % fmtctxt->oformat->name % fmtctxt->oformat->long_name;
      throw std::runtime_error(m.str());
    }
    retval = avcodec_find_encoder(fmtctxt->oformat->video_codec);

    if (!retval) {
440
      boost::format m("bob::io::video::avcodec_find_encoder(0x%x) failed: could not find encoder for codec with identifier for encoding video file `%s' using the output format `%s' == `%s'");
André Anjos's avatar
André Anjos committed
441 442 443 444 445 446 447 448 449 450
      m % fmtctxt->oformat->video_codec % filename
        % fmtctxt->oformat->name % fmtctxt->oformat->long_name;
      throw std::runtime_error(m.str());
    }
  }

  return retval;
}

static void deallocate_stream(AVStream* s) {
451
  //nop
André Anjos's avatar
André Anjos committed
452 453
}

454
boost::shared_ptr<AVStream> bob::io::video::make_stream(
André Anjos's avatar
André Anjos committed
455 456 457 458 459 460 461
    const std::string& filename,
    boost::shared_ptr<AVFormatContext> fmtctxt,
    AVCodec* codec) {

  AVStream* retval = avformat_new_stream(fmtctxt.get(), codec);

  if (!retval) {
462
    boost::format m("bob::io::video::avformat_new_stream(format=`%s' == `%s', codec=`%s[0x%x]' == `%s') failed: could not allocate video stream container for encoding video to file `%s'");
André Anjos's avatar
André Anjos committed
463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480
    m % fmtctxt->oformat->name % fmtctxt->oformat->long_name
      % codec->id % codec->name % codec->long_name % filename;
    throw std::runtime_error(m.str());
  }

  /* Some adjustments on the newly created stream */
  retval->id = fmtctxt->nb_streams-1; ///< this should be 0, normally

  return boost::shared_ptr<AVStream>(retval, std::ptr_fun(deallocate_stream));
}

static void deallocate_frame(AVFrame* f) {
  if (f) {
    if (f->data[0]) av_free(f->data[0]);
    av_free(f);
  }
}

481 482
boost::shared_ptr<AVFrame>
bob::io::video::make_frame(const std::string& filename,
483
    boost::shared_ptr<AVCodecContext> codec) {
André Anjos's avatar
André Anjos committed
484 485 486 487

  /* allocate and init a re-usable frame */
  AVFrame* retval = av_frame_alloc();
  if (!retval) {
488
    boost::format m("bob::io::video::av_frame_alloc() failed: cannot allocate frame to start encoding video file `%s'");
André Anjos's avatar
André Anjos committed
489 490 491 492
    m % filename;
    throw std::runtime_error(m.str());
  }

493
  retval->format = codec->pix_fmt;
André Anjos's avatar
André Anjos committed
494 495 496
  retval->width  = codec->width;
  retval->height = codec->height;

497
  int ok = av_image_alloc(retval->data, retval->linesize, codec->width, codec->height, codec->pix_fmt, 1);
498 499 500 501 502 503
  if (ok < 0) {
    av_free(retval);
    boost::format m("bob::io::video::av_image_alloc(data, linesize, width=%d, height=%d, 1) failed: cannot allocate frame/picture buffer start reading or writing video file `%s'");
    m % codec->width % codec->height % filename;
    throw std::runtime_error(m.str());
  }
André Anjos's avatar
André Anjos committed
504 505 506 507 508 509 510 511

  return boost::shared_ptr<AVFrame>(retval, std::ptr_fun(deallocate_frame));
}

static void deallocate_empty_frame(AVFrame* f) {
  if (f) av_free(f);
}

512
boost::shared_ptr<AVFrame> bob::io::video::make_empty_frame(const std::string& filename) {
André Anjos's avatar
André Anjos committed
513 514 515 516

  /* allocate and init a re-usable frame */
  AVFrame* retval = av_frame_alloc();
  if (!retval) {
517
    boost::format m("bob::io::video::av_frame_alloc() failed: cannot allocate frame to start encoding video file `%s'");
André Anjos's avatar
André Anjos committed
518 519 520 521 522 523 524 525 526 527 528
    m % filename;
    throw std::runtime_error(m.str());
  }

  return boost::shared_ptr<AVFrame>(retval, std::ptr_fun(deallocate_empty_frame));
}

static void deallocate_swscaler(SwsContext* s) {
  if (s) sws_freeContext(s);
}

529 530 531
boost::shared_ptr<SwsContext> bob::io::video::make_scaler
(const std::string& filename, boost::shared_ptr<AVCodecContext> ctxt,
 AVPixelFormat source_pixel_format, AVPixelFormat dest_pixel_format) {
532 533 534 535 536 537 538 539 540 541 542

  /* check pixel format before scaler gets allocated */
  if (source_pixel_format == AV_PIX_FMT_NONE) {
    boost::format m("bob::io::video::make_scaler() cannot be called with source_pixel_format == `AV_PIX_FMT_NONE'");
    throw std::runtime_error(m.str());
  }

  if (dest_pixel_format == AV_PIX_FMT_NONE) {
    boost::format m("bob::io::video::make_scaler() cannot be called with dest_pixel_format == `AV_PIX_FMT_NONE'");
    throw std::runtime_error(m.str());
  }
André Anjos's avatar
André Anjos committed
543 544 545 546 547 548 549 550 551 552 553 554 555 556

  /**
   * Initializes the software scaler (SWScale) so we can convert images to
   * the movie native format from RGB. You can define which kind of
   * interpolation to perform. Some options from libswscale are:
   * SWS_FAST_BILINEAR, SWS_BILINEAR, SWS_BICUBIC, SWS_X, SWS_POINT, SWS_AREA
   * SWS_BICUBLIN, SWS_GAUSS, SWS_SINC, SWS_LANCZOS, SWS_SPLINE
   */
  SwsContext* retval = sws_getContext(
      ctxt->width, ctxt->height, source_pixel_format,
      ctxt->width, ctxt->height, dest_pixel_format,
      SWS_BICUBIC, 0, 0, 0);

  if (!retval) {
557
    boost::format m("bob::io::video::sws_getContext(src_width=%d, src_height=%d, src_pix_format=`%s', dest_width=%d, dest_height=%d, dest_pix_format=`%s', flags=SWS_BICUBIC, 0, 0, 0) failed: cannot get software scaler context to start encoding or decoding video file `%s'");
558 559
    m % ctxt->width % ctxt->height % av_get_pix_fmt_name(source_pixel_format)
      % ctxt->width % ctxt->height % av_get_pix_fmt_name(dest_pixel_format)
André Anjos's avatar
André Anjos committed
560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580
      % filename;
    throw std::runtime_error(m.str());
  }
  return boost::shared_ptr<SwsContext>(retval, std::ptr_fun(deallocate_swscaler));
}

/**
 * Transforms from Bob's planar 8-bit RGB representation to whatever is
 * required by the FFmpeg encoder output context (peeked from the AVStream
 * object passed).
 */
static void image_to_context(const blitz::Array<uint8_t,3>& data,
    boost::shared_ptr<AVStream> stream,
    boost::shared_ptr<SwsContext> scaler,
    boost::shared_ptr<AVFrame> output_frame) {

  /** The ffmpeg sws scaler requires contiguous planes of data **/
  if (!bob::core::array::isCContiguous(data)) {
    throw std::runtime_error("sws_scale() check failed: cannot encode blitz::Array<uint8_t,3> in video stream - ffmpeg/libav requires contiguous color planes");
  }

581 582
  int width = stream->codecpar->width;
  int height = stream->codecpar->height;
André Anjos's avatar
André Anjos committed
583 584 585 586 587 588 589 590

  const uint8_t* datap = data.data();
  int plane_size = width * height;
  const uint8_t* planes[] = {datap+plane_size, datap+2*plane_size, datap, 0};
  int linesize[] = {width, width, width, 0};

  int ok = sws_scale(scaler.get(), planes, linesize, 0, height, output_frame->data, output_frame->linesize);
  if (ok < 0) {
591 592
    boost::format m("bob::io::video::sws_scale() failed: could not scale frame while encoding - ffmpeg reports error %d = `%s'");
    m % ok % ffmpeg_error(ok);
André Anjos's avatar
André Anjos committed
593 594 595 596 597 598 599 600 601 602 603 604 605 606 607
    throw std::runtime_error(m.str());
  }
}

/**
 * Transforms from Bob's planar 8-bit RGB representation to whatever is
 * required by the FFmpeg encoder output context (peeked from the AVStream
 * object passed).
 */
static void image_to_context(const blitz::Array<uint8_t,3>& data,
    boost::shared_ptr<AVStream> stream,
    boost::shared_ptr<SwsContext> scaler,
    boost::shared_ptr<AVFrame> output_frame,
    boost::shared_ptr<AVFrame> tmp_frame) {

608 609
  int width = stream->codecpar->width;
  int height = stream->codecpar->height;
André Anjos's avatar
André Anjos committed
610 611 612 613 614 615 616 617 618 619

  // replace data in the buffer frame by the pixmap to encode
  tmp_frame->linesize[0] = width*3;
  blitz::Array<uint8_t,3> ordered(tmp_frame->data[0],
		  blitz::shape(height, width, 3), blitz::neverDeleteData);
  ordered = const_cast<blitz::Array<uint8_t,3>&>(data).transpose(1,2,0); //organize for ffmpeg

  int ok = sws_scale(scaler.get(), tmp_frame->data, tmp_frame->linesize,
		  0, height, output_frame->data, output_frame->linesize);
  if (ok < 0) {
620 621
    boost::format m("bob::io::video::sws_scale() failed: could not scale frame while encoding - ffmpeg reports error %d = `%s'");
    m % ok % ffmpeg_error(ok);
André Anjos's avatar
André Anjos committed
622 623 624 625 626 627
    throw std::runtime_error(m.str());
  }
}

static void deallocate_codec_context(AVCodecContext* c) {
  int ok = avcodec_close(c);
628
  avcodec_free_context(&c);
André Anjos's avatar
André Anjos committed
629
  if (ok < 0) {
630 631 632
    boost::format m("bob::io::video::avcodec_close() failed: cannot close codec context (ffmpeg reports error %d = `%s')");
    m % ok % ffmpeg_error(ok);
    throw std::runtime_error(m.str());
André Anjos's avatar
André Anjos committed
633 634 635
  }
}

636
boost::shared_ptr<AVCodecContext> bob::io::video::make_decoder_context(
André Anjos's avatar
André Anjos committed
637 638
    const std::string& filename, AVStream* stream, AVCodec* codec) {

639
  AVCodecContext* retval = avcodec_alloc_context3(codec);
André Anjos's avatar
André Anjos committed
640

641 642 643 644 645 646 647 648
  /* copy the parameters from the demuxer to the codec context */
  int ok = avcodec_parameters_to_context(retval, stream->codecpar);
  if (ok < 0) {
    deallocate_codec_context(retval);
    boost::format m("bob::io::video::avcodec_parameters_to_context(codec=`%s'(0x%x) == `%s') failed: cannot open codec context to start reading video file `%s' - ffmpeg reports error %d == `%s'");
    m % codec->name % codec->id % codec->long_name % filename
      % ok % ffmpeg_error(ok);
    throw std::runtime_error(m.str());
André Anjos's avatar
André Anjos committed
649 650
  }

651 652
  // In the case we opened for writing, this should initialize the context
  ok = avcodec_open2(retval, codec, 0);
André Anjos's avatar
André Anjos committed
653
  if (ok < 0) {
654
    boost::format m("bob::io::video::avcodec_open2(codec=`%s'(0x%x) == `%s') failed: cannot open codec context to start writing video file `%s' - ffmpeg reports error %d == `%s'");
André Anjos's avatar
André Anjos committed
655 656 657 658 659
    m % codec->name % codec->id % codec->long_name % filename
      % ok % ffmpeg_error(ok);
    throw std::runtime_error(m.str());
  }

660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683
  return boost::shared_ptr<AVCodecContext>(retval,
      std::ptr_fun(deallocate_codec_context));
}

boost::shared_ptr<AVCodecContext> bob::io::video::make_encoder_context(
    const std::string& filename, AVFormatContext* fmtctxt, AVStream* stream,
    AVCodec* codec, size_t height, size_t width, double framerate, double
    bitrate, size_t gop) {

  AVCodecContext* retval = avcodec_alloc_context3(codec);

  /* Set user parameters */
  retval->bit_rate = bitrate;

  /* Resolution must be a multiple of two. */
  if (height%2 != 0 || height == 0 || width%2 != 0 || width == 0) {
    boost::format m("ffmpeg only accepts video height and width if they are, both, multiples of two, but you supplied %d x %d while configuring video output for file `%s' - correct these and re-run");
    m % height % width % filename;
    deallocate_codec_context(retval);
    throw std::runtime_error(m.str());
  }

  retval->width    = width;
  retval->height   = height;
André Anjos's avatar
André Anjos committed
684

685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724
  /* timebase: This is the fundamental unit of time (in seconds) in terms
   * of which frame timestamps are represented. For fixed-fps content,
   * timebase should be 1/framerate and timestamp increments should be
   * identical to 1. */
  stream->time_base = av_make_q(1, framerate);
  retval->time_base = stream->time_base;
  retval->framerate = av_make_q(framerate, 1);

  retval->gop_size      = gop; /* emit one intra frame every X at most */
  retval->pix_fmt       = AV_PIX_FMT_YUV420P;

  // checks if the wanted pixel format can be digested by codec
  if (codec->pix_fmts && codec->pix_fmts[0] != AV_PIX_FMT_NONE) {
    //override with preference for native formats supported by codec
    retval->pix_fmt = codec->pix_fmts[0];
  }

  if (retval->codec_id == AV_CODEC_ID_MPEG2VIDEO) {
    /* just for testing, we also add B frames */
    retval->max_b_frames = 2;
  }

  if (retval->codec_id == AV_CODEC_ID_MPEG1VIDEO) {
    /* Needed to avoid using macroblocks in which some coeffs overflow.
     * This does not happen with normal video, it just happens here as
     * the motion of the chroma plane does not match the luma plane. */
    retval->mb_decision = 2;
  }

  if (retval->codec_id == AV_CODEC_ID_MJPEG) {
    /* set jpeg color range */
    retval->color_range = AVCOL_RANGE_JPEG;
  }

  /* Some formats want stream headers to be separate. */
  if (fmtctxt->oformat->flags & AVFMT_GLOBALHEADER) {
    retval->flags |= CODEC_FLAG_GLOBAL_HEADER;
  }

  // In the case we opened for writing, this should initialize the context
André Anjos's avatar
André Anjos committed
725 726
  int ok = avcodec_open2(retval, codec, 0);
  if (ok < 0) {
727
    boost::format m("bob::io::video::avcodec_open2(codec=`%s'(0x%x) == `%s') failed: cannot open codec context to start reading or writing video file `%s' - ffmpeg reports error %d == `%s'");
André Anjos's avatar
André Anjos committed
728 729 730 731 732
    m % codec->name % codec->id % codec->long_name % filename
      % ok % ffmpeg_error(ok);
    throw std::runtime_error(m.str());
  }

733 734 735 736 737 738 739 740 741
  /* copy the parameters from the codec context to the muxer */
  ok = avcodec_parameters_from_context(stream->codecpar, retval);
  if (ok < 0) {
    deallocate_codec_context(retval);
    boost::format m("bob::io::video::avcodec_parameters_from_context(codec=`%s'(0x%x) == `%s') failed: cannot open codec context to start reading or writing video file `%s' - ffmpeg reports error %d == `%s'");
    m % codec->name % codec->id % codec->long_name % filename
      % ok % ffmpeg_error(ok);
    throw std::runtime_error(m.str());
  }
André Anjos's avatar
André Anjos committed
742 743 744 745 746

  return boost::shared_ptr<AVCodecContext>(retval,
      std::ptr_fun(deallocate_codec_context));
}

747
void bob::io::video::open_output_file(const std::string& filename,
André Anjos's avatar
André Anjos committed
748 749 750 751
    boost::shared_ptr<AVFormatContext> format_context) {

  /* open the output file, if needed */
  if (!(format_context->oformat->flags & AVFMT_NOFILE)) {
752
    if (avio_open(&format_context->pb, filename.c_str(), AVIO_FLAG_WRITE) < 0) {
753
      boost::format m("bob::io::video::avio_open(filename=`%s', AVIO_FLAG_WRITE) failed: cannot open output file for writing");
André Anjos's avatar
André Anjos committed
754 755 756 757 758 759 760 761
      m % filename.c_str();
      throw std::runtime_error(m.str());
    }
  }

  /* Write the stream header, if any. */
  int error = avformat_write_header(format_context.get(), 0);
  if (error < 0) {
762
    boost::format m("bob::io::video::avformat_write_header(filename=`%s') failed: cannot write header to output file for some reason - ffmpeg reports error %d == `%s'");
André Anjos's avatar
André Anjos committed
763 764 765 766 767
    m % filename.c_str() % error % ffmpeg_error(error);
    throw std::runtime_error(m.str());
  }
}

768
void bob::io::video::close_output_file(const std::string& filename,
André Anjos's avatar
André Anjos committed
769 770 771 772 773 774 775 776
    boost::shared_ptr<AVFormatContext> format_context) {

  /* Write the trailer, if any. The trailer must be written before you
   * close the CodecContexts open when you wrote the header; otherwise
   * av_write_trailer() may try to use memory that was freed on
   * av_codec_close(). */
  int error = av_write_trailer(format_context.get());
  if (error < 0) {
777
    boost::format m("bob::io::video::av_write_trailer(filename=`%s') failed: cannot write trailer to output file for some reason - ffmpeg reports error %d == `%s')");
André Anjos's avatar
André Anjos committed
778 779 780 781 782
    m % filename % error % ffmpeg_error(error);
    throw std::runtime_error(m.str());
  }

  /* Closes the output file */
783
  avio_closep(&format_context->pb);
André Anjos's avatar
André Anjos committed
784 785 786 787

}

static AVPacket* allocate_packet() {
788 789 790 791
  AVPacket* retval = av_packet_alloc();
  if (!retval) {
    boost::format m("bob::io::video::av_packet_alloc() failed to allocate a new packet");
  }
André Anjos's avatar
André Anjos committed
792 793 794 795 796 797 798
  av_init_packet(retval);
  retval->data = 0;
  retval->size = 0;
  return retval;
}

static void deallocate_packet(AVPacket* p) {
799
  av_packet_free(&p);
André Anjos's avatar
André Anjos committed
800 801 802 803 804 805 806
}

static boost::shared_ptr<AVPacket> make_packet() {
  return boost::shared_ptr<AVPacket>(allocate_packet(),
      std::ptr_fun(deallocate_packet));
}

807
static void write_packet_to_stream(const std::string& filename,
André Anjos's avatar
André Anjos committed
808
    boost::shared_ptr<AVFormatContext> format_context,
809 810 811 812 813 814 815 816 817 818 819 820 821 822 823
    boost::shared_ptr<AVStream> stream,
    boost::shared_ptr<AVCodecContext> codec_context,
    boost::shared_ptr<AVPacket> pkt) {

  //effectively writes the encoded packet to the stream
  /* writes the compressed frame to the media file. */
  pkt->stream_index = stream->index;
  pkt->duration = av_rescale_q(1, codec_context->time_base,
      stream->time_base);
  int ok = av_interleaved_write_frame(format_context.get(), pkt.get());
  if (ok && (ok != AVERROR(EINVAL))) {
    boost::format m("bob::io::video::av_interleaved_write_frame() failed: failed to write video frame while encoding file `%s' - ffmpeg reports error %d == `%s'");
    m % filename % ok % ffmpeg_error(ok);
    throw std::runtime_error(m.str());
  }
André Anjos's avatar
André Anjos committed
824

825
}
André Anjos's avatar
André Anjos committed
826

827 828 829 830
void bob::io::video::flush_encoder (const std::string& filename,
    boost::shared_ptr<AVFormatContext> format_context,
    boost::shared_ptr<AVStream> stream,
    boost::shared_ptr<AVCodecContext> codec_context) {
André Anjos's avatar
André Anjos committed
831

832
  if (format_context->oformat->flags & AVFMT_RAWPICTURE) return;
André Anjos's avatar
André Anjos committed
833

834
  boost::shared_ptr<AVPacket> pkt = make_packet();
André Anjos's avatar
André Anjos committed
835

836 837 838 839 840 841
  int ok = avcodec_send_frame(codec_context.get(), 0); //flush
  if (ok < 0) {
    boost::format m("bob::io::video::avcodec_send_frame() failed: failed to encode video frame while flushing file `%s' - ffmpeg reports error %d == `%s'");
    m % filename % ok % ffmpeg_error(ok);
    throw std::runtime_error(m.str());
  }
André Anjos's avatar
André Anjos committed
842

843 844 845 846
  while (ok >= 0) {
    ok = avcodec_receive_packet(codec_context.get(), pkt.get());
    if (ok < 0 && ok != AVERROR(EAGAIN) && ok != AVERROR_EOF) {
      boost::format m("bob::io::video::avcodec_receive_packet() failed: failed to flush encoder while writing to file `%s' - ffmpeg reports error %d == `%s'");
André Anjos's avatar
André Anjos committed
847 848 849
      m % filename % ok % ffmpeg_error(ok);
      throw std::runtime_error(m.str());
    }
850 851 852
    if (ok >= 0 && pkt->size != 0) { //write last packet to the stream
      write_packet_to_stream(filename, format_context, stream, codec_context,
          pkt);
André Anjos's avatar
André Anjos committed
853
    }
854
    av_packet_unref(pkt.get());
André Anjos's avatar
André Anjos committed
855 856 857 858
  }

}

859
void bob::io::video::write_video_frame (const blitz::Array<uint8_t,3>& data,
André Anjos's avatar
André Anjos committed
860 861 862
    const std::string& filename,
    boost::shared_ptr<AVFormatContext> format_context,
    boost::shared_ptr<AVStream> stream,
863
    boost::shared_ptr<AVCodecContext> codec_context,
André Anjos's avatar
André Anjos committed
864 865
    boost::shared_ptr<AVFrame> context_frame,
    boost::shared_ptr<AVFrame> tmp_frame,
866
    boost::shared_ptr<SwsContext> swscaler) {
André Anjos's avatar
André Anjos committed
867 868 869 870 871 872

  if (tmp_frame)
    image_to_context(data, stream, swscaler, context_frame, tmp_frame);
  else
    image_to_context(data, stream, swscaler, context_frame);

873
  boost::shared_ptr<AVPacket> pkt = make_packet();
André Anjos's avatar
André Anjos committed
874

875
  if (format_context->oformat->flags & AVFMT_RAWPICTURE) {
André Anjos's avatar
André Anjos committed
876
    /* Raw video case - directly store the picture in the packet */
877 878 879 880 881 882
    pkt->flags        |= AV_PKT_FLAG_KEY;
    pkt->data          = context_frame->data[0];
    pkt->size          = sizeof(AVPicture);
    write_packet_to_stream(filename, format_context, stream, codec_context,
        pkt);
  }
André Anjos's avatar
André Anjos committed
883

884
  else {
André Anjos's avatar
André Anjos committed
885

886 887 888
    int ok = avcodec_send_frame(codec_context.get(), context_frame.get());
    if (ok < 0) {
      boost::format m("bob::io::video::avcodec_send_frame() failed: failed to encode video frame while writing to file `%s' - ffmpeg reports error %d == `%s'");
André Anjos's avatar
André Anjos committed
889 890 891 892
      m % filename % ok % ffmpeg_error(ok);
      throw std::runtime_error(m.str());
    }

893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914
    while (ok >= 0) {
      ok = avcodec_receive_packet(codec_context.get(), pkt.get());
      if (ok == AVERROR(EAGAIN)) { //ready for new frame
        break;
      }
      else if (ok == AVERROR_EOF) {
        boost::format m("bob::io::video::avcodec_receive_packet() failed: failed to encode video frame while writing to file `%s' - ffmpeg reports error %d == `%s' (file is already closed)");
        m % filename % ok % ffmpeg_error(ok);
        throw std::runtime_error(m.str());
      }
      else if (ok < 0) { //real error condition
        boost::format m("bob::io::video::avcodec_receive_packet() failed: failed to encode video frame while writing to file `%s' - ffmpeg reports error %d == `%s'");
        m % filename % ok % ffmpeg_error(ok);
        throw std::runtime_error(m.str());
      }
      else if (pkt->size > 0) { //write last packet to the stream
        write_packet_to_stream(filename, format_context, stream, codec_context,
            pkt);
      }
      av_packet_unref(pkt.get());
    }

André Anjos's avatar
André Anjos committed
915 916
  }

917 918 919 920 921
  //sets the output frame PTS [Note: presentation timestamp in time_base
  //units (time when frame should be shown to user) If AV_NOPTS_VALUE then
  //frame_rate = 1/time_base will be assumed].
  context_frame->pts += av_rescale_q(1, codec_context->time_base,
      stream->time_base);
André Anjos's avatar
André Anjos committed
922

923
}
André Anjos's avatar
André Anjos committed
924

925 926 927 928 929 930 931 932 933 934 935 936 937 938
// The flush packet is a non-NULL packet with size 0 and data NULL
static int decode(AVCodecContext *avctx, AVFrame *frame, int *got_frame,
    AVPacket *pkt)
{
  int ret;

  *got_frame = 0;

  if (pkt) {
    ret = avcodec_send_packet(avctx, pkt);
    // In particular, we don't expect AVERROR(EAGAIN), because we read all
    // decoded frames with avcodec_receive_frame() until done.
    if (ret < 0)
      return ret == AVERROR_EOF ? 0 : ret;
939
  }
André Anjos's avatar
André Anjos committed
940

941 942 943 944 945
  ret = avcodec_receive_frame(avctx, frame);
  if (ret < 0 && ret != AVERROR(EAGAIN) && ret != AVERROR_EOF)
    return ret;
  if (ret >= 0)
    *got_frame = 1;
946

947
  return 0;
André Anjos's avatar
André Anjos committed
948
}
949

André Anjos's avatar
André Anjos committed
950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966

static int decode_frame (const std::string& filename, int current_frame,
    boost::shared_ptr<AVCodecContext> codec_context,
    boost::shared_ptr<SwsContext> scaler,
    boost::shared_ptr<AVFrame> context_frame, uint8_t* data,
    boost::shared_ptr<AVPacket> pkt,
    int& got_frame, bool throw_on_error) {

  // In this call, 3 things can happen:
  //
  // 1. if ok < 0, an error has been detected
  // 2. if ok >=0, something was read from the file, correctly. In this
  // condition, **only* if "got_frame" == 1, a frame is ready to be decoded.
  //
  // It is **not** an error that ok is >= 0 and got_frame == 0. This, in fact,
  // happens often with recent versions of ffmpeg.

967 968
  int ok = decode(codec_context.get(), context_frame.get(), &got_frame,
      pkt.get());
André Anjos's avatar
André Anjos committed
969
  if (ok < 0 && throw_on_error) {
970
    boost::format m("bob::io::video::avcodec_decode_video/2() failed: could not decode frame %d of file `%s' - ffmpeg reports error %d == `%s'");
André Anjos's avatar
André Anjos committed
971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988
    m % current_frame % filename % ok % ffmpeg_error(ok);
    throw std::runtime_error(m.str());
  }

  if (got_frame) {

    // In this case, we call the software scaler to decode the frame data.
    // Normally, this means converting from planar YUV420 into packed RGB.

    uint8_t* planes[] = {data, 0};
    int linesize[] = {3*codec_context->width, 0};

    int conv_height = sws_scale(scaler.get(), context_frame->data,
        context_frame->linesize, 0, codec_context->height, planes, linesize);

    if (conv_height < 0) {

      if (throw_on_error) {
989
        boost::format m("bob::io::video::sws_scale() failed: could not scale frame %d of file `%s' - ffmpeg reports error %d");
André Anjos's avatar
André Anjos committed
990 991 992 993 994 995 996 997 998 999 1000 1001
        m % current_frame % filename % conv_height;
        throw std::runtime_error(m.str());
      }

      return -1;
    }

  }

  return ok;
}

1002
bool bob::io::video::read_video_frame (const std::string& filename,
André Anjos's avatar
André Anjos committed
1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020
    int current_frame, int stream_index,
    boost::shared_ptr<AVFormatContext> format_context,
    boost::shared_ptr<AVCodecContext> codec_context,
    boost::shared_ptr<SwsContext> swscaler,
    boost::shared_ptr<AVFrame> context_frame, uint8_t* data,
    bool throw_on_error) {

  boost::shared_ptr<AVPacket> pkt = make_packet();

  int ok = 0;
  int got_frame = 0;

  while ((ok = av_read_frame(format_context.get(), pkt.get())) >= 0) {
    if (pkt->stream_index == stream_index) {
      decode_frame(filename, current_frame, codec_context,
          swscaler, context_frame, data, pkt, got_frame,
          throw_on_error);
    }
1021
    av_packet_unref(pkt.get());
André Anjos's avatar
André Anjos committed
1022 1023 1024 1025 1026
    if (got_frame) return true; //break loop
  }

  if (ok < 0 && ok != (int)AVERROR_EOF) {
    if (throw_on_error) {
1027
      boost::format m("bob::io::video::av_read_frame() failed: on file `%s' - ffmpeg reports error %d == `%s'");
André Anjos's avatar
André Anjos committed
1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047
      m % filename % ok % ffmpeg_error(ok);
      throw std::runtime_error(m.str());
    }
    else return false;
  }

  // it is the end of the file
  pkt->data = NULL;
  pkt->size = 0;
  //N.B.: got_frame == 0
  const unsigned int MAX_FLUSH_ITERATIONS = 128;
  unsigned int iteration_counter = MAX_FLUSH_ITERATIONS;
  do {
    if (pkt->stream_index == stream_index) {
      decode_frame(filename, current_frame, codec_context,
          swscaler, context_frame, data, pkt, got_frame,
          throw_on_error);
      --iteration_counter;
      if (iteration_counter == 0) {
        if (throw_on_error) {
1048
          boost::format m("bob::io::video::decode_frame() failed: on file `%s' - I've been iterating for over %d times and I cannot find a new frame: this codec (%s) must be buggy!");
André Anjos's avatar
André Anjos committed
1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075
          m % filename % MAX_FLUSH_ITERATIONS % codec_context->codec->name;
          throw std::runtime_error(m.str());
        }
        break;
      }
    }
    else break;
  } while (got_frame == 0);

  return true;
}

static int dummy_decode_frame (const std::string& filename, int current_frame,
    boost::shared_ptr<AVCodecContext> codec_context,
    boost::shared_ptr<AVFrame> context_frame,
    boost::shared_ptr<AVPacket> pkt,
    int& got_frame, bool throw_on_error) {

  // In this call, 3 things can happen:
  //
  // 1. if ok < 0, an error has been detected
  // 2. if ok >=0, something was read from the file, correctly. In this
  // condition, **only* if "got_frame" == 1, a frame is ready to be decoded.
  //
  // It is **not** an error that ok is >= 0 and got_frame == 0. This, in fact,
  // happens often with recent versions of ffmpeg.

1076 1077
  int ok = decode(codec_context.get(), context_frame.get(), &got_frame,
      pkt.get());
André Anjos's avatar
André Anjos committed
1078
  if (ok < 0 && throw_on_error) {
1079
    boost::format m("bob::io::video::avcodec_decode_video/2() failed: could not skip frame %d of file `%s' - ffmpeg reports error %d == `%s'");
André Anjos's avatar
André Anjos committed
1080 1081 1082 1083 1084 1085 1086
    m % current_frame % filename % ok % ffmpeg_error(ok);
    throw std::runtime_error(m.str());
  }

  return ok;
}

1087
bool bob::io::video::skip_video_frame (const std::string& filename,
André Anjos's avatar
André Anjos committed
1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103
    int current_frame, int stream_index,
    boost::shared_ptr<AVFormatContext> format_context,
    boost::shared_ptr<AVCodecContext> codec_context,
    boost::shared_ptr<AVFrame> context_frame,
    bool throw_on_error) {

  boost::shared_ptr<AVPacket> pkt = make_packet();

  int ok = 0;
  int got_frame = 0;

  while ((ok = av_read_frame(format_context.get(), pkt.get())) >= 0) {
    if (pkt->stream_index == stream_index) {
      dummy_decode_frame(filename, current_frame, codec_context,
          context_frame, pkt, got_frame, throw_on_error);
    }
1104
    av_packet_unref(pkt.get());
André Anjos's avatar
André Anjos committed
1105 1106 1107 1108 1109
    if (got_frame) return true; //break loop
  }

  if (ok < 0 && ok != (int)AVERROR_EOF) {
    if (throw_on_error) {
1110
      boost::format m("bob::io::video::av_read_frame() failed: on file `%s' - ffmpeg reports error %d == `%s'");
André Anjos's avatar
André Anjos committed
1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129
      m % filename % ok % ffmpeg_error(ok);
      throw std::runtime_error(m.str());
    }
    else return false;
  }

  // it is the end of the file
  pkt->data = NULL;
  pkt->size = 0;
  //N.B.: got_frame == 0
  const unsigned int MAX_FLUSH_ITERATIONS = 128;
  unsigned int iteration_counter = MAX_FLUSH_ITERATIONS;
  do {
    if (pkt->stream_index == stream_index) {
      dummy_decode_frame(filename, current_frame, codec_context,
          context_frame, pkt, got_frame, throw_on_error);
      --iteration_counter;
      if (iteration_counter == 0) {
        if (throw_on_error) {
1130
          boost::format m("bob::io::video::decode_frame() failed: on file `%s' - I've been iterating for over %d times and I cannot find a new frame: this codec (%s) must be buggy!");
André Anjos's avatar
André Anjos committed
1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141
          m % filename % MAX_FLUSH_ITERATIONS % codec_context->codec->name;
          throw std::runtime_error(m.str());
        }
        break;
      }
    }
    else break;
  } while (got_frame == 0);

  return true;
}