Merge branch 'master' into fix_out_of_files_in_encoding

This commit is contained in:
Isaac Connor 2021-05-16 17:05:51 -04:00
commit 4ee234293a
31 changed files with 855 additions and 637 deletions

View File

@ -31,7 +31,7 @@
%global _hardened_build 1
Name: zoneminder
Version: 1.36.0
Version: 1.37.0
Release: 1%{?dist}
Summary: A camera monitoring and analysis tool
Group: System Environment/Daemons

View File

@ -6,12 +6,10 @@ configure_file(zm_config_data.h.in "${CMAKE_BINARY_DIR}/zm_config_data.h" @ONLY)
# Group together all the source files that are used by all the binaries (zmc, zmu, zms etc)
set(ZM_BIN_SRC_FILES
zm_analysis_thread.cpp
zm_box.cpp
zm_buffer.cpp
zm_camera.cpp
zm_comms.cpp
zm_config.cpp
zm_coord.cpp
zm_curl_camera.cpp
zm_crypt.cpp
zm.cpp

View File

@ -1,22 +0,0 @@
//
// ZoneMinder Box Class Implementation, $Date$, $Revision$
// Copyright (C) 2001-2008 Philip Coombes
//
// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public License
// as published by the Free Software Foundation; either version 2
// of the License, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
//
#include "zm_box.h"
// This section deliberately left blank

View File

@ -20,49 +20,59 @@
#ifndef ZM_BOX_H
#define ZM_BOX_H
#include "zm_coord.h"
#include "zm_line.h"
#include "zm_vector2.h"
#include <cmath>
#include <vector>
//
// Class used for storing a box, which is defined as a region
// defined by two coordinates
//
class Box {
private:
Coord lo, hi;
Coord size;
public:
Box() = default;
Box(Vector2 lo, Vector2 hi) : lo_(lo), hi_(hi), size_(hi - lo) {}
public:
inline Box() : lo(0,0), hi(0,0), size(0,0) { }
explicit inline Box(unsigned int p_size) : lo(0, 0), hi(p_size-1, p_size-1), size(Coord::Range(hi, lo)) { }
inline Box( int p_x_size, int p_y_size ) : lo( 0, 0 ), hi ( p_x_size-1, p_y_size-1 ), size( Coord::Range( hi, lo ) ) { }
inline Box( int lo_x, int lo_y, int hi_x, int hi_y ) : lo( lo_x, lo_y ), hi( hi_x, hi_y ), size( Coord::Range( hi, lo ) ) { }
inline Box( const Coord &p_lo, const Coord &p_hi ) : lo( p_lo ), hi( p_hi ), size( Coord::Range( hi, lo ) ) { }
const Vector2 &Lo() const { return lo_; }
const Vector2 &Hi() const { return hi_; }
inline const Coord &Lo() const { return lo; }
inline int LoX() const { return lo.X(); }
inline int LoX(int p_lo_x) { return lo.X(p_lo_x); }
inline int LoY() const { return lo.Y(); }
inline int LoY(int p_lo_y) { return lo.Y(p_lo_y); }
inline const Coord &Hi() const { return hi; }
inline int HiX() const { return hi.X(); }
inline int HiX(int p_hi_x) { return hi.X(p_hi_x); }
inline int HiY() const { return hi.Y(); }
inline int HiY(int p_hi_y) { return hi.Y(p_hi_y); }
inline const Coord &Size() const { return size; }
inline int Width() const { return size.X(); }
inline int Height() const { return size.Y(); }
inline int Area() const { return size.X()*size.Y(); }
const Vector2 &Size() const { return size_; }
int32 Area() const { return size_.x_ * size_.y_; }
inline const Coord Centre() const {
int mid_x = int(std::round(lo.X()+(size.X()/2.0)));
int mid_y = int(std::round(lo.Y()+(size.Y()/2.0)));
return Coord( mid_x, mid_y );
Vector2 Centre() const {
int32 mid_x = static_cast<int32>(std::lround(lo_.x_ + (size_.x_ / 2.0)));
int32 mid_y = static_cast<int32>(std::lround(lo_.y_ + (size_.y_ / 2.0)));
return {mid_x, mid_y};
}
inline bool Inside( const Coord &coord ) const
{
return( coord.X() >= lo.X() && coord.X() <= hi.X() && coord.Y() >= lo.Y() && coord.Y() <= hi.Y() );
// Get vertices of the box in a counter-clockwise order
std::vector<Vector2> Vertices() const {
return {lo_, {hi_.x_, lo_.y_}, hi_, {lo_.x_, hi_.y_}};
}
// Get edges of the box in a counter-clockwise order
std::vector<LineSegment> Edges() const {
std::vector<LineSegment> edges;
edges.reserve(4);
std::vector<Vector2> v = Vertices();
edges.emplace_back(v[0], v[1]);
edges.emplace_back(v[1], v[2]);
edges.emplace_back(v[2], v[3]);
edges.emplace_back(v[3], v[0]);
return edges;
}
bool Contains(const Vector2 &coord) const {
return (coord.x_ >= lo_.x_ && coord.x_ <= hi_.x_ && coord.y_ >= lo_.y_ && coord.y_ <= hi_.y_);
}
private:
Vector2 lo_;
Vector2 hi_;
Vector2 size_;
};
#endif // ZM_BOX_H

View File

@ -1,22 +0,0 @@
//
// ZoneMinder Coordinate Class Implementation, $Date$, $Revision$
// Copyright (C) 2001-2008 Philip Coombes
//
// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public License
// as published by the Free Software Foundation; either version 2
// of the License, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
//
#include "zm_coord.h"
// This section deliberately left blank

View File

@ -1,64 +0,0 @@
//
// ZoneMinder Coordinate Class Interface, $Date$, $Revision$
// Copyright (C) 2001-2008 Philip Coombes
//
// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public License
// as published by the Free Software Foundation; either version 2
// of the License, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
//
#ifndef ZM_COORD_H
#define ZM_COORD_H
#include "zm_define.h"
//
// Class used for storing an x,y pair, i.e. a coordinate
//
class Coord {
private:
int x, y;
public:
inline Coord() : x(0), y(0) { }
inline Coord( int p_x, int p_y ) : x(p_x), y(p_y) { }
inline Coord( const Coord &p_coord ) : x(p_coord.x), y(p_coord.y) { }
inline Coord &operator =( const Coord &coord ) {
x = coord.x;
y = coord.y;
return *this;
}
inline int &X(int p_x) { x=p_x; return x; }
inline const int &X() const { return x; }
inline int &Y(int p_y) { y=p_y; return y; }
inline const int &Y() const { return y; }
inline static Coord Range( const Coord &coord1, const Coord &coord2 ) {
Coord result( (coord1.x-coord2.x)+1, (coord1.y-coord2.y)+1 );
return result;
}
inline bool operator==( const Coord &coord ) const { return( x == coord.x && y == coord.y ); }
inline bool operator!=( const Coord &coord ) const { return( x != coord.x || y != coord.y ); }
inline bool operator>( const Coord &coord ) const { return( x > coord.x && y > coord.y ); }
inline bool operator>=( const Coord &coord ) const { return( !(operator<(coord)) ); }
inline bool operator<( const Coord &coord ) const { return( x < coord.x && y < coord.y ); }
inline bool operator<=( const Coord &coord ) const { return( !(operator>(coord)) ); }
inline Coord &operator+=( const Coord &coord ) { x += coord.x; y += coord.y; return( *this ); }
inline Coord &operator-=( const Coord &coord ) { x -= coord.x; y -= coord.y; return( *this ); }
inline friend Coord operator+( const Coord &coord1, const Coord &coord2 ) { Coord result( coord1 ); result += coord2; return( result ); }
inline friend Coord operator-( const Coord &coord1, const Coord &coord2 ) { Coord result( coord1 ); result -= coord2; return( result ); }
};
#endif // ZM_COORD_H

View File

@ -29,6 +29,7 @@
#endif
#include <cinttypes>
#include <cstddef>
typedef std::int64_t int64;
typedef std::int32_t int32;

View File

@ -473,10 +473,10 @@ void Event::WriteDbFrames() {
stats.alarm_blobs_,
stats.min_blob_size_,
stats.max_blob_size_,
stats.alarm_box_.LoX(),
stats.alarm_box_.LoY(),
stats.alarm_box_.HiX(),
stats.alarm_box_.HiY(),
stats.alarm_box_.Lo().x_,
stats.alarm_box_.Lo().y_,
stats.alarm_box_.Hi().x_,
stats.alarm_box_.Hi().y_,
stats.score_);
} // end foreach zone stats
} // end if recording stats

View File

@ -781,7 +781,7 @@ void Image::Assign(const Image &image) {
return;
}
} else {
if ( new_size > allocation || !buffer ) {
if (new_size > allocation || !buffer) {
// DumpImgBuffer(); This is also done in AllocImgBuffer
AllocImgBuffer(new_size);
}
@ -820,10 +820,10 @@ Image *Image::HighlightEdges(
/* Set image to all black */
high_image->Clear();
unsigned int lo_x = limits ? limits->Lo().X() : 0;
unsigned int lo_y = limits ? limits->Lo().Y() : 0;
unsigned int hi_x = limits ? limits->Hi().X() : width-1;
unsigned int hi_y = limits ? limits->Hi().Y() : height-1;
unsigned int lo_x = limits ? limits->Lo().x_ : 0;
unsigned int lo_y = limits ? limits->Lo().y_ : 0;
unsigned int hi_x = limits ? limits->Hi().x_ : width - 1;
unsigned int hi_y = limits ? limits->Hi().y_ : height - 1;
if ( p_colours == ZM_COLOUR_GRAY8 ) {
for ( unsigned int y = lo_y; y <= hi_y; y++ ) {
@ -1508,7 +1508,7 @@ bool Image::Crop( unsigned int lo_x, unsigned int lo_y, unsigned int hi_x, unsig
}
bool Image::Crop(const Box &limits) {
return Crop(limits.LoX(), limits.LoY(), limits.HiX(), limits.HiY());
return Crop(limits.Lo().x_, limits.Lo().y_, limits.Hi().x_, limits.Hi().y_);
}
/* Far from complete */
@ -1932,7 +1932,7 @@ void Image::Delta(const Image &image, Image* targetimage) const {
#endif
}
const Coord Image::centreCoord( const char *text, int size=1 ) const {
const Vector2 Image::centreCoord(const char *text, int size = 1) const {
int index = 0;
int line_no = 0;
int text_len = strlen(text);
@ -1957,7 +1957,7 @@ const Coord Image::centreCoord( const char *text, int size=1 ) const {
uint16_t char_height = font_variant.GetCharHeight();
int x = (width - (max_line_len * char_width )) / 2;
int y = (height - (line_no * char_height) ) / 2;
return Coord(x, y);
return {x, y};
}
/* RGB32 compatible: complete */
@ -2007,7 +2007,7 @@ https://lemire.me/blog/2018/02/21/iterating-over-set-bits-quickly/
*/
void Image::Annotate(
const std::string &text,
const Coord &coord,
const Vector2 &coord,
const uint8 size,
const Rgb fg_colour,
const Rgb bg_colour) {
@ -2031,8 +2031,8 @@ void Image::Annotate(
// Calculate initial coordinates of annotation so that everything is displayed even if the
// user set coordinates would prevent that.
uint32 x0 = ZM::clamp(static_cast<uint32>(coord.X()), 0u, x0_max);
uint32 y0 = ZM::clamp(static_cast<uint32>(coord.Y()), 0u, y0_max);
uint32 x0 = ZM::clamp(static_cast<uint32>(coord.x_), 0u, x0_max);
uint32 y0 = ZM::clamp(static_cast<uint32>(coord.y_), 0u, y0_max);
uint32 y = y0;
for (const std::string &line : lines) {
@ -2127,7 +2127,7 @@ void Image::Annotate(
}
}
void Image::Timestamp( const char *label, const time_t when, const Coord &coord, const int size ) {
void Image::Timestamp(const char *label, const time_t when, const Vector2 &coord, const int size) {
char time_text[64];
tm when_tm = {};
strftime(time_text, sizeof(time_text), "%y/%m/%d %H:%M:%S", localtime_r(&when, &when_tm));
@ -2294,10 +2294,10 @@ void Image::Fill( Rgb colour, const Box *limits ) {
/* Convert the colour's RGBA subpixel order into the image's subpixel order */
colour = rgb_convert(colour,subpixelorder);
unsigned int lo_x = limits?limits->Lo().X():0;
unsigned int lo_y = limits?limits->Lo().Y():0;
unsigned int hi_x = limits?limits->Hi().X():width-1;
unsigned int hi_y = limits?limits->Hi().Y():height-1;
unsigned int lo_x = limits ? limits->Lo().x_ : 0;
unsigned int lo_y = limits ? limits->Lo().y_ : 0;
unsigned int hi_x = limits ? limits->Hi().x_ : width - 1;
unsigned int hi_y = limits ? limits->Hi().y_ : height - 1;
if ( colours == ZM_COLOUR_GRAY8 ) {
for ( unsigned int y = lo_y; y <= hi_y; y++ ) {
unsigned char *p = &buffer[(y*width)+lo_x];
@ -2339,10 +2339,10 @@ void Image::Fill( Rgb colour, int density, const Box *limits ) {
/* Convert the colour's RGBA subpixel order into the image's subpixel order */
colour = rgb_convert(colour, subpixelorder);
unsigned int lo_x = limits?limits->Lo().X():0;
unsigned int lo_y = limits?limits->Lo().Y():0;
unsigned int hi_x = limits?limits->Hi().X():width-1;
unsigned int hi_y = limits?limits->Hi().Y():height-1;
unsigned int lo_x = limits ? limits->Lo().x_ : 0;
unsigned int lo_y = limits ? limits->Lo().y_ : 0;
unsigned int hi_x = limits ? limits->Hi().x_ : width - 1;
unsigned int hi_y = limits ? limits->Hi().y_ : height - 1;
if ( colours == ZM_COLOUR_GRAY8 ) {
for ( unsigned int y = lo_y; y <= hi_y; y++ ) {
unsigned char *p = &buffer[(y*width)+lo_x];
@ -2382,17 +2382,18 @@ void Image::Outline( Rgb colour, const Polygon &polygon ) {
}
/* Convert the colour's RGBA subpixel order into the image's subpixel order */
colour = rgb_convert(colour,subpixelorder);
colour = rgb_convert(colour, subpixelorder);
int n_coords = polygon.getNumCoords();
for ( int j = 0, i = n_coords-1; j < n_coords; i = j++ ) {
const Coord &p1 = polygon.getCoord( i );
const Coord &p2 = polygon.getCoord( j );
size_t n_coords = polygon.GetVertices().size();
for (size_t j = 0, i = n_coords - 1; j < n_coords; i = j++) {
const Vector2 &p1 = polygon.GetVertices()[i];
const Vector2 &p2 = polygon.GetVertices()[j];
int x1 = p1.X();
int x2 = p2.X();
int y1 = p1.Y();
int y2 = p2.Y();
// The last pixel we can draw is width/height - 1. Clamp to that value.
int x1 = ZM::clamp(p1.x_, 0, static_cast<int>(width - 1));
int x2 = ZM::clamp(p2.x_, 0, static_cast<int>(width - 1));
int y1 = ZM::clamp(p1.y_, 0, static_cast<int>(height - 1));
int y2 = ZM::clamp(p2.y_, 0, static_cast<int>(height - 1));
double dx = x2 - x1;
double dy = y2 - y1;
@ -2466,17 +2467,17 @@ void Image::Fill(Rgb colour, int density, const Polygon &polygon) {
/* Convert the colour's RGBA subpixel order into the image's subpixel order */
colour = rgb_convert(colour, subpixelorder);
int n_coords = polygon.getNumCoords();
size_t n_coords = polygon.GetVertices().size();
int n_global_edges = 0;
Edge global_edges[n_coords];
for ( int j = 0, i = n_coords-1; j < n_coords; i = j++ ) {
const Coord &p1 = polygon.getCoord(i);
const Coord &p2 = polygon.getCoord(j);
for (size_t j = 0, i = n_coords - 1; j < n_coords; i = j++) {
const Vector2 &p1 = polygon.GetVertices()[i];
const Vector2 &p2 = polygon.GetVertices()[j];
int x1 = p1.X();
int x2 = p2.X();
int y1 = p1.Y();
int y2 = p2.Y();
int x1 = p1.x_;
int x2 = p2.x_;
int y1 = p1.y_;
int y2 = p2.y_;
//Debug( 9, "x1:%d,y1:%d x2:%d,y2:%d", x1, y1, x2, y2 );
if ( y1 == y2 )
@ -2511,8 +2512,7 @@ void Image::Fill(Rgb colour, int density, const Polygon &polygon) {
Debug(9, "Moving global edge");
active_edges[n_active_edges++] = global_edges[i];
if ( i < (n_global_edges-1) ) {
//memcpy( &global_edges[i], &global_edges[i+1], sizeof(*global_edges)*(n_global_edges-i) );
memmove( &global_edges[i], &global_edges[i+1], sizeof(*global_edges)*(n_global_edges-i) );
memmove(&global_edges[i], &global_edges[i + 1], sizeof(*global_edges) * (n_global_edges - i - 1));
i--;
}
n_global_edges--;

View File

@ -20,19 +20,18 @@
#ifndef ZM_IMAGE_H
#define ZM_IMAGE_H
#include "zm_coord.h"
#include "zm_ffmpeg.h"
#include "zm_jpeg.h"
#include "zm_logger.h"
#include "zm_mem_utils.h"
#include "zm_rgb.h"
#include "zm_vector2.h"
#if HAVE_ZLIB_H
#include <zlib.h>
#endif // HAVE_ZLIB_H
class Box;
class Image;
class Polygon;
#define ZM_BUFTYPE_DONTFREE 0
@ -96,24 +95,6 @@ class Image {
void update_function_pointers();
protected:
struct Edge {
int min_y;
int max_y;
double min_x;
double _1_m;
static bool CompareYX(const Edge &e1, const Edge &e2) {
if ( e1.min_y == e2.min_y )
return e1.min_x < e2.min_x;
return e1.min_y < e2.min_y;
}
static bool CompareX(const Edge &e1, const Edge &e2) {
return e1.min_x < e2.min_x;
}
};
inline void AllocImgBuffer(size_t p_bufsize) {
if ( buffer )
DumpImgBuffer();
@ -280,16 +261,16 @@ class Image {
//Image *Delta( const Image &image ) const;
void Delta( const Image &image, Image* targetimage) const;
const Coord centreCoord(const char *text, const int size) const;
const Vector2 centreCoord(const char *text, const int size) const;
void MaskPrivacy( const unsigned char *p_bitmask, const Rgb pixel_colour=0x00222222 );
void Annotate(const std::string &text,
const Coord &coord,
const Vector2 &coord,
uint8 size = 1,
Rgb fg_colour = kRGBWhite,
Rgb bg_colour = kRGBBlack);
Image *HighlightEdges( Rgb colour, unsigned int p_colours, unsigned int p_subpixelorder, const Box *limits=0 );
//Image *HighlightEdges( Rgb colour, const Polygon &polygon );
void Timestamp( const char *label, const time_t when, const Coord &coord, const int size );
void Timestamp(const char *label, const time_t when, const Vector2 &coord, const int size);
void Colourise(const unsigned int p_reqcolours, const unsigned int p_reqsubpixelorder);
void DeColourise();

64
src/zm_line.h Normal file
View File

@ -0,0 +1,64 @@
/*
* This file is part of the ZoneMinder Project. See AUTHORS file for Copyright information
*
* This program is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License as published by the
* Free Software Foundation; either version 2 of the License, or (at your
* option) any later version.
*
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
* more details.
*
* You should have received a copy of the GNU General Public License along
* with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef ZONEMINDER_SRC_ZM_LINE_H_
#define ZONEMINDER_SRC_ZM_LINE_H_
#include "zm_vector2.h"
// Represents a part of a line bounded by two end points
class LineSegment {
public:
LineSegment(Vector2 start, Vector2 end) : start_(start), end_(end) {}
public:
Vector2 start_;
Vector2 end_;
};
// Represents an infinite line
class Line {
public:
Line(Vector2 p1, Vector2 p2) : position_(p1), direction_(p2 - p1) {}
explicit Line(LineSegment segment) : Line(segment.start_, segment.end_) {};
bool IsPointLeftOfOrColinear(Vector2 p) const {
int32 det = direction_.Determinant(p - position_);
return det >= 0;
}
Vector2 Intersection(Line const &line) const {
int32 det = direction_.Determinant(line.direction_);
if (det == 0) {
// lines are parallel or overlap, no intersection
return Vector2::Inf();
}
Vector2 c = line.position_ - position_;
double t = c.Determinant(line.direction_) / static_cast<double>(det);
return position_ + direction_ * t;
}
private:
Vector2 position_;
Vector2 direction_;
};
#endif //ZONEMINDER_SRC_ZM_LINE_H_

View File

@ -327,7 +327,7 @@ Monitor::Monitor()
record_audio(0),
//event_prefix
//label_format
label_coord(Coord(0,0)),
label_coord(Vector2(0,0)),
label_size(0),
image_buffer_count(0),
max_image_buffer_count(0),
@ -561,7 +561,7 @@ void Monitor::Load(MYSQL_ROW dbrow, bool load_zones=true, Purpose p = QUERY) {
/* "EventPrefix, LabelFormat, LabelX, LabelY, LabelSize," */
event_prefix = dbrow[col] ? dbrow[col] : ""; col++;
label_format = dbrow[col] ? ReplaceAll(dbrow[col], "\\n", "\n") : ""; col++;
label_coord = Coord(atoi(dbrow[col]), atoi(dbrow[col+1])); col += 2;
label_coord = Vector2(atoi(dbrow[col]), atoi(dbrow[col + 1])); col += 2;
label_size = atoi(dbrow[col]); col++;
/* "ImageBufferCount, `MaxImageBufferCount`, WarmupCount, PreEventCount, PostEventCount, StreamReplayBuffer, AlarmFrameCount, " */
@ -1481,21 +1481,27 @@ void Monitor::DumpZoneImage(const char *zone_string) {
zone_image->Colourise(ZM_COLOUR_RGB24, ZM_SUBPIX_ORDER_RGB);
}
extra_zone.Clip(Box(
{0, 0},
{static_cast<int32>(zone_image->Width()), static_cast<int32>(zone_image->Height())}
));
for (const Zone &zone : zones) {
if ( exclude_id && (!extra_colour || extra_zone.getNumCoords()) && zone.Id() == exclude_id )
if (exclude_id && (!extra_colour || !extra_zone.GetVertices().empty()) && zone.Id() == exclude_id) {
continue;
}
Rgb colour;
if ( exclude_id && !extra_zone.getNumCoords() && zone.Id() == exclude_id ) {
if (exclude_id && extra_zone.GetVertices().empty() && zone.Id() == exclude_id) {
colour = extra_colour;
} else {
if ( zone.IsActive() ) {
if (zone.IsActive()) {
colour = kRGBRed;
} else if ( zone.IsInclusive() ) {
} else if (zone.IsInclusive()) {
colour = kRGBOrange;
} else if ( zone.IsExclusive() ) {
} else if (zone.IsExclusive()) {
colour = kRGBPurple;
} else if ( zone.IsPreclusive() ) {
} else if (zone.IsPreclusive()) {
colour = kRGBBlue;
} else {
colour = kRGBWhite;
@ -1505,7 +1511,7 @@ void Monitor::DumpZoneImage(const char *zone_string) {
zone_image->Outline(colour, zone.GetPolygon());
}
if ( extra_zone.getNumCoords() ) {
if (!extra_zone.GetVertices().empty()) {
zone_image->Fill(extra_colour, 2, extra_zone);
zone_image->Outline(extra_colour, extra_zone);
}
@ -1544,8 +1550,9 @@ bool Monitor::CheckSignal(const Image *image) {
if ( !config.timestamp_on_capture || !label_format[0] )
break;
// Avoid sampling the rows with timestamp in
if ( index < (label_coord.Y()*width) || index >= (label_coord.Y()+Image::LINE_HEIGHT)*width )
if (index < (label_coord.y_ * width) || index >= (label_coord.y_ + Image::LINE_HEIGHT) * width) {
break;
}
}
if ( colours == ZM_COLOUR_GRAY8 ) {
@ -2832,7 +2839,7 @@ unsigned int Monitor::DetectMotion(const Image &comp_image, Event::StringSet &zo
} // end if CheckAlarms
} // end foreach zone
Coord alarm_centre;
Vector2 alarm_centre;
int top_score = -1;
if (alarm) {
@ -2898,8 +2905,8 @@ unsigned int Monitor::DetectMotion(const Image &comp_image, Event::StringSet &zo
} // end if alarm
if (top_score > 0) {
shared_data->alarm_x = alarm_centre.X();
shared_data->alarm_y = alarm_centre.Y();
shared_data->alarm_x = alarm_centre.x_;
shared_data->alarm_y = alarm_centre.y_;
Info("Got alarm centre at %d,%d, at count %d",
shared_data->alarm_x, shared_data->alarm_y, analysis_image_count);
@ -2954,7 +2961,7 @@ bool Monitor::DumpSettings(char *output, bool verbose) {
sprintf(output+strlen(output), "Subpixel Order : %u\n", camera->SubpixelOrder() );
sprintf(output+strlen(output), "Event Prefix : %s\n", event_prefix.c_str() );
sprintf(output+strlen(output), "Label Format : %s\n", label_format.c_str() );
sprintf(output+strlen(output), "Label Coord : %d,%d\n", label_coord.X(), label_coord.Y() );
sprintf(output+strlen(output), "Label Coord : %d,%d\n", label_coord.x_, label_coord.y_ );
sprintf(output+strlen(output), "Label Size : %d\n", label_size );
sprintf(output+strlen(output), "Image Buffer Count : %d\n", image_buffer_count );
sprintf(output+strlen(output), "Warmup Count : %d\n", warmup_count );

View File

@ -305,7 +305,7 @@ protected:
std::string event_prefix; // The prefix applied to event names as they are created
std::string label_format; // The format of the timestamp on the images
Coord label_coord; // The coordinates of the timestamp on the images
Vector2 label_coord; // The coordinates of the timestamp on the images
int label_size; // Size of the timestamp on the images
int32_t image_buffer_count; // Size of circular image buffer, kept in /dev/shm
int32_t max_image_buffer_count; // Max # of video packets to keep in packet queue

View File

@ -19,91 +19,103 @@
#include "zm_poly.h"
#include "zm_line.h"
#include <cmath>
void Polygon::calcArea() {
double float_area = 0.0L;
for ( int i = 0, j = n_coords-1; i < n_coords; j = i++ ) {
double trap_area = ((coords[i].X()-coords[j].X())*((coords[i].Y()+coords[j].Y())))/2.0L;
Polygon::Polygon(std::vector<Vector2> vertices) : vertices_(std::move(vertices)), area(0) {
UpdateExtent();
UpdateArea();
UpdateCentre();
}
void Polygon::UpdateExtent() {
if (vertices_.empty())
return;
int min_x = vertices_[0].x_;
int max_x = 0;
int min_y = vertices_[0].y_;
int max_y = 0;
for (const Vector2 &vertex : vertices_) {
min_x = std::min(min_x, vertex.x_);
max_x = std::max(max_x, vertex.x_);
min_y = std::min(min_y, vertex.y_);
max_y = std::max(max_y, vertex.y_);
}
extent = Box({min_x, min_y}, {max_x, max_y});
}
void Polygon::UpdateArea() {
double float_area = 0.0;
for (size_t i = 0, j = vertices_.size() - 1; i < vertices_.size(); j = i++) {
double trap_area = ((vertices_[i].x_ - vertices_[j].x_) * ((vertices_[i].y_ + vertices_[j].y_))) / 2.0;
float_area += trap_area;
//printf( "%.2f (%.2f)\n", float_area, trap_area );
}
area = (int)round(fabs(float_area));
area = static_cast<int32>(std::lround(std::fabs(float_area)));
}
void Polygon::calcCentre() {
if ( !area && n_coords )
calcArea();
double float_x = 0.0L, float_y = 0.0L;
for ( int i = 0, j = n_coords-1; i < n_coords; j = i++ ) {
float_x += ((coords[i].Y()-coords[j].Y())*((coords[i].X()*2)+(coords[i].X()*coords[j].X())+(coords[j].X()*2)));
float_y += ((coords[j].X()-coords[i].X())*((coords[i].Y()*2)+(coords[i].Y()*coords[j].Y())+(coords[j].Y()*2)));
void Polygon::UpdateCentre() {
if (!area && !vertices_.empty())
UpdateArea();
double float_x = 0.0;
double float_y = 0.0;
for (size_t i = 0, j = vertices_.size() - 1; i < vertices_.size(); j = i++) {
float_x += ((vertices_[i].y_ - vertices_[j].y_)
* ((vertices_[i].x_ * 2) + (vertices_[i].x_ * vertices_[j].x_) + (vertices_[j].x_ * 2)));
float_y += ((vertices_[j].x_ - vertices_[i].x_)
* ((vertices_[i].y_ * 2) + (vertices_[i].y_ * vertices_[j].y_) + (vertices_[j].y_ * 2)));
}
float_x /= (6*area);
float_y /= (6*area);
centre = Coord( (int)round(float_x), (int)round(float_y) );
float_x /= (6 * area);
float_y /= (6 * area);
centre = Vector2(static_cast<int32>(std::lround(float_x)), static_cast<int32>(std::lround(float_y)));
}
Polygon::Polygon(int p_n_coords, const Coord *p_coords) : n_coords(p_n_coords) {
coords = new Coord[n_coords];
int min_x = -1;
int max_x = -1;
int min_y = -1;
int max_y = -1;
for ( int i = 0; i < n_coords; i++ ) {
coords[i] = p_coords[i];
if ( min_x == -1 || coords[i].X() < min_x )
min_x = coords[i].X();
if ( max_x == -1 || coords[i].X() > max_x )
max_x = coords[i].X();
if ( min_y == -1 || coords[i].Y() < min_y )
min_y = coords[i].Y();
if ( max_y == -1 || coords[i].Y() > max_y )
max_y = coords[i].Y();
}
extent = Box( min_x, min_y, max_x, max_y );
calcArea();
calcCentre();
}
Polygon::Polygon(const Polygon &p_polygon) :
n_coords(p_polygon.n_coords),
extent(p_polygon.extent),
area(p_polygon.area),
centre(p_polygon.centre)
{
coords = new Coord[n_coords];
for( int i = 0; i < n_coords; i++ ) {
coords[i] = p_polygon.coords[i];
}
}
Polygon &Polygon::operator=(const Polygon &p_polygon) {
n_coords = p_polygon.n_coords;
Coord *new_coords = new Coord[n_coords];
for (int i = 0; i < n_coords; i++) {
new_coords[i] = p_polygon.coords[i];
}
delete[] coords;
coords = new_coords;
extent = p_polygon.extent;
area = p_polygon.area;
centre = p_polygon.centre;
return *this;
}
bool Polygon::isInside( const Coord &coord ) const {
bool Polygon::Contains(const Vector2 &coord) const {
bool inside = false;
for ( int i = 0, j = n_coords-1; i < n_coords; j = i++ ) {
if ( (((coords[i].Y() <= coord.Y()) && (coord.Y() < coords[j].Y()) )
|| ((coords[j].Y() <= coord.Y()) && (coord.Y() < coords[i].Y())))
&& (coord.X() < (coords[j].X() - coords[i].X()) * (coord.Y() - coords[i].Y()) / (coords[j].Y() - coords[i].Y()) + coords[i].X()))
{
for (size_t i = 0, j = vertices_.size() - 1; i < vertices_.size(); j = i++) {
if ((((vertices_[i].y_ <= coord.y_) && (coord.y_ < vertices_[j].y_)) || ((vertices_[j].y_ <= coord.y_) && (coord.y_ < vertices_[i].y_)))
&& (coord.x_ < (vertices_[j].x_ - vertices_[i].x_) * (coord.y_ - vertices_[i].y_) / (vertices_[j].y_ - vertices_[i].y_) + vertices_[i].x_)) {
inside = !inside;
}
}
return inside;
}
// Clip the polygon to a rectangular boundary box using the Sutherland-Hodgman algorithm
void Polygon::Clip(const Box &boundary) {
std::vector<Vector2> clipped_vertices = vertices_;
for (LineSegment const &clip_edge : boundary.Edges()) {
// convert our line segment to an infinite line
Line clip_line = Line(clip_edge);
std::vector<Vector2> to_clip = clipped_vertices;
clipped_vertices.clear();
for (size_t i = 0; i < to_clip.size(); ++i) {
Vector2 vert1 = to_clip[i];
Vector2 vert2 = to_clip[(i + 1) % to_clip.size()];
bool vert1_left = clip_line.IsPointLeftOfOrColinear(vert1);
bool vert2_left = clip_line.IsPointLeftOfOrColinear(vert2);
if (vert2_left) {
if (!vert1_left) {
clipped_vertices.push_back(Line(vert1, vert2).Intersection(clip_line));
}
clipped_vertices.push_back(vert2);
} else if (vert1_left) {
clipped_vertices.push_back(Line(vert1, vert2).Intersection(clip_line));
}
}
}
vertices_ = clipped_vertices;
UpdateExtent();
UpdateArea();
UpdateCentre();
}

View File

@ -21,96 +21,53 @@
#define ZM_POLY_H
#include "zm_box.h"
#include <vector>
class Coord;
struct Edge {
int min_y;
int max_y;
double min_x;
double _1_m;
//
// Class used for storing a box, which is defined as a region
// defined by two coordinates
//
static bool CompareYX(const Edge &e1, const Edge &e2) {
if (e1.min_y == e2.min_y)
return e1.min_x < e2.min_x;
return e1.min_y < e2.min_y;
}
static bool CompareX(const Edge &e1, const Edge &e2) {
return e1.min_x < e2.min_x;
}
};
// This class represents convex or concave non-self-intersecting polygons.
class Polygon {
protected:
struct Edge {
int min_y;
int max_y;
double min_x;
double _1_m;
public:
Polygon() : area(0) {}
explicit Polygon(std::vector<Vector2> vertices);
static int CompareYX( const void *p1, const void *p2 ) {
const Edge *e1 = reinterpret_cast<const Edge *>(p1), *e2 = reinterpret_cast<const Edge *>(p2);
if ( e1->min_y == e2->min_y )
return int(e1->min_x - e2->min_x);
else
return int(e1->min_y - e2->min_y);
}
static int CompareX( const void *p1, const void *p2 ) {
const Edge *e1 = reinterpret_cast<const Edge *>(p1), *e2 = reinterpret_cast<const Edge *>(p2);
return int(e1->min_x - e2->min_x);
}
};
const std::vector<Vector2> &GetVertices() const {
return vertices_;
}
struct Slice {
int min_x;
int max_x;
int n_edges;
int *edges;
const Box &Extent() const { return extent; }
int32 Area() const { return area; }
const Vector2 &Centre() const { return centre; }
Slice() {
min_x = 0;
max_x = 0;
n_edges = 0;
edges = nullptr;
}
~Slice() {
delete edges;
}
};
bool Contains(const Vector2 &coord) const;
protected:
int n_coords;
Coord *coords;
void Clip(const Box &boundary);
private:
void UpdateExtent();
void UpdateArea();
void UpdateCentre();
private:
std::vector<Vector2> vertices_;
Box extent;
int area;
Coord centre;
protected:
void initialiseEdges();
void calcArea();
void calcCentre();
public:
inline Polygon() : n_coords(0), coords(nullptr), area(0) {
}
Polygon(int p_n_coords, const Coord *p_coords);
Polygon(const Polygon &p_polygon);
~Polygon() {
delete[] coords;
}
Polygon &operator=( const Polygon &p_polygon );
inline int getNumCoords() const { return n_coords; }
inline const Coord &getCoord( int index ) const {
return coords[index];
}
inline const Box &Extent() const { return extent; }
inline int LoX() const { return extent.LoX(); }
inline int LoX(int p_lo_x) { return extent.LoX(p_lo_x); }
inline int HiX() const { return extent.HiX(); }
inline int HiX(int p_hi_x) { return extent.HiX(p_hi_x); }
inline int LoY() const { return extent.LoY(); }
inline int LoY(int p_lo_y) { return extent.LoY(p_lo_y); }
inline int HiY() const { return extent.HiY(); }
inline int HiY(int p_hi_y) { return extent.HiY(p_hi_y); }
inline int Width() const { return extent.Width(); }
inline int Height() const { return extent.Height(); }
inline int Area() const { return area; }
inline const Coord &Centre() const {
return centre;
}
bool isInside( const Coord &coord ) const;
int32 area;
Vector2 centre;
};
#endif // ZM_POLY_H

View File

@ -204,10 +204,10 @@ Image *StreamBase::prepareImage(Image *image) {
last_crop = Box();
// Recalculate crop parameters, as %ges
int click_x = (last_crop.LoX() * 100 ) / last_act_image_width; // Initial crop offset from last image
click_x += ( x * 100 ) / last_virt_image_width;
int click_y = (last_crop.LoY() * 100 ) / last_act_image_height; // Initial crop offset from last image
click_y += ( y * 100 ) / last_virt_image_height;
int click_x = (last_crop.Lo().x_ * 100) / last_act_image_width; // Initial crop offset from last image
click_x += (x * 100) / last_virt_image_width;
int click_y = (last_crop.Lo().y_ * 100) / last_act_image_height; // Initial crop offset from last image
click_y += (y * 100) / last_virt_image_height;
Debug(3, "Got adjusted click at %d%%,%d%%", click_x, click_y);
// Convert the click locations to the current image pixels
@ -231,10 +231,10 @@ Image *StreamBase::prepareImage(Image *image) {
hi_y = act_image_height - 1;
lo_y = hi_y - (send_image_height - 1);
}
last_crop = Box( lo_x, lo_y, hi_x, hi_y );
last_crop = Box({lo_x, lo_y}, {hi_x, hi_y});
} // end if ( mag != last_mag || x != last_x || y != last_y )
Debug(3, "Cropping to %d,%d -> %d,%d", last_crop.LoX(), last_crop.LoY(), last_crop.HiX(), last_crop.HiY());
Debug(3, "Cropping to %d,%d -> %d,%d", last_crop.Lo().x_, last_crop.Lo().y_, last_crop.Hi().x_, last_crop.Hi().y_);
if ( !image_copied ) {
static Image copy_image;
copy_image.Assign(*image);

80
src/zm_vector2.h Normal file
View File

@ -0,0 +1,80 @@
//
// ZoneMinder Coordinate Class Interface, $Date$, $Revision$
// Copyright (C) 2001-2008 Philip Coombes
//
// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public License
// as published by the Free Software Foundation; either version 2
// of the License, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
//
#ifndef ZM_VECTOR2_H
#define ZM_VECTOR2_H
#include "zm_define.h"
#include <cmath>
#include <limits>
//
// Class used for storing an x,y pair, i.e. a coordinate/vector
//
class Vector2 {
public:
Vector2() : x_(0), y_(0) {}
Vector2(int32 x, int32 y) : x_(x), y_(y) {}
static Vector2 Inf() {
static const Vector2 inf = {std::numeric_limits<int32>::max(), std::numeric_limits<int32>::max()};
return inf;
}
bool operator==(const Vector2 &rhs) const { return (x_ == rhs.x_ && y_ == rhs.y_); }
bool operator!=(const Vector2 &rhs) const { return (x_ != rhs.x_ || y_ != rhs.y_); }
// These operators are not idiomatic. If lexicographic comparison is needed, it should be implemented separately.
bool operator>(const Vector2 &rhs) const = delete;
bool operator>=(const Vector2 &rhs) const = delete;
bool operator<(const Vector2 &rhs) const = delete;
bool operator<=(const Vector2 &rhs) const = delete;
Vector2 operator+(const Vector2 &rhs) const {
return {x_ + rhs.x_, y_ + rhs.y_};
}
Vector2 operator-(const Vector2 &rhs) const {
return {x_ - rhs.x_, y_ - rhs.y_};
}
Vector2 operator*(double rhs) const {
return {static_cast<int32>(std::lround(x_ * rhs)), static_cast<int32>(std::lround(y_ * rhs))};
}
Vector2 &operator+=(const Vector2 &rhs) {
x_ += rhs.x_;
y_ += rhs.y_;
return *this;
}
Vector2 &operator-=(const Vector2 &rhs) {
x_ -= rhs.x_;
y_ -= rhs.y_;
return *this;
}
// Calculated the determinant of the 2x2 matrix as given by [[x_, y_], [v.x_y, v.y_]]
int32 Determinant(Vector2 const &v) const {
return (x_ * v.y_) - (y_ * v.x_);
}
public:
int32 x_;
int32 y_;
};
#endif // ZM_VECTOR2_H

View File

@ -115,7 +115,7 @@ bool VideoStore::open() {
Debug(1, "Opening video storage stream %s format: %s", filename, format);
int ret = avformat_alloc_output_context2(&oc, nullptr, nullptr, filename);
if ( ret < 0 ) {
if (ret < 0) {
Warning(
"Could not create video storage stream %s as no out ctx"
" could be assigned based on filename: %s",
@ -123,9 +123,9 @@ bool VideoStore::open() {
}
// Couldn't deduce format from filename, trying from format name
if ( !oc ) {
if (!oc) {
avformat_alloc_output_context2(&oc, nullptr, format, filename);
if ( !oc ) {
if (!oc) {
Error(
"Could not create video storage stream %s as no out ctx"
" could not be assigned based on filename or format %s",
@ -142,11 +142,11 @@ bool VideoStore::open() {
out_format = oc->oformat;
out_format->flags |= AVFMT_TS_NONSTRICT; // allow non increasing dts
if ( video_in_stream ) {
if (video_in_stream) {
#if LIBAVCODEC_VERSION_CHECK(57, 64, 0, 64, 0)
zm_dump_codecpar(video_in_stream->codecpar);
#endif
if ( monitor->GetOptVideoWriter() == Monitor::PASSTHROUGH ) {
if (monitor->GetOptVideoWriter() == Monitor::PASSTHROUGH) {
// Don't care what codec, just copy parameters
video_out_ctx = avcodec_alloc_context3(nullptr);
// There might not be a useful video_in_stream. v4l in might not populate this very
@ -155,12 +155,12 @@ bool VideoStore::open() {
#else
ret = avcodec_copy_context(video_out_ctx, video_in_ctx);
#endif
if ( ret < 0 ) {
if (ret < 0) {
Error("Could not initialize ctx parameters");
return false;
}
video_out_ctx->pix_fmt = fix_deprecated_pix_fmt(video_out_ctx->pix_fmt);
if ( oc->oformat->flags & AVFMT_GLOBALHEADER ) {
if (oc->oformat->flags & AVFMT_GLOBALHEADER) {
#if LIBAVCODEC_VERSION_CHECK(56, 35, 0, 64, 0)
video_out_ctx->flags |= AV_CODEC_FLAG_GLOBAL_HEADER;
#else
@ -168,19 +168,19 @@ bool VideoStore::open() {
#endif
}
video_out_ctx->time_base = video_in_ctx->time_base;
if ( ! (video_out_ctx->time_base.num && video_out_ctx->time_base.den) ) {
if (!(video_out_ctx->time_base.num && video_out_ctx->time_base.den)) {
Debug(2,"No timebase found in video in context, defaulting to Q");
video_out_ctx->time_base = AV_TIME_BASE_Q;
}
} else if ( monitor->GetOptVideoWriter() == Monitor::ENCODE ) {
} else if (monitor->GetOptVideoWriter() == Monitor::ENCODE) {
int wanted_codec = monitor->OutputCodec();
if ( !wanted_codec ) {
if (!wanted_codec) {
// default to h264
//Debug(2, "Defaulting to H264");
//wanted_codec = AV_CODEC_ID_H264;
// FIXME what is the optimal codec? Probably low latency h264 which is effectively mjpeg
} else {
if ( AV_CODEC_ID_H264 != 27 and wanted_codec > 3 ) {
if (AV_CODEC_ID_H264 != 27 and wanted_codec > 3) {
// Older ffmpeg had AV_CODEC_ID_MPEG2VIDEO_XVMC at position 3 has been deprecated
wanted_codec += 1;
}
@ -233,14 +233,14 @@ bool VideoStore::open() {
video_out_ctx->height = monitor->Height();
video_out_ctx->codec_type = AVMEDIA_TYPE_VIDEO;
if ( video_out_ctx->codec_id == AV_CODEC_ID_H264 ) {
if (video_out_ctx->codec_id == AV_CODEC_ID_H264) {
video_out_ctx->bit_rate = 2000000;
video_out_ctx->gop_size = 12;
video_out_ctx->max_b_frames = 1;
} else if ( video_out_ctx->codec_id == AV_CODEC_ID_MPEG2VIDEO ) {
} else if (video_out_ctx->codec_id == AV_CODEC_ID_MPEG2VIDEO) {
/* just for testing, we also add B frames */
video_out_ctx->max_b_frames = 2;
} else if ( video_out_ctx->codec_id == AV_CODEC_ID_MPEG1VIDEO ) {
} else if (video_out_ctx->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. */
@ -281,7 +281,7 @@ bool VideoStore::open() {
}
}
av_buffer_unref(&hw_frames_ref);
}
} // end if hwdevice_type != NONE
#endif
AVDictionary *opts = 0;
@ -289,7 +289,7 @@ bool VideoStore::open() {
Debug(2, "Options? %s", Options.c_str());
ret = av_dict_parse_string(&opts, Options.c_str(), "=", ",#\n", 0);
if (ret < 0) {
Warning("Could not parse ffmpeg encoder options list '%s'\n", Options.c_str());
Warning("Could not parse ffmpeg encoder options list '%s'", Options.c_str());
} else {
AVDictionaryEntry *e = nullptr;
while ((e = av_dict_get(opts, "", e, AV_DICT_IGNORE_SUFFIX)) != nullptr) {
@ -318,16 +318,15 @@ bool VideoStore::open() {
Warning("Encoder Option %s not recognized by ffmpeg codec", e->key);
}
if (video_out_codec) break;
#if LIBAVCODEC_VERSION_CHECK(57, 64, 0, 64, 0)
// We allocate and copy in newer ffmpeg, so need to free it
avcodec_free_context(&video_out_ctx);
if (hw_device_ctx) av_buffer_unref(&hw_device_ctx);
#endif
} // end foreach codec
if (!video_out_codec) {
Error("Can't open video codec!");
#if LIBAVCODEC_VERSION_CHECK(57, 64, 0, 64, 0)
// We allocate and copy in newer ffmpeg, so need to free it
avcodec_free_context(&video_out_ctx);
#endif
return false;
} // end if can't open codec
Debug(2, "Success opening codec");
@ -336,7 +335,7 @@ bool VideoStore::open() {
} // end if video_in_stream
video_out_stream = avformat_new_stream(oc, video_out_codec);
if ( !video_out_stream ) {
if (!video_out_stream) {
Error("Unable to create video out stream");
return false;
}
@ -350,40 +349,14 @@ bool VideoStore::open() {
#else
avcodec_copy_context(video_out_stream->codec, video_out_ctx);
#endif
// Only set orientation if doing passthrough, otherwise the frame image will be rotated
Monitor::Orientation orientation = monitor->getOrientation();
if ( orientation ) {
Debug(3, "Have orientation %d", orientation);
if ( orientation == Monitor::ROTATE_0 ) {
} else if ( orientation == Monitor::ROTATE_90 ) {
ret = av_dict_set(&video_out_stream->metadata, "rotate", "90", 0);
if ( ret < 0 ) Warning("%s:%d: title set failed", __FILE__, __LINE__);
} else if ( orientation == Monitor::ROTATE_180 ) {
ret = av_dict_set(&video_out_stream->metadata, "rotate", "180", 0);
if ( ret < 0 ) Warning("%s:%d: title set failed", __FILE__, __LINE__);
} else if ( orientation == Monitor::ROTATE_270 ) {
ret = av_dict_set(&video_out_stream->metadata, "rotate", "270", 0);
if ( ret < 0 ) Warning("%s:%d: title set failed", __FILE__, __LINE__);
} else {
Warning("Unsupported Orientation(%d)", orientation);
}
} // end if orientation
video_out_stream->time_base = video_in_stream ? video_in_stream->time_base : AV_TIME_BASE_Q;
if ( audio_in_stream and audio_in_ctx ) {
if (audio_in_stream and audio_in_ctx) {
Debug(2, "Have audio_in_stream %p", audio_in_stream);
if (
#if LIBAVCODEC_VERSION_CHECK(57, 64, 0, 64, 0)
audio_in_stream->codecpar->codec_id
#else
audio_in_stream->codec->codec_id
#endif
!= AV_CODEC_ID_AAC
) {
if (CODEC(audio_in_stream)->codec_id != AV_CODEC_ID_AAC) {
audio_out_codec = avcodec_find_encoder(AV_CODEC_ID_AAC);
if ( !audio_out_codec ) {
if (!audio_out_codec) {
Error("Could not find codec for AAC");
} else {
#if LIBAVCODEC_VERSION_CHECK(57, 64, 0, 64, 0)
@ -397,7 +370,7 @@ bool VideoStore::open() {
#if LIBAVCODEC_VERSION_CHECK(57, 64, 0, 64, 0)
audio_out_ctx = avcodec_alloc_context3(audio_out_codec);
if ( !audio_out_ctx ) {
if (!audio_out_ctx) {
Error("could not allocate codec ctx for AAC");
return false;
}
@ -407,7 +380,7 @@ bool VideoStore::open() {
audio_out_stream = avformat_new_stream(oc, audio_out_codec);
audio_out_stream->time_base = audio_in_stream->time_base;
if ( !setup_resampler() ) {
if (!setup_resampler()) {
return false;
}
} // end if found AAC codec
@ -417,7 +390,7 @@ bool VideoStore::open() {
// normally we want to pass params from codec in here
// but since we are doing audio passthrough we don't care
audio_out_stream = avformat_new_stream(oc, audio_out_codec);
if ( !audio_out_stream ) {
if (!audio_out_stream) {
Error("Could not allocate new stream");
return false;
}
@ -426,7 +399,7 @@ bool VideoStore::open() {
#if LIBAVCODEC_VERSION_CHECK(57, 64, 0, 64, 0)
// Just use the ctx to copy the parameters over
audio_out_ctx = avcodec_alloc_context3(audio_out_codec);
if ( !audio_out_ctx ) {
if (!audio_out_ctx) {
Error("Could not allocate new output_context");
return false;
}
@ -437,20 +410,20 @@ bool VideoStore::open() {
// Copy params from instream to ctx
ret = avcodec_parameters_to_context(
audio_out_ctx, audio_in_stream->codecpar);
if ( ret < 0 ) {
if (ret < 0) {
Error("Unable to copy audio params to ctx %s",
av_make_error_string(ret).c_str());
}
ret = avcodec_parameters_from_context(
audio_out_stream->codecpar, audio_out_ctx);
if ( ret < 0 ) {
if (ret < 0) {
Error("Unable to copy audio params to stream %s",
av_make_error_string(ret).c_str());
}
#else
audio_out_ctx = audio_out_stream->codec;
ret = avcodec_copy_context(audio_out_ctx, audio_in_stream->codec);
if ( ret < 0 ) {
if (ret < 0) {
Error("Unable to copy audio ctx %s",
av_make_error_string(ret).c_str());
audio_out_stream = nullptr;
@ -459,7 +432,7 @@ bool VideoStore::open() {
audio_out_ctx->codec_tag = 0;
#endif
if ( audio_out_ctx->channels > 1 ) {
if (audio_out_ctx->channels > 1) {
Warning("Audio isn't mono, changing it.");
audio_out_ctx->channels = 1;
} else {
@ -467,7 +440,7 @@ bool VideoStore::open() {
}
} // end if is AAC
if ( oc->oformat->flags & AVFMT_GLOBALHEADER ) {
if (oc->oformat->flags & AVFMT_GLOBALHEADER) {
#if LIBAVCODEC_VERSION_CHECK(56, 35, 0, 64, 0)
audio_out_ctx->flags |= AV_CODEC_FLAG_GLOBAL_HEADER;
#else
@ -481,14 +454,14 @@ bool VideoStore::open() {
//max_stream_index is 0-based, so add 1
next_dts = new int64_t[max_stream_index+1];
for ( int i = 0; i <= max_stream_index; i++ ) {
for (int i = 0; i <= max_stream_index; i++) {
next_dts[i] = 0;
}
/* open the out file, if needed */
if ( !(out_format->flags & AVFMT_NOFILE) ) {
if (!(out_format->flags & AVFMT_NOFILE)) {
ret = avio_open2(&oc->pb, filename, AVIO_FLAG_WRITE, nullptr, nullptr);
if ( ret < 0 ) {
if (ret < 0) {
Error("Could not open out file '%s': %s", filename,
av_make_error_string(ret).c_str());
return false;
@ -496,39 +469,39 @@ bool VideoStore::open() {
}
zm_dump_stream_format(oc, 0, 0, 1);
if ( audio_out_stream ) zm_dump_stream_format(oc, 1, 0, 1);
if (audio_out_stream) zm_dump_stream_format(oc, 1, 0, 1);
AVDictionary *opts = nullptr;
std::string option_string = monitor->GetEncoderOptions();
ret = av_dict_parse_string(&opts, option_string.c_str(), "=", ",\n", 0);
if ( ret < 0 ) {
ret = av_dict_parse_string(&opts, option_string.c_str(), "=", "#,\n", 0);
if (ret < 0) {
Warning("Could not parse ffmpeg output options '%s'", option_string.c_str());
}
const AVDictionaryEntry *movflags_entry = av_dict_get(opts, "movflags", nullptr, AV_DICT_MATCH_CASE);
if ( !movflags_entry ) {
if (!movflags_entry) {
Debug(1, "setting movflags to frag_keyframe+empty_moov");
// Shiboleth reports that this may break seeking in mp4 before it downloads
av_dict_set(&opts, "movflags", "frag_keyframe+empty_moov", 0);
} else {
Debug(1, "using movflags %s", movflags_entry->value);
}
if ( (ret = avformat_write_header(oc, &opts)) < 0 ) {
if ((ret = avformat_write_header(oc, &opts)) < 0) {
Warning("Unable to set movflags trying with defaults.");
ret = avformat_write_header(oc, nullptr);
} else if ( av_dict_count(opts) != 0 ) {
} else if (av_dict_count(opts) != 0) {
Info("some options not used, turn on debugging for a list.");
AVDictionaryEntry *e = nullptr;
while ( (e = av_dict_get(opts, "", e, AV_DICT_IGNORE_SUFFIX)) != nullptr ) {
while ((e = av_dict_get(opts, "", e, AV_DICT_IGNORE_SUFFIX)) != nullptr) {
Debug(1, "Encoder Option %s=>%s", e->key, e->value);
if ( !e->value ) {
if (!e->value) {
av_dict_set(&opts, e->key, nullptr, 0);
}
}
}
if ( opts ) av_dict_free(&opts);
if ( ret < 0 ) {
if (opts) av_dict_free(&opts);
if (ret < 0) {
Error("Error occurred when writing out file header to %s: %s",
filename, av_make_error_string(ret).c_str());
avio_closep(&oc->pb);
@ -550,13 +523,13 @@ void VideoStore::flush_codecs() {
av_init_packet(&pkt);
// I got crashes if the codec didn't do DELAY, so let's test for it.
if ( video_out_ctx->codec && ( video_out_ctx->codec->capabilities &
if (video_out_ctx->codec && ( video_out_ctx->codec->capabilities &
#if LIBAVCODEC_VERSION_CHECK(57, 64, 0, 64, 0)
AV_CODEC_CAP_DELAY
#else
CODEC_CAP_DELAY
#endif
) ) {
)) {
// Put encoder into flushing mode
while ((zm_send_frame_receive_packet(video_out_ctx, nullptr, pkt)) > 0) {
av_packet_rescale_ts(&pkt,
@ -568,7 +541,7 @@ void VideoStore::flush_codecs() {
Debug(1, "Done writing buffered video.");
} // end if have delay capability
if ( audio_out_codec ) {
if (audio_out_codec) {
// The codec queues data. We need to send a flush command and out
// whatever we get. Failures are not fatal.
@ -576,13 +549,13 @@ void VideoStore::flush_codecs() {
/*
* At the end of the file, we pass the remaining samples to
* the encoder. */
while ( zm_resample_get_delay(resample_ctx, audio_out_ctx->sample_rate) ) {
while (zm_resample_get_delay(resample_ctx, audio_out_ctx->sample_rate)) {
zm_resample_audio(resample_ctx, nullptr, out_frame);
if ( zm_add_samples_to_fifo(fifo, out_frame) ) {
if (zm_add_samples_to_fifo(fifo, out_frame)) {
// Should probably set the frame size to what is reported FIXME
if ( zm_get_samples_from_fifo(fifo, out_frame) ) {
if ( zm_send_frame_receive_packet(audio_out_ctx, out_frame, pkt) > 0 ) {
if (zm_get_samples_from_fifo(fifo, out_frame)) {
if (zm_send_frame_receive_packet(audio_out_ctx, out_frame, pkt) > 0) {
av_packet_rescale_ts(&pkt,
audio_out_ctx->time_base,
audio_out_stream->time_base);
@ -591,11 +564,10 @@ void VideoStore::flush_codecs() {
}
} // end if data returned from fifo
}
} // end while have buffered samples in the resampler
Debug(2, "av_audio_fifo_size = %d", av_audio_fifo_size(fifo));
while ( av_audio_fifo_size(fifo) > 0 ) {
while (av_audio_fifo_size(fifo) > 0) {
/* Take one frame worth of audio samples from the FIFO buffer,
* encode it and write it to the output file. */
@ -603,8 +575,8 @@ void VideoStore::flush_codecs() {
frame_size, av_audio_fifo_size(fifo));
// SHould probably set the frame size to what is reported FIXME
if ( av_audio_fifo_read(fifo, (void **)out_frame->data, frame_size) ) {
if ( zm_send_frame_receive_packet(audio_out_ctx, out_frame, pkt) ) {
if (av_audio_fifo_read(fifo, (void **)out_frame->data, frame_size)) {
if (zm_send_frame_receive_packet(audio_out_ctx, out_frame, pkt)) {
pkt.stream_index = audio_out_stream->index;
av_packet_rescale_ts(&pkt,
@ -622,7 +594,7 @@ void VideoStore::flush_codecs() {
#endif
while (1) {
if ( 0 >= zm_receive_packet(audio_out_ctx, pkt) ) {
if (0 >= zm_receive_packet(audio_out_ctx, pkt)) {
Debug(1, "No more packets");
break;
}
@ -637,11 +609,11 @@ void VideoStore::flush_codecs() {
} // end flush_codecs
VideoStore::~VideoStore() {
if ( oc->pb ) {
if (oc->pb) {
flush_codecs();
// Flush Queues
Debug(1, "Flushing interleaved queues");
Debug(4, "Flushing interleaved queues");
av_interleaved_write_frame(oc, nullptr);
Debug(1, "Writing trailer");
@ -653,17 +625,17 @@ VideoStore::~VideoStore() {
}
// When will we not be using a file ?
if ( !(out_format->flags & AVFMT_NOFILE) ) {
if (!(out_format->flags & AVFMT_NOFILE)) {
/* Close the out file. */
Debug(2, "Closing");
if ( int rc = avio_close(oc->pb) ) {
Debug(4, "Closing");
if (int rc = avio_close(oc->pb)) {
Error("Error closing avio %s", av_err2str(rc));
}
} else {
Debug(3, "Not closing avio because we are not writing to a file.");
}
oc->pb = nullptr;
} // end if oc->pb
} // end if oc->pb
// I wonder if we should be closing the file first.
// I also wonder if we really need to be doing all the ctx
@ -671,7 +643,7 @@ VideoStore::~VideoStore() {
// Just do a file open/close/writeheader/etc.
// What if we were only doing audio recording?
if ( video_out_stream ) {
if (video_out_stream) {
video_in_ctx = nullptr;
avcodec_close(video_out_ctx);
@ -679,7 +651,7 @@ VideoStore::~VideoStore() {
avcodec_free_context(&video_out_ctx);
} // end if video_out_stream
if ( audio_out_stream ) {
if (audio_out_stream) {
#if LIBAVCODEC_VERSION_CHECK(57, 64, 0, 64, 0)
// We allocate and copy in newer ffmpeg, so need to free it
//avcodec_free_context(&audio_in_ctx);
@ -687,7 +659,7 @@ VideoStore::~VideoStore() {
//Debug(4, "Success freeing audio_in_ctx");
audio_in_codec = nullptr;
if ( audio_out_ctx ) {
if (audio_out_ctx) {
Debug(4, "Success closing audio_out_ctx");
avcodec_close(audio_out_ctx);
#if LIBAVCODEC_VERSION_CHECK(57, 64, 0, 64, 0)
@ -696,8 +668,8 @@ VideoStore::~VideoStore() {
}
#if defined(HAVE_LIBAVRESAMPLE) || defined(HAVE_LIBSWRESAMPLE)
if ( resample_ctx ) {
if ( fifo ) {
if (resample_ctx) {
if (fifo) {
av_audio_fifo_free(fifo);
fifo = nullptr;
}
@ -710,15 +682,15 @@ VideoStore::~VideoStore() {
#endif
#endif
}
if ( in_frame ) {
if (in_frame) {
av_frame_free(&in_frame);
in_frame = nullptr;
}
if ( out_frame ) {
if (out_frame) {
av_frame_free(&out_frame);
out_frame = nullptr;
}
if ( converted_in_samples ) {
if (converted_in_samples) {
av_free(converted_in_samples);
converted_in_samples = nullptr;
}
@ -734,7 +706,7 @@ VideoStore::~VideoStore() {
bool VideoStore::setup_resampler() {
#if !defined(HAVE_LIBSWRESAMPLE) && !defined(HAVE_LIBAVRESAMPLE)
Error("%s", "Not built with resample library. Cannot do audio conversion to AAC");
Error("Not built with resample library. Cannot do audio conversion to AAC");
return false;
#else
int ret;
@ -742,17 +714,14 @@ bool VideoStore::setup_resampler() {
#if LIBAVCODEC_VERSION_CHECK(57, 64, 0, 64, 0)
// Newer ffmpeg wants to keep everything separate... so have to lookup our own
// decoder, can't reuse the one from the camera.
audio_in_codec =
avcodec_find_decoder(audio_in_stream->codecpar->codec_id);
audio_in_codec = avcodec_find_decoder(audio_in_stream->codecpar->codec_id);
audio_in_ctx = avcodec_alloc_context3(audio_in_codec);
// Copy params from instream to ctx
ret = avcodec_parameters_to_context(
audio_in_ctx, audio_in_stream->codecpar);
if ( ret < 0 ) {
ret = avcodec_parameters_to_context(audio_in_ctx, audio_in_stream->codecpar);
if (ret < 0) {
Error("Unable to copy audio params to ctx %s",
av_make_error_string(ret).c_str());
}
#else
// codec is already open in ffmpeg_camera
audio_in_ctx = audio_in_stream->codec;
@ -766,7 +735,7 @@ bool VideoStore::setup_resampler() {
#endif
// if the codec is already open, nothing is done.
if ( (ret = avcodec_open2(audio_in_ctx, audio_in_codec, nullptr)) < 0 ) {
if ((ret = avcodec_open2(audio_in_ctx, audio_in_codec, nullptr)) < 0) {
Error("Can't open audio in codec!");
return false;
}
@ -774,7 +743,7 @@ bool VideoStore::setup_resampler() {
Debug(2, "Got something other than AAC (%s)", audio_in_codec->name);
// Some formats (i.e. WAV) do not produce the proper channel layout
if ( audio_in_ctx->channel_layout == 0 ) {
if (audio_in_ctx->channel_layout == 0) {
Debug(2, "Setting input channel layout to mono");
// Perhaps we should not be modifying the audio_in_ctx....
audio_in_ctx->channel_layout = av_get_channel_layout("mono");
@ -788,7 +757,7 @@ bool VideoStore::setup_resampler() {
audio_out_ctx->channel_layout = audio_in_ctx->channel_layout;
audio_out_ctx->sample_fmt = audio_in_ctx->sample_fmt;
#if LIBAVCODEC_VERSION_CHECK(56, 8, 0, 60, 100)
if ( !audio_out_ctx->channel_layout ) {
if (!audio_out_ctx->channel_layout) {
Debug(3, "Correcting channel layout from (%" PRIi64 ") to (%" PRIi64 ")",
audio_out_ctx->channel_layout,
av_get_default_channel_layout(audio_out_ctx->channels)
@ -796,27 +765,26 @@ bool VideoStore::setup_resampler() {
audio_out_ctx->channel_layout = av_get_default_channel_layout(audio_out_ctx->channels);
}
#endif
if ( audio_out_codec->supported_samplerates ) {
if (audio_out_codec->supported_samplerates) {
int found = 0;
for ( unsigned int i = 0; audio_out_codec->supported_samplerates[i]; i++ ) {
if ( audio_out_ctx->sample_rate ==
audio_out_codec->supported_samplerates[i] ) {
for (unsigned int i = 0; audio_out_codec->supported_samplerates[i]; i++) {
if (audio_out_ctx->sample_rate ==
audio_out_codec->supported_samplerates[i]) {
found = 1;
break;
}
}
if ( found ) {
if (found) {
Debug(3, "Sample rate is good %d", audio_out_ctx->sample_rate);
} else {
audio_out_ctx->sample_rate =
audio_out_codec->supported_samplerates[0];
audio_out_ctx->sample_rate = audio_out_codec->supported_samplerates[0];
Debug(1, "Sample rate is no good, setting to (%d)",
audio_out_codec->supported_samplerates[0]);
}
}
/* check that the encoder supports s16 pcm in */
if ( !check_sample_fmt(audio_out_codec, audio_out_ctx->sample_fmt) ) {
if (!check_sample_fmt(audio_out_codec, audio_out_ctx->sample_fmt)) {
Debug(3, "Encoder does not support sample format %s, setting to FLTP",
av_get_sample_fmt_name(audio_out_ctx->sample_fmt));
audio_out_ctx->sample_fmt = AV_SAMPLE_FMT_FLTP;
@ -827,12 +795,12 @@ bool VideoStore::setup_resampler() {
AVDictionary *opts = nullptr;
// Needed to allow AAC
if ( (ret = av_dict_set(&opts, "strict", "experimental", 0)) < 0 ) {
if ((ret = av_dict_set(&opts, "strict", "experimental", 0)) < 0) {
Error("Couldn't set experimental");
}
ret = avcodec_open2(audio_out_ctx, audio_out_codec, &opts);
av_dict_free(&opts);
if ( ret < 0 ) {
if (ret < 0) {
Error("could not open codec (%d) (%s)",
ret, av_make_error_string(ret).c_str());
audio_out_codec = nullptr;
@ -888,7 +856,7 @@ bool VideoStore::setup_resampler() {
#endif
/** Create a new frame to store the audio samples. */
if ( ! in_frame ) {
if (!in_frame) {
if (!(in_frame = zm_av_frame_alloc())) {
Error("Could not allocate in frame");
return false;
@ -896,16 +864,16 @@ bool VideoStore::setup_resampler() {
}
/** Create a new frame to store the audio samples. */
if ( !(out_frame = zm_av_frame_alloc()) ) {
if (!(out_frame = zm_av_frame_alloc())) {
Error("Could not allocate out frame");
av_frame_free(&in_frame);
return false;
}
out_frame->sample_rate = audio_out_ctx->sample_rate;
if ( !(fifo = av_audio_fifo_alloc(
if (!(fifo = av_audio_fifo_alloc(
audio_out_ctx->sample_fmt,
audio_out_ctx->channels, 1)) ) {
audio_out_ctx->channels, 1))) {
Error("Could not allocate FIFO");
return false;
}
@ -918,13 +886,13 @@ bool VideoStore::setup_resampler() {
audio_in_ctx->sample_fmt,
audio_in_ctx->sample_rate,
0, nullptr);
if ( !resample_ctx ) {
if (!resample_ctx) {
Error("Could not allocate resample context");
av_frame_free(&in_frame);
av_frame_free(&out_frame);
return false;
}
if ( (ret = swr_init(resample_ctx)) < 0 ) {
if ((ret = swr_init(resample_ctx)) < 0) {
Error("Could not open resampler");
av_frame_free(&in_frame);
av_frame_free(&out_frame);
@ -937,7 +905,7 @@ bool VideoStore::setup_resampler() {
// Setup the audio resampler
resample_ctx = avresample_alloc_context();
if ( !resample_ctx ) {
if (!resample_ctx) {
Error("Could not allocate resample ctx");
av_frame_free(&in_frame);
av_frame_free(&out_frame);
@ -961,7 +929,7 @@ bool VideoStore::setup_resampler() {
av_opt_set_int(resample_ctx, "out_channels",
audio_out_ctx->channels, 0);
if ( (ret = avresample_open(resample_ctx)) < 0 ) {
if ((ret = avresample_open(resample_ctx)) < 0) {
Error("Could not open resample ctx");
return false;
} else {
@ -986,7 +954,7 @@ bool VideoStore::setup_resampler() {
audio_out_ctx->sample_fmt, 0);
converted_in_samples = reinterpret_cast<uint8_t *>(av_malloc(audioSampleBuffer_size));
if ( !converted_in_samples ) {
if (!converted_in_samples) {
Error("Could not allocate converted in sample pointers");
return false;
} else {
@ -994,11 +962,11 @@ bool VideoStore::setup_resampler() {
}
// Setup the data pointers in the AVFrame
if ( avcodec_fill_audio_frame(
if (avcodec_fill_audio_frame(
out_frame, audio_out_ctx->channels,
audio_out_ctx->sample_fmt,
(const uint8_t *)converted_in_samples,
audioSampleBuffer_size, 0) < 0 ) {
audioSampleBuffer_size, 0) < 0) {
Error("Could not allocate converted in sample pointers");
return false;
}
@ -1048,17 +1016,16 @@ int VideoStore::writeVideoFramePacket(const std::shared_ptr<ZMPacket> &zm_packet
video_out_ctx->width,
video_out_ctx->height
);
} else if ( !zm_packet->in_frame ) {
} else if (!zm_packet->in_frame) {
Debug(4, "Have no in_frame");
if (zm_packet->packet.size and !zm_packet->decoded) {
Debug(4, "Decoding");
if ( !zm_packet->decode(video_in_ctx) ) {
if (!zm_packet->decode(video_in_ctx)) {
Debug(2, "unable to decode yet.");
return 0;
}
// Go straight to out frame
swscale.Convert(zm_packet->in_frame, out_frame);
} else {
Error("Have neither in_frame or image in packet %d!",
zm_packet->image_index);
@ -1110,7 +1077,7 @@ int VideoStore::writeVideoFramePacket(const std::shared_ptr<ZMPacket> &zm_packet
#endif
int64_t in_pts = zm_packet->timestamp.tv_sec * (uint64_t)1000000 + zm_packet->timestamp.tv_usec;
if ( !video_first_pts ) {
if (!video_first_pts) {
video_first_pts = in_pts;
Debug(2, "No video_first_pts, set to (%" PRId64 ") secs(%" PRIi64 ") usecs(%" PRIi64 ")",
video_first_pts,
@ -1146,9 +1113,9 @@ int VideoStore::writeVideoFramePacket(const std::shared_ptr<ZMPacket> &zm_packet
ZM_DUMP_PACKET(opkt, "packet returned by codec");
// Need to adjust pts/dts values from codec time to stream time
if ( opkt.pts != AV_NOPTS_VALUE )
if (opkt.pts != AV_NOPTS_VALUE)
opkt.pts = av_rescale_q(opkt.pts, video_out_ctx->time_base, video_out_stream->time_base);
if ( opkt.dts != AV_NOPTS_VALUE )
if (opkt.dts != AV_NOPTS_VALUE)
opkt.dts = av_rescale_q(opkt.dts, video_out_ctx->time_base, video_out_stream->time_base);
Debug(1, "Timebase conversions using %d/%d -> %d/%d",
video_out_ctx->time_base.num,
@ -1156,10 +1123,9 @@ int VideoStore::writeVideoFramePacket(const std::shared_ptr<ZMPacket> &zm_packet
video_out_stream->time_base.num,
video_out_stream->time_base.den);
int64_t duration = 0;
if ( zm_packet->in_frame ) {
if ( zm_packet->in_frame->pkt_duration ) {
if (zm_packet->in_frame) {
if (zm_packet->in_frame->pkt_duration) {
duration = av_rescale_q(
zm_packet->in_frame->pkt_duration,
video_in_stream->time_base,
@ -1173,9 +1139,8 @@ int VideoStore::writeVideoFramePacket(const std::shared_ptr<ZMPacket> &zm_packet
video_out_stream->time_base.num,
video_out_stream->time_base.den
);
} else if ( video_last_pts != AV_NOPTS_VALUE ) {
duration =
av_rescale_q(
} else if (video_last_pts != AV_NOPTS_VALUE) {
duration = av_rescale_q(
zm_packet->in_frame->pts - video_last_pts,
video_in_stream->time_base,
video_out_stream->time_base);
@ -1185,8 +1150,10 @@ int VideoStore::writeVideoFramePacket(const std::shared_ptr<ZMPacket> &zm_packet
zm_packet->in_frame->pts - video_last_pts,
duration
);
if ( duration <= 0 ) {
duration = zm_packet->in_frame->pkt_duration ? zm_packet->in_frame->pkt_duration : av_rescale_q(1, video_in_stream->time_base, video_out_stream->time_base);
if (duration <= 0) {
duration = zm_packet->in_frame->pkt_duration ?
zm_packet->in_frame->pkt_duration :
av_rescale_q(1, video_in_stream->time_base, video_out_stream->time_base);
}
} // end if in_frmae->pkt_duration
video_last_pts = zm_packet->in_frame->pts;
@ -1194,7 +1161,6 @@ int VideoStore::writeVideoFramePacket(const std::shared_ptr<ZMPacket> &zm_packet
//duration = av_rescale_q(zm_packet->out_frame->pts - video_last_pts, video_in_stream->time_base, video_out_stream->time_base);
} // end if in_frmae
opkt.duration = duration;
} else { // Passthrough
AVPacket *ipkt = &zm_packet->packet;
ZM_DUMP_STREAM_PACKET(video_in_stream, (*ipkt), "Doing passthrough, just copy packet");
@ -1205,8 +1171,8 @@ int VideoStore::writeVideoFramePacket(const std::shared_ptr<ZMPacket> &zm_packet
opkt.flags = ipkt->flags;
opkt.duration = ipkt->duration;
if ( ipkt->dts != AV_NOPTS_VALUE ) {
if ( !video_first_dts ) {
if (ipkt->dts != AV_NOPTS_VALUE) {
if (!video_first_dts) {
Debug(2, "Starting video first_dts will become %" PRId64, ipkt->dts);
video_first_dts = ipkt->dts;
}
@ -1215,14 +1181,13 @@ int VideoStore::writeVideoFramePacket(const std::shared_ptr<ZMPacket> &zm_packet
opkt.dts = next_dts[video_out_stream->index] ? av_rescale_q(next_dts[video_out_stream->index], video_out_stream->time_base, video_in_stream->time_base) : 0;
Debug(3, "Setting dts to video_next_dts %" PRId64 " from %" PRId64, opkt.dts, next_dts[video_out_stream->index]);
}
if ( ipkt->pts != AV_NOPTS_VALUE ) {
if (ipkt->pts != AV_NOPTS_VALUE) {
opkt.pts = ipkt->pts - video_first_dts;
} else {
opkt.pts = AV_NOPTS_VALUE;
}
av_packet_rescale_ts(&opkt, video_in_stream->time_base, video_out_stream->time_base);
ZM_DUMP_STREAM_PACKET(video_out_stream, opkt, "after pts adjustment");
} // end if codec matches
@ -1234,18 +1199,17 @@ int VideoStore::writeVideoFramePacket(const std::shared_ptr<ZMPacket> &zm_packet
} // end int VideoStore::writeVideoFramePacket( AVPacket *ipkt )
int VideoStore::writeAudioFramePacket(const std::shared_ptr<ZMPacket> &zm_packet) {
AVPacket *ipkt = &zm_packet->packet;
int ret;
if ( !audio_out_stream ) {
if (!audio_out_stream) {
Debug(1, "Called writeAudioFramePacket when no audio_out_stream");
return 0;
// FIXME -ve return codes do not free packet in ffmpeg_camera at the moment
}
AVPacket *ipkt = &zm_packet->packet;
int ret;
ZM_DUMP_STREAM_PACKET(audio_in_stream, (*ipkt), "input packet");
if ( !audio_first_dts ) {
if (!audio_first_dts) {
audio_first_dts = ipkt->dts;
audio_next_pts = audio_out_ctx->frame_size;
}
@ -1253,10 +1217,10 @@ int VideoStore::writeAudioFramePacket(const std::shared_ptr<ZMPacket> &zm_packet
Debug(3, "audio first_dts to %" PRId64, audio_first_dts);
// Need to adjust pts before feeding to decoder.... should really copy the pkt instead of modifying it
if ( audio_out_codec ) {
if (audio_out_codec) {
// I wonder if we can get multiple frames per packet? Probably
ret = zm_send_packet_receive_frame(audio_in_ctx, in_frame, *ipkt);
if ( ret < 0 ) {
if (ret < 0) {
Debug(3, "failed to receive frame code: %d", ret);
return 0;
}
@ -1264,15 +1228,15 @@ int VideoStore::writeAudioFramePacket(const std::shared_ptr<ZMPacket> &zm_packet
AVFrame *input_frame = in_frame;
while ( zm_resample_audio(resample_ctx, input_frame, out_frame) ) {
while (zm_resample_audio(resample_ctx, input_frame, out_frame)) {
//out_frame->pkt_duration = in_frame->pkt_duration; // resampling doesn't alter duration
if ( zm_add_samples_to_fifo(fifo, out_frame) <= 0 )
if (zm_add_samples_to_fifo(fifo, out_frame) <= 0)
break;
// We put the samples into the fifo so we are basically resetting the frame
out_frame->nb_samples = audio_out_ctx->frame_size;
if ( zm_get_samples_from_fifo(fifo, out_frame) <= 0 )
if (zm_get_samples_from_fifo(fifo, out_frame) <= 0)
break;
out_frame->pts = audio_next_pts;
@ -1281,7 +1245,7 @@ int VideoStore::writeAudioFramePacket(const std::shared_ptr<ZMPacket> &zm_packet
zm_dump_frame(out_frame, "Out frame after resample");
av_init_packet(&opkt);
if ( zm_send_frame_receive_packet(audio_out_ctx, out_frame, opkt) <= 0 )
if (zm_send_frame_receive_packet(audio_out_ctx, out_frame, opkt) <= 0)
break;
// Scale the PTS of the outgoing packet to be the correct time base
@ -1292,12 +1256,11 @@ int VideoStore::writeAudioFramePacket(const std::shared_ptr<ZMPacket> &zm_packet
write_packet(&opkt, audio_out_stream);
zm_av_packet_unref(&opkt);
if ( zm_resample_get_delay(resample_ctx, out_frame->sample_rate) < out_frame->nb_samples)
if (zm_resample_get_delay(resample_ctx, out_frame->sample_rate) < out_frame->nb_samples)
break;
// This will send a null frame, emptying out the resample buffer
input_frame = nullptr;
} // end while there is data in the resampler
} // end while there is data in the resampler
} else {
av_init_packet(&opkt);
opkt.data = ipkt->data;
@ -1323,15 +1286,15 @@ int VideoStore::write_packet(AVPacket *pkt, AVStream *stream) {
pkt->pos = -1;
pkt->stream_index = stream->index;
if ( pkt->dts == AV_NOPTS_VALUE ) {
if (pkt->dts == AV_NOPTS_VALUE) {
Debug(1, "undef dts, fixing by setting to stream cur_dts %" PRId64, stream->cur_dts);
pkt->dts = stream->cur_dts;
} else if ( pkt->dts < stream->cur_dts ) {
} else if (pkt->dts < stream->cur_dts) {
Debug(1, "non increasing dts, fixing. our dts %" PRId64 " stream cur_dts %" PRId64, pkt->dts, stream->cur_dts);
pkt->dts = stream->cur_dts;
}
if ( pkt->dts > pkt->pts ) {
if (pkt->dts > pkt->pts) {
Debug(1,
"pkt.dts(%" PRId64 ") must be <= pkt.pts(%" PRId64 ")."
"Decompression must happen before presentation.",
@ -1345,9 +1308,8 @@ int VideoStore::write_packet(AVPacket *pkt, AVStream *stream) {
stream->index, next_dts[stream->index]);
int ret = av_interleaved_write_frame(oc, pkt);
if ( ret != 0 ) {
Error("Error writing packet: %s",
av_make_error_string(ret).c_str());
if (ret != 0) {
Error("Error writing packet: %s", av_make_error_string(ret).c_str());
} else {
Debug(4, "Success writing packet");
}

View File

@ -32,7 +32,7 @@ void Zone::Setup(
int p_max_pixel_threshold,
int p_min_alarm_pixels,
int p_max_alarm_pixels,
const Coord &p_filter_box,
const Vector2 &p_filter_box,
int p_min_filter_pixels,
int p_max_filter_pixels,
int p_min_blob_pixels,
@ -130,10 +130,10 @@ void Zone::RecordStats(const Event *event) {
stats.alarm_blobs_,
stats.min_blob_size_,
stats.max_blob_size_,
stats.alarm_box_.LoX(),
stats.alarm_box_.LoY(),
stats.alarm_box_.HiX(),
stats.alarm_box_.HiY(),
stats.alarm_box_.Lo().x_,
stats.alarm_box_.Lo().y_,
stats.alarm_box_.Hi().x_,
stats.alarm_box_.Hi().y_,
stats.score_
);
zmDbDo(sql);
@ -219,10 +219,10 @@ bool Zone::CheckAlarms(const Image *delta_image) {
int alarm_mid_x = -1;
int alarm_mid_y = -1;
unsigned int lo_y = polygon.LoY();
unsigned int lo_x = polygon.LoX();
unsigned int hi_x = polygon.HiX();
unsigned int hi_y = polygon.HiY();
unsigned int lo_x = polygon.Extent().Lo().x_;
unsigned int lo_y = polygon.Extent().Lo().y_;
unsigned int hi_x = polygon.Extent().Hi().x_;
unsigned int hi_y = polygon.Extent().Hi().y_;
Debug(4, "Checking alarms for zone %d/%s in lines %d -> %d", id, label.c_str(), lo_y, hi_y);
@ -267,8 +267,8 @@ bool Zone::CheckAlarms(const Image *delta_image) {
Debug(5, "Current score is %d", stats.score_);
if (check_method >= FILTERED_PIXELS) {
int bx = filter_box.X();
int by = filter_box.Y();
int bx = filter_box.x_;
int by = filter_box.y_;
int bx1 = bx-1;
int by1 = by-1;
@ -630,10 +630,10 @@ bool Zone::CheckAlarms(const Image *delta_image) {
stats.score_ = 1; /* Fix for score of 0 when frame meets thresholds but alarmed area is not big enough */
Debug(5, "Current score is %d", stats.score_);
alarm_lo_x = polygon.HiX()+1;
alarm_hi_x = polygon.LoX()-1;
alarm_lo_y = polygon.HiY()+1;
alarm_hi_y = polygon.LoY()-1;
alarm_lo_x = polygon.Extent().Hi().x_ + 1;
alarm_hi_x = polygon.Extent().Lo().x_ - 1;
alarm_lo_y = polygon.Extent().Hi().y_ + 1;
alarm_hi_y = polygon.Extent().Lo().y_ - 1;
for (uint32 i = 1; i < kWhite; i++) {
BlobStats *bs = &blob_stats[i];
@ -684,11 +684,11 @@ bool Zone::CheckAlarms(const Image *delta_image) {
// Now outline the changed region
if (stats.score_) {
stats.alarm_box_ = Box(Coord(alarm_lo_x, alarm_lo_y), Coord(alarm_hi_x, alarm_hi_y));
stats.alarm_box_ = Box(Vector2(alarm_lo_x, alarm_lo_y), Vector2(alarm_hi_x, alarm_hi_y));
//if ( monitor->followMotion() )
if ( true ) {
stats.alarm_centre_ = Coord(alarm_mid_x, alarm_mid_y);
stats.alarm_centre_ = Vector2(alarm_mid_x, alarm_mid_y);
} else {
stats.alarm_centre_ = stats.alarm_box_.Centre();
}
@ -748,37 +748,39 @@ bool Zone::CheckAlarms(const Image *delta_image) {
bool Zone::ParsePolygonString(const char *poly_string, Polygon &polygon) {
char *str = (char *)poly_string;
int n_coords = 0;
int max_n_coords = strlen(str)/4;
Coord *coords = new Coord[max_n_coords];
std::vector<Vector2> vertices;
vertices.reserve(max_n_coords);
while (*str != '\0') {
char *cp = strchr(str, ',');
if (!cp) {
Error("Bogus coordinate %s found in polygon string", str);
break;
}
int x = atoi(str);
int y = atoi(cp+1);
int y = atoi(cp + 1);
Debug(3, "Got coordinate %d,%d from polygon string", x, y);
coords[n_coords++] = Coord(x, y);
vertices.emplace_back(x, y);
char *ws = strchr(cp+2, ' ');
if (ws)
str = ws+1;
else
char *ws = strchr(cp + 2, ' ');
if (ws) {
str = ws + 1;
} else {
break;
} // end while ! end of string
if (n_coords > 2) {
Debug(3, "Successfully parsed polygon string %s", str);
polygon = Polygon(n_coords, coords);
} else {
Error("Not enough coordinates to form a polygon!");
n_coords = 0;
}
}
delete[] coords;
return n_coords ? true : false;
if (vertices.size() > 2) {
Debug(3, "Successfully parsed polygon string %s", str);
polygon = Polygon(vertices);
} else {
Error("Not enough coordinates to form a polygon!");
}
return !vertices.empty();
} // end bool Zone::ParsePolygonString(const char *poly_string, Polygon &polygon)
bool Zone::ParseZoneString(const char *zone_string, int &zone_id, int &colour, Polygon &polygon) {
@ -872,14 +874,21 @@ std::vector<Zone> Zone::Load(Monitor *monitor) {
continue;
}
if ( polygon.LoX() < 0 || polygon.HiX() >= (int)monitor->Width()
|| polygon.LoY() < 0 || polygon.HiY() >= (int)monitor->Height() ) {
if (polygon.Extent().Lo().x_ < 0 || polygon.Extent().Hi().x_ > static_cast<int32>(monitor->Width())
|| polygon.Extent().Lo().y_ < 0 || polygon.Extent().Hi().y_ > static_cast<int32>(monitor->Height())) {
Error("Zone %d/%s for monitor %s extends outside of image dimensions, (%d,%d), (%d,%d), fixing",
Id, Name, monitor->Name(), polygon.LoX(), polygon.LoY(), polygon.HiX(), polygon.HiY());
if ( polygon.LoX() < 0 ) polygon.LoX(0);
if ( polygon.HiX() >= (int)monitor->Width()) polygon.HiX((int)monitor->Width());
if ( polygon.LoY() < 0 ) polygon.LoY(0);
if ( polygon.HiY() >= (int)monitor->Height() ) polygon.HiY((int)monitor->Height());
Id,
Name,
monitor->Name(),
polygon.Extent().Lo().x_,
polygon.Extent().Lo().y_,
polygon.Extent().Hi().x_,
polygon.Extent().Hi().y_);
polygon.Clip(Box(
{0, 0},
{static_cast<int32>(monitor->Width()), static_cast<int32>(monitor->Height())}
));
}
if ( false && !strcmp( Units, "Percent" ) ) {
@ -899,7 +908,7 @@ std::vector<Zone> Zone::Load(Monitor *monitor) {
zones.emplace_back(
monitor, Id, Name, Type, polygon, AlarmRGB,
CheckMethod, MinPixelThreshold, MaxPixelThreshold,
MinAlarmPixels, MaxAlarmPixels, Coord(FilterX, FilterY),
MinAlarmPixels, MaxAlarmPixels, Vector2(FilterX, FilterY),
MinFilterPixels, MaxFilterPixels,
MinBlobPixels, MaxBlobPixels, MinBlobs, MaxBlobs,
OverloadFrames, ExtendAlarmFrames);
@ -922,9 +931,9 @@ bool Zone::DumpSettings(char *output, bool /*verbose*/) const {
type==INACTIVE?"Inactive":(
type==PRIVACY?"Privacy":"Unknown"
))))));
sprintf( output+strlen(output), " Shape : %d points\n", polygon.getNumCoords() );
for ( int i = 0; i < polygon.getNumCoords(); i++ ) {
sprintf( output+strlen(output), " %i: %d,%d\n", i, polygon.getCoord( i ).X(), polygon.getCoord( i ).Y() );
sprintf( output+strlen(output), " Shape : %zu points\n", polygon.GetVertices().size() );
for (size_t i = 0; i < polygon.GetVertices().size(); i++) {
sprintf(output + strlen(output), " %zu: %d,%d\n", i, polygon.GetVertices()[i].x_, polygon.GetVertices()[i].y_);
}
sprintf( output+strlen(output), " Alarm RGB : %06x\n", alarm_rgb );
sprintf( output+strlen(output), " Check Method: %d - %s\n", check_method,
@ -936,7 +945,7 @@ bool Zone::DumpSettings(char *output, bool /*verbose*/) const {
sprintf( output+strlen(output), " Max Pixel Threshold : %d\n", max_pixel_threshold );
sprintf( output+strlen(output), " Min Alarm Pixels : %d\n", min_alarm_pixels );
sprintf( output+strlen(output), " Max Alarm Pixels : %d\n", max_alarm_pixels );
sprintf( output+strlen(output), " Filter Box : %d,%d\n", filter_box.X(), filter_box.Y() );
sprintf(output+strlen(output), " Filter Box : %d,%d\n", filter_box.x_, filter_box.y_ );
sprintf( output+strlen(output), " Min Filter Pixels : %d\n", min_filter_pixels );
sprintf( output+strlen(output), " Max Filter Pixels : %d\n", max_filter_pixels );
sprintf( output+strlen(output), " Min Blob Pixels : %d\n", min_blob_pixels );
@ -960,8 +969,8 @@ void Zone::std_alarmedpixels(
if ( max_pixel_threshold )
calc_max_pixel_threshold = max_pixel_threshold;
lo_y = polygon.LoY();
hi_y = polygon.HiY();
lo_y = polygon.Extent().Lo().y_;
hi_y = polygon.Extent().Hi().y_;
for ( unsigned int y = lo_y; y <= hi_y; y++ ) {
unsigned int lo_x = ranges[y].lo_x;
unsigned int hi_x = ranges[y].hi_x;

View File

@ -21,12 +21,12 @@
#define ZM_ZONE_H
#include "zm_box.h"
#include "zm_coord.h"
#include "zm_define.h"
#include "zm_config.h"
#include "zm_poly.h"
#include "zm_rgb.h"
#include "zm_zone_stats.h"
#include "zm_vector2.h"
#include <algorithm>
#include <string>
@ -77,7 +77,7 @@ class Zone {
int min_alarm_pixels;
int max_alarm_pixels;
Coord filter_box;
Vector2 filter_box;
int min_filter_pixels;
int max_filter_pixels;
@ -112,7 +112,7 @@ class Zone {
int p_max_pixel_threshold,
int p_min_alarm_pixels,
int p_max_alarm_pixels,
const Coord &p_filter_box,
const Vector2 &p_filter_box,
int p_min_filter_pixels,
int p_max_filter_pixels,
int p_min_blob_pixels,
@ -137,7 +137,7 @@ class Zone {
int p_max_pixel_threshold=0,
int p_min_alarm_pixels=50,
int p_max_alarm_pixels=75000,
const Coord &p_filter_box=Coord( 3, 3 ),
const Vector2 &p_filter_box = Vector2(3, 3),
int p_min_filter_pixels=50,
int p_max_filter_pixels=50000,
int p_min_blob_pixels=10,
@ -164,7 +164,7 @@ class Zone {
blob_stats{},
stats(p_id)
{
Setup(Zone::INACTIVE, p_polygon, kRGBBlack, (Zone::CheckMethod)0, 0, 0, 0, 0, Coord(0, 0), 0, 0, 0, 0, 0, 0, 0, 0);
Setup(Zone::INACTIVE, p_polygon, kRGBBlack, (Zone::CheckMethod)0, 0, 0, 0, 0, Vector2(0, 0), 0, 0, 0, 0, 0, 0, 0, 0);
}
Zone(Monitor *p_monitor, int p_id, const char *p_label, ZoneType p_type, const Polygon &p_polygon)
:
@ -174,7 +174,7 @@ class Zone {
blob_stats{},
stats(p_id)
{
Setup(p_type, p_polygon, kRGBBlack, (Zone::CheckMethod)0, 0, 0, 0, 0, Coord( 0, 0 ), 0, 0, 0, 0, 0, 0, 0, 0 );
Setup(p_type, p_polygon, kRGBBlack, (Zone::CheckMethod)0, 0, 0, 0, 0, Vector2(0, 0), 0, 0, 0, 0, 0, 0, 0, 0 );
}
Zone(const Zone &z);
@ -195,7 +195,7 @@ class Zone {
inline bool WasAlarmed() const { return was_alarmed; }
inline void SetAlarm() { was_alarmed = alarmed; alarmed = true; }
inline void ClearAlarm() { was_alarmed = alarmed; alarmed = false; }
inline Coord GetAlarmCentre() const { return stats.alarm_centre_; }
inline Vector2 GetAlarmCentre() const { return stats.alarm_centre_; }
inline unsigned int Score() const { return stats.score_; }
inline void ResetStats() {

View File

@ -21,8 +21,8 @@
#define ZM_ZONE_STATS_H
#include "zm_box.h"
#include "zm_coord.h"
#include "zm_logger.h"
#include "zm_vector2.h"
class ZoneStats {
public:
@ -35,8 +35,6 @@ class ZoneStats {
alarm_blobs_(0),
min_blob_size_(0),
max_blob_size_(0),
alarm_box_({}),
alarm_centre_({}),
score_(0) {};
void Reset() {
@ -47,10 +45,7 @@ class ZoneStats {
alarm_blobs_ = 0;
min_blob_size_ = 0;
max_blob_size_ = 0;
alarm_box_.LoX(0);
alarm_box_.LoY(0);
alarm_box_.HiX(0);
alarm_box_.HiY(0);
alarm_box_ = {};
alarm_centre_ = {};
score_ = 0;
}
@ -67,12 +62,12 @@ class ZoneStats {
alarm_blobs_,
min_blob_size_,
max_blob_size_,
alarm_box_.LoX(),
alarm_box_.LoY(),
alarm_box_.HiX(),
alarm_box_.HiY(),
alarm_centre_.X(),
alarm_centre_.Y(),
alarm_box_.Lo().x_,
alarm_box_.Lo().y_,
alarm_box_.Hi().x_,
alarm_box_.Hi().y_,
alarm_centre_.x_,
alarm_centre_.y_,
score_
);
}
@ -87,7 +82,7 @@ class ZoneStats {
int min_blob_size_;
int max_blob_size_;
Box alarm_box_;
Coord alarm_centre_;
Vector2 alarm_centre_;
unsigned int score_;
};

View File

@ -12,10 +12,13 @@
include(Catch)
set(TEST_SOURCES
zm_box.cpp
zm_comms.cpp
zm_crypt.cpp
zm_font.cpp
zm_utils.cpp)
zm_poly.cpp
zm_utils.cpp
zm_vector2.cpp)
add_executable(tests main.cpp ${TEST_SOURCES})

52
tests/zm_box.cpp Normal file
View File

@ -0,0 +1,52 @@
/*
* This file is part of the ZoneMinder Project. See AUTHORS file for Copyright information
*
* This program is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License as published by the
* Free Software Foundation; either version 2 of the License, or (at your
* option) any later version.
*
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
* more details.
*
* You should have received a copy of the GNU General Public License along
* with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "zm_catch2.h"
#include "zm_box.h"
TEST_CASE("Box: default constructor") {
Box b;
REQUIRE(b.Lo() == Vector2(0, 0));
REQUIRE(b.Hi() == Vector2(0, 0));
REQUIRE(b.Size() == Vector2(0, 0));
REQUIRE(b.Area() == 0);
}
TEST_CASE("Box: construct from lo and hi") {
Box b({1, 1}, {5, 5});
SECTION("basic properties") {
REQUIRE(b.Lo() == Vector2(1, 1));
REQUIRE(b.Hi() == Vector2(5, 5));
REQUIRE(b.Size() == Vector2(4 ,4));
REQUIRE(b.Area() == 16);
REQUIRE(b.Centre() == Vector2(3, 3));
REQUIRE(b.Vertices() == std::vector<Vector2>{{1, 1}, {5, 1}, {5, 5}, {1, 5}});
}
SECTION("contains") {
REQUIRE(b.Contains({0, 0}) == false);
REQUIRE(b.Contains({1, 1}) == true);
REQUIRE(b.Contains({3, 3}) == true);
REQUIRE(b.Contains({5, 5}) == true);
REQUIRE(b.Contains({6, 6}) == false);
}
}

30
tests/zm_catch2.h Normal file
View File

@ -0,0 +1,30 @@
/*
* This file is part of the ZoneMinder Project. See AUTHORS file for Copyright information
*
* This program is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License as published by the
* Free Software Foundation; either version 2 of the License, or (at your
* option) any later version.
*
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
* more details.
*
* You should have received a copy of the GNU General Public License along
* with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef ZONEMINDER_TESTS_ZM_CATCH2_H_
#define ZONEMINDER_TESTS_ZM_CATCH2_H_
#include "catch2/catch.hpp"
#include "zm_vector2.h"
inline std::ostream &operator<<(std::ostream &os, Vector2 const &value) {
os << "{ X: " << value.x_ << ", Y: " << value.y_ << " }";
return os;
}
#endif //ZONEMINDER_TESTS_ZM_CATCH2_H_

View File

@ -15,7 +15,7 @@
* with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "catch2/catch.hpp"
#include "zm_catch2.h"
#include "zm_comms.h"
#include <array>

View File

@ -15,7 +15,7 @@
* with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "catch2/catch.hpp"
#include "zm_catch2.h"
#include "zm_crypt.h"

View File

@ -15,7 +15,7 @@
* with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "catch2/catch.hpp"
#include "zm_catch2.h"
#include "zm_font.h"

71
tests/zm_poly.cpp Normal file
View File

@ -0,0 +1,71 @@
/*
* This file is part of the ZoneMinder Project. See AUTHORS file for Copyright information
*
* This program is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License as published by the
* Free Software Foundation; either version 2 of the License, or (at your
* option) any later version.
*
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
* more details.
*
* You should have received a copy of the GNU General Public License along
* with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "zm_catch2.h"
#include "zm_poly.h"
TEST_CASE("Polygon: default constructor") {
Polygon p;
REQUIRE(p.Area() == 0);
REQUIRE(p.Centre() == Vector2(0, 0));
}
TEST_CASE("Polygon: construct from vertices") {
std::vector<Vector2> vertices{{{0, 0}, {6, 0}, {0, 6}}};
Polygon p(vertices);
REQUIRE(p.Area() == 18);
REQUIRE(p.Extent().Size() == Vector2(6, 6));
}
TEST_CASE("Polygon: clipping") {
// This a concave polygon in a shape resembling a "W"
std::vector<Vector2> v = {
{3, 1},
{5, 1},
{6, 3},
{7, 1},
{9, 1},
{10, 8},
{8, 8},
{7, 5},
{5, 5},
{4, 8},
{2, 8}
};
Polygon p(v);
REQUIRE(p.GetVertices().size() == 11);
REQUIRE(p.Extent().Size() == Vector2(8, 7));
SECTION("boundary box larger than polygon") {
p.Clip(Box({1, 0}, {11, 9}));
REQUIRE(p.GetVertices().size() == 11);
REQUIRE(p.Extent().Size() == Vector2(8, 7));
}
SECTION("boundary box smaller than polygon") {
p.Clip(Box({2, 4}, {10, 7}));
REQUIRE(p.GetVertices().size() == 8);
REQUIRE(p.Extent().Size() == Vector2(8, 3));
}
}

View File

@ -15,7 +15,7 @@
* with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "catch2/catch.hpp"
#include "zm_catch2.h"
#include "zm_utils.h"
#include <sstream>

94
tests/zm_vector2.cpp Normal file
View File

@ -0,0 +1,94 @@
/*
* This file is part of the ZoneMinder Project. See AUTHORS file for Copyright information
*
* This program is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License as published by the
* Free Software Foundation; either version 2 of the License, or (at your
* option) any later version.
*
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
* more details.
*
* You should have received a copy of the GNU General Public License along
* with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "zm_catch2.h"
#include "zm_vector2.h"
TEST_CASE("Vector2: default constructor") {
Vector2 c;
REQUIRE(c.x_ == 0);
REQUIRE(c.y_ == 0);
}
TEST_CASE("Vector2: x/y constructor") {
Vector2 c(1, 2);
REQUIRE(c.x_ == 1);
REQUIRE(c.y_ == 2);
}
TEST_CASE("Vector2: assignment/copy") {
Vector2 c;
Vector2 c2(1, 2);
REQUIRE(c.x_ == 0);
REQUIRE(c.y_ == 0);
SECTION("assignment operator") {
c = c2;
REQUIRE(c.x_ == 1);
REQUIRE(c.y_ == 2);
}
SECTION("copy constructor") {
Vector2 c3(c2); // NOLINT(performance-unnecessary-copy-initialization)
REQUIRE(c3.x_ == 1);
REQUIRE(c3.y_ == 2);
}
}
TEST_CASE("Vector2: comparison operators") {
Vector2 c1(1, 2);
Vector2 c2(1, 2);
Vector2 c3(1, 3);
REQUIRE((c1 == c2) == true);
REQUIRE((c1 != c3) == true);
}
TEST_CASE("Vector2: arithmetic operators") {
Vector2 c(1, 1);
SECTION("addition") {
Vector2 c1 = c + Vector2(1, 1);
REQUIRE(c1 == Vector2(2, 2));
c += {1, 2};
REQUIRE(c == Vector2(2, 3));
}
SECTION("subtraction") {
Vector2 c1 = c - Vector2(1, 1);
REQUIRE(c1 == Vector2(0, 0));
c -= {1, 2};
REQUIRE(c == Vector2(0, -1));
}
SECTION("scalar multiplication") {
c = c * 2;
REQUIRE(c == Vector2(2, 2));
}
}
TEST_CASE("Vector2: determinate") {
Vector2 v(1, 1);
REQUIRE(v.Determinant({0, 0}) == 0);
REQUIRE(v.Determinant({1, 1}) == 0);
REQUIRE(v.Determinant({1, 2}) == 1);
}

View File

@ -1 +1 @@
1.36.0
1.37.0