| 6 | #include <libavformat/avformat.h> |
| 7 | #include <libavcodec/avcodec.h> |
| 8 | } |
| 9 |
| 10 | // Pull-based reader over a single decoded video stream. |
| 11 | // |
| 12 | // Wraps the FFmpeg send/receive API and exposes getNextFrame(), which yields |
| 13 | // frames one at a time in DISPLAY (presentation) order. The decoder itself |
| 14 | // performs B-frame reordering, so callers never see decode-order packets. |
| 15 | // |
| 16 | // Scope: this class owns ONLY the per-stream decode loop (send/receive/flush/ |
| 17 | // seek). Higher-level concerns -- looping, end_time trimming, target-pts |
| 18 | // skipping -- belong to the caller and are intentionally kept out. |
| 19 | // |
| 20 | // Ownership: the reader BORROWS the format and codec contexts. The caller |
| 21 | // allocates, opens, frees them, and keeps them alive for the reader's lifetime. |
| 22 | class VideoFrameReader { |
| 23 | public: |
| 24 | enum class Status { |
| 25 | Ok, // a frame is available via frame() |
| 26 | Eof, // stream fully drained, no more frames |
| 27 | Aborted, // shouldAbort() requested a stop mid-decode |
| 28 | Error, // unrecoverable decode/demux error |
| 29 | }; |
| 30 |
| 31 | VideoFrameReader(AVFormatContext *fmt, AVCodecContext *dec, int streamIndex) |
| 32 | : m_fmt(fmt), m_dec(dec), m_streamIndex(streamIndex) { |
| 33 | m_pkt = av_packet_alloc(); |
| 34 | m_frame = av_frame_alloc(); |
| 35 | } |
| 36 |
| 37 | ~VideoFrameReader() { |
| 38 | av_frame_free(&m_frame); |
| 39 | av_packet_free(&m_pkt); |
| 40 | // m_fmt and m_dec are borrowed: not freed here. |
| 41 | } |
| 42 |
| 43 | VideoFrameReader(const VideoFrameReader &) = delete; |
| 44 | VideoFrameReader &operator=(const VideoFrameReader &) = delete; |
| 45 |
| 46 | // Optional. If set and it returns true, getNextFrame() stops promptly and |
| 47 | // returns Status::Aborted. Polled between packet reads. Use it to react to |
| 48 | // external cancellation without waiting for a blocking I/O read to unwind. |
| 49 | std::function<bool()> shouldAbort; |
| 50 |
| 51 | // Decodes and returns the next frame in display order. |
| 52 | // On Status::Ok, the frame is valid via frame() until the next call. |
| 53 | // Idempotent after Eof. |
| 54 | Status getNextFrame() { |
| 55 | if (m_pkt == nullptr || m_frame == nullptr) { |
| 56 | return Status::Error; |
| 57 | } |
| 58 |
| 59 | av_frame_unref(m_frame); |
| 60 |
| 61 | for (;;) { |
| 62 | if (shouldAbort && shouldAbort()) { |
| 63 | return Status::Aborted; |
| 64 | } |
| 65 |
| 66 | // 1. Try to pull a decoded frame first. |
| 67 | int ret = avcodec_receive_frame(m_dec, m_frame); |
| 68 | if (ret == 0) { |
| 69 | return Status::Ok; // display-order frame |
| 70 | } |
| 71 | if (ret == AVERROR_EOF) { |
| 72 | return Status::Eof; // decoder fully drained |
| 73 | } |
| 74 | if (ret != AVERROR(EAGAIN)) { |
| 75 | return Status::Error; // real decode error |
| 76 | } |
| 77 |
| 78 | // 2. EAGAIN: decoder needs more input. Because we always fully drain |
| 79 | // receive before sending, send() itself can never return EAGAIN. |
| 80 | if (m_draining) { |
| 81 | return Status::Eof; // flushed and drained |
| 82 | } |
| 83 | if (!feedNextPacket()) { |
| 84 | return Status::Error; |
| 85 | } |
| 86 | } |
| 87 | } |
| 88 |
| 89 | // Seeks so that the next getNextFrame() starts at (or just before) pts. |
| 90 | // Flushes decoder buffers and clears draining state. pts is in the video |
| 91 | // stream's time_base. Returns false on seek failure. |
| 92 | bool seek(int64_t pts, |
| 93 | int flags = AVSEEK_FLAG_BACKWARD) { |
| 94 | int ret = av_seek_frame(m_fmt, m_streamIndex, pts, flags); |
| 95 | if (ret < 0) { |
| 96 | return false; |
| 97 | } |
| 98 | avcodec_flush_buffers(m_dec); |
| 99 | av_frame_unref(m_frame); |
| 100 | m_draining = false; |
| 101 | return true; |
| 102 | } |
| 103 |
| 104 | // Valid only immediately after getNextFrame() returned Status::Ok. |
| 105 | AVFrame *frame() const { return m_frame; } |
| 106 |
| 107 | // Presentation timestamp of the current frame, in seconds (display order). |
| 108 | double frameTimeSeconds() const { |
| 109 | AVRational tb = m_fmt->streams[m_streamIndex]->time_base; |
| 110 | return m_frame->best_effort_timestamp * av_q2d(tb); |
| 111 | } |
| 112 |
| 113 | int streamIndex() const { return m_streamIndex; } |
| 114 |
| 115 | private: |
| 116 | // Reads one packet for our stream and sends it to the decoder. On |
| 117 | // end-of-input, sends a NULL flush packet and enters draining mode. |
| 118 | bool feedNextPacket() { |
| 119 | for (;;) { |
| 120 | int ret = av_read_frame(m_fmt, m_pkt); |
| 121 | if (ret < 0) { |
| 122 | avcodec_send_packet(m_dec, nullptr); // flush |
| 123 | m_draining = true; |
| 124 | return true; |
| 125 | } |
| 126 |
| 127 | if (m_pkt->stream_index != m_streamIndex) { |
| 128 | av_packet_unref(m_pkt); |
| 129 | continue; // skip other streams |
| 130 | } |
| 131 |
| 132 | ret = avcodec_send_packet(m_dec, m_pkt); |
| 133 | av_packet_unref(m_pkt); |
| 134 | if (ret < 0 && ret != AVERROR(EAGAIN)) { |
| 135 | return false; |
| 136 | } |
| 137 | return true; |
| 138 | } |
| 139 | } |
| 140 |
| 141 | AVFormatContext *m_fmt; // borrowed |
| 142 | AVCodecContext *m_dec; // borrowed |
| 143 | int m_streamIndex; |
| 144 |
| 145 | AVPacket *m_pkt = nullptr; |
| 146 | AVFrame *m_frame = nullptr; |
| 147 | bool m_draining = false; |
| 148 | }; |