@@ -18,25 +18,6 @@ constexpr size_t kIoBufferSize = 96 * 1024;
18
18
constexpr size_t kIoPaddingSize = AV_INPUT_BUFFER_PADDING_SIZE;
19
19
constexpr size_t kLogBufferSize = 1024 ;
20
20
21
- int ffmpeg_lock (void ** mutex, enum AVLockOp op) {
22
- std::mutex** handle = (std::mutex**)mutex;
23
- switch (op) {
24
- case AV_LOCK_CREATE:
25
- *handle = new std::mutex ();
26
- break ;
27
- case AV_LOCK_OBTAIN:
28
- (*handle)->lock ();
29
- break ;
30
- case AV_LOCK_RELEASE:
31
- (*handle)->unlock ();
32
- break ;
33
- case AV_LOCK_DESTROY:
34
- delete *handle;
35
- break ;
36
- }
37
- return 0 ;
38
- }
39
-
40
21
bool mapFfmpegType (AVMediaType media, MediaType* type) {
41
22
switch (media) {
42
23
case AVMEDIA_TYPE_AUDIO:
@@ -202,8 +183,6 @@ void Decoder::initOnce() {
202
183
avcodec_register_all ();
203
184
#endif
204
185
avformat_network_init ();
205
- // register ffmpeg lock manager
206
- av_lockmgr_register (&ffmpeg_lock);
207
186
av_log_set_callback (Decoder::logFunction);
208
187
av_log_set_level (AV_LOG_ERROR);
209
188
VLOG (1 ) << " Registered ffmpeg libs" ;
@@ -277,7 +256,7 @@ bool Decoder::init(
277
256
break ;
278
257
}
279
258
280
- fmt = av_find_input_format (fmtName);
259
+ fmt = (AVInputFormat*) av_find_input_format (fmtName);
281
260
}
282
261
283
262
const size_t avioCtxBufferSize = kIoBufferSize ;
@@ -495,8 +474,8 @@ void Decoder::cleanUp() {
495
474
496
475
// function does actual work, derived class calls it in working thread
497
476
// periodically. On success method returns 0, ENODATA on EOF, ETIMEDOUT if
498
- // no frames got decoded in the specified timeout time, and error on
499
- // unrecoverable error.
477
+ // no frames got decoded in the specified timeout time, AVERROR_BUFFER_TOO_SMALL
478
+ // when unable to allocate packet and error on unrecoverable error
500
479
int Decoder::getFrame (size_t workingTimeInMs) {
501
480
if (inRange_.none ()) {
502
481
return ENODATA;
@@ -505,10 +484,15 @@ int Decoder::getFrame(size_t workingTimeInMs) {
505
484
// once decode() method gets called and grab some bytes
506
485
// run this method again
507
486
// init package
508
- AVPacket avPacket;
509
- av_init_packet (&avPacket);
510
- avPacket.data = nullptr ;
511
- avPacket.size = 0 ;
487
+ // update 03/22: moving memory management to ffmpeg
488
+ AVPacket* avPacket;
489
+ avPacket = av_packet_alloc ();
490
+ if (avPacket == nullptr ) {
491
+ LOG (ERROR) << " decoder as not able to allocate the packet." ;
492
+ return AVERROR_BUFFER_TOO_SMALL;
493
+ }
494
+ avPacket->data = nullptr ;
495
+ avPacket->size = 0 ;
512
496
513
497
auto end = std::chrono::steady_clock::now () +
514
498
std::chrono::milliseconds (workingTimeInMs);
@@ -520,8 +504,12 @@ int Decoder::getFrame(size_t workingTimeInMs) {
520
504
int result = 0 ;
521
505
size_t decodingErrors = 0 ;
522
506
bool decodedFrame = false ;
523
- while (!interrupted_ && inRange_.any () && !decodedFrame && watcher ()) {
524
- result = av_read_frame (inputCtx_, &avPacket);
507
+ while (!interrupted_ && inRange_.any () && !decodedFrame) {
508
+ if (watcher () == false ) {
509
+ result = ETIMEDOUT;
510
+ break ;
511
+ }
512
+ result = av_read_frame (inputCtx_, avPacket);
525
513
if (result == AVERROR (EAGAIN)) {
526
514
VLOG (4 ) << " Decoder is busy..." ;
527
515
std::this_thread::yield ();
@@ -538,10 +526,11 @@ int Decoder::getFrame(size_t workingTimeInMs) {
538
526
break ;
539
527
}
540
528
541
- // get stream
542
- auto stream = findByIndex (avPacket.stream_index );
529
+ // get stream; if stream cannot be found reset the packet to
530
+ // default settings
531
+ auto stream = findByIndex (avPacket->stream_index );
543
532
if (stream == nullptr || !inRange_.test (stream->getIndex ())) {
544
- av_packet_unref (& avPacket);
533
+ av_packet_unref (avPacket);
545
534
continue ;
546
535
}
547
536
@@ -553,7 +542,7 @@ int Decoder::getFrame(size_t workingTimeInMs) {
553
542
bool hasMsg = false ;
554
543
// packet either got consumed completely or not at all
555
544
if ((result = processPacket (
556
- stream, & avPacket, &gotFrame, &hasMsg, params_.fastSeek )) < 0 ) {
545
+ stream, avPacket, &gotFrame, &hasMsg, params_.fastSeek )) < 0 ) {
557
546
LOG (ERROR) << " processPacket failed with code: " << result;
558
547
break ;
559
548
}
@@ -585,20 +574,18 @@ int Decoder::getFrame(size_t workingTimeInMs) {
585
574
586
575
result = 0 ;
587
576
588
- av_packet_unref (& avPacket);
577
+ av_packet_unref (avPacket);
589
578
}
590
579
591
- av_packet_unref (&avPacket);
592
-
580
+ av_packet_free (&avPacket);
593
581
VLOG (2 ) << " Interrupted loop"
594
582
<< " , interrupted_ " << interrupted_ << " , inRange_.any() "
595
583
<< inRange_.any () << " , decodedFrame " << decodedFrame << " , result "
596
584
<< result;
597
585
598
586
// loop can be terminated, either by:
599
587
// 1. explcitly iterrupted
600
- // 2. terminated by workable timeout
601
- // 3. unrecoverable error or ENODATA (end of stream)
588
+ // 3. unrecoverable error or ENODATA (end of stream) or ETIMEDOUT (timeout)
602
589
// 4. decoded frames pts are out of the specified range
603
590
// 5. success decoded frame
604
591
if (interrupted_) {
0 commit comments