@@ -103,11 +103,10 @@ void SingleStreamDecoder::initializeDecoder() {
103
103
// which decodes a few frames to get missing info. For more, see:
104
104
// https://ffmpeg.org/doxygen/7.0/group__lavf__decoding.html
105
105
int status = avformat_find_stream_info (formatContext_.get (), nullptr );
106
- if (status < 0 ) {
107
- throw std::runtime_error (
108
- " Failed to find stream info: " +
109
- getFFMPEGErrorStringFromErrorCode (status));
110
- }
106
+ TORCH_CHECK (
107
+ status >= 0 ,
108
+ " Failed to find stream info: " ,
109
+ getFFMPEGErrorStringFromErrorCode (status));
111
110
112
111
for (unsigned int i = 0 ; i < formatContext_->nb_streams ; i++) {
113
112
AVStream* avStream = formatContext_->streams [i];
@@ -222,11 +221,10 @@ void SingleStreamDecoder::scanFileAndUpdateMetadataAndIndex() {
222
221
break ;
223
222
}
224
223
225
- if (status != AVSUCCESS) {
226
- throw std::runtime_error (
227
- " Failed to read frame from input file: " +
228
- getFFMPEGErrorStringFromErrorCode (status));
229
- }
224
+ TORCH_CHECK (
225
+ status == AVSUCCESS,
226
+ " Failed to read frame from input file: " ,
227
+ getFFMPEGErrorStringFromErrorCode (status));
230
228
231
229
if (packet->flags & AV_PKT_FLAG_DISCARD) {
232
230
continue ;
@@ -279,11 +277,10 @@ void SingleStreamDecoder::scanFileAndUpdateMetadataAndIndex() {
279
277
280
278
// Reset the seek-cursor back to the beginning.
281
279
int status = avformat_seek_file (formatContext_.get (), 0 , INT64_MIN, 0 , 0 , 0 );
282
- if (status < 0 ) {
283
- throw std::runtime_error (
284
- " Could not seek file to pts=0: " +
285
- getFFMPEGErrorStringFromErrorCode (status));
286
- }
280
+ TORCH_CHECK (
281
+ status >= 0 ,
282
+ " Could not seek file to pts=0: " ,
283
+ getFFMPEGErrorStringFromErrorCode (status));
287
284
288
285
// Sort all frames by their pts.
289
286
for (auto & [streamIndex, streamInfo] : streamInfos_) {
@@ -363,11 +360,11 @@ void SingleStreamDecoder::addStream(
363
360
activeStreamIndex_ = av_find_best_stream (
364
361
formatContext_.get (), mediaType, streamIndex, -1 , &avCodec, 0 );
365
362
366
- if (activeStreamIndex_ < 0 ) {
367
- throw std::invalid_argument (
368
- " No valid stream found in input file. Is " +
369
- std::to_string (streamIndex) + " of the desired media type? " );
370
- }
363
+ TORCH_CHECK (
364
+ activeStreamIndex_ >= 0 ,
365
+ " No valid stream found in input file. Is " ,
366
+ std::to_string (streamIndex),
367
+ " of the desired media type? " );
371
368
372
369
TORCH_CHECK (avCodec != nullptr );
373
370
@@ -415,9 +412,7 @@ void SingleStreamDecoder::addStream(
415
412
}
416
413
417
414
retVal = avcodec_open2 (streamInfo.codecContext .get (), avCodec, nullptr );
418
- if (retVal < AVSUCCESS) {
419
- throw std::invalid_argument (getFFMPEGErrorStringFromErrorCode (retVal));
420
- }
415
+ TORCH_CHECK (retVal >= AVSUCCESS, getFFMPEGErrorStringFromErrorCode (retVal));
421
416
422
417
codecContext->time_base = streamInfo.stream ->time_base ;
423
418
containerMetadata_.allStreamMetadata [activeStreamIndex_].codecName =
@@ -446,13 +441,12 @@ void SingleStreamDecoder::addVideoStream(
446
441
auto & streamMetadata =
447
442
containerMetadata_.allStreamMetadata [activeStreamIndex_];
448
443
449
- if (seekMode_ == SeekMode::approximate &&
450
- !streamMetadata.averageFpsFromHeader .has_value ()) {
451
- throw std::runtime_error (
452
- " Seek mode is approximate, but stream " +
453
- std::to_string (activeStreamIndex_) +
454
- " does not have an average fps in its metadata." );
455
- }
444
+ TORCH_CHECK (
445
+ !(seekMode_ == SeekMode::approximate &&
446
+ !streamMetadata.averageFps .has_value ()),
447
+ " Seek mode is approximate, but stream " ,
448
+ std::to_string (activeStreamIndex_),
449
+ " does not have an average fps in its metadata." );
456
450
457
451
auto & streamInfo = streamInfos_[activeStreamIndex_];
458
452
streamInfo.videoStreamOptions = videoStreamOptions;
@@ -1048,11 +1042,13 @@ void SingleStreamDecoder::maybeSeekToBeforeDesiredPts() {
1048
1042
desiredPts,
1049
1043
desiredPts,
1050
1044
0 );
1051
- if (status < 0 ) {
1052
- throw std::runtime_error (
1053
- " Could not seek file to pts=" + std::to_string (desiredPts) + " : " +
1054
- getFFMPEGErrorStringFromErrorCode (status));
1055
- }
1045
+ TORCH_CHECK (
1046
+ status >= 0 ,
1047
+ " Could not seek file to pts=" ,
1048
+ std::to_string (desiredPts),
1049
+ " : " ,
1050
+ getFFMPEGErrorStringFromErrorCode (status));
1051
+
1056
1052
decodeStats_.numFlushes ++;
1057
1053
avcodec_flush_buffers (streamInfo.codecContext .get ());
1058
1054
}
@@ -1121,21 +1117,20 @@ UniqueAVFrame SingleStreamDecoder::decodeAVFrame(
1121
1117
status = avcodec_send_packet (
1122
1118
streamInfo.codecContext .get (),
1123
1119
/* avpkt=*/ nullptr );
1124
- if (status < AVSUCCESS) {
1125
- throw std::runtime_error (
1126
- " Could not flush decoder: " +
1127
- getFFMPEGErrorStringFromErrorCode (status));
1128
- }
1120
+ TORCH_CHECK (
1121
+ status >= AVSUCCESS,
1122
+ " Could not flush decoder: " ,
1123
+ getFFMPEGErrorStringFromErrorCode (status));
1129
1124
1130
1125
reachedEOF = true ;
1131
1126
break ;
1132
1127
}
1133
1128
1134
- if (status < AVSUCCESS) {
1135
- throw std::runtime_error (
1136
- " Could not read frame from input file: " +
1137
- getFFMPEGErrorStringFromErrorCode (status));
1138
- }
1129
+ TORCH_CHECK (
1130
+ status >= AVSUCCESS,
1131
+ " Could not read frame from input file: " ,
1132
+ getFFMPEGErrorStringFromErrorCode (status));
1133
+
1139
1134
} while (packet->stream_index != activeStreamIndex_);
1140
1135
1141
1136
if (reachedEOF) {
@@ -1147,23 +1142,22 @@ UniqueAVFrame SingleStreamDecoder::decodeAVFrame(
1147
1142
// We got a valid packet. Send it to the decoder, and we'll receive it in
1148
1143
// the next iteration.
1149
1144
status = avcodec_send_packet (streamInfo.codecContext .get (), packet.get ());
1150
- if (status < AVSUCCESS) {
1151
- throw std::runtime_error (
1152
- " Could not push packet to decoder: " +
1153
- getFFMPEGErrorStringFromErrorCode (status));
1154
- }
1145
+ TORCH_CHECK (
1146
+ status >= AVSUCCESS,
1147
+ " Could not push packet to decoder: " ,
1148
+ getFFMPEGErrorStringFromErrorCode (status));
1155
1149
1156
1150
decodeStats_.numPacketsSentToDecoder ++;
1157
1151
}
1158
1152
1159
1153
if (status < AVSUCCESS) {
1160
- if (reachedEOF || status == AVERROR_EOF) {
1161
- throw SingleStreamDecoder::EndOfFileException (
1162
- " Requested next frame while there are no more frames left to "
1163
- " decode. " );
1164
- }
1165
- throw std::runtime_error (
1166
- " Could not receive frame from decoder: " +
1154
+ TORCH_CHECK (
1155
+ !(reachedEOF || status == AVERROR_EOF),
1156
+ " Requested next frame while there are no more frames left to decode. " );
1157
+
1158
+ TORCH_CHECK (
1159
+ false ,
1160
+ " Could not receive frame from decoder: " ,
1167
1161
getFFMPEGErrorStringFromErrorCode (status));
1168
1162
}
1169
1163
@@ -1429,7 +1423,7 @@ int64_t SingleStreamDecoder::secondsToIndexLowerBound(double seconds) {
1429
1423
return std::floor (seconds * streamMetadata.averageFpsFromHeader .value ());
1430
1424
}
1431
1425
default :
1432
- throw std::runtime_error ( " Unknown SeekMode" );
1426
+ TORCH_CHECK ( false , " Unknown SeekMode" );
1433
1427
}
1434
1428
}
1435
1429
@@ -1456,7 +1450,7 @@ int64_t SingleStreamDecoder::secondsToIndexUpperBound(double seconds) {
1456
1450
return std::ceil (seconds * streamMetadata.averageFpsFromHeader .value ());
1457
1451
}
1458
1452
default :
1459
- throw std::runtime_error ( " Unknown SeekMode" );
1453
+ TORCH_CHECK ( false , " Unknown SeekMode" );
1460
1454
}
1461
1455
}
1462
1456
@@ -1476,7 +1470,7 @@ int64_t SingleStreamDecoder::getPts(int64_t frameIndex) {
1476
1470
streamInfo.timeBase );
1477
1471
}
1478
1472
default :
1479
- throw std::runtime_error ( " Unknown SeekMode" );
1473
+ TORCH_CHECK ( false , " Unknown SeekMode" );
1480
1474
}
1481
1475
}
1482
1476
@@ -1493,7 +1487,7 @@ std::optional<int64_t> SingleStreamDecoder::getNumFrames(
1493
1487
return streamMetadata.numFramesFromHeader ;
1494
1488
}
1495
1489
default :
1496
- throw std::runtime_error ( " Unknown SeekMode" );
1490
+ TORCH_CHECK ( false , " Unknown SeekMode" );
1497
1491
}
1498
1492
}
1499
1493
@@ -1505,7 +1499,7 @@ double SingleStreamDecoder::getMinSeconds(
1505
1499
case SeekMode::approximate:
1506
1500
return 0 ;
1507
1501
default :
1508
- throw std::runtime_error ( " Unknown SeekMode" );
1502
+ TORCH_CHECK ( false , " Unknown SeekMode" );
1509
1503
}
1510
1504
}
1511
1505
@@ -1518,7 +1512,7 @@ std::optional<double> SingleStreamDecoder::getMaxSeconds(
1518
1512
return streamMetadata.durationSecondsFromHeader ;
1519
1513
}
1520
1514
default :
1521
- throw std::runtime_error ( " Unknown SeekMode" );
1515
+ TORCH_CHECK ( false , " Unknown SeekMode" );
1522
1516
}
1523
1517
}
1524
1518
@@ -1552,10 +1546,10 @@ void SingleStreamDecoder::validateActiveStream(
1552
1546
}
1553
1547
1554
1548
void SingleStreamDecoder::validateScannedAllStreams (const std::string& msg) {
1555
- if (!scannedAllStreams_) {
1556
- throw std::runtime_error (
1557
- " Must scan all streams to update metadata before calling " + msg);
1558
- }
1549
+ TORCH_CHECK (
1550
+ scannedAllStreams_,
1551
+ " Must scan all streams to update metadata before calling " ,
1552
+ msg);
1559
1553
}
1560
1554
1561
1555
void SingleStreamDecoder::validateFrameIndex (
0 commit comments