File indexing completed on 2025-01-19 03:57:06

0001 /*********************************************************
0002  * Copyright (C) 2020, Val Doroshchuk <valbok@gmail.com> *
0003  *                                                       *
0004  * This file is part of QtAVPlayer.                      *
0005  * Free Qt Media Player based on FFmpeg.                 *
0006  *********************************************************/
0007 
0008 #include "qavsubtitlecodec_p.h"
0009 #include "qavcodec_p_p.h"
0010 #include <QDebug>
0011 
0012 extern "C" {
0013 #include <libavcodec/avcodec.h>
0014 }
0015 
0016 QT_BEGIN_NAMESPACE
0017 
0018 class QAVSubtitleCodecPrivate : public QAVCodecPrivate
0019 {
0020     Q_DECLARE_PUBLIC(QAVSubtitleCodec)
0021 public:
0022     QAVSubtitleCodecPrivate(QAVSubtitleCodec *q) : q_ptr(q) { }
0023 
0024     QAVSubtitleCodec *q_ptr = nullptr;
0025     QAVSubtitleFrame frame;
0026     int gotOutput = 0;
0027 };
0028 
0029 QAVSubtitleCodec::QAVSubtitleCodec()
0030     : QAVCodec(*new QAVSubtitleCodecPrivate(this))
0031 {
0032 }
0033 
0034 int QAVSubtitleCodec::write(const QAVPacket &pkt)
0035 {
0036     Q_D(QAVSubtitleCodec);
0037     if (!d->avctx)
0038         return AVERROR(EINVAL);
0039     d->frame.setStream(pkt.stream());
0040     return avcodec_decode_subtitle2(
0041         d->avctx,
0042         d->frame.subtitle(),
0043         &d->gotOutput,
0044         const_cast<AVPacket *>(pkt.packet()));
0045 }
0046 
0047 int QAVSubtitleCodec::read(QAVStreamFrame &frame)
0048 {
0049     Q_D(QAVSubtitleCodec);
0050     if (!d->avctx)
0051         return AVERROR(EINVAL);
0052     if (!d->gotOutput)
0053         return AVERROR(EAGAIN);
0054     *static_cast<QAVSubtitleFrame *>(&frame) = d->frame;
0055     d->gotOutput = 0;
0056     d->frame = {};
0057     return 0;
0058 }
0059 
0060 QT_END_NAMESPACE