<commit_before>/**
 * PANDA 3D SOFTWARE
 * Copyright (c) Carnegie Mellon University.  All rights reserved.
 *
 * All use of this software is subject to the terms of the revised BSD
 * license.  You should have received a copy of this license along
 * with this source code in a file named "LICENSE."
 *
 * @file ffmpegVideoCursor.cxx
 * @author jyelon
 * @date 2007-08-01
 */

#include "ffmpegVideoCursor.h"
#include "config_ffmpeg.h"
#include "pStatCollector.h"
#include "pStatTimer.h"
#include "mutexHolder.h"
#include "reMutexHolder.h"
#include "ffmpegVideo.h"
#include "bamReader.h"
extern "C" {
  #include "libavcodec/avcodec.h"
  #include "libavformat/avformat.h"
#ifdef HAVE_SWSCALE
  #include "libswscale/swscale.h"
#endif
}

ReMutex FfmpegVideoCursor::_av_lock;
TypeHandle FfmpegVideoCursor::_type_handle;
TypeHandle FfmpegVideoCursor::FfmpegBuffer::_type_handle;

PStatCollector FfmpegVideoCursor::_fetch_buffer_pcollector("*:FFMPEG Video Decoding:Fetch");
PStatCollector FfmpegVideoCursor::_seek_pcollector("*:FFMPEG Video Decoding:Seek");
PStatCollector FfmpegVideoCursor::_export_frame_pcollector("*:FFMPEG Convert Video to BGR");


#if LIBAVFORMAT_VERSION_MAJOR < 53
  #define AVMEDIA_TYPE_VIDEO CODEC_TYPE_VIDEO
#endif

/**
 * This constructor is only used when reading from a bam file.
 */
FfmpegVideoCursor::
FfmpegVideoCursor() :
  _max_readahead_frames(0),
  _thread_priority(ffmpeg_thread_priority),
  _lock("FfmpegVideoCursor::_lock"),
  _action_cvar(_lock),
  _thread_status(TS_stopped),
  _seek_frame(0),
  _packet(NULL),
  _format_ctx(NULL),
  _video_ctx(NULL),
  _convert_ctx(NULL),
  _video_index(-1),
  _frame(NULL),
  _frame_out(NULL),
  _eof_known(false)
{
}

/**
 * Specifies the source of the video cursor.  This is normally called only by
 * the constructor or when reading from a bam file.
 */
void FfmpegVideoCursor::
init_from(FfmpegVideo *source) {
  nassertv(_thread == NULL && _thread_status == TS_stopped);
  nassertv(source != NULL);
  _source = source;
  _filename = _source->get_filename();

  if (!open_stream()) {
    cleanup();
    return;
  }

  ReMutexHolder av_holder(_av_lock);

#ifdef HAVE_SWSCALE
  nassertv(_convert_ctx == NULL);
  _convert_ctx = sws_getContext(_size_x, _size_y, _video_ctx->pix_fmt,
#if LIBAVCODEC_VERSION_INT >= AV_VERSION_INT(51, 74, 100)
                                _size_x, _size_y, AV_PIX_FMT_BGR24,
#else
                                _size_x, _size_y, PIX_FMT_BGR24,
#endif
                                SWS_BILINEAR | SWS_PRINT_INFO, NULL, NULL, NULL);
#endif  // HAVE_SWSCALE

#if LIBAVCODEC_VERSION_INT >= AV_VERSION_INT(54, 59, 100)
  _frame = av_frame_alloc();
  _frame_out = av_frame_alloc();
#else
  _frame = avcodec_alloc_frame();
  _frame_out = avcodec_alloc_frame();
#endif

  if ((_frame == 0)||(_frame_out == 0)) {<commit_msg>Compile fix for Ubuntu precise version of ffmpeg<commit_after>/**
 * PANDA 3D SOFTWARE
 * Copyright (c) <NAME>.  All rights reserved.
 *
 * All use of this software is subject to the terms of the revised BSD
 * license.  You should have received a copy of this license along
 * with this source code in a file named "LICENSE."
 *
 * @file ffmpegVideoCursor.cxx
 * @author jyelon
 * @date 2007-08-01
 */

#include "ffmpegVideoCursor.h"
#include "config_ffmpeg.h"
#include "pStatCollector.h"
#include "pStatTimer.h"
#include "mutexHolder.h"
#include "reMutexHolder.h"
#include "ffmpegVideo.h"
#include "bamReader.h"
extern "C" {
  #include "libavcodec/avcodec.h"
  #include "libavformat/avformat.h"
#ifdef HAVE_SWSCALE
  #include "libswscale/swscale.h"
#endif
}

ReMutex FfmpegVideoCursor::_av_lock;
TypeHandle FfmpegVideoCursor::_type_handle;
TypeHandle FfmpegVideoCursor::FfmpegBuffer::_type_handle;

PStatCollector FfmpegVideoCursor::_fetch_buffer_pcollector("*:FFMPEG Video Decoding:Fetch");
PStatCollector FfmpegVideoCursor::_seek_pcollector("*:FFMPEG Video Decoding:Seek");
PStatCollector FfmpegVideoCursor::_export_frame_pcollector("*:FFMPEG Convert Video to BGR");


#if LIBAVFORMAT_VERSION_MAJOR < 53
  #define AVMEDIA_TYPE_VIDEO CODEC_TYPE_VIDEO
#endif

/**
 * This constructor is only used when reading from a bam file.
 */
FfmpegVideoCursor::
FfmpegVideoCursor() :
  _max_readahead_frames(0),
  _thread_priority(ffmpeg_thread_priority),
  _lock("FfmpegVideoCursor::_lock"),
  _action_cvar(_lock),
  _thread_status(TS_stopped),
  _seek_frame(0),
  _packet(NULL),
  _format_ctx(NULL),
  _video_ctx(NULL),
  _convert_ctx(NULL),
  _video_index(-1),
  _frame(NULL),
  _frame_out(NULL),
  _eof_known(false)
{
}

/**
 * Specifies the source of the video cursor.  This is normally called only by
 * the constructor or when reading from a bam file.
 */
void FfmpegVideoCursor::
init_from(FfmpegVideo *source) {
  nassertv(_thread == NULL && _thread_status == TS_stopped);
  nassertv(source != NULL);
  _source = source;
  _filename = _source->get_filename();

  if (!open_stream()) {
    cleanup();
    return;
  }

  ReMutexHolder av_holder(_av_lock);

#ifdef HAVE_SWSCALE
  nassertv(_convert_ctx == NULL);
  _convert_ctx = sws_getContext(_size_x, _size_y, _video_ctx->pix_fmt,
#if LIBAVUTIL_VERSION_INT >= AV_VERSION_INT(51, 74, 100)
                                _size_x, _size_y, AV_PIX_FMT_BGR24,
#else
                                _size_x, _size_y, PIX_FMT_BGR24,
#endif
                                SWS_BILINEAR | SWS_PRINT_INFO, NULL, NULL, NULL);
#endif  // HAVE_SWSCALE

#if LIBAVCODEC_VERSION_INT >= AV_VERSION_INT(54, 59, 100)
  _frame = av_frame_alloc();
  _frame_out = av_frame_alloc();
#else
  _frame = avcodec_alloc_frame();
  _frame_out = avcodec_alloc_frame();
#endif

  if ((_frame == 0)||(_frame_out == 0)) {
{'fraction_non_alphanumeric': 0.111660777385159, 'fraction_numerical': 0.012014134275618375, 'mean_word_length': 4.604950495049505, 'pattern_counts': {'":': 0, '<': 6, '<?xml version=': 0, '>': 12, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4369302', 'n_tokens_mistral': 2101, 'n_tokens_neox': 2043, 'n_words': 449}