Skip to content

Commit 86b2d8a

Browse files
author
Clement Champetier
committed
VideoProperties: check formatContext/codecContext/codec pointers
* If can't acces the data: * return an error (a particular string for example). * or throw a runtime exception if no way to return an error.
1 parent e967cdc commit 86b2d8a

File tree

2 files changed

+253
-31
lines changed

2 files changed

+253
-31
lines changed

src/AvTranscoder/mediaProperty/VideoProperties.cpp

Lines changed: 230 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ extern "C" {
66
#include <libavutil/avutil.h>
77
}
88

9+
#include <stdexcept>
910
#include <iostream>
1011
#include <iomanip>
1112
#include <sstream>
@@ -42,7 +43,6 @@ VideoProperties::VideoProperties( const AVFormatContext* formatContext, const si
4243
#if LIBAVUTIL_VERSION_MAJOR > 51
4344
_pixFmt = av_pix_fmt_desc_get( _codecContext->pix_fmt );
4445
#else
45-
_pixFmt = NULL;
4646
if( _codecContext->pix_fmt >= 0 && _codecContext->pix_fmt < PIX_FMT_NB )
4747
_pixFmt = &av_pix_fmt_descriptors[ _codecContext->pix_fmt ];
4848
#endif
@@ -96,6 +96,9 @@ std::string VideoProperties::getProfileName() const
9696

9797
std::string VideoProperties::getColorTransfert() const
9898
{
99+
if( ! _codecContext )
100+
return "unknown codec context";
101+
99102
switch( _codecContext->color_trc )
100103
{
101104
case AVCOL_TRC_BT709:
@@ -157,6 +160,9 @@ std::string VideoProperties::getColorTransfert() const
157160

158161
std::string VideoProperties::getColorspace() const
159162
{
163+
if( ! _codecContext )
164+
return "unknown codec context";
165+
160166
switch( _codecContext->colorspace )
161167
{
162168
case AVCOL_SPC_RGB:
@@ -199,6 +205,9 @@ std::string VideoProperties::getColorspace() const
199205

200206
std::string VideoProperties::getColorRange() const
201207
{
208+
if( ! _codecContext )
209+
return "unknown codec context";
210+
202211
switch( _codecContext->color_range )
203212
{
204213
case AVCOL_RANGE_UNSPECIFIED:
@@ -216,6 +225,9 @@ std::string VideoProperties::getColorRange() const
216225

217226
std::string VideoProperties::getColorPrimaries() const
218227
{
228+
if( ! _codecContext )
229+
return "unknown codec context";
230+
219231
switch( _codecContext->color_primaries )
220232
{
221233
case AVCOL_PRI_BT709:
@@ -247,6 +259,9 @@ std::string VideoProperties::getColorPrimaries() const
247259

248260
std::string VideoProperties::getChromaSampleLocation() const
249261
{
262+
if( ! _codecContext )
263+
return "unknown codec context";
264+
250265
switch( _codecContext->chroma_sample_location )
251266
{
252267
case AVCHROMA_LOC_UNSPECIFIED:
@@ -272,6 +287,9 @@ std::string VideoProperties::getChromaSampleLocation() const
272287

273288
std::string VideoProperties::getFieldOrder() const
274289
{
290+
if( ! _codecContext )
291+
return "unknown codec context";
292+
275293
switch( _codecContext->field_order )
276294
{
277295
case AV_FIELD_UNKNOWN:
@@ -291,9 +309,27 @@ std::string VideoProperties::getFieldOrder() const
291309
}
292310
}
293311

312+
std::string VideoProperties::getPixelName() const
313+
{
314+
if( _pixFmt && _pixFmt->name )
315+
return std::string( _pixFmt->name );
316+
return "unknown pixel name";
317+
}
318+
294319
std::string VideoProperties::getEndianess() const
295320
{
296-
return ( _pixFmt->flags & PIX_FMT_BE ) ? "big" : "little";
321+
if( _pixFmt )
322+
return ( _pixFmt->flags & PIX_FMT_BE ) ? "big" : "little";
323+
return "unknown pixel format";
324+
}
325+
326+
327+
int64_t VideoProperties::getStartTimecode() const
328+
{
329+
if( ! _codecContext )
330+
throw std::runtime_error( "unknown codec context" );
331+
332+
return _codecContext->timecode_frame_start;
297333
}
298334

299335
std::string VideoProperties::getStartTimecodeString() const
@@ -312,6 +348,9 @@ std::string VideoProperties::getStartTimecodeString() const
312348

313349
Rational VideoProperties::getTimeBase() const
314350
{
351+
if( ! _codecContext )
352+
throw std::runtime_error( "unknown codec context" );
353+
315354
Rational timeBase;
316355
timeBase.num = _codecContext->time_base.num;
317356
timeBase.den = _codecContext->time_base.den;
@@ -320,6 +359,9 @@ Rational VideoProperties::getTimeBase() const
320359

321360
Rational VideoProperties::getSar() const
322361
{
362+
if( ! _codecContext )
363+
throw std::runtime_error( "unknown codec context" );
364+
323365
Rational sar;
324366
sar.num = _codecContext->sample_aspect_ratio.num;
325367
sar.den = _codecContext->sample_aspect_ratio.den;
@@ -328,6 +370,9 @@ Rational VideoProperties::getSar() const
328370

329371
Rational VideoProperties::getDar() const
330372
{
373+
if( ! _codecContext )
374+
throw std::runtime_error( "unknown codec context" );
375+
331376
int darNum, darDen;
332377
av_reduce( &darNum, &darDen,
333378
_codecContext->width * getSar().num,
@@ -340,6 +385,126 @@ Rational VideoProperties::getDar() const
340385
return dar;
341386
}
342387

388+
size_t VideoProperties::getCodecId() const
389+
{
390+
if( ! _codecContext )
391+
throw std::runtime_error( "unknown codec context" );
392+
393+
return _codecContext->codec_id;
394+
}
395+
396+
size_t VideoProperties::getBitRate() const
397+
{
398+
if( ! _codecContext )
399+
throw std::runtime_error( "unknown codec context" );
400+
401+
return _codecContext->bit_rate;
402+
}
403+
404+
size_t VideoProperties::getMaxBitRate() const
405+
{
406+
if( ! _codecContext )
407+
throw std::runtime_error( "unknown codec context" );
408+
409+
return _codecContext->rc_max_rate;
410+
}
411+
412+
size_t VideoProperties::getMinBitRate() const
413+
{
414+
if( ! _codecContext )
415+
throw std::runtime_error( "unknown codec context" );
416+
417+
return _codecContext->rc_min_rate;
418+
}
419+
420+
size_t VideoProperties::getTicksPerFrame() const
421+
{
422+
if( ! _codecContext )
423+
throw std::runtime_error( "unknown codec context" );
424+
425+
return _codecContext->ticks_per_frame;
426+
}
427+
428+
size_t VideoProperties::getWidth() const
429+
{
430+
if( ! _codecContext )
431+
throw std::runtime_error( "unknown codec context" );
432+
433+
return _codecContext->width;
434+
}
435+
436+
size_t VideoProperties::getHeight() const
437+
{
438+
if( ! _codecContext )
439+
throw std::runtime_error( "unknown codec context" );
440+
441+
return _codecContext->height;
442+
}
443+
444+
size_t VideoProperties::getGopSize() const
445+
{
446+
if( ! _codecContext )
447+
throw std::runtime_error( "unknown codec context" );
448+
449+
return _codecContext->gop_size;
450+
}
451+
452+
size_t VideoProperties::getDtgActiveFormat() const
453+
{
454+
if( ! _codecContext )
455+
throw std::runtime_error( "unknown codec context" );
456+
457+
return _codecContext->dtg_active_format;
458+
}
459+
460+
size_t VideoProperties::getReferencesFrames() const
461+
{
462+
if( ! _codecContext )
463+
throw std::runtime_error( "unknown codec context" );
464+
465+
return _codecContext->refs;
466+
}
467+
468+
int VideoProperties::getProfile() const
469+
{
470+
if( ! _codecContext )
471+
throw std::runtime_error( "unknown codec context" );
472+
473+
return _codecContext->profile;
474+
}
475+
476+
int VideoProperties::getLevel() const
477+
{
478+
if( ! _codecContext )
479+
throw std::runtime_error( "unknown codec context" );
480+
481+
return _codecContext->level;
482+
}
483+
484+
size_t VideoProperties::getComponentsCount() const
485+
{
486+
if( ! _pixFmt )
487+
throw std::runtime_error( "unknown pixel format" );
488+
489+
return _pixFmt->nb_components;
490+
}
491+
492+
size_t VideoProperties::getChromaWidth() const
493+
{
494+
if( ! _pixFmt )
495+
throw std::runtime_error( "unknown pixel format" );
496+
497+
return _pixFmt->log2_chroma_w;
498+
}
499+
500+
size_t VideoProperties::getChromaHeight() const
501+
{
502+
if( ! _pixFmt )
503+
throw std::runtime_error( "unknown pixel format" );
504+
505+
return _pixFmt->log2_chroma_h;
506+
}
507+
343508
double VideoProperties::getFps() const
344509
{
345510
Rational timeBase = getTimeBase();
@@ -349,8 +514,59 @@ double VideoProperties::getFps() const
349514
return fps;
350515
}
351516

517+
bool VideoProperties::hasBFrames() const
518+
{
519+
if( ! _codecContext )
520+
throw std::runtime_error( "unknown codec context" );
521+
522+
return (bool) _codecContext->has_b_frames;
523+
}
524+
525+
bool VideoProperties::isIndexedColors() const
526+
{
527+
if( ! _pixFmt )
528+
throw std::runtime_error( "unknown pixel format" );
529+
530+
return (bool) _pixFmt->flags & PIX_FMT_PAL;
531+
}
532+
533+
bool VideoProperties::isBitWisePacked() const
534+
{
535+
if( ! _pixFmt )
536+
throw std::runtime_error( "unknown pixel format" );
537+
538+
return (bool) _pixFmt->flags & PIX_FMT_BITSTREAM;
539+
}
540+
541+
bool VideoProperties::isHardwareAccelerated() const
542+
{
543+
if( ! _pixFmt )
544+
throw std::runtime_error( "unknown pixel format" );
545+
546+
return (bool) _pixFmt->flags & PIX_FMT_HWACCEL;
547+
}
548+
549+
bool VideoProperties::isPlanar() const
550+
{
551+
if( ! _pixFmt )
552+
throw std::runtime_error( "unknown pixel format" );
553+
554+
return (bool) _pixFmt->flags & PIX_FMT_PLANAR;
555+
}
556+
557+
bool VideoProperties::isRgbPixelData() const
558+
{
559+
if( ! _pixFmt )
560+
throw std::runtime_error( "unknown pixel format" );
561+
562+
return (bool) _pixFmt->flags & PIX_FMT_RGB;
563+
}
564+
352565
bool VideoProperties::isPseudoPaletted() const
353566
{
567+
if( ! _pixFmt )
568+
throw std::runtime_error( "unknown pixel format" );
569+
354570
#if LIBAVCODEC_VERSION_MAJOR > 53
355571
return (bool) _pixFmt->flags & PIX_FMT_PSEUDOPAL;
356572
#else
@@ -360,6 +576,9 @@ bool VideoProperties::isPseudoPaletted() const
360576

361577
bool VideoProperties::hasAlpha() const
362578
{
579+
if( ! _pixFmt )
580+
throw std::runtime_error( "unknown pixel format" );
581+
363582
#if LIBAVCODEC_VERSION_MAJOR > 53
364583
return (bool) _pixFmt->flags & PIX_FMT_ALPHA;
365584
#else
@@ -433,13 +652,16 @@ void VideoProperties::analyseGopStructure( IProgress& progress )
433652
std::vector<Channel> VideoProperties::getChannels() const
434653
{
435654
std::vector<Channel> channels;
436-
for( size_t channel = 0; channel < (size_t)_pixFmt->nb_components; ++channel )
655+
if( _pixFmt )
437656
{
438-
Channel c;
439-
c.id = channel;
440-
c.chromaHeight = (size_t)_pixFmt->comp[channel].plane;
441-
c.bitStep = (size_t)_pixFmt->comp[channel].step_minus1;
442-
channels.push_back( c );
657+
for( size_t channel = 0; channel < (size_t)_pixFmt->nb_components; ++channel )
658+
{
659+
Channel c;
660+
c.id = channel;
661+
c.chromaHeight = (size_t)_pixFmt->comp[channel].plane;
662+
c.bitStep = (size_t)_pixFmt->comp[channel].step_minus1;
663+
channels.push_back( c );
664+
}
443665
}
444666
return channels;
445667
}

0 commit comments

Comments
 (0)