| 543 | } |
| 544 | |
| 545 | bool FramesDecoderGpu::ReadNextFrameWithIndex(uint8_t *data) { |
| 546 | bool copy_to_output = data != nullptr; |
| 547 | |
| 548 | // No more frames to read |
| 549 | if (next_frame_idx_ >= NumFrames()) { |
| 550 | next_frame_idx_ = -1; |
| 551 | return false; |
| 552 | } |
| 553 | |
| 554 | // Check if requested frame was buffered earlier |
| 555 | assert(HasIndex()); |
| 556 | for (auto &frame : frame_buffer_) { |
| 557 | if (frame.pts_ != -1 && frame.pts_ == index_[next_frame_idx_].pts) { |
| 558 | if (copy_to_output) { |
| 559 | LOG_LINE << "Copying from frame buffer to frame_output" << std::endl; |
| 560 | copyD2D(data, frame.frame_.data(), FrameSize(), stream_); |
| 561 | } |
| 562 | LOG_LINE << "Found buffered frame with pts=" << frame.pts_ << std::endl; |
| 563 | |
| 564 | frame.pts_ = -1; |
| 565 | next_frame_idx_ = next_frame_idx_ == NumFrames() - 1 ? -1 : next_frame_idx_ + 1; |
| 566 | return true; |
| 567 | } |
| 568 | } |
| 569 | |
| 570 | current_copy_to_output_ = copy_to_output; |
| 571 | current_frame_output_ = data; |
| 572 | |
| 573 | while (next_frame_idx_ < NumFrames()) { |
| 574 | int ret = av_read_frame(ctx_, packet_); |
| 575 | auto packet = AVPacketScope(packet_, av_packet_unref); |
| 576 | if (ret != 0) { |
| 577 | LOG_LINE << "Hit EOF, sending last packet with " << piped_pts_.size() |
| 578 | << " frames in pipeline, " << NumBufferedFrames() << " buffered frames, " |
| 579 | << "next_frame_idx=" << next_frame_idx_ << " of " << NumFrames() << std::endl; |
| 580 | break; |
| 581 | } |
| 582 | |
| 583 | if (!SendFrameToParser()) { |
| 584 | continue; |
| 585 | } else { |
| 586 | packet.release(); // ownership is transferred via av_bsf_send_packet |
| 587 | } |
| 588 | |
| 589 | if (frame_returned_) { |
| 590 | next_frame_idx_ = next_frame_idx_ == NumFrames() - 1 ? -1 : next_frame_idx_ + 1; |
| 591 | return true; |
| 592 | } |
| 593 | } |
| 594 | |
| 595 | // At this point we've hit EOF but might have frames still in the pipeline |
| 596 | DALI_ENFORCE(piped_pts_.size() >= 1); |
| 597 | |
| 598 | SendLastPacket(); |
| 599 | |
| 600 | // Check if we got the frame we wanted during SendLastPacket |
| 601 | if (frame_returned_) { |
| 602 | next_frame_idx_ = next_frame_idx_ == NumFrames() - 1 ? -1 : next_frame_idx_ + 1; |