| 1 | #include "cvd/videoreader.h" |
| 2 | #include "cvd/videoscaler.h" |
| 3 | |
| 4 | extern "C" |
| 5 | { |
| 6 | #include <libavcodec/avcodec.h> |
| 7 | #include <libavformat/avformat.h> |
| 8 | #include <libavutil/imgutils.h> |
| 9 | #include <libswscale/swscale.h> |
| 10 | } |
| 11 | |
| 12 | #include <utility> |
| 13 | |
| 14 | namespace CVD |
| 15 | { |
| 16 | namespace |
| 17 | { |
| 18 | using std::int64_t; |
| 19 | using std::uint8_t; |
| 20 | } |
| 21 | |
| 22 | VideoReader::VideoReader(const std::string& filename, int threads) |
| 23 | { |
| 24 | #if LIBAVFORMAT_VERSION_INT < AV_VERSION_INT(58, 9, 100) |
| 25 | // see https://github.com/FFmpeg/FFmpeg/commit/0694d8702421e7aff1340038559c438b61bb30dd |
| 26 | av_register_all(); |
| 27 | #endif |
| 28 | #if LIBAVCODEC_VERSION_INT < AV_VERSION_INT(58, 10, 100) |
| 29 | // see https://github.com/FFmpeg/FFmpeg/commit/3f0a41367eb9180ab6d22d43ad42b9bd85a26df0 |
| 30 | avcodec_register_all(); |
| 31 | #endif |
| 32 | |
| 33 | AVDictionary* opts = nullptr; |
| 34 | AVFormatContext* formatContext = nullptr; |
| 35 | int result = avformat_open_input(&formatContext, filename.c_str(), nullptr, &opts); |
| 36 | internal::CheckAVError(result); |
| 37 | m_format_context.reset(formatContext); |
| 38 | |
| 39 | result = avformat_find_stream_info(m_format_context.get(), nullptr); |
| 40 | internal::CheckAVError(result); |
| 41 | AVCodecParameters* parameters = nullptr; |
| 42 | for(unsigned int i = 0; i != m_format_context->nb_streams; ++i) |
| 43 | { |
| 44 | if(m_format_context->streams[i]->codecpar->codec_type == AVMEDIA_TYPE_VIDEO) |
| 45 | { |
| 46 | parameters = m_format_context->streams[i]->codecpar; |
| 47 | m_stream_index = i; |
| 48 | break; |
| 49 | } |
| 50 | } |
| 51 | if(!parameters) |
| 52 | { |
| 53 | throw std::runtime_error("No video stream found in video file"); |
| 54 | } |
| 55 | auto codec = avcodec_find_decoder(parameters->codec_id); |
| 56 | if(!codec) |
| 57 | { |
| 58 | throw std::runtime_error("No video decoder found for video codec"); |
| 59 | } |
| 60 | |
| 61 | m_codec_context.reset(avcodec_alloc_context3(codec)); |
| 62 | avcodec_parameters_to_context(m_codec_context.get(), parameters); |
| 63 | m_codec_context->thread_count = threads; |
| 64 | result = avcodec_open2(m_codec_context.get(), codec, 0); |
| 65 | internal::CheckAVError(result); |
| 66 | |
| 67 | int colorspace = -1; |
| 68 | switch(m_codec_context->color_primaries) |
| 69 | { |
| 70 | case AVCOL_PRI_BT709: |
| 71 | { |
| 72 | colorspace = SWS_CS_ITU709; |
| 73 | break; |
| 74 | } |
| 75 | case AVCOL_PRI_BT470M: |
| 76 | case AVCOL_PRI_BT470BG: |
| 77 | case AVCOL_PRI_SMPTE170M: |
| 78 | case AVCOL_PRI_SMPTE240M: |
| 79 | case AVCOL_PRI_UNSPECIFIED: |
| 80 | // Rec. 601 color conversion is the FFmpeg default. |
| 81 | break; |
| 82 | default: |
| 83 | throw std::runtime_error("Unknown color primaries for video"); |
| 84 | } |
| 85 | |
| 86 | m_scaler = std::make_unique<internal::Scaler>( |
| 87 | m_codec_context->width, m_codec_context->height, m_codec_context->pix_fmt, AV_PIX_FMT_RGBA, colorspace, threads); |
| 88 | |
| 89 | m_raw_frame.reset(av_frame_alloc()); |
| 90 | } |
| 91 | |
| 92 | VideoReader::~VideoReader() = default; |
| 93 | |
| 94 | AVRational VideoReader::timebase() const |
| 95 | { |
| 96 | return m_format_context->streams[m_stream_index]->time_base; |
| 97 | } |
| 98 | |
| 99 | double VideoReader::timebase_to_seconds(int64_t pts) const |
| 100 | { |
| 101 | auto video_timebase = timebase(); |
| 102 | return static_cast<double>(pts * video_timebase.num) / video_timebase.den; |
| 103 | } |
| 104 | |
| 105 | ImageRef VideoReader::size() const |
| 106 | { |
| 107 | return { m_codec_context->width, m_codec_context->height }; |
| 108 | } |
| 109 | |
| 110 | AVRational VideoReader::frame_rate() const |
| 111 | { |
| 112 | return m_format_context->streams[m_stream_index]->r_frame_rate; |
| 113 | } |
| 114 | |
| 115 | int64_t VideoReader::duration() const |
| 116 | { |
| 117 | return m_format_context->streams[m_stream_index]->duration; |
| 118 | } |
| 119 | |
| 120 | std::pair<Image<Rgba<uint8_t>>, int64_t> VideoReader::get_frame() |
| 121 | { |
| 122 | for(;;) |
| 123 | { |
| 124 | int r = avcodec_receive_frame(m_codec_context.get(), m_raw_frame.get()); |
| 125 | if(r >= 0) |
| 126 | { |
| 127 | break; |
| 128 | } |
| 129 | if(r == AVERROR_EOF) |
| 130 | { |
| 131 | return {}; |
| 132 | } |
| 133 | if(r != AVERROR(EAGAIN)) |
| 134 | { |
| 135 | internal::CheckAVError(r); |
| 136 | } |
| 137 | |
| 138 | std::unique_ptr<AVPacket, internal::AVPacketDeleter> packet(av_packet_alloc()); |
| 139 | r = av_read_frame(m_format_context.get(), packet.get()); |
| 140 | // EOF packets need to be sent to flush the last frame |
| 141 | if(r != AVERROR_EOF) |
| 142 | { |
| 143 | internal::CheckAVError(r); |
| 144 | |
| 145 | if(packet->stream_index != m_stream_index) |
| 146 | { |
| 147 | continue; |
| 148 | } |
| 149 | r = avcodec_send_packet(m_codec_context.get(), packet.get()); |
| 150 | if(r != AVERROR(EAGAIN) && r != AVERROR(EOF)) |
| 151 | { |
| 152 | internal::CheckAVError(r); |
| 153 | } |
| 154 | } |
| 155 | else |
| 156 | { |
| 157 | avcodec_send_packet(m_codec_context.get(), nullptr); |
| 158 | } |
| 159 | } |
| 160 | |
| 161 | Image<Rgba<uint8_t>> result(CVD::ImageRef(m_codec_context->width, m_codec_context->height)); |
| 162 | |
| 163 | uint8_t* data[4]; |
| 164 | int linesize[4]; |
| 165 | av_image_fill_arrays(data, |
| 166 | linesize, |
| 167 | reinterpret_cast<uint8_t*>(result.data()), |
| 168 | AV_PIX_FMT_RGBA, |
| 169 | result.size().x, |
| 170 | result.size().y, |
| 171 | 1); |
| 172 | m_scaler->scale(m_raw_frame->data, m_raw_frame->linesize, 0, m_codec_context->height, data, linesize); |
| 173 | return { std::move(result), m_raw_frame->pts }; |
| 174 | } |
| 175 | |
| 176 | } |